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_lock: Protects the page table and use count 31 */ 32 struct mutex pages_lock; 33 34 /** 35 * @pages: Page table 36 */ 37 struct page **pages; 38 39 /** 40 * @pages_use_count: 41 * 42 * Reference count on the pages table. 43 * The pages are put when the count reaches zero. 44 */ 45 unsigned int pages_use_count; 46 47 int madv; 48 struct list_head madv_list; 49 50 /** 51 * @pages_mark_dirty_on_put: 52 * 53 * Mark pages as dirty when they are put. 54 */ 55 unsigned int pages_mark_dirty_on_put : 1; 56 57 /** 58 * @pages_mark_accessed_on_put: 59 * 60 * Mark pages as accessed when they are put. 61 */ 62 unsigned int pages_mark_accessed_on_put : 1; 63 64 /** 65 * @sgt: Scatter/gather table for imported PRIME buffers 66 */ 67 struct sg_table *sgt; 68 69 /** 70 * @vmap_lock: Protects the vmap address and use count 71 */ 72 struct mutex vmap_lock; 73 74 /** 75 * @vaddr: Kernel virtual address of the backing memory 76 */ 77 void *vaddr; 78 79 /** 80 * @vmap_use_count: 81 * 82 * Reference count on the virtual address. 83 * The address are un-mapped when the count reaches zero. 84 */ 85 unsigned int vmap_use_count; 86 }; 87 88 #define to_drm_gem_shmem_obj(obj) \ 89 container_of(obj, struct drm_gem_shmem_object, base) 90 91 /** 92 * DEFINE_DRM_GEM_SHMEM_FOPS() - Macro to generate file operations for shmem drivers 93 * @name: name for the generated structure 94 * 95 * This macro autogenerates a suitable &struct file_operations for shmem based 96 * drivers, which can be assigned to &drm_driver.fops. Note that this structure 97 * cannot be shared between drivers, because it contains a reference to the 98 * current module using THIS_MODULE. 99 * 100 * Note that the declaration is already marked as static - if you need a 101 * non-static version of this you're probably doing it wrong and will break the 102 * THIS_MODULE reference by accident. 103 */ 104 #define DEFINE_DRM_GEM_SHMEM_FOPS(name) \ 105 static const struct file_operations name = {\ 106 .owner = THIS_MODULE,\ 107 .open = drm_open,\ 108 .release = drm_release,\ 109 .unlocked_ioctl = drm_ioctl,\ 110 .compat_ioctl = drm_compat_ioctl,\ 111 .poll = drm_poll,\ 112 .read = drm_read,\ 113 .llseek = noop_llseek,\ 114 .mmap = drm_gem_shmem_mmap, \ 115 } 116 117 struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size); 118 void drm_gem_shmem_free_object(struct drm_gem_object *obj); 119 120 int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem); 121 void drm_gem_shmem_put_pages(struct drm_gem_shmem_object *shmem); 122 int drm_gem_shmem_pin(struct drm_gem_object *obj); 123 void drm_gem_shmem_unpin(struct drm_gem_object *obj); 124 void *drm_gem_shmem_vmap(struct drm_gem_object *obj); 125 void drm_gem_shmem_vunmap(struct drm_gem_object *obj, void *vaddr); 126 127 int drm_gem_shmem_madvise(struct drm_gem_object *obj, int madv); 128 129 static inline bool drm_gem_shmem_is_purgeable(struct drm_gem_shmem_object *shmem) 130 { 131 return (shmem->madv > 0) && 132 !shmem->vmap_use_count && shmem->sgt && 133 !shmem->base.dma_buf && !shmem->base.import_attach; 134 } 135 136 void drm_gem_shmem_purge_locked(struct drm_gem_object *obj); 137 void drm_gem_shmem_purge(struct drm_gem_object *obj); 138 139 struct drm_gem_shmem_object * 140 drm_gem_shmem_create_with_handle(struct drm_file *file_priv, 141 struct drm_device *dev, size_t size, 142 uint32_t *handle); 143 int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev, 144 struct drm_mode_create_dumb *args); 145 146 int drm_gem_shmem_mmap(struct file *filp, struct vm_area_struct *vma); 147 148 extern const struct vm_operations_struct drm_gem_shmem_vm_ops; 149 150 void drm_gem_shmem_print_info(struct drm_printer *p, unsigned int indent, 151 const struct drm_gem_object *obj); 152 153 struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_object *obj); 154 struct drm_gem_object * 155 drm_gem_shmem_prime_import_sg_table(struct drm_device *dev, 156 struct dma_buf_attachment *attach, 157 struct sg_table *sgt); 158 159 struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_object *obj); 160 161 /** 162 * DRM_GEM_SHMEM_DRIVER_OPS - Default shmem GEM operations 163 * 164 * This macro provides a shortcut for setting the shmem GEM operations in 165 * the &drm_driver structure. 166 */ 167 #define DRM_GEM_SHMEM_DRIVER_OPS \ 168 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \ 169 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \ 170 .gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table, \ 171 .gem_prime_mmap = drm_gem_prime_mmap, \ 172 .dumb_create = drm_gem_shmem_dumb_create 173 174 #endif /* __DRM_GEM_SHMEM_HELPER_H__ */ 175