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 	/* update performance setting before crtc kickoff */
652 	dpu_core_perf_crtc_update(crtc, 1, false);
653 
654 	/*
655 	 * Final plane updates: Give each plane a chance to complete all
656 	 *                      required writes/flushing before crtc's "flush
657 	 *                      everything" call below.
658 	 */
659 	drm_atomic_crtc_for_each_plane(plane, crtc) {
660 		if (dpu_crtc->smmu_state.transition_error)
661 			dpu_plane_set_error(plane, true);
662 		dpu_plane_flush(plane);
663 	}
664 
665 	/* Kickoff will be scheduled by outer layer */
666 }
667 
668 /**
669  * dpu_crtc_destroy_state - state destroy hook
670  * @crtc: drm CRTC
671  * @state: CRTC state object to release
672  */
673 static void dpu_crtc_destroy_state(struct drm_crtc *crtc,
674 		struct drm_crtc_state *state)
675 {
676 	struct dpu_crtc_state *cstate = to_dpu_crtc_state(state);
677 
678 	DPU_DEBUG("crtc%d\n", crtc->base.id);
679 
680 	__drm_atomic_helper_crtc_destroy_state(state);
681 
682 	kfree(cstate);
683 }
684 
685 static int _dpu_crtc_wait_for_frame_done(struct drm_crtc *crtc)
686 {
687 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
688 	int ret, rc = 0;
689 
690 	if (!atomic_read(&dpu_crtc->frame_pending)) {
691 		DPU_DEBUG("no frames pending\n");
692 		return 0;
693 	}
694 
695 	DPU_ATRACE_BEGIN("frame done completion wait");
696 	ret = wait_for_completion_timeout(&dpu_crtc->frame_done_comp,
697 			msecs_to_jiffies(DPU_CRTC_FRAME_DONE_TIMEOUT_MS));
698 	if (!ret) {
699 		DRM_ERROR("frame done wait timed out, ret:%d\n", ret);
700 		rc = -ETIMEDOUT;
701 	}
702 	DPU_ATRACE_END("frame done completion wait");
703 
704 	return rc;
705 }
706 
707 void dpu_crtc_commit_kickoff(struct drm_crtc *crtc)
708 {
709 	struct drm_encoder *encoder;
710 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
711 	struct dpu_kms *dpu_kms = _dpu_crtc_get_kms(crtc);
712 	struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc->state);
713 
714 	/*
715 	 * If no mixers has been allocated in dpu_crtc_atomic_check(),
716 	 * it means we are trying to start a CRTC whose state is disabled:
717 	 * nothing else needs to be done.
718 	 */
719 	if (unlikely(!cstate->num_mixers))
720 		return;
721 
722 	DPU_ATRACE_BEGIN("crtc_commit");
723 
724 	/*
725 	 * Encoder will flush/start now, unless it has a tx pending. If so, it
726 	 * may delay and flush at an irq event (e.g. ppdone)
727 	 */
728 	drm_for_each_encoder_mask(encoder, crtc->dev,
729 				  crtc->state->encoder_mask)
730 		dpu_encoder_prepare_for_kickoff(encoder);
731 
732 	if (atomic_inc_return(&dpu_crtc->frame_pending) == 1) {
733 		/* acquire bandwidth and other resources */
734 		DPU_DEBUG("crtc%d first commit\n", crtc->base.id);
735 	} else
736 		DPU_DEBUG("crtc%d commit\n", crtc->base.id);
737 
738 	dpu_crtc->play_count++;
739 
740 	dpu_vbif_clear_errors(dpu_kms);
741 
742 	drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask)
743 		dpu_encoder_kickoff(encoder);
744 
745 	reinit_completion(&dpu_crtc->frame_done_comp);
746 	DPU_ATRACE_END("crtc_commit");
747 }
748 
749 static void dpu_crtc_reset(struct drm_crtc *crtc)
750 {
751 	struct dpu_crtc_state *cstate = kzalloc(sizeof(*cstate), GFP_KERNEL);
752 
753 	if (crtc->state)
754 		dpu_crtc_destroy_state(crtc, crtc->state);
755 
756 	__drm_atomic_helper_crtc_reset(crtc, &cstate->base);
757 }
758 
759 /**
760  * dpu_crtc_duplicate_state - state duplicate hook
761  * @crtc: Pointer to drm crtc structure
762  */
763 static struct drm_crtc_state *dpu_crtc_duplicate_state(struct drm_crtc *crtc)
764 {
765 	struct dpu_crtc_state *cstate, *old_cstate = to_dpu_crtc_state(crtc->state);
766 
767 	cstate = kmemdup(old_cstate, sizeof(*old_cstate), GFP_KERNEL);
768 	if (!cstate) {
769 		DPU_ERROR("failed to allocate state\n");
770 		return NULL;
771 	}
772 
773 	/* duplicate base helper */
774 	__drm_atomic_helper_crtc_duplicate_state(crtc, &cstate->base);
775 
776 	return &cstate->base;
777 }
778 
779 static void dpu_crtc_disable(struct drm_crtc *crtc,
780 			     struct drm_atomic_state *state)
781 {
782 	struct drm_crtc_state *old_crtc_state = drm_atomic_get_old_crtc_state(state,
783 									      crtc);
784 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
785 	struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc->state);
786 	struct drm_encoder *encoder;
787 	unsigned long flags;
788 	bool release_bandwidth = false;
789 
790 	DRM_DEBUG_KMS("crtc%d\n", crtc->base.id);
791 
792 	/* Disable/save vblank irq handling */
793 	drm_crtc_vblank_off(crtc);
794 
795 	drm_for_each_encoder_mask(encoder, crtc->dev,
796 				  old_crtc_state->encoder_mask) {
797 		/* in video mode, we hold an extra bandwidth reference
798 		 * as we cannot drop bandwidth at frame-done if any
799 		 * crtc is being used in video mode.
800 		 */
801 		if (dpu_encoder_get_intf_mode(encoder) == INTF_MODE_VIDEO)
802 			release_bandwidth = true;
803 		dpu_encoder_assign_crtc(encoder, NULL);
804 	}
805 
806 	/* wait for frame_event_done completion */
807 	if (_dpu_crtc_wait_for_frame_done(crtc))
808 		DPU_ERROR("crtc%d wait for frame done failed;frame_pending%d\n",
809 				crtc->base.id,
810 				atomic_read(&dpu_crtc->frame_pending));
811 
812 	trace_dpu_crtc_disable(DRMID(crtc), false, dpu_crtc);
813 	dpu_crtc->enabled = false;
814 
815 	if (atomic_read(&dpu_crtc->frame_pending)) {
816 		trace_dpu_crtc_disable_frame_pending(DRMID(crtc),
817 				     atomic_read(&dpu_crtc->frame_pending));
818 		if (release_bandwidth)
819 			dpu_core_perf_crtc_release_bw(crtc);
820 		atomic_set(&dpu_crtc->frame_pending, 0);
821 	}
822 
823 	dpu_core_perf_crtc_update(crtc, 0, true);
824 
825 	drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask)
826 		dpu_encoder_register_frame_event_callback(encoder, NULL, NULL);
827 
828 	memset(cstate->mixers, 0, sizeof(cstate->mixers));
829 	cstate->num_mixers = 0;
830 
831 	/* disable clk & bw control until clk & bw properties are set */
832 	cstate->bw_control = false;
833 	cstate->bw_split_vote = false;
834 
835 	if (crtc->state->event && !crtc->state->active) {
836 		spin_lock_irqsave(&crtc->dev->event_lock, flags);
837 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
838 		crtc->state->event = NULL;
839 		spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
840 	}
841 
842 	pm_runtime_put_sync(crtc->dev->dev);
843 }
844 
845 static void dpu_crtc_enable(struct drm_crtc *crtc,
846 		struct drm_atomic_state *state)
847 {
848 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
849 	struct drm_encoder *encoder;
850 	bool request_bandwidth = false;
851 
852 	pm_runtime_get_sync(crtc->dev->dev);
853 
854 	DRM_DEBUG_KMS("crtc%d\n", crtc->base.id);
855 
856 	drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask) {
857 		/* in video mode, we hold an extra bandwidth reference
858 		 * as we cannot drop bandwidth at frame-done if any
859 		 * crtc is being used in video mode.
860 		 */
861 		if (dpu_encoder_get_intf_mode(encoder) == INTF_MODE_VIDEO)
862 			request_bandwidth = true;
863 		dpu_encoder_register_frame_event_callback(encoder,
864 				dpu_crtc_frame_event_cb, (void *)crtc);
865 	}
866 
867 	if (request_bandwidth)
868 		atomic_inc(&_dpu_crtc_get_kms(crtc)->bandwidth_ref);
869 
870 	trace_dpu_crtc_enable(DRMID(crtc), true, dpu_crtc);
871 	dpu_crtc->enabled = true;
872 
873 	drm_for_each_encoder_mask(encoder, crtc->dev, crtc->state->encoder_mask)
874 		dpu_encoder_assign_crtc(encoder, crtc);
875 
876 	/* Enable/restore vblank irq handling */
877 	drm_crtc_vblank_on(crtc);
878 }
879 
880 struct plane_state {
881 	struct dpu_plane_state *dpu_pstate;
882 	const struct drm_plane_state *drm_pstate;
883 	int stage;
884 	u32 pipe_id;
885 };
886 
887 static int dpu_crtc_atomic_check(struct drm_crtc *crtc,
888 		struct drm_atomic_state *state)
889 {
890 	struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
891 									  crtc);
892 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
893 	struct dpu_crtc_state *cstate = to_dpu_crtc_state(crtc_state);
894 	struct plane_state *pstates;
895 
896 	const struct drm_plane_state *pstate;
897 	struct drm_plane *plane;
898 	struct drm_display_mode *mode;
899 
900 	int cnt = 0, rc = 0, mixer_width = 0, i, z_pos;
901 
902 	struct dpu_multirect_plane_states multirect_plane[DPU_STAGE_MAX * 2];
903 	int multirect_count = 0;
904 	const struct drm_plane_state *pipe_staged[SSPP_MAX];
905 	int left_zpos_cnt = 0, right_zpos_cnt = 0;
906 	struct drm_rect crtc_rect = { 0 };
907 
908 	pstates = kzalloc(sizeof(*pstates) * DPU_STAGE_MAX * 4, GFP_KERNEL);
909 
910 	if (!crtc_state->enable || !crtc_state->active) {
911 		DPU_DEBUG("crtc%d -> enable %d, active %d, skip atomic_check\n",
912 				crtc->base.id, crtc_state->enable,
913 				crtc_state->active);
914 		memset(&cstate->new_perf, 0, sizeof(cstate->new_perf));
915 		goto end;
916 	}
917 
918 	mode = &crtc_state->adjusted_mode;
919 	DPU_DEBUG("%s: check\n", dpu_crtc->name);
920 
921 	/* force a full mode set if active state changed */
922 	if (crtc_state->active_changed)
923 		crtc_state->mode_changed = true;
924 
925 	memset(pipe_staged, 0, sizeof(pipe_staged));
926 
927 	if (cstate->num_mixers) {
928 		mixer_width = mode->hdisplay / cstate->num_mixers;
929 
930 		_dpu_crtc_setup_lm_bounds(crtc, crtc_state);
931 	}
932 
933 	crtc_rect.x2 = mode->hdisplay;
934 	crtc_rect.y2 = mode->vdisplay;
935 
936 	 /* get plane state for all drm planes associated with crtc state */
937 	drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
938 		struct drm_rect dst, clip = crtc_rect;
939 
940 		if (IS_ERR_OR_NULL(pstate)) {
941 			rc = PTR_ERR(pstate);
942 			DPU_ERROR("%s: failed to get plane%d state, %d\n",
943 					dpu_crtc->name, plane->base.id, rc);
944 			goto end;
945 		}
946 		if (cnt >= DPU_STAGE_MAX * 4)
947 			continue;
948 
949 		pstates[cnt].dpu_pstate = to_dpu_plane_state(pstate);
950 		pstates[cnt].drm_pstate = pstate;
951 		pstates[cnt].stage = pstate->normalized_zpos;
952 		pstates[cnt].pipe_id = dpu_plane_pipe(plane);
953 
954 		if (pipe_staged[pstates[cnt].pipe_id]) {
955 			multirect_plane[multirect_count].r0 =
956 				pipe_staged[pstates[cnt].pipe_id];
957 			multirect_plane[multirect_count].r1 = pstate;
958 			multirect_count++;
959 
960 			pipe_staged[pstates[cnt].pipe_id] = NULL;
961 		} else {
962 			pipe_staged[pstates[cnt].pipe_id] = pstate;
963 		}
964 
965 		cnt++;
966 
967 		dst = drm_plane_state_dest(pstate);
968 		if (!drm_rect_intersect(&clip, &dst)) {
969 			DPU_ERROR("invalid vertical/horizontal destination\n");
970 			DPU_ERROR("display: " DRM_RECT_FMT " plane: "
971 				  DRM_RECT_FMT "\n", DRM_RECT_ARG(&crtc_rect),
972 				  DRM_RECT_ARG(&dst));
973 			rc = -E2BIG;
974 			goto end;
975 		}
976 	}
977 
978 	for (i = 1; i < SSPP_MAX; i++) {
979 		if (pipe_staged[i]) {
980 			dpu_plane_clear_multirect(pipe_staged[i]);
981 
982 			if (is_dpu_plane_virtual(pipe_staged[i]->plane)) {
983 				DPU_ERROR(
984 					"r1 only virt plane:%d not supported\n",
985 					pipe_staged[i]->plane->base.id);
986 				rc  = -EINVAL;
987 				goto end;
988 			}
989 		}
990 	}
991 
992 	z_pos = -1;
993 	for (i = 0; i < cnt; i++) {
994 		/* reset counts at every new blend stage */
995 		if (pstates[i].stage != z_pos) {
996 			left_zpos_cnt = 0;
997 			right_zpos_cnt = 0;
998 			z_pos = pstates[i].stage;
999 		}
1000 
1001 		/* verify z_pos setting before using it */
1002 		if (z_pos >= DPU_STAGE_MAX - DPU_STAGE_0) {
1003 			DPU_ERROR("> %d plane stages assigned\n",
1004 					DPU_STAGE_MAX - DPU_STAGE_0);
1005 			rc = -EINVAL;
1006 			goto end;
1007 		} else if (pstates[i].drm_pstate->crtc_x < mixer_width) {
1008 			if (left_zpos_cnt == 2) {
1009 				DPU_ERROR("> 2 planes @ stage %d on left\n",
1010 					z_pos);
1011 				rc = -EINVAL;
1012 				goto end;
1013 			}
1014 			left_zpos_cnt++;
1015 
1016 		} else {
1017 			if (right_zpos_cnt == 2) {
1018 				DPU_ERROR("> 2 planes @ stage %d on right\n",
1019 					z_pos);
1020 				rc = -EINVAL;
1021 				goto end;
1022 			}
1023 			right_zpos_cnt++;
1024 		}
1025 
1026 		pstates[i].dpu_pstate->stage = z_pos + DPU_STAGE_0;
1027 		DPU_DEBUG("%s: zpos %d\n", dpu_crtc->name, z_pos);
1028 	}
1029 
1030 	for (i = 0; i < multirect_count; i++) {
1031 		if (dpu_plane_validate_multirect_v2(&multirect_plane[i])) {
1032 			DPU_ERROR(
1033 			"multirect validation failed for planes (%d - %d)\n",
1034 					multirect_plane[i].r0->plane->base.id,
1035 					multirect_plane[i].r1->plane->base.id);
1036 			rc = -EINVAL;
1037 			goto end;
1038 		}
1039 	}
1040 
1041 	atomic_inc(&_dpu_crtc_get_kms(crtc)->bandwidth_ref);
1042 
1043 	rc = dpu_core_perf_crtc_check(crtc, crtc_state);
1044 	if (rc) {
1045 		DPU_ERROR("crtc%d failed performance check %d\n",
1046 				crtc->base.id, rc);
1047 		goto end;
1048 	}
1049 
1050 	/* validate source split:
1051 	 * use pstates sorted by stage to check planes on same stage
1052 	 * we assume that all pipes are in source split so its valid to compare
1053 	 * without taking into account left/right mixer placement
1054 	 */
1055 	for (i = 1; i < cnt; i++) {
1056 		struct plane_state *prv_pstate, *cur_pstate;
1057 		struct drm_rect left_rect, right_rect;
1058 		int32_t left_pid, right_pid;
1059 		int32_t stage;
1060 
1061 		prv_pstate = &pstates[i - 1];
1062 		cur_pstate = &pstates[i];
1063 		if (prv_pstate->stage != cur_pstate->stage)
1064 			continue;
1065 
1066 		stage = cur_pstate->stage;
1067 
1068 		left_pid = prv_pstate->dpu_pstate->base.plane->base.id;
1069 		left_rect = drm_plane_state_dest(prv_pstate->drm_pstate);
1070 
1071 		right_pid = cur_pstate->dpu_pstate->base.plane->base.id;
1072 		right_rect = drm_plane_state_dest(cur_pstate->drm_pstate);
1073 
1074 		if (right_rect.x1 < left_rect.x1) {
1075 			swap(left_pid, right_pid);
1076 			swap(left_rect, right_rect);
1077 		}
1078 
1079 		/**
1080 		 * - planes are enumerated in pipe-priority order such that
1081 		 *   planes with lower drm_id must be left-most in a shared
1082 		 *   blend-stage when using source split.
1083 		 * - planes in source split must be contiguous in width
1084 		 * - planes in source split must have same dest yoff and height
1085 		 */
1086 		if (right_pid < left_pid) {
1087 			DPU_ERROR(
1088 				"invalid src split cfg. priority mismatch. stage: %d left: %d right: %d\n",
1089 				stage, left_pid, right_pid);
1090 			rc = -EINVAL;
1091 			goto end;
1092 		} else if (right_rect.x1 != drm_rect_width(&left_rect)) {
1093 			DPU_ERROR("non-contiguous coordinates for src split. "
1094 				  "stage: %d left: " DRM_RECT_FMT " right: "
1095 				  DRM_RECT_FMT "\n", stage,
1096 				  DRM_RECT_ARG(&left_rect),
1097 				  DRM_RECT_ARG(&right_rect));
1098 			rc = -EINVAL;
1099 			goto end;
1100 		} else if (left_rect.y1 != right_rect.y1 ||
1101 			   drm_rect_height(&left_rect) != drm_rect_height(&right_rect)) {
1102 			DPU_ERROR("source split at stage: %d. invalid "
1103 				  "yoff/height: left: " DRM_RECT_FMT " right: "
1104 				  DRM_RECT_FMT "\n", stage,
1105 				  DRM_RECT_ARG(&left_rect),
1106 				  DRM_RECT_ARG(&right_rect));
1107 			rc = -EINVAL;
1108 			goto end;
1109 		}
1110 	}
1111 
1112 end:
1113 	kfree(pstates);
1114 	return rc;
1115 }
1116 
1117 int dpu_crtc_vblank(struct drm_crtc *crtc, bool en)
1118 {
1119 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
1120 	struct drm_encoder *enc;
1121 
1122 	trace_dpu_crtc_vblank(DRMID(&dpu_crtc->base), en, dpu_crtc);
1123 
1124 	/*
1125 	 * Normally we would iterate through encoder_mask in crtc state to find
1126 	 * attached encoders. In this case, we might be disabling vblank _after_
1127 	 * encoder_mask has been cleared.
1128 	 *
1129 	 * Instead, we "assign" a crtc to the encoder in enable and clear it in
1130 	 * disable (which is also after encoder_mask is cleared). So instead of
1131 	 * using encoder mask, we'll ask the encoder to toggle itself iff it's
1132 	 * currently assigned to our crtc.
1133 	 *
1134 	 * Note also that this function cannot be called while crtc is disabled
1135 	 * since we use drm_crtc_vblank_on/off. So we don't need to worry
1136 	 * about the assigned crtcs being inconsistent with the current state
1137 	 * (which means no need to worry about modeset locks).
1138 	 */
1139 	list_for_each_entry(enc, &crtc->dev->mode_config.encoder_list, head) {
1140 		trace_dpu_crtc_vblank_enable(DRMID(crtc), DRMID(enc), en,
1141 					     dpu_crtc);
1142 
1143 		dpu_encoder_toggle_vblank_for_crtc(enc, crtc, en);
1144 	}
1145 
1146 	return 0;
1147 }
1148 
1149 #ifdef CONFIG_DEBUG_FS
1150 static int _dpu_debugfs_status_show(struct seq_file *s, void *data)
1151 {
1152 	struct dpu_crtc *dpu_crtc;
1153 	struct dpu_plane_state *pstate = NULL;
1154 	struct dpu_crtc_mixer *m;
1155 
1156 	struct drm_crtc *crtc;
1157 	struct drm_plane *plane;
1158 	struct drm_display_mode *mode;
1159 	struct drm_framebuffer *fb;
1160 	struct drm_plane_state *state;
1161 	struct dpu_crtc_state *cstate;
1162 
1163 	int i, out_width;
1164 
1165 	dpu_crtc = s->private;
1166 	crtc = &dpu_crtc->base;
1167 
1168 	drm_modeset_lock_all(crtc->dev);
1169 	cstate = to_dpu_crtc_state(crtc->state);
1170 
1171 	mode = &crtc->state->adjusted_mode;
1172 	out_width = mode->hdisplay / cstate->num_mixers;
1173 
1174 	seq_printf(s, "crtc:%d width:%d height:%d\n", crtc->base.id,
1175 				mode->hdisplay, mode->vdisplay);
1176 
1177 	seq_puts(s, "\n");
1178 
1179 	for (i = 0; i < cstate->num_mixers; ++i) {
1180 		m = &cstate->mixers[i];
1181 		seq_printf(s, "\tmixer:%d ctl:%d width:%d height:%d\n",
1182 			m->hw_lm->idx - LM_0, m->lm_ctl->idx - CTL_0,
1183 			out_width, mode->vdisplay);
1184 	}
1185 
1186 	seq_puts(s, "\n");
1187 
1188 	drm_atomic_crtc_for_each_plane(plane, crtc) {
1189 		pstate = to_dpu_plane_state(plane->state);
1190 		state = plane->state;
1191 
1192 		if (!pstate || !state)
1193 			continue;
1194 
1195 		seq_printf(s, "\tplane:%u stage:%d\n", plane->base.id,
1196 			pstate->stage);
1197 
1198 		if (plane->state->fb) {
1199 			fb = plane->state->fb;
1200 
1201 			seq_printf(s, "\tfb:%d image format:%4.4s wxh:%ux%u ",
1202 				fb->base.id, (char *) &fb->format->format,
1203 				fb->width, fb->height);
1204 			for (i = 0; i < ARRAY_SIZE(fb->format->cpp); ++i)
1205 				seq_printf(s, "cpp[%d]:%u ",
1206 						i, fb->format->cpp[i]);
1207 			seq_puts(s, "\n\t");
1208 
1209 			seq_printf(s, "modifier:%8llu ", fb->modifier);
1210 			seq_puts(s, "\n");
1211 
1212 			seq_puts(s, "\t");
1213 			for (i = 0; i < ARRAY_SIZE(fb->pitches); i++)
1214 				seq_printf(s, "pitches[%d]:%8u ", i,
1215 							fb->pitches[i]);
1216 			seq_puts(s, "\n");
1217 
1218 			seq_puts(s, "\t");
1219 			for (i = 0; i < ARRAY_SIZE(fb->offsets); i++)
1220 				seq_printf(s, "offsets[%d]:%8u ", i,
1221 							fb->offsets[i]);
1222 			seq_puts(s, "\n");
1223 		}
1224 
1225 		seq_printf(s, "\tsrc_x:%4d src_y:%4d src_w:%4d src_h:%4d\n",
1226 			state->src_x, state->src_y, state->src_w, state->src_h);
1227 
1228 		seq_printf(s, "\tdst x:%4d dst_y:%4d dst_w:%4d dst_h:%4d\n",
1229 			state->crtc_x, state->crtc_y, state->crtc_w,
1230 			state->crtc_h);
1231 		seq_printf(s, "\tmultirect: mode: %d index: %d\n",
1232 			pstate->multirect_mode, pstate->multirect_index);
1233 
1234 		seq_puts(s, "\n");
1235 	}
1236 	if (dpu_crtc->vblank_cb_count) {
1237 		ktime_t diff = ktime_sub(ktime_get(), dpu_crtc->vblank_cb_time);
1238 		s64 diff_ms = ktime_to_ms(diff);
1239 		s64 fps = diff_ms ? div_s64(
1240 				dpu_crtc->vblank_cb_count * 1000, diff_ms) : 0;
1241 
1242 		seq_printf(s,
1243 			"vblank fps:%lld count:%u total:%llums total_framecount:%llu\n",
1244 				fps, dpu_crtc->vblank_cb_count,
1245 				ktime_to_ms(diff), dpu_crtc->play_count);
1246 
1247 		/* reset time & count for next measurement */
1248 		dpu_crtc->vblank_cb_count = 0;
1249 		dpu_crtc->vblank_cb_time = ktime_set(0, 0);
1250 	}
1251 
1252 	drm_modeset_unlock_all(crtc->dev);
1253 
1254 	return 0;
1255 }
1256 
1257 DEFINE_SHOW_ATTRIBUTE(_dpu_debugfs_status);
1258 
1259 static int dpu_crtc_debugfs_state_show(struct seq_file *s, void *v)
1260 {
1261 	struct drm_crtc *crtc = (struct drm_crtc *) s->private;
1262 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
1263 
1264 	seq_printf(s, "client type: %d\n", dpu_crtc_get_client_type(crtc));
1265 	seq_printf(s, "intf_mode: %d\n", dpu_crtc_get_intf_mode(crtc));
1266 	seq_printf(s, "core_clk_rate: %llu\n",
1267 			dpu_crtc->cur_perf.core_clk_rate);
1268 	seq_printf(s, "bw_ctl: %llu\n", dpu_crtc->cur_perf.bw_ctl);
1269 	seq_printf(s, "max_per_pipe_ib: %llu\n",
1270 				dpu_crtc->cur_perf.max_per_pipe_ib);
1271 
1272 	return 0;
1273 }
1274 DEFINE_SHOW_ATTRIBUTE(dpu_crtc_debugfs_state);
1275 
1276 static int _dpu_crtc_init_debugfs(struct drm_crtc *crtc)
1277 {
1278 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
1279 
1280 	dpu_crtc->debugfs_root = debugfs_create_dir(dpu_crtc->name,
1281 			crtc->dev->primary->debugfs_root);
1282 
1283 	debugfs_create_file("status", 0400,
1284 			dpu_crtc->debugfs_root,
1285 			dpu_crtc, &_dpu_debugfs_status_fops);
1286 	debugfs_create_file("state", 0600,
1287 			dpu_crtc->debugfs_root,
1288 			&dpu_crtc->base,
1289 			&dpu_crtc_debugfs_state_fops);
1290 
1291 	return 0;
1292 }
1293 #else
1294 static int _dpu_crtc_init_debugfs(struct drm_crtc *crtc)
1295 {
1296 	return 0;
1297 }
1298 #endif /* CONFIG_DEBUG_FS */
1299 
1300 static int dpu_crtc_late_register(struct drm_crtc *crtc)
1301 {
1302 	return _dpu_crtc_init_debugfs(crtc);
1303 }
1304 
1305 static void dpu_crtc_early_unregister(struct drm_crtc *crtc)
1306 {
1307 	struct dpu_crtc *dpu_crtc = to_dpu_crtc(crtc);
1308 
1309 	debugfs_remove_recursive(dpu_crtc->debugfs_root);
1310 }
1311 
1312 static const struct drm_crtc_funcs dpu_crtc_funcs = {
1313 	.set_config = drm_atomic_helper_set_config,
1314 	.destroy = dpu_crtc_destroy,
1315 	.page_flip = drm_atomic_helper_page_flip,
1316 	.reset = dpu_crtc_reset,
1317 	.atomic_duplicate_state = dpu_crtc_duplicate_state,
1318 	.atomic_destroy_state = dpu_crtc_destroy_state,
1319 	.late_register = dpu_crtc_late_register,
1320 	.early_unregister = dpu_crtc_early_unregister,
1321 	.enable_vblank  = msm_crtc_enable_vblank,
1322 	.disable_vblank = msm_crtc_disable_vblank,
1323 	.get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp,
1324 	.get_vblank_counter = dpu_crtc_get_vblank_counter,
1325 };
1326 
1327 static const struct drm_crtc_helper_funcs dpu_crtc_helper_funcs = {
1328 	.atomic_disable = dpu_crtc_disable,
1329 	.atomic_enable = dpu_crtc_enable,
1330 	.atomic_check = dpu_crtc_atomic_check,
1331 	.atomic_begin = dpu_crtc_atomic_begin,
1332 	.atomic_flush = dpu_crtc_atomic_flush,
1333 	.get_scanout_position = dpu_crtc_get_scanout_position,
1334 };
1335 
1336 /* initialize crtc */
1337 struct drm_crtc *dpu_crtc_init(struct drm_device *dev, struct drm_plane *plane,
1338 				struct drm_plane *cursor)
1339 {
1340 	struct drm_crtc *crtc = NULL;
1341 	struct dpu_crtc *dpu_crtc = NULL;
1342 	int i;
1343 
1344 	dpu_crtc = kzalloc(sizeof(*dpu_crtc), GFP_KERNEL);
1345 	if (!dpu_crtc)
1346 		return ERR_PTR(-ENOMEM);
1347 
1348 	crtc = &dpu_crtc->base;
1349 	crtc->dev = dev;
1350 
1351 	spin_lock_init(&dpu_crtc->spin_lock);
1352 	atomic_set(&dpu_crtc->frame_pending, 0);
1353 
1354 	init_completion(&dpu_crtc->frame_done_comp);
1355 
1356 	INIT_LIST_HEAD(&dpu_crtc->frame_event_list);
1357 
1358 	for (i = 0; i < ARRAY_SIZE(dpu_crtc->frame_events); i++) {
1359 		INIT_LIST_HEAD(&dpu_crtc->frame_events[i].list);
1360 		list_add(&dpu_crtc->frame_events[i].list,
1361 				&dpu_crtc->frame_event_list);
1362 		kthread_init_work(&dpu_crtc->frame_events[i].work,
1363 				dpu_crtc_frame_event_work);
1364 	}
1365 
1366 	drm_crtc_init_with_planes(dev, crtc, plane, cursor, &dpu_crtc_funcs,
1367 				NULL);
1368 
1369 	drm_crtc_helper_add(crtc, &dpu_crtc_helper_funcs);
1370 
1371 	drm_crtc_enable_color_mgmt(crtc, 0, true, 0);
1372 
1373 	/* save user friendly CRTC name for later */
1374 	snprintf(dpu_crtc->name, DPU_CRTC_NAME_SIZE, "crtc%u", crtc->base.id);
1375 
1376 	/* initialize event handling */
1377 	spin_lock_init(&dpu_crtc->event_lock);
1378 
1379 	DPU_DEBUG("%s: successfully initialized crtc\n", dpu_crtc->name);
1380 	return crtc;
1381 }
1382