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