xref: /linux/drivers/gpu/drm/msm/disp/mdp5/mdp5_crtc.c (revision ebf68996de0ab250c5d520eb2291ab65643e9a1e)
1 /*
2  * Copyright (c) 2014-2015 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 #include <linux/sort.h>
20 #include <drm/drm_mode.h>
21 #include <drm/drm_crtc.h>
22 #include <drm/drm_flip_work.h>
23 #include <drm/drm_probe_helper.h>
24 
25 #include "mdp5_kms.h"
26 
27 #define CURSOR_WIDTH	64
28 #define CURSOR_HEIGHT	64
29 
30 struct mdp5_crtc {
31 	struct drm_crtc base;
32 	int id;
33 	bool enabled;
34 
35 	spinlock_t lm_lock;     /* protect REG_MDP5_LM_* registers */
36 
37 	/* if there is a pending flip, these will be non-null: */
38 	struct drm_pending_vblank_event *event;
39 
40 	/* Bits have been flushed at the last commit,
41 	 * used to decide if a vsync has happened since last commit.
42 	 */
43 	u32 flushed_mask;
44 
45 #define PENDING_CURSOR 0x1
46 #define PENDING_FLIP   0x2
47 	atomic_t pending;
48 
49 	/* for unref'ing cursor bo's after scanout completes: */
50 	struct drm_flip_work unref_cursor_work;
51 
52 	struct mdp_irq vblank;
53 	struct mdp_irq err;
54 	struct mdp_irq pp_done;
55 
56 	struct completion pp_completion;
57 
58 	bool lm_cursor_enabled;
59 
60 	struct {
61 		/* protect REG_MDP5_LM_CURSOR* registers and cursor scanout_bo*/
62 		spinlock_t lock;
63 
64 		/* current cursor being scanned out: */
65 		struct drm_gem_object *scanout_bo;
66 		uint64_t iova;
67 		uint32_t width, height;
68 		int x, y;
69 	} cursor;
70 };
71 #define to_mdp5_crtc(x) container_of(x, struct mdp5_crtc, base)
72 
73 static void mdp5_crtc_restore_cursor(struct drm_crtc *crtc);
74 
75 static struct mdp5_kms *get_kms(struct drm_crtc *crtc)
76 {
77 	struct msm_drm_private *priv = crtc->dev->dev_private;
78 	return to_mdp5_kms(to_mdp_kms(priv->kms));
79 }
80 
81 static void request_pending(struct drm_crtc *crtc, uint32_t pending)
82 {
83 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
84 
85 	atomic_or(pending, &mdp5_crtc->pending);
86 	mdp_irq_register(&get_kms(crtc)->base, &mdp5_crtc->vblank);
87 }
88 
89 static void request_pp_done_pending(struct drm_crtc *crtc)
90 {
91 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
92 	reinit_completion(&mdp5_crtc->pp_completion);
93 }
94 
95 static u32 crtc_flush(struct drm_crtc *crtc, u32 flush_mask)
96 {
97 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
98 	struct mdp5_ctl *ctl = mdp5_cstate->ctl;
99 	struct mdp5_pipeline *pipeline = &mdp5_cstate->pipeline;
100 	bool start = !mdp5_cstate->defer_start;
101 
102 	mdp5_cstate->defer_start = false;
103 
104 	DBG("%s: flush=%08x", crtc->name, flush_mask);
105 
106 	return mdp5_ctl_commit(ctl, pipeline, flush_mask, start);
107 }
108 
109 /*
110  * flush updates, to make sure hw is updated to new scanout fb,
111  * so that we can safely queue unref to current fb (ie. next
112  * vblank we know hw is done w/ previous scanout_fb).
113  */
114 static u32 crtc_flush_all(struct drm_crtc *crtc)
115 {
116 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
117 	struct mdp5_hw_mixer *mixer, *r_mixer;
118 	struct drm_plane *plane;
119 	uint32_t flush_mask = 0;
120 
121 	/* this should not happen: */
122 	if (WARN_ON(!mdp5_cstate->ctl))
123 		return 0;
124 
125 	drm_atomic_crtc_for_each_plane(plane, crtc) {
126 		if (!plane->state->visible)
127 			continue;
128 		flush_mask |= mdp5_plane_get_flush(plane);
129 	}
130 
131 	mixer = mdp5_cstate->pipeline.mixer;
132 	flush_mask |= mdp_ctl_flush_mask_lm(mixer->lm);
133 
134 	r_mixer = mdp5_cstate->pipeline.r_mixer;
135 	if (r_mixer)
136 		flush_mask |= mdp_ctl_flush_mask_lm(r_mixer->lm);
137 
138 	return crtc_flush(crtc, flush_mask);
139 }
140 
141 /* if file!=NULL, this is preclose potential cancel-flip path */
142 static void complete_flip(struct drm_crtc *crtc, struct drm_file *file)
143 {
144 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
145 	struct mdp5_pipeline *pipeline = &mdp5_cstate->pipeline;
146 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
147 	struct mdp5_ctl *ctl = mdp5_cstate->ctl;
148 	struct drm_device *dev = crtc->dev;
149 	struct drm_pending_vblank_event *event;
150 	unsigned long flags;
151 
152 	spin_lock_irqsave(&dev->event_lock, flags);
153 	event = mdp5_crtc->event;
154 	if (event) {
155 		mdp5_crtc->event = NULL;
156 		DBG("%s: send event: %p", crtc->name, event);
157 		drm_crtc_send_vblank_event(crtc, event);
158 	}
159 	spin_unlock_irqrestore(&dev->event_lock, flags);
160 
161 	if (ctl && !crtc->state->enable) {
162 		/* set STAGE_UNUSED for all layers */
163 		mdp5_ctl_blend(ctl, pipeline, NULL, NULL, 0, 0);
164 		/* XXX: What to do here? */
165 		/* mdp5_crtc->ctl = NULL; */
166 	}
167 }
168 
169 static void unref_cursor_worker(struct drm_flip_work *work, void *val)
170 {
171 	struct mdp5_crtc *mdp5_crtc =
172 		container_of(work, struct mdp5_crtc, unref_cursor_work);
173 	struct mdp5_kms *mdp5_kms = get_kms(&mdp5_crtc->base);
174 	struct msm_kms *kms = &mdp5_kms->base.base;
175 
176 	msm_gem_unpin_iova(val, kms->aspace);
177 	drm_gem_object_put_unlocked(val);
178 }
179 
180 static void mdp5_crtc_destroy(struct drm_crtc *crtc)
181 {
182 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
183 
184 	drm_crtc_cleanup(crtc);
185 	drm_flip_work_cleanup(&mdp5_crtc->unref_cursor_work);
186 
187 	kfree(mdp5_crtc);
188 }
189 
190 static inline u32 mdp5_lm_use_fg_alpha_mask(enum mdp_mixer_stage_id stage)
191 {
192 	switch (stage) {
193 	case STAGE0: return MDP5_LM_BLEND_COLOR_OUT_STAGE0_FG_ALPHA;
194 	case STAGE1: return MDP5_LM_BLEND_COLOR_OUT_STAGE1_FG_ALPHA;
195 	case STAGE2: return MDP5_LM_BLEND_COLOR_OUT_STAGE2_FG_ALPHA;
196 	case STAGE3: return MDP5_LM_BLEND_COLOR_OUT_STAGE3_FG_ALPHA;
197 	case STAGE4: return MDP5_LM_BLEND_COLOR_OUT_STAGE4_FG_ALPHA;
198 	case STAGE5: return MDP5_LM_BLEND_COLOR_OUT_STAGE5_FG_ALPHA;
199 	case STAGE6: return MDP5_LM_BLEND_COLOR_OUT_STAGE6_FG_ALPHA;
200 	default:
201 		return 0;
202 	}
203 }
204 
205 /*
206  * left/right pipe offsets for the stage array used in blend_setup()
207  */
208 #define PIPE_LEFT	0
209 #define PIPE_RIGHT	1
210 
211 /*
212  * blend_setup() - blend all the planes of a CRTC
213  *
214  * If no base layer is available, border will be enabled as the base layer.
215  * Otherwise all layers will be blended based on their stage calculated
216  * in mdp5_crtc_atomic_check.
217  */
218 static void blend_setup(struct drm_crtc *crtc)
219 {
220 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
221 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
222 	struct mdp5_pipeline *pipeline = &mdp5_cstate->pipeline;
223 	struct mdp5_kms *mdp5_kms = get_kms(crtc);
224 	struct drm_plane *plane;
225 	const struct mdp5_cfg_hw *hw_cfg;
226 	struct mdp5_plane_state *pstate, *pstates[STAGE_MAX + 1] = {NULL};
227 	const struct mdp_format *format;
228 	struct mdp5_hw_mixer *mixer = pipeline->mixer;
229 	uint32_t lm = mixer->lm;
230 	struct mdp5_hw_mixer *r_mixer = pipeline->r_mixer;
231 	uint32_t r_lm = r_mixer ? r_mixer->lm : 0;
232 	struct mdp5_ctl *ctl = mdp5_cstate->ctl;
233 	uint32_t blend_op, fg_alpha, bg_alpha, ctl_blend_flags = 0;
234 	unsigned long flags;
235 	enum mdp5_pipe stage[STAGE_MAX + 1][MAX_PIPE_STAGE] = { { SSPP_NONE } };
236 	enum mdp5_pipe r_stage[STAGE_MAX + 1][MAX_PIPE_STAGE] = { { SSPP_NONE } };
237 	int i, plane_cnt = 0;
238 	bool bg_alpha_enabled = false;
239 	u32 mixer_op_mode = 0;
240 	u32 val;
241 #define blender(stage)	((stage) - STAGE0)
242 
243 	hw_cfg = mdp5_cfg_get_hw_config(mdp5_kms->cfg);
244 
245 	spin_lock_irqsave(&mdp5_crtc->lm_lock, flags);
246 
247 	/* ctl could be released already when we are shutting down: */
248 	/* XXX: Can this happen now? */
249 	if (!ctl)
250 		goto out;
251 
252 	/* Collect all plane information */
253 	drm_atomic_crtc_for_each_plane(plane, crtc) {
254 		enum mdp5_pipe right_pipe;
255 
256 		if (!plane->state->visible)
257 			continue;
258 
259 		pstate = to_mdp5_plane_state(plane->state);
260 		pstates[pstate->stage] = pstate;
261 		stage[pstate->stage][PIPE_LEFT] = mdp5_plane_pipe(plane);
262 		/*
263 		 * if we have a right mixer, stage the same pipe as we
264 		 * have on the left mixer
265 		 */
266 		if (r_mixer)
267 			r_stage[pstate->stage][PIPE_LEFT] =
268 						mdp5_plane_pipe(plane);
269 		/*
270 		 * if we have a right pipe (i.e, the plane comprises of 2
271 		 * hwpipes, then stage the right pipe on the right side of both
272 		 * the layer mixers
273 		 */
274 		right_pipe = mdp5_plane_right_pipe(plane);
275 		if (right_pipe) {
276 			stage[pstate->stage][PIPE_RIGHT] = right_pipe;
277 			r_stage[pstate->stage][PIPE_RIGHT] = right_pipe;
278 		}
279 
280 		plane_cnt++;
281 	}
282 
283 	if (!pstates[STAGE_BASE]) {
284 		ctl_blend_flags |= MDP5_CTL_BLEND_OP_FLAG_BORDER_OUT;
285 		DBG("Border Color is enabled");
286 	} else if (plane_cnt) {
287 		format = to_mdp_format(msm_framebuffer_format(pstates[STAGE_BASE]->base.fb));
288 
289 		if (format->alpha_enable)
290 			bg_alpha_enabled = true;
291 	}
292 
293 	/* The reset for blending */
294 	for (i = STAGE0; i <= STAGE_MAX; i++) {
295 		if (!pstates[i])
296 			continue;
297 
298 		format = to_mdp_format(
299 			msm_framebuffer_format(pstates[i]->base.fb));
300 		plane = pstates[i]->base.plane;
301 		blend_op = MDP5_LM_BLEND_OP_MODE_FG_ALPHA(FG_CONST) |
302 			MDP5_LM_BLEND_OP_MODE_BG_ALPHA(BG_CONST);
303 		fg_alpha = pstates[i]->alpha;
304 		bg_alpha = 0xFF - pstates[i]->alpha;
305 
306 		if (!format->alpha_enable && bg_alpha_enabled)
307 			mixer_op_mode = 0;
308 		else
309 			mixer_op_mode |= mdp5_lm_use_fg_alpha_mask(i);
310 
311 		DBG("Stage %d fg_alpha %x bg_alpha %x", i, fg_alpha, bg_alpha);
312 
313 		if (format->alpha_enable && pstates[i]->premultiplied) {
314 			blend_op = MDP5_LM_BLEND_OP_MODE_FG_ALPHA(FG_CONST) |
315 				MDP5_LM_BLEND_OP_MODE_BG_ALPHA(FG_PIXEL);
316 			if (fg_alpha != 0xff) {
317 				bg_alpha = fg_alpha;
318 				blend_op |=
319 					MDP5_LM_BLEND_OP_MODE_BG_MOD_ALPHA |
320 					MDP5_LM_BLEND_OP_MODE_BG_INV_MOD_ALPHA;
321 			} else {
322 				blend_op |= MDP5_LM_BLEND_OP_MODE_BG_INV_ALPHA;
323 			}
324 		} else if (format->alpha_enable) {
325 			blend_op = MDP5_LM_BLEND_OP_MODE_FG_ALPHA(FG_PIXEL) |
326 				MDP5_LM_BLEND_OP_MODE_BG_ALPHA(FG_PIXEL);
327 			if (fg_alpha != 0xff) {
328 				bg_alpha = fg_alpha;
329 				blend_op |=
330 				       MDP5_LM_BLEND_OP_MODE_FG_MOD_ALPHA |
331 				       MDP5_LM_BLEND_OP_MODE_FG_INV_MOD_ALPHA |
332 				       MDP5_LM_BLEND_OP_MODE_BG_MOD_ALPHA |
333 				       MDP5_LM_BLEND_OP_MODE_BG_INV_MOD_ALPHA;
334 			} else {
335 				blend_op |= MDP5_LM_BLEND_OP_MODE_BG_INV_ALPHA;
336 			}
337 		}
338 
339 		mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_OP_MODE(lm,
340 				blender(i)), blend_op);
341 		mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_FG_ALPHA(lm,
342 				blender(i)), fg_alpha);
343 		mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_BG_ALPHA(lm,
344 				blender(i)), bg_alpha);
345 		if (r_mixer) {
346 			mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_OP_MODE(r_lm,
347 					blender(i)), blend_op);
348 			mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_FG_ALPHA(r_lm,
349 					blender(i)), fg_alpha);
350 			mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_BG_ALPHA(r_lm,
351 					blender(i)), bg_alpha);
352 		}
353 	}
354 
355 	val = mdp5_read(mdp5_kms, REG_MDP5_LM_BLEND_COLOR_OUT(lm));
356 	mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_COLOR_OUT(lm),
357 		   val | mixer_op_mode);
358 	if (r_mixer) {
359 		val = mdp5_read(mdp5_kms, REG_MDP5_LM_BLEND_COLOR_OUT(r_lm));
360 		mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_COLOR_OUT(r_lm),
361 			   val | mixer_op_mode);
362 	}
363 
364 	mdp5_ctl_blend(ctl, pipeline, stage, r_stage, plane_cnt,
365 		       ctl_blend_flags);
366 out:
367 	spin_unlock_irqrestore(&mdp5_crtc->lm_lock, flags);
368 }
369 
370 static void mdp5_crtc_mode_set_nofb(struct drm_crtc *crtc)
371 {
372 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
373 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
374 	struct mdp5_kms *mdp5_kms = get_kms(crtc);
375 	struct mdp5_hw_mixer *mixer = mdp5_cstate->pipeline.mixer;
376 	struct mdp5_hw_mixer *r_mixer = mdp5_cstate->pipeline.r_mixer;
377 	uint32_t lm = mixer->lm;
378 	u32 mixer_width, val;
379 	unsigned long flags;
380 	struct drm_display_mode *mode;
381 
382 	if (WARN_ON(!crtc->state))
383 		return;
384 
385 	mode = &crtc->state->adjusted_mode;
386 
387 	DBG("%s: set mode: " DRM_MODE_FMT, crtc->name, DRM_MODE_ARG(mode));
388 
389 	mixer_width = mode->hdisplay;
390 	if (r_mixer)
391 		mixer_width /= 2;
392 
393 	spin_lock_irqsave(&mdp5_crtc->lm_lock, flags);
394 	mdp5_write(mdp5_kms, REG_MDP5_LM_OUT_SIZE(lm),
395 			MDP5_LM_OUT_SIZE_WIDTH(mixer_width) |
396 			MDP5_LM_OUT_SIZE_HEIGHT(mode->vdisplay));
397 
398 	/* Assign mixer to LEFT side in source split mode */
399 	val = mdp5_read(mdp5_kms, REG_MDP5_LM_BLEND_COLOR_OUT(lm));
400 	val &= ~MDP5_LM_BLEND_COLOR_OUT_SPLIT_LEFT_RIGHT;
401 	mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_COLOR_OUT(lm), val);
402 
403 	if (r_mixer) {
404 		u32 r_lm = r_mixer->lm;
405 
406 		mdp5_write(mdp5_kms, REG_MDP5_LM_OUT_SIZE(r_lm),
407 			   MDP5_LM_OUT_SIZE_WIDTH(mixer_width) |
408 			   MDP5_LM_OUT_SIZE_HEIGHT(mode->vdisplay));
409 
410 		/* Assign mixer to RIGHT side in source split mode */
411 		val = mdp5_read(mdp5_kms, REG_MDP5_LM_BLEND_COLOR_OUT(r_lm));
412 		val |= MDP5_LM_BLEND_COLOR_OUT_SPLIT_LEFT_RIGHT;
413 		mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_COLOR_OUT(r_lm), val);
414 	}
415 
416 	spin_unlock_irqrestore(&mdp5_crtc->lm_lock, flags);
417 }
418 
419 static void mdp5_crtc_atomic_disable(struct drm_crtc *crtc,
420 				     struct drm_crtc_state *old_state)
421 {
422 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
423 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
424 	struct mdp5_kms *mdp5_kms = get_kms(crtc);
425 	struct device *dev = &mdp5_kms->pdev->dev;
426 	unsigned long flags;
427 
428 	DBG("%s", crtc->name);
429 
430 	if (WARN_ON(!mdp5_crtc->enabled))
431 		return;
432 
433 	/* Disable/save vblank irq handling before power is disabled */
434 	drm_crtc_vblank_off(crtc);
435 
436 	if (mdp5_cstate->cmd_mode)
437 		mdp_irq_unregister(&mdp5_kms->base, &mdp5_crtc->pp_done);
438 
439 	mdp_irq_unregister(&mdp5_kms->base, &mdp5_crtc->err);
440 	pm_runtime_put_sync(dev);
441 
442 	if (crtc->state->event && !crtc->state->active) {
443 		WARN_ON(mdp5_crtc->event);
444 		spin_lock_irqsave(&mdp5_kms->dev->event_lock, flags);
445 		drm_crtc_send_vblank_event(crtc, crtc->state->event);
446 		crtc->state->event = NULL;
447 		spin_unlock_irqrestore(&mdp5_kms->dev->event_lock, flags);
448 	}
449 
450 	mdp5_crtc->enabled = false;
451 }
452 
453 static void mdp5_crtc_atomic_enable(struct drm_crtc *crtc,
454 				    struct drm_crtc_state *old_state)
455 {
456 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
457 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
458 	struct mdp5_kms *mdp5_kms = get_kms(crtc);
459 	struct device *dev = &mdp5_kms->pdev->dev;
460 
461 	DBG("%s", crtc->name);
462 
463 	if (WARN_ON(mdp5_crtc->enabled))
464 		return;
465 
466 	pm_runtime_get_sync(dev);
467 
468 	if (mdp5_crtc->lm_cursor_enabled) {
469 		/*
470 		 * Restore LM cursor state, as it might have been lost
471 		 * with suspend:
472 		 */
473 		if (mdp5_crtc->cursor.iova) {
474 			unsigned long flags;
475 
476 			spin_lock_irqsave(&mdp5_crtc->cursor.lock, flags);
477 			mdp5_crtc_restore_cursor(crtc);
478 			spin_unlock_irqrestore(&mdp5_crtc->cursor.lock, flags);
479 
480 			mdp5_ctl_set_cursor(mdp5_cstate->ctl,
481 					    &mdp5_cstate->pipeline, 0, true);
482 		} else {
483 			mdp5_ctl_set_cursor(mdp5_cstate->ctl,
484 					    &mdp5_cstate->pipeline, 0, false);
485 		}
486 	}
487 
488 	/* Restore vblank irq handling after power is enabled */
489 	drm_crtc_vblank_on(crtc);
490 
491 	mdp5_crtc_mode_set_nofb(crtc);
492 
493 	mdp_irq_register(&mdp5_kms->base, &mdp5_crtc->err);
494 
495 	if (mdp5_cstate->cmd_mode)
496 		mdp_irq_register(&mdp5_kms->base, &mdp5_crtc->pp_done);
497 
498 	mdp5_crtc->enabled = true;
499 }
500 
501 int mdp5_crtc_setup_pipeline(struct drm_crtc *crtc,
502 			     struct drm_crtc_state *new_crtc_state,
503 			     bool need_right_mixer)
504 {
505 	struct mdp5_crtc_state *mdp5_cstate =
506 			to_mdp5_crtc_state(new_crtc_state);
507 	struct mdp5_pipeline *pipeline = &mdp5_cstate->pipeline;
508 	struct mdp5_interface *intf;
509 	bool new_mixer = false;
510 
511 	new_mixer = !pipeline->mixer;
512 
513 	if ((need_right_mixer && !pipeline->r_mixer) ||
514 	    (!need_right_mixer && pipeline->r_mixer))
515 		new_mixer = true;
516 
517 	if (new_mixer) {
518 		struct mdp5_hw_mixer *old_mixer = pipeline->mixer;
519 		struct mdp5_hw_mixer *old_r_mixer = pipeline->r_mixer;
520 		u32 caps;
521 		int ret;
522 
523 		caps = MDP_LM_CAP_DISPLAY;
524 		if (need_right_mixer)
525 			caps |= MDP_LM_CAP_PAIR;
526 
527 		ret = mdp5_mixer_assign(new_crtc_state->state, crtc, caps,
528 					&pipeline->mixer, need_right_mixer ?
529 					&pipeline->r_mixer : NULL);
530 		if (ret)
531 			return ret;
532 
533 		mdp5_mixer_release(new_crtc_state->state, old_mixer);
534 		if (old_r_mixer) {
535 			mdp5_mixer_release(new_crtc_state->state, old_r_mixer);
536 			if (!need_right_mixer)
537 				pipeline->r_mixer = NULL;
538 		}
539 	}
540 
541 	/*
542 	 * these should have been already set up in the encoder's atomic
543 	 * check (called by drm_atomic_helper_check_modeset)
544 	 */
545 	intf = pipeline->intf;
546 
547 	mdp5_cstate->err_irqmask = intf2err(intf->num);
548 	mdp5_cstate->vblank_irqmask = intf2vblank(pipeline->mixer, intf);
549 
550 	if ((intf->type == INTF_DSI) &&
551 	    (intf->mode == MDP5_INTF_DSI_MODE_COMMAND)) {
552 		mdp5_cstate->pp_done_irqmask = lm2ppdone(pipeline->mixer);
553 		mdp5_cstate->cmd_mode = true;
554 	} else {
555 		mdp5_cstate->pp_done_irqmask = 0;
556 		mdp5_cstate->cmd_mode = false;
557 	}
558 
559 	return 0;
560 }
561 
562 struct plane_state {
563 	struct drm_plane *plane;
564 	struct mdp5_plane_state *state;
565 };
566 
567 static int pstate_cmp(const void *a, const void *b)
568 {
569 	struct plane_state *pa = (struct plane_state *)a;
570 	struct plane_state *pb = (struct plane_state *)b;
571 	return pa->state->zpos - pb->state->zpos;
572 }
573 
574 /* is there a helper for this? */
575 static bool is_fullscreen(struct drm_crtc_state *cstate,
576 		struct drm_plane_state *pstate)
577 {
578 	return (pstate->crtc_x <= 0) && (pstate->crtc_y <= 0) &&
579 		((pstate->crtc_x + pstate->crtc_w) >= cstate->mode.hdisplay) &&
580 		((pstate->crtc_y + pstate->crtc_h) >= cstate->mode.vdisplay);
581 }
582 
583 static enum mdp_mixer_stage_id get_start_stage(struct drm_crtc *crtc,
584 					struct drm_crtc_state *new_crtc_state,
585 					struct drm_plane_state *bpstate)
586 {
587 	struct mdp5_crtc_state *mdp5_cstate =
588 			to_mdp5_crtc_state(new_crtc_state);
589 
590 	/*
591 	 * if we're in source split mode, it's mandatory to have
592 	 * border out on the base stage
593 	 */
594 	if (mdp5_cstate->pipeline.r_mixer)
595 		return STAGE0;
596 
597 	/* if the bottom-most layer is not fullscreen, we need to use
598 	 * it for solid-color:
599 	 */
600 	if (!is_fullscreen(new_crtc_state, bpstate))
601 		return STAGE0;
602 
603 	return STAGE_BASE;
604 }
605 
606 static int mdp5_crtc_atomic_check(struct drm_crtc *crtc,
607 		struct drm_crtc_state *state)
608 {
609 	struct mdp5_kms *mdp5_kms = get_kms(crtc);
610 	struct drm_plane *plane;
611 	struct drm_device *dev = crtc->dev;
612 	struct plane_state pstates[STAGE_MAX + 1];
613 	const struct mdp5_cfg_hw *hw_cfg;
614 	const struct drm_plane_state *pstate;
615 	const struct drm_display_mode *mode = &state->adjusted_mode;
616 	bool cursor_plane = false;
617 	bool need_right_mixer = false;
618 	int cnt = 0, i;
619 	int ret;
620 	enum mdp_mixer_stage_id start;
621 
622 	DBG("%s: check", crtc->name);
623 
624 	drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
625 		if (!pstate->visible)
626 			continue;
627 
628 		pstates[cnt].plane = plane;
629 		pstates[cnt].state = to_mdp5_plane_state(pstate);
630 
631 		/*
632 		 * if any plane on this crtc uses 2 hwpipes, then we need
633 		 * the crtc to have a right hwmixer.
634 		 */
635 		if (pstates[cnt].state->r_hwpipe)
636 			need_right_mixer = true;
637 		cnt++;
638 
639 		if (plane->type == DRM_PLANE_TYPE_CURSOR)
640 			cursor_plane = true;
641 	}
642 
643 	/* bail out early if there aren't any planes */
644 	if (!cnt)
645 		return 0;
646 
647 	hw_cfg = mdp5_cfg_get_hw_config(mdp5_kms->cfg);
648 
649 	/*
650 	 * we need a right hwmixer if the mode's width is greater than a single
651 	 * LM's max width
652 	 */
653 	if (mode->hdisplay > hw_cfg->lm.max_width)
654 		need_right_mixer = true;
655 
656 	ret = mdp5_crtc_setup_pipeline(crtc, state, need_right_mixer);
657 	if (ret) {
658 		DRM_DEV_ERROR(dev->dev, "couldn't assign mixers %d\n", ret);
659 		return ret;
660 	}
661 
662 	/* assign a stage based on sorted zpos property */
663 	sort(pstates, cnt, sizeof(pstates[0]), pstate_cmp, NULL);
664 
665 	/* trigger a warning if cursor isn't the highest zorder */
666 	WARN_ON(cursor_plane &&
667 		(pstates[cnt - 1].plane->type != DRM_PLANE_TYPE_CURSOR));
668 
669 	start = get_start_stage(crtc, state, &pstates[0].state->base);
670 
671 	/* verify that there are not too many planes attached to crtc
672 	 * and that we don't have conflicting mixer stages:
673 	 */
674 	if ((cnt + start - 1) >= hw_cfg->lm.nb_stages) {
675 		DRM_DEV_ERROR(dev->dev, "too many planes! cnt=%d, start stage=%d\n",
676 			cnt, start);
677 		return -EINVAL;
678 	}
679 
680 	for (i = 0; i < cnt; i++) {
681 		if (cursor_plane && (i == (cnt - 1)))
682 			pstates[i].state->stage = hw_cfg->lm.nb_stages;
683 		else
684 			pstates[i].state->stage = start + i;
685 		DBG("%s: assign pipe %s on stage=%d", crtc->name,
686 				pstates[i].plane->name,
687 				pstates[i].state->stage);
688 	}
689 
690 	return 0;
691 }
692 
693 static void mdp5_crtc_atomic_begin(struct drm_crtc *crtc,
694 				   struct drm_crtc_state *old_crtc_state)
695 {
696 	DBG("%s: begin", crtc->name);
697 }
698 
699 static void mdp5_crtc_atomic_flush(struct drm_crtc *crtc,
700 				   struct drm_crtc_state *old_crtc_state)
701 {
702 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
703 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
704 	struct drm_device *dev = crtc->dev;
705 	unsigned long flags;
706 
707 	DBG("%s: event: %p", crtc->name, crtc->state->event);
708 
709 	WARN_ON(mdp5_crtc->event);
710 
711 	spin_lock_irqsave(&dev->event_lock, flags);
712 	mdp5_crtc->event = crtc->state->event;
713 	crtc->state->event = NULL;
714 	spin_unlock_irqrestore(&dev->event_lock, flags);
715 
716 	/*
717 	 * If no CTL has been allocated in mdp5_crtc_atomic_check(),
718 	 * it means we are trying to flush a CRTC whose state is disabled:
719 	 * nothing else needs to be done.
720 	 */
721 	/* XXX: Can this happen now ? */
722 	if (unlikely(!mdp5_cstate->ctl))
723 		return;
724 
725 	blend_setup(crtc);
726 
727 	/* PP_DONE irq is only used by command mode for now.
728 	 * It is better to request pending before FLUSH and START trigger
729 	 * to make sure no pp_done irq missed.
730 	 * This is safe because no pp_done will happen before SW trigger
731 	 * in command mode.
732 	 */
733 	if (mdp5_cstate->cmd_mode)
734 		request_pp_done_pending(crtc);
735 
736 	mdp5_crtc->flushed_mask = crtc_flush_all(crtc);
737 
738 	/* XXX are we leaking out state here? */
739 	mdp5_crtc->vblank.irqmask = mdp5_cstate->vblank_irqmask;
740 	mdp5_crtc->err.irqmask = mdp5_cstate->err_irqmask;
741 	mdp5_crtc->pp_done.irqmask = mdp5_cstate->pp_done_irqmask;
742 
743 	request_pending(crtc, PENDING_FLIP);
744 }
745 
746 static void get_roi(struct drm_crtc *crtc, uint32_t *roi_w, uint32_t *roi_h)
747 {
748 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
749 	uint32_t xres = crtc->mode.hdisplay;
750 	uint32_t yres = crtc->mode.vdisplay;
751 
752 	/*
753 	 * Cursor Region Of Interest (ROI) is a plane read from cursor
754 	 * buffer to render. The ROI region is determined by the visibility of
755 	 * the cursor point. In the default Cursor image the cursor point will
756 	 * be at the top left of the cursor image.
757 	 *
758 	 * Without rotation:
759 	 * If the cursor point reaches the right (xres - x < cursor.width) or
760 	 * bottom (yres - y < cursor.height) boundary of the screen, then ROI
761 	 * width and ROI height need to be evaluated to crop the cursor image
762 	 * accordingly.
763 	 * (xres-x) will be new cursor width when x > (xres - cursor.width)
764 	 * (yres-y) will be new cursor height when y > (yres - cursor.height)
765 	 *
766 	 * With rotation:
767 	 * We get negative x and/or y coordinates.
768 	 * (cursor.width - abs(x)) will be new cursor width when x < 0
769 	 * (cursor.height - abs(y)) will be new cursor width when y < 0
770 	 */
771 	if (mdp5_crtc->cursor.x >= 0)
772 		*roi_w = min(mdp5_crtc->cursor.width, xres -
773 			mdp5_crtc->cursor.x);
774 	else
775 		*roi_w = mdp5_crtc->cursor.width - abs(mdp5_crtc->cursor.x);
776 	if (mdp5_crtc->cursor.y >= 0)
777 		*roi_h = min(mdp5_crtc->cursor.height, yres -
778 			mdp5_crtc->cursor.y);
779 	else
780 		*roi_h = mdp5_crtc->cursor.height - abs(mdp5_crtc->cursor.y);
781 }
782 
783 static void mdp5_crtc_restore_cursor(struct drm_crtc *crtc)
784 {
785 	const struct drm_format_info *info = drm_format_info(DRM_FORMAT_ARGB8888);
786 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
787 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
788 	struct mdp5_kms *mdp5_kms = get_kms(crtc);
789 	const enum mdp5_cursor_alpha cur_alpha = CURSOR_ALPHA_PER_PIXEL;
790 	uint32_t blendcfg, stride;
791 	uint32_t x, y, src_x, src_y, width, height;
792 	uint32_t roi_w, roi_h;
793 	int lm;
794 
795 	assert_spin_locked(&mdp5_crtc->cursor.lock);
796 
797 	lm = mdp5_cstate->pipeline.mixer->lm;
798 
799 	x = mdp5_crtc->cursor.x;
800 	y = mdp5_crtc->cursor.y;
801 	width = mdp5_crtc->cursor.width;
802 	height = mdp5_crtc->cursor.height;
803 
804 	stride = width * info->cpp[0];
805 
806 	get_roi(crtc, &roi_w, &roi_h);
807 
808 	/* If cusror buffer overlaps due to rotation on the
809 	 * upper or left screen border the pixel offset inside
810 	 * the cursor buffer of the ROI is the positive overlap
811 	 * distance.
812 	 */
813 	if (mdp5_crtc->cursor.x < 0) {
814 		src_x = abs(mdp5_crtc->cursor.x);
815 		x = 0;
816 	} else {
817 		src_x = 0;
818 	}
819 	if (mdp5_crtc->cursor.y < 0) {
820 		src_y = abs(mdp5_crtc->cursor.y);
821 		y = 0;
822 	} else {
823 		src_y = 0;
824 	}
825 	DBG("%s: x=%d, y=%d roi_w=%d roi_h=%d src_x=%d src_y=%d",
826 		crtc->name, x, y, roi_w, roi_h, src_x, src_y);
827 
828 	mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_STRIDE(lm), stride);
829 	mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_FORMAT(lm),
830 			MDP5_LM_CURSOR_FORMAT_FORMAT(CURSOR_FMT_ARGB8888));
831 	mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_IMG_SIZE(lm),
832 			MDP5_LM_CURSOR_IMG_SIZE_SRC_H(height) |
833 			MDP5_LM_CURSOR_IMG_SIZE_SRC_W(width));
834 	mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_SIZE(lm),
835 			MDP5_LM_CURSOR_SIZE_ROI_H(roi_h) |
836 			MDP5_LM_CURSOR_SIZE_ROI_W(roi_w));
837 	mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_START_XY(lm),
838 			MDP5_LM_CURSOR_START_XY_Y_START(y) |
839 			MDP5_LM_CURSOR_START_XY_X_START(x));
840 	mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_XY(lm),
841 			MDP5_LM_CURSOR_XY_SRC_Y(src_y) |
842 			MDP5_LM_CURSOR_XY_SRC_X(src_x));
843 	mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_BASE_ADDR(lm),
844 			mdp5_crtc->cursor.iova);
845 
846 	blendcfg = MDP5_LM_CURSOR_BLEND_CONFIG_BLEND_EN;
847 	blendcfg |= MDP5_LM_CURSOR_BLEND_CONFIG_BLEND_ALPHA_SEL(cur_alpha);
848 	mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_BLEND_CONFIG(lm), blendcfg);
849 }
850 
851 static int mdp5_crtc_cursor_set(struct drm_crtc *crtc,
852 		struct drm_file *file, uint32_t handle,
853 		uint32_t width, uint32_t height)
854 {
855 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
856 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
857 	struct mdp5_pipeline *pipeline = &mdp5_cstate->pipeline;
858 	struct drm_device *dev = crtc->dev;
859 	struct mdp5_kms *mdp5_kms = get_kms(crtc);
860 	struct platform_device *pdev = mdp5_kms->pdev;
861 	struct msm_kms *kms = &mdp5_kms->base.base;
862 	struct drm_gem_object *cursor_bo, *old_bo = NULL;
863 	struct mdp5_ctl *ctl;
864 	int ret;
865 	uint32_t flush_mask = mdp_ctl_flush_mask_cursor(0);
866 	bool cursor_enable = true;
867 	unsigned long flags;
868 
869 	if (!mdp5_crtc->lm_cursor_enabled) {
870 		dev_warn(dev->dev,
871 			 "cursor_set is deprecated with cursor planes\n");
872 		return -EINVAL;
873 	}
874 
875 	if ((width > CURSOR_WIDTH) || (height > CURSOR_HEIGHT)) {
876 		DRM_DEV_ERROR(dev->dev, "bad cursor size: %dx%d\n", width, height);
877 		return -EINVAL;
878 	}
879 
880 	ctl = mdp5_cstate->ctl;
881 	if (!ctl)
882 		return -EINVAL;
883 
884 	/* don't support LM cursors when we we have source split enabled */
885 	if (mdp5_cstate->pipeline.r_mixer)
886 		return -EINVAL;
887 
888 	if (!handle) {
889 		DBG("Cursor off");
890 		cursor_enable = false;
891 		mdp5_crtc->cursor.iova = 0;
892 		pm_runtime_get_sync(&pdev->dev);
893 		goto set_cursor;
894 	}
895 
896 	cursor_bo = drm_gem_object_lookup(file, handle);
897 	if (!cursor_bo)
898 		return -ENOENT;
899 
900 	ret = msm_gem_get_and_pin_iova(cursor_bo, kms->aspace,
901 			&mdp5_crtc->cursor.iova);
902 	if (ret)
903 		return -EINVAL;
904 
905 	pm_runtime_get_sync(&pdev->dev);
906 
907 	spin_lock_irqsave(&mdp5_crtc->cursor.lock, flags);
908 	old_bo = mdp5_crtc->cursor.scanout_bo;
909 
910 	mdp5_crtc->cursor.scanout_bo = cursor_bo;
911 	mdp5_crtc->cursor.width = width;
912 	mdp5_crtc->cursor.height = height;
913 
914 	mdp5_crtc_restore_cursor(crtc);
915 
916 	spin_unlock_irqrestore(&mdp5_crtc->cursor.lock, flags);
917 
918 set_cursor:
919 	ret = mdp5_ctl_set_cursor(ctl, pipeline, 0, cursor_enable);
920 	if (ret) {
921 		DRM_DEV_ERROR(dev->dev, "failed to %sable cursor: %d\n",
922 				cursor_enable ? "en" : "dis", ret);
923 		goto end;
924 	}
925 
926 	crtc_flush(crtc, flush_mask);
927 
928 end:
929 	pm_runtime_put_sync(&pdev->dev);
930 	if (old_bo) {
931 		drm_flip_work_queue(&mdp5_crtc->unref_cursor_work, old_bo);
932 		/* enable vblank to complete cursor work: */
933 		request_pending(crtc, PENDING_CURSOR);
934 	}
935 	return ret;
936 }
937 
938 static int mdp5_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
939 {
940 	struct mdp5_kms *mdp5_kms = get_kms(crtc);
941 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
942 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
943 	uint32_t flush_mask = mdp_ctl_flush_mask_cursor(0);
944 	struct drm_device *dev = crtc->dev;
945 	uint32_t roi_w;
946 	uint32_t roi_h;
947 	unsigned long flags;
948 
949 	if (!mdp5_crtc->lm_cursor_enabled) {
950 		dev_warn(dev->dev,
951 			 "cursor_move is deprecated with cursor planes\n");
952 		return -EINVAL;
953 	}
954 
955 	/* don't support LM cursors when we we have source split enabled */
956 	if (mdp5_cstate->pipeline.r_mixer)
957 		return -EINVAL;
958 
959 	/* In case the CRTC is disabled, just drop the cursor update */
960 	if (unlikely(!crtc->state->enable))
961 		return 0;
962 
963 	/* accept negative x/y coordinates up to maximum cursor overlap */
964 	mdp5_crtc->cursor.x = x = max(x, -(int)mdp5_crtc->cursor.width);
965 	mdp5_crtc->cursor.y = y = max(y, -(int)mdp5_crtc->cursor.height);
966 
967 	get_roi(crtc, &roi_w, &roi_h);
968 
969 	pm_runtime_get_sync(&mdp5_kms->pdev->dev);
970 
971 	spin_lock_irqsave(&mdp5_crtc->cursor.lock, flags);
972 	mdp5_crtc_restore_cursor(crtc);
973 	spin_unlock_irqrestore(&mdp5_crtc->cursor.lock, flags);
974 
975 	crtc_flush(crtc, flush_mask);
976 
977 	pm_runtime_put_sync(&mdp5_kms->pdev->dev);
978 
979 	return 0;
980 }
981 
982 static void
983 mdp5_crtc_atomic_print_state(struct drm_printer *p,
984 			     const struct drm_crtc_state *state)
985 {
986 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(state);
987 	struct mdp5_pipeline *pipeline = &mdp5_cstate->pipeline;
988 	struct mdp5_kms *mdp5_kms = get_kms(state->crtc);
989 
990 	if (WARN_ON(!pipeline))
991 		return;
992 
993 	if (mdp5_cstate->ctl)
994 		drm_printf(p, "\tctl=%d\n", mdp5_ctl_get_ctl_id(mdp5_cstate->ctl));
995 
996 	drm_printf(p, "\thwmixer=%s\n", pipeline->mixer ?
997 			pipeline->mixer->name : "(null)");
998 
999 	if (mdp5_kms->caps & MDP_CAP_SRC_SPLIT)
1000 		drm_printf(p, "\tright hwmixer=%s\n", pipeline->r_mixer ?
1001 			   pipeline->r_mixer->name : "(null)");
1002 
1003 	drm_printf(p, "\tcmd_mode=%d\n", mdp5_cstate->cmd_mode);
1004 }
1005 
1006 static struct drm_crtc_state *
1007 mdp5_crtc_duplicate_state(struct drm_crtc *crtc)
1008 {
1009 	struct mdp5_crtc_state *mdp5_cstate;
1010 
1011 	if (WARN_ON(!crtc->state))
1012 		return NULL;
1013 
1014 	mdp5_cstate = kmemdup(to_mdp5_crtc_state(crtc->state),
1015 			      sizeof(*mdp5_cstate), GFP_KERNEL);
1016 	if (!mdp5_cstate)
1017 		return NULL;
1018 
1019 	__drm_atomic_helper_crtc_duplicate_state(crtc, &mdp5_cstate->base);
1020 
1021 	return &mdp5_cstate->base;
1022 }
1023 
1024 static void mdp5_crtc_destroy_state(struct drm_crtc *crtc, struct drm_crtc_state *state)
1025 {
1026 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(state);
1027 
1028 	__drm_atomic_helper_crtc_destroy_state(state);
1029 
1030 	kfree(mdp5_cstate);
1031 }
1032 
1033 static void mdp5_crtc_reset(struct drm_crtc *crtc)
1034 {
1035 	struct mdp5_crtc_state *mdp5_cstate =
1036 		kzalloc(sizeof(*mdp5_cstate), GFP_KERNEL);
1037 
1038 	if (crtc->state)
1039 		mdp5_crtc_destroy_state(crtc, crtc->state);
1040 
1041 	__drm_atomic_helper_crtc_reset(crtc, &mdp5_cstate->base);
1042 }
1043 
1044 static const struct drm_crtc_funcs mdp5_crtc_funcs = {
1045 	.set_config = drm_atomic_helper_set_config,
1046 	.destroy = mdp5_crtc_destroy,
1047 	.page_flip = drm_atomic_helper_page_flip,
1048 	.reset = mdp5_crtc_reset,
1049 	.atomic_duplicate_state = mdp5_crtc_duplicate_state,
1050 	.atomic_destroy_state = mdp5_crtc_destroy_state,
1051 	.cursor_set = mdp5_crtc_cursor_set,
1052 	.cursor_move = mdp5_crtc_cursor_move,
1053 	.atomic_print_state = mdp5_crtc_atomic_print_state,
1054 };
1055 
1056 static const struct drm_crtc_helper_funcs mdp5_crtc_helper_funcs = {
1057 	.mode_set_nofb = mdp5_crtc_mode_set_nofb,
1058 	.atomic_check = mdp5_crtc_atomic_check,
1059 	.atomic_begin = mdp5_crtc_atomic_begin,
1060 	.atomic_flush = mdp5_crtc_atomic_flush,
1061 	.atomic_enable = mdp5_crtc_atomic_enable,
1062 	.atomic_disable = mdp5_crtc_atomic_disable,
1063 };
1064 
1065 static void mdp5_crtc_vblank_irq(struct mdp_irq *irq, uint32_t irqstatus)
1066 {
1067 	struct mdp5_crtc *mdp5_crtc = container_of(irq, struct mdp5_crtc, vblank);
1068 	struct drm_crtc *crtc = &mdp5_crtc->base;
1069 	struct msm_drm_private *priv = crtc->dev->dev_private;
1070 	unsigned pending;
1071 
1072 	mdp_irq_unregister(&get_kms(crtc)->base, &mdp5_crtc->vblank);
1073 
1074 	pending = atomic_xchg(&mdp5_crtc->pending, 0);
1075 
1076 	if (pending & PENDING_FLIP) {
1077 		complete_flip(crtc, NULL);
1078 	}
1079 
1080 	if (pending & PENDING_CURSOR)
1081 		drm_flip_work_commit(&mdp5_crtc->unref_cursor_work, priv->wq);
1082 }
1083 
1084 static void mdp5_crtc_err_irq(struct mdp_irq *irq, uint32_t irqstatus)
1085 {
1086 	struct mdp5_crtc *mdp5_crtc = container_of(irq, struct mdp5_crtc, err);
1087 
1088 	DBG("%s: error: %08x", mdp5_crtc->base.name, irqstatus);
1089 }
1090 
1091 static void mdp5_crtc_pp_done_irq(struct mdp_irq *irq, uint32_t irqstatus)
1092 {
1093 	struct mdp5_crtc *mdp5_crtc = container_of(irq, struct mdp5_crtc,
1094 								pp_done);
1095 
1096 	complete(&mdp5_crtc->pp_completion);
1097 }
1098 
1099 static void mdp5_crtc_wait_for_pp_done(struct drm_crtc *crtc)
1100 {
1101 	struct drm_device *dev = crtc->dev;
1102 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
1103 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
1104 	int ret;
1105 
1106 	ret = wait_for_completion_timeout(&mdp5_crtc->pp_completion,
1107 						msecs_to_jiffies(50));
1108 	if (ret == 0)
1109 		dev_warn(dev->dev, "pp done time out, lm=%d\n",
1110 			 mdp5_cstate->pipeline.mixer->lm);
1111 }
1112 
1113 static void mdp5_crtc_wait_for_flush_done(struct drm_crtc *crtc)
1114 {
1115 	struct drm_device *dev = crtc->dev;
1116 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
1117 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
1118 	struct mdp5_ctl *ctl = mdp5_cstate->ctl;
1119 	int ret;
1120 
1121 	/* Should not call this function if crtc is disabled. */
1122 	if (!ctl)
1123 		return;
1124 
1125 	ret = drm_crtc_vblank_get(crtc);
1126 	if (ret)
1127 		return;
1128 
1129 	ret = wait_event_timeout(dev->vblank[drm_crtc_index(crtc)].queue,
1130 		((mdp5_ctl_get_commit_status(ctl) &
1131 		mdp5_crtc->flushed_mask) == 0),
1132 		msecs_to_jiffies(50));
1133 	if (ret <= 0)
1134 		dev_warn(dev->dev, "vblank time out, crtc=%d\n", mdp5_crtc->id);
1135 
1136 	mdp5_crtc->flushed_mask = 0;
1137 
1138 	drm_crtc_vblank_put(crtc);
1139 }
1140 
1141 uint32_t mdp5_crtc_vblank(struct drm_crtc *crtc)
1142 {
1143 	struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
1144 	return mdp5_crtc->vblank.irqmask;
1145 }
1146 
1147 void mdp5_crtc_set_pipeline(struct drm_crtc *crtc)
1148 {
1149 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
1150 	struct mdp5_kms *mdp5_kms = get_kms(crtc);
1151 
1152 	/* should this be done elsewhere ? */
1153 	mdp_irq_update(&mdp5_kms->base);
1154 
1155 	mdp5_ctl_set_pipeline(mdp5_cstate->ctl, &mdp5_cstate->pipeline);
1156 }
1157 
1158 struct mdp5_ctl *mdp5_crtc_get_ctl(struct drm_crtc *crtc)
1159 {
1160 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
1161 
1162 	return mdp5_cstate->ctl;
1163 }
1164 
1165 struct mdp5_hw_mixer *mdp5_crtc_get_mixer(struct drm_crtc *crtc)
1166 {
1167 	struct mdp5_crtc_state *mdp5_cstate;
1168 
1169 	if (WARN_ON(!crtc))
1170 		return ERR_PTR(-EINVAL);
1171 
1172 	mdp5_cstate = to_mdp5_crtc_state(crtc->state);
1173 
1174 	return WARN_ON(!mdp5_cstate->pipeline.mixer) ?
1175 		ERR_PTR(-EINVAL) : mdp5_cstate->pipeline.mixer;
1176 }
1177 
1178 struct mdp5_pipeline *mdp5_crtc_get_pipeline(struct drm_crtc *crtc)
1179 {
1180 	struct mdp5_crtc_state *mdp5_cstate;
1181 
1182 	if (WARN_ON(!crtc))
1183 		return ERR_PTR(-EINVAL);
1184 
1185 	mdp5_cstate = to_mdp5_crtc_state(crtc->state);
1186 
1187 	return &mdp5_cstate->pipeline;
1188 }
1189 
1190 void mdp5_crtc_wait_for_commit_done(struct drm_crtc *crtc)
1191 {
1192 	struct mdp5_crtc_state *mdp5_cstate = to_mdp5_crtc_state(crtc->state);
1193 
1194 	if (mdp5_cstate->cmd_mode)
1195 		mdp5_crtc_wait_for_pp_done(crtc);
1196 	else
1197 		mdp5_crtc_wait_for_flush_done(crtc);
1198 }
1199 
1200 /* initialize crtc */
1201 struct drm_crtc *mdp5_crtc_init(struct drm_device *dev,
1202 				struct drm_plane *plane,
1203 				struct drm_plane *cursor_plane, int id)
1204 {
1205 	struct drm_crtc *crtc = NULL;
1206 	struct mdp5_crtc *mdp5_crtc;
1207 
1208 	mdp5_crtc = kzalloc(sizeof(*mdp5_crtc), GFP_KERNEL);
1209 	if (!mdp5_crtc)
1210 		return ERR_PTR(-ENOMEM);
1211 
1212 	crtc = &mdp5_crtc->base;
1213 
1214 	mdp5_crtc->id = id;
1215 
1216 	spin_lock_init(&mdp5_crtc->lm_lock);
1217 	spin_lock_init(&mdp5_crtc->cursor.lock);
1218 	init_completion(&mdp5_crtc->pp_completion);
1219 
1220 	mdp5_crtc->vblank.irq = mdp5_crtc_vblank_irq;
1221 	mdp5_crtc->err.irq = mdp5_crtc_err_irq;
1222 	mdp5_crtc->pp_done.irq = mdp5_crtc_pp_done_irq;
1223 
1224 	mdp5_crtc->lm_cursor_enabled = cursor_plane ? false : true;
1225 
1226 	drm_crtc_init_with_planes(dev, crtc, plane, cursor_plane,
1227 				  &mdp5_crtc_funcs, NULL);
1228 
1229 	drm_flip_work_init(&mdp5_crtc->unref_cursor_work,
1230 			"unref cursor", unref_cursor_worker);
1231 
1232 	drm_crtc_helper_add(crtc, &mdp5_crtc_helper_funcs);
1233 
1234 	return crtc;
1235 }
1236