2 * drivers/staging/android/ion/ion_priv.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>
32 struct ion_buffer *ion_handle_buffer(struct ion_handle *handle);
35 * struct ion_buffer - metadata for a particular buffer
36 * @ref: reference count
37 * @node: node in the ion_device buffers tree
38 * @dev: back pointer to the ion_device
39 * @heap: back pointer to the heap the buffer came from
40 * @flags: buffer specific flags
41 * @private_flags: internal buffer specific flags
42 * @size: size of the buffer
43 * @priv_virt: private data to the buffer representable as
45 * @lock: protects the buffers cnt fields
46 * @kmap_cnt: number of times the buffer is mapped to the kernel
47 * @vaddr: the kernel mapping if kmap_cnt is not zero
48 * @dmap_cnt: number of times the buffer is mapped for dma
49 * @sg_table: the sg table for the buffer if dmap_cnt is not zero
50 * @pages: flat array of pages in the buffer -- used by fault
51 * handler and only valid for buffers that are faulted in
52 * @vmas: list of vma's mapping this buffer
53 * @handle_count: count of handles referencing this buffer
54 * @task_comm: taskcomm of last client to reference this buffer in a
55 * handle, used for debugging
56 * @pid: pid of last client to reference this buffer in a
57 * handle, used for debugging
63 struct list_head list;
65 struct ion_device *dev;
66 struct ion_heap *heap;
68 unsigned long private_flags;
75 struct sg_table *sg_table;
77 struct list_head vmas;
78 /* used to track orphaned buffers */
80 char task_comm[TASK_COMM_LEN];
83 void ion_buffer_destroy(struct ion_buffer *buffer);
86 * struct ion_heap_ops - ops to operate on a given heap
87 * @allocate: allocate memory
89 * @map_kernel map memory to the kernel
90 * @unmap_kernel unmap memory to the kernel
91 * @map_user map memory to userspace
93 * allocate, phys, and map_user return 0 on success, -errno on error.
94 * map_dma and map_kernel return pointer on success, ERR_PTR on
95 * error. @free will be called with ION_PRIV_FLAG_SHRINKER_FREE set in
96 * the buffer's private_flags when called from a shrinker. In that
97 * case, the pages being free'd must be truly free'd back to the
98 * system, not put in a page pool or otherwise cached.
100 struct ion_heap_ops {
101 int (*allocate)(struct ion_heap *heap,
102 struct ion_buffer *buffer, unsigned long len,
103 unsigned long align, unsigned long flags);
104 void (*free)(struct ion_buffer *buffer);
105 void * (*map_kernel)(struct ion_heap *heap, struct ion_buffer *buffer);
106 void (*unmap_kernel)(struct ion_heap *heap, struct ion_buffer *buffer);
107 int (*map_user)(struct ion_heap *mapper, struct ion_buffer *buffer,
108 struct vm_area_struct *vma);
109 int (*shrink)(struct ion_heap *heap, gfp_t gfp_mask, int nr_to_scan);
113 * heap flags - flags between the heaps and core ion code
115 #define ION_HEAP_FLAG_DEFER_FREE (1 << 0)
118 * private flags - flags internal to ion
121 * Buffer is being freed from a shrinker function. Skip any possible
122 * heap-specific caching mechanism (e.g. page pools). Guarantees that
123 * any buffer storage that came from the system allocator will be
124 * returned to the system allocator.
126 #define ION_PRIV_FLAG_SHRINKER_FREE (1 << 0)
129 * struct ion_heap - represents a heap in the system
130 * @node: rb node to put the heap on the device's tree of heaps
131 * @dev: back pointer to the ion_device
132 * @type: type of heap
133 * @ops: ops struct as above
135 * @id: id of heap, also indicates priority of this heap when
136 * allocating. These are specified by platform data and
138 * @name: used for debugging
139 * @shrinker: a shrinker for the heap
140 * @free_list: free list head if deferred free is used
141 * @free_list_size size of the deferred free list in bytes
142 * @lock: protects the free list
143 * @waitqueue: queue to wait on from deferred free thread
144 * @task: task struct of deferred free thread
145 * @debug_show: called when heap debug file is read to add any
146 * heap specific debug info to output
148 * Represents a pool of memory from which buffers can be made. In some
149 * systems the only heap is regular system memory allocated via vmalloc.
150 * On others, some blocks might require large physically contiguous buffers
151 * that are allocated from a specially reserved heap.
154 struct plist_node node;
155 struct ion_device *dev;
156 enum ion_heap_type type;
157 struct ion_heap_ops *ops;
161 struct shrinker shrinker;
162 struct list_head free_list;
163 size_t free_list_size;
164 spinlock_t free_lock;
165 wait_queue_head_t waitqueue;
166 struct task_struct *task;
168 int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *);
172 * ion_buffer_cached - this ion buffer is cached
175 * indicates whether this ion buffer is cached
177 bool ion_buffer_cached(struct ion_buffer *buffer);
180 * ion_buffer_fault_user_mappings - fault in user mappings of this buffer
183 * indicates whether userspace mappings of this buffer will be faulted
184 * in, this can affect how buffers are allocated from the heap.
186 bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer);
189 * ion_device_create - allocates and returns an ion device
190 * @custom_ioctl: arch specific ioctl function if applicable
192 * returns a valid device or -PTR_ERR
194 struct ion_device *ion_device_create(long (*custom_ioctl)
195 (struct ion_client *client,
200 * ion_device_destroy - free and device and it's resource
203 void ion_device_destroy(struct ion_device *dev);
206 * ion_device_add_heap - adds a heap to the ion device
208 * @heap: the heap to add
210 void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap);
213 * some helpers for common operations on buffers using the sg_table
216 void *ion_heap_map_kernel(struct ion_heap *, struct ion_buffer *);
217 void ion_heap_unmap_kernel(struct ion_heap *, struct ion_buffer *);
218 int ion_heap_map_user(struct ion_heap *, struct ion_buffer *,
219 struct vm_area_struct *);
220 int ion_heap_buffer_zero(struct ion_buffer *buffer);
221 int ion_heap_pages_zero(struct page *page, size_t size, pgprot_t pgprot);
224 * ion_heap_init_shrinker
227 * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag or defines the shrink op
228 * this function will be called to setup a shrinker to shrink the freelists
229 * and call the heap's shrink op.
231 void ion_heap_init_shrinker(struct ion_heap *heap);
234 * ion_heap_init_deferred_free -- initialize deferred free functionality
237 * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag this function will
238 * be called to setup deferred frees. Calls to free the buffer will
239 * return immediately and the actual free will occur some time later
241 int ion_heap_init_deferred_free(struct ion_heap *heap);
244 * ion_heap_freelist_add - add a buffer to the deferred free list
246 * @buffer: the buffer
248 * Adds an item to the deferred freelist.
250 void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer);
253 * ion_heap_freelist_drain - drain the deferred free list
255 * @size: amount of memory to drain in bytes
257 * Drains the indicated amount of memory from the deferred freelist immediately.
258 * Returns the total amount freed. The total freed may be higher depending
259 * on the size of the items in the list, or lower if there is insufficient
260 * total memory on the freelist.
262 size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size);
265 * ion_heap_freelist_shrink - drain the deferred free
266 * list, skipping any heap-specific
267 * pooling or caching mechanisms
270 * @size: amount of memory to drain in bytes
272 * Drains the indicated amount of memory from the deferred freelist immediately.
273 * Returns the total amount freed. The total freed may be higher depending
274 * on the size of the items in the list, or lower if there is insufficient
275 * total memory on the freelist.
277 * Unlike with @ion_heap_freelist_drain, don't put any pages back into
278 * page pools or otherwise cache the pages. Everything must be
279 * genuinely free'd back to the system. If you're free'ing from a
280 * shrinker you probably want to use this. Note that this relies on
281 * the heap.ops.free callback honoring the ION_PRIV_FLAG_SHRINKER_FREE
284 size_t ion_heap_freelist_shrink(struct ion_heap *heap,
288 * ion_heap_freelist_size - returns the size of the freelist in bytes
291 size_t ion_heap_freelist_size(struct ion_heap *heap);
295 * functions for creating and destroying the built in ion heaps.
296 * architectures can add their own custom architecture specific
297 * heaps as appropriate.
300 struct ion_heap *ion_heap_create(struct ion_platform_heap *);
301 void ion_heap_destroy(struct ion_heap *);
302 struct ion_heap *ion_system_heap_create(struct ion_platform_heap *);
303 void ion_system_heap_destroy(struct ion_heap *);
305 struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *);
306 void ion_system_contig_heap_destroy(struct ion_heap *);
308 struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *);
309 void ion_carveout_heap_destroy(struct ion_heap *);
311 struct ion_heap *ion_chunk_heap_create(struct ion_platform_heap *);
312 void ion_chunk_heap_destroy(struct ion_heap *);
313 struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *);
314 void ion_cma_heap_destroy(struct ion_heap *);
317 * kernel api to allocate/free from carveout -- used when carveout is
318 * used to back an architecture specific custom heap
320 ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size,
321 unsigned long align);
322 void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr,
325 * The carveout heap returns physical addresses, since 0 may be a valid
326 * physical address, this is used to indicate allocation failed
328 #define ION_CARVEOUT_ALLOCATE_FAIL -1
331 * functions for creating and destroying a heap pool -- allows you
332 * to keep a pool of pre allocated memory to use from your heap. Keeping
333 * a pool of memory that is ready for dma, ie any cached mapping have been
334 * invalidated from the cache, provides a significant performance benefit on
339 * struct ion_page_pool - pagepool struct
340 * @high_count: number of highmem items in the pool
341 * @low_count: number of lowmem items in the pool
342 * @high_items: list of highmem items
343 * @low_items: list of lowmem items
344 * @mutex: lock protecting this struct and especially the count
346 * @gfp_mask: gfp_mask to use from alloc
347 * @order: order of pages in the pool
348 * @list: plist node for list of pools
350 * Allows you to keep a pool of pre allocated pages to use from your heap.
351 * Keeping a pool of pages that is ready for dma, ie any cached mapping have
352 * been invalidated from the cache, provides a significant performance benefit
355 struct ion_page_pool {
358 struct list_head high_items;
359 struct list_head low_items;
363 struct plist_node list;
366 struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order);
367 void ion_page_pool_destroy(struct ion_page_pool *);
368 struct page *ion_page_pool_alloc(struct ion_page_pool *);
369 void ion_page_pool_free(struct ion_page_pool *, struct page *);
371 /** ion_page_pool_shrink - shrinks the size of the memory cached in the pool
373 * @gfp_mask: the memory type to reclaim
374 * @nr_to_scan: number of items to shrink in pages
376 * returns the number of items freed in pages
378 int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
382 * ion_pages_sync_for_device - cache flush pages for use with the specified
384 * @dev: the device the pages will be used with
385 * @page: the first page to be flushed
386 * @size: size in bytes of region to be flushed
387 * @dir: direction of dma transfer
389 void ion_pages_sync_for_device(struct device *dev, struct page *page,
390 size_t size, enum dma_data_direction dir);
392 #endif /* _ION_PRIV_H */