blob: 1861801dcbca2effa4fd7a35b0d4f1e8b20bced6 [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
J-Alvesd15905d2023-02-20 11:52:37 +000016#include <stdbool.h>
17#include <stdint.h>
18
19#include "hf/check.h"
20#include "hf/ffa_memory.h"
21#include "hf/mpool.h"
22#include "hf/vm.h"
23
24#include "vmapi/hf/ffa.h"
25
J-Alves66652252022-07-06 09:49:51 +010026/**
27 * The maximum number of fragments into which a memory sharing message may be
28 * broken.
29 */
30#define MAX_FRAGMENTS 20
31
32static_assert(sizeof(struct ffa_memory_region_constituent) % 16 == 0,
33 "struct ffa_memory_region_constituent must be a multiple of 16 "
34 "bytes long.");
35static_assert(sizeof(struct ffa_composite_memory_region) % 16 == 0,
36 "struct ffa_composite_memory_region must be a multiple of 16 "
37 "bytes long.");
38static_assert(sizeof(struct ffa_memory_region_attributes) == 4,
39 "struct ffa_memory_region_attributes must be 4 bytes long.");
40static_assert(sizeof(struct ffa_memory_access) % 16 == 0,
41 "struct ffa_memory_access must be a multiple of 16 bytes long.");
42static_assert(sizeof(struct ffa_memory_region) % 16 == 0,
43 "struct ffa_memory_region must be a multiple of 16 bytes long.");
44static_assert(sizeof(struct ffa_mem_relinquish) % 16 == 0,
45 "struct ffa_mem_relinquish must be a multiple of 16 "
46 "bytes long.");
Demi Marie Obenourd4677412023-02-03 20:35:12 -050047static_assert(sizeof(((struct ffa_memory_region){0}).receiver_count == 4),
48 "struct ffa_memory_region::receiver_count must be 4 bytes long");
J-Alves66652252022-07-06 09:49:51 +010049
50struct ffa_memory_share_state {
51 /**
52 * The memory region being shared, or NULL if this share state is
53 * unallocated.
54 */
55 struct ffa_memory_region *memory_region;
56
57 struct ffa_memory_region_constituent *fragments[MAX_FRAGMENTS];
58
59 /** The number of constituents in each fragment. */
60 uint32_t fragment_constituent_counts[MAX_FRAGMENTS];
61
62 /**
63 * The number of valid elements in the `fragments` and
64 * `fragment_constituent_counts` arrays.
65 */
66 uint32_t fragment_count;
67
68 /**
69 * The FF-A function used for sharing the memory. Must be one of
70 * FFA_MEM_DONATE_32, FFA_MEM_LEND_32 or FFA_MEM_SHARE_32 if the
71 * share state is allocated, or 0.
72 */
73 uint32_t share_func;
74
75 /**
76 * The sender's original mode before invoking the FF-A function for
77 * sharing the memory.
78 * This is used to reset the original configuration when sender invokes
79 * FFA_MEM_RECLAIM_32.
80 */
81 uint32_t sender_orig_mode;
82
83 /**
84 * True if all the fragments of this sharing request have been sent and
85 * Hafnium has updated the sender page table accordingly.
86 */
87 bool sending_complete;
88
89 /**
90 * How many fragments of the memory region each recipient has retrieved
91 * so far. The order of this array matches the order of the endpoint
92 * memory access descriptors in the memory region descriptor. Any
93 * entries beyond the receiver_count will always be 0.
94 */
95 uint32_t retrieved_fragment_count[MAX_MEM_SHARE_RECIPIENTS];
96
97 /**
98 * Field for the SPMC to keep track of how many fragments of the memory
99 * region the hypervisor has managed to retrieve, using a
100 * `hypervisor retrieve request`, as defined by FF-A v1.1 EAC0
101 * specification.
102 */
103 uint32_t hypervisor_fragment_count;
104};
105
106/**
107 * Encapsulates the set of share states while the `share_states_lock` is held.
108 */
109struct share_states_locked {
110 struct ffa_memory_share_state *share_states;
111};
112
Karl Meakin52cdfe72023-06-30 14:49:10 +0100113struct ffa_memory_share_state *allocate_share_state(
114 struct share_states_locked share_states, uint32_t share_func,
115 struct ffa_memory_region *memory_region, uint32_t fragment_length,
116 ffa_memory_handle_t handle);
J-Alves66652252022-07-06 09:49:51 +0100117struct share_states_locked share_states_lock(void);
118void share_states_unlock(struct share_states_locked *share_states);
Karl Meakin4a2854a2023-06-30 16:26:52 +0100119struct ffa_memory_share_state *get_share_state(
120 struct share_states_locked share_states, ffa_memory_handle_t handle);
J-Alvesfdd29272022-07-19 13:16:31 +0100121void share_state_free(struct share_states_locked share_states,
122 struct ffa_memory_share_state *share_state,
123 struct mpool *page_pool);
124uint32_t share_state_next_fragment_offset(
125 struct share_states_locked share_states,
126 struct ffa_memory_share_state *share_state);
127/** Checks whether the given share state has been fully sent. */
128bool share_state_sending_complete(struct share_states_locked share_states,
129 struct ffa_memory_share_state *share_state);
J-Alves66652252022-07-06 09:49:51 +0100130void dump_share_states(void);
131
J-Alves66652252022-07-06 09:49:51 +0100132struct ffa_value ffa_memory_send_validate(
133 struct vm_locked from_locked, struct ffa_memory_region *memory_region,
134 uint32_t memory_share_length, uint32_t fragment_length,
135 uint32_t share_func);
136struct ffa_value ffa_send_check_update(
137 struct vm_locked from_locked,
138 struct ffa_memory_region_constituent **fragments,
139 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves8f11cde2022-12-21 16:18:22 +0000140 uint32_t total_page_count, uint32_t share_func,
141 struct ffa_memory_access *receivers, uint32_t receivers_count,
142 struct mpool *page_pool, bool clear, uint32_t *orig_from_mode_ret);
J-Alvesfdd29272022-07-19 13:16:31 +0100143struct ffa_value ffa_memory_send_complete(
144 struct vm_locked from_locked, struct share_states_locked share_states,
145 struct ffa_memory_share_state *share_state, struct mpool *page_pool,
146 uint32_t *orig_from_mode_ret);
147struct ffa_value ffa_memory_send_continue_validate(
148 struct share_states_locked share_states, ffa_memory_handle_t handle,
J-Alves19e20cf2023-08-02 12:48:55 +0100149 struct ffa_memory_share_state **share_state_ret, ffa_id_t from_vm_id,
J-Alvesfdd29272022-07-19 13:16:31 +0100150 struct mpool *page_pool);
J-Alvesfc19b372022-07-06 12:17:35 +0100151struct ffa_value ffa_retrieve_check_transition(
152 struct vm_locked to, uint32_t share_func,
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 *to_mode);
J-Alvesb5084cf2022-07-06 14:20:12 +0100156struct ffa_value ffa_retrieve_check_update(
J-Alves26483382023-04-20 12:01:49 +0100157 struct vm_locked to_locked,
J-Alvesb5084cf2022-07-06 14:20:12 +0100158 struct ffa_memory_region_constituent **fragments,
159 uint32_t *fragment_constituent_counts, uint32_t fragment_count,
J-Alves26483382023-04-20 12:01:49 +0100160 uint32_t sender_orig_mode, uint32_t share_func, bool clear,
J-Alvesb5084cf2022-07-06 14:20:12 +0100161 struct mpool *page_pool);
J-Alvesfdd29272022-07-19 13:16:31 +0100162bool ffa_region_group_identity_map(
163 struct vm_locked vm_locked,
164 struct ffa_memory_region_constituent **fragments,
165 const uint32_t *fragment_constituent_counts, uint32_t fragment_count,
166 uint32_t mode, struct mpool *ppool, bool commit);
167bool memory_region_receivers_from_other_world(
168 struct ffa_memory_region *memory_region);