blob: 125ea38d16dafa840f8a3f906c5e59cb849cf872 [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
Jose Marinho021528f2019-10-08 17:21:20 +010033#define SPCI_FEATURES_32 0x84000064
34#define SPCI_RX_RELEASE_32 0x84000065
35#define SPCI_RXTX_MAP_32 0x84000066
36#define SPCI_RXTX_UNMAP_32 0x84000067
37#define SPCI_PARTITION_INFO_GET_32 0x84000068
38#define SPCI_ID_GET_32 0x84000069
Andrew Walbran0de4f162019-09-03 16:44:20 +010039#define SPCI_MSG_POLL_32 0x8400006A
Jose Marinho021528f2019-10-08 17:21:20 +010040#define SPCI_MSG_WAIT_32 0x8400006B
41#define SPCI_YIELD_32 0x8400006C
Andrew Walbran0de4f162019-09-03 16:44:20 +010042#define SPCI_RUN_32 0x8400006D
Jose Marinho021528f2019-10-08 17:21:20 +010043#define SPCI_MSG_SEND_32 0x8400006E
44#define SPCI_MSG_SEND_DIRECT_REQ_32 0x8400006F
45#define SPCI_MSG_SEND_DIRECT_RESP_32 0x84000070
Jose Marinho4e4e4d52019-02-22 16:23:51 +000046
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 Walbran70bc8622019-10-07 14:15:58 +010064#define SPCI_MSG_RECV_BLOCK 0x1
Andrew Scull1262ac22019-04-05 12:44:26 +010065#define SPCI_MSG_RECV_BLOCK_MASK 0x1
Jose Marinho4e4e4d52019-02-22 16:23:51 +000066
67#define SPCI_MSG_SEND_NOTIFY 0x1
Andrew Walbran70bc8622019-10-07 14:15:58 +010068#define SPCI_MSG_SEND_NOTIFY_MASK 0x1
69#define SPCI_MSG_SEND_LEGACY_MEMORY 0x2
70#define SPCI_MSG_SEND_LEGACY_MEMORY_MASK 0x2
Andrew Scull1262ac22019-04-05 12:44:26 +010071
72/* The maximum length possible for a single message. */
Andrew Walbran70bc8622019-10-07 14:15:58 +010073#define SPCI_MSG_PAYLOAD_MAX HF_MAILBOX_SIZE
Jose Marinho713f13a2019-05-21 11:54:16 +010074
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
Andrew Walbranf18e63c2019-10-07 15:13:59 +0100175/** Parameter and return type of SPCI functions. */
Andrew Walbran7f920af2019-09-03 17:09:30 +0100176struct spci_value {
177 uint64_t func;
178 uint64_t arg1;
179 uint64_t arg2;
180 uint64_t arg3;
181 uint64_t arg4;
182 uint64_t arg5;
183 uint64_t arg6;
184 uint64_t arg7;
185};
Jose Marinho75509b42019-04-09 09:34:59 +0100186
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100187static inline spci_vm_id_t spci_msg_send_sender(struct spci_value args)
188{
189 return (args.arg1 >> 16) & 0xffff;
190}
191
192static inline spci_vm_id_t spci_msg_send_receiver(struct spci_value args)
193{
194 return args.arg1 & 0xffff;
195}
196
197static inline uint32_t spci_msg_send_size(struct spci_value args)
198{
199 return args.arg3;
200}
201
Andrew Walbran70bc8622019-10-07 14:15:58 +0100202static inline uint32_t spci_msg_send_attributes(struct spci_value args)
203{
204 return args.arg4;
205}
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000206
Jose Marinho75509b42019-04-09 09:34:59 +0100207struct spci_architected_message_header {
208 uint16_t type;
209
210 /*
211 * TODO: Padding is present to ensure that the field
212 * payload is aligned on a 64B boundary. SPCI
213 * spec must be updated to reflect this.
214 */
215 uint16_t reserved[3];
216 uint8_t payload[];
217};
218
219struct spci_memory_region_constituent {
220 uint64_t address;
221 uint32_t page_count;
222
223 uint32_t reserved;
224};
225
226struct spci_memory_region {
227 spci_memory_handle_t handle;
228 uint32_t count;
229
230 struct spci_memory_region_constituent constituents[];
231};
232
Jose Marinho713f13a2019-05-21 11:54:16 +0100233struct spci_memory_lend {
234 uint16_t flags;
235 uint16_t borrower_attributes;
236
237 uint32_t reserved;
238
239 uint8_t payload[];
240};
241
Jose Marinho75509b42019-04-09 09:34:59 +0100242/* TODO: Move all the functions below this line to a support library. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100243
244static inline struct spci_memory_lend *spci_get_lend_descriptor(void *message)
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000245{
Andrew Walbran70bc8622019-10-07 14:15:58 +0100246 return (struct spci_memory_lend
247 *)((struct spci_architected_message_header *)message)
248 ->payload;
Jose Marinho75509b42019-04-09 09:34:59 +0100249}
250
251/**
252 * Helper method to fill in the information about the architected message.
253 */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100254static inline void spci_architected_message_init(void *message,
Jose Marinho75509b42019-04-09 09:34:59 +0100255 enum spci_memory_share type)
256{
Jose Marinho75509b42019-04-09 09:34:59 +0100257 /* Fill the architected header. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100258 struct spci_architected_message_header *architected_header =
259 (struct spci_architected_message_header *)message;
Jose Marinho75509b42019-04-09 09:34:59 +0100260 architected_header->type = type;
261 architected_header->reserved[0] = 0;
262 architected_header->reserved[1] = 0;
263 architected_header->reserved[2] = 0;
264}
265
266/** Obtain a pointer to the start of the memory region in the donate message. */
267static inline struct spci_memory_region *spci_get_donated_memory_region(
Andrew Walbran70bc8622019-10-07 14:15:58 +0100268 void *message)
Jose Marinho75509b42019-04-09 09:34:59 +0100269{
270 struct spci_architected_message_header *architected_header =
Andrew Walbran70bc8622019-10-07 14:15:58 +0100271 (struct spci_architected_message_header *)message;
Jose Marinho75509b42019-04-09 09:34:59 +0100272 return (struct spci_memory_region *)architected_header->payload;
273}
274
275/**
Jose Marinho713f13a2019-05-21 11:54:16 +0100276 * Helper function that copies the memory constituents and the handle
277 * information onto the address pointed to by memory_region.
278 * The function returns the length in bytes occupied by the data copied to
279 * memory_region (constituents and memory region header size).
Jose Marinho75509b42019-04-09 09:34:59 +0100280 */
Jose Marinho713f13a2019-05-21 11:54:16 +0100281static inline uint32_t spci_memory_region_add(
282 struct spci_memory_region *memory_region, spci_memory_handle_t handle,
Jose Marinho75509b42019-04-09 09:34:59 +0100283 const struct spci_memory_region_constituent constituents[],
284 uint32_t num_constituents)
285{
Jose Marinho75509b42019-04-09 09:34:59 +0100286 uint32_t constituents_length =
287 num_constituents *
288 sizeof(struct spci_memory_region_constituent);
289 uint32_t index;
290
291 memory_region->handle = handle;
292 memory_region->count = num_constituents;
293
294 for (index = 0; index < num_constituents; index++) {
295 memory_region->constituents[index] = constituents[index];
296 memory_region->constituents[index].reserved = 0;
297 }
298
299 /*
300 * TODO: Add assert ensuring that the specified message
301 * length is not greater than SPCI_MSG_PAYLOAD_MAX.
302 */
Jose Marinho713f13a2019-05-21 11:54:16 +0100303
304 return sizeof(struct spci_memory_region) + constituents_length;
Jose Marinho75509b42019-04-09 09:34:59 +0100305}
306
307/** Construct the SPCI donate memory region message. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100308static inline uint32_t spci_memory_donate_init(
309 void *message,
Jose Marinho75509b42019-04-09 09:34:59 +0100310 struct spci_memory_region_constituent *region_constituents,
311 uint32_t num_elements, uint32_t handle)
312{
Andrew Walbran70bc8622019-10-07 14:15:58 +0100313 uint32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100314 struct spci_memory_region *memory_region =
315 spci_get_donated_memory_region(message);
Jose Marinho75509b42019-04-09 09:34:59 +0100316
317 message_length = sizeof(struct spci_architected_message_header);
318
319 /* Fill in the details on the common message header. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100320 spci_architected_message_init(message, SPCI_MEMORY_DONATE);
Jose Marinho75509b42019-04-09 09:34:59 +0100321
322 /* Create single memory region. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100323 message_length += spci_memory_region_add(
Jose Marinho713f13a2019-05-21 11:54:16 +0100324 memory_region, handle, region_constituents, num_elements);
Andrew Walbran70bc8622019-10-07 14:15:58 +0100325 return message_length;
Jose Marinho75509b42019-04-09 09:34:59 +0100326}
Jose Marinho56c25732019-05-20 09:48:53 +0100327
328/**
329 * Construct the SPCI memory region relinquish message.
330 * A set of memory regions can be given back to the owner.
331 */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100332static inline uint32_t spci_memory_relinquish_init(
333 void *message,
Jose Marinho56c25732019-05-20 09:48:53 +0100334 struct spci_memory_region_constituent *region_constituents,
335 uint64_t num_elements, uint32_t handle)
336{
Andrew Walbran70bc8622019-10-07 14:15:58 +0100337 uint32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100338 struct spci_memory_region *memory_region =
339 spci_get_donated_memory_region(message);
Jose Marinho56c25732019-05-20 09:48:53 +0100340
341 message_length = sizeof(struct spci_architected_message_header);
342
343 /* Fill in the details on the common message header. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100344 spci_architected_message_init(message, SPCI_MEMORY_RELINQUISH);
Jose Marinho56c25732019-05-20 09:48:53 +0100345
346 /* Create single memory region. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100347 message_length += spci_memory_region_add(
Jose Marinho713f13a2019-05-21 11:54:16 +0100348 memory_region, handle, region_constituents, num_elements);
Andrew Walbran70bc8622019-10-07 14:15:58 +0100349 return message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100350}
351
352/**
353 * Construct the SPCI memory region lend message.
354 */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100355static inline uint32_t spci_memory_lend_init(
356 void *message,
Jose Marinho713f13a2019-05-21 11:54:16 +0100357 struct spci_memory_region_constituent *region_constituents,
358 uint64_t num_elements, uint32_t handle, enum spci_lend_access access,
359 enum spci_lend_type type, enum spci_lend_cacheability cacheability,
360 enum spci_lend_shareability shareability)
361{
Andrew Walbran70bc8622019-10-07 14:15:58 +0100362 uint32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100363 struct spci_memory_region *memory_region;
364
365 const struct spci_memory_lend lend_init = {0};
366
367 struct spci_memory_lend *lend_descriptor =
368 spci_get_lend_descriptor(message);
369 memory_region = (struct spci_memory_region *)lend_descriptor->payload;
370
371 /* Initilise all struct elements to zero. */
372 *lend_descriptor = lend_init;
373
374 message_length = sizeof(struct spci_architected_message_header) +
375 sizeof(struct spci_memory_lend);
376
377 /* Fill in the details on the common message header. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100378 spci_architected_message_init(message, SPCI_MEMORY_LEND);
Jose Marinho713f13a2019-05-21 11:54:16 +0100379
380 lend_descriptor->flags = SPCI_LEND_KEEP_MAPPED;
381
382 /* Set memory region's page attributes. */
383 spci_set_lend_access_attr(&lend_descriptor->borrower_attributes,
384 access);
385 spci_set_lend_type_attr(&lend_descriptor->borrower_attributes, type);
386 spci_set_lend_cacheability_attr(&lend_descriptor->borrower_attributes,
387 cacheability);
388 spci_set_lend_shareability_attr(&lend_descriptor->borrower_attributes,
389 shareability);
390
391 /* Create single memory region. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100392 message_length += spci_memory_region_add(
Jose Marinho713f13a2019-05-21 11:54:16 +0100393 memory_region, handle, region_constituents, num_elements);
Andrew Walbran70bc8622019-10-07 14:15:58 +0100394 return message_length;
Jose Marinho56c25732019-05-20 09:48:53 +0100395}