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