Home | History | Annotate | Line # | Download | only in display
      1  1.3  riastrad /*	$NetBSD: intel_psr.c,v 1.3 2021/12/19 11:49:11 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright  2014 Intel Corporation
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     14  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     15  1.1  riastrad  * Software.
     16  1.1  riastrad  *
     17  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  1.1  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  1.1  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  1.1  riastrad  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     23  1.1  riastrad  * DEALINGS IN THE SOFTWARE.
     24  1.1  riastrad  */
     25  1.1  riastrad 
     26  1.1  riastrad #include <sys/cdefs.h>
     27  1.3  riastrad __KERNEL_RCSID(0, "$NetBSD: intel_psr.c,v 1.3 2021/12/19 11:49:11 riastradh Exp $");
     28  1.1  riastrad 
     29  1.1  riastrad #include <drm/drm_atomic_helper.h>
     30  1.1  riastrad 
     31  1.1  riastrad #include "display/intel_dp.h"
     32  1.1  riastrad 
     33  1.1  riastrad #include "i915_drv.h"
     34  1.1  riastrad #include "intel_atomic.h"
     35  1.1  riastrad #include "intel_display_types.h"
     36  1.1  riastrad #include "intel_psr.h"
     37  1.1  riastrad #include "intel_sprite.h"
     38  1.1  riastrad 
     39  1.3  riastrad #include <linux/nbsd-namespace.h>
     40  1.3  riastrad 
     41  1.1  riastrad /**
     42  1.1  riastrad  * DOC: Panel Self Refresh (PSR/SRD)
     43  1.1  riastrad  *
     44  1.1  riastrad  * Since Haswell Display controller supports Panel Self-Refresh on display
     45  1.1  riastrad  * panels witch have a remote frame buffer (RFB) implemented according to PSR
     46  1.1  riastrad  * spec in eDP1.3. PSR feature allows the display to go to lower standby states
     47  1.1  riastrad  * when system is idle but display is on as it eliminates display refresh
     48  1.1  riastrad  * request to DDR memory completely as long as the frame buffer for that
     49  1.1  riastrad  * display is unchanged.
     50  1.1  riastrad  *
     51  1.1  riastrad  * Panel Self Refresh must be supported by both Hardware (source) and
     52  1.1  riastrad  * Panel (sink).
     53  1.1  riastrad  *
     54  1.1  riastrad  * PSR saves power by caching the framebuffer in the panel RFB, which allows us
     55  1.1  riastrad  * to power down the link and memory controller. For DSI panels the same idea
     56  1.1  riastrad  * is called "manual mode".
     57  1.1  riastrad  *
     58  1.1  riastrad  * The implementation uses the hardware-based PSR support which automatically
     59  1.1  riastrad  * enters/exits self-refresh mode. The hardware takes care of sending the
     60  1.1  riastrad  * required DP aux message and could even retrain the link (that part isn't
     61  1.1  riastrad  * enabled yet though). The hardware also keeps track of any frontbuffer
     62  1.1  riastrad  * changes to know when to exit self-refresh mode again. Unfortunately that
     63  1.1  riastrad  * part doesn't work too well, hence why the i915 PSR support uses the
     64  1.1  riastrad  * software frontbuffer tracking to make sure it doesn't miss a screen
     65  1.1  riastrad  * update. For this integration intel_psr_invalidate() and intel_psr_flush()
     66  1.1  riastrad  * get called by the frontbuffer tracking code. Note that because of locking
     67  1.1  riastrad  * issues the self-refresh re-enable code is done from a work queue, which
     68  1.1  riastrad  * must be correctly synchronized/cancelled when shutting down the pipe."
     69  1.1  riastrad  */
     70  1.1  riastrad 
     71  1.1  riastrad static bool psr_global_enabled(u32 debug)
     72  1.1  riastrad {
     73  1.1  riastrad 	switch (debug & I915_PSR_DEBUG_MODE_MASK) {
     74  1.1  riastrad 	case I915_PSR_DEBUG_DEFAULT:
     75  1.1  riastrad 		return i915_modparams.enable_psr;
     76  1.1  riastrad 	case I915_PSR_DEBUG_DISABLE:
     77  1.1  riastrad 		return false;
     78  1.1  riastrad 	default:
     79  1.1  riastrad 		return true;
     80  1.1  riastrad 	}
     81  1.1  riastrad }
     82  1.1  riastrad 
     83  1.1  riastrad static bool intel_psr2_enabled(struct drm_i915_private *dev_priv,
     84  1.1  riastrad 			       const struct intel_crtc_state *crtc_state)
     85  1.1  riastrad {
     86  1.1  riastrad 	/* Cannot enable DSC and PSR2 simultaneously */
     87  1.1  riastrad 	WARN_ON(crtc_state->dsc.compression_enable &&
     88  1.1  riastrad 		crtc_state->has_psr2);
     89  1.1  riastrad 
     90  1.1  riastrad 	switch (dev_priv->psr.debug & I915_PSR_DEBUG_MODE_MASK) {
     91  1.1  riastrad 	case I915_PSR_DEBUG_DISABLE:
     92  1.1  riastrad 	case I915_PSR_DEBUG_FORCE_PSR1:
     93  1.1  riastrad 		return false;
     94  1.1  riastrad 	default:
     95  1.1  riastrad 		return crtc_state->has_psr2;
     96  1.1  riastrad 	}
     97  1.1  riastrad }
     98  1.1  riastrad 
     99  1.1  riastrad static void psr_irq_control(struct drm_i915_private *dev_priv)
    100  1.1  riastrad {
    101  1.1  riastrad 	enum transcoder trans_shift;
    102  1.1  riastrad 	u32 mask, val;
    103  1.1  riastrad 	i915_reg_t imr_reg;
    104  1.1  riastrad 
    105  1.1  riastrad 	/*
    106  1.1  riastrad 	 * gen12+ has registers relative to transcoder and one per transcoder
    107  1.1  riastrad 	 * using the same bit definition: handle it as TRANSCODER_EDP to force
    108  1.1  riastrad 	 * 0 shift in bit definition
    109  1.1  riastrad 	 */
    110  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 12) {
    111  1.1  riastrad 		trans_shift = 0;
    112  1.1  riastrad 		imr_reg = TRANS_PSR_IMR(dev_priv->psr.transcoder);
    113  1.1  riastrad 	} else {
    114  1.1  riastrad 		trans_shift = dev_priv->psr.transcoder;
    115  1.1  riastrad 		imr_reg = EDP_PSR_IMR;
    116  1.1  riastrad 	}
    117  1.1  riastrad 
    118  1.1  riastrad 	mask = EDP_PSR_ERROR(trans_shift);
    119  1.1  riastrad 	if (dev_priv->psr.debug & I915_PSR_DEBUG_IRQ)
    120  1.1  riastrad 		mask |= EDP_PSR_POST_EXIT(trans_shift) |
    121  1.1  riastrad 			EDP_PSR_PRE_ENTRY(trans_shift);
    122  1.1  riastrad 
    123  1.1  riastrad 	/* Warning: it is masking/setting reserved bits too */
    124  1.1  riastrad 	val = I915_READ(imr_reg);
    125  1.1  riastrad 	val &= ~EDP_PSR_TRANS_MASK(trans_shift);
    126  1.1  riastrad 	val |= ~mask;
    127  1.1  riastrad 	I915_WRITE(imr_reg, val);
    128  1.1  riastrad }
    129  1.1  riastrad 
    130  1.1  riastrad static void psr_event_print(u32 val, bool psr2_enabled)
    131  1.1  riastrad {
    132  1.1  riastrad 	DRM_DEBUG_KMS("PSR exit events: 0x%x\n", val);
    133  1.1  riastrad 	if (val & PSR_EVENT_PSR2_WD_TIMER_EXPIRE)
    134  1.1  riastrad 		DRM_DEBUG_KMS("\tPSR2 watchdog timer expired\n");
    135  1.1  riastrad 	if ((val & PSR_EVENT_PSR2_DISABLED) && psr2_enabled)
    136  1.1  riastrad 		DRM_DEBUG_KMS("\tPSR2 disabled\n");
    137  1.1  riastrad 	if (val & PSR_EVENT_SU_DIRTY_FIFO_UNDERRUN)
    138  1.1  riastrad 		DRM_DEBUG_KMS("\tSU dirty FIFO underrun\n");
    139  1.1  riastrad 	if (val & PSR_EVENT_SU_CRC_FIFO_UNDERRUN)
    140  1.1  riastrad 		DRM_DEBUG_KMS("\tSU CRC FIFO underrun\n");
    141  1.1  riastrad 	if (val & PSR_EVENT_GRAPHICS_RESET)
    142  1.1  riastrad 		DRM_DEBUG_KMS("\tGraphics reset\n");
    143  1.1  riastrad 	if (val & PSR_EVENT_PCH_INTERRUPT)
    144  1.1  riastrad 		DRM_DEBUG_KMS("\tPCH interrupt\n");
    145  1.1  riastrad 	if (val & PSR_EVENT_MEMORY_UP)
    146  1.1  riastrad 		DRM_DEBUG_KMS("\tMemory up\n");
    147  1.1  riastrad 	if (val & PSR_EVENT_FRONT_BUFFER_MODIFY)
    148  1.1  riastrad 		DRM_DEBUG_KMS("\tFront buffer modification\n");
    149  1.1  riastrad 	if (val & PSR_EVENT_WD_TIMER_EXPIRE)
    150  1.1  riastrad 		DRM_DEBUG_KMS("\tPSR watchdog timer expired\n");
    151  1.1  riastrad 	if (val & PSR_EVENT_PIPE_REGISTERS_UPDATE)
    152  1.1  riastrad 		DRM_DEBUG_KMS("\tPIPE registers updated\n");
    153  1.1  riastrad 	if (val & PSR_EVENT_REGISTER_UPDATE)
    154  1.1  riastrad 		DRM_DEBUG_KMS("\tRegister updated\n");
    155  1.1  riastrad 	if (val & PSR_EVENT_HDCP_ENABLE)
    156  1.1  riastrad 		DRM_DEBUG_KMS("\tHDCP enabled\n");
    157  1.1  riastrad 	if (val & PSR_EVENT_KVMR_SESSION_ENABLE)
    158  1.1  riastrad 		DRM_DEBUG_KMS("\tKVMR session enabled\n");
    159  1.1  riastrad 	if (val & PSR_EVENT_VBI_ENABLE)
    160  1.1  riastrad 		DRM_DEBUG_KMS("\tVBI enabled\n");
    161  1.1  riastrad 	if (val & PSR_EVENT_LPSP_MODE_EXIT)
    162  1.1  riastrad 		DRM_DEBUG_KMS("\tLPSP mode exited\n");
    163  1.1  riastrad 	if ((val & PSR_EVENT_PSR_DISABLE) && !psr2_enabled)
    164  1.1  riastrad 		DRM_DEBUG_KMS("\tPSR disabled\n");
    165  1.1  riastrad }
    166  1.1  riastrad 
    167  1.1  riastrad void intel_psr_irq_handler(struct drm_i915_private *dev_priv, u32 psr_iir)
    168  1.1  riastrad {
    169  1.1  riastrad 	enum transcoder cpu_transcoder = dev_priv->psr.transcoder;
    170  1.1  riastrad 	enum transcoder trans_shift;
    171  1.1  riastrad 	i915_reg_t imr_reg;
    172  1.1  riastrad 	ktime_t time_ns =  ktime_get();
    173  1.1  riastrad 
    174  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 12) {
    175  1.1  riastrad 		trans_shift = 0;
    176  1.1  riastrad 		imr_reg = TRANS_PSR_IMR(dev_priv->psr.transcoder);
    177  1.1  riastrad 	} else {
    178  1.1  riastrad 		trans_shift = dev_priv->psr.transcoder;
    179  1.1  riastrad 		imr_reg = EDP_PSR_IMR;
    180  1.1  riastrad 	}
    181  1.1  riastrad 
    182  1.1  riastrad 	if (psr_iir & EDP_PSR_PRE_ENTRY(trans_shift)) {
    183  1.1  riastrad 		dev_priv->psr.last_entry_attempt = time_ns;
    184  1.1  riastrad 		DRM_DEBUG_KMS("[transcoder %s] PSR entry attempt in 2 vblanks\n",
    185  1.1  riastrad 			      transcoder_name(cpu_transcoder));
    186  1.1  riastrad 	}
    187  1.1  riastrad 
    188  1.1  riastrad 	if (psr_iir & EDP_PSR_POST_EXIT(trans_shift)) {
    189  1.1  riastrad 		dev_priv->psr.last_exit = time_ns;
    190  1.1  riastrad 		DRM_DEBUG_KMS("[transcoder %s] PSR exit completed\n",
    191  1.1  riastrad 			      transcoder_name(cpu_transcoder));
    192  1.1  riastrad 
    193  1.1  riastrad 		if (INTEL_GEN(dev_priv) >= 9) {
    194  1.1  riastrad 			u32 val = I915_READ(PSR_EVENT(cpu_transcoder));
    195  1.1  riastrad 			bool psr2_enabled = dev_priv->psr.psr2_enabled;
    196  1.1  riastrad 
    197  1.1  riastrad 			I915_WRITE(PSR_EVENT(cpu_transcoder), val);
    198  1.1  riastrad 			psr_event_print(val, psr2_enabled);
    199  1.1  riastrad 		}
    200  1.1  riastrad 	}
    201  1.1  riastrad 
    202  1.1  riastrad 	if (psr_iir & EDP_PSR_ERROR(trans_shift)) {
    203  1.1  riastrad 		u32 val;
    204  1.1  riastrad 
    205  1.1  riastrad 		DRM_WARN("[transcoder %s] PSR aux error\n",
    206  1.1  riastrad 			 transcoder_name(cpu_transcoder));
    207  1.1  riastrad 
    208  1.1  riastrad 		dev_priv->psr.irq_aux_error = true;
    209  1.1  riastrad 
    210  1.1  riastrad 		/*
    211  1.1  riastrad 		 * If this interruption is not masked it will keep
    212  1.1  riastrad 		 * interrupting so fast that it prevents the scheduled
    213  1.1  riastrad 		 * work to run.
    214  1.1  riastrad 		 * Also after a PSR error, we don't want to arm PSR
    215  1.1  riastrad 		 * again so we don't care about unmask the interruption
    216  1.1  riastrad 		 * or unset irq_aux_error.
    217  1.1  riastrad 		 */
    218  1.1  riastrad 		val = I915_READ(imr_reg);
    219  1.1  riastrad 		val |= EDP_PSR_ERROR(trans_shift);
    220  1.1  riastrad 		I915_WRITE(imr_reg, val);
    221  1.1  riastrad 
    222  1.1  riastrad 		schedule_work(&dev_priv->psr.work);
    223  1.1  riastrad 	}
    224  1.1  riastrad }
    225  1.1  riastrad 
    226  1.1  riastrad static bool intel_dp_get_alpm_status(struct intel_dp *intel_dp)
    227  1.1  riastrad {
    228  1.1  riastrad 	u8 alpm_caps = 0;
    229  1.1  riastrad 
    230  1.1  riastrad 	if (drm_dp_dpcd_readb(&intel_dp->aux, DP_RECEIVER_ALPM_CAP,
    231  1.1  riastrad 			      &alpm_caps) != 1)
    232  1.1  riastrad 		return false;
    233  1.1  riastrad 	return alpm_caps & DP_ALPM_CAP;
    234  1.1  riastrad }
    235  1.1  riastrad 
    236  1.1  riastrad static u8 intel_dp_get_sink_sync_latency(struct intel_dp *intel_dp)
    237  1.1  riastrad {
    238  1.1  riastrad 	u8 val = 8; /* assume the worst if we can't read the value */
    239  1.1  riastrad 
    240  1.1  riastrad 	if (drm_dp_dpcd_readb(&intel_dp->aux,
    241  1.1  riastrad 			      DP_SYNCHRONIZATION_LATENCY_IN_SINK, &val) == 1)
    242  1.1  riastrad 		val &= DP_MAX_RESYNC_FRAME_COUNT_MASK;
    243  1.1  riastrad 	else
    244  1.1  riastrad 		DRM_DEBUG_KMS("Unable to get sink synchronization latency, assuming 8 frames\n");
    245  1.1  riastrad 	return val;
    246  1.1  riastrad }
    247  1.1  riastrad 
    248  1.1  riastrad static u16 intel_dp_get_su_x_granulartiy(struct intel_dp *intel_dp)
    249  1.1  riastrad {
    250  1.1  riastrad 	u16 val;
    251  1.1  riastrad 	ssize_t r;
    252  1.1  riastrad 
    253  1.1  riastrad 	/*
    254  1.1  riastrad 	 * Returning the default X granularity if granularity not required or
    255  1.1  riastrad 	 * if DPCD read fails
    256  1.1  riastrad 	 */
    257  1.1  riastrad 	if (!(intel_dp->psr_dpcd[1] & DP_PSR2_SU_GRANULARITY_REQUIRED))
    258  1.1  riastrad 		return 4;
    259  1.1  riastrad 
    260  1.1  riastrad 	r = drm_dp_dpcd_read(&intel_dp->aux, DP_PSR2_SU_X_GRANULARITY, &val, 2);
    261  1.1  riastrad 	if (r != 2)
    262  1.1  riastrad 		DRM_DEBUG_KMS("Unable to read DP_PSR2_SU_X_GRANULARITY\n");
    263  1.1  riastrad 
    264  1.1  riastrad 	/*
    265  1.1  riastrad 	 * Spec says that if the value read is 0 the default granularity should
    266  1.1  riastrad 	 * be used instead.
    267  1.1  riastrad 	 */
    268  1.1  riastrad 	if (r != 2 || val == 0)
    269  1.1  riastrad 		val = 4;
    270  1.1  riastrad 
    271  1.1  riastrad 	return val;
    272  1.1  riastrad }
    273  1.1  riastrad 
    274  1.1  riastrad void intel_psr_init_dpcd(struct intel_dp *intel_dp)
    275  1.1  riastrad {
    276  1.1  riastrad 	struct drm_i915_private *dev_priv =
    277  1.1  riastrad 		to_i915(dp_to_dig_port(intel_dp)->base.base.dev);
    278  1.1  riastrad 
    279  1.1  riastrad 	if (dev_priv->psr.dp) {
    280  1.1  riastrad 		DRM_WARN("More than one eDP panel found, PSR support should be extended\n");
    281  1.1  riastrad 		return;
    282  1.1  riastrad 	}
    283  1.1  riastrad 
    284  1.1  riastrad 	drm_dp_dpcd_read(&intel_dp->aux, DP_PSR_SUPPORT, intel_dp->psr_dpcd,
    285  1.1  riastrad 			 sizeof(intel_dp->psr_dpcd));
    286  1.1  riastrad 
    287  1.1  riastrad 	if (!intel_dp->psr_dpcd[0])
    288  1.1  riastrad 		return;
    289  1.1  riastrad 	DRM_DEBUG_KMS("eDP panel supports PSR version %x\n",
    290  1.1  riastrad 		      intel_dp->psr_dpcd[0]);
    291  1.1  riastrad 
    292  1.1  riastrad 	if (drm_dp_has_quirk(&intel_dp->desc, DP_DPCD_QUIRK_NO_PSR)) {
    293  1.1  riastrad 		DRM_DEBUG_KMS("PSR support not currently available for this panel\n");
    294  1.1  riastrad 		return;
    295  1.1  riastrad 	}
    296  1.1  riastrad 
    297  1.1  riastrad 	if (!(intel_dp->edp_dpcd[1] & DP_EDP_SET_POWER_CAP)) {
    298  1.1  riastrad 		DRM_DEBUG_KMS("Panel lacks power state control, PSR cannot be enabled\n");
    299  1.1  riastrad 		return;
    300  1.1  riastrad 	}
    301  1.1  riastrad 
    302  1.1  riastrad 	dev_priv->psr.sink_support = true;
    303  1.1  riastrad 	dev_priv->psr.sink_sync_latency =
    304  1.1  riastrad 		intel_dp_get_sink_sync_latency(intel_dp);
    305  1.1  riastrad 
    306  1.1  riastrad 	dev_priv->psr.dp = intel_dp;
    307  1.1  riastrad 
    308  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 9 &&
    309  1.1  riastrad 	    (intel_dp->psr_dpcd[0] == DP_PSR2_WITH_Y_COORD_IS_SUPPORTED)) {
    310  1.1  riastrad 		bool y_req = intel_dp->psr_dpcd[1] &
    311  1.1  riastrad 			     DP_PSR2_SU_Y_COORDINATE_REQUIRED;
    312  1.1  riastrad 		bool alpm = intel_dp_get_alpm_status(intel_dp);
    313  1.1  riastrad 
    314  1.1  riastrad 		/*
    315  1.1  riastrad 		 * All panels that supports PSR version 03h (PSR2 +
    316  1.1  riastrad 		 * Y-coordinate) can handle Y-coordinates in VSC but we are
    317  1.1  riastrad 		 * only sure that it is going to be used when required by the
    318  1.1  riastrad 		 * panel. This way panel is capable to do selective update
    319  1.1  riastrad 		 * without a aux frame sync.
    320  1.1  riastrad 		 *
    321  1.1  riastrad 		 * To support PSR version 02h and PSR version 03h without
    322  1.1  riastrad 		 * Y-coordinate requirement panels we would need to enable
    323  1.1  riastrad 		 * GTC first.
    324  1.1  riastrad 		 */
    325  1.1  riastrad 		dev_priv->psr.sink_psr2_support = y_req && alpm;
    326  1.1  riastrad 		DRM_DEBUG_KMS("PSR2 %ssupported\n",
    327  1.1  riastrad 			      dev_priv->psr.sink_psr2_support ? "" : "not ");
    328  1.1  riastrad 
    329  1.1  riastrad 		if (dev_priv->psr.sink_psr2_support) {
    330  1.1  riastrad 			dev_priv->psr.colorimetry_support =
    331  1.1  riastrad 				intel_dp_get_colorimetry_status(intel_dp);
    332  1.1  riastrad 			dev_priv->psr.su_x_granularity =
    333  1.1  riastrad 				intel_dp_get_su_x_granulartiy(intel_dp);
    334  1.1  riastrad 		}
    335  1.1  riastrad 	}
    336  1.1  riastrad }
    337  1.1  riastrad 
    338  1.1  riastrad static void intel_psr_setup_vsc(struct intel_dp *intel_dp,
    339  1.1  riastrad 				const struct intel_crtc_state *crtc_state)
    340  1.1  riastrad {
    341  1.1  riastrad 	struct intel_digital_port *intel_dig_port = dp_to_dig_port(intel_dp);
    342  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
    343  1.1  riastrad 	struct dp_sdp psr_vsc;
    344  1.1  riastrad 
    345  1.1  riastrad 	if (dev_priv->psr.psr2_enabled) {
    346  1.1  riastrad 		/* Prepare VSC Header for SU as per EDP 1.4 spec, Table 6.11 */
    347  1.1  riastrad 		memset(&psr_vsc, 0, sizeof(psr_vsc));
    348  1.1  riastrad 		psr_vsc.sdp_header.HB0 = 0;
    349  1.1  riastrad 		psr_vsc.sdp_header.HB1 = 0x7;
    350  1.1  riastrad 		if (dev_priv->psr.colorimetry_support) {
    351  1.1  riastrad 			psr_vsc.sdp_header.HB2 = 0x5;
    352  1.1  riastrad 			psr_vsc.sdp_header.HB3 = 0x13;
    353  1.1  riastrad 		} else {
    354  1.1  riastrad 			psr_vsc.sdp_header.HB2 = 0x4;
    355  1.1  riastrad 			psr_vsc.sdp_header.HB3 = 0xe;
    356  1.1  riastrad 		}
    357  1.1  riastrad 	} else {
    358  1.1  riastrad 		/* Prepare VSC packet as per EDP 1.3 spec, Table 3.10 */
    359  1.1  riastrad 		memset(&psr_vsc, 0, sizeof(psr_vsc));
    360  1.1  riastrad 		psr_vsc.sdp_header.HB0 = 0;
    361  1.1  riastrad 		psr_vsc.sdp_header.HB1 = 0x7;
    362  1.1  riastrad 		psr_vsc.sdp_header.HB2 = 0x2;
    363  1.1  riastrad 		psr_vsc.sdp_header.HB3 = 0x8;
    364  1.1  riastrad 	}
    365  1.1  riastrad 
    366  1.1  riastrad 	intel_dig_port->write_infoframe(&intel_dig_port->base,
    367  1.1  riastrad 					crtc_state,
    368  1.1  riastrad 					DP_SDP_VSC, &psr_vsc, sizeof(psr_vsc));
    369  1.1  riastrad }
    370  1.1  riastrad 
    371  1.1  riastrad static void hsw_psr_setup_aux(struct intel_dp *intel_dp)
    372  1.1  riastrad {
    373  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
    374  1.1  riastrad 	u32 aux_clock_divider, aux_ctl;
    375  1.1  riastrad 	int i;
    376  1.1  riastrad 	static const u8 aux_msg[] = {
    377  1.1  riastrad 		[0] = DP_AUX_NATIVE_WRITE << 4,
    378  1.1  riastrad 		[1] = DP_SET_POWER >> 8,
    379  1.1  riastrad 		[2] = DP_SET_POWER & 0xff,
    380  1.1  riastrad 		[3] = 1 - 1,
    381  1.1  riastrad 		[4] = DP_SET_POWER_D0,
    382  1.1  riastrad 	};
    383  1.1  riastrad 	u32 psr_aux_mask = EDP_PSR_AUX_CTL_TIME_OUT_MASK |
    384  1.1  riastrad 			   EDP_PSR_AUX_CTL_MESSAGE_SIZE_MASK |
    385  1.1  riastrad 			   EDP_PSR_AUX_CTL_PRECHARGE_2US_MASK |
    386  1.1  riastrad 			   EDP_PSR_AUX_CTL_BIT_CLOCK_2X_MASK;
    387  1.1  riastrad 
    388  1.1  riastrad 	BUILD_BUG_ON(sizeof(aux_msg) > 20);
    389  1.1  riastrad 	for (i = 0; i < sizeof(aux_msg); i += 4)
    390  1.1  riastrad 		I915_WRITE(EDP_PSR_AUX_DATA(dev_priv->psr.transcoder, i >> 2),
    391  1.1  riastrad 			   intel_dp_pack_aux(&aux_msg[i], sizeof(aux_msg) - i));
    392  1.1  riastrad 
    393  1.1  riastrad 	aux_clock_divider = intel_dp->get_aux_clock_divider(intel_dp, 0);
    394  1.1  riastrad 
    395  1.1  riastrad 	/* Start with bits set for DDI_AUX_CTL register */
    396  1.1  riastrad 	aux_ctl = intel_dp->get_aux_send_ctl(intel_dp, sizeof(aux_msg),
    397  1.1  riastrad 					     aux_clock_divider);
    398  1.1  riastrad 
    399  1.1  riastrad 	/* Select only valid bits for SRD_AUX_CTL */
    400  1.1  riastrad 	aux_ctl &= psr_aux_mask;
    401  1.1  riastrad 	I915_WRITE(EDP_PSR_AUX_CTL(dev_priv->psr.transcoder), aux_ctl);
    402  1.1  riastrad }
    403  1.1  riastrad 
    404  1.1  riastrad static void intel_psr_enable_sink(struct intel_dp *intel_dp)
    405  1.1  riastrad {
    406  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
    407  1.1  riastrad 	u8 dpcd_val = DP_PSR_ENABLE;
    408  1.1  riastrad 
    409  1.1  riastrad 	/* Enable ALPM at sink for psr2 */
    410  1.1  riastrad 	if (dev_priv->psr.psr2_enabled) {
    411  1.1  riastrad 		drm_dp_dpcd_writeb(&intel_dp->aux, DP_RECEIVER_ALPM_CONFIG,
    412  1.1  riastrad 				   DP_ALPM_ENABLE |
    413  1.1  riastrad 				   DP_ALPM_LOCK_ERROR_IRQ_HPD_ENABLE);
    414  1.1  riastrad 
    415  1.1  riastrad 		dpcd_val |= DP_PSR_ENABLE_PSR2 | DP_PSR_IRQ_HPD_WITH_CRC_ERRORS;
    416  1.1  riastrad 	} else {
    417  1.1  riastrad 		if (dev_priv->psr.link_standby)
    418  1.1  riastrad 			dpcd_val |= DP_PSR_MAIN_LINK_ACTIVE;
    419  1.1  riastrad 
    420  1.1  riastrad 		if (INTEL_GEN(dev_priv) >= 8)
    421  1.1  riastrad 			dpcd_val |= DP_PSR_CRC_VERIFICATION;
    422  1.1  riastrad 	}
    423  1.1  riastrad 
    424  1.1  riastrad 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG, dpcd_val);
    425  1.1  riastrad 
    426  1.1  riastrad 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, DP_SET_POWER_D0);
    427  1.1  riastrad }
    428  1.1  riastrad 
    429  1.1  riastrad static u32 intel_psr1_get_tp_time(struct intel_dp *intel_dp)
    430  1.1  riastrad {
    431  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
    432  1.1  riastrad 	u32 val = 0;
    433  1.1  riastrad 
    434  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 11)
    435  1.1  riastrad 		val |= EDP_PSR_TP4_TIME_0US;
    436  1.1  riastrad 
    437  1.1  riastrad 	if (dev_priv->vbt.psr.tp1_wakeup_time_us == 0)
    438  1.1  riastrad 		val |= EDP_PSR_TP1_TIME_0us;
    439  1.1  riastrad 	else if (dev_priv->vbt.psr.tp1_wakeup_time_us <= 100)
    440  1.1  riastrad 		val |= EDP_PSR_TP1_TIME_100us;
    441  1.1  riastrad 	else if (dev_priv->vbt.psr.tp1_wakeup_time_us <= 500)
    442  1.1  riastrad 		val |= EDP_PSR_TP1_TIME_500us;
    443  1.1  riastrad 	else
    444  1.1  riastrad 		val |= EDP_PSR_TP1_TIME_2500us;
    445  1.1  riastrad 
    446  1.1  riastrad 	if (dev_priv->vbt.psr.tp2_tp3_wakeup_time_us == 0)
    447  1.1  riastrad 		val |= EDP_PSR_TP2_TP3_TIME_0us;
    448  1.1  riastrad 	else if (dev_priv->vbt.psr.tp2_tp3_wakeup_time_us <= 100)
    449  1.1  riastrad 		val |= EDP_PSR_TP2_TP3_TIME_100us;
    450  1.1  riastrad 	else if (dev_priv->vbt.psr.tp2_tp3_wakeup_time_us <= 500)
    451  1.1  riastrad 		val |= EDP_PSR_TP2_TP3_TIME_500us;
    452  1.1  riastrad 	else
    453  1.1  riastrad 		val |= EDP_PSR_TP2_TP3_TIME_2500us;
    454  1.1  riastrad 
    455  1.1  riastrad 	if (intel_dp_source_supports_hbr2(intel_dp) &&
    456  1.1  riastrad 	    drm_dp_tps3_supported(intel_dp->dpcd))
    457  1.1  riastrad 		val |= EDP_PSR_TP1_TP3_SEL;
    458  1.1  riastrad 	else
    459  1.1  riastrad 		val |= EDP_PSR_TP1_TP2_SEL;
    460  1.1  riastrad 
    461  1.1  riastrad 	return val;
    462  1.1  riastrad }
    463  1.1  riastrad 
    464  1.1  riastrad static void hsw_activate_psr1(struct intel_dp *intel_dp)
    465  1.1  riastrad {
    466  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
    467  1.1  riastrad 	u32 max_sleep_time = 0x1f;
    468  1.1  riastrad 	u32 val = EDP_PSR_ENABLE;
    469  1.1  riastrad 
    470  1.1  riastrad 	/* Let's use 6 as the minimum to cover all known cases including the
    471  1.1  riastrad 	 * off-by-one issue that HW has in some cases.
    472  1.1  riastrad 	 */
    473  1.1  riastrad 	int idle_frames = max(6, dev_priv->vbt.psr.idle_frames);
    474  1.1  riastrad 
    475  1.1  riastrad 	/* sink_sync_latency of 8 means source has to wait for more than 8
    476  1.1  riastrad 	 * frames, we'll go with 9 frames for now
    477  1.1  riastrad 	 */
    478  1.1  riastrad 	idle_frames = max(idle_frames, dev_priv->psr.sink_sync_latency + 1);
    479  1.1  riastrad 	val |= idle_frames << EDP_PSR_IDLE_FRAME_SHIFT;
    480  1.1  riastrad 
    481  1.1  riastrad 	val |= max_sleep_time << EDP_PSR_MAX_SLEEP_TIME_SHIFT;
    482  1.1  riastrad 	if (IS_HASWELL(dev_priv))
    483  1.1  riastrad 		val |= EDP_PSR_MIN_LINK_ENTRY_TIME_8_LINES;
    484  1.1  riastrad 
    485  1.1  riastrad 	if (dev_priv->psr.link_standby)
    486  1.1  riastrad 		val |= EDP_PSR_LINK_STANDBY;
    487  1.1  riastrad 
    488  1.1  riastrad 	val |= intel_psr1_get_tp_time(intel_dp);
    489  1.1  riastrad 
    490  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 8)
    491  1.1  riastrad 		val |= EDP_PSR_CRC_ENABLE;
    492  1.1  riastrad 
    493  1.1  riastrad 	val |= (I915_READ(EDP_PSR_CTL(dev_priv->psr.transcoder)) &
    494  1.1  riastrad 		EDP_PSR_RESTORE_PSR_ACTIVE_CTX_MASK);
    495  1.1  riastrad 	I915_WRITE(EDP_PSR_CTL(dev_priv->psr.transcoder), val);
    496  1.1  riastrad }
    497  1.1  riastrad 
    498  1.1  riastrad static void hsw_activate_psr2(struct intel_dp *intel_dp)
    499  1.1  riastrad {
    500  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
    501  1.1  riastrad 	u32 val;
    502  1.1  riastrad 
    503  1.1  riastrad 	/* Let's use 6 as the minimum to cover all known cases including the
    504  1.1  riastrad 	 * off-by-one issue that HW has in some cases.
    505  1.1  riastrad 	 */
    506  1.1  riastrad 	int idle_frames = max(6, dev_priv->vbt.psr.idle_frames);
    507  1.1  riastrad 
    508  1.1  riastrad 	idle_frames = max(idle_frames, dev_priv->psr.sink_sync_latency + 1);
    509  1.1  riastrad 	val = idle_frames << EDP_PSR2_IDLE_FRAME_SHIFT;
    510  1.1  riastrad 
    511  1.1  riastrad 	val |= EDP_PSR2_ENABLE | EDP_SU_TRACK_ENABLE;
    512  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
    513  1.1  riastrad 		val |= EDP_Y_COORDINATE_ENABLE;
    514  1.1  riastrad 
    515  1.1  riastrad 	val |= EDP_PSR2_FRAME_BEFORE_SU(dev_priv->psr.sink_sync_latency + 1);
    516  1.1  riastrad 
    517  1.1  riastrad 	if (dev_priv->vbt.psr.psr2_tp2_tp3_wakeup_time_us >= 0 &&
    518  1.1  riastrad 	    dev_priv->vbt.psr.psr2_tp2_tp3_wakeup_time_us <= 50)
    519  1.1  riastrad 		val |= EDP_PSR2_TP2_TIME_50us;
    520  1.1  riastrad 	else if (dev_priv->vbt.psr.psr2_tp2_tp3_wakeup_time_us <= 100)
    521  1.1  riastrad 		val |= EDP_PSR2_TP2_TIME_100us;
    522  1.1  riastrad 	else if (dev_priv->vbt.psr.psr2_tp2_tp3_wakeup_time_us <= 500)
    523  1.1  riastrad 		val |= EDP_PSR2_TP2_TIME_500us;
    524  1.1  riastrad 	else
    525  1.1  riastrad 		val |= EDP_PSR2_TP2_TIME_2500us;
    526  1.1  riastrad 
    527  1.1  riastrad 	/*
    528  1.1  riastrad 	 * PSR2 HW is incorrectly using EDP_PSR_TP1_TP3_SEL and BSpec is
    529  1.1  riastrad 	 * recommending keep this bit unset while PSR2 is enabled.
    530  1.1  riastrad 	 */
    531  1.1  riastrad 	I915_WRITE(EDP_PSR_CTL(dev_priv->psr.transcoder), 0);
    532  1.1  riastrad 
    533  1.1  riastrad 	I915_WRITE(EDP_PSR2_CTL(dev_priv->psr.transcoder), val);
    534  1.1  riastrad }
    535  1.1  riastrad 
    536  1.1  riastrad static bool
    537  1.1  riastrad transcoder_has_psr2(struct drm_i915_private *dev_priv, enum transcoder trans)
    538  1.1  riastrad {
    539  1.1  riastrad 	if (INTEL_GEN(dev_priv) < 9)
    540  1.1  riastrad 		return false;
    541  1.1  riastrad 	else if (INTEL_GEN(dev_priv) >= 12)
    542  1.1  riastrad 		return trans == TRANSCODER_A;
    543  1.1  riastrad 	else
    544  1.1  riastrad 		return trans == TRANSCODER_EDP;
    545  1.1  riastrad }
    546  1.1  riastrad 
    547  1.1  riastrad static u32 intel_get_frame_time_us(const struct intel_crtc_state *cstate)
    548  1.1  riastrad {
    549  1.1  riastrad 	if (!cstate || !cstate->hw.active)
    550  1.1  riastrad 		return 0;
    551  1.1  riastrad 
    552  1.1  riastrad 	return DIV_ROUND_UP(1000 * 1000,
    553  1.1  riastrad 			    drm_mode_vrefresh(&cstate->hw.adjusted_mode));
    554  1.1  riastrad }
    555  1.1  riastrad 
    556  1.1  riastrad static void psr2_program_idle_frames(struct drm_i915_private *dev_priv,
    557  1.1  riastrad 				     u32 idle_frames)
    558  1.1  riastrad {
    559  1.1  riastrad 	u32 val;
    560  1.1  riastrad 
    561  1.1  riastrad 	idle_frames <<=  EDP_PSR2_IDLE_FRAME_SHIFT;
    562  1.1  riastrad 	val = I915_READ(EDP_PSR2_CTL(dev_priv->psr.transcoder));
    563  1.1  riastrad 	val &= ~EDP_PSR2_IDLE_FRAME_MASK;
    564  1.1  riastrad 	val |= idle_frames;
    565  1.1  riastrad 	I915_WRITE(EDP_PSR2_CTL(dev_priv->psr.transcoder), val);
    566  1.1  riastrad }
    567  1.1  riastrad 
    568  1.1  riastrad static void tgl_psr2_enable_dc3co(struct drm_i915_private *dev_priv)
    569  1.1  riastrad {
    570  1.1  riastrad 	psr2_program_idle_frames(dev_priv, 0);
    571  1.1  riastrad 	intel_display_power_set_target_dc_state(dev_priv, DC_STATE_EN_DC3CO);
    572  1.1  riastrad }
    573  1.1  riastrad 
    574  1.1  riastrad static void tgl_psr2_disable_dc3co(struct drm_i915_private *dev_priv)
    575  1.1  riastrad {
    576  1.1  riastrad 	int idle_frames;
    577  1.1  riastrad 
    578  1.1  riastrad 	intel_display_power_set_target_dc_state(dev_priv, DC_STATE_EN_UPTO_DC6);
    579  1.1  riastrad 	/*
    580  1.1  riastrad 	 * Restore PSR2 idle frame let's use 6 as the minimum to cover all known
    581  1.1  riastrad 	 * cases including the off-by-one issue that HW has in some cases.
    582  1.1  riastrad 	 */
    583  1.1  riastrad 	idle_frames = max(6, dev_priv->vbt.psr.idle_frames);
    584  1.1  riastrad 	idle_frames = max(idle_frames, dev_priv->psr.sink_sync_latency + 1);
    585  1.1  riastrad 	psr2_program_idle_frames(dev_priv, idle_frames);
    586  1.1  riastrad }
    587  1.1  riastrad 
    588  1.1  riastrad static void tgl_dc5_idle_thread(struct work_struct *work)
    589  1.1  riastrad {
    590  1.1  riastrad 	struct drm_i915_private *dev_priv =
    591  1.1  riastrad 		container_of(work, typeof(*dev_priv), psr.idle_work.work);
    592  1.1  riastrad 
    593  1.1  riastrad 	mutex_lock(&dev_priv->psr.lock);
    594  1.1  riastrad 	/* If delayed work is pending, it is not idle */
    595  1.1  riastrad 	if (delayed_work_pending(&dev_priv->psr.idle_work))
    596  1.1  riastrad 		goto unlock;
    597  1.1  riastrad 
    598  1.1  riastrad 	DRM_DEBUG_KMS("DC5/6 idle thread\n");
    599  1.1  riastrad 	tgl_psr2_disable_dc3co(dev_priv);
    600  1.1  riastrad unlock:
    601  1.1  riastrad 	mutex_unlock(&dev_priv->psr.lock);
    602  1.1  riastrad }
    603  1.1  riastrad 
    604  1.1  riastrad static void tgl_disallow_dc3co_on_psr2_exit(struct drm_i915_private *dev_priv)
    605  1.1  riastrad {
    606  1.1  riastrad 	if (!dev_priv->psr.dc3co_enabled)
    607  1.1  riastrad 		return;
    608  1.1  riastrad 
    609  1.1  riastrad 	cancel_delayed_work(&dev_priv->psr.idle_work);
    610  1.1  riastrad 	/* Before PSR2 exit disallow dc3co*/
    611  1.1  riastrad 	tgl_psr2_disable_dc3co(dev_priv);
    612  1.1  riastrad }
    613  1.1  riastrad 
    614  1.1  riastrad static bool intel_psr2_config_valid(struct intel_dp *intel_dp,
    615  1.1  riastrad 				    struct intel_crtc_state *crtc_state)
    616  1.1  riastrad {
    617  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
    618  1.1  riastrad 	int crtc_hdisplay = crtc_state->hw.adjusted_mode.crtc_hdisplay;
    619  1.1  riastrad 	int crtc_vdisplay = crtc_state->hw.adjusted_mode.crtc_vdisplay;
    620  1.1  riastrad 	int psr_max_h = 0, psr_max_v = 0, max_bpp = 0;
    621  1.1  riastrad 
    622  1.1  riastrad 	if (!dev_priv->psr.sink_psr2_support)
    623  1.1  riastrad 		return false;
    624  1.1  riastrad 
    625  1.1  riastrad 	if (!transcoder_has_psr2(dev_priv, crtc_state->cpu_transcoder)) {
    626  1.1  riastrad 		DRM_DEBUG_KMS("PSR2 not supported in transcoder %s\n",
    627  1.1  riastrad 			      transcoder_name(crtc_state->cpu_transcoder));
    628  1.1  riastrad 		return false;
    629  1.1  riastrad 	}
    630  1.1  riastrad 
    631  1.1  riastrad 	/*
    632  1.1  riastrad 	 * DSC and PSR2 cannot be enabled simultaneously. If a requested
    633  1.1  riastrad 	 * resolution requires DSC to be enabled, priority is given to DSC
    634  1.1  riastrad 	 * over PSR2.
    635  1.1  riastrad 	 */
    636  1.1  riastrad 	if (crtc_state->dsc.compression_enable) {
    637  1.1  riastrad 		DRM_DEBUG_KMS("PSR2 cannot be enabled since DSC is enabled\n");
    638  1.1  riastrad 		return false;
    639  1.1  riastrad 	}
    640  1.1  riastrad 
    641  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 12) {
    642  1.1  riastrad 		psr_max_h = 5120;
    643  1.1  riastrad 		psr_max_v = 3200;
    644  1.1  riastrad 		max_bpp = 30;
    645  1.1  riastrad 	} else if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
    646  1.1  riastrad 		psr_max_h = 4096;
    647  1.1  riastrad 		psr_max_v = 2304;
    648  1.1  riastrad 		max_bpp = 24;
    649  1.1  riastrad 	} else if (IS_GEN(dev_priv, 9)) {
    650  1.1  riastrad 		psr_max_h = 3640;
    651  1.1  riastrad 		psr_max_v = 2304;
    652  1.1  riastrad 		max_bpp = 24;
    653  1.1  riastrad 	}
    654  1.1  riastrad 
    655  1.1  riastrad 	if (crtc_hdisplay > psr_max_h || crtc_vdisplay > psr_max_v) {
    656  1.1  riastrad 		DRM_DEBUG_KMS("PSR2 not enabled, resolution %dx%d > max supported %dx%d\n",
    657  1.1  riastrad 			      crtc_hdisplay, crtc_vdisplay,
    658  1.1  riastrad 			      psr_max_h, psr_max_v);
    659  1.1  riastrad 		return false;
    660  1.1  riastrad 	}
    661  1.1  riastrad 
    662  1.1  riastrad 	if (crtc_state->pipe_bpp > max_bpp) {
    663  1.1  riastrad 		DRM_DEBUG_KMS("PSR2 not enabled, pipe bpp %d > max supported %d\n",
    664  1.1  riastrad 			      crtc_state->pipe_bpp, max_bpp);
    665  1.1  riastrad 		return false;
    666  1.1  riastrad 	}
    667  1.1  riastrad 
    668  1.1  riastrad 	/*
    669  1.1  riastrad 	 * HW sends SU blocks of size four scan lines, which means the starting
    670  1.1  riastrad 	 * X coordinate and Y granularity requirements will always be met. We
    671  1.1  riastrad 	 * only need to validate the SU block width is a multiple of
    672  1.1  riastrad 	 * x granularity.
    673  1.1  riastrad 	 */
    674  1.1  riastrad 	if (crtc_hdisplay % dev_priv->psr.su_x_granularity) {
    675  1.1  riastrad 		DRM_DEBUG_KMS("PSR2 not enabled, hdisplay(%d) not multiple of %d\n",
    676  1.1  riastrad 			      crtc_hdisplay, dev_priv->psr.su_x_granularity);
    677  1.1  riastrad 		return false;
    678  1.1  riastrad 	}
    679  1.1  riastrad 
    680  1.1  riastrad 	if (crtc_state->crc_enabled) {
    681  1.1  riastrad 		DRM_DEBUG_KMS("PSR2 not enabled because it would inhibit pipe CRC calculation\n");
    682  1.1  riastrad 		return false;
    683  1.1  riastrad 	}
    684  1.1  riastrad 
    685  1.1  riastrad 	return true;
    686  1.1  riastrad }
    687  1.1  riastrad 
    688  1.1  riastrad void intel_psr_compute_config(struct intel_dp *intel_dp,
    689  1.1  riastrad 			      struct intel_crtc_state *crtc_state)
    690  1.1  riastrad {
    691  1.1  riastrad 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
    692  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
    693  1.1  riastrad 	const struct drm_display_mode *adjusted_mode =
    694  1.1  riastrad 		&crtc_state->hw.adjusted_mode;
    695  1.1  riastrad 	int psr_setup_time;
    696  1.1  riastrad 
    697  1.1  riastrad 	if (!CAN_PSR(dev_priv))
    698  1.1  riastrad 		return;
    699  1.1  riastrad 
    700  1.1  riastrad 	if (intel_dp != dev_priv->psr.dp)
    701  1.1  riastrad 		return;
    702  1.1  riastrad 
    703  1.1  riastrad 	/*
    704  1.1  riastrad 	 * HSW spec explicitly says PSR is tied to port A.
    705  1.1  riastrad 	 * BDW+ platforms have a instance of PSR registers per transcoder but
    706  1.1  riastrad 	 * for now it only supports one instance of PSR, so lets keep it
    707  1.1  riastrad 	 * hardcoded to PORT_A
    708  1.1  riastrad 	 */
    709  1.1  riastrad 	if (dig_port->base.port != PORT_A) {
    710  1.1  riastrad 		DRM_DEBUG_KMS("PSR condition failed: Port not supported\n");
    711  1.1  riastrad 		return;
    712  1.1  riastrad 	}
    713  1.1  riastrad 
    714  1.1  riastrad 	if (dev_priv->psr.sink_not_reliable) {
    715  1.1  riastrad 		DRM_DEBUG_KMS("PSR sink implementation is not reliable\n");
    716  1.1  riastrad 		return;
    717  1.1  riastrad 	}
    718  1.1  riastrad 
    719  1.1  riastrad 	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
    720  1.1  riastrad 		DRM_DEBUG_KMS("PSR condition failed: Interlaced mode enabled\n");
    721  1.1  riastrad 		return;
    722  1.1  riastrad 	}
    723  1.1  riastrad 
    724  1.1  riastrad 	psr_setup_time = drm_dp_psr_setup_time(intel_dp->psr_dpcd);
    725  1.1  riastrad 	if (psr_setup_time < 0) {
    726  1.1  riastrad 		DRM_DEBUG_KMS("PSR condition failed: Invalid PSR setup time (0x%02x)\n",
    727  1.1  riastrad 			      intel_dp->psr_dpcd[1]);
    728  1.1  riastrad 		return;
    729  1.1  riastrad 	}
    730  1.1  riastrad 
    731  1.1  riastrad 	if (intel_usecs_to_scanlines(adjusted_mode, psr_setup_time) >
    732  1.1  riastrad 	    adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vdisplay - 1) {
    733  1.1  riastrad 		DRM_DEBUG_KMS("PSR condition failed: PSR setup time (%d us) too long\n",
    734  1.1  riastrad 			      psr_setup_time);
    735  1.1  riastrad 		return;
    736  1.1  riastrad 	}
    737  1.1  riastrad 
    738  1.1  riastrad 	crtc_state->has_psr = true;
    739  1.1  riastrad 	crtc_state->has_psr2 = intel_psr2_config_valid(intel_dp, crtc_state);
    740  1.1  riastrad }
    741  1.1  riastrad 
    742  1.1  riastrad static void intel_psr_activate(struct intel_dp *intel_dp)
    743  1.1  riastrad {
    744  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
    745  1.1  riastrad 
    746  1.1  riastrad 	if (transcoder_has_psr2(dev_priv, dev_priv->psr.transcoder))
    747  1.1  riastrad 		WARN_ON(I915_READ(EDP_PSR2_CTL(dev_priv->psr.transcoder)) & EDP_PSR2_ENABLE);
    748  1.1  riastrad 
    749  1.1  riastrad 	WARN_ON(I915_READ(EDP_PSR_CTL(dev_priv->psr.transcoder)) & EDP_PSR_ENABLE);
    750  1.1  riastrad 	WARN_ON(dev_priv->psr.active);
    751  1.1  riastrad 	lockdep_assert_held(&dev_priv->psr.lock);
    752  1.1  riastrad 
    753  1.1  riastrad 	/* psr1 and psr2 are mutually exclusive.*/
    754  1.1  riastrad 	if (dev_priv->psr.psr2_enabled)
    755  1.1  riastrad 		hsw_activate_psr2(intel_dp);
    756  1.1  riastrad 	else
    757  1.1  riastrad 		hsw_activate_psr1(intel_dp);
    758  1.1  riastrad 
    759  1.1  riastrad 	dev_priv->psr.active = true;
    760  1.1  riastrad }
    761  1.1  riastrad 
    762  1.1  riastrad static void intel_psr_enable_source(struct intel_dp *intel_dp,
    763  1.1  riastrad 				    const struct intel_crtc_state *crtc_state)
    764  1.1  riastrad {
    765  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
    766  1.1  riastrad 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
    767  1.1  riastrad 	u32 mask;
    768  1.1  riastrad 
    769  1.1  riastrad 	/* Only HSW and BDW have PSR AUX registers that need to be setup. SKL+
    770  1.1  riastrad 	 * use hardcoded values PSR AUX transactions
    771  1.1  riastrad 	 */
    772  1.1  riastrad 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
    773  1.1  riastrad 		hsw_psr_setup_aux(intel_dp);
    774  1.1  riastrad 
    775  1.1  riastrad 	if (dev_priv->psr.psr2_enabled && (IS_GEN(dev_priv, 9) &&
    776  1.1  riastrad 					   !IS_GEMINILAKE(dev_priv))) {
    777  1.1  riastrad 		i915_reg_t reg = CHICKEN_TRANS(cpu_transcoder);
    778  1.1  riastrad 		u32 chicken = I915_READ(reg);
    779  1.1  riastrad 
    780  1.1  riastrad 		chicken |= PSR2_VSC_ENABLE_PROG_HEADER |
    781  1.1  riastrad 			   PSR2_ADD_VERTICAL_LINE_COUNT;
    782  1.1  riastrad 		I915_WRITE(reg, chicken);
    783  1.1  riastrad 	}
    784  1.1  riastrad 
    785  1.1  riastrad 	/*
    786  1.1  riastrad 	 * Per Spec: Avoid continuous PSR exit by masking MEMUP and HPD also
    787  1.1  riastrad 	 * mask LPSP to avoid dependency on other drivers that might block
    788  1.1  riastrad 	 * runtime_pm besides preventing  other hw tracking issues now we
    789  1.1  riastrad 	 * can rely on frontbuffer tracking.
    790  1.1  riastrad 	 */
    791  1.1  riastrad 	mask = EDP_PSR_DEBUG_MASK_MEMUP |
    792  1.1  riastrad 	       EDP_PSR_DEBUG_MASK_HPD |
    793  1.1  riastrad 	       EDP_PSR_DEBUG_MASK_LPSP |
    794  1.1  riastrad 	       EDP_PSR_DEBUG_MASK_MAX_SLEEP;
    795  1.1  riastrad 
    796  1.1  riastrad 	if (INTEL_GEN(dev_priv) < 11)
    797  1.1  riastrad 		mask |= EDP_PSR_DEBUG_MASK_DISP_REG_WRITE;
    798  1.1  riastrad 
    799  1.1  riastrad 	I915_WRITE(EDP_PSR_DEBUG(dev_priv->psr.transcoder), mask);
    800  1.1  riastrad 
    801  1.1  riastrad 	psr_irq_control(dev_priv);
    802  1.1  riastrad }
    803  1.1  riastrad 
    804  1.1  riastrad static void intel_psr_enable_locked(struct drm_i915_private *dev_priv,
    805  1.1  riastrad 				    const struct intel_crtc_state *crtc_state)
    806  1.1  riastrad {
    807  1.1  riastrad 	struct intel_dp *intel_dp = dev_priv->psr.dp;
    808  1.1  riastrad 	u32 val;
    809  1.1  riastrad 
    810  1.1  riastrad 	WARN_ON(dev_priv->psr.enabled);
    811  1.1  riastrad 
    812  1.1  riastrad 	dev_priv->psr.psr2_enabled = intel_psr2_enabled(dev_priv, crtc_state);
    813  1.1  riastrad 	dev_priv->psr.busy_frontbuffer_bits = 0;
    814  1.1  riastrad 	dev_priv->psr.pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
    815  1.1  riastrad 	dev_priv->psr.dc3co_enabled = !!crtc_state->dc3co_exitline;
    816  1.1  riastrad 	dev_priv->psr.dc3co_exit_delay = intel_get_frame_time_us(crtc_state);
    817  1.1  riastrad 	dev_priv->psr.transcoder = crtc_state->cpu_transcoder;
    818  1.1  riastrad 
    819  1.1  riastrad 	/*
    820  1.1  riastrad 	 * If a PSR error happened and the driver is reloaded, the EDP_PSR_IIR
    821  1.1  riastrad 	 * will still keep the error set even after the reset done in the
    822  1.1  riastrad 	 * irq_preinstall and irq_uninstall hooks.
    823  1.1  riastrad 	 * And enabling in this situation cause the screen to freeze in the
    824  1.1  riastrad 	 * first time that PSR HW tries to activate so lets keep PSR disabled
    825  1.1  riastrad 	 * to avoid any rendering problems.
    826  1.1  riastrad 	 */
    827  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 12) {
    828  1.1  riastrad 		val = I915_READ(TRANS_PSR_IIR(dev_priv->psr.transcoder));
    829  1.1  riastrad 		val &= EDP_PSR_ERROR(0);
    830  1.1  riastrad 	} else {
    831  1.1  riastrad 		val = I915_READ(EDP_PSR_IIR);
    832  1.1  riastrad 		val &= EDP_PSR_ERROR(dev_priv->psr.transcoder);
    833  1.1  riastrad 	}
    834  1.1  riastrad 	if (val) {
    835  1.1  riastrad 		dev_priv->psr.sink_not_reliable = true;
    836  1.1  riastrad 		DRM_DEBUG_KMS("PSR interruption error set, not enabling PSR\n");
    837  1.1  riastrad 		return;
    838  1.1  riastrad 	}
    839  1.1  riastrad 
    840  1.1  riastrad 	DRM_DEBUG_KMS("Enabling PSR%s\n",
    841  1.1  riastrad 		      dev_priv->psr.psr2_enabled ? "2" : "1");
    842  1.1  riastrad 	intel_psr_setup_vsc(intel_dp, crtc_state);
    843  1.1  riastrad 	intel_psr_enable_sink(intel_dp);
    844  1.1  riastrad 	intel_psr_enable_source(intel_dp, crtc_state);
    845  1.1  riastrad 	dev_priv->psr.enabled = true;
    846  1.1  riastrad 
    847  1.1  riastrad 	intel_psr_activate(intel_dp);
    848  1.1  riastrad }
    849  1.1  riastrad 
    850  1.1  riastrad /**
    851  1.1  riastrad  * intel_psr_enable - Enable PSR
    852  1.1  riastrad  * @intel_dp: Intel DP
    853  1.1  riastrad  * @crtc_state: new CRTC state
    854  1.1  riastrad  *
    855  1.1  riastrad  * This function can only be called after the pipe is fully trained and enabled.
    856  1.1  riastrad  */
    857  1.1  riastrad void intel_psr_enable(struct intel_dp *intel_dp,
    858  1.1  riastrad 		      const struct intel_crtc_state *crtc_state)
    859  1.1  riastrad {
    860  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
    861  1.1  riastrad 
    862  1.1  riastrad 	if (!crtc_state->has_psr)
    863  1.1  riastrad 		return;
    864  1.1  riastrad 
    865  1.1  riastrad 	if (WARN_ON(!CAN_PSR(dev_priv)))
    866  1.1  riastrad 		return;
    867  1.1  riastrad 
    868  1.1  riastrad 	WARN_ON(dev_priv->drrs.dp);
    869  1.1  riastrad 
    870  1.1  riastrad 	mutex_lock(&dev_priv->psr.lock);
    871  1.1  riastrad 
    872  1.1  riastrad 	if (!psr_global_enabled(dev_priv->psr.debug)) {
    873  1.1  riastrad 		DRM_DEBUG_KMS("PSR disabled by flag\n");
    874  1.1  riastrad 		goto unlock;
    875  1.1  riastrad 	}
    876  1.1  riastrad 
    877  1.1  riastrad 	intel_psr_enable_locked(dev_priv, crtc_state);
    878  1.1  riastrad 
    879  1.1  riastrad unlock:
    880  1.1  riastrad 	mutex_unlock(&dev_priv->psr.lock);
    881  1.1  riastrad }
    882  1.1  riastrad 
    883  1.1  riastrad static void intel_psr_exit(struct drm_i915_private *dev_priv)
    884  1.1  riastrad {
    885  1.1  riastrad 	u32 val;
    886  1.1  riastrad 
    887  1.1  riastrad 	if (!dev_priv->psr.active) {
    888  1.1  riastrad 		if (transcoder_has_psr2(dev_priv, dev_priv->psr.transcoder)) {
    889  1.1  riastrad 			val = I915_READ(EDP_PSR2_CTL(dev_priv->psr.transcoder));
    890  1.1  riastrad 			WARN_ON(val & EDP_PSR2_ENABLE);
    891  1.1  riastrad 		}
    892  1.1  riastrad 
    893  1.1  riastrad 		val = I915_READ(EDP_PSR_CTL(dev_priv->psr.transcoder));
    894  1.1  riastrad 		WARN_ON(val & EDP_PSR_ENABLE);
    895  1.1  riastrad 
    896  1.1  riastrad 		return;
    897  1.1  riastrad 	}
    898  1.1  riastrad 
    899  1.1  riastrad 	if (dev_priv->psr.psr2_enabled) {
    900  1.1  riastrad 		tgl_disallow_dc3co_on_psr2_exit(dev_priv);
    901  1.1  riastrad 		val = I915_READ(EDP_PSR2_CTL(dev_priv->psr.transcoder));
    902  1.1  riastrad 		WARN_ON(!(val & EDP_PSR2_ENABLE));
    903  1.1  riastrad 		val &= ~EDP_PSR2_ENABLE;
    904  1.1  riastrad 		I915_WRITE(EDP_PSR2_CTL(dev_priv->psr.transcoder), val);
    905  1.1  riastrad 	} else {
    906  1.1  riastrad 		val = I915_READ(EDP_PSR_CTL(dev_priv->psr.transcoder));
    907  1.1  riastrad 		WARN_ON(!(val & EDP_PSR_ENABLE));
    908  1.1  riastrad 		val &= ~EDP_PSR_ENABLE;
    909  1.1  riastrad 		I915_WRITE(EDP_PSR_CTL(dev_priv->psr.transcoder), val);
    910  1.1  riastrad 	}
    911  1.1  riastrad 	dev_priv->psr.active = false;
    912  1.1  riastrad }
    913  1.1  riastrad 
    914  1.1  riastrad static void intel_psr_disable_locked(struct intel_dp *intel_dp)
    915  1.1  riastrad {
    916  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
    917  1.1  riastrad 	i915_reg_t psr_status;
    918  1.1  riastrad 	u32 psr_status_mask;
    919  1.1  riastrad 
    920  1.1  riastrad 	lockdep_assert_held(&dev_priv->psr.lock);
    921  1.1  riastrad 
    922  1.1  riastrad 	if (!dev_priv->psr.enabled)
    923  1.1  riastrad 		return;
    924  1.1  riastrad 
    925  1.1  riastrad 	DRM_DEBUG_KMS("Disabling PSR%s\n",
    926  1.1  riastrad 		      dev_priv->psr.psr2_enabled ? "2" : "1");
    927  1.1  riastrad 
    928  1.1  riastrad 	intel_psr_exit(dev_priv);
    929  1.1  riastrad 
    930  1.1  riastrad 	if (dev_priv->psr.psr2_enabled) {
    931  1.1  riastrad 		psr_status = EDP_PSR2_STATUS(dev_priv->psr.transcoder);
    932  1.1  riastrad 		psr_status_mask = EDP_PSR2_STATUS_STATE_MASK;
    933  1.1  riastrad 	} else {
    934  1.1  riastrad 		psr_status = EDP_PSR_STATUS(dev_priv->psr.transcoder);
    935  1.1  riastrad 		psr_status_mask = EDP_PSR_STATUS_STATE_MASK;
    936  1.1  riastrad 	}
    937  1.1  riastrad 
    938  1.1  riastrad 	/* Wait till PSR is idle */
    939  1.1  riastrad 	if (intel_de_wait_for_clear(dev_priv, psr_status,
    940  1.1  riastrad 				    psr_status_mask, 2000))
    941  1.1  riastrad 		DRM_ERROR("Timed out waiting PSR idle state\n");
    942  1.1  riastrad 
    943  1.1  riastrad 	/* Disable PSR on Sink */
    944  1.1  riastrad 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG, 0);
    945  1.1  riastrad 
    946  1.1  riastrad 	if (dev_priv->psr.psr2_enabled)
    947  1.1  riastrad 		drm_dp_dpcd_writeb(&intel_dp->aux, DP_RECEIVER_ALPM_CONFIG, 0);
    948  1.1  riastrad 
    949  1.1  riastrad 	dev_priv->psr.enabled = false;
    950  1.1  riastrad }
    951  1.1  riastrad 
    952  1.1  riastrad /**
    953  1.1  riastrad  * intel_psr_disable - Disable PSR
    954  1.1  riastrad  * @intel_dp: Intel DP
    955  1.1  riastrad  * @old_crtc_state: old CRTC state
    956  1.1  riastrad  *
    957  1.1  riastrad  * This function needs to be called before disabling pipe.
    958  1.1  riastrad  */
    959  1.1  riastrad void intel_psr_disable(struct intel_dp *intel_dp,
    960  1.1  riastrad 		       const struct intel_crtc_state *old_crtc_state)
    961  1.1  riastrad {
    962  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
    963  1.1  riastrad 
    964  1.1  riastrad 	if (!old_crtc_state->has_psr)
    965  1.1  riastrad 		return;
    966  1.1  riastrad 
    967  1.1  riastrad 	if (WARN_ON(!CAN_PSR(dev_priv)))
    968  1.1  riastrad 		return;
    969  1.1  riastrad 
    970  1.1  riastrad 	mutex_lock(&dev_priv->psr.lock);
    971  1.1  riastrad 
    972  1.1  riastrad 	intel_psr_disable_locked(intel_dp);
    973  1.1  riastrad 
    974  1.1  riastrad 	mutex_unlock(&dev_priv->psr.lock);
    975  1.1  riastrad 	cancel_work_sync(&dev_priv->psr.work);
    976  1.1  riastrad 	cancel_delayed_work_sync(&dev_priv->psr.idle_work);
    977  1.1  riastrad }
    978  1.1  riastrad 
    979  1.1  riastrad static void psr_force_hw_tracking_exit(struct drm_i915_private *dev_priv)
    980  1.1  riastrad {
    981  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 9)
    982  1.1  riastrad 		/*
    983  1.1  riastrad 		 * Display WA #0884: skl+
    984  1.1  riastrad 		 * This documented WA for bxt can be safely applied
    985  1.1  riastrad 		 * broadly so we can force HW tracking to exit PSR
    986  1.1  riastrad 		 * instead of disabling and re-enabling.
    987  1.1  riastrad 		 * Workaround tells us to write 0 to CUR_SURFLIVE_A,
    988  1.1  riastrad 		 * but it makes more sense write to the current active
    989  1.1  riastrad 		 * pipe.
    990  1.1  riastrad 		 */
    991  1.1  riastrad 		I915_WRITE(CURSURFLIVE(dev_priv->psr.pipe), 0);
    992  1.1  riastrad 	else
    993  1.1  riastrad 		/*
    994  1.1  riastrad 		 * A write to CURSURFLIVE do not cause HW tracking to exit PSR
    995  1.1  riastrad 		 * on older gens so doing the manual exit instead.
    996  1.1  riastrad 		 */
    997  1.1  riastrad 		intel_psr_exit(dev_priv);
    998  1.1  riastrad }
    999  1.1  riastrad 
   1000  1.1  riastrad /**
   1001  1.1  riastrad  * intel_psr_update - Update PSR state
   1002  1.1  riastrad  * @intel_dp: Intel DP
   1003  1.1  riastrad  * @crtc_state: new CRTC state
   1004  1.1  riastrad  *
   1005  1.1  riastrad  * This functions will update PSR states, disabling, enabling or switching PSR
   1006  1.1  riastrad  * version when executing fastsets. For full modeset, intel_psr_disable() and
   1007  1.1  riastrad  * intel_psr_enable() should be called instead.
   1008  1.1  riastrad  */
   1009  1.1  riastrad void intel_psr_update(struct intel_dp *intel_dp,
   1010  1.1  riastrad 		      const struct intel_crtc_state *crtc_state)
   1011  1.1  riastrad {
   1012  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
   1013  1.1  riastrad 	struct i915_psr *psr = &dev_priv->psr;
   1014  1.1  riastrad 	bool enable, psr2_enable;
   1015  1.1  riastrad 
   1016  1.1  riastrad 	if (!CAN_PSR(dev_priv) || READ_ONCE(psr->dp) != intel_dp)
   1017  1.1  riastrad 		return;
   1018  1.1  riastrad 
   1019  1.1  riastrad 	mutex_lock(&dev_priv->psr.lock);
   1020  1.1  riastrad 
   1021  1.1  riastrad 	enable = crtc_state->has_psr && psr_global_enabled(psr->debug);
   1022  1.1  riastrad 	psr2_enable = intel_psr2_enabled(dev_priv, crtc_state);
   1023  1.1  riastrad 
   1024  1.1  riastrad 	if (enable == psr->enabled && psr2_enable == psr->psr2_enabled) {
   1025  1.1  riastrad 		/* Force a PSR exit when enabling CRC to avoid CRC timeouts */
   1026  1.1  riastrad 		if (crtc_state->crc_enabled && psr->enabled)
   1027  1.1  riastrad 			psr_force_hw_tracking_exit(dev_priv);
   1028  1.1  riastrad 		else if (INTEL_GEN(dev_priv) < 9 && psr->enabled) {
   1029  1.1  riastrad 			/*
   1030  1.1  riastrad 			 * Activate PSR again after a force exit when enabling
   1031  1.1  riastrad 			 * CRC in older gens
   1032  1.1  riastrad 			 */
   1033  1.1  riastrad 			if (!dev_priv->psr.active &&
   1034  1.1  riastrad 			    !dev_priv->psr.busy_frontbuffer_bits)
   1035  1.1  riastrad 				schedule_work(&dev_priv->psr.work);
   1036  1.1  riastrad 		}
   1037  1.1  riastrad 
   1038  1.1  riastrad 		goto unlock;
   1039  1.1  riastrad 	}
   1040  1.1  riastrad 
   1041  1.1  riastrad 	if (psr->enabled)
   1042  1.1  riastrad 		intel_psr_disable_locked(intel_dp);
   1043  1.1  riastrad 
   1044  1.1  riastrad 	if (enable)
   1045  1.1  riastrad 		intel_psr_enable_locked(dev_priv, crtc_state);
   1046  1.1  riastrad 
   1047  1.1  riastrad unlock:
   1048  1.1  riastrad 	mutex_unlock(&dev_priv->psr.lock);
   1049  1.1  riastrad }
   1050  1.1  riastrad 
   1051  1.1  riastrad /**
   1052  1.1  riastrad  * intel_psr_wait_for_idle - wait for PSR1 to idle
   1053  1.1  riastrad  * @new_crtc_state: new CRTC state
   1054  1.1  riastrad  * @out_value: PSR status in case of failure
   1055  1.1  riastrad  *
   1056  1.1  riastrad  * This function is expected to be called from pipe_update_start() where it is
   1057  1.1  riastrad  * not expected to race with PSR enable or disable.
   1058  1.1  riastrad  *
   1059  1.1  riastrad  * Returns: 0 on success or -ETIMEOUT if PSR status does not idle.
   1060  1.1  riastrad  */
   1061  1.1  riastrad int intel_psr_wait_for_idle(const struct intel_crtc_state *new_crtc_state,
   1062  1.1  riastrad 			    u32 *out_value)
   1063  1.1  riastrad {
   1064  1.1  riastrad 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
   1065  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   1066  1.1  riastrad 
   1067  1.1  riastrad 	if (!dev_priv->psr.enabled || !new_crtc_state->has_psr)
   1068  1.1  riastrad 		return 0;
   1069  1.1  riastrad 
   1070  1.1  riastrad 	/* FIXME: Update this for PSR2 if we need to wait for idle */
   1071  1.1  riastrad 	if (READ_ONCE(dev_priv->psr.psr2_enabled))
   1072  1.1  riastrad 		return 0;
   1073  1.1  riastrad 
   1074  1.1  riastrad 	/*
   1075  1.1  riastrad 	 * From bspec: Panel Self Refresh (BDW+)
   1076  1.1  riastrad 	 * Max. time for PSR to idle = Inverse of the refresh rate + 6 ms of
   1077  1.1  riastrad 	 * exit training time + 1.5 ms of aux channel handshake. 50 ms is
   1078  1.1  riastrad 	 * defensive enough to cover everything.
   1079  1.1  riastrad 	 */
   1080  1.1  riastrad 
   1081  1.1  riastrad 	return __intel_wait_for_register(&dev_priv->uncore,
   1082  1.1  riastrad 					 EDP_PSR_STATUS(dev_priv->psr.transcoder),
   1083  1.1  riastrad 					 EDP_PSR_STATUS_STATE_MASK,
   1084  1.1  riastrad 					 EDP_PSR_STATUS_STATE_IDLE, 2, 50,
   1085  1.1  riastrad 					 out_value);
   1086  1.1  riastrad }
   1087  1.1  riastrad 
   1088  1.1  riastrad static bool __psr_wait_for_idle_locked(struct drm_i915_private *dev_priv)
   1089  1.1  riastrad {
   1090  1.1  riastrad 	i915_reg_t reg;
   1091  1.1  riastrad 	u32 mask;
   1092  1.1  riastrad 	int err;
   1093  1.1  riastrad 
   1094  1.1  riastrad 	if (!dev_priv->psr.enabled)
   1095  1.1  riastrad 		return false;
   1096  1.1  riastrad 
   1097  1.1  riastrad 	if (dev_priv->psr.psr2_enabled) {
   1098  1.1  riastrad 		reg = EDP_PSR2_STATUS(dev_priv->psr.transcoder);
   1099  1.1  riastrad 		mask = EDP_PSR2_STATUS_STATE_MASK;
   1100  1.1  riastrad 	} else {
   1101  1.1  riastrad 		reg = EDP_PSR_STATUS(dev_priv->psr.transcoder);
   1102  1.1  riastrad 		mask = EDP_PSR_STATUS_STATE_MASK;
   1103  1.1  riastrad 	}
   1104  1.1  riastrad 
   1105  1.1  riastrad 	mutex_unlock(&dev_priv->psr.lock);
   1106  1.1  riastrad 
   1107  1.1  riastrad 	err = intel_de_wait_for_clear(dev_priv, reg, mask, 50);
   1108  1.1  riastrad 	if (err)
   1109  1.1  riastrad 		DRM_ERROR("Timed out waiting for PSR Idle for re-enable\n");
   1110  1.1  riastrad 
   1111  1.1  riastrad 	/* After the unlocked wait, verify that PSR is still wanted! */
   1112  1.1  riastrad 	mutex_lock(&dev_priv->psr.lock);
   1113  1.1  riastrad 	return err == 0 && dev_priv->psr.enabled;
   1114  1.1  riastrad }
   1115  1.1  riastrad 
   1116  1.1  riastrad static int intel_psr_fastset_force(struct drm_i915_private *dev_priv)
   1117  1.1  riastrad {
   1118  1.1  riastrad 	struct drm_device *dev = &dev_priv->drm;
   1119  1.1  riastrad 	struct drm_modeset_acquire_ctx ctx;
   1120  1.1  riastrad 	struct drm_atomic_state *state;
   1121  1.1  riastrad 	struct intel_crtc *crtc;
   1122  1.1  riastrad 	int err;
   1123  1.1  riastrad 
   1124  1.1  riastrad 	state = drm_atomic_state_alloc(dev);
   1125  1.1  riastrad 	if (!state)
   1126  1.1  riastrad 		return -ENOMEM;
   1127  1.1  riastrad 
   1128  1.1  riastrad 	drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
   1129  1.1  riastrad 	state->acquire_ctx = &ctx;
   1130  1.1  riastrad 
   1131  1.1  riastrad retry:
   1132  1.1  riastrad 	for_each_intel_crtc(dev, crtc) {
   1133  1.1  riastrad 		struct intel_crtc_state *crtc_state =
   1134  1.1  riastrad 			intel_atomic_get_crtc_state(state, crtc);
   1135  1.1  riastrad 
   1136  1.1  riastrad 		if (IS_ERR(crtc_state)) {
   1137  1.1  riastrad 			err = PTR_ERR(crtc_state);
   1138  1.1  riastrad 			goto error;
   1139  1.1  riastrad 		}
   1140  1.1  riastrad 
   1141  1.1  riastrad 		if (crtc_state->hw.active && crtc_state->has_psr) {
   1142  1.1  riastrad 			/* Mark mode as changed to trigger a pipe->update() */
   1143  1.1  riastrad 			crtc_state->uapi.mode_changed = true;
   1144  1.1  riastrad 			break;
   1145  1.1  riastrad 		}
   1146  1.1  riastrad 	}
   1147  1.1  riastrad 
   1148  1.1  riastrad 	err = drm_atomic_commit(state);
   1149  1.1  riastrad 
   1150  1.1  riastrad error:
   1151  1.1  riastrad 	if (err == -EDEADLK) {
   1152  1.1  riastrad 		drm_atomic_state_clear(state);
   1153  1.1  riastrad 		err = drm_modeset_backoff(&ctx);
   1154  1.1  riastrad 		if (!err)
   1155  1.1  riastrad 			goto retry;
   1156  1.1  riastrad 	}
   1157  1.1  riastrad 
   1158  1.1  riastrad 	drm_modeset_drop_locks(&ctx);
   1159  1.1  riastrad 	drm_modeset_acquire_fini(&ctx);
   1160  1.1  riastrad 	drm_atomic_state_put(state);
   1161  1.1  riastrad 
   1162  1.1  riastrad 	return err;
   1163  1.1  riastrad }
   1164  1.1  riastrad 
   1165  1.1  riastrad int intel_psr_debug_set(struct drm_i915_private *dev_priv, u64 val)
   1166  1.1  riastrad {
   1167  1.1  riastrad 	const u32 mode = val & I915_PSR_DEBUG_MODE_MASK;
   1168  1.1  riastrad 	u32 old_mode;
   1169  1.1  riastrad 	int ret;
   1170  1.1  riastrad 
   1171  1.1  riastrad 	if (val & ~(I915_PSR_DEBUG_IRQ | I915_PSR_DEBUG_MODE_MASK) ||
   1172  1.1  riastrad 	    mode > I915_PSR_DEBUG_FORCE_PSR1) {
   1173  1.3  riastrad 		DRM_DEBUG_KMS("Invalid debug mask %"PRIx64"\n", val);
   1174  1.1  riastrad 		return -EINVAL;
   1175  1.1  riastrad 	}
   1176  1.1  riastrad 
   1177  1.1  riastrad 	ret = mutex_lock_interruptible(&dev_priv->psr.lock);
   1178  1.1  riastrad 	if (ret)
   1179  1.1  riastrad 		return ret;
   1180  1.1  riastrad 
   1181  1.1  riastrad 	old_mode = dev_priv->psr.debug & I915_PSR_DEBUG_MODE_MASK;
   1182  1.1  riastrad 	dev_priv->psr.debug = val;
   1183  1.1  riastrad 
   1184  1.1  riastrad 	/*
   1185  1.1  riastrad 	 * Do it right away if it's already enabled, otherwise it will be done
   1186  1.1  riastrad 	 * when enabling the source.
   1187  1.1  riastrad 	 */
   1188  1.1  riastrad 	if (dev_priv->psr.enabled)
   1189  1.1  riastrad 		psr_irq_control(dev_priv);
   1190  1.1  riastrad 
   1191  1.1  riastrad 	mutex_unlock(&dev_priv->psr.lock);
   1192  1.1  riastrad 
   1193  1.1  riastrad 	if (old_mode != mode)
   1194  1.1  riastrad 		ret = intel_psr_fastset_force(dev_priv);
   1195  1.1  riastrad 
   1196  1.1  riastrad 	return ret;
   1197  1.1  riastrad }
   1198  1.1  riastrad 
   1199  1.1  riastrad static void intel_psr_handle_irq(struct drm_i915_private *dev_priv)
   1200  1.1  riastrad {
   1201  1.1  riastrad 	struct i915_psr *psr = &dev_priv->psr;
   1202  1.1  riastrad 
   1203  1.1  riastrad 	intel_psr_disable_locked(psr->dp);
   1204  1.1  riastrad 	psr->sink_not_reliable = true;
   1205  1.1  riastrad 	/* let's make sure that sink is awaken */
   1206  1.1  riastrad 	drm_dp_dpcd_writeb(&psr->dp->aux, DP_SET_POWER, DP_SET_POWER_D0);
   1207  1.1  riastrad }
   1208  1.1  riastrad 
   1209  1.1  riastrad static void intel_psr_work(struct work_struct *work)
   1210  1.1  riastrad {
   1211  1.1  riastrad 	struct drm_i915_private *dev_priv =
   1212  1.1  riastrad 		container_of(work, typeof(*dev_priv), psr.work);
   1213  1.1  riastrad 
   1214  1.1  riastrad 	mutex_lock(&dev_priv->psr.lock);
   1215  1.1  riastrad 
   1216  1.1  riastrad 	if (!dev_priv->psr.enabled)
   1217  1.1  riastrad 		goto unlock;
   1218  1.1  riastrad 
   1219  1.1  riastrad 	if (READ_ONCE(dev_priv->psr.irq_aux_error))
   1220  1.1  riastrad 		intel_psr_handle_irq(dev_priv);
   1221  1.1  riastrad 
   1222  1.1  riastrad 	/*
   1223  1.1  riastrad 	 * We have to make sure PSR is ready for re-enable
   1224  1.1  riastrad 	 * otherwise it keeps disabled until next full enable/disable cycle.
   1225  1.1  riastrad 	 * PSR might take some time to get fully disabled
   1226  1.1  riastrad 	 * and be ready for re-enable.
   1227  1.1  riastrad 	 */
   1228  1.1  riastrad 	if (!__psr_wait_for_idle_locked(dev_priv))
   1229  1.1  riastrad 		goto unlock;
   1230  1.1  riastrad 
   1231  1.1  riastrad 	/*
   1232  1.1  riastrad 	 * The delayed work can race with an invalidate hence we need to
   1233  1.1  riastrad 	 * recheck. Since psr_flush first clears this and then reschedules we
   1234  1.1  riastrad 	 * won't ever miss a flush when bailing out here.
   1235  1.1  riastrad 	 */
   1236  1.1  riastrad 	if (dev_priv->psr.busy_frontbuffer_bits || dev_priv->psr.active)
   1237  1.1  riastrad 		goto unlock;
   1238  1.1  riastrad 
   1239  1.1  riastrad 	intel_psr_activate(dev_priv->psr.dp);
   1240  1.1  riastrad unlock:
   1241  1.1  riastrad 	mutex_unlock(&dev_priv->psr.lock);
   1242  1.1  riastrad }
   1243  1.1  riastrad 
   1244  1.1  riastrad /**
   1245  1.1  riastrad  * intel_psr_invalidate - Invalidade PSR
   1246  1.1  riastrad  * @dev_priv: i915 device
   1247  1.1  riastrad  * @frontbuffer_bits: frontbuffer plane tracking bits
   1248  1.1  riastrad  * @origin: which operation caused the invalidate
   1249  1.1  riastrad  *
   1250  1.1  riastrad  * Since the hardware frontbuffer tracking has gaps we need to integrate
   1251  1.1  riastrad  * with the software frontbuffer tracking. This function gets called every
   1252  1.1  riastrad  * time frontbuffer rendering starts and a buffer gets dirtied. PSR must be
   1253  1.1  riastrad  * disabled if the frontbuffer mask contains a buffer relevant to PSR.
   1254  1.1  riastrad  *
   1255  1.1  riastrad  * Dirty frontbuffers relevant to PSR are tracked in busy_frontbuffer_bits."
   1256  1.1  riastrad  */
   1257  1.1  riastrad void intel_psr_invalidate(struct drm_i915_private *dev_priv,
   1258  1.1  riastrad 			  unsigned frontbuffer_bits, enum fb_op_origin origin)
   1259  1.1  riastrad {
   1260  1.1  riastrad 	if (!CAN_PSR(dev_priv))
   1261  1.1  riastrad 		return;
   1262  1.1  riastrad 
   1263  1.1  riastrad 	if (origin == ORIGIN_FLIP)
   1264  1.1  riastrad 		return;
   1265  1.1  riastrad 
   1266  1.1  riastrad 	mutex_lock(&dev_priv->psr.lock);
   1267  1.1  riastrad 	if (!dev_priv->psr.enabled) {
   1268  1.1  riastrad 		mutex_unlock(&dev_priv->psr.lock);
   1269  1.1  riastrad 		return;
   1270  1.1  riastrad 	}
   1271  1.1  riastrad 
   1272  1.1  riastrad 	frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(dev_priv->psr.pipe);
   1273  1.1  riastrad 	dev_priv->psr.busy_frontbuffer_bits |= frontbuffer_bits;
   1274  1.1  riastrad 
   1275  1.1  riastrad 	if (frontbuffer_bits)
   1276  1.1  riastrad 		intel_psr_exit(dev_priv);
   1277  1.1  riastrad 
   1278  1.1  riastrad 	mutex_unlock(&dev_priv->psr.lock);
   1279  1.1  riastrad }
   1280  1.1  riastrad 
   1281  1.1  riastrad /*
   1282  1.1  riastrad  * When we will be completely rely on PSR2 S/W tracking in future,
   1283  1.1  riastrad  * intel_psr_flush() will invalidate and flush the PSR for ORIGIN_FLIP
   1284  1.1  riastrad  * event also therefore tgl_dc3co_flush() require to be changed
   1285  1.1  riastrad  * accrodingly in future.
   1286  1.1  riastrad  */
   1287  1.1  riastrad static void
   1288  1.1  riastrad tgl_dc3co_flush(struct drm_i915_private *dev_priv,
   1289  1.1  riastrad 		unsigned int frontbuffer_bits, enum fb_op_origin origin)
   1290  1.1  riastrad {
   1291  1.1  riastrad 	u32 delay;
   1292  1.1  riastrad 
   1293  1.1  riastrad 	mutex_lock(&dev_priv->psr.lock);
   1294  1.1  riastrad 
   1295  1.1  riastrad 	if (!dev_priv->psr.dc3co_enabled)
   1296  1.1  riastrad 		goto unlock;
   1297  1.1  riastrad 
   1298  1.1  riastrad 	if (!dev_priv->psr.psr2_enabled || !dev_priv->psr.active)
   1299  1.1  riastrad 		goto unlock;
   1300  1.1  riastrad 
   1301  1.1  riastrad 	/*
   1302  1.1  riastrad 	 * At every frontbuffer flush flip event modified delay of delayed work,
   1303  1.1  riastrad 	 * when delayed work schedules that means display has been idle.
   1304  1.1  riastrad 	 */
   1305  1.1  riastrad 	if (!(frontbuffer_bits &
   1306  1.1  riastrad 	    INTEL_FRONTBUFFER_ALL_MASK(dev_priv->psr.pipe)))
   1307  1.1  riastrad 		goto unlock;
   1308  1.1  riastrad 
   1309  1.1  riastrad 	tgl_psr2_enable_dc3co(dev_priv);
   1310  1.1  riastrad 	/* DC5/DC6 required idle frames = 6 */
   1311  1.1  riastrad 	delay = 6 * dev_priv->psr.dc3co_exit_delay;
   1312  1.1  riastrad 	mod_delayed_work(system_wq, &dev_priv->psr.idle_work,
   1313  1.1  riastrad 			 usecs_to_jiffies(delay));
   1314  1.1  riastrad 
   1315  1.1  riastrad unlock:
   1316  1.1  riastrad 	mutex_unlock(&dev_priv->psr.lock);
   1317  1.1  riastrad }
   1318  1.1  riastrad 
   1319  1.1  riastrad /**
   1320  1.1  riastrad  * intel_psr_flush - Flush PSR
   1321  1.1  riastrad  * @dev_priv: i915 device
   1322  1.1  riastrad  * @frontbuffer_bits: frontbuffer plane tracking bits
   1323  1.1  riastrad  * @origin: which operation caused the flush
   1324  1.1  riastrad  *
   1325  1.1  riastrad  * Since the hardware frontbuffer tracking has gaps we need to integrate
   1326  1.1  riastrad  * with the software frontbuffer tracking. This function gets called every
   1327  1.1  riastrad  * time frontbuffer rendering has completed and flushed out to memory. PSR
   1328  1.1  riastrad  * can be enabled again if no other frontbuffer relevant to PSR is dirty.
   1329  1.1  riastrad  *
   1330  1.1  riastrad  * Dirty frontbuffers relevant to PSR are tracked in busy_frontbuffer_bits.
   1331  1.1  riastrad  */
   1332  1.1  riastrad void intel_psr_flush(struct drm_i915_private *dev_priv,
   1333  1.1  riastrad 		     unsigned frontbuffer_bits, enum fb_op_origin origin)
   1334  1.1  riastrad {
   1335  1.1  riastrad 	if (!CAN_PSR(dev_priv))
   1336  1.1  riastrad 		return;
   1337  1.1  riastrad 
   1338  1.1  riastrad 	if (origin == ORIGIN_FLIP) {
   1339  1.1  riastrad 		tgl_dc3co_flush(dev_priv, frontbuffer_bits, origin);
   1340  1.1  riastrad 		return;
   1341  1.1  riastrad 	}
   1342  1.1  riastrad 
   1343  1.1  riastrad 	mutex_lock(&dev_priv->psr.lock);
   1344  1.1  riastrad 	if (!dev_priv->psr.enabled) {
   1345  1.1  riastrad 		mutex_unlock(&dev_priv->psr.lock);
   1346  1.1  riastrad 		return;
   1347  1.1  riastrad 	}
   1348  1.1  riastrad 
   1349  1.1  riastrad 	frontbuffer_bits &= INTEL_FRONTBUFFER_ALL_MASK(dev_priv->psr.pipe);
   1350  1.1  riastrad 	dev_priv->psr.busy_frontbuffer_bits &= ~frontbuffer_bits;
   1351  1.1  riastrad 
   1352  1.1  riastrad 	/* By definition flush = invalidate + flush */
   1353  1.1  riastrad 	if (frontbuffer_bits)
   1354  1.1  riastrad 		psr_force_hw_tracking_exit(dev_priv);
   1355  1.1  riastrad 
   1356  1.1  riastrad 	if (!dev_priv->psr.active && !dev_priv->psr.busy_frontbuffer_bits)
   1357  1.1  riastrad 		schedule_work(&dev_priv->psr.work);
   1358  1.1  riastrad 	mutex_unlock(&dev_priv->psr.lock);
   1359  1.1  riastrad }
   1360  1.1  riastrad 
   1361  1.1  riastrad /**
   1362  1.1  riastrad  * intel_psr_init - Init basic PSR work and mutex.
   1363  1.1  riastrad  * @dev_priv: i915 device private
   1364  1.1  riastrad  *
   1365  1.1  riastrad  * This function is  called only once at driver load to initialize basic
   1366  1.1  riastrad  * PSR stuff.
   1367  1.1  riastrad  */
   1368  1.1  riastrad void intel_psr_init(struct drm_i915_private *dev_priv)
   1369  1.1  riastrad {
   1370  1.1  riastrad 	if (!HAS_PSR(dev_priv))
   1371  1.1  riastrad 		return;
   1372  1.1  riastrad 
   1373  1.1  riastrad 	if (!dev_priv->psr.sink_support)
   1374  1.1  riastrad 		return;
   1375  1.1  riastrad 
   1376  1.1  riastrad 	if (IS_HASWELL(dev_priv))
   1377  1.1  riastrad 		/*
   1378  1.1  riastrad 		 * HSW don't have PSR registers on the same space as transcoder
   1379  1.1  riastrad 		 * so set this to a value that when subtract to the register
   1380  1.1  riastrad 		 * in transcoder space results in the right offset for HSW
   1381  1.1  riastrad 		 */
   1382  1.1  riastrad 		dev_priv->hsw_psr_mmio_adjust = _SRD_CTL_EDP - _HSW_EDP_PSR_BASE;
   1383  1.1  riastrad 
   1384  1.1  riastrad 	if (i915_modparams.enable_psr == -1)
   1385  1.1  riastrad 		if (INTEL_GEN(dev_priv) < 9 || !dev_priv->vbt.psr.enable)
   1386  1.1  riastrad 			i915_modparams.enable_psr = 0;
   1387  1.1  riastrad 
   1388  1.1  riastrad 	/* Set link_standby x link_off defaults */
   1389  1.1  riastrad 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
   1390  1.1  riastrad 		/* HSW and BDW require workarounds that we don't implement. */
   1391  1.1  riastrad 		dev_priv->psr.link_standby = false;
   1392  1.1  riastrad 	else if (INTEL_GEN(dev_priv) < 12)
   1393  1.1  riastrad 		/* For new platforms up to TGL let's respect VBT back again */
   1394  1.1  riastrad 		dev_priv->psr.link_standby = dev_priv->vbt.psr.full_link;
   1395  1.1  riastrad 
   1396  1.1  riastrad 	INIT_WORK(&dev_priv->psr.work, intel_psr_work);
   1397  1.1  riastrad 	INIT_DELAYED_WORK(&dev_priv->psr.idle_work, tgl_dc5_idle_thread);
   1398  1.1  riastrad 	mutex_init(&dev_priv->psr.lock);
   1399  1.1  riastrad }
   1400  1.1  riastrad 
   1401  1.1  riastrad static int psr_get_status_and_error_status(struct intel_dp *intel_dp,
   1402  1.1  riastrad 					   u8 *status, u8 *error_status)
   1403  1.1  riastrad {
   1404  1.1  riastrad 	struct drm_dp_aux *aux = &intel_dp->aux;
   1405  1.1  riastrad 	int ret;
   1406  1.1  riastrad 
   1407  1.1  riastrad 	ret = drm_dp_dpcd_readb(aux, DP_PSR_STATUS, status);
   1408  1.1  riastrad 	if (ret != 1)
   1409  1.1  riastrad 		return ret;
   1410  1.1  riastrad 
   1411  1.1  riastrad 	ret = drm_dp_dpcd_readb(aux, DP_PSR_ERROR_STATUS, error_status);
   1412  1.1  riastrad 	if (ret != 1)
   1413  1.1  riastrad 		return ret;
   1414  1.1  riastrad 
   1415  1.1  riastrad 	*status = *status & DP_PSR_SINK_STATE_MASK;
   1416  1.1  riastrad 
   1417  1.1  riastrad 	return 0;
   1418  1.1  riastrad }
   1419  1.1  riastrad 
   1420  1.1  riastrad static void psr_alpm_check(struct intel_dp *intel_dp)
   1421  1.1  riastrad {
   1422  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
   1423  1.1  riastrad 	struct drm_dp_aux *aux = &intel_dp->aux;
   1424  1.1  riastrad 	struct i915_psr *psr = &dev_priv->psr;
   1425  1.1  riastrad 	u8 val;
   1426  1.1  riastrad 	int r;
   1427  1.1  riastrad 
   1428  1.1  riastrad 	if (!psr->psr2_enabled)
   1429  1.1  riastrad 		return;
   1430  1.1  riastrad 
   1431  1.1  riastrad 	r = drm_dp_dpcd_readb(aux, DP_RECEIVER_ALPM_STATUS, &val);
   1432  1.1  riastrad 	if (r != 1) {
   1433  1.1  riastrad 		DRM_ERROR("Error reading ALPM status\n");
   1434  1.1  riastrad 		return;
   1435  1.1  riastrad 	}
   1436  1.1  riastrad 
   1437  1.1  riastrad 	if (val & DP_ALPM_LOCK_TIMEOUT_ERROR) {
   1438  1.1  riastrad 		intel_psr_disable_locked(intel_dp);
   1439  1.1  riastrad 		psr->sink_not_reliable = true;
   1440  1.1  riastrad 		DRM_DEBUG_KMS("ALPM lock timeout error, disabling PSR\n");
   1441  1.1  riastrad 
   1442  1.1  riastrad 		/* Clearing error */
   1443  1.1  riastrad 		drm_dp_dpcd_writeb(aux, DP_RECEIVER_ALPM_STATUS, val);
   1444  1.1  riastrad 	}
   1445  1.1  riastrad }
   1446  1.1  riastrad 
   1447  1.1  riastrad static void psr_capability_changed_check(struct intel_dp *intel_dp)
   1448  1.1  riastrad {
   1449  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
   1450  1.1  riastrad 	struct i915_psr *psr = &dev_priv->psr;
   1451  1.1  riastrad 	u8 val;
   1452  1.1  riastrad 	int r;
   1453  1.1  riastrad 
   1454  1.1  riastrad 	r = drm_dp_dpcd_readb(&intel_dp->aux, DP_PSR_ESI, &val);
   1455  1.1  riastrad 	if (r != 1) {
   1456  1.1  riastrad 		DRM_ERROR("Error reading DP_PSR_ESI\n");
   1457  1.1  riastrad 		return;
   1458  1.1  riastrad 	}
   1459  1.1  riastrad 
   1460  1.1  riastrad 	if (val & DP_PSR_CAPS_CHANGE) {
   1461  1.1  riastrad 		intel_psr_disable_locked(intel_dp);
   1462  1.1  riastrad 		psr->sink_not_reliable = true;
   1463  1.1  riastrad 		DRM_DEBUG_KMS("Sink PSR capability changed, disabling PSR\n");
   1464  1.1  riastrad 
   1465  1.1  riastrad 		/* Clearing it */
   1466  1.1  riastrad 		drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_ESI, val);
   1467  1.1  riastrad 	}
   1468  1.1  riastrad }
   1469  1.1  riastrad 
   1470  1.1  riastrad void intel_psr_short_pulse(struct intel_dp *intel_dp)
   1471  1.1  riastrad {
   1472  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
   1473  1.1  riastrad 	struct i915_psr *psr = &dev_priv->psr;
   1474  1.1  riastrad 	u8 status, error_status;
   1475  1.1  riastrad 	const u8 errors = DP_PSR_RFB_STORAGE_ERROR |
   1476  1.1  riastrad 			  DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR |
   1477  1.1  riastrad 			  DP_PSR_LINK_CRC_ERROR;
   1478  1.1  riastrad 
   1479  1.1  riastrad 	if (!CAN_PSR(dev_priv) || !intel_dp_is_edp(intel_dp))
   1480  1.1  riastrad 		return;
   1481  1.1  riastrad 
   1482  1.1  riastrad 	mutex_lock(&psr->lock);
   1483  1.1  riastrad 
   1484  1.1  riastrad 	if (!psr->enabled || psr->dp != intel_dp)
   1485  1.1  riastrad 		goto exit;
   1486  1.1  riastrad 
   1487  1.1  riastrad 	if (psr_get_status_and_error_status(intel_dp, &status, &error_status)) {
   1488  1.1  riastrad 		DRM_ERROR("Error reading PSR status or error status\n");
   1489  1.1  riastrad 		goto exit;
   1490  1.1  riastrad 	}
   1491  1.1  riastrad 
   1492  1.1  riastrad 	if (status == DP_PSR_SINK_INTERNAL_ERROR || (error_status & errors)) {
   1493  1.1  riastrad 		intel_psr_disable_locked(intel_dp);
   1494  1.1  riastrad 		psr->sink_not_reliable = true;
   1495  1.1  riastrad 	}
   1496  1.1  riastrad 
   1497  1.1  riastrad 	if (status == DP_PSR_SINK_INTERNAL_ERROR && !error_status)
   1498  1.1  riastrad 		DRM_DEBUG_KMS("PSR sink internal error, disabling PSR\n");
   1499  1.1  riastrad 	if (error_status & DP_PSR_RFB_STORAGE_ERROR)
   1500  1.1  riastrad 		DRM_DEBUG_KMS("PSR RFB storage error, disabling PSR\n");
   1501  1.1  riastrad 	if (error_status & DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR)
   1502  1.1  riastrad 		DRM_DEBUG_KMS("PSR VSC SDP uncorrectable error, disabling PSR\n");
   1503  1.1  riastrad 	if (error_status & DP_PSR_LINK_CRC_ERROR)
   1504  1.1  riastrad 		DRM_DEBUG_KMS("PSR Link CRC error, disabling PSR\n");
   1505  1.1  riastrad 
   1506  1.1  riastrad 	if (error_status & ~errors)
   1507  1.1  riastrad 		DRM_ERROR("PSR_ERROR_STATUS unhandled errors %x\n",
   1508  1.1  riastrad 			  error_status & ~errors);
   1509  1.1  riastrad 	/* clear status register */
   1510  1.1  riastrad 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_ERROR_STATUS, error_status);
   1511  1.1  riastrad 
   1512  1.1  riastrad 	psr_alpm_check(intel_dp);
   1513  1.1  riastrad 	psr_capability_changed_check(intel_dp);
   1514  1.1  riastrad 
   1515  1.1  riastrad exit:
   1516  1.1  riastrad 	mutex_unlock(&psr->lock);
   1517  1.1  riastrad }
   1518  1.1  riastrad 
   1519  1.1  riastrad bool intel_psr_enabled(struct intel_dp *intel_dp)
   1520  1.1  riastrad {
   1521  1.1  riastrad 	struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
   1522  1.1  riastrad 	bool ret;
   1523  1.1  riastrad 
   1524  1.1  riastrad 	if (!CAN_PSR(dev_priv) || !intel_dp_is_edp(intel_dp))
   1525  1.1  riastrad 		return false;
   1526  1.1  riastrad 
   1527  1.1  riastrad 	mutex_lock(&dev_priv->psr.lock);
   1528  1.1  riastrad 	ret = (dev_priv->psr.dp == intel_dp && dev_priv->psr.enabled);
   1529  1.1  riastrad 	mutex_unlock(&dev_priv->psr.lock);
   1530  1.1  riastrad 
   1531  1.1  riastrad 	return ret;
   1532  1.1  riastrad }
   1533  1.1  riastrad 
   1534  1.1  riastrad void intel_psr_atomic_check(struct drm_connector *connector,
   1535  1.1  riastrad 			    struct drm_connector_state *old_state,
   1536  1.1  riastrad 			    struct drm_connector_state *new_state)
   1537  1.1  riastrad {
   1538  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(connector->dev);
   1539  1.1  riastrad 	struct intel_connector *intel_connector;
   1540  1.1  riastrad 	struct intel_digital_port *dig_port;
   1541  1.1  riastrad 	struct drm_crtc_state *crtc_state;
   1542  1.1  riastrad 
   1543  1.1  riastrad 	if (!CAN_PSR(dev_priv) || !new_state->crtc ||
   1544  1.1  riastrad 	    dev_priv->psr.initially_probed)
   1545  1.1  riastrad 		return;
   1546  1.1  riastrad 
   1547  1.1  riastrad 	intel_connector = to_intel_connector(connector);
   1548  1.1  riastrad 	dig_port = enc_to_dig_port(intel_connector->encoder);
   1549  1.1  riastrad 	if (dev_priv->psr.dp != &dig_port->dp)
   1550  1.1  riastrad 		return;
   1551  1.1  riastrad 
   1552  1.1  riastrad 	crtc_state = drm_atomic_get_new_crtc_state(new_state->state,
   1553  1.1  riastrad 						   new_state->crtc);
   1554  1.1  riastrad 	crtc_state->mode_changed = true;
   1555  1.1  riastrad 	dev_priv->psr.initially_probed = true;
   1556  1.1  riastrad }
   1557