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_helper.h>
11 #include <drm/drm_crtc.h>
12 #include <drm/drm_fb_cma_helper.h>
13 #include <drm/drm_fourcc.h>
14 #include <drm/drm_gem_cma_helper.h>
15 #include <drm/drm_gem_framebuffer_helper.h>
16 #include <drm/drm_plane_helper.h>
17 #include <drm/drm_vblank.h>
19 #include <linux/bitops.h>
20 #include <linux/dma-mapping.h>
21 #include <linux/of_platform.h>
22 #include <linux/scatterlist.h>
23 #include <linux/videodev2.h>
25 #include <media/vsp1.h>
27 #include "rcar_du_drv.h"
28 #include "rcar_du_kms.h"
29 #include "rcar_du_vsp.h"
30 #include "rcar_du_writeback.h"
32 static void rcar_du_vsp_complete(void *private, unsigned int status, u32 crc)
34 struct rcar_du_crtc *crtc = private;
36 if (crtc->vblank_enable)
37 drm_crtc_handle_vblank(&crtc->crtc);
39 if (status & VSP1_DU_STATUS_COMPLETE)
40 rcar_du_crtc_finish_page_flip(crtc);
41 if (status & VSP1_DU_STATUS_WRITEBACK)
42 rcar_du_writeback_complete(crtc);
44 drm_crtc_add_crc_entry(&crtc->crtc, false, 0, &crc);
47 void rcar_du_vsp_enable(struct rcar_du_crtc *crtc)
49 const struct drm_display_mode *mode = &crtc->crtc.state->adjusted_mode;
50 struct rcar_du_device *rcdu = crtc->dev;
51 struct vsp1_du_lif_config cfg = {
52 .width = mode->hdisplay,
53 .height = mode->vdisplay,
54 .interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE,
55 .callback = rcar_du_vsp_complete,
56 .callback_data = crtc,
58 struct rcar_du_plane_state state = {
60 .alpha = DRM_BLEND_ALPHA_OPAQUE,
64 .dst.x2 = mode->hdisplay,
65 .dst.y2 = mode->vdisplay,
68 .src.x2 = mode->hdisplay << 16,
69 .src.y2 = mode->vdisplay << 16,
72 .format = rcar_du_format_info(DRM_FORMAT_ARGB8888),
73 .source = RCAR_DU_PLANE_VSPD1,
77 if (rcdu->info->gen >= 3)
78 state.hwindex = (crtc->index % 2) ? 2 : 0;
80 state.hwindex = crtc->index % 2;
82 __rcar_du_plane_setup(crtc->group, &state);
85 * Ensure that the plane source configuration takes effect by requesting
86 * a restart of the group. See rcar_du_plane_atomic_update() for a more
87 * detailed explanation.
89 * TODO: Check whether this is still needed on Gen3.
91 crtc->group->need_restart = true;
93 vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, &cfg);
96 void rcar_du_vsp_disable(struct rcar_du_crtc *crtc)
98 vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, NULL);
101 void rcar_du_vsp_atomic_begin(struct rcar_du_crtc *crtc)
103 vsp1_du_atomic_begin(crtc->vsp->vsp, crtc->vsp_pipe);
106 void rcar_du_vsp_atomic_flush(struct rcar_du_crtc *crtc)
108 struct vsp1_du_atomic_pipe_config cfg = { { 0, } };
109 struct rcar_du_crtc_state *state;
111 state = to_rcar_crtc_state(crtc->crtc.state);
112 cfg.crc = state->crc;
114 rcar_du_writeback_setup(crtc, &cfg.writeback);
116 vsp1_du_atomic_flush(crtc->vsp->vsp, crtc->vsp_pipe, &cfg);
119 static const u32 rcar_du_vsp_formats[] = {
147 static void rcar_du_vsp_plane_setup(struct rcar_du_vsp_plane *plane)
149 struct rcar_du_vsp_plane_state *state =
150 to_rcar_vsp_plane_state(plane->plane.state);
151 struct rcar_du_crtc *crtc = to_rcar_crtc(state->state.crtc);
152 struct drm_framebuffer *fb = plane->plane.state->fb;
153 const struct rcar_du_format_info *format;
154 struct vsp1_du_atomic_config cfg = {
156 .pitch = fb->pitches[0],
157 .alpha = state->state.alpha >> 8,
158 .zpos = state->state.zpos,
162 cfg.src.left = state->state.src.x1 >> 16;
163 cfg.src.top = state->state.src.y1 >> 16;
164 cfg.src.width = drm_rect_width(&state->state.src) >> 16;
165 cfg.src.height = drm_rect_height(&state->state.src) >> 16;
167 cfg.dst.left = state->state.dst.x1;
168 cfg.dst.top = state->state.dst.y1;
169 cfg.dst.width = drm_rect_width(&state->state.dst);
170 cfg.dst.height = drm_rect_height(&state->state.dst);
172 for (i = 0; i < state->format->planes; ++i)
173 cfg.mem[i] = sg_dma_address(state->sg_tables[i].sgl)
176 format = rcar_du_format_info(state->format->fourcc);
177 cfg.pixelformat = format->v4l2;
179 vsp1_du_atomic_update(plane->vsp->vsp, crtc->vsp_pipe,
183 int rcar_du_vsp_map_fb(struct rcar_du_vsp *vsp, struct drm_framebuffer *fb,
184 struct sg_table sg_tables[3])
186 struct rcar_du_device *rcdu = vsp->dev;
190 for (i = 0; i < fb->format->num_planes; ++i) {
191 struct drm_gem_cma_object *gem = drm_fb_cma_get_gem_obj(fb, i);
192 struct sg_table *sgt = &sg_tables[i];
194 ret = dma_get_sgtable(rcdu->dev, sgt, gem->vaddr, gem->paddr,
199 ret = vsp1_du_map_sg(vsp->vsp, sgt);
210 struct sg_table *sgt = &sg_tables[i];
212 vsp1_du_unmap_sg(vsp->vsp, sgt);
219 static int rcar_du_vsp_plane_prepare_fb(struct drm_plane *plane,
220 struct drm_plane_state *state)
222 struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
223 struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;
227 * There's no need to prepare (and unprepare) the framebuffer when the
228 * plane is not visible, as it will not be displayed.
233 ret = rcar_du_vsp_map_fb(vsp, state->fb, rstate->sg_tables);
237 return drm_gem_fb_prepare_fb(plane, state);
240 void rcar_du_vsp_unmap_fb(struct rcar_du_vsp *vsp, struct drm_framebuffer *fb,
241 struct sg_table sg_tables[3])
245 for (i = 0; i < fb->format->num_planes; ++i) {
246 struct sg_table *sgt = &sg_tables[i];
248 vsp1_du_unmap_sg(vsp->vsp, sgt);
253 static void rcar_du_vsp_plane_cleanup_fb(struct drm_plane *plane,
254 struct drm_plane_state *state)
256 struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
257 struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;
262 rcar_du_vsp_unmap_fb(vsp, state->fb, rstate->sg_tables);
265 static int rcar_du_vsp_plane_atomic_check(struct drm_plane *plane,
266 struct drm_plane_state *state)
268 struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
270 return __rcar_du_plane_atomic_check(plane, state, &rstate->format);
273 static void rcar_du_vsp_plane_atomic_update(struct drm_plane *plane,
274 struct drm_plane_state *old_state)
276 struct rcar_du_vsp_plane *rplane = to_rcar_vsp_plane(plane);
277 struct rcar_du_crtc *crtc = to_rcar_crtc(old_state->crtc);
279 if (plane->state->visible)
280 rcar_du_vsp_plane_setup(rplane);
282 vsp1_du_atomic_update(rplane->vsp->vsp, crtc->vsp_pipe,
283 rplane->index, NULL);
286 static const struct drm_plane_helper_funcs rcar_du_vsp_plane_helper_funcs = {
287 .prepare_fb = rcar_du_vsp_plane_prepare_fb,
288 .cleanup_fb = rcar_du_vsp_plane_cleanup_fb,
289 .atomic_check = rcar_du_vsp_plane_atomic_check,
290 .atomic_update = rcar_du_vsp_plane_atomic_update,
293 static struct drm_plane_state *
294 rcar_du_vsp_plane_atomic_duplicate_state(struct drm_plane *plane)
296 struct rcar_du_vsp_plane_state *copy;
298 if (WARN_ON(!plane->state))
301 copy = kzalloc(sizeof(*copy), GFP_KERNEL);
305 __drm_atomic_helper_plane_duplicate_state(plane, ©->state);
310 static void rcar_du_vsp_plane_atomic_destroy_state(struct drm_plane *plane,
311 struct drm_plane_state *state)
313 __drm_atomic_helper_plane_destroy_state(state);
314 kfree(to_rcar_vsp_plane_state(state));
317 static void rcar_du_vsp_plane_reset(struct drm_plane *plane)
319 struct rcar_du_vsp_plane_state *state;
322 rcar_du_vsp_plane_atomic_destroy_state(plane, plane->state);
326 state = kzalloc(sizeof(*state), GFP_KERNEL);
330 __drm_atomic_helper_plane_reset(plane, &state->state);
331 state->state.zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
334 static const struct drm_plane_funcs rcar_du_vsp_plane_funcs = {
335 .update_plane = drm_atomic_helper_update_plane,
336 .disable_plane = drm_atomic_helper_disable_plane,
337 .reset = rcar_du_vsp_plane_reset,
338 .destroy = drm_plane_cleanup,
339 .atomic_duplicate_state = rcar_du_vsp_plane_atomic_duplicate_state,
340 .atomic_destroy_state = rcar_du_vsp_plane_atomic_destroy_state,
343 int rcar_du_vsp_init(struct rcar_du_vsp *vsp, struct device_node *np,
346 struct rcar_du_device *rcdu = vsp->dev;
347 struct platform_device *pdev;
348 unsigned int num_crtcs = hweight32(crtcs);
352 /* Find the VSP device and initialize it. */
353 pdev = of_find_device_by_node(np);
357 vsp->vsp = &pdev->dev;
359 ret = vsp1_du_init(vsp->vsp);
364 * The VSP2D (Gen3) has 5 RPFs, but the VSP1D (Gen2) is limited to
367 vsp->num_planes = rcdu->info->gen >= 3 ? 5 : 4;
369 vsp->planes = devm_kcalloc(rcdu->dev, vsp->num_planes,
370 sizeof(*vsp->planes), GFP_KERNEL);
374 for (i = 0; i < vsp->num_planes; ++i) {
375 enum drm_plane_type type = i < num_crtcs
376 ? DRM_PLANE_TYPE_PRIMARY
377 : DRM_PLANE_TYPE_OVERLAY;
378 struct rcar_du_vsp_plane *plane = &vsp->planes[i];
383 ret = drm_universal_plane_init(rcdu->ddev, &plane->plane, crtcs,
384 &rcar_du_vsp_plane_funcs,
386 ARRAY_SIZE(rcar_du_vsp_formats),
391 drm_plane_helper_add(&plane->plane,
392 &rcar_du_vsp_plane_helper_funcs);
394 if (type == DRM_PLANE_TYPE_PRIMARY) {
395 drm_plane_create_zpos_immutable_property(&plane->plane,
398 drm_plane_create_alpha_property(&plane->plane);
399 drm_plane_create_zpos_property(&plane->plane, 1, 1,
400 vsp->num_planes - 1);