linux/drivers/gpu/drm/rcar-du/rcar_du_vsp.c
<<
>>
Prefs
   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.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>
  20
  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>
  27
  28#include <media/vsp1.h>
  29
  30#include "rcar_du_drv.h"
  31#include "rcar_du_kms.h"
  32#include "rcar_du_vsp.h"
  33#include "rcar_du_writeback.h"
  34
  35static void rcar_du_vsp_complete(void *private, unsigned int status, u32 crc)
  36{
  37        struct rcar_du_crtc *crtc = private;
  38
  39        if (crtc->vblank_enable)
  40                drm_crtc_handle_vblank(&crtc->crtc);
  41
  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);
  46
  47        drm_crtc_add_crc_entry(&crtc->crtc, false, 0, &crc);
  48}
  49
  50void rcar_du_vsp_enable(struct rcar_du_crtc *crtc)
  51{
  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,
  60        };
  61        struct rcar_du_plane_state state = {
  62                .state = {
  63                        .alpha = DRM_BLEND_ALPHA_OPAQUE,
  64                        .crtc = &crtc->crtc,
  65                        .dst.x1 = 0,
  66                        .dst.y1 = 0,
  67                        .dst.x2 = mode->hdisplay,
  68                        .dst.y2 = mode->vdisplay,
  69                        .src.x1 = 0,
  70                        .src.y1 = 0,
  71                        .src.x2 = mode->hdisplay << 16,
  72                        .src.y2 = mode->vdisplay << 16,
  73                        .zpos = 0,
  74                },
  75                .format = rcar_du_format_info(DRM_FORMAT_ARGB8888),
  76                .source = RCAR_DU_PLANE_VSPD1,
  77                .colorkey = 0,
  78        };
  79
  80        if (rcdu->info->gen >= 3)
  81                state.hwindex = (crtc->index % 2) ? 2 : 0;
  82        else
  83                state.hwindex = crtc->index % 2;
  84
  85        __rcar_du_plane_setup(crtc->group, &state);
  86
  87        /*
  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.
  91         *
  92         * TODO: Check whether this is still needed on Gen3.
  93         */
  94        crtc->group->need_restart = true;
  95
  96        vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, &cfg);
  97}
  98
  99void rcar_du_vsp_disable(struct rcar_du_crtc *crtc)
 100{
 101        vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, NULL);
 102}
 103
 104void rcar_du_vsp_atomic_begin(struct rcar_du_crtc *crtc)
 105{
 106        vsp1_du_atomic_begin(crtc->vsp->vsp, crtc->vsp_pipe);
 107}
 108
 109void rcar_du_vsp_atomic_flush(struct rcar_du_crtc *crtc)
 110{
 111        struct vsp1_du_atomic_pipe_config cfg = { { 0, } };
 112        struct rcar_du_crtc_state *state;
 113
 114        state = to_rcar_crtc_state(crtc->crtc.state);
 115        cfg.crc = state->crc;
 116
 117        rcar_du_writeback_setup(crtc, &cfg.writeback);
 118
 119        vsp1_du_atomic_flush(crtc->vsp->vsp, crtc->vsp_pipe, &cfg);
 120}
 121
 122static const u32 rcar_du_vsp_formats[] = {
 123        DRM_FORMAT_RGB332,
 124        DRM_FORMAT_ARGB4444,
 125        DRM_FORMAT_XRGB4444,
 126        DRM_FORMAT_ARGB1555,
 127        DRM_FORMAT_XRGB1555,
 128        DRM_FORMAT_RGB565,
 129        DRM_FORMAT_BGR888,
 130        DRM_FORMAT_RGB888,
 131        DRM_FORMAT_BGRA8888,
 132        DRM_FORMAT_BGRX8888,
 133        DRM_FORMAT_ARGB8888,
 134        DRM_FORMAT_XRGB8888,
 135        DRM_FORMAT_UYVY,
 136        DRM_FORMAT_YUYV,
 137        DRM_FORMAT_YVYU,
 138        DRM_FORMAT_NV12,
 139        DRM_FORMAT_NV21,
 140        DRM_FORMAT_NV16,
 141        DRM_FORMAT_NV61,
 142        DRM_FORMAT_YUV420,
 143        DRM_FORMAT_YVU420,
 144        DRM_FORMAT_YUV422,
 145        DRM_FORMAT_YVU422,
 146        DRM_FORMAT_YUV444,
 147        DRM_FORMAT_YVU444,
 148};
 149
 150static void rcar_du_vsp_plane_setup(struct rcar_du_vsp_plane *plane)
 151{
 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 = {
 158                .pixelformat = 0,
 159                .pitch = fb->pitches[0],
 160                .alpha = state->state.alpha >> 8,
 161                .zpos = state->state.zpos,
 162        };
 163        unsigned int i;
 164
 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;
 169
 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);
 174
 175        for (i = 0; i < state->format->planes; ++i)
 176                cfg.mem[i] = sg_dma_address(state->sg_tables[i].sgl)
 177                           + fb->offsets[i];
 178
 179        format = rcar_du_format_info(state->format->fourcc);
 180        cfg.pixelformat = format->v4l2;
 181
 182        vsp1_du_atomic_update(plane->vsp->vsp, crtc->vsp_pipe,
 183                              plane->index, &cfg);
 184}
 185
 186int rcar_du_vsp_map_fb(struct rcar_du_vsp *vsp, struct drm_framebuffer *fb,
 187                       struct sg_table sg_tables[3])
 188{
 189        struct rcar_du_device *rcdu = vsp->dev;
 190        unsigned int i, j;
 191        int ret;
 192
 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];
 196
 197                if (gem->sgt) {
 198                        struct scatterlist *src;
 199                        struct scatterlist *dst;
 200
 201                        /*
 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
 206                         * the VSP.
 207                         */
 208                        ret = sg_alloc_table(sgt, gem->sgt->orig_nents,
 209                                             GFP_KERNEL);
 210                        if (ret)
 211                                goto fail;
 212
 213                        src = gem->sgt->sgl;
 214                        dst = sgt->sgl;
 215                        for (j = 0; j < gem->sgt->orig_nents; ++j) {
 216                                sg_set_page(dst, sg_page(src), src->length,
 217                                            src->offset);
 218                                src = sg_next(src);
 219                                dst = sg_next(dst);
 220                        }
 221                } else {
 222                        ret = dma_get_sgtable(rcdu->dev, sgt, gem->vaddr,
 223                                              gem->paddr, gem->base.size);
 224                        if (ret)
 225                                goto fail;
 226                }
 227
 228                ret = vsp1_du_map_sg(vsp->vsp, sgt);
 229                if (ret) {
 230                        sg_free_table(sgt);
 231                        goto fail;
 232                }
 233        }
 234
 235        return 0;
 236
 237fail:
 238        while (i--) {
 239                struct sg_table *sgt = &sg_tables[i];
 240
 241                vsp1_du_unmap_sg(vsp->vsp, sgt);
 242                sg_free_table(sgt);
 243        }
 244
 245        return ret;
 246}
 247
 248static int rcar_du_vsp_plane_prepare_fb(struct drm_plane *plane,
 249                                        struct drm_plane_state *state)
 250{
 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;
 253        int ret;
 254
 255        /*
 256         * There's no need to prepare (and unprepare) the framebuffer when the
 257         * plane is not visible, as it will not be displayed.
 258         */
 259        if (!state->visible)
 260                return 0;
 261
 262        ret = rcar_du_vsp_map_fb(vsp, state->fb, rstate->sg_tables);
 263        if (ret < 0)
 264                return ret;
 265
 266        return drm_gem_plane_helper_prepare_fb(plane, state);
 267}
 268
 269void rcar_du_vsp_unmap_fb(struct rcar_du_vsp *vsp, struct drm_framebuffer *fb,
 270                          struct sg_table sg_tables[3])
 271{
 272        unsigned int i;
 273
 274        for (i = 0; i < fb->format->num_planes; ++i) {
 275                struct sg_table *sgt = &sg_tables[i];
 276
 277                vsp1_du_unmap_sg(vsp->vsp, sgt);
 278                sg_free_table(sgt);
 279        }
 280}
 281
 282static void rcar_du_vsp_plane_cleanup_fb(struct drm_plane *plane,
 283                                         struct drm_plane_state *state)
 284{
 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;
 287
 288        if (!state->visible)
 289                return;
 290
 291        rcar_du_vsp_unmap_fb(vsp, state->fb, rstate->sg_tables);
 292}
 293
 294static int rcar_du_vsp_plane_atomic_check(struct drm_plane *plane,
 295                                          struct drm_atomic_state *state)
 296{
 297        struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
 298                                                                                 plane);
 299        struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(new_plane_state);
 300
 301        return __rcar_du_plane_atomic_check(plane, new_plane_state,
 302                                            &rstate->format);
 303}
 304
 305static void rcar_du_vsp_plane_atomic_update(struct drm_plane *plane,
 306                                        struct drm_atomic_state *state)
 307{
 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);
 312
 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);
 318}
 319
 320static 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,
 325};
 326
 327static struct drm_plane_state *
 328rcar_du_vsp_plane_atomic_duplicate_state(struct drm_plane *plane)
 329{
 330        struct rcar_du_vsp_plane_state *copy;
 331
 332        if (WARN_ON(!plane->state))
 333                return NULL;
 334
 335        copy = kzalloc(sizeof(*copy), GFP_KERNEL);
 336        if (copy == NULL)
 337                return NULL;
 338
 339        __drm_atomic_helper_plane_duplicate_state(plane, &copy->state);
 340
 341        return &copy->state;
 342}
 343
 344static void rcar_du_vsp_plane_atomic_destroy_state(struct drm_plane *plane,
 345                                                   struct drm_plane_state *state)
 346{
 347        __drm_atomic_helper_plane_destroy_state(state);
 348        kfree(to_rcar_vsp_plane_state(state));
 349}
 350
 351static void rcar_du_vsp_plane_reset(struct drm_plane *plane)
 352{
 353        struct rcar_du_vsp_plane_state *state;
 354
 355        if (plane->state) {
 356                rcar_du_vsp_plane_atomic_destroy_state(plane, plane->state);
 357                plane->state = NULL;
 358        }
 359
 360        state = kzalloc(sizeof(*state), GFP_KERNEL);
 361        if (state == NULL)
 362                return;
 363
 364        __drm_atomic_helper_plane_reset(plane, &state->state);
 365        state->state.zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
 366}
 367
 368static 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,
 375};
 376
 377static void rcar_du_vsp_cleanup(struct drm_device *dev, void *res)
 378{
 379        struct rcar_du_vsp *vsp = res;
 380        unsigned int i;
 381
 382        for (i = 0; i < vsp->num_planes; ++i) {
 383                struct rcar_du_vsp_plane *plane = &vsp->planes[i];
 384
 385                drm_plane_cleanup(&plane->plane);
 386        }
 387
 388        kfree(vsp->planes);
 389
 390        put_device(vsp->vsp);
 391}
 392
 393int rcar_du_vsp_init(struct rcar_du_vsp *vsp, struct device_node *np,
 394                     unsigned int crtcs)
 395{
 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;
 400        unsigned int i;
 401        int ret;
 402
 403        /* Find the VSP device and initialize it. */
 404        pdev = of_find_device_by_node(np);
 405        if (!pdev)
 406                return -ENXIO;
 407
 408        vsp->vsp = &pdev->dev;
 409
 410        ret = drmm_add_action_or_reset(&rcdu->ddev, rcar_du_vsp_cleanup, vsp);
 411        if (ret < 0)
 412                return ret;
 413
 414        ret = vsp1_du_init(vsp->vsp);
 415        if (ret < 0)
 416                return ret;
 417
 418         /*
 419          * The VSP2D (Gen3) has 5 RPFs, but the VSP1D (Gen2) is limited to
 420          * 4 RPFs.
 421          */
 422        num_planes = rcdu->info->gen >= 3 ? 5 : 4;
 423
 424        vsp->planes = kcalloc(num_planes, sizeof(*vsp->planes), GFP_KERNEL);
 425        if (!vsp->planes)
 426                return -ENOMEM;
 427
 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];
 433
 434                plane->vsp = vsp;
 435                plane->index = i;
 436
 437                ret = drm_universal_plane_init(&rcdu->ddev, &plane->plane,
 438                                               crtcs, &rcar_du_vsp_plane_funcs,
 439                                               rcar_du_vsp_formats,
 440                                               ARRAY_SIZE(rcar_du_vsp_formats),
 441                                               NULL, type, NULL);
 442                if (ret < 0)
 443                        return ret;
 444
 445                drm_plane_helper_add(&plane->plane,
 446                                     &rcar_du_vsp_plane_helper_funcs);
 447
 448                if (type == DRM_PLANE_TYPE_PRIMARY) {
 449                        drm_plane_create_zpos_immutable_property(&plane->plane,
 450                                                                 0);
 451                } else {
 452                        drm_plane_create_alpha_property(&plane->plane);
 453                        drm_plane_create_zpos_property(&plane->plane, 1, 1,
 454                                                       num_planes - 1);
 455                }
 456
 457                vsp->num_planes++;
 458        }
 459
 460        return 0;
 461}
 462