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 | |
Andrew Walbran | a422445 | 2019-11-07 13:07:50 +0000 | [diff] [blame] | 21 | /* |
| 22 | * Copied from hf/arch/std.h because we can't include Hafnium internal headers |
| 23 | * here. |
| 24 | */ |
| 25 | #ifndef align_up |
| 26 | #define align_up(v, a) (((uintptr_t)(v) + (a - 1)) & ~(a - 1)) |
| 27 | #endif |
| 28 | |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 29 | /* clang-format off */ |
| 30 | |
| 31 | #define SPCI_LOW_32_ID 0x84000060 |
| 32 | #define SPCI_HIGH_32_ID 0x8400007F |
| 33 | #define SPCI_LOW_64_ID 0xC4000060 |
| 34 | #define SPCI_HIGH_32_ID 0x8400007F |
| 35 | |
| 36 | /* SPCI function identifiers. */ |
Andrew Walbran | 0de4f16 | 2019-09-03 16:44:20 +0100 | [diff] [blame] | 37 | #define SPCI_ERROR_32 0x84000060 |
| 38 | #define SPCI_SUCCESS_32 0x84000061 |
| 39 | #define SPCI_INTERRUPT_32 0x84000062 |
| 40 | #define SPCI_VERSION_32 0x84000063 |
Jose Marinho | 021528f | 2019-10-08 17:21:20 +0100 | [diff] [blame] | 41 | #define SPCI_FEATURES_32 0x84000064 |
| 42 | #define SPCI_RX_RELEASE_32 0x84000065 |
| 43 | #define SPCI_RXTX_MAP_32 0x84000066 |
| 44 | #define SPCI_RXTX_UNMAP_32 0x84000067 |
| 45 | #define SPCI_PARTITION_INFO_GET_32 0x84000068 |
| 46 | #define SPCI_ID_GET_32 0x84000069 |
Andrew Walbran | 0de4f16 | 2019-09-03 16:44:20 +0100 | [diff] [blame] | 47 | #define SPCI_MSG_POLL_32 0x8400006A |
Jose Marinho | 021528f | 2019-10-08 17:21:20 +0100 | [diff] [blame] | 48 | #define SPCI_MSG_WAIT_32 0x8400006B |
| 49 | #define SPCI_YIELD_32 0x8400006C |
Andrew Walbran | 0de4f16 | 2019-09-03 16:44:20 +0100 | [diff] [blame] | 50 | #define SPCI_RUN_32 0x8400006D |
Jose Marinho | 021528f | 2019-10-08 17:21:20 +0100 | [diff] [blame] | 51 | #define SPCI_MSG_SEND_32 0x8400006E |
| 52 | #define SPCI_MSG_SEND_DIRECT_REQ_32 0x8400006F |
| 53 | #define SPCI_MSG_SEND_DIRECT_RESP_32 0x84000070 |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 54 | |
Andrew Walbran | 0de4f16 | 2019-09-03 16:44:20 +0100 | [diff] [blame] | 55 | /* SPCI error codes. */ |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 56 | #define SPCI_NOT_SUPPORTED INT32_C(-1) |
| 57 | #define SPCI_INVALID_PARAMETERS INT32_C(-2) |
| 58 | #define SPCI_NO_MEMORY INT32_C(-3) |
| 59 | #define SPCI_BUSY INT32_C(-4) |
| 60 | #define SPCI_INTERRUPTED INT32_C(-5) |
| 61 | #define SPCI_DENIED INT32_C(-6) |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 62 | #define SPCI_RETRY INT32_C(-7) |
Andrew Walbran | f0c314d | 2019-10-02 14:24:26 +0100 | [diff] [blame] | 63 | #define SPCI_ABORTED INT32_C(-8) |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 64 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 65 | /* Architected memory sharing message IDs. */ |
| 66 | enum spci_memory_share { |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 67 | SPCI_MEMORY_DONATE = 0x0, |
| 68 | SPCI_MEMORY_LEND = 0x1, |
| 69 | SPCI_MEMORY_SHARE = 0x2, |
| 70 | SPCI_MEMORY_RELINQUISH = 0x3, |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 71 | }; |
| 72 | |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 73 | /* SPCI function specific constants. */ |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 74 | #define SPCI_MSG_RECV_BLOCK 0x1 |
Andrew Scull | 1262ac2 | 2019-04-05 12:44:26 +0100 | [diff] [blame] | 75 | #define SPCI_MSG_RECV_BLOCK_MASK 0x1 |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 76 | |
| 77 | #define SPCI_MSG_SEND_NOTIFY 0x1 |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 78 | #define SPCI_MSG_SEND_NOTIFY_MASK 0x1 |
| 79 | #define SPCI_MSG_SEND_LEGACY_MEMORY 0x2 |
| 80 | #define SPCI_MSG_SEND_LEGACY_MEMORY_MASK 0x2 |
Andrew Scull | 1262ac2 | 2019-04-05 12:44:26 +0100 | [diff] [blame] | 81 | |
Andrew Walbran | f0c314d | 2019-10-02 14:24:26 +0100 | [diff] [blame] | 82 | #define SPCI_SLEEP_INDEFINITE 0 |
| 83 | |
Andrew Walbran | bfffb0f | 2019-11-05 14:02:34 +0000 | [diff] [blame] | 84 | /** |
| 85 | * For use where the SPCI specification refers explicitly to '4K pages'. Not to |
| 86 | * be confused with PAGE_SIZE, which is the translation granule Hafnium is |
| 87 | * configured to use. |
| 88 | */ |
| 89 | #define SPCI_PAGE_SIZE 4096 |
| 90 | |
Andrew Scull | 1262ac2 | 2019-04-05 12:44:26 +0100 | [diff] [blame] | 91 | /* The maximum length possible for a single message. */ |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 92 | #define SPCI_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 93 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 94 | enum spci_memory_access { |
| 95 | SPCI_MEMORY_RO_NX, |
| 96 | SPCI_MEMORY_RO_X, |
| 97 | SPCI_MEMORY_RW_NX, |
| 98 | SPCI_MEMORY_RW_X, |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 99 | }; |
| 100 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 101 | enum spci_memory_type { |
| 102 | SPCI_MEMORY_DEVICE_MEM, |
| 103 | SPCI_MEMORY_NORMAL_MEM, |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 104 | }; |
| 105 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 106 | enum spci_memory_cacheability { |
| 107 | SPCI_MEMORY_CACHE_RESERVED = 0x0, |
| 108 | SPCI_MEMORY_CACHE_NON_CACHEABLE = 0x1, |
| 109 | SPCI_MEMORY_CACHE_WRITE_THROUGH = 0x2, |
| 110 | SPCI_MEMORY_CACHE_WRITE_BACK = 0x4, |
| 111 | SPCI_MEMORY_DEV_NGNRNE = 0x0, |
| 112 | SPCI_MEMORY_DEV_NGNRE = 0x1, |
| 113 | SPCI_MEMORY_DEV_NGRE = 0x2, |
| 114 | SPCI_MEMORY_DEV_GRE = 0x3, |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 115 | }; |
| 116 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 117 | enum spci_memory_shareability { |
| 118 | SPCI_MEMORY_SHARE_NON_SHAREABLE, |
| 119 | SPCI_MEMORY_RESERVED, |
| 120 | SPCI_MEMORY_OUTER_SHAREABLE, |
| 121 | SPCI_MEMORY_INNER_SHAREABLE, |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 122 | }; |
| 123 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 124 | #define SPCI_MEMORY_ACCESS_OFFSET (0x5U) |
| 125 | #define SPCI_MEMORY_ACCESS_MASK ((0x3U) << SPCI_MEMORY_ACCESS_OFFSET) |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 126 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 127 | #define SPCI_MEMORY_TYPE_OFFSET (0x4U) |
| 128 | #define SPCI_MEMORY_TYPE_MASK ((0x1U) << SPCI_MEMORY_TYPE_OFFSET) |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 129 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 130 | #define SPCI_MEMORY_CACHEABILITY_OFFSET (0x2U) |
| 131 | #define SPCI_MEMORY_CACHEABILITY_MASK ((0x3U) <<\ |
| 132 | SPCI_MEMORY_CACHEABILITY_OFFSET) |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 133 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 134 | #define SPCI_MEMORY_SHAREABILITY_OFFSET (0x0U) |
| 135 | #define SPCI_MEMORY_SHAREABILITY_MASK ((0x3U) <<\ |
| 136 | SPCI_MEMORY_SHAREABILITY_OFFSET) |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 137 | |
| 138 | #define LEND_ATTR_FUNCTION_SET(name, offset, mask) \ |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 139 | static inline void spci_set_memory_##name##_attr(uint16_t *attr,\ |
| 140 | const enum spci_memory_##name perm)\ |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 141 | {\ |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 142 | *attr = (*attr & ~(mask)) | ((perm << offset) & mask);\ |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | #define LEND_ATTR_FUNCTION_GET(name, offset, mask) \ |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 146 | static inline enum spci_memory_##name spci_get_memory_##name##_attr(\ |
| 147 | uint16_t attr)\ |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 148 | {\ |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 149 | return (enum spci_memory_##name)((attr & mask) >> offset);\ |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 152 | LEND_ATTR_FUNCTION_SET(access, SPCI_MEMORY_ACCESS_OFFSET, |
| 153 | SPCI_MEMORY_ACCESS_MASK) |
| 154 | LEND_ATTR_FUNCTION_GET(access, SPCI_MEMORY_ACCESS_OFFSET, |
| 155 | SPCI_MEMORY_ACCESS_MASK) |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 156 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 157 | LEND_ATTR_FUNCTION_SET(type, SPCI_MEMORY_TYPE_OFFSET, SPCI_MEMORY_TYPE_MASK) |
| 158 | LEND_ATTR_FUNCTION_GET(type, SPCI_MEMORY_TYPE_OFFSET, SPCI_MEMORY_TYPE_MASK) |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 159 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 160 | LEND_ATTR_FUNCTION_SET(cacheability, SPCI_MEMORY_CACHEABILITY_OFFSET, |
| 161 | SPCI_MEMORY_CACHEABILITY_MASK) |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 162 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 163 | LEND_ATTR_FUNCTION_GET(cacheability, SPCI_MEMORY_CACHEABILITY_OFFSET, |
| 164 | SPCI_MEMORY_CACHEABILITY_MASK) |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 165 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 166 | LEND_ATTR_FUNCTION_SET(shareability, SPCI_MEMORY_SHAREABILITY_OFFSET, |
| 167 | SPCI_MEMORY_SHAREABILITY_MASK) |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 168 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 169 | LEND_ATTR_FUNCTION_GET(shareability, SPCI_MEMORY_SHAREABILITY_OFFSET, |
| 170 | SPCI_MEMORY_SHAREABILITY_MASK) |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 171 | |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 172 | /* clang-format on */ |
| 173 | |
Fuad Tabba | 494376e | 2019-08-05 12:35:10 +0100 | [diff] [blame] | 174 | /** 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] | 175 | typedef uint16_t spci_vm_id_t; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 176 | typedef uint32_t spci_memory_handle_t; |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 177 | |
Andrew Walbran | 52d9967 | 2019-06-25 15:51:11 +0100 | [diff] [blame] | 178 | /** |
| 179 | * A count of VMs. This has the same range as the VM IDs but we give it a |
| 180 | * different name to make the different semantics clear. |
| 181 | */ |
| 182 | typedef spci_vm_id_t spci_vm_count_t; |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 183 | |
| 184 | /** The index of a vCPU within a particular VM. */ |
Andrew Walbran | b037d5b | 2019-06-25 17:19:41 +0100 | [diff] [blame] | 185 | typedef uint16_t spci_vcpu_index_t; |
Andrew Walbran | 52d9967 | 2019-06-25 15:51:11 +0100 | [diff] [blame] | 186 | |
Andrew Walbran | c6d23c4 | 2019-06-26 13:30:42 +0100 | [diff] [blame] | 187 | /** |
| 188 | * A count of vCPUs. This has the same range as the vCPU indices but we give it |
| 189 | * a different name to make the different semantics clear. |
| 190 | */ |
| 191 | typedef spci_vcpu_index_t spci_vcpu_count_t; |
| 192 | |
Andrew Walbran | f18e63c | 2019-10-07 15:13:59 +0100 | [diff] [blame] | 193 | /** Parameter and return type of SPCI functions. */ |
Andrew Walbran | 7f920af | 2019-09-03 17:09:30 +0100 | [diff] [blame] | 194 | struct spci_value { |
| 195 | uint64_t func; |
| 196 | uint64_t arg1; |
| 197 | uint64_t arg2; |
| 198 | uint64_t arg3; |
| 199 | uint64_t arg4; |
| 200 | uint64_t arg5; |
| 201 | uint64_t arg6; |
| 202 | uint64_t arg7; |
| 203 | }; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 204 | |
Andrew Walbran | d4d2fa1 | 2019-10-01 16:47:25 +0100 | [diff] [blame] | 205 | static inline spci_vm_id_t spci_msg_send_sender(struct spci_value args) |
| 206 | { |
| 207 | return (args.arg1 >> 16) & 0xffff; |
| 208 | } |
| 209 | |
| 210 | static inline spci_vm_id_t spci_msg_send_receiver(struct spci_value args) |
| 211 | { |
| 212 | return args.arg1 & 0xffff; |
| 213 | } |
| 214 | |
| 215 | static inline uint32_t spci_msg_send_size(struct spci_value args) |
| 216 | { |
| 217 | return args.arg3; |
| 218 | } |
| 219 | |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 220 | static inline uint32_t spci_msg_send_attributes(struct spci_value args) |
| 221 | { |
| 222 | return args.arg4; |
| 223 | } |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 224 | |
Andrew Walbran | 4db5f3a | 2019-11-04 11:42:42 +0000 | [diff] [blame] | 225 | static inline spci_vm_id_t spci_vm_id(struct spci_value args) |
| 226 | { |
| 227 | return (args.arg1 >> 16) & 0xffff; |
| 228 | } |
| 229 | |
| 230 | static inline spci_vcpu_index_t spci_vcpu_index(struct spci_value args) |
| 231 | { |
| 232 | return args.arg1 & 0xffff; |
| 233 | } |
| 234 | |
| 235 | static inline uint64_t spci_vm_vcpu(spci_vm_id_t vm_id, |
| 236 | spci_vcpu_index_t vcpu_index) |
| 237 | { |
| 238 | return ((uint32_t)vm_id << 16) | vcpu_index; |
| 239 | } |
| 240 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 241 | struct spci_architected_message_header { |
| 242 | uint16_t type; |
| 243 | |
| 244 | /* |
| 245 | * TODO: Padding is present to ensure that the field |
| 246 | * payload is aligned on a 64B boundary. SPCI |
| 247 | * spec must be updated to reflect this. |
| 248 | */ |
| 249 | uint16_t reserved[3]; |
| 250 | uint8_t payload[]; |
| 251 | }; |
| 252 | |
| 253 | struct spci_memory_region_constituent { |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 254 | /** |
| 255 | * The base IPA of the constituent memory region, aligned to 4 kiB page |
| 256 | * size granularity. |
| 257 | */ |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 258 | uint64_t address; |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 259 | /** The number of 4 kiB pages in the constituent memory region. */ |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 260 | uint32_t page_count; |
| 261 | |
| 262 | uint32_t reserved; |
| 263 | }; |
| 264 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 265 | struct spci_memory_region_attributes { |
| 266 | /** The ID of the VM to which the memory is being given or shared. */ |
| 267 | spci_vm_id_t receiver; |
| 268 | /** |
| 269 | * The attributes with which the memory region should be mapped in the |
| 270 | * receiver's page table. |
| 271 | */ |
| 272 | uint16_t memory_attributes; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 273 | }; |
| 274 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 275 | struct spci_memory_region { |
| 276 | /** |
| 277 | * An implementation defined value associated with the receiver and the |
| 278 | * memory region. |
| 279 | */ |
| 280 | uint32_t tag; |
| 281 | /** Flags to control behaviour of the transaction. */ |
| 282 | uint32_t flags; |
| 283 | /** |
| 284 | * The total number of 4 kiB pages included in this memory region. This |
| 285 | * must be equal to the sum of page counts specified in each |
| 286 | * `spci_memory_region_constituent`. |
| 287 | */ |
| 288 | uint32_t page_count; |
| 289 | /** |
| 290 | * The number of constituents (`spci_memory_region_constituent`) |
| 291 | * included in this memory region. |
| 292 | */ |
| 293 | uint32_t constituent_count; |
| 294 | /** |
| 295 | * The offset in bytes from the base address of this |
| 296 | * `spci_memory_region` to the start of the first |
| 297 | * `spci_memory_region_constituent`. |
| 298 | */ |
| 299 | uint32_t constituent_offset; |
| 300 | /** |
| 301 | * The number of `spci_memory_region_attributes` entries included in |
| 302 | * this memory region. |
| 303 | */ |
| 304 | uint32_t attribute_count; |
| 305 | /** |
| 306 | * An array of `attribute_count` memory region attribute descriptors. |
| 307 | * Each one specifies an endpoint and the attributes with which this |
| 308 | * memory region should be mapped in that endpoint's page table. |
| 309 | */ |
| 310 | struct spci_memory_region_attributes attributes[]; |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 311 | }; |
| 312 | |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 313 | /* TODO: Move all the functions below this line to a support library. */ |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 314 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 315 | /** |
| 316 | * Gets the constituent array for an `spci_memory_region`. |
| 317 | */ |
| 318 | static inline struct spci_memory_region_constituent * |
| 319 | spci_memory_region_get_constituents(struct spci_memory_region *memory_region) |
Jose Marinho | 4e4e4d5 | 2019-02-22 16:23:51 +0000 | [diff] [blame] | 320 | { |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 321 | return (struct spci_memory_region_constituent |
| 322 | *)((uint8_t *)memory_region + |
| 323 | memory_region->constituent_offset); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Helper method to fill in the information about the architected message. |
| 328 | */ |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 329 | static inline void spci_architected_message_init(void *message, |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 330 | enum spci_memory_share type) |
| 331 | { |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 332 | /* Fill the architected header. */ |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 333 | struct spci_architected_message_header *architected_header = |
| 334 | (struct spci_architected_message_header *)message; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 335 | architected_header->type = type; |
| 336 | architected_header->reserved[0] = 0; |
| 337 | architected_header->reserved[1] = 0; |
| 338 | architected_header->reserved[2] = 0; |
| 339 | } |
| 340 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 341 | /** Gets the spci_memory_region within an architected message. */ |
| 342 | static inline struct spci_memory_region *spci_get_memory_region(void *message) |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 343 | { |
| 344 | struct spci_architected_message_header *architected_header = |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 345 | (struct spci_architected_message_header *)message; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 346 | return (struct spci_memory_region *)architected_header->payload; |
| 347 | } |
| 348 | |
| 349 | /** |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 350 | * Initialises the given `spci_memory_region` and copies the constituent |
| 351 | * information to it. Returns the length in bytes occupied by the data copied to |
| 352 | * `memory_region` (attributes, constituents and memory region header size). |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 353 | */ |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 354 | static inline uint32_t spci_memory_region_init( |
| 355 | struct spci_memory_region *memory_region, spci_vm_id_t receiver, |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 356 | const struct spci_memory_region_constituent constituents[], |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 357 | uint32_t constituent_count, uint32_t tag, |
| 358 | enum spci_memory_access access, enum spci_memory_type type, |
| 359 | enum spci_memory_cacheability cacheability, |
| 360 | enum spci_memory_shareability shareability) |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 361 | { |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 362 | uint32_t constituents_length = |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 363 | constituent_count * |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 364 | sizeof(struct spci_memory_region_constituent); |
| 365 | uint32_t index; |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 366 | struct spci_memory_region_constituent *region_constituents; |
| 367 | uint16_t attributes = 0; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 368 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 369 | /* Set memory region's page attributes. */ |
| 370 | spci_set_memory_access_attr(&attributes, access); |
| 371 | spci_set_memory_type_attr(&attributes, type); |
| 372 | spci_set_memory_cacheability_attr(&attributes, cacheability); |
| 373 | spci_set_memory_shareability_attr(&attributes, shareability); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 374 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 375 | memory_region->tag = tag; |
| 376 | memory_region->flags = 0; |
| 377 | memory_region->page_count = 0; |
| 378 | memory_region->constituent_count = constituent_count; |
| 379 | memory_region->attribute_count = 1; |
| 380 | memory_region->attributes[0].receiver = receiver; |
| 381 | memory_region->attributes[0].memory_attributes = attributes; |
| 382 | |
Andrew Walbran | a422445 | 2019-11-07 13:07:50 +0000 | [diff] [blame] | 383 | /* |
| 384 | * Constituent offset must be aligned to a 64-bit boundary so that |
| 385 | * 64-bit addresses can be copied without alignment faults. |
| 386 | */ |
| 387 | memory_region->constituent_offset = align_up( |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 388 | sizeof(struct spci_memory_region) + |
Andrew Walbran | a422445 | 2019-11-07 13:07:50 +0000 | [diff] [blame] | 389 | memory_region->attribute_count * |
| 390 | sizeof(struct spci_memory_region_attributes), |
| 391 | 8); |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 392 | region_constituents = |
| 393 | spci_memory_region_get_constituents(memory_region); |
| 394 | |
| 395 | for (index = 0; index < constituent_count; index++) { |
| 396 | region_constituents[index] = constituents[index]; |
| 397 | region_constituents[index].reserved = 0; |
| 398 | memory_region->page_count += constituents[index].page_count; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /* |
| 402 | * TODO: Add assert ensuring that the specified message |
| 403 | * length is not greater than SPCI_MSG_PAYLOAD_MAX. |
| 404 | */ |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 405 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 406 | return memory_region->constituent_offset + constituents_length; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 407 | } |
| 408 | |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 409 | /** |
| 410 | * Constructs an 'architected message' for SPCI memory sharing of the given |
| 411 | * type. |
| 412 | */ |
| 413 | static inline uint32_t spci_memory_init( |
| 414 | void *message, enum spci_memory_share share_type, spci_vm_id_t receiver, |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 415 | struct spci_memory_region_constituent *region_constituents, |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 416 | uint32_t constituent_count, uint32_t tag, |
| 417 | enum spci_memory_access access, enum spci_memory_type type, |
| 418 | enum spci_memory_cacheability cacheability, |
| 419 | enum spci_memory_shareability shareability) |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 420 | { |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 421 | uint32_t message_length = |
| 422 | sizeof(struct spci_architected_message_header); |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 423 | struct spci_memory_region *memory_region = |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 424 | spci_get_memory_region(message); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 425 | |
| 426 | /* Fill in the details on the common message header. */ |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 427 | spci_architected_message_init(message, share_type); |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 428 | |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 429 | /* Fill in memory region. */ |
| 430 | message_length += spci_memory_region_init( |
| 431 | memory_region, receiver, region_constituents, constituent_count, |
| 432 | tag, access, type, cacheability, shareability); |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 433 | return message_length; |
Jose Marinho | 75509b4 | 2019-04-09 09:34:59 +0100 | [diff] [blame] | 434 | } |
Jose Marinho | 56c2573 | 2019-05-20 09:48:53 +0100 | [diff] [blame] | 435 | |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 436 | /** Constructs an SPCI donate memory region message. */ |
| 437 | static inline uint32_t spci_memory_donate_init( |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 438 | void *message, spci_vm_id_t receiver, |
Jose Marinho | 56c2573 | 2019-05-20 09:48:53 +0100 | [diff] [blame] | 439 | struct spci_memory_region_constituent *region_constituents, |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 440 | uint32_t constituent_count, uint32_t tag, |
| 441 | enum spci_memory_access access, enum spci_memory_type type, |
| 442 | enum spci_memory_cacheability cacheability, |
| 443 | enum spci_memory_shareability shareability) |
Jose Marinho | 56c2573 | 2019-05-20 09:48:53 +0100 | [diff] [blame] | 444 | { |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 445 | return spci_memory_init(message, SPCI_MEMORY_DONATE, receiver, |
| 446 | region_constituents, constituent_count, tag, |
| 447 | access, type, cacheability, shareability); |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /** |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 451 | * Constructs an SPCI memory region lend message. |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 452 | */ |
Andrew Walbran | 70bc862 | 2019-10-07 14:15:58 +0100 | [diff] [blame] | 453 | static inline uint32_t spci_memory_lend_init( |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 454 | void *message, spci_vm_id_t receiver, |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 455 | struct spci_memory_region_constituent *region_constituents, |
Andrew Walbran | f597218 | 2019-10-15 15:41:26 +0100 | [diff] [blame] | 456 | uint32_t constituent_count, uint32_t tag, |
| 457 | enum spci_memory_access access, enum spci_memory_type type, |
| 458 | enum spci_memory_cacheability cacheability, |
| 459 | enum spci_memory_shareability shareability) |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 460 | { |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 461 | return spci_memory_init(message, SPCI_MEMORY_LEND, receiver, |
| 462 | region_constituents, constituent_count, tag, |
| 463 | access, type, cacheability, shareability); |
| 464 | } |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 465 | |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 466 | /** |
| 467 | * Constructs an SPCI memory region share message. |
| 468 | */ |
| 469 | static inline uint32_t spci_memory_share_init( |
| 470 | void *message, spci_vm_id_t receiver, |
| 471 | struct spci_memory_region_constituent *region_constituents, |
| 472 | uint32_t constituent_count, uint32_t tag, |
| 473 | enum spci_memory_access access, enum spci_memory_type type, |
| 474 | enum spci_memory_cacheability cacheability, |
| 475 | enum spci_memory_shareability shareability) |
| 476 | { |
| 477 | return spci_memory_init(message, SPCI_MEMORY_SHARE, receiver, |
| 478 | region_constituents, constituent_count, tag, |
| 479 | access, type, cacheability, shareability); |
| 480 | } |
Jose Marinho | 713f13a | 2019-05-21 11:54:16 +0100 | [diff] [blame] | 481 | |
Andrew Walbran | 648fc3e | 2019-10-22 16:23:05 +0100 | [diff] [blame] | 482 | /** |
| 483 | * Constructs an SPCI memory region relinquish message. |
| 484 | * A set of memory regions can be given back to the owner. |
| 485 | */ |
| 486 | static inline uint32_t spci_memory_relinquish_init( |
| 487 | void *message, spci_vm_id_t receiver, |
| 488 | struct spci_memory_region_constituent *region_constituents, |
| 489 | uint32_t constituent_count, uint32_t tag) |
| 490 | { |
| 491 | return spci_memory_init(message, SPCI_MEMORY_RELINQUISH, receiver, |
| 492 | region_constituents, constituent_count, tag, |
| 493 | SPCI_MEMORY_RW_X, SPCI_MEMORY_DEVICE_MEM, |
| 494 | SPCI_MEMORY_DEV_NGNRNE, |
| 495 | SPCI_MEMORY_SHARE_NON_SHAREABLE); |
Jose Marinho | 56c2573 | 2019-05-20 09:48:53 +0100 | [diff] [blame] | 496 | } |