blob: 6d636ee294646737c3958f345541045d461f61c4 [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.
Olav Haugan0a852512012-01-09 10:20:55 -08005 * Copyright (c) 2011-2012, Code Aurora Forum. 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
29struct ion_mapping;
30
31struct ion_dma_mapping {
32 struct kref ref;
33 struct scatterlist *sglist;
34};
35
36struct ion_kernel_mapping {
37 struct kref ref;
38 void *vaddr;
39};
40
Olav Hauganb3676592012-03-02 15:02:25 -080041enum {
42 DI_PARTITION_NUM = 0,
43 DI_DOMAIN_NUM = 1,
44 DI_MAX,
45};
46
Laura Abbott8c017362011-09-22 20:59:12 -070047/**
48 * struct ion_iommu_map - represents a mapping of an ion buffer to an iommu
49 * @iova_addr - iommu virtual address
50 * @node - rb node to exist in the buffer's tree of iommu mappings
51 * @domain_info - contains the partition number and domain number
52 * domain_info[1] = domain number
53 * domain_info[0] = partition number
54 * @ref - for reference counting this mapping
55 * @mapped_size - size of the iova space mapped
56 * (may not be the same as the buffer size)
Olav Hauganb3676592012-03-02 15:02:25 -080057 * @flags - iommu domain/partition specific flags.
Laura Abbott8c017362011-09-22 20:59:12 -070058 *
59 * Represents a mapping of one ion buffer to a particular iommu domain
60 * and address range. There may exist other mappings of this buffer in
61 * different domains or address ranges. All mappings will have the same
62 * cacheability and security.
63 */
64struct ion_iommu_map {
65 unsigned long iova_addr;
66 struct rb_node node;
67 union {
Olav Hauganb3676592012-03-02 15:02:25 -080068 int domain_info[DI_MAX];
Laura Abbott8c017362011-09-22 20:59:12 -070069 uint64_t key;
70 };
71 struct ion_buffer *buffer;
72 struct kref ref;
73 int mapped_size;
Olav Hauganb3676592012-03-02 15:02:25 -080074 unsigned long flags;
Laura Abbott8c017362011-09-22 20:59:12 -070075};
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070076
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070077struct ion_buffer *ion_handle_buffer(struct ion_handle *handle);
78
79/**
80 * struct ion_buffer - metadata for a particular buffer
81 * @ref: refernce count
82 * @node: node in the ion_device buffers tree
83 * @dev: back pointer to the ion_device
84 * @heap: back pointer to the heap the buffer came from
85 * @flags: buffer specific flags
86 * @size: size of the buffer
87 * @priv_virt: private data to the buffer representable as
88 * a void *
89 * @priv_phys: private data to the buffer representable as
90 * an ion_phys_addr_t (and someday a phys_addr_t)
91 * @lock: protects the buffers cnt fields
92 * @kmap_cnt: number of times the buffer is mapped to the kernel
93 * @vaddr: the kenrel mapping if kmap_cnt is not zero
94 * @dmap_cnt: number of times the buffer is mapped for dma
Rebecca Schultz Zavine6ee1242011-06-30 12:19:55 -070095 * @sglist: the scatterlist for the buffer is dmap_cnt is not zero
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -070096*/
97struct ion_buffer {
98 struct kref ref;
99 struct rb_node node;
100 struct ion_device *dev;
101 struct ion_heap *heap;
102 unsigned long flags;
103 size_t size;
104 union {
105 void *priv_virt;
106 ion_phys_addr_t priv_phys;
107 };
108 struct mutex lock;
109 int kmap_cnt;
110 void *vaddr;
111 int dmap_cnt;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700112 struct scatterlist *sglist;
Laura Abbott894fd582011-08-19 13:33:56 -0700113 int umap_cnt;
Laura Abbott8c017362011-09-22 20:59:12 -0700114 unsigned int iommu_map_cnt;
115 struct rb_root iommu_maps;
Laura Abbott404f8242011-10-31 14:22:53 -0700116 int marked;
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700117};
118
119/**
120 * struct ion_heap_ops - ops to operate on a given heap
121 * @allocate: allocate memory
122 * @free: free memory
123 * @phys get physical address of a buffer (only define on
124 * physically contiguous heaps)
125 * @map_dma map the memory for dma to a scatterlist
126 * @unmap_dma unmap the memory for dma
127 * @map_kernel map memory to the kernel
128 * @unmap_kernel unmap memory to the kernel
129 * @map_user map memory to userspace
Alex Bird8a3ede32011-11-07 12:33:42 -0800130 * @unmap_user unmap memory to userspace
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700131 */
132struct ion_heap_ops {
133 int (*allocate) (struct ion_heap *heap,
134 struct ion_buffer *buffer, unsigned long len,
135 unsigned long align, unsigned long flags);
136 void (*free) (struct ion_buffer *buffer);
137 int (*phys) (struct ion_heap *heap, struct ion_buffer *buffer,
138 ion_phys_addr_t *addr, size_t *len);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700139 struct scatterlist *(*map_dma) (struct ion_heap *heap,
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700140 struct ion_buffer *buffer);
141 void (*unmap_dma) (struct ion_heap *heap, struct ion_buffer *buffer);
Laura Abbott894fd582011-08-19 13:33:56 -0700142 void * (*map_kernel) (struct ion_heap *heap, struct ion_buffer *buffer,
143 unsigned long flags);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700144 void (*unmap_kernel) (struct ion_heap *heap, struct ion_buffer *buffer);
145 int (*map_user) (struct ion_heap *mapper, struct ion_buffer *buffer,
Laura Abbott894fd582011-08-19 13:33:56 -0700146 struct vm_area_struct *vma, unsigned long flags);
Alex Bird8a3ede32011-11-07 12:33:42 -0800147 void (*unmap_user) (struct ion_heap *mapper, struct ion_buffer *buffer);
Laura Abbottabcb6f72011-10-04 16:26:49 -0700148 int (*cache_op)(struct ion_heap *heap, struct ion_buffer *buffer,
149 void *vaddr, unsigned int offset,
150 unsigned int length, unsigned int cmd);
Laura Abbott8c017362011-09-22 20:59:12 -0700151 int (*map_iommu)(struct ion_buffer *buffer,
152 struct ion_iommu_map *map_data,
153 unsigned int domain_num,
154 unsigned int partition_num,
155 unsigned long align,
156 unsigned long iova_length,
157 unsigned long flags);
158 void (*unmap_iommu)(struct ion_iommu_map *data);
Olav Haugan0671b9a2012-05-25 11:58:56 -0700159 int (*print_debug)(struct ion_heap *heap, struct seq_file *s,
160 const struct rb_root *mem_map);
Olav Haugan0a852512012-01-09 10:20:55 -0800161 int (*secure_heap)(struct ion_heap *heap);
162 int (*unsecure_heap)(struct ion_heap *heap);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700163};
164
165/**
166 * struct ion_heap - represents a heap in the system
167 * @node: rb node to put the heap on the device's tree of heaps
168 * @dev: back pointer to the ion_device
169 * @type: type of heap
170 * @ops: ops struct as above
171 * @id: id of heap, also indicates priority of this heap when
172 * allocating. These are specified by platform data and
173 * MUST be unique
174 * @name: used for debugging
175 *
176 * Represents a pool of memory from which buffers can be made. In some
177 * systems the only heap is regular system memory allocated via vmalloc.
178 * On others, some blocks might require large physically contiguous buffers
179 * that are allocated from a specially reserved heap.
180 */
181struct ion_heap {
182 struct rb_node node;
183 struct ion_device *dev;
184 enum ion_heap_type type;
185 struct ion_heap_ops *ops;
186 int id;
187 const char *name;
188};
189
190/**
Olav Haugan0671b9a2012-05-25 11:58:56 -0700191 * struct mem_map_data - represents information about the memory map for a heap
192 * @node: rb node used to store in the tree of mem_map_data
193 * @addr: start address of memory region.
194 * @addr: end address of memory region.
195 * @size: size of memory region
196 * @client_name: name of the client who owns this buffer.
197 *
198 */
199struct mem_map_data {
200 struct rb_node node;
201 unsigned long addr;
202 unsigned long addr_end;
203 unsigned long size;
204 const char *client_name;
205};
Laura Abbott8c017362011-09-22 20:59:12 -0700206
207#define iommu_map_domain(__m) ((__m)->domain_info[1])
208#define iommu_map_partition(__m) ((__m)->domain_info[0])
209
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700210/**
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700211 * ion_device_create - allocates and returns an ion device
212 * @custom_ioctl: arch specific ioctl function if applicable
213 *
214 * returns a valid device or -PTR_ERR
215 */
216struct ion_device *ion_device_create(long (*custom_ioctl)
217 (struct ion_client *client,
218 unsigned int cmd,
219 unsigned long arg));
220
221/**
222 * ion_device_destroy - free and device and it's resource
223 * @dev: the device
224 */
225void ion_device_destroy(struct ion_device *dev);
226
227/**
228 * ion_device_add_heap - adds a heap to the ion device
229 * @dev: the device
230 * @heap: the heap to add
231 */
232void ion_device_add_heap(struct ion_device *dev, struct ion_heap *heap);
233
234/**
235 * functions for creating and destroying the built in ion heaps.
236 * architectures can add their own custom architecture specific
237 * heaps as appropriate.
238 */
239
240struct ion_heap *ion_heap_create(struct ion_platform_heap *);
241void ion_heap_destroy(struct ion_heap *);
242
243struct ion_heap *ion_system_heap_create(struct ion_platform_heap *);
244void ion_system_heap_destroy(struct ion_heap *);
245
246struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *);
247void ion_system_contig_heap_destroy(struct ion_heap *);
248
249struct ion_heap *ion_carveout_heap_create(struct ion_platform_heap *);
250void ion_carveout_heap_destroy(struct ion_heap *);
Laura Abbott8c017362011-09-22 20:59:12 -0700251
252struct ion_heap *ion_iommu_heap_create(struct ion_platform_heap *);
253void ion_iommu_heap_destroy(struct ion_heap *);
254
Olav Haugan0a852512012-01-09 10:20:55 -0800255struct ion_heap *ion_cp_heap_create(struct ion_platform_heap *);
256void ion_cp_heap_destroy(struct ion_heap *);
257
Laura Abbottcaafeea2011-12-13 11:43:10 -0800258struct ion_heap *ion_reusable_heap_create(struct ion_platform_heap *);
259void ion_reusable_heap_destroy(struct ion_heap *);
260
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700261/**
262 * kernel api to allocate/free from carveout -- used when carveout is
263 * used to back an architecture specific custom heap
264 */
265ion_phys_addr_t ion_carveout_allocate(struct ion_heap *heap, unsigned long size,
266 unsigned long align);
267void ion_carveout_free(struct ion_heap *heap, ion_phys_addr_t addr,
268 unsigned long size);
Laura Abbott8c017362011-09-22 20:59:12 -0700269
270
271struct ion_heap *msm_get_contiguous_heap(void);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700272/**
Olav Haugan0a852512012-01-09 10:20:55 -0800273 * The carveout/cp heap returns physical addresses, since 0 may be a valid
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700274 * physical address, this is used to indicate allocation failed
275 */
276#define ION_CARVEOUT_ALLOCATE_FAIL -1
Olav Haugan0a852512012-01-09 10:20:55 -0800277#define ION_CP_ALLOCATE_FAIL -1
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700278
Laura Abbottcaafeea2011-12-13 11:43:10 -0800279/**
280 * The reserved heap returns physical addresses, since 0 may be a valid
281 * physical address, this is used to indicate allocation failed
282 */
283#define ION_RESERVED_ALLOCATE_FAIL -1
284
285/**
286 * ion_map_fmem_buffer - map fmem allocated memory into the kernel
287 * @buffer - buffer to map
288 * @phys_base - physical base of the heap
289 * @virt_base - virtual base of the heap
290 * @flags - flags for the heap
291 *
292 * Map fmem allocated memory into the kernel address space. This
293 * is designed to be used by other heaps that need fmem behavior.
294 * The virtual range must be pre-allocated.
295 */
296void *ion_map_fmem_buffer(struct ion_buffer *buffer, unsigned long phys_base,
297 void *virt_base, unsigned long flags);
298
Olav Haugan41f85792012-02-08 15:28:05 -0800299/**
300 * ion_do_cache_op - do cache operations.
301 *
302 * @client - pointer to ION client.
303 * @handle - pointer to buffer handle.
304 * @uaddr - virtual address to operate on.
305 * @offset - offset from physical address.
306 * @len - Length of data to do cache operation on.
307 * @cmd - Cache operation to perform:
308 * ION_IOC_CLEAN_CACHES
309 * ION_IOC_INV_CACHES
310 * ION_IOC_CLEAN_INV_CACHES
311 *
312 * Returns 0 on success
313 */
314int ion_do_cache_op(struct ion_client *client, struct ion_handle *handle,
315 void *uaddr, unsigned long offset, unsigned long len,
316 unsigned int cmd);
317
Olav Haugan0671b9a2012-05-25 11:58:56 -0700318void ion_cp_heap_get_base(struct ion_heap *heap, unsigned long *base,
319 unsigned long *size);
320
321void ion_mem_map_show(struct ion_heap *heap);
Rebecca Schultz Zavin0c38bfd2011-06-29 19:44:29 -0700322
323#endif /* _ION_PRIV_H */