xref: /openbmc/linux/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c (revision 15a1fbdcfb519c2bd291ed01c6c94e0b89537a77)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
4  * Copyright (C) 2013 Red Hat
5  * Author: Rob Clark <robdclark@gmail.com>
6  */
7 
8 #define pr_fmt(fmt)	"[drm:%s:%d] " fmt, __func__, __LINE__
9 #include <linux/debugfs.h>
10 #include <linux/kthread.h>
11 #include <linux/seq_file.h>
12 
13 #include <drm/drm_crtc.h>
14 #include <drm/drm_file.h>
15 #include <drm/drm_probe_helper.h>
16 
17 #include "msm_drv.h"
18 #include "dpu_kms.h"
19 #include "dpu_hwio.h"
20 #include "dpu_hw_catalog.h"
21 #include "dpu_hw_intf.h"
22 #include "dpu_hw_ctl.h"
23 #include "dpu_formats.h"
24 #include "dpu_encoder_phys.h"
25 #include "dpu_crtc.h"
26 #include "dpu_trace.h"
27 #include "dpu_core_irq.h"
28 
29 #define DPU_DEBUG_ENC(e, fmt, ...) DPU_DEBUG("enc%d " fmt,\
30 		(e) ? (e)->base.base.id : -1, ##__VA_ARGS__)
31 
32 #define DPU_ERROR_ENC(e, fmt, ...) DPU_ERROR("enc%d " fmt,\
33 		(e) ? (e)->base.base.id : -1, ##__VA_ARGS__)
34 
35 #define DPU_DEBUG_PHYS(p, fmt, ...) DPU_DEBUG("enc%d intf%d pp%d " fmt,\
36 		(p) ? (p)->parent->base.id : -1, \
37 		(p) ? (p)->intf_idx - INTF_0 : -1, \
38 		(p) ? ((p)->hw_pp ? (p)->hw_pp->idx - PINGPONG_0 : -1) : -1, \
39 		##__VA_ARGS__)
40 
41 #define DPU_ERROR_PHYS(p, fmt, ...) DPU_ERROR("enc%d intf%d pp%d " fmt,\
42 		(p) ? (p)->parent->base.id : -1, \
43 		(p) ? (p)->intf_idx - INTF_0 : -1, \
44 		(p) ? ((p)->hw_pp ? (p)->hw_pp->idx - PINGPONG_0 : -1) : -1, \
45 		##__VA_ARGS__)
46 
47 /*
48  * Two to anticipate panels that can do cmd/vid dynamic switching
49  * plan is to create all possible physical encoder types, and switch between
50  * them at runtime
51  */
52 #define NUM_PHYS_ENCODER_TYPES 2
53 
54 #define MAX_PHYS_ENCODERS_PER_VIRTUAL \
55 	(MAX_H_TILES_PER_DISPLAY * NUM_PHYS_ENCODER_TYPES)
56 
57 #define MAX_CHANNELS_PER_ENC 2
58 
59 #define IDLE_SHORT_TIMEOUT	1
60 
61 #define MAX_HDISPLAY_SPLIT 1080
62 
63 /* timeout in frames waiting for frame done */
64 #define DPU_ENCODER_FRAME_DONE_TIMEOUT_FRAMES 5
65 
66 /**
67  * enum dpu_enc_rc_events - events for resource control state machine
68  * @DPU_ENC_RC_EVENT_KICKOFF:
69  *	This event happens at NORMAL priority.
70  *	Event that signals the start of the transfer. When this event is
71  *	received, enable MDP/DSI core clocks. Regardless of the previous
72  *	state, the resource should be in ON state at the end of this event.
73  * @DPU_ENC_RC_EVENT_FRAME_DONE:
74  *	This event happens at INTERRUPT level.
75  *	Event signals the end of the data transfer after the PP FRAME_DONE
76  *	event. At the end of this event, a delayed work is scheduled to go to
77  *	IDLE_PC state after IDLE_TIMEOUT time.
78  * @DPU_ENC_RC_EVENT_PRE_STOP:
79  *	This event happens at NORMAL priority.
80  *	This event, when received during the ON state, leave the RC STATE
81  *	in the PRE_OFF state. It should be followed by the STOP event as
82  *	part of encoder disable.
83  *	If received during IDLE or OFF states, it will do nothing.
84  * @DPU_ENC_RC_EVENT_STOP:
85  *	This event happens at NORMAL priority.
86  *	When this event is received, disable all the MDP/DSI core clocks, and
87  *	disable IRQs. It should be called from the PRE_OFF or IDLE states.
88  *	IDLE is expected when IDLE_PC has run, and PRE_OFF did nothing.
89  *	PRE_OFF is expected when PRE_STOP was executed during the ON state.
90  *	Resource state should be in OFF at the end of the event.
91  * @DPU_ENC_RC_EVENT_ENTER_IDLE:
92  *	This event happens at NORMAL priority from a work item.
93  *	Event signals that there were no frame updates for IDLE_TIMEOUT time.
94  *	This would disable MDP/DSI core clocks and change the resource state
95  *	to IDLE.
96  */
97 enum dpu_enc_rc_events {
98 	DPU_ENC_RC_EVENT_KICKOFF = 1,
99 	DPU_ENC_RC_EVENT_FRAME_DONE,
100 	DPU_ENC_RC_EVENT_PRE_STOP,
101 	DPU_ENC_RC_EVENT_STOP,
102 	DPU_ENC_RC_EVENT_ENTER_IDLE
103 };
104 
105 /*
106  * enum dpu_enc_rc_states - states that the resource control maintains
107  * @DPU_ENC_RC_STATE_OFF: Resource is in OFF state
108  * @DPU_ENC_RC_STATE_PRE_OFF: Resource is transitioning to OFF state
109  * @DPU_ENC_RC_STATE_ON: Resource is in ON state
110  * @DPU_ENC_RC_STATE_MODESET: Resource is in modeset state
111  * @DPU_ENC_RC_STATE_IDLE: Resource is in IDLE state
112  */
113 enum dpu_enc_rc_states {
114 	DPU_ENC_RC_STATE_OFF,
115 	DPU_ENC_RC_STATE_PRE_OFF,
116 	DPU_ENC_RC_STATE_ON,
117 	DPU_ENC_RC_STATE_IDLE
118 };
119 
120 /**
121  * struct dpu_encoder_virt - virtual encoder. Container of one or more physical
122  *	encoders. Virtual encoder manages one "logical" display. Physical
123  *	encoders manage one intf block, tied to a specific panel/sub-panel.
124  *	Virtual encoder defers as much as possible to the physical encoders.
125  *	Virtual encoder registers itself with the DRM Framework as the encoder.
126  * @base:		drm_encoder base class for registration with DRM
127  * @enc_spinlock:	Virtual-Encoder-Wide Spin Lock for IRQ purposes
128  * @bus_scaling_client:	Client handle to the bus scaling interface
129  * @enabled:		True if the encoder is active, protected by enc_lock
130  * @num_phys_encs:	Actual number of physical encoders contained.
131  * @phys_encs:		Container of physical encoders managed.
132  * @cur_master:		Pointer to the current master in this mode. Optimization
133  *			Only valid after enable. Cleared as disable.
134  * @hw_pp		Handle to the pingpong blocks used for the display. No.
135  *			pingpong blocks can be different than num_phys_encs.
136  * @intfs_swapped	Whether or not the phys_enc interfaces have been swapped
137  *			for partial update right-only cases, such as pingpong
138  *			split where virtual pingpong does not generate IRQs
139  * @crtc:		Pointer to the currently assigned crtc. Normally you
140  *			would use crtc->state->encoder_mask to determine the
141  *			link between encoder/crtc. However in this case we need
142  *			to track crtc in the disable() hook which is called
143  *			_after_ encoder_mask is cleared.
144  * @crtc_kickoff_cb:		Callback into CRTC that will flush & start
145  *				all CTL paths
146  * @crtc_kickoff_cb_data:	Opaque user data given to crtc_kickoff_cb
147  * @debugfs_root:		Debug file system root file node
148  * @enc_lock:			Lock around physical encoder
149  *				create/destroy/enable/disable
150  * @frame_busy_mask:		Bitmask tracking which phys_enc we are still
151  *				busy processing current command.
152  *				Bit0 = phys_encs[0] etc.
153  * @crtc_frame_event_cb:	callback handler for frame event
154  * @crtc_frame_event_cb_data:	callback handler private data
155  * @frame_done_timeout_ms:	frame done timeout in ms
156  * @frame_done_timer:		watchdog timer for frame done event
157  * @vsync_event_timer:		vsync timer
158  * @disp_info:			local copy of msm_display_info struct
159  * @idle_pc_supported:		indicate if idle power collaps is supported
160  * @rc_lock:			resource control mutex lock to protect
161  *				virt encoder over various state changes
162  * @rc_state:			resource controller state
163  * @delayed_off_work:		delayed worker to schedule disabling of
164  *				clks and resources after IDLE_TIMEOUT time.
165  * @vsync_event_work:		worker to handle vsync event for autorefresh
166  * @topology:                   topology of the display
167  * @mode_set_complete:          flag to indicate modeset completion
168  * @idle_timeout:		idle timeout duration in milliseconds
169  */
170 struct dpu_encoder_virt {
171 	struct drm_encoder base;
172 	spinlock_t enc_spinlock;
173 	uint32_t bus_scaling_client;
174 
175 	bool enabled;
176 
177 	unsigned int num_phys_encs;
178 	struct dpu_encoder_phys *phys_encs[MAX_PHYS_ENCODERS_PER_VIRTUAL];
179 	struct dpu_encoder_phys *cur_master;
180 	struct dpu_encoder_phys *cur_slave;
181 	struct dpu_hw_pingpong *hw_pp[MAX_CHANNELS_PER_ENC];
182 
183 	bool intfs_swapped;
184 
185 	struct drm_crtc *crtc;
186 
187 	struct dentry *debugfs_root;
188 	struct mutex enc_lock;
189 	DECLARE_BITMAP(frame_busy_mask, MAX_PHYS_ENCODERS_PER_VIRTUAL);
190 	void (*crtc_frame_event_cb)(void *, u32 event);
191 	void *crtc_frame_event_cb_data;
192 
193 	atomic_t frame_done_timeout_ms;
194 	struct timer_list frame_done_timer;
195 	struct timer_list vsync_event_timer;
196 
197 	struct msm_display_info disp_info;
198 
199 	bool idle_pc_supported;
200 	struct mutex rc_lock;
201 	enum dpu_enc_rc_states rc_state;
202 	struct delayed_work delayed_off_work;
203 	struct kthread_work vsync_event_work;
204 	struct msm_display_topology topology;
205 	bool mode_set_complete;
206 
207 	u32 idle_timeout;
208 };
209 
210 #define to_dpu_encoder_virt(x) container_of(x, struct dpu_encoder_virt, base)
211 
212 void dpu_encoder_helper_report_irq_timeout(struct dpu_encoder_phys *phys_enc,
213 		enum dpu_intr_idx intr_idx)
214 {
215 	DRM_ERROR("irq timeout id=%u, intf=%d, pp=%d, intr=%d\n",
216 		  DRMID(phys_enc->parent), phys_enc->intf_idx - INTF_0,
217 		  phys_enc->hw_pp->idx - PINGPONG_0, intr_idx);
218 
219 	if (phys_enc->parent_ops->handle_frame_done)
220 		phys_enc->parent_ops->handle_frame_done(
221 				phys_enc->parent, phys_enc,
222 				DPU_ENCODER_FRAME_EVENT_ERROR);
223 }
224 
225 static int dpu_encoder_helper_wait_event_timeout(int32_t drm_id,
226 		int32_t hw_id, struct dpu_encoder_wait_info *info);
227 
228 int dpu_encoder_helper_wait_for_irq(struct dpu_encoder_phys *phys_enc,
229 		enum dpu_intr_idx intr_idx,
230 		struct dpu_encoder_wait_info *wait_info)
231 {
232 	struct dpu_encoder_irq *irq;
233 	u32 irq_status;
234 	int ret;
235 
236 	if (!wait_info || intr_idx >= INTR_IDX_MAX) {
237 		DPU_ERROR("invalid params\n");
238 		return -EINVAL;
239 	}
240 	irq = &phys_enc->irq[intr_idx];
241 
242 	/* note: do master / slave checking outside */
243 
244 	/* return EWOULDBLOCK since we know the wait isn't necessary */
245 	if (phys_enc->enable_state == DPU_ENC_DISABLED) {
246 		DRM_ERROR("encoder is disabled id=%u, intr=%d, hw=%d, irq=%d",
247 			  DRMID(phys_enc->parent), intr_idx, irq->hw_idx,
248 			  irq->irq_idx);
249 		return -EWOULDBLOCK;
250 	}
251 
252 	if (irq->irq_idx < 0) {
253 		DRM_DEBUG_KMS("skip irq wait id=%u, intr=%d, hw=%d, irq=%s",
254 			      DRMID(phys_enc->parent), intr_idx, irq->hw_idx,
255 			      irq->name);
256 		return 0;
257 	}
258 
259 	DRM_DEBUG_KMS("id=%u, intr=%d, hw=%d, irq=%d, pp=%d, pending_cnt=%d",
260 		      DRMID(phys_enc->parent), intr_idx, irq->hw_idx,
261 		      irq->irq_idx, phys_enc->hw_pp->idx - PINGPONG_0,
262 		      atomic_read(wait_info->atomic_cnt));
263 
264 	ret = dpu_encoder_helper_wait_event_timeout(
265 			DRMID(phys_enc->parent),
266 			irq->hw_idx,
267 			wait_info);
268 
269 	if (ret <= 0) {
270 		irq_status = dpu_core_irq_read(phys_enc->dpu_kms,
271 				irq->irq_idx, true);
272 		if (irq_status) {
273 			unsigned long flags;
274 
275 			DRM_DEBUG_KMS("irq not triggered id=%u, intr=%d, "
276 				      "hw=%d, irq=%d, pp=%d, atomic_cnt=%d",
277 				      DRMID(phys_enc->parent), intr_idx,
278 				      irq->hw_idx, irq->irq_idx,
279 				      phys_enc->hw_pp->idx - PINGPONG_0,
280 				      atomic_read(wait_info->atomic_cnt));
281 			local_irq_save(flags);
282 			irq->cb.func(phys_enc, irq->irq_idx);
283 			local_irq_restore(flags);
284 			ret = 0;
285 		} else {
286 			ret = -ETIMEDOUT;
287 			DRM_DEBUG_KMS("irq timeout id=%u, intr=%d, "
288 				      "hw=%d, irq=%d, pp=%d, atomic_cnt=%d",
289 				      DRMID(phys_enc->parent), intr_idx,
290 				      irq->hw_idx, irq->irq_idx,
291 				      phys_enc->hw_pp->idx - PINGPONG_0,
292 				      atomic_read(wait_info->atomic_cnt));
293 		}
294 	} else {
295 		ret = 0;
296 		trace_dpu_enc_irq_wait_success(DRMID(phys_enc->parent),
297 			intr_idx, irq->hw_idx, irq->irq_idx,
298 			phys_enc->hw_pp->idx - PINGPONG_0,
299 			atomic_read(wait_info->atomic_cnt));
300 	}
301 
302 	return ret;
303 }
304 
305 int dpu_encoder_helper_register_irq(struct dpu_encoder_phys *phys_enc,
306 		enum dpu_intr_idx intr_idx)
307 {
308 	struct dpu_encoder_irq *irq;
309 	int ret = 0;
310 
311 	if (intr_idx >= INTR_IDX_MAX) {
312 		DPU_ERROR("invalid params\n");
313 		return -EINVAL;
314 	}
315 	irq = &phys_enc->irq[intr_idx];
316 
317 	if (irq->irq_idx >= 0) {
318 		DPU_DEBUG_PHYS(phys_enc,
319 				"skipping already registered irq %s type %d\n",
320 				irq->name, irq->intr_type);
321 		return 0;
322 	}
323 
324 	irq->irq_idx = dpu_core_irq_idx_lookup(phys_enc->dpu_kms,
325 			irq->intr_type, irq->hw_idx);
326 	if (irq->irq_idx < 0) {
327 		DPU_ERROR_PHYS(phys_enc,
328 			"failed to lookup IRQ index for %s type:%d\n",
329 			irq->name, irq->intr_type);
330 		return -EINVAL;
331 	}
332 
333 	ret = dpu_core_irq_register_callback(phys_enc->dpu_kms, irq->irq_idx,
334 			&irq->cb);
335 	if (ret) {
336 		DPU_ERROR_PHYS(phys_enc,
337 			"failed to register IRQ callback for %s\n",
338 			irq->name);
339 		irq->irq_idx = -EINVAL;
340 		return ret;
341 	}
342 
343 	ret = dpu_core_irq_enable(phys_enc->dpu_kms, &irq->irq_idx, 1);
344 	if (ret) {
345 		DRM_ERROR("enable failed id=%u, intr=%d, hw=%d, irq=%d",
346 			  DRMID(phys_enc->parent), intr_idx, irq->hw_idx,
347 			  irq->irq_idx);
348 		dpu_core_irq_unregister_callback(phys_enc->dpu_kms,
349 				irq->irq_idx, &irq->cb);
350 		irq->irq_idx = -EINVAL;
351 		return ret;
352 	}
353 
354 	trace_dpu_enc_irq_register_success(DRMID(phys_enc->parent), intr_idx,
355 				irq->hw_idx, irq->irq_idx);
356 
357 	return ret;
358 }
359 
360 int dpu_encoder_helper_unregister_irq(struct dpu_encoder_phys *phys_enc,
361 		enum dpu_intr_idx intr_idx)
362 {
363 	struct dpu_encoder_irq *irq;
364 	int ret;
365 
366 	irq = &phys_enc->irq[intr_idx];
367 
368 	/* silently skip irqs that weren't registered */
369 	if (irq->irq_idx < 0) {
370 		DRM_ERROR("duplicate unregister id=%u, intr=%d, hw=%d, irq=%d",
371 			  DRMID(phys_enc->parent), intr_idx, irq->hw_idx,
372 			  irq->irq_idx);
373 		return 0;
374 	}
375 
376 	ret = dpu_core_irq_disable(phys_enc->dpu_kms, &irq->irq_idx, 1);
377 	if (ret) {
378 		DRM_ERROR("disable failed id=%u, intr=%d, hw=%d, irq=%d ret=%d",
379 			  DRMID(phys_enc->parent), intr_idx, irq->hw_idx,
380 			  irq->irq_idx, ret);
381 	}
382 
383 	ret = dpu_core_irq_unregister_callback(phys_enc->dpu_kms, irq->irq_idx,
384 			&irq->cb);
385 	if (ret) {
386 		DRM_ERROR("unreg cb fail id=%u, intr=%d, hw=%d, irq=%d ret=%d",
387 			  DRMID(phys_enc->parent), intr_idx, irq->hw_idx,
388 			  irq->irq_idx, ret);
389 	}
390 
391 	trace_dpu_enc_irq_unregister_success(DRMID(phys_enc->parent), intr_idx,
392 					     irq->hw_idx, irq->irq_idx);
393 
394 	irq->irq_idx = -EINVAL;
395 
396 	return 0;
397 }
398 
399 void dpu_encoder_get_hw_resources(struct drm_encoder *drm_enc,
400 				  struct dpu_encoder_hw_resources *hw_res)
401 {
402 	struct dpu_encoder_virt *dpu_enc = NULL;
403 	int i = 0;
404 
405 	dpu_enc = to_dpu_encoder_virt(drm_enc);
406 	DPU_DEBUG_ENC(dpu_enc, "\n");
407 
408 	/* Query resources used by phys encs, expected to be without overlap */
409 	memset(hw_res, 0, sizeof(*hw_res));
410 
411 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
412 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
413 
414 		if (phys->ops.get_hw_resources)
415 			phys->ops.get_hw_resources(phys, hw_res);
416 	}
417 }
418 
419 static void dpu_encoder_destroy(struct drm_encoder *drm_enc)
420 {
421 	struct dpu_encoder_virt *dpu_enc = NULL;
422 	int i = 0;
423 
424 	if (!drm_enc) {
425 		DPU_ERROR("invalid encoder\n");
426 		return;
427 	}
428 
429 	dpu_enc = to_dpu_encoder_virt(drm_enc);
430 	DPU_DEBUG_ENC(dpu_enc, "\n");
431 
432 	mutex_lock(&dpu_enc->enc_lock);
433 
434 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
435 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
436 
437 		if (phys->ops.destroy) {
438 			phys->ops.destroy(phys);
439 			--dpu_enc->num_phys_encs;
440 			dpu_enc->phys_encs[i] = NULL;
441 		}
442 	}
443 
444 	if (dpu_enc->num_phys_encs)
445 		DPU_ERROR_ENC(dpu_enc, "expected 0 num_phys_encs not %d\n",
446 				dpu_enc->num_phys_encs);
447 	dpu_enc->num_phys_encs = 0;
448 	mutex_unlock(&dpu_enc->enc_lock);
449 
450 	drm_encoder_cleanup(drm_enc);
451 	mutex_destroy(&dpu_enc->enc_lock);
452 }
453 
454 void dpu_encoder_helper_split_config(
455 		struct dpu_encoder_phys *phys_enc,
456 		enum dpu_intf interface)
457 {
458 	struct dpu_encoder_virt *dpu_enc;
459 	struct split_pipe_cfg cfg = { 0 };
460 	struct dpu_hw_mdp *hw_mdptop;
461 	struct msm_display_info *disp_info;
462 
463 	if (!phys_enc->hw_mdptop || !phys_enc->parent) {
464 		DPU_ERROR("invalid arg(s), encoder %d\n", phys_enc != 0);
465 		return;
466 	}
467 
468 	dpu_enc = to_dpu_encoder_virt(phys_enc->parent);
469 	hw_mdptop = phys_enc->hw_mdptop;
470 	disp_info = &dpu_enc->disp_info;
471 
472 	if (disp_info->intf_type != DRM_MODE_ENCODER_DSI)
473 		return;
474 
475 	/**
476 	 * disable split modes since encoder will be operating in as the only
477 	 * encoder, either for the entire use case in the case of, for example,
478 	 * single DSI, or for this frame in the case of left/right only partial
479 	 * update.
480 	 */
481 	if (phys_enc->split_role == ENC_ROLE_SOLO) {
482 		if (hw_mdptop->ops.setup_split_pipe)
483 			hw_mdptop->ops.setup_split_pipe(hw_mdptop, &cfg);
484 		return;
485 	}
486 
487 	cfg.en = true;
488 	cfg.mode = phys_enc->intf_mode;
489 	cfg.intf = interface;
490 
491 	if (cfg.en && phys_enc->ops.needs_single_flush &&
492 			phys_enc->ops.needs_single_flush(phys_enc))
493 		cfg.split_flush_en = true;
494 
495 	if (phys_enc->split_role == ENC_ROLE_MASTER) {
496 		DPU_DEBUG_ENC(dpu_enc, "enable %d\n", cfg.en);
497 
498 		if (hw_mdptop->ops.setup_split_pipe)
499 			hw_mdptop->ops.setup_split_pipe(hw_mdptop, &cfg);
500 	}
501 }
502 
503 static void _dpu_encoder_adjust_mode(struct drm_connector *connector,
504 		struct drm_display_mode *adj_mode)
505 {
506 	struct drm_display_mode *cur_mode;
507 
508 	if (!connector || !adj_mode)
509 		return;
510 
511 	list_for_each_entry(cur_mode, &connector->modes, head) {
512 		if (cur_mode->vdisplay == adj_mode->vdisplay &&
513 		    cur_mode->hdisplay == adj_mode->hdisplay &&
514 		    drm_mode_vrefresh(cur_mode) == drm_mode_vrefresh(adj_mode)) {
515 			adj_mode->private_flags |= cur_mode->private_flags;
516 		}
517 	}
518 }
519 
520 static struct msm_display_topology dpu_encoder_get_topology(
521 			struct dpu_encoder_virt *dpu_enc,
522 			struct dpu_kms *dpu_kms,
523 			struct drm_display_mode *mode)
524 {
525 	struct msm_display_topology topology;
526 	int i, intf_count = 0;
527 
528 	for (i = 0; i < MAX_PHYS_ENCODERS_PER_VIRTUAL; i++)
529 		if (dpu_enc->phys_encs[i])
530 			intf_count++;
531 
532 	/* Datapath topology selection
533 	 *
534 	 * Dual display
535 	 * 2 LM, 2 INTF ( Split display using 2 interfaces)
536 	 *
537 	 * Single display
538 	 * 1 LM, 1 INTF
539 	 * 2 LM, 1 INTF (stream merge to support high resolution interfaces)
540 	 *
541 	 */
542 	if (intf_count == 2)
543 		topology.num_lm = 2;
544 	else if (!dpu_kms->catalog->caps->has_3d_merge)
545 		topology.num_lm = 1;
546 	else
547 		topology.num_lm = (mode->hdisplay > MAX_HDISPLAY_SPLIT) ? 2 : 1;
548 
549 	topology.num_enc = 0;
550 	topology.num_intf = intf_count;
551 
552 	return topology;
553 }
554 static int dpu_encoder_virt_atomic_check(
555 		struct drm_encoder *drm_enc,
556 		struct drm_crtc_state *crtc_state,
557 		struct drm_connector_state *conn_state)
558 {
559 	struct dpu_encoder_virt *dpu_enc;
560 	struct msm_drm_private *priv;
561 	struct dpu_kms *dpu_kms;
562 	const struct drm_display_mode *mode;
563 	struct drm_display_mode *adj_mode;
564 	struct msm_display_topology topology;
565 	int i = 0;
566 	int ret = 0;
567 
568 	if (!drm_enc || !crtc_state || !conn_state) {
569 		DPU_ERROR("invalid arg(s), drm_enc %d, crtc/conn state %d/%d\n",
570 				drm_enc != 0, crtc_state != 0, conn_state != 0);
571 		return -EINVAL;
572 	}
573 
574 	dpu_enc = to_dpu_encoder_virt(drm_enc);
575 	DPU_DEBUG_ENC(dpu_enc, "\n");
576 
577 	priv = drm_enc->dev->dev_private;
578 	dpu_kms = to_dpu_kms(priv->kms);
579 	mode = &crtc_state->mode;
580 	adj_mode = &crtc_state->adjusted_mode;
581 	trace_dpu_enc_atomic_check(DRMID(drm_enc));
582 
583 	/*
584 	 * display drivers may populate private fields of the drm display mode
585 	 * structure while registering possible modes of a connector with DRM.
586 	 * These private fields are not populated back while DRM invokes
587 	 * the mode_set callbacks. This module retrieves and populates the
588 	 * private fields of the given mode.
589 	 */
590 	_dpu_encoder_adjust_mode(conn_state->connector, adj_mode);
591 
592 	/* perform atomic check on the first physical encoder (master) */
593 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
594 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
595 
596 		if (phys->ops.atomic_check)
597 			ret = phys->ops.atomic_check(phys, crtc_state,
598 					conn_state);
599 		else if (phys->ops.mode_fixup)
600 			if (!phys->ops.mode_fixup(phys, mode, adj_mode))
601 				ret = -EINVAL;
602 
603 		if (ret) {
604 			DPU_ERROR_ENC(dpu_enc,
605 					"mode unsupported, phys idx %d\n", i);
606 			break;
607 		}
608 	}
609 
610 	topology = dpu_encoder_get_topology(dpu_enc, dpu_kms, adj_mode);
611 
612 	/* Reserve dynamic resources now. Indicating AtomicTest phase */
613 	if (!ret) {
614 		/*
615 		 * Avoid reserving resources when mode set is pending. Topology
616 		 * info may not be available to complete reservation.
617 		 */
618 		if (drm_atomic_crtc_needs_modeset(crtc_state)
619 				&& dpu_enc->mode_set_complete) {
620 			ret = dpu_rm_reserve(&dpu_kms->rm, drm_enc, crtc_state,
621 					     topology, true);
622 			dpu_enc->mode_set_complete = false;
623 		}
624 	}
625 
626 	trace_dpu_enc_atomic_check_flags(DRMID(drm_enc), adj_mode->flags,
627 			adj_mode->private_flags);
628 
629 	return ret;
630 }
631 
632 static void _dpu_encoder_update_vsync_source(struct dpu_encoder_virt *dpu_enc,
633 			struct msm_display_info *disp_info)
634 {
635 	struct dpu_vsync_source_cfg vsync_cfg = { 0 };
636 	struct msm_drm_private *priv;
637 	struct dpu_kms *dpu_kms;
638 	struct dpu_hw_mdp *hw_mdptop;
639 	struct drm_encoder *drm_enc;
640 	int i;
641 
642 	if (!dpu_enc || !disp_info) {
643 		DPU_ERROR("invalid param dpu_enc:%d or disp_info:%d\n",
644 					dpu_enc != NULL, disp_info != NULL);
645 		return;
646 	} else if (dpu_enc->num_phys_encs > ARRAY_SIZE(dpu_enc->hw_pp)) {
647 		DPU_ERROR("invalid num phys enc %d/%d\n",
648 				dpu_enc->num_phys_encs,
649 				(int) ARRAY_SIZE(dpu_enc->hw_pp));
650 		return;
651 	}
652 
653 	drm_enc = &dpu_enc->base;
654 	/* this pointers are checked in virt_enable_helper */
655 	priv = drm_enc->dev->dev_private;
656 
657 	dpu_kms = to_dpu_kms(priv->kms);
658 	hw_mdptop = dpu_kms->hw_mdp;
659 	if (!hw_mdptop) {
660 		DPU_ERROR("invalid mdptop\n");
661 		return;
662 	}
663 
664 	if (hw_mdptop->ops.setup_vsync_source &&
665 			disp_info->capabilities & MSM_DISPLAY_CAP_CMD_MODE) {
666 		for (i = 0; i < dpu_enc->num_phys_encs; i++)
667 			vsync_cfg.ppnumber[i] = dpu_enc->hw_pp[i]->idx;
668 
669 		vsync_cfg.pp_count = dpu_enc->num_phys_encs;
670 		if (disp_info->is_te_using_watchdog_timer)
671 			vsync_cfg.vsync_source = DPU_VSYNC_SOURCE_WD_TIMER_0;
672 		else
673 			vsync_cfg.vsync_source = DPU_VSYNC0_SOURCE_GPIO;
674 
675 		hw_mdptop->ops.setup_vsync_source(hw_mdptop, &vsync_cfg);
676 	}
677 }
678 
679 static void _dpu_encoder_irq_control(struct drm_encoder *drm_enc, bool enable)
680 {
681 	struct dpu_encoder_virt *dpu_enc;
682 	int i;
683 
684 	if (!drm_enc) {
685 		DPU_ERROR("invalid encoder\n");
686 		return;
687 	}
688 
689 	dpu_enc = to_dpu_encoder_virt(drm_enc);
690 
691 	DPU_DEBUG_ENC(dpu_enc, "enable:%d\n", enable);
692 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
693 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
694 
695 		if (phys->ops.irq_control)
696 			phys->ops.irq_control(phys, enable);
697 	}
698 
699 }
700 
701 static void _dpu_encoder_resource_control_helper(struct drm_encoder *drm_enc,
702 		bool enable)
703 {
704 	struct msm_drm_private *priv;
705 	struct dpu_kms *dpu_kms;
706 	struct dpu_encoder_virt *dpu_enc;
707 
708 	dpu_enc = to_dpu_encoder_virt(drm_enc);
709 	priv = drm_enc->dev->dev_private;
710 	dpu_kms = to_dpu_kms(priv->kms);
711 
712 	trace_dpu_enc_rc_helper(DRMID(drm_enc), enable);
713 
714 	if (!dpu_enc->cur_master) {
715 		DPU_ERROR("encoder master not set\n");
716 		return;
717 	}
718 
719 	if (enable) {
720 		/* enable DPU core clks */
721 		pm_runtime_get_sync(&dpu_kms->pdev->dev);
722 
723 		/* enable all the irq */
724 		_dpu_encoder_irq_control(drm_enc, true);
725 
726 	} else {
727 		/* disable all the irq */
728 		_dpu_encoder_irq_control(drm_enc, false);
729 
730 		/* disable DPU core clks */
731 		pm_runtime_put_sync(&dpu_kms->pdev->dev);
732 	}
733 
734 }
735 
736 static int dpu_encoder_resource_control(struct drm_encoder *drm_enc,
737 		u32 sw_event)
738 {
739 	struct dpu_encoder_virt *dpu_enc;
740 	struct msm_drm_private *priv;
741 	bool is_vid_mode = false;
742 
743 	if (!drm_enc || !drm_enc->dev || !drm_enc->crtc) {
744 		DPU_ERROR("invalid parameters\n");
745 		return -EINVAL;
746 	}
747 	dpu_enc = to_dpu_encoder_virt(drm_enc);
748 	priv = drm_enc->dev->dev_private;
749 	is_vid_mode = dpu_enc->disp_info.capabilities &
750 						MSM_DISPLAY_CAP_VID_MODE;
751 
752 	/*
753 	 * when idle_pc is not supported, process only KICKOFF, STOP and MODESET
754 	 * events and return early for other events (ie wb display).
755 	 */
756 	if (!dpu_enc->idle_pc_supported &&
757 			(sw_event != DPU_ENC_RC_EVENT_KICKOFF &&
758 			sw_event != DPU_ENC_RC_EVENT_STOP &&
759 			sw_event != DPU_ENC_RC_EVENT_PRE_STOP))
760 		return 0;
761 
762 	trace_dpu_enc_rc(DRMID(drm_enc), sw_event, dpu_enc->idle_pc_supported,
763 			 dpu_enc->rc_state, "begin");
764 
765 	switch (sw_event) {
766 	case DPU_ENC_RC_EVENT_KICKOFF:
767 		/* cancel delayed off work, if any */
768 		if (cancel_delayed_work_sync(&dpu_enc->delayed_off_work))
769 			DPU_DEBUG_ENC(dpu_enc, "sw_event:%d, work cancelled\n",
770 					sw_event);
771 
772 		mutex_lock(&dpu_enc->rc_lock);
773 
774 		/* return if the resource control is already in ON state */
775 		if (dpu_enc->rc_state == DPU_ENC_RC_STATE_ON) {
776 			DRM_DEBUG_KMS("id;%u, sw_event:%d, rc in ON state\n",
777 				      DRMID(drm_enc), sw_event);
778 			mutex_unlock(&dpu_enc->rc_lock);
779 			return 0;
780 		} else if (dpu_enc->rc_state != DPU_ENC_RC_STATE_OFF &&
781 				dpu_enc->rc_state != DPU_ENC_RC_STATE_IDLE) {
782 			DRM_DEBUG_KMS("id;%u, sw_event:%d, rc in state %d\n",
783 				      DRMID(drm_enc), sw_event,
784 				      dpu_enc->rc_state);
785 			mutex_unlock(&dpu_enc->rc_lock);
786 			return -EINVAL;
787 		}
788 
789 		if (is_vid_mode && dpu_enc->rc_state == DPU_ENC_RC_STATE_IDLE)
790 			_dpu_encoder_irq_control(drm_enc, true);
791 		else
792 			_dpu_encoder_resource_control_helper(drm_enc, true);
793 
794 		dpu_enc->rc_state = DPU_ENC_RC_STATE_ON;
795 
796 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
797 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
798 				 "kickoff");
799 
800 		mutex_unlock(&dpu_enc->rc_lock);
801 		break;
802 
803 	case DPU_ENC_RC_EVENT_FRAME_DONE:
804 		/*
805 		 * mutex lock is not used as this event happens at interrupt
806 		 * context. And locking is not required as, the other events
807 		 * like KICKOFF and STOP does a wait-for-idle before executing
808 		 * the resource_control
809 		 */
810 		if (dpu_enc->rc_state != DPU_ENC_RC_STATE_ON) {
811 			DRM_DEBUG_KMS("id:%d, sw_event:%d,rc:%d-unexpected\n",
812 				      DRMID(drm_enc), sw_event,
813 				      dpu_enc->rc_state);
814 			return -EINVAL;
815 		}
816 
817 		/*
818 		 * schedule off work item only when there are no
819 		 * frames pending
820 		 */
821 		if (dpu_crtc_frame_pending(drm_enc->crtc) > 1) {
822 			DRM_DEBUG_KMS("id:%d skip schedule work\n",
823 				      DRMID(drm_enc));
824 			return 0;
825 		}
826 
827 		queue_delayed_work(priv->wq, &dpu_enc->delayed_off_work,
828 				   msecs_to_jiffies(dpu_enc->idle_timeout));
829 
830 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
831 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
832 				 "frame done");
833 		break;
834 
835 	case DPU_ENC_RC_EVENT_PRE_STOP:
836 		/* cancel delayed off work, if any */
837 		if (cancel_delayed_work_sync(&dpu_enc->delayed_off_work))
838 			DPU_DEBUG_ENC(dpu_enc, "sw_event:%d, work cancelled\n",
839 					sw_event);
840 
841 		mutex_lock(&dpu_enc->rc_lock);
842 
843 		if (is_vid_mode &&
844 			  dpu_enc->rc_state == DPU_ENC_RC_STATE_IDLE) {
845 			_dpu_encoder_irq_control(drm_enc, true);
846 		}
847 		/* skip if is already OFF or IDLE, resources are off already */
848 		else if (dpu_enc->rc_state == DPU_ENC_RC_STATE_OFF ||
849 				dpu_enc->rc_state == DPU_ENC_RC_STATE_IDLE) {
850 			DRM_DEBUG_KMS("id:%u, sw_event:%d, rc in %d state\n",
851 				      DRMID(drm_enc), sw_event,
852 				      dpu_enc->rc_state);
853 			mutex_unlock(&dpu_enc->rc_lock);
854 			return 0;
855 		}
856 
857 		dpu_enc->rc_state = DPU_ENC_RC_STATE_PRE_OFF;
858 
859 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
860 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
861 				 "pre stop");
862 
863 		mutex_unlock(&dpu_enc->rc_lock);
864 		break;
865 
866 	case DPU_ENC_RC_EVENT_STOP:
867 		mutex_lock(&dpu_enc->rc_lock);
868 
869 		/* return if the resource control is already in OFF state */
870 		if (dpu_enc->rc_state == DPU_ENC_RC_STATE_OFF) {
871 			DRM_DEBUG_KMS("id: %u, sw_event:%d, rc in OFF state\n",
872 				      DRMID(drm_enc), sw_event);
873 			mutex_unlock(&dpu_enc->rc_lock);
874 			return 0;
875 		} else if (dpu_enc->rc_state == DPU_ENC_RC_STATE_ON) {
876 			DRM_ERROR("id: %u, sw_event:%d, rc in state %d\n",
877 				  DRMID(drm_enc), sw_event, dpu_enc->rc_state);
878 			mutex_unlock(&dpu_enc->rc_lock);
879 			return -EINVAL;
880 		}
881 
882 		/**
883 		 * expect to arrive here only if in either idle state or pre-off
884 		 * and in IDLE state the resources are already disabled
885 		 */
886 		if (dpu_enc->rc_state == DPU_ENC_RC_STATE_PRE_OFF)
887 			_dpu_encoder_resource_control_helper(drm_enc, false);
888 
889 		dpu_enc->rc_state = DPU_ENC_RC_STATE_OFF;
890 
891 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
892 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
893 				 "stop");
894 
895 		mutex_unlock(&dpu_enc->rc_lock);
896 		break;
897 
898 	case DPU_ENC_RC_EVENT_ENTER_IDLE:
899 		mutex_lock(&dpu_enc->rc_lock);
900 
901 		if (dpu_enc->rc_state != DPU_ENC_RC_STATE_ON) {
902 			DRM_ERROR("id: %u, sw_event:%d, rc:%d !ON state\n",
903 				  DRMID(drm_enc), sw_event, dpu_enc->rc_state);
904 			mutex_unlock(&dpu_enc->rc_lock);
905 			return 0;
906 		}
907 
908 		/*
909 		 * if we are in ON but a frame was just kicked off,
910 		 * ignore the IDLE event, it's probably a stale timer event
911 		 */
912 		if (dpu_enc->frame_busy_mask[0]) {
913 			DRM_ERROR("id:%u, sw_event:%d, rc:%d frame pending\n",
914 				  DRMID(drm_enc), sw_event, dpu_enc->rc_state);
915 			mutex_unlock(&dpu_enc->rc_lock);
916 			return 0;
917 		}
918 
919 		if (is_vid_mode)
920 			_dpu_encoder_irq_control(drm_enc, false);
921 		else
922 			_dpu_encoder_resource_control_helper(drm_enc, false);
923 
924 		dpu_enc->rc_state = DPU_ENC_RC_STATE_IDLE;
925 
926 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
927 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
928 				 "idle");
929 
930 		mutex_unlock(&dpu_enc->rc_lock);
931 		break;
932 
933 	default:
934 		DRM_ERROR("id:%u, unexpected sw_event: %d\n", DRMID(drm_enc),
935 			  sw_event);
936 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
937 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
938 				 "error");
939 		break;
940 	}
941 
942 	trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
943 			 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
944 			 "end");
945 	return 0;
946 }
947 
948 static void dpu_encoder_virt_mode_set(struct drm_encoder *drm_enc,
949 				      struct drm_display_mode *mode,
950 				      struct drm_display_mode *adj_mode)
951 {
952 	struct dpu_encoder_virt *dpu_enc;
953 	struct msm_drm_private *priv;
954 	struct dpu_kms *dpu_kms;
955 	struct list_head *connector_list;
956 	struct drm_connector *conn = NULL, *conn_iter;
957 	struct drm_crtc *drm_crtc;
958 	struct dpu_crtc_state *cstate;
959 	struct dpu_rm_hw_iter hw_iter;
960 	struct msm_display_topology topology;
961 	struct dpu_hw_ctl *hw_ctl[MAX_CHANNELS_PER_ENC] = { NULL };
962 	struct dpu_hw_mixer *hw_lm[MAX_CHANNELS_PER_ENC] = { NULL };
963 	int num_lm = 0, num_ctl = 0;
964 	int i, j, ret;
965 
966 	if (!drm_enc) {
967 		DPU_ERROR("invalid encoder\n");
968 		return;
969 	}
970 
971 	dpu_enc = to_dpu_encoder_virt(drm_enc);
972 	DPU_DEBUG_ENC(dpu_enc, "\n");
973 
974 	priv = drm_enc->dev->dev_private;
975 	dpu_kms = to_dpu_kms(priv->kms);
976 	connector_list = &dpu_kms->dev->mode_config.connector_list;
977 
978 	trace_dpu_enc_mode_set(DRMID(drm_enc));
979 
980 	list_for_each_entry(conn_iter, connector_list, head)
981 		if (conn_iter->encoder == drm_enc)
982 			conn = conn_iter;
983 
984 	if (!conn) {
985 		DPU_ERROR_ENC(dpu_enc, "failed to find attached connector\n");
986 		return;
987 	} else if (!conn->state) {
988 		DPU_ERROR_ENC(dpu_enc, "invalid connector state\n");
989 		return;
990 	}
991 
992 	drm_for_each_crtc(drm_crtc, drm_enc->dev)
993 		if (drm_crtc->state->encoder_mask & drm_encoder_mask(drm_enc))
994 			break;
995 
996 	topology = dpu_encoder_get_topology(dpu_enc, dpu_kms, adj_mode);
997 
998 	/* Reserve dynamic resources now. Indicating non-AtomicTest phase */
999 	ret = dpu_rm_reserve(&dpu_kms->rm, drm_enc, drm_crtc->state,
1000 			     topology, false);
1001 	if (ret) {
1002 		DPU_ERROR_ENC(dpu_enc,
1003 				"failed to reserve hw resources, %d\n", ret);
1004 		return;
1005 	}
1006 
1007 	dpu_rm_init_hw_iter(&hw_iter, drm_enc->base.id, DPU_HW_BLK_PINGPONG);
1008 	for (i = 0; i < MAX_CHANNELS_PER_ENC; i++) {
1009 		dpu_enc->hw_pp[i] = NULL;
1010 		if (!dpu_rm_get_hw(&dpu_kms->rm, &hw_iter))
1011 			break;
1012 		dpu_enc->hw_pp[i] = (struct dpu_hw_pingpong *) hw_iter.hw;
1013 	}
1014 
1015 	dpu_rm_init_hw_iter(&hw_iter, drm_enc->base.id, DPU_HW_BLK_CTL);
1016 	for (i = 0; i < MAX_CHANNELS_PER_ENC; i++) {
1017 		if (!dpu_rm_get_hw(&dpu_kms->rm, &hw_iter))
1018 			break;
1019 		hw_ctl[i] = (struct dpu_hw_ctl *)hw_iter.hw;
1020 		num_ctl++;
1021 	}
1022 
1023 	dpu_rm_init_hw_iter(&hw_iter, drm_enc->base.id, DPU_HW_BLK_LM);
1024 	for (i = 0; i < MAX_CHANNELS_PER_ENC; i++) {
1025 		if (!dpu_rm_get_hw(&dpu_kms->rm, &hw_iter))
1026 			break;
1027 		hw_lm[i] = (struct dpu_hw_mixer *)hw_iter.hw;
1028 		num_lm++;
1029 	}
1030 
1031 	cstate = to_dpu_crtc_state(drm_crtc->state);
1032 
1033 	for (i = 0; i < num_lm; i++) {
1034 		int ctl_idx = (i < num_ctl) ? i : (num_ctl-1);
1035 
1036 		cstate->mixers[i].hw_lm = hw_lm[i];
1037 		cstate->mixers[i].lm_ctl = hw_ctl[ctl_idx];
1038 	}
1039 
1040 	cstate->num_mixers = num_lm;
1041 
1042 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1043 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1044 
1045 		if (!dpu_enc->hw_pp[i]) {
1046 			DPU_ERROR_ENC(dpu_enc,
1047 				"no pp block assigned at idx: %d\n", i);
1048 			goto error;
1049 		}
1050 
1051 		if (!hw_ctl[i]) {
1052 			DPU_ERROR_ENC(dpu_enc,
1053 				"no ctl block assigned at idx: %d\n", i);
1054 			goto error;
1055 		}
1056 
1057 		phys->hw_pp = dpu_enc->hw_pp[i];
1058 		phys->hw_ctl = hw_ctl[i];
1059 
1060 		dpu_rm_init_hw_iter(&hw_iter, drm_enc->base.id,
1061 				    DPU_HW_BLK_INTF);
1062 		for (j = 0; j < MAX_CHANNELS_PER_ENC; j++) {
1063 			struct dpu_hw_intf *hw_intf;
1064 
1065 			if (!dpu_rm_get_hw(&dpu_kms->rm, &hw_iter))
1066 				break;
1067 
1068 			hw_intf = (struct dpu_hw_intf *)hw_iter.hw;
1069 			if (hw_intf->idx == phys->intf_idx)
1070 				phys->hw_intf = hw_intf;
1071 		}
1072 
1073 		if (!phys->hw_intf) {
1074 			DPU_ERROR_ENC(dpu_enc,
1075 				      "no intf block assigned at idx: %d\n", i);
1076 				goto error;
1077 		}
1078 
1079 		phys->connector = conn->state->connector;
1080 		if (phys->ops.mode_set)
1081 			phys->ops.mode_set(phys, mode, adj_mode);
1082 	}
1083 
1084 	dpu_enc->mode_set_complete = true;
1085 
1086 error:
1087 	dpu_rm_release(&dpu_kms->rm, drm_enc);
1088 }
1089 
1090 static void _dpu_encoder_virt_enable_helper(struct drm_encoder *drm_enc)
1091 {
1092 	struct dpu_encoder_virt *dpu_enc = NULL;
1093 	struct msm_drm_private *priv;
1094 	struct dpu_kms *dpu_kms;
1095 
1096 	if (!drm_enc || !drm_enc->dev) {
1097 		DPU_ERROR("invalid parameters\n");
1098 		return;
1099 	}
1100 
1101 	priv = drm_enc->dev->dev_private;
1102 	dpu_kms = to_dpu_kms(priv->kms);
1103 
1104 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1105 	if (!dpu_enc || !dpu_enc->cur_master) {
1106 		DPU_ERROR("invalid dpu encoder/master\n");
1107 		return;
1108 	}
1109 
1110 	if (dpu_enc->cur_master->hw_mdptop &&
1111 			dpu_enc->cur_master->hw_mdptop->ops.reset_ubwc)
1112 		dpu_enc->cur_master->hw_mdptop->ops.reset_ubwc(
1113 				dpu_enc->cur_master->hw_mdptop,
1114 				dpu_kms->catalog);
1115 
1116 	_dpu_encoder_update_vsync_source(dpu_enc, &dpu_enc->disp_info);
1117 }
1118 
1119 void dpu_encoder_virt_runtime_resume(struct drm_encoder *drm_enc)
1120 {
1121 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1122 
1123 	mutex_lock(&dpu_enc->enc_lock);
1124 
1125 	if (!dpu_enc->enabled)
1126 		goto out;
1127 
1128 	if (dpu_enc->cur_slave && dpu_enc->cur_slave->ops.restore)
1129 		dpu_enc->cur_slave->ops.restore(dpu_enc->cur_slave);
1130 	if (dpu_enc->cur_master && dpu_enc->cur_master->ops.restore)
1131 		dpu_enc->cur_master->ops.restore(dpu_enc->cur_master);
1132 
1133 	_dpu_encoder_virt_enable_helper(drm_enc);
1134 
1135 out:
1136 	mutex_unlock(&dpu_enc->enc_lock);
1137 }
1138 
1139 static void dpu_encoder_virt_enable(struct drm_encoder *drm_enc)
1140 {
1141 	struct dpu_encoder_virt *dpu_enc = NULL;
1142 	int ret = 0;
1143 	struct drm_display_mode *cur_mode = NULL;
1144 
1145 	if (!drm_enc) {
1146 		DPU_ERROR("invalid encoder\n");
1147 		return;
1148 	}
1149 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1150 
1151 	mutex_lock(&dpu_enc->enc_lock);
1152 	cur_mode = &dpu_enc->base.crtc->state->adjusted_mode;
1153 
1154 	trace_dpu_enc_enable(DRMID(drm_enc), cur_mode->hdisplay,
1155 			     cur_mode->vdisplay);
1156 
1157 	/* always enable slave encoder before master */
1158 	if (dpu_enc->cur_slave && dpu_enc->cur_slave->ops.enable)
1159 		dpu_enc->cur_slave->ops.enable(dpu_enc->cur_slave);
1160 
1161 	if (dpu_enc->cur_master && dpu_enc->cur_master->ops.enable)
1162 		dpu_enc->cur_master->ops.enable(dpu_enc->cur_master);
1163 
1164 	ret = dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_KICKOFF);
1165 	if (ret) {
1166 		DPU_ERROR_ENC(dpu_enc, "dpu resource control failed: %d\n",
1167 				ret);
1168 		goto out;
1169 	}
1170 
1171 	_dpu_encoder_virt_enable_helper(drm_enc);
1172 
1173 	dpu_enc->enabled = true;
1174 
1175 out:
1176 	mutex_unlock(&dpu_enc->enc_lock);
1177 }
1178 
1179 static void dpu_encoder_virt_disable(struct drm_encoder *drm_enc)
1180 {
1181 	struct dpu_encoder_virt *dpu_enc = NULL;
1182 	struct msm_drm_private *priv;
1183 	struct dpu_kms *dpu_kms;
1184 	int i = 0;
1185 
1186 	if (!drm_enc) {
1187 		DPU_ERROR("invalid encoder\n");
1188 		return;
1189 	} else if (!drm_enc->dev) {
1190 		DPU_ERROR("invalid dev\n");
1191 		return;
1192 	}
1193 
1194 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1195 	DPU_DEBUG_ENC(dpu_enc, "\n");
1196 
1197 	mutex_lock(&dpu_enc->enc_lock);
1198 	dpu_enc->enabled = false;
1199 
1200 	priv = drm_enc->dev->dev_private;
1201 	dpu_kms = to_dpu_kms(priv->kms);
1202 
1203 	trace_dpu_enc_disable(DRMID(drm_enc));
1204 
1205 	/* wait for idle */
1206 	dpu_encoder_wait_for_event(drm_enc, MSM_ENC_TX_COMPLETE);
1207 
1208 	dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_PRE_STOP);
1209 
1210 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1211 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1212 
1213 		if (phys->ops.disable)
1214 			phys->ops.disable(phys);
1215 	}
1216 
1217 	/* after phys waits for frame-done, should be no more frames pending */
1218 	if (atomic_xchg(&dpu_enc->frame_done_timeout_ms, 0)) {
1219 		DPU_ERROR("enc%d timeout pending\n", drm_enc->base.id);
1220 		del_timer_sync(&dpu_enc->frame_done_timer);
1221 	}
1222 
1223 	dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_STOP);
1224 
1225 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1226 		dpu_enc->phys_encs[i]->connector = NULL;
1227 	}
1228 
1229 	DPU_DEBUG_ENC(dpu_enc, "encoder disabled\n");
1230 
1231 	dpu_rm_release(&dpu_kms->rm, drm_enc);
1232 
1233 	mutex_unlock(&dpu_enc->enc_lock);
1234 }
1235 
1236 static enum dpu_intf dpu_encoder_get_intf(struct dpu_mdss_cfg *catalog,
1237 		enum dpu_intf_type type, u32 controller_id)
1238 {
1239 	int i = 0;
1240 
1241 	for (i = 0; i < catalog->intf_count; i++) {
1242 		if (catalog->intf[i].type == type
1243 		    && catalog->intf[i].controller_id == controller_id) {
1244 			return catalog->intf[i].id;
1245 		}
1246 	}
1247 
1248 	return INTF_MAX;
1249 }
1250 
1251 static void dpu_encoder_vblank_callback(struct drm_encoder *drm_enc,
1252 		struct dpu_encoder_phys *phy_enc)
1253 {
1254 	struct dpu_encoder_virt *dpu_enc = NULL;
1255 	unsigned long lock_flags;
1256 
1257 	if (!drm_enc || !phy_enc)
1258 		return;
1259 
1260 	DPU_ATRACE_BEGIN("encoder_vblank_callback");
1261 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1262 
1263 	spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1264 	if (dpu_enc->crtc)
1265 		dpu_crtc_vblank_callback(dpu_enc->crtc);
1266 	spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1267 
1268 	atomic_inc(&phy_enc->vsync_cnt);
1269 	DPU_ATRACE_END("encoder_vblank_callback");
1270 }
1271 
1272 static void dpu_encoder_underrun_callback(struct drm_encoder *drm_enc,
1273 		struct dpu_encoder_phys *phy_enc)
1274 {
1275 	if (!phy_enc)
1276 		return;
1277 
1278 	DPU_ATRACE_BEGIN("encoder_underrun_callback");
1279 	atomic_inc(&phy_enc->underrun_cnt);
1280 	trace_dpu_enc_underrun_cb(DRMID(drm_enc),
1281 				  atomic_read(&phy_enc->underrun_cnt));
1282 	DPU_ATRACE_END("encoder_underrun_callback");
1283 }
1284 
1285 void dpu_encoder_assign_crtc(struct drm_encoder *drm_enc, struct drm_crtc *crtc)
1286 {
1287 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1288 	unsigned long lock_flags;
1289 
1290 	spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1291 	/* crtc should always be cleared before re-assigning */
1292 	WARN_ON(crtc && dpu_enc->crtc);
1293 	dpu_enc->crtc = crtc;
1294 	spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1295 }
1296 
1297 void dpu_encoder_toggle_vblank_for_crtc(struct drm_encoder *drm_enc,
1298 					struct drm_crtc *crtc, bool enable)
1299 {
1300 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1301 	unsigned long lock_flags;
1302 	int i;
1303 
1304 	trace_dpu_enc_vblank_cb(DRMID(drm_enc), enable);
1305 
1306 	spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1307 	if (dpu_enc->crtc != crtc) {
1308 		spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1309 		return;
1310 	}
1311 	spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1312 
1313 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1314 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1315 
1316 		if (phys->ops.control_vblank_irq)
1317 			phys->ops.control_vblank_irq(phys, enable);
1318 	}
1319 }
1320 
1321 void dpu_encoder_register_frame_event_callback(struct drm_encoder *drm_enc,
1322 		void (*frame_event_cb)(void *, u32 event),
1323 		void *frame_event_cb_data)
1324 {
1325 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1326 	unsigned long lock_flags;
1327 	bool enable;
1328 
1329 	enable = frame_event_cb ? true : false;
1330 
1331 	if (!drm_enc) {
1332 		DPU_ERROR("invalid encoder\n");
1333 		return;
1334 	}
1335 	trace_dpu_enc_frame_event_cb(DRMID(drm_enc), enable);
1336 
1337 	spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1338 	dpu_enc->crtc_frame_event_cb = frame_event_cb;
1339 	dpu_enc->crtc_frame_event_cb_data = frame_event_cb_data;
1340 	spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1341 }
1342 
1343 static void dpu_encoder_frame_done_callback(
1344 		struct drm_encoder *drm_enc,
1345 		struct dpu_encoder_phys *ready_phys, u32 event)
1346 {
1347 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1348 	unsigned int i;
1349 
1350 	if (event & (DPU_ENCODER_FRAME_EVENT_DONE
1351 			| DPU_ENCODER_FRAME_EVENT_ERROR
1352 			| DPU_ENCODER_FRAME_EVENT_PANEL_DEAD)) {
1353 
1354 		if (!dpu_enc->frame_busy_mask[0]) {
1355 			/**
1356 			 * suppress frame_done without waiter,
1357 			 * likely autorefresh
1358 			 */
1359 			trace_dpu_enc_frame_done_cb_not_busy(DRMID(drm_enc),
1360 					event, ready_phys->intf_idx);
1361 			return;
1362 		}
1363 
1364 		/* One of the physical encoders has become idle */
1365 		for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1366 			if (dpu_enc->phys_encs[i] == ready_phys) {
1367 				trace_dpu_enc_frame_done_cb(DRMID(drm_enc), i,
1368 						dpu_enc->frame_busy_mask[0]);
1369 				clear_bit(i, dpu_enc->frame_busy_mask);
1370 			}
1371 		}
1372 
1373 		if (!dpu_enc->frame_busy_mask[0]) {
1374 			atomic_set(&dpu_enc->frame_done_timeout_ms, 0);
1375 			del_timer(&dpu_enc->frame_done_timer);
1376 
1377 			dpu_encoder_resource_control(drm_enc,
1378 					DPU_ENC_RC_EVENT_FRAME_DONE);
1379 
1380 			if (dpu_enc->crtc_frame_event_cb)
1381 				dpu_enc->crtc_frame_event_cb(
1382 					dpu_enc->crtc_frame_event_cb_data,
1383 					event);
1384 		}
1385 	} else {
1386 		if (dpu_enc->crtc_frame_event_cb)
1387 			dpu_enc->crtc_frame_event_cb(
1388 				dpu_enc->crtc_frame_event_cb_data, event);
1389 	}
1390 }
1391 
1392 static void dpu_encoder_off_work(struct work_struct *work)
1393 {
1394 	struct dpu_encoder_virt *dpu_enc = container_of(work,
1395 			struct dpu_encoder_virt, delayed_off_work.work);
1396 
1397 	if (!dpu_enc) {
1398 		DPU_ERROR("invalid dpu encoder\n");
1399 		return;
1400 	}
1401 
1402 	dpu_encoder_resource_control(&dpu_enc->base,
1403 						DPU_ENC_RC_EVENT_ENTER_IDLE);
1404 
1405 	dpu_encoder_frame_done_callback(&dpu_enc->base, NULL,
1406 				DPU_ENCODER_FRAME_EVENT_IDLE);
1407 }
1408 
1409 /**
1410  * _dpu_encoder_trigger_flush - trigger flush for a physical encoder
1411  * drm_enc: Pointer to drm encoder structure
1412  * phys: Pointer to physical encoder structure
1413  * extra_flush_bits: Additional bit mask to include in flush trigger
1414  */
1415 static void _dpu_encoder_trigger_flush(struct drm_encoder *drm_enc,
1416 		struct dpu_encoder_phys *phys, uint32_t extra_flush_bits)
1417 {
1418 	struct dpu_hw_ctl *ctl;
1419 	int pending_kickoff_cnt;
1420 	u32 ret = UINT_MAX;
1421 
1422 	if (!phys->hw_pp) {
1423 		DPU_ERROR("invalid pingpong hw\n");
1424 		return;
1425 	}
1426 
1427 	ctl = phys->hw_ctl;
1428 	if (!ctl->ops.trigger_flush) {
1429 		DPU_ERROR("missing trigger cb\n");
1430 		return;
1431 	}
1432 
1433 	pending_kickoff_cnt = dpu_encoder_phys_inc_pending(phys);
1434 
1435 	if (extra_flush_bits && ctl->ops.update_pending_flush)
1436 		ctl->ops.update_pending_flush(ctl, extra_flush_bits);
1437 
1438 	ctl->ops.trigger_flush(ctl);
1439 
1440 	if (ctl->ops.get_pending_flush)
1441 		ret = ctl->ops.get_pending_flush(ctl);
1442 
1443 	trace_dpu_enc_trigger_flush(DRMID(drm_enc), phys->intf_idx,
1444 				    pending_kickoff_cnt, ctl->idx,
1445 				    extra_flush_bits, ret);
1446 }
1447 
1448 /**
1449  * _dpu_encoder_trigger_start - trigger start for a physical encoder
1450  * phys: Pointer to physical encoder structure
1451  */
1452 static void _dpu_encoder_trigger_start(struct dpu_encoder_phys *phys)
1453 {
1454 	if (!phys) {
1455 		DPU_ERROR("invalid argument(s)\n");
1456 		return;
1457 	}
1458 
1459 	if (!phys->hw_pp) {
1460 		DPU_ERROR("invalid pingpong hw\n");
1461 		return;
1462 	}
1463 
1464 	if (phys->ops.trigger_start && phys->enable_state != DPU_ENC_DISABLED)
1465 		phys->ops.trigger_start(phys);
1466 }
1467 
1468 void dpu_encoder_helper_trigger_start(struct dpu_encoder_phys *phys_enc)
1469 {
1470 	struct dpu_hw_ctl *ctl;
1471 
1472 	ctl = phys_enc->hw_ctl;
1473 	if (ctl->ops.trigger_start) {
1474 		ctl->ops.trigger_start(ctl);
1475 		trace_dpu_enc_trigger_start(DRMID(phys_enc->parent), ctl->idx);
1476 	}
1477 }
1478 
1479 static int dpu_encoder_helper_wait_event_timeout(
1480 		int32_t drm_id,
1481 		int32_t hw_id,
1482 		struct dpu_encoder_wait_info *info)
1483 {
1484 	int rc = 0;
1485 	s64 expected_time = ktime_to_ms(ktime_get()) + info->timeout_ms;
1486 	s64 jiffies = msecs_to_jiffies(info->timeout_ms);
1487 	s64 time;
1488 
1489 	do {
1490 		rc = wait_event_timeout(*(info->wq),
1491 				atomic_read(info->atomic_cnt) == 0, jiffies);
1492 		time = ktime_to_ms(ktime_get());
1493 
1494 		trace_dpu_enc_wait_event_timeout(drm_id, hw_id, rc, time,
1495 						 expected_time,
1496 						 atomic_read(info->atomic_cnt));
1497 	/* If we timed out, counter is valid and time is less, wait again */
1498 	} while (atomic_read(info->atomic_cnt) && (rc == 0) &&
1499 			(time < expected_time));
1500 
1501 	return rc;
1502 }
1503 
1504 static void dpu_encoder_helper_hw_reset(struct dpu_encoder_phys *phys_enc)
1505 {
1506 	struct dpu_encoder_virt *dpu_enc;
1507 	struct dpu_hw_ctl *ctl;
1508 	int rc;
1509 
1510 	dpu_enc = to_dpu_encoder_virt(phys_enc->parent);
1511 	ctl = phys_enc->hw_ctl;
1512 
1513 	if (!ctl->ops.reset)
1514 		return;
1515 
1516 	DRM_DEBUG_KMS("id:%u ctl %d reset\n", DRMID(phys_enc->parent),
1517 		      ctl->idx);
1518 
1519 	rc = ctl->ops.reset(ctl);
1520 	if (rc)
1521 		DPU_ERROR_ENC(dpu_enc, "ctl %d reset failure\n",  ctl->idx);
1522 
1523 	phys_enc->enable_state = DPU_ENC_ENABLED;
1524 }
1525 
1526 /**
1527  * _dpu_encoder_kickoff_phys - handle physical encoder kickoff
1528  *	Iterate through the physical encoders and perform consolidated flush
1529  *	and/or control start triggering as needed. This is done in the virtual
1530  *	encoder rather than the individual physical ones in order to handle
1531  *	use cases that require visibility into multiple physical encoders at
1532  *	a time.
1533  * dpu_enc: Pointer to virtual encoder structure
1534  */
1535 static void _dpu_encoder_kickoff_phys(struct dpu_encoder_virt *dpu_enc)
1536 {
1537 	struct dpu_hw_ctl *ctl;
1538 	uint32_t i, pending_flush;
1539 	unsigned long lock_flags;
1540 
1541 	pending_flush = 0x0;
1542 
1543 	/* update pending counts and trigger kickoff ctl flush atomically */
1544 	spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1545 
1546 	/* don't perform flush/start operations for slave encoders */
1547 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1548 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1549 
1550 		if (phys->enable_state == DPU_ENC_DISABLED)
1551 			continue;
1552 
1553 		ctl = phys->hw_ctl;
1554 
1555 		/*
1556 		 * This is cleared in frame_done worker, which isn't invoked
1557 		 * for async commits. So don't set this for async, since it'll
1558 		 * roll over to the next commit.
1559 		 */
1560 		if (phys->split_role != ENC_ROLE_SLAVE)
1561 			set_bit(i, dpu_enc->frame_busy_mask);
1562 
1563 		if (!phys->ops.needs_single_flush ||
1564 				!phys->ops.needs_single_flush(phys))
1565 			_dpu_encoder_trigger_flush(&dpu_enc->base, phys, 0x0);
1566 		else if (ctl->ops.get_pending_flush)
1567 			pending_flush |= ctl->ops.get_pending_flush(ctl);
1568 	}
1569 
1570 	/* for split flush, combine pending flush masks and send to master */
1571 	if (pending_flush && dpu_enc->cur_master) {
1572 		_dpu_encoder_trigger_flush(
1573 				&dpu_enc->base,
1574 				dpu_enc->cur_master,
1575 				pending_flush);
1576 	}
1577 
1578 	_dpu_encoder_trigger_start(dpu_enc->cur_master);
1579 
1580 	spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1581 }
1582 
1583 void dpu_encoder_trigger_kickoff_pending(struct drm_encoder *drm_enc)
1584 {
1585 	struct dpu_encoder_virt *dpu_enc;
1586 	struct dpu_encoder_phys *phys;
1587 	unsigned int i;
1588 	struct dpu_hw_ctl *ctl;
1589 	struct msm_display_info *disp_info;
1590 
1591 	if (!drm_enc) {
1592 		DPU_ERROR("invalid encoder\n");
1593 		return;
1594 	}
1595 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1596 	disp_info = &dpu_enc->disp_info;
1597 
1598 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1599 		phys = dpu_enc->phys_encs[i];
1600 
1601 		ctl = phys->hw_ctl;
1602 		if (ctl->ops.clear_pending_flush)
1603 			ctl->ops.clear_pending_flush(ctl);
1604 
1605 		/* update only for command mode primary ctl */
1606 		if ((phys == dpu_enc->cur_master) &&
1607 		   (disp_info->capabilities & MSM_DISPLAY_CAP_CMD_MODE)
1608 		    && ctl->ops.trigger_pending)
1609 			ctl->ops.trigger_pending(ctl);
1610 	}
1611 }
1612 
1613 static u32 _dpu_encoder_calculate_linetime(struct dpu_encoder_virt *dpu_enc,
1614 		struct drm_display_mode *mode)
1615 {
1616 	u64 pclk_rate;
1617 	u32 pclk_period;
1618 	u32 line_time;
1619 
1620 	/*
1621 	 * For linetime calculation, only operate on master encoder.
1622 	 */
1623 	if (!dpu_enc->cur_master)
1624 		return 0;
1625 
1626 	if (!dpu_enc->cur_master->ops.get_line_count) {
1627 		DPU_ERROR("get_line_count function not defined\n");
1628 		return 0;
1629 	}
1630 
1631 	pclk_rate = mode->clock; /* pixel clock in kHz */
1632 	if (pclk_rate == 0) {
1633 		DPU_ERROR("pclk is 0, cannot calculate line time\n");
1634 		return 0;
1635 	}
1636 
1637 	pclk_period = DIV_ROUND_UP_ULL(1000000000ull, pclk_rate);
1638 	if (pclk_period == 0) {
1639 		DPU_ERROR("pclk period is 0\n");
1640 		return 0;
1641 	}
1642 
1643 	/*
1644 	 * Line time calculation based on Pixel clock and HTOTAL.
1645 	 * Final unit is in ns.
1646 	 */
1647 	line_time = (pclk_period * mode->htotal) / 1000;
1648 	if (line_time == 0) {
1649 		DPU_ERROR("line time calculation is 0\n");
1650 		return 0;
1651 	}
1652 
1653 	DPU_DEBUG_ENC(dpu_enc,
1654 			"clk_rate=%lldkHz, clk_period=%d, linetime=%dns\n",
1655 			pclk_rate, pclk_period, line_time);
1656 
1657 	return line_time;
1658 }
1659 
1660 int dpu_encoder_vsync_time(struct drm_encoder *drm_enc, ktime_t *wakeup_time)
1661 {
1662 	struct drm_display_mode *mode;
1663 	struct dpu_encoder_virt *dpu_enc;
1664 	u32 cur_line;
1665 	u32 line_time;
1666 	u32 vtotal, time_to_vsync;
1667 	ktime_t cur_time;
1668 
1669 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1670 
1671 	if (!drm_enc->crtc || !drm_enc->crtc->state) {
1672 		DPU_ERROR("crtc/crtc state object is NULL\n");
1673 		return -EINVAL;
1674 	}
1675 	mode = &drm_enc->crtc->state->adjusted_mode;
1676 
1677 	line_time = _dpu_encoder_calculate_linetime(dpu_enc, mode);
1678 	if (!line_time)
1679 		return -EINVAL;
1680 
1681 	cur_line = dpu_enc->cur_master->ops.get_line_count(dpu_enc->cur_master);
1682 
1683 	vtotal = mode->vtotal;
1684 	if (cur_line >= vtotal)
1685 		time_to_vsync = line_time * vtotal;
1686 	else
1687 		time_to_vsync = line_time * (vtotal - cur_line);
1688 
1689 	if (time_to_vsync == 0) {
1690 		DPU_ERROR("time to vsync should not be zero, vtotal=%d\n",
1691 				vtotal);
1692 		return -EINVAL;
1693 	}
1694 
1695 	cur_time = ktime_get();
1696 	*wakeup_time = ktime_add_ns(cur_time, time_to_vsync);
1697 
1698 	DPU_DEBUG_ENC(dpu_enc,
1699 			"cur_line=%u vtotal=%u time_to_vsync=%u, cur_time=%lld, wakeup_time=%lld\n",
1700 			cur_line, vtotal, time_to_vsync,
1701 			ktime_to_ms(cur_time),
1702 			ktime_to_ms(*wakeup_time));
1703 	return 0;
1704 }
1705 
1706 static void dpu_encoder_vsync_event_handler(struct timer_list *t)
1707 {
1708 	struct dpu_encoder_virt *dpu_enc = from_timer(dpu_enc, t,
1709 			vsync_event_timer);
1710 	struct drm_encoder *drm_enc = &dpu_enc->base;
1711 	struct msm_drm_private *priv;
1712 	struct msm_drm_thread *event_thread;
1713 
1714 	if (!drm_enc->dev || !drm_enc->crtc) {
1715 		DPU_ERROR("invalid parameters\n");
1716 		return;
1717 	}
1718 
1719 	priv = drm_enc->dev->dev_private;
1720 
1721 	if (drm_enc->crtc->index >= ARRAY_SIZE(priv->event_thread)) {
1722 		DPU_ERROR("invalid crtc index\n");
1723 		return;
1724 	}
1725 	event_thread = &priv->event_thread[drm_enc->crtc->index];
1726 	if (!event_thread) {
1727 		DPU_ERROR("event_thread not found for crtc:%d\n",
1728 				drm_enc->crtc->index);
1729 		return;
1730 	}
1731 
1732 	del_timer(&dpu_enc->vsync_event_timer);
1733 }
1734 
1735 static void dpu_encoder_vsync_event_work_handler(struct kthread_work *work)
1736 {
1737 	struct dpu_encoder_virt *dpu_enc = container_of(work,
1738 			struct dpu_encoder_virt, vsync_event_work);
1739 	ktime_t wakeup_time;
1740 
1741 	if (!dpu_enc) {
1742 		DPU_ERROR("invalid dpu encoder\n");
1743 		return;
1744 	}
1745 
1746 	if (dpu_encoder_vsync_time(&dpu_enc->base, &wakeup_time))
1747 		return;
1748 
1749 	trace_dpu_enc_vsync_event_work(DRMID(&dpu_enc->base), wakeup_time);
1750 	mod_timer(&dpu_enc->vsync_event_timer,
1751 			nsecs_to_jiffies(ktime_to_ns(wakeup_time)));
1752 }
1753 
1754 void dpu_encoder_prepare_for_kickoff(struct drm_encoder *drm_enc)
1755 {
1756 	struct dpu_encoder_virt *dpu_enc;
1757 	struct dpu_encoder_phys *phys;
1758 	bool needs_hw_reset = false;
1759 	unsigned int i;
1760 
1761 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1762 
1763 	trace_dpu_enc_prepare_kickoff(DRMID(drm_enc));
1764 
1765 	/* prepare for next kickoff, may include waiting on previous kickoff */
1766 	DPU_ATRACE_BEGIN("enc_prepare_for_kickoff");
1767 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1768 		phys = dpu_enc->phys_encs[i];
1769 		if (phys->ops.prepare_for_kickoff)
1770 			phys->ops.prepare_for_kickoff(phys);
1771 		if (phys->enable_state == DPU_ENC_ERR_NEEDS_HW_RESET)
1772 			needs_hw_reset = true;
1773 	}
1774 	DPU_ATRACE_END("enc_prepare_for_kickoff");
1775 
1776 	dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_KICKOFF);
1777 
1778 	/* if any phys needs reset, reset all phys, in-order */
1779 	if (needs_hw_reset) {
1780 		trace_dpu_enc_prepare_kickoff_reset(DRMID(drm_enc));
1781 		for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1782 			dpu_encoder_helper_hw_reset(dpu_enc->phys_encs[i]);
1783 		}
1784 	}
1785 }
1786 
1787 void dpu_encoder_kickoff(struct drm_encoder *drm_enc)
1788 {
1789 	struct dpu_encoder_virt *dpu_enc;
1790 	struct dpu_encoder_phys *phys;
1791 	ktime_t wakeup_time;
1792 	unsigned long timeout_ms;
1793 	unsigned int i;
1794 
1795 	DPU_ATRACE_BEGIN("encoder_kickoff");
1796 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1797 
1798 	trace_dpu_enc_kickoff(DRMID(drm_enc));
1799 
1800 	timeout_ms = DPU_ENCODER_FRAME_DONE_TIMEOUT_FRAMES * 1000 /
1801 			drm_mode_vrefresh(&drm_enc->crtc->state->adjusted_mode);
1802 
1803 	atomic_set(&dpu_enc->frame_done_timeout_ms, timeout_ms);
1804 	mod_timer(&dpu_enc->frame_done_timer,
1805 			jiffies + msecs_to_jiffies(timeout_ms));
1806 
1807 	/* All phys encs are ready to go, trigger the kickoff */
1808 	_dpu_encoder_kickoff_phys(dpu_enc);
1809 
1810 	/* allow phys encs to handle any post-kickoff business */
1811 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1812 		phys = dpu_enc->phys_encs[i];
1813 		if (phys->ops.handle_post_kickoff)
1814 			phys->ops.handle_post_kickoff(phys);
1815 	}
1816 
1817 	if (dpu_enc->disp_info.intf_type == DRM_MODE_ENCODER_DSI &&
1818 			!dpu_encoder_vsync_time(drm_enc, &wakeup_time)) {
1819 		trace_dpu_enc_early_kickoff(DRMID(drm_enc),
1820 					    ktime_to_ms(wakeup_time));
1821 		mod_timer(&dpu_enc->vsync_event_timer,
1822 				nsecs_to_jiffies(ktime_to_ns(wakeup_time)));
1823 	}
1824 
1825 	DPU_ATRACE_END("encoder_kickoff");
1826 }
1827 
1828 void dpu_encoder_prepare_commit(struct drm_encoder *drm_enc)
1829 {
1830 	struct dpu_encoder_virt *dpu_enc;
1831 	struct dpu_encoder_phys *phys;
1832 	int i;
1833 
1834 	if (!drm_enc) {
1835 		DPU_ERROR("invalid encoder\n");
1836 		return;
1837 	}
1838 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1839 
1840 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1841 		phys = dpu_enc->phys_encs[i];
1842 		if (phys->ops.prepare_commit)
1843 			phys->ops.prepare_commit(phys);
1844 	}
1845 }
1846 
1847 #ifdef CONFIG_DEBUG_FS
1848 static int _dpu_encoder_status_show(struct seq_file *s, void *data)
1849 {
1850 	struct dpu_encoder_virt *dpu_enc = s->private;
1851 	int i;
1852 
1853 	mutex_lock(&dpu_enc->enc_lock);
1854 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1855 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1856 
1857 		seq_printf(s, "intf:%d    vsync:%8d     underrun:%8d    ",
1858 				phys->intf_idx - INTF_0,
1859 				atomic_read(&phys->vsync_cnt),
1860 				atomic_read(&phys->underrun_cnt));
1861 
1862 		switch (phys->intf_mode) {
1863 		case INTF_MODE_VIDEO:
1864 			seq_puts(s, "mode: video\n");
1865 			break;
1866 		case INTF_MODE_CMD:
1867 			seq_puts(s, "mode: command\n");
1868 			break;
1869 		default:
1870 			seq_puts(s, "mode: ???\n");
1871 			break;
1872 		}
1873 	}
1874 	mutex_unlock(&dpu_enc->enc_lock);
1875 
1876 	return 0;
1877 }
1878 
1879 static int _dpu_encoder_debugfs_status_open(struct inode *inode,
1880 		struct file *file)
1881 {
1882 	return single_open(file, _dpu_encoder_status_show, inode->i_private);
1883 }
1884 
1885 static int _dpu_encoder_init_debugfs(struct drm_encoder *drm_enc)
1886 {
1887 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1888 	int i;
1889 
1890 	static const struct file_operations debugfs_status_fops = {
1891 		.open =		_dpu_encoder_debugfs_status_open,
1892 		.read =		seq_read,
1893 		.llseek =	seq_lseek,
1894 		.release =	single_release,
1895 	};
1896 
1897 	char name[DPU_NAME_SIZE];
1898 
1899 	if (!drm_enc->dev) {
1900 		DPU_ERROR("invalid encoder or kms\n");
1901 		return -EINVAL;
1902 	}
1903 
1904 	snprintf(name, DPU_NAME_SIZE, "encoder%u", drm_enc->base.id);
1905 
1906 	/* create overall sub-directory for the encoder */
1907 	dpu_enc->debugfs_root = debugfs_create_dir(name,
1908 			drm_enc->dev->primary->debugfs_root);
1909 
1910 	/* don't error check these */
1911 	debugfs_create_file("status", 0600,
1912 		dpu_enc->debugfs_root, dpu_enc, &debugfs_status_fops);
1913 
1914 	for (i = 0; i < dpu_enc->num_phys_encs; i++)
1915 		if (dpu_enc->phys_encs[i]->ops.late_register)
1916 			dpu_enc->phys_encs[i]->ops.late_register(
1917 					dpu_enc->phys_encs[i],
1918 					dpu_enc->debugfs_root);
1919 
1920 	return 0;
1921 }
1922 #else
1923 static int _dpu_encoder_init_debugfs(struct drm_encoder *drm_enc)
1924 {
1925 	return 0;
1926 }
1927 #endif
1928 
1929 static int dpu_encoder_late_register(struct drm_encoder *encoder)
1930 {
1931 	return _dpu_encoder_init_debugfs(encoder);
1932 }
1933 
1934 static void dpu_encoder_early_unregister(struct drm_encoder *encoder)
1935 {
1936 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(encoder);
1937 
1938 	debugfs_remove_recursive(dpu_enc->debugfs_root);
1939 }
1940 
1941 static int dpu_encoder_virt_add_phys_encs(
1942 		u32 display_caps,
1943 		struct dpu_encoder_virt *dpu_enc,
1944 		struct dpu_enc_phys_init_params *params)
1945 {
1946 	struct dpu_encoder_phys *enc = NULL;
1947 
1948 	DPU_DEBUG_ENC(dpu_enc, "\n");
1949 
1950 	/*
1951 	 * We may create up to NUM_PHYS_ENCODER_TYPES physical encoder types
1952 	 * in this function, check up-front.
1953 	 */
1954 	if (dpu_enc->num_phys_encs + NUM_PHYS_ENCODER_TYPES >=
1955 			ARRAY_SIZE(dpu_enc->phys_encs)) {
1956 		DPU_ERROR_ENC(dpu_enc, "too many physical encoders %d\n",
1957 			  dpu_enc->num_phys_encs);
1958 		return -EINVAL;
1959 	}
1960 
1961 	if (display_caps & MSM_DISPLAY_CAP_VID_MODE) {
1962 		enc = dpu_encoder_phys_vid_init(params);
1963 
1964 		if (IS_ERR_OR_NULL(enc)) {
1965 			DPU_ERROR_ENC(dpu_enc, "failed to init vid enc: %ld\n",
1966 				PTR_ERR(enc));
1967 			return enc == 0 ? -EINVAL : PTR_ERR(enc);
1968 		}
1969 
1970 		dpu_enc->phys_encs[dpu_enc->num_phys_encs] = enc;
1971 		++dpu_enc->num_phys_encs;
1972 	}
1973 
1974 	if (display_caps & MSM_DISPLAY_CAP_CMD_MODE) {
1975 		enc = dpu_encoder_phys_cmd_init(params);
1976 
1977 		if (IS_ERR_OR_NULL(enc)) {
1978 			DPU_ERROR_ENC(dpu_enc, "failed to init cmd enc: %ld\n",
1979 				PTR_ERR(enc));
1980 			return enc == 0 ? -EINVAL : PTR_ERR(enc);
1981 		}
1982 
1983 		dpu_enc->phys_encs[dpu_enc->num_phys_encs] = enc;
1984 		++dpu_enc->num_phys_encs;
1985 	}
1986 
1987 	if (params->split_role == ENC_ROLE_SLAVE)
1988 		dpu_enc->cur_slave = enc;
1989 	else
1990 		dpu_enc->cur_master = enc;
1991 
1992 	return 0;
1993 }
1994 
1995 static const struct dpu_encoder_virt_ops dpu_encoder_parent_ops = {
1996 	.handle_vblank_virt = dpu_encoder_vblank_callback,
1997 	.handle_underrun_virt = dpu_encoder_underrun_callback,
1998 	.handle_frame_done = dpu_encoder_frame_done_callback,
1999 };
2000 
2001 static int dpu_encoder_setup_display(struct dpu_encoder_virt *dpu_enc,
2002 				 struct dpu_kms *dpu_kms,
2003 				 struct msm_display_info *disp_info)
2004 {
2005 	int ret = 0;
2006 	int i = 0;
2007 	enum dpu_intf_type intf_type;
2008 	struct dpu_enc_phys_init_params phys_params;
2009 
2010 	if (!dpu_enc) {
2011 		DPU_ERROR("invalid arg(s), enc %d\n", dpu_enc != 0);
2012 		return -EINVAL;
2013 	}
2014 
2015 	dpu_enc->cur_master = NULL;
2016 
2017 	memset(&phys_params, 0, sizeof(phys_params));
2018 	phys_params.dpu_kms = dpu_kms;
2019 	phys_params.parent = &dpu_enc->base;
2020 	phys_params.parent_ops = &dpu_encoder_parent_ops;
2021 	phys_params.enc_spinlock = &dpu_enc->enc_spinlock;
2022 
2023 	DPU_DEBUG("\n");
2024 
2025 	switch (disp_info->intf_type) {
2026 	case DRM_MODE_ENCODER_DSI:
2027 		intf_type = INTF_DSI;
2028 		break;
2029 	default:
2030 		DPU_ERROR_ENC(dpu_enc, "unsupported display interface type\n");
2031 		return -EINVAL;
2032 	}
2033 
2034 	WARN_ON(disp_info->num_of_h_tiles < 1);
2035 
2036 	DPU_DEBUG("dsi_info->num_of_h_tiles %d\n", disp_info->num_of_h_tiles);
2037 
2038 	if ((disp_info->capabilities & MSM_DISPLAY_CAP_CMD_MODE) ||
2039 	    (disp_info->capabilities & MSM_DISPLAY_CAP_VID_MODE))
2040 		dpu_enc->idle_pc_supported =
2041 				dpu_kms->catalog->caps->has_idle_pc;
2042 
2043 	mutex_lock(&dpu_enc->enc_lock);
2044 	for (i = 0; i < disp_info->num_of_h_tiles && !ret; i++) {
2045 		/*
2046 		 * Left-most tile is at index 0, content is controller id
2047 		 * h_tile_instance_ids[2] = {0, 1}; DSI0 = left, DSI1 = right
2048 		 * h_tile_instance_ids[2] = {1, 0}; DSI1 = left, DSI0 = right
2049 		 */
2050 		u32 controller_id = disp_info->h_tile_instance[i];
2051 
2052 		if (disp_info->num_of_h_tiles > 1) {
2053 			if (i == 0)
2054 				phys_params.split_role = ENC_ROLE_MASTER;
2055 			else
2056 				phys_params.split_role = ENC_ROLE_SLAVE;
2057 		} else {
2058 			phys_params.split_role = ENC_ROLE_SOLO;
2059 		}
2060 
2061 		DPU_DEBUG("h_tile_instance %d = %d, split_role %d\n",
2062 				i, controller_id, phys_params.split_role);
2063 
2064 		phys_params.intf_idx = dpu_encoder_get_intf(dpu_kms->catalog,
2065 													intf_type,
2066 													controller_id);
2067 		if (phys_params.intf_idx == INTF_MAX) {
2068 			DPU_ERROR_ENC(dpu_enc, "could not get intf: type %d, id %d\n",
2069 						  intf_type, controller_id);
2070 			ret = -EINVAL;
2071 		}
2072 
2073 		if (!ret) {
2074 			ret = dpu_encoder_virt_add_phys_encs(disp_info->capabilities,
2075 												 dpu_enc,
2076 												 &phys_params);
2077 			if (ret)
2078 				DPU_ERROR_ENC(dpu_enc, "failed to add phys encs\n");
2079 		}
2080 	}
2081 
2082 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2083 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
2084 		atomic_set(&phys->vsync_cnt, 0);
2085 		atomic_set(&phys->underrun_cnt, 0);
2086 	}
2087 	mutex_unlock(&dpu_enc->enc_lock);
2088 
2089 	return ret;
2090 }
2091 
2092 static void dpu_encoder_frame_done_timeout(struct timer_list *t)
2093 {
2094 	struct dpu_encoder_virt *dpu_enc = from_timer(dpu_enc, t,
2095 			frame_done_timer);
2096 	struct drm_encoder *drm_enc = &dpu_enc->base;
2097 	u32 event;
2098 
2099 	if (!drm_enc->dev) {
2100 		DPU_ERROR("invalid parameters\n");
2101 		return;
2102 	}
2103 
2104 	if (!dpu_enc->frame_busy_mask[0] || !dpu_enc->crtc_frame_event_cb) {
2105 		DRM_DEBUG_KMS("id:%u invalid timeout frame_busy_mask=%lu\n",
2106 			      DRMID(drm_enc), dpu_enc->frame_busy_mask[0]);
2107 		return;
2108 	} else if (!atomic_xchg(&dpu_enc->frame_done_timeout_ms, 0)) {
2109 		DRM_DEBUG_KMS("id:%u invalid timeout\n", DRMID(drm_enc));
2110 		return;
2111 	}
2112 
2113 	DPU_ERROR_ENC(dpu_enc, "frame done timeout\n");
2114 
2115 	event = DPU_ENCODER_FRAME_EVENT_ERROR;
2116 	trace_dpu_enc_frame_done_timeout(DRMID(drm_enc), event);
2117 	dpu_enc->crtc_frame_event_cb(dpu_enc->crtc_frame_event_cb_data, event);
2118 }
2119 
2120 static const struct drm_encoder_helper_funcs dpu_encoder_helper_funcs = {
2121 	.mode_set = dpu_encoder_virt_mode_set,
2122 	.disable = dpu_encoder_virt_disable,
2123 	.enable = dpu_kms_encoder_enable,
2124 	.atomic_check = dpu_encoder_virt_atomic_check,
2125 
2126 	/* This is called by dpu_kms_encoder_enable */
2127 	.commit = dpu_encoder_virt_enable,
2128 };
2129 
2130 static const struct drm_encoder_funcs dpu_encoder_funcs = {
2131 		.destroy = dpu_encoder_destroy,
2132 		.late_register = dpu_encoder_late_register,
2133 		.early_unregister = dpu_encoder_early_unregister,
2134 };
2135 
2136 int dpu_encoder_setup(struct drm_device *dev, struct drm_encoder *enc,
2137 		struct msm_display_info *disp_info)
2138 {
2139 	struct msm_drm_private *priv = dev->dev_private;
2140 	struct dpu_kms *dpu_kms = to_dpu_kms(priv->kms);
2141 	struct drm_encoder *drm_enc = NULL;
2142 	struct dpu_encoder_virt *dpu_enc = NULL;
2143 	int ret = 0;
2144 
2145 	dpu_enc = to_dpu_encoder_virt(enc);
2146 
2147 	mutex_init(&dpu_enc->enc_lock);
2148 	ret = dpu_encoder_setup_display(dpu_enc, dpu_kms, disp_info);
2149 	if (ret)
2150 		goto fail;
2151 
2152 	atomic_set(&dpu_enc->frame_done_timeout_ms, 0);
2153 	timer_setup(&dpu_enc->frame_done_timer,
2154 			dpu_encoder_frame_done_timeout, 0);
2155 
2156 	if (disp_info->intf_type == DRM_MODE_ENCODER_DSI)
2157 		timer_setup(&dpu_enc->vsync_event_timer,
2158 				dpu_encoder_vsync_event_handler,
2159 				0);
2160 
2161 
2162 	mutex_init(&dpu_enc->rc_lock);
2163 	INIT_DELAYED_WORK(&dpu_enc->delayed_off_work,
2164 			dpu_encoder_off_work);
2165 	dpu_enc->idle_timeout = IDLE_TIMEOUT;
2166 
2167 	kthread_init_work(&dpu_enc->vsync_event_work,
2168 			dpu_encoder_vsync_event_work_handler);
2169 
2170 	memcpy(&dpu_enc->disp_info, disp_info, sizeof(*disp_info));
2171 
2172 	DPU_DEBUG_ENC(dpu_enc, "created\n");
2173 
2174 	return ret;
2175 
2176 fail:
2177 	DPU_ERROR("failed to create encoder\n");
2178 	if (drm_enc)
2179 		dpu_encoder_destroy(drm_enc);
2180 
2181 	return ret;
2182 
2183 
2184 }
2185 
2186 struct drm_encoder *dpu_encoder_init(struct drm_device *dev,
2187 		int drm_enc_mode)
2188 {
2189 	struct dpu_encoder_virt *dpu_enc = NULL;
2190 	int rc = 0;
2191 
2192 	dpu_enc = devm_kzalloc(dev->dev, sizeof(*dpu_enc), GFP_KERNEL);
2193 	if (!dpu_enc)
2194 		return ERR_PTR(ENOMEM);
2195 
2196 	rc = drm_encoder_init(dev, &dpu_enc->base, &dpu_encoder_funcs,
2197 			drm_enc_mode, NULL);
2198 	if (rc) {
2199 		devm_kfree(dev->dev, dpu_enc);
2200 		return ERR_PTR(rc);
2201 	}
2202 
2203 	drm_encoder_helper_add(&dpu_enc->base, &dpu_encoder_helper_funcs);
2204 
2205 	spin_lock_init(&dpu_enc->enc_spinlock);
2206 	dpu_enc->enabled = false;
2207 
2208 	return &dpu_enc->base;
2209 }
2210 
2211 int dpu_encoder_wait_for_event(struct drm_encoder *drm_enc,
2212 	enum msm_event_wait event)
2213 {
2214 	int (*fn_wait)(struct dpu_encoder_phys *phys_enc) = NULL;
2215 	struct dpu_encoder_virt *dpu_enc = NULL;
2216 	int i, ret = 0;
2217 
2218 	if (!drm_enc) {
2219 		DPU_ERROR("invalid encoder\n");
2220 		return -EINVAL;
2221 	}
2222 	dpu_enc = to_dpu_encoder_virt(drm_enc);
2223 	DPU_DEBUG_ENC(dpu_enc, "\n");
2224 
2225 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2226 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
2227 
2228 		switch (event) {
2229 		case MSM_ENC_COMMIT_DONE:
2230 			fn_wait = phys->ops.wait_for_commit_done;
2231 			break;
2232 		case MSM_ENC_TX_COMPLETE:
2233 			fn_wait = phys->ops.wait_for_tx_complete;
2234 			break;
2235 		case MSM_ENC_VBLANK:
2236 			fn_wait = phys->ops.wait_for_vblank;
2237 			break;
2238 		default:
2239 			DPU_ERROR_ENC(dpu_enc, "unknown wait event %d\n",
2240 					event);
2241 			return -EINVAL;
2242 		}
2243 
2244 		if (fn_wait) {
2245 			DPU_ATRACE_BEGIN("wait_for_completion_event");
2246 			ret = fn_wait(phys);
2247 			DPU_ATRACE_END("wait_for_completion_event");
2248 			if (ret)
2249 				return ret;
2250 		}
2251 	}
2252 
2253 	return ret;
2254 }
2255 
2256 enum dpu_intf_mode dpu_encoder_get_intf_mode(struct drm_encoder *encoder)
2257 {
2258 	struct dpu_encoder_virt *dpu_enc = NULL;
2259 
2260 	if (!encoder) {
2261 		DPU_ERROR("invalid encoder\n");
2262 		return INTF_MODE_NONE;
2263 	}
2264 	dpu_enc = to_dpu_encoder_virt(encoder);
2265 
2266 	if (dpu_enc->cur_master)
2267 		return dpu_enc->cur_master->intf_mode;
2268 
2269 	if (dpu_enc->num_phys_encs)
2270 		return dpu_enc->phys_encs[0]->intf_mode;
2271 
2272 	return INTF_MODE_NONE;
2273 }
2274