xref: /linux/drivers/gpu/drm/i915/intel_clock_gating.c (revision ba3193fa8fc8910f724b67a523ec67ee24997d3e)
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eugeni Dodonov <eugeni.dodonov@intel.com>
25  *
26  */
27 
28 #include "display/intel_de.h"
29 #include "display/intel_display.h"
30 #include "display/intel_display_trace.h"
31 #include "display/skl_watermark.h"
32 
33 #include "gt/intel_engine_regs.h"
34 #include "gt/intel_gt.h"
35 #include "gt/intel_gt_mcr.h"
36 #include "gt/intel_gt_regs.h"
37 
38 #include "i915_drv.h"
39 #include "i915_reg.h"
40 #include "intel_clock_gating.h"
41 #include "intel_mchbar_regs.h"
42 #include "vlv_sideband.h"
43 
44 struct drm_i915_clock_gating_funcs {
45 	void (*init_clock_gating)(struct drm_i915_private *i915);
46 };
47 
48 static void gen9_init_clock_gating(struct drm_i915_private *i915)
49 {
50 	if (HAS_LLC(i915)) {
51 		/*
52 		 * WaCompressedResourceDisplayNewHashMode:skl,kbl
53 		 * Display WA #0390: skl,kbl
54 		 *
55 		 * Must match Sampler, Pixel Back End, and Media. See
56 		 * WaCompressedResourceSamplerPbeMediaNewHashMode.
57 		 */
58 		intel_uncore_rmw(&i915->uncore, CHICKEN_PAR1_1, 0, SKL_DE_COMPRESSED_HASH_MODE);
59 	}
60 
61 	/* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
62 	intel_uncore_rmw(&i915->uncore, CHICKEN_PAR1_1, 0, SKL_EDP_PSR_FIX_RDWRAP);
63 
64 	/* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
65 	intel_uncore_rmw(&i915->uncore, GEN8_CHICKEN_DCPR_1, 0, MASK_WAKEMEM);
66 
67 	/*
68 	 * WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl
69 	 * Display WA #0859: skl,bxt,kbl,glk,cfl
70 	 */
71 	intel_uncore_rmw(&i915->uncore, DISP_ARB_CTL, 0, DISP_FBC_MEMORY_WAKE);
72 }
73 
74 static void bxt_init_clock_gating(struct drm_i915_private *i915)
75 {
76 	gen9_init_clock_gating(i915);
77 
78 	/* WaDisableSDEUnitClockGating:bxt */
79 	intel_uncore_rmw(&i915->uncore, GEN8_UCGCTL6, 0, GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
80 
81 	/*
82 	 * FIXME:
83 	 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
84 	 */
85 	intel_uncore_rmw(&i915->uncore, GEN8_UCGCTL6, 0, GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
86 
87 	/*
88 	 * Wa: Backlight PWM may stop in the asserted state, causing backlight
89 	 * to stay fully on.
90 	 */
91 	intel_uncore_write(&i915->uncore, GEN9_CLKGATE_DIS_0,
92 			   intel_uncore_read(&i915->uncore, GEN9_CLKGATE_DIS_0) |
93 			   PWM1_GATING_DIS | PWM2_GATING_DIS);
94 
95 	/*
96 	 * Lower the display internal timeout.
97 	 * This is needed to avoid any hard hangs when DSI port PLL
98 	 * is off and a MMIO access is attempted by any privilege
99 	 * application, using batch buffers or any other means.
100 	 */
101 	intel_uncore_write(&i915->uncore, RM_TIMEOUT, MMIO_TIMEOUT_US(950));
102 
103 	/*
104 	 * WaFbcTurnOffFbcWatermark:bxt
105 	 * Display WA #0562: bxt
106 	 */
107 	intel_uncore_rmw(&i915->uncore, DISP_ARB_CTL, 0, DISP_FBC_WM_DIS);
108 }
109 
110 static void glk_init_clock_gating(struct drm_i915_private *i915)
111 {
112 	gen9_init_clock_gating(i915);
113 
114 	/*
115 	 * WaDisablePWMClockGating:glk
116 	 * Backlight PWM may stop in the asserted state, causing backlight
117 	 * to stay fully on.
118 	 */
119 	intel_uncore_write(&i915->uncore, GEN9_CLKGATE_DIS_0,
120 			   intel_uncore_read(&i915->uncore, GEN9_CLKGATE_DIS_0) |
121 			   PWM1_GATING_DIS | PWM2_GATING_DIS);
122 }
123 
124 static void ibx_init_clock_gating(struct drm_i915_private *i915)
125 {
126 	/*
127 	 * On Ibex Peak and Cougar Point, we need to disable clock
128 	 * gating for the panel power sequencer or it will fail to
129 	 * start up when no ports are active.
130 	 */
131 	intel_uncore_write(&i915->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
132 }
133 
134 static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
135 {
136 	enum pipe pipe;
137 
138 	for_each_pipe(dev_priv, pipe) {
139 		intel_uncore_rmw(&dev_priv->uncore, DSPCNTR(pipe), 0, DISP_TRICKLE_FEED_DISABLE);
140 
141 		intel_uncore_rmw(&dev_priv->uncore, DSPSURF(pipe), 0, 0);
142 		intel_uncore_posting_read(&dev_priv->uncore, DSPSURF(pipe));
143 	}
144 }
145 
146 static void ilk_init_clock_gating(struct drm_i915_private *i915)
147 {
148 	u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
149 
150 	/*
151 	 * Required for FBC
152 	 * WaFbcDisableDpfcClockGating:ilk
153 	 */
154 	dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
155 		   ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
156 		   ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
157 
158 	intel_uncore_write(&i915->uncore, PCH_3DCGDIS0,
159 			   MARIUNIT_CLOCK_GATE_DISABLE |
160 			   SVSMUNIT_CLOCK_GATE_DISABLE);
161 	intel_uncore_write(&i915->uncore, PCH_3DCGDIS1,
162 			   VFMUNIT_CLOCK_GATE_DISABLE);
163 
164 	/*
165 	 * According to the spec the following bits should be set in
166 	 * order to enable memory self-refresh
167 	 * The bit 22/21 of 0x42004
168 	 * The bit 5 of 0x42020
169 	 * The bit 15 of 0x45000
170 	 */
171 	intel_uncore_write(&i915->uncore, ILK_DISPLAY_CHICKEN2,
172 			   (intel_uncore_read(&i915->uncore, ILK_DISPLAY_CHICKEN2) |
173 			    ILK_DPARB_GATE | ILK_VSDPFD_FULL));
174 	dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
175 	intel_uncore_write(&i915->uncore, DISP_ARB_CTL,
176 			   (intel_uncore_read(&i915->uncore, DISP_ARB_CTL) |
177 			    DISP_FBC_WM_DIS));
178 
179 	/*
180 	 * Based on the document from hardware guys the following bits
181 	 * should be set unconditionally in order to enable FBC.
182 	 * The bit 22 of 0x42000
183 	 * The bit 22 of 0x42004
184 	 * The bit 7,8,9 of 0x42020.
185 	 */
186 	if (IS_IRONLAKE_M(i915)) {
187 		/* WaFbcAsynchFlipDisableFbcQueue:ilk */
188 		intel_uncore_rmw(&i915->uncore, ILK_DISPLAY_CHICKEN1, 0, ILK_FBCQ_DIS);
189 		intel_uncore_rmw(&i915->uncore, ILK_DISPLAY_CHICKEN2, 0, ILK_DPARB_GATE);
190 	}
191 
192 	intel_uncore_write(&i915->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
193 
194 	intel_uncore_rmw(&i915->uncore, ILK_DISPLAY_CHICKEN2, 0, ILK_ELPIN_409_SELECT);
195 
196 	g4x_disable_trickle_feed(i915);
197 
198 	ibx_init_clock_gating(i915);
199 }
200 
201 static void cpt_init_clock_gating(struct drm_i915_private *i915)
202 {
203 	enum pipe pipe;
204 	u32 val;
205 
206 	/*
207 	 * On Ibex Peak and Cougar Point, we need to disable clock
208 	 * gating for the panel power sequencer or it will fail to
209 	 * start up when no ports are active.
210 	 */
211 	intel_uncore_write(&i915->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
212 			   PCH_DPLUNIT_CLOCK_GATE_DISABLE |
213 			   PCH_CPUNIT_CLOCK_GATE_DISABLE);
214 	intel_uncore_rmw(&i915->uncore, SOUTH_CHICKEN2, 0, DPLS_EDP_PPS_FIX_DIS);
215 	/* The below fixes the weird display corruption, a few pixels shifted
216 	 * downward, on (only) LVDS of some HP laptops with IVY.
217 	 */
218 	for_each_pipe(i915, pipe) {
219 		val = intel_uncore_read(&i915->uncore, TRANS_CHICKEN2(pipe));
220 		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
221 		val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
222 		if (i915->display.vbt.fdi_rx_polarity_inverted)
223 			val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
224 		val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
225 		val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
226 		intel_uncore_write(&i915->uncore, TRANS_CHICKEN2(pipe), val);
227 	}
228 	/* WADP0ClockGatingDisable */
229 	for_each_pipe(i915, pipe) {
230 		intel_uncore_write(&i915->uncore, TRANS_CHICKEN1(pipe),
231 				   TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
232 	}
233 }
234 
235 static void gen6_check_mch_setup(struct drm_i915_private *i915)
236 {
237 	u32 tmp;
238 
239 	tmp = intel_uncore_read(&i915->uncore, MCH_SSKPD);
240 	if (REG_FIELD_GET(SSKPD_WM0_MASK_SNB, tmp) != 12)
241 		drm_dbg_kms(&i915->drm,
242 			    "Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
243 			    tmp);
244 }
245 
246 static void gen6_init_clock_gating(struct drm_i915_private *i915)
247 {
248 	u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
249 
250 	intel_uncore_write(&i915->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
251 
252 	intel_uncore_rmw(&i915->uncore, ILK_DISPLAY_CHICKEN2, 0, ILK_ELPIN_409_SELECT);
253 
254 	intel_uncore_write(&i915->uncore, GEN6_UCGCTL1,
255 			   intel_uncore_read(&i915->uncore, GEN6_UCGCTL1) |
256 			   GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
257 			   GEN6_CSUNIT_CLOCK_GATE_DISABLE);
258 
259 	/* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
260 	 * gating disable must be set.  Failure to set it results in
261 	 * flickering pixels due to Z write ordering failures after
262 	 * some amount of runtime in the Mesa "fire" demo, and Unigine
263 	 * Sanctuary and Tropics, and apparently anything else with
264 	 * alpha test or pixel discard.
265 	 *
266 	 * According to the spec, bit 11 (RCCUNIT) must also be set,
267 	 * but we didn't debug actual testcases to find it out.
268 	 *
269 	 * WaDisableRCCUnitClockGating:snb
270 	 * WaDisableRCPBUnitClockGating:snb
271 	 */
272 	intel_uncore_write(&i915->uncore, GEN6_UCGCTL2,
273 			   GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
274 			   GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
275 
276 	/*
277 	 * According to the spec the following bits should be
278 	 * set in order to enable memory self-refresh and fbc:
279 	 * The bit21 and bit22 of 0x42000
280 	 * The bit21 and bit22 of 0x42004
281 	 * The bit5 and bit7 of 0x42020
282 	 * The bit14 of 0x70180
283 	 * The bit14 of 0x71180
284 	 *
285 	 * WaFbcAsynchFlipDisableFbcQueue:snb
286 	 */
287 	intel_uncore_write(&i915->uncore, ILK_DISPLAY_CHICKEN1,
288 			   intel_uncore_read(&i915->uncore, ILK_DISPLAY_CHICKEN1) |
289 			   ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
290 	intel_uncore_write(&i915->uncore, ILK_DISPLAY_CHICKEN2,
291 			   intel_uncore_read(&i915->uncore, ILK_DISPLAY_CHICKEN2) |
292 			   ILK_DPARB_GATE | ILK_VSDPFD_FULL);
293 	intel_uncore_write(&i915->uncore, ILK_DSPCLK_GATE_D,
294 			   intel_uncore_read(&i915->uncore, ILK_DSPCLK_GATE_D) |
295 			   ILK_DPARBUNIT_CLOCK_GATE_ENABLE  |
296 			   ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
297 
298 	g4x_disable_trickle_feed(i915);
299 
300 	cpt_init_clock_gating(i915);
301 
302 	gen6_check_mch_setup(i915);
303 }
304 
305 static void lpt_init_clock_gating(struct drm_i915_private *i915)
306 {
307 	/*
308 	 * TODO: this bit should only be enabled when really needed, then
309 	 * disabled when not needed anymore in order to save power.
310 	 */
311 	if (HAS_PCH_LPT_LP(i915))
312 		intel_uncore_rmw(&i915->uncore, SOUTH_DSPCLK_GATE_D,
313 				 0, PCH_LP_PARTITION_LEVEL_DISABLE);
314 
315 	/* WADPOClockGatingDisable:hsw */
316 	intel_uncore_rmw(&i915->uncore, TRANS_CHICKEN1(PIPE_A),
317 			 0, TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
318 }
319 
320 static void gen8_set_l3sqc_credits(struct drm_i915_private *i915,
321 				   int general_prio_credits,
322 				   int high_prio_credits)
323 {
324 	u32 misccpctl;
325 	u32 val;
326 
327 	/* WaTempDisableDOPClkGating:bdw */
328 	misccpctl = intel_uncore_rmw(&i915->uncore, GEN7_MISCCPCTL,
329 				     GEN7_DOP_CLOCK_GATE_ENABLE, 0);
330 
331 	val = intel_gt_mcr_read_any(to_gt(i915), GEN8_L3SQCREG1);
332 	val &= ~L3_PRIO_CREDITS_MASK;
333 	val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
334 	val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
335 	intel_gt_mcr_multicast_write(to_gt(i915), GEN8_L3SQCREG1, val);
336 
337 	/*
338 	 * Wait at least 100 clocks before re-enabling clock gating.
339 	 * See the definition of L3SQCREG1 in BSpec.
340 	 */
341 	intel_gt_mcr_read_any(to_gt(i915), GEN8_L3SQCREG1);
342 	udelay(1);
343 	intel_uncore_write(&i915->uncore, GEN7_MISCCPCTL, misccpctl);
344 }
345 
346 static void dg2_init_clock_gating(struct drm_i915_private *i915)
347 {
348 	/* Wa_22010954014:dg2 */
349 	intel_uncore_rmw(&i915->uncore, XEHP_CLOCK_GATE_DIS, 0,
350 			 SGSI_SIDECLK_DIS);
351 }
352 
353 static void cnp_init_clock_gating(struct drm_i915_private *i915)
354 {
355 	if (!HAS_PCH_CNP(i915))
356 		return;
357 
358 	/* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
359 	intel_uncore_rmw(&i915->uncore, SOUTH_DSPCLK_GATE_D, 0, CNP_PWM_CGE_GATING_DISABLE);
360 }
361 
362 static void cfl_init_clock_gating(struct drm_i915_private *i915)
363 {
364 	cnp_init_clock_gating(i915);
365 	gen9_init_clock_gating(i915);
366 
367 	/* WAC6entrylatency:cfl */
368 	intel_uncore_rmw(&i915->uncore, FBC_LLC_READ_CTRL, 0, FBC_LLC_FULLY_OPEN);
369 
370 	/*
371 	 * WaFbcTurnOffFbcWatermark:cfl
372 	 * Display WA #0562: cfl
373 	 */
374 	intel_uncore_rmw(&i915->uncore, DISP_ARB_CTL, 0, DISP_FBC_WM_DIS);
375 }
376 
377 static void kbl_init_clock_gating(struct drm_i915_private *i915)
378 {
379 	gen9_init_clock_gating(i915);
380 
381 	/* WAC6entrylatency:kbl */
382 	intel_uncore_rmw(&i915->uncore, FBC_LLC_READ_CTRL, 0, FBC_LLC_FULLY_OPEN);
383 
384 	/* WaDisableSDEUnitClockGating:kbl */
385 	if (IS_KABYLAKE(i915) && IS_GRAPHICS_STEP(i915, 0, STEP_C0))
386 		intel_uncore_rmw(&i915->uncore, GEN8_UCGCTL6,
387 				 0, GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
388 
389 	/* WaDisableGamClockGating:kbl */
390 	if (IS_KABYLAKE(i915) && IS_GRAPHICS_STEP(i915, 0, STEP_C0))
391 		intel_uncore_rmw(&i915->uncore, GEN6_UCGCTL1,
392 				 0, GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
393 
394 	/*
395 	 * WaFbcTurnOffFbcWatermark:kbl
396 	 * Display WA #0562: kbl
397 	 */
398 	intel_uncore_rmw(&i915->uncore, DISP_ARB_CTL, 0, DISP_FBC_WM_DIS);
399 }
400 
401 static void skl_init_clock_gating(struct drm_i915_private *i915)
402 {
403 	gen9_init_clock_gating(i915);
404 
405 	/* WaDisableDopClockGating:skl */
406 	intel_uncore_rmw(&i915->uncore, GEN7_MISCCPCTL,
407 			 GEN7_DOP_CLOCK_GATE_ENABLE, 0);
408 
409 	/* WAC6entrylatency:skl */
410 	intel_uncore_rmw(&i915->uncore, FBC_LLC_READ_CTRL, 0, FBC_LLC_FULLY_OPEN);
411 
412 	/*
413 	 * WaFbcTurnOffFbcWatermark:skl
414 	 * Display WA #0562: skl
415 	 */
416 	intel_uncore_rmw(&i915->uncore, DISP_ARB_CTL, 0, DISP_FBC_WM_DIS);
417 }
418 
419 static void bdw_init_clock_gating(struct drm_i915_private *i915)
420 {
421 	enum pipe pipe;
422 
423 	/* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
424 	intel_uncore_rmw(&i915->uncore, CHICKEN_PIPESL_1(PIPE_A), 0, HSW_FBCQ_DIS);
425 
426 	/* WaSwitchSolVfFArbitrationPriority:bdw */
427 	intel_uncore_rmw(&i915->uncore, GAM_ECOCHK, 0, HSW_ECOCHK_ARB_PRIO_SOL);
428 
429 	/* WaPsrDPAMaskVBlankInSRD:bdw */
430 	intel_uncore_rmw(&i915->uncore, CHICKEN_PAR1_1, 0, HSW_MASK_VBL_TO_PIPE_IN_SRD);
431 
432 	for_each_pipe(i915, pipe) {
433 		/* WaPsrDPRSUnmaskVBlankInSRD:bdw */
434 		intel_uncore_rmw(&i915->uncore, CHICKEN_PIPESL_1(pipe),
435 				 0, BDW_UNMASK_VBL_TO_REGS_IN_SRD);
436 	}
437 
438 	/* WaVSRefCountFullforceMissDisable:bdw */
439 	/* WaDSRefCountFullforceMissDisable:bdw */
440 	intel_uncore_rmw(&i915->uncore, GEN7_FF_THREAD_MODE,
441 			 GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME, 0);
442 
443 	intel_uncore_write(&i915->uncore, RING_PSMI_CTL(RENDER_RING_BASE),
444 			   _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
445 
446 	/* WaDisableSDEUnitClockGating:bdw */
447 	intel_uncore_rmw(&i915->uncore, GEN8_UCGCTL6, 0, GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
448 
449 	/* WaProgramL3SqcReg1Default:bdw */
450 	gen8_set_l3sqc_credits(i915, 30, 2);
451 
452 	/* WaKVMNotificationOnConfigChange:bdw */
453 	intel_uncore_rmw(&i915->uncore, CHICKEN_PAR2_1,
454 			 0, KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
455 
456 	lpt_init_clock_gating(i915);
457 
458 	/* WaDisableDopClockGating:bdw
459 	 *
460 	 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
461 	 * clock gating.
462 	 */
463 	intel_uncore_rmw(&i915->uncore, GEN6_UCGCTL1, 0, GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
464 }
465 
466 static void hsw_init_clock_gating(struct drm_i915_private *i915)
467 {
468 	enum pipe pipe;
469 
470 	/* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
471 	intel_uncore_rmw(&i915->uncore, CHICKEN_PIPESL_1(PIPE_A), 0, HSW_FBCQ_DIS);
472 
473 	/* WaPsrDPAMaskVBlankInSRD:hsw */
474 	intel_uncore_rmw(&i915->uncore, CHICKEN_PAR1_1, 0, HSW_MASK_VBL_TO_PIPE_IN_SRD);
475 
476 	for_each_pipe(i915, pipe) {
477 		/* WaPsrDPRSUnmaskVBlankInSRD:hsw */
478 		intel_uncore_rmw(&i915->uncore, CHICKEN_PIPESL_1(pipe),
479 				 0, HSW_UNMASK_VBL_TO_REGS_IN_SRD);
480 	}
481 
482 	/* This is required by WaCatErrorRejectionIssue:hsw */
483 	intel_uncore_rmw(&i915->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
484 			 0, GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
485 
486 	/* WaSwitchSolVfFArbitrationPriority:hsw */
487 	intel_uncore_rmw(&i915->uncore, GAM_ECOCHK, 0, HSW_ECOCHK_ARB_PRIO_SOL);
488 
489 	lpt_init_clock_gating(i915);
490 }
491 
492 static void ivb_init_clock_gating(struct drm_i915_private *i915)
493 {
494 	intel_uncore_write(&i915->uncore, ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
495 
496 	/* WaFbcAsynchFlipDisableFbcQueue:ivb */
497 	intel_uncore_rmw(&i915->uncore, ILK_DISPLAY_CHICKEN1, 0, ILK_FBCQ_DIS);
498 
499 	/* WaDisableBackToBackFlipFix:ivb */
500 	intel_uncore_write(&i915->uncore, IVB_CHICKEN3,
501 			   CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
502 			   CHICKEN3_DGMG_DONE_FIX_DISABLE);
503 
504 	if (IS_IVB_GT1(i915))
505 		intel_uncore_write(&i915->uncore, GEN7_ROW_CHICKEN2,
506 				   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
507 	else {
508 		/* must write both registers */
509 		intel_uncore_write(&i915->uncore, GEN7_ROW_CHICKEN2,
510 				   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
511 		intel_uncore_write(&i915->uncore, GEN7_ROW_CHICKEN2_GT2,
512 				   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
513 	}
514 
515 	/*
516 	 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
517 	 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
518 	 */
519 	intel_uncore_write(&i915->uncore, GEN6_UCGCTL2,
520 			   GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
521 
522 	/* This is required by WaCatErrorRejectionIssue:ivb */
523 	intel_uncore_rmw(&i915->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
524 			 0, GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
525 
526 	g4x_disable_trickle_feed(i915);
527 
528 	intel_uncore_rmw(&i915->uncore, GEN6_MBCUNIT_SNPCR, GEN6_MBC_SNPCR_MASK,
529 			 GEN6_MBC_SNPCR_MED);
530 
531 	if (!HAS_PCH_NOP(i915))
532 		cpt_init_clock_gating(i915);
533 
534 	gen6_check_mch_setup(i915);
535 }
536 
537 static void vlv_init_clock_gating(struct drm_i915_private *i915)
538 {
539 	/* WaDisableBackToBackFlipFix:vlv */
540 	intel_uncore_write(&i915->uncore, IVB_CHICKEN3,
541 			   CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
542 			   CHICKEN3_DGMG_DONE_FIX_DISABLE);
543 
544 	/* WaDisableDopClockGating:vlv */
545 	intel_uncore_write(&i915->uncore, GEN7_ROW_CHICKEN2,
546 			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
547 
548 	/* This is required by WaCatErrorRejectionIssue:vlv */
549 	intel_uncore_rmw(&i915->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
550 			 0, GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
551 
552 	/*
553 	 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
554 	 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
555 	 */
556 	intel_uncore_write(&i915->uncore, GEN6_UCGCTL2,
557 			   GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
558 
559 	/* WaDisableL3Bank2xClockGate:vlv
560 	 * Disabling L3 clock gating- MMIO 940c[25] = 1
561 	 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
562 	intel_uncore_rmw(&i915->uncore, GEN7_UCGCTL4, 0, GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
563 
564 	/*
565 	 * WaDisableVLVClockGating_VBIIssue:vlv
566 	 * Disable clock gating on th GCFG unit to prevent a delay
567 	 * in the reporting of vblank events.
568 	 */
569 	intel_uncore_write(&i915->uncore, VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
570 }
571 
572 static void chv_init_clock_gating(struct drm_i915_private *i915)
573 {
574 	/* WaVSRefCountFullforceMissDisable:chv */
575 	/* WaDSRefCountFullforceMissDisable:chv */
576 	intel_uncore_rmw(&i915->uncore, GEN7_FF_THREAD_MODE,
577 			 GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME, 0);
578 
579 	/* WaDisableSemaphoreAndSyncFlipWait:chv */
580 	intel_uncore_write(&i915->uncore, RING_PSMI_CTL(RENDER_RING_BASE),
581 			   _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
582 
583 	/* WaDisableCSUnitClockGating:chv */
584 	intel_uncore_rmw(&i915->uncore, GEN6_UCGCTL1, 0, GEN6_CSUNIT_CLOCK_GATE_DISABLE);
585 
586 	/* WaDisableSDEUnitClockGating:chv */
587 	intel_uncore_rmw(&i915->uncore, GEN8_UCGCTL6, 0, GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
588 
589 	/*
590 	 * WaProgramL3SqcReg1Default:chv
591 	 * See gfxspecs/Related Documents/Performance Guide/
592 	 * LSQC Setting Recommendations.
593 	 */
594 	gen8_set_l3sqc_credits(i915, 38, 2);
595 }
596 
597 static void g4x_init_clock_gating(struct drm_i915_private *i915)
598 {
599 	u32 dspclk_gate;
600 
601 	intel_uncore_write(&i915->uncore, RENCLK_GATE_D1, 0);
602 	intel_uncore_write(&i915->uncore, RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
603 			   GS_UNIT_CLOCK_GATE_DISABLE |
604 			   CL_UNIT_CLOCK_GATE_DISABLE);
605 	intel_uncore_write(&i915->uncore, RAMCLK_GATE_D, 0);
606 	dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
607 		OVRUNIT_CLOCK_GATE_DISABLE |
608 		OVCUNIT_CLOCK_GATE_DISABLE;
609 	if (IS_GM45(i915))
610 		dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
611 	intel_uncore_write(&i915->uncore, DSPCLK_GATE_D(i915), dspclk_gate);
612 
613 	g4x_disable_trickle_feed(i915);
614 }
615 
616 static void i965gm_init_clock_gating(struct drm_i915_private *i915)
617 {
618 	struct intel_uncore *uncore = &i915->uncore;
619 
620 	intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
621 	intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
622 	intel_uncore_write(uncore, DSPCLK_GATE_D(i915), 0);
623 	intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
624 	intel_uncore_write16(uncore, DEUC, 0);
625 	intel_uncore_write(uncore,
626 			   MI_ARB_STATE,
627 			   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
628 }
629 
630 static void i965g_init_clock_gating(struct drm_i915_private *i915)
631 {
632 	intel_uncore_write(&i915->uncore, RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
633 			   I965_RCC_CLOCK_GATE_DISABLE |
634 			   I965_RCPB_CLOCK_GATE_DISABLE |
635 			   I965_ISC_CLOCK_GATE_DISABLE |
636 			   I965_FBC_CLOCK_GATE_DISABLE);
637 	intel_uncore_write(&i915->uncore, RENCLK_GATE_D2, 0);
638 	intel_uncore_write(&i915->uncore, MI_ARB_STATE,
639 			   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
640 }
641 
642 static void gen3_init_clock_gating(struct drm_i915_private *i915)
643 {
644 	u32 dstate = intel_uncore_read(&i915->uncore, D_STATE);
645 
646 	dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
647 		DSTATE_DOT_CLOCK_GATING;
648 	intel_uncore_write(&i915->uncore, D_STATE, dstate);
649 
650 	if (IS_PINEVIEW(i915))
651 		intel_uncore_write(&i915->uncore, ECOSKPD(RENDER_RING_BASE),
652 				   _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
653 
654 	/* IIR "flip pending" means done if this bit is set */
655 	intel_uncore_write(&i915->uncore, ECOSKPD(RENDER_RING_BASE),
656 			   _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
657 
658 	/* interrupts should cause a wake up from C3 */
659 	intel_uncore_write(&i915->uncore, INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
660 
661 	/* On GEN3 we really need to make sure the ARB C3 LP bit is set */
662 	intel_uncore_write(&i915->uncore, MI_ARB_STATE,
663 			   _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
664 
665 	intel_uncore_write(&i915->uncore, MI_ARB_STATE,
666 			   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
667 }
668 
669 static void i85x_init_clock_gating(struct drm_i915_private *i915)
670 {
671 	intel_uncore_write(&i915->uncore, RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
672 
673 	/* interrupts should cause a wake up from C3 */
674 	intel_uncore_write(&i915->uncore, MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
675 			   _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
676 
677 	intel_uncore_write(&i915->uncore, MEM_MODE,
678 			   _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
679 
680 	/*
681 	 * Have FBC ignore 3D activity since we use software
682 	 * render tracking, and otherwise a pure 3D workload
683 	 * (even if it just renders a single frame and then does
684 	 * abosultely nothing) would not allow FBC to recompress
685 	 * until a 2D blit occurs.
686 	 */
687 	intel_uncore_write(&i915->uncore, SCPD0,
688 			   _MASKED_BIT_ENABLE(SCPD_FBC_IGNORE_3D));
689 }
690 
691 static void i830_init_clock_gating(struct drm_i915_private *i915)
692 {
693 	intel_uncore_write(&i915->uncore, MEM_MODE,
694 			   _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
695 			   _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
696 }
697 
698 void intel_clock_gating_init(struct drm_i915_private *i915)
699 {
700 	i915->clock_gating_funcs->init_clock_gating(i915);
701 }
702 
703 static void nop_init_clock_gating(struct drm_i915_private *i915)
704 {
705 	drm_dbg_kms(&i915->drm,
706 		    "No clock gating settings or workarounds applied.\n");
707 }
708 
709 #define CG_FUNCS(platform)						\
710 static const struct drm_i915_clock_gating_funcs platform##_clock_gating_funcs = { \
711 	.init_clock_gating = platform##_init_clock_gating,		\
712 }
713 
714 CG_FUNCS(dg2);
715 CG_FUNCS(cfl);
716 CG_FUNCS(skl);
717 CG_FUNCS(kbl);
718 CG_FUNCS(bxt);
719 CG_FUNCS(glk);
720 CG_FUNCS(bdw);
721 CG_FUNCS(chv);
722 CG_FUNCS(hsw);
723 CG_FUNCS(ivb);
724 CG_FUNCS(vlv);
725 CG_FUNCS(gen6);
726 CG_FUNCS(ilk);
727 CG_FUNCS(g4x);
728 CG_FUNCS(i965gm);
729 CG_FUNCS(i965g);
730 CG_FUNCS(gen3);
731 CG_FUNCS(i85x);
732 CG_FUNCS(i830);
733 CG_FUNCS(nop);
734 #undef CG_FUNCS
735 
736 /**
737  * intel_clock_gating_hooks_init - setup the clock gating hooks
738  * @i915: device private
739  *
740  * Setup the hooks that configure which clocks of a given platform can be
741  * gated and also apply various GT and display specific workarounds for these
742  * platforms. Note that some GT specific workarounds are applied separately
743  * when GPU contexts or batchbuffers start their execution.
744  */
745 void intel_clock_gating_hooks_init(struct drm_i915_private *i915)
746 {
747 	if (IS_DG2(i915))
748 		i915->clock_gating_funcs = &dg2_clock_gating_funcs;
749 	else if (IS_COFFEELAKE(i915) || IS_COMETLAKE(i915))
750 		i915->clock_gating_funcs = &cfl_clock_gating_funcs;
751 	else if (IS_SKYLAKE(i915))
752 		i915->clock_gating_funcs = &skl_clock_gating_funcs;
753 	else if (IS_KABYLAKE(i915))
754 		i915->clock_gating_funcs = &kbl_clock_gating_funcs;
755 	else if (IS_BROXTON(i915))
756 		i915->clock_gating_funcs = &bxt_clock_gating_funcs;
757 	else if (IS_GEMINILAKE(i915))
758 		i915->clock_gating_funcs = &glk_clock_gating_funcs;
759 	else if (IS_BROADWELL(i915))
760 		i915->clock_gating_funcs = &bdw_clock_gating_funcs;
761 	else if (IS_CHERRYVIEW(i915))
762 		i915->clock_gating_funcs = &chv_clock_gating_funcs;
763 	else if (IS_HASWELL(i915))
764 		i915->clock_gating_funcs = &hsw_clock_gating_funcs;
765 	else if (IS_IVYBRIDGE(i915))
766 		i915->clock_gating_funcs = &ivb_clock_gating_funcs;
767 	else if (IS_VALLEYVIEW(i915))
768 		i915->clock_gating_funcs = &vlv_clock_gating_funcs;
769 	else if (GRAPHICS_VER(i915) == 6)
770 		i915->clock_gating_funcs = &gen6_clock_gating_funcs;
771 	else if (GRAPHICS_VER(i915) == 5)
772 		i915->clock_gating_funcs = &ilk_clock_gating_funcs;
773 	else if (IS_G4X(i915))
774 		i915->clock_gating_funcs = &g4x_clock_gating_funcs;
775 	else if (IS_I965GM(i915))
776 		i915->clock_gating_funcs = &i965gm_clock_gating_funcs;
777 	else if (IS_I965G(i915))
778 		i915->clock_gating_funcs = &i965g_clock_gating_funcs;
779 	else if (GRAPHICS_VER(i915) == 3)
780 		i915->clock_gating_funcs = &gen3_clock_gating_funcs;
781 	else if (IS_I85X(i915) || IS_I865G(i915))
782 		i915->clock_gating_funcs = &i85x_clock_gating_funcs;
783 	else if (GRAPHICS_VER(i915) == 2)
784 		i915->clock_gating_funcs = &i830_clock_gating_funcs;
785 	else
786 		i915->clock_gating_funcs = &nop_clock_gating_funcs;
787 }
788