1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2020-2021, 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 	TAILQ_ENTRY(sp_session) link;
45 };
46 
47 struct sp_ctx {
48 	struct thread_ctx_regs sp_regs;
49 	struct sp_session *open_session;
50 	struct user_mode_ctx uctx;
51 	struct ts_ctx ts_ctx;
52 };
53 
54 #ifdef CFG_SECURE_PARTITION
55 bool is_sp_ctx(struct ts_ctx *ctx);
56 #else
is_sp_ctx(struct ts_ctx * ctx __unused)57 static inline bool is_sp_ctx(struct ts_ctx *ctx __unused)
58 {
59 	return false;
60 }
61 #endif
62 
63 static inline struct sp_session *__noprof
to_sp_session(struct ts_session * sess)64 to_sp_session(struct ts_session *sess)
65 {
66 	assert(is_sp_ctx(sess->ctx));
67 	return container_of(sess, struct sp_session, ts_sess);
68 }
69 
to_sp_ctx(struct ts_ctx * ctx)70 static inline struct sp_ctx *to_sp_ctx(struct ts_ctx *ctx)
71 {
72 	assert(is_sp_ctx(ctx));
73 	return container_of(ctx, struct sp_ctx, ts_ctx);
74 }
75 
76 struct sp_session *sp_get_session(uint32_t session_id);
77 TEE_Result sp_enter(struct thread_smc_args *args, struct sp_session *sp);
78 TEE_Result sp_partition_info_get_all(struct ffa_partition_info *fpi,
79 				     size_t *elem_count);
80 
81 TEE_Result sp_find_session_id(const TEE_UUID *uuid, uint32_t *session_id);
82 bool sp_has_exclusive_access(struct sp_mem_map_region *mem,
83 			     struct user_mode_ctx *uctx);
84 TEE_Result sp_map_shared(struct sp_session *s,
85 			 struct sp_mem_receiver *receiver,
86 			 struct sp_mem *mem,
87 			 uint64_t *va);
88 TEE_Result sp_unmap_ffa_regions(struct sp_session *s, struct sp_mem *smem);
89 
90 #define for_each_secure_partition(_sp) \
91 	SCATTERED_ARRAY_FOREACH(_sp, sp_images, struct embedded_ts)
92 
93 #endif /* __KERNEL_SECURE_PARTITION_H */
94