xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/i915/vlv_suspend.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: MIT
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright © 2020 Intel Corporation
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/kernel.h>
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <drm/drm_print.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include "i915_drv.h"
11*4882a593Smuzhiyun #include "i915_reg.h"
12*4882a593Smuzhiyun #include "i915_trace.h"
13*4882a593Smuzhiyun #include "i915_utils.h"
14*4882a593Smuzhiyun #include "intel_pm.h"
15*4882a593Smuzhiyun #include "vlv_suspend.h"
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun struct vlv_s0ix_state {
18*4882a593Smuzhiyun 	/* GAM */
19*4882a593Smuzhiyun 	u32 wr_watermark;
20*4882a593Smuzhiyun 	u32 gfx_prio_ctrl;
21*4882a593Smuzhiyun 	u32 arb_mode;
22*4882a593Smuzhiyun 	u32 gfx_pend_tlb0;
23*4882a593Smuzhiyun 	u32 gfx_pend_tlb1;
24*4882a593Smuzhiyun 	u32 lra_limits[GEN7_LRA_LIMITS_REG_NUM];
25*4882a593Smuzhiyun 	u32 media_max_req_count;
26*4882a593Smuzhiyun 	u32 gfx_max_req_count;
27*4882a593Smuzhiyun 	u32 render_hwsp;
28*4882a593Smuzhiyun 	u32 ecochk;
29*4882a593Smuzhiyun 	u32 bsd_hwsp;
30*4882a593Smuzhiyun 	u32 blt_hwsp;
31*4882a593Smuzhiyun 	u32 tlb_rd_addr;
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	/* MBC */
34*4882a593Smuzhiyun 	u32 g3dctl;
35*4882a593Smuzhiyun 	u32 gsckgctl;
36*4882a593Smuzhiyun 	u32 mbctl;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	/* GCP */
39*4882a593Smuzhiyun 	u32 ucgctl1;
40*4882a593Smuzhiyun 	u32 ucgctl3;
41*4882a593Smuzhiyun 	u32 rcgctl1;
42*4882a593Smuzhiyun 	u32 rcgctl2;
43*4882a593Smuzhiyun 	u32 rstctl;
44*4882a593Smuzhiyun 	u32 misccpctl;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun 	/* GPM */
47*4882a593Smuzhiyun 	u32 gfxpause;
48*4882a593Smuzhiyun 	u32 rpdeuhwtc;
49*4882a593Smuzhiyun 	u32 rpdeuc;
50*4882a593Smuzhiyun 	u32 ecobus;
51*4882a593Smuzhiyun 	u32 pwrdwnupctl;
52*4882a593Smuzhiyun 	u32 rp_down_timeout;
53*4882a593Smuzhiyun 	u32 rp_deucsw;
54*4882a593Smuzhiyun 	u32 rcubmabdtmr;
55*4882a593Smuzhiyun 	u32 rcedata;
56*4882a593Smuzhiyun 	u32 spare2gh;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	/* Display 1 CZ domain */
59*4882a593Smuzhiyun 	u32 gt_imr;
60*4882a593Smuzhiyun 	u32 gt_ier;
61*4882a593Smuzhiyun 	u32 pm_imr;
62*4882a593Smuzhiyun 	u32 pm_ier;
63*4882a593Smuzhiyun 	u32 gt_scratch[GEN7_GT_SCRATCH_REG_NUM];
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	/* GT SA CZ domain */
66*4882a593Smuzhiyun 	u32 tilectl;
67*4882a593Smuzhiyun 	u32 gt_fifoctl;
68*4882a593Smuzhiyun 	u32 gtlc_wake_ctrl;
69*4882a593Smuzhiyun 	u32 gtlc_survive;
70*4882a593Smuzhiyun 	u32 pmwgicz;
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	/* Display 2 CZ domain */
73*4882a593Smuzhiyun 	u32 gu_ctl0;
74*4882a593Smuzhiyun 	u32 gu_ctl1;
75*4882a593Smuzhiyun 	u32 pcbr;
76*4882a593Smuzhiyun 	u32 clock_gate_dis2;
77*4882a593Smuzhiyun };
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun /*
80*4882a593Smuzhiyun  * Save all Gunit registers that may be lost after a D3 and a subsequent
81*4882a593Smuzhiyun  * S0i[R123] transition. The list of registers needing a save/restore is
82*4882a593Smuzhiyun  * defined in the VLV2_S0IXRegs document. This documents marks all Gunit
83*4882a593Smuzhiyun  * registers in the following way:
84*4882a593Smuzhiyun  * - Driver: saved/restored by the driver
85*4882a593Smuzhiyun  * - Punit : saved/restored by the Punit firmware
86*4882a593Smuzhiyun  * - No, w/o marking: no need to save/restore, since the register is R/O or
87*4882a593Smuzhiyun  *                    used internally by the HW in a way that doesn't depend
88*4882a593Smuzhiyun  *                    keeping the content across a suspend/resume.
89*4882a593Smuzhiyun  * - Debug : used for debugging
90*4882a593Smuzhiyun  *
91*4882a593Smuzhiyun  * We save/restore all registers marked with 'Driver', with the following
92*4882a593Smuzhiyun  * exceptions:
93*4882a593Smuzhiyun  * - Registers out of use, including also registers marked with 'Debug'.
94*4882a593Smuzhiyun  *   These have no effect on the driver's operation, so we don't save/restore
95*4882a593Smuzhiyun  *   them to reduce the overhead.
96*4882a593Smuzhiyun  * - Registers that are fully setup by an initialization function called from
97*4882a593Smuzhiyun  *   the resume path. For example many clock gating and RPS/RC6 registers.
98*4882a593Smuzhiyun  * - Registers that provide the right functionality with their reset defaults.
99*4882a593Smuzhiyun  *
100*4882a593Smuzhiyun  * TODO: Except for registers that based on the above 3 criteria can be safely
101*4882a593Smuzhiyun  * ignored, we save/restore all others, practically treating the HW context as
102*4882a593Smuzhiyun  * a black-box for the driver. Further investigation is needed to reduce the
103*4882a593Smuzhiyun  * saved/restored registers even further, by following the same 3 criteria.
104*4882a593Smuzhiyun  */
vlv_save_gunit_s0ix_state(struct drm_i915_private * i915)105*4882a593Smuzhiyun static void vlv_save_gunit_s0ix_state(struct drm_i915_private *i915)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun 	struct vlv_s0ix_state *s = i915->vlv_s0ix_state;
108*4882a593Smuzhiyun 	struct intel_uncore *uncore = &i915->uncore;
109*4882a593Smuzhiyun 	int i;
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	if (!s)
112*4882a593Smuzhiyun 		return;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	/* GAM 0x4000-0x4770 */
115*4882a593Smuzhiyun 	s->wr_watermark = intel_uncore_read(uncore, GEN7_WR_WATERMARK);
116*4882a593Smuzhiyun 	s->gfx_prio_ctrl = intel_uncore_read(uncore, GEN7_GFX_PRIO_CTRL);
117*4882a593Smuzhiyun 	s->arb_mode = intel_uncore_read(uncore, ARB_MODE);
118*4882a593Smuzhiyun 	s->gfx_pend_tlb0 = intel_uncore_read(uncore, GEN7_GFX_PEND_TLB0);
119*4882a593Smuzhiyun 	s->gfx_pend_tlb1 = intel_uncore_read(uncore, GEN7_GFX_PEND_TLB1);
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
122*4882a593Smuzhiyun 		s->lra_limits[i] = intel_uncore_read(uncore, GEN7_LRA_LIMITS(i));
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	s->media_max_req_count = intel_uncore_read(uncore, GEN7_MEDIA_MAX_REQ_COUNT);
125*4882a593Smuzhiyun 	s->gfx_max_req_count = intel_uncore_read(uncore, GEN7_GFX_MAX_REQ_COUNT);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	s->render_hwsp = intel_uncore_read(uncore, RENDER_HWS_PGA_GEN7);
128*4882a593Smuzhiyun 	s->ecochk = intel_uncore_read(uncore, GAM_ECOCHK);
129*4882a593Smuzhiyun 	s->bsd_hwsp = intel_uncore_read(uncore, BSD_HWS_PGA_GEN7);
130*4882a593Smuzhiyun 	s->blt_hwsp = intel_uncore_read(uncore, BLT_HWS_PGA_GEN7);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	s->tlb_rd_addr = intel_uncore_read(uncore, GEN7_TLB_RD_ADDR);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	/* MBC 0x9024-0x91D0, 0x8500 */
135*4882a593Smuzhiyun 	s->g3dctl = intel_uncore_read(uncore, VLV_G3DCTL);
136*4882a593Smuzhiyun 	s->gsckgctl = intel_uncore_read(uncore, VLV_GSCKGCTL);
137*4882a593Smuzhiyun 	s->mbctl = intel_uncore_read(uncore, GEN6_MBCTL);
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	/* GCP 0x9400-0x9424, 0x8100-0x810C */
140*4882a593Smuzhiyun 	s->ucgctl1 = intel_uncore_read(uncore, GEN6_UCGCTL1);
141*4882a593Smuzhiyun 	s->ucgctl3 = intel_uncore_read(uncore, GEN6_UCGCTL3);
142*4882a593Smuzhiyun 	s->rcgctl1 = intel_uncore_read(uncore, GEN6_RCGCTL1);
143*4882a593Smuzhiyun 	s->rcgctl2 = intel_uncore_read(uncore, GEN6_RCGCTL2);
144*4882a593Smuzhiyun 	s->rstctl = intel_uncore_read(uncore, GEN6_RSTCTL);
145*4882a593Smuzhiyun 	s->misccpctl = intel_uncore_read(uncore, GEN7_MISCCPCTL);
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	/* GPM 0xA000-0xAA84, 0x8000-0x80FC */
148*4882a593Smuzhiyun 	s->gfxpause = intel_uncore_read(uncore, GEN6_GFXPAUSE);
149*4882a593Smuzhiyun 	s->rpdeuhwtc = intel_uncore_read(uncore, GEN6_RPDEUHWTC);
150*4882a593Smuzhiyun 	s->rpdeuc = intel_uncore_read(uncore, GEN6_RPDEUC);
151*4882a593Smuzhiyun 	s->ecobus = intel_uncore_read(uncore, ECOBUS);
152*4882a593Smuzhiyun 	s->pwrdwnupctl = intel_uncore_read(uncore, VLV_PWRDWNUPCTL);
153*4882a593Smuzhiyun 	s->rp_down_timeout = intel_uncore_read(uncore, GEN6_RP_DOWN_TIMEOUT);
154*4882a593Smuzhiyun 	s->rp_deucsw = intel_uncore_read(uncore, GEN6_RPDEUCSW);
155*4882a593Smuzhiyun 	s->rcubmabdtmr = intel_uncore_read(uncore, GEN6_RCUBMABDTMR);
156*4882a593Smuzhiyun 	s->rcedata = intel_uncore_read(uncore, VLV_RCEDATA);
157*4882a593Smuzhiyun 	s->spare2gh = intel_uncore_read(uncore, VLV_SPAREG2H);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	/* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
160*4882a593Smuzhiyun 	s->gt_imr = intel_uncore_read(uncore, GTIMR);
161*4882a593Smuzhiyun 	s->gt_ier = intel_uncore_read(uncore, GTIER);
162*4882a593Smuzhiyun 	s->pm_imr = intel_uncore_read(uncore, GEN6_PMIMR);
163*4882a593Smuzhiyun 	s->pm_ier = intel_uncore_read(uncore, GEN6_PMIER);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
166*4882a593Smuzhiyun 		s->gt_scratch[i] = intel_uncore_read(uncore, GEN7_GT_SCRATCH(i));
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	/* GT SA CZ domain, 0x100000-0x138124 */
169*4882a593Smuzhiyun 	s->tilectl = intel_uncore_read(uncore, TILECTL);
170*4882a593Smuzhiyun 	s->gt_fifoctl = intel_uncore_read(uncore, GTFIFOCTL);
171*4882a593Smuzhiyun 	s->gtlc_wake_ctrl = intel_uncore_read(uncore, VLV_GTLC_WAKE_CTRL);
172*4882a593Smuzhiyun 	s->gtlc_survive = intel_uncore_read(uncore, VLV_GTLC_SURVIVABILITY_REG);
173*4882a593Smuzhiyun 	s->pmwgicz = intel_uncore_read(uncore, VLV_PMWGICZ);
174*4882a593Smuzhiyun 
175*4882a593Smuzhiyun 	/* Gunit-Display CZ domain, 0x182028-0x1821CF */
176*4882a593Smuzhiyun 	s->gu_ctl0 = intel_uncore_read(uncore, VLV_GU_CTL0);
177*4882a593Smuzhiyun 	s->gu_ctl1 = intel_uncore_read(uncore, VLV_GU_CTL1);
178*4882a593Smuzhiyun 	s->pcbr = intel_uncore_read(uncore, VLV_PCBR);
179*4882a593Smuzhiyun 	s->clock_gate_dis2 = intel_uncore_read(uncore, VLV_GUNIT_CLOCK_GATE2);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	/*
182*4882a593Smuzhiyun 	 * Not saving any of:
183*4882a593Smuzhiyun 	 * DFT,		0x9800-0x9EC0
184*4882a593Smuzhiyun 	 * SARB,	0xB000-0xB1FC
185*4882a593Smuzhiyun 	 * GAC,		0x5208-0x524C, 0x14000-0x14C000
186*4882a593Smuzhiyun 	 * PCI CFG
187*4882a593Smuzhiyun 	 */
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun 
vlv_restore_gunit_s0ix_state(struct drm_i915_private * i915)190*4882a593Smuzhiyun static void vlv_restore_gunit_s0ix_state(struct drm_i915_private *i915)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	struct vlv_s0ix_state *s = i915->vlv_s0ix_state;
193*4882a593Smuzhiyun 	struct intel_uncore *uncore = &i915->uncore;
194*4882a593Smuzhiyun 	u32 val;
195*4882a593Smuzhiyun 	int i;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	if (!s)
198*4882a593Smuzhiyun 		return;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	/* GAM 0x4000-0x4770 */
201*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN7_WR_WATERMARK, s->wr_watermark);
202*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN7_GFX_PRIO_CTRL, s->gfx_prio_ctrl);
203*4882a593Smuzhiyun 	intel_uncore_write(uncore, ARB_MODE, s->arb_mode | (0xffff << 16));
204*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN7_GFX_PEND_TLB0, s->gfx_pend_tlb0);
205*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN7_GFX_PEND_TLB1, s->gfx_pend_tlb1);
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(s->lra_limits); i++)
208*4882a593Smuzhiyun 		intel_uncore_write(uncore, GEN7_LRA_LIMITS(i), s->lra_limits[i]);
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN7_MEDIA_MAX_REQ_COUNT, s->media_max_req_count);
211*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN7_GFX_MAX_REQ_COUNT, s->gfx_max_req_count);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun 	intel_uncore_write(uncore, RENDER_HWS_PGA_GEN7, s->render_hwsp);
214*4882a593Smuzhiyun 	intel_uncore_write(uncore, GAM_ECOCHK, s->ecochk);
215*4882a593Smuzhiyun 	intel_uncore_write(uncore, BSD_HWS_PGA_GEN7, s->bsd_hwsp);
216*4882a593Smuzhiyun 	intel_uncore_write(uncore, BLT_HWS_PGA_GEN7, s->blt_hwsp);
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN7_TLB_RD_ADDR, s->tlb_rd_addr);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	/* MBC 0x9024-0x91D0, 0x8500 */
221*4882a593Smuzhiyun 	intel_uncore_write(uncore, VLV_G3DCTL, s->g3dctl);
222*4882a593Smuzhiyun 	intel_uncore_write(uncore, VLV_GSCKGCTL, s->gsckgctl);
223*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN6_MBCTL, s->mbctl);
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	/* GCP 0x9400-0x9424, 0x8100-0x810C */
226*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN6_UCGCTL1, s->ucgctl1);
227*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN6_UCGCTL3, s->ucgctl3);
228*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN6_RCGCTL1, s->rcgctl1);
229*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN6_RCGCTL2, s->rcgctl2);
230*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN6_RSTCTL, s->rstctl);
231*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN7_MISCCPCTL, s->misccpctl);
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	/* GPM 0xA000-0xAA84, 0x8000-0x80FC */
234*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN6_GFXPAUSE, s->gfxpause);
235*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN6_RPDEUHWTC, s->rpdeuhwtc);
236*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN6_RPDEUC, s->rpdeuc);
237*4882a593Smuzhiyun 	intel_uncore_write(uncore, ECOBUS, s->ecobus);
238*4882a593Smuzhiyun 	intel_uncore_write(uncore, VLV_PWRDWNUPCTL, s->pwrdwnupctl);
239*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN6_RP_DOWN_TIMEOUT, s->rp_down_timeout);
240*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN6_RPDEUCSW, s->rp_deucsw);
241*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN6_RCUBMABDTMR, s->rcubmabdtmr);
242*4882a593Smuzhiyun 	intel_uncore_write(uncore, VLV_RCEDATA, s->rcedata);
243*4882a593Smuzhiyun 	intel_uncore_write(uncore, VLV_SPAREG2H, s->spare2gh);
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	/* Display CZ domain, 0x4400C-0x4402C, 0x4F000-0x4F11F */
246*4882a593Smuzhiyun 	intel_uncore_write(uncore, GTIMR, s->gt_imr);
247*4882a593Smuzhiyun 	intel_uncore_write(uncore, GTIER, s->gt_ier);
248*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN6_PMIMR, s->pm_imr);
249*4882a593Smuzhiyun 	intel_uncore_write(uncore, GEN6_PMIER, s->pm_ier);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(s->gt_scratch); i++)
252*4882a593Smuzhiyun 		intel_uncore_write(uncore, GEN7_GT_SCRATCH(i), s->gt_scratch[i]);
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	/* GT SA CZ domain, 0x100000-0x138124 */
255*4882a593Smuzhiyun 	intel_uncore_write(uncore, TILECTL, s->tilectl);
256*4882a593Smuzhiyun 	intel_uncore_write(uncore, GTFIFOCTL, s->gt_fifoctl);
257*4882a593Smuzhiyun 	/*
258*4882a593Smuzhiyun 	 * Preserve the GT allow wake and GFX force clock bit, they are not
259*4882a593Smuzhiyun 	 * be restored, as they are used to control the s0ix suspend/resume
260*4882a593Smuzhiyun 	 * sequence by the caller.
261*4882a593Smuzhiyun 	 */
262*4882a593Smuzhiyun 	val = intel_uncore_read(uncore, VLV_GTLC_WAKE_CTRL);
263*4882a593Smuzhiyun 	val &= VLV_GTLC_ALLOWWAKEREQ;
264*4882a593Smuzhiyun 	val |= s->gtlc_wake_ctrl & ~VLV_GTLC_ALLOWWAKEREQ;
265*4882a593Smuzhiyun 	intel_uncore_write(uncore, VLV_GTLC_WAKE_CTRL, val);
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	val = intel_uncore_read(uncore, VLV_GTLC_SURVIVABILITY_REG);
268*4882a593Smuzhiyun 	val &= VLV_GFX_CLK_FORCE_ON_BIT;
269*4882a593Smuzhiyun 	val |= s->gtlc_survive & ~VLV_GFX_CLK_FORCE_ON_BIT;
270*4882a593Smuzhiyun 	intel_uncore_write(uncore, VLV_GTLC_SURVIVABILITY_REG, val);
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 	intel_uncore_write(uncore, VLV_PMWGICZ, s->pmwgicz);
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	/* Gunit-Display CZ domain, 0x182028-0x1821CF */
275*4882a593Smuzhiyun 	intel_uncore_write(uncore, VLV_GU_CTL0, s->gu_ctl0);
276*4882a593Smuzhiyun 	intel_uncore_write(uncore, VLV_GU_CTL1, s->gu_ctl1);
277*4882a593Smuzhiyun 	intel_uncore_write(uncore, VLV_PCBR, s->pcbr);
278*4882a593Smuzhiyun 	intel_uncore_write(uncore, VLV_GUNIT_CLOCK_GATE2, s->clock_gate_dis2);
279*4882a593Smuzhiyun }
280*4882a593Smuzhiyun 
vlv_wait_for_pw_status(struct drm_i915_private * i915,u32 mask,u32 val)281*4882a593Smuzhiyun static int vlv_wait_for_pw_status(struct drm_i915_private *i915,
282*4882a593Smuzhiyun 				  u32 mask, u32 val)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun 	i915_reg_t reg = VLV_GTLC_PW_STATUS;
285*4882a593Smuzhiyun 	u32 reg_value;
286*4882a593Smuzhiyun 	int ret;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	/* The HW does not like us polling for PW_STATUS frequently, so
289*4882a593Smuzhiyun 	 * use the sleeping loop rather than risk the busy spin within
290*4882a593Smuzhiyun 	 * intel_wait_for_register().
291*4882a593Smuzhiyun 	 *
292*4882a593Smuzhiyun 	 * Transitioning between RC6 states should be at most 2ms (see
293*4882a593Smuzhiyun 	 * valleyview_enable_rps) so use a 3ms timeout.
294*4882a593Smuzhiyun 	 */
295*4882a593Smuzhiyun 	ret = wait_for(((reg_value =
296*4882a593Smuzhiyun 			 intel_uncore_read_notrace(&i915->uncore, reg)) & mask)
297*4882a593Smuzhiyun 		       == val, 3);
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	/* just trace the final value */
300*4882a593Smuzhiyun 	trace_i915_reg_rw(false, reg, reg_value, sizeof(reg_value), true);
301*4882a593Smuzhiyun 
302*4882a593Smuzhiyun 	return ret;
303*4882a593Smuzhiyun }
304*4882a593Smuzhiyun 
vlv_force_gfx_clock(struct drm_i915_private * i915,bool force_on)305*4882a593Smuzhiyun static int vlv_force_gfx_clock(struct drm_i915_private *i915, bool force_on)
306*4882a593Smuzhiyun {
307*4882a593Smuzhiyun 	struct intel_uncore *uncore = &i915->uncore;
308*4882a593Smuzhiyun 	u32 val;
309*4882a593Smuzhiyun 	int err;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	val = intel_uncore_read(uncore, VLV_GTLC_SURVIVABILITY_REG);
312*4882a593Smuzhiyun 	val &= ~VLV_GFX_CLK_FORCE_ON_BIT;
313*4882a593Smuzhiyun 	if (force_on)
314*4882a593Smuzhiyun 		val |= VLV_GFX_CLK_FORCE_ON_BIT;
315*4882a593Smuzhiyun 	intel_uncore_write(uncore, VLV_GTLC_SURVIVABILITY_REG, val);
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	if (!force_on)
318*4882a593Smuzhiyun 		return 0;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	err = intel_wait_for_register(uncore,
321*4882a593Smuzhiyun 				      VLV_GTLC_SURVIVABILITY_REG,
322*4882a593Smuzhiyun 				      VLV_GFX_CLK_STATUS_BIT,
323*4882a593Smuzhiyun 				      VLV_GFX_CLK_STATUS_BIT,
324*4882a593Smuzhiyun 				      20);
325*4882a593Smuzhiyun 	if (err)
326*4882a593Smuzhiyun 		drm_err(&i915->drm,
327*4882a593Smuzhiyun 			"timeout waiting for GFX clock force-on (%08x)\n",
328*4882a593Smuzhiyun 			intel_uncore_read(uncore, VLV_GTLC_SURVIVABILITY_REG));
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	return err;
331*4882a593Smuzhiyun }
332*4882a593Smuzhiyun 
vlv_allow_gt_wake(struct drm_i915_private * i915,bool allow)333*4882a593Smuzhiyun static int vlv_allow_gt_wake(struct drm_i915_private *i915, bool allow)
334*4882a593Smuzhiyun {
335*4882a593Smuzhiyun 	struct intel_uncore *uncore = &i915->uncore;
336*4882a593Smuzhiyun 	u32 mask;
337*4882a593Smuzhiyun 	u32 val;
338*4882a593Smuzhiyun 	int err;
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 	val = intel_uncore_read(uncore, VLV_GTLC_WAKE_CTRL);
341*4882a593Smuzhiyun 	val &= ~VLV_GTLC_ALLOWWAKEREQ;
342*4882a593Smuzhiyun 	if (allow)
343*4882a593Smuzhiyun 		val |= VLV_GTLC_ALLOWWAKEREQ;
344*4882a593Smuzhiyun 	intel_uncore_write(uncore, VLV_GTLC_WAKE_CTRL, val);
345*4882a593Smuzhiyun 	intel_uncore_posting_read(uncore, VLV_GTLC_WAKE_CTRL);
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	mask = VLV_GTLC_ALLOWWAKEACK;
348*4882a593Smuzhiyun 	val = allow ? mask : 0;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	err = vlv_wait_for_pw_status(i915, mask, val);
351*4882a593Smuzhiyun 	if (err)
352*4882a593Smuzhiyun 		drm_err(&i915->drm, "timeout disabling GT waking\n");
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	return err;
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun 
vlv_wait_for_gt_wells(struct drm_i915_private * dev_priv,bool wait_for_on)357*4882a593Smuzhiyun static void vlv_wait_for_gt_wells(struct drm_i915_private *dev_priv,
358*4882a593Smuzhiyun 				  bool wait_for_on)
359*4882a593Smuzhiyun {
360*4882a593Smuzhiyun 	u32 mask;
361*4882a593Smuzhiyun 	u32 val;
362*4882a593Smuzhiyun 
363*4882a593Smuzhiyun 	mask = VLV_GTLC_PW_MEDIA_STATUS_MASK | VLV_GTLC_PW_RENDER_STATUS_MASK;
364*4882a593Smuzhiyun 	val = wait_for_on ? mask : 0;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	/*
367*4882a593Smuzhiyun 	 * RC6 transitioning can be delayed up to 2 msec (see
368*4882a593Smuzhiyun 	 * valleyview_enable_rps), use 3 msec for safety.
369*4882a593Smuzhiyun 	 *
370*4882a593Smuzhiyun 	 * This can fail to turn off the rc6 if the GPU is stuck after a failed
371*4882a593Smuzhiyun 	 * reset and we are trying to force the machine to sleep.
372*4882a593Smuzhiyun 	 */
373*4882a593Smuzhiyun 	if (vlv_wait_for_pw_status(dev_priv, mask, val))
374*4882a593Smuzhiyun 		drm_dbg(&dev_priv->drm,
375*4882a593Smuzhiyun 			"timeout waiting for GT wells to go %s\n",
376*4882a593Smuzhiyun 			onoff(wait_for_on));
377*4882a593Smuzhiyun }
378*4882a593Smuzhiyun 
vlv_check_no_gt_access(struct drm_i915_private * i915)379*4882a593Smuzhiyun static void vlv_check_no_gt_access(struct drm_i915_private *i915)
380*4882a593Smuzhiyun {
381*4882a593Smuzhiyun 	struct intel_uncore *uncore = &i915->uncore;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	if (!(intel_uncore_read(uncore, VLV_GTLC_PW_STATUS) & VLV_GTLC_ALLOWWAKEERR))
384*4882a593Smuzhiyun 		return;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	drm_dbg(&i915->drm, "GT register access while GT waking disabled\n");
387*4882a593Smuzhiyun 	intel_uncore_write(uncore, VLV_GTLC_PW_STATUS, VLV_GTLC_ALLOWWAKEERR);
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun 
vlv_suspend_complete(struct drm_i915_private * dev_priv)390*4882a593Smuzhiyun int vlv_suspend_complete(struct drm_i915_private *dev_priv)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun 	u32 mask;
393*4882a593Smuzhiyun 	int err;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
396*4882a593Smuzhiyun 		return 0;
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	/*
399*4882a593Smuzhiyun 	 * Bspec defines the following GT well on flags as debug only, so
400*4882a593Smuzhiyun 	 * don't treat them as hard failures.
401*4882a593Smuzhiyun 	 */
402*4882a593Smuzhiyun 	vlv_wait_for_gt_wells(dev_priv, false);
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	mask = VLV_GTLC_RENDER_CTX_EXISTS | VLV_GTLC_MEDIA_CTX_EXISTS;
405*4882a593Smuzhiyun 	drm_WARN_ON(&dev_priv->drm,
406*4882a593Smuzhiyun 		    (intel_uncore_read(&dev_priv->uncore, VLV_GTLC_WAKE_CTRL) & mask) != mask);
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	vlv_check_no_gt_access(dev_priv);
409*4882a593Smuzhiyun 
410*4882a593Smuzhiyun 	err = vlv_force_gfx_clock(dev_priv, true);
411*4882a593Smuzhiyun 	if (err)
412*4882a593Smuzhiyun 		goto err1;
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	err = vlv_allow_gt_wake(dev_priv, false);
415*4882a593Smuzhiyun 	if (err)
416*4882a593Smuzhiyun 		goto err2;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	vlv_save_gunit_s0ix_state(dev_priv);
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	err = vlv_force_gfx_clock(dev_priv, false);
421*4882a593Smuzhiyun 	if (err)
422*4882a593Smuzhiyun 		goto err2;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	return 0;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun err2:
427*4882a593Smuzhiyun 	/* For safety always re-enable waking and disable gfx clock forcing */
428*4882a593Smuzhiyun 	vlv_allow_gt_wake(dev_priv, true);
429*4882a593Smuzhiyun err1:
430*4882a593Smuzhiyun 	vlv_force_gfx_clock(dev_priv, false);
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	return err;
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun 
vlv_resume_prepare(struct drm_i915_private * dev_priv,bool rpm_resume)435*4882a593Smuzhiyun int vlv_resume_prepare(struct drm_i915_private *dev_priv, bool rpm_resume)
436*4882a593Smuzhiyun {
437*4882a593Smuzhiyun 	int err;
438*4882a593Smuzhiyun 	int ret;
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv))
441*4882a593Smuzhiyun 		return 0;
442*4882a593Smuzhiyun 
443*4882a593Smuzhiyun 	/*
444*4882a593Smuzhiyun 	 * If any of the steps fail just try to continue, that's the best we
445*4882a593Smuzhiyun 	 * can do at this point. Return the first error code (which will also
446*4882a593Smuzhiyun 	 * leave RPM permanently disabled).
447*4882a593Smuzhiyun 	 */
448*4882a593Smuzhiyun 	ret = vlv_force_gfx_clock(dev_priv, true);
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	vlv_restore_gunit_s0ix_state(dev_priv);
451*4882a593Smuzhiyun 
452*4882a593Smuzhiyun 	err = vlv_allow_gt_wake(dev_priv, true);
453*4882a593Smuzhiyun 	if (!ret)
454*4882a593Smuzhiyun 		ret = err;
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 	err = vlv_force_gfx_clock(dev_priv, false);
457*4882a593Smuzhiyun 	if (!ret)
458*4882a593Smuzhiyun 		ret = err;
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	vlv_check_no_gt_access(dev_priv);
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	if (rpm_resume)
463*4882a593Smuzhiyun 		intel_init_clock_gating(dev_priv);
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	return ret;
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun 
vlv_suspend_init(struct drm_i915_private * i915)468*4882a593Smuzhiyun int vlv_suspend_init(struct drm_i915_private *i915)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun 	if (!IS_VALLEYVIEW(i915))
471*4882a593Smuzhiyun 		return 0;
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	/* we write all the values in the struct, so no need to zero it out */
474*4882a593Smuzhiyun 	i915->vlv_s0ix_state = kmalloc(sizeof(*i915->vlv_s0ix_state),
475*4882a593Smuzhiyun 				       GFP_KERNEL);
476*4882a593Smuzhiyun 	if (!i915->vlv_s0ix_state)
477*4882a593Smuzhiyun 		return -ENOMEM;
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	return 0;
480*4882a593Smuzhiyun }
481*4882a593Smuzhiyun 
vlv_suspend_cleanup(struct drm_i915_private * i915)482*4882a593Smuzhiyun void vlv_suspend_cleanup(struct drm_i915_private *i915)
483*4882a593Smuzhiyun {
484*4882a593Smuzhiyun 	if (!i915->vlv_s0ix_state)
485*4882a593Smuzhiyun 		return;
486*4882a593Smuzhiyun 
487*4882a593Smuzhiyun 	kfree(i915->vlv_s0ix_state);
488*4882a593Smuzhiyun 	i915->vlv_s0ix_state = NULL;
489*4882a593Smuzhiyun }
490