Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 The Hafnium Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 17 | #pragma once |
| 18 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 19 | #include "hf/types.h" |
| 20 | |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 21 | /* clang-format off */ |
| 22 | |
| 23 | #define SPCI_LOW_32_ID 0x84000060 |
| 24 | #define SPCI_HIGH_32_ID 0x8400007F |
| 25 | #define SPCI_LOW_64_ID 0xC4000060 |
| 26 | #define SPCI_HIGH_32_ID 0x8400007F |
| 27 | |
| 28 | /* SPCI function identifiers. */ |
| 29 | #define SPCI_VERSION_32 0x84000060 |
| 30 | #define SPCI_MSG_BUF_LIST_EXCHANGE_32 0x84000061 |
| 31 | #define SPCI_MSG_RECV_32 0x84000062 |
| 32 | #define SPCI_MSG_PUT_32 0x84000063 |
| 33 | #define SPCI_MSG_SEND_32 0x84000064 |
| 34 | #define SPCI_MSG_SEND_REC_32 0x84000065 |
| 35 | #define SPCI_RUN_32 0x84000066 |
| 36 | #define SPCI_YIELD_32 0x84000067 |
| 37 | |
| 38 | /* SPCI return codes. */ |
| 39 | #define SPCI_SUCCESS INT32_C(0) |
| 40 | #define SPCI_NOT_SUPPORTED INT32_C(-1) |
| 41 | #define SPCI_INVALID_PARAMETERS INT32_C(-2) |
| 42 | #define SPCI_NO_MEMORY INT32_C(-3) |
| 43 | #define SPCI_BUSY INT32_C(-4) |
| 44 | #define SPCI_INTERRUPTED INT32_C(-5) |
| 45 | #define SPCI_DENIED INT32_C(-6) |
| 46 | /* TODO: return code currently undefined in SPCI alpha2. */ |
| 47 | #define SPCI_RETRY INT32_C(-7) |
| 48 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 49 | /* Architected memory sharing message IDs. */ |
| 50 | enum spci_memory_share { |
Jose Marinho | 56c2573 | 2019-05-20 09:48:53 +0100 | [diff] [blame^] | 51 | SPCI_MEMORY_RELINQUISH = 0x1, |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 52 | SPCI_MEMORY_DONATE = 0x2, |
| 53 | }; |
| 54 | |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 55 | /* SPCI function specific constants. */ |
Andrew Scull | 1262ac2 | 2019-04-05 12:44:26 +0100 | [diff] [blame] | 56 | #define SPCI_MSG_RECV_BLOCK_MASK 0x1 |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 57 | #define SPCI_MSG_SEND_NOTIFY_MASK 0x1 |
| 58 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 59 | #define SPCI_MESSAGE_ARCHITECTED 0x0 |
| 60 | #define SPCI_MESSAGE_IMPDEF 0x1 |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 61 | #define SPCI_MESSAGE_IMPDEF_MASK 0x1 |
| 62 | |
| 63 | #define SPCI_MSG_SEND_NOTIFY 0x1 |
Andrew Scull | 1262ac2 | 2019-04-05 12:44:26 +0100 | [diff] [blame] | 64 | #define SPCI_MSG_RECV_BLOCK 0x1 |
| 65 | |
| 66 | /* The maximum length possible for a single message. */ |
| 67 | #define SPCI_MSG_PAYLOAD_MAX (HF_MAILBOX_SIZE - sizeof(struct spci_message)) |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 68 | |
| 69 | /* clang-format on */ |
| 70 | |
Fuad Tabba | 494376e | 2019-08-05 12:35:10 +0100 | [diff] [blame] | 71 | /** The ID of a VM. These are assigned sequentially starting with an offset. */ |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 72 | typedef uint16_t spci_vm_id_t; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 73 | typedef uint32_t spci_memory_handle_t; |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 74 | |
Andrew Walbran | 52d9967 | 2019-06-25 15:51:11 +0100 | [diff] [blame] | 75 | /** |
| 76 | * A count of VMs. This has the same range as the VM IDs but we give it a |
| 77 | * different name to make the different semantics clear. |
| 78 | */ |
| 79 | typedef spci_vm_id_t spci_vm_count_t; |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 80 | |
| 81 | /** The index of a vCPU within a particular VM. */ |
Andrew Walbran | b037d5b | 2019-06-25 17:19:41 +0100 | [diff] [blame] | 82 | typedef uint16_t spci_vcpu_index_t; |
Andrew Walbran | 52d9967 | 2019-06-25 15:51:11 +0100 | [diff] [blame] | 83 | |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 84 | /** |
| 85 | * A count of vCPUs. This has the same range as the vCPU indices but we give it |
| 86 | * a different name to make the different semantics clear. |
| 87 | */ |
| 88 | typedef spci_vcpu_index_t spci_vcpu_count_t; |
| 89 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 90 | /** Return type of SPCI functions. */ |
| 91 | /* TODO: Reuse spci_return_t type on all SPCI functions declarations. */ |
| 92 | typedef int32_t spci_return_t; |
| 93 | |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 94 | /** SPCI common message header. */ |
| 95 | struct spci_message { |
| 96 | /* |
| 97 | * TODO: version is part of SPCI alpha2 but will be |
| 98 | * removed in the next spec revision hence we are not |
| 99 | * including it in the header. |
| 100 | */ |
| 101 | |
| 102 | /** |
| 103 | * flags[0]: |
| 104 | * 0: Architected message payload; |
| 105 | * 1: Implementation defined message payload. |
| 106 | * flags[15:1] reserved (MBZ). |
| 107 | */ |
| 108 | uint16_t flags; |
| 109 | |
| 110 | /* |
| 111 | * TODO: Padding is present to ensure controlled offset |
| 112 | * of the length field. SPCI spec must be updated |
| 113 | * to reflect this (TBD). |
| 114 | */ |
| 115 | uint16_t reserved_1; |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 116 | |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 117 | uint32_t length; |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 118 | spci_vm_id_t target_vm_id; |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 119 | spci_vm_id_t source_vm_id; |
| 120 | |
| 121 | /* |
| 122 | * TODO: Padding is present to ensure that the field |
| 123 | * payload alignment is 64B. SPCI spec must be updated |
| 124 | * to reflect this. |
| 125 | */ |
| 126 | uint32_t reserved_2; |
| 127 | |
| 128 | uint8_t payload[]; |
| 129 | }; |
| 130 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 131 | struct spci_architected_message_header { |
| 132 | uint16_t type; |
| 133 | |
| 134 | /* |
| 135 | * TODO: Padding is present to ensure that the field |
| 136 | * payload is aligned on a 64B boundary. SPCI |
| 137 | * spec must be updated to reflect this. |
| 138 | */ |
| 139 | uint16_t reserved[3]; |
| 140 | uint8_t payload[]; |
| 141 | }; |
| 142 | |
| 143 | struct spci_memory_region_constituent { |
| 144 | uint64_t address; |
| 145 | uint32_t page_count; |
| 146 | |
| 147 | uint32_t reserved; |
| 148 | }; |
| 149 | |
| 150 | struct spci_memory_region { |
| 151 | spci_memory_handle_t handle; |
| 152 | uint32_t count; |
| 153 | |
| 154 | struct spci_memory_region_constituent constituents[]; |
| 155 | }; |
| 156 | |
| 157 | /* TODO: Move all the functions below this line to a support library. */ |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 158 | /** |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 159 | * Fill all the fields, except for the flags, in the SPCI message common header. |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 160 | */ |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 161 | static inline void spci_common_header_init(struct spci_message *message, |
| 162 | uint32_t message_length, |
| 163 | spci_vm_id_t target_vm_id, |
| 164 | spci_vm_id_t source_vm_id) |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 165 | { |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 166 | message->length = message_length; |
| 167 | message->target_vm_id = target_vm_id; |
| 168 | message->source_vm_id = source_vm_id; |
| 169 | |
| 170 | /* |
| 171 | * TODO: Reserved fields in the common message header will be |
| 172 | * defined as MBZ in next SPCI spec updates. |
| 173 | */ |
| 174 | message->reserved_1 = 0; |
| 175 | message->reserved_2 = 0; |
| 176 | } |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * Set the SPCI implementation defined message header fields. |
| 180 | */ |
| 181 | static inline void spci_message_init(struct spci_message *message, |
| 182 | uint32_t message_length, |
| 183 | spci_vm_id_t target_vm_id, |
| 184 | spci_vm_id_t source_vm_id) |
| 185 | { |
| 186 | spci_common_header_init(message, message_length, target_vm_id, |
| 187 | source_vm_id); |
| 188 | |
| 189 | message->flags = SPCI_MESSAGE_IMPDEF; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Obtain a pointer to the architected header in the spci_message. |
| 194 | * |
| 195 | * Note: the argument "message" has const qualifier. This qualifier |
| 196 | * is meant to forbid changes in information enclosed in the |
| 197 | * struct spci_message. The spci_architected_message_header, for which |
| 198 | * a pointer is returned in this function, is not part of spci_message. |
| 199 | * Its information is meant to be changed and hence the returned pointer |
| 200 | * does not have const type qualifier. |
| 201 | */ |
| 202 | static inline struct spci_architected_message_header * |
| 203 | spci_get_architected_message_header(const struct spci_message *message) |
| 204 | { |
| 205 | return (struct spci_architected_message_header *)message->payload; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Helper method to fill in the information about the architected message. |
| 210 | */ |
| 211 | static inline void spci_architected_message_init(struct spci_message *message, |
| 212 | uint32_t message_length, |
| 213 | spci_vm_id_t target_vm_id, |
| 214 | spci_vm_id_t source_vm_id, |
| 215 | enum spci_memory_share type) |
| 216 | { |
| 217 | struct spci_architected_message_header *architected_header; |
| 218 | |
| 219 | spci_common_header_init(message, message_length, target_vm_id, |
| 220 | source_vm_id); |
| 221 | message->flags = SPCI_MESSAGE_ARCHITECTED; |
| 222 | |
| 223 | /* Fill the architected header. */ |
| 224 | architected_header = spci_get_architected_message_header(message); |
| 225 | architected_header->type = type; |
| 226 | architected_header->reserved[0] = 0; |
| 227 | architected_header->reserved[1] = 0; |
| 228 | architected_header->reserved[2] = 0; |
| 229 | } |
| 230 | |
| 231 | /** Obtain a pointer to the start of the memory region in the donate message. */ |
| 232 | static inline struct spci_memory_region *spci_get_donated_memory_region( |
| 233 | struct spci_message *message) |
| 234 | { |
| 235 | struct spci_architected_message_header *architected_header = |
| 236 | spci_get_architected_message_header(message); |
| 237 | return (struct spci_memory_region *)architected_header->payload; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Add a memory region to the current message. |
| 242 | * A memory region is composed of one or more constituents. |
| 243 | */ |
| 244 | static inline void spci_memory_region_add( |
| 245 | struct spci_message *message, spci_memory_handle_t handle, |
| 246 | const struct spci_memory_region_constituent constituents[], |
| 247 | uint32_t num_constituents) |
| 248 | { |
| 249 | struct spci_memory_region *memory_region = |
| 250 | spci_get_donated_memory_region(message); |
| 251 | |
| 252 | uint32_t constituents_length = |
| 253 | num_constituents * |
| 254 | sizeof(struct spci_memory_region_constituent); |
| 255 | uint32_t index; |
| 256 | |
| 257 | memory_region->handle = handle; |
| 258 | memory_region->count = num_constituents; |
| 259 | |
| 260 | for (index = 0; index < num_constituents; index++) { |
| 261 | memory_region->constituents[index] = constituents[index]; |
| 262 | memory_region->constituents[index].reserved = 0; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * TODO: Add assert ensuring that the specified message |
| 267 | * length is not greater than SPCI_MSG_PAYLOAD_MAX. |
| 268 | */ |
| 269 | message->length += |
| 270 | sizeof(struct spci_memory_region) + constituents_length; |
| 271 | } |
| 272 | |
| 273 | /** Construct the SPCI donate memory region message. */ |
| 274 | static inline void spci_memory_donate( |
| 275 | struct spci_message *message, spci_vm_id_t target_vm_id, |
| 276 | spci_vm_id_t source_vm_id, |
| 277 | struct spci_memory_region_constituent *region_constituents, |
| 278 | uint32_t num_elements, uint32_t handle) |
| 279 | { |
| 280 | int32_t message_length; |
| 281 | |
| 282 | message_length = sizeof(struct spci_architected_message_header); |
| 283 | |
| 284 | /* Fill in the details on the common message header. */ |
| 285 | spci_architected_message_init(message, message_length, target_vm_id, |
| 286 | source_vm_id, SPCI_MEMORY_DONATE); |
| 287 | |
| 288 | /* Create single memory region. */ |
| 289 | spci_memory_region_add(message, handle, region_constituents, |
| 290 | num_elements); |
| 291 | } |
Jose Marinho | 56c2573 | 2019-05-20 09:48:53 +0100 | [diff] [blame^] | 292 | |
| 293 | /** |
| 294 | * Construct the SPCI memory region relinquish message. |
| 295 | * A set of memory regions can be given back to the owner. |
| 296 | */ |
| 297 | static inline void spci_memory_relinquish( |
| 298 | struct spci_message *message, spci_vm_id_t target_vm_id, |
| 299 | spci_vm_id_t source_vm_id, |
| 300 | struct spci_memory_region_constituent *region_constituents, |
| 301 | uint64_t num_elements, uint32_t handle) |
| 302 | { |
| 303 | int32_t message_length; |
| 304 | |
| 305 | message_length = sizeof(struct spci_architected_message_header); |
| 306 | |
| 307 | /* Fill in the details on the common message header. */ |
| 308 | spci_architected_message_init(message, message_length, target_vm_id, |
| 309 | source_vm_id, SPCI_MEMORY_RELINQUISH); |
| 310 | |
| 311 | /* Create single memory region. */ |
| 312 | spci_memory_region_add(message, handle, region_constituents, |
| 313 | num_elements); |
| 314 | } |