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