2 * drivers/staging/android/ion/ion.h
4 * Copyright (C) 2011 Google, Inc.
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.
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.
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>
31 #include "../uapi/ion.h"
34 * struct ion_platform_heap - defines a heap in the given platform
35 * @type: type of the heap from ion_heap_type enum
36 * @id: unique identifier for heap. When allocating higher numb ers
37 * will be allocated from first. At allocation these are passed
38 * as a bit mask and therefore can not exceed ION_NUM_HEAP_IDS.
39 * @name: used for debug purposes
40 * @base: base address of heap in physical memory if applicable
41 * @size: size of the heap in bytes if applicable
42 * @priv: private info passed from the board file
44 * Provided by the board file.
46 struct ion_platform_heap {
47 enum ion_heap_type type;
57 * struct ion_buffer - metadata for a particular buffer
58 * @ref: reference count
59 * @node: node in the ion_device buffers tree
60 * @dev: back pointer to the ion_device
61 * @heap: back pointer to the heap the buffer came from
62 * @flags: buffer specific flags
63 * @private_flags: internal buffer specific flags
64 * @size: size of the buffer
65 * @priv_virt: private data to the buffer representable as
67 * @lock: protects the buffers cnt fields
68 * @kmap_cnt: number of times the buffer is mapped to the kernel
69 * @vaddr: the kernel mapping if kmap_cnt is not zero
70 * @sg_table: the sg table for the buffer if dmap_cnt is not zero
75 struct list_head list;
77 struct ion_device *dev;
78 struct ion_heap *heap;
80 unsigned long private_flags;
86 struct sg_table *sg_table;
87 struct list_head attachments;
89 void ion_buffer_destroy(struct ion_buffer *buffer);
92 * struct ion_device - the metadata of the ion device node
93 * @dev: the actual misc device
94 * @buffers: an rb tree of all the existing buffers
95 * @buffer_lock: lock protecting the tree of buffers
96 * @lock: rwsem protecting the tree of heaps and clients
99 struct miscdevice dev;
100 struct rb_root buffers;
101 struct mutex buffer_lock;
102 struct rw_semaphore lock;
103 struct plist_head heaps;
104 struct dentry *debug_root;
109 * struct ion_heap_ops - ops to operate on a given heap
110 * @allocate: allocate memory
112 * @map_kernel map memory to the kernel
113 * @unmap_kernel unmap memory to the kernel
114 * @map_user map memory to userspace
116 * allocate, phys, and map_user return 0 on success, -errno on error.
117 * map_dma and map_kernel return pointer on success, ERR_PTR on
118 * error. @free will be called with ION_PRIV_FLAG_SHRINKER_FREE set in
119 * the buffer's private_flags when called from a shrinker. In that
120 * case, the pages being free'd must be truly free'd back to the
121 * system, not put in a page pool or otherwise cached.
123 struct ion_heap_ops {
124 int (*allocate)(struct ion_heap *heap,
125 struct ion_buffer *buffer, unsigned long len,
126 unsigned long flags);
127 void (*free)(struct ion_buffer *buffer);
128 void * (*map_kernel)(struct ion_heap *heap, struct ion_buffer *buffer);
129 void (*unmap_kernel)(struct ion_heap *heap, struct ion_buffer *buffer);
130 int (*map_user)(struct ion_heap *mapper, struct ion_buffer *buffer,
131 struct vm_area_struct *vma);
132 int (*shrink)(struct ion_heap *heap, gfp_t gfp_mask, int nr_to_scan);
136 * heap flags - flags between the heaps and core ion code
138 #define ION_HEAP_FLAG_DEFER_FREE BIT(0)
141 * private flags - flags internal to ion
144 * Buffer is being freed from a shrinker function. Skip any possible
145 * heap-specific caching mechanism (e.g. page pools). Guarantees that
146 * any buffer storage that came from the system allocator will be
147 * returned to the system allocator.
149 #define ION_PRIV_FLAG_SHRINKER_FREE BIT(0)
152 * struct ion_heap - represents a heap in the system
153 * @node: rb node to put the heap on the device's tree of heaps
154 * @dev: back pointer to the ion_device
155 * @type: type of heap
156 * @ops: ops struct as above
158 * @id: id of heap, also indicates priority of this heap when
159 * allocating. These are specified by platform data and
161 * @name: used for debugging
162 * @shrinker: a shrinker for the heap
163 * @free_list: free list head if deferred free is used
164 * @free_list_size size of the deferred free list in bytes
165 * @lock: protects the free list
166 * @waitqueue: queue to wait on from deferred free thread
167 * @task: task struct of deferred free thread
168 * @debug_show: called when heap debug file is read to add any
169 * heap specific debug info to output
171 * Represents a pool of memory from which buffers can be made. In some
172 * systems the only heap is regular system memory allocated via vmalloc.
173 * On others, some blocks might require large physically contiguous buffers
174 * that are allocated from a specially reserved heap.
177 struct plist_node node;
178 struct ion_device *dev;
179 enum ion_heap_type type;
180 struct ion_heap_ops *ops;
184 struct shrinker shrinker;
185 struct list_head free_list;
186 size_t free_list_size;
187 spinlock_t free_lock;
188 wait_queue_head_t waitqueue;
189 struct task_struct *task;
191 int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *);
195 * ion_buffer_cached - this ion buffer is cached
198 * indicates whether this ion buffer is cached
200 bool ion_buffer_cached(struct ion_buffer *buffer);
203 * ion_buffer_fault_user_mappings - fault in user mappings of this buffer
206 * indicates whether userspace mappings of this buffer will be faulted
207 * in, this can affect how buffers are allocated from the heap.
209 bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer);
212 * ion_device_add_heap - adds a heap to the ion device
213 * @heap: the heap to add
215 void ion_device_add_heap(struct ion_heap *heap);
218 * some helpers for common operations on buffers using the sg_table
221 void *ion_heap_map_kernel(struct ion_heap *heap, struct ion_buffer *buffer);
222 void ion_heap_unmap_kernel(struct ion_heap *heap, struct ion_buffer *buffer);
223 int ion_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer,
224 struct vm_area_struct *vma);
225 int ion_heap_buffer_zero(struct ion_buffer *buffer);
226 int ion_heap_pages_zero(struct page *page, size_t size, pgprot_t pgprot);
228 int ion_alloc(size_t len,
229 unsigned int heap_id_mask,
233 * ion_heap_init_shrinker
236 * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag or defines the shrink op
237 * this function will be called to setup a shrinker to shrink the freelists
238 * and call the heap's shrink op.
240 void ion_heap_init_shrinker(struct ion_heap *heap);
243 * ion_heap_init_deferred_free -- initialize deferred free functionality
246 * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag this function will
247 * be called to setup deferred frees. Calls to free the buffer will
248 * return immediately and the actual free will occur some time later
250 int ion_heap_init_deferred_free(struct ion_heap *heap);
253 * ion_heap_freelist_add - add a buffer to the deferred free list
255 * @buffer: the buffer
257 * Adds an item to the deferred freelist.
259 void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer);
262 * ion_heap_freelist_drain - drain the deferred free list
264 * @size: amount of memory to drain in bytes
266 * Drains the indicated amount of memory from the deferred freelist immediately.
267 * Returns the total amount freed. The total freed may be higher depending
268 * on the size of the items in the list, or lower if there is insufficient
269 * total memory on the freelist.
271 size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size);
274 * ion_heap_freelist_shrink - drain the deferred free
275 * list, skipping any heap-specific
276 * pooling or caching mechanisms
279 * @size: amount of memory to drain in bytes
281 * Drains the indicated amount of memory from the deferred freelist immediately.
282 * Returns the total amount freed. The total freed may be higher depending
283 * on the size of the items in the list, or lower if there is insufficient
284 * total memory on the freelist.
286 * Unlike with @ion_heap_freelist_drain, don't put any pages back into
287 * page pools or otherwise cache the pages. Everything must be
288 * genuinely free'd back to the system. If you're free'ing from a
289 * shrinker you probably want to use this. Note that this relies on
290 * the heap.ops.free callback honoring the ION_PRIV_FLAG_SHRINKER_FREE
293 size_t ion_heap_freelist_shrink(struct ion_heap *heap,
297 * ion_heap_freelist_size - returns the size of the freelist in bytes
300 size_t ion_heap_freelist_size(struct ion_heap *heap);
304 * functions for creating and destroying a heap pool -- allows you
305 * to keep a pool of pre allocated memory to use from your heap. Keeping
306 * a pool of memory that is ready for dma, ie any cached mapping have been
307 * invalidated from the cache, provides a significant performance benefit on
312 * struct ion_page_pool - pagepool struct
313 * @high_count: number of highmem items in the pool
314 * @low_count: number of lowmem items in the pool
315 * @high_items: list of highmem items
316 * @low_items: list of lowmem items
317 * @mutex: lock protecting this struct and especially the count
319 * @gfp_mask: gfp_mask to use from alloc
320 * @order: order of pages in the pool
321 * @list: plist node for list of pools
322 * @cached: it's cached pool or not
324 * Allows you to keep a pool of pre allocated pages to use from your heap.
325 * Keeping a pool of pages that is ready for dma, ie any cached mapping have
326 * been invalidated from the cache, provides a significant performance benefit
329 struct ion_page_pool {
333 struct list_head high_items;
334 struct list_head low_items;
338 struct plist_node list;
341 struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order,
343 void ion_page_pool_destroy(struct ion_page_pool *pool);
344 struct page *ion_page_pool_alloc(struct ion_page_pool *pool);
345 void ion_page_pool_free(struct ion_page_pool *pool, struct page *page);
347 /** ion_page_pool_shrink - shrinks the size of the memory cached in the pool
349 * @gfp_mask: the memory type to reclaim
350 * @nr_to_scan: number of items to shrink in pages
352 * returns the number of items freed in pages
354 int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
357 long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
359 int ion_query_heaps(struct ion_heap_query *query);