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 thread_id;
28 uint16_t caller_id;
29 uint32_t boot_order;
30 struct ts_session ts_sess;
31 unsigned int spinlock;
32 const void *fdt;
33 bool is_initialized;
34 TEE_UUID ffa_uuid;
35 uint32_t ns_int_mode;
36 uint32_t ns_int_mode_inherited;
37 uint32_t props;
38 TAILQ_ENTRY(sp_session) link;
39 };
40
41 struct sp_ctx {
42 struct thread_ctx_regs sp_regs;
43 struct sp_session *open_session;
44 struct user_mode_ctx uctx;
45 struct ts_ctx ts_ctx;
46 };
47
48 struct sp_image {
49 struct embedded_ts image;
50 const void *fdt;
51 };
52
53 #ifdef CFG_SECURE_PARTITION
54 bool is_sp_ctx(struct ts_ctx *ctx);
55 #else
is_sp_ctx(struct ts_ctx * ctx __unused)56 static inline bool is_sp_ctx(struct ts_ctx *ctx __unused)
57 {
58 return false;
59 }
60 #endif
61
62 static inline struct sp_session *__noprof
to_sp_session(struct ts_session * sess)63 to_sp_session(struct ts_session *sess)
64 {
65 assert(is_sp_ctx(sess->ctx));
66 return container_of(sess, struct sp_session, ts_sess);
67 }
68
to_sp_ctx(struct ts_ctx * ctx)69 static inline struct sp_ctx *to_sp_ctx(struct ts_ctx *ctx)
70 {
71 assert(is_sp_ctx(ctx));
72 return container_of(ctx, struct sp_ctx, ts_ctx);
73 }
74
75 struct sp_session *sp_get_session(uint32_t session_id);
76 TEE_Result sp_enter(struct thread_smc_1_2_regs *args, struct sp_session *sp);
77 TEE_Result sp_partition_info_get(uint32_t ffa_vers, void *buf, size_t buf_size,
78 const uint32_t uuid_words[4],
79 size_t *elem_count, bool count_only);
80 bool sp_has_exclusive_access(struct sp_mem_map_region *mem,
81 struct user_mode_ctx *uctx);
82 TEE_Result sp_map_shared(struct sp_session *s,
83 struct sp_mem_receiver *receiver,
84 struct sp_mem *mem,
85 uint64_t *va);
86 TEE_Result sp_unmap_ffa_regions(struct sp_session *s, struct sp_mem *smem);
87
88 #define for_each_secure_partition(_sp) \
89 SCATTERED_ARRAY_FOREACH(_sp, sp_images, struct sp_image)
90
91 struct fip_sp {
92 struct sp_image sp_img;
93 STAILQ_ENTRY(fip_sp) link;
94 };
95
96 STAILQ_HEAD(fip_sp_head, fip_sp);
97 extern struct fip_sp_head fip_sp_list;
98
99 #define for_each_fip_sp(_sp) \
100 STAILQ_FOREACH(_sp, &fip_sp_list, link)
101
102 #endif /* __KERNEL_SECURE_PARTITION_H */
103