xref: /linux/drivers/gpu/drm/i915/display/intel_psr.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
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 			     const struct drm_rect *damage_area,
2716 			     const struct drm_rect *display_area)
2717 {
2718 	struct drm_rect inter = *damage_area;
2719 
2720 	if (!drm_rect_intersect(&inter, display_area))
2721 		return;
2722 
2723 	if (overlap_damage_area->y1 == -1) {
2724 		overlap_damage_area->y1 = inter.y1;
2725 		overlap_damage_area->y2 = inter.y2;
2726 		return;
2727 	}
2728 
2729 	if (inter.y1 < overlap_damage_area->y1)
2730 		overlap_damage_area->y1 = inter.y1;
2731 
2732 	if (inter.y2 > overlap_damage_area->y2)
2733 		overlap_damage_area->y2 = inter.y2;
2734 }
2735 
2736 static bool intel_psr2_sel_fetch_pipe_alignment(struct intel_crtc_state *crtc_state)
2737 {
2738 	struct intel_display *display = to_intel_display(crtc_state);
2739 	const struct drm_dsc_config *vdsc_cfg = &crtc_state->dsc.config;
2740 	u16 y_alignment;
2741 	bool su_area_changed = false;
2742 
2743 	/* ADLP aligns the SU region to vdsc slice height in case dsc is enabled */
2744 	if (crtc_state->dsc.compression_enable &&
2745 	    (display->platform.alderlake_p || DISPLAY_VER(display) >= 14))
2746 		y_alignment = vdsc_cfg->slice_height;
2747 	else
2748 		y_alignment = crtc_state->su_y_granularity;
2749 
2750 	if (crtc_state->psr2_su_area.y1 % y_alignment) {
2751 		crtc_state->psr2_su_area.y1 -= crtc_state->psr2_su_area.y1 % y_alignment;
2752 		su_area_changed = true;
2753 	}
2754 
2755 	if (crtc_state->psr2_su_area.y2 % y_alignment) {
2756 		crtc_state->psr2_su_area.y2 = ((crtc_state->psr2_su_area.y2 /
2757 						y_alignment) + 1) * y_alignment;
2758 		su_area_changed = true;
2759 	}
2760 
2761 	return su_area_changed;
2762 }
2763 
2764 /*
2765  * When early transport is in use we need to extend SU area to cover
2766  * cursor fully when cursor is in SU area.
2767  */
2768 static void
2769 intel_psr2_sel_fetch_et_alignment(struct intel_atomic_state *state,
2770 				  struct intel_crtc *crtc,
2771 				  struct drm_rect *display_area,
2772 				  bool *cursor_in_su_area)
2773 {
2774 	struct intel_crtc_state *crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
2775 	struct intel_plane_state *new_plane_state;
2776 	struct intel_plane *plane;
2777 	int i;
2778 
2779 	if (!crtc_state->enable_psr2_su_region_et)
2780 		return;
2781 
2782 	for_each_new_intel_plane_in_state(state, plane, new_plane_state, i) {
2783 		struct drm_rect inter;
2784 
2785 		if (new_plane_state->hw.crtc != crtc_state->uapi.crtc)
2786 			continue;
2787 
2788 		if (plane->id != PLANE_CURSOR)
2789 			continue;
2790 
2791 		if (!new_plane_state->uapi.visible)
2792 			continue;
2793 
2794 		inter = crtc_state->psr2_su_area;
2795 		if (!drm_rect_intersect(&inter, &new_plane_state->uapi.dst))
2796 			continue;
2797 
2798 		clip_area_update(&crtc_state->psr2_su_area, &new_plane_state->uapi.dst,
2799 				 display_area);
2800 		*cursor_in_su_area = true;
2801 	}
2802 }
2803 
2804 /*
2805  * TODO: Not clear how to handle planes with negative position,
2806  * also planes are not updated if they have a negative X
2807  * position so for now doing a full update in this cases
2808  *
2809  * Plane scaling and rotation is not supported by selective fetch and both
2810  * properties can change without a modeset, so need to be check at every
2811  * atomic commit.
2812  */
2813 static bool psr2_sel_fetch_plane_state_supported(const struct intel_plane_state *plane_state)
2814 {
2815 	if (plane_state->uapi.dst.y1 < 0 ||
2816 	    plane_state->uapi.dst.x1 < 0 ||
2817 	    plane_state->scaler_id >= 0 ||
2818 	    plane_state->hw.rotation != DRM_MODE_ROTATE_0)
2819 		return false;
2820 
2821 	return true;
2822 }
2823 
2824 /*
2825  * Check for pipe properties that is not supported by selective fetch.
2826  *
2827  * TODO: pipe scaling causes a modeset but skl_update_scaler_crtc() is executed
2828  * after intel_psr_compute_config(), so for now keeping PSR2 selective fetch
2829  * enabled and going to the full update path.
2830  */
2831 static bool psr2_sel_fetch_pipe_state_supported(const struct intel_crtc_state *crtc_state)
2832 {
2833 	if (crtc_state->scaler_state.scaler_id >= 0 ||
2834 	    crtc_state->async_flip_planes)
2835 		return false;
2836 
2837 	return true;
2838 }
2839 
2840 /* Wa 14019834836 */
2841 static void intel_psr_apply_pr_link_on_su_wa(struct intel_crtc_state *crtc_state)
2842 {
2843 	struct intel_display *display = to_intel_display(crtc_state);
2844 	struct intel_encoder *encoder;
2845 	int hactive_limit;
2846 
2847 	if (crtc_state->psr2_su_area.y1 != 0 ||
2848 	    crtc_state->psr2_su_area.y2 != 0)
2849 		return;
2850 
2851 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
2852 		hactive_limit = intel_dp_is_uhbr(crtc_state) ? 1230 : 546;
2853 	else
2854 		hactive_limit = intel_dp_is_uhbr(crtc_state) ? 615 : 273;
2855 
2856 	if (crtc_state->hw.adjusted_mode.hdisplay < hactive_limit)
2857 		return;
2858 
2859 	for_each_intel_encoder_mask_with_psr(display->drm, encoder,
2860 					     crtc_state->uapi.encoder_mask) {
2861 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2862 
2863 		if (!intel_dp_is_edp(intel_dp) &&
2864 		    intel_dp->psr.panel_replay_enabled &&
2865 		    intel_dp->psr.sel_update_enabled) {
2866 			crtc_state->psr2_su_area.y2++;
2867 			return;
2868 		}
2869 	}
2870 }
2871 
2872 static void
2873 intel_psr_apply_su_area_workarounds(struct intel_crtc_state *crtc_state)
2874 {
2875 	struct intel_display *display = to_intel_display(crtc_state);
2876 
2877 	/* Wa_14014971492 */
2878 	if (!crtc_state->has_panel_replay &&
2879 	    ((IS_DISPLAY_VERx100_STEP(display, 1400, STEP_A0, STEP_B0) ||
2880 	      display->platform.alderlake_p || display->platform.tigerlake)) &&
2881 	    crtc_state->splitter.enable)
2882 		crtc_state->psr2_su_area.y1 = 0;
2883 
2884 	if (intel_display_wa(display, INTEL_DISPLAY_WA_16029024088) &&
2885 	    crtc_state->req_psr2_sdp_prior_scanline &&
2886 	    !crtc_state->enable_psr2_su_region_et)
2887 		crtc_state->psr2_su_area.y1 = 0;
2888 
2889 	/* Wa 14019834836 */
2890 	if (DISPLAY_VER(display) == 30)
2891 		intel_psr_apply_pr_link_on_su_wa(crtc_state);
2892 }
2893 
2894 int intel_psr2_sel_fetch_update(struct intel_atomic_state *state,
2895 				struct intel_crtc *crtc)
2896 {
2897 	struct intel_display *display = to_intel_display(state);
2898 	const struct intel_crtc_state *old_crtc_state =
2899 		intel_atomic_get_old_crtc_state(state, crtc);
2900 	struct intel_crtc_state *crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
2901 	struct intel_plane_state *new_plane_state, *old_plane_state;
2902 	struct intel_plane *plane;
2903 	struct drm_rect display_area = {
2904 		.x1 = 0,
2905 		.y1 = 0,
2906 		.x2 = crtc_state->hw.adjusted_mode.crtc_hdisplay,
2907 		.y2 = crtc_state->hw.adjusted_mode.crtc_vdisplay,
2908 	};
2909 	bool full_update = false, su_area_changed;
2910 	int i;
2911 
2912 	/*
2913 	 * Selective fetch is not always usable, for instance it is dropped
2914 	 * while pipe CRC is active. The planes keep their selective fetch
2915 	 * enable bit set in hardware over that, and a plane disabled while
2916 	 * selective fetch is off never gets the bit cleared. Once selective
2917 	 * fetch comes back the hardware would resume fetching for a plane that
2918 	 * is no longer enabled and keep its DDB range reserved, so have the
2919 	 * plane update drop the bit for every plane of the pipe as selective
2920 	 * fetch is turned off.
2921 	 */
2922 	crtc_state->clear_psr2_sel_fetch = old_crtc_state->enable_psr2_sel_fetch &&
2923 		!crtc_state->enable_psr2_sel_fetch;
2924 
2925 	if (!crtc_state->enable_psr2_sel_fetch)
2926 		return 0;
2927 
2928 	crtc_state->psr2_su_area.x1 = 0;
2929 	crtc_state->psr2_su_area.y1 = -1;
2930 	crtc_state->psr2_su_area.x2 = drm_rect_width(&display_area);
2931 	crtc_state->psr2_su_area.y2 = -1;
2932 
2933 	if (!psr2_sel_fetch_pipe_state_supported(crtc_state)) {
2934 		full_update = true;
2935 		goto skip_su_area_calc;
2936 	}
2937 
2938 	/*
2939 	 * Calculate minimal selective fetch area of each plane and calculate
2940 	 * the pipe damaged area.
2941 	 * In the next loop the plane selective fetch area will actually be set
2942 	 * using whole pipe damaged area.
2943 	 */
2944 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
2945 					     new_plane_state, i) {
2946 		struct drm_rect src, damaged_area = { .x1 = 0, .y1 = -1,
2947 						      .x2 = INT_MAX };
2948 
2949 		if (new_plane_state->hw.crtc != crtc_state->uapi.crtc)
2950 			continue;
2951 
2952 		if (!new_plane_state->uapi.visible &&
2953 		    !old_plane_state->uapi.visible)
2954 			continue;
2955 
2956 		if (!psr2_sel_fetch_plane_state_supported(new_plane_state)) {
2957 			full_update = true;
2958 			break;
2959 		}
2960 
2961 		/*
2962 		 * If visibility or plane moved, mark the whole plane area as
2963 		 * damaged as it needs to be complete redraw in the new and old
2964 		 * position.
2965 		 */
2966 		if (new_plane_state->uapi.visible != old_plane_state->uapi.visible ||
2967 		    !drm_rect_equals(&new_plane_state->uapi.dst,
2968 				     &old_plane_state->uapi.dst)) {
2969 			if (old_plane_state->uapi.visible) {
2970 				damaged_area.y1 = old_plane_state->uapi.dst.y1;
2971 				damaged_area.y2 = old_plane_state->uapi.dst.y2;
2972 				clip_area_update(&crtc_state->psr2_su_area, &damaged_area,
2973 						 &display_area);
2974 			}
2975 
2976 			if (new_plane_state->uapi.visible) {
2977 				damaged_area.y1 = new_plane_state->uapi.dst.y1;
2978 				damaged_area.y2 = new_plane_state->uapi.dst.y2;
2979 				clip_area_update(&crtc_state->psr2_su_area, &damaged_area,
2980 						 &display_area);
2981 			}
2982 			continue;
2983 		} else if (new_plane_state->uapi.alpha != old_plane_state->uapi.alpha) {
2984 			/* If alpha changed mark the whole plane area as damaged */
2985 			damaged_area.y1 = new_plane_state->uapi.dst.y1;
2986 			damaged_area.y2 = new_plane_state->uapi.dst.y2;
2987 			clip_area_update(&crtc_state->psr2_su_area, &damaged_area,
2988 					 &display_area);
2989 			continue;
2990 		}
2991 
2992 		src = drm_plane_state_src(&new_plane_state->uapi);
2993 		drm_rect_fp_to_int(&src, &src);
2994 
2995 		if (!drm_atomic_helper_damage_merged(&old_plane_state->uapi,
2996 						     &new_plane_state->uapi, &damaged_area))
2997 			continue;
2998 
2999 		damaged_area.y1 += new_plane_state->uapi.dst.y1 - src.y1;
3000 		damaged_area.y2 += new_plane_state->uapi.dst.y1 - src.y1;
3001 		damaged_area.x1 += new_plane_state->uapi.dst.x1 - src.x1;
3002 		damaged_area.x2 += new_plane_state->uapi.dst.x1 - src.x1;
3003 
3004 		clip_area_update(&crtc_state->psr2_su_area, &damaged_area, &display_area);
3005 	}
3006 
3007 	/*
3008 	 * TODO: For now we are just using full update in case
3009 	 * selective fetch area calculation fails. To optimize this we
3010 	 * should identify cases where this happens and fix the area
3011 	 * calculation for those.
3012 	 */
3013 	if (crtc_state->psr2_su_area.y1 == -1) {
3014 		drm_info_once(display->drm,
3015 			      "Selective fetch area calculation failed in pipe %c\n",
3016 			      pipe_name(crtc->pipe));
3017 		full_update = true;
3018 	}
3019 
3020 	if (full_update)
3021 		goto skip_su_area_calc;
3022 
3023 	intel_psr_apply_su_area_workarounds(crtc_state);
3024 
3025 	do {
3026 		bool cursor_in_su_area = false;
3027 
3028 		/*
3029 		 * Adjust su area to cover cursor fully as necessary
3030 		 * (early transport). A visible cursor is in the state
3031 		 * even when it is not updated by itself as all planes
3032 		 * of the pipe are added into the state before the plane
3033 		 * checks when selective fetch is enabled.
3034 		 */
3035 		intel_psr2_sel_fetch_et_alignment(state, crtc, &display_area,
3036 						  &cursor_in_su_area);
3037 
3038 		su_area_changed = intel_psr2_sel_fetch_pipe_alignment(crtc_state);
3039 
3040 		/*
3041 		 * If the cursor was outside the SU area before
3042 		 * alignment, the alignment step (which only expands
3043 		 * SU) may pull the cursor partially inside, so we
3044 		 * must run ET alignment again to fully cover it. But
3045 		 * if the cursor was already fully inside before
3046 		 * alignment, expanding the SU area won't change that,
3047 		 * so no further work is needed.
3048 		 */
3049 		if (cursor_in_su_area)
3050 			break;
3051 	} while (su_area_changed);
3052 
3053 skip_su_area_calc:
3054 	if (full_update)
3055 		clip_area_update(&crtc_state->psr2_su_area, &display_area,
3056 				 &display_area);
3057 
3058 	/*
3059 	 * Now that we have the pipe damaged area check if it intersect with
3060 	 * every plane, if it does set the plane selective fetch area.
3061 	 */
3062 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
3063 					     new_plane_state, i) {
3064 		struct drm_rect *sel_fetch_area, inter;
3065 		struct intel_plane *linked = new_plane_state->planar_linked_plane;
3066 
3067 		if (new_plane_state->hw.crtc != crtc_state->uapi.crtc ||
3068 		    !new_plane_state->uapi.visible)
3069 			continue;
3070 
3071 		inter = crtc_state->psr2_su_area;
3072 		sel_fetch_area = &new_plane_state->psr2_sel_fetch_area;
3073 		if (drm_rect_intersect(&inter, &new_plane_state->uapi.dst)) {
3074 			sel_fetch_area->y1 = inter.y1 - new_plane_state->uapi.dst.y1;
3075 			sel_fetch_area->y2 = inter.y2 - new_plane_state->uapi.dst.y1;
3076 		}
3077 
3078 		if (!drm_rect_equals(&old_plane_state->psr2_sel_fetch_area,
3079 				     sel_fetch_area))
3080 			crtc_state->update_planes |= BIT(plane->id);
3081 
3082 		/*
3083 		 * Sel_fetch_area is calculated for UV plane. Use
3084 		 * same area for Y plane as well.
3085 		 */
3086 		if (linked) {
3087 			struct intel_plane_state *linked_new_plane_state;
3088 			struct intel_plane_state *linked_old_plane_state;
3089 			struct drm_rect *linked_sel_fetch_area;
3090 
3091 			linked_new_plane_state = intel_atomic_get_new_plane_state(state, linked);
3092 			if (!linked_new_plane_state)
3093 				return -EINVAL;
3094 
3095 			linked_old_plane_state = intel_atomic_get_old_plane_state(state, linked);
3096 
3097 			linked_sel_fetch_area = &linked_new_plane_state->psr2_sel_fetch_area;
3098 			linked_sel_fetch_area->y1 = sel_fetch_area->y1;
3099 			linked_sel_fetch_area->y2 = sel_fetch_area->y2;
3100 
3101 			if (!drm_rect_equals(&linked_old_plane_state->psr2_sel_fetch_area,
3102 					     linked_sel_fetch_area))
3103 				crtc_state->update_planes |= BIT(linked->id);
3104 		}
3105 	}
3106 
3107 	psr2_man_trk_ctl_calc(crtc_state, full_update);
3108 	crtc_state->pipe_srcsz_early_tpt =
3109 		psr2_pipe_srcsz_early_tpt_calc(crtc_state, full_update);
3110 	return 0;
3111 }
3112 
3113 void intel_psr2_panic_force_full_update(const struct intel_crtc_state *crtc_state)
3114 {
3115 	struct intel_display *display = to_intel_display(crtc_state);
3116 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3117 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
3118 	u32 val = man_trk_ctl_enable_bit_get(display);
3119 
3120 	/* SF partial frame enable has to be set even on full update */
3121 	val |= man_trk_ctl_partial_frame_bit_get(display);
3122 	val |= man_trk_ctl_continuos_full_frame(display);
3123 
3124 	/* Directly write the register */
3125 	intel_de_write_fw(display, PSR2_MAN_TRK_CTL(display, cpu_transcoder), val);
3126 
3127 	if (!crtc_state->enable_psr2_su_region_et)
3128 		return;
3129 
3130 	intel_de_write_fw(display, PIPE_SRCSZ_ERLY_TPT(crtc->pipe), 0);
3131 }
3132 
3133 void intel_psr_pre_plane_update(struct intel_atomic_state *state,
3134 				struct intel_crtc *crtc)
3135 {
3136 	struct intel_display *display = to_intel_display(state);
3137 	const struct intel_crtc_state *old_crtc_state =
3138 		intel_atomic_get_old_crtc_state(state, crtc);
3139 	const struct intel_crtc_state *new_crtc_state =
3140 		intel_atomic_get_new_crtc_state(state, crtc);
3141 	struct intel_encoder *encoder;
3142 
3143 	if (!HAS_PSR(display))
3144 		return;
3145 
3146 	for_each_intel_encoder_mask_with_psr(state->base.dev, encoder,
3147 					     old_crtc_state->uapi.encoder_mask) {
3148 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3149 		struct intel_psr *psr = &intel_dp->psr;
3150 
3151 		mutex_lock(&psr->lock);
3152 
3153 		if (!new_crtc_state->has_psr)
3154 			psr->no_psr_reason = new_crtc_state->no_psr_reason;
3155 
3156 		if (psr->enabled) {
3157 			/*
3158 			 * Reasons to disable:
3159 			 * - PSR disabled in new state
3160 			 * - All planes will go inactive
3161 			 * - Changing between PSR versions
3162 			 * - Region Early Transport changing
3163 			 * - Display WA #1136: skl, bxt
3164 			 */
3165 			if (intel_crtc_needs_modeset(new_crtc_state) ||
3166 			    new_crtc_state->update_m_n ||
3167 			    new_crtc_state->update_lrr ||
3168 			    !new_crtc_state->has_psr ||
3169 			    !new_crtc_state->active_planes ||
3170 			    new_crtc_state->has_sel_update != psr->sel_update_enabled ||
3171 			    new_crtc_state->enable_psr2_su_region_et != psr->su_region_et_enabled ||
3172 			    new_crtc_state->has_panel_replay != psr->panel_replay_enabled ||
3173 			    (DISPLAY_VER(display) < 11 && new_crtc_state->wm_level_disabled))
3174 				intel_psr_disable_locked(intel_dp);
3175 			else if (new_crtc_state->wm_level_disabled)
3176 				/* Wa_14015648006 */
3177 				wm_optimization_wa(intel_dp, new_crtc_state);
3178 		}
3179 
3180 		mutex_unlock(&psr->lock);
3181 	}
3182 }
3183 
3184 static void
3185 verify_panel_replay_dsc_state(const struct intel_crtc_state *crtc_state)
3186 {
3187 	struct intel_display *display = to_intel_display(crtc_state);
3188 
3189 	if (!crtc_state->has_panel_replay)
3190 		return;
3191 
3192 	drm_WARN_ON(display->drm,
3193 		    intel_dsc_enabled_on_link(crtc_state) &&
3194 		    crtc_state->panel_replay_dsc_support ==
3195 		    INTEL_DP_PANEL_REPLAY_DSC_NOT_SUPPORTED);
3196 }
3197 
3198 void intel_psr_post_plane_update(struct intel_atomic_state *state,
3199 				 struct intel_crtc *crtc)
3200 {
3201 	struct intel_display *display = to_intel_display(state);
3202 	const struct intel_crtc_state *crtc_state =
3203 		intel_atomic_get_new_crtc_state(state, crtc);
3204 	struct intel_encoder *encoder;
3205 	bool dc3co_allowed;
3206 
3207 	if (!crtc_state->has_psr)
3208 		return;
3209 
3210 	dc3co_allowed = intel_display_power_dc3co_allowed(display);
3211 
3212 	verify_panel_replay_dsc_state(crtc_state);
3213 
3214 	for_each_intel_encoder_mask_with_psr(state->base.dev, encoder,
3215 					     crtc_state->uapi.encoder_mask) {
3216 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3217 		struct intel_psr *psr = &intel_dp->psr;
3218 		bool keep_disabled = false;
3219 
3220 		mutex_lock(&psr->lock);
3221 
3222 		drm_WARN_ON(display->drm,
3223 			    psr->enabled && !crtc_state->active_planes);
3224 
3225 		if (psr->sink_not_reliable)
3226 			keep_disabled = true;
3227 
3228 		if (!crtc_state->active_planes) {
3229 			psr->no_psr_reason = "All planes inactive";
3230 			keep_disabled = true;
3231 		}
3232 
3233 		/* Display WA #1136: skl, bxt */
3234 		if (DISPLAY_VER(display) < 11 && crtc_state->wm_level_disabled) {
3235 			psr->no_psr_reason = "Workaround #1136 for skl, bxt";
3236 			keep_disabled = true;
3237 		}
3238 
3239 		intel_dp->psr.dc3co_allowed = dc3co_allowed;
3240 
3241 		if (!psr->enabled && !keep_disabled)
3242 			intel_psr_enable_locked(intel_dp, crtc_state);
3243 		else if (psr->enabled && !crtc_state->wm_level_disabled)
3244 			/* Wa_14015648006 */
3245 			wm_optimization_wa(intel_dp, crtc_state);
3246 
3247 		/* Force a PSR exit when enabling CRC to avoid CRC timeouts */
3248 		if (crtc_state->crc_enabled && psr->enabled)
3249 			intel_psr_force_update(intel_dp);
3250 
3251 		/*
3252 		 * Clear possible busy bits in case we have
3253 		 * invalidate -> flip -> flush sequence.
3254 		 */
3255 		intel_dp->psr.busy_frontbuffer_bits = 0;
3256 
3257 		if (intel_dp->psr.dc3co_allowed) {
3258 			mod_delayed_work(display->wq.unordered, &intel_dp->psr.dc3co_work,
3259 					 intel_dp->psr.dc3co_exit_delay);
3260 		}
3261 
3262 		mutex_unlock(&psr->lock);
3263 	}
3264 }
3265 
3266 /*
3267  * From bspec: Panel Self Refresh (BDW+)
3268  * Max. time for PSR to idle = Inverse of the refresh rate + 6 ms of
3269  * exit training time + 1.5 ms of aux channel handshake. 50 ms is
3270  * defensive enough to cover everything.
3271  */
3272 #define PSR_IDLE_TIMEOUT_MS 50
3273 
3274 static int
3275 _psr2_ready_for_pipe_update_locked(const struct intel_crtc_state *new_crtc_state,
3276 				   struct intel_dsb *dsb)
3277 {
3278 	struct intel_display *display = to_intel_display(new_crtc_state);
3279 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
3280 
3281 	/*
3282 	 * Any state lower than EDP_PSR2_STATUS_STATE_DEEP_SLEEP is enough.
3283 	 * As all higher states has bit 4 of PSR2 state set we can just wait for
3284 	 * EDP_PSR2_STATUS_STATE_DEEP_SLEEP to be cleared.
3285 	 */
3286 	if (dsb) {
3287 		intel_dsb_poll(dsb, EDP_PSR2_STATUS(display, cpu_transcoder),
3288 			       EDP_PSR2_STATUS_STATE_DEEP_SLEEP, 0, 200,
3289 			       PSR_IDLE_TIMEOUT_MS * 1000 / 200);
3290 		return true;
3291 	}
3292 
3293 	return intel_de_wait_for_clear_ms(display,
3294 				       EDP_PSR2_STATUS(display, cpu_transcoder),
3295 				       EDP_PSR2_STATUS_STATE_DEEP_SLEEP,
3296 				       PSR_IDLE_TIMEOUT_MS);
3297 }
3298 
3299 static int
3300 _psr1_ready_for_pipe_update_locked(const struct intel_crtc_state *new_crtc_state,
3301 				   struct intel_dsb *dsb)
3302 {
3303 	struct intel_display *display = to_intel_display(new_crtc_state);
3304 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
3305 
3306 	if (dsb) {
3307 		intel_dsb_poll(dsb, psr_status_reg(display, cpu_transcoder),
3308 			       EDP_PSR_STATUS_STATE_MASK, 0, 200,
3309 			       PSR_IDLE_TIMEOUT_MS * 1000 / 200);
3310 		return true;
3311 	}
3312 
3313 	return intel_de_wait_for_clear_ms(display,
3314 				       psr_status_reg(display, cpu_transcoder),
3315 				       EDP_PSR_STATUS_STATE_MASK,
3316 				       PSR_IDLE_TIMEOUT_MS);
3317 }
3318 
3319 /**
3320  * intel_psr_wait_for_idle_locked - wait for PSR be ready for a pipe update
3321  * @new_crtc_state: new CRTC state
3322  *
3323  * This function is expected to be called from pipe_update_start() where it is
3324  * not expected to race with PSR enable or disable.
3325  */
3326 void intel_psr_wait_for_idle_locked(const struct intel_crtc_state *new_crtc_state)
3327 {
3328 	struct intel_display *display = to_intel_display(new_crtc_state);
3329 	struct intel_encoder *encoder;
3330 
3331 	if (!new_crtc_state->has_psr)
3332 		return;
3333 
3334 	for_each_intel_encoder_mask_with_psr(display->drm, encoder,
3335 					     new_crtc_state->uapi.encoder_mask) {
3336 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3337 		int ret;
3338 
3339 		lockdep_assert_held(&intel_dp->psr.lock);
3340 
3341 		if (!intel_dp->psr.enabled || intel_dp->psr.panel_replay_enabled)
3342 			continue;
3343 
3344 		if (intel_dp->psr.sel_update_enabled)
3345 			ret = _psr2_ready_for_pipe_update_locked(new_crtc_state,
3346 								 NULL);
3347 		else
3348 			ret = _psr1_ready_for_pipe_update_locked(new_crtc_state,
3349 								 NULL);
3350 
3351 		if (ret)
3352 			drm_err(display->drm,
3353 				"PSR wait timed out, atomic update may fail\n");
3354 	}
3355 }
3356 
3357 void intel_psr_wait_for_idle_dsb(struct intel_dsb *dsb,
3358 				 const struct intel_crtc_state *new_crtc_state)
3359 {
3360 	if (!new_crtc_state->has_psr || new_crtc_state->has_panel_replay)
3361 		return;
3362 
3363 	if (new_crtc_state->has_sel_update)
3364 		_psr2_ready_for_pipe_update_locked(new_crtc_state, dsb);
3365 	else
3366 		_psr1_ready_for_pipe_update_locked(new_crtc_state, dsb);
3367 }
3368 
3369 static bool __psr_wait_for_idle_locked(struct intel_dp *intel_dp)
3370 {
3371 	struct intel_display *display = to_intel_display(intel_dp);
3372 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
3373 	intel_reg_t reg;
3374 	u32 mask;
3375 	int err;
3376 
3377 	if (!intel_dp->psr.enabled)
3378 		return false;
3379 
3380 	if (intel_dp_is_edp(intel_dp) && (intel_dp->psr.sel_update_enabled ||
3381 					  intel_dp->psr.panel_replay_enabled)) {
3382 		reg = EDP_PSR2_STATUS(display, cpu_transcoder);
3383 		mask = EDP_PSR2_STATUS_STATE_MASK;
3384 	} else {
3385 		reg = psr_status_reg(display, cpu_transcoder);
3386 		mask = EDP_PSR_STATUS_STATE_MASK;
3387 	}
3388 
3389 	mutex_unlock(&intel_dp->psr.lock);
3390 
3391 	err = intel_de_wait_for_clear_ms(display, reg, mask, 50);
3392 	if (err)
3393 		drm_err(display->drm,
3394 			"Timed out waiting for PSR Idle for re-enable\n");
3395 
3396 	/* After the unlocked wait, verify that PSR is still wanted! */
3397 	mutex_lock(&intel_dp->psr.lock);
3398 	return err == 0 && intel_dp->psr.enabled && !intel_dp->psr.pause_counter;
3399 }
3400 
3401 static int intel_psr_fastset_force(struct intel_display *display)
3402 {
3403 	struct drm_connector_list_iter conn_iter;
3404 	struct drm_modeset_acquire_ctx ctx;
3405 	struct drm_atomic_commit *state;
3406 	struct drm_connector *conn;
3407 	int err = 0;
3408 
3409 	state = drm_atomic_commit_alloc(display->drm);
3410 	if (!state)
3411 		return -ENOMEM;
3412 
3413 	drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
3414 
3415 	state->acquire_ctx = &ctx;
3416 	to_intel_atomic_state(state)->internal = true;
3417 
3418 retry:
3419 	drm_connector_list_iter_begin(display->drm, &conn_iter);
3420 	drm_for_each_connector_iter(conn, &conn_iter) {
3421 		struct drm_connector_state *conn_state;
3422 		struct drm_crtc_state *crtc_state;
3423 
3424 		if (conn->connector_type != DRM_MODE_CONNECTOR_eDP)
3425 			continue;
3426 
3427 		conn_state = drm_atomic_get_connector_state(state, conn);
3428 		if (IS_ERR(conn_state)) {
3429 			err = PTR_ERR(conn_state);
3430 			break;
3431 		}
3432 
3433 		if (!conn_state->crtc)
3434 			continue;
3435 
3436 		crtc_state = drm_atomic_get_crtc_state(state, conn_state->crtc);
3437 		if (IS_ERR(crtc_state)) {
3438 			err = PTR_ERR(crtc_state);
3439 			break;
3440 		}
3441 
3442 		/* Mark mode as changed to trigger a pipe->update() */
3443 		crtc_state->mode_changed = true;
3444 	}
3445 	drm_connector_list_iter_end(&conn_iter);
3446 
3447 	if (err == 0)
3448 		err = drm_atomic_commit(state);
3449 
3450 	if (err == -EDEADLK) {
3451 		drm_atomic_commit_clear(state);
3452 		err = drm_modeset_backoff(&ctx);
3453 		if (!err)
3454 			goto retry;
3455 	}
3456 
3457 	drm_modeset_drop_locks(&ctx);
3458 	drm_modeset_acquire_fini(&ctx);
3459 	drm_atomic_commit_put(state);
3460 
3461 	return err;
3462 }
3463 
3464 int intel_psr_debug_set(struct intel_dp *intel_dp, u64 val)
3465 {
3466 	struct intel_display *display = to_intel_display(intel_dp);
3467 	const u32 mode = val & I915_PSR_DEBUG_MODE_MASK;
3468 	const u32 disable_bits = val & (I915_PSR_DEBUG_SU_REGION_ET_DISABLE |
3469 					I915_PSR_DEBUG_PANEL_REPLAY_DISABLE);
3470 	u32 old_mode, old_disable_bits;
3471 	int ret;
3472 
3473 	if (val & ~(I915_PSR_DEBUG_IRQ | I915_PSR_DEBUG_SU_REGION_ET_DISABLE |
3474 		    I915_PSR_DEBUG_PANEL_REPLAY_DISABLE |
3475 		    I915_PSR_DEBUG_MODE_MASK) ||
3476 	    mode > I915_PSR_DEBUG_ENABLE_SEL_FETCH) {
3477 		drm_dbg_kms(display->drm, "Invalid debug mask %llx\n", val);
3478 		return -EINVAL;
3479 	}
3480 
3481 	ret = mutex_lock_interruptible(&intel_dp->psr.lock);
3482 	if (ret)
3483 		return ret;
3484 
3485 	old_mode = intel_dp->psr.debug & I915_PSR_DEBUG_MODE_MASK;
3486 	old_disable_bits = intel_dp->psr.debug &
3487 		(I915_PSR_DEBUG_SU_REGION_ET_DISABLE |
3488 		 I915_PSR_DEBUG_PANEL_REPLAY_DISABLE);
3489 
3490 	intel_dp->psr.debug = val;
3491 
3492 	/*
3493 	 * Do it right away if it's already enabled, otherwise it will be done
3494 	 * when enabling the source.
3495 	 */
3496 	if (intel_dp->psr.enabled)
3497 		psr_irq_control(intel_dp);
3498 
3499 	mutex_unlock(&intel_dp->psr.lock);
3500 
3501 	if (old_mode != mode || old_disable_bits != disable_bits)
3502 		ret = intel_psr_fastset_force(display);
3503 
3504 	return ret;
3505 }
3506 
3507 static void intel_psr_handle_irq(struct intel_dp *intel_dp)
3508 {
3509 	struct intel_psr *psr = &intel_dp->psr;
3510 
3511 	intel_psr_disable_locked(intel_dp);
3512 	psr->sink_not_reliable = true;
3513 	/* let's make sure that sink is awaken */
3514 	drm_dp_dpcd_write_byte(&intel_dp->aux, DP_SET_POWER, DP_SET_POWER_D0);
3515 }
3516 
3517 static void intel_psr_work(struct work_struct *work)
3518 {
3519 	struct intel_dp *intel_dp =
3520 		container_of(work, typeof(*intel_dp), psr.work);
3521 
3522 	mutex_lock(&intel_dp->psr.lock);
3523 
3524 	if (!intel_dp->psr.enabled)
3525 		goto unlock;
3526 
3527 	if (READ_ONCE(intel_dp->psr.irq_aux_error)) {
3528 		intel_psr_handle_irq(intel_dp);
3529 		goto unlock;
3530 	}
3531 
3532 	if (intel_dp->psr.pause_counter)
3533 		goto unlock;
3534 
3535 	/*
3536 	 * We have to make sure PSR is ready for re-enable
3537 	 * otherwise it keeps disabled until next full enable/disable cycle.
3538 	 * PSR might take some time to get fully disabled
3539 	 * and be ready for re-enable.
3540 	 */
3541 	if (!__psr_wait_for_idle_locked(intel_dp))
3542 		goto unlock;
3543 
3544 	/*
3545 	 * The delayed work can race with an invalidate hence we need to
3546 	 * recheck. Since psr_flush first clears this and then reschedules we
3547 	 * won't ever miss a flush when bailing out here.
3548 	 */
3549 	if (intel_dp->psr.busy_frontbuffer_bits || intel_dp->psr.active)
3550 		goto unlock;
3551 
3552 	intel_psr_activate(intel_dp);
3553 unlock:
3554 	mutex_unlock(&intel_dp->psr.lock);
3555 }
3556 
3557 static void intel_psr_configure_full_frame_update(struct intel_dp *intel_dp)
3558 {
3559 	struct intel_display *display = to_intel_display(intel_dp);
3560 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
3561 
3562 	if (!intel_dp->psr.psr2_sel_fetch_enabled)
3563 		return;
3564 
3565 	if (DISPLAY_VER(display) >= 20)
3566 		intel_de_write(display, LNL_SFF_CTL(cpu_transcoder),
3567 			       LNL_SFF_CTL_SF_SINGLE_FULL_FRAME);
3568 	else
3569 		intel_de_write(display,
3570 			       PSR2_MAN_TRK_CTL(display, cpu_transcoder),
3571 			       man_trk_ctl_enable_bit_get(display) |
3572 			       man_trk_ctl_partial_frame_bit_get(display) |
3573 			       man_trk_ctl_single_full_frame_bit_get(display) |
3574 			       man_trk_ctl_continuos_full_frame(display));
3575 }
3576 
3577 static void _psr_invalidate_handle(struct intel_dp *intel_dp)
3578 {
3579 	struct intel_display *display = to_intel_display(intel_dp);
3580 
3581 	if (DISPLAY_VER(display) < 20 && intel_dp->psr.psr2_sel_fetch_enabled) {
3582 		if (!intel_dp->psr.psr2_sel_fetch_cff_enabled) {
3583 			intel_dp->psr.psr2_sel_fetch_cff_enabled = true;
3584 			intel_psr_configure_full_frame_update(intel_dp);
3585 		}
3586 
3587 		intel_psr_force_update(intel_dp);
3588 	} else {
3589 		intel_psr_exit(intel_dp);
3590 	}
3591 }
3592 
3593 /**
3594  * intel_psr_invalidate - Invalidate PSR
3595  * @display: display device
3596  * @frontbuffer_bits: frontbuffer plane tracking bits
3597  * @origin: which operation caused the invalidate
3598  *
3599  * Since the hardware frontbuffer tracking has gaps we need to integrate
3600  * with the software frontbuffer tracking. This function gets called every
3601  * time frontbuffer rendering starts and a buffer gets dirtied. PSR must be
3602  * disabled if the frontbuffer mask contains a buffer relevant to PSR.
3603  *
3604  * Dirty frontbuffers relevant to PSR are tracked in busy_frontbuffer_bits."
3605  */
3606 void intel_psr_invalidate(struct intel_display *display,
3607 			  unsigned frontbuffer_bits, enum fb_op_origin origin)
3608 {
3609 	struct intel_encoder *encoder;
3610 
3611 	if (origin == ORIGIN_FLIP)
3612 		return;
3613 
3614 	for_each_intel_encoder_with_psr(display->drm, encoder) {
3615 		unsigned int pipe_frontbuffer_bits = frontbuffer_bits;
3616 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3617 
3618 		mutex_lock(&intel_dp->psr.lock);
3619 		if (!intel_dp->psr.enabled) {
3620 			mutex_unlock(&intel_dp->psr.lock);
3621 			continue;
3622 		}
3623 
3624 		pipe_frontbuffer_bits &=
3625 			INTEL_FRONTBUFFER_ALL_MASK(intel_dp->psr.pipe);
3626 		intel_dp->psr.busy_frontbuffer_bits |= pipe_frontbuffer_bits;
3627 
3628 		if (pipe_frontbuffer_bits)
3629 			_psr_invalidate_handle(intel_dp);
3630 
3631 		mutex_unlock(&intel_dp->psr.lock);
3632 	}
3633 }
3634 
3635 static void _psr_flush_handle(struct intel_dp *intel_dp)
3636 {
3637 	struct intel_display *display = to_intel_display(intel_dp);
3638 
3639 	if (DISPLAY_VER(display) >= 20) {
3640 		/*
3641 		 * We can use PSR exit on LunarLake onwards. Also
3642 		 * using trans push mechanism to trigger Frame Change
3643 		 * event requires using PSR exit.
3644 		 */
3645 		intel_psr_exit(intel_dp);
3646 	} else if (intel_dp->psr.psr2_sel_fetch_enabled) {
3647 		/* Selective fetch prior LNL */
3648 		if (intel_dp->psr.psr2_sel_fetch_cff_enabled) {
3649 			/* can we turn CFF off? */
3650 			if (intel_dp->psr.busy_frontbuffer_bits == 0)
3651 				intel_dp->psr.psr2_sel_fetch_cff_enabled = false;
3652 		}
3653 
3654 		/*
3655 		 * Still keep cff bit enabled as we don't have proper SU
3656 		 * configuration in case update is sent for any reason after
3657 		 * sff bit gets cleared by the HW on next vblank.
3658 		 *
3659 		 * NOTE: Setting cff bit is not needed for LunarLake onwards as
3660 		 * we have own register for SFF bit and we are not overwriting
3661 		 * existing SU configuration
3662 		 */
3663 		intel_psr_configure_full_frame_update(intel_dp);
3664 
3665 		intel_psr_force_update(intel_dp);
3666 	} else {
3667 		/*
3668 		 * On older platforms using PSR exit was seen causing problems
3669 		 */
3670 		intel_psr_force_update(intel_dp);
3671 	}
3672 
3673 	if (!intel_dp->psr.active && !intel_dp->psr.busy_frontbuffer_bits)
3674 		queue_work(display->wq.unordered, &intel_dp->psr.work);
3675 }
3676 
3677 /**
3678  * intel_psr_flush - Flush PSR
3679  * @display: display device
3680  * @frontbuffer_bits: frontbuffer plane tracking bits
3681  * @origin: which operation caused the flush
3682  *
3683  * Since the hardware frontbuffer tracking has gaps we need to integrate
3684  * with the software frontbuffer tracking. This function gets called every
3685  * time frontbuffer rendering has completed and flushed out to memory. PSR
3686  * can be enabled again if no other frontbuffer relevant to PSR is dirty.
3687  *
3688  * Dirty frontbuffers relevant to PSR are tracked in busy_frontbuffer_bits.
3689  */
3690 void intel_psr_flush(struct intel_display *display,
3691 		     unsigned frontbuffer_bits, enum fb_op_origin origin)
3692 {
3693 	struct intel_encoder *encoder;
3694 
3695 	for_each_intel_encoder_with_psr(display->drm, encoder) {
3696 		unsigned int pipe_frontbuffer_bits = frontbuffer_bits;
3697 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3698 
3699 		mutex_lock(&intel_dp->psr.lock);
3700 		if (!intel_dp->psr.enabled) {
3701 			mutex_unlock(&intel_dp->psr.lock);
3702 			continue;
3703 		}
3704 
3705 		pipe_frontbuffer_bits &=
3706 			INTEL_FRONTBUFFER_ALL_MASK(intel_dp->psr.pipe);
3707 		intel_dp->psr.busy_frontbuffer_bits &= ~pipe_frontbuffer_bits;
3708 
3709 		/*
3710 		 * If the PSR is paused by an explicit intel_psr_paused() call,
3711 		 * we have to ensure that the PSR is not activated until
3712 		 * intel_psr_resume() is called.
3713 		 */
3714 		if (intel_dp->psr.pause_counter)
3715 			goto unlock;
3716 
3717 		if (origin == ORIGIN_FLIP ||
3718 		    (origin == ORIGIN_CURSOR_UPDATE &&
3719 		     !intel_dp->psr.psr2_sel_fetch_enabled)) {
3720 			psr2_dc3co_flush_locked(intel_dp, frontbuffer_bits);
3721 			goto unlock;
3722 		}
3723 
3724 		if (pipe_frontbuffer_bits == 0)
3725 			goto unlock;
3726 
3727 		/* By definition flush = invalidate + flush */
3728 		_psr_flush_handle(intel_dp);
3729 unlock:
3730 		mutex_unlock(&intel_dp->psr.lock);
3731 	}
3732 }
3733 
3734 /**
3735  * intel_psr_init - Init basic PSR work and mutex.
3736  * @intel_dp: Intel DP
3737  *
3738  * This function is called after the initializing connector.
3739  * (the initializing of connector treats the handling of connector capabilities)
3740  * And it initializes basic PSR stuff for each DP Encoder.
3741  */
3742 void intel_psr_init(struct intel_dp *intel_dp)
3743 {
3744 	struct intel_display *display = to_intel_display(intel_dp);
3745 	struct intel_connector *connector = intel_dp->attached_connector;
3746 	struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
3747 
3748 	if (!(HAS_PSR(display) || HAS_DP20(display)))
3749 		return;
3750 
3751 	/*
3752 	 * HSW spec explicitly says PSR is tied to port A.
3753 	 * BDW+ platforms have a instance of PSR registers per transcoder but
3754 	 * BDW, GEN9 and GEN11 are not validated by HW team in other transcoder
3755 	 * than eDP one.
3756 	 * For now it only supports one instance of PSR for BDW, GEN9 and GEN11.
3757 	 * So lets keep it hardcoded to PORT_A for BDW, GEN9 and GEN11.
3758 	 * But GEN12 supports a instance of PSR registers per transcoder.
3759 	 */
3760 	if (DISPLAY_VER(display) < 12 && dig_port->base.port != PORT_A) {
3761 		drm_dbg_kms(display->drm,
3762 			    "PSR condition failed: Port not supported\n");
3763 		return;
3764 	}
3765 
3766 	if ((HAS_DP20(display) && !intel_dp_is_edp(intel_dp)) ||
3767 	    DISPLAY_VER(display) >= 20)
3768 		intel_dp->psr.source_panel_replay_support = true;
3769 
3770 	if (HAS_PSR(display) && intel_dp_is_edp(intel_dp))
3771 		intel_dp->psr.source_support = true;
3772 
3773 	/* Set link_standby x link_off defaults */
3774 	if (DISPLAY_VER(display) < 12)
3775 		/* For new platforms up to TGL let's respect VBT back again */
3776 		intel_dp->psr.link_standby = connector->panel.vbt.psr.full_link;
3777 
3778 	INIT_WORK(&intel_dp->psr.work, intel_psr_work);
3779 	INIT_DELAYED_WORK(&intel_dp->psr.dc3co_work, psr2_dc3co_disable_work);
3780 	mutex_init(&intel_dp->psr.lock);
3781 }
3782 
3783 static int psr_get_status_and_error_status(struct intel_dp *intel_dp,
3784 					   u8 *status, u8 *error_status)
3785 {
3786 	struct drm_dp_aux *aux = &intel_dp->aux;
3787 	int ret;
3788 	unsigned int offset;
3789 
3790 	offset = intel_dp->psr.panel_replay_enabled ?
3791 		 DP_SINK_DEVICE_PR_AND_FRAME_LOCK_STATUS : DP_PSR_STATUS;
3792 
3793 	ret = drm_dp_dpcd_read_byte(aux, offset, status);
3794 	if (ret < 0)
3795 		return ret;
3796 
3797 	offset = intel_dp->psr.panel_replay_enabled ?
3798 		 DP_PANEL_REPLAY_ERROR_STATUS : DP_PSR_ERROR_STATUS;
3799 
3800 	ret = drm_dp_dpcd_read_byte(aux, offset, error_status);
3801 	if (ret < 0)
3802 		return ret;
3803 
3804 	*status = *status & DP_PSR_SINK_STATE_MASK;
3805 
3806 	return 0;
3807 }
3808 
3809 static void psr_alpm_check(struct intel_dp *intel_dp)
3810 {
3811 	struct intel_psr *psr = &intel_dp->psr;
3812 
3813 	if (!psr->sel_update_enabled)
3814 		return;
3815 
3816 	if (intel_alpm_get_error(intel_dp)) {
3817 		intel_psr_disable_locked(intel_dp);
3818 		psr->sink_not_reliable = true;
3819 	}
3820 }
3821 
3822 static void psr_capability_changed_check(struct intel_dp *intel_dp)
3823 {
3824 	struct intel_display *display = to_intel_display(intel_dp);
3825 	struct intel_psr *psr = &intel_dp->psr;
3826 	int ret;
3827 	u8 val;
3828 
3829 	ret = drm_dp_dpcd_read_byte(&intel_dp->aux, DP_PSR_ESI, &val);
3830 	if (ret < 0) {
3831 		drm_err(display->drm, "Error reading DP_PSR_ESI (%pe)\n", ERR_PTR(ret));
3832 		return;
3833 	}
3834 
3835 	if (val & DP_PSR_CAPS_CHANGE) {
3836 		intel_psr_disable_locked(intel_dp);
3837 		psr->sink_not_reliable = true;
3838 		drm_dbg_kms(display->drm,
3839 			    "Sink PSR capability changed, disabling PSR\n");
3840 
3841 		/* Clearing it */
3842 		drm_dp_dpcd_write_byte(&intel_dp->aux, DP_PSR_ESI, val);
3843 	}
3844 }
3845 
3846 /*
3847  * On common bits:
3848  * DP_PSR_RFB_STORAGE_ERROR == DP_PANEL_REPLAY_RFB_STORAGE_ERROR
3849  * DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR == DP_PANEL_REPLAY_VSC_SDP_UNCORRECTABLE_ERROR
3850  * DP_PSR_LINK_CRC_ERROR == DP_PANEL_REPLAY_LINK_CRC_ERROR
3851  * this function is relying on PSR definitions
3852  */
3853 void intel_psr_short_pulse(struct intel_dp *intel_dp)
3854 {
3855 	struct intel_display *display = to_intel_display(intel_dp);
3856 	struct intel_psr *psr = &intel_dp->psr;
3857 	u8 status, error_status;
3858 	bool panel_replay_enabled;
3859 	const u8 errors = DP_PSR_RFB_STORAGE_ERROR |
3860 			  DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR |
3861 			  DP_PSR_LINK_CRC_ERROR;
3862 
3863 	if (!CAN_PSR(intel_dp) && !CAN_PANEL_REPLAY(intel_dp))
3864 		return;
3865 
3866 	mutex_lock(&psr->lock);
3867 
3868 	psr->link_ok = false;
3869 
3870 	if (!psr->enabled)
3871 		goto exit;
3872 
3873 	if (psr_get_status_and_error_status(intel_dp, &status, &error_status)) {
3874 		drm_err(display->drm,
3875 			"Error reading PSR status or error status\n");
3876 		goto exit;
3877 	}
3878 
3879 	/*
3880 	 * Save this before intel_psr_disable_locked() clears it; the error
3881 	 * status is acknowledged to a different DPCD address depending on it.
3882 	 */
3883 	panel_replay_enabled = psr->panel_replay_enabled;
3884 
3885 	if ((!psr->panel_replay_enabled && status == DP_PSR_SINK_INTERNAL_ERROR) ||
3886 	    (error_status & errors)) {
3887 		intel_psr_disable_locked(intel_dp);
3888 		psr->sink_not_reliable = true;
3889 	}
3890 
3891 	if (!psr->panel_replay_enabled && status == DP_PSR_SINK_INTERNAL_ERROR &&
3892 	    !error_status)
3893 		drm_dbg_kms(display->drm,
3894 			    "PSR sink internal error, disabling PSR\n");
3895 	if (error_status & DP_PSR_RFB_STORAGE_ERROR)
3896 		drm_dbg_kms(display->drm,
3897 			    "PSR RFB storage error, disabling PSR\n");
3898 	if (error_status & DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR)
3899 		drm_dbg_kms(display->drm,
3900 			    "PSR VSC SDP uncorrectable error, disabling PSR\n");
3901 	if (error_status & DP_PSR_LINK_CRC_ERROR)
3902 		drm_dbg_kms(display->drm,
3903 			    "PSR Link CRC error, disabling PSR\n");
3904 
3905 	if (error_status & ~errors)
3906 		drm_err(display->drm,
3907 			"PSR_ERROR_STATUS unhandled errors %x\n",
3908 			error_status & ~errors);
3909 	/* clear status register */
3910 	drm_dp_dpcd_write_byte(&intel_dp->aux,
3911 			       panel_replay_enabled ?
3912 			       DP_PANEL_REPLAY_ERROR_STATUS : DP_PSR_ERROR_STATUS,
3913 			       error_status);
3914 
3915 	if (!psr->panel_replay_enabled) {
3916 		psr_alpm_check(intel_dp);
3917 		psr_capability_changed_check(intel_dp);
3918 	}
3919 
3920 exit:
3921 	mutex_unlock(&psr->lock);
3922 }
3923 
3924 bool intel_psr_enabled(struct intel_dp *intel_dp)
3925 {
3926 	bool ret;
3927 
3928 	if (!CAN_PSR(intel_dp))
3929 		return false;
3930 
3931 	mutex_lock(&intel_dp->psr.lock);
3932 	ret = intel_dp->psr.enabled;
3933 	mutex_unlock(&intel_dp->psr.lock);
3934 
3935 	return ret;
3936 }
3937 
3938 /**
3939  * intel_psr_link_ok - return psr->link_ok
3940  * @intel_dp: struct intel_dp
3941  *
3942  * We are seeing unexpected link re-trainings with some panels. This is caused
3943  * by panel stating bad link status after PSR is enabled. Code checking link
3944  * status can call this to ensure it can ignore bad link status stated by the
3945  * panel I.e. if panel is stating bad link and intel_psr_link_ok is stating link
3946  * is ok caller should rely on latter.
3947  *
3948  * Return value of link_ok
3949  */
3950 bool intel_psr_link_ok(struct intel_dp *intel_dp)
3951 {
3952 	bool ret;
3953 
3954 	if ((!CAN_PSR(intel_dp) && !CAN_PANEL_REPLAY(intel_dp)) ||
3955 	    !intel_dp_is_edp(intel_dp))
3956 		return false;
3957 
3958 	mutex_lock(&intel_dp->psr.lock);
3959 	ret = intel_dp->psr.link_ok;
3960 	mutex_unlock(&intel_dp->psr.lock);
3961 
3962 	return ret;
3963 }
3964 
3965 /**
3966  * intel_psr_lock - grab PSR lock
3967  * @crtc_state: the crtc state
3968  *
3969  * This is initially meant to be used by around CRTC update, when
3970  * vblank sensitive registers are updated and we need grab the lock
3971  * before it to avoid vblank evasion.
3972  */
3973 void intel_psr_lock(const struct intel_crtc_state *crtc_state)
3974 {
3975 	struct intel_display *display = to_intel_display(crtc_state);
3976 	struct intel_encoder *encoder;
3977 
3978 	if (!crtc_state->has_psr)
3979 		return;
3980 
3981 	for_each_intel_encoder_mask_with_psr(display->drm, encoder,
3982 					     crtc_state->uapi.encoder_mask) {
3983 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
3984 
3985 		mutex_lock(&intel_dp->psr.lock);
3986 		break;
3987 	}
3988 }
3989 
3990 /**
3991  * intel_psr_unlock - release PSR lock
3992  * @crtc_state: the crtc state
3993  *
3994  * Release the PSR lock that was held during pipe update.
3995  */
3996 void intel_psr_unlock(const struct intel_crtc_state *crtc_state)
3997 {
3998 	struct intel_display *display = to_intel_display(crtc_state);
3999 	struct intel_encoder *encoder;
4000 
4001 	if (!crtc_state->has_psr)
4002 		return;
4003 
4004 	for_each_intel_encoder_mask_with_psr(display->drm, encoder,
4005 					     crtc_state->uapi.encoder_mask) {
4006 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4007 
4008 		mutex_unlock(&intel_dp->psr.lock);
4009 		break;
4010 	}
4011 }
4012 
4013 /* Wa_16025596647 */
4014 static void intel_psr_apply_underrun_on_idle_wa_locked(struct intel_dp *intel_dp)
4015 {
4016 	struct intel_display *display = to_intel_display(intel_dp);
4017 	bool dc5_dc6_blocked;
4018 
4019 	if (!intel_dp->psr.active || !intel_dp->psr.pkg_c_latency_used)
4020 		return;
4021 
4022 	dc5_dc6_blocked = is_dc5_dc6_blocked(intel_dp);
4023 
4024 	if (intel_dp->psr.sel_update_enabled)
4025 		psr2_program_idle_frames(intel_dp, dc5_dc6_blocked ? 0 :
4026 					 psr_compute_idle_frames(intel_dp));
4027 	else
4028 		intel_dmc_start_pkgc_exit_at_start_of_undelayed_vblank(display,
4029 								       intel_dp->psr.pipe,
4030 								       dc5_dc6_blocked);
4031 }
4032 
4033 static void psr_dc5_dc6_wa_work(struct work_struct *work)
4034 {
4035 	struct intel_display *display = container_of(work, typeof(*display),
4036 						     psr_dc5_dc6_wa_work);
4037 	struct intel_encoder *encoder;
4038 
4039 	for_each_intel_encoder_with_psr(display->drm, encoder) {
4040 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4041 
4042 		mutex_lock(&intel_dp->psr.lock);
4043 
4044 		if (intel_dp->psr.enabled && !intel_dp->psr.panel_replay_enabled &&
4045 		    !intel_dp->psr.pkg_c_latency_used)
4046 			intel_psr_apply_underrun_on_idle_wa_locked(intel_dp);
4047 
4048 		mutex_unlock(&intel_dp->psr.lock);
4049 	}
4050 }
4051 
4052 /**
4053  * intel_psr_notify_dc5_dc6 - Notify PSR about enable/disable dc5/dc6
4054  * @display: intel atomic state
4055  *
4056  * This is targeted for underrun on idle PSR HW bug (Wa_16025596647) to schedule
4057  * psr_dc5_dc6_wa_work used for applying/removing the workaround.
4058  */
4059 void intel_psr_notify_dc5_dc6(struct intel_display *display)
4060 {
4061 	if (!intel_display_wa(display, INTEL_DISPLAY_WA_16025596647))
4062 		return;
4063 
4064 	schedule_work(&display->psr_dc5_dc6_wa_work);
4065 }
4066 
4067 /**
4068  * intel_psr_dc5_dc6_wa_init - Init work for underrun on idle PSR HW bug wa
4069  * @display: intel atomic state
4070  *
4071  * This is targeted for underrun on idle PSR HW bug (Wa_16025596647) to init
4072  * psr_dc5_dc6_wa_work used for applying the workaround.
4073  */
4074 void intel_psr_dc5_dc6_wa_init(struct intel_display *display)
4075 {
4076 	if (!intel_display_wa(display, INTEL_DISPLAY_WA_16025596647))
4077 		return;
4078 
4079 	INIT_WORK(&display->psr_dc5_dc6_wa_work, psr_dc5_dc6_wa_work);
4080 }
4081 
4082 /**
4083  * intel_psr_notify_pipe_change - Notify PSR about enable/disable of a pipe
4084  * @state: intel atomic state
4085  * @crtc: intel crtc
4086  * @enable: enable/disable
4087  *
4088  * This is targeted for underrun on idle PSR HW bug (Wa_16025596647) to apply
4089  * remove the workaround when pipe is getting enabled/disabled
4090  */
4091 void intel_psr_notify_pipe_change(struct intel_atomic_state *state,
4092 				  struct intel_crtc *crtc, bool enable)
4093 {
4094 	struct intel_display *display = to_intel_display(state);
4095 	struct intel_encoder *encoder;
4096 
4097 	if (!intel_display_wa(display, INTEL_DISPLAY_WA_16025596647))
4098 		return;
4099 
4100 	for_each_intel_encoder_with_psr(display->drm, encoder) {
4101 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4102 		u8 active_non_psr_pipes;
4103 
4104 		mutex_lock(&intel_dp->psr.lock);
4105 
4106 		if (!intel_dp->psr.enabled || intel_dp->psr.panel_replay_enabled)
4107 			goto unlock;
4108 
4109 		active_non_psr_pipes = intel_dp->psr.active_non_psr_pipes;
4110 
4111 		if (enable)
4112 			active_non_psr_pipes |= BIT(crtc->pipe);
4113 		else
4114 			active_non_psr_pipes &= ~BIT(crtc->pipe);
4115 
4116 		if (active_non_psr_pipes == intel_dp->psr.active_non_psr_pipes)
4117 			goto unlock;
4118 
4119 		if ((enable && intel_dp->psr.active_non_psr_pipes) ||
4120 		    (!enable && !intel_dp->psr.active_non_psr_pipes) ||
4121 		    !intel_dp->psr.pkg_c_latency_used) {
4122 			intel_dp->psr.active_non_psr_pipes = active_non_psr_pipes;
4123 			goto unlock;
4124 		}
4125 
4126 		intel_dp->psr.active_non_psr_pipes = active_non_psr_pipes;
4127 
4128 		intel_psr_apply_underrun_on_idle_wa_locked(intel_dp);
4129 unlock:
4130 		mutex_unlock(&intel_dp->psr.lock);
4131 	}
4132 }
4133 
4134 /**
4135  * intel_psr_notify_vblank_enable_disable - Notify PSR about enable/disable of vblank
4136  * @display: intel display struct
4137  * @enable: enable/disable
4138  *
4139  * This is targeted for underrun on idle PSR HW bug (Wa_16025596647) to apply
4140  * remove the workaround when vblank is getting enabled/disabled
4141  */
4142 void intel_psr_notify_vblank_enable_disable(struct intel_display *display,
4143 					    bool enable)
4144 {
4145 	struct intel_encoder *encoder;
4146 
4147 	for_each_intel_encoder_with_psr(display->drm, encoder) {
4148 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4149 
4150 		mutex_lock(&intel_dp->psr.lock);
4151 		if (CAN_PANEL_REPLAY(intel_dp)) {
4152 			if (enable)
4153 				intel_dp->psr.vblank_wakeref =
4154 					intel_display_power_get(display,
4155 								POWER_DOMAIN_DC_OFF);
4156 			else
4157 				intel_display_power_put(display, POWER_DOMAIN_DC_OFF,
4158 							intel_dp->psr.vblank_wakeref);
4159 		}
4160 
4161 		if (intel_dp->psr.enabled && !intel_dp->psr.panel_replay_enabled &&
4162 		    intel_dp->psr.pkg_c_latency_used)
4163 			intel_psr_apply_underrun_on_idle_wa_locked(intel_dp);
4164 
4165 		mutex_unlock(&intel_dp->psr.lock);
4166 	}
4167 }
4168 
4169 static void
4170 psr_source_status(struct intel_dp *intel_dp, struct seq_file *m)
4171 {
4172 	struct intel_display *display = to_intel_display(intel_dp);
4173 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
4174 	const char *status = "unknown";
4175 	u32 val, status_val;
4176 
4177 	if ((intel_dp_is_edp(intel_dp) || DISPLAY_VER(display) >= 30) &&
4178 	    (intel_dp->psr.sel_update_enabled || intel_dp->psr.panel_replay_enabled)) {
4179 		static const char * const live_status[] = {
4180 			"IDLE",
4181 			"CAPTURE",
4182 			"CAPTURE_FS",
4183 			"SLEEP",
4184 			"BUFON_FW",
4185 			"ML_UP",
4186 			"SU_STANDBY",
4187 			"FAST_SLEEP",
4188 			"DEEP_SLEEP",
4189 			"BUF_ON",
4190 			"TG_ON"
4191 		};
4192 		val = intel_de_read(display,
4193 				    EDP_PSR2_STATUS(display, cpu_transcoder));
4194 		status_val = REG_FIELD_GET(EDP_PSR2_STATUS_STATE_MASK, val);
4195 		if (status_val < ARRAY_SIZE(live_status))
4196 			status = live_status[status_val];
4197 	} else {
4198 		static const char * const live_status[] = {
4199 			"IDLE",
4200 			"SRDONACK",
4201 			"SRDENT",
4202 			"BUFOFF",
4203 			"BUFON",
4204 			"AUXACK",
4205 			"SRDOFFACK",
4206 			"SRDENT_ON",
4207 		};
4208 		val = intel_de_read(display,
4209 				    psr_status_reg(display, cpu_transcoder));
4210 		status_val = REG_FIELD_GET(EDP_PSR_STATUS_STATE_MASK, val);
4211 		if (status_val < ARRAY_SIZE(live_status))
4212 			status = live_status[status_val];
4213 	}
4214 
4215 	seq_printf(m, "Source PSR/PanelReplay status: %s [0x%08x]\n", status, val);
4216 }
4217 
4218 static void intel_psr_sink_capability(struct intel_connector *connector,
4219 				      struct seq_file *m)
4220 {
4221 	seq_printf(m, "Sink support: PSR = %s",
4222 		   str_yes_no(connector->dp.psr_caps.support));
4223 
4224 	if (connector->dp.psr_caps.support)
4225 		seq_printf(m, " [0x%02x]", connector->dp.psr_caps.dpcd[0]);
4226 	if (connector->dp.psr_caps.dpcd[0] == DP_PSR2_WITH_Y_COORD_ET_SUPPORTED)
4227 		seq_printf(m, " (Early Transport)");
4228 	seq_printf(m, ", Panel Replay = %s", str_yes_no(connector->dp.panel_replay_caps.support));
4229 	seq_printf(m, ", Panel Replay Selective Update = %s",
4230 		   str_yes_no(connector->dp.panel_replay_caps.su_support));
4231 	seq_printf(m, ", Panel Replay DSC support = %s",
4232 		   panel_replay_dsc_support_str(connector->dp.panel_replay_caps.dsc_support));
4233 	if (connector->dp.panel_replay_caps.dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_SUPPORT)] &
4234 	    DP_PANEL_REPLAY_EARLY_TRANSPORT_SUPPORT)
4235 		seq_printf(m, " (Early Transport)");
4236 	seq_printf(m, "\n");
4237 }
4238 
4239 static void intel_psr_print_mode(struct intel_dp *intel_dp,
4240 				 struct seq_file *m)
4241 {
4242 	struct intel_psr *psr = &intel_dp->psr;
4243 	const char *status, *mode, *region_et;
4244 
4245 	if (psr->enabled)
4246 		status = " enabled";
4247 	else
4248 		status = "disabled";
4249 
4250 	if (psr->panel_replay_enabled && psr->sel_update_enabled)
4251 		mode = "Panel Replay Selective Update";
4252 	else if (psr->panel_replay_enabled)
4253 		mode = "Panel Replay";
4254 	else if (psr->sel_update_enabled)
4255 		mode = "PSR2";
4256 	else if (psr->enabled)
4257 		mode = "PSR1";
4258 	else
4259 		mode = "";
4260 
4261 	if (psr->su_region_et_enabled)
4262 		region_et = " (Early Transport)";
4263 	else
4264 		region_et = "";
4265 
4266 	seq_printf(m, "PSR mode: %s%s%s\n", mode, status, region_et);
4267 	if (psr->no_psr_reason)
4268 		seq_printf(m, "  %s\n", psr->no_psr_reason);
4269 }
4270 
4271 static int intel_psr_status(struct seq_file *m, struct intel_dp *intel_dp,
4272 			    struct intel_connector *connector)
4273 {
4274 	struct intel_display *display = to_intel_display(intel_dp);
4275 	enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
4276 	struct intel_psr *psr = &intel_dp->psr;
4277 	struct ref_tracker *wakeref;
4278 	bool enabled;
4279 	u32 val, psr2_ctl;
4280 
4281 	intel_psr_sink_capability(connector, m);
4282 
4283 	if (!(connector->dp.psr_caps.support || connector->dp.panel_replay_caps.support))
4284 		return 0;
4285 
4286 	wakeref = intel_display_rpm_get(display);
4287 	mutex_lock(&psr->lock);
4288 
4289 	intel_psr_print_mode(intel_dp, m);
4290 
4291 	if (!psr->enabled) {
4292 		seq_printf(m, "PSR sink not reliable: %s\n",
4293 			   str_yes_no(psr->sink_not_reliable));
4294 
4295 		goto unlock;
4296 	}
4297 
4298 	if (psr->panel_replay_enabled) {
4299 		val = intel_de_read(display, TRANS_DP2_CTL(cpu_transcoder));
4300 
4301 		if (intel_dp_is_edp(intel_dp))
4302 			psr2_ctl = intel_de_read(display,
4303 						 EDP_PSR2_CTL(display,
4304 							      cpu_transcoder));
4305 
4306 		enabled = val & TRANS_DP2_PANEL_REPLAY_ENABLE;
4307 	} else if (psr->sel_update_enabled) {
4308 		val = intel_de_read(display,
4309 				    EDP_PSR2_CTL(display, cpu_transcoder));
4310 		enabled = val & EDP_PSR2_ENABLE;
4311 	} else {
4312 		val = intel_de_read(display, psr_ctl_reg(display, cpu_transcoder));
4313 		enabled = val & EDP_PSR_ENABLE;
4314 	}
4315 	seq_printf(m, "Source PSR/PanelReplay ctl: %s [0x%08x]\n",
4316 		   str_enabled_disabled(enabled), val);
4317 	if (psr->panel_replay_enabled && intel_dp_is_edp(intel_dp))
4318 		seq_printf(m, "PSR2_CTL: 0x%08x\n",
4319 			   psr2_ctl);
4320 	psr_source_status(intel_dp, m);
4321 	seq_printf(m, "Busy frontbuffer bits: 0x%08x\n",
4322 		   psr->busy_frontbuffer_bits);
4323 
4324 	/*
4325 	 * SKL+ Perf counter is reset to 0 everytime DC state is entered
4326 	 */
4327 	val = intel_de_read(display, psr_perf_cnt_reg(display, cpu_transcoder));
4328 	seq_printf(m, "Performance counter: %u\n",
4329 		   REG_FIELD_GET(EDP_PSR_PERF_CNT_MASK, val));
4330 
4331 	if (psr->debug & I915_PSR_DEBUG_IRQ) {
4332 		seq_printf(m, "Last attempted entry at: %lld\n",
4333 			   psr->last_entry_attempt);
4334 		seq_printf(m, "Last exit at: %lld\n", psr->last_exit);
4335 	}
4336 
4337 	if (psr->sel_update_enabled) {
4338 		u32 su_frames_val[3];
4339 		int frame;
4340 
4341 		/*
4342 		 * PSR2_SU_STATUS register has been tied-off since DG2/ADL-P
4343 		 * (it returns zeros only) and it has been removed on Xe2_LPD.
4344 		 */
4345 		if (DISPLAY_VER(display) < 13) {
4346 			/*
4347 			 * Reading all 3 registers before hand to minimize crossing a
4348 			 * frame boundary between register reads
4349 			 */
4350 			for (frame = 0; frame < PSR2_SU_STATUS_FRAMES; frame += 3) {
4351 				val = intel_de_read(display,
4352 						    PSR2_SU_STATUS(display, cpu_transcoder, frame));
4353 				su_frames_val[frame / 3] = val;
4354 			}
4355 
4356 			seq_puts(m, "Frame:\tPSR2 SU blocks:\n");
4357 
4358 			for (frame = 0; frame < PSR2_SU_STATUS_FRAMES; frame++) {
4359 				u32 su_blocks;
4360 
4361 				su_blocks = su_frames_val[frame / 3] &
4362 					PSR2_SU_STATUS_MASK(frame);
4363 				su_blocks = su_blocks >> PSR2_SU_STATUS_SHIFT(frame);
4364 				seq_printf(m, "%d\t%d\n", frame, su_blocks);
4365 			}
4366 		}
4367 
4368 		seq_printf(m, "PSR2 selective fetch: %s\n",
4369 			   str_enabled_disabled(psr->psr2_sel_fetch_enabled));
4370 	}
4371 
4372 unlock:
4373 	mutex_unlock(&psr->lock);
4374 	intel_display_rpm_put(display, wakeref);
4375 
4376 	return 0;
4377 }
4378 
4379 static int i915_edp_psr_status_show(struct seq_file *m, void *data)
4380 {
4381 	struct intel_display *display = m->private;
4382 	struct intel_dp *intel_dp = NULL;
4383 	struct intel_encoder *encoder;
4384 
4385 	if (!HAS_PSR(display))
4386 		return -ENODEV;
4387 
4388 	/* Find the first EDP which supports PSR */
4389 	for_each_intel_encoder_with_psr(display->drm, encoder) {
4390 		intel_dp = enc_to_intel_dp(encoder);
4391 		break;
4392 	}
4393 
4394 	if (!intel_dp)
4395 		return -ENODEV;
4396 
4397 	return intel_psr_status(m, intel_dp, intel_dp->attached_connector);
4398 }
4399 DEFINE_SHOW_ATTRIBUTE(i915_edp_psr_status);
4400 
4401 static int
4402 i915_edp_psr_debug_set(void *data, u64 val)
4403 {
4404 	struct intel_display *display = data;
4405 	struct intel_encoder *encoder;
4406 	int ret = -ENODEV;
4407 
4408 	if (!HAS_PSR(display))
4409 		return ret;
4410 
4411 	for_each_intel_encoder_with_psr(display->drm, encoder) {
4412 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4413 
4414 		drm_dbg_kms(display->drm, "Setting PSR debug to %llx\n", val);
4415 
4416 		// TODO: split to each transcoder's PSR debug state
4417 		with_intel_display_rpm(display)
4418 			ret = intel_psr_debug_set(intel_dp, val);
4419 	}
4420 
4421 	return ret;
4422 }
4423 
4424 static int
4425 i915_edp_psr_debug_get(void *data, u64 *val)
4426 {
4427 	struct intel_display *display = data;
4428 	struct intel_encoder *encoder;
4429 
4430 	if (!HAS_PSR(display))
4431 		return -ENODEV;
4432 
4433 	for_each_intel_encoder_with_psr(display->drm, encoder) {
4434 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
4435 
4436 		// TODO: split to each transcoder's PSR debug state
4437 		*val = READ_ONCE(intel_dp->psr.debug);
4438 		return 0;
4439 	}
4440 
4441 	return -ENODEV;
4442 }
4443 
4444 DEFINE_SIMPLE_ATTRIBUTE(i915_edp_psr_debug_fops,
4445 			i915_edp_psr_debug_get, i915_edp_psr_debug_set,
4446 			"%llu\n");
4447 
4448 void intel_psr_debugfs_register(struct intel_display *display)
4449 {
4450 	struct dentry *debugfs_root = display->drm->debugfs_root;
4451 
4452 	debugfs_create_file("i915_edp_psr_debug", 0644, debugfs_root,
4453 			    display, &i915_edp_psr_debug_fops);
4454 
4455 	debugfs_create_file("i915_edp_psr_status", 0444, debugfs_root,
4456 			    display, &i915_edp_psr_status_fops);
4457 }
4458 
4459 static const char *psr_mode_str(struct intel_dp *intel_dp)
4460 {
4461 	if (intel_dp->psr.panel_replay_enabled)
4462 		return "PANEL-REPLAY";
4463 	else if (intel_dp->psr.enabled)
4464 		return "PSR";
4465 
4466 	return "unknown";
4467 }
4468 
4469 static int i915_psr_sink_status_show(struct seq_file *m, void *data)
4470 {
4471 	struct intel_connector *connector = m->private;
4472 	struct intel_dp *intel_dp = intel_attached_dp(connector);
4473 	static const char * const sink_status[] = {
4474 		"inactive",
4475 		"transition to active, capture and display",
4476 		"active, display from RFB",
4477 		"active, capture and display on sink device timings",
4478 		"transition to inactive, capture and display, timing re-sync",
4479 		"reserved",
4480 		"reserved",
4481 		"sink internal error",
4482 	};
4483 	const char *str;
4484 	int ret;
4485 	u8 status, error_status;
4486 
4487 	if (!(CAN_PSR(intel_dp) || CAN_PANEL_REPLAY(intel_dp))) {
4488 		seq_puts(m, "PSR/Panel-Replay Unsupported\n");
4489 		return -ENODEV;
4490 	}
4491 
4492 	if (connector->base.status != connector_status_connected)
4493 		return -ENODEV;
4494 
4495 	ret = psr_get_status_and_error_status(intel_dp, &status, &error_status);
4496 	if (ret)
4497 		return ret;
4498 
4499 	status &= DP_PSR_SINK_STATE_MASK;
4500 	if (status < ARRAY_SIZE(sink_status))
4501 		str = sink_status[status];
4502 	else
4503 		str = "unknown";
4504 
4505 	seq_printf(m, "Sink %s status: 0x%x [%s]\n", psr_mode_str(intel_dp), status, str);
4506 
4507 	seq_printf(m, "Sink %s error status: 0x%x", psr_mode_str(intel_dp), error_status);
4508 
4509 	if (error_status & (DP_PSR_RFB_STORAGE_ERROR |
4510 			    DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR |
4511 			    DP_PSR_LINK_CRC_ERROR))
4512 		seq_puts(m, ":\n");
4513 	else
4514 		seq_puts(m, "\n");
4515 	if (error_status & DP_PSR_RFB_STORAGE_ERROR)
4516 		seq_printf(m, "\t%s RFB storage error\n", psr_mode_str(intel_dp));
4517 	if (error_status & DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR)
4518 		seq_printf(m, "\t%s VSC SDP uncorrectable error\n", psr_mode_str(intel_dp));
4519 	if (error_status & DP_PSR_LINK_CRC_ERROR)
4520 		seq_printf(m, "\t%s Link CRC error\n", psr_mode_str(intel_dp));
4521 
4522 	return ret;
4523 }
4524 DEFINE_SHOW_ATTRIBUTE(i915_psr_sink_status);
4525 
4526 static int i915_psr_status_show(struct seq_file *m, void *data)
4527 {
4528 	struct intel_connector *connector = m->private;
4529 	struct intel_dp *intel_dp = intel_attached_dp(connector);
4530 
4531 	return intel_psr_status(m, intel_dp, connector);
4532 }
4533 DEFINE_SHOW_ATTRIBUTE(i915_psr_status);
4534 
4535 void intel_psr_connector_debugfs_add(struct intel_connector *connector)
4536 {
4537 	struct intel_display *display = to_intel_display(connector);
4538 	struct dentry *root = connector->base.debugfs_entry;
4539 
4540 	if (connector->base.connector_type != DRM_MODE_CONNECTOR_eDP &&
4541 	    connector->base.connector_type != DRM_MODE_CONNECTOR_DisplayPort)
4542 		return;
4543 
4544 	debugfs_create_file("i915_psr_sink_status", 0444, root,
4545 			    connector, &i915_psr_sink_status_fops);
4546 
4547 	if (HAS_PSR(display) || HAS_DP20(display))
4548 		debugfs_create_file("i915_psr_status", 0444, root,
4549 				    connector, &i915_psr_status_fops);
4550 }
4551 
4552 bool intel_psr_needs_alpm(struct intel_dp *intel_dp, const struct intel_crtc_state *crtc_state)
4553 {
4554 	/*
4555 	 * eDP Panel Replay uses always ALPM
4556 	 * PSR2 uses ALPM but PSR1 doesn't
4557 	 */
4558 	return intel_dp_is_edp(intel_dp) && (crtc_state->has_sel_update ||
4559 					     crtc_state->has_panel_replay);
4560 }
4561 
4562 bool intel_psr_needs_alpm_aux_less(struct intel_dp *intel_dp,
4563 				   const struct intel_crtc_state *crtc_state)
4564 {
4565 	return intel_dp_is_edp(intel_dp) && crtc_state->has_panel_replay;
4566 }
4567 
4568 void intel_psr_compute_config_late(struct intel_dp *intel_dp,
4569 				   struct intel_crtc_state *crtc_state)
4570 {
4571 	struct intel_display *display = to_intel_display(intel_dp);
4572 	int vblank = intel_crtc_vblank_length(crtc_state);
4573 	int wake_lines;
4574 
4575 	if (intel_psr_needs_alpm_aux_less(intel_dp, crtc_state))
4576 		wake_lines = crtc_state->alpm_state.aux_less_wake_lines;
4577 	else if (intel_psr_needs_alpm(intel_dp, crtc_state))
4578 		wake_lines = DISPLAY_VER(display) < 20 ?
4579 			     psr2_block_count_lines(crtc_state->alpm_state.io_wake_lines,
4580 						    crtc_state->alpm_state.fast_wake_lines) :
4581 			     crtc_state->alpm_state.io_wake_lines;
4582 	else
4583 		wake_lines = 0;
4584 
4585 	/*
4586 	 * Disable the PSR features if wake lines exceed the available vblank.
4587 	 * Though SCL is computed based on these PSR features, it is not reset
4588 	 * even if the PSR features are disabled to avoid changing vblank start
4589 	 * at this stage.
4590 	 */
4591 	if (wake_lines && !_wake_lines_fit_into_vblank(crtc_state, vblank, wake_lines)) {
4592 		drm_dbg_kms(display->drm,
4593 			    "Adjusting PSR/PR mode: vblank too short for wake lines = %d\n",
4594 			    wake_lines);
4595 
4596 		if (crtc_state->has_panel_replay) {
4597 			crtc_state->has_panel_replay = false;
4598 			/*
4599 			 * #TODO : Add fall back to PSR/PSR2
4600 			 * Since panel replay cannot be supported, we can fall back to PSR/PSR2.
4601 			 * This will require calling compute_config for psr and psr2 with check for
4602 			 * actual guardband instead of vblank_length.
4603 			 */
4604 			crtc_state->has_psr = false;
4605 		}
4606 
4607 		crtc_state->has_sel_update = false;
4608 		crtc_state->enable_psr2_su_region_et = false;
4609 		crtc_state->enable_psr2_sel_fetch = false;
4610 	}
4611 
4612 	/* Wa_18037818876 */
4613 	if (intel_psr_needs_wa_18037818876(intel_dp, crtc_state)) {
4614 		crtc_state->has_psr = false;
4615 		drm_dbg_kms(display->drm,
4616 			    "PSR disabled to workaround PSR FSM hang issue\n");
4617 	}
4618 
4619 	intel_psr_set_non_psr_pipes(intel_dp, crtc_state);
4620 }
4621 
4622 int intel_psr_min_guardband(struct intel_crtc_state *crtc_state)
4623 {
4624 	struct intel_display *display = to_intel_display(crtc_state);
4625 	int psr_min_guardband;
4626 	int wake_lines;
4627 
4628 	if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP))
4629 		return 0;
4630 
4631 	if (crtc_state->has_panel_replay)
4632 		wake_lines = crtc_state->alpm_state.aux_less_wake_lines;
4633 	else if (crtc_state->has_sel_update)
4634 		wake_lines = DISPLAY_VER(display) < 20 ?
4635 			     psr2_block_count_lines(crtc_state->alpm_state.io_wake_lines,
4636 						    crtc_state->alpm_state.fast_wake_lines) :
4637 			     crtc_state->alpm_state.io_wake_lines;
4638 	else
4639 		return 0;
4640 
4641 	psr_min_guardband = wake_lines + crtc_state->set_context_latency;
4642 
4643 	if (crtc_state->req_psr2_sdp_prior_scanline)
4644 		psr_min_guardband++;
4645 
4646 	return psr_min_guardband;
4647 }
4648 
4649 bool intel_psr_use_trans_push(const struct intel_crtc_state *crtc_state)
4650 {
4651 	struct intel_display *display = to_intel_display(crtc_state);
4652 
4653 	return HAS_PSR_TRANS_PUSH_FRAME_CHANGE(display) && crtc_state->has_psr;
4654 }
4655 
4656 bool intel_psr_pr_async_video_timing_supported(struct intel_dp *intel_dp)
4657 {
4658 	struct intel_connector *connector = intel_dp->attached_connector;
4659 	u8 *dpcd = connector->dp.panel_replay_caps.dpcd;
4660 	u8 pr_support = dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_SUPPORT)];
4661 	u8 pr_cap = dpcd[INTEL_PR_DPCD_INDEX(DP_PANEL_REPLAY_CAP_CAPABILITY)];
4662 
4663 	return (pr_support & DP_PANEL_REPLAY_SUPPORT) &&
4664 		!(pr_cap & DP_PANEL_REPLAY_ASYNC_VIDEO_TIMING_NOT_SUPPORTED_IN_PR);
4665 }
4666