blob: c42b2430313c6b4712705ad97579b1fdce9c3154 [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
33#define SPCI_RX_RELEASE_32 0x84000064
34#define SPCI_RXTX_MAP_32 0x84000065
35#define SPCI_RXTX_UNMAP_32 0x84000066
36#define SPCI_PARTITION_INFO_GET_32 0x84000067
37#define SPCI_ID_GET_32 0x84000068
38#define SPCI_MSG_WAIT_32 0x84000069
39#define SPCI_MSG_POLL_32 0x8400006A
40#define SPCI_YIELD_32 0x8400006B
41#define SPCI_MSG_SEND_32 0x8400006C
42#define SPCI_RUN_32 0x8400006D
43#define SPCI_MSG_SEND_DIRECT_REQ_32 0x8400006E
44#define SPCI_MSG_SEND_DIRECT_RESP_32 0x8400006F
Jose Marinho4e4e4d52019-02-22 16:23:51 +000045
Andrew Walbran0de4f162019-09-03 16:44:20 +010046/* SPCI error codes. */
Jose Marinho4e4e4d52019-02-22 16:23:51 +000047#define SPCI_NOT_SUPPORTED INT32_C(-1)
48#define SPCI_INVALID_PARAMETERS INT32_C(-2)
49#define SPCI_NO_MEMORY INT32_C(-3)
50#define SPCI_BUSY INT32_C(-4)
51#define SPCI_INTERRUPTED INT32_C(-5)
52#define SPCI_DENIED INT32_C(-6)
Jose Marinho4e4e4d52019-02-22 16:23:51 +000053#define SPCI_RETRY INT32_C(-7)
54
Jose Marinho75509b42019-04-09 09:34:59 +010055/* Architected memory sharing message IDs. */
56enum spci_memory_share {
Jose Marinho713f13a2019-05-21 11:54:16 +010057 SPCI_MEMORY_LEND = 0x0,
Jose Marinho56c25732019-05-20 09:48:53 +010058 SPCI_MEMORY_RELINQUISH = 0x1,
Jose Marinho75509b42019-04-09 09:34:59 +010059 SPCI_MEMORY_DONATE = 0x2,
60};
61
Jose Marinho4e4e4d52019-02-22 16:23:51 +000062/* SPCI function specific constants. */
Andrew Walbran70bc8622019-10-07 14:15:58 +010063#define SPCI_MSG_RECV_BLOCK 0x1
Andrew Scull1262ac22019-04-05 12:44:26 +010064#define SPCI_MSG_RECV_BLOCK_MASK 0x1
Jose Marinho4e4e4d52019-02-22 16:23:51 +000065
66#define SPCI_MSG_SEND_NOTIFY 0x1
Andrew Walbran70bc8622019-10-07 14:15:58 +010067#define SPCI_MSG_SEND_NOTIFY_MASK 0x1
68#define SPCI_MSG_SEND_LEGACY_MEMORY 0x2
69#define SPCI_MSG_SEND_LEGACY_MEMORY_MASK 0x2
Andrew Scull1262ac22019-04-05 12:44:26 +010070
71/* The maximum length possible for a single message. */
Andrew Walbran70bc8622019-10-07 14:15:58 +010072#define SPCI_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
Jose Marinho713f13a2019-05-21 11:54:16 +010073
74enum spci_lend_access {
75 SPCI_LEND_RO_NX,
76 SPCI_LEND_RO_X,
77 SPCI_LEND_RW_NX,
78 SPCI_LEND_RW_X,
79};
80
81enum spci_lend_type {
82 SPCI_LEND_NORMAL_MEM,
83 SPCI_LEND_DEV_NGNRNE,
84 SPCI_LEND_DEV_NGNRE,
85 SPCI_LEND_DEV_NGRE,
86 SPCI_LEND_DEV_GRE,
87};
88
89enum spci_lend_cacheability {
90 SPCI_LEND_CACHE_NON_CACHEABLE,
91 SPCI_LEND_CACHE_WRITE_THROUGH,
92 SPCI_LEND_CACHE_WRITE_BACK,
93};
94
95enum spci_lend_shareability {
96 SPCI_LEND_SHARE_NON_SHAREABLE,
97 SPCI_LEND_RESERVED,
98 SPCI_LEND_OUTER_SHAREABLE,
99 SPCI_LEND_INNER_SHAREABLE,
100};
101
102#define SPCI_LEND_ACCESS_OFFSET (0x7U)
103#define SPCI_LEND_ACCESS_MASK ((0x3U) << SPCI_LEND_ACCESS_OFFSET)
104
105#define SPCI_LEND_TYPE_OFFSET (0x4U)
106#define SPCI_LEND_TYPE_MASK ((0x7U) << SPCI_LEND_TYPE_OFFSET)
107
108#define SPCI_LEND_CACHEABILITY_OFFSET (0x2U)
109#define SPCI_LEND_CACHEABILITY_MASK ((0x3U) <<\
110 SPCI_LEND_CACHEABILITY_OFFSET)
111
112#define SPCI_LEND_SHAREABILITY_OFFSET (0x0U)
113#define SPCI_LEND_SHAREABILITY_MASK ((0x3U) <<\
114 SPCI_LEND_SHAREABILITY_OFFSET)
115
116#define LEND_ATTR_FUNCTION_SET(name, offset, mask) \
117static inline void spci_set_lend_##name##_attr(uint16_t *lend_attr,\
118 const enum spci_lend_##name perm)\
119{\
120 *lend_attr = (*lend_attr & ~(mask)) | ((perm << offset) & mask);\
121}
122
123#define LEND_ATTR_FUNCTION_GET(name, offset, mask) \
124static inline enum spci_lend_##name spci_get_lend_##name##_attr(\
125 uint16_t lend_attr)\
126{\
127 return (enum spci_lend_##name)((lend_attr & mask) >> offset);\
128}
129
130LEND_ATTR_FUNCTION_SET(access, SPCI_LEND_ACCESS_OFFSET, SPCI_LEND_ACCESS_MASK)
131LEND_ATTR_FUNCTION_GET(access, SPCI_LEND_ACCESS_OFFSET, SPCI_LEND_ACCESS_MASK)
132
133LEND_ATTR_FUNCTION_SET(type, SPCI_LEND_TYPE_OFFSET, SPCI_LEND_TYPE_MASK)
134LEND_ATTR_FUNCTION_GET(type, SPCI_LEND_TYPE_OFFSET, SPCI_LEND_TYPE_MASK)
135
136LEND_ATTR_FUNCTION_SET(cacheability, SPCI_LEND_CACHEABILITY_OFFSET,
137 SPCI_LEND_CACHEABILITY_MASK)
138
139LEND_ATTR_FUNCTION_GET(cacheability, SPCI_LEND_CACHEABILITY_OFFSET,
140 SPCI_LEND_CACHEABILITY_MASK)
141
142LEND_ATTR_FUNCTION_SET(shareability, SPCI_LEND_SHAREABILITY_OFFSET,
143 SPCI_LEND_SHAREABILITY_MASK)
144
145LEND_ATTR_FUNCTION_GET(shareability, SPCI_LEND_SHAREABILITY_OFFSET,
146 SPCI_LEND_SHAREABILITY_MASK)
147
148enum spci_lend_flags {
149 SPCI_LEND_KEEP_MAPPED = 0x0,
150 SPCI_LEND_UNMAP = 0x1
151};
152
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000153/* clang-format on */
154
Fuad Tabba494376e2019-08-05 12:35:10 +0100155/** The ID of a VM. These are assigned sequentially starting with an offset. */
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000156typedef uint16_t spci_vm_id_t;
Jose Marinho75509b42019-04-09 09:34:59 +0100157typedef uint32_t spci_memory_handle_t;
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000158
Andrew Walbran52d99672019-06-25 15:51:11 +0100159/**
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 */
163typedef spci_vm_id_t spci_vm_count_t;
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100164
165/** The index of a vCPU within a particular VM. */
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100166typedef uint16_t spci_vcpu_index_t;
Andrew Walbran52d99672019-06-25 15:51:11 +0100167
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100168/**
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 */
172typedef spci_vcpu_index_t spci_vcpu_count_t;
173
Andrew Walbranf18e63c2019-10-07 15:13:59 +0100174/** Parameter and return type of SPCI functions. */
Andrew Walbran7f920af2019-09-03 17:09:30 +0100175struct 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 Marinho75509b42019-04-09 09:34:59 +0100185
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100186static inline spci_vm_id_t spci_msg_send_sender(struct spci_value args)
187{
188 return (args.arg1 >> 16) & 0xffff;
189}
190
191static inline spci_vm_id_t spci_msg_send_receiver(struct spci_value args)
192{
193 return args.arg1 & 0xffff;
194}
195
196static inline uint32_t spci_msg_send_size(struct spci_value args)
197{
198 return args.arg3;
199}
200
Andrew Walbran70bc8622019-10-07 14:15:58 +0100201static inline uint32_t spci_msg_send_attributes(struct spci_value args)
202{
203 return args.arg4;
204}
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000205
Jose Marinho75509b42019-04-09 09:34:59 +0100206struct 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
218struct spci_memory_region_constituent {
219 uint64_t address;
220 uint32_t page_count;
221
222 uint32_t reserved;
223};
224
225struct spci_memory_region {
226 spci_memory_handle_t handle;
227 uint32_t count;
228
229 struct spci_memory_region_constituent constituents[];
230};
231
Jose Marinho713f13a2019-05-21 11:54:16 +0100232struct spci_memory_lend {
233 uint16_t flags;
234 uint16_t borrower_attributes;
235
236 uint32_t reserved;
237
238 uint8_t payload[];
239};
240
Jose Marinho75509b42019-04-09 09:34:59 +0100241/* TODO: Move all the functions below this line to a support library. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100242
243static inline struct spci_memory_lend *spci_get_lend_descriptor(void *message)
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000244{
Andrew Walbran70bc8622019-10-07 14:15:58 +0100245 return (struct spci_memory_lend
246 *)((struct spci_architected_message_header *)message)
247 ->payload;
Jose Marinho75509b42019-04-09 09:34:59 +0100248}
249
250/**
251 * Helper method to fill in the information about the architected message.
252 */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100253static inline void spci_architected_message_init(void *message,
Jose Marinho75509b42019-04-09 09:34:59 +0100254 enum spci_memory_share type)
255{
Jose Marinho75509b42019-04-09 09:34:59 +0100256 /* Fill the architected header. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100257 struct spci_architected_message_header *architected_header =
258 (struct spci_architected_message_header *)message;
Jose Marinho75509b42019-04-09 09:34:59 +0100259 architected_header->type = type;
260 architected_header->reserved[0] = 0;
261 architected_header->reserved[1] = 0;
262 architected_header->reserved[2] = 0;
263}
264
265/** Obtain a pointer to the start of the memory region in the donate message. */
266static inline struct spci_memory_region *spci_get_donated_memory_region(
Andrew Walbran70bc8622019-10-07 14:15:58 +0100267 void *message)
Jose Marinho75509b42019-04-09 09:34:59 +0100268{
269 struct spci_architected_message_header *architected_header =
Andrew Walbran70bc8622019-10-07 14:15:58 +0100270 (struct spci_architected_message_header *)message;
Jose Marinho75509b42019-04-09 09:34:59 +0100271 return (struct spci_memory_region *)architected_header->payload;
272}
273
274/**
Jose Marinho713f13a2019-05-21 11:54:16 +0100275 * Helper function that copies the memory constituents and the handle
276 * information onto the address pointed to by memory_region.
277 * The function returns the length in bytes occupied by the data copied to
278 * memory_region (constituents and memory region header size).
Jose Marinho75509b42019-04-09 09:34:59 +0100279 */
Jose Marinho713f13a2019-05-21 11:54:16 +0100280static inline uint32_t spci_memory_region_add(
281 struct spci_memory_region *memory_region, spci_memory_handle_t handle,
Jose Marinho75509b42019-04-09 09:34:59 +0100282 const struct spci_memory_region_constituent constituents[],
283 uint32_t num_constituents)
284{
Jose Marinho75509b42019-04-09 09:34:59 +0100285 uint32_t constituents_length =
286 num_constituents *
287 sizeof(struct spci_memory_region_constituent);
288 uint32_t index;
289
290 memory_region->handle = handle;
291 memory_region->count = num_constituents;
292
293 for (index = 0; index < num_constituents; index++) {
294 memory_region->constituents[index] = constituents[index];
295 memory_region->constituents[index].reserved = 0;
296 }
297
298 /*
299 * TODO: Add assert ensuring that the specified message
300 * length is not greater than SPCI_MSG_PAYLOAD_MAX.
301 */
Jose Marinho713f13a2019-05-21 11:54:16 +0100302
303 return sizeof(struct spci_memory_region) + constituents_length;
Jose Marinho75509b42019-04-09 09:34:59 +0100304}
305
306/** Construct the SPCI donate memory region message. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100307static inline uint32_t spci_memory_donate_init(
308 void *message,
Jose Marinho75509b42019-04-09 09:34:59 +0100309 struct spci_memory_region_constituent *region_constituents,
310 uint32_t num_elements, uint32_t handle)
311{
Andrew Walbran70bc8622019-10-07 14:15:58 +0100312 uint32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100313 struct spci_memory_region *memory_region =
314 spci_get_donated_memory_region(message);
Jose Marinho75509b42019-04-09 09:34:59 +0100315
316 message_length = sizeof(struct spci_architected_message_header);
317
318 /* Fill in the details on the common message header. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100319 spci_architected_message_init(message, SPCI_MEMORY_DONATE);
Jose Marinho75509b42019-04-09 09:34:59 +0100320
321 /* Create single memory region. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100322 message_length += spci_memory_region_add(
Jose Marinho713f13a2019-05-21 11:54:16 +0100323 memory_region, handle, region_constituents, num_elements);
Andrew Walbran70bc8622019-10-07 14:15:58 +0100324 return message_length;
Jose Marinho75509b42019-04-09 09:34:59 +0100325}
Jose Marinho56c25732019-05-20 09:48:53 +0100326
327/**
328 * Construct the SPCI memory region relinquish message.
329 * A set of memory regions can be given back to the owner.
330 */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100331static inline uint32_t spci_memory_relinquish_init(
332 void *message,
Jose Marinho56c25732019-05-20 09:48:53 +0100333 struct spci_memory_region_constituent *region_constituents,
334 uint64_t num_elements, uint32_t handle)
335{
Andrew Walbran70bc8622019-10-07 14:15:58 +0100336 uint32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100337 struct spci_memory_region *memory_region =
338 spci_get_donated_memory_region(message);
Jose Marinho56c25732019-05-20 09:48:53 +0100339
340 message_length = sizeof(struct spci_architected_message_header);
341
342 /* Fill in the details on the common message header. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100343 spci_architected_message_init(message, SPCI_MEMORY_RELINQUISH);
Jose Marinho56c25732019-05-20 09:48:53 +0100344
345 /* Create single memory region. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100346 message_length += spci_memory_region_add(
Jose Marinho713f13a2019-05-21 11:54:16 +0100347 memory_region, handle, region_constituents, num_elements);
Andrew Walbran70bc8622019-10-07 14:15:58 +0100348 return message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100349}
350
351/**
352 * Construct the SPCI memory region lend message.
353 */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100354static inline uint32_t spci_memory_lend_init(
355 void *message,
Jose Marinho713f13a2019-05-21 11:54:16 +0100356 struct spci_memory_region_constituent *region_constituents,
357 uint64_t num_elements, uint32_t handle, enum spci_lend_access access,
358 enum spci_lend_type type, enum spci_lend_cacheability cacheability,
359 enum spci_lend_shareability shareability)
360{
Andrew Walbran70bc8622019-10-07 14:15:58 +0100361 uint32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100362 struct spci_memory_region *memory_region;
363
364 const struct spci_memory_lend lend_init = {0};
365
366 struct spci_memory_lend *lend_descriptor =
367 spci_get_lend_descriptor(message);
368 memory_region = (struct spci_memory_region *)lend_descriptor->payload;
369
370 /* Initilise all struct elements to zero. */
371 *lend_descriptor = lend_init;
372
373 message_length = sizeof(struct spci_architected_message_header) +
374 sizeof(struct spci_memory_lend);
375
376 /* Fill in the details on the common message header. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100377 spci_architected_message_init(message, SPCI_MEMORY_LEND);
Jose Marinho713f13a2019-05-21 11:54:16 +0100378
379 lend_descriptor->flags = SPCI_LEND_KEEP_MAPPED;
380
381 /* Set memory region's page attributes. */
382 spci_set_lend_access_attr(&lend_descriptor->borrower_attributes,
383 access);
384 spci_set_lend_type_attr(&lend_descriptor->borrower_attributes, type);
385 spci_set_lend_cacheability_attr(&lend_descriptor->borrower_attributes,
386 cacheability);
387 spci_set_lend_shareability_attr(&lend_descriptor->borrower_attributes,
388 shareability);
389
390 /* Create single memory region. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100391 message_length += spci_memory_region_add(
Jose Marinho713f13a2019-05-21 11:54:16 +0100392 memory_region, handle, region_constituents, num_elements);
Andrew Walbran70bc8622019-10-07 14:15:58 +0100393 return message_length;
Jose Marinho56c25732019-05-20 09:48:53 +0100394}