1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/dma-buf-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_ttm_helper.h>
13 #include <drm/drm_gem_vram_helper.h>
14 #include <drm/drm_managed.h>
15 #include <drm/drm_mode.h>
16 #include <drm/drm_plane.h>
17 #include <drm/drm_prime.h>
18 #include <drm/drm_simple_kms_helper.h>
20 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs;
25 * This library provides &struct drm_gem_vram_object (GEM VRAM), a GEM
26 * buffer object that is backed by video RAM (VRAM). It can be used for
27 * framebuffer devices with dedicated memory.
29 * The data structure &struct drm_vram_mm and its helpers implement a memory
30 * manager for simple framebuffer devices with dedicated video memory. GEM
31 * VRAM buffer objects are either placed in the video memory or remain evicted
34 * With the GEM interface userspace applications create, manage and destroy
35 * graphics buffers, such as an on-screen framebuffer. GEM does not provide
36 * an implementation of these interfaces. It's up to the DRM driver to
37 * provide an implementation that suits the hardware. If the hardware device
38 * contains dedicated video memory, the DRM driver can use the VRAM helper
39 * library. Each active buffer object is stored in video RAM. Active
40 * buffer are used for drawing the current frame, typically something like
41 * the frame's scanout buffer or the cursor image. If there's no more space
42 * left in VRAM, inactive GEM objects can be moved to system memory.
44 * To initialize the VRAM helper library call drmm_vram_helper_alloc_mm().
45 * The function allocates and initializes an instance of &struct drm_vram_mm
46 * in &struct drm_device.vram_mm . Use &DRM_GEM_VRAM_DRIVER to initialize
47 * &struct drm_driver and &DRM_VRAM_MM_FILE_OPERATIONS to initialize
48 * &struct file_operations; as illustrated below.
52 * struct file_operations fops ={
53 * .owner = THIS_MODULE,
54 * DRM_VRAM_MM_FILE_OPERATION
56 * struct drm_driver drv = {
57 * .driver_feature = DRM_ ... ,
62 * int init_drm_driver()
64 * struct drm_device *dev;
66 * unsigned long vram_size;
69 * // setup device, vram base and size
72 * ret = drmm_vram_helper_alloc_mm(dev, vram_base, vram_size);
78 * This creates an instance of &struct drm_vram_mm, exports DRM userspace
79 * interfaces for GEM buffer management and initializes file operations to
80 * allow for accessing created GEM buffers. With this setup, the DRM driver
81 * manages an area of video RAM with VRAM MM and provides GEM VRAM objects
84 * You don't have to clean up the instance of VRAM MM.
85 * drmm_vram_helper_alloc_mm() is a managed interface that installs a
86 * clean-up handler to run during the DRM device's release.
88 * For drawing or scanout operations, rsp. buffer objects have to be pinned
89 * in video RAM. Call drm_gem_vram_pin() with &DRM_GEM_VRAM_PL_FLAG_VRAM or
90 * &DRM_GEM_VRAM_PL_FLAG_SYSTEM to pin a buffer object in video RAM or system
91 * memory. Call drm_gem_vram_unpin() to release the pinned object afterwards.
93 * A buffer object that is pinned in video RAM has a fixed address within that
94 * memory region. Call drm_gem_vram_offset() to retrieve this value. Typically
95 * it's used to program the hardware's scanout engine for framebuffers, set
96 * the cursor overlay's image for a mouse cursor, or use it as input to the
97 * hardware's draing engine.
99 * To access a buffer object's memory from the DRM driver, call
100 * drm_gem_vram_vmap(). It maps the buffer into kernel address
101 * space and returns the memory address. Use drm_gem_vram_vunmap() to
102 * release the mapping.
106 * Buffer-objects helpers
109 static void drm_gem_vram_cleanup(struct drm_gem_vram_object *gbo)
111 /* We got here via ttm_bo_put(), which means that the
112 * TTM buffer object in 'bo' has already been cleaned
113 * up; only release the GEM object.
116 WARN_ON(gbo->vmap_use_count);
117 WARN_ON(dma_buf_map_is_set(&gbo->map));
119 drm_gem_object_release(&gbo->bo.base);
122 static void drm_gem_vram_destroy(struct drm_gem_vram_object *gbo)
124 drm_gem_vram_cleanup(gbo);
128 static void ttm_buffer_object_destroy(struct ttm_buffer_object *bo)
130 struct drm_gem_vram_object *gbo = drm_gem_vram_of_bo(bo);
132 drm_gem_vram_destroy(gbo);
135 static void drm_gem_vram_placement(struct drm_gem_vram_object *gbo,
136 unsigned long pl_flag)
138 u32 invariant_flags = 0;
142 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_TOPDOWN)
143 invariant_flags = TTM_PL_FLAG_TOPDOWN;
145 gbo->placement.placement = gbo->placements;
146 gbo->placement.busy_placement = gbo->placements;
148 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_VRAM) {
149 gbo->placements[c].mem_type = TTM_PL_VRAM;
150 gbo->placements[c++].flags = invariant_flags;
153 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_SYSTEM || !c) {
154 gbo->placements[c].mem_type = TTM_PL_SYSTEM;
155 gbo->placements[c++].flags = invariant_flags;
158 gbo->placement.num_placement = c;
159 gbo->placement.num_busy_placement = c;
161 for (i = 0; i < c; ++i) {
162 gbo->placements[i].fpfn = 0;
163 gbo->placements[i].lpfn = 0;
168 * drm_gem_vram_create() - Creates a VRAM-backed GEM object
169 * @dev: the DRM device
170 * @size: the buffer size in bytes
171 * @pg_align: the buffer's alignment in multiples of the page size
173 * GEM objects are allocated by calling struct drm_driver.gem_create_object,
174 * if set. Otherwise kzalloc() will be used. Drivers can set their own GEM
175 * object functions in struct drm_driver.gem_create_object. If no functions
176 * are set, the new GEM object will use the default functions from GEM VRAM
180 * A new instance of &struct drm_gem_vram_object on success, or
181 * an ERR_PTR()-encoded error code otherwise.
183 struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
185 unsigned long pg_align)
187 struct drm_gem_vram_object *gbo;
188 struct drm_gem_object *gem;
189 struct drm_vram_mm *vmm = dev->vram_mm;
190 struct ttm_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);
220 drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
223 * A failing ttm_bo_init will call ttm_buffer_object_destroy
224 * to release gbo->bo.base and kfree gbo.
226 ret = ttm_bo_init(bdev, &gbo->bo, size, ttm_bo_type_device,
227 &gbo->placement, pg_align, false, NULL, NULL,
228 ttm_buffer_object_destroy);
234 EXPORT_SYMBOL(drm_gem_vram_create);
237 * drm_gem_vram_put() - Releases a reference to a VRAM-backed GEM object
238 * @gbo: the GEM VRAM object
240 * See ttm_bo_put() for more information.
242 void drm_gem_vram_put(struct drm_gem_vram_object *gbo)
244 ttm_bo_put(&gbo->bo);
246 EXPORT_SYMBOL(drm_gem_vram_put);
248 static u64 drm_gem_vram_pg_offset(struct drm_gem_vram_object *gbo)
250 /* Keep TTM behavior for now, remove when drivers are audited */
251 if (WARN_ON_ONCE(!gbo->bo.mem.mm_node))
254 return gbo->bo.mem.start;
258 * drm_gem_vram_offset() - \
259 Returns a GEM VRAM object's offset in video memory
260 * @gbo: the GEM VRAM object
262 * This function returns the buffer object's offset in the device's video
263 * memory. The buffer object has to be pinned to %TTM_PL_VRAM.
266 * The buffer object's offset in video memory on success, or
267 * a negative errno code otherwise.
269 s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo)
271 if (WARN_ON_ONCE(!gbo->bo.pin_count))
273 return drm_gem_vram_pg_offset(gbo) << PAGE_SHIFT;
275 EXPORT_SYMBOL(drm_gem_vram_offset);
277 static int drm_gem_vram_pin_locked(struct drm_gem_vram_object *gbo,
278 unsigned long pl_flag)
280 struct ttm_operation_ctx ctx = { false, false };
283 if (gbo->bo.pin_count)
287 drm_gem_vram_placement(gbo, pl_flag);
289 ret = ttm_bo_validate(&gbo->bo, &gbo->placement, &ctx);
294 ttm_bo_pin(&gbo->bo);
300 * drm_gem_vram_pin() - Pins a GEM VRAM object in a region.
301 * @gbo: the GEM VRAM object
302 * @pl_flag: a bitmask of possible memory regions
304 * Pinning a buffer object ensures that it is not evicted from
305 * a memory region. A pinned buffer object has to be unpinned before
306 * it can be pinned to another region. If the pl_flag argument is 0,
307 * the buffer is pinned at its current location (video RAM or system
310 * Small buffer objects, such as cursor images, can lead to memory
311 * fragmentation if they are pinned in the middle of video RAM. This
312 * is especially a problem on devices with only a small amount of
313 * video RAM. Fragmentation can prevent the primary framebuffer from
314 * fitting in, even though there's enough memory overall. The modifier
315 * DRM_GEM_VRAM_PL_FLAG_TOPDOWN marks the buffer object to be pinned
316 * at the high end of the memory region to avoid fragmentation.
320 * a negative error code otherwise.
322 int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag)
326 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
329 ret = drm_gem_vram_pin_locked(gbo, pl_flag);
330 ttm_bo_unreserve(&gbo->bo);
334 EXPORT_SYMBOL(drm_gem_vram_pin);
336 static void drm_gem_vram_unpin_locked(struct drm_gem_vram_object *gbo)
338 ttm_bo_unpin(&gbo->bo);
342 * drm_gem_vram_unpin() - Unpins a GEM VRAM object
343 * @gbo: the GEM VRAM object
347 * a negative error code otherwise.
349 int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo)
353 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
357 drm_gem_vram_unpin_locked(gbo);
358 ttm_bo_unreserve(&gbo->bo);
362 EXPORT_SYMBOL(drm_gem_vram_unpin);
364 static int drm_gem_vram_kmap_locked(struct drm_gem_vram_object *gbo,
365 struct dma_buf_map *map)
369 if (gbo->vmap_use_count > 0)
373 * VRAM helpers unmap the BO only on demand. So the previous
374 * page mapping might still be around. Only vmap if the there's
375 * no mapping present.
377 if (dma_buf_map_is_null(&gbo->map)) {
378 ret = ttm_bo_vmap(&gbo->bo, &gbo->map);
384 ++gbo->vmap_use_count;
390 static void drm_gem_vram_kunmap_locked(struct drm_gem_vram_object *gbo,
391 struct dma_buf_map *map)
393 struct drm_device *dev = gbo->bo.base.dev;
395 if (drm_WARN_ON_ONCE(dev, !gbo->vmap_use_count))
398 if (drm_WARN_ON_ONCE(dev, !dma_buf_map_is_equal(&gbo->map, map)))
399 return; /* BUG: map not mapped from this BO */
401 if (--gbo->vmap_use_count > 0)
405 * Permanently mapping and unmapping buffers adds overhead from
406 * updating the page tables and creates debugging output. Therefore,
407 * we delay the actual unmap operation until the BO gets evicted
408 * from memory. See drm_gem_vram_bo_driver_move_notify().
413 * drm_gem_vram_vmap() - Pins and maps a GEM VRAM object into kernel address
415 * @gbo: The GEM VRAM object to map
416 * @map: Returns the kernel virtual address of the VRAM GEM object's backing
419 * The vmap function pins a GEM VRAM object to its current location, either
420 * system or video memory, and maps its buffer into kernel address space.
421 * As pinned object cannot be relocated, you should avoid pinning objects
422 * permanently. Call drm_gem_vram_vunmap() with the returned address to
423 * unmap and unpin the GEM VRAM object.
426 * 0 on success, or a negative error code otherwise.
428 int drm_gem_vram_vmap(struct drm_gem_vram_object *gbo, struct dma_buf_map *map)
432 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
436 ret = drm_gem_vram_pin_locked(gbo, 0);
438 goto err_ttm_bo_unreserve;
439 ret = drm_gem_vram_kmap_locked(gbo, map);
441 goto err_drm_gem_vram_unpin_locked;
443 ttm_bo_unreserve(&gbo->bo);
447 err_drm_gem_vram_unpin_locked:
448 drm_gem_vram_unpin_locked(gbo);
449 err_ttm_bo_unreserve:
450 ttm_bo_unreserve(&gbo->bo);
453 EXPORT_SYMBOL(drm_gem_vram_vmap);
456 * drm_gem_vram_vunmap() - Unmaps and unpins a GEM VRAM object
457 * @gbo: The GEM VRAM object to unmap
458 * @map: Kernel virtual address where the VRAM GEM object was mapped
460 * A call to drm_gem_vram_vunmap() unmaps and unpins a GEM VRAM buffer. See
461 * the documentation for drm_gem_vram_vmap() for more information.
463 void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo, struct dma_buf_map *map)
467 ret = ttm_bo_reserve(&gbo->bo, false, false, NULL);
468 if (WARN_ONCE(ret, "ttm_bo_reserve_failed(): ret=%d\n", ret))
471 drm_gem_vram_kunmap_locked(gbo, map);
472 drm_gem_vram_unpin_locked(gbo);
474 ttm_bo_unreserve(&gbo->bo);
476 EXPORT_SYMBOL(drm_gem_vram_vunmap);
479 * drm_gem_vram_fill_create_dumb() - \
480 Helper for implementing &struct drm_driver.dumb_create
481 * @file: the DRM file
482 * @dev: the DRM device
483 * @pg_align: the buffer's alignment in multiples of the page size
484 * @pitch_align: the scanline's alignment in powers of 2
485 * @args: the arguments as provided to \
486 &struct drm_driver.dumb_create
488 * This helper function fills &struct drm_mode_create_dumb, which is used
489 * by &struct drm_driver.dumb_create. Implementations of this interface
490 * should forwards their arguments to this helper, plus the driver-specific
495 * a negative error code otherwise.
497 int drm_gem_vram_fill_create_dumb(struct drm_file *file,
498 struct drm_device *dev,
499 unsigned long pg_align,
500 unsigned long pitch_align,
501 struct drm_mode_create_dumb *args)
504 struct drm_gem_vram_object *gbo;
508 pitch = args->width * DIV_ROUND_UP(args->bpp, 8);
510 if (WARN_ON_ONCE(!is_power_of_2(pitch_align)))
512 pitch = ALIGN(pitch, pitch_align);
514 size = pitch * args->height;
516 size = roundup(size, PAGE_SIZE);
520 gbo = drm_gem_vram_create(dev, size, pg_align);
524 ret = drm_gem_handle_create(file, &gbo->bo.base, &handle);
526 goto err_drm_gem_object_put;
528 drm_gem_object_put(&gbo->bo.base);
532 args->handle = handle;
536 err_drm_gem_object_put:
537 drm_gem_object_put(&gbo->bo.base);
540 EXPORT_SYMBOL(drm_gem_vram_fill_create_dumb);
543 * Helpers for struct ttm_device_funcs
546 static bool drm_is_gem_vram(struct ttm_buffer_object *bo)
548 return (bo->destroy == ttm_buffer_object_destroy);
551 static void drm_gem_vram_bo_driver_evict_flags(struct drm_gem_vram_object *gbo,
552 struct ttm_placement *pl)
554 drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
555 *pl = gbo->placement;
558 static void drm_gem_vram_bo_driver_move_notify(struct drm_gem_vram_object *gbo)
560 struct ttm_buffer_object *bo = &gbo->bo;
561 struct drm_device *dev = bo->base.dev;
563 if (drm_WARN_ON_ONCE(dev, gbo->vmap_use_count))
566 ttm_bo_vunmap(bo, &gbo->map);
567 dma_buf_map_clear(&gbo->map); /* explicitly clear mapping for next vmap call */
570 static int drm_gem_vram_bo_driver_move(struct drm_gem_vram_object *gbo,
572 struct ttm_operation_ctx *ctx,
573 struct ttm_resource *new_mem)
575 drm_gem_vram_bo_driver_move_notify(gbo);
576 return ttm_bo_move_memcpy(&gbo->bo, ctx, new_mem);
580 * Helpers for struct drm_gem_object_funcs
584 * drm_gem_vram_object_free() - \
585 Implements &struct drm_gem_object_funcs.free
586 * @gem: GEM object. Refers to &struct drm_gem_vram_object.gem
588 static void drm_gem_vram_object_free(struct drm_gem_object *gem)
590 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
592 drm_gem_vram_put(gbo);
596 * Helpers for dump buffers
600 * drm_gem_vram_driver_dumb_create() - \
601 Implements &struct drm_driver.dumb_create
602 * @file: the DRM file
603 * @dev: the DRM device
604 * @args: the arguments as provided to \
605 &struct drm_driver.dumb_create
607 * This function requires the driver to use @drm_device.vram_mm for its
608 * instance of VRAM MM.
612 * a negative error code otherwise.
614 int drm_gem_vram_driver_dumb_create(struct drm_file *file,
615 struct drm_device *dev,
616 struct drm_mode_create_dumb *args)
618 if (WARN_ONCE(!dev->vram_mm, "VRAM MM not initialized"))
621 return drm_gem_vram_fill_create_dumb(file, dev, 0, 0, args);
623 EXPORT_SYMBOL(drm_gem_vram_driver_dumb_create);
626 * Helpers for struct drm_plane_helper_funcs
630 * drm_gem_vram_plane_helper_prepare_fb() - \
631 * Implements &struct drm_plane_helper_funcs.prepare_fb
632 * @plane: a DRM plane
633 * @new_state: the plane's new state
635 * During plane updates, this function sets the plane's fence and
636 * pins the GEM VRAM objects of the plane's new framebuffer to VRAM.
637 * Call drm_gem_vram_plane_helper_cleanup_fb() to unpin them.
641 * a negative errno code otherwise.
644 drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane,
645 struct drm_plane_state *new_state)
648 struct drm_gem_vram_object *gbo;
654 for (i = 0; i < ARRAY_SIZE(new_state->fb->obj); ++i) {
655 if (!new_state->fb->obj[i])
657 gbo = drm_gem_vram_of_gem(new_state->fb->obj[i]);
658 ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
660 goto err_drm_gem_vram_unpin;
663 ret = drm_gem_plane_helper_prepare_fb(plane, new_state);
665 goto err_drm_gem_vram_unpin;
669 err_drm_gem_vram_unpin:
672 gbo = drm_gem_vram_of_gem(new_state->fb->obj[i]);
673 drm_gem_vram_unpin(gbo);
677 EXPORT_SYMBOL(drm_gem_vram_plane_helper_prepare_fb);
680 * drm_gem_vram_plane_helper_cleanup_fb() - \
681 * Implements &struct drm_plane_helper_funcs.cleanup_fb
682 * @plane: a DRM plane
683 * @old_state: the plane's old state
685 * During plane updates, this function unpins the GEM VRAM
686 * objects of the plane's old framebuffer from VRAM. Complements
687 * drm_gem_vram_plane_helper_prepare_fb().
690 drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
691 struct drm_plane_state *old_state)
694 struct drm_gem_vram_object *gbo;
699 for (i = 0; i < ARRAY_SIZE(old_state->fb->obj); ++i) {
700 if (!old_state->fb->obj[i])
702 gbo = drm_gem_vram_of_gem(old_state->fb->obj[i]);
703 drm_gem_vram_unpin(gbo);
706 EXPORT_SYMBOL(drm_gem_vram_plane_helper_cleanup_fb);
709 * Helpers for struct drm_simple_display_pipe_funcs
713 * drm_gem_vram_simple_display_pipe_prepare_fb() - \
714 * Implements &struct drm_simple_display_pipe_funcs.prepare_fb
715 * @pipe: a simple display pipe
716 * @new_state: the plane's new state
718 * During plane updates, this function pins the GEM VRAM
719 * objects of the plane's new framebuffer to VRAM. Call
720 * drm_gem_vram_simple_display_pipe_cleanup_fb() to unpin them.
724 * a negative errno code otherwise.
726 int drm_gem_vram_simple_display_pipe_prepare_fb(
727 struct drm_simple_display_pipe *pipe,
728 struct drm_plane_state *new_state)
730 return drm_gem_vram_plane_helper_prepare_fb(&pipe->plane, new_state);
732 EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_prepare_fb);
735 * drm_gem_vram_simple_display_pipe_cleanup_fb() - \
736 * Implements &struct drm_simple_display_pipe_funcs.cleanup_fb
737 * @pipe: a simple display pipe
738 * @old_state: the plane's old state
740 * During plane updates, this function unpins the GEM VRAM
741 * objects of the plane's old framebuffer from VRAM. Complements
742 * drm_gem_vram_simple_display_pipe_prepare_fb().
744 void drm_gem_vram_simple_display_pipe_cleanup_fb(
745 struct drm_simple_display_pipe *pipe,
746 struct drm_plane_state *old_state)
748 drm_gem_vram_plane_helper_cleanup_fb(&pipe->plane, old_state);
750 EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_cleanup_fb);
757 * drm_gem_vram_object_pin() - \
758 Implements &struct drm_gem_object_funcs.pin
759 * @gem: The GEM object to pin
763 * a negative errno code otherwise.
765 static int drm_gem_vram_object_pin(struct drm_gem_object *gem)
767 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
769 /* Fbdev console emulation is the use case of these PRIME
770 * helpers. This may involve updating a hardware buffer from
771 * a shadow FB. We pin the buffer to it's current location
772 * (either video RAM or system memory) to prevent it from
773 * being relocated during the update operation. If you require
774 * the buffer to be pinned to VRAM, implement a callback that
775 * sets the flags accordingly.
777 return drm_gem_vram_pin(gbo, 0);
781 * drm_gem_vram_object_unpin() - \
782 Implements &struct drm_gem_object_funcs.unpin
783 * @gem: The GEM object to unpin
785 static void drm_gem_vram_object_unpin(struct drm_gem_object *gem)
787 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
789 drm_gem_vram_unpin(gbo);
793 * drm_gem_vram_object_vmap() -
794 * Implements &struct drm_gem_object_funcs.vmap
795 * @gem: The GEM object to map
796 * @map: Returns the kernel virtual address of the VRAM GEM object's backing
800 * 0 on success, or a negative error code otherwise.
802 static int drm_gem_vram_object_vmap(struct drm_gem_object *gem, struct dma_buf_map *map)
804 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
806 return drm_gem_vram_vmap(gbo, map);
810 * drm_gem_vram_object_vunmap() -
811 * Implements &struct drm_gem_object_funcs.vunmap
812 * @gem: The GEM object to unmap
813 * @map: Kernel virtual address where the VRAM GEM object was mapped
815 static void drm_gem_vram_object_vunmap(struct drm_gem_object *gem, struct dma_buf_map *map)
817 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
819 drm_gem_vram_vunmap(gbo, map);
826 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs = {
827 .free = drm_gem_vram_object_free,
828 .pin = drm_gem_vram_object_pin,
829 .unpin = drm_gem_vram_object_unpin,
830 .vmap = drm_gem_vram_object_vmap,
831 .vunmap = drm_gem_vram_object_vunmap,
832 .mmap = drm_gem_ttm_mmap,
833 .print_info = drm_gem_ttm_print_info,
837 * VRAM memory manager
844 static void bo_driver_ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *tt)
846 ttm_tt_destroy_common(bdev, tt);
855 static struct ttm_tt *bo_driver_ttm_tt_create(struct ttm_buffer_object *bo,
861 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
865 ret = ttm_tt_init(tt, bo, page_flags, ttm_cached);
867 goto err_ttm_tt_init;
876 static void bo_driver_evict_flags(struct ttm_buffer_object *bo,
877 struct ttm_placement *placement)
879 struct drm_gem_vram_object *gbo;
881 /* TTM may pass BOs that are not GEM VRAM BOs. */
882 if (!drm_is_gem_vram(bo))
885 gbo = drm_gem_vram_of_bo(bo);
887 drm_gem_vram_bo_driver_evict_flags(gbo, placement);
890 static void bo_driver_delete_mem_notify(struct ttm_buffer_object *bo)
892 struct drm_gem_vram_object *gbo;
894 /* TTM may pass BOs that are not GEM VRAM BOs. */
895 if (!drm_is_gem_vram(bo))
898 gbo = drm_gem_vram_of_bo(bo);
900 drm_gem_vram_bo_driver_move_notify(gbo);
903 static int bo_driver_move(struct ttm_buffer_object *bo,
905 struct ttm_operation_ctx *ctx,
906 struct ttm_resource *new_mem,
907 struct ttm_place *hop)
909 struct drm_gem_vram_object *gbo;
911 gbo = drm_gem_vram_of_bo(bo);
913 return drm_gem_vram_bo_driver_move(gbo, evict, ctx, new_mem);
916 static int bo_driver_io_mem_reserve(struct ttm_device *bdev,
917 struct ttm_resource *mem)
919 struct drm_vram_mm *vmm = drm_vram_mm_of_bdev(bdev);
921 switch (mem->mem_type) {
922 case TTM_PL_SYSTEM: /* nothing to do */
925 mem->bus.offset = (mem->start << PAGE_SHIFT) + vmm->vram_base;
926 mem->bus.is_iomem = true;
927 mem->bus.caching = ttm_write_combined;
936 static struct ttm_device_funcs bo_driver = {
937 .ttm_tt_create = bo_driver_ttm_tt_create,
938 .ttm_tt_destroy = bo_driver_ttm_tt_destroy,
939 .eviction_valuable = ttm_bo_eviction_valuable,
940 .evict_flags = bo_driver_evict_flags,
941 .move = bo_driver_move,
942 .delete_mem_notify = bo_driver_delete_mem_notify,
943 .io_mem_reserve = bo_driver_io_mem_reserve,
950 static int drm_vram_mm_debugfs(struct seq_file *m, void *data)
952 struct drm_info_node *node = (struct drm_info_node *) m->private;
953 struct drm_vram_mm *vmm = node->minor->dev->vram_mm;
954 struct ttm_resource_manager *man = ttm_manager_type(&vmm->bdev, TTM_PL_VRAM);
955 struct drm_printer p = drm_seq_file_printer(m);
957 ttm_resource_manager_debug(man, &p);
961 static const struct drm_info_list drm_vram_mm_debugfs_list[] = {
962 { "vram-mm", drm_vram_mm_debugfs, 0, NULL },
966 * drm_vram_mm_debugfs_init() - Register VRAM MM debugfs file.
968 * @minor: drm minor device.
971 void drm_vram_mm_debugfs_init(struct drm_minor *minor)
973 drm_debugfs_create_files(drm_vram_mm_debugfs_list,
974 ARRAY_SIZE(drm_vram_mm_debugfs_list),
975 minor->debugfs_root, minor);
977 EXPORT_SYMBOL(drm_vram_mm_debugfs_init);
979 static int drm_vram_mm_init(struct drm_vram_mm *vmm, struct drm_device *dev,
980 uint64_t vram_base, size_t vram_size)
984 vmm->vram_base = vram_base;
985 vmm->vram_size = vram_size;
987 ret = ttm_device_init(&vmm->bdev, &bo_driver, dev->dev,
988 dev->anon_inode->i_mapping,
989 dev->vma_offset_manager,
994 ret = ttm_range_man_init(&vmm->bdev, TTM_PL_VRAM,
995 false, vram_size >> PAGE_SHIFT);
1002 static void drm_vram_mm_cleanup(struct drm_vram_mm *vmm)
1004 ttm_range_man_fini(&vmm->bdev, TTM_PL_VRAM);
1005 ttm_device_fini(&vmm->bdev);
1009 * Helpers for integration with struct drm_device
1012 /* deprecated; use drmm_vram_mm_init() */
1013 struct drm_vram_mm *drm_vram_helper_alloc_mm(
1014 struct drm_device *dev, uint64_t vram_base, size_t vram_size)
1018 if (WARN_ON(dev->vram_mm))
1019 return dev->vram_mm;
1021 dev->vram_mm = kzalloc(sizeof(*dev->vram_mm), GFP_KERNEL);
1023 return ERR_PTR(-ENOMEM);
1025 ret = drm_vram_mm_init(dev->vram_mm, dev, vram_base, vram_size);
1029 return dev->vram_mm;
1032 kfree(dev->vram_mm);
1033 dev->vram_mm = NULL;
1034 return ERR_PTR(ret);
1036 EXPORT_SYMBOL(drm_vram_helper_alloc_mm);
1038 void drm_vram_helper_release_mm(struct drm_device *dev)
1043 drm_vram_mm_cleanup(dev->vram_mm);
1044 kfree(dev->vram_mm);
1045 dev->vram_mm = NULL;
1047 EXPORT_SYMBOL(drm_vram_helper_release_mm);
1049 static void drm_vram_mm_release(struct drm_device *dev, void *ptr)
1051 drm_vram_helper_release_mm(dev);
1055 * drmm_vram_helper_init - Initializes a device's instance of
1056 * &struct drm_vram_mm
1057 * @dev: the DRM device
1058 * @vram_base: the base address of the video memory
1059 * @vram_size: the size of the video memory in bytes
1061 * Creates a new instance of &struct drm_vram_mm and stores it in
1062 * struct &drm_device.vram_mm. The instance is auto-managed and cleaned
1063 * up as part of device cleanup. Calling this function multiple times
1064 * will generate an error message.
1067 * 0 on success, or a negative errno code otherwise.
1069 int drmm_vram_helper_init(struct drm_device *dev, uint64_t vram_base,
1072 struct drm_vram_mm *vram_mm;
1074 if (drm_WARN_ON_ONCE(dev, dev->vram_mm))
1077 vram_mm = drm_vram_helper_alloc_mm(dev, vram_base, vram_size);
1078 if (IS_ERR(vram_mm))
1079 return PTR_ERR(vram_mm);
1080 return drmm_add_action_or_reset(dev, drm_vram_mm_release, NULL);
1082 EXPORT_SYMBOL(drmm_vram_helper_init);
1085 * Mode-config helpers
1088 static enum drm_mode_status
1089 drm_vram_helper_mode_valid_internal(struct drm_device *dev,
1090 const struct drm_display_mode *mode,
1091 unsigned long max_bpp)
1093 struct drm_vram_mm *vmm = dev->vram_mm;
1094 unsigned long fbsize, fbpages, max_fbpages;
1096 if (WARN_ON(!dev->vram_mm))
1099 max_fbpages = (vmm->vram_size / 2) >> PAGE_SHIFT;
1101 fbsize = mode->hdisplay * mode->vdisplay * max_bpp;
1102 fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE);
1104 if (fbpages > max_fbpages)
1111 * drm_vram_helper_mode_valid - Tests if a display mode's
1112 * framebuffer fits into the available video memory.
1113 * @dev: the DRM device
1114 * @mode: the mode to test
1116 * This function tests if enough video memory is available for using the
1117 * specified display mode. Atomic modesetting requires importing the
1118 * designated framebuffer into video memory before evicting the active
1119 * one. Hence, any framebuffer may consume at most half of the available
1120 * VRAM. Display modes that require a larger framebuffer can not be used,
1121 * even if the CRTC does support them. Each framebuffer is assumed to
1122 * have 32-bit color depth.
1125 * The function can only test if the display mode is supported in
1126 * general. If there are too many framebuffers pinned to video memory,
1127 * a display mode may still not be usable in practice. The color depth of
1128 * 32-bit fits all current use case. A more flexible test can be added
1132 * MODE_OK if the display mode is supported, or an error code of type
1133 * enum drm_mode_status otherwise.
1135 enum drm_mode_status
1136 drm_vram_helper_mode_valid(struct drm_device *dev,
1137 const struct drm_display_mode *mode)
1139 static const unsigned long max_bpp = 4; /* DRM_FORMAT_XRGB8888 */
1141 return drm_vram_helper_mode_valid_internal(dev, mode, max_bpp);
1143 EXPORT_SYMBOL(drm_vram_helper_mode_valid);
1145 MODULE_DESCRIPTION("DRM VRAM memory-management helpers");
1146 MODULE_LICENSE("GPL");