1 /* 2 * Copyright (c) 2014, STMicroelectronics International N.V. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef TEE_MM_H 29 #define TEE_MM_H 30 31 #include <malloc.h> 32 #include <stdint.h> 33 #include <stdbool.h> 34 #include <kernel/tee_common_unpg.h> 35 36 /* Define to indicate default pool initiation */ 37 #define TEE_MM_POOL_NO_FLAGS 0 38 /* Flag to indicate that memory is allocated from hi address to low address */ 39 #define TEE_MM_POOL_HI_ALLOC (1u << 0) 40 41 struct _tee_mm_entry_t { 42 struct _tee_mm_pool_t *pool; 43 struct _tee_mm_entry_t *next; 44 uint32_t offset; /* offset in pages/sections */ 45 uint32_t size; /* size in pages/sections */ 46 }; 47 typedef struct _tee_mm_entry_t tee_mm_entry_t; 48 49 struct _tee_mm_pool_t { 50 tee_mm_entry_t *entry; 51 uint32_t lo; /* low boundery pf the pool */ 52 uint32_t hi; /* high boundery pf the pool */ 53 uint32_t flags; /* Config flags for the pool */ 54 uint8_t shift; /* size shift */ 55 #ifdef CFG_WITH_STATS 56 size_t max_allocated; 57 #endif 58 }; 59 typedef struct _tee_mm_pool_t tee_mm_pool_t; 60 61 /* Physical Secure DDR pool */ 62 extern tee_mm_pool_t tee_mm_sec_ddr; 63 64 /* Virtual eSRAM pool */ 65 extern tee_mm_pool_t tee_mm_vcore; 66 67 /* 68 * Returns a pointer to the mm covering the supplied address, 69 * if no mm is found NULL is returned. 70 */ 71 tee_mm_entry_t *tee_mm_find(const tee_mm_pool_t *pool, uint32_t addr); 72 73 /* 74 * Validates that an address (addr) is part of the secure virtual memory 75 * Returns false if not valid, true if valid 76 * NOTE: This function is executed in abort mode. 77 * Please take care of stack usage 78 */ 79 static inline bool tee_mm_validate(const tee_mm_pool_t *pool, uint32_t addr) 80 { 81 return tee_mm_find(pool, addr) != 0; 82 } 83 84 /* 85 * Returns a virtual address to the start of the allocated memory 86 * for the mm entry. 87 */ 88 uintptr_t tee_mm_get_smem(const tee_mm_entry_t *mm); 89 90 /* Init managed memory area */ 91 bool tee_mm_init(tee_mm_pool_t *pool, uint32_t lo, uint32_t hi, uint8_t shift, 92 uint32_t flags); 93 94 /* Kill managed memory area*/ 95 void tee_mm_final(tee_mm_pool_t *pool); 96 97 /* 98 * Allocates size number of bytes in the paged virtual address space 99 * Returns a handle to the memory. The handle is used as an input to 100 * the tee_mm_free function. 101 */ 102 tee_mm_entry_t *tee_mm_alloc(tee_mm_pool_t *pool, uint32_t size); 103 104 /* Allocate supplied memory range if it's free */ 105 tee_mm_entry_t *tee_mm_alloc2(tee_mm_pool_t *pool, tee_vaddr_t base, 106 size_t size); 107 108 /* 109 * Frees the entry in the paged virtual address space pointed to by the 110 * input parameter p 111 */ 112 void tee_mm_free(tee_mm_entry_t *p); 113 114 /* Returns size in sections or pages */ 115 static inline uint32_t tee_mm_get_size(tee_mm_entry_t *p) 116 { 117 return p->size; 118 } 119 120 /* Returns offset from start of area in sections or pages */ 121 static inline uint32_t tee_mm_get_offset(tee_mm_entry_t *p) 122 { 123 return p->offset; 124 } 125 126 /* Return size of the mm entry in bytes */ 127 size_t tee_mm_get_bytes(const tee_mm_entry_t *mm); 128 129 bool tee_mm_addr_is_within_range(tee_mm_pool_t *pool, uint32_t addr); 130 131 bool tee_mm_is_empty(tee_mm_pool_t *pool); 132 133 #ifdef CFG_WITH_STATS 134 void tee_mm_get_pool_stats(tee_mm_pool_t *pool, struct malloc_stats *stats, 135 bool reset); 136 #endif 137 138 #endif 139