1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <drm/drm_gem_vram_helper.h>
4 #include <drm/ttm/ttm_page_alloc.h>
9 * This library provides a GEM buffer object that is backed by video RAM
10 * (VRAM). It can be used for framebuffer devices with dedicated memory.
14 * Buffer-objects helpers
17 static void drm_gem_vram_cleanup(struct drm_gem_vram_object *gbo)
19 /* We got here via ttm_bo_put(), which means that the
20 * TTM buffer object in 'bo' has already been cleaned
21 * up; only release the GEM object.
23 drm_gem_object_release(&gbo->gem);
26 static void drm_gem_vram_destroy(struct drm_gem_vram_object *gbo)
28 drm_gem_vram_cleanup(gbo);
32 static void ttm_buffer_object_destroy(struct ttm_buffer_object *bo)
34 struct drm_gem_vram_object *gbo = drm_gem_vram_of_bo(bo);
36 drm_gem_vram_destroy(gbo);
39 static void drm_gem_vram_placement(struct drm_gem_vram_object *gbo,
40 unsigned long pl_flag)
45 gbo->placement.placement = gbo->placements;
46 gbo->placement.busy_placement = gbo->placements;
48 if (pl_flag & TTM_PL_FLAG_VRAM)
49 gbo->placements[c++].flags = TTM_PL_FLAG_WC |
50 TTM_PL_FLAG_UNCACHED |
53 if (pl_flag & TTM_PL_FLAG_SYSTEM)
54 gbo->placements[c++].flags = TTM_PL_MASK_CACHING |
58 gbo->placements[c++].flags = TTM_PL_MASK_CACHING |
61 gbo->placement.num_placement = c;
62 gbo->placement.num_busy_placement = c;
64 for (i = 0; i < c; ++i) {
65 gbo->placements[i].fpfn = 0;
66 gbo->placements[i].lpfn = 0;
70 static int drm_gem_vram_init(struct drm_device *dev,
71 struct ttm_bo_device *bdev,
72 struct drm_gem_vram_object *gbo,
73 size_t size, unsigned long pg_align,
79 ret = drm_gem_object_init(dev, &gbo->gem, size);
83 acc_size = ttm_bo_dma_acc_size(bdev, size, sizeof(*gbo));
86 drm_gem_vram_placement(gbo, TTM_PL_FLAG_VRAM | TTM_PL_FLAG_SYSTEM);
88 ret = ttm_bo_init(bdev, &gbo->bo, size, ttm_bo_type_device,
89 &gbo->placement, pg_align, interruptible, acc_size,
90 NULL, NULL, ttm_buffer_object_destroy);
92 goto err_drm_gem_object_release;
96 err_drm_gem_object_release:
97 drm_gem_object_release(&gbo->gem);
102 * drm_gem_vram_create() - Creates a VRAM-backed GEM object
103 * @dev: the DRM device
104 * @bdev: the TTM BO device backing the object
105 * @size: the buffer size in bytes
106 * @pg_align: the buffer's alignment in multiples of the page size
107 * @interruptible: sleep interruptible if waiting for memory
110 * A new instance of &struct drm_gem_vram_object on success, or
111 * an ERR_PTR()-encoded error code otherwise.
113 struct drm_gem_vram_object *drm_gem_vram_create(struct drm_device *dev,
114 struct ttm_bo_device *bdev,
116 unsigned long pg_align,
119 struct drm_gem_vram_object *gbo;
122 gbo = kzalloc(sizeof(*gbo), GFP_KERNEL);
124 return ERR_PTR(-ENOMEM);
126 ret = drm_gem_vram_init(dev, bdev, gbo, size, pg_align, interruptible);
136 EXPORT_SYMBOL(drm_gem_vram_create);
139 * drm_gem_vram_put() - Releases a reference to a VRAM-backed GEM object
140 * @gbo: the GEM VRAM object
142 * See ttm_bo_put() for more information.
144 void drm_gem_vram_put(struct drm_gem_vram_object *gbo)
146 ttm_bo_put(&gbo->bo);
148 EXPORT_SYMBOL(drm_gem_vram_put);
151 * drm_gem_vram_reserve() - Reserves a VRAM-backed GEM object
152 * @gbo: the GEM VRAM object
153 * @no_wait: don't wait for buffer object to become available
155 * See ttm_bo_reserve() for more information.
159 * a negative error code otherwise
161 int drm_gem_vram_reserve(struct drm_gem_vram_object *gbo, bool no_wait)
163 return ttm_bo_reserve(&gbo->bo, true, no_wait, NULL);
165 EXPORT_SYMBOL(drm_gem_vram_reserve);
168 * drm_gem_vram_unreserve() - \
169 Release a reservation acquired by drm_gem_vram_reserve()
170 * @gbo: the GEM VRAM object
172 * See ttm_bo_unreserve() for more information.
174 void drm_gem_vram_unreserve(struct drm_gem_vram_object *gbo)
176 ttm_bo_unreserve(&gbo->bo);
178 EXPORT_SYMBOL(drm_gem_vram_unreserve);
181 * drm_gem_vram_mmap_offset() - Returns a GEM VRAM object's mmap offset
182 * @gbo: the GEM VRAM object
184 * See drm_vma_node_offset_addr() for more information.
187 * The buffer object's offset for userspace mappings on success, or
188 * 0 if no offset is allocated.
190 u64 drm_gem_vram_mmap_offset(struct drm_gem_vram_object *gbo)
192 return drm_vma_node_offset_addr(&gbo->bo.vma_node);
194 EXPORT_SYMBOL(drm_gem_vram_mmap_offset);
197 * drm_gem_vram_offset() - \
198 Returns a GEM VRAM object's offset in video memory
199 * @gbo: the GEM VRAM object
201 * This function returns the buffer object's offset in the device's video
202 * memory. The buffer object has to be pinned to %TTM_PL_VRAM.
205 * The buffer object's offset in video memory on success, or
206 * a negative errno code otherwise.
208 s64 drm_gem_vram_offset(struct drm_gem_vram_object *gbo)
210 if (WARN_ON_ONCE(!gbo->pin_count))
212 return gbo->bo.offset;
214 EXPORT_SYMBOL(drm_gem_vram_offset);
217 * drm_gem_vram_pin() - Pins a GEM VRAM object in a region.
218 * @gbo: the GEM VRAM object
219 * @pl_flag: a bitmask of possible memory regions
221 * Pinning a buffer object ensures that it is not evicted from
222 * a memory region. A pinned buffer object has to be unpinned before
223 * it can be pinned to another region.
227 * a negative error code otherwise.
229 int drm_gem_vram_pin(struct drm_gem_vram_object *gbo, unsigned long pl_flag)
232 struct ttm_operation_ctx ctx = { false, false };
234 if (gbo->pin_count) {
239 drm_gem_vram_placement(gbo, pl_flag);
240 for (i = 0; i < gbo->placement.num_placement; ++i)
241 gbo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
243 ret = ttm_bo_validate(&gbo->bo, &gbo->placement, &ctx);
251 EXPORT_SYMBOL(drm_gem_vram_pin);
254 * drm_gem_vram_unpin() - Unpins a GEM VRAM object
255 * @gbo: the GEM VRAM object
259 * a negative error code otherwise.
261 int drm_gem_vram_unpin(struct drm_gem_vram_object *gbo)
264 struct ttm_operation_ctx ctx = { false, false };
266 if (WARN_ON_ONCE(!gbo->pin_count))
273 for (i = 0; i < gbo->placement.num_placement ; ++i)
274 gbo->placements[i].flags &= ~TTM_PL_FLAG_NO_EVICT;
276 ret = ttm_bo_validate(&gbo->bo, &gbo->placement, &ctx);
282 EXPORT_SYMBOL(drm_gem_vram_unpin);
285 * drm_gem_vram_push_to_system() - \
286 Unpins a GEM VRAM object and moves it to system memory
287 * @gbo: the GEM VRAM object
289 * This operation only works if the caller holds the final pin on the
294 * a negative error code otherwise.
296 int drm_gem_vram_push_to_system(struct drm_gem_vram_object *gbo)
299 struct ttm_operation_ctx ctx = { false, false };
301 if (WARN_ON_ONCE(!gbo->pin_count))
308 if (gbo->kmap.virtual)
309 ttm_bo_kunmap(&gbo->kmap);
311 drm_gem_vram_placement(gbo, TTM_PL_FLAG_SYSTEM);
312 for (i = 0; i < gbo->placement.num_placement ; ++i)
313 gbo->placements[i].flags |= TTM_PL_FLAG_NO_EVICT;
315 ret = ttm_bo_validate(&gbo->bo, &gbo->placement, &ctx);
321 EXPORT_SYMBOL(drm_gem_vram_push_to_system);
324 * drm_gem_vram_kmap_at() - Maps a GEM VRAM object into kernel address space
325 * @gbo: the GEM VRAM object
326 * @map: establish a mapping if necessary
327 * @is_iomem: returns true if the mapped memory is I/O memory, or false \
328 otherwise; can be NULL
329 * @kmap: the mapping's kmap object
331 * This function maps the buffer object into the kernel's address space
332 * or returns the current mapping. If the parameter map is false, the
333 * function only queries the current mapping, but does not establish a
337 * The buffers virtual address if mapped, or
338 * NULL if not mapped, or
339 * an ERR_PTR()-encoded error code otherwise.
341 void *drm_gem_vram_kmap_at(struct drm_gem_vram_object *gbo, bool map,
342 bool *is_iomem, struct ttm_bo_kmap_obj *kmap)
346 if (kmap->virtual || !map)
349 ret = ttm_bo_kmap(&gbo->bo, 0, gbo->bo.num_pages, kmap);
355 return kmap->virtual;
356 if (!kmap->virtual) {
360 return ttm_kmap_obj_virtual(kmap, is_iomem);
362 EXPORT_SYMBOL(drm_gem_vram_kmap_at);
365 * drm_gem_vram_kmap() - Maps a GEM VRAM object into kernel address space
366 * @gbo: the GEM VRAM object
367 * @map: establish a mapping if necessary
368 * @is_iomem: returns true if the mapped memory is I/O memory, or false \
369 otherwise; can be NULL
371 * This function maps the buffer object into the kernel's address space
372 * or returns the current mapping. If the parameter map is false, the
373 * function only queries the current mapping, but does not establish a
377 * The buffers virtual address if mapped, or
378 * NULL if not mapped, or
379 * an ERR_PTR()-encoded error code otherwise.
381 void *drm_gem_vram_kmap(struct drm_gem_vram_object *gbo, bool map,
384 return drm_gem_vram_kmap_at(gbo, map, is_iomem, &gbo->kmap);
386 EXPORT_SYMBOL(drm_gem_vram_kmap);
389 * drm_gem_vram_kunmap_at() - Unmaps a GEM VRAM object
390 * @gbo: the GEM VRAM object
391 * @kmap: the mapping's kmap object
393 void drm_gem_vram_kunmap_at(struct drm_gem_vram_object *gbo,
394 struct ttm_bo_kmap_obj *kmap)
400 kmap->virtual = NULL;
402 EXPORT_SYMBOL(drm_gem_vram_kunmap_at);
405 * drm_gem_vram_kunmap() - Unmaps a GEM VRAM object
406 * @gbo: the GEM VRAM object
408 void drm_gem_vram_kunmap(struct drm_gem_vram_object *gbo)
410 drm_gem_vram_kunmap_at(gbo, &gbo->kmap);
412 EXPORT_SYMBOL(drm_gem_vram_kunmap);
415 * Helpers for struct ttm_bo_driver
418 static bool drm_is_gem_vram(struct ttm_buffer_object *bo)
420 return (bo->destroy == ttm_buffer_object_destroy);
424 * drm_gem_vram_bo_driver_evict_flags() - \
425 Implements &struct ttm_bo_driver.evict_flags
426 * @bo: TTM buffer object. Refers to &struct drm_gem_vram_object.bo
427 * @pl: TTM placement information.
429 void drm_gem_vram_bo_driver_evict_flags(struct ttm_buffer_object *bo,
430 struct ttm_placement *pl)
432 struct drm_gem_vram_object *gbo;
434 /* TTM may pass BOs that are not GEM VRAM BOs. */
435 if (!drm_is_gem_vram(bo))
438 gbo = drm_gem_vram_of_bo(bo);
439 drm_gem_vram_placement(gbo, TTM_PL_FLAG_SYSTEM);
440 *pl = gbo->placement;
442 EXPORT_SYMBOL(drm_gem_vram_bo_driver_evict_flags);
445 * drm_gem_vram_bo_driver_verify_access() - \
446 Implements &struct ttm_bo_driver.verify_access
447 * @bo: TTM buffer object. Refers to &struct drm_gem_vram_object.bo
448 * @filp: File pointer.
452 * a negative errno code otherwise.
454 int drm_gem_vram_bo_driver_verify_access(struct ttm_buffer_object *bo,
457 struct drm_gem_vram_object *gbo = drm_gem_vram_of_bo(bo);
459 return drm_vma_node_verify_access(&gbo->gem.vma_node,
462 EXPORT_SYMBOL(drm_gem_vram_bo_driver_verify_access);
465 * Helpers for struct drm_driver
469 * drm_gem_vram_driver_gem_free_object_unlocked() - \
470 Implements &struct drm_driver.gem_free_object_unlocked
471 * @gem: GEM object. Refers to &struct drm_gem_vram_object.gem
473 void drm_gem_vram_driver_gem_free_object_unlocked(struct drm_gem_object *gem)
475 struct drm_gem_vram_object *gbo = drm_gem_vram_of_gem(gem);
477 drm_gem_vram_put(gbo);
479 EXPORT_SYMBOL(drm_gem_vram_driver_gem_free_object_unlocked);
482 * drm_gem_vram_driver_dumb_mmap_offset() - \
483 Implements &struct drm_driver.dumb_mmap_offset
484 * @file: DRM file pointer.
486 * @handle: GEM handle
487 * @offset: Returns the mapping's memory offset on success
491 * a negative errno code otherwise.
493 int drm_gem_vram_driver_dumb_mmap_offset(struct drm_file *file,
494 struct drm_device *dev,
495 uint32_t handle, uint64_t *offset)
497 struct drm_gem_object *gem;
498 struct drm_gem_vram_object *gbo;
500 gem = drm_gem_object_lookup(file, handle);
504 gbo = drm_gem_vram_of_gem(gem);
505 *offset = drm_gem_vram_mmap_offset(gbo);
507 drm_gem_object_put_unlocked(gem);
511 EXPORT_SYMBOL(drm_gem_vram_driver_dumb_mmap_offset);