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