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/sort.h>
10 #include <linux/debugfs.h>
11 #include <linux/ktime.h>
12 #include <linux/bits.h>
13 
14 #include <drm/drm_atomic.h>
15 #include <drm/drm_crtc.h>
16 #include <drm/drm_flip_work.h>
17 #include <drm/drm_mode.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_rect.h>
20 #include <drm/drm_vblank.h>
21 
22 #include "dpu_kms.h"
23 #include "dpu_hw_lm.h"
24 #include "dpu_hw_ctl.h"
25 #include "dpu_hw_dspp.h"
26 #include "dpu_crtc.h"
27 #include "dpu_plane.h"
28 #include "dpu_encoder.h"
29 #include "dpu_vbif.h"
30 #include "dpu_core_perf.h"
31 #include "dpu_trace.h"
32 
33 #define DPU_DRM_BLEND_OP_NOT_DEFINED    0
34 #define DPU_DRM_BLEND_OP_OPAQUE         1
35 #define DPU_DRM_BLEND_OP_PREMULTIPLIED  2
36 #define DPU_DRM_BLEND_OP_COVERAGE       3
37 #define DPU_DRM_BLEND_OP_MAX            4
38 
39 /* layer mixer index on dpu_crtc */
40 #define LEFT_MIXER 0
41 #define RIGHT_MIXER 1
42 
43 /* timeout in ms waiting for frame done */
44 #define DPU_CRTC_FRAME_DONE_TIMEOUT_MS	60
45 
46 #define	CONVERT_S3_15(val) \
47 	(((((u64)val) & ~BIT_ULL(63)) >> 17) & GENMASK_ULL(17, 0))
48 
49 static struct dpu_kms *_dpu_crtc_get_kms(struct drm_crtc *crtc)
50 {
51 	struct msm_drm_private *priv = crtc->dev->dev_private;
52 
53 	return to_dpu_kms(priv->kms);
54 }
55 
56 static void dpu_crtc_destroy(struct drm_crtc *crtc)
57 {
58 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
59 
60 	DPU_DEBUG("\n");
61 
62 	if (!crtc)
63 		return;
64 
65 	drm_crtc_cleanup(crtc);
66 	kfree(dpu_crtc);
67 }
68 
69 static struct drm_encoder *get_encoder_from_crtc(struct drm_crtc *crtc)
70 {
71 	struct drm_device *dev = crtc->dev;
72 	struct drm_encoder *encoder;
73 
74 	drm_for_each_encoder(encoder, dev)
75 		if (encoder->crtc == crtc)
76 			return encoder;
77 
78 	return NULL;
79 }
80 
81 static u32 dpu_crtc_get_vblank_counter(struct drm_crtc *crtc)
82 {
83 	struct drm_encoder *encoder;
84 
85 	encoder = get_encoder_from_crtc(crtc);
86 	if (!encoder) {
87 		DRM_ERROR("no encoder found for crtc %d\n", crtc->index);
88 		return false;
89 	}
90 
91 	return dpu_encoder_get_frame_count(encoder);
92 }
93 
94 static bool dpu_crtc_get_scanout_position(struct drm_crtc *crtc,
95 					   bool in_vblank_irq,
96 					   int *vpos, int *hpos,
97 					   ktime_t *stime, ktime_t *etime,
98 					   const struct drm_display_mode *mode)
99 {
100 	unsigned int pipe = crtc->index;
101 	struct drm_encoder *encoder;
102 	int line, vsw, vbp, vactive_start, vactive_end, vfp_end;
103 
104 	encoder = get_encoder_from_crtc(crtc);
105 	if (!encoder) {
106 		DRM_ERROR("no encoder found for crtc %d\n", pipe);
107 		return false;
108 	}
109 
110 	vsw = mode->crtc_vsync_end - mode->crtc_vsync_start;
111 	vbp = mode->crtc_vtotal - mode->crtc_vsync_end;
112 
113 	/*
114 	 * the line counter is 1 at the start of the VSYNC pulse and VTOTAL at
115 	 * the end of VFP. Translate the porch values relative to the line
116 	 * counter positions.
117 	 */
118 
119 	vactive_start = vsw + vbp + 1;
120 	vactive_end = vactive_start + mode->crtc_vdisplay;
121 
122 	/* last scan line before VSYNC */
123 	vfp_end = mode->crtc_vtotal;
124 
125 	if (stime)
126 		*stime = ktime_get();
127 
128 	line = dpu_encoder_get_linecount(encoder);
129 
130 	if (line < vactive_start)
131 		line -= vactive_start;
132 	else if (line > vactive_end)
133 		line = line - vfp_end - vactive_start;
134 	else
135 		line -= vactive_start;
136 
137 	*vpos = line;
138 	*hpos = 0;
139 
140 	if (etime)
141 		*etime = ktime_get();
142 
143 	return true;
144 }
145 
146 static void _dpu_crtc_setup_blend_cfg(struct dpu_crtc_mixer *mixer,
147 		struct dpu_plane_state *pstate, struct dpu_format *format)
148 {
149 	struct dpu_hw_mixer *lm = mixer->hw_lm;
150 	uint32_t blend_op;
151 
152 	/* default to opaque blending */
153 	blend_op = DPU_BLEND_FG_ALPHA_FG_CONST |
154 		DPU_BLEND_BG_ALPHA_BG_CONST;
155 
156 	if (format->alpha_enable) {
157 		/* coverage blending */
158 		blend_op = DPU_BLEND_FG_ALPHA_FG_PIXEL |
159 			DPU_BLEND_BG_ALPHA_FG_PIXEL |
160 			DPU_BLEND_BG_INV_ALPHA;
161 	}
162 
163 	lm->ops.setup_blend_config(lm, pstate->stage,
164 				0xFF, 0, blend_op);
165 
166 	DPU_DEBUG("format:%p4cc, alpha_en:%u blend_op:0x%x\n",
167 		  &format->base.pixel_format, format->alpha_enable, blend_op);
168 }
169 
170 static void _dpu_crtc_program_lm_output_roi(struct drm_crtc *crtc)
171 {
172 	struct dpu_crtc_state *crtc_state;
173 	int lm_idx, lm_horiz_position;
174 
175 	crtc_state = to_dpu_crtc_state(crtc->state);
176 
177 	lm_horiz_position = 0;
178 	for (lm_idx = 0; lm_idx < crtc_state->num_mixers; lm_idx++) {
179 		const struct drm_rect *lm_roi = &crtc_state->lm_bounds[lm_idx];
180 		struct dpu_hw_mixer *hw_lm = crtc_state->mixers[lm_idx].hw_lm;
181 		struct dpu_hw_mixer_cfg cfg;
182 
183 		if (!lm_roi || !drm_rect_visible(lm_roi))
184 			continue;
185 
186 		cfg.out_width = drm_rect_width(lm_roi);
187 		cfg.out_height = drm_rect_height(lm_roi);
188 		cfg.right_mixer = lm_horiz_position++;
189 		cfg.flags = 0;
190 		hw_lm->ops.setup_mixer_out(hw_lm, &cfg);
191 	}
192 }
193 
194 static void _dpu_crtc_blend_setup_mixer(struct drm_crtc *crtc,
195 	struct dpu_crtc *dpu_crtc, struct dpu_crtc_mixer *mixer)
196 {
197 	struct drm_plane *plane;
198 	struct drm_framebuffer *fb;
199 	struct drm_plane_state *state;
200 	struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc->state);
201 	struct dpu_plane_state *pstate = NULL;
202 	struct dpu_format *format;
203 	struct dpu_hw_ctl *ctl = mixer->lm_ctl;
204 	struct dpu_hw_stage_cfg *stage_cfg = &dpu_crtc->stage_cfg;
205 
206 	u32 flush_mask;
207 	uint32_t stage_idx, lm_idx;
208 	int zpos_cnt[DPU_STAGE_MAX + 1] = { 0 };
209 	bool bg_alpha_enable = false;
210 	DECLARE_BITMAP(fetch_active, SSPP_MAX);
211 
212 	memset(fetch_active, 0, sizeof(fetch_active));
213 	drm_atomic_crtc_for_each_plane(plane, crtc) {
214 		state = plane->state;
215 		if (!state)
216 			continue;
217 
218 		pstate = to_dpu_plane_state(state);
219 		fb = state->fb;
220 
221 		dpu_plane_get_ctl_flush(plane, ctl, &flush_mask);
222 		set_bit(dpu_plane_pipe(plane), fetch_active);
223 		DPU_DEBUG("crtc %d stage:%d - plane %d sspp %d fb %d\n",
224 				crtc->base.id,
225 				pstate->stage,
226 				plane->base.id,
227 				dpu_plane_pipe(plane) - SSPP_VIG0,
228 				state->fb ? state->fb->base.id : -1);
229 
230 		format = to_dpu_format(msm_framebuffer_format(pstate->base.fb));
231 
232 		if (pstate->stage == DPU_STAGE_BASE && format->alpha_enable)
233 			bg_alpha_enable = true;
234 
235 		stage_idx = zpos_cnt[pstate->stage]++;
236 		stage_cfg->stage[pstate->stage][stage_idx] =
237 					dpu_plane_pipe(plane);
238 		stage_cfg->multirect_index[pstate->stage][stage_idx] =
239 					pstate->multirect_index;
240 
241 		trace_dpu_crtc_setup_mixer(DRMID(crtc), DRMID(plane),
242 					   state, pstate, stage_idx,
243 					   dpu_plane_pipe(plane) - SSPP_VIG0,
244 					   format->base.pixel_format,
245 					   fb ? fb->modifier : 0);
246 
247 		/* blend config update */
248 		for (lm_idx = 0; lm_idx < cstate->num_mixers; lm_idx++) {
249 			_dpu_crtc_setup_blend_cfg(mixer + lm_idx,
250 						pstate, format);
251 
252 			mixer[lm_idx].flush_mask |= flush_mask;
253 
254 			if (bg_alpha_enable && !format->alpha_enable)
255 				mixer[lm_idx].mixer_op_mode = 0;
256 			else
257 				mixer[lm_idx].mixer_op_mode |=
258 						1 << pstate->stage;
259 		}
260 	}
261 
262 	if (ctl->ops.set_active_pipes)
263 		ctl->ops.set_active_pipes(ctl, fetch_active);
264 
265 	 _dpu_crtc_program_lm_output_roi(crtc);
266 }
267 
268 /**
269  * _dpu_crtc_blend_setup - configure crtc mixers
270  * @crtc: Pointer to drm crtc structure
271  */
272 static void _dpu_crtc_blend_setup(struct drm_crtc *crtc)
273 {
274 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
275 	struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc->state);
276 	struct dpu_crtc_mixer *mixer = cstate->mixers;
277 	struct dpu_hw_ctl *ctl;
278 	struct dpu_hw_mixer *lm;
279 	int i;
280 
281 	DPU_DEBUG("%s\n", dpu_crtc->name);
282 
283 	for (i = 0; i < cstate->num_mixers; i++) {
284 		mixer[i].mixer_op_mode = 0;
285 		mixer[i].flush_mask = 0;
286 		if (mixer[i].lm_ctl->ops.clear_all_blendstages)
287 			mixer[i].lm_ctl->ops.clear_all_blendstages(
288 					mixer[i].lm_ctl);
289 	}
290 
291 	/* initialize stage cfg */
292 	memset(&dpu_crtc->stage_cfg, 0, sizeof(struct dpu_hw_stage_cfg));
293 
294 	_dpu_crtc_blend_setup_mixer(crtc, dpu_crtc, mixer);
295 
296 	for (i = 0; i < cstate->num_mixers; i++) {
297 		ctl = mixer[i].lm_ctl;
298 		lm = mixer[i].hw_lm;
299 
300 		lm->ops.setup_alpha_out(lm, mixer[i].mixer_op_mode);
301 
302 		mixer[i].flush_mask |= ctl->ops.get_bitmask_mixer(ctl,
303 			mixer[i].hw_lm->idx);
304 
305 		/* stage config flush mask */
306 		ctl->ops.update_pending_flush(ctl, mixer[i].flush_mask);
307 
308 		DPU_DEBUG("lm %d, op_mode 0x%X, ctl %d, flush mask 0x%x\n",
309 			mixer[i].hw_lm->idx - LM_0,
310 			mixer[i].mixer_op_mode,
311 			ctl->idx - CTL_0,
312 			mixer[i].flush_mask);
313 
314 		ctl->ops.setup_blendstage(ctl, mixer[i].hw_lm->idx,
315 			&dpu_crtc->stage_cfg);
316 	}
317 }
318 
319 /**
320  *  _dpu_crtc_complete_flip - signal pending page_flip events
321  * Any pending vblank events are added to the vblank_event_list
322  * so that the next vblank interrupt shall signal them.
323  * However PAGE_FLIP events are not handled through the vblank_event_list.
324  * This API signals any pending PAGE_FLIP events requested through
325  * DRM_IOCTL_MODE_PAGE_FLIP and are cached in the dpu_crtc->event.
326  * @crtc: Pointer to drm crtc structure
327  */
328 static void _dpu_crtc_complete_flip(struct drm_crtc *crtc)
329 {
330 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
331 	struct drm_device *dev = crtc->dev;
332 	unsigned long flags;
333 
334 	spin_lock_irqsave(&dev->event_lock, flags);
335 	if (dpu_crtc->event) {
336 		DRM_DEBUG_VBL("%s: send event: %pK\n", dpu_crtc->name,
337 			      dpu_crtc->event);
338 		trace_dpu_crtc_complete_flip(DRMID(crtc));
339 		drm_crtc_send_vblank_event(crtc, dpu_crtc->event);
340 		dpu_crtc->event = NULL;
341 	}
342 	spin_unlock_irqrestore(&dev->event_lock, flags);
343 }
344 
345 enum dpu_intf_mode dpu_crtc_get_intf_mode(struct drm_crtc *crtc)
346 {
347 	struct drm_encoder *encoder;
348 
349 	/*
350 	 * TODO: This function is called from dpu debugfs and as part of atomic
351 	 * check. When called from debugfs, the crtc->mutex must be held to
352 	 * read crtc->state. However reading crtc->state from atomic check isn't
353 	 * allowed (unless you have a good reason, a big comment, and a deep
354 	 * understanding of how the atomic/modeset locks work (<- and this is
355 	 * probably not possible)). So we'll keep the WARN_ON here for now, but
356 	 * really we need to figure out a better way to track our operating mode
357 	 */
358 	WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
359 
360 	/* TODO: Returns the first INTF_MODE, could there be multiple values? */
361 	drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask)
362 		return dpu_encoder_get_intf_mode(encoder);
363 
364 	return INTF_MODE_NONE;
365 }
366 
367 void dpu_crtc_vblank_callback(struct drm_crtc *crtc)
368 {
369 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
370 
371 	/* keep statistics on vblank callback - with auto reset via debugfs */
372 	if (ktime_compare(dpu_crtc->vblank_cb_time, ktime_set(0, 0)) == 0)
373 		dpu_crtc->vblank_cb_time = ktime_get();
374 	else
375 		dpu_crtc->vblank_cb_count++;
376 	drm_crtc_handle_vblank(crtc);
377 	trace_dpu_crtc_vblank_cb(DRMID(crtc));
378 }
379 
380 static void dpu_crtc_frame_event_work(struct kthread_work *work)
381 {
382 	struct dpu_crtc_frame_event *fevent = container_of(work,
383 			struct dpu_crtc_frame_event, work);
384 	struct drm_crtc *crtc = fevent->crtc;
385 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
386 	unsigned long flags;
387 	bool frame_done = false;
388 
389 	DPU_ATRACE_BEGIN("crtc_frame_event");
390 
391 	DRM_DEBUG_KMS("crtc%d event:%u ts:%lld\n", crtc->base.id, fevent->event,
392 			ktime_to_ns(fevent->ts));
393 
394 	if (fevent->event & (DPU_ENCODER_FRAME_EVENT_DONE
395 				| DPU_ENCODER_FRAME_EVENT_ERROR
396 				| DPU_ENCODER_FRAME_EVENT_PANEL_DEAD)) {
397 
398 		if (atomic_read(&dpu_crtc->frame_pending) < 1) {
399 			/* ignore vblank when not pending */
400 		} else if (atomic_dec_return(&dpu_crtc->frame_pending) == 0) {
401 			/* release bandwidth and other resources */
402 			trace_dpu_crtc_frame_event_done(DRMID(crtc),
403 							fevent->event);
404 			dpu_core_perf_crtc_release_bw(crtc);
405 		} else {
406 			trace_dpu_crtc_frame_event_more_pending(DRMID(crtc),
407 								fevent->event);
408 		}
409 
410 		if (fevent->event & DPU_ENCODER_FRAME_EVENT_DONE)
411 			dpu_core_perf_crtc_update(crtc, 0, false);
412 
413 		if (fevent->event & (DPU_ENCODER_FRAME_EVENT_DONE
414 					| DPU_ENCODER_FRAME_EVENT_ERROR))
415 			frame_done = true;
416 	}
417 
418 	if (fevent->event & DPU_ENCODER_FRAME_EVENT_PANEL_DEAD)
419 		DPU_ERROR("crtc%d ts:%lld received panel dead event\n",
420 				crtc->base.id, ktime_to_ns(fevent->ts));
421 
422 	if (frame_done)
423 		complete_all(&dpu_crtc->frame_done_comp);
424 
425 	spin_lock_irqsave(&dpu_crtc->spin_lock, flags);
426 	list_add_tail(&fevent->list, &dpu_crtc->frame_event_list);
427 	spin_unlock_irqrestore(&dpu_crtc->spin_lock, flags);
428 	DPU_ATRACE_END("crtc_frame_event");
429 }
430 
431 /*
432  * dpu_crtc_frame_event_cb - crtc frame event callback API. CRTC module
433  * registers this API to encoder for all frame event callbacks like
434  * frame_error, frame_done, idle_timeout, etc. Encoder may call different events
435  * from different context - IRQ, user thread, commit_thread, etc. Each event
436  * should be carefully reviewed and should be processed in proper task context
437  * to avoid schedulin delay or properly manage the irq context's bottom half
438  * processing.
439  */
440 static void dpu_crtc_frame_event_cb(void *data, u32 event)
441 {
442 	struct drm_crtc *crtc = (struct drm_crtc *)data;
443 	struct dpu_crtc *dpu_crtc;
444 	struct msm_drm_private *priv;
445 	struct dpu_crtc_frame_event *fevent;
446 	unsigned long flags;
447 	u32 crtc_id;
448 
449 	/* Nothing to do on idle event */
450 	if (event & DPU_ENCODER_FRAME_EVENT_IDLE)
451 		return;
452 
453 	dpu_crtc = to_dpu_crtc(crtc);
454 	priv = crtc->dev->dev_private;
455 	crtc_id = drm_crtc_index(crtc);
456 
457 	trace_dpu_crtc_frame_event_cb(DRMID(crtc), event);
458 
459 	spin_lock_irqsave(&dpu_crtc->spin_lock, flags);
460 	fevent = list_first_entry_or_null(&dpu_crtc->frame_event_list,
461 			struct dpu_crtc_frame_event, list);
462 	if (fevent)
463 		list_del_init(&fevent->list);
464 	spin_unlock_irqrestore(&dpu_crtc->spin_lock, flags);
465 
466 	if (!fevent) {
467 		DRM_ERROR_RATELIMITED("crtc%d event %d overflow\n", crtc->base.id, event);
468 		return;
469 	}
470 
471 	fevent->event = event;
472 	fevent->crtc = crtc;
473 	fevent->ts = ktime_get();
474 	kthread_queue_work(priv->event_thread[crtc_id].worker, &fevent->work);
475 }
476 
477 void dpu_crtc_complete_commit(struct drm_crtc *crtc)
478 {
479 	trace_dpu_crtc_complete_commit(DRMID(crtc));
480 	_dpu_crtc_complete_flip(crtc);
481 }
482 
483 static void _dpu_crtc_setup_lm_bounds(struct drm_crtc *crtc,
484 		struct drm_crtc_state *state)
485 {
486 	struct dpu_crtc_state *cstate = to_dpu_crtc_state(state);
487 	struct drm_display_mode *adj_mode = &state->adjusted_mode;
488 	u32 crtc_split_width = adj_mode->hdisplay / cstate->num_mixers;
489 	int i;
490 
491 	for (i = 0; i < cstate->num_mixers; i++) {
492 		struct drm_rect *r = &cstate->lm_bounds[i];
493 		r->x1 = crtc_split_width * i;
494 		r->y1 = 0;
495 		r->x2 = r->x1 + crtc_split_width;
496 		r->y2 = adj_mode->vdisplay;
497 
498 		trace_dpu_crtc_setup_lm_bounds(DRMID(crtc), i, r);
499 	}
500 }
501 
502 static void _dpu_crtc_get_pcc_coeff(struct drm_crtc_state *state,
503 		struct dpu_hw_pcc_cfg *cfg)
504 {
505 	struct drm_color_ctm *ctm;
506 
507 	memset(cfg, 0, sizeof(struct dpu_hw_pcc_cfg));
508 
509 	ctm = (struct drm_color_ctm *)state->ctm->data;
510 
511 	if (!ctm)
512 		return;
513 
514 	cfg->r.r = CONVERT_S3_15(ctm->matrix[0]);
515 	cfg->g.r = CONVERT_S3_15(ctm->matrix[1]);
516 	cfg->b.r = CONVERT_S3_15(ctm->matrix[2]);
517 
518 	cfg->r.g = CONVERT_S3_15(ctm->matrix[3]);
519 	cfg->g.g = CONVERT_S3_15(ctm->matrix[4]);
520 	cfg->b.g = CONVERT_S3_15(ctm->matrix[5]);
521 
522 	cfg->r.b = CONVERT_S3_15(ctm->matrix[6]);
523 	cfg->g.b = CONVERT_S3_15(ctm->matrix[7]);
524 	cfg->b.b = CONVERT_S3_15(ctm->matrix[8]);
525 }
526 
527 static void _dpu_crtc_setup_cp_blocks(struct drm_crtc *crtc)
528 {
529 	struct drm_crtc_state *state = crtc->state;
530 	struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc->state);
531 	struct dpu_crtc_mixer *mixer = cstate->mixers;
532 	struct dpu_hw_pcc_cfg cfg;
533 	struct dpu_hw_ctl *ctl;
534 	struct dpu_hw_dspp *dspp;
535 	int i;
536 
537 
538 	if (!state->color_mgmt_changed)
539 		return;
540 
541 	for (i = 0; i < cstate->num_mixers; i++) {
542 		ctl = mixer[i].lm_ctl;
543 		dspp = mixer[i].hw_dspp;
544 
545 		if (!dspp || !dspp->ops.setup_pcc)
546 			continue;
547 
548 		if (!state->ctm) {
549 			dspp->ops.setup_pcc(dspp, NULL);
550 		} else {
551 			_dpu_crtc_get_pcc_coeff(state, &cfg);
552 			dspp->ops.setup_pcc(dspp, &cfg);
553 		}
554 
555 		mixer[i].flush_mask |= ctl->ops.get_bitmask_dspp(ctl,
556 			mixer[i].hw_dspp->idx);
557 
558 		/* stage config flush mask */
559 		ctl->ops.update_pending_flush(ctl, mixer[i].flush_mask);
560 
561 		DPU_DEBUG("lm %d, ctl %d, flush mask 0x%x\n",
562 			mixer[i].hw_lm->idx - DSPP_0,
563 			ctl->idx - CTL_0,
564 			mixer[i].flush_mask);
565 	}
566 }
567 
568 static void dpu_crtc_atomic_begin(struct drm_crtc *crtc,
569 		struct drm_atomic_state *state)
570 {
571 	struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc->state);
572 	struct drm_encoder *encoder;
573 
574 	if (!crtc->state->enable) {
575 		DPU_DEBUG("crtc%d -> enable %d, skip atomic_begin\n",
576 				crtc->base.id, crtc->state->enable);
577 		return;
578 	}
579 
580 	DPU_DEBUG("crtc%d\n", crtc->base.id);
581 
582 	_dpu_crtc_setup_lm_bounds(crtc, crtc->state);
583 
584 	/* encoder will trigger pending mask now */
585 	drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask)
586 		dpu_encoder_trigger_kickoff_pending(encoder);
587 
588 	/*
589 	 * If no mixers have been allocated in dpu_crtc_atomic_check(),
590 	 * it means we are trying to flush a CRTC whose state is disabled:
591 	 * nothing else needs to be done.
592 	 */
593 	if (unlikely(!cstate->num_mixers))
594 		return;
595 
596 	_dpu_crtc_blend_setup(crtc);
597 
598 	_dpu_crtc_setup_cp_blocks(crtc);
599 
600 	/*
601 	 * PP_DONE irq is only used by command mode for now.
602 	 * It is better to request pending before FLUSH and START trigger
603 	 * to make sure no pp_done irq missed.
604 	 * This is safe because no pp_done will happen before SW trigger
605 	 * in command mode.
606 	 */
607 }
608 
609 static void dpu_crtc_atomic_flush(struct drm_crtc *crtc,
610 		struct drm_atomic_state *state)
611 {
612 	struct dpu_crtc *dpu_crtc;
613 	struct drm_device *dev;
614 	struct drm_plane *plane;
615 	struct msm_drm_private *priv;
616 	unsigned long flags;
617 	struct dpu_crtc_state *cstate;
618 
619 	if (!crtc->state->enable) {
620 		DPU_DEBUG("crtc%d -> enable %d, skip atomic_flush\n",
621 				crtc->base.id, crtc->state->enable);
622 		return;
623 	}
624 
625 	DPU_DEBUG("crtc%d\n", crtc->base.id);
626 
627 	dpu_crtc = to_dpu_crtc(crtc);
628 	cstate = to_dpu_crtc_state(crtc->state);
629 	dev = crtc->dev;
630 	priv = dev->dev_private;
631 
632 	if (crtc->index >= ARRAY_SIZE(priv->event_thread)) {
633 		DPU_ERROR("invalid crtc index[%d]\n", crtc->index);
634 		return;
635 	}
636 
637 	WARN_ON(dpu_crtc->event);
638 	spin_lock_irqsave(&dev->event_lock, flags);
639 	dpu_crtc->event = crtc->state->event;
640 	crtc->state->event = NULL;
641 	spin_unlock_irqrestore(&dev->event_lock, flags);
642 
643 	/*
644 	 * If no mixers has been allocated in dpu_crtc_atomic_check(),
645 	 * it means we are trying to flush a CRTC whose state is disabled:
646 	 * nothing else needs to be done.
647 	 */
648 	if (unlikely(!cstate->num_mixers))
649 		return;
650 
651 	/*
652 	 * For planes without commit update, drm framework will not add
653 	 * those planes to current state since hardware update is not
654 	 * required. However, if those planes were power collapsed since
655 	 * last commit cycle, driver has to restore the hardware state
656 	 * of those planes explicitly here prior to plane flush.
657 	 */
658 	drm_atomic_crtc_for_each_plane(plane, crtc)
659 		dpu_plane_restore(plane, state);
660 
661 	/* update performance setting before crtc kickoff */
662 	dpu_core_perf_crtc_update(crtc, 1, false);
663 
664 	/*
665 	 * Final plane updates: Give each plane a chance to complete all
666 	 *                      required writes/flushing before crtc's "flush
667 	 *                      everything" call below.
668 	 */
669 	drm_atomic_crtc_for_each_plane(plane, crtc) {
670 		if (dpu_crtc->smmu_state.transition_error)
671 			dpu_plane_set_error(plane, true);
672 		dpu_plane_flush(plane);
673 	}
674 
675 	/* Kickoff will be scheduled by outer layer */
676 }
677 
678 /**
679  * dpu_crtc_destroy_state - state destroy hook
680  * @crtc: drm CRTC
681  * @state: CRTC state object to release
682  */
683 static void dpu_crtc_destroy_state(struct drm_crtc *crtc,
684 		struct drm_crtc_state *state)
685 {
686 	struct dpu_crtc_state *cstate = to_dpu_crtc_state(state);
687 
688 	DPU_DEBUG("crtc%d\n", crtc->base.id);
689 
690 	__drm_atomic_helper_crtc_destroy_state(state);
691 
692 	kfree(cstate);
693 }
694 
695 static int _dpu_crtc_wait_for_frame_done(struct drm_crtc *crtc)
696 {
697 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
698 	int ret, rc = 0;
699 
700 	if (!atomic_read(&dpu_crtc->frame_pending)) {
701 		DPU_DEBUG("no frames pending\n");
702 		return 0;
703 	}
704 
705 	DPU_ATRACE_BEGIN("frame done completion wait");
706 	ret = wait_for_completion_timeout(&dpu_crtc->frame_done_comp,
707 			msecs_to_jiffies(DPU_CRTC_FRAME_DONE_TIMEOUT_MS));
708 	if (!ret) {
709 		DRM_ERROR("frame done wait timed out, ret:%d\n", ret);
710 		rc = -ETIMEDOUT;
711 	}
712 	DPU_ATRACE_END("frame done completion wait");
713 
714 	return rc;
715 }
716 
717 void dpu_crtc_commit_kickoff(struct drm_crtc *crtc)
718 {
719 	struct drm_encoder *encoder;
720 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
721 	struct dpu_kms *dpu_kms = _dpu_crtc_get_kms(crtc);
722 	struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc->state);
723 
724 	/*
725 	 * If no mixers has been allocated in dpu_crtc_atomic_check(),
726 	 * it means we are trying to start a CRTC whose state is disabled:
727 	 * nothing else needs to be done.
728 	 */
729 	if (unlikely(!cstate->num_mixers))
730 		return;
731 
732 	DPU_ATRACE_BEGIN("crtc_commit");
733 
734 	/*
735 	 * Encoder will flush/start now, unless it has a tx pending. If so, it
736 	 * may delay and flush at an irq event (e.g. ppdone)
737 	 */
738 	drm_for_each_encoder_mask(encoder, crtc->dev,
739 				  crtc->state->encoder_mask)
740 		dpu_encoder_prepare_for_kickoff(encoder);
741 
742 	if (atomic_inc_return(&dpu_crtc->frame_pending) == 1) {
743 		/* acquire bandwidth and other resources */
744 		DPU_DEBUG("crtc%d first commit\n", crtc->base.id);
745 	} else
746 		DPU_DEBUG("crtc%d commit\n", crtc->base.id);
747 
748 	dpu_crtc->play_count++;
749 
750 	dpu_vbif_clear_errors(dpu_kms);
751 
752 	drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask)
753 		dpu_encoder_kickoff(encoder);
754 
755 	reinit_completion(&dpu_crtc->frame_done_comp);
756 	DPU_ATRACE_END("crtc_commit");
757 }
758 
759 static void dpu_crtc_reset(struct drm_crtc *crtc)
760 {
761 	struct dpu_crtc_state *cstate = kzalloc(sizeof(*cstate), GFP_KERNEL);
762 
763 	if (crtc->state)
764 		dpu_crtc_destroy_state(crtc, crtc->state);
765 
766 	__drm_atomic_helper_crtc_reset(crtc, &cstate->base);
767 }
768 
769 /**
770  * dpu_crtc_duplicate_state - state duplicate hook
771  * @crtc: Pointer to drm crtc structure
772  */
773 static struct drm_crtc_state *dpu_crtc_duplicate_state(struct drm_crtc *crtc)
774 {
775 	struct dpu_crtc_state *cstate, *old_cstate = to_dpu_crtc_state(crtc->state);
776 
777 	cstate = kmemdup(old_cstate, sizeof(*old_cstate), GFP_KERNEL);
778 	if (!cstate) {
779 		DPU_ERROR("failed to allocate state\n");
780 		return NULL;
781 	}
782 
783 	/* duplicate base helper */
784 	__drm_atomic_helper_crtc_duplicate_state(crtc, &cstate->base);
785 
786 	return &cstate->base;
787 }
788 
789 static void dpu_crtc_disable(struct drm_crtc *crtc,
790 			     struct drm_atomic_state *state)
791 {
792 	struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state,
793 									      crtc);
794 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
795 	struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc->state);
796 	struct drm_encoder *encoder;
797 	unsigned long flags;
798 	bool release_bandwidth = false;
799 
800 	DRM_DEBUG_KMS("crtc%d\n", crtc->base.id);
801 
802 	/* Disable/save vblank irq handling */
803 	drm_crtc_vblank_off(crtc);
804 
805 	drm_for_each_encoder_mask(encoder, crtc->dev,
806 				  old_crtc_state->encoder_mask) {
807 		/* in video mode, we hold an extra bandwidth reference
808 		 * as we cannot drop bandwidth at frame-done if any
809 		 * crtc is being used in video mode.
810 		 */
811 		if (dpu_encoder_get_intf_mode(encoder) == INTF_MODE_VIDEO)
812 			release_bandwidth = true;
813 		dpu_encoder_assign_crtc(encoder, NULL);
814 	}
815 
816 	/* wait for frame_event_done completion */
817 	if (_dpu_crtc_wait_for_frame_done(crtc))
818 		DPU_ERROR("crtc%d wait for frame done failed;frame_pending%d\n",
819 				crtc->base.id,
820 				atomic_read(&dpu_crtc->frame_pending));
821 
822 	trace_dpu_crtc_disable(DRMID(crtc), false, dpu_crtc);
823 	dpu_crtc->enabled = false;
824 
825 	if (atomic_read(&dpu_crtc->frame_pending)) {
826 		trace_dpu_crtc_disable_frame_pending(DRMID(crtc),
827 				     atomic_read(&dpu_crtc->frame_pending));
828 		if (release_bandwidth)
829 			dpu_core_perf_crtc_release_bw(crtc);
830 		atomic_set(&dpu_crtc->frame_pending, 0);
831 	}
832 
833 	dpu_core_perf_crtc_update(crtc, 0, true);
834 
835 	drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask)
836 		dpu_encoder_register_frame_event_callback(encoder, NULL, NULL);
837 
838 	memset(cstate->mixers, 0, sizeof(cstate->mixers));
839 	cstate->num_mixers = 0;
840 
841 	/* disable clk & bw control until clk & bw properties are set */
842 	cstate->bw_control = false;
843 	cstate->bw_split_vote = false;
844 
845 	if (crtc->state->event && !crtc->state->active) {
846 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
847 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
848 		crtc->state->event = NULL;
849 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
850 	}
851 
852 	pm_runtime_put_sync(crtc->dev->dev);
853 }
854 
855 static void dpu_crtc_enable(struct drm_crtc *crtc,
856 		struct drm_atomic_state *state)
857 {
858 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
859 	struct drm_encoder *encoder;
860 	bool request_bandwidth = false;
861 
862 	pm_runtime_get_sync(crtc->dev->dev);
863 
864 	DRM_DEBUG_KMS("crtc%d\n", crtc->base.id);
865 
866 	drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask) {
867 		/* in video mode, we hold an extra bandwidth reference
868 		 * as we cannot drop bandwidth at frame-done if any
869 		 * crtc is being used in video mode.
870 		 */
871 		if (dpu_encoder_get_intf_mode(encoder) == INTF_MODE_VIDEO)
872 			request_bandwidth = true;
873 		dpu_encoder_register_frame_event_callback(encoder,
874 				dpu_crtc_frame_event_cb, (void *)crtc);
875 	}
876 
877 	if (request_bandwidth)
878 		atomic_inc(&_dpu_crtc_get_kms(crtc)->bandwidth_ref);
879 
880 	trace_dpu_crtc_enable(DRMID(crtc), true, dpu_crtc);
881 	dpu_crtc->enabled = true;
882 
883 	drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask)
884 		dpu_encoder_assign_crtc(encoder, crtc);
885 
886 	/* Enable/restore vblank irq handling */
887 	drm_crtc_vblank_on(crtc);
888 }
889 
890 struct plane_state {
891 	struct dpu_plane_state *dpu_pstate;
892 	const struct drm_plane_state *drm_pstate;
893 	int stage;
894 	u32 pipe_id;
895 };
896 
897 static int dpu_crtc_atomic_check(struct drm_crtc *crtc,
898 		struct drm_atomic_state *state)
899 {
900 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
901 									  crtc);
902 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
903 	struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc_state);
904 	struct plane_state *pstates;
905 
906 	const struct drm_plane_state *pstate;
907 	struct drm_plane *plane;
908 	struct drm_display_mode *mode;
909 
910 	int cnt = 0, rc = 0, mixer_width = 0, i, z_pos;
911 
912 	struct dpu_multirect_plane_states multirect_plane[DPU_STAGE_MAX * 2];
913 	int multirect_count = 0;
914 	const struct drm_plane_state *pipe_staged[SSPP_MAX];
915 	int left_zpos_cnt = 0, right_zpos_cnt = 0;
916 	struct drm_rect crtc_rect = { 0 };
917 
918 	pstates = kzalloc(sizeof(*pstates) * DPU_STAGE_MAX * 4, GFP_KERNEL);
919 
920 	if (!crtc_state->enable || !crtc_state->active) {
921 		DPU_DEBUG("crtc%d -> enable %d, active %d, skip atomic_check\n",
922 				crtc->base.id, crtc_state->enable,
923 				crtc_state->active);
924 		memset(&cstate->new_perf, 0, sizeof(cstate->new_perf));
925 		goto end;
926 	}
927 
928 	mode = &crtc_state->adjusted_mode;
929 	DPU_DEBUG("%s: check\n", dpu_crtc->name);
930 
931 	/* force a full mode set if active state changed */
932 	if (crtc_state->active_changed)
933 		crtc_state->mode_changed = true;
934 
935 	memset(pipe_staged, 0, sizeof(pipe_staged));
936 
937 	if (cstate->num_mixers) {
938 		mixer_width = mode->hdisplay / cstate->num_mixers;
939 
940 		_dpu_crtc_setup_lm_bounds(crtc, crtc_state);
941 	}
942 
943 	crtc_rect.x2 = mode->hdisplay;
944 	crtc_rect.y2 = mode->vdisplay;
945 
946 	 /* get plane state for all drm planes associated with crtc state */
947 	drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
948 		struct drm_rect dst, clip = crtc_rect;
949 
950 		if (IS_ERR_OR_NULL(pstate)) {
951 			rc = PTR_ERR(pstate);
952 			DPU_ERROR("%s: failed to get plane%d state, %d\n",
953 					dpu_crtc->name, plane->base.id, rc);
954 			goto end;
955 		}
956 		if (cnt >= DPU_STAGE_MAX * 4)
957 			continue;
958 
959 		pstates[cnt].dpu_pstate = to_dpu_plane_state(pstate);
960 		pstates[cnt].drm_pstate = pstate;
961 		pstates[cnt].stage = pstate->normalized_zpos;
962 		pstates[cnt].pipe_id = dpu_plane_pipe(plane);
963 
964 		if (pipe_staged[pstates[cnt].pipe_id]) {
965 			multirect_plane[multirect_count].r0 =
966 				pipe_staged[pstates[cnt].pipe_id];
967 			multirect_plane[multirect_count].r1 = pstate;
968 			multirect_count++;
969 
970 			pipe_staged[pstates[cnt].pipe_id] = NULL;
971 		} else {
972 			pipe_staged[pstates[cnt].pipe_id] = pstate;
973 		}
974 
975 		cnt++;
976 
977 		dst = drm_plane_state_dest(pstate);
978 		if (!drm_rect_intersect(&clip, &dst)) {
979 			DPU_ERROR("invalid vertical/horizontal destination\n");
980 			DPU_ERROR("display: " DRM_RECT_FMT " plane: "
981 				  DRM_RECT_FMT "\n", DRM_RECT_ARG(&crtc_rect),
982 				  DRM_RECT_ARG(&dst));
983 			rc = -E2BIG;
984 			goto end;
985 		}
986 	}
987 
988 	for (i = 1; i < SSPP_MAX; i++) {
989 		if (pipe_staged[i]) {
990 			dpu_plane_clear_multirect(pipe_staged[i]);
991 
992 			if (is_dpu_plane_virtual(pipe_staged[i]->plane)) {
993 				DPU_ERROR(
994 					"r1 only virt plane:%d not supported\n",
995 					pipe_staged[i]->plane->base.id);
996 				rc  = -EINVAL;
997 				goto end;
998 			}
999 		}
1000 	}
1001 
1002 	z_pos = -1;
1003 	for (i = 0; i < cnt; i++) {
1004 		/* reset counts at every new blend stage */
1005 		if (pstates[i].stage != z_pos) {
1006 			left_zpos_cnt = 0;
1007 			right_zpos_cnt = 0;
1008 			z_pos = pstates[i].stage;
1009 		}
1010 
1011 		/* verify z_pos setting before using it */
1012 		if (z_pos >= DPU_STAGE_MAX - DPU_STAGE_0) {
1013 			DPU_ERROR("> %d plane stages assigned\n",
1014 					DPU_STAGE_MAX - DPU_STAGE_0);
1015 			rc = -EINVAL;
1016 			goto end;
1017 		} else if (pstates[i].drm_pstate->crtc_x < mixer_width) {
1018 			if (left_zpos_cnt == 2) {
1019 				DPU_ERROR("> 2 planes @ stage %d on left\n",
1020 					z_pos);
1021 				rc = -EINVAL;
1022 				goto end;
1023 			}
1024 			left_zpos_cnt++;
1025 
1026 		} else {
1027 			if (right_zpos_cnt == 2) {
1028 				DPU_ERROR("> 2 planes @ stage %d on right\n",
1029 					z_pos);
1030 				rc = -EINVAL;
1031 				goto end;
1032 			}
1033 			right_zpos_cnt++;
1034 		}
1035 
1036 		pstates[i].dpu_pstate->stage = z_pos + DPU_STAGE_0;
1037 		DPU_DEBUG("%s: zpos %d\n", dpu_crtc->name, z_pos);
1038 	}
1039 
1040 	for (i = 0; i < multirect_count; i++) {
1041 		if (dpu_plane_validate_multirect_v2(&multirect_plane[i])) {
1042 			DPU_ERROR(
1043 			"multirect validation failed for planes (%d - %d)\n",
1044 					multirect_plane[i].r0->plane->base.id,
1045 					multirect_plane[i].r1->plane->base.id);
1046 			rc = -EINVAL;
1047 			goto end;
1048 		}
1049 	}
1050 
1051 	atomic_inc(&_dpu_crtc_get_kms(crtc)->bandwidth_ref);
1052 
1053 	rc = dpu_core_perf_crtc_check(crtc, crtc_state);
1054 	if (rc) {
1055 		DPU_ERROR("crtc%d failed performance check %d\n",
1056 				crtc->base.id, rc);
1057 		goto end;
1058 	}
1059 
1060 	/* validate source split:
1061 	 * use pstates sorted by stage to check planes on same stage
1062 	 * we assume that all pipes are in source split so its valid to compare
1063 	 * without taking into account left/right mixer placement
1064 	 */
1065 	for (i = 1; i < cnt; i++) {
1066 		struct plane_state *prv_pstate, *cur_pstate;
1067 		struct drm_rect left_rect, right_rect;
1068 		int32_t left_pid, right_pid;
1069 		int32_t stage;
1070 
1071 		prv_pstate = &pstates[i - 1];
1072 		cur_pstate = &pstates[i];
1073 		if (prv_pstate->stage != cur_pstate->stage)
1074 			continue;
1075 
1076 		stage = cur_pstate->stage;
1077 
1078 		left_pid = prv_pstate->dpu_pstate->base.plane->base.id;
1079 		left_rect = drm_plane_state_dest(prv_pstate->drm_pstate);
1080 
1081 		right_pid = cur_pstate->dpu_pstate->base.plane->base.id;
1082 		right_rect = drm_plane_state_dest(cur_pstate->drm_pstate);
1083 
1084 		if (right_rect.x1 < left_rect.x1) {
1085 			swap(left_pid, right_pid);
1086 			swap(left_rect, right_rect);
1087 		}
1088 
1089 		/**
1090 		 * - planes are enumerated in pipe-priority order such that
1091 		 *   planes with lower drm_id must be left-most in a shared
1092 		 *   blend-stage when using source split.
1093 		 * - planes in source split must be contiguous in width
1094 		 * - planes in source split must have same dest yoff and height
1095 		 */
1096 		if (right_pid < left_pid) {
1097 			DPU_ERROR(
1098 				"invalid src split cfg. priority mismatch. stage: %d left: %d right: %d\n",
1099 				stage, left_pid, right_pid);
1100 			rc = -EINVAL;
1101 			goto end;
1102 		} else if (right_rect.x1 != drm_rect_width(&left_rect)) {
1103 			DPU_ERROR("non-contiguous coordinates for src split. "
1104 				  "stage: %d left: " DRM_RECT_FMT " right: "
1105 				  DRM_RECT_FMT "\n", stage,
1106 				  DRM_RECT_ARG(&left_rect),
1107 				  DRM_RECT_ARG(&right_rect));
1108 			rc = -EINVAL;
1109 			goto end;
1110 		} else if (left_rect.y1 != right_rect.y1 ||
1111 			   drm_rect_height(&left_rect) != drm_rect_height(&right_rect)) {
1112 			DPU_ERROR("source split at stage: %d. invalid "
1113 				  "yoff/height: left: " DRM_RECT_FMT " right: "
1114 				  DRM_RECT_FMT "\n", stage,
1115 				  DRM_RECT_ARG(&left_rect),
1116 				  DRM_RECT_ARG(&right_rect));
1117 			rc = -EINVAL;
1118 			goto end;
1119 		}
1120 	}
1121 
1122 end:
1123 	kfree(pstates);
1124 	return rc;
1125 }
1126 
1127 int dpu_crtc_vblank(struct drm_crtc *crtc, bool en)
1128 {
1129 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
1130 	struct drm_encoder *enc;
1131 
1132 	trace_dpu_crtc_vblank(DRMID(&dpu_crtc->base), en, dpu_crtc);
1133 
1134 	/*
1135 	 * Normally we would iterate through encoder_mask in crtc state to find
1136 	 * attached encoders. In this case, we might be disabling vblank _after_
1137 	 * encoder_mask has been cleared.
1138 	 *
1139 	 * Instead, we "assign" a crtc to the encoder in enable and clear it in
1140 	 * disable (which is also after encoder_mask is cleared). So instead of
1141 	 * using encoder mask, we'll ask the encoder to toggle itself iff it's
1142 	 * currently assigned to our crtc.
1143 	 *
1144 	 * Note also that this function cannot be called while crtc is disabled
1145 	 * since we use drm_crtc_vblank_on/off. So we don't need to worry
1146 	 * about the assigned crtcs being inconsistent with the current state
1147 	 * (which means no need to worry about modeset locks).
1148 	 */
1149 	list_for_each_entry(enc, &crtc->dev->mode_config.encoder_list, head) {
1150 		trace_dpu_crtc_vblank_enable(DRMID(crtc), DRMID(enc), en,
1151 					     dpu_crtc);
1152 
1153 		dpu_encoder_toggle_vblank_for_crtc(enc, crtc, en);
1154 	}
1155 
1156 	return 0;
1157 }
1158 
1159 #ifdef CONFIG_DEBUG_FS
1160 static int _dpu_debugfs_status_show(struct seq_file *s, void *data)
1161 {
1162 	struct dpu_crtc *dpu_crtc;
1163 	struct dpu_plane_state *pstate = NULL;
1164 	struct dpu_crtc_mixer *m;
1165 
1166 	struct drm_crtc *crtc;
1167 	struct drm_plane *plane;
1168 	struct drm_display_mode *mode;
1169 	struct drm_framebuffer *fb;
1170 	struct drm_plane_state *state;
1171 	struct dpu_crtc_state *cstate;
1172 
1173 	int i, out_width;
1174 
1175 	dpu_crtc = s->private;
1176 	crtc = &dpu_crtc->base;
1177 
1178 	drm_modeset_lock_all(crtc->dev);
1179 	cstate = to_dpu_crtc_state(crtc->state);
1180 
1181 	mode = &crtc->state->adjusted_mode;
1182 	out_width = mode->hdisplay / cstate->num_mixers;
1183 
1184 	seq_printf(s, "crtc:%d width:%d height:%d\n", crtc->base.id,
1185 				mode->hdisplay, mode->vdisplay);
1186 
1187 	seq_puts(s, "\n");
1188 
1189 	for (i = 0; i < cstate->num_mixers; ++i) {
1190 		m = &cstate->mixers[i];
1191 		seq_printf(s, "\tmixer:%d ctl:%d width:%d height:%d\n",
1192 			m->hw_lm->idx - LM_0, m->lm_ctl->idx - CTL_0,
1193 			out_width, mode->vdisplay);
1194 	}
1195 
1196 	seq_puts(s, "\n");
1197 
1198 	drm_atomic_crtc_for_each_plane(plane, crtc) {
1199 		pstate = to_dpu_plane_state(plane->state);
1200 		state = plane->state;
1201 
1202 		if (!pstate || !state)
1203 			continue;
1204 
1205 		seq_printf(s, "\tplane:%u stage:%d\n", plane->base.id,
1206 			pstate->stage);
1207 
1208 		if (plane->state->fb) {
1209 			fb = plane->state->fb;
1210 
1211 			seq_printf(s, "\tfb:%d image format:%4.4s wxh:%ux%u ",
1212 				fb->base.id, (char *) &fb->format->format,
1213 				fb->width, fb->height);
1214 			for (i = 0; i < ARRAY_SIZE(fb->format->cpp); ++i)
1215 				seq_printf(s, "cpp[%d]:%u ",
1216 						i, fb->format->cpp[i]);
1217 			seq_puts(s, "\n\t");
1218 
1219 			seq_printf(s, "modifier:%8llu ", fb->modifier);
1220 			seq_puts(s, "\n");
1221 
1222 			seq_puts(s, "\t");
1223 			for (i = 0; i < ARRAY_SIZE(fb->pitches); i++)
1224 				seq_printf(s, "pitches[%d]:%8u ", i,
1225 							fb->pitches[i]);
1226 			seq_puts(s, "\n");
1227 
1228 			seq_puts(s, "\t");
1229 			for (i = 0; i < ARRAY_SIZE(fb->offsets); i++)
1230 				seq_printf(s, "offsets[%d]:%8u ", i,
1231 							fb->offsets[i]);
1232 			seq_puts(s, "\n");
1233 		}
1234 
1235 		seq_printf(s, "\tsrc_x:%4d src_y:%4d src_w:%4d src_h:%4d\n",
1236 			state->src_x, state->src_y, state->src_w, state->src_h);
1237 
1238 		seq_printf(s, "\tdst x:%4d dst_y:%4d dst_w:%4d dst_h:%4d\n",
1239 			state->crtc_x, state->crtc_y, state->crtc_w,
1240 			state->crtc_h);
1241 		seq_printf(s, "\tmultirect: mode: %d index: %d\n",
1242 			pstate->multirect_mode, pstate->multirect_index);
1243 
1244 		seq_puts(s, "\n");
1245 	}
1246 	if (dpu_crtc->vblank_cb_count) {
1247 		ktime_t diff = ktime_sub(ktime_get(), dpu_crtc->vblank_cb_time);
1248 		s64 diff_ms = ktime_to_ms(diff);
1249 		s64 fps = diff_ms ? div_s64(
1250 				dpu_crtc->vblank_cb_count * 1000, diff_ms) : 0;
1251 
1252 		seq_printf(s,
1253 			"vblank fps:%lld count:%u total:%llums total_framecount:%llu\n",
1254 				fps, dpu_crtc->vblank_cb_count,
1255 				ktime_to_ms(diff), dpu_crtc->play_count);
1256 
1257 		/* reset time & count for next measurement */
1258 		dpu_crtc->vblank_cb_count = 0;
1259 		dpu_crtc->vblank_cb_time = ktime_set(0, 0);
1260 	}
1261 
1262 	drm_modeset_unlock_all(crtc->dev);
1263 
1264 	return 0;
1265 }
1266 
1267 DEFINE_SHOW_ATTRIBUTE(_dpu_debugfs_status);
1268 
1269 static int dpu_crtc_debugfs_state_show(struct seq_file *s, void *v)
1270 {
1271 	struct drm_crtc *crtc = (struct drm_crtc *) s->private;
1272 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
1273 
1274 	seq_printf(s, "client type: %d\n", dpu_crtc_get_client_type(crtc));
1275 	seq_printf(s, "intf_mode: %d\n", dpu_crtc_get_intf_mode(crtc));
1276 	seq_printf(s, "core_clk_rate: %llu\n",
1277 			dpu_crtc->cur_perf.core_clk_rate);
1278 	seq_printf(s, "bw_ctl: %llu\n", dpu_crtc->cur_perf.bw_ctl);
1279 	seq_printf(s, "max_per_pipe_ib: %llu\n",
1280 				dpu_crtc->cur_perf.max_per_pipe_ib);
1281 
1282 	return 0;
1283 }
1284 DEFINE_SHOW_ATTRIBUTE(dpu_crtc_debugfs_state);
1285 
1286 static int _dpu_crtc_init_debugfs(struct drm_crtc *crtc)
1287 {
1288 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
1289 
1290 	dpu_crtc->debugfs_root = debugfs_create_dir(dpu_crtc->name,
1291 			crtc->dev->primary->debugfs_root);
1292 
1293 	debugfs_create_file("status", 0400,
1294 			dpu_crtc->debugfs_root,
1295 			dpu_crtc, &_dpu_debugfs_status_fops);
1296 	debugfs_create_file("state", 0600,
1297 			dpu_crtc->debugfs_root,
1298 			&dpu_crtc->base,
1299 			&dpu_crtc_debugfs_state_fops);
1300 
1301 	return 0;
1302 }
1303 #else
1304 static int _dpu_crtc_init_debugfs(struct drm_crtc *crtc)
1305 {
1306 	return 0;
1307 }
1308 #endif /* CONFIG_DEBUG_FS */
1309 
1310 static int dpu_crtc_late_register(struct drm_crtc *crtc)
1311 {
1312 	return _dpu_crtc_init_debugfs(crtc);
1313 }
1314 
1315 static void dpu_crtc_early_unregister(struct drm_crtc *crtc)
1316 {
1317 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
1318 
1319 	debugfs_remove_recursive(dpu_crtc->debugfs_root);
1320 }
1321 
1322 static const struct drm_crtc_funcs dpu_crtc_funcs = {
1323 	.set_config = drm_atomic_helper_set_config,
1324 	.destroy = dpu_crtc_destroy,
1325 	.page_flip = drm_atomic_helper_page_flip,
1326 	.reset = dpu_crtc_reset,
1327 	.atomic_duplicate_state = dpu_crtc_duplicate_state,
1328 	.atomic_destroy_state = dpu_crtc_destroy_state,
1329 	.late_register = dpu_crtc_late_register,
1330 	.early_unregister = dpu_crtc_early_unregister,
1331 	.enable_vblank  = msm_crtc_enable_vblank,
1332 	.disable_vblank = msm_crtc_disable_vblank,
1333 	.get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
1334 	.get_vblank_counter = dpu_crtc_get_vblank_counter,
1335 };
1336 
1337 static const struct drm_crtc_helper_funcs dpu_crtc_helper_funcs = {
1338 	.atomic_disable = dpu_crtc_disable,
1339 	.atomic_enable = dpu_crtc_enable,
1340 	.atomic_check = dpu_crtc_atomic_check,
1341 	.atomic_begin = dpu_crtc_atomic_begin,
1342 	.atomic_flush = dpu_crtc_atomic_flush,
1343 	.get_scanout_position = dpu_crtc_get_scanout_position,
1344 };
1345 
1346 /* initialize crtc */
1347 struct drm_crtc *dpu_crtc_init(struct drm_device *dev, struct drm_plane *plane,
1348 				struct drm_plane *cursor)
1349 {
1350 	struct drm_crtc *crtc = NULL;
1351 	struct dpu_crtc *dpu_crtc = NULL;
1352 	int i;
1353 
1354 	dpu_crtc = kzalloc(sizeof(*dpu_crtc), GFP_KERNEL);
1355 	if (!dpu_crtc)
1356 		return ERR_PTR(-ENOMEM);
1357 
1358 	crtc = &dpu_crtc->base;
1359 	crtc->dev = dev;
1360 
1361 	spin_lock_init(&dpu_crtc->spin_lock);
1362 	atomic_set(&dpu_crtc->frame_pending, 0);
1363 
1364 	init_completion(&dpu_crtc->frame_done_comp);
1365 
1366 	INIT_LIST_HEAD(&dpu_crtc->frame_event_list);
1367 
1368 	for (i = 0; i < ARRAY_SIZE(dpu_crtc->frame_events); i++) {
1369 		INIT_LIST_HEAD(&dpu_crtc->frame_events[i].list);
1370 		list_add(&dpu_crtc->frame_events[i].list,
1371 				&dpu_crtc->frame_event_list);
1372 		kthread_init_work(&dpu_crtc->frame_events[i].work,
1373 				dpu_crtc_frame_event_work);
1374 	}
1375 
1376 	drm_crtc_init_with_planes(dev, crtc, plane, cursor, &dpu_crtc_funcs,
1377 				NULL);
1378 
1379 	drm_crtc_helper_add(crtc, &dpu_crtc_helper_funcs);
1380 
1381 	drm_crtc_enable_color_mgmt(crtc, 0, true, 0);
1382 
1383 	/* save user friendly CRTC name for later */
1384 	snprintf(dpu_crtc->name, DPU_CRTC_NAME_SIZE, "crtc%u", crtc->base.id);
1385 
1386 	/* initialize event handling */
1387 	spin_lock_init(&dpu_crtc->event_lock);
1388 
1389 	DPU_DEBUG("%s: successfully initialized crtc\n", dpu_crtc->name);
1390 	return crtc;
1391 }
1392