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