2 * Copyright (c) 2015-2018 The Linux Foundation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
16 #include "dpu_encoder_phys.h"
17 #include "dpu_hw_interrupts.h"
18 #include "dpu_core_irq.h"
19 #include "dpu_formats.h"
20 #include "dpu_trace.h"
22 #define DPU_DEBUG_CMDENC(e, fmt, ...) DPU_DEBUG("enc%d intf%d " fmt, \
23 (e) && (e)->base.parent ? \
24 (e)->base.parent->base.id : -1, \
25 (e) ? (e)->base.intf_idx - INTF_0 : -1, ##__VA_ARGS__)
27 #define DPU_ERROR_CMDENC(e, fmt, ...) DPU_ERROR("enc%d intf%d " fmt, \
28 (e) && (e)->base.parent ? \
29 (e)->base.parent->base.id : -1, \
30 (e) ? (e)->base.intf_idx - INTF_0 : -1, ##__VA_ARGS__)
32 #define to_dpu_encoder_phys_cmd(x) \
33 container_of(x, struct dpu_encoder_phys_cmd, base)
35 #define PP_TIMEOUT_MAX_TRIALS 10
38 * Tearcheck sync start and continue thresholds are empirically found
39 * based on common panels In the future, may want to allow panels to override
40 * these default values
42 #define DEFAULT_TEARCHECK_SYNC_THRESH_START 4
43 #define DEFAULT_TEARCHECK_SYNC_THRESH_CONTINUE 4
45 #define DPU_ENC_WR_PTR_START_TIMEOUT_US 20000
47 static inline int _dpu_encoder_phys_cmd_get_idle_timeout(
48 struct dpu_encoder_phys_cmd *cmd_enc)
50 return KICKOFF_TIMEOUT_MS;
53 static inline bool dpu_encoder_phys_cmd_is_master(
54 struct dpu_encoder_phys *phys_enc)
56 return (phys_enc->split_role != ENC_ROLE_SLAVE) ? true : false;
59 static bool dpu_encoder_phys_cmd_mode_fixup(
60 struct dpu_encoder_phys *phys_enc,
61 const struct drm_display_mode *mode,
62 struct drm_display_mode *adj_mode)
65 DPU_DEBUG_CMDENC(to_dpu_encoder_phys_cmd(phys_enc), "\n");
69 static void _dpu_encoder_phys_cmd_update_intf_cfg(
70 struct dpu_encoder_phys *phys_enc)
72 struct dpu_encoder_phys_cmd *cmd_enc =
73 to_dpu_encoder_phys_cmd(phys_enc);
74 struct dpu_hw_ctl *ctl;
75 struct dpu_hw_intf_cfg intf_cfg = { 0 };
80 ctl = phys_enc->hw_ctl;
81 if (!ctl || !ctl->ops.setup_intf_cfg)
84 intf_cfg.intf = phys_enc->intf_idx;
85 intf_cfg.intf_mode_sel = DPU_CTL_MODE_SEL_CMD;
86 intf_cfg.stream_sel = cmd_enc->stream_sel;
87 intf_cfg.mode_3d = dpu_encoder_helper_get_3d_blend_mode(phys_enc);
88 ctl->ops.setup_intf_cfg(ctl, &intf_cfg);
91 static void dpu_encoder_phys_cmd_pp_tx_done_irq(void *arg, int irq_idx)
93 struct dpu_encoder_phys *phys_enc = arg;
94 unsigned long lock_flags;
96 u32 event = DPU_ENCODER_FRAME_EVENT_DONE;
98 if (!phys_enc || !phys_enc->hw_pp)
101 DPU_ATRACE_BEGIN("pp_done_irq");
102 /* notify all synchronous clients first, then asynchronous clients */
103 if (phys_enc->parent_ops->handle_frame_done)
104 phys_enc->parent_ops->handle_frame_done(phys_enc->parent,
107 spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
108 new_cnt = atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0);
109 spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
111 trace_dpu_enc_phys_cmd_pp_tx_done(DRMID(phys_enc->parent),
112 phys_enc->hw_pp->idx - PINGPONG_0,
115 /* Signal any waiting atomic commit thread */
116 wake_up_all(&phys_enc->pending_kickoff_wq);
117 DPU_ATRACE_END("pp_done_irq");
120 static void dpu_encoder_phys_cmd_pp_rd_ptr_irq(void *arg, int irq_idx)
122 struct dpu_encoder_phys *phys_enc = arg;
123 struct dpu_encoder_phys_cmd *cmd_enc;
125 if (!phys_enc || !phys_enc->hw_pp)
128 DPU_ATRACE_BEGIN("rd_ptr_irq");
129 cmd_enc = to_dpu_encoder_phys_cmd(phys_enc);
131 if (phys_enc->parent_ops->handle_vblank_virt)
132 phys_enc->parent_ops->handle_vblank_virt(phys_enc->parent,
135 atomic_add_unless(&cmd_enc->pending_vblank_cnt, -1, 0);
136 wake_up_all(&cmd_enc->pending_vblank_wq);
137 DPU_ATRACE_END("rd_ptr_irq");
140 static void dpu_encoder_phys_cmd_ctl_start_irq(void *arg, int irq_idx)
142 struct dpu_encoder_phys *phys_enc = arg;
143 struct dpu_encoder_phys_cmd *cmd_enc;
145 if (!phys_enc || !phys_enc->hw_ctl)
148 DPU_ATRACE_BEGIN("ctl_start_irq");
149 cmd_enc = to_dpu_encoder_phys_cmd(phys_enc);
151 atomic_add_unless(&phys_enc->pending_ctlstart_cnt, -1, 0);
153 /* Signal any waiting ctl start interrupt */
154 wake_up_all(&phys_enc->pending_kickoff_wq);
155 DPU_ATRACE_END("ctl_start_irq");
158 static void dpu_encoder_phys_cmd_underrun_irq(void *arg, int irq_idx)
160 struct dpu_encoder_phys *phys_enc = arg;
165 if (phys_enc->parent_ops->handle_underrun_virt)
166 phys_enc->parent_ops->handle_underrun_virt(phys_enc->parent,
170 static void _dpu_encoder_phys_cmd_setup_irq_hw_idx(
171 struct dpu_encoder_phys *phys_enc)
173 struct dpu_encoder_irq *irq;
175 irq = &phys_enc->irq[INTR_IDX_CTL_START];
176 irq->hw_idx = phys_enc->hw_ctl->idx;
177 irq->irq_idx = -EINVAL;
179 irq = &phys_enc->irq[INTR_IDX_PINGPONG];
180 irq->hw_idx = phys_enc->hw_pp->idx;
181 irq->irq_idx = -EINVAL;
183 irq = &phys_enc->irq[INTR_IDX_RDPTR];
184 irq->hw_idx = phys_enc->hw_pp->idx;
185 irq->irq_idx = -EINVAL;
187 irq = &phys_enc->irq[INTR_IDX_UNDERRUN];
188 irq->hw_idx = phys_enc->intf_idx;
189 irq->irq_idx = -EINVAL;
192 static void dpu_encoder_phys_cmd_mode_set(
193 struct dpu_encoder_phys *phys_enc,
194 struct drm_display_mode *mode,
195 struct drm_display_mode *adj_mode)
197 struct dpu_encoder_phys_cmd *cmd_enc =
198 to_dpu_encoder_phys_cmd(phys_enc);
199 struct dpu_rm *rm = &phys_enc->dpu_kms->rm;
200 struct dpu_rm_hw_iter iter;
203 if (!phys_enc || !mode || !adj_mode) {
204 DPU_ERROR("invalid args\n");
207 phys_enc->cached_mode = *adj_mode;
208 DPU_DEBUG_CMDENC(cmd_enc, "caching mode:\n");
209 drm_mode_debug_printmodeline(adj_mode);
211 instance = phys_enc->split_role == ENC_ROLE_SLAVE ? 1 : 0;
213 /* Retrieve previously allocated HW Resources. Shouldn't fail */
214 dpu_rm_init_hw_iter(&iter, phys_enc->parent->base.id, DPU_HW_BLK_CTL);
215 for (i = 0; i <= instance; i++) {
216 if (dpu_rm_get_hw(rm, &iter))
217 phys_enc->hw_ctl = (struct dpu_hw_ctl *)iter.hw;
220 if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
221 DPU_ERROR_CMDENC(cmd_enc, "failed to init ctl: %ld\n",
222 PTR_ERR(phys_enc->hw_ctl));
223 phys_enc->hw_ctl = NULL;
227 _dpu_encoder_phys_cmd_setup_irq_hw_idx(phys_enc);
230 static int _dpu_encoder_phys_cmd_handle_ppdone_timeout(
231 struct dpu_encoder_phys *phys_enc)
233 struct dpu_encoder_phys_cmd *cmd_enc =
234 to_dpu_encoder_phys_cmd(phys_enc);
235 u32 frame_event = DPU_ENCODER_FRAME_EVENT_ERROR;
238 if (!phys_enc || !phys_enc->hw_pp || !phys_enc->hw_ctl)
241 cmd_enc->pp_timeout_report_cnt++;
242 if (cmd_enc->pp_timeout_report_cnt == PP_TIMEOUT_MAX_TRIALS) {
243 frame_event |= DPU_ENCODER_FRAME_EVENT_PANEL_DEAD;
245 } else if (cmd_enc->pp_timeout_report_cnt == 1) {
249 trace_dpu_enc_phys_cmd_pdone_timeout(DRMID(phys_enc->parent),
250 phys_enc->hw_pp->idx - PINGPONG_0,
251 cmd_enc->pp_timeout_report_cnt,
252 atomic_read(&phys_enc->pending_kickoff_cnt),
255 /* to avoid flooding, only log first time, and "dead" time */
257 DRM_ERROR("id:%d pp:%d kickoff timeout %d cnt %d koff_cnt %d\n",
258 DRMID(phys_enc->parent),
259 phys_enc->hw_pp->idx - PINGPONG_0,
260 phys_enc->hw_ctl->idx - CTL_0,
261 cmd_enc->pp_timeout_report_cnt,
262 atomic_read(&phys_enc->pending_kickoff_cnt));
264 dpu_encoder_helper_unregister_irq(phys_enc, INTR_IDX_RDPTR);
265 dpu_dbg_dump(false, __func__, true, true);
268 atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0);
270 /* request a ctl reset before the next kickoff */
271 phys_enc->enable_state = DPU_ENC_ERR_NEEDS_HW_RESET;
273 if (phys_enc->parent_ops->handle_frame_done)
274 phys_enc->parent_ops->handle_frame_done(
275 phys_enc->parent, phys_enc, frame_event);
280 static int _dpu_encoder_phys_cmd_wait_for_idle(
281 struct dpu_encoder_phys *phys_enc)
283 struct dpu_encoder_phys_cmd *cmd_enc =
284 to_dpu_encoder_phys_cmd(phys_enc);
285 struct dpu_encoder_wait_info wait_info;
289 DPU_ERROR("invalid encoder\n");
293 wait_info.wq = &phys_enc->pending_kickoff_wq;
294 wait_info.atomic_cnt = &phys_enc->pending_kickoff_cnt;
295 wait_info.timeout_ms = KICKOFF_TIMEOUT_MS;
297 ret = dpu_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_PINGPONG,
299 if (ret == -ETIMEDOUT)
300 _dpu_encoder_phys_cmd_handle_ppdone_timeout(phys_enc);
302 cmd_enc->pp_timeout_report_cnt = 0;
307 static int dpu_encoder_phys_cmd_control_vblank_irq(
308 struct dpu_encoder_phys *phys_enc,
314 if (!phys_enc || !phys_enc->hw_pp) {
315 DPU_ERROR("invalid encoder\n");
319 refcount = atomic_read(&phys_enc->vblank_refcount);
321 /* Slave encoders don't report vblank */
322 if (!dpu_encoder_phys_cmd_is_master(phys_enc))
325 /* protect against negative */
326 if (!enable && refcount == 0) {
331 DRM_DEBUG_KMS("id:%u pp:%d enable=%s/%d\n", DRMID(phys_enc->parent),
332 phys_enc->hw_pp->idx - PINGPONG_0,
333 enable ? "true" : "false", refcount);
335 if (enable && atomic_inc_return(&phys_enc->vblank_refcount) == 1)
336 ret = dpu_encoder_helper_register_irq(phys_enc, INTR_IDX_RDPTR);
337 else if (!enable && atomic_dec_return(&phys_enc->vblank_refcount) == 0)
338 ret = dpu_encoder_helper_unregister_irq(phys_enc,
343 DRM_ERROR("vblank irq err id:%u pp:%d ret:%d, enable %s/%d\n",
344 DRMID(phys_enc->parent),
345 phys_enc->hw_pp->idx - PINGPONG_0, ret,
346 enable ? "true" : "false", refcount);
352 static void dpu_encoder_phys_cmd_irq_control(struct dpu_encoder_phys *phys_enc,
355 struct dpu_encoder_phys_cmd *cmd_enc;
360 cmd_enc = to_dpu_encoder_phys_cmd(phys_enc);
362 trace_dpu_enc_phys_cmd_irq_ctrl(DRMID(phys_enc->parent),
363 phys_enc->hw_pp->idx - PINGPONG_0,
364 enable, atomic_read(&phys_enc->vblank_refcount));
367 dpu_encoder_helper_register_irq(phys_enc, INTR_IDX_PINGPONG);
368 dpu_encoder_helper_register_irq(phys_enc, INTR_IDX_UNDERRUN);
369 dpu_encoder_phys_cmd_control_vblank_irq(phys_enc, true);
371 if (dpu_encoder_phys_cmd_is_master(phys_enc))
372 dpu_encoder_helper_register_irq(phys_enc,
375 if (dpu_encoder_phys_cmd_is_master(phys_enc))
376 dpu_encoder_helper_unregister_irq(phys_enc,
379 dpu_encoder_helper_unregister_irq(phys_enc, INTR_IDX_UNDERRUN);
380 dpu_encoder_phys_cmd_control_vblank_irq(phys_enc, false);
381 dpu_encoder_helper_unregister_irq(phys_enc, INTR_IDX_PINGPONG);
385 static void dpu_encoder_phys_cmd_tearcheck_config(
386 struct dpu_encoder_phys *phys_enc)
388 struct dpu_encoder_phys_cmd *cmd_enc =
389 to_dpu_encoder_phys_cmd(phys_enc);
390 struct dpu_hw_tear_check tc_cfg = { 0 };
391 struct drm_display_mode *mode;
392 bool tc_enable = true;
394 struct msm_drm_private *priv;
395 struct dpu_kms *dpu_kms;
397 if (!phys_enc || !phys_enc->hw_pp) {
398 DPU_ERROR("invalid encoder\n");
401 mode = &phys_enc->cached_mode;
403 DPU_DEBUG_CMDENC(cmd_enc, "pp %d\n", phys_enc->hw_pp->idx - PINGPONG_0);
405 if (!phys_enc->hw_pp->ops.setup_tearcheck ||
406 !phys_enc->hw_pp->ops.enable_tearcheck) {
407 DPU_DEBUG_CMDENC(cmd_enc, "tearcheck not supported\n");
411 dpu_kms = phys_enc->dpu_kms;
412 if (!dpu_kms || !dpu_kms->dev || !dpu_kms->dev->dev_private) {
413 DPU_ERROR("invalid device\n");
416 priv = dpu_kms->dev->dev_private;
419 * TE default: dsi byte clock calculated base on 70 fps;
420 * around 14 ms to complete a kickoff cycle if te disabled;
421 * vclk_line base on 60 fps; write is faster than read;
422 * init == start == rdptr;
424 * vsync_count is ratio of MDP VSYNC clock frequency to LCD panel
425 * frequency divided by the no. of rows (lines) in the LCDpanel.
427 vsync_hz = dpu_kms_get_clk_rate(dpu_kms, "vsync");
429 DPU_DEBUG_CMDENC(cmd_enc, "invalid - vsync_hz %u\n",
434 tc_cfg.vsync_count = vsync_hz / (mode->vtotal * mode->vrefresh);
436 /* enable external TE after kickoff to avoid premature autorefresh */
437 tc_cfg.hw_vsync_mode = 0;
440 * By setting sync_cfg_height to near max register value, we essentially
441 * disable dpu hw generated TE signal, since hw TE will arrive first.
442 * Only caveat is if due to error, we hit wrap-around.
444 tc_cfg.sync_cfg_height = 0xFFF0;
445 tc_cfg.vsync_init_val = mode->vdisplay;
446 tc_cfg.sync_threshold_start = DEFAULT_TEARCHECK_SYNC_THRESH_START;
447 tc_cfg.sync_threshold_continue = DEFAULT_TEARCHECK_SYNC_THRESH_CONTINUE;
448 tc_cfg.start_pos = mode->vdisplay;
449 tc_cfg.rd_ptr_irq = mode->vdisplay + 1;
451 DPU_DEBUG_CMDENC(cmd_enc,
452 "tc %d vsync_clk_speed_hz %u vtotal %u vrefresh %u\n",
453 phys_enc->hw_pp->idx - PINGPONG_0, vsync_hz,
454 mode->vtotal, mode->vrefresh);
455 DPU_DEBUG_CMDENC(cmd_enc,
456 "tc %d enable %u start_pos %u rd_ptr_irq %u\n",
457 phys_enc->hw_pp->idx - PINGPONG_0, tc_enable, tc_cfg.start_pos,
459 DPU_DEBUG_CMDENC(cmd_enc,
460 "tc %d hw_vsync_mode %u vsync_count %u vsync_init_val %u\n",
461 phys_enc->hw_pp->idx - PINGPONG_0, tc_cfg.hw_vsync_mode,
462 tc_cfg.vsync_count, tc_cfg.vsync_init_val);
463 DPU_DEBUG_CMDENC(cmd_enc,
464 "tc %d cfgheight %u thresh_start %u thresh_cont %u\n",
465 phys_enc->hw_pp->idx - PINGPONG_0, tc_cfg.sync_cfg_height,
466 tc_cfg.sync_threshold_start, tc_cfg.sync_threshold_continue);
468 phys_enc->hw_pp->ops.setup_tearcheck(phys_enc->hw_pp, &tc_cfg);
469 phys_enc->hw_pp->ops.enable_tearcheck(phys_enc->hw_pp, tc_enable);
472 static void _dpu_encoder_phys_cmd_pingpong_config(
473 struct dpu_encoder_phys *phys_enc)
475 struct dpu_encoder_phys_cmd *cmd_enc =
476 to_dpu_encoder_phys_cmd(phys_enc);
478 if (!phys_enc || !phys_enc->hw_ctl || !phys_enc->hw_pp
479 || !phys_enc->hw_ctl->ops.setup_intf_cfg) {
480 DPU_ERROR("invalid arg(s), enc %d\n", phys_enc != 0);
484 DPU_DEBUG_CMDENC(cmd_enc, "pp %d, enabling mode:\n",
485 phys_enc->hw_pp->idx - PINGPONG_0);
486 drm_mode_debug_printmodeline(&phys_enc->cached_mode);
488 _dpu_encoder_phys_cmd_update_intf_cfg(phys_enc);
489 dpu_encoder_phys_cmd_tearcheck_config(phys_enc);
492 static bool dpu_encoder_phys_cmd_needs_single_flush(
493 struct dpu_encoder_phys *phys_enc)
496 * we do separate flush for each CTL and let
497 * CTL_START synchronize them
502 static void dpu_encoder_phys_cmd_enable_helper(
503 struct dpu_encoder_phys *phys_enc)
505 struct dpu_hw_ctl *ctl;
508 if (!phys_enc || !phys_enc->hw_ctl || !phys_enc->hw_pp) {
509 DPU_ERROR("invalid arg(s), encoder %d\n", phys_enc != 0);
513 dpu_encoder_helper_split_config(phys_enc, phys_enc->intf_idx);
515 _dpu_encoder_phys_cmd_pingpong_config(phys_enc);
517 if (!dpu_encoder_phys_cmd_is_master(phys_enc))
520 ctl = phys_enc->hw_ctl;
521 ctl->ops.get_bitmask_intf(ctl, &flush_mask, phys_enc->intf_idx);
522 ctl->ops.update_pending_flush(ctl, flush_mask);
528 static void dpu_encoder_phys_cmd_enable(struct dpu_encoder_phys *phys_enc)
530 struct dpu_encoder_phys_cmd *cmd_enc =
531 to_dpu_encoder_phys_cmd(phys_enc);
533 if (!phys_enc || !phys_enc->hw_pp) {
534 DPU_ERROR("invalid phys encoder\n");
538 DPU_DEBUG_CMDENC(cmd_enc, "pp %d\n", phys_enc->hw_pp->idx - PINGPONG_0);
540 if (phys_enc->enable_state == DPU_ENC_ENABLED) {
541 DPU_ERROR("already enabled\n");
545 dpu_encoder_phys_cmd_enable_helper(phys_enc);
546 phys_enc->enable_state = DPU_ENC_ENABLED;
549 static void _dpu_encoder_phys_cmd_connect_te(
550 struct dpu_encoder_phys *phys_enc, bool enable)
552 if (!phys_enc || !phys_enc->hw_pp ||
553 !phys_enc->hw_pp->ops.connect_external_te)
556 trace_dpu_enc_phys_cmd_connect_te(DRMID(phys_enc->parent), enable);
557 phys_enc->hw_pp->ops.connect_external_te(phys_enc->hw_pp, enable);
560 static void dpu_encoder_phys_cmd_prepare_idle_pc(
561 struct dpu_encoder_phys *phys_enc)
563 _dpu_encoder_phys_cmd_connect_te(phys_enc, false);
566 static int dpu_encoder_phys_cmd_get_line_count(
567 struct dpu_encoder_phys *phys_enc)
569 struct dpu_hw_pingpong *hw_pp;
571 if (!phys_enc || !phys_enc->hw_pp)
574 if (!dpu_encoder_phys_cmd_is_master(phys_enc))
577 hw_pp = phys_enc->hw_pp;
578 if (!hw_pp->ops.get_line_count)
581 return hw_pp->ops.get_line_count(hw_pp);
584 static void dpu_encoder_phys_cmd_disable(struct dpu_encoder_phys *phys_enc)
586 struct dpu_encoder_phys_cmd *cmd_enc =
587 to_dpu_encoder_phys_cmd(phys_enc);
589 if (!phys_enc || !phys_enc->hw_pp) {
590 DPU_ERROR("invalid encoder\n");
593 DRM_DEBUG_KMS("id:%u pp:%d state:%d\n", DRMID(phys_enc->parent),
594 phys_enc->hw_pp->idx - PINGPONG_0,
595 phys_enc->enable_state);
597 if (phys_enc->enable_state == DPU_ENC_DISABLED) {
598 DPU_ERROR_CMDENC(cmd_enc, "already disabled\n");
602 if (phys_enc->hw_pp->ops.enable_tearcheck)
603 phys_enc->hw_pp->ops.enable_tearcheck(phys_enc->hw_pp, false);
604 phys_enc->enable_state = DPU_ENC_DISABLED;
607 static void dpu_encoder_phys_cmd_destroy(struct dpu_encoder_phys *phys_enc)
609 struct dpu_encoder_phys_cmd *cmd_enc =
610 to_dpu_encoder_phys_cmd(phys_enc);
613 DPU_ERROR("invalid encoder\n");
619 static void dpu_encoder_phys_cmd_get_hw_resources(
620 struct dpu_encoder_phys *phys_enc,
621 struct dpu_encoder_hw_resources *hw_res,
622 struct drm_connector_state *conn_state)
624 struct dpu_encoder_phys_cmd *cmd_enc =
625 to_dpu_encoder_phys_cmd(phys_enc);
628 DPU_ERROR("invalid encoder\n");
632 if ((phys_enc->intf_idx - INTF_0) >= INTF_MAX) {
633 DPU_ERROR("invalid intf idx:%d\n", phys_enc->intf_idx);
637 DPU_DEBUG_CMDENC(cmd_enc, "\n");
638 hw_res->intfs[phys_enc->intf_idx - INTF_0] = INTF_MODE_CMD;
641 static void dpu_encoder_phys_cmd_prepare_for_kickoff(
642 struct dpu_encoder_phys *phys_enc,
643 struct dpu_encoder_kickoff_params *params)
645 struct dpu_encoder_phys_cmd *cmd_enc =
646 to_dpu_encoder_phys_cmd(phys_enc);
649 if (!phys_enc || !phys_enc->hw_pp) {
650 DPU_ERROR("invalid encoder\n");
653 DRM_DEBUG_KMS("id:%u pp:%d pending_cnt:%d\n", DRMID(phys_enc->parent),
654 phys_enc->hw_pp->idx - PINGPONG_0,
655 atomic_read(&phys_enc->pending_kickoff_cnt));
658 * Mark kickoff request as outstanding. If there are more than one,
659 * outstanding, then we have to wait for the previous one to complete
661 ret = _dpu_encoder_phys_cmd_wait_for_idle(phys_enc);
663 /* force pending_kickoff_cnt 0 to discard failed kickoff */
664 atomic_set(&phys_enc->pending_kickoff_cnt, 0);
665 DRM_ERROR("failed wait_for_idle: id:%u ret:%d pp:%d\n",
666 DRMID(phys_enc->parent), ret,
667 phys_enc->hw_pp->idx - PINGPONG_0);
670 DPU_DEBUG_CMDENC(cmd_enc, "pp:%d pending_cnt %d\n",
671 phys_enc->hw_pp->idx - PINGPONG_0,
672 atomic_read(&phys_enc->pending_kickoff_cnt));
675 static int _dpu_encoder_phys_cmd_wait_for_ctl_start(
676 struct dpu_encoder_phys *phys_enc)
678 struct dpu_encoder_phys_cmd *cmd_enc =
679 to_dpu_encoder_phys_cmd(phys_enc);
680 struct dpu_encoder_wait_info wait_info;
683 if (!phys_enc || !phys_enc->hw_ctl) {
684 DPU_ERROR("invalid argument(s)\n");
688 wait_info.wq = &phys_enc->pending_kickoff_wq;
689 wait_info.atomic_cnt = &phys_enc->pending_ctlstart_cnt;
690 wait_info.timeout_ms = KICKOFF_TIMEOUT_MS;
692 ret = dpu_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_CTL_START,
694 if (ret == -ETIMEDOUT) {
695 DPU_ERROR_CMDENC(cmd_enc, "ctl start interrupt wait failed\n");
703 static int dpu_encoder_phys_cmd_wait_for_tx_complete(
704 struct dpu_encoder_phys *phys_enc)
707 struct dpu_encoder_phys_cmd *cmd_enc;
712 cmd_enc = to_dpu_encoder_phys_cmd(phys_enc);
714 rc = _dpu_encoder_phys_cmd_wait_for_idle(phys_enc);
716 DRM_ERROR("failed wait_for_idle: id:%u ret:%d intf:%d\n",
717 DRMID(phys_enc->parent), rc,
718 phys_enc->intf_idx - INTF_0);
724 static int dpu_encoder_phys_cmd_wait_for_commit_done(
725 struct dpu_encoder_phys *phys_enc)
728 struct dpu_encoder_phys_cmd *cmd_enc;
733 cmd_enc = to_dpu_encoder_phys_cmd(phys_enc);
735 /* only required for master controller */
736 if (dpu_encoder_phys_cmd_is_master(phys_enc))
737 rc = _dpu_encoder_phys_cmd_wait_for_ctl_start(phys_enc);
739 /* required for both controllers */
740 if (!rc && cmd_enc->serialize_wait4pp)
741 dpu_encoder_phys_cmd_prepare_for_kickoff(phys_enc, NULL);
746 static int dpu_encoder_phys_cmd_wait_for_vblank(
747 struct dpu_encoder_phys *phys_enc)
750 struct dpu_encoder_phys_cmd *cmd_enc;
751 struct dpu_encoder_wait_info wait_info;
756 cmd_enc = to_dpu_encoder_phys_cmd(phys_enc);
758 /* only required for master controller */
759 if (!dpu_encoder_phys_cmd_is_master(phys_enc))
762 wait_info.wq = &cmd_enc->pending_vblank_wq;
763 wait_info.atomic_cnt = &cmd_enc->pending_vblank_cnt;
764 wait_info.timeout_ms = _dpu_encoder_phys_cmd_get_idle_timeout(cmd_enc);
766 atomic_inc(&cmd_enc->pending_vblank_cnt);
768 rc = dpu_encoder_helper_wait_for_irq(phys_enc, INTR_IDX_RDPTR,
774 static void dpu_encoder_phys_cmd_handle_post_kickoff(
775 struct dpu_encoder_phys *phys_enc)
781 * re-enable external TE, either for the first time after enabling
782 * or if disabled for Autorefresh
784 _dpu_encoder_phys_cmd_connect_te(phys_enc, true);
787 static void dpu_encoder_phys_cmd_trigger_start(
788 struct dpu_encoder_phys *phys_enc)
793 dpu_encoder_helper_trigger_start(phys_enc);
796 static void dpu_encoder_phys_cmd_init_ops(
797 struct dpu_encoder_phys_ops *ops)
799 ops->is_master = dpu_encoder_phys_cmd_is_master;
800 ops->mode_set = dpu_encoder_phys_cmd_mode_set;
801 ops->mode_fixup = dpu_encoder_phys_cmd_mode_fixup;
802 ops->enable = dpu_encoder_phys_cmd_enable;
803 ops->disable = dpu_encoder_phys_cmd_disable;
804 ops->destroy = dpu_encoder_phys_cmd_destroy;
805 ops->get_hw_resources = dpu_encoder_phys_cmd_get_hw_resources;
806 ops->control_vblank_irq = dpu_encoder_phys_cmd_control_vblank_irq;
807 ops->wait_for_commit_done = dpu_encoder_phys_cmd_wait_for_commit_done;
808 ops->prepare_for_kickoff = dpu_encoder_phys_cmd_prepare_for_kickoff;
809 ops->wait_for_tx_complete = dpu_encoder_phys_cmd_wait_for_tx_complete;
810 ops->wait_for_vblank = dpu_encoder_phys_cmd_wait_for_vblank;
811 ops->trigger_start = dpu_encoder_phys_cmd_trigger_start;
812 ops->needs_single_flush = dpu_encoder_phys_cmd_needs_single_flush;
813 ops->hw_reset = dpu_encoder_helper_hw_reset;
814 ops->irq_control = dpu_encoder_phys_cmd_irq_control;
815 ops->restore = dpu_encoder_phys_cmd_enable_helper;
816 ops->prepare_idle_pc = dpu_encoder_phys_cmd_prepare_idle_pc;
817 ops->handle_post_kickoff = dpu_encoder_phys_cmd_handle_post_kickoff;
818 ops->get_line_count = dpu_encoder_phys_cmd_get_line_count;
821 struct dpu_encoder_phys *dpu_encoder_phys_cmd_init(
822 struct dpu_enc_phys_init_params *p)
824 struct dpu_encoder_phys *phys_enc = NULL;
825 struct dpu_encoder_phys_cmd *cmd_enc = NULL;
826 struct dpu_hw_mdp *hw_mdp;
827 struct dpu_encoder_irq *irq;
830 DPU_DEBUG("intf %d\n", p->intf_idx - INTF_0);
832 cmd_enc = kzalloc(sizeof(*cmd_enc), GFP_KERNEL);
835 DPU_ERROR("failed to allocate\n");
838 phys_enc = &cmd_enc->base;
840 hw_mdp = dpu_rm_get_mdp(&p->dpu_kms->rm);
841 if (IS_ERR_OR_NULL(hw_mdp)) {
842 ret = PTR_ERR(hw_mdp);
843 DPU_ERROR("failed to get mdptop\n");
846 phys_enc->hw_mdptop = hw_mdp;
847 phys_enc->intf_idx = p->intf_idx;
849 dpu_encoder_phys_cmd_init_ops(&phys_enc->ops);
850 phys_enc->parent = p->parent;
851 phys_enc->parent_ops = p->parent_ops;
852 phys_enc->dpu_kms = p->dpu_kms;
853 phys_enc->split_role = p->split_role;
854 phys_enc->intf_mode = INTF_MODE_CMD;
855 phys_enc->enc_spinlock = p->enc_spinlock;
856 cmd_enc->stream_sel = 0;
857 phys_enc->enable_state = DPU_ENC_DISABLED;
858 for (i = 0; i < INTR_IDX_MAX; i++) {
859 irq = &phys_enc->irq[i];
860 INIT_LIST_HEAD(&irq->cb.list);
861 irq->irq_idx = -EINVAL;
862 irq->hw_idx = -EINVAL;
863 irq->cb.arg = phys_enc;
866 irq = &phys_enc->irq[INTR_IDX_CTL_START];
867 irq->name = "ctl_start";
868 irq->intr_type = DPU_IRQ_TYPE_CTL_START;
869 irq->intr_idx = INTR_IDX_CTL_START;
870 irq->cb.func = dpu_encoder_phys_cmd_ctl_start_irq;
872 irq = &phys_enc->irq[INTR_IDX_PINGPONG];
873 irq->name = "pp_done";
874 irq->intr_type = DPU_IRQ_TYPE_PING_PONG_COMP;
875 irq->intr_idx = INTR_IDX_PINGPONG;
876 irq->cb.func = dpu_encoder_phys_cmd_pp_tx_done_irq;
878 irq = &phys_enc->irq[INTR_IDX_RDPTR];
879 irq->name = "pp_rd_ptr";
880 irq->intr_type = DPU_IRQ_TYPE_PING_PONG_RD_PTR;
881 irq->intr_idx = INTR_IDX_RDPTR;
882 irq->cb.func = dpu_encoder_phys_cmd_pp_rd_ptr_irq;
884 irq = &phys_enc->irq[INTR_IDX_UNDERRUN];
885 irq->name = "underrun";
886 irq->intr_type = DPU_IRQ_TYPE_INTF_UNDER_RUN;
887 irq->intr_idx = INTR_IDX_UNDERRUN;
888 irq->cb.func = dpu_encoder_phys_cmd_underrun_irq;
890 atomic_set(&phys_enc->vblank_refcount, 0);
891 atomic_set(&phys_enc->pending_kickoff_cnt, 0);
892 atomic_set(&phys_enc->pending_ctlstart_cnt, 0);
893 atomic_set(&cmd_enc->pending_vblank_cnt, 0);
894 init_waitqueue_head(&phys_enc->pending_kickoff_wq);
895 init_waitqueue_head(&cmd_enc->pending_vblank_wq);
897 DPU_DEBUG_CMDENC(cmd_enc, "created\n");