/** * DOC: overview * * This library provides &struct drm_gem_vram_object (GEM VRAM), a GEM * buffer object that is backed by video RAM (VRAM). It can be used for * framebuffer devices with dedicated memory. * * The data structure &struct drm_vram_mm and its helpers implement a memory * manager for simple framebuffer devices with dedicated video memory. GEM * VRAM buffer objects are either placed in the video memory or remain evicted * to system memory. * * With the GEM interface userspace applications create, manage and destroy * graphics buffers, such as an on-screen framebuffer. GEM does not provide * an implementation of these interfaces. It's up to the DRM driver to * provide an implementation that suits the hardware. If the hardware device * contains dedicated video memory, the DRM driver can use the VRAM helper * library. Each active buffer object is stored in video RAM. Active * buffer are used for drawing the current frame, typically something like * the frame's scanout buffer or the cursor image. If there's no more space * left in VRAM, inactive GEM objects can be moved to system memory. * * To initialize the VRAM helper library call drmm_vram_helper_init(). * The function allocates and initializes an instance of &struct drm_vram_mm * in &struct drm_device.vram_mm . Use &DRM_GEM_VRAM_DRIVER to initialize * &struct drm_driver and &DRM_VRAM_MM_FILE_OPERATIONS to initialize * &struct file_operations; as illustrated below. * * .. code-block:: c * * struct file_operations fops ={ * .owner = THIS_MODULE, * DRM_VRAM_MM_FILE_OPERATION * }; * struct drm_driver drv = { * .driver_feature = DRM_ ... , * .fops = &fops, * DRM_GEM_VRAM_DRIVER * }; * * int init_drm_driver() * { * struct drm_device *dev; * uint64_t vram_base; * unsigned long vram_size; * int ret; * * // setup device, vram base and size * // ... * * ret = drmm_vram_helper_init(dev, vram_base, vram_size); * if (ret) * return ret; * return 0; * } * * This creates an instance of &struct drm_vram_mm, exports DRM userspace * interfaces for GEM buffer management and initializes file operations to * allow for accessing created GEM buffers. With this setup, the DRM driver * manages an area of video RAM with VRAM MM and provides GEM VRAM objects * to userspace. * * You don't have to clean up the instance of VRAM MM. * drmm_vram_helper_init() is a managed interface that installs a * clean-up handler to run during the DRM device's release. * * A buffer object that is pinned in video RAM has a fixed address within that * memory region. Call drm_gem_vram_offset() to retrieve this value. Typically * it's used to program the hardware's scanout engine for framebuffers, set * the cursor overlay's image for a mouse cursor, or use it as input to the * hardware's drawing engine. * * To access a buffer object's memory from the DRM driver, call * drm_gem_vram_vmap(). It maps the buffer into kernel address * space and returns the memory address. Use drm_gem_vram_vunmap() to * release the mapping.
*/
/* * Buffer-objects helpers
*/
staticvoid drm_gem_vram_cleanup(struct drm_gem_vram_object *gbo)
{ /* We got here via ttm_bo_put(), which means that the * TTM buffer object in 'bo' has already been cleaned * up; only release the GEM object.
*/
for (i = 0; i < c; ++i) {
gbo->placements[i].fpfn = 0;
gbo->placements[i].lpfn = 0;
}
}
/** * drm_gem_vram_create() - Creates a VRAM-backed GEM object * @dev: the DRM device * @size: the buffer size in bytes * @pg_align: the buffer's alignment in multiples of the page size * * GEM objects are allocated by calling struct drm_driver.gem_create_object, * if set. Otherwise kzalloc() will be used. Drivers can set their own GEM * object functions in struct drm_driver.gem_create_object. If no functions * are set, the new GEM object will use the default functions from GEM VRAM * helpers. * * Returns: * A new instance of &struct drm_gem_vram_object on success, or * an ERR_PTR()-encoded error code otherwise.
*/ struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
size_t size, unsignedlong pg_align)
{ struct drm_gem_vram_object *gbo; struct drm_gem_object *gem; struct drm_vram_mm *vmm = dev->vram_mm; struct ttm_device *bdev; int ret;
if (WARN_ONCE(!vmm, "VRAM MM not initialized")) return ERR_PTR(-EINVAL);
if (dev->driver->gem_create_object) {
gem = dev->driver->gem_create_object(dev, size); if (IS_ERR(gem)) return ERR_CAST(gem);
gbo = drm_gem_vram_of_gem(gem);
} else {
gbo = kzalloc(sizeof(*gbo), GFP_KERNEL); if (!gbo) return ERR_PTR(-ENOMEM);
gem = &gbo->bo.base;
}
if (!gem->funcs)
gem->funcs = &drm_gem_vram_object_funcs;
ret = drm_gem_object_init(dev, gem, size); if (ret) {
kfree(gbo); return ERR_PTR(ret);
}
/* * A failing ttm_bo_init will call ttm_buffer_object_destroy * to release gbo->bo.base and kfree gbo.
*/
ret = ttm_bo_init_validate(bdev, &gbo->bo, ttm_bo_type_device,
&gbo->placement, pg_align, false, NULL, NULL,
ttm_buffer_object_destroy); if (ret) return ERR_PTR(ret);
return gbo;
}
EXPORT_SYMBOL(drm_gem_vram_create);
/** * drm_gem_vram_put() - Releases a reference to a VRAM-backed GEM object * @gbo: the GEM VRAM object * * See ttm_bo_put() for more information.
*/ void drm_gem_vram_put(struct drm_gem_vram_object *gbo)
{
ttm_bo_put(&gbo->bo);
}
EXPORT_SYMBOL(drm_gem_vram_put);
static u64 drm_gem_vram_pg_offset(struct drm_gem_vram_object *gbo)
{ /* Keep TTM behavior for now, remove when drivers are audited */ if (WARN_ON_ONCE(!gbo->bo.resource ||
gbo->bo.resource->mem_type == TTM_PL_SYSTEM)) return 0;
return gbo->bo.resource->start;
}
/** * drm_gem_vram_offset() - Returns a GEM VRAM object's offset in video memory * @gbo: the GEM VRAM object * * This function returns the buffer object's offset in the device's video * memory. The buffer object has to be pinned to %TTM_PL_VRAM. * * Returns: * The buffer object's offset in video memory on success, or * a negative errno code otherwise.
*/
s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo)
{ if (WARN_ON_ONCE(!gbo->bo.pin_count)) return (s64)-ENODEV; return drm_gem_vram_pg_offset(gbo) << PAGE_SHIFT;
}
EXPORT_SYMBOL(drm_gem_vram_offset);
/** * drm_gem_vram_vmap() - Pins and maps a GEM VRAM object into kernel address * space * @gbo: The GEM VRAM object to map * @map: Returns the kernel virtual address of the VRAM GEM object's backing * store. * * The vmap function pins a GEM VRAM object to its current location, either * system or video memory, and maps its buffer into kernel address space. * As pinned object cannot be relocated, you should avoid pinning objects * permanently. Call drm_gem_vram_vunmap() with the returned address to * unmap and unpin the GEM VRAM object. * * Returns: * 0 on success, or a negative error code otherwise.
*/ int drm_gem_vram_vmap(struct drm_gem_vram_object *gbo, struct iosys_map *map)
{ int ret;
dma_resv_assert_held(gbo->bo.base.resv);
if (gbo->vmap_use_count > 0) goto out;
/* * VRAM helpers unmap the BO only on demand. So the previous * page mapping might still be around. Only vmap if the there's * no mapping present.
*/ if (iosys_map_is_null(&gbo->map)) {
ret = ttm_bo_vmap(&gbo->bo, &gbo->map); if (ret) return ret;
}
out:
++gbo->vmap_use_count;
*map = gbo->map;
return 0;
}
EXPORT_SYMBOL(drm_gem_vram_vmap);
/** * drm_gem_vram_vunmap() - Unmaps and unpins a GEM VRAM object * @gbo: The GEM VRAM object to unmap * @map: Kernel virtual address where the VRAM GEM object was mapped * * A call to drm_gem_vram_vunmap() unmaps and unpins a GEM VRAM buffer. See * the documentation for drm_gem_vram_vmap() for more information.
*/ void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo, struct iosys_map *map)
{ struct drm_device *dev = gbo->bo.base.dev;
dma_resv_assert_held(gbo->bo.base.resv);
if (drm_WARN_ON_ONCE(dev, !gbo->vmap_use_count)) return;
if (drm_WARN_ON_ONCE(dev, !iosys_map_is_equal(&gbo->map, map))) return; /* BUG: map not mapped from this BO */
if (--gbo->vmap_use_count > 0) return;
/* * Permanently mapping and unmapping buffers adds overhead from * updating the page tables and creates debugging output. Therefore, * we delay the actual unmap operation until the BO gets evicted * from memory. See drm_gem_vram_bo_driver_move_notify().
*/
}
EXPORT_SYMBOL(drm_gem_vram_vunmap);
/** * drm_gem_vram_fill_create_dumb() - Helper for implementing * &struct drm_driver.dumb_create * * @file: the DRM file * @dev: the DRM device * @pg_align: the buffer's alignment in multiples of the page size * @pitch_align: the scanline's alignment in powers of 2 * @args: the arguments as provided to * &struct drm_driver.dumb_create * * This helper function fills &struct drm_mode_create_dumb, which is used * by &struct drm_driver.dumb_create. Implementations of this interface * should forwards their arguments to this helper, plus the driver-specific * parameters. * * Returns: * 0 on success, or * a negative error code otherwise.
*/ int drm_gem_vram_fill_create_dumb(struct drm_file *file, struct drm_device *dev, unsignedlong pg_align, unsignedlong pitch_align, struct drm_mode_create_dumb *args)
{
size_t pitch, size; struct drm_gem_vram_object *gbo; int ret;
u32 handle;
/** * drm_gem_vram_driver_dumb_create() - Implements &struct drm_driver.dumb_create * @file: the DRM file * @dev: the DRM device * @args: the arguments as provided to * &struct drm_driver.dumb_create * * This function requires the driver to use @drm_device.vram_mm for its * instance of VRAM MM. * * Returns: * 0 on success, or * a negative error code otherwise.
*/ int drm_gem_vram_driver_dumb_create(struct drm_file *file, struct drm_device *dev, struct drm_mode_create_dumb *args)
{ if (WARN_ONCE(!dev->vram_mm, "VRAM MM not initialized")) return -EINVAL;
while (num_planes) {
--num_planes;
obj = drm_gem_fb_get_obj(fb, num_planes); if (!obj) continue;
gbo = drm_gem_vram_of_gem(obj);
drm_gem_vram_unpin(gbo);
}
}
/** * drm_gem_vram_plane_helper_prepare_fb() - Implements &struct * drm_plane_helper_funcs.prepare_fb * @plane: a DRM plane * @new_state: the plane's new state * * During plane updates, this function sets the plane's fence and * pins the GEM VRAM objects of the plane's new framebuffer to VRAM. * Call drm_gem_vram_plane_helper_cleanup_fb() to unpin them. * * Returns: * 0 on success, or * a negative errno code otherwise.
*/ int
drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane, struct drm_plane_state *new_state)
{ struct drm_framebuffer *fb = new_state->fb; struct drm_gem_vram_object *gbo; struct drm_gem_object *obj; unsignedint i; int ret;
if (!fb) return 0;
for (i = 0; i < fb->format->num_planes; ++i) {
obj = drm_gem_fb_get_obj(fb, i); if (!obj) {
ret = -EINVAL; goto err_drm_gem_vram_unpin;
}
gbo = drm_gem_vram_of_gem(obj);
ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM); if (ret) goto err_drm_gem_vram_unpin;
}
ret = drm_gem_plane_helper_prepare_fb(plane, new_state); if (ret) goto err_drm_gem_vram_unpin;
/** * drm_gem_vram_plane_helper_cleanup_fb() - Implements &struct * drm_plane_helper_funcs.cleanup_fb * @plane: a DRM plane * @old_state: the plane's old state * * During plane updates, this function unpins the GEM VRAM * objects of the plane's old framebuffer from VRAM. Complements * drm_gem_vram_plane_helper_prepare_fb().
*/ void
drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane, struct drm_plane_state *old_state)
{ struct drm_framebuffer *fb = old_state->fb;
/** * drmm_vram_helper_init - Initializes a device's instance of * &struct drm_vram_mm * @dev: the DRM device * @vram_base: the base address of the video memory * @vram_size: the size of the video memory in bytes * * Creates a new instance of &struct drm_vram_mm and stores it in * struct &drm_device.vram_mm. The instance is auto-managed and cleaned * up as part of device cleanup. Calling this function multiple times * will generate an error message. * * Returns: * 0 on success, or a negative errno code otherwise.
*/ int drmm_vram_helper_init(struct drm_device *dev, uint64_t vram_base,
size_t vram_size)
{ struct drm_vram_mm *vram_mm;
if (drm_WARN_ON_ONCE(dev, dev->vram_mm)) return 0;
/** * drm_vram_helper_mode_valid - Tests if a display mode's * framebuffer fits into the available video memory. * @dev: the DRM device * @mode: the mode to test * * This function tests if enough video memory is available for using the * specified display mode. Atomic modesetting requires importing the * designated framebuffer into video memory before evicting the active * one. Hence, any framebuffer may consume at most half of the available * VRAM. Display modes that require a larger framebuffer can not be used, * even if the CRTC does support them. Each framebuffer is assumed to * have 32-bit color depth. * * Note: * The function can only test if the display mode is supported in * general. If there are too many framebuffers pinned to video memory, * a display mode may still not be usable in practice. The color depth of * 32-bit fits all current use case. A more flexible test can be added * when necessary. * * Returns: * MODE_OK if the display mode is supported, or an error code of type * enum drm_mode_status otherwise.
*/ enum drm_mode_status
drm_vram_helper_mode_valid(struct drm_device *dev, conststruct drm_display_mode *mode)
{ staticconstunsignedlong max_bpp = 4; /* DRM_FORMAT_XRGB8888 */
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.