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