1 /*
2  * Copyright 2021 The Hafnium Authors.
3  *
4  * Use of this source code is governed by a BSD-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/BSD-3-Clause.
7  */
8 
9 #pragma once
10 
11 #include "vmapi/hf/ffa.h"
12 
13 /*
14  * A number of pages that is large enough that it must take two fragments to
15  * share.
16  */
17 #define FRAGMENTED_SHARE_PAGE_COUNT \
18 	(PAGE_SIZE / sizeof(struct ffa_memory_region_constituent))
19 
20 #define EXPECT_FFA_ERROR(value, ffa_error)                 \
21 	do {                                               \
22 		struct ffa_value v = (value);              \
23 		EXPECT_EQ(v.func, FFA_ERROR_32);           \
24 		EXPECT_EQ(ffa_error_code(v), (ffa_error)); \
25 	} while (0)
26 
27 #define SERVICE_PARTITION_INFO_GET(service_name, uuid)                         \
28 	struct ffa_partition_info *service_name(void *recv)                    \
29 	{                                                                      \
30 		static struct ffa_partition_info partition;                    \
31 		static bool is_set = false;                                    \
32 		struct ffa_uuid to_get_uuid = uuid;                            \
33 		if (!is_set) {                                                 \
34 			ASSERT_EQ(get_ffa_partition_info(to_get_uuid,          \
35 							 &partition, 1, recv), \
36 				  1);                                          \
37 			is_set = true;                                         \
38 		}                                                              \
39 		return &partition;                                             \
40 	}
41 
42 /*
43  * The bit 15 of the FF-A ID indicates whether the partition is executing
44  * in the normal world, in case it is a Virtual Machine (VM); or in the
45  * secure world, in case it is a Secure Partition (SP).
46  *
47  * If bit 15 is set partition is an SP; if bit 15 is clear partition is
48  * a VM.
49  */
50 #define SP_ID_MASK 0x1U << 15
51 #define SP_ID(x) ((x) | SP_ID_MASK)
52 #define VM_ID(x) ((x) & ~SP_ID_MASK)
53 
54 /*
55  * Implementation-defined maximum registers that may be used in a
56  * direct message response.
57  */
58 #define MAX_MSG_SIZE (14 * sizeof(uint64_t))
59 
60 struct mailbox_buffers {
61 	void *send;
62 	void *recv;
63 };
64 
65 struct mailbox_buffers set_up_mailbox(void);
66 void mailbox_unmap_buffers(struct mailbox_buffers *mb);
67 void mailbox_receive_retry(void *payload, size_t payload_size,
68 			   const void *recv_buf,
69 			   struct ffa_partition_rxtx_header *header);
70 ffa_memory_handle_t send_memory_and_retrieve_request_multi_receiver(
71 	uint32_t share_func, void *tx_buffer, ffa_id_t sender,
72 	struct ffa_memory_region_constituent constituents[],
73 	uint32_t constituent_count, struct ffa_memory_access receivers_send[],
74 	uint32_t receivers_send_count,
75 	struct ffa_memory_access receivers_retrieve[],
76 	uint32_t receivers_retrieve_count, ffa_memory_region_flags_t send_flags,
77 	ffa_memory_region_flags_t retrieve_flags,
78 	enum ffa_memory_type send_memory_type,
79 	enum ffa_memory_type receive_memory_type,
80 	enum ffa_memory_cacheability send_cacheability,
81 	enum ffa_memory_cacheability receive_cacheability);
82 ffa_memory_handle_t send_memory_and_retrieve_request(
83 	uint32_t share_func, void *tx_buffer, ffa_id_t sender,
84 	ffa_id_t recipient, struct ffa_memory_region_constituent constituents[],
85 	uint32_t constituent_count, ffa_memory_region_flags_t send_flags,
86 	ffa_memory_region_flags_t retrieve_flags,
87 	enum ffa_data_access send_data_access,
88 	enum ffa_data_access retrieve_data_access,
89 	enum ffa_instruction_access send_instruction_access,
90 	enum ffa_instruction_access retrieve_instruction_access,
91 	enum ffa_memory_type send_memory_type,
92 	enum ffa_memory_type receive_memory_type,
93 	enum ffa_memory_cacheability send_cacheability,
94 	enum ffa_memory_cacheability receive_cacheability);
95 ffa_memory_handle_t send_memory_and_retrieve_request_force_fragmented(
96 	uint32_t share_func, void *tx_buffer, ffa_id_t sender,
97 	ffa_id_t recipient, struct ffa_memory_region_constituent constituents[],
98 	uint32_t constituent_count, ffa_memory_region_flags_t flags,
99 	enum ffa_data_access send_data_access,
100 	enum ffa_data_access retrieve_data_access,
101 	enum ffa_instruction_access send_instruction_access,
102 	enum ffa_instruction_access retrieve_instruction_access);
103 void send_retrieve_request_single_receiver(
104 	void *send, ffa_memory_handle_t handle, ffa_id_t sender,
105 	ffa_id_t receiver, uint32_t tag, ffa_memory_region_flags_t flags,
106 	enum ffa_data_access data_access,
107 	enum ffa_instruction_access instruction_access,
108 	enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
109 	enum ffa_memory_shareability shareability,
110 	struct ffa_memory_access_impdef *impdef_val);
111 void send_retrieve_request(
112 	void *send, ffa_memory_handle_t handle, ffa_id_t sender,
113 	struct ffa_memory_access receivers[], uint32_t receiver_count,
114 	uint32_t tag, ffa_memory_region_flags_t flags,
115 	enum ffa_memory_type type, enum ffa_memory_cacheability cacheability,
116 	enum ffa_memory_shareability shareability, ffa_id_t recipient);
117 void send_fragmented_memory_region(
118 	struct ffa_value *send_ret, void *tx_buffer,
119 	struct ffa_memory_region_constituent constituents[],
120 	uint32_t constituent_count, uint32_t remaining_constituent_count,
121 	uint32_t sent_length, uint32_t total_length,
122 	ffa_memory_handle_t *handle,
123 	enum ffa_memory_handle_allocator allocator);
124 void memory_region_desc_from_rx_fragments(uint32_t fragment_length,
125 					  uint32_t total_length,
126 					  ffa_memory_handle_t handle,
127 					  void *memory_region, void *recv_buf,
128 					  uint32_t memory_region_max_size);
129 void retrieve_memory(void *recv_buf, ffa_memory_handle_t handle,
130 		     struct ffa_memory_region *memory_region_ret,
131 		     size_t memory_region_max_size, uint32_t msg_size);
132 ffa_id_t retrieve_memory_from_message(
133 	void *recv_buf, void *send_buf, ffa_memory_handle_t *handle,
134 	struct ffa_memory_region *memory_region_ret,
135 	size_t memory_region_max_size);
136 ffa_id_t retrieve_memory_from_message_expect_fail(
137 	void *recv_buf, void *send_buf, enum ffa_error expected_error);
138 
139 ffa_vm_count_t get_ffa_partition_info(struct ffa_uuid uuid,
140 				      struct ffa_partition_info infos[],
141 				      size_t info_len, void *recv);
142 
143 struct ffa_boot_info_header *get_boot_info_header(void);
144 void dump_boot_info(struct ffa_boot_info_header *boot_info_header);
145 struct ffa_boot_info_desc *get_boot_info_desc(
146 	struct ffa_boot_info_header *boot_info_heade, uint8_t type,
147 	uint8_t type_id);
148 
149 struct ffa_value send_indirect_message(ffa_id_t sender, ffa_id_t receiver,
150 				       void *send_buf, const void *payload,
151 				       size_t payload_size,
152 				       uint32_t send_flags);
153 
154 struct ffa_value send_indirect_message_v1_1(ffa_id_t sender, ffa_id_t receiver,
155 					    void *send_buf, const void *payload,
156 					    size_t payload_size,
157 					    uint32_t send_flags);
158 
159 struct ffa_value send_indirect_message_with_uuid(
160 	ffa_id_t sender, ffa_id_t receiver, void *send_buf, const void *payload,
161 	size_t payload_size, struct ffa_uuid uuid, uint32_t send_flags);
162 
163 struct ffa_partition_rxtx_header receive_indirect_message(void *payload,
164 							  size_t payload_size,
165 							  const void *recv_buf);
166 
167 bool ffa_partition_info_regs_get_part_info(
168 	struct ffa_value args, uint8_t idx,
169 	struct ffa_partition_info *partition_info);
170 
171 void update_mm_security_state(struct ffa_composite_memory_region *composite,
172 			      ffa_memory_attributes_t attributes);
173 
174 uint64_t get_shared_page_from_message(void *recv_buf, void *send_buf,
175 				      void *retrieve_buffer,
176 				      ffa_memory_handle_t *handle);
177 
178 ffa_memory_handle_t share_page_with_endpoints(uint64_t page,
179 					      ffa_id_t receivers_ids[],
180 					      size_t receivers_count,
181 					      void *send_buf);
182