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 111a193f3b4SThomas Zimmermann int drm_gem_shmem_madvise(struct drm_gem_shmem_object *shmem, int madv); 11217acb9f3SRob Herring 11317acb9f3SRob Herring static inline bool drm_gem_shmem_is_purgeable(struct drm_gem_shmem_object *shmem) 11417acb9f3SRob Herring { 11517acb9f3SRob Herring return (shmem->madv > 0) && 11617acb9f3SRob Herring !shmem->vmap_use_count && shmem->sgt && 11717acb9f3SRob Herring !shmem->base.dma_buf && !shmem->base.import_attach; 11817acb9f3SRob Herring } 11917acb9f3SRob Herring 120*21aa27ddSDmitry Osipenko void drm_gem_shmem_purge(struct drm_gem_shmem_object *shmem); 12117acb9f3SRob Herring 122a193f3b4SThomas Zimmermann struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object *shmem); 123a193f3b4SThomas Zimmermann struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem); 1242194a63aSNoralf Trønnes 125a193f3b4SThomas Zimmermann void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem, 126a193f3b4SThomas Zimmermann struct drm_printer *p, unsigned int indent); 127c7fbcb71SThomas Zimmermann 128d315bdbfSThomas Zimmermann extern const struct vm_operations_struct drm_gem_shmem_vm_ops; 129d315bdbfSThomas Zimmermann 130c7fbcb71SThomas Zimmermann /* 131c7fbcb71SThomas Zimmermann * GEM object functions 132c7fbcb71SThomas Zimmermann */ 133c7fbcb71SThomas Zimmermann 134c7fbcb71SThomas Zimmermann /** 135a193f3b4SThomas Zimmermann * drm_gem_shmem_object_free - GEM object function for drm_gem_shmem_free() 136c7fbcb71SThomas Zimmermann * @obj: GEM object to free 137c7fbcb71SThomas Zimmermann * 138a193f3b4SThomas Zimmermann * This function wraps drm_gem_shmem_free(). Drivers that employ the shmem helpers 139c7fbcb71SThomas Zimmermann * should use it as their &drm_gem_object_funcs.free handler. 140c7fbcb71SThomas Zimmermann */ 141c7fbcb71SThomas Zimmermann static inline void drm_gem_shmem_object_free(struct drm_gem_object *obj) 142c7fbcb71SThomas Zimmermann { 143a193f3b4SThomas Zimmermann struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 144a193f3b4SThomas Zimmermann 145a193f3b4SThomas Zimmermann drm_gem_shmem_free(shmem); 146c7fbcb71SThomas Zimmermann } 147c7fbcb71SThomas Zimmermann 148c7fbcb71SThomas Zimmermann /** 149c7fbcb71SThomas Zimmermann * drm_gem_shmem_object_print_info() - Print &drm_gem_shmem_object info for debugfs 150c7fbcb71SThomas Zimmermann * @p: DRM printer 151c7fbcb71SThomas Zimmermann * @indent: Tab indentation level 152c7fbcb71SThomas Zimmermann * @obj: GEM object 153c7fbcb71SThomas Zimmermann * 154c7fbcb71SThomas Zimmermann * This function wraps drm_gem_shmem_print_info(). Drivers that employ the shmem helpers should 155c7fbcb71SThomas Zimmermann * use this function as their &drm_gem_object_funcs.print_info handler. 156c7fbcb71SThomas Zimmermann */ 157c7fbcb71SThomas Zimmermann static inline void drm_gem_shmem_object_print_info(struct drm_printer *p, unsigned int indent, 158c7fbcb71SThomas Zimmermann const struct drm_gem_object *obj) 159c7fbcb71SThomas Zimmermann { 160a193f3b4SThomas Zimmermann const struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 161a193f3b4SThomas Zimmermann 162a193f3b4SThomas Zimmermann drm_gem_shmem_print_info(shmem, p, indent); 163c7fbcb71SThomas Zimmermann } 164c7fbcb71SThomas Zimmermann 165c7fbcb71SThomas Zimmermann /** 166c7fbcb71SThomas Zimmermann * drm_gem_shmem_object_pin - GEM object function for drm_gem_shmem_pin() 167c7fbcb71SThomas Zimmermann * @obj: GEM object 168c7fbcb71SThomas Zimmermann * 169c7fbcb71SThomas Zimmermann * This function wraps drm_gem_shmem_pin(). Drivers that employ the shmem helpers should 170c7fbcb71SThomas Zimmermann * use it as their &drm_gem_object_funcs.pin handler. 171c7fbcb71SThomas Zimmermann */ 172c7fbcb71SThomas Zimmermann static inline int drm_gem_shmem_object_pin(struct drm_gem_object *obj) 173c7fbcb71SThomas Zimmermann { 174a193f3b4SThomas Zimmermann struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 175a193f3b4SThomas Zimmermann 176a193f3b4SThomas Zimmermann return drm_gem_shmem_pin(shmem); 177c7fbcb71SThomas Zimmermann } 178c7fbcb71SThomas Zimmermann 179c7fbcb71SThomas Zimmermann /** 180c7fbcb71SThomas Zimmermann * drm_gem_shmem_object_unpin - GEM object function for drm_gem_shmem_unpin() 181c7fbcb71SThomas Zimmermann * @obj: GEM object 182c7fbcb71SThomas Zimmermann * 183c7fbcb71SThomas Zimmermann * This function wraps drm_gem_shmem_unpin(). Drivers that employ the shmem helpers should 184c7fbcb71SThomas Zimmermann * use it as their &drm_gem_object_funcs.unpin handler. 185c7fbcb71SThomas Zimmermann */ 186c7fbcb71SThomas Zimmermann static inline void drm_gem_shmem_object_unpin(struct drm_gem_object *obj) 187c7fbcb71SThomas Zimmermann { 188a193f3b4SThomas Zimmermann struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 189a193f3b4SThomas Zimmermann 190a193f3b4SThomas Zimmermann drm_gem_shmem_unpin(shmem); 191c7fbcb71SThomas Zimmermann } 192c7fbcb71SThomas Zimmermann 193c7fbcb71SThomas Zimmermann /** 194c7fbcb71SThomas Zimmermann * drm_gem_shmem_object_get_sg_table - GEM object function for drm_gem_shmem_get_sg_table() 195c7fbcb71SThomas Zimmermann * @obj: GEM object 196c7fbcb71SThomas Zimmermann * 197c7fbcb71SThomas Zimmermann * This function wraps drm_gem_shmem_get_sg_table(). Drivers that employ the shmem helpers should 198c7fbcb71SThomas Zimmermann * use it as their &drm_gem_object_funcs.get_sg_table handler. 199c7fbcb71SThomas Zimmermann * 200c7fbcb71SThomas Zimmermann * Returns: 2012b8428a1SLiu Zixian * A pointer to the scatter/gather table of pinned pages or error pointer on failure. 202c7fbcb71SThomas Zimmermann */ 203c7fbcb71SThomas Zimmermann static inline struct sg_table *drm_gem_shmem_object_get_sg_table(struct drm_gem_object *obj) 204c7fbcb71SThomas Zimmermann { 205a193f3b4SThomas Zimmermann struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 206a193f3b4SThomas Zimmermann 207a193f3b4SThomas Zimmermann return drm_gem_shmem_get_sg_table(shmem); 208c7fbcb71SThomas Zimmermann } 209c7fbcb71SThomas Zimmermann 210c7fbcb71SThomas Zimmermann /* 211c7fbcb71SThomas Zimmermann * drm_gem_shmem_object_vmap - GEM object function for drm_gem_shmem_vmap() 212c7fbcb71SThomas Zimmermann * @obj: GEM object 213c7fbcb71SThomas Zimmermann * @map: Returns the kernel virtual address of the SHMEM GEM object's backing store. 214c7fbcb71SThomas Zimmermann * 215c7fbcb71SThomas Zimmermann * This function wraps drm_gem_shmem_vmap(). Drivers that employ the shmem helpers should 216c7fbcb71SThomas Zimmermann * use it as their &drm_gem_object_funcs.vmap handler. 217c7fbcb71SThomas Zimmermann * 218c7fbcb71SThomas Zimmermann * Returns: 219c7fbcb71SThomas Zimmermann * 0 on success or a negative error code on failure. 220c7fbcb71SThomas Zimmermann */ 2217938f421SLucas De Marchi static inline int drm_gem_shmem_object_vmap(struct drm_gem_object *obj, 2227938f421SLucas De Marchi struct iosys_map *map) 223c7fbcb71SThomas Zimmermann { 224a193f3b4SThomas Zimmermann struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 225a193f3b4SThomas Zimmermann 226a193f3b4SThomas Zimmermann return drm_gem_shmem_vmap(shmem, map); 227c7fbcb71SThomas Zimmermann } 228c7fbcb71SThomas Zimmermann 229c7fbcb71SThomas Zimmermann /* 230c7fbcb71SThomas Zimmermann * drm_gem_shmem_object_vunmap - GEM object function for drm_gem_shmem_vunmap() 231c7fbcb71SThomas Zimmermann * @obj: GEM object 232c7fbcb71SThomas Zimmermann * @map: Kernel virtual address where the SHMEM GEM object was mapped 233c7fbcb71SThomas Zimmermann * 234c7fbcb71SThomas Zimmermann * This function wraps drm_gem_shmem_vunmap(). Drivers that employ the shmem helpers should 235c7fbcb71SThomas Zimmermann * use it as their &drm_gem_object_funcs.vunmap handler. 236c7fbcb71SThomas Zimmermann */ 2377938f421SLucas De Marchi static inline void drm_gem_shmem_object_vunmap(struct drm_gem_object *obj, 2387938f421SLucas De Marchi struct iosys_map *map) 239c7fbcb71SThomas Zimmermann { 240a193f3b4SThomas Zimmermann struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 241a193f3b4SThomas Zimmermann 242a193f3b4SThomas Zimmermann drm_gem_shmem_vunmap(shmem, map); 243c7fbcb71SThomas Zimmermann } 244c7fbcb71SThomas Zimmermann 245c7fbcb71SThomas Zimmermann /** 246c7fbcb71SThomas Zimmermann * drm_gem_shmem_object_mmap - GEM object function for drm_gem_shmem_mmap() 247c7fbcb71SThomas Zimmermann * @obj: GEM object 248c7fbcb71SThomas Zimmermann * @vma: VMA for the area to be mapped 249c7fbcb71SThomas Zimmermann * 250c7fbcb71SThomas Zimmermann * This function wraps drm_gem_shmem_mmap(). Drivers that employ the shmem helpers should 251c7fbcb71SThomas Zimmermann * use it as their &drm_gem_object_funcs.mmap handler. 252c7fbcb71SThomas Zimmermann * 253c7fbcb71SThomas Zimmermann * Returns: 254c7fbcb71SThomas Zimmermann * 0 on success or a negative error code on failure. 255c7fbcb71SThomas Zimmermann */ 256c7fbcb71SThomas Zimmermann static inline int drm_gem_shmem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma) 257c7fbcb71SThomas Zimmermann { 258a193f3b4SThomas Zimmermann struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 259a193f3b4SThomas Zimmermann 260a193f3b4SThomas Zimmermann return drm_gem_shmem_mmap(shmem, vma); 261c7fbcb71SThomas Zimmermann } 262c7fbcb71SThomas Zimmermann 263c7fbcb71SThomas Zimmermann /* 264c7fbcb71SThomas Zimmermann * Driver ops 265c7fbcb71SThomas Zimmermann */ 266c7fbcb71SThomas Zimmermann 2672194a63aSNoralf Trønnes struct drm_gem_object * 2682194a63aSNoralf Trønnes drm_gem_shmem_prime_import_sg_table(struct drm_device *dev, 2692194a63aSNoralf Trønnes struct dma_buf_attachment *attach, 2702194a63aSNoralf Trønnes struct sg_table *sgt); 271a193f3b4SThomas Zimmermann int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev, 272a193f3b4SThomas Zimmermann struct drm_mode_create_dumb *args); 2732194a63aSNoralf Trønnes 2742194a63aSNoralf Trønnes /** 2752194a63aSNoralf Trønnes * DRM_GEM_SHMEM_DRIVER_OPS - Default shmem GEM operations 2762194a63aSNoralf Trønnes * 2772194a63aSNoralf Trønnes * This macro provides a shortcut for setting the shmem GEM operations in 2782194a63aSNoralf Trønnes * the &drm_driver structure. 2792194a63aSNoralf Trønnes */ 2802194a63aSNoralf Trønnes #define DRM_GEM_SHMEM_DRIVER_OPS \ 2812194a63aSNoralf Trønnes .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \ 2822194a63aSNoralf Trønnes .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \ 2832194a63aSNoralf Trønnes .gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table, \ 2842194a63aSNoralf Trønnes .dumb_create = drm_gem_shmem_dumb_create 2852194a63aSNoralf Trønnes 2862194a63aSNoralf Trønnes #endif /* __DRM_GEM_SHMEM_HELPER_H__ */ 287