GNU Linux-libre 4.14.290-gnu1
[releases.git] / drivers / staging / android / ion / ion.h
1 /*
2  * drivers/staging/android/ion/ion.h
3  *
4  * Copyright (C) 2011 Google, Inc.
5  *
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.
9  *
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.
14  *
15  */
16
17 #ifndef _ION_H
18 #define _ION_H
19
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>
30
31 #include "../uapi/ion.h"
32
33 /**
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
43  *
44  * Provided by the board file.
45  */
46 struct ion_platform_heap {
47         enum ion_heap_type type;
48         unsigned int id;
49         const char *name;
50         phys_addr_t base;
51         size_t size;
52         phys_addr_t align;
53         void *priv;
54 };
55
56 /**
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
66  *                      a void *
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
71  */
72 struct ion_buffer {
73         union {
74                 struct rb_node node;
75                 struct list_head list;
76         };
77         struct ion_device *dev;
78         struct ion_heap *heap;
79         unsigned long flags;
80         unsigned long private_flags;
81         size_t size;
82         void *priv_virt;
83         struct mutex lock;
84         int kmap_cnt;
85         void *vaddr;
86         struct sg_table *sg_table;
87         struct list_head attachments;
88 };
89 void ion_buffer_destroy(struct ion_buffer *buffer);
90
91 /**
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
97  */
98 struct ion_device {
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;
105         int heap_cnt;
106 };
107
108 /**
109  * struct ion_heap_ops - ops to operate on a given heap
110  * @allocate:           allocate memory
111  * @free:               free memory
112  * @map_kernel          map memory to the kernel
113  * @unmap_kernel        unmap memory to the kernel
114  * @map_user            map memory to userspace
115  *
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.
122  */
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);
133 };
134
135 /**
136  * heap flags - flags between the heaps and core ion code
137  */
138 #define ION_HEAP_FLAG_DEFER_FREE BIT(0)
139
140 /**
141  * private flags - flags internal to ion
142  */
143 /*
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.
148  */
149 #define ION_PRIV_FLAG_SHRINKER_FREE BIT(0)
150
151 /**
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
157  * @flags:              flags
158  * @id:                 id of heap, also indicates priority of this heap when
159  *                      allocating.  These are specified by platform data and
160  *                      MUST be unique
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
170  *
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.
175  */
176 struct ion_heap {
177         struct plist_node node;
178         struct ion_device *dev;
179         enum ion_heap_type type;
180         struct ion_heap_ops *ops;
181         unsigned long flags;
182         unsigned int id;
183         const char *name;
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;
190
191         int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *);
192 };
193
194 /**
195  * ion_buffer_cached - this ion buffer is cached
196  * @buffer:             buffer
197  *
198  * indicates whether this ion buffer is cached
199  */
200 bool ion_buffer_cached(struct ion_buffer *buffer);
201
202 /**
203  * ion_buffer_fault_user_mappings - fault in user mappings of this buffer
204  * @buffer:             buffer
205  *
206  * indicates whether userspace mappings of this buffer will be faulted
207  * in, this can affect how buffers are allocated from the heap.
208  */
209 bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer);
210
211 /**
212  * ion_device_add_heap - adds a heap to the ion device
213  * @heap:               the heap to add
214  */
215 void ion_device_add_heap(struct ion_heap *heap);
216
217 /**
218  * some helpers for common operations on buffers using the sg_table
219  * and vaddr fields
220  */
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);
227
228 int ion_alloc(size_t len,
229               unsigned int heap_id_mask,
230               unsigned int flags);
231
232 /**
233  * ion_heap_init_shrinker
234  * @heap:               the heap
235  *
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.
239  */
240 void ion_heap_init_shrinker(struct ion_heap *heap);
241
242 /**
243  * ion_heap_init_deferred_free -- initialize deferred free functionality
244  * @heap:               the heap
245  *
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
249  */
250 int ion_heap_init_deferred_free(struct ion_heap *heap);
251
252 /**
253  * ion_heap_freelist_add - add a buffer to the deferred free list
254  * @heap:               the heap
255  * @buffer:             the buffer
256  *
257  * Adds an item to the deferred freelist.
258  */
259 void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer);
260
261 /**
262  * ion_heap_freelist_drain - drain the deferred free list
263  * @heap:               the heap
264  * @size:               amount of memory to drain in bytes
265  *
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.
270  */
271 size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size);
272
273 /**
274  * ion_heap_freelist_shrink - drain the deferred free
275  *                              list, skipping any heap-specific
276  *                              pooling or caching mechanisms
277  *
278  * @heap:               the heap
279  * @size:               amount of memory to drain in bytes
280  *
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.
285  *
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
291  * flag.
292  */
293 size_t ion_heap_freelist_shrink(struct ion_heap *heap,
294                                 size_t size);
295
296 /**
297  * ion_heap_freelist_size - returns the size of the freelist in bytes
298  * @heap:               the heap
299  */
300 size_t ion_heap_freelist_size(struct ion_heap *heap);
301
302
303 /**
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
308  * many systems
309  */
310
311 /**
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
318  *                      item list
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
323  *
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
327  * on many systems
328  */
329 struct ion_page_pool {
330         int high_count;
331         int low_count;
332         bool cached;
333         struct list_head high_items;
334         struct list_head low_items;
335         struct mutex mutex;
336         gfp_t gfp_mask;
337         unsigned int order;
338         struct plist_node list;
339 };
340
341 struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order,
342                                            bool cached);
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);
346
347 /** ion_page_pool_shrink - shrinks the size of the memory cached in the pool
348  * @pool:               the pool
349  * @gfp_mask:           the memory type to reclaim
350  * @nr_to_scan:         number of items to shrink in pages
351  *
352  * returns the number of items freed in pages
353  */
354 int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
355                          int nr_to_scan);
356
357 long ion_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
358
359 int ion_query_heaps(struct ion_heap_query *query);
360
361 #endif /* _ION_H */