blob: 98d4155dad0a83b82a35762a8dfea049c2f176db [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. */
29#define SPCI_VERSION_32 0x84000060
30#define SPCI_MSG_BUF_LIST_EXCHANGE_32 0x84000061
31#define SPCI_MSG_RECV_32 0x84000062
32#define SPCI_MSG_PUT_32 0x84000063
33#define SPCI_MSG_SEND_32 0x84000064
34#define SPCI_MSG_SEND_REC_32 0x84000065
35#define SPCI_RUN_32 0x84000066
36#define SPCI_YIELD_32 0x84000067
37
38/* SPCI return codes. */
39#define SPCI_SUCCESS INT32_C(0)
40#define SPCI_NOT_SUPPORTED INT32_C(-1)
41#define SPCI_INVALID_PARAMETERS INT32_C(-2)
42#define SPCI_NO_MEMORY INT32_C(-3)
43#define SPCI_BUSY INT32_C(-4)
44#define SPCI_INTERRUPTED INT32_C(-5)
45#define SPCI_DENIED INT32_C(-6)
46/* TODO: return code currently undefined in SPCI alpha2. */
47#define SPCI_RETRY INT32_C(-7)
48
Jose Marinho75509b42019-04-09 09:34:59 +010049/* Architected memory sharing message IDs. */
50enum spci_memory_share {
Jose Marinho713f13a2019-05-21 11:54:16 +010051 SPCI_MEMORY_LEND = 0x0,
Jose Marinho56c25732019-05-20 09:48:53 +010052 SPCI_MEMORY_RELINQUISH = 0x1,
Jose Marinho75509b42019-04-09 09:34:59 +010053 SPCI_MEMORY_DONATE = 0x2,
54};
55
Jose Marinho4e4e4d52019-02-22 16:23:51 +000056/* SPCI function specific constants. */
Andrew Scull1262ac22019-04-05 12:44:26 +010057#define SPCI_MSG_RECV_BLOCK_MASK 0x1
Jose Marinho4e4e4d52019-02-22 16:23:51 +000058#define SPCI_MSG_SEND_NOTIFY_MASK 0x1
59
Jose Marinho75509b42019-04-09 09:34:59 +010060#define SPCI_MESSAGE_ARCHITECTED 0x0
61#define SPCI_MESSAGE_IMPDEF 0x1
Jose Marinho4e4e4d52019-02-22 16:23:51 +000062#define SPCI_MESSAGE_IMPDEF_MASK 0x1
63
64#define SPCI_MSG_SEND_NOTIFY 0x1
Andrew Scull1262ac22019-04-05 12:44:26 +010065#define SPCI_MSG_RECV_BLOCK 0x1
66
67/* The maximum length possible for a single message. */
68#define SPCI_MSG_PAYLOAD_MAX (HF_MAILBOX_SIZE - sizeof(struct spci_message))
Jose Marinho4e4e4d52019-02-22 16:23:51 +000069
Jose Marinho713f13a2019-05-21 11:54:16 +010070#define spci_get_lend_descriptor(message)\
71 ((struct spci_memory_lend *)(((uint8_t *) message)\
72 + sizeof(struct spci_message)\
73 + sizeof(struct spci_architected_message_header)))
74
75enum spci_lend_access {
76 SPCI_LEND_RO_NX,
77 SPCI_LEND_RO_X,
78 SPCI_LEND_RW_NX,
79 SPCI_LEND_RW_X,
80};
81
82enum spci_lend_type {
83 SPCI_LEND_NORMAL_MEM,
84 SPCI_LEND_DEV_NGNRNE,
85 SPCI_LEND_DEV_NGNRE,
86 SPCI_LEND_DEV_NGRE,
87 SPCI_LEND_DEV_GRE,
88};
89
90enum spci_lend_cacheability {
91 SPCI_LEND_CACHE_NON_CACHEABLE,
92 SPCI_LEND_CACHE_WRITE_THROUGH,
93 SPCI_LEND_CACHE_WRITE_BACK,
94};
95
96enum spci_lend_shareability {
97 SPCI_LEND_SHARE_NON_SHAREABLE,
98 SPCI_LEND_RESERVED,
99 SPCI_LEND_OUTER_SHAREABLE,
100 SPCI_LEND_INNER_SHAREABLE,
101};
102
103#define SPCI_LEND_ACCESS_OFFSET (0x7U)
104#define SPCI_LEND_ACCESS_MASK ((0x3U) << SPCI_LEND_ACCESS_OFFSET)
105
106#define SPCI_LEND_TYPE_OFFSET (0x4U)
107#define SPCI_LEND_TYPE_MASK ((0x7U) << SPCI_LEND_TYPE_OFFSET)
108
109#define SPCI_LEND_CACHEABILITY_OFFSET (0x2U)
110#define SPCI_LEND_CACHEABILITY_MASK ((0x3U) <<\
111 SPCI_LEND_CACHEABILITY_OFFSET)
112
113#define SPCI_LEND_SHAREABILITY_OFFSET (0x0U)
114#define SPCI_LEND_SHAREABILITY_MASK ((0x3U) <<\
115 SPCI_LEND_SHAREABILITY_OFFSET)
116
117#define LEND_ATTR_FUNCTION_SET(name, offset, mask) \
118static inline void spci_set_lend_##name##_attr(uint16_t *lend_attr,\
119 const enum spci_lend_##name perm)\
120{\
121 *lend_attr = (*lend_attr & ~(mask)) | ((perm << offset) & mask);\
122}
123
124#define LEND_ATTR_FUNCTION_GET(name, offset, mask) \
125static inline enum spci_lend_##name spci_get_lend_##name##_attr(\
126 uint16_t lend_attr)\
127{\
128 return (enum spci_lend_##name)((lend_attr & mask) >> offset);\
129}
130
131LEND_ATTR_FUNCTION_SET(access, SPCI_LEND_ACCESS_OFFSET, SPCI_LEND_ACCESS_MASK)
132LEND_ATTR_FUNCTION_GET(access, SPCI_LEND_ACCESS_OFFSET, SPCI_LEND_ACCESS_MASK)
133
134LEND_ATTR_FUNCTION_SET(type, SPCI_LEND_TYPE_OFFSET, SPCI_LEND_TYPE_MASK)
135LEND_ATTR_FUNCTION_GET(type, SPCI_LEND_TYPE_OFFSET, SPCI_LEND_TYPE_MASK)
136
137LEND_ATTR_FUNCTION_SET(cacheability, SPCI_LEND_CACHEABILITY_OFFSET,
138 SPCI_LEND_CACHEABILITY_MASK)
139
140LEND_ATTR_FUNCTION_GET(cacheability, SPCI_LEND_CACHEABILITY_OFFSET,
141 SPCI_LEND_CACHEABILITY_MASK)
142
143LEND_ATTR_FUNCTION_SET(shareability, SPCI_LEND_SHAREABILITY_OFFSET,
144 SPCI_LEND_SHAREABILITY_MASK)
145
146LEND_ATTR_FUNCTION_GET(shareability, SPCI_LEND_SHAREABILITY_OFFSET,
147 SPCI_LEND_SHAREABILITY_MASK)
148
149enum spci_lend_flags {
150 SPCI_LEND_KEEP_MAPPED = 0x0,
151 SPCI_LEND_UNMAP = 0x1
152};
153
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000154/* clang-format on */
155
Fuad Tabba494376e2019-08-05 12:35:10 +0100156/** The ID of a VM. These are assigned sequentially starting with an offset. */
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000157typedef uint16_t spci_vm_id_t;
Jose Marinho75509b42019-04-09 09:34:59 +0100158typedef uint32_t spci_memory_handle_t;
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000159
Andrew Walbran52d99672019-06-25 15:51:11 +0100160/**
161 * A count of VMs. This has the same range as the VM IDs but we give it a
162 * different name to make the different semantics clear.
163 */
164typedef spci_vm_id_t spci_vm_count_t;
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100165
166/** The index of a vCPU within a particular VM. */
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100167typedef uint16_t spci_vcpu_index_t;
Andrew Walbran52d99672019-06-25 15:51:11 +0100168
Andrew Walbranc6d23c42019-06-26 13:30:42 +0100169/**
170 * A count of vCPUs. This has the same range as the vCPU indices but we give it
171 * a different name to make the different semantics clear.
172 */
173typedef spci_vcpu_index_t spci_vcpu_count_t;
174
Jose Marinho75509b42019-04-09 09:34:59 +0100175/** Return type of SPCI functions. */
176/* TODO: Reuse spci_return_t type on all SPCI functions declarations. */
177typedef int32_t spci_return_t;
178
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000179/** SPCI common message header. */
180struct spci_message {
181 /*
182 * TODO: version is part of SPCI alpha2 but will be
183 * removed in the next spec revision hence we are not
184 * including it in the header.
185 */
186
187 /**
188 * flags[0]:
189 * 0: Architected message payload;
190 * 1: Implementation defined message payload.
191 * flags[15:1] reserved (MBZ).
192 */
193 uint16_t flags;
194
195 /*
196 * TODO: Padding is present to ensure controlled offset
197 * of the length field. SPCI spec must be updated
198 * to reflect this (TBD).
199 */
200 uint16_t reserved_1;
Andrew Sculla1aa2ba2019-04-05 11:49:02 +0100201
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000202 uint32_t length;
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000203 spci_vm_id_t target_vm_id;
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000204 spci_vm_id_t source_vm_id;
205
206 /*
207 * TODO: Padding is present to ensure that the field
208 * payload alignment is 64B. SPCI spec must be updated
209 * to reflect this.
210 */
211 uint32_t reserved_2;
212
213 uint8_t payload[];
214};
215
Jose Marinho75509b42019-04-09 09:34:59 +0100216struct spci_architected_message_header {
217 uint16_t type;
218
219 /*
220 * TODO: Padding is present to ensure that the field
221 * payload is aligned on a 64B boundary. SPCI
222 * spec must be updated to reflect this.
223 */
224 uint16_t reserved[3];
225 uint8_t payload[];
226};
227
228struct spci_memory_region_constituent {
229 uint64_t address;
230 uint32_t page_count;
231
232 uint32_t reserved;
233};
234
235struct spci_memory_region {
236 spci_memory_handle_t handle;
237 uint32_t count;
238
239 struct spci_memory_region_constituent constituents[];
240};
241
Jose Marinho713f13a2019-05-21 11:54:16 +0100242struct spci_memory_lend {
243 uint16_t flags;
244 uint16_t borrower_attributes;
245
246 uint32_t reserved;
247
248 uint8_t payload[];
249};
250
Jose Marinho75509b42019-04-09 09:34:59 +0100251/* TODO: Move all the functions below this line to a support library. */
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000252/**
Jose Marinho75509b42019-04-09 09:34:59 +0100253 * Fill all the fields, except for the flags, in the SPCI message common header.
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000254 */
Jose Marinho75509b42019-04-09 09:34:59 +0100255static inline void spci_common_header_init(struct spci_message *message,
256 uint32_t message_length,
257 spci_vm_id_t target_vm_id,
258 spci_vm_id_t source_vm_id)
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000259{
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000260 message->length = message_length;
261 message->target_vm_id = target_vm_id;
262 message->source_vm_id = source_vm_id;
263
264 /*
265 * TODO: Reserved fields in the common message header will be
266 * defined as MBZ in next SPCI spec updates.
267 */
268 message->reserved_1 = 0;
269 message->reserved_2 = 0;
270}
Jose Marinho75509b42019-04-09 09:34:59 +0100271
272/**
273 * Set the SPCI implementation defined message header fields.
274 */
275static inline void spci_message_init(struct spci_message *message,
276 uint32_t message_length,
277 spci_vm_id_t target_vm_id,
278 spci_vm_id_t source_vm_id)
279{
280 spci_common_header_init(message, message_length, target_vm_id,
281 source_vm_id);
282
283 message->flags = SPCI_MESSAGE_IMPDEF;
284}
285
286/**
287 * Obtain a pointer to the architected header in the spci_message.
288 *
289 * Note: the argument "message" has const qualifier. This qualifier
290 * is meant to forbid changes in information enclosed in the
291 * struct spci_message. The spci_architected_message_header, for which
292 * a pointer is returned in this function, is not part of spci_message.
293 * Its information is meant to be changed and hence the returned pointer
294 * does not have const type qualifier.
295 */
296static inline struct spci_architected_message_header *
297spci_get_architected_message_header(const struct spci_message *message)
298{
299 return (struct spci_architected_message_header *)message->payload;
300}
301
302/**
303 * Helper method to fill in the information about the architected message.
304 */
305static inline void spci_architected_message_init(struct spci_message *message,
306 uint32_t message_length,
307 spci_vm_id_t target_vm_id,
308 spci_vm_id_t source_vm_id,
309 enum spci_memory_share type)
310{
311 struct spci_architected_message_header *architected_header;
312
313 spci_common_header_init(message, message_length, target_vm_id,
314 source_vm_id);
315 message->flags = SPCI_MESSAGE_ARCHITECTED;
316
317 /* Fill the architected header. */
318 architected_header = spci_get_architected_message_header(message);
319 architected_header->type = type;
320 architected_header->reserved[0] = 0;
321 architected_header->reserved[1] = 0;
322 architected_header->reserved[2] = 0;
323}
324
325/** Obtain a pointer to the start of the memory region in the donate message. */
326static inline struct spci_memory_region *spci_get_donated_memory_region(
327 struct spci_message *message)
328{
329 struct spci_architected_message_header *architected_header =
330 spci_get_architected_message_header(message);
331 return (struct spci_memory_region *)architected_header->payload;
332}
333
334/**
Jose Marinho713f13a2019-05-21 11:54:16 +0100335 * Helper function that copies the memory constituents and the handle
336 * information onto the address pointed to by memory_region.
337 * The function returns the length in bytes occupied by the data copied to
338 * memory_region (constituents and memory region header size).
Jose Marinho75509b42019-04-09 09:34:59 +0100339 */
Jose Marinho713f13a2019-05-21 11:54:16 +0100340static inline uint32_t spci_memory_region_add(
341 struct spci_memory_region *memory_region, spci_memory_handle_t handle,
Jose Marinho75509b42019-04-09 09:34:59 +0100342 const struct spci_memory_region_constituent constituents[],
343 uint32_t num_constituents)
344{
Jose Marinho75509b42019-04-09 09:34:59 +0100345 uint32_t constituents_length =
346 num_constituents *
347 sizeof(struct spci_memory_region_constituent);
348 uint32_t index;
349
350 memory_region->handle = handle;
351 memory_region->count = num_constituents;
352
353 for (index = 0; index < num_constituents; index++) {
354 memory_region->constituents[index] = constituents[index];
355 memory_region->constituents[index].reserved = 0;
356 }
357
358 /*
359 * TODO: Add assert ensuring that the specified message
360 * length is not greater than SPCI_MSG_PAYLOAD_MAX.
361 */
Jose Marinho713f13a2019-05-21 11:54:16 +0100362
363 return sizeof(struct spci_memory_region) + constituents_length;
Jose Marinho75509b42019-04-09 09:34:59 +0100364}
365
366/** Construct the SPCI donate memory region message. */
367static inline void spci_memory_donate(
368 struct spci_message *message, spci_vm_id_t target_vm_id,
369 spci_vm_id_t source_vm_id,
370 struct spci_memory_region_constituent *region_constituents,
371 uint32_t num_elements, uint32_t handle)
372{
373 int32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100374 struct spci_memory_region *memory_region =
375 spci_get_donated_memory_region(message);
Jose Marinho75509b42019-04-09 09:34:59 +0100376
377 message_length = sizeof(struct spci_architected_message_header);
378
379 /* Fill in the details on the common message header. */
380 spci_architected_message_init(message, message_length, target_vm_id,
381 source_vm_id, SPCI_MEMORY_DONATE);
382
383 /* Create single memory region. */
Jose Marinho713f13a2019-05-21 11:54:16 +0100384 message->length += spci_memory_region_add(
385 memory_region, handle, region_constituents, num_elements);
Jose Marinho75509b42019-04-09 09:34:59 +0100386}
Jose Marinho56c25732019-05-20 09:48:53 +0100387
388/**
389 * Construct the SPCI memory region relinquish message.
390 * A set of memory regions can be given back to the owner.
391 */
392static inline void spci_memory_relinquish(
393 struct spci_message *message, spci_vm_id_t target_vm_id,
394 spci_vm_id_t source_vm_id,
395 struct spci_memory_region_constituent *region_constituents,
396 uint64_t num_elements, uint32_t handle)
397{
398 int32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100399 struct spci_memory_region *memory_region =
400 spci_get_donated_memory_region(message);
Jose Marinho56c25732019-05-20 09:48:53 +0100401
402 message_length = sizeof(struct spci_architected_message_header);
403
404 /* Fill in the details on the common message header. */
405 spci_architected_message_init(message, message_length, target_vm_id,
406 source_vm_id, SPCI_MEMORY_RELINQUISH);
407
408 /* Create single memory region. */
Jose Marinho713f13a2019-05-21 11:54:16 +0100409 message->length += spci_memory_region_add(
410 memory_region, handle, region_constituents, num_elements);
411}
412
413/**
414 * Construct the SPCI memory region lend message.
415 */
416static inline void spci_memory_lend(
417 struct spci_message *message, spci_vm_id_t target_vm_id,
418 spci_vm_id_t source_vm_id,
419 struct spci_memory_region_constituent *region_constituents,
420 uint64_t num_elements, uint32_t handle, enum spci_lend_access access,
421 enum spci_lend_type type, enum spci_lend_cacheability cacheability,
422 enum spci_lend_shareability shareability)
423{
424 int32_t message_length;
425 struct spci_memory_region *memory_region;
426
427 const struct spci_memory_lend lend_init = {0};
428
429 struct spci_memory_lend *lend_descriptor =
430 spci_get_lend_descriptor(message);
431 memory_region = (struct spci_memory_region *)lend_descriptor->payload;
432
433 /* Initilise all struct elements to zero. */
434 *lend_descriptor = lend_init;
435
436 message_length = sizeof(struct spci_architected_message_header) +
437 sizeof(struct spci_memory_lend);
438
439 /* Fill in the details on the common message header. */
440 spci_architected_message_init(message, message_length, target_vm_id,
441 source_vm_id, SPCI_MEMORY_LEND);
442
443 lend_descriptor->flags = SPCI_LEND_KEEP_MAPPED;
444
445 /* Set memory region's page attributes. */
446 spci_set_lend_access_attr(&lend_descriptor->borrower_attributes,
447 access);
448 spci_set_lend_type_attr(&lend_descriptor->borrower_attributes, type);
449 spci_set_lend_cacheability_attr(&lend_descriptor->borrower_attributes,
450 cacheability);
451 spci_set_lend_shareability_attr(&lend_descriptor->borrower_attributes,
452 shareability);
453
454 /* Create single memory region. */
455 message->length += spci_memory_region_add(
456 memory_region, handle, region_constituents, num_elements);
Jose Marinho56c25732019-05-20 09:48:53 +0100457}