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