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