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