1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <drm/drm_atomic_helper.h>
25#include <drm/drm_damage_helper.h>
26
27#include "display/intel_dp.h"
28
29#include "i915_drv.h"
30#include "intel_atomic.h"
31#include "intel_crtc.h"
32#include "intel_de.h"
33#include "intel_display_types.h"
34#include "intel_dp_aux.h"
35#include "intel_hdmi.h"
36#include "intel_psr.h"
37#include "intel_snps_phy.h"
38#include "skl_universal_plane.h"
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87static bool psr_global_enabled(struct intel_dp *intel_dp)
88{
89 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
90
91 switch (intel_dp->psr.debug & I915_PSR_DEBUG_MODE_MASK) {
92 case I915_PSR_DEBUG_DEFAULT:
93 return i915->params.enable_psr;
94 case I915_PSR_DEBUG_DISABLE:
95 return false;
96 default:
97 return true;
98 }
99}
100
101static bool psr2_global_enabled(struct intel_dp *intel_dp)
102{
103 switch (intel_dp->psr.debug & I915_PSR_DEBUG_MODE_MASK) {
104 case I915_PSR_DEBUG_DISABLE:
105 case I915_PSR_DEBUG_FORCE_PSR1:
106 return false;
107 default:
108 return true;
109 }
110}
111
112static void psr_irq_control(struct intel_dp *intel_dp)
113{
114 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
115 enum transcoder trans_shift;
116 i915_reg_t imr_reg;
117 u32 mask, val;
118
119
120
121
122
123
124 if (DISPLAY_VER(dev_priv) >= 12) {
125 trans_shift = 0;
126 imr_reg = TRANS_PSR_IMR(intel_dp->psr.transcoder);
127 } else {
128 trans_shift = intel_dp->psr.transcoder;
129 imr_reg = EDP_PSR_IMR;
130 }
131
132 mask = EDP_PSR_ERROR(trans_shift);
133 if (intel_dp->psr.debug & I915_PSR_DEBUG_IRQ)
134 mask |= EDP_PSR_POST_EXIT(trans_shift) |
135 EDP_PSR_PRE_ENTRY(trans_shift);
136
137
138 val = intel_de_read(dev_priv, imr_reg);
139 val &= ~EDP_PSR_TRANS_MASK(trans_shift);
140 val |= ~mask;
141 intel_de_write(dev_priv, imr_reg, val);
142}
143
144static void psr_event_print(struct drm_i915_private *i915,
145 u32 val, bool psr2_enabled)
146{
147 drm_dbg_kms(&i915->drm, "PSR exit events: 0x%x\n", val);
148 if (val & PSR_EVENT_PSR2_WD_TIMER_EXPIRE)
149 drm_dbg_kms(&i915->drm, "\tPSR2 watchdog timer expired\n");
150 if ((val & PSR_EVENT_PSR2_DISABLED) && psr2_enabled)
151 drm_dbg_kms(&i915->drm, "\tPSR2 disabled\n");
152 if (val & PSR_EVENT_SU_DIRTY_FIFO_UNDERRUN)
153 drm_dbg_kms(&i915->drm, "\tSU dirty FIFO underrun\n");
154 if (val & PSR_EVENT_SU_CRC_FIFO_UNDERRUN)
155 drm_dbg_kms(&i915->drm, "\tSU CRC FIFO underrun\n");
156 if (val & PSR_EVENT_GRAPHICS_RESET)
157 drm_dbg_kms(&i915->drm, "\tGraphics reset\n");
158 if (val & PSR_EVENT_PCH_INTERRUPT)
159 drm_dbg_kms(&i915->drm, "\tPCH interrupt\n");
160 if (val & PSR_EVENT_MEMORY_UP)
161 drm_dbg_kms(&i915->drm, "\tMemory up\n");
162 if (val & PSR_EVENT_FRONT_BUFFER_MODIFY)
163 drm_dbg_kms(&i915->drm, "\tFront buffer modification\n");
164 if (val & PSR_EVENT_WD_TIMER_EXPIRE)
165 drm_dbg_kms(&i915->drm, "\tPSR watchdog timer expired\n");
166 if (val & PSR_EVENT_PIPE_REGISTERS_UPDATE)
167 drm_dbg_kms(&i915->drm, "\tPIPE registers updated\n");
168 if (val & PSR_EVENT_REGISTER_UPDATE)
169 drm_dbg_kms(&i915->drm, "\tRegister updated\n");
170 if (val & PSR_EVENT_HDCP_ENABLE)
171 drm_dbg_kms(&i915->drm, "\tHDCP enabled\n");
172 if (val & PSR_EVENT_KVMR_SESSION_ENABLE)
173 drm_dbg_kms(&i915->drm, "\tKVMR session enabled\n");
174 if (val & PSR_EVENT_VBI_ENABLE)
175 drm_dbg_kms(&i915->drm, "\tVBI enabled\n");
176 if (val & PSR_EVENT_LPSP_MODE_EXIT)
177 drm_dbg_kms(&i915->drm, "\tLPSP mode exited\n");
178 if ((val & PSR_EVENT_PSR_DISABLE) && !psr2_enabled)
179 drm_dbg_kms(&i915->drm, "\tPSR disabled\n");
180}
181
182void intel_psr_irq_handler(struct intel_dp *intel_dp, u32 psr_iir)
183{
184 enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
185 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
186 ktime_t time_ns = ktime_get();
187 enum transcoder trans_shift;
188 i915_reg_t imr_reg;
189
190 if (DISPLAY_VER(dev_priv) >= 12) {
191 trans_shift = 0;
192 imr_reg = TRANS_PSR_IMR(intel_dp->psr.transcoder);
193 } else {
194 trans_shift = intel_dp->psr.transcoder;
195 imr_reg = EDP_PSR_IMR;
196 }
197
198 if (psr_iir & EDP_PSR_PRE_ENTRY(trans_shift)) {
199 intel_dp->psr.last_entry_attempt = time_ns;
200 drm_dbg_kms(&dev_priv->drm,
201 "[transcoder %s] PSR entry attempt in 2 vblanks\n",
202 transcoder_name(cpu_transcoder));
203 }
204
205 if (psr_iir & EDP_PSR_POST_EXIT(trans_shift)) {
206 intel_dp->psr.last_exit = time_ns;
207 drm_dbg_kms(&dev_priv->drm,
208 "[transcoder %s] PSR exit completed\n",
209 transcoder_name(cpu_transcoder));
210
211 if (DISPLAY_VER(dev_priv) >= 9) {
212 u32 val = intel_de_read(dev_priv,
213 PSR_EVENT(cpu_transcoder));
214 bool psr2_enabled = intel_dp->psr.psr2_enabled;
215
216 intel_de_write(dev_priv, PSR_EVENT(cpu_transcoder),
217 val);
218 psr_event_print(dev_priv, val, psr2_enabled);
219 }
220 }
221
222 if (psr_iir & EDP_PSR_ERROR(trans_shift)) {
223 u32 val;
224
225 drm_warn(&dev_priv->drm, "[transcoder %s] PSR aux error\n",
226 transcoder_name(cpu_transcoder));
227
228 intel_dp->psr.irq_aux_error = true;
229
230
231
232
233
234
235
236
237
238 val = intel_de_read(dev_priv, imr_reg);
239 val |= EDP_PSR_ERROR(trans_shift);
240 intel_de_write(dev_priv, imr_reg, val);
241
242 schedule_work(&intel_dp->psr.work);
243 }
244}
245
246static bool intel_dp_get_alpm_status(struct intel_dp *intel_dp)
247{
248 u8 alpm_caps = 0;
249
250 if (drm_dp_dpcd_readb(&intel_dp->aux, DP_RECEIVER_ALPM_CAP,
251 &alpm_caps) != 1)
252 return false;
253 return alpm_caps & DP_ALPM_CAP;
254}
255
256static u8 intel_dp_get_sink_sync_latency(struct intel_dp *intel_dp)
257{
258 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
259 u8 val = 8;
260
261 if (drm_dp_dpcd_readb(&intel_dp->aux,
262 DP_SYNCHRONIZATION_LATENCY_IN_SINK, &val) == 1)
263 val &= DP_MAX_RESYNC_FRAME_COUNT_MASK;
264 else
265 drm_dbg_kms(&i915->drm,
266 "Unable to get sink synchronization latency, assuming 8 frames\n");
267 return val;
268}
269
270static void intel_dp_get_su_granularity(struct intel_dp *intel_dp)
271{
272 struct drm_i915_private *i915 = dp_to_i915(intel_dp);
273 ssize_t r;
274 u16 w;
275 u8 y;
276
277
278 if (!(intel_dp->psr_dpcd[1] & DP_PSR2_SU_GRANULARITY_REQUIRED)) {
279
280 w = 4;
281 y = 4;
282 goto exit;
283 }
284
285 r = drm_dp_dpcd_read(&intel_dp->aux, DP_PSR2_SU_X_GRANULARITY, &w, 2);
286 if (r != 2)
287 drm_dbg_kms(&i915->drm,
288 "Unable to read DP_PSR2_SU_X_GRANULARITY\n");
289
290
291
292
293 if (r != 2 || w == 0)
294 w = 4;
295
296 r = drm_dp_dpcd_read(&intel_dp->aux, DP_PSR2_SU_Y_GRANULARITY, &y, 1);
297 if (r != 1) {
298 drm_dbg_kms(&i915->drm,
299 "Unable to read DP_PSR2_SU_Y_GRANULARITY\n");
300 y = 4;
301 }
302 if (y == 0)
303 y = 1;
304
305exit:
306 intel_dp->psr.su_w_granularity = w;
307 intel_dp->psr.su_y_granularity = y;
308}
309
310void intel_psr_init_dpcd(struct intel_dp *intel_dp)
311{
312 struct drm_i915_private *dev_priv =
313 to_i915(dp_to_dig_port(intel_dp)->base.base.dev);
314
315 drm_dp_dpcd_read(&intel_dp->aux, DP_PSR_SUPPORT, intel_dp->psr_dpcd,
316 sizeof(intel_dp->psr_dpcd));
317
318 if (!intel_dp->psr_dpcd[0])
319 return;
320 drm_dbg_kms(&dev_priv->drm, "eDP panel supports PSR version %x\n",
321 intel_dp->psr_dpcd[0]);
322
323 if (drm_dp_has_quirk(&intel_dp->desc, DP_DPCD_QUIRK_NO_PSR)) {
324 drm_dbg_kms(&dev_priv->drm,
325 "PSR support not currently available for this panel\n");
326 return;
327 }
328
329 if (!(intel_dp->edp_dpcd[1] & DP_EDP_SET_POWER_CAP)) {
330 drm_dbg_kms(&dev_priv->drm,
331 "Panel lacks power state control, PSR cannot be enabled\n");
332 return;
333 }
334
335 intel_dp->psr.sink_support = true;
336 intel_dp->psr.sink_sync_latency =
337 intel_dp_get_sink_sync_latency(intel_dp);
338
339 if (DISPLAY_VER(dev_priv) >= 9 &&
340 (intel_dp->psr_dpcd[0] == DP_PSR2_WITH_Y_COORD_IS_SUPPORTED)) {
341 bool y_req = intel_dp->psr_dpcd[1] &
342 DP_PSR2_SU_Y_COORDINATE_REQUIRED;
343 bool alpm = intel_dp_get_alpm_status(intel_dp);
344
345
346
347
348
349
350
351
352
353
354
355
356 intel_dp->psr.sink_psr2_support = y_req && alpm;
357 drm_dbg_kms(&dev_priv->drm, "PSR2 %ssupported\n",
358 intel_dp->psr.sink_psr2_support ? "" : "not ");
359
360 if (intel_dp->psr.sink_psr2_support) {
361 intel_dp->psr.colorimetry_support =
362 intel_dp_get_colorimetry_status(intel_dp);
363 intel_dp_get_su_granularity(intel_dp);
364 }
365 }
366}
367
368static void intel_psr_enable_sink(struct intel_dp *intel_dp)
369{
370 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
371 u8 dpcd_val = DP_PSR_ENABLE;
372
373
374 if (intel_dp->psr.psr2_enabled) {
375 drm_dp_dpcd_writeb(&intel_dp->aux, DP_RECEIVER_ALPM_CONFIG,
376 DP_ALPM_ENABLE |
377 DP_ALPM_LOCK_ERROR_IRQ_HPD_ENABLE);
378
379 dpcd_val |= DP_PSR_ENABLE_PSR2 | DP_PSR_IRQ_HPD_WITH_CRC_ERRORS;
380 } else {
381 if (intel_dp->psr.link_standby)
382 dpcd_val |= DP_PSR_MAIN_LINK_ACTIVE;
383
384 if (DISPLAY_VER(dev_priv) >= 8)
385 dpcd_val |= DP_PSR_CRC_VERIFICATION;
386 }
387
388 if (intel_dp->psr.req_psr2_sdp_prior_scanline)
389 dpcd_val |= DP_PSR_SU_REGION_SCANLINE_CAPTURE;
390
391 drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG, dpcd_val);
392
393 drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, DP_SET_POWER_D0);
394}
395
396static u32 intel_psr1_get_tp_time(struct intel_dp *intel_dp)
397{
398 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
399 u32 val = 0;
400
401 if (DISPLAY_VER(dev_priv) >= 11)
402 val |= EDP_PSR_TP4_TIME_0US;
403
404 if (dev_priv->params.psr_safest_params) {
405 val |= EDP_PSR_TP1_TIME_2500us;
406 val |= EDP_PSR_TP2_TP3_TIME_2500us;
407 goto check_tp3_sel;
408 }
409
410 if (dev_priv->vbt.psr.tp1_wakeup_time_us == 0)
411 val |= EDP_PSR_TP1_TIME_0us;
412 else if (dev_priv->vbt.psr.tp1_wakeup_time_us <= 100)
413 val |= EDP_PSR_TP1_TIME_100us;
414 else if (dev_priv->vbt.psr.tp1_wakeup_time_us <= 500)
415 val |= EDP_PSR_TP1_TIME_500us;
416 else
417 val |= EDP_PSR_TP1_TIME_2500us;
418
419 if (dev_priv->vbt.psr.tp2_tp3_wakeup_time_us == 0)
420 val |= EDP_PSR_TP2_TP3_TIME_0us;
421 else if (dev_priv->vbt.psr.tp2_tp3_wakeup_time_us <= 100)
422 val |= EDP_PSR_TP2_TP3_TIME_100us;
423 else if (dev_priv->vbt.psr.tp2_tp3_wakeup_time_us <= 500)
424 val |= EDP_PSR_TP2_TP3_TIME_500us;
425 else
426 val |= EDP_PSR_TP2_TP3_TIME_2500us;
427
428check_tp3_sel:
429 if (intel_dp_source_supports_tps3(dev_priv) &&
430 drm_dp_tps3_supported(intel_dp->dpcd))
431 val |= EDP_PSR_TP1_TP3_SEL;
432 else
433 val |= EDP_PSR_TP1_TP2_SEL;
434
435 return val;
436}
437
438static u8 psr_compute_idle_frames(struct intel_dp *intel_dp)
439{
440 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
441 int idle_frames;
442
443
444
445
446 idle_frames = max(6, dev_priv->vbt.psr.idle_frames);
447 idle_frames = max(idle_frames, intel_dp->psr.sink_sync_latency + 1);
448
449 if (drm_WARN_ON(&dev_priv->drm, idle_frames > 0xf))
450 idle_frames = 0xf;
451
452 return idle_frames;
453}
454
455static void hsw_activate_psr1(struct intel_dp *intel_dp)
456{
457 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
458 u32 max_sleep_time = 0x1f;
459 u32 val = EDP_PSR_ENABLE;
460
461 val |= psr_compute_idle_frames(intel_dp) << EDP_PSR_IDLE_FRAME_SHIFT;
462
463 val |= max_sleep_time << EDP_PSR_MAX_SLEEP_TIME_SHIFT;
464 if (IS_HASWELL(dev_priv))
465 val |= EDP_PSR_MIN_LINK_ENTRY_TIME_8_LINES;
466
467 if (intel_dp->psr.link_standby)
468 val |= EDP_PSR_LINK_STANDBY;
469
470 val |= intel_psr1_get_tp_time(intel_dp);
471
472 if (DISPLAY_VER(dev_priv) >= 8)
473 val |= EDP_PSR_CRC_ENABLE;
474
475 val |= (intel_de_read(dev_priv, EDP_PSR_CTL(intel_dp->psr.transcoder)) &
476 EDP_PSR_RESTORE_PSR_ACTIVE_CTX_MASK);
477 intel_de_write(dev_priv, EDP_PSR_CTL(intel_dp->psr.transcoder), val);
478}
479
480static u32 intel_psr2_get_tp_time(struct intel_dp *intel_dp)
481{
482 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
483 u32 val = 0;
484
485 if (dev_priv->params.psr_safest_params)
486 return EDP_PSR2_TP2_TIME_2500us;
487
488 if (dev_priv->vbt.psr.psr2_tp2_tp3_wakeup_time_us >= 0 &&
489 dev_priv->vbt.psr.psr2_tp2_tp3_wakeup_time_us <= 50)
490 val |= EDP_PSR2_TP2_TIME_50us;
491 else if (dev_priv->vbt.psr.psr2_tp2_tp3_wakeup_time_us <= 100)
492 val |= EDP_PSR2_TP2_TIME_100us;
493 else if (dev_priv->vbt.psr.psr2_tp2_tp3_wakeup_time_us <= 500)
494 val |= EDP_PSR2_TP2_TIME_500us;
495 else
496 val |= EDP_PSR2_TP2_TIME_2500us;
497
498 return val;
499}
500
501static void hsw_activate_psr2(struct intel_dp *intel_dp)
502{
503 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
504 u32 val = EDP_PSR2_ENABLE;
505
506 val |= psr_compute_idle_frames(intel_dp) << EDP_PSR2_IDLE_FRAME_SHIFT;
507
508 if (!IS_ALDERLAKE_P(dev_priv))
509 val |= EDP_SU_TRACK_ENABLE;
510
511 if (DISPLAY_VER(dev_priv) >= 10 && DISPLAY_VER(dev_priv) <= 12)
512 val |= EDP_Y_COORDINATE_ENABLE;
513
514 val |= EDP_PSR2_FRAME_BEFORE_SU(max_t(u8, intel_dp->psr.sink_sync_latency + 1, 2));
515 val |= intel_psr2_get_tp_time(intel_dp);
516
517
518 if (IS_ADLP_DISPLAY_STEP(dev_priv, STEP_A0, STEP_E0)) {
519 static const u8 map[] = {
520 2,
521 1,
522 0,
523 3,
524 6,
525 5,
526 4,
527 7,
528 };
529
530
531
532
533 u32 tmp, lines = 7;
534
535 val |= TGL_EDP_PSR2_BLOCK_COUNT_NUM_2;
536
537 tmp = map[lines - TGL_EDP_PSR2_IO_BUFFER_WAKE_MIN_LINES];
538 tmp = tmp << TGL_EDP_PSR2_IO_BUFFER_WAKE_SHIFT;
539 val |= tmp;
540
541 tmp = map[lines - TGL_EDP_PSR2_FAST_WAKE_MIN_LINES];
542 tmp = tmp << TGL_EDP_PSR2_FAST_WAKE_MIN_SHIFT;
543 val |= tmp;
544 } else if (DISPLAY_VER(dev_priv) >= 12) {
545
546
547
548
549
550
551
552 val |= TGL_EDP_PSR2_BLOCK_COUNT_NUM_2;
553 val |= TGL_EDP_PSR2_IO_BUFFER_WAKE(7);
554 val |= TGL_EDP_PSR2_FAST_WAKE(7);
555 } else if (DISPLAY_VER(dev_priv) >= 9) {
556 val |= EDP_PSR2_IO_BUFFER_WAKE(7);
557 val |= EDP_PSR2_FAST_WAKE(7);
558 }
559
560 if (intel_dp->psr.req_psr2_sdp_prior_scanline)
561 val |= EDP_PSR2_SU_SDP_SCANLINE;
562
563 if (intel_dp->psr.psr2_sel_fetch_enabled) {
564 u32 tmp;
565
566
567 if (IS_TGL_DISPLAY_STEP(dev_priv, STEP_A0, STEP_B0))
568 intel_de_rmw(dev_priv, CHICKEN_PAR1_1,
569 DIS_RAM_BYPASS_PSR2_MAN_TRACK,
570 DIS_RAM_BYPASS_PSR2_MAN_TRACK);
571
572 tmp = intel_de_read(dev_priv, PSR2_MAN_TRK_CTL(intel_dp->psr.transcoder));
573 drm_WARN_ON(&dev_priv->drm, !(tmp & PSR2_MAN_TRK_CTL_ENABLE));
574 } else if (HAS_PSR2_SEL_FETCH(dev_priv)) {
575 intel_de_write(dev_priv,
576 PSR2_MAN_TRK_CTL(intel_dp->psr.transcoder), 0);
577 }
578
579
580
581
582
583 intel_de_write(dev_priv, EDP_PSR_CTL(intel_dp->psr.transcoder), 0);
584
585 intel_de_write(dev_priv, EDP_PSR2_CTL(intel_dp->psr.transcoder), val);
586}
587
588static bool
589transcoder_has_psr2(struct drm_i915_private *dev_priv, enum transcoder trans)
590{
591 if (IS_ALDERLAKE_P(dev_priv))
592 return trans == TRANSCODER_A || trans == TRANSCODER_B;
593 else if (DISPLAY_VER(dev_priv) >= 12)
594 return trans == TRANSCODER_A;
595 else
596 return trans == TRANSCODER_EDP;
597}
598
599static u32 intel_get_frame_time_us(const struct intel_crtc_state *cstate)
600{
601 if (!cstate || !cstate->hw.active)
602 return 0;
603
604 return DIV_ROUND_UP(1000 * 1000,
605 drm_mode_vrefresh(&cstate->hw.adjusted_mode));
606}
607
608static void psr2_program_idle_frames(struct intel_dp *intel_dp,
609 u32 idle_frames)
610{
611 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
612 u32 val;
613
614 idle_frames <<= EDP_PSR2_IDLE_FRAME_SHIFT;
615 val = intel_de_read(dev_priv, EDP_PSR2_CTL(intel_dp->psr.transcoder));
616 val &= ~EDP_PSR2_IDLE_FRAME_MASK;
617 val |= idle_frames;
618 intel_de_write(dev_priv, EDP_PSR2_CTL(intel_dp->psr.transcoder), val);
619}
620
621static void tgl_psr2_enable_dc3co(struct intel_dp *intel_dp)
622{
623 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
624
625 psr2_program_idle_frames(intel_dp, 0);
626 intel_display_power_set_target_dc_state(dev_priv, DC_STATE_EN_DC3CO);
627}
628
629static void tgl_psr2_disable_dc3co(struct intel_dp *intel_dp)
630{
631 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
632
633 intel_display_power_set_target_dc_state(dev_priv, DC_STATE_EN_UPTO_DC6);
634 psr2_program_idle_frames(intel_dp, psr_compute_idle_frames(intel_dp));
635}
636
637static void tgl_dc3co_disable_work(struct work_struct *work)
638{
639 struct intel_dp *intel_dp =
640 container_of(work, typeof(*intel_dp), psr.dc3co_work.work);
641
642 mutex_lock(&intel_dp->psr.lock);
643
644 if (delayed_work_pending(&intel_dp->psr.dc3co_work))
645 goto unlock;
646
647 tgl_psr2_disable_dc3co(intel_dp);
648unlock:
649 mutex_unlock(&intel_dp->psr.lock);
650}
651
652static void tgl_disallow_dc3co_on_psr2_exit(struct intel_dp *intel_dp)
653{
654 if (!intel_dp->psr.dc3co_exitline)
655 return;
656
657 cancel_delayed_work(&intel_dp->psr.dc3co_work);
658
659 tgl_psr2_disable_dc3co(intel_dp);
660}
661
662static bool
663dc3co_is_pipe_port_compatible(struct intel_dp *intel_dp,
664 struct intel_crtc_state *crtc_state)
665{
666 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
667 enum pipe pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
668 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
669 enum port port = dig_port->base.port;
670
671 if (IS_ALDERLAKE_P(dev_priv))
672 return pipe <= PIPE_B && port <= PORT_B;
673 else
674 return pipe == PIPE_A && port == PORT_A;
675}
676
677static void
678tgl_dc3co_exitline_compute_config(struct intel_dp *intel_dp,
679 struct intel_crtc_state *crtc_state)
680{
681 const u32 crtc_vdisplay = crtc_state->uapi.adjusted_mode.crtc_vdisplay;
682 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
683 u32 exit_scanlines;
684
685
686
687
688
689
690 return;
691
692
693
694
695
696 if (crtc_state->enable_psr2_sel_fetch)
697 return;
698
699 if (!(dev_priv->dmc.allowed_dc_mask & DC_STATE_EN_DC3CO))
700 return;
701
702 if (!dc3co_is_pipe_port_compatible(intel_dp, crtc_state))
703 return;
704
705
706 if (IS_ADLP_DISPLAY_STEP(dev_priv, STEP_A0, STEP_B0))
707 return;
708
709
710
711
712
713 exit_scanlines =
714 intel_usecs_to_scanlines(&crtc_state->uapi.adjusted_mode, 200) + 1;
715
716 if (drm_WARN_ON(&dev_priv->drm, exit_scanlines > crtc_vdisplay))
717 return;
718
719 crtc_state->dc3co_exitline = crtc_vdisplay - exit_scanlines;
720}
721
722static bool intel_psr2_sel_fetch_config_valid(struct intel_dp *intel_dp,
723 struct intel_crtc_state *crtc_state)
724{
725 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
726
727 if (!dev_priv->params.enable_psr2_sel_fetch &&
728 intel_dp->psr.debug != I915_PSR_DEBUG_ENABLE_SEL_FETCH) {
729 drm_dbg_kms(&dev_priv->drm,
730 "PSR2 sel fetch not enabled, disabled by parameter\n");
731 return false;
732 }
733
734 if (crtc_state->uapi.async_flip) {
735 drm_dbg_kms(&dev_priv->drm,
736 "PSR2 sel fetch not enabled, async flip enabled\n");
737 return false;
738 }
739
740
741 if (IS_TGL_DISPLAY_STEP(dev_priv, STEP_A0, STEP_C0)) {
742 drm_dbg_kms(&dev_priv->drm,
743 "PSR2 sel fetch not enabled, missing the implementation of WAs\n");
744 return false;
745 }
746
747 return crtc_state->enable_psr2_sel_fetch = true;
748}
749
750static bool psr2_granularity_check(struct intel_dp *intel_dp,
751 struct intel_crtc_state *crtc_state)
752{
753 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
754 const int crtc_hdisplay = crtc_state->hw.adjusted_mode.crtc_hdisplay;
755 const int crtc_vdisplay = crtc_state->hw.adjusted_mode.crtc_vdisplay;
756 u16 y_granularity = 0;
757
758
759 if (crtc_hdisplay % intel_dp->psr.su_w_granularity)
760 return false;
761
762 if (crtc_vdisplay % intel_dp->psr.su_y_granularity)
763 return false;
764
765
766 if (!crtc_state->enable_psr2_sel_fetch)
767 return intel_dp->psr.su_y_granularity == 4;
768
769
770
771
772
773
774 if (IS_ALDERLAKE_P(dev_priv))
775 y_granularity = intel_dp->psr.su_y_granularity;
776 else if (intel_dp->psr.su_y_granularity <= 2)
777 y_granularity = 4;
778 else if ((intel_dp->psr.su_y_granularity % 4) == 0)
779 y_granularity = intel_dp->psr.su_y_granularity;
780
781 if (y_granularity == 0 || crtc_vdisplay % y_granularity)
782 return false;
783
784 crtc_state->su_y_granularity = y_granularity;
785 return true;
786}
787
788static bool _compute_psr2_sdp_prior_scanline_indication(struct intel_dp *intel_dp,
789 struct intel_crtc_state *crtc_state)
790{
791 const struct drm_display_mode *adjusted_mode = &crtc_state->uapi.adjusted_mode;
792 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
793 u32 hblank_total, hblank_ns, req_ns;
794
795 hblank_total = adjusted_mode->crtc_hblank_end - adjusted_mode->crtc_hblank_start;
796 hblank_ns = div_u64(1000000ULL * hblank_total, adjusted_mode->crtc_clock);
797
798
799 req_ns = (72 / crtc_state->lane_count) * 1000 / (crtc_state->port_clock / 1000);
800
801 if ((hblank_ns - req_ns) > 100)
802 return true;
803
804 if (DISPLAY_VER(dev_priv) < 13 || intel_dp->edp_dpcd[0] < DP_EDP_14b)
805 return false;
806
807 crtc_state->req_psr2_sdp_prior_scanline = true;
808 return true;
809}
810
811static bool intel_psr2_config_valid(struct intel_dp *intel_dp,
812 struct intel_crtc_state *crtc_state)
813{
814 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
815 int crtc_hdisplay = crtc_state->hw.adjusted_mode.crtc_hdisplay;
816 int crtc_vdisplay = crtc_state->hw.adjusted_mode.crtc_vdisplay;
817 int psr_max_h = 0, psr_max_v = 0, max_bpp = 0;
818
819 if (!intel_dp->psr.sink_psr2_support)
820 return false;
821
822
823 if (IS_JSL_EHL(dev_priv)) {
824 drm_dbg_kms(&dev_priv->drm, "PSR2 not supported by phy\n");
825 return false;
826 }
827
828
829 if (IS_ROCKETLAKE(dev_priv) || IS_ALDERLAKE_S(dev_priv) ||
830 IS_DG2(dev_priv)) {
831 drm_dbg_kms(&dev_priv->drm, "PSR2 is defeatured for this platform\n");
832 return false;
833 }
834
835 if (IS_ADLP_DISPLAY_STEP(dev_priv, STEP_A0, STEP_B0)) {
836 drm_dbg_kms(&dev_priv->drm, "PSR2 not completely functional in this stepping\n");
837 return false;
838 }
839
840 if (!transcoder_has_psr2(dev_priv, crtc_state->cpu_transcoder)) {
841 drm_dbg_kms(&dev_priv->drm,
842 "PSR2 not supported in transcoder %s\n",
843 transcoder_name(crtc_state->cpu_transcoder));
844 return false;
845 }
846
847 if (!psr2_global_enabled(intel_dp)) {
848 drm_dbg_kms(&dev_priv->drm, "PSR2 disabled by flag\n");
849 return false;
850 }
851
852
853
854
855
856
857 if (crtc_state->dsc.compression_enable) {
858 drm_dbg_kms(&dev_priv->drm,
859 "PSR2 cannot be enabled since DSC is enabled\n");
860 return false;
861 }
862
863 if (crtc_state->crc_enabled) {
864 drm_dbg_kms(&dev_priv->drm,
865 "PSR2 not enabled because it would inhibit pipe CRC calculation\n");
866 return false;
867 }
868
869 if (DISPLAY_VER(dev_priv) >= 12) {
870 psr_max_h = 5120;
871 psr_max_v = 3200;
872 max_bpp = 30;
873 } else if (DISPLAY_VER(dev_priv) >= 10) {
874 psr_max_h = 4096;
875 psr_max_v = 2304;
876 max_bpp = 24;
877 } else if (DISPLAY_VER(dev_priv) == 9) {
878 psr_max_h = 3640;
879 psr_max_v = 2304;
880 max_bpp = 24;
881 }
882
883 if (crtc_state->pipe_bpp > max_bpp) {
884 drm_dbg_kms(&dev_priv->drm,
885 "PSR2 not enabled, pipe bpp %d > max supported %d\n",
886 crtc_state->pipe_bpp, max_bpp);
887 return false;
888 }
889
890 if (HAS_PSR2_SEL_FETCH(dev_priv)) {
891 if (!intel_psr2_sel_fetch_config_valid(intel_dp, crtc_state) &&
892 !HAS_PSR_HW_TRACKING(dev_priv)) {
893 drm_dbg_kms(&dev_priv->drm,
894 "PSR2 not enabled, selective fetch not valid and no HW tracking available\n");
895 return false;
896 }
897 }
898
899
900 if (!crtc_state->enable_psr2_sel_fetch &&
901 IS_TGL_DISPLAY_STEP(dev_priv, STEP_A0, STEP_C0)) {
902 drm_dbg_kms(&dev_priv->drm, "PSR2 HW tracking is not supported this Display stepping\n");
903 return false;
904 }
905
906 if (!psr2_granularity_check(intel_dp, crtc_state)) {
907 drm_dbg_kms(&dev_priv->drm, "PSR2 not enabled, SU granularity not compatible\n");
908 return false;
909 }
910
911 if (!crtc_state->enable_psr2_sel_fetch &&
912 (crtc_hdisplay > psr_max_h || crtc_vdisplay > psr_max_v)) {
913 drm_dbg_kms(&dev_priv->drm,
914 "PSR2 not enabled, resolution %dx%d > max supported %dx%d\n",
915 crtc_hdisplay, crtc_vdisplay,
916 psr_max_h, psr_max_v);
917 return false;
918 }
919
920 if (!_compute_psr2_sdp_prior_scanline_indication(intel_dp, crtc_state)) {
921 drm_dbg_kms(&dev_priv->drm,
922 "PSR2 not enabled, PSR2 SDP indication do not fit in hblank\n");
923 return false;
924 }
925
926
927 if (crtc_state->vrr.enable &&
928 IS_ADLP_DISPLAY_STEP(dev_priv, STEP_A0, STEP_B0)) {
929 drm_dbg_kms(&dev_priv->drm,
930 "PSR2 not enabled, not compatible with HW stepping + VRR\n");
931 return false;
932 }
933
934 tgl_dc3co_exitline_compute_config(intel_dp, crtc_state);
935 return true;
936}
937
938void intel_psr_compute_config(struct intel_dp *intel_dp,
939 struct intel_crtc_state *crtc_state,
940 struct drm_connector_state *conn_state)
941{
942 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
943 const struct drm_display_mode *adjusted_mode =
944 &crtc_state->hw.adjusted_mode;
945 int psr_setup_time;
946
947
948
949
950
951 if (crtc_state->vrr.enable)
952 return;
953
954 if (!CAN_PSR(intel_dp))
955 return;
956
957 if (!psr_global_enabled(intel_dp)) {
958 drm_dbg_kms(&dev_priv->drm, "PSR disabled by flag\n");
959 return;
960 }
961
962 if (intel_dp->psr.sink_not_reliable) {
963 drm_dbg_kms(&dev_priv->drm,
964 "PSR sink implementation is not reliable\n");
965 return;
966 }
967
968 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
969 drm_dbg_kms(&dev_priv->drm,
970 "PSR condition failed: Interlaced mode enabled\n");
971 return;
972 }
973
974 psr_setup_time = drm_dp_psr_setup_time(intel_dp->psr_dpcd);
975 if (psr_setup_time < 0) {
976 drm_dbg_kms(&dev_priv->drm,
977 "PSR condition failed: Invalid PSR setup time (0x%02x)\n",
978 intel_dp->psr_dpcd[1]);
979 return;
980 }
981
982 if (intel_usecs_to_scanlines(adjusted_mode, psr_setup_time) >
983 adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vdisplay - 1) {
984 drm_dbg_kms(&dev_priv->drm,
985 "PSR condition failed: PSR setup time (%d us) too long\n",
986 psr_setup_time);
987 return;
988 }
989
990 crtc_state->has_psr = true;
991 crtc_state->has_psr2 = intel_psr2_config_valid(intel_dp, crtc_state);
992
993 crtc_state->infoframes.enable |= intel_hdmi_infoframe_enable(DP_SDP_VSC);
994 intel_dp_compute_psr_vsc_sdp(intel_dp, crtc_state, conn_state,
995 &crtc_state->psr_vsc);
996}
997
998void intel_psr_get_config(struct intel_encoder *encoder,
999 struct intel_crtc_state *pipe_config)
1000{
1001 struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1002 struct intel_digital_port *dig_port = enc_to_dig_port(encoder);
1003 struct intel_dp *intel_dp;
1004 u32 val;
1005
1006 if (!dig_port)
1007 return;
1008
1009 intel_dp = &dig_port->dp;
1010 if (!CAN_PSR(intel_dp))
1011 return;
1012
1013 mutex_lock(&intel_dp->psr.lock);
1014 if (!intel_dp->psr.enabled)
1015 goto unlock;
1016
1017
1018
1019
1020
1021 pipe_config->has_psr = true;
1022 pipe_config->has_psr2 = intel_dp->psr.psr2_enabled;
1023 pipe_config->infoframes.enable |= intel_hdmi_infoframe_enable(DP_SDP_VSC);
1024
1025 if (!intel_dp->psr.psr2_enabled)
1026 goto unlock;
1027
1028 if (HAS_PSR2_SEL_FETCH(dev_priv)) {
1029 val = intel_de_read(dev_priv, PSR2_MAN_TRK_CTL(intel_dp->psr.transcoder));
1030 if (val & PSR2_MAN_TRK_CTL_ENABLE)
1031 pipe_config->enable_psr2_sel_fetch = true;
1032 }
1033
1034 if (DISPLAY_VER(dev_priv) >= 12) {
1035 val = intel_de_read(dev_priv, EXITLINE(intel_dp->psr.transcoder));
1036 val &= EXITLINE_MASK;
1037 pipe_config->dc3co_exitline = val;
1038 }
1039unlock:
1040 mutex_unlock(&intel_dp->psr.lock);
1041}
1042
1043static void intel_psr_activate(struct intel_dp *intel_dp)
1044{
1045 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1046 enum transcoder transcoder = intel_dp->psr.transcoder;
1047
1048 if (transcoder_has_psr2(dev_priv, transcoder))
1049 drm_WARN_ON(&dev_priv->drm,
1050 intel_de_read(dev_priv, EDP_PSR2_CTL(transcoder)) & EDP_PSR2_ENABLE);
1051
1052 drm_WARN_ON(&dev_priv->drm,
1053 intel_de_read(dev_priv, EDP_PSR_CTL(transcoder)) & EDP_PSR_ENABLE);
1054 drm_WARN_ON(&dev_priv->drm, intel_dp->psr.active);
1055 lockdep_assert_held(&intel_dp->psr.lock);
1056
1057
1058 if (intel_dp->psr.psr2_enabled)
1059 hsw_activate_psr2(intel_dp);
1060 else
1061 hsw_activate_psr1(intel_dp);
1062
1063 intel_dp->psr.active = true;
1064}
1065
1066static void intel_psr_enable_source(struct intel_dp *intel_dp)
1067{
1068 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1069 enum transcoder cpu_transcoder = intel_dp->psr.transcoder;
1070 u32 mask;
1071
1072 if (intel_dp->psr.psr2_enabled && DISPLAY_VER(dev_priv) == 9) {
1073 i915_reg_t reg = CHICKEN_TRANS(cpu_transcoder);
1074 u32 chicken = intel_de_read(dev_priv, reg);
1075
1076 chicken |= PSR2_VSC_ENABLE_PROG_HEADER |
1077 PSR2_ADD_VERTICAL_LINE_COUNT;
1078 intel_de_write(dev_priv, reg, chicken);
1079 }
1080
1081
1082
1083
1084
1085
1086 if (IS_ALDERLAKE_P(dev_priv) &&
1087 intel_dp->psr.psr2_enabled)
1088 intel_de_rmw(dev_priv, CHICKEN_TRANS(cpu_transcoder), 0,
1089 ADLP_1_BASED_X_GRANULARITY);
1090
1091
1092
1093
1094
1095
1096
1097 mask = EDP_PSR_DEBUG_MASK_MEMUP |
1098 EDP_PSR_DEBUG_MASK_HPD |
1099 EDP_PSR_DEBUG_MASK_LPSP |
1100 EDP_PSR_DEBUG_MASK_MAX_SLEEP;
1101
1102 if (DISPLAY_VER(dev_priv) < 11)
1103 mask |= EDP_PSR_DEBUG_MASK_DISP_REG_WRITE;
1104
1105 intel_de_write(dev_priv, EDP_PSR_DEBUG(intel_dp->psr.transcoder),
1106 mask);
1107
1108 psr_irq_control(intel_dp);
1109
1110 if (intel_dp->psr.dc3co_exitline) {
1111 u32 val;
1112
1113
1114
1115
1116
1117 val = intel_de_read(dev_priv, EXITLINE(cpu_transcoder));
1118 val &= ~EXITLINE_MASK;
1119 val |= intel_dp->psr.dc3co_exitline << EXITLINE_SHIFT;
1120 val |= EXITLINE_ENABLE;
1121 intel_de_write(dev_priv, EXITLINE(cpu_transcoder), val);
1122 }
1123
1124 if (HAS_PSR_HW_TRACKING(dev_priv) && HAS_PSR2_SEL_FETCH(dev_priv))
1125 intel_de_rmw(dev_priv, CHICKEN_PAR1_1, IGNORE_PSR2_HW_TRACKING,
1126 intel_dp->psr.psr2_sel_fetch_enabled ?
1127 IGNORE_PSR2_HW_TRACKING : 0);
1128
1129
1130 if (IS_ADLP_DISPLAY_STEP(dev_priv, STEP_A0, STEP_B0) &&
1131 intel_dp->psr.psr2_enabled)
1132 intel_de_rmw(dev_priv,
1133 TRANS_SET_CONTEXT_LATENCY(intel_dp->psr.transcoder),
1134 TRANS_SET_CONTEXT_LATENCY_MASK,
1135 TRANS_SET_CONTEXT_LATENCY_VALUE(1));
1136
1137
1138 if (IS_ALDERLAKE_P(dev_priv) && intel_dp->psr.psr2_enabled)
1139 intel_de_rmw(dev_priv, CLKGATE_DIS_MISC, 0,
1140 CLKGATE_DIS_MISC_DMASC_GATING_DIS);
1141}
1142
1143static bool psr_interrupt_error_check(struct intel_dp *intel_dp)
1144{
1145 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1146 u32 val;
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156 if (DISPLAY_VER(dev_priv) >= 12) {
1157 val = intel_de_read(dev_priv,
1158 TRANS_PSR_IIR(intel_dp->psr.transcoder));
1159 val &= EDP_PSR_ERROR(0);
1160 } else {
1161 val = intel_de_read(dev_priv, EDP_PSR_IIR);
1162 val &= EDP_PSR_ERROR(intel_dp->psr.transcoder);
1163 }
1164 if (val) {
1165 intel_dp->psr.sink_not_reliable = true;
1166 drm_dbg_kms(&dev_priv->drm,
1167 "PSR interruption error set, not enabling PSR\n");
1168 return false;
1169 }
1170
1171 return true;
1172}
1173
1174static void intel_psr_enable_locked(struct intel_dp *intel_dp,
1175 const struct intel_crtc_state *crtc_state)
1176{
1177 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
1178 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1179 enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
1180 struct intel_encoder *encoder = &dig_port->base;
1181 u32 val;
1182
1183 drm_WARN_ON(&dev_priv->drm, intel_dp->psr.enabled);
1184
1185 intel_dp->psr.psr2_enabled = crtc_state->has_psr2;
1186 intel_dp->psr.busy_frontbuffer_bits = 0;
1187 intel_dp->psr.pipe = to_intel_crtc(crtc_state->uapi.crtc)->pipe;
1188 intel_dp->psr.transcoder = crtc_state->cpu_transcoder;
1189
1190 val = usecs_to_jiffies(intel_get_frame_time_us(crtc_state) * 6);
1191 intel_dp->psr.dc3co_exit_delay = val;
1192 intel_dp->psr.dc3co_exitline = crtc_state->dc3co_exitline;
1193 intel_dp->psr.psr2_sel_fetch_enabled = crtc_state->enable_psr2_sel_fetch;
1194 intel_dp->psr.req_psr2_sdp_prior_scanline =
1195 crtc_state->req_psr2_sdp_prior_scanline;
1196
1197 if (!psr_interrupt_error_check(intel_dp))
1198 return;
1199
1200 drm_dbg_kms(&dev_priv->drm, "Enabling PSR%s\n",
1201 intel_dp->psr.psr2_enabled ? "2" : "1");
1202 intel_write_dp_vsc_sdp(encoder, crtc_state, &crtc_state->psr_vsc);
1203 intel_snps_phy_update_psr_power_state(dev_priv, phy, true);
1204 intel_psr_enable_sink(intel_dp);
1205 intel_psr_enable_source(intel_dp);
1206 intel_dp->psr.enabled = true;
1207 intel_dp->psr.paused = false;
1208
1209 intel_psr_activate(intel_dp);
1210}
1211
1212static void intel_psr_exit(struct intel_dp *intel_dp)
1213{
1214 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1215 u32 val;
1216
1217 if (!intel_dp->psr.active) {
1218 if (transcoder_has_psr2(dev_priv, intel_dp->psr.transcoder)) {
1219 val = intel_de_read(dev_priv,
1220 EDP_PSR2_CTL(intel_dp->psr.transcoder));
1221 drm_WARN_ON(&dev_priv->drm, val & EDP_PSR2_ENABLE);
1222 }
1223
1224 val = intel_de_read(dev_priv,
1225 EDP_PSR_CTL(intel_dp->psr.transcoder));
1226 drm_WARN_ON(&dev_priv->drm, val & EDP_PSR_ENABLE);
1227
1228 return;
1229 }
1230
1231 if (intel_dp->psr.psr2_enabled) {
1232 tgl_disallow_dc3co_on_psr2_exit(intel_dp);
1233 val = intel_de_read(dev_priv,
1234 EDP_PSR2_CTL(intel_dp->psr.transcoder));
1235 drm_WARN_ON(&dev_priv->drm, !(val & EDP_PSR2_ENABLE));
1236 val &= ~EDP_PSR2_ENABLE;
1237 intel_de_write(dev_priv,
1238 EDP_PSR2_CTL(intel_dp->psr.transcoder), val);
1239 } else {
1240 val = intel_de_read(dev_priv,
1241 EDP_PSR_CTL(intel_dp->psr.transcoder));
1242 drm_WARN_ON(&dev_priv->drm, !(val & EDP_PSR_ENABLE));
1243 val &= ~EDP_PSR_ENABLE;
1244 intel_de_write(dev_priv,
1245 EDP_PSR_CTL(intel_dp->psr.transcoder), val);
1246 }
1247 intel_dp->psr.active = false;
1248}
1249
1250static void intel_psr_wait_exit_locked(struct intel_dp *intel_dp)
1251{
1252 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1253 i915_reg_t psr_status;
1254 u32 psr_status_mask;
1255
1256 if (intel_dp->psr.psr2_enabled) {
1257 psr_status = EDP_PSR2_STATUS(intel_dp->psr.transcoder);
1258 psr_status_mask = EDP_PSR2_STATUS_STATE_MASK;
1259 } else {
1260 psr_status = EDP_PSR_STATUS(intel_dp->psr.transcoder);
1261 psr_status_mask = EDP_PSR_STATUS_STATE_MASK;
1262 }
1263
1264
1265 if (intel_de_wait_for_clear(dev_priv, psr_status,
1266 psr_status_mask, 2000))
1267 drm_err(&dev_priv->drm, "Timed out waiting PSR idle state\n");
1268}
1269
1270static void intel_psr_disable_locked(struct intel_dp *intel_dp)
1271{
1272 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1273 enum phy phy = intel_port_to_phy(dev_priv,
1274 dp_to_dig_port(intel_dp)->base.port);
1275
1276 lockdep_assert_held(&intel_dp->psr.lock);
1277
1278 if (!intel_dp->psr.enabled)
1279 return;
1280
1281 drm_dbg_kms(&dev_priv->drm, "Disabling PSR%s\n",
1282 intel_dp->psr.psr2_enabled ? "2" : "1");
1283
1284 intel_psr_exit(intel_dp);
1285 intel_psr_wait_exit_locked(intel_dp);
1286
1287
1288 if (intel_dp->psr.psr2_sel_fetch_enabled &&
1289 IS_TGL_DISPLAY_STEP(dev_priv, STEP_A0, STEP_B0))
1290 intel_de_rmw(dev_priv, CHICKEN_PAR1_1,
1291 DIS_RAM_BYPASS_PSR2_MAN_TRACK, 0);
1292
1293
1294 if (IS_ADLP_DISPLAY_STEP(dev_priv, STEP_A0, STEP_B0) &&
1295 intel_dp->psr.psr2_enabled)
1296 intel_de_rmw(dev_priv,
1297 TRANS_SET_CONTEXT_LATENCY(intel_dp->psr.transcoder),
1298 TRANS_SET_CONTEXT_LATENCY_MASK, 0);
1299
1300
1301 if (IS_ALDERLAKE_P(dev_priv) && intel_dp->psr.psr2_enabled)
1302 intel_de_rmw(dev_priv, CLKGATE_DIS_MISC,
1303 CLKGATE_DIS_MISC_DMASC_GATING_DIS, 0);
1304
1305 intel_snps_phy_update_psr_power_state(dev_priv, phy, false);
1306
1307
1308 drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_EN_CFG, 0);
1309
1310 if (intel_dp->psr.psr2_enabled)
1311 drm_dp_dpcd_writeb(&intel_dp->aux, DP_RECEIVER_ALPM_CONFIG, 0);
1312
1313 intel_dp->psr.enabled = false;
1314}
1315
1316
1317
1318
1319
1320
1321
1322
1323void intel_psr_disable(struct intel_dp *intel_dp,
1324 const struct intel_crtc_state *old_crtc_state)
1325{
1326 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1327
1328 if (!old_crtc_state->has_psr)
1329 return;
1330
1331 if (drm_WARN_ON(&dev_priv->drm, !CAN_PSR(intel_dp)))
1332 return;
1333
1334 mutex_lock(&intel_dp->psr.lock);
1335
1336 intel_psr_disable_locked(intel_dp);
1337
1338 mutex_unlock(&intel_dp->psr.lock);
1339 cancel_work_sync(&intel_dp->psr.work);
1340 cancel_delayed_work_sync(&intel_dp->psr.dc3co_work);
1341}
1342
1343
1344
1345
1346
1347
1348
1349void intel_psr_pause(struct intel_dp *intel_dp)
1350{
1351 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1352 struct intel_psr *psr = &intel_dp->psr;
1353
1354 if (!CAN_PSR(intel_dp))
1355 return;
1356
1357 mutex_lock(&psr->lock);
1358
1359 if (!psr->enabled) {
1360 mutex_unlock(&psr->lock);
1361 return;
1362 }
1363
1364
1365 drm_WARN_ON(&dev_priv->drm, psr->paused);
1366
1367 intel_psr_exit(intel_dp);
1368 intel_psr_wait_exit_locked(intel_dp);
1369 psr->paused = true;
1370
1371 mutex_unlock(&psr->lock);
1372
1373 cancel_work_sync(&psr->work);
1374 cancel_delayed_work_sync(&psr->dc3co_work);
1375}
1376
1377
1378
1379
1380
1381
1382
1383void intel_psr_resume(struct intel_dp *intel_dp)
1384{
1385 struct intel_psr *psr = &intel_dp->psr;
1386
1387 if (!CAN_PSR(intel_dp))
1388 return;
1389
1390 mutex_lock(&psr->lock);
1391
1392 if (!psr->paused)
1393 goto unlock;
1394
1395 psr->paused = false;
1396 intel_psr_activate(intel_dp);
1397
1398unlock:
1399 mutex_unlock(&psr->lock);
1400}
1401
1402static inline u32 man_trk_ctl_single_full_frame_bit_get(struct drm_i915_private *dev_priv)
1403{
1404 return IS_ALDERLAKE_P(dev_priv) ?
1405 ADLP_PSR2_MAN_TRK_CTL_SF_SINGLE_FULL_FRAME :
1406 PSR2_MAN_TRK_CTL_SF_SINGLE_FULL_FRAME;
1407}
1408
1409static inline u32 man_trk_ctl_partial_frame_bit_get(struct drm_i915_private *dev_priv)
1410{
1411 return IS_ALDERLAKE_P(dev_priv) ?
1412 ADLP_PSR2_MAN_TRK_CTL_SF_PARTIAL_FRAME_UPDATE :
1413 PSR2_MAN_TRK_CTL_SF_PARTIAL_FRAME_UPDATE;
1414}
1415
1416static void psr_force_hw_tracking_exit(struct intel_dp *intel_dp)
1417{
1418 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1419
1420 if (intel_dp->psr.psr2_sel_fetch_enabled)
1421 intel_de_rmw(dev_priv,
1422 PSR2_MAN_TRK_CTL(intel_dp->psr.transcoder), 0,
1423 man_trk_ctl_single_full_frame_bit_get(dev_priv));
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438 intel_de_write(dev_priv, CURSURFLIVE(intel_dp->psr.pipe), 0);
1439}
1440
1441void intel_psr2_disable_plane_sel_fetch(struct intel_plane *plane,
1442 const struct intel_crtc_state *crtc_state)
1443{
1444 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1445 enum pipe pipe = plane->pipe;
1446
1447 if (!crtc_state->enable_psr2_sel_fetch)
1448 return;
1449
1450 intel_de_write_fw(dev_priv, PLANE_SEL_FETCH_CTL(pipe, plane->id), 0);
1451}
1452
1453void intel_psr2_program_plane_sel_fetch(struct intel_plane *plane,
1454 const struct intel_crtc_state *crtc_state,
1455 const struct intel_plane_state *plane_state,
1456 int color_plane)
1457{
1458 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1459 enum pipe pipe = plane->pipe;
1460 const struct drm_rect *clip;
1461 u32 val;
1462 int x, y;
1463
1464 if (!crtc_state->enable_psr2_sel_fetch)
1465 return;
1466
1467 if (plane->id == PLANE_CURSOR) {
1468 intel_de_write_fw(dev_priv, PLANE_SEL_FETCH_CTL(pipe, plane->id),
1469 plane_state->ctl);
1470 return;
1471 }
1472
1473 clip = &plane_state->psr2_sel_fetch_area;
1474
1475 val = (clip->y1 + plane_state->uapi.dst.y1) << 16;
1476 val |= plane_state->uapi.dst.x1;
1477 intel_de_write_fw(dev_priv, PLANE_SEL_FETCH_POS(pipe, plane->id), val);
1478
1479 x = plane_state->view.color_plane[color_plane].x;
1480
1481
1482
1483
1484
1485 if (!color_plane)
1486 y = plane_state->view.color_plane[color_plane].y + clip->y1;
1487 else
1488 y = plane_state->view.color_plane[color_plane].y + clip->y1 / 2;
1489
1490 val = y << 16 | x;
1491
1492 intel_de_write_fw(dev_priv, PLANE_SEL_FETCH_OFFSET(pipe, plane->id),
1493 val);
1494
1495
1496 val = (drm_rect_height(clip) - 1) << 16;
1497 val |= (drm_rect_width(&plane_state->uapi.src) >> 16) - 1;
1498 intel_de_write_fw(dev_priv, PLANE_SEL_FETCH_SIZE(pipe, plane->id), val);
1499
1500 intel_de_write_fw(dev_priv, PLANE_SEL_FETCH_CTL(pipe, plane->id),
1501 PLANE_SEL_FETCH_CTL_ENABLE);
1502}
1503
1504void intel_psr2_program_trans_man_trk_ctl(const struct intel_crtc_state *crtc_state)
1505{
1506 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1507
1508 if (!crtc_state->enable_psr2_sel_fetch)
1509 return;
1510
1511 intel_de_write(dev_priv, PSR2_MAN_TRK_CTL(crtc_state->cpu_transcoder),
1512 crtc_state->psr2_man_track_ctl);
1513}
1514
1515static void psr2_man_trk_ctl_calc(struct intel_crtc_state *crtc_state,
1516 struct drm_rect *clip, bool full_update)
1517{
1518 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1519 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1520 u32 val = 0;
1521
1522 if (!IS_ALDERLAKE_P(dev_priv))
1523 val = PSR2_MAN_TRK_CTL_ENABLE;
1524
1525
1526 val |= man_trk_ctl_partial_frame_bit_get(dev_priv);
1527
1528 if (full_update) {
1529
1530
1531
1532
1533 val |= man_trk_ctl_single_full_frame_bit_get(dev_priv);
1534 goto exit;
1535 }
1536
1537 if (clip->y1 == -1)
1538 goto exit;
1539
1540 if (IS_ALDERLAKE_P(dev_priv)) {
1541 val |= ADLP_PSR2_MAN_TRK_CTL_SU_REGION_START_ADDR(clip->y1);
1542 val |= ADLP_PSR2_MAN_TRK_CTL_SU_REGION_END_ADDR(clip->y2 - 1);
1543 } else {
1544 drm_WARN_ON(crtc_state->uapi.crtc->dev, clip->y1 % 4 || clip->y2 % 4);
1545
1546 val |= PSR2_MAN_TRK_CTL_SU_REGION_START_ADDR(clip->y1 / 4 + 1);
1547 val |= PSR2_MAN_TRK_CTL_SU_REGION_END_ADDR(clip->y2 / 4 + 1);
1548 }
1549exit:
1550 crtc_state->psr2_man_track_ctl = val;
1551}
1552
1553static void clip_area_update(struct drm_rect *overlap_damage_area,
1554 struct drm_rect *damage_area)
1555{
1556 if (overlap_damage_area->y1 == -1) {
1557 overlap_damage_area->y1 = damage_area->y1;
1558 overlap_damage_area->y2 = damage_area->y2;
1559 return;
1560 }
1561
1562 if (damage_area->y1 < overlap_damage_area->y1)
1563 overlap_damage_area->y1 = damage_area->y1;
1564
1565 if (damage_area->y2 > overlap_damage_area->y2)
1566 overlap_damage_area->y2 = damage_area->y2;
1567}
1568
1569static void intel_psr2_sel_fetch_pipe_alignment(const struct intel_crtc_state *crtc_state,
1570 struct drm_rect *pipe_clip)
1571{
1572 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1573 const u16 y_alignment = crtc_state->su_y_granularity;
1574
1575 pipe_clip->y1 -= pipe_clip->y1 % y_alignment;
1576 if (pipe_clip->y2 % y_alignment)
1577 pipe_clip->y2 = ((pipe_clip->y2 / y_alignment) + 1) * y_alignment;
1578
1579 if (IS_ALDERLAKE_P(dev_priv) && crtc_state->dsc.compression_enable)
1580 drm_warn(&dev_priv->drm, "Missing PSR2 sel fetch alignment with DSC\n");
1581}
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592static bool psr2_sel_fetch_plane_state_supported(const struct intel_plane_state *plane_state)
1593{
1594 if (plane_state->uapi.dst.y1 < 0 ||
1595 plane_state->uapi.dst.x1 < 0 ||
1596 plane_state->scaler_id >= 0 ||
1597 plane_state->uapi.rotation != DRM_MODE_ROTATE_0)
1598 return false;
1599
1600 return true;
1601}
1602
1603
1604
1605
1606
1607
1608
1609
1610static bool psr2_sel_fetch_pipe_state_supported(const struct intel_crtc_state *crtc_state)
1611{
1612 if (crtc_state->scaler_state.scaler_id >= 0)
1613 return false;
1614
1615 return true;
1616}
1617
1618int intel_psr2_sel_fetch_update(struct intel_atomic_state *state,
1619 struct intel_crtc *crtc)
1620{
1621 struct intel_crtc_state *crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
1622 struct drm_rect pipe_clip = { .x1 = 0, .y1 = -1, .x2 = INT_MAX, .y2 = -1 };
1623 struct intel_plane_state *new_plane_state, *old_plane_state;
1624 struct intel_plane *plane;
1625 bool full_update = false;
1626 int i, ret;
1627
1628 if (!crtc_state->enable_psr2_sel_fetch)
1629 return 0;
1630
1631 if (!psr2_sel_fetch_pipe_state_supported(crtc_state)) {
1632 full_update = true;
1633 goto skip_sel_fetch_set_loop;
1634 }
1635
1636
1637
1638
1639
1640
1641
1642 for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
1643 new_plane_state, i) {
1644 struct drm_rect src, damaged_area = { .y1 = -1 };
1645 struct drm_atomic_helper_damage_iter iter;
1646 struct drm_rect clip;
1647
1648 if (new_plane_state->uapi.crtc != crtc_state->uapi.crtc)
1649 continue;
1650
1651 if (!new_plane_state->uapi.visible &&
1652 !old_plane_state->uapi.visible)
1653 continue;
1654
1655 if (!psr2_sel_fetch_plane_state_supported(new_plane_state)) {
1656 full_update = true;
1657 break;
1658 }
1659
1660
1661
1662
1663
1664
1665 if (new_plane_state->uapi.visible != old_plane_state->uapi.visible ||
1666 !drm_rect_equals(&new_plane_state->uapi.dst,
1667 &old_plane_state->uapi.dst)) {
1668 if (old_plane_state->uapi.visible) {
1669 damaged_area.y1 = old_plane_state->uapi.dst.y1;
1670 damaged_area.y2 = old_plane_state->uapi.dst.y2;
1671 clip_area_update(&pipe_clip, &damaged_area);
1672 }
1673
1674 if (new_plane_state->uapi.visible) {
1675 damaged_area.y1 = new_plane_state->uapi.dst.y1;
1676 damaged_area.y2 = new_plane_state->uapi.dst.y2;
1677 clip_area_update(&pipe_clip, &damaged_area);
1678 }
1679 continue;
1680 } else if (new_plane_state->uapi.alpha != old_plane_state->uapi.alpha) {
1681
1682 damaged_area.y1 = new_plane_state->uapi.dst.y1;
1683 damaged_area.y2 = new_plane_state->uapi.dst.y2;
1684 clip_area_update(&pipe_clip, &damaged_area);
1685 continue;
1686 }
1687
1688 drm_rect_fp_to_int(&src, &new_plane_state->uapi.src);
1689
1690 drm_atomic_helper_damage_iter_init(&iter,
1691 &old_plane_state->uapi,
1692 &new_plane_state->uapi);
1693 drm_atomic_for_each_plane_damage(&iter, &clip) {
1694 if (drm_rect_intersect(&clip, &src))
1695 clip_area_update(&damaged_area, &clip);
1696 }
1697
1698 if (damaged_area.y1 == -1)
1699 continue;
1700
1701 damaged_area.y1 += new_plane_state->uapi.dst.y1 - src.y1;
1702 damaged_area.y2 += new_plane_state->uapi.dst.y1 - src.y1;
1703 clip_area_update(&pipe_clip, &damaged_area);
1704 }
1705
1706 if (full_update)
1707 goto skip_sel_fetch_set_loop;
1708
1709 ret = drm_atomic_add_affected_planes(&state->base, &crtc->base);
1710 if (ret)
1711 return ret;
1712
1713 intel_psr2_sel_fetch_pipe_alignment(crtc_state, &pipe_clip);
1714
1715
1716
1717
1718
1719 for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
1720 new_plane_state, i) {
1721 struct drm_rect *sel_fetch_area, inter;
1722 struct intel_plane *linked = new_plane_state->planar_linked_plane;
1723
1724 if (new_plane_state->uapi.crtc != crtc_state->uapi.crtc ||
1725 !new_plane_state->uapi.visible)
1726 continue;
1727
1728 inter = pipe_clip;
1729 if (!drm_rect_intersect(&inter, &new_plane_state->uapi.dst))
1730 continue;
1731
1732 if (!psr2_sel_fetch_plane_state_supported(new_plane_state)) {
1733 full_update = true;
1734 break;
1735 }
1736
1737 sel_fetch_area = &new_plane_state->psr2_sel_fetch_area;
1738 sel_fetch_area->y1 = inter.y1 - new_plane_state->uapi.dst.y1;
1739 sel_fetch_area->y2 = inter.y2 - new_plane_state->uapi.dst.y1;
1740 crtc_state->update_planes |= BIT(plane->id);
1741
1742
1743
1744
1745
1746 if (linked) {
1747 struct intel_plane_state *linked_new_plane_state;
1748 struct drm_rect *linked_sel_fetch_area;
1749
1750 linked_new_plane_state = intel_atomic_get_plane_state(state, linked);
1751 if (IS_ERR(linked_new_plane_state))
1752 return PTR_ERR(linked_new_plane_state);
1753
1754 linked_sel_fetch_area = &linked_new_plane_state->psr2_sel_fetch_area;
1755 linked_sel_fetch_area->y1 = sel_fetch_area->y1;
1756 linked_sel_fetch_area->y2 = sel_fetch_area->y2;
1757 crtc_state->update_planes |= BIT(linked->id);
1758 }
1759 }
1760
1761skip_sel_fetch_set_loop:
1762 psr2_man_trk_ctl_calc(crtc_state, &pipe_clip, full_update);
1763 return 0;
1764}
1765
1766void intel_psr_pre_plane_update(struct intel_atomic_state *state,
1767 struct intel_crtc *crtc)
1768{
1769 struct drm_i915_private *i915 = to_i915(state->base.dev);
1770 const struct intel_crtc_state *crtc_state =
1771 intel_atomic_get_new_crtc_state(state, crtc);
1772 struct intel_encoder *encoder;
1773
1774 if (!HAS_PSR(i915))
1775 return;
1776
1777 for_each_intel_encoder_mask_with_psr(state->base.dev, encoder,
1778 crtc_state->uapi.encoder_mask) {
1779 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1780 struct intel_psr *psr = &intel_dp->psr;
1781 bool needs_to_disable = false;
1782
1783 mutex_lock(&psr->lock);
1784
1785
1786
1787
1788
1789
1790
1791 needs_to_disable |= intel_crtc_needs_modeset(crtc_state);
1792 needs_to_disable |= !crtc_state->has_psr;
1793 needs_to_disable |= !crtc_state->active_planes;
1794 needs_to_disable |= crtc_state->has_psr2 != psr->psr2_enabled;
1795
1796 if (psr->enabled && needs_to_disable)
1797 intel_psr_disable_locked(intel_dp);
1798
1799 mutex_unlock(&psr->lock);
1800 }
1801}
1802
1803static void _intel_psr_post_plane_update(const struct intel_atomic_state *state,
1804 const struct intel_crtc_state *crtc_state)
1805{
1806 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1807 struct intel_encoder *encoder;
1808
1809 if (!crtc_state->has_psr)
1810 return;
1811
1812 for_each_intel_encoder_mask_with_psr(state->base.dev, encoder,
1813 crtc_state->uapi.encoder_mask) {
1814 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1815 struct intel_psr *psr = &intel_dp->psr;
1816
1817 mutex_lock(&psr->lock);
1818
1819 drm_WARN_ON(&dev_priv->drm, psr->enabled && !crtc_state->active_planes);
1820
1821
1822 if (!psr->enabled && crtc_state->active_planes)
1823 intel_psr_enable_locked(intel_dp, crtc_state);
1824
1825
1826 if (crtc_state->crc_enabled && psr->enabled)
1827 psr_force_hw_tracking_exit(intel_dp);
1828
1829 mutex_unlock(&psr->lock);
1830 }
1831}
1832
1833void intel_psr_post_plane_update(const struct intel_atomic_state *state)
1834{
1835 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1836 struct intel_crtc_state *crtc_state;
1837 struct intel_crtc *crtc;
1838 int i;
1839
1840 if (!HAS_PSR(dev_priv))
1841 return;
1842
1843 for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i)
1844 _intel_psr_post_plane_update(state, crtc_state);
1845}
1846
1847static int _psr2_ready_for_pipe_update_locked(struct intel_dp *intel_dp)
1848{
1849 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1850
1851
1852
1853
1854
1855
1856 return intel_de_wait_for_clear(dev_priv,
1857 EDP_PSR2_STATUS(intel_dp->psr.transcoder),
1858 EDP_PSR2_STATUS_STATE_DEEP_SLEEP, 50);
1859}
1860
1861static int _psr1_ready_for_pipe_update_locked(struct intel_dp *intel_dp)
1862{
1863 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1864
1865
1866
1867
1868
1869
1870
1871 return intel_de_wait_for_clear(dev_priv,
1872 EDP_PSR_STATUS(intel_dp->psr.transcoder),
1873 EDP_PSR_STATUS_STATE_MASK, 50);
1874}
1875
1876
1877
1878
1879
1880
1881
1882
1883void intel_psr_wait_for_idle(const struct intel_crtc_state *new_crtc_state)
1884{
1885 struct drm_i915_private *dev_priv = to_i915(new_crtc_state->uapi.crtc->dev);
1886 struct intel_encoder *encoder;
1887
1888 if (!new_crtc_state->has_psr)
1889 return;
1890
1891 for_each_intel_encoder_mask_with_psr(&dev_priv->drm, encoder,
1892 new_crtc_state->uapi.encoder_mask) {
1893 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
1894 int ret;
1895
1896 mutex_lock(&intel_dp->psr.lock);
1897
1898 if (!intel_dp->psr.enabled) {
1899 mutex_unlock(&intel_dp->psr.lock);
1900 continue;
1901 }
1902
1903 if (intel_dp->psr.psr2_enabled)
1904 ret = _psr2_ready_for_pipe_update_locked(intel_dp);
1905 else
1906 ret = _psr1_ready_for_pipe_update_locked(intel_dp);
1907
1908 if (ret)
1909 drm_err(&dev_priv->drm, "PSR wait timed out, atomic update may fail\n");
1910
1911 mutex_unlock(&intel_dp->psr.lock);
1912 }
1913}
1914
1915static bool __psr_wait_for_idle_locked(struct intel_dp *intel_dp)
1916{
1917 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
1918 i915_reg_t reg;
1919 u32 mask;
1920 int err;
1921
1922 if (!intel_dp->psr.enabled)
1923 return false;
1924
1925 if (intel_dp->psr.psr2_enabled) {
1926 reg = EDP_PSR2_STATUS(intel_dp->psr.transcoder);
1927 mask = EDP_PSR2_STATUS_STATE_MASK;
1928 } else {
1929 reg = EDP_PSR_STATUS(intel_dp->psr.transcoder);
1930 mask = EDP_PSR_STATUS_STATE_MASK;
1931 }
1932
1933 mutex_unlock(&intel_dp->psr.lock);
1934
1935 err = intel_de_wait_for_clear(dev_priv, reg, mask, 50);
1936 if (err)
1937 drm_err(&dev_priv->drm,
1938 "Timed out waiting for PSR Idle for re-enable\n");
1939
1940
1941 mutex_lock(&intel_dp->psr.lock);
1942 return err == 0 && intel_dp->psr.enabled;
1943}
1944
1945static int intel_psr_fastset_force(struct drm_i915_private *dev_priv)
1946{
1947 struct drm_connector_list_iter conn_iter;
1948 struct drm_device *dev = &dev_priv->drm;
1949 struct drm_modeset_acquire_ctx ctx;
1950 struct drm_atomic_state *state;
1951 struct drm_connector *conn;
1952 int err = 0;
1953
1954 state = drm_atomic_state_alloc(dev);
1955 if (!state)
1956 return -ENOMEM;
1957
1958 drm_modeset_acquire_init(&ctx, DRM_MODESET_ACQUIRE_INTERRUPTIBLE);
1959 state->acquire_ctx = &ctx;
1960
1961retry:
1962
1963 drm_connector_list_iter_begin(dev, &conn_iter);
1964 drm_for_each_connector_iter(conn, &conn_iter) {
1965 struct drm_connector_state *conn_state;
1966 struct drm_crtc_state *crtc_state;
1967
1968 if (conn->connector_type != DRM_MODE_CONNECTOR_eDP)
1969 continue;
1970
1971 conn_state = drm_atomic_get_connector_state(state, conn);
1972 if (IS_ERR(conn_state)) {
1973 err = PTR_ERR(conn_state);
1974 break;
1975 }
1976
1977 if (!conn_state->crtc)
1978 continue;
1979
1980 crtc_state = drm_atomic_get_crtc_state(state, conn_state->crtc);
1981 if (IS_ERR(crtc_state)) {
1982 err = PTR_ERR(crtc_state);
1983 break;
1984 }
1985
1986
1987 crtc_state->mode_changed = true;
1988 }
1989 drm_connector_list_iter_end(&conn_iter);
1990
1991 if (err == 0)
1992 err = drm_atomic_commit(state);
1993
1994 if (err == -EDEADLK) {
1995 drm_atomic_state_clear(state);
1996 err = drm_modeset_backoff(&ctx);
1997 if (!err)
1998 goto retry;
1999 }
2000
2001 drm_modeset_drop_locks(&ctx);
2002 drm_modeset_acquire_fini(&ctx);
2003 drm_atomic_state_put(state);
2004
2005 return err;
2006}
2007
2008int intel_psr_debug_set(struct intel_dp *intel_dp, u64 val)
2009{
2010 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
2011 const u32 mode = val & I915_PSR_DEBUG_MODE_MASK;
2012 u32 old_mode;
2013 int ret;
2014
2015 if (val & ~(I915_PSR_DEBUG_IRQ | I915_PSR_DEBUG_MODE_MASK) ||
2016 mode > I915_PSR_DEBUG_ENABLE_SEL_FETCH) {
2017 drm_dbg_kms(&dev_priv->drm, "Invalid debug mask %llx\n", val);
2018 return -EINVAL;
2019 }
2020
2021 ret = mutex_lock_interruptible(&intel_dp->psr.lock);
2022 if (ret)
2023 return ret;
2024
2025 old_mode = intel_dp->psr.debug & I915_PSR_DEBUG_MODE_MASK;
2026 intel_dp->psr.debug = val;
2027
2028
2029
2030
2031
2032 if (intel_dp->psr.enabled)
2033 psr_irq_control(intel_dp);
2034
2035 mutex_unlock(&intel_dp->psr.lock);
2036
2037 if (old_mode != mode)
2038 ret = intel_psr_fastset_force(dev_priv);
2039
2040 return ret;
2041}
2042
2043static void intel_psr_handle_irq(struct intel_dp *intel_dp)
2044{
2045 struct intel_psr *psr = &intel_dp->psr;
2046
2047 intel_psr_disable_locked(intel_dp);
2048 psr->sink_not_reliable = true;
2049
2050 drm_dp_dpcd_writeb(&intel_dp->aux, DP_SET_POWER, DP_SET_POWER_D0);
2051}
2052
2053static void intel_psr_work(struct work_struct *work)
2054{
2055 struct intel_dp *intel_dp =
2056 container_of(work, typeof(*intel_dp), psr.work);
2057
2058 mutex_lock(&intel_dp->psr.lock);
2059
2060 if (!intel_dp->psr.enabled)
2061 goto unlock;
2062
2063 if (READ_ONCE(intel_dp->psr.irq_aux_error))
2064 intel_psr_handle_irq(intel_dp);
2065
2066
2067
2068
2069
2070
2071
2072 if (!__psr_wait_for_idle_locked(intel_dp))
2073 goto unlock;
2074
2075
2076
2077
2078
2079
2080 if (intel_dp->psr.busy_frontbuffer_bits || intel_dp->psr.active)
2081 goto unlock;
2082
2083 intel_psr_activate(intel_dp);
2084unlock:
2085 mutex_unlock(&intel_dp->psr.lock);
2086}
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101void intel_psr_invalidate(struct drm_i915_private *dev_priv,
2102 unsigned frontbuffer_bits, enum fb_op_origin origin)
2103{
2104 struct intel_encoder *encoder;
2105
2106 if (origin == ORIGIN_FLIP)
2107 return;
2108
2109 for_each_intel_encoder_with_psr(&dev_priv->drm, encoder) {
2110 unsigned int pipe_frontbuffer_bits = frontbuffer_bits;
2111 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2112
2113 mutex_lock(&intel_dp->psr.lock);
2114 if (!intel_dp->psr.enabled) {
2115 mutex_unlock(&intel_dp->psr.lock);
2116 continue;
2117 }
2118
2119 pipe_frontbuffer_bits &=
2120 INTEL_FRONTBUFFER_ALL_MASK(intel_dp->psr.pipe);
2121 intel_dp->psr.busy_frontbuffer_bits |= pipe_frontbuffer_bits;
2122
2123 if (pipe_frontbuffer_bits)
2124 intel_psr_exit(intel_dp);
2125
2126 mutex_unlock(&intel_dp->psr.lock);
2127 }
2128}
2129
2130
2131
2132
2133
2134
2135static void
2136tgl_dc3co_flush_locked(struct intel_dp *intel_dp, unsigned int frontbuffer_bits,
2137 enum fb_op_origin origin)
2138{
2139 if (!intel_dp->psr.dc3co_exitline || !intel_dp->psr.psr2_enabled ||
2140 !intel_dp->psr.active)
2141 return;
2142
2143
2144
2145
2146
2147 if (!(frontbuffer_bits &
2148 INTEL_FRONTBUFFER_ALL_MASK(intel_dp->psr.pipe)))
2149 return;
2150
2151 tgl_psr2_enable_dc3co(intel_dp);
2152 mod_delayed_work(system_wq, &intel_dp->psr.dc3co_work,
2153 intel_dp->psr.dc3co_exit_delay);
2154}
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169void intel_psr_flush(struct drm_i915_private *dev_priv,
2170 unsigned frontbuffer_bits, enum fb_op_origin origin)
2171{
2172 struct intel_encoder *encoder;
2173
2174 for_each_intel_encoder_with_psr(&dev_priv->drm, encoder) {
2175 unsigned int pipe_frontbuffer_bits = frontbuffer_bits;
2176 struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2177
2178 mutex_lock(&intel_dp->psr.lock);
2179 if (!intel_dp->psr.enabled) {
2180 mutex_unlock(&intel_dp->psr.lock);
2181 continue;
2182 }
2183
2184 pipe_frontbuffer_bits &=
2185 INTEL_FRONTBUFFER_ALL_MASK(intel_dp->psr.pipe);
2186 intel_dp->psr.busy_frontbuffer_bits &= ~pipe_frontbuffer_bits;
2187
2188
2189
2190
2191
2192
2193 if (intel_dp->psr.paused) {
2194 mutex_unlock(&intel_dp->psr.lock);
2195 continue;
2196 }
2197
2198 if (origin == ORIGIN_FLIP ||
2199 (origin == ORIGIN_CURSOR_UPDATE &&
2200 !intel_dp->psr.psr2_sel_fetch_enabled)) {
2201 tgl_dc3co_flush_locked(intel_dp, frontbuffer_bits, origin);
2202 mutex_unlock(&intel_dp->psr.lock);
2203 continue;
2204 }
2205
2206
2207 if (pipe_frontbuffer_bits)
2208 psr_force_hw_tracking_exit(intel_dp);
2209
2210 if (!intel_dp->psr.active && !intel_dp->psr.busy_frontbuffer_bits)
2211 schedule_work(&intel_dp->psr.work);
2212 mutex_unlock(&intel_dp->psr.lock);
2213 }
2214}
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224void intel_psr_init(struct intel_dp *intel_dp)
2225{
2226 struct intel_digital_port *dig_port = dp_to_dig_port(intel_dp);
2227 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
2228
2229 if (!HAS_PSR(dev_priv))
2230 return;
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241 if (DISPLAY_VER(dev_priv) < 12 && dig_port->base.port != PORT_A) {
2242 drm_dbg_kms(&dev_priv->drm,
2243 "PSR condition failed: Port not supported\n");
2244 return;
2245 }
2246
2247 intel_dp->psr.source_support = true;
2248
2249 if (dev_priv->params.enable_psr == -1)
2250 if (!dev_priv->vbt.psr.enable)
2251 dev_priv->params.enable_psr = 0;
2252
2253
2254 if (DISPLAY_VER(dev_priv) < 12)
2255
2256 intel_dp->psr.link_standby = dev_priv->vbt.psr.full_link;
2257
2258 INIT_WORK(&intel_dp->psr.work, intel_psr_work);
2259 INIT_DELAYED_WORK(&intel_dp->psr.dc3co_work, tgl_dc3co_disable_work);
2260 mutex_init(&intel_dp->psr.lock);
2261}
2262
2263static int psr_get_status_and_error_status(struct intel_dp *intel_dp,
2264 u8 *status, u8 *error_status)
2265{
2266 struct drm_dp_aux *aux = &intel_dp->aux;
2267 int ret;
2268
2269 ret = drm_dp_dpcd_readb(aux, DP_PSR_STATUS, status);
2270 if (ret != 1)
2271 return ret;
2272
2273 ret = drm_dp_dpcd_readb(aux, DP_PSR_ERROR_STATUS, error_status);
2274 if (ret != 1)
2275 return ret;
2276
2277 *status = *status & DP_PSR_SINK_STATE_MASK;
2278
2279 return 0;
2280}
2281
2282static void psr_alpm_check(struct intel_dp *intel_dp)
2283{
2284 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
2285 struct drm_dp_aux *aux = &intel_dp->aux;
2286 struct intel_psr *psr = &intel_dp->psr;
2287 u8 val;
2288 int r;
2289
2290 if (!psr->psr2_enabled)
2291 return;
2292
2293 r = drm_dp_dpcd_readb(aux, DP_RECEIVER_ALPM_STATUS, &val);
2294 if (r != 1) {
2295 drm_err(&dev_priv->drm, "Error reading ALPM status\n");
2296 return;
2297 }
2298
2299 if (val & DP_ALPM_LOCK_TIMEOUT_ERROR) {
2300 intel_psr_disable_locked(intel_dp);
2301 psr->sink_not_reliable = true;
2302 drm_dbg_kms(&dev_priv->drm,
2303 "ALPM lock timeout error, disabling PSR\n");
2304
2305
2306 drm_dp_dpcd_writeb(aux, DP_RECEIVER_ALPM_STATUS, val);
2307 }
2308}
2309
2310static void psr_capability_changed_check(struct intel_dp *intel_dp)
2311{
2312 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
2313 struct intel_psr *psr = &intel_dp->psr;
2314 u8 val;
2315 int r;
2316
2317 r = drm_dp_dpcd_readb(&intel_dp->aux, DP_PSR_ESI, &val);
2318 if (r != 1) {
2319 drm_err(&dev_priv->drm, "Error reading DP_PSR_ESI\n");
2320 return;
2321 }
2322
2323 if (val & DP_PSR_CAPS_CHANGE) {
2324 intel_psr_disable_locked(intel_dp);
2325 psr->sink_not_reliable = true;
2326 drm_dbg_kms(&dev_priv->drm,
2327 "Sink PSR capability changed, disabling PSR\n");
2328
2329
2330 drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_ESI, val);
2331 }
2332}
2333
2334void intel_psr_short_pulse(struct intel_dp *intel_dp)
2335{
2336 struct drm_i915_private *dev_priv = dp_to_i915(intel_dp);
2337 struct intel_psr *psr = &intel_dp->psr;
2338 u8 status, error_status;
2339 const u8 errors = DP_PSR_RFB_STORAGE_ERROR |
2340 DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR |
2341 DP_PSR_LINK_CRC_ERROR;
2342
2343 if (!CAN_PSR(intel_dp))
2344 return;
2345
2346 mutex_lock(&psr->lock);
2347
2348 if (!psr->enabled)
2349 goto exit;
2350
2351 if (psr_get_status_and_error_status(intel_dp, &status, &error_status)) {
2352 drm_err(&dev_priv->drm,
2353 "Error reading PSR status or error status\n");
2354 goto exit;
2355 }
2356
2357 if (status == DP_PSR_SINK_INTERNAL_ERROR || (error_status & errors)) {
2358 intel_psr_disable_locked(intel_dp);
2359 psr->sink_not_reliable = true;
2360 }
2361
2362 if (status == DP_PSR_SINK_INTERNAL_ERROR && !error_status)
2363 drm_dbg_kms(&dev_priv->drm,
2364 "PSR sink internal error, disabling PSR\n");
2365 if (error_status & DP_PSR_RFB_STORAGE_ERROR)
2366 drm_dbg_kms(&dev_priv->drm,
2367 "PSR RFB storage error, disabling PSR\n");
2368 if (error_status & DP_PSR_VSC_SDP_UNCORRECTABLE_ERROR)
2369 drm_dbg_kms(&dev_priv->drm,
2370 "PSR VSC SDP uncorrectable error, disabling PSR\n");
2371 if (error_status & DP_PSR_LINK_CRC_ERROR)
2372 drm_dbg_kms(&dev_priv->drm,
2373 "PSR Link CRC error, disabling PSR\n");
2374
2375 if (error_status & ~errors)
2376 drm_err(&dev_priv->drm,
2377 "PSR_ERROR_STATUS unhandled errors %x\n",
2378 error_status & ~errors);
2379
2380 drm_dp_dpcd_writeb(&intel_dp->aux, DP_PSR_ERROR_STATUS, error_status);
2381
2382 psr_alpm_check(intel_dp);
2383 psr_capability_changed_check(intel_dp);
2384
2385exit:
2386 mutex_unlock(&psr->lock);
2387}
2388
2389bool intel_psr_enabled(struct intel_dp *intel_dp)
2390{
2391 bool ret;
2392
2393 if (!CAN_PSR(intel_dp))
2394 return false;
2395
2396 mutex_lock(&intel_dp->psr.lock);
2397 ret = intel_dp->psr.enabled;
2398 mutex_unlock(&intel_dp->psr.lock);
2399
2400 return ret;
2401}
2402