11bb92983SJerome Forissier /* SPDX-License-Identifier: BSD-2-Clause */
2b0104773SPascal Brand /*
3b0104773SPascal Brand * Copyright (c) 2014, STMicroelectronics International N.V.
4b0104773SPascal Brand */
5b0104773SPascal Brand
6fbe66cf8SEtienne Carriere #ifndef __MM_TEE_MM_H
7fbe66cf8SEtienne Carriere #define __MM_TEE_MM_H
8b0104773SPascal Brand
952f8b816SJerome Forissier #include <malloc.h>
10a05577eaSEtienne Carriere #include <pta_stats.h>
112ffdd194SJens Wiklander #include <types_ext.h>
12b0104773SPascal Brand
13f17dfc0cSPascal Brand /* Define to indicate default pool initiation */
14b462b681SJens Wiklander #define TEE_MM_POOL_NO_FLAGS MAF_NULL
15f17dfc0cSPascal Brand /* Flag to indicate that memory is allocated from hi address to low address */
16b462b681SJens Wiklander #define TEE_MM_POOL_HI_ALLOC MAF_HI_ALLOC
17a386ba2eSVolodymyr Babchuk /* Flag to indicate that pool should use nex_malloc instead of malloc */
18b462b681SJens Wiklander #define TEE_MM_POOL_NEX_MALLOC MAF_NEX
19f17dfc0cSPascal Brand
20f17dfc0cSPascal Brand struct _tee_mm_entry_t {
21f17dfc0cSPascal Brand struct _tee_mm_pool_t *pool;
22f17dfc0cSPascal Brand struct _tee_mm_entry_t *next;
23f17dfc0cSPascal Brand uint32_t offset; /* offset in pages/sections */
24f17dfc0cSPascal Brand uint32_t size; /* size in pages/sections */
25f17dfc0cSPascal Brand };
26f17dfc0cSPascal Brand typedef struct _tee_mm_entry_t tee_mm_entry_t;
27f17dfc0cSPascal Brand
28f17dfc0cSPascal Brand struct _tee_mm_pool_t {
29f17dfc0cSPascal Brand tee_mm_entry_t *entry;
3036d5a313SPhilip Attfield paddr_t lo; /* low boundary of the pool */
312380d700SLionel Debieve paddr_size_t size; /* pool size */
32f17dfc0cSPascal Brand uint32_t flags; /* Config flags for the pool */
33f17dfc0cSPascal Brand uint8_t shift; /* size shift */
3469306abdSVolodymyr Babchuk unsigned int lock;
35f6d80bd6SPascal Brand #ifdef CFG_WITH_STATS
36f6d80bd6SPascal Brand size_t max_allocated;
37f6d80bd6SPascal Brand #endif
38f17dfc0cSPascal Brand };
39f17dfc0cSPascal Brand typedef struct _tee_mm_pool_t tee_mm_pool_t;
40f17dfc0cSPascal Brand
41f17dfc0cSPascal Brand /*
42f17dfc0cSPascal Brand * Returns a pointer to the mm covering the supplied address,
43f17dfc0cSPascal Brand * if no mm is found NULL is returned.
44f17dfc0cSPascal Brand */
4536d5a313SPhilip Attfield tee_mm_entry_t *tee_mm_find(const tee_mm_pool_t *pool, paddr_t addr);
46f17dfc0cSPascal Brand
47f17dfc0cSPascal Brand /*
48f17dfc0cSPascal Brand * Validates that an address (addr) is part of the secure virtual memory
49f17dfc0cSPascal Brand * Returns false if not valid, true if valid
50f17dfc0cSPascal Brand * NOTE: This function is executed in abort mode.
51f17dfc0cSPascal Brand * Please take care of stack usage
52f17dfc0cSPascal Brand */
tee_mm_validate(const tee_mm_pool_t * pool,paddr_t addr)5336d5a313SPhilip Attfield static inline bool tee_mm_validate(const tee_mm_pool_t *pool, paddr_t addr)
54f17dfc0cSPascal Brand {
55f17dfc0cSPascal Brand return tee_mm_find(pool, addr) != 0;
56f17dfc0cSPascal Brand }
57f17dfc0cSPascal Brand
58f17dfc0cSPascal Brand /*
59f17dfc0cSPascal Brand * Returns a virtual address to the start of the allocated memory
60f17dfc0cSPascal Brand * for the mm entry.
61f17dfc0cSPascal Brand */
62f17dfc0cSPascal Brand uintptr_t tee_mm_get_smem(const tee_mm_entry_t *mm);
63f17dfc0cSPascal Brand
64f17dfc0cSPascal Brand /* Init managed memory area */
652380d700SLionel Debieve bool tee_mm_init(tee_mm_pool_t *pool, paddr_t lo, paddr_size_t size,
662380d700SLionel Debieve uint8_t shift, uint32_t flags);
67b0104773SPascal Brand
68f17dfc0cSPascal Brand /* Kill managed memory area*/
69b0104773SPascal Brand void tee_mm_final(tee_mm_pool_t *pool);
70b0104773SPascal Brand
71*5f76bc75SJens Wiklander tee_mm_entry_t *tee_mm_alloc_flags(tee_mm_pool_t *pool, size_t size,
72*5f76bc75SJens Wiklander uint32_t flags);
73*5f76bc75SJens Wiklander
74f17dfc0cSPascal Brand /*
75b0104773SPascal Brand * Allocates size number of bytes in the paged virtual address space
76b0104773SPascal Brand * Returns a handle to the memory. The handle is used as an input to
77b0104773SPascal Brand * the tee_mm_free function.
78f17dfc0cSPascal Brand */
tee_mm_alloc(tee_mm_pool_t * pool,size_t size)79*5f76bc75SJens Wiklander static inline tee_mm_entry_t *tee_mm_alloc(tee_mm_pool_t *pool, size_t size)
80*5f76bc75SJens Wiklander {
81*5f76bc75SJens Wiklander return tee_mm_alloc_flags(pool, size, MAF_NULL);
82*5f76bc75SJens Wiklander }
83b0104773SPascal Brand
84b0104773SPascal Brand /* Allocate supplied memory range if it's free */
8536d5a313SPhilip Attfield tee_mm_entry_t *tee_mm_alloc2(tee_mm_pool_t *pool, paddr_t base, size_t size);
86b0104773SPascal Brand
87f17dfc0cSPascal Brand /*
88b0104773SPascal Brand * Frees the entry in the paged virtual address space pointed to by the
89b0104773SPascal Brand * input parameter p
90f17dfc0cSPascal Brand */
91b0104773SPascal Brand void tee_mm_free(tee_mm_entry_t *p);
92b0104773SPascal Brand
93f17dfc0cSPascal Brand /* Returns size in sections or pages */
tee_mm_get_size(tee_mm_entry_t * p)9436d5a313SPhilip Attfield static inline size_t tee_mm_get_size(tee_mm_entry_t *p)
95b0104773SPascal Brand {
96b0104773SPascal Brand return p->size;
97b0104773SPascal Brand }
98b0104773SPascal Brand
99f17dfc0cSPascal Brand /* Returns offset from start of area in sections or pages */
tee_mm_get_offset(tee_mm_entry_t * p)100b0104773SPascal Brand static inline uint32_t tee_mm_get_offset(tee_mm_entry_t *p)
101b0104773SPascal Brand {
102b0104773SPascal Brand return p->offset;
103b0104773SPascal Brand }
104b0104773SPascal Brand
105b0104773SPascal Brand /* Return size of the mm entry in bytes */
106b0104773SPascal Brand size_t tee_mm_get_bytes(const tee_mm_entry_t *mm);
107b0104773SPascal Brand
1082380d700SLionel Debieve bool tee_mm_addr_is_within_range(const tee_mm_pool_t *pool, paddr_t addr);
109b0104773SPascal Brand
110b0104773SPascal Brand bool tee_mm_is_empty(tee_mm_pool_t *pool);
111b0104773SPascal Brand
112f6d80bd6SPascal Brand #ifdef CFG_WITH_STATS
1132617f49fSEtienne Carriere void tee_mm_get_pool_stats(tee_mm_pool_t *pool, struct pta_stats_alloc *stats,
114f6d80bd6SPascal Brand bool reset);
115f6d80bd6SPascal Brand #endif
116f6d80bd6SPascal Brand
117b0104773SPascal Brand #endif
118