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