xref: /dflybsd-src/sys/dev/drm/i915/intel_psr.c (revision 2c9916cd50d5c4c4defa089bebed8c8865efa896)
1*2c9916cdSFrançois Tigeot /*
2*2c9916cdSFrançois Tigeot  * Copyright © 2014 Intel Corporation
3*2c9916cdSFrançois Tigeot  *
4*2c9916cdSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5*2c9916cdSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6*2c9916cdSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7*2c9916cdSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*2c9916cdSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9*2c9916cdSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10*2c9916cdSFrançois Tigeot  *
11*2c9916cdSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
12*2c9916cdSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
13*2c9916cdSFrançois Tigeot  * Software.
14*2c9916cdSFrançois Tigeot  *
15*2c9916cdSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*2c9916cdSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*2c9916cdSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*2c9916cdSFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*2c9916cdSFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*2c9916cdSFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*2c9916cdSFrançois Tigeot  * DEALINGS IN THE SOFTWARE.
22*2c9916cdSFrançois Tigeot  */
23*2c9916cdSFrançois Tigeot 
24*2c9916cdSFrançois Tigeot /**
25*2c9916cdSFrançois Tigeot  * DOC: Panel Self Refresh (PSR/SRD)
26*2c9916cdSFrançois Tigeot  *
27*2c9916cdSFrançois Tigeot  * Since Haswell Display controller supports Panel Self-Refresh on display
28*2c9916cdSFrançois Tigeot  * panels witch have a remote frame buffer (RFB) implemented according to PSR
29*2c9916cdSFrançois Tigeot  * spec in eDP1.3. PSR feature allows the display to go to lower standby states
30*2c9916cdSFrançois Tigeot  * when system is idle but display is on as it eliminates display refresh
31*2c9916cdSFrançois Tigeot  * request to DDR memory completely as long as the frame buffer for that
32*2c9916cdSFrançois Tigeot  * display is unchanged.
33*2c9916cdSFrançois Tigeot  *
34*2c9916cdSFrançois Tigeot  * Panel Self Refresh must be supported by both Hardware (source) and
35*2c9916cdSFrançois Tigeot  * Panel (sink).
36*2c9916cdSFrançois Tigeot  *
37*2c9916cdSFrançois Tigeot  * PSR saves power by caching the framebuffer in the panel RFB, which allows us
38*2c9916cdSFrançois Tigeot  * to power down the link and memory controller. For DSI panels the same idea
39*2c9916cdSFrançois Tigeot  * is called "manual mode".
40*2c9916cdSFrançois Tigeot  *
41*2c9916cdSFrançois Tigeot  * The implementation uses the hardware-based PSR support which automatically
42*2c9916cdSFrançois Tigeot  * enters/exits self-refresh mode. The hardware takes care of sending the
43*2c9916cdSFrançois Tigeot  * required DP aux message and could even retrain the link (that part isn't
44*2c9916cdSFrançois Tigeot  * enabled yet though). The hardware also keeps track of any frontbuffer
45*2c9916cdSFrançois Tigeot  * changes to know when to exit self-refresh mode again. Unfortunately that
46*2c9916cdSFrançois Tigeot  * part doesn't work too well, hence why the i915 PSR support uses the
47*2c9916cdSFrançois Tigeot  * software frontbuffer tracking to make sure it doesn't miss a screen
48*2c9916cdSFrançois Tigeot  * update. For this integration intel_psr_invalidate() and intel_psr_flush()
49*2c9916cdSFrançois Tigeot  * get called by the frontbuffer tracking code. Note that because of locking
50*2c9916cdSFrançois Tigeot  * issues the self-refresh re-enable code is done from a work queue, which
51*2c9916cdSFrançois Tigeot  * must be correctly synchronized/cancelled when shutting down the pipe."
52*2c9916cdSFrançois Tigeot  */
53*2c9916cdSFrançois Tigeot 
54*2c9916cdSFrançois Tigeot #include <drm/drmP.h>
55*2c9916cdSFrançois Tigeot 
56*2c9916cdSFrançois Tigeot #include "intel_drv.h"
57*2c9916cdSFrançois Tigeot #include "i915_drv.h"
58*2c9916cdSFrançois Tigeot 
59*2c9916cdSFrançois Tigeot static bool is_edp_psr(struct intel_dp *intel_dp)
60*2c9916cdSFrançois Tigeot {
61*2c9916cdSFrançois Tigeot 	return intel_dp->psr_dpcd[0] & DP_PSR_IS_SUPPORTED;
62*2c9916cdSFrançois Tigeot }
63*2c9916cdSFrançois Tigeot 
64*2c9916cdSFrançois Tigeot static bool vlv_is_psr_active_on_pipe(struct drm_device *dev, int pipe)
65*2c9916cdSFrançois Tigeot {
66*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
67*2c9916cdSFrançois Tigeot 	uint32_t val;
68*2c9916cdSFrançois Tigeot 
69*2c9916cdSFrançois Tigeot 	val = I915_READ(VLV_PSRSTAT(pipe)) &
70*2c9916cdSFrançois Tigeot 	      VLV_EDP_PSR_CURR_STATE_MASK;
71*2c9916cdSFrançois Tigeot 	return (val == VLV_EDP_PSR_ACTIVE_NORFB_UP) ||
72*2c9916cdSFrançois Tigeot 	       (val == VLV_EDP_PSR_ACTIVE_SF_UPDATE);
73*2c9916cdSFrançois Tigeot }
74*2c9916cdSFrançois Tigeot 
75*2c9916cdSFrançois Tigeot static void intel_psr_write_vsc(struct intel_dp *intel_dp,
76*2c9916cdSFrançois Tigeot 				    struct edp_vsc_psr *vsc_psr)
77*2c9916cdSFrançois Tigeot {
78*2c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
79*2c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
80*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
81*2c9916cdSFrançois Tigeot 	struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
82*2c9916cdSFrançois Tigeot 	u32 ctl_reg = HSW_TVIDEO_DIP_CTL(crtc->config->cpu_transcoder);
83*2c9916cdSFrançois Tigeot 	u32 data_reg = HSW_TVIDEO_DIP_VSC_DATA(crtc->config->cpu_transcoder);
84*2c9916cdSFrançois Tigeot 	uint32_t *data = (uint32_t *) vsc_psr;
85*2c9916cdSFrançois Tigeot 	unsigned int i;
86*2c9916cdSFrançois Tigeot 
87*2c9916cdSFrançois Tigeot 	/* As per BSPec (Pipe Video Data Island Packet), we need to disable
88*2c9916cdSFrançois Tigeot 	   the video DIP being updated before program video DIP data buffer
89*2c9916cdSFrançois Tigeot 	   registers for DIP being updated. */
90*2c9916cdSFrançois Tigeot 	I915_WRITE(ctl_reg, 0);
91*2c9916cdSFrançois Tigeot 	POSTING_READ(ctl_reg);
92*2c9916cdSFrançois Tigeot 
93*2c9916cdSFrançois Tigeot 	for (i = 0; i < VIDEO_DIP_VSC_DATA_SIZE; i += 4) {
94*2c9916cdSFrançois Tigeot 		if (i < sizeof(struct edp_vsc_psr))
95*2c9916cdSFrançois Tigeot 			I915_WRITE(data_reg + i, *data++);
96*2c9916cdSFrançois Tigeot 		else
97*2c9916cdSFrançois Tigeot 			I915_WRITE(data_reg + i, 0);
98*2c9916cdSFrançois Tigeot 	}
99*2c9916cdSFrançois Tigeot 
100*2c9916cdSFrançois Tigeot 	I915_WRITE(ctl_reg, VIDEO_DIP_ENABLE_VSC_HSW);
101*2c9916cdSFrançois Tigeot 	POSTING_READ(ctl_reg);
102*2c9916cdSFrançois Tigeot }
103*2c9916cdSFrançois Tigeot 
104*2c9916cdSFrançois Tigeot static void vlv_psr_setup_vsc(struct intel_dp *intel_dp)
105*2c9916cdSFrançois Tigeot {
106*2c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
107*2c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
108*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
109*2c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
110*2c9916cdSFrançois Tigeot 	enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
111*2c9916cdSFrançois Tigeot 	uint32_t val;
112*2c9916cdSFrançois Tigeot 
113*2c9916cdSFrançois Tigeot 	/* VLV auto-generate VSC package as per EDP 1.3 spec, Table 3.10 */
114*2c9916cdSFrançois Tigeot 	val  = I915_READ(VLV_VSCSDP(pipe));
115*2c9916cdSFrançois Tigeot 	val &= ~VLV_EDP_PSR_SDP_FREQ_MASK;
116*2c9916cdSFrançois Tigeot 	val |= VLV_EDP_PSR_SDP_FREQ_EVFRAME;
117*2c9916cdSFrançois Tigeot 	I915_WRITE(VLV_VSCSDP(pipe), val);
118*2c9916cdSFrançois Tigeot }
119*2c9916cdSFrançois Tigeot 
120*2c9916cdSFrançois Tigeot static void hsw_psr_setup_vsc(struct intel_dp *intel_dp)
121*2c9916cdSFrançois Tigeot {
122*2c9916cdSFrançois Tigeot 	struct edp_vsc_psr psr_vsc;
123*2c9916cdSFrançois Tigeot 
124*2c9916cdSFrançois Tigeot 	/* Prepare VSC packet as per EDP 1.3 spec, Table 3.10 */
125*2c9916cdSFrançois Tigeot 	memset(&psr_vsc, 0, sizeof(psr_vsc));
126*2c9916cdSFrançois Tigeot 	psr_vsc.sdp_header.HB0 = 0;
127*2c9916cdSFrançois Tigeot 	psr_vsc.sdp_header.HB1 = 0x7;
128*2c9916cdSFrançois Tigeot 	psr_vsc.sdp_header.HB2 = 0x2;
129*2c9916cdSFrançois Tigeot 	psr_vsc.sdp_header.HB3 = 0x8;
130*2c9916cdSFrançois Tigeot 	intel_psr_write_vsc(intel_dp, &psr_vsc);
131*2c9916cdSFrançois Tigeot }
132*2c9916cdSFrançois Tigeot 
133*2c9916cdSFrançois Tigeot static void vlv_psr_enable_sink(struct intel_dp *intel_dp)
134*2c9916cdSFrançois Tigeot {
135*2c9916cdSFrançois Tigeot 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG,
136*2c9916cdSFrançois Tigeot 			   DP_PSR_ENABLE);
137*2c9916cdSFrançois Tigeot }
138*2c9916cdSFrançois Tigeot 
139*2c9916cdSFrançois Tigeot static void hsw_psr_enable_sink(struct intel_dp *intel_dp)
140*2c9916cdSFrançois Tigeot {
141*2c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
142*2c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
143*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
144*2c9916cdSFrançois Tigeot 	uint32_t aux_clock_divider;
145*2c9916cdSFrançois Tigeot 	uint32_t aux_data_reg, aux_ctl_reg;
146*2c9916cdSFrançois Tigeot 	int precharge = 0x3;
147*2c9916cdSFrançois Tigeot 	static const uint8_t aux_msg[] = {
148*2c9916cdSFrançois Tigeot 		[0] = DP_AUX_NATIVE_WRITE << 4,
149*2c9916cdSFrançois Tigeot 		[1] = DP_SET_POWER >> 8,
150*2c9916cdSFrançois Tigeot 		[2] = DP_SET_POWER & 0xff,
151*2c9916cdSFrançois Tigeot 		[3] = 1 - 1,
152*2c9916cdSFrançois Tigeot 		[4] = DP_SET_POWER_D0,
153*2c9916cdSFrançois Tigeot 	};
154*2c9916cdSFrançois Tigeot 	int i;
155*2c9916cdSFrançois Tigeot 
156*2c9916cdSFrançois Tigeot 	BUILD_BUG_ON(sizeof(aux_msg) > 20);
157*2c9916cdSFrançois Tigeot 
158*2c9916cdSFrançois Tigeot 	aux_clock_divider = intel_dp->get_aux_clock_divider(intel_dp, 0);
159*2c9916cdSFrançois Tigeot 
160*2c9916cdSFrançois Tigeot 	/* Enable PSR in sink */
161*2c9916cdSFrançois Tigeot 	if (dev_priv->psr.link_standby)
162*2c9916cdSFrançois Tigeot 		drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG,
163*2c9916cdSFrançois Tigeot 				   DP_PSR_ENABLE | DP_PSR_MAIN_LINK_ACTIVE);
164*2c9916cdSFrançois Tigeot 	else
165*2c9916cdSFrançois Tigeot 		drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG,
166*2c9916cdSFrançois Tigeot 				   DP_PSR_ENABLE & ~DP_PSR_MAIN_LINK_ACTIVE);
167*2c9916cdSFrançois Tigeot 
168*2c9916cdSFrançois Tigeot 	aux_data_reg = (INTEL_INFO(dev)->gen >= 9) ?
169*2c9916cdSFrançois Tigeot 				DPA_AUX_CH_DATA1 : EDP_PSR_AUX_DATA1(dev);
170*2c9916cdSFrançois Tigeot 	aux_ctl_reg = (INTEL_INFO(dev)->gen >= 9) ?
171*2c9916cdSFrançois Tigeot 				DPA_AUX_CH_CTL : EDP_PSR_AUX_CTL(dev);
172*2c9916cdSFrançois Tigeot 
173*2c9916cdSFrançois Tigeot 	/* Setup AUX registers */
174*2c9916cdSFrançois Tigeot 	for (i = 0; i < sizeof(aux_msg); i += 4)
175*2c9916cdSFrançois Tigeot 		I915_WRITE(aux_data_reg + i,
176*2c9916cdSFrançois Tigeot 			   intel_dp_pack_aux(&aux_msg[i], sizeof(aux_msg) - i));
177*2c9916cdSFrançois Tigeot 
178*2c9916cdSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 9) {
179*2c9916cdSFrançois Tigeot 		uint32_t val;
180*2c9916cdSFrançois Tigeot 
181*2c9916cdSFrançois Tigeot 		val = I915_READ(aux_ctl_reg);
182*2c9916cdSFrançois Tigeot 		val &= ~DP_AUX_CH_CTL_TIME_OUT_MASK;
183*2c9916cdSFrançois Tigeot 		val |= DP_AUX_CH_CTL_TIME_OUT_1600us;
184*2c9916cdSFrançois Tigeot 		val &= ~DP_AUX_CH_CTL_MESSAGE_SIZE_MASK;
185*2c9916cdSFrançois Tigeot 		val |= (sizeof(aux_msg) << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
186*2c9916cdSFrançois Tigeot 		/* Use hardcoded data values for PSR */
187*2c9916cdSFrançois Tigeot 		val &= ~DP_AUX_CH_CTL_PSR_DATA_AUX_REG_SKL;
188*2c9916cdSFrançois Tigeot 		I915_WRITE(aux_ctl_reg, val);
189*2c9916cdSFrançois Tigeot 	} else {
190*2c9916cdSFrançois Tigeot 		I915_WRITE(aux_ctl_reg,
191*2c9916cdSFrançois Tigeot 		   DP_AUX_CH_CTL_TIME_OUT_400us |
192*2c9916cdSFrançois Tigeot 		   (sizeof(aux_msg) << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
193*2c9916cdSFrançois Tigeot 		   (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
194*2c9916cdSFrançois Tigeot 		   (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT));
195*2c9916cdSFrançois Tigeot 	}
196*2c9916cdSFrançois Tigeot }
197*2c9916cdSFrançois Tigeot 
198*2c9916cdSFrançois Tigeot static void vlv_psr_enable_source(struct intel_dp *intel_dp)
199*2c9916cdSFrançois Tigeot {
200*2c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
201*2c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
202*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
203*2c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = dig_port->base.base.crtc;
204*2c9916cdSFrançois Tigeot 	enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
205*2c9916cdSFrançois Tigeot 
206*2c9916cdSFrançois Tigeot 	/* Transition from PSR_state 0 to PSR_state 1, i.e. PSR Inactive */
207*2c9916cdSFrançois Tigeot 	I915_WRITE(VLV_PSRCTL(pipe),
208*2c9916cdSFrançois Tigeot 		   VLV_EDP_PSR_MODE_SW_TIMER |
209*2c9916cdSFrançois Tigeot 		   VLV_EDP_PSR_SRC_TRANSMITTER_STATE |
210*2c9916cdSFrançois Tigeot 		   VLV_EDP_PSR_ENABLE);
211*2c9916cdSFrançois Tigeot }
212*2c9916cdSFrançois Tigeot 
213*2c9916cdSFrançois Tigeot static void vlv_psr_activate(struct intel_dp *intel_dp)
214*2c9916cdSFrançois Tigeot {
215*2c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
216*2c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
217*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
218*2c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = dig_port->base.base.crtc;
219*2c9916cdSFrançois Tigeot 	enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
220*2c9916cdSFrançois Tigeot 
221*2c9916cdSFrançois Tigeot 	/* Let's do the transition from PSR_state 1 to PSR_state 2
222*2c9916cdSFrançois Tigeot 	 * that is PSR transition to active - static frame transmission.
223*2c9916cdSFrançois Tigeot 	 * Then Hardware is responsible for the transition to PSR_state 3
224*2c9916cdSFrançois Tigeot 	 * that is PSR active - no Remote Frame Buffer (RFB) update.
225*2c9916cdSFrançois Tigeot 	 */
226*2c9916cdSFrançois Tigeot 	I915_WRITE(VLV_PSRCTL(pipe), I915_READ(VLV_PSRCTL(pipe)) |
227*2c9916cdSFrançois Tigeot 		   VLV_EDP_PSR_ACTIVE_ENTRY);
228*2c9916cdSFrançois Tigeot }
229*2c9916cdSFrançois Tigeot 
230*2c9916cdSFrançois Tigeot static void hsw_psr_enable_source(struct intel_dp *intel_dp)
231*2c9916cdSFrançois Tigeot {
232*2c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
233*2c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
234*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
235*2c9916cdSFrançois Tigeot 	uint32_t max_sleep_time = 0x1f;
236*2c9916cdSFrançois Tigeot 	/* Lately it was identified that depending on panel idle frame count
237*2c9916cdSFrançois Tigeot 	 * calculated at HW can be off by 1. So let's use what came
238*2c9916cdSFrançois Tigeot 	 * from VBT + 1 and at minimum 2 to be on the safe side.
239*2c9916cdSFrançois Tigeot 	 */
240*2c9916cdSFrançois Tigeot 	uint32_t idle_frames = dev_priv->vbt.psr.idle_frames ?
241*2c9916cdSFrançois Tigeot 			       dev_priv->vbt.psr.idle_frames + 1 : 2;
242*2c9916cdSFrançois Tigeot 	uint32_t val = 0x0;
243*2c9916cdSFrançois Tigeot 	const uint32_t link_entry_time = EDP_PSR_MIN_LINK_ENTRY_TIME_8_LINES;
244*2c9916cdSFrançois Tigeot 
245*2c9916cdSFrançois Tigeot 	if (dev_priv->psr.link_standby) {
246*2c9916cdSFrançois Tigeot 		val |= EDP_PSR_LINK_STANDBY;
247*2c9916cdSFrançois Tigeot 		val |= EDP_PSR_TP2_TP3_TIME_0us;
248*2c9916cdSFrançois Tigeot 		val |= EDP_PSR_TP1_TIME_0us;
249*2c9916cdSFrançois Tigeot 		val |= EDP_PSR_SKIP_AUX_EXIT;
250*2c9916cdSFrançois Tigeot 	} else
251*2c9916cdSFrançois Tigeot 		val |= EDP_PSR_LINK_DISABLE;
252*2c9916cdSFrançois Tigeot 
253*2c9916cdSFrançois Tigeot 	I915_WRITE(EDP_PSR_CTL(dev), val |
254*2c9916cdSFrançois Tigeot 		   (IS_BROADWELL(dev) ? 0 : link_entry_time) |
255*2c9916cdSFrançois Tigeot 		   max_sleep_time << EDP_PSR_MAX_SLEEP_TIME_SHIFT |
256*2c9916cdSFrançois Tigeot 		   idle_frames << EDP_PSR_IDLE_FRAME_SHIFT |
257*2c9916cdSFrançois Tigeot 		   EDP_PSR_ENABLE);
258*2c9916cdSFrançois Tigeot }
259*2c9916cdSFrançois Tigeot 
260*2c9916cdSFrançois Tigeot static bool intel_psr_match_conditions(struct intel_dp *intel_dp)
261*2c9916cdSFrançois Tigeot {
262*2c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
263*2c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
264*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
265*2c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = dig_port->base.base.crtc;
266*2c9916cdSFrançois Tigeot 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
267*2c9916cdSFrançois Tigeot 
268*2c9916cdSFrançois Tigeot 	lockdep_assert_held(&dev_priv->psr.lock);
269*2c9916cdSFrançois Tigeot 	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
270*2c9916cdSFrançois Tigeot 	WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
271*2c9916cdSFrançois Tigeot 
272*2c9916cdSFrançois Tigeot 	dev_priv->psr.source_ok = false;
273*2c9916cdSFrançois Tigeot 
274*2c9916cdSFrançois Tigeot 	if (IS_HASWELL(dev) && dig_port->port != PORT_A) {
275*2c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("HSW ties PSR to DDI A (eDP)\n");
276*2c9916cdSFrançois Tigeot 		return false;
277*2c9916cdSFrançois Tigeot 	}
278*2c9916cdSFrançois Tigeot 
279*2c9916cdSFrançois Tigeot 	if (!i915.enable_psr) {
280*2c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR disable by flag\n");
281*2c9916cdSFrançois Tigeot 		return false;
282*2c9916cdSFrançois Tigeot 	}
283*2c9916cdSFrançois Tigeot 
284*2c9916cdSFrançois Tigeot 	if (IS_HASWELL(dev) &&
285*2c9916cdSFrançois Tigeot 	    I915_READ(HSW_STEREO_3D_CTL(intel_crtc->config->cpu_transcoder)) &
286*2c9916cdSFrançois Tigeot 		      S3D_ENABLE) {
287*2c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR condition failed: Stereo 3D is Enabled\n");
288*2c9916cdSFrançois Tigeot 		return false;
289*2c9916cdSFrançois Tigeot 	}
290*2c9916cdSFrançois Tigeot 
291*2c9916cdSFrançois Tigeot 	if (IS_HASWELL(dev) &&
292*2c9916cdSFrançois Tigeot 	    intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
293*2c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR condition failed: Interlaced is Enabled\n");
294*2c9916cdSFrançois Tigeot 		return false;
295*2c9916cdSFrançois Tigeot 	}
296*2c9916cdSFrançois Tigeot 
297*2c9916cdSFrançois Tigeot 	dev_priv->psr.source_ok = true;
298*2c9916cdSFrançois Tigeot 	return true;
299*2c9916cdSFrançois Tigeot }
300*2c9916cdSFrançois Tigeot 
301*2c9916cdSFrançois Tigeot static void intel_psr_activate(struct intel_dp *intel_dp)
302*2c9916cdSFrançois Tigeot {
303*2c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
304*2c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
305*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
306*2c9916cdSFrançois Tigeot 
307*2c9916cdSFrançois Tigeot 	WARN_ON(I915_READ(EDP_PSR_CTL(dev)) & EDP_PSR_ENABLE);
308*2c9916cdSFrançois Tigeot 	WARN_ON(dev_priv->psr.active);
309*2c9916cdSFrançois Tigeot 	lockdep_assert_held(&dev_priv->psr.lock);
310*2c9916cdSFrançois Tigeot 
311*2c9916cdSFrançois Tigeot 	/* Enable/Re-enable PSR on the host */
312*2c9916cdSFrançois Tigeot 	if (HAS_DDI(dev))
313*2c9916cdSFrançois Tigeot 		/* On HSW+ after we enable PSR on source it will activate it
314*2c9916cdSFrançois Tigeot 		 * as soon as it match configure idle_frame count. So
315*2c9916cdSFrançois Tigeot 		 * we just actually enable it here on activation time.
316*2c9916cdSFrançois Tigeot 		 */
317*2c9916cdSFrançois Tigeot 		hsw_psr_enable_source(intel_dp);
318*2c9916cdSFrançois Tigeot 	else
319*2c9916cdSFrançois Tigeot 		vlv_psr_activate(intel_dp);
320*2c9916cdSFrançois Tigeot 
321*2c9916cdSFrançois Tigeot 	dev_priv->psr.active = true;
322*2c9916cdSFrançois Tigeot }
323*2c9916cdSFrançois Tigeot 
324*2c9916cdSFrançois Tigeot /**
325*2c9916cdSFrançois Tigeot  * intel_psr_enable - Enable PSR
326*2c9916cdSFrançois Tigeot  * @intel_dp: Intel DP
327*2c9916cdSFrançois Tigeot  *
328*2c9916cdSFrançois Tigeot  * This function can only be called after the pipe is fully trained and enabled.
329*2c9916cdSFrançois Tigeot  */
330*2c9916cdSFrançois Tigeot void intel_psr_enable(struct intel_dp *intel_dp)
331*2c9916cdSFrançois Tigeot {
332*2c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
333*2c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
334*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
335*2c9916cdSFrançois Tigeot 
336*2c9916cdSFrançois Tigeot 	if (!HAS_PSR(dev)) {
337*2c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR not supported on this platform\n");
338*2c9916cdSFrançois Tigeot 		return;
339*2c9916cdSFrançois Tigeot 	}
340*2c9916cdSFrançois Tigeot 
341*2c9916cdSFrançois Tigeot 	if (!is_edp_psr(intel_dp)) {
342*2c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR not supported by this panel\n");
343*2c9916cdSFrançois Tigeot 		return;
344*2c9916cdSFrançois Tigeot 	}
345*2c9916cdSFrançois Tigeot 
346*2c9916cdSFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
347*2c9916cdSFrançois Tigeot 	if (dev_priv->psr.enabled) {
348*2c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR already in use\n");
349*2c9916cdSFrançois Tigeot 		goto unlock;
350*2c9916cdSFrançois Tigeot 	}
351*2c9916cdSFrançois Tigeot 
352*2c9916cdSFrançois Tigeot 	if (!intel_psr_match_conditions(intel_dp))
353*2c9916cdSFrançois Tigeot 		goto unlock;
354*2c9916cdSFrançois Tigeot 
355*2c9916cdSFrançois Tigeot 	/* First we check VBT, but we must respect sink and source
356*2c9916cdSFrançois Tigeot 	 * known restrictions */
357*2c9916cdSFrançois Tigeot 	dev_priv->psr.link_standby = dev_priv->vbt.psr.full_link;
358*2c9916cdSFrançois Tigeot 	if ((intel_dp->psr_dpcd[1] & DP_PSR_NO_TRAIN_ON_EXIT) ||
359*2c9916cdSFrançois Tigeot 	    (IS_BROADWELL(dev) && intel_dig_port->port != PORT_A))
360*2c9916cdSFrançois Tigeot 		dev_priv->psr.link_standby = true;
361*2c9916cdSFrançois Tigeot 
362*2c9916cdSFrançois Tigeot 	dev_priv->psr.busy_frontbuffer_bits = 0;
363*2c9916cdSFrançois Tigeot 
364*2c9916cdSFrançois Tigeot 	if (HAS_DDI(dev)) {
365*2c9916cdSFrançois Tigeot 		hsw_psr_setup_vsc(intel_dp);
366*2c9916cdSFrançois Tigeot 
367*2c9916cdSFrançois Tigeot 		/* Avoid continuous PSR exit by masking memup and hpd */
368*2c9916cdSFrançois Tigeot 		I915_WRITE(EDP_PSR_DEBUG_CTL(dev), EDP_PSR_DEBUG_MASK_MEMUP |
369*2c9916cdSFrançois Tigeot 			   EDP_PSR_DEBUG_MASK_HPD | EDP_PSR_DEBUG_MASK_LPSP);
370*2c9916cdSFrançois Tigeot 
371*2c9916cdSFrançois Tigeot 		/* Enable PSR on the panel */
372*2c9916cdSFrançois Tigeot 		hsw_psr_enable_sink(intel_dp);
373*2c9916cdSFrançois Tigeot 
374*2c9916cdSFrançois Tigeot 		if (INTEL_INFO(dev)->gen >= 9)
375*2c9916cdSFrançois Tigeot 			intel_psr_activate(intel_dp);
376*2c9916cdSFrançois Tigeot 	} else {
377*2c9916cdSFrançois Tigeot 		vlv_psr_setup_vsc(intel_dp);
378*2c9916cdSFrançois Tigeot 
379*2c9916cdSFrançois Tigeot 		/* Enable PSR on the panel */
380*2c9916cdSFrançois Tigeot 		vlv_psr_enable_sink(intel_dp);
381*2c9916cdSFrançois Tigeot 
382*2c9916cdSFrançois Tigeot 		/* On HSW+ enable_source also means go to PSR entry/active
383*2c9916cdSFrançois Tigeot 		 * state as soon as idle_frame achieved and here would be
384*2c9916cdSFrançois Tigeot 		 * to soon. However on VLV enable_source just enable PSR
385*2c9916cdSFrançois Tigeot 		 * but let it on inactive state. So we might do this prior
386*2c9916cdSFrançois Tigeot 		 * to active transition, i.e. here.
387*2c9916cdSFrançois Tigeot 		 */
388*2c9916cdSFrançois Tigeot 		vlv_psr_enable_source(intel_dp);
389*2c9916cdSFrançois Tigeot 	}
390*2c9916cdSFrançois Tigeot 
391*2c9916cdSFrançois Tigeot 	dev_priv->psr.enabled = intel_dp;
392*2c9916cdSFrançois Tigeot unlock:
393*2c9916cdSFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
394*2c9916cdSFrançois Tigeot }
395*2c9916cdSFrançois Tigeot 
396*2c9916cdSFrançois Tigeot static void vlv_psr_disable(struct intel_dp *intel_dp)
397*2c9916cdSFrançois Tigeot {
398*2c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
399*2c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
400*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
401*2c9916cdSFrançois Tigeot 	struct intel_crtc *intel_crtc =
402*2c9916cdSFrançois Tigeot 		to_intel_crtc(intel_dig_port->base.base.crtc);
403*2c9916cdSFrançois Tigeot 	uint32_t val;
404*2c9916cdSFrançois Tigeot 
405*2c9916cdSFrançois Tigeot 	if (dev_priv->psr.active) {
406*2c9916cdSFrançois Tigeot 		/* Put VLV PSR back to PSR_state 0 that is PSR Disabled. */
407*2c9916cdSFrançois Tigeot 		if (wait_for((I915_READ(VLV_PSRSTAT(intel_crtc->pipe)) &
408*2c9916cdSFrançois Tigeot 			      VLV_EDP_PSR_IN_TRANS) == 0, 1))
409*2c9916cdSFrançois Tigeot 			WARN(1, "PSR transition took longer than expected\n");
410*2c9916cdSFrançois Tigeot 
411*2c9916cdSFrançois Tigeot 		val = I915_READ(VLV_PSRCTL(intel_crtc->pipe));
412*2c9916cdSFrançois Tigeot 		val &= ~VLV_EDP_PSR_ACTIVE_ENTRY;
413*2c9916cdSFrançois Tigeot 		val &= ~VLV_EDP_PSR_ENABLE;
414*2c9916cdSFrançois Tigeot 		val &= ~VLV_EDP_PSR_MODE_MASK;
415*2c9916cdSFrançois Tigeot 		I915_WRITE(VLV_PSRCTL(intel_crtc->pipe), val);
416*2c9916cdSFrançois Tigeot 
417*2c9916cdSFrançois Tigeot 		dev_priv->psr.active = false;
418*2c9916cdSFrançois Tigeot 	} else {
419*2c9916cdSFrançois Tigeot 		WARN_ON(vlv_is_psr_active_on_pipe(dev, intel_crtc->pipe));
420*2c9916cdSFrançois Tigeot 	}
421*2c9916cdSFrançois Tigeot }
422*2c9916cdSFrançois Tigeot 
423*2c9916cdSFrançois Tigeot static void hsw_psr_disable(struct intel_dp *intel_dp)
424*2c9916cdSFrançois Tigeot {
425*2c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
426*2c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
427*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
428*2c9916cdSFrançois Tigeot 
429*2c9916cdSFrançois Tigeot 	if (dev_priv->psr.active) {
430*2c9916cdSFrançois Tigeot 		I915_WRITE(EDP_PSR_CTL(dev),
431*2c9916cdSFrançois Tigeot 			   I915_READ(EDP_PSR_CTL(dev)) & ~EDP_PSR_ENABLE);
432*2c9916cdSFrançois Tigeot 
433*2c9916cdSFrançois Tigeot 		/* Wait till PSR is idle */
434*2c9916cdSFrançois Tigeot 		if (_wait_for((I915_READ(EDP_PSR_STATUS_CTL(dev)) &
435*2c9916cdSFrançois Tigeot 			       EDP_PSR_STATUS_STATE_MASK) == 0, 2000, 10))
436*2c9916cdSFrançois Tigeot 			DRM_ERROR("Timed out waiting for PSR Idle State\n");
437*2c9916cdSFrançois Tigeot 
438*2c9916cdSFrançois Tigeot 		dev_priv->psr.active = false;
439*2c9916cdSFrançois Tigeot 	} else {
440*2c9916cdSFrançois Tigeot 		WARN_ON(I915_READ(EDP_PSR_CTL(dev)) & EDP_PSR_ENABLE);
441*2c9916cdSFrançois Tigeot 	}
442*2c9916cdSFrançois Tigeot }
443*2c9916cdSFrançois Tigeot 
444*2c9916cdSFrançois Tigeot /**
445*2c9916cdSFrançois Tigeot  * intel_psr_disable - Disable PSR
446*2c9916cdSFrançois Tigeot  * @intel_dp: Intel DP
447*2c9916cdSFrançois Tigeot  *
448*2c9916cdSFrançois Tigeot  * This function needs to be called before disabling pipe.
449*2c9916cdSFrançois Tigeot  */
450*2c9916cdSFrançois Tigeot void intel_psr_disable(struct intel_dp *intel_dp)
451*2c9916cdSFrançois Tigeot {
452*2c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
453*2c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
454*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
455*2c9916cdSFrançois Tigeot 
456*2c9916cdSFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
457*2c9916cdSFrançois Tigeot 	if (!dev_priv->psr.enabled) {
458*2c9916cdSFrançois Tigeot 		mutex_unlock(&dev_priv->psr.lock);
459*2c9916cdSFrançois Tigeot 		return;
460*2c9916cdSFrançois Tigeot 	}
461*2c9916cdSFrançois Tigeot 
462*2c9916cdSFrançois Tigeot 	if (HAS_DDI(dev))
463*2c9916cdSFrançois Tigeot 		hsw_psr_disable(intel_dp);
464*2c9916cdSFrançois Tigeot 	else
465*2c9916cdSFrançois Tigeot 		vlv_psr_disable(intel_dp);
466*2c9916cdSFrançois Tigeot 
467*2c9916cdSFrançois Tigeot 	dev_priv->psr.enabled = NULL;
468*2c9916cdSFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
469*2c9916cdSFrançois Tigeot 
470*2c9916cdSFrançois Tigeot 	cancel_delayed_work_sync(&dev_priv->psr.work);
471*2c9916cdSFrançois Tigeot }
472*2c9916cdSFrançois Tigeot 
473*2c9916cdSFrançois Tigeot static void intel_psr_work(struct work_struct *work)
474*2c9916cdSFrançois Tigeot {
475*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv =
476*2c9916cdSFrançois Tigeot 		container_of(work, typeof(*dev_priv), psr.work.work);
477*2c9916cdSFrançois Tigeot 	struct intel_dp *intel_dp = dev_priv->psr.enabled;
478*2c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = dp_to_dig_port(intel_dp)->base.base.crtc;
479*2c9916cdSFrançois Tigeot 	enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
480*2c9916cdSFrançois Tigeot 
481*2c9916cdSFrançois Tigeot 	/* We have to make sure PSR is ready for re-enable
482*2c9916cdSFrançois Tigeot 	 * otherwise it keeps disabled until next full enable/disable cycle.
483*2c9916cdSFrançois Tigeot 	 * PSR might take some time to get fully disabled
484*2c9916cdSFrançois Tigeot 	 * and be ready for re-enable.
485*2c9916cdSFrançois Tigeot 	 */
486*2c9916cdSFrançois Tigeot 	if (HAS_DDI(dev_priv->dev)) {
487*2c9916cdSFrançois Tigeot 		if (wait_for((I915_READ(EDP_PSR_STATUS_CTL(dev_priv->dev)) &
488*2c9916cdSFrançois Tigeot 			      EDP_PSR_STATUS_STATE_MASK) == 0, 50)) {
489*2c9916cdSFrançois Tigeot 			DRM_ERROR("Timed out waiting for PSR Idle for re-enable\n");
490*2c9916cdSFrançois Tigeot 			return;
491*2c9916cdSFrançois Tigeot 		}
492*2c9916cdSFrançois Tigeot 	} else {
493*2c9916cdSFrançois Tigeot 		if (wait_for((I915_READ(VLV_PSRSTAT(pipe)) &
494*2c9916cdSFrançois Tigeot 			      VLV_EDP_PSR_IN_TRANS) == 0, 1)) {
495*2c9916cdSFrançois Tigeot 			DRM_ERROR("Timed out waiting for PSR Idle for re-enable\n");
496*2c9916cdSFrançois Tigeot 			return;
497*2c9916cdSFrançois Tigeot 		}
498*2c9916cdSFrançois Tigeot 	}
499*2c9916cdSFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
500*2c9916cdSFrançois Tigeot 	intel_dp = dev_priv->psr.enabled;
501*2c9916cdSFrançois Tigeot 
502*2c9916cdSFrançois Tigeot 	if (!intel_dp)
503*2c9916cdSFrançois Tigeot 		goto unlock;
504*2c9916cdSFrançois Tigeot 
505*2c9916cdSFrançois Tigeot 	/*
506*2c9916cdSFrançois Tigeot 	 * The delayed work can race with an invalidate hence we need to
507*2c9916cdSFrançois Tigeot 	 * recheck. Since psr_flush first clears this and then reschedules we
508*2c9916cdSFrançois Tigeot 	 * won't ever miss a flush when bailing out here.
509*2c9916cdSFrançois Tigeot 	 */
510*2c9916cdSFrançois Tigeot 	if (dev_priv->psr.busy_frontbuffer_bits)
511*2c9916cdSFrançois Tigeot 		goto unlock;
512*2c9916cdSFrançois Tigeot 
513*2c9916cdSFrançois Tigeot 	intel_psr_activate(intel_dp);
514*2c9916cdSFrançois Tigeot unlock:
515*2c9916cdSFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
516*2c9916cdSFrançois Tigeot }
517*2c9916cdSFrançois Tigeot 
518*2c9916cdSFrançois Tigeot static void intel_psr_exit(struct drm_device *dev)
519*2c9916cdSFrançois Tigeot {
520*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
521*2c9916cdSFrançois Tigeot 	struct intel_dp *intel_dp = dev_priv->psr.enabled;
522*2c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = dp_to_dig_port(intel_dp)->base.base.crtc;
523*2c9916cdSFrançois Tigeot 	enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
524*2c9916cdSFrançois Tigeot 	u32 val;
525*2c9916cdSFrançois Tigeot 
526*2c9916cdSFrançois Tigeot 	if (!dev_priv->psr.active)
527*2c9916cdSFrançois Tigeot 		return;
528*2c9916cdSFrançois Tigeot 
529*2c9916cdSFrançois Tigeot 	if (HAS_DDI(dev)) {
530*2c9916cdSFrançois Tigeot 		val = I915_READ(EDP_PSR_CTL(dev));
531*2c9916cdSFrançois Tigeot 
532*2c9916cdSFrançois Tigeot 		WARN_ON(!(val & EDP_PSR_ENABLE));
533*2c9916cdSFrançois Tigeot 
534*2c9916cdSFrançois Tigeot 		I915_WRITE(EDP_PSR_CTL(dev), val & ~EDP_PSR_ENABLE);
535*2c9916cdSFrançois Tigeot 
536*2c9916cdSFrançois Tigeot 		dev_priv->psr.active = false;
537*2c9916cdSFrançois Tigeot 	} else {
538*2c9916cdSFrançois Tigeot 		val = I915_READ(VLV_PSRCTL(pipe));
539*2c9916cdSFrançois Tigeot 
540*2c9916cdSFrançois Tigeot 		/* Here we do the transition from PSR_state 3 to PSR_state 5
541*2c9916cdSFrançois Tigeot 		 * directly once PSR State 4 that is active with single frame
542*2c9916cdSFrançois Tigeot 		 * update can be skipped. PSR_state 5 that is PSR exit then
543*2c9916cdSFrançois Tigeot 		 * Hardware is responsible to transition back to PSR_state 1
544*2c9916cdSFrançois Tigeot 		 * that is PSR inactive. Same state after
545*2c9916cdSFrançois Tigeot 		 * vlv_edp_psr_enable_source.
546*2c9916cdSFrançois Tigeot 		 */
547*2c9916cdSFrançois Tigeot 		val &= ~VLV_EDP_PSR_ACTIVE_ENTRY;
548*2c9916cdSFrançois Tigeot 		I915_WRITE(VLV_PSRCTL(pipe), val);
549*2c9916cdSFrançois Tigeot 
550*2c9916cdSFrançois Tigeot 		/* Send AUX wake up - Spec says after transitioning to PSR
551*2c9916cdSFrançois Tigeot 		 * active we have to send AUX wake up by writing 01h in DPCD
552*2c9916cdSFrançois Tigeot 		 * 600h of sink device.
553*2c9916cdSFrançois Tigeot 		 * XXX: This might slow down the transition, but without this
554*2c9916cdSFrançois Tigeot 		 * HW doesn't complete the transition to PSR_state 1 and we
555*2c9916cdSFrançois Tigeot 		 * never get the screen updated.
556*2c9916cdSFrançois Tigeot 		 */
557*2c9916cdSFrançois Tigeot 		drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
558*2c9916cdSFrançois Tigeot 				   DP_SET_POWER_D0);
559*2c9916cdSFrançois Tigeot 	}
560*2c9916cdSFrançois Tigeot 
561*2c9916cdSFrançois Tigeot 	dev_priv->psr.active = false;
562*2c9916cdSFrançois Tigeot }
563*2c9916cdSFrançois Tigeot 
564*2c9916cdSFrançois Tigeot /**
565*2c9916cdSFrançois Tigeot  * intel_psr_invalidate - Invalidade PSR
566*2c9916cdSFrançois Tigeot  * @dev: DRM device
567*2c9916cdSFrançois Tigeot  * @frontbuffer_bits: frontbuffer plane tracking bits
568*2c9916cdSFrançois Tigeot  *
569*2c9916cdSFrançois Tigeot  * Since the hardware frontbuffer tracking has gaps we need to integrate
570*2c9916cdSFrançois Tigeot  * with the software frontbuffer tracking. This function gets called every
571*2c9916cdSFrançois Tigeot  * time frontbuffer rendering starts and a buffer gets dirtied. PSR must be
572*2c9916cdSFrançois Tigeot  * disabled if the frontbuffer mask contains a buffer relevant to PSR.
573*2c9916cdSFrançois Tigeot  *
574*2c9916cdSFrançois Tigeot  * Dirty frontbuffers relevant to PSR are tracked in busy_frontbuffer_bits."
575*2c9916cdSFrançois Tigeot  */
576*2c9916cdSFrançois Tigeot void intel_psr_invalidate(struct drm_device *dev,
577*2c9916cdSFrançois Tigeot 			      unsigned frontbuffer_bits)
578*2c9916cdSFrançois Tigeot {
579*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
580*2c9916cdSFrançois Tigeot 	struct drm_crtc *crtc;
581*2c9916cdSFrançois Tigeot 	enum i915_pipe pipe;
582*2c9916cdSFrançois Tigeot 
583*2c9916cdSFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
584*2c9916cdSFrançois Tigeot 	if (!dev_priv->psr.enabled) {
585*2c9916cdSFrançois Tigeot 		mutex_unlock(&dev_priv->psr.lock);
586*2c9916cdSFrançois Tigeot 		return;
587*2c9916cdSFrançois Tigeot 	}
588*2c9916cdSFrançois Tigeot 
589*2c9916cdSFrançois Tigeot 	crtc = dp_to_dig_port(dev_priv->psr.enabled)->base.base.crtc;
590*2c9916cdSFrançois Tigeot 	pipe = to_intel_crtc(crtc)->pipe;
591*2c9916cdSFrançois Tigeot 
592*2c9916cdSFrançois Tigeot 	intel_psr_exit(dev);
593*2c9916cdSFrançois Tigeot 
594*2c9916cdSFrançois Tigeot 	frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
595*2c9916cdSFrançois Tigeot 
596*2c9916cdSFrançois Tigeot 	dev_priv->psr.busy_frontbuffer_bits |= frontbuffer_bits;
597*2c9916cdSFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
598*2c9916cdSFrançois Tigeot }
599*2c9916cdSFrançois Tigeot 
600*2c9916cdSFrançois Tigeot /**
601*2c9916cdSFrançois Tigeot  * intel_psr_flush - Flush PSR
602*2c9916cdSFrançois Tigeot  * @dev: DRM device
603*2c9916cdSFrançois Tigeot  * @frontbuffer_bits: frontbuffer plane tracking bits
604*2c9916cdSFrançois Tigeot  *
605*2c9916cdSFrançois Tigeot  * Since the hardware frontbuffer tracking has gaps we need to integrate
606*2c9916cdSFrançois Tigeot  * with the software frontbuffer tracking. This function gets called every
607*2c9916cdSFrançois Tigeot  * time frontbuffer rendering has completed and flushed out to memory. PSR
608*2c9916cdSFrançois Tigeot  * can be enabled again if no other frontbuffer relevant to PSR is dirty.
609*2c9916cdSFrançois Tigeot  *
610*2c9916cdSFrançois Tigeot  * Dirty frontbuffers relevant to PSR are tracked in busy_frontbuffer_bits.
611*2c9916cdSFrançois Tigeot  */
612*2c9916cdSFrançois Tigeot void intel_psr_flush(struct drm_device *dev,
613*2c9916cdSFrançois Tigeot 			 unsigned frontbuffer_bits)
614*2c9916cdSFrançois Tigeot {
615*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
616*2c9916cdSFrançois Tigeot 	struct drm_crtc *crtc;
617*2c9916cdSFrançois Tigeot 	enum i915_pipe pipe;
618*2c9916cdSFrançois Tigeot 
619*2c9916cdSFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
620*2c9916cdSFrançois Tigeot 	if (!dev_priv->psr.enabled) {
621*2c9916cdSFrançois Tigeot 		mutex_unlock(&dev_priv->psr.lock);
622*2c9916cdSFrançois Tigeot 		return;
623*2c9916cdSFrançois Tigeot 	}
624*2c9916cdSFrançois Tigeot 
625*2c9916cdSFrançois Tigeot 	crtc = dp_to_dig_port(dev_priv->psr.enabled)->base.base.crtc;
626*2c9916cdSFrançois Tigeot 	pipe = to_intel_crtc(crtc)->pipe;
627*2c9916cdSFrançois Tigeot 	dev_priv->psr.busy_frontbuffer_bits &= ~frontbuffer_bits;
628*2c9916cdSFrançois Tigeot 
629*2c9916cdSFrançois Tigeot 	/*
630*2c9916cdSFrançois Tigeot 	 * On Haswell sprite plane updates don't result in a psr invalidating
631*2c9916cdSFrançois Tigeot 	 * signal in the hardware. Which means we need to manually fake this in
632*2c9916cdSFrançois Tigeot 	 * software for all flushes, not just when we've seen a preceding
633*2c9916cdSFrançois Tigeot 	 * invalidation through frontbuffer rendering.
634*2c9916cdSFrançois Tigeot 	 */
635*2c9916cdSFrançois Tigeot 	if (IS_HASWELL(dev) &&
636*2c9916cdSFrançois Tigeot 	    (frontbuffer_bits & INTEL_FRONTBUFFER_SPRITE(pipe)))
637*2c9916cdSFrançois Tigeot 		intel_psr_exit(dev);
638*2c9916cdSFrançois Tigeot 
639*2c9916cdSFrançois Tigeot 	/*
640*2c9916cdSFrançois Tigeot 	 * On Valleyview and Cherryview we don't use hardware tracking so
641*2c9916cdSFrançois Tigeot 	 * any plane updates or cursor moves don't result in a PSR
642*2c9916cdSFrançois Tigeot 	 * invalidating. Which means we need to manually fake this in
643*2c9916cdSFrançois Tigeot 	 * software for all flushes, not just when we've seen a preceding
644*2c9916cdSFrançois Tigeot 	 * invalidation through frontbuffer rendering. */
645*2c9916cdSFrançois Tigeot 	if (!HAS_DDI(dev))
646*2c9916cdSFrançois Tigeot 		intel_psr_exit(dev);
647*2c9916cdSFrançois Tigeot 
648*2c9916cdSFrançois Tigeot 	if (!dev_priv->psr.active && !dev_priv->psr.busy_frontbuffer_bits)
649*2c9916cdSFrançois Tigeot 		schedule_delayed_work(&dev_priv->psr.work,
650*2c9916cdSFrançois Tigeot 				      msecs_to_jiffies(100));
651*2c9916cdSFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
652*2c9916cdSFrançois Tigeot }
653*2c9916cdSFrançois Tigeot 
654*2c9916cdSFrançois Tigeot /**
655*2c9916cdSFrançois Tigeot  * intel_psr_init - Init basic PSR work and mutex.
656*2c9916cdSFrançois Tigeot  * @dev: DRM device
657*2c9916cdSFrançois Tigeot  *
658*2c9916cdSFrançois Tigeot  * This function is  called only once at driver load to initialize basic
659*2c9916cdSFrançois Tigeot  * PSR stuff.
660*2c9916cdSFrançois Tigeot  */
661*2c9916cdSFrançois Tigeot void intel_psr_init(struct drm_device *dev)
662*2c9916cdSFrançois Tigeot {
663*2c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
664*2c9916cdSFrançois Tigeot 
665*2c9916cdSFrançois Tigeot 	INIT_DELAYED_WORK(&dev_priv->psr.work, intel_psr_work);
666*2c9916cdSFrançois Tigeot 	lockinit(&dev_priv->psr.lock, "i915dpl", 0, LK_CANRECURSE);
667*2c9916cdSFrançois Tigeot }
668