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 { |
| 51 | SPCI_MEMORY_DONATE = 0x2, |
| 52 | }; |
| 53 | |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 54 | /* SPCI function specific constants. */ |
Andrew Scull | 1262ac2 | 2019-04-05 12:44:26 +0100 | [diff] [blame] | 55 | #define SPCI_MSG_RECV_BLOCK_MASK 0x1 |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 56 | #define SPCI_MSG_SEND_NOTIFY_MASK 0x1 |
| 57 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 58 | #define SPCI_MESSAGE_ARCHITECTED 0x0 |
| 59 | #define SPCI_MESSAGE_IMPDEF 0x1 |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 60 | #define SPCI_MESSAGE_IMPDEF_MASK 0x1 |
| 61 | |
| 62 | #define SPCI_MSG_SEND_NOTIFY 0x1 |
Andrew Scull | 1262ac2 | 2019-04-05 12:44:26 +0100 | [diff] [blame] | 63 | #define SPCI_MSG_RECV_BLOCK 0x1 |
| 64 | |
| 65 | /* The maximum length possible for a single message. */ |
| 66 | #define SPCI_MSG_PAYLOAD_MAX (HF_MAILBOX_SIZE - sizeof(struct spci_message)) |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 67 | |
| 68 | /* clang-format on */ |
| 69 | |
Andrew Walbran | 52d9967 | 2019-06-25 15:51:11 +0100 | [diff] [blame] | 70 | /** The ID of a VM. These are assigned sequentially. */ |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 71 | typedef uint16_t spci_vm_id_t; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 72 | typedef uint32_t spci_memory_handle_t; |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 73 | |
Andrew Walbran | 52d9967 | 2019-06-25 15:51:11 +0100 | [diff] [blame] | 74 | /** |
| 75 | * A count of VMs. This has the same range as the VM IDs but we give it a |
| 76 | * different name to make the different semantics clear. |
| 77 | */ |
| 78 | typedef spci_vm_id_t spci_vm_count_t; |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 79 | |
| 80 | /** The index of a vCPU within a particular VM. */ |
Andrew Walbran | b037d5b | 2019-06-25 17:19:41 +0100 | [diff] [blame] | 81 | typedef uint16_t spci_vcpu_index_t; |
Andrew Walbran | 52d9967 | 2019-06-25 15:51:11 +0100 | [diff] [blame] | 82 | |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 83 | /** |
| 84 | * A count of vCPUs. This has the same range as the vCPU indices but we give it |
| 85 | * a different name to make the different semantics clear. |
| 86 | */ |
| 87 | typedef spci_vcpu_index_t spci_vcpu_count_t; |
| 88 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 89 | /** Return type of SPCI functions. */ |
| 90 | /* TODO: Reuse spci_return_t type on all SPCI functions declarations. */ |
| 91 | typedef int32_t spci_return_t; |
| 92 | |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 93 | /** SPCI common message header. */ |
| 94 | struct spci_message { |
| 95 | /* |
| 96 | * TODO: version is part of SPCI alpha2 but will be |
| 97 | * removed in the next spec revision hence we are not |
| 98 | * including it in the header. |
| 99 | */ |
| 100 | |
| 101 | /** |
| 102 | * flags[0]: |
| 103 | * 0: Architected message payload; |
| 104 | * 1: Implementation defined message payload. |
| 105 | * flags[15:1] reserved (MBZ). |
| 106 | */ |
| 107 | uint16_t flags; |
| 108 | |
| 109 | /* |
| 110 | * TODO: Padding is present to ensure controlled offset |
| 111 | * of the length field. SPCI spec must be updated |
| 112 | * to reflect this (TBD). |
| 113 | */ |
| 114 | uint16_t reserved_1; |
Andrew Scull | a1aa2ba | 2019-04-05 11:49:02 +0100 | [diff] [blame] | 115 | |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 116 | uint32_t length; |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 117 | spci_vm_id_t target_vm_id; |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 118 | spci_vm_id_t source_vm_id; |
| 119 | |
| 120 | /* |
| 121 | * TODO: Padding is present to ensure that the field |
| 122 | * payload alignment is 64B. SPCI spec must be updated |
| 123 | * to reflect this. |
| 124 | */ |
| 125 | uint32_t reserved_2; |
| 126 | |
| 127 | uint8_t payload[]; |
| 128 | }; |
| 129 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 130 | struct spci_architected_message_header { |
| 131 | uint16_t type; |
| 132 | |
| 133 | /* |
| 134 | * TODO: Padding is present to ensure that the field |
| 135 | * payload is aligned on a 64B boundary. SPCI |
| 136 | * spec must be updated to reflect this. |
| 137 | */ |
| 138 | uint16_t reserved[3]; |
| 139 | uint8_t payload[]; |
| 140 | }; |
| 141 | |
| 142 | struct spci_memory_region_constituent { |
| 143 | uint64_t address; |
| 144 | uint32_t page_count; |
| 145 | |
| 146 | uint32_t reserved; |
| 147 | }; |
| 148 | |
| 149 | struct spci_memory_region { |
| 150 | spci_memory_handle_t handle; |
| 151 | uint32_t count; |
| 152 | |
| 153 | struct spci_memory_region_constituent constituents[]; |
| 154 | }; |
| 155 | |
| 156 | /* TODO: Move all the functions below this line to a support library. */ |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 157 | /** |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 158 | * 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] | 159 | */ |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 160 | static inline void spci_common_header_init(struct spci_message *message, |
| 161 | uint32_t message_length, |
| 162 | spci_vm_id_t target_vm_id, |
| 163 | spci_vm_id_t source_vm_id) |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 164 | { |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 165 | message->length = message_length; |
| 166 | message->target_vm_id = target_vm_id; |
| 167 | message->source_vm_id = source_vm_id; |
| 168 | |
| 169 | /* |
| 170 | * TODO: Reserved fields in the common message header will be |
| 171 | * defined as MBZ in next SPCI spec updates. |
| 172 | */ |
| 173 | message->reserved_1 = 0; |
| 174 | message->reserved_2 = 0; |
| 175 | } |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 176 | |
| 177 | /** |
| 178 | * Set the SPCI implementation defined message header fields. |
| 179 | */ |
| 180 | static inline void spci_message_init(struct spci_message *message, |
| 181 | uint32_t message_length, |
| 182 | spci_vm_id_t target_vm_id, |
| 183 | spci_vm_id_t source_vm_id) |
| 184 | { |
| 185 | spci_common_header_init(message, message_length, target_vm_id, |
| 186 | source_vm_id); |
| 187 | |
| 188 | message->flags = SPCI_MESSAGE_IMPDEF; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Obtain a pointer to the architected header in the spci_message. |
| 193 | * |
| 194 | * Note: the argument "message" has const qualifier. This qualifier |
| 195 | * is meant to forbid changes in information enclosed in the |
| 196 | * struct spci_message. The spci_architected_message_header, for which |
| 197 | * a pointer is returned in this function, is not part of spci_message. |
| 198 | * Its information is meant to be changed and hence the returned pointer |
| 199 | * does not have const type qualifier. |
| 200 | */ |
| 201 | static inline struct spci_architected_message_header * |
| 202 | spci_get_architected_message_header(const struct spci_message *message) |
| 203 | { |
| 204 | return (struct spci_architected_message_header *)message->payload; |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Helper method to fill in the information about the architected message. |
| 209 | */ |
| 210 | static inline void spci_architected_message_init(struct spci_message *message, |
| 211 | uint32_t message_length, |
| 212 | spci_vm_id_t target_vm_id, |
| 213 | spci_vm_id_t source_vm_id, |
| 214 | enum spci_memory_share type) |
| 215 | { |
| 216 | struct spci_architected_message_header *architected_header; |
| 217 | |
| 218 | spci_common_header_init(message, message_length, target_vm_id, |
| 219 | source_vm_id); |
| 220 | message->flags = SPCI_MESSAGE_ARCHITECTED; |
| 221 | |
| 222 | /* Fill the architected header. */ |
| 223 | architected_header = spci_get_architected_message_header(message); |
| 224 | architected_header->type = type; |
| 225 | architected_header->reserved[0] = 0; |
| 226 | architected_header->reserved[1] = 0; |
| 227 | architected_header->reserved[2] = 0; |
| 228 | } |
| 229 | |
| 230 | /** Obtain a pointer to the start of the memory region in the donate message. */ |
| 231 | static inline struct spci_memory_region *spci_get_donated_memory_region( |
| 232 | struct spci_message *message) |
| 233 | { |
| 234 | struct spci_architected_message_header *architected_header = |
| 235 | spci_get_architected_message_header(message); |
| 236 | return (struct spci_memory_region *)architected_header->payload; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Add a memory region to the current message. |
| 241 | * A memory region is composed of one or more constituents. |
| 242 | */ |
| 243 | static inline void spci_memory_region_add( |
| 244 | struct spci_message *message, spci_memory_handle_t handle, |
| 245 | const struct spci_memory_region_constituent constituents[], |
| 246 | uint32_t num_constituents) |
| 247 | { |
| 248 | struct spci_memory_region *memory_region = |
| 249 | spci_get_donated_memory_region(message); |
| 250 | |
| 251 | uint32_t constituents_length = |
| 252 | num_constituents * |
| 253 | sizeof(struct spci_memory_region_constituent); |
| 254 | uint32_t index; |
| 255 | |
| 256 | memory_region->handle = handle; |
| 257 | memory_region->count = num_constituents; |
| 258 | |
| 259 | for (index = 0; index < num_constituents; index++) { |
| 260 | memory_region->constituents[index] = constituents[index]; |
| 261 | memory_region->constituents[index].reserved = 0; |
| 262 | } |
| 263 | |
| 264 | /* |
| 265 | * TODO: Add assert ensuring that the specified message |
| 266 | * length is not greater than SPCI_MSG_PAYLOAD_MAX. |
| 267 | */ |
| 268 | message->length += |
| 269 | sizeof(struct spci_memory_region) + constituents_length; |
| 270 | } |
| 271 | |
| 272 | /** Construct the SPCI donate memory region message. */ |
| 273 | static inline void spci_memory_donate( |
| 274 | struct spci_message *message, spci_vm_id_t target_vm_id, |
| 275 | spci_vm_id_t source_vm_id, |
| 276 | struct spci_memory_region_constituent *region_constituents, |
| 277 | uint32_t num_elements, uint32_t handle) |
| 278 | { |
| 279 | int32_t message_length; |
| 280 | |
| 281 | message_length = sizeof(struct spci_architected_message_header); |
| 282 | |
| 283 | /* Fill in the details on the common message header. */ |
| 284 | spci_architected_message_init(message, message_length, target_vm_id, |
| 285 | source_vm_id, SPCI_MEMORY_DONATE); |
| 286 | |
| 287 | /* Create single memory region. */ |
| 288 | spci_memory_region_add(message, handle, region_constituents, |
| 289 | num_elements); |
| 290 | } |