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