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