blob: 2473dd26666973aac6824e7cd63cdf259d9f44aa [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.
Duy Truong790f06d2013-02-13 16:38:12 -08005 * Copyright (c) 2011-2012, 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
21#include <linux/kref.h>
22#include <linux/mm_types.h>
23#include <linux/mutex.h>
24#include <linux/rbtree.h>
25#include <linux/ion.h>
Laura Abbott8c017362011-09-22 20:59:12 -070026#include <linux/iommu.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070027#include <linux/seq_file.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070028
Olav Hauganb3676592012-03-02 15:02:25 -080029enum {
30 DI_PARTITION_NUM = 0,
31 DI_DOMAIN_NUM = 1,
32 DI_MAX,
33};
34
Laura Abbott8c017362011-09-22 20:59:12 -070035/**
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 * @domain_info - contains the partition number and domain number
40 * domain_info[1] = domain number
41 * domain_info[0] = partition number
42 * @ref - for reference counting this mapping
43 * @mapped_size - size of the iova space mapped
44 * (may not be the same as the buffer size)
Olav Hauganb3676592012-03-02 15:02:25 -080045 * @flags - iommu domain/partition specific flags.
Laura Abbott8c017362011-09-22 20:59:12 -070046 *
47 * Represents a mapping of one ion buffer to a particular iommu domain
48 * and address range. There may exist other mappings of this buffer in
49 * different domains or address ranges. All mappings will have the same
50 * cacheability and security.
51 */
52struct ion_iommu_map {
53 unsigned long iova_addr;
54 struct rb_node node;
55 union {
Olav Hauganb3676592012-03-02 15:02:25 -080056 int domain_info[DI_MAX];
Laura Abbott8c017362011-09-22 20:59:12 -070057 uint64_t key;
58 };
59 struct ion_buffer *buffer;
60 struct kref ref;
61 int mapped_size;
Olav Hauganb3676592012-03-02 15:02:25 -080062 unsigned long flags;
Laura Abbott8c017362011-09-22 20:59:12 -070063};
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070064
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070065struct ion_buffer *ion_handle_buffer(struct ion_handle *handle);
66
67/**
68 * struct ion_buffer - metadata for a particular buffer
69 * @ref: refernce count
70 * @node: node in the ion_device buffers tree
71 * @dev: back pointer to the ion_device
72 * @heap: back pointer to the heap the buffer came from
73 * @flags: buffer specific flags
74 * @size: size of the buffer
75 * @priv_virt: private data to the buffer representable as
76 * a void *
77 * @priv_phys: private data to the buffer representable as
78 * an ion_phys_addr_t (and someday a phys_addr_t)
79 * @lock: protects the buffers cnt fields
80 * @kmap_cnt: number of times the buffer is mapped to the kernel
81 * @vaddr: the kenrel mapping if kmap_cnt is not zero
82 * @dmap_cnt: number of times the buffer is mapped for dma
Laura Abbottb14ed962012-01-30 14:18:08 -080083 * @sg_table: the sg table for the buffer if dmap_cnt is not zero
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070084*/
85struct ion_buffer {
86 struct kref ref;
87 struct rb_node node;
88 struct ion_device *dev;
89 struct ion_heap *heap;
90 unsigned long flags;
91 size_t size;
92 union {
93 void *priv_virt;
94 ion_phys_addr_t priv_phys;
95 };
96 struct mutex lock;
97 int kmap_cnt;
98 void *vaddr;
99 int dmap_cnt;
Laura Abbottb14ed962012-01-30 14:18:08 -0800100 struct sg_table *sg_table;
Laura Abbott894fd582011-08-19 13:33:56 -0700101 int umap_cnt;
Laura Abbott8c017362011-09-22 20:59:12 -0700102 unsigned int iommu_map_cnt;
103 struct rb_root iommu_maps;
Laura Abbott404f8242011-10-31 14:22:53 -0700104 int marked;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700105};
106
107/**
108 * struct ion_heap_ops - ops to operate on a given heap
109 * @allocate: allocate memory
110 * @free: free memory
111 * @phys get physical address of a buffer (only define on
112 * physically contiguous heaps)
113 * @map_dma map the memory for dma to a scatterlist
114 * @unmap_dma unmap the memory for dma
115 * @map_kernel map memory to the kernel
116 * @unmap_kernel unmap memory to the kernel
117 * @map_user map memory to userspace
Alex Bird8a3ede32011-11-07 12:33:42 -0800118 * @unmap_user unmap memory to userspace
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700119 */
120struct ion_heap_ops {
121 int (*allocate) (struct ion_heap *heap,
122 struct ion_buffer *buffer, unsigned long len,
123 unsigned long align, unsigned long flags);
124 void (*free) (struct ion_buffer *buffer);
125 int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer,
126 ion_phys_addr_t *addr, size_t *len);
Laura Abbottb14ed962012-01-30 14:18:08 -0800127 struct sg_table *(*map_dma) (struct ion_heap *heap,
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700128 struct ion_buffer *buffer);
129 void (*unmap_dma) (struct ion_heap *heap, struct ion_buffer *buffer);
Laura Abbottb14ed962012-01-30 14:18:08 -0800130 void * (*map_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700131 void (*unmap_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
132 int (*map_user) (struct ion_heap *mapper, struct ion_buffer *buffer,
Laura Abbottb14ed962012-01-30 14:18:08 -0800133 struct vm_area_struct *vma);
Alex Bird8a3ede32011-11-07 12:33:42 -0800134 void (*unmap_user) (struct ion_heap *mapper, struct ion_buffer *buffer);
Laura Abbottabcb6f72011-10-04 16:26:49 -0700135 int (*cache_op)(struct ion_heap *heap, struct ion_buffer *buffer,
136 void *vaddr, unsigned int offset,
137 unsigned int length, unsigned int cmd);
Laura Abbott8c017362011-09-22 20:59:12 -0700138 int (*map_iommu)(struct ion_buffer *buffer,
139 struct ion_iommu_map *map_data,
140 unsigned int domain_num,
141 unsigned int partition_num,
142 unsigned long align,
143 unsigned long iova_length,
144 unsigned long flags);
145 void (*unmap_iommu)(struct ion_iommu_map *data);
Olav Haugan0671b9a2012-05-25 11:58:56 -0700146 int (*print_debug)(struct ion_heap *heap, struct seq_file *s,
147 const struct rb_root *mem_map);
Laura Abbott7e446482012-06-13 15:59:39 -0700148 int (*secure_heap)(struct ion_heap *heap, int version, void *data);
149 int (*unsecure_heap)(struct ion_heap *heap, int version, void *data);
Laura Abbott93619302012-10-11 11:51:40 -0700150 int (*secure_buffer)(struct ion_buffer *buffer, int version,
151 void *data, int flags);
152 int (*unsecure_buffer)(struct ion_buffer *buffer, int force_unsecure);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700153};
154
155/**
156 * struct ion_heap - represents a heap in the system
157 * @node: rb node to put the heap on the device's tree of heaps
158 * @dev: back pointer to the ion_device
159 * @type: type of heap
160 * @ops: ops struct as above
161 * @id: id of heap, also indicates priority of this heap when
162 * allocating. These are specified by platform data and
163 * MUST be unique
164 * @name: used for debugging
Benjamin Gaignard8dff0a62012-06-25 15:30:18 -0700165 * @priv: private heap data
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700166 *
167 * Represents a pool of memory from which buffers can be made. In some
168 * systems the only heap is regular system memory allocated via vmalloc.
169 * On others, some blocks might require large physically contiguous buffers
170 * that are allocated from a specially reserved heap.
171 */
172struct ion_heap {
173 struct rb_node node;
174 struct ion_device *dev;
175 enum ion_heap_type type;
176 struct ion_heap_ops *ops;
177 int id;
178 const char *name;
Benjamin Gaignard8dff0a62012-06-25 15:30:18 -0700179 void *priv;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700180};
181
182/**
Olav Haugan0671b9a2012-05-25 11:58:56 -0700183 * struct mem_map_data - represents information about the memory map for a heap
184 * @node: rb node used to store in the tree of mem_map_data
185 * @addr: start address of memory region.
186 * @addr: end address of memory region.
187 * @size: size of memory region
188 * @client_name: name of the client who owns this buffer.
189 *
190 */
191struct mem_map_data {
192 struct rb_node node;
193 unsigned long addr;
194 unsigned long addr_end;
195 unsigned long size;
196 const char *client_name;
197};
Laura Abbott8c017362011-09-22 20:59:12 -0700198
199#define iommu_map_domain(__m) ((__m)->domain_info[1])
200#define iommu_map_partition(__m) ((__m)->domain_info[0])
201
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700202/**
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700203 * ion_device_create - allocates and returns an ion device
204 * @custom_ioctl: arch specific ioctl function if applicable
205 *
206 * returns a valid device or -PTR_ERR
207 */
208struct ion_device *ion_device_create(long (*custom_ioctl)
209 (struct ion_client *client,
210 unsigned int cmd,
211 unsigned long arg));
212
213/**
214 * ion_device_destroy - free and device and it's resource
215 * @dev: the device
216 */
217void ion_device_destroy(struct ion_device *dev);
218
219/**
220 * ion_device_add_heap - adds a heap to the ion device
221 * @dev: the device
222 * @heap: the heap to add
223 */
224void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap);
225
226/**
227 * functions for creating and destroying the built in ion heaps.
228 * architectures can add their own custom architecture specific
229 * heaps as appropriate.
230 */
231
232struct ion_heap *ion_heap_create(struct ion_platform_heap *);
233void ion_heap_destroy(struct ion_heap *);
234
235struct ion_heap *ion_system_heap_create(struct ion_platform_heap *);
236void ion_system_heap_destroy(struct ion_heap *);
237
238struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *);
239void ion_system_contig_heap_destroy(struct ion_heap *);
240
241struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *);
242void ion_carveout_heap_destroy(struct ion_heap *);
Laura Abbott8c017362011-09-22 20:59:12 -0700243
244struct ion_heap *ion_iommu_heap_create(struct ion_platform_heap *);
245void ion_iommu_heap_destroy(struct ion_heap *);
246
Olav Haugan0a852512012-01-09 10:20:55 -0800247struct ion_heap *ion_cp_heap_create(struct ion_platform_heap *);
248void ion_cp_heap_destroy(struct ion_heap *);
249
Laura Abbottcaafeea2011-12-13 11:43:10 -0800250struct ion_heap *ion_reusable_heap_create(struct ion_platform_heap *);
251void ion_reusable_heap_destroy(struct ion_heap *);
252
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700253/**
254 * kernel api to allocate/free from carveout -- used when carveout is
255 * used to back an architecture specific custom heap
256 */
257ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size,
258 unsigned long align);
259void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr,
260 unsigned long size);
Laura Abbott8c017362011-09-22 20:59:12 -0700261
Benjamin Gaignard07b590e2012-08-15 10:55:10 -0700262#ifdef CONFIG_CMA
263struct ion_heap *ion_cma_heap_create(struct ion_platform_heap *);
264void ion_cma_heap_destroy(struct ion_heap *);
265#endif
Laura Abbott8c017362011-09-22 20:59:12 -0700266
267struct ion_heap *msm_get_contiguous_heap(void);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700268/**
Olav Haugan0a852512012-01-09 10:20:55 -0800269 * The carveout/cp heap returns physical addresses, since 0 may be a valid
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700270 * physical address, this is used to indicate allocation failed
271 */
272#define ION_CARVEOUT_ALLOCATE_FAIL -1
Olav Haugan0a852512012-01-09 10:20:55 -0800273#define ION_CP_ALLOCATE_FAIL -1
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700274
Laura Abbottcaafeea2011-12-13 11:43:10 -0800275/**
276 * The reserved heap returns physical addresses, since 0 may be a valid
277 * physical address, this is used to indicate allocation failed
278 */
279#define ION_RESERVED_ALLOCATE_FAIL -1
280
281/**
282 * ion_map_fmem_buffer - map fmem allocated memory into the kernel
283 * @buffer - buffer to map
284 * @phys_base - physical base of the heap
285 * @virt_base - virtual base of the heap
286 * @flags - flags for the heap
287 *
288 * Map fmem allocated memory into the kernel address space. This
289 * is designed to be used by other heaps that need fmem behavior.
290 * The virtual range must be pre-allocated.
291 */
292void *ion_map_fmem_buffer(struct ion_buffer *buffer, unsigned long phys_base,
293 void *virt_base, unsigned long flags);
294
Olav Haugan41f85792012-02-08 15:28:05 -0800295/**
296 * ion_do_cache_op - do cache operations.
297 *
298 * @client - pointer to ION client.
299 * @handle - pointer to buffer handle.
300 * @uaddr - virtual address to operate on.
301 * @offset - offset from physical address.
302 * @len - Length of data to do cache operation on.
303 * @cmd - Cache operation to perform:
304 * ION_IOC_CLEAN_CACHES
305 * ION_IOC_INV_CACHES
306 * ION_IOC_CLEAN_INV_CACHES
307 *
308 * Returns 0 on success
309 */
310int ion_do_cache_op(struct ion_client *client, struct ion_handle *handle,
311 void *uaddr, unsigned long offset, unsigned long len,
312 unsigned int cmd);
313
Olav Haugan0671b9a2012-05-25 11:58:56 -0700314void ion_cp_heap_get_base(struct ion_heap *heap, unsigned long *base,
315 unsigned long *size);
316
317void ion_mem_map_show(struct ion_heap *heap);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700318
Laura Abbott93619302012-10-11 11:51:40 -0700319
320
321int ion_secure_handle(struct ion_client *client, struct ion_handle *handle,
322 int version, void *data, int flags);
323
324int ion_unsecure_handle(struct ion_client *client, struct ion_handle *handle);
Laura Abbott4afbd8b2013-02-15 09:21:33 -0800325
326int ion_heap_allow_secure_allocation(enum ion_heap_type type);
327
328int ion_heap_allow_heap_secure(enum ion_heap_type type);
329
330int ion_heap_allow_handle_secure(enum ion_heap_type type);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700331#endif /* _ION_PRIV_H */