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); 110a193f3b4SThomas 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); 114a193f3b4SThomas Zimmermann int drm_gem_shmem_pin(struct drm_gem_shmem_object *shmem); 115a193f3b4SThomas Zimmermann void drm_gem_shmem_unpin(struct drm_gem_shmem_object *shmem); 116*7938f421SLucas De Marchi int drm_gem_shmem_vmap(struct drm_gem_shmem_object *shmem, 117*7938f421SLucas De Marchi struct iosys_map *map); 118*7938f421SLucas De Marchi void drm_gem_shmem_vunmap(struct drm_gem_shmem_object *shmem, 119*7938f421SLucas De Marchi struct iosys_map *map); 120a193f3b4SThomas Zimmermann int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct *vma); 1212194a63aSNoralf Trønnes 122a193f3b4SThomas Zimmermann int drm_gem_shmem_madvise(struct drm_gem_shmem_object *shmem, int madv); 12317acb9f3SRob Herring 12417acb9f3SRob Herring static inline bool drm_gem_shmem_is_purgeable(struct drm_gem_shmem_object *shmem) 12517acb9f3SRob Herring { 12617acb9f3SRob Herring return (shmem->madv > 0) && 12717acb9f3SRob Herring !shmem->vmap_use_count && shmem->sgt && 12817acb9f3SRob Herring !shmem->base.dma_buf && !shmem->base.import_attach; 12917acb9f3SRob Herring } 13017acb9f3SRob Herring 131a193f3b4SThomas Zimmermann void drm_gem_shmem_purge_locked(struct drm_gem_shmem_object *shmem); 132a193f3b4SThomas Zimmermann bool drm_gem_shmem_purge(struct drm_gem_shmem_object *shmem); 13317acb9f3SRob Herring 134a193f3b4SThomas Zimmermann struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object *shmem); 135a193f3b4SThomas Zimmermann struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem); 1362194a63aSNoralf Trønnes 137a193f3b4SThomas Zimmermann void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem, 138a193f3b4SThomas Zimmermann struct drm_printer *p, unsigned int indent); 139c7fbcb71SThomas Zimmermann 140c7fbcb71SThomas Zimmermann /* 141c7fbcb71SThomas Zimmermann * GEM object functions 142c7fbcb71SThomas Zimmermann */ 143c7fbcb71SThomas Zimmermann 144c7fbcb71SThomas Zimmermann /** 145a193f3b4SThomas Zimmermann * drm_gem_shmem_object_free - GEM object function for drm_gem_shmem_free() 146c7fbcb71SThomas Zimmermann * @obj: GEM object to free 147c7fbcb71SThomas Zimmermann * 148a193f3b4SThomas Zimmermann * This function wraps drm_gem_shmem_free(). Drivers that employ the shmem helpers 149c7fbcb71SThomas Zimmermann * should use it as their &drm_gem_object_funcs.free handler. 150c7fbcb71SThomas Zimmermann */ 151c7fbcb71SThomas Zimmermann static inline void drm_gem_shmem_object_free(struct drm_gem_object *obj) 152c7fbcb71SThomas Zimmermann { 153a193f3b4SThomas Zimmermann struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 154a193f3b4SThomas Zimmermann 155a193f3b4SThomas Zimmermann drm_gem_shmem_free(shmem); 156c7fbcb71SThomas Zimmermann } 157c7fbcb71SThomas Zimmermann 158c7fbcb71SThomas Zimmermann /** 159c7fbcb71SThomas Zimmermann * drm_gem_shmem_object_print_info() - Print &drm_gem_shmem_object info for debugfs 160c7fbcb71SThomas Zimmermann * @p: DRM printer 161c7fbcb71SThomas Zimmermann * @indent: Tab indentation level 162c7fbcb71SThomas Zimmermann * @obj: GEM object 163c7fbcb71SThomas Zimmermann * 164c7fbcb71SThomas Zimmermann * This function wraps drm_gem_shmem_print_info(). Drivers that employ the shmem helpers should 165c7fbcb71SThomas Zimmermann * use this function as their &drm_gem_object_funcs.print_info handler. 166c7fbcb71SThomas Zimmermann */ 167c7fbcb71SThomas Zimmermann static inline void drm_gem_shmem_object_print_info(struct drm_printer *p, unsigned int indent, 168c7fbcb71SThomas Zimmermann const struct drm_gem_object *obj) 169c7fbcb71SThomas Zimmermann { 170a193f3b4SThomas Zimmermann const struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 171a193f3b4SThomas Zimmermann 172a193f3b4SThomas Zimmermann drm_gem_shmem_print_info(shmem, p, indent); 173c7fbcb71SThomas Zimmermann } 174c7fbcb71SThomas Zimmermann 175c7fbcb71SThomas Zimmermann /** 176c7fbcb71SThomas Zimmermann * drm_gem_shmem_object_pin - GEM object function for drm_gem_shmem_pin() 177c7fbcb71SThomas Zimmermann * @obj: GEM object 178c7fbcb71SThomas Zimmermann * 179c7fbcb71SThomas Zimmermann * This function wraps drm_gem_shmem_pin(). Drivers that employ the shmem helpers should 180c7fbcb71SThomas Zimmermann * use it as their &drm_gem_object_funcs.pin handler. 181c7fbcb71SThomas Zimmermann */ 182c7fbcb71SThomas Zimmermann static inline int drm_gem_shmem_object_pin(struct drm_gem_object *obj) 183c7fbcb71SThomas Zimmermann { 184a193f3b4SThomas Zimmermann struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 185a193f3b4SThomas Zimmermann 186a193f3b4SThomas Zimmermann return drm_gem_shmem_pin(shmem); 187c7fbcb71SThomas Zimmermann } 188c7fbcb71SThomas Zimmermann 189c7fbcb71SThomas Zimmermann /** 190c7fbcb71SThomas Zimmermann * drm_gem_shmem_object_unpin - GEM object function for drm_gem_shmem_unpin() 191c7fbcb71SThomas Zimmermann * @obj: GEM object 192c7fbcb71SThomas Zimmermann * 193c7fbcb71SThomas Zimmermann * This function wraps drm_gem_shmem_unpin(). Drivers that employ the shmem helpers should 194c7fbcb71SThomas Zimmermann * use it as their &drm_gem_object_funcs.unpin handler. 195c7fbcb71SThomas Zimmermann */ 196c7fbcb71SThomas Zimmermann static inline void drm_gem_shmem_object_unpin(struct drm_gem_object *obj) 197c7fbcb71SThomas Zimmermann { 198a193f3b4SThomas Zimmermann struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 199a193f3b4SThomas Zimmermann 200a193f3b4SThomas Zimmermann drm_gem_shmem_unpin(shmem); 201c7fbcb71SThomas Zimmermann } 202c7fbcb71SThomas Zimmermann 203c7fbcb71SThomas Zimmermann /** 204c7fbcb71SThomas Zimmermann * drm_gem_shmem_object_get_sg_table - GEM object function for drm_gem_shmem_get_sg_table() 205c7fbcb71SThomas Zimmermann * @obj: GEM object 206c7fbcb71SThomas Zimmermann * 207c7fbcb71SThomas Zimmermann * This function wraps drm_gem_shmem_get_sg_table(). Drivers that employ the shmem helpers should 208c7fbcb71SThomas Zimmermann * use it as their &drm_gem_object_funcs.get_sg_table handler. 209c7fbcb71SThomas Zimmermann * 210c7fbcb71SThomas Zimmermann * Returns: 211c7fbcb71SThomas Zimmermann * A pointer to the scatter/gather table of pinned pages or NULL on failure. 212c7fbcb71SThomas Zimmermann */ 213c7fbcb71SThomas Zimmermann static inline struct sg_table *drm_gem_shmem_object_get_sg_table(struct drm_gem_object *obj) 214c7fbcb71SThomas Zimmermann { 215a193f3b4SThomas Zimmermann struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 216a193f3b4SThomas Zimmermann 217a193f3b4SThomas Zimmermann return drm_gem_shmem_get_sg_table(shmem); 218c7fbcb71SThomas Zimmermann } 219c7fbcb71SThomas Zimmermann 220c7fbcb71SThomas Zimmermann /* 221c7fbcb71SThomas Zimmermann * drm_gem_shmem_object_vmap - GEM object function for drm_gem_shmem_vmap() 222c7fbcb71SThomas Zimmermann * @obj: GEM object 223c7fbcb71SThomas Zimmermann * @map: Returns the kernel virtual address of the SHMEM GEM object's backing store. 224c7fbcb71SThomas Zimmermann * 225c7fbcb71SThomas Zimmermann * This function wraps drm_gem_shmem_vmap(). Drivers that employ the shmem helpers should 226c7fbcb71SThomas Zimmermann * use it as their &drm_gem_object_funcs.vmap handler. 227c7fbcb71SThomas Zimmermann * 228c7fbcb71SThomas Zimmermann * Returns: 229c7fbcb71SThomas Zimmermann * 0 on success or a negative error code on failure. 230c7fbcb71SThomas Zimmermann */ 231*7938f421SLucas De Marchi static inline int drm_gem_shmem_object_vmap(struct drm_gem_object *obj, 232*7938f421SLucas De Marchi struct iosys_map *map) 233c7fbcb71SThomas Zimmermann { 234a193f3b4SThomas Zimmermann struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 235a193f3b4SThomas Zimmermann 236a193f3b4SThomas Zimmermann return drm_gem_shmem_vmap(shmem, map); 237c7fbcb71SThomas Zimmermann } 238c7fbcb71SThomas Zimmermann 239c7fbcb71SThomas Zimmermann /* 240c7fbcb71SThomas Zimmermann * drm_gem_shmem_object_vunmap - GEM object function for drm_gem_shmem_vunmap() 241c7fbcb71SThomas Zimmermann * @obj: GEM object 242c7fbcb71SThomas Zimmermann * @map: Kernel virtual address where the SHMEM GEM object was mapped 243c7fbcb71SThomas Zimmermann * 244c7fbcb71SThomas Zimmermann * This function wraps drm_gem_shmem_vunmap(). Drivers that employ the shmem helpers should 245c7fbcb71SThomas Zimmermann * use it as their &drm_gem_object_funcs.vunmap handler. 246c7fbcb71SThomas Zimmermann */ 247*7938f421SLucas De Marchi static inline void drm_gem_shmem_object_vunmap(struct drm_gem_object *obj, 248*7938f421SLucas De Marchi struct iosys_map *map) 249c7fbcb71SThomas Zimmermann { 250a193f3b4SThomas Zimmermann struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 251a193f3b4SThomas Zimmermann 252a193f3b4SThomas Zimmermann drm_gem_shmem_vunmap(shmem, map); 253c7fbcb71SThomas Zimmermann } 254c7fbcb71SThomas Zimmermann 255c7fbcb71SThomas Zimmermann /** 256c7fbcb71SThomas Zimmermann * drm_gem_shmem_object_mmap - GEM object function for drm_gem_shmem_mmap() 257c7fbcb71SThomas Zimmermann * @obj: GEM object 258c7fbcb71SThomas Zimmermann * @vma: VMA for the area to be mapped 259c7fbcb71SThomas Zimmermann * 260c7fbcb71SThomas Zimmermann * This function wraps drm_gem_shmem_mmap(). Drivers that employ the shmem helpers should 261c7fbcb71SThomas Zimmermann * use it as their &drm_gem_object_funcs.mmap handler. 262c7fbcb71SThomas Zimmermann * 263c7fbcb71SThomas Zimmermann * Returns: 264c7fbcb71SThomas Zimmermann * 0 on success or a negative error code on failure. 265c7fbcb71SThomas Zimmermann */ 266c7fbcb71SThomas Zimmermann static inline int drm_gem_shmem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma) 267c7fbcb71SThomas Zimmermann { 268a193f3b4SThomas Zimmermann struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 269a193f3b4SThomas Zimmermann 270a193f3b4SThomas Zimmermann return drm_gem_shmem_mmap(shmem, vma); 271c7fbcb71SThomas Zimmermann } 272c7fbcb71SThomas Zimmermann 273c7fbcb71SThomas Zimmermann /* 274c7fbcb71SThomas Zimmermann * Driver ops 275c7fbcb71SThomas Zimmermann */ 276c7fbcb71SThomas Zimmermann 2772194a63aSNoralf Trønnes struct drm_gem_object * 2782194a63aSNoralf Trønnes drm_gem_shmem_prime_import_sg_table(struct drm_device *dev, 2792194a63aSNoralf Trønnes struct dma_buf_attachment *attach, 2802194a63aSNoralf Trønnes struct sg_table *sgt); 281a193f3b4SThomas Zimmermann int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev, 282a193f3b4SThomas Zimmermann struct drm_mode_create_dumb *args); 2832194a63aSNoralf Trønnes 2842194a63aSNoralf Trønnes /** 2852194a63aSNoralf Trønnes * DRM_GEM_SHMEM_DRIVER_OPS - Default shmem GEM operations 2862194a63aSNoralf Trønnes * 2872194a63aSNoralf Trønnes * This macro provides a shortcut for setting the shmem GEM operations in 2882194a63aSNoralf Trønnes * the &drm_driver structure. 2892194a63aSNoralf Trønnes */ 2902194a63aSNoralf Trønnes #define DRM_GEM_SHMEM_DRIVER_OPS \ 2912194a63aSNoralf Trønnes .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \ 2922194a63aSNoralf Trønnes .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \ 2932194a63aSNoralf Trønnes .gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table, \ 2942194a63aSNoralf Trønnes .gem_prime_mmap = drm_gem_prime_mmap, \ 2952194a63aSNoralf Trønnes .dumb_create = drm_gem_shmem_dumb_create 2962194a63aSNoralf Trønnes 2972194a63aSNoralf Trønnes #endif /* __DRM_GEM_SHMEM_HELPER_H__ */ 298