blob: 2b00ee651d4d69f0d40548b6d7262397073b707c [file] [log] [blame]
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07001/*
2 * drivers/gpu/ion/ion_priv.h
3 *
4 * Copyright (C) 2011 Google, Inc.
Laura Abbotta8c373f2013-02-15 09:25:35 -08005 * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -07006 *
7 * This software is licensed under the terms of the GNU General Public
8 * License version 2, as published by the Free Software Foundation, and
9 * may be copied, distributed, and modified under those terms.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#ifndef _ION_PRIV_H
19#define _ION_PRIV_H
20
Rebecca Schultz Zavin050372e2012-06-07 16:36:44 -070021#include <linux/ion.h>
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070022#include <linux/kref.h>
23#include <linux/mm_types.h>
24#include <linux/mutex.h>
25#include <linux/rbtree.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070026#include <linux/seq_file.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070027
Laura Abbott61e89592013-03-21 10:55:17 -070028#include "msm_ion_priv.h"
Rebecca Schultz Zavin050372e2012-06-07 16:36:44 -070029#include <linux/sched.h>
30#include <linux/shrinker.h>
31#include <linux/types.h>
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070032
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070033struct ion_buffer *ion_handle_buffer(struct ion_handle *handle);
34
35/**
36 * struct ion_buffer - metadata for a particular buffer
37 * @ref: refernce count
38 * @node: node in the ion_device buffers tree
39 * @dev: back pointer to the ion_device
40 * @heap: back pointer to the heap the buffer came from
41 * @flags: buffer specific flags
42 * @size: size of the buffer
43 * @priv_virt: private data to the buffer representable as
44 * a void *
45 * @priv_phys: private data to the buffer representable as
46 * an ion_phys_addr_t (and someday a phys_addr_t)
47 * @lock: protects the buffers cnt fields
48 * @kmap_cnt: number of times the buffer is mapped to the kernel
49 * @vaddr: the kenrel mapping if kmap_cnt is not zero
50 * @dmap_cnt: number of times the buffer is mapped for dma
Laura Abbottb14ed962012-01-30 14:18:08 -080051 * @sg_table: the sg table for the buffer if dmap_cnt is not zero
Rebecca Schultz Zavindb70ae62012-08-28 17:27:22 -070052 * @dirty: bitmask representing which pages of this buffer have
53 * been dirtied by the cpu and need cache maintenance
54 * before dma
55 * @vmas: list of vma's mapping this buffer
56 * @handle_count: count of handles referencing this buffer
57 * @task_comm: taskcomm of last client to reference this buffer in a
58 * handle, used for debugging
59 * @pid: pid of last client to reference this buffer in a
60 * handle, used for debugging
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070061*/
62struct ion_buffer {
63 struct kref ref;
Rebecca Schultz Zavin618d6be2013-02-13 14:48:11 -080064 union {
65 struct rb_node node;
66 struct list_head list;
67 };
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070068 struct ion_device *dev;
69 struct ion_heap *heap;
70 unsigned long flags;
71 size_t size;
72 union {
73 void *priv_virt;
74 ion_phys_addr_t priv_phys;
75 };
76 struct mutex lock;
77 int kmap_cnt;
78 void *vaddr;
79 int dmap_cnt;
Laura Abbottb14ed962012-01-30 14:18:08 -080080 struct sg_table *sg_table;
Rebecca Schultz Zavinb1790672012-06-14 15:08:53 -070081 unsigned long *dirty;
82 struct list_head vmas;
Rebecca Schultz Zavindb70ae62012-08-28 17:27:22 -070083 /* used to track orphaned buffers */
84 int handle_count;
85 char task_comm[TASK_COMM_LEN];
86 pid_t pid;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070087};
Rebecca Schultz Zavin83ff5da2013-05-23 13:37:25 -070088void ion_buffer_destroy(struct ion_buffer *buffer);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070089
90/**
91 * struct ion_heap_ops - ops to operate on a given heap
92 * @allocate: allocate memory
Mitchel Humpherys58607432013-08-06 15:08:23 -070093 * @free: free memory. Will be called with
94 * ION_FLAG_FREED_FROM_SHRINKER set in buffer flags when
95 * called from a shrinker. In that case, the pages being
96 * free'd must be truly free'd back to the system, not put
97 * in a page pool or otherwise cached.
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070098 * @phys get physical address of a buffer (only define on
99 * physically contiguous heaps)
100 * @map_dma map the memory for dma to a scatterlist
101 * @unmap_dma unmap the memory for dma
102 * @map_kernel map memory to the kernel
103 * @unmap_kernel unmap memory to the kernel
104 * @map_user map memory to userspace
Alex Bird8a3ede32011-11-07 12:33:42 -0800105 * @unmap_user unmap memory to userspace
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700106 */
107struct ion_heap_ops {
108 int (*allocate) (struct ion_heap *heap,
109 struct ion_buffer *buffer, unsigned long len,
110 unsigned long align, unsigned long flags);
111 void (*free) (struct ion_buffer *buffer);
112 int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer,
113 ion_phys_addr_t *addr, size_t *len);
Laura Abbottb14ed962012-01-30 14:18:08 -0800114 struct sg_table *(*map_dma) (struct ion_heap *heap,
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700115 struct ion_buffer *buffer);
116 void (*unmap_dma) (struct ion_heap *heap, struct ion_buffer *buffer);
Laura Abbottb14ed962012-01-30 14:18:08 -0800117 void * (*map_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700118 void (*unmap_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
119 int (*map_user) (struct ion_heap *mapper, struct ion_buffer *buffer,
Laura Abbottb14ed962012-01-30 14:18:08 -0800120 struct vm_area_struct *vma);
Alex Bird8a3ede32011-11-07 12:33:42 -0800121 void (*unmap_user) (struct ion_heap *mapper, struct ion_buffer *buffer);
Olav Haugan0671b9a2012-05-25 11:58:56 -0700122 int (*print_debug)(struct ion_heap *heap, struct seq_file *s,
Mitchel Humpherysee0aa9c2013-11-15 22:56:04 -0800123 const struct list_head *mem_map);
Laura Abbott7e446482012-06-13 15:59:39 -0700124 int (*secure_heap)(struct ion_heap *heap, int version, void *data);
125 int (*unsecure_heap)(struct ion_heap *heap, int version, void *data);
Laura Abbott93619302012-10-11 11:51:40 -0700126 int (*secure_buffer)(struct ion_buffer *buffer, int version,
127 void *data, int flags);
128 int (*unsecure_buffer)(struct ion_buffer *buffer, int force_unsecure);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700129};
130
131/**
Rebecca Schultz Zavin618d6be2013-02-13 14:48:11 -0800132 * heap flags - flags between the heaps and core ion code
133 */
134#define ION_HEAP_FLAG_DEFER_FREE (1 << 0)
135
136/**
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700137 * struct ion_heap - represents a heap in the system
138 * @node: rb node to put the heap on the device's tree of heaps
139 * @dev: back pointer to the ion_device
140 * @type: type of heap
141 * @ops: ops struct as above
Rebecca Schultz Zavin618d6be2013-02-13 14:48:11 -0800142 * @flags: flags
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700143 * @id: id of heap, also indicates priority of this heap when
144 * allocating. These are specified by platform data and
145 * MUST be unique
146 * @name: used for debugging
Rebecca Schultz Zavin83ff5da2013-05-23 13:37:25 -0700147 * @shrinker: a shrinker for the heap, if the heap caches system
148 * memory, it must define a shrinker to return it on low
149 * memory conditions, this includes system memory cached
150 * in the deferred free lists for heaps that support it
Benjamin Gaignard8dff0a62012-06-25 15:30:18 -0700151 * @priv: private heap data
Rebecca Schultz Zavin618d6be2013-02-13 14:48:11 -0800152 * @free_list: free list head if deferred free is used
Rebecca Schultz Zavin83ff5da2013-05-23 13:37:25 -0700153 * @free_list_size size of the deferred free list in bytes
Rebecca Schultz Zavin618d6be2013-02-13 14:48:11 -0800154 * @lock: protects the free list
155 * @waitqueue: queue to wait on from deferred free thread
156 * @task: task struct of deferred free thread
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700157 * @debug_show: called when heap debug file is read to add any
158 * heap specific debug info to output
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700159 *
160 * Represents a pool of memory from which buffers can be made. In some
161 * systems the only heap is regular system memory allocated via vmalloc.
162 * On others, some blocks might require large physically contiguous buffers
163 * that are allocated from a specially reserved heap.
164 */
165struct ion_heap {
Rebecca Schultz Zavin47b98882012-11-15 10:36:10 -0800166 struct plist_node node;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700167 struct ion_device *dev;
168 enum ion_heap_type type;
169 struct ion_heap_ops *ops;
Rebecca Schultz Zavin618d6be2013-02-13 14:48:11 -0800170 unsigned long flags;
Rebecca Schultz Zavin47b98882012-11-15 10:36:10 -0800171 unsigned int id;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700172 const char *name;
Rebecca Schultz Zavin83ff5da2013-05-23 13:37:25 -0700173 struct shrinker shrinker;
Benjamin Gaignard8dff0a62012-06-25 15:30:18 -0700174 void *priv;
Rebecca Schultz Zavin618d6be2013-02-13 14:48:11 -0800175 struct list_head free_list;
Rebecca Schultz Zavin83ff5da2013-05-23 13:37:25 -0700176 size_t free_list_size;
Rebecca Schultz Zavin618d6be2013-02-13 14:48:11 -0800177 struct rt_mutex lock;
178 wait_queue_head_t waitqueue;
179 struct task_struct *task;
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700180 int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700181};
182
183/**
Rebecca Schultz Zavin943facc2012-08-06 21:37:23 -0700184 * ion_buffer_cached - this ion buffer is cached
185 * @buffer: buffer
186 *
187 * indicates whether this ion buffer is cached
188 */
189bool ion_buffer_cached(struct ion_buffer *buffer);
190
191/**
Rebecca Schultz Zavinf858ba42012-09-21 11:46:06 -0700192 * ion_buffer_fault_user_mappings - fault in user mappings of this buffer
193 * @buffer: buffer
194 *
195 * indicates whether userspace mappings of this buffer will be faulted
196 * in, this can affect how buffers are allocated from the heap.
197 */
198bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer);
199
200/**
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700201 * ion_device_create - allocates and returns an ion device
202 * @custom_ioctl: arch specific ioctl function if applicable
203 *
204 * returns a valid device or -PTR_ERR
205 */
206struct ion_device *ion_device_create(long (*custom_ioctl)
207 (struct ion_client *client,
208 unsigned int cmd,
209 unsigned long arg));
210
211/**
212 * ion_device_destroy - free and device and it's resource
213 * @dev: the device
214 */
215void ion_device_destroy(struct ion_device *dev);
216
217/**
218 * ion_device_add_heap - adds a heap to the ion device
219 * @dev: the device
220 * @heap: the heap to add
221 */
222void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap);
223
Mitchel Humpherys2bf68fc2013-10-03 17:51:49 -0700224struct pages_mem {
225 struct page **pages;
226 void (*free_fn) (const void *);
227};
228
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700229/**
Rebecca Schultz Zavin3df181c2012-11-15 10:43:46 -0800230 * some helpers for common operations on buffers using the sg_table
231 * and vaddr fields
232 */
233void *ion_heap_map_kernel(struct ion_heap *, struct ion_buffer *);
234void ion_heap_unmap_kernel(struct ion_heap *, struct ion_buffer *);
235int ion_heap_map_user(struct ion_heap *, struct ion_buffer *,
236 struct vm_area_struct *);
Mitchel Humpherys2bf68fc2013-10-03 17:51:49 -0700237int ion_heap_pages_zero(struct page **pages, int num_pages,
238 bool should_invalidate);
Rebecca Schultz Zavinca12f5d2013-01-09 11:26:37 -0800239int ion_heap_buffer_zero(struct ion_buffer *buffer);
Mitchel Humpherys2bf68fc2013-10-03 17:51:49 -0700240int ion_heap_high_order_page_zero(struct page *page,
241 int order, bool should_invalidate);
Rebecca Schultz Zavin3df181c2012-11-15 10:43:46 -0800242
Rebecca Schultz Zavin83ff5da2013-05-23 13:37:25 -0700243/**
244 * ion_heap_init_deferred_free -- initialize deferred free functionality
245 * @heap: the heap
246 *
247 * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag this function will
248 * be called to setup deferred frees. Calls to free the buffer will
249 * return immediately and the actual free will occur some time later
250 */
251int ion_heap_init_deferred_free(struct ion_heap *heap);
252
253/**
254 * ion_heap_freelist_add - add a buffer to the deferred free list
255 * @heap: the heap
256 * @buffer: the buffer
257 *
258 * Adds an item to the deferred freelist.
259 */
260void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer);
261
262/**
263 * ion_heap_freelist_drain - drain the deferred free list
264 * @heap: the heap
265 * @size: ammount of memory to drain in bytes
266 *
267 * Drains the indicated amount of memory from the deferred freelist immediately.
268 * Returns the total amount freed. The total freed may be higher depending
269 * on the size of the items in the list, or lower if there is insufficient
270 * total memory on the freelist.
271 */
272size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size);
273
274/**
Mitchel Humpherys58607432013-08-06 15:08:23 -0700275 * ion_heap_freelist_drain_from_shrinker - drain the deferred free
276 * list, skipping any heap-specific
277 * pooling or caching mechanisms
278 *
279 * @heap: the heap
280 * @size: amount of memory to drain in bytes
281 *
282 * Drains the indicated amount of memory from the deferred freelist immediately.
283 * Returns the total amount freed. The total freed may be higher depending
284 * on the size of the items in the list, or lower if there is insufficient
285 * total memory on the freelist.
286 *
287 * Unlike with @ion_heap_freelist_drain, don't put any pages back into
288 * page pools or otherwise cache the pages. Everything must be
289 * genuinely free'd back to the system. If you're free'ing from a
290 * shrinker you probably want to use this. Note that this relies on
291 * the heap.ops.free callback honoring the
292 * ION_FLAG_FREED_FROM_SHRINKER flag.
293 */
294size_t ion_heap_freelist_drain_from_shrinker(struct ion_heap *heap,
295 size_t size);
296
297/**
Rebecca Schultz Zavin83ff5da2013-05-23 13:37:25 -0700298 * ion_heap_freelist_size - returns the size of the freelist in bytes
299 * @heap: the heap
300 */
301size_t ion_heap_freelist_size(struct ion_heap *heap);
302
Rebecca Schultz Zavin3df181c2012-11-15 10:43:46 -0800303
304/**
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700305 * functions for creating and destroying the built in ion heaps.
306 * architectures can add their own custom architecture specific
307 * heaps as appropriate.
308 */
309
310struct ion_heap *ion_heap_create(struct ion_platform_heap *);
311void ion_heap_destroy(struct ion_heap *);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700312struct ion_heap *ion_system_heap_create(struct ion_platform_heap *);
313void ion_system_heap_destroy(struct ion_heap *);
314
315struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *);
316void ion_system_contig_heap_destroy(struct ion_heap *);
317
318struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *);
319void ion_carveout_heap_destroy(struct ion_heap *);
Laura Abbott8c017362011-09-22 20:59:12 -0700320
Rebecca Schultz Zavind2ce6f82012-11-15 10:52:45 -0800321struct ion_heap *ion_chunk_heap_create(struct ion_platform_heap *);
322void ion_chunk_heap_destroy(struct ion_heap *);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700323/**
324 * kernel api to allocate/free from carveout -- used when carveout is
325 * used to back an architecture specific custom heap
326 */
327ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size,
328 unsigned long align);
329void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr,
330 unsigned long size);
331/**
Laura Abbott61e89592013-03-21 10:55:17 -0700332 * The carveout heap returns physical addresses, since 0 may be a valid
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700333 * physical address, this is used to indicate allocation failed
334 */
335#define ION_CARVEOUT_ALLOCATE_FAIL -1
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700336
Rebecca Schultz Zavin050372e2012-06-07 16:36:44 -0700337/**
338 * functions for creating and destroying a heap pool -- allows you
339 * to keep a pool of pre allocated memory to use from your heap. Keeping
340 * a pool of memory that is ready for dma, ie any cached mapping have been
341 * invalidated from the cache, provides a significant peformance benefit on
342 * many systems */
343
344/**
345 * struct ion_page_pool - pagepool struct
Rebecca Schultz Zavin9fad2fe2012-10-08 23:01:23 -0700346 * @high_count: number of highmem items in the pool
347 * @low_count: number of lowmem items in the pool
348 * @high_items: list of highmem items
349 * @low_items: list of lowmem items
Rebecca Schultz Zavin050372e2012-06-07 16:36:44 -0700350 * @shrinker: a shrinker for the items
351 * @mutex: lock protecting this struct and especially the count
352 * item list
353 * @alloc: function to be used to allocate pageory when the pool
354 * is empty
355 * @free: function to be used to free pageory back to the system
356 * when the shrinker fires
357 * @gfp_mask: gfp_mask to use from alloc
358 * @order: order of pages in the pool
Rebecca Schultz Zavin8afce332012-10-10 14:19:17 -0700359 * @list: plist node for list of pools
Mitchel Humpherys2bf68fc2013-10-03 17:51:49 -0700360 * @should_invalidate: whether or not the cache needs to be invalidated at
361 * page allocation time.
Rebecca Schultz Zavin050372e2012-06-07 16:36:44 -0700362 *
363 * Allows you to keep a pool of pre allocated pages to use from your heap.
364 * Keeping a pool of pages that is ready for dma, ie any cached mapping have
365 * been invalidated from the cache, provides a significant peformance benefit
366 * on many systems
367 */
368struct ion_page_pool {
Rebecca Schultz Zavin9fad2fe2012-10-08 23:01:23 -0700369 int high_count;
370 int low_count;
371 struct list_head high_items;
372 struct list_head low_items;
Rebecca Schultz Zavin050372e2012-06-07 16:36:44 -0700373 struct mutex mutex;
Rebecca Schultz Zavin050372e2012-06-07 16:36:44 -0700374 gfp_t gfp_mask;
375 unsigned int order;
Rebecca Schultz Zavin8afce332012-10-10 14:19:17 -0700376 struct plist_node list;
Mitchel Humpherys2bf68fc2013-10-03 17:51:49 -0700377 bool should_invalidate;
Rebecca Schultz Zavin050372e2012-06-07 16:36:44 -0700378};
379
Mitchel Humpherys2bf68fc2013-10-03 17:51:49 -0700380struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order,
381 bool should_invalidate);
Rebecca Schultz Zavin050372e2012-06-07 16:36:44 -0700382void ion_page_pool_destroy(struct ion_page_pool *);
383void *ion_page_pool_alloc(struct ion_page_pool *);
384void ion_page_pool_free(struct ion_page_pool *, struct page *);
385
Rebecca Schultz Zavin83ff5da2013-05-23 13:37:25 -0700386/** ion_page_pool_shrink - shrinks the size of the memory cached in the pool
387 * @pool: the pool
388 * @gfp_mask: the memory type to reclaim
389 * @nr_to_scan: number of items to shrink in pages
390 *
391 * returns the number of items freed in pages
392 */
393int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
394 int nr_to_scan);
395
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700396#endif /* _ION_PRIV_H */