xref: /optee_os/core/include/mm/pgt_cache.h (revision 7aa2bec847863b665853f1a397f95bcf9f2f5032)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2016, Linaro Limited
4  */
5 #ifndef MM_PGT_CACHE_H
6 #define MM_PGT_CACHE_H
7 
8 #ifdef CFG_WITH_LPAE
9 #define PGT_SIZE	(4 * 1024)
10 #define PGT_NUM_PGT_PER_PAGE	1
11 #else
12 #define PGT_SIZE	(1 * 1024)
13 #define PGT_NUM_PGT_PER_PAGE	4
14 #endif
15 
16 #include <assert.h>
17 #include <kernel/tee_ta_manager.h>
18 #include <sys/queue.h>
19 #include <types_ext.h>
20 #include <util.h>
21 
22 struct ts_ctx;
23 
24 struct pgt {
25 	void *tbl;
26 	vaddr_t vabase;
27 	struct ts_ctx *ctx;
28 	bool populated;
29 #if defined(CFG_PAGED_USER_TA)
30 	uint16_t num_used_entries;
31 #endif
32 #if defined(CFG_WITH_PAGER) && !defined(CFG_WITH_LPAE)
33 	struct pgt_parent *parent;
34 #endif
35 	SLIST_ENTRY(pgt) link;
36 };
37 
38 /*
39  * A proper value for PGT_CACHE_SIZE depends on many factors: CFG_WITH_LPAE,
40  * CFG_TA_ASLR, size of TA, size of memrefs passed to TA, CFG_ULIBS_SHARED and
41  * possibly others. The value is based on the number of threads as an indicator
42  * on how large the system might be.
43  */
44 #if CFG_NUM_THREADS < 2
45 #define PGT_CACHE_SIZE	4
46 #elif (CFG_NUM_THREADS == 2 && !defined(CFG_WITH_LPAE))
47 #define PGT_CACHE_SIZE	8
48 #else
49 #define PGT_CACHE_SIZE	ROUNDUP(CFG_NUM_THREADS * 2, PGT_NUM_PGT_PER_PAGE)
50 #endif
51 
52 SLIST_HEAD(pgt_cache, pgt);
53 
54 bool pgt_check_avail(struct vm_info *vm_info);
55 
56 void pgt_alloc(struct pgt_cache *pgt_cache, struct ts_ctx *owning_ctx,
57 	       struct vm_info *vm_info);
58 void pgt_free(struct pgt_cache *pgt_cache);
59 
60 void pgt_clear_ctx_range(struct pgt_cache *pgt_cache, struct ts_ctx *ctx,
61 			 vaddr_t begin, vaddr_t end);
62 void pgt_flush_ctx_range(struct pgt_cache *pgt_cache, struct ts_ctx *ctx,
63 			 vaddr_t begin, vaddr_t last);
64 
65 struct pgt *pgt_pop_from_cache_list(vaddr_t vabase, struct ts_ctx *ctx);
66 void pgt_push_to_cache_list(struct pgt *pgt);
67 
68 void pgt_init(void);
69 
70 void pgt_flush_ctx(struct ts_ctx *ctx);
71 
72 #if defined(CFG_PAGED_USER_TA)
73 static inline void pgt_inc_used_entries(struct pgt *pgt)
74 {
75 	pgt->num_used_entries++;
76 	assert(pgt->num_used_entries);
77 }
78 
79 static inline void pgt_dec_used_entries(struct pgt *pgt)
80 {
81 	assert(pgt->num_used_entries);
82 	pgt->num_used_entries--;
83 }
84 
85 static inline void pgt_set_used_entries(struct pgt *pgt, size_t val)
86 {
87 	pgt->num_used_entries = val;
88 }
89 
90 #else
91 static inline void pgt_inc_used_entries(struct pgt *pgt __unused)
92 {
93 }
94 
95 static inline void pgt_dec_used_entries(struct pgt *pgt __unused)
96 {
97 }
98 
99 static inline void pgt_set_used_entries(struct pgt *pgt __unused,
100 					size_t val __unused)
101 {
102 }
103 
104 #endif
105 
106 #endif /*MM_PGT_CACHE_H*/
107