Laura Abbott | 6438e53 | 2012-07-20 10:10:41 -0700 | [diff] [blame] | 1 | /* |
Laura Abbott | 6438e53 | 2012-07-20 10:10:41 -0700 | [diff] [blame] | 2 | * |
Laura Abbott | 7db4e0b | 2013-01-03 14:20:16 -0800 | [diff] [blame] | 3 | * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Laura Abbott | 6438e53 | 2012-07-20 10:10:41 -0700 | [diff] [blame] | 4 | * |
| 5 | * This software is licensed under the terms of the GNU General Public |
| 6 | * License version 2, as published by the Free Software Foundation, and |
| 7 | * may be copied, distributed, and modified under those terms. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #ifndef _LINUX_MSM_ION_H |
| 17 | #define _LINUX_MSM_ION_H |
| 18 | |
| 19 | #include <linux/ion.h> |
| 20 | |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 21 | enum msm_ion_heap_types { |
| 22 | ION_HEAP_TYPE_MSM_START = ION_HEAP_TYPE_CUSTOM + 1, |
| 23 | ION_HEAP_TYPE_IOMMU = ION_HEAP_TYPE_MSM_START, |
| 24 | ION_HEAP_TYPE_CP, |
Laura Abbott | a8c373f | 2013-02-15 09:25:35 -0800 | [diff] [blame] | 25 | ION_HEAP_TYPE_SECURE_DMA, |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | /** |
| 29 | * These are the only ids that should be used for Ion heap ids. |
| 30 | * The ids listed are the order in which allocation will be attempted |
| 31 | * if specified. Don't swap the order of heap ids unless you know what |
| 32 | * you are doing! |
| 33 | * Id's are spaced by purpose to allow new Id's to be inserted in-between (for |
| 34 | * possible fallbacks) |
| 35 | */ |
| 36 | |
| 37 | enum ion_heap_ids { |
| 38 | INVALID_HEAP_ID = -1, |
| 39 | ION_CP_MM_HEAP_ID = 8, |
| 40 | ION_CP_MFC_HEAP_ID = 12, |
| 41 | ION_CP_WB_HEAP_ID = 16, /* 8660 only */ |
| 42 | ION_CAMERA_HEAP_ID = 20, /* 8660 only */ |
Neeti Desai | 9dc9db4 | 2012-10-18 17:53:51 -0700 | [diff] [blame] | 43 | ION_PIL1_HEAP_ID = 23, /* Currently used for other PIL images */ |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 44 | ION_SF_HEAP_ID = 24, |
| 45 | ION_IOMMU_HEAP_ID = 25, |
Neeti Desai | 9dc9db4 | 2012-10-18 17:53:51 -0700 | [diff] [blame] | 46 | ION_PIL2_HEAP_ID = 26, /* Currently used for modem firmware images */ |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 47 | ION_QSECOM_HEAP_ID = 27, |
| 48 | ION_AUDIO_HEAP_ID = 28, |
| 49 | |
| 50 | ION_MM_FIRMWARE_HEAP_ID = 29, |
| 51 | ION_SYSTEM_HEAP_ID = 30, |
| 52 | |
| 53 | ION_HEAP_ID_RESERVED = 31 /** Bit reserved for ION_SECURE flag */ |
| 54 | }; |
| 55 | |
| 56 | enum ion_fixed_position { |
| 57 | NOT_FIXED, |
| 58 | FIXED_LOW, |
| 59 | FIXED_MIDDLE, |
| 60 | FIXED_HIGH, |
| 61 | }; |
| 62 | |
| 63 | enum cp_mem_usage { |
| 64 | VIDEO_BITSTREAM = 0x1, |
| 65 | VIDEO_PIXEL = 0x2, |
| 66 | VIDEO_NONPIXEL = 0x3, |
| 67 | MAX_USAGE = 0x4, |
| 68 | UNKNOWN = 0x7FFFFFFF, |
| 69 | }; |
| 70 | |
| 71 | #define ION_HEAP_CP_MASK (1 << ION_HEAP_TYPE_CP) |
| 72 | |
| 73 | /** |
| 74 | * Flag to use when allocating to indicate that a heap is secure. |
| 75 | */ |
| 76 | #define ION_SECURE (1 << ION_HEAP_ID_RESERVED) |
| 77 | |
| 78 | /** |
Laura Abbott | 7db4e0b | 2013-01-03 14:20:16 -0800 | [diff] [blame] | 79 | * Flag for clients to force contiguous memort allocation |
| 80 | * |
| 81 | * Use of this flag is carefully monitored! |
| 82 | */ |
| 83 | #define ION_FORCE_CONTIGUOUS (1 << 30) |
| 84 | |
| 85 | /** |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 86 | * Macro should be used with ion_heap_ids defined above. |
| 87 | */ |
| 88 | #define ION_HEAP(bit) (1 << (bit)) |
| 89 | |
| 90 | #define ION_VMALLOC_HEAP_NAME "vmalloc" |
| 91 | #define ION_AUDIO_HEAP_NAME "audio" |
| 92 | #define ION_SF_HEAP_NAME "sf" |
| 93 | #define ION_MM_HEAP_NAME "mm" |
| 94 | #define ION_CAMERA_HEAP_NAME "camera_preview" |
| 95 | #define ION_IOMMU_HEAP_NAME "iommu" |
| 96 | #define ION_MFC_HEAP_NAME "mfc" |
| 97 | #define ION_WB_HEAP_NAME "wb" |
| 98 | #define ION_MM_FIRMWARE_HEAP_NAME "mm_fw" |
Neeti Desai | 9dc9db4 | 2012-10-18 17:53:51 -0700 | [diff] [blame] | 99 | #define ION_PIL1_HEAP_NAME "pil_1" |
| 100 | #define ION_PIL2_HEAP_NAME "pil_2" |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 101 | #define ION_QSECOM_HEAP_NAME "qsecom" |
| 102 | #define ION_FMEM_HEAP_NAME "fmem" |
| 103 | |
| 104 | #define ION_SET_CACHED(__cache) (__cache | ION_FLAG_CACHED) |
| 105 | #define ION_SET_UNCACHED(__cache) (__cache & ~ION_FLAG_CACHED) |
| 106 | |
| 107 | #define ION_IS_CACHED(__flags) ((__flags) & ION_FLAG_CACHED) |
| 108 | |
| 109 | #ifdef __KERNEL__ |
| 110 | |
| 111 | /* |
| 112 | * This flag allows clients when mapping into the IOMMU to specify to |
| 113 | * defer un-mapping from the IOMMU until the buffer memory is freed. |
| 114 | */ |
| 115 | #define ION_IOMMU_UNMAP_DELAYED 1 |
| 116 | |
Laura Abbott | 9361930 | 2012-10-11 11:51:40 -0700 | [diff] [blame] | 117 | /* |
| 118 | * This flag allows clients to defer unsecuring a buffer until the buffer |
| 119 | * is actually freed. |
| 120 | */ |
| 121 | #define ION_UNSECURE_DELAYED 1 |
| 122 | |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 123 | /** |
| 124 | * struct ion_cp_heap_pdata - defines a content protection heap in the given |
| 125 | * platform |
| 126 | * @permission_type: Memory ID used to identify the memory to TZ |
| 127 | * @align: Alignment requirement for the memory |
| 128 | * @secure_base: Base address for securing the heap. |
| 129 | * Note: This might be different from actual base address |
| 130 | * of this heap in the case of a shared heap. |
| 131 | * @secure_size: Memory size for securing the heap. |
| 132 | * Note: This might be different from actual size |
| 133 | * of this heap in the case of a shared heap. |
| 134 | * @reusable Flag indicating whether this heap is reusable of not. |
| 135 | * (see FMEM) |
| 136 | * @mem_is_fmem Flag indicating whether this memory is coming from fmem |
| 137 | * or not. |
| 138 | * @fixed_position If nonzero, position in the fixed area. |
| 139 | * @virt_addr: Virtual address used when using fmem. |
| 140 | * @iommu_map_all: Indicates whether we should map whole heap into IOMMU. |
| 141 | * @iommu_2x_map_domain: Indicates the domain to use for overmapping. |
| 142 | * @request_region: function to be called when the number of allocations |
| 143 | * goes from 0 -> 1 |
| 144 | * @release_region: function to be called when the number of allocations |
| 145 | * goes from 1 -> 0 |
| 146 | * @setup_region: function to be called upon ion registration |
| 147 | * @memory_type:Memory type used for the heap |
Mitchel Humpherys | 345f023 | 2013-01-11 10:55:25 -0800 | [diff] [blame] | 148 | * @allow_nonsecure_alloc: allow non-secure allocations from this heap. For |
| 149 | * secure heaps, this flag must be set so allow non-secure |
| 150 | * allocations. For non-secure heaps, this flag is ignored. |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 151 | * |
| 152 | */ |
| 153 | struct ion_cp_heap_pdata { |
| 154 | enum ion_permission_type permission_type; |
| 155 | unsigned int align; |
| 156 | ion_phys_addr_t secure_base; /* Base addr used when heap is shared */ |
| 157 | size_t secure_size; /* Size used for securing heap when heap is shared*/ |
| 158 | int reusable; |
| 159 | int mem_is_fmem; |
Laura Abbott | 3180a5f | 2012-08-03 17:31:03 -0700 | [diff] [blame] | 160 | int is_cma; |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 161 | enum ion_fixed_position fixed_position; |
| 162 | int iommu_map_all; |
| 163 | int iommu_2x_map_domain; |
| 164 | ion_virt_addr_t *virt_addr; |
| 165 | int (*request_region)(void *); |
| 166 | int (*release_region)(void *); |
| 167 | void *(*setup_region)(void); |
| 168 | enum ion_memory_types memory_type; |
Mitchel Humpherys | 345f023 | 2013-01-11 10:55:25 -0800 | [diff] [blame] | 169 | int allow_nonsecure_alloc; |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | /** |
| 173 | * struct ion_co_heap_pdata - defines a carveout heap in the given platform |
| 174 | * @adjacent_mem_id: Id of heap that this heap must be adjacent to. |
| 175 | * @align: Alignment requirement for the memory |
| 176 | * @mem_is_fmem Flag indicating whether this memory is coming from fmem |
| 177 | * or not. |
| 178 | * @fixed_position If nonzero, position in the fixed area. |
| 179 | * @request_region: function to be called when the number of allocations |
| 180 | * goes from 0 -> 1 |
| 181 | * @release_region: function to be called when the number of allocations |
| 182 | * goes from 1 -> 0 |
| 183 | * @setup_region: function to be called upon ion registration |
| 184 | * @memory_type:Memory type used for the heap |
| 185 | * |
| 186 | */ |
| 187 | struct ion_co_heap_pdata { |
| 188 | int adjacent_mem_id; |
| 189 | unsigned int align; |
| 190 | int mem_is_fmem; |
| 191 | enum ion_fixed_position fixed_position; |
| 192 | int (*request_region)(void *); |
| 193 | int (*release_region)(void *); |
| 194 | void *(*setup_region)(void); |
| 195 | enum ion_memory_types memory_type; |
| 196 | }; |
| 197 | |
| 198 | #ifdef CONFIG_ION |
| 199 | /** |
| 200 | * msm_ion_secure_heap - secure a heap. Wrapper around ion_secure_heap. |
| 201 | * |
| 202 | * @heap_id - heap id to secure. |
| 203 | * |
| 204 | * Secure a heap |
| 205 | * Returns 0 on success |
| 206 | */ |
| 207 | int msm_ion_secure_heap(int heap_id); |
| 208 | |
| 209 | /** |
| 210 | * msm_ion_unsecure_heap - unsecure a heap. Wrapper around ion_unsecure_heap. |
| 211 | * |
| 212 | * @heap_id - heap id to secure. |
| 213 | * |
| 214 | * Un-secure a heap |
| 215 | * Returns 0 on success |
| 216 | */ |
| 217 | int msm_ion_unsecure_heap(int heap_id); |
| 218 | |
| 219 | /** |
| 220 | * msm_ion_secure_heap_2_0 - secure a heap using 2.0 APIs |
| 221 | * Wrapper around ion_secure_heap. |
| 222 | * |
| 223 | * @heap_id - heap id to secure. |
| 224 | * @usage - usage hint to TZ |
| 225 | * |
| 226 | * Secure a heap |
| 227 | * Returns 0 on success |
| 228 | */ |
| 229 | int msm_ion_secure_heap_2_0(int heap_id, enum cp_mem_usage usage); |
| 230 | |
| 231 | /** |
| 232 | * msm_ion_unsecure_heap - unsecure a heap secured with 3.0 APIs. |
| 233 | * Wrapper around ion_unsecure_heap. |
| 234 | * |
| 235 | * @heap_id - heap id to secure. |
| 236 | * @usage - usage hint to TZ |
| 237 | * |
| 238 | * Un-secure a heap |
| 239 | * Returns 0 on success |
| 240 | */ |
| 241 | int msm_ion_unsecure_heap_2_0(int heap_id, enum cp_mem_usage usage); |
Laura Abbott | 9361930 | 2012-10-11 11:51:40 -0700 | [diff] [blame] | 242 | |
| 243 | /** |
| 244 | * msm_ion_secure_buffer - secure an individual buffer |
| 245 | * |
| 246 | * @client - client who has access to the buffer |
| 247 | * @handle - buffer to secure |
| 248 | * @usage - usage hint to TZ |
| 249 | * @flags - flags for the securing |
| 250 | */ |
| 251 | int msm_ion_secure_buffer(struct ion_client *client, struct ion_handle *handle, |
| 252 | enum cp_mem_usage usage, int flags); |
| 253 | |
| 254 | /** |
| 255 | * msm_ion_unsecure_buffer - unsecure an individual buffer |
| 256 | * |
| 257 | * @client - client who has access to the buffer |
| 258 | * @handle - buffer to secure |
| 259 | */ |
| 260 | int msm_ion_unsecure_buffer(struct ion_client *client, |
| 261 | struct ion_handle *handle); |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 262 | #else |
| 263 | static inline int msm_ion_secure_heap(int heap_id) |
| 264 | { |
| 265 | return -ENODEV; |
| 266 | |
| 267 | } |
| 268 | |
| 269 | static inline int msm_ion_unsecure_heap(int heap_id) |
| 270 | { |
| 271 | return -ENODEV; |
| 272 | } |
| 273 | |
| 274 | static inline int msm_ion_secure_heap_2_0(int heap_id, enum cp_mem_usage usage) |
| 275 | { |
| 276 | return -ENODEV; |
| 277 | } |
| 278 | |
| 279 | static inline int msm_ion_unsecure_heap_2_0(int heap_id, |
| 280 | enum cp_mem_usage usage) |
| 281 | { |
| 282 | return -ENODEV; |
| 283 | } |
Mitchel Humpherys | 782653e | 2013-02-25 18:54:53 -0800 | [diff] [blame] | 284 | |
| 285 | static inline int msm_ion_secure_buffer(struct ion_client *client, |
| 286 | struct ion_handle *handle, |
| 287 | enum cp_mem_usage usage, |
| 288 | int flags) |
| 289 | { |
| 290 | return -ENODEV; |
| 291 | } |
| 292 | |
| 293 | static inline int msm_ion_unsecure_buffer(struct ion_client *client, |
| 294 | struct ion_handle *handle) |
| 295 | { |
| 296 | return -ENODEV; |
| 297 | } |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 298 | #endif /* CONFIG_ION */ |
| 299 | |
| 300 | #endif /* __KERNEL */ |
| 301 | |
| 302 | /* struct ion_flush_data - data passed to ion for flushing caches |
| 303 | * |
| 304 | * @handle: handle with data to flush |
| 305 | * @fd: fd to flush |
| 306 | * @vaddr: userspace virtual address mapped with mmap |
| 307 | * @offset: offset into the handle to flush |
| 308 | * @length: length of handle to flush |
| 309 | * |
| 310 | * Performs cache operations on the handle. If p is the start address |
| 311 | * of the handle, p + offset through p + offset + length will have |
| 312 | * the cache operations performed |
| 313 | */ |
| 314 | struct ion_flush_data { |
| 315 | struct ion_handle *handle; |
| 316 | int fd; |
| 317 | void *vaddr; |
| 318 | unsigned int offset; |
| 319 | unsigned int length; |
| 320 | }; |
| 321 | |
Mitchel Humpherys | 362b52b | 2012-09-13 10:53:22 -0700 | [diff] [blame] | 322 | #define ION_IOC_MSM_MAGIC 'M' |
| 323 | |
| 324 | /** |
| 325 | * DOC: ION_IOC_CLEAN_CACHES - clean the caches |
| 326 | * |
| 327 | * Clean the caches of the handle specified. |
| 328 | */ |
| 329 | #define ION_IOC_CLEAN_CACHES _IOWR(ION_IOC_MSM_MAGIC, 0, \ |
| 330 | struct ion_flush_data) |
| 331 | /** |
| 332 | * DOC: ION_IOC_INV_CACHES - invalidate the caches |
| 333 | * |
| 334 | * Invalidate the caches of the handle specified. |
| 335 | */ |
| 336 | #define ION_IOC_INV_CACHES _IOWR(ION_IOC_MSM_MAGIC, 1, \ |
| 337 | struct ion_flush_data) |
| 338 | /** |
| 339 | * DOC: ION_IOC_CLEAN_INV_CACHES - clean and invalidate the caches |
| 340 | * |
| 341 | * Clean and invalidate the caches of the handle specified. |
| 342 | */ |
| 343 | #define ION_IOC_CLEAN_INV_CACHES _IOWR(ION_IOC_MSM_MAGIC, 2, \ |
| 344 | struct ion_flush_data) |
| 345 | |
Laura Abbott | 6438e53 | 2012-07-20 10:10:41 -0700 | [diff] [blame] | 346 | #endif |