1 /* SPDX-License-Identifier: BSD-2-Clause */ 2 /* 3 * Copyright (c) 2014, STMicroelectronics International N.V. 4 */ 5 6 #ifndef TEE_MM_H 7 #define TEE_MM_H 8 9 #include <malloc.h> 10 #include <types_ext.h> 11 12 /* Define to indicate default pool initiation */ 13 #define TEE_MM_POOL_NO_FLAGS 0 14 /* Flag to indicate that memory is allocated from hi address to low address */ 15 #define TEE_MM_POOL_HI_ALLOC (1u << 0) 16 17 struct _tee_mm_entry_t { 18 struct _tee_mm_pool_t *pool; 19 struct _tee_mm_entry_t *next; 20 uint32_t offset; /* offset in pages/sections */ 21 uint32_t size; /* size in pages/sections */ 22 }; 23 typedef struct _tee_mm_entry_t tee_mm_entry_t; 24 25 struct _tee_mm_pool_t { 26 tee_mm_entry_t *entry; 27 paddr_t lo; /* low boundary of the pool */ 28 paddr_t hi; /* high boundary of the pool */ 29 uint32_t flags; /* Config flags for the pool */ 30 uint8_t shift; /* size shift */ 31 unsigned int lock; 32 #ifdef CFG_WITH_STATS 33 size_t max_allocated; 34 #endif 35 }; 36 typedef struct _tee_mm_pool_t tee_mm_pool_t; 37 38 /* Physical Secure DDR pool */ 39 extern tee_mm_pool_t tee_mm_sec_ddr; 40 41 /* Virtual eSRAM pool */ 42 extern tee_mm_pool_t tee_mm_vcore; 43 44 /* Shared memory pool */ 45 extern tee_mm_pool_t tee_mm_shm; 46 47 /* 48 * Returns a pointer to the mm covering the supplied address, 49 * if no mm is found NULL is returned. 50 */ 51 tee_mm_entry_t *tee_mm_find(const tee_mm_pool_t *pool, paddr_t addr); 52 53 /* 54 * Validates that an address (addr) is part of the secure virtual memory 55 * Returns false if not valid, true if valid 56 * NOTE: This function is executed in abort mode. 57 * Please take care of stack usage 58 */ 59 static inline bool tee_mm_validate(const tee_mm_pool_t *pool, paddr_t addr) 60 { 61 return tee_mm_find(pool, addr) != 0; 62 } 63 64 /* 65 * Returns a virtual address to the start of the allocated memory 66 * for the mm entry. 67 */ 68 uintptr_t tee_mm_get_smem(const tee_mm_entry_t *mm); 69 70 /* Init managed memory area */ 71 bool tee_mm_init(tee_mm_pool_t *pool, paddr_t lo, paddr_t hi, uint8_t shift, 72 uint32_t flags); 73 74 /* Kill managed memory area*/ 75 void tee_mm_final(tee_mm_pool_t *pool); 76 77 /* 78 * Allocates size number of bytes in the paged virtual address space 79 * Returns a handle to the memory. The handle is used as an input to 80 * the tee_mm_free function. 81 */ 82 tee_mm_entry_t *tee_mm_alloc(tee_mm_pool_t *pool, size_t size); 83 84 /* Allocate supplied memory range if it's free */ 85 tee_mm_entry_t *tee_mm_alloc2(tee_mm_pool_t *pool, paddr_t base, size_t size); 86 87 /* 88 * Frees the entry in the paged virtual address space pointed to by the 89 * input parameter p 90 */ 91 void tee_mm_free(tee_mm_entry_t *p); 92 93 /* Returns size in sections or pages */ 94 static inline size_t tee_mm_get_size(tee_mm_entry_t *p) 95 { 96 return p->size; 97 } 98 99 /* Returns offset from start of area in sections or pages */ 100 static inline uint32_t tee_mm_get_offset(tee_mm_entry_t *p) 101 { 102 return p->offset; 103 } 104 105 /* Return size of the mm entry in bytes */ 106 size_t tee_mm_get_bytes(const tee_mm_entry_t *mm); 107 108 bool tee_mm_addr_is_within_range(tee_mm_pool_t *pool, paddr_t addr); 109 110 bool tee_mm_is_empty(tee_mm_pool_t *pool); 111 112 #ifdef CFG_WITH_STATS 113 void tee_mm_get_pool_stats(tee_mm_pool_t *pool, struct malloc_stats *stats, 114 bool reset); 115 #endif 116 117 #endif 118