1 /* SPDX-License-Identifier: GPL-2.0 */ 1 /* SPDX-License-Identifier: GPL-2.0 */ 2 2 3 #ifndef __DRM_GEM_SHMEM_HELPER_H__ 3 #ifndef __DRM_GEM_SHMEM_HELPER_H__ 4 #define __DRM_GEM_SHMEM_HELPER_H__ 4 #define __DRM_GEM_SHMEM_HELPER_H__ 5 5 6 #include <linux/fs.h> 6 #include <linux/fs.h> 7 #include <linux/mm.h> 7 #include <linux/mm.h> 8 #include <linux/mutex.h> 8 #include <linux/mutex.h> 9 9 10 #include <drm/drm_file.h> 10 #include <drm/drm_file.h> 11 #include <drm/drm_gem.h> 11 #include <drm/drm_gem.h> 12 #include <drm/drm_ioctl.h> 12 #include <drm/drm_ioctl.h> 13 #include <drm/drm_prime.h> 13 #include <drm/drm_prime.h> 14 14 15 struct dma_buf_attachment; 15 struct dma_buf_attachment; 16 struct drm_mode_create_dumb; 16 struct drm_mode_create_dumb; 17 struct drm_printer; 17 struct drm_printer; 18 struct sg_table; 18 struct sg_table; 19 19 20 /** 20 /** 21 * struct drm_gem_shmem_object - GEM object ba 21 * struct drm_gem_shmem_object - GEM object backed by shmem 22 */ 22 */ 23 struct drm_gem_shmem_object { 23 struct drm_gem_shmem_object { 24 /** 24 /** 25 * @base: Base GEM object 25 * @base: Base GEM object 26 */ 26 */ 27 struct drm_gem_object base; 27 struct drm_gem_object base; 28 28 29 /** 29 /** >> 30 * @pages_lock: Protects the page table and use count >> 31 */ >> 32 struct mutex pages_lock; >> 33 >> 34 /** 30 * @pages: Page table 35 * @pages: Page table 31 */ 36 */ 32 struct page **pages; 37 struct page **pages; 33 38 34 /** 39 /** 35 * @pages_use_count: 40 * @pages_use_count: 36 * 41 * 37 * Reference count on the pages table. 42 * Reference count on the pages table. 38 * The pages are put when the count re 43 * The pages are put when the count reaches zero. 39 */ 44 */ 40 unsigned int pages_use_count; 45 unsigned int pages_use_count; 41 46 42 /** 47 /** 43 * @madv: State for madvise 48 * @madv: State for madvise 44 * 49 * 45 * 0 is active/inuse. 50 * 0 is active/inuse. 46 * A negative value is the object is p 51 * A negative value is the object is purged. 47 * Positive values are driver specific 52 * Positive values are driver specific and not used by the helpers. 48 */ 53 */ 49 int madv; 54 int madv; 50 55 51 /** 56 /** 52 * @madv_list: List entry for madvise 57 * @madv_list: List entry for madvise tracking 53 * 58 * 54 * Typically used by drivers to track 59 * Typically used by drivers to track purgeable objects 55 */ 60 */ 56 struct list_head madv_list; 61 struct list_head madv_list; 57 62 58 /** 63 /** 59 * @sgt: Scatter/gather table for impo 64 * @sgt: Scatter/gather table for imported PRIME buffers 60 */ 65 */ 61 struct sg_table *sgt; 66 struct sg_table *sgt; 62 67 63 /** 68 /** >> 69 * @vmap_lock: Protects the vmap address and use count >> 70 */ >> 71 struct mutex vmap_lock; >> 72 >> 73 /** 64 * @vaddr: Kernel virtual address of t 74 * @vaddr: Kernel virtual address of the backing memory 65 */ 75 */ 66 void *vaddr; 76 void *vaddr; 67 77 68 /** 78 /** 69 * @vmap_use_count: 79 * @vmap_use_count: 70 * 80 * 71 * Reference count on the virtual addr 81 * Reference count on the virtual address. 72 * The address are un-mapped when the 82 * The address are un-mapped when the count reaches zero. 73 */ 83 */ 74 unsigned int vmap_use_count; 84 unsigned int vmap_use_count; 75 85 76 /** 86 /** 77 * @pages_mark_dirty_on_put: 87 * @pages_mark_dirty_on_put: 78 * 88 * 79 * Mark pages as dirty when they are p 89 * Mark pages as dirty when they are put. 80 */ 90 */ 81 bool pages_mark_dirty_on_put : 1; 91 bool pages_mark_dirty_on_put : 1; 82 92 83 /** 93 /** 84 * @pages_mark_accessed_on_put: 94 * @pages_mark_accessed_on_put: 85 * 95 * 86 * Mark pages as accessed when they ar 96 * Mark pages as accessed when they are put. 87 */ 97 */ 88 bool pages_mark_accessed_on_put : 1; 98 bool pages_mark_accessed_on_put : 1; 89 99 90 /** 100 /** 91 * @map_wc: map object write-combined 101 * @map_wc: map object write-combined (instead of using shmem defaults). 92 */ 102 */ 93 bool map_wc : 1; 103 bool map_wc : 1; 94 }; 104 }; 95 105 96 #define to_drm_gem_shmem_obj(obj) \ 106 #define to_drm_gem_shmem_obj(obj) \ 97 container_of(obj, struct drm_gem_shmem 107 container_of(obj, struct drm_gem_shmem_object, base) 98 108 99 struct drm_gem_shmem_object *drm_gem_shmem_cre 109 struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size); 100 void drm_gem_shmem_free(struct drm_gem_shmem_o 110 void drm_gem_shmem_free(struct drm_gem_shmem_object *shmem); 101 111 >> 112 int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem); 102 void drm_gem_shmem_put_pages(struct drm_gem_sh 113 void drm_gem_shmem_put_pages(struct drm_gem_shmem_object *shmem); 103 int drm_gem_shmem_pin(struct drm_gem_shmem_obj 114 int drm_gem_shmem_pin(struct drm_gem_shmem_object *shmem); 104 void drm_gem_shmem_unpin(struct drm_gem_shmem_ 115 void drm_gem_shmem_unpin(struct drm_gem_shmem_object *shmem); 105 int drm_gem_shmem_vmap(struct drm_gem_shmem_ob 116 int drm_gem_shmem_vmap(struct drm_gem_shmem_object *shmem, 106 struct iosys_map *map); 117 struct iosys_map *map); 107 void drm_gem_shmem_vunmap(struct drm_gem_shmem 118 void drm_gem_shmem_vunmap(struct drm_gem_shmem_object *shmem, 108 struct iosys_map *ma 119 struct iosys_map *map); 109 int drm_gem_shmem_mmap(struct drm_gem_shmem_ob 120 int drm_gem_shmem_mmap(struct drm_gem_shmem_object *shmem, struct vm_area_struct *vma); 110 121 111 int drm_gem_shmem_pin_locked(struct drm_gem_sh << 112 void drm_gem_shmem_unpin_locked(struct drm_gem << 113 << 114 int drm_gem_shmem_madvise(struct drm_gem_shmem 122 int drm_gem_shmem_madvise(struct drm_gem_shmem_object *shmem, int madv); 115 123 116 static inline bool drm_gem_shmem_is_purgeable( 124 static inline bool drm_gem_shmem_is_purgeable(struct drm_gem_shmem_object *shmem) 117 { 125 { 118 return (shmem->madv > 0) && 126 return (shmem->madv > 0) && 119 !shmem->vmap_use_count && shme 127 !shmem->vmap_use_count && shmem->sgt && 120 !shmem->base.dma_buf && !shmem 128 !shmem->base.dma_buf && !shmem->base.import_attach; 121 } 129 } 122 130 123 void drm_gem_shmem_purge(struct drm_gem_shmem_ !! 131 void drm_gem_shmem_purge_locked(struct drm_gem_shmem_object *shmem); >> 132 bool drm_gem_shmem_purge(struct drm_gem_shmem_object *shmem); 124 133 125 struct sg_table *drm_gem_shmem_get_sg_table(st 134 struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_shmem_object *shmem); 126 struct sg_table *drm_gem_shmem_get_pages_sgt(s 135 struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_shmem_object *shmem); 127 136 128 void drm_gem_shmem_print_info(const struct drm 137 void drm_gem_shmem_print_info(const struct drm_gem_shmem_object *shmem, 129 struct drm_print 138 struct drm_printer *p, unsigned int indent); 130 139 131 extern const struct vm_operations_struct drm_g 140 extern const struct vm_operations_struct drm_gem_shmem_vm_ops; 132 141 133 /* 142 /* 134 * GEM object functions 143 * GEM object functions 135 */ 144 */ 136 145 137 /** 146 /** 138 * drm_gem_shmem_object_free - GEM object func 147 * drm_gem_shmem_object_free - GEM object function for drm_gem_shmem_free() 139 * @obj: GEM object to free 148 * @obj: GEM object to free 140 * 149 * 141 * This function wraps drm_gem_shmem_free(). D 150 * This function wraps drm_gem_shmem_free(). Drivers that employ the shmem helpers 142 * should use it as their &drm_gem_object_func 151 * should use it as their &drm_gem_object_funcs.free handler. 143 */ 152 */ 144 static inline void drm_gem_shmem_object_free(s 153 static inline void drm_gem_shmem_object_free(struct drm_gem_object *obj) 145 { 154 { 146 struct drm_gem_shmem_object *shmem = t 155 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 147 156 148 drm_gem_shmem_free(shmem); 157 drm_gem_shmem_free(shmem); 149 } 158 } 150 159 151 /** 160 /** 152 * drm_gem_shmem_object_print_info() - Print & 161 * drm_gem_shmem_object_print_info() - Print &drm_gem_shmem_object info for debugfs 153 * @p: DRM printer 162 * @p: DRM printer 154 * @indent: Tab indentation level 163 * @indent: Tab indentation level 155 * @obj: GEM object 164 * @obj: GEM object 156 * 165 * 157 * This function wraps drm_gem_shmem_print_inf 166 * This function wraps drm_gem_shmem_print_info(). Drivers that employ the shmem helpers should 158 * use this function as their &drm_gem_object_ 167 * use this function as their &drm_gem_object_funcs.print_info handler. 159 */ 168 */ 160 static inline void drm_gem_shmem_object_print_ 169 static inline void drm_gem_shmem_object_print_info(struct drm_printer *p, unsigned int indent, 161 170 const struct drm_gem_object *obj) 162 { 171 { 163 const struct drm_gem_shmem_object *shm 172 const struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 164 173 165 drm_gem_shmem_print_info(shmem, p, ind 174 drm_gem_shmem_print_info(shmem, p, indent); 166 } 175 } 167 176 168 /** 177 /** 169 * drm_gem_shmem_object_pin - GEM object funct 178 * drm_gem_shmem_object_pin - GEM object function for drm_gem_shmem_pin() 170 * @obj: GEM object 179 * @obj: GEM object 171 * 180 * 172 * This function wraps drm_gem_shmem_pin(). Dr 181 * This function wraps drm_gem_shmem_pin(). Drivers that employ the shmem helpers should 173 * use it as their &drm_gem_object_funcs.pin h 182 * use it as their &drm_gem_object_funcs.pin handler. 174 */ 183 */ 175 static inline int drm_gem_shmem_object_pin(str 184 static inline int drm_gem_shmem_object_pin(struct drm_gem_object *obj) 176 { 185 { 177 struct drm_gem_shmem_object *shmem = t 186 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 178 187 179 return drm_gem_shmem_pin_locked(shmem) !! 188 return drm_gem_shmem_pin(shmem); 180 } 189 } 181 190 182 /** 191 /** 183 * drm_gem_shmem_object_unpin - GEM object fun 192 * drm_gem_shmem_object_unpin - GEM object function for drm_gem_shmem_unpin() 184 * @obj: GEM object 193 * @obj: GEM object 185 * 194 * 186 * This function wraps drm_gem_shmem_unpin(). 195 * This function wraps drm_gem_shmem_unpin(). Drivers that employ the shmem helpers should 187 * use it as their &drm_gem_object_funcs.unpin 196 * use it as their &drm_gem_object_funcs.unpin handler. 188 */ 197 */ 189 static inline void drm_gem_shmem_object_unpin( 198 static inline void drm_gem_shmem_object_unpin(struct drm_gem_object *obj) 190 { 199 { 191 struct drm_gem_shmem_object *shmem = t 200 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 192 201 193 drm_gem_shmem_unpin_locked(shmem); !! 202 drm_gem_shmem_unpin(shmem); 194 } 203 } 195 204 196 /** 205 /** 197 * drm_gem_shmem_object_get_sg_table - GEM obj 206 * drm_gem_shmem_object_get_sg_table - GEM object function for drm_gem_shmem_get_sg_table() 198 * @obj: GEM object 207 * @obj: GEM object 199 * 208 * 200 * This function wraps drm_gem_shmem_get_sg_ta 209 * This function wraps drm_gem_shmem_get_sg_table(). Drivers that employ the shmem helpers should 201 * use it as their &drm_gem_object_funcs.get_s 210 * use it as their &drm_gem_object_funcs.get_sg_table handler. 202 * 211 * 203 * Returns: 212 * Returns: 204 * A pointer to the scatter/gather table of pi 213 * A pointer to the scatter/gather table of pinned pages or error pointer on failure. 205 */ 214 */ 206 static inline struct sg_table *drm_gem_shmem_o 215 static inline struct sg_table *drm_gem_shmem_object_get_sg_table(struct drm_gem_object *obj) 207 { 216 { 208 struct drm_gem_shmem_object *shmem = t 217 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 209 218 210 return drm_gem_shmem_get_sg_table(shme 219 return drm_gem_shmem_get_sg_table(shmem); 211 } 220 } 212 221 213 /* 222 /* 214 * drm_gem_shmem_object_vmap - GEM object func 223 * drm_gem_shmem_object_vmap - GEM object function for drm_gem_shmem_vmap() 215 * @obj: GEM object 224 * @obj: GEM object 216 * @map: Returns the kernel virtual address of 225 * @map: Returns the kernel virtual address of the SHMEM GEM object's backing store. 217 * 226 * 218 * This function wraps drm_gem_shmem_vmap(). D 227 * This function wraps drm_gem_shmem_vmap(). Drivers that employ the shmem helpers should 219 * use it as their &drm_gem_object_funcs.vmap 228 * use it as their &drm_gem_object_funcs.vmap handler. 220 * 229 * 221 * Returns: 230 * Returns: 222 * 0 on success or a negative error code on fa 231 * 0 on success or a negative error code on failure. 223 */ 232 */ 224 static inline int drm_gem_shmem_object_vmap(st 233 static inline int drm_gem_shmem_object_vmap(struct drm_gem_object *obj, 225 st 234 struct iosys_map *map) 226 { 235 { 227 struct drm_gem_shmem_object *shmem = t 236 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 228 237 229 return drm_gem_shmem_vmap(shmem, map); 238 return drm_gem_shmem_vmap(shmem, map); 230 } 239 } 231 240 232 /* 241 /* 233 * drm_gem_shmem_object_vunmap - GEM object fu 242 * drm_gem_shmem_object_vunmap - GEM object function for drm_gem_shmem_vunmap() 234 * @obj: GEM object 243 * @obj: GEM object 235 * @map: Kernel virtual address where the SHME 244 * @map: Kernel virtual address where the SHMEM GEM object was mapped 236 * 245 * 237 * This function wraps drm_gem_shmem_vunmap(). 246 * This function wraps drm_gem_shmem_vunmap(). Drivers that employ the shmem helpers should 238 * use it as their &drm_gem_object_funcs.vunma 247 * use it as their &drm_gem_object_funcs.vunmap handler. 239 */ 248 */ 240 static inline void drm_gem_shmem_object_vunmap 249 static inline void drm_gem_shmem_object_vunmap(struct drm_gem_object *obj, 241 250 struct iosys_map *map) 242 { 251 { 243 struct drm_gem_shmem_object *shmem = t 252 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 244 253 245 drm_gem_shmem_vunmap(shmem, map); 254 drm_gem_shmem_vunmap(shmem, map); 246 } 255 } 247 256 248 /** 257 /** 249 * drm_gem_shmem_object_mmap - GEM object func 258 * drm_gem_shmem_object_mmap - GEM object function for drm_gem_shmem_mmap() 250 * @obj: GEM object 259 * @obj: GEM object 251 * @vma: VMA for the area to be mapped 260 * @vma: VMA for the area to be mapped 252 * 261 * 253 * This function wraps drm_gem_shmem_mmap(). D 262 * This function wraps drm_gem_shmem_mmap(). Drivers that employ the shmem helpers should 254 * use it as their &drm_gem_object_funcs.mmap 263 * use it as their &drm_gem_object_funcs.mmap handler. 255 * 264 * 256 * Returns: 265 * Returns: 257 * 0 on success or a negative error code on fa 266 * 0 on success or a negative error code on failure. 258 */ 267 */ 259 static inline int drm_gem_shmem_object_mmap(st 268 static inline int drm_gem_shmem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma) 260 { 269 { 261 struct drm_gem_shmem_object *shmem = t 270 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj); 262 271 263 return drm_gem_shmem_mmap(shmem, vma); 272 return drm_gem_shmem_mmap(shmem, vma); 264 } 273 } 265 274 266 /* 275 /* 267 * Driver ops 276 * Driver ops 268 */ 277 */ 269 278 270 struct drm_gem_object * 279 struct drm_gem_object * 271 drm_gem_shmem_prime_import_sg_table(struct drm 280 drm_gem_shmem_prime_import_sg_table(struct drm_device *dev, 272 struct dma 281 struct dma_buf_attachment *attach, 273 struct sg_ 282 struct sg_table *sgt); 274 int drm_gem_shmem_dumb_create(struct drm_file 283 int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev, 275 struct drm_mode_ 284 struct drm_mode_create_dumb *args); 276 285 277 /** 286 /** 278 * DRM_GEM_SHMEM_DRIVER_OPS - Default shmem GE 287 * DRM_GEM_SHMEM_DRIVER_OPS - Default shmem GEM operations 279 * 288 * 280 * This macro provides a shortcut for setting 289 * This macro provides a shortcut for setting the shmem GEM operations in 281 * the &drm_driver structure. 290 * the &drm_driver structure. 282 */ 291 */ 283 #define DRM_GEM_SHMEM_DRIVER_OPS \ 292 #define DRM_GEM_SHMEM_DRIVER_OPS \ >> 293 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \ >> 294 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \ 284 .gem_prime_import_sg_table = drm_gem_s 295 .gem_prime_import_sg_table = drm_gem_shmem_prime_import_sg_table, \ 285 .dumb_create = drm_gem_s !! 296 .gem_prime_mmap = drm_gem_prime_mmap, \ >> 297 .dumb_create = drm_gem_shmem_dumb_create 286 298 287 #endif /* __DRM_GEM_SHMEM_HELPER_H__ */ 299 #endif /* __DRM_GEM_SHMEM_HELPER_H__ */ 288 300
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.