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