ion_priv.h revision 9e907654d3c0ff58fb27b51d94128a9749c7e85c
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/kref.h> 21#include <linux/mm_types.h> 22#include <linux/mutex.h> 23#include <linux/rbtree.h> 24#include <linux/sched.h> 25#include <linux/shrinker.h> 26#include <linux/types.h> 27 28#include "ion.h" 29 30struct ion_buffer *ion_handle_buffer(struct ion_handle *handle); 31 32/** 33 * struct ion_buffer - metadata for a particular buffer 34 * @ref: refernce count 35 * @node: node in the ion_device buffers tree 36 * @dev: back pointer to the ion_device 37 * @heap: back pointer to the heap the buffer came from 38 * @flags: buffer specific flags 39 * @size: size of the buffer 40 * @priv_virt: private data to the buffer representable as 41 * a void * 42 * @priv_phys: private data to the buffer representable as 43 * an ion_phys_addr_t (and someday a phys_addr_t) 44 * @lock: protects the buffers cnt fields 45 * @kmap_cnt: number of times the buffer is mapped to the kernel 46 * @vaddr: the kenrel mapping if kmap_cnt is not zero 47 * @dmap_cnt: number of times the buffer is mapped for dma 48 * @sg_table: the sg table for the buffer if dmap_cnt is not zero 49 * @pages: flat array of pages in the buffer -- used by fault 50 * handler and only valid for buffers that are faulted in 51 * @vmas: list of vma's mapping this buffer 52 * @handle_count: count of handles referencing this buffer 53 * @task_comm: taskcomm of last client to reference this buffer in a 54 * handle, used for debugging 55 * @pid: pid of last client to reference this buffer in a 56 * handle, used for debugging 57*/ 58struct ion_buffer { 59 struct kref ref; 60 union { 61 struct rb_node node; 62 struct list_head list; 63 }; 64 struct ion_device *dev; 65 struct ion_heap *heap; 66 unsigned long flags; 67 size_t size; 68 union { 69 void *priv_virt; 70 ion_phys_addr_t priv_phys; 71 }; 72 struct mutex lock; 73 int kmap_cnt; 74 void *vaddr; 75 int dmap_cnt; 76 struct sg_table *sg_table; 77 struct page **pages; 78 struct list_head vmas; 79 /* used to track orphaned buffers */ 80 int handle_count; 81 char task_comm[TASK_COMM_LEN]; 82 pid_t pid; 83}; 84void ion_buffer_destroy(struct ion_buffer *buffer); 85 86/** 87 * struct ion_heap_ops - ops to operate on a given heap 88 * @allocate: allocate memory 89 * @free: free memory 90 * @phys get physical address of a buffer (only define on 91 * physically contiguous heaps) 92 * @map_dma map the memory for dma to a scatterlist 93 * @unmap_dma unmap the memory for dma 94 * @map_kernel map memory to the kernel 95 * @unmap_kernel unmap memory to the kernel 96 * @map_user map memory to userspace 97 * 98 * allocate, phys, and map_user return 0 on success, -errno on error. 99 * map_dma and map_kernel return pointer on success, ERR_PTR on error. 100 */ 101struct ion_heap_ops { 102 int (*allocate) (struct ion_heap *heap, 103 struct ion_buffer *buffer, unsigned long len, 104 unsigned long align, unsigned long flags); 105 void (*free) (struct ion_buffer *buffer); 106 int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer, 107 ion_phys_addr_t *addr, size_t *len); 108 struct sg_table *(*map_dma) (struct ion_heap *heap, 109 struct ion_buffer *buffer); 110 void (*unmap_dma) (struct ion_heap *heap, struct ion_buffer *buffer); 111 void * (*map_kernel) (struct ion_heap *heap, struct ion_buffer *buffer); 112 void (*unmap_kernel) (struct ion_heap *heap, struct ion_buffer *buffer); 113 int (*map_user) (struct ion_heap *mapper, struct ion_buffer *buffer, 114 struct vm_area_struct *vma); 115}; 116 117/** 118 * heap flags - flags between the heaps and core ion code 119 */ 120#define ION_HEAP_FLAG_DEFER_FREE (1 << 0) 121 122/** 123 * struct ion_heap - represents a heap in the system 124 * @node: rb node to put the heap on the device's tree of heaps 125 * @dev: back pointer to the ion_device 126 * @type: type of heap 127 * @ops: ops struct as above 128 * @flags: flags 129 * @id: id of heap, also indicates priority of this heap when 130 * allocating. These are specified by platform data and 131 * MUST be unique 132 * @name: used for debugging 133 * @shrinker: a shrinker for the heap, if the heap caches system 134 * memory, it must define a shrinker to return it on low 135 * memory conditions, this includes system memory cached 136 * in the deferred free lists for heaps that support it 137 * @free_list: free list head if deferred free is used 138 * @free_list_size size of the deferred free list in bytes 139 * @lock: protects the free list 140 * @waitqueue: queue to wait on from deferred free thread 141 * @task: task struct of deferred free thread 142 * @debug_show: called when heap debug file is read to add any 143 * heap specific debug info to output 144 * 145 * Represents a pool of memory from which buffers can be made. In some 146 * systems the only heap is regular system memory allocated via vmalloc. 147 * On others, some blocks might require large physically contiguous buffers 148 * that are allocated from a specially reserved heap. 149 */ 150struct ion_heap { 151 struct plist_node node; 152 struct ion_device *dev; 153 enum ion_heap_type type; 154 struct ion_heap_ops *ops; 155 unsigned long flags; 156 unsigned int id; 157 const char *name; 158 struct shrinker shrinker; 159 struct list_head free_list; 160 size_t free_list_size; 161 struct rt_mutex lock; 162 wait_queue_head_t waitqueue; 163 struct task_struct *task; 164 int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *); 165}; 166 167/** 168 * ion_buffer_cached - this ion buffer is cached 169 * @buffer: buffer 170 * 171 * indicates whether this ion buffer is cached 172 */ 173bool ion_buffer_cached(struct ion_buffer *buffer); 174 175/** 176 * ion_buffer_fault_user_mappings - fault in user mappings of this buffer 177 * @buffer: buffer 178 * 179 * indicates whether userspace mappings of this buffer will be faulted 180 * in, this can affect how buffers are allocated from the heap. 181 */ 182bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer); 183 184/** 185 * ion_device_create - allocates and returns an ion device 186 * @custom_ioctl: arch specific ioctl function if applicable 187 * 188 * returns a valid device or -PTR_ERR 189 */ 190struct ion_device *ion_device_create(long (*custom_ioctl) 191 (struct ion_client *client, 192 unsigned int cmd, 193 unsigned long arg)); 194 195/** 196 * ion_device_destroy - free and device and it's resource 197 * @dev: the device 198 */ 199void ion_device_destroy(struct ion_device *dev); 200 201/** 202 * ion_device_add_heap - adds a heap to the ion device 203 * @dev: the device 204 * @heap: the heap to add 205 */ 206void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap); 207 208/** 209 * some helpers for common operations on buffers using the sg_table 210 * and vaddr fields 211 */ 212void *ion_heap_map_kernel(struct ion_heap *, struct ion_buffer *); 213void ion_heap_unmap_kernel(struct ion_heap *, struct ion_buffer *); 214int ion_heap_map_user(struct ion_heap *, struct ion_buffer *, 215 struct vm_area_struct *); 216int ion_heap_buffer_zero(struct ion_buffer *buffer); 217 218/** 219 * ion_heap_alloc_pages - allocate pages from alloc_pages 220 * @buffer: the buffer to allocate for, used to extract the flags 221 * @gfp_flags: the gfp_t for the allocation 222 * @order: the order of the allocatoin 223 * 224 * This funciton allocations from alloc pages and also does any other 225 * necessary operations based on the buffer->flags. For buffers which 226 * will be faulted in the pages are split using split_page 227 */ 228struct page *ion_heap_alloc_pages(struct ion_buffer *buffer, gfp_t gfp_flags, 229 unsigned int order); 230 231/** 232 * ion_heap_init_deferred_free -- initialize deferred free functionality 233 * @heap: the heap 234 * 235 * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag this function will 236 * be called to setup deferred frees. Calls to free the buffer will 237 * return immediately and the actual free will occur some time later 238 */ 239int ion_heap_init_deferred_free(struct ion_heap *heap); 240 241/** 242 * ion_heap_freelist_add - add a buffer to the deferred free list 243 * @heap: the heap 244 * @buffer: the buffer 245 * 246 * Adds an item to the deferred freelist. 247 */ 248void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer); 249 250/** 251 * ion_heap_freelist_drain - drain the deferred free list 252 * @heap: the heap 253 * @size: ammount of memory to drain in bytes 254 * 255 * Drains the indicated amount of memory from the deferred freelist immediately. 256 * Returns the total amount freed. The total freed may be higher depending 257 * on the size of the items in the list, or lower if there is insufficient 258 * total memory on the freelist. 259 */ 260size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size); 261 262/** 263 * ion_heap_freelist_size - returns the size of the freelist in bytes 264 * @heap: the heap 265 */ 266size_t ion_heap_freelist_size(struct ion_heap *heap); 267 268 269/** 270 * functions for creating and destroying the built in ion heaps. 271 * architectures can add their own custom architecture specific 272 * heaps as appropriate. 273 */ 274 275struct ion_heap *ion_heap_create(struct ion_platform_heap *); 276void ion_heap_destroy(struct ion_heap *); 277struct ion_heap *ion_system_heap_create(struct ion_platform_heap *); 278void ion_system_heap_destroy(struct ion_heap *); 279 280struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *); 281void ion_system_contig_heap_destroy(struct ion_heap *); 282 283struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *); 284void ion_carveout_heap_destroy(struct ion_heap *); 285 286struct ion_heap *ion_chunk_heap_create(struct ion_platform_heap *); 287void ion_chunk_heap_destroy(struct ion_heap *); 288struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *); 289void ion_cma_heap_destroy(struct ion_heap *); 290 291/** 292 * kernel api to allocate/free from carveout -- used when carveout is 293 * used to back an architecture specific custom heap 294 */ 295ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size, 296 unsigned long align); 297void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr, 298 unsigned long size); 299/** 300 * The carveout heap returns physical addresses, since 0 may be a valid 301 * physical address, this is used to indicate allocation failed 302 */ 303#define ION_CARVEOUT_ALLOCATE_FAIL -1 304 305/** 306 * functions for creating and destroying a heap pool -- allows you 307 * to keep a pool of pre allocated memory to use from your heap. Keeping 308 * a pool of memory that is ready for dma, ie any cached mapping have been 309 * invalidated from the cache, provides a significant peformance benefit on 310 * many systems */ 311 312/** 313 * struct ion_page_pool - pagepool struct 314 * @high_count: number of highmem items in the pool 315 * @low_count: number of lowmem items in the pool 316 * @high_items: list of highmem items 317 * @low_items: list of lowmem items 318 * @shrinker: a shrinker for the items 319 * @mutex: lock protecting this struct and especially the count 320 * item list 321 * @alloc: function to be used to allocate pageory when the pool 322 * is empty 323 * @free: function to be used to free pageory back to the system 324 * when the shrinker fires 325 * @gfp_mask: gfp_mask to use from alloc 326 * @order: order of pages in the pool 327 * @list: plist node for list of pools 328 * 329 * Allows you to keep a pool of pre allocated pages to use from your heap. 330 * Keeping a pool of pages that is ready for dma, ie any cached mapping have 331 * been invalidated from the cache, provides a significant peformance benefit 332 * on many systems 333 */ 334struct ion_page_pool { 335 int high_count; 336 int low_count; 337 struct list_head high_items; 338 struct list_head low_items; 339 struct mutex mutex; 340 gfp_t gfp_mask; 341 unsigned int order; 342 struct plist_node list; 343}; 344 345struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order); 346void ion_page_pool_destroy(struct ion_page_pool *); 347void *ion_page_pool_alloc(struct ion_page_pool *); 348void ion_page_pool_free(struct ion_page_pool *, struct page *); 349 350/** ion_page_pool_shrink - shrinks the size of the memory cached in the pool 351 * @pool: the pool 352 * @gfp_mask: the memory type to reclaim 353 * @nr_to_scan: number of items to shrink in pages 354 * 355 * returns the number of items freed in pages 356 */ 357int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask, 358 int nr_to_scan); 359 360#endif /* _ION_PRIV_H */ 361