xref: /linux/drivers/gpu/drm/i915/display/intel_psr.c (revision 6812ce4e4379ffc99c52401ec28f0d7ffbc36206)
1 /*
2  * Copyright © 2014 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <linux/debugfs.h>
25 
26 #include <drm/drm_atomic_helper.h>
27 #include <drm/drm_damage_helper.h>
28 #include <drm/drm_debugfs.h>
29 #include <drm/drm_print.h>
30 #include <drm/drm_vblank.h>
31 #include <drm/intel/step.h>
32 
33 #include "intel_alpm.h"
34 #include "intel_atomic.h"
35 #include "intel_crtc.h"
36 #include "intel_cursor_regs.h"
37 #include "intel_ddi.h"
38 #include "intel_de.h"
39 #include "intel_display_irq.h"
40 #include "intel_display_regs.h"
41 #include "intel_display_rpm.h"
42 #include "intel_display_types.h"
43 #include "intel_display_utils.h"
44 #include "intel_display_wa.h"
45 #include "intel_dmc.h"
46 #include "intel_dp.h"
47 #include "intel_dpcd.h"
48 #include "intel_dp_aux.h"
49 #include "intel_dp_tunnel.h"
50 #include "intel_dsb.h"
51 #include "intel_frontbuffer.h"
52 #include "intel_hdmi.h"
53 #include "intel_psr.h"
54 #include "intel_psr_regs.h"
55 #include "intel_quirks.h"
56 #include "intel_snps_phy.h"
57 #include "intel_vblank.h"
58 #include "intel_vdsc.h"
59 #include "intel_vrr.h"
60 #include "skl_universal_plane.h"
61 
62 /**
63  * DOC: Panel Self Refresh (PSR/SRD)
64  *
65  * Since Haswell Display controller supports Panel Self-Refresh on display
66  * panels witch have a remote frame buffer (RFB) implemented according to PSR
67  * spec in eDP1.3. PSR feature allows the display to go to lower standby states
68  * when system is idle but display is on as it eliminates display refresh
69  * request to DDR memory completely as long as the frame buffer for that
70  * display is unchanged.
71  *
72  * Panel Self Refresh must be supported by both Hardware (source) and
73  * Panel (sink).
74  *
75  * PSR saves power by caching the framebuffer in the panel RFB, which allows us
76  * to power down the link and memory controller. For DSI panels the same idea
77  * is called "manual mode".
78  *
79  * The implementation uses the hardware-based PSR support which automatically
80  * enters/exits self-refresh mode. The hardware takes care of sending the
81  * required DP aux message and could even retrain the link (that part isn't
82  * enabled yet though). The hardware also keeps track of any frontbuffer
83  * changes to know when to exit self-refresh mode again. Unfortunately that
84  * part doesn't work too well, hence why the i915 PSR support uses the
85  * software frontbuffer tracking to make sure it doesn't miss a screen
86  * update. For this integration intel_psr_invalidate() and intel_psr_flush()
87  * get called by the frontbuffer tracking code. Note that because of locking
88  * issues the self-refresh re-enable code is done from a work queue, which
89  * must be correctly synchronized/cancelled when shutting down the pipe."
90  *
91  */
92 
93 /*
94  * Description of PSR mask bits:
95  *
96  * EDP_PSR_DEBUG[16]/EDP_PSR_DEBUG_MASK_DISP_REG_WRITE (hsw-skl):
97  *
98  *  When unmasked (nearly) all display register writes (eg. even
99  *  SWF) trigger a PSR exit. Some registers are excluded from this
100  *  and they have a more specific mask (described below). On icl+
101  *  this bit no longer exists and is effectively always set.
102  *
103  * PIPE_MISC[21]/PIPE_MISC_PSR_MASK_PIPE_REG_WRITE (skl+):
104  *
105  *  When unmasked (nearly) all pipe/plane register writes
106  *  trigger a PSR exit. Some plane registers are excluded from this
107  *  and they have a more specific mask (described below).
108  *
109  * CHICKEN_PIPESL_1[11]/SKL_PSR_MASK_PLANE_FLIP (skl+):
110  * PIPE_MISC[23]/PIPE_MISC_PSR_MASK_PRIMARY_FLIP (bdw):
111  * EDP_PSR_DEBUG[23]/EDP_PSR_DEBUG_MASK_PRIMARY_FLIP (hsw):
112  *
113  *  When unmasked PRI_SURF/PLANE_SURF writes trigger a PSR exit.
114  *  SPR_SURF/CURBASE are not included in this and instead are
115  *  controlled by PIPE_MISC_PSR_MASK_PIPE_REG_WRITE (skl+) or
116  *  EDP_PSR_DEBUG_MASK_DISP_REG_WRITE (hsw/bdw).
117  *
118  * PIPE_MISC[22]/PIPE_MISC_PSR_MASK_SPRITE_ENABLE (bdw):
119  * EDP_PSR_DEBUG[21]/EDP_PSR_DEBUG_MASK_SPRITE_ENABLE (hsw):
120  *
121  *  When unmasked PSR is blocked as long as the sprite
122  *  plane is enabled. skl+ with their universal planes no
123  *  longer have a mask bit like this, and no plane being
124  *  enabledb blocks PSR.
125  *
126  * PIPE_MISC[21]/PIPE_MISC_PSR_MASK_CURSOR_MOVE (bdw):
127  * EDP_PSR_DEBUG[20]/EDP_PSR_DEBUG_MASK_CURSOR_MOVE (hsw):
128  *
129  *  When umasked CURPOS writes trigger a PSR exit. On skl+
130  *  this doesn't exit but CURPOS is included in the
131  *  PIPE_MISC_PSR_MASK_PIPE_REG_WRITE mask.
132  *
133  * PIPE_MISC[20]/PIPE_MISC_PSR_MASK_VBLANK_VSYNC_INT (bdw+):
134  * EDP_PSR_DEBUG[19]/EDP_PSR_DEBUG_MASK_VBLANK_VSYNC_INT (hsw):
135  *
136  *  When unmasked PSR is blocked as long as vblank and/or vsync
137  *  interrupt is unmasked in IMR *and* enabled in IER.
138  *
139  * CHICKEN_TRANS[30]/SKL_UNMASK_VBL_TO_PIPE_IN_SRD (skl+):
140  * CHICKEN_PAR1_1[15]/HSW_MASK_VBL_TO_PIPE_IN_SRD (hsw/bdw):
141  *
142  *  Selectcs whether PSR exit generates an extra vblank before
143  *  the first frame is transmitted. Also note the opposite polarity
144  *  if the bit on hsw/bdw vs. skl+ (masked==generate the extra vblank,
145  *  unmasked==do not generate the extra vblank).
146  *
147  *  With DC states enabled the extra vblank happens after link training,
148  *  with DC states disabled it happens immediately upuon PSR exit trigger.
149  *  No idea as of now why there is a difference. HSW/BDW (which don't
150  *  even have DMC) always generate it after link training. Go figure.
151  *
152  *  Unfortunately CHICKEN_TRANS itself seems to be double buffered
153  *  and thus won't latch until the first vblank. So with DC states
154  *  enabled the register effectively uses the reset value during DC5
155  *  exit+PSR exit sequence, and thus the bit does nothing until
156  *  latched by the vblank that it was trying to prevent from being
157  *  generated in the first place. So we should probably call this
158  *  one a chicken/egg bit instead on skl+.
159  *
160  *  In standby mode (as opposed to link-off) this makes no difference
161  *  as the timing generator keeps running the whole time generating
162  *  normal periodic vblanks.
163  *
164  *  WaPsrDPAMaskVBlankInSRD asks us to set the bit on hsw/bdw,
165  *  and doing so makes the behaviour match the skl+ reset value.
166  *
167  * CHICKEN_PIPESL_1[0]/BDW_UNMASK_VBL_TO_REGS_IN_SRD (bdw):
168  * CHICKEN_PIPESL_1[15]/HSW_UNMASK_VBL_TO_REGS_IN_SRD (hsw):
169  *
170  *  On BDW without this bit is no vblanks whatsoever are
171  *  generated after PSR exit. On HSW this has no apparent effect.
172  *  WaPsrDPRSUnmaskVBlankInSRD says to set this.
173  *
174  * The rest of the bits are more self-explanatory and/or
175  * irrelevant for normal operation.
176  *
177  * Description of intel_crtc_state variables. has_psr, has_panel_replay and
178  * has_sel_update:
179  *
180  *  has_psr (alone):					PSR1
181  *  has_psr + has_sel_update:				PSR2
182  *  has_psr + has_panel_replay:				Panel Replay
183  *  has_psr + has_panel_replay + has_sel_update:	Panel Replay Selective Update
184  *
185  * Description of some intel_psr variables. enabled, panel_replay_enabled,
186  * sel_update_enabled
187  *
188  *  enabled (alone):						PSR1
189  *  enabled + sel_update_enabled:				PSR2
190  *  enabled + panel_replay_enabled:				Panel Replay
191  *  enabled + panel_replay_enabled + sel_update_enabled:	Panel Replay SU
192  */
193 
194 #define CAN_PSR(intel_dp) ((intel_dp)->psr.sink_support && \
195 			   (intel_dp)->psr.source_support)
196 
intel_encoder_can_psr(struct intel_encoder * encoder)197 bool intel_encoder_can_psr(struct intel_encoder *encoder)
198 {
199 	if (intel_encoder_is_dp(encoder) || encoder->type == INTEL_OUTPUT_DP_MST)
200 		return CAN_PSR(enc_to_intel_dp(encoder)) ||
201 		       CAN_PANEL_REPLAY(enc_to_intel_dp(encoder));
202 	else
203 		return false;
204 }
205 
intel_psr_needs_aux_io_power(struct intel_encoder * encoder,const struct intel_crtc_state * crtc_state)206 bool intel_psr_needs_aux_io_power(struct intel_encoder *encoder,
207 				  const struct intel_crtc_state *crtc_state)
208 {
209 	/*
210 	 * For PSR/PR modes only eDP requires the AUX IO power to be enabled whenever
211 	 * the output is enabled. For non-eDP outputs the main link is always
212 	 * on, hence it doesn't require the HW initiated AUX wake-up signaling used
213 	 * for eDP.
214 	 *
215 	 * TODO:
216 	 * - Consider leaving AUX IO disabled for eDP / PR as well, in case
217 	 *   the ALPM with main-link off mode is not enabled.
218 	 * - Leave AUX IO enabled for DP / PR, once support for ALPM with
219 	 *   main-link off mode is added for it and this mode gets enabled.
220 	 */
221 	return intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP) &&
222 	       intel_encoder_can_psr(encoder);
223 }
224 
psr_global_enabled(struct intel_dp * intel_dp)225 static bool psr_global_enabled(struct intel_dp *intel_dp)
226 {
227 	struct intel_connector *connector = intel_dp->attached_connector;
228 
229 	switch (intel_dp->psr.debug & I915_PSR_DEBUG_MODE_MASK) {
230 	case I915_PSR_DEBUG_DEFAULT:
231 		return intel_dp_is_edp(intel_dp) ?
232 			connector->panel.vbt.psr.enable : true;
233 	case I915_PSR_DEBUG_DISABLE:
234 		return false;
235 	default:
236 		return true;
237 	}
238 }
239 
sel_update_global_enabled(struct intel_dp * intel_dp)240 static bool sel_update_global_enabled(struct intel_dp *intel_dp)
241 {
242 	switch (intel_dp->psr.debug & I915_PSR_DEBUG_MODE_MASK) {
243 	case I915_PSR_DEBUG_DISABLE:
244 	case I915_PSR_DEBUG_FORCE_PSR1:
245 		return false;
246 	default:
247 		return true;
248 	}
249 }
250 
panel_replay_global_enabled(struct intel_dp * intel_dp)251 static bool panel_replay_global_enabled(struct intel_dp *intel_dp)
252 {
253 	struct intel_display *display = to_intel_display(intel_dp);
254 
255 	return !(intel_dp->psr.debug & I915_PSR_DEBUG_PANEL_REPLAY_DISABLE) &&
256 		display->params.enable_panel_replay;
257 }
258 
psr_irq_psr_error_bit_get(struct intel_dp * intel_dp)259 static u32 psr_irq_psr_error_bit_get(struct intel_dp *intel_dp)
260 {
261 	struct intel_display *display = to_intel_display(intel_dp);
262 
263 	return DISPLAY_VER(display) >= 12 ? TGL_PSR_ERROR :
264 		EDP_PSR_ERROR(intel_dp->psr.transcoder);
265 }
266 
psr_irq_post_exit_bit_get(struct intel_dp * intel_dp)267 static u32 psr_irq_post_exit_bit_get(struct intel_dp *intel_dp)
268 {
269 	struct intel_display *display = to_intel_display(intel_dp);
270 
271 	return DISPLAY_VER(display) >= 12 ? TGL_PSR_POST_EXIT :
272 		EDP_PSR_POST_EXIT(intel_dp->psr.transcoder);
273 }
274 
psr_irq_pre_entry_bit_get(struct intel_dp * intel_dp)275 static u32 psr_irq_pre_entry_bit_get(struct intel_dp *intel_dp)
276 {
277 	struct intel_display *display = to_intel_display(intel_dp);
278 
279 	return DISPLAY_VER(display) >= 12 ? TGL_PSR_PRE_ENTRY :
280 		EDP_PSR_PRE_ENTRY(intel_dp->psr.transcoder);
281 }
282 
psr_irq_mask_get(struct intel_dp * intel_dp)283 static u32 psr_irq_mask_get(struct intel_dp *intel_dp)
284 {
285 	struct intel_display *display = to_intel_display(intel_dp);
286 
287 	return DISPLAY_VER(display) >= 12 ? TGL_PSR_MASK :
288 		EDP_PSR_MASK(intel_dp->psr.transcoder);
289 }
290 
psr_ctl_reg(struct intel_display * display,enum transcoder cpu_transcoder)291 static intel_reg_t psr_ctl_reg(struct intel_display *display,
292 			       enum transcoder cpu_transcoder)
293 {
294 	if (DISPLAY_VER(display) >= 8)
295 		return EDP_PSR_CTL(display, cpu_transcoder);
296 	else
297 		return HSW_SRD_CTL;
298 }
299 
psr_debug_reg(struct intel_display * display,enum transcoder cpu_transcoder)300 static intel_reg_t psr_debug_reg(struct intel_display *display,
301 				 enum transcoder cpu_transcoder)
302 {
303 	if (DISPLAY_VER(display) >= 8)
304 		return EDP_PSR_DEBUG(display, cpu_transcoder);
305 	else
306 		return HSW_SRD_DEBUG;
307 }
308 
psr_perf_cnt_reg(struct intel_display * display,enum transcoder cpu_transcoder)309 static intel_reg_t psr_perf_cnt_reg(struct intel_display *display,
310 				    enum transcoder cpu_transcoder)
311 {
312 	if (DISPLAY_VER(display) >= 8)
313 		return EDP_PSR_PERF_CNT(display, cpu_transcoder);
314 	else
315 		return HSW_SRD_PERF_CNT;
316 }
317 
psr_status_reg(struct intel_display * display,enum transcoder cpu_transcoder)318 static intel_reg_t psr_status_reg(struct intel_display *display,
319 				  enum transcoder cpu_transcoder)
320 {
321 	if (DISPLAY_VER(display) >= 8)
322 		return EDP_PSR_STATUS(display, cpu_transcoder);
323 	else
324 		return HSW_SRD_STATUS;
325 }
326 
psr_imr_reg(struct intel_display * display,enum transcoder cpu_transcoder)327 static intel_reg_t psr_imr_reg(struct intel_display *display,
328 			       enum transcoder cpu_transcoder)
329 {
330 	if (DISPLAY_VER(display) >= 12)
331 		return TRANS_PSR_IMR(display, cpu_transcoder);
332 	else
333 		return EDP_PSR_IMR;
334 }
335 
psr_iir_reg(struct intel_display * display,enum transcoder cpu_transcoder)336 static intel_reg_t psr_iir_reg(struct intel_display *display,
337 			       enum transcoder cpu_transcoder)
338 {
339 	if (DISPLAY_VER(display) >= 12)
340 		return TRANS_PSR_IIR(display, cpu_transcoder);
341 	else
342 		return EDP_PSR_IIR;
343 }
344 
psr_aux_ctl_reg(struct intel_display * display,enum transcoder cpu_transcoder)345 static intel_reg_t psr_aux_ctl_reg(struct intel_display *display,
346 				   enum transcoder cpu_transcoder)
347 {
348 	if (DISPLAY_VER(display) >= 8)
349 		return EDP_PSR_AUX_CTL(display, cpu_transcoder);
350 	else
351 		return HSW_SRD_AUX_CTL;
352 }
353 
psr_aux_data_reg(struct intel_display * display,enum transcoder cpu_transcoder,int i)354 static intel_reg_t psr_aux_data_reg(struct intel_display *display,
355 				    enum transcoder cpu_transcoder, int i)
356 {
357 	if (DISPLAY_VER(display) >= 8)
358 		return EDP_PSR_AUX_DATA(display, cpu_transcoder, i);
359 	else
360 		return HSW_SRD_AUX_DATA(i);
361 }
362 
psr_irq_control(struct intel_dp * intel_dp)363 static void psr_irq_control(struct intel_dp *intel_dp)
364 {
365 	struct intel_display *display = to_intel_display(intel_dp);
366 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
367 	u32 mask;
368 
369 	if (intel_dp->psr.panel_replay_enabled)
370 		return;
371 
372 	mask = psr_irq_psr_error_bit_get(intel_dp);
373 	if (intel_dp->psr.debug & I915_PSR_DEBUG_IRQ)
374 		mask |= psr_irq_post_exit_bit_get(intel_dp) |
375 			psr_irq_pre_entry_bit_get(intel_dp);
376 
377 	intel_de_rmw(display, psr_imr_reg(display, cpu_transcoder),
378 		     psr_irq_mask_get(intel_dp), ~mask);
379 }
380 
psr_event_print(struct intel_display * display,u32 val,bool sel_update_enabled)381 static void psr_event_print(struct intel_display *display,
382 			    u32 val, bool sel_update_enabled)
383 {
384 	drm_dbg_kms(display->drm, "PSR exit events: 0x%x\n", val);
385 	if (val & PSR_EVENT_PSR2_WD_TIMER_EXPIRE)
386 		drm_dbg_kms(display->drm, "\tPSR2 watchdog timer expired\n");
387 	if ((val & PSR_EVENT_PSR2_DISABLED) && sel_update_enabled)
388 		drm_dbg_kms(display->drm, "\tPSR2 disabled\n");
389 	if (val & PSR_EVENT_SU_DIRTY_FIFO_UNDERRUN)
390 		drm_dbg_kms(display->drm, "\tSU dirty FIFO underrun\n");
391 	if (val & PSR_EVENT_SU_CRC_FIFO_UNDERRUN)
392 		drm_dbg_kms(display->drm, "\tSU CRC FIFO underrun\n");
393 	if (val & PSR_EVENT_GRAPHICS_RESET)
394 		drm_dbg_kms(display->drm, "\tGraphics reset\n");
395 	if (val & PSR_EVENT_PCH_INTERRUPT)
396 		drm_dbg_kms(display->drm, "\tPCH interrupt\n");
397 	if (val & PSR_EVENT_MEMORY_UP)
398 		drm_dbg_kms(display->drm, "\tMemory up\n");
399 	if (val & PSR_EVENT_FRONT_BUFFER_MODIFY)
400 		drm_dbg_kms(display->drm, "\tFront buffer modification\n");
401 	if (val & PSR_EVENT_WD_TIMER_EXPIRE)
402 		drm_dbg_kms(display->drm, "\tPSR watchdog timer expired\n");
403 	if (val & PSR_EVENT_PIPE_REGISTERS_UPDATE)
404 		drm_dbg_kms(display->drm, "\tPIPE registers updated\n");
405 	if (val & PSR_EVENT_REGISTER_UPDATE)
406 		drm_dbg_kms(display->drm, "\tRegister updated\n");
407 	if (val & PSR_EVENT_HDCP_ENABLE)
408 		drm_dbg_kms(display->drm, "\tHDCP enabled\n");
409 	if (val & PSR_EVENT_KVMR_SESSION_ENABLE)
410 		drm_dbg_kms(display->drm, "\tKVMR session enabled\n");
411 	if (val & PSR_EVENT_VBI_ENABLE)
412 		drm_dbg_kms(display->drm, "\tVBI enabled\n");
413 	if (val & PSR_EVENT_LPSP_MODE_EXIT)
414 		drm_dbg_kms(display->drm, "\tLPSP mode exited\n");
415 	if ((val & PSR_EVENT_PSR_DISABLE) && !sel_update_enabled)
416 		drm_dbg_kms(display->drm, "\tPSR disabled\n");
417 }
418 
intel_psr_irq_handler(struct intel_dp * intel_dp,u32 psr_iir)419 void intel_psr_irq_handler(struct intel_dp *intel_dp, u32 psr_iir)
420 {
421 	struct intel_display *display = to_intel_display(intel_dp);
422 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
423 	ktime_t time_ns =  ktime_get();
424 
425 	if (psr_iir & psr_irq_pre_entry_bit_get(intel_dp)) {
426 		intel_dp->psr.last_entry_attempt = time_ns;
427 		drm_dbg_kms(display->drm,
428 			    "[transcoder %s] PSR entry attempt in 2 vblanks\n",
429 			    transcoder_name(cpu_transcoder));
430 	}
431 
432 	if (psr_iir & psr_irq_post_exit_bit_get(intel_dp)) {
433 		intel_dp->psr.last_exit = time_ns;
434 		drm_dbg_kms(display->drm,
435 			    "[transcoder %s] PSR exit completed\n",
436 			    transcoder_name(cpu_transcoder));
437 
438 		if (DISPLAY_VER(display) >= 9) {
439 			u32 val;
440 
441 			val = intel_de_rmw(display,
442 					   PSR_EVENT(display, cpu_transcoder),
443 					   0, 0);
444 
445 			psr_event_print(display, val, intel_dp->psr.sel_update_enabled);
446 		}
447 	}
448 
449 	if (psr_iir & psr_irq_psr_error_bit_get(intel_dp)) {
450 		drm_warn(display->drm, "[transcoder %s] PSR aux error\n",
451 			 transcoder_name(cpu_transcoder));
452 
453 		intel_dp->psr.irq_aux_error = true;
454 
455 		/*
456 		 * If this interruption is not masked it will keep
457 		 * interrupting so fast that it prevents the scheduled
458 		 * work to run.
459 		 * Also after a PSR error, we don't want to arm PSR
460 		 * again so we don't care about unmask the interruption
461 		 * or unset irq_aux_error.
462 		 */
463 		intel_de_rmw(display, psr_imr_reg(display, cpu_transcoder),
464 			     0, psr_irq_psr_error_bit_get(intel_dp));
465 
466 		queue_work(display->wq.unordered, &intel_dp->psr.work);
467 	}
468 }
469 
intel_dp_get_sink_sync_latency(struct intel_dp * intel_dp)470 static u8 intel_dp_get_sink_sync_latency(struct intel_dp *intel_dp)
471 {
472 	struct intel_display *display = to_intel_display(intel_dp);
473 	u8 val = 8; /* assume the worst if we can't read the value */
474 
475 	if (drm_dp_dpcd_readb(&intel_dp->aux,
476 			      DP_SYNCHRONIZATION_LATENCY_IN_SINK, &val) == 1)
477 		val &= DP_MAX_RESYNC_FRAME_COUNT_MASK;
478 	else
479 		drm_dbg_kms(display->drm,
480 			    "Unable to get sink synchronization latency, assuming 8 frames\n");
481 	return val;
482 }
483 
_psr_compute_su_granularity(struct intel_dp * intel_dp,struct intel_connector * connector)484 static void _psr_compute_su_granularity(struct intel_dp *intel_dp,
485 					struct intel_connector *connector)
486 {
487 	struct intel_display *display = to_intel_display(intel_dp);
488 	ssize_t r;
489 	__le16 w;
490 	u8 y;
491 
492 	/*
493 	 * If sink don't have specific granularity requirements set legacy
494 	 * ones.
495 	 */
496 	if (!(connector->dp.psr_caps.dpcd[1] & DP_PSR2_SU_GRANULARITY_REQUIRED)) {
497 		/* As PSR2 HW sends full lines, we do not care about x granularity */
498 		w = cpu_to_le16(4);
499 		y = 4;
500 		goto exit;
501 	}
502 
503 	r = drm_dp_dpcd_read(&intel_dp->aux, DP_PSR2_SU_X_GRANULARITY, &w, sizeof(w));
504 	if (r != sizeof(w))
505 		drm_dbg_kms(display->drm,
506 			    "Unable to read selective update x granularity\n");
507 	/*
508 	 * Spec says that if the value read is 0 the default granularity should
509 	 * be used instead.
510 	 */
511 	if (r != sizeof(w) || w == 0)
512 		w = cpu_to_le16(4);
513 
514 	r = drm_dp_dpcd_read(&intel_dp->aux, DP_PSR2_SU_Y_GRANULARITY, &y, 1);
515 	if (r != 1) {
516 		drm_dbg_kms(display->drm,
517 			    "Unable to read selective update y granularity\n");
518 		y = 4;
519 	}
520 	if (y == 0)
521 		y = 1;
522 
523 exit:
524 	connector->dp.psr_caps.su_w_granularity = le16_to_cpu(w);
525 	connector->dp.psr_caps.su_y_granularity = y;
526 }
527 
528 static enum intel_panel_replay_dsc_support
compute_pr_dsc_support(struct intel_connector * connector)529 compute_pr_dsc_support(struct intel_connector *connector)
530 {
531 	u8 pr_dsc_mode;
532 	u8 val;
533 
534 	val = connector->dp.panel_replay_caps.dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_CAPABILITY)];
535 	pr_dsc_mode = REG_FIELD_GET8(DP_PANEL_REPLAY_DSC_DECODE_CAPABILITY_IN_PR_MASK, val);
536 
537 	switch (pr_dsc_mode) {
538 	case DP_DSC_DECODE_CAPABILITY_IN_PR_FULL_FRAME_ONLY:
539 		return INTEL_DP_PANEL_REPLAY_DSC_FULL_FRAME_ONLY;
540 	case DP_DSC_DECODE_CAPABILITY_IN_PR_SUPPORTED:
541 		return INTEL_DP_PANEL_REPLAY_DSC_SELECTIVE_UPDATE;
542 	default:
543 		MISSING_CASE(pr_dsc_mode);
544 		fallthrough;
545 	case DP_DSC_DECODE_CAPABILITY_IN_PR_NOT_SUPPORTED:
546 	case DP_DSC_DECODE_CAPABILITY_IN_PR_RESERVED:
547 		return INTEL_DP_PANEL_REPLAY_DSC_NOT_SUPPORTED;
548 	}
549 }
550 
panel_replay_dsc_support_str(enum intel_panel_replay_dsc_support dsc_support)551 static const char *panel_replay_dsc_support_str(enum intel_panel_replay_dsc_support dsc_support)
552 {
553 	switch (dsc_support) {
554 	case INTEL_DP_PANEL_REPLAY_DSC_NOT_SUPPORTED:
555 		return "not supported";
556 	case INTEL_DP_PANEL_REPLAY_DSC_FULL_FRAME_ONLY:
557 		return "full frame only";
558 	case INTEL_DP_PANEL_REPLAY_DSC_SELECTIVE_UPDATE:
559 		return "selective update";
560 	default:
561 		MISSING_CASE(dsc_support);
562 		return "n/a";
563 	};
564 }
565 
_panel_replay_compute_su_granularity(struct intel_connector * connector)566 static void _panel_replay_compute_su_granularity(struct intel_connector *connector)
567 {
568 	u16 w;
569 	u8 y;
570 
571 	if (!(connector->dp.panel_replay_caps.dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_CAPABILITY)] &
572 	       DP_PANEL_REPLAY_SU_GRANULARITY_REQUIRED)) {
573 		w = 4;
574 		y = 4;
575 		goto exit;
576 	}
577 
578 	/*
579 	 * Spec says that if the value read is 0 the default granularity should
580 	 * be used instead.
581 	 */
582 	w = le16_to_cpu(*(__le16 *)&connector->dp.panel_replay_caps.dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_X_GRANULARITY)]) ? : 4;
583 	y = connector->dp.panel_replay_caps.dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_Y_GRANULARITY)] ? : 1;
584 
585 exit:
586 	connector->dp.panel_replay_caps.su_w_granularity = w;
587 	connector->dp.panel_replay_caps.su_y_granularity = y;
588 }
589 
_panel_replay_init_dpcd(struct intel_dp * intel_dp,struct intel_connector * connector)590 static void _panel_replay_init_dpcd(struct intel_dp *intel_dp, struct intel_connector *connector)
591 {
592 	struct intel_display *display = to_intel_display(intel_dp);
593 	int ret;
594 
595 	/* TODO: Enable Panel Replay on MST once it's properly implemented. */
596 	if (intel_dp->mst_detect == DRM_DP_MST)
597 		return;
598 
599 	if (intel_dp_is_edp(intel_dp) &&
600 	    intel_has_dpcd_quirk(intel_dp, QUIRK_DISABLE_EDP_PANEL_REPLAY)) {
601 		drm_dbg_kms(display->drm,
602 			    "Panel Replay support not currently available for this setup\n");
603 		return;
604 	}
605 
606 	ret = drm_dp_dpcd_read_data(&intel_dp->aux, DP_PANEL_REPLAY_CAP_SUPPORT,
607 				    &connector->dp.panel_replay_caps.dpcd,
608 				    sizeof(connector->dp.panel_replay_caps.dpcd));
609 	if (ret < 0)
610 		return;
611 
612 	if (!(connector->dp.panel_replay_caps.dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_SUPPORT)] &
613 	      DP_PANEL_REPLAY_SUPPORT))
614 		return;
615 
616 	if (intel_dp_is_edp(intel_dp)) {
617 		if (!intel_alpm_aux_less_wake_supported(intel_dp)) {
618 			drm_dbg_kms(display->drm,
619 				    "Panel doesn't support AUX-less ALPM, eDP Panel Replay not possible\n");
620 			return;
621 		}
622 
623 		if (!(connector->dp.panel_replay_caps.dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_SUPPORT)] &
624 		      DP_PANEL_REPLAY_EARLY_TRANSPORT_SUPPORT)) {
625 			drm_dbg_kms(display->drm,
626 				    "Panel doesn't support early transport, eDP Panel Replay not possible\n");
627 			return;
628 		}
629 	}
630 
631 	connector->dp.panel_replay_caps.support = true;
632 	intel_dp->psr.sink_panel_replay_support = true;
633 
634 	if (connector->dp.panel_replay_caps.dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_SUPPORT)] &
635 	    DP_PANEL_REPLAY_SU_SUPPORT) {
636 		connector->dp.panel_replay_caps.su_support = true;
637 
638 		_panel_replay_compute_su_granularity(connector);
639 	}
640 
641 	connector->dp.panel_replay_caps.dsc_support = compute_pr_dsc_support(connector);
642 
643 	drm_dbg_kms(display->drm,
644 		    "Panel replay %sis supported by panel (in DSC mode: %s)\n",
645 		    connector->dp.panel_replay_caps.su_support ?
646 		    "selective_update " : "",
647 		    panel_replay_dsc_support_str(connector->dp.panel_replay_caps.dsc_support));
648 }
649 
_psr_init_dpcd(struct intel_dp * intel_dp,struct intel_connector * connector)650 static void _psr_init_dpcd(struct intel_dp *intel_dp, struct intel_connector *connector)
651 {
652 	struct intel_display *display = to_intel_display(intel_dp);
653 	int ret;
654 
655 	ret = drm_dp_dpcd_read_data(&intel_dp->aux, DP_PSR_SUPPORT, connector->dp.psr_caps.dpcd,
656 				    sizeof(connector->dp.psr_caps.dpcd));
657 	if (ret < 0)
658 		return;
659 
660 	if (!connector->dp.psr_caps.dpcd[0])
661 		return;
662 
663 	drm_dbg_kms(display->drm, "eDP panel supports PSR version %x\n",
664 		    connector->dp.psr_caps.dpcd[0]);
665 
666 	if (drm_dp_has_quirk(&intel_dp->desc, DP_DPCD_QUIRK_NO_PSR)) {
667 		drm_dbg_kms(display->drm,
668 			    "PSR support not currently available for this panel\n");
669 		return;
670 	}
671 
672 	if (!(intel_dp->edp_dpcd[1] & DP_EDP_SET_POWER_CAP)) {
673 		drm_dbg_kms(display->drm,
674 			    "Panel lacks power state control, PSR cannot be enabled\n");
675 		return;
676 	}
677 
678 	connector->dp.psr_caps.support = true;
679 	intel_dp->psr.sink_support = true;
680 
681 	connector->dp.psr_caps.sync_latency = intel_dp_get_sink_sync_latency(intel_dp);
682 
683 	if (intel_has_quirk(display, QUIRK_DISABLE_PSR2))
684 		return;
685 
686 	if (DISPLAY_VER(display) >= 9 &&
687 	    connector->dp.psr_caps.dpcd[0] >= DP_PSR2_WITH_Y_COORD_IS_SUPPORTED) {
688 		bool y_req = connector->dp.psr_caps.dpcd[1] &
689 			     DP_PSR2_SU_Y_COORDINATE_REQUIRED;
690 
691 		/*
692 		 * All panels that supports PSR version 03h (PSR2 +
693 		 * Y-coordinate) can handle Y-coordinates in VSC but we are
694 		 * only sure that it is going to be used when required by the
695 		 * panel. This way panel is capable to do selective update
696 		 * without a aux frame sync.
697 		 *
698 		 * To support PSR version 02h and PSR version 03h without
699 		 * Y-coordinate requirement panels we would need to enable
700 		 * GTC first.
701 		 */
702 		connector->dp.psr_caps.su_support = y_req &&
703 			intel_alpm_aux_wake_supported(intel_dp);
704 		drm_dbg_kms(display->drm, "PSR2 %ssupported\n",
705 			    connector->dp.psr_caps.su_support ? "" : "not ");
706 	}
707 
708 	if (connector->dp.psr_caps.su_support) {
709 		ret = drm_dp_dpcd_read_byte(&intel_dp->aux,
710 					    INTEL_DPCD_INTEL_WA_REGISTER_CAPS,
711 					    &connector->dp.psr_caps.intel_wa_dpcd);
712 		if (ret < 0)
713 			return;
714 		_psr_compute_su_granularity(intel_dp, connector);
715 	}
716 }
717 
intel_psr_init_dpcd(struct intel_dp * intel_dp,struct intel_connector * connector)718 void intel_psr_init_dpcd(struct intel_dp *intel_dp, struct intel_connector *connector)
719 {
720 	_psr_init_dpcd(intel_dp, connector);
721 
722 	_panel_replay_init_dpcd(intel_dp, connector);
723 }
724 
hsw_psr_setup_aux(struct intel_dp * intel_dp)725 static void hsw_psr_setup_aux(struct intel_dp *intel_dp)
726 {
727 	struct intel_display *display = to_intel_display(intel_dp);
728 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
729 	u32 aux_clock_divider, aux_ctl;
730 	/* write DP_SET_POWER=D0 */
731 	static const u8 aux_msg[] = {
732 		[0] = (DP_AUX_NATIVE_WRITE << 4) | ((DP_SET_POWER >> 16) & 0xf),
733 		[1] = (DP_SET_POWER >> 8) & 0xff,
734 		[2] = DP_SET_POWER & 0xff,
735 		[3] = 1 - 1,
736 		[4] = DP_SET_POWER_D0,
737 	};
738 	int i;
739 
740 	BUILD_BUG_ON(sizeof(aux_msg) > 20);
741 	for (i = 0; i < sizeof(aux_msg); i += 4)
742 		intel_de_write(display,
743 			       psr_aux_data_reg(display, cpu_transcoder, i >> 2),
744 			       intel_dp_aux_pack(&aux_msg[i], sizeof(aux_msg) - i));
745 
746 	aux_clock_divider = intel_dp->get_aux_clock_divider(intel_dp, 0);
747 
748 	/* Start with bits set for DDI_AUX_CTL register */
749 	aux_ctl = intel_dp->get_aux_send_ctl(intel_dp, sizeof(aux_msg),
750 					     aux_clock_divider);
751 
752 	/* Select only valid bits for SRD_AUX_CTL */
753 	aux_ctl &= EDP_PSR_AUX_CTL_TIME_OUT_MASK |
754 		EDP_PSR_AUX_CTL_MESSAGE_SIZE_MASK |
755 		EDP_PSR_AUX_CTL_PRECHARGE_2US_MASK |
756 		EDP_PSR_AUX_CTL_BIT_CLOCK_2X_MASK;
757 
758 	intel_de_write(display, psr_aux_ctl_reg(display, cpu_transcoder),
759 		       aux_ctl);
760 }
761 
psr2_su_region_et_valid(struct intel_connector * connector,bool panel_replay)762 static bool psr2_su_region_et_valid(struct intel_connector *connector, bool panel_replay)
763 {
764 	struct intel_dp *intel_dp = intel_attached_dp(connector);
765 	struct intel_display *display = to_intel_display(intel_dp);
766 
767 	if (DISPLAY_VER(display) < 20 || !intel_dp_is_edp(intel_dp) ||
768 	    intel_dp->psr.debug & I915_PSR_DEBUG_SU_REGION_ET_DISABLE)
769 		return false;
770 
771 	return panel_replay ?
772 		connector->dp.panel_replay_caps.dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_SUPPORT)] &
773 		DP_PANEL_REPLAY_EARLY_TRANSPORT_SUPPORT :
774 		connector->dp.psr_caps.dpcd[0] == DP_PSR2_WITH_Y_COORD_ET_SUPPORTED;
775 }
776 
_panel_replay_enable_sink(struct intel_dp * intel_dp,const struct intel_crtc_state * crtc_state)777 static void _panel_replay_enable_sink(struct intel_dp *intel_dp,
778 				      const struct intel_crtc_state *crtc_state)
779 {
780 	u8 panel_replay_config[2];
781 	u8 panel_replay_config_3;
782 
783 	panel_replay_config[0] = DP_PANEL_REPLAY_ENABLE |
784 				 DP_PANEL_REPLAY_VSC_SDP_CRC_EN |
785 				 DP_PANEL_REPLAY_UNRECOVERABLE_ERROR_EN |
786 				 DP_PANEL_REPLAY_RFB_STORAGE_ERROR_EN |
787 				 DP_PANEL_REPLAY_ACTIVE_FRAME_CRC_ERROR_EN;
788 	panel_replay_config[1] = DP_PANEL_REPLAY_CRC_VERIFICATION;
789 	if (crtc_state->has_sel_update)
790 		panel_replay_config[0] |= DP_PANEL_REPLAY_SU_ENABLE;
791 
792 	if (crtc_state->enable_psr2_su_region_et)
793 		panel_replay_config[0] |= DP_PANEL_REPLAY_ENABLE_SU_REGION_ET;
794 
795 	if (crtc_state->req_psr2_sdp_prior_scanline)
796 		panel_replay_config[1] |=
797 			DP_PANEL_REPLAY_SU_REGION_SCANLINE_CAPTURE;
798 
799 	drm_dp_dpcd_write(&intel_dp->aux, PANEL_REPLAY_CONFIG,
800 			  panel_replay_config, sizeof(panel_replay_config));
801 
802 	panel_replay_config_3 = intel_dp_as_sdp_transmission_time();
803 	drm_dp_dpcd_writeb(&intel_dp->aux, PANEL_REPLAY_CONFIG3, panel_replay_config_3);
804 }
805 
_psr_enable_sink(struct intel_dp * intel_dp,const struct intel_crtc_state * crtc_state)806 static void _psr_enable_sink(struct intel_dp *intel_dp,
807 			     const struct intel_crtc_state *crtc_state)
808 {
809 	struct intel_display *display = to_intel_display(intel_dp);
810 	u8 val = 0;
811 
812 	if (crtc_state->has_sel_update) {
813 		val |= DP_PSR_ENABLE_PSR2 | DP_PSR_IRQ_HPD_WITH_CRC_ERRORS;
814 	} else {
815 		if (intel_dp->psr.link_standby)
816 			val |= DP_PSR_MAIN_LINK_ACTIVE;
817 
818 		if (DISPLAY_VER(display) >= 8)
819 			val |= DP_PSR_CRC_VERIFICATION;
820 	}
821 
822 	if (crtc_state->req_psr2_sdp_prior_scanline)
823 		val |= DP_PSR_SU_REGION_SCANLINE_CAPTURE;
824 
825 	if (crtc_state->enable_psr2_su_region_et)
826 		val |= DP_PANEL_REPLAY_ENABLE_SU_REGION_ET;
827 
828 	if (intel_dp->psr.entry_setup_frames > 0)
829 		val |= DP_PSR_FRAME_CAPTURE;
830 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG, val);
831 
832 	val |= DP_PSR_ENABLE;
833 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG, val);
834 }
835 
intel_psr_enable_sink(struct intel_dp * intel_dp,const struct intel_crtc_state * crtc_state)836 static void intel_psr_enable_sink(struct intel_dp *intel_dp,
837 				  const struct intel_crtc_state *crtc_state)
838 {
839 	intel_alpm_enable_sink(intel_dp, crtc_state);
840 
841 	crtc_state->has_panel_replay ?
842 		_panel_replay_enable_sink(intel_dp, crtc_state) :
843 		_psr_enable_sink(intel_dp, crtc_state);
844 
845 	if (intel_dp_is_edp(intel_dp))
846 		drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, DP_SET_POWER_D0);
847 }
848 
intel_psr_panel_replay_enable_sink(struct intel_dp * intel_dp)849 void intel_psr_panel_replay_enable_sink(struct intel_dp *intel_dp)
850 {
851 	/*
852 	 * NOTE: We might want to trigger mode set when
853 	 * disabling/enabling Panel Replay via debugfs interface to
854 	 * ensure this bit is cleared/set accordingly.
855 	 */
856 	if (CAN_PANEL_REPLAY(intel_dp) && panel_replay_global_enabled(intel_dp))
857 		drm_dp_dpcd_writeb(&intel_dp->aux, PANEL_REPLAY_CONFIG,
858 				   DP_PANEL_REPLAY_ENABLE);
859 }
860 
intel_psr1_get_tp_time(struct intel_dp * intel_dp)861 static u32 intel_psr1_get_tp_time(struct intel_dp *intel_dp)
862 {
863 	struct intel_display *display = to_intel_display(intel_dp);
864 	struct intel_connector *connector = intel_dp->attached_connector;
865 	u32 val = 0;
866 
867 	if (DISPLAY_VER(display) >= 11)
868 		val |= EDP_PSR_TP4_TIME_0us;
869 
870 	if (display->params.psr_safest_params) {
871 		val |= EDP_PSR_TP1_TIME_2500us;
872 		val |= EDP_PSR_TP2_TP3_TIME_2500us;
873 		goto check_tp3_sel;
874 	}
875 
876 	if (connector->panel.vbt.psr.tp1_wakeup_time_us == 0)
877 		val |= EDP_PSR_TP1_TIME_0us;
878 	else if (connector->panel.vbt.psr.tp1_wakeup_time_us <= 100)
879 		val |= EDP_PSR_TP1_TIME_100us;
880 	else if (connector->panel.vbt.psr.tp1_wakeup_time_us <= 500)
881 		val |= EDP_PSR_TP1_TIME_500us;
882 	else
883 		val |= EDP_PSR_TP1_TIME_2500us;
884 
885 	if (connector->panel.vbt.psr.tp2_tp3_wakeup_time_us == 0)
886 		val |= EDP_PSR_TP2_TP3_TIME_0us;
887 	else if (connector->panel.vbt.psr.tp2_tp3_wakeup_time_us <= 100)
888 		val |= EDP_PSR_TP2_TP3_TIME_100us;
889 	else if (connector->panel.vbt.psr.tp2_tp3_wakeup_time_us <= 500)
890 		val |= EDP_PSR_TP2_TP3_TIME_500us;
891 	else
892 		val |= EDP_PSR_TP2_TP3_TIME_2500us;
893 
894 	/*
895 	 * WA 0479: hsw,bdw
896 	 * "Do not skip both TP1 and TP2/TP3"
897 	 */
898 	if (DISPLAY_VER(display) < 9 &&
899 	    connector->panel.vbt.psr.tp1_wakeup_time_us == 0 &&
900 	    connector->panel.vbt.psr.tp2_tp3_wakeup_time_us == 0)
901 		val |= EDP_PSR_TP2_TP3_TIME_100us;
902 
903 check_tp3_sel:
904 	if (intel_dp_source_supports_tps3(display) &&
905 	    drm_dp_tps3_supported(intel_dp->dpcd))
906 		val |= EDP_PSR_TP_TP1_TP3;
907 	else
908 		val |= EDP_PSR_TP_TP1_TP2;
909 
910 	return val;
911 }
912 
psr_compute_idle_frames(struct intel_dp * intel_dp)913 static u8 psr_compute_idle_frames(struct intel_dp *intel_dp)
914 {
915 	struct intel_display *display = to_intel_display(intel_dp);
916 	struct intel_connector *connector = intel_dp->attached_connector;
917 	int idle_frames;
918 
919 	/* Let's use 6 as the minimum to cover all known cases including the
920 	 * off-by-one issue that HW has in some cases.
921 	 */
922 	idle_frames = max(6, connector->panel.vbt.psr.idle_frames);
923 	idle_frames = max(idle_frames, connector->dp.psr_caps.sync_latency + 1);
924 
925 	if (drm_WARN_ON(display->drm, idle_frames > 0xf))
926 		idle_frames = 0xf;
927 
928 	return idle_frames;
929 }
930 
is_dc5_dc6_blocked(struct intel_dp * intel_dp)931 static bool is_dc5_dc6_blocked(struct intel_dp *intel_dp)
932 {
933 	struct intel_display *display = to_intel_display(intel_dp);
934 	u32 current_dc_state = intel_display_power_get_current_dc_state(display);
935 	struct intel_crtc *crtc = intel_crtc_for_pipe(display, intel_dp->psr.pipe);
936 	struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(&crtc->base);
937 
938 	return (current_dc_state != DC_STATE_EN_UPTO_DC5 &&
939 		current_dc_state != DC_STATE_EN_UPTO_DC6) ||
940 		intel_dp->psr.active_non_psr_pipes ||
941 		READ_ONCE(vblank->enabled);
942 }
943 
hsw_activate_psr1(struct intel_dp * intel_dp)944 static void hsw_activate_psr1(struct intel_dp *intel_dp)
945 {
946 	struct intel_display *display = to_intel_display(intel_dp);
947 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
948 	u32 max_sleep_time = 0x1f;
949 	u32 val = EDP_PSR_ENABLE;
950 
951 	val |= EDP_PSR_IDLE_FRAMES(psr_compute_idle_frames(intel_dp));
952 
953 	if (DISPLAY_VER(display) < 20)
954 		val |= EDP_PSR_MAX_SLEEP_TIME(max_sleep_time);
955 
956 	if (display->platform.haswell)
957 		val |= EDP_PSR_MIN_LINK_ENTRY_TIME_8_LINES;
958 
959 	if (intel_dp->psr.link_standby)
960 		val |= EDP_PSR_LINK_STANDBY;
961 
962 	val |= intel_psr1_get_tp_time(intel_dp);
963 
964 	if (DISPLAY_VER(display) >= 8)
965 		val |= EDP_PSR_CRC_ENABLE;
966 
967 	if (DISPLAY_VER(display) >= 20)
968 		val |= LNL_EDP_PSR_ENTRY_SETUP_FRAMES(intel_dp->psr.entry_setup_frames);
969 
970 	intel_de_rmw(display, psr_ctl_reg(display, cpu_transcoder),
971 		     ~EDP_PSR_RESTORE_PSR_ACTIVE_CTX_MASK, val);
972 
973 	/* Wa_16025596647 */
974 	if ((DISPLAY_VER(display) == 20 ||
975 	     IS_DISPLAY_VERx100_STEP(display, 3000, STEP_A0, STEP_B0)) &&
976 	    is_dc5_dc6_blocked(intel_dp) && intel_dp->psr.pkg_c_latency_used)
977 		intel_dmc_start_pkgc_exit_at_start_of_undelayed_vblank(display,
978 								       intel_dp->psr.pipe,
979 								       true);
980 }
981 
intel_psr2_get_tp_time(struct intel_dp * intel_dp)982 static u32 intel_psr2_get_tp_time(struct intel_dp *intel_dp)
983 {
984 	struct intel_display *display = to_intel_display(intel_dp);
985 	struct intel_connector *connector = intel_dp->attached_connector;
986 	u32 val = 0;
987 
988 	if (display->params.psr_safest_params)
989 		return EDP_PSR2_TP2_TIME_2500us;
990 
991 	if (connector->panel.vbt.psr.psr2_tp2_tp3_wakeup_time_us >= 0 &&
992 	    connector->panel.vbt.psr.psr2_tp2_tp3_wakeup_time_us <= 50)
993 		val |= EDP_PSR2_TP2_TIME_50us;
994 	else if (connector->panel.vbt.psr.psr2_tp2_tp3_wakeup_time_us <= 100)
995 		val |= EDP_PSR2_TP2_TIME_100us;
996 	else if (connector->panel.vbt.psr.psr2_tp2_tp3_wakeup_time_us <= 500)
997 		val |= EDP_PSR2_TP2_TIME_500us;
998 	else
999 		val |= EDP_PSR2_TP2_TIME_2500us;
1000 
1001 	return val;
1002 }
1003 
1004 static int
psr2_block_count_lines(u8 io_wake_lines,u8 fast_wake_lines)1005 psr2_block_count_lines(u8 io_wake_lines, u8 fast_wake_lines)
1006 {
1007 	return io_wake_lines < 9 && fast_wake_lines < 9 ? 8 : 12;
1008 }
1009 
psr2_block_count(struct intel_dp * intel_dp)1010 static int psr2_block_count(struct intel_dp *intel_dp)
1011 {
1012 	return psr2_block_count_lines(intel_dp->psr.io_wake_lines,
1013 				      intel_dp->psr.fast_wake_lines) / 4;
1014 }
1015 
frames_before_su_entry(struct intel_dp * intel_dp)1016 static u8 frames_before_su_entry(struct intel_dp *intel_dp)
1017 {
1018 	struct intel_connector *connector = intel_dp->attached_connector;
1019 	u8 frames_before_su_entry;
1020 
1021 	frames_before_su_entry = max_t(u8,
1022 				       connector->dp.psr_caps.sync_latency + 1,
1023 				       2);
1024 
1025 	/* Entry setup frames must be at least 1 less than frames before SU entry */
1026 	if (intel_dp->psr.entry_setup_frames >= frames_before_su_entry)
1027 		frames_before_su_entry = intel_dp->psr.entry_setup_frames + 1;
1028 
1029 	return frames_before_su_entry;
1030 }
1031 
intel_psr_allow_pr_bw_optimization(struct intel_dp * intel_dp)1032 static bool intel_psr_allow_pr_bw_optimization(struct intel_dp *intel_dp)
1033 {
1034 	if (intel_dp_is_edp(intel_dp))
1035 		return false;
1036 
1037 	if (!intel_dp_tunnel_bw_alloc_is_enabled(intel_dp))
1038 		return false;
1039 
1040 	if (!intel_dp_tunnel_pr_optimization_supported(intel_dp))
1041 		return false;
1042 
1043 	return true;
1044 }
1045 
dg2_activate_panel_replay(struct intel_dp * intel_dp)1046 static void dg2_activate_panel_replay(struct intel_dp *intel_dp)
1047 {
1048 	struct intel_display *display = to_intel_display(intel_dp);
1049 	struct intel_psr *psr = &intel_dp->psr;
1050 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
1051 	u32 dp2_ctl_set = TRANS_DP2_PANEL_REPLAY_ENABLE;
1052 	u32 dp2_ctl_clear = 0;
1053 
1054 	if (intel_dp_is_edp(intel_dp) && psr->sel_update_enabled) {
1055 		u32 val = psr->su_region_et_enabled ?
1056 			LNL_EDP_PSR2_SU_REGION_ET_ENABLE : 0;
1057 
1058 		if (intel_dp->psr.req_psr2_sdp_prior_scanline)
1059 			val |= EDP_PSR2_SU_SDP_SCANLINE;
1060 
1061 		intel_de_write(display, EDP_PSR2_CTL(display, cpu_transcoder),
1062 			       val);
1063 	}
1064 
1065 	if (intel_psr_allow_pr_bw_optimization(intel_dp))
1066 		dp2_ctl_set |= TRANS_DP2_PR_TUNNELING_ENABLE;
1067 	else
1068 		dp2_ctl_clear = TRANS_DP2_PR_TUNNELING_ENABLE;
1069 
1070 	intel_de_rmw(display,
1071 		     PSR2_MAN_TRK_CTL(display, intel_dp->psr.transcoder),
1072 		     0, ADLP_PSR2_MAN_TRK_CTL_SF_CONTINUOS_FULL_FRAME);
1073 
1074 	intel_de_rmw(display, TRANS_DP2_CTL(intel_dp->psr.transcoder), dp2_ctl_clear, dp2_ctl_set);
1075 }
1076 
hsw_activate_psr2(struct intel_dp * intel_dp)1077 static void hsw_activate_psr2(struct intel_dp *intel_dp)
1078 {
1079 	struct intel_display *display = to_intel_display(intel_dp);
1080 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
1081 	u32 val = EDP_PSR2_ENABLE;
1082 	u32 psr_val = 0;
1083 	u8 idle_frames;
1084 
1085 	/* DC3CO / Wa_16025596647 */
1086 	if (intel_dp->psr.dc3co_allowed ||
1087 	    ((DISPLAY_VER(display) == 20 ||
1088 	      IS_DISPLAY_VERx100_STEP(display, 3000, STEP_A0, STEP_B0)) &&
1089 	     is_dc5_dc6_blocked(intel_dp) && intel_dp->psr.pkg_c_latency_used))
1090 		idle_frames = 0;
1091 	else
1092 		idle_frames = psr_compute_idle_frames(intel_dp);
1093 	val |= EDP_PSR2_IDLE_FRAMES(idle_frames);
1094 
1095 	if (DISPLAY_VER(display) < 14 && !display->platform.alderlake_p)
1096 		val |= EDP_SU_TRACK_ENABLE;
1097 
1098 	if (DISPLAY_VER(display) >= 10 && DISPLAY_VER(display) < 13)
1099 		val |= EDP_Y_COORDINATE_ENABLE;
1100 
1101 	val |= EDP_PSR2_FRAME_BEFORE_SU(frames_before_su_entry(intel_dp));
1102 
1103 	val |= intel_psr2_get_tp_time(intel_dp);
1104 
1105 	if (DISPLAY_VER(display) >= 12 && DISPLAY_VER(display) < 20) {
1106 		if (psr2_block_count(intel_dp) > 2)
1107 			val |= TGL_EDP_PSR2_BLOCK_COUNT_NUM_3;
1108 		else
1109 			val |= TGL_EDP_PSR2_BLOCK_COUNT_NUM_2;
1110 	}
1111 
1112 	/* Wa_22012278275:adl-p */
1113 	if (intel_display_wa(display, INTEL_DISPLAY_WA_22012278275)) {
1114 		static const u8 map[] = {
1115 			2, /* 5 lines */
1116 			1, /* 6 lines */
1117 			0, /* 7 lines */
1118 			3, /* 8 lines */
1119 			6, /* 9 lines */
1120 			5, /* 10 lines */
1121 			4, /* 11 lines */
1122 			7, /* 12 lines */
1123 		};
1124 		/*
1125 		 * Still using the default IO_BUFFER_WAKE and FAST_WAKE, see
1126 		 * comments below for more information
1127 		 */
1128 		int tmp;
1129 
1130 		tmp = map[intel_dp->psr.io_wake_lines -
1131 			  TGL_EDP_PSR2_IO_BUFFER_WAKE_MIN_LINES];
1132 		val |= TGL_EDP_PSR2_IO_BUFFER_WAKE(tmp + TGL_EDP_PSR2_IO_BUFFER_WAKE_MIN_LINES);
1133 
1134 		tmp = map[intel_dp->psr.fast_wake_lines - TGL_EDP_PSR2_FAST_WAKE_MIN_LINES];
1135 		val |= TGL_EDP_PSR2_FAST_WAKE(tmp + TGL_EDP_PSR2_FAST_WAKE_MIN_LINES);
1136 	} else if (DISPLAY_VER(display) >= 20) {
1137 		val |= LNL_EDP_PSR2_IO_BUFFER_WAKE(intel_dp->psr.io_wake_lines);
1138 	} else if (DISPLAY_VER(display) >= 12) {
1139 		val |= TGL_EDP_PSR2_IO_BUFFER_WAKE(intel_dp->psr.io_wake_lines);
1140 		val |= TGL_EDP_PSR2_FAST_WAKE(intel_dp->psr.fast_wake_lines);
1141 	} else if (DISPLAY_VER(display) >= 9) {
1142 		val |= EDP_PSR2_IO_BUFFER_WAKE(intel_dp->psr.io_wake_lines);
1143 		val |= EDP_PSR2_FAST_WAKE(intel_dp->psr.fast_wake_lines);
1144 	}
1145 
1146 	if (intel_dp->psr.req_psr2_sdp_prior_scanline)
1147 		val |= EDP_PSR2_SU_SDP_SCANLINE;
1148 
1149 	if (DISPLAY_VER(display) >= 20)
1150 		psr_val |= LNL_EDP_PSR_ENTRY_SETUP_FRAMES(intel_dp->psr.entry_setup_frames);
1151 
1152 	if (intel_dp->psr.psr2_sel_fetch_enabled) {
1153 		u32 tmp;
1154 
1155 		tmp = intel_de_read(display,
1156 				    PSR2_MAN_TRK_CTL(display, cpu_transcoder));
1157 		drm_WARN_ON(display->drm, !(tmp & PSR2_MAN_TRK_CTL_ENABLE));
1158 	} else if (HAS_PSR2_SEL_FETCH(display)) {
1159 		intel_de_write(display,
1160 			       PSR2_MAN_TRK_CTL(display, cpu_transcoder), 0);
1161 	}
1162 
1163 	if (intel_dp->psr.su_region_et_enabled)
1164 		val |= LNL_EDP_PSR2_SU_REGION_ET_ENABLE;
1165 
1166 	/*
1167 	 * PSR2 HW is incorrectly using EDP_PSR_TP1_TP3_SEL and BSpec is
1168 	 * recommending keep this bit unset while PSR2 is enabled.
1169 	 */
1170 	intel_de_write(display, psr_ctl_reg(display, cpu_transcoder), psr_val);
1171 
1172 	intel_de_write(display, EDP_PSR2_CTL(display, cpu_transcoder), val);
1173 }
1174 
1175 static bool
transcoder_has_psr2(struct intel_display * display,enum transcoder cpu_transcoder)1176 transcoder_has_psr2(struct intel_display *display, enum transcoder cpu_transcoder)
1177 {
1178 	if (display->platform.alderlake_p || DISPLAY_VER(display) >= 14)
1179 		return cpu_transcoder == TRANSCODER_A || cpu_transcoder == TRANSCODER_B;
1180 	else if (DISPLAY_VER(display) >= 12)
1181 		return cpu_transcoder == TRANSCODER_A;
1182 	else if (DISPLAY_VER(display) >= 9)
1183 		return cpu_transcoder == TRANSCODER_EDP;
1184 	else
1185 		return false;
1186 }
1187 
intel_get_frame_time_us(const struct intel_crtc_state * crtc_state)1188 static u32 intel_get_frame_time_us(const struct intel_crtc_state *crtc_state)
1189 {
1190 	if (!crtc_state->hw.active)
1191 		return 0;
1192 
1193 	return DIV_ROUND_UP(1000 * 1000,
1194 			    drm_mode_vrefresh(&crtc_state->hw.adjusted_mode));
1195 }
1196 
psr2_program_idle_frames(struct intel_dp * intel_dp,u32 idle_frames)1197 static void psr2_program_idle_frames(struct intel_dp *intel_dp,
1198 				     u32 idle_frames)
1199 {
1200 	struct intel_display *display = to_intel_display(intel_dp);
1201 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
1202 
1203 	intel_de_rmw(display, EDP_PSR2_CTL(display, cpu_transcoder),
1204 		     EDP_PSR2_IDLE_FRAMES_MASK,
1205 		     EDP_PSR2_IDLE_FRAMES(idle_frames));
1206 }
1207 
intel_psr2_sel_fetch_config_valid(struct intel_dp * intel_dp,struct intel_crtc_state * crtc_state)1208 static bool intel_psr2_sel_fetch_config_valid(struct intel_dp *intel_dp,
1209 					      struct intel_crtc_state *crtc_state)
1210 {
1211 	struct intel_display *display = to_intel_display(intel_dp);
1212 
1213 	if (!display->params.enable_psr2_sel_fetch &&
1214 	    intel_dp->psr.debug != I915_PSR_DEBUG_ENABLE_SEL_FETCH) {
1215 		drm_dbg_kms(display->drm,
1216 			    "PSR2 sel fetch not enabled, disabled by parameter\n");
1217 		return false;
1218 	}
1219 
1220 	return crtc_state->enable_psr2_sel_fetch = true;
1221 }
1222 
psr2_granularity_check(struct intel_crtc_state * crtc_state,struct intel_connector * connector)1223 static bool psr2_granularity_check(struct intel_crtc_state *crtc_state,
1224 				   struct intel_connector *connector)
1225 {
1226 	struct intel_dp *intel_dp = intel_attached_dp(connector);
1227 	struct intel_display *display = to_intel_display(intel_dp);
1228 	const struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
1229 	const int crtc_hdisplay = crtc_state->hw.adjusted_mode.crtc_hdisplay;
1230 	const int crtc_vdisplay = crtc_state->hw.adjusted_mode.crtc_vdisplay;
1231 	u16 y_granularity = 0;
1232 	u16 sink_y_granularity = crtc_state->has_panel_replay ?
1233 		connector->dp.panel_replay_caps.su_y_granularity :
1234 		connector->dp.psr_caps.su_y_granularity;
1235 	u16 sink_w_granularity;
1236 
1237 	if (crtc_state->has_panel_replay)
1238 		sink_w_granularity = connector->dp.panel_replay_caps.su_w_granularity ==
1239 			DP_PANEL_REPLAY_FULL_LINE_GRANULARITY ?
1240 			crtc_hdisplay : connector->dp.panel_replay_caps.su_w_granularity;
1241 	else
1242 		sink_w_granularity = connector->dp.psr_caps.su_w_granularity;
1243 
1244 	/* PSR2 HW only send full lines so we only need to validate the width */
1245 	if (crtc_hdisplay % sink_w_granularity)
1246 		return false;
1247 
1248 	if (crtc_vdisplay % sink_y_granularity)
1249 		return false;
1250 
1251 	/* HW tracking is only aligned to 4 lines */
1252 	if (!crtc_state->enable_psr2_sel_fetch)
1253 		return sink_y_granularity == 4;
1254 
1255 	/*
1256 	 * adl_p and mtl platforms have 1 line granularity.
1257 	 * For other platforms with SW tracking we can adjust the y coordinates
1258 	 * to match sink requirement if multiple of 4.
1259 	 */
1260 	if (display->platform.alderlake_p || DISPLAY_VER(display) >= 14)
1261 		y_granularity = sink_y_granularity;
1262 	else if (sink_y_granularity <= 2)
1263 		y_granularity = 4;
1264 	else if ((sink_y_granularity % 4) == 0)
1265 		y_granularity = sink_y_granularity;
1266 
1267 	if (y_granularity == 0 || crtc_vdisplay % y_granularity)
1268 		return false;
1269 
1270 	if (crtc_state->dsc.compression_enable &&
1271 	    vdsc_cfg->slice_height % y_granularity)
1272 		return false;
1273 
1274 	crtc_state->su_y_granularity = y_granularity;
1275 	return true;
1276 }
1277 
apply_scanline_indication_wa(struct intel_crtc_state * crtc_state,struct intel_connector * connector)1278 static bool apply_scanline_indication_wa(struct intel_crtc_state *crtc_state,
1279 					 struct intel_connector *connector)
1280 {
1281 	struct intel_dp *intel_dp = intel_attached_dp(connector);
1282 	u8 early_scanline_support = connector->dp.psr_caps.intel_wa_dpcd &
1283 		INTEL_DPCD_INTEL_WA_REGISTER_CAPS_PSR2_EARLYSCANLINE_SDP_SUPPORT_MASK;
1284 
1285 	if (intel_dp->edp_dpcd[0] >= DP_EDP_15)
1286 		return true;
1287 
1288 	switch (early_scanline_support)	{
1289 	case INTEL_DPCD_INTEL_WA_REGISTER_CAPS_FALL_BACK_TO_PSR1:
1290 		crtc_state->req_psr2_sdp_prior_scanline = false;
1291 		return false;
1292 	case INTEL_DPCD_INTEL_WA_REGISTER_CAPS_PSR2_WITH_EARLY_SCANLINE:
1293 		return true;
1294 	case INTEL_DPCD_INTEL_WA_REGISTER_CAPS_PSR2_WITHOUT_EARLY_SCANLINE:
1295 		crtc_state->req_psr2_sdp_prior_scanline = false;
1296 		return true;
1297 	default:
1298 		MISSING_CASE(early_scanline_support);
1299 		return false;
1300 	}
1301 }
1302 
_compute_psr2_sdp_prior_scanline_indication(struct intel_crtc_state * crtc_state,struct intel_connector * connector)1303 static bool _compute_psr2_sdp_prior_scanline_indication(struct intel_crtc_state *crtc_state,
1304 							struct intel_connector *connector)
1305 {
1306 	struct intel_dp *intel_dp = intel_attached_dp(connector);
1307 	struct intel_display *display = to_intel_display(intel_dp);
1308 	const struct drm_display_mode *adjusted_mode = &crtc_state->uapi.adjusted_mode;
1309 	u32 hblank_total, hblank_ns, req_ns;
1310 
1311 	hblank_total = adjusted_mode->crtc_hblank_end - adjusted_mode->crtc_hblank_start;
1312 	hblank_ns = div_u64(1000000ULL * hblank_total, adjusted_mode->crtc_clock);
1313 
1314 	/* From spec: ((60 / number of lanes) + 11) * 1000 / symbol clock frequency MHz */
1315 	req_ns = ((60 / crtc_state->lane_count) + 11) * 1000 / (crtc_state->port_clock / 1000);
1316 
1317 	if ((hblank_ns - req_ns) > 100)
1318 		return true;
1319 
1320 	/* Not supported <13 / Wa_22012279113:adl-p */
1321 	if (DISPLAY_VER(display) < 14 || intel_dp->edp_dpcd[0] < DP_EDP_14b)
1322 		return false;
1323 
1324 	crtc_state->req_psr2_sdp_prior_scanline = true;
1325 
1326 	return apply_scanline_indication_wa(crtc_state, connector);
1327 }
1328 
intel_psr_entry_setup_frames(struct intel_dp * intel_dp,struct drm_connector_state * conn_state,const struct drm_display_mode * adjusted_mode)1329 static int intel_psr_entry_setup_frames(struct intel_dp *intel_dp,
1330 					struct drm_connector_state *conn_state,
1331 					const struct drm_display_mode *adjusted_mode)
1332 {
1333 	struct intel_display *display = to_intel_display(intel_dp);
1334 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1335 	int psr_setup_time = drm_dp_psr_setup_time(connector->dp.psr_caps.dpcd);
1336 	int entry_setup_frames = 0;
1337 
1338 	if (psr_setup_time < 0) {
1339 		drm_dbg_kms(display->drm,
1340 			    "PSR condition failed: Invalid PSR setup time (0x%02x)\n",
1341 			    connector->dp.psr_caps.dpcd[1]);
1342 		return -ETIME;
1343 	}
1344 
1345 	if (intel_usecs_to_scanlines(adjusted_mode, psr_setup_time) >
1346 	    adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vdisplay - 1) {
1347 		if (DISPLAY_VER(display) >= 20) {
1348 			/* setup entry frames can be up to 3 frames */
1349 			entry_setup_frames = 1;
1350 			drm_dbg_kms(display->drm,
1351 				    "PSR setup entry frames %d\n",
1352 				    entry_setup_frames);
1353 		} else {
1354 			drm_dbg_kms(display->drm,
1355 				    "PSR condition failed: PSR setup time (%d us) too long\n",
1356 				    psr_setup_time);
1357 			return -ETIME;
1358 		}
1359 	}
1360 
1361 	return entry_setup_frames;
1362 }
1363 
1364 static
_intel_psr_min_set_context_latency(const struct intel_crtc_state * crtc_state,bool needs_panel_replay,bool needs_sel_update)1365 int _intel_psr_min_set_context_latency(const struct intel_crtc_state *crtc_state,
1366 				       bool needs_panel_replay,
1367 				       bool needs_sel_update)
1368 {
1369 	struct intel_display *display = to_intel_display(crtc_state);
1370 
1371 	if (!crtc_state->has_psr)
1372 		return 0;
1373 
1374 	/* Wa_14015401596 */
1375 	if (intel_vrr_possible(crtc_state) && IS_DISPLAY_VER(display, 13, 14))
1376 		return 1;
1377 
1378 	/* Rest is for SRD_STATUS needed on LunarLake and onwards */
1379 	if (DISPLAY_VER(display) < 20)
1380 		return 0;
1381 
1382 	/*
1383 	 * Comment on SRD_STATUS register in Bspec for LunarLake and onwards:
1384 	 *
1385 	 * To deterministically capture the transition of the state machine
1386 	 * going from SRDOFFACK to IDLE, the delayed V. Blank should be at least
1387 	 * one line after the non-delayed V. Blank.
1388 	 *
1389 	 * Legacy TG: TRANS_SET_CONTEXT_LATENCY > 0
1390 	 * VRR TG: TRANS_VRR_CTL[ VRR Guardband ] < (TRANS_VRR_VMAX[ VRR Vmax ]
1391 	 * - TRANS_VTOTAL[ Vertical Active ])
1392 	 *
1393 	 * SRD_STATUS is used only by PSR1 on PantherLake.
1394 	 * SRD_STATUS is used by PSR1 and Panel Replay DP on LunarLake.
1395 	 */
1396 
1397 	if (needs_sel_update)
1398 		return 0;
1399 
1400 	if (DISPLAY_VER(display) < 30 &&
1401 	    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP))
1402 		return 0;
1403 
1404 	if (DISPLAY_VER(display) >= 30 &&
1405 	    needs_panel_replay)
1406 		return 0;
1407 
1408 	return 1;
1409 }
1410 
_wake_lines_fit_into_vblank(const struct intel_crtc_state * crtc_state,int vblank,int wake_lines)1411 static bool _wake_lines_fit_into_vblank(const struct intel_crtc_state *crtc_state,
1412 					int vblank,
1413 					int wake_lines)
1414 {
1415 	if (crtc_state->req_psr2_sdp_prior_scanline)
1416 		vblank -= 1;
1417 
1418 	/* Vblank >= PSR2_CTL Block Count Number maximum line count */
1419 	if (vblank < wake_lines)
1420 		return false;
1421 
1422 	return true;
1423 }
1424 
wake_lines_fit_into_vblank(struct intel_dp * intel_dp,const struct intel_crtc_state * crtc_state,bool aux_less,bool needs_panel_replay,bool needs_sel_update)1425 static bool wake_lines_fit_into_vblank(struct intel_dp *intel_dp,
1426 				       const struct intel_crtc_state *crtc_state,
1427 				       bool aux_less,
1428 				       bool needs_panel_replay,
1429 				       bool needs_sel_update)
1430 {
1431 	struct intel_display *display = to_intel_display(intel_dp);
1432 	int vblank = crtc_state->hw.adjusted_mode.crtc_vblank_end -
1433 		crtc_state->hw.adjusted_mode.crtc_vblank_start;
1434 	int wake_lines;
1435 	int scl = _intel_psr_min_set_context_latency(crtc_state,
1436 						     needs_panel_replay,
1437 						     needs_sel_update);
1438 	vblank -= scl;
1439 
1440 	if (aux_less)
1441 		wake_lines = crtc_state->alpm_state.aux_less_wake_lines;
1442 	else
1443 		wake_lines = DISPLAY_VER(display) < 20 ?
1444 			psr2_block_count_lines(crtc_state->alpm_state.io_wake_lines,
1445 					       crtc_state->alpm_state.fast_wake_lines) :
1446 			crtc_state->alpm_state.io_wake_lines;
1447 
1448 	/*
1449 	 * Guardband has not been computed yet, so we conservatively check if the
1450 	 * full vblank duration is sufficient to accommodate wake line requirements
1451 	 * for PSR features like Panel Replay and Selective Update.
1452 	 *
1453 	 * Once the actual guardband is available, a more accurate validation is
1454 	 * performed in intel_psr_compute_config_late(), and PSR features are
1455 	 * disabled if wake lines exceed the available guardband.
1456 	 */
1457 	return _wake_lines_fit_into_vblank(crtc_state, vblank, wake_lines);
1458 }
1459 
alpm_config_valid(struct intel_dp * intel_dp,struct intel_crtc_state * crtc_state,bool aux_less,bool needs_panel_replay,bool needs_sel_update)1460 static bool alpm_config_valid(struct intel_dp *intel_dp,
1461 			      struct intel_crtc_state *crtc_state,
1462 			      bool aux_less,
1463 			      bool needs_panel_replay,
1464 			      bool needs_sel_update)
1465 {
1466 	struct intel_display *display = to_intel_display(intel_dp);
1467 
1468 	if (!intel_alpm_compute_params(intel_dp, crtc_state)) {
1469 		drm_dbg_kms(display->drm,
1470 			    "PSR2/Panel Replay  not enabled, Unable to use long enough wake times\n");
1471 		return false;
1472 	}
1473 
1474 	if (!wake_lines_fit_into_vblank(intel_dp, crtc_state, aux_less,
1475 					needs_panel_replay, needs_sel_update)) {
1476 		drm_dbg_kms(display->drm,
1477 			    "PSR2/Panel Replay not enabled, too short vblank time\n");
1478 		return false;
1479 	}
1480 
1481 	return true;
1482 }
1483 
intel_psr2_config_valid(struct intel_dp * intel_dp,struct intel_crtc_state * crtc_state,struct drm_connector_state * conn_state)1484 static bool intel_psr2_config_valid(struct intel_dp *intel_dp,
1485 				    struct intel_crtc_state *crtc_state,
1486 				    struct drm_connector_state *conn_state)
1487 {
1488 	struct intel_display *display = to_intel_display(intel_dp);
1489 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1490 	int crtc_hdisplay = crtc_state->hw.adjusted_mode.crtc_hdisplay;
1491 	int crtc_vdisplay = crtc_state->hw.adjusted_mode.crtc_vdisplay;
1492 	int psr_max_h = 0, psr_max_v = 0, max_bpp = 0;
1493 
1494 	if (!connector->dp.psr_caps.su_support || display->params.enable_psr == 1)
1495 		return false;
1496 
1497 	/* JSL and EHL only supports eDP 1.3 */
1498 	if (display->platform.jasperlake || display->platform.elkhartlake) {
1499 		drm_dbg_kms(display->drm, "PSR2 not supported by phy\n");
1500 		return false;
1501 	}
1502 
1503 	/* Wa_16011181250 */
1504 	if (intel_display_wa(display, INTEL_DISPLAY_WA_16011181250)) {
1505 		drm_dbg_kms(display->drm,
1506 			    "PSR2 is defeatured for this platform\n");
1507 		return false;
1508 	}
1509 
1510 	if (display->platform.alderlake_p && IS_DISPLAY_STEP(display, STEP_A0, STEP_B0)) {
1511 		drm_dbg_kms(display->drm,
1512 			    "PSR2 not completely functional in this stepping\n");
1513 		return false;
1514 	}
1515 
1516 	if (!transcoder_has_psr2(display, crtc_state->cpu_transcoder)) {
1517 		drm_dbg_kms(display->drm,
1518 			    "PSR2 not supported in transcoder %s\n",
1519 			    transcoder_name(crtc_state->cpu_transcoder));
1520 		return false;
1521 	}
1522 
1523 	/*
1524 	 * DSC and PSR2 cannot be enabled simultaneously. If a requested
1525 	 * resolution requires DSC to be enabled, priority is given to DSC
1526 	 * over PSR2.
1527 	 */
1528 	if (crtc_state->dsc.compression_enable &&
1529 	    (DISPLAY_VER(display) < 14 && !display->platform.alderlake_p)) {
1530 		drm_dbg_kms(display->drm,
1531 			    "PSR2 cannot be enabled since DSC is enabled\n");
1532 		return false;
1533 	}
1534 
1535 	if (DISPLAY_VER(display) >= 20) {
1536 		psr_max_h = crtc_hdisplay;
1537 		psr_max_v = crtc_vdisplay;
1538 		max_bpp = crtc_state->pipe_bpp;
1539 	} else if (IS_DISPLAY_VER(display, 12, 14)) {
1540 		psr_max_h = 5120;
1541 		psr_max_v = 3200;
1542 		max_bpp = 30;
1543 	} else if (IS_DISPLAY_VER(display, 10, 11)) {
1544 		psr_max_h = 4096;
1545 		psr_max_v = 2304;
1546 		max_bpp = 24;
1547 	} else if (DISPLAY_VER(display) == 9) {
1548 		psr_max_h = 3640;
1549 		psr_max_v = 2304;
1550 		max_bpp = 24;
1551 	}
1552 
1553 	if (crtc_state->pipe_bpp > max_bpp) {
1554 		drm_dbg_kms(display->drm,
1555 			    "PSR2 not enabled, pipe bpp %d > max supported %d\n",
1556 			    crtc_state->pipe_bpp, max_bpp);
1557 		return false;
1558 	}
1559 
1560 	/* Wa_16011303918:adl-p */
1561 	if (crtc_state->vrr.enable &&
1562 	    display->platform.alderlake_p && IS_DISPLAY_STEP(display, STEP_A0, STEP_B0)) {
1563 		drm_dbg_kms(display->drm,
1564 			    "PSR2 not enabled, not compatible with HW stepping + VRR\n");
1565 		return false;
1566 	}
1567 
1568 	if (!alpm_config_valid(intel_dp, crtc_state, false, false, true))
1569 		return false;
1570 
1571 	if (!crtc_state->enable_psr2_sel_fetch &&
1572 	    (crtc_hdisplay > psr_max_h || crtc_vdisplay > psr_max_v)) {
1573 		drm_dbg_kms(display->drm,
1574 			    "PSR2 not enabled, resolution %dx%d > max supported %dx%d\n",
1575 			    crtc_hdisplay, crtc_vdisplay,
1576 			    psr_max_h, psr_max_v);
1577 		return false;
1578 	}
1579 
1580 	return true;
1581 }
1582 
intel_sel_update_config_valid(struct intel_crtc_state * crtc_state,struct drm_connector_state * conn_state)1583 static bool intel_sel_update_config_valid(struct intel_crtc_state *crtc_state,
1584 					  struct drm_connector_state *conn_state)
1585 {
1586 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1587 	struct intel_dp *intel_dp = intel_attached_dp(connector);
1588 	struct intel_display *display = to_intel_display(intel_dp);
1589 
1590 	if (HAS_PSR2_SEL_FETCH(display) &&
1591 	    !intel_psr2_sel_fetch_config_valid(intel_dp, crtc_state) &&
1592 	    !HAS_PSR_HW_TRACKING(display)) {
1593 		drm_dbg_kms(display->drm,
1594 			    "Selective update not enabled, selective fetch not valid and no HW tracking available\n");
1595 		goto unsupported;
1596 	}
1597 
1598 	if (!sel_update_global_enabled(intel_dp)) {
1599 		drm_dbg_kms(display->drm,
1600 			    "Selective update disabled by flag\n");
1601 		goto unsupported;
1602 	}
1603 
1604 	if (!crtc_state->has_panel_replay && !intel_psr2_config_valid(intel_dp, crtc_state,
1605 								      conn_state))
1606 		goto unsupported;
1607 
1608 	if (!_compute_psr2_sdp_prior_scanline_indication(crtc_state, connector)) {
1609 		drm_dbg_kms(display->drm,
1610 			    "Selective update not enabled, SDP indication do not fit in hblank\n");
1611 		goto unsupported;
1612 	}
1613 
1614 	if (crtc_state->has_panel_replay) {
1615 		if (DISPLAY_VER(display) < 14)
1616 			goto unsupported;
1617 
1618 		if (!connector->dp.panel_replay_caps.su_support)
1619 			goto unsupported;
1620 
1621 		if (intel_dsc_enabled_on_link(crtc_state) &&
1622 		    connector->dp.panel_replay_caps.dsc_support !=
1623 		    INTEL_DP_PANEL_REPLAY_DSC_SELECTIVE_UPDATE) {
1624 			drm_dbg_kms(display->drm,
1625 				    "Selective update with Panel Replay not enabled because it's not supported with DSC\n");
1626 			goto unsupported;
1627 		}
1628 	}
1629 
1630 	if (crtc_state->crc_enabled) {
1631 		drm_dbg_kms(display->drm,
1632 			    "Selective update not enabled because it would inhibit pipe CRC calculation\n");
1633 		goto unsupported;
1634 	}
1635 
1636 	if (!psr2_granularity_check(crtc_state, connector)) {
1637 		drm_dbg_kms(display->drm,
1638 			    "Selective update not enabled, SU granularity not compatible\n");
1639 		goto unsupported;
1640 	}
1641 
1642 	crtc_state->enable_psr2_su_region_et = psr2_su_region_et_valid(connector,
1643 								       crtc_state->has_panel_replay);
1644 
1645 	return true;
1646 
1647 unsupported:
1648 	crtc_state->enable_psr2_sel_fetch = false;
1649 	return false;
1650 }
1651 
_psr_compute_config(struct intel_dp * intel_dp,struct intel_crtc_state * crtc_state,struct drm_connector_state * conn_state)1652 static bool _psr_compute_config(struct intel_dp *intel_dp,
1653 				struct intel_crtc_state *crtc_state,
1654 				struct drm_connector_state *conn_state)
1655 {
1656 	struct intel_display *display = to_intel_display(intel_dp);
1657 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
1658 	int entry_setup_frames;
1659 
1660 	if (!CAN_PSR(intel_dp) || !display->params.enable_psr)
1661 		return false;
1662 
1663 	/*
1664 	 * Currently PSR doesn't work reliably with VRR enabled.
1665 	 */
1666 	if (crtc_state->vrr.enable)
1667 		return false;
1668 
1669 	entry_setup_frames = intel_psr_entry_setup_frames(intel_dp, conn_state, adjusted_mode);
1670 
1671 	if (entry_setup_frames >= 0) {
1672 		crtc_state->entry_setup_frames = entry_setup_frames;
1673 	} else {
1674 		crtc_state->no_psr_reason = "PSR setup timing not met";
1675 		drm_dbg_kms(display->drm,
1676 			    "PSR condition failed: PSR setup timing not met\n");
1677 		return false;
1678 	}
1679 
1680 	return true;
1681 }
1682 
compute_link_off_after_as_sdp_when_pr_active(struct intel_connector * connector)1683 static inline bool compute_link_off_after_as_sdp_when_pr_active(struct intel_connector *connector)
1684 {
1685 	return (connector->dp.panel_replay_caps.dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_CAPABILITY)] &
1686 		DP_PANEL_REPLAY_LINK_OFF_SUPPORTED_IN_PR_AFTER_ADAPTIVE_SYNC_SDP);
1687 }
1688 
compute_disable_as_sdp_when_pr_active(struct intel_connector * connector)1689 static inline bool compute_disable_as_sdp_when_pr_active(struct intel_connector *connector)
1690 {
1691 	return !(connector->dp.panel_replay_caps.dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_CAPABILITY)] &
1692 		 DP_PANEL_REPLAY_ASYNC_VIDEO_TIMING_NOT_SUPPORTED_IN_PR);
1693 }
1694 
_panel_replay_compute_config(struct intel_crtc_state * crtc_state,const struct drm_connector_state * conn_state)1695 static bool _panel_replay_compute_config(struct intel_crtc_state *crtc_state,
1696 					 const struct drm_connector_state *conn_state)
1697 {
1698 	struct intel_connector *connector =
1699 		to_intel_connector(conn_state->connector);
1700 	struct intel_dp *intel_dp = intel_attached_dp(connector);
1701 	struct intel_display *display = to_intel_display(intel_dp);
1702 	struct intel_hdcp *hdcp = &connector->hdcp;
1703 
1704 	if (!CAN_PANEL_REPLAY(intel_dp))
1705 		return false;
1706 
1707 	if (!connector->dp.panel_replay_caps.support)
1708 		return false;
1709 
1710 	if (!panel_replay_global_enabled(intel_dp)) {
1711 		drm_dbg_kms(display->drm, "Panel Replay disabled by flag\n");
1712 		return false;
1713 	}
1714 
1715 	if (crtc_state->crc_enabled) {
1716 		drm_dbg_kms(display->drm,
1717 			    "Panel Replay not enabled because it would inhibit pipe CRC calculation\n");
1718 		return false;
1719 	}
1720 
1721 	if (intel_dsc_enabled_on_link(crtc_state) &&
1722 	    connector->dp.panel_replay_caps.dsc_support ==
1723 	    INTEL_DP_PANEL_REPLAY_DSC_NOT_SUPPORTED) {
1724 		drm_dbg_kms(display->drm,
1725 			    "Panel Replay not enabled because it's not supported with DSC\n");
1726 		return false;
1727 	}
1728 
1729 	crtc_state->link_off_after_as_sdp_when_pr_active = compute_link_off_after_as_sdp_when_pr_active(connector);
1730 	crtc_state->disable_as_sdp_when_pr_active = compute_disable_as_sdp_when_pr_active(connector);
1731 
1732 	if (!intel_dp_is_edp(intel_dp))
1733 		return true;
1734 
1735 	/* Remaining checks are for eDP only */
1736 
1737 	if (to_intel_crtc(crtc_state->uapi.crtc)->pipe != PIPE_A &&
1738 	    to_intel_crtc(crtc_state->uapi.crtc)->pipe != PIPE_B)
1739 		return false;
1740 
1741 	/* 128b/132b Panel Replay is not supported on eDP */
1742 	if (intel_dp_is_uhbr(crtc_state)) {
1743 		drm_dbg_kms(display->drm,
1744 			    "Panel Replay is not supported with 128b/132b\n");
1745 		return false;
1746 	}
1747 
1748 	/* HW will not allow Panel Replay on eDP when HDCP enabled */
1749 	if (conn_state->content_protection ==
1750 	    DRM_MODE_CONTENT_PROTECTION_DESIRED ||
1751 	    (conn_state->content_protection ==
1752 	     DRM_MODE_CONTENT_PROTECTION_ENABLED && hdcp->value ==
1753 	     DRM_MODE_CONTENT_PROTECTION_UNDESIRED)) {
1754 		drm_dbg_kms(display->drm,
1755 			    "Panel Replay is not supported with HDCP\n");
1756 		return false;
1757 	}
1758 
1759 	if (!alpm_config_valid(intel_dp, crtc_state, true, true, false))
1760 		return false;
1761 
1762 	return true;
1763 }
1764 
intel_psr_needs_wa_18037818876(struct intel_dp * intel_dp,struct intel_crtc_state * crtc_state)1765 static bool intel_psr_needs_wa_18037818876(struct intel_dp *intel_dp,
1766 					   struct intel_crtc_state *crtc_state)
1767 {
1768 	struct intel_display *display = to_intel_display(intel_dp);
1769 
1770 	return (DISPLAY_VER(display) == 20 && crtc_state->entry_setup_frames > 0 &&
1771 		!crtc_state->has_sel_update);
1772 }
1773 
psr2_dc3co_disable_locked(struct intel_dp * intel_dp)1774 static void psr2_dc3co_disable_locked(struct intel_dp *intel_dp)
1775 {
1776 	struct intel_display *display = to_intel_display(intel_dp);
1777 
1778 	if (intel_dp->psr.dc3co_allowed) {
1779 		intel_dp->psr.dc3co_allowed = false;
1780 		intel_display_power_set_target_dc_state(display, DC_STATE_EN_UPTO_DC6);
1781 		psr2_program_idle_frames(intel_dp, psr_compute_idle_frames(intel_dp));
1782 	}
1783 }
1784 
psr2_dc3co_disable_work(struct work_struct * work)1785 static void psr2_dc3co_disable_work(struct work_struct *work)
1786 {
1787 	struct intel_dp *intel_dp =
1788 		container_of(work, typeof(*intel_dp), psr.dc3co_work.work);
1789 
1790 	mutex_lock(&intel_dp->psr.lock);
1791 	psr2_dc3co_disable_locked(intel_dp);
1792 	mutex_unlock(&intel_dp->psr.lock);
1793 }
1794 
1795 static void
psr2_dc3co_flush_locked(struct intel_dp * intel_dp,unsigned int frontbuffer_bits)1796 psr2_dc3co_flush_locked(struct intel_dp *intel_dp, unsigned int frontbuffer_bits)
1797 {
1798 	struct intel_display *display = to_intel_display(intel_dp);
1799 
1800 	if (!intel_dp->psr.dc3co_allowed)
1801 		return;
1802 
1803 	if (!intel_dp->psr.sel_update_enabled ||
1804 	    !intel_dp->psr.active)
1805 		return;
1806 	/*
1807 	 * At every frontbuffer flush flip event modified delay of delayed work,
1808 	 * when delayed work schedules that means display has been idle.
1809 	 */
1810 	if (!(frontbuffer_bits &
1811 	    INTEL_FRONTBUFFER_ALL_MASK(intel_dp->psr.pipe)))
1812 		return;
1813 
1814 	mod_delayed_work(display->wq.unordered, &intel_dp->psr.dc3co_work,
1815 			 intel_dp->psr.dc3co_exit_delay);
1816 }
1817 
1818 static
intel_psr_set_non_psr_pipes(struct intel_dp * intel_dp,struct intel_crtc_state * crtc_state)1819 void intel_psr_set_non_psr_pipes(struct intel_dp *intel_dp,
1820 				 struct intel_crtc_state *crtc_state)
1821 {
1822 	struct intel_display *display = to_intel_display(intel_dp);
1823 	struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->uapi.state);
1824 	struct intel_crtc *crtc;
1825 	u8 active_pipes = 0;
1826 
1827 	/* Wa_16025596647 */
1828 	if (!intel_display_wa(display, INTEL_DISPLAY_WA_16025596647))
1829 		return;
1830 
1831 	/* Not needed by Panel Replay  */
1832 	if (crtc_state->has_panel_replay)
1833 		return;
1834 
1835 	/* We ignore possible secondary PSR/Panel Replay capable eDP */
1836 	for_each_intel_crtc(display, crtc)
1837 		active_pipes |= crtc->active ? BIT(crtc->pipe) : 0;
1838 
1839 	active_pipes = intel_calc_active_pipes(state, active_pipes);
1840 
1841 	crtc_state->active_non_psr_pipes = active_pipes &
1842 		~BIT(to_intel_crtc(crtc_state->uapi.crtc)->pipe);
1843 }
1844 
intel_psr_compute_config(struct intel_dp * intel_dp,struct intel_crtc_state * crtc_state,struct drm_connector_state * conn_state)1845 void intel_psr_compute_config(struct intel_dp *intel_dp,
1846 			      struct intel_crtc_state *crtc_state,
1847 			      struct drm_connector_state *conn_state)
1848 {
1849 	struct intel_display *display = to_intel_display(intel_dp);
1850 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
1851 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
1852 
1853 	if (!psr_global_enabled(intel_dp)) {
1854 		drm_dbg_kms(display->drm, "PSR disabled by flag\n");
1855 		return;
1856 	}
1857 
1858 	if (intel_dp->psr.sink_not_reliable) {
1859 		drm_dbg_kms(display->drm,
1860 			    "PSR sink implementation is not reliable\n");
1861 		return;
1862 	}
1863 
1864 	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
1865 		drm_dbg_kms(display->drm,
1866 			    "PSR condition failed: Interlaced mode enabled\n");
1867 		return;
1868 	}
1869 
1870 	/*
1871 	 * FIXME figure out what is wrong with PSR+joiner and
1872 	 * fix it. Presumably something related to the fact that
1873 	 * PSR is a transcoder level feature.
1874 	 */
1875 	if (crtc_state->joiner_pipes) {
1876 		drm_dbg_kms(display->drm,
1877 			    "PSR disabled due to joiner\n");
1878 		return;
1879 	}
1880 
1881 	/* Only used for state verification. */
1882 	crtc_state->panel_replay_dsc_support = connector->dp.panel_replay_caps.dsc_support;
1883 	crtc_state->has_panel_replay = _panel_replay_compute_config(crtc_state, conn_state);
1884 
1885 	crtc_state->has_psr = crtc_state->has_panel_replay ? true :
1886 		_psr_compute_config(intel_dp, crtc_state, conn_state);
1887 
1888 	if (!crtc_state->has_psr)
1889 		return;
1890 
1891 	crtc_state->has_sel_update = intel_sel_update_config_valid(crtc_state, conn_state);
1892 }
1893 
intel_psr_get_config(struct intel_encoder * encoder,struct intel_crtc_state * pipe_config)1894 void intel_psr_get_config(struct intel_encoder *encoder,
1895 			  struct intel_crtc_state *pipe_config)
1896 {
1897 	struct intel_display *display = to_intel_display(encoder);
1898 	struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1899 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
1900 	struct intel_dp *intel_dp;
1901 	u32 val;
1902 
1903 	if (!dig_port)
1904 		return;
1905 
1906 	intel_dp = &dig_port->dp;
1907 	if (!(CAN_PSR(intel_dp) || CAN_PANEL_REPLAY(intel_dp)))
1908 		return;
1909 
1910 	mutex_lock(&intel_dp->psr.lock);
1911 	if (!intel_dp->psr.enabled)
1912 		goto unlock;
1913 
1914 	if (intel_dp->psr.panel_replay_enabled) {
1915 		pipe_config->has_psr = pipe_config->has_panel_replay = true;
1916 	} else {
1917 		/*
1918 		 * Not possible to read EDP_PSR/PSR2_CTL registers as it is
1919 		 * enabled/disabled because of frontbuffer tracking and others.
1920 		 */
1921 		pipe_config->has_psr = true;
1922 	}
1923 
1924 	pipe_config->has_sel_update = intel_dp->psr.sel_update_enabled;
1925 	pipe_config->infoframes.enable |= intel_hdmi_infoframe_enable(DP_SDP_VSC);
1926 
1927 	if (!intel_dp->psr.sel_update_enabled)
1928 		goto unlock;
1929 
1930 	if (HAS_PSR2_SEL_FETCH(display)) {
1931 		val = intel_de_read(display,
1932 				    PSR2_MAN_TRK_CTL(display, cpu_transcoder));
1933 		if (val & PSR2_MAN_TRK_CTL_ENABLE)
1934 			pipe_config->enable_psr2_sel_fetch = true;
1935 	}
1936 
1937 	pipe_config->enable_psr2_su_region_et = intel_dp->psr.su_region_et_enabled;
1938 unlock:
1939 	mutex_unlock(&intel_dp->psr.lock);
1940 }
1941 
intel_psr_activate(struct intel_dp * intel_dp)1942 static void intel_psr_activate(struct intel_dp *intel_dp)
1943 {
1944 	struct intel_display *display = to_intel_display(intel_dp);
1945 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
1946 
1947 	drm_WARN_ON(display->drm,
1948 		    transcoder_has_psr2(display, cpu_transcoder) &&
1949 		    intel_de_read(display, EDP_PSR2_CTL(display, cpu_transcoder)) & EDP_PSR2_ENABLE);
1950 
1951 	drm_WARN_ON(display->drm,
1952 		    intel_de_read(display, psr_ctl_reg(display, cpu_transcoder)) & EDP_PSR_ENABLE);
1953 
1954 	drm_WARN_ON(display->drm, intel_dp->psr.active);
1955 
1956 	drm_WARN_ON(display->drm, !intel_dp->psr.enabled);
1957 
1958 	lockdep_assert_held(&intel_dp->psr.lock);
1959 
1960 	/* psr1, psr2 and panel-replay are mutually exclusive.*/
1961 	if (intel_dp->psr.panel_replay_enabled)
1962 		dg2_activate_panel_replay(intel_dp);
1963 	else if (intel_dp->psr.sel_update_enabled)
1964 		hsw_activate_psr2(intel_dp);
1965 	else
1966 		hsw_activate_psr1(intel_dp);
1967 
1968 	intel_dp->psr.active = true;
1969 	intel_dp->psr.no_psr_reason = NULL;
1970 }
1971 
1972 /*
1973  * Wa_16013835468
1974  * Wa_14015648006
1975  */
wm_optimization_wa(struct intel_dp * intel_dp,const struct intel_crtc_state * crtc_state)1976 static void wm_optimization_wa(struct intel_dp *intel_dp,
1977 			       const struct intel_crtc_state *crtc_state)
1978 {
1979 	struct intel_display *display = to_intel_display(intel_dp);
1980 	enum pipe pipe = intel_dp->psr.pipe;
1981 	bool activate = false;
1982 
1983 	/* Wa_14015648006 */
1984 	if (IS_DISPLAY_VER(display, 11, 14) && crtc_state->wm_level_disabled)
1985 		activate = true;
1986 
1987 	/* Wa_16013835468 */
1988 	if (DISPLAY_VER(display) == 12 &&
1989 	    crtc_state->hw.adjusted_mode.crtc_vblank_start !=
1990 	    crtc_state->hw.adjusted_mode.crtc_vdisplay)
1991 		activate = true;
1992 
1993 	if (activate)
1994 		intel_de_rmw(display, GEN8_CHICKEN_DCPR_1,
1995 			     0, LATENCY_REPORTING_REMOVED(pipe));
1996 	else
1997 		intel_de_rmw(display, GEN8_CHICKEN_DCPR_1,
1998 			     LATENCY_REPORTING_REMOVED(pipe), 0);
1999 }
2000 
intel_psr_enable_source(struct intel_dp * intel_dp,const struct intel_crtc_state * crtc_state)2001 static void intel_psr_enable_source(struct intel_dp *intel_dp,
2002 				    const struct intel_crtc_state *crtc_state)
2003 {
2004 	struct intel_display *display = to_intel_display(intel_dp);
2005 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
2006 	u32 mask = 0;
2007 
2008 	/*
2009 	 * Only HSW and BDW have PSR AUX registers that need to be setup.
2010 	 * SKL+ use hardcoded values PSR AUX transactions
2011 	 */
2012 	if (DISPLAY_VER(display) < 9)
2013 		hsw_psr_setup_aux(intel_dp);
2014 
2015 	/*
2016 	 * Per Spec: Avoid continuous PSR exit by masking MEMUP and HPD also
2017 	 * mask LPSP to avoid dependency on other drivers that might block
2018 	 * runtime_pm besides preventing  other hw tracking issues now we
2019 	 * can rely on frontbuffer tracking.
2020 	 *
2021 	 * From bspec prior LunarLake:
2022 	 * Only PSR_MASK[Mask FBC modify] and PSR_MASK[Mask Hotplug] are used in
2023 	 * panel replay mode.
2024 	 *
2025 	 * From bspec beyod LunarLake:
2026 	 * Panel Replay on DP: No bits are applicable
2027 	 * Panel Replay on eDP: All bits are applicable
2028 	 */
2029 	if (DISPLAY_VER(display) < 20 || intel_dp_is_edp(intel_dp))
2030 		mask = EDP_PSR_DEBUG_MASK_HPD;
2031 
2032 	if (intel_dp_is_edp(intel_dp)) {
2033 		mask |= EDP_PSR_DEBUG_MASK_MEMUP;
2034 
2035 		/*
2036 		 * For some unknown reason on HSW non-ULT (or at least on
2037 		 * Dell Latitude E6540) external displays start to flicker
2038 		 * when PSR is enabled on the eDP. SR/PC6 residency is much
2039 		 * higher than should be possible with an external display.
2040 		 * As a workaround leave LPSP unmasked to prevent PSR entry
2041 		 * when external displays are active.
2042 		 */
2043 		if (DISPLAY_VER(display) >= 8 || display->platform.haswell_ult)
2044 			mask |= EDP_PSR_DEBUG_MASK_LPSP;
2045 
2046 		if (DISPLAY_VER(display) < 20)
2047 			mask |= EDP_PSR_DEBUG_MASK_MAX_SLEEP;
2048 
2049 		/*
2050 		 * No separate pipe reg write mask on hsw/bdw, so have to unmask all
2051 		 * registers in order to keep the CURSURFLIVE tricks working :(
2052 		 */
2053 		if (IS_DISPLAY_VER(display, 9, 10))
2054 			mask |= EDP_PSR_DEBUG_MASK_DISP_REG_WRITE;
2055 
2056 		/* allow PSR with sprite enabled */
2057 		if (display->platform.haswell)
2058 			mask |= EDP_PSR_DEBUG_MASK_SPRITE_ENABLE;
2059 	}
2060 
2061 	intel_de_write(display, psr_debug_reg(display, cpu_transcoder), mask);
2062 
2063 	psr_irq_control(intel_dp);
2064 
2065 	if (HAS_PSR_HW_TRACKING(display) && HAS_PSR2_SEL_FETCH(display))
2066 		intel_de_rmw(display, CHICKEN_PAR1_1, IGNORE_PSR2_HW_TRACKING,
2067 			     intel_dp->psr.psr2_sel_fetch_enabled ?
2068 			     IGNORE_PSR2_HW_TRACKING : 0);
2069 
2070 	/*
2071 	 * Wa_16013835468
2072 	 * Wa_14015648006
2073 	 */
2074 	wm_optimization_wa(intel_dp, crtc_state);
2075 
2076 	if (intel_dp->psr.sel_update_enabled) {
2077 		if (DISPLAY_VER(display) == 9)
2078 			intel_de_rmw(display, CHICKEN_TRANS(display, cpu_transcoder), 0,
2079 				     PSR2_VSC_ENABLE_PROG_HEADER |
2080 				     PSR2_ADD_VERTICAL_LINE_COUNT);
2081 
2082 		/*
2083 		 * Wa_16014451276:adlp,mtl[a0,b0]
2084 		 * All supported adlp panels have 1-based X granularity, this may
2085 		 * cause issues if non-supported panels are used.
2086 		 */
2087 		if (!intel_dp->psr.panel_replay_enabled &&
2088 		    (IS_DISPLAY_VERx100_STEP(display, 1400, STEP_A0, STEP_B0) ||
2089 		     display->platform.alderlake_p))
2090 			intel_de_rmw(display, CHICKEN_TRANS(display, cpu_transcoder),
2091 				     0, ADLP_1_BASED_X_GRANULARITY);
2092 
2093 		/* Wa_16012604467:adlp,mtl[a0,b0] */
2094 		if (!intel_dp->psr.panel_replay_enabled &&
2095 		    IS_DISPLAY_VERx100_STEP(display, 1400, STEP_A0, STEP_B0))
2096 			intel_de_rmw(display,
2097 				     MTL_CLKGATE_DIS_TRANS(display, cpu_transcoder),
2098 				     0,
2099 				     MTL_CLKGATE_DIS_TRANS_DMASC_GATING_DIS);
2100 		else if (display->platform.alderlake_p)
2101 			intel_de_rmw(display, CLKGATE_DIS_MISC, 0,
2102 				     CLKGATE_DIS_MISC_DMASC_GATING_DIS);
2103 
2104 		/*
2105 		 * Wa_14026643300
2106 		 * On Xe3P, restrict DC3CO entry during active frame when PSR2 is
2107 		 * enabled without panel Early Transport; required to avoid pipe bad state.
2108 		 * DMC honours CHICKEN_DCPR_4 bit 24 to block DC3CO entry during active frame.
2109 		 */
2110 		if (intel_display_wa(display, INTEL_DISPLAY_WA_14026643300) &&
2111 		    !intel_dp->psr.panel_replay_enabled &&
2112 		    !intel_dp->psr.su_region_et_enabled)
2113 			intel_de_rmw(display, XE3P_CHICKEN_DCPR_4,
2114 				     0, DCPR4_BLOCK_DC3CO_ACTIVE_FRAME);
2115 	}
2116 
2117 	/* Wa_16025596647 */
2118 	if ((DISPLAY_VER(display) == 20 ||
2119 	     IS_DISPLAY_VERx100_STEP(display, 3000, STEP_A0, STEP_B0)) &&
2120 	    !intel_dp->psr.panel_replay_enabled)
2121 		intel_dmc_block_pkgc(display, intel_dp->psr.pipe, true);
2122 
2123 	intel_alpm_configure(intel_dp, crtc_state);
2124 
2125 	if (HAS_PSR_TRANS_PUSH_FRAME_CHANGE(display))
2126 		intel_vrr_psr_frame_change_enable(crtc_state);
2127 }
2128 
psr_interrupt_error_check(struct intel_dp * intel_dp)2129 static bool psr_interrupt_error_check(struct intel_dp *intel_dp)
2130 {
2131 	struct intel_display *display = to_intel_display(intel_dp);
2132 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
2133 	u32 val;
2134 
2135 	if (intel_dp->psr.panel_replay_enabled)
2136 		goto no_err;
2137 
2138 	/*
2139 	 * If a PSR error happened and the driver is reloaded, the EDP_PSR_IIR
2140 	 * will still keep the error set even after the reset done in the
2141 	 * irq_preinstall and irq_uninstall hooks.
2142 	 * And enabling in this situation cause the screen to freeze in the
2143 	 * first time that PSR HW tries to activate so lets keep PSR disabled
2144 	 * to avoid any rendering problems.
2145 	 */
2146 	val = intel_de_read(display, psr_iir_reg(display, cpu_transcoder));
2147 	val &= psr_irq_psr_error_bit_get(intel_dp);
2148 	if (val) {
2149 		intel_dp->psr.sink_not_reliable = true;
2150 		drm_dbg_kms(display->drm,
2151 			    "PSR interruption error set, not enabling PSR\n");
2152 		return false;
2153 	}
2154 
2155 no_err:
2156 	return true;
2157 }
2158 
intel_psr_enable_locked(struct intel_dp * intel_dp,const struct intel_crtc_state * crtc_state)2159 static void intel_psr_enable_locked(struct intel_dp *intel_dp,
2160 				    const struct intel_crtc_state *crtc_state)
2161 {
2162 	struct intel_display *display = to_intel_display(intel_dp);
2163 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2164 	u32 val;
2165 
2166 	drm_WARN_ON(display->drm, intel_dp->psr.enabled);
2167 
2168 	intel_dp->psr.sel_update_enabled = crtc_state->has_sel_update;
2169 	intel_dp->psr.panel_replay_enabled = crtc_state->has_panel_replay;
2170 	intel_dp->psr.busy_frontbuffer_bits = 0;
2171 	intel_dp->psr.pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
2172 	intel_dp->psr.transcoder = crtc_state->cpu_transcoder;
2173 	/* DC5/DC6 requires at least 6 idle frames */
2174 	val = usecs_to_jiffies(intel_get_frame_time_us(crtc_state) * 6);
2175 	intel_dp->psr.dc3co_exit_delay = val;
2176 	intel_dp->psr.psr2_sel_fetch_enabled = crtc_state->enable_psr2_sel_fetch;
2177 	intel_dp->psr.su_region_et_enabled = crtc_state->enable_psr2_su_region_et;
2178 	intel_dp->psr.psr2_sel_fetch_cff_enabled = false;
2179 	intel_dp->psr.req_psr2_sdp_prior_scanline =
2180 		crtc_state->req_psr2_sdp_prior_scanline;
2181 	intel_dp->psr.active_non_psr_pipes = crtc_state->active_non_psr_pipes;
2182 	intel_dp->psr.pkg_c_latency_used = crtc_state->pkg_c_latency_used;
2183 	intel_dp->psr.io_wake_lines = crtc_state->alpm_state.io_wake_lines;
2184 	intel_dp->psr.fast_wake_lines = crtc_state->alpm_state.fast_wake_lines;
2185 	intel_dp->psr.entry_setup_frames = crtc_state->entry_setup_frames;
2186 
2187 	if (!psr_interrupt_error_check(intel_dp))
2188 		return;
2189 
2190 	if (intel_dp->psr.panel_replay_enabled)
2191 		drm_dbg_kms(display->drm, "Enabling Panel Replay\n");
2192 	else
2193 		drm_dbg_kms(display->drm, "Enabling PSR%s\n",
2194 			    intel_dp->psr.sel_update_enabled ? "2" : "1");
2195 
2196 	/*
2197 	 * Enabling sink PSR/Panel Replay here only for PSR. Panel Replay enable
2198 	 * bit is already written at this point. Sink ALPM is enabled here for
2199 	 * PSR and Panel Replay. See
2200 	 * intel_psr_panel_replay_enable_sink. Modifiers/options:
2201 	 *  - Selective Update
2202 	 *  - Region Early Transport
2203 	 *  - Selective Update Region Scanline Capture
2204 	 *  - VSC_SDP_CRC
2205 	 *  - HPD on different Errors
2206 	 *  - CRC verification
2207 	 * are written for PSR and Panel Replay here.
2208 	 */
2209 	intel_psr_enable_sink(intel_dp, crtc_state);
2210 
2211 	if (intel_dp_is_edp(intel_dp))
2212 		intel_snps_phy_update_psr_power_state(&dig_port->base, true);
2213 
2214 	intel_psr_enable_source(intel_dp, crtc_state);
2215 	intel_dp->psr.enabled = true;
2216 	intel_dp->psr.pause_counter = 0;
2217 
2218 	/*
2219 	 * Link_ok is sticky and set here on PSR enable. We can assume link
2220 	 * training is complete as we never continue to PSR enable with
2221 	 * untrained link. Link_ok is kept as set until first short pulse
2222 	 * interrupt. This is targeted to workaround panels stating bad link
2223 	 * after PSR is enabled.
2224 	 */
2225 	intel_dp->psr.link_ok = true;
2226 
2227 	intel_psr_activate(intel_dp);
2228 }
2229 
intel_psr_exit(struct intel_dp * intel_dp)2230 static void intel_psr_exit(struct intel_dp *intel_dp)
2231 {
2232 	struct intel_display *display = to_intel_display(intel_dp);
2233 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
2234 	u32 val;
2235 
2236 	if (!intel_dp->psr.active) {
2237 		if (transcoder_has_psr2(display, cpu_transcoder)) {
2238 			val = intel_de_read(display,
2239 					    EDP_PSR2_CTL(display, cpu_transcoder));
2240 			drm_WARN_ON(display->drm, val & EDP_PSR2_ENABLE);
2241 		}
2242 
2243 		val = intel_de_read(display,
2244 				    psr_ctl_reg(display, cpu_transcoder));
2245 		drm_WARN_ON(display->drm, val & EDP_PSR_ENABLE);
2246 
2247 		return;
2248 	}
2249 
2250 	if (intel_dp->psr.panel_replay_enabled) {
2251 		intel_de_rmw(display, TRANS_DP2_CTL(intel_dp->psr.transcoder),
2252 			     TRANS_DP2_PANEL_REPLAY_ENABLE, 0);
2253 	} else if (intel_dp->psr.sel_update_enabled) {
2254 		val = intel_de_rmw(display,
2255 				   EDP_PSR2_CTL(display, cpu_transcoder),
2256 				   EDP_PSR2_ENABLE, 0);
2257 
2258 		drm_WARN_ON(display->drm, !(val & EDP_PSR2_ENABLE));
2259 	} else {
2260 		if ((DISPLAY_VER(display) == 20 ||
2261 		     IS_DISPLAY_VERx100_STEP(display, 3000, STEP_A0, STEP_B0)) &&
2262 			intel_dp->psr.pkg_c_latency_used)
2263 			intel_dmc_start_pkgc_exit_at_start_of_undelayed_vblank(display,
2264 								       intel_dp->psr.pipe,
2265 								       false);
2266 
2267 		val = intel_de_rmw(display,
2268 				   psr_ctl_reg(display, cpu_transcoder),
2269 				   EDP_PSR_ENABLE, 0);
2270 
2271 		drm_WARN_ON(display->drm, !(val & EDP_PSR_ENABLE));
2272 	}
2273 	intel_dp->psr.active = false;
2274 }
2275 
intel_psr2_in_deep_sleep(struct intel_dp * intel_dp)2276 bool intel_psr2_in_deep_sleep(struct intel_dp *intel_dp)
2277 {
2278 	struct intel_display *display = to_intel_display(intel_dp);
2279 	enum transcoder cpu_transcoder;
2280 	bool in_deep_sleep = false;
2281 	u32 val;
2282 
2283 	mutex_lock(&intel_dp->psr.lock);
2284 
2285 	if (!intel_dp->psr.enabled || !intel_dp->psr.sel_update_enabled)
2286 		goto out;
2287 
2288 	cpu_transcoder = intel_dp->psr.transcoder;
2289 	val = intel_de_read(display, EDP_PSR2_STATUS(display, cpu_transcoder));
2290 	in_deep_sleep = (val & EDP_PSR2_STATUS_STATE_MASK) ==
2291 		EDP_PSR2_STATUS_STATE_DEEP_SLEEP;
2292 out:
2293 	mutex_unlock(&intel_dp->psr.lock);
2294 	return in_deep_sleep;
2295 }
2296 
intel_psr_wait_exit_locked(struct intel_dp * intel_dp)2297 static void intel_psr_wait_exit_locked(struct intel_dp *intel_dp)
2298 {
2299 	struct intel_display *display = to_intel_display(intel_dp);
2300 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
2301 	intel_reg_t psr_status;
2302 	u32 psr_status_mask;
2303 
2304 	if (intel_dp_is_edp(intel_dp) && (intel_dp->psr.sel_update_enabled ||
2305 					  intel_dp->psr.panel_replay_enabled)) {
2306 		psr_status = EDP_PSR2_STATUS(display, cpu_transcoder);
2307 		psr_status_mask = EDP_PSR2_STATUS_STATE_MASK;
2308 	} else {
2309 		psr_status = psr_status_reg(display, cpu_transcoder);
2310 		psr_status_mask = EDP_PSR_STATUS_STATE_MASK;
2311 	}
2312 
2313 	/* Wait till PSR is idle */
2314 	if (intel_de_wait_for_clear_ms(display, psr_status,
2315 				       psr_status_mask, 2000))
2316 		drm_err(display->drm, "Timed out waiting PSR idle state\n");
2317 }
2318 
intel_psr_disable_locked(struct intel_dp * intel_dp)2319 static void intel_psr_disable_locked(struct intel_dp *intel_dp)
2320 {
2321 	struct intel_display *display = to_intel_display(intel_dp);
2322 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
2323 
2324 	lockdep_assert_held(&intel_dp->psr.lock);
2325 
2326 	if (!intel_dp->psr.enabled)
2327 		return;
2328 
2329 	if (intel_dp->psr.panel_replay_enabled)
2330 		drm_dbg_kms(display->drm, "Disabling Panel Replay\n");
2331 	else
2332 		drm_dbg_kms(display->drm, "Disabling PSR%s\n",
2333 			    intel_dp->psr.sel_update_enabled ? "2" : "1");
2334 
2335 	intel_psr_exit(intel_dp);
2336 	intel_psr_wait_exit_locked(intel_dp);
2337 
2338 	/*
2339 	 * Wa_16013835468
2340 	 * Wa_14015648006
2341 	 */
2342 	if (DISPLAY_VER(display) >= 11)
2343 		intel_de_rmw(display, GEN8_CHICKEN_DCPR_1,
2344 			     LATENCY_REPORTING_REMOVED(intel_dp->psr.pipe), 0);
2345 
2346 	if (intel_dp->psr.sel_update_enabled) {
2347 		/* Wa_16012604467:adlp,mtl[a0,b0] */
2348 		if (!intel_dp->psr.panel_replay_enabled &&
2349 		    IS_DISPLAY_VERx100_STEP(display, 1400, STEP_A0, STEP_B0))
2350 			intel_de_rmw(display,
2351 				     MTL_CLKGATE_DIS_TRANS(display, cpu_transcoder),
2352 				     MTL_CLKGATE_DIS_TRANS_DMASC_GATING_DIS, 0);
2353 		else if (display->platform.alderlake_p)
2354 			intel_de_rmw(display, CLKGATE_DIS_MISC,
2355 				     CLKGATE_DIS_MISC_DMASC_GATING_DIS, 0);
2356 
2357 		if (intel_display_wa(display, INTEL_DISPLAY_WA_14026643300))
2358 			intel_de_rmw(display, XE3P_CHICKEN_DCPR_4,
2359 				     DCPR4_BLOCK_DC3CO_ACTIVE_FRAME, 0);
2360 	}
2361 
2362 	if (intel_dp_is_edp(intel_dp))
2363 		intel_snps_phy_update_psr_power_state(&dp_to_dig_port(intel_dp)->base, false);
2364 
2365 	if (intel_dp->psr.panel_replay_enabled && intel_dp_is_edp(intel_dp))
2366 		intel_alpm_disable(intel_dp);
2367 
2368 	/* Disable PSR on Sink */
2369 	if (!intel_dp->psr.panel_replay_enabled) {
2370 		drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG, 0);
2371 
2372 		if (intel_dp->psr.sel_update_enabled)
2373 			drm_dp_dpcd_writeb(&intel_dp->aux,
2374 					   DP_RECEIVER_ALPM_CONFIG, 0);
2375 	}
2376 
2377 	/* Wa_16025596647 */
2378 	if ((DISPLAY_VER(display) == 20 ||
2379 	     IS_DISPLAY_VERx100_STEP(display, 3000, STEP_A0, STEP_B0)) &&
2380 	    !intel_dp->psr.panel_replay_enabled)
2381 		intel_dmc_block_pkgc(display, intel_dp->psr.pipe, false);
2382 
2383 	intel_dp->psr.enabled = false;
2384 	intel_dp->psr.panel_replay_enabled = false;
2385 	intel_dp->psr.sel_update_enabled = false;
2386 	intel_dp->psr.psr2_sel_fetch_enabled = false;
2387 	intel_dp->psr.su_region_et_enabled = false;
2388 	intel_dp->psr.psr2_sel_fetch_cff_enabled = false;
2389 	intel_dp->psr.active_non_psr_pipes = 0;
2390 	intel_dp->psr.pkg_c_latency_used = 0;
2391 	cancel_delayed_work(&intel_dp->psr.dc3co_work);
2392 	intel_dp->psr.dc3co_allowed = false;
2393 }
2394 
2395 /**
2396  * intel_psr_disable - Disable PSR
2397  * @intel_dp: Intel DP
2398  * @old_crtc_state: old CRTC state
2399  *
2400  * This function needs to be called before disabling pipe.
2401  */
intel_psr_disable(struct intel_dp * intel_dp,const struct intel_crtc_state * old_crtc_state)2402 void intel_psr_disable(struct intel_dp *intel_dp,
2403 		       const struct intel_crtc_state *old_crtc_state)
2404 {
2405 	struct intel_display *display = to_intel_display(intel_dp);
2406 
2407 	if (!old_crtc_state->has_psr)
2408 		return;
2409 
2410 	if (drm_WARN_ON(display->drm, !CAN_PSR(intel_dp) &&
2411 			!CAN_PANEL_REPLAY(intel_dp)))
2412 		return;
2413 
2414 	mutex_lock(&intel_dp->psr.lock);
2415 
2416 	intel_psr_disable_locked(intel_dp);
2417 
2418 	intel_dp->psr.link_ok = false;
2419 
2420 	mutex_unlock(&intel_dp->psr.lock);
2421 	cancel_work_sync(&intel_dp->psr.work);
2422 	cancel_delayed_work_sync(&intel_dp->psr.dc3co_work);
2423 }
2424 
2425 /**
2426  * intel_psr_pause - Pause PSR
2427  * @intel_dp: Intel DP
2428  *
2429  * This function need to be called after enabling psr.
2430  */
intel_psr_pause(struct intel_dp * intel_dp)2431 void intel_psr_pause(struct intel_dp *intel_dp)
2432 {
2433 	struct intel_psr *psr = &intel_dp->psr;
2434 
2435 	if (!CAN_PSR(intel_dp) && !CAN_PANEL_REPLAY(intel_dp))
2436 		return;
2437 
2438 	mutex_lock(&psr->lock);
2439 
2440 	if (!psr->enabled) {
2441 		mutex_unlock(&psr->lock);
2442 		return;
2443 	}
2444 
2445 	if (intel_dp->psr.pause_counter++ == 0) {
2446 		intel_psr_exit(intel_dp);
2447 		intel_psr_wait_exit_locked(intel_dp);
2448 	}
2449 
2450 	mutex_unlock(&psr->lock);
2451 
2452 	cancel_work_sync(&psr->work);
2453 	cancel_delayed_work_sync(&psr->dc3co_work);
2454 }
2455 
2456 /**
2457  * intel_psr_resume - Resume PSR
2458  * @intel_dp: Intel DP
2459  *
2460  * This function need to be called after pausing psr.
2461  */
intel_psr_resume(struct intel_dp * intel_dp)2462 void intel_psr_resume(struct intel_dp *intel_dp)
2463 {
2464 	struct intel_display *display = to_intel_display(intel_dp);
2465 	struct intel_psr *psr = &intel_dp->psr;
2466 
2467 	if (!CAN_PSR(intel_dp) && !CAN_PANEL_REPLAY(intel_dp))
2468 		return;
2469 
2470 	mutex_lock(&psr->lock);
2471 
2472 	if (!psr->enabled)
2473 		goto out;
2474 
2475 	if (!psr->pause_counter) {
2476 		drm_warn(display->drm, "Unbalanced PSR pause/resume!\n");
2477 		goto out;
2478 	}
2479 
2480 	if (--intel_dp->psr.pause_counter == 0) {
2481 		intel_psr_activate(intel_dp);
2482 		/* re-arm cancelled dc3co work from pause */
2483 		if (intel_dp->psr.dc3co_allowed)
2484 			mod_delayed_work(display->wq.unordered, &intel_dp->psr.dc3co_work,
2485 					 intel_dp->psr.dc3co_exit_delay);
2486 	}
2487 
2488 out:
2489 	mutex_unlock(&psr->lock);
2490 }
2491 
2492 /**
2493  * intel_psr_needs_vblank_notification - Check if PSR need vblank enable/disable
2494  * notification.
2495  * @crtc_state: CRTC status
2496  *
2497  * We need to block DC6 entry in case of Panel Replay as enabling VBI doesn't
2498  * prevent it in case of Panel Replay. Panel Replay switches main link off on
2499  * DC entry. This means vblank interrupts are not fired and is a problem if
2500  * user-space is polling for vblank events. Also Wa_16025596647 needs
2501  * information when vblank is enabled/disabled.
2502  */
intel_psr_needs_vblank_notification(const struct intel_crtc_state * crtc_state)2503 bool intel_psr_needs_vblank_notification(const struct intel_crtc_state *crtc_state)
2504 {
2505 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2506 	struct intel_display *display = to_intel_display(crtc_state);
2507 	struct intel_encoder *encoder;
2508 
2509 	for_each_encoder_on_crtc(crtc->base.dev, &crtc->base, encoder) {
2510 		struct intel_dp *intel_dp;
2511 
2512 		if (!intel_encoder_is_dp(encoder))
2513 			continue;
2514 
2515 		intel_dp = enc_to_intel_dp(encoder);
2516 
2517 		if (!intel_dp_is_edp(intel_dp))
2518 			continue;
2519 
2520 		if (CAN_PANEL_REPLAY(intel_dp))
2521 			return true;
2522 
2523 		if ((DISPLAY_VER(display) == 20 ||
2524 		     IS_DISPLAY_VERx100_STEP(display, 3000, STEP_A0, STEP_B0)) &&
2525 		    CAN_PSR(intel_dp))
2526 			return true;
2527 	}
2528 
2529 	return false;
2530 }
2531 
2532 /**
2533  * intel_psr_trigger_frame_change_event - Trigger "Frame Change" event
2534  * @dsb: DSB context
2535  * @state: the atomic state
2536  * @crtc: the CRTC
2537  *
2538  * Generate PSR "Frame Change" event.
2539  */
intel_psr_trigger_frame_change_event(struct intel_dsb * dsb,struct intel_atomic_state * state,struct intel_crtc * crtc)2540 void intel_psr_trigger_frame_change_event(struct intel_dsb *dsb,
2541 					  struct intel_atomic_state *state,
2542 					  struct intel_crtc *crtc)
2543 {
2544 	const struct intel_crtc_state *crtc_state =
2545 		intel_pre_commit_crtc_state(state, crtc);
2546 	struct intel_display *display = to_intel_display(crtc);
2547 
2548 	if (!crtc_state->has_psr || intel_psr_use_trans_push(crtc_state))
2549 		return;
2550 
2551 	intel_de_write_dsb(display, dsb,
2552 			   CURSURFLIVE(display, crtc->pipe), 0);
2553 }
2554 
2555 /**
2556  * intel_psr_min_set_context_latency - Minimum 'set context latency' lines needed by PSR
2557  * @crtc_state: the crtc state
2558  *
2559  * Return minimum SCL lines/delay needed by PSR.
2560  */
intel_psr_min_set_context_latency(const struct intel_crtc_state * crtc_state)2561 int intel_psr_min_set_context_latency(const struct intel_crtc_state *crtc_state)
2562 {
2563 
2564 	return _intel_psr_min_set_context_latency(crtc_state,
2565 						  crtc_state->has_panel_replay,
2566 						  crtc_state->has_sel_update);
2567 }
2568 
man_trk_ctl_enable_bit_get(struct intel_display * display)2569 static u32 man_trk_ctl_enable_bit_get(struct intel_display *display)
2570 {
2571 	return display->platform.alderlake_p || DISPLAY_VER(display) >= 14 ? 0 :
2572 		PSR2_MAN_TRK_CTL_ENABLE;
2573 }
2574 
man_trk_ctl_single_full_frame_bit_get(struct intel_display * display)2575 static u32 man_trk_ctl_single_full_frame_bit_get(struct intel_display *display)
2576 {
2577 	return display->platform.alderlake_p || DISPLAY_VER(display) >= 14 ?
2578 	       ADLP_PSR2_MAN_TRK_CTL_SF_SINGLE_FULL_FRAME :
2579 	       PSR2_MAN_TRK_CTL_SF_SINGLE_FULL_FRAME;
2580 }
2581 
man_trk_ctl_partial_frame_bit_get(struct intel_display * display)2582 static u32 man_trk_ctl_partial_frame_bit_get(struct intel_display *display)
2583 {
2584 	return display->platform.alderlake_p || DISPLAY_VER(display) >= 14 ?
2585 	       ADLP_PSR2_MAN_TRK_CTL_SF_PARTIAL_FRAME_UPDATE :
2586 	       PSR2_MAN_TRK_CTL_SF_PARTIAL_FRAME_UPDATE;
2587 }
2588 
man_trk_ctl_continuos_full_frame(struct intel_display * display)2589 static u32 man_trk_ctl_continuos_full_frame(struct intel_display *display)
2590 {
2591 	return display->platform.alderlake_p || DISPLAY_VER(display) >= 14 ?
2592 	       ADLP_PSR2_MAN_TRK_CTL_SF_CONTINUOS_FULL_FRAME :
2593 	       PSR2_MAN_TRK_CTL_SF_CONTINUOS_FULL_FRAME;
2594 }
2595 
intel_psr_force_update(struct intel_dp * intel_dp)2596 static void intel_psr_force_update(struct intel_dp *intel_dp)
2597 {
2598 	struct intel_display *display = to_intel_display(intel_dp);
2599 
2600 	/*
2601 	 * Display WA #0884: skl+
2602 	 * This documented WA for bxt can be safely applied
2603 	 * broadly so we can force HW tracking to exit PSR
2604 	 * instead of disabling and re-enabling.
2605 	 * Workaround tells us to write 0 to CUR_SURFLIVE_A,
2606 	 * but it makes more sense write to the current active
2607 	 * pipe.
2608 	 *
2609 	 * This workaround do not exist for platforms with display 10 or newer
2610 	 * but testing proved that it works for up display 13, for newer
2611 	 * than that testing will be needed.
2612 	 */
2613 	intel_de_write(display, CURSURFLIVE(display, intel_dp->psr.pipe), 0);
2614 }
2615 
intel_psr2_program_trans_man_trk_ctl(struct intel_dsb * dsb,const struct intel_crtc_state * crtc_state)2616 void intel_psr2_program_trans_man_trk_ctl(struct intel_dsb *dsb,
2617 					  const struct intel_crtc_state *crtc_state)
2618 {
2619 	struct intel_display *display = to_intel_display(crtc_state);
2620 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2621 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2622 	struct intel_encoder *encoder;
2623 
2624 	if (!crtc_state->enable_psr2_sel_fetch)
2625 		return;
2626 
2627 	for_each_intel_encoder_mask_with_psr(display->drm, encoder,
2628 					     crtc_state->uapi.encoder_mask) {
2629 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2630 
2631 		if (!dsb)
2632 			lockdep_assert_held(&intel_dp->psr.lock);
2633 
2634 		if (DISPLAY_VER(display) < 20 && intel_dp->psr.psr2_sel_fetch_cff_enabled)
2635 			return;
2636 		break;
2637 	}
2638 
2639 	intel_de_write_dsb(display, dsb,
2640 			   PSR2_MAN_TRK_CTL(display, cpu_transcoder),
2641 			   crtc_state->psr2_man_track_ctl);
2642 
2643 	if (!crtc_state->enable_psr2_su_region_et)
2644 		return;
2645 
2646 	intel_de_write_dsb(display, dsb, PIPE_SRCSZ_ERLY_TPT(crtc->pipe),
2647 			   crtc_state->pipe_srcsz_early_tpt);
2648 
2649 	if (!crtc_state->dsc.compression_enable)
2650 		return;
2651 
2652 	intel_dsc_su_et_parameters_configure(dsb, encoder, crtc_state,
2653 					     drm_rect_height(&crtc_state->psr2_su_area));
2654 }
2655 
psr2_man_trk_ctl_calc(struct intel_crtc_state * crtc_state,bool full_update)2656 static void psr2_man_trk_ctl_calc(struct intel_crtc_state *crtc_state,
2657 				  bool full_update)
2658 {
2659 	struct intel_display *display = to_intel_display(crtc_state);
2660 	u32 val = man_trk_ctl_enable_bit_get(display);
2661 
2662 	/* SF partial frame enable has to be set even on full update */
2663 	val |= man_trk_ctl_partial_frame_bit_get(display);
2664 
2665 	if (full_update) {
2666 		val |= man_trk_ctl_continuos_full_frame(display);
2667 		goto exit;
2668 	}
2669 
2670 	if (crtc_state->psr2_su_area.y1 == -1)
2671 		goto exit;
2672 
2673 	if (display->platform.alderlake_p || DISPLAY_VER(display) >= 14) {
2674 		val |= ADLP_PSR2_MAN_TRK_CTL_SU_REGION_START_ADDR(crtc_state->psr2_su_area.y1);
2675 		val |= ADLP_PSR2_MAN_TRK_CTL_SU_REGION_END_ADDR(crtc_state->psr2_su_area.y2 - 1);
2676 	} else {
2677 		drm_WARN_ON(crtc_state->uapi.crtc->dev,
2678 			    crtc_state->psr2_su_area.y1 % 4 ||
2679 			    crtc_state->psr2_su_area.y2 % 4);
2680 
2681 		val |= PSR2_MAN_TRK_CTL_SU_REGION_START_ADDR(
2682 			crtc_state->psr2_su_area.y1 / 4 + 1);
2683 		val |= PSR2_MAN_TRK_CTL_SU_REGION_END_ADDR(
2684 			crtc_state->psr2_su_area.y2 / 4 + 1);
2685 	}
2686 exit:
2687 	crtc_state->psr2_man_track_ctl = val;
2688 }
2689 
psr2_pipe_srcsz_early_tpt_calc(struct intel_crtc_state * crtc_state,bool full_update)2690 static u32 psr2_pipe_srcsz_early_tpt_calc(struct intel_crtc_state *crtc_state,
2691 					  bool full_update)
2692 {
2693 	int width, height;
2694 
2695 	if (!crtc_state->enable_psr2_su_region_et || full_update)
2696 		return 0;
2697 
2698 	width = drm_rect_width(&crtc_state->psr2_su_area);
2699 	height = drm_rect_height(&crtc_state->psr2_su_area);
2700 
2701 	return PIPESRC_WIDTH(width - 1) | PIPESRC_HEIGHT(height - 1);
2702 }
2703 
clip_area_update(struct drm_rect * overlap_damage_area,struct drm_rect * damage_area,struct drm_rect * display_area)2704 static void clip_area_update(struct drm_rect *overlap_damage_area,
2705 			     struct drm_rect *damage_area,
2706 			     struct drm_rect *display_area)
2707 {
2708 	if (!drm_rect_intersect(damage_area, display_area))
2709 		return;
2710 
2711 	if (overlap_damage_area->y1 == -1) {
2712 		overlap_damage_area->y1 = damage_area->y1;
2713 		overlap_damage_area->y2 = damage_area->y2;
2714 		return;
2715 	}
2716 
2717 	if (damage_area->y1 < overlap_damage_area->y1)
2718 		overlap_damage_area->y1 = damage_area->y1;
2719 
2720 	if (damage_area->y2 > overlap_damage_area->y2)
2721 		overlap_damage_area->y2 = damage_area->y2;
2722 }
2723 
intel_psr2_sel_fetch_pipe_alignment(struct intel_crtc_state * crtc_state)2724 static bool intel_psr2_sel_fetch_pipe_alignment(struct intel_crtc_state *crtc_state)
2725 {
2726 	struct intel_display *display = to_intel_display(crtc_state);
2727 	const struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
2728 	u16 y_alignment;
2729 	bool su_area_changed = false;
2730 
2731 	/* ADLP aligns the SU region to vdsc slice height in case dsc is enabled */
2732 	if (crtc_state->dsc.compression_enable &&
2733 	    (display->platform.alderlake_p || DISPLAY_VER(display) >= 14))
2734 		y_alignment = vdsc_cfg->slice_height;
2735 	else
2736 		y_alignment = crtc_state->su_y_granularity;
2737 
2738 	if (crtc_state->psr2_su_area.y1 % y_alignment) {
2739 		crtc_state->psr2_su_area.y1 -= crtc_state->psr2_su_area.y1 % y_alignment;
2740 		su_area_changed = true;
2741 	}
2742 
2743 	if (crtc_state->psr2_su_area.y2 % y_alignment) {
2744 		crtc_state->psr2_su_area.y2 = ((crtc_state->psr2_su_area.y2 /
2745 						y_alignment) + 1) * y_alignment;
2746 		su_area_changed = true;
2747 	}
2748 
2749 	return su_area_changed;
2750 }
2751 
2752 /*
2753  * When early transport is in use we need to extend SU area to cover
2754  * cursor fully when cursor is in SU area.
2755  */
2756 static void
intel_psr2_sel_fetch_et_alignment(struct intel_atomic_state * state,struct intel_crtc * crtc,struct drm_rect * display_area,bool * cursor_in_su_area)2757 intel_psr2_sel_fetch_et_alignment(struct intel_atomic_state *state,
2758 				  struct intel_crtc *crtc,
2759 				  struct drm_rect *display_area,
2760 				  bool *cursor_in_su_area)
2761 {
2762 	struct intel_crtc_state *crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
2763 	struct intel_plane_state *new_plane_state;
2764 	struct intel_plane *plane;
2765 	int i;
2766 
2767 	if (!crtc_state->enable_psr2_su_region_et)
2768 		return;
2769 
2770 	for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
2771 		struct drm_rect inter;
2772 
2773 		if (new_plane_state->hw.crtc != crtc_state->uapi.crtc)
2774 			continue;
2775 
2776 		if (plane->id != PLANE_CURSOR)
2777 			continue;
2778 
2779 		if (!new_plane_state->uapi.visible)
2780 			continue;
2781 
2782 		inter = crtc_state->psr2_su_area;
2783 		if (!drm_rect_intersect(&inter, &new_plane_state->uapi.dst))
2784 			continue;
2785 
2786 		clip_area_update(&crtc_state->psr2_su_area, &new_plane_state->uapi.dst,
2787 				 display_area);
2788 		*cursor_in_su_area = true;
2789 	}
2790 }
2791 
2792 /*
2793  * TODO: Not clear how to handle planes with negative position,
2794  * also planes are not updated if they have a negative X
2795  * position so for now doing a full update in this cases
2796  *
2797  * Plane scaling and rotation is not supported by selective fetch and both
2798  * properties can change without a modeset, so need to be check at every
2799  * atomic commit.
2800  */
psr2_sel_fetch_plane_state_supported(const struct intel_plane_state * plane_state)2801 static bool psr2_sel_fetch_plane_state_supported(const struct intel_plane_state *plane_state)
2802 {
2803 	if (plane_state->uapi.dst.y1 < 0 ||
2804 	    plane_state->uapi.dst.x1 < 0 ||
2805 	    plane_state->scaler_id >= 0 ||
2806 	    plane_state->hw.rotation != DRM_MODE_ROTATE_0)
2807 		return false;
2808 
2809 	return true;
2810 }
2811 
2812 /*
2813  * Check for pipe properties that is not supported by selective fetch.
2814  *
2815  * TODO: pipe scaling causes a modeset but skl_update_scaler_crtc() is executed
2816  * after intel_psr_compute_config(), so for now keeping PSR2 selective fetch
2817  * enabled and going to the full update path.
2818  */
psr2_sel_fetch_pipe_state_supported(const struct intel_crtc_state * crtc_state)2819 static bool psr2_sel_fetch_pipe_state_supported(const struct intel_crtc_state *crtc_state)
2820 {
2821 	if (crtc_state->scaler_state.scaler_id >= 0 ||
2822 	    crtc_state->async_flip_planes)
2823 		return false;
2824 
2825 	return true;
2826 }
2827 
2828 /* Wa 14019834836 */
intel_psr_apply_pr_link_on_su_wa(struct intel_crtc_state * crtc_state)2829 static void intel_psr_apply_pr_link_on_su_wa(struct intel_crtc_state *crtc_state)
2830 {
2831 	struct intel_display *display = to_intel_display(crtc_state);
2832 	struct intel_encoder *encoder;
2833 	int hactive_limit;
2834 
2835 	if (crtc_state->psr2_su_area.y1 != 0 ||
2836 	    crtc_state->psr2_su_area.y2 != 0)
2837 		return;
2838 
2839 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
2840 		hactive_limit = intel_dp_is_uhbr(crtc_state) ? 1230 : 546;
2841 	else
2842 		hactive_limit = intel_dp_is_uhbr(crtc_state) ? 615 : 273;
2843 
2844 	if (crtc_state->hw.adjusted_mode.hdisplay < hactive_limit)
2845 		return;
2846 
2847 	for_each_intel_encoder_mask_with_psr(display->drm, encoder,
2848 					     crtc_state->uapi.encoder_mask) {
2849 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2850 
2851 		if (!intel_dp_is_edp(intel_dp) &&
2852 		    intel_dp->psr.panel_replay_enabled &&
2853 		    intel_dp->psr.sel_update_enabled) {
2854 			crtc_state->psr2_su_area.y2++;
2855 			return;
2856 		}
2857 	}
2858 }
2859 
2860 static void
intel_psr_apply_su_area_workarounds(struct intel_crtc_state * crtc_state)2861 intel_psr_apply_su_area_workarounds(struct intel_crtc_state *crtc_state)
2862 {
2863 	struct intel_display *display = to_intel_display(crtc_state);
2864 
2865 	/* Wa_14014971492 */
2866 	if (!crtc_state->has_panel_replay &&
2867 	    ((IS_DISPLAY_VERx100_STEP(display, 1400, STEP_A0, STEP_B0) ||
2868 	      display->platform.alderlake_p || display->platform.tigerlake)) &&
2869 	    crtc_state->splitter.enable)
2870 		crtc_state->psr2_su_area.y1 = 0;
2871 
2872 	if (intel_display_wa(display, INTEL_DISPLAY_WA_16029024088) &&
2873 	    crtc_state->req_psr2_sdp_prior_scanline &&
2874 	    !crtc_state->enable_psr2_su_region_et)
2875 		crtc_state->psr2_su_area.y1 = 0;
2876 
2877 	/* Wa 14019834836 */
2878 	if (DISPLAY_VER(display) == 30)
2879 		intel_psr_apply_pr_link_on_su_wa(crtc_state);
2880 }
2881 
intel_psr2_sel_fetch_update(struct intel_atomic_state * state,struct intel_crtc * crtc)2882 int intel_psr2_sel_fetch_update(struct intel_atomic_state *state,
2883 				struct intel_crtc *crtc)
2884 {
2885 	struct intel_display *display = to_intel_display(state);
2886 	const struct intel_crtc_state *old_crtc_state =
2887 		intel_atomic_get_old_crtc_state(state, crtc);
2888 	struct intel_crtc_state *crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
2889 	struct intel_plane_state *new_plane_state, *old_plane_state;
2890 	struct intel_plane *plane;
2891 	struct drm_rect display_area = {
2892 		.x1 = 0,
2893 		.y1 = 0,
2894 		.x2 = crtc_state->hw.adjusted_mode.crtc_hdisplay,
2895 		.y2 = crtc_state->hw.adjusted_mode.crtc_vdisplay,
2896 	};
2897 	bool full_update = false, su_area_changed;
2898 	int i, ret;
2899 
2900 	/*
2901 	 * Selective fetch is not always usable, for instance it is dropped
2902 	 * while pipe CRC is active. The planes keep their selective fetch
2903 	 * enable bit set in hardware over that, and a plane disabled while
2904 	 * selective fetch is off never gets the bit cleared. Once selective
2905 	 * fetch comes back the hardware would resume fetching for a plane that
2906 	 * is no longer enabled and keep its DDB range reserved, so have the
2907 	 * plane update drop the bit for every plane of the pipe as selective
2908 	 * fetch is turned off.
2909 	 */
2910 	crtc_state->clear_psr2_sel_fetch = old_crtc_state->enable_psr2_sel_fetch &&
2911 		!crtc_state->enable_psr2_sel_fetch;
2912 
2913 	if (!crtc_state->enable_psr2_sel_fetch)
2914 		return 0;
2915 
2916 	if (!psr2_sel_fetch_pipe_state_supported(crtc_state)) {
2917 		full_update = true;
2918 		goto skip_sel_fetch_set_loop;
2919 	}
2920 
2921 	crtc_state->psr2_su_area.x1 = 0;
2922 	crtc_state->psr2_su_area.y1 = -1;
2923 	crtc_state->psr2_su_area.x2 = drm_rect_width(&display_area);
2924 	crtc_state->psr2_su_area.y2 = -1;
2925 
2926 	/*
2927 	 * Calculate minimal selective fetch area of each plane and calculate
2928 	 * the pipe damaged area.
2929 	 * In the next loop the plane selective fetch area will actually be set
2930 	 * using whole pipe damaged area.
2931 	 */
2932 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
2933 					     new_plane_state, i) {
2934 		struct drm_rect src, damaged_area = { .x1 = 0, .y1 = -1,
2935 						      .x2 = INT_MAX };
2936 
2937 		if (new_plane_state->hw.crtc != crtc_state->uapi.crtc)
2938 			continue;
2939 
2940 		if (!new_plane_state->uapi.visible &&
2941 		    !old_plane_state->uapi.visible)
2942 			continue;
2943 
2944 		if (!psr2_sel_fetch_plane_state_supported(new_plane_state)) {
2945 			full_update = true;
2946 			break;
2947 		}
2948 
2949 		/*
2950 		 * If visibility or plane moved, mark the whole plane area as
2951 		 * damaged as it needs to be complete redraw in the new and old
2952 		 * position.
2953 		 */
2954 		if (new_plane_state->uapi.visible != old_plane_state->uapi.visible ||
2955 		    !drm_rect_equals(&new_plane_state->uapi.dst,
2956 				     &old_plane_state->uapi.dst)) {
2957 			if (old_plane_state->uapi.visible) {
2958 				damaged_area.y1 = old_plane_state->uapi.dst.y1;
2959 				damaged_area.y2 = old_plane_state->uapi.dst.y2;
2960 				clip_area_update(&crtc_state->psr2_su_area, &damaged_area,
2961 						 &display_area);
2962 			}
2963 
2964 			if (new_plane_state->uapi.visible) {
2965 				damaged_area.y1 = new_plane_state->uapi.dst.y1;
2966 				damaged_area.y2 = new_plane_state->uapi.dst.y2;
2967 				clip_area_update(&crtc_state->psr2_su_area, &damaged_area,
2968 						 &display_area);
2969 			}
2970 			continue;
2971 		} else if (new_plane_state->uapi.alpha != old_plane_state->uapi.alpha) {
2972 			/* If alpha changed mark the whole plane area as damaged */
2973 			damaged_area.y1 = new_plane_state->uapi.dst.y1;
2974 			damaged_area.y2 = new_plane_state->uapi.dst.y2;
2975 			clip_area_update(&crtc_state->psr2_su_area, &damaged_area,
2976 					 &display_area);
2977 			continue;
2978 		}
2979 
2980 		src = drm_plane_state_src(&new_plane_state->uapi);
2981 		drm_rect_fp_to_int(&src, &src);
2982 
2983 		if (!drm_atomic_helper_damage_merged(&old_plane_state->uapi,
2984 						     &new_plane_state->uapi, &damaged_area))
2985 			continue;
2986 
2987 		damaged_area.y1 += new_plane_state->uapi.dst.y1 - src.y1;
2988 		damaged_area.y2 += new_plane_state->uapi.dst.y1 - src.y1;
2989 		damaged_area.x1 += new_plane_state->uapi.dst.x1 - src.x1;
2990 		damaged_area.x2 += new_plane_state->uapi.dst.x1 - src.x1;
2991 
2992 		clip_area_update(&crtc_state->psr2_su_area, &damaged_area, &display_area);
2993 	}
2994 
2995 	/*
2996 	 * TODO: For now we are just using full update in case
2997 	 * selective fetch area calculation fails. To optimize this we
2998 	 * should identify cases where this happens and fix the area
2999 	 * calculation for those.
3000 	 */
3001 	if (crtc_state->psr2_su_area.y1 == -1) {
3002 		drm_info_once(display->drm,
3003 			      "Selective fetch area calculation failed in pipe %c\n",
3004 			      pipe_name(crtc->pipe));
3005 		full_update = true;
3006 	}
3007 
3008 	if (full_update)
3009 		goto skip_sel_fetch_set_loop;
3010 
3011 	intel_psr_apply_su_area_workarounds(crtc_state);
3012 
3013 	ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
3014 	if (ret)
3015 		return ret;
3016 
3017 	do {
3018 		bool cursor_in_su_area = false;
3019 
3020 		/*
3021 		 * Adjust su area to cover cursor fully as necessary
3022 		 * (early transport). This needs to be done after
3023 		 * drm_atomic_add_affected_planes to ensure visible
3024 		 * cursor is added into affected planes even when
3025 		 * cursor is not updated by itself.
3026 		 */
3027 		intel_psr2_sel_fetch_et_alignment(state, crtc, &display_area,
3028 						  &cursor_in_su_area);
3029 
3030 		su_area_changed = intel_psr2_sel_fetch_pipe_alignment(crtc_state);
3031 
3032 		/*
3033 		 * If the cursor was outside the SU area before
3034 		 * alignment, the alignment step (which only expands
3035 		 * SU) may pull the cursor partially inside, so we
3036 		 * must run ET alignment again to fully cover it. But
3037 		 * if the cursor was already fully inside before
3038 		 * alignment, expanding the SU area won't change that,
3039 		 * so no further work is needed.
3040 		 */
3041 		if (cursor_in_su_area)
3042 			break;
3043 	} while (su_area_changed);
3044 
3045 	/*
3046 	 * Now that we have the pipe damaged area check if it intersect with
3047 	 * every plane, if it does set the plane selective fetch area.
3048 	 */
3049 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
3050 					     new_plane_state, i) {
3051 		struct drm_rect *sel_fetch_area, inter;
3052 		struct intel_plane *linked = new_plane_state->planar_linked_plane;
3053 
3054 		if (new_plane_state->hw.crtc != crtc_state->uapi.crtc ||
3055 		    !new_plane_state->uapi.visible)
3056 			continue;
3057 
3058 		inter = crtc_state->psr2_su_area;
3059 		sel_fetch_area = &new_plane_state->psr2_sel_fetch_area;
3060 		if (!drm_rect_intersect(&inter, &new_plane_state->uapi.dst)) {
3061 			sel_fetch_area->y1 = -1;
3062 			sel_fetch_area->y2 = -1;
3063 			/*
3064 			 * if plane sel fetch was previously enabled ->
3065 			 * disable it
3066 			 */
3067 			if (drm_rect_height(&old_plane_state->psr2_sel_fetch_area) > 0)
3068 				crtc_state->update_planes |= BIT(plane->id);
3069 
3070 			continue;
3071 		}
3072 
3073 		if (!psr2_sel_fetch_plane_state_supported(new_plane_state)) {
3074 			full_update = true;
3075 			break;
3076 		}
3077 
3078 		sel_fetch_area = &new_plane_state->psr2_sel_fetch_area;
3079 		sel_fetch_area->y1 = inter.y1 - new_plane_state->uapi.dst.y1;
3080 		sel_fetch_area->y2 = inter.y2 - new_plane_state->uapi.dst.y1;
3081 		crtc_state->update_planes |= BIT(plane->id);
3082 
3083 		/*
3084 		 * Sel_fetch_area is calculated for UV plane. Use
3085 		 * same area for Y plane as well.
3086 		 */
3087 		if (linked) {
3088 			struct intel_plane_state *linked_new_plane_state;
3089 			struct drm_rect *linked_sel_fetch_area;
3090 
3091 			linked_new_plane_state = intel_atomic_get_plane_state(state, linked);
3092 			if (IS_ERR(linked_new_plane_state))
3093 				return PTR_ERR(linked_new_plane_state);
3094 
3095 			linked_sel_fetch_area = &linked_new_plane_state->psr2_sel_fetch_area;
3096 			linked_sel_fetch_area->y1 = sel_fetch_area->y1;
3097 			linked_sel_fetch_area->y2 = sel_fetch_area->y2;
3098 			crtc_state->update_planes |= BIT(linked->id);
3099 		}
3100 	}
3101 
3102 skip_sel_fetch_set_loop:
3103 	if (full_update)
3104 		clip_area_update(&crtc_state->psr2_su_area, &display_area,
3105 				 &display_area);
3106 
3107 	psr2_man_trk_ctl_calc(crtc_state, full_update);
3108 	crtc_state->pipe_srcsz_early_tpt =
3109 		psr2_pipe_srcsz_early_tpt_calc(crtc_state, full_update);
3110 	return 0;
3111 }
3112 
intel_psr2_panic_force_full_update(const struct intel_crtc_state * crtc_state)3113 void intel_psr2_panic_force_full_update(const struct intel_crtc_state *crtc_state)
3114 {
3115 	struct intel_display *display = to_intel_display(crtc_state);
3116 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3117 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
3118 	u32 val = man_trk_ctl_enable_bit_get(display);
3119 
3120 	/* SF partial frame enable has to be set even on full update */
3121 	val |= man_trk_ctl_partial_frame_bit_get(display);
3122 	val |= man_trk_ctl_continuos_full_frame(display);
3123 
3124 	/* Directly write the register */
3125 	intel_de_write_fw(display, PSR2_MAN_TRK_CTL(display, cpu_transcoder), val);
3126 
3127 	if (!crtc_state->enable_psr2_su_region_et)
3128 		return;
3129 
3130 	intel_de_write_fw(display, PIPE_SRCSZ_ERLY_TPT(crtc->pipe), 0);
3131 }
3132 
intel_psr_pre_plane_update(struct intel_atomic_state * state,struct intel_crtc * crtc)3133 void intel_psr_pre_plane_update(struct intel_atomic_state *state,
3134 				struct intel_crtc *crtc)
3135 {
3136 	struct intel_display *display = to_intel_display(state);
3137 	const struct intel_crtc_state *old_crtc_state =
3138 		intel_atomic_get_old_crtc_state(state, crtc);
3139 	const struct intel_crtc_state *new_crtc_state =
3140 		intel_atomic_get_new_crtc_state(state, crtc);
3141 	struct intel_encoder *encoder;
3142 
3143 	if (!HAS_PSR(display))
3144 		return;
3145 
3146 	for_each_intel_encoder_mask_with_psr(state->base.dev, encoder,
3147 					     old_crtc_state->uapi.encoder_mask) {
3148 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3149 		struct intel_psr *psr = &intel_dp->psr;
3150 
3151 		mutex_lock(&psr->lock);
3152 
3153 		if (!new_crtc_state->has_psr)
3154 			psr->no_psr_reason = new_crtc_state->no_psr_reason;
3155 
3156 		if (psr->enabled) {
3157 			/*
3158 			 * Reasons to disable:
3159 			 * - PSR disabled in new state
3160 			 * - All planes will go inactive
3161 			 * - Changing between PSR versions
3162 			 * - Region Early Transport changing
3163 			 * - Display WA #1136: skl, bxt
3164 			 */
3165 			if (intel_crtc_needs_modeset(new_crtc_state) ||
3166 			    new_crtc_state->update_m_n ||
3167 			    new_crtc_state->update_lrr ||
3168 			    !new_crtc_state->has_psr ||
3169 			    !new_crtc_state->active_planes ||
3170 			    new_crtc_state->has_sel_update != psr->sel_update_enabled ||
3171 			    new_crtc_state->enable_psr2_su_region_et != psr->su_region_et_enabled ||
3172 			    new_crtc_state->has_panel_replay != psr->panel_replay_enabled ||
3173 			    (DISPLAY_VER(display) < 11 && new_crtc_state->wm_level_disabled))
3174 				intel_psr_disable_locked(intel_dp);
3175 			else if (new_crtc_state->wm_level_disabled)
3176 				/* Wa_14015648006 */
3177 				wm_optimization_wa(intel_dp, new_crtc_state);
3178 		}
3179 
3180 		mutex_unlock(&psr->lock);
3181 	}
3182 }
3183 
3184 static void
verify_panel_replay_dsc_state(const struct intel_crtc_state * crtc_state)3185 verify_panel_replay_dsc_state(const struct intel_crtc_state *crtc_state)
3186 {
3187 	struct intel_display *display = to_intel_display(crtc_state);
3188 
3189 	if (!crtc_state->has_panel_replay)
3190 		return;
3191 
3192 	drm_WARN_ON(display->drm,
3193 		    intel_dsc_enabled_on_link(crtc_state) &&
3194 		    crtc_state->panel_replay_dsc_support ==
3195 		    INTEL_DP_PANEL_REPLAY_DSC_NOT_SUPPORTED);
3196 }
3197 
intel_psr_post_plane_update(struct intel_atomic_state * state,struct intel_crtc * crtc)3198 void intel_psr_post_plane_update(struct intel_atomic_state *state,
3199 				 struct intel_crtc *crtc)
3200 {
3201 	struct intel_display *display = to_intel_display(state);
3202 	const struct intel_crtc_state *crtc_state =
3203 		intel_atomic_get_new_crtc_state(state, crtc);
3204 	struct intel_encoder *encoder;
3205 	bool dc3co_allowed;
3206 
3207 	if (!crtc_state->has_psr)
3208 		return;
3209 
3210 	dc3co_allowed = intel_display_power_dc3co_allowed(display);
3211 
3212 	verify_panel_replay_dsc_state(crtc_state);
3213 
3214 	for_each_intel_encoder_mask_with_psr(state->base.dev, encoder,
3215 					     crtc_state->uapi.encoder_mask) {
3216 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3217 		struct intel_psr *psr = &intel_dp->psr;
3218 		bool keep_disabled = false;
3219 
3220 		mutex_lock(&psr->lock);
3221 
3222 		drm_WARN_ON(display->drm,
3223 			    psr->enabled && !crtc_state->active_planes);
3224 
3225 		if (psr->sink_not_reliable)
3226 			keep_disabled = true;
3227 
3228 		if (!crtc_state->active_planes) {
3229 			psr->no_psr_reason = "All planes inactive";
3230 			keep_disabled = true;
3231 		}
3232 
3233 		/* Display WA #1136: skl, bxt */
3234 		if (DISPLAY_VER(display) < 11 && crtc_state->wm_level_disabled) {
3235 			psr->no_psr_reason = "Workaround #1136 for skl, bxt";
3236 			keep_disabled = true;
3237 		}
3238 
3239 		intel_dp->psr.dc3co_allowed = dc3co_allowed;
3240 
3241 		if (!psr->enabled && !keep_disabled)
3242 			intel_psr_enable_locked(intel_dp, crtc_state);
3243 		else if (psr->enabled && !crtc_state->wm_level_disabled)
3244 			/* Wa_14015648006 */
3245 			wm_optimization_wa(intel_dp, crtc_state);
3246 
3247 		/* Force a PSR exit when enabling CRC to avoid CRC timeouts */
3248 		if (crtc_state->crc_enabled && psr->enabled)
3249 			intel_psr_force_update(intel_dp);
3250 
3251 		/*
3252 		 * Clear possible busy bits in case we have
3253 		 * invalidate -> flip -> flush sequence.
3254 		 */
3255 		intel_dp->psr.busy_frontbuffer_bits = 0;
3256 
3257 		if (intel_dp->psr.dc3co_allowed) {
3258 			mod_delayed_work(display->wq.unordered, &intel_dp->psr.dc3co_work,
3259 					 intel_dp->psr.dc3co_exit_delay);
3260 		}
3261 
3262 		mutex_unlock(&psr->lock);
3263 	}
3264 }
3265 
3266 /*
3267  * From bspec: Panel Self Refresh (BDW+)
3268  * Max. time for PSR to idle = Inverse of the refresh rate + 6 ms of
3269  * exit training time + 1.5 ms of aux channel handshake. 50 ms is
3270  * defensive enough to cover everything.
3271  */
3272 #define PSR_IDLE_TIMEOUT_MS 50
3273 
3274 static int
_psr2_ready_for_pipe_update_locked(const struct intel_crtc_state * new_crtc_state,struct intel_dsb * dsb)3275 _psr2_ready_for_pipe_update_locked(const struct intel_crtc_state *new_crtc_state,
3276 				   struct intel_dsb *dsb)
3277 {
3278 	struct intel_display *display = to_intel_display(new_crtc_state);
3279 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
3280 
3281 	/*
3282 	 * Any state lower than EDP_PSR2_STATUS_STATE_DEEP_SLEEP is enough.
3283 	 * As all higher states has bit 4 of PSR2 state set we can just wait for
3284 	 * EDP_PSR2_STATUS_STATE_DEEP_SLEEP to be cleared.
3285 	 */
3286 	if (dsb) {
3287 		intel_dsb_poll(dsb, EDP_PSR2_STATUS(display, cpu_transcoder),
3288 			       EDP_PSR2_STATUS_STATE_DEEP_SLEEP, 0, 200,
3289 			       PSR_IDLE_TIMEOUT_MS * 1000 / 200);
3290 		return true;
3291 	}
3292 
3293 	return intel_de_wait_for_clear_ms(display,
3294 				       EDP_PSR2_STATUS(display, cpu_transcoder),
3295 				       EDP_PSR2_STATUS_STATE_DEEP_SLEEP,
3296 				       PSR_IDLE_TIMEOUT_MS);
3297 }
3298 
3299 static int
_psr1_ready_for_pipe_update_locked(const struct intel_crtc_state * new_crtc_state,struct intel_dsb * dsb)3300 _psr1_ready_for_pipe_update_locked(const struct intel_crtc_state *new_crtc_state,
3301 				   struct intel_dsb *dsb)
3302 {
3303 	struct intel_display *display = to_intel_display(new_crtc_state);
3304 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
3305 
3306 	if (dsb) {
3307 		intel_dsb_poll(dsb, psr_status_reg(display, cpu_transcoder),
3308 			       EDP_PSR_STATUS_STATE_MASK, 0, 200,
3309 			       PSR_IDLE_TIMEOUT_MS * 1000 / 200);
3310 		return true;
3311 	}
3312 
3313 	return intel_de_wait_for_clear_ms(display,
3314 				       psr_status_reg(display, cpu_transcoder),
3315 				       EDP_PSR_STATUS_STATE_MASK,
3316 				       PSR_IDLE_TIMEOUT_MS);
3317 }
3318 
3319 /**
3320  * intel_psr_wait_for_idle_locked - wait for PSR be ready for a pipe update
3321  * @new_crtc_state: new CRTC state
3322  *
3323  * This function is expected to be called from pipe_update_start() where it is
3324  * not expected to race with PSR enable or disable.
3325  */
intel_psr_wait_for_idle_locked(const struct intel_crtc_state * new_crtc_state)3326 void intel_psr_wait_for_idle_locked(const struct intel_crtc_state *new_crtc_state)
3327 {
3328 	struct intel_display *display = to_intel_display(new_crtc_state);
3329 	struct intel_encoder *encoder;
3330 
3331 	if (!new_crtc_state->has_psr)
3332 		return;
3333 
3334 	for_each_intel_encoder_mask_with_psr(display->drm, encoder,
3335 					     new_crtc_state->uapi.encoder_mask) {
3336 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3337 		int ret;
3338 
3339 		lockdep_assert_held(&intel_dp->psr.lock);
3340 
3341 		if (!intel_dp->psr.enabled || intel_dp->psr.panel_replay_enabled)
3342 			continue;
3343 
3344 		if (intel_dp->psr.sel_update_enabled)
3345 			ret = _psr2_ready_for_pipe_update_locked(new_crtc_state,
3346 								 NULL);
3347 		else
3348 			ret = _psr1_ready_for_pipe_update_locked(new_crtc_state,
3349 								 NULL);
3350 
3351 		if (ret)
3352 			drm_err(display->drm,
3353 				"PSR wait timed out, atomic update may fail\n");
3354 	}
3355 }
3356 
intel_psr_wait_for_idle_dsb(struct intel_dsb * dsb,const struct intel_crtc_state * new_crtc_state)3357 void intel_psr_wait_for_idle_dsb(struct intel_dsb *dsb,
3358 				 const struct intel_crtc_state *new_crtc_state)
3359 {
3360 	if (!new_crtc_state->has_psr || new_crtc_state->has_panel_replay)
3361 		return;
3362 
3363 	if (new_crtc_state->has_sel_update)
3364 		_psr2_ready_for_pipe_update_locked(new_crtc_state, dsb);
3365 	else
3366 		_psr1_ready_for_pipe_update_locked(new_crtc_state, dsb);
3367 }
3368 
__psr_wait_for_idle_locked(struct intel_dp * intel_dp)3369 static bool __psr_wait_for_idle_locked(struct intel_dp *intel_dp)
3370 {
3371 	struct intel_display *display = to_intel_display(intel_dp);
3372 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
3373 	intel_reg_t reg;
3374 	u32 mask;
3375 	int err;
3376 
3377 	if (!intel_dp->psr.enabled)
3378 		return false;
3379 
3380 	if (intel_dp_is_edp(intel_dp) && (intel_dp->psr.sel_update_enabled ||
3381 					  intel_dp->psr.panel_replay_enabled)) {
3382 		reg = EDP_PSR2_STATUS(display, cpu_transcoder);
3383 		mask = EDP_PSR2_STATUS_STATE_MASK;
3384 	} else {
3385 		reg = psr_status_reg(display, cpu_transcoder);
3386 		mask = EDP_PSR_STATUS_STATE_MASK;
3387 	}
3388 
3389 	mutex_unlock(&intel_dp->psr.lock);
3390 
3391 	err = intel_de_wait_for_clear_ms(display, reg, mask, 50);
3392 	if (err)
3393 		drm_err(display->drm,
3394 			"Timed out waiting for PSR Idle for re-enable\n");
3395 
3396 	/* After the unlocked wait, verify that PSR is still wanted! */
3397 	mutex_lock(&intel_dp->psr.lock);
3398 	return err == 0 && intel_dp->psr.enabled && !intel_dp->psr.pause_counter;
3399 }
3400 
intel_psr_fastset_force(struct intel_display * display)3401 static int intel_psr_fastset_force(struct intel_display *display)
3402 {
3403 	struct drm_connector_list_iter conn_iter;
3404 	struct drm_modeset_acquire_ctx ctx;
3405 	struct drm_atomic_commit *state;
3406 	struct drm_connector *conn;
3407 	int err = 0;
3408 
3409 	state = drm_atomic_commit_alloc(display->drm);
3410 	if (!state)
3411 		return -ENOMEM;
3412 
3413 	drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
3414 
3415 	state->acquire_ctx = &ctx;
3416 	to_intel_atomic_state(state)->internal = true;
3417 
3418 retry:
3419 	drm_connector_list_iter_begin(display->drm, &conn_iter);
3420 	drm_for_each_connector_iter(conn, &conn_iter) {
3421 		struct drm_connector_state *conn_state;
3422 		struct drm_crtc_state *crtc_state;
3423 
3424 		if (conn->connector_type != DRM_MODE_CONNECTOR_eDP)
3425 			continue;
3426 
3427 		conn_state = drm_atomic_get_connector_state(state, conn);
3428 		if (IS_ERR(conn_state)) {
3429 			err = PTR_ERR(conn_state);
3430 			break;
3431 		}
3432 
3433 		if (!conn_state->crtc)
3434 			continue;
3435 
3436 		crtc_state = drm_atomic_get_crtc_state(state, conn_state->crtc);
3437 		if (IS_ERR(crtc_state)) {
3438 			err = PTR_ERR(crtc_state);
3439 			break;
3440 		}
3441 
3442 		/* Mark mode as changed to trigger a pipe->update() */
3443 		crtc_state->mode_changed = true;
3444 	}
3445 	drm_connector_list_iter_end(&conn_iter);
3446 
3447 	if (err == 0)
3448 		err = drm_atomic_commit(state);
3449 
3450 	if (err == -EDEADLK) {
3451 		drm_atomic_commit_clear(state);
3452 		err = drm_modeset_backoff(&ctx);
3453 		if (!err)
3454 			goto retry;
3455 	}
3456 
3457 	drm_modeset_drop_locks(&ctx);
3458 	drm_modeset_acquire_fini(&ctx);
3459 	drm_atomic_commit_put(state);
3460 
3461 	return err;
3462 }
3463 
intel_psr_debug_set(struct intel_dp * intel_dp,u64 val)3464 int intel_psr_debug_set(struct intel_dp *intel_dp, u64 val)
3465 {
3466 	struct intel_display *display = to_intel_display(intel_dp);
3467 	const u32 mode = val & I915_PSR_DEBUG_MODE_MASK;
3468 	const u32 disable_bits = val & (I915_PSR_DEBUG_SU_REGION_ET_DISABLE |
3469 					I915_PSR_DEBUG_PANEL_REPLAY_DISABLE);
3470 	u32 old_mode, old_disable_bits;
3471 	int ret;
3472 
3473 	if (val & ~(I915_PSR_DEBUG_IRQ | I915_PSR_DEBUG_SU_REGION_ET_DISABLE |
3474 		    I915_PSR_DEBUG_PANEL_REPLAY_DISABLE |
3475 		    I915_PSR_DEBUG_MODE_MASK) ||
3476 	    mode > I915_PSR_DEBUG_ENABLE_SEL_FETCH) {
3477 		drm_dbg_kms(display->drm, "Invalid debug mask %llx\n", val);
3478 		return -EINVAL;
3479 	}
3480 
3481 	ret = mutex_lock_interruptible(&intel_dp->psr.lock);
3482 	if (ret)
3483 		return ret;
3484 
3485 	old_mode = intel_dp->psr.debug & I915_PSR_DEBUG_MODE_MASK;
3486 	old_disable_bits = intel_dp->psr.debug &
3487 		(I915_PSR_DEBUG_SU_REGION_ET_DISABLE |
3488 		 I915_PSR_DEBUG_PANEL_REPLAY_DISABLE);
3489 
3490 	intel_dp->psr.debug = val;
3491 
3492 	/*
3493 	 * Do it right away if it's already enabled, otherwise it will be done
3494 	 * when enabling the source.
3495 	 */
3496 	if (intel_dp->psr.enabled)
3497 		psr_irq_control(intel_dp);
3498 
3499 	mutex_unlock(&intel_dp->psr.lock);
3500 
3501 	if (old_mode != mode || old_disable_bits != disable_bits)
3502 		ret = intel_psr_fastset_force(display);
3503 
3504 	return ret;
3505 }
3506 
intel_psr_handle_irq(struct intel_dp * intel_dp)3507 static void intel_psr_handle_irq(struct intel_dp *intel_dp)
3508 {
3509 	struct intel_psr *psr = &intel_dp->psr;
3510 
3511 	intel_psr_disable_locked(intel_dp);
3512 	psr->sink_not_reliable = true;
3513 	/* let's make sure that sink is awaken */
3514 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, DP_SET_POWER_D0);
3515 }
3516 
intel_psr_work(struct work_struct * work)3517 static void intel_psr_work(struct work_struct *work)
3518 {
3519 	struct intel_dp *intel_dp =
3520 		container_of(work, typeof(*intel_dp), psr.work);
3521 
3522 	mutex_lock(&intel_dp->psr.lock);
3523 
3524 	if (!intel_dp->psr.enabled)
3525 		goto unlock;
3526 
3527 	if (READ_ONCE(intel_dp->psr.irq_aux_error)) {
3528 		intel_psr_handle_irq(intel_dp);
3529 		goto unlock;
3530 	}
3531 
3532 	if (intel_dp->psr.pause_counter)
3533 		goto unlock;
3534 
3535 	/*
3536 	 * We have to make sure PSR is ready for re-enable
3537 	 * otherwise it keeps disabled until next full enable/disable cycle.
3538 	 * PSR might take some time to get fully disabled
3539 	 * and be ready for re-enable.
3540 	 */
3541 	if (!__psr_wait_for_idle_locked(intel_dp))
3542 		goto unlock;
3543 
3544 	/*
3545 	 * The delayed work can race with an invalidate hence we need to
3546 	 * recheck. Since psr_flush first clears this and then reschedules we
3547 	 * won't ever miss a flush when bailing out here.
3548 	 */
3549 	if (intel_dp->psr.busy_frontbuffer_bits || intel_dp->psr.active)
3550 		goto unlock;
3551 
3552 	intel_psr_activate(intel_dp);
3553 unlock:
3554 	mutex_unlock(&intel_dp->psr.lock);
3555 }
3556 
intel_psr_configure_full_frame_update(struct intel_dp * intel_dp)3557 static void intel_psr_configure_full_frame_update(struct intel_dp *intel_dp)
3558 {
3559 	struct intel_display *display = to_intel_display(intel_dp);
3560 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
3561 
3562 	if (!intel_dp->psr.psr2_sel_fetch_enabled)
3563 		return;
3564 
3565 	if (DISPLAY_VER(display) >= 20)
3566 		intel_de_write(display, LNL_SFF_CTL(cpu_transcoder),
3567 			       LNL_SFF_CTL_SF_SINGLE_FULL_FRAME);
3568 	else
3569 		intel_de_write(display,
3570 			       PSR2_MAN_TRK_CTL(display, cpu_transcoder),
3571 			       man_trk_ctl_enable_bit_get(display) |
3572 			       man_trk_ctl_partial_frame_bit_get(display) |
3573 			       man_trk_ctl_single_full_frame_bit_get(display) |
3574 			       man_trk_ctl_continuos_full_frame(display));
3575 }
3576 
_psr_invalidate_handle(struct intel_dp * intel_dp)3577 static void _psr_invalidate_handle(struct intel_dp *intel_dp)
3578 {
3579 	struct intel_display *display = to_intel_display(intel_dp);
3580 
3581 	if (DISPLAY_VER(display) < 20 && intel_dp->psr.psr2_sel_fetch_enabled) {
3582 		if (!intel_dp->psr.psr2_sel_fetch_cff_enabled) {
3583 			intel_dp->psr.psr2_sel_fetch_cff_enabled = true;
3584 			intel_psr_configure_full_frame_update(intel_dp);
3585 		}
3586 
3587 		intel_psr_force_update(intel_dp);
3588 	} else {
3589 		intel_psr_exit(intel_dp);
3590 	}
3591 }
3592 
3593 /**
3594  * intel_psr_invalidate - Invalidate PSR
3595  * @display: display device
3596  * @frontbuffer_bits: frontbuffer plane tracking bits
3597  * @origin: which operation caused the invalidate
3598  *
3599  * Since the hardware frontbuffer tracking has gaps we need to integrate
3600  * with the software frontbuffer tracking. This function gets called every
3601  * time frontbuffer rendering starts and a buffer gets dirtied. PSR must be
3602  * disabled if the frontbuffer mask contains a buffer relevant to PSR.
3603  *
3604  * Dirty frontbuffers relevant to PSR are tracked in busy_frontbuffer_bits."
3605  */
intel_psr_invalidate(struct intel_display * display,unsigned frontbuffer_bits,enum fb_op_origin origin)3606 void intel_psr_invalidate(struct intel_display *display,
3607 			  unsigned frontbuffer_bits, enum fb_op_origin origin)
3608 {
3609 	struct intel_encoder *encoder;
3610 
3611 	if (origin == ORIGIN_FLIP)
3612 		return;
3613 
3614 	for_each_intel_encoder_with_psr(display->drm, encoder) {
3615 		unsigned int pipe_frontbuffer_bits = frontbuffer_bits;
3616 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3617 
3618 		mutex_lock(&intel_dp->psr.lock);
3619 		if (!intel_dp->psr.enabled) {
3620 			mutex_unlock(&intel_dp->psr.lock);
3621 			continue;
3622 		}
3623 
3624 		pipe_frontbuffer_bits &=
3625 			INTEL_FRONTBUFFER_ALL_MASK(intel_dp->psr.pipe);
3626 		intel_dp->psr.busy_frontbuffer_bits |= pipe_frontbuffer_bits;
3627 
3628 		if (pipe_frontbuffer_bits)
3629 			_psr_invalidate_handle(intel_dp);
3630 
3631 		mutex_unlock(&intel_dp->psr.lock);
3632 	}
3633 }
3634 
_psr_flush_handle(struct intel_dp * intel_dp)3635 static void _psr_flush_handle(struct intel_dp *intel_dp)
3636 {
3637 	struct intel_display *display = to_intel_display(intel_dp);
3638 
3639 	if (DISPLAY_VER(display) >= 20) {
3640 		/*
3641 		 * We can use PSR exit on LunarLake onwards. Also
3642 		 * using trans push mechanism to trigger Frame Change
3643 		 * event requires using PSR exit.
3644 		 */
3645 		intel_psr_exit(intel_dp);
3646 	} else if (intel_dp->psr.psr2_sel_fetch_enabled) {
3647 		/* Selective fetch prior LNL */
3648 		if (intel_dp->psr.psr2_sel_fetch_cff_enabled) {
3649 			/* can we turn CFF off? */
3650 			if (intel_dp->psr.busy_frontbuffer_bits == 0)
3651 				intel_dp->psr.psr2_sel_fetch_cff_enabled = false;
3652 		}
3653 
3654 		/*
3655 		 * Still keep cff bit enabled as we don't have proper SU
3656 		 * configuration in case update is sent for any reason after
3657 		 * sff bit gets cleared by the HW on next vblank.
3658 		 *
3659 		 * NOTE: Setting cff bit is not needed for LunarLake onwards as
3660 		 * we have own register for SFF bit and we are not overwriting
3661 		 * existing SU configuration
3662 		 */
3663 		intel_psr_configure_full_frame_update(intel_dp);
3664 
3665 		intel_psr_force_update(intel_dp);
3666 	} else {
3667 		/*
3668 		 * On older platforms using PSR exit was seen causing problems
3669 		 */
3670 		intel_psr_force_update(intel_dp);
3671 	}
3672 
3673 	if (!intel_dp->psr.active && !intel_dp->psr.busy_frontbuffer_bits)
3674 		queue_work(display->wq.unordered, &intel_dp->psr.work);
3675 }
3676 
3677 /**
3678  * intel_psr_flush - Flush PSR
3679  * @display: display device
3680  * @frontbuffer_bits: frontbuffer plane tracking bits
3681  * @origin: which operation caused the flush
3682  *
3683  * Since the hardware frontbuffer tracking has gaps we need to integrate
3684  * with the software frontbuffer tracking. This function gets called every
3685  * time frontbuffer rendering has completed and flushed out to memory. PSR
3686  * can be enabled again if no other frontbuffer relevant to PSR is dirty.
3687  *
3688  * Dirty frontbuffers relevant to PSR are tracked in busy_frontbuffer_bits.
3689  */
intel_psr_flush(struct intel_display * display,unsigned frontbuffer_bits,enum fb_op_origin origin)3690 void intel_psr_flush(struct intel_display *display,
3691 		     unsigned frontbuffer_bits, enum fb_op_origin origin)
3692 {
3693 	struct intel_encoder *encoder;
3694 
3695 	for_each_intel_encoder_with_psr(display->drm, encoder) {
3696 		unsigned int pipe_frontbuffer_bits = frontbuffer_bits;
3697 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3698 
3699 		mutex_lock(&intel_dp->psr.lock);
3700 		if (!intel_dp->psr.enabled) {
3701 			mutex_unlock(&intel_dp->psr.lock);
3702 			continue;
3703 		}
3704 
3705 		pipe_frontbuffer_bits &=
3706 			INTEL_FRONTBUFFER_ALL_MASK(intel_dp->psr.pipe);
3707 		intel_dp->psr.busy_frontbuffer_bits &= ~pipe_frontbuffer_bits;
3708 
3709 		/*
3710 		 * If the PSR is paused by an explicit intel_psr_paused() call,
3711 		 * we have to ensure that the PSR is not activated until
3712 		 * intel_psr_resume() is called.
3713 		 */
3714 		if (intel_dp->psr.pause_counter)
3715 			goto unlock;
3716 
3717 		if (origin == ORIGIN_FLIP ||
3718 		    (origin == ORIGIN_CURSOR_UPDATE &&
3719 		     !intel_dp->psr.psr2_sel_fetch_enabled)) {
3720 			psr2_dc3co_flush_locked(intel_dp, frontbuffer_bits);
3721 			goto unlock;
3722 		}
3723 
3724 		if (pipe_frontbuffer_bits == 0)
3725 			goto unlock;
3726 
3727 		/* By definition flush = invalidate + flush */
3728 		_psr_flush_handle(intel_dp);
3729 unlock:
3730 		mutex_unlock(&intel_dp->psr.lock);
3731 	}
3732 }
3733 
3734 /**
3735  * intel_psr_init - Init basic PSR work and mutex.
3736  * @intel_dp: Intel DP
3737  *
3738  * This function is called after the initializing connector.
3739  * (the initializing of connector treats the handling of connector capabilities)
3740  * And it initializes basic PSR stuff for each DP Encoder.
3741  */
intel_psr_init(struct intel_dp * intel_dp)3742 void intel_psr_init(struct intel_dp *intel_dp)
3743 {
3744 	struct intel_display *display = to_intel_display(intel_dp);
3745 	struct intel_connector *connector = intel_dp->attached_connector;
3746 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3747 
3748 	if (!(HAS_PSR(display) || HAS_DP20(display)))
3749 		return;
3750 
3751 	/*
3752 	 * HSW spec explicitly says PSR is tied to port A.
3753 	 * BDW+ platforms have a instance of PSR registers per transcoder but
3754 	 * BDW, GEN9 and GEN11 are not validated by HW team in other transcoder
3755 	 * than eDP one.
3756 	 * For now it only supports one instance of PSR for BDW, GEN9 and GEN11.
3757 	 * So lets keep it hardcoded to PORT_A for BDW, GEN9 and GEN11.
3758 	 * But GEN12 supports a instance of PSR registers per transcoder.
3759 	 */
3760 	if (DISPLAY_VER(display) < 12 && dig_port->base.port != PORT_A) {
3761 		drm_dbg_kms(display->drm,
3762 			    "PSR condition failed: Port not supported\n");
3763 		return;
3764 	}
3765 
3766 	if ((HAS_DP20(display) && !intel_dp_is_edp(intel_dp)) ||
3767 	    DISPLAY_VER(display) >= 20)
3768 		intel_dp->psr.source_panel_replay_support = true;
3769 
3770 	if (HAS_PSR(display) && intel_dp_is_edp(intel_dp))
3771 		intel_dp->psr.source_support = true;
3772 
3773 	/* Set link_standby x link_off defaults */
3774 	if (DISPLAY_VER(display) < 12)
3775 		/* For new platforms up to TGL let's respect VBT back again */
3776 		intel_dp->psr.link_standby = connector->panel.vbt.psr.full_link;
3777 
3778 	INIT_WORK(&intel_dp->psr.work, intel_psr_work);
3779 	INIT_DELAYED_WORK(&intel_dp->psr.dc3co_work, psr2_dc3co_disable_work);
3780 	mutex_init(&intel_dp->psr.lock);
3781 }
3782 
psr_get_status_and_error_status(struct intel_dp * intel_dp,u8 * status,u8 * error_status)3783 static int psr_get_status_and_error_status(struct intel_dp *intel_dp,
3784 					   u8 *status, u8 *error_status)
3785 {
3786 	struct drm_dp_aux *aux = &intel_dp->aux;
3787 	int ret;
3788 	unsigned int offset;
3789 
3790 	offset = intel_dp->psr.panel_replay_enabled ?
3791 		 DP_SINK_DEVICE_PR_AND_FRAME_LOCK_STATUS : DP_PSR_STATUS;
3792 
3793 	ret = drm_dp_dpcd_readb(aux, offset, status);
3794 	if (ret != 1)
3795 		return ret;
3796 
3797 	offset = intel_dp->psr.panel_replay_enabled ?
3798 		 DP_PANEL_REPLAY_ERROR_STATUS : DP_PSR_ERROR_STATUS;
3799 
3800 	ret = drm_dp_dpcd_readb(aux, offset, error_status);
3801 	if (ret != 1)
3802 		return ret;
3803 
3804 	*status = *status & DP_PSR_SINK_STATE_MASK;
3805 
3806 	return 0;
3807 }
3808 
psr_alpm_check(struct intel_dp * intel_dp)3809 static void psr_alpm_check(struct intel_dp *intel_dp)
3810 {
3811 	struct intel_psr *psr = &intel_dp->psr;
3812 
3813 	if (!psr->sel_update_enabled)
3814 		return;
3815 
3816 	if (intel_alpm_get_error(intel_dp)) {
3817 		intel_psr_disable_locked(intel_dp);
3818 		psr->sink_not_reliable = true;
3819 	}
3820 }
3821 
psr_capability_changed_check(struct intel_dp * intel_dp)3822 static void psr_capability_changed_check(struct intel_dp *intel_dp)
3823 {
3824 	struct intel_display *display = to_intel_display(intel_dp);
3825 	struct intel_psr *psr = &intel_dp->psr;
3826 	u8 val;
3827 	int r;
3828 
3829 	r = drm_dp_dpcd_readb(&intel_dp->aux, DP_PSR_ESI, &val);
3830 	if (r != 1) {
3831 		drm_err(display->drm, "Error reading DP_PSR_ESI\n");
3832 		return;
3833 	}
3834 
3835 	if (val & DP_PSR_CAPS_CHANGE) {
3836 		intel_psr_disable_locked(intel_dp);
3837 		psr->sink_not_reliable = true;
3838 		drm_dbg_kms(display->drm,
3839 			    "Sink PSR capability changed, disabling PSR\n");
3840 
3841 		/* Clearing it */
3842 		drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_ESI, val);
3843 	}
3844 }
3845 
3846 /*
3847  * On common bits:
3848  * DP_PSR_RFB_STORAGE_ERROR == DP_PANEL_REPLAY_RFB_STORAGE_ERROR
3849  * DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR == DP_PANEL_REPLAY_VSC_SDP_UNCORRECTABLE_ERROR
3850  * DP_PSR_LINK_CRC_ERROR == DP_PANEL_REPLAY_LINK_CRC_ERROR
3851  * this function is relying on PSR definitions
3852  */
intel_psr_short_pulse(struct intel_dp * intel_dp)3853 void intel_psr_short_pulse(struct intel_dp *intel_dp)
3854 {
3855 	struct intel_display *display = to_intel_display(intel_dp);
3856 	struct intel_psr *psr = &intel_dp->psr;
3857 	u8 status, error_status;
3858 	const u8 errors = DP_PSR_RFB_STORAGE_ERROR |
3859 			  DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR |
3860 			  DP_PSR_LINK_CRC_ERROR;
3861 
3862 	if (!CAN_PSR(intel_dp) && !CAN_PANEL_REPLAY(intel_dp))
3863 		return;
3864 
3865 	mutex_lock(&psr->lock);
3866 
3867 	psr->link_ok = false;
3868 
3869 	if (!psr->enabled)
3870 		goto exit;
3871 
3872 	if (psr_get_status_and_error_status(intel_dp, &status, &error_status)) {
3873 		drm_err(display->drm,
3874 			"Error reading PSR status or error status\n");
3875 		goto exit;
3876 	}
3877 
3878 	if ((!psr->panel_replay_enabled && status == DP_PSR_SINK_INTERNAL_ERROR) ||
3879 	    (error_status & errors)) {
3880 		intel_psr_disable_locked(intel_dp);
3881 		psr->sink_not_reliable = true;
3882 	}
3883 
3884 	if (!psr->panel_replay_enabled && status == DP_PSR_SINK_INTERNAL_ERROR &&
3885 	    !error_status)
3886 		drm_dbg_kms(display->drm,
3887 			    "PSR sink internal error, disabling PSR\n");
3888 	if (error_status & DP_PSR_RFB_STORAGE_ERROR)
3889 		drm_dbg_kms(display->drm,
3890 			    "PSR RFB storage error, disabling PSR\n");
3891 	if (error_status & DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR)
3892 		drm_dbg_kms(display->drm,
3893 			    "PSR VSC SDP uncorrectable error, disabling PSR\n");
3894 	if (error_status & DP_PSR_LINK_CRC_ERROR)
3895 		drm_dbg_kms(display->drm,
3896 			    "PSR Link CRC error, disabling PSR\n");
3897 
3898 	if (error_status & ~errors)
3899 		drm_err(display->drm,
3900 			"PSR_ERROR_STATUS unhandled errors %x\n",
3901 			error_status & ~errors);
3902 	/* clear status register */
3903 	drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_ERROR_STATUS, error_status);
3904 
3905 	if (!psr->panel_replay_enabled) {
3906 		psr_alpm_check(intel_dp);
3907 		psr_capability_changed_check(intel_dp);
3908 	}
3909 
3910 exit:
3911 	mutex_unlock(&psr->lock);
3912 }
3913 
intel_psr_enabled(struct intel_dp * intel_dp)3914 bool intel_psr_enabled(struct intel_dp *intel_dp)
3915 {
3916 	bool ret;
3917 
3918 	if (!CAN_PSR(intel_dp))
3919 		return false;
3920 
3921 	mutex_lock(&intel_dp->psr.lock);
3922 	ret = intel_dp->psr.enabled;
3923 	mutex_unlock(&intel_dp->psr.lock);
3924 
3925 	return ret;
3926 }
3927 
3928 /**
3929  * intel_psr_link_ok - return psr->link_ok
3930  * @intel_dp: struct intel_dp
3931  *
3932  * We are seeing unexpected link re-trainings with some panels. This is caused
3933  * by panel stating bad link status after PSR is enabled. Code checking link
3934  * status can call this to ensure it can ignore bad link status stated by the
3935  * panel I.e. if panel is stating bad link and intel_psr_link_ok is stating link
3936  * is ok caller should rely on latter.
3937  *
3938  * Return value of link_ok
3939  */
intel_psr_link_ok(struct intel_dp * intel_dp)3940 bool intel_psr_link_ok(struct intel_dp *intel_dp)
3941 {
3942 	bool ret;
3943 
3944 	if ((!CAN_PSR(intel_dp) && !CAN_PANEL_REPLAY(intel_dp)) ||
3945 	    !intel_dp_is_edp(intel_dp))
3946 		return false;
3947 
3948 	mutex_lock(&intel_dp->psr.lock);
3949 	ret = intel_dp->psr.link_ok;
3950 	mutex_unlock(&intel_dp->psr.lock);
3951 
3952 	return ret;
3953 }
3954 
3955 /**
3956  * intel_psr_lock - grab PSR lock
3957  * @crtc_state: the crtc state
3958  *
3959  * This is initially meant to be used by around CRTC update, when
3960  * vblank sensitive registers are updated and we need grab the lock
3961  * before it to avoid vblank evasion.
3962  */
intel_psr_lock(const struct intel_crtc_state * crtc_state)3963 void intel_psr_lock(const struct intel_crtc_state *crtc_state)
3964 {
3965 	struct intel_display *display = to_intel_display(crtc_state);
3966 	struct intel_encoder *encoder;
3967 
3968 	if (!crtc_state->has_psr)
3969 		return;
3970 
3971 	for_each_intel_encoder_mask_with_psr(display->drm, encoder,
3972 					     crtc_state->uapi.encoder_mask) {
3973 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3974 
3975 		mutex_lock(&intel_dp->psr.lock);
3976 		break;
3977 	}
3978 }
3979 
3980 /**
3981  * intel_psr_unlock - release PSR lock
3982  * @crtc_state: the crtc state
3983  *
3984  * Release the PSR lock that was held during pipe update.
3985  */
intel_psr_unlock(const struct intel_crtc_state * crtc_state)3986 void intel_psr_unlock(const struct intel_crtc_state *crtc_state)
3987 {
3988 	struct intel_display *display = to_intel_display(crtc_state);
3989 	struct intel_encoder *encoder;
3990 
3991 	if (!crtc_state->has_psr)
3992 		return;
3993 
3994 	for_each_intel_encoder_mask_with_psr(display->drm, encoder,
3995 					     crtc_state->uapi.encoder_mask) {
3996 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3997 
3998 		mutex_unlock(&intel_dp->psr.lock);
3999 		break;
4000 	}
4001 }
4002 
4003 /* Wa_16025596647 */
intel_psr_apply_underrun_on_idle_wa_locked(struct intel_dp * intel_dp)4004 static void intel_psr_apply_underrun_on_idle_wa_locked(struct intel_dp *intel_dp)
4005 {
4006 	struct intel_display *display = to_intel_display(intel_dp);
4007 	bool dc5_dc6_blocked;
4008 
4009 	if (!intel_dp->psr.active || !intel_dp->psr.pkg_c_latency_used)
4010 		return;
4011 
4012 	dc5_dc6_blocked = is_dc5_dc6_blocked(intel_dp);
4013 
4014 	if (intel_dp->psr.sel_update_enabled)
4015 		psr2_program_idle_frames(intel_dp, dc5_dc6_blocked ? 0 :
4016 					 psr_compute_idle_frames(intel_dp));
4017 	else
4018 		intel_dmc_start_pkgc_exit_at_start_of_undelayed_vblank(display,
4019 								       intel_dp->psr.pipe,
4020 								       dc5_dc6_blocked);
4021 }
4022 
psr_dc5_dc6_wa_work(struct work_struct * work)4023 static void psr_dc5_dc6_wa_work(struct work_struct *work)
4024 {
4025 	struct intel_display *display = container_of(work, typeof(*display),
4026 						     psr_dc5_dc6_wa_work);
4027 	struct intel_encoder *encoder;
4028 
4029 	for_each_intel_encoder_with_psr(display->drm, encoder) {
4030 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4031 
4032 		mutex_lock(&intel_dp->psr.lock);
4033 
4034 		if (intel_dp->psr.enabled && !intel_dp->psr.panel_replay_enabled &&
4035 		    !intel_dp->psr.pkg_c_latency_used)
4036 			intel_psr_apply_underrun_on_idle_wa_locked(intel_dp);
4037 
4038 		mutex_unlock(&intel_dp->psr.lock);
4039 	}
4040 }
4041 
4042 /**
4043  * intel_psr_notify_dc5_dc6 - Notify PSR about enable/disable dc5/dc6
4044  * @display: intel atomic state
4045  *
4046  * This is targeted for underrun on idle PSR HW bug (Wa_16025596647) to schedule
4047  * psr_dc5_dc6_wa_work used for applying/removing the workaround.
4048  */
intel_psr_notify_dc5_dc6(struct intel_display * display)4049 void intel_psr_notify_dc5_dc6(struct intel_display *display)
4050 {
4051 	if (!intel_display_wa(display, INTEL_DISPLAY_WA_16025596647))
4052 		return;
4053 
4054 	schedule_work(&display->psr_dc5_dc6_wa_work);
4055 }
4056 
4057 /**
4058  * intel_psr_dc5_dc6_wa_init - Init work for underrun on idle PSR HW bug wa
4059  * @display: intel atomic state
4060  *
4061  * This is targeted for underrun on idle PSR HW bug (Wa_16025596647) to init
4062  * psr_dc5_dc6_wa_work used for applying the workaround.
4063  */
intel_psr_dc5_dc6_wa_init(struct intel_display * display)4064 void intel_psr_dc5_dc6_wa_init(struct intel_display *display)
4065 {
4066 	if (!intel_display_wa(display, INTEL_DISPLAY_WA_16025596647))
4067 		return;
4068 
4069 	INIT_WORK(&display->psr_dc5_dc6_wa_work, psr_dc5_dc6_wa_work);
4070 }
4071 
4072 /**
4073  * intel_psr_notify_pipe_change - Notify PSR about enable/disable of a pipe
4074  * @state: intel atomic state
4075  * @crtc: intel crtc
4076  * @enable: enable/disable
4077  *
4078  * This is targeted for underrun on idle PSR HW bug (Wa_16025596647) to apply
4079  * remove the workaround when pipe is getting enabled/disabled
4080  */
intel_psr_notify_pipe_change(struct intel_atomic_state * state,struct intel_crtc * crtc,bool enable)4081 void intel_psr_notify_pipe_change(struct intel_atomic_state *state,
4082 				  struct intel_crtc *crtc, bool enable)
4083 {
4084 	struct intel_display *display = to_intel_display(state);
4085 	struct intel_encoder *encoder;
4086 
4087 	if (!intel_display_wa(display, INTEL_DISPLAY_WA_16025596647))
4088 		return;
4089 
4090 	for_each_intel_encoder_with_psr(display->drm, encoder) {
4091 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4092 		u8 active_non_psr_pipes;
4093 
4094 		mutex_lock(&intel_dp->psr.lock);
4095 
4096 		if (!intel_dp->psr.enabled || intel_dp->psr.panel_replay_enabled)
4097 			goto unlock;
4098 
4099 		active_non_psr_pipes = intel_dp->psr.active_non_psr_pipes;
4100 
4101 		if (enable)
4102 			active_non_psr_pipes |= BIT(crtc->pipe);
4103 		else
4104 			active_non_psr_pipes &= ~BIT(crtc->pipe);
4105 
4106 		if (active_non_psr_pipes == intel_dp->psr.active_non_psr_pipes)
4107 			goto unlock;
4108 
4109 		if ((enable && intel_dp->psr.active_non_psr_pipes) ||
4110 		    (!enable && !intel_dp->psr.active_non_psr_pipes) ||
4111 		    !intel_dp->psr.pkg_c_latency_used) {
4112 			intel_dp->psr.active_non_psr_pipes = active_non_psr_pipes;
4113 			goto unlock;
4114 		}
4115 
4116 		intel_dp->psr.active_non_psr_pipes = active_non_psr_pipes;
4117 
4118 		intel_psr_apply_underrun_on_idle_wa_locked(intel_dp);
4119 unlock:
4120 		mutex_unlock(&intel_dp->psr.lock);
4121 	}
4122 }
4123 
4124 /**
4125  * intel_psr_notify_vblank_enable_disable - Notify PSR about enable/disable of vblank
4126  * @display: intel display struct
4127  * @enable: enable/disable
4128  *
4129  * This is targeted for underrun on idle PSR HW bug (Wa_16025596647) to apply
4130  * remove the workaround when vblank is getting enabled/disabled
4131  */
intel_psr_notify_vblank_enable_disable(struct intel_display * display,bool enable)4132 void intel_psr_notify_vblank_enable_disable(struct intel_display *display,
4133 					    bool enable)
4134 {
4135 	struct intel_encoder *encoder;
4136 
4137 	for_each_intel_encoder_with_psr(display->drm, encoder) {
4138 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4139 
4140 		mutex_lock(&intel_dp->psr.lock);
4141 		if (CAN_PANEL_REPLAY(intel_dp)) {
4142 			if (enable)
4143 				intel_dp->psr.vblank_wakeref =
4144 					intel_display_power_get(display,
4145 								POWER_DOMAIN_DC_OFF);
4146 			else
4147 				intel_display_power_put(display, POWER_DOMAIN_DC_OFF,
4148 							intel_dp->psr.vblank_wakeref);
4149 		}
4150 
4151 		if (intel_dp->psr.enabled && !intel_dp->psr.panel_replay_enabled &&
4152 		    intel_dp->psr.pkg_c_latency_used)
4153 			intel_psr_apply_underrun_on_idle_wa_locked(intel_dp);
4154 
4155 		mutex_unlock(&intel_dp->psr.lock);
4156 	}
4157 }
4158 
4159 static void
psr_source_status(struct intel_dp * intel_dp,struct seq_file * m)4160 psr_source_status(struct intel_dp *intel_dp, struct seq_file *m)
4161 {
4162 	struct intel_display *display = to_intel_display(intel_dp);
4163 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
4164 	const char *status = "unknown";
4165 	u32 val, status_val;
4166 
4167 	if ((intel_dp_is_edp(intel_dp) || DISPLAY_VER(display) >= 30) &&
4168 	    (intel_dp->psr.sel_update_enabled || intel_dp->psr.panel_replay_enabled)) {
4169 		static const char * const live_status[] = {
4170 			"IDLE",
4171 			"CAPTURE",
4172 			"CAPTURE_FS",
4173 			"SLEEP",
4174 			"BUFON_FW",
4175 			"ML_UP",
4176 			"SU_STANDBY",
4177 			"FAST_SLEEP",
4178 			"DEEP_SLEEP",
4179 			"BUF_ON",
4180 			"TG_ON"
4181 		};
4182 		val = intel_de_read(display,
4183 				    EDP_PSR2_STATUS(display, cpu_transcoder));
4184 		status_val = REG_FIELD_GET(EDP_PSR2_STATUS_STATE_MASK, val);
4185 		if (status_val < ARRAY_SIZE(live_status))
4186 			status = live_status[status_val];
4187 	} else {
4188 		static const char * const live_status[] = {
4189 			"IDLE",
4190 			"SRDONACK",
4191 			"SRDENT",
4192 			"BUFOFF",
4193 			"BUFON",
4194 			"AUXACK",
4195 			"SRDOFFACK",
4196 			"SRDENT_ON",
4197 		};
4198 		val = intel_de_read(display,
4199 				    psr_status_reg(display, cpu_transcoder));
4200 		status_val = REG_FIELD_GET(EDP_PSR_STATUS_STATE_MASK, val);
4201 		if (status_val < ARRAY_SIZE(live_status))
4202 			status = live_status[status_val];
4203 	}
4204 
4205 	seq_printf(m, "Source PSR/PanelReplay status: %s [0x%08x]\n", status, val);
4206 }
4207 
intel_psr_sink_capability(struct intel_connector * connector,struct seq_file * m)4208 static void intel_psr_sink_capability(struct intel_connector *connector,
4209 				      struct seq_file *m)
4210 {
4211 	seq_printf(m, "Sink support: PSR = %s",
4212 		   str_yes_no(connector->dp.psr_caps.support));
4213 
4214 	if (connector->dp.psr_caps.support)
4215 		seq_printf(m, " [0x%02x]", connector->dp.psr_caps.dpcd[0]);
4216 	if (connector->dp.psr_caps.dpcd[0] == DP_PSR2_WITH_Y_COORD_ET_SUPPORTED)
4217 		seq_printf(m, " (Early Transport)");
4218 	seq_printf(m, ", Panel Replay = %s", str_yes_no(connector->dp.panel_replay_caps.support));
4219 	seq_printf(m, ", Panel Replay Selective Update = %s",
4220 		   str_yes_no(connector->dp.panel_replay_caps.su_support));
4221 	seq_printf(m, ", Panel Replay DSC support = %s",
4222 		   panel_replay_dsc_support_str(connector->dp.panel_replay_caps.dsc_support));
4223 	if (connector->dp.panel_replay_caps.dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_SUPPORT)] &
4224 	    DP_PANEL_REPLAY_EARLY_TRANSPORT_SUPPORT)
4225 		seq_printf(m, " (Early Transport)");
4226 	seq_printf(m, "\n");
4227 }
4228 
intel_psr_print_mode(struct intel_dp * intel_dp,struct seq_file * m)4229 static void intel_psr_print_mode(struct intel_dp *intel_dp,
4230 				 struct seq_file *m)
4231 {
4232 	struct intel_psr *psr = &intel_dp->psr;
4233 	const char *status, *mode, *region_et;
4234 
4235 	if (psr->enabled)
4236 		status = " enabled";
4237 	else
4238 		status = "disabled";
4239 
4240 	if (psr->panel_replay_enabled && psr->sel_update_enabled)
4241 		mode = "Panel Replay Selective Update";
4242 	else if (psr->panel_replay_enabled)
4243 		mode = "Panel Replay";
4244 	else if (psr->sel_update_enabled)
4245 		mode = "PSR2";
4246 	else if (psr->enabled)
4247 		mode = "PSR1";
4248 	else
4249 		mode = "";
4250 
4251 	if (psr->su_region_et_enabled)
4252 		region_et = " (Early Transport)";
4253 	else
4254 		region_et = "";
4255 
4256 	seq_printf(m, "PSR mode: %s%s%s\n", mode, status, region_et);
4257 	if (psr->no_psr_reason)
4258 		seq_printf(m, "  %s\n", psr->no_psr_reason);
4259 }
4260 
intel_psr_status(struct seq_file * m,struct intel_dp * intel_dp,struct intel_connector * connector)4261 static int intel_psr_status(struct seq_file *m, struct intel_dp *intel_dp,
4262 			    struct intel_connector *connector)
4263 {
4264 	struct intel_display *display = to_intel_display(intel_dp);
4265 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
4266 	struct intel_psr *psr = &intel_dp->psr;
4267 	struct ref_tracker *wakeref;
4268 	bool enabled;
4269 	u32 val, psr2_ctl;
4270 
4271 	intel_psr_sink_capability(connector, m);
4272 
4273 	if (!(connector->dp.psr_caps.support || connector->dp.panel_replay_caps.support))
4274 		return 0;
4275 
4276 	wakeref = intel_display_rpm_get(display);
4277 	mutex_lock(&psr->lock);
4278 
4279 	intel_psr_print_mode(intel_dp, m);
4280 
4281 	if (!psr->enabled) {
4282 		seq_printf(m, "PSR sink not reliable: %s\n",
4283 			   str_yes_no(psr->sink_not_reliable));
4284 
4285 		goto unlock;
4286 	}
4287 
4288 	if (psr->panel_replay_enabled) {
4289 		val = intel_de_read(display, TRANS_DP2_CTL(cpu_transcoder));
4290 
4291 		if (intel_dp_is_edp(intel_dp))
4292 			psr2_ctl = intel_de_read(display,
4293 						 EDP_PSR2_CTL(display,
4294 							      cpu_transcoder));
4295 
4296 		enabled = val & TRANS_DP2_PANEL_REPLAY_ENABLE;
4297 	} else if (psr->sel_update_enabled) {
4298 		val = intel_de_read(display,
4299 				    EDP_PSR2_CTL(display, cpu_transcoder));
4300 		enabled = val & EDP_PSR2_ENABLE;
4301 	} else {
4302 		val = intel_de_read(display, psr_ctl_reg(display, cpu_transcoder));
4303 		enabled = val & EDP_PSR_ENABLE;
4304 	}
4305 	seq_printf(m, "Source PSR/PanelReplay ctl: %s [0x%08x]\n",
4306 		   str_enabled_disabled(enabled), val);
4307 	if (psr->panel_replay_enabled && intel_dp_is_edp(intel_dp))
4308 		seq_printf(m, "PSR2_CTL: 0x%08x\n",
4309 			   psr2_ctl);
4310 	psr_source_status(intel_dp, m);
4311 	seq_printf(m, "Busy frontbuffer bits: 0x%08x\n",
4312 		   psr->busy_frontbuffer_bits);
4313 
4314 	/*
4315 	 * SKL+ Perf counter is reset to 0 everytime DC state is entered
4316 	 */
4317 	val = intel_de_read(display, psr_perf_cnt_reg(display, cpu_transcoder));
4318 	seq_printf(m, "Performance counter: %u\n",
4319 		   REG_FIELD_GET(EDP_PSR_PERF_CNT_MASK, val));
4320 
4321 	if (psr->debug & I915_PSR_DEBUG_IRQ) {
4322 		seq_printf(m, "Last attempted entry at: %lld\n",
4323 			   psr->last_entry_attempt);
4324 		seq_printf(m, "Last exit at: %lld\n", psr->last_exit);
4325 	}
4326 
4327 	if (psr->sel_update_enabled) {
4328 		u32 su_frames_val[3];
4329 		int frame;
4330 
4331 		/*
4332 		 * PSR2_SU_STATUS register has been tied-off since DG2/ADL-P
4333 		 * (it returns zeros only) and it has been removed on Xe2_LPD.
4334 		 */
4335 		if (DISPLAY_VER(display) < 13) {
4336 			/*
4337 			 * Reading all 3 registers before hand to minimize crossing a
4338 			 * frame boundary between register reads
4339 			 */
4340 			for (frame = 0; frame < PSR2_SU_STATUS_FRAMES; frame += 3) {
4341 				val = intel_de_read(display,
4342 						    PSR2_SU_STATUS(display, cpu_transcoder, frame));
4343 				su_frames_val[frame / 3] = val;
4344 			}
4345 
4346 			seq_puts(m, "Frame:\tPSR2 SU blocks:\n");
4347 
4348 			for (frame = 0; frame < PSR2_SU_STATUS_FRAMES; frame++) {
4349 				u32 su_blocks;
4350 
4351 				su_blocks = su_frames_val[frame / 3] &
4352 					PSR2_SU_STATUS_MASK(frame);
4353 				su_blocks = su_blocks >> PSR2_SU_STATUS_SHIFT(frame);
4354 				seq_printf(m, "%d\t%d\n", frame, su_blocks);
4355 			}
4356 		}
4357 
4358 		seq_printf(m, "PSR2 selective fetch: %s\n",
4359 			   str_enabled_disabled(psr->psr2_sel_fetch_enabled));
4360 	}
4361 
4362 unlock:
4363 	mutex_unlock(&psr->lock);
4364 	intel_display_rpm_put(display, wakeref);
4365 
4366 	return 0;
4367 }
4368 
i915_edp_psr_status_show(struct seq_file * m,void * data)4369 static int i915_edp_psr_status_show(struct seq_file *m, void *data)
4370 {
4371 	struct intel_display *display = m->private;
4372 	struct intel_dp *intel_dp = NULL;
4373 	struct intel_encoder *encoder;
4374 
4375 	if (!HAS_PSR(display))
4376 		return -ENODEV;
4377 
4378 	/* Find the first EDP which supports PSR */
4379 	for_each_intel_encoder_with_psr(display->drm, encoder) {
4380 		intel_dp = enc_to_intel_dp(encoder);
4381 		break;
4382 	}
4383 
4384 	if (!intel_dp)
4385 		return -ENODEV;
4386 
4387 	return intel_psr_status(m, intel_dp, intel_dp->attached_connector);
4388 }
4389 DEFINE_SHOW_ATTRIBUTE(i915_edp_psr_status);
4390 
4391 static int
i915_edp_psr_debug_set(void * data,u64 val)4392 i915_edp_psr_debug_set(void *data, u64 val)
4393 {
4394 	struct intel_display *display = data;
4395 	struct intel_encoder *encoder;
4396 	int ret = -ENODEV;
4397 
4398 	if (!HAS_PSR(display))
4399 		return ret;
4400 
4401 	for_each_intel_encoder_with_psr(display->drm, encoder) {
4402 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4403 
4404 		drm_dbg_kms(display->drm, "Setting PSR debug to %llx\n", val);
4405 
4406 		// TODO: split to each transcoder's PSR debug state
4407 		with_intel_display_rpm(display)
4408 			ret = intel_psr_debug_set(intel_dp, val);
4409 	}
4410 
4411 	return ret;
4412 }
4413 
4414 static int
i915_edp_psr_debug_get(void * data,u64 * val)4415 i915_edp_psr_debug_get(void *data, u64 *val)
4416 {
4417 	struct intel_display *display = data;
4418 	struct intel_encoder *encoder;
4419 
4420 	if (!HAS_PSR(display))
4421 		return -ENODEV;
4422 
4423 	for_each_intel_encoder_with_psr(display->drm, encoder) {
4424 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4425 
4426 		// TODO: split to each transcoder's PSR debug state
4427 		*val = READ_ONCE(intel_dp->psr.debug);
4428 		return 0;
4429 	}
4430 
4431 	return -ENODEV;
4432 }
4433 
4434 DEFINE_SIMPLE_ATTRIBUTE(i915_edp_psr_debug_fops,
4435 			i915_edp_psr_debug_get, i915_edp_psr_debug_set,
4436 			"%llu\n");
4437 
intel_psr_debugfs_register(struct intel_display * display)4438 void intel_psr_debugfs_register(struct intel_display *display)
4439 {
4440 	struct dentry *debugfs_root = display->drm->debugfs_root;
4441 
4442 	debugfs_create_file("i915_edp_psr_debug", 0644, debugfs_root,
4443 			    display, &i915_edp_psr_debug_fops);
4444 
4445 	debugfs_create_file("i915_edp_psr_status", 0444, debugfs_root,
4446 			    display, &i915_edp_psr_status_fops);
4447 }
4448 
psr_mode_str(struct intel_dp * intel_dp)4449 static const char *psr_mode_str(struct intel_dp *intel_dp)
4450 {
4451 	if (intel_dp->psr.panel_replay_enabled)
4452 		return "PANEL-REPLAY";
4453 	else if (intel_dp->psr.enabled)
4454 		return "PSR";
4455 
4456 	return "unknown";
4457 }
4458 
i915_psr_sink_status_show(struct seq_file * m,void * data)4459 static int i915_psr_sink_status_show(struct seq_file *m, void *data)
4460 {
4461 	struct intel_connector *connector = m->private;
4462 	struct intel_dp *intel_dp = intel_attached_dp(connector);
4463 	static const char * const sink_status[] = {
4464 		"inactive",
4465 		"transition to active, capture and display",
4466 		"active, display from RFB",
4467 		"active, capture and display on sink device timings",
4468 		"transition to inactive, capture and display, timing re-sync",
4469 		"reserved",
4470 		"reserved",
4471 		"sink internal error",
4472 	};
4473 	const char *str;
4474 	int ret;
4475 	u8 status, error_status;
4476 
4477 	if (!(CAN_PSR(intel_dp) || CAN_PANEL_REPLAY(intel_dp))) {
4478 		seq_puts(m, "PSR/Panel-Replay Unsupported\n");
4479 		return -ENODEV;
4480 	}
4481 
4482 	if (connector->base.status != connector_status_connected)
4483 		return -ENODEV;
4484 
4485 	ret = psr_get_status_and_error_status(intel_dp, &status, &error_status);
4486 	if (ret)
4487 		return ret;
4488 
4489 	status &= DP_PSR_SINK_STATE_MASK;
4490 	if (status < ARRAY_SIZE(sink_status))
4491 		str = sink_status[status];
4492 	else
4493 		str = "unknown";
4494 
4495 	seq_printf(m, "Sink %s status: 0x%x [%s]\n", psr_mode_str(intel_dp), status, str);
4496 
4497 	seq_printf(m, "Sink %s error status: 0x%x", psr_mode_str(intel_dp), error_status);
4498 
4499 	if (error_status & (DP_PSR_RFB_STORAGE_ERROR |
4500 			    DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR |
4501 			    DP_PSR_LINK_CRC_ERROR))
4502 		seq_puts(m, ":\n");
4503 	else
4504 		seq_puts(m, "\n");
4505 	if (error_status & DP_PSR_RFB_STORAGE_ERROR)
4506 		seq_printf(m, "\t%s RFB storage error\n", psr_mode_str(intel_dp));
4507 	if (error_status & DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR)
4508 		seq_printf(m, "\t%s VSC SDP uncorrectable error\n", psr_mode_str(intel_dp));
4509 	if (error_status & DP_PSR_LINK_CRC_ERROR)
4510 		seq_printf(m, "\t%s Link CRC error\n", psr_mode_str(intel_dp));
4511 
4512 	return ret;
4513 }
4514 DEFINE_SHOW_ATTRIBUTE(i915_psr_sink_status);
4515 
i915_psr_status_show(struct seq_file * m,void * data)4516 static int i915_psr_status_show(struct seq_file *m, void *data)
4517 {
4518 	struct intel_connector *connector = m->private;
4519 	struct intel_dp *intel_dp = intel_attached_dp(connector);
4520 
4521 	return intel_psr_status(m, intel_dp, connector);
4522 }
4523 DEFINE_SHOW_ATTRIBUTE(i915_psr_status);
4524 
intel_psr_connector_debugfs_add(struct intel_connector * connector)4525 void intel_psr_connector_debugfs_add(struct intel_connector *connector)
4526 {
4527 	struct intel_display *display = to_intel_display(connector);
4528 	struct dentry *root = connector->base.debugfs_entry;
4529 
4530 	if (connector->base.connector_type != DRM_MODE_CONNECTOR_eDP &&
4531 	    connector->base.connector_type != DRM_MODE_CONNECTOR_DisplayPort)
4532 		return;
4533 
4534 	debugfs_create_file("i915_psr_sink_status", 0444, root,
4535 			    connector, &i915_psr_sink_status_fops);
4536 
4537 	if (HAS_PSR(display) || HAS_DP20(display))
4538 		debugfs_create_file("i915_psr_status", 0444, root,
4539 				    connector, &i915_psr_status_fops);
4540 }
4541 
intel_psr_needs_alpm(struct intel_dp * intel_dp,const struct intel_crtc_state * crtc_state)4542 bool intel_psr_needs_alpm(struct intel_dp *intel_dp, const struct intel_crtc_state *crtc_state)
4543 {
4544 	/*
4545 	 * eDP Panel Replay uses always ALPM
4546 	 * PSR2 uses ALPM but PSR1 doesn't
4547 	 */
4548 	return intel_dp_is_edp(intel_dp) && (crtc_state->has_sel_update ||
4549 					     crtc_state->has_panel_replay);
4550 }
4551 
intel_psr_needs_alpm_aux_less(struct intel_dp * intel_dp,const struct intel_crtc_state * crtc_state)4552 bool intel_psr_needs_alpm_aux_less(struct intel_dp *intel_dp,
4553 				   const struct intel_crtc_state *crtc_state)
4554 {
4555 	return intel_dp_is_edp(intel_dp) && crtc_state->has_panel_replay;
4556 }
4557 
intel_psr_compute_config_late(struct intel_dp * intel_dp,struct intel_crtc_state * crtc_state)4558 void intel_psr_compute_config_late(struct intel_dp *intel_dp,
4559 				   struct intel_crtc_state *crtc_state)
4560 {
4561 	struct intel_display *display = to_intel_display(intel_dp);
4562 	int vblank = intel_crtc_vblank_length(crtc_state);
4563 	int wake_lines;
4564 
4565 	if (intel_psr_needs_alpm_aux_less(intel_dp, crtc_state))
4566 		wake_lines = crtc_state->alpm_state.aux_less_wake_lines;
4567 	else if (intel_psr_needs_alpm(intel_dp, crtc_state))
4568 		wake_lines = DISPLAY_VER(display) < 20 ?
4569 			     psr2_block_count_lines(crtc_state->alpm_state.io_wake_lines,
4570 						    crtc_state->alpm_state.fast_wake_lines) :
4571 			     crtc_state->alpm_state.io_wake_lines;
4572 	else
4573 		wake_lines = 0;
4574 
4575 	/*
4576 	 * Disable the PSR features if wake lines exceed the available vblank.
4577 	 * Though SCL is computed based on these PSR features, it is not reset
4578 	 * even if the PSR features are disabled to avoid changing vblank start
4579 	 * at this stage.
4580 	 */
4581 	if (wake_lines && !_wake_lines_fit_into_vblank(crtc_state, vblank, wake_lines)) {
4582 		drm_dbg_kms(display->drm,
4583 			    "Adjusting PSR/PR mode: vblank too short for wake lines = %d\n",
4584 			    wake_lines);
4585 
4586 		if (crtc_state->has_panel_replay) {
4587 			crtc_state->has_panel_replay = false;
4588 			/*
4589 			 * #TODO : Add fall back to PSR/PSR2
4590 			 * Since panel replay cannot be supported, we can fall back to PSR/PSR2.
4591 			 * This will require calling compute_config for psr and psr2 with check for
4592 			 * actual guardband instead of vblank_length.
4593 			 */
4594 			crtc_state->has_psr = false;
4595 		}
4596 
4597 		crtc_state->has_sel_update = false;
4598 		crtc_state->enable_psr2_su_region_et = false;
4599 		crtc_state->enable_psr2_sel_fetch = false;
4600 	}
4601 
4602 	/* Wa_18037818876 */
4603 	if (intel_psr_needs_wa_18037818876(intel_dp, crtc_state)) {
4604 		crtc_state->has_psr = false;
4605 		drm_dbg_kms(display->drm,
4606 			    "PSR disabled to workaround PSR FSM hang issue\n");
4607 	}
4608 
4609 	intel_psr_set_non_psr_pipes(intel_dp, crtc_state);
4610 }
4611 
intel_psr_min_guardband(struct intel_crtc_state * crtc_state)4612 int intel_psr_min_guardband(struct intel_crtc_state *crtc_state)
4613 {
4614 	struct intel_display *display = to_intel_display(crtc_state);
4615 	int psr_min_guardband;
4616 	int wake_lines;
4617 
4618 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP))
4619 		return 0;
4620 
4621 	if (crtc_state->has_panel_replay)
4622 		wake_lines = crtc_state->alpm_state.aux_less_wake_lines;
4623 	else if (crtc_state->has_sel_update)
4624 		wake_lines = DISPLAY_VER(display) < 20 ?
4625 			     psr2_block_count_lines(crtc_state->alpm_state.io_wake_lines,
4626 						    crtc_state->alpm_state.fast_wake_lines) :
4627 			     crtc_state->alpm_state.io_wake_lines;
4628 	else
4629 		return 0;
4630 
4631 	psr_min_guardband = wake_lines + crtc_state->set_context_latency;
4632 
4633 	if (crtc_state->req_psr2_sdp_prior_scanline)
4634 		psr_min_guardband++;
4635 
4636 	return psr_min_guardband;
4637 }
4638 
intel_psr_use_trans_push(const struct intel_crtc_state * crtc_state)4639 bool intel_psr_use_trans_push(const struct intel_crtc_state *crtc_state)
4640 {
4641 	struct intel_display *display = to_intel_display(crtc_state);
4642 
4643 	return HAS_PSR_TRANS_PUSH_FRAME_CHANGE(display) && crtc_state->has_psr;
4644 }
4645 
intel_psr_pr_async_video_timing_supported(struct intel_dp * intel_dp)4646 bool intel_psr_pr_async_video_timing_supported(struct intel_dp *intel_dp)
4647 {
4648 	struct intel_connector *connector = intel_dp->attached_connector;
4649 	u8 *dpcd = connector->dp.panel_replay_caps.dpcd;
4650 	u8 pr_support = dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_SUPPORT)];
4651 	u8 pr_cap = dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_CAPABILITY)];
4652 
4653 	return (pr_support & DP_PANEL_REPLAY_SUPPORT) &&
4654 		!(pr_cap & DP_PANEL_REPLAY_ASYNC_VIDEO_TIMING_NOT_SUPPORTED_IN_PR);
4655 }
4656