1 /* SPDX-License-Identifier: GPL-2.0 */
3 * ION Memory Allocator kernel interface header
5 * Copyright (C) 2011 Google, Inc.
11 #include <linux/device.h>
12 #include <linux/dma-direction.h>
13 #include <linux/kref.h>
14 #include <linux/mm_types.h>
15 #include <linux/mutex.h>
16 #include <linux/rbtree.h>
17 #include <linux/sched.h>
18 #include <linux/shrinker.h>
19 #include <linux/types.h>
20 #include <linux/miscdevice.h>
22 #include "../uapi/ion.h"
25 * struct ion_buffer - metadata for a particular buffer
26 * @node: node in the ion_device buffers tree
27 * @list: element in list of deferred freeable buffers
28 * @dev: back pointer to the ion_device
29 * @heap: back pointer to the heap the buffer came from
30 * @flags: buffer specific flags
31 * @private_flags: internal buffer specific flags
32 * @size: size of the buffer
33 * @priv_virt: private data to the buffer representable as
35 * @lock: protects the buffers cnt fields
36 * @kmap_cnt: number of times the buffer is mapped to the kernel
37 * @vaddr: the kernel mapping if kmap_cnt is not zero
38 * @sg_table: the sg table for the buffer
39 * @attachments: list of devices attached to this buffer
44 struct list_head list;
46 struct ion_device *dev;
47 struct ion_heap *heap;
49 unsigned long private_flags;
55 struct sg_table *sg_table;
56 struct list_head attachments;
59 void ion_buffer_destroy(struct ion_buffer *buffer);
62 * struct ion_device - the metadata of the ion device node
63 * @dev: the actual misc device
64 * @buffers: an rb tree of all the existing buffers
65 * @buffer_lock: lock protecting the tree of buffers
66 * @lock: rwsem protecting the tree of heaps and clients
69 struct miscdevice dev;
70 struct rb_root buffers;
71 struct mutex buffer_lock;
72 struct rw_semaphore lock;
73 struct plist_head heaps;
74 struct dentry *debug_root;
79 * struct ion_heap_ops - ops to operate on a given heap
80 * @allocate: allocate memory
82 * @map_kernel map memory to the kernel
83 * @unmap_kernel unmap memory to the kernel
84 * @map_user map memory to userspace
86 * allocate, phys, and map_user return 0 on success, -errno on error.
87 * map_dma and map_kernel return pointer on success, ERR_PTR on
88 * error. @free will be called with ION_PRIV_FLAG_SHRINKER_FREE set in
89 * the buffer's private_flags when called from a shrinker. In that
90 * case, the pages being free'd must be truly free'd back to the
91 * system, not put in a page pool or otherwise cached.
94 int (*allocate)(struct ion_heap *heap,
95 struct ion_buffer *buffer, unsigned long len,
97 void (*free)(struct ion_buffer *buffer);
98 void * (*map_kernel)(struct ion_heap *heap, struct ion_buffer *buffer);
99 void (*unmap_kernel)(struct ion_heap *heap, struct ion_buffer *buffer);
100 int (*map_user)(struct ion_heap *mapper, struct ion_buffer *buffer,
101 struct vm_area_struct *vma);
102 int (*shrink)(struct ion_heap *heap, gfp_t gfp_mask, int nr_to_scan);
106 * heap flags - flags between the heaps and core ion code
108 #define ION_HEAP_FLAG_DEFER_FREE BIT(0)
111 * private flags - flags internal to ion
114 * Buffer is being freed from a shrinker function. Skip any possible
115 * heap-specific caching mechanism (e.g. page pools). Guarantees that
116 * any buffer storage that came from the system allocator will be
117 * returned to the system allocator.
119 #define ION_PRIV_FLAG_SHRINKER_FREE BIT(0)
122 * struct ion_heap - represents a heap in the system
123 * @node: rb node to put the heap on the device's tree of heaps
124 * @dev: back pointer to the ion_device
125 * @type: type of heap
126 * @ops: ops struct as above
128 * @id: id of heap, also indicates priority of this heap when
129 * allocating. These are specified by platform data and
131 * @name: used for debugging
132 * @shrinker: a shrinker for the heap
133 * @free_list: free list head if deferred free is used
134 * @free_list_size size of the deferred free list in bytes
135 * @lock: protects the free list
136 * @waitqueue: queue to wait on from deferred free thread
137 * @task: task struct of deferred free thread
138 * @num_of_buffers the number of currently allocated buffers
139 * @num_of_alloc_bytes the number of allocated bytes
140 * @alloc_bytes_wm the number of allocated bytes watermark
142 * Represents a pool of memory from which buffers can be made. In some
143 * systems the only heap is regular system memory allocated via vmalloc.
144 * On others, some blocks might require large physically contiguous buffers
145 * that are allocated from a specially reserved heap.
148 struct plist_node node;
149 struct ion_device *dev;
150 enum ion_heap_type type;
151 struct ion_heap_ops *ops;
156 /* deferred free support */
157 struct shrinker shrinker;
158 struct list_head free_list;
159 size_t free_list_size;
160 spinlock_t free_lock;
161 wait_queue_head_t waitqueue;
162 struct task_struct *task;
164 /* heap statistics */
166 u64 num_of_alloc_bytes;
169 /* protect heap statistics */
170 spinlock_t stat_lock;
174 * ion_device_add_heap - adds a heap to the ion device
175 * @heap: the heap to add
177 void ion_device_add_heap(struct ion_heap *heap);
180 * some helpers for common operations on buffers using the sg_table
183 void *ion_heap_map_kernel(struct ion_heap *heap, struct ion_buffer *buffer);
184 void ion_heap_unmap_kernel(struct ion_heap *heap, struct ion_buffer *buffer);
185 int ion_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer,
186 struct vm_area_struct *vma);
187 int ion_heap_buffer_zero(struct ion_buffer *buffer);
188 int ion_heap_pages_zero(struct page *page, size_t size, pgprot_t pgprot);
191 * ion_heap_init_shrinker
194 * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag or defines the shrink op
195 * this function will be called to setup a shrinker to shrink the freelists
196 * and call the heap's shrink op.
198 int ion_heap_init_shrinker(struct ion_heap *heap);
201 * ion_heap_init_deferred_free -- initialize deferred free functionality
204 * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag this function will
205 * be called to setup deferred frees. Calls to free the buffer will
206 * return immediately and the actual free will occur some time later
208 int ion_heap_init_deferred_free(struct ion_heap *heap);
211 * ion_heap_freelist_add - add a buffer to the deferred free list
213 * @buffer: the buffer
215 * Adds an item to the deferred freelist.
217 void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer);
220 * ion_heap_freelist_drain - drain the deferred free list
222 * @size: amount of memory to drain in bytes
224 * Drains the indicated amount of memory from the deferred freelist immediately.
225 * Returns the total amount freed. The total freed may be higher depending
226 * on the size of the items in the list, or lower if there is insufficient
227 * total memory on the freelist.
229 size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size);
232 * ion_heap_freelist_shrink - drain the deferred free
233 * list, skipping any heap-specific
234 * pooling or caching mechanisms
237 * @size: amount of memory to drain in bytes
239 * Drains the indicated amount of memory from the deferred freelist immediately.
240 * Returns the total amount freed. The total freed may be higher depending
241 * on the size of the items in the list, or lower if there is insufficient
242 * total memory on the freelist.
244 * Unlike with @ion_heap_freelist_drain, don't put any pages back into
245 * page pools or otherwise cache the pages. Everything must be
246 * genuinely free'd back to the system. If you're free'ing from a
247 * shrinker you probably want to use this. Note that this relies on
248 * the heap.ops.free callback honoring the ION_PRIV_FLAG_SHRINKER_FREE
251 size_t ion_heap_freelist_shrink(struct ion_heap *heap,
255 * ion_heap_freelist_size - returns the size of the freelist in bytes
258 size_t ion_heap_freelist_size(struct ion_heap *heap);
261 * functions for creating and destroying a heap pool -- allows you
262 * to keep a pool of pre allocated memory to use from your heap. Keeping
263 * a pool of memory that is ready for dma, ie any cached mapping have been
264 * invalidated from the cache, provides a significant performance benefit on
269 * struct ion_page_pool - pagepool struct
270 * @high_count: number of highmem items in the pool
271 * @low_count: number of lowmem items in the pool
272 * @high_items: list of highmem items
273 * @low_items: list of lowmem items
274 * @mutex: lock protecting this struct and especially the count
276 * @gfp_mask: gfp_mask to use from alloc
277 * @order: order of pages in the pool
278 * @list: plist node for list of pools
280 * Allows you to keep a pool of pre allocated pages to use from your heap.
281 * Keeping a pool of pages that is ready for dma, ie any cached mapping have
282 * been invalidated from the cache, provides a significant performance benefit
285 struct ion_page_pool {
288 struct list_head high_items;
289 struct list_head low_items;
293 struct plist_node list;
296 struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order);
297 void ion_page_pool_destroy(struct ion_page_pool *pool);
298 struct page *ion_page_pool_alloc(struct ion_page_pool *pool);
299 void ion_page_pool_free(struct ion_page_pool *pool, struct page *page);
301 /** ion_page_pool_shrink - shrinks the size of the memory cached in the pool
303 * @gfp_mask: the memory type to reclaim
304 * @nr_to_scan: number of items to shrink in pages
306 * returns the number of items freed in pages
308 int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,