blob: bb119cc2e5672fd00507c2374758cd64a0b6bd8a [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.
Sudarshan Rajagopalan33ae0432017-05-18 00:12:53 -07005 * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved.
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -08006 *
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
John Stultza33b2fc2014-02-04 16:08:40 -080021#include <linux/device.h>
Colin Crosse946b202013-12-13 14:25:01 -080022#include <linux/dma-direction.h>
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080023#include <linux/kref.h>
24#include <linux/mm_types.h>
25#include <linux/mutex.h>
26#include <linux/rbtree.h>
Patrick Dalyeeeb9402016-11-01 20:54:41 -070027#include <linux/seq_file.h>
28
Rebecca Schultz Zavin5ad7bc32013-12-13 14:24:03 -080029#include <linux/sched.h>
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -080030#include <linux/shrinker.h>
31#include <linux/types.h>
Patrick Daly7e8cbb42016-11-01 18:37:42 -070032#ifdef CONFIG_ION_POOL_CACHE_POLICY
33#include <asm/cacheflush.h>
34#endif
Patrick Dalyeeeb9402016-11-01 20:54:41 -070035#include <linux/device.h>
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080036
37#include "ion.h"
38
Patrick Daly7e8cbb42016-11-01 18:37:42 -070039struct ion_buffer *ion_handle_buffer(struct ion_handle *handle);
40
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080041/**
Patrick Dalyeeeb9402016-11-01 20:54:41 -070042 * struct mem_map_data - represents information about the memory map for a heap
43 * @node: list node used to store in the list of mem_map_data
44 * @addr: start address of memory region.
45 * @addr: end address of memory region.
46 * @size: size of memory region
47 * @client_name: name of the client who owns this buffer.
48 *
49 */
50struct mem_map_data {
51 struct list_head node;
52 ion_phys_addr_t addr;
53 ion_phys_addr_t addr_end;
54 unsigned long size;
55 const char *client_name;
56};
57
58/**
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080059 * struct ion_buffer - metadata for a particular buffer
Carlos E. Garcia69e98df2015-04-24 09:40:42 -040060 * @ref: reference count
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080061 * @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
Mitchel Humpherys53a91c62014-02-17 13:58:39 -080065 * @private_flags: internal buffer specific flags
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080066 * @size: size of the buffer
67 * @priv_virt: private data to the buffer representable as
68 * a void *
Patrick Daly7e8cbb42016-11-01 18:37:42 -070069 * @priv_phys: private data to the buffer representable as
70 * an ion_phys_addr_t (and someday a phys_addr_t)
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080071 * @lock: protects the buffers cnt fields
72 * @kmap_cnt: number of times the buffer is mapped to the kernel
Laura Abbott29defcc2014-08-01 16:13:40 -070073 * @vaddr: the kenrel mapping if kmap_cnt is not zero
74 * @sg_table: the sg table for the buffer. Note that if you need
75 * an sg_table for this buffer, you should likely be
76 * using Ion as a DMA Buf exporter and using
77 * dma_buf_map_attachment rather than trying to use this
78 * field directly.
Rebecca Schultz Zavinc13bd1c2013-12-13 14:24:45 -080079 * @pages: flat array of pages in the buffer -- used by fault
80 * handler and only valid for buffers that are faulted in
Rebecca Schultz Zavin5ad7bc32013-12-13 14:24:03 -080081 * @vmas: list of vma's mapping this buffer
82 * @handle_count: count of handles referencing this buffer
83 * @task_comm: taskcomm of last client to reference this buffer in a
84 * handle, used for debugging
85 * @pid: pid of last client to reference this buffer in a
86 * handle, used for debugging
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080087*/
88struct ion_buffer {
89 struct kref ref;
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -080090 union {
91 struct rb_node node;
92 struct list_head list;
93 };
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080094 struct ion_device *dev;
95 struct ion_heap *heap;
96 unsigned long flags;
Mitchel Humpherys53a91c62014-02-17 13:58:39 -080097 unsigned long private_flags;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080098 size_t size;
Patrick Daly7e8cbb42016-11-01 18:37:42 -070099 union {
100 void *priv_virt;
101 ion_phys_addr_t priv_phys;
102 };
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800103 struct mutex lock;
104 int kmap_cnt;
105 void *vaddr;
Rebecca Schultz Zavin4d5ca322013-12-13 14:23:37 -0800106 struct sg_table *sg_table;
Rebecca Schultz Zavinc13bd1c2013-12-13 14:24:45 -0800107 struct page **pages;
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -0800108 struct list_head vmas;
Rebecca Schultz Zavin5ad7bc32013-12-13 14:24:03 -0800109 /* used to track orphaned buffers */
110 int handle_count;
111 char task_comm[TASK_COMM_LEN];
112 pid_t pid;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800113};
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800114void ion_buffer_destroy(struct ion_buffer *buffer);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800115
116/**
117 * struct ion_heap_ops - ops to operate on a given heap
118 * @allocate: allocate memory
Patrick Dalyeeeb9402016-11-01 20:54:41 -0700119 * @free: free memory. Will be called with
120 * ION_PRIV_FLAG_SHRINKER_FREE set in buffer flags when
121 * called from a shrinker. In that case, the pages being
122 * free'd must be truly free'd back to the system, not put
123 * in a page pool or otherwise cached.
Patrick Daly7e8cbb42016-11-01 18:37:42 -0700124 * @phys get physical address of a buffer (only define on
125 * physically contiguous heaps)
126 * @map_dma map the memory for dma to a scatterlist
127 * @unmap_dma unmap the memory for dma
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800128 * @map_kernel map memory to the kernel
129 * @unmap_kernel unmap memory to the kernel
130 * @map_user map memory to userspace
Patrick Dalyeeeb9402016-11-01 20:54:41 -0700131 * @unmap_user unmap memory to userspace
Colin Cross9e907652013-12-13 14:24:49 -0800132 *
133 * allocate, phys, and map_user return 0 on success, -errno on error.
Mitchel Humpherys53a91c62014-02-17 13:58:39 -0800134 * map_dma and map_kernel return pointer on success, ERR_PTR on
135 * error. @free will be called with ION_PRIV_FLAG_SHRINKER_FREE set in
136 * the buffer's private_flags when called from a shrinker. In that
137 * case, the pages being free'd must be truly free'd back to the
138 * system, not put in a page pool or otherwise cached.
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800139 */
140struct ion_heap_ops {
Daeseok Youn51108982014-02-10 20:16:50 +0900141 int (*allocate)(struct ion_heap *heap,
142 struct ion_buffer *buffer, unsigned long len,
143 unsigned long align, unsigned long flags);
144 void (*free)(struct ion_buffer *buffer);
Patrick Daly7e8cbb42016-11-01 18:37:42 -0700145 int (*phys)(struct ion_heap *heap, struct ion_buffer *buffer,
146 ion_phys_addr_t *addr, size_t *len);
147 struct sg_table * (*map_dma)(struct ion_heap *heap,
148 struct ion_buffer *buffer);
149 void (*unmap_dma)(struct ion_heap *heap, struct ion_buffer *buffer);
Daeseok Youn51108982014-02-10 20:16:50 +0900150 void * (*map_kernel)(struct ion_heap *heap, struct ion_buffer *buffer);
151 void (*unmap_kernel)(struct ion_heap *heap, struct ion_buffer *buffer);
152 int (*map_user)(struct ion_heap *mapper, struct ion_buffer *buffer,
153 struct vm_area_struct *vma);
Colin Crossb9daf0b2014-02-17 13:58:38 -0800154 int (*shrink)(struct ion_heap *heap, gfp_t gfp_mask, int nr_to_scan);
Patrick Dalyeeeb9402016-11-01 20:54:41 -0700155 void (*unmap_user)(struct ion_heap *mapper, struct ion_buffer *buffer);
156 int (*print_debug)(struct ion_heap *heap, struct seq_file *s,
157 const struct list_head *mem_map);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800158};
159
160/**
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800161 * heap flags - flags between the heaps and core ion code
162 */
163#define ION_HEAP_FLAG_DEFER_FREE (1 << 0)
164
165/**
Mitchel Humpherys53a91c62014-02-17 13:58:39 -0800166 * private flags - flags internal to ion
167 */
168/*
169 * Buffer is being freed from a shrinker function. Skip any possible
170 * heap-specific caching mechanism (e.g. page pools). Guarantees that
171 * any buffer storage that came from the system allocator will be
172 * returned to the system allocator.
173 */
174#define ION_PRIV_FLAG_SHRINKER_FREE (1 << 0)
175
176/**
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800177 * struct ion_heap - represents a heap in the system
178 * @node: rb node to put the heap on the device's tree of heaps
179 * @dev: back pointer to the ion_device
180 * @type: type of heap
181 * @ops: ops struct as above
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800182 * @flags: flags
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800183 * @id: id of heap, also indicates priority of this heap when
184 * allocating. These are specified by platform data and
185 * MUST be unique
186 * @name: used for debugging
Colin Crossb9daf0b2014-02-17 13:58:38 -0800187 * @shrinker: a shrinker for the heap
Patrick Dalyeeeb9402016-11-01 20:54:41 -0700188 * @priv: private heap data
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800189 * @free_list: free list head if deferred free is used
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800190 * @free_list_size size of the deferred free list in bytes
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800191 * @lock: protects the free list
192 * @waitqueue: queue to wait on from deferred free thread
193 * @task: task struct of deferred free thread
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800194 * @debug_show: called when heap debug file is read to add any
195 * heap specific debug info to output
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800196 *
197 * Represents a pool of memory from which buffers can be made. In some
198 * systems the only heap is regular system memory allocated via vmalloc.
199 * On others, some blocks might require large physically contiguous buffers
200 * that are allocated from a specially reserved heap.
201 */
202struct ion_heap {
Rebecca Schultz Zavincd694882013-12-13 14:24:25 -0800203 struct plist_node node;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800204 struct ion_device *dev;
205 enum ion_heap_type type;
206 struct ion_heap_ops *ops;
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800207 unsigned long flags;
Rebecca Schultz Zavincd694882013-12-13 14:24:25 -0800208 unsigned int id;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800209 const char *name;
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800210 struct shrinker shrinker;
Patrick Dalyeeeb9402016-11-01 20:54:41 -0700211 void *priv;
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800212 struct list_head free_list;
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800213 size_t free_list_size;
John Stultz6a72a702013-12-17 17:04:29 -0800214 spinlock_t free_lock;
Rebecca Schultz Zavinfe2faea2013-12-13 14:24:35 -0800215 wait_queue_head_t waitqueue;
216 struct task_struct *task;
Seunghun Lee10f62862014-05-01 01:30:23 +0900217
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800218 int (*debug_show)(struct ion_heap *heap, struct seq_file *, void *);
Patrick Dalye4640062017-08-01 19:56:52 -0700219 atomic_long_t total_allocated;
220 atomic_long_t total_handles;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800221};
222
223/**
Rebecca Schultz Zavin45b17a82013-12-13 14:24:11 -0800224 * ion_buffer_cached - this ion buffer is cached
225 * @buffer: buffer
226 *
227 * indicates whether this ion buffer is cached
228 */
229bool ion_buffer_cached(struct ion_buffer *buffer);
230
231/**
Rebecca Schultz Zavin13ba7802013-12-13 14:24:06 -0800232 * ion_buffer_fault_user_mappings - fault in user mappings of this buffer
233 * @buffer: buffer
234 *
235 * indicates whether userspace mappings of this buffer will be faulted
236 * in, this can affect how buffers are allocated from the heap.
237 */
238bool ion_buffer_fault_user_mappings(struct ion_buffer *buffer);
239
240/**
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800241 * ion_device_create - allocates and returns an ion device
242 * @custom_ioctl: arch specific ioctl function if applicable
243 *
244 * returns a valid device or -PTR_ERR
245 */
246struct ion_device *ion_device_create(long (*custom_ioctl)
247 (struct ion_client *client,
248 unsigned int cmd,
249 unsigned long arg));
250
251/**
252 * ion_device_destroy - free and device and it's resource
253 * @dev: the device
254 */
255void ion_device_destroy(struct ion_device *dev);
256
257/**
258 * ion_device_add_heap - adds a heap to the ion device
259 * @dev: the device
260 * @heap: the heap to add
261 */
262void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap);
263
Patrick Dalyeeeb9402016-11-01 20:54:41 -0700264struct pages_mem {
265 struct page **pages;
266 u32 size;
267 void (*free_fn)(const void *);
268};
269
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800270/**
Rebecca Schultz Zavin88982272013-12-13 14:24:26 -0800271 * some helpers for common operations on buffers using the sg_table
272 * and vaddr fields
273 */
274void *ion_heap_map_kernel(struct ion_heap *, struct ion_buffer *);
275void ion_heap_unmap_kernel(struct ion_heap *, struct ion_buffer *);
276int ion_heap_map_user(struct ion_heap *, struct ion_buffer *,
277 struct vm_area_struct *);
Rebecca Schultz Zavin0b6b2cd2013-12-13 14:24:32 -0800278int ion_heap_buffer_zero(struct ion_buffer *buffer);
Colin Crossdf6cf5c2013-12-13 19:26:30 -0800279int ion_heap_pages_zero(struct page *page, size_t size, pgprot_t pgprot);
Rebecca Schultz Zavin88982272013-12-13 14:24:26 -0800280
Laura Abbott29defcc2014-08-01 16:13:40 -0700281int msm_ion_heap_high_order_page_zero(struct device *dev, struct page *page,
282 int order);
Patrick Dalyc1005d82016-09-22 17:43:26 -0700283struct ion_heap *get_ion_heap(int heap_id);
Laura Abbott29defcc2014-08-01 16:13:40 -0700284int msm_ion_heap_sg_table_zero(struct device *dev, struct sg_table *sg,
285 size_t size);
Patrick Dalyeeeb9402016-11-01 20:54:41 -0700286int msm_ion_heap_pages_zero(struct page **pages, int num_pages);
287int msm_ion_heap_alloc_pages_mem(struct pages_mem *pages_mem);
288void msm_ion_heap_free_pages_mem(struct pages_mem *pages_mem);
289
290long msm_ion_custom_ioctl(struct ion_client *client,
291 unsigned int cmd,
292 unsigned long arg);
293
Patrick Dalyc1005d82016-09-22 17:43:26 -0700294int ion_heap_is_system_secure_heap_type(enum ion_heap_type type);
Laura Abbott29defcc2014-08-01 16:13:40 -0700295int get_secure_vmid(unsigned long flags);
Sudarshan Rajagopalanacc4a032017-07-20 16:56:20 -0700296int get_vmid(unsigned long flags);
Laura Abbott29defcc2014-08-01 16:13:40 -0700297bool is_secure_vmid_valid(int vmid);
Sudarshan Rajagopalan33ae0432017-05-18 00:12:53 -0700298unsigned int count_set_bits(unsigned long val);
299int populate_vm_list(unsigned long flags, unsigned int *vm_list, int nelems);
Laura Abbott29defcc2014-08-01 16:13:40 -0700300
301/**
302 * Functions to help assign/unassign sg_table for System Secure Heap
303 */
304
305int ion_system_secure_heap_unassign_sg(struct sg_table *sgt, int source_vmid);
306int ion_system_secure_heap_assign_sg(struct sg_table *sgt, int dest_vmid);
307int ion_system_secure_heap_prefetch(struct ion_heap *heap, void *data);
308int ion_system_secure_heap_drain(struct ion_heap *heap, void *data);
Patrick Dalyc1005d82016-09-22 17:43:26 -0700309
Patrick Dalyeeeb9402016-11-01 20:54:41 -0700310/**
311 * ion_heap_init_shrinker
312 * @heap: the heap
313 *
314 * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag or defines the shrink op
315 * this function will be called to setup a shrinker to shrink the freelists
316 * and call the heap's shrink op.
317 */
318void ion_heap_init_shrinker(struct ion_heap *heap);
319
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800320/**
Colin Crossb9daf0b2014-02-17 13:58:38 -0800321 * ion_heap_init_shrinker
322 * @heap: the heap
323 *
324 * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag or defines the shrink op
325 * this function will be called to setup a shrinker to shrink the freelists
326 * and call the heap's shrink op.
327 */
328void ion_heap_init_shrinker(struct ion_heap *heap);
329
330/**
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800331 * ion_heap_init_deferred_free -- initialize deferred free functionality
332 * @heap: the heap
333 *
334 * If a heap sets the ION_HEAP_FLAG_DEFER_FREE flag this function will
335 * be called to setup deferred frees. Calls to free the buffer will
336 * return immediately and the actual free will occur some time later
337 */
338int ion_heap_init_deferred_free(struct ion_heap *heap);
339
340/**
341 * ion_heap_freelist_add - add a buffer to the deferred free list
342 * @heap: the heap
John Stultze1d855b2013-12-13 19:26:33 -0800343 * @buffer: the buffer
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800344 *
345 * Adds an item to the deferred freelist.
346 */
347void ion_heap_freelist_add(struct ion_heap *heap, struct ion_buffer *buffer);
348
349/**
350 * ion_heap_freelist_drain - drain the deferred free list
351 * @heap: the heap
Carlos E. Garcia69e98df2015-04-24 09:40:42 -0400352 * @size: amount of memory to drain in bytes
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800353 *
354 * Drains the indicated amount of memory from the deferred freelist immediately.
355 * Returns the total amount freed. The total freed may be higher depending
356 * on the size of the items in the list, or lower if there is insufficient
357 * total memory on the freelist.
358 */
359size_t ion_heap_freelist_drain(struct ion_heap *heap, size_t size);
360
361/**
Patrick Dalyeeeb9402016-11-01 20:54:41 -0700362 * ion_heap_freelist_drain_from_shrinker - drain the deferred free
Mitchel Humpherys53a91c62014-02-17 13:58:39 -0800363 * list, skipping any heap-specific
364 * pooling or caching mechanisms
365 *
366 * @heap: the heap
367 * @size: amount of memory to drain in bytes
368 *
369 * Drains the indicated amount of memory from the deferred freelist immediately.
370 * Returns the total amount freed. The total freed may be higher depending
371 * on the size of the items in the list, or lower if there is insufficient
372 * total memory on the freelist.
373 *
374 * Unlike with @ion_heap_freelist_drain, don't put any pages back into
375 * page pools or otherwise cache the pages. Everything must be
376 * genuinely free'd back to the system. If you're free'ing from a
377 * shrinker you probably want to use this. Note that this relies on
Patrick Dalyeeeb9402016-11-01 20:54:41 -0700378 * the heap.ops.free callback honoring the
379 * ION_PRIV_FLAG_SHRINKER_FREE flag.
Mitchel Humpherys53a91c62014-02-17 13:58:39 -0800380 */
Patrick Dalyeeeb9402016-11-01 20:54:41 -0700381size_t ion_heap_freelist_drain_from_shrinker(struct ion_heap *heap,
382 size_t size);
Mitchel Humpherys53a91c62014-02-17 13:58:39 -0800383
384/**
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800385 * ion_heap_freelist_size - returns the size of the freelist in bytes
386 * @heap: the heap
387 */
388size_t ion_heap_freelist_size(struct ion_heap *heap);
389
Rebecca Schultz Zavin88982272013-12-13 14:24:26 -0800390
391/**
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800392 * functions for creating and destroying the built in ion heaps.
393 * architectures can add their own custom architecture specific
394 * heaps as appropriate.
395 */
396
397struct ion_heap *ion_heap_create(struct ion_platform_heap *);
398void ion_heap_destroy(struct ion_heap *);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800399struct ion_heap *ion_system_heap_create(struct ion_platform_heap *);
400void ion_system_heap_destroy(struct ion_heap *);
401
402struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *);
403void ion_system_contig_heap_destroy(struct ion_heap *);
404
405struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *);
406void ion_carveout_heap_destroy(struct ion_heap *);
Rebecca Schultz Zavine3c2eb72013-12-13 14:24:27 -0800407
408struct ion_heap *ion_chunk_heap_create(struct ion_platform_heap *);
409void ion_chunk_heap_destroy(struct ion_heap *);
Laura Abbott29defcc2014-08-01 16:13:40 -0700410#ifdef CONFIG_CMA
Benjamin Gaignard349c9e12013-12-13 14:24:44 -0800411struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *);
412void ion_cma_heap_destroy(struct ion_heap *);
Laura Abbott29defcc2014-08-01 16:13:40 -0700413#else
414static inline struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *h)
415{
416 return NULL;
417}
418
419static inline void ion_cma_heap_destroy(struct ion_heap *h) {}
420#endif
Benjamin Gaignard349c9e12013-12-13 14:24:44 -0800421
Patrick Dalyc1005d82016-09-22 17:43:26 -0700422struct ion_heap *ion_system_secure_heap_create(struct ion_platform_heap *heap);
423void ion_system_secure_heap_destroy(struct ion_heap *heap);
424
Laura Abbott29defcc2014-08-01 16:13:40 -0700425struct ion_heap *ion_cma_secure_heap_create(struct ion_platform_heap *heap);
426void ion_cma_secure_heap_destroy(struct ion_heap *heap);
427
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800428/**
Patrick Daly7e8cbb42016-11-01 18:37:42 -0700429 * kernel api to allocate/free from carveout -- used when carveout is
430 * used to back an architecture specific custom heap
431 */
432ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size,
433 unsigned long align);
434void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr,
435 unsigned long size);
436/**
437 * The carveout heap returns physical addresses, since 0 may be a valid
438 * physical address, this is used to indicate allocation failed
439 */
440#define ION_CARVEOUT_ALLOCATE_FAIL -1
441
442/**
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800443 * functions for creating and destroying a heap pool -- allows you
444 * to keep a pool of pre allocated memory to use from your heap. Keeping
445 * a pool of memory that is ready for dma, ie any cached mapping have been
Tristan Lelongbc47e7d2014-10-31 16:31:31 -0700446 * invalidated from the cache, provides a significant performance benefit on
Sriram Raghunathan7e416172015-09-22 22:35:51 +0530447 * many systems
448 */
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800449
450/**
451 * struct ion_page_pool - pagepool struct
Rebecca Schultz Zavin0fb9b812013-12-13 14:24:13 -0800452 * @high_count: number of highmem items in the pool
453 * @low_count: number of lowmem items in the pool
454 * @high_items: list of highmem items
455 * @low_items: list of lowmem items
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800456 * @mutex: lock protecting this struct and especially the count
457 * item list
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800458 * @gfp_mask: gfp_mask to use from alloc
459 * @order: order of pages in the pool
Rebecca Schultz Zavin797a95c2013-12-13 14:24:15 -0800460 * @list: plist node for list of pools
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800461 *
462 * Allows you to keep a pool of pre allocated pages to use from your heap.
463 * Keeping a pool of pages that is ready for dma, ie any cached mapping have
Tristan Lelongbc47e7d2014-10-31 16:31:31 -0700464 * been invalidated from the cache, provides a significant performance benefit
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800465 * on many systems
466 */
467struct ion_page_pool {
Rebecca Schultz Zavin0fb9b812013-12-13 14:24:13 -0800468 int high_count;
469 int low_count;
470 struct list_head high_items;
471 struct list_head low_items;
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800472 struct mutex mutex;
Laura Abbott29defcc2014-08-01 16:13:40 -0700473 struct device *dev;
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800474 gfp_t gfp_mask;
475 unsigned int order;
Rebecca Schultz Zavin797a95c2013-12-13 14:24:15 -0800476 struct plist_node list;
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800477};
478
Laura Abbott29defcc2014-08-01 16:13:40 -0700479struct ion_page_pool *ion_page_pool_create(struct device *dev, gfp_t gfp_mask,
480 unsigned int order);
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800481void ion_page_pool_destroy(struct ion_page_pool *);
Patrick Dalyeeeb9402016-11-01 20:54:41 -0700482void *ion_page_pool_alloc(struct ion_page_pool *a, bool *from_pool);
Laura Abbott29defcc2014-08-01 16:13:40 -0700483void *ion_page_pool_alloc_pool_only(struct ion_page_pool *a);
484void ion_page_pool_free(struct ion_page_pool *a, struct page *b);
Vinil Cheeramvelil21ed4042015-07-08 10:35:06 +0800485void ion_page_pool_free_immediate(struct ion_page_pool *, struct page *);
Laura Abbott29defcc2014-08-01 16:13:40 -0700486int ion_page_pool_total(struct ion_page_pool *pool, bool high);
487size_t ion_system_heap_secure_page_pool_total(struct ion_heap *heap, int vmid);
Vinil Cheeramvelil21ed4042015-07-08 10:35:06 +0800488
489#ifdef CONFIG_ION_POOL_CACHE_POLICY
490static inline void ion_page_pool_alloc_set_cache_policy
491 (struct ion_page_pool *pool,
492 struct page *page){
493 void *va = page_address(page);
494
495 if (va)
496 set_memory_wc((unsigned long)va, 1 << pool->order);
497}
498
499static inline void ion_page_pool_free_set_cache_policy
500 (struct ion_page_pool *pool,
501 struct page *page){
502 void *va = page_address(page);
503
504 if (va)
505 set_memory_wb((unsigned long)va, 1 << pool->order);
506
507}
508#else
509static inline void ion_page_pool_alloc_set_cache_policy
510 (struct ion_page_pool *pool,
511 struct page *page){ }
512
513static inline void ion_page_pool_free_set_cache_policy
514 (struct ion_page_pool *pool,
515 struct page *page){ }
516#endif
517
Rebecca Schultz Zavin0214c7f2013-12-13 14:24:10 -0800518
Rebecca Schultz Zavinea313b52013-12-13 14:24:39 -0800519/** ion_page_pool_shrink - shrinks the size of the memory cached in the pool
520 * @pool: the pool
521 * @gfp_mask: the memory type to reclaim
522 * @nr_to_scan: number of items to shrink in pages
523 *
524 * returns the number of items freed in pages
525 */
526int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
527 int nr_to_scan);
528
Colin Crosse946b202013-12-13 14:25:01 -0800529/**
530 * ion_pages_sync_for_device - cache flush pages for use with the specified
531 * device
532 * @dev: the device the pages will be used with
533 * @page: the first page to be flushed
534 * @size: size in bytes of region to be flushed
535 * @dir: direction of dma transfer
536 */
537void ion_pages_sync_for_device(struct device *dev, struct page *page,
538 size_t size, enum dma_data_direction dir);
539
Laura Abbott29defcc2014-08-01 16:13:40 -0700540int ion_walk_heaps(struct ion_client *client, int heap_id,
541 enum ion_heap_type type, void *data,
Patrick Dalyeeeb9402016-11-01 20:54:41 -0700542 int (*f)(struct ion_heap *heap, void *data));
543
544struct ion_handle *ion_handle_get_by_id(struct ion_client *client,
545 int id);
546
547int ion_handle_put(struct ion_handle *handle);
548
Laura Abbott29defcc2014-08-01 16:13:40 -0700549void show_ion_usage(struct ion_device *dev);
550
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800551#endif /* _ION_PRIV_H */