1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/module.h>
5 #include <drm/drm_gem_ttm_helper.h>
6 #include <drm/ttm/ttm_placement.h>
7 #include <drm/ttm/ttm_tt.h>
12 * This library provides helper functions for gem objects backed by
17 * drm_gem_ttm_print_info() - Print &ttm_buffer_object info for debugfs
19 * @indent: Tab indentation level
22 * This function can be used as &drm_gem_object_funcs.print_info
25 void drm_gem_ttm_print_info(struct drm_printer *p, unsigned int indent,
26 const struct drm_gem_object *gem)
28 static const char * const plname[] = {
29 [ TTM_PL_SYSTEM ] = "system",
31 [ TTM_PL_VRAM ] = "vram",
32 [ TTM_PL_PRIV ] = "priv",
39 [ 21 ] = "pinned", /* NO_EVICT */
42 const struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(gem);
44 drm_printf_indent(p, indent, "placement=");
45 drm_print_bits(p, bo->resource->placement, plname, ARRAY_SIZE(plname));
48 if (bo->resource->bus.is_iomem)
49 drm_printf_indent(p, indent, "bus.offset=%lx\n",
50 (unsigned long)bo->resource->bus.offset);
52 EXPORT_SYMBOL(drm_gem_ttm_print_info);
55 * drm_gem_ttm_vmap() - vmap &ttm_buffer_object
57 * @map: [out] returns the dma-buf mapping.
59 * Maps a GEM object with ttm_bo_vmap(). This function can be used as
60 * &drm_gem_object_funcs.vmap callback.
63 * 0 on success, or a negative errno code otherwise.
65 int drm_gem_ttm_vmap(struct drm_gem_object *gem,
66 struct iosys_map *map)
68 struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(gem);
70 return ttm_bo_vmap(bo, map);
72 EXPORT_SYMBOL(drm_gem_ttm_vmap);
75 * drm_gem_ttm_vunmap() - vunmap &ttm_buffer_object
77 * @map: dma-buf mapping.
79 * Unmaps a GEM object with ttm_bo_vunmap(). This function can be used as
80 * &drm_gem_object_funcs.vmap callback.
82 void drm_gem_ttm_vunmap(struct drm_gem_object *gem,
83 struct iosys_map *map)
85 struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(gem);
87 ttm_bo_vunmap(bo, map);
89 EXPORT_SYMBOL(drm_gem_ttm_vunmap);
92 * drm_gem_ttm_mmap() - mmap &ttm_buffer_object
96 * This function can be used as &drm_gem_object_funcs.mmap
99 int drm_gem_ttm_mmap(struct drm_gem_object *gem,
100 struct vm_area_struct *vma)
102 struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(gem);
105 ret = ttm_bo_mmap_obj(vma, bo);
110 * ttm has its own object refcounting, so drop gem reference
111 * to avoid double accounting counting.
113 drm_gem_object_put(gem);
117 EXPORT_SYMBOL(drm_gem_ttm_mmap);
120 * drm_gem_ttm_dumb_map_offset() - Implements struct &drm_driver.dumb_map_offset
121 * @file: DRM file pointer.
123 * @handle: GEM handle
124 * @offset: Returns the mapping's memory offset on success
126 * Provides an implementation of struct &drm_driver.dumb_map_offset for
127 * TTM-based GEM drivers. TTM allocates the offset internally and
128 * drm_gem_ttm_dumb_map_offset() returns it for dumb-buffer implementations.
130 * See struct &drm_driver.dumb_map_offset.
133 * 0 on success, or a negative errno code otherwise.
135 int drm_gem_ttm_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
136 uint32_t handle, uint64_t *offset)
138 struct drm_gem_object *gem;
140 gem = drm_gem_object_lookup(file, handle);
144 *offset = drm_vma_node_offset_addr(&gem->vma_node);
146 drm_gem_object_put(gem);
150 EXPORT_SYMBOL(drm_gem_ttm_dumb_map_offset);
152 MODULE_DESCRIPTION("DRM gem ttm helpers");
153 MODULE_LICENSE("GPL");