1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 #include <linux/module.h>
4 
5 #include <drm/drm_gem_ttm_helper.h>
6 
7 /**
8  * DOC: overview
9  *
10  * This library provides helper functions for gem objects backed by
11  * ttm.
12  */
13 
14 /**
15  * drm_gem_ttm_print_info() - Print &ttm_buffer_object info for debugfs
16  * @p: DRM printer
17  * @indent: Tab indentation level
18  * @gem: GEM object
19  *
20  * This function can be used as &drm_gem_object_funcs.print_info
21  * callback.
22  */
23 void drm_gem_ttm_print_info(struct drm_printer *p, unsigned int indent,
24 			    const struct drm_gem_object *gem)
25 {
26 	static const char * const plname[] = {
27 		[ TTM_PL_SYSTEM ] = "system",
28 		[ TTM_PL_TT     ] = "tt",
29 		[ TTM_PL_VRAM   ] = "vram",
30 		[ TTM_PL_PRIV   ] = "priv",
31 
32 		[ 16 ]            = "cached",
33 		[ 17 ]            = "uncached",
34 		[ 18 ]            = "wc",
35 		[ 19 ]            = "contig",
36 
37 		[ 21 ]            = "pinned", /* NO_EVICT */
38 		[ 22 ]            = "topdown",
39 	};
40 	const struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(gem);
41 
42 	drm_printf_indent(p, indent, "placement=");
43 	drm_print_bits(p, bo->resource->placement, plname, ARRAY_SIZE(plname));
44 	drm_printf(p, "\n");
45 
46 	if (bo->resource->bus.is_iomem)
47 		drm_printf_indent(p, indent, "bus.offset=%lx\n",
48 				  (unsigned long)bo->resource->bus.offset);
49 }
50 EXPORT_SYMBOL(drm_gem_ttm_print_info);
51 
52 /**
53  * drm_gem_ttm_vmap() - vmap &ttm_buffer_object
54  * @gem: GEM object.
55  * @map: [out] returns the dma-buf mapping.
56  *
57  * Maps a GEM object with ttm_bo_vmap(). This function can be used as
58  * &drm_gem_object_funcs.vmap callback.
59  *
60  * Returns:
61  * 0 on success, or a negative errno code otherwise.
62  */
63 int drm_gem_ttm_vmap(struct drm_gem_object *gem,
64 		     struct iosys_map *map)
65 {
66 	struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(gem);
67 	int ret;
68 
69 	dma_resv_lock(gem->resv, NULL);
70 	ret = ttm_bo_vmap(bo, map);
71 	dma_resv_unlock(gem->resv);
72 
73 	return ret;
74 }
75 EXPORT_SYMBOL(drm_gem_ttm_vmap);
76 
77 /**
78  * drm_gem_ttm_vunmap() - vunmap &ttm_buffer_object
79  * @gem: GEM object.
80  * @map: dma-buf mapping.
81  *
82  * Unmaps a GEM object with ttm_bo_vunmap(). This function can be used as
83  * &drm_gem_object_funcs.vmap callback.
84  */
85 void drm_gem_ttm_vunmap(struct drm_gem_object *gem,
86 			struct iosys_map *map)
87 {
88 	struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(gem);
89 
90 	dma_resv_lock(gem->resv, NULL);
91 	ttm_bo_vunmap(bo, map);
92 	dma_resv_unlock(gem->resv);
93 }
94 EXPORT_SYMBOL(drm_gem_ttm_vunmap);
95 
96 /**
97  * drm_gem_ttm_mmap() - mmap &ttm_buffer_object
98  * @gem: GEM object.
99  * @vma: vm area.
100  *
101  * This function can be used as &drm_gem_object_funcs.mmap
102  * callback.
103  */
104 int drm_gem_ttm_mmap(struct drm_gem_object *gem,
105 		     struct vm_area_struct *vma)
106 {
107 	struct ttm_buffer_object *bo = drm_gem_ttm_of_gem(gem);
108 	int ret;
109 
110 	ret = ttm_bo_mmap_obj(vma, bo);
111 	if (ret < 0)
112 		return ret;
113 
114 	/*
115 	 * ttm has its own object refcounting, so drop gem reference
116 	 * to avoid double accounting counting.
117 	 */
118 	drm_gem_object_put(gem);
119 
120 	return 0;
121 }
122 EXPORT_SYMBOL(drm_gem_ttm_mmap);
123 
124 /**
125  * drm_gem_ttm_dumb_map_offset() - Implements struct &drm_driver.dumb_map_offset
126  * @file:	DRM file pointer.
127  * @dev:	DRM device.
128  * @handle:	GEM handle
129  * @offset:	Returns the mapping's memory offset on success
130  *
131  * Provides an implementation of struct &drm_driver.dumb_map_offset for
132  * TTM-based GEM drivers. TTM allocates the offset internally and
133  * drm_gem_ttm_dumb_map_offset() returns it for dumb-buffer implementations.
134  *
135  * See struct &drm_driver.dumb_map_offset.
136  *
137  * Returns:
138  * 0 on success, or a negative errno code otherwise.
139  */
140 int drm_gem_ttm_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
141 				uint32_t handle, uint64_t *offset)
142 {
143 	struct drm_gem_object *gem;
144 
145 	gem = drm_gem_object_lookup(file, handle);
146 	if (!gem)
147 		return -ENOENT;
148 
149 	*offset = drm_vma_node_offset_addr(&gem->vma_node);
150 
151 	drm_gem_object_put(gem);
152 
153 	return 0;
154 }
155 EXPORT_SYMBOL(drm_gem_ttm_dumb_map_offset);
156 
157 MODULE_DESCRIPTION("DRM gem ttm helpers");
158 MODULE_LICENSE("GPL");
159