1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/iosys-map.h>
4 #include <linux/module.h>
6 #include <drm/drm_debugfs.h>
7 #include <drm/drm_device.h>
8 #include <drm/drm_drv.h>
9 #include <drm/drm_file.h>
10 #include <drm/drm_framebuffer.h>
11 #include <drm/drm_gem_atomic_helper.h>
12 #include <drm/drm_gem_framebuffer_helper.h>
13 #include <drm/drm_gem_ttm_helper.h>
14 #include <drm/drm_gem_vram_helper.h>
15 #include <drm/drm_managed.h>
16 #include <drm/drm_mode.h>
17 #include <drm/drm_plane.h>
18 #include <drm/drm_prime.h>
19 #include <drm/drm_simple_kms_helper.h>
21 #include <drm/ttm/ttm_range_manager.h>
22 #include <drm/ttm/ttm_tt.h>
24 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs;
29 * This library provides &struct drm_gem_vram_object (GEM VRAM), a GEM
30 * buffer object that is backed by video RAM (VRAM). It can be used for
31 * framebuffer devices with dedicated memory.
33 * The data structure &struct drm_vram_mm and its helpers implement a memory
34 * manager for simple framebuffer devices with dedicated video memory. GEM
35 * VRAM buffer objects are either placed in the video memory or remain evicted
38 * With the GEM interface userspace applications create, manage and destroy
39 * graphics buffers, such as an on-screen framebuffer. GEM does not provide
40 * an implementation of these interfaces. It's up to the DRM driver to
41 * provide an implementation that suits the hardware. If the hardware device
42 * contains dedicated video memory, the DRM driver can use the VRAM helper
43 * library. Each active buffer object is stored in video RAM. Active
44 * buffer are used for drawing the current frame, typically something like
45 * the frame's scanout buffer or the cursor image. If there's no more space
46 * left in VRAM, inactive GEM objects can be moved to system memory.
48 * To initialize the VRAM helper library call drmm_vram_helper_init().
49 * The function allocates and initializes an instance of &struct drm_vram_mm
50 * in &struct drm_device.vram_mm . Use &DRM_GEM_VRAM_DRIVER to initialize
51 * &struct drm_driver and &DRM_VRAM_MM_FILE_OPERATIONS to initialize
52 * &struct file_operations; as illustrated below.
56 * struct file_operations fops ={
57 * .owner = THIS_MODULE,
58 * DRM_VRAM_MM_FILE_OPERATION
60 * struct drm_driver drv = {
61 * .driver_feature = DRM_ ... ,
66 * int init_drm_driver()
68 * struct drm_device *dev;
70 * unsigned long vram_size;
73 * // setup device, vram base and size
76 * ret = drmm_vram_helper_init(dev, vram_base, vram_size);
82 * This creates an instance of &struct drm_vram_mm, exports DRM userspace
83 * interfaces for GEM buffer management and initializes file operations to
84 * allow for accessing created GEM buffers. With this setup, the DRM driver
85 * manages an area of video RAM with VRAM MM and provides GEM VRAM objects
88 * You don't have to clean up the instance of VRAM MM.
89 * drmm_vram_helper_init() is a managed interface that installs a
90 * clean-up handler to run during the DRM device's release.
92 * For drawing or scanout operations, rsp. buffer objects have to be pinned
93 * in video RAM. Call drm_gem_vram_pin() with &DRM_GEM_VRAM_PL_FLAG_VRAM or
94 * &DRM_GEM_VRAM_PL_FLAG_SYSTEM to pin a buffer object in video RAM or system
95 * memory. Call drm_gem_vram_unpin() to release the pinned object afterwards.
97 * A buffer object that is pinned in video RAM has a fixed address within that
98 * memory region. Call drm_gem_vram_offset() to retrieve this value. Typically
99 * it's used to program the hardware's scanout engine for framebuffers, set
100 * the cursor overlay's image for a mouse cursor, or use it as input to the
101 * hardware's drawing engine.
103 * To access a buffer object's memory from the DRM driver, call
104 * drm_gem_vram_vmap(). It maps the buffer into kernel address
105 * space and returns the memory address. Use drm_gem_vram_vunmap() to
106 * release the mapping.
110 * Buffer-objects helpers
113 static void drm_gem_vram_cleanup(struct drm_gem_vram_object *gbo)
115 /* We got here via ttm_bo_put(), which means that the
116 * TTM buffer object in 'bo' has already been cleaned
117 * up; only release the GEM object.
120 WARN_ON(gbo->vmap_use_count);
121 WARN_ON(iosys_map_is_set(&gbo->map));
123 drm_gem_object_release(&gbo->bo.base);
126 static void drm_gem_vram_destroy(struct drm_gem_vram_object *gbo)
128 drm_gem_vram_cleanup(gbo);
132 static void ttm_buffer_object_destroy(struct ttm_buffer_object *bo)
134 struct drm_gem_vram_object *gbo = drm_gem_vram_of_bo(bo);
136 drm_gem_vram_destroy(gbo);
139 static void drm_gem_vram_placement(struct drm_gem_vram_object *gbo,
140 unsigned long pl_flag)
142 u32 invariant_flags = 0;
146 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_TOPDOWN)
147 invariant_flags = TTM_PL_FLAG_TOPDOWN;
149 gbo->placement.placement = gbo->placements;
150 gbo->placement.busy_placement = gbo->placements;
152 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_VRAM) {
153 gbo->placements[c].mem_type = TTM_PL_VRAM;
154 gbo->placements[c++].flags = invariant_flags;
157 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_SYSTEM || !c) {
158 gbo->placements[c].mem_type = TTM_PL_SYSTEM;
159 gbo->placements[c++].flags = invariant_flags;
162 gbo->placement.num_placement = c;
163 gbo->placement.num_busy_placement = c;
165 for (i = 0; i < c; ++i) {
166 gbo->placements[i].fpfn = 0;
167 gbo->placements[i].lpfn = 0;
172 * drm_gem_vram_create() - Creates a VRAM-backed GEM object
173 * @dev: the DRM device
174 * @size: the buffer size in bytes
175 * @pg_align: the buffer's alignment in multiples of the page size
177 * GEM objects are allocated by calling struct drm_driver.gem_create_object,
178 * if set. Otherwise kzalloc() will be used. Drivers can set their own GEM
179 * object functions in struct drm_driver.gem_create_object. If no functions
180 * are set, the new GEM object will use the default functions from GEM VRAM
184 * A new instance of &struct drm_gem_vram_object on success, or
185 * an ERR_PTR()-encoded error code otherwise.
187 struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
189 unsigned long pg_align)
191 struct drm_gem_vram_object *gbo;
192 struct drm_gem_object *gem;
193 struct drm_vram_mm *vmm = dev->vram_mm;
194 struct ttm_device *bdev;
197 if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
198 return ERR_PTR(-EINVAL);
200 if (dev->driver->gem_create_object) {
201 gem = dev->driver->gem_create_object(dev, size);
203 return ERR_CAST(gem);
204 gbo = drm_gem_vram_of_gem(gem);
206 gbo = kzalloc(sizeof(*gbo), GFP_KERNEL);
208 return ERR_PTR(-ENOMEM);
213 gem->funcs = &drm_gem_vram_object_funcs;
215 ret = drm_gem_object_init(dev, gem, size);
224 drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
227 * A failing ttm_bo_init will call ttm_buffer_object_destroy
228 * to release gbo->bo.base and kfree gbo.
230 ret = ttm_bo_init_validate(bdev, &gbo->bo, ttm_bo_type_device,
231 &gbo->placement, pg_align, false, NULL, NULL,
232 ttm_buffer_object_destroy);
238 EXPORT_SYMBOL(drm_gem_vram_create);
241 * drm_gem_vram_put() - Releases a reference to a VRAM-backed GEM object
242 * @gbo: the GEM VRAM object
244 * See ttm_bo_put() for more information.
246 void drm_gem_vram_put(struct drm_gem_vram_object *gbo)
248 ttm_bo_put(&gbo->bo);
250 EXPORT_SYMBOL(drm_gem_vram_put);
252 static u64 drm_gem_vram_pg_offset(struct drm_gem_vram_object *gbo)
254 /* Keep TTM behavior for now, remove when drivers are audited */
255 if (WARN_ON_ONCE(!gbo->bo.resource ||
256 gbo->bo.resource->mem_type == TTM_PL_SYSTEM))
259 return gbo->bo.resource->start;
263 * drm_gem_vram_offset() - Returns a GEM VRAM object's offset in video memory
264 * @gbo: the GEM VRAM object
266 * This function returns the buffer object's offset in the device's video
267 * memory. The buffer object has to be pinned to %TTM_PL_VRAM.
270 * The buffer object's offset in video memory on success, or
271 * a negative errno code otherwise.
273 s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo)
275 if (WARN_ON_ONCE(!gbo->bo.pin_count))
277 return drm_gem_vram_pg_offset(gbo) << PAGE_SHIFT;
279 EXPORT_SYMBOL(drm_gem_vram_offset);
281 static int drm_gem_vram_pin_locked(struct drm_gem_vram_object *gbo,
282 unsigned long pl_flag)
284 struct ttm_operation_ctx ctx = { false, false };
287 if (gbo->bo.pin_count)
291 drm_gem_vram_placement(gbo, pl_flag);
293 ret = ttm_bo_validate(&gbo->bo, &gbo->placement, &ctx);
298 ttm_bo_pin(&gbo->bo);
304 * drm_gem_vram_pin() - Pins a GEM VRAM object in a region.
305 * @gbo: the GEM VRAM object
306 * @pl_flag: a bitmask of possible memory regions
308 * Pinning a buffer object ensures that it is not evicted from
309 * a memory region. A pinned buffer object has to be unpinned before
310 * it can be pinned to another region. If the pl_flag argument is 0,
311 * the buffer is pinned at its current location (video RAM or system
314 * Small buffer objects, such as cursor images, can lead to memory
315 * fragmentation if they are pinned in the middle of video RAM. This
316 * is especially a problem on devices with only a small amount of
317 * video RAM. Fragmentation can prevent the primary framebuffer from
318 * fitting in, even though there's enough memory overall. The modifier
319 * DRM_GEM_VRAM_PL_FLAG_TOPDOWN marks the buffer object to be pinned
320 * at the high end of the memory region to avoid fragmentation.
324 * a negative error code otherwise.
326 int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag)
330 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
333 ret = drm_gem_vram_pin_locked(gbo, pl_flag);
334 ttm_bo_unreserve(&gbo->bo);
338 EXPORT_SYMBOL(drm_gem_vram_pin);
340 static void drm_gem_vram_unpin_locked(struct drm_gem_vram_object *gbo)
342 ttm_bo_unpin(&gbo->bo);
346 * drm_gem_vram_unpin() - Unpins a GEM VRAM object
347 * @gbo: the GEM VRAM object
351 * a negative error code otherwise.
353 int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo)
357 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
361 drm_gem_vram_unpin_locked(gbo);
362 ttm_bo_unreserve(&gbo->bo);
366 EXPORT_SYMBOL(drm_gem_vram_unpin);
368 static int drm_gem_vram_kmap_locked(struct drm_gem_vram_object *gbo,
369 struct iosys_map *map)
373 if (gbo->vmap_use_count > 0)
377 * VRAM helpers unmap the BO only on demand. So the previous
378 * page mapping might still be around. Only vmap if the there's
379 * no mapping present.
381 if (iosys_map_is_null(&gbo->map)) {
382 ret = ttm_bo_vmap(&gbo->bo, &gbo->map);
388 ++gbo->vmap_use_count;
394 static void drm_gem_vram_kunmap_locked(struct drm_gem_vram_object *gbo,
395 struct iosys_map *map)
397 struct drm_device *dev = gbo->bo.base.dev;
399 if (drm_WARN_ON_ONCE(dev, !gbo->vmap_use_count))
402 if (drm_WARN_ON_ONCE(dev, !iosys_map_is_equal(&gbo->map, map)))
403 return; /* BUG: map not mapped from this BO */
405 if (--gbo->vmap_use_count > 0)
409 * Permanently mapping and unmapping buffers adds overhead from
410 * updating the page tables and creates debugging output. Therefore,
411 * we delay the actual unmap operation until the BO gets evicted
412 * from memory. See drm_gem_vram_bo_driver_move_notify().
417 * drm_gem_vram_vmap() - Pins and maps a GEM VRAM object into kernel address
419 * @gbo: The GEM VRAM object to map
420 * @map: Returns the kernel virtual address of the VRAM GEM object's backing
423 * The vmap function pins a GEM VRAM object to its current location, either
424 * system or video memory, and maps its buffer into kernel address space.
425 * As pinned object cannot be relocated, you should avoid pinning objects
426 * permanently. Call drm_gem_vram_vunmap() with the returned address to
427 * unmap and unpin the GEM VRAM object.
430 * 0 on success, or a negative error code otherwise.
432 int drm_gem_vram_vmap(struct drm_gem_vram_object *gbo, struct iosys_map *map)
436 dma_resv_assert_held(gbo->bo.base.resv);
438 ret = drm_gem_vram_pin_locked(gbo, 0);
441 ret = drm_gem_vram_kmap_locked(gbo, map);
443 goto err_drm_gem_vram_unpin_locked;
447 err_drm_gem_vram_unpin_locked:
448 drm_gem_vram_unpin_locked(gbo);
451 EXPORT_SYMBOL(drm_gem_vram_vmap);
454 * drm_gem_vram_vunmap() - Unmaps and unpins a GEM VRAM object
455 * @gbo: The GEM VRAM object to unmap
456 * @map: Kernel virtual address where the VRAM GEM object was mapped
458 * A call to drm_gem_vram_vunmap() unmaps and unpins a GEM VRAM buffer. See
459 * the documentation for drm_gem_vram_vmap() for more information.
461 void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo,
462 struct iosys_map *map)
464 dma_resv_assert_held(gbo->bo.base.resv);
466 drm_gem_vram_kunmap_locked(gbo, map);
467 drm_gem_vram_unpin_locked(gbo);
469 EXPORT_SYMBOL(drm_gem_vram_vunmap);
472 * drm_gem_vram_fill_create_dumb() - Helper for implementing
473 * &struct drm_driver.dumb_create
475 * @file: the DRM file
476 * @dev: the DRM device
477 * @pg_align: the buffer's alignment in multiples of the page size
478 * @pitch_align: the scanline's alignment in powers of 2
479 * @args: the arguments as provided to
480 * &struct drm_driver.dumb_create
482 * This helper function fills &struct drm_mode_create_dumb, which is used
483 * by &struct drm_driver.dumb_create. Implementations of this interface
484 * should forwards their arguments to this helper, plus the driver-specific
489 * a negative error code otherwise.
491 int drm_gem_vram_fill_create_dumb(struct drm_file *file,
492 struct drm_device *dev,
493 unsigned long pg_align,
494 unsigned long pitch_align,
495 struct drm_mode_create_dumb *args)
498 struct drm_gem_vram_object *gbo;
502 pitch = args->width * DIV_ROUND_UP(args->bpp, 8);
504 if (WARN_ON_ONCE(!is_power_of_2(pitch_align)))
506 pitch = ALIGN(pitch, pitch_align);
508 size = pitch * args->height;
510 size = roundup(size, PAGE_SIZE);
514 gbo = drm_gem_vram_create(dev, size, pg_align);
518 ret = drm_gem_handle_create(file, &gbo->bo.base, &handle);
520 goto err_drm_gem_object_put;
522 drm_gem_object_put(&gbo->bo.base);
526 args->handle = handle;
530 err_drm_gem_object_put:
531 drm_gem_object_put(&gbo->bo.base);
534 EXPORT_SYMBOL(drm_gem_vram_fill_create_dumb);
537 * Helpers for struct ttm_device_funcs
540 static bool drm_is_gem_vram(struct ttm_buffer_object *bo)
542 return (bo->destroy == ttm_buffer_object_destroy);
545 static void drm_gem_vram_bo_driver_evict_flags(struct drm_gem_vram_object *gbo,
546 struct ttm_placement *pl)
548 drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
549 *pl = gbo->placement;
552 static void drm_gem_vram_bo_driver_move_notify(struct drm_gem_vram_object *gbo)
554 struct ttm_buffer_object *bo = &gbo->bo;
555 struct drm_device *dev = bo->base.dev;
557 if (drm_WARN_ON_ONCE(dev, gbo->vmap_use_count))
560 ttm_bo_vunmap(bo, &gbo->map);
561 iosys_map_clear(&gbo->map); /* explicitly clear mapping for next vmap call */
564 static int drm_gem_vram_bo_driver_move(struct drm_gem_vram_object *gbo,
566 struct ttm_operation_ctx *ctx,
567 struct ttm_resource *new_mem)
569 drm_gem_vram_bo_driver_move_notify(gbo);
570 return ttm_bo_move_memcpy(&gbo->bo, ctx, new_mem);
574 * Helpers for struct drm_gem_object_funcs
578 * drm_gem_vram_object_free() - Implements &struct drm_gem_object_funcs.free
579 * @gem: GEM object. Refers to &struct drm_gem_vram_object.gem
581 static void drm_gem_vram_object_free(struct drm_gem_object *gem)
583 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
585 drm_gem_vram_put(gbo);
589 * Helpers for dump buffers
593 * drm_gem_vram_driver_dumb_create() - Implements &struct drm_driver.dumb_create
594 * @file: the DRM file
595 * @dev: the DRM device
596 * @args: the arguments as provided to
597 * &struct drm_driver.dumb_create
599 * This function requires the driver to use @drm_device.vram_mm for its
600 * instance of VRAM MM.
604 * a negative error code otherwise.
606 int drm_gem_vram_driver_dumb_create(struct drm_file *file,
607 struct drm_device *dev,
608 struct drm_mode_create_dumb *args)
610 if (WARN_ONCE(!dev->vram_mm, "VRAM MM not initialized"))
613 return drm_gem_vram_fill_create_dumb(file, dev, 0, 0, args);
615 EXPORT_SYMBOL(drm_gem_vram_driver_dumb_create);
618 * Helpers for struct drm_plane_helper_funcs
621 static void __drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
622 struct drm_plane_state *state,
623 unsigned int num_planes)
625 struct drm_gem_object *obj;
626 struct drm_gem_vram_object *gbo;
627 struct drm_framebuffer *fb = state->fb;
631 obj = drm_gem_fb_get_obj(fb, num_planes);
634 gbo = drm_gem_vram_of_gem(obj);
635 drm_gem_vram_unpin(gbo);
640 * drm_gem_vram_plane_helper_prepare_fb() - Implements &struct
641 * drm_plane_helper_funcs.prepare_fb
642 * @plane: a DRM plane
643 * @new_state: the plane's new state
645 * During plane updates, this function sets the plane's fence and
646 * pins the GEM VRAM objects of the plane's new framebuffer to VRAM.
647 * Call drm_gem_vram_plane_helper_cleanup_fb() to unpin them.
651 * a negative errno code otherwise.
654 drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane,
655 struct drm_plane_state *new_state)
657 struct drm_framebuffer *fb = new_state->fb;
658 struct drm_gem_vram_object *gbo;
659 struct drm_gem_object *obj;
666 for (i = 0; i < fb->format->num_planes; ++i) {
667 obj = drm_gem_fb_get_obj(fb, i);
670 goto err_drm_gem_vram_unpin;
672 gbo = drm_gem_vram_of_gem(obj);
673 ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
675 goto err_drm_gem_vram_unpin;
678 ret = drm_gem_plane_helper_prepare_fb(plane, new_state);
680 goto err_drm_gem_vram_unpin;
684 err_drm_gem_vram_unpin:
685 __drm_gem_vram_plane_helper_cleanup_fb(plane, new_state, i);
688 EXPORT_SYMBOL(drm_gem_vram_plane_helper_prepare_fb);
691 * drm_gem_vram_plane_helper_cleanup_fb() - Implements &struct
692 * drm_plane_helper_funcs.cleanup_fb
693 * @plane: a DRM plane
694 * @old_state: the plane's old state
696 * During plane updates, this function unpins the GEM VRAM
697 * objects of the plane's old framebuffer from VRAM. Complements
698 * drm_gem_vram_plane_helper_prepare_fb().
701 drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
702 struct drm_plane_state *old_state)
704 struct drm_framebuffer *fb = old_state->fb;
709 __drm_gem_vram_plane_helper_cleanup_fb(plane, old_state, fb->format->num_planes);
711 EXPORT_SYMBOL(drm_gem_vram_plane_helper_cleanup_fb);
714 * Helpers for struct drm_simple_display_pipe_funcs
718 * drm_gem_vram_simple_display_pipe_prepare_fb() - Implements &struct
719 * drm_simple_display_pipe_funcs.prepare_fb
720 * @pipe: a simple display pipe
721 * @new_state: the plane's new state
723 * During plane updates, this function pins the GEM VRAM
724 * objects of the plane's new framebuffer to VRAM. Call
725 * drm_gem_vram_simple_display_pipe_cleanup_fb() to unpin them.
729 * a negative errno code otherwise.
731 int drm_gem_vram_simple_display_pipe_prepare_fb(
732 struct drm_simple_display_pipe *pipe,
733 struct drm_plane_state *new_state)
735 return drm_gem_vram_plane_helper_prepare_fb(&pipe->plane, new_state);
737 EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_prepare_fb);
740 * drm_gem_vram_simple_display_pipe_cleanup_fb() - Implements &struct
741 * drm_simple_display_pipe_funcs.cleanup_fb
742 * @pipe: a simple display pipe
743 * @old_state: the plane's old state
745 * During plane updates, this function unpins the GEM VRAM
746 * objects of the plane's old framebuffer from VRAM. Complements
747 * drm_gem_vram_simple_display_pipe_prepare_fb().
749 void drm_gem_vram_simple_display_pipe_cleanup_fb(
750 struct drm_simple_display_pipe *pipe,
751 struct drm_plane_state *old_state)
753 drm_gem_vram_plane_helper_cleanup_fb(&pipe->plane, old_state);
755 EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_cleanup_fb);
762 * drm_gem_vram_object_pin() - Implements &struct drm_gem_object_funcs.pin
763 * @gem: The GEM object to pin
767 * a negative errno code otherwise.
769 static int drm_gem_vram_object_pin(struct drm_gem_object *gem)
771 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
773 /* Fbdev console emulation is the use case of these PRIME
774 * helpers. This may involve updating a hardware buffer from
775 * a shadow FB. We pin the buffer to it's current location
776 * (either video RAM or system memory) to prevent it from
777 * being relocated during the update operation. If you require
778 * the buffer to be pinned to VRAM, implement a callback that
779 * sets the flags accordingly.
781 return drm_gem_vram_pin(gbo, 0);
785 * drm_gem_vram_object_unpin() - Implements &struct drm_gem_object_funcs.unpin
786 * @gem: The GEM object to unpin
788 static void drm_gem_vram_object_unpin(struct drm_gem_object *gem)
790 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
792 drm_gem_vram_unpin(gbo);
796 * drm_gem_vram_object_vmap() -
797 * Implements &struct drm_gem_object_funcs.vmap
798 * @gem: The GEM object to map
799 * @map: Returns the kernel virtual address of the VRAM GEM object's backing
803 * 0 on success, or a negative error code otherwise.
805 static int drm_gem_vram_object_vmap(struct drm_gem_object *gem,
806 struct iosys_map *map)
808 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
810 return drm_gem_vram_vmap(gbo, map);
814 * drm_gem_vram_object_vunmap() -
815 * Implements &struct drm_gem_object_funcs.vunmap
816 * @gem: The GEM object to unmap
817 * @map: Kernel virtual address where the VRAM GEM object was mapped
819 static void drm_gem_vram_object_vunmap(struct drm_gem_object *gem,
820 struct iosys_map *map)
822 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
824 drm_gem_vram_vunmap(gbo, map);
831 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs = {
832 .free = drm_gem_vram_object_free,
833 .pin = drm_gem_vram_object_pin,
834 .unpin = drm_gem_vram_object_unpin,
835 .vmap = drm_gem_vram_object_vmap,
836 .vunmap = drm_gem_vram_object_vunmap,
837 .mmap = drm_gem_ttm_mmap,
838 .print_info = drm_gem_ttm_print_info,
842 * VRAM memory manager
849 static void bo_driver_ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *tt)
859 static struct ttm_tt *bo_driver_ttm_tt_create(struct ttm_buffer_object *bo,
865 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
869 ret = ttm_tt_init(tt, bo, page_flags, ttm_cached, 0);
871 goto err_ttm_tt_init;
880 static void bo_driver_evict_flags(struct ttm_buffer_object *bo,
881 struct ttm_placement *placement)
883 struct drm_gem_vram_object *gbo;
885 /* TTM may pass BOs that are not GEM VRAM BOs. */
886 if (!drm_is_gem_vram(bo))
889 gbo = drm_gem_vram_of_bo(bo);
891 drm_gem_vram_bo_driver_evict_flags(gbo, placement);
894 static void bo_driver_delete_mem_notify(struct ttm_buffer_object *bo)
896 struct drm_gem_vram_object *gbo;
898 /* TTM may pass BOs that are not GEM VRAM BOs. */
899 if (!drm_is_gem_vram(bo))
902 gbo = drm_gem_vram_of_bo(bo);
904 drm_gem_vram_bo_driver_move_notify(gbo);
907 static int bo_driver_move(struct ttm_buffer_object *bo,
909 struct ttm_operation_ctx *ctx,
910 struct ttm_resource *new_mem,
911 struct ttm_place *hop)
913 struct drm_gem_vram_object *gbo;
916 if (new_mem->mem_type != TTM_PL_SYSTEM) {
917 hop->mem_type = TTM_PL_SYSTEM;
918 hop->flags = TTM_PL_FLAG_TEMPORARY;
922 ttm_bo_move_null(bo, new_mem);
926 gbo = drm_gem_vram_of_bo(bo);
928 return drm_gem_vram_bo_driver_move(gbo, evict, ctx, new_mem);
931 static int bo_driver_io_mem_reserve(struct ttm_device *bdev,
932 struct ttm_resource *mem)
934 struct drm_vram_mm *vmm = drm_vram_mm_of_bdev(bdev);
936 switch (mem->mem_type) {
937 case TTM_PL_SYSTEM: /* nothing to do */
940 mem->bus.offset = (mem->start << PAGE_SHIFT) + vmm->vram_base;
941 mem->bus.is_iomem = true;
942 mem->bus.caching = ttm_write_combined;
951 static struct ttm_device_funcs bo_driver = {
952 .ttm_tt_create = bo_driver_ttm_tt_create,
953 .ttm_tt_destroy = bo_driver_ttm_tt_destroy,
954 .eviction_valuable = ttm_bo_eviction_valuable,
955 .evict_flags = bo_driver_evict_flags,
956 .move = bo_driver_move,
957 .delete_mem_notify = bo_driver_delete_mem_notify,
958 .io_mem_reserve = bo_driver_io_mem_reserve,
965 static int drm_vram_mm_debugfs(struct seq_file *m, void *data)
967 struct drm_debugfs_entry *entry = m->private;
968 struct drm_vram_mm *vmm = entry->dev->vram_mm;
969 struct ttm_resource_manager *man = ttm_manager_type(&vmm->bdev, TTM_PL_VRAM);
970 struct drm_printer p = drm_seq_file_printer(m);
972 ttm_resource_manager_debug(man, &p);
976 static const struct drm_debugfs_info drm_vram_mm_debugfs_list[] = {
977 { "vram-mm", drm_vram_mm_debugfs, 0, NULL },
981 * drm_vram_mm_debugfs_init() - Register VRAM MM debugfs file.
983 * @minor: drm minor device.
986 void drm_vram_mm_debugfs_init(struct drm_minor *minor)
988 drm_debugfs_add_files(minor->dev, drm_vram_mm_debugfs_list,
989 ARRAY_SIZE(drm_vram_mm_debugfs_list));
991 EXPORT_SYMBOL(drm_vram_mm_debugfs_init);
993 static int drm_vram_mm_init(struct drm_vram_mm *vmm, struct drm_device *dev,
994 uint64_t vram_base, size_t vram_size)
998 vmm->vram_base = vram_base;
999 vmm->vram_size = vram_size;
1001 ret = ttm_device_init(&vmm->bdev, &bo_driver, dev->dev,
1002 dev->anon_inode->i_mapping,
1003 dev->vma_offset_manager,
1008 ret = ttm_range_man_init(&vmm->bdev, TTM_PL_VRAM,
1009 false, vram_size >> PAGE_SHIFT);
1016 static void drm_vram_mm_cleanup(struct drm_vram_mm *vmm)
1018 ttm_range_man_fini(&vmm->bdev, TTM_PL_VRAM);
1019 ttm_device_fini(&vmm->bdev);
1023 * Helpers for integration with struct drm_device
1026 static struct drm_vram_mm *drm_vram_helper_alloc_mm(struct drm_device *dev, uint64_t vram_base,
1031 if (WARN_ON(dev->vram_mm))
1032 return dev->vram_mm;
1034 dev->vram_mm = kzalloc(sizeof(*dev->vram_mm), GFP_KERNEL);
1036 return ERR_PTR(-ENOMEM);
1038 ret = drm_vram_mm_init(dev->vram_mm, dev, vram_base, vram_size);
1042 return dev->vram_mm;
1045 kfree(dev->vram_mm);
1046 dev->vram_mm = NULL;
1047 return ERR_PTR(ret);
1050 static void drm_vram_helper_release_mm(struct drm_device *dev)
1055 drm_vram_mm_cleanup(dev->vram_mm);
1056 kfree(dev->vram_mm);
1057 dev->vram_mm = NULL;
1060 static void drm_vram_mm_release(struct drm_device *dev, void *ptr)
1062 drm_vram_helper_release_mm(dev);
1066 * drmm_vram_helper_init - Initializes a device's instance of
1067 * &struct drm_vram_mm
1068 * @dev: the DRM device
1069 * @vram_base: the base address of the video memory
1070 * @vram_size: the size of the video memory in bytes
1072 * Creates a new instance of &struct drm_vram_mm and stores it in
1073 * struct &drm_device.vram_mm. The instance is auto-managed and cleaned
1074 * up as part of device cleanup. Calling this function multiple times
1075 * will generate an error message.
1078 * 0 on success, or a negative errno code otherwise.
1080 int drmm_vram_helper_init(struct drm_device *dev, uint64_t vram_base,
1083 struct drm_vram_mm *vram_mm;
1085 if (drm_WARN_ON_ONCE(dev, dev->vram_mm))
1088 vram_mm = drm_vram_helper_alloc_mm(dev, vram_base, vram_size);
1089 if (IS_ERR(vram_mm))
1090 return PTR_ERR(vram_mm);
1091 return drmm_add_action_or_reset(dev, drm_vram_mm_release, NULL);
1093 EXPORT_SYMBOL(drmm_vram_helper_init);
1096 * Mode-config helpers
1099 static enum drm_mode_status
1100 drm_vram_helper_mode_valid_internal(struct drm_device *dev,
1101 const struct drm_display_mode *mode,
1102 unsigned long max_bpp)
1104 struct drm_vram_mm *vmm = dev->vram_mm;
1105 unsigned long fbsize, fbpages, max_fbpages;
1107 if (WARN_ON(!dev->vram_mm))
1110 max_fbpages = (vmm->vram_size / 2) >> PAGE_SHIFT;
1112 fbsize = mode->hdisplay * mode->vdisplay * max_bpp;
1113 fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE);
1115 if (fbpages > max_fbpages)
1122 * drm_vram_helper_mode_valid - Tests if a display mode's
1123 * framebuffer fits into the available video memory.
1124 * @dev: the DRM device
1125 * @mode: the mode to test
1127 * This function tests if enough video memory is available for using the
1128 * specified display mode. Atomic modesetting requires importing the
1129 * designated framebuffer into video memory before evicting the active
1130 * one. Hence, any framebuffer may consume at most half of the available
1131 * VRAM. Display modes that require a larger framebuffer can not be used,
1132 * even if the CRTC does support them. Each framebuffer is assumed to
1133 * have 32-bit color depth.
1136 * The function can only test if the display mode is supported in
1137 * general. If there are too many framebuffers pinned to video memory,
1138 * a display mode may still not be usable in practice. The color depth of
1139 * 32-bit fits all current use case. A more flexible test can be added
1143 * MODE_OK if the display mode is supported, or an error code of type
1144 * enum drm_mode_status otherwise.
1146 enum drm_mode_status
1147 drm_vram_helper_mode_valid(struct drm_device *dev,
1148 const struct drm_display_mode *mode)
1150 static const unsigned long max_bpp = 4; /* DRM_FORMAT_XRGB8888 */
1152 return drm_vram_helper_mode_valid_internal(dev, mode, max_bpp);
1154 EXPORT_SYMBOL(drm_vram_helper_mode_valid);
1156 MODULE_DESCRIPTION("DRM VRAM memory-management helpers");
1157 MODULE_LICENSE("GPL");