]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/staging/android/ion/ion_priv.h
staging: android: ion: Drop ion_phys interface
[linux.git] / drivers / staging / android / ion / ion_priv.h
1 /*
2  * drivers/staging/android/ion/ion_priv.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_PRIV_H
18 #define _ION_PRIV_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
30 #include "ion.h"
31
32 struct ion_buffer *ion_handle_buffer(struct ion_handle *handle);
33
34 /**
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
44  *                      a void *
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
58 */
59 struct ion_buffer {
60         struct kref ref;
61         union {
62                 struct rb_node node;
63                 struct list_head list;
64         };
65         struct ion_device *dev;
66         struct ion_heap *heap;
67         unsigned long flags;
68         unsigned long private_flags;
69         size_t size;
70         void *priv_virt;
71         struct mutex lock;
72         int kmap_cnt;
73         void *vaddr;
74         int dmap_cnt;
75         struct sg_table *sg_table;
76         struct page **pages;
77         struct list_head vmas;
78         /* used to track orphaned buffers */
79         int handle_count;
80         char task_comm[TASK_COMM_LEN];
81         pid_t pid;
82 };
83 void ion_buffer_destroy(struct ion_buffer *buffer);
84
85 /**
86  * struct ion_heap_ops - ops to operate on a given heap
87  * @allocate:           allocate memory
88  * @free:               free memory
89  * @map_dma             map the memory for dma to a scatterlist
90  * @unmap_dma           unmap the memory for dma
91  * @map_kernel          map memory to the kernel
92  * @unmap_kernel        unmap memory to the kernel
93  * @map_user            map memory to userspace
94  *
95  * allocate, phys, and map_user return 0 on success, -errno on error.
96  * map_dma and map_kernel return pointer on success, ERR_PTR on
97  * error. @free will be called with ION_PRIV_FLAG_SHRINKER_FREE set in
98  * the buffer's private_flags when called from a shrinker. In that
99  * case, the pages being free'd must be truly free'd back to the
100  * system, not put in a page pool or otherwise cached.
101  */
102 struct ion_heap_ops {
103         int (*allocate)(struct ion_heap *heap,
104                         struct ion_buffer *buffer, unsigned long len,
105                         unsigned long align, unsigned long flags);
106         void (*free)(struct ion_buffer *buffer);
107         struct sg_table * (*map_dma)(struct ion_heap *heap,
108                                      struct ion_buffer *buffer);
109         void (*unmap_dma)(struct ion_heap *heap, struct ion_buffer *buffer);
110         void * (*map_kernel)(struct ion_heap *heap, struct ion_buffer *buffer);
111         void (*unmap_kernel)(struct ion_heap *heap, struct ion_buffer *buffer);
112         int (*map_user)(struct ion_heap *mapper, struct ion_buffer *buffer,
113                         struct vm_area_struct *vma);
114         int (*shrink)(struct ion_heap *heap, gfp_t gfp_mask, int nr_to_scan);
115 };
116
117 /**
118  * heap flags - flags between the heaps and core ion code
119  */
120 #define ION_HEAP_FLAG_DEFER_FREE (1 << 0)
121
122 /**
123  * private flags - flags internal to ion
124  */
125 /*
126  * Buffer is being freed from a shrinker function. Skip any possible
127  * heap-specific caching mechanism (e.g. page pools). Guarantees that
128  * any buffer storage that came from the system allocator will be
129  * returned to the system allocator.
130  */
131 #define ION_PRIV_FLAG_SHRINKER_FREE (1 << 0)
132
133 /**
134  * struct ion_heap - represents a heap in the system
135  * @node:               rb node to put the heap on the device's tree of heaps
136  * @dev:                back pointer to the ion_device
137  * @type:               type of heap
138  * @ops:                ops struct as above
139  * @flags:              flags
140  * @id:                 id of heap, also indicates priority of this heap when
141  *                      allocating.  These are specified by platform data and
142  *                      MUST be unique
143  * @name:               used for debugging
144  * @shrinker:           a shrinker for the heap
145  * @free_list:          free list head if deferred free is used
146  * @free_list_size      size of the deferred free list in bytes
147  * @lock:               protects the free list
148  * @waitqueue:          queue to wait on from deferred free thread
149  * @task:               task struct of deferred free thread
150  * @debug_show:         called when heap debug file is read to add any
151  *                      heap specific debug info to output
152  *
153  * Represents a pool of memory from which buffers can be made.  In some
154  * systems the only heap is regular system memory allocated via vmalloc.
155  * On others, some blocks might require large physically contiguous buffers
156  * that are allocated from a specially reserved heap.
157  */
158 struct ion_heap {
159         struct plist_node node;
160         struct ion_device *dev;
161         enum ion_heap_type type;
162         struct ion_heap_ops *ops;
163         unsigned long flags;
164         unsigned int id;
165         const char *name;
166         struct shrinker shrinker;
167         struct list_head free_list;
168         size_t free_list_size;
169         spinlock_t free_lock;
170         wait_queue_head_t waitqueue;
171         struct task_struct *task;
172
173         int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *);
174 };
175
176 /**
177  * ion_buffer_cached - this ion buffer is cached
178  * @buffer:             buffer
179  *
180  * indicates whether this ion buffer is cached
181  */
182 bool ion_buffer_cached(struct ion_buffer *buffer);
183
184 /**
185  * ion_buffer_fault_user_mappings - fault in user mappings of this buffer
186  * @buffer:             buffer
187  *
188  * indicates whether userspace mappings of this buffer will be faulted
189  * in, this can affect how buffers are allocated from the heap.
190  */
191 bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer);
192
193 /**
194  * ion_device_create - allocates and returns an ion device
195  * @custom_ioctl:       arch specific ioctl function if applicable
196  *
197  * returns a valid device or -PTR_ERR
198  */
199 struct ion_device *ion_device_create(long (*custom_ioctl)
200                                      (struct ion_client *client,
201                                       unsigned int cmd,
202                                       unsigned long arg));
203
204 /**
205  * ion_device_destroy - free and device and it's resource
206  * @dev:                the device
207  */
208 void ion_device_destroy(struct ion_device *dev);
209
210 /**
211  * ion_device_add_heap - adds a heap to the ion device
212  * @dev:                the device
213  * @heap:               the heap to add
214  */
215 void ion_device_add_heap(struct ion_device *dev, 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 *, struct ion_buffer *);
222 void ion_heap_unmap_kernel(struct ion_heap *, struct ion_buffer *);
223 int ion_heap_map_user(struct ion_heap *, struct ion_buffer *,
224                         struct vm_area_struct *);
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 /**
229  * ion_heap_init_shrinker
230  * @heap:               the heap
231  *
232  * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag or defines the shrink op
233  * this function will be called to setup a shrinker to shrink the freelists
234  * and call the heap's shrink op.
235  */
236 void ion_heap_init_shrinker(struct ion_heap *heap);
237
238 /**
239  * ion_heap_init_deferred_free -- initialize deferred free functionality
240  * @heap:               the heap
241  *
242  * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag this function will
243  * be called to setup deferred frees. Calls to free the buffer will
244  * return immediately and the actual free will occur some time later
245  */
246 int ion_heap_init_deferred_free(struct ion_heap *heap);
247
248 /**
249  * ion_heap_freelist_add - add a buffer to the deferred free list
250  * @heap:               the heap
251  * @buffer:             the buffer
252  *
253  * Adds an item to the deferred freelist.
254  */
255 void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer);
256
257 /**
258  * ion_heap_freelist_drain - drain the deferred free list
259  * @heap:               the heap
260  * @size:               amount of memory to drain in bytes
261  *
262  * Drains the indicated amount of memory from the deferred freelist immediately.
263  * Returns the total amount freed.  The total freed may be higher depending
264  * on the size of the items in the list, or lower if there is insufficient
265  * total memory on the freelist.
266  */
267 size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size);
268
269 /**
270  * ion_heap_freelist_shrink - drain the deferred free
271  *                              list, skipping any heap-specific
272  *                              pooling or caching mechanisms
273  *
274  * @heap:               the heap
275  * @size:               amount of memory to drain in bytes
276  *
277  * Drains the indicated amount of memory from the deferred freelist immediately.
278  * Returns the total amount freed.  The total freed may be higher depending
279  * on the size of the items in the list, or lower if there is insufficient
280  * total memory on the freelist.
281  *
282  * Unlike with @ion_heap_freelist_drain, don't put any pages back into
283  * page pools or otherwise cache the pages. Everything must be
284  * genuinely free'd back to the system. If you're free'ing from a
285  * shrinker you probably want to use this. Note that this relies on
286  * the heap.ops.free callback honoring the ION_PRIV_FLAG_SHRINKER_FREE
287  * flag.
288  */
289 size_t ion_heap_freelist_shrink(struct ion_heap *heap,
290                                         size_t size);
291
292 /**
293  * ion_heap_freelist_size - returns the size of the freelist in bytes
294  * @heap:               the heap
295  */
296 size_t ion_heap_freelist_size(struct ion_heap *heap);
297
298
299 /**
300  * functions for creating and destroying the built in ion heaps.
301  * architectures can add their own custom architecture specific
302  * heaps as appropriate.
303  */
304
305 struct ion_heap *ion_heap_create(struct ion_platform_heap *);
306 void ion_heap_destroy(struct ion_heap *);
307 struct ion_heap *ion_system_heap_create(struct ion_platform_heap *);
308 void ion_system_heap_destroy(struct ion_heap *);
309
310 struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *);
311 void ion_system_contig_heap_destroy(struct ion_heap *);
312
313 struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *);
314 void ion_carveout_heap_destroy(struct ion_heap *);
315
316 struct ion_heap *ion_chunk_heap_create(struct ion_platform_heap *);
317 void ion_chunk_heap_destroy(struct ion_heap *);
318 struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *);
319 void ion_cma_heap_destroy(struct ion_heap *);
320
321 /**
322  * kernel api to allocate/free from carveout -- used when carveout is
323  * used to back an architecture specific custom heap
324  */
325 ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size,
326                                       unsigned long align);
327 void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr,
328                        unsigned long size);
329 /**
330  * The carveout heap returns physical addresses, since 0 may be a valid
331  * physical address, this is used to indicate allocation failed
332  */
333 #define ION_CARVEOUT_ALLOCATE_FAIL -1
334
335 /**
336  * functions for creating and destroying a heap pool -- allows you
337  * to keep a pool of pre allocated memory to use from your heap.  Keeping
338  * a pool of memory that is ready for dma, ie any cached mapping have been
339  * invalidated from the cache, provides a significant performance benefit on
340  * many systems
341  */
342
343 /**
344  * struct ion_page_pool - pagepool struct
345  * @high_count:         number of highmem items in the pool
346  * @low_count:          number of lowmem items in the pool
347  * @high_items:         list of highmem items
348  * @low_items:          list of lowmem items
349  * @mutex:              lock protecting this struct and especially the count
350  *                      item list
351  * @gfp_mask:           gfp_mask to use from alloc
352  * @order:              order of pages in the pool
353  * @list:               plist node for list of pools
354  *
355  * Allows you to keep a pool of pre allocated pages to use from your heap.
356  * Keeping a pool of pages that is ready for dma, ie any cached mapping have
357  * been invalidated from the cache, provides a significant performance benefit
358  * on many systems
359  */
360 struct ion_page_pool {
361         int high_count;
362         int low_count;
363         struct list_head high_items;
364         struct list_head low_items;
365         struct mutex mutex;
366         gfp_t gfp_mask;
367         unsigned int order;
368         struct plist_node list;
369 };
370
371 struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order);
372 void ion_page_pool_destroy(struct ion_page_pool *);
373 struct page *ion_page_pool_alloc(struct ion_page_pool *);
374 void ion_page_pool_free(struct ion_page_pool *, struct page *);
375
376 /** ion_page_pool_shrink - shrinks the size of the memory cached in the pool
377  * @pool:               the pool
378  * @gfp_mask:           the memory type to reclaim
379  * @nr_to_scan:         number of items to shrink in pages
380  *
381  * returns the number of items freed in pages
382  */
383 int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
384                           int nr_to_scan);
385
386 /**
387  * ion_pages_sync_for_device - cache flush pages for use with the specified
388  *                             device
389  * @dev:                the device the pages will be used with
390  * @page:               the first page to be flushed
391  * @size:               size in bytes of region to be flushed
392  * @dir:                direction of dma transfer
393  */
394 void ion_pages_sync_for_device(struct device *dev, struct page *page,
395                 size_t size, enum dma_data_direction dir);
396
397 #endif /* _ION_PRIV_H */