blob: c7638ce8b895611e5f65556938e341be4d5e8e7b [file] [log] [blame]
Jose Marinho4e4e4d52019-02-22 16:23:51 +00001/*
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 Marinho4e4e4d52019-02-22 16:23:51 +000017#pragma once
18
Jose Marinho75509b42019-04-09 09:34:59 +010019#include "hf/types.h"
20
Jose Marinho4e4e4d52019-02-22 16:23:51 +000021/* 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 Walbran0de4f162019-09-03 16:44:20 +010029#define SPCI_ERROR_32 0x84000060
30#define SPCI_SUCCESS_32 0x84000061
31#define SPCI_INTERRUPT_32 0x84000062
32#define SPCI_VERSION_32 0x84000063
Jose Marinho021528f2019-10-08 17:21:20 +010033#define SPCI_FEATURES_32 0x84000064
34#define SPCI_RX_RELEASE_32 0x84000065
35#define SPCI_RXTX_MAP_32 0x84000066
Andrew Walbran3abf29e2020-01-21 17:34:15 +000036#define SPCI_RXTX_MAP_64 0xC4000066
Jose Marinho021528f2019-10-08 17:21:20 +010037#define SPCI_RXTX_UNMAP_32 0x84000067
38#define SPCI_PARTITION_INFO_GET_32 0x84000068
39#define SPCI_ID_GET_32 0x84000069
Andrew Walbran0de4f162019-09-03 16:44:20 +010040#define SPCI_MSG_POLL_32 0x8400006A
Jose Marinho021528f2019-10-08 17:21:20 +010041#define SPCI_MSG_WAIT_32 0x8400006B
42#define SPCI_YIELD_32 0x8400006C
Andrew Walbran0de4f162019-09-03 16:44:20 +010043#define SPCI_RUN_32 0x8400006D
Jose Marinho021528f2019-10-08 17:21:20 +010044#define SPCI_MSG_SEND_32 0x8400006E
45#define SPCI_MSG_SEND_DIRECT_REQ_32 0x8400006F
46#define SPCI_MSG_SEND_DIRECT_RESP_32 0x84000070
Andrew Walbrane908c4a2019-12-02 17:13:47 +000047#define SPCI_MEM_DONATE_32 0x84000071
48#define SPCI_MEM_LEND_32 0x84000072
49#define SPCI_MEM_SHARE_32 0x84000073
Jose Marinho4e4e4d52019-02-22 16:23:51 +000050
Andrew Walbran0de4f162019-09-03 16:44:20 +010051/* SPCI error codes. */
Jose Marinho4e4e4d52019-02-22 16:23:51 +000052#define SPCI_NOT_SUPPORTED INT32_C(-1)
53#define SPCI_INVALID_PARAMETERS INT32_C(-2)
54#define SPCI_NO_MEMORY INT32_C(-3)
55#define SPCI_BUSY INT32_C(-4)
56#define SPCI_INTERRUPTED INT32_C(-5)
57#define SPCI_DENIED INT32_C(-6)
Jose Marinho4e4e4d52019-02-22 16:23:51 +000058#define SPCI_RETRY INT32_C(-7)
Andrew Walbranf0c314d2019-10-02 14:24:26 +010059#define SPCI_ABORTED INT32_C(-8)
Jose Marinho4e4e4d52019-02-22 16:23:51 +000060
61/* SPCI function specific constants. */
Andrew Walbran70bc8622019-10-07 14:15:58 +010062#define SPCI_MSG_RECV_BLOCK 0x1
Andrew Scull1262ac22019-04-05 12:44:26 +010063#define SPCI_MSG_RECV_BLOCK_MASK 0x1
Jose Marinho4e4e4d52019-02-22 16:23:51 +000064
65#define SPCI_MSG_SEND_NOTIFY 0x1
Andrew Walbran70bc8622019-10-07 14:15:58 +010066#define SPCI_MSG_SEND_NOTIFY_MASK 0x1
Andrew Scull1262ac22019-04-05 12:44:26 +010067
Andrew Walbranf0c314d2019-10-02 14:24:26 +010068#define SPCI_SLEEP_INDEFINITE 0
69
Andrew Walbranbfffb0f2019-11-05 14:02:34 +000070/**
71 * For use where the SPCI specification refers explicitly to '4K pages'. Not to
72 * be confused with PAGE_SIZE, which is the translation granule Hafnium is
73 * configured to use.
74 */
75#define SPCI_PAGE_SIZE 4096
76
Andrew Scull1262ac22019-04-05 12:44:26 +010077/* The maximum length possible for a single message. */
Andrew Walbran70bc8622019-10-07 14:15:58 +010078#define SPCI_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
Jose Marinho713f13a2019-05-21 11:54:16 +010079
Andrew Walbranf5972182019-10-15 15:41:26 +010080enum spci_memory_access {
81 SPCI_MEMORY_RO_NX,
82 SPCI_MEMORY_RO_X,
83 SPCI_MEMORY_RW_NX,
84 SPCI_MEMORY_RW_X,
Jose Marinho713f13a2019-05-21 11:54:16 +010085};
86
Andrew Walbranf5972182019-10-15 15:41:26 +010087enum spci_memory_type {
88 SPCI_MEMORY_DEVICE_MEM,
89 SPCI_MEMORY_NORMAL_MEM,
Jose Marinho713f13a2019-05-21 11:54:16 +010090};
91
Andrew Walbranf5972182019-10-15 15:41:26 +010092enum spci_memory_cacheability {
93 SPCI_MEMORY_CACHE_RESERVED = 0x0,
94 SPCI_MEMORY_CACHE_NON_CACHEABLE = 0x1,
95 SPCI_MEMORY_CACHE_WRITE_THROUGH = 0x2,
96 SPCI_MEMORY_CACHE_WRITE_BACK = 0x4,
97 SPCI_MEMORY_DEV_NGNRNE = 0x0,
98 SPCI_MEMORY_DEV_NGNRE = 0x1,
99 SPCI_MEMORY_DEV_NGRE = 0x2,
100 SPCI_MEMORY_DEV_GRE = 0x3,
Jose Marinho713f13a2019-05-21 11:54:16 +0100101};
102
Andrew Walbranf5972182019-10-15 15:41:26 +0100103enum spci_memory_shareability {
104 SPCI_MEMORY_SHARE_NON_SHAREABLE,
105 SPCI_MEMORY_RESERVED,
106 SPCI_MEMORY_OUTER_SHAREABLE,
107 SPCI_MEMORY_INNER_SHAREABLE,
Jose Marinho713f13a2019-05-21 11:54:16 +0100108};
109
Andrew Walbranf5972182019-10-15 15:41:26 +0100110#define SPCI_MEMORY_ACCESS_OFFSET (0x5U)
111#define SPCI_MEMORY_ACCESS_MASK ((0x3U) << SPCI_MEMORY_ACCESS_OFFSET)
Jose Marinho713f13a2019-05-21 11:54:16 +0100112
Andrew Walbranf5972182019-10-15 15:41:26 +0100113#define SPCI_MEMORY_TYPE_OFFSET (0x4U)
114#define SPCI_MEMORY_TYPE_MASK ((0x1U) << SPCI_MEMORY_TYPE_OFFSET)
Jose Marinho713f13a2019-05-21 11:54:16 +0100115
Andrew Walbranf5972182019-10-15 15:41:26 +0100116#define SPCI_MEMORY_CACHEABILITY_OFFSET (0x2U)
117#define SPCI_MEMORY_CACHEABILITY_MASK ((0x3U) <<\
118 SPCI_MEMORY_CACHEABILITY_OFFSET)
Jose Marinho713f13a2019-05-21 11:54:16 +0100119
Andrew Walbranf5972182019-10-15 15:41:26 +0100120#define SPCI_MEMORY_SHAREABILITY_OFFSET (0x0U)
121#define SPCI_MEMORY_SHAREABILITY_MASK ((0x3U) <<\
122 SPCI_MEMORY_SHAREABILITY_OFFSET)
Jose Marinho713f13a2019-05-21 11:54:16 +0100123
124#define LEND_ATTR_FUNCTION_SET(name, offset, mask) \
Andrew Walbranf5972182019-10-15 15:41:26 +0100125static inline void spci_set_memory_##name##_attr(uint16_t *attr,\
126 const enum spci_memory_##name perm)\
Jose Marinho713f13a2019-05-21 11:54:16 +0100127{\
Andrew Walbranf5972182019-10-15 15:41:26 +0100128 *attr = (*attr & ~(mask)) | ((perm << offset) & mask);\
Jose Marinho713f13a2019-05-21 11:54:16 +0100129}
130
131#define LEND_ATTR_FUNCTION_GET(name, offset, mask) \
Andrew Walbranf5972182019-10-15 15:41:26 +0100132static inline enum spci_memory_##name spci_get_memory_##name##_attr(\
133 uint16_t attr)\
Jose Marinho713f13a2019-05-21 11:54:16 +0100134{\
Andrew Walbranf5972182019-10-15 15:41:26 +0100135 return (enum spci_memory_##name)((attr & mask) >> offset);\
Jose Marinho713f13a2019-05-21 11:54:16 +0100136}
137
Andrew Walbranf5972182019-10-15 15:41:26 +0100138LEND_ATTR_FUNCTION_SET(access, SPCI_MEMORY_ACCESS_OFFSET,
139 SPCI_MEMORY_ACCESS_MASK)
140LEND_ATTR_FUNCTION_GET(access, SPCI_MEMORY_ACCESS_OFFSET,
141 SPCI_MEMORY_ACCESS_MASK)
Jose Marinho713f13a2019-05-21 11:54:16 +0100142
Andrew Walbranf5972182019-10-15 15:41:26 +0100143LEND_ATTR_FUNCTION_SET(type, SPCI_MEMORY_TYPE_OFFSET, SPCI_MEMORY_TYPE_MASK)
144LEND_ATTR_FUNCTION_GET(type, SPCI_MEMORY_TYPE_OFFSET, SPCI_MEMORY_TYPE_MASK)
Jose Marinho713f13a2019-05-21 11:54:16 +0100145
Andrew Walbranf5972182019-10-15 15:41:26 +0100146LEND_ATTR_FUNCTION_SET(cacheability, SPCI_MEMORY_CACHEABILITY_OFFSET,
147 SPCI_MEMORY_CACHEABILITY_MASK)
Jose Marinho713f13a2019-05-21 11:54:16 +0100148
Andrew Walbranf5972182019-10-15 15:41:26 +0100149LEND_ATTR_FUNCTION_GET(cacheability, SPCI_MEMORY_CACHEABILITY_OFFSET,
150 SPCI_MEMORY_CACHEABILITY_MASK)
Jose Marinho713f13a2019-05-21 11:54:16 +0100151
Andrew Walbranf5972182019-10-15 15:41:26 +0100152LEND_ATTR_FUNCTION_SET(shareability, SPCI_MEMORY_SHAREABILITY_OFFSET,
153 SPCI_MEMORY_SHAREABILITY_MASK)
Jose Marinho713f13a2019-05-21 11:54:16 +0100154
Andrew Walbranf5972182019-10-15 15:41:26 +0100155LEND_ATTR_FUNCTION_GET(shareability, SPCI_MEMORY_SHAREABILITY_OFFSET,
156 SPCI_MEMORY_SHAREABILITY_MASK)
Jose Marinho713f13a2019-05-21 11:54:16 +0100157
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000158/* clang-format on */
159
Fuad Tabba494376e2019-08-05 12:35:10 +0100160/** The ID of a VM. These are assigned sequentially starting with an offset. */
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000161typedef uint16_t spci_vm_id_t;
Jose Marinho75509b42019-04-09 09:34:59 +0100162typedef uint32_t spci_memory_handle_t;
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000163
Andrew Walbran52d99672019-06-25 15:51:11 +0100164/**
165 * A count of VMs. This has the same range as the VM IDs but we give it a
166 * different name to make the different semantics clear.
167 */
168typedef spci_vm_id_t spci_vm_count_t;
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100169
170/** The index of a vCPU within a particular VM. */
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100171typedef uint16_t spci_vcpu_index_t;
Andrew Walbran52d99672019-06-25 15:51:11 +0100172
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100173/**
174 * A count of vCPUs. This has the same range as the vCPU indices but we give it
175 * a different name to make the different semantics clear.
176 */
177typedef spci_vcpu_index_t spci_vcpu_count_t;
178
Andrew Walbranf18e63c2019-10-07 15:13:59 +0100179/** Parameter and return type of SPCI functions. */
Andrew Walbran7f920af2019-09-03 17:09:30 +0100180struct spci_value {
181 uint64_t func;
182 uint64_t arg1;
183 uint64_t arg2;
184 uint64_t arg3;
185 uint64_t arg4;
186 uint64_t arg5;
187 uint64_t arg6;
188 uint64_t arg7;
189};
Jose Marinho75509b42019-04-09 09:34:59 +0100190
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100191static inline spci_vm_id_t spci_msg_send_sender(struct spci_value args)
192{
193 return (args.arg1 >> 16) & 0xffff;
194}
195
196static inline spci_vm_id_t spci_msg_send_receiver(struct spci_value args)
197{
198 return args.arg1 & 0xffff;
199}
200
201static inline uint32_t spci_msg_send_size(struct spci_value args)
202{
203 return args.arg3;
204}
205
Andrew Walbran70bc8622019-10-07 14:15:58 +0100206static inline uint32_t spci_msg_send_attributes(struct spci_value args)
207{
208 return args.arg4;
209}
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000210
Andrew Walbran4db5f3a2019-11-04 11:42:42 +0000211static inline spci_vm_id_t spci_vm_id(struct spci_value args)
212{
213 return (args.arg1 >> 16) & 0xffff;
214}
215
216static inline spci_vcpu_index_t spci_vcpu_index(struct spci_value args)
217{
218 return args.arg1 & 0xffff;
219}
220
221static inline uint64_t spci_vm_vcpu(spci_vm_id_t vm_id,
222 spci_vcpu_index_t vcpu_index)
223{
224 return ((uint32_t)vm_id << 16) | vcpu_index;
225}
226
Jose Marinho75509b42019-04-09 09:34:59 +0100227struct spci_memory_region_constituent {
Andrew Walbranf5972182019-10-15 15:41:26 +0100228 /**
229 * The base IPA of the constituent memory region, aligned to 4 kiB page
230 * size granularity.
231 */
Jose Marinho75509b42019-04-09 09:34:59 +0100232 uint64_t address;
Andrew Walbranf5972182019-10-15 15:41:26 +0100233 /** The number of 4 kiB pages in the constituent memory region. */
Jose Marinho75509b42019-04-09 09:34:59 +0100234 uint32_t page_count;
235
236 uint32_t reserved;
237};
238
Andrew Walbranf5972182019-10-15 15:41:26 +0100239struct spci_memory_region_attributes {
240 /** The ID of the VM to which the memory is being given or shared. */
241 spci_vm_id_t receiver;
242 /**
243 * The attributes with which the memory region should be mapped in the
244 * receiver's page table.
245 */
246 uint16_t memory_attributes;
Jose Marinho75509b42019-04-09 09:34:59 +0100247};
248
Andrew Walbran28b02102019-11-20 18:03:10 +0000249/** Flags to control the behaviour of a memory sharing transaction. */
250typedef uint32_t spci_memory_region_flags_t;
251
252/**
253 * Clear memory region contents after unmapping it from the sender and before
254 * mapping it for any receiver.
255 */
256#define SPCI_MEMORY_REGION_FLAG_CLEAR 0x1
257
Andrew Walbranf5972182019-10-15 15:41:26 +0100258struct spci_memory_region {
259 /**
260 * An implementation defined value associated with the receiver and the
261 * memory region.
262 */
263 uint32_t tag;
264 /** Flags to control behaviour of the transaction. */
Andrew Walbran28b02102019-11-20 18:03:10 +0000265 spci_memory_region_flags_t flags;
Andrew Walbrane28f4a22019-12-24 15:45:36 +0000266 /** Sender VM ID. */
267 spci_vm_id_t sender;
268 /** Reserved field, must be 0. */
269 uint16_t reserved;
Andrew Walbranf5972182019-10-15 15:41:26 +0100270 /**
271 * The total number of 4 kiB pages included in this memory region. This
272 * must be equal to the sum of page counts specified in each
273 * `spci_memory_region_constituent`.
274 */
275 uint32_t page_count;
276 /**
277 * The number of constituents (`spci_memory_region_constituent`)
278 * included in this memory region.
279 */
280 uint32_t constituent_count;
281 /**
282 * The offset in bytes from the base address of this
283 * `spci_memory_region` to the start of the first
284 * `spci_memory_region_constituent`.
285 */
286 uint32_t constituent_offset;
287 /**
288 * The number of `spci_memory_region_attributes` entries included in
289 * this memory region.
290 */
291 uint32_t attribute_count;
292 /**
293 * An array of `attribute_count` memory region attribute descriptors.
294 * Each one specifies an endpoint and the attributes with which this
295 * memory region should be mapped in that endpoint's page table.
296 */
297 struct spci_memory_region_attributes attributes[];
Jose Marinho713f13a2019-05-21 11:54:16 +0100298};
299
Andrew Walbranf5972182019-10-15 15:41:26 +0100300/**
301 * Gets the constituent array for an `spci_memory_region`.
302 */
303static inline struct spci_memory_region_constituent *
304spci_memory_region_get_constituents(struct spci_memory_region *memory_region)
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000305{
Andrew Walbranf5972182019-10-15 15:41:26 +0100306 return (struct spci_memory_region_constituent
307 *)((uint8_t *)memory_region +
308 memory_region->constituent_offset);
Jose Marinho75509b42019-04-09 09:34:59 +0100309}
310
Andrew Walbran66839252019-11-07 16:01:48 +0000311uint32_t spci_memory_region_init(
Andrew Walbrane28f4a22019-12-24 15:45:36 +0000312 struct spci_memory_region *memory_region, spci_vm_id_t sender,
313 spci_vm_id_t receiver,
Jose Marinho75509b42019-04-09 09:34:59 +0100314 const struct spci_memory_region_constituent constituents[],
Andrew Walbranf5972182019-10-15 15:41:26 +0100315 uint32_t constituent_count, uint32_t tag,
Andrew Walbran28b02102019-11-20 18:03:10 +0000316 spci_memory_region_flags_t flags, enum spci_memory_access access,
317 enum spci_memory_type type, enum spci_memory_cacheability cacheability,
Andrew Walbran66839252019-11-07 16:01:48 +0000318 enum spci_memory_shareability shareability);