1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/module.h>
5 #include <drm/drm_debugfs.h>
6 #include <drm/drm_device.h>
7 #include <drm/drm_drv.h>
8 #include <drm/drm_file.h>
9 #include <drm/drm_framebuffer.h>
10 #include <drm/drm_gem_framebuffer_helper.h>
11 #include <drm/drm_gem_ttm_helper.h>
12 #include <drm/drm_gem_vram_helper.h>
13 #include <drm/drm_managed.h>
14 #include <drm/drm_mode.h>
15 #include <drm/drm_plane.h>
16 #include <drm/drm_prime.h>
17 #include <drm/drm_simple_kms_helper.h>
19 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs;
24 * This library provides &struct drm_gem_vram_object (GEM VRAM), a GEM
25 * buffer object that is backed by video RAM (VRAM). It can be used for
26 * framebuffer devices with dedicated memory.
28 * The data structure &struct drm_vram_mm and its helpers implement a memory
29 * manager for simple framebuffer devices with dedicated video memory. GEM
30 * VRAM buffer objects are either placed in the video memory or remain evicted
33 * With the GEM interface userspace applications create, manage and destroy
34 * graphics buffers, such as an on-screen framebuffer. GEM does not provide
35 * an implementation of these interfaces. It's up to the DRM driver to
36 * provide an implementation that suits the hardware. If the hardware device
37 * contains dedicated video memory, the DRM driver can use the VRAM helper
38 * library. Each active buffer object is stored in video RAM. Active
39 * buffer are used for drawing the current frame, typically something like
40 * the frame's scanout buffer or the cursor image. If there's no more space
41 * left in VRAM, inactive GEM objects can be moved to system memory.
43 * To initialize the VRAM helper library call drmm_vram_helper_alloc_mm().
44 * The function allocates and initializes an instance of &struct drm_vram_mm
45 * in &struct drm_device.vram_mm . Use &DRM_GEM_VRAM_DRIVER to initialize
46 * &struct drm_driver and &DRM_VRAM_MM_FILE_OPERATIONS to initialize
47 * &struct file_operations; as illustrated below.
51 * struct file_operations fops ={
52 * .owner = THIS_MODULE,
53 * DRM_VRAM_MM_FILE_OPERATION
55 * struct drm_driver drv = {
56 * .driver_feature = DRM_ ... ,
61 * int init_drm_driver()
63 * struct drm_device *dev;
65 * unsigned long vram_size;
68 * // setup device, vram base and size
71 * ret = drmm_vram_helper_alloc_mm(dev, vram_base, vram_size);
77 * This creates an instance of &struct drm_vram_mm, exports DRM userspace
78 * interfaces for GEM buffer management and initializes file operations to
79 * allow for accessing created GEM buffers. With this setup, the DRM driver
80 * manages an area of video RAM with VRAM MM and provides GEM VRAM objects
83 * You don't have to clean up the instance of VRAM MM.
84 * drmm_vram_helper_alloc_mm() is a managed interface that installs a
85 * clean-up handler to run during the DRM device's release.
87 * For drawing or scanout operations, rsp. buffer objects have to be pinned
88 * in video RAM. Call drm_gem_vram_pin() with &DRM_GEM_VRAM_PL_FLAG_VRAM or
89 * &DRM_GEM_VRAM_PL_FLAG_SYSTEM to pin a buffer object in video RAM or system
90 * memory. Call drm_gem_vram_unpin() to release the pinned object afterwards.
92 * A buffer object that is pinned in video RAM has a fixed address within that
93 * memory region. Call drm_gem_vram_offset() to retrieve this value. Typically
94 * it's used to program the hardware's scanout engine for framebuffers, set
95 * the cursor overlay's image for a mouse cursor, or use it as input to the
96 * hardware's draing engine.
98 * To access a buffer object's memory from the DRM driver, call
99 * drm_gem_vram_vmap(). It maps the buffer into kernel address
100 * space and returns the memory address. Use drm_gem_vram_vunmap() to
101 * release the mapping.
105 * Buffer-objects helpers
108 static void drm_gem_vram_cleanup(struct drm_gem_vram_object *gbo)
110 /* We got here via ttm_bo_put(), which means that the
111 * TTM buffer object in 'bo' has already been cleaned
112 * up; only release the GEM object.
115 WARN_ON(gbo->kmap_use_count);
116 WARN_ON(gbo->kmap.virtual);
118 drm_gem_object_release(&gbo->bo.base);
121 static void drm_gem_vram_destroy(struct drm_gem_vram_object *gbo)
123 drm_gem_vram_cleanup(gbo);
127 static void ttm_buffer_object_destroy(struct ttm_buffer_object *bo)
129 struct drm_gem_vram_object *gbo = drm_gem_vram_of_bo(bo);
131 drm_gem_vram_destroy(gbo);
134 static void drm_gem_vram_placement(struct drm_gem_vram_object *gbo,
135 unsigned long pl_flag)
137 u32 invariant_flags = 0;
141 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_TOPDOWN)
142 invariant_flags = TTM_PL_FLAG_TOPDOWN;
144 gbo->placement.placement = gbo->placements;
145 gbo->placement.busy_placement = gbo->placements;
147 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_VRAM) {
148 gbo->placements[c].mem_type = TTM_PL_VRAM;
149 gbo->placements[c++].flags = invariant_flags;
152 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_SYSTEM || !c) {
153 gbo->placements[c].mem_type = TTM_PL_SYSTEM;
154 gbo->placements[c++].flags = invariant_flags;
157 gbo->placement.num_placement = c;
158 gbo->placement.num_busy_placement = c;
160 for (i = 0; i < c; ++i) {
161 gbo->placements[i].fpfn = 0;
162 gbo->placements[i].lpfn = 0;
167 * drm_gem_vram_create() - Creates a VRAM-backed GEM object
168 * @dev: the DRM device
169 * @size: the buffer size in bytes
170 * @pg_align: the buffer's alignment in multiples of the page size
172 * GEM objects are allocated by calling struct drm_driver.gem_create_object,
173 * if set. Otherwise kzalloc() will be used. Drivers can set their own GEM
174 * object functions in struct drm_driver.gem_create_object. If no functions
175 * are set, the new GEM object will use the default functions from GEM VRAM
179 * A new instance of &struct drm_gem_vram_object on success, or
180 * an ERR_PTR()-encoded error code otherwise.
182 struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
184 unsigned long pg_align)
186 struct drm_gem_vram_object *gbo;
187 struct drm_gem_object *gem;
188 struct drm_vram_mm *vmm = dev->vram_mm;
189 struct ttm_bo_device *bdev;
193 if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
194 return ERR_PTR(-EINVAL);
196 if (dev->driver->gem_create_object) {
197 gem = dev->driver->gem_create_object(dev, size);
199 return ERR_PTR(-ENOMEM);
200 gbo = drm_gem_vram_of_gem(gem);
202 gbo = kzalloc(sizeof(*gbo), GFP_KERNEL);
204 return ERR_PTR(-ENOMEM);
209 gem->funcs = &drm_gem_vram_object_funcs;
211 ret = drm_gem_object_init(dev, gem, size);
218 acc_size = ttm_bo_dma_acc_size(bdev, size, sizeof(*gbo));
221 drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
224 * A failing ttm_bo_init will call ttm_buffer_object_destroy
225 * to release gbo->bo.base and kfree gbo.
227 ret = ttm_bo_init(bdev, &gbo->bo, size, ttm_bo_type_device,
228 &gbo->placement, pg_align, false, acc_size,
229 NULL, NULL, ttm_buffer_object_destroy);
235 EXPORT_SYMBOL(drm_gem_vram_create);
238 * drm_gem_vram_put() - Releases a reference to a VRAM-backed GEM object
239 * @gbo: the GEM VRAM object
241 * See ttm_bo_put() for more information.
243 void drm_gem_vram_put(struct drm_gem_vram_object *gbo)
245 ttm_bo_put(&gbo->bo);
247 EXPORT_SYMBOL(drm_gem_vram_put);
250 * drm_gem_vram_mmap_offset() - Returns a GEM VRAM object's mmap offset
251 * @gbo: the GEM VRAM object
253 * See drm_vma_node_offset_addr() for more information.
256 * The buffer object's offset for userspace mappings on success, or
257 * 0 if no offset is allocated.
259 u64 drm_gem_vram_mmap_offset(struct drm_gem_vram_object *gbo)
261 return drm_vma_node_offset_addr(&gbo->bo.base.vma_node);
263 EXPORT_SYMBOL(drm_gem_vram_mmap_offset);
265 static u64 drm_gem_vram_pg_offset(struct drm_gem_vram_object *gbo)
267 /* Keep TTM behavior for now, remove when drivers are audited */
268 if (WARN_ON_ONCE(!gbo->bo.mem.mm_node))
271 return gbo->bo.mem.start;
275 * drm_gem_vram_offset() - \
276 Returns a GEM VRAM object's offset in video memory
277 * @gbo: the GEM VRAM object
279 * This function returns the buffer object's offset in the device's video
280 * memory. The buffer object has to be pinned to %TTM_PL_VRAM.
283 * The buffer object's offset in video memory on success, or
284 * a negative errno code otherwise.
286 s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo)
288 if (WARN_ON_ONCE(!gbo->bo.pin_count))
290 return drm_gem_vram_pg_offset(gbo) << PAGE_SHIFT;
292 EXPORT_SYMBOL(drm_gem_vram_offset);
294 static int drm_gem_vram_pin_locked(struct drm_gem_vram_object *gbo,
295 unsigned long pl_flag)
297 struct ttm_operation_ctx ctx = { false, false };
300 if (gbo->bo.pin_count)
304 drm_gem_vram_placement(gbo, pl_flag);
306 ret = ttm_bo_validate(&gbo->bo, &gbo->placement, &ctx);
311 ttm_bo_pin(&gbo->bo);
317 * drm_gem_vram_pin() - Pins a GEM VRAM object in a region.
318 * @gbo: the GEM VRAM object
319 * @pl_flag: a bitmask of possible memory regions
321 * Pinning a buffer object ensures that it is not evicted from
322 * a memory region. A pinned buffer object has to be unpinned before
323 * it can be pinned to another region. If the pl_flag argument is 0,
324 * the buffer is pinned at its current location (video RAM or system
327 * Small buffer objects, such as cursor images, can lead to memory
328 * fragmentation if they are pinned in the middle of video RAM. This
329 * is especially a problem on devices with only a small amount of
330 * video RAM. Fragmentation can prevent the primary framebuffer from
331 * fitting in, even though there's enough memory overall. The modifier
332 * DRM_GEM_VRAM_PL_FLAG_TOPDOWN marks the buffer object to be pinned
333 * at the high end of the memory region to avoid fragmentation.
337 * a negative error code otherwise.
339 int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag)
343 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
346 ret = drm_gem_vram_pin_locked(gbo, pl_flag);
347 ttm_bo_unreserve(&gbo->bo);
351 EXPORT_SYMBOL(drm_gem_vram_pin);
353 static void drm_gem_vram_unpin_locked(struct drm_gem_vram_object *gbo)
355 ttm_bo_unpin(&gbo->bo);
359 * drm_gem_vram_unpin() - Unpins a GEM VRAM object
360 * @gbo: the GEM VRAM object
364 * a negative error code otherwise.
366 int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo)
370 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
374 drm_gem_vram_unpin_locked(gbo);
375 ttm_bo_unreserve(&gbo->bo);
379 EXPORT_SYMBOL(drm_gem_vram_unpin);
381 static void *drm_gem_vram_kmap_locked(struct drm_gem_vram_object *gbo,
382 bool map, bool *is_iomem)
385 struct ttm_bo_kmap_obj *kmap = &gbo->kmap;
387 if (gbo->kmap_use_count > 0)
390 if (kmap->virtual || !map)
393 ret = ttm_bo_kmap(&gbo->bo, 0, gbo->bo.num_pages, kmap);
398 if (!kmap->virtual) {
401 return NULL; /* not mapped; don't increment ref */
403 ++gbo->kmap_use_count;
405 return ttm_kmap_obj_virtual(kmap, is_iomem);
406 return kmap->virtual;
409 static void drm_gem_vram_kunmap_locked(struct drm_gem_vram_object *gbo)
411 if (WARN_ON_ONCE(!gbo->kmap_use_count))
413 if (--gbo->kmap_use_count > 0)
417 * Permanently mapping and unmapping buffers adds overhead from
418 * updating the page tables and creates debugging output. Therefore,
419 * we delay the actual unmap operation until the BO gets evicted
420 * from memory. See drm_gem_vram_bo_driver_move_notify().
425 * drm_gem_vram_vmap() - Pins and maps a GEM VRAM object into kernel address
427 * @gbo: The GEM VRAM object to map
429 * The vmap function pins a GEM VRAM object to its current location, either
430 * system or video memory, and maps its buffer into kernel address space.
431 * As pinned object cannot be relocated, you should avoid pinning objects
432 * permanently. Call drm_gem_vram_vunmap() with the returned address to
433 * unmap and unpin the GEM VRAM object.
436 * The buffer's virtual address on success, or
437 * an ERR_PTR()-encoded error code otherwise.
439 void *drm_gem_vram_vmap(struct drm_gem_vram_object *gbo)
444 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
448 ret = drm_gem_vram_pin_locked(gbo, 0);
450 goto err_ttm_bo_unreserve;
451 base = drm_gem_vram_kmap_locked(gbo, true, NULL);
454 goto err_drm_gem_vram_unpin_locked;
457 ttm_bo_unreserve(&gbo->bo);
461 err_drm_gem_vram_unpin_locked:
462 drm_gem_vram_unpin_locked(gbo);
463 err_ttm_bo_unreserve:
464 ttm_bo_unreserve(&gbo->bo);
467 EXPORT_SYMBOL(drm_gem_vram_vmap);
470 * drm_gem_vram_vunmap() - Unmaps and unpins a GEM VRAM object
471 * @gbo: The GEM VRAM object to unmap
472 * @vaddr: The mapping's base address as returned by drm_gem_vram_vmap()
474 * A call to drm_gem_vram_vunmap() unmaps and unpins a GEM VRAM buffer. See
475 * the documentation for drm_gem_vram_vmap() for more information.
477 void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo, void *vaddr)
481 ret = ttm_bo_reserve(&gbo->bo, false, false, NULL);
482 if (WARN_ONCE(ret, "ttm_bo_reserve_failed(): ret=%d\n", ret))
485 drm_gem_vram_kunmap_locked(gbo);
486 drm_gem_vram_unpin_locked(gbo);
488 ttm_bo_unreserve(&gbo->bo);
490 EXPORT_SYMBOL(drm_gem_vram_vunmap);
493 * drm_gem_vram_fill_create_dumb() - \
494 Helper for implementing &struct drm_driver.dumb_create
495 * @file: the DRM file
496 * @dev: the DRM device
497 * @pg_align: the buffer's alignment in multiples of the page size
498 * @pitch_align: the scanline's alignment in powers of 2
499 * @args: the arguments as provided to \
500 &struct drm_driver.dumb_create
502 * This helper function fills &struct drm_mode_create_dumb, which is used
503 * by &struct drm_driver.dumb_create. Implementations of this interface
504 * should forwards their arguments to this helper, plus the driver-specific
509 * a negative error code otherwise.
511 int drm_gem_vram_fill_create_dumb(struct drm_file *file,
512 struct drm_device *dev,
513 unsigned long pg_align,
514 unsigned long pitch_align,
515 struct drm_mode_create_dumb *args)
518 struct drm_gem_vram_object *gbo;
522 pitch = args->width * DIV_ROUND_UP(args->bpp, 8);
524 if (WARN_ON_ONCE(!is_power_of_2(pitch_align)))
526 pitch = ALIGN(pitch, pitch_align);
528 size = pitch * args->height;
530 size = roundup(size, PAGE_SIZE);
534 gbo = drm_gem_vram_create(dev, size, pg_align);
538 ret = drm_gem_handle_create(file, &gbo->bo.base, &handle);
540 goto err_drm_gem_object_put;
542 drm_gem_object_put(&gbo->bo.base);
546 args->handle = handle;
550 err_drm_gem_object_put:
551 drm_gem_object_put(&gbo->bo.base);
554 EXPORT_SYMBOL(drm_gem_vram_fill_create_dumb);
557 * Helpers for struct ttm_bo_driver
560 static bool drm_is_gem_vram(struct ttm_buffer_object *bo)
562 return (bo->destroy == ttm_buffer_object_destroy);
565 static void drm_gem_vram_bo_driver_evict_flags(struct drm_gem_vram_object *gbo,
566 struct ttm_placement *pl)
568 drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
569 *pl = gbo->placement;
572 static void drm_gem_vram_bo_driver_move_notify(struct drm_gem_vram_object *gbo,
574 struct ttm_resource *new_mem)
576 struct ttm_bo_kmap_obj *kmap = &gbo->kmap;
578 if (WARN_ON_ONCE(gbo->kmap_use_count))
584 kmap->virtual = NULL;
587 static int drm_gem_vram_bo_driver_move(struct drm_gem_vram_object *gbo,
589 struct ttm_operation_ctx *ctx,
590 struct ttm_resource *new_mem)
594 drm_gem_vram_bo_driver_move_notify(gbo, evict, new_mem);
595 ret = ttm_bo_move_memcpy(&gbo->bo, ctx, new_mem);
597 swap(*new_mem, gbo->bo.mem);
598 drm_gem_vram_bo_driver_move_notify(gbo, false, new_mem);
599 swap(*new_mem, gbo->bo.mem);
605 * Helpers for struct drm_gem_object_funcs
609 * drm_gem_vram_object_free() - \
610 Implements &struct drm_gem_object_funcs.free
611 * @gem: GEM object. Refers to &struct drm_gem_vram_object.gem
613 static void drm_gem_vram_object_free(struct drm_gem_object *gem)
615 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
617 drm_gem_vram_put(gbo);
621 * Helpers for dump buffers
625 * drm_gem_vram_driver_create_dumb() - \
626 Implements &struct drm_driver.dumb_create
627 * @file: the DRM file
628 * @dev: the DRM device
629 * @args: the arguments as provided to \
630 &struct drm_driver.dumb_create
632 * This function requires the driver to use @drm_device.vram_mm for its
633 * instance of VRAM MM.
637 * a negative error code otherwise.
639 int drm_gem_vram_driver_dumb_create(struct drm_file *file,
640 struct drm_device *dev,
641 struct drm_mode_create_dumb *args)
643 if (WARN_ONCE(!dev->vram_mm, "VRAM MM not initialized"))
646 return drm_gem_vram_fill_create_dumb(file, dev, 0, 0, args);
648 EXPORT_SYMBOL(drm_gem_vram_driver_dumb_create);
651 * drm_gem_vram_driver_dumb_mmap_offset() - \
652 Implements &struct drm_driver.dumb_mmap_offset
653 * @file: DRM file pointer.
655 * @handle: GEM handle
656 * @offset: Returns the mapping's memory offset on success
660 * a negative errno code otherwise.
662 int drm_gem_vram_driver_dumb_mmap_offset(struct drm_file *file,
663 struct drm_device *dev,
664 uint32_t handle, uint64_t *offset)
666 struct drm_gem_object *gem;
667 struct drm_gem_vram_object *gbo;
669 gem = drm_gem_object_lookup(file, handle);
673 gbo = drm_gem_vram_of_gem(gem);
674 *offset = drm_gem_vram_mmap_offset(gbo);
676 drm_gem_object_put(gem);
680 EXPORT_SYMBOL(drm_gem_vram_driver_dumb_mmap_offset);
683 * Helpers for struct drm_plane_helper_funcs
687 * drm_gem_vram_plane_helper_prepare_fb() - \
688 * Implements &struct drm_plane_helper_funcs.prepare_fb
689 * @plane: a DRM plane
690 * @new_state: the plane's new state
692 * During plane updates, this function sets the plane's fence and
693 * pins the GEM VRAM objects of the plane's new framebuffer to VRAM.
694 * Call drm_gem_vram_plane_helper_cleanup_fb() to unpin them.
698 * a negative errno code otherwise.
701 drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane,
702 struct drm_plane_state *new_state)
705 struct drm_gem_vram_object *gbo;
711 for (i = 0; i < ARRAY_SIZE(new_state->fb->obj); ++i) {
712 if (!new_state->fb->obj[i])
714 gbo = drm_gem_vram_of_gem(new_state->fb->obj[i]);
715 ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
717 goto err_drm_gem_vram_unpin;
720 ret = drm_gem_fb_prepare_fb(plane, new_state);
722 goto err_drm_gem_vram_unpin;
726 err_drm_gem_vram_unpin:
729 gbo = drm_gem_vram_of_gem(new_state->fb->obj[i]);
730 drm_gem_vram_unpin(gbo);
734 EXPORT_SYMBOL(drm_gem_vram_plane_helper_prepare_fb);
737 * drm_gem_vram_plane_helper_cleanup_fb() - \
738 * Implements &struct drm_plane_helper_funcs.cleanup_fb
739 * @plane: a DRM plane
740 * @old_state: the plane's old state
742 * During plane updates, this function unpins the GEM VRAM
743 * objects of the plane's old framebuffer from VRAM. Complements
744 * drm_gem_vram_plane_helper_prepare_fb().
747 drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
748 struct drm_plane_state *old_state)
751 struct drm_gem_vram_object *gbo;
756 for (i = 0; i < ARRAY_SIZE(old_state->fb->obj); ++i) {
757 if (!old_state->fb->obj[i])
759 gbo = drm_gem_vram_of_gem(old_state->fb->obj[i]);
760 drm_gem_vram_unpin(gbo);
763 EXPORT_SYMBOL(drm_gem_vram_plane_helper_cleanup_fb);
766 * Helpers for struct drm_simple_display_pipe_funcs
770 * drm_gem_vram_simple_display_pipe_prepare_fb() - \
771 * Implements &struct drm_simple_display_pipe_funcs.prepare_fb
772 * @pipe: a simple display pipe
773 * @new_state: the plane's new state
775 * During plane updates, this function pins the GEM VRAM
776 * objects of the plane's new framebuffer to VRAM. Call
777 * drm_gem_vram_simple_display_pipe_cleanup_fb() to unpin them.
781 * a negative errno code otherwise.
783 int drm_gem_vram_simple_display_pipe_prepare_fb(
784 struct drm_simple_display_pipe *pipe,
785 struct drm_plane_state *new_state)
787 return drm_gem_vram_plane_helper_prepare_fb(&pipe->plane, new_state);
789 EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_prepare_fb);
792 * drm_gem_vram_simple_display_pipe_cleanup_fb() - \
793 * Implements &struct drm_simple_display_pipe_funcs.cleanup_fb
794 * @pipe: a simple display pipe
795 * @old_state: the plane's old state
797 * During plane updates, this function unpins the GEM VRAM
798 * objects of the plane's old framebuffer from VRAM. Complements
799 * drm_gem_vram_simple_display_pipe_prepare_fb().
801 void drm_gem_vram_simple_display_pipe_cleanup_fb(
802 struct drm_simple_display_pipe *pipe,
803 struct drm_plane_state *old_state)
805 drm_gem_vram_plane_helper_cleanup_fb(&pipe->plane, old_state);
807 EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_cleanup_fb);
814 * drm_gem_vram_object_pin() - \
815 Implements &struct drm_gem_object_funcs.pin
816 * @gem: The GEM object to pin
820 * a negative errno code otherwise.
822 static int drm_gem_vram_object_pin(struct drm_gem_object *gem)
824 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
826 /* Fbdev console emulation is the use case of these PRIME
827 * helpers. This may involve updating a hardware buffer from
828 * a shadow FB. We pin the buffer to it's current location
829 * (either video RAM or system memory) to prevent it from
830 * being relocated during the update operation. If you require
831 * the buffer to be pinned to VRAM, implement a callback that
832 * sets the flags accordingly.
834 return drm_gem_vram_pin(gbo, 0);
838 * drm_gem_vram_object_unpin() - \
839 Implements &struct drm_gem_object_funcs.unpin
840 * @gem: The GEM object to unpin
842 static void drm_gem_vram_object_unpin(struct drm_gem_object *gem)
844 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
846 drm_gem_vram_unpin(gbo);
850 * drm_gem_vram_object_vmap() - \
851 Implements &struct drm_gem_object_funcs.vmap
852 * @gem: The GEM object to map
855 * The buffers virtual address on success, or
858 static void *drm_gem_vram_object_vmap(struct drm_gem_object *gem)
860 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
863 base = drm_gem_vram_vmap(gbo);
870 * drm_gem_vram_object_vunmap() - \
871 Implements &struct drm_gem_object_funcs.vunmap
872 * @gem: The GEM object to unmap
873 * @vaddr: The mapping's base address
875 static void drm_gem_vram_object_vunmap(struct drm_gem_object *gem,
878 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
880 drm_gem_vram_vunmap(gbo, vaddr);
887 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs = {
888 .free = drm_gem_vram_object_free,
889 .pin = drm_gem_vram_object_pin,
890 .unpin = drm_gem_vram_object_unpin,
891 .vmap = drm_gem_vram_object_vmap,
892 .vunmap = drm_gem_vram_object_vunmap,
893 .mmap = drm_gem_ttm_mmap,
894 .print_info = drm_gem_ttm_print_info,
898 * VRAM memory manager
905 static void bo_driver_ttm_tt_destroy(struct ttm_bo_device *bdev, struct ttm_tt *tt)
907 ttm_tt_destroy_common(bdev, tt);
916 static struct ttm_tt *bo_driver_ttm_tt_create(struct ttm_buffer_object *bo,
922 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
926 ret = ttm_tt_init(tt, bo, page_flags, ttm_cached);
928 goto err_ttm_tt_init;
937 static void bo_driver_evict_flags(struct ttm_buffer_object *bo,
938 struct ttm_placement *placement)
940 struct drm_gem_vram_object *gbo;
942 /* TTM may pass BOs that are not GEM VRAM BOs. */
943 if (!drm_is_gem_vram(bo))
946 gbo = drm_gem_vram_of_bo(bo);
948 drm_gem_vram_bo_driver_evict_flags(gbo, placement);
951 static void bo_driver_delete_mem_notify(struct ttm_buffer_object *bo)
953 struct drm_gem_vram_object *gbo;
955 /* TTM may pass BOs that are not GEM VRAM BOs. */
956 if (!drm_is_gem_vram(bo))
959 gbo = drm_gem_vram_of_bo(bo);
961 drm_gem_vram_bo_driver_move_notify(gbo, false, NULL);
964 static int bo_driver_move(struct ttm_buffer_object *bo,
966 struct ttm_operation_ctx *ctx,
967 struct ttm_resource *new_mem)
969 struct drm_gem_vram_object *gbo;
971 gbo = drm_gem_vram_of_bo(bo);
973 return drm_gem_vram_bo_driver_move(gbo, evict, ctx, new_mem);
976 static int bo_driver_io_mem_reserve(struct ttm_bo_device *bdev,
977 struct ttm_resource *mem)
979 struct drm_vram_mm *vmm = drm_vram_mm_of_bdev(bdev);
981 switch (mem->mem_type) {
982 case TTM_PL_SYSTEM: /* nothing to do */
985 mem->bus.offset = (mem->start << PAGE_SHIFT) + vmm->vram_base;
986 mem->bus.is_iomem = true;
987 mem->bus.caching = ttm_write_combined;
996 static struct ttm_bo_driver bo_driver = {
997 .ttm_tt_create = bo_driver_ttm_tt_create,
998 .ttm_tt_destroy = bo_driver_ttm_tt_destroy,
999 .eviction_valuable = ttm_bo_eviction_valuable,
1000 .evict_flags = bo_driver_evict_flags,
1001 .move = bo_driver_move,
1002 .delete_mem_notify = bo_driver_delete_mem_notify,
1003 .io_mem_reserve = bo_driver_io_mem_reserve,
1007 * struct drm_vram_mm
1010 static int drm_vram_mm_debugfs(struct seq_file *m, void *data)
1012 struct drm_info_node *node = (struct drm_info_node *) m->private;
1013 struct drm_vram_mm *vmm = node->minor->dev->vram_mm;
1014 struct ttm_resource_manager *man = ttm_manager_type(&vmm->bdev, TTM_PL_VRAM);
1015 struct drm_printer p = drm_seq_file_printer(m);
1017 ttm_resource_manager_debug(man, &p);
1021 static const struct drm_info_list drm_vram_mm_debugfs_list[] = {
1022 { "vram-mm", drm_vram_mm_debugfs, 0, NULL },
1026 * drm_vram_mm_debugfs_init() - Register VRAM MM debugfs file.
1028 * @minor: drm minor device.
1031 void drm_vram_mm_debugfs_init(struct drm_minor *minor)
1033 drm_debugfs_create_files(drm_vram_mm_debugfs_list,
1034 ARRAY_SIZE(drm_vram_mm_debugfs_list),
1035 minor->debugfs_root, minor);
1037 EXPORT_SYMBOL(drm_vram_mm_debugfs_init);
1039 static int drm_vram_mm_init(struct drm_vram_mm *vmm, struct drm_device *dev,
1040 uint64_t vram_base, size_t vram_size)
1044 vmm->vram_base = vram_base;
1045 vmm->vram_size = vram_size;
1047 ret = ttm_bo_device_init(&vmm->bdev, &bo_driver, dev->dev,
1048 dev->anon_inode->i_mapping,
1049 dev->vma_offset_manager,
1054 ret = ttm_range_man_init(&vmm->bdev, TTM_PL_VRAM,
1055 false, vram_size >> PAGE_SHIFT);
1062 static void drm_vram_mm_cleanup(struct drm_vram_mm *vmm)
1064 ttm_range_man_fini(&vmm->bdev, TTM_PL_VRAM);
1065 ttm_bo_device_release(&vmm->bdev);
1069 * Helpers for integration with struct drm_device
1072 /* deprecated; use drmm_vram_mm_init() */
1073 struct drm_vram_mm *drm_vram_helper_alloc_mm(
1074 struct drm_device *dev, uint64_t vram_base, size_t vram_size)
1078 if (WARN_ON(dev->vram_mm))
1079 return dev->vram_mm;
1081 dev->vram_mm = kzalloc(sizeof(*dev->vram_mm), GFP_KERNEL);
1083 return ERR_PTR(-ENOMEM);
1085 ret = drm_vram_mm_init(dev->vram_mm, dev, vram_base, vram_size);
1089 return dev->vram_mm;
1092 kfree(dev->vram_mm);
1093 dev->vram_mm = NULL;
1094 return ERR_PTR(ret);
1096 EXPORT_SYMBOL(drm_vram_helper_alloc_mm);
1098 void drm_vram_helper_release_mm(struct drm_device *dev)
1103 drm_vram_mm_cleanup(dev->vram_mm);
1104 kfree(dev->vram_mm);
1105 dev->vram_mm = NULL;
1107 EXPORT_SYMBOL(drm_vram_helper_release_mm);
1109 static void drm_vram_mm_release(struct drm_device *dev, void *ptr)
1111 drm_vram_helper_release_mm(dev);
1115 * drmm_vram_helper_init - Initializes a device's instance of
1116 * &struct drm_vram_mm
1117 * @dev: the DRM device
1118 * @vram_base: the base address of the video memory
1119 * @vram_size: the size of the video memory in bytes
1121 * Creates a new instance of &struct drm_vram_mm and stores it in
1122 * struct &drm_device.vram_mm. The instance is auto-managed and cleaned
1123 * up as part of device cleanup. Calling this function multiple times
1124 * will generate an error message.
1127 * 0 on success, or a negative errno code otherwise.
1129 int drmm_vram_helper_init(struct drm_device *dev, uint64_t vram_base,
1132 struct drm_vram_mm *vram_mm;
1134 if (drm_WARN_ON_ONCE(dev, dev->vram_mm))
1137 vram_mm = drm_vram_helper_alloc_mm(dev, vram_base, vram_size);
1138 if (IS_ERR(vram_mm))
1139 return PTR_ERR(vram_mm);
1140 return drmm_add_action_or_reset(dev, drm_vram_mm_release, NULL);
1142 EXPORT_SYMBOL(drmm_vram_helper_init);
1145 * Mode-config helpers
1148 static enum drm_mode_status
1149 drm_vram_helper_mode_valid_internal(struct drm_device *dev,
1150 const struct drm_display_mode *mode,
1151 unsigned long max_bpp)
1153 struct drm_vram_mm *vmm = dev->vram_mm;
1154 unsigned long fbsize, fbpages, max_fbpages;
1156 if (WARN_ON(!dev->vram_mm))
1159 max_fbpages = (vmm->vram_size / 2) >> PAGE_SHIFT;
1161 fbsize = mode->hdisplay * mode->vdisplay * max_bpp;
1162 fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE);
1164 if (fbpages > max_fbpages)
1171 * drm_vram_helper_mode_valid - Tests if a display mode's
1172 * framebuffer fits into the available video memory.
1173 * @dev: the DRM device
1174 * @mode: the mode to test
1176 * This function tests if enough video memory is available for using the
1177 * specified display mode. Atomic modesetting requires importing the
1178 * designated framebuffer into video memory before evicting the active
1179 * one. Hence, any framebuffer may consume at most half of the available
1180 * VRAM. Display modes that require a larger framebuffer can not be used,
1181 * even if the CRTC does support them. Each framebuffer is assumed to
1182 * have 32-bit color depth.
1185 * The function can only test if the display mode is supported in
1186 * general. If there are too many framebuffers pinned to video memory,
1187 * a display mode may still not be usable in practice. The color depth of
1188 * 32-bit fits all current use case. A more flexible test can be added
1192 * MODE_OK if the display mode is supported, or an error code of type
1193 * enum drm_mode_status otherwise.
1195 enum drm_mode_status
1196 drm_vram_helper_mode_valid(struct drm_device *dev,
1197 const struct drm_display_mode *mode)
1199 static const unsigned long max_bpp = 4; /* DRM_FORMAT_XRGB8888 */
1201 return drm_vram_helper_mode_valid_internal(dev, mode, max_bpp);
1203 EXPORT_SYMBOL(drm_vram_helper_mode_valid);
1205 MODULE_DESCRIPTION("DRM VRAM memory-management helpers");
1206 MODULE_LICENSE("GPL");