blob: e878ffe0b4edbb17fab55e65186bbe5d160b16d1 [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 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
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;
Andrew Walbran7f920af2019-09-03 17:09:30 +0100178struct spci_value {
179 uint64_t func;
180 uint64_t arg1;
181 uint64_t arg2;
182 uint64_t arg3;
183 uint64_t arg4;
184 uint64_t arg5;
185 uint64_t arg6;
186 uint64_t arg7;
187};
Jose Marinho75509b42019-04-09 09:34:59 +0100188
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100189static inline spci_vm_id_t spci_msg_send_sender(struct spci_value args)
190{
191 return (args.arg1 >> 16) & 0xffff;
192}
193
194static inline spci_vm_id_t spci_msg_send_receiver(struct spci_value args)
195{
196 return args.arg1 & 0xffff;
197}
198
199static inline uint32_t spci_msg_send_size(struct spci_value args)
200{
201 return args.arg3;
202}
203
Andrew Walbran70bc8622019-10-07 14:15:58 +0100204static inline uint32_t spci_msg_send_attributes(struct spci_value args)
205{
206 return args.arg4;
207}
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000208
Jose Marinho75509b42019-04-09 09:34:59 +0100209struct spci_architected_message_header {
210 uint16_t type;
211
212 /*
213 * TODO: Padding is present to ensure that the field
214 * payload is aligned on a 64B boundary. SPCI
215 * spec must be updated to reflect this.
216 */
217 uint16_t reserved[3];
218 uint8_t payload[];
219};
220
221struct spci_memory_region_constituent {
222 uint64_t address;
223 uint32_t page_count;
224
225 uint32_t reserved;
226};
227
228struct spci_memory_region {
229 spci_memory_handle_t handle;
230 uint32_t count;
231
232 struct spci_memory_region_constituent constituents[];
233};
234
Jose Marinho713f13a2019-05-21 11:54:16 +0100235struct spci_memory_lend {
236 uint16_t flags;
237 uint16_t borrower_attributes;
238
239 uint32_t reserved;
240
241 uint8_t payload[];
242};
243
Jose Marinho75509b42019-04-09 09:34:59 +0100244/* TODO: Move all the functions below this line to a support library. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100245
246static inline struct spci_memory_lend *spci_get_lend_descriptor(void *message)
Jose Marinho4e4e4d52019-02-22 16:23:51 +0000247{
Andrew Walbran70bc8622019-10-07 14:15:58 +0100248 return (struct spci_memory_lend
249 *)((struct spci_architected_message_header *)message)
250 ->payload;
Jose Marinho75509b42019-04-09 09:34:59 +0100251}
252
253/**
254 * Helper method to fill in the information about the architected message.
255 */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100256static inline void spci_architected_message_init(void *message,
Jose Marinho75509b42019-04-09 09:34:59 +0100257 enum spci_memory_share type)
258{
Jose Marinho75509b42019-04-09 09:34:59 +0100259 /* Fill the architected header. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100260 struct spci_architected_message_header *architected_header =
261 (struct spci_architected_message_header *)message;
Jose Marinho75509b42019-04-09 09:34:59 +0100262 architected_header->type = type;
263 architected_header->reserved[0] = 0;
264 architected_header->reserved[1] = 0;
265 architected_header->reserved[2] = 0;
266}
267
268/** Obtain a pointer to the start of the memory region in the donate message. */
269static inline struct spci_memory_region *spci_get_donated_memory_region(
Andrew Walbran70bc8622019-10-07 14:15:58 +0100270 void *message)
Jose Marinho75509b42019-04-09 09:34:59 +0100271{
272 struct spci_architected_message_header *architected_header =
Andrew Walbran70bc8622019-10-07 14:15:58 +0100273 (struct spci_architected_message_header *)message;
Jose Marinho75509b42019-04-09 09:34:59 +0100274 return (struct spci_memory_region *)architected_header->payload;
275}
276
277/**
Jose Marinho713f13a2019-05-21 11:54:16 +0100278 * Helper function that copies the memory constituents and the handle
279 * information onto the address pointed to by memory_region.
280 * The function returns the length in bytes occupied by the data copied to
281 * memory_region (constituents and memory region header size).
Jose Marinho75509b42019-04-09 09:34:59 +0100282 */
Jose Marinho713f13a2019-05-21 11:54:16 +0100283static inline uint32_t spci_memory_region_add(
284 struct spci_memory_region *memory_region, spci_memory_handle_t handle,
Jose Marinho75509b42019-04-09 09:34:59 +0100285 const struct spci_memory_region_constituent constituents[],
286 uint32_t num_constituents)
287{
Jose Marinho75509b42019-04-09 09:34:59 +0100288 uint32_t constituents_length =
289 num_constituents *
290 sizeof(struct spci_memory_region_constituent);
291 uint32_t index;
292
293 memory_region->handle = handle;
294 memory_region->count = num_constituents;
295
296 for (index = 0; index < num_constituents; index++) {
297 memory_region->constituents[index] = constituents[index];
298 memory_region->constituents[index].reserved = 0;
299 }
300
301 /*
302 * TODO: Add assert ensuring that the specified message
303 * length is not greater than SPCI_MSG_PAYLOAD_MAX.
304 */
Jose Marinho713f13a2019-05-21 11:54:16 +0100305
306 return sizeof(struct spci_memory_region) + constituents_length;
Jose Marinho75509b42019-04-09 09:34:59 +0100307}
308
309/** Construct the SPCI donate memory region message. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100310static inline uint32_t spci_memory_donate_init(
311 void *message,
Jose Marinho75509b42019-04-09 09:34:59 +0100312 struct spci_memory_region_constituent *region_constituents,
313 uint32_t num_elements, uint32_t handle)
314{
Andrew Walbran70bc8622019-10-07 14:15:58 +0100315 uint32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100316 struct spci_memory_region *memory_region =
317 spci_get_donated_memory_region(message);
Jose Marinho75509b42019-04-09 09:34:59 +0100318
319 message_length = sizeof(struct spci_architected_message_header);
320
321 /* Fill in the details on the common message header. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100322 spci_architected_message_init(message, SPCI_MEMORY_DONATE);
Jose Marinho75509b42019-04-09 09:34:59 +0100323
324 /* Create single memory region. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100325 message_length += spci_memory_region_add(
Jose Marinho713f13a2019-05-21 11:54:16 +0100326 memory_region, handle, region_constituents, num_elements);
Andrew Walbran70bc8622019-10-07 14:15:58 +0100327 return message_length;
Jose Marinho75509b42019-04-09 09:34:59 +0100328}
Jose Marinho56c25732019-05-20 09:48:53 +0100329
330/**
331 * Construct the SPCI memory region relinquish message.
332 * A set of memory regions can be given back to the owner.
333 */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100334static inline uint32_t spci_memory_relinquish_init(
335 void *message,
Jose Marinho56c25732019-05-20 09:48:53 +0100336 struct spci_memory_region_constituent *region_constituents,
337 uint64_t num_elements, uint32_t handle)
338{
Andrew Walbran70bc8622019-10-07 14:15:58 +0100339 uint32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100340 struct spci_memory_region *memory_region =
341 spci_get_donated_memory_region(message);
Jose Marinho56c25732019-05-20 09:48:53 +0100342
343 message_length = sizeof(struct spci_architected_message_header);
344
345 /* Fill in the details on the common message header. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100346 spci_architected_message_init(message, SPCI_MEMORY_RELINQUISH);
Jose Marinho56c25732019-05-20 09:48:53 +0100347
348 /* Create single memory region. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100349 message_length += spci_memory_region_add(
Jose Marinho713f13a2019-05-21 11:54:16 +0100350 memory_region, handle, region_constituents, num_elements);
Andrew Walbran70bc8622019-10-07 14:15:58 +0100351 return message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100352}
353
354/**
355 * Construct the SPCI memory region lend message.
356 */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100357static inline uint32_t spci_memory_lend_init(
358 void *message,
Jose Marinho713f13a2019-05-21 11:54:16 +0100359 struct spci_memory_region_constituent *region_constituents,
360 uint64_t num_elements, uint32_t handle, enum spci_lend_access access,
361 enum spci_lend_type type, enum spci_lend_cacheability cacheability,
362 enum spci_lend_shareability shareability)
363{
Andrew Walbran70bc8622019-10-07 14:15:58 +0100364 uint32_t message_length;
Jose Marinho713f13a2019-05-21 11:54:16 +0100365 struct spci_memory_region *memory_region;
366
367 const struct spci_memory_lend lend_init = {0};
368
369 struct spci_memory_lend *lend_descriptor =
370 spci_get_lend_descriptor(message);
371 memory_region = (struct spci_memory_region *)lend_descriptor->payload;
372
373 /* Initilise all struct elements to zero. */
374 *lend_descriptor = lend_init;
375
376 message_length = sizeof(struct spci_architected_message_header) +
377 sizeof(struct spci_memory_lend);
378
379 /* Fill in the details on the common message header. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100380 spci_architected_message_init(message, SPCI_MEMORY_LEND);
Jose Marinho713f13a2019-05-21 11:54:16 +0100381
382 lend_descriptor->flags = SPCI_LEND_KEEP_MAPPED;
383
384 /* Set memory region's page attributes. */
385 spci_set_lend_access_attr(&lend_descriptor->borrower_attributes,
386 access);
387 spci_set_lend_type_attr(&lend_descriptor->borrower_attributes, type);
388 spci_set_lend_cacheability_attr(&lend_descriptor->borrower_attributes,
389 cacheability);
390 spci_set_lend_shareability_attr(&lend_descriptor->borrower_attributes,
391 shareability);
392
393 /* Create single memory region. */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100394 message_length += spci_memory_region_add(
Jose Marinho713f13a2019-05-21 11:54:16 +0100395 memory_region, handle, region_constituents, num_elements);
Andrew Walbran70bc8622019-10-07 14:15:58 +0100396 return message_length;
Jose Marinho56c25732019-05-20 09:48:53 +0100397}