1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
4 * Copyright (C) 2013 Red Hat
5 * Author: Rob Clark <robdclark@gmail.com>
8 #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
9 #include <linux/kthread.h>
10 #include <linux/debugfs.h>
11 #include <linux/seq_file.h>
15 #include <drm/drm_crtc.h>
16 #include <drm/drm_probe_helper.h>
18 #include "dpu_hw_catalog.h"
19 #include "dpu_hw_intf.h"
20 #include "dpu_hw_ctl.h"
21 #include "dpu_formats.h"
22 #include "dpu_encoder_phys.h"
24 #include "dpu_trace.h"
25 #include "dpu_core_irq.h"
27 #define DPU_DEBUG_ENC(e, fmt, ...) DPU_DEBUG("enc%d " fmt,\
28 (e) ? (e)->base.base.id : -1, ##__VA_ARGS__)
30 #define DPU_ERROR_ENC(e, fmt, ...) DPU_ERROR("enc%d " fmt,\
31 (e) ? (e)->base.base.id : -1, ##__VA_ARGS__)
33 #define DPU_DEBUG_PHYS(p, fmt, ...) DPU_DEBUG("enc%d intf%d pp%d " fmt,\
34 (p) ? (p)->parent->base.id : -1, \
35 (p) ? (p)->intf_idx - INTF_0 : -1, \
36 (p) ? ((p)->hw_pp ? (p)->hw_pp->idx - PINGPONG_0 : -1) : -1, \
39 #define DPU_ERROR_PHYS(p, fmt, ...) DPU_ERROR("enc%d intf%d pp%d " fmt,\
40 (p) ? (p)->parent->base.id : -1, \
41 (p) ? (p)->intf_idx - INTF_0 : -1, \
42 (p) ? ((p)->hw_pp ? (p)->hw_pp->idx - PINGPONG_0 : -1) : -1, \
46 * Two to anticipate panels that can do cmd/vid dynamic switching
47 * plan is to create all possible physical encoder types, and switch between
50 #define NUM_PHYS_ENCODER_TYPES 2
52 #define MAX_PHYS_ENCODERS_PER_VIRTUAL \
53 (MAX_H_TILES_PER_DISPLAY * NUM_PHYS_ENCODER_TYPES)
55 #define MAX_CHANNELS_PER_ENC 2
57 #define IDLE_SHORT_TIMEOUT 1
59 #define MAX_VDISPLAY_SPLIT 1080
61 /* timeout in frames waiting for frame done */
62 #define DPU_ENCODER_FRAME_DONE_TIMEOUT_FRAMES 5
65 * enum dpu_enc_rc_events - events for resource control state machine
66 * @DPU_ENC_RC_EVENT_KICKOFF:
67 * This event happens at NORMAL priority.
68 * Event that signals the start of the transfer. When this event is
69 * received, enable MDP/DSI core clocks. Regardless of the previous
70 * state, the resource should be in ON state at the end of this event.
71 * @DPU_ENC_RC_EVENT_FRAME_DONE:
72 * This event happens at INTERRUPT level.
73 * Event signals the end of the data transfer after the PP FRAME_DONE
74 * event. At the end of this event, a delayed work is scheduled to go to
75 * IDLE_PC state after IDLE_TIMEOUT time.
76 * @DPU_ENC_RC_EVENT_PRE_STOP:
77 * This event happens at NORMAL priority.
78 * This event, when received during the ON state, leave the RC STATE
79 * in the PRE_OFF state. It should be followed by the STOP event as
80 * part of encoder disable.
81 * If received during IDLE or OFF states, it will do nothing.
82 * @DPU_ENC_RC_EVENT_STOP:
83 * This event happens at NORMAL priority.
84 * When this event is received, disable all the MDP/DSI core clocks, and
85 * disable IRQs. It should be called from the PRE_OFF or IDLE states.
86 * IDLE is expected when IDLE_PC has run, and PRE_OFF did nothing.
87 * PRE_OFF is expected when PRE_STOP was executed during the ON state.
88 * Resource state should be in OFF at the end of the event.
89 * @DPU_ENC_RC_EVENT_ENTER_IDLE:
90 * This event happens at NORMAL priority from a work item.
91 * Event signals that there were no frame updates for IDLE_TIMEOUT time.
92 * This would disable MDP/DSI core clocks and change the resource state
95 enum dpu_enc_rc_events
{
96 DPU_ENC_RC_EVENT_KICKOFF
= 1,
97 DPU_ENC_RC_EVENT_FRAME_DONE
,
98 DPU_ENC_RC_EVENT_PRE_STOP
,
99 DPU_ENC_RC_EVENT_STOP
,
100 DPU_ENC_RC_EVENT_ENTER_IDLE
104 * enum dpu_enc_rc_states - states that the resource control maintains
105 * @DPU_ENC_RC_STATE_OFF: Resource is in OFF state
106 * @DPU_ENC_RC_STATE_PRE_OFF: Resource is transitioning to OFF state
107 * @DPU_ENC_RC_STATE_ON: Resource is in ON state
108 * @DPU_ENC_RC_STATE_MODESET: Resource is in modeset state
109 * @DPU_ENC_RC_STATE_IDLE: Resource is in IDLE state
111 enum dpu_enc_rc_states
{
112 DPU_ENC_RC_STATE_OFF
,
113 DPU_ENC_RC_STATE_PRE_OFF
,
115 DPU_ENC_RC_STATE_IDLE
119 * struct dpu_encoder_virt - virtual encoder. Container of one or more physical
120 * encoders. Virtual encoder manages one "logical" display. Physical
121 * encoders manage one intf block, tied to a specific panel/sub-panel.
122 * Virtual encoder defers as much as possible to the physical encoders.
123 * Virtual encoder registers itself with the DRM Framework as the encoder.
124 * @base: drm_encoder base class for registration with DRM
125 * @enc_spinlock: Virtual-Encoder-Wide Spin Lock for IRQ purposes
126 * @bus_scaling_client: Client handle to the bus scaling interface
127 * @enabled: True if the encoder is active, protected by enc_lock
128 * @num_phys_encs: Actual number of physical encoders contained.
129 * @phys_encs: Container of physical encoders managed.
130 * @cur_master: Pointer to the current master in this mode. Optimization
131 * Only valid after enable. Cleared as disable.
132 * @hw_pp Handle to the pingpong blocks used for the display. No.
133 * pingpong blocks can be different than num_phys_encs.
134 * @intfs_swapped Whether or not the phys_enc interfaces have been swapped
135 * for partial update right-only cases, such as pingpong
136 * split where virtual pingpong does not generate IRQs
137 * @crtc: Pointer to the currently assigned crtc. Normally you
138 * would use crtc->state->encoder_mask to determine the
139 * link between encoder/crtc. However in this case we need
140 * to track crtc in the disable() hook which is called
141 * _after_ encoder_mask is cleared.
142 * @crtc_kickoff_cb: Callback into CRTC that will flush & start
144 * @crtc_kickoff_cb_data: Opaque user data given to crtc_kickoff_cb
145 * @debugfs_root: Debug file system root file node
146 * @enc_lock: Lock around physical encoder
147 * create/destroy/enable/disable
148 * @frame_busy_mask: Bitmask tracking which phys_enc we are still
149 * busy processing current command.
150 * Bit0 = phys_encs[0] etc.
151 * @crtc_frame_event_cb: callback handler for frame event
152 * @crtc_frame_event_cb_data: callback handler private data
153 * @frame_done_timeout_ms: frame done timeout in ms
154 * @frame_done_timer: watchdog timer for frame done event
155 * @vsync_event_timer: vsync timer
156 * @disp_info: local copy of msm_display_info struct
157 * @idle_pc_supported: indicate if idle power collaps is supported
158 * @rc_lock: resource control mutex lock to protect
159 * virt encoder over various state changes
160 * @rc_state: resource controller state
161 * @delayed_off_work: delayed worker to schedule disabling of
162 * clks and resources after IDLE_TIMEOUT time.
163 * @vsync_event_work: worker to handle vsync event for autorefresh
164 * @topology: topology of the display
165 * @mode_set_complete: flag to indicate modeset completion
166 * @idle_timeout: idle timeout duration in milliseconds
168 struct dpu_encoder_virt
{
169 struct drm_encoder base
;
170 spinlock_t enc_spinlock
;
171 uint32_t bus_scaling_client
;
175 unsigned int num_phys_encs
;
176 struct dpu_encoder_phys
*phys_encs
[MAX_PHYS_ENCODERS_PER_VIRTUAL
];
177 struct dpu_encoder_phys
*cur_master
;
178 struct dpu_encoder_phys
*cur_slave
;
179 struct dpu_hw_pingpong
*hw_pp
[MAX_CHANNELS_PER_ENC
];
183 struct drm_crtc
*crtc
;
185 struct dentry
*debugfs_root
;
186 struct mutex enc_lock
;
187 DECLARE_BITMAP(frame_busy_mask
, MAX_PHYS_ENCODERS_PER_VIRTUAL
);
188 void (*crtc_frame_event_cb
)(void *, u32 event
);
189 void *crtc_frame_event_cb_data
;
191 atomic_t frame_done_timeout_ms
;
192 struct timer_list frame_done_timer
;
193 struct timer_list vsync_event_timer
;
195 struct msm_display_info disp_info
;
197 bool idle_pc_supported
;
198 struct mutex rc_lock
;
199 enum dpu_enc_rc_states rc_state
;
200 struct delayed_work delayed_off_work
;
201 struct kthread_work vsync_event_work
;
202 struct msm_display_topology topology
;
203 bool mode_set_complete
;
208 #define to_dpu_encoder_virt(x) container_of(x, struct dpu_encoder_virt, base)
210 void dpu_encoder_helper_report_irq_timeout(struct dpu_encoder_phys
*phys_enc
,
211 enum dpu_intr_idx intr_idx
)
213 DRM_ERROR("irq timeout id=%u, intf=%d, pp=%d, intr=%d\n",
214 DRMID(phys_enc
->parent
), phys_enc
->intf_idx
- INTF_0
,
215 phys_enc
->hw_pp
->idx
- PINGPONG_0
, intr_idx
);
217 if (phys_enc
->parent_ops
->handle_frame_done
)
218 phys_enc
->parent_ops
->handle_frame_done(
219 phys_enc
->parent
, phys_enc
,
220 DPU_ENCODER_FRAME_EVENT_ERROR
);
223 static int dpu_encoder_helper_wait_event_timeout(int32_t drm_id
,
224 int32_t hw_id
, struct dpu_encoder_wait_info
*info
);
226 int dpu_encoder_helper_wait_for_irq(struct dpu_encoder_phys
*phys_enc
,
227 enum dpu_intr_idx intr_idx
,
228 struct dpu_encoder_wait_info
*wait_info
)
230 struct dpu_encoder_irq
*irq
;
234 if (!phys_enc
|| !wait_info
|| intr_idx
>= INTR_IDX_MAX
) {
235 DPU_ERROR("invalid params\n");
238 irq
= &phys_enc
->irq
[intr_idx
];
240 /* note: do master / slave checking outside */
242 /* return EWOULDBLOCK since we know the wait isn't necessary */
243 if (phys_enc
->enable_state
== DPU_ENC_DISABLED
) {
244 DRM_ERROR("encoder is disabled id=%u, intr=%d, hw=%d, irq=%d",
245 DRMID(phys_enc
->parent
), intr_idx
, irq
->hw_idx
,
250 if (irq
->irq_idx
< 0) {
251 DRM_DEBUG_KMS("skip irq wait id=%u, intr=%d, hw=%d, irq=%s",
252 DRMID(phys_enc
->parent
), intr_idx
, irq
->hw_idx
,
257 DRM_DEBUG_KMS("id=%u, intr=%d, hw=%d, irq=%d, pp=%d, pending_cnt=%d",
258 DRMID(phys_enc
->parent
), intr_idx
, irq
->hw_idx
,
259 irq
->irq_idx
, phys_enc
->hw_pp
->idx
- PINGPONG_0
,
260 atomic_read(wait_info
->atomic_cnt
));
262 ret
= dpu_encoder_helper_wait_event_timeout(
263 DRMID(phys_enc
->parent
),
268 irq_status
= dpu_core_irq_read(phys_enc
->dpu_kms
,
273 DRM_DEBUG_KMS("irq not triggered id=%u, intr=%d, "
274 "hw=%d, irq=%d, pp=%d, atomic_cnt=%d",
275 DRMID(phys_enc
->parent
), intr_idx
,
276 irq
->hw_idx
, irq
->irq_idx
,
277 phys_enc
->hw_pp
->idx
- PINGPONG_0
,
278 atomic_read(wait_info
->atomic_cnt
));
279 local_irq_save(flags
);
280 irq
->cb
.func(phys_enc
, irq
->irq_idx
);
281 local_irq_restore(flags
);
285 DRM_DEBUG_KMS("irq timeout id=%u, intr=%d, "
286 "hw=%d, irq=%d, pp=%d, atomic_cnt=%d",
287 DRMID(phys_enc
->parent
), intr_idx
,
288 irq
->hw_idx
, irq
->irq_idx
,
289 phys_enc
->hw_pp
->idx
- PINGPONG_0
,
290 atomic_read(wait_info
->atomic_cnt
));
294 trace_dpu_enc_irq_wait_success(DRMID(phys_enc
->parent
),
295 intr_idx
, irq
->hw_idx
, irq
->irq_idx
,
296 phys_enc
->hw_pp
->idx
- PINGPONG_0
,
297 atomic_read(wait_info
->atomic_cnt
));
303 int dpu_encoder_helper_register_irq(struct dpu_encoder_phys
*phys_enc
,
304 enum dpu_intr_idx intr_idx
)
306 struct dpu_encoder_irq
*irq
;
309 if (!phys_enc
|| intr_idx
>= INTR_IDX_MAX
) {
310 DPU_ERROR("invalid params\n");
313 irq
= &phys_enc
->irq
[intr_idx
];
315 if (irq
->irq_idx
>= 0) {
316 DPU_DEBUG_PHYS(phys_enc
,
317 "skipping already registered irq %s type %d\n",
318 irq
->name
, irq
->intr_type
);
322 irq
->irq_idx
= dpu_core_irq_idx_lookup(phys_enc
->dpu_kms
,
323 irq
->intr_type
, irq
->hw_idx
);
324 if (irq
->irq_idx
< 0) {
325 DPU_ERROR_PHYS(phys_enc
,
326 "failed to lookup IRQ index for %s type:%d\n",
327 irq
->name
, irq
->intr_type
);
331 ret
= dpu_core_irq_register_callback(phys_enc
->dpu_kms
, irq
->irq_idx
,
334 DPU_ERROR_PHYS(phys_enc
,
335 "failed to register IRQ callback for %s\n",
337 irq
->irq_idx
= -EINVAL
;
341 ret
= dpu_core_irq_enable(phys_enc
->dpu_kms
, &irq
->irq_idx
, 1);
343 DRM_ERROR("enable failed id=%u, intr=%d, hw=%d, irq=%d",
344 DRMID(phys_enc
->parent
), intr_idx
, irq
->hw_idx
,
346 dpu_core_irq_unregister_callback(phys_enc
->dpu_kms
,
347 irq
->irq_idx
, &irq
->cb
);
348 irq
->irq_idx
= -EINVAL
;
352 trace_dpu_enc_irq_register_success(DRMID(phys_enc
->parent
), intr_idx
,
353 irq
->hw_idx
, irq
->irq_idx
);
358 int dpu_encoder_helper_unregister_irq(struct dpu_encoder_phys
*phys_enc
,
359 enum dpu_intr_idx intr_idx
)
361 struct dpu_encoder_irq
*irq
;
365 DPU_ERROR("invalid encoder\n");
368 irq
= &phys_enc
->irq
[intr_idx
];
370 /* silently skip irqs that weren't registered */
371 if (irq
->irq_idx
< 0) {
372 DRM_ERROR("duplicate unregister id=%u, intr=%d, hw=%d, irq=%d",
373 DRMID(phys_enc
->parent
), intr_idx
, irq
->hw_idx
,
378 ret
= dpu_core_irq_disable(phys_enc
->dpu_kms
, &irq
->irq_idx
, 1);
380 DRM_ERROR("disable failed id=%u, intr=%d, hw=%d, irq=%d ret=%d",
381 DRMID(phys_enc
->parent
), intr_idx
, irq
->hw_idx
,
385 ret
= dpu_core_irq_unregister_callback(phys_enc
->dpu_kms
, irq
->irq_idx
,
388 DRM_ERROR("unreg cb fail id=%u, intr=%d, hw=%d, irq=%d ret=%d",
389 DRMID(phys_enc
->parent
), intr_idx
, irq
->hw_idx
,
393 trace_dpu_enc_irq_unregister_success(DRMID(phys_enc
->parent
), intr_idx
,
394 irq
->hw_idx
, irq
->irq_idx
);
396 irq
->irq_idx
= -EINVAL
;
401 void dpu_encoder_get_hw_resources(struct drm_encoder
*drm_enc
,
402 struct dpu_encoder_hw_resources
*hw_res
)
404 struct dpu_encoder_virt
*dpu_enc
= NULL
;
407 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
408 DPU_DEBUG_ENC(dpu_enc
, "\n");
410 /* Query resources used by phys encs, expected to be without overlap */
411 memset(hw_res
, 0, sizeof(*hw_res
));
413 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
414 struct dpu_encoder_phys
*phys
= dpu_enc
->phys_encs
[i
];
416 if (phys
&& phys
->ops
.get_hw_resources
)
417 phys
->ops
.get_hw_resources(phys
, hw_res
);
421 static void dpu_encoder_destroy(struct drm_encoder
*drm_enc
)
423 struct dpu_encoder_virt
*dpu_enc
= NULL
;
427 DPU_ERROR("invalid encoder\n");
431 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
432 DPU_DEBUG_ENC(dpu_enc
, "\n");
434 mutex_lock(&dpu_enc
->enc_lock
);
436 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
437 struct dpu_encoder_phys
*phys
= dpu_enc
->phys_encs
[i
];
439 if (phys
&& phys
->ops
.destroy
) {
440 phys
->ops
.destroy(phys
);
441 --dpu_enc
->num_phys_encs
;
442 dpu_enc
->phys_encs
[i
] = NULL
;
446 if (dpu_enc
->num_phys_encs
)
447 DPU_ERROR_ENC(dpu_enc
, "expected 0 num_phys_encs not %d\n",
448 dpu_enc
->num_phys_encs
);
449 dpu_enc
->num_phys_encs
= 0;
450 mutex_unlock(&dpu_enc
->enc_lock
);
452 drm_encoder_cleanup(drm_enc
);
453 mutex_destroy(&dpu_enc
->enc_lock
);
456 void dpu_encoder_helper_split_config(
457 struct dpu_encoder_phys
*phys_enc
,
458 enum dpu_intf interface
)
460 struct dpu_encoder_virt
*dpu_enc
;
461 struct split_pipe_cfg cfg
= { 0 };
462 struct dpu_hw_mdp
*hw_mdptop
;
463 struct msm_display_info
*disp_info
;
465 if (!phys_enc
|| !phys_enc
->hw_mdptop
|| !phys_enc
->parent
) {
466 DPU_ERROR("invalid arg(s), encoder %d\n", phys_enc
!= 0);
470 dpu_enc
= to_dpu_encoder_virt(phys_enc
->parent
);
471 hw_mdptop
= phys_enc
->hw_mdptop
;
472 disp_info
= &dpu_enc
->disp_info
;
474 if (disp_info
->intf_type
!= DRM_MODE_ENCODER_DSI
)
478 * disable split modes since encoder will be operating in as the only
479 * encoder, either for the entire use case in the case of, for example,
480 * single DSI, or for this frame in the case of left/right only partial
483 if (phys_enc
->split_role
== ENC_ROLE_SOLO
) {
484 if (hw_mdptop
->ops
.setup_split_pipe
)
485 hw_mdptop
->ops
.setup_split_pipe(hw_mdptop
, &cfg
);
490 cfg
.mode
= phys_enc
->intf_mode
;
491 cfg
.intf
= interface
;
493 if (cfg
.en
&& phys_enc
->ops
.needs_single_flush
&&
494 phys_enc
->ops
.needs_single_flush(phys_enc
))
495 cfg
.split_flush_en
= true;
497 if (phys_enc
->split_role
== ENC_ROLE_MASTER
) {
498 DPU_DEBUG_ENC(dpu_enc
, "enable %d\n", cfg
.en
);
500 if (hw_mdptop
->ops
.setup_split_pipe
)
501 hw_mdptop
->ops
.setup_split_pipe(hw_mdptop
, &cfg
);
505 static void _dpu_encoder_adjust_mode(struct drm_connector
*connector
,
506 struct drm_display_mode
*adj_mode
)
508 struct drm_display_mode
*cur_mode
;
510 if (!connector
|| !adj_mode
)
513 list_for_each_entry(cur_mode
, &connector
->modes
, head
) {
514 if (cur_mode
->vdisplay
== adj_mode
->vdisplay
&&
515 cur_mode
->hdisplay
== adj_mode
->hdisplay
&&
516 drm_mode_vrefresh(cur_mode
) == drm_mode_vrefresh(adj_mode
)) {
517 adj_mode
->private = cur_mode
->private;
518 adj_mode
->private_flags
|= cur_mode
->private_flags
;
523 static struct msm_display_topology
dpu_encoder_get_topology(
524 struct dpu_encoder_virt
*dpu_enc
,
525 struct dpu_kms
*dpu_kms
,
526 struct drm_display_mode
*mode
)
528 struct msm_display_topology topology
;
529 int i
, intf_count
= 0;
531 for (i
= 0; i
< MAX_PHYS_ENCODERS_PER_VIRTUAL
; i
++)
532 if (dpu_enc
->phys_encs
[i
])
535 /* User split topology for width > 1080 */
536 topology
.num_lm
= (mode
->vdisplay
> MAX_VDISPLAY_SPLIT
) ? 2 : 1;
537 topology
.num_enc
= 0;
538 topology
.num_intf
= intf_count
;
542 static int dpu_encoder_virt_atomic_check(
543 struct drm_encoder
*drm_enc
,
544 struct drm_crtc_state
*crtc_state
,
545 struct drm_connector_state
*conn_state
)
547 struct dpu_encoder_virt
*dpu_enc
;
548 struct msm_drm_private
*priv
;
549 struct dpu_kms
*dpu_kms
;
550 const struct drm_display_mode
*mode
;
551 struct drm_display_mode
*adj_mode
;
552 struct msm_display_topology topology
;
556 if (!drm_enc
|| !crtc_state
|| !conn_state
) {
557 DPU_ERROR("invalid arg(s), drm_enc %d, crtc/conn state %d/%d\n",
558 drm_enc
!= 0, crtc_state
!= 0, conn_state
!= 0);
562 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
563 DPU_DEBUG_ENC(dpu_enc
, "\n");
565 priv
= drm_enc
->dev
->dev_private
;
566 dpu_kms
= to_dpu_kms(priv
->kms
);
567 mode
= &crtc_state
->mode
;
568 adj_mode
= &crtc_state
->adjusted_mode
;
569 trace_dpu_enc_atomic_check(DRMID(drm_enc
));
572 * display drivers may populate private fields of the drm display mode
573 * structure while registering possible modes of a connector with DRM.
574 * These private fields are not populated back while DRM invokes
575 * the mode_set callbacks. This module retrieves and populates the
576 * private fields of the given mode.
578 _dpu_encoder_adjust_mode(conn_state
->connector
, adj_mode
);
580 /* perform atomic check on the first physical encoder (master) */
581 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
582 struct dpu_encoder_phys
*phys
= dpu_enc
->phys_encs
[i
];
584 if (phys
&& phys
->ops
.atomic_check
)
585 ret
= phys
->ops
.atomic_check(phys
, crtc_state
,
587 else if (phys
&& phys
->ops
.mode_fixup
)
588 if (!phys
->ops
.mode_fixup(phys
, mode
, adj_mode
))
592 DPU_ERROR_ENC(dpu_enc
,
593 "mode unsupported, phys idx %d\n", i
);
598 topology
= dpu_encoder_get_topology(dpu_enc
, dpu_kms
, adj_mode
);
600 /* Reserve dynamic resources now. Indicating AtomicTest phase */
603 * Avoid reserving resources when mode set is pending. Topology
604 * info may not be available to complete reservation.
606 if (drm_atomic_crtc_needs_modeset(crtc_state
)
607 && dpu_enc
->mode_set_complete
) {
608 ret
= dpu_rm_reserve(&dpu_kms
->rm
, drm_enc
, crtc_state
,
610 dpu_enc
->mode_set_complete
= false;
615 drm_mode_set_crtcinfo(adj_mode
, 0);
617 trace_dpu_enc_atomic_check_flags(DRMID(drm_enc
), adj_mode
->flags
,
618 adj_mode
->private_flags
);
623 static void _dpu_encoder_update_vsync_source(struct dpu_encoder_virt
*dpu_enc
,
624 struct msm_display_info
*disp_info
)
626 struct dpu_vsync_source_cfg vsync_cfg
= { 0 };
627 struct msm_drm_private
*priv
;
628 struct dpu_kms
*dpu_kms
;
629 struct dpu_hw_mdp
*hw_mdptop
;
630 struct drm_encoder
*drm_enc
;
633 if (!dpu_enc
|| !disp_info
) {
634 DPU_ERROR("invalid param dpu_enc:%d or disp_info:%d\n",
635 dpu_enc
!= NULL
, disp_info
!= NULL
);
637 } else if (dpu_enc
->num_phys_encs
> ARRAY_SIZE(dpu_enc
->hw_pp
)) {
638 DPU_ERROR("invalid num phys enc %d/%d\n",
639 dpu_enc
->num_phys_encs
,
640 (int) ARRAY_SIZE(dpu_enc
->hw_pp
));
644 drm_enc
= &dpu_enc
->base
;
645 /* this pointers are checked in virt_enable_helper */
646 priv
= drm_enc
->dev
->dev_private
;
648 dpu_kms
= to_dpu_kms(priv
->kms
);
650 DPU_ERROR("invalid dpu_kms\n");
654 hw_mdptop
= dpu_kms
->hw_mdp
;
656 DPU_ERROR("invalid mdptop\n");
660 if (hw_mdptop
->ops
.setup_vsync_source
&&
661 disp_info
->capabilities
& MSM_DISPLAY_CAP_CMD_MODE
) {
662 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++)
663 vsync_cfg
.ppnumber
[i
] = dpu_enc
->hw_pp
[i
]->idx
;
665 vsync_cfg
.pp_count
= dpu_enc
->num_phys_encs
;
666 if (disp_info
->is_te_using_watchdog_timer
)
667 vsync_cfg
.vsync_source
= DPU_VSYNC_SOURCE_WD_TIMER_0
;
669 vsync_cfg
.vsync_source
= DPU_VSYNC0_SOURCE_GPIO
;
671 hw_mdptop
->ops
.setup_vsync_source(hw_mdptop
, &vsync_cfg
);
675 static void _dpu_encoder_irq_control(struct drm_encoder
*drm_enc
, bool enable
)
677 struct dpu_encoder_virt
*dpu_enc
;
681 DPU_ERROR("invalid encoder\n");
685 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
687 DPU_DEBUG_ENC(dpu_enc
, "enable:%d\n", enable
);
688 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
689 struct dpu_encoder_phys
*phys
= dpu_enc
->phys_encs
[i
];
691 if (phys
&& phys
->ops
.irq_control
)
692 phys
->ops
.irq_control(phys
, enable
);
697 static void _dpu_encoder_resource_control_helper(struct drm_encoder
*drm_enc
,
700 struct msm_drm_private
*priv
;
701 struct dpu_kms
*dpu_kms
;
702 struct dpu_encoder_virt
*dpu_enc
;
704 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
705 priv
= drm_enc
->dev
->dev_private
;
706 dpu_kms
= to_dpu_kms(priv
->kms
);
708 trace_dpu_enc_rc_helper(DRMID(drm_enc
), enable
);
710 if (!dpu_enc
->cur_master
) {
711 DPU_ERROR("encoder master not set\n");
716 /* enable DPU core clks */
717 pm_runtime_get_sync(&dpu_kms
->pdev
->dev
);
719 /* enable all the irq */
720 _dpu_encoder_irq_control(drm_enc
, true);
723 /* disable all the irq */
724 _dpu_encoder_irq_control(drm_enc
, false);
726 /* disable DPU core clks */
727 pm_runtime_put_sync(&dpu_kms
->pdev
->dev
);
732 static int dpu_encoder_resource_control(struct drm_encoder
*drm_enc
,
735 struct dpu_encoder_virt
*dpu_enc
;
736 struct msm_drm_private
*priv
;
737 bool is_vid_mode
= false;
739 if (!drm_enc
|| !drm_enc
->dev
|| !drm_enc
->dev
->dev_private
||
741 DPU_ERROR("invalid parameters\n");
744 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
745 priv
= drm_enc
->dev
->dev_private
;
746 is_vid_mode
= dpu_enc
->disp_info
.capabilities
&
747 MSM_DISPLAY_CAP_VID_MODE
;
750 * when idle_pc is not supported, process only KICKOFF, STOP and MODESET
751 * events and return early for other events (ie wb display).
753 if (!dpu_enc
->idle_pc_supported
&&
754 (sw_event
!= DPU_ENC_RC_EVENT_KICKOFF
&&
755 sw_event
!= DPU_ENC_RC_EVENT_STOP
&&
756 sw_event
!= DPU_ENC_RC_EVENT_PRE_STOP
))
759 trace_dpu_enc_rc(DRMID(drm_enc
), sw_event
, dpu_enc
->idle_pc_supported
,
760 dpu_enc
->rc_state
, "begin");
763 case DPU_ENC_RC_EVENT_KICKOFF
:
764 /* cancel delayed off work, if any */
765 if (cancel_delayed_work_sync(&dpu_enc
->delayed_off_work
))
766 DPU_DEBUG_ENC(dpu_enc
, "sw_event:%d, work cancelled\n",
769 mutex_lock(&dpu_enc
->rc_lock
);
771 /* return if the resource control is already in ON state */
772 if (dpu_enc
->rc_state
== DPU_ENC_RC_STATE_ON
) {
773 DRM_DEBUG_KMS("id;%u, sw_event:%d, rc in ON state\n",
774 DRMID(drm_enc
), sw_event
);
775 mutex_unlock(&dpu_enc
->rc_lock
);
777 } else if (dpu_enc
->rc_state
!= DPU_ENC_RC_STATE_OFF
&&
778 dpu_enc
->rc_state
!= DPU_ENC_RC_STATE_IDLE
) {
779 DRM_DEBUG_KMS("id;%u, sw_event:%d, rc in state %d\n",
780 DRMID(drm_enc
), sw_event
,
782 mutex_unlock(&dpu_enc
->rc_lock
);
786 if (is_vid_mode
&& dpu_enc
->rc_state
== DPU_ENC_RC_STATE_IDLE
)
787 _dpu_encoder_irq_control(drm_enc
, true);
789 _dpu_encoder_resource_control_helper(drm_enc
, true);
791 dpu_enc
->rc_state
= DPU_ENC_RC_STATE_ON
;
793 trace_dpu_enc_rc(DRMID(drm_enc
), sw_event
,
794 dpu_enc
->idle_pc_supported
, dpu_enc
->rc_state
,
797 mutex_unlock(&dpu_enc
->rc_lock
);
800 case DPU_ENC_RC_EVENT_FRAME_DONE
:
802 * mutex lock is not used as this event happens at interrupt
803 * context. And locking is not required as, the other events
804 * like KICKOFF and STOP does a wait-for-idle before executing
805 * the resource_control
807 if (dpu_enc
->rc_state
!= DPU_ENC_RC_STATE_ON
) {
808 DRM_DEBUG_KMS("id:%d, sw_event:%d,rc:%d-unexpected\n",
809 DRMID(drm_enc
), sw_event
,
815 * schedule off work item only when there are no
818 if (dpu_crtc_frame_pending(drm_enc
->crtc
) > 1) {
819 DRM_DEBUG_KMS("id:%d skip schedule work\n",
824 queue_delayed_work(priv
->wq
, &dpu_enc
->delayed_off_work
,
825 msecs_to_jiffies(dpu_enc
->idle_timeout
));
827 trace_dpu_enc_rc(DRMID(drm_enc
), sw_event
,
828 dpu_enc
->idle_pc_supported
, dpu_enc
->rc_state
,
832 case DPU_ENC_RC_EVENT_PRE_STOP
:
833 /* cancel delayed off work, if any */
834 if (cancel_delayed_work_sync(&dpu_enc
->delayed_off_work
))
835 DPU_DEBUG_ENC(dpu_enc
, "sw_event:%d, work cancelled\n",
838 mutex_lock(&dpu_enc
->rc_lock
);
841 dpu_enc
->rc_state
== DPU_ENC_RC_STATE_IDLE
) {
842 _dpu_encoder_irq_control(drm_enc
, true);
844 /* skip if is already OFF or IDLE, resources are off already */
845 else if (dpu_enc
->rc_state
== DPU_ENC_RC_STATE_OFF
||
846 dpu_enc
->rc_state
== DPU_ENC_RC_STATE_IDLE
) {
847 DRM_DEBUG_KMS("id:%u, sw_event:%d, rc in %d state\n",
848 DRMID(drm_enc
), sw_event
,
850 mutex_unlock(&dpu_enc
->rc_lock
);
854 dpu_enc
->rc_state
= DPU_ENC_RC_STATE_PRE_OFF
;
856 trace_dpu_enc_rc(DRMID(drm_enc
), sw_event
,
857 dpu_enc
->idle_pc_supported
, dpu_enc
->rc_state
,
860 mutex_unlock(&dpu_enc
->rc_lock
);
863 case DPU_ENC_RC_EVENT_STOP
:
864 mutex_lock(&dpu_enc
->rc_lock
);
866 /* return if the resource control is already in OFF state */
867 if (dpu_enc
->rc_state
== DPU_ENC_RC_STATE_OFF
) {
868 DRM_DEBUG_KMS("id: %u, sw_event:%d, rc in OFF state\n",
869 DRMID(drm_enc
), sw_event
);
870 mutex_unlock(&dpu_enc
->rc_lock
);
872 } else if (dpu_enc
->rc_state
== DPU_ENC_RC_STATE_ON
) {
873 DRM_ERROR("id: %u, sw_event:%d, rc in state %d\n",
874 DRMID(drm_enc
), sw_event
, dpu_enc
->rc_state
);
875 mutex_unlock(&dpu_enc
->rc_lock
);
880 * expect to arrive here only if in either idle state or pre-off
881 * and in IDLE state the resources are already disabled
883 if (dpu_enc
->rc_state
== DPU_ENC_RC_STATE_PRE_OFF
)
884 _dpu_encoder_resource_control_helper(drm_enc
, false);
886 dpu_enc
->rc_state
= DPU_ENC_RC_STATE_OFF
;
888 trace_dpu_enc_rc(DRMID(drm_enc
), sw_event
,
889 dpu_enc
->idle_pc_supported
, dpu_enc
->rc_state
,
892 mutex_unlock(&dpu_enc
->rc_lock
);
895 case DPU_ENC_RC_EVENT_ENTER_IDLE
:
896 mutex_lock(&dpu_enc
->rc_lock
);
898 if (dpu_enc
->rc_state
!= DPU_ENC_RC_STATE_ON
) {
899 DRM_ERROR("id: %u, sw_event:%d, rc:%d !ON state\n",
900 DRMID(drm_enc
), sw_event
, dpu_enc
->rc_state
);
901 mutex_unlock(&dpu_enc
->rc_lock
);
906 * if we are in ON but a frame was just kicked off,
907 * ignore the IDLE event, it's probably a stale timer event
909 if (dpu_enc
->frame_busy_mask
[0]) {
910 DRM_ERROR("id:%u, sw_event:%d, rc:%d frame pending\n",
911 DRMID(drm_enc
), sw_event
, dpu_enc
->rc_state
);
912 mutex_unlock(&dpu_enc
->rc_lock
);
917 _dpu_encoder_irq_control(drm_enc
, false);
919 _dpu_encoder_resource_control_helper(drm_enc
, false);
921 dpu_enc
->rc_state
= DPU_ENC_RC_STATE_IDLE
;
923 trace_dpu_enc_rc(DRMID(drm_enc
), sw_event
,
924 dpu_enc
->idle_pc_supported
, dpu_enc
->rc_state
,
927 mutex_unlock(&dpu_enc
->rc_lock
);
931 DRM_ERROR("id:%u, unexpected sw_event: %d\n", DRMID(drm_enc
),
933 trace_dpu_enc_rc(DRMID(drm_enc
), sw_event
,
934 dpu_enc
->idle_pc_supported
, dpu_enc
->rc_state
,
939 trace_dpu_enc_rc(DRMID(drm_enc
), sw_event
,
940 dpu_enc
->idle_pc_supported
, dpu_enc
->rc_state
,
945 static void dpu_encoder_virt_mode_set(struct drm_encoder
*drm_enc
,
946 struct drm_display_mode
*mode
,
947 struct drm_display_mode
*adj_mode
)
949 struct dpu_encoder_virt
*dpu_enc
;
950 struct msm_drm_private
*priv
;
951 struct dpu_kms
*dpu_kms
;
952 struct list_head
*connector_list
;
953 struct drm_connector
*conn
= NULL
, *conn_iter
;
954 struct drm_crtc
*drm_crtc
;
955 struct dpu_crtc_state
*cstate
;
956 struct dpu_rm_hw_iter hw_iter
;
957 struct msm_display_topology topology
;
958 struct dpu_hw_ctl
*hw_ctl
[MAX_CHANNELS_PER_ENC
] = { NULL
};
959 struct dpu_hw_mixer
*hw_lm
[MAX_CHANNELS_PER_ENC
] = { NULL
};
960 int num_lm
= 0, num_ctl
= 0;
964 DPU_ERROR("invalid encoder\n");
968 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
969 DPU_DEBUG_ENC(dpu_enc
, "\n");
971 priv
= drm_enc
->dev
->dev_private
;
972 dpu_kms
= to_dpu_kms(priv
->kms
);
973 connector_list
= &dpu_kms
->dev
->mode_config
.connector_list
;
975 trace_dpu_enc_mode_set(DRMID(drm_enc
));
977 list_for_each_entry(conn_iter
, connector_list
, head
)
978 if (conn_iter
->encoder
== drm_enc
)
982 DPU_ERROR_ENC(dpu_enc
, "failed to find attached connector\n");
984 } else if (!conn
->state
) {
985 DPU_ERROR_ENC(dpu_enc
, "invalid connector state\n");
989 drm_for_each_crtc(drm_crtc
, drm_enc
->dev
)
990 if (drm_crtc
->state
->encoder_mask
& drm_encoder_mask(drm_enc
))
993 topology
= dpu_encoder_get_topology(dpu_enc
, dpu_kms
, adj_mode
);
995 /* Reserve dynamic resources now. Indicating non-AtomicTest phase */
996 ret
= dpu_rm_reserve(&dpu_kms
->rm
, drm_enc
, drm_crtc
->state
,
999 DPU_ERROR_ENC(dpu_enc
,
1000 "failed to reserve hw resources, %d\n", ret
);
1004 dpu_rm_init_hw_iter(&hw_iter
, drm_enc
->base
.id
, DPU_HW_BLK_PINGPONG
);
1005 for (i
= 0; i
< MAX_CHANNELS_PER_ENC
; i
++) {
1006 dpu_enc
->hw_pp
[i
] = NULL
;
1007 if (!dpu_rm_get_hw(&dpu_kms
->rm
, &hw_iter
))
1009 dpu_enc
->hw_pp
[i
] = (struct dpu_hw_pingpong
*) hw_iter
.hw
;
1012 dpu_rm_init_hw_iter(&hw_iter
, drm_enc
->base
.id
, DPU_HW_BLK_CTL
);
1013 for (i
= 0; i
< MAX_CHANNELS_PER_ENC
; i
++) {
1014 if (!dpu_rm_get_hw(&dpu_kms
->rm
, &hw_iter
))
1016 hw_ctl
[i
] = (struct dpu_hw_ctl
*)hw_iter
.hw
;
1020 dpu_rm_init_hw_iter(&hw_iter
, drm_enc
->base
.id
, DPU_HW_BLK_LM
);
1021 for (i
= 0; i
< MAX_CHANNELS_PER_ENC
; i
++) {
1022 if (!dpu_rm_get_hw(&dpu_kms
->rm
, &hw_iter
))
1024 hw_lm
[i
] = (struct dpu_hw_mixer
*)hw_iter
.hw
;
1028 cstate
= to_dpu_crtc_state(drm_crtc
->state
);
1030 for (i
= 0; i
< num_lm
; i
++) {
1031 int ctl_idx
= (i
< num_ctl
) ? i
: (num_ctl
-1);
1033 cstate
->mixers
[i
].hw_lm
= hw_lm
[i
];
1034 cstate
->mixers
[i
].lm_ctl
= hw_ctl
[ctl_idx
];
1037 cstate
->num_mixers
= num_lm
;
1039 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
1040 struct dpu_encoder_phys
*phys
= dpu_enc
->phys_encs
[i
];
1043 if (!dpu_enc
->hw_pp
[i
]) {
1044 DPU_ERROR_ENC(dpu_enc
, "no pp block assigned"
1050 DPU_ERROR_ENC(dpu_enc
, "no ctl block assigned"
1055 phys
->hw_pp
= dpu_enc
->hw_pp
[i
];
1056 phys
->hw_ctl
= hw_ctl
[i
];
1058 dpu_rm_init_hw_iter(&hw_iter
, drm_enc
->base
.id
,
1060 for (j
= 0; j
< MAX_CHANNELS_PER_ENC
; j
++) {
1061 struct dpu_hw_intf
*hw_intf
;
1063 if (!dpu_rm_get_hw(&dpu_kms
->rm
, &hw_iter
))
1066 hw_intf
= (struct dpu_hw_intf
*)hw_iter
.hw
;
1067 if (hw_intf
->idx
== phys
->intf_idx
)
1068 phys
->hw_intf
= hw_intf
;
1071 if (!phys
->hw_intf
) {
1072 DPU_ERROR_ENC(dpu_enc
,
1073 "no intf block assigned at idx: %d\n",
1078 phys
->connector
= conn
->state
->connector
;
1079 if (phys
->ops
.mode_set
)
1080 phys
->ops
.mode_set(phys
, mode
, adj_mode
);
1084 dpu_enc
->mode_set_complete
= true;
1087 dpu_rm_release(&dpu_kms
->rm
, drm_enc
);
1090 static void _dpu_encoder_virt_enable_helper(struct drm_encoder
*drm_enc
)
1092 struct dpu_encoder_virt
*dpu_enc
= NULL
;
1093 struct msm_drm_private
*priv
;
1094 struct dpu_kms
*dpu_kms
;
1096 if (!drm_enc
|| !drm_enc
->dev
|| !drm_enc
->dev
->dev_private
) {
1097 DPU_ERROR("invalid parameters\n");
1101 priv
= drm_enc
->dev
->dev_private
;
1102 dpu_kms
= to_dpu_kms(priv
->kms
);
1104 DPU_ERROR("invalid dpu_kms\n");
1108 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
1109 if (!dpu_enc
|| !dpu_enc
->cur_master
) {
1110 DPU_ERROR("invalid dpu encoder/master\n");
1114 if (dpu_enc
->cur_master
->hw_mdptop
&&
1115 dpu_enc
->cur_master
->hw_mdptop
->ops
.reset_ubwc
)
1116 dpu_enc
->cur_master
->hw_mdptop
->ops
.reset_ubwc(
1117 dpu_enc
->cur_master
->hw_mdptop
,
1120 _dpu_encoder_update_vsync_source(dpu_enc
, &dpu_enc
->disp_info
);
1123 void dpu_encoder_virt_runtime_resume(struct drm_encoder
*drm_enc
)
1125 struct dpu_encoder_virt
*dpu_enc
= to_dpu_encoder_virt(drm_enc
);
1127 mutex_lock(&dpu_enc
->enc_lock
);
1129 if (!dpu_enc
->enabled
)
1132 if (dpu_enc
->cur_slave
&& dpu_enc
->cur_slave
->ops
.restore
)
1133 dpu_enc
->cur_slave
->ops
.restore(dpu_enc
->cur_slave
);
1134 if (dpu_enc
->cur_master
&& dpu_enc
->cur_master
->ops
.restore
)
1135 dpu_enc
->cur_master
->ops
.restore(dpu_enc
->cur_master
);
1137 _dpu_encoder_virt_enable_helper(drm_enc
);
1140 mutex_unlock(&dpu_enc
->enc_lock
);
1143 static void dpu_encoder_virt_enable(struct drm_encoder
*drm_enc
)
1145 struct dpu_encoder_virt
*dpu_enc
= NULL
;
1147 struct drm_display_mode
*cur_mode
= NULL
;
1150 DPU_ERROR("invalid encoder\n");
1153 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
1155 mutex_lock(&dpu_enc
->enc_lock
);
1156 cur_mode
= &dpu_enc
->base
.crtc
->state
->adjusted_mode
;
1158 trace_dpu_enc_enable(DRMID(drm_enc
), cur_mode
->hdisplay
,
1159 cur_mode
->vdisplay
);
1161 /* always enable slave encoder before master */
1162 if (dpu_enc
->cur_slave
&& dpu_enc
->cur_slave
->ops
.enable
)
1163 dpu_enc
->cur_slave
->ops
.enable(dpu_enc
->cur_slave
);
1165 if (dpu_enc
->cur_master
&& dpu_enc
->cur_master
->ops
.enable
)
1166 dpu_enc
->cur_master
->ops
.enable(dpu_enc
->cur_master
);
1168 ret
= dpu_encoder_resource_control(drm_enc
, DPU_ENC_RC_EVENT_KICKOFF
);
1170 DPU_ERROR_ENC(dpu_enc
, "dpu resource control failed: %d\n",
1175 _dpu_encoder_virt_enable_helper(drm_enc
);
1177 dpu_enc
->enabled
= true;
1180 mutex_unlock(&dpu_enc
->enc_lock
);
1183 static void dpu_encoder_virt_disable(struct drm_encoder
*drm_enc
)
1185 struct dpu_encoder_virt
*dpu_enc
= NULL
;
1186 struct msm_drm_private
*priv
;
1187 struct dpu_kms
*dpu_kms
;
1188 struct drm_display_mode
*mode
;
1192 DPU_ERROR("invalid encoder\n");
1194 } else if (!drm_enc
->dev
) {
1195 DPU_ERROR("invalid dev\n");
1197 } else if (!drm_enc
->dev
->dev_private
) {
1198 DPU_ERROR("invalid dev_private\n");
1202 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
1203 DPU_DEBUG_ENC(dpu_enc
, "\n");
1205 mutex_lock(&dpu_enc
->enc_lock
);
1206 dpu_enc
->enabled
= false;
1208 mode
= &drm_enc
->crtc
->state
->adjusted_mode
;
1210 priv
= drm_enc
->dev
->dev_private
;
1211 dpu_kms
= to_dpu_kms(priv
->kms
);
1213 trace_dpu_enc_disable(DRMID(drm_enc
));
1216 dpu_encoder_wait_for_event(drm_enc
, MSM_ENC_TX_COMPLETE
);
1218 dpu_encoder_resource_control(drm_enc
, DPU_ENC_RC_EVENT_PRE_STOP
);
1220 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
1221 struct dpu_encoder_phys
*phys
= dpu_enc
->phys_encs
[i
];
1223 if (phys
&& phys
->ops
.disable
)
1224 phys
->ops
.disable(phys
);
1227 /* after phys waits for frame-done, should be no more frames pending */
1228 if (atomic_xchg(&dpu_enc
->frame_done_timeout_ms
, 0)) {
1229 DPU_ERROR("enc%d timeout pending\n", drm_enc
->base
.id
);
1230 del_timer_sync(&dpu_enc
->frame_done_timer
);
1233 dpu_encoder_resource_control(drm_enc
, DPU_ENC_RC_EVENT_STOP
);
1235 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
1236 if (dpu_enc
->phys_encs
[i
])
1237 dpu_enc
->phys_encs
[i
]->connector
= NULL
;
1240 DPU_DEBUG_ENC(dpu_enc
, "encoder disabled\n");
1242 dpu_rm_release(&dpu_kms
->rm
, drm_enc
);
1244 mutex_unlock(&dpu_enc
->enc_lock
);
1247 static enum dpu_intf
dpu_encoder_get_intf(struct dpu_mdss_cfg
*catalog
,
1248 enum dpu_intf_type type
, u32 controller_id
)
1252 for (i
= 0; i
< catalog
->intf_count
; i
++) {
1253 if (catalog
->intf
[i
].type
== type
1254 && catalog
->intf
[i
].controller_id
== controller_id
) {
1255 return catalog
->intf
[i
].id
;
1262 static void dpu_encoder_vblank_callback(struct drm_encoder
*drm_enc
,
1263 struct dpu_encoder_phys
*phy_enc
)
1265 struct dpu_encoder_virt
*dpu_enc
= NULL
;
1266 unsigned long lock_flags
;
1268 if (!drm_enc
|| !phy_enc
)
1271 DPU_ATRACE_BEGIN("encoder_vblank_callback");
1272 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
1274 spin_lock_irqsave(&dpu_enc
->enc_spinlock
, lock_flags
);
1276 dpu_crtc_vblank_callback(dpu_enc
->crtc
);
1277 spin_unlock_irqrestore(&dpu_enc
->enc_spinlock
, lock_flags
);
1279 atomic_inc(&phy_enc
->vsync_cnt
);
1280 DPU_ATRACE_END("encoder_vblank_callback");
1283 static void dpu_encoder_underrun_callback(struct drm_encoder
*drm_enc
,
1284 struct dpu_encoder_phys
*phy_enc
)
1289 DPU_ATRACE_BEGIN("encoder_underrun_callback");
1290 atomic_inc(&phy_enc
->underrun_cnt
);
1291 trace_dpu_enc_underrun_cb(DRMID(drm_enc
),
1292 atomic_read(&phy_enc
->underrun_cnt
));
1293 DPU_ATRACE_END("encoder_underrun_callback");
1296 void dpu_encoder_assign_crtc(struct drm_encoder
*drm_enc
, struct drm_crtc
*crtc
)
1298 struct dpu_encoder_virt
*dpu_enc
= to_dpu_encoder_virt(drm_enc
);
1299 unsigned long lock_flags
;
1301 spin_lock_irqsave(&dpu_enc
->enc_spinlock
, lock_flags
);
1302 /* crtc should always be cleared before re-assigning */
1303 WARN_ON(crtc
&& dpu_enc
->crtc
);
1304 dpu_enc
->crtc
= crtc
;
1305 spin_unlock_irqrestore(&dpu_enc
->enc_spinlock
, lock_flags
);
1308 void dpu_encoder_toggle_vblank_for_crtc(struct drm_encoder
*drm_enc
,
1309 struct drm_crtc
*crtc
, bool enable
)
1311 struct dpu_encoder_virt
*dpu_enc
= to_dpu_encoder_virt(drm_enc
);
1312 unsigned long lock_flags
;
1315 trace_dpu_enc_vblank_cb(DRMID(drm_enc
), enable
);
1317 spin_lock_irqsave(&dpu_enc
->enc_spinlock
, lock_flags
);
1318 if (dpu_enc
->crtc
!= crtc
) {
1319 spin_unlock_irqrestore(&dpu_enc
->enc_spinlock
, lock_flags
);
1322 spin_unlock_irqrestore(&dpu_enc
->enc_spinlock
, lock_flags
);
1324 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
1325 struct dpu_encoder_phys
*phys
= dpu_enc
->phys_encs
[i
];
1327 if (phys
&& phys
->ops
.control_vblank_irq
)
1328 phys
->ops
.control_vblank_irq(phys
, enable
);
1332 void dpu_encoder_register_frame_event_callback(struct drm_encoder
*drm_enc
,
1333 void (*frame_event_cb
)(void *, u32 event
),
1334 void *frame_event_cb_data
)
1336 struct dpu_encoder_virt
*dpu_enc
= to_dpu_encoder_virt(drm_enc
);
1337 unsigned long lock_flags
;
1340 enable
= frame_event_cb
? true : false;
1343 DPU_ERROR("invalid encoder\n");
1346 trace_dpu_enc_frame_event_cb(DRMID(drm_enc
), enable
);
1348 spin_lock_irqsave(&dpu_enc
->enc_spinlock
, lock_flags
);
1349 dpu_enc
->crtc_frame_event_cb
= frame_event_cb
;
1350 dpu_enc
->crtc_frame_event_cb_data
= frame_event_cb_data
;
1351 spin_unlock_irqrestore(&dpu_enc
->enc_spinlock
, lock_flags
);
1354 static void dpu_encoder_frame_done_callback(
1355 struct drm_encoder
*drm_enc
,
1356 struct dpu_encoder_phys
*ready_phys
, u32 event
)
1358 struct dpu_encoder_virt
*dpu_enc
= to_dpu_encoder_virt(drm_enc
);
1361 if (event
& (DPU_ENCODER_FRAME_EVENT_DONE
1362 | DPU_ENCODER_FRAME_EVENT_ERROR
1363 | DPU_ENCODER_FRAME_EVENT_PANEL_DEAD
)) {
1365 if (!dpu_enc
->frame_busy_mask
[0]) {
1367 * suppress frame_done without waiter,
1368 * likely autorefresh
1370 trace_dpu_enc_frame_done_cb_not_busy(DRMID(drm_enc
),
1371 event
, ready_phys
->intf_idx
);
1375 /* One of the physical encoders has become idle */
1376 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
1377 if (dpu_enc
->phys_encs
[i
] == ready_phys
) {
1378 trace_dpu_enc_frame_done_cb(DRMID(drm_enc
), i
,
1379 dpu_enc
->frame_busy_mask
[0]);
1380 clear_bit(i
, dpu_enc
->frame_busy_mask
);
1384 if (!dpu_enc
->frame_busy_mask
[0]) {
1385 atomic_set(&dpu_enc
->frame_done_timeout_ms
, 0);
1386 del_timer(&dpu_enc
->frame_done_timer
);
1388 dpu_encoder_resource_control(drm_enc
,
1389 DPU_ENC_RC_EVENT_FRAME_DONE
);
1391 if (dpu_enc
->crtc_frame_event_cb
)
1392 dpu_enc
->crtc_frame_event_cb(
1393 dpu_enc
->crtc_frame_event_cb_data
,
1397 if (dpu_enc
->crtc_frame_event_cb
)
1398 dpu_enc
->crtc_frame_event_cb(
1399 dpu_enc
->crtc_frame_event_cb_data
, event
);
1403 static void dpu_encoder_off_work(struct work_struct
*work
)
1405 struct dpu_encoder_virt
*dpu_enc
= container_of(work
,
1406 struct dpu_encoder_virt
, delayed_off_work
.work
);
1409 DPU_ERROR("invalid dpu encoder\n");
1413 dpu_encoder_resource_control(&dpu_enc
->base
,
1414 DPU_ENC_RC_EVENT_ENTER_IDLE
);
1416 dpu_encoder_frame_done_callback(&dpu_enc
->base
, NULL
,
1417 DPU_ENCODER_FRAME_EVENT_IDLE
);
1421 * _dpu_encoder_trigger_flush - trigger flush for a physical encoder
1422 * drm_enc: Pointer to drm encoder structure
1423 * phys: Pointer to physical encoder structure
1424 * extra_flush_bits: Additional bit mask to include in flush trigger
1426 static void _dpu_encoder_trigger_flush(struct drm_encoder
*drm_enc
,
1427 struct dpu_encoder_phys
*phys
, uint32_t extra_flush_bits
,
1430 struct dpu_hw_ctl
*ctl
;
1431 int pending_kickoff_cnt
;
1434 if (!drm_enc
|| !phys
) {
1435 DPU_ERROR("invalid argument(s), drm_enc %d, phys_enc %d\n",
1436 drm_enc
!= 0, phys
!= 0);
1441 DPU_ERROR("invalid pingpong hw\n");
1446 if (!ctl
|| !ctl
->ops
.trigger_flush
) {
1447 DPU_ERROR("missing trigger cb\n");
1452 pending_kickoff_cnt
= dpu_encoder_phys_inc_pending(phys
);
1454 pending_kickoff_cnt
= atomic_read(&phys
->pending_kickoff_cnt
);
1456 if (extra_flush_bits
&& ctl
->ops
.update_pending_flush
)
1457 ctl
->ops
.update_pending_flush(ctl
, extra_flush_bits
);
1459 ctl
->ops
.trigger_flush(ctl
);
1461 if (ctl
->ops
.get_pending_flush
)
1462 ret
= ctl
->ops
.get_pending_flush(ctl
);
1464 trace_dpu_enc_trigger_flush(DRMID(drm_enc
), phys
->intf_idx
,
1465 pending_kickoff_cnt
, ctl
->idx
,
1466 extra_flush_bits
, ret
);
1470 * _dpu_encoder_trigger_start - trigger start for a physical encoder
1471 * phys: Pointer to physical encoder structure
1473 static void _dpu_encoder_trigger_start(struct dpu_encoder_phys
*phys
)
1476 DPU_ERROR("invalid argument(s)\n");
1481 DPU_ERROR("invalid pingpong hw\n");
1485 if (phys
->ops
.trigger_start
&& phys
->enable_state
!= DPU_ENC_DISABLED
)
1486 phys
->ops
.trigger_start(phys
);
1489 void dpu_encoder_helper_trigger_start(struct dpu_encoder_phys
*phys_enc
)
1491 struct dpu_hw_ctl
*ctl
;
1494 DPU_ERROR("invalid encoder\n");
1498 ctl
= phys_enc
->hw_ctl
;
1499 if (ctl
&& ctl
->ops
.trigger_start
) {
1500 ctl
->ops
.trigger_start(ctl
);
1501 trace_dpu_enc_trigger_start(DRMID(phys_enc
->parent
), ctl
->idx
);
1505 static int dpu_encoder_helper_wait_event_timeout(
1508 struct dpu_encoder_wait_info
*info
)
1511 s64 expected_time
= ktime_to_ms(ktime_get()) + info
->timeout_ms
;
1512 s64 jiffies
= msecs_to_jiffies(info
->timeout_ms
);
1516 rc
= wait_event_timeout(*(info
->wq
),
1517 atomic_read(info
->atomic_cnt
) == 0, jiffies
);
1518 time
= ktime_to_ms(ktime_get());
1520 trace_dpu_enc_wait_event_timeout(drm_id
, hw_id
, rc
, time
,
1522 atomic_read(info
->atomic_cnt
));
1523 /* If we timed out, counter is valid and time is less, wait again */
1524 } while (atomic_read(info
->atomic_cnt
) && (rc
== 0) &&
1525 (time
< expected_time
));
1530 static void dpu_encoder_helper_hw_reset(struct dpu_encoder_phys
*phys_enc
)
1532 struct dpu_encoder_virt
*dpu_enc
;
1533 struct dpu_hw_ctl
*ctl
;
1537 DPU_ERROR("invalid encoder\n");
1540 dpu_enc
= to_dpu_encoder_virt(phys_enc
->parent
);
1541 ctl
= phys_enc
->hw_ctl
;
1543 if (!ctl
|| !ctl
->ops
.reset
)
1546 DRM_DEBUG_KMS("id:%u ctl %d reset\n", DRMID(phys_enc
->parent
),
1549 rc
= ctl
->ops
.reset(ctl
);
1551 DPU_ERROR_ENC(dpu_enc
, "ctl %d reset failure\n", ctl
->idx
);
1553 phys_enc
->enable_state
= DPU_ENC_ENABLED
;
1557 * _dpu_encoder_kickoff_phys - handle physical encoder kickoff
1558 * Iterate through the physical encoders and perform consolidated flush
1559 * and/or control start triggering as needed. This is done in the virtual
1560 * encoder rather than the individual physical ones in order to handle
1561 * use cases that require visibility into multiple physical encoders at
1563 * dpu_enc: Pointer to virtual encoder structure
1565 static void _dpu_encoder_kickoff_phys(struct dpu_encoder_virt
*dpu_enc
,
1568 struct dpu_hw_ctl
*ctl
;
1569 uint32_t i
, pending_flush
;
1570 unsigned long lock_flags
;
1573 DPU_ERROR("invalid encoder\n");
1577 pending_flush
= 0x0;
1579 /* update pending counts and trigger kickoff ctl flush atomically */
1580 spin_lock_irqsave(&dpu_enc
->enc_spinlock
, lock_flags
);
1582 /* don't perform flush/start operations for slave encoders */
1583 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
1584 struct dpu_encoder_phys
*phys
= dpu_enc
->phys_encs
[i
];
1586 if (!phys
|| phys
->enable_state
== DPU_ENC_DISABLED
)
1594 * This is cleared in frame_done worker, which isn't invoked
1595 * for async commits. So don't set this for async, since it'll
1596 * roll over to the next commit.
1598 if (!async
&& phys
->split_role
!= ENC_ROLE_SLAVE
)
1599 set_bit(i
, dpu_enc
->frame_busy_mask
);
1601 if (!phys
->ops
.needs_single_flush
||
1602 !phys
->ops
.needs_single_flush(phys
))
1603 _dpu_encoder_trigger_flush(&dpu_enc
->base
, phys
, 0x0,
1605 else if (ctl
->ops
.get_pending_flush
)
1606 pending_flush
|= ctl
->ops
.get_pending_flush(ctl
);
1609 /* for split flush, combine pending flush masks and send to master */
1610 if (pending_flush
&& dpu_enc
->cur_master
) {
1611 _dpu_encoder_trigger_flush(
1613 dpu_enc
->cur_master
,
1614 pending_flush
, async
);
1617 _dpu_encoder_trigger_start(dpu_enc
->cur_master
);
1619 spin_unlock_irqrestore(&dpu_enc
->enc_spinlock
, lock_flags
);
1622 void dpu_encoder_trigger_kickoff_pending(struct drm_encoder
*drm_enc
)
1624 struct dpu_encoder_virt
*dpu_enc
;
1625 struct dpu_encoder_phys
*phys
;
1627 struct dpu_hw_ctl
*ctl
;
1628 struct msm_display_info
*disp_info
;
1631 DPU_ERROR("invalid encoder\n");
1634 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
1635 disp_info
= &dpu_enc
->disp_info
;
1637 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
1638 phys
= dpu_enc
->phys_encs
[i
];
1640 if (phys
&& phys
->hw_ctl
) {
1642 if (ctl
->ops
.clear_pending_flush
)
1643 ctl
->ops
.clear_pending_flush(ctl
);
1645 /* update only for command mode primary ctl */
1646 if ((phys
== dpu_enc
->cur_master
) &&
1647 (disp_info
->capabilities
& MSM_DISPLAY_CAP_CMD_MODE
)
1648 && ctl
->ops
.trigger_pending
)
1649 ctl
->ops
.trigger_pending(ctl
);
1654 static u32
_dpu_encoder_calculate_linetime(struct dpu_encoder_virt
*dpu_enc
,
1655 struct drm_display_mode
*mode
)
1662 * For linetime calculation, only operate on master encoder.
1664 if (!dpu_enc
->cur_master
)
1667 if (!dpu_enc
->cur_master
->ops
.get_line_count
) {
1668 DPU_ERROR("get_line_count function not defined\n");
1672 pclk_rate
= mode
->clock
; /* pixel clock in kHz */
1673 if (pclk_rate
== 0) {
1674 DPU_ERROR("pclk is 0, cannot calculate line time\n");
1678 pclk_period
= DIV_ROUND_UP_ULL(1000000000ull, pclk_rate
);
1679 if (pclk_period
== 0) {
1680 DPU_ERROR("pclk period is 0\n");
1685 * Line time calculation based on Pixel clock and HTOTAL.
1686 * Final unit is in ns.
1688 line_time
= (pclk_period
* mode
->htotal
) / 1000;
1689 if (line_time
== 0) {
1690 DPU_ERROR("line time calculation is 0\n");
1694 DPU_DEBUG_ENC(dpu_enc
,
1695 "clk_rate=%lldkHz, clk_period=%d, linetime=%dns\n",
1696 pclk_rate
, pclk_period
, line_time
);
1701 static int _dpu_encoder_wakeup_time(struct drm_encoder
*drm_enc
,
1702 ktime_t
*wakeup_time
)
1704 struct drm_display_mode
*mode
;
1705 struct dpu_encoder_virt
*dpu_enc
;
1708 u32 vtotal
, time_to_vsync
;
1711 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
1713 if (!drm_enc
->crtc
|| !drm_enc
->crtc
->state
) {
1714 DPU_ERROR("crtc/crtc state object is NULL\n");
1717 mode
= &drm_enc
->crtc
->state
->adjusted_mode
;
1719 line_time
= _dpu_encoder_calculate_linetime(dpu_enc
, mode
);
1723 cur_line
= dpu_enc
->cur_master
->ops
.get_line_count(dpu_enc
->cur_master
);
1725 vtotal
= mode
->vtotal
;
1726 if (cur_line
>= vtotal
)
1727 time_to_vsync
= line_time
* vtotal
;
1729 time_to_vsync
= line_time
* (vtotal
- cur_line
);
1731 if (time_to_vsync
== 0) {
1732 DPU_ERROR("time to vsync should not be zero, vtotal=%d\n",
1737 cur_time
= ktime_get();
1738 *wakeup_time
= ktime_add_ns(cur_time
, time_to_vsync
);
1740 DPU_DEBUG_ENC(dpu_enc
,
1741 "cur_line=%u vtotal=%u time_to_vsync=%u, cur_time=%lld, wakeup_time=%lld\n",
1742 cur_line
, vtotal
, time_to_vsync
,
1743 ktime_to_ms(cur_time
),
1744 ktime_to_ms(*wakeup_time
));
1748 static void dpu_encoder_vsync_event_handler(struct timer_list
*t
)
1750 struct dpu_encoder_virt
*dpu_enc
= from_timer(dpu_enc
, t
,
1752 struct drm_encoder
*drm_enc
= &dpu_enc
->base
;
1753 struct msm_drm_private
*priv
;
1754 struct msm_drm_thread
*event_thread
;
1756 if (!drm_enc
->dev
|| !drm_enc
->dev
->dev_private
||
1758 DPU_ERROR("invalid parameters\n");
1762 priv
= drm_enc
->dev
->dev_private
;
1764 if (drm_enc
->crtc
->index
>= ARRAY_SIZE(priv
->event_thread
)) {
1765 DPU_ERROR("invalid crtc index\n");
1768 event_thread
= &priv
->event_thread
[drm_enc
->crtc
->index
];
1769 if (!event_thread
) {
1770 DPU_ERROR("event_thread not found for crtc:%d\n",
1771 drm_enc
->crtc
->index
);
1775 del_timer(&dpu_enc
->vsync_event_timer
);
1778 static void dpu_encoder_vsync_event_work_handler(struct kthread_work
*work
)
1780 struct dpu_encoder_virt
*dpu_enc
= container_of(work
,
1781 struct dpu_encoder_virt
, vsync_event_work
);
1782 ktime_t wakeup_time
;
1785 DPU_ERROR("invalid dpu encoder\n");
1789 if (_dpu_encoder_wakeup_time(&dpu_enc
->base
, &wakeup_time
))
1792 trace_dpu_enc_vsync_event_work(DRMID(&dpu_enc
->base
), wakeup_time
);
1793 mod_timer(&dpu_enc
->vsync_event_timer
,
1794 nsecs_to_jiffies(ktime_to_ns(wakeup_time
)));
1797 void dpu_encoder_prepare_for_kickoff(struct drm_encoder
*drm_enc
, bool async
)
1799 struct dpu_encoder_virt
*dpu_enc
;
1800 struct dpu_encoder_phys
*phys
;
1801 bool needs_hw_reset
= false;
1805 DPU_ERROR("invalid args\n");
1808 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
1810 trace_dpu_enc_prepare_kickoff(DRMID(drm_enc
));
1812 /* prepare for next kickoff, may include waiting on previous kickoff */
1813 DPU_ATRACE_BEGIN("enc_prepare_for_kickoff");
1814 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
1815 phys
= dpu_enc
->phys_encs
[i
];
1817 if (phys
->ops
.prepare_for_kickoff
)
1818 phys
->ops
.prepare_for_kickoff(phys
);
1819 if (phys
->enable_state
== DPU_ENC_ERR_NEEDS_HW_RESET
)
1820 needs_hw_reset
= true;
1823 DPU_ATRACE_END("enc_prepare_for_kickoff");
1825 dpu_encoder_resource_control(drm_enc
, DPU_ENC_RC_EVENT_KICKOFF
);
1827 /* if any phys needs reset, reset all phys, in-order */
1828 if (needs_hw_reset
) {
1829 trace_dpu_enc_prepare_kickoff_reset(DRMID(drm_enc
));
1830 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
1831 dpu_encoder_helper_hw_reset(dpu_enc
->phys_encs
[i
]);
1836 void dpu_encoder_kickoff(struct drm_encoder
*drm_enc
, bool async
)
1838 struct dpu_encoder_virt
*dpu_enc
;
1839 struct dpu_encoder_phys
*phys
;
1840 ktime_t wakeup_time
;
1844 DPU_ERROR("invalid encoder\n");
1847 DPU_ATRACE_BEGIN("encoder_kickoff");
1848 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
1850 trace_dpu_enc_kickoff(DRMID(drm_enc
));
1853 * Asynchronous frames don't handle FRAME_DONE events. As such, they
1854 * shouldn't enable the frame_done watchdog since it will always time
1858 unsigned long timeout_ms
;
1859 timeout_ms
= DPU_ENCODER_FRAME_DONE_TIMEOUT_FRAMES
* 1000 /
1860 drm_mode_vrefresh(&drm_enc
->crtc
->state
->adjusted_mode
);
1862 atomic_set(&dpu_enc
->frame_done_timeout_ms
, timeout_ms
);
1863 mod_timer(&dpu_enc
->frame_done_timer
,
1864 jiffies
+ msecs_to_jiffies(timeout_ms
));
1867 /* All phys encs are ready to go, trigger the kickoff */
1868 _dpu_encoder_kickoff_phys(dpu_enc
, async
);
1870 /* allow phys encs to handle any post-kickoff business */
1871 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
1872 phys
= dpu_enc
->phys_encs
[i
];
1873 if (phys
&& phys
->ops
.handle_post_kickoff
)
1874 phys
->ops
.handle_post_kickoff(phys
);
1877 if (dpu_enc
->disp_info
.intf_type
== DRM_MODE_ENCODER_DSI
&&
1878 !_dpu_encoder_wakeup_time(drm_enc
, &wakeup_time
)) {
1879 trace_dpu_enc_early_kickoff(DRMID(drm_enc
),
1880 ktime_to_ms(wakeup_time
));
1881 mod_timer(&dpu_enc
->vsync_event_timer
,
1882 nsecs_to_jiffies(ktime_to_ns(wakeup_time
)));
1885 DPU_ATRACE_END("encoder_kickoff");
1888 void dpu_encoder_prepare_commit(struct drm_encoder
*drm_enc
)
1890 struct dpu_encoder_virt
*dpu_enc
;
1891 struct dpu_encoder_phys
*phys
;
1895 DPU_ERROR("invalid encoder\n");
1898 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
1900 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
1901 phys
= dpu_enc
->phys_encs
[i
];
1902 if (phys
&& phys
->ops
.prepare_commit
)
1903 phys
->ops
.prepare_commit(phys
);
1907 #ifdef CONFIG_DEBUG_FS
1908 static int _dpu_encoder_status_show(struct seq_file
*s
, void *data
)
1910 struct dpu_encoder_virt
*dpu_enc
= s
->private;
1913 mutex_lock(&dpu_enc
->enc_lock
);
1914 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
1915 struct dpu_encoder_phys
*phys
= dpu_enc
->phys_encs
[i
];
1920 seq_printf(s
, "intf:%d vsync:%8d underrun:%8d ",
1921 phys
->intf_idx
- INTF_0
,
1922 atomic_read(&phys
->vsync_cnt
),
1923 atomic_read(&phys
->underrun_cnt
));
1925 switch (phys
->intf_mode
) {
1926 case INTF_MODE_VIDEO
:
1927 seq_puts(s
, "mode: video\n");
1930 seq_puts(s
, "mode: command\n");
1933 seq_puts(s
, "mode: ???\n");
1937 mutex_unlock(&dpu_enc
->enc_lock
);
1942 static int _dpu_encoder_debugfs_status_open(struct inode
*inode
,
1945 return single_open(file
, _dpu_encoder_status_show
, inode
->i_private
);
1948 static int _dpu_encoder_init_debugfs(struct drm_encoder
*drm_enc
)
1950 struct dpu_encoder_virt
*dpu_enc
= to_dpu_encoder_virt(drm_enc
);
1951 struct msm_drm_private
*priv
;
1952 struct dpu_kms
*dpu_kms
;
1955 static const struct file_operations debugfs_status_fops
= {
1956 .open
= _dpu_encoder_debugfs_status_open
,
1958 .llseek
= seq_lseek
,
1959 .release
= single_release
,
1962 char name
[DPU_NAME_SIZE
];
1964 if (!drm_enc
->dev
|| !drm_enc
->dev
->dev_private
) {
1965 DPU_ERROR("invalid encoder or kms\n");
1969 priv
= drm_enc
->dev
->dev_private
;
1970 dpu_kms
= to_dpu_kms(priv
->kms
);
1972 snprintf(name
, DPU_NAME_SIZE
, "encoder%u", drm_enc
->base
.id
);
1974 /* create overall sub-directory for the encoder */
1975 dpu_enc
->debugfs_root
= debugfs_create_dir(name
,
1976 drm_enc
->dev
->primary
->debugfs_root
);
1977 if (!dpu_enc
->debugfs_root
)
1980 /* don't error check these */
1981 debugfs_create_file("status", 0600,
1982 dpu_enc
->debugfs_root
, dpu_enc
, &debugfs_status_fops
);
1984 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++)
1985 if (dpu_enc
->phys_encs
[i
] &&
1986 dpu_enc
->phys_encs
[i
]->ops
.late_register
)
1987 dpu_enc
->phys_encs
[i
]->ops
.late_register(
1988 dpu_enc
->phys_encs
[i
],
1989 dpu_enc
->debugfs_root
);
1994 static int _dpu_encoder_init_debugfs(struct drm_encoder
*drm_enc
)
2000 static int dpu_encoder_late_register(struct drm_encoder
*encoder
)
2002 return _dpu_encoder_init_debugfs(encoder
);
2005 static void dpu_encoder_early_unregister(struct drm_encoder
*encoder
)
2007 struct dpu_encoder_virt
*dpu_enc
= to_dpu_encoder_virt(encoder
);
2009 debugfs_remove_recursive(dpu_enc
->debugfs_root
);
2012 static int dpu_encoder_virt_add_phys_encs(
2014 struct dpu_encoder_virt
*dpu_enc
,
2015 struct dpu_enc_phys_init_params
*params
)
2017 struct dpu_encoder_phys
*enc
= NULL
;
2019 DPU_DEBUG_ENC(dpu_enc
, "\n");
2022 * We may create up to NUM_PHYS_ENCODER_TYPES physical encoder types
2023 * in this function, check up-front.
2025 if (dpu_enc
->num_phys_encs
+ NUM_PHYS_ENCODER_TYPES
>=
2026 ARRAY_SIZE(dpu_enc
->phys_encs
)) {
2027 DPU_ERROR_ENC(dpu_enc
, "too many physical encoders %d\n",
2028 dpu_enc
->num_phys_encs
);
2032 if (display_caps
& MSM_DISPLAY_CAP_VID_MODE
) {
2033 enc
= dpu_encoder_phys_vid_init(params
);
2035 if (IS_ERR_OR_NULL(enc
)) {
2036 DPU_ERROR_ENC(dpu_enc
, "failed to init vid enc: %ld\n",
2038 return enc
== 0 ? -EINVAL
: PTR_ERR(enc
);
2041 dpu_enc
->phys_encs
[dpu_enc
->num_phys_encs
] = enc
;
2042 ++dpu_enc
->num_phys_encs
;
2045 if (display_caps
& MSM_DISPLAY_CAP_CMD_MODE
) {
2046 enc
= dpu_encoder_phys_cmd_init(params
);
2048 if (IS_ERR_OR_NULL(enc
)) {
2049 DPU_ERROR_ENC(dpu_enc
, "failed to init cmd enc: %ld\n",
2051 return enc
== 0 ? -EINVAL
: PTR_ERR(enc
);
2054 dpu_enc
->phys_encs
[dpu_enc
->num_phys_encs
] = enc
;
2055 ++dpu_enc
->num_phys_encs
;
2058 if (params
->split_role
== ENC_ROLE_SLAVE
)
2059 dpu_enc
->cur_slave
= enc
;
2061 dpu_enc
->cur_master
= enc
;
2066 static const struct dpu_encoder_virt_ops dpu_encoder_parent_ops
= {
2067 .handle_vblank_virt
= dpu_encoder_vblank_callback
,
2068 .handle_underrun_virt
= dpu_encoder_underrun_callback
,
2069 .handle_frame_done
= dpu_encoder_frame_done_callback
,
2072 static int dpu_encoder_setup_display(struct dpu_encoder_virt
*dpu_enc
,
2073 struct dpu_kms
*dpu_kms
,
2074 struct msm_display_info
*disp_info
)
2078 enum dpu_intf_type intf_type
;
2079 struct dpu_enc_phys_init_params phys_params
;
2081 if (!dpu_enc
|| !dpu_kms
) {
2082 DPU_ERROR("invalid arg(s), enc %d kms %d\n",
2083 dpu_enc
!= 0, dpu_kms
!= 0);
2087 dpu_enc
->cur_master
= NULL
;
2089 memset(&phys_params
, 0, sizeof(phys_params
));
2090 phys_params
.dpu_kms
= dpu_kms
;
2091 phys_params
.parent
= &dpu_enc
->base
;
2092 phys_params
.parent_ops
= &dpu_encoder_parent_ops
;
2093 phys_params
.enc_spinlock
= &dpu_enc
->enc_spinlock
;
2097 switch (disp_info
->intf_type
) {
2098 case DRM_MODE_ENCODER_DSI
:
2099 intf_type
= INTF_DSI
;
2102 DPU_ERROR_ENC(dpu_enc
, "unsupported display interface type\n");
2106 WARN_ON(disp_info
->num_of_h_tiles
< 1);
2108 DPU_DEBUG("dsi_info->num_of_h_tiles %d\n", disp_info
->num_of_h_tiles
);
2110 if ((disp_info
->capabilities
& MSM_DISPLAY_CAP_CMD_MODE
) ||
2111 (disp_info
->capabilities
& MSM_DISPLAY_CAP_VID_MODE
))
2112 dpu_enc
->idle_pc_supported
=
2113 dpu_kms
->catalog
->caps
->has_idle_pc
;
2115 mutex_lock(&dpu_enc
->enc_lock
);
2116 for (i
= 0; i
< disp_info
->num_of_h_tiles
&& !ret
; i
++) {
2118 * Left-most tile is at index 0, content is controller id
2119 * h_tile_instance_ids[2] = {0, 1}; DSI0 = left, DSI1 = right
2120 * h_tile_instance_ids[2] = {1, 0}; DSI1 = left, DSI0 = right
2122 u32 controller_id
= disp_info
->h_tile_instance
[i
];
2124 if (disp_info
->num_of_h_tiles
> 1) {
2126 phys_params
.split_role
= ENC_ROLE_MASTER
;
2128 phys_params
.split_role
= ENC_ROLE_SLAVE
;
2130 phys_params
.split_role
= ENC_ROLE_SOLO
;
2133 DPU_DEBUG("h_tile_instance %d = %d, split_role %d\n",
2134 i
, controller_id
, phys_params
.split_role
);
2136 phys_params
.intf_idx
= dpu_encoder_get_intf(dpu_kms
->catalog
,
2139 if (phys_params
.intf_idx
== INTF_MAX
) {
2140 DPU_ERROR_ENC(dpu_enc
, "could not get intf: type %d, id %d\n",
2141 intf_type
, controller_id
);
2146 ret
= dpu_encoder_virt_add_phys_encs(disp_info
->capabilities
,
2150 DPU_ERROR_ENC(dpu_enc
, "failed to add phys encs\n");
2154 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
2155 struct dpu_encoder_phys
*phys
= dpu_enc
->phys_encs
[i
];
2158 atomic_set(&phys
->vsync_cnt
, 0);
2159 atomic_set(&phys
->underrun_cnt
, 0);
2162 mutex_unlock(&dpu_enc
->enc_lock
);
2167 static void dpu_encoder_frame_done_timeout(struct timer_list
*t
)
2169 struct dpu_encoder_virt
*dpu_enc
= from_timer(dpu_enc
, t
,
2171 struct drm_encoder
*drm_enc
= &dpu_enc
->base
;
2172 struct msm_drm_private
*priv
;
2175 if (!drm_enc
->dev
|| !drm_enc
->dev
->dev_private
) {
2176 DPU_ERROR("invalid parameters\n");
2179 priv
= drm_enc
->dev
->dev_private
;
2181 if (!dpu_enc
->frame_busy_mask
[0] || !dpu_enc
->crtc_frame_event_cb
) {
2182 DRM_DEBUG_KMS("id:%u invalid timeout frame_busy_mask=%lu\n",
2183 DRMID(drm_enc
), dpu_enc
->frame_busy_mask
[0]);
2185 } else if (!atomic_xchg(&dpu_enc
->frame_done_timeout_ms
, 0)) {
2186 DRM_DEBUG_KMS("id:%u invalid timeout\n", DRMID(drm_enc
));
2190 DPU_ERROR_ENC(dpu_enc
, "frame done timeout\n");
2192 event
= DPU_ENCODER_FRAME_EVENT_ERROR
;
2193 trace_dpu_enc_frame_done_timeout(DRMID(drm_enc
), event
);
2194 dpu_enc
->crtc_frame_event_cb(dpu_enc
->crtc_frame_event_cb_data
, event
);
2197 static const struct drm_encoder_helper_funcs dpu_encoder_helper_funcs
= {
2198 .mode_set
= dpu_encoder_virt_mode_set
,
2199 .disable
= dpu_encoder_virt_disable
,
2200 .enable
= dpu_kms_encoder_enable
,
2201 .atomic_check
= dpu_encoder_virt_atomic_check
,
2203 /* This is called by dpu_kms_encoder_enable */
2204 .commit
= dpu_encoder_virt_enable
,
2207 static const struct drm_encoder_funcs dpu_encoder_funcs
= {
2208 .destroy
= dpu_encoder_destroy
,
2209 .late_register
= dpu_encoder_late_register
,
2210 .early_unregister
= dpu_encoder_early_unregister
,
2213 int dpu_encoder_setup(struct drm_device
*dev
, struct drm_encoder
*enc
,
2214 struct msm_display_info
*disp_info
)
2216 struct msm_drm_private
*priv
= dev
->dev_private
;
2217 struct dpu_kms
*dpu_kms
= to_dpu_kms(priv
->kms
);
2218 struct drm_encoder
*drm_enc
= NULL
;
2219 struct dpu_encoder_virt
*dpu_enc
= NULL
;
2222 dpu_enc
= to_dpu_encoder_virt(enc
);
2224 mutex_init(&dpu_enc
->enc_lock
);
2225 ret
= dpu_encoder_setup_display(dpu_enc
, dpu_kms
, disp_info
);
2229 spin_lock_init(&dpu_enc
->enc_spinlock
);
2231 atomic_set(&dpu_enc
->frame_done_timeout_ms
, 0);
2232 timer_setup(&dpu_enc
->frame_done_timer
,
2233 dpu_encoder_frame_done_timeout
, 0);
2235 if (disp_info
->intf_type
== DRM_MODE_ENCODER_DSI
)
2236 timer_setup(&dpu_enc
->vsync_event_timer
,
2237 dpu_encoder_vsync_event_handler
,
2241 mutex_init(&dpu_enc
->rc_lock
);
2242 INIT_DELAYED_WORK(&dpu_enc
->delayed_off_work
,
2243 dpu_encoder_off_work
);
2244 dpu_enc
->idle_timeout
= IDLE_TIMEOUT
;
2246 kthread_init_work(&dpu_enc
->vsync_event_work
,
2247 dpu_encoder_vsync_event_work_handler
);
2249 memcpy(&dpu_enc
->disp_info
, disp_info
, sizeof(*disp_info
));
2251 DPU_DEBUG_ENC(dpu_enc
, "created\n");
2256 DPU_ERROR("failed to create encoder\n");
2258 dpu_encoder_destroy(drm_enc
);
2265 struct drm_encoder
*dpu_encoder_init(struct drm_device
*dev
,
2268 struct dpu_encoder_virt
*dpu_enc
= NULL
;
2271 dpu_enc
= devm_kzalloc(dev
->dev
, sizeof(*dpu_enc
), GFP_KERNEL
);
2273 return ERR_PTR(ENOMEM
);
2275 rc
= drm_encoder_init(dev
, &dpu_enc
->base
, &dpu_encoder_funcs
,
2276 drm_enc_mode
, NULL
);
2278 devm_kfree(dev
->dev
, dpu_enc
);
2282 drm_encoder_helper_add(&dpu_enc
->base
, &dpu_encoder_helper_funcs
);
2284 dpu_enc
->enabled
= false;
2286 return &dpu_enc
->base
;
2289 int dpu_encoder_wait_for_event(struct drm_encoder
*drm_enc
,
2290 enum msm_event_wait event
)
2292 int (*fn_wait
)(struct dpu_encoder_phys
*phys_enc
) = NULL
;
2293 struct dpu_encoder_virt
*dpu_enc
= NULL
;
2297 DPU_ERROR("invalid encoder\n");
2300 dpu_enc
= to_dpu_encoder_virt(drm_enc
);
2301 DPU_DEBUG_ENC(dpu_enc
, "\n");
2303 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
2304 struct dpu_encoder_phys
*phys
= dpu_enc
->phys_encs
[i
];
2309 case MSM_ENC_COMMIT_DONE
:
2310 fn_wait
= phys
->ops
.wait_for_commit_done
;
2312 case MSM_ENC_TX_COMPLETE
:
2313 fn_wait
= phys
->ops
.wait_for_tx_complete
;
2315 case MSM_ENC_VBLANK
:
2316 fn_wait
= phys
->ops
.wait_for_vblank
;
2319 DPU_ERROR_ENC(dpu_enc
, "unknown wait event %d\n",
2325 DPU_ATRACE_BEGIN("wait_for_completion_event");
2326 ret
= fn_wait(phys
);
2327 DPU_ATRACE_END("wait_for_completion_event");
2336 enum dpu_intf_mode
dpu_encoder_get_intf_mode(struct drm_encoder
*encoder
)
2338 struct dpu_encoder_virt
*dpu_enc
= NULL
;
2342 DPU_ERROR("invalid encoder\n");
2343 return INTF_MODE_NONE
;
2345 dpu_enc
= to_dpu_encoder_virt(encoder
);
2347 if (dpu_enc
->cur_master
)
2348 return dpu_enc
->cur_master
->intf_mode
;
2350 for (i
= 0; i
< dpu_enc
->num_phys_encs
; i
++) {
2351 struct dpu_encoder_phys
*phys
= dpu_enc
->phys_encs
[i
];
2354 return phys
->intf_mode
;
2357 return INTF_MODE_NONE
;