1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2020-2024, Arm Limited.
4  */
5 #ifndef __KERNEL_SECURE_PARTITION_H
6 #define __KERNEL_SECURE_PARTITION_H
7 
8 #include <assert.h>
9 #include <config.h>
10 #include <ffa.h>
11 #include <kernel/embedded_ts.h>
12 #include <kernel/thread_spmc.h>
13 #include <kernel/user_mode_ctx_struct.h>
14 #include <mm/sp_mem.h>
15 #include <stdint.h>
16 #include <tee_api_types.h>
17 #include <tee/entry_std.h>
18 
19 TAILQ_HEAD(sp_sessions_head, sp_session);
20 
21 enum sp_status { sp_idle, sp_busy, sp_preempted, sp_dead };
22 
23 struct sp_session {
24 	struct ffa_rxtx rxtx;
25 	enum sp_status state;
26 	uint16_t endpoint_id;
27 	uint16_t caller_id;
28 	uint32_t boot_order;
29 	struct ts_session ts_sess;
30 	unsigned int spinlock;
31 	const void *fdt;
32 	bool is_initialized;
33 	TEE_UUID ffa_uuid;
34 	uint32_t ns_int_mode;
35 	uint32_t ns_int_mode_inherited;
36 	uint32_t props;
37 	TAILQ_ENTRY(sp_session) link;
38 };
39 
40 struct sp_ctx {
41 	struct thread_ctx_regs sp_regs;
42 	struct sp_session *open_session;
43 	struct user_mode_ctx uctx;
44 	struct ts_ctx ts_ctx;
45 };
46 
47 struct sp_image {
48 	struct embedded_ts image;
49 	const void *fdt;
50 };
51 
52 #ifdef CFG_SECURE_PARTITION
53 bool is_sp_ctx(struct ts_ctx *ctx);
54 #else
is_sp_ctx(struct ts_ctx * ctx __unused)55 static inline bool is_sp_ctx(struct ts_ctx *ctx __unused)
56 {
57 	return false;
58 }
59 #endif
60 
61 static inline struct sp_session *__noprof
to_sp_session(struct ts_session * sess)62 to_sp_session(struct ts_session *sess)
63 {
64 	assert(is_sp_ctx(sess->ctx));
65 	return container_of(sess, struct sp_session, ts_sess);
66 }
67 
to_sp_ctx(struct ts_ctx * ctx)68 static inline struct sp_ctx *to_sp_ctx(struct ts_ctx *ctx)
69 {
70 	assert(is_sp_ctx(ctx));
71 	return container_of(ctx, struct sp_ctx, ts_ctx);
72 }
73 
74 struct sp_session *sp_get_session(uint32_t session_id);
75 TEE_Result sp_enter(struct thread_smc_args *args, struct sp_session *sp);
76 TEE_Result sp_partition_info_get(uint32_t ffa_vers, void *buf, size_t buf_size,
77 				 const TEE_UUID *ffa_uuid, size_t *elem_count,
78 				 bool count_only);
79 bool sp_has_exclusive_access(struct sp_mem_map_region *mem,
80 			     struct user_mode_ctx *uctx);
81 TEE_Result sp_map_shared(struct sp_session *s,
82 			 struct sp_mem_receiver *receiver,
83 			 struct sp_mem *mem,
84 			 uint64_t *va);
85 TEE_Result sp_unmap_ffa_regions(struct sp_session *s, struct sp_mem *smem);
86 
87 #define for_each_secure_partition(_sp) \
88 	SCATTERED_ARRAY_FOREACH(_sp, sp_images, struct sp_image)
89 
90 struct fip_sp {
91 	struct sp_image sp_img;
92 	STAILQ_ENTRY(fip_sp) link;
93 };
94 
95 STAILQ_HEAD(fip_sp_head, fip_sp);
96 extern struct fip_sp_head fip_sp_list;
97 
98 #define for_each_fip_sp(_sp) \
99 	STAILQ_FOREACH(_sp, &fip_sp_list, link)
100 
101 #endif /* __KERNEL_SECURE_PARTITION_H */
102