xref: /dflybsd-src/sys/dev/drm/i915/intel_psr.c (revision bf0175970ba1c394f8039834a26ca6d163b23d0e)
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 {
66*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
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;
80*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
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;
83aee94f86SFrançois Tigeot 	i915_reg_t ctl_reg = HSW_TVIDEO_DIP_CTL(cpu_transcoder);
84*bf017597SFrançois Tigeot 	uint32_t *data = (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;
110*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
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 
154aee94f86SFrançois Tigeot static i915_reg_t psr_aux_ctl_reg(struct drm_i915_private *dev_priv,
155aee94f86SFrançois Tigeot 				       enum port port)
156aee94f86SFrançois Tigeot {
157aee94f86SFrançois Tigeot 	if (INTEL_INFO(dev_priv)->gen >= 9)
158aee94f86SFrançois Tigeot 		return DP_AUX_CH_CTL(port);
159aee94f86SFrançois Tigeot 	else
160aee94f86SFrançois Tigeot 		return EDP_PSR_AUX_CTL;
161aee94f86SFrançois Tigeot }
162aee94f86SFrançois Tigeot 
163aee94f86SFrançois Tigeot static i915_reg_t psr_aux_data_reg(struct drm_i915_private *dev_priv,
164aee94f86SFrançois Tigeot 					enum port port, int index)
165aee94f86SFrançois Tigeot {
166aee94f86SFrançois Tigeot 	if (INTEL_INFO(dev_priv)->gen >= 9)
167aee94f86SFrançois Tigeot 		return DP_AUX_CH_DATA(port, index);
168aee94f86SFrançois Tigeot 	else
169aee94f86SFrançois Tigeot 		return EDP_PSR_AUX_DATA(index);
170aee94f86SFrançois Tigeot }
171aee94f86SFranç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;
176*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
1772c9916cdSFrançois Tigeot 	uint32_t aux_clock_divider;
178aee94f86SFrançois Tigeot 	i915_reg_t aux_ctl_reg;
1792c9916cdSFrançois Tigeot 	static const uint8_t aux_msg[] = {
1802c9916cdSFrançois Tigeot 		[0] = DP_AUX_NATIVE_WRITE << 4,
1812c9916cdSFrançois Tigeot 		[1] = DP_SET_POWER >> 8,
1822c9916cdSFrançois Tigeot 		[2] = DP_SET_POWER & 0xff,
1832c9916cdSFrançois Tigeot 		[3] = 1 - 1,
1842c9916cdSFrançois Tigeot 		[4] = DP_SET_POWER_D0,
1852c9916cdSFrançois Tigeot 	};
186aee94f86SFrançois Tigeot 	enum port port = dig_port->port;
1871487f786SFrançois Tigeot 	u32 aux_ctl;
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 
2001487f786SFrançois Tigeot 	if (dev_priv->psr.link_standby)
2011487f786SFrançois Tigeot 		drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG,
2021487f786SFrançois Tigeot 				   DP_PSR_ENABLE | DP_PSR_MAIN_LINK_ACTIVE);
2031487f786SFrançois Tigeot 	else
2041487f786SFrançois Tigeot 		drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG,
2051487f786SFrançois Tigeot 				   DP_PSR_ENABLE);
2061487f786SFrançois Tigeot 
207aee94f86SFrançois Tigeot 	aux_ctl_reg = psr_aux_ctl_reg(dev_priv, port);
2082c9916cdSFrançois Tigeot 
2092c9916cdSFrançois Tigeot 	/* Setup AUX registers */
2102c9916cdSFrançois Tigeot 	for (i = 0; i < sizeof(aux_msg); i += 4)
211aee94f86SFrançois Tigeot 		I915_WRITE(psr_aux_data_reg(dev_priv, port, i >> 2),
2122c9916cdSFrançois Tigeot 			   intel_dp_pack_aux(&aux_msg[i], sizeof(aux_msg) - i));
2132c9916cdSFrançois Tigeot 
2141487f786SFrançois Tigeot 	aux_ctl = intel_dp->get_aux_send_ctl(intel_dp, 0, sizeof(aux_msg),
2151487f786SFrançois Tigeot 					     aux_clock_divider);
2161487f786SFrançois Tigeot 	I915_WRITE(aux_ctl_reg, aux_ctl);
2172c9916cdSFrançois Tigeot }
2182c9916cdSFrançois Tigeot 
2192c9916cdSFrançois Tigeot static void vlv_psr_enable_source(struct intel_dp *intel_dp)
2202c9916cdSFrançois Tigeot {
2212c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2222c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
223*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
2242c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = dig_port->base.base.crtc;
2252c9916cdSFrançois Tigeot 	enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
2262c9916cdSFrançois Tigeot 
2272c9916cdSFrançois Tigeot 	/* Transition from PSR_state 0 to PSR_state 1, i.e. PSR Inactive */
2282c9916cdSFrançois Tigeot 	I915_WRITE(VLV_PSRCTL(pipe),
2292c9916cdSFrançois Tigeot 		   VLV_EDP_PSR_MODE_SW_TIMER |
2302c9916cdSFrançois Tigeot 		   VLV_EDP_PSR_SRC_TRANSMITTER_STATE |
2312c9916cdSFrançois Tigeot 		   VLV_EDP_PSR_ENABLE);
2322c9916cdSFrançois Tigeot }
2332c9916cdSFrançois Tigeot 
2342c9916cdSFrançois Tigeot static void vlv_psr_activate(struct intel_dp *intel_dp)
2352c9916cdSFrançois Tigeot {
2362c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2372c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
238*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
2392c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = dig_port->base.base.crtc;
2402c9916cdSFrançois Tigeot 	enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
2412c9916cdSFrançois Tigeot 
2422c9916cdSFrançois Tigeot 	/* Let's do the transition from PSR_state 1 to PSR_state 2
2432c9916cdSFrançois Tigeot 	 * that is PSR transition to active - static frame transmission.
2442c9916cdSFrançois Tigeot 	 * Then Hardware is responsible for the transition to PSR_state 3
2452c9916cdSFrançois Tigeot 	 * that is PSR active - no Remote Frame Buffer (RFB) update.
2462c9916cdSFrançois Tigeot 	 */
2472c9916cdSFrançois Tigeot 	I915_WRITE(VLV_PSRCTL(pipe), I915_READ(VLV_PSRCTL(pipe)) |
2482c9916cdSFrançois Tigeot 		   VLV_EDP_PSR_ACTIVE_ENTRY);
2492c9916cdSFrançois Tigeot }
2502c9916cdSFrançois Tigeot 
2512c9916cdSFrançois Tigeot static void hsw_psr_enable_source(struct intel_dp *intel_dp)
2522c9916cdSFrançois Tigeot {
2532c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2542c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
255*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
25619c468b4SFrançois Tigeot 
2572c9916cdSFrançois Tigeot 	uint32_t max_sleep_time = 0x1f;
258*bf017597SFrançois Tigeot 	/*
259*bf017597SFrançois Tigeot 	 * Let's respect VBT in case VBT asks a higher idle_frame value.
260*bf017597SFrançois Tigeot 	 * Let's use 6 as the minimum to cover all known cases including
261*bf017597SFrançois Tigeot 	 * the off-by-one issue that HW has in some cases. Also there are
262*bf017597SFrançois Tigeot 	 * cases where sink should be able to train
263*bf017597SFrançois Tigeot 	 * with the 5 or 6 idle patterns.
2642c9916cdSFrançois Tigeot 	 */
265*bf017597SFrançois Tigeot 	uint32_t idle_frames = max(6, dev_priv->vbt.psr.idle_frames);
266c62a0e9aSFrançois Tigeot 	uint32_t val = EDP_PSR_ENABLE;
267c62a0e9aSFrançois Tigeot 
268c62a0e9aSFrançois Tigeot 	val |= max_sleep_time << EDP_PSR_MAX_SLEEP_TIME_SHIFT;
269c62a0e9aSFrançois Tigeot 	val |= idle_frames << EDP_PSR_IDLE_FRAME_SHIFT;
2702c9916cdSFrançois Tigeot 
271aee94f86SFrançois Tigeot 	if (IS_HASWELL(dev))
272aee94f86SFrançois Tigeot 		val |= EDP_PSR_MIN_LINK_ENTRY_TIME_8_LINES;
2732c9916cdSFrançois Tigeot 
274c0e85e96SFrançois Tigeot 	if (dev_priv->psr.link_standby)
275c0e85e96SFrançois Tigeot 		val |= EDP_PSR_LINK_STANDBY;
276c0e85e96SFrançois Tigeot 
277c62a0e9aSFrançois Tigeot 	if (dev_priv->vbt.psr.tp1_wakeup_time > 5)
278c62a0e9aSFrançois Tigeot 		val |= EDP_PSR_TP1_TIME_2500us;
279c62a0e9aSFrançois Tigeot 	else if (dev_priv->vbt.psr.tp1_wakeup_time > 1)
280c62a0e9aSFrançois Tigeot 		val |= EDP_PSR_TP1_TIME_500us;
281c62a0e9aSFrançois Tigeot 	else if (dev_priv->vbt.psr.tp1_wakeup_time > 0)
282c62a0e9aSFrançois Tigeot 		val |= EDP_PSR_TP1_TIME_100us;
283c62a0e9aSFrançois Tigeot 	else
284c62a0e9aSFrançois Tigeot 		val |= EDP_PSR_TP1_TIME_0us;
28519c468b4SFrançois Tigeot 
286c62a0e9aSFrançois Tigeot 	if (dev_priv->vbt.psr.tp2_tp3_wakeup_time > 5)
287c62a0e9aSFrançois Tigeot 		val |= EDP_PSR_TP2_TP3_TIME_2500us;
288c62a0e9aSFrançois Tigeot 	else if (dev_priv->vbt.psr.tp2_tp3_wakeup_time > 1)
289c62a0e9aSFrançois Tigeot 		val |= EDP_PSR_TP2_TP3_TIME_500us;
290c62a0e9aSFrançois Tigeot 	else if (dev_priv->vbt.psr.tp2_tp3_wakeup_time > 0)
291c62a0e9aSFrançois Tigeot 		val |= EDP_PSR_TP2_TP3_TIME_100us;
292c62a0e9aSFrançois Tigeot 	else
293c62a0e9aSFrançois Tigeot 		val |= EDP_PSR_TP2_TP3_TIME_0us;
294c62a0e9aSFrançois Tigeot 
295c62a0e9aSFrançois Tigeot 	if (intel_dp_source_supports_hbr2(intel_dp) &&
296c62a0e9aSFrançois Tigeot 	    drm_dp_tps3_supported(intel_dp->dpcd))
297c62a0e9aSFrançois Tigeot 		val |= EDP_PSR_TP1_TP3_SEL;
298c62a0e9aSFrançois Tigeot 	else
299c62a0e9aSFrançois Tigeot 		val |= EDP_PSR_TP1_TP2_SEL;
300c62a0e9aSFrançois Tigeot 
301c62a0e9aSFrançois Tigeot 	I915_WRITE(EDP_PSR_CTL, val);
302c62a0e9aSFrançois Tigeot 
303c62a0e9aSFrançois Tigeot 	if (!dev_priv->psr.psr2_support)
304c62a0e9aSFrançois Tigeot 		return;
305c62a0e9aSFrançois Tigeot 
306c62a0e9aSFrançois Tigeot 	/* FIXME: selective update is probably totally broken because it doesn't
307c62a0e9aSFrançois Tigeot 	 * mesh at all with our frontbuffer tracking. And the hw alone isn't
308c62a0e9aSFrançois Tigeot 	 * good enough. */
309c62a0e9aSFrançois Tigeot 	val = EDP_PSR2_ENABLE | EDP_SU_TRACK_ENABLE;
310c62a0e9aSFrançois Tigeot 
311c62a0e9aSFrançois Tigeot 	if (dev_priv->vbt.psr.tp2_tp3_wakeup_time > 5)
312c62a0e9aSFrançois Tigeot 		val |= EDP_PSR2_TP2_TIME_2500;
313c62a0e9aSFrançois Tigeot 	else if (dev_priv->vbt.psr.tp2_tp3_wakeup_time > 1)
314c62a0e9aSFrançois Tigeot 		val |= EDP_PSR2_TP2_TIME_500;
315c62a0e9aSFrançois Tigeot 	else if (dev_priv->vbt.psr.tp2_tp3_wakeup_time > 0)
316c62a0e9aSFrançois Tigeot 		val |= EDP_PSR2_TP2_TIME_100;
317c62a0e9aSFrançois Tigeot 	else
318c62a0e9aSFrançois Tigeot 		val |= EDP_PSR2_TP2_TIME_50;
319c62a0e9aSFrançois Tigeot 
320c62a0e9aSFrançois Tigeot 	I915_WRITE(EDP_PSR2_CTL, val);
3212c9916cdSFrançois Tigeot }
3222c9916cdSFrançois Tigeot 
3232c9916cdSFrançois Tigeot static bool intel_psr_match_conditions(struct intel_dp *intel_dp)
3242c9916cdSFrançois Tigeot {
3252c9916cdSFrançois Tigeot 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3262c9916cdSFrançois Tigeot 	struct drm_device *dev = dig_port->base.base.dev;
327*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
3282c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = dig_port->base.base.crtc;
3292c9916cdSFrançois Tigeot 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
330*bf017597SFrançois Tigeot 	const struct drm_display_mode *adjusted_mode =
331*bf017597SFrançois Tigeot 		&intel_crtc->config->base.adjusted_mode;
332*bf017597SFrançois Tigeot 	int psr_setup_time;
3332c9916cdSFrançois Tigeot 
3342c9916cdSFrançois Tigeot 	lockdep_assert_held(&dev_priv->psr.lock);
3352c9916cdSFrançois Tigeot 	WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
3362c9916cdSFrançois Tigeot 	WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
3372c9916cdSFrançois Tigeot 
3382c9916cdSFrançois Tigeot 	dev_priv->psr.source_ok = false;
3392c9916cdSFrançois Tigeot 
340c0e85e96SFrançois Tigeot 	/*
341c0e85e96SFrançois Tigeot 	 * HSW spec explicitly says PSR is tied to port A.
342c0e85e96SFrançois Tigeot 	 * BDW+ platforms with DDI implementation of PSR have different
343c0e85e96SFrançois Tigeot 	 * PSR registers per transcoder and we only implement transcoder EDP
344c0e85e96SFrançois Tigeot 	 * ones. Since by Display design transcoder EDP is tied to port A
345c0e85e96SFrançois Tigeot 	 * we can safely escape based on the port A.
346c0e85e96SFrançois Tigeot 	 */
347c0e85e96SFrançois Tigeot 	if (HAS_DDI(dev) && dig_port->port != PORT_A) {
348c0e85e96SFrançois Tigeot 		DRM_DEBUG_KMS("PSR condition failed: Port not supported\n");
3492c9916cdSFrançois Tigeot 		return false;
3502c9916cdSFrançois Tigeot 	}
3512c9916cdSFrançois Tigeot 
3522c9916cdSFrançois Tigeot 	if (!i915.enable_psr) {
3532c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR disable by flag\n");
3542c9916cdSFrançois Tigeot 		return false;
3552c9916cdSFrançois Tigeot 	}
3562c9916cdSFrançois Tigeot 
357c0e85e96SFrançois Tigeot 	if ((IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) &&
358c0e85e96SFrançois Tigeot 	    !dev_priv->psr.link_standby) {
359c0e85e96SFrançois Tigeot 		DRM_ERROR("PSR condition failed: Link off requested but not supported on this platform\n");
360c0e85e96SFrançois Tigeot 		return false;
361c0e85e96SFrançois Tigeot 	}
362c0e85e96SFrançois Tigeot 
3632c9916cdSFrançois Tigeot 	if (IS_HASWELL(dev) &&
3642c9916cdSFrançois Tigeot 	    I915_READ(HSW_STEREO_3D_CTL(intel_crtc->config->cpu_transcoder)) &
3652c9916cdSFrançois Tigeot 		      S3D_ENABLE) {
3662c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR condition failed: Stereo 3D is Enabled\n");
3672c9916cdSFrançois Tigeot 		return false;
3682c9916cdSFrançois Tigeot 	}
3692c9916cdSFrançois Tigeot 
3702c9916cdSFrançois Tigeot 	if (IS_HASWELL(dev) &&
371*bf017597SFrançois Tigeot 	    adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
3722c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR condition failed: Interlaced is Enabled\n");
3732c9916cdSFrançois Tigeot 		return false;
3742c9916cdSFrançois Tigeot 	}
3752c9916cdSFrançois Tigeot 
376*bf017597SFrançois Tigeot 	psr_setup_time = drm_dp_psr_setup_time(intel_dp->psr_dpcd);
377*bf017597SFrançois Tigeot 	if (psr_setup_time < 0) {
378*bf017597SFrançois Tigeot 		DRM_DEBUG_KMS("PSR condition failed: Invalid PSR setup time (0x%02x)\n",
379*bf017597SFrançois Tigeot 			      intel_dp->psr_dpcd[1]);
380*bf017597SFrançois Tigeot 		return false;
381*bf017597SFrançois Tigeot 	}
382*bf017597SFrançois Tigeot 
383*bf017597SFrançois Tigeot 	if (intel_usecs_to_scanlines(adjusted_mode, psr_setup_time) >
384*bf017597SFrançois Tigeot 	    adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vdisplay - 1) {
385*bf017597SFrançois Tigeot 		DRM_DEBUG_KMS("PSR condition failed: PSR setup time (%d us) too long\n",
386*bf017597SFrançois Tigeot 			      psr_setup_time);
387*bf017597SFrançois Tigeot 		return false;
388*bf017597SFrançois Tigeot 	}
389*bf017597SFrançois Tigeot 
3902c9916cdSFrançois Tigeot 	dev_priv->psr.source_ok = true;
3912c9916cdSFrançois Tigeot 	return true;
3922c9916cdSFrançois Tigeot }
3932c9916cdSFrançois Tigeot 
3942c9916cdSFrançois Tigeot static void intel_psr_activate(struct intel_dp *intel_dp)
3952c9916cdSFrançois Tigeot {
3962c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
3972c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
398*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
3992c9916cdSFrançois Tigeot 
400aee94f86SFrançois Tigeot 	WARN_ON(I915_READ(EDP_PSR_CTL) & EDP_PSR_ENABLE);
4012c9916cdSFrançois Tigeot 	WARN_ON(dev_priv->psr.active);
4022c9916cdSFrançois Tigeot 	lockdep_assert_held(&dev_priv->psr.lock);
4032c9916cdSFrançois Tigeot 
4042c9916cdSFrançois Tigeot 	/* Enable/Re-enable PSR on the host */
4052c9916cdSFrançois Tigeot 	if (HAS_DDI(dev))
4062c9916cdSFrançois Tigeot 		/* On HSW+ after we enable PSR on source it will activate it
4072c9916cdSFrançois Tigeot 		 * as soon as it match configure idle_frame count. So
4082c9916cdSFrançois Tigeot 		 * we just actually enable it here on activation time.
4092c9916cdSFrançois Tigeot 		 */
4102c9916cdSFrançois Tigeot 		hsw_psr_enable_source(intel_dp);
4112c9916cdSFrançois Tigeot 	else
4122c9916cdSFrançois Tigeot 		vlv_psr_activate(intel_dp);
4132c9916cdSFrançois Tigeot 
4142c9916cdSFrançois Tigeot 	dev_priv->psr.active = true;
4152c9916cdSFrançois Tigeot }
4162c9916cdSFrançois Tigeot 
4172c9916cdSFrançois Tigeot /**
4182c9916cdSFrançois Tigeot  * intel_psr_enable - Enable PSR
4192c9916cdSFrançois Tigeot  * @intel_dp: Intel DP
4202c9916cdSFrançois Tigeot  *
4212c9916cdSFrançois Tigeot  * This function can only be called after the pipe is fully trained and enabled.
4222c9916cdSFrançois Tigeot  */
4232c9916cdSFrançois Tigeot void intel_psr_enable(struct intel_dp *intel_dp)
4242c9916cdSFrançois Tigeot {
4252c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
4262c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
427*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
42819c468b4SFrançois Tigeot 	struct intel_crtc *crtc = to_intel_crtc(intel_dig_port->base.base.crtc);
4292c9916cdSFrançois Tigeot 
4302c9916cdSFrançois Tigeot 	if (!HAS_PSR(dev)) {
4312c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR not supported on this platform\n");
4322c9916cdSFrançois Tigeot 		return;
4332c9916cdSFrançois Tigeot 	}
4342c9916cdSFrançois Tigeot 
4352c9916cdSFrançois Tigeot 	if (!is_edp_psr(intel_dp)) {
4362c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR not supported by this panel\n");
4372c9916cdSFrançois Tigeot 		return;
4382c9916cdSFrançois Tigeot 	}
4392c9916cdSFrançois Tigeot 
4402c9916cdSFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
4412c9916cdSFrançois Tigeot 	if (dev_priv->psr.enabled) {
4422c9916cdSFrançois Tigeot 		DRM_DEBUG_KMS("PSR already in use\n");
4432c9916cdSFrançois Tigeot 		goto unlock;
4442c9916cdSFrançois Tigeot 	}
4452c9916cdSFrançois Tigeot 
4462c9916cdSFrançois Tigeot 	if (!intel_psr_match_conditions(intel_dp))
4472c9916cdSFrançois Tigeot 		goto unlock;
4482c9916cdSFrançois Tigeot 
4492c9916cdSFrançois Tigeot 	dev_priv->psr.busy_frontbuffer_bits = 0;
4502c9916cdSFrançois Tigeot 
4512c9916cdSFrançois Tigeot 	if (HAS_DDI(dev)) {
4522c9916cdSFrançois Tigeot 		hsw_psr_setup_vsc(intel_dp);
4532c9916cdSFrançois Tigeot 
45419c468b4SFrançois Tigeot 		if (dev_priv->psr.psr2_support) {
45519c468b4SFrançois Tigeot 			/* PSR2 is restricted to work with panel resolutions upto 3200x2000 */
45619c468b4SFrançois Tigeot 			if (crtc->config->pipe_src_w > 3200 ||
45719c468b4SFrançois Tigeot 				crtc->config->pipe_src_h > 2000)
45819c468b4SFrançois Tigeot 				dev_priv->psr.psr2_support = false;
45919c468b4SFrançois Tigeot 			else
46019c468b4SFrançois Tigeot 				skl_psr_setup_su_vsc(intel_dp);
46119c468b4SFrançois Tigeot 		}
46219c468b4SFrançois Tigeot 
463aee94f86SFrançois Tigeot 		/*
464aee94f86SFrançois Tigeot 		 * Per Spec: Avoid continuous PSR exit by masking MEMUP and HPD.
465aee94f86SFrançois Tigeot 		 * Also mask LPSP to avoid dependency on other drivers that
466aee94f86SFrançois Tigeot 		 * might block runtime_pm besides preventing other hw tracking
467aee94f86SFrançois Tigeot 		 * issues now we can rely on frontbuffer tracking.
468aee94f86SFrançois Tigeot 		 */
469aee94f86SFrançois Tigeot 		I915_WRITE(EDP_PSR_DEBUG_CTL, EDP_PSR_DEBUG_MASK_MEMUP |
470aee94f86SFrançois Tigeot 			   EDP_PSR_DEBUG_MASK_HPD | EDP_PSR_DEBUG_MASK_LPSP);
4712c9916cdSFrançois Tigeot 
4722c9916cdSFrançois Tigeot 		/* Enable PSR on the panel */
4732c9916cdSFrançois Tigeot 		hsw_psr_enable_sink(intel_dp);
4742c9916cdSFrançois Tigeot 
4752c9916cdSFrançois Tigeot 		if (INTEL_INFO(dev)->gen >= 9)
4762c9916cdSFrançois Tigeot 			intel_psr_activate(intel_dp);
4772c9916cdSFrançois Tigeot 	} else {
4782c9916cdSFrançois Tigeot 		vlv_psr_setup_vsc(intel_dp);
4792c9916cdSFrançois Tigeot 
4802c9916cdSFrançois Tigeot 		/* Enable PSR on the panel */
4812c9916cdSFrançois Tigeot 		vlv_psr_enable_sink(intel_dp);
4822c9916cdSFrançois Tigeot 
4832c9916cdSFrançois Tigeot 		/* On HSW+ enable_source also means go to PSR entry/active
4842c9916cdSFrançois Tigeot 		 * state as soon as idle_frame achieved and here would be
4852c9916cdSFrançois Tigeot 		 * to soon. However on VLV enable_source just enable PSR
4862c9916cdSFrançois Tigeot 		 * but let it on inactive state. So we might do this prior
4872c9916cdSFrançois Tigeot 		 * to active transition, i.e. here.
4882c9916cdSFrançois Tigeot 		 */
4892c9916cdSFrançois Tigeot 		vlv_psr_enable_source(intel_dp);
4902c9916cdSFrançois Tigeot 	}
4912c9916cdSFrançois Tigeot 
492aee94f86SFrançois Tigeot 	/*
493aee94f86SFrançois Tigeot 	 * FIXME: Activation should happen immediately since this function
494aee94f86SFrançois Tigeot 	 * is just called after pipe is fully trained and enabled.
495aee94f86SFrançois Tigeot 	 * However on every platform we face issues when first activation
496aee94f86SFrançois Tigeot 	 * follows a modeset so quickly.
497aee94f86SFrançois Tigeot 	 *     - On VLV/CHV we get bank screen on first activation
498aee94f86SFrançois Tigeot 	 *     - On HSW/BDW we get a recoverable frozen screen until next
499aee94f86SFrançois Tigeot 	 *       exit-activate sequence.
500aee94f86SFrançois Tigeot 	 */
501aee94f86SFrançois Tigeot 	if (INTEL_INFO(dev)->gen < 9)
502aee94f86SFrançois Tigeot 		schedule_delayed_work(&dev_priv->psr.work,
503aee94f86SFrançois Tigeot 				      msecs_to_jiffies(intel_dp->panel_power_cycle_delay * 5));
504aee94f86SFrançois Tigeot 
5052c9916cdSFrançois Tigeot 	dev_priv->psr.enabled = intel_dp;
5062c9916cdSFrançois Tigeot unlock:
5072c9916cdSFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
5082c9916cdSFrançois Tigeot }
5092c9916cdSFrançois Tigeot 
5102c9916cdSFrançois Tigeot static void vlv_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;
514*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
5152c9916cdSFrançois Tigeot 	struct intel_crtc *intel_crtc =
5162c9916cdSFrançois Tigeot 		to_intel_crtc(intel_dig_port->base.base.crtc);
5172c9916cdSFrançois Tigeot 	uint32_t val;
5182c9916cdSFrançois Tigeot 
5192c9916cdSFrançois Tigeot 	if (dev_priv->psr.active) {
5202c9916cdSFrançois Tigeot 		/* Put VLV PSR back to PSR_state 0 that is PSR Disabled. */
5211487f786SFrançois Tigeot 		if (intel_wait_for_register(dev_priv,
5221487f786SFrançois Tigeot 					    VLV_PSRSTAT(intel_crtc->pipe),
5231487f786SFrançois Tigeot 					    VLV_EDP_PSR_IN_TRANS,
5241487f786SFrançois Tigeot 					    0,
5251487f786SFrançois Tigeot 					    1))
5262c9916cdSFrançois Tigeot 			WARN(1, "PSR transition took longer than expected\n");
5272c9916cdSFrançois Tigeot 
5282c9916cdSFrançois Tigeot 		val = I915_READ(VLV_PSRCTL(intel_crtc->pipe));
5292c9916cdSFrançois Tigeot 		val &= ~VLV_EDP_PSR_ACTIVE_ENTRY;
5302c9916cdSFrançois Tigeot 		val &= ~VLV_EDP_PSR_ENABLE;
5312c9916cdSFrançois Tigeot 		val &= ~VLV_EDP_PSR_MODE_MASK;
5322c9916cdSFrançois Tigeot 		I915_WRITE(VLV_PSRCTL(intel_crtc->pipe), val);
5332c9916cdSFrançois Tigeot 
5342c9916cdSFrançois Tigeot 		dev_priv->psr.active = false;
5352c9916cdSFrançois Tigeot 	} else {
5362c9916cdSFrançois Tigeot 		WARN_ON(vlv_is_psr_active_on_pipe(dev, intel_crtc->pipe));
5372c9916cdSFrançois Tigeot 	}
5382c9916cdSFrançois Tigeot }
5392c9916cdSFrançois Tigeot 
5402c9916cdSFrançois Tigeot static void hsw_psr_disable(struct intel_dp *intel_dp)
5412c9916cdSFrançois Tigeot {
5422c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
5432c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
544*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
5452c9916cdSFrançois Tigeot 
5462c9916cdSFrançois Tigeot 	if (dev_priv->psr.active) {
547aee94f86SFrançois Tigeot 		I915_WRITE(EDP_PSR_CTL,
548aee94f86SFrançois Tigeot 			   I915_READ(EDP_PSR_CTL) & ~EDP_PSR_ENABLE);
5492c9916cdSFrançois Tigeot 
5502c9916cdSFrançois Tigeot 		/* Wait till PSR is idle */
5511487f786SFrançois Tigeot 		if (intel_wait_for_register(dev_priv,
5521487f786SFrançois Tigeot 					    EDP_PSR_STATUS_CTL,
5531487f786SFrançois Tigeot 					    EDP_PSR_STATUS_STATE_MASK,
5541487f786SFrançois Tigeot 					    0,
5551487f786SFrançois Tigeot 					    2000))
5562c9916cdSFrançois Tigeot 			DRM_ERROR("Timed out waiting for PSR Idle State\n");
5572c9916cdSFrançois Tigeot 
5582c9916cdSFrançois Tigeot 		dev_priv->psr.active = false;
5592c9916cdSFrançois Tigeot 	} else {
560aee94f86SFrançois Tigeot 		WARN_ON(I915_READ(EDP_PSR_CTL) & EDP_PSR_ENABLE);
5612c9916cdSFrançois Tigeot 	}
5622c9916cdSFrançois Tigeot }
5632c9916cdSFrançois Tigeot 
5642c9916cdSFrançois Tigeot /**
5652c9916cdSFrançois Tigeot  * intel_psr_disable - Disable PSR
5662c9916cdSFrançois Tigeot  * @intel_dp: Intel DP
5672c9916cdSFrançois Tigeot  *
5682c9916cdSFrançois Tigeot  * This function needs to be called before disabling pipe.
5692c9916cdSFrançois Tigeot  */
5702c9916cdSFrançois Tigeot void intel_psr_disable(struct intel_dp *intel_dp)
5712c9916cdSFrançois Tigeot {
5722c9916cdSFrançois Tigeot 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
5732c9916cdSFrançois Tigeot 	struct drm_device *dev = intel_dig_port->base.base.dev;
574*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
5752c9916cdSFrançois Tigeot 
5762c9916cdSFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
5772c9916cdSFrançois Tigeot 	if (!dev_priv->psr.enabled) {
5782c9916cdSFrançois Tigeot 		mutex_unlock(&dev_priv->psr.lock);
5792c9916cdSFrançois Tigeot 		return;
5802c9916cdSFrançois Tigeot 	}
5812c9916cdSFrançois Tigeot 
582aee94f86SFrançois Tigeot 	/* Disable PSR on Source */
5832c9916cdSFrançois Tigeot 	if (HAS_DDI(dev))
5842c9916cdSFrançois Tigeot 		hsw_psr_disable(intel_dp);
5852c9916cdSFrançois Tigeot 	else
5862c9916cdSFrançois Tigeot 		vlv_psr_disable(intel_dp);
5872c9916cdSFrançois Tigeot 
588aee94f86SFrançois Tigeot 	/* Disable PSR on Sink */
589aee94f86SFrançois Tigeot 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG, 0);
590aee94f86SFrançois Tigeot 
5912c9916cdSFrançois Tigeot 	dev_priv->psr.enabled = NULL;
5922c9916cdSFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
5932c9916cdSFrançois Tigeot 
5942c9916cdSFrançois Tigeot 	cancel_delayed_work_sync(&dev_priv->psr.work);
5952c9916cdSFrançois Tigeot }
5962c9916cdSFrançois Tigeot 
5972c9916cdSFrançois Tigeot static void intel_psr_work(struct work_struct *work)
5982c9916cdSFrançois Tigeot {
5992c9916cdSFrançois Tigeot 	struct drm_i915_private *dev_priv =
6002c9916cdSFrançois Tigeot 		container_of(work, typeof(*dev_priv), psr.work.work);
6012c9916cdSFrançois Tigeot 	struct intel_dp *intel_dp = dev_priv->psr.enabled;
6022c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = dp_to_dig_port(intel_dp)->base.base.crtc;
6032c9916cdSFrançois Tigeot 	enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
6042c9916cdSFrançois Tigeot 
6052c9916cdSFrançois Tigeot 	/* We have to make sure PSR is ready for re-enable
6062c9916cdSFrançois Tigeot 	 * otherwise it keeps disabled until next full enable/disable cycle.
6072c9916cdSFrançois Tigeot 	 * PSR might take some time to get fully disabled
6082c9916cdSFrançois Tigeot 	 * and be ready for re-enable.
6092c9916cdSFrançois Tigeot 	 */
6108621f407SFrançois Tigeot 	if (HAS_DDI(dev_priv)) {
6111487f786SFrançois Tigeot 		if (intel_wait_for_register(dev_priv,
6121487f786SFrançois Tigeot 					    EDP_PSR_STATUS_CTL,
6131487f786SFrançois Tigeot 					    EDP_PSR_STATUS_STATE_MASK,
6141487f786SFrançois Tigeot 					    0,
6151487f786SFrançois Tigeot 					    50)) {
6162c9916cdSFrançois Tigeot 			DRM_ERROR("Timed out waiting for PSR Idle for re-enable\n");
6172c9916cdSFrançois Tigeot 			return;
6182c9916cdSFrançois Tigeot 		}
6192c9916cdSFrançois Tigeot 	} else {
6201487f786SFrançois Tigeot 		if (intel_wait_for_register(dev_priv,
6211487f786SFrançois Tigeot 					    VLV_PSRSTAT(pipe),
6221487f786SFrançois Tigeot 					    VLV_EDP_PSR_IN_TRANS,
6231487f786SFrançois Tigeot 					    0,
6241487f786SFrançois Tigeot 					    1)) {
6252c9916cdSFrançois Tigeot 			DRM_ERROR("Timed out waiting for PSR Idle for re-enable\n");
6262c9916cdSFrançois Tigeot 			return;
6272c9916cdSFrançois Tigeot 		}
6282c9916cdSFrançois Tigeot 	}
6292c9916cdSFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
6302c9916cdSFrançois Tigeot 	intel_dp = dev_priv->psr.enabled;
6312c9916cdSFrançois Tigeot 
6322c9916cdSFrançois Tigeot 	if (!intel_dp)
6332c9916cdSFrançois Tigeot 		goto unlock;
6342c9916cdSFrançois Tigeot 
6352c9916cdSFrançois Tigeot 	/*
6362c9916cdSFrançois Tigeot 	 * The delayed work can race with an invalidate hence we need to
6372c9916cdSFrançois Tigeot 	 * recheck. Since psr_flush first clears this and then reschedules we
6382c9916cdSFrançois Tigeot 	 * won't ever miss a flush when bailing out here.
6392c9916cdSFrançois Tigeot 	 */
6402c9916cdSFrançois Tigeot 	if (dev_priv->psr.busy_frontbuffer_bits)
6412c9916cdSFrançois Tigeot 		goto unlock;
6422c9916cdSFrançois Tigeot 
6432c9916cdSFrançois Tigeot 	intel_psr_activate(intel_dp);
6442c9916cdSFrançois Tigeot unlock:
6452c9916cdSFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
6462c9916cdSFrançois Tigeot }
6472c9916cdSFrançois Tigeot 
6482c9916cdSFrançois Tigeot static void intel_psr_exit(struct drm_device *dev)
6492c9916cdSFrançois Tigeot {
650*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
6512c9916cdSFrançois Tigeot 	struct intel_dp *intel_dp = dev_priv->psr.enabled;
6522c9916cdSFrançois Tigeot 	struct drm_crtc *crtc = dp_to_dig_port(intel_dp)->base.base.crtc;
6532c9916cdSFrançois Tigeot 	enum i915_pipe pipe = to_intel_crtc(crtc)->pipe;
6542c9916cdSFrançois Tigeot 	u32 val;
6552c9916cdSFrançois Tigeot 
6562c9916cdSFrançois Tigeot 	if (!dev_priv->psr.active)
6572c9916cdSFrançois Tigeot 		return;
6582c9916cdSFrançois Tigeot 
6592c9916cdSFrançois Tigeot 	if (HAS_DDI(dev)) {
660aee94f86SFrançois Tigeot 		val = I915_READ(EDP_PSR_CTL);
6612c9916cdSFrançois Tigeot 
6622c9916cdSFrançois Tigeot 		WARN_ON(!(val & EDP_PSR_ENABLE));
6632c9916cdSFrançois Tigeot 
664aee94f86SFrançois Tigeot 		I915_WRITE(EDP_PSR_CTL, val & ~EDP_PSR_ENABLE);
6652c9916cdSFrançois Tigeot 	} else {
6662c9916cdSFrançois Tigeot 		val = I915_READ(VLV_PSRCTL(pipe));
6672c9916cdSFrançois Tigeot 
6682c9916cdSFrançois Tigeot 		/* Here we do the transition from PSR_state 3 to PSR_state 5
6692c9916cdSFrançois Tigeot 		 * directly once PSR State 4 that is active with single frame
6702c9916cdSFrançois Tigeot 		 * update can be skipped. PSR_state 5 that is PSR exit then
6712c9916cdSFrançois Tigeot 		 * Hardware is responsible to transition back to PSR_state 1
6722c9916cdSFrançois Tigeot 		 * that is PSR inactive. Same state after
6732c9916cdSFrançois Tigeot 		 * vlv_edp_psr_enable_source.
6742c9916cdSFrançois Tigeot 		 */
6752c9916cdSFrançois Tigeot 		val &= ~VLV_EDP_PSR_ACTIVE_ENTRY;
6762c9916cdSFrançois Tigeot 		I915_WRITE(VLV_PSRCTL(pipe), val);
6772c9916cdSFrançois Tigeot 
6782c9916cdSFrançois Tigeot 		/* Send AUX wake up - Spec says after transitioning to PSR
6792c9916cdSFrançois Tigeot 		 * active we have to send AUX wake up by writing 01h in DPCD
6802c9916cdSFrançois Tigeot 		 * 600h of sink device.
6812c9916cdSFrançois Tigeot 		 * XXX: This might slow down the transition, but without this
6822c9916cdSFrançois Tigeot 		 * HW doesn't complete the transition to PSR_state 1 and we
6832c9916cdSFrançois Tigeot 		 * never get the screen updated.
6842c9916cdSFrançois Tigeot 		 */
6852c9916cdSFrançois Tigeot 		drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER,
6862c9916cdSFrançois Tigeot 				   DP_SET_POWER_D0);
6872c9916cdSFrançois Tigeot 	}
6882c9916cdSFrançois Tigeot 
6892c9916cdSFrançois Tigeot 	dev_priv->psr.active = false;
6902c9916cdSFrançois Tigeot }
6912c9916cdSFrançois Tigeot 
6922c9916cdSFrançois Tigeot /**
69319c468b4SFrançois Tigeot  * intel_psr_single_frame_update - Single Frame Update
69419c468b4SFrançois Tigeot  * @dev: DRM device
695a05eeebfSFrançois Tigeot  * @frontbuffer_bits: frontbuffer plane tracking bits
69619c468b4SFrançois Tigeot  *
69719c468b4SFrançois Tigeot  * Some platforms support a single frame update feature that is used to
69819c468b4SFrançois Tigeot  * send and update only one frame on Remote Frame Buffer.
69919c468b4SFrançois Tigeot  * So far it is only implemented for Valleyview and Cherryview because
70019c468b4SFrançois Tigeot  * hardware requires this to be done before a page flip.
70119c468b4SFrançois Tigeot  */
702a05eeebfSFrançois Tigeot void intel_psr_single_frame_update(struct drm_device *dev,
703a05eeebfSFrançois Tigeot 				   unsigned frontbuffer_bits)
70419c468b4SFrançois Tigeot {
705*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
70619c468b4SFrançois Tigeot 	struct drm_crtc *crtc;
70719c468b4SFrançois Tigeot 	enum i915_pipe pipe;
70819c468b4SFrançois Tigeot 	u32 val;
70919c468b4SFrançois Tigeot 
71019c468b4SFrançois Tigeot 	/*
71119c468b4SFrançois Tigeot 	 * Single frame update is already supported on BDW+ but it requires
71219c468b4SFrançois Tigeot 	 * many W/A and it isn't really needed.
71319c468b4SFrançois Tigeot 	 */
714aee94f86SFrançois Tigeot 	if (!IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev))
71519c468b4SFrançois Tigeot 		return;
71619c468b4SFrançois Tigeot 
71719c468b4SFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
71819c468b4SFrançois Tigeot 	if (!dev_priv->psr.enabled) {
71919c468b4SFrançois Tigeot 		mutex_unlock(&dev_priv->psr.lock);
72019c468b4SFrançois Tigeot 		return;
72119c468b4SFrançois Tigeot 	}
72219c468b4SFrançois Tigeot 
72319c468b4SFrançois Tigeot 	crtc = dp_to_dig_port(dev_priv->psr.enabled)->base.base.crtc;
72419c468b4SFrançois Tigeot 	pipe = to_intel_crtc(crtc)->pipe;
725a05eeebfSFrançois Tigeot 
726a05eeebfSFrançois Tigeot 	if (frontbuffer_bits & INTEL_FRONTBUFFER_ALL_MASK(pipe)) {
72719c468b4SFrançois Tigeot 		val = I915_READ(VLV_PSRCTL(pipe));
72819c468b4SFrançois Tigeot 
72919c468b4SFrançois Tigeot 		/*
73019c468b4SFrançois Tigeot 		 * We need to set this bit before writing registers for a flip.
73119c468b4SFrançois Tigeot 		 * This bit will be self-clear when it gets to the PSR active state.
73219c468b4SFrançois Tigeot 		 */
73319c468b4SFrançois Tigeot 		I915_WRITE(VLV_PSRCTL(pipe), val | VLV_EDP_PSR_SINGLE_FRAME_UPDATE);
734a05eeebfSFrançois Tigeot 	}
73519c468b4SFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
73619c468b4SFrançois Tigeot }
73719c468b4SFrançois Tigeot 
73819c468b4SFrançois Tigeot /**
7392c9916cdSFrançois Tigeot  * intel_psr_invalidate - Invalidade PSR
7402c9916cdSFrançois Tigeot  * @dev: DRM device
7412c9916cdSFrançois Tigeot  * @frontbuffer_bits: frontbuffer plane tracking bits
7422c9916cdSFrançois Tigeot  *
7432c9916cdSFrançois Tigeot  * Since the hardware frontbuffer tracking has gaps we need to integrate
7442c9916cdSFrançois Tigeot  * with the software frontbuffer tracking. This function gets called every
7452c9916cdSFrançois Tigeot  * time frontbuffer rendering starts and a buffer gets dirtied. PSR must be
7462c9916cdSFrançois Tigeot  * disabled if the frontbuffer mask contains a buffer relevant to PSR.
7472c9916cdSFrançois Tigeot  *
7482c9916cdSFrançois Tigeot  * Dirty frontbuffers relevant to PSR are tracked in busy_frontbuffer_bits."
7492c9916cdSFrançois Tigeot  */
7502c9916cdSFrançois Tigeot void intel_psr_invalidate(struct drm_device *dev,
7512c9916cdSFrançois Tigeot 			  unsigned frontbuffer_bits)
7522c9916cdSFrançois Tigeot {
753*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
7542c9916cdSFrançois Tigeot 	struct drm_crtc *crtc;
7552c9916cdSFrançois Tigeot 	enum i915_pipe pipe;
7562c9916cdSFrançois Tigeot 
7572c9916cdSFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
7582c9916cdSFrançois Tigeot 	if (!dev_priv->psr.enabled) {
7592c9916cdSFrançois Tigeot 		mutex_unlock(&dev_priv->psr.lock);
7602c9916cdSFrançois Tigeot 		return;
7612c9916cdSFrançois Tigeot 	}
7622c9916cdSFrançois Tigeot 
7632c9916cdSFrançois Tigeot 	crtc = dp_to_dig_port(dev_priv->psr.enabled)->base.base.crtc;
7642c9916cdSFrançois Tigeot 	pipe = to_intel_crtc(crtc)->pipe;
7652c9916cdSFrançois Tigeot 
766a05eeebfSFrançois Tigeot 	frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
767a05eeebfSFrançois Tigeot 	dev_priv->psr.busy_frontbuffer_bits |= frontbuffer_bits;
768a05eeebfSFrançois Tigeot 
769a05eeebfSFrançois Tigeot 	if (frontbuffer_bits)
7702c9916cdSFrançois Tigeot 		intel_psr_exit(dev);
7712c9916cdSFrançois Tigeot 
7722c9916cdSFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
7732c9916cdSFrançois Tigeot }
7742c9916cdSFrançois Tigeot 
7752c9916cdSFrançois Tigeot /**
7762c9916cdSFrançois Tigeot  * intel_psr_flush - Flush PSR
7772c9916cdSFrançois Tigeot  * @dev: DRM device
7782c9916cdSFrançois Tigeot  * @frontbuffer_bits: frontbuffer plane tracking bits
779a05eeebfSFrançois Tigeot  * @origin: which operation caused the flush
7802c9916cdSFrançois Tigeot  *
7812c9916cdSFrançois Tigeot  * Since the hardware frontbuffer tracking has gaps we need to integrate
7822c9916cdSFrançois Tigeot  * with the software frontbuffer tracking. This function gets called every
7832c9916cdSFrançois Tigeot  * time frontbuffer rendering has completed and flushed out to memory. PSR
7842c9916cdSFrançois Tigeot  * can be enabled again if no other frontbuffer relevant to PSR is dirty.
7852c9916cdSFrançois Tigeot  *
7862c9916cdSFrançois Tigeot  * Dirty frontbuffers relevant to PSR are tracked in busy_frontbuffer_bits.
7872c9916cdSFrançois Tigeot  */
7882c9916cdSFrançois Tigeot void intel_psr_flush(struct drm_device *dev,
789a05eeebfSFrançois Tigeot 		     unsigned frontbuffer_bits, enum fb_op_origin origin)
7902c9916cdSFrançois Tigeot {
791*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
7922c9916cdSFrançois Tigeot 	struct drm_crtc *crtc;
7932c9916cdSFrançois Tigeot 	enum i915_pipe pipe;
7942c9916cdSFrançois Tigeot 
7952c9916cdSFrançois Tigeot 	mutex_lock(&dev_priv->psr.lock);
7962c9916cdSFrançois Tigeot 	if (!dev_priv->psr.enabled) {
7972c9916cdSFrançois Tigeot 		mutex_unlock(&dev_priv->psr.lock);
7982c9916cdSFrançois Tigeot 		return;
7992c9916cdSFrançois Tigeot 	}
8002c9916cdSFrançois Tigeot 
8012c9916cdSFrançois Tigeot 	crtc = dp_to_dig_port(dev_priv->psr.enabled)->base.base.crtc;
8022c9916cdSFrançois Tigeot 	pipe = to_intel_crtc(crtc)->pipe;
803a05eeebfSFrançois Tigeot 
804a05eeebfSFrançois Tigeot 	frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(pipe);
8052c9916cdSFrançois Tigeot 	dev_priv->psr.busy_frontbuffer_bits &= ~frontbuffer_bits;
8062c9916cdSFrançois Tigeot 
807aee94f86SFrançois Tigeot 	/* By definition flush = invalidate + flush */
808a05eeebfSFrançois Tigeot 	if (frontbuffer_bits)
8092c9916cdSFrançois Tigeot 		intel_psr_exit(dev);
8102c9916cdSFrançois Tigeot 
8112c9916cdSFrançois Tigeot 	if (!dev_priv->psr.active && !dev_priv->psr.busy_frontbuffer_bits)
812aee94f86SFrançois Tigeot 		if (!work_busy(&dev_priv->psr.work.work))
8132c9916cdSFrançois Tigeot 			schedule_delayed_work(&dev_priv->psr.work,
814aee94f86SFrançois Tigeot 					      msecs_to_jiffies(100));
8152c9916cdSFrançois Tigeot 	mutex_unlock(&dev_priv->psr.lock);
8162c9916cdSFrançois Tigeot }
8172c9916cdSFrançois Tigeot 
8182c9916cdSFrançois Tigeot /**
8192c9916cdSFrançois Tigeot  * intel_psr_init - Init basic PSR work and mutex.
8202c9916cdSFrançois Tigeot  * @dev: DRM device
8212c9916cdSFrançois Tigeot  *
8222c9916cdSFrançois Tigeot  * This function is  called only once at driver load to initialize basic
8232c9916cdSFrançois Tigeot  * PSR stuff.
8242c9916cdSFrançois Tigeot  */
8252c9916cdSFrançois Tigeot void intel_psr_init(struct drm_device *dev)
8262c9916cdSFrançois Tigeot {
827*bf017597SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
8282c9916cdSFrançois Tigeot 
829aee94f86SFrançois Tigeot 	dev_priv->psr_mmio_base = IS_HASWELL(dev_priv) ?
830aee94f86SFrançois Tigeot 		HSW_EDP_PSR_BASE : BDW_EDP_PSR_BASE;
831aee94f86SFrançois Tigeot 
832c0e85e96SFrançois Tigeot 	/* Per platform default */
833c0e85e96SFrançois Tigeot 	if (i915.enable_psr == -1) {
8348621f407SFrançois Tigeot 		if (IS_HASWELL(dev) || IS_BROADWELL(dev))
835c0e85e96SFrançois Tigeot 			i915.enable_psr = 1;
836c0e85e96SFrançois Tigeot 		else
837c0e85e96SFrançois Tigeot 			i915.enable_psr = 0;
838c0e85e96SFrançois Tigeot 	}
839c0e85e96SFrançois Tigeot 
840c0e85e96SFrançois Tigeot 	/* Set link_standby x link_off defaults */
841c0e85e96SFrançois Tigeot 	if (IS_HASWELL(dev) || IS_BROADWELL(dev))
842c0e85e96SFrançois Tigeot 		/* HSW and BDW require workarounds that we don't implement. */
843c0e85e96SFrançois Tigeot 		dev_priv->psr.link_standby = false;
844c0e85e96SFrançois Tigeot 	else if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
845c0e85e96SFrançois Tigeot 		/* On VLV and CHV only standby mode is supported. */
846c0e85e96SFrançois Tigeot 		dev_priv->psr.link_standby = true;
847c0e85e96SFrançois Tigeot 	else
848c0e85e96SFrançois Tigeot 		/* For new platforms let's respect VBT back again */
849c0e85e96SFrançois Tigeot 		dev_priv->psr.link_standby = dev_priv->vbt.psr.full_link;
850c0e85e96SFrançois Tigeot 
851c0e85e96SFrançois Tigeot 	/* Override link_standby x link_off defaults */
852c0e85e96SFrançois Tigeot 	if (i915.enable_psr == 2 && !dev_priv->psr.link_standby) {
853c0e85e96SFrançois Tigeot 		DRM_DEBUG_KMS("PSR: Forcing link standby\n");
854c0e85e96SFrançois Tigeot 		dev_priv->psr.link_standby = true;
855c0e85e96SFrançois Tigeot 	}
856c0e85e96SFrançois Tigeot 	if (i915.enable_psr == 3 && dev_priv->psr.link_standby) {
857c0e85e96SFrançois Tigeot 		DRM_DEBUG_KMS("PSR: Forcing main link off\n");
858c0e85e96SFrançois Tigeot 		dev_priv->psr.link_standby = false;
859c0e85e96SFrançois Tigeot 	}
860c0e85e96SFrançois Tigeot 
8612c9916cdSFrançois Tigeot 	INIT_DELAYED_WORK(&dev_priv->psr.work, intel_psr_work);
8622c9916cdSFrançois Tigeot 	lockinit(&dev_priv->psr.lock, "i915dpl", 0, LK_CANRECURSE);
8632c9916cdSFrançois Tigeot }
864