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