2 * drivers/staging/android/ion/ion_priv.h
4 * Copyright (C) 2011 Google, Inc.
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.
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.
20 #include <linux/kref.h>
21 #include <linux/mm_types.h>
22 #include <linux/mutex.h>
23 #include <linux/rbtree.h>
24 #include <linux/sched.h>
25 #include <linux/shrinker.h>
26 #include <linux/types.h>
30 struct ion_buffer
*ion_handle_buffer(struct ion_handle
*handle
);
33 * struct ion_buffer - metadata for a particular buffer
34 * @ref: refernce count
35 * @node: node in the ion_device buffers tree
36 * @dev: back pointer to the ion_device
37 * @heap: back pointer to the heap the buffer came from
38 * @flags: buffer specific flags
39 * @size: size of the buffer
40 * @priv_virt: private data to the buffer representable as
42 * @priv_phys: private data to the buffer representable as
43 * an ion_phys_addr_t (and someday a phys_addr_t)
44 * @lock: protects the buffers cnt fields
45 * @kmap_cnt: number of times the buffer is mapped to the kernel
46 * @vaddr: the kenrel mapping if kmap_cnt is not zero
47 * @dmap_cnt: number of times the buffer is mapped for dma
48 * @sg_table: the sg table for the buffer if dmap_cnt is not zero
49 * @pages: flat array of pages in the buffer -- used by fault
50 * handler and only valid for buffers that are faulted in
51 * @vmas: list of vma's mapping this buffer
52 * @handle_count: count of handles referencing this buffer
53 * @task_comm: taskcomm of last client to reference this buffer in a
54 * handle, used for debugging
55 * @pid: pid of last client to reference this buffer in a
56 * handle, used for debugging
62 struct list_head list
;
64 struct ion_device
*dev
;
65 struct ion_heap
*heap
;
70 ion_phys_addr_t priv_phys
;
76 struct sg_table
*sg_table
;
78 struct list_head vmas
;
79 /* used to track orphaned buffers */
81 char task_comm
[TASK_COMM_LEN
];
84 void ion_buffer_destroy(struct ion_buffer
*buffer
);
87 * struct ion_heap_ops - ops to operate on a given heap
88 * @allocate: allocate memory
90 * @phys get physical address of a buffer (only define on
91 * physically contiguous heaps)
92 * @map_dma map the memory for dma to a scatterlist
93 * @unmap_dma unmap the memory for dma
94 * @map_kernel map memory to the kernel
95 * @unmap_kernel unmap memory to the kernel
96 * @map_user map memory to userspace
99 int (*allocate
) (struct ion_heap
*heap
,
100 struct ion_buffer
*buffer
, unsigned long len
,
101 unsigned long align
, unsigned long flags
);
102 void (*free
) (struct ion_buffer
*buffer
);
103 int (*phys
) (struct ion_heap
*heap
, struct ion_buffer
*buffer
,
104 ion_phys_addr_t
*addr
, size_t *len
);
105 struct sg_table
*(*map_dma
) (struct ion_heap
*heap
,
106 struct ion_buffer
*buffer
);
107 void (*unmap_dma
) (struct ion_heap
*heap
, struct ion_buffer
*buffer
);
108 void * (*map_kernel
) (struct ion_heap
*heap
, struct ion_buffer
*buffer
);
109 void (*unmap_kernel
) (struct ion_heap
*heap
, struct ion_buffer
*buffer
);
110 int (*map_user
) (struct ion_heap
*mapper
, struct ion_buffer
*buffer
,
111 struct vm_area_struct
*vma
);
115 * heap flags - flags between the heaps and core ion code
117 #define ION_HEAP_FLAG_DEFER_FREE (1 << 0)
120 * struct ion_heap - represents a heap in the system
121 * @node: rb node to put the heap on the device's tree of heaps
122 * @dev: back pointer to the ion_device
123 * @type: type of heap
124 * @ops: ops struct as above
126 * @id: id of heap, also indicates priority of this heap when
127 * allocating. These are specified by platform data and
129 * @name: used for debugging
130 * @shrinker: a shrinker for the heap, if the heap caches system
131 * memory, it must define a shrinker to return it on low
132 * memory conditions, this includes system memory cached
133 * in the deferred free lists for heaps that support it
134 * @free_list: free list head if deferred free is used
135 * @free_list_size size of the deferred free list in bytes
136 * @lock: protects the free list
137 * @waitqueue: queue to wait on from deferred free thread
138 * @task: task struct of deferred free thread
139 * @debug_show: called when heap debug file is read to add any
140 * heap specific debug info to output
142 * Represents a pool of memory from which buffers can be made. In some
143 * systems the only heap is regular system memory allocated via vmalloc.
144 * On others, some blocks might require large physically contiguous buffers
145 * that are allocated from a specially reserved heap.
148 struct plist_node node
;
149 struct ion_device
*dev
;
150 enum ion_heap_type type
;
151 struct ion_heap_ops
*ops
;
155 struct shrinker shrinker
;
156 struct list_head free_list
;
157 size_t free_list_size
;
158 struct rt_mutex lock
;
159 wait_queue_head_t waitqueue
;
160 struct task_struct
*task
;
161 int (*debug_show
)(struct ion_heap
*heap
, struct seq_file
*, void *);
165 * ion_buffer_cached - this ion buffer is cached
168 * indicates whether this ion buffer is cached
170 bool ion_buffer_cached(struct ion_buffer
*buffer
);
173 * ion_buffer_fault_user_mappings - fault in user mappings of this buffer
176 * indicates whether userspace mappings of this buffer will be faulted
177 * in, this can affect how buffers are allocated from the heap.
179 bool ion_buffer_fault_user_mappings(struct ion_buffer
*buffer
);
182 * ion_device_create - allocates and returns an ion device
183 * @custom_ioctl: arch specific ioctl function if applicable
185 * returns a valid device or -PTR_ERR
187 struct ion_device
*ion_device_create(long (*custom_ioctl
)
188 (struct ion_client
*client
,
193 * ion_device_destroy - free and device and it's resource
196 void ion_device_destroy(struct ion_device
*dev
);
199 * ion_device_add_heap - adds a heap to the ion device
201 * @heap: the heap to add
203 void ion_device_add_heap(struct ion_device
*dev
, struct ion_heap
*heap
);
206 * some helpers for common operations on buffers using the sg_table
209 void *ion_heap_map_kernel(struct ion_heap
*, struct ion_buffer
*);
210 void ion_heap_unmap_kernel(struct ion_heap
*, struct ion_buffer
*);
211 int ion_heap_map_user(struct ion_heap
*, struct ion_buffer
*,
212 struct vm_area_struct
*);
213 int ion_heap_buffer_zero(struct ion_buffer
*buffer
);
216 * ion_heap_alloc_pages - allocate pages from alloc_pages
217 * @buffer: the buffer to allocate for, used to extract the flags
218 * @gfp_flags: the gfp_t for the allocation
219 * @order: the order of the allocatoin
221 * This funciton allocations from alloc pages and also does any other
222 * necessary operations based on the buffer->flags. For buffers which
223 * will be faulted in the pages are split using split_page
225 struct page
*ion_heap_alloc_pages(struct ion_buffer
*buffer
, gfp_t gfp_flags
,
229 * ion_heap_init_deferred_free -- initialize deferred free functionality
232 * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag this function will
233 * be called to setup deferred frees. Calls to free the buffer will
234 * return immediately and the actual free will occur some time later
236 int ion_heap_init_deferred_free(struct ion_heap
*heap
);
239 * ion_heap_freelist_add - add a buffer to the deferred free list
241 * @buffer: the buffer
243 * Adds an item to the deferred freelist.
245 void ion_heap_freelist_add(struct ion_heap
*heap
, struct ion_buffer
*buffer
);
248 * ion_heap_freelist_drain - drain the deferred free list
250 * @size: ammount of memory to drain in bytes
252 * Drains the indicated amount of memory from the deferred freelist immediately.
253 * Returns the total amount freed. The total freed may be higher depending
254 * on the size of the items in the list, or lower if there is insufficient
255 * total memory on the freelist.
257 size_t ion_heap_freelist_drain(struct ion_heap
*heap
, size_t size
);
260 * ion_heap_freelist_size - returns the size of the freelist in bytes
263 size_t ion_heap_freelist_size(struct ion_heap
*heap
);
267 * functions for creating and destroying the built in ion heaps.
268 * architectures can add their own custom architecture specific
269 * heaps as appropriate.
272 struct ion_heap
*ion_heap_create(struct ion_platform_heap
*);
273 void ion_heap_destroy(struct ion_heap
*);
274 struct ion_heap
*ion_system_heap_create(struct ion_platform_heap
*);
275 void ion_system_heap_destroy(struct ion_heap
*);
277 struct ion_heap
*ion_system_contig_heap_create(struct ion_platform_heap
*);
278 void ion_system_contig_heap_destroy(struct ion_heap
*);
280 struct ion_heap
*ion_carveout_heap_create(struct ion_platform_heap
*);
281 void ion_carveout_heap_destroy(struct ion_heap
*);
283 struct ion_heap
*ion_chunk_heap_create(struct ion_platform_heap
*);
284 void ion_chunk_heap_destroy(struct ion_heap
*);
285 struct ion_heap
*ion_cma_heap_create(struct ion_platform_heap
*);
286 void ion_cma_heap_destroy(struct ion_heap
*);
289 * kernel api to allocate/free from carveout -- used when carveout is
290 * used to back an architecture specific custom heap
292 ion_phys_addr_t
ion_carveout_allocate(struct ion_heap
*heap
, unsigned long size
,
293 unsigned long align
);
294 void ion_carveout_free(struct ion_heap
*heap
, ion_phys_addr_t addr
,
297 * The carveout heap returns physical addresses, since 0 may be a valid
298 * physical address, this is used to indicate allocation failed
300 #define ION_CARVEOUT_ALLOCATE_FAIL -1
303 * functions for creating and destroying a heap pool -- allows you
304 * to keep a pool of pre allocated memory to use from your heap. Keeping
305 * a pool of memory that is ready for dma, ie any cached mapping have been
306 * invalidated from the cache, provides a significant peformance benefit on
310 * struct ion_page_pool - pagepool struct
311 * @high_count: number of highmem items in the pool
312 * @low_count: number of lowmem items in the pool
313 * @high_items: list of highmem items
314 * @low_items: list of lowmem items
315 * @shrinker: a shrinker for the items
316 * @mutex: lock protecting this struct and especially the count
318 * @alloc: function to be used to allocate pageory when the pool
320 * @free: function to be used to free pageory back to the system
321 * when the shrinker fires
322 * @gfp_mask: gfp_mask to use from alloc
323 * @order: order of pages in the pool
324 * @list: plist node for list of pools
326 * Allows you to keep a pool of pre allocated pages to use from your heap.
327 * Keeping a pool of pages that is ready for dma, ie any cached mapping have
328 * been invalidated from the cache, provides a significant peformance benefit
331 struct ion_page_pool
{
334 struct list_head high_items
;
335 struct list_head low_items
;
339 struct plist_node list
;
342 struct ion_page_pool
*ion_page_pool_create(gfp_t gfp_mask
, unsigned int order
);
343 void ion_page_pool_destroy(struct ion_page_pool
*);
344 void *ion_page_pool_alloc(struct ion_page_pool
*);
345 void ion_page_pool_free(struct ion_page_pool
*, struct page
*);
347 /** ion_page_pool_shrink - shrinks the size of the memory cached in the pool
349 * @gfp_mask: the memory type to reclaim
350 * @nr_to_scan: number of items to shrink in pages
352 * returns the number of items freed in pages
354 int ion_page_pool_shrink(struct ion_page_pool
*pool
, gfp_t gfp_mask
,
357 #endif /* _ION_PRIV_H */