blob: 125380b629bc067866f1bbdaa1ae163976e68890 [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
Jose Marinho4e4e4d52019-02-22 16:23:51 +000046#define SPCI_SUCCESS INT32_C(0)
Andrew Walbran0de4f162019-09-03 16:44:20 +010047/* SPCI error codes. */
Jose Marinho4e4e4d52019-02-22 16:23:51 +000048#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 Marinho4e4e4d52019-02-22 16:23:51 +000054#define SPCI_RETRY INT32_C(-7)
55
Jose Marinho75509b42019-04-09 09:34:59 +010056/* Architected memory sharing message IDs. */
57enum spci_memory_share {
Jose Marinho713f13a2019-05-21 11:54:16 +010058 SPCI_MEMORY_LEND = 0x0,
Jose Marinho56c25732019-05-20 09:48:53 +010059 SPCI_MEMORY_RELINQUISH = 0x1,
Jose Marinho75509b42019-04-09 09:34:59 +010060 SPCI_MEMORY_DONATE = 0x2,
61};
62
Jose Marinho4e4e4d52019-02-22 16:23:51 +000063/* SPCI function specific constants. */
Andrew Scull1262ac22019-04-05 12:44:26 +010064#define SPCI_MSG_RECV_BLOCK_MASK 0x1
Jose Marinho4e4e4d52019-02-22 16:23:51 +000065#define SPCI_MSG_SEND_NOTIFY_MASK 0x1
66
Jose Marinho75509b42019-04-09 09:34:59 +010067#define SPCI_MESSAGE_ARCHITECTED 0x0
68#define SPCI_MESSAGE_IMPDEF 0x1
Jose Marinho4e4e4d52019-02-22 16:23:51 +000069#define SPCI_MESSAGE_IMPDEF_MASK 0x1
70
71#define SPCI_MSG_SEND_NOTIFY 0x1
Andrew Scull1262ac22019-04-05 12:44:26 +010072#define SPCI_MSG_RECV_BLOCK 0x1
73
74/* The maximum length possible for a single message. */
75#define SPCI_MSG_PAYLOAD_MAX (HF_MAILBOX_SIZE - sizeof(struct spci_message))
Jose Marinho4e4e4d52019-02-22 16:23:51 +000076
Jose Marinho713f13a2019-05-21 11:54:16 +010077#define spci_get_lend_descriptor(message)\
78 ((struct spci_memory_lend *)(((uint8_t *) message)\
79 + sizeof(struct spci_message)\
80 + sizeof(struct spci_architected_message_header)))
81
82enum spci_lend_access {
83 SPCI_LEND_RO_NX,
84 SPCI_LEND_RO_X,
85 SPCI_LEND_RW_NX,
86 SPCI_LEND_RW_X,
87};
88
89enum spci_lend_type {
90 SPCI_LEND_NORMAL_MEM,
91 SPCI_LEND_DEV_NGNRNE,
92 SPCI_LEND_DEV_NGNRE,
93 SPCI_LEND_DEV_NGRE,
94 SPCI_LEND_DEV_GRE,
95};
96
97enum spci_lend_cacheability {
98 SPCI_LEND_CACHE_NON_CACHEABLE,
99 SPCI_LEND_CACHE_WRITE_THROUGH,
100 SPCI_LEND_CACHE_WRITE_BACK,
101};
102
103enum spci_lend_shareability {
104 SPCI_LEND_SHARE_NON_SHAREABLE,
105 SPCI_LEND_RESERVED,
106 SPCI_LEND_OUTER_SHAREABLE,
107 SPCI_LEND_INNER_SHAREABLE,
108};
109
110#define SPCI_LEND_ACCESS_OFFSET (0x7U)
111#define SPCI_LEND_ACCESS_MASK ((0x3U) << SPCI_LEND_ACCESS_OFFSET)
112
113#define SPCI_LEND_TYPE_OFFSET (0x4U)
114#define SPCI_LEND_TYPE_MASK ((0x7U) << SPCI_LEND_TYPE_OFFSET)
115
116#define SPCI_LEND_CACHEABILITY_OFFSET (0x2U)
117#define SPCI_LEND_CACHEABILITY_MASK ((0x3U) <<\
118 SPCI_LEND_CACHEABILITY_OFFSET)
119
120#define SPCI_LEND_SHAREABILITY_OFFSET (0x0U)
121#define SPCI_LEND_SHAREABILITY_MASK ((0x3U) <<\
122 SPCI_LEND_SHAREABILITY_OFFSET)
123
124#define LEND_ATTR_FUNCTION_SET(name, offset, mask) \
125static inline void spci_set_lend_##name##_attr(uint16_t *lend_attr,\
126 const enum spci_lend_##name perm)\
127{\
128 *lend_attr = (*lend_attr & ~(mask)) | ((perm << offset) & mask);\
129}
130
131#define LEND_ATTR_FUNCTION_GET(name, offset, mask) \
132static inline enum spci_lend_##name spci_get_lend_##name##_attr(\
133 uint16_t lend_attr)\
134{\
135 return (enum spci_lend_##name)((lend_attr & mask) >> offset);\
136}
137
138LEND_ATTR_FUNCTION_SET(access, SPCI_LEND_ACCESS_OFFSET, SPCI_LEND_ACCESS_MASK)
139LEND_ATTR_FUNCTION_GET(access, SPCI_LEND_ACCESS_OFFSET, SPCI_LEND_ACCESS_MASK)
140
141LEND_ATTR_FUNCTION_SET(type, SPCI_LEND_TYPE_OFFSET, SPCI_LEND_TYPE_MASK)
142LEND_ATTR_FUNCTION_GET(type, SPCI_LEND_TYPE_OFFSET, SPCI_LEND_TYPE_MASK)
143
144LEND_ATTR_FUNCTION_SET(cacheability, SPCI_LEND_CACHEABILITY_OFFSET,
145 SPCI_LEND_CACHEABILITY_MASK)
146
147LEND_ATTR_FUNCTION_GET(cacheability, SPCI_LEND_CACHEABILITY_OFFSET,
148 SPCI_LEND_CACHEABILITY_MASK)
149
150LEND_ATTR_FUNCTION_SET(shareability, SPCI_LEND_SHAREABILITY_OFFSET,
151 SPCI_LEND_SHAREABILITY_MASK)
152
153LEND_ATTR_FUNCTION_GET(shareability, SPCI_LEND_SHAREABILITY_OFFSET,
154 SPCI_LEND_SHAREABILITY_MASK)
155
156enum spci_lend_flags {
157 SPCI_LEND_KEEP_MAPPED = 0x0,
158 SPCI_LEND_UNMAP = 0x1
159};
160
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000161/* clang-format on */
162
Fuad Tabba494376e2019-08-05 12:35:10 +0100163/** The ID of a VM. These are assigned sequentially starting with an offset. */
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000164typedef uint16_t spci_vm_id_t;
Jose Marinho75509b42019-04-09 09:34:59 +0100165typedef uint32_t spci_memory_handle_t;
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000166
Andrew Walbran52d99672019-06-25 15:51:11 +0100167/**
168 * A count of VMs. This has the same range as the VM IDs but we give it a
169 * different name to make the different semantics clear.
170 */
171typedef spci_vm_id_t spci_vm_count_t;
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100172
173/** The index of a vCPU within a particular VM. */
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100174typedef uint16_t spci_vcpu_index_t;
Andrew Walbran52d99672019-06-25 15:51:11 +0100175
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100176/**
177 * A count of vCPUs. This has the same range as the vCPU indices but we give it
178 * a different name to make the different semantics clear.
179 */
180typedef spci_vcpu_index_t spci_vcpu_count_t;
181
Jose Marinho75509b42019-04-09 09:34:59 +0100182/** Return type of SPCI functions. */
183/* TODO: Reuse spci_return_t type on all SPCI functions declarations. */
184typedef int32_t spci_return_t;
Andrew Walbran7f920af2019-09-03 17:09:30 +0100185struct spci_value {
186 uint64_t func;
187 uint64_t arg1;
188 uint64_t arg2;
189 uint64_t arg3;
190 uint64_t arg4;
191 uint64_t arg5;
192 uint64_t arg6;
193 uint64_t arg7;
194};
Jose Marinho75509b42019-04-09 09:34:59 +0100195
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100196static inline spci_vm_id_t spci_msg_send_sender(struct spci_value args)
197{
198 return (args.arg1 >> 16) & 0xffff;
199}
200
201static inline spci_vm_id_t spci_msg_send_receiver(struct spci_value args)
202{
203 return args.arg1 & 0xffff;
204}
205
206static inline uint32_t spci_msg_send_size(struct spci_value args)
207{
208 return args.arg3;
209}
210
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000211/** SPCI common message header. */
212struct spci_message {
213 /*
214 * TODO: version is part of SPCI alpha2 but will be
215 * removed in the next spec revision hence we are not
216 * including it in the header.
217 */
218
219 /**
220 * flags[0]:
221 * 0: Architected message payload;
222 * 1: Implementation defined message payload.
223 * flags[15:1] reserved (MBZ).
224 */
225 uint16_t flags;
226
227 /*
228 * TODO: Padding is present to ensure controlled offset
229 * of the length field. SPCI spec must be updated
230 * to reflect this (TBD).
231 */
232 uint16_t reserved_1;
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100233
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000234 uint32_t length;
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000235 spci_vm_id_t target_vm_id;
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000236 spci_vm_id_t source_vm_id;
237
238 /*
239 * TODO: Padding is present to ensure that the field
240 * payload alignment is 64B. SPCI spec must be updated
241 * to reflect this.
242 */
243 uint32_t reserved_2;
244
245 uint8_t payload[];
246};
247
Jose Marinho75509b42019-04-09 09:34:59 +0100248struct spci_architected_message_header {
249 uint16_t type;
250
251 /*
252 * TODO: Padding is present to ensure that the field
253 * payload is aligned on a 64B boundary. SPCI
254 * spec must be updated to reflect this.
255 */
256 uint16_t reserved[3];
257 uint8_t payload[];
258};
259
260struct spci_memory_region_constituent {
261 uint64_t address;
262 uint32_t page_count;
263
264 uint32_t reserved;
265};
266
267struct spci_memory_region {
268 spci_memory_handle_t handle;
269 uint32_t count;
270
271 struct spci_memory_region_constituent constituents[];
272};
273
Jose Marinho713f13a2019-05-21 11:54:16 +0100274struct spci_memory_lend {
275 uint16_t flags;
276 uint16_t borrower_attributes;
277
278 uint32_t reserved;
279
280 uint8_t payload[];
281};
282
Jose Marinho75509b42019-04-09 09:34:59 +0100283/* TODO: Move all the functions below this line to a support library. */
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000284/**
Jose Marinho75509b42019-04-09 09:34:59 +0100285 * Fill all the fields, except for the flags, in the SPCI message common header.
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000286 */
Jose Marinho75509b42019-04-09 09:34:59 +0100287static inline void spci_common_header_init(struct spci_message *message,
288 uint32_t message_length,
289 spci_vm_id_t target_vm_id,
290 spci_vm_id_t source_vm_id)
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000291{
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000292 message->length = message_length;
293 message->target_vm_id = target_vm_id;
294 message->source_vm_id = source_vm_id;
295
296 /*
297 * TODO: Reserved fields in the common message header will be
298 * defined as MBZ in next SPCI spec updates.
299 */
300 message->reserved_1 = 0;
301 message->reserved_2 = 0;
302}
Jose Marinho75509b42019-04-09 09:34:59 +0100303
304/**
305 * Set the SPCI implementation defined message header fields.
306 */
307static inline void spci_message_init(struct spci_message *message,
308 uint32_t message_length,
309 spci_vm_id_t target_vm_id,
310 spci_vm_id_t source_vm_id)
311{
312 spci_common_header_init(message, message_length, target_vm_id,
313 source_vm_id);
314
315 message->flags = SPCI_MESSAGE_IMPDEF;
316}
317
318/**
319 * Obtain a pointer to the architected header in the spci_message.
320 *
321 * Note: the argument "message" has const qualifier. This qualifier
322 * is meant to forbid changes in information enclosed in the
323 * struct spci_message. The spci_architected_message_header, for which
324 * a pointer is returned in this function, is not part of spci_message.
325 * Its information is meant to be changed and hence the returned pointer
326 * does not have const type qualifier.
327 */
328static inline struct spci_architected_message_header *
329spci_get_architected_message_header(const struct spci_message *message)
330{
331 return (struct spci_architected_message_header *)message->payload;
332}
333
334/**
335 * Helper method to fill in the information about the architected message.
336 */
337static inline void spci_architected_message_init(struct spci_message *message,
338 uint32_t message_length,
339 spci_vm_id_t target_vm_id,
340 spci_vm_id_t source_vm_id,
341 enum spci_memory_share type)
342{
343 struct spci_architected_message_header *architected_header;
344
345 spci_common_header_init(message, message_length, target_vm_id,
346 source_vm_id);
347 message->flags = SPCI_MESSAGE_ARCHITECTED;
348
349 /* Fill the architected header. */
350 architected_header = spci_get_architected_message_header(message);
351 architected_header->type = type;
352 architected_header->reserved[0] = 0;
353 architected_header->reserved[1] = 0;
354 architected_header->reserved[2] = 0;
355}
356
357/** Obtain a pointer to the start of the memory region in the donate message. */
358static inline struct spci_memory_region *spci_get_donated_memory_region(
359 struct spci_message *message)
360{
361 struct spci_architected_message_header *architected_header =
362 spci_get_architected_message_header(message);
363 return (struct spci_memory_region *)architected_header->payload;
364}
365
366/**
Jose Marinho713f13a2019-05-21 11:54:16 +0100367 * Helper function that copies the memory constituents and the handle
368 * information onto the address pointed to by memory_region.
369 * The function returns the length in bytes occupied by the data copied to
370 * memory_region (constituents and memory region header size).
Jose Marinho75509b42019-04-09 09:34:59 +0100371 */
Jose Marinho713f13a2019-05-21 11:54:16 +0100372static inline uint32_t spci_memory_region_add(
373 struct spci_memory_region *memory_region, spci_memory_handle_t handle,
Jose Marinho75509b42019-04-09 09:34:59 +0100374 const struct spci_memory_region_constituent constituents[],
375 uint32_t num_constituents)
376{
Jose Marinho75509b42019-04-09 09:34:59 +0100377 uint32_t constituents_length =
378 num_constituents *
379 sizeof(struct spci_memory_region_constituent);
380 uint32_t index;
381
382 memory_region->handle = handle;
383 memory_region->count = num_constituents;
384
385 for (index = 0; index < num_constituents; index++) {
386 memory_region->constituents[index] = constituents[index];
387 memory_region->constituents[index].reserved = 0;
388 }
389
390 /*
391 * TODO: Add assert ensuring that the specified message
392 * length is not greater than SPCI_MSG_PAYLOAD_MAX.
393 */
Jose Marinho713f13a2019-05-21 11:54:16 +0100394
395 return sizeof(struct spci_memory_region) + constituents_length;
Jose Marinho75509b42019-04-09 09:34:59 +0100396}
397
398/** Construct the SPCI donate memory region message. */
399static inline void spci_memory_donate(
400 struct spci_message *message, spci_vm_id_t target_vm_id,
401 spci_vm_id_t source_vm_id,
402 struct spci_memory_region_constituent *region_constituents,
403 uint32_t num_elements, uint32_t handle)
404{
405 int32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100406 struct spci_memory_region *memory_region =
407 spci_get_donated_memory_region(message);
Jose Marinho75509b42019-04-09 09:34:59 +0100408
409 message_length = sizeof(struct spci_architected_message_header);
410
411 /* Fill in the details on the common message header. */
412 spci_architected_message_init(message, message_length, target_vm_id,
413 source_vm_id, SPCI_MEMORY_DONATE);
414
415 /* Create single memory region. */
Jose Marinho713f13a2019-05-21 11:54:16 +0100416 message->length += spci_memory_region_add(
417 memory_region, handle, region_constituents, num_elements);
Jose Marinho75509b42019-04-09 09:34:59 +0100418}
Jose Marinho56c25732019-05-20 09:48:53 +0100419
420/**
421 * Construct the SPCI memory region relinquish message.
422 * A set of memory regions can be given back to the owner.
423 */
424static inline void spci_memory_relinquish(
425 struct spci_message *message, spci_vm_id_t target_vm_id,
426 spci_vm_id_t source_vm_id,
427 struct spci_memory_region_constituent *region_constituents,
428 uint64_t num_elements, uint32_t handle)
429{
430 int32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100431 struct spci_memory_region *memory_region =
432 spci_get_donated_memory_region(message);
Jose Marinho56c25732019-05-20 09:48:53 +0100433
434 message_length = sizeof(struct spci_architected_message_header);
435
436 /* Fill in the details on the common message header. */
437 spci_architected_message_init(message, message_length, target_vm_id,
438 source_vm_id, SPCI_MEMORY_RELINQUISH);
439
440 /* Create single memory region. */
Jose Marinho713f13a2019-05-21 11:54:16 +0100441 message->length += spci_memory_region_add(
442 memory_region, handle, region_constituents, num_elements);
443}
444
445/**
446 * Construct the SPCI memory region lend message.
447 */
448static inline void spci_memory_lend(
449 struct spci_message *message, spci_vm_id_t target_vm_id,
450 spci_vm_id_t source_vm_id,
451 struct spci_memory_region_constituent *region_constituents,
452 uint64_t num_elements, uint32_t handle, enum spci_lend_access access,
453 enum spci_lend_type type, enum spci_lend_cacheability cacheability,
454 enum spci_lend_shareability shareability)
455{
456 int32_t message_length;
457 struct spci_memory_region *memory_region;
458
459 const struct spci_memory_lend lend_init = {0};
460
461 struct spci_memory_lend *lend_descriptor =
462 spci_get_lend_descriptor(message);
463 memory_region = (struct spci_memory_region *)lend_descriptor->payload;
464
465 /* Initilise all struct elements to zero. */
466 *lend_descriptor = lend_init;
467
468 message_length = sizeof(struct spci_architected_message_header) +
469 sizeof(struct spci_memory_lend);
470
471 /* Fill in the details on the common message header. */
472 spci_architected_message_init(message, message_length, target_vm_id,
473 source_vm_id, SPCI_MEMORY_LEND);
474
475 lend_descriptor->flags = SPCI_LEND_KEEP_MAPPED;
476
477 /* Set memory region's page attributes. */
478 spci_set_lend_access_attr(&lend_descriptor->borrower_attributes,
479 access);
480 spci_set_lend_type_attr(&lend_descriptor->borrower_attributes, type);
481 spci_set_lend_cacheability_attr(&lend_descriptor->borrower_attributes,
482 cacheability);
483 spci_set_lend_shareability_attr(&lend_descriptor->borrower_attributes,
484 shareability);
485
486 /* Create single memory region. */
487 message->length += spci_memory_region_add(
488 memory_region, handle, region_constituents, num_elements);
Jose Marinho56c25732019-05-20 09:48:53 +0100489}