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