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