1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
6 #include <drm/drm_atomic_state_helper.h>
10 #include "i915_utils.h"
11 #include "intel_atomic.h"
13 #include "intel_cdclk.h"
14 #include "intel_display_core.h"
15 #include "intel_display_types.h"
16 #include "skl_watermark.h"
17 #include "intel_mchbar_regs.h"
18 #include "intel_pcode.h"
20 /* Parameters for Qclk Geyserville (QGV) */
21 struct intel_qgv_point {
22 u16 dclk, t_rp, t_rdpre, t_rc, t_ras, t_rcd;
25 #define DEPROGBWPCLIMIT 60
27 struct intel_psf_gv_point {
28 u8 clk; /* clock in multiples of 16.6666 MHz */
31 struct intel_qgv_info {
32 struct intel_qgv_point points[I915_NUM_QGV_POINTS];
33 struct intel_psf_gv_point psf_points[I915_NUM_PSF_GV_POINTS];
42 static int dg1_mchbar_read_qgv_point_info(struct drm_i915_private *dev_priv,
43 struct intel_qgv_point *sp,
46 u32 dclk_ratio, dclk_reference;
49 val = intel_uncore_read(&dev_priv->uncore, SA_PERF_STATUS_0_0_0_MCHBAR_PC);
50 dclk_ratio = REG_FIELD_GET(DG1_QCLK_RATIO_MASK, val);
51 if (val & DG1_QCLK_REFERENCE)
52 dclk_reference = 6; /* 6 * 16.666 MHz = 100 MHz */
54 dclk_reference = 8; /* 8 * 16.666 MHz = 133 MHz */
55 sp->dclk = DIV_ROUND_UP((16667 * dclk_ratio * dclk_reference) + 500, 1000);
57 val = intel_uncore_read(&dev_priv->uncore, SKL_MC_BIOS_DATA_0_0_0_MCHBAR_PCU);
58 if (val & DG1_GEAR_TYPE)
64 val = intel_uncore_read(&dev_priv->uncore, MCHBAR_CH0_CR_TC_PRE_0_0_0_MCHBAR);
65 sp->t_rp = REG_FIELD_GET(DG1_DRAM_T_RP_MASK, val);
66 sp->t_rdpre = REG_FIELD_GET(DG1_DRAM_T_RDPRE_MASK, val);
68 val = intel_uncore_read(&dev_priv->uncore, MCHBAR_CH0_CR_TC_PRE_0_0_0_MCHBAR_HIGH);
69 sp->t_rcd = REG_FIELD_GET(DG1_DRAM_T_RCD_MASK, val);
70 sp->t_ras = REG_FIELD_GET(DG1_DRAM_T_RAS_MASK, val);
72 sp->t_rc = sp->t_rp + sp->t_ras;
77 static int icl_pcode_read_qgv_point_info(struct drm_i915_private *dev_priv,
78 struct intel_qgv_point *sp,
81 u32 val = 0, val2 = 0;
85 ret = snb_pcode_read(&dev_priv->uncore, ICL_PCODE_MEM_SUBSYSYSTEM_INFO |
86 ICL_PCODE_MEM_SS_READ_QGV_POINT_INFO(point),
92 sp->dclk = DIV_ROUND_UP((16667 * dclk) + (DISPLAY_VER(dev_priv) >= 12 ? 500 : 0),
94 sp->t_rp = (val & 0xff0000) >> 16;
95 sp->t_rcd = (val & 0xff000000) >> 24;
97 sp->t_rdpre = val2 & 0xff;
98 sp->t_ras = (val2 & 0xff00) >> 8;
100 sp->t_rc = sp->t_rp + sp->t_ras;
105 static int adls_pcode_read_psf_gv_point_info(struct drm_i915_private *dev_priv,
106 struct intel_psf_gv_point *points)
112 ret = snb_pcode_read(&dev_priv->uncore, ICL_PCODE_MEM_SUBSYSYSTEM_INFO |
113 ADL_PCODE_MEM_SS_READ_PSF_GV_INFO, &val, NULL);
117 for (i = 0; i < I915_NUM_PSF_GV_POINTS; i++) {
118 points[i].clk = val & 0xff;
125 static u16 icl_qgv_points_mask(struct drm_i915_private *i915)
127 unsigned int num_psf_gv_points = i915->display.bw.max[0].num_psf_gv_points;
128 unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
129 u16 qgv_points = 0, psf_points = 0;
132 * We can _not_ use the whole ADLS_QGV_PT_MASK here, as PCode rejects
133 * it with failure if we try masking any unadvertised points.
134 * So need to operate only with those returned from PCode.
136 if (num_qgv_points > 0)
137 qgv_points = GENMASK(num_qgv_points - 1, 0);
139 if (num_psf_gv_points > 0)
140 psf_points = GENMASK(num_psf_gv_points - 1, 0);
142 return ICL_PCODE_REQ_QGV_PT(qgv_points) | ADLS_PCODE_REQ_PSF_PT(psf_points);
145 static bool is_sagv_enabled(struct drm_i915_private *i915, u16 points_mask)
147 return !is_power_of_2(~points_mask & icl_qgv_points_mask(i915) &
148 ICL_PCODE_REQ_QGV_PT_MASK);
151 int icl_pcode_restrict_qgv_points(struct drm_i915_private *dev_priv,
156 if (DISPLAY_VER(dev_priv) >= 14)
159 /* bspec says to keep retrying for at least 1 ms */
160 ret = skl_pcode_request(&dev_priv->uncore, ICL_PCODE_SAGV_DE_MEM_SS_CONFIG,
162 ICL_PCODE_REP_QGV_MASK | ADLS_PCODE_REP_PSF_MASK,
163 ICL_PCODE_REP_QGV_SAFE | ADLS_PCODE_REP_PSF_SAFE,
167 drm_err(&dev_priv->drm,
168 "Failed to disable qgv points (0x%x) points: 0x%x\n",
173 dev_priv->display.sagv.status = is_sagv_enabled(dev_priv, points_mask) ?
174 I915_SAGV_ENABLED : I915_SAGV_DISABLED;
179 static int mtl_read_qgv_point_info(struct drm_i915_private *dev_priv,
180 struct intel_qgv_point *sp, int point)
185 val = intel_uncore_read(&dev_priv->uncore,
186 MTL_MEM_SS_INFO_QGV_POINT_LOW(point));
187 val2 = intel_uncore_read(&dev_priv->uncore,
188 MTL_MEM_SS_INFO_QGV_POINT_HIGH(point));
189 dclk = REG_FIELD_GET(MTL_DCLK_MASK, val);
190 sp->dclk = DIV_ROUND_CLOSEST(16667 * dclk, 1000);
191 sp->t_rp = REG_FIELD_GET(MTL_TRP_MASK, val);
192 sp->t_rcd = REG_FIELD_GET(MTL_TRCD_MASK, val);
194 sp->t_rdpre = REG_FIELD_GET(MTL_TRDPRE_MASK, val2);
195 sp->t_ras = REG_FIELD_GET(MTL_TRAS_MASK, val2);
197 sp->t_rc = sp->t_rp + sp->t_ras;
203 intel_read_qgv_point_info(struct drm_i915_private *dev_priv,
204 struct intel_qgv_point *sp,
207 if (DISPLAY_VER(dev_priv) >= 14)
208 return mtl_read_qgv_point_info(dev_priv, sp, point);
209 else if (IS_DG1(dev_priv))
210 return dg1_mchbar_read_qgv_point_info(dev_priv, sp, point);
212 return icl_pcode_read_qgv_point_info(dev_priv, sp, point);
215 static int icl_get_qgv_points(struct drm_i915_private *dev_priv,
216 struct intel_qgv_info *qi,
219 const struct dram_info *dram_info = &dev_priv->dram_info;
222 qi->num_points = dram_info->num_qgv_points;
223 qi->num_psf_points = dram_info->num_psf_gv_points;
225 if (DISPLAY_VER(dev_priv) >= 14) {
226 switch (dram_info->type) {
227 case INTEL_DRAM_DDR4:
229 qi->max_numchannels = 2;
230 qi->channel_width = 64;
231 qi->deinterleave = 2;
233 case INTEL_DRAM_DDR5:
235 qi->max_numchannels = 4;
236 qi->channel_width = 32;
237 qi->deinterleave = 2;
239 case INTEL_DRAM_LPDDR4:
240 case INTEL_DRAM_LPDDR5:
242 qi->max_numchannels = 8;
243 qi->channel_width = 16;
244 qi->deinterleave = 4;
246 case INTEL_DRAM_GDDR:
247 qi->channel_width = 32;
250 MISSING_CASE(dram_info->type);
253 } else if (DISPLAY_VER(dev_priv) >= 12) {
254 switch (dram_info->type) {
255 case INTEL_DRAM_DDR4:
256 qi->t_bl = is_y_tile ? 8 : 4;
257 qi->max_numchannels = 2;
258 qi->channel_width = 64;
259 qi->deinterleave = is_y_tile ? 1 : 2;
261 case INTEL_DRAM_DDR5:
262 qi->t_bl = is_y_tile ? 16 : 8;
263 qi->max_numchannels = 4;
264 qi->channel_width = 32;
265 qi->deinterleave = is_y_tile ? 1 : 2;
267 case INTEL_DRAM_LPDDR4:
268 if (IS_ROCKETLAKE(dev_priv)) {
270 qi->max_numchannels = 4;
271 qi->channel_width = 32;
272 qi->deinterleave = 2;
276 case INTEL_DRAM_LPDDR5:
278 qi->max_numchannels = 8;
279 qi->channel_width = 16;
280 qi->deinterleave = is_y_tile ? 2 : 4;
284 qi->max_numchannels = 1;
287 } else if (DISPLAY_VER(dev_priv) == 11) {
288 qi->t_bl = dev_priv->dram_info.type == INTEL_DRAM_DDR4 ? 4 : 8;
289 qi->max_numchannels = 1;
292 if (drm_WARN_ON(&dev_priv->drm,
293 qi->num_points > ARRAY_SIZE(qi->points)))
294 qi->num_points = ARRAY_SIZE(qi->points);
296 for (i = 0; i < qi->num_points; i++) {
297 struct intel_qgv_point *sp = &qi->points[i];
299 ret = intel_read_qgv_point_info(dev_priv, sp, i);
301 drm_dbg_kms(&dev_priv->drm, "Could not read QGV %d info\n", i);
305 drm_dbg_kms(&dev_priv->drm,
306 "QGV %d: DCLK=%d tRP=%d tRDPRE=%d tRAS=%d tRCD=%d tRC=%d\n",
307 i, sp->dclk, sp->t_rp, sp->t_rdpre, sp->t_ras,
308 sp->t_rcd, sp->t_rc);
311 if (qi->num_psf_points > 0) {
312 ret = adls_pcode_read_psf_gv_point_info(dev_priv, qi->psf_points);
314 drm_err(&dev_priv->drm, "Failed to read PSF point data; PSF points will not be considered in bandwidth calculations.\n");
315 qi->num_psf_points = 0;
318 for (i = 0; i < qi->num_psf_points; i++)
319 drm_dbg_kms(&dev_priv->drm,
320 "PSF GV %d: CLK=%d \n",
321 i, qi->psf_points[i].clk);
327 static int adl_calc_psf_bw(int clk)
330 * clk is multiples of 16.666MHz (100/6)
331 * According to BSpec PSF GV bandwidth is
332 * calculated as BW = 64 * clk * 16.666Mhz
334 return DIV_ROUND_CLOSEST(64 * clk * 100, 6);
337 static int icl_sagv_max_dclk(const struct intel_qgv_info *qi)
342 for (i = 0; i < qi->num_points; i++)
343 dclk = max(dclk, qi->points[i].dclk);
348 struct intel_sa_info {
350 u8 deburst, deprogbwlimit, derating;
353 static const struct intel_sa_info icl_sa_info = {
355 .deprogbwlimit = 25, /* GB/s */
360 static const struct intel_sa_info tgl_sa_info = {
362 .deprogbwlimit = 34, /* GB/s */
367 static const struct intel_sa_info rkl_sa_info = {
369 .deprogbwlimit = 20, /* GB/s */
374 static const struct intel_sa_info adls_sa_info = {
376 .deprogbwlimit = 38, /* GB/s */
381 static const struct intel_sa_info adlp_sa_info = {
383 .deprogbwlimit = 38, /* GB/s */
388 static const struct intel_sa_info mtl_sa_info = {
390 .deprogbwlimit = 38, /* GB/s */
395 static const struct intel_sa_info xe2_hpd_sa_info = {
398 /* Other values not used by simplified algorithm */
401 static int icl_get_bw_info(struct drm_i915_private *dev_priv, const struct intel_sa_info *sa)
403 struct intel_qgv_info qi = {};
404 bool is_y_tile = true; /* assume y tile may be used */
405 int num_channels = max_t(u8, 1, dev_priv->dram_info.num_channels);
406 int ipqdepth, ipqdepthpch = 16;
409 int num_groups = ARRAY_SIZE(dev_priv->display.bw.max);
412 ret = icl_get_qgv_points(dev_priv, &qi, is_y_tile);
414 drm_dbg_kms(&dev_priv->drm,
415 "Failed to get memory subsystem information, ignoring bandwidth limits");
419 dclk_max = icl_sagv_max_dclk(&qi);
420 maxdebw = min(sa->deprogbwlimit * 1000, dclk_max * 16 * 6 / 10);
421 ipqdepth = min(ipqdepthpch, sa->displayrtids / num_channels);
422 qi.deinterleave = DIV_ROUND_UP(num_channels, is_y_tile ? 4 : 2);
424 for (i = 0; i < num_groups; i++) {
425 struct intel_bw_info *bi = &dev_priv->display.bw.max[i];
429 clpchgroup = (sa->deburst * qi.deinterleave / num_channels) << i;
430 bi->num_planes = (ipqdepth - clpchgroup) / clpchgroup + 1;
432 bi->num_qgv_points = qi.num_points;
433 bi->num_psf_gv_points = qi.num_psf_points;
435 for (j = 0; j < qi.num_points; j++) {
436 const struct intel_qgv_point *sp = &qi.points[j];
442 * FIXME what is the logic behind the
443 * assumed burst length?
445 ct = max_t(int, sp->t_rc, sp->t_rp + sp->t_rcd +
446 (clpchgroup - 1) * qi.t_bl + sp->t_rdpre);
447 bw = DIV_ROUND_UP(sp->dclk * clpchgroup * 32 * num_channels, ct);
449 bi->deratedbw[j] = min(maxdebw,
450 bw * (100 - sa->derating) / 100);
452 drm_dbg_kms(&dev_priv->drm,
453 "BW%d / QGV %d: num_planes=%d deratedbw=%u\n",
454 i, j, bi->num_planes, bi->deratedbw[j]);
458 * In case if SAGV is disabled in BIOS, we always get 1
459 * SAGV point, but we can't send PCode commands to restrict it
460 * as it will fail and pointless anyway.
462 if (qi.num_points == 1)
463 dev_priv->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
465 dev_priv->display.sagv.status = I915_SAGV_ENABLED;
470 static int tgl_get_bw_info(struct drm_i915_private *dev_priv, const struct intel_sa_info *sa)
472 struct intel_qgv_info qi = {};
473 const struct dram_info *dram_info = &dev_priv->dram_info;
474 bool is_y_tile = true; /* assume y tile may be used */
475 int num_channels = max_t(u8, 1, dev_priv->dram_info.num_channels);
476 int ipqdepth, ipqdepthpch = 16;
480 int num_groups = ARRAY_SIZE(dev_priv->display.bw.max);
483 ret = icl_get_qgv_points(dev_priv, &qi, is_y_tile);
485 drm_dbg_kms(&dev_priv->drm,
486 "Failed to get memory subsystem information, ignoring bandwidth limits");
490 if (DISPLAY_VER(dev_priv) < 14 &&
491 (dram_info->type == INTEL_DRAM_LPDDR4 || dram_info->type == INTEL_DRAM_LPDDR5))
494 qi.deinterleave = qi.deinterleave ? : DIV_ROUND_UP(num_channels, is_y_tile ? 4 : 2);
496 if (num_channels < qi.max_numchannels && DISPLAY_VER(dev_priv) >= 12)
497 qi.deinterleave = max(DIV_ROUND_UP(qi.deinterleave, 2), 1);
499 if (DISPLAY_VER(dev_priv) >= 12 && num_channels > qi.max_numchannels)
500 drm_warn(&dev_priv->drm, "Number of channels exceeds max number of channels.");
501 if (qi.max_numchannels != 0)
502 num_channels = min_t(u8, num_channels, qi.max_numchannels);
504 dclk_max = icl_sagv_max_dclk(&qi);
506 peakbw = num_channels * DIV_ROUND_UP(qi.channel_width, 8) * dclk_max;
507 maxdebw = min(sa->deprogbwlimit * 1000, peakbw * DEPROGBWPCLIMIT / 100);
509 ipqdepth = min(ipqdepthpch, sa->displayrtids / num_channels);
511 * clperchgroup = 4kpagespermempage * clperchperblock,
512 * clperchperblock = 8 / num_channels * interleave
514 clperchgroup = 4 * DIV_ROUND_UP(8, num_channels) * qi.deinterleave;
516 for (i = 0; i < num_groups; i++) {
517 struct intel_bw_info *bi = &dev_priv->display.bw.max[i];
518 struct intel_bw_info *bi_next;
522 clpchgroup = (sa->deburst * qi.deinterleave / num_channels) << i;
524 if (i < num_groups - 1) {
525 bi_next = &dev_priv->display.bw.max[i + 1];
527 if (clpchgroup < clperchgroup)
528 bi_next->num_planes = (ipqdepth - clpchgroup) /
531 bi_next->num_planes = 0;
534 bi->num_qgv_points = qi.num_points;
535 bi->num_psf_gv_points = qi.num_psf_points;
537 for (j = 0; j < qi.num_points; j++) {
538 const struct intel_qgv_point *sp = &qi.points[j];
544 * FIXME what is the logic behind the
545 * assumed burst length?
547 ct = max_t(int, sp->t_rc, sp->t_rp + sp->t_rcd +
548 (clpchgroup - 1) * qi.t_bl + sp->t_rdpre);
549 bw = DIV_ROUND_UP(sp->dclk * clpchgroup * 32 * num_channels, ct);
551 bi->deratedbw[j] = min(maxdebw,
552 bw * (100 - sa->derating) / 100);
553 bi->peakbw[j] = DIV_ROUND_CLOSEST(sp->dclk *
555 qi.channel_width, 8);
557 drm_dbg_kms(&dev_priv->drm,
558 "BW%d / QGV %d: num_planes=%d deratedbw=%u peakbw: %u\n",
559 i, j, bi->num_planes, bi->deratedbw[j],
563 for (j = 0; j < qi.num_psf_points; j++) {
564 const struct intel_psf_gv_point *sp = &qi.psf_points[j];
566 bi->psf_bw[j] = adl_calc_psf_bw(sp->clk);
568 drm_dbg_kms(&dev_priv->drm,
569 "BW%d / PSF GV %d: num_planes=%d bw=%u\n",
570 i, j, bi->num_planes, bi->psf_bw[j]);
575 * In case if SAGV is disabled in BIOS, we always get 1
576 * SAGV point, but we can't send PCode commands to restrict it
577 * as it will fail and pointless anyway.
579 if (qi.num_points == 1)
580 dev_priv->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
582 dev_priv->display.sagv.status = I915_SAGV_ENABLED;
587 static void dg2_get_bw_info(struct drm_i915_private *i915)
589 unsigned int deratedbw = IS_DG2_G11(i915) ? 38000 : 50000;
590 int num_groups = ARRAY_SIZE(i915->display.bw.max);
594 * DG2 doesn't have SAGV or QGV points, just a constant max bandwidth
595 * that doesn't depend on the number of planes enabled. So fill all the
596 * plane group with constant bw information for uniformity with other
597 * platforms. DG2-G10 platforms have a constant 50 GB/s bandwidth,
598 * whereas DG2-G11 platforms have 38 GB/s.
600 for (i = 0; i < num_groups; i++) {
601 struct intel_bw_info *bi = &i915->display.bw.max[i];
604 /* Need only one dummy QGV point per group */
605 bi->num_qgv_points = 1;
606 bi->deratedbw[0] = deratedbw;
609 i915->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
612 static int xe2_hpd_get_bw_info(struct drm_i915_private *i915,
613 const struct intel_sa_info *sa)
615 struct intel_qgv_info qi = {};
616 int num_channels = i915->dram_info.num_channels;
620 ret = icl_get_qgv_points(i915, &qi, true);
622 drm_dbg_kms(&i915->drm,
623 "Failed to get memory subsystem information, ignoring bandwidth limits");
627 peakbw = num_channels * qi.channel_width / 8 * icl_sagv_max_dclk(&qi);
628 maxdebw = min(sa->deprogbwlimit * 1000, peakbw * DEPROGBWPCLIMIT / 10);
630 for (i = 0; i < qi.num_points; i++) {
631 const struct intel_qgv_point *point = &qi.points[i];
632 int bw = num_channels * (qi.channel_width / 8) * point->dclk;
634 i915->display.bw.max[0].deratedbw[i] =
635 min(maxdebw, (100 - sa->derating) * bw / 100);
636 i915->display.bw.max[0].peakbw[i] = bw;
638 drm_dbg_kms(&i915->drm, "QGV %d: deratedbw=%u peakbw: %u\n",
639 i, i915->display.bw.max[0].deratedbw[i],
640 i915->display.bw.max[0].peakbw[i]);
643 /* Bandwidth does not depend on # of planes; set all groups the same */
644 i915->display.bw.max[0].num_planes = 1;
645 i915->display.bw.max[0].num_qgv_points = qi.num_points;
646 for (i = 1; i < ARRAY_SIZE(i915->display.bw.max); i++)
647 memcpy(&i915->display.bw.max[i], &i915->display.bw.max[0],
648 sizeof(i915->display.bw.max[0]));
651 * Xe2_HPD should always have exactly two QGV points representing
652 * battery and plugged-in operation.
654 drm_WARN_ON(&i915->drm, qi.num_points != 2);
655 i915->display.sagv.status = I915_SAGV_ENABLED;
660 static unsigned int icl_max_bw_index(struct drm_i915_private *dev_priv,
661 int num_planes, int qgv_point)
666 * Let's return max bw for 0 planes
668 num_planes = max(1, num_planes);
670 for (i = 0; i < ARRAY_SIZE(dev_priv->display.bw.max); i++) {
671 const struct intel_bw_info *bi =
672 &dev_priv->display.bw.max[i];
675 * Pcode will not expose all QGV points when
676 * SAGV is forced to off/min/med/max.
678 if (qgv_point >= bi->num_qgv_points)
681 if (num_planes >= bi->num_planes)
688 static unsigned int tgl_max_bw_index(struct drm_i915_private *dev_priv,
689 int num_planes, int qgv_point)
694 * Let's return max bw for 0 planes
696 num_planes = max(1, num_planes);
698 for (i = ARRAY_SIZE(dev_priv->display.bw.max) - 1; i >= 0; i--) {
699 const struct intel_bw_info *bi =
700 &dev_priv->display.bw.max[i];
703 * Pcode will not expose all QGV points when
704 * SAGV is forced to off/min/med/max.
706 if (qgv_point >= bi->num_qgv_points)
709 if (num_planes <= bi->num_planes)
716 static unsigned int adl_psf_bw(struct drm_i915_private *dev_priv,
719 const struct intel_bw_info *bi =
720 &dev_priv->display.bw.max[0];
722 return bi->psf_bw[psf_gv_point];
725 static unsigned int icl_qgv_bw(struct drm_i915_private *i915,
726 int num_active_planes, int qgv_point)
730 if (DISPLAY_VER(i915) >= 12)
731 idx = tgl_max_bw_index(i915, num_active_planes, qgv_point);
733 idx = icl_max_bw_index(i915, num_active_planes, qgv_point);
735 if (idx >= ARRAY_SIZE(i915->display.bw.max))
738 return i915->display.bw.max[idx].deratedbw[qgv_point];
741 void intel_bw_init_hw(struct drm_i915_private *dev_priv)
743 if (!HAS_DISPLAY(dev_priv))
746 if (DISPLAY_VERx100(dev_priv) >= 1401 && IS_DGFX(dev_priv))
747 xe2_hpd_get_bw_info(dev_priv, &xe2_hpd_sa_info);
748 else if (DISPLAY_VER(dev_priv) >= 14)
749 tgl_get_bw_info(dev_priv, &mtl_sa_info);
750 else if (IS_DG2(dev_priv))
751 dg2_get_bw_info(dev_priv);
752 else if (IS_ALDERLAKE_P(dev_priv))
753 tgl_get_bw_info(dev_priv, &adlp_sa_info);
754 else if (IS_ALDERLAKE_S(dev_priv))
755 tgl_get_bw_info(dev_priv, &adls_sa_info);
756 else if (IS_ROCKETLAKE(dev_priv))
757 tgl_get_bw_info(dev_priv, &rkl_sa_info);
758 else if (DISPLAY_VER(dev_priv) == 12)
759 tgl_get_bw_info(dev_priv, &tgl_sa_info);
760 else if (DISPLAY_VER(dev_priv) == 11)
761 icl_get_bw_info(dev_priv, &icl_sa_info);
764 static unsigned int intel_bw_crtc_num_active_planes(const struct intel_crtc_state *crtc_state)
767 * We assume cursors are small enough
768 * to not not cause bandwidth problems.
770 return hweight8(crtc_state->active_planes & ~BIT(PLANE_CURSOR));
773 static unsigned int intel_bw_crtc_data_rate(const struct intel_crtc_state *crtc_state)
775 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
776 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
777 unsigned int data_rate = 0;
778 enum plane_id plane_id;
780 for_each_plane_id_on_crtc(crtc, plane_id) {
782 * We assume cursors are small enough
783 * to not not cause bandwidth problems.
785 if (plane_id == PLANE_CURSOR)
788 data_rate += crtc_state->data_rate[plane_id];
790 if (DISPLAY_VER(i915) < 11)
791 data_rate += crtc_state->data_rate_y[plane_id];
797 /* "Maximum Pipe Read Bandwidth" */
798 static int intel_bw_crtc_min_cdclk(const struct intel_crtc_state *crtc_state)
800 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
801 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
803 if (DISPLAY_VER(i915) < 12)
806 return DIV_ROUND_UP_ULL(mul_u32_u32(intel_bw_crtc_data_rate(crtc_state), 10), 512);
809 void intel_bw_crtc_update(struct intel_bw_state *bw_state,
810 const struct intel_crtc_state *crtc_state)
812 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
813 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
815 bw_state->data_rate[crtc->pipe] =
816 intel_bw_crtc_data_rate(crtc_state);
817 bw_state->num_active_planes[crtc->pipe] =
818 intel_bw_crtc_num_active_planes(crtc_state);
819 bw_state->force_check_qgv = true;
821 drm_dbg_kms(&i915->drm, "pipe %c data rate %u num active planes %u\n",
822 pipe_name(crtc->pipe),
823 bw_state->data_rate[crtc->pipe],
824 bw_state->num_active_planes[crtc->pipe]);
827 static unsigned int intel_bw_num_active_planes(struct drm_i915_private *dev_priv,
828 const struct intel_bw_state *bw_state)
830 unsigned int num_active_planes = 0;
833 for_each_pipe(dev_priv, pipe)
834 num_active_planes += bw_state->num_active_planes[pipe];
836 return num_active_planes;
839 static unsigned int intel_bw_data_rate(struct drm_i915_private *dev_priv,
840 const struct intel_bw_state *bw_state)
842 unsigned int data_rate = 0;
845 for_each_pipe(dev_priv, pipe)
846 data_rate += bw_state->data_rate[pipe];
848 if (DISPLAY_VER(dev_priv) >= 13 && i915_vtd_active(dev_priv))
849 data_rate = DIV_ROUND_UP(data_rate * 105, 100);
854 struct intel_bw_state *
855 intel_atomic_get_old_bw_state(struct intel_atomic_state *state)
857 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
858 struct intel_global_state *bw_state;
860 bw_state = intel_atomic_get_old_global_obj_state(state, &dev_priv->display.bw.obj);
862 return to_intel_bw_state(bw_state);
865 struct intel_bw_state *
866 intel_atomic_get_new_bw_state(struct intel_atomic_state *state)
868 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
869 struct intel_global_state *bw_state;
871 bw_state = intel_atomic_get_new_global_obj_state(state, &dev_priv->display.bw.obj);
873 return to_intel_bw_state(bw_state);
876 struct intel_bw_state *
877 intel_atomic_get_bw_state(struct intel_atomic_state *state)
879 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
880 struct intel_global_state *bw_state;
882 bw_state = intel_atomic_get_global_obj_state(state, &dev_priv->display.bw.obj);
883 if (IS_ERR(bw_state))
884 return ERR_CAST(bw_state);
886 return to_intel_bw_state(bw_state);
889 static unsigned int icl_max_bw_qgv_point_mask(struct drm_i915_private *i915,
890 int num_active_planes)
892 unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
893 unsigned int max_bw_point = 0;
894 unsigned int max_bw = 0;
897 for (i = 0; i < num_qgv_points; i++) {
898 unsigned int max_data_rate =
899 icl_qgv_bw(i915, num_active_planes, i);
902 * We need to know which qgv point gives us
903 * maximum bandwidth in order to disable SAGV
904 * if we find that we exceed SAGV block time
905 * with watermarks. By that moment we already
906 * have those, as it is calculated earlier in
907 * intel_atomic_check,
909 if (max_data_rate > max_bw) {
910 max_bw_point = BIT(i);
911 max_bw = max_data_rate;
918 static u16 icl_prepare_qgv_points_mask(struct drm_i915_private *i915,
919 unsigned int qgv_points,
920 unsigned int psf_points)
922 return ~(ICL_PCODE_REQ_QGV_PT(qgv_points) |
923 ADLS_PCODE_REQ_PSF_PT(psf_points)) & icl_qgv_points_mask(i915);
926 static unsigned int icl_max_bw_psf_gv_point_mask(struct drm_i915_private *i915)
928 unsigned int num_psf_gv_points = i915->display.bw.max[0].num_psf_gv_points;
929 unsigned int max_bw_point_mask = 0;
930 unsigned int max_bw = 0;
933 for (i = 0; i < num_psf_gv_points; i++) {
934 unsigned int max_data_rate = adl_psf_bw(i915, i);
936 if (max_data_rate > max_bw) {
937 max_bw_point_mask = BIT(i);
938 max_bw = max_data_rate;
939 } else if (max_data_rate == max_bw) {
940 max_bw_point_mask |= BIT(i);
944 return max_bw_point_mask;
947 static void icl_force_disable_sagv(struct drm_i915_private *i915,
948 struct intel_bw_state *bw_state)
950 unsigned int qgv_points = icl_max_bw_qgv_point_mask(i915, 0);
951 unsigned int psf_points = icl_max_bw_psf_gv_point_mask(i915);
953 bw_state->qgv_points_mask = icl_prepare_qgv_points_mask(i915,
957 drm_dbg_kms(&i915->drm, "Forcing SAGV disable: mask 0x%x\n",
958 bw_state->qgv_points_mask);
960 icl_pcode_restrict_qgv_points(i915, bw_state->qgv_points_mask);
963 static int mtl_find_qgv_points(struct drm_i915_private *i915,
964 unsigned int data_rate,
965 unsigned int num_active_planes,
966 struct intel_bw_state *new_bw_state)
968 unsigned int best_rate = UINT_MAX;
969 unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
970 unsigned int qgv_peak_bw = 0;
974 ret = intel_atomic_lock_global_state(&new_bw_state->base);
979 * If SAGV cannot be enabled, disable the pcode SAGV by passing all 1's
980 * for qgv peak bw in PM Demand request. So assign UINT_MAX if SAGV is
981 * not enabled. PM Demand code will clamp the value for the register
983 if (!intel_can_enable_sagv(i915, new_bw_state)) {
984 new_bw_state->qgv_point_peakbw = U16_MAX;
985 drm_dbg_kms(&i915->drm, "No SAGV, use UINT_MAX as peak bw.");
990 * Find the best QGV point by comparing the data_rate with max data rate
991 * offered per plane group
993 for (i = 0; i < num_qgv_points; i++) {
994 unsigned int bw_index =
995 tgl_max_bw_index(i915, num_active_planes, i);
996 unsigned int max_data_rate;
998 if (bw_index >= ARRAY_SIZE(i915->display.bw.max))
1001 max_data_rate = i915->display.bw.max[bw_index].deratedbw[i];
1003 if (max_data_rate < data_rate)
1006 if (max_data_rate - data_rate < best_rate) {
1007 best_rate = max_data_rate - data_rate;
1008 qgv_peak_bw = i915->display.bw.max[bw_index].peakbw[i];
1011 drm_dbg_kms(&i915->drm, "QGV point %d: max bw %d required %d qgv_peak_bw: %d\n",
1012 i, max_data_rate, data_rate, qgv_peak_bw);
1015 drm_dbg_kms(&i915->drm, "Matching peaks QGV bw: %d for required data rate: %d\n",
1016 qgv_peak_bw, data_rate);
1019 * The display configuration cannot be supported if no QGV point
1020 * satisfying the required data rate is found
1022 if (qgv_peak_bw == 0) {
1023 drm_dbg_kms(&i915->drm, "No QGV points for bw %d for display configuration(%d active planes).\n",
1024 data_rate, num_active_planes);
1028 /* MTL PM DEMAND expects QGV BW parameter in multiples of 100 mbps */
1029 new_bw_state->qgv_point_peakbw = DIV_ROUND_CLOSEST(qgv_peak_bw, 100);
1034 static int icl_find_qgv_points(struct drm_i915_private *i915,
1035 unsigned int data_rate,
1036 unsigned int num_active_planes,
1037 const struct intel_bw_state *old_bw_state,
1038 struct intel_bw_state *new_bw_state)
1040 unsigned int num_psf_gv_points = i915->display.bw.max[0].num_psf_gv_points;
1041 unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
1047 ret = intel_atomic_lock_global_state(&new_bw_state->base);
1051 for (i = 0; i < num_qgv_points; i++) {
1052 unsigned int max_data_rate = icl_qgv_bw(i915,
1053 num_active_planes, i);
1054 if (max_data_rate >= data_rate)
1055 qgv_points |= BIT(i);
1057 drm_dbg_kms(&i915->drm, "QGV point %d: max bw %d required %d\n",
1058 i, max_data_rate, data_rate);
1061 for (i = 0; i < num_psf_gv_points; i++) {
1062 unsigned int max_data_rate = adl_psf_bw(i915, i);
1064 if (max_data_rate >= data_rate)
1065 psf_points |= BIT(i);
1067 drm_dbg_kms(&i915->drm, "PSF GV point %d: max bw %d"
1069 i, max_data_rate, data_rate);
1073 * BSpec states that we always should have at least one allowed point
1074 * left, so if we couldn't - simply reject the configuration for obvious
1077 if (qgv_points == 0) {
1078 drm_dbg_kms(&i915->drm, "No QGV points provide sufficient memory"
1079 " bandwidth %d for display configuration(%d active planes).\n",
1080 data_rate, num_active_planes);
1084 if (num_psf_gv_points > 0 && psf_points == 0) {
1085 drm_dbg_kms(&i915->drm, "No PSF GV points provide sufficient memory"
1086 " bandwidth %d for display configuration(%d active planes).\n",
1087 data_rate, num_active_planes);
1092 * Leave only single point with highest bandwidth, if
1093 * we can't enable SAGV due to the increased memory latency it may
1096 if (!intel_can_enable_sagv(i915, new_bw_state)) {
1097 qgv_points = icl_max_bw_qgv_point_mask(i915, num_active_planes);
1098 drm_dbg_kms(&i915->drm, "No SAGV, using single QGV point mask 0x%x\n",
1103 * We store the ones which need to be masked as that is what PCode
1104 * actually accepts as a parameter.
1106 new_bw_state->qgv_points_mask = icl_prepare_qgv_points_mask(i915,
1110 * If the actual mask had changed we need to make sure that
1111 * the commits are serialized(in case this is a nomodeset, nonblocking)
1113 if (new_bw_state->qgv_points_mask != old_bw_state->qgv_points_mask) {
1114 ret = intel_atomic_serialize_global_state(&new_bw_state->base);
1122 static int intel_bw_check_qgv_points(struct drm_i915_private *i915,
1123 const struct intel_bw_state *old_bw_state,
1124 struct intel_bw_state *new_bw_state)
1126 unsigned int data_rate = intel_bw_data_rate(i915, new_bw_state);
1127 unsigned int num_active_planes =
1128 intel_bw_num_active_planes(i915, new_bw_state);
1130 data_rate = DIV_ROUND_UP(data_rate, 1000);
1132 if (DISPLAY_VER(i915) >= 14)
1133 return mtl_find_qgv_points(i915, data_rate, num_active_planes,
1136 return icl_find_qgv_points(i915, data_rate, num_active_planes,
1137 old_bw_state, new_bw_state);
1140 static bool intel_bw_state_changed(struct drm_i915_private *i915,
1141 const struct intel_bw_state *old_bw_state,
1142 const struct intel_bw_state *new_bw_state)
1146 for_each_pipe(i915, pipe) {
1147 const struct intel_dbuf_bw *old_crtc_bw =
1148 &old_bw_state->dbuf_bw[pipe];
1149 const struct intel_dbuf_bw *new_crtc_bw =
1150 &new_bw_state->dbuf_bw[pipe];
1151 enum dbuf_slice slice;
1153 for_each_dbuf_slice(i915, slice) {
1154 if (old_crtc_bw->max_bw[slice] != new_crtc_bw->max_bw[slice] ||
1155 old_crtc_bw->active_planes[slice] != new_crtc_bw->active_planes[slice])
1159 if (old_bw_state->min_cdclk[pipe] != new_bw_state->min_cdclk[pipe])
1166 static void skl_plane_calc_dbuf_bw(struct intel_bw_state *bw_state,
1167 struct intel_crtc *crtc,
1168 enum plane_id plane_id,
1169 const struct skl_ddb_entry *ddb,
1170 unsigned int data_rate)
1172 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1173 struct intel_dbuf_bw *crtc_bw = &bw_state->dbuf_bw[crtc->pipe];
1174 unsigned int dbuf_mask = skl_ddb_dbuf_slice_mask(i915, ddb);
1175 enum dbuf_slice slice;
1178 * The arbiter can only really guarantee an
1179 * equal share of the total bw to each plane.
1181 for_each_dbuf_slice_in_mask(i915, slice, dbuf_mask) {
1182 crtc_bw->max_bw[slice] = max(crtc_bw->max_bw[slice], data_rate);
1183 crtc_bw->active_planes[slice] |= BIT(plane_id);
1187 static void skl_crtc_calc_dbuf_bw(struct intel_bw_state *bw_state,
1188 const struct intel_crtc_state *crtc_state)
1190 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1191 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1192 struct intel_dbuf_bw *crtc_bw = &bw_state->dbuf_bw[crtc->pipe];
1193 enum plane_id plane_id;
1195 memset(crtc_bw, 0, sizeof(*crtc_bw));
1197 if (!crtc_state->hw.active)
1200 for_each_plane_id_on_crtc(crtc, plane_id) {
1202 * We assume cursors are small enough
1203 * to not cause bandwidth problems.
1205 if (plane_id == PLANE_CURSOR)
1208 skl_plane_calc_dbuf_bw(bw_state, crtc, plane_id,
1209 &crtc_state->wm.skl.plane_ddb[plane_id],
1210 crtc_state->data_rate[plane_id]);
1212 if (DISPLAY_VER(i915) < 11)
1213 skl_plane_calc_dbuf_bw(bw_state, crtc, plane_id,
1214 &crtc_state->wm.skl.plane_ddb_y[plane_id],
1215 crtc_state->data_rate[plane_id]);
1219 /* "Maximum Data Buffer Bandwidth" */
1221 intel_bw_dbuf_min_cdclk(struct drm_i915_private *i915,
1222 const struct intel_bw_state *bw_state)
1224 unsigned int total_max_bw = 0;
1225 enum dbuf_slice slice;
1227 for_each_dbuf_slice(i915, slice) {
1228 int num_active_planes = 0;
1229 unsigned int max_bw = 0;
1233 * The arbiter can only really guarantee an
1234 * equal share of the total bw to each plane.
1236 for_each_pipe(i915, pipe) {
1237 const struct intel_dbuf_bw *crtc_bw = &bw_state->dbuf_bw[pipe];
1239 max_bw = max(crtc_bw->max_bw[slice], max_bw);
1240 num_active_planes += hweight8(crtc_bw->active_planes[slice]);
1242 max_bw *= num_active_planes;
1244 total_max_bw = max(total_max_bw, max_bw);
1247 return DIV_ROUND_UP(total_max_bw, 64);
1250 int intel_bw_min_cdclk(struct drm_i915_private *i915,
1251 const struct intel_bw_state *bw_state)
1256 min_cdclk = intel_bw_dbuf_min_cdclk(i915, bw_state);
1258 for_each_pipe(i915, pipe)
1259 min_cdclk = max(min_cdclk, bw_state->min_cdclk[pipe]);
1264 int intel_bw_calc_min_cdclk(struct intel_atomic_state *state,
1265 bool *need_cdclk_calc)
1267 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1268 struct intel_bw_state *new_bw_state = NULL;
1269 const struct intel_bw_state *old_bw_state = NULL;
1270 const struct intel_cdclk_state *cdclk_state;
1271 const struct intel_crtc_state *crtc_state;
1272 int old_min_cdclk, new_min_cdclk;
1273 struct intel_crtc *crtc;
1276 if (DISPLAY_VER(dev_priv) < 9)
1279 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
1280 new_bw_state = intel_atomic_get_bw_state(state);
1281 if (IS_ERR(new_bw_state))
1282 return PTR_ERR(new_bw_state);
1284 old_bw_state = intel_atomic_get_old_bw_state(state);
1286 skl_crtc_calc_dbuf_bw(new_bw_state, crtc_state);
1288 new_bw_state->min_cdclk[crtc->pipe] =
1289 intel_bw_crtc_min_cdclk(crtc_state);
1295 if (intel_bw_state_changed(dev_priv, old_bw_state, new_bw_state)) {
1296 int ret = intel_atomic_lock_global_state(&new_bw_state->base);
1301 old_min_cdclk = intel_bw_min_cdclk(dev_priv, old_bw_state);
1302 new_min_cdclk = intel_bw_min_cdclk(dev_priv, new_bw_state);
1305 * No need to check against the cdclk state if
1306 * the min cdclk doesn't increase.
1308 * Ie. we only ever increase the cdclk due to bandwidth
1309 * requirements. This can reduce back and forth
1310 * display blinking due to constant cdclk changes.
1312 if (new_min_cdclk <= old_min_cdclk)
1315 cdclk_state = intel_atomic_get_cdclk_state(state);
1316 if (IS_ERR(cdclk_state))
1317 return PTR_ERR(cdclk_state);
1320 * No need to recalculate the cdclk state if
1321 * the min cdclk doesn't increase.
1323 * Ie. we only ever increase the cdclk due to bandwidth
1324 * requirements. This can reduce back and forth
1325 * display blinking due to constant cdclk changes.
1327 if (new_min_cdclk <= cdclk_state->bw_min_cdclk)
1330 drm_dbg_kms(&dev_priv->drm,
1331 "new bandwidth min cdclk (%d kHz) > old min cdclk (%d kHz)\n",
1332 new_min_cdclk, cdclk_state->bw_min_cdclk);
1333 *need_cdclk_calc = true;
1338 static int intel_bw_check_data_rate(struct intel_atomic_state *state, bool *changed)
1340 struct drm_i915_private *i915 = to_i915(state->base.dev);
1341 const struct intel_crtc_state *new_crtc_state, *old_crtc_state;
1342 struct intel_crtc *crtc;
1345 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
1346 new_crtc_state, i) {
1347 unsigned int old_data_rate =
1348 intel_bw_crtc_data_rate(old_crtc_state);
1349 unsigned int new_data_rate =
1350 intel_bw_crtc_data_rate(new_crtc_state);
1351 unsigned int old_active_planes =
1352 intel_bw_crtc_num_active_planes(old_crtc_state);
1353 unsigned int new_active_planes =
1354 intel_bw_crtc_num_active_planes(new_crtc_state);
1355 struct intel_bw_state *new_bw_state;
1358 * Avoid locking the bw state when
1359 * nothing significant has changed.
1361 if (old_data_rate == new_data_rate &&
1362 old_active_planes == new_active_planes)
1365 new_bw_state = intel_atomic_get_bw_state(state);
1366 if (IS_ERR(new_bw_state))
1367 return PTR_ERR(new_bw_state);
1369 new_bw_state->data_rate[crtc->pipe] = new_data_rate;
1370 new_bw_state->num_active_planes[crtc->pipe] = new_active_planes;
1374 drm_dbg_kms(&i915->drm,
1375 "[CRTC:%d:%s] data rate %u num active planes %u\n",
1376 crtc->base.base.id, crtc->base.name,
1377 new_bw_state->data_rate[crtc->pipe],
1378 new_bw_state->num_active_planes[crtc->pipe]);
1384 int intel_bw_atomic_check(struct intel_atomic_state *state)
1386 bool changed = false;
1387 struct drm_i915_private *i915 = to_i915(state->base.dev);
1388 struct intel_bw_state *new_bw_state;
1389 const struct intel_bw_state *old_bw_state;
1392 /* FIXME earlier gens need some checks too */
1393 if (DISPLAY_VER(i915) < 11)
1396 ret = intel_bw_check_data_rate(state, &changed);
1400 old_bw_state = intel_atomic_get_old_bw_state(state);
1401 new_bw_state = intel_atomic_get_new_bw_state(state);
1404 (intel_can_enable_sagv(i915, old_bw_state) !=
1405 intel_can_enable_sagv(i915, new_bw_state) ||
1406 new_bw_state->force_check_qgv))
1410 * If none of our inputs (data rates, number of active
1411 * planes, SAGV yes/no) changed then nothing to do here.
1416 ret = intel_bw_check_qgv_points(i915, old_bw_state, new_bw_state);
1420 new_bw_state->force_check_qgv = false;
1425 static struct intel_global_state *
1426 intel_bw_duplicate_state(struct intel_global_obj *obj)
1428 struct intel_bw_state *state;
1430 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
1434 return &state->base;
1437 static void intel_bw_destroy_state(struct intel_global_obj *obj,
1438 struct intel_global_state *state)
1443 static const struct intel_global_state_funcs intel_bw_funcs = {
1444 .atomic_duplicate_state = intel_bw_duplicate_state,
1445 .atomic_destroy_state = intel_bw_destroy_state,
1448 int intel_bw_init(struct drm_i915_private *i915)
1450 struct intel_display *display = &i915->display;
1451 struct intel_bw_state *state;
1453 state = kzalloc(sizeof(*state), GFP_KERNEL);
1457 intel_atomic_global_obj_init(display, &display->bw.obj,
1458 &state->base, &intel_bw_funcs);
1461 * Limit this only if we have SAGV. And for Display version 14 onwards
1462 * sagv is handled though pmdemand requests
1464 if (intel_has_sagv(i915) && IS_DISPLAY_VER(i915, 11, 13))
1465 icl_force_disable_sagv(i915, state);