xref: /linux/include/drm/drm_gem_shmem_helper.h (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef __DRM_GEM_SHMEM_HELPER_H__
4 #define __DRM_GEM_SHMEM_HELPER_H__
5 
6 #include <linux/fs.h>
7 #include <linux/mm.h>
8 #include <linux/mutex.h>
9 
10 #include <drm/drm_file.h>
11 #include <drm/drm_gem.h>
12 #include <drm/drm_ioctl.h>
13 #include <drm/drm_prime.h>
14 
15 struct dma_buf_attachment;
16 struct drm_mode_create_dumb;
17 struct drm_printer;
18 struct sg_table;
19 
20 /**
21  * struct drm_gem_shmem_object - GEM object backed by shmem
22  */
23 struct drm_gem_shmem_object {
24 	/**
25 	 * @base: Base GEM object
26 	 */
27 	struct drm_gem_object base;
28 
29 	/**
30 	 * @pages: Page table
31 	 */
32 	struct page **pages;
33 
34 	/**
35 	 * @pages_use_count:
36 	 *
37 	 * Reference count on the pages table.
38 	 * The pages are put when the count reaches zero.
39 	 */
40 	unsigned int pages_use_count;
41 
42 	/**
43 	 * @madv: State for madvise
44 	 *
45 	 * 0 is active/inuse.
46 	 * A negative value is the object is purged.
47 	 * Positive values are driver specific and not used by the helpers.
48 	 */
49 	int madv;
50 
51 	/**
52 	 * @madv_list: List entry for madvise tracking
53 	 *
54 	 * Typically used by drivers to track purgeable objects
55 	 */
56 	struct list_head madv_list;
57 
58 	/**
59 	 * @sgt: Scatter/gather table for imported PRIME buffers
60 	 */
61 	struct sg_table *sgt;
62 
63 	/**
64 	 * @vaddr: Kernel virtual address of the backing memory
65 	 */
66 	void *vaddr;
67 
68 	/**
69 	 * @vmap_use_count:
70 	 *
71 	 * Reference count on the virtual address.
72 	 * The address are un-mapped when the count reaches zero.
73 	 */
74 	unsigned int vmap_use_count;
75 
76 	/**
77 	 * @pages_mark_dirty_on_put:
78 	 *
79 	 * Mark pages as dirty when they are put.
80 	 */
81 	bool pages_mark_dirty_on_put : 1;
82 
83 	/**
84 	 * @pages_mark_accessed_on_put:
85 	 *
86 	 * Mark pages as accessed when they are put.
87 	 */
88 	bool pages_mark_accessed_on_put : 1;
89 
90 	/**
91 	 * @map_wc: map object write-combined (instead of using shmem defaults).
92 	 */
93 	bool map_wc : 1;
94 };
95 
96 #define to_drm_gem_shmem_obj(obj) \
97 	container_of(obj, struct drm_gem_shmem_object, base)
98 
99 struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size);
100 struct drm_gem_shmem_object *drm_gem_shmem_create_with_mnt(struct drm_device *dev,
101 							   size_t size,
102 							   struct vfsmount *gemfs);
103 void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem);
104 
105 void drm_gem_shmem_put_pages(struct drm_gem_shmem_object *shmem);
106 int drm_gem_shmem_pin(struct drm_gem_shmem_object *shmem);
107 void drm_gem_shmem_unpin(struct drm_gem_shmem_object *shmem);
108 int drm_gem_shmem_vmap(struct drm_gem_shmem_object *shmem,
109 		       struct iosys_map *map);
110 void drm_gem_shmem_vunmap(struct drm_gem_shmem_object *shmem,
111 			  struct iosys_map *map);
112 int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct *vma);
113 
114 int drm_gem_shmem_pin_locked(struct drm_gem_shmem_object *shmem);
115 void drm_gem_shmem_unpin_locked(struct drm_gem_shmem_object *shmem);
116 
117 int drm_gem_shmem_madvise(struct drm_gem_shmem_object *shmem, int madv);
118 
119 static inline bool drm_gem_shmem_is_purgeable(struct drm_gem_shmem_object *shmem)
120 {
121 	return (shmem->madv > 0) &&
122 		!shmem->vmap_use_count && shmem->sgt &&
123 		!shmem->base.dma_buf && !shmem->base.import_attach;
124 }
125 
126 void drm_gem_shmem_purge(struct drm_gem_shmem_object *shmem);
127 
128 struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object *shmem);
129 struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem);
130 
131 void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem,
132 			      struct drm_printer *p, unsigned int indent);
133 
134 extern const struct vm_operations_struct drm_gem_shmem_vm_ops;
135 
136 /*
137  * GEM object functions
138  */
139 
140 /**
141  * drm_gem_shmem_object_free - GEM object function for drm_gem_shmem_free()
142  * @obj: GEM object to free
143  *
144  * This function wraps drm_gem_shmem_free(). Drivers that employ the shmem helpers
145  * should use it as their &drm_gem_object_funcs.free handler.
146  */
147 static inline void drm_gem_shmem_object_free(struct drm_gem_object *obj)
148 {
149 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
150 
151 	drm_gem_shmem_free(shmem);
152 }
153 
154 /**
155  * drm_gem_shmem_object_print_info() - Print &drm_gem_shmem_object info for debugfs
156  * @p: DRM printer
157  * @indent: Tab indentation level
158  * @obj: GEM object
159  *
160  * This function wraps drm_gem_shmem_print_info(). Drivers that employ the shmem helpers should
161  * use this function as their &drm_gem_object_funcs.print_info handler.
162  */
163 static inline void drm_gem_shmem_object_print_info(struct drm_printer *p, unsigned int indent,
164 						   const struct drm_gem_object *obj)
165 {
166 	const struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
167 
168 	drm_gem_shmem_print_info(shmem, p, indent);
169 }
170 
171 /**
172  * drm_gem_shmem_object_pin - GEM object function for drm_gem_shmem_pin()
173  * @obj: GEM object
174  *
175  * This function wraps drm_gem_shmem_pin(). Drivers that employ the shmem helpers should
176  * use it as their &drm_gem_object_funcs.pin handler.
177  */
178 static inline int drm_gem_shmem_object_pin(struct drm_gem_object *obj)
179 {
180 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
181 
182 	return drm_gem_shmem_pin_locked(shmem);
183 }
184 
185 /**
186  * drm_gem_shmem_object_unpin - GEM object function for drm_gem_shmem_unpin()
187  * @obj: GEM object
188  *
189  * This function wraps drm_gem_shmem_unpin(). Drivers that employ the shmem helpers should
190  * use it as their &drm_gem_object_funcs.unpin handler.
191  */
192 static inline void drm_gem_shmem_object_unpin(struct drm_gem_object *obj)
193 {
194 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
195 
196 	drm_gem_shmem_unpin_locked(shmem);
197 }
198 
199 /**
200  * drm_gem_shmem_object_get_sg_table - GEM object function for drm_gem_shmem_get_sg_table()
201  * @obj: GEM object
202  *
203  * This function wraps drm_gem_shmem_get_sg_table(). Drivers that employ the shmem helpers should
204  * use it as their &drm_gem_object_funcs.get_sg_table handler.
205  *
206  * Returns:
207  * A pointer to the scatter/gather table of pinned pages or error pointer on failure.
208  */
209 static inline struct sg_table *drm_gem_shmem_object_get_sg_table(struct drm_gem_object *obj)
210 {
211 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
212 
213 	return drm_gem_shmem_get_sg_table(shmem);
214 }
215 
216 /*
217  * drm_gem_shmem_object_vmap - GEM object function for drm_gem_shmem_vmap()
218  * @obj: GEM object
219  * @map: Returns the kernel virtual address of the SHMEM GEM object's backing store.
220  *
221  * This function wraps drm_gem_shmem_vmap(). Drivers that employ the shmem helpers should
222  * use it as their &drm_gem_object_funcs.vmap handler.
223  *
224  * Returns:
225  * 0 on success or a negative error code on failure.
226  */
227 static inline int drm_gem_shmem_object_vmap(struct drm_gem_object *obj,
228 					    struct iosys_map *map)
229 {
230 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
231 
232 	return drm_gem_shmem_vmap(shmem, map);
233 }
234 
235 /*
236  * drm_gem_shmem_object_vunmap - GEM object function for drm_gem_shmem_vunmap()
237  * @obj: GEM object
238  * @map: Kernel virtual address where the SHMEM GEM object was mapped
239  *
240  * This function wraps drm_gem_shmem_vunmap(). Drivers that employ the shmem helpers should
241  * use it as their &drm_gem_object_funcs.vunmap handler.
242  */
243 static inline void drm_gem_shmem_object_vunmap(struct drm_gem_object *obj,
244 					       struct iosys_map *map)
245 {
246 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
247 
248 	drm_gem_shmem_vunmap(shmem, map);
249 }
250 
251 /**
252  * drm_gem_shmem_object_mmap - GEM object function for drm_gem_shmem_mmap()
253  * @obj: GEM object
254  * @vma: VMA for the area to be mapped
255  *
256  * This function wraps drm_gem_shmem_mmap(). Drivers that employ the shmem helpers should
257  * use it as their &drm_gem_object_funcs.mmap handler.
258  *
259  * Returns:
260  * 0 on success or a negative error code on failure.
261  */
262 static inline int drm_gem_shmem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
263 {
264 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
265 
266 	return drm_gem_shmem_mmap(shmem, vma);
267 }
268 
269 /*
270  * Driver ops
271  */
272 
273 struct drm_gem_object *
274 drm_gem_shmem_prime_import_sg_table(struct drm_device *dev,
275 				    struct dma_buf_attachment *attach,
276 				    struct sg_table *sgt);
277 int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev,
278 			      struct drm_mode_create_dumb *args);
279 
280 /**
281  * DRM_GEM_SHMEM_DRIVER_OPS - Default shmem GEM operations
282  *
283  * This macro provides a shortcut for setting the shmem GEM operations in
284  * the &drm_driver structure.
285  */
286 #define DRM_GEM_SHMEM_DRIVER_OPS \
287 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table, \
288 	.dumb_create		   = drm_gem_shmem_dumb_create
289 
290 #endif /* __DRM_GEM_SHMEM_HELPER_H__ */
291