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;
 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                ret = dma_get_sgtable(rcdu->dev, sgt, gem->vaddr, gem->paddr,
 198                                      gem->base.size);
 199                if (ret)
 200                        goto fail;
 201
 202                ret = vsp1_du_map_sg(vsp->vsp, sgt);
 203                if (ret) {
 204                        sg_free_table(sgt);
 205                        goto fail;
 206                }
 207        }
 208
 209        return 0;
 210
 211fail:
 212        while (i--) {
 213                struct sg_table *sgt = &sg_tables[i];
 214
 215                vsp1_du_unmap_sg(vsp->vsp, sgt);
 216                sg_free_table(sgt);
 217        }
 218
 219        return ret;
 220}
 221
 222static int rcar_du_vsp_plane_prepare_fb(struct drm_plane *plane,
 223                                        struct drm_plane_state *state)
 224{
 225        struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
 226        struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;
 227        int ret;
 228
 229        /*
 230         * There's no need to prepare (and unprepare) the framebuffer when the
 231         * plane is not visible, as it will not be displayed.
 232         */
 233        if (!state->visible)
 234                return 0;
 235
 236        ret = rcar_du_vsp_map_fb(vsp, state->fb, rstate->sg_tables);
 237        if (ret < 0)
 238                return ret;
 239
 240        return drm_gem_plane_helper_prepare_fb(plane, state);
 241}
 242
 243void rcar_du_vsp_unmap_fb(struct rcar_du_vsp *vsp, struct drm_framebuffer *fb,
 244                          struct sg_table sg_tables[3])
 245{
 246        unsigned int i;
 247
 248        for (i = 0; i < fb->format->num_planes; ++i) {
 249                struct sg_table *sgt = &sg_tables[i];
 250
 251                vsp1_du_unmap_sg(vsp->vsp, sgt);
 252                sg_free_table(sgt);
 253        }
 254}
 255
 256static void rcar_du_vsp_plane_cleanup_fb(struct drm_plane *plane,
 257                                         struct drm_plane_state *state)
 258{
 259        struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
 260        struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;
 261
 262        if (!state->visible)
 263                return;
 264
 265        rcar_du_vsp_unmap_fb(vsp, state->fb, rstate->sg_tables);
 266}
 267
 268static int rcar_du_vsp_plane_atomic_check(struct drm_plane *plane,
 269                                          struct drm_atomic_state *state)
 270{
 271        struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
 272                                                                                 plane);
 273        struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(new_plane_state);
 274
 275        return __rcar_du_plane_atomic_check(plane, new_plane_state,
 276                                            &rstate->format);
 277}
 278
 279static void rcar_du_vsp_plane_atomic_update(struct drm_plane *plane,
 280                                        struct drm_atomic_state *state)
 281{
 282        struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
 283        struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
 284        struct rcar_du_vsp_plane *rplane = to_rcar_vsp_plane(plane);
 285        struct rcar_du_crtc *crtc = to_rcar_crtc(old_state->crtc);
 286
 287        if (new_state->visible)
 288                rcar_du_vsp_plane_setup(rplane);
 289        else if (old_state->crtc)
 290                vsp1_du_atomic_update(rplane->vsp->vsp, crtc->vsp_pipe,
 291                                      rplane->index, NULL);
 292}
 293
 294static const struct drm_plane_helper_funcs rcar_du_vsp_plane_helper_funcs = {
 295        .prepare_fb = rcar_du_vsp_plane_prepare_fb,
 296        .cleanup_fb = rcar_du_vsp_plane_cleanup_fb,
 297        .atomic_check = rcar_du_vsp_plane_atomic_check,
 298        .atomic_update = rcar_du_vsp_plane_atomic_update,
 299};
 300
 301static struct drm_plane_state *
 302rcar_du_vsp_plane_atomic_duplicate_state(struct drm_plane *plane)
 303{
 304        struct rcar_du_vsp_plane_state *copy;
 305
 306        if (WARN_ON(!plane->state))
 307                return NULL;
 308
 309        copy = kzalloc(sizeof(*copy), GFP_KERNEL);
 310        if (copy == NULL)
 311                return NULL;
 312
 313        __drm_atomic_helper_plane_duplicate_state(plane, &copy->state);
 314
 315        return &copy->state;
 316}
 317
 318static void rcar_du_vsp_plane_atomic_destroy_state(struct drm_plane *plane,
 319                                                   struct drm_plane_state *state)
 320{
 321        __drm_atomic_helper_plane_destroy_state(state);
 322        kfree(to_rcar_vsp_plane_state(state));
 323}
 324
 325static void rcar_du_vsp_plane_reset(struct drm_plane *plane)
 326{
 327        struct rcar_du_vsp_plane_state *state;
 328
 329        if (plane->state) {
 330                rcar_du_vsp_plane_atomic_destroy_state(plane, plane->state);
 331                plane->state = NULL;
 332        }
 333
 334        state = kzalloc(sizeof(*state), GFP_KERNEL);
 335        if (state == NULL)
 336                return;
 337
 338        __drm_atomic_helper_plane_reset(plane, &state->state);
 339        state->state.zpos = plane->type == DRM_PLANE_TYPE_PRIMARY ? 0 : 1;
 340}
 341
 342static const struct drm_plane_funcs rcar_du_vsp_plane_funcs = {
 343        .update_plane = drm_atomic_helper_update_plane,
 344        .disable_plane = drm_atomic_helper_disable_plane,
 345        .reset = rcar_du_vsp_plane_reset,
 346        .destroy = drm_plane_cleanup,
 347        .atomic_duplicate_state = rcar_du_vsp_plane_atomic_duplicate_state,
 348        .atomic_destroy_state = rcar_du_vsp_plane_atomic_destroy_state,
 349};
 350
 351static void rcar_du_vsp_cleanup(struct drm_device *dev, void *res)
 352{
 353        struct rcar_du_vsp *vsp = res;
 354        unsigned int i;
 355
 356        for (i = 0; i < vsp->num_planes; ++i) {
 357                struct rcar_du_vsp_plane *plane = &vsp->planes[i];
 358
 359                drm_plane_cleanup(&plane->plane);
 360        }
 361
 362        kfree(vsp->planes);
 363
 364        put_device(vsp->vsp);
 365}
 366
 367int rcar_du_vsp_init(struct rcar_du_vsp *vsp, struct device_node *np,
 368                     unsigned int crtcs)
 369{
 370        struct rcar_du_device *rcdu = vsp->dev;
 371        struct platform_device *pdev;
 372        unsigned int num_crtcs = hweight32(crtcs);
 373        unsigned int num_planes;
 374        unsigned int i;
 375        int ret;
 376
 377        /* Find the VSP device and initialize it. */
 378        pdev = of_find_device_by_node(np);
 379        if (!pdev)
 380                return -ENXIO;
 381
 382        vsp->vsp = &pdev->dev;
 383
 384        ret = drmm_add_action_or_reset(&rcdu->ddev, rcar_du_vsp_cleanup, vsp);
 385        if (ret < 0)
 386                return ret;
 387
 388        ret = vsp1_du_init(vsp->vsp);
 389        if (ret < 0)
 390                return ret;
 391
 392         /*
 393          * The VSP2D (Gen3) has 5 RPFs, but the VSP1D (Gen2) is limited to
 394          * 4 RPFs.
 395          */
 396        num_planes = rcdu->info->gen >= 3 ? 5 : 4;
 397
 398        vsp->planes = kcalloc(num_planes, sizeof(*vsp->planes), GFP_KERNEL);
 399        if (!vsp->planes)
 400                return -ENOMEM;
 401
 402        for (i = 0; i < num_planes; ++i) {
 403                enum drm_plane_type type = i < num_crtcs
 404                                         ? DRM_PLANE_TYPE_PRIMARY
 405                                         : DRM_PLANE_TYPE_OVERLAY;
 406                struct rcar_du_vsp_plane *plane = &vsp->planes[i];
 407
 408                plane->vsp = vsp;
 409                plane->index = i;
 410
 411                ret = drm_universal_plane_init(&rcdu->ddev, &plane->plane,
 412                                               crtcs, &rcar_du_vsp_plane_funcs,
 413                                               rcar_du_vsp_formats,
 414                                               ARRAY_SIZE(rcar_du_vsp_formats),
 415                                               NULL, type, NULL);
 416                if (ret < 0)
 417                        return ret;
 418
 419                drm_plane_helper_add(&plane->plane,
 420                                     &rcar_du_vsp_plane_helper_funcs);
 421
 422                if (type == DRM_PLANE_TYPE_PRIMARY) {
 423                        drm_plane_create_zpos_immutable_property(&plane->plane,
 424                                                                 0);
 425                } else {
 426                        drm_plane_create_alpha_property(&plane->plane);
 427                        drm_plane_create_zpos_property(&plane->plane, 1, 1,
 428                                                       num_planes - 1);
 429                }
 430
 431                vsp->num_planes++;
 432        }
 433
 434        return 0;
 435}
 436