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