Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 
0003 #include <linux/iosys-map.h>
0004 #include <linux/module.h>
0005 
0006 #include <drm/drm_debugfs.h>
0007 #include <drm/drm_device.h>
0008 #include <drm/drm_drv.h>
0009 #include <drm/drm_file.h>
0010 #include <drm/drm_framebuffer.h>
0011 #include <drm/drm_gem_atomic_helper.h>
0012 #include <drm/drm_gem_framebuffer_helper.h>
0013 #include <drm/drm_gem_ttm_helper.h>
0014 #include <drm/drm_gem_vram_helper.h>
0015 #include <drm/drm_managed.h>
0016 #include <drm/drm_mode.h>
0017 #include <drm/drm_plane.h>
0018 #include <drm/drm_prime.h>
0019 #include <drm/drm_simple_kms_helper.h>
0020 
0021 #include <drm/ttm/ttm_range_manager.h>
0022 
0023 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs;
0024 
0025 /**
0026  * DOC: overview
0027  *
0028  * This library provides &struct drm_gem_vram_object (GEM VRAM), a GEM
0029  * buffer object that is backed by video RAM (VRAM). It can be used for
0030  * framebuffer devices with dedicated memory.
0031  *
0032  * The data structure &struct drm_vram_mm and its helpers implement a memory
0033  * manager for simple framebuffer devices with dedicated video memory. GEM
0034  * VRAM buffer objects are either placed in the video memory or remain evicted
0035  * to system memory.
0036  *
0037  * With the GEM interface userspace applications create, manage and destroy
0038  * graphics buffers, such as an on-screen framebuffer. GEM does not provide
0039  * an implementation of these interfaces. It's up to the DRM driver to
0040  * provide an implementation that suits the hardware. If the hardware device
0041  * contains dedicated video memory, the DRM driver can use the VRAM helper
0042  * library. Each active buffer object is stored in video RAM. Active
0043  * buffer are used for drawing the current frame, typically something like
0044  * the frame's scanout buffer or the cursor image. If there's no more space
0045  * left in VRAM, inactive GEM objects can be moved to system memory.
0046  *
0047  * To initialize the VRAM helper library call drmm_vram_helper_alloc_mm().
0048  * The function allocates and initializes an instance of &struct drm_vram_mm
0049  * in &struct drm_device.vram_mm . Use &DRM_GEM_VRAM_DRIVER to initialize
0050  * &struct drm_driver and  &DRM_VRAM_MM_FILE_OPERATIONS to initialize
0051  * &struct file_operations; as illustrated below.
0052  *
0053  * .. code-block:: c
0054  *
0055  *  struct file_operations fops ={
0056  *      .owner = THIS_MODULE,
0057  *      DRM_VRAM_MM_FILE_OPERATION
0058  *  };
0059  *  struct drm_driver drv = {
0060  *      .driver_feature = DRM_ ... ,
0061  *      .fops = &fops,
0062  *      DRM_GEM_VRAM_DRIVER
0063  *  };
0064  *
0065  *  int init_drm_driver()
0066  *  {
0067  *      struct drm_device *dev;
0068  *      uint64_t vram_base;
0069  *      unsigned long vram_size;
0070  *      int ret;
0071  *
0072  *      // setup device, vram base and size
0073  *      // ...
0074  *
0075  *      ret = drmm_vram_helper_alloc_mm(dev, vram_base, vram_size);
0076  *      if (ret)
0077  *          return ret;
0078  *      return 0;
0079  *  }
0080  *
0081  * This creates an instance of &struct drm_vram_mm, exports DRM userspace
0082  * interfaces for GEM buffer management and initializes file operations to
0083  * allow for accessing created GEM buffers. With this setup, the DRM driver
0084  * manages an area of video RAM with VRAM MM and provides GEM VRAM objects
0085  * to userspace.
0086  *
0087  * You don't have to clean up the instance of VRAM MM.
0088  * drmm_vram_helper_alloc_mm() is a managed interface that installs a
0089  * clean-up handler to run during the DRM device's release.
0090  *
0091  * For drawing or scanout operations, rsp. buffer objects have to be pinned
0092  * in video RAM. Call drm_gem_vram_pin() with &DRM_GEM_VRAM_PL_FLAG_VRAM or
0093  * &DRM_GEM_VRAM_PL_FLAG_SYSTEM to pin a buffer object in video RAM or system
0094  * memory. Call drm_gem_vram_unpin() to release the pinned object afterwards.
0095  *
0096  * A buffer object that is pinned in video RAM has a fixed address within that
0097  * memory region. Call drm_gem_vram_offset() to retrieve this value. Typically
0098  * it's used to program the hardware's scanout engine for framebuffers, set
0099  * the cursor overlay's image for a mouse cursor, or use it as input to the
0100  * hardware's drawing engine.
0101  *
0102  * To access a buffer object's memory from the DRM driver, call
0103  * drm_gem_vram_vmap(). It maps the buffer into kernel address
0104  * space and returns the memory address. Use drm_gem_vram_vunmap() to
0105  * release the mapping.
0106  */
0107 
0108 /*
0109  * Buffer-objects helpers
0110  */
0111 
0112 static void drm_gem_vram_cleanup(struct drm_gem_vram_object *gbo)
0113 {
0114     /* We got here via ttm_bo_put(), which means that the
0115      * TTM buffer object in 'bo' has already been cleaned
0116      * up; only release the GEM object.
0117      */
0118 
0119     WARN_ON(gbo->vmap_use_count);
0120     WARN_ON(iosys_map_is_set(&gbo->map));
0121 
0122     drm_gem_object_release(&gbo->bo.base);
0123 }
0124 
0125 static void drm_gem_vram_destroy(struct drm_gem_vram_object *gbo)
0126 {
0127     drm_gem_vram_cleanup(gbo);
0128     kfree(gbo);
0129 }
0130 
0131 static void ttm_buffer_object_destroy(struct ttm_buffer_object *bo)
0132 {
0133     struct drm_gem_vram_object *gbo = drm_gem_vram_of_bo(bo);
0134 
0135     drm_gem_vram_destroy(gbo);
0136 }
0137 
0138 static void drm_gem_vram_placement(struct drm_gem_vram_object *gbo,
0139                    unsigned long pl_flag)
0140 {
0141     u32 invariant_flags = 0;
0142     unsigned int i;
0143     unsigned int c = 0;
0144 
0145     if (pl_flag & DRM_GEM_VRAM_PL_FLAG_TOPDOWN)
0146         invariant_flags = TTM_PL_FLAG_TOPDOWN;
0147 
0148     gbo->placement.placement = gbo->placements;
0149     gbo->placement.busy_placement = gbo->placements;
0150 
0151     if (pl_flag & DRM_GEM_VRAM_PL_FLAG_VRAM) {
0152         gbo->placements[c].mem_type = TTM_PL_VRAM;
0153         gbo->placements[c++].flags = invariant_flags;
0154     }
0155 
0156     if (pl_flag & DRM_GEM_VRAM_PL_FLAG_SYSTEM || !c) {
0157         gbo->placements[c].mem_type = TTM_PL_SYSTEM;
0158         gbo->placements[c++].flags = invariant_flags;
0159     }
0160 
0161     gbo->placement.num_placement = c;
0162     gbo->placement.num_busy_placement = c;
0163 
0164     for (i = 0; i < c; ++i) {
0165         gbo->placements[i].fpfn = 0;
0166         gbo->placements[i].lpfn = 0;
0167     }
0168 }
0169 
0170 /**
0171  * drm_gem_vram_create() - Creates a VRAM-backed GEM object
0172  * @dev:        the DRM device
0173  * @size:       the buffer size in bytes
0174  * @pg_align:       the buffer's alignment in multiples of the page size
0175  *
0176  * GEM objects are allocated by calling struct drm_driver.gem_create_object,
0177  * if set. Otherwise kzalloc() will be used. Drivers can set their own GEM
0178  * object functions in struct drm_driver.gem_create_object. If no functions
0179  * are set, the new GEM object will use the default functions from GEM VRAM
0180  * helpers.
0181  *
0182  * Returns:
0183  * A new instance of &struct drm_gem_vram_object on success, or
0184  * an ERR_PTR()-encoded error code otherwise.
0185  */
0186 struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
0187                         size_t size,
0188                         unsigned long pg_align)
0189 {
0190     struct drm_gem_vram_object *gbo;
0191     struct drm_gem_object *gem;
0192     struct drm_vram_mm *vmm = dev->vram_mm;
0193     struct ttm_device *bdev;
0194     int ret;
0195 
0196     if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
0197         return ERR_PTR(-EINVAL);
0198 
0199     if (dev->driver->gem_create_object) {
0200         gem = dev->driver->gem_create_object(dev, size);
0201         if (IS_ERR(gem))
0202             return ERR_CAST(gem);
0203         gbo = drm_gem_vram_of_gem(gem);
0204     } else {
0205         gbo = kzalloc(sizeof(*gbo), GFP_KERNEL);
0206         if (!gbo)
0207             return ERR_PTR(-ENOMEM);
0208         gem = &gbo->bo.base;
0209     }
0210 
0211     if (!gem->funcs)
0212         gem->funcs = &drm_gem_vram_object_funcs;
0213 
0214     ret = drm_gem_object_init(dev, gem, size);
0215     if (ret) {
0216         kfree(gbo);
0217         return ERR_PTR(ret);
0218     }
0219 
0220     bdev = &vmm->bdev;
0221 
0222     gbo->bo.bdev = bdev;
0223     drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
0224 
0225     /*
0226      * A failing ttm_bo_init will call ttm_buffer_object_destroy
0227      * to release gbo->bo.base and kfree gbo.
0228      */
0229     ret = ttm_bo_init(bdev, &gbo->bo, size, ttm_bo_type_device,
0230               &gbo->placement, pg_align, false, NULL, NULL,
0231               ttm_buffer_object_destroy);
0232     if (ret)
0233         return ERR_PTR(ret);
0234 
0235     return gbo;
0236 }
0237 EXPORT_SYMBOL(drm_gem_vram_create);
0238 
0239 /**
0240  * drm_gem_vram_put() - Releases a reference to a VRAM-backed GEM object
0241  * @gbo:    the GEM VRAM object
0242  *
0243  * See ttm_bo_put() for more information.
0244  */
0245 void drm_gem_vram_put(struct drm_gem_vram_object *gbo)
0246 {
0247     ttm_bo_put(&gbo->bo);
0248 }
0249 EXPORT_SYMBOL(drm_gem_vram_put);
0250 
0251 static u64 drm_gem_vram_pg_offset(struct drm_gem_vram_object *gbo)
0252 {
0253     /* Keep TTM behavior for now, remove when drivers are audited */
0254     if (WARN_ON_ONCE(!gbo->bo.resource ||
0255              gbo->bo.resource->mem_type == TTM_PL_SYSTEM))
0256         return 0;
0257 
0258     return gbo->bo.resource->start;
0259 }
0260 
0261 /**
0262  * drm_gem_vram_offset() - \
0263     Returns a GEM VRAM object's offset in video memory
0264  * @gbo:    the GEM VRAM object
0265  *
0266  * This function returns the buffer object's offset in the device's video
0267  * memory. The buffer object has to be pinned to %TTM_PL_VRAM.
0268  *
0269  * Returns:
0270  * The buffer object's offset in video memory on success, or
0271  * a negative errno code otherwise.
0272  */
0273 s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo)
0274 {
0275     if (WARN_ON_ONCE(!gbo->bo.pin_count))
0276         return (s64)-ENODEV;
0277     return drm_gem_vram_pg_offset(gbo) << PAGE_SHIFT;
0278 }
0279 EXPORT_SYMBOL(drm_gem_vram_offset);
0280 
0281 static int drm_gem_vram_pin_locked(struct drm_gem_vram_object *gbo,
0282                    unsigned long pl_flag)
0283 {
0284     struct ttm_operation_ctx ctx = { false, false };
0285     int ret;
0286 
0287     if (gbo->bo.pin_count)
0288         goto out;
0289 
0290     if (pl_flag)
0291         drm_gem_vram_placement(gbo, pl_flag);
0292 
0293     ret = ttm_bo_validate(&gbo->bo, &gbo->placement, &ctx);
0294     if (ret < 0)
0295         return ret;
0296 
0297 out:
0298     ttm_bo_pin(&gbo->bo);
0299 
0300     return 0;
0301 }
0302 
0303 /**
0304  * drm_gem_vram_pin() - Pins a GEM VRAM object in a region.
0305  * @gbo:    the GEM VRAM object
0306  * @pl_flag:    a bitmask of possible memory regions
0307  *
0308  * Pinning a buffer object ensures that it is not evicted from
0309  * a memory region. A pinned buffer object has to be unpinned before
0310  * it can be pinned to another region. If the pl_flag argument is 0,
0311  * the buffer is pinned at its current location (video RAM or system
0312  * memory).
0313  *
0314  * Small buffer objects, such as cursor images, can lead to memory
0315  * fragmentation if they are pinned in the middle of video RAM. This
0316  * is especially a problem on devices with only a small amount of
0317  * video RAM. Fragmentation can prevent the primary framebuffer from
0318  * fitting in, even though there's enough memory overall. The modifier
0319  * DRM_GEM_VRAM_PL_FLAG_TOPDOWN marks the buffer object to be pinned
0320  * at the high end of the memory region to avoid fragmentation.
0321  *
0322  * Returns:
0323  * 0 on success, or
0324  * a negative error code otherwise.
0325  */
0326 int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag)
0327 {
0328     int ret;
0329 
0330     ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
0331     if (ret)
0332         return ret;
0333     ret = drm_gem_vram_pin_locked(gbo, pl_flag);
0334     ttm_bo_unreserve(&gbo->bo);
0335 
0336     return ret;
0337 }
0338 EXPORT_SYMBOL(drm_gem_vram_pin);
0339 
0340 static void drm_gem_vram_unpin_locked(struct drm_gem_vram_object *gbo)
0341 {
0342     ttm_bo_unpin(&gbo->bo);
0343 }
0344 
0345 /**
0346  * drm_gem_vram_unpin() - Unpins a GEM VRAM object
0347  * @gbo:    the GEM VRAM object
0348  *
0349  * Returns:
0350  * 0 on success, or
0351  * a negative error code otherwise.
0352  */
0353 int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo)
0354 {
0355     int ret;
0356 
0357     ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
0358     if (ret)
0359         return ret;
0360 
0361     drm_gem_vram_unpin_locked(gbo);
0362     ttm_bo_unreserve(&gbo->bo);
0363 
0364     return 0;
0365 }
0366 EXPORT_SYMBOL(drm_gem_vram_unpin);
0367 
0368 static int drm_gem_vram_kmap_locked(struct drm_gem_vram_object *gbo,
0369                     struct iosys_map *map)
0370 {
0371     int ret;
0372 
0373     if (gbo->vmap_use_count > 0)
0374         goto out;
0375 
0376     /*
0377      * VRAM helpers unmap the BO only on demand. So the previous
0378      * page mapping might still be around. Only vmap if the there's
0379      * no mapping present.
0380      */
0381     if (iosys_map_is_null(&gbo->map)) {
0382         ret = ttm_bo_vmap(&gbo->bo, &gbo->map);
0383         if (ret)
0384             return ret;
0385     }
0386 
0387 out:
0388     ++gbo->vmap_use_count;
0389     *map = gbo->map;
0390 
0391     return 0;
0392 }
0393 
0394 static void drm_gem_vram_kunmap_locked(struct drm_gem_vram_object *gbo,
0395                        struct iosys_map *map)
0396 {
0397     struct drm_device *dev = gbo->bo.base.dev;
0398 
0399     if (drm_WARN_ON_ONCE(dev, !gbo->vmap_use_count))
0400         return;
0401 
0402     if (drm_WARN_ON_ONCE(dev, !iosys_map_is_equal(&gbo->map, map)))
0403         return; /* BUG: map not mapped from this BO */
0404 
0405     if (--gbo->vmap_use_count > 0)
0406         return;
0407 
0408     /*
0409      * Permanently mapping and unmapping buffers adds overhead from
0410      * updating the page tables and creates debugging output. Therefore,
0411      * we delay the actual unmap operation until the BO gets evicted
0412      * from memory. See drm_gem_vram_bo_driver_move_notify().
0413      */
0414 }
0415 
0416 /**
0417  * drm_gem_vram_vmap() - Pins and maps a GEM VRAM object into kernel address
0418  *                       space
0419  * @gbo: The GEM VRAM object to map
0420  * @map: Returns the kernel virtual address of the VRAM GEM object's backing
0421  *       store.
0422  *
0423  * The vmap function pins a GEM VRAM object to its current location, either
0424  * system or video memory, and maps its buffer into kernel address space.
0425  * As pinned object cannot be relocated, you should avoid pinning objects
0426  * permanently. Call drm_gem_vram_vunmap() with the returned address to
0427  * unmap and unpin the GEM VRAM object.
0428  *
0429  * Returns:
0430  * 0 on success, or a negative error code otherwise.
0431  */
0432 int drm_gem_vram_vmap(struct drm_gem_vram_object *gbo, struct iosys_map *map)
0433 {
0434     int ret;
0435 
0436     ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
0437     if (ret)
0438         return ret;
0439 
0440     ret = drm_gem_vram_pin_locked(gbo, 0);
0441     if (ret)
0442         goto err_ttm_bo_unreserve;
0443     ret = drm_gem_vram_kmap_locked(gbo, map);
0444     if (ret)
0445         goto err_drm_gem_vram_unpin_locked;
0446 
0447     ttm_bo_unreserve(&gbo->bo);
0448 
0449     return 0;
0450 
0451 err_drm_gem_vram_unpin_locked:
0452     drm_gem_vram_unpin_locked(gbo);
0453 err_ttm_bo_unreserve:
0454     ttm_bo_unreserve(&gbo->bo);
0455     return ret;
0456 }
0457 EXPORT_SYMBOL(drm_gem_vram_vmap);
0458 
0459 /**
0460  * drm_gem_vram_vunmap() - Unmaps and unpins a GEM VRAM object
0461  * @gbo: The GEM VRAM object to unmap
0462  * @map: Kernel virtual address where the VRAM GEM object was mapped
0463  *
0464  * A call to drm_gem_vram_vunmap() unmaps and unpins a GEM VRAM buffer. See
0465  * the documentation for drm_gem_vram_vmap() for more information.
0466  */
0467 void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo,
0468              struct iosys_map *map)
0469 {
0470     int ret;
0471 
0472     ret = ttm_bo_reserve(&gbo->bo, false, false, NULL);
0473     if (WARN_ONCE(ret, "ttm_bo_reserve_failed(): ret=%d\n", ret))
0474         return;
0475 
0476     drm_gem_vram_kunmap_locked(gbo, map);
0477     drm_gem_vram_unpin_locked(gbo);
0478 
0479     ttm_bo_unreserve(&gbo->bo);
0480 }
0481 EXPORT_SYMBOL(drm_gem_vram_vunmap);
0482 
0483 /**
0484  * drm_gem_vram_fill_create_dumb() - \
0485     Helper for implementing &struct drm_driver.dumb_create
0486  * @file:       the DRM file
0487  * @dev:        the DRM device
0488  * @pg_align:       the buffer's alignment in multiples of the page size
0489  * @pitch_align:    the scanline's alignment in powers of 2
0490  * @args:       the arguments as provided to \
0491                 &struct drm_driver.dumb_create
0492  *
0493  * This helper function fills &struct drm_mode_create_dumb, which is used
0494  * by &struct drm_driver.dumb_create. Implementations of this interface
0495  * should forwards their arguments to this helper, plus the driver-specific
0496  * parameters.
0497  *
0498  * Returns:
0499  * 0 on success, or
0500  * a negative error code otherwise.
0501  */
0502 int drm_gem_vram_fill_create_dumb(struct drm_file *file,
0503                   struct drm_device *dev,
0504                   unsigned long pg_align,
0505                   unsigned long pitch_align,
0506                   struct drm_mode_create_dumb *args)
0507 {
0508     size_t pitch, size;
0509     struct drm_gem_vram_object *gbo;
0510     int ret;
0511     u32 handle;
0512 
0513     pitch = args->width * DIV_ROUND_UP(args->bpp, 8);
0514     if (pitch_align) {
0515         if (WARN_ON_ONCE(!is_power_of_2(pitch_align)))
0516             return -EINVAL;
0517         pitch = ALIGN(pitch, pitch_align);
0518     }
0519     size = pitch * args->height;
0520 
0521     size = roundup(size, PAGE_SIZE);
0522     if (!size)
0523         return -EINVAL;
0524 
0525     gbo = drm_gem_vram_create(dev, size, pg_align);
0526     if (IS_ERR(gbo))
0527         return PTR_ERR(gbo);
0528 
0529     ret = drm_gem_handle_create(file, &gbo->bo.base, &handle);
0530     if (ret)
0531         goto err_drm_gem_object_put;
0532 
0533     drm_gem_object_put(&gbo->bo.base);
0534 
0535     args->pitch = pitch;
0536     args->size = size;
0537     args->handle = handle;
0538 
0539     return 0;
0540 
0541 err_drm_gem_object_put:
0542     drm_gem_object_put(&gbo->bo.base);
0543     return ret;
0544 }
0545 EXPORT_SYMBOL(drm_gem_vram_fill_create_dumb);
0546 
0547 /*
0548  * Helpers for struct ttm_device_funcs
0549  */
0550 
0551 static bool drm_is_gem_vram(struct ttm_buffer_object *bo)
0552 {
0553     return (bo->destroy == ttm_buffer_object_destroy);
0554 }
0555 
0556 static void drm_gem_vram_bo_driver_evict_flags(struct drm_gem_vram_object *gbo,
0557                            struct ttm_placement *pl)
0558 {
0559     drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
0560     *pl = gbo->placement;
0561 }
0562 
0563 static void drm_gem_vram_bo_driver_move_notify(struct drm_gem_vram_object *gbo)
0564 {
0565     struct ttm_buffer_object *bo = &gbo->bo;
0566     struct drm_device *dev = bo->base.dev;
0567 
0568     if (drm_WARN_ON_ONCE(dev, gbo->vmap_use_count))
0569         return;
0570 
0571     ttm_bo_vunmap(bo, &gbo->map);
0572     iosys_map_clear(&gbo->map); /* explicitly clear mapping for next vmap call */
0573 }
0574 
0575 static int drm_gem_vram_bo_driver_move(struct drm_gem_vram_object *gbo,
0576                        bool evict,
0577                        struct ttm_operation_ctx *ctx,
0578                        struct ttm_resource *new_mem)
0579 {
0580     drm_gem_vram_bo_driver_move_notify(gbo);
0581     return ttm_bo_move_memcpy(&gbo->bo, ctx, new_mem);
0582 }
0583 
0584 /*
0585  * Helpers for struct drm_gem_object_funcs
0586  */
0587 
0588 /**
0589  * drm_gem_vram_object_free() - \
0590     Implements &struct drm_gem_object_funcs.free
0591  * @gem:       GEM object. Refers to &struct drm_gem_vram_object.gem
0592  */
0593 static void drm_gem_vram_object_free(struct drm_gem_object *gem)
0594 {
0595     struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
0596 
0597     drm_gem_vram_put(gbo);
0598 }
0599 
0600 /*
0601  * Helpers for dump buffers
0602  */
0603 
0604 /**
0605  * drm_gem_vram_driver_dumb_create() - \
0606     Implements &struct drm_driver.dumb_create
0607  * @file:       the DRM file
0608  * @dev:        the DRM device
0609  * @args:       the arguments as provided to \
0610                 &struct drm_driver.dumb_create
0611  *
0612  * This function requires the driver to use @drm_device.vram_mm for its
0613  * instance of VRAM MM.
0614  *
0615  * Returns:
0616  * 0 on success, or
0617  * a negative error code otherwise.
0618  */
0619 int drm_gem_vram_driver_dumb_create(struct drm_file *file,
0620                     struct drm_device *dev,
0621                     struct drm_mode_create_dumb *args)
0622 {
0623     if (WARN_ONCE(!dev->vram_mm, "VRAM MM not initialized"))
0624         return -EINVAL;
0625 
0626     return drm_gem_vram_fill_create_dumb(file, dev, 0, 0, args);
0627 }
0628 EXPORT_SYMBOL(drm_gem_vram_driver_dumb_create);
0629 
0630 /*
0631  * Helpers for struct drm_plane_helper_funcs
0632  */
0633 
0634 static void __drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
0635                            struct drm_plane_state *state,
0636                            unsigned int num_planes)
0637 {
0638     struct drm_gem_object *obj;
0639     struct drm_gem_vram_object *gbo;
0640     struct drm_framebuffer *fb = state->fb;
0641 
0642     while (num_planes) {
0643         --num_planes;
0644         obj = drm_gem_fb_get_obj(fb, num_planes);
0645         if (!obj)
0646             continue;
0647         gbo = drm_gem_vram_of_gem(obj);
0648         drm_gem_vram_unpin(gbo);
0649     }
0650 }
0651 
0652 /**
0653  * drm_gem_vram_plane_helper_prepare_fb() - \
0654  *  Implements &struct drm_plane_helper_funcs.prepare_fb
0655  * @plane:  a DRM plane
0656  * @new_state:  the plane's new state
0657  *
0658  * During plane updates, this function sets the plane's fence and
0659  * pins the GEM VRAM objects of the plane's new framebuffer to VRAM.
0660  * Call drm_gem_vram_plane_helper_cleanup_fb() to unpin them.
0661  *
0662  * Returns:
0663  *  0 on success, or
0664  *  a negative errno code otherwise.
0665  */
0666 int
0667 drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane,
0668                      struct drm_plane_state *new_state)
0669 {
0670     struct drm_framebuffer *fb = new_state->fb;
0671     struct drm_gem_vram_object *gbo;
0672     struct drm_gem_object *obj;
0673     unsigned int i;
0674     int ret;
0675 
0676     if (!fb)
0677         return 0;
0678 
0679     for (i = 0; i < fb->format->num_planes; ++i) {
0680         obj = drm_gem_fb_get_obj(fb, i);
0681         if (!obj) {
0682             ret = -EINVAL;
0683             goto err_drm_gem_vram_unpin;
0684         }
0685         gbo = drm_gem_vram_of_gem(obj);
0686         ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
0687         if (ret)
0688             goto err_drm_gem_vram_unpin;
0689     }
0690 
0691     ret = drm_gem_plane_helper_prepare_fb(plane, new_state);
0692     if (ret)
0693         goto err_drm_gem_vram_unpin;
0694 
0695     return 0;
0696 
0697 err_drm_gem_vram_unpin:
0698     __drm_gem_vram_plane_helper_cleanup_fb(plane, new_state, i);
0699     return ret;
0700 }
0701 EXPORT_SYMBOL(drm_gem_vram_plane_helper_prepare_fb);
0702 
0703 /**
0704  * drm_gem_vram_plane_helper_cleanup_fb() - \
0705  *  Implements &struct drm_plane_helper_funcs.cleanup_fb
0706  * @plane:  a DRM plane
0707  * @old_state:  the plane's old state
0708  *
0709  * During plane updates, this function unpins the GEM VRAM
0710  * objects of the plane's old framebuffer from VRAM. Complements
0711  * drm_gem_vram_plane_helper_prepare_fb().
0712  */
0713 void
0714 drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
0715                      struct drm_plane_state *old_state)
0716 {
0717     struct drm_framebuffer *fb = old_state->fb;
0718 
0719     if (!fb)
0720         return;
0721 
0722     __drm_gem_vram_plane_helper_cleanup_fb(plane, old_state, fb->format->num_planes);
0723 }
0724 EXPORT_SYMBOL(drm_gem_vram_plane_helper_cleanup_fb);
0725 
0726 /*
0727  * Helpers for struct drm_simple_display_pipe_funcs
0728  */
0729 
0730 /**
0731  * drm_gem_vram_simple_display_pipe_prepare_fb() - \
0732  *  Implements &struct drm_simple_display_pipe_funcs.prepare_fb
0733  * @pipe:   a simple display pipe
0734  * @new_state:  the plane's new state
0735  *
0736  * During plane updates, this function pins the GEM VRAM
0737  * objects of the plane's new framebuffer to VRAM. Call
0738  * drm_gem_vram_simple_display_pipe_cleanup_fb() to unpin them.
0739  *
0740  * Returns:
0741  *  0 on success, or
0742  *  a negative errno code otherwise.
0743  */
0744 int drm_gem_vram_simple_display_pipe_prepare_fb(
0745     struct drm_simple_display_pipe *pipe,
0746     struct drm_plane_state *new_state)
0747 {
0748     return drm_gem_vram_plane_helper_prepare_fb(&pipe->plane, new_state);
0749 }
0750 EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_prepare_fb);
0751 
0752 /**
0753  * drm_gem_vram_simple_display_pipe_cleanup_fb() - \
0754  *  Implements &struct drm_simple_display_pipe_funcs.cleanup_fb
0755  * @pipe:   a simple display pipe
0756  * @old_state:  the plane's old state
0757  *
0758  * During plane updates, this function unpins the GEM VRAM
0759  * objects of the plane's old framebuffer from VRAM. Complements
0760  * drm_gem_vram_simple_display_pipe_prepare_fb().
0761  */
0762 void drm_gem_vram_simple_display_pipe_cleanup_fb(
0763     struct drm_simple_display_pipe *pipe,
0764     struct drm_plane_state *old_state)
0765 {
0766     drm_gem_vram_plane_helper_cleanup_fb(&pipe->plane, old_state);
0767 }
0768 EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_cleanup_fb);
0769 
0770 /*
0771  * PRIME helpers
0772  */
0773 
0774 /**
0775  * drm_gem_vram_object_pin() - \
0776     Implements &struct drm_gem_object_funcs.pin
0777  * @gem:    The GEM object to pin
0778  *
0779  * Returns:
0780  * 0 on success, or
0781  * a negative errno code otherwise.
0782  */
0783 static int drm_gem_vram_object_pin(struct drm_gem_object *gem)
0784 {
0785     struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
0786 
0787     /* Fbdev console emulation is the use case of these PRIME
0788      * helpers. This may involve updating a hardware buffer from
0789      * a shadow FB. We pin the buffer to it's current location
0790      * (either video RAM or system memory) to prevent it from
0791      * being relocated during the update operation. If you require
0792      * the buffer to be pinned to VRAM, implement a callback that
0793      * sets the flags accordingly.
0794      */
0795     return drm_gem_vram_pin(gbo, 0);
0796 }
0797 
0798 /**
0799  * drm_gem_vram_object_unpin() - \
0800     Implements &struct drm_gem_object_funcs.unpin
0801  * @gem:    The GEM object to unpin
0802  */
0803 static void drm_gem_vram_object_unpin(struct drm_gem_object *gem)
0804 {
0805     struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
0806 
0807     drm_gem_vram_unpin(gbo);
0808 }
0809 
0810 /**
0811  * drm_gem_vram_object_vmap() -
0812  *  Implements &struct drm_gem_object_funcs.vmap
0813  * @gem: The GEM object to map
0814  * @map: Returns the kernel virtual address of the VRAM GEM object's backing
0815  *       store.
0816  *
0817  * Returns:
0818  * 0 on success, or a negative error code otherwise.
0819  */
0820 static int drm_gem_vram_object_vmap(struct drm_gem_object *gem,
0821                     struct iosys_map *map)
0822 {
0823     struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
0824 
0825     return drm_gem_vram_vmap(gbo, map);
0826 }
0827 
0828 /**
0829  * drm_gem_vram_object_vunmap() -
0830  *  Implements &struct drm_gem_object_funcs.vunmap
0831  * @gem: The GEM object to unmap
0832  * @map: Kernel virtual address where the VRAM GEM object was mapped
0833  */
0834 static void drm_gem_vram_object_vunmap(struct drm_gem_object *gem,
0835                        struct iosys_map *map)
0836 {
0837     struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
0838 
0839     drm_gem_vram_vunmap(gbo, map);
0840 }
0841 
0842 /*
0843  * GEM object funcs
0844  */
0845 
0846 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs = {
0847     .free   = drm_gem_vram_object_free,
0848     .pin    = drm_gem_vram_object_pin,
0849     .unpin  = drm_gem_vram_object_unpin,
0850     .vmap   = drm_gem_vram_object_vmap,
0851     .vunmap = drm_gem_vram_object_vunmap,
0852     .mmap   = drm_gem_ttm_mmap,
0853     .print_info = drm_gem_ttm_print_info,
0854 };
0855 
0856 /*
0857  * VRAM memory manager
0858  */
0859 
0860 /*
0861  * TTM TT
0862  */
0863 
0864 static void bo_driver_ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *tt)
0865 {
0866     ttm_tt_fini(tt);
0867     kfree(tt);
0868 }
0869 
0870 /*
0871  * TTM BO device
0872  */
0873 
0874 static struct ttm_tt *bo_driver_ttm_tt_create(struct ttm_buffer_object *bo,
0875                           uint32_t page_flags)
0876 {
0877     struct ttm_tt *tt;
0878     int ret;
0879 
0880     tt = kzalloc(sizeof(*tt), GFP_KERNEL);
0881     if (!tt)
0882         return NULL;
0883 
0884     ret = ttm_tt_init(tt, bo, page_flags, ttm_cached, 0);
0885     if (ret < 0)
0886         goto err_ttm_tt_init;
0887 
0888     return tt;
0889 
0890 err_ttm_tt_init:
0891     kfree(tt);
0892     return NULL;
0893 }
0894 
0895 static void bo_driver_evict_flags(struct ttm_buffer_object *bo,
0896                   struct ttm_placement *placement)
0897 {
0898     struct drm_gem_vram_object *gbo;
0899 
0900     /* TTM may pass BOs that are not GEM VRAM BOs. */
0901     if (!drm_is_gem_vram(bo))
0902         return;
0903 
0904     gbo = drm_gem_vram_of_bo(bo);
0905 
0906     drm_gem_vram_bo_driver_evict_flags(gbo, placement);
0907 }
0908 
0909 static void bo_driver_delete_mem_notify(struct ttm_buffer_object *bo)
0910 {
0911     struct drm_gem_vram_object *gbo;
0912 
0913     /* TTM may pass BOs that are not GEM VRAM BOs. */
0914     if (!drm_is_gem_vram(bo))
0915         return;
0916 
0917     gbo = drm_gem_vram_of_bo(bo);
0918 
0919     drm_gem_vram_bo_driver_move_notify(gbo);
0920 }
0921 
0922 static int bo_driver_move(struct ttm_buffer_object *bo,
0923               bool evict,
0924               struct ttm_operation_ctx *ctx,
0925               struct ttm_resource *new_mem,
0926               struct ttm_place *hop)
0927 {
0928     struct drm_gem_vram_object *gbo;
0929 
0930     gbo = drm_gem_vram_of_bo(bo);
0931 
0932     return drm_gem_vram_bo_driver_move(gbo, evict, ctx, new_mem);
0933 }
0934 
0935 static int bo_driver_io_mem_reserve(struct ttm_device *bdev,
0936                     struct ttm_resource *mem)
0937 {
0938     struct drm_vram_mm *vmm = drm_vram_mm_of_bdev(bdev);
0939 
0940     switch (mem->mem_type) {
0941     case TTM_PL_SYSTEM: /* nothing to do */
0942         break;
0943     case TTM_PL_VRAM:
0944         mem->bus.offset = (mem->start << PAGE_SHIFT) + vmm->vram_base;
0945         mem->bus.is_iomem = true;
0946         mem->bus.caching = ttm_write_combined;
0947         break;
0948     default:
0949         return -EINVAL;
0950     }
0951 
0952     return 0;
0953 }
0954 
0955 static struct ttm_device_funcs bo_driver = {
0956     .ttm_tt_create = bo_driver_ttm_tt_create,
0957     .ttm_tt_destroy = bo_driver_ttm_tt_destroy,
0958     .eviction_valuable = ttm_bo_eviction_valuable,
0959     .evict_flags = bo_driver_evict_flags,
0960     .move = bo_driver_move,
0961     .delete_mem_notify = bo_driver_delete_mem_notify,
0962     .io_mem_reserve = bo_driver_io_mem_reserve,
0963 };
0964 
0965 /*
0966  * struct drm_vram_mm
0967  */
0968 
0969 static int drm_vram_mm_debugfs(struct seq_file *m, void *data)
0970 {
0971     struct drm_info_node *node = (struct drm_info_node *) m->private;
0972     struct drm_vram_mm *vmm = node->minor->dev->vram_mm;
0973     struct ttm_resource_manager *man = ttm_manager_type(&vmm->bdev, TTM_PL_VRAM);
0974     struct drm_printer p = drm_seq_file_printer(m);
0975 
0976     ttm_resource_manager_debug(man, &p);
0977     return 0;
0978 }
0979 
0980 static const struct drm_info_list drm_vram_mm_debugfs_list[] = {
0981     { "vram-mm", drm_vram_mm_debugfs, 0, NULL },
0982 };
0983 
0984 /**
0985  * drm_vram_mm_debugfs_init() - Register VRAM MM debugfs file.
0986  *
0987  * @minor: drm minor device.
0988  *
0989  */
0990 void drm_vram_mm_debugfs_init(struct drm_minor *minor)
0991 {
0992     drm_debugfs_create_files(drm_vram_mm_debugfs_list,
0993                  ARRAY_SIZE(drm_vram_mm_debugfs_list),
0994                  minor->debugfs_root, minor);
0995 }
0996 EXPORT_SYMBOL(drm_vram_mm_debugfs_init);
0997 
0998 static int drm_vram_mm_init(struct drm_vram_mm *vmm, struct drm_device *dev,
0999                 uint64_t vram_base, size_t vram_size)
1000 {
1001     int ret;
1002 
1003     vmm->vram_base = vram_base;
1004     vmm->vram_size = vram_size;
1005 
1006     ret = ttm_device_init(&vmm->bdev, &bo_driver, dev->dev,
1007                  dev->anon_inode->i_mapping,
1008                  dev->vma_offset_manager,
1009                  false, true);
1010     if (ret)
1011         return ret;
1012 
1013     ret = ttm_range_man_init(&vmm->bdev, TTM_PL_VRAM,
1014                  false, vram_size >> PAGE_SHIFT);
1015     if (ret)
1016         return ret;
1017 
1018     return 0;
1019 }
1020 
1021 static void drm_vram_mm_cleanup(struct drm_vram_mm *vmm)
1022 {
1023     ttm_range_man_fini(&vmm->bdev, TTM_PL_VRAM);
1024     ttm_device_fini(&vmm->bdev);
1025 }
1026 
1027 /*
1028  * Helpers for integration with struct drm_device
1029  */
1030 
1031 static struct drm_vram_mm *drm_vram_helper_alloc_mm(struct drm_device *dev, uint64_t vram_base,
1032                             size_t vram_size)
1033 {
1034     int ret;
1035 
1036     if (WARN_ON(dev->vram_mm))
1037         return dev->vram_mm;
1038 
1039     dev->vram_mm = kzalloc(sizeof(*dev->vram_mm), GFP_KERNEL);
1040     if (!dev->vram_mm)
1041         return ERR_PTR(-ENOMEM);
1042 
1043     ret = drm_vram_mm_init(dev->vram_mm, dev, vram_base, vram_size);
1044     if (ret)
1045         goto err_kfree;
1046 
1047     return dev->vram_mm;
1048 
1049 err_kfree:
1050     kfree(dev->vram_mm);
1051     dev->vram_mm = NULL;
1052     return ERR_PTR(ret);
1053 }
1054 
1055 static void drm_vram_helper_release_mm(struct drm_device *dev)
1056 {
1057     if (!dev->vram_mm)
1058         return;
1059 
1060     drm_vram_mm_cleanup(dev->vram_mm);
1061     kfree(dev->vram_mm);
1062     dev->vram_mm = NULL;
1063 }
1064 
1065 static void drm_vram_mm_release(struct drm_device *dev, void *ptr)
1066 {
1067     drm_vram_helper_release_mm(dev);
1068 }
1069 
1070 /**
1071  * drmm_vram_helper_init - Initializes a device's instance of
1072  *                         &struct drm_vram_mm
1073  * @dev:    the DRM device
1074  * @vram_base:  the base address of the video memory
1075  * @vram_size:  the size of the video memory in bytes
1076  *
1077  * Creates a new instance of &struct drm_vram_mm and stores it in
1078  * struct &drm_device.vram_mm. The instance is auto-managed and cleaned
1079  * up as part of device cleanup. Calling this function multiple times
1080  * will generate an error message.
1081  *
1082  * Returns:
1083  * 0 on success, or a negative errno code otherwise.
1084  */
1085 int drmm_vram_helper_init(struct drm_device *dev, uint64_t vram_base,
1086               size_t vram_size)
1087 {
1088     struct drm_vram_mm *vram_mm;
1089 
1090     if (drm_WARN_ON_ONCE(dev, dev->vram_mm))
1091         return 0;
1092 
1093     vram_mm = drm_vram_helper_alloc_mm(dev, vram_base, vram_size);
1094     if (IS_ERR(vram_mm))
1095         return PTR_ERR(vram_mm);
1096     return drmm_add_action_or_reset(dev, drm_vram_mm_release, NULL);
1097 }
1098 EXPORT_SYMBOL(drmm_vram_helper_init);
1099 
1100 /*
1101  * Mode-config helpers
1102  */
1103 
1104 static enum drm_mode_status
1105 drm_vram_helper_mode_valid_internal(struct drm_device *dev,
1106                     const struct drm_display_mode *mode,
1107                     unsigned long max_bpp)
1108 {
1109     struct drm_vram_mm *vmm = dev->vram_mm;
1110     unsigned long fbsize, fbpages, max_fbpages;
1111 
1112     if (WARN_ON(!dev->vram_mm))
1113         return MODE_BAD;
1114 
1115     max_fbpages = (vmm->vram_size / 2) >> PAGE_SHIFT;
1116 
1117     fbsize = mode->hdisplay * mode->vdisplay * max_bpp;
1118     fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE);
1119 
1120     if (fbpages > max_fbpages)
1121         return MODE_MEM;
1122 
1123     return MODE_OK;
1124 }
1125 
1126 /**
1127  * drm_vram_helper_mode_valid - Tests if a display mode's
1128  *  framebuffer fits into the available video memory.
1129  * @dev:    the DRM device
1130  * @mode:   the mode to test
1131  *
1132  * This function tests if enough video memory is available for using the
1133  * specified display mode. Atomic modesetting requires importing the
1134  * designated framebuffer into video memory before evicting the active
1135  * one. Hence, any framebuffer may consume at most half of the available
1136  * VRAM. Display modes that require a larger framebuffer can not be used,
1137  * even if the CRTC does support them. Each framebuffer is assumed to
1138  * have 32-bit color depth.
1139  *
1140  * Note:
1141  * The function can only test if the display mode is supported in
1142  * general. If there are too many framebuffers pinned to video memory,
1143  * a display mode may still not be usable in practice. The color depth of
1144  * 32-bit fits all current use case. A more flexible test can be added
1145  * when necessary.
1146  *
1147  * Returns:
1148  * MODE_OK if the display mode is supported, or an error code of type
1149  * enum drm_mode_status otherwise.
1150  */
1151 enum drm_mode_status
1152 drm_vram_helper_mode_valid(struct drm_device *dev,
1153                const struct drm_display_mode *mode)
1154 {
1155     static const unsigned long max_bpp = 4; /* DRM_FORMAT_XRGB8888 */
1156 
1157     return drm_vram_helper_mode_valid_internal(dev, mode, max_bpp);
1158 }
1159 EXPORT_SYMBOL(drm_vram_helper_mode_valid);
1160 
1161 MODULE_DESCRIPTION("DRM VRAM memory-management helpers");
1162 MODULE_LICENSE("GPL");