Merge branch 'android-4.4'
[firefly-linux-kernel-4.4.55.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 #ifdef CONFIG_ION_POOL_CACHE_POLICY
30 #include <asm/cacheflush.h>
31 #endif
32
33 #include "ion.h"
34
35 struct ion_buffer *ion_handle_buffer(struct ion_handle *handle);
36
37 #ifdef CONFIG_RK_IOMMU
38 /**
39  * struct ion_iommu_map - represents a mapping of an ion buffer to an iommu
40  * @iova_addr - iommu virtual address
41  * @node - rb node to exist in the buffer's tree of iommu mappings
42  * @key - contains the iommu device info
43  * @ref - for reference counting this mapping
44  * @mapped_size - size of the iova space mapped
45  *              (may not be the same as the buffer size)
46  *
47  * Represents a mapping of one ion buffer to a particular iommu domain
48  * and address range. There may exist other mappings of this buffer in
49  * different domains or address ranges. All mappings will have the same
50  * cacheability and security.
51  */
52 struct ion_iommu_map {
53         unsigned long iova_addr;
54         struct rb_node node;
55         unsigned long key;
56         struct ion_buffer *buffer;
57         struct kref ref;
58         int mapped_size;
59 };
60 #endif
61
62 /**
63  * struct ion_buffer - metadata for a particular buffer
64  * @ref:                reference count
65  * @node:               node in the ion_device buffers tree
66  * @dev:                back pointer to the ion_device
67  * @heap:               back pointer to the heap the buffer came from
68  * @flags:              buffer specific flags
69  * @private_flags:      internal buffer specific flags
70  * @size:               size of the buffer
71  * @priv_virt:          private data to the buffer representable as
72  *                      a void *
73  * @priv_phys:          private data to the buffer representable as
74  *                      an ion_phys_addr_t (and someday a phys_addr_t)
75  * @lock:               protects the buffers cnt fields
76  * @kmap_cnt:           number of times the buffer is mapped to the kernel
77  * @vaddr:              the kernel mapping if kmap_cnt is not zero
78  * @dmap_cnt:           number of times the buffer is mapped for dma
79  * @sg_table:           the sg table for the buffer if dmap_cnt is not zero
80  * @pages:              flat array of pages in the buffer -- used by fault
81  *                      handler and only valid for buffers that are faulted in
82  * @vmas:               list of vma's mapping this buffer
83  * @handle_count:       count of handles referencing this buffer
84  * @task_comm:          taskcomm of last client to reference this buffer in a
85  *                      handle, used for debugging
86  * @pid:                pid of last client to reference this buffer in a
87  *                      handle, used for debugging
88 */
89 struct ion_buffer {
90         struct kref ref;
91         union {
92                 struct rb_node node;
93                 struct list_head list;
94         };
95         struct ion_device *dev;
96         struct ion_heap *heap;
97         unsigned long flags;
98         unsigned long private_flags;
99         size_t size;
100         union {
101                 void *priv_virt;
102                 ion_phys_addr_t priv_phys;
103         };
104         struct mutex lock;
105         int kmap_cnt;
106         void *vaddr;
107         int dmap_cnt;
108         struct sg_table *sg_table;
109         struct page **pages;
110         struct list_head vmas;
111         /* used to track orphaned buffers */
112         int handle_count;
113         char task_comm[TASK_COMM_LEN];
114         pid_t pid;
115 #ifdef CONFIG_RK_IOMMU
116         unsigned int iommu_map_cnt;
117         struct rb_root iommu_maps;
118 #endif
119 };
120 void ion_buffer_destroy(struct ion_buffer *buffer);
121
122 /**
123  * struct ion_heap_ops - ops to operate on a given heap
124  * @allocate:           allocate memory
125  * @free:               free memory
126  * @phys                get physical address of a buffer (only define on
127  *                      physically contiguous heaps)
128  * @map_dma             map the memory for dma to a scatterlist
129  * @unmap_dma           unmap the memory for dma
130  * @map_kernel          map memory to the kernel
131  * @unmap_kernel        unmap memory to the kernel
132  * @map_user            map memory to userspace
133  *
134  * allocate, phys, and map_user return 0 on success, -errno on error.
135  * map_dma and map_kernel return pointer on success, ERR_PTR on
136  * error. @free will be called with ION_PRIV_FLAG_SHRINKER_FREE set in
137  * the buffer's private_flags when called from a shrinker. In that
138  * case, the pages being free'd must be truly free'd back to the
139  * system, not put in a page pool or otherwise cached.
140  */
141 struct ion_heap_ops {
142         int (*allocate)(struct ion_heap *heap,
143                         struct ion_buffer *buffer, unsigned long len,
144                         unsigned long align, unsigned long flags);
145         void (*free)(struct ion_buffer *buffer);
146         int (*phys)(struct ion_heap *heap, struct ion_buffer *buffer,
147                     ion_phys_addr_t *addr, size_t *len);
148         struct sg_table * (*map_dma)(struct ion_heap *heap,
149                                      struct ion_buffer *buffer);
150         void (*unmap_dma)(struct ion_heap *heap, struct ion_buffer *buffer);
151         void * (*map_kernel)(struct ion_heap *heap, struct ion_buffer *buffer);
152         void (*unmap_kernel)(struct ion_heap *heap, struct ion_buffer *buffer);
153         int (*map_user)(struct ion_heap *mapper, struct ion_buffer *buffer,
154                         struct vm_area_struct *vma);
155         int (*shrink)(struct ion_heap *heap, gfp_t gfp_mask, int nr_to_scan);
156 #ifdef CONFIG_RK_IOMMU
157         int (*map_iommu)(struct ion_buffer *buffer,
158                          struct device *iommu_dev,
159                          struct ion_iommu_map *map_data,
160                          unsigned long iova_length,
161                          unsigned long flags);
162         void (*unmap_iommu)(struct device *iommu_dev,
163                             struct ion_iommu_map *data);
164 #endif
165 };
166
167 /**
168  * heap flags - flags between the heaps and core ion code
169  */
170 #define ION_HEAP_FLAG_DEFER_FREE (1 << 0)
171
172 /**
173  * private flags - flags internal to ion
174  */
175 /*
176  * Buffer is being freed from a shrinker function. Skip any possible
177  * heap-specific caching mechanism (e.g. page pools). Guarantees that
178  * any buffer storage that came from the system allocator will be
179  * returned to the system allocator.
180  */
181 #define ION_PRIV_FLAG_SHRINKER_FREE (1 << 0)
182
183 /**
184  * struct ion_heap - represents a heap in the system
185  * @node:               rb node to put the heap on the device's tree of heaps
186  * @dev:                back pointer to the ion_device
187  * @type:               type of heap
188  * @ops:                ops struct as above
189  * @flags:              flags
190  * @id:                 id of heap, also indicates priority of this heap when
191  *                      allocating.  These are specified by platform data and
192  *                      MUST be unique
193  * @name:               used for debugging
194  * @shrinker:           a shrinker for the heap
195  * @free_list:          free list head if deferred free is used
196  * @free_list_size      size of the deferred free list in bytes
197  * @lock:               protects the free list
198  * @waitqueue:          queue to wait on from deferred free thread
199  * @task:               task struct of deferred free thread
200  * @debug_show:         called when heap debug file is read to add any
201  *                      heap specific debug info to output
202  *
203  * Represents a pool of memory from which buffers can be made.  In some
204  * systems the only heap is regular system memory allocated via vmalloc.
205  * On others, some blocks might require large physically contiguous buffers
206  * that are allocated from a specially reserved heap.
207  */
208 struct ion_heap {
209         struct plist_node node;
210         struct ion_device *dev;
211         enum ion_heap_type type;
212         struct ion_heap_ops *ops;
213         unsigned long flags;
214         unsigned int id;
215         const char *name;
216         struct shrinker shrinker;
217         struct list_head free_list;
218         size_t free_list_size;
219         spinlock_t free_lock;
220         wait_queue_head_t waitqueue;
221         struct task_struct *task;
222
223         int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *);
224 };
225
226 /**
227  * ion_buffer_cached - this ion buffer is cached
228  * @buffer:             buffer
229  *
230  * indicates whether this ion buffer is cached
231  */
232 bool ion_buffer_cached(struct ion_buffer *buffer);
233
234 /**
235  * ion_buffer_fault_user_mappings - fault in user mappings of this buffer
236  * @buffer:             buffer
237  *
238  * indicates whether userspace mappings of this buffer will be faulted
239  * in, this can affect how buffers are allocated from the heap.
240  */
241 bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer);
242
243 /**
244  * ion_device_create - allocates and returns an ion device
245  * @custom_ioctl:       arch specific ioctl function if applicable
246  *
247  * returns a valid device or -PTR_ERR
248  */
249 struct ion_device *ion_device_create(long (*custom_ioctl)
250                                      (struct ion_client *client,
251                                       unsigned int cmd,
252                                       unsigned long arg));
253
254 /**
255  * ion_device_destroy - free and device and it's resource
256  * @dev:                the device
257  */
258 void ion_device_destroy(struct ion_device *dev);
259
260 /**
261  * ion_device_add_heap - adds a heap to the ion device
262  * @dev:                the device
263  * @heap:               the heap to add
264  */
265 void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap);
266
267 /**
268  * some helpers for common operations on buffers using the sg_table
269  * and vaddr fields
270  */
271 void *ion_heap_map_kernel(struct ion_heap *, struct ion_buffer *);
272 void ion_heap_unmap_kernel(struct ion_heap *, struct ion_buffer *);
273 int ion_heap_map_user(struct ion_heap *, struct ion_buffer *,
274                         struct vm_area_struct *);
275 int ion_heap_buffer_zero(struct ion_buffer *buffer);
276 int ion_heap_pages_zero(struct page *page, size_t size, pgprot_t pgprot);
277
278 /**
279  * ion_heap_init_shrinker
280  * @heap:               the heap
281  *
282  * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag or defines the shrink op
283  * this function will be called to setup a shrinker to shrink the freelists
284  * and call the heap's shrink op.
285  */
286 void ion_heap_init_shrinker(struct ion_heap *heap);
287
288 /**
289  * ion_heap_init_deferred_free -- initialize deferred free functionality
290  * @heap:               the heap
291  *
292  * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag this function will
293  * be called to setup deferred frees. Calls to free the buffer will
294  * return immediately and the actual free will occur some time later
295  */
296 int ion_heap_init_deferred_free(struct ion_heap *heap);
297
298 /**
299  * ion_heap_freelist_add - add a buffer to the deferred free list
300  * @heap:               the heap
301  * @buffer:             the buffer
302  *
303  * Adds an item to the deferred freelist.
304  */
305 void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer);
306
307 /**
308  * ion_heap_freelist_drain - drain the deferred free list
309  * @heap:               the heap
310  * @size:               amount of memory to drain in bytes
311  *
312  * Drains the indicated amount of memory from the deferred freelist immediately.
313  * Returns the total amount freed.  The total freed may be higher depending
314  * on the size of the items in the list, or lower if there is insufficient
315  * total memory on the freelist.
316  */
317 size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size);
318
319 /**
320  * ion_heap_freelist_shrink - drain the deferred free
321  *                              list, skipping any heap-specific
322  *                              pooling or caching mechanisms
323  *
324  * @heap:               the heap
325  * @size:               amount of memory to drain in bytes
326  *
327  * Drains the indicated amount of memory from the deferred freelist immediately.
328  * Returns the total amount freed.  The total freed may be higher depending
329  * on the size of the items in the list, or lower if there is insufficient
330  * total memory on the freelist.
331  *
332  * Unlike with @ion_heap_freelist_drain, don't put any pages back into
333  * page pools or otherwise cache the pages. Everything must be
334  * genuinely free'd back to the system. If you're free'ing from a
335  * shrinker you probably want to use this. Note that this relies on
336  * the heap.ops.free callback honoring the ION_PRIV_FLAG_SHRINKER_FREE
337  * flag.
338  */
339 size_t ion_heap_freelist_shrink(struct ion_heap *heap,
340                                         size_t size);
341
342 /**
343  * ion_heap_freelist_size - returns the size of the freelist in bytes
344  * @heap:               the heap
345  */
346 size_t ion_heap_freelist_size(struct ion_heap *heap);
347
348
349 /**
350  * functions for creating and destroying the built in ion heaps.
351  * architectures can add their own custom architecture specific
352  * heaps as appropriate.
353  */
354
355 struct ion_heap *ion_heap_create(struct ion_platform_heap *);
356 void ion_heap_destroy(struct ion_heap *);
357 struct ion_heap *ion_system_heap_create(struct ion_platform_heap *);
358 void ion_system_heap_destroy(struct ion_heap *);
359
360 struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *);
361 void ion_system_contig_heap_destroy(struct ion_heap *);
362
363 struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *);
364 void ion_carveout_heap_destroy(struct ion_heap *);
365
366 struct ion_heap *ion_chunk_heap_create(struct ion_platform_heap *);
367 void ion_chunk_heap_destroy(struct ion_heap *);
368 struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *);
369 void ion_cma_heap_destroy(struct ion_heap *);
370
371 /**
372  * kernel api to allocate/free from carveout -- used when carveout is
373  * used to back an architecture specific custom heap
374  */
375 ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size,
376                                       unsigned long align);
377 void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr,
378                        unsigned long size);
379 /**
380  * The carveout heap returns physical addresses, since 0 may be a valid
381  * physical address, this is used to indicate allocation failed
382  */
383 #define ION_CARVEOUT_ALLOCATE_FAIL -1
384
385 /**
386  * functions for creating and destroying a heap pool -- allows you
387  * to keep a pool of pre allocated memory to use from your heap.  Keeping
388  * a pool of memory that is ready for dma, ie any cached mapping have been
389  * invalidated from the cache, provides a significant performance benefit on
390  * many systems
391  */
392
393 /**
394  * struct ion_page_pool - pagepool struct
395  * @high_count:         number of highmem items in the pool
396  * @low_count:          number of lowmem items in the pool
397  * @high_items:         list of highmem items
398  * @low_items:          list of lowmem items
399  * @mutex:              lock protecting this struct and especially the count
400  *                      item list
401  * @gfp_mask:           gfp_mask to use from alloc
402  * @order:              order of pages in the pool
403  * @list:               plist node for list of pools
404  *
405  * Allows you to keep a pool of pre allocated pages to use from your heap.
406  * Keeping a pool of pages that is ready for dma, ie any cached mapping have
407  * been invalidated from the cache, provides a significant performance benefit
408  * on many systems
409  */
410 struct ion_page_pool {
411         int high_count;
412         int low_count;
413         struct list_head high_items;
414         struct list_head low_items;
415         struct mutex mutex;
416         gfp_t gfp_mask;
417         unsigned int order;
418         struct plist_node list;
419 };
420
421 struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order);
422 void ion_page_pool_destroy(struct ion_page_pool *);
423 struct page *ion_page_pool_alloc(struct ion_page_pool *);
424 void ion_page_pool_free(struct ion_page_pool *, struct page *);
425 void ion_page_pool_free_immediate(struct ion_page_pool *, struct page *);
426
427 #ifdef CONFIG_ION_POOL_CACHE_POLICY
428 static inline void ion_page_pool_alloc_set_cache_policy
429                                 (struct ion_page_pool *pool,
430                                 struct page *page){
431         void *va = page_address(page);
432
433         if (va)
434                 set_memory_wc((unsigned long)va, 1 << pool->order);
435 }
436
437 static inline void ion_page_pool_free_set_cache_policy
438                                 (struct ion_page_pool *pool,
439                                 struct page *page){
440         void *va = page_address(page);
441
442         if (va)
443                 set_memory_wb((unsigned long)va, 1 << pool->order);
444
445 }
446 #else
447 static inline void ion_page_pool_alloc_set_cache_policy
448                                 (struct ion_page_pool *pool,
449                                 struct page *page){ }
450
451 static inline void ion_page_pool_free_set_cache_policy
452                                 (struct ion_page_pool *pool,
453                                 struct page *page){ }
454 #endif
455
456
457 /** ion_page_pool_shrink - shrinks the size of the memory cached in the pool
458  * @pool:               the pool
459  * @gfp_mask:           the memory type to reclaim
460  * @nr_to_scan:         number of items to shrink in pages
461  *
462  * returns the number of items freed in pages
463  */
464 int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
465                           int nr_to_scan);
466
467 /**
468  * ion_pages_sync_for_device - cache flush pages for use with the specified
469  *                             device
470  * @dev:                the device the pages will be used with
471  * @page:               the first page to be flushed
472  * @size:               size in bytes of region to be flushed
473  * @dir:                direction of dma transfer
474  */
475 void ion_pages_sync_for_device(struct device *dev, struct page *page,
476                 size_t size, enum dma_data_direction dir);
477
478 #endif /* _ION_PRIV_H */