1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2015-2018, 2020-2021 The Linux Foundation. All rights reserved.
3 */
4
5 #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
6 #include "dpu_encoder_phys.h"
7 #include "dpu_hw_interrupts.h"
8 #include "dpu_hw_merge3d.h"
9 #include "dpu_core_irq.h"
10 #include "dpu_formats.h"
11 #include "dpu_trace.h"
12 #include "disp/msm_disp_snapshot.h"
13
14 #include <drm/drm_managed.h>
15
16 #define DPU_DEBUG_VIDENC(e, fmt, ...) DPU_DEBUG("enc%d intf%d " fmt, \
17 (e) && (e)->parent ? \
18 (e)->parent->base.id : -1, \
19 (e) && (e)->hw_intf ? \
20 (e)->hw_intf->idx - INTF_0 : -1, ##__VA_ARGS__)
21
22 #define DPU_ERROR_VIDENC(e, fmt, ...) DPU_ERROR("enc%d intf%d " fmt, \
23 (e) && (e)->parent ? \
24 (e)->parent->base.id : -1, \
25 (e) && (e)->hw_intf ? \
26 (e)->hw_intf->idx - INTF_0 : -1, ##__VA_ARGS__)
27
28 #define to_dpu_encoder_phys_vid(x) \
29 container_of(x, struct dpu_encoder_phys_vid, base)
30
dpu_encoder_phys_vid_is_master(struct dpu_encoder_phys * phys_enc)31 static bool dpu_encoder_phys_vid_is_master(
32 struct dpu_encoder_phys *phys_enc)
33 {
34 bool ret = false;
35
36 if (phys_enc->split_role != ENC_ROLE_SLAVE)
37 ret = true;
38
39 return ret;
40 }
41
drm_mode_to_intf_timing_params(const struct dpu_encoder_phys * phys_enc,const struct drm_display_mode * mode,struct dpu_hw_intf_timing_params * timing)42 static void drm_mode_to_intf_timing_params(
43 const struct dpu_encoder_phys *phys_enc,
44 const struct drm_display_mode *mode,
45 struct dpu_hw_intf_timing_params *timing)
46 {
47 memset(timing, 0, sizeof(*timing));
48
49 if ((mode->htotal < mode->hsync_end)
50 || (mode->hsync_start < mode->hdisplay)
51 || (mode->vtotal < mode->vsync_end)
52 || (mode->vsync_start < mode->vdisplay)
53 || (mode->hsync_end < mode->hsync_start)
54 || (mode->vsync_end < mode->vsync_start)) {
55 DPU_ERROR(
56 "invalid params - hstart:%d,hend:%d,htot:%d,hdisplay:%d\n",
57 mode->hsync_start, mode->hsync_end,
58 mode->htotal, mode->hdisplay);
59 DPU_ERROR("vstart:%d,vend:%d,vtot:%d,vdisplay:%d\n",
60 mode->vsync_start, mode->vsync_end,
61 mode->vtotal, mode->vdisplay);
62 return;
63 }
64
65 /*
66 * https://www.kernel.org/doc/htmldocs/drm/ch02s05.html
67 * Active Region Front Porch Sync Back Porch
68 * <-----------------><------------><-----><----------->
69 * <- [hv]display --->
70 * <--------- [hv]sync_start ------>
71 * <----------------- [hv]sync_end ------->
72 * <---------------------------- [hv]total ------------->
73 */
74 timing->width = mode->hdisplay; /* active width */
75 timing->height = mode->vdisplay; /* active height */
76 timing->xres = timing->width;
77 timing->yres = timing->height;
78 timing->h_back_porch = mode->htotal - mode->hsync_end;
79 timing->h_front_porch = mode->hsync_start - mode->hdisplay;
80 timing->v_back_porch = mode->vtotal - mode->vsync_end;
81 timing->v_front_porch = mode->vsync_start - mode->vdisplay;
82 timing->hsync_pulse_width = mode->hsync_end - mode->hsync_start;
83 timing->vsync_pulse_width = mode->vsync_end - mode->vsync_start;
84 timing->hsync_polarity = (mode->flags & DRM_MODE_FLAG_NHSYNC) ? 1 : 0;
85 timing->vsync_polarity = (mode->flags & DRM_MODE_FLAG_NVSYNC) ? 1 : 0;
86 timing->border_clr = 0;
87 timing->underflow_clr = 0xff;
88 timing->hsync_skew = mode->hskew;
89
90 /* DSI controller cannot handle active-low sync signals. */
91 if (phys_enc->hw_intf->cap->type == INTF_DSI) {
92 timing->hsync_polarity = 0;
93 timing->vsync_polarity = 0;
94 }
95
96 timing->wide_bus_en = dpu_encoder_is_widebus_enabled(phys_enc->parent);
97 timing->compression_en = dpu_encoder_is_dsc_enabled(phys_enc->parent);
98
99 /*
100 * For DP/EDP, Shift timings to align it to bottom right.
101 * wide_bus_en is set for everything excluding SDM845 &
102 * porch changes cause DisplayPort failure and HDMI tearing.
103 */
104 if (phys_enc->hw_intf->cap->type == INTF_DP && timing->wide_bus_en) {
105 timing->h_back_porch += timing->h_front_porch;
106 timing->h_front_porch = 0;
107 timing->v_back_porch += timing->v_front_porch;
108 timing->v_front_porch = 0;
109 }
110
111 /*
112 * for DP, divide the horizonal parameters by 2 when
113 * widebus is enabled
114 */
115 if (phys_enc->hw_intf->cap->type == INTF_DP && timing->wide_bus_en) {
116 timing->width = timing->width >> 1;
117 timing->xres = timing->xres >> 1;
118 timing->h_back_porch = timing->h_back_porch >> 1;
119 timing->h_front_porch = timing->h_front_porch >> 1;
120 timing->hsync_pulse_width = timing->hsync_pulse_width >> 1;
121 }
122 }
123
get_horizontal_total(const struct dpu_hw_intf_timing_params * timing)124 static u32 get_horizontal_total(const struct dpu_hw_intf_timing_params *timing)
125 {
126 u32 active = timing->xres;
127 u32 inactive =
128 timing->h_back_porch + timing->h_front_porch +
129 timing->hsync_pulse_width;
130 return active + inactive;
131 }
132
get_vertical_total(const struct dpu_hw_intf_timing_params * timing)133 static u32 get_vertical_total(const struct dpu_hw_intf_timing_params *timing)
134 {
135 u32 active = timing->yres;
136 u32 inactive =
137 timing->v_back_porch + timing->v_front_porch +
138 timing->vsync_pulse_width;
139 return active + inactive;
140 }
141
142 /*
143 * programmable_fetch_get_num_lines:
144 * Number of fetch lines in vertical front porch
145 * @timing: Pointer to the intf timing information for the requested mode
146 *
147 * Returns the number of fetch lines in vertical front porch at which mdp
148 * can start fetching the next frame.
149 *
150 * Number of needed prefetch lines is anything that cannot be absorbed in the
151 * start of frame time (back porch + vsync pulse width).
152 *
153 * Some panels have very large VFP, however we only need a total number of
154 * lines based on the chip worst case latencies.
155 */
programmable_fetch_get_num_lines(struct dpu_encoder_phys * phys_enc,const struct dpu_hw_intf_timing_params * timing)156 static u32 programmable_fetch_get_num_lines(
157 struct dpu_encoder_phys *phys_enc,
158 const struct dpu_hw_intf_timing_params *timing)
159 {
160 u32 worst_case_needed_lines =
161 phys_enc->hw_intf->cap->prog_fetch_lines_worst_case;
162 u32 start_of_frame_lines =
163 timing->v_back_porch + timing->vsync_pulse_width;
164 u32 needed_vfp_lines = worst_case_needed_lines - start_of_frame_lines;
165 u32 actual_vfp_lines = 0;
166
167 /* Fetch must be outside active lines, otherwise undefined. */
168 if (start_of_frame_lines >= worst_case_needed_lines) {
169 DPU_DEBUG_VIDENC(phys_enc,
170 "prog fetch is not needed, large vbp+vsw\n");
171 actual_vfp_lines = 0;
172 } else if (timing->v_front_porch < needed_vfp_lines) {
173 /* Warn fetch needed, but not enough porch in panel config */
174 pr_warn_once
175 ("low vbp+vfp may lead to perf issues in some cases\n");
176 DPU_DEBUG_VIDENC(phys_enc,
177 "less vfp than fetch req, using entire vfp\n");
178 actual_vfp_lines = timing->v_front_porch;
179 } else {
180 DPU_DEBUG_VIDENC(phys_enc, "room in vfp for needed prefetch\n");
181 actual_vfp_lines = needed_vfp_lines;
182 }
183
184 DPU_DEBUG_VIDENC(phys_enc,
185 "v_front_porch %u v_back_porch %u vsync_pulse_width %u\n",
186 timing->v_front_porch, timing->v_back_porch,
187 timing->vsync_pulse_width);
188 DPU_DEBUG_VIDENC(phys_enc,
189 "wc_lines %u needed_vfp_lines %u actual_vfp_lines %u\n",
190 worst_case_needed_lines, needed_vfp_lines, actual_vfp_lines);
191
192 return actual_vfp_lines;
193 }
194
195 /*
196 * programmable_fetch_config: Programs HW to prefetch lines by offsetting
197 * the start of fetch into the vertical front porch for cases where the
198 * vsync pulse width and vertical back porch time is insufficient
199 *
200 * Gets # of lines to pre-fetch, then calculate VSYNC counter value.
201 * HW layer requires VSYNC counter of first pixel of tgt VFP line.
202 *
203 * @timing: Pointer to the intf timing information for the requested mode
204 */
programmable_fetch_config(struct dpu_encoder_phys * phys_enc,const struct dpu_hw_intf_timing_params * timing)205 static void programmable_fetch_config(struct dpu_encoder_phys *phys_enc,
206 const struct dpu_hw_intf_timing_params *timing)
207 {
208 struct dpu_hw_intf_prog_fetch f = { 0 };
209 u32 vfp_fetch_lines = 0;
210 u32 horiz_total = 0;
211 u32 vert_total = 0;
212 u32 vfp_fetch_start_vsync_counter = 0;
213 unsigned long lock_flags;
214
215 if (WARN_ON_ONCE(!phys_enc->hw_intf->ops.setup_prg_fetch))
216 return;
217
218 vfp_fetch_lines = programmable_fetch_get_num_lines(phys_enc, timing);
219 if (vfp_fetch_lines) {
220 vert_total = get_vertical_total(timing);
221 horiz_total = get_horizontal_total(timing);
222 vfp_fetch_start_vsync_counter =
223 (vert_total - vfp_fetch_lines) * horiz_total + 1;
224 f.enable = 1;
225 f.fetch_start = vfp_fetch_start_vsync_counter;
226 }
227
228 DPU_DEBUG_VIDENC(phys_enc,
229 "vfp_fetch_lines %u vfp_fetch_start_vsync_counter %u\n",
230 vfp_fetch_lines, vfp_fetch_start_vsync_counter);
231
232 spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
233 phys_enc->hw_intf->ops.setup_prg_fetch(phys_enc->hw_intf, &f);
234 spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
235 }
236
dpu_encoder_phys_vid_setup_timing_engine(struct dpu_encoder_phys * phys_enc)237 static void dpu_encoder_phys_vid_setup_timing_engine(
238 struct dpu_encoder_phys *phys_enc)
239 {
240 struct drm_display_mode mode;
241 struct dpu_hw_intf_timing_params timing_params = { 0 };
242 const struct dpu_format *fmt = NULL;
243 u32 fmt_fourcc = DRM_FORMAT_RGB888;
244 unsigned long lock_flags;
245 struct dpu_hw_intf_cfg intf_cfg = { 0 };
246
247 drm_mode_init(&mode, &phys_enc->cached_mode);
248
249 if (!phys_enc->hw_ctl->ops.setup_intf_cfg) {
250 DPU_ERROR("invalid encoder %d\n", phys_enc != NULL);
251 return;
252 }
253
254 if (!phys_enc->hw_intf->ops.setup_timing_gen) {
255 DPU_ERROR("timing engine setup is not supported\n");
256 return;
257 }
258
259 DPU_DEBUG_VIDENC(phys_enc, "enabling mode:\n");
260 drm_mode_debug_printmodeline(&mode);
261
262 if (phys_enc->split_role != ENC_ROLE_SOLO) {
263 mode.hdisplay >>= 1;
264 mode.htotal >>= 1;
265 mode.hsync_start >>= 1;
266 mode.hsync_end >>= 1;
267 mode.hskew >>= 1;
268
269 DPU_DEBUG_VIDENC(phys_enc,
270 "split_role %d, halve horizontal %d %d %d %d %d\n",
271 phys_enc->split_role,
272 mode.hdisplay, mode.htotal,
273 mode.hsync_start, mode.hsync_end,
274 mode.hskew);
275 }
276
277 drm_mode_to_intf_timing_params(phys_enc, &mode, &timing_params);
278
279 fmt = dpu_get_dpu_format(fmt_fourcc);
280 DPU_DEBUG_VIDENC(phys_enc, "fmt_fourcc 0x%X\n", fmt_fourcc);
281
282 intf_cfg.intf = phys_enc->hw_intf->idx;
283 intf_cfg.intf_mode_sel = DPU_CTL_MODE_SEL_VID;
284 intf_cfg.stream_sel = 0; /* Don't care value for video mode */
285 intf_cfg.mode_3d = dpu_encoder_helper_get_3d_blend_mode(phys_enc);
286 intf_cfg.dsc = dpu_encoder_helper_get_dsc(phys_enc);
287 if (intf_cfg.mode_3d && phys_enc->hw_pp->merge_3d)
288 intf_cfg.merge_3d = phys_enc->hw_pp->merge_3d->idx;
289
290 spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
291 phys_enc->hw_intf->ops.setup_timing_gen(phys_enc->hw_intf,
292 &timing_params, fmt);
293 phys_enc->hw_ctl->ops.setup_intf_cfg(phys_enc->hw_ctl, &intf_cfg);
294
295 /* setup which pp blk will connect to this intf */
296 if (phys_enc->hw_intf->ops.bind_pingpong_blk)
297 phys_enc->hw_intf->ops.bind_pingpong_blk(
298 phys_enc->hw_intf,
299 phys_enc->hw_pp->idx);
300
301 if (phys_enc->hw_pp->merge_3d)
302 phys_enc->hw_pp->merge_3d->ops.setup_3d_mode(phys_enc->hw_pp->merge_3d, intf_cfg.mode_3d);
303
304 spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
305
306 programmable_fetch_config(phys_enc, &timing_params);
307 }
308
dpu_encoder_phys_vid_vblank_irq(void * arg)309 static void dpu_encoder_phys_vid_vblank_irq(void *arg)
310 {
311 struct dpu_encoder_phys *phys_enc = arg;
312 struct dpu_hw_ctl *hw_ctl;
313 unsigned long lock_flags;
314 u32 flush_register = 0;
315
316 hw_ctl = phys_enc->hw_ctl;
317
318 DPU_ATRACE_BEGIN("vblank_irq");
319
320 dpu_encoder_vblank_callback(phys_enc->parent, phys_enc);
321
322 atomic_read(&phys_enc->pending_kickoff_cnt);
323
324 /*
325 * only decrement the pending flush count if we've actually flushed
326 * hardware. due to sw irq latency, vblank may have already happened
327 * so we need to double-check with hw that it accepted the flush bits
328 */
329 spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
330 if (hw_ctl->ops.get_flush_register)
331 flush_register = hw_ctl->ops.get_flush_register(hw_ctl);
332
333 if (!(flush_register & hw_ctl->ops.get_pending_flush(hw_ctl)))
334 atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0);
335 spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
336
337 /* Signal any waiting atomic commit thread */
338 wake_up_all(&phys_enc->pending_kickoff_wq);
339
340 dpu_encoder_frame_done_callback(phys_enc->parent, phys_enc,
341 DPU_ENCODER_FRAME_EVENT_DONE);
342
343 DPU_ATRACE_END("vblank_irq");
344 }
345
dpu_encoder_phys_vid_underrun_irq(void * arg)346 static void dpu_encoder_phys_vid_underrun_irq(void *arg)
347 {
348 struct dpu_encoder_phys *phys_enc = arg;
349
350 dpu_encoder_underrun_callback(phys_enc->parent, phys_enc);
351 }
352
dpu_encoder_phys_vid_needs_single_flush(struct dpu_encoder_phys * phys_enc)353 static bool dpu_encoder_phys_vid_needs_single_flush(
354 struct dpu_encoder_phys *phys_enc)
355 {
356 return phys_enc->split_role != ENC_ROLE_SOLO;
357 }
358
dpu_encoder_phys_vid_atomic_mode_set(struct dpu_encoder_phys * phys_enc,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)359 static void dpu_encoder_phys_vid_atomic_mode_set(
360 struct dpu_encoder_phys *phys_enc,
361 struct drm_crtc_state *crtc_state,
362 struct drm_connector_state *conn_state)
363 {
364 phys_enc->irq[INTR_IDX_VSYNC] = phys_enc->hw_intf->cap->intr_vsync;
365
366 phys_enc->irq[INTR_IDX_UNDERRUN] = phys_enc->hw_intf->cap->intr_underrun;
367 }
368
dpu_encoder_phys_vid_control_vblank_irq(struct dpu_encoder_phys * phys_enc,bool enable)369 static int dpu_encoder_phys_vid_control_vblank_irq(
370 struct dpu_encoder_phys *phys_enc,
371 bool enable)
372 {
373 int ret = 0;
374 int refcount;
375
376 refcount = atomic_read(&phys_enc->vblank_refcount);
377
378 /* Slave encoders don't report vblank */
379 if (!dpu_encoder_phys_vid_is_master(phys_enc))
380 goto end;
381
382 /* protect against negative */
383 if (!enable && refcount == 0) {
384 ret = -EINVAL;
385 goto end;
386 }
387
388 DRM_DEBUG_VBL("id:%u enable=%d/%d\n", DRMID(phys_enc->parent), enable,
389 atomic_read(&phys_enc->vblank_refcount));
390
391 if (enable && atomic_inc_return(&phys_enc->vblank_refcount) == 1)
392 ret = dpu_core_irq_register_callback(phys_enc->dpu_kms,
393 phys_enc->irq[INTR_IDX_VSYNC],
394 dpu_encoder_phys_vid_vblank_irq,
395 phys_enc);
396 else if (!enable && atomic_dec_return(&phys_enc->vblank_refcount) == 0)
397 ret = dpu_core_irq_unregister_callback(phys_enc->dpu_kms,
398 phys_enc->irq[INTR_IDX_VSYNC]);
399
400 end:
401 if (ret) {
402 DRM_ERROR("failed: id:%u intf:%d ret:%d enable:%d refcnt:%d\n",
403 DRMID(phys_enc->parent),
404 phys_enc->hw_intf->idx - INTF_0, ret, enable,
405 refcount);
406 }
407 return ret;
408 }
409
dpu_encoder_phys_vid_enable(struct dpu_encoder_phys * phys_enc)410 static void dpu_encoder_phys_vid_enable(struct dpu_encoder_phys *phys_enc)
411 {
412 struct dpu_hw_ctl *ctl;
413
414 ctl = phys_enc->hw_ctl;
415
416 DPU_DEBUG_VIDENC(phys_enc, "\n");
417
418 if (WARN_ON(!phys_enc->hw_intf->ops.enable_timing))
419 return;
420
421 dpu_encoder_helper_split_config(phys_enc, phys_enc->hw_intf->idx);
422
423 dpu_encoder_phys_vid_setup_timing_engine(phys_enc);
424
425 /*
426 * For single flush cases (dual-ctl or pp-split), skip setting the
427 * flush bit for the slave intf, since both intfs use same ctl
428 * and HW will only flush the master.
429 */
430 if (dpu_encoder_phys_vid_needs_single_flush(phys_enc) &&
431 !dpu_encoder_phys_vid_is_master(phys_enc))
432 goto skip_flush;
433
434 ctl->ops.update_pending_flush_intf(ctl, phys_enc->hw_intf->idx);
435 if (ctl->ops.update_pending_flush_merge_3d && phys_enc->hw_pp->merge_3d)
436 ctl->ops.update_pending_flush_merge_3d(ctl, phys_enc->hw_pp->merge_3d->idx);
437
438 skip_flush:
439 DPU_DEBUG_VIDENC(phys_enc,
440 "update pending flush ctl %d intf %d\n",
441 ctl->idx - CTL_0, phys_enc->hw_intf->idx);
442
443 atomic_set(&phys_enc->underrun_cnt, 0);
444
445 /* ctl_flush & timing engine enable will be triggered by framework */
446 if (phys_enc->enable_state == DPU_ENC_DISABLED)
447 phys_enc->enable_state = DPU_ENC_ENABLING;
448 }
449
dpu_encoder_phys_vid_wait_for_tx_complete(struct dpu_encoder_phys * phys_enc)450 static int dpu_encoder_phys_vid_wait_for_tx_complete(
451 struct dpu_encoder_phys *phys_enc)
452 {
453 struct dpu_encoder_wait_info wait_info;
454 int ret;
455
456 wait_info.wq = &phys_enc->pending_kickoff_wq;
457 wait_info.atomic_cnt = &phys_enc->pending_kickoff_cnt;
458 wait_info.timeout_ms = KICKOFF_TIMEOUT_MS;
459
460 if (!dpu_encoder_phys_vid_is_master(phys_enc)) {
461 return 0;
462 }
463
464 /* Wait for kickoff to complete */
465 ret = dpu_encoder_helper_wait_for_irq(phys_enc,
466 phys_enc->irq[INTR_IDX_VSYNC],
467 dpu_encoder_phys_vid_vblank_irq,
468 &wait_info);
469
470 if (ret == -ETIMEDOUT) {
471 dpu_encoder_helper_report_irq_timeout(phys_enc, INTR_IDX_VSYNC);
472 }
473
474 return ret;
475 }
476
dpu_encoder_phys_vid_wait_for_commit_done(struct dpu_encoder_phys * phys_enc)477 static int dpu_encoder_phys_vid_wait_for_commit_done(
478 struct dpu_encoder_phys *phys_enc)
479 {
480 struct dpu_hw_ctl *hw_ctl = phys_enc->hw_ctl;
481 int ret;
482
483 if (!hw_ctl)
484 return 0;
485
486 ret = wait_event_timeout(phys_enc->pending_kickoff_wq,
487 (hw_ctl->ops.get_flush_register(hw_ctl) == 0),
488 msecs_to_jiffies(50));
489 if (ret <= 0) {
490 DPU_ERROR("vblank timeout\n");
491 return -ETIMEDOUT;
492 }
493
494 return 0;
495 }
496
dpu_encoder_phys_vid_prepare_for_kickoff(struct dpu_encoder_phys * phys_enc)497 static void dpu_encoder_phys_vid_prepare_for_kickoff(
498 struct dpu_encoder_phys *phys_enc)
499 {
500 struct dpu_hw_ctl *ctl;
501 int rc;
502 struct drm_encoder *drm_enc;
503
504 drm_enc = phys_enc->parent;
505
506 ctl = phys_enc->hw_ctl;
507 if (!ctl->ops.wait_reset_status)
508 return;
509
510 /*
511 * hw supports hardware initiated ctl reset, so before we kickoff a new
512 * frame, need to check and wait for hw initiated ctl reset completion
513 */
514 rc = ctl->ops.wait_reset_status(ctl);
515 if (rc) {
516 DPU_ERROR_VIDENC(phys_enc, "ctl %d reset failure: %d\n",
517 ctl->idx, rc);
518 msm_disp_snapshot_state(drm_enc->dev);
519 dpu_core_irq_unregister_callback(phys_enc->dpu_kms,
520 phys_enc->irq[INTR_IDX_VSYNC]);
521 }
522 }
523
dpu_encoder_phys_vid_disable(struct dpu_encoder_phys * phys_enc)524 static void dpu_encoder_phys_vid_disable(struct dpu_encoder_phys *phys_enc)
525 {
526 unsigned long lock_flags;
527 int ret;
528 struct dpu_hw_intf_status intf_status = {0};
529
530 if (!phys_enc->parent || !phys_enc->parent->dev) {
531 DPU_ERROR("invalid encoder/device\n");
532 return;
533 }
534
535 if (!phys_enc->hw_intf) {
536 DPU_ERROR("invalid hw_intf %d hw_ctl %d\n",
537 phys_enc->hw_intf != NULL, phys_enc->hw_ctl != NULL);
538 return;
539 }
540
541 if (WARN_ON(!phys_enc->hw_intf->ops.enable_timing))
542 return;
543
544 if (phys_enc->enable_state == DPU_ENC_DISABLED) {
545 DPU_ERROR("already disabled\n");
546 return;
547 }
548
549 spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
550 phys_enc->hw_intf->ops.enable_timing(phys_enc->hw_intf, 0);
551 if (dpu_encoder_phys_vid_is_master(phys_enc))
552 dpu_encoder_phys_inc_pending(phys_enc);
553 spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
554
555 /*
556 * Wait for a vsync so we know the ENABLE=0 latched before
557 * the (connector) source of the vsync's gets disabled,
558 * otherwise we end up in a funny state if we re-enable
559 * before the disable latches, which results that some of
560 * the settings changes for the new modeset (like new
561 * scanout buffer) don't latch properly..
562 */
563 if (dpu_encoder_phys_vid_is_master(phys_enc)) {
564 ret = dpu_encoder_phys_vid_wait_for_tx_complete(phys_enc);
565 if (ret) {
566 atomic_set(&phys_enc->pending_kickoff_cnt, 0);
567 DRM_ERROR("wait disable failed: id:%u intf:%d ret:%d\n",
568 DRMID(phys_enc->parent),
569 phys_enc->hw_intf->idx - INTF_0, ret);
570 }
571 }
572
573 if (phys_enc->hw_intf && phys_enc->hw_intf->ops.get_status)
574 phys_enc->hw_intf->ops.get_status(phys_enc->hw_intf, &intf_status);
575
576 /*
577 * Wait for a vsync if timing en status is on after timing engine
578 * is disabled.
579 */
580 if (intf_status.is_en && dpu_encoder_phys_vid_is_master(phys_enc)) {
581 spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
582 dpu_encoder_phys_inc_pending(phys_enc);
583 spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
584 ret = dpu_encoder_phys_vid_wait_for_tx_complete(phys_enc);
585 if (ret) {
586 atomic_set(&phys_enc->pending_kickoff_cnt, 0);
587 DRM_ERROR("wait disable failed: id:%u intf:%d ret:%d\n",
588 DRMID(phys_enc->parent),
589 phys_enc->hw_intf->idx - INTF_0, ret);
590 }
591 }
592
593 dpu_encoder_helper_phys_cleanup(phys_enc);
594 phys_enc->enable_state = DPU_ENC_DISABLED;
595 }
596
dpu_encoder_phys_vid_handle_post_kickoff(struct dpu_encoder_phys * phys_enc)597 static void dpu_encoder_phys_vid_handle_post_kickoff(
598 struct dpu_encoder_phys *phys_enc)
599 {
600 unsigned long lock_flags;
601
602 /*
603 * Video mode must flush CTL before enabling timing engine
604 * Video encoders need to turn on their interfaces now
605 */
606 if (phys_enc->enable_state == DPU_ENC_ENABLING) {
607 trace_dpu_enc_phys_vid_post_kickoff(DRMID(phys_enc->parent),
608 phys_enc->hw_intf->idx - INTF_0);
609 spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
610 phys_enc->hw_intf->ops.enable_timing(phys_enc->hw_intf, 1);
611 spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
612 phys_enc->enable_state = DPU_ENC_ENABLED;
613 }
614 }
615
dpu_encoder_phys_vid_irq_control(struct dpu_encoder_phys * phys_enc,bool enable)616 static void dpu_encoder_phys_vid_irq_control(struct dpu_encoder_phys *phys_enc,
617 bool enable)
618 {
619 int ret;
620
621 trace_dpu_enc_phys_vid_irq_ctrl(DRMID(phys_enc->parent),
622 phys_enc->hw_intf->idx - INTF_0,
623 enable,
624 atomic_read(&phys_enc->vblank_refcount));
625
626 if (enable) {
627 ret = dpu_encoder_phys_vid_control_vblank_irq(phys_enc, true);
628 if (WARN_ON(ret))
629 return;
630
631 dpu_core_irq_register_callback(phys_enc->dpu_kms,
632 phys_enc->irq[INTR_IDX_UNDERRUN],
633 dpu_encoder_phys_vid_underrun_irq,
634 phys_enc);
635 } else {
636 dpu_encoder_phys_vid_control_vblank_irq(phys_enc, false);
637 dpu_core_irq_unregister_callback(phys_enc->dpu_kms,
638 phys_enc->irq[INTR_IDX_UNDERRUN]);
639 }
640 }
641
dpu_encoder_phys_vid_get_line_count(struct dpu_encoder_phys * phys_enc)642 static int dpu_encoder_phys_vid_get_line_count(
643 struct dpu_encoder_phys *phys_enc)
644 {
645 if (!dpu_encoder_phys_vid_is_master(phys_enc))
646 return -EINVAL;
647
648 if (!phys_enc->hw_intf || !phys_enc->hw_intf->ops.get_line_count)
649 return -EINVAL;
650
651 return phys_enc->hw_intf->ops.get_line_count(phys_enc->hw_intf);
652 }
653
dpu_encoder_phys_vid_get_frame_count(struct dpu_encoder_phys * phys_enc)654 static int dpu_encoder_phys_vid_get_frame_count(
655 struct dpu_encoder_phys *phys_enc)
656 {
657 struct dpu_hw_intf_status s = {0};
658 u32 fetch_start = 0;
659 struct drm_display_mode mode;
660
661 drm_mode_init(&mode, &phys_enc->cached_mode);
662
663 if (!dpu_encoder_phys_vid_is_master(phys_enc))
664 return -EINVAL;
665
666 if (!phys_enc->hw_intf || !phys_enc->hw_intf->ops.get_status)
667 return -EINVAL;
668
669 phys_enc->hw_intf->ops.get_status(phys_enc->hw_intf, &s);
670
671 if (s.is_prog_fetch_en && s.is_en) {
672 fetch_start = mode.vtotal - (mode.vsync_start - mode.vdisplay);
673 if ((s.line_count > fetch_start) &&
674 (s.line_count <= mode.vtotal))
675 return s.frame_count + 1;
676 }
677
678 return s.frame_count;
679 }
680
dpu_encoder_phys_vid_init_ops(struct dpu_encoder_phys_ops * ops)681 static void dpu_encoder_phys_vid_init_ops(struct dpu_encoder_phys_ops *ops)
682 {
683 ops->is_master = dpu_encoder_phys_vid_is_master;
684 ops->atomic_mode_set = dpu_encoder_phys_vid_atomic_mode_set;
685 ops->enable = dpu_encoder_phys_vid_enable;
686 ops->disable = dpu_encoder_phys_vid_disable;
687 ops->control_vblank_irq = dpu_encoder_phys_vid_control_vblank_irq;
688 ops->wait_for_commit_done = dpu_encoder_phys_vid_wait_for_commit_done;
689 ops->wait_for_tx_complete = dpu_encoder_phys_vid_wait_for_tx_complete;
690 ops->irq_control = dpu_encoder_phys_vid_irq_control;
691 ops->prepare_for_kickoff = dpu_encoder_phys_vid_prepare_for_kickoff;
692 ops->handle_post_kickoff = dpu_encoder_phys_vid_handle_post_kickoff;
693 ops->needs_single_flush = dpu_encoder_phys_vid_needs_single_flush;
694 ops->get_line_count = dpu_encoder_phys_vid_get_line_count;
695 ops->get_frame_count = dpu_encoder_phys_vid_get_frame_count;
696 }
697
dpu_encoder_phys_vid_init(struct drm_device * dev,struct dpu_enc_phys_init_params * p)698 struct dpu_encoder_phys *dpu_encoder_phys_vid_init(struct drm_device *dev,
699 struct dpu_enc_phys_init_params *p)
700 {
701 struct dpu_encoder_phys *phys_enc = NULL;
702
703 if (!p) {
704 DPU_ERROR("failed to create encoder due to invalid parameter\n");
705 return ERR_PTR(-EINVAL);
706 }
707
708 phys_enc = drmm_kzalloc(dev, sizeof(*phys_enc), GFP_KERNEL);
709 if (!phys_enc) {
710 DPU_ERROR("failed to create encoder due to memory allocation error\n");
711 return ERR_PTR(-ENOMEM);
712 }
713
714 DPU_DEBUG_VIDENC(phys_enc, "\n");
715
716 dpu_encoder_phys_init(phys_enc, p);
717
718 dpu_encoder_phys_vid_init_ops(&phys_enc->ops);
719 phys_enc->intf_mode = INTF_MODE_VIDEO;
720
721 DPU_DEBUG_VIDENC(phys_enc, "created intf idx:%d\n", p->hw_intf->idx);
722
723 return phys_enc;
724 }
725