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