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