Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

staging: android: ion: Collapse internal header files

Ion current has ion_priv.h and ion.h as header files. ion.h was intended
to be used for public APIs but Ion never ended up really having anything
public. Combine the two headers so there is only one internal header.

Signed-off-by: Laura Abbott <labbott@redhat.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Laura Abbott and committed by
Greg Kroah-Hartman
eb9751db ddeb587b

+416 -554
-1
drivers/staging/android/ion/ion-ioctl.c
··· 19 19 #include <linux/uaccess.h> 20 20 21 21 #include "ion.h" 22 - #include "ion_priv.h" 23 22 24 23 union ion_ioctl_arg { 25 24 struct ion_fd_data fd;
-1
drivers/staging/android/ion/ion.c
··· 39 39 #include <linux/sched/task.h> 40 40 41 41 #include "ion.h" 42 - #include "ion_priv.h" 43 42 44 43 bool ion_buffer_cached(struct ion_buffer *buffer) 45 44 {
+414 -93
drivers/staging/android/ion/ion.h
··· 14 14 * 15 15 */ 16 16 17 - #ifndef _LINUX_ION_H 18 - #define _LINUX_ION_H 17 + #ifndef _ION_H 18 + #define _ION_H 19 19 20 + #include <linux/device.h> 21 + #include <linux/dma-direction.h> 22 + #include <linux/kref.h> 23 + #include <linux/mm_types.h> 24 + #include <linux/mutex.h> 25 + #include <linux/rbtree.h> 26 + #include <linux/sched.h> 27 + #include <linux/shrinker.h> 20 28 #include <linux/types.h> 29 + #include <linux/miscdevice.h> 21 30 22 31 #include "../uapi/ion.h" 23 - 24 - struct ion_handle; 25 - struct ion_device; 26 - struct ion_heap; 27 - struct ion_mapper; 28 - struct ion_client; 29 - struct ion_buffer; 30 32 31 33 /** 32 34 * struct ion_platform_heap - defines a heap in the given platform 33 35 * @type: type of the heap from ion_heap_type enum 34 - * @id: unique identifier for heap. When allocating higher numbers 36 + * @id: unique identifier for heap. When allocating higher numb ers 35 37 * will be allocated from first. At allocation these are passed 36 38 * as a bit mask and therefore can not exceed ION_NUM_HEAP_IDS. 37 39 * @name: used for debug purposes ··· 54 52 }; 55 53 56 54 /** 57 - * struct ion_platform_data - array of platform heaps passed from board file 58 - * @nr: number of structures in the array 59 - * @heaps: array of platform_heap structions 60 - * 61 - * Provided by the board file in the form of platform data to a platform device. 55 + * struct ion_buffer - metadata for a particular buffer 56 + * @ref: reference count 57 + * @node: node in the ion_device buffers tree 58 + * @dev: back pointer to the ion_device 59 + * @heap: back pointer to the heap the buffer came from 60 + * @flags: buffer specific flags 61 + * @private_flags: internal buffer specific flags 62 + * @size: size of the buffer 63 + * @priv_virt: private data to the buffer representable as 64 + * a void * 65 + * @lock: protects the buffers cnt fields 66 + * @kmap_cnt: number of times the buffer is mapped to the kernel 67 + * @vaddr: the kernel mapping if kmap_cnt is not zero 68 + * @sg_table: the sg table for the buffer if dmap_cnt is not zero 69 + * @pages: flat array of pages in the buffer -- used by fault 70 + * handler and only valid for buffers that are faulted in 71 + * @vmas: list of vma's mapping this buffer 72 + * @handle_count: count of handles referencing this buffer 73 + * @task_comm: taskcomm of last client to reference this buffer in a 74 + * handle, used for debugging 75 + * @pid: pid of last client to reference this buffer in a 76 + * handle, used for debugging 62 77 */ 63 - struct ion_platform_data { 64 - int nr; 65 - struct ion_platform_heap *heaps; 78 + struct ion_buffer { 79 + struct kref ref; 80 + union { 81 + struct rb_node node; 82 + struct list_head list; 83 + }; 84 + struct ion_device *dev; 85 + struct ion_heap *heap; 86 + unsigned long flags; 87 + unsigned long private_flags; 88 + size_t size; 89 + void *priv_virt; 90 + struct mutex lock; 91 + int kmap_cnt; 92 + void *vaddr; 93 + struct sg_table *sg_table; 94 + struct page **pages; 95 + struct list_head vmas; 96 + struct list_head attachments; 97 + /* used to track orphaned buffers */ 98 + int handle_count; 99 + char task_comm[TASK_COMM_LEN]; 100 + pid_t pid; 101 + }; 102 + void ion_buffer_destroy(struct ion_buffer *buffer); 103 + 104 + /** 105 + * struct ion_device - the metadata of the ion device node 106 + * @dev: the actual misc device 107 + * @buffers: an rb tree of all the existing buffers 108 + * @buffer_lock: lock protecting the tree of buffers 109 + * @lock: rwsem protecting the tree of heaps and clients 110 + * @heaps: list of all the heaps in the system 111 + * @user_clients: list of all the clients created from userspace 112 + */ 113 + struct ion_device { 114 + struct miscdevice dev; 115 + struct rb_root buffers; 116 + struct mutex buffer_lock; 117 + struct rw_semaphore lock; 118 + struct plist_head heaps; 119 + struct rb_root clients; 120 + struct dentry *debug_root; 121 + struct dentry *heaps_debug_root; 122 + struct dentry *clients_debug_root; 123 + int heap_cnt; 66 124 }; 67 125 68 126 /** 69 - * ion_client_create() - allocate a client and returns it 70 - * @dev: the global ion device 127 + * struct ion_client - a process/hw block local address space 128 + * @node: node in the tree of all clients 129 + * @dev: backpointer to ion device 130 + * @handles: an rb tree of all the handles in this client 131 + * @idr: an idr space for allocating handle ids 132 + * @lock: lock protecting the tree of handles 71 133 * @name: used for debugging 134 + * @display_name: used for debugging (unique version of @name) 135 + * @display_serial: used for debugging (to make display_name unique) 136 + * @task: used for debugging 137 + * 138 + * A client represents a list of buffers this client may access. 139 + * The mutex stored here is used to protect both handles tree 140 + * as well as the handles themselves, and should be held while modifying either. 72 141 */ 73 - struct ion_client *ion_client_create(struct ion_device *dev, 74 - const char *name); 142 + struct ion_client { 143 + struct rb_node node; 144 + struct ion_device *dev; 145 + struct rb_root handles; 146 + struct idr idr; 147 + struct mutex lock; 148 + const char *name; 149 + char *display_name; 150 + int display_serial; 151 + struct task_struct *task; 152 + pid_t pid; 153 + struct dentry *debug_root; 154 + }; 75 155 76 156 /** 77 - * ion_client_destroy() - free's a client and all it's handles 78 - * @client: the client 157 + * ion_handle - a client local reference to a buffer 158 + * @ref: reference count 159 + * @client: back pointer to the client the buffer resides in 160 + * @buffer: pointer to the buffer 161 + * @node: node in the client's handle rbtree 162 + * @kmap_cnt: count of times this client has mapped to kernel 163 + * @id: client-unique id allocated by client->idr 79 164 * 80 - * Free the provided client and all it's resources including 81 - * any handles it is holding. 165 + * Modifications to node, map_cnt or mapping should be protected by the 166 + * lock in the client. Other fields are never changed after initialization. 82 167 */ 83 - void ion_client_destroy(struct ion_client *client); 168 + struct ion_handle { 169 + struct kref ref; 170 + struct ion_client *client; 171 + struct ion_buffer *buffer; 172 + struct rb_node node; 173 + unsigned int kmap_cnt; 174 + int id; 175 + }; 84 176 85 177 /** 86 - * ion_alloc - allocate ion memory 87 - * @client: the client 88 - * @len: size of the allocation 89 - * @heap_id_mask: mask of heaps to allocate from, if multiple bits are set 90 - * heaps will be tried in order from highest to lowest 91 - * id 92 - * @flags: heap flags, the low 16 bits are consumed by ion, the 93 - * high 16 bits are passed on to the respective heap and 94 - * can be heap custom 178 + * struct ion_heap_ops - ops to operate on a given heap 179 + * @allocate: allocate memory 180 + * @free: free memory 181 + * @map_kernel map memory to the kernel 182 + * @unmap_kernel unmap memory to the kernel 183 + * @map_user map memory to userspace 95 184 * 96 - * Allocate memory in one of the heaps provided in heap mask and return 97 - * an opaque handle to it. 185 + * allocate, phys, and map_user return 0 on success, -errno on error. 186 + * map_dma and map_kernel return pointer on success, ERR_PTR on 187 + * error. @free will be called with ION_PRIV_FLAG_SHRINKER_FREE set in 188 + * the buffer's private_flags when called from a shrinker. In that 189 + * case, the pages being free'd must be truly free'd back to the 190 + * system, not put in a page pool or otherwise cached. 98 191 */ 192 + struct ion_heap_ops { 193 + int (*allocate)(struct ion_heap *heap, 194 + struct ion_buffer *buffer, unsigned long len, 195 + unsigned long flags); 196 + void (*free)(struct ion_buffer *buffer); 197 + void * (*map_kernel)(struct ion_heap *heap, struct ion_buffer *buffer); 198 + void (*unmap_kernel)(struct ion_heap *heap, struct ion_buffer *buffer); 199 + int (*map_user)(struct ion_heap *mapper, struct ion_buffer *buffer, 200 + struct vm_area_struct *vma); 201 + int (*shrink)(struct ion_heap *heap, gfp_t gfp_mask, int nr_to_scan); 202 + }; 203 + 204 + /** 205 + * heap flags - flags between the heaps and core ion code 206 + */ 207 + #define ION_HEAP_FLAG_DEFER_FREE (1 << 0) 208 + 209 + /** 210 + * private flags - flags internal to ion 211 + */ 212 + /* 213 + * Buffer is being freed from a shrinker function. Skip any possible 214 + * heap-specific caching mechanism (e.g. page pools). Guarantees that 215 + * any buffer storage that came from the system allocator will be 216 + * returned to the system allocator. 217 + */ 218 + #define ION_PRIV_FLAG_SHRINKER_FREE (1 << 0) 219 + 220 + /** 221 + * struct ion_heap - represents a heap in the system 222 + * @node: rb node to put the heap on the device's tree of heaps 223 + * @dev: back pointer to the ion_device 224 + * @type: type of heap 225 + * @ops: ops struct as above 226 + * @flags: flags 227 + * @id: id of heap, also indicates priority of this heap when 228 + * allocating. These are specified by platform data and 229 + * MUST be unique 230 + * @name: used for debugging 231 + * @shrinker: a shrinker for the heap 232 + * @free_list: free list head if deferred free is used 233 + * @free_list_size size of the deferred free list in bytes 234 + * @lock: protects the free list 235 + * @waitqueue: queue to wait on from deferred free thread 236 + * @task: task struct of deferred free thread 237 + * @debug_show: called when heap debug file is read to add any 238 + * heap specific debug info to output 239 + * 240 + * Represents a pool of memory from which buffers can be made. In some 241 + * systems the only heap is regular system memory allocated via vmalloc. 242 + * On others, some blocks might require large physically contiguous buffers 243 + * that are allocated from a specially reserved heap. 244 + */ 245 + struct ion_heap { 246 + struct plist_node node; 247 + struct ion_device *dev; 248 + enum ion_heap_type type; 249 + struct ion_heap_ops *ops; 250 + unsigned long flags; 251 + unsigned int id; 252 + const char *name; 253 + struct shrinker shrinker; 254 + struct list_head free_list; 255 + size_t free_list_size; 256 + spinlock_t free_lock; 257 + wait_queue_head_t waitqueue; 258 + struct task_struct *task; 259 + 260 + int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *); 261 + }; 262 + 263 + /** 264 + * ion_buffer_cached - this ion buffer is cached 265 + * @buffer: buffer 266 + * 267 + * indicates whether this ion buffer is cached 268 + */ 269 + bool ion_buffer_cached(struct ion_buffer *buffer); 270 + 271 + /** 272 + * ion_buffer_fault_user_mappings - fault in user mappings of this buffer 273 + * @buffer: buffer 274 + * 275 + * indicates whether userspace mappings of this buffer will be faulted 276 + * in, this can affect how buffers are allocated from the heap. 277 + */ 278 + bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer); 279 + 280 + /** 281 + * ion_device_create - allocates and returns an ion device 282 + * 283 + * returns a valid device or -PTR_ERR 284 + */ 285 + struct ion_device *ion_device_create(void); 286 + 287 + /** 288 + * ion_device_destroy - free and device and it's resource 289 + * @dev: the device 290 + */ 291 + void ion_device_destroy(struct ion_device *dev); 292 + 293 + /** 294 + * ion_device_add_heap - adds a heap to the ion device 295 + * @dev: the device 296 + * @heap: the heap to add 297 + */ 298 + void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap); 299 + 300 + /** 301 + * some helpers for common operations on buffers using the sg_table 302 + * and vaddr fields 303 + */ 304 + void *ion_heap_map_kernel(struct ion_heap *heap, struct ion_buffer *buffer); 305 + void ion_heap_unmap_kernel(struct ion_heap *heap, struct ion_buffer *buffer); 306 + int ion_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer, 307 + struct vm_area_struct *vma); 308 + int ion_heap_buffer_zero(struct ion_buffer *buffer); 309 + int ion_heap_pages_zero(struct page *page, size_t size, pgprot_t pgprot); 310 + 99 311 struct ion_handle *ion_alloc(struct ion_client *client, size_t len, 100 - unsigned int heap_id_mask, 101 - unsigned int flags); 312 + unsigned int heap_id_mask, 313 + unsigned int flags); 102 314 103 - /** 104 - * ion_free - free a handle 105 - * @client: the client 106 - * @handle: the handle to free 107 - * 108 - * Free the provided handle. 109 - */ 110 315 void ion_free(struct ion_client *client, struct ion_handle *handle); 111 316 112 - /** 113 - * ion_map_kernel - create mapping for the given handle 114 - * @client: the client 115 - * @handle: handle to map 116 - * 117 - * Map the given handle into the kernel and return a kernel address that 118 - * can be used to access this address. 119 - */ 120 - void *ion_map_kernel(struct ion_client *client, struct ion_handle *handle); 121 - 122 - /** 123 - * ion_unmap_kernel() - destroy a kernel mapping for a handle 124 - * @client: the client 125 - * @handle: handle to unmap 126 - */ 127 - void ion_unmap_kernel(struct ion_client *client, struct ion_handle *handle); 128 - 129 - /** 130 - * ion_share_dma_buf() - share buffer as dma-buf 131 - * @client: the client 132 - * @handle: the handle 133 - */ 134 - struct dma_buf *ion_share_dma_buf(struct ion_client *client, 135 - struct ion_handle *handle); 136 - 137 - /** 138 - * ion_share_dma_buf_fd() - given an ion client, create a dma-buf fd 139 - * @client: the client 140 - * @handle: the handle 141 - */ 142 317 int ion_share_dma_buf_fd(struct ion_client *client, struct ion_handle *handle); 143 318 144 319 /** 145 - * ion_import_dma_buf() - get ion_handle from dma-buf 146 - * @client: the client 147 - * @dmabuf: the dma-buf 320 + * ion_heap_init_shrinker 321 + * @heap: the heap 148 322 * 149 - * Get the ion_buffer associated with the dma-buf and return the ion_handle. 150 - * If no ion_handle exists for this buffer, return newly created ion_handle. 151 - * If dma-buf from another exporter is passed, return ERR_PTR(-EINVAL) 323 + * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag or defines the shrink op 324 + * this function will be called to setup a shrinker to shrink the freelists 325 + * and call the heap's shrink op. 152 326 */ 153 - struct ion_handle *ion_import_dma_buf(struct ion_client *client, 154 - struct dma_buf *dmabuf); 327 + void ion_heap_init_shrinker(struct ion_heap *heap); 155 328 156 329 /** 157 - * ion_import_dma_buf_fd() - given a dma-buf fd from the ion exporter get handle 158 - * @client: the client 159 - * @fd: the dma-buf fd 330 + * ion_heap_init_deferred_free -- initialize deferred free functionality 331 + * @heap: the heap 160 332 * 161 - * Given an dma-buf fd that was allocated through ion via ion_share_dma_buf_fd, 162 - * import that fd and return a handle representing it. If a dma-buf from 163 - * another exporter is passed in this function will return ERR_PTR(-EINVAL) 333 + * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag this function will 334 + * be called to setup deferred frees. Calls to free the buffer will 335 + * return immediately and the actual free will occur some time later 164 336 */ 165 - struct ion_handle *ion_import_dma_buf_fd(struct ion_client *client, int fd); 337 + int ion_heap_init_deferred_free(struct ion_heap *heap); 166 338 167 - #endif /* _LINUX_ION_H */ 339 + /** 340 + * ion_heap_freelist_add - add a buffer to the deferred free list 341 + * @heap: the heap 342 + * @buffer: the buffer 343 + * 344 + * Adds an item to the deferred freelist. 345 + */ 346 + void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer); 347 + 348 + /** 349 + * ion_heap_freelist_drain - drain the deferred free list 350 + * @heap: the heap 351 + * @size: amount of memory to drain in bytes 352 + * 353 + * Drains the indicated amount of memory from the deferred freelist immediately. 354 + * Returns the total amount freed. The total freed may be higher depending 355 + * on the size of the items in the list, or lower if there is insufficient 356 + * total memory on the freelist. 357 + */ 358 + size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size); 359 + 360 + /** 361 + * ion_heap_freelist_shrink - drain the deferred free 362 + * list, skipping any heap-specific 363 + * pooling or caching mechanisms 364 + * 365 + * @heap: the heap 366 + * @size: amount of memory to drain in bytes 367 + * 368 + * Drains the indicated amount of memory from the deferred freelist immediately. 369 + * Returns the total amount freed. The total freed may be higher depending 370 + * on the size of the items in the list, or lower if there is insufficient 371 + * total memory on the freelist. 372 + * 373 + * Unlike with @ion_heap_freelist_drain, don't put any pages back into 374 + * page pools or otherwise cache the pages. Everything must be 375 + * genuinely free'd back to the system. If you're free'ing from a 376 + * shrinker you probably want to use this. Note that this relies on 377 + * the heap.ops.free callback honoring the ION_PRIV_FLAG_SHRINKER_FREE 378 + * flag. 379 + */ 380 + size_t ion_heap_freelist_shrink(struct ion_heap *heap, 381 + size_t size); 382 + 383 + /** 384 + * ion_heap_freelist_size - returns the size of the freelist in bytes 385 + * @heap: the heap 386 + */ 387 + size_t ion_heap_freelist_size(struct ion_heap *heap); 388 + 389 + 390 + /** 391 + * functions for creating and destroying the built in ion heaps. 392 + * architectures can add their own custom architecture specific 393 + * heaps as appropriate. 394 + */ 395 + 396 + 397 + struct ion_heap *ion_heap_create(struct ion_platform_heap *heap_data); 398 + void ion_heap_destroy(struct ion_heap *heap); 399 + 400 + struct ion_heap *ion_system_heap_create(struct ion_platform_heap *unused); 401 + void ion_system_heap_destroy(struct ion_heap *heap); 402 + struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *heap); 403 + void ion_system_contig_heap_destroy(struct ion_heap *heap); 404 + 405 + struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *heap_data); 406 + void ion_carveout_heap_destroy(struct ion_heap *heap); 407 + 408 + struct ion_heap *ion_chunk_heap_create(struct ion_platform_heap *heap_data); 409 + void ion_chunk_heap_destroy(struct ion_heap *heap); 410 + 411 + struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *data); 412 + void ion_cma_heap_destroy(struct ion_heap *heap); 413 + 414 + /** 415 + * functions for creating and destroying a heap pool -- allows you 416 + * to keep a pool of pre allocated memory to use from your heap. Keeping 417 + * a pool of memory that is ready for dma, ie any cached mapping have been 418 + * invalidated from the cache, provides a significant performance benefit on 419 + * many systems 420 + */ 421 + 422 + /** 423 + * struct ion_page_pool - pagepool struct 424 + * @high_count: number of highmem items in the pool 425 + * @low_count: number of lowmem items in the pool 426 + * @high_items: list of highmem items 427 + * @low_items: list of lowmem items 428 + * @mutex: lock protecting this struct and especially the count 429 + * item list 430 + * @gfp_mask: gfp_mask to use from alloc 431 + * @order: order of pages in the pool 432 + * @list: plist node for list of pools 433 + * @cached: it's cached pool or not 434 + * 435 + * Allows you to keep a pool of pre allocated pages to use from your heap. 436 + * Keeping a pool of pages that is ready for dma, ie any cached mapping have 437 + * been invalidated from the cache, provides a significant performance benefit 438 + * on many systems 439 + */ 440 + struct ion_page_pool { 441 + int high_count; 442 + int low_count; 443 + bool cached; 444 + struct list_head high_items; 445 + struct list_head low_items; 446 + struct mutex mutex; 447 + gfp_t gfp_mask; 448 + unsigned int order; 449 + struct plist_node list; 450 + }; 451 + 452 + struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order, 453 + bool cached); 454 + void ion_page_pool_destroy(struct ion_page_pool *pool); 455 + struct page *ion_page_pool_alloc(struct ion_page_pool *pool); 456 + void ion_page_pool_free(struct ion_page_pool *pool, struct page *page); 457 + 458 + /** ion_page_pool_shrink - shrinks the size of the memory cached in the pool 459 + * @pool: the pool 460 + * @gfp_mask: the memory type to reclaim 461 + * @nr_to_scan: number of items to shrink in pages 462 + * 463 + * returns the number of items freed in pages 464 + */ 465 + int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask, 466 + int nr_to_scan); 467 + 468 + long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg); 469 + 470 + struct ion_handle *ion_handle_get_by_id_nolock(struct ion_client *client, 471 + int id); 472 + 473 + void ion_free_nolock(struct ion_client *client, struct ion_handle *handle); 474 + 475 + int ion_handle_put_nolock(struct ion_handle *handle); 476 + 477 + struct ion_handle *ion_handle_get_by_id(struct ion_client *client, 478 + int id); 479 + 480 + int ion_handle_put(struct ion_handle *handle); 481 + 482 + int ion_query_heaps(struct ion_client *client, struct ion_heap_query *query); 483 + 484 + #endif /* _ION_H */
-1
drivers/staging/android/ion/ion_carveout_heap.c
··· 23 23 #include <linux/slab.h> 24 24 #include <linux/vmalloc.h> 25 25 #include "ion.h" 26 - #include "ion_priv.h" 27 26 28 27 #define ION_CARVEOUT_ALLOCATE_FAIL -1 29 28
-1
drivers/staging/android/ion/ion_chunk_heap.c
··· 22 22 #include <linux/slab.h> 23 23 #include <linux/vmalloc.h> 24 24 #include "ion.h" 25 - #include "ion_priv.h" 26 25 27 26 struct ion_chunk_heap { 28 27 struct ion_heap heap;
-1
drivers/staging/android/ion/ion_cma_heap.c
··· 23 23 #include <linux/scatterlist.h> 24 24 25 25 #include "ion.h" 26 - #include "ion_priv.h" 27 26 28 27 struct ion_cma_heap { 29 28 struct ion_heap heap;
-1
drivers/staging/android/ion/ion_heap.c
··· 24 24 #include <linux/scatterlist.h> 25 25 #include <linux/vmalloc.h> 26 26 #include "ion.h" 27 - #include "ion_priv.h" 28 27 29 28 void *ion_heap_map_kernel(struct ion_heap *heap, 30 29 struct ion_buffer *buffer)
+2 -1
drivers/staging/android/ion/ion_page_pool.c
··· 22 22 #include <linux/init.h> 23 23 #include <linux/slab.h> 24 24 #include <linux/swap.h> 25 - #include "ion_priv.h" 25 + 26 + #include "ion.h" 26 27 27 28 static void *ion_page_pool_alloc_pages(struct ion_page_pool *pool) 28 29 {
-453
drivers/staging/android/ion/ion_priv.h
··· 1 - /* 2 - * drivers/staging/android/ion/ion_priv.h 3 - * 4 - * Copyright (C) 2011 Google, Inc. 5 - * 6 - * This software is licensed under the terms of the GNU General Public 7 - * License version 2, as published by the Free Software Foundation, and 8 - * may be copied, distributed, and modified under those terms. 9 - * 10 - * This program is distributed in the hope that it will be useful, 11 - * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 - * GNU General Public License for more details. 14 - * 15 - */ 16 - 17 - #ifndef _ION_PRIV_H 18 - #define _ION_PRIV_H 19 - 20 - #include <linux/device.h> 21 - #include <linux/dma-direction.h> 22 - #include <linux/kref.h> 23 - #include <linux/mm_types.h> 24 - #include <linux/mutex.h> 25 - #include <linux/rbtree.h> 26 - #include <linux/sched.h> 27 - #include <linux/shrinker.h> 28 - #include <linux/types.h> 29 - #include <linux/miscdevice.h> 30 - 31 - #include "ion.h" 32 - 33 - /** 34 - * struct ion_buffer - metadata for a particular buffer 35 - * @ref: reference count 36 - * @node: node in the ion_device buffers tree 37 - * @dev: back pointer to the ion_device 38 - * @heap: back pointer to the heap the buffer came from 39 - * @flags: buffer specific flags 40 - * @private_flags: internal buffer specific flags 41 - * @size: size of the buffer 42 - * @priv_virt: private data to the buffer representable as 43 - * a void * 44 - * @lock: protects the buffers cnt fields 45 - * @kmap_cnt: number of times the buffer is mapped to the kernel 46 - * @vaddr: the kernel mapping if kmap_cnt is not zero 47 - * @sg_table: the sg table for the buffer if dmap_cnt is not zero 48 - * @pages: flat array of pages in the buffer -- used by fault 49 - * handler and only valid for buffers that are faulted in 50 - * @vmas: list of vma's mapping this buffer 51 - * @handle_count: count of handles referencing this buffer 52 - * @task_comm: taskcomm of last client to reference this buffer in a 53 - * handle, used for debugging 54 - * @pid: pid of last client to reference this buffer in a 55 - * handle, used for debugging 56 - */ 57 - struct ion_buffer { 58 - struct kref ref; 59 - union { 60 - struct rb_node node; 61 - struct list_head list; 62 - }; 63 - struct ion_device *dev; 64 - struct ion_heap *heap; 65 - unsigned long flags; 66 - unsigned long private_flags; 67 - size_t size; 68 - void *priv_virt; 69 - struct mutex lock; 70 - int kmap_cnt; 71 - void *vaddr; 72 - struct sg_table *sg_table; 73 - struct page **pages; 74 - struct list_head vmas; 75 - struct list_head attachments; 76 - /* used to track orphaned buffers */ 77 - int handle_count; 78 - char task_comm[TASK_COMM_LEN]; 79 - pid_t pid; 80 - }; 81 - void ion_buffer_destroy(struct ion_buffer *buffer); 82 - 83 - /** 84 - * struct ion_device - the metadata of the ion device node 85 - * @dev: the actual misc device 86 - * @buffers: an rb tree of all the existing buffers 87 - * @buffer_lock: lock protecting the tree of buffers 88 - * @lock: rwsem protecting the tree of heaps and clients 89 - * @heaps: list of all the heaps in the system 90 - * @user_clients: list of all the clients created from userspace 91 - */ 92 - struct ion_device { 93 - struct miscdevice dev; 94 - struct rb_root buffers; 95 - struct mutex buffer_lock; 96 - struct rw_semaphore lock; 97 - struct plist_head heaps; 98 - struct rb_root clients; 99 - struct dentry *debug_root; 100 - struct dentry *heaps_debug_root; 101 - struct dentry *clients_debug_root; 102 - int heap_cnt; 103 - }; 104 - 105 - /** 106 - * struct ion_client - a process/hw block local address space 107 - * @node: node in the tree of all clients 108 - * @dev: backpointer to ion device 109 - * @handles: an rb tree of all the handles in this client 110 - * @idr: an idr space for allocating handle ids 111 - * @lock: lock protecting the tree of handles 112 - * @name: used for debugging 113 - * @display_name: used for debugging (unique version of @name) 114 - * @display_serial: used for debugging (to make display_name unique) 115 - * @task: used for debugging 116 - * 117 - * A client represents a list of buffers this client may access. 118 - * The mutex stored here is used to protect both handles tree 119 - * as well as the handles themselves, and should be held while modifying either. 120 - */ 121 - struct ion_client { 122 - struct rb_node node; 123 - struct ion_device *dev; 124 - struct rb_root handles; 125 - struct idr idr; 126 - struct mutex lock; 127 - const char *name; 128 - char *display_name; 129 - int display_serial; 130 - struct task_struct *task; 131 - pid_t pid; 132 - struct dentry *debug_root; 133 - }; 134 - 135 - /** 136 - * ion_handle - a client local reference to a buffer 137 - * @ref: reference count 138 - * @client: back pointer to the client the buffer resides in 139 - * @buffer: pointer to the buffer 140 - * @node: node in the client's handle rbtree 141 - * @kmap_cnt: count of times this client has mapped to kernel 142 - * @id: client-unique id allocated by client->idr 143 - * 144 - * Modifications to node, map_cnt or mapping should be protected by the 145 - * lock in the client. Other fields are never changed after initialization. 146 - */ 147 - struct ion_handle { 148 - struct kref ref; 149 - struct ion_client *client; 150 - struct ion_buffer *buffer; 151 - struct rb_node node; 152 - unsigned int kmap_cnt; 153 - int id; 154 - }; 155 - 156 - /** 157 - * struct ion_heap_ops - ops to operate on a given heap 158 - * @allocate: allocate memory 159 - * @free: free memory 160 - * @map_kernel map memory to the kernel 161 - * @unmap_kernel unmap memory to the kernel 162 - * @map_user map memory to userspace 163 - * 164 - * allocate, phys, and map_user return 0 on success, -errno on error. 165 - * map_dma and map_kernel return pointer on success, ERR_PTR on 166 - * error. @free will be called with ION_PRIV_FLAG_SHRINKER_FREE set in 167 - * the buffer's private_flags when called from a shrinker. In that 168 - * case, the pages being free'd must be truly free'd back to the 169 - * system, not put in a page pool or otherwise cached. 170 - */ 171 - struct ion_heap_ops { 172 - int (*allocate)(struct ion_heap *heap, 173 - struct ion_buffer *buffer, unsigned long len, 174 - unsigned long flags); 175 - void (*free)(struct ion_buffer *buffer); 176 - void * (*map_kernel)(struct ion_heap *heap, struct ion_buffer *buffer); 177 - void (*unmap_kernel)(struct ion_heap *heap, struct ion_buffer *buffer); 178 - int (*map_user)(struct ion_heap *mapper, struct ion_buffer *buffer, 179 - struct vm_area_struct *vma); 180 - int (*shrink)(struct ion_heap *heap, gfp_t gfp_mask, int nr_to_scan); 181 - }; 182 - 183 - /** 184 - * heap flags - flags between the heaps and core ion code 185 - */ 186 - #define ION_HEAP_FLAG_DEFER_FREE (1 << 0) 187 - 188 - /** 189 - * private flags - flags internal to ion 190 - */ 191 - /* 192 - * Buffer is being freed from a shrinker function. Skip any possible 193 - * heap-specific caching mechanism (e.g. page pools). Guarantees that 194 - * any buffer storage that came from the system allocator will be 195 - * returned to the system allocator. 196 - */ 197 - #define ION_PRIV_FLAG_SHRINKER_FREE (1 << 0) 198 - 199 - /** 200 - * struct ion_heap - represents a heap in the system 201 - * @node: rb node to put the heap on the device's tree of heaps 202 - * @dev: back pointer to the ion_device 203 - * @type: type of heap 204 - * @ops: ops struct as above 205 - * @flags: flags 206 - * @id: id of heap, also indicates priority of this heap when 207 - * allocating. These are specified by platform data and 208 - * MUST be unique 209 - * @name: used for debugging 210 - * @shrinker: a shrinker for the heap 211 - * @free_list: free list head if deferred free is used 212 - * @free_list_size size of the deferred free list in bytes 213 - * @lock: protects the free list 214 - * @waitqueue: queue to wait on from deferred free thread 215 - * @task: task struct of deferred free thread 216 - * @debug_show: called when heap debug file is read to add any 217 - * heap specific debug info to output 218 - * 219 - * Represents a pool of memory from which buffers can be made. In some 220 - * systems the only heap is regular system memory allocated via vmalloc. 221 - * On others, some blocks might require large physically contiguous buffers 222 - * that are allocated from a specially reserved heap. 223 - */ 224 - struct ion_heap { 225 - struct plist_node node; 226 - struct ion_device *dev; 227 - enum ion_heap_type type; 228 - struct ion_heap_ops *ops; 229 - unsigned long flags; 230 - unsigned int id; 231 - const char *name; 232 - struct shrinker shrinker; 233 - struct list_head free_list; 234 - size_t free_list_size; 235 - spinlock_t free_lock; 236 - wait_queue_head_t waitqueue; 237 - struct task_struct *task; 238 - 239 - int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *); 240 - }; 241 - 242 - /** 243 - * ion_buffer_cached - this ion buffer is cached 244 - * @buffer: buffer 245 - * 246 - * indicates whether this ion buffer is cached 247 - */ 248 - bool ion_buffer_cached(struct ion_buffer *buffer); 249 - 250 - /** 251 - * ion_buffer_fault_user_mappings - fault in user mappings of this buffer 252 - * @buffer: buffer 253 - * 254 - * indicates whether userspace mappings of this buffer will be faulted 255 - * in, this can affect how buffers are allocated from the heap. 256 - */ 257 - bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer); 258 - 259 - /** 260 - * ion_device_create - allocates and returns an ion device 261 - * 262 - * returns a valid device or -PTR_ERR 263 - */ 264 - struct ion_device *ion_device_create(void); 265 - 266 - /** 267 - * ion_device_destroy - free and device and it's resource 268 - * @dev: the device 269 - */ 270 - void ion_device_destroy(struct ion_device *dev); 271 - 272 - /** 273 - * ion_device_add_heap - adds a heap to the ion device 274 - * @dev: the device 275 - * @heap: the heap to add 276 - */ 277 - void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap); 278 - 279 - /** 280 - * some helpers for common operations on buffers using the sg_table 281 - * and vaddr fields 282 - */ 283 - void *ion_heap_map_kernel(struct ion_heap *heap, struct ion_buffer *buffer); 284 - void ion_heap_unmap_kernel(struct ion_heap *heap, struct ion_buffer *buffer); 285 - int ion_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer, 286 - struct vm_area_struct *vma); 287 - int ion_heap_buffer_zero(struct ion_buffer *buffer); 288 - int ion_heap_pages_zero(struct page *page, size_t size, pgprot_t pgprot); 289 - 290 - /** 291 - * ion_heap_init_shrinker 292 - * @heap: the heap 293 - * 294 - * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag or defines the shrink op 295 - * this function will be called to setup a shrinker to shrink the freelists 296 - * and call the heap's shrink op. 297 - */ 298 - void ion_heap_init_shrinker(struct ion_heap *heap); 299 - 300 - /** 301 - * ion_heap_init_deferred_free -- initialize deferred free functionality 302 - * @heap: the heap 303 - * 304 - * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag this function will 305 - * be called to setup deferred frees. Calls to free the buffer will 306 - * return immediately and the actual free will occur some time later 307 - */ 308 - int ion_heap_init_deferred_free(struct ion_heap *heap); 309 - 310 - /** 311 - * ion_heap_freelist_add - add a buffer to the deferred free list 312 - * @heap: the heap 313 - * @buffer: the buffer 314 - * 315 - * Adds an item to the deferred freelist. 316 - */ 317 - void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer); 318 - 319 - /** 320 - * ion_heap_freelist_drain - drain the deferred free list 321 - * @heap: the heap 322 - * @size: amount of memory to drain in bytes 323 - * 324 - * Drains the indicated amount of memory from the deferred freelist immediately. 325 - * Returns the total amount freed. The total freed may be higher depending 326 - * on the size of the items in the list, or lower if there is insufficient 327 - * total memory on the freelist. 328 - */ 329 - size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size); 330 - 331 - /** 332 - * ion_heap_freelist_shrink - drain the deferred free 333 - * list, skipping any heap-specific 334 - * pooling or caching mechanisms 335 - * 336 - * @heap: the heap 337 - * @size: amount of memory to drain in bytes 338 - * 339 - * Drains the indicated amount of memory from the deferred freelist immediately. 340 - * Returns the total amount freed. The total freed may be higher depending 341 - * on the size of the items in the list, or lower if there is insufficient 342 - * total memory on the freelist. 343 - * 344 - * Unlike with @ion_heap_freelist_drain, don't put any pages back into 345 - * page pools or otherwise cache the pages. Everything must be 346 - * genuinely free'd back to the system. If you're free'ing from a 347 - * shrinker you probably want to use this. Note that this relies on 348 - * the heap.ops.free callback honoring the ION_PRIV_FLAG_SHRINKER_FREE 349 - * flag. 350 - */ 351 - size_t ion_heap_freelist_shrink(struct ion_heap *heap, 352 - size_t size); 353 - 354 - /** 355 - * ion_heap_freelist_size - returns the size of the freelist in bytes 356 - * @heap: the heap 357 - */ 358 - size_t ion_heap_freelist_size(struct ion_heap *heap); 359 - 360 - 361 - /** 362 - * functions for creating and destroying the built in ion heaps. 363 - * architectures can add their own custom architecture specific 364 - * heaps as appropriate. 365 - */ 366 - 367 - struct ion_heap *ion_heap_create(struct ion_platform_heap *heap_data); 368 - void ion_heap_destroy(struct ion_heap *heap); 369 - struct ion_heap *ion_system_heap_create(struct ion_platform_heap *unused); 370 - void ion_system_heap_destroy(struct ion_heap *heap); 371 - 372 - struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *heap); 373 - void ion_system_contig_heap_destroy(struct ion_heap *heap); 374 - 375 - struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *heap_data); 376 - void ion_carveout_heap_destroy(struct ion_heap *heap); 377 - 378 - struct ion_heap *ion_chunk_heap_create(struct ion_platform_heap *heap_data); 379 - void ion_chunk_heap_destroy(struct ion_heap *heap); 380 - struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *data); 381 - void ion_cma_heap_destroy(struct ion_heap *heap); 382 - 383 - /** 384 - * functions for creating and destroying a heap pool -- allows you 385 - * to keep a pool of pre allocated memory to use from your heap. Keeping 386 - * a pool of memory that is ready for dma, ie any cached mapping have been 387 - * invalidated from the cache, provides a significant performance benefit on 388 - * many systems 389 - */ 390 - 391 - /** 392 - * struct ion_page_pool - pagepool struct 393 - * @high_count: number of highmem items in the pool 394 - * @low_count: number of lowmem items in the pool 395 - * @high_items: list of highmem items 396 - * @low_items: list of lowmem items 397 - * @mutex: lock protecting this struct and especially the count 398 - * item list 399 - * @gfp_mask: gfp_mask to use from alloc 400 - * @order: order of pages in the pool 401 - * @list: plist node for list of pools 402 - * @cached: it's cached pool or not 403 - * 404 - * Allows you to keep a pool of pre allocated pages to use from your heap. 405 - * Keeping a pool of pages that is ready for dma, ie any cached mapping have 406 - * been invalidated from the cache, provides a significant performance benefit 407 - * on many systems 408 - */ 409 - struct ion_page_pool { 410 - int high_count; 411 - int low_count; 412 - bool cached; 413 - struct list_head high_items; 414 - struct list_head low_items; 415 - struct mutex mutex; 416 - gfp_t gfp_mask; 417 - unsigned int order; 418 - struct plist_node list; 419 - }; 420 - 421 - struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order, 422 - bool cached); 423 - void ion_page_pool_destroy(struct ion_page_pool *pool); 424 - struct page *ion_page_pool_alloc(struct ion_page_pool *pool); 425 - void ion_page_pool_free(struct ion_page_pool *pool, struct page *page); 426 - 427 - /** ion_page_pool_shrink - shrinks the size of the memory cached in the pool 428 - * @pool: the pool 429 - * @gfp_mask: the memory type to reclaim 430 - * @nr_to_scan: number of items to shrink in pages 431 - * 432 - * returns the number of items freed in pages 433 - */ 434 - int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask, 435 - int nr_to_scan); 436 - 437 - long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg); 438 - 439 - struct ion_handle *ion_handle_get_by_id_nolock(struct ion_client *client, 440 - int id); 441 - 442 - void ion_free_nolock(struct ion_client *client, struct ion_handle *handle); 443 - 444 - int ion_handle_put_nolock(struct ion_handle *handle); 445 - 446 - struct ion_handle *ion_handle_get_by_id(struct ion_client *client, 447 - int id); 448 - 449 - int ion_handle_put(struct ion_handle *handle); 450 - 451 - int ion_query_heaps(struct ion_client *client, struct ion_heap_query *query); 452 - 453 - #endif /* _ION_PRIV_H */
-1
drivers/staging/android/ion/ion_system_heap.c
··· 24 24 #include <linux/slab.h> 25 25 #include <linux/vmalloc.h> 26 26 #include "ion.h" 27 - #include "ion_priv.h" 28 27 29 28 #define NUM_ORDERS ARRAY_SIZE(orders) 30 29