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 <drm/drm_crtc.h>
0008 #include <drm/drm_util.h>
0009 #include <drm/drm_fb_helper.h>
0010 #include <drm/drm_file.h>
0011 #include <drm/drm_fourcc.h>
0012 #include <drm/drm_framebuffer.h>
0013 
0014 #include "omap_drv.h"
0015 
0016 MODULE_PARM_DESC(ywrap, "Enable ywrap scrolling (omap44xx and later, default 'y')");
0017 static bool ywrap_enabled = true;
0018 module_param_named(ywrap, ywrap_enabled, bool, 0644);
0019 
0020 /*
0021  * fbdev funcs, to implement legacy fbdev interface on top of drm driver
0022  */
0023 
0024 #define to_omap_fbdev(x) container_of(x, struct omap_fbdev, base)
0025 
0026 struct omap_fbdev {
0027     struct drm_fb_helper base;
0028     struct drm_framebuffer *fb;
0029     struct drm_gem_object *bo;
0030     bool ywrap_enabled;
0031 
0032     /* for deferred dmm roll when getting called in atomic ctx */
0033     struct work_struct work;
0034 };
0035 
0036 static struct drm_fb_helper *get_fb(struct fb_info *fbi);
0037 
0038 static void pan_worker(struct work_struct *work)
0039 {
0040     struct omap_fbdev *fbdev = container_of(work, struct omap_fbdev, work);
0041     struct fb_info *fbi = fbdev->base.fbdev;
0042     int npages;
0043 
0044     /* DMM roll shifts in 4K pages: */
0045     npages = fbi->fix.line_length >> PAGE_SHIFT;
0046     omap_gem_roll(fbdev->bo, fbi->var.yoffset * npages);
0047 }
0048 
0049 static int omap_fbdev_pan_display(struct fb_var_screeninfo *var,
0050         struct fb_info *fbi)
0051 {
0052     struct drm_fb_helper *helper = get_fb(fbi);
0053     struct omap_fbdev *fbdev = to_omap_fbdev(helper);
0054 
0055     if (!helper)
0056         goto fallback;
0057 
0058     if (!fbdev->ywrap_enabled)
0059         goto fallback;
0060 
0061     if (drm_can_sleep()) {
0062         pan_worker(&fbdev->work);
0063     } else {
0064         struct omap_drm_private *priv = helper->dev->dev_private;
0065         queue_work(priv->wq, &fbdev->work);
0066     }
0067 
0068     return 0;
0069 
0070 fallback:
0071     return drm_fb_helper_pan_display(var, fbi);
0072 }
0073 
0074 static const struct fb_ops omap_fb_ops = {
0075     .owner = THIS_MODULE,
0076 
0077     .fb_check_var   = drm_fb_helper_check_var,
0078     .fb_set_par = drm_fb_helper_set_par,
0079     .fb_setcmap = drm_fb_helper_setcmap,
0080     .fb_blank   = drm_fb_helper_blank,
0081     .fb_pan_display = omap_fbdev_pan_display,
0082     .fb_ioctl   = drm_fb_helper_ioctl,
0083 
0084     .fb_read = drm_fb_helper_sys_read,
0085     .fb_write = drm_fb_helper_sys_write,
0086     .fb_fillrect = drm_fb_helper_sys_fillrect,
0087     .fb_copyarea = drm_fb_helper_sys_copyarea,
0088     .fb_imageblit = drm_fb_helper_sys_imageblit,
0089 };
0090 
0091 static int omap_fbdev_create(struct drm_fb_helper *helper,
0092         struct drm_fb_helper_surface_size *sizes)
0093 {
0094     struct omap_fbdev *fbdev = to_omap_fbdev(helper);
0095     struct drm_device *dev = helper->dev;
0096     struct omap_drm_private *priv = dev->dev_private;
0097     struct drm_framebuffer *fb = NULL;
0098     union omap_gem_size gsize;
0099     struct fb_info *fbi = NULL;
0100     struct drm_mode_fb_cmd2 mode_cmd = {0};
0101     dma_addr_t dma_addr;
0102     int ret;
0103 
0104     sizes->surface_bpp = 32;
0105     sizes->surface_depth = 24;
0106 
0107     DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
0108             sizes->surface_height, sizes->surface_bpp,
0109             sizes->fb_width, sizes->fb_height);
0110 
0111     mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
0112             sizes->surface_depth);
0113 
0114     mode_cmd.width = sizes->surface_width;
0115     mode_cmd.height = sizes->surface_height;
0116 
0117     mode_cmd.pitches[0] =
0118             DIV_ROUND_UP(mode_cmd.width * sizes->surface_bpp, 8);
0119 
0120     fbdev->ywrap_enabled = priv->has_dmm && ywrap_enabled;
0121     if (fbdev->ywrap_enabled) {
0122         /* need to align pitch to page size if using DMM scrolling */
0123         mode_cmd.pitches[0] = PAGE_ALIGN(mode_cmd.pitches[0]);
0124     }
0125 
0126     /* allocate backing bo */
0127     gsize = (union omap_gem_size){
0128         .bytes = PAGE_ALIGN(mode_cmd.pitches[0] * mode_cmd.height),
0129     };
0130     DBG("allocating %d bytes for fb %d", gsize.bytes, dev->primary->index);
0131     fbdev->bo = omap_gem_new(dev, gsize, OMAP_BO_SCANOUT | OMAP_BO_WC);
0132     if (!fbdev->bo) {
0133         dev_err(dev->dev, "failed to allocate buffer object\n");
0134         ret = -ENOMEM;
0135         goto fail;
0136     }
0137 
0138     fb = omap_framebuffer_init(dev, &mode_cmd, &fbdev->bo);
0139     if (IS_ERR(fb)) {
0140         dev_err(dev->dev, "failed to allocate fb\n");
0141         /* note: if fb creation failed, we can't rely on fb destroy
0142          * to unref the bo:
0143          */
0144         drm_gem_object_put(fbdev->bo);
0145         ret = PTR_ERR(fb);
0146         goto fail;
0147     }
0148 
0149     /* note: this keeps the bo pinned.. which is perhaps not ideal,
0150      * but is needed as long as we use fb_mmap() to mmap to userspace
0151      * (since this happens using fix.smem_start).  Possibly we could
0152      * implement our own mmap using GEM mmap support to avoid this
0153      * (non-tiled buffer doesn't need to be pinned for fbcon to write
0154      * to it).  Then we just need to be sure that we are able to re-
0155      * pin it in case of an opps.
0156      */
0157     ret = omap_gem_pin(fbdev->bo, &dma_addr);
0158     if (ret) {
0159         dev_err(dev->dev, "could not pin framebuffer\n");
0160         ret = -ENOMEM;
0161         goto fail;
0162     }
0163 
0164     fbi = drm_fb_helper_alloc_fbi(helper);
0165     if (IS_ERR(fbi)) {
0166         dev_err(dev->dev, "failed to allocate fb info\n");
0167         ret = PTR_ERR(fbi);
0168         goto fail;
0169     }
0170 
0171     DBG("fbi=%p, dev=%p", fbi, dev);
0172 
0173     fbdev->fb = fb;
0174     helper->fb = fb;
0175 
0176     fbi->fbops = &omap_fb_ops;
0177 
0178     drm_fb_helper_fill_info(fbi, helper, sizes);
0179 
0180     dev->mode_config.fb_base = dma_addr;
0181 
0182     fbi->screen_buffer = omap_gem_vaddr(fbdev->bo);
0183     fbi->screen_size = fbdev->bo->size;
0184     fbi->fix.smem_start = dma_addr;
0185     fbi->fix.smem_len = fbdev->bo->size;
0186 
0187     /* if we have DMM, then we can use it for scrolling by just
0188      * shuffling pages around in DMM rather than doing sw blit.
0189      */
0190     if (fbdev->ywrap_enabled) {
0191         DRM_INFO("Enabling DMM ywrap scrolling\n");
0192         fbi->flags |= FBINFO_HWACCEL_YWRAP | FBINFO_READS_FAST;
0193         fbi->fix.ywrapstep = 1;
0194     }
0195 
0196 
0197     DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
0198     DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
0199 
0200     return 0;
0201 
0202 fail:
0203 
0204     if (ret) {
0205         if (fb)
0206             drm_framebuffer_remove(fb);
0207     }
0208 
0209     return ret;
0210 }
0211 
0212 static const struct drm_fb_helper_funcs omap_fb_helper_funcs = {
0213     .fb_probe = omap_fbdev_create,
0214 };
0215 
0216 static struct drm_fb_helper *get_fb(struct fb_info *fbi)
0217 {
0218     if (!fbi || strcmp(fbi->fix.id, MODULE_NAME)) {
0219         /* these are not the fb's you're looking for */
0220         return NULL;
0221     }
0222     return fbi->par;
0223 }
0224 
0225 /* initialize fbdev helper */
0226 void omap_fbdev_init(struct drm_device *dev)
0227 {
0228     struct omap_drm_private *priv = dev->dev_private;
0229     struct omap_fbdev *fbdev = NULL;
0230     struct drm_fb_helper *helper;
0231     int ret = 0;
0232 
0233     if (!priv->num_pipes)
0234         return;
0235 
0236     fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
0237     if (!fbdev)
0238         goto fail;
0239 
0240     INIT_WORK(&fbdev->work, pan_worker);
0241 
0242     helper = &fbdev->base;
0243 
0244     drm_fb_helper_prepare(dev, helper, &omap_fb_helper_funcs);
0245 
0246     ret = drm_fb_helper_init(dev, helper);
0247     if (ret)
0248         goto fail;
0249 
0250     ret = drm_fb_helper_initial_config(helper, 32);
0251     if (ret)
0252         goto fini;
0253 
0254     priv->fbdev = helper;
0255 
0256     return;
0257 
0258 fini:
0259     drm_fb_helper_fini(helper);
0260 fail:
0261     kfree(fbdev);
0262 
0263     dev_warn(dev->dev, "omap_fbdev_init failed\n");
0264 }
0265 
0266 void omap_fbdev_fini(struct drm_device *dev)
0267 {
0268     struct omap_drm_private *priv = dev->dev_private;
0269     struct drm_fb_helper *helper = priv->fbdev;
0270     struct omap_fbdev *fbdev;
0271 
0272     DBG();
0273 
0274     if (!helper)
0275         return;
0276 
0277     drm_fb_helper_unregister_fbi(helper);
0278 
0279     drm_fb_helper_fini(helper);
0280 
0281     fbdev = to_omap_fbdev(helper);
0282 
0283     /* unpin the GEM object pinned in omap_fbdev_create() */
0284     if (fbdev->bo)
0285         omap_gem_unpin(fbdev->bo);
0286 
0287     /* this will free the backing object */
0288     if (fbdev->fb)
0289         drm_framebuffer_remove(fbdev->fb);
0290 
0291     kfree(fbdev);
0292 
0293     priv->fbdev = NULL;
0294 }