1 // SPDX-License-Identifier: GPL-2.0+
3 * rcar_du_vsp.h -- R-Car Display Unit VSP-Based Compositor
5 * Copyright (C) 2015 Renesas Electronics Corporation
7 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
10 #include <drm/drm_atomic.h>
11 #include <drm/drm_atomic_helper.h>
12 #include <drm/drm_crtc.h>
13 #include <drm/drm_fb_cma_helper.h>
14 #include <drm/drm_fourcc.h>
15 #include <drm/drm_gem_atomic_helper.h>
16 #include <drm/drm_gem_cma_helper.h>
17 #include <drm/drm_managed.h>
18 #include <drm/drm_plane_helper.h>
19 #include <drm/drm_vblank.h>
21 #include <linux/bitops.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/of_platform.h>
24 #include <linux/scatterlist.h>
25 #include <linux/slab.h>
26 #include <linux/videodev2.h>
28 #include <media/vsp1.h>
30 #include "rcar_du_drv.h"
31 #include "rcar_du_kms.h"
32 #include "rcar_du_vsp.h"
33 #include "rcar_du_writeback.h"
35 static void rcar_du_vsp_complete(void *private, unsigned int status, u32 crc)
37 struct rcar_du_crtc *crtc = private;
39 if (crtc->vblank_enable)
40 drm_crtc_handle_vblank(&crtc->crtc);
42 if (status & VSP1_DU_STATUS_COMPLETE)
43 rcar_du_crtc_finish_page_flip(crtc);
44 if (status & VSP1_DU_STATUS_WRITEBACK)
45 rcar_du_writeback_complete(crtc);
47 drm_crtc_add_crc_entry(&crtc->crtc, false, 0, &crc);
50 void rcar_du_vsp_enable(struct rcar_du_crtc *crtc)
52 const struct drm_display_mode *mode = &crtc->crtc.state->adjusted_mode;
53 struct rcar_du_device *rcdu = crtc->dev;
54 struct vsp1_du_lif_config cfg = {
55 .width = mode->hdisplay,
56 .height = mode->vdisplay,
57 .interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE,
58 .callback = rcar_du_vsp_complete,
59 .callback_data = crtc,
61 struct rcar_du_plane_state state = {
63 .alpha = DRM_BLEND_ALPHA_OPAQUE,
67 .dst.x2 = mode->hdisplay,
68 .dst.y2 = mode->vdisplay,
71 .src.x2 = mode->hdisplay << 16,
72 .src.y2 = mode->vdisplay << 16,
75 .format = rcar_du_format_info(DRM_FORMAT_ARGB8888),
76 .source = RCAR_DU_PLANE_VSPD1,
80 if (rcdu->info->gen >= 3)
81 state.hwindex = (crtc->index % 2) ? 2 : 0;
83 state.hwindex = crtc->index % 2;
85 __rcar_du_plane_setup(crtc->group, &state);
88 * Ensure that the plane source configuration takes effect by requesting
89 * a restart of the group. See rcar_du_plane_atomic_update() for a more
90 * detailed explanation.
92 * TODO: Check whether this is still needed on Gen3.
94 crtc->group->need_restart = true;
96 vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, &cfg);
99 void rcar_du_vsp_disable(struct rcar_du_crtc *crtc)
101 vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, NULL);
104 void rcar_du_vsp_atomic_begin(struct rcar_du_crtc *crtc)
106 vsp1_du_atomic_begin(crtc->vsp->vsp, crtc->vsp_pipe);
109 void rcar_du_vsp_atomic_flush(struct rcar_du_crtc *crtc)
111 struct vsp1_du_atomic_pipe_config cfg = { { 0, } };
112 struct rcar_du_crtc_state *state;
114 state = to_rcar_crtc_state(crtc->crtc.state);
115 cfg.crc = state->crc;
117 rcar_du_writeback_setup(crtc, &cfg.writeback);
119 vsp1_du_atomic_flush(crtc->vsp->vsp, crtc->vsp_pipe, &cfg);
122 static const u32 rcar_du_vsp_formats[] = {
150 static void rcar_du_vsp_plane_setup(struct rcar_du_vsp_plane *plane)
152 struct rcar_du_vsp_plane_state *state =
153 to_rcar_vsp_plane_state(plane->plane.state);
154 struct rcar_du_crtc *crtc = to_rcar_crtc(state->state.crtc);
155 struct drm_framebuffer *fb = plane->plane.state->fb;
156 const struct rcar_du_format_info *format;
157 struct vsp1_du_atomic_config cfg = {
159 .pitch = fb->pitches[0],
160 .alpha = state->state.alpha >> 8,
161 .zpos = state->state.zpos,
165 cfg.src.left = state->state.src.x1 >> 16;
166 cfg.src.top = state->state.src.y1 >> 16;
167 cfg.src.width = drm_rect_width(&state->state.src) >> 16;
168 cfg.src.height = drm_rect_height(&state->state.src) >> 16;
170 cfg.dst.left = state->state.dst.x1;
171 cfg.dst.top = state->state.dst.y1;
172 cfg.dst.width = drm_rect_width(&state->state.dst);
173 cfg.dst.height = drm_rect_height(&state->state.dst);
175 for (i = 0; i < state->format->planes; ++i)
176 cfg.mem[i] = sg_dma_address(state->sg_tables[i].sgl)
179 format = rcar_du_format_info(state->format->fourcc);
180 cfg.pixelformat = format->v4l2;
182 vsp1_du_atomic_update(plane->vsp->vsp, crtc->vsp_pipe,
186 int rcar_du_vsp_map_fb(struct rcar_du_vsp *vsp, struct drm_framebuffer *fb,
187 struct sg_table sg_tables[3])
189 struct rcar_du_device *rcdu = vsp->dev;
193 for (i = 0; i < fb->format->num_planes; ++i) {
194 struct drm_gem_cma_object *gem = drm_fb_cma_get_gem_obj(fb, i);
195 struct sg_table *sgt = &sg_tables[i];
198 struct scatterlist *src;
199 struct scatterlist *dst;
202 * If the GEM buffer has a scatter gather table, it has
203 * been imported from a dma-buf and has no physical
204 * address as it might not be physically contiguous.
205 * Copy the original scatter gather table to map it to
208 ret = sg_alloc_table(sgt, gem->sgt->orig_nents,
215 for (j = 0; j < gem->sgt->orig_nents; ++j) {
216 sg_set_page(dst, sg_page(src), src->length,
222 ret = dma_get_sgtable(rcdu->dev, sgt, gem->vaddr,
223 gem->paddr, gem->base.size);
228 ret = vsp1_du_map_sg(vsp->vsp, sgt);
239 struct sg_table *sgt = &sg_tables[i];
241 vsp1_du_unmap_sg(vsp->vsp, sgt);
248 static int rcar_du_vsp_plane_prepare_fb(struct drm_plane *plane,
249 struct drm_plane_state *state)
251 struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
252 struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;
256 * There's no need to prepare (and unprepare) the framebuffer when the
257 * plane is not visible, as it will not be displayed.
262 ret = rcar_du_vsp_map_fb(vsp, state->fb, rstate->sg_tables);
266 return drm_gem_plane_helper_prepare_fb(plane, state);
269 void rcar_du_vsp_unmap_fb(struct rcar_du_vsp *vsp, struct drm_framebuffer *fb,
270 struct sg_table sg_tables[3])
274 for (i = 0; i < fb->format->num_planes; ++i) {
275 struct sg_table *sgt = &sg_tables[i];
277 vsp1_du_unmap_sg(vsp->vsp, sgt);
282 static void rcar_du_vsp_plane_cleanup_fb(struct drm_plane *plane,
283 struct drm_plane_state *state)
285 struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
286 struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;
291 rcar_du_vsp_unmap_fb(vsp, state->fb, rstate->sg_tables);
294 static int rcar_du_vsp_plane_atomic_check(struct drm_plane *plane,
295 struct drm_atomic_state *state)
297 struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
299 struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(new_plane_state);
301 return __rcar_du_plane_atomic_check(plane, new_plane_state,
305 static void rcar_du_vsp_plane_atomic_update(struct drm_plane *plane,
306 struct drm_atomic_state *state)
308 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
309 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
310 struct rcar_du_vsp_plane *rplane = to_rcar_vsp_plane(plane);
311 struct rcar_du_crtc *crtc = to_rcar_crtc(old_state->crtc);
313 if (new_state->visible)
314 rcar_du_vsp_plane_setup(rplane);
315 else if (old_state->crtc)
316 vsp1_du_atomic_update(rplane->vsp->vsp, crtc->vsp_pipe,
317 rplane->index, NULL);
320 static const struct drm_plane_helper_funcs rcar_du_vsp_plane_helper_funcs = {
321 .prepare_fb = rcar_du_vsp_plane_prepare_fb,
322 .cleanup_fb = rcar_du_vsp_plane_cleanup_fb,
323 .atomic_check = rcar_du_vsp_plane_atomic_check,
324 .atomic_update = rcar_du_vsp_plane_atomic_update,
327 static struct drm_plane_state *
328 rcar_du_vsp_plane_atomic_duplicate_state(struct drm_plane *plane)
330 struct rcar_du_vsp_plane_state *copy;
332 if (WARN_ON(!plane->state))
335 copy = kzalloc(sizeof(*copy), GFP_KERNEL);
339 __drm_atomic_helper_plane_duplicate_state(plane, ©->state);
344 static void rcar_du_vsp_plane_atomic_destroy_state(struct drm_plane *plane,
345 struct drm_plane_state *state)
347 __drm_atomic_helper_plane_destroy_state(state);
348 kfree(to_rcar_vsp_plane_state(state));
351 static void rcar_du_vsp_plane_reset(struct drm_plane *plane)
353 struct rcar_du_vsp_plane_state *state;
356 rcar_du_vsp_plane_atomic_destroy_state(plane, plane->state);
360 state = kzalloc(sizeof(*state), GFP_KERNEL);
364 __drm_atomic_helper_plane_reset(plane, &state->state);
365 state->state.zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
368 static const struct drm_plane_funcs rcar_du_vsp_plane_funcs = {
369 .update_plane = drm_atomic_helper_update_plane,
370 .disable_plane = drm_atomic_helper_disable_plane,
371 .reset = rcar_du_vsp_plane_reset,
372 .destroy = drm_plane_cleanup,
373 .atomic_duplicate_state = rcar_du_vsp_plane_atomic_duplicate_state,
374 .atomic_destroy_state = rcar_du_vsp_plane_atomic_destroy_state,
377 static void rcar_du_vsp_cleanup(struct drm_device *dev, void *res)
379 struct rcar_du_vsp *vsp = res;
382 for (i = 0; i < vsp->num_planes; ++i) {
383 struct rcar_du_vsp_plane *plane = &vsp->planes[i];
385 drm_plane_cleanup(&plane->plane);
390 put_device(vsp->vsp);
393 int rcar_du_vsp_init(struct rcar_du_vsp *vsp, struct device_node *np,
396 struct rcar_du_device *rcdu = vsp->dev;
397 struct platform_device *pdev;
398 unsigned int num_crtcs = hweight32(crtcs);
399 unsigned int num_planes;
403 /* Find the VSP device and initialize it. */
404 pdev = of_find_device_by_node(np);
408 vsp->vsp = &pdev->dev;
410 ret = drmm_add_action_or_reset(&rcdu->ddev, rcar_du_vsp_cleanup, vsp);
414 ret = vsp1_du_init(vsp->vsp);
419 * The VSP2D (Gen3) has 5 RPFs, but the VSP1D (Gen2) is limited to
422 num_planes = rcdu->info->gen >= 3 ? 5 : 4;
424 vsp->planes = kcalloc(num_planes, sizeof(*vsp->planes), GFP_KERNEL);
428 for (i = 0; i < num_planes; ++i) {
429 enum drm_plane_type type = i < num_crtcs
430 ? DRM_PLANE_TYPE_PRIMARY
431 : DRM_PLANE_TYPE_OVERLAY;
432 struct rcar_du_vsp_plane *plane = &vsp->planes[i];
437 ret = drm_universal_plane_init(&rcdu->ddev, &plane->plane,
438 crtcs, &rcar_du_vsp_plane_funcs,
440 ARRAY_SIZE(rcar_du_vsp_formats),
445 drm_plane_helper_add(&plane->plane,
446 &rcar_du_vsp_plane_helper_funcs);
448 if (type == DRM_PLANE_TYPE_PRIMARY) {
449 drm_plane_create_zpos_immutable_property(&plane->plane,
452 drm_plane_create_alpha_property(&plane->plane);
453 drm_plane_create_zpos_property(&plane->plane, 1, 1,