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