2 * drivers/gpu/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/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>
33 struct ion_buffer *ion_handle_buffer(struct ion_handle *handle);
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)
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.
49 struct ion_iommu_map {
50 unsigned long iova_addr;
53 struct ion_buffer *buffer;
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
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
89 struct list_head list;
91 struct ion_device *dev;
92 struct ion_heap *heap;
94 unsigned long private_flags;
98 ion_phys_addr_t priv_phys;
104 struct sg_table *sg_table;
106 struct list_head vmas;
107 /* used to track orphaned buffers */
109 char task_comm[TASK_COMM_LEN];
111 unsigned int iommu_map_cnt;
112 struct rb_root iommu_maps;
114 void ion_buffer_destroy(struct ion_buffer *buffer);
117 * struct ion_heap_ops - ops to operate on a given heap
118 * @allocate: allocate 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
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.
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);
159 * heap flags - flags between the heaps and core ion code
161 #define ION_HEAP_FLAG_DEFER_FREE (1 << 0)
164 * private flags - flags internal to ion
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.
172 #define ION_PRIV_FLAG_SHRINKER_FREE (1 << 0)
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
181 * @id: id of heap, also indicates priority of this heap when
182 * allocating. These are specified by platform data and
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
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.
200 struct plist_node node;
201 struct ion_device *dev;
202 enum ion_heap_type type;
203 struct ion_heap_ops *ops;
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 *);
217 * ion_buffer_cached - this ion buffer is cached
220 * indicates whether this ion buffer is cached
222 bool ion_buffer_cached(struct ion_buffer *buffer);
225 * ion_buffer_fault_user_mappings - fault in user mappings of this buffer
228 * indicates whether userspace mappings of this buffer will be faulted
229 * in, this can affect how buffers are allocated from the heap.
231 bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer);
234 * ion_device_create - allocates and returns an ion device
235 * @custom_ioctl: arch specific ioctl function if applicable
237 * returns a valid device or -PTR_ERR
239 struct ion_device *ion_device_create(long (*custom_ioctl)
240 (struct ion_client *client,
245 * ion_device_destroy - free and device and it's resource
248 void ion_device_destroy(struct ion_device *dev);
251 * ion_device_add_heap - adds a heap to the ion device
253 * @heap: the heap to add
255 void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap);
258 * some helpers for common operations on buffers using the sg_table
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);
269 * ion_heap_init_shrinker
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.
276 void ion_heap_init_shrinker(struct ion_heap *heap);
279 * ion_heap_init_deferred_free -- initialize deferred free functionality
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
286 int ion_heap_init_deferred_free(struct ion_heap *heap);
289 * ion_heap_freelist_add - add a buffer to the deferred free list
291 * @buffer: the buffer
293 * Adds an item to the deferred freelist.
295 void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer);
298 * ion_heap_freelist_drain - drain the deferred free list
300 * @size: ammount of memory to drain in bytes
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.
307 size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size);
310 * ion_heap_freelist_shrink - drain the deferred free
311 * list, skipping any heap-specific
312 * pooling or caching mechanisms
315 * @size: amount of memory to drain in bytes
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.
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
329 size_t ion_heap_freelist_shrink(struct ion_heap *heap,
333 * ion_heap_freelist_size - returns the size of the freelist in bytes
336 size_t ion_heap_freelist_size(struct ion_heap *heap);
340 * functions for creating and destroying the built in ion heaps.
341 * architectures can add their own custom architecture specific
342 * heaps as appropriate.
345 struct ion_heap *ion_heap_create(struct ion_platform_heap *);
346 void ion_heap_destroy(struct ion_heap *);
348 struct ion_heap *ion_system_heap_create(struct ion_platform_heap *);
349 void ion_system_heap_destroy(struct ion_heap *);
351 struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *);
352 void ion_system_contig_heap_destroy(struct ion_heap *);
354 struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *);
355 void ion_carveout_heap_destroy(struct ion_heap *);
357 struct ion_heap *ion_chunk_heap_create(struct ion_platform_heap *);
358 void ion_chunk_heap_destroy(struct ion_heap *);
360 struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *);
361 void ion_cma_heap_destroy(struct ion_heap *);
363 struct ion_heap *ion_drm_heap_create(struct ion_platform_heap *);
364 void ion_drm_heap_destroy(struct ion_heap *);
367 * kernel api to allocate/free from carveout -- used when carveout is
368 * used to back an architecture specific custom heap
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,
375 * The carveout heap returns physical addresses, since 0 may be a valid
376 * physical address, this is used to indicate allocation failed
378 #define ION_CARVEOUT_ALLOCATE_FAIL -1
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
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
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
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
404 struct ion_page_pool {
407 struct list_head high_items;
408 struct list_head low_items;
412 struct plist_node list;
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 *);
420 /** ion_page_pool_shrink - shrinks the size of the memory cached in the pool
422 * @gfp_mask: the memory type to reclaim
423 * @nr_to_scan: number of items to shrink in pages
425 * returns the number of items freed in pages
427 int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
431 * ion_pages_sync_for_device - cache flush pages for use with the specified
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
438 void ion_pages_sync_for_device(struct device *dev, struct page *page,
439 size_t size, enum dma_data_direction dir);
441 #endif /* _ION_PRIV_H */