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