xref: /linux/include/drm/drm_gem_shmem_helper.h (revision a78027847226493ea6f09a00875fa4871fd29e69)
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: Page table
312194a63aSNoralf Trønnes 	 */
322194a63aSNoralf Trønnes 	struct page **pages;
332194a63aSNoralf Trønnes 
342194a63aSNoralf Trønnes 	/**
352194a63aSNoralf Trønnes 	 * @pages_use_count:
362194a63aSNoralf Trønnes 	 *
372194a63aSNoralf Trønnes 	 * Reference count on the pages table.
382194a63aSNoralf Trønnes 	 * The pages are put when the count reaches zero.
392194a63aSNoralf Trønnes 	 */
402194a63aSNoralf Trønnes 	unsigned int pages_use_count;
412194a63aSNoralf Trønnes 
42105401b6SRob Herring 	/**
43105401b6SRob Herring 	 * @madv: State for madvise
44105401b6SRob Herring 	 *
45105401b6SRob Herring 	 * 0 is active/inuse.
46105401b6SRob Herring 	 * A negative value is the object is purged.
47105401b6SRob Herring 	 * Positive values are driver specific and not used by the helpers.
48105401b6SRob Herring 	 */
4917acb9f3SRob Herring 	int madv;
50105401b6SRob Herring 
51105401b6SRob Herring 	/**
52105401b6SRob Herring 	 * @madv_list: List entry for madvise tracking
53105401b6SRob Herring 	 *
54105401b6SRob Herring 	 * Typically used by drivers to track purgeable objects
55105401b6SRob Herring 	 */
5617acb9f3SRob Herring 	struct list_head madv_list;
5717acb9f3SRob Herring 
582194a63aSNoralf Trønnes 	/**
592194a63aSNoralf Trønnes 	 * @sgt: Scatter/gather table for imported PRIME buffers
602194a63aSNoralf Trønnes 	 */
612194a63aSNoralf Trønnes 	struct sg_table *sgt;
622194a63aSNoralf Trønnes 
632194a63aSNoralf Trønnes 	/**
642194a63aSNoralf Trønnes 	 * @vaddr: Kernel virtual address of the backing memory
652194a63aSNoralf Trønnes 	 */
662194a63aSNoralf Trønnes 	void *vaddr;
672194a63aSNoralf Trønnes 
682194a63aSNoralf Trønnes 	/**
692194a63aSNoralf Trønnes 	 * @vmap_use_count:
702194a63aSNoralf Trønnes 	 *
712194a63aSNoralf Trønnes 	 * Reference count on the virtual address.
722194a63aSNoralf Trønnes 	 * The address are un-mapped when the count reaches zero.
732194a63aSNoralf Trønnes 	 */
742194a63aSNoralf Trønnes 	unsigned int vmap_use_count;
751cad6292SGerd Hoffmann 
761cad6292SGerd Hoffmann 	/**
773842d671SDmitry Osipenko 	 * @pages_mark_dirty_on_put:
783842d671SDmitry Osipenko 	 *
793842d671SDmitry Osipenko 	 * Mark pages as dirty when they are put.
803842d671SDmitry Osipenko 	 */
813842d671SDmitry Osipenko 	bool pages_mark_dirty_on_put : 1;
823842d671SDmitry Osipenko 
833842d671SDmitry Osipenko 	/**
843842d671SDmitry Osipenko 	 * @pages_mark_accessed_on_put:
853842d671SDmitry Osipenko 	 *
863842d671SDmitry Osipenko 	 * Mark pages as accessed when they are put.
873842d671SDmitry Osipenko 	 */
883842d671SDmitry Osipenko 	bool pages_mark_accessed_on_put : 1;
893842d671SDmitry Osipenko 
903842d671SDmitry Osipenko 	/**
910cf2ef46SThomas Zimmermann 	 * @map_wc: map object write-combined (instead of using shmem defaults).
921cad6292SGerd Hoffmann 	 */
933842d671SDmitry Osipenko 	bool map_wc : 1;
942194a63aSNoralf Trønnes };
952194a63aSNoralf Trønnes 
962194a63aSNoralf Trønnes #define to_drm_gem_shmem_obj(obj) \
972194a63aSNoralf Trønnes 	container_of(obj, struct drm_gem_shmem_object, base)
982194a63aSNoralf Trønnes 
992194a63aSNoralf Trønnes struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size);
100a193f3b4SThomas Zimmermann void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem);
1012194a63aSNoralf Trønnes 
1022194a63aSNoralf Trønnes void drm_gem_shmem_put_pages(struct drm_gem_shmem_object *shmem);
103a193f3b4SThomas Zimmermann int drm_gem_shmem_pin(struct drm_gem_shmem_object *shmem);
104a193f3b4SThomas Zimmermann void drm_gem_shmem_unpin(struct drm_gem_shmem_object *shmem);
1057938f421SLucas De Marchi int drm_gem_shmem_vmap(struct drm_gem_shmem_object *shmem,
1067938f421SLucas De Marchi 		       struct iosys_map *map);
1077938f421SLucas De Marchi void drm_gem_shmem_vunmap(struct drm_gem_shmem_object *shmem,
1087938f421SLucas De Marchi 			  struct iosys_map *map);
109a193f3b4SThomas Zimmermann int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct *vma);
1102194a63aSNoralf Trønnes 
111ec144244SThomas Zimmermann int drm_gem_shmem_pin_locked(struct drm_gem_shmem_object *shmem);
112ec144244SThomas Zimmermann void drm_gem_shmem_unpin_locked(struct drm_gem_shmem_object *shmem);
113ec144244SThomas Zimmermann 
114a193f3b4SThomas Zimmermann int drm_gem_shmem_madvise(struct drm_gem_shmem_object *shmem, int madv);
11517acb9f3SRob Herring 
11617acb9f3SRob Herring static inline bool drm_gem_shmem_is_purgeable(struct drm_gem_shmem_object *shmem)
11717acb9f3SRob Herring {
11817acb9f3SRob Herring 	return (shmem->madv > 0) &&
11917acb9f3SRob Herring 		!shmem->vmap_use_count && shmem->sgt &&
12017acb9f3SRob Herring 		!shmem->base.dma_buf && !shmem->base.import_attach;
12117acb9f3SRob Herring }
12217acb9f3SRob Herring 
12321aa27ddSDmitry Osipenko void drm_gem_shmem_purge(struct drm_gem_shmem_object *shmem);
12417acb9f3SRob Herring 
125a193f3b4SThomas Zimmermann struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object *shmem);
126a193f3b4SThomas Zimmermann struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem);
1272194a63aSNoralf Trønnes 
128a193f3b4SThomas Zimmermann void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem,
129a193f3b4SThomas Zimmermann 			      struct drm_printer *p, unsigned int indent);
130c7fbcb71SThomas Zimmermann 
131d315bdbfSThomas Zimmermann extern const struct vm_operations_struct drm_gem_shmem_vm_ops;
132d315bdbfSThomas Zimmermann 
133c7fbcb71SThomas Zimmermann /*
134c7fbcb71SThomas Zimmermann  * GEM object functions
135c7fbcb71SThomas Zimmermann  */
136c7fbcb71SThomas Zimmermann 
137c7fbcb71SThomas Zimmermann /**
138a193f3b4SThomas Zimmermann  * drm_gem_shmem_object_free - GEM object function for drm_gem_shmem_free()
139c7fbcb71SThomas Zimmermann  * @obj: GEM object to free
140c7fbcb71SThomas Zimmermann  *
141a193f3b4SThomas Zimmermann  * This function wraps drm_gem_shmem_free(). Drivers that employ the shmem helpers
142c7fbcb71SThomas Zimmermann  * should use it as their &drm_gem_object_funcs.free handler.
143c7fbcb71SThomas Zimmermann  */
144c7fbcb71SThomas Zimmermann static inline void drm_gem_shmem_object_free(struct drm_gem_object *obj)
145c7fbcb71SThomas Zimmermann {
146a193f3b4SThomas Zimmermann 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
147a193f3b4SThomas Zimmermann 
148a193f3b4SThomas Zimmermann 	drm_gem_shmem_free(shmem);
149c7fbcb71SThomas Zimmermann }
150c7fbcb71SThomas Zimmermann 
151c7fbcb71SThomas Zimmermann /**
152c7fbcb71SThomas Zimmermann  * drm_gem_shmem_object_print_info() - Print &drm_gem_shmem_object info for debugfs
153c7fbcb71SThomas Zimmermann  * @p: DRM printer
154c7fbcb71SThomas Zimmermann  * @indent: Tab indentation level
155c7fbcb71SThomas Zimmermann  * @obj: GEM object
156c7fbcb71SThomas Zimmermann  *
157c7fbcb71SThomas Zimmermann  * This function wraps drm_gem_shmem_print_info(). Drivers that employ the shmem helpers should
158c7fbcb71SThomas Zimmermann  * use this function as their &drm_gem_object_funcs.print_info handler.
159c7fbcb71SThomas Zimmermann  */
160c7fbcb71SThomas Zimmermann static inline void drm_gem_shmem_object_print_info(struct drm_printer *p, unsigned int indent,
161c7fbcb71SThomas Zimmermann 						   const struct drm_gem_object *obj)
162c7fbcb71SThomas Zimmermann {
163a193f3b4SThomas Zimmermann 	const struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
164a193f3b4SThomas Zimmermann 
165a193f3b4SThomas Zimmermann 	drm_gem_shmem_print_info(shmem, p, indent);
166c7fbcb71SThomas Zimmermann }
167c7fbcb71SThomas Zimmermann 
168c7fbcb71SThomas Zimmermann /**
169c7fbcb71SThomas Zimmermann  * drm_gem_shmem_object_pin - GEM object function for drm_gem_shmem_pin()
170c7fbcb71SThomas Zimmermann  * @obj: GEM object
171c7fbcb71SThomas Zimmermann  *
172c7fbcb71SThomas Zimmermann  * This function wraps drm_gem_shmem_pin(). Drivers that employ the shmem helpers should
173c7fbcb71SThomas Zimmermann  * use it as their &drm_gem_object_funcs.pin handler.
174c7fbcb71SThomas Zimmermann  */
175c7fbcb71SThomas Zimmermann static inline int drm_gem_shmem_object_pin(struct drm_gem_object *obj)
176c7fbcb71SThomas Zimmermann {
177a193f3b4SThomas Zimmermann 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
178a193f3b4SThomas Zimmermann 
179*a7802784SThomas Zimmermann 	return drm_gem_shmem_pin_locked(shmem);
180c7fbcb71SThomas Zimmermann }
181c7fbcb71SThomas Zimmermann 
182c7fbcb71SThomas Zimmermann /**
183c7fbcb71SThomas Zimmermann  * drm_gem_shmem_object_unpin - GEM object function for drm_gem_shmem_unpin()
184c7fbcb71SThomas Zimmermann  * @obj: GEM object
185c7fbcb71SThomas Zimmermann  *
186c7fbcb71SThomas Zimmermann  * This function wraps drm_gem_shmem_unpin(). Drivers that employ the shmem helpers should
187c7fbcb71SThomas Zimmermann  * use it as their &drm_gem_object_funcs.unpin handler.
188c7fbcb71SThomas Zimmermann  */
189c7fbcb71SThomas Zimmermann static inline void drm_gem_shmem_object_unpin(struct drm_gem_object *obj)
190c7fbcb71SThomas Zimmermann {
191a193f3b4SThomas Zimmermann 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
192a193f3b4SThomas Zimmermann 
193ec144244SThomas Zimmermann 	drm_gem_shmem_unpin_locked(shmem);
194c7fbcb71SThomas Zimmermann }
195c7fbcb71SThomas Zimmermann 
196c7fbcb71SThomas Zimmermann /**
197c7fbcb71SThomas Zimmermann  * drm_gem_shmem_object_get_sg_table - GEM object function for drm_gem_shmem_get_sg_table()
198c7fbcb71SThomas Zimmermann  * @obj: GEM object
199c7fbcb71SThomas Zimmermann  *
200c7fbcb71SThomas Zimmermann  * This function wraps drm_gem_shmem_get_sg_table(). Drivers that employ the shmem helpers should
201c7fbcb71SThomas Zimmermann  * use it as their &drm_gem_object_funcs.get_sg_table handler.
202c7fbcb71SThomas Zimmermann  *
203c7fbcb71SThomas Zimmermann  * Returns:
2042b8428a1SLiu Zixian  * A pointer to the scatter/gather table of pinned pages or error pointer on failure.
205c7fbcb71SThomas Zimmermann  */
206c7fbcb71SThomas Zimmermann static inline struct sg_table *drm_gem_shmem_object_get_sg_table(struct drm_gem_object *obj)
207c7fbcb71SThomas Zimmermann {
208a193f3b4SThomas Zimmermann 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
209a193f3b4SThomas Zimmermann 
210a193f3b4SThomas Zimmermann 	return drm_gem_shmem_get_sg_table(shmem);
211c7fbcb71SThomas Zimmermann }
212c7fbcb71SThomas Zimmermann 
213c7fbcb71SThomas Zimmermann /*
214c7fbcb71SThomas Zimmermann  * drm_gem_shmem_object_vmap - GEM object function for drm_gem_shmem_vmap()
215c7fbcb71SThomas Zimmermann  * @obj: GEM object
216c7fbcb71SThomas Zimmermann  * @map: Returns the kernel virtual address of the SHMEM GEM object's backing store.
217c7fbcb71SThomas Zimmermann  *
218c7fbcb71SThomas Zimmermann  * This function wraps drm_gem_shmem_vmap(). Drivers that employ the shmem helpers should
219c7fbcb71SThomas Zimmermann  * use it as their &drm_gem_object_funcs.vmap handler.
220c7fbcb71SThomas Zimmermann  *
221c7fbcb71SThomas Zimmermann  * Returns:
222c7fbcb71SThomas Zimmermann  * 0 on success or a negative error code on failure.
223c7fbcb71SThomas Zimmermann  */
2247938f421SLucas De Marchi static inline int drm_gem_shmem_object_vmap(struct drm_gem_object *obj,
2257938f421SLucas De Marchi 					    struct iosys_map *map)
226c7fbcb71SThomas Zimmermann {
227a193f3b4SThomas Zimmermann 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
228a193f3b4SThomas Zimmermann 
229a193f3b4SThomas Zimmermann 	return drm_gem_shmem_vmap(shmem, map);
230c7fbcb71SThomas Zimmermann }
231c7fbcb71SThomas Zimmermann 
232c7fbcb71SThomas Zimmermann /*
233c7fbcb71SThomas Zimmermann  * drm_gem_shmem_object_vunmap - GEM object function for drm_gem_shmem_vunmap()
234c7fbcb71SThomas Zimmermann  * @obj: GEM object
235c7fbcb71SThomas Zimmermann  * @map: Kernel virtual address where the SHMEM GEM object was mapped
236c7fbcb71SThomas Zimmermann  *
237c7fbcb71SThomas Zimmermann  * This function wraps drm_gem_shmem_vunmap(). Drivers that employ the shmem helpers should
238c7fbcb71SThomas Zimmermann  * use it as their &drm_gem_object_funcs.vunmap handler.
239c7fbcb71SThomas Zimmermann  */
2407938f421SLucas De Marchi static inline void drm_gem_shmem_object_vunmap(struct drm_gem_object *obj,
2417938f421SLucas De Marchi 					       struct iosys_map *map)
242c7fbcb71SThomas Zimmermann {
243a193f3b4SThomas Zimmermann 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
244a193f3b4SThomas Zimmermann 
245a193f3b4SThomas Zimmermann 	drm_gem_shmem_vunmap(shmem, map);
246c7fbcb71SThomas Zimmermann }
247c7fbcb71SThomas Zimmermann 
248c7fbcb71SThomas Zimmermann /**
249c7fbcb71SThomas Zimmermann  * drm_gem_shmem_object_mmap - GEM object function for drm_gem_shmem_mmap()
250c7fbcb71SThomas Zimmermann  * @obj: GEM object
251c7fbcb71SThomas Zimmermann  * @vma: VMA for the area to be mapped
252c7fbcb71SThomas Zimmermann  *
253c7fbcb71SThomas Zimmermann  * This function wraps drm_gem_shmem_mmap(). Drivers that employ the shmem helpers should
254c7fbcb71SThomas Zimmermann  * use it as their &drm_gem_object_funcs.mmap handler.
255c7fbcb71SThomas Zimmermann  *
256c7fbcb71SThomas Zimmermann  * Returns:
257c7fbcb71SThomas Zimmermann  * 0 on success or a negative error code on failure.
258c7fbcb71SThomas Zimmermann  */
259c7fbcb71SThomas Zimmermann static inline int drm_gem_shmem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
260c7fbcb71SThomas Zimmermann {
261a193f3b4SThomas Zimmermann 	struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
262a193f3b4SThomas Zimmermann 
263a193f3b4SThomas Zimmermann 	return drm_gem_shmem_mmap(shmem, vma);
264c7fbcb71SThomas Zimmermann }
265c7fbcb71SThomas Zimmermann 
266c7fbcb71SThomas Zimmermann /*
267c7fbcb71SThomas Zimmermann  * Driver ops
268c7fbcb71SThomas Zimmermann  */
269c7fbcb71SThomas Zimmermann 
2702194a63aSNoralf Trønnes struct drm_gem_object *
2712194a63aSNoralf Trønnes drm_gem_shmem_prime_import_sg_table(struct drm_device *dev,
2722194a63aSNoralf Trønnes 				    struct dma_buf_attachment *attach,
2732194a63aSNoralf Trønnes 				    struct sg_table *sgt);
274a193f3b4SThomas Zimmermann int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev,
275a193f3b4SThomas Zimmermann 			      struct drm_mode_create_dumb *args);
2762194a63aSNoralf Trønnes 
2772194a63aSNoralf Trønnes /**
2782194a63aSNoralf Trønnes  * DRM_GEM_SHMEM_DRIVER_OPS - Default shmem GEM operations
2792194a63aSNoralf Trønnes  *
2802194a63aSNoralf Trønnes  * This macro provides a shortcut for setting the shmem GEM operations in
2812194a63aSNoralf Trønnes  * the &drm_driver structure.
2822194a63aSNoralf Trønnes  */
2832194a63aSNoralf Trønnes #define DRM_GEM_SHMEM_DRIVER_OPS \
2842194a63aSNoralf Trønnes 	.gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table, \
2852194a63aSNoralf Trønnes 	.dumb_create		   = drm_gem_shmem_dumb_create
2862194a63aSNoralf Trønnes 
2872194a63aSNoralf Trønnes #endif /* __DRM_GEM_SHMEM_HELPER_H__ */
288