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