blob: 08830386b566685ce3050c04756621c682bccde1 [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
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000196/** SPCI common message header. */
197struct spci_message {
198 /*
199 * TODO: version is part of SPCI alpha2 but will be
200 * removed in the next spec revision hence we are not
201 * including it in the header.
202 */
203
204 /**
205 * flags[0]:
206 * 0: Architected message payload;
207 * 1: Implementation defined message payload.
208 * flags[15:1] reserved (MBZ).
209 */
210 uint16_t flags;
211
212 /*
213 * TODO: Padding is present to ensure controlled offset
214 * of the length field. SPCI spec must be updated
215 * to reflect this (TBD).
216 */
217 uint16_t reserved_1;
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100218
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000219 uint32_t length;
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000220 spci_vm_id_t target_vm_id;
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000221 spci_vm_id_t source_vm_id;
222
223 /*
224 * TODO: Padding is present to ensure that the field
225 * payload alignment is 64B. SPCI spec must be updated
226 * to reflect this.
227 */
228 uint32_t reserved_2;
229
230 uint8_t payload[];
231};
232
Jose Marinho75509b42019-04-09 09:34:59 +0100233struct spci_architected_message_header {
234 uint16_t type;
235
236 /*
237 * TODO: Padding is present to ensure that the field
238 * payload is aligned on a 64B boundary. SPCI
239 * spec must be updated to reflect this.
240 */
241 uint16_t reserved[3];
242 uint8_t payload[];
243};
244
245struct spci_memory_region_constituent {
246 uint64_t address;
247 uint32_t page_count;
248
249 uint32_t reserved;
250};
251
252struct spci_memory_region {
253 spci_memory_handle_t handle;
254 uint32_t count;
255
256 struct spci_memory_region_constituent constituents[];
257};
258
Jose Marinho713f13a2019-05-21 11:54:16 +0100259struct spci_memory_lend {
260 uint16_t flags;
261 uint16_t borrower_attributes;
262
263 uint32_t reserved;
264
265 uint8_t payload[];
266};
267
Jose Marinho75509b42019-04-09 09:34:59 +0100268/* TODO: Move all the functions below this line to a support library. */
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000269/**
Jose Marinho75509b42019-04-09 09:34:59 +0100270 * Fill all the fields, except for the flags, in the SPCI message common header.
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000271 */
Jose Marinho75509b42019-04-09 09:34:59 +0100272static inline void spci_common_header_init(struct spci_message *message,
273 uint32_t message_length,
274 spci_vm_id_t target_vm_id,
275 spci_vm_id_t source_vm_id)
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000276{
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000277 message->length = message_length;
278 message->target_vm_id = target_vm_id;
279 message->source_vm_id = source_vm_id;
280
281 /*
282 * TODO: Reserved fields in the common message header will be
283 * defined as MBZ in next SPCI spec updates.
284 */
285 message->reserved_1 = 0;
286 message->reserved_2 = 0;
287}
Jose Marinho75509b42019-04-09 09:34:59 +0100288
289/**
290 * Set the SPCI implementation defined message header fields.
291 */
292static inline void spci_message_init(struct spci_message *message,
293 uint32_t message_length,
294 spci_vm_id_t target_vm_id,
295 spci_vm_id_t source_vm_id)
296{
297 spci_common_header_init(message, message_length, target_vm_id,
298 source_vm_id);
299
300 message->flags = SPCI_MESSAGE_IMPDEF;
301}
302
303/**
304 * Obtain a pointer to the architected header in the spci_message.
305 *
306 * Note: the argument "message" has const qualifier. This qualifier
307 * is meant to forbid changes in information enclosed in the
308 * struct spci_message. The spci_architected_message_header, for which
309 * a pointer is returned in this function, is not part of spci_message.
310 * Its information is meant to be changed and hence the returned pointer
311 * does not have const type qualifier.
312 */
313static inline struct spci_architected_message_header *
314spci_get_architected_message_header(const struct spci_message *message)
315{
316 return (struct spci_architected_message_header *)message->payload;
317}
318
319/**
320 * Helper method to fill in the information about the architected message.
321 */
322static inline void spci_architected_message_init(struct spci_message *message,
323 uint32_t message_length,
324 spci_vm_id_t target_vm_id,
325 spci_vm_id_t source_vm_id,
326 enum spci_memory_share type)
327{
328 struct spci_architected_message_header *architected_header;
329
330 spci_common_header_init(message, message_length, target_vm_id,
331 source_vm_id);
332 message->flags = SPCI_MESSAGE_ARCHITECTED;
333
334 /* Fill the architected header. */
335 architected_header = spci_get_architected_message_header(message);
336 architected_header->type = type;
337 architected_header->reserved[0] = 0;
338 architected_header->reserved[1] = 0;
339 architected_header->reserved[2] = 0;
340}
341
342/** Obtain a pointer to the start of the memory region in the donate message. */
343static inline struct spci_memory_region *spci_get_donated_memory_region(
344 struct spci_message *message)
345{
346 struct spci_architected_message_header *architected_header =
347 spci_get_architected_message_header(message);
348 return (struct spci_memory_region *)architected_header->payload;
349}
350
351/**
Jose Marinho713f13a2019-05-21 11:54:16 +0100352 * Helper function that copies the memory constituents and the handle
353 * information onto the address pointed to by memory_region.
354 * The function returns the length in bytes occupied by the data copied to
355 * memory_region (constituents and memory region header size).
Jose Marinho75509b42019-04-09 09:34:59 +0100356 */
Jose Marinho713f13a2019-05-21 11:54:16 +0100357static inline uint32_t spci_memory_region_add(
358 struct spci_memory_region *memory_region, spci_memory_handle_t handle,
Jose Marinho75509b42019-04-09 09:34:59 +0100359 const struct spci_memory_region_constituent constituents[],
360 uint32_t num_constituents)
361{
Jose Marinho75509b42019-04-09 09:34:59 +0100362 uint32_t constituents_length =
363 num_constituents *
364 sizeof(struct spci_memory_region_constituent);
365 uint32_t index;
366
367 memory_region->handle = handle;
368 memory_region->count = num_constituents;
369
370 for (index = 0; index < num_constituents; index++) {
371 memory_region->constituents[index] = constituents[index];
372 memory_region->constituents[index].reserved = 0;
373 }
374
375 /*
376 * TODO: Add assert ensuring that the specified message
377 * length is not greater than SPCI_MSG_PAYLOAD_MAX.
378 */
Jose Marinho713f13a2019-05-21 11:54:16 +0100379
380 return sizeof(struct spci_memory_region) + constituents_length;
Jose Marinho75509b42019-04-09 09:34:59 +0100381}
382
383/** Construct the SPCI donate memory region message. */
384static inline void spci_memory_donate(
385 struct spci_message *message, spci_vm_id_t target_vm_id,
386 spci_vm_id_t source_vm_id,
387 struct spci_memory_region_constituent *region_constituents,
388 uint32_t num_elements, uint32_t handle)
389{
390 int32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100391 struct spci_memory_region *memory_region =
392 spci_get_donated_memory_region(message);
Jose Marinho75509b42019-04-09 09:34:59 +0100393
394 message_length = sizeof(struct spci_architected_message_header);
395
396 /* Fill in the details on the common message header. */
397 spci_architected_message_init(message, message_length, target_vm_id,
398 source_vm_id, SPCI_MEMORY_DONATE);
399
400 /* Create single memory region. */
Jose Marinho713f13a2019-05-21 11:54:16 +0100401 message->length += spci_memory_region_add(
402 memory_region, handle, region_constituents, num_elements);
Jose Marinho75509b42019-04-09 09:34:59 +0100403}
Jose Marinho56c25732019-05-20 09:48:53 +0100404
405/**
406 * Construct the SPCI memory region relinquish message.
407 * A set of memory regions can be given back to the owner.
408 */
409static inline void spci_memory_relinquish(
410 struct spci_message *message, spci_vm_id_t target_vm_id,
411 spci_vm_id_t source_vm_id,
412 struct spci_memory_region_constituent *region_constituents,
413 uint64_t num_elements, uint32_t handle)
414{
415 int32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100416 struct spci_memory_region *memory_region =
417 spci_get_donated_memory_region(message);
Jose Marinho56c25732019-05-20 09:48:53 +0100418
419 message_length = sizeof(struct spci_architected_message_header);
420
421 /* Fill in the details on the common message header. */
422 spci_architected_message_init(message, message_length, target_vm_id,
423 source_vm_id, SPCI_MEMORY_RELINQUISH);
424
425 /* Create single memory region. */
Jose Marinho713f13a2019-05-21 11:54:16 +0100426 message->length += spci_memory_region_add(
427 memory_region, handle, region_constituents, num_elements);
428}
429
430/**
431 * Construct the SPCI memory region lend message.
432 */
433static inline void spci_memory_lend(
434 struct spci_message *message, spci_vm_id_t target_vm_id,
435 spci_vm_id_t source_vm_id,
436 struct spci_memory_region_constituent *region_constituents,
437 uint64_t num_elements, uint32_t handle, enum spci_lend_access access,
438 enum spci_lend_type type, enum spci_lend_cacheability cacheability,
439 enum spci_lend_shareability shareability)
440{
441 int32_t message_length;
442 struct spci_memory_region *memory_region;
443
444 const struct spci_memory_lend lend_init = {0};
445
446 struct spci_memory_lend *lend_descriptor =
447 spci_get_lend_descriptor(message);
448 memory_region = (struct spci_memory_region *)lend_descriptor->payload;
449
450 /* Initilise all struct elements to zero. */
451 *lend_descriptor = lend_init;
452
453 message_length = sizeof(struct spci_architected_message_header) +
454 sizeof(struct spci_memory_lend);
455
456 /* Fill in the details on the common message header. */
457 spci_architected_message_init(message, message_length, target_vm_id,
458 source_vm_id, SPCI_MEMORY_LEND);
459
460 lend_descriptor->flags = SPCI_LEND_KEEP_MAPPED;
461
462 /* Set memory region's page attributes. */
463 spci_set_lend_access_attr(&lend_descriptor->borrower_attributes,
464 access);
465 spci_set_lend_type_attr(&lend_descriptor->borrower_attributes, type);
466 spci_set_lend_cacheability_attr(&lend_descriptor->borrower_attributes,
467 cacheability);
468 spci_set_lend_shareability_attr(&lend_descriptor->borrower_attributes,
469 shareability);
470
471 /* Create single memory region. */
472 message->length += spci_memory_region_add(
473 memory_region, handle, region_constituents, num_elements);
Jose Marinho56c25732019-05-20 09:48:53 +0100474}