1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2019 Intel Corporation
4 */
5
6 #include <drm/drm_atomic_state_helper.h>
7
8 #include "i915_drv.h"
9 #include "i915_reg.h"
10 #include "i915_utils.h"
11 #include "intel_atomic.h"
12 #include "intel_bw.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"
19
20 /* Parameters for Qclk Geyserville (QGV) */
21 struct intel_qgv_point {
22 u16 dclk, t_rp, t_rdpre, t_rc, t_ras, t_rcd;
23 };
24
25 #define DEPROGBWPCLIMIT 60
26
27 struct intel_psf_gv_point {
28 u8 clk; /* clock in multiples of 16.6666 MHz */
29 };
30
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];
34 u8 num_points;
35 u8 num_psf_points;
36 u8 t_bl;
37 u8 max_numchannels;
38 u8 channel_width;
39 u8 deinterleave;
40 };
41
dg1_mchbar_read_qgv_point_info(struct drm_i915_private * dev_priv,struct intel_qgv_point * sp,int point)42 static int dg1_mchbar_read_qgv_point_info(struct drm_i915_private *dev_priv,
43 struct intel_qgv_point *sp,
44 int point)
45 {
46 u32 dclk_ratio, dclk_reference;
47 u32 val;
48
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 */
53 else
54 dclk_reference = 8; /* 8 * 16.666 MHz = 133 MHz */
55 sp->dclk = DIV_ROUND_UP((16667 * dclk_ratio * dclk_reference) + 500, 1000);
56
57 val = intel_uncore_read(&dev_priv->uncore, SKL_MC_BIOS_DATA_0_0_0_MCHBAR_PCU);
58 if (val & DG1_GEAR_TYPE)
59 sp->dclk *= 2;
60
61 if (sp->dclk == 0)
62 return -EINVAL;
63
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);
67
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);
71
72 sp->t_rc = sp->t_rp + sp->t_ras;
73
74 return 0;
75 }
76
icl_pcode_read_qgv_point_info(struct drm_i915_private * dev_priv,struct intel_qgv_point * sp,int point)77 static int icl_pcode_read_qgv_point_info(struct drm_i915_private *dev_priv,
78 struct intel_qgv_point *sp,
79 int point)
80 {
81 u32 val = 0, val2 = 0;
82 u16 dclk;
83 int ret;
84
85 ret = snb_pcode_read(&dev_priv->uncore, ICL_PCODE_MEM_SUBSYSYSTEM_INFO |
86 ICL_PCODE_MEM_SS_READ_QGV_POINT_INFO(point),
87 &val, &val2);
88 if (ret)
89 return ret;
90
91 dclk = val & 0xffff;
92 sp->dclk = DIV_ROUND_UP((16667 * dclk) + (DISPLAY_VER(dev_priv) >= 12 ? 500 : 0),
93 1000);
94 sp->t_rp = (val & 0xff0000) >> 16;
95 sp->t_rcd = (val & 0xff000000) >> 24;
96
97 sp->t_rdpre = val2 & 0xff;
98 sp->t_ras = (val2 & 0xff00) >> 8;
99
100 sp->t_rc = sp->t_rp + sp->t_ras;
101
102 return 0;
103 }
104
adls_pcode_read_psf_gv_point_info(struct drm_i915_private * dev_priv,struct intel_psf_gv_point * points)105 static int adls_pcode_read_psf_gv_point_info(struct drm_i915_private *dev_priv,
106 struct intel_psf_gv_point *points)
107 {
108 u32 val = 0;
109 int ret;
110 int i;
111
112 ret = snb_pcode_read(&dev_priv->uncore, ICL_PCODE_MEM_SUBSYSYSTEM_INFO |
113 ADL_PCODE_MEM_SS_READ_PSF_GV_INFO, &val, NULL);
114 if (ret)
115 return ret;
116
117 for (i = 0; i < I915_NUM_PSF_GV_POINTS; i++) {
118 points[i].clk = val & 0xff;
119 val >>= 8;
120 }
121
122 return 0;
123 }
124
icl_qgv_points_mask(struct drm_i915_private * i915)125 static u16 icl_qgv_points_mask(struct drm_i915_private *i915)
126 {
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;
130
131 /*
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.
135 */
136 if (num_qgv_points > 0)
137 qgv_points = GENMASK(num_qgv_points - 1, 0);
138
139 if (num_psf_gv_points > 0)
140 psf_points = GENMASK(num_psf_gv_points - 1, 0);
141
142 return ICL_PCODE_REQ_QGV_PT(qgv_points) | ADLS_PCODE_REQ_PSF_PT(psf_points);
143 }
144
is_sagv_enabled(struct drm_i915_private * i915,u16 points_mask)145 static bool is_sagv_enabled(struct drm_i915_private *i915, u16 points_mask)
146 {
147 return !is_power_of_2(~points_mask & icl_qgv_points_mask(i915) &
148 ICL_PCODE_REQ_QGV_PT_MASK);
149 }
150
icl_pcode_restrict_qgv_points(struct drm_i915_private * dev_priv,u32 points_mask)151 int icl_pcode_restrict_qgv_points(struct drm_i915_private *dev_priv,
152 u32 points_mask)
153 {
154 int ret;
155
156 if (DISPLAY_VER(dev_priv) >= 14)
157 return 0;
158
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,
161 points_mask,
162 ICL_PCODE_REP_QGV_MASK | ADLS_PCODE_REP_PSF_MASK,
163 ICL_PCODE_REP_QGV_SAFE | ADLS_PCODE_REP_PSF_SAFE,
164 1);
165
166 if (ret < 0) {
167 drm_err(&dev_priv->drm,
168 "Failed to disable qgv points (0x%x) points: 0x%x\n",
169 ret, points_mask);
170 return ret;
171 }
172
173 dev_priv->display.sagv.status = is_sagv_enabled(dev_priv, points_mask) ?
174 I915_SAGV_ENABLED : I915_SAGV_DISABLED;
175
176 return 0;
177 }
178
mtl_read_qgv_point_info(struct drm_i915_private * dev_priv,struct intel_qgv_point * sp,int point)179 static int mtl_read_qgv_point_info(struct drm_i915_private *dev_priv,
180 struct intel_qgv_point *sp, int point)
181 {
182 u32 val, val2;
183 u16 dclk;
184
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);
193
194 sp->t_rdpre = REG_FIELD_GET(MTL_TRDPRE_MASK, val2);
195 sp->t_ras = REG_FIELD_GET(MTL_TRAS_MASK, val2);
196
197 sp->t_rc = sp->t_rp + sp->t_ras;
198
199 return 0;
200 }
201
202 static int
intel_read_qgv_point_info(struct drm_i915_private * dev_priv,struct intel_qgv_point * sp,int point)203 intel_read_qgv_point_info(struct drm_i915_private *dev_priv,
204 struct intel_qgv_point *sp,
205 int point)
206 {
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);
211 else
212 return icl_pcode_read_qgv_point_info(dev_priv, sp, point);
213 }
214
icl_get_qgv_points(struct drm_i915_private * dev_priv,struct intel_qgv_info * qi,bool is_y_tile)215 static int icl_get_qgv_points(struct drm_i915_private *dev_priv,
216 struct intel_qgv_info *qi,
217 bool is_y_tile)
218 {
219 const struct dram_info *dram_info = &dev_priv->dram_info;
220 int i, ret;
221
222 qi->num_points = dram_info->num_qgv_points;
223 qi->num_psf_points = dram_info->num_psf_gv_points;
224
225 if (DISPLAY_VER(dev_priv) >= 14) {
226 switch (dram_info->type) {
227 case INTEL_DRAM_DDR4:
228 qi->t_bl = 4;
229 qi->max_numchannels = 2;
230 qi->channel_width = 64;
231 qi->deinterleave = 2;
232 break;
233 case INTEL_DRAM_DDR5:
234 qi->t_bl = 8;
235 qi->max_numchannels = 4;
236 qi->channel_width = 32;
237 qi->deinterleave = 2;
238 break;
239 case INTEL_DRAM_LPDDR4:
240 case INTEL_DRAM_LPDDR5:
241 qi->t_bl = 16;
242 qi->max_numchannels = 8;
243 qi->channel_width = 16;
244 qi->deinterleave = 4;
245 break;
246 case INTEL_DRAM_GDDR:
247 case INTEL_DRAM_GDDR_ECC:
248 qi->channel_width = 32;
249 break;
250 default:
251 MISSING_CASE(dram_info->type);
252 return -EINVAL;
253 }
254 } else if (DISPLAY_VER(dev_priv) >= 12) {
255 switch (dram_info->type) {
256 case INTEL_DRAM_DDR4:
257 qi->t_bl = is_y_tile ? 8 : 4;
258 qi->max_numchannels = 2;
259 qi->channel_width = 64;
260 qi->deinterleave = is_y_tile ? 1 : 2;
261 break;
262 case INTEL_DRAM_DDR5:
263 qi->t_bl = is_y_tile ? 16 : 8;
264 qi->max_numchannels = 4;
265 qi->channel_width = 32;
266 qi->deinterleave = is_y_tile ? 1 : 2;
267 break;
268 case INTEL_DRAM_LPDDR4:
269 if (IS_ROCKETLAKE(dev_priv)) {
270 qi->t_bl = 8;
271 qi->max_numchannels = 4;
272 qi->channel_width = 32;
273 qi->deinterleave = 2;
274 break;
275 }
276 fallthrough;
277 case INTEL_DRAM_LPDDR5:
278 qi->t_bl = 16;
279 qi->max_numchannels = 8;
280 qi->channel_width = 16;
281 qi->deinterleave = is_y_tile ? 2 : 4;
282 break;
283 default:
284 qi->t_bl = 16;
285 qi->max_numchannels = 1;
286 break;
287 }
288 } else if (DISPLAY_VER(dev_priv) == 11) {
289 qi->t_bl = dev_priv->dram_info.type == INTEL_DRAM_DDR4 ? 4 : 8;
290 qi->max_numchannels = 1;
291 }
292
293 if (drm_WARN_ON(&dev_priv->drm,
294 qi->num_points > ARRAY_SIZE(qi->points)))
295 qi->num_points = ARRAY_SIZE(qi->points);
296
297 for (i = 0; i < qi->num_points; i++) {
298 struct intel_qgv_point *sp = &qi->points[i];
299
300 ret = intel_read_qgv_point_info(dev_priv, sp, i);
301 if (ret) {
302 drm_dbg_kms(&dev_priv->drm, "Could not read QGV %d info\n", i);
303 return ret;
304 }
305
306 drm_dbg_kms(&dev_priv->drm,
307 "QGV %d: DCLK=%d tRP=%d tRDPRE=%d tRAS=%d tRCD=%d tRC=%d\n",
308 i, sp->dclk, sp->t_rp, sp->t_rdpre, sp->t_ras,
309 sp->t_rcd, sp->t_rc);
310 }
311
312 if (qi->num_psf_points > 0) {
313 ret = adls_pcode_read_psf_gv_point_info(dev_priv, qi->psf_points);
314 if (ret) {
315 drm_err(&dev_priv->drm, "Failed to read PSF point data; PSF points will not be considered in bandwidth calculations.\n");
316 qi->num_psf_points = 0;
317 }
318
319 for (i = 0; i < qi->num_psf_points; i++)
320 drm_dbg_kms(&dev_priv->drm,
321 "PSF GV %d: CLK=%d \n",
322 i, qi->psf_points[i].clk);
323 }
324
325 return 0;
326 }
327
adl_calc_psf_bw(int clk)328 static int adl_calc_psf_bw(int clk)
329 {
330 /*
331 * clk is multiples of 16.666MHz (100/6)
332 * According to BSpec PSF GV bandwidth is
333 * calculated as BW = 64 * clk * 16.666Mhz
334 */
335 return DIV_ROUND_CLOSEST(64 * clk * 100, 6);
336 }
337
icl_sagv_max_dclk(const struct intel_qgv_info * qi)338 static int icl_sagv_max_dclk(const struct intel_qgv_info *qi)
339 {
340 u16 dclk = 0;
341 int i;
342
343 for (i = 0; i < qi->num_points; i++)
344 dclk = max(dclk, qi->points[i].dclk);
345
346 return dclk;
347 }
348
349 struct intel_sa_info {
350 u16 displayrtids;
351 u8 deburst, deprogbwlimit, derating;
352 };
353
354 static const struct intel_sa_info icl_sa_info = {
355 .deburst = 8,
356 .deprogbwlimit = 25, /* GB/s */
357 .displayrtids = 128,
358 .derating = 10,
359 };
360
361 static const struct intel_sa_info tgl_sa_info = {
362 .deburst = 16,
363 .deprogbwlimit = 34, /* GB/s */
364 .displayrtids = 256,
365 .derating = 10,
366 };
367
368 static const struct intel_sa_info rkl_sa_info = {
369 .deburst = 8,
370 .deprogbwlimit = 20, /* GB/s */
371 .displayrtids = 128,
372 .derating = 10,
373 };
374
375 static const struct intel_sa_info adls_sa_info = {
376 .deburst = 16,
377 .deprogbwlimit = 38, /* GB/s */
378 .displayrtids = 256,
379 .derating = 10,
380 };
381
382 static const struct intel_sa_info adlp_sa_info = {
383 .deburst = 16,
384 .deprogbwlimit = 38, /* GB/s */
385 .displayrtids = 256,
386 .derating = 20,
387 };
388
389 static const struct intel_sa_info mtl_sa_info = {
390 .deburst = 32,
391 .deprogbwlimit = 38, /* GB/s */
392 .displayrtids = 256,
393 .derating = 10,
394 };
395
396 static const struct intel_sa_info xe2_hpd_sa_info = {
397 .derating = 30,
398 .deprogbwlimit = 53,
399 /* Other values not used by simplified algorithm */
400 };
401
402 static const struct intel_sa_info xe2_hpd_ecc_sa_info = {
403 .derating = 45,
404 .deprogbwlimit = 53,
405 /* Other values not used by simplified algorithm */
406 };
407
icl_get_bw_info(struct drm_i915_private * dev_priv,const struct intel_sa_info * sa)408 static int icl_get_bw_info(struct drm_i915_private *dev_priv, const struct intel_sa_info *sa)
409 {
410 struct intel_qgv_info qi = {};
411 bool is_y_tile = true; /* assume y tile may be used */
412 int num_channels = max_t(u8, 1, dev_priv->dram_info.num_channels);
413 int ipqdepth, ipqdepthpch = 16;
414 int dclk_max;
415 int maxdebw;
416 int num_groups = ARRAY_SIZE(dev_priv->display.bw.max);
417 int i, ret;
418
419 ret = icl_get_qgv_points(dev_priv, &qi, is_y_tile);
420 if (ret) {
421 drm_dbg_kms(&dev_priv->drm,
422 "Failed to get memory subsystem information, ignoring bandwidth limits");
423 return ret;
424 }
425
426 dclk_max = icl_sagv_max_dclk(&qi);
427 maxdebw = min(sa->deprogbwlimit * 1000, dclk_max * 16 * 6 / 10);
428 ipqdepth = min(ipqdepthpch, sa->displayrtids / num_channels);
429 qi.deinterleave = DIV_ROUND_UP(num_channels, is_y_tile ? 4 : 2);
430
431 for (i = 0; i < num_groups; i++) {
432 struct intel_bw_info *bi = &dev_priv->display.bw.max[i];
433 int clpchgroup;
434 int j;
435
436 clpchgroup = (sa->deburst * qi.deinterleave / num_channels) << i;
437 bi->num_planes = (ipqdepth - clpchgroup) / clpchgroup + 1;
438
439 bi->num_qgv_points = qi.num_points;
440 bi->num_psf_gv_points = qi.num_psf_points;
441
442 for (j = 0; j < qi.num_points; j++) {
443 const struct intel_qgv_point *sp = &qi.points[j];
444 int ct, bw;
445
446 /*
447 * Max row cycle time
448 *
449 * FIXME what is the logic behind the
450 * assumed burst length?
451 */
452 ct = max_t(int, sp->t_rc, sp->t_rp + sp->t_rcd +
453 (clpchgroup - 1) * qi.t_bl + sp->t_rdpre);
454 bw = DIV_ROUND_UP(sp->dclk * clpchgroup * 32 * num_channels, ct);
455
456 bi->deratedbw[j] = min(maxdebw,
457 bw * (100 - sa->derating) / 100);
458
459 drm_dbg_kms(&dev_priv->drm,
460 "BW%d / QGV %d: num_planes=%d deratedbw=%u\n",
461 i, j, bi->num_planes, bi->deratedbw[j]);
462 }
463 }
464 /*
465 * In case if SAGV is disabled in BIOS, we always get 1
466 * SAGV point, but we can't send PCode commands to restrict it
467 * as it will fail and pointless anyway.
468 */
469 if (qi.num_points == 1)
470 dev_priv->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
471 else
472 dev_priv->display.sagv.status = I915_SAGV_ENABLED;
473
474 return 0;
475 }
476
tgl_get_bw_info(struct drm_i915_private * dev_priv,const struct intel_sa_info * sa)477 static int tgl_get_bw_info(struct drm_i915_private *dev_priv, const struct intel_sa_info *sa)
478 {
479 struct intel_qgv_info qi = {};
480 const struct dram_info *dram_info = &dev_priv->dram_info;
481 bool is_y_tile = true; /* assume y tile may be used */
482 int num_channels = max_t(u8, 1, dev_priv->dram_info.num_channels);
483 int ipqdepth, ipqdepthpch = 16;
484 int dclk_max;
485 int maxdebw, peakbw;
486 int clperchgroup;
487 int num_groups = ARRAY_SIZE(dev_priv->display.bw.max);
488 int i, ret;
489
490 ret = icl_get_qgv_points(dev_priv, &qi, is_y_tile);
491 if (ret) {
492 drm_dbg_kms(&dev_priv->drm,
493 "Failed to get memory subsystem information, ignoring bandwidth limits");
494 return ret;
495 }
496
497 if (DISPLAY_VER(dev_priv) < 14 &&
498 (dram_info->type == INTEL_DRAM_LPDDR4 || dram_info->type == INTEL_DRAM_LPDDR5))
499 num_channels *= 2;
500
501 qi.deinterleave = qi.deinterleave ? : DIV_ROUND_UP(num_channels, is_y_tile ? 4 : 2);
502
503 if (num_channels < qi.max_numchannels && DISPLAY_VER(dev_priv) >= 12)
504 qi.deinterleave = max(DIV_ROUND_UP(qi.deinterleave, 2), 1);
505
506 if (DISPLAY_VER(dev_priv) >= 12 && num_channels > qi.max_numchannels)
507 drm_warn(&dev_priv->drm, "Number of channels exceeds max number of channels.");
508 if (qi.max_numchannels != 0)
509 num_channels = min_t(u8, num_channels, qi.max_numchannels);
510
511 dclk_max = icl_sagv_max_dclk(&qi);
512
513 peakbw = num_channels * DIV_ROUND_UP(qi.channel_width, 8) * dclk_max;
514 maxdebw = min(sa->deprogbwlimit * 1000, peakbw * DEPROGBWPCLIMIT / 100);
515
516 ipqdepth = min(ipqdepthpch, sa->displayrtids / num_channels);
517 /*
518 * clperchgroup = 4kpagespermempage * clperchperblock,
519 * clperchperblock = 8 / num_channels * interleave
520 */
521 clperchgroup = 4 * DIV_ROUND_UP(8, num_channels) * qi.deinterleave;
522
523 for (i = 0; i < num_groups; i++) {
524 struct intel_bw_info *bi = &dev_priv->display.bw.max[i];
525 struct intel_bw_info *bi_next;
526 int clpchgroup;
527 int j;
528
529 clpchgroup = (sa->deburst * qi.deinterleave / num_channels) << i;
530
531 if (i < num_groups - 1) {
532 bi_next = &dev_priv->display.bw.max[i + 1];
533
534 if (clpchgroup < clperchgroup)
535 bi_next->num_planes = (ipqdepth - clpchgroup) /
536 clpchgroup + 1;
537 else
538 bi_next->num_planes = 0;
539 }
540
541 bi->num_qgv_points = qi.num_points;
542 bi->num_psf_gv_points = qi.num_psf_points;
543
544 for (j = 0; j < qi.num_points; j++) {
545 const struct intel_qgv_point *sp = &qi.points[j];
546 int ct, bw;
547
548 /*
549 * Max row cycle time
550 *
551 * FIXME what is the logic behind the
552 * assumed burst length?
553 */
554 ct = max_t(int, sp->t_rc, sp->t_rp + sp->t_rcd +
555 (clpchgroup - 1) * qi.t_bl + sp->t_rdpre);
556 bw = DIV_ROUND_UP(sp->dclk * clpchgroup * 32 * num_channels, ct);
557
558 bi->deratedbw[j] = min(maxdebw,
559 bw * (100 - sa->derating) / 100);
560 bi->peakbw[j] = DIV_ROUND_CLOSEST(sp->dclk *
561 num_channels *
562 qi.channel_width, 8);
563
564 drm_dbg_kms(&dev_priv->drm,
565 "BW%d / QGV %d: num_planes=%d deratedbw=%u peakbw: %u\n",
566 i, j, bi->num_planes, bi->deratedbw[j],
567 bi->peakbw[j]);
568 }
569
570 for (j = 0; j < qi.num_psf_points; j++) {
571 const struct intel_psf_gv_point *sp = &qi.psf_points[j];
572
573 bi->psf_bw[j] = adl_calc_psf_bw(sp->clk);
574
575 drm_dbg_kms(&dev_priv->drm,
576 "BW%d / PSF GV %d: num_planes=%d bw=%u\n",
577 i, j, bi->num_planes, bi->psf_bw[j]);
578 }
579 }
580
581 /*
582 * In case if SAGV is disabled in BIOS, we always get 1
583 * SAGV point, but we can't send PCode commands to restrict it
584 * as it will fail and pointless anyway.
585 */
586 if (qi.num_points == 1)
587 dev_priv->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
588 else
589 dev_priv->display.sagv.status = I915_SAGV_ENABLED;
590
591 return 0;
592 }
593
dg2_get_bw_info(struct drm_i915_private * i915)594 static void dg2_get_bw_info(struct drm_i915_private *i915)
595 {
596 unsigned int deratedbw = IS_DG2_G11(i915) ? 38000 : 50000;
597 int num_groups = ARRAY_SIZE(i915->display.bw.max);
598 int i;
599
600 /*
601 * DG2 doesn't have SAGV or QGV points, just a constant max bandwidth
602 * that doesn't depend on the number of planes enabled. So fill all the
603 * plane group with constant bw information for uniformity with other
604 * platforms. DG2-G10 platforms have a constant 50 GB/s bandwidth,
605 * whereas DG2-G11 platforms have 38 GB/s.
606 */
607 for (i = 0; i < num_groups; i++) {
608 struct intel_bw_info *bi = &i915->display.bw.max[i];
609
610 bi->num_planes = 1;
611 /* Need only one dummy QGV point per group */
612 bi->num_qgv_points = 1;
613 bi->deratedbw[0] = deratedbw;
614 }
615
616 i915->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
617 }
618
xe2_hpd_get_bw_info(struct drm_i915_private * i915,const struct intel_sa_info * sa)619 static int xe2_hpd_get_bw_info(struct drm_i915_private *i915,
620 const struct intel_sa_info *sa)
621 {
622 struct intel_qgv_info qi = {};
623 int num_channels = i915->dram_info.num_channels;
624 int peakbw, maxdebw;
625 int ret, i;
626
627 ret = icl_get_qgv_points(i915, &qi, true);
628 if (ret) {
629 drm_dbg_kms(&i915->drm,
630 "Failed to get memory subsystem information, ignoring bandwidth limits");
631 return ret;
632 }
633
634 peakbw = num_channels * qi.channel_width / 8 * icl_sagv_max_dclk(&qi);
635 maxdebw = min(sa->deprogbwlimit * 1000, peakbw * DEPROGBWPCLIMIT / 10);
636
637 for (i = 0; i < qi.num_points; i++) {
638 const struct intel_qgv_point *point = &qi.points[i];
639 int bw = num_channels * (qi.channel_width / 8) * point->dclk;
640
641 i915->display.bw.max[0].deratedbw[i] =
642 min(maxdebw, (100 - sa->derating) * bw / 100);
643 i915->display.bw.max[0].peakbw[i] = bw;
644
645 drm_dbg_kms(&i915->drm, "QGV %d: deratedbw=%u peakbw: %u\n",
646 i, i915->display.bw.max[0].deratedbw[i],
647 i915->display.bw.max[0].peakbw[i]);
648 }
649
650 /* Bandwidth does not depend on # of planes; set all groups the same */
651 i915->display.bw.max[0].num_planes = 1;
652 i915->display.bw.max[0].num_qgv_points = qi.num_points;
653 for (i = 1; i < ARRAY_SIZE(i915->display.bw.max); i++)
654 memcpy(&i915->display.bw.max[i], &i915->display.bw.max[0],
655 sizeof(i915->display.bw.max[0]));
656
657 /*
658 * Xe2_HPD should always have exactly two QGV points representing
659 * battery and plugged-in operation.
660 */
661 drm_WARN_ON(&i915->drm, qi.num_points != 2);
662 i915->display.sagv.status = I915_SAGV_ENABLED;
663
664 return 0;
665 }
666
icl_max_bw_index(struct drm_i915_private * dev_priv,int num_planes,int qgv_point)667 static unsigned int icl_max_bw_index(struct drm_i915_private *dev_priv,
668 int num_planes, int qgv_point)
669 {
670 int i;
671
672 /*
673 * Let's return max bw for 0 planes
674 */
675 num_planes = max(1, num_planes);
676
677 for (i = 0; i < ARRAY_SIZE(dev_priv->display.bw.max); i++) {
678 const struct intel_bw_info *bi =
679 &dev_priv->display.bw.max[i];
680
681 /*
682 * Pcode will not expose all QGV points when
683 * SAGV is forced to off/min/med/max.
684 */
685 if (qgv_point >= bi->num_qgv_points)
686 return UINT_MAX;
687
688 if (num_planes >= bi->num_planes)
689 return i;
690 }
691
692 return UINT_MAX;
693 }
694
tgl_max_bw_index(struct drm_i915_private * dev_priv,int num_planes,int qgv_point)695 static unsigned int tgl_max_bw_index(struct drm_i915_private *dev_priv,
696 int num_planes, int qgv_point)
697 {
698 int i;
699
700 /*
701 * Let's return max bw for 0 planes
702 */
703 num_planes = max(1, num_planes);
704
705 for (i = ARRAY_SIZE(dev_priv->display.bw.max) - 1; i >= 0; i--) {
706 const struct intel_bw_info *bi =
707 &dev_priv->display.bw.max[i];
708
709 /*
710 * Pcode will not expose all QGV points when
711 * SAGV is forced to off/min/med/max.
712 */
713 if (qgv_point >= bi->num_qgv_points)
714 return UINT_MAX;
715
716 if (num_planes <= bi->num_planes)
717 return i;
718 }
719
720 return 0;
721 }
722
adl_psf_bw(struct drm_i915_private * dev_priv,int psf_gv_point)723 static unsigned int adl_psf_bw(struct drm_i915_private *dev_priv,
724 int psf_gv_point)
725 {
726 const struct intel_bw_info *bi =
727 &dev_priv->display.bw.max[0];
728
729 return bi->psf_bw[psf_gv_point];
730 }
731
icl_qgv_bw(struct drm_i915_private * i915,int num_active_planes,int qgv_point)732 static unsigned int icl_qgv_bw(struct drm_i915_private *i915,
733 int num_active_planes, int qgv_point)
734 {
735 unsigned int idx;
736
737 if (DISPLAY_VER(i915) >= 12)
738 idx = tgl_max_bw_index(i915, num_active_planes, qgv_point);
739 else
740 idx = icl_max_bw_index(i915, num_active_planes, qgv_point);
741
742 if (idx >= ARRAY_SIZE(i915->display.bw.max))
743 return 0;
744
745 return i915->display.bw.max[idx].deratedbw[qgv_point];
746 }
747
intel_bw_init_hw(struct drm_i915_private * dev_priv)748 void intel_bw_init_hw(struct drm_i915_private *dev_priv)
749 {
750 const struct dram_info *dram_info = &dev_priv->dram_info;
751
752 if (!HAS_DISPLAY(dev_priv))
753 return;
754
755 if (DISPLAY_VERx100(dev_priv) >= 1401 && IS_DGFX(dev_priv) &&
756 dram_info->type == INTEL_DRAM_GDDR_ECC)
757 xe2_hpd_get_bw_info(dev_priv, &xe2_hpd_ecc_sa_info);
758 else if (DISPLAY_VERx100(dev_priv) >= 1401 && IS_DGFX(dev_priv))
759 xe2_hpd_get_bw_info(dev_priv, &xe2_hpd_sa_info);
760 else if (DISPLAY_VER(dev_priv) >= 14)
761 tgl_get_bw_info(dev_priv, &mtl_sa_info);
762 else if (IS_DG2(dev_priv))
763 dg2_get_bw_info(dev_priv);
764 else if (IS_ALDERLAKE_P(dev_priv))
765 tgl_get_bw_info(dev_priv, &adlp_sa_info);
766 else if (IS_ALDERLAKE_S(dev_priv))
767 tgl_get_bw_info(dev_priv, &adls_sa_info);
768 else if (IS_ROCKETLAKE(dev_priv))
769 tgl_get_bw_info(dev_priv, &rkl_sa_info);
770 else if (DISPLAY_VER(dev_priv) == 12)
771 tgl_get_bw_info(dev_priv, &tgl_sa_info);
772 else if (DISPLAY_VER(dev_priv) == 11)
773 icl_get_bw_info(dev_priv, &icl_sa_info);
774 }
775
intel_bw_crtc_num_active_planes(const struct intel_crtc_state * crtc_state)776 static unsigned int intel_bw_crtc_num_active_planes(const struct intel_crtc_state *crtc_state)
777 {
778 /*
779 * We assume cursors are small enough
780 * to not not cause bandwidth problems.
781 */
782 return hweight8(crtc_state->active_planes & ~BIT(PLANE_CURSOR));
783 }
784
intel_bw_crtc_data_rate(const struct intel_crtc_state * crtc_state)785 static unsigned int intel_bw_crtc_data_rate(const struct intel_crtc_state *crtc_state)
786 {
787 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
788 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
789 unsigned int data_rate = 0;
790 enum plane_id plane_id;
791
792 for_each_plane_id_on_crtc(crtc, plane_id) {
793 /*
794 * We assume cursors are small enough
795 * to not not cause bandwidth problems.
796 */
797 if (plane_id == PLANE_CURSOR)
798 continue;
799
800 data_rate += crtc_state->data_rate[plane_id];
801
802 if (DISPLAY_VER(i915) < 11)
803 data_rate += crtc_state->data_rate_y[plane_id];
804 }
805
806 return data_rate;
807 }
808
809 /* "Maximum Pipe Read Bandwidth" */
intel_bw_crtc_min_cdclk(const struct intel_crtc_state * crtc_state)810 static int intel_bw_crtc_min_cdclk(const struct intel_crtc_state *crtc_state)
811 {
812 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
813 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
814
815 if (DISPLAY_VER(i915) < 12)
816 return 0;
817
818 return DIV_ROUND_UP_ULL(mul_u32_u32(intel_bw_crtc_data_rate(crtc_state), 10), 512);
819 }
820
intel_bw_num_active_planes(struct drm_i915_private * dev_priv,const struct intel_bw_state * bw_state)821 static unsigned int intel_bw_num_active_planes(struct drm_i915_private *dev_priv,
822 const struct intel_bw_state *bw_state)
823 {
824 unsigned int num_active_planes = 0;
825 enum pipe pipe;
826
827 for_each_pipe(dev_priv, pipe)
828 num_active_planes += bw_state->num_active_planes[pipe];
829
830 return num_active_planes;
831 }
832
intel_bw_data_rate(struct drm_i915_private * dev_priv,const struct intel_bw_state * bw_state)833 static unsigned int intel_bw_data_rate(struct drm_i915_private *dev_priv,
834 const struct intel_bw_state *bw_state)
835 {
836 unsigned int data_rate = 0;
837 enum pipe pipe;
838
839 for_each_pipe(dev_priv, pipe)
840 data_rate += bw_state->data_rate[pipe];
841
842 if (DISPLAY_VER(dev_priv) >= 13 && i915_vtd_active(dev_priv))
843 data_rate = DIV_ROUND_UP(data_rate * 105, 100);
844
845 return data_rate;
846 }
847
848 struct intel_bw_state *
intel_atomic_get_old_bw_state(struct intel_atomic_state * state)849 intel_atomic_get_old_bw_state(struct intel_atomic_state *state)
850 {
851 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
852 struct intel_global_state *bw_state;
853
854 bw_state = intel_atomic_get_old_global_obj_state(state, &dev_priv->display.bw.obj);
855
856 return to_intel_bw_state(bw_state);
857 }
858
859 struct intel_bw_state *
intel_atomic_get_new_bw_state(struct intel_atomic_state * state)860 intel_atomic_get_new_bw_state(struct intel_atomic_state *state)
861 {
862 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
863 struct intel_global_state *bw_state;
864
865 bw_state = intel_atomic_get_new_global_obj_state(state, &dev_priv->display.bw.obj);
866
867 return to_intel_bw_state(bw_state);
868 }
869
870 struct intel_bw_state *
intel_atomic_get_bw_state(struct intel_atomic_state * state)871 intel_atomic_get_bw_state(struct intel_atomic_state *state)
872 {
873 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
874 struct intel_global_state *bw_state;
875
876 bw_state = intel_atomic_get_global_obj_state(state, &dev_priv->display.bw.obj);
877 if (IS_ERR(bw_state))
878 return ERR_CAST(bw_state);
879
880 return to_intel_bw_state(bw_state);
881 }
882
icl_max_bw_qgv_point_mask(struct drm_i915_private * i915,int num_active_planes)883 static unsigned int icl_max_bw_qgv_point_mask(struct drm_i915_private *i915,
884 int num_active_planes)
885 {
886 unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
887 unsigned int max_bw_point = 0;
888 unsigned int max_bw = 0;
889 int i;
890
891 for (i = 0; i < num_qgv_points; i++) {
892 unsigned int max_data_rate =
893 icl_qgv_bw(i915, num_active_planes, i);
894
895 /*
896 * We need to know which qgv point gives us
897 * maximum bandwidth in order to disable SAGV
898 * if we find that we exceed SAGV block time
899 * with watermarks. By that moment we already
900 * have those, as it is calculated earlier in
901 * intel_atomic_check,
902 */
903 if (max_data_rate > max_bw) {
904 max_bw_point = BIT(i);
905 max_bw = max_data_rate;
906 }
907 }
908
909 return max_bw_point;
910 }
911
icl_prepare_qgv_points_mask(struct drm_i915_private * i915,unsigned int qgv_points,unsigned int psf_points)912 static u16 icl_prepare_qgv_points_mask(struct drm_i915_private *i915,
913 unsigned int qgv_points,
914 unsigned int psf_points)
915 {
916 return ~(ICL_PCODE_REQ_QGV_PT(qgv_points) |
917 ADLS_PCODE_REQ_PSF_PT(psf_points)) & icl_qgv_points_mask(i915);
918 }
919
icl_max_bw_psf_gv_point_mask(struct drm_i915_private * i915)920 static unsigned int icl_max_bw_psf_gv_point_mask(struct drm_i915_private *i915)
921 {
922 unsigned int num_psf_gv_points = i915->display.bw.max[0].num_psf_gv_points;
923 unsigned int max_bw_point_mask = 0;
924 unsigned int max_bw = 0;
925 int i;
926
927 for (i = 0; i < num_psf_gv_points; i++) {
928 unsigned int max_data_rate = adl_psf_bw(i915, i);
929
930 if (max_data_rate > max_bw) {
931 max_bw_point_mask = BIT(i);
932 max_bw = max_data_rate;
933 } else if (max_data_rate == max_bw) {
934 max_bw_point_mask |= BIT(i);
935 }
936 }
937
938 return max_bw_point_mask;
939 }
940
icl_force_disable_sagv(struct drm_i915_private * i915,struct intel_bw_state * bw_state)941 static void icl_force_disable_sagv(struct drm_i915_private *i915,
942 struct intel_bw_state *bw_state)
943 {
944 unsigned int qgv_points = icl_max_bw_qgv_point_mask(i915, 0);
945 unsigned int psf_points = icl_max_bw_psf_gv_point_mask(i915);
946
947 bw_state->qgv_points_mask = icl_prepare_qgv_points_mask(i915,
948 qgv_points,
949 psf_points);
950
951 drm_dbg_kms(&i915->drm, "Forcing SAGV disable: mask 0x%x\n",
952 bw_state->qgv_points_mask);
953
954 icl_pcode_restrict_qgv_points(i915, bw_state->qgv_points_mask);
955 }
956
mtl_find_qgv_points(struct drm_i915_private * i915,unsigned int data_rate,unsigned int num_active_planes,struct intel_bw_state * new_bw_state)957 static int mtl_find_qgv_points(struct drm_i915_private *i915,
958 unsigned int data_rate,
959 unsigned int num_active_planes,
960 struct intel_bw_state *new_bw_state)
961 {
962 unsigned int best_rate = UINT_MAX;
963 unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
964 unsigned int qgv_peak_bw = 0;
965 int i;
966 int ret;
967
968 ret = intel_atomic_lock_global_state(&new_bw_state->base);
969 if (ret)
970 return ret;
971
972 /*
973 * If SAGV cannot be enabled, disable the pcode SAGV by passing all 1's
974 * for qgv peak bw in PM Demand request. So assign UINT_MAX if SAGV is
975 * not enabled. PM Demand code will clamp the value for the register
976 */
977 if (!intel_can_enable_sagv(i915, new_bw_state)) {
978 new_bw_state->qgv_point_peakbw = U16_MAX;
979 drm_dbg_kms(&i915->drm, "No SAGV, use UINT_MAX as peak bw.");
980 return 0;
981 }
982
983 /*
984 * Find the best QGV point by comparing the data_rate with max data rate
985 * offered per plane group
986 */
987 for (i = 0; i < num_qgv_points; i++) {
988 unsigned int bw_index =
989 tgl_max_bw_index(i915, num_active_planes, i);
990 unsigned int max_data_rate;
991
992 if (bw_index >= ARRAY_SIZE(i915->display.bw.max))
993 continue;
994
995 max_data_rate = i915->display.bw.max[bw_index].deratedbw[i];
996
997 if (max_data_rate < data_rate)
998 continue;
999
1000 if (max_data_rate - data_rate < best_rate) {
1001 best_rate = max_data_rate - data_rate;
1002 qgv_peak_bw = i915->display.bw.max[bw_index].peakbw[i];
1003 }
1004
1005 drm_dbg_kms(&i915->drm, "QGV point %d: max bw %d required %d qgv_peak_bw: %d\n",
1006 i, max_data_rate, data_rate, qgv_peak_bw);
1007 }
1008
1009 drm_dbg_kms(&i915->drm, "Matching peaks QGV bw: %d for required data rate: %d\n",
1010 qgv_peak_bw, data_rate);
1011
1012 /*
1013 * The display configuration cannot be supported if no QGV point
1014 * satisfying the required data rate is found
1015 */
1016 if (qgv_peak_bw == 0) {
1017 drm_dbg_kms(&i915->drm, "No QGV points for bw %d for display configuration(%d active planes).\n",
1018 data_rate, num_active_planes);
1019 return -EINVAL;
1020 }
1021
1022 /* MTL PM DEMAND expects QGV BW parameter in multiples of 100 mbps */
1023 new_bw_state->qgv_point_peakbw = DIV_ROUND_CLOSEST(qgv_peak_bw, 100);
1024
1025 return 0;
1026 }
1027
icl_find_qgv_points(struct drm_i915_private * i915,unsigned int data_rate,unsigned int num_active_planes,const struct intel_bw_state * old_bw_state,struct intel_bw_state * new_bw_state)1028 static int icl_find_qgv_points(struct drm_i915_private *i915,
1029 unsigned int data_rate,
1030 unsigned int num_active_planes,
1031 const struct intel_bw_state *old_bw_state,
1032 struct intel_bw_state *new_bw_state)
1033 {
1034 unsigned int num_psf_gv_points = i915->display.bw.max[0].num_psf_gv_points;
1035 unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
1036 u16 psf_points = 0;
1037 u16 qgv_points = 0;
1038 int i;
1039 int ret;
1040
1041 ret = intel_atomic_lock_global_state(&new_bw_state->base);
1042 if (ret)
1043 return ret;
1044
1045 for (i = 0; i < num_qgv_points; i++) {
1046 unsigned int max_data_rate = icl_qgv_bw(i915,
1047 num_active_planes, i);
1048 if (max_data_rate >= data_rate)
1049 qgv_points |= BIT(i);
1050
1051 drm_dbg_kms(&i915->drm, "QGV point %d: max bw %d required %d\n",
1052 i, max_data_rate, data_rate);
1053 }
1054
1055 for (i = 0; i < num_psf_gv_points; i++) {
1056 unsigned int max_data_rate = adl_psf_bw(i915, i);
1057
1058 if (max_data_rate >= data_rate)
1059 psf_points |= BIT(i);
1060
1061 drm_dbg_kms(&i915->drm, "PSF GV point %d: max bw %d"
1062 " required %d\n",
1063 i, max_data_rate, data_rate);
1064 }
1065
1066 /*
1067 * BSpec states that we always should have at least one allowed point
1068 * left, so if we couldn't - simply reject the configuration for obvious
1069 * reasons.
1070 */
1071 if (qgv_points == 0) {
1072 drm_dbg_kms(&i915->drm, "No QGV points provide sufficient memory"
1073 " bandwidth %d for display configuration(%d active planes).\n",
1074 data_rate, num_active_planes);
1075 return -EINVAL;
1076 }
1077
1078 if (num_psf_gv_points > 0 && psf_points == 0) {
1079 drm_dbg_kms(&i915->drm, "No PSF GV points provide sufficient memory"
1080 " bandwidth %d for display configuration(%d active planes).\n",
1081 data_rate, num_active_planes);
1082 return -EINVAL;
1083 }
1084
1085 /*
1086 * Leave only single point with highest bandwidth, if
1087 * we can't enable SAGV due to the increased memory latency it may
1088 * cause.
1089 */
1090 if (!intel_can_enable_sagv(i915, new_bw_state)) {
1091 qgv_points = icl_max_bw_qgv_point_mask(i915, num_active_planes);
1092 drm_dbg_kms(&i915->drm, "No SAGV, using single QGV point mask 0x%x\n",
1093 qgv_points);
1094 }
1095
1096 /*
1097 * We store the ones which need to be masked as that is what PCode
1098 * actually accepts as a parameter.
1099 */
1100 new_bw_state->qgv_points_mask = icl_prepare_qgv_points_mask(i915,
1101 qgv_points,
1102 psf_points);
1103 /*
1104 * If the actual mask had changed we need to make sure that
1105 * the commits are serialized(in case this is a nomodeset, nonblocking)
1106 */
1107 if (new_bw_state->qgv_points_mask != old_bw_state->qgv_points_mask) {
1108 ret = intel_atomic_serialize_global_state(&new_bw_state->base);
1109 if (ret)
1110 return ret;
1111 }
1112
1113 return 0;
1114 }
1115
intel_bw_check_qgv_points(struct drm_i915_private * i915,const struct intel_bw_state * old_bw_state,struct intel_bw_state * new_bw_state)1116 static int intel_bw_check_qgv_points(struct drm_i915_private *i915,
1117 const struct intel_bw_state *old_bw_state,
1118 struct intel_bw_state *new_bw_state)
1119 {
1120 unsigned int data_rate = intel_bw_data_rate(i915, new_bw_state);
1121 unsigned int num_active_planes =
1122 intel_bw_num_active_planes(i915, new_bw_state);
1123
1124 data_rate = DIV_ROUND_UP(data_rate, 1000);
1125
1126 if (DISPLAY_VER(i915) >= 14)
1127 return mtl_find_qgv_points(i915, data_rate, num_active_planes,
1128 new_bw_state);
1129 else
1130 return icl_find_qgv_points(i915, data_rate, num_active_planes,
1131 old_bw_state, new_bw_state);
1132 }
1133
intel_bw_state_changed(struct drm_i915_private * i915,const struct intel_bw_state * old_bw_state,const struct intel_bw_state * new_bw_state)1134 static bool intel_bw_state_changed(struct drm_i915_private *i915,
1135 const struct intel_bw_state *old_bw_state,
1136 const struct intel_bw_state *new_bw_state)
1137 {
1138 enum pipe pipe;
1139
1140 for_each_pipe(i915, pipe) {
1141 const struct intel_dbuf_bw *old_crtc_bw =
1142 &old_bw_state->dbuf_bw[pipe];
1143 const struct intel_dbuf_bw *new_crtc_bw =
1144 &new_bw_state->dbuf_bw[pipe];
1145 enum dbuf_slice slice;
1146
1147 for_each_dbuf_slice(i915, slice) {
1148 if (old_crtc_bw->max_bw[slice] != new_crtc_bw->max_bw[slice] ||
1149 old_crtc_bw->active_planes[slice] != new_crtc_bw->active_planes[slice])
1150 return true;
1151 }
1152
1153 if (old_bw_state->min_cdclk[pipe] != new_bw_state->min_cdclk[pipe])
1154 return true;
1155 }
1156
1157 return false;
1158 }
1159
skl_plane_calc_dbuf_bw(struct intel_bw_state * bw_state,struct intel_crtc * crtc,enum plane_id plane_id,const struct skl_ddb_entry * ddb,unsigned int data_rate)1160 static void skl_plane_calc_dbuf_bw(struct intel_bw_state *bw_state,
1161 struct intel_crtc *crtc,
1162 enum plane_id plane_id,
1163 const struct skl_ddb_entry *ddb,
1164 unsigned int data_rate)
1165 {
1166 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1167 struct intel_dbuf_bw *crtc_bw = &bw_state->dbuf_bw[crtc->pipe];
1168 unsigned int dbuf_mask = skl_ddb_dbuf_slice_mask(i915, ddb);
1169 enum dbuf_slice slice;
1170
1171 /*
1172 * The arbiter can only really guarantee an
1173 * equal share of the total bw to each plane.
1174 */
1175 for_each_dbuf_slice_in_mask(i915, slice, dbuf_mask) {
1176 crtc_bw->max_bw[slice] = max(crtc_bw->max_bw[slice], data_rate);
1177 crtc_bw->active_planes[slice] |= BIT(plane_id);
1178 }
1179 }
1180
skl_crtc_calc_dbuf_bw(struct intel_bw_state * bw_state,const struct intel_crtc_state * crtc_state)1181 static void skl_crtc_calc_dbuf_bw(struct intel_bw_state *bw_state,
1182 const struct intel_crtc_state *crtc_state)
1183 {
1184 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1185 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1186 struct intel_dbuf_bw *crtc_bw = &bw_state->dbuf_bw[crtc->pipe];
1187 enum plane_id plane_id;
1188
1189 memset(crtc_bw, 0, sizeof(*crtc_bw));
1190
1191 if (!crtc_state->hw.active)
1192 return;
1193
1194 for_each_plane_id_on_crtc(crtc, plane_id) {
1195 /*
1196 * We assume cursors are small enough
1197 * to not cause bandwidth problems.
1198 */
1199 if (plane_id == PLANE_CURSOR)
1200 continue;
1201
1202 skl_plane_calc_dbuf_bw(bw_state, crtc, plane_id,
1203 &crtc_state->wm.skl.plane_ddb[plane_id],
1204 crtc_state->data_rate[plane_id]);
1205
1206 if (DISPLAY_VER(i915) < 11)
1207 skl_plane_calc_dbuf_bw(bw_state, crtc, plane_id,
1208 &crtc_state->wm.skl.plane_ddb_y[plane_id],
1209 crtc_state->data_rate[plane_id]);
1210 }
1211 }
1212
1213 /* "Maximum Data Buffer Bandwidth" */
1214 static int
intel_bw_dbuf_min_cdclk(struct drm_i915_private * i915,const struct intel_bw_state * bw_state)1215 intel_bw_dbuf_min_cdclk(struct drm_i915_private *i915,
1216 const struct intel_bw_state *bw_state)
1217 {
1218 unsigned int total_max_bw = 0;
1219 enum dbuf_slice slice;
1220
1221 for_each_dbuf_slice(i915, slice) {
1222 int num_active_planes = 0;
1223 unsigned int max_bw = 0;
1224 enum pipe pipe;
1225
1226 /*
1227 * The arbiter can only really guarantee an
1228 * equal share of the total bw to each plane.
1229 */
1230 for_each_pipe(i915, pipe) {
1231 const struct intel_dbuf_bw *crtc_bw = &bw_state->dbuf_bw[pipe];
1232
1233 max_bw = max(crtc_bw->max_bw[slice], max_bw);
1234 num_active_planes += hweight8(crtc_bw->active_planes[slice]);
1235 }
1236 max_bw *= num_active_planes;
1237
1238 total_max_bw = max(total_max_bw, max_bw);
1239 }
1240
1241 return DIV_ROUND_UP(total_max_bw, 64);
1242 }
1243
intel_bw_min_cdclk(struct drm_i915_private * i915,const struct intel_bw_state * bw_state)1244 int intel_bw_min_cdclk(struct drm_i915_private *i915,
1245 const struct intel_bw_state *bw_state)
1246 {
1247 enum pipe pipe;
1248 int min_cdclk;
1249
1250 min_cdclk = intel_bw_dbuf_min_cdclk(i915, bw_state);
1251
1252 for_each_pipe(i915, pipe)
1253 min_cdclk = max(min_cdclk, bw_state->min_cdclk[pipe]);
1254
1255 return min_cdclk;
1256 }
1257
intel_bw_calc_min_cdclk(struct intel_atomic_state * state,bool * need_cdclk_calc)1258 int intel_bw_calc_min_cdclk(struct intel_atomic_state *state,
1259 bool *need_cdclk_calc)
1260 {
1261 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1262 struct intel_bw_state *new_bw_state = NULL;
1263 const struct intel_bw_state *old_bw_state = NULL;
1264 const struct intel_cdclk_state *cdclk_state;
1265 const struct intel_crtc_state *crtc_state;
1266 int old_min_cdclk, new_min_cdclk;
1267 struct intel_crtc *crtc;
1268 int i;
1269
1270 if (DISPLAY_VER(dev_priv) < 9)
1271 return 0;
1272
1273 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
1274 new_bw_state = intel_atomic_get_bw_state(state);
1275 if (IS_ERR(new_bw_state))
1276 return PTR_ERR(new_bw_state);
1277
1278 old_bw_state = intel_atomic_get_old_bw_state(state);
1279
1280 skl_crtc_calc_dbuf_bw(new_bw_state, crtc_state);
1281
1282 new_bw_state->min_cdclk[crtc->pipe] =
1283 intel_bw_crtc_min_cdclk(crtc_state);
1284 }
1285
1286 if (!old_bw_state)
1287 return 0;
1288
1289 if (intel_bw_state_changed(dev_priv, old_bw_state, new_bw_state)) {
1290 int ret = intel_atomic_lock_global_state(&new_bw_state->base);
1291 if (ret)
1292 return ret;
1293 }
1294
1295 old_min_cdclk = intel_bw_min_cdclk(dev_priv, old_bw_state);
1296 new_min_cdclk = intel_bw_min_cdclk(dev_priv, new_bw_state);
1297
1298 /*
1299 * No need to check against the cdclk state if
1300 * the min cdclk doesn't increase.
1301 *
1302 * Ie. we only ever increase the cdclk due to bandwidth
1303 * requirements. This can reduce back and forth
1304 * display blinking due to constant cdclk changes.
1305 */
1306 if (new_min_cdclk <= old_min_cdclk)
1307 return 0;
1308
1309 cdclk_state = intel_atomic_get_cdclk_state(state);
1310 if (IS_ERR(cdclk_state))
1311 return PTR_ERR(cdclk_state);
1312
1313 /*
1314 * No need to recalculate the cdclk state if
1315 * the min cdclk doesn't increase.
1316 *
1317 * Ie. we only ever increase the cdclk due to bandwidth
1318 * requirements. This can reduce back and forth
1319 * display blinking due to constant cdclk changes.
1320 */
1321 if (new_min_cdclk <= cdclk_state->bw_min_cdclk)
1322 return 0;
1323
1324 drm_dbg_kms(&dev_priv->drm,
1325 "new bandwidth min cdclk (%d kHz) > old min cdclk (%d kHz)\n",
1326 new_min_cdclk, cdclk_state->bw_min_cdclk);
1327 *need_cdclk_calc = true;
1328
1329 return 0;
1330 }
1331
intel_bw_check_data_rate(struct intel_atomic_state * state,bool * changed)1332 static int intel_bw_check_data_rate(struct intel_atomic_state *state, bool *changed)
1333 {
1334 struct drm_i915_private *i915 = to_i915(state->base.dev);
1335 const struct intel_crtc_state *new_crtc_state, *old_crtc_state;
1336 struct intel_crtc *crtc;
1337 int i;
1338
1339 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
1340 new_crtc_state, i) {
1341 unsigned int old_data_rate =
1342 intel_bw_crtc_data_rate(old_crtc_state);
1343 unsigned int new_data_rate =
1344 intel_bw_crtc_data_rate(new_crtc_state);
1345 unsigned int old_active_planes =
1346 intel_bw_crtc_num_active_planes(old_crtc_state);
1347 unsigned int new_active_planes =
1348 intel_bw_crtc_num_active_planes(new_crtc_state);
1349 struct intel_bw_state *new_bw_state;
1350
1351 /*
1352 * Avoid locking the bw state when
1353 * nothing significant has changed.
1354 */
1355 if (old_data_rate == new_data_rate &&
1356 old_active_planes == new_active_planes)
1357 continue;
1358
1359 new_bw_state = intel_atomic_get_bw_state(state);
1360 if (IS_ERR(new_bw_state))
1361 return PTR_ERR(new_bw_state);
1362
1363 new_bw_state->data_rate[crtc->pipe] = new_data_rate;
1364 new_bw_state->num_active_planes[crtc->pipe] = new_active_planes;
1365
1366 *changed = true;
1367
1368 drm_dbg_kms(&i915->drm,
1369 "[CRTC:%d:%s] data rate %u num active planes %u\n",
1370 crtc->base.base.id, crtc->base.name,
1371 new_bw_state->data_rate[crtc->pipe],
1372 new_bw_state->num_active_planes[crtc->pipe]);
1373 }
1374
1375 return 0;
1376 }
1377
intel_bw_atomic_check(struct intel_atomic_state * state)1378 int intel_bw_atomic_check(struct intel_atomic_state *state)
1379 {
1380 bool changed = false;
1381 struct drm_i915_private *i915 = to_i915(state->base.dev);
1382 struct intel_bw_state *new_bw_state;
1383 const struct intel_bw_state *old_bw_state;
1384 int ret;
1385
1386 /* FIXME earlier gens need some checks too */
1387 if (DISPLAY_VER(i915) < 11)
1388 return 0;
1389
1390 ret = intel_bw_check_data_rate(state, &changed);
1391 if (ret)
1392 return ret;
1393
1394 old_bw_state = intel_atomic_get_old_bw_state(state);
1395 new_bw_state = intel_atomic_get_new_bw_state(state);
1396
1397 if (new_bw_state &&
1398 (intel_can_enable_sagv(i915, old_bw_state) !=
1399 intel_can_enable_sagv(i915, new_bw_state) ||
1400 new_bw_state->force_check_qgv))
1401 changed = true;
1402
1403 /*
1404 * If none of our inputs (data rates, number of active
1405 * planes, SAGV yes/no) changed then nothing to do here.
1406 */
1407 if (!changed)
1408 return 0;
1409
1410 ret = intel_bw_check_qgv_points(i915, old_bw_state, new_bw_state);
1411 if (ret)
1412 return ret;
1413
1414 new_bw_state->force_check_qgv = false;
1415
1416 return 0;
1417 }
1418
intel_bw_crtc_update(struct intel_bw_state * bw_state,const struct intel_crtc_state * crtc_state)1419 static void intel_bw_crtc_update(struct intel_bw_state *bw_state,
1420 const struct intel_crtc_state *crtc_state)
1421 {
1422 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1423 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1424
1425 bw_state->data_rate[crtc->pipe] =
1426 intel_bw_crtc_data_rate(crtc_state);
1427 bw_state->num_active_planes[crtc->pipe] =
1428 intel_bw_crtc_num_active_planes(crtc_state);
1429 bw_state->force_check_qgv = true;
1430
1431 drm_dbg_kms(&i915->drm, "pipe %c data rate %u num active planes %u\n",
1432 pipe_name(crtc->pipe),
1433 bw_state->data_rate[crtc->pipe],
1434 bw_state->num_active_planes[crtc->pipe]);
1435 }
1436
intel_bw_update_hw_state(struct intel_display * display)1437 void intel_bw_update_hw_state(struct intel_display *display)
1438 {
1439 struct intel_bw_state *bw_state =
1440 to_intel_bw_state(display->bw.obj.state);
1441 struct intel_crtc *crtc;
1442
1443 if (DISPLAY_VER(display) < 9)
1444 return;
1445
1446 bw_state->active_pipes = 0;
1447
1448 for_each_intel_crtc(display->drm, crtc) {
1449 const struct intel_crtc_state *crtc_state =
1450 to_intel_crtc_state(crtc->base.state);
1451 enum pipe pipe = crtc->pipe;
1452
1453 if (crtc_state->hw.active)
1454 bw_state->active_pipes |= BIT(pipe);
1455
1456 if (DISPLAY_VER(display) >= 11)
1457 intel_bw_crtc_update(bw_state, crtc_state);
1458 }
1459 }
1460
intel_bw_crtc_disable_noatomic(struct intel_crtc * crtc)1461 void intel_bw_crtc_disable_noatomic(struct intel_crtc *crtc)
1462 {
1463 struct intel_display *display = to_intel_display(crtc);
1464 struct intel_bw_state *bw_state =
1465 to_intel_bw_state(display->bw.obj.state);
1466 enum pipe pipe = crtc->pipe;
1467
1468 if (DISPLAY_VER(display) < 9)
1469 return;
1470
1471 bw_state->data_rate[pipe] = 0;
1472 bw_state->num_active_planes[pipe] = 0;
1473 }
1474
1475 static struct intel_global_state *
intel_bw_duplicate_state(struct intel_global_obj * obj)1476 intel_bw_duplicate_state(struct intel_global_obj *obj)
1477 {
1478 struct intel_bw_state *state;
1479
1480 state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
1481 if (!state)
1482 return NULL;
1483
1484 return &state->base;
1485 }
1486
intel_bw_destroy_state(struct intel_global_obj * obj,struct intel_global_state * state)1487 static void intel_bw_destroy_state(struct intel_global_obj *obj,
1488 struct intel_global_state *state)
1489 {
1490 kfree(state);
1491 }
1492
1493 static const struct intel_global_state_funcs intel_bw_funcs = {
1494 .atomic_duplicate_state = intel_bw_duplicate_state,
1495 .atomic_destroy_state = intel_bw_destroy_state,
1496 };
1497
intel_bw_init(struct drm_i915_private * i915)1498 int intel_bw_init(struct drm_i915_private *i915)
1499 {
1500 struct intel_display *display = &i915->display;
1501 struct intel_bw_state *state;
1502
1503 state = kzalloc(sizeof(*state), GFP_KERNEL);
1504 if (!state)
1505 return -ENOMEM;
1506
1507 intel_atomic_global_obj_init(display, &display->bw.obj,
1508 &state->base, &intel_bw_funcs);
1509
1510 /*
1511 * Limit this only if we have SAGV. And for Display version 14 onwards
1512 * sagv is handled though pmdemand requests
1513 */
1514 if (intel_has_sagv(i915) && IS_DISPLAY_VER(i915, 11, 13))
1515 icl_force_disable_sagv(i915, state);
1516
1517 return 0;
1518 }
1519