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 /*
28  * The bit 15 of the FF-A ID indicates whether the partition is executing
29  * in the normal world, in case it is a Virtual Machine (VM); or in the
30  * secure world, in case it is a Secure Partition (SP).
31  *
32  * If bit 15 is set partition is an SP; if bit 15 is clear partition is
33  * a VM.
34  */
35 #define SP_ID_MASK 1 << 15
36 #define SP_ID(x) ((x) | SP_ID_MASK)
37 #define VM_ID(x) (x & ~SP_ID_MASK)
38 #define IS_SP_ID(x) ((x & SP_ID_MASK) != 0U)
39 
40 struct mailbox_buffers {
41 	void *send;
42 	void *recv;
43 };
44 
45 struct mailbox_buffers set_up_mailbox(void);
46 ffa_memory_handle_t send_memory_and_retrieve_request_multi_receiver(
47 	uint32_t share_func, void *tx_buffer, ffa_vm_id_t sender,
48 	struct ffa_memory_region_constituent constituents[],
49 	uint32_t constituent_count, struct ffa_memory_access receivers_send[],
50 	uint32_t receivers_send_count,
51 	struct ffa_memory_access receivers_retrieve[],
52 	uint32_t receivers_retrieve_count, ffa_memory_region_flags_t send_flags,
53 	ffa_memory_region_flags_t retrieve_flags);
54 ffa_memory_handle_t send_memory_and_retrieve_request(
55 	uint32_t share_func, void *tx_buffer, ffa_vm_id_t sender,
56 	ffa_vm_id_t recipient,
57 	struct ffa_memory_region_constituent constituents[],
58 	uint32_t constituent_count, ffa_memory_region_flags_t send_flags,
59 	ffa_memory_region_flags_t retrieve_flags,
60 	enum ffa_data_access send_data_access,
61 	enum ffa_data_access retrieve_data_access,
62 	enum ffa_instruction_access send_instruction_access,
63 	enum ffa_instruction_access retrieve_instruction_access);
64 ffa_memory_handle_t send_memory_and_retrieve_request_force_fragmented(
65 	uint32_t share_func, void *tx_buffer, ffa_vm_id_t sender,
66 	ffa_vm_id_t recipient,
67 	struct ffa_memory_region_constituent constituents[],
68 	uint32_t constituent_count, ffa_memory_region_flags_t flags,
69 	enum ffa_data_access send_data_access,
70 	enum ffa_data_access retrieve_data_access,
71 	enum ffa_instruction_access send_instruction_access,
72 	enum ffa_instruction_access retrieve_instruction_access);
73 ffa_vm_id_t retrieve_memory_from_message(
74 	void *recv_buf, void *send_buf, struct ffa_value msg_ret,
75 	ffa_memory_handle_t *handle,
76 	struct ffa_memory_region *memory_region_ret,
77 	size_t memory_region_max_size);
78 ffa_vm_id_t retrieve_memory_from_message_expect_fail(void *recv_buf,
79 						     void *send_buf,
80 						     struct ffa_value msg_ret,
81 						     int32_t expected_error);
82 
83 ffa_vm_count_t get_ffa_partition_info(struct ffa_uuid *uuid,
84 				      struct ffa_partition_info *info,
85 				      size_t info_size);
86 
87 struct ffa_boot_info_header *get_boot_info_header(void);
88 void dump_boot_info(struct ffa_boot_info_header *boot_info_header);
89 struct ffa_boot_info_desc *get_boot_info_desc(
90 	struct ffa_boot_info_header *boot_info_heade, uint8_t type,
91 	uint8_t type_id);
92 
93 struct ffa_value send_indirect_message(ffa_vm_id_t from, ffa_vm_id_t to,
94 				       void *send, const void *payload,
95 				       size_t payload_size,
96 				       uint32_t send_flags);
97