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