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