blob: 39fcaf11c9f34ebb8345418b037bb33a08221d32 [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;
185
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000186/** SPCI common message header. */
187struct spci_message {
188 /*
189 * TODO: version is part of SPCI alpha2 but will be
190 * removed in the next spec revision hence we are not
191 * including it in the header.
192 */
193
194 /**
195 * flags[0]:
196 * 0: Architected message payload;
197 * 1: Implementation defined message payload.
198 * flags[15:1] reserved (MBZ).
199 */
200 uint16_t flags;
201
202 /*
203 * TODO: Padding is present to ensure controlled offset
204 * of the length field. SPCI spec must be updated
205 * to reflect this (TBD).
206 */
207 uint16_t reserved_1;
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100208
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000209 uint32_t length;
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000210 spci_vm_id_t target_vm_id;
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000211 spci_vm_id_t source_vm_id;
212
213 /*
214 * TODO: Padding is present to ensure that the field
215 * payload alignment is 64B. SPCI spec must be updated
216 * to reflect this.
217 */
218 uint32_t reserved_2;
219
220 uint8_t payload[];
221};
222
Jose Marinho75509b42019-04-09 09:34:59 +0100223struct spci_architected_message_header {
224 uint16_t type;
225
226 /*
227 * TODO: Padding is present to ensure that the field
228 * payload is aligned on a 64B boundary. SPCI
229 * spec must be updated to reflect this.
230 */
231 uint16_t reserved[3];
232 uint8_t payload[];
233};
234
235struct spci_memory_region_constituent {
236 uint64_t address;
237 uint32_t page_count;
238
239 uint32_t reserved;
240};
241
242struct spci_memory_region {
243 spci_memory_handle_t handle;
244 uint32_t count;
245
246 struct spci_memory_region_constituent constituents[];
247};
248
Jose Marinho713f13a2019-05-21 11:54:16 +0100249struct spci_memory_lend {
250 uint16_t flags;
251 uint16_t borrower_attributes;
252
253 uint32_t reserved;
254
255 uint8_t payload[];
256};
257
Jose Marinho75509b42019-04-09 09:34:59 +0100258/* TODO: Move all the functions below this line to a support library. */
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000259/**
Jose Marinho75509b42019-04-09 09:34:59 +0100260 * Fill all the fields, except for the flags, in the SPCI message common header.
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000261 */
Jose Marinho75509b42019-04-09 09:34:59 +0100262static inline void spci_common_header_init(struct spci_message *message,
263 uint32_t message_length,
264 spci_vm_id_t target_vm_id,
265 spci_vm_id_t source_vm_id)
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000266{
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000267 message->length = message_length;
268 message->target_vm_id = target_vm_id;
269 message->source_vm_id = source_vm_id;
270
271 /*
272 * TODO: Reserved fields in the common message header will be
273 * defined as MBZ in next SPCI spec updates.
274 */
275 message->reserved_1 = 0;
276 message->reserved_2 = 0;
277}
Jose Marinho75509b42019-04-09 09:34:59 +0100278
279/**
280 * Set the SPCI implementation defined message header fields.
281 */
282static inline void spci_message_init(struct spci_message *message,
283 uint32_t message_length,
284 spci_vm_id_t target_vm_id,
285 spci_vm_id_t source_vm_id)
286{
287 spci_common_header_init(message, message_length, target_vm_id,
288 source_vm_id);
289
290 message->flags = SPCI_MESSAGE_IMPDEF;
291}
292
293/**
294 * Obtain a pointer to the architected header in the spci_message.
295 *
296 * Note: the argument "message" has const qualifier. This qualifier
297 * is meant to forbid changes in information enclosed in the
298 * struct spci_message. The spci_architected_message_header, for which
299 * a pointer is returned in this function, is not part of spci_message.
300 * Its information is meant to be changed and hence the returned pointer
301 * does not have const type qualifier.
302 */
303static inline struct spci_architected_message_header *
304spci_get_architected_message_header(const struct spci_message *message)
305{
306 return (struct spci_architected_message_header *)message->payload;
307}
308
309/**
310 * Helper method to fill in the information about the architected message.
311 */
312static inline void spci_architected_message_init(struct spci_message *message,
313 uint32_t message_length,
314 spci_vm_id_t target_vm_id,
315 spci_vm_id_t source_vm_id,
316 enum spci_memory_share type)
317{
318 struct spci_architected_message_header *architected_header;
319
320 spci_common_header_init(message, message_length, target_vm_id,
321 source_vm_id);
322 message->flags = SPCI_MESSAGE_ARCHITECTED;
323
324 /* Fill the architected header. */
325 architected_header = spci_get_architected_message_header(message);
326 architected_header->type = type;
327 architected_header->reserved[0] = 0;
328 architected_header->reserved[1] = 0;
329 architected_header->reserved[2] = 0;
330}
331
332/** Obtain a pointer to the start of the memory region in the donate message. */
333static inline struct spci_memory_region *spci_get_donated_memory_region(
334 struct spci_message *message)
335{
336 struct spci_architected_message_header *architected_header =
337 spci_get_architected_message_header(message);
338 return (struct spci_memory_region *)architected_header->payload;
339}
340
341/**
Jose Marinho713f13a2019-05-21 11:54:16 +0100342 * Helper function that copies the memory constituents and the handle
343 * information onto the address pointed to by memory_region.
344 * The function returns the length in bytes occupied by the data copied to
345 * memory_region (constituents and memory region header size).
Jose Marinho75509b42019-04-09 09:34:59 +0100346 */
Jose Marinho713f13a2019-05-21 11:54:16 +0100347static inline uint32_t spci_memory_region_add(
348 struct spci_memory_region *memory_region, spci_memory_handle_t handle,
Jose Marinho75509b42019-04-09 09:34:59 +0100349 const struct spci_memory_region_constituent constituents[],
350 uint32_t num_constituents)
351{
Jose Marinho75509b42019-04-09 09:34:59 +0100352 uint32_t constituents_length =
353 num_constituents *
354 sizeof(struct spci_memory_region_constituent);
355 uint32_t index;
356
357 memory_region->handle = handle;
358 memory_region->count = num_constituents;
359
360 for (index = 0; index < num_constituents; index++) {
361 memory_region->constituents[index] = constituents[index];
362 memory_region->constituents[index].reserved = 0;
363 }
364
365 /*
366 * TODO: Add assert ensuring that the specified message
367 * length is not greater than SPCI_MSG_PAYLOAD_MAX.
368 */
Jose Marinho713f13a2019-05-21 11:54:16 +0100369
370 return sizeof(struct spci_memory_region) + constituents_length;
Jose Marinho75509b42019-04-09 09:34:59 +0100371}
372
373/** Construct the SPCI donate memory region message. */
374static inline void spci_memory_donate(
375 struct spci_message *message, spci_vm_id_t target_vm_id,
376 spci_vm_id_t source_vm_id,
377 struct spci_memory_region_constituent *region_constituents,
378 uint32_t num_elements, uint32_t handle)
379{
380 int32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100381 struct spci_memory_region *memory_region =
382 spci_get_donated_memory_region(message);
Jose Marinho75509b42019-04-09 09:34:59 +0100383
384 message_length = sizeof(struct spci_architected_message_header);
385
386 /* Fill in the details on the common message header. */
387 spci_architected_message_init(message, message_length, target_vm_id,
388 source_vm_id, SPCI_MEMORY_DONATE);
389
390 /* Create single memory region. */
Jose Marinho713f13a2019-05-21 11:54:16 +0100391 message->length += spci_memory_region_add(
392 memory_region, handle, region_constituents, num_elements);
Jose Marinho75509b42019-04-09 09:34:59 +0100393}
Jose Marinho56c25732019-05-20 09:48:53 +0100394
395/**
396 * Construct the SPCI memory region relinquish message.
397 * A set of memory regions can be given back to the owner.
398 */
399static inline void spci_memory_relinquish(
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 uint64_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 Marinho56c25732019-05-20 09:48:53 +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_RELINQUISH);
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);
418}
419
420/**
421 * Construct the SPCI memory region lend message.
422 */
423static inline void spci_memory_lend(
424 struct spci_message *message, spci_vm_id_t target_vm_id,
425 spci_vm_id_t source_vm_id,
426 struct spci_memory_region_constituent *region_constituents,
427 uint64_t num_elements, uint32_t handle, enum spci_lend_access access,
428 enum spci_lend_type type, enum spci_lend_cacheability cacheability,
429 enum spci_lend_shareability shareability)
430{
431 int32_t message_length;
432 struct spci_memory_region *memory_region;
433
434 const struct spci_memory_lend lend_init = {0};
435
436 struct spci_memory_lend *lend_descriptor =
437 spci_get_lend_descriptor(message);
438 memory_region = (struct spci_memory_region *)lend_descriptor->payload;
439
440 /* Initilise all struct elements to zero. */
441 *lend_descriptor = lend_init;
442
443 message_length = sizeof(struct spci_architected_message_header) +
444 sizeof(struct spci_memory_lend);
445
446 /* Fill in the details on the common message header. */
447 spci_architected_message_init(message, message_length, target_vm_id,
448 source_vm_id, SPCI_MEMORY_LEND);
449
450 lend_descriptor->flags = SPCI_LEND_KEEP_MAPPED;
451
452 /* Set memory region's page attributes. */
453 spci_set_lend_access_attr(&lend_descriptor->borrower_attributes,
454 access);
455 spci_set_lend_type_attr(&lend_descriptor->borrower_attributes, type);
456 spci_set_lend_cacheability_attr(&lend_descriptor->borrower_attributes,
457 cacheability);
458 spci_set_lend_shareability_attr(&lend_descriptor->borrower_attributes,
459 shareability);
460
461 /* Create single memory region. */
462 message->length += spci_memory_region_add(
463 memory_region, handle, region_constituents, num_elements);
Jose Marinho56c25732019-05-20 09:48:53 +0100464}