xref: /optee_os/core/include/mm/tee_mm.h (revision ba6d8df98e3cf376aab45d0d958204c498a94123)
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 <types_ext.h>
33 
34 /* Define to indicate default pool initiation */
35 #define TEE_MM_POOL_NO_FLAGS            0
36 /* Flag to indicate that memory is allocated from hi address to low address */
37 #define TEE_MM_POOL_HI_ALLOC            (1u << 0)
38 
39 struct _tee_mm_entry_t {
40 	struct _tee_mm_pool_t *pool;
41 	struct _tee_mm_entry_t *next;
42 	uint32_t offset;	/* offset in pages/sections */
43 	uint32_t size;		/* size in pages/sections */
44 };
45 typedef struct _tee_mm_entry_t tee_mm_entry_t;
46 
47 struct _tee_mm_pool_t {
48 	tee_mm_entry_t *entry;
49 	paddr_t lo;		/* low boundary of the pool */
50 	paddr_t hi;		/* high boundary of the pool */
51 	uint32_t flags;		/* Config flags for the pool */
52 	uint8_t shift;		/* size shift */
53 	unsigned int lock;
54 #ifdef CFG_WITH_STATS
55 	size_t max_allocated;
56 #endif
57 };
58 typedef struct _tee_mm_pool_t tee_mm_pool_t;
59 
60 /* Physical Secure DDR pool */
61 extern tee_mm_pool_t tee_mm_sec_ddr;
62 
63 /* Virtual eSRAM pool */
64 extern tee_mm_pool_t tee_mm_vcore;
65 
66 /*
67  * Returns a pointer to the mm covering the supplied address,
68  * if no mm is found NULL is returned.
69  */
70 tee_mm_entry_t *tee_mm_find(const tee_mm_pool_t *pool, paddr_t addr);
71 
72 /*
73  * Validates that an address (addr) is part of the secure virtual memory
74  * Returns false if not valid, true if valid
75  * NOTE: This function is executed in abort mode.
76  *       Please take care of stack usage
77  */
78 static inline bool tee_mm_validate(const tee_mm_pool_t *pool, paddr_t addr)
79 {
80 	return tee_mm_find(pool, addr) != 0;
81 }
82 
83 /*
84  * Returns a virtual address to the start of the allocated memory
85  * for the mm entry.
86  */
87 uintptr_t tee_mm_get_smem(const tee_mm_entry_t *mm);
88 
89 /* Init managed memory area */
90 bool tee_mm_init(tee_mm_pool_t *pool, paddr_t lo, paddr_t hi, uint8_t shift,
91 		 uint32_t flags);
92 
93 /* Kill managed memory area*/
94 void tee_mm_final(tee_mm_pool_t *pool);
95 
96 /*
97  * Allocates size number of bytes in the paged virtual address space
98  * Returns a handle to the memory. The handle is used as an input to
99  * the tee_mm_free function.
100  */
101 tee_mm_entry_t *tee_mm_alloc(tee_mm_pool_t *pool, size_t size);
102 
103 /* Allocate supplied memory range if it's free */
104 tee_mm_entry_t *tee_mm_alloc2(tee_mm_pool_t *pool, paddr_t base, size_t size);
105 
106 /*
107  * Frees the entry in the paged virtual address space pointed to by the
108  * input parameter p
109  */
110 void tee_mm_free(tee_mm_entry_t *p);
111 
112 /* Returns size in sections or pages */
113 static inline size_t tee_mm_get_size(tee_mm_entry_t *p)
114 {
115 	return p->size;
116 }
117 
118 /* Returns offset from start of area in sections or pages */
119 static inline uint32_t tee_mm_get_offset(tee_mm_entry_t *p)
120 {
121 	return p->offset;
122 }
123 
124 /* Return size of the mm entry in bytes */
125 size_t tee_mm_get_bytes(const tee_mm_entry_t *mm);
126 
127 bool tee_mm_addr_is_within_range(tee_mm_pool_t *pool, paddr_t addr);
128 
129 bool tee_mm_is_empty(tee_mm_pool_t *pool);
130 
131 #ifdef CFG_WITH_STATS
132 void tee_mm_get_pool_stats(tee_mm_pool_t *pool, struct malloc_stats *stats,
133 			   bool reset);
134 #endif
135 
136 #endif
137