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