msm_ion.h revision 21eba50ceeab342ba3946a587e50df275b50f193
1#ifndef _LINUX_MSM_ION_H 2#define _LINUX_MSM_ION_H 3 4#include <linux/ion.h> 5 6enum msm_ion_heap_types { 7 ION_HEAP_TYPE_MSM_START = ION_HEAP_TYPE_CUSTOM + 1, 8 ION_HEAP_TYPE_IOMMU = ION_HEAP_TYPE_MSM_START, 9 ION_HEAP_TYPE_DMA, 10 ION_HEAP_TYPE_CP, 11 ION_HEAP_TYPE_SECURE_DMA, 12 ION_HEAP_TYPE_REMOVED, 13}; 14 15/** 16 * These are the only ids that should be used for Ion heap ids. 17 * The ids listed are the order in which allocation will be attempted 18 * if specified. Don't swap the order of heap ids unless you know what 19 * you are doing! 20 * Id's are spaced by purpose to allow new Id's to be inserted in-between (for 21 * possible fallbacks) 22 */ 23 24enum ion_heap_ids { 25 INVALID_HEAP_ID = -1, 26 ION_CP_MM_HEAP_ID = 8, 27 ION_CP_MFC_HEAP_ID = 12, 28 ION_CP_WB_HEAP_ID = 16, /* 8660 only */ 29 ION_CAMERA_HEAP_ID = 20, /* 8660 only */ 30 ION_SYSTEM_CONTIG_HEAP_ID = 21, 31 ION_ADSP_HEAP_ID = 22, 32 ION_PIL1_HEAP_ID = 23, /* Currently used for other PIL images */ 33 ION_SF_HEAP_ID = 24, 34 ION_IOMMU_HEAP_ID = 25, 35 ION_PIL2_HEAP_ID = 26, /* Currently used for modem firmware images */ 36 ION_QSECOM_HEAP_ID = 27, 37 ION_AUDIO_HEAP_ID = 28, 38 39 ION_MM_FIRMWARE_HEAP_ID = 29, 40 ION_SYSTEM_HEAP_ID = 30, 41 42 ION_HEAP_ID_RESERVED = 31 /** Bit reserved for ION_FLAG_SECURE flag */ 43}; 44 45enum ion_fixed_position { 46 NOT_FIXED, 47 FIXED_LOW, 48 FIXED_MIDDLE, 49 FIXED_HIGH, 50}; 51 52enum cp_mem_usage { 53 VIDEO_BITSTREAM = 0x1, 54 VIDEO_PIXEL = 0x2, 55 VIDEO_NONPIXEL = 0x3, 56 MAX_USAGE = 0x4, 57 UNKNOWN = 0x7FFFFFFF, 58}; 59 60#define ION_HEAP_CP_MASK (1 << ION_HEAP_TYPE_CP) 61#define ION_HEAP_TYPE_DMA_MASK (1 << ION_HEAP_TYPE_DMA) 62 63/** 64 * Flag to use when allocating to indicate that a heap is secure. 65 */ 66#define ION_FLAG_SECURE (1 << ION_HEAP_ID_RESERVED) 67 68/** 69 * Flag for clients to force contiguous memort allocation 70 * 71 * Use of this flag is carefully monitored! 72 */ 73#define ION_FLAG_FORCE_CONTIGUOUS (1 << 30) 74 75/** 76* Deprecated! Please use the corresponding ION_FLAG_* 77*/ 78#define ION_SECURE ION_FLAG_SECURE 79#define ION_FORCE_CONTIGUOUS ION_FLAG_FORCE_CONTIGUOUS 80 81/** 82 * Macro should be used with ion_heap_ids defined above. 83 */ 84#define ION_HEAP(bit) (1 << (bit)) 85 86#define ION_ADSP_HEAP_NAME "adsp" 87#define ION_VMALLOC_HEAP_NAME "vmalloc" 88#define ION_KMALLOC_HEAP_NAME "kmalloc" 89#define ION_AUDIO_HEAP_NAME "audio" 90#define ION_SF_HEAP_NAME "sf" 91#define ION_MM_HEAP_NAME "mm" 92#define ION_CAMERA_HEAP_NAME "camera_preview" 93#define ION_IOMMU_HEAP_NAME "iommu" 94#define ION_MFC_HEAP_NAME "mfc" 95#define ION_WB_HEAP_NAME "wb" 96#define ION_MM_FIRMWARE_HEAP_NAME "mm_fw" 97#define ION_PIL1_HEAP_NAME "pil_1" 98#define ION_PIL2_HEAP_NAME "pil_2" 99#define ION_QSECOM_HEAP_NAME "qsecom" 100 101#define ION_SET_CACHED(__cache) (__cache | ION_FLAG_CACHED) 102#define ION_SET_UNCACHED(__cache) (__cache & ~ION_FLAG_CACHED) 103 104#define ION_IS_CACHED(__flags) ((__flags) & ION_FLAG_CACHED) 105 106#ifdef __KERNEL__ 107 108/* 109 * This flag allows clients when mapping into the IOMMU to specify to 110 * defer un-mapping from the IOMMU until the buffer memory is freed. 111 */ 112#define ION_IOMMU_UNMAP_DELAYED 1 113 114/* 115 * This flag allows clients to defer unsecuring a buffer until the buffer 116 * is actually freed. 117 */ 118#define ION_UNSECURE_DELAYED 1 119 120/** 121 * struct ion_cp_heap_pdata - defines a content protection heap in the given 122 * platform 123 * @permission_type: Memory ID used to identify the memory to TZ 124 * @align: Alignment requirement for the memory 125 * @secure_base: Base address for securing the heap. 126 * Note: This might be different from actual base address 127 * of this heap in the case of a shared heap. 128 * @secure_size: Memory size for securing the heap. 129 * Note: This might be different from actual size 130 * of this heap in the case of a shared heap. 131 * @fixed_position If nonzero, position in the fixed area. 132 * @iommu_map_all: Indicates whether we should map whole heap into IOMMU. 133 * @iommu_2x_map_domain: Indicates the domain to use for overmapping. 134 * @request_region: function to be called when the number of allocations 135 * goes from 0 -> 1 136 * @release_region: function to be called when the number of allocations 137 * goes from 1 -> 0 138 * @setup_region: function to be called upon ion registration 139 * @memory_type:Memory type used for the heap 140 * @allow_nonsecure_alloc: allow non-secure allocations from this heap. For 141 * secure heaps, this flag must be set so allow non-secure 142 * allocations. For non-secure heaps, this flag is ignored. 143 * 144 */ 145struct ion_cp_heap_pdata { 146 enum ion_permission_type permission_type; 147 unsigned int align; 148 ion_phys_addr_t secure_base; /* Base addr used when heap is shared */ 149 size_t secure_size; /* Size used for securing heap when heap is shared*/ 150 int is_cma; 151 enum ion_fixed_position fixed_position; 152 int iommu_map_all; 153 int iommu_2x_map_domain; 154 int (*request_region)(void *); 155 int (*release_region)(void *); 156 void *(*setup_region)(void); 157 enum ion_memory_types memory_type; 158 int allow_nonsecure_alloc; 159}; 160 161/** 162 * struct ion_co_heap_pdata - defines a carveout heap in the given platform 163 * @adjacent_mem_id: Id of heap that this heap must be adjacent to. 164 * @align: Alignment requirement for the memory 165 * @fixed_position If nonzero, position in the fixed area. 166 * @request_region: function to be called when the number of allocations 167 * goes from 0 -> 1 168 * @release_region: function to be called when the number of allocations 169 * goes from 1 -> 0 170 * @setup_region: function to be called upon ion registration 171 * @memory_type:Memory type used for the heap 172 * 173 */ 174struct ion_co_heap_pdata { 175 int adjacent_mem_id; 176 unsigned int align; 177 enum ion_fixed_position fixed_position; 178 int (*request_region)(void *); 179 int (*release_region)(void *); 180 void *(*setup_region)(void); 181 enum ion_memory_types memory_type; 182}; 183 184#ifdef CONFIG_ION 185/** 186 * msm_ion_client_create - allocate a client using the ion_device specified in 187 * drivers/gpu/ion/msm/msm_ion.c 188 * 189 * heap_mask and name are the same as ion_client_create, return values 190 * are the same as ion_client_create. 191 */ 192 193struct ion_client *msm_ion_client_create(unsigned int heap_mask, 194 const char *name); 195 196/** 197 * ion_handle_get_flags - get the flags for a given handle 198 * 199 * @client - client who allocated the handle 200 * @handle - handle to get the flags 201 * @flags - pointer to store the flags 202 * 203 * Gets the current flags for a handle. These flags indicate various options 204 * of the buffer (caching, security, etc.) 205 */ 206int ion_handle_get_flags(struct ion_client *client, struct ion_handle *handle, 207 unsigned long *flags); 208 209 210/** 211 * ion_map_iommu - map the given handle into an iommu 212 * 213 * @client - client who allocated the handle 214 * @handle - handle to map 215 * @domain_num - domain number to map to 216 * @partition_num - partition number to allocate iova from 217 * @align - alignment for the iova 218 * @iova_length - length of iova to map. If the iova length is 219 * greater than the handle length, the remaining 220 * address space will be mapped to a dummy buffer. 221 * @iova - pointer to store the iova address 222 * @buffer_size - pointer to store the size of the buffer 223 * @flags - flags for options to map 224 * @iommu_flags - flags specific to the iommu. 225 * 226 * Maps the handle into the iova space specified via domain number. Iova 227 * will be allocated from the partition specified via partition_num. 228 * Returns 0 on success, negative value on error. 229 */ 230int ion_map_iommu(struct ion_client *client, struct ion_handle *handle, 231 int domain_num, int partition_num, unsigned long align, 232 unsigned long iova_length, unsigned long *iova, 233 unsigned long *buffer_size, 234 unsigned long flags, unsigned long iommu_flags); 235 236 237/** 238 * ion_handle_get_size - get the allocated size of a given handle 239 * 240 * @client - client who allocated the handle 241 * @handle - handle to get the size 242 * @size - pointer to store the size 243 * 244 * gives the allocated size of a handle. returns 0 on success, negative 245 * value on error 246 * 247 * NOTE: This is intended to be used only to get a size to pass to map_iommu. 248 * You should *NOT* rely on this for any other usage. 249 */ 250 251int ion_handle_get_size(struct ion_client *client, struct ion_handle *handle, 252 unsigned long *size); 253 254/** 255 * ion_unmap_iommu - unmap the handle from an iommu 256 * 257 * @client - client who allocated the handle 258 * @handle - handle to unmap 259 * @domain_num - domain to unmap from 260 * @partition_num - partition to unmap from 261 * 262 * Decrement the reference count on the iommu mapping. If the count is 263 * 0, the mapping will be removed from the iommu. 264 */ 265void ion_unmap_iommu(struct ion_client *client, struct ion_handle *handle, 266 int domain_num, int partition_num); 267 268 269/** 270 * ion_secure_heap - secure a heap 271 * 272 * @client - a client that has allocated from the heap heap_id 273 * @heap_id - heap id to secure. 274 * @version - version of content protection 275 * @data - extra data needed for protection 276 * 277 * Secure a heap 278 * Returns 0 on success 279 */ 280int ion_secure_heap(struct ion_device *dev, int heap_id, int version, 281 void *data); 282 283/** 284 * ion_unsecure_heap - un-secure a heap 285 * 286 * @client - a client that has allocated from the heap heap_id 287 * @heap_id - heap id to un-secure. 288 * @version - version of content protection 289 * @data - extra data needed for protection 290 * 291 * Un-secure a heap 292 * Returns 0 on success 293 */ 294int ion_unsecure_heap(struct ion_device *dev, int heap_id, int version, 295 void *data); 296 297/** 298 * msm_ion_do_cache_op - do cache operations. 299 * 300 * @client - pointer to ION client. 301 * @handle - pointer to buffer handle. 302 * @vaddr - virtual address to operate on. 303 * @len - Length of data to do cache operation on. 304 * @cmd - Cache operation to perform: 305 * ION_IOC_CLEAN_CACHES 306 * ION_IOC_INV_CACHES 307 * ION_IOC_CLEAN_INV_CACHES 308 * 309 * Returns 0 on success 310 */ 311int msm_ion_do_cache_op(struct ion_client *client, struct ion_handle *handle, 312 void *vaddr, unsigned long len, unsigned int cmd); 313 314/** 315 * msm_ion_secure_heap - secure a heap. Wrapper around ion_secure_heap. 316 * 317 * @heap_id - heap id to secure. 318 * 319 * Secure a heap 320 * Returns 0 on success 321 */ 322int msm_ion_secure_heap(int heap_id); 323 324/** 325 * msm_ion_unsecure_heap - unsecure a heap. Wrapper around ion_unsecure_heap. 326 * 327 * @heap_id - heap id to secure. 328 * 329 * Un-secure a heap 330 * Returns 0 on success 331 */ 332int msm_ion_unsecure_heap(int heap_id); 333 334/** 335 * msm_ion_secure_heap_2_0 - secure a heap using 2.0 APIs 336 * Wrapper around ion_secure_heap. 337 * 338 * @heap_id - heap id to secure. 339 * @usage - usage hint to TZ 340 * 341 * Secure a heap 342 * Returns 0 on success 343 */ 344int msm_ion_secure_heap_2_0(int heap_id, enum cp_mem_usage usage); 345 346/** 347 * msm_ion_unsecure_heap - unsecure a heap secured with 3.0 APIs. 348 * Wrapper around ion_unsecure_heap. 349 * 350 * @heap_id - heap id to secure. 351 * @usage - usage hint to TZ 352 * 353 * Un-secure a heap 354 * Returns 0 on success 355 */ 356int msm_ion_unsecure_heap_2_0(int heap_id, enum cp_mem_usage usage); 357 358/** 359 * msm_ion_secure_buffer - secure an individual buffer 360 * 361 * @client - client who has access to the buffer 362 * @handle - buffer to secure 363 * @usage - usage hint to TZ 364 * @flags - flags for the securing 365 */ 366int msm_ion_secure_buffer(struct ion_client *client, struct ion_handle *handle, 367 enum cp_mem_usage usage, int flags); 368 369/** 370 * msm_ion_unsecure_buffer - unsecure an individual buffer 371 * 372 * @client - client who has access to the buffer 373 * @handle - buffer to secure 374 */ 375int msm_ion_unsecure_buffer(struct ion_client *client, 376 struct ion_handle *handle); 377#else 378static inline struct ion_client *msm_ion_client_create(unsigned int heap_mask, 379 const char *name) 380{ 381 return ERR_PTR(-ENODEV); 382} 383 384static inline int ion_map_iommu(struct ion_client *client, 385 struct ion_handle *handle, int domain_num, 386 int partition_num, unsigned long align, 387 unsigned long iova_length, unsigned long *iova, 388 unsigned long *buffer_size, 389 unsigned long flags, 390 unsigned long iommu_flags) 391{ 392 return -ENODEV; 393} 394 395static inline int ion_handle_get_size(struct ion_client *client, 396 struct ion_handle *handle, unsigned long *size) 397{ 398 return -ENODEV; 399} 400 401static inline void ion_unmap_iommu(struct ion_client *client, 402 struct ion_handle *handle, int domain_num, 403 int partition_num) 404{ 405 return; 406} 407 408static inline int ion_secure_heap(struct ion_device *dev, int heap_id, 409 int version, void *data) 410{ 411 return -ENODEV; 412 413} 414 415static inline int ion_unsecure_heap(struct ion_device *dev, int heap_id, 416 int version, void *data) 417{ 418 return -ENODEV; 419} 420 421static inline void ion_mark_dangling_buffers_locked(struct ion_device *dev) 422{ 423} 424 425static inline int msm_ion_do_cache_op(struct ion_client *client, 426 struct ion_handle *handle, void *vaddr, 427 unsigned long len, unsigned int cmd) 428{ 429 return -ENODEV; 430} 431 432static inline int msm_ion_secure_heap(int heap_id) 433{ 434 return -ENODEV; 435 436} 437 438static inline int msm_ion_unsecure_heap(int heap_id) 439{ 440 return -ENODEV; 441} 442 443static inline int msm_ion_secure_heap_2_0(int heap_id, enum cp_mem_usage usage) 444{ 445 return -ENODEV; 446} 447 448static inline int msm_ion_unsecure_heap_2_0(int heap_id, 449 enum cp_mem_usage usage) 450{ 451 return -ENODEV; 452} 453 454static inline int msm_ion_secure_buffer(struct ion_client *client, 455 struct ion_handle *handle, 456 enum cp_mem_usage usage, 457 int flags) 458{ 459 return -ENODEV; 460} 461 462static inline int msm_ion_unsecure_buffer(struct ion_client *client, 463 struct ion_handle *handle) 464{ 465 return -ENODEV; 466} 467#endif /* CONFIG_ION */ 468 469#endif /* __KERNEL */ 470 471/* struct ion_flush_data - data passed to ion for flushing caches 472 * 473 * @handle: handle with data to flush 474 * @fd: fd to flush 475 * @vaddr: userspace virtual address mapped with mmap 476 * @offset: offset into the handle to flush 477 * @length: length of handle to flush 478 * 479 * Performs cache operations on the handle. If p is the start address 480 * of the handle, p + offset through p + offset + length will have 481 * the cache operations performed 482 */ 483struct ion_flush_data { 484 struct ion_handle *handle; 485 int fd; 486 void *vaddr; 487 unsigned int offset; 488 unsigned int length; 489}; 490 491#define ION_IOC_MSM_MAGIC 'M' 492 493/** 494 * DOC: ION_IOC_CLEAN_CACHES - clean the caches 495 * 496 * Clean the caches of the handle specified. 497 */ 498#define ION_IOC_CLEAN_CACHES _IOWR(ION_IOC_MSM_MAGIC, 0, \ 499 struct ion_flush_data) 500/** 501 * DOC: ION_IOC_INV_CACHES - invalidate the caches 502 * 503 * Invalidate the caches of the handle specified. 504 */ 505#define ION_IOC_INV_CACHES _IOWR(ION_IOC_MSM_MAGIC, 1, \ 506 struct ion_flush_data) 507/** 508 * DOC: ION_IOC_CLEAN_INV_CACHES - clean and invalidate the caches 509 * 510 * Clean and invalidate the caches of the handle specified. 511 */ 512#define ION_IOC_CLEAN_INV_CACHES _IOWR(ION_IOC_MSM_MAGIC, 2, \ 513 struct ion_flush_data) 514 515#endif 516