blob: 0650868aaffd05113e3902f04826ce63a664d277 [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 Scull19503262018-09-20 14:48:39 +010032/* The ID of the primary VM which is responsile for scheduling. */
33#define HF_PRIMARY_VM_ID 0
34
Andrew Scull13652af2018-09-17 14:49:08 +010035/* Return codes for hf_vcpu_run(). */
36#define HF_VCPU_RUN_YIELD 0x00
37#define HF_VCPU_RUN_WAIT_FOR_INTERRUPT 0x01
38#define HF_VCPU_RUN_WAKE_UP 0x02
39#define HF_VCPU_RUN_RESPONSE_READY 0x03
40
41/* Construct and destruct the hf_vcpu_run() response. */
42#define HF_VCPU_RUN_RESPONSE(code, data) ((code & 0xff) | (data << 8))
43#define HF_VCPU_RUN_CODE(ret) (ret & 0xff)
44#define HF_VCPU_RUN_DATA(ret) (ret >> 8)
45
46#define HF_RPC_REQUEST_MAX_SIZE 4096
47
Andrew Scullf35a5c92018-08-07 18:09:46 +010048/* clang-format on */
49
Andrew Scull5ac05f02018-08-10 17:23:22 +010050/**
51 * This function must be implemented to trigger the architecture specific
52 * mechanism to call to the hypervisor.
Andrew Scullf35a5c92018-08-07 18:09:46 +010053 */
Andrew Scullc0e569a2018-10-02 18:05:21 +010054int64_t hf_call(size_t arg0, size_t arg1, size_t arg2, size_t arg3);
Andrew Scullf35a5c92018-08-07 18:09:46 +010055
Andrew Scull5ac05f02018-08-10 17:23:22 +010056/**
57 * Runs the given vcpu of the given vm.
58 */
Andrew Scullc0e569a2018-10-02 18:05:21 +010059static inline int64_t hf_vcpu_run(uint32_t vm_id, uint32_t vcpu_idx)
Andrew Scull5ac05f02018-08-10 17:23:22 +010060{
Andrew Scull19503262018-09-20 14:48:39 +010061 return hf_call(HF_VCPU_RUN, vm_id, vcpu_idx, 0);
Andrew Scull5ac05f02018-08-10 17:23:22 +010062}
63
64/**
Andrew Scullf35a5c92018-08-07 18:09:46 +010065 * Returns the number of secondary VMs.
66 */
Andrew Scullc0e569a2018-10-02 18:05:21 +010067static inline int64_t hf_vm_get_count(void)
Andrew Scullf35a5c92018-08-07 18:09:46 +010068{
69 return hf_call(HF_VM_GET_COUNT, 0, 0, 0);
70}
71
Andrew Scull5ac05f02018-08-10 17:23:22 +010072/**
Andrew Scullf35a5c92018-08-07 18:09:46 +010073 * Returns the number of VCPUs configured in the given secondary VM.
74 */
Andrew Scullc0e569a2018-10-02 18:05:21 +010075static inline int64_t hf_vcpu_get_count(uint32_t vm_id)
Andrew Scullf35a5c92018-08-07 18:09:46 +010076{
Andrew Scull19503262018-09-20 14:48:39 +010077 return hf_call(HF_VCPU_GET_COUNT, vm_id, 0, 0);
Andrew Scullf35a5c92018-08-07 18:09:46 +010078}
79
Andrew Scull5ac05f02018-08-10 17:23:22 +010080/**
81 * Configures the pages to send/receive data through. The pages must not be
82 * shared.
83 */
Andrew Scullc0e569a2018-10-02 18:05:21 +010084static inline int64_t hf_vm_configure(hf_ipaddr_t send, hf_ipaddr_t recv)
Andrew Scull5ac05f02018-08-10 17:23:22 +010085{
86 return hf_call(HF_VM_CONFIGURE, send, recv, 0);
87}
88
89/**
90 * Called by the primary VM to send an RPC request to a secondary VM. Data is
91 * copied from the caller's send buffer to the destination's receive buffer.
92 */
Andrew Scullc0e569a2018-10-02 18:05:21 +010093static inline int64_t hf_rpc_request(uint32_t vm_id, size_t size)
Andrew Scull5ac05f02018-08-10 17:23:22 +010094{
Andrew Scull19503262018-09-20 14:48:39 +010095 return hf_call(HF_RPC_REQUEST, vm_id, size, 0);
Andrew Scull5ac05f02018-08-10 17:23:22 +010096}
97
98/**
99 * Called by the primary VM to read a request sent from a previous call to
100 * api_rpc_request. If one isn't available, this function can optionally block
101 * the caller until one becomes available.
102 *
103 * Once the caller has completed handling a request, it must indicate it by
104 * either calling api_rpc_reply or api_rpc_ack. No new requests can be accepted
105 * until the current one is acknowledged.
106 */
Andrew Scullc0e569a2018-10-02 18:05:21 +0100107static inline int64_t hf_rpc_read_request(bool block)
Andrew Scull5ac05f02018-08-10 17:23:22 +0100108{
109 return hf_call(HF_RPC_READ_REQUEST, block, 0, 0);
110}
111
112/**
113 * Acknowledges that either a request or a reply has been received and handled.
114 * After this call completes, the caller will be able to receive additional
115 * requests or replies.
116 */
Andrew Scullc0e569a2018-10-02 18:05:21 +0100117static inline int64_t hf_rpc_ack(void)
Andrew Scull5ac05f02018-08-10 17:23:22 +0100118{
119 return hf_call(HF_RPC_ACK, 0, 0, 0);
120}
121
122/**
123 * Called by a secondary VM to send a reply to the primary VM. Data is copied
124 * from the caller's send buffer to the destination's receive buffer.
125 *
126 * It can optionally acknowledge the pending request.
127 */
Andrew Scullc0e569a2018-10-02 18:05:21 +0100128static inline int64_t hf_rpc_reply(size_t size, bool ack)
Andrew Scull5ac05f02018-08-10 17:23:22 +0100129{
130 return hf_call(HF_RPC_REPLY, size, ack, 0);
131}