xref: /dflybsd-src/sys/dev/drm/i915/intel_psr.c (revision aee94f86171368465eaa15d649743f13cea3363a)
12c9916cdSFrançois Tigeot /*
22c9916cdSFrançois Tigeot  * Copyright © 2014 Intel Corporation
32c9916cdSFrançois Tigeot  *
42c9916cdSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
52c9916cdSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
62c9916cdSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
72c9916cdSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
82c9916cdSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
92c9916cdSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
102c9916cdSFrançois Tigeot  *
112c9916cdSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
122c9916cdSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
132c9916cdSFrançois Tigeot  * Software.
142c9916cdSFrançois Tigeot  *
152c9916cdSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
162c9916cdSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
172c9916cdSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
182c9916cdSFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
192c9916cdSFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
202c9916cdSFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
212c9916cdSFrançois Tigeot  * DEALINGS IN THE SOFTWARE.
222c9916cdSFrançois Tigeot  */
232c9916cdSFrançois Tigeot 
242c9916cdSFrançois Tigeot /**
252c9916cdSFrançois Tigeot  * DOC: Panel Self Refresh (PSR/SRD)
262c9916cdSFrançois Tigeot  *
272c9916cdSFrançois Tigeot  * Since Haswell Display controller supports Panel Self-Refresh on display
282c9916cdSFrançois Tigeot  * panels witch have a remote frame buffer (RFB) implemented according to PSR
292c9916cdSFrançois Tigeot  * spec in eDP1.3. PSR feature allows the display to go to lower standby states
302c9916cdSFrançois Tigeot  * when system is idle but display is on as it eliminates display refresh
312c9916cdSFrançois Tigeot  * request to DDR memory completely as long as the frame buffer for that
322c9916cdSFrançois Tigeot  * display is unchanged.
332c9916cdSFrançois Tigeot  *
342c9916cdSFrançois Tigeot  * Panel Self Refresh must be supported by both Hardware (source) and
352c9916cdSFrançois Tigeot  * Panel (sink).
362c9916cdSFrançois Tigeot  *
372c9916cdSFrançois Tigeot  * PSR saves power by caching the framebuffer in the panel RFB, which allows us
382c9916cdSFrançois Tigeot  * to power down the link and memory controller. For DSI panels the same idea
392c9916cdSFrançois Tigeot  * is called "manual mode".
402c9916cdSFrançois Tigeot  *
412c9916cdSFrançois Tigeot  * The implementation uses the hardware-based PSR support which automatically
422c9916cdSFrançois Tigeot  * enters/exits self-refresh mode. The hardware takes care of sending the
432c9916cdSFrançois Tigeot  * required DP aux message and could even retrain the link (that part isn't
442c9916cdSFrançois Tigeot  * enabled yet though). The hardware also keeps track of any frontbuffer
452c9916cdSFrançois Tigeot  * changes to know when to exit self-refresh mode again. Unfortunately that
462c9916cdSFrançois Tigeot  * part doesn't work too well, hence why the i915 PSR support uses the
472c9916cdSFrançois Tigeot  * software frontbuffer tracking to make sure it doesn't miss a screen
482c9916cdSFrançois Tigeot  * update. For this integration intel_psr_invalidate() and intel_psr_flush()
492c9916cdSFrançois Tigeot  * get called by the frontbuffer tracking code. Note that because of locking
502c9916cdSFrançois Tigeot  * issues the self-refresh re-enable code is done from a work queue, which
512c9916cdSFrançois Tigeot  * must be correctly synchronized/cancelled when shutting down the pipe."
522c9916cdSFrançois Tigeot  */
532c9916cdSFrançois Tigeot 
542c9916cdSFrançois Tigeot #include <drm/drmP.h>
552c9916cdSFrançois Tigeot 
562c9916cdSFrançois Tigeot #include "intel_drv.h"
572c9916cdSFrançois Tigeot #include "i915_drv.h"
582c9916cdSFrançois Tigeot 
592c9916cdSFrançois Tigeot static bool is_edp_psr(struct intel_dp *intel_dp)
602c9916cdSFrançois Tigeot {
612c9916cdSFrançois Tigeot 	return intel_dp->psr_dpcd[0] & DP_PSR_IS_SUPPORTED;
622c9916cdSFrançois Tigeot }
632c9916cdSFrançois Tigeot 
642c9916cdSFrançois Tigeot static bool vlv_is_psr_active_on_pipe(struct drm_device *dev, int pipe)
652c9916cdSFrançois Tigeot {
662c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
672c9916cdSFrançois Tigeot 	uint32_t val;
682c9916cdSFrançois Tigeot 
692c9916cdSFrançois Tigeot 	val = I915_READ(VLV_PSRSTAT(pipe)) &
702c9916cdSFrançois Tigeot 	      VLV_EDP_PSR_CURR_STATE_MASK;
712c9916cdSFrançois Tigeot 	return (val == VLV_EDP_PSR_ACTIVE_NORFB_UP) ||
722c9916cdSFrançois Tigeot 	       (val == VLV_EDP_PSR_ACTIVE_SF_UPDATE);
732c9916cdSFrançois Tigeot }
742c9916cdSFrançois Tigeot 
752c9916cdSFrançois Tigeot static void intel_psr_write_vsc(struct intel_dp *intel_dp,
76352ff8bdSFrançois Tigeot 				const struct edp_vsc_psr *vsc_psr)
772c9916cdSFrançois Tigeot {
782c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
792c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
802c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
812c9916cdSFrançois Tigeot 	struct intel_crtc *crtc = to_intel_crtc(dig_port->base.base.crtc);
82352ff8bdSFrançois Tigeot 	enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
83*aee94f86SFrançois Tigeot 	i915_reg_t ctl_reg = HSW_TVIDEO_DIP_CTL(cpu_transcoder);
84352ff8bdSFrançois Tigeot 	const uint32_t *data = (const uint32_t *) vsc_psr;
852c9916cdSFrançois Tigeot 	unsigned int i;
862c9916cdSFrançois Tigeot 
872c9916cdSFrançois Tigeot 	/* As per BSPec (Pipe Video Data Island Packet), we need to disable
882c9916cdSFrançois Tigeot 	   the video DIP being updated before program video DIP data buffer
892c9916cdSFrançois Tigeot 	   registers for DIP being updated. */
902c9916cdSFrançois Tigeot 	I915_WRITE(ctl_reg, 0);
912c9916cdSFrançois Tigeot 	POSTING_READ(ctl_reg);
922c9916cdSFrançois Tigeot 
93352ff8bdSFrançois Tigeot 	for (i = 0; i < sizeof(*vsc_psr); i += 4) {
94352ff8bdSFrançois Tigeot 		I915_WRITE(HSW_TVIDEO_DIP_VSC_DATA(cpu_transcoder,
95352ff8bdSFrançois Tigeot 						   i >> 2), *data);
96352ff8bdSFrançois Tigeot 		data++;
972c9916cdSFrançois Tigeot 	}
98352ff8bdSFrançois Tigeot 	for (; i < VIDEO_DIP_VSC_DATA_SIZE; i += 4)
99352ff8bdSFrançois Tigeot 		I915_WRITE(HSW_TVIDEO_DIP_VSC_DATA(cpu_transcoder,
100352ff8bdSFrançois Tigeot 						   i >> 2), 0);
1012c9916cdSFrançois Tigeot 
1022c9916cdSFrançois Tigeot 	I915_WRITE(ctl_reg, VIDEO_DIP_ENABLE_VSC_HSW);
1032c9916cdSFrançois Tigeot 	POSTING_READ(ctl_reg);
1042c9916cdSFrançois Tigeot }
1052c9916cdSFrançois Tigeot 
1062c9916cdSFrançois Tigeot static void vlv_psr_setup_vsc(struct intel_dp *intel_dp)
1072c9916cdSFrançois Tigeot {
1082c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
1092c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
1102c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1112c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = intel_dig_port->base.base.crtc;
1122c9916cdSFrançois Tigeot 	enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
1132c9916cdSFrançois Tigeot 	uint32_t val;
1142c9916cdSFrançois Tigeot 
1152c9916cdSFrançois Tigeot 	/* VLV auto-generate VSC package as per EDP 1.3 spec, Table 3.10 */
1162c9916cdSFrançois Tigeot 	val  = I915_READ(VLV_VSCSDP(pipe));
1172c9916cdSFrançois Tigeot 	val &= ~VLV_EDP_PSR_SDP_FREQ_MASK;
1182c9916cdSFrançois Tigeot 	val |= VLV_EDP_PSR_SDP_FREQ_EVFRAME;
1192c9916cdSFrançois Tigeot 	I915_WRITE(VLV_VSCSDP(pipe), val);
1202c9916cdSFrançois Tigeot }
1212c9916cdSFrançois Tigeot 
12219c468b4SFrançois Tigeot static void skl_psr_setup_su_vsc(struct intel_dp *intel_dp)
12319c468b4SFrançois Tigeot {
12419c468b4SFrançois Tigeot 	struct edp_vsc_psr psr_vsc;
12519c468b4SFrançois Tigeot 
12619c468b4SFrançois Tigeot 	/* Prepare VSC Header for SU as per EDP 1.4 spec, Table 6.11 */
12719c468b4SFrançois Tigeot 	memset(&psr_vsc, 0, sizeof(psr_vsc));
12819c468b4SFrançois Tigeot 	psr_vsc.sdp_header.HB0 = 0;
12919c468b4SFrançois Tigeot 	psr_vsc.sdp_header.HB1 = 0x7;
13019c468b4SFrançois Tigeot 	psr_vsc.sdp_header.HB2 = 0x3;
13119c468b4SFrançois Tigeot 	psr_vsc.sdp_header.HB3 = 0xb;
13219c468b4SFrançois Tigeot 	intel_psr_write_vsc(intel_dp, &psr_vsc);
13319c468b4SFrançois Tigeot }
13419c468b4SFrançois Tigeot 
1352c9916cdSFrançois Tigeot static void hsw_psr_setup_vsc(struct intel_dp *intel_dp)
1362c9916cdSFrançois Tigeot {
1372c9916cdSFrançois Tigeot 	struct edp_vsc_psr psr_vsc;
1382c9916cdSFrançois Tigeot 
1392c9916cdSFrançois Tigeot 	/* Prepare VSC packet as per EDP 1.3 spec, Table 3.10 */
1402c9916cdSFrançois Tigeot 	memset(&psr_vsc, 0, sizeof(psr_vsc));
1412c9916cdSFrançois Tigeot 	psr_vsc.sdp_header.HB0 = 0;
1422c9916cdSFrançois Tigeot 	psr_vsc.sdp_header.HB1 = 0x7;
1432c9916cdSFrançois Tigeot 	psr_vsc.sdp_header.HB2 = 0x2;
1442c9916cdSFrançois Tigeot 	psr_vsc.sdp_header.HB3 = 0x8;
1452c9916cdSFrançois Tigeot 	intel_psr_write_vsc(intel_dp, &psr_vsc);
1462c9916cdSFrançois Tigeot }
1472c9916cdSFrançois Tigeot 
1482c9916cdSFrançois Tigeot static void vlv_psr_enable_sink(struct intel_dp *intel_dp)
1492c9916cdSFrançois Tigeot {
1502c9916cdSFrançois Tigeot 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG,
15119c468b4SFrançois Tigeot 			   DP_PSR_ENABLE | DP_PSR_MAIN_LINK_ACTIVE);
1522c9916cdSFrançois Tigeot }
1532c9916cdSFrançois Tigeot 
154*aee94f86SFrançois Tigeot static i915_reg_t psr_aux_ctl_reg(struct drm_i915_private *dev_priv,
155*aee94f86SFrançois Tigeot 				       enum port port)
156*aee94f86SFrançois Tigeot {
157*aee94f86SFrançois Tigeot 	if (INTEL_INFO(dev_priv)->gen >= 9)
158*aee94f86SFrançois Tigeot 		return DP_AUX_CH_CTL(port);
159*aee94f86SFrançois Tigeot 	else
160*aee94f86SFrançois Tigeot 		return EDP_PSR_AUX_CTL;
161*aee94f86SFrançois Tigeot }
162*aee94f86SFrançois Tigeot 
163*aee94f86SFrançois Tigeot static i915_reg_t psr_aux_data_reg(struct drm_i915_private *dev_priv,
164*aee94f86SFrançois Tigeot 					enum port port, int index)
165*aee94f86SFrançois Tigeot {
166*aee94f86SFrançois Tigeot 	if (INTEL_INFO(dev_priv)->gen >= 9)
167*aee94f86SFrançois Tigeot 		return DP_AUX_CH_DATA(port, index);
168*aee94f86SFrançois Tigeot 	else
169*aee94f86SFrançois Tigeot 		return EDP_PSR_AUX_DATA(index);
170*aee94f86SFrançois Tigeot }
171*aee94f86SFrançois Tigeot 
1722c9916cdSFrançois Tigeot static void hsw_psr_enable_sink(struct intel_dp *intel_dp)
1732c9916cdSFrançois Tigeot {
1742c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1752c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
1762c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
1772c9916cdSFrançois Tigeot 	uint32_t aux_clock_divider;
178*aee94f86SFrançois Tigeot 	i915_reg_t aux_ctl_reg;
1792c9916cdSFrançois Tigeot 	int precharge = 0x3;
1802c9916cdSFrançois Tigeot 	static const uint8_t aux_msg[] = {
1812c9916cdSFrançois Tigeot 		[0] = DP_AUX_NATIVE_WRITE << 4,
1822c9916cdSFrançois Tigeot 		[1] = DP_SET_POWER >> 8,
1832c9916cdSFrançois Tigeot 		[2] = DP_SET_POWER & 0xff,
1842c9916cdSFrançois Tigeot 		[3] = 1 - 1,
1852c9916cdSFrançois Tigeot 		[4] = DP_SET_POWER_D0,
1862c9916cdSFrançois Tigeot 	};
187*aee94f86SFrançois Tigeot 	enum port port = dig_port->port;
1882c9916cdSFrançois Tigeot 	int i;
1892c9916cdSFrançois Tigeot 
1902c9916cdSFrançois Tigeot 	BUILD_BUG_ON(sizeof(aux_msg) > 20);
1912c9916cdSFrançois Tigeot 
1922c9916cdSFrançois Tigeot 	aux_clock_divider = intel_dp->get_aux_clock_divider(intel_dp, 0);
1932c9916cdSFrançois Tigeot 
19419c468b4SFrançois Tigeot 	/* Enable AUX frame sync at sink */
19519c468b4SFrançois Tigeot 	if (dev_priv->psr.aux_frame_sync)
19619c468b4SFrançois Tigeot 		drm_dp_dpcd_writeb(&intel_dp->aux,
19719c468b4SFrançois Tigeot 				DP_SINK_DEVICE_AUX_FRAME_SYNC_CONF,
19819c468b4SFrançois Tigeot 				DP_AUX_FRAME_SYNC_ENABLE);
19919c468b4SFrançois Tigeot 
200*aee94f86SFrançois Tigeot 	aux_ctl_reg = psr_aux_ctl_reg(dev_priv, port);
2012c9916cdSFrançois Tigeot 
2022c9916cdSFrançois Tigeot 	/* Setup AUX registers */
2032c9916cdSFrançois Tigeot 	for (i = 0; i < sizeof(aux_msg); i += 4)
204*aee94f86SFrançois Tigeot 		I915_WRITE(psr_aux_data_reg(dev_priv, port, i >> 2),
2052c9916cdSFrançois Tigeot 			   intel_dp_pack_aux(&aux_msg[i], sizeof(aux_msg) - i));
2062c9916cdSFrançois Tigeot 
2072c9916cdSFrançois Tigeot 	if (INTEL_INFO(dev)->gen >= 9) {
2082c9916cdSFrançois Tigeot 		uint32_t val;
2092c9916cdSFrançois Tigeot 
2102c9916cdSFrançois Tigeot 		val = I915_READ(aux_ctl_reg);
2112c9916cdSFrançois Tigeot 		val &= ~DP_AUX_CH_CTL_TIME_OUT_MASK;
2122c9916cdSFrançois Tigeot 		val |= DP_AUX_CH_CTL_TIME_OUT_1600us;
2132c9916cdSFrançois Tigeot 		val &= ~DP_AUX_CH_CTL_MESSAGE_SIZE_MASK;
2142c9916cdSFrançois Tigeot 		val |= (sizeof(aux_msg) << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT);
21519c468b4SFrançois Tigeot 		/* Use hardcoded data values for PSR, frame sync and GTC */
2162c9916cdSFrançois Tigeot 		val &= ~DP_AUX_CH_CTL_PSR_DATA_AUX_REG_SKL;
21719c468b4SFrançois Tigeot 		val &= ~DP_AUX_CH_CTL_FS_DATA_AUX_REG_SKL;
21819c468b4SFrançois Tigeot 		val &= ~DP_AUX_CH_CTL_GTC_DATA_AUX_REG_SKL;
2192c9916cdSFrançois Tigeot 		I915_WRITE(aux_ctl_reg, val);
2202c9916cdSFrançois Tigeot 	} else {
2212c9916cdSFrançois Tigeot 		I915_WRITE(aux_ctl_reg,
2222c9916cdSFrançois Tigeot 		   DP_AUX_CH_CTL_TIME_OUT_400us |
2232c9916cdSFrançois Tigeot 		   (sizeof(aux_msg) << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) |
2242c9916cdSFrançois Tigeot 		   (precharge << DP_AUX_CH_CTL_PRECHARGE_2US_SHIFT) |
2252c9916cdSFrançois Tigeot 		   (aux_clock_divider << DP_AUX_CH_CTL_BIT_CLOCK_2X_SHIFT));
2262c9916cdSFrançois Tigeot 	}
22719c468b4SFrançois Tigeot 
22819c468b4SFrançois Tigeot 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG, DP_PSR_ENABLE);
2292c9916cdSFrançois Tigeot }
2302c9916cdSFrançois Tigeot 
2312c9916cdSFrançois Tigeot static void vlv_psr_enable_source(struct intel_dp *intel_dp)
2322c9916cdSFrançois Tigeot {
2332c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2342c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
2352c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2362c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = dig_port->base.base.crtc;
2372c9916cdSFrançois Tigeot 	enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
2382c9916cdSFrançois Tigeot 
2392c9916cdSFrançois Tigeot 	/* Transition from PSR_state 0 to PSR_state 1, i.e. PSR Inactive */
2402c9916cdSFrançois Tigeot 	I915_WRITE(VLV_PSRCTL(pipe),
2412c9916cdSFrançois Tigeot 		   VLV_EDP_PSR_MODE_SW_TIMER |
2422c9916cdSFrançois Tigeot 		   VLV_EDP_PSR_SRC_TRANSMITTER_STATE |
2432c9916cdSFrançois Tigeot 		   VLV_EDP_PSR_ENABLE);
2442c9916cdSFrançois Tigeot }
2452c9916cdSFrançois Tigeot 
2462c9916cdSFrançois Tigeot static void vlv_psr_activate(struct intel_dp *intel_dp)
2472c9916cdSFrançois Tigeot {
2482c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2492c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
2502c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2512c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = dig_port->base.base.crtc;
2522c9916cdSFrançois Tigeot 	enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
2532c9916cdSFrançois Tigeot 
2542c9916cdSFrançois Tigeot 	/* Let's do the transition from PSR_state 1 to PSR_state 2
2552c9916cdSFrançois Tigeot 	 * that is PSR transition to active - static frame transmission.
2562c9916cdSFrançois Tigeot 	 * Then Hardware is responsible for the transition to PSR_state 3
2572c9916cdSFrançois Tigeot 	 * that is PSR active - no Remote Frame Buffer (RFB) update.
2582c9916cdSFrançois Tigeot 	 */
2592c9916cdSFrançois Tigeot 	I915_WRITE(VLV_PSRCTL(pipe), I915_READ(VLV_PSRCTL(pipe)) |
2602c9916cdSFrançois Tigeot 		   VLV_EDP_PSR_ACTIVE_ENTRY);
2612c9916cdSFrançois Tigeot }
2622c9916cdSFrançois Tigeot 
2632c9916cdSFrançois Tigeot static void hsw_psr_enable_source(struct intel_dp *intel_dp)
2642c9916cdSFrançois Tigeot {
2652c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2662c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
2672c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
26819c468b4SFrançois Tigeot 
2692c9916cdSFrançois Tigeot 	uint32_t max_sleep_time = 0x1f;
270*aee94f86SFrançois Tigeot 	/*
271*aee94f86SFrançois Tigeot 	 * Let's respect VBT in case VBT asks a higher idle_frame value.
272*aee94f86SFrançois Tigeot 	 * Let's use 6 as the minimum to cover all known cases including
273*aee94f86SFrançois Tigeot 	 * the off-by-one issue that HW has in some cases. Also there are
274*aee94f86SFrançois Tigeot 	 * cases where sink should be able to train
275*aee94f86SFrançois Tigeot 	 * with the 5 or 6 idle patterns.
2762c9916cdSFrançois Tigeot 	 */
277*aee94f86SFrançois Tigeot 	uint32_t idle_frames = max(6, dev_priv->vbt.psr.idle_frames);
2782c9916cdSFrançois Tigeot 	uint32_t val = 0x0;
2792c9916cdSFrançois Tigeot 
280*aee94f86SFrançois Tigeot 	if (IS_HASWELL(dev))
281*aee94f86SFrançois Tigeot 		val |= EDP_PSR_MIN_LINK_ENTRY_TIME_8_LINES;
2822c9916cdSFrançois Tigeot 
283*aee94f86SFrançois Tigeot 	I915_WRITE(EDP_PSR_CTL, val |
2842c9916cdSFrançois Tigeot 		   max_sleep_time << EDP_PSR_MAX_SLEEP_TIME_SHIFT |
2852c9916cdSFrançois Tigeot 		   idle_frames << EDP_PSR_IDLE_FRAME_SHIFT |
2862c9916cdSFrançois Tigeot 		   EDP_PSR_ENABLE);
28719c468b4SFrançois Tigeot 
28819c468b4SFrançois Tigeot 	if (dev_priv->psr.psr2_support)
28919c468b4SFrançois Tigeot 		I915_WRITE(EDP_PSR2_CTL, EDP_PSR2_ENABLE |
29019c468b4SFrançois Tigeot 				EDP_SU_TRACK_ENABLE | EDP_PSR2_TP2_TIME_100);
2912c9916cdSFrançois Tigeot }
2922c9916cdSFrançois Tigeot 
2932c9916cdSFrançois Tigeot static bool intel_psr_match_conditions(struct intel_dp *intel_dp)
2942c9916cdSFrançois Tigeot {
2952c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2962c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
2972c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
2982c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = dig_port->base.base.crtc;
2992c9916cdSFrançois Tigeot 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3002c9916cdSFrançois Tigeot 
3012c9916cdSFrançois Tigeot 	lockdep_assert_held(&dev_priv->psr.lock);
3022c9916cdSFrançois Tigeot 	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
3032c9916cdSFrançois Tigeot 	WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
3042c9916cdSFrançois Tigeot 
3052c9916cdSFrançois Tigeot 	dev_priv->psr.source_ok = false;
3062c9916cdSFrançois Tigeot 
3072c9916cdSFrançois Tigeot 	if (IS_HASWELL(dev) && dig_port->port != PORT_A) {
3082c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("HSW ties PSR to DDI A (eDP)\n");
3092c9916cdSFrançois Tigeot 		return false;
3102c9916cdSFrançois Tigeot 	}
3112c9916cdSFrançois Tigeot 
3122c9916cdSFrançois Tigeot 	if (!i915.enable_psr) {
3132c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR disable by flag\n");
3142c9916cdSFrançois Tigeot 		return false;
3152c9916cdSFrançois Tigeot 	}
3162c9916cdSFrançois Tigeot 
3172c9916cdSFrançois Tigeot 	if (IS_HASWELL(dev) &&
3182c9916cdSFrançois Tigeot 	    I915_READ(HSW_STEREO_3D_CTL(intel_crtc->config->cpu_transcoder)) &
3192c9916cdSFrançois Tigeot 		      S3D_ENABLE) {
3202c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR condition failed: Stereo 3D is Enabled\n");
3212c9916cdSFrançois Tigeot 		return false;
3222c9916cdSFrançois Tigeot 	}
3232c9916cdSFrançois Tigeot 
3242c9916cdSFrançois Tigeot 	if (IS_HASWELL(dev) &&
3252c9916cdSFrançois Tigeot 	    intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
3262c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR condition failed: Interlaced is Enabled\n");
3272c9916cdSFrançois Tigeot 		return false;
3282c9916cdSFrançois Tigeot 	}
3292c9916cdSFrançois Tigeot 
330*aee94f86SFrançois Tigeot 	if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
331*aee94f86SFrançois Tigeot 	    ((dev_priv->vbt.psr.full_link) || (dig_port->port != PORT_A))) {
33219c468b4SFrançois Tigeot 		DRM_DEBUG_KMS("PSR condition failed: Link Standby requested/needed but not supported on this platform\n");
33319c468b4SFrançois Tigeot 		return false;
33419c468b4SFrançois Tigeot 	}
33519c468b4SFrançois Tigeot 
3362c9916cdSFrançois Tigeot 	dev_priv->psr.source_ok = true;
3372c9916cdSFrançois Tigeot 	return true;
3382c9916cdSFrançois Tigeot }
3392c9916cdSFrançois Tigeot 
3402c9916cdSFrançois Tigeot static void intel_psr_activate(struct intel_dp *intel_dp)
3412c9916cdSFrançois Tigeot {
3422c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
3432c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
3442c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
3452c9916cdSFrançois Tigeot 
346*aee94f86SFrançois Tigeot 	WARN_ON(I915_READ(EDP_PSR_CTL) & EDP_PSR_ENABLE);
3472c9916cdSFrançois Tigeot 	WARN_ON(dev_priv->psr.active);
3482c9916cdSFrançois Tigeot 	lockdep_assert_held(&dev_priv->psr.lock);
3492c9916cdSFrançois Tigeot 
3502c9916cdSFrançois Tigeot 	/* Enable/Re-enable PSR on the host */
3512c9916cdSFrançois Tigeot 	if (HAS_DDI(dev))
3522c9916cdSFrançois Tigeot 		/* On HSW+ after we enable PSR on source it will activate it
3532c9916cdSFrançois Tigeot 		 * as soon as it match configure idle_frame count. So
3542c9916cdSFrançois Tigeot 		 * we just actually enable it here on activation time.
3552c9916cdSFrançois Tigeot 		 */
3562c9916cdSFrançois Tigeot 		hsw_psr_enable_source(intel_dp);
3572c9916cdSFrançois Tigeot 	else
3582c9916cdSFrançois Tigeot 		vlv_psr_activate(intel_dp);
3592c9916cdSFrançois Tigeot 
3602c9916cdSFrançois Tigeot 	dev_priv->psr.active = true;
3612c9916cdSFrançois Tigeot }
3622c9916cdSFrançois Tigeot 
3632c9916cdSFrançois Tigeot /**
3642c9916cdSFrançois Tigeot  * intel_psr_enable - Enable PSR
3652c9916cdSFrançois Tigeot  * @intel_dp: Intel DP
3662c9916cdSFrançois Tigeot  *
3672c9916cdSFrançois Tigeot  * This function can only be called after the pipe is fully trained and enabled.
3682c9916cdSFrançois Tigeot  */
3692c9916cdSFrançois Tigeot void intel_psr_enable(struct intel_dp *intel_dp)
3702c9916cdSFrançois Tigeot {
3712c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
3722c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
3732c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
37419c468b4SFrançois Tigeot 	struct intel_crtc *crtc = to_intel_crtc(intel_dig_port->base.base.crtc);
3752c9916cdSFrançois Tigeot 
3762c9916cdSFrançois Tigeot 	if (!HAS_PSR(dev)) {
3772c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR not supported on this platform\n");
3782c9916cdSFrançois Tigeot 		return;
3792c9916cdSFrançois Tigeot 	}
3802c9916cdSFrançois Tigeot 
3812c9916cdSFrançois Tigeot 	if (!is_edp_psr(intel_dp)) {
3822c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR not supported by this panel\n");
3832c9916cdSFrançois Tigeot 		return;
3842c9916cdSFrançois Tigeot 	}
3852c9916cdSFrançois Tigeot 
3862c9916cdSFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
3872c9916cdSFrançois Tigeot 	if (dev_priv->psr.enabled) {
3882c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR already in use\n");
3892c9916cdSFrançois Tigeot 		goto unlock;
3902c9916cdSFrançois Tigeot 	}
3912c9916cdSFrançois Tigeot 
3922c9916cdSFrançois Tigeot 	if (!intel_psr_match_conditions(intel_dp))
3932c9916cdSFrançois Tigeot 		goto unlock;
3942c9916cdSFrançois Tigeot 
3952c9916cdSFrançois Tigeot 	dev_priv->psr.busy_frontbuffer_bits = 0;
3962c9916cdSFrançois Tigeot 
3972c9916cdSFrançois Tigeot 	if (HAS_DDI(dev)) {
3982c9916cdSFrançois Tigeot 		hsw_psr_setup_vsc(intel_dp);
3992c9916cdSFrançois Tigeot 
40019c468b4SFrançois Tigeot 		if (dev_priv->psr.psr2_support) {
40119c468b4SFrançois Tigeot 			/* PSR2 is restricted to work with panel resolutions upto 3200x2000 */
40219c468b4SFrançois Tigeot 			if (crtc->config->pipe_src_w > 3200 ||
40319c468b4SFrançois Tigeot 				crtc->config->pipe_src_h > 2000)
40419c468b4SFrançois Tigeot 				dev_priv->psr.psr2_support = false;
40519c468b4SFrançois Tigeot 			else
40619c468b4SFrançois Tigeot 				skl_psr_setup_su_vsc(intel_dp);
40719c468b4SFrançois Tigeot 		}
40819c468b4SFrançois Tigeot 
409*aee94f86SFrançois Tigeot 		/*
410*aee94f86SFrançois Tigeot 		 * Per Spec: Avoid continuous PSR exit by masking MEMUP and HPD.
411*aee94f86SFrançois Tigeot 		 * Also mask LPSP to avoid dependency on other drivers that
412*aee94f86SFrançois Tigeot 		 * might block runtime_pm besides preventing other hw tracking
413*aee94f86SFrançois Tigeot 		 * issues now we can rely on frontbuffer tracking.
414*aee94f86SFrançois Tigeot 		 */
415*aee94f86SFrançois Tigeot 		I915_WRITE(EDP_PSR_DEBUG_CTL, EDP_PSR_DEBUG_MASK_MEMUP |
416*aee94f86SFrançois Tigeot 			   EDP_PSR_DEBUG_MASK_HPD | EDP_PSR_DEBUG_MASK_LPSP);
4172c9916cdSFrançois Tigeot 
4182c9916cdSFrançois Tigeot 		/* Enable PSR on the panel */
4192c9916cdSFrançois Tigeot 		hsw_psr_enable_sink(intel_dp);
4202c9916cdSFrançois Tigeot 
4212c9916cdSFrançois Tigeot 		if (INTEL_INFO(dev)->gen >= 9)
4222c9916cdSFrançois Tigeot 			intel_psr_activate(intel_dp);
4232c9916cdSFrançois Tigeot 	} else {
4242c9916cdSFrançois Tigeot 		vlv_psr_setup_vsc(intel_dp);
4252c9916cdSFrançois Tigeot 
4262c9916cdSFrançois Tigeot 		/* Enable PSR on the panel */
4272c9916cdSFrançois Tigeot 		vlv_psr_enable_sink(intel_dp);
4282c9916cdSFrançois Tigeot 
4292c9916cdSFrançois Tigeot 		/* On HSW+ enable_source also means go to PSR entry/active
4302c9916cdSFrançois Tigeot 		 * state as soon as idle_frame achieved and here would be
4312c9916cdSFrançois Tigeot 		 * to soon. However on VLV enable_source just enable PSR
4322c9916cdSFrançois Tigeot 		 * but let it on inactive state. So we might do this prior
4332c9916cdSFrançois Tigeot 		 * to active transition, i.e. here.
4342c9916cdSFrançois Tigeot 		 */
4352c9916cdSFrançois Tigeot 		vlv_psr_enable_source(intel_dp);
4362c9916cdSFrançois Tigeot 	}
4372c9916cdSFrançois Tigeot 
438*aee94f86SFrançois Tigeot 	/*
439*aee94f86SFrançois Tigeot 	 * FIXME: Activation should happen immediately since this function
440*aee94f86SFrançois Tigeot 	 * is just called after pipe is fully trained and enabled.
441*aee94f86SFrançois Tigeot 	 * However on every platform we face issues when first activation
442*aee94f86SFrançois Tigeot 	 * follows a modeset so quickly.
443*aee94f86SFrançois Tigeot 	 *     - On VLV/CHV we get bank screen on first activation
444*aee94f86SFrançois Tigeot 	 *     - On HSW/BDW we get a recoverable frozen screen until next
445*aee94f86SFrançois Tigeot 	 *       exit-activate sequence.
446*aee94f86SFrançois Tigeot 	 */
447*aee94f86SFrançois Tigeot 	if (INTEL_INFO(dev)->gen < 9)
448*aee94f86SFrançois Tigeot 		schedule_delayed_work(&dev_priv->psr.work,
449*aee94f86SFrançois Tigeot 				      msecs_to_jiffies(intel_dp->panel_power_cycle_delay * 5));
450*aee94f86SFrançois Tigeot 
4512c9916cdSFrançois Tigeot 	dev_priv->psr.enabled = intel_dp;
4522c9916cdSFrançois Tigeot unlock:
4532c9916cdSFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
4542c9916cdSFrançois Tigeot }
4552c9916cdSFrançois Tigeot 
4562c9916cdSFrançois Tigeot static void vlv_psr_disable(struct intel_dp *intel_dp)
4572c9916cdSFrançois Tigeot {
4582c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
4592c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
4602c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
4612c9916cdSFrançois Tigeot 	struct intel_crtc *intel_crtc =
4622c9916cdSFrançois Tigeot 		to_intel_crtc(intel_dig_port->base.base.crtc);
4632c9916cdSFrançois Tigeot 	uint32_t val;
4642c9916cdSFrançois Tigeot 
4652c9916cdSFrançois Tigeot 	if (dev_priv->psr.active) {
4662c9916cdSFrançois Tigeot 		/* Put VLV PSR back to PSR_state 0 that is PSR Disabled. */
4672c9916cdSFrançois Tigeot 		if (wait_for((I915_READ(VLV_PSRSTAT(intel_crtc->pipe)) &
4682c9916cdSFrançois Tigeot 			      VLV_EDP_PSR_IN_TRANS) == 0, 1))
4692c9916cdSFrançois Tigeot 			WARN(1, "PSR transition took longer than expected\n");
4702c9916cdSFrançois Tigeot 
4712c9916cdSFrançois Tigeot 		val = I915_READ(VLV_PSRCTL(intel_crtc->pipe));
4722c9916cdSFrançois Tigeot 		val &= ~VLV_EDP_PSR_ACTIVE_ENTRY;
4732c9916cdSFrançois Tigeot 		val &= ~VLV_EDP_PSR_ENABLE;
4742c9916cdSFrançois Tigeot 		val &= ~VLV_EDP_PSR_MODE_MASK;
4752c9916cdSFrançois Tigeot 		I915_WRITE(VLV_PSRCTL(intel_crtc->pipe), val);
4762c9916cdSFrançois Tigeot 
4772c9916cdSFrançois Tigeot 		dev_priv->psr.active = false;
4782c9916cdSFrançois Tigeot 	} else {
4792c9916cdSFrançois Tigeot 		WARN_ON(vlv_is_psr_active_on_pipe(dev, intel_crtc->pipe));
4802c9916cdSFrançois Tigeot 	}
4812c9916cdSFrançois Tigeot }
4822c9916cdSFrançois Tigeot 
4832c9916cdSFrançois Tigeot static void hsw_psr_disable(struct intel_dp *intel_dp)
4842c9916cdSFrançois Tigeot {
4852c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
4862c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
4872c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
4882c9916cdSFrançois Tigeot 
4892c9916cdSFrançois Tigeot 	if (dev_priv->psr.active) {
490*aee94f86SFrançois Tigeot 		I915_WRITE(EDP_PSR_CTL,
491*aee94f86SFrançois Tigeot 			   I915_READ(EDP_PSR_CTL) & ~EDP_PSR_ENABLE);
4922c9916cdSFrançois Tigeot 
4932c9916cdSFrançois Tigeot 		/* Wait till PSR is idle */
494*aee94f86SFrançois Tigeot 		if (_wait_for((I915_READ(EDP_PSR_STATUS_CTL) &
4952c9916cdSFrançois Tigeot 			       EDP_PSR_STATUS_STATE_MASK) == 0, 2000, 10))
4962c9916cdSFrançois Tigeot 			DRM_ERROR("Timed out waiting for PSR Idle State\n");
4972c9916cdSFrançois Tigeot 
4982c9916cdSFrançois Tigeot 		dev_priv->psr.active = false;
4992c9916cdSFrançois Tigeot 	} else {
500*aee94f86SFrançois Tigeot 		WARN_ON(I915_READ(EDP_PSR_CTL) & EDP_PSR_ENABLE);
5012c9916cdSFrançois Tigeot 	}
5022c9916cdSFrançois Tigeot }
5032c9916cdSFrançois Tigeot 
5042c9916cdSFrançois Tigeot /**
5052c9916cdSFrançois Tigeot  * intel_psr_disable - Disable PSR
5062c9916cdSFrançois Tigeot  * @intel_dp: Intel DP
5072c9916cdSFrançois Tigeot  *
5082c9916cdSFrançois Tigeot  * This function needs to be called before disabling pipe.
5092c9916cdSFrançois Tigeot  */
5102c9916cdSFrançois Tigeot void intel_psr_disable(struct intel_dp *intel_dp)
5112c9916cdSFrançois Tigeot {
5122c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
5132c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
5142c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
5152c9916cdSFrançois Tigeot 
5162c9916cdSFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
5172c9916cdSFrançois Tigeot 	if (!dev_priv->psr.enabled) {
5182c9916cdSFrançois Tigeot 		mutex_unlock(&dev_priv->psr.lock);
5192c9916cdSFrançois Tigeot 		return;
5202c9916cdSFrançois Tigeot 	}
5212c9916cdSFrançois Tigeot 
522*aee94f86SFrançois Tigeot 	/* Disable PSR on Source */
5232c9916cdSFrançois Tigeot 	if (HAS_DDI(dev))
5242c9916cdSFrançois Tigeot 		hsw_psr_disable(intel_dp);
5252c9916cdSFrançois Tigeot 	else
5262c9916cdSFrançois Tigeot 		vlv_psr_disable(intel_dp);
5272c9916cdSFrançois Tigeot 
528*aee94f86SFrançois Tigeot 	/* Disable PSR on Sink */
529*aee94f86SFrançois Tigeot 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG, 0);
530*aee94f86SFrançois Tigeot 
5312c9916cdSFrançois Tigeot 	dev_priv->psr.enabled = NULL;
5322c9916cdSFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
5332c9916cdSFrançois Tigeot 
5342c9916cdSFrançois Tigeot 	cancel_delayed_work_sync(&dev_priv->psr.work);
5352c9916cdSFrançois Tigeot }
5362c9916cdSFrançois Tigeot 
5372c9916cdSFrançois Tigeot static void intel_psr_work(struct work_struct *work)
5382c9916cdSFrançois Tigeot {
5392c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv =
5402c9916cdSFrançois Tigeot 		container_of(work, typeof(*dev_priv), psr.work.work);
5412c9916cdSFrançois Tigeot 	struct intel_dp *intel_dp = dev_priv->psr.enabled;
5422c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = dp_to_dig_port(intel_dp)->base.base.crtc;
5432c9916cdSFrançois Tigeot 	enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
5442c9916cdSFrançois Tigeot 
5452c9916cdSFrançois Tigeot 	/* We have to make sure PSR is ready for re-enable
5462c9916cdSFrançois Tigeot 	 * otherwise it keeps disabled until next full enable/disable cycle.
5472c9916cdSFrançois Tigeot 	 * PSR might take some time to get fully disabled
5482c9916cdSFrançois Tigeot 	 * and be ready for re-enable.
5492c9916cdSFrançois Tigeot 	 */
5502c9916cdSFrançois Tigeot 	if (HAS_DDI(dev_priv->dev)) {
551*aee94f86SFrançois Tigeot 		if (wait_for((I915_READ(EDP_PSR_STATUS_CTL) &
5522c9916cdSFrançois Tigeot 			      EDP_PSR_STATUS_STATE_MASK) == 0, 50)) {
5532c9916cdSFrançois Tigeot 			DRM_ERROR("Timed out waiting for PSR Idle for re-enable\n");
5542c9916cdSFrançois Tigeot 			return;
5552c9916cdSFrançois Tigeot 		}
5562c9916cdSFrançois Tigeot 	} else {
5572c9916cdSFrançois Tigeot 		if (wait_for((I915_READ(VLV_PSRSTAT(pipe)) &
5582c9916cdSFrançois Tigeot 			      VLV_EDP_PSR_IN_TRANS) == 0, 1)) {
5592c9916cdSFrançois Tigeot 			DRM_ERROR("Timed out waiting for PSR Idle for re-enable\n");
5602c9916cdSFrançois Tigeot 			return;
5612c9916cdSFrançois Tigeot 		}
5622c9916cdSFrançois Tigeot 	}
5632c9916cdSFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
5642c9916cdSFrançois Tigeot 	intel_dp = dev_priv->psr.enabled;
5652c9916cdSFrançois Tigeot 
5662c9916cdSFrançois Tigeot 	if (!intel_dp)
5672c9916cdSFrançois Tigeot 		goto unlock;
5682c9916cdSFrançois Tigeot 
5692c9916cdSFrançois Tigeot 	/*
5702c9916cdSFrançois Tigeot 	 * The delayed work can race with an invalidate hence we need to
5712c9916cdSFrançois Tigeot 	 * recheck. Since psr_flush first clears this and then reschedules we
5722c9916cdSFrançois Tigeot 	 * won't ever miss a flush when bailing out here.
5732c9916cdSFrançois Tigeot 	 */
5742c9916cdSFrançois Tigeot 	if (dev_priv->psr.busy_frontbuffer_bits)
5752c9916cdSFrançois Tigeot 		goto unlock;
5762c9916cdSFrançois Tigeot 
5772c9916cdSFrançois Tigeot 	intel_psr_activate(intel_dp);
5782c9916cdSFrançois Tigeot unlock:
5792c9916cdSFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
5802c9916cdSFrançois Tigeot }
5812c9916cdSFrançois Tigeot 
5822c9916cdSFrançois Tigeot static void intel_psr_exit(struct drm_device *dev)
5832c9916cdSFrançois Tigeot {
5842c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
5852c9916cdSFrançois Tigeot 	struct intel_dp *intel_dp = dev_priv->psr.enabled;
5862c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = dp_to_dig_port(intel_dp)->base.base.crtc;
5872c9916cdSFrançois Tigeot 	enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
5882c9916cdSFrançois Tigeot 	u32 val;
5892c9916cdSFrançois Tigeot 
5902c9916cdSFrançois Tigeot 	if (!dev_priv->psr.active)
5912c9916cdSFrançois Tigeot 		return;
5922c9916cdSFrançois Tigeot 
5932c9916cdSFrançois Tigeot 	if (HAS_DDI(dev)) {
594*aee94f86SFrançois Tigeot 		val = I915_READ(EDP_PSR_CTL);
5952c9916cdSFrançois Tigeot 
5962c9916cdSFrançois Tigeot 		WARN_ON(!(val & EDP_PSR_ENABLE));
5972c9916cdSFrançois Tigeot 
598*aee94f86SFrançois Tigeot 		I915_WRITE(EDP_PSR_CTL, val & ~EDP_PSR_ENABLE);
5992c9916cdSFrançois Tigeot 	} else {
6002c9916cdSFrançois Tigeot 		val = I915_READ(VLV_PSRCTL(pipe));
6012c9916cdSFrançois Tigeot 
6022c9916cdSFrançois Tigeot 		/* Here we do the transition from PSR_state 3 to PSR_state 5
6032c9916cdSFrançois Tigeot 		 * directly once PSR State 4 that is active with single frame
6042c9916cdSFrançois Tigeot 		 * update can be skipped. PSR_state 5 that is PSR exit then
6052c9916cdSFrançois Tigeot 		 * Hardware is responsible to transition back to PSR_state 1
6062c9916cdSFrançois Tigeot 		 * that is PSR inactive. Same state after
6072c9916cdSFrançois Tigeot 		 * vlv_edp_psr_enable_source.
6082c9916cdSFrançois Tigeot 		 */
6092c9916cdSFrançois Tigeot 		val &= ~VLV_EDP_PSR_ACTIVE_ENTRY;
6102c9916cdSFrançois Tigeot 		I915_WRITE(VLV_PSRCTL(pipe), val);
6112c9916cdSFrançois Tigeot 
6122c9916cdSFrançois Tigeot 		/* Send AUX wake up - Spec says after transitioning to PSR
6132c9916cdSFrançois Tigeot 		 * active we have to send AUX wake up by writing 01h in DPCD
6142c9916cdSFrançois Tigeot 		 * 600h of sink device.
6152c9916cdSFrançois Tigeot 		 * XXX: This might slow down the transition, but without this
6162c9916cdSFrançois Tigeot 		 * HW doesn't complete the transition to PSR_state 1 and we
6172c9916cdSFrançois Tigeot 		 * never get the screen updated.
6182c9916cdSFrançois Tigeot 		 */
6192c9916cdSFrançois Tigeot 		drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
6202c9916cdSFrançois Tigeot 				   DP_SET_POWER_D0);
6212c9916cdSFrançois Tigeot 	}
6222c9916cdSFrançois Tigeot 
6232c9916cdSFrançois Tigeot 	dev_priv->psr.active = false;
6242c9916cdSFrançois Tigeot }
6252c9916cdSFrançois Tigeot 
6262c9916cdSFrançois Tigeot /**
62719c468b4SFrançois Tigeot  * intel_psr_single_frame_update - Single Frame Update
62819c468b4SFrançois Tigeot  * @dev: DRM device
629a05eeebfSFrançois Tigeot  * @frontbuffer_bits: frontbuffer plane tracking bits
63019c468b4SFrançois Tigeot  *
63119c468b4SFrançois Tigeot  * Some platforms support a single frame update feature that is used to
63219c468b4SFrançois Tigeot  * send and update only one frame on Remote Frame Buffer.
63319c468b4SFrançois Tigeot  * So far it is only implemented for Valleyview and Cherryview because
63419c468b4SFrançois Tigeot  * hardware requires this to be done before a page flip.
63519c468b4SFrançois Tigeot  */
636a05eeebfSFrançois Tigeot void intel_psr_single_frame_update(struct drm_device *dev,
637a05eeebfSFrançois Tigeot 				   unsigned frontbuffer_bits)
63819c468b4SFrançois Tigeot {
63919c468b4SFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
64019c468b4SFrançois Tigeot 	struct drm_crtc *crtc;
64119c468b4SFrançois Tigeot 	enum i915_pipe pipe;
64219c468b4SFrançois Tigeot 	u32 val;
64319c468b4SFrançois Tigeot 
64419c468b4SFrançois Tigeot 	/*
64519c468b4SFrançois Tigeot 	 * Single frame update is already supported on BDW+ but it requires
64619c468b4SFrançois Tigeot 	 * many W/A and it isn't really needed.
64719c468b4SFrançois Tigeot 	 */
648*aee94f86SFrançois Tigeot 	if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev))
64919c468b4SFrançois Tigeot 		return;
65019c468b4SFrançois Tigeot 
65119c468b4SFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
65219c468b4SFrançois Tigeot 	if (!dev_priv->psr.enabled) {
65319c468b4SFrançois Tigeot 		mutex_unlock(&dev_priv->psr.lock);
65419c468b4SFrançois Tigeot 		return;
65519c468b4SFrançois Tigeot 	}
65619c468b4SFrançois Tigeot 
65719c468b4SFrançois Tigeot 	crtc = dp_to_dig_port(dev_priv->psr.enabled)->base.base.crtc;
65819c468b4SFrançois Tigeot 	pipe = to_intel_crtc(crtc)->pipe;
659a05eeebfSFrançois Tigeot 
660a05eeebfSFrançois Tigeot 	if (frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)) {
66119c468b4SFrançois Tigeot 		val = I915_READ(VLV_PSRCTL(pipe));
66219c468b4SFrançois Tigeot 
66319c468b4SFrançois Tigeot 		/*
66419c468b4SFrançois Tigeot 		 * We need to set this bit before writing registers for a flip.
66519c468b4SFrançois Tigeot 		 * This bit will be self-clear when it gets to the PSR active state.
66619c468b4SFrançois Tigeot 		 */
66719c468b4SFrançois Tigeot 		I915_WRITE(VLV_PSRCTL(pipe), val | VLV_EDP_PSR_SINGLE_FRAME_UPDATE);
668a05eeebfSFrançois Tigeot 	}
66919c468b4SFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
67019c468b4SFrançois Tigeot }
67119c468b4SFrançois Tigeot 
67219c468b4SFrançois Tigeot /**
6732c9916cdSFrançois Tigeot  * intel_psr_invalidate - Invalidade PSR
6742c9916cdSFrançois Tigeot  * @dev: DRM device
6752c9916cdSFrançois Tigeot  * @frontbuffer_bits: frontbuffer plane tracking bits
6762c9916cdSFrançois Tigeot  *
6772c9916cdSFrançois Tigeot  * Since the hardware frontbuffer tracking has gaps we need to integrate
6782c9916cdSFrançois Tigeot  * with the software frontbuffer tracking. This function gets called every
6792c9916cdSFrançois Tigeot  * time frontbuffer rendering starts and a buffer gets dirtied. PSR must be
6802c9916cdSFrançois Tigeot  * disabled if the frontbuffer mask contains a buffer relevant to PSR.
6812c9916cdSFrançois Tigeot  *
6822c9916cdSFrançois Tigeot  * Dirty frontbuffers relevant to PSR are tracked in busy_frontbuffer_bits."
6832c9916cdSFrançois Tigeot  */
6842c9916cdSFrançois Tigeot void intel_psr_invalidate(struct drm_device *dev,
6852c9916cdSFrançois Tigeot 			  unsigned frontbuffer_bits)
6862c9916cdSFrançois Tigeot {
6872c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
6882c9916cdSFrançois Tigeot 	struct drm_crtc *crtc;
6892c9916cdSFrançois Tigeot 	enum i915_pipe pipe;
6902c9916cdSFrançois Tigeot 
6912c9916cdSFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
6922c9916cdSFrançois Tigeot 	if (!dev_priv->psr.enabled) {
6932c9916cdSFrançois Tigeot 		mutex_unlock(&dev_priv->psr.lock);
6942c9916cdSFrançois Tigeot 		return;
6952c9916cdSFrançois Tigeot 	}
6962c9916cdSFrançois Tigeot 
6972c9916cdSFrançois Tigeot 	crtc = dp_to_dig_port(dev_priv->psr.enabled)->base.base.crtc;
6982c9916cdSFrançois Tigeot 	pipe = to_intel_crtc(crtc)->pipe;
6992c9916cdSFrançois Tigeot 
700a05eeebfSFrançois Tigeot 	frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
701a05eeebfSFrançois Tigeot 	dev_priv->psr.busy_frontbuffer_bits |= frontbuffer_bits;
702a05eeebfSFrançois Tigeot 
703a05eeebfSFrançois Tigeot 	if (frontbuffer_bits)
7042c9916cdSFrançois Tigeot 		intel_psr_exit(dev);
7052c9916cdSFrançois Tigeot 
7062c9916cdSFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
7072c9916cdSFrançois Tigeot }
7082c9916cdSFrançois Tigeot 
7092c9916cdSFrançois Tigeot /**
7102c9916cdSFrançois Tigeot  * intel_psr_flush - Flush PSR
7112c9916cdSFrançois Tigeot  * @dev: DRM device
7122c9916cdSFrançois Tigeot  * @frontbuffer_bits: frontbuffer plane tracking bits
713a05eeebfSFrançois Tigeot  * @origin: which operation caused the flush
7142c9916cdSFrançois Tigeot  *
7152c9916cdSFrançois Tigeot  * Since the hardware frontbuffer tracking has gaps we need to integrate
7162c9916cdSFrançois Tigeot  * with the software frontbuffer tracking. This function gets called every
7172c9916cdSFrançois Tigeot  * time frontbuffer rendering has completed and flushed out to memory. PSR
7182c9916cdSFrançois Tigeot  * can be enabled again if no other frontbuffer relevant to PSR is dirty.
7192c9916cdSFrançois Tigeot  *
7202c9916cdSFrançois Tigeot  * Dirty frontbuffers relevant to PSR are tracked in busy_frontbuffer_bits.
7212c9916cdSFrançois Tigeot  */
7222c9916cdSFrançois Tigeot void intel_psr_flush(struct drm_device *dev,
723a05eeebfSFrançois Tigeot 		     unsigned frontbuffer_bits, enum fb_op_origin origin)
7242c9916cdSFrançois Tigeot {
7252c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
7262c9916cdSFrançois Tigeot 	struct drm_crtc *crtc;
7272c9916cdSFrançois Tigeot 	enum i915_pipe pipe;
7282c9916cdSFrançois Tigeot 
7292c9916cdSFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
7302c9916cdSFrançois Tigeot 	if (!dev_priv->psr.enabled) {
7312c9916cdSFrançois Tigeot 		mutex_unlock(&dev_priv->psr.lock);
7322c9916cdSFrançois Tigeot 		return;
7332c9916cdSFrançois Tigeot 	}
7342c9916cdSFrançois Tigeot 
7352c9916cdSFrançois Tigeot 	crtc = dp_to_dig_port(dev_priv->psr.enabled)->base.base.crtc;
7362c9916cdSFrançois Tigeot 	pipe = to_intel_crtc(crtc)->pipe;
737a05eeebfSFrançois Tigeot 
738a05eeebfSFrançois Tigeot 	frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
7392c9916cdSFrançois Tigeot 	dev_priv->psr.busy_frontbuffer_bits &= ~frontbuffer_bits;
7402c9916cdSFrançois Tigeot 
741*aee94f86SFrançois Tigeot 	/* By definition flush = invalidate + flush */
742a05eeebfSFrançois Tigeot 	if (frontbuffer_bits)
7432c9916cdSFrançois Tigeot 		intel_psr_exit(dev);
7442c9916cdSFrançois Tigeot 
7452c9916cdSFrançois Tigeot 	if (!dev_priv->psr.active && !dev_priv->psr.busy_frontbuffer_bits)
746*aee94f86SFrançois Tigeot #if 0
747*aee94f86SFrançois Tigeot 		if (!work_busy(&dev_priv->psr.work.work))
748*aee94f86SFrançois Tigeot #endif
7492c9916cdSFrançois Tigeot 			schedule_delayed_work(&dev_priv->psr.work,
750*aee94f86SFrançois Tigeot 					      msecs_to_jiffies(100));
7512c9916cdSFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
7522c9916cdSFrançois Tigeot }
7532c9916cdSFrançois Tigeot 
7542c9916cdSFrançois Tigeot /**
7552c9916cdSFrançois Tigeot  * intel_psr_init - Init basic PSR work and mutex.
7562c9916cdSFrançois Tigeot  * @dev: DRM device
7572c9916cdSFrançois Tigeot  *
7582c9916cdSFrançois Tigeot  * This function is  called only once at driver load to initialize basic
7592c9916cdSFrançois Tigeot  * PSR stuff.
7602c9916cdSFrançois Tigeot  */
7612c9916cdSFrançois Tigeot void intel_psr_init(struct drm_device *dev)
7622c9916cdSFrançois Tigeot {
7632c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv = dev->dev_private;
7642c9916cdSFrançois Tigeot 
765*aee94f86SFrançois Tigeot 	dev_priv->psr_mmio_base = IS_HASWELL(dev_priv) ?
766*aee94f86SFrançois Tigeot 		HSW_EDP_PSR_BASE : BDW_EDP_PSR_BASE;
767*aee94f86SFrançois Tigeot 
7682c9916cdSFrançois Tigeot 	INIT_DELAYED_WORK(&dev_priv->psr.work, intel_psr_work);
7692c9916cdSFrançois Tigeot 	lockinit(&dev_priv->psr.lock, "i915dpl", 0, LK_CANRECURSE);
7702c9916cdSFrançois Tigeot }
771