xref: /linux/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
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-2024 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_atomic.h>
16 #include <drm/drm_crtc.h>
17 #include <drm/drm_file.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_framebuffer.h>
20 
21 #include "msm_drv.h"
22 #include "dpu_kms.h"
23 #include "dpu_hwio.h"
24 #include "dpu_hw_catalog.h"
25 #include "dpu_hw_intf.h"
26 #include "dpu_hw_ctl.h"
27 #include "dpu_hw_cwb.h"
28 #include "dpu_hw_dspp.h"
29 #include "dpu_hw_dsc.h"
30 #include "dpu_hw_merge3d.h"
31 #include "dpu_hw_cdm.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 #include "disp/msm_disp_snapshot.h"
38 
39 #define DPU_DEBUG_ENC(e, fmt, ...) DRM_DEBUG_ATOMIC("enc%d " fmt,\
40 		(e) ? (e)->base.base.id : -1, ##__VA_ARGS__)
41 
42 #define DPU_ERROR_ENC(e, fmt, ...) DPU_ERROR("enc%d " fmt,\
43 		(e) ? (e)->base.base.id : -1, ##__VA_ARGS__)
44 
45 #define DPU_ERROR_ENC_RATELIMITED(e, fmt, ...) DPU_ERROR_RATELIMITED("enc%d " fmt,\
46 		(e) ? (e)->base.base.id : -1, ##__VA_ARGS__)
47 
48 /*
49  * Two to anticipate panels that can do cmd/vid dynamic switching
50  * plan is to create all possible physical encoder types, and switch between
51  * them at runtime
52  */
53 #define NUM_PHYS_ENCODER_TYPES 2
54 
55 #define MAX_PHYS_ENCODERS_PER_VIRTUAL \
56 	(MAX_H_TILES_PER_DISPLAY * NUM_PHYS_ENCODER_TYPES)
57 
58 #define MAX_CHANNELS_PER_ENC 2
59 #define MAX_CWB_PER_ENC 2
60 
61 #define IDLE_SHORT_TIMEOUT	1
62 
63 /* timeout in frames waiting for frame done */
64 #define DPU_ENCODER_FRAME_DONE_TIMEOUT_FRAMES 5
65 
66 /**
67  * enum dpu_enc_rc_events - events for resource control state machine
68  * @DPU_ENC_RC_EVENT_KICKOFF:
69  *	This event happens at NORMAL priority.
70  *	Event that signals the start of the transfer. When this event is
71  *	received, enable MDP/DSI core clocks. Regardless of the previous
72  *	state, the resource should be in ON state at the end of this event.
73  * @DPU_ENC_RC_EVENT_FRAME_DONE:
74  *	This event happens at INTERRUPT level.
75  *	Event signals the end of the data transfer after the PP FRAME_DONE
76  *	event. At the end of this event, a delayed work is scheduled to go to
77  *	IDLE_PC state after IDLE_TIMEOUT time.
78  * @DPU_ENC_RC_EVENT_PRE_STOP:
79  *	This event happens at NORMAL priority.
80  *	This event, when received during the ON state, leave the RC STATE
81  *	in the PRE_OFF state. It should be followed by the STOP event as
82  *	part of encoder disable.
83  *	If received during IDLE or OFF states, it will do nothing.
84  * @DPU_ENC_RC_EVENT_STOP:
85  *	This event happens at NORMAL priority.
86  *	When this event is received, disable all the MDP/DSI core clocks, and
87  *	disable IRQs. It should be called from the PRE_OFF or IDLE states.
88  *	IDLE is expected when IDLE_PC has run, and PRE_OFF did nothing.
89  *	PRE_OFF is expected when PRE_STOP was executed during the ON state.
90  *	Resource state should be in OFF at the end of the event.
91  * @DPU_ENC_RC_EVENT_ENTER_IDLE:
92  *	This event happens at NORMAL priority from a work item.
93  *	Event signals that there were no frame updates for IDLE_TIMEOUT time.
94  *	This would disable MDP/DSI core clocks and change the resource state
95  *	to IDLE.
96  */
97 enum dpu_enc_rc_events {
98 	DPU_ENC_RC_EVENT_KICKOFF = 1,
99 	DPU_ENC_RC_EVENT_FRAME_DONE,
100 	DPU_ENC_RC_EVENT_PRE_STOP,
101 	DPU_ENC_RC_EVENT_STOP,
102 	DPU_ENC_RC_EVENT_ENTER_IDLE
103 };
104 
105 /*
106  * enum dpu_enc_rc_states - states that the resource control maintains
107  * @DPU_ENC_RC_STATE_OFF: Resource is in OFF state
108  * @DPU_ENC_RC_STATE_PRE_OFF: Resource is transitioning to OFF state
109  * @DPU_ENC_RC_STATE_ON: Resource is in ON state
110  * @DPU_ENC_RC_STATE_MODESET: Resource is in modeset state
111  * @DPU_ENC_RC_STATE_IDLE: Resource is in IDLE state
112  */
113 enum dpu_enc_rc_states {
114 	DPU_ENC_RC_STATE_OFF,
115 	DPU_ENC_RC_STATE_PRE_OFF,
116 	DPU_ENC_RC_STATE_ON,
117 	DPU_ENC_RC_STATE_IDLE
118 };
119 
120 /**
121  * struct dpu_encoder_virt - virtual encoder. Container of one or more physical
122  *	encoders. Virtual encoder manages one "logical" display. Physical
123  *	encoders manage one intf block, tied to a specific panel/sub-panel.
124  *	Virtual encoder defers as much as possible to the physical encoders.
125  *	Virtual encoder registers itself with the DRM Framework as the encoder.
126  * @base:		drm_encoder base class for registration with DRM
127  * @enc_spinlock:	Virtual-Encoder-Wide Spin Lock for IRQ purposes
128  * @enabled:		True if the encoder is active, protected by enc_lock
129  * @commit_done_timedout: True if there has been a timeout on commit after
130  *			enabling the encoder.
131  * @num_phys_encs:	Actual number of physical encoders contained.
132  * @phys_encs:		Container of physical encoders managed.
133  * @cur_master:		Pointer to the current master in this mode. Optimization
134  *			Only valid after enable. Cleared as disable.
135  * @cur_slave:		As above but for the slave encoder.
136  * @hw_pp:		Handle to the pingpong blocks used for the display. No.
137  *			pingpong blocks can be different than num_phys_encs.
138  * @hw_cwb:		Handle to the CWB muxes used for concurrent writeback
139  *			display. Number of CWB muxes can be different than
140  *			num_phys_encs.
141  * @hw_dsc:		Handle to the DSC blocks used for the display.
142  * @dsc_mask:		Bitmask of used DSC blocks.
143  * @cwb_mask:		Bitmask of used CWB muxes
144  * @intfs_swapped:	Whether or not the phys_enc interfaces have been swapped
145  *			for partial update right-only cases, such as pingpong
146  *			split where virtual pingpong does not generate IRQs
147  * @crtc:		Pointer to the currently assigned crtc. Normally you
148  *			would use crtc->state->encoder_mask to determine the
149  *			link between encoder/crtc. However in this case we need
150  *			to track crtc in the disable() hook which is called
151  *			_after_ encoder_mask is cleared.
152  * @connector:		If a mode is set, cached pointer to the active connector
153  * @enc_lock:			Lock around physical encoder
154  *				create/destroy/enable/disable
155  * @frame_busy_mask:		Bitmask tracking which phys_enc we are still
156  *				busy processing current command.
157  *				Bit0 = phys_encs[0] etc.
158  * @frame_done_timeout_ms:	frame done timeout in ms
159  * @frame_done_timeout_cnt:	atomic counter tracking the number of frame
160  * 				done timeouts
161  * @frame_done_timer:		watchdog timer for frame done event
162  * @disp_info:			local copy of msm_display_info struct
163  * @idle_pc_supported:		indicate if idle power collaps is supported
164  * @rc_lock:			resource control mutex lock to protect
165  *				virt encoder over various state changes
166  * @rc_state:			resource controller state
167  * @delayed_off_work:		delayed worker to schedule disabling of
168  *				clks and resources after IDLE_TIMEOUT time.
169  * @topology:                   topology of the display
170  * @idle_timeout:		idle timeout duration in milliseconds
171  * @wide_bus_en:		wide bus is enabled on this interface
172  * @dsc:			drm_dsc_config pointer, for DSC-enabled encoders
173  */
174 struct dpu_encoder_virt {
175 	struct drm_encoder base;
176 	spinlock_t enc_spinlock;
177 
178 	bool enabled;
179 	bool commit_done_timedout;
180 
181 	unsigned int num_phys_encs;
182 	struct dpu_encoder_phys *phys_encs[MAX_PHYS_ENCODERS_PER_VIRTUAL];
183 	struct dpu_encoder_phys *cur_master;
184 	struct dpu_encoder_phys *cur_slave;
185 	struct dpu_hw_pingpong *hw_pp[MAX_CHANNELS_PER_ENC];
186 	struct dpu_hw_cwb *hw_cwb[MAX_CWB_PER_ENC];
187 	struct dpu_hw_dsc *hw_dsc[MAX_CHANNELS_PER_ENC];
188 
189 	unsigned int dsc_mask;
190 	unsigned int cwb_mask;
191 
192 	bool intfs_swapped;
193 
194 	struct drm_crtc *crtc;
195 	struct drm_connector *connector;
196 
197 	struct mutex enc_lock;
198 	DECLARE_BITMAP(frame_busy_mask, MAX_PHYS_ENCODERS_PER_VIRTUAL);
199 
200 	atomic_t frame_done_timeout_ms;
201 	atomic_t frame_done_timeout_cnt;
202 	struct timer_list frame_done_timer;
203 
204 	struct msm_display_info disp_info;
205 
206 	bool idle_pc_supported;
207 	struct mutex rc_lock;
208 	enum dpu_enc_rc_states rc_state;
209 	struct delayed_work delayed_off_work;
210 	struct msm_display_topology topology;
211 
212 	u32 idle_timeout;
213 
214 	bool wide_bus_en;
215 
216 	/* DSC configuration */
217 	struct drm_dsc_config *dsc;
218 };
219 
220 #define to_dpu_encoder_virt(x) container_of(x, struct dpu_encoder_virt, base)
221 
222 static u32 dither_matrix[DITHER_MATRIX_SZ] = {
223 	15, 7, 13, 5, 3, 11, 1, 9, 12, 4, 14, 6, 0, 8, 2, 10
224 };
225 
226 /**
227  * dpu_encoder_get_drm_fmt - return DRM fourcc format
228  * @phys_enc: Pointer to physical encoder structure
229  */
230 u32 dpu_encoder_get_drm_fmt(struct dpu_encoder_phys *phys_enc)
231 {
232 	struct drm_encoder *drm_enc;
233 	struct dpu_encoder_virt *dpu_enc;
234 	struct drm_display_info *info;
235 	struct drm_display_mode *mode;
236 
237 	drm_enc = phys_enc->parent;
238 	dpu_enc = to_dpu_encoder_virt(drm_enc);
239 	info = &dpu_enc->connector->display_info;
240 	mode = &phys_enc->cached_mode;
241 
242 	if (drm_mode_is_420_only(info, mode))
243 		return DRM_FORMAT_YUV420;
244 
245 	return DRM_FORMAT_RGB888;
246 }
247 
248 /**
249  * dpu_encoder_needs_periph_flush - return true if physical encoder requires
250  *	peripheral flush
251  * @phys_enc: Pointer to physical encoder structure
252  */
253 bool dpu_encoder_needs_periph_flush(struct dpu_encoder_phys *phys_enc)
254 {
255 	struct drm_encoder *drm_enc;
256 	struct dpu_encoder_virt *dpu_enc;
257 	struct msm_display_info *disp_info;
258 	struct msm_drm_private *priv;
259 	struct drm_display_mode *mode;
260 
261 	drm_enc = phys_enc->parent;
262 	dpu_enc = to_dpu_encoder_virt(drm_enc);
263 	disp_info = &dpu_enc->disp_info;
264 	priv = drm_enc->dev->dev_private;
265 	mode = &phys_enc->cached_mode;
266 
267 	return phys_enc->hw_intf->cap->type == INTF_DP &&
268 	       msm_dp_needs_periph_flush(priv->kms->dp[disp_info->h_tile_instance[0]], mode);
269 }
270 
271 /**
272  * dpu_encoder_is_widebus_enabled - return bool value if widebus is enabled
273  * @drm_enc:    Pointer to previously created drm encoder structure
274  */
275 bool dpu_encoder_is_widebus_enabled(const struct drm_encoder *drm_enc)
276 {
277 	const struct dpu_encoder_virt *dpu_enc;
278 	struct msm_drm_private *priv = drm_enc->dev->dev_private;
279 	const struct msm_display_info *disp_info;
280 	int index;
281 
282 	dpu_enc = to_dpu_encoder_virt(drm_enc);
283 	disp_info = &dpu_enc->disp_info;
284 	index = disp_info->h_tile_instance[0];
285 
286 	if (disp_info->intf_type == INTF_DP)
287 		return msm_dp_wide_bus_available(priv->kms->dp[index]);
288 	else if (disp_info->intf_type == INTF_DSI)
289 		return msm_dsi_wide_bus_enabled(priv->kms->dsi[index]);
290 
291 	return false;
292 }
293 
294 /**
295  * dpu_encoder_is_dsc_enabled - indicate whether dsc is enabled
296  *				for the encoder.
297  * @drm_enc:    Pointer to previously created drm encoder structure
298  */
299 bool dpu_encoder_is_dsc_enabled(const struct drm_encoder *drm_enc)
300 {
301 	const struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
302 
303 	return dpu_enc->dsc ? true : false;
304 }
305 
306 /**
307  * dpu_encoder_get_crc_values_cnt - get number of physical encoders contained
308  *	in virtual encoder that can collect CRC values
309  * @drm_enc:    Pointer to previously created drm encoder structure
310  * Returns:     Number of physical encoders for given drm encoder
311  */
312 int dpu_encoder_get_crc_values_cnt(const struct drm_encoder *drm_enc)
313 {
314 	struct dpu_encoder_virt *dpu_enc;
315 	int i, num_intf = 0;
316 
317 	dpu_enc = to_dpu_encoder_virt(drm_enc);
318 
319 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
320 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
321 
322 		if (phys->hw_intf && phys->hw_intf->ops.setup_misr
323 				&& phys->hw_intf->ops.collect_misr)
324 			num_intf++;
325 	}
326 
327 	return num_intf;
328 }
329 
330 /**
331  * dpu_encoder_setup_misr - enable misr calculations
332  * @drm_enc:    Pointer to previously created drm encoder structure
333  */
334 void dpu_encoder_setup_misr(const struct drm_encoder *drm_enc)
335 {
336 	struct dpu_encoder_virt *dpu_enc;
337 
338 	int i;
339 
340 	dpu_enc = to_dpu_encoder_virt(drm_enc);
341 
342 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
343 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
344 
345 		if (!phys->hw_intf || !phys->hw_intf->ops.setup_misr)
346 			continue;
347 
348 		phys->hw_intf->ops.setup_misr(phys->hw_intf);
349 	}
350 }
351 
352 /**
353  * dpu_encoder_get_crc - get the crc value from interface blocks
354  * @drm_enc:    Pointer to previously created drm encoder structure
355  * @crcs:	array to fill with CRC data
356  * @pos:	offset into the @crcs array
357  * Returns:     0 on success, error otherwise
358  */
359 int dpu_encoder_get_crc(const struct drm_encoder *drm_enc, u32 *crcs, int pos)
360 {
361 	struct dpu_encoder_virt *dpu_enc;
362 
363 	int i, rc = 0, entries_added = 0;
364 
365 	if (!drm_enc->crtc) {
366 		DRM_ERROR("no crtc found for encoder %d\n", drm_enc->index);
367 		return -EINVAL;
368 	}
369 
370 	dpu_enc = to_dpu_encoder_virt(drm_enc);
371 
372 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
373 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
374 
375 		if (!phys->hw_intf || !phys->hw_intf->ops.collect_misr)
376 			continue;
377 
378 		rc = phys->hw_intf->ops.collect_misr(phys->hw_intf, &crcs[pos + entries_added]);
379 		if (rc)
380 			return rc;
381 		entries_added++;
382 	}
383 
384 	return entries_added;
385 }
386 
387 static void _dpu_encoder_setup_dither(struct dpu_hw_pingpong *hw_pp, unsigned bpc)
388 {
389 	struct dpu_hw_dither_cfg dither_cfg = { 0 };
390 
391 	if (!hw_pp->ops.setup_dither)
392 		return;
393 
394 	switch (bpc) {
395 	case 6:
396 		dither_cfg.c0_bitdepth = 6;
397 		dither_cfg.c1_bitdepth = 6;
398 		dither_cfg.c2_bitdepth = 6;
399 		dither_cfg.c3_bitdepth = 6;
400 		dither_cfg.temporal_en = 0;
401 		break;
402 	default:
403 		hw_pp->ops.setup_dither(hw_pp, NULL);
404 		return;
405 	}
406 
407 	memcpy(&dither_cfg.matrix, dither_matrix,
408 			sizeof(u32) * DITHER_MATRIX_SZ);
409 
410 	hw_pp->ops.setup_dither(hw_pp, &dither_cfg);
411 }
412 
413 static char *dpu_encoder_helper_get_intf_type(enum dpu_intf_mode intf_mode)
414 {
415 	switch (intf_mode) {
416 	case INTF_MODE_VIDEO:
417 		return "INTF_MODE_VIDEO";
418 	case INTF_MODE_CMD:
419 		return "INTF_MODE_CMD";
420 	case INTF_MODE_WB_BLOCK:
421 		return "INTF_MODE_WB_BLOCK";
422 	case INTF_MODE_WB_LINE:
423 		return "INTF_MODE_WB_LINE";
424 	default:
425 		return "INTF_MODE_UNKNOWN";
426 	}
427 }
428 
429 /**
430  * dpu_encoder_helper_report_irq_timeout - utility to report error that irq has
431  *	timed out, including reporting frame error event to crtc and debug dump
432  * @phys_enc: Pointer to physical encoder structure
433  * @intr_idx: Failing interrupt index
434  */
435 void dpu_encoder_helper_report_irq_timeout(struct dpu_encoder_phys *phys_enc,
436 		enum dpu_intr_idx intr_idx)
437 {
438 	DRM_ERROR("irq timeout id=%u, intf_mode=%s intf=%d wb=%d, pp=%d, intr=%d\n",
439 			DRMID(phys_enc->parent),
440 			dpu_encoder_helper_get_intf_type(phys_enc->intf_mode),
441 			phys_enc->hw_intf ? phys_enc->hw_intf->idx - INTF_0 : -1,
442 			phys_enc->hw_wb ? phys_enc->hw_wb->idx - WB_0 : -1,
443 			phys_enc->hw_pp->idx - PINGPONG_0, intr_idx);
444 
445 	dpu_encoder_frame_done_callback(phys_enc->parent, phys_enc,
446 				DPU_ENCODER_FRAME_EVENT_ERROR);
447 }
448 
449 static int dpu_encoder_helper_wait_event_timeout(int32_t drm_id,
450 		u32 irq_idx, struct dpu_encoder_wait_info *info);
451 
452 /**
453  * dpu_encoder_helper_wait_for_irq - utility to wait on an irq.
454  *	note: will call dpu_encoder_helper_wait_for_irq on timeout
455  * @phys_enc: Pointer to physical encoder structure
456  * @irq_idx: IRQ index
457  * @func: IRQ callback to be called in case of timeout
458  * @wait_info: wait info struct
459  * @return: 0 or -ERROR
460  */
461 int dpu_encoder_helper_wait_for_irq(struct dpu_encoder_phys *phys_enc,
462 		unsigned int irq_idx,
463 		void (*func)(void *arg),
464 		struct dpu_encoder_wait_info *wait_info)
465 {
466 	u32 irq_status;
467 	int ret;
468 
469 	if (!wait_info) {
470 		DPU_ERROR("invalid params\n");
471 		return -EINVAL;
472 	}
473 	/* note: do master / slave checking outside */
474 
475 	/* return EWOULDBLOCK since we know the wait isn't necessary */
476 	if (phys_enc->enable_state == DPU_ENC_DISABLED) {
477 		DRM_ERROR("encoder is disabled id=%u, callback=%ps, IRQ=[%d, %d]\n",
478 			  DRMID(phys_enc->parent), func,
479 			  DPU_IRQ_REG(irq_idx), DPU_IRQ_BIT(irq_idx));
480 		return -EWOULDBLOCK;
481 	}
482 
483 	if (irq_idx == 0) {
484 		DRM_DEBUG_KMS("skip irq wait id=%u, callback=%ps\n",
485 			      DRMID(phys_enc->parent), func);
486 		return 0;
487 	}
488 
489 	DRM_DEBUG_KMS("id=%u, callback=%ps, IRQ=[%d, %d], pp=%d, pending_cnt=%d\n",
490 		      DRMID(phys_enc->parent), func,
491 		      DPU_IRQ_REG(irq_idx), DPU_IRQ_BIT(irq_idx), phys_enc->hw_pp->idx - PINGPONG_0,
492 		      atomic_read(wait_info->atomic_cnt));
493 
494 	ret = dpu_encoder_helper_wait_event_timeout(
495 			DRMID(phys_enc->parent),
496 			irq_idx,
497 			wait_info);
498 
499 	if (ret <= 0) {
500 		irq_status = dpu_core_irq_read(phys_enc->dpu_kms, irq_idx);
501 		if (irq_status) {
502 			unsigned long flags;
503 
504 			DRM_DEBUG_KMS("IRQ=[%d, %d] not triggered id=%u, callback=%ps, pp=%d, atomic_cnt=%d\n",
505 				      DPU_IRQ_REG(irq_idx), DPU_IRQ_BIT(irq_idx),
506 				      DRMID(phys_enc->parent), func,
507 				      phys_enc->hw_pp->idx - PINGPONG_0,
508 				      atomic_read(wait_info->atomic_cnt));
509 			local_irq_save(flags);
510 			func(phys_enc);
511 			local_irq_restore(flags);
512 			ret = 0;
513 		} else {
514 			ret = -ETIMEDOUT;
515 			DRM_DEBUG_KMS("IRQ=[%d, %d] timeout id=%u, callback=%ps, pp=%d, atomic_cnt=%d\n",
516 				      DPU_IRQ_REG(irq_idx), DPU_IRQ_BIT(irq_idx),
517 				      DRMID(phys_enc->parent), func,
518 				      phys_enc->hw_pp->idx - PINGPONG_0,
519 				      atomic_read(wait_info->atomic_cnt));
520 		}
521 	} else {
522 		ret = 0;
523 		trace_dpu_enc_irq_wait_success(DRMID(phys_enc->parent),
524 			func, DPU_IRQ_REG(irq_idx), DPU_IRQ_BIT(irq_idx),
525 			phys_enc->hw_pp->idx - PINGPONG_0,
526 			atomic_read(wait_info->atomic_cnt));
527 	}
528 
529 	return ret;
530 }
531 
532 /**
533  * dpu_encoder_get_vsync_count - get vsync count for the encoder.
534  * @drm_enc:    Pointer to previously created drm encoder structure
535  */
536 int dpu_encoder_get_vsync_count(struct drm_encoder *drm_enc)
537 {
538 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
539 	struct dpu_encoder_phys *phys = dpu_enc ? dpu_enc->cur_master : NULL;
540 	return phys ? atomic_read(&phys->vsync_cnt) : 0;
541 }
542 
543 /**
544  * dpu_encoder_get_linecount - get interface line count for the encoder.
545  * @drm_enc:    Pointer to previously created drm encoder structure
546  */
547 int dpu_encoder_get_linecount(struct drm_encoder *drm_enc)
548 {
549 	struct dpu_encoder_virt *dpu_enc;
550 	struct dpu_encoder_phys *phys;
551 	int linecount = 0;
552 
553 	dpu_enc = to_dpu_encoder_virt(drm_enc);
554 	phys = dpu_enc ? dpu_enc->cur_master : NULL;
555 
556 	if (phys && phys->ops.get_line_count)
557 		linecount = phys->ops.get_line_count(phys);
558 
559 	return linecount;
560 }
561 
562 /**
563  * dpu_encoder_helper_split_config - split display configuration helper function
564  *	This helper function may be used by physical encoders to configure
565  *	the split display related registers.
566  * @phys_enc: Pointer to physical encoder structure
567  * @interface: enum dpu_intf setting
568  */
569 void dpu_encoder_helper_split_config(
570 		struct dpu_encoder_phys *phys_enc,
571 		enum dpu_intf interface)
572 {
573 	struct dpu_encoder_virt *dpu_enc;
574 	struct split_pipe_cfg cfg = { 0 };
575 	struct dpu_hw_mdp *hw_mdptop;
576 	struct msm_display_info *disp_info;
577 
578 	if (!phys_enc->hw_mdptop || !phys_enc->parent) {
579 		DPU_ERROR("invalid arg(s), encoder %d\n", phys_enc != NULL);
580 		return;
581 	}
582 
583 	dpu_enc = to_dpu_encoder_virt(phys_enc->parent);
584 	hw_mdptop = phys_enc->hw_mdptop;
585 	disp_info = &dpu_enc->disp_info;
586 
587 	if (disp_info->intf_type != INTF_DSI)
588 		return;
589 
590 	/**
591 	 * disable split modes since encoder will be operating in as the only
592 	 * encoder, either for the entire use case in the case of, for example,
593 	 * single DSI, or for this frame in the case of left/right only partial
594 	 * update.
595 	 */
596 	if (phys_enc->split_role == ENC_ROLE_SOLO) {
597 		if (hw_mdptop->ops.setup_split_pipe)
598 			hw_mdptop->ops.setup_split_pipe(hw_mdptop, &cfg);
599 		return;
600 	}
601 
602 	cfg.en = true;
603 	cfg.mode = phys_enc->intf_mode;
604 	cfg.intf = interface;
605 
606 	if (cfg.en && phys_enc->ops.needs_single_flush &&
607 			phys_enc->ops.needs_single_flush(phys_enc))
608 		cfg.split_flush_en = true;
609 
610 	if (phys_enc->split_role == ENC_ROLE_MASTER) {
611 		DPU_DEBUG_ENC(dpu_enc, "enable %d\n", cfg.en);
612 
613 		if (hw_mdptop->ops.setup_split_pipe)
614 			hw_mdptop->ops.setup_split_pipe(hw_mdptop, &cfg);
615 	}
616 }
617 
618 /**
619  * dpu_encoder_use_dsc_merge - returns true if the encoder uses DSC merge topology.
620  * @drm_enc:    Pointer to previously created drm encoder structure
621  */
622 bool dpu_encoder_use_dsc_merge(struct drm_encoder *drm_enc)
623 {
624 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
625 	int i, intf_count = 0, num_dsc = 0;
626 
627 	for (i = 0; i < MAX_PHYS_ENCODERS_PER_VIRTUAL; i++)
628 		if (dpu_enc->phys_encs[i])
629 			intf_count++;
630 
631 	for (i = 0; i < MAX_CHANNELS_PER_ENC; i++)
632 		if (dpu_enc->hw_dsc[i])
633 			num_dsc++;
634 
635 	return (num_dsc > 0) && (num_dsc > intf_count);
636 }
637 
638 /**
639  * dpu_encoder_get_dsc_config - get DSC config for the DPU encoder
640  *   This helper function is used by physical encoder to get DSC config
641  *   used for this encoder.
642  * @drm_enc: Pointer to encoder structure
643  */
644 struct drm_dsc_config *dpu_encoder_get_dsc_config(struct drm_encoder *drm_enc)
645 {
646 	struct msm_drm_private *priv = drm_enc->dev->dev_private;
647 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
648 	int index = dpu_enc->disp_info.h_tile_instance[0];
649 
650 	if (dpu_enc->disp_info.intf_type == INTF_DSI)
651 		return msm_dsi_get_dsc_config(priv->kms->dsi[index]);
652 
653 	return NULL;
654 }
655 
656 void dpu_encoder_update_topology(struct drm_encoder *drm_enc,
657 				 struct msm_display_topology *topology,
658 				 struct drm_atomic_commit *state,
659 				 const struct drm_display_mode *adj_mode)
660 {
661 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
662 	struct msm_drm_private *priv = dpu_enc->base.dev->dev_private;
663 	struct msm_display_info *disp_info = &dpu_enc->disp_info;
664 	struct dpu_kms *dpu_kms = to_dpu_kms(priv->kms);
665 	struct drm_connector *connector;
666 	struct drm_connector_state *conn_state;
667 	struct drm_framebuffer *fb;
668 	struct drm_dsc_config *dsc;
669 
670 	int i;
671 
672 	for (i = 0; i < MAX_PHYS_ENCODERS_PER_VIRTUAL; i++)
673 		if (dpu_enc->phys_encs[i])
674 			topology->num_intf++;
675 
676 	dsc = dpu_encoder_get_dsc_config(drm_enc);
677 
678 	/* We only support 2 DSC mode (with 2 LM and 1 INTF) */
679 	if (dsc) {
680 		/*
681 		 * Use 2 DSC encoders, 2 layer mixers and 1 or 2 interfaces
682 		 * when Display Stream Compression (DSC) is enabled,
683 		 * and when enough DSC blocks are available.
684 		 * This is power-optimal and can drive up to (including) 4k
685 		 * screens.
686 		 */
687 		WARN(topology->num_intf > 2,
688 		     "DSC topology cannot support more than 2 interfaces\n");
689 		if (topology->num_intf >= 2 || dpu_kms->catalog->dsc_count >= 2)
690 			topology->num_dsc = 2;
691 		else
692 			topology->num_dsc = 1;
693 	}
694 
695 	connector = drm_atomic_get_new_connector_for_encoder(state, drm_enc);
696 	if (!connector)
697 		return;
698 	conn_state = drm_atomic_get_new_connector_state(state, connector);
699 	if (!conn_state)
700 		return;
701 
702 	/*
703 	 * Use CDM only for writeback or DP at the moment as other interfaces cannot handle it.
704 	 * If writeback itself cannot handle cdm for some reason it will fail in its atomic_check()
705 	 * earlier.
706 	 */
707 	if (disp_info->intf_type == INTF_WB && conn_state->writeback_job) {
708 		fb = conn_state->writeback_job->fb;
709 
710 		if (fb && MSM_FORMAT_IS_YUV(msm_framebuffer_format(fb)))
711 			topology->num_cdm++;
712 	} else if (disp_info->intf_type == INTF_DP) {
713 		if (drm_mode_is_420_only(&connector->display_info, adj_mode))
714 			topology->num_cdm++;
715 	}
716 }
717 
718 bool dpu_encoder_needs_modeset(struct drm_encoder *drm_enc, struct drm_atomic_commit *state)
719 {
720 	struct drm_connector *connector;
721 	struct drm_connector_state *conn_state;
722 	struct drm_framebuffer *fb;
723 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
724 
725 	if (!drm_enc || !state)
726 		return false;
727 
728 	connector = drm_atomic_get_new_connector_for_encoder(state, drm_enc);
729 	if (!connector)
730 		return false;
731 
732 	conn_state = drm_atomic_get_new_connector_state(state, connector);
733 	if (!conn_state)
734 		return false;
735 
736 	/**
737 	 * These checks are duplicated from dpu_encoder_update_topology() since
738 	 * CRTC and encoder don't hold topology information
739 	 */
740 	if (dpu_enc->disp_info.intf_type == INTF_WB && conn_state->writeback_job) {
741 		fb = conn_state->writeback_job->fb;
742 		if (fb && MSM_FORMAT_IS_YUV(msm_framebuffer_format(fb))) {
743 			if (!dpu_enc->cur_master->hw_cdm)
744 				return true;
745 		} else {
746 			if (dpu_enc->cur_master->hw_cdm)
747 				return true;
748 		}
749 	}
750 
751 	return false;
752 }
753 
754 static void _dpu_encoder_update_vsync_source(struct dpu_encoder_virt *dpu_enc,
755 			struct msm_display_info *disp_info)
756 {
757 	struct dpu_vsync_source_cfg vsync_cfg = { 0 };
758 	struct msm_drm_private *priv;
759 	struct dpu_kms *dpu_kms;
760 	struct dpu_hw_mdp *hw_mdptop;
761 	struct drm_encoder *drm_enc;
762 	struct dpu_encoder_phys *phys_enc;
763 	int i;
764 
765 	if (!dpu_enc || !disp_info) {
766 		DPU_ERROR("invalid param dpu_enc:%d or disp_info:%d\n",
767 					dpu_enc != NULL, disp_info != NULL);
768 		return;
769 	} else if (dpu_enc->num_phys_encs > ARRAY_SIZE(dpu_enc->hw_pp)) {
770 		DPU_ERROR("invalid num phys enc %d/%d\n",
771 				dpu_enc->num_phys_encs,
772 				(int) ARRAY_SIZE(dpu_enc->hw_pp));
773 		return;
774 	}
775 
776 	drm_enc = &dpu_enc->base;
777 	/* this pointers are checked in virt_enable_helper */
778 	priv = drm_enc->dev->dev_private;
779 
780 	dpu_kms = to_dpu_kms(priv->kms);
781 	hw_mdptop = dpu_kms->hw_mdp;
782 	if (!hw_mdptop) {
783 		DPU_ERROR("invalid mdptop\n");
784 		return;
785 	}
786 
787 	vsync_cfg.vsync_source = disp_info->vsync_source;
788 	vsync_cfg.frame_rate = drm_mode_vrefresh(&dpu_enc->base.crtc->state->adjusted_mode);
789 
790 	if (hw_mdptop->ops.setup_vsync_source) {
791 		for (i = 0; i < dpu_enc->num_phys_encs; i++)
792 			vsync_cfg.ppnumber[i] = dpu_enc->hw_pp[i]->idx;
793 
794 		vsync_cfg.pp_count = dpu_enc->num_phys_encs;
795 
796 		hw_mdptop->ops.setup_vsync_source(hw_mdptop, &vsync_cfg);
797 	}
798 
799 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
800 		phys_enc = dpu_enc->phys_encs[i];
801 
802 		if (phys_enc->has_intf_te && phys_enc->hw_intf->ops.vsync_sel)
803 			phys_enc->hw_intf->ops.vsync_sel(phys_enc->hw_intf,
804 							 &vsync_cfg);
805 	}
806 }
807 
808 static void _dpu_encoder_irq_enable(struct drm_encoder *drm_enc)
809 {
810 	struct dpu_encoder_virt *dpu_enc;
811 	int i;
812 
813 	if (!drm_enc) {
814 		DPU_ERROR("invalid encoder\n");
815 		return;
816 	}
817 
818 	dpu_enc = to_dpu_encoder_virt(drm_enc);
819 
820 	DPU_DEBUG_ENC(dpu_enc, "\n");
821 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
822 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
823 
824 		phys->ops.irq_enable(phys);
825 	}
826 }
827 
828 static void _dpu_encoder_irq_disable(struct drm_encoder *drm_enc)
829 {
830 	struct dpu_encoder_virt *dpu_enc;
831 	int i;
832 
833 	if (!drm_enc) {
834 		DPU_ERROR("invalid encoder\n");
835 		return;
836 	}
837 
838 	dpu_enc = to_dpu_encoder_virt(drm_enc);
839 
840 	DPU_DEBUG_ENC(dpu_enc, "\n");
841 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
842 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
843 
844 		phys->ops.irq_disable(phys);
845 	}
846 }
847 
848 static void _dpu_encoder_resource_enable(struct drm_encoder *drm_enc)
849 {
850 	struct msm_drm_private *priv;
851 	struct dpu_kms *dpu_kms;
852 	struct dpu_encoder_virt *dpu_enc;
853 
854 	dpu_enc = to_dpu_encoder_virt(drm_enc);
855 	priv = drm_enc->dev->dev_private;
856 	dpu_kms = to_dpu_kms(priv->kms);
857 
858 	trace_dpu_enc_rc_enable(DRMID(drm_enc));
859 
860 	if (!dpu_enc->cur_master) {
861 		DPU_ERROR("encoder master not set\n");
862 		return;
863 	}
864 
865 	/* enable DPU core clks */
866 	pm_runtime_get_sync(&dpu_kms->pdev->dev);
867 
868 	/* enable all the irq */
869 	_dpu_encoder_irq_enable(drm_enc);
870 }
871 
872 static void _dpu_encoder_resource_disable(struct drm_encoder *drm_enc)
873 {
874 	struct msm_drm_private *priv;
875 	struct dpu_kms *dpu_kms;
876 	struct dpu_encoder_virt *dpu_enc;
877 
878 	dpu_enc = to_dpu_encoder_virt(drm_enc);
879 	priv = drm_enc->dev->dev_private;
880 	dpu_kms = to_dpu_kms(priv->kms);
881 
882 	trace_dpu_enc_rc_disable(DRMID(drm_enc));
883 
884 	if (!dpu_enc->cur_master) {
885 		DPU_ERROR("encoder master not set\n");
886 		return;
887 	}
888 
889 	/* disable all the irq */
890 	_dpu_encoder_irq_disable(drm_enc);
891 
892 	/* disable DPU core clks */
893 	pm_runtime_put_sync(&dpu_kms->pdev->dev);
894 }
895 
896 static int dpu_encoder_resource_control(struct drm_encoder *drm_enc,
897 		u32 sw_event)
898 {
899 	struct dpu_encoder_virt *dpu_enc;
900 	struct msm_drm_private *priv;
901 	bool is_vid_mode = false;
902 
903 	if (!drm_enc || !drm_enc->dev || !drm_enc->crtc) {
904 		DPU_ERROR("invalid parameters\n");
905 		return -EINVAL;
906 	}
907 	dpu_enc = to_dpu_encoder_virt(drm_enc);
908 	priv = drm_enc->dev->dev_private;
909 	is_vid_mode = !dpu_enc->disp_info.is_cmd_mode;
910 
911 	/*
912 	 * when idle_pc is not supported, process only KICKOFF, STOP and MODESET
913 	 * events and return early for other events (ie wb display).
914 	 */
915 	if (!dpu_enc->idle_pc_supported &&
916 			(sw_event != DPU_ENC_RC_EVENT_KICKOFF &&
917 			sw_event != DPU_ENC_RC_EVENT_STOP &&
918 			sw_event != DPU_ENC_RC_EVENT_PRE_STOP))
919 		return 0;
920 
921 	trace_dpu_enc_rc(DRMID(drm_enc), sw_event, dpu_enc->idle_pc_supported,
922 			 dpu_enc->rc_state, "begin");
923 
924 	switch (sw_event) {
925 	case DPU_ENC_RC_EVENT_KICKOFF:
926 		/* cancel delayed off work, if any */
927 		if (cancel_delayed_work_sync(&dpu_enc->delayed_off_work))
928 			DPU_DEBUG_ENC(dpu_enc, "sw_event:%d, work cancelled\n",
929 					sw_event);
930 
931 		mutex_lock(&dpu_enc->rc_lock);
932 
933 		/* return if the resource control is already in ON state */
934 		if (dpu_enc->rc_state == DPU_ENC_RC_STATE_ON) {
935 			DRM_DEBUG_ATOMIC("id;%u, sw_event:%d, rc in ON state\n",
936 				      DRMID(drm_enc), sw_event);
937 			mutex_unlock(&dpu_enc->rc_lock);
938 			return 0;
939 		} else if (dpu_enc->rc_state != DPU_ENC_RC_STATE_OFF &&
940 				dpu_enc->rc_state != DPU_ENC_RC_STATE_IDLE) {
941 			DRM_DEBUG_ATOMIC("id;%u, sw_event:%d, rc in state %d\n",
942 				      DRMID(drm_enc), sw_event,
943 				      dpu_enc->rc_state);
944 			mutex_unlock(&dpu_enc->rc_lock);
945 			return -EINVAL;
946 		}
947 
948 		if (is_vid_mode && dpu_enc->rc_state == DPU_ENC_RC_STATE_IDLE)
949 			_dpu_encoder_irq_enable(drm_enc);
950 		else
951 			_dpu_encoder_resource_enable(drm_enc);
952 
953 		dpu_enc->rc_state = DPU_ENC_RC_STATE_ON;
954 
955 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
956 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
957 				 "kickoff");
958 
959 		mutex_unlock(&dpu_enc->rc_lock);
960 		break;
961 
962 	case DPU_ENC_RC_EVENT_FRAME_DONE:
963 		/*
964 		 * mutex lock is not used as this event happens at interrupt
965 		 * context. And locking is not required as, the other events
966 		 * like KICKOFF and STOP does a wait-for-idle before executing
967 		 * the resource_control
968 		 */
969 		if (dpu_enc->rc_state != DPU_ENC_RC_STATE_ON) {
970 			DRM_DEBUG_KMS("id:%d, sw_event:%d,rc:%d-unexpected\n",
971 				      DRMID(drm_enc), sw_event,
972 				      dpu_enc->rc_state);
973 			return -EINVAL;
974 		}
975 
976 		/*
977 		 * schedule off work item only when there are no
978 		 * frames pending
979 		 */
980 		if (dpu_crtc_frame_pending(drm_enc->crtc) > 1) {
981 			DRM_DEBUG_KMS("id:%d skip schedule work\n",
982 				      DRMID(drm_enc));
983 			return 0;
984 		}
985 
986 		queue_delayed_work(priv->kms->wq, &dpu_enc->delayed_off_work,
987 				   msecs_to_jiffies(dpu_enc->idle_timeout));
988 
989 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
990 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
991 				 "frame done");
992 		break;
993 
994 	case DPU_ENC_RC_EVENT_PRE_STOP:
995 		/* cancel delayed off work, if any */
996 		if (cancel_delayed_work_sync(&dpu_enc->delayed_off_work))
997 			DPU_DEBUG_ENC(dpu_enc, "sw_event:%d, work cancelled\n",
998 					sw_event);
999 
1000 		mutex_lock(&dpu_enc->rc_lock);
1001 
1002 		if (is_vid_mode &&
1003 			  dpu_enc->rc_state == DPU_ENC_RC_STATE_IDLE) {
1004 			_dpu_encoder_irq_enable(drm_enc);
1005 		}
1006 		/* skip if is already OFF or IDLE, resources are off already */
1007 		else if (dpu_enc->rc_state == DPU_ENC_RC_STATE_OFF ||
1008 				dpu_enc->rc_state == DPU_ENC_RC_STATE_IDLE) {
1009 			DRM_DEBUG_KMS("id:%u, sw_event:%d, rc in %d state\n",
1010 				      DRMID(drm_enc), sw_event,
1011 				      dpu_enc->rc_state);
1012 			mutex_unlock(&dpu_enc->rc_lock);
1013 			return 0;
1014 		}
1015 
1016 		dpu_enc->rc_state = DPU_ENC_RC_STATE_PRE_OFF;
1017 
1018 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
1019 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
1020 				 "pre stop");
1021 
1022 		mutex_unlock(&dpu_enc->rc_lock);
1023 		break;
1024 
1025 	case DPU_ENC_RC_EVENT_STOP:
1026 		mutex_lock(&dpu_enc->rc_lock);
1027 
1028 		/* return if the resource control is already in OFF state */
1029 		if (dpu_enc->rc_state == DPU_ENC_RC_STATE_OFF) {
1030 			DRM_DEBUG_KMS("id: %u, sw_event:%d, rc in OFF state\n",
1031 				      DRMID(drm_enc), sw_event);
1032 			mutex_unlock(&dpu_enc->rc_lock);
1033 			return 0;
1034 		} else if (dpu_enc->rc_state == DPU_ENC_RC_STATE_ON) {
1035 			DRM_ERROR("id: %u, sw_event:%d, rc in state %d\n",
1036 				  DRMID(drm_enc), sw_event, dpu_enc->rc_state);
1037 			mutex_unlock(&dpu_enc->rc_lock);
1038 			return -EINVAL;
1039 		}
1040 
1041 		/**
1042 		 * expect to arrive here only if in either idle state or pre-off
1043 		 * and in IDLE state the resources are already disabled
1044 		 */
1045 		if (dpu_enc->rc_state == DPU_ENC_RC_STATE_PRE_OFF)
1046 			_dpu_encoder_resource_disable(drm_enc);
1047 
1048 		dpu_enc->rc_state = DPU_ENC_RC_STATE_OFF;
1049 
1050 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
1051 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
1052 				 "stop");
1053 
1054 		mutex_unlock(&dpu_enc->rc_lock);
1055 		break;
1056 
1057 	case DPU_ENC_RC_EVENT_ENTER_IDLE:
1058 		mutex_lock(&dpu_enc->rc_lock);
1059 
1060 		if (dpu_enc->rc_state != DPU_ENC_RC_STATE_ON) {
1061 			DRM_ERROR("id: %u, sw_event:%d, rc:%d !ON state\n",
1062 				  DRMID(drm_enc), sw_event, dpu_enc->rc_state);
1063 			mutex_unlock(&dpu_enc->rc_lock);
1064 			return 0;
1065 		}
1066 
1067 		/*
1068 		 * if we are in ON but a frame was just kicked off,
1069 		 * ignore the IDLE event, it's probably a stale timer event
1070 		 */
1071 		if (dpu_enc->frame_busy_mask[0]) {
1072 			DRM_ERROR("id:%u, sw_event:%d, rc:%d frame pending\n",
1073 				  DRMID(drm_enc), sw_event, dpu_enc->rc_state);
1074 			mutex_unlock(&dpu_enc->rc_lock);
1075 			return 0;
1076 		}
1077 
1078 		if (is_vid_mode)
1079 			_dpu_encoder_irq_disable(drm_enc);
1080 		else
1081 			_dpu_encoder_resource_disable(drm_enc);
1082 
1083 		dpu_enc->rc_state = DPU_ENC_RC_STATE_IDLE;
1084 
1085 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
1086 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
1087 				 "idle");
1088 
1089 		mutex_unlock(&dpu_enc->rc_lock);
1090 		break;
1091 
1092 	default:
1093 		DRM_ERROR("id:%u, unexpected sw_event: %d\n", DRMID(drm_enc),
1094 			  sw_event);
1095 		trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
1096 				 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
1097 				 "error");
1098 		break;
1099 	}
1100 
1101 	trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
1102 			 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
1103 			 "end");
1104 	return 0;
1105 }
1106 
1107 /**
1108  * dpu_encoder_prepare_wb_job - prepare writeback job for the encoder.
1109  * @drm_enc:    Pointer to previously created drm encoder structure
1110  * @job:        Pointer to the current drm writeback job
1111  */
1112 void dpu_encoder_prepare_wb_job(struct drm_encoder *drm_enc,
1113 		struct drm_writeback_job *job)
1114 {
1115 	struct dpu_encoder_virt *dpu_enc;
1116 	int i;
1117 
1118 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1119 
1120 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1121 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1122 
1123 		if (phys->ops.prepare_wb_job)
1124 			phys->ops.prepare_wb_job(phys, job);
1125 
1126 	}
1127 }
1128 
1129 /**
1130  * dpu_encoder_cleanup_wb_job - cleanup writeback job for the encoder.
1131  * @drm_enc:    Pointer to previously created drm encoder structure
1132  * @job:        Pointer to the current drm writeback job
1133  */
1134 void dpu_encoder_cleanup_wb_job(struct drm_encoder *drm_enc,
1135 		struct drm_writeback_job *job)
1136 {
1137 	struct dpu_encoder_virt *dpu_enc;
1138 	int i;
1139 
1140 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1141 
1142 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1143 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1144 
1145 		if (phys->ops.cleanup_wb_job)
1146 			phys->ops.cleanup_wb_job(phys, job);
1147 
1148 	}
1149 }
1150 
1151 static void dpu_encoder_virt_atomic_mode_set(struct drm_encoder *drm_enc,
1152 					     struct drm_crtc_state *crtc_state,
1153 					     struct drm_connector_state *conn_state)
1154 {
1155 	struct dpu_encoder_virt *dpu_enc;
1156 	struct msm_drm_private *priv;
1157 	struct dpu_kms *dpu_kms;
1158 	struct dpu_global_state *global_state;
1159 	struct dpu_hw_blk *hw_pp[MAX_CHANNELS_PER_ENC];
1160 	struct dpu_hw_blk *hw_ctl[MAX_CHANNELS_PER_ENC];
1161 	struct dpu_hw_blk *hw_dsc[MAX_CHANNELS_PER_ENC];
1162 	struct dpu_hw_blk *hw_cwb[MAX_CHANNELS_PER_ENC];
1163 	int num_ctl, num_pp, num_dsc, num_pp_per_intf;
1164 	int num_cwb = 0;
1165 	bool is_cwb_encoder;
1166 	unsigned int dsc_mask = 0;
1167 	unsigned int cwb_mask = 0;
1168 	int i;
1169 
1170 	if (!drm_enc) {
1171 		DPU_ERROR("invalid encoder\n");
1172 		return;
1173 	}
1174 
1175 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1176 	DPU_DEBUG_ENC(dpu_enc, "\n");
1177 
1178 	priv = drm_enc->dev->dev_private;
1179 	dpu_kms = to_dpu_kms(priv->kms);
1180 	is_cwb_encoder = drm_crtc_in_clone_mode(crtc_state) &&
1181 			dpu_enc->disp_info.intf_type == INTF_WB;
1182 
1183 	global_state = dpu_kms_get_existing_global_state(dpu_kms);
1184 	if (IS_ERR_OR_NULL(global_state)) {
1185 		DPU_ERROR("Failed to get global state");
1186 		return;
1187 	}
1188 
1189 	trace_dpu_enc_mode_set(DRMID(drm_enc));
1190 
1191 	/* Query resource that have been reserved in atomic check step. */
1192 	if (is_cwb_encoder) {
1193 		num_pp = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
1194 						       drm_enc->crtc,
1195 						       DPU_HW_BLK_DCWB_PINGPONG,
1196 						       hw_pp, ARRAY_SIZE(hw_pp));
1197 		num_cwb = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
1198 						       drm_enc->crtc,
1199 						       DPU_HW_BLK_CWB,
1200 						       hw_cwb, ARRAY_SIZE(hw_cwb));
1201 	} else {
1202 		num_pp = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
1203 						       drm_enc->crtc,
1204 						       DPU_HW_BLK_PINGPONG, hw_pp,
1205 						       ARRAY_SIZE(hw_pp));
1206 	}
1207 
1208 	for (i = 0; i < num_cwb; i++) {
1209 		dpu_enc->hw_cwb[i] = to_dpu_hw_cwb(hw_cwb[i]);
1210 		cwb_mask |= BIT(dpu_enc->hw_cwb[i]->idx - CWB_0);
1211 	}
1212 
1213 	dpu_enc->cwb_mask = cwb_mask;
1214 
1215 	num_ctl = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
1216 		drm_enc->crtc, DPU_HW_BLK_CTL, hw_ctl, ARRAY_SIZE(hw_ctl));
1217 
1218 	for (i = 0; i < MAX_CHANNELS_PER_ENC; i++)
1219 		dpu_enc->hw_pp[i] = i < num_pp ? to_dpu_hw_pingpong(hw_pp[i])
1220 						: NULL;
1221 
1222 	num_dsc = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
1223 						drm_enc->crtc, DPU_HW_BLK_DSC,
1224 						hw_dsc, ARRAY_SIZE(hw_dsc));
1225 	for (i = 0; i < num_dsc; i++) {
1226 		dpu_enc->hw_dsc[i] = to_dpu_hw_dsc(hw_dsc[i]);
1227 		dsc_mask |= BIT(dpu_enc->hw_dsc[i]->idx - DSC_0);
1228 	}
1229 
1230 	dpu_enc->dsc_mask = dsc_mask;
1231 
1232 	if ((dpu_enc->disp_info.intf_type == INTF_WB && conn_state->writeback_job) ||
1233 	    dpu_enc->disp_info.intf_type == INTF_DP) {
1234 		struct dpu_hw_blk *hw_cdm = NULL;
1235 
1236 		dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
1237 					      drm_enc->crtc, DPU_HW_BLK_CDM,
1238 					      &hw_cdm, 1);
1239 		dpu_enc->cur_master->hw_cdm = hw_cdm ? to_dpu_hw_cdm(hw_cdm) : NULL;
1240 	}
1241 
1242 	/*
1243 	 * There may be 4 PP and 2 INTF for quad pipe case, so INTF is not
1244 	 * mapped to PP 1:1. Let's calculate the stride with pipe/INTF
1245 	 */
1246 	num_pp_per_intf = num_pp / dpu_enc->num_phys_encs;
1247 
1248 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1249 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1250 
1251 		phys->hw_pp = dpu_enc->hw_pp[num_pp_per_intf * i];
1252 		if (!phys->hw_pp) {
1253 			DPU_ERROR_ENC(dpu_enc,
1254 				"no pp block assigned at idx: %d\n", i);
1255 			return;
1256 		}
1257 
1258 		/* Use first (and only) CTL if active CTLs are supported */
1259 		if (num_ctl == 1)
1260 			phys->hw_ctl = to_dpu_hw_ctl(hw_ctl[0]);
1261 		else
1262 			phys->hw_ctl = i < num_ctl ? to_dpu_hw_ctl(hw_ctl[i]) : NULL;
1263 		if (!phys->hw_ctl) {
1264 			DPU_ERROR_ENC(dpu_enc,
1265 				"no ctl block assigned at idx: %d\n", i);
1266 			return;
1267 		}
1268 
1269 		phys->cached_mode = crtc_state->adjusted_mode;
1270 		if (phys->ops.atomic_mode_set)
1271 			phys->ops.atomic_mode_set(phys, crtc_state, conn_state);
1272 	}
1273 }
1274 
1275 static void _dpu_encoder_virt_enable_helper(struct drm_encoder *drm_enc)
1276 {
1277 	struct dpu_encoder_virt *dpu_enc = NULL;
1278 	int i;
1279 
1280 	if (!drm_enc || !drm_enc->dev) {
1281 		DPU_ERROR("invalid parameters\n");
1282 		return;
1283 	}
1284 
1285 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1286 	if (!dpu_enc || !dpu_enc->cur_master) {
1287 		DPU_ERROR("invalid dpu encoder/master\n");
1288 		return;
1289 	}
1290 
1291 
1292 	if (dpu_enc->disp_info.intf_type == INTF_DP &&
1293 		dpu_enc->cur_master->hw_mdptop &&
1294 		dpu_enc->cur_master->hw_mdptop->ops.intf_audio_select)
1295 		dpu_enc->cur_master->hw_mdptop->ops.intf_audio_select(
1296 			dpu_enc->cur_master->hw_mdptop);
1297 
1298 	if (dpu_enc->disp_info.is_cmd_mode)
1299 		_dpu_encoder_update_vsync_source(dpu_enc, &dpu_enc->disp_info);
1300 
1301 	if (dpu_enc->disp_info.intf_type == INTF_DSI &&
1302 			!WARN_ON(dpu_enc->num_phys_encs == 0)) {
1303 		unsigned bpc = dpu_enc->connector->display_info.bpc;
1304 		for (i = 0; i < MAX_CHANNELS_PER_ENC; i++) {
1305 			if (!dpu_enc->hw_pp[i])
1306 				continue;
1307 			_dpu_encoder_setup_dither(dpu_enc->hw_pp[i], bpc);
1308 		}
1309 	}
1310 }
1311 
1312 /**
1313  * dpu_encoder_virt_runtime_resume - pm runtime resume the encoder configs
1314  * @drm_enc:	encoder pointer
1315  */
1316 void dpu_encoder_virt_runtime_resume(struct drm_encoder *drm_enc)
1317 {
1318 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1319 
1320 	mutex_lock(&dpu_enc->enc_lock);
1321 
1322 	if (!dpu_enc->enabled)
1323 		goto out;
1324 
1325 	if (dpu_enc->cur_slave && dpu_enc->cur_slave->ops.restore)
1326 		dpu_enc->cur_slave->ops.restore(dpu_enc->cur_slave);
1327 	if (dpu_enc->cur_master && dpu_enc->cur_master->ops.restore)
1328 		dpu_enc->cur_master->ops.restore(dpu_enc->cur_master);
1329 
1330 	_dpu_encoder_virt_enable_helper(drm_enc);
1331 
1332 out:
1333 	mutex_unlock(&dpu_enc->enc_lock);
1334 }
1335 
1336 static void dpu_encoder_virt_atomic_enable(struct drm_encoder *drm_enc,
1337 					struct drm_atomic_commit *state)
1338 {
1339 	struct dpu_encoder_virt *dpu_enc = NULL;
1340 	int ret = 0;
1341 	struct drm_display_mode *cur_mode = NULL;
1342 
1343 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1344 	dpu_enc->dsc = dpu_encoder_get_dsc_config(drm_enc);
1345 
1346 	atomic_set(&dpu_enc->frame_done_timeout_cnt, 0);
1347 
1348 	mutex_lock(&dpu_enc->enc_lock);
1349 
1350 	dpu_enc->commit_done_timedout = false;
1351 
1352 	dpu_enc->connector = drm_atomic_get_new_connector_for_encoder(state, drm_enc);
1353 
1354 	cur_mode = &dpu_enc->base.crtc->state->adjusted_mode;
1355 
1356 	dpu_enc->wide_bus_en = dpu_encoder_is_widebus_enabled(drm_enc);
1357 
1358 	trace_dpu_enc_enable(DRMID(drm_enc), cur_mode->hdisplay,
1359 			     cur_mode->vdisplay);
1360 
1361 	/* always enable slave encoder before master */
1362 	if (dpu_enc->cur_slave && dpu_enc->cur_slave->ops.enable)
1363 		dpu_enc->cur_slave->ops.enable(dpu_enc->cur_slave);
1364 
1365 	if (dpu_enc->cur_master && dpu_enc->cur_master->ops.enable)
1366 		dpu_enc->cur_master->ops.enable(dpu_enc->cur_master);
1367 
1368 	ret = dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_KICKOFF);
1369 	if (ret) {
1370 		DPU_ERROR_ENC(dpu_enc, "dpu resource control failed: %d\n",
1371 				ret);
1372 		goto out;
1373 	}
1374 
1375 	_dpu_encoder_virt_enable_helper(drm_enc);
1376 
1377 	dpu_enc->enabled = true;
1378 
1379 out:
1380 	mutex_unlock(&dpu_enc->enc_lock);
1381 }
1382 
1383 static void dpu_encoder_virt_atomic_disable(struct drm_encoder *drm_enc,
1384 					struct drm_atomic_commit *state)
1385 {
1386 	struct dpu_encoder_virt *dpu_enc = NULL;
1387 	struct drm_crtc *crtc;
1388 	struct drm_crtc_state *old_state = NULL;
1389 	int i = 0;
1390 
1391 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1392 	DPU_DEBUG_ENC(dpu_enc, "\n");
1393 
1394 	crtc = drm_atomic_get_old_crtc_for_encoder(state, drm_enc);
1395 	if (crtc)
1396 		old_state = drm_atomic_get_old_crtc_state(state, crtc);
1397 
1398 	/*
1399 	 * The encoder is already disabled if self refresh mode was set earlier,
1400 	 * in the old_state for the corresponding crtc.
1401 	 */
1402 	if (old_state && old_state->self_refresh_active)
1403 		return;
1404 
1405 	mutex_lock(&dpu_enc->enc_lock);
1406 	dpu_enc->enabled = false;
1407 
1408 	trace_dpu_enc_disable(DRMID(drm_enc));
1409 
1410 	/* wait for idle */
1411 	dpu_encoder_wait_for_tx_complete(drm_enc);
1412 
1413 	dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_PRE_STOP);
1414 
1415 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1416 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1417 
1418 		if (phys->ops.disable)
1419 			phys->ops.disable(phys);
1420 	}
1421 
1422 
1423 	/* after phys waits for frame-done, should be no more frames pending */
1424 	if (atomic_xchg(&dpu_enc->frame_done_timeout_ms, 0)) {
1425 		DPU_ERROR("enc%d timeout pending\n", drm_enc->base.id);
1426 		timer_delete_sync(&dpu_enc->frame_done_timer);
1427 	}
1428 
1429 	dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_STOP);
1430 
1431 	dpu_enc->connector = NULL;
1432 
1433 	DPU_DEBUG_ENC(dpu_enc, "encoder disabled\n");
1434 
1435 	mutex_unlock(&dpu_enc->enc_lock);
1436 }
1437 
1438 static struct dpu_hw_intf *dpu_encoder_get_intf(const struct dpu_mdss_cfg *catalog,
1439 		struct dpu_rm *dpu_rm,
1440 		enum dpu_intf_type type, u32 controller_id)
1441 {
1442 	int i = 0;
1443 
1444 	if (type == INTF_WB)
1445 		return NULL;
1446 
1447 	for (i = 0; i < catalog->intf_count; i++) {
1448 		if (catalog->intf[i].type == type
1449 		    && catalog->intf[i].controller_id == controller_id) {
1450 			return dpu_rm_get_intf(dpu_rm, catalog->intf[i].id);
1451 		}
1452 	}
1453 
1454 	return NULL;
1455 }
1456 
1457 /**
1458  * dpu_encoder_vblank_callback - Notify virtual encoder of vblank IRQ reception
1459  * @drm_enc:    Pointer to drm encoder structure
1460  * @phy_enc:	Pointer to physical encoder
1461  * Note: This is called from IRQ handler context.
1462  */
1463 void dpu_encoder_vblank_callback(struct drm_encoder *drm_enc,
1464 		struct dpu_encoder_phys *phy_enc)
1465 {
1466 	struct dpu_encoder_virt *dpu_enc = NULL;
1467 	unsigned long lock_flags;
1468 
1469 	if (!drm_enc || !phy_enc)
1470 		return;
1471 
1472 	DPU_ATRACE_BEGIN("encoder_vblank_callback");
1473 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1474 
1475 	atomic_inc(&phy_enc->vsync_cnt);
1476 
1477 	spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1478 	if (dpu_enc->crtc)
1479 		dpu_crtc_vblank_callback(dpu_enc->crtc);
1480 	spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1481 
1482 	DPU_ATRACE_END("encoder_vblank_callback");
1483 }
1484 
1485 /**
1486  * dpu_encoder_underrun_callback - Notify virtual encoder of underrun IRQ reception
1487  * @drm_enc:    Pointer to drm encoder structure
1488  * @phy_enc:	Pointer to physical encoder
1489  * Note: This is called from IRQ handler context.
1490  */
1491 void dpu_encoder_underrun_callback(struct drm_encoder *drm_enc,
1492 		struct dpu_encoder_phys *phy_enc)
1493 {
1494 	if (!phy_enc)
1495 		return;
1496 
1497 	DPU_ATRACE_BEGIN("encoder_underrun_callback");
1498 	atomic_inc(&phy_enc->underrun_cnt);
1499 
1500 	/* trigger dump only on the first underrun */
1501 	if (atomic_read(&phy_enc->underrun_cnt) == 1)
1502 		msm_disp_snapshot_state(drm_enc->dev);
1503 
1504 	trace_dpu_enc_underrun_cb(DRMID(drm_enc),
1505 				  atomic_read(&phy_enc->underrun_cnt));
1506 	DPU_ATRACE_END("encoder_underrun_callback");
1507 }
1508 
1509 /**
1510  * dpu_encoder_assign_crtc - Link the encoder to the crtc it's assigned to
1511  * @drm_enc:	encoder pointer
1512  * @crtc:	crtc pointer
1513  */
1514 void dpu_encoder_assign_crtc(struct drm_encoder *drm_enc, struct drm_crtc *crtc)
1515 {
1516 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1517 	unsigned long lock_flags;
1518 
1519 	spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1520 	/* crtc should always be cleared before re-assigning */
1521 	WARN_ON(crtc && dpu_enc->crtc);
1522 	dpu_enc->crtc = crtc;
1523 	spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1524 }
1525 
1526 /**
1527  * dpu_encoder_toggle_vblank_for_crtc - Toggles vblank interrupts on or off if
1528  *	the encoder is assigned to the given crtc
1529  * @drm_enc:	encoder pointer
1530  * @crtc:	crtc pointer
1531  * @enable:	true if vblank should be enabled
1532  */
1533 void dpu_encoder_toggle_vblank_for_crtc(struct drm_encoder *drm_enc,
1534 					struct drm_crtc *crtc, bool enable)
1535 {
1536 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1537 	unsigned long lock_flags;
1538 	int i;
1539 
1540 	trace_dpu_enc_vblank_cb(DRMID(drm_enc), enable);
1541 
1542 	spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1543 	if (dpu_enc->crtc != crtc) {
1544 		spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1545 		return;
1546 	}
1547 	spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1548 
1549 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1550 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1551 
1552 		if (phys->ops.control_vblank_irq)
1553 			phys->ops.control_vblank_irq(phys, enable);
1554 	}
1555 }
1556 
1557 /**
1558  * dpu_encoder_frame_done_callback - Notify virtual encoder that this phys
1559  *     encoder completes last request frame
1560  * @drm_enc:    Pointer to drm encoder structure
1561  * @ready_phys:	Pointer to physical encoder
1562  * @event:	Event to process
1563  */
1564 void dpu_encoder_frame_done_callback(
1565 		struct drm_encoder *drm_enc,
1566 		struct dpu_encoder_phys *ready_phys, u32 event)
1567 {
1568 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1569 	unsigned int i;
1570 
1571 	if (event & (DPU_ENCODER_FRAME_EVENT_DONE
1572 			| DPU_ENCODER_FRAME_EVENT_ERROR
1573 			| DPU_ENCODER_FRAME_EVENT_PANEL_DEAD)) {
1574 
1575 		if (!dpu_enc->frame_busy_mask[0]) {
1576 			/**
1577 			 * suppress frame_done without waiter,
1578 			 * likely autorefresh
1579 			 */
1580 			trace_dpu_enc_frame_done_cb_not_busy(DRMID(drm_enc), event,
1581 					dpu_encoder_helper_get_intf_type(ready_phys->intf_mode),
1582 					ready_phys->hw_intf ? ready_phys->hw_intf->idx : -1,
1583 					ready_phys->hw_wb ? ready_phys->hw_wb->idx : -1);
1584 			return;
1585 		}
1586 
1587 		/* One of the physical encoders has become idle */
1588 		for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1589 			if (dpu_enc->phys_encs[i] == ready_phys) {
1590 				trace_dpu_enc_frame_done_cb(DRMID(drm_enc), i,
1591 						dpu_enc->frame_busy_mask[0]);
1592 				clear_bit(i, dpu_enc->frame_busy_mask);
1593 			}
1594 		}
1595 
1596 		if (!dpu_enc->frame_busy_mask[0]) {
1597 			atomic_set(&dpu_enc->frame_done_timeout_ms, 0);
1598 			timer_delete(&dpu_enc->frame_done_timer);
1599 
1600 			dpu_encoder_resource_control(drm_enc,
1601 					DPU_ENC_RC_EVENT_FRAME_DONE);
1602 
1603 			if (dpu_enc->crtc)
1604 				dpu_crtc_frame_event_cb(dpu_enc->crtc, event);
1605 		}
1606 	} else {
1607 		if (dpu_enc->crtc)
1608 			dpu_crtc_frame_event_cb(dpu_enc->crtc, event);
1609 	}
1610 }
1611 
1612 static void dpu_encoder_off_work(struct work_struct *work)
1613 {
1614 	struct dpu_encoder_virt *dpu_enc = container_of(work,
1615 			struct dpu_encoder_virt, delayed_off_work.work);
1616 
1617 	dpu_encoder_resource_control(&dpu_enc->base,
1618 						DPU_ENC_RC_EVENT_ENTER_IDLE);
1619 
1620 	dpu_encoder_frame_done_callback(&dpu_enc->base, NULL,
1621 				DPU_ENCODER_FRAME_EVENT_IDLE);
1622 }
1623 
1624 /**
1625  * _dpu_encoder_trigger_flush - trigger flush for a physical encoder
1626  * @drm_enc: Pointer to drm encoder structure
1627  * @phys: Pointer to physical encoder structure
1628  * @extra_flush_bits: Additional bit mask to include in flush trigger
1629  */
1630 static void _dpu_encoder_trigger_flush(struct drm_encoder *drm_enc,
1631 		struct dpu_encoder_phys *phys, uint32_t extra_flush_bits)
1632 {
1633 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1634 	struct dpu_hw_ctl *ctl;
1635 	int pending_kickoff_cnt;
1636 	u32 ret = UINT_MAX;
1637 
1638 	if (!phys->hw_pp) {
1639 		DPU_ERROR("invalid pingpong hw\n");
1640 		return;
1641 	}
1642 
1643 	ctl = phys->hw_ctl;
1644 	if (!ctl->ops.trigger_flush) {
1645 		DPU_ERROR("missing trigger cb\n");
1646 		return;
1647 	}
1648 
1649 	pending_kickoff_cnt = dpu_encoder_phys_inc_pending(phys);
1650 
1651 	/* Return early if encoder is writeback and in clone mode */
1652 	if (drm_enc->encoder_type == DRM_MODE_ENCODER_VIRTUAL &&
1653 	    dpu_enc->cwb_mask) {
1654 		DPU_DEBUG("encoder %d skip flush for concurrent writeback encoder\n",
1655 				DRMID(drm_enc));
1656 		return;
1657 	}
1658 
1659 
1660 	if (extra_flush_bits && ctl->ops.update_pending_flush)
1661 		ctl->ops.update_pending_flush(ctl, extra_flush_bits);
1662 
1663 	ctl->ops.trigger_flush(ctl);
1664 
1665 	if (ctl->ops.get_pending_flush)
1666 		ret = ctl->ops.get_pending_flush(ctl);
1667 
1668 	trace_dpu_enc_trigger_flush(DRMID(drm_enc),
1669 			dpu_encoder_helper_get_intf_type(phys->intf_mode),
1670 			phys->hw_intf ? phys->hw_intf->idx : -1,
1671 			phys->hw_wb ? phys->hw_wb->idx : -1,
1672 			pending_kickoff_cnt, ctl->idx,
1673 			extra_flush_bits, ret);
1674 }
1675 
1676 /**
1677  * _dpu_encoder_trigger_start - trigger start for a physical encoder
1678  * @phys: Pointer to physical encoder structure
1679  */
1680 static void _dpu_encoder_trigger_start(struct dpu_encoder_phys *phys)
1681 {
1682 	struct dpu_encoder_virt *dpu_enc;
1683 
1684 	if (!phys) {
1685 		DPU_ERROR("invalid argument(s)\n");
1686 		return;
1687 	}
1688 
1689 	if (!phys->hw_pp) {
1690 		DPU_ERROR("invalid pingpong hw\n");
1691 		return;
1692 	}
1693 
1694 	dpu_enc = to_dpu_encoder_virt(phys->parent);
1695 
1696 	if (phys->parent->encoder_type == DRM_MODE_ENCODER_VIRTUAL &&
1697 	    dpu_enc->cwb_mask) {
1698 		DPU_DEBUG("encoder %d CWB enabled, skipping\n", DRMID(phys->parent));
1699 		return;
1700 	}
1701 
1702 	if (phys->ops.trigger_start && phys->enable_state != DPU_ENC_DISABLED)
1703 		phys->ops.trigger_start(phys);
1704 }
1705 
1706 /**
1707  * dpu_encoder_helper_trigger_start - control start helper function
1708  *	This helper function may be optionally specified by physical
1709  *	encoders if they require ctl_start triggering.
1710  * @phys_enc: Pointer to physical encoder structure
1711  */
1712 void dpu_encoder_helper_trigger_start(struct dpu_encoder_phys *phys_enc)
1713 {
1714 	struct dpu_hw_ctl *ctl;
1715 
1716 	ctl = phys_enc->hw_ctl;
1717 	if (ctl->ops.trigger_start) {
1718 		ctl->ops.trigger_start(ctl);
1719 		trace_dpu_enc_trigger_start(DRMID(phys_enc->parent), ctl->idx);
1720 	}
1721 }
1722 
1723 static int dpu_encoder_helper_wait_event_timeout(
1724 		int32_t drm_id,
1725 		unsigned int irq_idx,
1726 		struct dpu_encoder_wait_info *info)
1727 {
1728 	int rc = 0;
1729 	s64 expected_time = ktime_to_ms(ktime_get()) + info->timeout_ms;
1730 	s64 jiffies = msecs_to_jiffies(info->timeout_ms);
1731 	s64 time;
1732 
1733 	do {
1734 		rc = wait_event_timeout(*(info->wq),
1735 				atomic_read(info->atomic_cnt) == 0, jiffies);
1736 		time = ktime_to_ms(ktime_get());
1737 
1738 		trace_dpu_enc_wait_event_timeout(drm_id,
1739 						 DPU_IRQ_REG(irq_idx), DPU_IRQ_BIT(irq_idx),
1740 						 rc, time,
1741 						 expected_time,
1742 						 atomic_read(info->atomic_cnt));
1743 	/* If we timed out, counter is valid and time is less, wait again */
1744 	} while (atomic_read(info->atomic_cnt) && (rc == 0) &&
1745 			(time < expected_time));
1746 
1747 	return rc;
1748 }
1749 
1750 static void dpu_encoder_helper_hw_reset(struct dpu_encoder_phys *phys_enc)
1751 {
1752 	struct dpu_encoder_virt *dpu_enc;
1753 	struct dpu_hw_ctl *ctl;
1754 	int rc;
1755 	struct drm_encoder *drm_enc;
1756 
1757 	dpu_enc = to_dpu_encoder_virt(phys_enc->parent);
1758 	ctl = phys_enc->hw_ctl;
1759 	drm_enc = phys_enc->parent;
1760 
1761 	if (!ctl->ops.reset)
1762 		return;
1763 
1764 	DRM_DEBUG_KMS("id:%u ctl %d reset\n", DRMID(drm_enc),
1765 		      ctl->idx);
1766 
1767 	rc = ctl->ops.reset(ctl);
1768 	if (rc) {
1769 		DPU_ERROR_ENC(dpu_enc, "ctl %d reset failure\n",  ctl->idx);
1770 		msm_disp_snapshot_state(drm_enc->dev);
1771 	}
1772 
1773 	phys_enc->enable_state = DPU_ENC_ENABLED;
1774 }
1775 
1776 /**
1777  * _dpu_encoder_kickoff_phys - handle physical encoder kickoff
1778  *	Iterate through the physical encoders and perform consolidated flush
1779  *	and/or control start triggering as needed. This is done in the virtual
1780  *	encoder rather than the individual physical ones in order to handle
1781  *	use cases that require visibility into multiple physical encoders at
1782  *	a time.
1783  * @dpu_enc: Pointer to virtual encoder structure
1784  */
1785 static void _dpu_encoder_kickoff_phys(struct dpu_encoder_virt *dpu_enc)
1786 {
1787 	struct dpu_hw_ctl *ctl;
1788 	uint32_t i, pending_flush;
1789 	unsigned long lock_flags;
1790 
1791 	pending_flush = 0x0;
1792 
1793 	/* update pending counts and trigger kickoff ctl flush atomically */
1794 	spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1795 
1796 	/* don't perform flush/start operations for slave encoders */
1797 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1798 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1799 
1800 		if (phys->enable_state == DPU_ENC_DISABLED)
1801 			continue;
1802 
1803 		ctl = phys->hw_ctl;
1804 
1805 		/*
1806 		 * This is cleared in frame_done worker, which isn't invoked
1807 		 * for async commits. So don't set this for async, since it'll
1808 		 * roll over to the next commit.
1809 		 */
1810 		if (phys->split_role != ENC_ROLE_SLAVE)
1811 			set_bit(i, dpu_enc->frame_busy_mask);
1812 
1813 		if (!phys->ops.needs_single_flush ||
1814 				!phys->ops.needs_single_flush(phys))
1815 			_dpu_encoder_trigger_flush(&dpu_enc->base, phys, 0x0);
1816 		else if (ctl->ops.get_pending_flush)
1817 			pending_flush |= ctl->ops.get_pending_flush(ctl);
1818 	}
1819 
1820 	/* for split flush, combine pending flush masks and send to master */
1821 	if (pending_flush && dpu_enc->cur_master) {
1822 		_dpu_encoder_trigger_flush(
1823 				&dpu_enc->base,
1824 				dpu_enc->cur_master,
1825 				pending_flush);
1826 	}
1827 
1828 	_dpu_encoder_trigger_start(dpu_enc->cur_master);
1829 
1830 	spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1831 }
1832 
1833 /**
1834  * dpu_encoder_trigger_kickoff_pending - Clear the flush bits from previous
1835  *        kickoff and trigger the ctl prepare progress for command mode display.
1836  * @drm_enc:	encoder pointer
1837  */
1838 void dpu_encoder_trigger_kickoff_pending(struct drm_encoder *drm_enc)
1839 {
1840 	struct dpu_encoder_virt *dpu_enc;
1841 	struct dpu_encoder_phys *phys;
1842 	unsigned int i;
1843 	struct dpu_hw_ctl *ctl;
1844 	struct msm_display_info *disp_info;
1845 
1846 	if (!drm_enc) {
1847 		DPU_ERROR("invalid encoder\n");
1848 		return;
1849 	}
1850 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1851 	disp_info = &dpu_enc->disp_info;
1852 
1853 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1854 		phys = dpu_enc->phys_encs[i];
1855 
1856 		ctl = phys->hw_ctl;
1857 		ctl->ops.clear_pending_flush(ctl);
1858 
1859 		/* update only for command mode primary ctl */
1860 		if ((phys == dpu_enc->cur_master) &&
1861 		    disp_info->is_cmd_mode
1862 		    && ctl->ops.trigger_pending)
1863 			ctl->ops.trigger_pending(ctl);
1864 	}
1865 }
1866 
1867 static u32 _dpu_encoder_calculate_linetime(struct dpu_encoder_virt *dpu_enc,
1868 		struct drm_display_mode *mode)
1869 {
1870 	u64 pclk_rate;
1871 	u32 pclk_period;
1872 	u32 line_time;
1873 
1874 	/*
1875 	 * For linetime calculation, only operate on master encoder.
1876 	 */
1877 	if (!dpu_enc->cur_master)
1878 		return 0;
1879 
1880 	if (!dpu_enc->cur_master->ops.get_line_count) {
1881 		DPU_ERROR("get_line_count function not defined\n");
1882 		return 0;
1883 	}
1884 
1885 	pclk_rate = mode->clock; /* pixel clock in kHz */
1886 	if (pclk_rate == 0) {
1887 		DPU_ERROR("pclk is 0, cannot calculate line time\n");
1888 		return 0;
1889 	}
1890 
1891 	pclk_period = DIV_ROUND_UP_ULL(1000000000ull, pclk_rate);
1892 	if (pclk_period == 0) {
1893 		DPU_ERROR("pclk period is 0\n");
1894 		return 0;
1895 	}
1896 
1897 	/*
1898 	 * Line time calculation based on Pixel clock and HTOTAL.
1899 	 * Final unit is in ns.
1900 	 */
1901 	line_time = (pclk_period * mode->htotal) / 1000;
1902 	if (line_time == 0) {
1903 		DPU_ERROR("line time calculation is 0\n");
1904 		return 0;
1905 	}
1906 
1907 	DPU_DEBUG_ENC(dpu_enc,
1908 			"clk_rate=%lldkHz, clk_period=%d, linetime=%dns\n",
1909 			pclk_rate, pclk_period, line_time);
1910 
1911 	return line_time;
1912 }
1913 
1914 /**
1915  * dpu_encoder_vsync_time - get the time of the next vsync
1916  * @drm_enc:	encoder pointer
1917  * @wakeup_time: pointer to ktime_t to write the vsync time to
1918  */
1919 int dpu_encoder_vsync_time(struct drm_encoder *drm_enc, ktime_t *wakeup_time)
1920 {
1921 	struct drm_display_mode *mode;
1922 	struct dpu_encoder_virt *dpu_enc;
1923 	u32 cur_line;
1924 	u32 line_time;
1925 	u32 vtotal, time_to_vsync;
1926 	ktime_t cur_time;
1927 
1928 	dpu_enc = to_dpu_encoder_virt(drm_enc);
1929 
1930 	if (!drm_enc->crtc || !drm_enc->crtc->state) {
1931 		DPU_ERROR("crtc/crtc state object is NULL\n");
1932 		return -EINVAL;
1933 	}
1934 	mode = &drm_enc->crtc->state->adjusted_mode;
1935 
1936 	line_time = _dpu_encoder_calculate_linetime(dpu_enc, mode);
1937 	if (!line_time)
1938 		return -EINVAL;
1939 
1940 	cur_line = dpu_enc->cur_master->ops.get_line_count(dpu_enc->cur_master);
1941 
1942 	vtotal = mode->vtotal;
1943 	if (cur_line >= vtotal)
1944 		time_to_vsync = line_time * vtotal;
1945 	else
1946 		time_to_vsync = line_time * (vtotal - cur_line);
1947 
1948 	if (time_to_vsync == 0) {
1949 		DPU_ERROR("time to vsync should not be zero, vtotal=%d\n",
1950 				vtotal);
1951 		return -EINVAL;
1952 	}
1953 
1954 	cur_time = ktime_get();
1955 	*wakeup_time = ktime_add_ns(cur_time, time_to_vsync);
1956 
1957 	DPU_DEBUG_ENC(dpu_enc,
1958 			"cur_line=%u vtotal=%u time_to_vsync=%u, cur_time=%lld, wakeup_time=%lld\n",
1959 			cur_line, vtotal, time_to_vsync,
1960 			ktime_to_ms(cur_time),
1961 			ktime_to_ms(*wakeup_time));
1962 	return 0;
1963 }
1964 
1965 static u32
1966 dpu_encoder_dsc_initial_line_calc(struct drm_dsc_config *dsc,
1967 				  u32 enc_ip_width)
1968 {
1969 	int ssm_delay, total_pixels, soft_slice_per_enc;
1970 
1971 	soft_slice_per_enc = enc_ip_width / dsc->slice_width;
1972 
1973 	/*
1974 	 * minimum number of initial line pixels is a sum of:
1975 	 * 1. sub-stream multiplexer delay (83 groups for 8bpc,
1976 	 *    91 for 10 bpc) * 3
1977 	 * 2. for two soft slice cases, add extra sub-stream multiplexer * 3
1978 	 * 3. the initial xmit delay
1979 	 * 4. total pipeline delay through the "lock step" of encoder (47)
1980 	 * 5. 6 additional pixels as the output of the rate buffer is
1981 	 *    48 bits wide
1982 	 */
1983 	ssm_delay = ((dsc->bits_per_component < 10) ? 84 : 92);
1984 	total_pixels = ssm_delay * 3 + dsc->initial_xmit_delay + 47;
1985 	if (soft_slice_per_enc > 1)
1986 		total_pixels += (ssm_delay * 3);
1987 	return DIV_ROUND_UP(total_pixels, dsc->slice_width);
1988 }
1989 
1990 static void dpu_encoder_dsc_pipe_cfg(struct dpu_hw_ctl *ctl,
1991 				     struct dpu_hw_dsc *hw_dsc,
1992 				     struct dpu_hw_pingpong *hw_pp,
1993 				     struct drm_dsc_config *dsc,
1994 				     u32 common_mode,
1995 				     u32 initial_lines)
1996 {
1997 	if (hw_dsc->ops.dsc_config)
1998 		hw_dsc->ops.dsc_config(hw_dsc, dsc, common_mode, initial_lines);
1999 
2000 	if (hw_dsc->ops.dsc_config_thresh)
2001 		hw_dsc->ops.dsc_config_thresh(hw_dsc, dsc);
2002 
2003 	if (hw_pp->ops.setup_dsc)
2004 		hw_pp->ops.setup_dsc(hw_pp);
2005 
2006 	if (hw_dsc->ops.dsc_bind_pingpong_blk)
2007 		hw_dsc->ops.dsc_bind_pingpong_blk(hw_dsc, hw_pp->idx);
2008 
2009 	if (hw_pp->ops.enable_dsc)
2010 		hw_pp->ops.enable_dsc(hw_pp);
2011 
2012 	if (ctl->ops.update_pending_flush_dsc)
2013 		ctl->ops.update_pending_flush_dsc(ctl, hw_dsc->idx);
2014 }
2015 
2016 static void dpu_encoder_prep_dsc(struct dpu_encoder_virt *dpu_enc,
2017 				 struct drm_dsc_config *dsc)
2018 {
2019 	struct dpu_encoder_phys *enc_master = dpu_enc->cur_master;
2020 	struct dpu_hw_ctl *ctl = enc_master->hw_ctl;
2021 	struct dpu_hw_dsc *hw_dsc[MAX_CHANNELS_PER_ENC];
2022 	struct dpu_hw_pingpong *hw_pp[MAX_CHANNELS_PER_ENC];
2023 	int this_frame_slices;
2024 	int intf_ip_w, enc_ip_w;
2025 	int dsc_common_mode;
2026 	int pic_width;
2027 	u32 initial_lines;
2028 	int num_dsc = 0;
2029 	int i;
2030 
2031 	for (i = 0; i < MAX_CHANNELS_PER_ENC; i++) {
2032 		hw_pp[i] = dpu_enc->hw_pp[i];
2033 		hw_dsc[i] = dpu_enc->hw_dsc[i];
2034 
2035 		if (!hw_pp[i] || !hw_dsc[i])
2036 			break;
2037 
2038 		num_dsc++;
2039 	}
2040 
2041 	pic_width = dsc->pic_width;
2042 
2043 	dsc_common_mode = 0;
2044 	if (num_dsc > 1)
2045 		dsc_common_mode |= DSC_MODE_SPLIT_PANEL;
2046 	if (dpu_encoder_use_dsc_merge(enc_master->parent))
2047 		dsc_common_mode |= DSC_MODE_MULTIPLEX;
2048 	if (enc_master->intf_mode == INTF_MODE_VIDEO)
2049 		dsc_common_mode |= DSC_MODE_VIDEO;
2050 
2051 	this_frame_slices = pic_width / dsc->slice_width;
2052 	intf_ip_w = this_frame_slices * dsc->slice_width;
2053 
2054 	enc_ip_w = intf_ip_w / num_dsc;
2055 	initial_lines = dpu_encoder_dsc_initial_line_calc(dsc, enc_ip_w);
2056 
2057 	for (i = 0; i < num_dsc; i++)
2058 		dpu_encoder_dsc_pipe_cfg(ctl, hw_dsc[i], hw_pp[i],
2059 					 dsc, dsc_common_mode, initial_lines);
2060 }
2061 
2062 /**
2063  * dpu_encoder_prepare_for_kickoff - schedule double buffer flip of the ctl
2064  *	path (i.e. ctl flush and start) at next appropriate time.
2065  *	Immediately: if no previous commit is outstanding.
2066  *	Delayed: Block until next trigger can be issued.
2067  * @drm_enc:	encoder pointer
2068  */
2069 void dpu_encoder_prepare_for_kickoff(struct drm_encoder *drm_enc)
2070 {
2071 	struct dpu_encoder_virt *dpu_enc;
2072 	struct dpu_encoder_phys *phys;
2073 	bool needs_hw_reset = false;
2074 	unsigned int i;
2075 
2076 	dpu_enc = to_dpu_encoder_virt(drm_enc);
2077 
2078 	trace_dpu_enc_prepare_kickoff(DRMID(drm_enc));
2079 
2080 	/* prepare for next kickoff, may include waiting on previous kickoff */
2081 	DPU_ATRACE_BEGIN("enc_prepare_for_kickoff");
2082 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2083 		phys = dpu_enc->phys_encs[i];
2084 		if (phys->ops.prepare_for_kickoff)
2085 			phys->ops.prepare_for_kickoff(phys);
2086 		if (phys->enable_state == DPU_ENC_ERR_NEEDS_HW_RESET)
2087 			needs_hw_reset = true;
2088 	}
2089 	DPU_ATRACE_END("enc_prepare_for_kickoff");
2090 
2091 	dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_KICKOFF);
2092 
2093 	/* if any phys needs reset, reset all phys, in-order */
2094 	if (needs_hw_reset) {
2095 		trace_dpu_enc_prepare_kickoff_reset(DRMID(drm_enc));
2096 		for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2097 			dpu_encoder_helper_hw_reset(dpu_enc->phys_encs[i]);
2098 		}
2099 	}
2100 
2101 	if (dpu_enc->dsc)
2102 		dpu_encoder_prep_dsc(dpu_enc, dpu_enc->dsc);
2103 }
2104 
2105 /**
2106  * dpu_encoder_is_valid_for_commit - check if encode has valid parameters for commit.
2107  * @drm_enc:    Pointer to drm encoder structure
2108  */
2109 bool dpu_encoder_is_valid_for_commit(struct drm_encoder *drm_enc)
2110 {
2111 	struct dpu_encoder_virt *dpu_enc;
2112 	unsigned int i;
2113 	struct dpu_encoder_phys *phys;
2114 
2115 	dpu_enc = to_dpu_encoder_virt(drm_enc);
2116 
2117 	if (drm_enc->encoder_type == DRM_MODE_ENCODER_VIRTUAL) {
2118 		for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2119 			phys = dpu_enc->phys_encs[i];
2120 			if (phys->ops.is_valid_for_commit && !phys->ops.is_valid_for_commit(phys)) {
2121 				DPU_DEBUG("invalid FB not kicking off\n");
2122 				return false;
2123 			}
2124 		}
2125 	}
2126 
2127 	return true;
2128 }
2129 
2130 /**
2131  * dpu_encoder_start_frame_done_timer - Start the encoder frame done timer
2132  * @drm_enc: Pointer to drm encoder structure
2133  */
2134 void dpu_encoder_start_frame_done_timer(struct drm_encoder *drm_enc)
2135 {
2136 	struct dpu_encoder_virt *dpu_enc;
2137 	unsigned long timeout_ms;
2138 
2139 	dpu_enc = to_dpu_encoder_virt(drm_enc);
2140 	timeout_ms = DPU_ENCODER_FRAME_DONE_TIMEOUT_FRAMES * 1000 /
2141 			drm_mode_vrefresh(&drm_enc->crtc->state->adjusted_mode);
2142 
2143 	atomic_set(&dpu_enc->frame_done_timeout_ms, timeout_ms);
2144 	mod_timer(&dpu_enc->frame_done_timer,
2145 			jiffies + msecs_to_jiffies(timeout_ms));
2146 
2147 }
2148 
2149 /**
2150  * dpu_encoder_kickoff - trigger a double buffer flip of the ctl path
2151  *	(i.e. ctl flush and start) immediately.
2152  * @drm_enc:	encoder pointer
2153  */
2154 void dpu_encoder_kickoff(struct drm_encoder *drm_enc)
2155 {
2156 	struct dpu_encoder_virt *dpu_enc;
2157 	struct dpu_encoder_phys *phys;
2158 	unsigned int i;
2159 
2160 	DPU_ATRACE_BEGIN("encoder_kickoff");
2161 	dpu_enc = to_dpu_encoder_virt(drm_enc);
2162 
2163 	trace_dpu_enc_kickoff(DRMID(drm_enc));
2164 
2165 	/* All phys encs are ready to go, trigger the kickoff */
2166 	_dpu_encoder_kickoff_phys(dpu_enc);
2167 
2168 	/* allow phys encs to handle any post-kickoff business */
2169 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2170 		phys = dpu_enc->phys_encs[i];
2171 		if (phys->ops.handle_post_kickoff)
2172 			phys->ops.handle_post_kickoff(phys);
2173 	}
2174 
2175 	DPU_ATRACE_END("encoder_kickoff");
2176 }
2177 
2178 static void dpu_encoder_helper_reset_mixers(struct dpu_encoder_phys *phys_enc)
2179 {
2180 	int i, num_lm;
2181 	struct dpu_global_state *global_state;
2182 	struct dpu_hw_blk *hw_lm[2];
2183 	struct dpu_hw_mixer *hw_mixer[2];
2184 	struct dpu_hw_ctl *ctl = phys_enc->hw_ctl;
2185 
2186 	/* reset all mixers for this encoder */
2187 	if (ctl->ops.clear_all_blendstages)
2188 		ctl->ops.clear_all_blendstages(ctl);
2189 
2190 	global_state = dpu_kms_get_existing_global_state(phys_enc->dpu_kms);
2191 
2192 	num_lm = dpu_rm_get_assigned_resources(&phys_enc->dpu_kms->rm, global_state,
2193 		phys_enc->parent->crtc, DPU_HW_BLK_LM, hw_lm, ARRAY_SIZE(hw_lm));
2194 
2195 	for (i = 0; i < num_lm; i++) {
2196 		hw_mixer[i] = to_dpu_hw_mixer(hw_lm[i]);
2197 		if (ctl->ops.update_pending_flush_mixer)
2198 			ctl->ops.update_pending_flush_mixer(ctl, hw_mixer[i]->idx);
2199 
2200 		/* clear all blendstages */
2201 		if (ctl->ops.setup_blendstage)
2202 			ctl->ops.setup_blendstage(ctl, hw_mixer[i]->idx, NULL);
2203 
2204 		if (hw_mixer[i]->ops.clear_all_blendstages)
2205 			hw_mixer[i]->ops.clear_all_blendstages(hw_mixer[i]);
2206 
2207 		if (ctl->ops.set_active_lms)
2208 			ctl->ops.set_active_lms(ctl, NULL);
2209 
2210 		if (ctl->ops.set_active_fetch_pipes)
2211 			ctl->ops.set_active_fetch_pipes(ctl, NULL);
2212 
2213 		if (ctl->ops.set_active_pipes)
2214 			ctl->ops.set_active_pipes(ctl, NULL);
2215 	}
2216 }
2217 
2218 static void dpu_encoder_dsc_pipe_clr(struct dpu_hw_ctl *ctl,
2219 				     struct dpu_hw_dsc *hw_dsc,
2220 				     struct dpu_hw_pingpong *hw_pp)
2221 {
2222 	if (hw_dsc->ops.dsc_disable)
2223 		hw_dsc->ops.dsc_disable(hw_dsc);
2224 
2225 	if (hw_pp->ops.disable_dsc)
2226 		hw_pp->ops.disable_dsc(hw_pp);
2227 
2228 	if (hw_dsc->ops.dsc_bind_pingpong_blk)
2229 		hw_dsc->ops.dsc_bind_pingpong_blk(hw_dsc, PINGPONG_NONE);
2230 
2231 	if (ctl->ops.update_pending_flush_dsc)
2232 		ctl->ops.update_pending_flush_dsc(ctl, hw_dsc->idx);
2233 }
2234 
2235 static void dpu_encoder_unprep_dsc(struct dpu_encoder_virt *dpu_enc)
2236 {
2237 	/* coding only for 2LM, 2enc, 1 dsc config */
2238 	struct dpu_encoder_phys *enc_master = dpu_enc->cur_master;
2239 	struct dpu_hw_ctl *ctl = enc_master->hw_ctl;
2240 	struct dpu_hw_dsc *hw_dsc[MAX_CHANNELS_PER_ENC];
2241 	struct dpu_hw_pingpong *hw_pp[MAX_CHANNELS_PER_ENC];
2242 	int i;
2243 
2244 	for (i = 0; i < MAX_CHANNELS_PER_ENC; i++) {
2245 		hw_pp[i] = dpu_enc->hw_pp[i];
2246 		hw_dsc[i] = dpu_enc->hw_dsc[i];
2247 
2248 		if (hw_pp[i] && hw_dsc[i])
2249 			dpu_encoder_dsc_pipe_clr(ctl, hw_dsc[i], hw_pp[i]);
2250 	}
2251 }
2252 
2253 /**
2254  * dpu_encoder_helper_phys_cleanup - helper to cleanup dpu pipeline
2255  * @phys_enc: Pointer to physical encoder structure
2256  */
2257 void dpu_encoder_helper_phys_cleanup(struct dpu_encoder_phys *phys_enc)
2258 {
2259 	struct dpu_hw_ctl *ctl = phys_enc->hw_ctl;
2260 	struct dpu_hw_intf_cfg intf_cfg = { 0 };
2261 	int i;
2262 	struct dpu_encoder_virt *dpu_enc;
2263 
2264 	dpu_enc = to_dpu_encoder_virt(phys_enc->parent);
2265 
2266 	ctl->ops.reset(ctl);
2267 
2268 	dpu_encoder_helper_reset_mixers(phys_enc);
2269 
2270 	/*
2271 	 * TODO: move the once-only operation like CTL flush/trigger
2272 	 * into dpu_encoder_virt_disable() and all operations which need
2273 	 * to be done per phys encoder into the phys_disable() op.
2274 	 */
2275 	if (phys_enc->hw_wb) {
2276 		/* disable the PP block */
2277 		if (phys_enc->hw_wb->ops.bind_pingpong_blk)
2278 			phys_enc->hw_wb->ops.bind_pingpong_blk(phys_enc->hw_wb, PINGPONG_NONE);
2279 
2280 		/* mark WB flush as pending */
2281 		if (ctl->ops.update_pending_flush_wb)
2282 			ctl->ops.update_pending_flush_wb(ctl, phys_enc->hw_wb->idx);
2283 	} else {
2284 		for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2285 			if (dpu_enc->phys_encs[i] && phys_enc->hw_intf->ops.bind_pingpong_blk)
2286 				phys_enc->hw_intf->ops.bind_pingpong_blk(
2287 						dpu_enc->phys_encs[i]->hw_intf,
2288 						PINGPONG_NONE);
2289 
2290 			/* mark INTF flush as pending */
2291 			if (ctl->ops.update_pending_flush_intf)
2292 				ctl->ops.update_pending_flush_intf(ctl,
2293 						dpu_enc->phys_encs[i]->hw_intf->idx);
2294 		}
2295 	}
2296 
2297 	if (phys_enc->hw_pp && phys_enc->hw_pp->ops.setup_dither)
2298 		phys_enc->hw_pp->ops.setup_dither(phys_enc->hw_pp, NULL);
2299 
2300 	if (dpu_enc->cwb_mask)
2301 		dpu_encoder_helper_phys_setup_cwb(phys_enc, false);
2302 
2303 	/* reset the merge 3D HW block */
2304 	if (phys_enc->hw_pp && phys_enc->hw_pp->merge_3d) {
2305 		phys_enc->hw_pp->merge_3d->ops.setup_3d_mode(phys_enc->hw_pp->merge_3d,
2306 				BLEND_3D_NONE);
2307 		if (ctl->ops.update_pending_flush_merge_3d)
2308 			ctl->ops.update_pending_flush_merge_3d(ctl,
2309 					phys_enc->hw_pp->merge_3d->idx);
2310 	}
2311 
2312 	if (phys_enc->hw_cdm) {
2313 		if (phys_enc->hw_cdm->ops.bind_pingpong_blk && phys_enc->hw_pp)
2314 			phys_enc->hw_cdm->ops.bind_pingpong_blk(phys_enc->hw_cdm,
2315 								PINGPONG_NONE);
2316 		if (ctl->ops.update_pending_flush_cdm)
2317 			ctl->ops.update_pending_flush_cdm(ctl,
2318 							  phys_enc->hw_cdm->idx);
2319 	}
2320 
2321 	if (dpu_enc->dsc) {
2322 		dpu_encoder_unprep_dsc(dpu_enc);
2323 		dpu_enc->dsc = NULL;
2324 	}
2325 
2326 	intf_cfg.stream_sel = 0; /* Don't care value for video mode */
2327 	intf_cfg.mode_3d = dpu_encoder_helper_get_3d_blend_mode(phys_enc);
2328 	intf_cfg.dsc = dpu_encoder_helper_get_dsc(phys_enc);
2329 	intf_cfg.cwb = dpu_enc->cwb_mask;
2330 
2331 	if (phys_enc->hw_intf)
2332 		intf_cfg.intf = phys_enc->hw_intf->idx;
2333 	if (phys_enc->hw_wb)
2334 		intf_cfg.wb = phys_enc->hw_wb->idx;
2335 
2336 	if (phys_enc->hw_pp && phys_enc->hw_pp->merge_3d)
2337 		intf_cfg.merge_3d = phys_enc->hw_pp->merge_3d->idx;
2338 
2339 	if (ctl->ops.reset_intf_cfg)
2340 		ctl->ops.reset_intf_cfg(ctl, &intf_cfg);
2341 
2342 	ctl->ops.trigger_flush(ctl);
2343 	ctl->ops.trigger_start(ctl);
2344 	ctl->ops.clear_pending_flush(ctl);
2345 }
2346 
2347 void dpu_encoder_helper_phys_setup_cwb(struct dpu_encoder_phys *phys_enc,
2348 				       bool enable)
2349 {
2350 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(phys_enc->parent);
2351 	struct dpu_hw_cwb *hw_cwb;
2352 	struct dpu_hw_ctl *hw_ctl;
2353 	struct dpu_hw_cwb_setup_cfg cwb_cfg;
2354 
2355 	struct dpu_kms *dpu_kms;
2356 	struct dpu_global_state *global_state;
2357 	struct dpu_hw_blk *rt_pp_list[MAX_CHANNELS_PER_ENC];
2358 	int num_pp;
2359 
2360 	if (!phys_enc->hw_wb)
2361 		return;
2362 
2363 	hw_ctl = phys_enc->hw_ctl;
2364 
2365 	if (!phys_enc->hw_ctl) {
2366 		DPU_DEBUG("[wb:%d] no ctl assigned\n",
2367 			  phys_enc->hw_wb->idx - WB_0);
2368 		return;
2369 	}
2370 
2371 	dpu_kms = phys_enc->dpu_kms;
2372 	global_state = dpu_kms_get_existing_global_state(dpu_kms);
2373 	num_pp = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
2374 					       phys_enc->parent->crtc,
2375 					       DPU_HW_BLK_PINGPONG, rt_pp_list,
2376 					       ARRAY_SIZE(rt_pp_list));
2377 
2378 	if (num_pp == 0 || num_pp > MAX_CHANNELS_PER_ENC) {
2379 		DPU_DEBUG_ENC(dpu_enc, "invalid num_pp %d\n", num_pp);
2380 		return;
2381 	}
2382 
2383 	/*
2384 	 * The CWB mux supports using LM or DSPP as tap points. For now,
2385 	 * always use LM tap point
2386 	 */
2387 	cwb_cfg.input = INPUT_MODE_LM_OUT;
2388 
2389 	for (int i = 0; i < MAX_CWB_PER_ENC; i++) {
2390 		hw_cwb = dpu_enc->hw_cwb[i];
2391 		if (!hw_cwb)
2392 			continue;
2393 
2394 		if (enable) {
2395 			struct dpu_hw_pingpong *hw_pp =
2396 					to_dpu_hw_pingpong(rt_pp_list[i]);
2397 			cwb_cfg.pp_idx = hw_pp->idx;
2398 		} else {
2399 			cwb_cfg.pp_idx = PINGPONG_NONE;
2400 		}
2401 
2402 		hw_cwb->ops.config_cwb(hw_cwb, &cwb_cfg);
2403 
2404 		if (hw_ctl->ops.update_pending_flush_cwb)
2405 			hw_ctl->ops.update_pending_flush_cwb(hw_ctl, hw_cwb->idx);
2406 	}
2407 }
2408 
2409 /**
2410  * dpu_encoder_helper_phys_setup_cdm - setup chroma down sampling block
2411  * @phys_enc: Pointer to physical encoder
2412  * @dpu_fmt: Pinter to the format description
2413  * @output_type: HDMI/WB
2414  */
2415 void dpu_encoder_helper_phys_setup_cdm(struct dpu_encoder_phys *phys_enc,
2416 				       const struct msm_format *dpu_fmt,
2417 				       u32 output_type)
2418 {
2419 	struct dpu_hw_cdm *hw_cdm;
2420 	struct dpu_hw_cdm_cfg *cdm_cfg;
2421 	struct dpu_hw_pingpong *hw_pp;
2422 	int ret;
2423 
2424 	if (!phys_enc)
2425 		return;
2426 
2427 	cdm_cfg = &phys_enc->cdm_cfg;
2428 	hw_pp = phys_enc->hw_pp;
2429 	hw_cdm = phys_enc->hw_cdm;
2430 
2431 	if (!hw_cdm)
2432 		return;
2433 
2434 	if (!MSM_FORMAT_IS_YUV(dpu_fmt)) {
2435 		DPU_DEBUG("[enc:%d] cdm_disable fmt:%p4cc\n", DRMID(phys_enc->parent),
2436 			  &dpu_fmt->pixel_format);
2437 		if (hw_cdm->ops.bind_pingpong_blk)
2438 			hw_cdm->ops.bind_pingpong_blk(hw_cdm, PINGPONG_NONE);
2439 
2440 		return;
2441 	}
2442 
2443 	memset(cdm_cfg, 0, sizeof(struct dpu_hw_cdm_cfg));
2444 
2445 	cdm_cfg->output_width = phys_enc->cached_mode.hdisplay;
2446 	cdm_cfg->output_height = phys_enc->cached_mode.vdisplay;
2447 	cdm_cfg->output_fmt = dpu_fmt;
2448 	cdm_cfg->output_type = output_type;
2449 	cdm_cfg->output_bit_depth = MSM_FORMAT_IS_DX(dpu_fmt) ?
2450 			CDM_CDWN_OUTPUT_10BIT : CDM_CDWN_OUTPUT_8BIT;
2451 	cdm_cfg->csc_cfg = &dpu_csc10_rgb2yuv_601l;
2452 
2453 	/* enable 10 bit logic */
2454 	switch (cdm_cfg->output_fmt->chroma_sample) {
2455 	case CHROMA_FULL:
2456 		cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
2457 		cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
2458 		break;
2459 	case CHROMA_H2V1:
2460 		cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
2461 		cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
2462 		break;
2463 	case CHROMA_420:
2464 		cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
2465 		cdm_cfg->v_cdwn_type = CDM_CDWN_OFFSITE;
2466 		break;
2467 	case CHROMA_H1V2:
2468 	default:
2469 		DPU_ERROR("[enc:%d] unsupported chroma sampling type\n",
2470 			  DRMID(phys_enc->parent));
2471 		cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
2472 		cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
2473 		break;
2474 	}
2475 
2476 	DPU_DEBUG("[enc:%d] cdm_enable:%d,%d,%p4cc,%d,%d,%d,%d]\n",
2477 		  DRMID(phys_enc->parent), cdm_cfg->output_width,
2478 		  cdm_cfg->output_height, &cdm_cfg->output_fmt->pixel_format,
2479 		  cdm_cfg->output_type, cdm_cfg->output_bit_depth,
2480 		  cdm_cfg->h_cdwn_type, cdm_cfg->v_cdwn_type);
2481 
2482 	if (hw_cdm->ops.enable) {
2483 		cdm_cfg->pp_id = hw_pp->idx;
2484 		ret = hw_cdm->ops.enable(hw_cdm, cdm_cfg);
2485 		if (ret < 0) {
2486 			DPU_ERROR("[enc:%d] failed to enable CDM; ret:%d\n",
2487 				  DRMID(phys_enc->parent), ret);
2488 			return;
2489 		}
2490 	}
2491 }
2492 
2493 #ifdef CONFIG_DEBUG_FS
2494 static int _dpu_encoder_status_show(struct seq_file *s, void *data)
2495 {
2496 	struct drm_encoder *drm_enc = s->private;
2497 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
2498 	int i;
2499 
2500 	mutex_lock(&dpu_enc->enc_lock);
2501 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2502 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
2503 
2504 		seq_printf(s, "intf:%d  wb:%d  vsync:%8d     underrun:%8d    frame_done_cnt:%d",
2505 				phys->hw_intf ? phys->hw_intf->idx - INTF_0 : -1,
2506 				phys->hw_wb ? phys->hw_wb->idx - WB_0 : -1,
2507 				atomic_read(&phys->vsync_cnt),
2508 				atomic_read(&phys->underrun_cnt),
2509 				atomic_read(&dpu_enc->frame_done_timeout_cnt));
2510 
2511 		seq_printf(s, "mode: %s\n", dpu_encoder_helper_get_intf_type(phys->intf_mode));
2512 	}
2513 	mutex_unlock(&dpu_enc->enc_lock);
2514 
2515 	return 0;
2516 }
2517 
2518 DEFINE_SHOW_ATTRIBUTE(_dpu_encoder_status);
2519 
2520 static void dpu_encoder_debugfs_init(struct drm_encoder *drm_enc, struct dentry *root)
2521 {
2522 	/* don't error check these */
2523 	debugfs_create_file("status", 0600,
2524 			    root, drm_enc, &_dpu_encoder_status_fops);
2525 }
2526 #else
2527 #define dpu_encoder_debugfs_init NULL
2528 #endif
2529 
2530 static int dpu_encoder_virt_add_phys_encs(
2531 		struct drm_device *dev,
2532 		struct msm_display_info *disp_info,
2533 		struct dpu_encoder_virt *dpu_enc,
2534 		struct dpu_enc_phys_init_params *params)
2535 {
2536 	struct dpu_encoder_phys *enc = NULL;
2537 
2538 	DPU_DEBUG_ENC(dpu_enc, "\n");
2539 
2540 	/*
2541 	 * We may create up to NUM_PHYS_ENCODER_TYPES physical encoder types
2542 	 * in this function, check up-front.
2543 	 */
2544 	if (dpu_enc->num_phys_encs + NUM_PHYS_ENCODER_TYPES >=
2545 			ARRAY_SIZE(dpu_enc->phys_encs)) {
2546 		DPU_ERROR_ENC(dpu_enc, "too many physical encoders %d\n",
2547 			  dpu_enc->num_phys_encs);
2548 		return -EINVAL;
2549 	}
2550 
2551 
2552 	if (disp_info->intf_type == INTF_WB) {
2553 		enc = dpu_encoder_phys_wb_init(dev, params);
2554 
2555 		if (IS_ERR(enc)) {
2556 			DPU_ERROR_ENC(dpu_enc, "failed to init wb enc: %ld\n",
2557 				PTR_ERR(enc));
2558 			return PTR_ERR(enc);
2559 		}
2560 
2561 		dpu_enc->phys_encs[dpu_enc->num_phys_encs] = enc;
2562 		++dpu_enc->num_phys_encs;
2563 	} else if (disp_info->is_cmd_mode) {
2564 		enc = dpu_encoder_phys_cmd_init(dev, params);
2565 
2566 		if (IS_ERR(enc)) {
2567 			DPU_ERROR_ENC(dpu_enc, "failed to init cmd enc: %ld\n",
2568 				PTR_ERR(enc));
2569 			return PTR_ERR(enc);
2570 		}
2571 
2572 		dpu_enc->phys_encs[dpu_enc->num_phys_encs] = enc;
2573 		++dpu_enc->num_phys_encs;
2574 	} else {
2575 		enc = dpu_encoder_phys_vid_init(dev, params);
2576 
2577 		if (IS_ERR(enc)) {
2578 			DPU_ERROR_ENC(dpu_enc, "failed to init vid enc: %ld\n",
2579 				PTR_ERR(enc));
2580 			return PTR_ERR(enc);
2581 		}
2582 
2583 		dpu_enc->phys_encs[dpu_enc->num_phys_encs] = enc;
2584 		++dpu_enc->num_phys_encs;
2585 	}
2586 
2587 	if (params->split_role == ENC_ROLE_SLAVE)
2588 		dpu_enc->cur_slave = enc;
2589 	else
2590 		dpu_enc->cur_master = enc;
2591 
2592 	return 0;
2593 }
2594 
2595 /**
2596  * dpu_encoder_get_clones - Calculate the possible_clones for DPU encoder
2597  * @drm_enc:        DRM encoder pointer
2598  * Returns:         possible_clones mask
2599  */
2600 uint32_t dpu_encoder_get_clones(struct drm_encoder *drm_enc)
2601 {
2602 	struct drm_encoder *curr;
2603 	int type = drm_enc->encoder_type;
2604 	uint32_t clone_mask = drm_encoder_mask(drm_enc);
2605 
2606 	/*
2607 	 * Set writeback as possible clones of real-time DSI encoders and vice
2608 	 * versa
2609 	 *
2610 	 * Writeback encoders can't be clones of each other and DSI
2611 	 * encoders can't be clones of each other.
2612 	 *
2613 	 * TODO: Add DP encoders as valid possible clones for writeback encoders
2614 	 * (and vice versa) once concurrent writeback has been validated for DP
2615 	 */
2616 	drm_for_each_encoder(curr, drm_enc->dev) {
2617 		if ((type == DRM_MODE_ENCODER_VIRTUAL &&
2618 		    curr->encoder_type == DRM_MODE_ENCODER_DSI) ||
2619 		    (type == DRM_MODE_ENCODER_DSI &&
2620 		    curr->encoder_type == DRM_MODE_ENCODER_VIRTUAL))
2621 			clone_mask |= drm_encoder_mask(curr);
2622 	}
2623 
2624 	return clone_mask;
2625 }
2626 
2627 static int dpu_encoder_setup_display(struct dpu_encoder_virt *dpu_enc,
2628 				 struct dpu_kms *dpu_kms,
2629 				 struct msm_display_info *disp_info)
2630 {
2631 	int ret = 0;
2632 	int i = 0;
2633 	struct dpu_enc_phys_init_params phys_params;
2634 
2635 	if (!dpu_enc) {
2636 		DPU_ERROR("invalid arg(s), enc %d\n", dpu_enc != NULL);
2637 		return -EINVAL;
2638 	}
2639 
2640 	dpu_enc->cur_master = NULL;
2641 
2642 	memset(&phys_params, 0, sizeof(phys_params));
2643 	phys_params.dpu_kms = dpu_kms;
2644 	phys_params.parent = &dpu_enc->base;
2645 	phys_params.enc_spinlock = &dpu_enc->enc_spinlock;
2646 
2647 	WARN_ON(disp_info->num_of_h_tiles < 1);
2648 
2649 	DPU_DEBUG("dsi_info->num_of_h_tiles %d\n", disp_info->num_of_h_tiles);
2650 
2651 	if (disp_info->intf_type != INTF_WB)
2652 		dpu_enc->idle_pc_supported =
2653 				dpu_kms->catalog->caps->has_idle_pc;
2654 
2655 	mutex_lock(&dpu_enc->enc_lock);
2656 	for (i = 0; i < disp_info->num_of_h_tiles && !ret; i++) {
2657 		/*
2658 		 * Left-most tile is at index 0, content is controller id
2659 		 * h_tile_instance_ids[2] = {0, 1}; DSI0 = left, DSI1 = right
2660 		 * h_tile_instance_ids[2] = {1, 0}; DSI1 = left, DSI0 = right
2661 		 */
2662 		u32 controller_id = disp_info->h_tile_instance[i];
2663 
2664 		if (disp_info->num_of_h_tiles > 1) {
2665 			if (i == 0)
2666 				phys_params.split_role = ENC_ROLE_MASTER;
2667 			else
2668 				phys_params.split_role = ENC_ROLE_SLAVE;
2669 		} else {
2670 			phys_params.split_role = ENC_ROLE_SOLO;
2671 		}
2672 
2673 		DPU_DEBUG("h_tile_instance %d = %d, split_role %d\n",
2674 				i, controller_id, phys_params.split_role);
2675 
2676 		phys_params.hw_intf = dpu_encoder_get_intf(dpu_kms->catalog, &dpu_kms->rm,
2677 							   disp_info->intf_type,
2678 							   controller_id);
2679 
2680 		if (disp_info->intf_type == INTF_WB && controller_id < WB_MAX)
2681 			phys_params.hw_wb = dpu_rm_get_wb(&dpu_kms->rm, controller_id);
2682 
2683 		if (!phys_params.hw_intf && !phys_params.hw_wb) {
2684 			DPU_ERROR_ENC(dpu_enc, "no intf or wb block assigned at idx: %d\n", i);
2685 			ret = -EINVAL;
2686 			break;
2687 		}
2688 
2689 		if (phys_params.hw_intf && phys_params.hw_wb) {
2690 			DPU_ERROR_ENC(dpu_enc,
2691 					"invalid phys both intf and wb block at idx: %d\n", i);
2692 			ret = -EINVAL;
2693 			break;
2694 		}
2695 
2696 		ret = dpu_encoder_virt_add_phys_encs(dpu_kms->dev, disp_info,
2697 				dpu_enc, &phys_params);
2698 		if (ret) {
2699 			DPU_ERROR_ENC(dpu_enc, "failed to add phys encs\n");
2700 			break;
2701 		}
2702 	}
2703 
2704 	mutex_unlock(&dpu_enc->enc_lock);
2705 
2706 	return ret;
2707 }
2708 
2709 static void dpu_encoder_frame_done_timeout(struct timer_list *t)
2710 {
2711 	struct dpu_encoder_virt *dpu_enc = timer_container_of(dpu_enc, t,
2712 							      frame_done_timer);
2713 	struct drm_encoder *drm_enc = &dpu_enc->base;
2714 	u32 event;
2715 
2716 	if (!drm_enc->dev) {
2717 		DPU_ERROR("invalid parameters\n");
2718 		return;
2719 	}
2720 
2721 	if (!dpu_enc->frame_busy_mask[0] || !dpu_enc->crtc) {
2722 		DRM_DEBUG_KMS("id:%u invalid timeout frame_busy_mask=%lu\n",
2723 			      DRMID(drm_enc), dpu_enc->frame_busy_mask[0]);
2724 		return;
2725 	} else if (!atomic_xchg(&dpu_enc->frame_done_timeout_ms, 0)) {
2726 		DRM_DEBUG_KMS("id:%u invalid timeout\n", DRMID(drm_enc));
2727 		return;
2728 	}
2729 
2730 	DPU_ERROR_ENC_RATELIMITED(dpu_enc, "frame done timeout\n");
2731 
2732 	if (atomic_inc_return(&dpu_enc->frame_done_timeout_cnt) == 1)
2733 		msm_disp_snapshot_state(drm_enc->dev);
2734 
2735 	event = DPU_ENCODER_FRAME_EVENT_ERROR;
2736 	trace_dpu_enc_frame_done_timeout(DRMID(drm_enc), event);
2737 	dpu_crtc_frame_event_cb(dpu_enc->crtc, event);
2738 }
2739 
2740 static const struct drm_encoder_helper_funcs dpu_encoder_helper_funcs = {
2741 	.atomic_mode_set = dpu_encoder_virt_atomic_mode_set,
2742 	.atomic_disable = dpu_encoder_virt_atomic_disable,
2743 	.atomic_enable = dpu_encoder_virt_atomic_enable,
2744 };
2745 
2746 static const struct drm_encoder_funcs dpu_encoder_funcs = {
2747 	.debugfs_init = dpu_encoder_debugfs_init,
2748 };
2749 
2750 /**
2751  * dpu_encoder_init - initialize virtual encoder object
2752  * @dev:        Pointer to drm device structure
2753  * @drm_enc_mode: corresponding DRM_MODE_ENCODER_* constant
2754  * @disp_info:  Pointer to display information structure
2755  * Returns:     Pointer to newly created drm encoder
2756  */
2757 struct drm_encoder *dpu_encoder_init(struct drm_device *dev,
2758 		int drm_enc_mode,
2759 		struct msm_display_info *disp_info)
2760 {
2761 	struct msm_drm_private *priv = dev->dev_private;
2762 	struct dpu_kms *dpu_kms = to_dpu_kms(priv->kms);
2763 	struct dpu_encoder_virt *dpu_enc;
2764 	int ret;
2765 
2766 	dpu_enc = drmm_encoder_alloc(dev, struct dpu_encoder_virt, base,
2767 				     &dpu_encoder_funcs, drm_enc_mode, NULL);
2768 	if (IS_ERR(dpu_enc))
2769 		return ERR_CAST(dpu_enc);
2770 
2771 	drm_encoder_helper_add(&dpu_enc->base, &dpu_encoder_helper_funcs);
2772 
2773 	spin_lock_init(&dpu_enc->enc_spinlock);
2774 	dpu_enc->enabled = false;
2775 	mutex_init(&dpu_enc->enc_lock);
2776 	mutex_init(&dpu_enc->rc_lock);
2777 
2778 	ret = dpu_encoder_setup_display(dpu_enc, dpu_kms, disp_info);
2779 	if (ret) {
2780 		DPU_ERROR("failed to setup encoder\n");
2781 		return ERR_PTR(-ENOMEM);
2782 	}
2783 
2784 	atomic_set(&dpu_enc->frame_done_timeout_ms, 0);
2785 	atomic_set(&dpu_enc->frame_done_timeout_cnt, 0);
2786 	timer_setup(&dpu_enc->frame_done_timer,
2787 			dpu_encoder_frame_done_timeout, 0);
2788 
2789 	INIT_DELAYED_WORK(&dpu_enc->delayed_off_work,
2790 			dpu_encoder_off_work);
2791 	dpu_enc->idle_timeout = IDLE_TIMEOUT;
2792 
2793 	memcpy(&dpu_enc->disp_info, disp_info, sizeof(*disp_info));
2794 
2795 	DPU_DEBUG_ENC(dpu_enc, "created\n");
2796 
2797 	return &dpu_enc->base;
2798 }
2799 
2800 /**
2801  * dpu_encoder_wait_for_commit_done() - Wait for encoder to flush pending state
2802  * @drm_enc:	encoder pointer
2803  *
2804  * Wait for hardware to have flushed the current pending changes to hardware at
2805  * a vblank or CTL_START. Physical encoders will map this differently depending
2806  * on the type: vid mode -> vsync_irq, cmd mode -> CTL_START.
2807  *
2808  * Return: 0 on success, -EWOULDBLOCK if already signaled, error otherwise
2809  */
2810 int dpu_encoder_wait_for_commit_done(struct drm_encoder *drm_enc)
2811 {
2812 	struct dpu_encoder_virt *dpu_enc = NULL;
2813 	int i, ret = 0;
2814 
2815 	if (!drm_enc) {
2816 		DPU_ERROR("invalid encoder\n");
2817 		return -EINVAL;
2818 	}
2819 	dpu_enc = to_dpu_encoder_virt(drm_enc);
2820 	DPU_DEBUG_ENC(dpu_enc, "\n");
2821 
2822 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2823 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
2824 
2825 		if (phys->ops.wait_for_commit_done) {
2826 			DPU_ATRACE_BEGIN("wait_for_commit_done");
2827 			ret = phys->ops.wait_for_commit_done(phys);
2828 			DPU_ATRACE_END("wait_for_commit_done");
2829 			if (ret == -ETIMEDOUT && !dpu_enc->commit_done_timedout) {
2830 				dpu_enc->commit_done_timedout = true;
2831 				msm_disp_snapshot_state(drm_enc->dev);
2832 			}
2833 			if (ret)
2834 				return ret;
2835 		}
2836 	}
2837 
2838 	return ret;
2839 }
2840 
2841 /**
2842  * dpu_encoder_wait_for_tx_complete() - Wait for encoder to transfer pixels to panel
2843  * @drm_enc:	encoder pointer
2844  *
2845  * Wait for the hardware to transfer all the pixels to the panel. Physical
2846  * encoders will map this differently depending on the type: vid mode -> vsync_irq,
2847  * cmd mode -> pp_done.
2848  *
2849  * Return: 0 on success, -EWOULDBLOCK if already signaled, error otherwise
2850  */
2851 int dpu_encoder_wait_for_tx_complete(struct drm_encoder *drm_enc)
2852 {
2853 	struct dpu_encoder_virt *dpu_enc = NULL;
2854 	int i, ret = 0;
2855 
2856 	if (!drm_enc) {
2857 		DPU_ERROR("invalid encoder\n");
2858 		return -EINVAL;
2859 	}
2860 	dpu_enc = to_dpu_encoder_virt(drm_enc);
2861 	DPU_DEBUG_ENC(dpu_enc, "\n");
2862 
2863 	for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2864 		struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
2865 
2866 		if (phys->ops.wait_for_tx_complete) {
2867 			DPU_ATRACE_BEGIN("wait_for_tx_complete");
2868 			ret = phys->ops.wait_for_tx_complete(phys);
2869 			DPU_ATRACE_END("wait_for_tx_complete");
2870 			if (ret)
2871 				return ret;
2872 		}
2873 	}
2874 
2875 	return ret;
2876 }
2877 
2878 /**
2879  * dpu_encoder_get_intf_mode - get interface mode of the given encoder
2880  * @encoder: Pointer to drm encoder object
2881  */
2882 enum dpu_intf_mode dpu_encoder_get_intf_mode(struct drm_encoder *encoder)
2883 {
2884 	struct dpu_encoder_virt *dpu_enc = NULL;
2885 
2886 	if (!encoder) {
2887 		DPU_ERROR("invalid encoder\n");
2888 		return INTF_MODE_NONE;
2889 	}
2890 	dpu_enc = to_dpu_encoder_virt(encoder);
2891 
2892 	if (dpu_enc->cur_master)
2893 		return dpu_enc->cur_master->intf_mode;
2894 
2895 	if (dpu_enc->num_phys_encs)
2896 		return dpu_enc->phys_encs[0]->intf_mode;
2897 
2898 	return INTF_MODE_NONE;
2899 }
2900 
2901 /**
2902  * dpu_encoder_helper_get_cwb_mask - get CWB blocks mask for the DPU encoder
2903  * @phys_enc: Pointer to physical encoder structure
2904  */
2905 unsigned int dpu_encoder_helper_get_cwb_mask(struct dpu_encoder_phys *phys_enc)
2906 {
2907 	struct drm_encoder *encoder = phys_enc->parent;
2908 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(encoder);
2909 
2910 	return dpu_enc->cwb_mask;
2911 }
2912 
2913 /**
2914  * dpu_encoder_helper_get_dsc - get DSC blocks mask for the DPU encoder
2915  *   This helper function is used by physical encoder to get DSC blocks mask
2916  *   used for this encoder.
2917  * @phys_enc: Pointer to physical encoder structure
2918  */
2919 unsigned int dpu_encoder_helper_get_dsc(struct dpu_encoder_phys *phys_enc)
2920 {
2921 	struct drm_encoder *encoder = phys_enc->parent;
2922 	struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(encoder);
2923 
2924 	return dpu_enc->dsc_mask;
2925 }
2926 
2927 void dpu_encoder_phys_init(struct dpu_encoder_phys *phys_enc,
2928 			  struct dpu_enc_phys_init_params *p)
2929 {
2930 	phys_enc->hw_mdptop = p->dpu_kms->hw_mdp;
2931 	phys_enc->hw_intf = p->hw_intf;
2932 	phys_enc->hw_wb = p->hw_wb;
2933 	phys_enc->parent = p->parent;
2934 	phys_enc->dpu_kms = p->dpu_kms;
2935 	phys_enc->split_role = p->split_role;
2936 	phys_enc->enc_spinlock = p->enc_spinlock;
2937 	phys_enc->enable_state = DPU_ENC_DISABLED;
2938 
2939 	atomic_set(&phys_enc->pending_kickoff_cnt, 0);
2940 	atomic_set(&phys_enc->pending_ctlstart_cnt, 0);
2941 
2942 	atomic_set(&phys_enc->vsync_cnt, 0);
2943 	atomic_set(&phys_enc->underrun_cnt, 0);
2944 
2945 	init_waitqueue_head(&phys_enc->pending_kickoff_wq);
2946 }
2947