1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2020-2022, 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 struct sp_name_value_pair {
22 	uint32_t name[4];
23 	uint64_t value;
24 	uint64_t size;
25 };
26 
27 /* SP entry arguments passed to SP image: see ABI in FF-A specification */
28 struct sp_ffa_init_info {
29 	uint32_t magic; /* FF-A */
30 	uint32_t count; /* Count of name value size pairs */
31 	struct sp_name_value_pair nvp[]; /* Array of name value size pairs */
32 };
33 
34 enum sp_status { sp_idle, sp_busy, sp_preempted, sp_dead };
35 
36 struct sp_session {
37 	struct ffa_rxtx rxtx;
38 	enum sp_status state;
39 	uint16_t endpoint_id;
40 	uint16_t caller_id;
41 	struct ts_session ts_sess;
42 	struct sp_ffa_init_info *info;
43 	unsigned int spinlock;
44 	const void *fdt;
45 	bool is_initialized;
46 	TAILQ_ENTRY(sp_session) link;
47 };
48 
49 struct sp_ctx {
50 	struct thread_ctx_regs sp_regs;
51 	struct sp_session *open_session;
52 	struct user_mode_ctx uctx;
53 	struct ts_ctx ts_ctx;
54 };
55 
56 struct sp_image {
57 	struct embedded_ts image;
58 	const void *fdt;
59 };
60 
61 #ifdef CFG_SECURE_PARTITION
62 bool is_sp_ctx(struct ts_ctx *ctx);
63 #else
is_sp_ctx(struct ts_ctx * ctx __unused)64 static inline bool is_sp_ctx(struct ts_ctx *ctx __unused)
65 {
66 	return false;
67 }
68 #endif
69 
70 static inline struct sp_session *__noprof
to_sp_session(struct ts_session * sess)71 to_sp_session(struct ts_session *sess)
72 {
73 	assert(is_sp_ctx(sess->ctx));
74 	return container_of(sess, struct sp_session, ts_sess);
75 }
76 
to_sp_ctx(struct ts_ctx * ctx)77 static inline struct sp_ctx *to_sp_ctx(struct ts_ctx *ctx)
78 {
79 	assert(is_sp_ctx(ctx));
80 	return container_of(ctx, struct sp_ctx, ts_ctx);
81 }
82 
83 struct sp_session *sp_get_session(uint32_t session_id);
84 TEE_Result sp_enter(struct thread_smc_args *args, struct sp_session *sp);
85 TEE_Result sp_partition_info_get_all(struct ffa_partition_info *fpi,
86 				     size_t *elem_count);
87 
88 TEE_Result sp_find_session_id(const TEE_UUID *uuid, uint32_t *session_id);
89 bool sp_has_exclusive_access(struct sp_mem_map_region *mem,
90 			     struct user_mode_ctx *uctx);
91 TEE_Result sp_map_shared(struct sp_session *s,
92 			 struct sp_mem_receiver *receiver,
93 			 struct sp_mem *mem,
94 			 uint64_t *va);
95 TEE_Result sp_unmap_ffa_regions(struct sp_session *s, struct sp_mem *smem);
96 
97 #define for_each_secure_partition(_sp) \
98 	SCATTERED_ARRAY_FOREACH(_sp, sp_images, struct sp_image)
99 
100 struct fip_sp {
101 	tee_mm_entry_t *mm;
102 	struct sp_image sp_img;
103 
104 	STAILQ_ENTRY(fip_sp) link;
105 };
106 
107 STAILQ_HEAD(fip_sp_head, fip_sp);
108 extern struct fip_sp_head fip_sp_list;
109 
110 #define for_each_fip_sp(_sp) \
111 	STAILQ_FOREACH(_sp, &fip_sp_list, link)
112 
113 #endif /* __KERNEL_SECURE_PARTITION_H */
114