2 * Copyright (C) 2013 Red Hat
3 * Author: Rob Clark <robdclark@gmail.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <drm/drm_mode.h>
22 #include "drm_crtc_helper.h"
23 #include "drm_flip_work.h"
28 struct drm_plane
*plane
;
29 struct drm_plane
*planes
[8];
33 /* which mixer/encoder we route output to: */
36 /* if there is a pending flip, these will be non-null: */
37 struct drm_pending_vblank_event
*event
;
38 struct msm_fence_cb pageflip_cb
;
40 #define PENDING_CURSOR 0x1
41 #define PENDING_FLIP 0x2
44 /* the fb that we logically (from PoV of KMS API) hold a ref
45 * to. Which we may not yet be scanning out (we may still
46 * be scanning out previous in case of page_flip while waiting
47 * for gpu rendering to complete:
49 struct drm_framebuffer
*fb
;
51 /* the fb that we currently hold a scanout ref to: */
52 struct drm_framebuffer
*scanout_fb
;
54 /* for unref'ing framebuffers after scanout completes: */
55 struct drm_flip_work unref_fb_work
;
57 struct mdp_irq vblank
;
60 #define to_mdp5_crtc(x) container_of(x, struct mdp5_crtc, base)
62 static struct mdp5_kms
*get_kms(struct drm_crtc
*crtc
)
64 struct msm_drm_private
*priv
= crtc
->dev
->dev_private
;
65 return to_mdp5_kms(to_mdp_kms(priv
->kms
));
68 static void request_pending(struct drm_crtc
*crtc
, uint32_t pending
)
70 struct mdp5_crtc
*mdp5_crtc
= to_mdp5_crtc(crtc
);
72 atomic_or(pending
, &mdp5_crtc
->pending
);
73 mdp_irq_register(&get_kms(crtc
)->base
, &mdp5_crtc
->vblank
);
76 static void crtc_flush(struct drm_crtc
*crtc
)
78 struct mdp5_crtc
*mdp5_crtc
= to_mdp5_crtc(crtc
);
79 struct mdp5_kms
*mdp5_kms
= get_kms(crtc
);
80 int id
= mdp5_crtc
->id
;
81 uint32_t i
, flush
= 0;
83 for (i
= 0; i
< ARRAY_SIZE(mdp5_crtc
->planes
); i
++) {
84 struct drm_plane
*plane
= mdp5_crtc
->planes
[i
];
86 enum mdp5_pipe pipe
= mdp5_plane_pipe(plane
);
87 flush
|= pipe2flush(pipe
);
90 flush
|= mixer2flush(mdp5_crtc
->id
);
91 flush
|= MDP5_CTL_FLUSH_CTL
;
93 DBG("%s: flush=%08x", mdp5_crtc
->name
, flush
);
95 mdp5_write(mdp5_kms
, REG_MDP5_CTL_FLUSH(id
), flush
);
98 static void update_fb(struct drm_crtc
*crtc
, struct drm_framebuffer
*new_fb
)
100 struct mdp5_crtc
*mdp5_crtc
= to_mdp5_crtc(crtc
);
101 struct drm_framebuffer
*old_fb
= mdp5_crtc
->fb
;
103 /* grab reference to incoming scanout fb: */
104 drm_framebuffer_reference(new_fb
);
105 mdp5_crtc
->base
.fb
= new_fb
;
106 mdp5_crtc
->fb
= new_fb
;
109 drm_flip_work_queue(&mdp5_crtc
->unref_fb_work
, old_fb
);
112 /* unlike update_fb(), take a ref to the new scanout fb *before* updating
113 * plane, then call this. Needed to ensure we don't unref the buffer that
114 * is actually still being scanned out.
116 * Note that this whole thing goes away with atomic.. since we can defer
117 * calling into driver until rendering is done.
119 static void update_scanout(struct drm_crtc
*crtc
, struct drm_framebuffer
*fb
)
121 struct mdp5_crtc
*mdp5_crtc
= to_mdp5_crtc(crtc
);
123 /* flush updates, to make sure hw is updated to new scanout fb,
124 * so that we can safely queue unref to current fb (ie. next
125 * vblank we know hw is done w/ previous scanout_fb).
129 if (mdp5_crtc
->scanout_fb
)
130 drm_flip_work_queue(&mdp5_crtc
->unref_fb_work
,
131 mdp5_crtc
->scanout_fb
);
133 mdp5_crtc
->scanout_fb
= fb
;
135 /* enable vblank to complete flip: */
136 request_pending(crtc
, PENDING_FLIP
);
139 /* if file!=NULL, this is preclose potential cancel-flip path */
140 static void complete_flip(struct drm_crtc
*crtc
, struct drm_file
*file
)
142 struct mdp5_crtc
*mdp5_crtc
= to_mdp5_crtc(crtc
);
143 struct drm_device
*dev
= crtc
->dev
;
144 struct drm_pending_vblank_event
*event
;
145 unsigned long flags
, i
;
147 spin_lock_irqsave(&dev
->event_lock
, flags
);
148 event
= mdp5_crtc
->event
;
150 /* if regular vblank case (!file) or if cancel-flip from
151 * preclose on file that requested flip, then send the
154 if (!file
|| (event
->base
.file_priv
== file
)) {
155 mdp5_crtc
->event
= NULL
;
156 drm_send_vblank_event(dev
, mdp5_crtc
->id
, event
);
159 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
161 for (i
= 0; i
< ARRAY_SIZE(mdp5_crtc
->planes
); i
++) {
162 struct drm_plane
*plane
= mdp5_crtc
->planes
[i
];
164 mdp5_plane_complete_flip(plane
);
168 static void pageflip_cb(struct msm_fence_cb
*cb
)
170 struct mdp5_crtc
*mdp5_crtc
=
171 container_of(cb
, struct mdp5_crtc
, pageflip_cb
);
172 struct drm_crtc
*crtc
= &mdp5_crtc
->base
;
173 struct drm_framebuffer
*fb
= mdp5_crtc
->fb
;
178 drm_framebuffer_reference(fb
);
179 mdp5_plane_set_scanout(mdp5_crtc
->plane
, fb
);
180 update_scanout(crtc
, fb
);
183 static void unref_fb_worker(struct drm_flip_work
*work
, void *val
)
185 struct mdp5_crtc
*mdp5_crtc
=
186 container_of(work
, struct mdp5_crtc
, unref_fb_work
);
187 struct drm_device
*dev
= mdp5_crtc
->base
.dev
;
189 mutex_lock(&dev
->mode_config
.mutex
);
190 drm_framebuffer_unreference(val
);
191 mutex_unlock(&dev
->mode_config
.mutex
);
194 static void mdp5_crtc_destroy(struct drm_crtc
*crtc
)
196 struct mdp5_crtc
*mdp5_crtc
= to_mdp5_crtc(crtc
);
198 mdp5_crtc
->plane
->funcs
->destroy(mdp5_crtc
->plane
);
200 drm_crtc_cleanup(crtc
);
201 drm_flip_work_cleanup(&mdp5_crtc
->unref_fb_work
);
206 static void mdp5_crtc_dpms(struct drm_crtc
*crtc
, int mode
)
208 struct mdp5_crtc
*mdp5_crtc
= to_mdp5_crtc(crtc
);
209 struct mdp5_kms
*mdp5_kms
= get_kms(crtc
);
210 bool enabled
= (mode
== DRM_MODE_DPMS_ON
);
212 DBG("%s: mode=%d", mdp5_crtc
->name
, mode
);
214 if (enabled
!= mdp5_crtc
->enabled
) {
216 mdp5_enable(mdp5_kms
);
217 mdp_irq_register(&mdp5_kms
->base
, &mdp5_crtc
->err
);
219 mdp_irq_unregister(&mdp5_kms
->base
, &mdp5_crtc
->err
);
220 mdp5_disable(mdp5_kms
);
222 mdp5_crtc
->enabled
= enabled
;
226 static bool mdp5_crtc_mode_fixup(struct drm_crtc
*crtc
,
227 const struct drm_display_mode
*mode
,
228 struct drm_display_mode
*adjusted_mode
)
233 static void blend_setup(struct drm_crtc
*crtc
)
235 struct mdp5_crtc
*mdp5_crtc
= to_mdp5_crtc(crtc
);
236 struct mdp5_kms
*mdp5_kms
= get_kms(crtc
);
237 int id
= mdp5_crtc
->id
;
240 * Hard-coded setup for now until I figure out how the
245 mdp5_write(mdp5_kms
, REG_MDP5_LM_BLEND_COLOR_OUT(id
),
246 MDP5_LM_BLEND_COLOR_OUT_STAGE0_FG_ALPHA
);
247 mdp5_write(mdp5_kms
, REG_MDP5_LM_BLEND_OP_MODE(id
, 0),
248 MDP5_LM_BLEND_OP_MODE_FG_ALPHA(FG_CONST
) |
249 MDP5_LM_BLEND_OP_MODE_BG_ALPHA(FG_PIXEL
) |
250 MDP5_LM_BLEND_OP_MODE_BG_INV_ALPHA
);
251 mdp5_write(mdp5_kms
, REG_MDP5_LM_BLEND_FG_ALPHA(id
, 0), 0xff);
252 mdp5_write(mdp5_kms
, REG_MDP5_LM_BLEND_BG_ALPHA(id
, 0), 0x00);
254 /* NOTE: seems that LM[n] and CTL[m], we do not need n==m.. but
255 * we want to be setting CTL[m].LAYER[n]. Not sure what the
256 * point of having CTL[m].LAYER[o] (for o!=n).. maybe that is
257 * used when chaining up mixers for high resolution displays?
261 mdp5_write(mdp5_kms
, REG_MDP5_CTL_LAYER_REG(id
, 0),
262 MDP5_CTL_LAYER_REG_RGB0(STAGE0
) |
263 MDP5_CTL_LAYER_REG_BORDER_COLOR
);
264 mdp5_write(mdp5_kms
, REG_MDP5_CTL_LAYER_REG(id
, 1), 0);
265 mdp5_write(mdp5_kms
, REG_MDP5_CTL_LAYER_REG(id
, 2), 0);
266 mdp5_write(mdp5_kms
, REG_MDP5_CTL_LAYER_REG(id
, 3), 0);
267 mdp5_write(mdp5_kms
, REG_MDP5_CTL_LAYER_REG(id
, 4), 0);
270 static int mdp5_crtc_mode_set(struct drm_crtc
*crtc
,
271 struct drm_display_mode
*mode
,
272 struct drm_display_mode
*adjusted_mode
,
274 struct drm_framebuffer
*old_fb
)
276 struct mdp5_crtc
*mdp5_crtc
= to_mdp5_crtc(crtc
);
277 struct mdp5_kms
*mdp5_kms
= get_kms(crtc
);
280 mode
= adjusted_mode
;
282 DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
283 mdp5_crtc
->name
, mode
->base
.id
, mode
->name
,
284 mode
->vrefresh
, mode
->clock
,
285 mode
->hdisplay
, mode
->hsync_start
,
286 mode
->hsync_end
, mode
->htotal
,
287 mode
->vdisplay
, mode
->vsync_start
,
288 mode
->vsync_end
, mode
->vtotal
,
289 mode
->type
, mode
->flags
);
291 /* grab extra ref for update_scanout() */
292 drm_framebuffer_reference(crtc
->fb
);
294 ret
= mdp5_plane_mode_set(mdp5_crtc
->plane
, crtc
, crtc
->fb
,
295 0, 0, mode
->hdisplay
, mode
->vdisplay
,
297 mode
->hdisplay
<< 16, mode
->vdisplay
<< 16);
299 dev_err(crtc
->dev
->dev
, "%s: failed to set mode on plane: %d\n",
300 mdp5_crtc
->name
, ret
);
304 mdp5_write(mdp5_kms
, REG_MDP5_LM_OUT_SIZE(mdp5_crtc
->id
),
305 MDP5_LM_OUT_SIZE_WIDTH(mode
->hdisplay
) |
306 MDP5_LM_OUT_SIZE_HEIGHT(mode
->vdisplay
));
308 update_fb(crtc
, crtc
->fb
);
309 update_scanout(crtc
, crtc
->fb
);
314 static void mdp5_crtc_prepare(struct drm_crtc
*crtc
)
316 struct mdp5_crtc
*mdp5_crtc
= to_mdp5_crtc(crtc
);
317 DBG("%s", mdp5_crtc
->name
);
318 /* make sure we hold a ref to mdp clks while setting up mode: */
319 mdp5_enable(get_kms(crtc
));
320 mdp5_crtc_dpms(crtc
, DRM_MODE_DPMS_OFF
);
323 static void mdp5_crtc_commit(struct drm_crtc
*crtc
)
325 mdp5_crtc_dpms(crtc
, DRM_MODE_DPMS_ON
);
327 /* drop the ref to mdp clk's that we got in prepare: */
328 mdp5_disable(get_kms(crtc
));
331 static int mdp5_crtc_mode_set_base(struct drm_crtc
*crtc
, int x
, int y
,
332 struct drm_framebuffer
*old_fb
)
334 struct mdp5_crtc
*mdp5_crtc
= to_mdp5_crtc(crtc
);
335 struct drm_plane
*plane
= mdp5_crtc
->plane
;
336 struct drm_display_mode
*mode
= &crtc
->mode
;
339 /* grab extra ref for update_scanout() */
340 drm_framebuffer_reference(crtc
->fb
);
342 ret
= mdp5_plane_mode_set(plane
, crtc
, crtc
->fb
,
343 0, 0, mode
->hdisplay
, mode
->vdisplay
,
345 mode
->hdisplay
<< 16, mode
->vdisplay
<< 16);
347 update_fb(crtc
, crtc
->fb
);
348 update_scanout(crtc
, crtc
->fb
);
353 static void mdp5_crtc_load_lut(struct drm_crtc
*crtc
)
357 static int mdp5_crtc_page_flip(struct drm_crtc
*crtc
,
358 struct drm_framebuffer
*new_fb
,
359 struct drm_pending_vblank_event
*event
,
360 uint32_t page_flip_flags
)
362 struct mdp5_crtc
*mdp5_crtc
= to_mdp5_crtc(crtc
);
363 struct drm_device
*dev
= crtc
->dev
;
364 struct drm_gem_object
*obj
;
367 if (mdp5_crtc
->event
) {
368 dev_err(dev
->dev
, "already pending flip!\n");
372 obj
= msm_framebuffer_bo(new_fb
, 0);
374 spin_lock_irqsave(&dev
->event_lock
, flags
);
375 mdp5_crtc
->event
= event
;
376 spin_unlock_irqrestore(&dev
->event_lock
, flags
);
378 update_fb(crtc
, new_fb
);
380 return msm_gem_queue_inactive_cb(obj
, &mdp5_crtc
->pageflip_cb
);
383 static int mdp5_crtc_set_property(struct drm_crtc
*crtc
,
384 struct drm_property
*property
, uint64_t val
)
390 static const struct drm_crtc_funcs mdp5_crtc_funcs
= {
391 .set_config
= drm_crtc_helper_set_config
,
392 .destroy
= mdp5_crtc_destroy
,
393 .page_flip
= mdp5_crtc_page_flip
,
394 .set_property
= mdp5_crtc_set_property
,
397 static const struct drm_crtc_helper_funcs mdp5_crtc_helper_funcs
= {
398 .dpms
= mdp5_crtc_dpms
,
399 .mode_fixup
= mdp5_crtc_mode_fixup
,
400 .mode_set
= mdp5_crtc_mode_set
,
401 .prepare
= mdp5_crtc_prepare
,
402 .commit
= mdp5_crtc_commit
,
403 .mode_set_base
= mdp5_crtc_mode_set_base
,
404 .load_lut
= mdp5_crtc_load_lut
,
407 static void mdp5_crtc_vblank_irq(struct mdp_irq
*irq
, uint32_t irqstatus
)
409 struct mdp5_crtc
*mdp5_crtc
= container_of(irq
, struct mdp5_crtc
, vblank
);
410 struct drm_crtc
*crtc
= &mdp5_crtc
->base
;
411 struct msm_drm_private
*priv
= crtc
->dev
->dev_private
;
414 mdp_irq_unregister(&get_kms(crtc
)->base
, &mdp5_crtc
->vblank
);
416 pending
= atomic_xchg(&mdp5_crtc
->pending
, 0);
418 if (pending
& PENDING_FLIP
) {
419 complete_flip(crtc
, NULL
);
420 drm_flip_work_commit(&mdp5_crtc
->unref_fb_work
, priv
->wq
);
424 static void mdp5_crtc_err_irq(struct mdp_irq
*irq
, uint32_t irqstatus
)
426 struct mdp5_crtc
*mdp5_crtc
= container_of(irq
, struct mdp5_crtc
, err
);
427 struct drm_crtc
*crtc
= &mdp5_crtc
->base
;
428 DBG("%s: error: %08x", mdp5_crtc
->name
, irqstatus
);
432 uint32_t mdp5_crtc_vblank(struct drm_crtc
*crtc
)
434 struct mdp5_crtc
*mdp5_crtc
= to_mdp5_crtc(crtc
);
435 return mdp5_crtc
->vblank
.irqmask
;
438 void mdp5_crtc_cancel_pending_flip(struct drm_crtc
*crtc
, struct drm_file
*file
)
440 DBG("cancel: %p", file
);
441 complete_flip(crtc
, file
);
444 /* set interface for routing crtc->encoder: */
445 void mdp5_crtc_set_intf(struct drm_crtc
*crtc
, int intf
,
446 enum mdp5_intf intf_id
)
448 struct mdp5_crtc
*mdp5_crtc
= to_mdp5_crtc(crtc
);
449 struct mdp5_kms
*mdp5_kms
= get_kms(crtc
);
450 static const enum mdp5_intfnum intfnum
[] = {
451 INTF0
, INTF1
, INTF2
, INTF3
,
455 /* now that we know what irq's we want: */
456 mdp5_crtc
->err
.irqmask
= intf2err(intf
);
457 mdp5_crtc
->vblank
.irqmask
= intf2vblank(intf
);
459 /* when called from modeset_init(), skip the rest until later: */
463 intf_sel
= mdp5_read(mdp5_kms
, REG_MDP5_DISP_INTF_SEL
);
467 intf_sel
&= ~MDP5_DISP_INTF_SEL_INTF0__MASK
;
468 intf_sel
|= MDP5_DISP_INTF_SEL_INTF0(intf_id
);
471 intf_sel
&= ~MDP5_DISP_INTF_SEL_INTF1__MASK
;
472 intf_sel
|= MDP5_DISP_INTF_SEL_INTF1(intf_id
);
475 intf_sel
&= ~MDP5_DISP_INTF_SEL_INTF2__MASK
;
476 intf_sel
|= MDP5_DISP_INTF_SEL_INTF2(intf_id
);
479 intf_sel
&= ~MDP5_DISP_INTF_SEL_INTF3__MASK
;
480 intf_sel
|= MDP5_DISP_INTF_SEL_INTF3(intf_id
);
489 DBG("%s: intf_sel=%08x", mdp5_crtc
->name
, intf_sel
);
491 mdp5_write(mdp5_kms
, REG_MDP5_DISP_INTF_SEL
, intf_sel
);
492 mdp5_write(mdp5_kms
, REG_MDP5_CTL_OP(mdp5_crtc
->id
),
493 MDP5_CTL_OP_MODE(MODE_NONE
) |
494 MDP5_CTL_OP_INTF_NUM(intfnum
[intf
]));
499 static void set_attach(struct drm_crtc
*crtc
, enum mdp5_pipe pipe_id
,
500 struct drm_plane
*plane
)
502 struct mdp5_crtc
*mdp5_crtc
= to_mdp5_crtc(crtc
);
504 BUG_ON(pipe_id
>= ARRAY_SIZE(mdp5_crtc
->planes
));
506 if (mdp5_crtc
->planes
[pipe_id
] == plane
)
509 mdp5_crtc
->planes
[pipe_id
] = plane
;
511 if (mdp5_crtc
->enabled
&& (plane
!= mdp5_crtc
->plane
))
515 void mdp5_crtc_attach(struct drm_crtc
*crtc
, struct drm_plane
*plane
)
517 set_attach(crtc
, mdp5_plane_pipe(plane
), plane
);
520 void mdp5_crtc_detach(struct drm_crtc
*crtc
, struct drm_plane
*plane
)
522 set_attach(crtc
, mdp5_plane_pipe(plane
), NULL
);
525 /* initialize crtc */
526 struct drm_crtc
*mdp5_crtc_init(struct drm_device
*dev
,
527 struct drm_plane
*plane
, int id
)
529 struct drm_crtc
*crtc
= NULL
;
530 struct mdp5_crtc
*mdp5_crtc
;
533 mdp5_crtc
= kzalloc(sizeof(*mdp5_crtc
), GFP_KERNEL
);
539 crtc
= &mdp5_crtc
->base
;
541 mdp5_crtc
->plane
= plane
;
544 mdp5_crtc
->vblank
.irq
= mdp5_crtc_vblank_irq
;
545 mdp5_crtc
->err
.irq
= mdp5_crtc_err_irq
;
547 snprintf(mdp5_crtc
->name
, sizeof(mdp5_crtc
->name
), "%s:%d",
548 pipe2name(mdp5_plane_pipe(plane
)), id
);
550 ret
= drm_flip_work_init(&mdp5_crtc
->unref_fb_work
, 16,
551 "unref fb", unref_fb_worker
);
555 INIT_FENCE_CB(&mdp5_crtc
->pageflip_cb
, pageflip_cb
);
557 drm_crtc_init(dev
, crtc
, &mdp5_crtc_funcs
);
558 drm_crtc_helper_add(crtc
, &mdp5_crtc_helper_funcs
);
560 mdp5_plane_install_properties(mdp5_crtc
->plane
, &crtc
->base
);
566 mdp5_crtc_destroy(crtc
);