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 * @list: element in list of deferred freeable buffers
27 * @dev: back pointer to the ion_device
28 * @heap: back pointer to the heap the buffer came from
29 * @flags: buffer specific flags
30 * @private_flags: internal buffer specific flags
31 * @size: size of the buffer
32 * @priv_virt: private data to the buffer representable as
34 * @lock: protects the buffers cnt fields
35 * @kmap_cnt: number of times the buffer is mapped to the kernel
36 * @vaddr: the kernel mapping if kmap_cnt is not zero
37 * @sg_table: the sg table for the buffer
38 * @attachments: list of devices attached to this buffer
41 struct list_head list;
42 struct ion_device *dev;
43 struct ion_heap *heap;
45 unsigned long private_flags;
51 struct sg_table *sg_table;
52 struct list_head attachments;
55 void ion_buffer_destroy(struct ion_buffer *buffer);
58 * struct ion_device - the metadata of the ion device node
59 * @dev: the actual misc device
60 * @lock: rwsem protecting the tree of heaps and clients
63 struct miscdevice dev;
64 struct rw_semaphore lock;
65 struct plist_head heaps;
66 struct dentry *debug_root;
71 * struct ion_heap_ops - ops to operate on a given heap
72 * @allocate: allocate memory
74 * @map_kernel map memory to the kernel
75 * @unmap_kernel unmap memory to the kernel
76 * @map_user map memory to userspace
78 * allocate, phys, and map_user return 0 on success, -errno on error.
79 * map_dma and map_kernel return pointer on success, ERR_PTR on
80 * error. @free will be called with ION_PRIV_FLAG_SHRINKER_FREE set in
81 * the buffer's private_flags when called from a shrinker. In that
82 * case, the pages being free'd must be truly free'd back to the
83 * system, not put in a page pool or otherwise cached.
86 int (*allocate)(struct ion_heap *heap,
87 struct ion_buffer *buffer, unsigned long len,
89 void (*free)(struct ion_buffer *buffer);
90 void * (*map_kernel)(struct ion_heap *heap, struct ion_buffer *buffer);
91 void (*unmap_kernel)(struct ion_heap *heap, struct ion_buffer *buffer);
92 int (*map_user)(struct ion_heap *mapper, struct ion_buffer *buffer,
93 struct vm_area_struct *vma);
94 int (*shrink)(struct ion_heap *heap, gfp_t gfp_mask, int nr_to_scan);
98 * heap flags - flags between the heaps and core ion code
100 #define ION_HEAP_FLAG_DEFER_FREE BIT(0)
103 * private flags - flags internal to ion
106 * Buffer is being freed from a shrinker function. Skip any possible
107 * heap-specific caching mechanism (e.g. page pools). Guarantees that
108 * any buffer storage that came from the system allocator will be
109 * returned to the system allocator.
111 #define ION_PRIV_FLAG_SHRINKER_FREE BIT(0)
114 * struct ion_heap - represents a heap in the system
115 * @node: rb node to put the heap on the device's tree of heaps
116 * @dev: back pointer to the ion_device
117 * @type: type of heap
118 * @ops: ops struct as above
120 * @id: id of heap, also indicates priority of this heap when
121 * allocating. These are specified by platform data and
123 * @name: used for debugging
124 * @shrinker: a shrinker for the heap
125 * @free_list: free list head if deferred free is used
126 * @free_list_size size of the deferred free list in bytes
127 * @lock: protects the free list
128 * @waitqueue: queue to wait on from deferred free thread
129 * @task: task struct of deferred free thread
130 * @num_of_buffers the number of currently allocated buffers
131 * @num_of_alloc_bytes the number of allocated bytes
132 * @alloc_bytes_wm the number of allocated bytes watermark
134 * Represents a pool of memory from which buffers can be made. In some
135 * systems the only heap is regular system memory allocated via vmalloc.
136 * On others, some blocks might require large physically contiguous buffers
137 * that are allocated from a specially reserved heap.
140 struct plist_node node;
141 struct ion_device *dev;
142 enum ion_heap_type type;
143 struct ion_heap_ops *ops;
148 /* deferred free support */
149 struct shrinker shrinker;
150 struct list_head free_list;
151 size_t free_list_size;
152 spinlock_t free_lock;
153 wait_queue_head_t waitqueue;
154 struct task_struct *task;
156 /* heap statistics */
158 u64 num_of_alloc_bytes;
161 /* protect heap statistics */
162 spinlock_t stat_lock;
166 * ion_device_add_heap - adds a heap to the ion device
167 * @heap: the heap to add
169 void ion_device_add_heap(struct ion_heap *heap);
172 * some helpers for common operations on buffers using the sg_table
175 void *ion_heap_map_kernel(struct ion_heap *heap, struct ion_buffer *buffer);
176 void ion_heap_unmap_kernel(struct ion_heap *heap, struct ion_buffer *buffer);
177 int ion_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer,
178 struct vm_area_struct *vma);
179 int ion_heap_buffer_zero(struct ion_buffer *buffer);
180 int ion_heap_pages_zero(struct page *page, size_t size, pgprot_t pgprot);
183 * ion_heap_init_shrinker
186 * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag or defines the shrink op
187 * this function will be called to setup a shrinker to shrink the freelists
188 * and call the heap's shrink op.
190 int ion_heap_init_shrinker(struct ion_heap *heap);
193 * ion_heap_init_deferred_free -- initialize deferred free functionality
196 * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag this function will
197 * be called to setup deferred frees. Calls to free the buffer will
198 * return immediately and the actual free will occur some time later
200 int ion_heap_init_deferred_free(struct ion_heap *heap);
203 * ion_heap_freelist_add - add a buffer to the deferred free list
205 * @buffer: the buffer
207 * Adds an item to the deferred freelist.
209 void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer);
212 * ion_heap_freelist_drain - drain the deferred free list
214 * @size: amount of memory to drain in bytes
216 * Drains the indicated amount of memory from the deferred freelist immediately.
217 * Returns the total amount freed. The total freed may be higher depending
218 * on the size of the items in the list, or lower if there is insufficient
219 * total memory on the freelist.
221 size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size);
224 * ion_heap_freelist_shrink - drain the deferred free
225 * list, skipping any heap-specific
226 * pooling or caching mechanisms
229 * @size: amount of memory to drain in bytes
231 * Drains the indicated amount of memory from the deferred freelist immediately.
232 * Returns the total amount freed. The total freed may be higher depending
233 * on the size of the items in the list, or lower if there is insufficient
234 * total memory on the freelist.
236 * Unlike with @ion_heap_freelist_drain, don't put any pages back into
237 * page pools or otherwise cache the pages. Everything must be
238 * genuinely free'd back to the system. If you're free'ing from a
239 * shrinker you probably want to use this. Note that this relies on
240 * the heap.ops.free callback honoring the ION_PRIV_FLAG_SHRINKER_FREE
243 size_t ion_heap_freelist_shrink(struct ion_heap *heap,
247 * ion_heap_freelist_size - returns the size of the freelist in bytes
250 size_t ion_heap_freelist_size(struct ion_heap *heap);
253 * functions for creating and destroying a heap pool -- allows you
254 * to keep a pool of pre allocated memory to use from your heap. Keeping
255 * a pool of memory that is ready for dma, ie any cached mapping have been
256 * invalidated from the cache, provides a significant performance benefit on
261 * struct ion_page_pool - pagepool struct
262 * @high_count: number of highmem items in the pool
263 * @low_count: number of lowmem items in the pool
264 * @high_items: list of highmem items
265 * @low_items: list of lowmem items
266 * @mutex: lock protecting this struct and especially the count
268 * @gfp_mask: gfp_mask to use from alloc
269 * @order: order of pages in the pool
270 * @list: plist node for list of pools
272 * Allows you to keep a pool of pre allocated pages to use from your heap.
273 * Keeping a pool of pages that is ready for dma, ie any cached mapping have
274 * been invalidated from the cache, provides a significant performance benefit
277 struct ion_page_pool {
280 struct list_head high_items;
281 struct list_head low_items;
285 struct plist_node list;
288 struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order);
289 void ion_page_pool_destroy(struct ion_page_pool *pool);
290 struct page *ion_page_pool_alloc(struct ion_page_pool *pool);
291 void ion_page_pool_free(struct ion_page_pool *pool, struct page *page);
293 /** ion_page_pool_shrink - shrinks the size of the memory cached in the pool
295 * @gfp_mask: the memory type to reclaim
296 * @nr_to_scan: number of items to shrink in pages
298 * returns the number of items freed in pages
300 int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,