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 struct intel_psf_gv_point {
26 u8 clk; /* clock in multiples of 16.6666 MHz */
29 struct intel_qgv_info {
30 struct intel_qgv_point points[I915_NUM_QGV_POINTS];
31 struct intel_psf_gv_point psf_points[I915_NUM_PSF_GV_POINTS];
40 static int dg1_mchbar_read_qgv_point_info(struct drm_i915_private *dev_priv,
41 struct intel_qgv_point *sp,
44 u32 dclk_ratio, dclk_reference;
47 val = intel_uncore_read(&dev_priv->uncore, SA_PERF_STATUS_0_0_0_MCHBAR_PC);
48 dclk_ratio = REG_FIELD_GET(DG1_QCLK_RATIO_MASK, val);
49 if (val & DG1_QCLK_REFERENCE)
50 dclk_reference = 6; /* 6 * 16.666 MHz = 100 MHz */
52 dclk_reference = 8; /* 8 * 16.666 MHz = 133 MHz */
53 sp->dclk = DIV_ROUND_UP((16667 * dclk_ratio * dclk_reference) + 500, 1000);
55 val = intel_uncore_read(&dev_priv->uncore, SKL_MC_BIOS_DATA_0_0_0_MCHBAR_PCU);
56 if (val & DG1_GEAR_TYPE)
62 val = intel_uncore_read(&dev_priv->uncore, MCHBAR_CH0_CR_TC_PRE_0_0_0_MCHBAR);
63 sp->t_rp = REG_FIELD_GET(DG1_DRAM_T_RP_MASK, val);
64 sp->t_rdpre = REG_FIELD_GET(DG1_DRAM_T_RDPRE_MASK, val);
66 val = intel_uncore_read(&dev_priv->uncore, MCHBAR_CH0_CR_TC_PRE_0_0_0_MCHBAR_HIGH);
67 sp->t_rcd = REG_FIELD_GET(DG1_DRAM_T_RCD_MASK, val);
68 sp->t_ras = REG_FIELD_GET(DG1_DRAM_T_RAS_MASK, val);
70 sp->t_rc = sp->t_rp + sp->t_ras;
75 static int icl_pcode_read_qgv_point_info(struct drm_i915_private *dev_priv,
76 struct intel_qgv_point *sp,
79 u32 val = 0, val2 = 0;
83 ret = snb_pcode_read(&dev_priv->uncore, ICL_PCODE_MEM_SUBSYSYSTEM_INFO |
84 ICL_PCODE_MEM_SS_READ_QGV_POINT_INFO(point),
90 sp->dclk = DIV_ROUND_UP((16667 * dclk) + (DISPLAY_VER(dev_priv) >= 12 ? 500 : 0),
92 sp->t_rp = (val & 0xff0000) >> 16;
93 sp->t_rcd = (val & 0xff000000) >> 24;
95 sp->t_rdpre = val2 & 0xff;
96 sp->t_ras = (val2 & 0xff00) >> 8;
98 sp->t_rc = sp->t_rp + sp->t_ras;
103 static int adls_pcode_read_psf_gv_point_info(struct drm_i915_private *dev_priv,
104 struct intel_psf_gv_point *points)
110 ret = snb_pcode_read(&dev_priv->uncore, ICL_PCODE_MEM_SUBSYSYSTEM_INFO |
111 ADL_PCODE_MEM_SS_READ_PSF_GV_INFO, &val, NULL);
115 for (i = 0; i < I915_NUM_PSF_GV_POINTS; i++) {
116 points[i].clk = val & 0xff;
123 static u16 icl_qgv_points_mask(struct drm_i915_private *i915)
125 unsigned int num_psf_gv_points = i915->display.bw.max[0].num_psf_gv_points;
126 unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
127 u16 qgv_points = 0, psf_points = 0;
130 * We can _not_ use the whole ADLS_QGV_PT_MASK here, as PCode rejects
131 * it with failure if we try masking any unadvertised points.
132 * So need to operate only with those returned from PCode.
134 if (num_qgv_points > 0)
135 qgv_points = GENMASK(num_qgv_points - 1, 0);
137 if (num_psf_gv_points > 0)
138 psf_points = GENMASK(num_psf_gv_points - 1, 0);
140 return ICL_PCODE_REQ_QGV_PT(qgv_points) | ADLS_PCODE_REQ_PSF_PT(psf_points);
143 static bool is_sagv_enabled(struct drm_i915_private *i915, u16 points_mask)
145 return !is_power_of_2(~points_mask & icl_qgv_points_mask(i915) &
146 ICL_PCODE_REQ_QGV_PT_MASK);
149 int icl_pcode_restrict_qgv_points(struct drm_i915_private *dev_priv,
154 if (DISPLAY_VER(dev_priv) >= 14)
157 /* bspec says to keep retrying for at least 1 ms */
158 ret = skl_pcode_request(&dev_priv->uncore, ICL_PCODE_SAGV_DE_MEM_SS_CONFIG,
160 ICL_PCODE_REP_QGV_MASK | ADLS_PCODE_REP_PSF_MASK,
161 ICL_PCODE_REP_QGV_SAFE | ADLS_PCODE_REP_PSF_SAFE,
165 drm_err(&dev_priv->drm,
166 "Failed to disable qgv points (0x%x) points: 0x%x\n",
171 dev_priv->display.sagv.status = is_sagv_enabled(dev_priv, points_mask) ?
172 I915_SAGV_ENABLED : I915_SAGV_DISABLED;
177 static int mtl_read_qgv_point_info(struct drm_i915_private *dev_priv,
178 struct intel_qgv_point *sp, int point)
183 val = intel_uncore_read(&dev_priv->uncore,
184 MTL_MEM_SS_INFO_QGV_POINT_LOW(point));
185 val2 = intel_uncore_read(&dev_priv->uncore,
186 MTL_MEM_SS_INFO_QGV_POINT_HIGH(point));
187 dclk = REG_FIELD_GET(MTL_DCLK_MASK, val);
188 sp->dclk = DIV_ROUND_CLOSEST(16667 * dclk, 1000);
189 sp->t_rp = REG_FIELD_GET(MTL_TRP_MASK, val);
190 sp->t_rcd = REG_FIELD_GET(MTL_TRCD_MASK, val);
192 sp->t_rdpre = REG_FIELD_GET(MTL_TRDPRE_MASK, val2);
193 sp->t_ras = REG_FIELD_GET(MTL_TRAS_MASK, val2);
195 sp->t_rc = sp->t_rp + sp->t_ras;
201 intel_read_qgv_point_info(struct drm_i915_private *dev_priv,
202 struct intel_qgv_point *sp,
205 if (DISPLAY_VER(dev_priv) >= 14)
206 return mtl_read_qgv_point_info(dev_priv, sp, point);
207 else if (IS_DG1(dev_priv))
208 return dg1_mchbar_read_qgv_point_info(dev_priv, sp, point);
210 return icl_pcode_read_qgv_point_info(dev_priv, sp, point);
213 static int icl_get_qgv_points(struct drm_i915_private *dev_priv,
214 struct intel_qgv_info *qi,
217 const struct dram_info *dram_info = &dev_priv->dram_info;
220 qi->num_points = dram_info->num_qgv_points;
221 qi->num_psf_points = dram_info->num_psf_gv_points;
223 if (DISPLAY_VER(dev_priv) >= 14) {
224 switch (dram_info->type) {
225 case INTEL_DRAM_DDR4:
227 qi->max_numchannels = 2;
228 qi->channel_width = 64;
229 qi->deinterleave = 2;
231 case INTEL_DRAM_DDR5:
233 qi->max_numchannels = 4;
234 qi->channel_width = 32;
235 qi->deinterleave = 2;
237 case INTEL_DRAM_LPDDR4:
238 case INTEL_DRAM_LPDDR5:
240 qi->max_numchannels = 8;
241 qi->channel_width = 16;
242 qi->deinterleave = 4;
245 MISSING_CASE(dram_info->type);
248 } else if (DISPLAY_VER(dev_priv) >= 12) {
249 switch (dram_info->type) {
250 case INTEL_DRAM_DDR4:
251 qi->t_bl = is_y_tile ? 8 : 4;
252 qi->max_numchannels = 2;
253 qi->channel_width = 64;
254 qi->deinterleave = is_y_tile ? 1 : 2;
256 case INTEL_DRAM_DDR5:
257 qi->t_bl = is_y_tile ? 16 : 8;
258 qi->max_numchannels = 4;
259 qi->channel_width = 32;
260 qi->deinterleave = is_y_tile ? 1 : 2;
262 case INTEL_DRAM_LPDDR4:
263 if (IS_ROCKETLAKE(dev_priv)) {
265 qi->max_numchannels = 4;
266 qi->channel_width = 32;
267 qi->deinterleave = 2;
271 case INTEL_DRAM_LPDDR5:
273 qi->max_numchannels = 8;
274 qi->channel_width = 16;
275 qi->deinterleave = is_y_tile ? 2 : 4;
279 qi->max_numchannels = 1;
282 } else if (DISPLAY_VER(dev_priv) == 11) {
283 qi->t_bl = dev_priv->dram_info.type == INTEL_DRAM_DDR4 ? 4 : 8;
284 qi->max_numchannels = 1;
287 if (drm_WARN_ON(&dev_priv->drm,
288 qi->num_points > ARRAY_SIZE(qi->points)))
289 qi->num_points = ARRAY_SIZE(qi->points);
291 for (i = 0; i < qi->num_points; i++) {
292 struct intel_qgv_point *sp = &qi->points[i];
294 ret = intel_read_qgv_point_info(dev_priv, sp, i);
296 drm_dbg_kms(&dev_priv->drm, "Could not read QGV %d info\n", i);
300 drm_dbg_kms(&dev_priv->drm,
301 "QGV %d: DCLK=%d tRP=%d tRDPRE=%d tRAS=%d tRCD=%d tRC=%d\n",
302 i, sp->dclk, sp->t_rp, sp->t_rdpre, sp->t_ras,
303 sp->t_rcd, sp->t_rc);
306 if (qi->num_psf_points > 0) {
307 ret = adls_pcode_read_psf_gv_point_info(dev_priv, qi->psf_points);
309 drm_err(&dev_priv->drm, "Failed to read PSF point data; PSF points will not be considered in bandwidth calculations.\n");
310 qi->num_psf_points = 0;
313 for (i = 0; i < qi->num_psf_points; i++)
314 drm_dbg_kms(&dev_priv->drm,
315 "PSF GV %d: CLK=%d \n",
316 i, qi->psf_points[i].clk);
322 static int adl_calc_psf_bw(int clk)
325 * clk is multiples of 16.666MHz (100/6)
326 * According to BSpec PSF GV bandwidth is
327 * calculated as BW = 64 * clk * 16.666Mhz
329 return DIV_ROUND_CLOSEST(64 * clk * 100, 6);
332 static int icl_sagv_max_dclk(const struct intel_qgv_info *qi)
337 for (i = 0; i < qi->num_points; i++)
338 dclk = max(dclk, qi->points[i].dclk);
343 struct intel_sa_info {
345 u8 deburst, deprogbwlimit, derating;
348 static const struct intel_sa_info icl_sa_info = {
350 .deprogbwlimit = 25, /* GB/s */
355 static const struct intel_sa_info tgl_sa_info = {
357 .deprogbwlimit = 34, /* GB/s */
362 static const struct intel_sa_info rkl_sa_info = {
364 .deprogbwlimit = 20, /* GB/s */
369 static const struct intel_sa_info adls_sa_info = {
371 .deprogbwlimit = 38, /* GB/s */
376 static const struct intel_sa_info adlp_sa_info = {
378 .deprogbwlimit = 38, /* GB/s */
383 static const struct intel_sa_info mtl_sa_info = {
385 .deprogbwlimit = 38, /* GB/s */
390 static int icl_get_bw_info(struct drm_i915_private *dev_priv, const struct intel_sa_info *sa)
392 struct intel_qgv_info qi = {};
393 bool is_y_tile = true; /* assume y tile may be used */
394 int num_channels = max_t(u8, 1, dev_priv->dram_info.num_channels);
395 int ipqdepth, ipqdepthpch = 16;
398 int num_groups = ARRAY_SIZE(dev_priv->display.bw.max);
401 ret = icl_get_qgv_points(dev_priv, &qi, is_y_tile);
403 drm_dbg_kms(&dev_priv->drm,
404 "Failed to get memory subsystem information, ignoring bandwidth limits");
408 dclk_max = icl_sagv_max_dclk(&qi);
409 maxdebw = min(sa->deprogbwlimit * 1000, dclk_max * 16 * 6 / 10);
410 ipqdepth = min(ipqdepthpch, sa->displayrtids / num_channels);
411 qi.deinterleave = DIV_ROUND_UP(num_channels, is_y_tile ? 4 : 2);
413 for (i = 0; i < num_groups; i++) {
414 struct intel_bw_info *bi = &dev_priv->display.bw.max[i];
418 clpchgroup = (sa->deburst * qi.deinterleave / num_channels) << i;
419 bi->num_planes = (ipqdepth - clpchgroup) / clpchgroup + 1;
421 bi->num_qgv_points = qi.num_points;
422 bi->num_psf_gv_points = qi.num_psf_points;
424 for (j = 0; j < qi.num_points; j++) {
425 const struct intel_qgv_point *sp = &qi.points[j];
431 * FIXME what is the logic behind the
432 * assumed burst length?
434 ct = max_t(int, sp->t_rc, sp->t_rp + sp->t_rcd +
435 (clpchgroup - 1) * qi.t_bl + sp->t_rdpre);
436 bw = DIV_ROUND_UP(sp->dclk * clpchgroup * 32 * num_channels, ct);
438 bi->deratedbw[j] = min(maxdebw,
439 bw * (100 - sa->derating) / 100);
441 drm_dbg_kms(&dev_priv->drm,
442 "BW%d / QGV %d: num_planes=%d deratedbw=%u\n",
443 i, j, bi->num_planes, bi->deratedbw[j]);
447 * In case if SAGV is disabled in BIOS, we always get 1
448 * SAGV point, but we can't send PCode commands to restrict it
449 * as it will fail and pointless anyway.
451 if (qi.num_points == 1)
452 dev_priv->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
454 dev_priv->display.sagv.status = I915_SAGV_ENABLED;
459 static int tgl_get_bw_info(struct drm_i915_private *dev_priv, const struct intel_sa_info *sa)
461 struct intel_qgv_info qi = {};
462 const struct dram_info *dram_info = &dev_priv->dram_info;
463 bool is_y_tile = true; /* assume y tile may be used */
464 int num_channels = max_t(u8, 1, dev_priv->dram_info.num_channels);
465 int ipqdepth, ipqdepthpch = 16;
469 int num_groups = ARRAY_SIZE(dev_priv->display.bw.max);
472 ret = icl_get_qgv_points(dev_priv, &qi, is_y_tile);
474 drm_dbg_kms(&dev_priv->drm,
475 "Failed to get memory subsystem information, ignoring bandwidth limits");
479 if (DISPLAY_VER(dev_priv) < 14 &&
480 (dram_info->type == INTEL_DRAM_LPDDR4 || dram_info->type == INTEL_DRAM_LPDDR5))
483 qi.deinterleave = qi.deinterleave ? : DIV_ROUND_UP(num_channels, is_y_tile ? 4 : 2);
485 if (num_channels < qi.max_numchannels && DISPLAY_VER(dev_priv) >= 12)
486 qi.deinterleave = max(DIV_ROUND_UP(qi.deinterleave, 2), 1);
488 if (DISPLAY_VER(dev_priv) >= 12 && num_channels > qi.max_numchannels)
489 drm_warn(&dev_priv->drm, "Number of channels exceeds max number of channels.");
490 if (qi.max_numchannels != 0)
491 num_channels = min_t(u8, num_channels, qi.max_numchannels);
493 dclk_max = icl_sagv_max_dclk(&qi);
495 peakbw = num_channels * DIV_ROUND_UP(qi.channel_width, 8) * dclk_max;
496 maxdebw = min(sa->deprogbwlimit * 1000, peakbw * 6 / 10); /* 60% */
498 ipqdepth = min(ipqdepthpch, sa->displayrtids / num_channels);
500 * clperchgroup = 4kpagespermempage * clperchperblock,
501 * clperchperblock = 8 / num_channels * interleave
503 clperchgroup = 4 * DIV_ROUND_UP(8, num_channels) * qi.deinterleave;
505 for (i = 0; i < num_groups; i++) {
506 struct intel_bw_info *bi = &dev_priv->display.bw.max[i];
507 struct intel_bw_info *bi_next;
511 clpchgroup = (sa->deburst * qi.deinterleave / num_channels) << i;
513 if (i < num_groups - 1) {
514 bi_next = &dev_priv->display.bw.max[i + 1];
516 if (clpchgroup < clperchgroup)
517 bi_next->num_planes = (ipqdepth - clpchgroup) /
520 bi_next->num_planes = 0;
523 bi->num_qgv_points = qi.num_points;
524 bi->num_psf_gv_points = qi.num_psf_points;
526 for (j = 0; j < qi.num_points; j++) {
527 const struct intel_qgv_point *sp = &qi.points[j];
533 * FIXME what is the logic behind the
534 * assumed burst length?
536 ct = max_t(int, sp->t_rc, sp->t_rp + sp->t_rcd +
537 (clpchgroup - 1) * qi.t_bl + sp->t_rdpre);
538 bw = DIV_ROUND_UP(sp->dclk * clpchgroup * 32 * num_channels, ct);
540 bi->deratedbw[j] = min(maxdebw,
541 bw * (100 - sa->derating) / 100);
542 bi->peakbw[j] = DIV_ROUND_CLOSEST(sp->dclk *
544 qi.channel_width, 8);
546 drm_dbg_kms(&dev_priv->drm,
547 "BW%d / QGV %d: num_planes=%d deratedbw=%u peakbw: %u\n",
548 i, j, bi->num_planes, bi->deratedbw[j],
552 for (j = 0; j < qi.num_psf_points; j++) {
553 const struct intel_psf_gv_point *sp = &qi.psf_points[j];
555 bi->psf_bw[j] = adl_calc_psf_bw(sp->clk);
557 drm_dbg_kms(&dev_priv->drm,
558 "BW%d / PSF GV %d: num_planes=%d bw=%u\n",
559 i, j, bi->num_planes, bi->psf_bw[j]);
564 * In case if SAGV is disabled in BIOS, we always get 1
565 * SAGV point, but we can't send PCode commands to restrict it
566 * as it will fail and pointless anyway.
568 if (qi.num_points == 1)
569 dev_priv->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
571 dev_priv->display.sagv.status = I915_SAGV_ENABLED;
576 static void dg2_get_bw_info(struct drm_i915_private *i915)
578 unsigned int deratedbw = IS_DG2_G11(i915) ? 38000 : 50000;
579 int num_groups = ARRAY_SIZE(i915->display.bw.max);
583 * DG2 doesn't have SAGV or QGV points, just a constant max bandwidth
584 * that doesn't depend on the number of planes enabled. So fill all the
585 * plane group with constant bw information for uniformity with other
586 * platforms. DG2-G10 platforms have a constant 50 GB/s bandwidth,
587 * whereas DG2-G11 platforms have 38 GB/s.
589 for (i = 0; i < num_groups; i++) {
590 struct intel_bw_info *bi = &i915->display.bw.max[i];
593 /* Need only one dummy QGV point per group */
594 bi->num_qgv_points = 1;
595 bi->deratedbw[0] = deratedbw;
598 i915->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
601 static unsigned int icl_max_bw_index(struct drm_i915_private *dev_priv,
602 int num_planes, int qgv_point)
607 * Let's return max bw for 0 planes
609 num_planes = max(1, num_planes);
611 for (i = 0; i < ARRAY_SIZE(dev_priv->display.bw.max); i++) {
612 const struct intel_bw_info *bi =
613 &dev_priv->display.bw.max[i];
616 * Pcode will not expose all QGV points when
617 * SAGV is forced to off/min/med/max.
619 if (qgv_point >= bi->num_qgv_points)
622 if (num_planes >= bi->num_planes)
629 static unsigned int tgl_max_bw_index(struct drm_i915_private *dev_priv,
630 int num_planes, int qgv_point)
635 * Let's return max bw for 0 planes
637 num_planes = max(1, num_planes);
639 for (i = ARRAY_SIZE(dev_priv->display.bw.max) - 1; i >= 0; i--) {
640 const struct intel_bw_info *bi =
641 &dev_priv->display.bw.max[i];
644 * Pcode will not expose all QGV points when
645 * SAGV is forced to off/min/med/max.
647 if (qgv_point >= bi->num_qgv_points)
650 if (num_planes <= bi->num_planes)
657 static unsigned int adl_psf_bw(struct drm_i915_private *dev_priv,
660 const struct intel_bw_info *bi =
661 &dev_priv->display.bw.max[0];
663 return bi->psf_bw[psf_gv_point];
666 static unsigned int icl_qgv_bw(struct drm_i915_private *i915,
667 int num_active_planes, int qgv_point)
671 if (DISPLAY_VER(i915) >= 12)
672 idx = tgl_max_bw_index(i915, num_active_planes, qgv_point);
674 idx = icl_max_bw_index(i915, num_active_planes, qgv_point);
676 if (idx >= ARRAY_SIZE(i915->display.bw.max))
679 return i915->display.bw.max[idx].deratedbw[qgv_point];
682 void intel_bw_init_hw(struct drm_i915_private *dev_priv)
684 if (!HAS_DISPLAY(dev_priv))
687 if (DISPLAY_VER(dev_priv) >= 14)
688 tgl_get_bw_info(dev_priv, &mtl_sa_info);
689 else if (IS_DG2(dev_priv))
690 dg2_get_bw_info(dev_priv);
691 else if (IS_ALDERLAKE_P(dev_priv))
692 tgl_get_bw_info(dev_priv, &adlp_sa_info);
693 else if (IS_ALDERLAKE_S(dev_priv))
694 tgl_get_bw_info(dev_priv, &adls_sa_info);
695 else if (IS_ROCKETLAKE(dev_priv))
696 tgl_get_bw_info(dev_priv, &rkl_sa_info);
697 else if (DISPLAY_VER(dev_priv) == 12)
698 tgl_get_bw_info(dev_priv, &tgl_sa_info);
699 else if (DISPLAY_VER(dev_priv) == 11)
700 icl_get_bw_info(dev_priv, &icl_sa_info);
703 static unsigned int intel_bw_crtc_num_active_planes(const struct intel_crtc_state *crtc_state)
706 * We assume cursors are small enough
707 * to not not cause bandwidth problems.
709 return hweight8(crtc_state->active_planes & ~BIT(PLANE_CURSOR));
712 static unsigned int intel_bw_crtc_data_rate(const struct intel_crtc_state *crtc_state)
714 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
715 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
716 unsigned int data_rate = 0;
717 enum plane_id plane_id;
719 for_each_plane_id_on_crtc(crtc, plane_id) {
721 * We assume cursors are small enough
722 * to not not cause bandwidth problems.
724 if (plane_id == PLANE_CURSOR)
727 data_rate += crtc_state->data_rate[plane_id];
729 if (DISPLAY_VER(i915) < 11)
730 data_rate += crtc_state->data_rate_y[plane_id];
736 /* "Maximum Pipe Read Bandwidth" */
737 static int intel_bw_crtc_min_cdclk(const struct intel_crtc_state *crtc_state)
739 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
740 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
742 if (DISPLAY_VER(i915) < 12)
745 return DIV_ROUND_UP_ULL(mul_u32_u32(intel_bw_crtc_data_rate(crtc_state), 10), 512);
748 void intel_bw_crtc_update(struct intel_bw_state *bw_state,
749 const struct intel_crtc_state *crtc_state)
751 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
752 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
754 bw_state->data_rate[crtc->pipe] =
755 intel_bw_crtc_data_rate(crtc_state);
756 bw_state->num_active_planes[crtc->pipe] =
757 intel_bw_crtc_num_active_planes(crtc_state);
758 bw_state->force_check_qgv = true;
760 drm_dbg_kms(&i915->drm, "pipe %c data rate %u num active planes %u\n",
761 pipe_name(crtc->pipe),
762 bw_state->data_rate[crtc->pipe],
763 bw_state->num_active_planes[crtc->pipe]);
766 static unsigned int intel_bw_num_active_planes(struct drm_i915_private *dev_priv,
767 const struct intel_bw_state *bw_state)
769 unsigned int num_active_planes = 0;
772 for_each_pipe(dev_priv, pipe)
773 num_active_planes += bw_state->num_active_planes[pipe];
775 return num_active_planes;
778 static unsigned int intel_bw_data_rate(struct drm_i915_private *dev_priv,
779 const struct intel_bw_state *bw_state)
781 unsigned int data_rate = 0;
784 for_each_pipe(dev_priv, pipe)
785 data_rate += bw_state->data_rate[pipe];
787 if (DISPLAY_VER(dev_priv) >= 13 && i915_vtd_active(dev_priv))
788 data_rate = DIV_ROUND_UP(data_rate * 105, 100);
793 struct intel_bw_state *
794 intel_atomic_get_old_bw_state(struct intel_atomic_state *state)
796 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
797 struct intel_global_state *bw_state;
799 bw_state = intel_atomic_get_old_global_obj_state(state, &dev_priv->display.bw.obj);
801 return to_intel_bw_state(bw_state);
804 struct intel_bw_state *
805 intel_atomic_get_new_bw_state(struct intel_atomic_state *state)
807 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
808 struct intel_global_state *bw_state;
810 bw_state = intel_atomic_get_new_global_obj_state(state, &dev_priv->display.bw.obj);
812 return to_intel_bw_state(bw_state);
815 struct intel_bw_state *
816 intel_atomic_get_bw_state(struct intel_atomic_state *state)
818 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
819 struct intel_global_state *bw_state;
821 bw_state = intel_atomic_get_global_obj_state(state, &dev_priv->display.bw.obj);
822 if (IS_ERR(bw_state))
823 return ERR_CAST(bw_state);
825 return to_intel_bw_state(bw_state);
828 static unsigned int icl_max_bw_qgv_point_mask(struct drm_i915_private *i915,
829 int num_active_planes)
831 unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
832 unsigned int max_bw_point = 0;
833 unsigned int max_bw = 0;
836 for (i = 0; i < num_qgv_points; i++) {
837 unsigned int max_data_rate =
838 icl_qgv_bw(i915, num_active_planes, i);
841 * We need to know which qgv point gives us
842 * maximum bandwidth in order to disable SAGV
843 * if we find that we exceed SAGV block time
844 * with watermarks. By that moment we already
845 * have those, as it is calculated earlier in
846 * intel_atomic_check,
848 if (max_data_rate > max_bw) {
849 max_bw_point = BIT(i);
850 max_bw = max_data_rate;
857 static u16 icl_prepare_qgv_points_mask(struct drm_i915_private *i915,
858 unsigned int qgv_points,
859 unsigned int psf_points)
861 return ~(ICL_PCODE_REQ_QGV_PT(qgv_points) |
862 ADLS_PCODE_REQ_PSF_PT(psf_points)) & icl_qgv_points_mask(i915);
865 static unsigned int icl_max_bw_psf_gv_point_mask(struct drm_i915_private *i915)
867 unsigned int num_psf_gv_points = i915->display.bw.max[0].num_psf_gv_points;
868 unsigned int max_bw_point_mask = 0;
869 unsigned int max_bw = 0;
872 for (i = 0; i < num_psf_gv_points; i++) {
873 unsigned int max_data_rate = adl_psf_bw(i915, i);
875 if (max_data_rate > max_bw) {
876 max_bw_point_mask = BIT(i);
877 max_bw = max_data_rate;
878 } else if (max_data_rate == max_bw) {
879 max_bw_point_mask |= BIT(i);
883 return max_bw_point_mask;
886 static void icl_force_disable_sagv(struct drm_i915_private *i915,
887 struct intel_bw_state *bw_state)
889 unsigned int qgv_points = icl_max_bw_qgv_point_mask(i915, 0);
890 unsigned int psf_points = icl_max_bw_psf_gv_point_mask(i915);
892 bw_state->qgv_points_mask = icl_prepare_qgv_points_mask(i915,
896 drm_dbg_kms(&i915->drm, "Forcing SAGV disable: mask 0x%x\n",
897 bw_state->qgv_points_mask);
899 icl_pcode_restrict_qgv_points(i915, bw_state->qgv_points_mask);
902 static int mtl_find_qgv_points(struct drm_i915_private *i915,
903 unsigned int data_rate,
904 unsigned int num_active_planes,
905 struct intel_bw_state *new_bw_state)
907 unsigned int best_rate = UINT_MAX;
908 unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
909 unsigned int qgv_peak_bw = 0;
913 ret = intel_atomic_lock_global_state(&new_bw_state->base);
918 * If SAGV cannot be enabled, disable the pcode SAGV by passing all 1's
919 * for qgv peak bw in PM Demand request. So assign UINT_MAX if SAGV is
920 * not enabled. PM Demand code will clamp the value for the register
922 if (!intel_can_enable_sagv(i915, new_bw_state)) {
923 new_bw_state->qgv_point_peakbw = U16_MAX;
924 drm_dbg_kms(&i915->drm, "No SAGV, use UINT_MAX as peak bw.");
929 * Find the best QGV point by comparing the data_rate with max data rate
930 * offered per plane group
932 for (i = 0; i < num_qgv_points; i++) {
933 unsigned int bw_index =
934 tgl_max_bw_index(i915, num_active_planes, i);
935 unsigned int max_data_rate;
937 if (bw_index >= ARRAY_SIZE(i915->display.bw.max))
940 max_data_rate = i915->display.bw.max[bw_index].deratedbw[i];
942 if (max_data_rate < data_rate)
945 if (max_data_rate - data_rate < best_rate) {
946 best_rate = max_data_rate - data_rate;
947 qgv_peak_bw = i915->display.bw.max[bw_index].peakbw[i];
950 drm_dbg_kms(&i915->drm, "QGV point %d: max bw %d required %d qgv_peak_bw: %d\n",
951 i, max_data_rate, data_rate, qgv_peak_bw);
954 drm_dbg_kms(&i915->drm, "Matching peaks QGV bw: %d for required data rate: %d\n",
955 qgv_peak_bw, data_rate);
958 * The display configuration cannot be supported if no QGV point
959 * satisfying the required data rate is found
961 if (qgv_peak_bw == 0) {
962 drm_dbg_kms(&i915->drm, "No QGV points for bw %d for display configuration(%d active planes).\n",
963 data_rate, num_active_planes);
967 /* MTL PM DEMAND expects QGV BW parameter in multiples of 100 mbps */
968 new_bw_state->qgv_point_peakbw = DIV_ROUND_CLOSEST(qgv_peak_bw, 100);
973 static int icl_find_qgv_points(struct drm_i915_private *i915,
974 unsigned int data_rate,
975 unsigned int num_active_planes,
976 const struct intel_bw_state *old_bw_state,
977 struct intel_bw_state *new_bw_state)
979 unsigned int num_psf_gv_points = i915->display.bw.max[0].num_psf_gv_points;
980 unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
986 ret = intel_atomic_lock_global_state(&new_bw_state->base);
990 for (i = 0; i < num_qgv_points; i++) {
991 unsigned int max_data_rate = icl_qgv_bw(i915,
992 num_active_planes, i);
993 if (max_data_rate >= data_rate)
994 qgv_points |= BIT(i);
996 drm_dbg_kms(&i915->drm, "QGV point %d: max bw %d required %d\n",
997 i, max_data_rate, data_rate);
1000 for (i = 0; i < num_psf_gv_points; i++) {
1001 unsigned int max_data_rate = adl_psf_bw(i915, i);
1003 if (max_data_rate >= data_rate)
1004 psf_points |= BIT(i);
1006 drm_dbg_kms(&i915->drm, "PSF GV point %d: max bw %d"
1008 i, max_data_rate, data_rate);
1012 * BSpec states that we always should have at least one allowed point
1013 * left, so if we couldn't - simply reject the configuration for obvious
1016 if (qgv_points == 0) {
1017 drm_dbg_kms(&i915->drm, "No QGV points provide sufficient memory"
1018 " bandwidth %d for display configuration(%d active planes).\n",
1019 data_rate, num_active_planes);
1023 if (num_psf_gv_points > 0 && psf_points == 0) {
1024 drm_dbg_kms(&i915->drm, "No PSF GV points provide sufficient memory"
1025 " bandwidth %d for display configuration(%d active planes).\n",
1026 data_rate, num_active_planes);
1031 * Leave only single point with highest bandwidth, if
1032 * we can't enable SAGV due to the increased memory latency it may
1035 if (!intel_can_enable_sagv(i915, new_bw_state)) {
1036 qgv_points = icl_max_bw_qgv_point_mask(i915, num_active_planes);
1037 drm_dbg_kms(&i915->drm, "No SAGV, using single QGV point mask 0x%x\n",
1042 * We store the ones which need to be masked as that is what PCode
1043 * actually accepts as a parameter.
1045 new_bw_state->qgv_points_mask = icl_prepare_qgv_points_mask(i915,
1049 * If the actual mask had changed we need to make sure that
1050 * the commits are serialized(in case this is a nomodeset, nonblocking)
1052 if (new_bw_state->qgv_points_mask != old_bw_state->qgv_points_mask) {
1053 ret = intel_atomic_serialize_global_state(&new_bw_state->base);
1061 static int intel_bw_check_qgv_points(struct drm_i915_private *i915,
1062 const struct intel_bw_state *old_bw_state,
1063 struct intel_bw_state *new_bw_state)
1065 unsigned int data_rate = intel_bw_data_rate(i915, new_bw_state);
1066 unsigned int num_active_planes =
1067 intel_bw_num_active_planes(i915, new_bw_state);
1069 data_rate = DIV_ROUND_UP(data_rate, 1000);
1071 if (DISPLAY_VER(i915) >= 14)
1072 return mtl_find_qgv_points(i915, data_rate, num_active_planes,
1075 return icl_find_qgv_points(i915, data_rate, num_active_planes,
1076 old_bw_state, new_bw_state);
1079 static bool intel_bw_state_changed(struct drm_i915_private *i915,
1080 const struct intel_bw_state *old_bw_state,
1081 const struct intel_bw_state *new_bw_state)
1085 for_each_pipe(i915, pipe) {
1086 const struct intel_dbuf_bw *old_crtc_bw =
1087 &old_bw_state->dbuf_bw[pipe];
1088 const struct intel_dbuf_bw *new_crtc_bw =
1089 &new_bw_state->dbuf_bw[pipe];
1090 enum dbuf_slice slice;
1092 for_each_dbuf_slice(i915, slice) {
1093 if (old_crtc_bw->max_bw[slice] != new_crtc_bw->max_bw[slice] ||
1094 old_crtc_bw->active_planes[slice] != new_crtc_bw->active_planes[slice])
1098 if (old_bw_state->min_cdclk[pipe] != new_bw_state->min_cdclk[pipe])
1105 static void skl_plane_calc_dbuf_bw(struct intel_bw_state *bw_state,
1106 struct intel_crtc *crtc,
1107 enum plane_id plane_id,
1108 const struct skl_ddb_entry *ddb,
1109 unsigned int data_rate)
1111 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1112 struct intel_dbuf_bw *crtc_bw = &bw_state->dbuf_bw[crtc->pipe];
1113 unsigned int dbuf_mask = skl_ddb_dbuf_slice_mask(i915, ddb);
1114 enum dbuf_slice slice;
1117 * The arbiter can only really guarantee an
1118 * equal share of the total bw to each plane.
1120 for_each_dbuf_slice_in_mask(i915, slice, dbuf_mask) {
1121 crtc_bw->max_bw[slice] = max(crtc_bw->max_bw[slice], data_rate);
1122 crtc_bw->active_planes[slice] |= BIT(plane_id);
1126 static void skl_crtc_calc_dbuf_bw(struct intel_bw_state *bw_state,
1127 const struct intel_crtc_state *crtc_state)
1129 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1130 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1131 struct intel_dbuf_bw *crtc_bw = &bw_state->dbuf_bw[crtc->pipe];
1132 enum plane_id plane_id;
1134 memset(crtc_bw, 0, sizeof(*crtc_bw));
1136 if (!crtc_state->hw.active)
1139 for_each_plane_id_on_crtc(crtc, plane_id) {
1141 * We assume cursors are small enough
1142 * to not cause bandwidth problems.
1144 if (plane_id == PLANE_CURSOR)
1147 skl_plane_calc_dbuf_bw(bw_state, crtc, plane_id,
1148 &crtc_state->wm.skl.plane_ddb[plane_id],
1149 crtc_state->data_rate[plane_id]);
1151 if (DISPLAY_VER(i915) < 11)
1152 skl_plane_calc_dbuf_bw(bw_state, crtc, plane_id,
1153 &crtc_state->wm.skl.plane_ddb_y[plane_id],
1154 crtc_state->data_rate[plane_id]);
1158 /* "Maximum Data Buffer Bandwidth" */
1160 intel_bw_dbuf_min_cdclk(struct drm_i915_private *i915,
1161 const struct intel_bw_state *bw_state)
1163 unsigned int total_max_bw = 0;
1164 enum dbuf_slice slice;
1166 for_each_dbuf_slice(i915, slice) {
1167 int num_active_planes = 0;
1168 unsigned int max_bw = 0;
1172 * The arbiter can only really guarantee an
1173 * equal share of the total bw to each plane.
1175 for_each_pipe(i915, pipe) {
1176 const struct intel_dbuf_bw *crtc_bw = &bw_state->dbuf_bw[pipe];
1178 max_bw = max(crtc_bw->max_bw[slice], max_bw);
1179 num_active_planes += hweight8(crtc_bw->active_planes[slice]);
1181 max_bw *= num_active_planes;
1183 total_max_bw = max(total_max_bw, max_bw);
1186 return DIV_ROUND_UP(total_max_bw, 64);
1189 int intel_bw_min_cdclk(struct drm_i915_private *i915,
1190 const struct intel_bw_state *bw_state)
1195 min_cdclk = intel_bw_dbuf_min_cdclk(i915, bw_state);
1197 for_each_pipe(i915, pipe)
1198 min_cdclk = max(bw_state->min_cdclk[pipe], min_cdclk);
1203 int intel_bw_calc_min_cdclk(struct intel_atomic_state *state,
1204 bool *need_cdclk_calc)
1206 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1207 struct intel_bw_state *new_bw_state = NULL;
1208 const struct intel_bw_state *old_bw_state = NULL;
1209 const struct intel_cdclk_state *cdclk_state;
1210 const struct intel_crtc_state *crtc_state;
1211 int old_min_cdclk, new_min_cdclk;
1212 struct intel_crtc *crtc;
1215 if (DISPLAY_VER(dev_priv) < 9)
1218 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
1219 new_bw_state = intel_atomic_get_bw_state(state);
1220 if (IS_ERR(new_bw_state))
1221 return PTR_ERR(new_bw_state);
1223 old_bw_state = intel_atomic_get_old_bw_state(state);
1225 skl_crtc_calc_dbuf_bw(new_bw_state, crtc_state);
1227 new_bw_state->min_cdclk[crtc->pipe] =
1228 intel_bw_crtc_min_cdclk(crtc_state);
1234 if (intel_bw_state_changed(dev_priv, old_bw_state, new_bw_state)) {
1235 int ret = intel_atomic_lock_global_state(&new_bw_state->base);
1240 old_min_cdclk = intel_bw_min_cdclk(dev_priv, old_bw_state);
1241 new_min_cdclk = intel_bw_min_cdclk(dev_priv, new_bw_state);
1244 * No need to check against the cdclk state if
1245 * the min cdclk doesn't increase.
1247 * Ie. we only ever increase the cdclk due to bandwidth
1248 * requirements. This can reduce back and forth
1249 * display blinking due to constant cdclk changes.
1251 if (new_min_cdclk <= old_min_cdclk)
1254 cdclk_state = intel_atomic_get_cdclk_state(state);
1255 if (IS_ERR(cdclk_state))
1256 return PTR_ERR(cdclk_state);
1259 * No need to recalculate the cdclk state if
1260 * the min cdclk doesn't increase.
1262 * Ie. we only ever increase the cdclk due to bandwidth
1263 * requirements. This can reduce back and forth
1264 * display blinking due to constant cdclk changes.
1266 if (new_min_cdclk <= cdclk_state->bw_min_cdclk)
1269 drm_dbg_kms(&dev_priv->drm,
1270 "new bandwidth min cdclk (%d kHz) > old min cdclk (%d kHz)\n",
1271 new_min_cdclk, cdclk_state->bw_min_cdclk);
1272 *need_cdclk_calc = true;
1277 static int intel_bw_check_data_rate(struct intel_atomic_state *state, bool *changed)
1279 struct drm_i915_private *i915 = to_i915(state->base.dev);
1280 const struct intel_crtc_state *new_crtc_state, *old_crtc_state;
1281 struct intel_crtc *crtc;
1284 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
1285 new_crtc_state, i) {
1286 unsigned int old_data_rate =
1287 intel_bw_crtc_data_rate(old_crtc_state);
1288 unsigned int new_data_rate =
1289 intel_bw_crtc_data_rate(new_crtc_state);
1290 unsigned int old_active_planes =
1291 intel_bw_crtc_num_active_planes(old_crtc_state);
1292 unsigned int new_active_planes =
1293 intel_bw_crtc_num_active_planes(new_crtc_state);
1294 struct intel_bw_state *new_bw_state;
1297 * Avoid locking the bw state when
1298 * nothing significant has changed.
1300 if (old_data_rate == new_data_rate &&
1301 old_active_planes == new_active_planes)
1304 new_bw_state = intel_atomic_get_bw_state(state);
1305 if (IS_ERR(new_bw_state))
1306 return PTR_ERR(new_bw_state);
1308 new_bw_state->data_rate[crtc->pipe] = new_data_rate;
1309 new_bw_state->num_active_planes[crtc->pipe] = new_active_planes;
1313 drm_dbg_kms(&i915->drm,
1314 "[CRTC:%d:%s] data rate %u num active planes %u\n",
1315 crtc->base.base.id, crtc->base.name,
1316 new_bw_state->data_rate[crtc->pipe],
1317 new_bw_state->num_active_planes[crtc->pipe]);
1323 int intel_bw_atomic_check(struct intel_atomic_state *state)
1325 bool changed = false;
1326 struct drm_i915_private *i915 = to_i915(state->base.dev);
1327 struct intel_bw_state *new_bw_state;
1328 const struct intel_bw_state *old_bw_state;
1331 /* FIXME earlier gens need some checks too */
1332 if (DISPLAY_VER(i915) < 11)
1335 ret = intel_bw_check_data_rate(state, &changed);
1339 old_bw_state = intel_atomic_get_old_bw_state(state);
1340 new_bw_state = intel_atomic_get_new_bw_state(state);
1343 (intel_can_enable_sagv(i915, old_bw_state) !=
1344 intel_can_enable_sagv(i915, new_bw_state) ||
1345 new_bw_state->force_check_qgv))
1349 * If none of our inputs (data rates, number of active
1350 * planes, SAGV yes/no) changed then nothing to do here.
1355 ret = intel_bw_check_qgv_points(i915, old_bw_state, new_bw_state);
1359 new_bw_state->force_check_qgv = false;
1364 static struct intel_global_state *
1365 intel_bw_duplicate_state(struct intel_global_obj *obj)
1367 struct intel_bw_state *state;
1369 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
1373 return &state->base;
1376 static void intel_bw_destroy_state(struct intel_global_obj *obj,
1377 struct intel_global_state *state)
1382 static const struct intel_global_state_funcs intel_bw_funcs = {
1383 .atomic_duplicate_state = intel_bw_duplicate_state,
1384 .atomic_destroy_state = intel_bw_destroy_state,
1387 int intel_bw_init(struct drm_i915_private *i915)
1389 struct intel_bw_state *state;
1391 state = kzalloc(sizeof(*state), GFP_KERNEL);
1395 intel_atomic_global_obj_init(i915, &i915->display.bw.obj,
1396 &state->base, &intel_bw_funcs);
1399 * Limit this only if we have SAGV. And for Display version 14 onwards
1400 * sagv is handled though pmdemand requests
1402 if (intel_has_sagv(i915) && IS_DISPLAY_VER(i915, 11, 13))
1403 icl_force_disable_sagv(i915, state);