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