xref: /linux/include/drm/drm_gem_shmem_helper.h (revision a193f3b4e050e35c506a34d0870c838d8e0b0449)
12194a63aSNoralf Trønnes /* SPDX-License-Identifier: GPL-2.0 */
22194a63aSNoralf Trønnes 
32194a63aSNoralf Trønnes #ifndef __DRM_GEM_SHMEM_HELPER_H__
42194a63aSNoralf Trønnes #define __DRM_GEM_SHMEM_HELPER_H__
52194a63aSNoralf Trønnes 
62194a63aSNoralf Trønnes #include <linux/fs.h>
72194a63aSNoralf Trønnes #include <linux/mm.h>
82194a63aSNoralf Trønnes #include <linux/mutex.h>
92194a63aSNoralf Trønnes 
102194a63aSNoralf Trønnes #include <drm/drm_file.h>
112194a63aSNoralf Trønnes #include <drm/drm_gem.h>
122194a63aSNoralf Trønnes #include <drm/drm_ioctl.h>
132194a63aSNoralf Trønnes #include <drm/drm_prime.h>
142194a63aSNoralf Trønnes 
152194a63aSNoralf Trønnes struct dma_buf_attachment;
162194a63aSNoralf Trønnes struct drm_mode_create_dumb;
172194a63aSNoralf Trønnes struct drm_printer;
182194a63aSNoralf Trønnes struct sg_table;
192194a63aSNoralf Trønnes 
202194a63aSNoralf Trønnes /**
212194a63aSNoralf Trønnes  * struct drm_gem_shmem_object - GEM object backed by shmem
222194a63aSNoralf Trønnes  */
232194a63aSNoralf Trønnes struct drm_gem_shmem_object {
242194a63aSNoralf Trønnes 	/**
252194a63aSNoralf Trønnes 	 * @base: Base GEM object
262194a63aSNoralf Trønnes 	 */
272194a63aSNoralf Trønnes 	struct drm_gem_object base;
282194a63aSNoralf Trønnes 
292194a63aSNoralf Trønnes 	/**
302194a63aSNoralf Trønnes 	 * @pages_lock: Protects the page table and use count
312194a63aSNoralf Trønnes 	 */
322194a63aSNoralf Trønnes 	struct mutex pages_lock;
332194a63aSNoralf Trønnes 
342194a63aSNoralf Trønnes 	/**
352194a63aSNoralf Trønnes 	 * @pages: Page table
362194a63aSNoralf Trønnes 	 */
372194a63aSNoralf Trønnes 	struct page **pages;
382194a63aSNoralf Trønnes 
392194a63aSNoralf Trønnes 	/**
402194a63aSNoralf Trønnes 	 * @pages_use_count:
412194a63aSNoralf Trønnes 	 *
422194a63aSNoralf Trønnes 	 * Reference count on the pages table.
432194a63aSNoralf Trønnes 	 * The pages are put when the count reaches zero.
442194a63aSNoralf Trønnes 	 */
452194a63aSNoralf Trønnes 	unsigned int pages_use_count;
462194a63aSNoralf Trønnes 
47105401b6SRob Herring 	/**
48105401b6SRob Herring 	 * @madv: State for madvise
49105401b6SRob Herring 	 *
50105401b6SRob Herring 	 * 0 is active/inuse.
51105401b6SRob Herring 	 * A negative value is the object is purged.
52105401b6SRob Herring 	 * Positive values are driver specific and not used by the helpers.
53105401b6SRob Herring 	 */
5417acb9f3SRob Herring 	int madv;
55105401b6SRob Herring 
56105401b6SRob Herring 	/**
57105401b6SRob Herring 	 * @madv_list: List entry for madvise tracking
58105401b6SRob Herring 	 *
59105401b6SRob Herring 	 * Typically used by drivers to track purgeable objects
60105401b6SRob Herring 	 */
6117acb9f3SRob Herring 	struct list_head madv_list;
6217acb9f3SRob Herring 
632194a63aSNoralf Trønnes 	/**
642194a63aSNoralf Trønnes 	 * @pages_mark_dirty_on_put:
652194a63aSNoralf Trønnes 	 *
662194a63aSNoralf Trønnes 	 * Mark pages as dirty when they are put.
672194a63aSNoralf Trønnes 	 */
682194a63aSNoralf Trønnes 	unsigned int pages_mark_dirty_on_put    : 1;
692194a63aSNoralf Trønnes 
702194a63aSNoralf Trønnes 	/**
712194a63aSNoralf Trønnes 	 * @pages_mark_accessed_on_put:
722194a63aSNoralf Trønnes 	 *
732194a63aSNoralf Trønnes 	 * Mark pages as accessed when they are put.
742194a63aSNoralf Trønnes 	 */
752194a63aSNoralf Trønnes 	unsigned int pages_mark_accessed_on_put : 1;
762194a63aSNoralf Trønnes 
772194a63aSNoralf Trønnes 	/**
782194a63aSNoralf Trønnes 	 * @sgt: Scatter/gather table for imported PRIME buffers
792194a63aSNoralf Trønnes 	 */
802194a63aSNoralf Trønnes 	struct sg_table *sgt;
812194a63aSNoralf Trønnes 
822194a63aSNoralf Trønnes 	/**
832194a63aSNoralf Trønnes 	 * @vmap_lock: Protects the vmap address and use count
842194a63aSNoralf Trønnes 	 */
852194a63aSNoralf Trønnes 	struct mutex vmap_lock;
862194a63aSNoralf Trønnes 
872194a63aSNoralf Trønnes 	/**
882194a63aSNoralf Trønnes 	 * @vaddr: Kernel virtual address of the backing memory
892194a63aSNoralf Trønnes 	 */
902194a63aSNoralf Trønnes 	void *vaddr;
912194a63aSNoralf Trønnes 
922194a63aSNoralf Trønnes 	/**
932194a63aSNoralf Trønnes 	 * @vmap_use_count:
942194a63aSNoralf Trønnes 	 *
952194a63aSNoralf Trønnes 	 * Reference count on the virtual address.
962194a63aSNoralf Trønnes 	 * The address are un-mapped when the count reaches zero.
972194a63aSNoralf Trønnes 	 */
982194a63aSNoralf Trønnes 	unsigned int vmap_use_count;
991cad6292SGerd Hoffmann 
1001cad6292SGerd Hoffmann 	/**
1010cf2ef46SThomas Zimmermann 	 * @map_wc: map object write-combined (instead of using shmem defaults).
1021cad6292SGerd Hoffmann 	 */
1030cf2ef46SThomas Zimmermann 	bool map_wc;
1042194a63aSNoralf Trønnes };
1052194a63aSNoralf Trønnes 
1062194a63aSNoralf Trønnes #define to_drm_gem_shmem_obj(obj) \
1072194a63aSNoralf Trønnes 	container_of(obj, struct drm_gem_shmem_object, base)
1082194a63aSNoralf Trønnes 
1092194a63aSNoralf Trønnes struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size);
110*a193f3b4SThomas Zimmermann void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem);
1112194a63aSNoralf Trønnes 
1122194a63aSNoralf Trønnes int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem);
1132194a63aSNoralf Trønnes void drm_gem_shmem_put_pages(struct drm_gem_shmem_object *shmem);
114*a193f3b4SThomas Zimmermann int drm_gem_shmem_pin(struct drm_gem_shmem_object *shmem);
115*a193f3b4SThomas Zimmermann void drm_gem_shmem_unpin(struct drm_gem_shmem_object *shmem);
116*a193f3b4SThomas Zimmermann int drm_gem_shmem_vmap(struct drm_gem_shmem_object *shmem, struct dma_buf_map *map);
117*a193f3b4SThomas Zimmermann void drm_gem_shmem_vunmap(struct drm_gem_shmem_object *shmem, struct dma_buf_map *map);
118*a193f3b4SThomas Zimmermann int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct *vma);
1192194a63aSNoralf Trønnes 
120*a193f3b4SThomas Zimmermann int drm_gem_shmem_madvise(struct drm_gem_shmem_object *shmem, int madv);
12117acb9f3SRob Herring 
12217acb9f3SRob Herring static inline bool drm_gem_shmem_is_purgeable(struct drm_gem_shmem_object *shmem)
12317acb9f3SRob Herring {
12417acb9f3SRob Herring 	return (shmem->madv > 0) &&
12517acb9f3SRob Herring 		!shmem->vmap_use_count && shmem->sgt &&
12617acb9f3SRob Herring 		!shmem->base.dma_buf && !shmem->base.import_attach;
12717acb9f3SRob Herring }
12817acb9f3SRob Herring 
129*a193f3b4SThomas Zimmermann void drm_gem_shmem_purge_locked(struct drm_gem_shmem_object *shmem);
130*a193f3b4SThomas Zimmermann bool drm_gem_shmem_purge(struct drm_gem_shmem_object *shmem);
13117acb9f3SRob Herring 
132*a193f3b4SThomas Zimmermann struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object *shmem);
133*a193f3b4SThomas Zimmermann struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem);
1342194a63aSNoralf Trønnes 
135*a193f3b4SThomas Zimmermann void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem,
136*a193f3b4SThomas Zimmermann 			      struct drm_printer *p, unsigned int indent);
137c7fbcb71SThomas Zimmermann 
138c7fbcb71SThomas Zimmermann /*
139c7fbcb71SThomas Zimmermann  * GEM object functions
140c7fbcb71SThomas Zimmermann  */
141c7fbcb71SThomas Zimmermann 
142c7fbcb71SThomas Zimmermann /**
143*a193f3b4SThomas Zimmermann  * drm_gem_shmem_object_free - GEM object function for drm_gem_shmem_free()
144c7fbcb71SThomas Zimmermann  * @obj: GEM object to free
145c7fbcb71SThomas Zimmermann  *
146*a193f3b4SThomas Zimmermann  * This function wraps drm_gem_shmem_free(). Drivers that employ the shmem helpers
147c7fbcb71SThomas Zimmermann  * should use it as their &drm_gem_object_funcs.free handler.
148c7fbcb71SThomas Zimmermann  */
149c7fbcb71SThomas Zimmermann static inline void drm_gem_shmem_object_free(struct drm_gem_object *obj)
150c7fbcb71SThomas Zimmermann {
151*a193f3b4SThomas Zimmermann 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
152*a193f3b4SThomas Zimmermann 
153*a193f3b4SThomas Zimmermann 	drm_gem_shmem_free(shmem);
154c7fbcb71SThomas Zimmermann }
155c7fbcb71SThomas Zimmermann 
156c7fbcb71SThomas Zimmermann /**
157c7fbcb71SThomas Zimmermann  * drm_gem_shmem_object_print_info() - Print &drm_gem_shmem_object info for debugfs
158c7fbcb71SThomas Zimmermann  * @p: DRM printer
159c7fbcb71SThomas Zimmermann  * @indent: Tab indentation level
160c7fbcb71SThomas Zimmermann  * @obj: GEM object
161c7fbcb71SThomas Zimmermann  *
162c7fbcb71SThomas Zimmermann  * This function wraps drm_gem_shmem_print_info(). Drivers that employ the shmem helpers should
163c7fbcb71SThomas Zimmermann  * use this function as their &drm_gem_object_funcs.print_info handler.
164c7fbcb71SThomas Zimmermann  */
165c7fbcb71SThomas Zimmermann static inline void drm_gem_shmem_object_print_info(struct drm_printer *p, unsigned int indent,
166c7fbcb71SThomas Zimmermann 						   const struct drm_gem_object *obj)
167c7fbcb71SThomas Zimmermann {
168*a193f3b4SThomas Zimmermann 	const struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
169*a193f3b4SThomas Zimmermann 
170*a193f3b4SThomas Zimmermann 	drm_gem_shmem_print_info(shmem, p, indent);
171c7fbcb71SThomas Zimmermann }
172c7fbcb71SThomas Zimmermann 
173c7fbcb71SThomas Zimmermann /**
174c7fbcb71SThomas Zimmermann  * drm_gem_shmem_object_pin - GEM object function for drm_gem_shmem_pin()
175c7fbcb71SThomas Zimmermann  * @obj: GEM object
176c7fbcb71SThomas Zimmermann  *
177c7fbcb71SThomas Zimmermann  * This function wraps drm_gem_shmem_pin(). Drivers that employ the shmem helpers should
178c7fbcb71SThomas Zimmermann  * use it as their &drm_gem_object_funcs.pin handler.
179c7fbcb71SThomas Zimmermann  */
180c7fbcb71SThomas Zimmermann static inline int drm_gem_shmem_object_pin(struct drm_gem_object *obj)
181c7fbcb71SThomas Zimmermann {
182*a193f3b4SThomas Zimmermann 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
183*a193f3b4SThomas Zimmermann 
184*a193f3b4SThomas Zimmermann 	return drm_gem_shmem_pin(shmem);
185c7fbcb71SThomas Zimmermann }
186c7fbcb71SThomas Zimmermann 
187c7fbcb71SThomas Zimmermann /**
188c7fbcb71SThomas Zimmermann  * drm_gem_shmem_object_unpin - GEM object function for drm_gem_shmem_unpin()
189c7fbcb71SThomas Zimmermann  * @obj: GEM object
190c7fbcb71SThomas Zimmermann  *
191c7fbcb71SThomas Zimmermann  * This function wraps drm_gem_shmem_unpin(). Drivers that employ the shmem helpers should
192c7fbcb71SThomas Zimmermann  * use it as their &drm_gem_object_funcs.unpin handler.
193c7fbcb71SThomas Zimmermann  */
194c7fbcb71SThomas Zimmermann static inline void drm_gem_shmem_object_unpin(struct drm_gem_object *obj)
195c7fbcb71SThomas Zimmermann {
196*a193f3b4SThomas Zimmermann 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
197*a193f3b4SThomas Zimmermann 
198*a193f3b4SThomas Zimmermann 	drm_gem_shmem_unpin(shmem);
199c7fbcb71SThomas Zimmermann }
200c7fbcb71SThomas Zimmermann 
201c7fbcb71SThomas Zimmermann /**
202c7fbcb71SThomas Zimmermann  * drm_gem_shmem_object_get_sg_table - GEM object function for drm_gem_shmem_get_sg_table()
203c7fbcb71SThomas Zimmermann  * @obj: GEM object
204c7fbcb71SThomas Zimmermann  *
205c7fbcb71SThomas Zimmermann  * This function wraps drm_gem_shmem_get_sg_table(). Drivers that employ the shmem helpers should
206c7fbcb71SThomas Zimmermann  * use it as their &drm_gem_object_funcs.get_sg_table handler.
207c7fbcb71SThomas Zimmermann  *
208c7fbcb71SThomas Zimmermann  * Returns:
209c7fbcb71SThomas Zimmermann  * A pointer to the scatter/gather table of pinned pages or NULL on failure.
210c7fbcb71SThomas Zimmermann  */
211c7fbcb71SThomas Zimmermann static inline struct sg_table *drm_gem_shmem_object_get_sg_table(struct drm_gem_object *obj)
212c7fbcb71SThomas Zimmermann {
213*a193f3b4SThomas Zimmermann 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
214*a193f3b4SThomas Zimmermann 
215*a193f3b4SThomas Zimmermann 	return drm_gem_shmem_get_sg_table(shmem);
216c7fbcb71SThomas Zimmermann }
217c7fbcb71SThomas Zimmermann 
218c7fbcb71SThomas Zimmermann /*
219c7fbcb71SThomas Zimmermann  * drm_gem_shmem_object_vmap - GEM object function for drm_gem_shmem_vmap()
220c7fbcb71SThomas Zimmermann  * @obj: GEM object
221c7fbcb71SThomas Zimmermann  * @map: Returns the kernel virtual address of the SHMEM GEM object's backing store.
222c7fbcb71SThomas Zimmermann  *
223c7fbcb71SThomas Zimmermann  * This function wraps drm_gem_shmem_vmap(). Drivers that employ the shmem helpers should
224c7fbcb71SThomas Zimmermann  * use it as their &drm_gem_object_funcs.vmap handler.
225c7fbcb71SThomas Zimmermann  *
226c7fbcb71SThomas Zimmermann  * Returns:
227c7fbcb71SThomas Zimmermann  * 0 on success or a negative error code on failure.
228c7fbcb71SThomas Zimmermann  */
229c7fbcb71SThomas Zimmermann static inline int drm_gem_shmem_object_vmap(struct drm_gem_object *obj, struct dma_buf_map *map)
230c7fbcb71SThomas Zimmermann {
231*a193f3b4SThomas Zimmermann 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
232*a193f3b4SThomas Zimmermann 
233*a193f3b4SThomas Zimmermann 	return drm_gem_shmem_vmap(shmem, map);
234c7fbcb71SThomas Zimmermann }
235c7fbcb71SThomas Zimmermann 
236c7fbcb71SThomas Zimmermann /*
237c7fbcb71SThomas Zimmermann  * drm_gem_shmem_object_vunmap - GEM object function for drm_gem_shmem_vunmap()
238c7fbcb71SThomas Zimmermann  * @obj: GEM object
239c7fbcb71SThomas Zimmermann  * @map: Kernel virtual address where the SHMEM GEM object was mapped
240c7fbcb71SThomas Zimmermann  *
241c7fbcb71SThomas Zimmermann  * This function wraps drm_gem_shmem_vunmap(). Drivers that employ the shmem helpers should
242c7fbcb71SThomas Zimmermann  * use it as their &drm_gem_object_funcs.vunmap handler.
243c7fbcb71SThomas Zimmermann  */
244c7fbcb71SThomas Zimmermann static inline void drm_gem_shmem_object_vunmap(struct drm_gem_object *obj, struct dma_buf_map *map)
245c7fbcb71SThomas Zimmermann {
246*a193f3b4SThomas Zimmermann 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
247*a193f3b4SThomas Zimmermann 
248*a193f3b4SThomas Zimmermann 	drm_gem_shmem_vunmap(shmem, map);
249c7fbcb71SThomas Zimmermann }
250c7fbcb71SThomas Zimmermann 
251c7fbcb71SThomas Zimmermann /**
252c7fbcb71SThomas Zimmermann  * drm_gem_shmem_object_mmap - GEM object function for drm_gem_shmem_mmap()
253c7fbcb71SThomas Zimmermann  * @obj: GEM object
254c7fbcb71SThomas Zimmermann  * @vma: VMA for the area to be mapped
255c7fbcb71SThomas Zimmermann  *
256c7fbcb71SThomas Zimmermann  * This function wraps drm_gem_shmem_mmap(). Drivers that employ the shmem helpers should
257c7fbcb71SThomas Zimmermann  * use it as their &drm_gem_object_funcs.mmap handler.
258c7fbcb71SThomas Zimmermann  *
259c7fbcb71SThomas Zimmermann  * Returns:
260c7fbcb71SThomas Zimmermann  * 0 on success or a negative error code on failure.
261c7fbcb71SThomas Zimmermann  */
262c7fbcb71SThomas Zimmermann static inline int drm_gem_shmem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
263c7fbcb71SThomas Zimmermann {
264*a193f3b4SThomas Zimmermann 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
265*a193f3b4SThomas Zimmermann 
266*a193f3b4SThomas Zimmermann 	return drm_gem_shmem_mmap(shmem, vma);
267c7fbcb71SThomas Zimmermann }
268c7fbcb71SThomas Zimmermann 
269c7fbcb71SThomas Zimmermann /*
270c7fbcb71SThomas Zimmermann  * Driver ops
271c7fbcb71SThomas Zimmermann  */
272c7fbcb71SThomas Zimmermann 
2732194a63aSNoralf Trønnes struct drm_gem_object *
2742194a63aSNoralf Trønnes drm_gem_shmem_prime_import_sg_table(struct drm_device *dev,
2752194a63aSNoralf Trønnes 				    struct dma_buf_attachment *attach,
2762194a63aSNoralf Trønnes 				    struct sg_table *sgt);
277*a193f3b4SThomas Zimmermann int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev,
278*a193f3b4SThomas Zimmermann 			      struct drm_mode_create_dumb *args);
2792194a63aSNoralf Trønnes 
2802194a63aSNoralf Trønnes /**
2812194a63aSNoralf Trønnes  * DRM_GEM_SHMEM_DRIVER_OPS - Default shmem GEM operations
2822194a63aSNoralf Trønnes  *
2832194a63aSNoralf Trønnes  * This macro provides a shortcut for setting the shmem GEM operations in
2842194a63aSNoralf Trønnes  * the &drm_driver structure.
2852194a63aSNoralf Trønnes  */
2862194a63aSNoralf Trønnes #define DRM_GEM_SHMEM_DRIVER_OPS \
2872194a63aSNoralf Trønnes 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd, \
2882194a63aSNoralf Trønnes 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle, \
2892194a63aSNoralf Trønnes 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table, \
2902194a63aSNoralf Trønnes 	.gem_prime_mmap		= drm_gem_prime_mmap, \
2912194a63aSNoralf Trønnes 	.dumb_create		= drm_gem_shmem_dumb_create
2922194a63aSNoralf Trønnes 
2932194a63aSNoralf Trønnes #endif /* __DRM_GEM_SHMEM_HELPER_H__ */
294