Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * rcar_du_vsp.h -- R-Car Display Unit VSP-Based Compositor
4 *
5 * Copyright (C) 2015 Renesas Electronics Corporation
6 *
7 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8 */
9
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_managed.h>
17#include <drm/drm_plane_helper.h>
18#include <drm/drm_vblank.h>
19
20#include <linux/bitops.h>
21#include <linux/dma-mapping.h>
22#include <linux/of_platform.h>
23#include <linux/scatterlist.h>
24#include <linux/slab.h>
25#include <linux/videodev2.h>
26
27#include <media/vsp1.h>
28
29#include "rcar_du_drv.h"
30#include "rcar_du_kms.h"
31#include "rcar_du_vsp.h"
32#include "rcar_du_writeback.h"
33
34static void rcar_du_vsp_complete(void *private, unsigned int status, u32 crc)
35{
36 struct rcar_du_crtc *crtc = private;
37
38 if (crtc->vblank_enable)
39 drm_crtc_handle_vblank(&crtc->crtc);
40
41 if (status & VSP1_DU_STATUS_COMPLETE)
42 rcar_du_crtc_finish_page_flip(crtc);
43 if (status & VSP1_DU_STATUS_WRITEBACK)
44 rcar_du_writeback_complete(crtc);
45
46 drm_crtc_add_crc_entry(&crtc->crtc, false, 0, &crc);
47}
48
49void rcar_du_vsp_enable(struct rcar_du_crtc *crtc)
50{
51 const struct drm_display_mode *mode = &crtc->crtc.state->adjusted_mode;
52 struct rcar_du_device *rcdu = crtc->dev;
53 struct vsp1_du_lif_config cfg = {
54 .width = mode->hdisplay,
55 .height = mode->vdisplay,
56 .interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE,
57 .callback = rcar_du_vsp_complete,
58 .callback_data = crtc,
59 };
60 struct rcar_du_plane_state state = {
61 .state = {
62 .alpha = DRM_BLEND_ALPHA_OPAQUE,
63 .crtc = &crtc->crtc,
64 .dst.x1 = 0,
65 .dst.y1 = 0,
66 .dst.x2 = mode->hdisplay,
67 .dst.y2 = mode->vdisplay,
68 .src.x1 = 0,
69 .src.y1 = 0,
70 .src.x2 = mode->hdisplay << 16,
71 .src.y2 = mode->vdisplay << 16,
72 .zpos = 0,
73 },
74 .format = rcar_du_format_info(DRM_FORMAT_ARGB8888),
75 .source = RCAR_DU_PLANE_VSPD1,
76 .colorkey = 0,
77 };
78
79 if (rcdu->info->gen >= 3)
80 state.hwindex = (crtc->index % 2) ? 2 : 0;
81 else
82 state.hwindex = crtc->index % 2;
83
84 __rcar_du_plane_setup(crtc->group, &state);
85
86 /*
87 * Ensure that the plane source configuration takes effect by requesting
88 * a restart of the group. See rcar_du_plane_atomic_update() for a more
89 * detailed explanation.
90 *
91 * TODO: Check whether this is still needed on Gen3.
92 */
93 crtc->group->need_restart = true;
94
95 vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, &cfg);
96}
97
98void rcar_du_vsp_disable(struct rcar_du_crtc *crtc)
99{
100 vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, NULL);
101}
102
103void rcar_du_vsp_atomic_begin(struct rcar_du_crtc *crtc)
104{
105 vsp1_du_atomic_begin(crtc->vsp->vsp, crtc->vsp_pipe);
106}
107
108void rcar_du_vsp_atomic_flush(struct rcar_du_crtc *crtc)
109{
110 struct vsp1_du_atomic_pipe_config cfg = { { 0, } };
111 struct rcar_du_crtc_state *state;
112
113 state = to_rcar_crtc_state(crtc->crtc.state);
114 cfg.crc = state->crc;
115
116 rcar_du_writeback_setup(crtc, &cfg.writeback);
117
118 vsp1_du_atomic_flush(crtc->vsp->vsp, crtc->vsp_pipe, &cfg);
119}
120
121static const u32 rcar_du_vsp_formats[] = {
122 DRM_FORMAT_RGB332,
123 DRM_FORMAT_ARGB4444,
124 DRM_FORMAT_XRGB4444,
125 DRM_FORMAT_ARGB1555,
126 DRM_FORMAT_XRGB1555,
127 DRM_FORMAT_RGB565,
128 DRM_FORMAT_BGR888,
129 DRM_FORMAT_RGB888,
130 DRM_FORMAT_BGRA8888,
131 DRM_FORMAT_BGRX8888,
132 DRM_FORMAT_ARGB8888,
133 DRM_FORMAT_XRGB8888,
134 DRM_FORMAT_UYVY,
135 DRM_FORMAT_YUYV,
136 DRM_FORMAT_YVYU,
137 DRM_FORMAT_NV12,
138 DRM_FORMAT_NV21,
139 DRM_FORMAT_NV16,
140 DRM_FORMAT_NV61,
141 DRM_FORMAT_YUV420,
142 DRM_FORMAT_YVU420,
143 DRM_FORMAT_YUV422,
144 DRM_FORMAT_YVU422,
145 DRM_FORMAT_YUV444,
146 DRM_FORMAT_YVU444,
147};
148
149static void rcar_du_vsp_plane_setup(struct rcar_du_vsp_plane *plane)
150{
151 struct rcar_du_vsp_plane_state *state =
152 to_rcar_vsp_plane_state(plane->plane.state);
153 struct rcar_du_crtc *crtc = to_rcar_crtc(state->state.crtc);
154 struct drm_framebuffer *fb = plane->plane.state->fb;
155 const struct rcar_du_format_info *format;
156 struct vsp1_du_atomic_config cfg = {
157 .pixelformat = 0,
158 .pitch = fb->pitches[0],
159 .alpha = state->state.alpha >> 8,
160 .zpos = state->state.zpos,
161 };
162 unsigned int i;
163
164 cfg.src.left = state->state.src.x1 >> 16;
165 cfg.src.top = state->state.src.y1 >> 16;
166 cfg.src.width = drm_rect_width(&state->state.src) >> 16;
167 cfg.src.height = drm_rect_height(&state->state.src) >> 16;
168
169 cfg.dst.left = state->state.dst.x1;
170 cfg.dst.top = state->state.dst.y1;
171 cfg.dst.width = drm_rect_width(&state->state.dst);
172 cfg.dst.height = drm_rect_height(&state->state.dst);
173
174 for (i = 0; i < state->format->planes; ++i)
175 cfg.mem[i] = sg_dma_address(state->sg_tables[i].sgl)
176 + fb->offsets[i];
177
178 format = rcar_du_format_info(state->format->fourcc);
179 cfg.pixelformat = format->v4l2;
180
181 vsp1_du_atomic_update(plane->vsp->vsp, crtc->vsp_pipe,
182 plane->index, &cfg);
183}
184
185int rcar_du_vsp_map_fb(struct rcar_du_vsp *vsp, struct drm_framebuffer *fb,
186 struct sg_table sg_tables[3])
187{
188 struct rcar_du_device *rcdu = vsp->dev;
189 unsigned int i;
190 int ret;
191
192 for (i = 0; i < fb->format->num_planes; ++i) {
193 struct drm_gem_cma_object *gem = drm_fb_cma_get_gem_obj(fb, i);
194 struct sg_table *sgt = &sg_tables[i];
195
196 ret = dma_get_sgtable(rcdu->dev, sgt, gem->vaddr, gem->paddr,
197 gem->base.size);
198 if (ret)
199 goto fail;
200
201 ret = vsp1_du_map_sg(vsp->vsp, sgt);
202 if (ret) {
203 sg_free_table(sgt);
204 goto fail;
205 }
206 }
207
208 return 0;
209
210fail:
211 while (i--) {
212 struct sg_table *sgt = &sg_tables[i];
213
214 vsp1_du_unmap_sg(vsp->vsp, sgt);
215 sg_free_table(sgt);
216 }
217
218 return ret;
219}
220
221static int rcar_du_vsp_plane_prepare_fb(struct drm_plane *plane,
222 struct drm_plane_state *state)
223{
224 struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
225 struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;
226 int ret;
227
228 /*
229 * There's no need to prepare (and unprepare) the framebuffer when the
230 * plane is not visible, as it will not be displayed.
231 */
232 if (!state->visible)
233 return 0;
234
235 ret = rcar_du_vsp_map_fb(vsp, state->fb, rstate->sg_tables);
236 if (ret < 0)
237 return ret;
238
239 return drm_gem_fb_prepare_fb(plane, state);
240}
241
242void rcar_du_vsp_unmap_fb(struct rcar_du_vsp *vsp, struct drm_framebuffer *fb,
243 struct sg_table sg_tables[3])
244{
245 unsigned int i;
246
247 for (i = 0; i < fb->format->num_planes; ++i) {
248 struct sg_table *sgt = &sg_tables[i];
249
250 vsp1_du_unmap_sg(vsp->vsp, sgt);
251 sg_free_table(sgt);
252 }
253}
254
255static void rcar_du_vsp_plane_cleanup_fb(struct drm_plane *plane,
256 struct drm_plane_state *state)
257{
258 struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
259 struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;
260
261 if (!state->visible)
262 return;
263
264 rcar_du_vsp_unmap_fb(vsp, state->fb, rstate->sg_tables);
265}
266
267static int rcar_du_vsp_plane_atomic_check(struct drm_plane *plane,
268 struct drm_plane_state *state)
269{
270 struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
271
272 return __rcar_du_plane_atomic_check(plane, state, &rstate->format);
273}
274
275static void rcar_du_vsp_plane_atomic_update(struct drm_plane *plane,
276 struct drm_plane_state *old_state)
277{
278 struct rcar_du_vsp_plane *rplane = to_rcar_vsp_plane(plane);
279 struct rcar_du_crtc *crtc = to_rcar_crtc(old_state->crtc);
280
281 if (plane->state->visible)
282 rcar_du_vsp_plane_setup(rplane);
283 else if (old_state->crtc)
284 vsp1_du_atomic_update(rplane->vsp->vsp, crtc->vsp_pipe,
285 rplane->index, NULL);
286}
287
288static const struct drm_plane_helper_funcs rcar_du_vsp_plane_helper_funcs = {
289 .prepare_fb = rcar_du_vsp_plane_prepare_fb,
290 .cleanup_fb = rcar_du_vsp_plane_cleanup_fb,
291 .atomic_check = rcar_du_vsp_plane_atomic_check,
292 .atomic_update = rcar_du_vsp_plane_atomic_update,
293};
294
295static struct drm_plane_state *
296rcar_du_vsp_plane_atomic_duplicate_state(struct drm_plane *plane)
297{
298 struct rcar_du_vsp_plane_state *copy;
299
300 if (WARN_ON(!plane->state))
301 return NULL;
302
303 copy = kzalloc(sizeof(*copy), GFP_KERNEL);
304 if (copy == NULL)
305 return NULL;
306
307 __drm_atomic_helper_plane_duplicate_state(plane, ©->state);
308
309 return ©->state;
310}
311
312static void rcar_du_vsp_plane_atomic_destroy_state(struct drm_plane *plane,
313 struct drm_plane_state *state)
314{
315 __drm_atomic_helper_plane_destroy_state(state);
316 kfree(to_rcar_vsp_plane_state(state));
317}
318
319static void rcar_du_vsp_plane_reset(struct drm_plane *plane)
320{
321 struct rcar_du_vsp_plane_state *state;
322
323 if (plane->state) {
324 rcar_du_vsp_plane_atomic_destroy_state(plane, plane->state);
325 plane->state = NULL;
326 }
327
328 state = kzalloc(sizeof(*state), GFP_KERNEL);
329 if (state == NULL)
330 return;
331
332 __drm_atomic_helper_plane_reset(plane, &state->state);
333 state->state.zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
334}
335
336static const struct drm_plane_funcs rcar_du_vsp_plane_funcs = {
337 .update_plane = drm_atomic_helper_update_plane,
338 .disable_plane = drm_atomic_helper_disable_plane,
339 .reset = rcar_du_vsp_plane_reset,
340 .destroy = drm_plane_cleanup,
341 .atomic_duplicate_state = rcar_du_vsp_plane_atomic_duplicate_state,
342 .atomic_destroy_state = rcar_du_vsp_plane_atomic_destroy_state,
343};
344
345static void rcar_du_vsp_cleanup(struct drm_device *dev, void *res)
346{
347 struct rcar_du_vsp *vsp = res;
348 unsigned int i;
349
350 for (i = 0; i < vsp->num_planes; ++i) {
351 struct rcar_du_vsp_plane *plane = &vsp->planes[i];
352
353 drm_plane_cleanup(&plane->plane);
354 }
355
356 kfree(vsp->planes);
357
358 put_device(vsp->vsp);
359}
360
361int rcar_du_vsp_init(struct rcar_du_vsp *vsp, struct device_node *np,
362 unsigned int crtcs)
363{
364 struct rcar_du_device *rcdu = vsp->dev;
365 struct platform_device *pdev;
366 unsigned int num_crtcs = hweight32(crtcs);
367 unsigned int num_planes;
368 unsigned int i;
369 int ret;
370
371 /* Find the VSP device and initialize it. */
372 pdev = of_find_device_by_node(np);
373 if (!pdev)
374 return -ENXIO;
375
376 vsp->vsp = &pdev->dev;
377
378 ret = drmm_add_action_or_reset(&rcdu->ddev, rcar_du_vsp_cleanup, vsp);
379 if (ret < 0)
380 return ret;
381
382 ret = vsp1_du_init(vsp->vsp);
383 if (ret < 0)
384 return ret;
385
386 /*
387 * The VSP2D (Gen3) has 5 RPFs, but the VSP1D (Gen2) is limited to
388 * 4 RPFs.
389 */
390 num_planes = rcdu->info->gen >= 3 ? 5 : 4;
391
392 vsp->planes = kcalloc(num_planes, sizeof(*vsp->planes), GFP_KERNEL);
393 if (!vsp->planes)
394 return -ENOMEM;
395
396 for (i = 0; i < num_planes; ++i) {
397 enum drm_plane_type type = i < num_crtcs
398 ? DRM_PLANE_TYPE_PRIMARY
399 : DRM_PLANE_TYPE_OVERLAY;
400 struct rcar_du_vsp_plane *plane = &vsp->planes[i];
401
402 plane->vsp = vsp;
403 plane->index = i;
404
405 ret = drm_universal_plane_init(&rcdu->ddev, &plane->plane,
406 crtcs, &rcar_du_vsp_plane_funcs,
407 rcar_du_vsp_formats,
408 ARRAY_SIZE(rcar_du_vsp_formats),
409 NULL, type, NULL);
410 if (ret < 0)
411 return ret;
412
413 drm_plane_helper_add(&plane->plane,
414 &rcar_du_vsp_plane_helper_funcs);
415
416 if (type == DRM_PLANE_TYPE_PRIMARY) {
417 drm_plane_create_zpos_immutable_property(&plane->plane,
418 0);
419 } else {
420 drm_plane_create_alpha_property(&plane->plane);
421 drm_plane_create_zpos_property(&plane->plane, 1, 1,
422 num_planes - 1);
423 }
424
425 vsp->num_planes++;
426 }
427
428 return 0;
429}