blob: 21951133020640e123583644a8b0212aa1feb1e6 [file] [log] [blame]
Andrew Scullfbc938a2018-08-20 14:09:28 +01001#pragma once
Andrew Scullf35a5c92018-08-07 18:09:46 +01002
Andrew Scull5ac05f02018-08-10 17:23:22 +01003#if defined(__linux__) && defined(__KERNEL__)
4
5#include <linux/types.h>
6
7typedef phys_addr_t hf_ipaddr_t;
8
9#else
10
11#include <stdbool.h>
Andrew Scullf35a5c92018-08-07 18:09:46 +010012#include <stddef.h>
Andrew Scull5ac05f02018-08-10 17:23:22 +010013#include <stdint.h>
14
15typedef uintptr_t hf_ipaddr_t;
16
17#endif
Andrew Scullf35a5c92018-08-07 18:09:46 +010018
19/* Keep macro alignment */
20/* clang-format off */
21
Andrew Scullf35a5c92018-08-07 18:09:46 +010022/* TODO: Define constants below according to spec. */
23#define HF_VCPU_RUN 0xff00
24#define HF_VM_GET_COUNT 0xff01
25#define HF_VCPU_GET_COUNT 0xff02
26#define HF_VM_CONFIGURE 0xff03
27#define HF_RPC_REQUEST 0xff04
28#define HF_RPC_READ_REQUEST 0xff05
29#define HF_RPC_ACK 0xff06
30#define HF_RPC_REPLY 0xff07
31
Andrew Scull13652af2018-09-17 14:49:08 +010032/* Return codes for hf_vcpu_run(). */
33#define HF_VCPU_RUN_YIELD 0x00
34#define HF_VCPU_RUN_WAIT_FOR_INTERRUPT 0x01
35#define HF_VCPU_RUN_WAKE_UP 0x02
36#define HF_VCPU_RUN_RESPONSE_READY 0x03
37
38/* Construct and destruct the hf_vcpu_run() response. */
39#define HF_VCPU_RUN_RESPONSE(code, data) ((code & 0xff) | (data << 8))
40#define HF_VCPU_RUN_CODE(ret) (ret & 0xff)
41#define HF_VCPU_RUN_DATA(ret) (ret >> 8)
42
43#define HF_RPC_REQUEST_MAX_SIZE 4096
44
Andrew Scullf35a5c92018-08-07 18:09:46 +010045/* clang-format on */
46
Andrew Scull5ac05f02018-08-10 17:23:22 +010047/**
48 * This function must be implemented to trigger the architecture specific
49 * mechanism to call to the hypervisor.
Andrew Scullf35a5c92018-08-07 18:09:46 +010050 */
Andrew Scull5ac05f02018-08-10 17:23:22 +010051size_t hf_call(size_t arg0, size_t arg1, size_t arg2, size_t arg3);
Andrew Scullf35a5c92018-08-07 18:09:46 +010052
Andrew Scull5ac05f02018-08-10 17:23:22 +010053/**
54 * Runs the given vcpu of the given vm.
55 */
56static inline int32_t hf_vcpu_run(uint32_t vm_idx, uint32_t vcpu_idx)
57{
58 return hf_call(HF_VCPU_RUN, vm_idx, vcpu_idx, 0);
59}
60
61/**
Andrew Scullf35a5c92018-08-07 18:09:46 +010062 * Returns the number of secondary VMs.
63 */
Andrew Scull5ac05f02018-08-10 17:23:22 +010064static inline int32_t hf_vm_get_count(void)
Andrew Scullf35a5c92018-08-07 18:09:46 +010065{
66 return hf_call(HF_VM_GET_COUNT, 0, 0, 0);
67}
68
Andrew Scull5ac05f02018-08-10 17:23:22 +010069/**
Andrew Scullf35a5c92018-08-07 18:09:46 +010070 * Returns the number of VCPUs configured in the given secondary VM.
71 */
Andrew Scull5ac05f02018-08-10 17:23:22 +010072static inline int32_t hf_vcpu_get_count(uint32_t vm_idx)
Andrew Scullf35a5c92018-08-07 18:09:46 +010073{
74 return hf_call(HF_VCPU_GET_COUNT, vm_idx, 0, 0);
75}
76
Andrew Scull5ac05f02018-08-10 17:23:22 +010077/**
78 * Configures the pages to send/receive data through. The pages must not be
79 * shared.
80 */
81static inline int32_t hf_vm_configure(hf_ipaddr_t send, hf_ipaddr_t recv)
82{
83 return hf_call(HF_VM_CONFIGURE, send, recv, 0);
84}
85
86/**
87 * Called by the primary VM to send an RPC request to a secondary VM. Data is
88 * copied from the caller's send buffer to the destination's receive buffer.
89 */
90static inline int32_t hf_rpc_request(uint32_t vm_idx, size_t size)
91{
92 return hf_call(HF_RPC_REQUEST, vm_idx, size, 0);
93}
94
95/**
96 * Called by the primary VM to read a request sent from a previous call to
97 * api_rpc_request. If one isn't available, this function can optionally block
98 * the caller until one becomes available.
99 *
100 * Once the caller has completed handling a request, it must indicate it by
101 * either calling api_rpc_reply or api_rpc_ack. No new requests can be accepted
102 * until the current one is acknowledged.
103 */
104static inline int32_t hf_rpc_read_request(bool block)
105{
106 return hf_call(HF_RPC_READ_REQUEST, block, 0, 0);
107}
108
109/**
110 * Acknowledges that either a request or a reply has been received and handled.
111 * After this call completes, the caller will be able to receive additional
112 * requests or replies.
113 */
114static inline int32_t hf_rpc_ack(void)
115{
116 return hf_call(HF_RPC_ACK, 0, 0, 0);
117}
118
119/**
120 * Called by a secondary VM to send a reply to the primary VM. Data is copied
121 * from the caller's send buffer to the destination's receive buffer.
122 *
123 * It can optionally acknowledge the pending request.
124 */
125static inline int32_t hf_rpc_reply(size_t size, bool ack)
126{
127 return hf_call(HF_RPC_REPLY, size, ack, 0);
128}