blob: be5897d5cc0bb51ca4180a104370389fd96fccc5 [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);
108void dump_share_states(void);
109
110/**
111 * Return the offset to the first constituent within the
112 * `ffa_composite_memory_region` for the given receiver from an
113 * `ffa_memory_region`. The caller must check that the receiver_index is within
114 * bounds, and that it has a composite memory region offset.
115 */
116static inline uint32_t ffa_composite_constituent_offset(
117 struct ffa_memory_region *memory_region, uint32_t receiver_index)
118{
119 CHECK(receiver_index < memory_region->receiver_count);
120 CHECK(memory_region->receivers[receiver_index]
121 .composite_memory_region_offset != 0);
122
123 return memory_region->receivers[receiver_index]
124 .composite_memory_region_offset +
125 sizeof(struct ffa_composite_memory_region);
126}
127
128struct ffa_value ffa_memory_send_validate(
129 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
130 uint32_t memory_share_length, uint32_t fragment_length,
131 uint32_t share_func);
132struct ffa_value ffa_send_check_update(
133 struct vm_locked from_locked,
134 struct ffa_memory_region_constituent **fragments,
135 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
136 uint32_t share_func, struct ffa_memory_access *receivers,
137 uint32_t receivers_count, struct mpool *page_pool, bool clear,
138 uint32_t *orig_from_mode_ret);
139bool ffa_region_group_identity_map(
140 struct vm_locked vm_locked,
141 struct ffa_memory_region_constituent **fragments,
142 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
143 uint32_t mode, struct mpool *ppool, bool commit);
J-Alvesfc19b372022-07-06 12:17:35 +0100144struct ffa_value ffa_retrieve_check_transition(
145 struct vm_locked to, uint32_t share_func,
146 struct ffa_memory_region_constituent **fragments,
147 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
148 uint32_t memory_to_attributes, uint32_t *to_mode);
J-Alvesb5084cf2022-07-06 14:20:12 +0100149uint32_t ffa_memory_region_get_receiver(struct ffa_memory_region *memory_region,
150 ffa_vm_id_t receiver);
151struct ffa_value ffa_retrieve_check_update(
152 struct vm_locked to_locked, ffa_vm_id_t from_id,
153 struct ffa_memory_region_constituent **fragments,
154 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
155 uint32_t memory_to_attributes, uint32_t share_func, bool clear,
156 struct mpool *page_pool);