Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
0004  * Author: Rob Clark <rob@ti.com>
0005  */
0006 
0007 #include <linux/dma-mapping.h>
0008 
0009 #include <drm/drm_blend.h>
0010 #include <drm/drm_modeset_helper.h>
0011 #include <drm/drm_fourcc.h>
0012 #include <drm/drm_framebuffer.h>
0013 #include <drm/drm_gem_framebuffer_helper.h>
0014 
0015 #include "omap_dmm_tiler.h"
0016 #include "omap_drv.h"
0017 
0018 /*
0019  * framebuffer funcs
0020  */
0021 
0022 static const u32 formats[] = {
0023     /* 16bpp [A]RGB: */
0024     DRM_FORMAT_RGB565, /* RGB16-565 */
0025     DRM_FORMAT_RGBX4444, /* RGB12x-4444 */
0026     DRM_FORMAT_XRGB4444, /* xRGB12-4444 */
0027     DRM_FORMAT_RGBA4444, /* RGBA12-4444 */
0028     DRM_FORMAT_ARGB4444, /* ARGB16-4444 */
0029     DRM_FORMAT_XRGB1555, /* xRGB15-1555 */
0030     DRM_FORMAT_ARGB1555, /* ARGB16-1555 */
0031     /* 24bpp RGB: */
0032     DRM_FORMAT_RGB888,   /* RGB24-888 */
0033     /* 32bpp [A]RGB: */
0034     DRM_FORMAT_RGBX8888, /* RGBx24-8888 */
0035     DRM_FORMAT_XRGB8888, /* xRGB24-8888 */
0036     DRM_FORMAT_RGBA8888, /* RGBA32-8888 */
0037     DRM_FORMAT_ARGB8888, /* ARGB32-8888 */
0038     /* YUV: */
0039     DRM_FORMAT_NV12,
0040     DRM_FORMAT_YUYV,
0041     DRM_FORMAT_UYVY,
0042 };
0043 
0044 /* per-plane info for the fb: */
0045 struct plane {
0046     dma_addr_t dma_addr;
0047 };
0048 
0049 #define to_omap_framebuffer(x) container_of(x, struct omap_framebuffer, base)
0050 
0051 struct omap_framebuffer {
0052     struct drm_framebuffer base;
0053     int pin_count;
0054     const struct drm_format_info *format;
0055     struct plane planes[2];
0056     /* lock for pinning (pin_count and planes.dma_addr) */
0057     struct mutex lock;
0058 };
0059 
0060 static int omap_framebuffer_dirty(struct drm_framebuffer *fb,
0061                   struct drm_file *file_priv,
0062                   unsigned flags, unsigned color,
0063                   struct drm_clip_rect *clips,
0064                   unsigned num_clips)
0065 {
0066     struct drm_crtc *crtc;
0067 
0068     drm_modeset_lock_all(fb->dev);
0069 
0070     drm_for_each_crtc(crtc, fb->dev)
0071         omap_crtc_flush(crtc);
0072 
0073     drm_modeset_unlock_all(fb->dev);
0074 
0075     return 0;
0076 }
0077 
0078 static const struct drm_framebuffer_funcs omap_framebuffer_funcs = {
0079     .create_handle = drm_gem_fb_create_handle,
0080     .dirty = omap_framebuffer_dirty,
0081     .destroy = drm_gem_fb_destroy,
0082 };
0083 
0084 static u32 get_linear_addr(struct drm_framebuffer *fb,
0085         const struct drm_format_info *format, int n, int x, int y)
0086 {
0087     struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
0088     struct plane *plane = &omap_fb->planes[n];
0089     u32 offset;
0090 
0091     offset = fb->offsets[n]
0092            + (x * format->cpp[n] / (n == 0 ? 1 : format->hsub))
0093            + (y * fb->pitches[n] / (n == 0 ? 1 : format->vsub));
0094 
0095     return plane->dma_addr + offset;
0096 }
0097 
0098 bool omap_framebuffer_supports_rotation(struct drm_framebuffer *fb)
0099 {
0100     return omap_gem_flags(fb->obj[0]) & OMAP_BO_TILED_MASK;
0101 }
0102 
0103 /* Note: DRM rotates counter-clockwise, TILER & DSS rotates clockwise */
0104 static u32 drm_rotation_to_tiler(unsigned int drm_rot)
0105 {
0106     u32 orient;
0107 
0108     switch (drm_rot & DRM_MODE_ROTATE_MASK) {
0109     default:
0110     case DRM_MODE_ROTATE_0:
0111         orient = 0;
0112         break;
0113     case DRM_MODE_ROTATE_90:
0114         orient = MASK_XY_FLIP | MASK_X_INVERT;
0115         break;
0116     case DRM_MODE_ROTATE_180:
0117         orient = MASK_X_INVERT | MASK_Y_INVERT;
0118         break;
0119     case DRM_MODE_ROTATE_270:
0120         orient = MASK_XY_FLIP | MASK_Y_INVERT;
0121         break;
0122     }
0123 
0124     if (drm_rot & DRM_MODE_REFLECT_X)
0125         orient ^= MASK_X_INVERT;
0126 
0127     if (drm_rot & DRM_MODE_REFLECT_Y)
0128         orient ^= MASK_Y_INVERT;
0129 
0130     return orient;
0131 }
0132 
0133 /* update ovl info for scanout, handles cases of multi-planar fb's, etc.
0134  */
0135 void omap_framebuffer_update_scanout(struct drm_framebuffer *fb,
0136         struct drm_plane_state *state,
0137         struct omap_overlay_info *info,
0138         struct omap_overlay_info *r_info)
0139 {
0140     struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
0141     const struct drm_format_info *format = omap_fb->format;
0142     u32 x, y, orient = 0;
0143 
0144     info->fourcc = fb->format->format;
0145 
0146     info->pos_x      = state->crtc_x;
0147     info->pos_y      = state->crtc_y;
0148     info->out_width  = state->crtc_w;
0149     info->out_height = state->crtc_h;
0150     info->width      = state->src_w >> 16;
0151     info->height     = state->src_h >> 16;
0152 
0153     /* DSS driver wants the w & h in rotated orientation */
0154     if (drm_rotation_90_or_270(state->rotation))
0155         swap(info->width, info->height);
0156 
0157     x = state->src_x >> 16;
0158     y = state->src_y >> 16;
0159 
0160     if (omap_gem_flags(fb->obj[0]) & OMAP_BO_TILED_MASK) {
0161         u32 w = state->src_w >> 16;
0162         u32 h = state->src_h >> 16;
0163 
0164         orient = drm_rotation_to_tiler(state->rotation);
0165 
0166         /*
0167          * omap_gem_rotated_paddr() wants the x & y in tiler units.
0168          * Usually tiler unit size is the same as the pixel size, except
0169          * for YUV422 formats, for which the tiler unit size is 32 bits
0170          * and pixel size is 16 bits.
0171          */
0172         if (fb->format->format == DRM_FORMAT_UYVY ||
0173                 fb->format->format == DRM_FORMAT_YUYV) {
0174             x /= 2;
0175             w /= 2;
0176         }
0177 
0178         /* adjust x,y offset for invert: */
0179         if (orient & MASK_Y_INVERT)
0180             y += h - 1;
0181         if (orient & MASK_X_INVERT)
0182             x += w - 1;
0183 
0184         /* Note: x and y are in TILER units, not pixels */
0185         omap_gem_rotated_dma_addr(fb->obj[0], orient, x, y,
0186                       &info->paddr);
0187         info->rotation_type = OMAP_DSS_ROT_TILER;
0188         info->rotation = state->rotation ?: DRM_MODE_ROTATE_0;
0189         /* Note: stride in TILER units, not pixels */
0190         info->screen_width  = omap_gem_tiled_stride(fb->obj[0], orient);
0191     } else {
0192         switch (state->rotation & DRM_MODE_ROTATE_MASK) {
0193         case 0:
0194         case DRM_MODE_ROTATE_0:
0195             /* OK */
0196             break;
0197 
0198         default:
0199             dev_warn(fb->dev->dev,
0200                 "rotation '%d' ignored for non-tiled fb\n",
0201                 state->rotation);
0202             break;
0203         }
0204 
0205         info->paddr         = get_linear_addr(fb, format, 0, x, y);
0206         info->rotation_type = OMAP_DSS_ROT_NONE;
0207         info->rotation      = DRM_MODE_ROTATE_0;
0208         info->screen_width  = fb->pitches[0];
0209     }
0210 
0211     /* convert to pixels: */
0212     info->screen_width /= format->cpp[0];
0213 
0214     if (fb->format->format == DRM_FORMAT_NV12) {
0215         if (info->rotation_type == OMAP_DSS_ROT_TILER) {
0216             WARN_ON(!(omap_gem_flags(fb->obj[1]) & OMAP_BO_TILED_MASK));
0217             omap_gem_rotated_dma_addr(fb->obj[1], orient, x/2, y/2,
0218                           &info->p_uv_addr);
0219         } else {
0220             info->p_uv_addr = get_linear_addr(fb, format, 1, x, y);
0221         }
0222     } else {
0223         info->p_uv_addr = 0;
0224     }
0225 
0226     if (r_info) {
0227         info->width /= 2;
0228         info->out_width /= 2;
0229 
0230         *r_info = *info;
0231 
0232         if (fb->format->is_yuv) {
0233             if (info->width & 1) {
0234                 info->width++;
0235                 r_info->width--;
0236             }
0237 
0238             if (info->out_width & 1) {
0239                 info->out_width++;
0240                 r_info->out_width--;
0241             }
0242         }
0243 
0244         r_info->pos_x = info->pos_x + info->out_width;
0245 
0246         r_info->paddr = get_linear_addr(fb, format, 0,
0247                         x + info->width, y);
0248         if (fb->format->format == DRM_FORMAT_NV12) {
0249             r_info->p_uv_addr =
0250                 get_linear_addr(fb, format, 1,
0251                         x + info->width, y);
0252         }
0253     }
0254 }
0255 
0256 /* pin, prepare for scanout: */
0257 int omap_framebuffer_pin(struct drm_framebuffer *fb)
0258 {
0259     struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
0260     int ret, i, n = fb->format->num_planes;
0261 
0262     mutex_lock(&omap_fb->lock);
0263 
0264     if (omap_fb->pin_count > 0) {
0265         omap_fb->pin_count++;
0266         mutex_unlock(&omap_fb->lock);
0267         return 0;
0268     }
0269 
0270     for (i = 0; i < n; i++) {
0271         struct plane *plane = &omap_fb->planes[i];
0272         ret = omap_gem_pin(fb->obj[i], &plane->dma_addr);
0273         if (ret)
0274             goto fail;
0275         omap_gem_dma_sync_buffer(fb->obj[i], DMA_TO_DEVICE);
0276     }
0277 
0278     omap_fb->pin_count++;
0279 
0280     mutex_unlock(&omap_fb->lock);
0281 
0282     return 0;
0283 
0284 fail:
0285     for (i--; i >= 0; i--) {
0286         struct plane *plane = &omap_fb->planes[i];
0287         omap_gem_unpin(fb->obj[i]);
0288         plane->dma_addr = 0;
0289     }
0290 
0291     mutex_unlock(&omap_fb->lock);
0292 
0293     return ret;
0294 }
0295 
0296 /* unpin, no longer being scanned out: */
0297 void omap_framebuffer_unpin(struct drm_framebuffer *fb)
0298 {
0299     struct omap_framebuffer *omap_fb = to_omap_framebuffer(fb);
0300     int i, n = fb->format->num_planes;
0301 
0302     mutex_lock(&omap_fb->lock);
0303 
0304     omap_fb->pin_count--;
0305 
0306     if (omap_fb->pin_count > 0) {
0307         mutex_unlock(&omap_fb->lock);
0308         return;
0309     }
0310 
0311     for (i = 0; i < n; i++) {
0312         struct plane *plane = &omap_fb->planes[i];
0313         omap_gem_unpin(fb->obj[i]);
0314         plane->dma_addr = 0;
0315     }
0316 
0317     mutex_unlock(&omap_fb->lock);
0318 }
0319 
0320 #ifdef CONFIG_DEBUG_FS
0321 void omap_framebuffer_describe(struct drm_framebuffer *fb, struct seq_file *m)
0322 {
0323     int i, n = fb->format->num_planes;
0324 
0325     seq_printf(m, "fb: %dx%d@%4.4s\n", fb->width, fb->height,
0326             (char *)&fb->format->format);
0327 
0328     for (i = 0; i < n; i++) {
0329         seq_printf(m, "   %d: offset=%d pitch=%d, obj: ",
0330                 i, fb->offsets[n], fb->pitches[i]);
0331         omap_gem_describe(fb->obj[i], m);
0332     }
0333 }
0334 #endif
0335 
0336 struct drm_framebuffer *omap_framebuffer_create(struct drm_device *dev,
0337         struct drm_file *file, const struct drm_mode_fb_cmd2 *mode_cmd)
0338 {
0339     const struct drm_format_info *info = drm_get_format_info(dev,
0340                                  mode_cmd);
0341     unsigned int num_planes = info->num_planes;
0342     struct drm_gem_object *bos[4];
0343     struct drm_framebuffer *fb;
0344     int i;
0345 
0346     for (i = 0; i < num_planes; i++) {
0347         bos[i] = drm_gem_object_lookup(file, mode_cmd->handles[i]);
0348         if (!bos[i]) {
0349             fb = ERR_PTR(-ENOENT);
0350             goto error;
0351         }
0352     }
0353 
0354     fb = omap_framebuffer_init(dev, mode_cmd, bos);
0355     if (IS_ERR(fb))
0356         goto error;
0357 
0358     return fb;
0359 
0360 error:
0361     while (--i >= 0)
0362         drm_gem_object_put(bos[i]);
0363 
0364     return fb;
0365 }
0366 
0367 struct drm_framebuffer *omap_framebuffer_init(struct drm_device *dev,
0368         const struct drm_mode_fb_cmd2 *mode_cmd, struct drm_gem_object **bos)
0369 {
0370     const struct drm_format_info *format = NULL;
0371     struct omap_framebuffer *omap_fb = NULL;
0372     struct drm_framebuffer *fb = NULL;
0373     unsigned int pitch = mode_cmd->pitches[0];
0374     int ret, i;
0375 
0376     DBG("create framebuffer: dev=%p, mode_cmd=%p (%dx%d@%4.4s)",
0377             dev, mode_cmd, mode_cmd->width, mode_cmd->height,
0378             (char *)&mode_cmd->pixel_format);
0379 
0380     format = drm_get_format_info(dev, mode_cmd);
0381 
0382     for (i = 0; i < ARRAY_SIZE(formats); i++) {
0383         if (formats[i] == mode_cmd->pixel_format)
0384             break;
0385     }
0386 
0387     if (!format || i == ARRAY_SIZE(formats)) {
0388         dev_dbg(dev->dev, "unsupported pixel format: %4.4s\n",
0389             (char *)&mode_cmd->pixel_format);
0390         ret = -EINVAL;
0391         goto fail;
0392     }
0393 
0394     omap_fb = kzalloc(sizeof(*omap_fb), GFP_KERNEL);
0395     if (!omap_fb) {
0396         ret = -ENOMEM;
0397         goto fail;
0398     }
0399 
0400     fb = &omap_fb->base;
0401     omap_fb->format = format;
0402     mutex_init(&omap_fb->lock);
0403 
0404     /*
0405      * The code below assumes that no format use more than two planes, and
0406      * that the two planes of multiplane formats need the same number of
0407      * bytes per pixel.
0408      */
0409     if (format->num_planes == 2 && pitch != mode_cmd->pitches[1]) {
0410         dev_dbg(dev->dev, "pitches differ between planes 0 and 1\n");
0411         ret = -EINVAL;
0412         goto fail;
0413     }
0414 
0415     if (pitch % format->cpp[0]) {
0416         dev_dbg(dev->dev,
0417             "buffer pitch (%u bytes) is not a multiple of pixel size (%u bytes)\n",
0418             pitch, format->cpp[0]);
0419         ret = -EINVAL;
0420         goto fail;
0421     }
0422 
0423     for (i = 0; i < format->num_planes; i++) {
0424         struct plane *plane = &omap_fb->planes[i];
0425         unsigned int vsub = i == 0 ? 1 : format->vsub;
0426         unsigned int size;
0427 
0428         size = pitch * mode_cmd->height / vsub;
0429 
0430         if (size > omap_gem_mmap_size(bos[i]) - mode_cmd->offsets[i]) {
0431             dev_dbg(dev->dev,
0432                 "provided buffer object is too small! %zu < %d\n",
0433                 bos[i]->size - mode_cmd->offsets[i], size);
0434             ret = -EINVAL;
0435             goto fail;
0436         }
0437 
0438         fb->obj[i]    = bos[i];
0439         plane->dma_addr  = 0;
0440     }
0441 
0442     drm_helper_mode_fill_fb_struct(dev, fb, mode_cmd);
0443 
0444     ret = drm_framebuffer_init(dev, fb, &omap_framebuffer_funcs);
0445     if (ret) {
0446         dev_err(dev->dev, "framebuffer init failed: %d\n", ret);
0447         goto fail;
0448     }
0449 
0450     DBG("create: FB ID: %d (%p)", fb->base.id, fb);
0451 
0452     return fb;
0453 
0454 fail:
0455     kfree(omap_fb);
0456 
0457     return ERR_PTR(ret);
0458 }