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_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 #include <drm/ttm/ttm_range_manager.h>
22 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs;
27 * This library provides &struct drm_gem_vram_object (GEM VRAM), a GEM
28 * buffer object that is backed by video RAM (VRAM). It can be used for
29 * framebuffer devices with dedicated memory.
31 * The data structure &struct drm_vram_mm and its helpers implement a memory
32 * manager for simple framebuffer devices with dedicated video memory. GEM
33 * VRAM buffer objects are either placed in the video memory or remain evicted
36 * With the GEM interface userspace applications create, manage and destroy
37 * graphics buffers, such as an on-screen framebuffer. GEM does not provide
38 * an implementation of these interfaces. It's up to the DRM driver to
39 * provide an implementation that suits the hardware. If the hardware device
40 * contains dedicated video memory, the DRM driver can use the VRAM helper
41 * library. Each active buffer object is stored in video RAM. Active
42 * buffer are used for drawing the current frame, typically something like
43 * the frame's scanout buffer or the cursor image. If there's no more space
44 * left in VRAM, inactive GEM objects can be moved to system memory.
46 * To initialize the VRAM helper library call drmm_vram_helper_alloc_mm().
47 * The function allocates and initializes an instance of &struct drm_vram_mm
48 * in &struct drm_device.vram_mm . Use &DRM_GEM_VRAM_DRIVER to initialize
49 * &struct drm_driver and &DRM_VRAM_MM_FILE_OPERATIONS to initialize
50 * &struct file_operations; as illustrated below.
54 * struct file_operations fops ={
55 * .owner = THIS_MODULE,
56 * DRM_VRAM_MM_FILE_OPERATION
58 * struct drm_driver drv = {
59 * .driver_feature = DRM_ ... ,
64 * int init_drm_driver()
66 * struct drm_device *dev;
68 * unsigned long vram_size;
71 * // setup device, vram base and size
74 * ret = drmm_vram_helper_alloc_mm(dev, vram_base, vram_size);
80 * This creates an instance of &struct drm_vram_mm, exports DRM userspace
81 * interfaces for GEM buffer management and initializes file operations to
82 * allow for accessing created GEM buffers. With this setup, the DRM driver
83 * manages an area of video RAM with VRAM MM and provides GEM VRAM objects
86 * You don't have to clean up the instance of VRAM MM.
87 * drmm_vram_helper_alloc_mm() is a managed interface that installs a
88 * clean-up handler to run during the DRM device's release.
90 * For drawing or scanout operations, rsp. buffer objects have to be pinned
91 * in video RAM. Call drm_gem_vram_pin() with &DRM_GEM_VRAM_PL_FLAG_VRAM or
92 * &DRM_GEM_VRAM_PL_FLAG_SYSTEM to pin a buffer object in video RAM or system
93 * memory. Call drm_gem_vram_unpin() to release the pinned object afterwards.
95 * A buffer object that is pinned in video RAM has a fixed address within that
96 * memory region. Call drm_gem_vram_offset() to retrieve this value. Typically
97 * it's used to program the hardware's scanout engine for framebuffers, set
98 * the cursor overlay's image for a mouse cursor, or use it as input to the
99 * hardware's drawing engine.
101 * To access a buffer object's memory from the DRM driver, call
102 * drm_gem_vram_vmap(). It maps the buffer into kernel address
103 * space and returns the memory address. Use drm_gem_vram_vunmap() to
104 * release the mapping.
108 * Buffer-objects helpers
111 static void drm_gem_vram_cleanup(struct drm_gem_vram_object *gbo)
113 /* We got here via ttm_bo_put(), which means that the
114 * TTM buffer object in 'bo' has already been cleaned
115 * up; only release the GEM object.
118 WARN_ON(gbo->vmap_use_count);
119 WARN_ON(iosys_map_is_set(&gbo->map));
121 drm_gem_object_release(&gbo->bo.base);
124 static void drm_gem_vram_destroy(struct drm_gem_vram_object *gbo)
126 drm_gem_vram_cleanup(gbo);
130 static void ttm_buffer_object_destroy(struct ttm_buffer_object *bo)
132 struct drm_gem_vram_object *gbo = drm_gem_vram_of_bo(bo);
134 drm_gem_vram_destroy(gbo);
137 static void drm_gem_vram_placement(struct drm_gem_vram_object *gbo,
138 unsigned long pl_flag)
140 u32 invariant_flags = 0;
144 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_TOPDOWN)
145 invariant_flags = TTM_PL_FLAG_TOPDOWN;
147 gbo->placement.placement = gbo->placements;
148 gbo->placement.busy_placement = gbo->placements;
150 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_VRAM) {
151 gbo->placements[c].mem_type = TTM_PL_VRAM;
152 gbo->placements[c++].flags = invariant_flags;
155 if (pl_flag & DRM_GEM_VRAM_PL_FLAG_SYSTEM || !c) {
156 gbo->placements[c].mem_type = TTM_PL_SYSTEM;
157 gbo->placements[c++].flags = invariant_flags;
160 gbo->placement.num_placement = c;
161 gbo->placement.num_busy_placement = c;
163 for (i = 0; i < c; ++i) {
164 gbo->placements[i].fpfn = 0;
165 gbo->placements[i].lpfn = 0;
170 * drm_gem_vram_create() - Creates a VRAM-backed GEM object
171 * @dev: the DRM device
172 * @size: the buffer size in bytes
173 * @pg_align: the buffer's alignment in multiples of the page size
175 * GEM objects are allocated by calling struct drm_driver.gem_create_object,
176 * if set. Otherwise kzalloc() will be used. Drivers can set their own GEM
177 * object functions in struct drm_driver.gem_create_object. If no functions
178 * are set, the new GEM object will use the default functions from GEM VRAM
182 * A new instance of &struct drm_gem_vram_object on success, or
183 * an ERR_PTR()-encoded error code otherwise.
185 struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
187 unsigned long pg_align)
189 struct drm_gem_vram_object *gbo;
190 struct drm_gem_object *gem;
191 struct drm_vram_mm *vmm = dev->vram_mm;
192 struct ttm_device *bdev;
195 if (WARN_ONCE(!vmm, "VRAM MM not initialized"))
196 return ERR_PTR(-EINVAL);
198 if (dev->driver->gem_create_object) {
199 gem = dev->driver->gem_create_object(dev, size);
201 return ERR_CAST(gem);
202 gbo = drm_gem_vram_of_gem(gem);
204 gbo = kzalloc(sizeof(*gbo), GFP_KERNEL);
206 return ERR_PTR(-ENOMEM);
211 gem->funcs = &drm_gem_vram_object_funcs;
213 ret = drm_gem_object_init(dev, gem, size);
222 drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
225 * A failing ttm_bo_init will call ttm_buffer_object_destroy
226 * to release gbo->bo.base and kfree gbo.
228 ret = ttm_bo_init(bdev, &gbo->bo, size, ttm_bo_type_device,
229 &gbo->placement, pg_align, false, NULL, NULL,
230 ttm_buffer_object_destroy);
236 EXPORT_SYMBOL(drm_gem_vram_create);
239 * drm_gem_vram_put() - Releases a reference to a VRAM-backed GEM object
240 * @gbo: the GEM VRAM object
242 * See ttm_bo_put() for more information.
244 void drm_gem_vram_put(struct drm_gem_vram_object *gbo)
246 ttm_bo_put(&gbo->bo);
248 EXPORT_SYMBOL(drm_gem_vram_put);
250 static u64 drm_gem_vram_pg_offset(struct drm_gem_vram_object *gbo)
252 /* Keep TTM behavior for now, remove when drivers are audited */
253 if (WARN_ON_ONCE(!gbo->bo.resource ||
254 gbo->bo.resource->mem_type == TTM_PL_SYSTEM))
257 return gbo->bo.resource->start;
261 * drm_gem_vram_offset() - \
262 Returns a GEM VRAM object's offset in video memory
263 * @gbo: the GEM VRAM object
265 * This function returns the buffer object's offset in the device's video
266 * memory. The buffer object has to be pinned to %TTM_PL_VRAM.
269 * The buffer object's offset in video memory on success, or
270 * a negative errno code otherwise.
272 s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo)
274 if (WARN_ON_ONCE(!gbo->bo.pin_count))
276 return drm_gem_vram_pg_offset(gbo) << PAGE_SHIFT;
278 EXPORT_SYMBOL(drm_gem_vram_offset);
280 static int drm_gem_vram_pin_locked(struct drm_gem_vram_object *gbo,
281 unsigned long pl_flag)
283 struct ttm_operation_ctx ctx = { false, false };
286 if (gbo->bo.pin_count)
290 drm_gem_vram_placement(gbo, pl_flag);
292 ret = ttm_bo_validate(&gbo->bo, &gbo->placement, &ctx);
297 ttm_bo_pin(&gbo->bo);
303 * drm_gem_vram_pin() - Pins a GEM VRAM object in a region.
304 * @gbo: the GEM VRAM object
305 * @pl_flag: a bitmask of possible memory regions
307 * Pinning a buffer object ensures that it is not evicted from
308 * a memory region. A pinned buffer object has to be unpinned before
309 * it can be pinned to another region. If the pl_flag argument is 0,
310 * the buffer is pinned at its current location (video RAM or system
313 * Small buffer objects, such as cursor images, can lead to memory
314 * fragmentation if they are pinned in the middle of video RAM. This
315 * is especially a problem on devices with only a small amount of
316 * video RAM. Fragmentation can prevent the primary framebuffer from
317 * fitting in, even though there's enough memory overall. The modifier
318 * DRM_GEM_VRAM_PL_FLAG_TOPDOWN marks the buffer object to be pinned
319 * at the high end of the memory region to avoid fragmentation.
323 * a negative error code otherwise.
325 int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag)
329 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
332 ret = drm_gem_vram_pin_locked(gbo, pl_flag);
333 ttm_bo_unreserve(&gbo->bo);
337 EXPORT_SYMBOL(drm_gem_vram_pin);
339 static void drm_gem_vram_unpin_locked(struct drm_gem_vram_object *gbo)
341 ttm_bo_unpin(&gbo->bo);
345 * drm_gem_vram_unpin() - Unpins a GEM VRAM object
346 * @gbo: the GEM VRAM object
350 * a negative error code otherwise.
352 int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo)
356 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
360 drm_gem_vram_unpin_locked(gbo);
361 ttm_bo_unreserve(&gbo->bo);
365 EXPORT_SYMBOL(drm_gem_vram_unpin);
367 static int drm_gem_vram_kmap_locked(struct drm_gem_vram_object *gbo,
368 struct iosys_map *map)
372 if (gbo->vmap_use_count > 0)
376 * VRAM helpers unmap the BO only on demand. So the previous
377 * page mapping might still be around. Only vmap if the there's
378 * no mapping present.
380 if (iosys_map_is_null(&gbo->map)) {
381 ret = ttm_bo_vmap(&gbo->bo, &gbo->map);
387 ++gbo->vmap_use_count;
393 static void drm_gem_vram_kunmap_locked(struct drm_gem_vram_object *gbo,
394 struct iosys_map *map)
396 struct drm_device *dev = gbo->bo.base.dev;
398 if (drm_WARN_ON_ONCE(dev, !gbo->vmap_use_count))
401 if (drm_WARN_ON_ONCE(dev, !iosys_map_is_equal(&gbo->map, map)))
402 return; /* BUG: map not mapped from this BO */
404 if (--gbo->vmap_use_count > 0)
408 * Permanently mapping and unmapping buffers adds overhead from
409 * updating the page tables and creates debugging output. Therefore,
410 * we delay the actual unmap operation until the BO gets evicted
411 * from memory. See drm_gem_vram_bo_driver_move_notify().
416 * drm_gem_vram_vmap() - Pins and maps a GEM VRAM object into kernel address
418 * @gbo: The GEM VRAM object to map
419 * @map: Returns the kernel virtual address of the VRAM GEM object's backing
422 * The vmap function pins a GEM VRAM object to its current location, either
423 * system or video memory, and maps its buffer into kernel address space.
424 * As pinned object cannot be relocated, you should avoid pinning objects
425 * permanently. Call drm_gem_vram_vunmap() with the returned address to
426 * unmap and unpin the GEM VRAM object.
429 * 0 on success, or a negative error code otherwise.
431 int drm_gem_vram_vmap(struct drm_gem_vram_object *gbo, struct iosys_map *map)
435 ret = ttm_bo_reserve(&gbo->bo, true, false, NULL);
439 ret = drm_gem_vram_pin_locked(gbo, 0);
441 goto err_ttm_bo_unreserve;
442 ret = drm_gem_vram_kmap_locked(gbo, map);
444 goto err_drm_gem_vram_unpin_locked;
446 ttm_bo_unreserve(&gbo->bo);
450 err_drm_gem_vram_unpin_locked:
451 drm_gem_vram_unpin_locked(gbo);
452 err_ttm_bo_unreserve:
453 ttm_bo_unreserve(&gbo->bo);
456 EXPORT_SYMBOL(drm_gem_vram_vmap);
459 * drm_gem_vram_vunmap() - Unmaps and unpins a GEM VRAM object
460 * @gbo: The GEM VRAM object to unmap
461 * @map: Kernel virtual address where the VRAM GEM object was mapped
463 * A call to drm_gem_vram_vunmap() unmaps and unpins a GEM VRAM buffer. See
464 * the documentation for drm_gem_vram_vmap() for more information.
466 void drm_gem_vram_vunmap(struct drm_gem_vram_object *gbo,
467 struct iosys_map *map)
471 ret = ttm_bo_reserve(&gbo->bo, false, false, NULL);
472 if (WARN_ONCE(ret, "ttm_bo_reserve_failed(): ret=%d\n", ret))
475 drm_gem_vram_kunmap_locked(gbo, map);
476 drm_gem_vram_unpin_locked(gbo);
478 ttm_bo_unreserve(&gbo->bo);
480 EXPORT_SYMBOL(drm_gem_vram_vunmap);
483 * drm_gem_vram_fill_create_dumb() - \
484 Helper for implementing &struct drm_driver.dumb_create
485 * @file: the DRM file
486 * @dev: the DRM device
487 * @pg_align: the buffer's alignment in multiples of the page size
488 * @pitch_align: the scanline's alignment in powers of 2
489 * @args: the arguments as provided to \
490 &struct drm_driver.dumb_create
492 * This helper function fills &struct drm_mode_create_dumb, which is used
493 * by &struct drm_driver.dumb_create. Implementations of this interface
494 * should forwards their arguments to this helper, plus the driver-specific
499 * a negative error code otherwise.
501 int drm_gem_vram_fill_create_dumb(struct drm_file *file,
502 struct drm_device *dev,
503 unsigned long pg_align,
504 unsigned long pitch_align,
505 struct drm_mode_create_dumb *args)
508 struct drm_gem_vram_object *gbo;
512 pitch = args->width * DIV_ROUND_UP(args->bpp, 8);
514 if (WARN_ON_ONCE(!is_power_of_2(pitch_align)))
516 pitch = ALIGN(pitch, pitch_align);
518 size = pitch * args->height;
520 size = roundup(size, PAGE_SIZE);
524 gbo = drm_gem_vram_create(dev, size, pg_align);
528 ret = drm_gem_handle_create(file, &gbo->bo.base, &handle);
530 goto err_drm_gem_object_put;
532 drm_gem_object_put(&gbo->bo.base);
536 args->handle = handle;
540 err_drm_gem_object_put:
541 drm_gem_object_put(&gbo->bo.base);
544 EXPORT_SYMBOL(drm_gem_vram_fill_create_dumb);
547 * Helpers for struct ttm_device_funcs
550 static bool drm_is_gem_vram(struct ttm_buffer_object *bo)
552 return (bo->destroy == ttm_buffer_object_destroy);
555 static void drm_gem_vram_bo_driver_evict_flags(struct drm_gem_vram_object *gbo,
556 struct ttm_placement *pl)
558 drm_gem_vram_placement(gbo, DRM_GEM_VRAM_PL_FLAG_SYSTEM);
559 *pl = gbo->placement;
562 static void drm_gem_vram_bo_driver_move_notify(struct drm_gem_vram_object *gbo)
564 struct ttm_buffer_object *bo = &gbo->bo;
565 struct drm_device *dev = bo->base.dev;
567 if (drm_WARN_ON_ONCE(dev, gbo->vmap_use_count))
570 ttm_bo_vunmap(bo, &gbo->map);
571 iosys_map_clear(&gbo->map); /* explicitly clear mapping for next vmap call */
574 static int drm_gem_vram_bo_driver_move(struct drm_gem_vram_object *gbo,
576 struct ttm_operation_ctx *ctx,
577 struct ttm_resource *new_mem)
579 drm_gem_vram_bo_driver_move_notify(gbo);
580 return ttm_bo_move_memcpy(&gbo->bo, ctx, new_mem);
584 * Helpers for struct drm_gem_object_funcs
588 * drm_gem_vram_object_free() - \
589 Implements &struct drm_gem_object_funcs.free
590 * @gem: GEM object. Refers to &struct drm_gem_vram_object.gem
592 static void drm_gem_vram_object_free(struct drm_gem_object *gem)
594 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
596 drm_gem_vram_put(gbo);
600 * Helpers for dump buffers
604 * drm_gem_vram_driver_dumb_create() - \
605 Implements &struct drm_driver.dumb_create
606 * @file: the DRM file
607 * @dev: the DRM device
608 * @args: the arguments as provided to \
609 &struct drm_driver.dumb_create
611 * This function requires the driver to use @drm_device.vram_mm for its
612 * instance of VRAM MM.
616 * a negative error code otherwise.
618 int drm_gem_vram_driver_dumb_create(struct drm_file *file,
619 struct drm_device *dev,
620 struct drm_mode_create_dumb *args)
622 if (WARN_ONCE(!dev->vram_mm, "VRAM MM not initialized"))
625 return drm_gem_vram_fill_create_dumb(file, dev, 0, 0, args);
627 EXPORT_SYMBOL(drm_gem_vram_driver_dumb_create);
630 * Helpers for struct drm_plane_helper_funcs
634 * drm_gem_vram_plane_helper_prepare_fb() - \
635 * Implements &struct drm_plane_helper_funcs.prepare_fb
636 * @plane: a DRM plane
637 * @new_state: the plane's new state
639 * During plane updates, this function sets the plane's fence and
640 * pins the GEM VRAM objects of the plane's new framebuffer to VRAM.
641 * Call drm_gem_vram_plane_helper_cleanup_fb() to unpin them.
645 * a negative errno code otherwise.
648 drm_gem_vram_plane_helper_prepare_fb(struct drm_plane *plane,
649 struct drm_plane_state *new_state)
652 struct drm_gem_vram_object *gbo;
658 for (i = 0; i < ARRAY_SIZE(new_state->fb->obj); ++i) {
659 if (!new_state->fb->obj[i])
661 gbo = drm_gem_vram_of_gem(new_state->fb->obj[i]);
662 ret = drm_gem_vram_pin(gbo, DRM_GEM_VRAM_PL_FLAG_VRAM);
664 goto err_drm_gem_vram_unpin;
667 ret = drm_gem_plane_helper_prepare_fb(plane, new_state);
669 goto err_drm_gem_vram_unpin;
673 err_drm_gem_vram_unpin:
676 gbo = drm_gem_vram_of_gem(new_state->fb->obj[i]);
677 drm_gem_vram_unpin(gbo);
681 EXPORT_SYMBOL(drm_gem_vram_plane_helper_prepare_fb);
684 * drm_gem_vram_plane_helper_cleanup_fb() - \
685 * Implements &struct drm_plane_helper_funcs.cleanup_fb
686 * @plane: a DRM plane
687 * @old_state: the plane's old state
689 * During plane updates, this function unpins the GEM VRAM
690 * objects of the plane's old framebuffer from VRAM. Complements
691 * drm_gem_vram_plane_helper_prepare_fb().
694 drm_gem_vram_plane_helper_cleanup_fb(struct drm_plane *plane,
695 struct drm_plane_state *old_state)
698 struct drm_gem_vram_object *gbo;
703 for (i = 0; i < ARRAY_SIZE(old_state->fb->obj); ++i) {
704 if (!old_state->fb->obj[i])
706 gbo = drm_gem_vram_of_gem(old_state->fb->obj[i]);
707 drm_gem_vram_unpin(gbo);
710 EXPORT_SYMBOL(drm_gem_vram_plane_helper_cleanup_fb);
713 * Helpers for struct drm_simple_display_pipe_funcs
717 * drm_gem_vram_simple_display_pipe_prepare_fb() - \
718 * Implements &struct drm_simple_display_pipe_funcs.prepare_fb
719 * @pipe: a simple display pipe
720 * @new_state: the plane's new state
722 * During plane updates, this function pins the GEM VRAM
723 * objects of the plane's new framebuffer to VRAM. Call
724 * drm_gem_vram_simple_display_pipe_cleanup_fb() to unpin them.
728 * a negative errno code otherwise.
730 int drm_gem_vram_simple_display_pipe_prepare_fb(
731 struct drm_simple_display_pipe *pipe,
732 struct drm_plane_state *new_state)
734 return drm_gem_vram_plane_helper_prepare_fb(&pipe->plane, new_state);
736 EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_prepare_fb);
739 * drm_gem_vram_simple_display_pipe_cleanup_fb() - \
740 * Implements &struct drm_simple_display_pipe_funcs.cleanup_fb
741 * @pipe: a simple display pipe
742 * @old_state: the plane's old state
744 * During plane updates, this function unpins the GEM VRAM
745 * objects of the plane's old framebuffer from VRAM. Complements
746 * drm_gem_vram_simple_display_pipe_prepare_fb().
748 void drm_gem_vram_simple_display_pipe_cleanup_fb(
749 struct drm_simple_display_pipe *pipe,
750 struct drm_plane_state *old_state)
752 drm_gem_vram_plane_helper_cleanup_fb(&pipe->plane, old_state);
754 EXPORT_SYMBOL(drm_gem_vram_simple_display_pipe_cleanup_fb);
761 * drm_gem_vram_object_pin() - \
762 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() - \
786 Implements &struct drm_gem_object_funcs.unpin
787 * @gem: The GEM object to unpin
789 static void drm_gem_vram_object_unpin(struct drm_gem_object *gem)
791 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
793 drm_gem_vram_unpin(gbo);
797 * drm_gem_vram_object_vmap() -
798 * Implements &struct drm_gem_object_funcs.vmap
799 * @gem: The GEM object to map
800 * @map: Returns the kernel virtual address of the VRAM GEM object's backing
804 * 0 on success, or a negative error code otherwise.
806 static int drm_gem_vram_object_vmap(struct drm_gem_object *gem,
807 struct iosys_map *map)
809 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
811 return drm_gem_vram_vmap(gbo, map);
815 * drm_gem_vram_object_vunmap() -
816 * Implements &struct drm_gem_object_funcs.vunmap
817 * @gem: The GEM object to unmap
818 * @map: Kernel virtual address where the VRAM GEM object was mapped
820 static void drm_gem_vram_object_vunmap(struct drm_gem_object *gem,
821 struct iosys_map *map)
823 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
825 drm_gem_vram_vunmap(gbo, map);
832 static const struct drm_gem_object_funcs drm_gem_vram_object_funcs = {
833 .free = drm_gem_vram_object_free,
834 .pin = drm_gem_vram_object_pin,
835 .unpin = drm_gem_vram_object_unpin,
836 .vmap = drm_gem_vram_object_vmap,
837 .vunmap = drm_gem_vram_object_vunmap,
838 .mmap = drm_gem_ttm_mmap,
839 .print_info = drm_gem_ttm_print_info,
843 * VRAM memory manager
850 static void bo_driver_ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *tt)
860 static struct ttm_tt *bo_driver_ttm_tt_create(struct ttm_buffer_object *bo,
866 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
870 ret = ttm_tt_init(tt, bo, page_flags, ttm_cached, 0);
872 goto err_ttm_tt_init;
881 static void bo_driver_evict_flags(struct ttm_buffer_object *bo,
882 struct ttm_placement *placement)
884 struct drm_gem_vram_object *gbo;
886 /* TTM may pass BOs that are not GEM VRAM BOs. */
887 if (!drm_is_gem_vram(bo))
890 gbo = drm_gem_vram_of_bo(bo);
892 drm_gem_vram_bo_driver_evict_flags(gbo, placement);
895 static void bo_driver_delete_mem_notify(struct ttm_buffer_object *bo)
897 struct drm_gem_vram_object *gbo;
899 /* TTM may pass BOs that are not GEM VRAM BOs. */
900 if (!drm_is_gem_vram(bo))
903 gbo = drm_gem_vram_of_bo(bo);
905 drm_gem_vram_bo_driver_move_notify(gbo);
908 static int bo_driver_move(struct ttm_buffer_object *bo,
910 struct ttm_operation_ctx *ctx,
911 struct ttm_resource *new_mem,
912 struct ttm_place *hop)
914 struct drm_gem_vram_object *gbo;
916 gbo = drm_gem_vram_of_bo(bo);
918 return drm_gem_vram_bo_driver_move(gbo, evict, ctx, new_mem);
921 static int bo_driver_io_mem_reserve(struct ttm_device *bdev,
922 struct ttm_resource *mem)
924 struct drm_vram_mm *vmm = drm_vram_mm_of_bdev(bdev);
926 switch (mem->mem_type) {
927 case TTM_PL_SYSTEM: /* nothing to do */
930 mem->bus.offset = (mem->start << PAGE_SHIFT) + vmm->vram_base;
931 mem->bus.is_iomem = true;
932 mem->bus.caching = ttm_write_combined;
941 static struct ttm_device_funcs bo_driver = {
942 .ttm_tt_create = bo_driver_ttm_tt_create,
943 .ttm_tt_destroy = bo_driver_ttm_tt_destroy,
944 .eviction_valuable = ttm_bo_eviction_valuable,
945 .evict_flags = bo_driver_evict_flags,
946 .move = bo_driver_move,
947 .delete_mem_notify = bo_driver_delete_mem_notify,
948 .io_mem_reserve = bo_driver_io_mem_reserve,
955 static int drm_vram_mm_debugfs(struct seq_file *m, void *data)
957 struct drm_info_node *node = (struct drm_info_node *) m->private;
958 struct drm_vram_mm *vmm = node->minor->dev->vram_mm;
959 struct ttm_resource_manager *man = ttm_manager_type(&vmm->bdev, TTM_PL_VRAM);
960 struct drm_printer p = drm_seq_file_printer(m);
962 ttm_resource_manager_debug(man, &p);
966 static const struct drm_info_list drm_vram_mm_debugfs_list[] = {
967 { "vram-mm", drm_vram_mm_debugfs, 0, NULL },
971 * drm_vram_mm_debugfs_init() - Register VRAM MM debugfs file.
973 * @minor: drm minor device.
976 void drm_vram_mm_debugfs_init(struct drm_minor *minor)
978 drm_debugfs_create_files(drm_vram_mm_debugfs_list,
979 ARRAY_SIZE(drm_vram_mm_debugfs_list),
980 minor->debugfs_root, minor);
982 EXPORT_SYMBOL(drm_vram_mm_debugfs_init);
984 static int drm_vram_mm_init(struct drm_vram_mm *vmm, struct drm_device *dev,
985 uint64_t vram_base, size_t vram_size)
989 vmm->vram_base = vram_base;
990 vmm->vram_size = vram_size;
992 ret = ttm_device_init(&vmm->bdev, &bo_driver, dev->dev,
993 dev->anon_inode->i_mapping,
994 dev->vma_offset_manager,
999 ret = ttm_range_man_init(&vmm->bdev, TTM_PL_VRAM,
1000 false, vram_size >> PAGE_SHIFT);
1007 static void drm_vram_mm_cleanup(struct drm_vram_mm *vmm)
1009 ttm_range_man_fini(&vmm->bdev, TTM_PL_VRAM);
1010 ttm_device_fini(&vmm->bdev);
1014 * Helpers for integration with struct drm_device
1017 static struct drm_vram_mm *drm_vram_helper_alloc_mm(struct drm_device *dev, uint64_t vram_base,
1022 if (WARN_ON(dev->vram_mm))
1023 return dev->vram_mm;
1025 dev->vram_mm = kzalloc(sizeof(*dev->vram_mm), GFP_KERNEL);
1027 return ERR_PTR(-ENOMEM);
1029 ret = drm_vram_mm_init(dev->vram_mm, dev, vram_base, vram_size);
1033 return dev->vram_mm;
1036 kfree(dev->vram_mm);
1037 dev->vram_mm = NULL;
1038 return ERR_PTR(ret);
1041 static void drm_vram_helper_release_mm(struct drm_device *dev)
1046 drm_vram_mm_cleanup(dev->vram_mm);
1047 kfree(dev->vram_mm);
1048 dev->vram_mm = NULL;
1051 static void drm_vram_mm_release(struct drm_device *dev, void *ptr)
1053 drm_vram_helper_release_mm(dev);
1057 * drmm_vram_helper_init - Initializes a device's instance of
1058 * &struct drm_vram_mm
1059 * @dev: the DRM device
1060 * @vram_base: the base address of the video memory
1061 * @vram_size: the size of the video memory in bytes
1063 * Creates a new instance of &struct drm_vram_mm and stores it in
1064 * struct &drm_device.vram_mm. The instance is auto-managed and cleaned
1065 * up as part of device cleanup. Calling this function multiple times
1066 * will generate an error message.
1069 * 0 on success, or a negative errno code otherwise.
1071 int drmm_vram_helper_init(struct drm_device *dev, uint64_t vram_base,
1074 struct drm_vram_mm *vram_mm;
1076 if (drm_WARN_ON_ONCE(dev, dev->vram_mm))
1079 vram_mm = drm_vram_helper_alloc_mm(dev, vram_base, vram_size);
1080 if (IS_ERR(vram_mm))
1081 return PTR_ERR(vram_mm);
1082 return drmm_add_action_or_reset(dev, drm_vram_mm_release, NULL);
1084 EXPORT_SYMBOL(drmm_vram_helper_init);
1087 * Mode-config helpers
1090 static enum drm_mode_status
1091 drm_vram_helper_mode_valid_internal(struct drm_device *dev,
1092 const struct drm_display_mode *mode,
1093 unsigned long max_bpp)
1095 struct drm_vram_mm *vmm = dev->vram_mm;
1096 unsigned long fbsize, fbpages, max_fbpages;
1098 if (WARN_ON(!dev->vram_mm))
1101 max_fbpages = (vmm->vram_size / 2) >> PAGE_SHIFT;
1103 fbsize = mode->hdisplay * mode->vdisplay * max_bpp;
1104 fbpages = DIV_ROUND_UP(fbsize, PAGE_SIZE);
1106 if (fbpages > max_fbpages)
1113 * drm_vram_helper_mode_valid - Tests if a display mode's
1114 * framebuffer fits into the available video memory.
1115 * @dev: the DRM device
1116 * @mode: the mode to test
1118 * This function tests if enough video memory is available for using the
1119 * specified display mode. Atomic modesetting requires importing the
1120 * designated framebuffer into video memory before evicting the active
1121 * one. Hence, any framebuffer may consume at most half of the available
1122 * VRAM. Display modes that require a larger framebuffer can not be used,
1123 * even if the CRTC does support them. Each framebuffer is assumed to
1124 * have 32-bit color depth.
1127 * The function can only test if the display mode is supported in
1128 * general. If there are too many framebuffers pinned to video memory,
1129 * a display mode may still not be usable in practice. The color depth of
1130 * 32-bit fits all current use case. A more flexible test can be added
1134 * MODE_OK if the display mode is supported, or an error code of type
1135 * enum drm_mode_status otherwise.
1137 enum drm_mode_status
1138 drm_vram_helper_mode_valid(struct drm_device *dev,
1139 const struct drm_display_mode *mode)
1141 static const unsigned long max_bpp = 4; /* DRM_FORMAT_XRGB8888 */
1143 return drm_vram_helper_mode_valid_internal(dev, mode, max_bpp);
1145 EXPORT_SYMBOL(drm_vram_helper_mode_valid);
1147 MODULE_DESCRIPTION("DRM VRAM memory-management helpers");
1148 MODULE_LICENSE("GPL");