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_crtc_update(struct intel_bw_state * bw_state,const struct intel_crtc_state * crtc_state)821 void intel_bw_crtc_update(struct intel_bw_state *bw_state,
822 			  const struct intel_crtc_state *crtc_state)
823 {
824 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
825 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
826 
827 	bw_state->data_rate[crtc->pipe] =
828 		intel_bw_crtc_data_rate(crtc_state);
829 	bw_state->num_active_planes[crtc->pipe] =
830 		intel_bw_crtc_num_active_planes(crtc_state);
831 	bw_state->force_check_qgv = true;
832 
833 	drm_dbg_kms(&i915->drm, "pipe %c data rate %u num active planes %u\n",
834 		    pipe_name(crtc->pipe),
835 		    bw_state->data_rate[crtc->pipe],
836 		    bw_state->num_active_planes[crtc->pipe]);
837 }
838 
intel_bw_num_active_planes(struct drm_i915_private * dev_priv,const struct intel_bw_state * bw_state)839 static unsigned int intel_bw_num_active_planes(struct drm_i915_private *dev_priv,
840 					       const struct intel_bw_state *bw_state)
841 {
842 	unsigned int num_active_planes = 0;
843 	enum pipe pipe;
844 
845 	for_each_pipe(dev_priv, pipe)
846 		num_active_planes += bw_state->num_active_planes[pipe];
847 
848 	return num_active_planes;
849 }
850 
intel_bw_data_rate(struct drm_i915_private * dev_priv,const struct intel_bw_state * bw_state)851 static unsigned int intel_bw_data_rate(struct drm_i915_private *dev_priv,
852 				       const struct intel_bw_state *bw_state)
853 {
854 	unsigned int data_rate = 0;
855 	enum pipe pipe;
856 
857 	for_each_pipe(dev_priv, pipe)
858 		data_rate += bw_state->data_rate[pipe];
859 
860 	if (DISPLAY_VER(dev_priv) >= 13 && i915_vtd_active(dev_priv))
861 		data_rate = DIV_ROUND_UP(data_rate * 105, 100);
862 
863 	return data_rate;
864 }
865 
866 struct intel_bw_state *
intel_atomic_get_old_bw_state(struct intel_atomic_state * state)867 intel_atomic_get_old_bw_state(struct intel_atomic_state *state)
868 {
869 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
870 	struct intel_global_state *bw_state;
871 
872 	bw_state = intel_atomic_get_old_global_obj_state(state, &dev_priv->display.bw.obj);
873 
874 	return to_intel_bw_state(bw_state);
875 }
876 
877 struct intel_bw_state *
intel_atomic_get_new_bw_state(struct intel_atomic_state * state)878 intel_atomic_get_new_bw_state(struct intel_atomic_state *state)
879 {
880 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
881 	struct intel_global_state *bw_state;
882 
883 	bw_state = intel_atomic_get_new_global_obj_state(state, &dev_priv->display.bw.obj);
884 
885 	return to_intel_bw_state(bw_state);
886 }
887 
888 struct intel_bw_state *
intel_atomic_get_bw_state(struct intel_atomic_state * state)889 intel_atomic_get_bw_state(struct intel_atomic_state *state)
890 {
891 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
892 	struct intel_global_state *bw_state;
893 
894 	bw_state = intel_atomic_get_global_obj_state(state, &dev_priv->display.bw.obj);
895 	if (IS_ERR(bw_state))
896 		return ERR_CAST(bw_state);
897 
898 	return to_intel_bw_state(bw_state);
899 }
900 
icl_max_bw_qgv_point_mask(struct drm_i915_private * i915,int num_active_planes)901 static unsigned int icl_max_bw_qgv_point_mask(struct drm_i915_private *i915,
902 					      int num_active_planes)
903 {
904 	unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
905 	unsigned int max_bw_point = 0;
906 	unsigned int max_bw = 0;
907 	int i;
908 
909 	for (i = 0; i < num_qgv_points; i++) {
910 		unsigned int max_data_rate =
911 			icl_qgv_bw(i915, num_active_planes, i);
912 
913 		/*
914 		 * We need to know which qgv point gives us
915 		 * maximum bandwidth in order to disable SAGV
916 		 * if we find that we exceed SAGV block time
917 		 * with watermarks. By that moment we already
918 		 * have those, as it is calculated earlier in
919 		 * intel_atomic_check,
920 		 */
921 		if (max_data_rate > max_bw) {
922 			max_bw_point = BIT(i);
923 			max_bw = max_data_rate;
924 		}
925 	}
926 
927 	return max_bw_point;
928 }
929 
icl_prepare_qgv_points_mask(struct drm_i915_private * i915,unsigned int qgv_points,unsigned int psf_points)930 static u16 icl_prepare_qgv_points_mask(struct drm_i915_private *i915,
931 				       unsigned int qgv_points,
932 				       unsigned int psf_points)
933 {
934 	return ~(ICL_PCODE_REQ_QGV_PT(qgv_points) |
935 		 ADLS_PCODE_REQ_PSF_PT(psf_points)) & icl_qgv_points_mask(i915);
936 }
937 
icl_max_bw_psf_gv_point_mask(struct drm_i915_private * i915)938 static unsigned int icl_max_bw_psf_gv_point_mask(struct drm_i915_private *i915)
939 {
940 	unsigned int num_psf_gv_points = i915->display.bw.max[0].num_psf_gv_points;
941 	unsigned int max_bw_point_mask = 0;
942 	unsigned int max_bw = 0;
943 	int i;
944 
945 	for (i = 0; i < num_psf_gv_points; i++) {
946 		unsigned int max_data_rate = adl_psf_bw(i915, i);
947 
948 		if (max_data_rate > max_bw) {
949 			max_bw_point_mask = BIT(i);
950 			max_bw = max_data_rate;
951 		} else if (max_data_rate == max_bw) {
952 			max_bw_point_mask |= BIT(i);
953 		}
954 	}
955 
956 	return max_bw_point_mask;
957 }
958 
icl_force_disable_sagv(struct drm_i915_private * i915,struct intel_bw_state * bw_state)959 static void icl_force_disable_sagv(struct drm_i915_private *i915,
960 				   struct intel_bw_state *bw_state)
961 {
962 	unsigned int qgv_points = icl_max_bw_qgv_point_mask(i915, 0);
963 	unsigned int psf_points = icl_max_bw_psf_gv_point_mask(i915);
964 
965 	bw_state->qgv_points_mask = icl_prepare_qgv_points_mask(i915,
966 								qgv_points,
967 								psf_points);
968 
969 	drm_dbg_kms(&i915->drm, "Forcing SAGV disable: mask 0x%x\n",
970 		    bw_state->qgv_points_mask);
971 
972 	icl_pcode_restrict_qgv_points(i915, bw_state->qgv_points_mask);
973 }
974 
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)975 static int mtl_find_qgv_points(struct drm_i915_private *i915,
976 			       unsigned int data_rate,
977 			       unsigned int num_active_planes,
978 			       struct intel_bw_state *new_bw_state)
979 {
980 	unsigned int best_rate = UINT_MAX;
981 	unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
982 	unsigned int qgv_peak_bw  = 0;
983 	int i;
984 	int ret;
985 
986 	ret = intel_atomic_lock_global_state(&new_bw_state->base);
987 	if (ret)
988 		return ret;
989 
990 	/*
991 	 * If SAGV cannot be enabled, disable the pcode SAGV by passing all 1's
992 	 * for qgv peak bw in PM Demand request. So assign UINT_MAX if SAGV is
993 	 * not enabled. PM Demand code will clamp the value for the register
994 	 */
995 	if (!intel_can_enable_sagv(i915, new_bw_state)) {
996 		new_bw_state->qgv_point_peakbw = U16_MAX;
997 		drm_dbg_kms(&i915->drm, "No SAGV, use UINT_MAX as peak bw.");
998 		return 0;
999 	}
1000 
1001 	/*
1002 	 * Find the best QGV point by comparing the data_rate with max data rate
1003 	 * offered per plane group
1004 	 */
1005 	for (i = 0; i < num_qgv_points; i++) {
1006 		unsigned int bw_index =
1007 			tgl_max_bw_index(i915, num_active_planes, i);
1008 		unsigned int max_data_rate;
1009 
1010 		if (bw_index >= ARRAY_SIZE(i915->display.bw.max))
1011 			continue;
1012 
1013 		max_data_rate = i915->display.bw.max[bw_index].deratedbw[i];
1014 
1015 		if (max_data_rate < data_rate)
1016 			continue;
1017 
1018 		if (max_data_rate - data_rate < best_rate) {
1019 			best_rate = max_data_rate - data_rate;
1020 			qgv_peak_bw = i915->display.bw.max[bw_index].peakbw[i];
1021 		}
1022 
1023 		drm_dbg_kms(&i915->drm, "QGV point %d: max bw %d required %d qgv_peak_bw: %d\n",
1024 			    i, max_data_rate, data_rate, qgv_peak_bw);
1025 	}
1026 
1027 	drm_dbg_kms(&i915->drm, "Matching peaks QGV bw: %d for required data rate: %d\n",
1028 		    qgv_peak_bw, data_rate);
1029 
1030 	/*
1031 	 * The display configuration cannot be supported if no QGV point
1032 	 * satisfying the required data rate is found
1033 	 */
1034 	if (qgv_peak_bw == 0) {
1035 		drm_dbg_kms(&i915->drm, "No QGV points for bw %d for display configuration(%d active planes).\n",
1036 			    data_rate, num_active_planes);
1037 		return -EINVAL;
1038 	}
1039 
1040 	/* MTL PM DEMAND expects QGV BW parameter in multiples of 100 mbps */
1041 	new_bw_state->qgv_point_peakbw = DIV_ROUND_CLOSEST(qgv_peak_bw, 100);
1042 
1043 	return 0;
1044 }
1045 
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)1046 static int icl_find_qgv_points(struct drm_i915_private *i915,
1047 			       unsigned int data_rate,
1048 			       unsigned int num_active_planes,
1049 			       const struct intel_bw_state *old_bw_state,
1050 			       struct intel_bw_state *new_bw_state)
1051 {
1052 	unsigned int num_psf_gv_points = i915->display.bw.max[0].num_psf_gv_points;
1053 	unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
1054 	u16 psf_points = 0;
1055 	u16 qgv_points = 0;
1056 	int i;
1057 	int ret;
1058 
1059 	ret = intel_atomic_lock_global_state(&new_bw_state->base);
1060 	if (ret)
1061 		return ret;
1062 
1063 	for (i = 0; i < num_qgv_points; i++) {
1064 		unsigned int max_data_rate = icl_qgv_bw(i915,
1065 							num_active_planes, i);
1066 		if (max_data_rate >= data_rate)
1067 			qgv_points |= BIT(i);
1068 
1069 		drm_dbg_kms(&i915->drm, "QGV point %d: max bw %d required %d\n",
1070 			    i, max_data_rate, data_rate);
1071 	}
1072 
1073 	for (i = 0; i < num_psf_gv_points; i++) {
1074 		unsigned int max_data_rate = adl_psf_bw(i915, i);
1075 
1076 		if (max_data_rate >= data_rate)
1077 			psf_points |= BIT(i);
1078 
1079 		drm_dbg_kms(&i915->drm, "PSF GV point %d: max bw %d"
1080 			    " required %d\n",
1081 			    i, max_data_rate, data_rate);
1082 	}
1083 
1084 	/*
1085 	 * BSpec states that we always should have at least one allowed point
1086 	 * left, so if we couldn't - simply reject the configuration for obvious
1087 	 * reasons.
1088 	 */
1089 	if (qgv_points == 0) {
1090 		drm_dbg_kms(&i915->drm, "No QGV points provide sufficient memory"
1091 			    " bandwidth %d for display configuration(%d active planes).\n",
1092 			    data_rate, num_active_planes);
1093 		return -EINVAL;
1094 	}
1095 
1096 	if (num_psf_gv_points > 0 && psf_points == 0) {
1097 		drm_dbg_kms(&i915->drm, "No PSF GV points provide sufficient memory"
1098 			    " bandwidth %d for display configuration(%d active planes).\n",
1099 			    data_rate, num_active_planes);
1100 		return -EINVAL;
1101 	}
1102 
1103 	/*
1104 	 * Leave only single point with highest bandwidth, if
1105 	 * we can't enable SAGV due to the increased memory latency it may
1106 	 * cause.
1107 	 */
1108 	if (!intel_can_enable_sagv(i915, new_bw_state)) {
1109 		qgv_points = icl_max_bw_qgv_point_mask(i915, num_active_planes);
1110 		drm_dbg_kms(&i915->drm, "No SAGV, using single QGV point mask 0x%x\n",
1111 			    qgv_points);
1112 	}
1113 
1114 	/*
1115 	 * We store the ones which need to be masked as that is what PCode
1116 	 * actually accepts as a parameter.
1117 	 */
1118 	new_bw_state->qgv_points_mask = icl_prepare_qgv_points_mask(i915,
1119 								    qgv_points,
1120 								    psf_points);
1121 	/*
1122 	 * If the actual mask had changed we need to make sure that
1123 	 * the commits are serialized(in case this is a nomodeset, nonblocking)
1124 	 */
1125 	if (new_bw_state->qgv_points_mask != old_bw_state->qgv_points_mask) {
1126 		ret = intel_atomic_serialize_global_state(&new_bw_state->base);
1127 		if (ret)
1128 			return ret;
1129 	}
1130 
1131 	return 0;
1132 }
1133 
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)1134 static int intel_bw_check_qgv_points(struct drm_i915_private *i915,
1135 				     const struct intel_bw_state *old_bw_state,
1136 				     struct intel_bw_state *new_bw_state)
1137 {
1138 	unsigned int data_rate = intel_bw_data_rate(i915, new_bw_state);
1139 	unsigned int num_active_planes =
1140 			intel_bw_num_active_planes(i915, new_bw_state);
1141 
1142 	data_rate = DIV_ROUND_UP(data_rate, 1000);
1143 
1144 	if (DISPLAY_VER(i915) >= 14)
1145 		return mtl_find_qgv_points(i915, data_rate, num_active_planes,
1146 					   new_bw_state);
1147 	else
1148 		return icl_find_qgv_points(i915, data_rate, num_active_planes,
1149 					   old_bw_state, new_bw_state);
1150 }
1151 
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)1152 static bool intel_bw_state_changed(struct drm_i915_private *i915,
1153 				   const struct intel_bw_state *old_bw_state,
1154 				   const struct intel_bw_state *new_bw_state)
1155 {
1156 	enum pipe pipe;
1157 
1158 	for_each_pipe(i915, pipe) {
1159 		const struct intel_dbuf_bw *old_crtc_bw =
1160 			&old_bw_state->dbuf_bw[pipe];
1161 		const struct intel_dbuf_bw *new_crtc_bw =
1162 			&new_bw_state->dbuf_bw[pipe];
1163 		enum dbuf_slice slice;
1164 
1165 		for_each_dbuf_slice(i915, slice) {
1166 			if (old_crtc_bw->max_bw[slice] != new_crtc_bw->max_bw[slice] ||
1167 			    old_crtc_bw->active_planes[slice] != new_crtc_bw->active_planes[slice])
1168 				return true;
1169 		}
1170 
1171 		if (old_bw_state->min_cdclk[pipe] != new_bw_state->min_cdclk[pipe])
1172 			return true;
1173 	}
1174 
1175 	return false;
1176 }
1177 
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)1178 static void skl_plane_calc_dbuf_bw(struct intel_bw_state *bw_state,
1179 				   struct intel_crtc *crtc,
1180 				   enum plane_id plane_id,
1181 				   const struct skl_ddb_entry *ddb,
1182 				   unsigned int data_rate)
1183 {
1184 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1185 	struct intel_dbuf_bw *crtc_bw = &bw_state->dbuf_bw[crtc->pipe];
1186 	unsigned int dbuf_mask = skl_ddb_dbuf_slice_mask(i915, ddb);
1187 	enum dbuf_slice slice;
1188 
1189 	/*
1190 	 * The arbiter can only really guarantee an
1191 	 * equal share of the total bw to each plane.
1192 	 */
1193 	for_each_dbuf_slice_in_mask(i915, slice, dbuf_mask) {
1194 		crtc_bw->max_bw[slice] = max(crtc_bw->max_bw[slice], data_rate);
1195 		crtc_bw->active_planes[slice] |= BIT(plane_id);
1196 	}
1197 }
1198 
skl_crtc_calc_dbuf_bw(struct intel_bw_state * bw_state,const struct intel_crtc_state * crtc_state)1199 static void skl_crtc_calc_dbuf_bw(struct intel_bw_state *bw_state,
1200 				  const struct intel_crtc_state *crtc_state)
1201 {
1202 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1203 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1204 	struct intel_dbuf_bw *crtc_bw = &bw_state->dbuf_bw[crtc->pipe];
1205 	enum plane_id plane_id;
1206 
1207 	memset(crtc_bw, 0, sizeof(*crtc_bw));
1208 
1209 	if (!crtc_state->hw.active)
1210 		return;
1211 
1212 	for_each_plane_id_on_crtc(crtc, plane_id) {
1213 		/*
1214 		 * We assume cursors are small enough
1215 		 * to not cause bandwidth problems.
1216 		 */
1217 		if (plane_id == PLANE_CURSOR)
1218 			continue;
1219 
1220 		skl_plane_calc_dbuf_bw(bw_state, crtc, plane_id,
1221 				       &crtc_state->wm.skl.plane_ddb[plane_id],
1222 				       crtc_state->data_rate[plane_id]);
1223 
1224 		if (DISPLAY_VER(i915) < 11)
1225 			skl_plane_calc_dbuf_bw(bw_state, crtc, plane_id,
1226 					       &crtc_state->wm.skl.plane_ddb_y[plane_id],
1227 					       crtc_state->data_rate[plane_id]);
1228 	}
1229 }
1230 
1231 /* "Maximum Data Buffer Bandwidth" */
1232 static int
intel_bw_dbuf_min_cdclk(struct drm_i915_private * i915,const struct intel_bw_state * bw_state)1233 intel_bw_dbuf_min_cdclk(struct drm_i915_private *i915,
1234 			const struct intel_bw_state *bw_state)
1235 {
1236 	unsigned int total_max_bw = 0;
1237 	enum dbuf_slice slice;
1238 
1239 	for_each_dbuf_slice(i915, slice) {
1240 		int num_active_planes = 0;
1241 		unsigned int max_bw = 0;
1242 		enum pipe pipe;
1243 
1244 		/*
1245 		 * The arbiter can only really guarantee an
1246 		 * equal share of the total bw to each plane.
1247 		 */
1248 		for_each_pipe(i915, pipe) {
1249 			const struct intel_dbuf_bw *crtc_bw = &bw_state->dbuf_bw[pipe];
1250 
1251 			max_bw = max(crtc_bw->max_bw[slice], max_bw);
1252 			num_active_planes += hweight8(crtc_bw->active_planes[slice]);
1253 		}
1254 		max_bw *= num_active_planes;
1255 
1256 		total_max_bw = max(total_max_bw, max_bw);
1257 	}
1258 
1259 	return DIV_ROUND_UP(total_max_bw, 64);
1260 }
1261 
intel_bw_min_cdclk(struct drm_i915_private * i915,const struct intel_bw_state * bw_state)1262 int intel_bw_min_cdclk(struct drm_i915_private *i915,
1263 		       const struct intel_bw_state *bw_state)
1264 {
1265 	enum pipe pipe;
1266 	int min_cdclk;
1267 
1268 	min_cdclk = intel_bw_dbuf_min_cdclk(i915, bw_state);
1269 
1270 	for_each_pipe(i915, pipe)
1271 		min_cdclk = max(min_cdclk, bw_state->min_cdclk[pipe]);
1272 
1273 	return min_cdclk;
1274 }
1275 
intel_bw_calc_min_cdclk(struct intel_atomic_state * state,bool * need_cdclk_calc)1276 int intel_bw_calc_min_cdclk(struct intel_atomic_state *state,
1277 			    bool *need_cdclk_calc)
1278 {
1279 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1280 	struct intel_bw_state *new_bw_state = NULL;
1281 	const struct intel_bw_state *old_bw_state = NULL;
1282 	const struct intel_cdclk_state *cdclk_state;
1283 	const struct intel_crtc_state *crtc_state;
1284 	int old_min_cdclk, new_min_cdclk;
1285 	struct intel_crtc *crtc;
1286 	int i;
1287 
1288 	if (DISPLAY_VER(dev_priv) < 9)
1289 		return 0;
1290 
1291 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
1292 		new_bw_state = intel_atomic_get_bw_state(state);
1293 		if (IS_ERR(new_bw_state))
1294 			return PTR_ERR(new_bw_state);
1295 
1296 		old_bw_state = intel_atomic_get_old_bw_state(state);
1297 
1298 		skl_crtc_calc_dbuf_bw(new_bw_state, crtc_state);
1299 
1300 		new_bw_state->min_cdclk[crtc->pipe] =
1301 			intel_bw_crtc_min_cdclk(crtc_state);
1302 	}
1303 
1304 	if (!old_bw_state)
1305 		return 0;
1306 
1307 	if (intel_bw_state_changed(dev_priv, old_bw_state, new_bw_state)) {
1308 		int ret = intel_atomic_lock_global_state(&new_bw_state->base);
1309 		if (ret)
1310 			return ret;
1311 	}
1312 
1313 	old_min_cdclk = intel_bw_min_cdclk(dev_priv, old_bw_state);
1314 	new_min_cdclk = intel_bw_min_cdclk(dev_priv, new_bw_state);
1315 
1316 	/*
1317 	 * No need to check against the cdclk state if
1318 	 * the min cdclk doesn't increase.
1319 	 *
1320 	 * Ie. we only ever increase the cdclk due to bandwidth
1321 	 * requirements. This can reduce back and forth
1322 	 * display blinking due to constant cdclk changes.
1323 	 */
1324 	if (new_min_cdclk <= old_min_cdclk)
1325 		return 0;
1326 
1327 	cdclk_state = intel_atomic_get_cdclk_state(state);
1328 	if (IS_ERR(cdclk_state))
1329 		return PTR_ERR(cdclk_state);
1330 
1331 	/*
1332 	 * No need to recalculate the cdclk state if
1333 	 * the min cdclk doesn't increase.
1334 	 *
1335 	 * Ie. we only ever increase the cdclk due to bandwidth
1336 	 * requirements. This can reduce back and forth
1337 	 * display blinking due to constant cdclk changes.
1338 	 */
1339 	if (new_min_cdclk <= cdclk_state->bw_min_cdclk)
1340 		return 0;
1341 
1342 	drm_dbg_kms(&dev_priv->drm,
1343 		    "new bandwidth min cdclk (%d kHz) > old min cdclk (%d kHz)\n",
1344 		    new_min_cdclk, cdclk_state->bw_min_cdclk);
1345 	*need_cdclk_calc = true;
1346 
1347 	return 0;
1348 }
1349 
intel_bw_check_data_rate(struct intel_atomic_state * state,bool * changed)1350 static int intel_bw_check_data_rate(struct intel_atomic_state *state, bool *changed)
1351 {
1352 	struct drm_i915_private *i915 = to_i915(state->base.dev);
1353 	const struct intel_crtc_state *new_crtc_state, *old_crtc_state;
1354 	struct intel_crtc *crtc;
1355 	int i;
1356 
1357 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
1358 					    new_crtc_state, i) {
1359 		unsigned int old_data_rate =
1360 			intel_bw_crtc_data_rate(old_crtc_state);
1361 		unsigned int new_data_rate =
1362 			intel_bw_crtc_data_rate(new_crtc_state);
1363 		unsigned int old_active_planes =
1364 			intel_bw_crtc_num_active_planes(old_crtc_state);
1365 		unsigned int new_active_planes =
1366 			intel_bw_crtc_num_active_planes(new_crtc_state);
1367 		struct intel_bw_state *new_bw_state;
1368 
1369 		/*
1370 		 * Avoid locking the bw state when
1371 		 * nothing significant has changed.
1372 		 */
1373 		if (old_data_rate == new_data_rate &&
1374 		    old_active_planes == new_active_planes)
1375 			continue;
1376 
1377 		new_bw_state = intel_atomic_get_bw_state(state);
1378 		if (IS_ERR(new_bw_state))
1379 			return PTR_ERR(new_bw_state);
1380 
1381 		new_bw_state->data_rate[crtc->pipe] = new_data_rate;
1382 		new_bw_state->num_active_planes[crtc->pipe] = new_active_planes;
1383 
1384 		*changed = true;
1385 
1386 		drm_dbg_kms(&i915->drm,
1387 			    "[CRTC:%d:%s] data rate %u num active planes %u\n",
1388 			    crtc->base.base.id, crtc->base.name,
1389 			    new_bw_state->data_rate[crtc->pipe],
1390 			    new_bw_state->num_active_planes[crtc->pipe]);
1391 	}
1392 
1393 	return 0;
1394 }
1395 
intel_bw_atomic_check(struct intel_atomic_state * state)1396 int intel_bw_atomic_check(struct intel_atomic_state *state)
1397 {
1398 	bool changed = false;
1399 	struct drm_i915_private *i915 = to_i915(state->base.dev);
1400 	struct intel_bw_state *new_bw_state;
1401 	const struct intel_bw_state *old_bw_state;
1402 	int ret;
1403 
1404 	/* FIXME earlier gens need some checks too */
1405 	if (DISPLAY_VER(i915) < 11)
1406 		return 0;
1407 
1408 	ret = intel_bw_check_data_rate(state, &changed);
1409 	if (ret)
1410 		return ret;
1411 
1412 	old_bw_state = intel_atomic_get_old_bw_state(state);
1413 	new_bw_state = intel_atomic_get_new_bw_state(state);
1414 
1415 	if (new_bw_state &&
1416 	    (intel_can_enable_sagv(i915, old_bw_state) !=
1417 	     intel_can_enable_sagv(i915, new_bw_state) ||
1418 	     new_bw_state->force_check_qgv))
1419 		changed = true;
1420 
1421 	/*
1422 	 * If none of our inputs (data rates, number of active
1423 	 * planes, SAGV yes/no) changed then nothing to do here.
1424 	 */
1425 	if (!changed)
1426 		return 0;
1427 
1428 	ret = intel_bw_check_qgv_points(i915, old_bw_state, new_bw_state);
1429 	if (ret)
1430 		return ret;
1431 
1432 	new_bw_state->force_check_qgv = false;
1433 
1434 	return 0;
1435 }
1436 
1437 static struct intel_global_state *
intel_bw_duplicate_state(struct intel_global_obj * obj)1438 intel_bw_duplicate_state(struct intel_global_obj *obj)
1439 {
1440 	struct intel_bw_state *state;
1441 
1442 	state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
1443 	if (!state)
1444 		return NULL;
1445 
1446 	return &state->base;
1447 }
1448 
intel_bw_destroy_state(struct intel_global_obj * obj,struct intel_global_state * state)1449 static void intel_bw_destroy_state(struct intel_global_obj *obj,
1450 				   struct intel_global_state *state)
1451 {
1452 	kfree(state);
1453 }
1454 
1455 static const struct intel_global_state_funcs intel_bw_funcs = {
1456 	.atomic_duplicate_state = intel_bw_duplicate_state,
1457 	.atomic_destroy_state = intel_bw_destroy_state,
1458 };
1459 
intel_bw_init(struct drm_i915_private * i915)1460 int intel_bw_init(struct drm_i915_private *i915)
1461 {
1462 	struct intel_display *display = &i915->display;
1463 	struct intel_bw_state *state;
1464 
1465 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1466 	if (!state)
1467 		return -ENOMEM;
1468 
1469 	intel_atomic_global_obj_init(display, &display->bw.obj,
1470 				     &state->base, &intel_bw_funcs);
1471 
1472 	/*
1473 	 * Limit this only if we have SAGV. And for Display version 14 onwards
1474 	 * sagv is handled though pmdemand requests
1475 	 */
1476 	if (intel_has_sagv(i915) && IS_DISPLAY_VER(i915, 11, 13))
1477 		icl_force_disable_sagv(i915, state);
1478 
1479 	return 0;
1480 }
1481