Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * R-Car Display Unit VSP-Based Compositor
0004  *
0005  * Copyright (C) 2015 Renesas Electronics Corporation
0006  *
0007  * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
0008  */
0009 
0010 #include <drm/drm_atomic.h>
0011 #include <drm/drm_atomic_helper.h>
0012 #include <drm/drm_blend.h>
0013 #include <drm/drm_crtc.h>
0014 #include <drm/drm_fb_cma_helper.h>
0015 #include <drm/drm_fourcc.h>
0016 #include <drm/drm_framebuffer.h>
0017 #include <drm/drm_gem_atomic_helper.h>
0018 #include <drm/drm_gem_cma_helper.h>
0019 #include <drm/drm_managed.h>
0020 #include <drm/drm_plane_helper.h>
0021 #include <drm/drm_vblank.h>
0022 
0023 #include <linux/bitops.h>
0024 #include <linux/dma-mapping.h>
0025 #include <linux/of_platform.h>
0026 #include <linux/scatterlist.h>
0027 #include <linux/slab.h>
0028 #include <linux/videodev2.h>
0029 
0030 #include <media/vsp1.h>
0031 
0032 #include "rcar_du_drv.h"
0033 #include "rcar_du_kms.h"
0034 #include "rcar_du_vsp.h"
0035 #include "rcar_du_writeback.h"
0036 
0037 static void rcar_du_vsp_complete(void *private, unsigned int status, u32 crc)
0038 {
0039     struct rcar_du_crtc *crtc = private;
0040 
0041     if (crtc->vblank_enable)
0042         drm_crtc_handle_vblank(&crtc->crtc);
0043 
0044     if (status & VSP1_DU_STATUS_COMPLETE)
0045         rcar_du_crtc_finish_page_flip(crtc);
0046     if (status & VSP1_DU_STATUS_WRITEBACK)
0047         rcar_du_writeback_complete(crtc);
0048 
0049     drm_crtc_add_crc_entry(&crtc->crtc, false, 0, &crc);
0050 }
0051 
0052 void rcar_du_vsp_enable(struct rcar_du_crtc *crtc)
0053 {
0054     const struct drm_display_mode *mode = &crtc->crtc.state->adjusted_mode;
0055     struct rcar_du_device *rcdu = crtc->dev;
0056     struct vsp1_du_lif_config cfg = {
0057         .width = mode->hdisplay,
0058         .height = mode->vdisplay,
0059         .interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE,
0060         .callback = rcar_du_vsp_complete,
0061         .callback_data = crtc,
0062     };
0063     struct rcar_du_plane_state state = {
0064         .state = {
0065             .alpha = DRM_BLEND_ALPHA_OPAQUE,
0066             .crtc = &crtc->crtc,
0067             .dst.x1 = 0,
0068             .dst.y1 = 0,
0069             .dst.x2 = mode->hdisplay,
0070             .dst.y2 = mode->vdisplay,
0071             .src.x1 = 0,
0072             .src.y1 = 0,
0073             .src.x2 = mode->hdisplay << 16,
0074             .src.y2 = mode->vdisplay << 16,
0075             .zpos = 0,
0076         },
0077         .format = rcar_du_format_info(DRM_FORMAT_ARGB8888),
0078         .source = RCAR_DU_PLANE_VSPD1,
0079         .colorkey = 0,
0080     };
0081 
0082     if (rcdu->info->gen >= 3)
0083         state.hwindex = (crtc->index % 2) ? 2 : 0;
0084     else
0085         state.hwindex = crtc->index % 2;
0086 
0087     __rcar_du_plane_setup(crtc->group, &state);
0088 
0089     vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, &cfg);
0090 }
0091 
0092 void rcar_du_vsp_disable(struct rcar_du_crtc *crtc)
0093 {
0094     vsp1_du_setup_lif(crtc->vsp->vsp, crtc->vsp_pipe, NULL);
0095 }
0096 
0097 void rcar_du_vsp_atomic_begin(struct rcar_du_crtc *crtc)
0098 {
0099     vsp1_du_atomic_begin(crtc->vsp->vsp, crtc->vsp_pipe);
0100 }
0101 
0102 void rcar_du_vsp_atomic_flush(struct rcar_du_crtc *crtc)
0103 {
0104     struct vsp1_du_atomic_pipe_config cfg = { { 0, } };
0105     struct rcar_du_crtc_state *state;
0106 
0107     state = to_rcar_crtc_state(crtc->crtc.state);
0108     cfg.crc = state->crc;
0109 
0110     rcar_du_writeback_setup(crtc, &cfg.writeback);
0111 
0112     vsp1_du_atomic_flush(crtc->vsp->vsp, crtc->vsp_pipe, &cfg);
0113 }
0114 
0115 static const u32 rcar_du_vsp_formats[] = {
0116     DRM_FORMAT_RGB332,
0117     DRM_FORMAT_ARGB4444,
0118     DRM_FORMAT_XRGB4444,
0119     DRM_FORMAT_ARGB1555,
0120     DRM_FORMAT_XRGB1555,
0121     DRM_FORMAT_RGB565,
0122     DRM_FORMAT_BGR888,
0123     DRM_FORMAT_RGB888,
0124     DRM_FORMAT_BGRA8888,
0125     DRM_FORMAT_BGRX8888,
0126     DRM_FORMAT_ARGB8888,
0127     DRM_FORMAT_XRGB8888,
0128     DRM_FORMAT_UYVY,
0129     DRM_FORMAT_YUYV,
0130     DRM_FORMAT_YVYU,
0131     DRM_FORMAT_NV12,
0132     DRM_FORMAT_NV21,
0133     DRM_FORMAT_NV16,
0134     DRM_FORMAT_NV61,
0135     DRM_FORMAT_YUV420,
0136     DRM_FORMAT_YVU420,
0137     DRM_FORMAT_YUV422,
0138     DRM_FORMAT_YVU422,
0139     DRM_FORMAT_YUV444,
0140     DRM_FORMAT_YVU444,
0141 };
0142 
0143 static void rcar_du_vsp_plane_setup(struct rcar_du_vsp_plane *plane)
0144 {
0145     struct rcar_du_vsp_plane_state *state =
0146         to_rcar_vsp_plane_state(plane->plane.state);
0147     struct rcar_du_crtc *crtc = to_rcar_crtc(state->state.crtc);
0148     struct drm_framebuffer *fb = plane->plane.state->fb;
0149     const struct rcar_du_format_info *format;
0150     struct vsp1_du_atomic_config cfg = {
0151         .pixelformat = 0,
0152         .pitch = fb->pitches[0],
0153         .alpha = state->state.alpha >> 8,
0154         .zpos = state->state.zpos,
0155     };
0156     unsigned int i;
0157 
0158     cfg.src.left = state->state.src.x1 >> 16;
0159     cfg.src.top = state->state.src.y1 >> 16;
0160     cfg.src.width = drm_rect_width(&state->state.src) >> 16;
0161     cfg.src.height = drm_rect_height(&state->state.src) >> 16;
0162 
0163     cfg.dst.left = state->state.dst.x1;
0164     cfg.dst.top = state->state.dst.y1;
0165     cfg.dst.width = drm_rect_width(&state->state.dst);
0166     cfg.dst.height = drm_rect_height(&state->state.dst);
0167 
0168     for (i = 0; i < state->format->planes; ++i)
0169         cfg.mem[i] = sg_dma_address(state->sg_tables[i].sgl)
0170                + fb->offsets[i];
0171 
0172     format = rcar_du_format_info(state->format->fourcc);
0173     cfg.pixelformat = format->v4l2;
0174 
0175     vsp1_du_atomic_update(plane->vsp->vsp, crtc->vsp_pipe,
0176                   plane->index, &cfg);
0177 }
0178 
0179 int rcar_du_vsp_map_fb(struct rcar_du_vsp *vsp, struct drm_framebuffer *fb,
0180                struct sg_table sg_tables[3])
0181 {
0182     struct rcar_du_device *rcdu = vsp->dev;
0183     unsigned int i, j;
0184     int ret;
0185 
0186     for (i = 0; i < fb->format->num_planes; ++i) {
0187         struct drm_gem_cma_object *gem = drm_fb_cma_get_gem_obj(fb, i);
0188         struct sg_table *sgt = &sg_tables[i];
0189 
0190         if (gem->sgt) {
0191             struct scatterlist *src;
0192             struct scatterlist *dst;
0193 
0194             /*
0195              * If the GEM buffer has a scatter gather table, it has
0196              * been imported from a dma-buf and has no physical
0197              * address as it might not be physically contiguous.
0198              * Copy the original scatter gather table to map it to
0199              * the VSP.
0200              */
0201             ret = sg_alloc_table(sgt, gem->sgt->orig_nents,
0202                          GFP_KERNEL);
0203             if (ret)
0204                 goto fail;
0205 
0206             src = gem->sgt->sgl;
0207             dst = sgt->sgl;
0208             for (j = 0; j < gem->sgt->orig_nents; ++j) {
0209                 sg_set_page(dst, sg_page(src), src->length,
0210                         src->offset);
0211                 src = sg_next(src);
0212                 dst = sg_next(dst);
0213             }
0214         } else {
0215             ret = dma_get_sgtable(rcdu->dev, sgt, gem->vaddr,
0216                           gem->paddr, gem->base.size);
0217             if (ret)
0218                 goto fail;
0219         }
0220 
0221         ret = vsp1_du_map_sg(vsp->vsp, sgt);
0222         if (ret) {
0223             sg_free_table(sgt);
0224             goto fail;
0225         }
0226     }
0227 
0228     return 0;
0229 
0230 fail:
0231     while (i--) {
0232         struct sg_table *sgt = &sg_tables[i];
0233 
0234         vsp1_du_unmap_sg(vsp->vsp, sgt);
0235         sg_free_table(sgt);
0236     }
0237 
0238     return ret;
0239 }
0240 
0241 static int rcar_du_vsp_plane_prepare_fb(struct drm_plane *plane,
0242                     struct drm_plane_state *state)
0243 {
0244     struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
0245     struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;
0246     int ret;
0247 
0248     /*
0249      * There's no need to prepare (and unprepare) the framebuffer when the
0250      * plane is not visible, as it will not be displayed.
0251      */
0252     if (!state->visible)
0253         return 0;
0254 
0255     ret = rcar_du_vsp_map_fb(vsp, state->fb, rstate->sg_tables);
0256     if (ret < 0)
0257         return ret;
0258 
0259     return drm_gem_plane_helper_prepare_fb(plane, state);
0260 }
0261 
0262 void rcar_du_vsp_unmap_fb(struct rcar_du_vsp *vsp, struct drm_framebuffer *fb,
0263               struct sg_table sg_tables[3])
0264 {
0265     unsigned int i;
0266 
0267     for (i = 0; i < fb->format->num_planes; ++i) {
0268         struct sg_table *sgt = &sg_tables[i];
0269 
0270         vsp1_du_unmap_sg(vsp->vsp, sgt);
0271         sg_free_table(sgt);
0272     }
0273 }
0274 
0275 static void rcar_du_vsp_plane_cleanup_fb(struct drm_plane *plane,
0276                      struct drm_plane_state *state)
0277 {
0278     struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(state);
0279     struct rcar_du_vsp *vsp = to_rcar_vsp_plane(plane)->vsp;
0280 
0281     if (!state->visible)
0282         return;
0283 
0284     rcar_du_vsp_unmap_fb(vsp, state->fb, rstate->sg_tables);
0285 }
0286 
0287 static int rcar_du_vsp_plane_atomic_check(struct drm_plane *plane,
0288                       struct drm_atomic_state *state)
0289 {
0290     struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state,
0291                                          plane);
0292     struct rcar_du_vsp_plane_state *rstate = to_rcar_vsp_plane_state(new_plane_state);
0293 
0294     return __rcar_du_plane_atomic_check(plane, new_plane_state,
0295                         &rstate->format);
0296 }
0297 
0298 static void rcar_du_vsp_plane_atomic_update(struct drm_plane *plane,
0299                     struct drm_atomic_state *state)
0300 {
0301     struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
0302     struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
0303     struct rcar_du_vsp_plane *rplane = to_rcar_vsp_plane(plane);
0304     struct rcar_du_crtc *crtc = to_rcar_crtc(old_state->crtc);
0305 
0306     if (new_state->visible)
0307         rcar_du_vsp_plane_setup(rplane);
0308     else if (old_state->crtc)
0309         vsp1_du_atomic_update(rplane->vsp->vsp, crtc->vsp_pipe,
0310                       rplane->index, NULL);
0311 }
0312 
0313 static const struct drm_plane_helper_funcs rcar_du_vsp_plane_helper_funcs = {
0314     .prepare_fb = rcar_du_vsp_plane_prepare_fb,
0315     .cleanup_fb = rcar_du_vsp_plane_cleanup_fb,
0316     .atomic_check = rcar_du_vsp_plane_atomic_check,
0317     .atomic_update = rcar_du_vsp_plane_atomic_update,
0318 };
0319 
0320 static struct drm_plane_state *
0321 rcar_du_vsp_plane_atomic_duplicate_state(struct drm_plane *plane)
0322 {
0323     struct rcar_du_vsp_plane_state *copy;
0324 
0325     if (WARN_ON(!plane->state))
0326         return NULL;
0327 
0328     copy = kzalloc(sizeof(*copy), GFP_KERNEL);
0329     if (copy == NULL)
0330         return NULL;
0331 
0332     __drm_atomic_helper_plane_duplicate_state(plane, &copy->state);
0333 
0334     return &copy->state;
0335 }
0336 
0337 static void rcar_du_vsp_plane_atomic_destroy_state(struct drm_plane *plane,
0338                            struct drm_plane_state *state)
0339 {
0340     __drm_atomic_helper_plane_destroy_state(state);
0341     kfree(to_rcar_vsp_plane_state(state));
0342 }
0343 
0344 static void rcar_du_vsp_plane_reset(struct drm_plane *plane)
0345 {
0346     struct rcar_du_vsp_plane_state *state;
0347 
0348     if (plane->state) {
0349         rcar_du_vsp_plane_atomic_destroy_state(plane, plane->state);
0350         plane->state = NULL;
0351     }
0352 
0353     state = kzalloc(sizeof(*state), GFP_KERNEL);
0354     if (state == NULL)
0355         return;
0356 
0357     __drm_atomic_helper_plane_reset(plane, &state->state);
0358 }
0359 
0360 static const struct drm_plane_funcs rcar_du_vsp_plane_funcs = {
0361     .update_plane = drm_atomic_helper_update_plane,
0362     .disable_plane = drm_atomic_helper_disable_plane,
0363     .reset = rcar_du_vsp_plane_reset,
0364     .destroy = drm_plane_cleanup,
0365     .atomic_duplicate_state = rcar_du_vsp_plane_atomic_duplicate_state,
0366     .atomic_destroy_state = rcar_du_vsp_plane_atomic_destroy_state,
0367 };
0368 
0369 static void rcar_du_vsp_cleanup(struct drm_device *dev, void *res)
0370 {
0371     struct rcar_du_vsp *vsp = res;
0372     unsigned int i;
0373 
0374     for (i = 0; i < vsp->num_planes; ++i) {
0375         struct rcar_du_vsp_plane *plane = &vsp->planes[i];
0376 
0377         drm_plane_cleanup(&plane->plane);
0378     }
0379 
0380     kfree(vsp->planes);
0381 
0382     put_device(vsp->vsp);
0383 }
0384 
0385 int rcar_du_vsp_init(struct rcar_du_vsp *vsp, struct device_node *np,
0386              unsigned int crtcs)
0387 {
0388     struct rcar_du_device *rcdu = vsp->dev;
0389     struct platform_device *pdev;
0390     unsigned int num_crtcs = hweight32(crtcs);
0391     unsigned int num_planes;
0392     unsigned int i;
0393     int ret;
0394 
0395     /* Find the VSP device and initialize it. */
0396     pdev = of_find_device_by_node(np);
0397     if (!pdev)
0398         return -ENXIO;
0399 
0400     vsp->vsp = &pdev->dev;
0401 
0402     ret = drmm_add_action_or_reset(&rcdu->ddev, rcar_du_vsp_cleanup, vsp);
0403     if (ret < 0)
0404         return ret;
0405 
0406     ret = vsp1_du_init(vsp->vsp);
0407     if (ret < 0)
0408         return ret;
0409 
0410     num_planes = rcdu->info->num_rpf;
0411 
0412     vsp->planes = kcalloc(num_planes, sizeof(*vsp->planes), GFP_KERNEL);
0413     if (!vsp->planes)
0414         return -ENOMEM;
0415 
0416     for (i = 0; i < num_planes; ++i) {
0417         enum drm_plane_type type = i < num_crtcs
0418                      ? DRM_PLANE_TYPE_PRIMARY
0419                      : DRM_PLANE_TYPE_OVERLAY;
0420         struct rcar_du_vsp_plane *plane = &vsp->planes[i];
0421 
0422         plane->vsp = vsp;
0423         plane->index = i;
0424 
0425         ret = drm_universal_plane_init(&rcdu->ddev, &plane->plane,
0426                            crtcs, &rcar_du_vsp_plane_funcs,
0427                            rcar_du_vsp_formats,
0428                            ARRAY_SIZE(rcar_du_vsp_formats),
0429                            NULL, type, NULL);
0430         if (ret < 0)
0431             return ret;
0432 
0433         drm_plane_helper_add(&plane->plane,
0434                      &rcar_du_vsp_plane_helper_funcs);
0435 
0436         drm_plane_create_alpha_property(&plane->plane);
0437         drm_plane_create_zpos_property(&plane->plane, i, 0,
0438                            num_planes - 1);
0439 
0440         vsp->num_planes++;
0441     }
0442 
0443     return 0;
0444 }