Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 2 | * Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 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. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
Andrew Scull | fbc938a | 2018-08-20 14:09:28 +0100 | [diff] [blame] | 9 | #pragma once |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 10 | |
Andrew Scull | 9726c25 | 2019-01-23 13:44:19 +0000 | [diff] [blame] | 11 | #include <stdatomic.h> |
| 12 | |
Olivier Deprez | febf9e6 | 2023-03-02 15:44:35 +0100 | [diff] [blame] | 13 | #include "hf/arch/vm/vm.h" |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 14 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 15 | #include "hf/cpu.h" |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 16 | #include "hf/ffa_partition_manifest.h" |
Madhukar Pappireddy | 464f246 | 2021-08-03 11:23:07 -0500 | [diff] [blame] | 17 | #include "hf/interrupt_desc.h" |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 18 | #include "hf/list.h" |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 19 | #include "hf/mm.h" |
Wedson Almeida Filho | 22d5eaa | 2018-12-16 00:38:49 +0000 | [diff] [blame] | 20 | #include "hf/mpool.h" |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 21 | |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 22 | #include "vmapi/hf/ffa.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 23 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 24 | #define MAX_SMCS 32 |
Madhukar Pappireddy | 464f246 | 2021-08-03 11:23:07 -0500 | [diff] [blame] | 25 | #define VM_MANIFEST_MAX_INTERRUPTS 32 |
Andrew Walbran | c1ad4ce | 2019-05-09 11:41:39 +0100 | [diff] [blame] | 26 | |
Madhukar Pappireddy | 5c04a38 | 2022-12-28 11:29:26 -0600 | [diff] [blame] | 27 | /** Action for Other-Secure interrupts by SPMC. */ |
| 28 | #define OTHER_S_INT_ACTION_QUEUED 0 |
| 29 | #define OTHER_S_INT_ACTION_SIGNALED 1 |
| 30 | |
Andrew Walbran | a36f759 | 2019-12-13 18:43:38 +0000 | [diff] [blame] | 31 | /** |
Olivier Deprez | 2013775 | 2023-02-07 10:55:35 +0100 | [diff] [blame] | 32 | * Power management bifields stating which messages a VM is willing to be |
| 33 | * notified about. |
| 34 | */ |
| 35 | #define VM_POWER_MANAGEMENT_CPU_OFF_SHIFT (0) |
Olivier Deprez | 2013775 | 2023-02-07 10:55:35 +0100 | [diff] [blame] | 36 | |
| 37 | /** |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 38 | * The state of an RX buffer, as defined by FF-A v1.1 EAC0 specification. |
| 39 | * It is used to implement ownership rules, as defined in the section 6.2.2.4.2. |
Andrew Walbran | a36f759 | 2019-12-13 18:43:38 +0000 | [diff] [blame] | 40 | * |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 41 | * EMPTY is the initial state. It is set by default to the endpoints at the |
| 42 | * virtual instance. |
| 43 | * The follow state transitions are possible: |
| 44 | * * EMPTY => FULL: message sent to a partition. Ownership given to the |
| 45 | * partition. |
| 46 | * * EMPTY => OTHER_WORLD_OWNED: This state transition only applies to NWd VMs. |
| 47 | * Used by the SPMC or Hypervisor to track that ownership of the RX buffer |
| 48 | * belong to the other world: |
| 49 | * - The Hypervisor does this state transition after forwarding |
| 50 | * FFA_RXTX_MAP call to the SPMC, for it to map a VM's RXTX buffers into SPMC's |
| 51 | * translation regime. |
| 52 | * - SPMC was previously given ownership of the VM's RX buffer, after the |
| 53 | * FFA_RXTX_MAP interface has been successfully forwarded to it. The SPMC does |
| 54 | * this state transition, when handling a successful FFA_RX_ACQUIRE, assigning |
| 55 | * ownership to the hypervisor. |
| 56 | * * FULL => EMPTY: Partition received an RX buffer full notification, consumed |
| 57 | * the content of buffers, and called FFA_RX_RELEASE or FFA_MSG_WAIT. SPMC or |
| 58 | * Hypervisor's ownership reestablished. |
| 59 | * * OTHER_WORLD_OWNED => EMPTY: VM called FFA_RX_RELEASE, the hypervisor |
| 60 | * forwarded it to the SPMC, which reestablishes ownership of the VM's buffer. |
| 61 | * SPs should never have their buffers state set to OTHER_WORLD_OWNED. |
Andrew Walbran | a36f759 | 2019-12-13 18:43:38 +0000 | [diff] [blame] | 62 | */ |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 63 | enum mailbox_state { |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 64 | /** There is no message in the mailbox. */ |
Andrew Scull | d6ee110 | 2019-04-05 22:12:42 +0100 | [diff] [blame] | 65 | MAILBOX_STATE_EMPTY, |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 66 | |
Andrew Walbran | c3910f7 | 2018-11-27 14:24:36 +0000 | [diff] [blame] | 67 | /** There is a message in the mailbox that is waiting for a reader. */ |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 68 | MAILBOX_STATE_FULL, |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 69 | |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 70 | /** |
| 71 | * In the SPMC, it means the Hypervisor/OS Kernel owns the RX buffer. |
| 72 | * In the Hypervisor, it means the SPMC owns the Rx buffer. |
| 73 | */ |
| 74 | MAILBOX_STATE_OTHER_WORLD_OWNED, |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 75 | }; |
| 76 | |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 77 | struct mailbox { |
| 78 | enum mailbox_state state; |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 79 | void *recv; |
| 80 | const void *send; |
| 81 | |
| 82 | /** The ID of the VM which sent the message currently in `recv`. */ |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 83 | ffa_id_t recv_sender; |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 84 | |
| 85 | /** The size of the message currently in `recv`. */ |
| 86 | uint32_t recv_size; |
| 87 | |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 88 | /** |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 89 | * The FF-A function ID to use to deliver the message currently in |
Andrew Walbran | e7ad3c0 | 2019-12-24 17:03:04 +0000 | [diff] [blame] | 90 | * `recv`. |
| 91 | */ |
| 92 | uint32_t recv_func; |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 93 | }; |
| 94 | |
J-Alves | 4ef6e84 | 2021-03-18 12:47:01 +0000 | [diff] [blame] | 95 | struct notifications_state { |
| 96 | /** |
| 97 | * To keep track of the notifications pending. |
| 98 | * Set on call to FFA_NOTIFICATION_SET, and cleared on call to |
| 99 | * FFA_NOTIFICATION_GET. |
| 100 | */ |
| 101 | ffa_notifications_bitmap_t pending; |
| 102 | |
| 103 | /** |
| 104 | * Set on FFA_NOTIFICATION_INFO_GET to keep track of the notifications |
| 105 | * whose information has been retrieved by the referred ABI. |
| 106 | * Cleared on call to FFA_NOTIFICATION_GET. |
| 107 | */ |
| 108 | ffa_notifications_bitmap_t info_get_retrieved; |
| 109 | }; |
| 110 | |
| 111 | struct notifications { |
| 112 | /** |
| 113 | * The following array maps the notifications to the bound FF-A |
| 114 | * endpoint. |
| 115 | * The index in the bindings array relates to the notification |
| 116 | * ID, and bit position in 'ffa_notifications_bitmap_t'. |
| 117 | */ |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 118 | ffa_id_t bindings_sender_id[MAX_FFA_NOTIFICATIONS]; |
J-Alves | 4ef6e84 | 2021-03-18 12:47:01 +0000 | [diff] [blame] | 119 | ffa_notifications_bitmap_t bindings_per_vcpu; |
| 120 | |
Raghu Krishnamurthy | f5fec20 | 2022-09-30 07:25:10 -0700 | [diff] [blame] | 121 | /* The index of the array below relates to the ID of the VCPU. |
| 122 | * This is a dynamically allocated array of struct |
| 123 | * notifications_state and has as many entries as vcpu_count. |
| 124 | */ |
| 125 | struct notifications_state *per_vcpu; |
J-Alves | 4ef6e84 | 2021-03-18 12:47:01 +0000 | [diff] [blame] | 126 | struct notifications_state global; |
| 127 | }; |
| 128 | |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 129 | /** |
| 130 | * The following enum relates to a state machine to guide the insertion of |
| 131 | * IDs in the respective list as a result of a FFA_NOTIFICATION_INFO_GET call. |
| 132 | * As per the FF-A v1.1 specification, the return of the interface |
| 133 | * FFA_NOTIFICATION_INFO_GET, is a list of 16-bit values, regarding the VM ID |
| 134 | * and VCPU IDs of those with pending notifications. |
| 135 | * The overall list, is composed of "sub-lists", that starts with the VM ID, and |
| 136 | * can follow with up to 3 more VCPU IDs. A VM can have multiple 'sub-lists'. |
| 137 | * The states are traversed on a per VM basis, and should help with filling the |
| 138 | * list of IDs. |
| 139 | * |
| 140 | * INIT is the initial state. The following state transitions are possible: |
| 141 | * * INIT => INSERTING: no list has been created for the VM prior. There are |
| 142 | * notifications pending and VM ID should be inserted first. If it regards to |
| 143 | * a per VCPU notification the VCPU ID should follow. Only VCPU IDs should be |
| 144 | * inserted from this point, until reaching "sub-list" size limit. |
| 145 | * * INIT => FULL: There is no space in the ID list to insert IDs. |
| 146 | * * INSERTING => STARTING_NEW: list has been created. Adding only VCPU IDs, |
| 147 | * however "sub-list" limit has been reached. If there are more pending per VCPU |
| 148 | * notifications pending for the VM, a new list should be created starting with |
| 149 | * VM ID. |
| 150 | * * INSERTING => FULL: There is no space in the ID list to insert IDs. |
| 151 | * * STARTING_NEW => INSERTING: Started a new 'sub-list' for the given VM, for |
| 152 | * the remaining pending per VCPU notifications, only the VCPU ID should be |
| 153 | * inserted. |
| 154 | * * STARTING_NEW => FULL: There is no space in the ID list to insert IDs. |
| 155 | */ |
| 156 | enum notifications_info_get_state { |
| 157 | INIT, |
| 158 | INSERTING, |
| 159 | STARTING_NEW, |
| 160 | FULL, |
| 161 | }; |
| 162 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 163 | struct smc_whitelist { |
| 164 | uint32_t smcs[MAX_SMCS]; |
| 165 | uint16_t smc_count; |
| 166 | bool permissive; |
| 167 | }; |
| 168 | |
Madhukar Pappireddy | 84259c9 | 2025-06-27 14:28:45 -0500 | [diff] [blame] | 169 | /* |
| 170 | * Implementation defined states for a VM (or SP). Refer to `vm_set_state` |
| 171 | * helper for documentation of legal transitions. |
| 172 | */ |
| 173 | enum vm_state { |
| 174 | /* VM has not yet been created. This is the default value. */ |
| 175 | VM_STATE_NULL, |
| 176 | |
| 177 | /* VM has been created and initialized by partition manager. */ |
| 178 | VM_STATE_CREATED, |
| 179 | |
| 180 | /* |
| 181 | * At least one execution context of the VM has been given CPU cycles to |
| 182 | * initialize itself. |
| 183 | */ |
| 184 | VM_STATE_RUNNING, |
| 185 | |
| 186 | /* The VM has been aborted due to a fatal error. */ |
| 187 | VM_STATE_ABORTING, |
| 188 | }; |
| 189 | |
Karl Meakin | 2ad6b66 | 2024-07-29 20:45:40 +0100 | [diff] [blame] | 190 | /* NOLINTNEXTLINE(clang-analyzer-optin.performance.Padding) */ |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 191 | struct vm { |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 192 | ffa_id_t id; |
Kathleen Capella | 422b10b | 2023-06-30 18:28:27 -0400 | [diff] [blame] | 193 | struct ffa_uuid uuids[PARTITION_MAX_UUIDS]; |
Karl Meakin | 0e617d9 | 2024-04-05 12:55:22 +0100 | [diff] [blame] | 194 | enum ffa_version ffa_version; |
Madhukar Pappireddy | 84259c9 | 2025-06-27 14:28:45 -0500 | [diff] [blame] | 195 | enum vm_state state; |
Karl Meakin | 6eeec8e | 2024-03-07 18:07:20 +0000 | [diff] [blame] | 196 | |
| 197 | /* |
| 198 | * Whether this FF-A instance has negotiated an FF-A version through a |
| 199 | * call to FFA_VERSION. Once the version has been negotiated, it is an |
| 200 | * error to attempt to change it through another call to FFA_VERSION. |
| 201 | */ |
| 202 | bool ffa_version_negotiated; |
| 203 | |
Andrew Scull | ae9962e | 2019-10-03 16:51:16 +0100 | [diff] [blame] | 204 | struct smc_whitelist smc_whitelist; |
| 205 | |
Andrew Walbran | 0d7a068 | 2018-12-06 16:48:47 +0000 | [diff] [blame] | 206 | /** See api.c for the partial ordering on locks. */ |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 207 | struct spinlock lock; |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 208 | ffa_vcpu_count_t vcpu_count; |
Raghu Krishnamurthy | 30aabd6 | 2022-09-17 21:41:00 -0700 | [diff] [blame] | 209 | struct vcpu *vcpus; |
Andrew Scull | 89a7524 | 2018-08-06 17:04:55 +0100 | [diff] [blame] | 210 | struct mm_ptable ptable; |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 211 | |
| 212 | /** |
| 213 | * Set of page tables used for defining the peripheral's secure |
| 214 | * IPA space, in the context of SPMC. |
| 215 | */ |
| 216 | struct mm_ptable iommu_ptables[PARTITION_MAX_DMA_DEVICES]; |
| 217 | /** Count of DMA devices assigned to this VM. */ |
| 218 | uint8_t dma_device_count; |
Andrew Scull | aa039b3 | 2018-10-04 15:02:26 +0100 | [diff] [blame] | 219 | struct mailbox mailbox; |
J-Alves | 4ef6e84 | 2021-03-18 12:47:01 +0000 | [diff] [blame] | 220 | |
| 221 | struct { |
| 222 | /** |
| 223 | * State structures for notifications coming from VMs or coming |
| 224 | * from SPs. Both fields are maintained by the SPMC. |
| 225 | * The hypervisor ignores the 'from_sp' field, given VM |
| 226 | * notifications from SPs are managed by the SPMC. |
| 227 | */ |
| 228 | struct notifications from_vm; |
| 229 | struct notifications from_sp; |
J-Alves | 52578f8 | 2022-03-25 12:30:47 +0000 | [diff] [blame] | 230 | struct notifications_state framework; |
J-Alves | 4ef6e84 | 2021-03-18 12:47:01 +0000 | [diff] [blame] | 231 | bool enabled; |
| 232 | } notifications; |
| 233 | |
Karl Meakin | a603e08 | 2024-08-02 17:57:27 +0100 | [diff] [blame] | 234 | /** |
| 235 | * Whether this partition is subscribed to receiving VM created/VM |
| 236 | * destroyed messages. |
| 237 | */ |
| 238 | struct { |
| 239 | bool vm_created; |
| 240 | bool vm_destroyed; |
| 241 | } vm_availability_messages; |
| 242 | |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 243 | /** |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 244 | * Booting parameters (FF-A SP partitions). |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 245 | */ |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 246 | uint16_t boot_order; |
J-Alves | 7d38f7b | 2022-04-13 13:22:30 +0100 | [diff] [blame] | 247 | |
| 248 | /** Entries to pass boot data to the VM. */ |
| 249 | struct { |
| 250 | uint32_t gp_register_num; |
| 251 | ipaddr_t blob_addr; |
| 252 | } boot_info; |
| 253 | |
Kathleen Capella | f71dee4 | 2023-08-08 16:24:14 -0400 | [diff] [blame] | 254 | uint16_t messaging_method; |
Madhukar Pappireddy | 8415405 | 2022-06-21 18:30:25 -0500 | [diff] [blame] | 255 | |
| 256 | /** |
| 257 | * Action specified by a Partition through the manifest in response to |
| 258 | * non secure interrupt. |
| 259 | */ |
| 260 | uint8_t ns_interrupts_action; |
Madhukar Pappireddy | 5c04a38 | 2022-12-28 11:29:26 -0600 | [diff] [blame] | 261 | |
| 262 | /** |
J-Alves | ce42f7a | 2025-02-10 13:57:41 +0000 | [diff] [blame] | 263 | * Whether the SRI is used for requesting CPU cycles for a partition to |
| 264 | * handle interrupts. |
| 265 | */ |
| 266 | struct sri_interrupts_policy sri_policy; |
| 267 | |
| 268 | /** |
Madhukar Pappireddy | 5c04a38 | 2022-12-28 11:29:26 -0600 | [diff] [blame] | 269 | * Action specified by a Partition through the manifest in response to |
| 270 | * Other-S-Int. |
| 271 | */ |
| 272 | uint8_t other_s_interrupts_action; |
Madhukar Pappireddy | 046dad0 | 2022-06-21 18:43:33 -0500 | [diff] [blame] | 273 | bool me_signal_virq; |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 274 | |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 275 | /** |
Olivier Deprez | a15f235 | 2022-09-26 09:17:24 +0200 | [diff] [blame] | 276 | * Bitmask reporting the power management events that a partition |
| 277 | * requests to the signaled about. |
| 278 | */ |
| 279 | uint32_t power_management; |
| 280 | |
| 281 | /** |
Max Shvetsov | 40108e7 | 2020-08-27 12:39:50 +0100 | [diff] [blame] | 282 | * Secondary entry point supplied by FFA_SECONDARY_EP_REGISTER used |
| 283 | * for cold and warm boot of SP execution contexts. |
| 284 | */ |
| 285 | ipaddr_t secondary_ep; |
| 286 | |
Andrew Walbran | 1f32e72 | 2019-06-07 17:57:26 +0100 | [diff] [blame] | 287 | /** Arch-specific VM information. */ |
| 288 | struct arch_vm arch; |
Raghu Krishnamurthy | cd1eceb | 2021-01-04 12:20:48 -0800 | [diff] [blame] | 289 | bool el0_partition; |
Madhukar Pappireddy | 464f246 | 2021-08-03 11:23:07 -0500 | [diff] [blame] | 290 | |
| 291 | /** Interrupt descriptor */ |
| 292 | struct interrupt_descriptor interrupt_desc[VM_MANIFEST_MAX_INTERRUPTS]; |
Madhukar Pappireddy | a49ba16 | 2024-11-25 09:40:45 -0600 | [diff] [blame] | 293 | |
| 294 | /* List entry pointing to the next VM in the boot order list. */ |
| 295 | struct list_entry boot_list_node; |
Madhukar Pappireddy | c35573d | 2025-03-17 18:00:32 -0500 | [diff] [blame] | 296 | |
| 297 | /** |
| 298 | * Abort action taken by SPMC if the vCPU of this partition encounters |
| 299 | * a fatal error. |
| 300 | */ |
| 301 | enum abort_action abort_action; |
| 302 | |
| 303 | /** |
| 304 | * Whether the partition supports all the states defined in the |
| 305 | * Partition Lifecycle guidance. |
| 306 | */ |
| 307 | bool lifecycle_support; |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | /** Encapsulates a VM whose lock is held. */ |
| 311 | struct vm_locked { |
| 312 | struct vm *vm; |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 313 | }; |
| 314 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 315 | /** Container for two vm_locked structures */ |
| 316 | struct two_vm_locked { |
| 317 | struct vm_locked vm1; |
| 318 | struct vm_locked vm2; |
| 319 | }; |
| 320 | |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 321 | struct vm *vm_init(ffa_id_t id, ffa_vcpu_count_t vcpu_count, |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 322 | struct mpool *ppool, bool el0_partition, |
| 323 | uint8_t dma_device_count); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 324 | bool vm_init_next(ffa_vcpu_count_t vcpu_count, struct mpool *ppool, |
Madhukar Pappireddy | 070f49e | 2024-01-12 13:02:27 -0600 | [diff] [blame] | 325 | struct vm **new_vm, bool el0_partition, |
| 326 | uint8_t dma_device_count); |
Madhukar Pappireddy | 8e5d51a | 2025-05-08 18:01:09 -0500 | [diff] [blame] | 327 | bool vm_reinit(struct vm *vm, struct mpool *ppool); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 328 | ffa_vm_count_t vm_get_count(void); |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 329 | struct vm *vm_find(ffa_id_t id); |
| 330 | struct vm_locked vm_find_locked(ffa_id_t id); |
Fuad Tabba | e4efcc3 | 2020-07-16 15:37:27 +0100 | [diff] [blame] | 331 | struct vm *vm_find_index(uint16_t index); |
Andrew Walbran | 7e932bd | 2019-04-29 16:47:06 +0100 | [diff] [blame] | 332 | struct vm_locked vm_lock(struct vm *vm); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 333 | struct two_vm_locked vm_lock_both(struct vm *vm1, struct vm *vm2); |
Wedson Almeida Filho | ea62e2e | 2019-01-09 19:14:59 +0000 | [diff] [blame] | 334 | void vm_unlock(struct vm_locked *locked); |
Karl Meakin | e143080 | 2024-03-06 14:08:11 +0000 | [diff] [blame] | 335 | struct two_vm_locked vm_lock_both_in_order(struct vm_locked vm1, |
| 336 | struct vm *vm2); |
Andrew Walbran | b5ab43c | 2020-04-30 11:32:54 +0100 | [diff] [blame] | 337 | struct vcpu *vm_get_vcpu(struct vm *vm, ffa_vcpu_index_t vcpu_index); |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 338 | bool vm_id_is_current_world(ffa_id_t vm_id); |
J-Alves | 122f1a1 | 2022-12-12 15:55:42 +0000 | [diff] [blame] | 339 | bool vm_is_mailbox_busy(struct vm_locked to); |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 340 | bool vm_is_mailbox_other_world_owned(struct vm_locked to); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 341 | bool vm_identity_map(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 342 | mm_mode_t mode, struct mpool *ppool, ipaddr_t *ipa); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 343 | bool vm_identity_prepare(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 344 | mm_mode_t mode, struct mpool *ppool); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 345 | void vm_identity_commit(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 346 | mm_mode_t mode, struct mpool *ppool, ipaddr_t *ipa); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 347 | bool vm_unmap(struct vm_locked vm_locked, paddr_t begin, paddr_t end, |
| 348 | struct mpool *ppool); |
Raghu Krishnamurthy | 7ad3d14 | 2021-03-28 00:47:35 -0700 | [diff] [blame] | 349 | void vm_ptable_defrag(struct vm_locked vm_locked, struct mpool *ppool); |
Madhukar Pappireddy | f7d5af4 | 2025-05-08 17:02:40 -0500 | [diff] [blame] | 350 | void vm_free_ptables(struct vm *vm, struct mpool *ppool); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 351 | bool vm_unmap_hypervisor(struct vm_locked vm_locked, struct mpool *ppool); |
Madhukar Pappireddy | 507c997 | 2025-05-08 17:25:03 -0500 | [diff] [blame] | 352 | void vm_unmap_rxtx(struct vm_locked vm_locked, struct mpool *ppool); |
Madhukar Pappireddy | d88b014 | 2025-05-08 17:45:53 -0500 | [diff] [blame] | 353 | void vm_unmap_memory_regions(struct vm_locked vm_locked, struct mpool *ppool); |
J-Alves | b37fd08 | 2020-10-22 12:29:21 +0100 | [diff] [blame] | 354 | |
Raghu Krishnamurthy | ea195fa | 2021-02-12 23:29:00 -0800 | [diff] [blame] | 355 | bool vm_mem_get_mode(struct vm_locked vm_locked, ipaddr_t begin, ipaddr_t end, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 356 | mm_mode_t *mode); |
Madhukar Pappireddy | 0e57d3d | 2023-10-11 15:49:05 -0500 | [diff] [blame] | 357 | bool vm_iommu_mm_identity_map(struct vm_locked vm_locked, paddr_t begin, |
Karl Meakin | 07a69ab | 2025-02-07 14:53:19 +0000 | [diff] [blame] | 358 | paddr_t end, mm_mode_t mode, struct mpool *ppool, |
Madhukar Pappireddy | 0e57d3d | 2023-10-11 15:49:05 -0500 | [diff] [blame] | 359 | ipaddr_t *ipa, uint8_t dma_device_id); |
| 360 | |
Raghu Krishnamurthy | f5fec20 | 2022-09-30 07:25:10 -0700 | [diff] [blame] | 361 | void vm_notifications_init(struct vm *vm, ffa_vcpu_count_t vcpu_count, |
| 362 | struct mpool *ppool); |
Madhukar Pappireddy | cbb836c | 2025-05-08 17:16:42 -0500 | [diff] [blame] | 363 | void vm_reset_notifications(struct vm_locked vm_locked, struct mpool *ppool); |
J-Alves | 6665225 | 2022-07-06 09:49:51 +0100 | [diff] [blame] | 364 | bool vm_mailbox_state_busy(struct vm_locked vm_locked); |
J-Alves | a0f317d | 2021-06-09 13:31:59 +0100 | [diff] [blame] | 365 | bool vm_are_notifications_pending(struct vm_locked vm_locked, bool from_vm, |
| 366 | ffa_notifications_bitmap_t notifications); |
J-Alves | e8c8c2b | 2022-12-16 15:34:48 +0000 | [diff] [blame] | 367 | bool vm_are_fwk_notifications_pending(struct vm_locked vm_locked); |
J-Alves | 7461ef2 | 2021-10-18 17:21:33 +0100 | [diff] [blame] | 368 | bool vm_are_global_notifications_pending(struct vm_locked vm_locked); |
| 369 | bool vm_are_per_vcpu_notifications_pending(struct vm_locked vm_locked, |
| 370 | ffa_vcpu_index_t vcpu_id); |
J-Alves | 09ff9d8 | 2021-11-02 11:55:20 +0000 | [diff] [blame] | 371 | bool vm_are_notifications_enabled(struct vm *vm); |
| 372 | bool vm_locked_are_notifications_enabled(struct vm_locked vm_locked); |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 373 | bool vm_notifications_validate_per_vcpu(struct vm_locked vm_locked, |
| 374 | bool is_from_vm, bool is_per_vcpu, |
| 375 | ffa_notifications_bitmap_t notif); |
| 376 | bool vm_notifications_validate_bound_sender( |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 377 | struct vm_locked vm_locked, bool is_from_vm, ffa_id_t sender_id, |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 378 | ffa_notifications_bitmap_t notifications); |
| 379 | bool vm_notifications_validate_binding(struct vm_locked vm_locked, |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 380 | bool is_from_vm, ffa_id_t sender_id, |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 381 | ffa_notifications_bitmap_t notifications, |
| 382 | bool is_per_vcpu); |
| 383 | void vm_notifications_update_bindings(struct vm_locked vm_locked, |
J-Alves | 19e20cf | 2023-08-02 12:48:55 +0100 | [diff] [blame] | 384 | bool is_from_vm, ffa_id_t sender_id, |
J-Alves | c003a7a | 2021-03-18 13:06:53 +0000 | [diff] [blame] | 385 | ffa_notifications_bitmap_t notifications, |
| 386 | bool is_per_vcpu); |
J-Alves | 5a16c96 | 2022-03-25 12:32:51 +0000 | [diff] [blame] | 387 | void vm_notifications_partition_set_pending( |
| 388 | struct vm_locked vm_locked, bool is_from_vm, |
| 389 | ffa_notifications_bitmap_t notifications, ffa_vcpu_index_t vcpu_id, |
| 390 | bool is_per_vcpu); |
J-Alves | 5136dda | 2022-03-25 12:26:38 +0000 | [diff] [blame] | 391 | ffa_notifications_bitmap_t vm_notifications_partition_get_pending( |
| 392 | struct vm_locked vm_locked, bool is_from_vm, ffa_vcpu_index_t vcpu_id); |
J-Alves | 14163a7 | 2022-03-25 14:01:34 +0000 | [diff] [blame] | 393 | void vm_notifications_framework_set_pending( |
| 394 | struct vm_locked vm_locked, ffa_notifications_bitmap_t notifications); |
J-Alves | 663682a | 2022-03-25 13:56:51 +0000 | [diff] [blame] | 395 | ffa_notifications_bitmap_t vm_notifications_framework_get_pending( |
| 396 | struct vm_locked vm_locked); |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 397 | void vm_notifications_info_get_pending( |
| 398 | struct vm_locked vm_locked, bool is_from_vm, uint16_t *ids, |
| 399 | uint32_t *ids_count, uint32_t *lists_sizes, uint32_t *lists_count, |
Karl Meakin | 2ad6b66 | 2024-07-29 20:45:40 +0100 | [diff] [blame] | 400 | uint32_t ids_max_count, |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 401 | enum notifications_info_get_state *info_get_state); |
J-Alves | fe23ebe | 2021-10-13 16:07:07 +0100 | [diff] [blame] | 402 | bool vm_notifications_pending_not_retrieved_by_scheduler(void); |
| 403 | bool vm_is_notifications_pending_count_zero(void); |
J-Alves | c8e8a22 | 2021-06-08 17:33:52 +0100 | [diff] [blame] | 404 | bool vm_notifications_info_get(struct vm_locked vm_locked, uint16_t *ids, |
| 405 | uint32_t *ids_count, uint32_t *lists_sizes, |
Karl Meakin | 2ad6b66 | 2024-07-29 20:45:40 +0100 | [diff] [blame] | 406 | uint32_t *lists_count, uint32_t ids_max_count); |
Kathleen Capella | f71dee4 | 2023-08-08 16:24:14 -0400 | [diff] [blame] | 407 | bool vm_supports_messaging_method(struct vm *vm, uint16_t messaging_method); |
J-Alves | 7d38f7b | 2022-04-13 13:22:30 +0100 | [diff] [blame] | 408 | void vm_set_boot_info_gp_reg(struct vm *vm, struct vcpu *vcpu); |
Olivier Deprez | 2013775 | 2023-02-07 10:55:35 +0100 | [diff] [blame] | 409 | |
| 410 | /** |
Olivier Deprez | 2013775 | 2023-02-07 10:55:35 +0100 | [diff] [blame] | 411 | * Returns true if the VM requested to receive cpu off power management |
| 412 | * events. |
| 413 | */ |
| 414 | static inline bool vm_power_management_cpu_off_requested(struct vm *vm) |
| 415 | { |
| 416 | return (vm->power_management & |
| 417 | (UINT32_C(1) << VM_POWER_MANAGEMENT_CPU_OFF_SHIFT)) != 0; |
| 418 | } |
Madhukar Pappireddy | 18c6eb7 | 2023-08-21 12:16:18 -0500 | [diff] [blame] | 419 | |
Karl Meakin | 8204182 | 2024-05-20 11:14:34 +0100 | [diff] [blame] | 420 | /* Return true if `vm` is a UP. */ |
| 421 | static inline bool vm_is_up(const struct vm *vm) |
| 422 | { |
| 423 | return vm->vcpu_count == 1; |
| 424 | } |
| 425 | |
| 426 | /* Return true if `vm` is a MP. */ |
| 427 | static inline bool vm_is_mp(const struct vm *vm) |
| 428 | { |
| 429 | return vm->vcpu_count > 1; |
| 430 | } |
| 431 | |
Karl Meakin | 5e99699 | 2024-05-20 11:27:07 +0100 | [diff] [blame] | 432 | /* Return true if `vm` is the primary VM. */ |
| 433 | static inline bool vm_is_primary(const struct vm *vm) |
| 434 | { |
| 435 | return vm->id == HF_PRIMARY_VM_ID; |
| 436 | } |
| 437 | |
Daniel Boulby | f3cf28c | 2024-08-22 10:46:23 +0100 | [diff] [blame] | 438 | /** |
| 439 | * Convert a CPU ID for a secondary VM to the corresponding vCPU index. |
| 440 | */ |
| 441 | static inline ffa_vcpu_index_t vcpu_id_to_index(cpu_id_t vcpu_id) |
| 442 | { |
| 443 | /* For now we use indices as IDs. */ |
| 444 | return vcpu_id; |
| 445 | } |
| 446 | |
Madhukar Pappireddy | 18c6eb7 | 2023-08-21 12:16:18 -0500 | [diff] [blame] | 447 | struct interrupt_descriptor *vm_interrupt_set_target_mpidr( |
| 448 | struct vm_locked vm_locked, uint32_t id, uint32_t target_mpidr); |
| 449 | struct interrupt_descriptor *vm_interrupt_set_sec_state( |
| 450 | struct vm_locked vm_locked, uint32_t id, uint32_t sec_state); |
Madhukar Pappireddy | 938faaf | 2023-07-31 17:56:55 -0500 | [diff] [blame] | 451 | struct interrupt_descriptor *vm_interrupt_set_enable(struct vm_locked vm_locked, |
| 452 | uint32_t id, bool enable); |
Madhukar Pappireddy | a49ba16 | 2024-11-25 09:40:45 -0600 | [diff] [blame] | 453 | |
| 454 | void vm_update_boot(struct vm *vm); |
| 455 | struct vm *vm_get_boot_vm(void); |
Madhukar Pappireddy | a81f541 | 2024-11-25 09:46:48 -0600 | [diff] [blame] | 456 | struct vm *vm_get_boot_vm_secondary_core(void); |
Madhukar Pappireddy | a49ba16 | 2024-11-25 09:40:45 -0600 | [diff] [blame] | 457 | struct vm *vm_get_next_boot(struct vm *vm); |
Madhukar Pappireddy | a81f541 | 2024-11-25 09:46:48 -0600 | [diff] [blame] | 458 | struct vm *vm_get_next_boot_secondary_core(struct vm *vm); |
Madhukar Pappireddy | 84259c9 | 2025-06-27 14:28:45 -0500 | [diff] [blame] | 459 | enum vm_state vm_read_state(struct vm *vm); |
| 460 | bool vm_set_state(struct vm_locked vm_locked, enum vm_state to_state); |
Madhukar Pappireddy | 6cb80be | 2025-06-02 16:10:51 -0500 | [diff] [blame] | 461 | bool vm_is_discoverable(struct vm *vm); |