xref: /linux/drivers/gpu/drm/i915/display/intel_bw.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1df0566a6SJani Nikula // SPDX-License-Identifier: MIT
2df0566a6SJani Nikula /*
3df0566a6SJani Nikula  * Copyright © 2019 Intel Corporation
4df0566a6SJani Nikula  */
5df0566a6SJani Nikula 
6df0566a6SJani Nikula #include <drm/drm_atomic_state_helper.h>
7df0566a6SJani Nikula 
842a0d256SVille Syrjälä #include "i915_drv.h"
9ce2fce25SMatt Roper #include "i915_reg.h"
10a7f46d5bSTvrtko Ursulin #include "i915_utils.h"
1120f505f2SStanislav Lisovskiy #include "intel_atomic.h"
12cac91e67SStanislav Lisovskiy #include "intel_bw.h"
13cd191546SStanislav Lisovskiy #include "intel_cdclk.h"
1442a0d256SVille Syrjälä #include "intel_display_core.h"
15cac91e67SStanislav Lisovskiy #include "intel_display_types.h"
1642a0d256SVille Syrjälä #include "skl_watermark.h"
17e30e6c7bSMatt Roper #include "intel_mchbar_regs.h"
184dd4375bSJani Nikula #include "intel_pcode.h"
19df0566a6SJani Nikula 
20df0566a6SJani Nikula /* Parameters for Qclk Geyserville (QGV) */
21df0566a6SJani Nikula struct intel_qgv_point {
22df0566a6SJani Nikula 	u16 dclk, t_rp, t_rdpre, t_rc, t_ras, t_rcd;
23df0566a6SJani Nikula };
24df0566a6SJani Nikula 
25*772933b3SMatt Roper #define DEPROGBWPCLIMIT		60
26*772933b3SMatt Roper 
27192fbfb7SStanislav Lisovskiy struct intel_psf_gv_point {
28192fbfb7SStanislav Lisovskiy 	u8 clk; /* clock in multiples of 16.6666 MHz */
29192fbfb7SStanislav Lisovskiy };
30192fbfb7SStanislav Lisovskiy 
31df0566a6SJani Nikula struct intel_qgv_info {
329b93daa9SStanislav Lisovskiy 	struct intel_qgv_point points[I915_NUM_QGV_POINTS];
33192fbfb7SStanislav Lisovskiy 	struct intel_psf_gv_point psf_points[I915_NUM_PSF_GV_POINTS];
34df0566a6SJani Nikula 	u8 num_points;
35192fbfb7SStanislav Lisovskiy 	u8 num_psf_points;
36df0566a6SJani Nikula 	u8 t_bl;
37c64a9a7cSRadhakrishna Sripada 	u8 max_numchannels;
38c64a9a7cSRadhakrishna Sripada 	u8 channel_width;
39c64a9a7cSRadhakrishna Sripada 	u8 deinterleave;
40df0566a6SJani Nikula };
41df0566a6SJani Nikula 
dg1_mchbar_read_qgv_point_info(struct drm_i915_private * dev_priv,struct intel_qgv_point * sp,int point)424de06246SClint Taylor static int dg1_mchbar_read_qgv_point_info(struct drm_i915_private *dev_priv,
434de06246SClint Taylor 					  struct intel_qgv_point *sp,
444de06246SClint Taylor 					  int point)
454de06246SClint Taylor {
464de06246SClint Taylor 	u32 dclk_ratio, dclk_reference;
474de06246SClint Taylor 	u32 val;
484de06246SClint Taylor 
494de06246SClint Taylor 	val = intel_uncore_read(&dev_priv->uncore, SA_PERF_STATUS_0_0_0_MCHBAR_PC);
504de06246SClint Taylor 	dclk_ratio = REG_FIELD_GET(DG1_QCLK_RATIO_MASK, val);
514de06246SClint Taylor 	if (val & DG1_QCLK_REFERENCE)
524de06246SClint Taylor 		dclk_reference = 6; /* 6 * 16.666 MHz = 100 MHz */
534de06246SClint Taylor 	else
544de06246SClint Taylor 		dclk_reference = 8; /* 8 * 16.666 MHz = 133 MHz */
55c64a9a7cSRadhakrishna Sripada 	sp->dclk = DIV_ROUND_UP((16667 * dclk_ratio * dclk_reference) + 500, 1000);
564de06246SClint Taylor 
574de06246SClint Taylor 	val = intel_uncore_read(&dev_priv->uncore, SKL_MC_BIOS_DATA_0_0_0_MCHBAR_PCU);
584de06246SClint Taylor 	if (val & DG1_GEAR_TYPE)
594de06246SClint Taylor 		sp->dclk *= 2;
604de06246SClint Taylor 
614de06246SClint Taylor 	if (sp->dclk == 0)
624de06246SClint Taylor 		return -EINVAL;
634de06246SClint Taylor 
644de06246SClint Taylor 	val = intel_uncore_read(&dev_priv->uncore, MCHBAR_CH0_CR_TC_PRE_0_0_0_MCHBAR);
654de06246SClint Taylor 	sp->t_rp = REG_FIELD_GET(DG1_DRAM_T_RP_MASK, val);
664de06246SClint Taylor 	sp->t_rdpre = REG_FIELD_GET(DG1_DRAM_T_RDPRE_MASK, val);
674de06246SClint Taylor 
684de06246SClint Taylor 	val = intel_uncore_read(&dev_priv->uncore, MCHBAR_CH0_CR_TC_PRE_0_0_0_MCHBAR_HIGH);
694de06246SClint Taylor 	sp->t_rcd = REG_FIELD_GET(DG1_DRAM_T_RCD_MASK, val);
704de06246SClint Taylor 	sp->t_ras = REG_FIELD_GET(DG1_DRAM_T_RAS_MASK, val);
714de06246SClint Taylor 
724de06246SClint Taylor 	sp->t_rc = sp->t_rp + sp->t_ras;
734de06246SClint Taylor 
744de06246SClint Taylor 	return 0;
754de06246SClint Taylor }
764de06246SClint Taylor 
icl_pcode_read_qgv_point_info(struct drm_i915_private * dev_priv,struct intel_qgv_point * sp,int point)77df0566a6SJani Nikula static int icl_pcode_read_qgv_point_info(struct drm_i915_private *dev_priv,
78df0566a6SJani Nikula 					 struct intel_qgv_point *sp,
79df0566a6SJani Nikula 					 int point)
80df0566a6SJani Nikula {
81b12d5944SVille Syrjälä 	u32 val = 0, val2 = 0;
82c64a9a7cSRadhakrishna Sripada 	u16 dclk;
83df0566a6SJani Nikula 	int ret;
84df0566a6SJani Nikula 
85ee421bb4SAshutosh Dixit 	ret = snb_pcode_read(&dev_priv->uncore, ICL_PCODE_MEM_SUBSYSYSTEM_INFO |
86df0566a6SJani Nikula 			     ICL_PCODE_MEM_SS_READ_QGV_POINT_INFO(point),
87df0566a6SJani Nikula 			     &val, &val2);
88df0566a6SJani Nikula 	if (ret)
89df0566a6SJani Nikula 		return ret;
90df0566a6SJani Nikula 
91c64a9a7cSRadhakrishna Sripada 	dclk = val & 0xffff;
928dfce5f3SVille Syrjälä 	sp->dclk = DIV_ROUND_UP((16667 * dclk) + (DISPLAY_VER(dev_priv) >= 12 ? 500 : 0),
938dfce5f3SVille Syrjälä 				1000);
94df0566a6SJani Nikula 	sp->t_rp = (val & 0xff0000) >> 16;
95df0566a6SJani Nikula 	sp->t_rcd = (val & 0xff000000) >> 24;
96df0566a6SJani Nikula 
97df0566a6SJani Nikula 	sp->t_rdpre = val2 & 0xff;
98df0566a6SJani Nikula 	sp->t_ras = (val2 & 0xff00) >> 8;
99df0566a6SJani Nikula 
100df0566a6SJani Nikula 	sp->t_rc = sp->t_rp + sp->t_ras;
101df0566a6SJani Nikula 
102df0566a6SJani Nikula 	return 0;
103df0566a6SJani Nikula }
104df0566a6SJani Nikula 
adls_pcode_read_psf_gv_point_info(struct drm_i915_private * dev_priv,struct intel_psf_gv_point * points)105192fbfb7SStanislav Lisovskiy static int adls_pcode_read_psf_gv_point_info(struct drm_i915_private *dev_priv,
106192fbfb7SStanislav Lisovskiy 					    struct intel_psf_gv_point *points)
107192fbfb7SStanislav Lisovskiy {
108192fbfb7SStanislav Lisovskiy 	u32 val = 0;
109192fbfb7SStanislav Lisovskiy 	int ret;
110192fbfb7SStanislav Lisovskiy 	int i;
111192fbfb7SStanislav Lisovskiy 
112ee421bb4SAshutosh Dixit 	ret = snb_pcode_read(&dev_priv->uncore, ICL_PCODE_MEM_SUBSYSYSTEM_INFO |
1136650ebcbSJani Nikula 			     ADL_PCODE_MEM_SS_READ_PSF_GV_INFO, &val, NULL);
114192fbfb7SStanislav Lisovskiy 	if (ret)
115192fbfb7SStanislav Lisovskiy 		return ret;
116192fbfb7SStanislav Lisovskiy 
117192fbfb7SStanislav Lisovskiy 	for (i = 0; i < I915_NUM_PSF_GV_POINTS; i++) {
118192fbfb7SStanislav Lisovskiy 		points[i].clk = val & 0xff;
119192fbfb7SStanislav Lisovskiy 		val >>= 8;
120192fbfb7SStanislav Lisovskiy 	}
121192fbfb7SStanislav Lisovskiy 
122192fbfb7SStanislav Lisovskiy 	return 0;
123192fbfb7SStanislav Lisovskiy }
124192fbfb7SStanislav Lisovskiy 
icl_qgv_points_mask(struct drm_i915_private * i915)1259541fd16SVille Syrjälä static u16 icl_qgv_points_mask(struct drm_i915_private *i915)
1269541fd16SVille Syrjälä {
1279541fd16SVille Syrjälä 	unsigned int num_psf_gv_points = i915->display.bw.max[0].num_psf_gv_points;
1289541fd16SVille Syrjälä 	unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
1299541fd16SVille Syrjälä 	u16 qgv_points = 0, psf_points = 0;
1309541fd16SVille Syrjälä 
1319541fd16SVille Syrjälä 	/*
1329541fd16SVille Syrjälä 	 * We can _not_ use the whole ADLS_QGV_PT_MASK here, as PCode rejects
1339541fd16SVille Syrjälä 	 * it with failure if we try masking any unadvertised points.
1349541fd16SVille Syrjälä 	 * So need to operate only with those returned from PCode.
1359541fd16SVille Syrjälä 	 */
1369541fd16SVille Syrjälä 	if (num_qgv_points > 0)
1379541fd16SVille Syrjälä 		qgv_points = GENMASK(num_qgv_points - 1, 0);
1389541fd16SVille Syrjälä 
1399541fd16SVille Syrjälä 	if (num_psf_gv_points > 0)
1409541fd16SVille Syrjälä 		psf_points = GENMASK(num_psf_gv_points - 1, 0);
1419541fd16SVille Syrjälä 
1429541fd16SVille Syrjälä 	return ICL_PCODE_REQ_QGV_PT(qgv_points) | ADLS_PCODE_REQ_PSF_PT(psf_points);
1439541fd16SVille Syrjälä }
1449541fd16SVille Syrjälä 
is_sagv_enabled(struct drm_i915_private * i915,u16 points_mask)1459541fd16SVille Syrjälä static bool is_sagv_enabled(struct drm_i915_private *i915, u16 points_mask)
1469541fd16SVille Syrjälä {
1479541fd16SVille Syrjälä 	return !is_power_of_2(~points_mask & icl_qgv_points_mask(i915) &
1489541fd16SVille Syrjälä 			      ICL_PCODE_REQ_QGV_PT_MASK);
1499541fd16SVille Syrjälä }
1509541fd16SVille Syrjälä 
icl_pcode_restrict_qgv_points(struct drm_i915_private * dev_priv,u32 points_mask)15120f505f2SStanislav Lisovskiy int icl_pcode_restrict_qgv_points(struct drm_i915_private *dev_priv,
15220f505f2SStanislav Lisovskiy 				  u32 points_mask)
15320f505f2SStanislav Lisovskiy {
15420f505f2SStanislav Lisovskiy 	int ret;
15520f505f2SStanislav Lisovskiy 
1566152aec1SRadhakrishna Sripada 	if (DISPLAY_VER(dev_priv) >= 14)
1576152aec1SRadhakrishna Sripada 		return 0;
1586152aec1SRadhakrishna Sripada 
15920f505f2SStanislav Lisovskiy 	/* bspec says to keep retrying for at least 1 ms */
160ee421bb4SAshutosh Dixit 	ret = skl_pcode_request(&dev_priv->uncore, ICL_PCODE_SAGV_DE_MEM_SS_CONFIG,
16120f505f2SStanislav Lisovskiy 				points_mask,
1624bdba4f4SVille Syrjälä 				ICL_PCODE_REP_QGV_MASK | ADLS_PCODE_REP_PSF_MASK,
1634bdba4f4SVille Syrjälä 				ICL_PCODE_REP_QGV_SAFE | ADLS_PCODE_REP_PSF_SAFE,
16420f505f2SStanislav Lisovskiy 				1);
16520f505f2SStanislav Lisovskiy 
16620f505f2SStanislav Lisovskiy 	if (ret < 0) {
1679299cde9SStanislav Lisovskiy 		drm_err(&dev_priv->drm,
1689299cde9SStanislav Lisovskiy 			"Failed to disable qgv points (0x%x) points: 0x%x\n",
1699299cde9SStanislav Lisovskiy 			ret, points_mask);
17020f505f2SStanislav Lisovskiy 		return ret;
17120f505f2SStanislav Lisovskiy 	}
17220f505f2SStanislav Lisovskiy 
1739541fd16SVille Syrjälä 	dev_priv->display.sagv.status = is_sagv_enabled(dev_priv, points_mask) ?
1749541fd16SVille Syrjälä 		I915_SAGV_ENABLED : I915_SAGV_DISABLED;
1759541fd16SVille Syrjälä 
17620f505f2SStanislav Lisovskiy 	return 0;
17720f505f2SStanislav Lisovskiy }
17820f505f2SStanislav Lisovskiy 
mtl_read_qgv_point_info(struct drm_i915_private * dev_priv,struct intel_qgv_point * sp,int point)179825477e7SRadhakrishna Sripada static int mtl_read_qgv_point_info(struct drm_i915_private *dev_priv,
180825477e7SRadhakrishna Sripada 				   struct intel_qgv_point *sp, int point)
181825477e7SRadhakrishna Sripada {
182825477e7SRadhakrishna Sripada 	u32 val, val2;
183825477e7SRadhakrishna Sripada 	u16 dclk;
184825477e7SRadhakrishna Sripada 
185825477e7SRadhakrishna Sripada 	val = intel_uncore_read(&dev_priv->uncore,
186825477e7SRadhakrishna Sripada 				MTL_MEM_SS_INFO_QGV_POINT_LOW(point));
187825477e7SRadhakrishna Sripada 	val2 = intel_uncore_read(&dev_priv->uncore,
188825477e7SRadhakrishna Sripada 				 MTL_MEM_SS_INFO_QGV_POINT_HIGH(point));
189825477e7SRadhakrishna Sripada 	dclk = REG_FIELD_GET(MTL_DCLK_MASK, val);
19039bea0ffSVinod Govindapillai 	sp->dclk = DIV_ROUND_CLOSEST(16667 * dclk, 1000);
191825477e7SRadhakrishna Sripada 	sp->t_rp = REG_FIELD_GET(MTL_TRP_MASK, val);
192825477e7SRadhakrishna Sripada 	sp->t_rcd = REG_FIELD_GET(MTL_TRCD_MASK, val);
193825477e7SRadhakrishna Sripada 
194825477e7SRadhakrishna Sripada 	sp->t_rdpre = REG_FIELD_GET(MTL_TRDPRE_MASK, val2);
195825477e7SRadhakrishna Sripada 	sp->t_ras = REG_FIELD_GET(MTL_TRAS_MASK, val2);
196825477e7SRadhakrishna Sripada 
197825477e7SRadhakrishna Sripada 	sp->t_rc = sp->t_rp + sp->t_ras;
198825477e7SRadhakrishna Sripada 
199825477e7SRadhakrishna Sripada 	return 0;
200825477e7SRadhakrishna Sripada }
201825477e7SRadhakrishna Sripada 
202825477e7SRadhakrishna Sripada static int
intel_read_qgv_point_info(struct drm_i915_private * dev_priv,struct intel_qgv_point * sp,int point)203825477e7SRadhakrishna Sripada intel_read_qgv_point_info(struct drm_i915_private *dev_priv,
204825477e7SRadhakrishna Sripada 			  struct intel_qgv_point *sp,
205825477e7SRadhakrishna Sripada 			  int point)
206825477e7SRadhakrishna Sripada {
207825477e7SRadhakrishna Sripada 	if (DISPLAY_VER(dev_priv) >= 14)
208825477e7SRadhakrishna Sripada 		return mtl_read_qgv_point_info(dev_priv, sp, point);
209825477e7SRadhakrishna Sripada 	else if (IS_DG1(dev_priv))
210825477e7SRadhakrishna Sripada 		return dg1_mchbar_read_qgv_point_info(dev_priv, sp, point);
211825477e7SRadhakrishna Sripada 	else
212825477e7SRadhakrishna Sripada 		return icl_pcode_read_qgv_point_info(dev_priv, sp, point);
213825477e7SRadhakrishna Sripada }
214825477e7SRadhakrishna Sripada 
icl_get_qgv_points(struct drm_i915_private * dev_priv,struct intel_qgv_info * qi,bool is_y_tile)215df0566a6SJani Nikula static int icl_get_qgv_points(struct drm_i915_private *dev_priv,
216c64a9a7cSRadhakrishna Sripada 			      struct intel_qgv_info *qi,
217c64a9a7cSRadhakrishna Sripada 			      bool is_y_tile)
218df0566a6SJani Nikula {
2195d0c938eSJosé Roberto de Souza 	const struct dram_info *dram_info = &dev_priv->dram_info;
220df0566a6SJani Nikula 	int i, ret;
221df0566a6SJani Nikula 
2225d0c938eSJosé Roberto de Souza 	qi->num_points = dram_info->num_qgv_points;
223192fbfb7SStanislav Lisovskiy 	qi->num_psf_points = dram_info->num_psf_gv_points;
2245d0c938eSJosé Roberto de Souza 
2253eb4ad93SRadhakrishna Sripada 	if (DISPLAY_VER(dev_priv) >= 14) {
2263eb4ad93SRadhakrishna Sripada 		switch (dram_info->type) {
2273eb4ad93SRadhakrishna Sripada 		case INTEL_DRAM_DDR4:
2283eb4ad93SRadhakrishna Sripada 			qi->t_bl = 4;
2293eb4ad93SRadhakrishna Sripada 			qi->max_numchannels = 2;
2303eb4ad93SRadhakrishna Sripada 			qi->channel_width = 64;
2313eb4ad93SRadhakrishna Sripada 			qi->deinterleave = 2;
2323eb4ad93SRadhakrishna Sripada 			break;
2333eb4ad93SRadhakrishna Sripada 		case INTEL_DRAM_DDR5:
2343eb4ad93SRadhakrishna Sripada 			qi->t_bl = 8;
2353eb4ad93SRadhakrishna Sripada 			qi->max_numchannels = 4;
2363eb4ad93SRadhakrishna Sripada 			qi->channel_width = 32;
2373eb4ad93SRadhakrishna Sripada 			qi->deinterleave = 2;
2383eb4ad93SRadhakrishna Sripada 			break;
2393eb4ad93SRadhakrishna Sripada 		case INTEL_DRAM_LPDDR4:
2403eb4ad93SRadhakrishna Sripada 		case INTEL_DRAM_LPDDR5:
2413eb4ad93SRadhakrishna Sripada 			qi->t_bl = 16;
2423eb4ad93SRadhakrishna Sripada 			qi->max_numchannels = 8;
2433eb4ad93SRadhakrishna Sripada 			qi->channel_width = 16;
2443eb4ad93SRadhakrishna Sripada 			qi->deinterleave = 4;
2453eb4ad93SRadhakrishna Sripada 			break;
246*772933b3SMatt Roper 		case INTEL_DRAM_GDDR:
247*772933b3SMatt Roper 			qi->channel_width = 32;
248*772933b3SMatt Roper 			break;
2493eb4ad93SRadhakrishna Sripada 		default:
2503eb4ad93SRadhakrishna Sripada 			MISSING_CASE(dram_info->type);
2513eb4ad93SRadhakrishna Sripada 			return -EINVAL;
2523eb4ad93SRadhakrishna Sripada 		}
2533eb4ad93SRadhakrishna Sripada 	} else if (DISPLAY_VER(dev_priv) >= 12) {
2541f1257a6SClint Taylor 		switch (dram_info->type) {
2551f1257a6SClint Taylor 		case INTEL_DRAM_DDR4:
256c64a9a7cSRadhakrishna Sripada 			qi->t_bl = is_y_tile ? 8 : 4;
257c64a9a7cSRadhakrishna Sripada 			qi->max_numchannels = 2;
258c64a9a7cSRadhakrishna Sripada 			qi->channel_width = 64;
259c64a9a7cSRadhakrishna Sripada 			qi->deinterleave = is_y_tile ? 1 : 2;
2601f1257a6SClint Taylor 			break;
2611f1257a6SClint Taylor 		case INTEL_DRAM_DDR5:
262c64a9a7cSRadhakrishna Sripada 			qi->t_bl = is_y_tile ? 16 : 8;
263c64a9a7cSRadhakrishna Sripada 			qi->max_numchannels = 4;
264c64a9a7cSRadhakrishna Sripada 			qi->channel_width = 32;
265c64a9a7cSRadhakrishna Sripada 			qi->deinterleave = is_y_tile ? 1 : 2;
266c64a9a7cSRadhakrishna Sripada 			break;
267c64a9a7cSRadhakrishna Sripada 		case INTEL_DRAM_LPDDR4:
268c64a9a7cSRadhakrishna Sripada 			if (IS_ROCKETLAKE(dev_priv)) {
2691f1257a6SClint Taylor 				qi->t_bl = 8;
270c64a9a7cSRadhakrishna Sripada 				qi->max_numchannels = 4;
271c64a9a7cSRadhakrishna Sripada 				qi->channel_width = 32;
272c64a9a7cSRadhakrishna Sripada 				qi->deinterleave = 2;
273c64a9a7cSRadhakrishna Sripada 				break;
274c64a9a7cSRadhakrishna Sripada 			}
275c64a9a7cSRadhakrishna Sripada 			fallthrough;
276c64a9a7cSRadhakrishna Sripada 		case INTEL_DRAM_LPDDR5:
277c64a9a7cSRadhakrishna Sripada 			qi->t_bl = 16;
278c64a9a7cSRadhakrishna Sripada 			qi->max_numchannels = 8;
279c64a9a7cSRadhakrishna Sripada 			qi->channel_width = 16;
280c64a9a7cSRadhakrishna Sripada 			qi->deinterleave = is_y_tile ? 2 : 4;
2811f1257a6SClint Taylor 			break;
2821f1257a6SClint Taylor 		default:
2831f1257a6SClint Taylor 			qi->t_bl = 16;
284c64a9a7cSRadhakrishna Sripada 			qi->max_numchannels = 1;
2851f1257a6SClint Taylor 			break;
2861f1257a6SClint Taylor 		}
2873eb4ad93SRadhakrishna Sripada 	} else if (DISPLAY_VER(dev_priv) == 11) {
2885d0c938eSJosé Roberto de Souza 		qi->t_bl = dev_priv->dram_info.type == INTEL_DRAM_DDR4 ? 4 : 8;
289c64a9a7cSRadhakrishna Sripada 		qi->max_numchannels = 1;
290c64a9a7cSRadhakrishna Sripada 	}
291df0566a6SJani Nikula 
292f4224a4cSPankaj Bharadiya 	if (drm_WARN_ON(&dev_priv->drm,
293f4224a4cSPankaj Bharadiya 			qi->num_points > ARRAY_SIZE(qi->points)))
294df0566a6SJani Nikula 		qi->num_points = ARRAY_SIZE(qi->points);
295df0566a6SJani Nikula 
296df0566a6SJani Nikula 	for (i = 0; i < qi->num_points; i++) {
297df0566a6SJani Nikula 		struct intel_qgv_point *sp = &qi->points[i];
298df0566a6SJani Nikula 
299825477e7SRadhakrishna Sripada 		ret = intel_read_qgv_point_info(dev_priv, sp, i);
30092363681SStanislav Lisovskiy 		if (ret) {
30192363681SStanislav Lisovskiy 			drm_dbg_kms(&dev_priv->drm, "Could not read QGV %d info\n", i);
302df0566a6SJani Nikula 			return ret;
30392363681SStanislav Lisovskiy 		}
304df0566a6SJani Nikula 
3052e3586ceSWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
3062e3586ceSWambui Karuga 			    "QGV %d: DCLK=%d tRP=%d tRDPRE=%d tRAS=%d tRCD=%d tRC=%d\n",
307df0566a6SJani Nikula 			    i, sp->dclk, sp->t_rp, sp->t_rdpre, sp->t_ras,
308df0566a6SJani Nikula 			    sp->t_rcd, sp->t_rc);
309df0566a6SJani Nikula 	}
310df0566a6SJani Nikula 
311192fbfb7SStanislav Lisovskiy 	if (qi->num_psf_points > 0) {
312192fbfb7SStanislav Lisovskiy 		ret = adls_pcode_read_psf_gv_point_info(dev_priv, qi->psf_points);
313192fbfb7SStanislav Lisovskiy 		if (ret) {
314192fbfb7SStanislav Lisovskiy 			drm_err(&dev_priv->drm, "Failed to read PSF point data; PSF points will not be considered in bandwidth calculations.\n");
315192fbfb7SStanislav Lisovskiy 			qi->num_psf_points = 0;
316192fbfb7SStanislav Lisovskiy 		}
317192fbfb7SStanislav Lisovskiy 
318192fbfb7SStanislav Lisovskiy 		for (i = 0; i < qi->num_psf_points; i++)
319192fbfb7SStanislav Lisovskiy 			drm_dbg_kms(&dev_priv->drm,
320192fbfb7SStanislav Lisovskiy 				    "PSF GV %d: CLK=%d \n",
321192fbfb7SStanislav Lisovskiy 				    i, qi->psf_points[i].clk);
322192fbfb7SStanislav Lisovskiy 	}
323192fbfb7SStanislav Lisovskiy 
324df0566a6SJani Nikula 	return 0;
325df0566a6SJani Nikula }
326df0566a6SJani Nikula 
adl_calc_psf_bw(int clk)327192fbfb7SStanislav Lisovskiy static int adl_calc_psf_bw(int clk)
328192fbfb7SStanislav Lisovskiy {
329192fbfb7SStanislav Lisovskiy 	/*
330192fbfb7SStanislav Lisovskiy 	 * clk is multiples of 16.666MHz (100/6)
331192fbfb7SStanislav Lisovskiy 	 * According to BSpec PSF GV bandwidth is
332192fbfb7SStanislav Lisovskiy 	 * calculated as BW = 64 * clk * 16.666Mhz
333192fbfb7SStanislav Lisovskiy 	 */
334192fbfb7SStanislav Lisovskiy 	return DIV_ROUND_CLOSEST(64 * clk * 100, 6);
335192fbfb7SStanislav Lisovskiy }
336192fbfb7SStanislav Lisovskiy 
icl_sagv_max_dclk(const struct intel_qgv_info * qi)337df0566a6SJani Nikula static int icl_sagv_max_dclk(const struct intel_qgv_info *qi)
338df0566a6SJani Nikula {
339df0566a6SJani Nikula 	u16 dclk = 0;
340df0566a6SJani Nikula 	int i;
341df0566a6SJani Nikula 
342df0566a6SJani Nikula 	for (i = 0; i < qi->num_points; i++)
343df0566a6SJani Nikula 		dclk = max(dclk, qi->points[i].dclk);
344df0566a6SJani Nikula 
345df0566a6SJani Nikula 	return dclk;
346df0566a6SJani Nikula }
347df0566a6SJani Nikula 
348df0566a6SJani Nikula struct intel_sa_info {
3491b74d467SStanislav Lisovskiy 	u16 displayrtids;
350f6d66fc8SRadhakrishna Sripada 	u8 deburst, deprogbwlimit, derating;
351df0566a6SJani Nikula };
352df0566a6SJani Nikula 
353df0566a6SJani Nikula static const struct intel_sa_info icl_sa_info = {
354df0566a6SJani Nikula 	.deburst = 8,
355df0566a6SJani Nikula 	.deprogbwlimit = 25, /* GB/s */
356df0566a6SJani Nikula 	.displayrtids = 128,
357f6d66fc8SRadhakrishna Sripada 	.derating = 10,
358df0566a6SJani Nikula };
359df0566a6SJani Nikula 
3601b74d467SStanislav Lisovskiy static const struct intel_sa_info tgl_sa_info = {
3611b74d467SStanislav Lisovskiy 	.deburst = 16,
3621b74d467SStanislav Lisovskiy 	.deprogbwlimit = 34, /* GB/s */
3631b74d467SStanislav Lisovskiy 	.displayrtids = 256,
364f6d66fc8SRadhakrishna Sripada 	.derating = 10,
3651b74d467SStanislav Lisovskiy };
3661b74d467SStanislav Lisovskiy 
367affd7bb6SMatt Roper static const struct intel_sa_info rkl_sa_info = {
368c64a9a7cSRadhakrishna Sripada 	.deburst = 8,
369affd7bb6SMatt Roper 	.deprogbwlimit = 20, /* GB/s */
370affd7bb6SMatt Roper 	.displayrtids = 128,
371f6d66fc8SRadhakrishna Sripada 	.derating = 10,
372affd7bb6SMatt Roper };
373affd7bb6SMatt Roper 
374918cc934STejas Upadhyay static const struct intel_sa_info adls_sa_info = {
375918cc934STejas Upadhyay 	.deburst = 16,
376918cc934STejas Upadhyay 	.deprogbwlimit = 38, /* GB/s */
377918cc934STejas Upadhyay 	.displayrtids = 256,
378f6d66fc8SRadhakrishna Sripada 	.derating = 10,
379f6d66fc8SRadhakrishna Sripada };
380f6d66fc8SRadhakrishna Sripada 
381f6d66fc8SRadhakrishna Sripada static const struct intel_sa_info adlp_sa_info = {
382f6d66fc8SRadhakrishna Sripada 	.deburst = 16,
383f6d66fc8SRadhakrishna Sripada 	.deprogbwlimit = 38, /* GB/s */
384f6d66fc8SRadhakrishna Sripada 	.displayrtids = 256,
385f6d66fc8SRadhakrishna Sripada 	.derating = 20,
386918cc934STejas Upadhyay };
387918cc934STejas Upadhyay 
3883eb4ad93SRadhakrishna Sripada static const struct intel_sa_info mtl_sa_info = {
3893eb4ad93SRadhakrishna Sripada 	.deburst = 32,
3903eb4ad93SRadhakrishna Sripada 	.deprogbwlimit = 38, /* GB/s */
3913eb4ad93SRadhakrishna Sripada 	.displayrtids = 256,
392626765bbSVinod Govindapillai 	.derating = 10,
3933eb4ad93SRadhakrishna Sripada };
3943eb4ad93SRadhakrishna Sripada 
395*772933b3SMatt Roper static const struct intel_sa_info xe2_hpd_sa_info = {
396*772933b3SMatt Roper 	.derating = 30,
397*772933b3SMatt Roper 	.deprogbwlimit = 53,
398*772933b3SMatt Roper 	/* Other values not used by simplified algorithm */
399*772933b3SMatt Roper };
400*772933b3SMatt Roper 
icl_get_bw_info(struct drm_i915_private * dev_priv,const struct intel_sa_info * sa)4011b74d467SStanislav Lisovskiy static int icl_get_bw_info(struct drm_i915_private *dev_priv, const struct intel_sa_info *sa)
402df0566a6SJani Nikula {
403df0566a6SJani Nikula 	struct intel_qgv_info qi = {};
404df0566a6SJani Nikula 	bool is_y_tile = true; /* assume y tile may be used */
405b554065cSJosé Roberto de Souza 	int num_channels = max_t(u8, 1, dev_priv->dram_info.num_channels);
406c64a9a7cSRadhakrishna Sripada 	int ipqdepth, ipqdepthpch = 16;
407df0566a6SJani Nikula 	int dclk_max;
408df0566a6SJani Nikula 	int maxdebw;
409f0acaf9dSJani Nikula 	int num_groups = ARRAY_SIZE(dev_priv->display.bw.max);
410df0566a6SJani Nikula 	int i, ret;
411df0566a6SJani Nikula 
412c64a9a7cSRadhakrishna Sripada 	ret = icl_get_qgv_points(dev_priv, &qi, is_y_tile);
413df0566a6SJani Nikula 	if (ret) {
4142e3586ceSWambui Karuga 		drm_dbg_kms(&dev_priv->drm,
4152e3586ceSWambui Karuga 			    "Failed to get memory subsystem information, ignoring bandwidth limits");
416df0566a6SJani Nikula 		return ret;
417df0566a6SJani Nikula 	}
418df0566a6SJani Nikula 
419df0566a6SJani Nikula 	dclk_max = icl_sagv_max_dclk(&qi);
420c64a9a7cSRadhakrishna Sripada 	maxdebw = min(sa->deprogbwlimit * 1000, dclk_max * 16 * 6 / 10);
421df0566a6SJani Nikula 	ipqdepth = min(ipqdepthpch, sa->displayrtids / num_channels);
422c64a9a7cSRadhakrishna Sripada 	qi.deinterleave = DIV_ROUND_UP(num_channels, is_y_tile ? 4 : 2);
423df0566a6SJani Nikula 
424c64a9a7cSRadhakrishna Sripada 	for (i = 0; i < num_groups; i++) {
425f0acaf9dSJani Nikula 		struct intel_bw_info *bi = &dev_priv->display.bw.max[i];
426df0566a6SJani Nikula 		int clpchgroup;
427df0566a6SJani Nikula 		int j;
428df0566a6SJani Nikula 
429c64a9a7cSRadhakrishna Sripada 		clpchgroup = (sa->deburst * qi.deinterleave / num_channels) << i;
430df0566a6SJani Nikula 		bi->num_planes = (ipqdepth - clpchgroup) / clpchgroup + 1;
431df0566a6SJani Nikula 
43256e9371bSVille Syrjälä 		bi->num_qgv_points = qi.num_points;
433192fbfb7SStanislav Lisovskiy 		bi->num_psf_gv_points = qi.num_psf_points;
43456e9371bSVille Syrjälä 
435df0566a6SJani Nikula 		for (j = 0; j < qi.num_points; j++) {
436df0566a6SJani Nikula 			const struct intel_qgv_point *sp = &qi.points[j];
437df0566a6SJani Nikula 			int ct, bw;
438df0566a6SJani Nikula 
439df0566a6SJani Nikula 			/*
440df0566a6SJani Nikula 			 * Max row cycle time
441df0566a6SJani Nikula 			 *
442df0566a6SJani Nikula 			 * FIXME what is the logic behind the
443df0566a6SJani Nikula 			 * assumed burst length?
444df0566a6SJani Nikula 			 */
445df0566a6SJani Nikula 			ct = max_t(int, sp->t_rc, sp->t_rp + sp->t_rcd +
446df0566a6SJani Nikula 				   (clpchgroup - 1) * qi.t_bl + sp->t_rdpre);
447c64a9a7cSRadhakrishna Sripada 			bw = DIV_ROUND_UP(sp->dclk * clpchgroup * 32 * num_channels, ct);
448c64a9a7cSRadhakrishna Sripada 
449c64a9a7cSRadhakrishna Sripada 			bi->deratedbw[j] = min(maxdebw,
450c64a9a7cSRadhakrishna Sripada 					       bw * (100 - sa->derating) / 100);
451c64a9a7cSRadhakrishna Sripada 
452c64a9a7cSRadhakrishna Sripada 			drm_dbg_kms(&dev_priv->drm,
453c64a9a7cSRadhakrishna Sripada 				    "BW%d / QGV %d: num_planes=%d deratedbw=%u\n",
454c64a9a7cSRadhakrishna Sripada 				    i, j, bi->num_planes, bi->deratedbw[j]);
455c64a9a7cSRadhakrishna Sripada 		}
456c64a9a7cSRadhakrishna Sripada 	}
457c64a9a7cSRadhakrishna Sripada 	/*
458c64a9a7cSRadhakrishna Sripada 	 * In case if SAGV is disabled in BIOS, we always get 1
459c64a9a7cSRadhakrishna Sripada 	 * SAGV point, but we can't send PCode commands to restrict it
460c64a9a7cSRadhakrishna Sripada 	 * as it will fail and pointless anyway.
461c64a9a7cSRadhakrishna Sripada 	 */
462c64a9a7cSRadhakrishna Sripada 	if (qi.num_points == 1)
463c3704f19SJani Nikula 		dev_priv->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
464c64a9a7cSRadhakrishna Sripada 	else
465c3704f19SJani Nikula 		dev_priv->display.sagv.status = I915_SAGV_ENABLED;
466c64a9a7cSRadhakrishna Sripada 
467c64a9a7cSRadhakrishna Sripada 	return 0;
468c64a9a7cSRadhakrishna Sripada }
469c64a9a7cSRadhakrishna Sripada 
tgl_get_bw_info(struct drm_i915_private * dev_priv,const struct intel_sa_info * sa)470c64a9a7cSRadhakrishna Sripada static int tgl_get_bw_info(struct drm_i915_private *dev_priv, const struct intel_sa_info *sa)
471c64a9a7cSRadhakrishna Sripada {
472c64a9a7cSRadhakrishna Sripada 	struct intel_qgv_info qi = {};
473c64a9a7cSRadhakrishna Sripada 	const struct dram_info *dram_info = &dev_priv->dram_info;
474c64a9a7cSRadhakrishna Sripada 	bool is_y_tile = true; /* assume y tile may be used */
475c64a9a7cSRadhakrishna Sripada 	int num_channels = max_t(u8, 1, dev_priv->dram_info.num_channels);
476c64a9a7cSRadhakrishna Sripada 	int ipqdepth, ipqdepthpch = 16;
477c64a9a7cSRadhakrishna Sripada 	int dclk_max;
478c64a9a7cSRadhakrishna Sripada 	int maxdebw, peakbw;
479c64a9a7cSRadhakrishna Sripada 	int clperchgroup;
480f0acaf9dSJani Nikula 	int num_groups = ARRAY_SIZE(dev_priv->display.bw.max);
481c64a9a7cSRadhakrishna Sripada 	int i, ret;
482c64a9a7cSRadhakrishna Sripada 
483c64a9a7cSRadhakrishna Sripada 	ret = icl_get_qgv_points(dev_priv, &qi, is_y_tile);
484c64a9a7cSRadhakrishna Sripada 	if (ret) {
485c64a9a7cSRadhakrishna Sripada 		drm_dbg_kms(&dev_priv->drm,
486c64a9a7cSRadhakrishna Sripada 			    "Failed to get memory subsystem information, ignoring bandwidth limits");
487c64a9a7cSRadhakrishna Sripada 		return ret;
488c64a9a7cSRadhakrishna Sripada 	}
489c64a9a7cSRadhakrishna Sripada 
490244c679bSRadhakrishna Sripada 	if (DISPLAY_VER(dev_priv) < 14 &&
491244c679bSRadhakrishna Sripada 	    (dram_info->type == INTEL_DRAM_LPDDR4 || dram_info->type == INTEL_DRAM_LPDDR5))
492c64a9a7cSRadhakrishna Sripada 		num_channels *= 2;
493c64a9a7cSRadhakrishna Sripada 
494c64a9a7cSRadhakrishna Sripada 	qi.deinterleave = qi.deinterleave ? : DIV_ROUND_UP(num_channels, is_y_tile ? 4 : 2);
495c64a9a7cSRadhakrishna Sripada 
496c64a9a7cSRadhakrishna Sripada 	if (num_channels < qi.max_numchannels && DISPLAY_VER(dev_priv) >= 12)
497c64a9a7cSRadhakrishna Sripada 		qi.deinterleave = max(DIV_ROUND_UP(qi.deinterleave, 2), 1);
498c64a9a7cSRadhakrishna Sripada 
4998dfce5f3SVille Syrjälä 	if (DISPLAY_VER(dev_priv) >= 12 && num_channels > qi.max_numchannels)
500c64a9a7cSRadhakrishna Sripada 		drm_warn(&dev_priv->drm, "Number of channels exceeds max number of channels.");
501c64a9a7cSRadhakrishna Sripada 	if (qi.max_numchannels != 0)
502c64a9a7cSRadhakrishna Sripada 		num_channels = min_t(u8, num_channels, qi.max_numchannels);
503c64a9a7cSRadhakrishna Sripada 
504c64a9a7cSRadhakrishna Sripada 	dclk_max = icl_sagv_max_dclk(&qi);
505c64a9a7cSRadhakrishna Sripada 
506c64a9a7cSRadhakrishna Sripada 	peakbw = num_channels * DIV_ROUND_UP(qi.channel_width, 8) * dclk_max;
507*772933b3SMatt Roper 	maxdebw = min(sa->deprogbwlimit * 1000, peakbw * DEPROGBWPCLIMIT / 100);
508c64a9a7cSRadhakrishna Sripada 
509c64a9a7cSRadhakrishna Sripada 	ipqdepth = min(ipqdepthpch, sa->displayrtids / num_channels);
510c64a9a7cSRadhakrishna Sripada 	/*
511c64a9a7cSRadhakrishna Sripada 	 * clperchgroup = 4kpagespermempage * clperchperblock,
512c64a9a7cSRadhakrishna Sripada 	 * clperchperblock = 8 / num_channels * interleave
513c64a9a7cSRadhakrishna Sripada 	 */
514c64a9a7cSRadhakrishna Sripada 	clperchgroup = 4 * DIV_ROUND_UP(8, num_channels) * qi.deinterleave;
515c64a9a7cSRadhakrishna Sripada 
516c64a9a7cSRadhakrishna Sripada 	for (i = 0; i < num_groups; i++) {
517f0acaf9dSJani Nikula 		struct intel_bw_info *bi = &dev_priv->display.bw.max[i];
518c64a9a7cSRadhakrishna Sripada 		struct intel_bw_info *bi_next;
519c64a9a7cSRadhakrishna Sripada 		int clpchgroup;
520c64a9a7cSRadhakrishna Sripada 		int j;
521c64a9a7cSRadhakrishna Sripada 
522c64a9a7cSRadhakrishna Sripada 		clpchgroup = (sa->deburst * qi.deinterleave / num_channels) << i;
523c64a9a7cSRadhakrishna Sripada 
524c247cd03SŁukasz Bartosik 		if (i < num_groups - 1) {
525f0acaf9dSJani Nikula 			bi_next = &dev_priv->display.bw.max[i + 1];
526c247cd03SŁukasz Bartosik 
527c247cd03SŁukasz Bartosik 			if (clpchgroup < clperchgroup)
528c247cd03SŁukasz Bartosik 				bi_next->num_planes = (ipqdepth - clpchgroup) /
529c247cd03SŁukasz Bartosik 						       clpchgroup + 1;
530c64a9a7cSRadhakrishna Sripada 			else
531c64a9a7cSRadhakrishna Sripada 				bi_next->num_planes = 0;
532c247cd03SŁukasz Bartosik 		}
533c64a9a7cSRadhakrishna Sripada 
534c64a9a7cSRadhakrishna Sripada 		bi->num_qgv_points = qi.num_points;
535c64a9a7cSRadhakrishna Sripada 		bi->num_psf_gv_points = qi.num_psf_points;
536c64a9a7cSRadhakrishna Sripada 
537c64a9a7cSRadhakrishna Sripada 		for (j = 0; j < qi.num_points; j++) {
538c64a9a7cSRadhakrishna Sripada 			const struct intel_qgv_point *sp = &qi.points[j];
539c64a9a7cSRadhakrishna Sripada 			int ct, bw;
540c64a9a7cSRadhakrishna Sripada 
541c64a9a7cSRadhakrishna Sripada 			/*
542c64a9a7cSRadhakrishna Sripada 			 * Max row cycle time
543c64a9a7cSRadhakrishna Sripada 			 *
544c64a9a7cSRadhakrishna Sripada 			 * FIXME what is the logic behind the
545c64a9a7cSRadhakrishna Sripada 			 * assumed burst length?
546c64a9a7cSRadhakrishna Sripada 			 */
547c64a9a7cSRadhakrishna Sripada 			ct = max_t(int, sp->t_rc, sp->t_rp + sp->t_rcd +
548c64a9a7cSRadhakrishna Sripada 				   (clpchgroup - 1) * qi.t_bl + sp->t_rdpre);
549c64a9a7cSRadhakrishna Sripada 			bw = DIV_ROUND_UP(sp->dclk * clpchgroup * 32 * num_channels, ct);
550df0566a6SJani Nikula 
551df0566a6SJani Nikula 			bi->deratedbw[j] = min(maxdebw,
552f6d66fc8SRadhakrishna Sripada 					       bw * (100 - sa->derating) / 100);
55388d0ecbdSVinod Govindapillai 			bi->peakbw[j] = DIV_ROUND_CLOSEST(sp->dclk *
55488d0ecbdSVinod Govindapillai 							  num_channels *
55588d0ecbdSVinod Govindapillai 							  qi.channel_width, 8);
556df0566a6SJani Nikula 
5572e3586ceSWambui Karuga 			drm_dbg_kms(&dev_priv->drm,
55888d0ecbdSVinod Govindapillai 				    "BW%d / QGV %d: num_planes=%d deratedbw=%u peakbw: %u\n",
55988d0ecbdSVinod Govindapillai 				    i, j, bi->num_planes, bi->deratedbw[j],
56088d0ecbdSVinod Govindapillai 				    bi->peakbw[j]);
561df0566a6SJani Nikula 		}
562df0566a6SJani Nikula 
563192fbfb7SStanislav Lisovskiy 		for (j = 0; j < qi.num_psf_points; j++) {
564192fbfb7SStanislav Lisovskiy 			const struct intel_psf_gv_point *sp = &qi.psf_points[j];
565192fbfb7SStanislav Lisovskiy 
566192fbfb7SStanislav Lisovskiy 			bi->psf_bw[j] = adl_calc_psf_bw(sp->clk);
567192fbfb7SStanislav Lisovskiy 
568192fbfb7SStanislav Lisovskiy 			drm_dbg_kms(&dev_priv->drm,
569192fbfb7SStanislav Lisovskiy 				    "BW%d / PSF GV %d: num_planes=%d bw=%u\n",
570192fbfb7SStanislav Lisovskiy 				    i, j, bi->num_planes, bi->psf_bw[j]);
571192fbfb7SStanislav Lisovskiy 		}
572df0566a6SJani Nikula 	}
573df0566a6SJani Nikula 
57420f505f2SStanislav Lisovskiy 	/*
57520f505f2SStanislav Lisovskiy 	 * In case if SAGV is disabled in BIOS, we always get 1
57620f505f2SStanislav Lisovskiy 	 * SAGV point, but we can't send PCode commands to restrict it
57720f505f2SStanislav Lisovskiy 	 * as it will fail and pointless anyway.
57820f505f2SStanislav Lisovskiy 	 */
57920f505f2SStanislav Lisovskiy 	if (qi.num_points == 1)
580c3704f19SJani Nikula 		dev_priv->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
58120f505f2SStanislav Lisovskiy 	else
582c3704f19SJani Nikula 		dev_priv->display.sagv.status = I915_SAGV_ENABLED;
58320f505f2SStanislav Lisovskiy 
584df0566a6SJani Nikula 	return 0;
585df0566a6SJani Nikula }
586df0566a6SJani Nikula 
dg2_get_bw_info(struct drm_i915_private * i915)58734ba3c8aSMatt Roper static void dg2_get_bw_info(struct drm_i915_private *i915)
58834ba3c8aSMatt Roper {
589bc58192aSVinod Govindapillai 	unsigned int deratedbw = IS_DG2_G11(i915) ? 38000 : 50000;
590f0acaf9dSJani Nikula 	int num_groups = ARRAY_SIZE(i915->display.bw.max);
591bc58192aSVinod Govindapillai 	int i;
59234ba3c8aSMatt Roper 
59334ba3c8aSMatt Roper 	/*
59434ba3c8aSMatt Roper 	 * DG2 doesn't have SAGV or QGV points, just a constant max bandwidth
595bc58192aSVinod Govindapillai 	 * that doesn't depend on the number of planes enabled. So fill all the
596bc58192aSVinod Govindapillai 	 * plane group with constant bw information for uniformity with other
597bc58192aSVinod Govindapillai 	 * platforms. DG2-G10 platforms have a constant 50 GB/s bandwidth,
598bc58192aSVinod Govindapillai 	 * whereas DG2-G11 platforms have 38 GB/s.
59934ba3c8aSMatt Roper 	 */
600bc58192aSVinod Govindapillai 	for (i = 0; i < num_groups; i++) {
601f0acaf9dSJani Nikula 		struct intel_bw_info *bi = &i915->display.bw.max[i];
602bc58192aSVinod Govindapillai 
60334ba3c8aSMatt Roper 		bi->num_planes = 1;
604bc58192aSVinod Govindapillai 		/* Need only one dummy QGV point per group */
60534ba3c8aSMatt Roper 		bi->num_qgv_points = 1;
606bc58192aSVinod Govindapillai 		bi->deratedbw[0] = deratedbw;
607bc58192aSVinod Govindapillai 	}
60834ba3c8aSMatt Roper 
609c3704f19SJani Nikula 	i915->display.sagv.status = I915_SAGV_NOT_CONTROLLED;
61034ba3c8aSMatt Roper }
61134ba3c8aSMatt Roper 
xe2_hpd_get_bw_info(struct drm_i915_private * i915,const struct intel_sa_info * sa)612*772933b3SMatt Roper static int xe2_hpd_get_bw_info(struct drm_i915_private *i915,
613*772933b3SMatt Roper 			       const struct intel_sa_info *sa)
614*772933b3SMatt Roper {
615*772933b3SMatt Roper 	struct intel_qgv_info qi = {};
616*772933b3SMatt Roper 	int num_channels = i915->dram_info.num_channels;
617*772933b3SMatt Roper 	int peakbw, maxdebw;
618*772933b3SMatt Roper 	int ret, i;
619*772933b3SMatt Roper 
620*772933b3SMatt Roper 	ret = icl_get_qgv_points(i915, &qi, true);
621*772933b3SMatt Roper 	if (ret) {
622*772933b3SMatt Roper 		drm_dbg_kms(&i915->drm,
623*772933b3SMatt Roper 			    "Failed to get memory subsystem information, ignoring bandwidth limits");
624*772933b3SMatt Roper 		return ret;
625*772933b3SMatt Roper 	}
626*772933b3SMatt Roper 
627*772933b3SMatt Roper 	peakbw = num_channels * qi.channel_width / 8 * icl_sagv_max_dclk(&qi);
628*772933b3SMatt Roper 	maxdebw = min(sa->deprogbwlimit * 1000, peakbw * DEPROGBWPCLIMIT / 10);
629*772933b3SMatt Roper 
630*772933b3SMatt Roper 	for (i = 0; i < qi.num_points; i++) {
631*772933b3SMatt Roper 		const struct intel_qgv_point *point = &qi.points[i];
632*772933b3SMatt Roper 		int bw = num_channels * (qi.channel_width / 8) * point->dclk;
633*772933b3SMatt Roper 
634*772933b3SMatt Roper 		i915->display.bw.max[0].deratedbw[i] =
635*772933b3SMatt Roper 			min(maxdebw, (100 - sa->derating) * bw / 100);
636*772933b3SMatt Roper 		i915->display.bw.max[0].peakbw[i] = bw;
637*772933b3SMatt Roper 
638*772933b3SMatt Roper 		drm_dbg_kms(&i915->drm, "QGV %d: deratedbw=%u peakbw: %u\n",
639*772933b3SMatt Roper 			    i, i915->display.bw.max[0].deratedbw[i],
640*772933b3SMatt Roper 			    i915->display.bw.max[0].peakbw[i]);
641*772933b3SMatt Roper 	}
642*772933b3SMatt Roper 
643*772933b3SMatt Roper 	/* Bandwidth does not depend on # of planes; set all groups the same */
644*772933b3SMatt Roper 	i915->display.bw.max[0].num_planes = 1;
645*772933b3SMatt Roper 	i915->display.bw.max[0].num_qgv_points = qi.num_points;
646*772933b3SMatt Roper 	for (i = 1; i < ARRAY_SIZE(i915->display.bw.max); i++)
647*772933b3SMatt Roper 		memcpy(&i915->display.bw.max[i], &i915->display.bw.max[0],
648*772933b3SMatt Roper 		       sizeof(i915->display.bw.max[0]));
649*772933b3SMatt Roper 
650*772933b3SMatt Roper 	/*
651*772933b3SMatt Roper 	 * Xe2_HPD should always have exactly two QGV points representing
652*772933b3SMatt Roper 	 * battery and plugged-in operation.
653*772933b3SMatt Roper 	 */
654*772933b3SMatt Roper 	drm_WARN_ON(&i915->drm, qi.num_points != 2);
655*772933b3SMatt Roper 	i915->display.sagv.status = I915_SAGV_ENABLED;
656*772933b3SMatt Roper 
657*772933b3SMatt Roper 	return 0;
658*772933b3SMatt Roper }
659*772933b3SMatt Roper 
icl_max_bw_index(struct drm_i915_private * dev_priv,int num_planes,int qgv_point)6606400c215SVinod Govindapillai static unsigned int icl_max_bw_index(struct drm_i915_private *dev_priv,
661df0566a6SJani Nikula 				     int num_planes, int qgv_point)
662df0566a6SJani Nikula {
663df0566a6SJani Nikula 	int i;
664df0566a6SJani Nikula 
66520f505f2SStanislav Lisovskiy 	/*
66620f505f2SStanislav Lisovskiy 	 * Let's return max bw for 0 planes
66720f505f2SStanislav Lisovskiy 	 */
66820f505f2SStanislav Lisovskiy 	num_planes = max(1, num_planes);
66920f505f2SStanislav Lisovskiy 
670f0acaf9dSJani Nikula 	for (i = 0; i < ARRAY_SIZE(dev_priv->display.bw.max); i++) {
671df0566a6SJani Nikula 		const struct intel_bw_info *bi =
672f0acaf9dSJani Nikula 			&dev_priv->display.bw.max[i];
673df0566a6SJani Nikula 
67456e9371bSVille Syrjälä 		/*
67556e9371bSVille Syrjälä 		 * Pcode will not expose all QGV points when
67656e9371bSVille Syrjälä 		 * SAGV is forced to off/min/med/max.
67756e9371bSVille Syrjälä 		 */
67856e9371bSVille Syrjälä 		if (qgv_point >= bi->num_qgv_points)
67956e9371bSVille Syrjälä 			return UINT_MAX;
68056e9371bSVille Syrjälä 
681df0566a6SJani Nikula 		if (num_planes >= bi->num_planes)
6826400c215SVinod Govindapillai 			return i;
683df0566a6SJani Nikula 	}
684df0566a6SJani Nikula 
6856400c215SVinod Govindapillai 	return UINT_MAX;
686df0566a6SJani Nikula }
687df0566a6SJani Nikula 
tgl_max_bw_index(struct drm_i915_private * dev_priv,int num_planes,int qgv_point)6886400c215SVinod Govindapillai static unsigned int tgl_max_bw_index(struct drm_i915_private *dev_priv,
689c64a9a7cSRadhakrishna Sripada 				     int num_planes, int qgv_point)
690c64a9a7cSRadhakrishna Sripada {
691c64a9a7cSRadhakrishna Sripada 	int i;
692c64a9a7cSRadhakrishna Sripada 
693c64a9a7cSRadhakrishna Sripada 	/*
694c64a9a7cSRadhakrishna Sripada 	 * Let's return max bw for 0 planes
695c64a9a7cSRadhakrishna Sripada 	 */
696c64a9a7cSRadhakrishna Sripada 	num_planes = max(1, num_planes);
697c64a9a7cSRadhakrishna Sripada 
698f0acaf9dSJani Nikula 	for (i = ARRAY_SIZE(dev_priv->display.bw.max) - 1; i >= 0; i--) {
699c64a9a7cSRadhakrishna Sripada 		const struct intel_bw_info *bi =
700f0acaf9dSJani Nikula 			&dev_priv->display.bw.max[i];
701c64a9a7cSRadhakrishna Sripada 
702c64a9a7cSRadhakrishna Sripada 		/*
703c64a9a7cSRadhakrishna Sripada 		 * Pcode will not expose all QGV points when
704c64a9a7cSRadhakrishna Sripada 		 * SAGV is forced to off/min/med/max.
705c64a9a7cSRadhakrishna Sripada 		 */
706c64a9a7cSRadhakrishna Sripada 		if (qgv_point >= bi->num_qgv_points)
707c64a9a7cSRadhakrishna Sripada 			return UINT_MAX;
708c64a9a7cSRadhakrishna Sripada 
709c64a9a7cSRadhakrishna Sripada 		if (num_planes <= bi->num_planes)
7106400c215SVinod Govindapillai 			return i;
711c64a9a7cSRadhakrishna Sripada 	}
712c64a9a7cSRadhakrishna Sripada 
7136400c215SVinod Govindapillai 	return 0;
714c64a9a7cSRadhakrishna Sripada }
715c64a9a7cSRadhakrishna Sripada 
adl_psf_bw(struct drm_i915_private * dev_priv,int psf_gv_point)716192fbfb7SStanislav Lisovskiy static unsigned int adl_psf_bw(struct drm_i915_private *dev_priv,
717192fbfb7SStanislav Lisovskiy 			       int psf_gv_point)
718192fbfb7SStanislav Lisovskiy {
719192fbfb7SStanislav Lisovskiy 	const struct intel_bw_info *bi =
720f0acaf9dSJani Nikula 			&dev_priv->display.bw.max[0];
721192fbfb7SStanislav Lisovskiy 
722192fbfb7SStanislav Lisovskiy 	return bi->psf_bw[psf_gv_point];
723192fbfb7SStanislav Lisovskiy }
724192fbfb7SStanislav Lisovskiy 
icl_qgv_bw(struct drm_i915_private * i915,int num_active_planes,int qgv_point)725193caff1SStanislav Lisovskiy static unsigned int icl_qgv_bw(struct drm_i915_private *i915,
726193caff1SStanislav Lisovskiy 			       int num_active_planes, int qgv_point)
727193caff1SStanislav Lisovskiy {
728193caff1SStanislav Lisovskiy 	unsigned int idx;
729193caff1SStanislav Lisovskiy 
730193caff1SStanislav Lisovskiy 	if (DISPLAY_VER(i915) >= 12)
731193caff1SStanislav Lisovskiy 		idx = tgl_max_bw_index(i915, num_active_planes, qgv_point);
732193caff1SStanislav Lisovskiy 	else
733193caff1SStanislav Lisovskiy 		idx = icl_max_bw_index(i915, num_active_planes, qgv_point);
734193caff1SStanislav Lisovskiy 
735193caff1SStanislav Lisovskiy 	if (idx >= ARRAY_SIZE(i915->display.bw.max))
736193caff1SStanislav Lisovskiy 		return 0;
737193caff1SStanislav Lisovskiy 
738193caff1SStanislav Lisovskiy 	return i915->display.bw.max[idx].deratedbw[qgv_point];
739193caff1SStanislav Lisovskiy }
740193caff1SStanislav Lisovskiy 
intel_bw_init_hw(struct drm_i915_private * dev_priv)741df0566a6SJani Nikula void intel_bw_init_hw(struct drm_i915_private *dev_priv)
742df0566a6SJani Nikula {
7438a126392SStuart Summers 	if (!HAS_DISPLAY(dev_priv))
7448a126392SStuart Summers 		return;
7458a126392SStuart Summers 
746*772933b3SMatt Roper 	if (DISPLAY_VER_FULL(dev_priv) >= IP_VER(14, 1) && IS_DGFX(dev_priv))
747*772933b3SMatt Roper 		xe2_hpd_get_bw_info(dev_priv, &xe2_hpd_sa_info);
748*772933b3SMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 14)
7493eb4ad93SRadhakrishna Sripada 		tgl_get_bw_info(dev_priv, &mtl_sa_info);
7503eb4ad93SRadhakrishna Sripada 	else if (IS_DG2(dev_priv))
75134ba3c8aSMatt Roper 		dg2_get_bw_info(dev_priv);
752f6d66fc8SRadhakrishna Sripada 	else if (IS_ALDERLAKE_P(dev_priv))
753c64a9a7cSRadhakrishna Sripada 		tgl_get_bw_info(dev_priv, &adlp_sa_info);
754f6d66fc8SRadhakrishna Sripada 	else if (IS_ALDERLAKE_S(dev_priv))
755c64a9a7cSRadhakrishna Sripada 		tgl_get_bw_info(dev_priv, &adls_sa_info);
756918cc934STejas Upadhyay 	else if (IS_ROCKETLAKE(dev_priv))
757c64a9a7cSRadhakrishna Sripada 		tgl_get_bw_info(dev_priv, &rkl_sa_info);
75893e7e61eSLucas De Marchi 	else if (DISPLAY_VER(dev_priv) == 12)
759c64a9a7cSRadhakrishna Sripada 		tgl_get_bw_info(dev_priv, &tgl_sa_info);
76093e7e61eSLucas De Marchi 	else if (DISPLAY_VER(dev_priv) == 11)
7611b74d467SStanislav Lisovskiy 		icl_get_bw_info(dev_priv, &icl_sa_info);
762df0566a6SJani Nikula }
763df0566a6SJani Nikula 
intel_bw_crtc_num_active_planes(const struct intel_crtc_state * crtc_state)764df0566a6SJani Nikula static unsigned int intel_bw_crtc_num_active_planes(const struct intel_crtc_state *crtc_state)
765df0566a6SJani Nikula {
766df0566a6SJani Nikula 	/*
767df0566a6SJani Nikula 	 * We assume cursors are small enough
768df0566a6SJani Nikula 	 * to not not cause bandwidth problems.
769df0566a6SJani Nikula 	 */
770df0566a6SJani Nikula 	return hweight8(crtc_state->active_planes & ~BIT(PLANE_CURSOR));
771df0566a6SJani Nikula }
772df0566a6SJani Nikula 
intel_bw_crtc_data_rate(const struct intel_crtc_state * crtc_state)773df0566a6SJani Nikula static unsigned int intel_bw_crtc_data_rate(const struct intel_crtc_state *crtc_state)
774df0566a6SJani Nikula {
7752225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
776943ed3ccSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
777df0566a6SJani Nikula 	unsigned int data_rate = 0;
778df0566a6SJani Nikula 	enum plane_id plane_id;
779df0566a6SJani Nikula 
780df0566a6SJani Nikula 	for_each_plane_id_on_crtc(crtc, plane_id) {
781df0566a6SJani Nikula 		/*
782df0566a6SJani Nikula 		 * We assume cursors are small enough
783df0566a6SJani Nikula 		 * to not not cause bandwidth problems.
784df0566a6SJani Nikula 		 */
785df0566a6SJani Nikula 		if (plane_id == PLANE_CURSOR)
786df0566a6SJani Nikula 			continue;
787df0566a6SJani Nikula 
788df0566a6SJani Nikula 		data_rate += crtc_state->data_rate[plane_id];
789943ed3ccSVille Syrjälä 
790943ed3ccSVille Syrjälä 		if (DISPLAY_VER(i915) < 11)
791943ed3ccSVille Syrjälä 			data_rate += crtc_state->data_rate_y[plane_id];
792df0566a6SJani Nikula 	}
793df0566a6SJani Nikula 
794df0566a6SJani Nikula 	return data_rate;
795df0566a6SJani Nikula }
796cac91e67SStanislav Lisovskiy 
797ea083969SVille Syrjälä /* "Maximum Pipe Read Bandwidth" */
intel_bw_crtc_min_cdclk(const struct intel_crtc_state * crtc_state)798ea083969SVille Syrjälä static int intel_bw_crtc_min_cdclk(const struct intel_crtc_state *crtc_state)
799ea083969SVille Syrjälä {
800ea083969SVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
801ea083969SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
802ea083969SVille Syrjälä 
803ea083969SVille Syrjälä 	if (DISPLAY_VER(i915) < 12)
804ea083969SVille Syrjälä 		return 0;
805ea083969SVille Syrjälä 
806ea083969SVille Syrjälä 	return DIV_ROUND_UP_ULL(mul_u32_u32(intel_bw_crtc_data_rate(crtc_state), 10), 512);
807ea083969SVille Syrjälä }
808ea083969SVille Syrjälä 
intel_bw_crtc_update(struct intel_bw_state * bw_state,const struct intel_crtc_state * crtc_state)809df0566a6SJani Nikula void intel_bw_crtc_update(struct intel_bw_state *bw_state,
810df0566a6SJani Nikula 			  const struct intel_crtc_state *crtc_state)
811df0566a6SJani Nikula {
8122225f3c6SMaarten Lankhorst 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
813c3f81563SJani Nikula 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
814df0566a6SJani Nikula 
815df0566a6SJani Nikula 	bw_state->data_rate[crtc->pipe] =
816df0566a6SJani Nikula 		intel_bw_crtc_data_rate(crtc_state);
817df0566a6SJani Nikula 	bw_state->num_active_planes[crtc->pipe] =
818df0566a6SJani Nikula 		intel_bw_crtc_num_active_planes(crtc_state);
819aaba7a95SVinod Govindapillai 	bw_state->force_check_qgv = true;
820df0566a6SJani Nikula 
821c3f81563SJani Nikula 	drm_dbg_kms(&i915->drm, "pipe %c data rate %u num active planes %u\n",
822df0566a6SJani Nikula 		    pipe_name(crtc->pipe),
823df0566a6SJani Nikula 		    bw_state->data_rate[crtc->pipe],
824df0566a6SJani Nikula 		    bw_state->num_active_planes[crtc->pipe]);
825df0566a6SJani Nikula }
826df0566a6SJani Nikula 
intel_bw_num_active_planes(struct drm_i915_private * dev_priv,const struct intel_bw_state * bw_state)827df0566a6SJani Nikula static unsigned int intel_bw_num_active_planes(struct drm_i915_private *dev_priv,
828df0566a6SJani Nikula 					       const struct intel_bw_state *bw_state)
829df0566a6SJani Nikula {
830df0566a6SJani Nikula 	unsigned int num_active_planes = 0;
831df0566a6SJani Nikula 	enum pipe pipe;
832df0566a6SJani Nikula 
833df0566a6SJani Nikula 	for_each_pipe(dev_priv, pipe)
834df0566a6SJani Nikula 		num_active_planes += bw_state->num_active_planes[pipe];
835df0566a6SJani Nikula 
836df0566a6SJani Nikula 	return num_active_planes;
837df0566a6SJani Nikula }
838df0566a6SJani Nikula 
intel_bw_data_rate(struct drm_i915_private * dev_priv,const struct intel_bw_state * bw_state)839df0566a6SJani Nikula static unsigned int intel_bw_data_rate(struct drm_i915_private *dev_priv,
840df0566a6SJani Nikula 				       const struct intel_bw_state *bw_state)
841df0566a6SJani Nikula {
842df0566a6SJani Nikula 	unsigned int data_rate = 0;
843df0566a6SJani Nikula 	enum pipe pipe;
844df0566a6SJani Nikula 
845df0566a6SJani Nikula 	for_each_pipe(dev_priv, pipe)
846df0566a6SJani Nikula 		data_rate += bw_state->data_rate[pipe];
847df0566a6SJani Nikula 
848a7f46d5bSTvrtko Ursulin 	if (DISPLAY_VER(dev_priv) >= 13 && i915_vtd_active(dev_priv))
8496c69d0bbSVille Syrjälä 		data_rate = DIV_ROUND_UP(data_rate * 105, 100);
8500788abdeSMatt Roper 
851df0566a6SJani Nikula 	return data_rate;
852df0566a6SJani Nikula }
853df0566a6SJani Nikula 
854442e7ee8SStanislav Lisovskiy struct intel_bw_state *
intel_atomic_get_old_bw_state(struct intel_atomic_state * state)855442e7ee8SStanislav Lisovskiy intel_atomic_get_old_bw_state(struct intel_atomic_state *state)
856442e7ee8SStanislav Lisovskiy {
857442e7ee8SStanislav Lisovskiy 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
858442e7ee8SStanislav Lisovskiy 	struct intel_global_state *bw_state;
859442e7ee8SStanislav Lisovskiy 
860f0acaf9dSJani Nikula 	bw_state = intel_atomic_get_old_global_obj_state(state, &dev_priv->display.bw.obj);
861442e7ee8SStanislav Lisovskiy 
862442e7ee8SStanislav Lisovskiy 	return to_intel_bw_state(bw_state);
863442e7ee8SStanislav Lisovskiy }
864442e7ee8SStanislav Lisovskiy 
865442e7ee8SStanislav Lisovskiy struct intel_bw_state *
intel_atomic_get_new_bw_state(struct intel_atomic_state * state)866442e7ee8SStanislav Lisovskiy intel_atomic_get_new_bw_state(struct intel_atomic_state *state)
867442e7ee8SStanislav Lisovskiy {
868442e7ee8SStanislav Lisovskiy 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
869442e7ee8SStanislav Lisovskiy 	struct intel_global_state *bw_state;
870442e7ee8SStanislav Lisovskiy 
871f0acaf9dSJani Nikula 	bw_state = intel_atomic_get_new_global_obj_state(state, &dev_priv->display.bw.obj);
872442e7ee8SStanislav Lisovskiy 
873442e7ee8SStanislav Lisovskiy 	return to_intel_bw_state(bw_state);
874442e7ee8SStanislav Lisovskiy }
875442e7ee8SStanislav Lisovskiy 
876442e7ee8SStanislav Lisovskiy struct intel_bw_state *
intel_atomic_get_bw_state(struct intel_atomic_state * state)877366b6200SJani Nikula intel_atomic_get_bw_state(struct intel_atomic_state *state)
878366b6200SJani Nikula {
879366b6200SJani Nikula 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
880fd1a9bbaSVille Syrjälä 	struct intel_global_state *bw_state;
881366b6200SJani Nikula 
882f0acaf9dSJani Nikula 	bw_state = intel_atomic_get_global_obj_state(state, &dev_priv->display.bw.obj);
883366b6200SJani Nikula 	if (IS_ERR(bw_state))
884366b6200SJani Nikula 		return ERR_CAST(bw_state);
885366b6200SJani Nikula 
886366b6200SJani Nikula 	return to_intel_bw_state(bw_state);
887366b6200SJani Nikula }
888366b6200SJani Nikula 
icl_max_bw_qgv_point_mask(struct drm_i915_private * i915,int num_active_planes)889193caff1SStanislav Lisovskiy static unsigned int icl_max_bw_qgv_point_mask(struct drm_i915_private *i915,
890193caff1SStanislav Lisovskiy 					      int num_active_planes)
891193caff1SStanislav Lisovskiy {
892193caff1SStanislav Lisovskiy 	unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
893193caff1SStanislav Lisovskiy 	unsigned int max_bw_point = 0;
894193caff1SStanislav Lisovskiy 	unsigned int max_bw = 0;
895193caff1SStanislav Lisovskiy 	int i;
896193caff1SStanislav Lisovskiy 
897193caff1SStanislav Lisovskiy 	for (i = 0; i < num_qgv_points; i++) {
898193caff1SStanislav Lisovskiy 		unsigned int max_data_rate =
899193caff1SStanislav Lisovskiy 			icl_qgv_bw(i915, num_active_planes, i);
900193caff1SStanislav Lisovskiy 
901193caff1SStanislav Lisovskiy 		/*
902193caff1SStanislav Lisovskiy 		 * We need to know which qgv point gives us
903193caff1SStanislav Lisovskiy 		 * maximum bandwidth in order to disable SAGV
904193caff1SStanislav Lisovskiy 		 * if we find that we exceed SAGV block time
905193caff1SStanislav Lisovskiy 		 * with watermarks. By that moment we already
906193caff1SStanislav Lisovskiy 		 * have those, as it is calculated earlier in
907193caff1SStanislav Lisovskiy 		 * intel_atomic_check,
908193caff1SStanislav Lisovskiy 		 */
909193caff1SStanislav Lisovskiy 		if (max_data_rate > max_bw) {
910193caff1SStanislav Lisovskiy 			max_bw_point = BIT(i);
911193caff1SStanislav Lisovskiy 			max_bw = max_data_rate;
912193caff1SStanislav Lisovskiy 		}
913193caff1SStanislav Lisovskiy 	}
914193caff1SStanislav Lisovskiy 
915193caff1SStanislav Lisovskiy 	return max_bw_point;
916193caff1SStanislav Lisovskiy }
917193caff1SStanislav Lisovskiy 
icl_prepare_qgv_points_mask(struct drm_i915_private * i915,unsigned int qgv_points,unsigned int psf_points)918f09f9517SVinod Govindapillai static u16 icl_prepare_qgv_points_mask(struct drm_i915_private *i915,
919f09f9517SVinod Govindapillai 				       unsigned int qgv_points,
920f09f9517SVinod Govindapillai 				       unsigned int psf_points)
921f09f9517SVinod Govindapillai {
922f09f9517SVinod Govindapillai 	return ~(ICL_PCODE_REQ_QGV_PT(qgv_points) |
923f09f9517SVinod Govindapillai 		 ADLS_PCODE_REQ_PSF_PT(psf_points)) & icl_qgv_points_mask(i915);
924f09f9517SVinod Govindapillai }
925f09f9517SVinod Govindapillai 
icl_max_bw_psf_gv_point_mask(struct drm_i915_private * i915)9269299cde9SStanislav Lisovskiy static unsigned int icl_max_bw_psf_gv_point_mask(struct drm_i915_private *i915)
9279299cde9SStanislav Lisovskiy {
9289299cde9SStanislav Lisovskiy 	unsigned int num_psf_gv_points = i915->display.bw.max[0].num_psf_gv_points;
9299299cde9SStanislav Lisovskiy 	unsigned int max_bw_point_mask = 0;
9309299cde9SStanislav Lisovskiy 	unsigned int max_bw = 0;
9319299cde9SStanislav Lisovskiy 	int i;
9329299cde9SStanislav Lisovskiy 
9339299cde9SStanislav Lisovskiy 	for (i = 0; i < num_psf_gv_points; i++) {
9349299cde9SStanislav Lisovskiy 		unsigned int max_data_rate = adl_psf_bw(i915, i);
9359299cde9SStanislav Lisovskiy 
9369299cde9SStanislav Lisovskiy 		if (max_data_rate > max_bw) {
9379299cde9SStanislav Lisovskiy 			max_bw_point_mask = BIT(i);
9389299cde9SStanislav Lisovskiy 			max_bw = max_data_rate;
9391e9e4be8SStanislav Lisovskiy 		} else if (max_data_rate == max_bw) {
9401e9e4be8SStanislav Lisovskiy 			max_bw_point_mask |= BIT(i);
9419299cde9SStanislav Lisovskiy 		}
9429299cde9SStanislav Lisovskiy 	}
9439299cde9SStanislav Lisovskiy 
9449299cde9SStanislav Lisovskiy 	return max_bw_point_mask;
9459299cde9SStanislav Lisovskiy }
9469299cde9SStanislav Lisovskiy 
icl_force_disable_sagv(struct drm_i915_private * i915,struct intel_bw_state * bw_state)9479299cde9SStanislav Lisovskiy static void icl_force_disable_sagv(struct drm_i915_private *i915,
9489299cde9SStanislav Lisovskiy 				   struct intel_bw_state *bw_state)
9499299cde9SStanislav Lisovskiy {
9509299cde9SStanislav Lisovskiy 	unsigned int qgv_points = icl_max_bw_qgv_point_mask(i915, 0);
9519299cde9SStanislav Lisovskiy 	unsigned int psf_points = icl_max_bw_psf_gv_point_mask(i915);
9529299cde9SStanislav Lisovskiy 
9539299cde9SStanislav Lisovskiy 	bw_state->qgv_points_mask = icl_prepare_qgv_points_mask(i915,
9549299cde9SStanislav Lisovskiy 								qgv_points,
9559299cde9SStanislav Lisovskiy 								psf_points);
9569299cde9SStanislav Lisovskiy 
9579299cde9SStanislav Lisovskiy 	drm_dbg_kms(&i915->drm, "Forcing SAGV disable: mask 0x%x\n",
9589299cde9SStanislav Lisovskiy 		    bw_state->qgv_points_mask);
9599299cde9SStanislav Lisovskiy 
9609299cde9SStanislav Lisovskiy 	icl_pcode_restrict_qgv_points(i915, bw_state->qgv_points_mask);
9619299cde9SStanislav Lisovskiy }
9629299cde9SStanislav Lisovskiy 
mtl_find_qgv_points(struct drm_i915_private * i915,unsigned int data_rate,unsigned int num_active_planes,struct intel_bw_state * new_bw_state)963a5819e51SVinod Govindapillai static int mtl_find_qgv_points(struct drm_i915_private *i915,
964a5819e51SVinod Govindapillai 			       unsigned int data_rate,
965a5819e51SVinod Govindapillai 			       unsigned int num_active_planes,
966a5819e51SVinod Govindapillai 			       struct intel_bw_state *new_bw_state)
967a5819e51SVinod Govindapillai {
968a5819e51SVinod Govindapillai 	unsigned int best_rate = UINT_MAX;
969a5819e51SVinod Govindapillai 	unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
970a5819e51SVinod Govindapillai 	unsigned int qgv_peak_bw  = 0;
971a5819e51SVinod Govindapillai 	int i;
972a5819e51SVinod Govindapillai 	int ret;
973a5819e51SVinod Govindapillai 
974a5819e51SVinod Govindapillai 	ret = intel_atomic_lock_global_state(&new_bw_state->base);
975a5819e51SVinod Govindapillai 	if (ret)
976a5819e51SVinod Govindapillai 		return ret;
977a5819e51SVinod Govindapillai 
978a5819e51SVinod Govindapillai 	/*
979a5819e51SVinod Govindapillai 	 * If SAGV cannot be enabled, disable the pcode SAGV by passing all 1's
980a5819e51SVinod Govindapillai 	 * for qgv peak bw in PM Demand request. So assign UINT_MAX if SAGV is
981a5819e51SVinod Govindapillai 	 * not enabled. PM Demand code will clamp the value for the register
982a5819e51SVinod Govindapillai 	 */
983a5819e51SVinod Govindapillai 	if (!intel_can_enable_sagv(i915, new_bw_state)) {
984a5819e51SVinod Govindapillai 		new_bw_state->qgv_point_peakbw = U16_MAX;
985a5819e51SVinod Govindapillai 		drm_dbg_kms(&i915->drm, "No SAGV, use UINT_MAX as peak bw.");
986a5819e51SVinod Govindapillai 		return 0;
987a5819e51SVinod Govindapillai 	}
988a5819e51SVinod Govindapillai 
989a5819e51SVinod Govindapillai 	/*
990a5819e51SVinod Govindapillai 	 * Find the best QGV point by comparing the data_rate with max data rate
991a5819e51SVinod Govindapillai 	 * offered per plane group
992a5819e51SVinod Govindapillai 	 */
993a5819e51SVinod Govindapillai 	for (i = 0; i < num_qgv_points; i++) {
994a5819e51SVinod Govindapillai 		unsigned int bw_index =
995a5819e51SVinod Govindapillai 			tgl_max_bw_index(i915, num_active_planes, i);
996a5819e51SVinod Govindapillai 		unsigned int max_data_rate;
997a5819e51SVinod Govindapillai 
998a5819e51SVinod Govindapillai 		if (bw_index >= ARRAY_SIZE(i915->display.bw.max))
999a5819e51SVinod Govindapillai 			continue;
1000a5819e51SVinod Govindapillai 
1001a5819e51SVinod Govindapillai 		max_data_rate = i915->display.bw.max[bw_index].deratedbw[i];
1002a5819e51SVinod Govindapillai 
1003a5819e51SVinod Govindapillai 		if (max_data_rate < data_rate)
1004a5819e51SVinod Govindapillai 			continue;
1005a5819e51SVinod Govindapillai 
1006a5819e51SVinod Govindapillai 		if (max_data_rate - data_rate < best_rate) {
1007a5819e51SVinod Govindapillai 			best_rate = max_data_rate - data_rate;
1008a5819e51SVinod Govindapillai 			qgv_peak_bw = i915->display.bw.max[bw_index].peakbw[i];
1009a5819e51SVinod Govindapillai 		}
1010a5819e51SVinod Govindapillai 
1011a5819e51SVinod Govindapillai 		drm_dbg_kms(&i915->drm, "QGV point %d: max bw %d required %d qgv_peak_bw: %d\n",
1012a5819e51SVinod Govindapillai 			    i, max_data_rate, data_rate, qgv_peak_bw);
1013a5819e51SVinod Govindapillai 	}
1014a5819e51SVinod Govindapillai 
1015a5819e51SVinod Govindapillai 	drm_dbg_kms(&i915->drm, "Matching peaks QGV bw: %d for required data rate: %d\n",
1016a5819e51SVinod Govindapillai 		    qgv_peak_bw, data_rate);
1017a5819e51SVinod Govindapillai 
1018a5819e51SVinod Govindapillai 	/*
1019a5819e51SVinod Govindapillai 	 * The display configuration cannot be supported if no QGV point
1020a5819e51SVinod Govindapillai 	 * satisfying the required data rate is found
1021a5819e51SVinod Govindapillai 	 */
1022a5819e51SVinod Govindapillai 	if (qgv_peak_bw == 0) {
1023a5819e51SVinod Govindapillai 		drm_dbg_kms(&i915->drm, "No QGV points for bw %d for display configuration(%d active planes).\n",
1024a5819e51SVinod Govindapillai 			    data_rate, num_active_planes);
1025a5819e51SVinod Govindapillai 		return -EINVAL;
1026a5819e51SVinod Govindapillai 	}
1027a5819e51SVinod Govindapillai 
1028a5819e51SVinod Govindapillai 	/* MTL PM DEMAND expects QGV BW parameter in multiples of 100 mbps */
1029a5819e51SVinod Govindapillai 	new_bw_state->qgv_point_peakbw = DIV_ROUND_CLOSEST(qgv_peak_bw, 100);
1030a5819e51SVinod Govindapillai 
1031a5819e51SVinod Govindapillai 	return 0;
1032a5819e51SVinod Govindapillai }
1033a5819e51SVinod Govindapillai 
icl_find_qgv_points(struct drm_i915_private * i915,unsigned int data_rate,unsigned int num_active_planes,const struct intel_bw_state * old_bw_state,struct intel_bw_state * new_bw_state)1034463cc940SVinod Govindapillai static int icl_find_qgv_points(struct drm_i915_private *i915,
1035463cc940SVinod Govindapillai 			       unsigned int data_rate,
1036463cc940SVinod Govindapillai 			       unsigned int num_active_planes,
1037463cc940SVinod Govindapillai 			       const struct intel_bw_state *old_bw_state,
1038463cc940SVinod Govindapillai 			       struct intel_bw_state *new_bw_state)
1039463cc940SVinod Govindapillai {
1040463cc940SVinod Govindapillai 	unsigned int num_psf_gv_points = i915->display.bw.max[0].num_psf_gv_points;
1041463cc940SVinod Govindapillai 	unsigned int num_qgv_points = i915->display.bw.max[0].num_qgv_points;
1042463cc940SVinod Govindapillai 	u16 psf_points = 0;
1043463cc940SVinod Govindapillai 	u16 qgv_points = 0;
1044463cc940SVinod Govindapillai 	int i;
1045463cc940SVinod Govindapillai 	int ret;
1046463cc940SVinod Govindapillai 
1047463cc940SVinod Govindapillai 	ret = intel_atomic_lock_global_state(&new_bw_state->base);
1048463cc940SVinod Govindapillai 	if (ret)
1049463cc940SVinod Govindapillai 		return ret;
1050463cc940SVinod Govindapillai 
1051463cc940SVinod Govindapillai 	for (i = 0; i < num_qgv_points; i++) {
1052193caff1SStanislav Lisovskiy 		unsigned int max_data_rate = icl_qgv_bw(i915,
1053193caff1SStanislav Lisovskiy 							num_active_planes, i);
1054463cc940SVinod Govindapillai 		if (max_data_rate >= data_rate)
1055463cc940SVinod Govindapillai 			qgv_points |= BIT(i);
1056463cc940SVinod Govindapillai 
1057463cc940SVinod Govindapillai 		drm_dbg_kms(&i915->drm, "QGV point %d: max bw %d required %d\n",
1058463cc940SVinod Govindapillai 			    i, max_data_rate, data_rate);
1059463cc940SVinod Govindapillai 	}
1060463cc940SVinod Govindapillai 
1061463cc940SVinod Govindapillai 	for (i = 0; i < num_psf_gv_points; i++) {
1062463cc940SVinod Govindapillai 		unsigned int max_data_rate = adl_psf_bw(i915, i);
1063463cc940SVinod Govindapillai 
1064463cc940SVinod Govindapillai 		if (max_data_rate >= data_rate)
1065463cc940SVinod Govindapillai 			psf_points |= BIT(i);
1066463cc940SVinod Govindapillai 
1067463cc940SVinod Govindapillai 		drm_dbg_kms(&i915->drm, "PSF GV point %d: max bw %d"
1068463cc940SVinod Govindapillai 			    " required %d\n",
1069463cc940SVinod Govindapillai 			    i, max_data_rate, data_rate);
1070463cc940SVinod Govindapillai 	}
1071463cc940SVinod Govindapillai 
1072463cc940SVinod Govindapillai 	/*
1073463cc940SVinod Govindapillai 	 * BSpec states that we always should have at least one allowed point
1074463cc940SVinod Govindapillai 	 * left, so if we couldn't - simply reject the configuration for obvious
1075463cc940SVinod Govindapillai 	 * reasons.
1076463cc940SVinod Govindapillai 	 */
1077463cc940SVinod Govindapillai 	if (qgv_points == 0) {
1078463cc940SVinod Govindapillai 		drm_dbg_kms(&i915->drm, "No QGV points provide sufficient memory"
1079463cc940SVinod Govindapillai 			    " bandwidth %d for display configuration(%d active planes).\n",
1080463cc940SVinod Govindapillai 			    data_rate, num_active_planes);
1081463cc940SVinod Govindapillai 		return -EINVAL;
1082463cc940SVinod Govindapillai 	}
1083463cc940SVinod Govindapillai 
1084463cc940SVinod Govindapillai 	if (num_psf_gv_points > 0 && psf_points == 0) {
1085463cc940SVinod Govindapillai 		drm_dbg_kms(&i915->drm, "No PSF GV points provide sufficient memory"
1086463cc940SVinod Govindapillai 			    " bandwidth %d for display configuration(%d active planes).\n",
1087463cc940SVinod Govindapillai 			    data_rate, num_active_planes);
1088463cc940SVinod Govindapillai 		return -EINVAL;
1089463cc940SVinod Govindapillai 	}
1090463cc940SVinod Govindapillai 
1091463cc940SVinod Govindapillai 	/*
1092463cc940SVinod Govindapillai 	 * Leave only single point with highest bandwidth, if
1093463cc940SVinod Govindapillai 	 * we can't enable SAGV due to the increased memory latency it may
1094463cc940SVinod Govindapillai 	 * cause.
1095463cc940SVinod Govindapillai 	 */
1096463cc940SVinod Govindapillai 	if (!intel_can_enable_sagv(i915, new_bw_state)) {
1097193caff1SStanislav Lisovskiy 		qgv_points = icl_max_bw_qgv_point_mask(i915, num_active_planes);
1098193caff1SStanislav Lisovskiy 		drm_dbg_kms(&i915->drm, "No SAGV, using single QGV point mask 0x%x\n",
1099193caff1SStanislav Lisovskiy 			    qgv_points);
1100463cc940SVinod Govindapillai 	}
1101463cc940SVinod Govindapillai 
1102463cc940SVinod Govindapillai 	/*
1103463cc940SVinod Govindapillai 	 * We store the ones which need to be masked as that is what PCode
1104463cc940SVinod Govindapillai 	 * actually accepts as a parameter.
1105463cc940SVinod Govindapillai 	 */
1106f09f9517SVinod Govindapillai 	new_bw_state->qgv_points_mask = icl_prepare_qgv_points_mask(i915,
1107f09f9517SVinod Govindapillai 								    qgv_points,
1108f09f9517SVinod Govindapillai 								    psf_points);
1109463cc940SVinod Govindapillai 	/*
1110463cc940SVinod Govindapillai 	 * If the actual mask had changed we need to make sure that
1111463cc940SVinod Govindapillai 	 * the commits are serialized(in case this is a nomodeset, nonblocking)
1112463cc940SVinod Govindapillai 	 */
1113463cc940SVinod Govindapillai 	if (new_bw_state->qgv_points_mask != old_bw_state->qgv_points_mask) {
1114463cc940SVinod Govindapillai 		ret = intel_atomic_serialize_global_state(&new_bw_state->base);
1115463cc940SVinod Govindapillai 		if (ret)
1116463cc940SVinod Govindapillai 			return ret;
1117463cc940SVinod Govindapillai 	}
1118463cc940SVinod Govindapillai 
1119463cc940SVinod Govindapillai 	return 0;
1120463cc940SVinod Govindapillai }
1121463cc940SVinod Govindapillai 
intel_bw_check_qgv_points(struct drm_i915_private * i915,const struct intel_bw_state * old_bw_state,struct intel_bw_state * new_bw_state)1122463cc940SVinod Govindapillai static int intel_bw_check_qgv_points(struct drm_i915_private *i915,
1123463cc940SVinod Govindapillai 				     const struct intel_bw_state *old_bw_state,
1124463cc940SVinod Govindapillai 				     struct intel_bw_state *new_bw_state)
1125463cc940SVinod Govindapillai {
1126463cc940SVinod Govindapillai 	unsigned int data_rate = intel_bw_data_rate(i915, new_bw_state);
1127463cc940SVinod Govindapillai 	unsigned int num_active_planes =
1128463cc940SVinod Govindapillai 			intel_bw_num_active_planes(i915, new_bw_state);
1129463cc940SVinod Govindapillai 
1130463cc940SVinod Govindapillai 	data_rate = DIV_ROUND_UP(data_rate, 1000);
1131463cc940SVinod Govindapillai 
1132a5819e51SVinod Govindapillai 	if (DISPLAY_VER(i915) >= 14)
1133a5819e51SVinod Govindapillai 		return mtl_find_qgv_points(i915, data_rate, num_active_planes,
1134a5819e51SVinod Govindapillai 					   new_bw_state);
1135a5819e51SVinod Govindapillai 	else
1136463cc940SVinod Govindapillai 		return icl_find_qgv_points(i915, data_rate, num_active_planes,
1137463cc940SVinod Govindapillai 					   old_bw_state, new_bw_state);
1138463cc940SVinod Govindapillai }
1139463cc940SVinod Govindapillai 
intel_bw_state_changed(struct drm_i915_private * i915,const struct intel_bw_state * old_bw_state,const struct intel_bw_state * new_bw_state)11406731eb04SVille Syrjälä static bool intel_bw_state_changed(struct drm_i915_private *i915,
11416731eb04SVille Syrjälä 				   const struct intel_bw_state *old_bw_state,
11426731eb04SVille Syrjälä 				   const struct intel_bw_state *new_bw_state)
11436731eb04SVille Syrjälä {
11446731eb04SVille Syrjälä 	enum pipe pipe;
11456731eb04SVille Syrjälä 
11466731eb04SVille Syrjälä 	for_each_pipe(i915, pipe) {
11476731eb04SVille Syrjälä 		const struct intel_dbuf_bw *old_crtc_bw =
11486731eb04SVille Syrjälä 			&old_bw_state->dbuf_bw[pipe];
11496731eb04SVille Syrjälä 		const struct intel_dbuf_bw *new_crtc_bw =
11506731eb04SVille Syrjälä 			&new_bw_state->dbuf_bw[pipe];
11516731eb04SVille Syrjälä 		enum dbuf_slice slice;
11526731eb04SVille Syrjälä 
11536731eb04SVille Syrjälä 		for_each_dbuf_slice(i915, slice) {
11545ac860ccSVille Syrjälä 			if (old_crtc_bw->max_bw[slice] != new_crtc_bw->max_bw[slice] ||
11555ac860ccSVille Syrjälä 			    old_crtc_bw->active_planes[slice] != new_crtc_bw->active_planes[slice])
11566731eb04SVille Syrjälä 				return true;
11576731eb04SVille Syrjälä 		}
1158ea083969SVille Syrjälä 
1159ea083969SVille Syrjälä 		if (old_bw_state->min_cdclk[pipe] != new_bw_state->min_cdclk[pipe])
1160ea083969SVille Syrjälä 			return true;
11616731eb04SVille Syrjälä 	}
11626731eb04SVille Syrjälä 
11635ac860ccSVille Syrjälä 	return false;
11645ac860ccSVille Syrjälä }
11655ac860ccSVille Syrjälä 
skl_plane_calc_dbuf_bw(struct intel_bw_state * bw_state,struct intel_crtc * crtc,enum plane_id plane_id,const struct skl_ddb_entry * ddb,unsigned int data_rate)11665ac860ccSVille Syrjälä static void skl_plane_calc_dbuf_bw(struct intel_bw_state *bw_state,
11675ac860ccSVille Syrjälä 				   struct intel_crtc *crtc,
11685ac860ccSVille Syrjälä 				   enum plane_id plane_id,
11695ac860ccSVille Syrjälä 				   const struct skl_ddb_entry *ddb,
11705ac860ccSVille Syrjälä 				   unsigned int data_rate)
11715ac860ccSVille Syrjälä {
11725ac860ccSVille Syrjälä 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
11735ac860ccSVille Syrjälä 	struct intel_dbuf_bw *crtc_bw = &bw_state->dbuf_bw[crtc->pipe];
11745ac860ccSVille Syrjälä 	unsigned int dbuf_mask = skl_ddb_dbuf_slice_mask(i915, ddb);
11755ac860ccSVille Syrjälä 	enum dbuf_slice slice;
11765ac860ccSVille Syrjälä 
11775ac860ccSVille Syrjälä 	/*
11785ac860ccSVille Syrjälä 	 * The arbiter can only really guarantee an
11795ac860ccSVille Syrjälä 	 * equal share of the total bw to each plane.
11805ac860ccSVille Syrjälä 	 */
11815ac860ccSVille Syrjälä 	for_each_dbuf_slice_in_mask(i915, slice, dbuf_mask) {
11825ac860ccSVille Syrjälä 		crtc_bw->max_bw[slice] = max(crtc_bw->max_bw[slice], data_rate);
11835ac860ccSVille Syrjälä 		crtc_bw->active_planes[slice] |= BIT(plane_id);
11845ac860ccSVille Syrjälä 	}
11856731eb04SVille Syrjälä }
11866731eb04SVille Syrjälä 
skl_crtc_calc_dbuf_bw(struct intel_bw_state * bw_state,const struct intel_crtc_state * crtc_state)1187cad3fab4SVille Syrjälä static void skl_crtc_calc_dbuf_bw(struct intel_bw_state *bw_state,
1188cad3fab4SVille Syrjälä 				  const struct intel_crtc_state *crtc_state)
1189cad3fab4SVille Syrjälä {
1190cad3fab4SVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1191cad3fab4SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1192cad3fab4SVille Syrjälä 	struct intel_dbuf_bw *crtc_bw = &bw_state->dbuf_bw[crtc->pipe];
1193cad3fab4SVille Syrjälä 	enum plane_id plane_id;
1194cad3fab4SVille Syrjälä 
11955ac860ccSVille Syrjälä 	memset(crtc_bw, 0, sizeof(*crtc_bw));
1196cad3fab4SVille Syrjälä 
1197cad3fab4SVille Syrjälä 	if (!crtc_state->hw.active)
1198cad3fab4SVille Syrjälä 		return;
1199cad3fab4SVille Syrjälä 
1200cad3fab4SVille Syrjälä 	for_each_plane_id_on_crtc(crtc, plane_id) {
12015ac860ccSVille Syrjälä 		/*
12025ac860ccSVille Syrjälä 		 * We assume cursors are small enough
12035ac860ccSVille Syrjälä 		 * to not cause bandwidth problems.
12045ac860ccSVille Syrjälä 		 */
12055ac860ccSVille Syrjälä 		if (plane_id == PLANE_CURSOR)
12065ac860ccSVille Syrjälä 			continue;
12075ac860ccSVille Syrjälä 
12085ac860ccSVille Syrjälä 		skl_plane_calc_dbuf_bw(bw_state, crtc, plane_id,
12095ac860ccSVille Syrjälä 				       &crtc_state->wm.skl.plane_ddb[plane_id],
12105ac860ccSVille Syrjälä 				       crtc_state->data_rate[plane_id]);
12115ac860ccSVille Syrjälä 
12125ac860ccSVille Syrjälä 		if (DISPLAY_VER(i915) < 11)
12135ac860ccSVille Syrjälä 			skl_plane_calc_dbuf_bw(bw_state, crtc, plane_id,
12145ac860ccSVille Syrjälä 					       &crtc_state->wm.skl.plane_ddb_y[plane_id],
12155ac860ccSVille Syrjälä 					       crtc_state->data_rate[plane_id]);
12165ac860ccSVille Syrjälä 	}
12175ac860ccSVille Syrjälä }
12185ac860ccSVille Syrjälä 
12195ac860ccSVille Syrjälä /* "Maximum Data Buffer Bandwidth" */
12205ac860ccSVille Syrjälä static int
intel_bw_dbuf_min_cdclk(struct drm_i915_private * i915,const struct intel_bw_state * bw_state)12215ac860ccSVille Syrjälä intel_bw_dbuf_min_cdclk(struct drm_i915_private *i915,
12225ac860ccSVille Syrjälä 			const struct intel_bw_state *bw_state)
12235ac860ccSVille Syrjälä {
12245ac860ccSVille Syrjälä 	unsigned int total_max_bw = 0;
1225cad3fab4SVille Syrjälä 	enum dbuf_slice slice;
1226cad3fab4SVille Syrjälä 
12275ac860ccSVille Syrjälä 	for_each_dbuf_slice(i915, slice) {
12285ac860ccSVille Syrjälä 		int num_active_planes = 0;
12295ac860ccSVille Syrjälä 		unsigned int max_bw = 0;
12305ac860ccSVille Syrjälä 		enum pipe pipe;
12315ac860ccSVille Syrjälä 
12325ac860ccSVille Syrjälä 		/*
12335ac860ccSVille Syrjälä 		 * The arbiter can only really guarantee an
12345ac860ccSVille Syrjälä 		 * equal share of the total bw to each plane.
12355ac860ccSVille Syrjälä 		 */
12365ac860ccSVille Syrjälä 		for_each_pipe(i915, pipe) {
12375ac860ccSVille Syrjälä 			const struct intel_dbuf_bw *crtc_bw = &bw_state->dbuf_bw[pipe];
12385ac860ccSVille Syrjälä 
12395ac860ccSVille Syrjälä 			max_bw = max(crtc_bw->max_bw[slice], max_bw);
12405ac860ccSVille Syrjälä 			num_active_planes += hweight8(crtc_bw->active_planes[slice]);
12415ac860ccSVille Syrjälä 		}
12425ac860ccSVille Syrjälä 		max_bw *= num_active_planes;
12435ac860ccSVille Syrjälä 
12445ac860ccSVille Syrjälä 		total_max_bw = max(total_max_bw, max_bw);
1245943ed3ccSVille Syrjälä 	}
1246cad3fab4SVille Syrjälä 
12475ac860ccSVille Syrjälä 	return DIV_ROUND_UP(total_max_bw, 64);
1248cad3fab4SVille Syrjälä }
1249cad3fab4SVille Syrjälä 
intel_bw_min_cdclk(struct drm_i915_private * i915,const struct intel_bw_state * bw_state)12505ac860ccSVille Syrjälä int intel_bw_min_cdclk(struct drm_i915_private *i915,
12515ac860ccSVille Syrjälä 		       const struct intel_bw_state *bw_state)
12525ac860ccSVille Syrjälä {
1253ea083969SVille Syrjälä 	enum pipe pipe;
1254ea083969SVille Syrjälä 	int min_cdclk;
1255ea083969SVille Syrjälä 
1256ea083969SVille Syrjälä 	min_cdclk = intel_bw_dbuf_min_cdclk(i915, bw_state);
1257ea083969SVille Syrjälä 
1258ea083969SVille Syrjälä 	for_each_pipe(i915, pipe)
1259ea083969SVille Syrjälä 		min_cdclk = max(bw_state->min_cdclk[pipe], min_cdclk);
1260ea083969SVille Syrjälä 
1261ea083969SVille Syrjälä 	return min_cdclk;
12625ac860ccSVille Syrjälä }
12635ac860ccSVille Syrjälä 
intel_bw_calc_min_cdclk(struct intel_atomic_state * state,bool * need_cdclk_calc)12645ac860ccSVille Syrjälä int intel_bw_calc_min_cdclk(struct intel_atomic_state *state,
12655ac860ccSVille Syrjälä 			    bool *need_cdclk_calc)
1266cd191546SStanislav Lisovskiy {
1267cd191546SStanislav Lisovskiy 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1268cac91e67SStanislav Lisovskiy 	struct intel_bw_state *new_bw_state = NULL;
12695ac860ccSVille Syrjälä 	const struct intel_bw_state *old_bw_state = NULL;
12705ac860ccSVille Syrjälä 	const struct intel_cdclk_state *cdclk_state;
1271cd191546SStanislav Lisovskiy 	const struct intel_crtc_state *crtc_state;
12725ac860ccSVille Syrjälä 	int old_min_cdclk, new_min_cdclk;
1273cd191546SStanislav Lisovskiy 	struct intel_crtc *crtc;
1274cac91e67SStanislav Lisovskiy 	int i;
1275cd191546SStanislav Lisovskiy 
12767243867cSVille Syrjälä 	if (DISPLAY_VER(dev_priv) < 9)
12777243867cSVille Syrjälä 		return 0;
12787243867cSVille Syrjälä 
1279cd191546SStanislav Lisovskiy 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
1280cd191546SStanislav Lisovskiy 		new_bw_state = intel_atomic_get_bw_state(state);
1281cd191546SStanislav Lisovskiy 		if (IS_ERR(new_bw_state))
1282cd191546SStanislav Lisovskiy 			return PTR_ERR(new_bw_state);
1283cd191546SStanislav Lisovskiy 
128419aefbc7SStanislav Lisovskiy 		old_bw_state = intel_atomic_get_old_bw_state(state);
128519aefbc7SStanislav Lisovskiy 
1286cad3fab4SVille Syrjälä 		skl_crtc_calc_dbuf_bw(new_bw_state, crtc_state);
1287ea083969SVille Syrjälä 
1288ea083969SVille Syrjälä 		new_bw_state->min_cdclk[crtc->pipe] =
1289ea083969SVille Syrjälä 			intel_bw_crtc_min_cdclk(crtc_state);
129019aefbc7SStanislav Lisovskiy 	}
129119aefbc7SStanislav Lisovskiy 
129219aefbc7SStanislav Lisovskiy 	if (!old_bw_state)
129319aefbc7SStanislav Lisovskiy 		return 0;
129419aefbc7SStanislav Lisovskiy 
12956731eb04SVille Syrjälä 	if (intel_bw_state_changed(dev_priv, old_bw_state, new_bw_state)) {
1296cd191546SStanislav Lisovskiy 		int ret = intel_atomic_lock_global_state(&new_bw_state->base);
1297cd191546SStanislav Lisovskiy 		if (ret)
1298cd191546SStanislav Lisovskiy 			return ret;
1299cd191546SStanislav Lisovskiy 	}
1300cd191546SStanislav Lisovskiy 
13015ac860ccSVille Syrjälä 	old_min_cdclk = intel_bw_min_cdclk(dev_priv, old_bw_state);
13025ac860ccSVille Syrjälä 	new_min_cdclk = intel_bw_min_cdclk(dev_priv, new_bw_state);
13035ac860ccSVille Syrjälä 
13045ac860ccSVille Syrjälä 	/*
13055ac860ccSVille Syrjälä 	 * No need to check against the cdclk state if
1306ea083969SVille Syrjälä 	 * the min cdclk doesn't increase.
13075ac860ccSVille Syrjälä 	 *
1308ea083969SVille Syrjälä 	 * Ie. we only ever increase the cdclk due to bandwidth
13095ac860ccSVille Syrjälä 	 * requirements. This can reduce back and forth
13105ac860ccSVille Syrjälä 	 * display blinking due to constant cdclk changes.
13115ac860ccSVille Syrjälä 	 */
13125ac860ccSVille Syrjälä 	if (new_min_cdclk <= old_min_cdclk)
13135ac860ccSVille Syrjälä 		return 0;
13145ac860ccSVille Syrjälä 
13155ac860ccSVille Syrjälä 	cdclk_state = intel_atomic_get_cdclk_state(state);
13165ac860ccSVille Syrjälä 	if (IS_ERR(cdclk_state))
13175ac860ccSVille Syrjälä 		return PTR_ERR(cdclk_state);
13185ac860ccSVille Syrjälä 
13195ac860ccSVille Syrjälä 	/*
13205ac860ccSVille Syrjälä 	 * No need to recalculate the cdclk state if
1321ea083969SVille Syrjälä 	 * the min cdclk doesn't increase.
13225ac860ccSVille Syrjälä 	 *
1323ea083969SVille Syrjälä 	 * Ie. we only ever increase the cdclk due to bandwidth
13245ac860ccSVille Syrjälä 	 * requirements. This can reduce back and forth
13255ac860ccSVille Syrjälä 	 * display blinking due to constant cdclk changes.
13265ac860ccSVille Syrjälä 	 */
13275ac860ccSVille Syrjälä 	if (new_min_cdclk <= cdclk_state->bw_min_cdclk)
13285ac860ccSVille Syrjälä 		return 0;
13295ac860ccSVille Syrjälä 
13305ac860ccSVille Syrjälä 	drm_dbg_kms(&dev_priv->drm,
13315ac860ccSVille Syrjälä 		    "new bandwidth min cdclk (%d kHz) > old min cdclk (%d kHz)\n",
13325ac860ccSVille Syrjälä 		    new_min_cdclk, cdclk_state->bw_min_cdclk);
13335ac860ccSVille Syrjälä 	*need_cdclk_calc = true;
13345ac860ccSVille Syrjälä 
1335cd191546SStanislav Lisovskiy 	return 0;
1336cd191546SStanislav Lisovskiy }
1337cd191546SStanislav Lisovskiy 
intel_bw_check_data_rate(struct intel_atomic_state * state,bool * changed)13386d8ebef5SVille Syrjälä static int intel_bw_check_data_rate(struct intel_atomic_state *state, bool *changed)
1339df0566a6SJani Nikula {
13406d8ebef5SVille Syrjälä 	struct drm_i915_private *i915 = to_i915(state->base.dev);
13416d8ebef5SVille Syrjälä 	const struct intel_crtc_state *new_crtc_state, *old_crtc_state;
1342df0566a6SJani Nikula 	struct intel_crtc *crtc;
13436d8ebef5SVille Syrjälä 	int i;
1344df0566a6SJani Nikula 
1345df0566a6SJani Nikula 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
1346df0566a6SJani Nikula 					    new_crtc_state, i) {
1347df0566a6SJani Nikula 		unsigned int old_data_rate =
1348df0566a6SJani Nikula 			intel_bw_crtc_data_rate(old_crtc_state);
1349df0566a6SJani Nikula 		unsigned int new_data_rate =
1350df0566a6SJani Nikula 			intel_bw_crtc_data_rate(new_crtc_state);
1351df0566a6SJani Nikula 		unsigned int old_active_planes =
1352df0566a6SJani Nikula 			intel_bw_crtc_num_active_planes(old_crtc_state);
1353df0566a6SJani Nikula 		unsigned int new_active_planes =
1354df0566a6SJani Nikula 			intel_bw_crtc_num_active_planes(new_crtc_state);
13556d8ebef5SVille Syrjälä 		struct intel_bw_state *new_bw_state;
1356df0566a6SJani Nikula 
1357df0566a6SJani Nikula 		/*
1358df0566a6SJani Nikula 		 * Avoid locking the bw state when
1359df0566a6SJani Nikula 		 * nothing significant has changed.
1360df0566a6SJani Nikula 		 */
1361df0566a6SJani Nikula 		if (old_data_rate == new_data_rate &&
1362df0566a6SJani Nikula 		    old_active_planes == new_active_planes)
1363df0566a6SJani Nikula 			continue;
1364df0566a6SJani Nikula 
13659ff79708SStanislav Lisovskiy 		new_bw_state = intel_atomic_get_bw_state(state);
13669ff79708SStanislav Lisovskiy 		if (IS_ERR(new_bw_state))
13679ff79708SStanislav Lisovskiy 			return PTR_ERR(new_bw_state);
1368df0566a6SJani Nikula 
13699ff79708SStanislav Lisovskiy 		new_bw_state->data_rate[crtc->pipe] = new_data_rate;
13709ff79708SStanislav Lisovskiy 		new_bw_state->num_active_planes[crtc->pipe] = new_active_planes;
1371df0566a6SJani Nikula 
13726d8ebef5SVille Syrjälä 		*changed = true;
13736b728595SVille Syrjälä 
13746d8ebef5SVille Syrjälä 		drm_dbg_kms(&i915->drm,
13756d8ebef5SVille Syrjälä 			    "[CRTC:%d:%s] data rate %u num active planes %u\n",
13766d8ebef5SVille Syrjälä 			    crtc->base.base.id, crtc->base.name,
13779ff79708SStanislav Lisovskiy 			    new_bw_state->data_rate[crtc->pipe],
13789ff79708SStanislav Lisovskiy 			    new_bw_state->num_active_planes[crtc->pipe]);
1379df0566a6SJani Nikula 	}
1380df0566a6SJani Nikula 
13816d8ebef5SVille Syrjälä 	return 0;
13826d8ebef5SVille Syrjälä }
13836d8ebef5SVille Syrjälä 
intel_bw_atomic_check(struct intel_atomic_state * state)13846d8ebef5SVille Syrjälä int intel_bw_atomic_check(struct intel_atomic_state *state)
13856d8ebef5SVille Syrjälä {
13866d8ebef5SVille Syrjälä 	bool changed = false;
1387463cc940SVinod Govindapillai 	struct drm_i915_private *i915 = to_i915(state->base.dev);
1388463cc940SVinod Govindapillai 	struct intel_bw_state *new_bw_state;
1389463cc940SVinod Govindapillai 	const struct intel_bw_state *old_bw_state;
1390463cc940SVinod Govindapillai 	int ret;
13916d8ebef5SVille Syrjälä 
13926d8ebef5SVille Syrjälä 	/* FIXME earlier gens need some checks too */
1393463cc940SVinod Govindapillai 	if (DISPLAY_VER(i915) < 11)
13946d8ebef5SVille Syrjälä 		return 0;
13956d8ebef5SVille Syrjälä 
13966d8ebef5SVille Syrjälä 	ret = intel_bw_check_data_rate(state, &changed);
13976d8ebef5SVille Syrjälä 	if (ret)
13986d8ebef5SVille Syrjälä 		return ret;
13996d8ebef5SVille Syrjälä 
14006b728595SVille Syrjälä 	old_bw_state = intel_atomic_get_old_bw_state(state);
14016b728595SVille Syrjälä 	new_bw_state = intel_atomic_get_new_bw_state(state);
14026b728595SVille Syrjälä 
14036b728595SVille Syrjälä 	if (new_bw_state &&
1404aaba7a95SVinod Govindapillai 	    (intel_can_enable_sagv(i915, old_bw_state) !=
1405aaba7a95SVinod Govindapillai 	     intel_can_enable_sagv(i915, new_bw_state) ||
1406aaba7a95SVinod Govindapillai 	     new_bw_state->force_check_qgv))
14076b728595SVille Syrjälä 		changed = true;
14086b728595SVille Syrjälä 
14096b728595SVille Syrjälä 	/*
14106b728595SVille Syrjälä 	 * If none of our inputs (data rates, number of active
14116b728595SVille Syrjälä 	 * planes, SAGV yes/no) changed then nothing to do here.
14126b728595SVille Syrjälä 	 */
14136b728595SVille Syrjälä 	if (!changed)
1414df0566a6SJani Nikula 		return 0;
1415df0566a6SJani Nikula 
1416463cc940SVinod Govindapillai 	ret = intel_bw_check_qgv_points(i915, old_bw_state, new_bw_state);
1417fd1a9bbaSVille Syrjälä 	if (ret)
1418fd1a9bbaSVille Syrjälä 		return ret;
1419fd1a9bbaSVille Syrjälä 
1420aaba7a95SVinod Govindapillai 	new_bw_state->force_check_qgv = false;
1421aaba7a95SVinod Govindapillai 
1422df0566a6SJani Nikula 	return 0;
1423df0566a6SJani Nikula }
1424df0566a6SJani Nikula 
1425fd1a9bbaSVille Syrjälä static struct intel_global_state *
intel_bw_duplicate_state(struct intel_global_obj * obj)1426fd1a9bbaSVille Syrjälä intel_bw_duplicate_state(struct intel_global_obj *obj)
1427df0566a6SJani Nikula {
1428df0566a6SJani Nikula 	struct intel_bw_state *state;
1429df0566a6SJani Nikula 
1430df0566a6SJani Nikula 	state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
1431df0566a6SJani Nikula 	if (!state)
1432df0566a6SJani Nikula 		return NULL;
1433df0566a6SJani Nikula 
1434df0566a6SJani Nikula 	return &state->base;
1435df0566a6SJani Nikula }
1436df0566a6SJani Nikula 
intel_bw_destroy_state(struct intel_global_obj * obj,struct intel_global_state * state)1437fd1a9bbaSVille Syrjälä static void intel_bw_destroy_state(struct intel_global_obj *obj,
1438fd1a9bbaSVille Syrjälä 				   struct intel_global_state *state)
1439df0566a6SJani Nikula {
1440df0566a6SJani Nikula 	kfree(state);
1441df0566a6SJani Nikula }
1442df0566a6SJani Nikula 
1443fd1a9bbaSVille Syrjälä static const struct intel_global_state_funcs intel_bw_funcs = {
1444df0566a6SJani Nikula 	.atomic_duplicate_state = intel_bw_duplicate_state,
1445df0566a6SJani Nikula 	.atomic_destroy_state = intel_bw_destroy_state,
1446df0566a6SJani Nikula };
1447df0566a6SJani Nikula 
intel_bw_init(struct drm_i915_private * i915)14489299cde9SStanislav Lisovskiy int intel_bw_init(struct drm_i915_private *i915)
1449df0566a6SJani Nikula {
1450df0566a6SJani Nikula 	struct intel_bw_state *state;
1451df0566a6SJani Nikula 
1452df0566a6SJani Nikula 	state = kzalloc(sizeof(*state), GFP_KERNEL);
1453df0566a6SJani Nikula 	if (!state)
1454df0566a6SJani Nikula 		return -ENOMEM;
1455df0566a6SJani Nikula 
14569299cde9SStanislav Lisovskiy 	intel_atomic_global_obj_init(i915, &i915->display.bw.obj,
1457df0566a6SJani Nikula 				     &state->base, &intel_bw_funcs);
1458df0566a6SJani Nikula 
14599299cde9SStanislav Lisovskiy 	/*
14609299cde9SStanislav Lisovskiy 	 * Limit this only if we have SAGV. And for Display version 14 onwards
14619299cde9SStanislav Lisovskiy 	 * sagv is handled though pmdemand requests
14629299cde9SStanislav Lisovskiy 	 */
14639299cde9SStanislav Lisovskiy 	if (intel_has_sagv(i915) && IS_DISPLAY_VER(i915, 11, 13))
14649299cde9SStanislav Lisovskiy 		icl_force_disable_sagv(i915, state);
14659299cde9SStanislav Lisovskiy 
1466df0566a6SJani Nikula 	return 0;
1467df0566a6SJani Nikula }
1468