blob: fc2e4fccf69d216d60761e67775df69f6fd5ea45 [file] [log] [blame]
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -08001/*
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
John Stultza33b2fc2014-02-04 16:08:40 -080020#include <linux/device.h>
Colin Crosse946b202013-12-13 14:25:01 -080021#include <linux/dma-direction.h>
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080022#include <linux/kref.h>
23#include <linux/mm_types.h>
24#include <linux/mutex.h>
25#include <linux/rbtree.h>
Rebecca Schultz Zavin5ad7bc32013-12-13 14:24:03 -080026#include <linux/sched.h>
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -080027#include <linux/shrinker.h>
28#include <linux/types.h>
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080029
30#include "ion.h"
31
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080032struct ion_buffer *ion_handle_buffer(struct ion_handle *handle);
33
34/**
35 * struct ion_buffer - metadata for a particular buffer
36 * @ref: refernce 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 * @size: size of the buffer
42 * @priv_virt: private data to the buffer representable as
43 * a void *
44 * @priv_phys: private data to the buffer representable as
45 * an ion_phys_addr_t (and someday a phys_addr_t)
46 * @lock: protects the buffers cnt fields
47 * @kmap_cnt: number of times the buffer is mapped to the kernel
48 * @vaddr: the kenrel mapping if kmap_cnt is not zero
49 * @dmap_cnt: number of times the buffer is mapped for dma
Rebecca Schultz Zavin4d5ca322013-12-13 14:23:37 -080050 * @sg_table: the sg table for the buffer if dmap_cnt is not zero
Rebecca Schultz Zavinc13bd1c2013-12-13 14:24:45 -080051 * @pages: flat array of pages in the buffer -- used by fault
52 * handler and only valid for buffers that are faulted in
Rebecca Schultz Zavin5ad7bc32013-12-13 14:24:03 -080053 * @vmas: list of vma's mapping this buffer
54 * @handle_count: count of handles referencing this buffer
55 * @task_comm: taskcomm of last client to reference this buffer in a
56 * handle, used for debugging
57 * @pid: pid of last client to reference this buffer in a
58 * handle, used for debugging
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080059*/
60struct ion_buffer {
61 struct kref ref;
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -080062 union {
63 struct rb_node node;
64 struct list_head list;
65 };
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080066 struct ion_device *dev;
67 struct ion_heap *heap;
68 unsigned long flags;
69 size_t size;
70 union {
71 void *priv_virt;
72 ion_phys_addr_t priv_phys;
73 };
74 struct mutex lock;
75 int kmap_cnt;
76 void *vaddr;
77 int dmap_cnt;
Rebecca Schultz Zavin4d5ca322013-12-13 14:23:37 -080078 struct sg_table *sg_table;
Rebecca Schultz Zavinc13bd1c2013-12-13 14:24:45 -080079 struct page **pages;
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -080080 struct list_head vmas;
Rebecca Schultz Zavin5ad7bc32013-12-13 14:24:03 -080081 /* used to track orphaned buffers */
82 int handle_count;
83 char task_comm[TASK_COMM_LEN];
84 pid_t pid;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080085};
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -080086void ion_buffer_destroy(struct ion_buffer *buffer);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080087
88/**
89 * struct ion_heap_ops - ops to operate on a given heap
90 * @allocate: allocate memory
91 * @free: free memory
92 * @phys get physical address of a buffer (only define on
93 * physically contiguous heaps)
94 * @map_dma map the memory for dma to a scatterlist
95 * @unmap_dma unmap the memory for dma
96 * @map_kernel map memory to the kernel
97 * @unmap_kernel unmap memory to the kernel
98 * @map_user map memory to userspace
Colin Cross9e907652013-12-13 14:24:49 -080099 *
100 * allocate, phys, and map_user return 0 on success, -errno on error.
101 * map_dma and map_kernel return pointer on success, ERR_PTR on error.
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800102 */
103struct ion_heap_ops {
104 int (*allocate) (struct ion_heap *heap,
105 struct ion_buffer *buffer, unsigned long len,
106 unsigned long align, unsigned long flags);
107 void (*free) (struct ion_buffer *buffer);
108 int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer,
109 ion_phys_addr_t *addr, size_t *len);
Rebecca Schultz Zavin4d5ca322013-12-13 14:23:37 -0800110 struct sg_table *(*map_dma) (struct ion_heap *heap,
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800111 struct ion_buffer *buffer);
112 void (*unmap_dma) (struct ion_heap *heap, struct ion_buffer *buffer);
113 void * (*map_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
114 void (*unmap_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
115 int (*map_user) (struct ion_heap *mapper, struct ion_buffer *buffer,
116 struct vm_area_struct *vma);
117};
118
119/**
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800120 * heap flags - flags between the heaps and core ion code
121 */
122#define ION_HEAP_FLAG_DEFER_FREE (1 << 0)
123
124/**
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800125 * struct ion_heap - represents a heap in the system
126 * @node: rb node to put the heap on the device's tree of heaps
127 * @dev: back pointer to the ion_device
128 * @type: type of heap
129 * @ops: ops struct as above
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800130 * @flags: flags
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800131 * @id: id of heap, also indicates priority of this heap when
132 * allocating. These are specified by platform data and
133 * MUST be unique
134 * @name: used for debugging
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800135 * @shrinker: a shrinker for the heap, if the heap caches system
136 * memory, it must define a shrinker to return it on low
137 * memory conditions, this includes system memory cached
138 * in the deferred free lists for heaps that support it
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800139 * @free_list: free list head if deferred free is used
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800140 * @free_list_size size of the deferred free list in bytes
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800141 * @lock: protects the free list
142 * @waitqueue: queue to wait on from deferred free thread
143 * @task: task struct of deferred free thread
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800144 * @debug_show: called when heap debug file is read to add any
145 * heap specific debug info to output
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800146 *
147 * Represents a pool of memory from which buffers can be made. In some
148 * systems the only heap is regular system memory allocated via vmalloc.
149 * On others, some blocks might require large physically contiguous buffers
150 * that are allocated from a specially reserved heap.
151 */
152struct ion_heap {
Rebecca Schultz Zavincd694882013-12-13 14:24:25 -0800153 struct plist_node node;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800154 struct ion_device *dev;
155 enum ion_heap_type type;
156 struct ion_heap_ops *ops;
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800157 unsigned long flags;
Rebecca Schultz Zavincd694882013-12-13 14:24:25 -0800158 unsigned int id;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800159 const char *name;
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800160 struct shrinker shrinker;
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800161 struct list_head free_list;
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800162 size_t free_list_size;
John Stultz6a72a702013-12-17 17:04:29 -0800163 spinlock_t free_lock;
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800164 wait_queue_head_t waitqueue;
165 struct task_struct *task;
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800166 int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800167};
168
169/**
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800170 * ion_buffer_cached - this ion buffer is cached
171 * @buffer: buffer
172 *
173 * indicates whether this ion buffer is cached
174 */
175bool ion_buffer_cached(struct ion_buffer *buffer);
176
177/**
Rebecca Schultz Zavin13ba7802013-12-13 14:24:06 -0800178 * ion_buffer_fault_user_mappings - fault in user mappings of this buffer
179 * @buffer: buffer
180 *
181 * indicates whether userspace mappings of this buffer will be faulted
182 * in, this can affect how buffers are allocated from the heap.
183 */
184bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer);
185
186/**
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800187 * ion_device_create - allocates and returns an ion device
188 * @custom_ioctl: arch specific ioctl function if applicable
189 *
190 * returns a valid device or -PTR_ERR
191 */
192struct ion_device *ion_device_create(long (*custom_ioctl)
193 (struct ion_client *client,
194 unsigned int cmd,
195 unsigned long arg));
196
197/**
198 * ion_device_destroy - free and device and it's resource
199 * @dev: the device
200 */
201void ion_device_destroy(struct ion_device *dev);
202
203/**
204 * ion_device_add_heap - adds a heap to the ion device
205 * @dev: the device
206 * @heap: the heap to add
207 */
208void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap);
209
210/**
Rebecca Schultz Zavin88982272013-12-13 14:24:26 -0800211 * some helpers for common operations on buffers using the sg_table
212 * and vaddr fields
213 */
214void *ion_heap_map_kernel(struct ion_heap *, struct ion_buffer *);
215void ion_heap_unmap_kernel(struct ion_heap *, struct ion_buffer *);
216int ion_heap_map_user(struct ion_heap *, struct ion_buffer *,
217 struct vm_area_struct *);
Rebecca Schultz Zavin0b6b2cd2013-12-13 14:24:32 -0800218int ion_heap_buffer_zero(struct ion_buffer *buffer);
Colin Crossdf6cf5c2013-12-13 19:26:30 -0800219int ion_heap_pages_zero(struct page *page, size_t size, pgprot_t pgprot);
Rebecca Schultz Zavin88982272013-12-13 14:24:26 -0800220
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800221/**
222 * ion_heap_init_deferred_free -- initialize deferred free functionality
223 * @heap: the heap
224 *
225 * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag this function will
226 * be called to setup deferred frees. Calls to free the buffer will
227 * return immediately and the actual free will occur some time later
228 */
229int ion_heap_init_deferred_free(struct ion_heap *heap);
230
231/**
232 * ion_heap_freelist_add - add a buffer to the deferred free list
233 * @heap: the heap
John Stultze1d855b2013-12-13 19:26:33 -0800234 * @buffer: the buffer
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800235 *
236 * Adds an item to the deferred freelist.
237 */
238void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer);
239
240/**
241 * ion_heap_freelist_drain - drain the deferred free list
242 * @heap: the heap
243 * @size: ammount of memory to drain in bytes
244 *
245 * Drains the indicated amount of memory from the deferred freelist immediately.
246 * Returns the total amount freed. The total freed may be higher depending
247 * on the size of the items in the list, or lower if there is insufficient
248 * total memory on the freelist.
249 */
250size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size);
251
252/**
253 * ion_heap_freelist_size - returns the size of the freelist in bytes
254 * @heap: the heap
255 */
256size_t ion_heap_freelist_size(struct ion_heap *heap);
257
Rebecca Schultz Zavin88982272013-12-13 14:24:26 -0800258
259/**
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800260 * functions for creating and destroying the built in ion heaps.
261 * architectures can add their own custom architecture specific
262 * heaps as appropriate.
263 */
264
265struct ion_heap *ion_heap_create(struct ion_platform_heap *);
266void ion_heap_destroy(struct ion_heap *);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800267struct ion_heap *ion_system_heap_create(struct ion_platform_heap *);
268void ion_system_heap_destroy(struct ion_heap *);
269
270struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *);
271void ion_system_contig_heap_destroy(struct ion_heap *);
272
273struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *);
274void ion_carveout_heap_destroy(struct ion_heap *);
Rebecca Schultz Zavine3c2eb72013-12-13 14:24:27 -0800275
276struct ion_heap *ion_chunk_heap_create(struct ion_platform_heap *);
277void ion_chunk_heap_destroy(struct ion_heap *);
Benjamin Gaignard349c9e12013-12-13 14:24:44 -0800278struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *);
279void ion_cma_heap_destroy(struct ion_heap *);
280
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800281/**
282 * kernel api to allocate/free from carveout -- used when carveout is
283 * used to back an architecture specific custom heap
284 */
285ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size,
286 unsigned long align);
287void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr,
288 unsigned long size);
289/**
290 * The carveout heap returns physical addresses, since 0 may be a valid
291 * physical address, this is used to indicate allocation failed
292 */
293#define ION_CARVEOUT_ALLOCATE_FAIL -1
294
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800295/**
296 * functions for creating and destroying a heap pool -- allows you
297 * to keep a pool of pre allocated memory to use from your heap. Keeping
298 * a pool of memory that is ready for dma, ie any cached mapping have been
299 * invalidated from the cache, provides a significant peformance benefit on
300 * many systems */
301
302/**
303 * struct ion_page_pool - pagepool struct
Rebecca Schultz Zavin0fb9b812013-12-13 14:24:13 -0800304 * @high_count: number of highmem items in the pool
305 * @low_count: number of lowmem items in the pool
306 * @high_items: list of highmem items
307 * @low_items: list of lowmem items
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800308 * @shrinker: a shrinker for the items
309 * @mutex: lock protecting this struct and especially the count
310 * item list
311 * @alloc: function to be used to allocate pageory when the pool
312 * is empty
313 * @free: function to be used to free pageory back to the system
314 * when the shrinker fires
315 * @gfp_mask: gfp_mask to use from alloc
316 * @order: order of pages in the pool
Rebecca Schultz Zavin797a95c2013-12-13 14:24:15 -0800317 * @list: plist node for list of pools
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800318 *
319 * Allows you to keep a pool of pre allocated pages to use from your heap.
320 * Keeping a pool of pages that is ready for dma, ie any cached mapping have
321 * been invalidated from the cache, provides a significant peformance benefit
322 * on many systems
323 */
324struct ion_page_pool {
Rebecca Schultz Zavin0fb9b812013-12-13 14:24:13 -0800325 int high_count;
326 int low_count;
327 struct list_head high_items;
328 struct list_head low_items;
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800329 struct mutex mutex;
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800330 gfp_t gfp_mask;
331 unsigned int order;
Rebecca Schultz Zavin797a95c2013-12-13 14:24:15 -0800332 struct plist_node list;
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800333};
334
335struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order);
336void ion_page_pool_destroy(struct ion_page_pool *);
337void *ion_page_pool_alloc(struct ion_page_pool *);
338void ion_page_pool_free(struct ion_page_pool *, struct page *);
339
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800340/** ion_page_pool_shrink - shrinks the size of the memory cached in the pool
341 * @pool: the pool
342 * @gfp_mask: the memory type to reclaim
343 * @nr_to_scan: number of items to shrink in pages
344 *
345 * returns the number of items freed in pages
346 */
347int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
348 int nr_to_scan);
349
Colin Crosse946b202013-12-13 14:25:01 -0800350/**
351 * ion_pages_sync_for_device - cache flush pages for use with the specified
352 * device
353 * @dev: the device the pages will be used with
354 * @page: the first page to be flushed
355 * @size: size in bytes of region to be flushed
356 * @dir: direction of dma transfer
357 */
358void ion_pages_sync_for_device(struct device *dev, struct page *page,
359 size_t size, enum dma_data_direction dir);
360
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800361#endif /* _ION_PRIV_H */