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