blob: 52396aac3f3b16e4f7e5e57575cc8758d49b938e [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
Andrew Walbran692b3252019-03-07 15:51:31 +00002 * Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01003 *
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
Andrew Scullfbc938a2018-08-20 14:09:28 +010017#pragma once
Andrew Scullf35a5c92018-08-07 18:09:46 +010018
Andrew Scull6d2db332018-10-10 15:28:17 +010019#include "hf/abi.h"
Jose Marinhoa1dfeda2019-02-27 16:46:03 +000020#include "hf/spci.h"
Andrew Scull6d2db332018-10-10 15:28:17 +010021#include "hf/types.h"
Andrew Scullf35a5c92018-08-07 18:09:46 +010022
23/* Keep macro alignment */
24/* clang-format off */
25
Andrew Scullf35a5c92018-08-07 18:09:46 +010026/* TODO: Define constants below according to spec. */
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000027#define HF_VM_GET_COUNT 0xff01
28#define HF_VCPU_GET_COUNT 0xff02
29#define HF_VCPU_RUN 0xff03
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000030#define HF_VM_CONFIGURE 0xff05
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +000031#define HF_MAILBOX_CLEAR 0xff08
32#define HF_MAILBOX_WRITABLE_GET 0xff09
33#define HF_MAILBOX_WAITER_GET 0xff0a
34#define HF_INTERRUPT_ENABLE 0xff0b
35#define HF_INTERRUPT_GET 0xff0c
36#define HF_INTERRUPT_INJECT 0xff0d
Andrew Scull6386f252018-12-06 13:29:10 +000037#define HF_SHARE_MEMORY 0xff0e
Andrew Scullf35a5c92018-08-07 18:09:46 +010038
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +010039/* This matches what Trusty and its ATF module currently use. */
40#define HF_DEBUG_LOG 0xbd000000
41
Andrew Scullf35a5c92018-08-07 18:09:46 +010042/* clang-format on */
43
Andrew Scull5ac05f02018-08-10 17:23:22 +010044/**
45 * This function must be implemented to trigger the architecture specific
46 * mechanism to call to the hypervisor.
Andrew Scullf35a5c92018-08-07 18:09:46 +010047 */
Andrew Walbran4e6bcc72019-09-11 13:57:22 +010048int64_t hf_call(uint64_t arg0, uint64_t arg1, uint64_t arg2, uint64_t arg3);
Andrew Walbran7f920af2019-09-03 17:09:30 +010049struct spci_value spci_call(struct spci_value args);
Andrew Scullf35a5c92018-08-07 18:09:46 +010050
Andrew Scull5ac05f02018-08-10 17:23:22 +010051/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +000052 * Returns the VM's own ID.
Andrew Scull5ac05f02018-08-10 17:23:22 +010053 */
Andrew Walbrand230f662019-10-07 18:03:36 +010054static inline struct spci_value spci_id_get(void)
55{
56 return spci_call((struct spci_value){.func = SPCI_ID_GET_32});
57}
58
59/**
60 * Returns the VM's own ID.
61 */
Andrew Walbran95534922019-06-19 11:32:54 +010062static inline spci_vm_id_t hf_vm_get_id(void)
Andrew Scull5ac05f02018-08-10 17:23:22 +010063{
Andrew Walbrand230f662019-10-07 18:03:36 +010064 return spci_id_get().arg2;
Andrew Scull5ac05f02018-08-10 17:23:22 +010065}
66
67/**
Andrew Scullf35a5c92018-08-07 18:09:46 +010068 * Returns the number of secondary VMs.
69 */
Andrew Walbran52d99672019-06-25 15:51:11 +010070static inline spci_vm_count_t hf_vm_get_count(void)
Andrew Scullf35a5c92018-08-07 18:09:46 +010071{
72 return hf_call(HF_VM_GET_COUNT, 0, 0, 0);
73}
74
Andrew Scull5ac05f02018-08-10 17:23:22 +010075/**
Andrew Scullf35a5c92018-08-07 18:09:46 +010076 * Returns the number of VCPUs configured in the given secondary VM.
77 */
Andrew Walbranc6d23c42019-06-26 13:30:42 +010078static inline spci_vcpu_count_t hf_vcpu_get_count(spci_vm_id_t vm_id)
Andrew Scullf35a5c92018-08-07 18:09:46 +010079{
Andrew Scull19503262018-09-20 14:48:39 +010080 return hf_call(HF_VCPU_GET_COUNT, vm_id, 0, 0);
Andrew Scullf35a5c92018-08-07 18:09:46 +010081}
82
Andrew Scull5ac05f02018-08-10 17:23:22 +010083/**
Andrew Scull55c4d8b2018-12-18 18:50:18 +000084 * Runs the given vcpu of the given vm.
85 *
86 * Returns an hf_vcpu_run_return struct telling the scheduler what to do next.
87 */
Andrew Walbran95534922019-06-19 11:32:54 +010088static inline struct hf_vcpu_run_return hf_vcpu_run(spci_vm_id_t vm_id,
Andrew Walbranb037d5b2019-06-25 17:19:41 +010089 spci_vcpu_index_t vcpu_idx)
Andrew Scull55c4d8b2018-12-18 18:50:18 +000090{
91 return hf_vcpu_run_return_decode(
92 hf_call(HF_VCPU_RUN, vm_id, vcpu_idx, 0));
93}
94
95/**
96 * Hints that the vcpu is willing to yield its current use of the physical CPU.
Jose Marinho135dff32019-02-28 10:25:57 +000097 * This call always returns SPCI_SUCCESS.
Andrew Scull55c4d8b2018-12-18 18:50:18 +000098 */
Andrew Walbran16075b62019-09-03 17:11:07 +010099static inline struct spci_value spci_yield(void)
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000100{
Andrew Walbran16075b62019-09-03 17:11:07 +0100101 return spci_call((struct spci_value){.func = SPCI_YIELD_32});
Andrew Scull55c4d8b2018-12-18 18:50:18 +0000102}
103
104/**
Andrew Scull5ac05f02018-08-10 17:23:22 +0100105 * Configures the pages to send/receive data through. The pages must not be
106 * shared.
Andrew Walbran54afb502018-11-26 16:01:11 +0000107 *
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000108 * Returns:
109 * - -1 on failure.
110 * - 0 on success if no further action is needed.
111 * - 1 if it was called by the primary VM and the primary VM now needs to wake
112 * up or kick waiters.
Andrew Scull5ac05f02018-08-10 17:23:22 +0100113 */
Andrew Scullc0e569a2018-10-02 18:05:21 +0100114static inline int64_t hf_vm_configure(hf_ipaddr_t send, hf_ipaddr_t recv)
Andrew Scull5ac05f02018-08-10 17:23:22 +0100115{
116 return hf_call(HF_VM_CONFIGURE, send, recv, 0);
117}
118
119/**
Andrew Scullaa039b32018-10-04 15:02:26 +0100120 * Copies data from the sender's send buffer to the recipient's receive buffer.
Andrew Walbran54afb502018-11-26 16:01:11 +0000121 *
Wedson Almeida Filho17c997f2019-01-09 18:50:09 +0000122 * If the recipient's receive buffer is busy, it can optionally register the
123 * caller to be notified when the recipient's receive buffer becomes available.
124 *
Andrew Walbran70bc8622019-10-07 14:15:58 +0100125 * Attributes may include:
126 * - SPCI_MSG_SEND_NOTIFY, to notify the caller when it should try again.
127 * - SPCI_MSG_SEND_LEGACY_MEMORY, to send a legacy architected memory sharing
128 * message.
129 *
130 * Returns SPCI_SUCCESS if the message is sent, or an error code otherwise:
131 * - INVALID_PARAMETERS: one or more of the parameters do not conform.
Jose Marinhoa1dfeda2019-02-27 16:46:03 +0000132 * - BUSY: the message could not be delivered either because the mailbox
Andrew Walbran70bc8622019-10-07 14:15:58 +0100133 * was full or the target VM is not yet set up.
Andrew Scull5ac05f02018-08-10 17:23:22 +0100134 */
Andrew Walbran70bc8622019-10-07 14:15:58 +0100135static inline struct spci_value spci_msg_send(spci_vm_id_t sender_vm_id,
136 spci_vm_id_t target_vm_id,
137 uint32_t size,
138 uint32_t attributes)
Andrew Scull5ac05f02018-08-10 17:23:22 +0100139{
Andrew Walbran70bc8622019-10-07 14:15:58 +0100140 return spci_call((struct spci_value){
141 .func = SPCI_MSG_SEND_32,
142 .arg1 = ((uint64_t)sender_vm_id << 16) | target_vm_id,
143 .arg3 = size,
144 .arg4 = attributes});
Andrew Scull5ac05f02018-08-10 17:23:22 +0100145}
146
147/**
Andrew Walbran0de4f162019-09-03 16:44:20 +0100148 * Called by secondary VMs to receive a message. This will block until a message
149 * is received.
Andrew Scull5ac05f02018-08-10 17:23:22 +0100150 *
Andrew Scullaa039b32018-10-04 15:02:26 +0100151 * The mailbox must be cleared before a new message can be received.
Andrew Walbran9311c9a2019-03-12 16:59:04 +0000152 *
Andrew Walbran0de4f162019-09-03 16:44:20 +0100153 * If no message is immediately available and there are no enabled and pending
154 * interrupts (irrespective of whether interrupts are enabled globally), then
155 * this will block until a message is available or an enabled interrupt becomes
156 * pending. This matches the behaviour of the WFI instruction on aarch64, except
157 * that a message becoming available is also treated like a wake-up event.
Andrew Walbranc8500812019-06-26 10:36:48 +0100158 *
159 * Returns:
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100160 * - SPCI_MSG_SEND if a message is successfully received.
161 * - SPCI_ERROR SPCI_NOT_SUPPORTED if called from the primary VM.
162 * - SPCI_ERROR SPCI_INTERRUPTED if an interrupt happened during the call.
Andrew Scull5ac05f02018-08-10 17:23:22 +0100163 */
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100164static inline struct spci_value spci_msg_wait(void)
Andrew Scull5ac05f02018-08-10 17:23:22 +0100165{
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100166 return spci_call((struct spci_value){.func = SPCI_MSG_WAIT_32});
Andrew Walbran0de4f162019-09-03 16:44:20 +0100167}
168
169/**
170 * Called by secondary VMs to receive a message. The call will return whether or
171 * not a message is available.
172 *
173 * The mailbox must be cleared before a new message can be received.
174 *
175 * Returns:
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100176 * - SPCI_MSG_SEND if a message is successfully received.
177 * - SPCI_ERROR SPCI_NOT_SUPPORTED if called from the primary VM.
178 * - SPCI_ERROR SPCI_INTERRUPTED if an interrupt happened during the call.
179 * - SPCI_ERROR SPCI_RETRY if there was no pending message.
Andrew Walbran0de4f162019-09-03 16:44:20 +0100180 */
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100181static inline struct spci_value spci_msg_poll(void)
Andrew Walbran0de4f162019-09-03 16:44:20 +0100182{
Andrew Walbrand4d2fa12019-10-01 16:47:25 +0100183 return spci_call((struct spci_value){.func = SPCI_MSG_POLL_32});
Andrew Scull5ac05f02018-08-10 17:23:22 +0100184}
185
186/**
Andrew Walbran54afb502018-11-26 16:01:11 +0000187 * Clears the caller's mailbox so a new message can be received.
188 *
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000189 * Returns:
Andrew Scullaa7db8e2019-02-01 14:12:19 +0000190 * - -1 on failure, if the mailbox hasn't been read.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000191 * - 0 on success if no further action is needed.
192 * - 1 if it was called by the primary VM and the primary VM now needs to wake
193 * up or kick waiters. Waiters should be retrieved by calling
194 * hf_mailbox_waiter_get.
Andrew Scull5ac05f02018-08-10 17:23:22 +0100195 */
Andrew Scullaa039b32018-10-04 15:02:26 +0100196static inline int64_t hf_mailbox_clear(void)
Andrew Scull5ac05f02018-08-10 17:23:22 +0100197{
Andrew Scullaa039b32018-10-04 15:02:26 +0100198 return hf_call(HF_MAILBOX_CLEAR, 0, 0, 0);
Andrew Scull5ac05f02018-08-10 17:23:22 +0100199}
Andrew Walbran318f5732018-11-20 16:23:42 +0000200
201/**
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000202 * Retrieves the next VM whose mailbox became writable. For a VM to be notified
203 * by this function, the caller must have called api_mailbox_send before with
204 * the notify argument set to true, and this call must have failed because the
205 * mailbox was not available.
206 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000207 * It should be called repeatedly to retrieve a list of VMs.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000208 *
209 * Returns -1 if no VM became writable, or the id of the VM whose mailbox
210 * became writable.
211 */
212static inline int64_t hf_mailbox_writable_get(void)
213{
214 return hf_call(HF_MAILBOX_WRITABLE_GET, 0, 0, 0);
215}
216
217/**
218 * Retrieves the next VM waiting to be notified that the mailbox of the
219 * specified VM became writable. Only primary VMs are allowed to call this.
220 *
Wedson Almeida Filhob790f652019-01-22 23:41:56 +0000221 * Returns -1 on failure or if there are no waiters; the VM id of the next
222 * waiter otherwise.
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000223 */
Andrew Walbran95534922019-06-19 11:32:54 +0100224static inline int64_t hf_mailbox_waiter_get(spci_vm_id_t vm_id)
Wedson Almeida Filhoea62e2e2019-01-09 19:14:59 +0000225{
226 return hf_call(HF_MAILBOX_WAITER_GET, vm_id, 0, 0);
227}
228
229/**
Andrew Walbran318f5732018-11-20 16:23:42 +0000230 * Enables or disables a given interrupt ID.
231 *
232 * Returns 0 on success, or -1 if the intid is invalid.
233 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000234static inline int64_t hf_interrupt_enable(uint32_t intid, bool enable)
Andrew Walbran318f5732018-11-20 16:23:42 +0000235{
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000236 return hf_call(HF_INTERRUPT_ENABLE, intid, enable, 0);
Andrew Walbran318f5732018-11-20 16:23:42 +0000237}
238
239/**
240 * Gets the ID of the pending interrupt (if any) and acknowledge it.
241 *
242 * Returns HF_INVALID_INTID if there are no pending interrupts.
243 */
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000244static inline uint32_t hf_interrupt_get(void)
Andrew Walbran318f5732018-11-20 16:23:42 +0000245{
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000246 return hf_call(HF_INTERRUPT_GET, 0, 0, 0);
Andrew Walbran318f5732018-11-20 16:23:42 +0000247}
248
249/**
250 * Injects a virtual interrupt of the given ID into the given target vCPU.
251 * This doesn't cause the vCPU to actually be run immediately; it will be taken
252 * when the vCPU is next run, which is up to the scheduler.
253 *
Andrew Walbran3d84a262018-12-13 14:41:19 +0000254 * Returns:
255 * - -1 on failure because the target VM or vCPU doesn't exist, the interrupt
256 * ID is invalid, or the current VM is not allowed to inject interrupts to
257 * the target VM.
258 * - 0 on success if no further action is needed.
259 * - 1 if it was called by the primary VM and the primary VM now needs to wake
260 * up or kick the target vCPU.
Andrew Walbran318f5732018-11-20 16:23:42 +0000261 */
Andrew Walbran95534922019-06-19 11:32:54 +0100262static inline int64_t hf_interrupt_inject(spci_vm_id_t target_vm_id,
Andrew Walbranb037d5b2019-06-25 17:19:41 +0100263 spci_vcpu_index_t target_vcpu_idx,
Andrew Walbran318f5732018-11-20 16:23:42 +0000264 uint32_t intid)
265{
Wedson Almeida Filhoc559d132019-01-09 19:33:40 +0000266 return hf_call(HF_INTERRUPT_INJECT, target_vm_id, target_vcpu_idx,
Andrew Walbran318f5732018-11-20 16:23:42 +0000267 intid);
268}
Andrew Scull6386f252018-12-06 13:29:10 +0000269
270/**
271 * Shares a region of memory with another VM.
272 *
273 * Returns 0 on success or -1 if the sharing was not allowed or failed.
274 *
275 * TODO: replace this with a better API once we have decided what that should
276 * look like.
277 */
Andrew Walbran95534922019-06-19 11:32:54 +0100278static inline int64_t hf_share_memory(spci_vm_id_t vm_id, hf_ipaddr_t addr,
Andrew Scull6386f252018-12-06 13:29:10 +0000279 size_t size, enum hf_share share)
280{
281 return hf_call(HF_SHARE_MEMORY, (((uint64_t)vm_id) << 32) | share, addr,
282 size);
283}
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100284
Andrew Walbranc1ad4ce2019-05-09 11:41:39 +0100285/**
286 * Sends a character to the debug log for the VM.
287 *
288 * Returns 0 on success, or -1 if it failed for some reason.
289 */
290static inline int64_t hf_debug_log(char c)
291{
292 return hf_call(HF_DEBUG_LOG, c, 0, 0);
293}
294
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100295/** Obtains the Hafnium's version of the implemented SPCI specification. */
Andrew Walbran7f920af2019-09-03 17:09:30 +0100296static inline struct spci_value spci_version(void)
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100297{
Andrew Walbran7f920af2019-09-03 17:09:30 +0100298 return spci_call((struct spci_value){.func = SPCI_VERSION_32});
Jose Marinhofc0b2b62019-06-06 11:18:45 +0100299}
Jose Marinhoc0f4ff22019-10-09 10:37:42 +0100300
301/**
302 * Discovery function returning information about the implementation of optional
303 * SPCI interfaces.
304 *
305 * Returns:
306 * - SPCI_SUCCESS in .func if the the optional interface with function_id is
307 * implemented.
308 * - SPCI_ERROR in .func if the optional interface with function_id is not
309 * implemented.
310 */
311static inline struct spci_value spci_features(uint32_t function_id)
312{
313 return spci_call((struct spci_value){.func = SPCI_FEATURES_32,
314 .arg1 = function_id});
315}