]> Git Repo - linux.git/blob - drivers/staging/android/ion/ion.h
ARM: dts: imx7s: Enable SNVS power key according to board design
[linux.git] / drivers / staging / android / ion / ion.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * ION Memory Allocator kernel interface header
4  *
5  * Copyright (C) 2011 Google, Inc.
6  */
7
8 #ifndef _ION_H
9 #define _ION_H
10
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>
21
22 #include "../uapi/ion.h"
23
24 /**
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
34  *                      a void *
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
40  */
41 struct ion_buffer {
42         union {
43                 struct rb_node node;
44                 struct list_head list;
45         };
46         struct ion_device *dev;
47         struct ion_heap *heap;
48         unsigned long flags;
49         unsigned long private_flags;
50         size_t size;
51         void *priv_virt;
52         struct mutex lock;
53         int kmap_cnt;
54         void *vaddr;
55         struct sg_table *sg_table;
56         struct list_head attachments;
57 };
58
59 void ion_buffer_destroy(struct ion_buffer *buffer);
60
61 /**
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
67  */
68 struct ion_device {
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;
75         int heap_cnt;
76 };
77
78 /**
79  * struct ion_heap_ops - ops to operate on a given heap
80  * @allocate:           allocate memory
81  * @free:               free memory
82  * @map_kernel          map memory to the kernel
83  * @unmap_kernel        unmap memory to the kernel
84  * @map_user            map memory to userspace
85  *
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.
92  */
93 struct ion_heap_ops {
94         int (*allocate)(struct ion_heap *heap,
95                         struct ion_buffer *buffer, unsigned long len,
96                         unsigned long flags);
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);
103 };
104
105 /**
106  * heap flags - flags between the heaps and core ion code
107  */
108 #define ION_HEAP_FLAG_DEFER_FREE BIT(0)
109
110 /**
111  * private flags - flags internal to ion
112  */
113 /*
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.
118  */
119 #define ION_PRIV_FLAG_SHRINKER_FREE BIT(0)
120
121 /**
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
127  * @flags:              flags
128  * @id:                 id of heap, also indicates priority of this heap when
129  *                      allocating.  These are specified by platform data and
130  *                      MUST be unique
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
141  *
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.
146  */
147 struct ion_heap {
148         struct plist_node node;
149         struct ion_device *dev;
150         enum ion_heap_type type;
151         struct ion_heap_ops *ops;
152         unsigned long flags;
153         unsigned int id;
154         const char *name;
155
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;
163
164         /* heap statistics */
165         u64 num_of_buffers;
166         u64 num_of_alloc_bytes;
167         u64 alloc_bytes_wm;
168
169         /* protect heap statistics */
170         spinlock_t stat_lock;
171 };
172
173 /**
174  * ion_device_add_heap - adds a heap to the ion device
175  * @heap:               the heap to add
176  */
177 void ion_device_add_heap(struct ion_heap *heap);
178
179 /**
180  * some helpers for common operations on buffers using the sg_table
181  * and vaddr fields
182  */
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);
189
190 /**
191  * ion_heap_init_shrinker
192  * @heap:               the heap
193  *
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.
197  */
198 int ion_heap_init_shrinker(struct ion_heap *heap);
199
200 /**
201  * ion_heap_init_deferred_free -- initialize deferred free functionality
202  * @heap:               the heap
203  *
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
207  */
208 int ion_heap_init_deferred_free(struct ion_heap *heap);
209
210 /**
211  * ion_heap_freelist_add - add a buffer to the deferred free list
212  * @heap:               the heap
213  * @buffer:             the buffer
214  *
215  * Adds an item to the deferred freelist.
216  */
217 void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer);
218
219 /**
220  * ion_heap_freelist_drain - drain the deferred free list
221  * @heap:               the heap
222  * @size:               amount of memory to drain in bytes
223  *
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.
228  */
229 size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size);
230
231 /**
232  * ion_heap_freelist_shrink - drain the deferred free
233  *                              list, skipping any heap-specific
234  *                              pooling or caching mechanisms
235  *
236  * @heap:               the heap
237  * @size:               amount of memory to drain in bytes
238  *
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.
243  *
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
249  * flag.
250  */
251 size_t ion_heap_freelist_shrink(struct ion_heap *heap,
252                                 size_t size);
253
254 /**
255  * ion_heap_freelist_size - returns the size of the freelist in bytes
256  * @heap:               the heap
257  */
258 size_t ion_heap_freelist_size(struct ion_heap *heap);
259
260 /**
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
265  * many systems
266  */
267
268 /**
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
275  *                      item list
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
279  *
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
283  * on many systems
284  */
285 struct ion_page_pool {
286         int high_count;
287         int low_count;
288         struct list_head high_items;
289         struct list_head low_items;
290         struct mutex mutex;
291         gfp_t gfp_mask;
292         unsigned int order;
293         struct plist_node list;
294 };
295
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);
300
301 /** ion_page_pool_shrink - shrinks the size of the memory cached in the pool
302  * @pool:               the pool
303  * @gfp_mask:           the memory type to reclaim
304  * @nr_to_scan:         number of items to shrink in pages
305  *
306  * returns the number of items freed in pages
307  */
308 int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
309                          int nr_to_scan);
310
311 #endif /* _ION_H */
This page took 0.051039 seconds and 4 git commands to generate.