ion_priv.h revision 8898227ed555b477e2989a2a9b984fa37e7a9b42
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/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>
27
28#include "ion.h"
29
30struct ion_buffer *ion_handle_buffer(struct ion_handle *handle);
31
32/**
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
41 *			a void *
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 * @dirty:		bitmask representing which pages of this buffer have
50 *			been dirtied by the cpu and need cache maintenance
51 *			before dma
52 * @vmas:		list of vma's mapping this buffer
53 * @handle_count:	count of handles referencing this buffer
54 * @task_comm:		taskcomm of last client to reference this buffer in a
55 *			handle, used for debugging
56 * @pid:		pid of last client to reference this buffer in a
57 *			handle, used for debugging
58*/
59struct ion_buffer {
60	struct kref ref;
61	struct rb_node node;
62	struct ion_device *dev;
63	struct ion_heap *heap;
64	unsigned long flags;
65	size_t size;
66	union {
67		void *priv_virt;
68		ion_phys_addr_t priv_phys;
69	};
70	struct mutex lock;
71	int kmap_cnt;
72	void *vaddr;
73	int dmap_cnt;
74	struct sg_table *sg_table;
75	unsigned long *dirty;
76	struct list_head vmas;
77	/* used to track orphaned buffers */
78	int handle_count;
79	char task_comm[TASK_COMM_LEN];
80	pid_t pid;
81};
82
83/**
84 * struct ion_heap_ops - ops to operate on a given heap
85 * @allocate:		allocate memory
86 * @free:		free memory
87 * @phys		get physical address of a buffer (only define on
88 *			physically contiguous heaps)
89 * @map_dma		map the memory for dma to a scatterlist
90 * @unmap_dma		unmap the memory for dma
91 * @map_kernel		map memory to the kernel
92 * @unmap_kernel	unmap memory to the kernel
93 * @map_user		map memory to userspace
94 */
95struct ion_heap_ops {
96	int (*allocate) (struct ion_heap *heap,
97			 struct ion_buffer *buffer, unsigned long len,
98			 unsigned long align, unsigned long flags);
99	void (*free) (struct ion_buffer *buffer);
100	int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer,
101		     ion_phys_addr_t *addr, size_t *len);
102	struct sg_table *(*map_dma) (struct ion_heap *heap,
103					struct ion_buffer *buffer);
104	void (*unmap_dma) (struct ion_heap *heap, struct ion_buffer *buffer);
105	void * (*map_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
106	void (*unmap_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
107	int (*map_user) (struct ion_heap *mapper, struct ion_buffer *buffer,
108			 struct vm_area_struct *vma);
109};
110
111/**
112 * struct ion_heap - represents a heap in the system
113 * @node:		rb node to put the heap on the device's tree of heaps
114 * @dev:		back pointer to the ion_device
115 * @type:		type of heap
116 * @ops:		ops struct as above
117 * @id:			id of heap, also indicates priority of this heap when
118 *			allocating.  These are specified by platform data and
119 *			MUST be unique
120 * @name:		used for debugging
121 * @debug_show:		called when heap debug file is read to add any
122 *			heap specific debug info to output
123 *
124 * Represents a pool of memory from which buffers can be made.  In some
125 * systems the only heap is regular system memory allocated via vmalloc.
126 * On others, some blocks might require large physically contiguous buffers
127 * that are allocated from a specially reserved heap.
128 */
129struct ion_heap {
130	struct plist_node node;
131	struct ion_device *dev;
132	enum ion_heap_type type;
133	struct ion_heap_ops *ops;
134	unsigned int id;
135	const char *name;
136	int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *);
137};
138
139/**
140 * ion_buffer_cached - this ion buffer is cached
141 * @buffer:		buffer
142 *
143 * indicates whether this ion buffer is cached
144 */
145bool ion_buffer_cached(struct ion_buffer *buffer);
146
147/**
148 * ion_buffer_fault_user_mappings - fault in user mappings of this buffer
149 * @buffer:		buffer
150 *
151 * indicates whether userspace mappings of this buffer will be faulted
152 * in, this can affect how buffers are allocated from the heap.
153 */
154bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer);
155
156/**
157 * ion_device_create - allocates and returns an ion device
158 * @custom_ioctl:	arch specific ioctl function if applicable
159 *
160 * returns a valid device or -PTR_ERR
161 */
162struct ion_device *ion_device_create(long (*custom_ioctl)
163				     (struct ion_client *client,
164				      unsigned int cmd,
165				      unsigned long arg));
166
167/**
168 * ion_device_destroy - free and device and it's resource
169 * @dev:		the device
170 */
171void ion_device_destroy(struct ion_device *dev);
172
173/**
174 * ion_device_add_heap - adds a heap to the ion device
175 * @dev:		the device
176 * @heap:		the heap to add
177 */
178void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap);
179
180/**
181 * some helpers for common operations on buffers using the sg_table
182 * and vaddr fields
183 */
184void *ion_heap_map_kernel(struct ion_heap *, struct ion_buffer *);
185void ion_heap_unmap_kernel(struct ion_heap *, struct ion_buffer *);
186int ion_heap_map_user(struct ion_heap *, struct ion_buffer *,
187			struct vm_area_struct *);
188
189
190/**
191 * functions for creating and destroying the built in ion heaps.
192 * architectures can add their own custom architecture specific
193 * heaps as appropriate.
194 */
195
196struct ion_heap *ion_heap_create(struct ion_platform_heap *);
197void ion_heap_destroy(struct ion_heap *);
198
199struct ion_heap *ion_system_heap_create(struct ion_platform_heap *);
200void ion_system_heap_destroy(struct ion_heap *);
201
202struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *);
203void ion_system_contig_heap_destroy(struct ion_heap *);
204
205struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *);
206void ion_carveout_heap_destroy(struct ion_heap *);
207/**
208 * kernel api to allocate/free from carveout -- used when carveout is
209 * used to back an architecture specific custom heap
210 */
211ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size,
212				      unsigned long align);
213void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr,
214		       unsigned long size);
215/**
216 * The carveout heap returns physical addresses, since 0 may be a valid
217 * physical address, this is used to indicate allocation failed
218 */
219#define ION_CARVEOUT_ALLOCATE_FAIL -1
220
221/**
222 * functions for creating and destroying a heap pool -- allows you
223 * to keep a pool of pre allocated memory to use from your heap.  Keeping
224 * a pool of memory that is ready for dma, ie any cached mapping have been
225 * invalidated from the cache, provides a significant peformance benefit on
226 * many systems */
227
228/**
229 * struct ion_page_pool - pagepool struct
230 * @high_count:		number of highmem items in the pool
231 * @low_count:		number of lowmem items in the pool
232 * @high_items:		list of highmem items
233 * @low_items:		list of lowmem items
234 * @shrinker:		a shrinker for the items
235 * @mutex:		lock protecting this struct and especially the count
236 *			item list
237 * @alloc:		function to be used to allocate pageory when the pool
238 *			is empty
239 * @free:		function to be used to free pageory back to the system
240 *			when the shrinker fires
241 * @gfp_mask:		gfp_mask to use from alloc
242 * @order:		order of pages in the pool
243 * @list:		plist node for list of pools
244 *
245 * Allows you to keep a pool of pre allocated pages to use from your heap.
246 * Keeping a pool of pages that is ready for dma, ie any cached mapping have
247 * been invalidated from the cache, provides a significant peformance benefit
248 * on many systems
249 */
250struct ion_page_pool {
251	int high_count;
252	int low_count;
253	struct list_head high_items;
254	struct list_head low_items;
255	struct mutex mutex;
256	void *(*alloc)(struct ion_page_pool *pool);
257	void (*free)(struct ion_page_pool *pool, struct page *page);
258	gfp_t gfp_mask;
259	unsigned int order;
260	struct plist_node list;
261};
262
263struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order);
264void ion_page_pool_destroy(struct ion_page_pool *);
265void *ion_page_pool_alloc(struct ion_page_pool *);
266void ion_page_pool_free(struct ion_page_pool *, struct page *);
267
268#endif /* _ION_PRIV_H */
269