xref: /linux/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c (revision 94cad89ae4505672ae65457d12f77c44ca87655b)
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 = {0};
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 if available in
524 	 * sufficient number
525 	 */
526 	if (intf_count == 2)
527 		topology.num_lm = 2;
528 	else if (!dpu_kms->catalog->caps->has_3d_merge)
529 		topology.num_lm = 1;
530 	else
531 		topology.num_lm = (mode->hdisplay > MAX_HDISPLAY_SPLIT) ? 2 : 1;
532 
533 	if (dpu_enc->disp_info.intf_type == DRM_MODE_ENCODER_DSI) {
534 		if (dpu_kms->catalog->dspp &&
535 			(dpu_kms->catalog->dspp_count >= topology.num_lm))
536 			topology.num_dspp = topology.num_lm;
537 	}
538 
539 	topology.num_enc = 0;
540 	topology.num_intf = intf_count;
541 
542 	return topology;
543 }
544 static int dpu_encoder_virt_atomic_check(
545 		struct drm_encoder *drm_enc,
546 		struct drm_crtc_state *crtc_state,
547 		struct drm_connector_state *conn_state)
548 {
549 	struct dpu_encoder_virt *dpu_enc;
550 	struct msm_drm_private *priv;
551 	struct dpu_kms *dpu_kms;
552 	const struct drm_display_mode *mode;
553 	struct drm_display_mode *adj_mode;
554 	struct msm_display_topology topology;
555 	struct dpu_global_state *global_state;
556 	int i = 0;
557 	int ret = 0;
558 
559 	if (!drm_enc || !crtc_state || !conn_state) {
560 		DPU_ERROR("invalid arg(s), drm_enc %d, crtc/conn state %d/%d\n",
561 				drm_enc != NULL, crtc_state != NULL, conn_state != NULL);
562 		return -EINVAL;
563 	}
564 
565 	dpu_enc = to_dpu_encoder_virt(drm_enc);
566 	DPU_DEBUG_ENC(dpu_enc, "\n");
567 
568 	priv = drm_enc->dev->dev_private;
569 	dpu_kms = to_dpu_kms(priv->kms);
570 	mode = &crtc_state->mode;
571 	adj_mode = &crtc_state->adjusted_mode;
572 	global_state = dpu_kms_get_existing_global_state(dpu_kms);
573 	trace_dpu_enc_atomic_check(DRMID(drm_enc));
574 
575 	/* perform atomic check on the first physical encoder (master) */
576 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
577 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
578 
579 		if (phys->ops.atomic_check)
580 			ret = phys->ops.atomic_check(phys, crtc_state,
581 					conn_state);
582 		else if (phys->ops.mode_fixup)
583 			if (!phys->ops.mode_fixup(phys, mode, adj_mode))
584 				ret = -EINVAL;
585 
586 		if (ret) {
587 			DPU_ERROR_ENC(dpu_enc,
588 					"mode unsupported, phys idx %d\n", i);
589 			break;
590 		}
591 	}
592 
593 	topology = dpu_encoder_get_topology(dpu_enc, dpu_kms, adj_mode);
594 
595 	/* Reserve dynamic resources now. */
596 	if (!ret) {
597 		/*
598 		 * Avoid reserving resources when mode set is pending. Topology
599 		 * info may not be available to complete reservation.
600 		 */
601 		if (drm_atomic_crtc_needs_modeset(crtc_state)) {
602 			ret = dpu_rm_reserve(&dpu_kms->rm, global_state,
603 					drm_enc, crtc_state, topology);
604 		}
605 	}
606 
607 	trace_dpu_enc_atomic_check_flags(DRMID(drm_enc), adj_mode->flags);
608 
609 	return ret;
610 }
611 
612 static void _dpu_encoder_update_vsync_source(struct dpu_encoder_virt *dpu_enc,
613 			struct msm_display_info *disp_info)
614 {
615 	struct dpu_vsync_source_cfg vsync_cfg = { 0 };
616 	struct msm_drm_private *priv;
617 	struct dpu_kms *dpu_kms;
618 	struct dpu_hw_mdp *hw_mdptop;
619 	struct drm_encoder *drm_enc;
620 	int i;
621 
622 	if (!dpu_enc || !disp_info) {
623 		DPU_ERROR("invalid param dpu_enc:%d or disp_info:%d\n",
624 					dpu_enc != NULL, disp_info != NULL);
625 		return;
626 	} else if (dpu_enc->num_phys_encs > ARRAY_SIZE(dpu_enc->hw_pp)) {
627 		DPU_ERROR("invalid num phys enc %d/%d\n",
628 				dpu_enc->num_phys_encs,
629 				(int) ARRAY_SIZE(dpu_enc->hw_pp));
630 		return;
631 	}
632 
633 	drm_enc = &dpu_enc->base;
634 	/* this pointers are checked in virt_enable_helper */
635 	priv = drm_enc->dev->dev_private;
636 
637 	dpu_kms = to_dpu_kms(priv->kms);
638 	hw_mdptop = dpu_kms->hw_mdp;
639 	if (!hw_mdptop) {
640 		DPU_ERROR("invalid mdptop\n");
641 		return;
642 	}
643 
644 	if (hw_mdptop->ops.setup_vsync_source &&
645 			disp_info->capabilities & MSM_DISPLAY_CAP_CMD_MODE) {
646 		for (i = 0; i < dpu_enc->num_phys_encs; i++)
647 			vsync_cfg.ppnumber[i] = dpu_enc->hw_pp[i]->idx;
648 
649 		vsync_cfg.pp_count = dpu_enc->num_phys_encs;
650 		if (disp_info->is_te_using_watchdog_timer)
651 			vsync_cfg.vsync_source = DPU_VSYNC_SOURCE_WD_TIMER_0;
652 		else
653 			vsync_cfg.vsync_source = DPU_VSYNC0_SOURCE_GPIO;
654 
655 		hw_mdptop->ops.setup_vsync_source(hw_mdptop, &vsync_cfg);
656 	}
657 }
658 
659 static void _dpu_encoder_irq_control(struct drm_encoder *drm_enc, bool enable)
660 {
661 	struct dpu_encoder_virt *dpu_enc;
662 	int i;
663 
664 	if (!drm_enc) {
665 		DPU_ERROR("invalid encoder\n");
666 		return;
667 	}
668 
669 	dpu_enc = to_dpu_encoder_virt(drm_enc);
670 
671 	DPU_DEBUG_ENC(dpu_enc, "enable:%d\n", enable);
672 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
673 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
674 
675 		if (phys->ops.irq_control)
676 			phys->ops.irq_control(phys, enable);
677 	}
678 
679 }
680 
681 static void _dpu_encoder_resource_control_helper(struct drm_encoder *drm_enc,
682 		bool enable)
683 {
684 	struct msm_drm_private *priv;
685 	struct dpu_kms *dpu_kms;
686 	struct dpu_encoder_virt *dpu_enc;
687 
688 	dpu_enc = to_dpu_encoder_virt(drm_enc);
689 	priv = drm_enc->dev->dev_private;
690 	dpu_kms = to_dpu_kms(priv->kms);
691 
692 	trace_dpu_enc_rc_helper(DRMID(drm_enc), enable);
693 
694 	if (!dpu_enc->cur_master) {
695 		DPU_ERROR("encoder master not set\n");
696 		return;
697 	}
698 
699 	if (enable) {
700 		/* enable DPU core clks */
701 		pm_runtime_get_sync(&dpu_kms->pdev->dev);
702 
703 		/* enable all the irq */
704 		_dpu_encoder_irq_control(drm_enc, true);
705 
706 	} else {
707 		/* disable all the irq */
708 		_dpu_encoder_irq_control(drm_enc, false);
709 
710 		/* disable DPU core clks */
711 		pm_runtime_put_sync(&dpu_kms->pdev->dev);
712 	}
713 
714 }
715 
716 static int dpu_encoder_resource_control(struct drm_encoder *drm_enc,
717 		u32 sw_event)
718 {
719 	struct dpu_encoder_virt *dpu_enc;
720 	struct msm_drm_private *priv;
721 	bool is_vid_mode = false;
722 
723 	if (!drm_enc || !drm_enc->dev || !drm_enc->crtc) {
724 		DPU_ERROR("invalid parameters\n");
725 		return -EINVAL;
726 	}
727 	dpu_enc = to_dpu_encoder_virt(drm_enc);
728 	priv = drm_enc->dev->dev_private;
729 	is_vid_mode = dpu_enc->disp_info.capabilities &
730 						MSM_DISPLAY_CAP_VID_MODE;
731 
732 	/*
733 	 * when idle_pc is not supported, process only KICKOFF, STOP and MODESET
734 	 * events and return early for other events (ie wb display).
735 	 */
736 	if (!dpu_enc->idle_pc_supported &&
737 			(sw_event != DPU_ENC_RC_EVENT_KICKOFF &&
738 			sw_event != DPU_ENC_RC_EVENT_STOP &&
739 			sw_event != DPU_ENC_RC_EVENT_PRE_STOP))
740 		return 0;
741 
742 	trace_dpu_enc_rc(DRMID(drm_enc), sw_event, dpu_enc->idle_pc_supported,
743 			 dpu_enc->rc_state, "begin");
744 
745 	switch (sw_event) {
746 	case DPU_ENC_RC_EVENT_KICKOFF:
747 		/* cancel delayed off work, if any */
748 		if (cancel_delayed_work_sync(&dpu_enc->delayed_off_work))
749 			DPU_DEBUG_ENC(dpu_enc, "sw_event:%d, work cancelled\n",
750 					sw_event);
751 
752 		mutex_lock(&dpu_enc->rc_lock);
753 
754 		/* return if the resource control is already in ON state */
755 		if (dpu_enc->rc_state == DPU_ENC_RC_STATE_ON) {
756 			DRM_DEBUG_KMS("id;%u, sw_event:%d, rc in ON state\n",
757 				      DRMID(drm_enc), sw_event);
758 			mutex_unlock(&dpu_enc->rc_lock);
759 			return 0;
760 		} else if (dpu_enc->rc_state != DPU_ENC_RC_STATE_OFF &&
761 				dpu_enc->rc_state != DPU_ENC_RC_STATE_IDLE) {
762 			DRM_DEBUG_KMS("id;%u, sw_event:%d, rc in state %d\n",
763 				      DRMID(drm_enc), sw_event,
764 				      dpu_enc->rc_state);
765 			mutex_unlock(&dpu_enc->rc_lock);
766 			return -EINVAL;
767 		}
768 
769 		if (is_vid_mode && dpu_enc->rc_state == DPU_ENC_RC_STATE_IDLE)
770 			_dpu_encoder_irq_control(drm_enc, true);
771 		else
772 			_dpu_encoder_resource_control_helper(drm_enc, true);
773 
774 		dpu_enc->rc_state = DPU_ENC_RC_STATE_ON;
775 
776 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
777 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
778 				 "kickoff");
779 
780 		mutex_unlock(&dpu_enc->rc_lock);
781 		break;
782 
783 	case DPU_ENC_RC_EVENT_FRAME_DONE:
784 		/*
785 		 * mutex lock is not used as this event happens at interrupt
786 		 * context. And locking is not required as, the other events
787 		 * like KICKOFF and STOP does a wait-for-idle before executing
788 		 * the resource_control
789 		 */
790 		if (dpu_enc->rc_state != DPU_ENC_RC_STATE_ON) {
791 			DRM_DEBUG_KMS("id:%d, sw_event:%d,rc:%d-unexpected\n",
792 				      DRMID(drm_enc), sw_event,
793 				      dpu_enc->rc_state);
794 			return -EINVAL;
795 		}
796 
797 		/*
798 		 * schedule off work item only when there are no
799 		 * frames pending
800 		 */
801 		if (dpu_crtc_frame_pending(drm_enc->crtc) > 1) {
802 			DRM_DEBUG_KMS("id:%d skip schedule work\n",
803 				      DRMID(drm_enc));
804 			return 0;
805 		}
806 
807 		queue_delayed_work(priv->wq, &dpu_enc->delayed_off_work,
808 				   msecs_to_jiffies(dpu_enc->idle_timeout));
809 
810 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
811 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
812 				 "frame done");
813 		break;
814 
815 	case DPU_ENC_RC_EVENT_PRE_STOP:
816 		/* cancel delayed off work, if any */
817 		if (cancel_delayed_work_sync(&dpu_enc->delayed_off_work))
818 			DPU_DEBUG_ENC(dpu_enc, "sw_event:%d, work cancelled\n",
819 					sw_event);
820 
821 		mutex_lock(&dpu_enc->rc_lock);
822 
823 		if (is_vid_mode &&
824 			  dpu_enc->rc_state == DPU_ENC_RC_STATE_IDLE) {
825 			_dpu_encoder_irq_control(drm_enc, true);
826 		}
827 		/* skip if is already OFF or IDLE, resources are off already */
828 		else if (dpu_enc->rc_state == DPU_ENC_RC_STATE_OFF ||
829 				dpu_enc->rc_state == DPU_ENC_RC_STATE_IDLE) {
830 			DRM_DEBUG_KMS("id:%u, sw_event:%d, rc in %d state\n",
831 				      DRMID(drm_enc), sw_event,
832 				      dpu_enc->rc_state);
833 			mutex_unlock(&dpu_enc->rc_lock);
834 			return 0;
835 		}
836 
837 		dpu_enc->rc_state = DPU_ENC_RC_STATE_PRE_OFF;
838 
839 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
840 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
841 				 "pre stop");
842 
843 		mutex_unlock(&dpu_enc->rc_lock);
844 		break;
845 
846 	case DPU_ENC_RC_EVENT_STOP:
847 		mutex_lock(&dpu_enc->rc_lock);
848 
849 		/* return if the resource control is already in OFF state */
850 		if (dpu_enc->rc_state == DPU_ENC_RC_STATE_OFF) {
851 			DRM_DEBUG_KMS("id: %u, sw_event:%d, rc in OFF state\n",
852 				      DRMID(drm_enc), sw_event);
853 			mutex_unlock(&dpu_enc->rc_lock);
854 			return 0;
855 		} else if (dpu_enc->rc_state == DPU_ENC_RC_STATE_ON) {
856 			DRM_ERROR("id: %u, sw_event:%d, rc in state %d\n",
857 				  DRMID(drm_enc), sw_event, dpu_enc->rc_state);
858 			mutex_unlock(&dpu_enc->rc_lock);
859 			return -EINVAL;
860 		}
861 
862 		/**
863 		 * expect to arrive here only if in either idle state or pre-off
864 		 * and in IDLE state the resources are already disabled
865 		 */
866 		if (dpu_enc->rc_state == DPU_ENC_RC_STATE_PRE_OFF)
867 			_dpu_encoder_resource_control_helper(drm_enc, false);
868 
869 		dpu_enc->rc_state = DPU_ENC_RC_STATE_OFF;
870 
871 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
872 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
873 				 "stop");
874 
875 		mutex_unlock(&dpu_enc->rc_lock);
876 		break;
877 
878 	case DPU_ENC_RC_EVENT_ENTER_IDLE:
879 		mutex_lock(&dpu_enc->rc_lock);
880 
881 		if (dpu_enc->rc_state != DPU_ENC_RC_STATE_ON) {
882 			DRM_ERROR("id: %u, sw_event:%d, rc:%d !ON state\n",
883 				  DRMID(drm_enc), sw_event, dpu_enc->rc_state);
884 			mutex_unlock(&dpu_enc->rc_lock);
885 			return 0;
886 		}
887 
888 		/*
889 		 * if we are in ON but a frame was just kicked off,
890 		 * ignore the IDLE event, it's probably a stale timer event
891 		 */
892 		if (dpu_enc->frame_busy_mask[0]) {
893 			DRM_ERROR("id:%u, sw_event:%d, rc:%d frame pending\n",
894 				  DRMID(drm_enc), sw_event, dpu_enc->rc_state);
895 			mutex_unlock(&dpu_enc->rc_lock);
896 			return 0;
897 		}
898 
899 		if (is_vid_mode)
900 			_dpu_encoder_irq_control(drm_enc, false);
901 		else
902 			_dpu_encoder_resource_control_helper(drm_enc, false);
903 
904 		dpu_enc->rc_state = DPU_ENC_RC_STATE_IDLE;
905 
906 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
907 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
908 				 "idle");
909 
910 		mutex_unlock(&dpu_enc->rc_lock);
911 		break;
912 
913 	default:
914 		DRM_ERROR("id:%u, unexpected sw_event: %d\n", DRMID(drm_enc),
915 			  sw_event);
916 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
917 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
918 				 "error");
919 		break;
920 	}
921 
922 	trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
923 			 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
924 			 "end");
925 	return 0;
926 }
927 
928 static void dpu_encoder_virt_mode_set(struct drm_encoder *drm_enc,
929 				      struct drm_display_mode *mode,
930 				      struct drm_display_mode *adj_mode)
931 {
932 	struct dpu_encoder_virt *dpu_enc;
933 	struct msm_drm_private *priv;
934 	struct dpu_kms *dpu_kms;
935 	struct list_head *connector_list;
936 	struct drm_connector *conn = NULL, *conn_iter;
937 	struct drm_crtc *drm_crtc;
938 	struct dpu_crtc_state *cstate;
939 	struct dpu_global_state *global_state;
940 	struct msm_display_topology topology;
941 	struct dpu_hw_blk *hw_pp[MAX_CHANNELS_PER_ENC];
942 	struct dpu_hw_blk *hw_ctl[MAX_CHANNELS_PER_ENC];
943 	struct dpu_hw_blk *hw_lm[MAX_CHANNELS_PER_ENC];
944 	struct dpu_hw_blk *hw_dspp[MAX_CHANNELS_PER_ENC] = { NULL };
945 	int num_lm, num_ctl, num_pp, num_dspp;
946 	int i, j;
947 
948 	if (!drm_enc) {
949 		DPU_ERROR("invalid encoder\n");
950 		return;
951 	}
952 
953 	dpu_enc = to_dpu_encoder_virt(drm_enc);
954 	DPU_DEBUG_ENC(dpu_enc, "\n");
955 
956 	priv = drm_enc->dev->dev_private;
957 	dpu_kms = to_dpu_kms(priv->kms);
958 	connector_list = &dpu_kms->dev->mode_config.connector_list;
959 
960 	global_state = dpu_kms_get_existing_global_state(dpu_kms);
961 	if (IS_ERR_OR_NULL(global_state)) {
962 		DPU_ERROR("Failed to get global state");
963 		return;
964 	}
965 
966 	trace_dpu_enc_mode_set(DRMID(drm_enc));
967 
968 	list_for_each_entry(conn_iter, connector_list, head)
969 		if (conn_iter->encoder == drm_enc)
970 			conn = conn_iter;
971 
972 	if (!conn) {
973 		DPU_ERROR_ENC(dpu_enc, "failed to find attached connector\n");
974 		return;
975 	} else if (!conn->state) {
976 		DPU_ERROR_ENC(dpu_enc, "invalid connector state\n");
977 		return;
978 	}
979 
980 	drm_for_each_crtc(drm_crtc, drm_enc->dev)
981 		if (drm_crtc->state->encoder_mask & drm_encoder_mask(drm_enc))
982 			break;
983 
984 	topology = dpu_encoder_get_topology(dpu_enc, dpu_kms, adj_mode);
985 
986 	/* Query resource that have been reserved in atomic check step. */
987 	num_pp = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
988 		drm_enc->base.id, DPU_HW_BLK_PINGPONG, hw_pp,
989 		ARRAY_SIZE(hw_pp));
990 	num_ctl = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
991 		drm_enc->base.id, DPU_HW_BLK_CTL, hw_ctl, ARRAY_SIZE(hw_ctl));
992 	num_lm = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
993 		drm_enc->base.id, DPU_HW_BLK_LM, hw_lm, ARRAY_SIZE(hw_lm));
994 	num_dspp = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
995 		drm_enc->base.id, DPU_HW_BLK_DSPP, hw_dspp,
996 		ARRAY_SIZE(hw_dspp));
997 
998 	for (i = 0; i < MAX_CHANNELS_PER_ENC; i++)
999 		dpu_enc->hw_pp[i] = i < num_pp ? to_dpu_hw_pingpong(hw_pp[i])
1000 						: NULL;
1001 
1002 	cstate = to_dpu_crtc_state(drm_crtc->state);
1003 
1004 	for (i = 0; i < num_lm; i++) {
1005 		int ctl_idx = (i < num_ctl) ? i : (num_ctl-1);
1006 
1007 		cstate->mixers[i].hw_lm = to_dpu_hw_mixer(hw_lm[i]);
1008 		cstate->mixers[i].lm_ctl = to_dpu_hw_ctl(hw_ctl[ctl_idx]);
1009 		cstate->mixers[i].hw_dspp = to_dpu_hw_dspp(hw_dspp[i]);
1010 	}
1011 
1012 	cstate->num_mixers = num_lm;
1013 
1014 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1015 		int num_blk;
1016 		struct dpu_hw_blk *hw_blk[MAX_CHANNELS_PER_ENC];
1017 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1018 
1019 		if (!dpu_enc->hw_pp[i]) {
1020 			DPU_ERROR_ENC(dpu_enc,
1021 				"no pp block assigned at idx: %d\n", i);
1022 			return;
1023 		}
1024 
1025 		if (!hw_ctl[i]) {
1026 			DPU_ERROR_ENC(dpu_enc,
1027 				"no ctl block assigned at idx: %d\n", i);
1028 			return;
1029 		}
1030 
1031 		phys->hw_pp = dpu_enc->hw_pp[i];
1032 		phys->hw_ctl = to_dpu_hw_ctl(hw_ctl[i]);
1033 
1034 		num_blk = dpu_rm_get_assigned_resources(&dpu_kms->rm,
1035 			global_state, drm_enc->base.id, DPU_HW_BLK_INTF,
1036 			hw_blk, ARRAY_SIZE(hw_blk));
1037 		for (j = 0; j < num_blk; j++) {
1038 			struct dpu_hw_intf *hw_intf;
1039 
1040 			hw_intf = to_dpu_hw_intf(hw_blk[i]);
1041 			if (hw_intf->idx == phys->intf_idx)
1042 				phys->hw_intf = hw_intf;
1043 		}
1044 
1045 		if (!phys->hw_intf) {
1046 			DPU_ERROR_ENC(dpu_enc,
1047 				      "no intf block assigned at idx: %d\n", i);
1048 			return;
1049 		}
1050 
1051 		phys->connector = conn->state->connector;
1052 		if (phys->ops.mode_set)
1053 			phys->ops.mode_set(phys, mode, adj_mode);
1054 	}
1055 }
1056 
1057 static void _dpu_encoder_virt_enable_helper(struct drm_encoder *drm_enc)
1058 {
1059 	struct dpu_encoder_virt *dpu_enc = NULL;
1060 	struct msm_drm_private *priv;
1061 	struct dpu_kms *dpu_kms;
1062 
1063 	if (!drm_enc || !drm_enc->dev) {
1064 		DPU_ERROR("invalid parameters\n");
1065 		return;
1066 	}
1067 
1068 	priv = drm_enc->dev->dev_private;
1069 	dpu_kms = to_dpu_kms(priv->kms);
1070 
1071 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1072 	if (!dpu_enc || !dpu_enc->cur_master) {
1073 		DPU_ERROR("invalid dpu encoder/master\n");
1074 		return;
1075 	}
1076 
1077 	if (dpu_enc->cur_master->hw_mdptop &&
1078 			dpu_enc->cur_master->hw_mdptop->ops.reset_ubwc)
1079 		dpu_enc->cur_master->hw_mdptop->ops.reset_ubwc(
1080 				dpu_enc->cur_master->hw_mdptop,
1081 				dpu_kms->catalog);
1082 
1083 	_dpu_encoder_update_vsync_source(dpu_enc, &dpu_enc->disp_info);
1084 }
1085 
1086 void dpu_encoder_virt_runtime_resume(struct drm_encoder *drm_enc)
1087 {
1088 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1089 
1090 	mutex_lock(&dpu_enc->enc_lock);
1091 
1092 	if (!dpu_enc->enabled)
1093 		goto out;
1094 
1095 	if (dpu_enc->cur_slave && dpu_enc->cur_slave->ops.restore)
1096 		dpu_enc->cur_slave->ops.restore(dpu_enc->cur_slave);
1097 	if (dpu_enc->cur_master && dpu_enc->cur_master->ops.restore)
1098 		dpu_enc->cur_master->ops.restore(dpu_enc->cur_master);
1099 
1100 	_dpu_encoder_virt_enable_helper(drm_enc);
1101 
1102 out:
1103 	mutex_unlock(&dpu_enc->enc_lock);
1104 }
1105 
1106 static void dpu_encoder_virt_enable(struct drm_encoder *drm_enc)
1107 {
1108 	struct dpu_encoder_virt *dpu_enc = NULL;
1109 	int ret = 0;
1110 	struct drm_display_mode *cur_mode = NULL;
1111 
1112 	if (!drm_enc) {
1113 		DPU_ERROR("invalid encoder\n");
1114 		return;
1115 	}
1116 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1117 
1118 	mutex_lock(&dpu_enc->enc_lock);
1119 	cur_mode = &dpu_enc->base.crtc->state->adjusted_mode;
1120 
1121 	trace_dpu_enc_enable(DRMID(drm_enc), cur_mode->hdisplay,
1122 			     cur_mode->vdisplay);
1123 
1124 	/* always enable slave encoder before master */
1125 	if (dpu_enc->cur_slave && dpu_enc->cur_slave->ops.enable)
1126 		dpu_enc->cur_slave->ops.enable(dpu_enc->cur_slave);
1127 
1128 	if (dpu_enc->cur_master && dpu_enc->cur_master->ops.enable)
1129 		dpu_enc->cur_master->ops.enable(dpu_enc->cur_master);
1130 
1131 	ret = dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_KICKOFF);
1132 	if (ret) {
1133 		DPU_ERROR_ENC(dpu_enc, "dpu resource control failed: %d\n",
1134 				ret);
1135 		goto out;
1136 	}
1137 
1138 	_dpu_encoder_virt_enable_helper(drm_enc);
1139 
1140 	dpu_enc->enabled = true;
1141 
1142 out:
1143 	mutex_unlock(&dpu_enc->enc_lock);
1144 }
1145 
1146 static void dpu_encoder_virt_disable(struct drm_encoder *drm_enc)
1147 {
1148 	struct dpu_encoder_virt *dpu_enc = NULL;
1149 	struct msm_drm_private *priv;
1150 	struct dpu_kms *dpu_kms;
1151 	struct dpu_global_state *global_state;
1152 	int i = 0;
1153 
1154 	if (!drm_enc) {
1155 		DPU_ERROR("invalid encoder\n");
1156 		return;
1157 	} else if (!drm_enc->dev) {
1158 		DPU_ERROR("invalid dev\n");
1159 		return;
1160 	}
1161 
1162 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1163 	DPU_DEBUG_ENC(dpu_enc, "\n");
1164 
1165 	mutex_lock(&dpu_enc->enc_lock);
1166 	dpu_enc->enabled = false;
1167 
1168 	priv = drm_enc->dev->dev_private;
1169 	dpu_kms = to_dpu_kms(priv->kms);
1170 	global_state = dpu_kms_get_existing_global_state(dpu_kms);
1171 
1172 	trace_dpu_enc_disable(DRMID(drm_enc));
1173 
1174 	/* wait for idle */
1175 	dpu_encoder_wait_for_event(drm_enc, MSM_ENC_TX_COMPLETE);
1176 
1177 	dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_PRE_STOP);
1178 
1179 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1180 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1181 
1182 		if (phys->ops.disable)
1183 			phys->ops.disable(phys);
1184 	}
1185 
1186 	/* after phys waits for frame-done, should be no more frames pending */
1187 	if (atomic_xchg(&dpu_enc->frame_done_timeout_ms, 0)) {
1188 		DPU_ERROR("enc%d timeout pending\n", drm_enc->base.id);
1189 		del_timer_sync(&dpu_enc->frame_done_timer);
1190 	}
1191 
1192 	dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_STOP);
1193 
1194 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1195 		dpu_enc->phys_encs[i]->connector = NULL;
1196 	}
1197 
1198 	DPU_DEBUG_ENC(dpu_enc, "encoder disabled\n");
1199 
1200 	dpu_rm_release(global_state, drm_enc);
1201 
1202 	mutex_unlock(&dpu_enc->enc_lock);
1203 }
1204 
1205 static enum dpu_intf dpu_encoder_get_intf(struct dpu_mdss_cfg *catalog,
1206 		enum dpu_intf_type type, u32 controller_id)
1207 {
1208 	int i = 0;
1209 
1210 	for (i = 0; i < catalog->intf_count; i++) {
1211 		if (catalog->intf[i].type == type
1212 		    && catalog->intf[i].controller_id == controller_id) {
1213 			return catalog->intf[i].id;
1214 		}
1215 	}
1216 
1217 	return INTF_MAX;
1218 }
1219 
1220 static void dpu_encoder_vblank_callback(struct drm_encoder *drm_enc,
1221 		struct dpu_encoder_phys *phy_enc)
1222 {
1223 	struct dpu_encoder_virt *dpu_enc = NULL;
1224 	unsigned long lock_flags;
1225 
1226 	if (!drm_enc || !phy_enc)
1227 		return;
1228 
1229 	DPU_ATRACE_BEGIN("encoder_vblank_callback");
1230 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1231 
1232 	spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1233 	if (dpu_enc->crtc)
1234 		dpu_crtc_vblank_callback(dpu_enc->crtc);
1235 	spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1236 
1237 	atomic_inc(&phy_enc->vsync_cnt);
1238 	DPU_ATRACE_END("encoder_vblank_callback");
1239 }
1240 
1241 static void dpu_encoder_underrun_callback(struct drm_encoder *drm_enc,
1242 		struct dpu_encoder_phys *phy_enc)
1243 {
1244 	if (!phy_enc)
1245 		return;
1246 
1247 	DPU_ATRACE_BEGIN("encoder_underrun_callback");
1248 	atomic_inc(&phy_enc->underrun_cnt);
1249 	trace_dpu_enc_underrun_cb(DRMID(drm_enc),
1250 				  atomic_read(&phy_enc->underrun_cnt));
1251 	DPU_ATRACE_END("encoder_underrun_callback");
1252 }
1253 
1254 void dpu_encoder_assign_crtc(struct drm_encoder *drm_enc, struct drm_crtc *crtc)
1255 {
1256 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1257 	unsigned long lock_flags;
1258 
1259 	spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1260 	/* crtc should always be cleared before re-assigning */
1261 	WARN_ON(crtc && dpu_enc->crtc);
1262 	dpu_enc->crtc = crtc;
1263 	spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1264 }
1265 
1266 void dpu_encoder_toggle_vblank_for_crtc(struct drm_encoder *drm_enc,
1267 					struct drm_crtc *crtc, bool enable)
1268 {
1269 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1270 	unsigned long lock_flags;
1271 	int i;
1272 
1273 	trace_dpu_enc_vblank_cb(DRMID(drm_enc), enable);
1274 
1275 	spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1276 	if (dpu_enc->crtc != crtc) {
1277 		spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1278 		return;
1279 	}
1280 	spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1281 
1282 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1283 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1284 
1285 		if (phys->ops.control_vblank_irq)
1286 			phys->ops.control_vblank_irq(phys, enable);
1287 	}
1288 }
1289 
1290 void dpu_encoder_register_frame_event_callback(struct drm_encoder *drm_enc,
1291 		void (*frame_event_cb)(void *, u32 event),
1292 		void *frame_event_cb_data)
1293 {
1294 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1295 	unsigned long lock_flags;
1296 	bool enable;
1297 
1298 	enable = frame_event_cb ? true : false;
1299 
1300 	if (!drm_enc) {
1301 		DPU_ERROR("invalid encoder\n");
1302 		return;
1303 	}
1304 	trace_dpu_enc_frame_event_cb(DRMID(drm_enc), enable);
1305 
1306 	spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1307 	dpu_enc->crtc_frame_event_cb = frame_event_cb;
1308 	dpu_enc->crtc_frame_event_cb_data = frame_event_cb_data;
1309 	spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1310 }
1311 
1312 static void dpu_encoder_frame_done_callback(
1313 		struct drm_encoder *drm_enc,
1314 		struct dpu_encoder_phys *ready_phys, u32 event)
1315 {
1316 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1317 	unsigned int i;
1318 
1319 	if (event & (DPU_ENCODER_FRAME_EVENT_DONE
1320 			| DPU_ENCODER_FRAME_EVENT_ERROR
1321 			| DPU_ENCODER_FRAME_EVENT_PANEL_DEAD)) {
1322 
1323 		if (!dpu_enc->frame_busy_mask[0]) {
1324 			/**
1325 			 * suppress frame_done without waiter,
1326 			 * likely autorefresh
1327 			 */
1328 			trace_dpu_enc_frame_done_cb_not_busy(DRMID(drm_enc),
1329 					event, ready_phys->intf_idx);
1330 			return;
1331 		}
1332 
1333 		/* One of the physical encoders has become idle */
1334 		for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1335 			if (dpu_enc->phys_encs[i] == ready_phys) {
1336 				trace_dpu_enc_frame_done_cb(DRMID(drm_enc), i,
1337 						dpu_enc->frame_busy_mask[0]);
1338 				clear_bit(i, dpu_enc->frame_busy_mask);
1339 			}
1340 		}
1341 
1342 		if (!dpu_enc->frame_busy_mask[0]) {
1343 			atomic_set(&dpu_enc->frame_done_timeout_ms, 0);
1344 			del_timer(&dpu_enc->frame_done_timer);
1345 
1346 			dpu_encoder_resource_control(drm_enc,
1347 					DPU_ENC_RC_EVENT_FRAME_DONE);
1348 
1349 			if (dpu_enc->crtc_frame_event_cb)
1350 				dpu_enc->crtc_frame_event_cb(
1351 					dpu_enc->crtc_frame_event_cb_data,
1352 					event);
1353 		}
1354 	} else {
1355 		if (dpu_enc->crtc_frame_event_cb)
1356 			dpu_enc->crtc_frame_event_cb(
1357 				dpu_enc->crtc_frame_event_cb_data, event);
1358 	}
1359 }
1360 
1361 static void dpu_encoder_off_work(struct work_struct *work)
1362 {
1363 	struct dpu_encoder_virt *dpu_enc = container_of(work,
1364 			struct dpu_encoder_virt, delayed_off_work.work);
1365 
1366 	if (!dpu_enc) {
1367 		DPU_ERROR("invalid dpu encoder\n");
1368 		return;
1369 	}
1370 
1371 	dpu_encoder_resource_control(&dpu_enc->base,
1372 						DPU_ENC_RC_EVENT_ENTER_IDLE);
1373 
1374 	dpu_encoder_frame_done_callback(&dpu_enc->base, NULL,
1375 				DPU_ENCODER_FRAME_EVENT_IDLE);
1376 }
1377 
1378 /**
1379  * _dpu_encoder_trigger_flush - trigger flush for a physical encoder
1380  * drm_enc: Pointer to drm encoder structure
1381  * phys: Pointer to physical encoder structure
1382  * extra_flush_bits: Additional bit mask to include in flush trigger
1383  */
1384 static void _dpu_encoder_trigger_flush(struct drm_encoder *drm_enc,
1385 		struct dpu_encoder_phys *phys, uint32_t extra_flush_bits)
1386 {
1387 	struct dpu_hw_ctl *ctl;
1388 	int pending_kickoff_cnt;
1389 	u32 ret = UINT_MAX;
1390 
1391 	if (!phys->hw_pp) {
1392 		DPU_ERROR("invalid pingpong hw\n");
1393 		return;
1394 	}
1395 
1396 	ctl = phys->hw_ctl;
1397 	if (!ctl->ops.trigger_flush) {
1398 		DPU_ERROR("missing trigger cb\n");
1399 		return;
1400 	}
1401 
1402 	pending_kickoff_cnt = dpu_encoder_phys_inc_pending(phys);
1403 
1404 	if (extra_flush_bits && ctl->ops.update_pending_flush)
1405 		ctl->ops.update_pending_flush(ctl, extra_flush_bits);
1406 
1407 	ctl->ops.trigger_flush(ctl);
1408 
1409 	if (ctl->ops.get_pending_flush)
1410 		ret = ctl->ops.get_pending_flush(ctl);
1411 
1412 	trace_dpu_enc_trigger_flush(DRMID(drm_enc), phys->intf_idx,
1413 				    pending_kickoff_cnt, ctl->idx,
1414 				    extra_flush_bits, ret);
1415 }
1416 
1417 /**
1418  * _dpu_encoder_trigger_start - trigger start for a physical encoder
1419  * phys: Pointer to physical encoder structure
1420  */
1421 static void _dpu_encoder_trigger_start(struct dpu_encoder_phys *phys)
1422 {
1423 	if (!phys) {
1424 		DPU_ERROR("invalid argument(s)\n");
1425 		return;
1426 	}
1427 
1428 	if (!phys->hw_pp) {
1429 		DPU_ERROR("invalid pingpong hw\n");
1430 		return;
1431 	}
1432 
1433 	if (phys->ops.trigger_start && phys->enable_state != DPU_ENC_DISABLED)
1434 		phys->ops.trigger_start(phys);
1435 }
1436 
1437 void dpu_encoder_helper_trigger_start(struct dpu_encoder_phys *phys_enc)
1438 {
1439 	struct dpu_hw_ctl *ctl;
1440 
1441 	ctl = phys_enc->hw_ctl;
1442 	if (ctl->ops.trigger_start) {
1443 		ctl->ops.trigger_start(ctl);
1444 		trace_dpu_enc_trigger_start(DRMID(phys_enc->parent), ctl->idx);
1445 	}
1446 }
1447 
1448 static int dpu_encoder_helper_wait_event_timeout(
1449 		int32_t drm_id,
1450 		int32_t hw_id,
1451 		struct dpu_encoder_wait_info *info)
1452 {
1453 	int rc = 0;
1454 	s64 expected_time = ktime_to_ms(ktime_get()) + info->timeout_ms;
1455 	s64 jiffies = msecs_to_jiffies(info->timeout_ms);
1456 	s64 time;
1457 
1458 	do {
1459 		rc = wait_event_timeout(*(info->wq),
1460 				atomic_read(info->atomic_cnt) == 0, jiffies);
1461 		time = ktime_to_ms(ktime_get());
1462 
1463 		trace_dpu_enc_wait_event_timeout(drm_id, hw_id, rc, time,
1464 						 expected_time,
1465 						 atomic_read(info->atomic_cnt));
1466 	/* If we timed out, counter is valid and time is less, wait again */
1467 	} while (atomic_read(info->atomic_cnt) && (rc == 0) &&
1468 			(time < expected_time));
1469 
1470 	return rc;
1471 }
1472 
1473 static void dpu_encoder_helper_hw_reset(struct dpu_encoder_phys *phys_enc)
1474 {
1475 	struct dpu_encoder_virt *dpu_enc;
1476 	struct dpu_hw_ctl *ctl;
1477 	int rc;
1478 
1479 	dpu_enc = to_dpu_encoder_virt(phys_enc->parent);
1480 	ctl = phys_enc->hw_ctl;
1481 
1482 	if (!ctl->ops.reset)
1483 		return;
1484 
1485 	DRM_DEBUG_KMS("id:%u ctl %d reset\n", DRMID(phys_enc->parent),
1486 		      ctl->idx);
1487 
1488 	rc = ctl->ops.reset(ctl);
1489 	if (rc)
1490 		DPU_ERROR_ENC(dpu_enc, "ctl %d reset failure\n",  ctl->idx);
1491 
1492 	phys_enc->enable_state = DPU_ENC_ENABLED;
1493 }
1494 
1495 /**
1496  * _dpu_encoder_kickoff_phys - handle physical encoder kickoff
1497  *	Iterate through the physical encoders and perform consolidated flush
1498  *	and/or control start triggering as needed. This is done in the virtual
1499  *	encoder rather than the individual physical ones in order to handle
1500  *	use cases that require visibility into multiple physical encoders at
1501  *	a time.
1502  * dpu_enc: Pointer to virtual encoder structure
1503  */
1504 static void _dpu_encoder_kickoff_phys(struct dpu_encoder_virt *dpu_enc)
1505 {
1506 	struct dpu_hw_ctl *ctl;
1507 	uint32_t i, pending_flush;
1508 	unsigned long lock_flags;
1509 
1510 	pending_flush = 0x0;
1511 
1512 	/* update pending counts and trigger kickoff ctl flush atomically */
1513 	spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1514 
1515 	/* don't perform flush/start operations for slave encoders */
1516 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1517 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1518 
1519 		if (phys->enable_state == DPU_ENC_DISABLED)
1520 			continue;
1521 
1522 		ctl = phys->hw_ctl;
1523 
1524 		/*
1525 		 * This is cleared in frame_done worker, which isn't invoked
1526 		 * for async commits. So don't set this for async, since it'll
1527 		 * roll over to the next commit.
1528 		 */
1529 		if (phys->split_role != ENC_ROLE_SLAVE)
1530 			set_bit(i, dpu_enc->frame_busy_mask);
1531 
1532 		if (!phys->ops.needs_single_flush ||
1533 				!phys->ops.needs_single_flush(phys))
1534 			_dpu_encoder_trigger_flush(&dpu_enc->base, phys, 0x0);
1535 		else if (ctl->ops.get_pending_flush)
1536 			pending_flush |= ctl->ops.get_pending_flush(ctl);
1537 	}
1538 
1539 	/* for split flush, combine pending flush masks and send to master */
1540 	if (pending_flush && dpu_enc->cur_master) {
1541 		_dpu_encoder_trigger_flush(
1542 				&dpu_enc->base,
1543 				dpu_enc->cur_master,
1544 				pending_flush);
1545 	}
1546 
1547 	_dpu_encoder_trigger_start(dpu_enc->cur_master);
1548 
1549 	spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1550 }
1551 
1552 void dpu_encoder_trigger_kickoff_pending(struct drm_encoder *drm_enc)
1553 {
1554 	struct dpu_encoder_virt *dpu_enc;
1555 	struct dpu_encoder_phys *phys;
1556 	unsigned int i;
1557 	struct dpu_hw_ctl *ctl;
1558 	struct msm_display_info *disp_info;
1559 
1560 	if (!drm_enc) {
1561 		DPU_ERROR("invalid encoder\n");
1562 		return;
1563 	}
1564 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1565 	disp_info = &dpu_enc->disp_info;
1566 
1567 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1568 		phys = dpu_enc->phys_encs[i];
1569 
1570 		ctl = phys->hw_ctl;
1571 		if (ctl->ops.clear_pending_flush)
1572 			ctl->ops.clear_pending_flush(ctl);
1573 
1574 		/* update only for command mode primary ctl */
1575 		if ((phys == dpu_enc->cur_master) &&
1576 		   (disp_info->capabilities & MSM_DISPLAY_CAP_CMD_MODE)
1577 		    && ctl->ops.trigger_pending)
1578 			ctl->ops.trigger_pending(ctl);
1579 	}
1580 }
1581 
1582 static u32 _dpu_encoder_calculate_linetime(struct dpu_encoder_virt *dpu_enc,
1583 		struct drm_display_mode *mode)
1584 {
1585 	u64 pclk_rate;
1586 	u32 pclk_period;
1587 	u32 line_time;
1588 
1589 	/*
1590 	 * For linetime calculation, only operate on master encoder.
1591 	 */
1592 	if (!dpu_enc->cur_master)
1593 		return 0;
1594 
1595 	if (!dpu_enc->cur_master->ops.get_line_count) {
1596 		DPU_ERROR("get_line_count function not defined\n");
1597 		return 0;
1598 	}
1599 
1600 	pclk_rate = mode->clock; /* pixel clock in kHz */
1601 	if (pclk_rate == 0) {
1602 		DPU_ERROR("pclk is 0, cannot calculate line time\n");
1603 		return 0;
1604 	}
1605 
1606 	pclk_period = DIV_ROUND_UP_ULL(1000000000ull, pclk_rate);
1607 	if (pclk_period == 0) {
1608 		DPU_ERROR("pclk period is 0\n");
1609 		return 0;
1610 	}
1611 
1612 	/*
1613 	 * Line time calculation based on Pixel clock and HTOTAL.
1614 	 * Final unit is in ns.
1615 	 */
1616 	line_time = (pclk_period * mode->htotal) / 1000;
1617 	if (line_time == 0) {
1618 		DPU_ERROR("line time calculation is 0\n");
1619 		return 0;
1620 	}
1621 
1622 	DPU_DEBUG_ENC(dpu_enc,
1623 			"clk_rate=%lldkHz, clk_period=%d, linetime=%dns\n",
1624 			pclk_rate, pclk_period, line_time);
1625 
1626 	return line_time;
1627 }
1628 
1629 int dpu_encoder_vsync_time(struct drm_encoder *drm_enc, ktime_t *wakeup_time)
1630 {
1631 	struct drm_display_mode *mode;
1632 	struct dpu_encoder_virt *dpu_enc;
1633 	u32 cur_line;
1634 	u32 line_time;
1635 	u32 vtotal, time_to_vsync;
1636 	ktime_t cur_time;
1637 
1638 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1639 
1640 	if (!drm_enc->crtc || !drm_enc->crtc->state) {
1641 		DPU_ERROR("crtc/crtc state object is NULL\n");
1642 		return -EINVAL;
1643 	}
1644 	mode = &drm_enc->crtc->state->adjusted_mode;
1645 
1646 	line_time = _dpu_encoder_calculate_linetime(dpu_enc, mode);
1647 	if (!line_time)
1648 		return -EINVAL;
1649 
1650 	cur_line = dpu_enc->cur_master->ops.get_line_count(dpu_enc->cur_master);
1651 
1652 	vtotal = mode->vtotal;
1653 	if (cur_line >= vtotal)
1654 		time_to_vsync = line_time * vtotal;
1655 	else
1656 		time_to_vsync = line_time * (vtotal - cur_line);
1657 
1658 	if (time_to_vsync == 0) {
1659 		DPU_ERROR("time to vsync should not be zero, vtotal=%d\n",
1660 				vtotal);
1661 		return -EINVAL;
1662 	}
1663 
1664 	cur_time = ktime_get();
1665 	*wakeup_time = ktime_add_ns(cur_time, time_to_vsync);
1666 
1667 	DPU_DEBUG_ENC(dpu_enc,
1668 			"cur_line=%u vtotal=%u time_to_vsync=%u, cur_time=%lld, wakeup_time=%lld\n",
1669 			cur_line, vtotal, time_to_vsync,
1670 			ktime_to_ms(cur_time),
1671 			ktime_to_ms(*wakeup_time));
1672 	return 0;
1673 }
1674 
1675 static void dpu_encoder_vsync_event_handler(struct timer_list *t)
1676 {
1677 	struct dpu_encoder_virt *dpu_enc = from_timer(dpu_enc, t,
1678 			vsync_event_timer);
1679 	struct drm_encoder *drm_enc = &dpu_enc->base;
1680 	struct msm_drm_private *priv;
1681 	struct msm_drm_thread *event_thread;
1682 
1683 	if (!drm_enc->dev || !drm_enc->crtc) {
1684 		DPU_ERROR("invalid parameters\n");
1685 		return;
1686 	}
1687 
1688 	priv = drm_enc->dev->dev_private;
1689 
1690 	if (drm_enc->crtc->index >= ARRAY_SIZE(priv->event_thread)) {
1691 		DPU_ERROR("invalid crtc index\n");
1692 		return;
1693 	}
1694 	event_thread = &priv->event_thread[drm_enc->crtc->index];
1695 	if (!event_thread) {
1696 		DPU_ERROR("event_thread not found for crtc:%d\n",
1697 				drm_enc->crtc->index);
1698 		return;
1699 	}
1700 
1701 	del_timer(&dpu_enc->vsync_event_timer);
1702 }
1703 
1704 static void dpu_encoder_vsync_event_work_handler(struct kthread_work *work)
1705 {
1706 	struct dpu_encoder_virt *dpu_enc = container_of(work,
1707 			struct dpu_encoder_virt, vsync_event_work);
1708 	ktime_t wakeup_time;
1709 
1710 	if (!dpu_enc) {
1711 		DPU_ERROR("invalid dpu encoder\n");
1712 		return;
1713 	}
1714 
1715 	if (dpu_encoder_vsync_time(&dpu_enc->base, &wakeup_time))
1716 		return;
1717 
1718 	trace_dpu_enc_vsync_event_work(DRMID(&dpu_enc->base), wakeup_time);
1719 	mod_timer(&dpu_enc->vsync_event_timer,
1720 			nsecs_to_jiffies(ktime_to_ns(wakeup_time)));
1721 }
1722 
1723 void dpu_encoder_prepare_for_kickoff(struct drm_encoder *drm_enc)
1724 {
1725 	struct dpu_encoder_virt *dpu_enc;
1726 	struct dpu_encoder_phys *phys;
1727 	bool needs_hw_reset = false;
1728 	unsigned int i;
1729 
1730 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1731 
1732 	trace_dpu_enc_prepare_kickoff(DRMID(drm_enc));
1733 
1734 	/* prepare for next kickoff, may include waiting on previous kickoff */
1735 	DPU_ATRACE_BEGIN("enc_prepare_for_kickoff");
1736 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1737 		phys = dpu_enc->phys_encs[i];
1738 		if (phys->ops.prepare_for_kickoff)
1739 			phys->ops.prepare_for_kickoff(phys);
1740 		if (phys->enable_state == DPU_ENC_ERR_NEEDS_HW_RESET)
1741 			needs_hw_reset = true;
1742 	}
1743 	DPU_ATRACE_END("enc_prepare_for_kickoff");
1744 
1745 	dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_KICKOFF);
1746 
1747 	/* if any phys needs reset, reset all phys, in-order */
1748 	if (needs_hw_reset) {
1749 		trace_dpu_enc_prepare_kickoff_reset(DRMID(drm_enc));
1750 		for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1751 			dpu_encoder_helper_hw_reset(dpu_enc->phys_encs[i]);
1752 		}
1753 	}
1754 }
1755 
1756 void dpu_encoder_kickoff(struct drm_encoder *drm_enc)
1757 {
1758 	struct dpu_encoder_virt *dpu_enc;
1759 	struct dpu_encoder_phys *phys;
1760 	ktime_t wakeup_time;
1761 	unsigned long timeout_ms;
1762 	unsigned int i;
1763 
1764 	DPU_ATRACE_BEGIN("encoder_kickoff");
1765 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1766 
1767 	trace_dpu_enc_kickoff(DRMID(drm_enc));
1768 
1769 	timeout_ms = DPU_ENCODER_FRAME_DONE_TIMEOUT_FRAMES * 1000 /
1770 			drm_mode_vrefresh(&drm_enc->crtc->state->adjusted_mode);
1771 
1772 	atomic_set(&dpu_enc->frame_done_timeout_ms, timeout_ms);
1773 	mod_timer(&dpu_enc->frame_done_timer,
1774 			jiffies + msecs_to_jiffies(timeout_ms));
1775 
1776 	/* All phys encs are ready to go, trigger the kickoff */
1777 	_dpu_encoder_kickoff_phys(dpu_enc);
1778 
1779 	/* allow phys encs to handle any post-kickoff business */
1780 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1781 		phys = dpu_enc->phys_encs[i];
1782 		if (phys->ops.handle_post_kickoff)
1783 			phys->ops.handle_post_kickoff(phys);
1784 	}
1785 
1786 	if (dpu_enc->disp_info.intf_type == DRM_MODE_ENCODER_DSI &&
1787 			!dpu_encoder_vsync_time(drm_enc, &wakeup_time)) {
1788 		trace_dpu_enc_early_kickoff(DRMID(drm_enc),
1789 					    ktime_to_ms(wakeup_time));
1790 		mod_timer(&dpu_enc->vsync_event_timer,
1791 				nsecs_to_jiffies(ktime_to_ns(wakeup_time)));
1792 	}
1793 
1794 	DPU_ATRACE_END("encoder_kickoff");
1795 }
1796 
1797 void dpu_encoder_prepare_commit(struct drm_encoder *drm_enc)
1798 {
1799 	struct dpu_encoder_virt *dpu_enc;
1800 	struct dpu_encoder_phys *phys;
1801 	int i;
1802 
1803 	if (!drm_enc) {
1804 		DPU_ERROR("invalid encoder\n");
1805 		return;
1806 	}
1807 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1808 
1809 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1810 		phys = dpu_enc->phys_encs[i];
1811 		if (phys->ops.prepare_commit)
1812 			phys->ops.prepare_commit(phys);
1813 	}
1814 }
1815 
1816 #ifdef CONFIG_DEBUG_FS
1817 static int _dpu_encoder_status_show(struct seq_file *s, void *data)
1818 {
1819 	struct dpu_encoder_virt *dpu_enc = s->private;
1820 	int i;
1821 
1822 	mutex_lock(&dpu_enc->enc_lock);
1823 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1824 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1825 
1826 		seq_printf(s, "intf:%d    vsync:%8d     underrun:%8d    ",
1827 				phys->intf_idx - INTF_0,
1828 				atomic_read(&phys->vsync_cnt),
1829 				atomic_read(&phys->underrun_cnt));
1830 
1831 		switch (phys->intf_mode) {
1832 		case INTF_MODE_VIDEO:
1833 			seq_puts(s, "mode: video\n");
1834 			break;
1835 		case INTF_MODE_CMD:
1836 			seq_puts(s, "mode: command\n");
1837 			break;
1838 		default:
1839 			seq_puts(s, "mode: ???\n");
1840 			break;
1841 		}
1842 	}
1843 	mutex_unlock(&dpu_enc->enc_lock);
1844 
1845 	return 0;
1846 }
1847 
1848 static int _dpu_encoder_debugfs_status_open(struct inode *inode,
1849 		struct file *file)
1850 {
1851 	return single_open(file, _dpu_encoder_status_show, inode->i_private);
1852 }
1853 
1854 static int _dpu_encoder_init_debugfs(struct drm_encoder *drm_enc)
1855 {
1856 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1857 	int i;
1858 
1859 	static const struct file_operations debugfs_status_fops = {
1860 		.open =		_dpu_encoder_debugfs_status_open,
1861 		.read =		seq_read,
1862 		.llseek =	seq_lseek,
1863 		.release =	single_release,
1864 	};
1865 
1866 	char name[DPU_NAME_SIZE];
1867 
1868 	if (!drm_enc->dev) {
1869 		DPU_ERROR("invalid encoder or kms\n");
1870 		return -EINVAL;
1871 	}
1872 
1873 	snprintf(name, DPU_NAME_SIZE, "encoder%u", drm_enc->base.id);
1874 
1875 	/* create overall sub-directory for the encoder */
1876 	dpu_enc->debugfs_root = debugfs_create_dir(name,
1877 			drm_enc->dev->primary->debugfs_root);
1878 
1879 	/* don't error check these */
1880 	debugfs_create_file("status", 0600,
1881 		dpu_enc->debugfs_root, dpu_enc, &debugfs_status_fops);
1882 
1883 	for (i = 0; i < dpu_enc->num_phys_encs; i++)
1884 		if (dpu_enc->phys_encs[i]->ops.late_register)
1885 			dpu_enc->phys_encs[i]->ops.late_register(
1886 					dpu_enc->phys_encs[i],
1887 					dpu_enc->debugfs_root);
1888 
1889 	return 0;
1890 }
1891 #else
1892 static int _dpu_encoder_init_debugfs(struct drm_encoder *drm_enc)
1893 {
1894 	return 0;
1895 }
1896 #endif
1897 
1898 static int dpu_encoder_late_register(struct drm_encoder *encoder)
1899 {
1900 	return _dpu_encoder_init_debugfs(encoder);
1901 }
1902 
1903 static void dpu_encoder_early_unregister(struct drm_encoder *encoder)
1904 {
1905 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(encoder);
1906 
1907 	debugfs_remove_recursive(dpu_enc->debugfs_root);
1908 }
1909 
1910 static int dpu_encoder_virt_add_phys_encs(
1911 		u32 display_caps,
1912 		struct dpu_encoder_virt *dpu_enc,
1913 		struct dpu_enc_phys_init_params *params)
1914 {
1915 	struct dpu_encoder_phys *enc = NULL;
1916 
1917 	DPU_DEBUG_ENC(dpu_enc, "\n");
1918 
1919 	/*
1920 	 * We may create up to NUM_PHYS_ENCODER_TYPES physical encoder types
1921 	 * in this function, check up-front.
1922 	 */
1923 	if (dpu_enc->num_phys_encs + NUM_PHYS_ENCODER_TYPES >=
1924 			ARRAY_SIZE(dpu_enc->phys_encs)) {
1925 		DPU_ERROR_ENC(dpu_enc, "too many physical encoders %d\n",
1926 			  dpu_enc->num_phys_encs);
1927 		return -EINVAL;
1928 	}
1929 
1930 	if (display_caps & MSM_DISPLAY_CAP_VID_MODE) {
1931 		enc = dpu_encoder_phys_vid_init(params);
1932 
1933 		if (IS_ERR_OR_NULL(enc)) {
1934 			DPU_ERROR_ENC(dpu_enc, "failed to init vid enc: %ld\n",
1935 				PTR_ERR(enc));
1936 			return enc == NULL ? -EINVAL : PTR_ERR(enc);
1937 		}
1938 
1939 		dpu_enc->phys_encs[dpu_enc->num_phys_encs] = enc;
1940 		++dpu_enc->num_phys_encs;
1941 	}
1942 
1943 	if (display_caps & MSM_DISPLAY_CAP_CMD_MODE) {
1944 		enc = dpu_encoder_phys_cmd_init(params);
1945 
1946 		if (IS_ERR_OR_NULL(enc)) {
1947 			DPU_ERROR_ENC(dpu_enc, "failed to init cmd enc: %ld\n",
1948 				PTR_ERR(enc));
1949 			return enc == NULL ? -EINVAL : PTR_ERR(enc);
1950 		}
1951 
1952 		dpu_enc->phys_encs[dpu_enc->num_phys_encs] = enc;
1953 		++dpu_enc->num_phys_encs;
1954 	}
1955 
1956 	if (params->split_role == ENC_ROLE_SLAVE)
1957 		dpu_enc->cur_slave = enc;
1958 	else
1959 		dpu_enc->cur_master = enc;
1960 
1961 	return 0;
1962 }
1963 
1964 static const struct dpu_encoder_virt_ops dpu_encoder_parent_ops = {
1965 	.handle_vblank_virt = dpu_encoder_vblank_callback,
1966 	.handle_underrun_virt = dpu_encoder_underrun_callback,
1967 	.handle_frame_done = dpu_encoder_frame_done_callback,
1968 };
1969 
1970 static int dpu_encoder_setup_display(struct dpu_encoder_virt *dpu_enc,
1971 				 struct dpu_kms *dpu_kms,
1972 				 struct msm_display_info *disp_info)
1973 {
1974 	int ret = 0;
1975 	int i = 0;
1976 	enum dpu_intf_type intf_type;
1977 	struct dpu_enc_phys_init_params phys_params;
1978 
1979 	if (!dpu_enc) {
1980 		DPU_ERROR("invalid arg(s), enc %d\n", dpu_enc != NULL);
1981 		return -EINVAL;
1982 	}
1983 
1984 	dpu_enc->cur_master = NULL;
1985 
1986 	memset(&phys_params, 0, sizeof(phys_params));
1987 	phys_params.dpu_kms = dpu_kms;
1988 	phys_params.parent = &dpu_enc->base;
1989 	phys_params.parent_ops = &dpu_encoder_parent_ops;
1990 	phys_params.enc_spinlock = &dpu_enc->enc_spinlock;
1991 
1992 	DPU_DEBUG("\n");
1993 
1994 	switch (disp_info->intf_type) {
1995 	case DRM_MODE_ENCODER_DSI:
1996 		intf_type = INTF_DSI;
1997 		break;
1998 	default:
1999 		DPU_ERROR_ENC(dpu_enc, "unsupported display interface type\n");
2000 		return -EINVAL;
2001 	}
2002 
2003 	WARN_ON(disp_info->num_of_h_tiles < 1);
2004 
2005 	DPU_DEBUG("dsi_info->num_of_h_tiles %d\n", disp_info->num_of_h_tiles);
2006 
2007 	if ((disp_info->capabilities & MSM_DISPLAY_CAP_CMD_MODE) ||
2008 	    (disp_info->capabilities & MSM_DISPLAY_CAP_VID_MODE))
2009 		dpu_enc->idle_pc_supported =
2010 				dpu_kms->catalog->caps->has_idle_pc;
2011 
2012 	mutex_lock(&dpu_enc->enc_lock);
2013 	for (i = 0; i < disp_info->num_of_h_tiles && !ret; i++) {
2014 		/*
2015 		 * Left-most tile is at index 0, content is controller id
2016 		 * h_tile_instance_ids[2] = {0, 1}; DSI0 = left, DSI1 = right
2017 		 * h_tile_instance_ids[2] = {1, 0}; DSI1 = left, DSI0 = right
2018 		 */
2019 		u32 controller_id = disp_info->h_tile_instance[i];
2020 
2021 		if (disp_info->num_of_h_tiles > 1) {
2022 			if (i == 0)
2023 				phys_params.split_role = ENC_ROLE_MASTER;
2024 			else
2025 				phys_params.split_role = ENC_ROLE_SLAVE;
2026 		} else {
2027 			phys_params.split_role = ENC_ROLE_SOLO;
2028 		}
2029 
2030 		DPU_DEBUG("h_tile_instance %d = %d, split_role %d\n",
2031 				i, controller_id, phys_params.split_role);
2032 
2033 		phys_params.intf_idx = dpu_encoder_get_intf(dpu_kms->catalog,
2034 													intf_type,
2035 													controller_id);
2036 		if (phys_params.intf_idx == INTF_MAX) {
2037 			DPU_ERROR_ENC(dpu_enc, "could not get intf: type %d, id %d\n",
2038 						  intf_type, controller_id);
2039 			ret = -EINVAL;
2040 		}
2041 
2042 		if (!ret) {
2043 			ret = dpu_encoder_virt_add_phys_encs(disp_info->capabilities,
2044 												 dpu_enc,
2045 												 &phys_params);
2046 			if (ret)
2047 				DPU_ERROR_ENC(dpu_enc, "failed to add phys encs\n");
2048 		}
2049 	}
2050 
2051 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2052 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
2053 		atomic_set(&phys->vsync_cnt, 0);
2054 		atomic_set(&phys->underrun_cnt, 0);
2055 	}
2056 	mutex_unlock(&dpu_enc->enc_lock);
2057 
2058 	return ret;
2059 }
2060 
2061 static void dpu_encoder_frame_done_timeout(struct timer_list *t)
2062 {
2063 	struct dpu_encoder_virt *dpu_enc = from_timer(dpu_enc, t,
2064 			frame_done_timer);
2065 	struct drm_encoder *drm_enc = &dpu_enc->base;
2066 	u32 event;
2067 
2068 	if (!drm_enc->dev) {
2069 		DPU_ERROR("invalid parameters\n");
2070 		return;
2071 	}
2072 
2073 	if (!dpu_enc->frame_busy_mask[0] || !dpu_enc->crtc_frame_event_cb) {
2074 		DRM_DEBUG_KMS("id:%u invalid timeout frame_busy_mask=%lu\n",
2075 			      DRMID(drm_enc), dpu_enc->frame_busy_mask[0]);
2076 		return;
2077 	} else if (!atomic_xchg(&dpu_enc->frame_done_timeout_ms, 0)) {
2078 		DRM_DEBUG_KMS("id:%u invalid timeout\n", DRMID(drm_enc));
2079 		return;
2080 	}
2081 
2082 	DPU_ERROR_ENC(dpu_enc, "frame done timeout\n");
2083 
2084 	event = DPU_ENCODER_FRAME_EVENT_ERROR;
2085 	trace_dpu_enc_frame_done_timeout(DRMID(drm_enc), event);
2086 	dpu_enc->crtc_frame_event_cb(dpu_enc->crtc_frame_event_cb_data, event);
2087 }
2088 
2089 static const struct drm_encoder_helper_funcs dpu_encoder_helper_funcs = {
2090 	.mode_set = dpu_encoder_virt_mode_set,
2091 	.disable = dpu_encoder_virt_disable,
2092 	.enable = dpu_kms_encoder_enable,
2093 	.atomic_check = dpu_encoder_virt_atomic_check,
2094 
2095 	/* This is called by dpu_kms_encoder_enable */
2096 	.commit = dpu_encoder_virt_enable,
2097 };
2098 
2099 static const struct drm_encoder_funcs dpu_encoder_funcs = {
2100 		.destroy = dpu_encoder_destroy,
2101 		.late_register = dpu_encoder_late_register,
2102 		.early_unregister = dpu_encoder_early_unregister,
2103 };
2104 
2105 int dpu_encoder_setup(struct drm_device *dev, struct drm_encoder *enc,
2106 		struct msm_display_info *disp_info)
2107 {
2108 	struct msm_drm_private *priv = dev->dev_private;
2109 	struct dpu_kms *dpu_kms = to_dpu_kms(priv->kms);
2110 	struct drm_encoder *drm_enc = NULL;
2111 	struct dpu_encoder_virt *dpu_enc = NULL;
2112 	int ret = 0;
2113 
2114 	dpu_enc = to_dpu_encoder_virt(enc);
2115 
2116 	ret = dpu_encoder_setup_display(dpu_enc, dpu_kms, disp_info);
2117 	if (ret)
2118 		goto fail;
2119 
2120 	atomic_set(&dpu_enc->frame_done_timeout_ms, 0);
2121 	timer_setup(&dpu_enc->frame_done_timer,
2122 			dpu_encoder_frame_done_timeout, 0);
2123 
2124 	if (disp_info->intf_type == DRM_MODE_ENCODER_DSI)
2125 		timer_setup(&dpu_enc->vsync_event_timer,
2126 				dpu_encoder_vsync_event_handler,
2127 				0);
2128 
2129 
2130 	INIT_DELAYED_WORK(&dpu_enc->delayed_off_work,
2131 			dpu_encoder_off_work);
2132 	dpu_enc->idle_timeout = IDLE_TIMEOUT;
2133 
2134 	kthread_init_work(&dpu_enc->vsync_event_work,
2135 			dpu_encoder_vsync_event_work_handler);
2136 
2137 	memcpy(&dpu_enc->disp_info, disp_info, sizeof(*disp_info));
2138 
2139 	DPU_DEBUG_ENC(dpu_enc, "created\n");
2140 
2141 	return ret;
2142 
2143 fail:
2144 	DPU_ERROR("failed to create encoder\n");
2145 	if (drm_enc)
2146 		dpu_encoder_destroy(drm_enc);
2147 
2148 	return ret;
2149 
2150 
2151 }
2152 
2153 struct drm_encoder *dpu_encoder_init(struct drm_device *dev,
2154 		int drm_enc_mode)
2155 {
2156 	struct dpu_encoder_virt *dpu_enc = NULL;
2157 	int rc = 0;
2158 
2159 	dpu_enc = devm_kzalloc(dev->dev, sizeof(*dpu_enc), GFP_KERNEL);
2160 	if (!dpu_enc)
2161 		return ERR_PTR(-ENOMEM);
2162 
2163 	rc = drm_encoder_init(dev, &dpu_enc->base, &dpu_encoder_funcs,
2164 			drm_enc_mode, NULL);
2165 	if (rc) {
2166 		devm_kfree(dev->dev, dpu_enc);
2167 		return ERR_PTR(rc);
2168 	}
2169 
2170 	drm_encoder_helper_add(&dpu_enc->base, &dpu_encoder_helper_funcs);
2171 
2172 	spin_lock_init(&dpu_enc->enc_spinlock);
2173 	dpu_enc->enabled = false;
2174 	mutex_init(&dpu_enc->enc_lock);
2175 	mutex_init(&dpu_enc->rc_lock);
2176 
2177 	return &dpu_enc->base;
2178 }
2179 
2180 int dpu_encoder_wait_for_event(struct drm_encoder *drm_enc,
2181 	enum msm_event_wait event)
2182 {
2183 	int (*fn_wait)(struct dpu_encoder_phys *phys_enc) = NULL;
2184 	struct dpu_encoder_virt *dpu_enc = NULL;
2185 	int i, ret = 0;
2186 
2187 	if (!drm_enc) {
2188 		DPU_ERROR("invalid encoder\n");
2189 		return -EINVAL;
2190 	}
2191 	dpu_enc = to_dpu_encoder_virt(drm_enc);
2192 	DPU_DEBUG_ENC(dpu_enc, "\n");
2193 
2194 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2195 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
2196 
2197 		switch (event) {
2198 		case MSM_ENC_COMMIT_DONE:
2199 			fn_wait = phys->ops.wait_for_commit_done;
2200 			break;
2201 		case MSM_ENC_TX_COMPLETE:
2202 			fn_wait = phys->ops.wait_for_tx_complete;
2203 			break;
2204 		case MSM_ENC_VBLANK:
2205 			fn_wait = phys->ops.wait_for_vblank;
2206 			break;
2207 		default:
2208 			DPU_ERROR_ENC(dpu_enc, "unknown wait event %d\n",
2209 					event);
2210 			return -EINVAL;
2211 		}
2212 
2213 		if (fn_wait) {
2214 			DPU_ATRACE_BEGIN("wait_for_completion_event");
2215 			ret = fn_wait(phys);
2216 			DPU_ATRACE_END("wait_for_completion_event");
2217 			if (ret)
2218 				return ret;
2219 		}
2220 	}
2221 
2222 	return ret;
2223 }
2224 
2225 enum dpu_intf_mode dpu_encoder_get_intf_mode(struct drm_encoder *encoder)
2226 {
2227 	struct dpu_encoder_virt *dpu_enc = NULL;
2228 
2229 	if (!encoder) {
2230 		DPU_ERROR("invalid encoder\n");
2231 		return INTF_MODE_NONE;
2232 	}
2233 	dpu_enc = to_dpu_encoder_virt(encoder);
2234 
2235 	if (dpu_enc->cur_master)
2236 		return dpu_enc->cur_master->intf_mode;
2237 
2238 	if (dpu_enc->num_phys_encs)
2239 		return dpu_enc->phys_encs[0]->intf_mode;
2240 
2241 	return INTF_MODE_NONE;
2242 }
2243