blob: dbf6158298540b2e529ab88d4270101c1f133f1b [file] [log] [blame]
J-Alves66652252022-07-06 09:49:51 +01001/*
2 * Copyright 2022 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/**
10 * The maximum number of memory sharing handles which may be active at once. A
11 * DONATE handle is active from when it is sent to when it is retrieved; a SHARE
12 * or LEND handle is active from when it is sent to when it is reclaimed.
13 */
14#define MAX_MEM_SHARES 100
15
16/**
17 * The maximum number of fragments into which a memory sharing message may be
18 * broken.
19 */
20#define MAX_FRAGMENTS 20
21
22static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0,
23 "struct ffa_memory_region_constituent must be a multiple of 16 "
24 "bytes long.");
25static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0,
26 "struct ffa_composite_memory_region must be a multiple of 16 "
27 "bytes long.");
28static_assert(sizeof(struct ffa_memory_region_attributes) == 4,
29 "struct ffa_memory_region_attributes must be 4 bytes long.");
30static_assert(sizeof(struct ffa_memory_access) % 16 == 0,
31 "struct ffa_memory_access must be a multiple of 16 bytes long.");
32static_assert(sizeof(struct ffa_memory_region) % 16 == 0,
33 "struct ffa_memory_region must be a multiple of 16 bytes long.");
34static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0,
35 "struct ffa_mem_relinquish must be a multiple of 16 "
36 "bytes long.");
37
38struct ffa_memory_share_state {
39 /**
40 * The memory region being shared, or NULL if this share state is
41 * unallocated.
42 */
43 struct ffa_memory_region *memory_region;
44
45 struct ffa_memory_region_constituent *fragments[MAX_FRAGMENTS];
46
47 /** The number of constituents in each fragment. */
48 uint32_t fragment_constituent_counts[MAX_FRAGMENTS];
49
50 /**
51 * The number of valid elements in the `fragments` and
52 * `fragment_constituent_counts` arrays.
53 */
54 uint32_t fragment_count;
55
56 /**
57 * The FF-A function used for sharing the memory. Must be one of
58 * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the
59 * share state is allocated, or 0.
60 */
61 uint32_t share_func;
62
63 /**
64 * The sender's original mode before invoking the FF-A function for
65 * sharing the memory.
66 * This is used to reset the original configuration when sender invokes
67 * FFA_MEM_RECLAIM_32.
68 */
69 uint32_t sender_orig_mode;
70
71 /**
72 * True if all the fragments of this sharing request have been sent and
73 * Hafnium has updated the sender page table accordingly.
74 */
75 bool sending_complete;
76
77 /**
78 * How many fragments of the memory region each recipient has retrieved
79 * so far. The order of this array matches the order of the endpoint
80 * memory access descriptors in the memory region descriptor. Any
81 * entries beyond the receiver_count will always be 0.
82 */
83 uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS];
84
85 /**
86 * Field for the SPMC to keep track of how many fragments of the memory
87 * region the hypervisor has managed to retrieve, using a
88 * `hypervisor retrieve request`, as defined by FF-A v1.1 EAC0
89 * specification.
90 */
91 uint32_t hypervisor_fragment_count;
92};
93
94/**
95 * Encapsulates the set of share states while the `share_states_lock` is held.
96 */
97struct share_states_locked {
98 struct ffa_memory_share_state *share_states;
99};
100
101bool allocate_share_state(struct share_states_locked share_states,
102 uint32_t share_func,
103 struct ffa_memory_region *memory_region,
104 uint32_t fragment_length, ffa_memory_handle_t handle,
105 struct ffa_memory_share_state **share_state_ret);
106struct share_states_locked share_states_lock(void);
107void share_states_unlock(struct share_states_locked *share_states);
J-Alvesfdd29272022-07-19 13:16:31 +0100108void share_state_free(struct share_states_locked share_states,
109 struct ffa_memory_share_state *share_state,
110 struct mpool *page_pool);
111uint32_t share_state_next_fragment_offset(
112 struct share_states_locked share_states,
113 struct ffa_memory_share_state *share_state);
114/** Checks whether the given share state has been fully sent. */
115bool share_state_sending_complete(struct share_states_locked share_states,
116 struct ffa_memory_share_state *share_state);
J-Alves66652252022-07-06 09:49:51 +0100117void dump_share_states(void);
118
119/**
120 * Return the offset to the first constituent within the
121 * `ffa_composite_memory_region` for the given receiver from an
122 * `ffa_memory_region`. The caller must check that the receiver_index is within
123 * bounds, and that it has a composite memory region offset.
124 */
125static inline uint32_t ffa_composite_constituent_offset(
126 struct ffa_memory_region *memory_region, uint32_t receiver_index)
127{
128 CHECK(receiver_index < memory_region->receiver_count);
129 CHECK(memory_region->receivers[receiver_index]
130 .composite_memory_region_offset != 0);
131
132 return memory_region->receivers[receiver_index]
133 .composite_memory_region_offset +
134 sizeof(struct ffa_composite_memory_region);
135}
136
137struct ffa_value ffa_memory_send_validate(
138 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
139 uint32_t memory_share_length, uint32_t fragment_length,
140 uint32_t share_func);
141struct ffa_value ffa_send_check_update(
142 struct vm_locked from_locked,
143 struct ffa_memory_region_constituent **fragments,
144 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
145 uint32_t share_func, struct ffa_memory_access *receivers,
146 uint32_t receivers_count, struct mpool *page_pool, bool clear,
147 uint32_t *orig_from_mode_ret);
J-Alvesfdd29272022-07-19 13:16:31 +0100148struct ffa_value ffa_memory_send_complete(
149 struct vm_locked from_locked, struct share_states_locked share_states,
150 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
151 uint32_t *orig_from_mode_ret);
152struct ffa_value ffa_memory_send_continue_validate(
153 struct share_states_locked share_states, ffa_memory_handle_t handle,
154 struct ffa_memory_share_state **share_state_ret, ffa_vm_id_t from_vm_id,
155 struct mpool *page_pool);
156struct ffa_value ffa_send_check_update(
157 struct vm_locked from_locked,
J-Alves66652252022-07-06 09:49:51 +0100158 struct ffa_memory_region_constituent **fragments,
J-Alvesfdd29272022-07-19 13:16:31 +0100159 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
160 uint32_t share_func, struct ffa_memory_access *receivers,
161 uint32_t receivers_count, struct mpool *page_pool, bool clear,
162 uint32_t *orig_from_mode_ret);
J-Alvesfc19b372022-07-06 12:17:35 +0100163struct ffa_value ffa_retrieve_check_transition(
164 struct vm_locked to, uint32_t share_func,
165 struct ffa_memory_region_constituent **fragments,
166 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
167 uint32_t memory_to_attributes, uint32_t *to_mode);
J-Alvesb5084cf2022-07-06 14:20:12 +0100168struct ffa_value ffa_retrieve_check_update(
169 struct vm_locked to_locked, ffa_vm_id_t from_id,
170 struct ffa_memory_region_constituent **fragments,
171 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
172 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
173 struct mpool *page_pool);
J-Alvesfdd29272022-07-19 13:16:31 +0100174uint32_t ffa_memory_region_get_receiver(struct ffa_memory_region *memory_region,
175 ffa_vm_id_t receiver);
176bool ffa_region_group_identity_map(
177 struct vm_locked vm_locked,
178 struct ffa_memory_region_constituent **fragments,
179 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
180 uint32_t mode, struct mpool *ppool, bool commit);
181bool memory_region_receivers_from_other_world(
182 struct ffa_memory_region *memory_region);