1 /*-
2  * Copyright (c) 2011 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef _VMMAPI_H_
30 #define	_VMMAPI_H_
31 
32 #include <sys/param.h>
33 #include "types.h"
34 #include "macros.h"
35 #include "pm.h"
36 #include "hsm_ioctl_defs.h"
37 
38 /*
39  * API version for out-of-tree consumers for making compile time decisions.
40  */
41 #define	VMMAPI_VERSION	0103	/* 2 digit major followed by 2 digit minor */
42 
43 #define ALIGN_UP(x, align)	(((x) + ((align)-1)) & ~((align)-1))
44 #define ALIGN_DOWN(x, align)	((x) & ~((align)-1))
45 
46 #define CMOS_BUF_SIZE		256
47 
48 struct vmctx {
49 	int     fd;
50 	int     vmid;
51 	int     ioreq_client;
52 	uint32_t lowmem_limit;
53 	uint64_t highmem_gpa_base;
54 	size_t  lowmem;
55 	size_t  fbmem;
56 	size_t  biosmem;
57 	size_t  highmem;
58 	char    *baseaddr;
59 	char    *name;
60 
61 	/* fields to track virtual devices */
62 	void *atkbdc_base;
63 	void *vrtc;
64 	void *vpit;
65 	void *ioc_dev;
66 	void *tpm_dev;
67 	void *fb_base;
68 
69 	/* BSP state. guest loader needs to fill it */
70 	struct acrn_vcpu_regs bsp_regs;
71 
72 	/* if gvt-g is enabled for current VM */
73 	bool gvt_enabled;
74 
75 	void (*update_gvt_bar)(struct vmctx *ctx);
76 };
77 
78 #define	PROT_RW		(PROT_READ | PROT_WRITE)
79 #define	PROT_ALL	(PROT_READ | PROT_WRITE | PROT_EXEC)
80 
81 struct vm_lapic_msi {
82 	uint64_t	msg;
83 	uint64_t	addr;
84 };
85 
86 struct vm_isa_irq {
87 	int		atpic_irq;
88 	int		ioapic_irq;
89 };
90 
91 struct vm_mem_region {
92 	uint64_t fd_offset;
93 	int fd;
94 };
95 bool	vm_find_memfd_region(struct vmctx *ctx, vm_paddr_t gpa,
96 			     struct vm_mem_region *ret_region);
97 bool    vm_allow_dmabuf(struct vmctx *ctx);
98 /*
99  * Create a device memory segment identified by 'segid'.
100  *
101  * Returns a pointer to the memory segment on success and MAP_FAILED otherwise.
102  */
103 struct	vmctx *vm_create(const char *name, uint64_t req_buf, int* vcpu_num);
104 void	vm_pause(struct vmctx *ctx);
105 void	vm_reset(struct vmctx *ctx);
106 int	vm_create_ioreq_client(struct vmctx *ctx);
107 int	vm_destroy_ioreq_client(struct vmctx *ctx);
108 int	vm_attach_ioreq_client(struct vmctx *ctx);
109 int	vm_notify_request_done(struct vmctx *ctx, int vcpu);
110 int	vm_setup_asyncio(struct vmctx *ctx, uint64_t base);
111 void	vm_clear_ioreq(struct vmctx *ctx);
112 const char *vm_state_to_str(enum vm_suspend_how idx);
113 void	vm_set_suspend_mode(enum vm_suspend_how how);
114 #ifdef DM_DEBUG
115 void	notify_vmloop_thread(void);
116 #endif
117 int	vm_get_suspend_mode(void);
118 void	vm_destroy(struct vmctx *ctx);
119 int	vm_parse_memsize(const char *optarg, size_t *memsize);
120 int	vm_map_memseg_vma(struct vmctx *ctx, size_t len, vm_paddr_t gpa,
121 	uint64_t vma, int prot);
122 int	vm_setup_memory(struct vmctx *ctx, size_t len);
123 void	vm_unsetup_memory(struct vmctx *ctx);
124 bool	init_hugetlb(void);
125 void	uninit_hugetlb(void);
126 int	hugetlb_setup_memory(struct vmctx *ctx);
127 void	hugetlb_unsetup_memory(struct vmctx *ctx);
128 void	*vm_map_gpa(struct vmctx *ctx, vm_paddr_t gaddr, size_t len);
129 uint32_t vm_get_lowmem_limit(struct vmctx *ctx);
130 size_t	vm_get_lowmem_size(struct vmctx *ctx);
131 size_t	vm_get_highmem_size(struct vmctx *ctx);
132 int	vm_run(struct vmctx *ctx);
133 int	vm_suspend(struct vmctx *ctx, enum vm_suspend_how how);
134 int	vm_lapic_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg);
135 int	vm_set_gsi_irq(struct vmctx *ctx, int gsi, uint32_t operation);
136 int	vm_assign_pcidev(struct vmctx *ctx, struct acrn_pcidev *pcidev);
137 int	vm_deassign_pcidev(struct vmctx *ctx, struct acrn_pcidev *pcidev);
138 int	vm_assign_mmiodev(struct vmctx *ctx, struct acrn_mmiodev *mmiodev);
139 int	vm_deassign_mmiodev(struct vmctx *ctx, struct acrn_mmiodev *mmiodev);
140 int	vm_map_ptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
141 			  vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
142 int	vm_unmap_ptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
143 			  vm_paddr_t gpa, size_t len, vm_paddr_t hpa);
144 int	vm_set_ptdev_intx_info(struct vmctx *ctx, uint16_t virt_bdf,
145 	uint16_t phys_bdf, int virt_pin, int phys_pin, bool pic_pin);
146 int	vm_reset_ptdev_intx_info(struct vmctx *ctx, uint16_t virt_bdf,
147 	uint16_t phys_bdf, int virt_pin, bool pic_pin);
148 int	vm_add_hv_vdev(struct vmctx *ctx, struct acrn_vdev *dev);
149 int	vm_remove_hv_vdev(struct vmctx *ctx, struct acrn_vdev *dev);
150 
151 int	acrn_parse_cpu_affinity(char *arg);
152 uint64_t vm_get_cpu_affinity_dm(void);
153 int	vm_set_vcpu_regs(struct vmctx *ctx, struct acrn_vcpu_regs *cpu_regs);
154 
155 int	vm_get_cpu_state(struct vmctx *ctx, void *state_buf);
156 int	vm_intr_monitor(struct vmctx *ctx, void *intr_buf);
157 void	vm_stop_watchdog(struct vmctx *ctx);
158 void	vm_reset_watchdog(struct vmctx *ctx);
159 
160 int	vm_ioeventfd(struct vmctx *ctx, struct acrn_ioeventfd *args);
161 int	vm_irqfd(struct vmctx *ctx, struct acrn_irqfd *args);
162 
163 /*
164  * Return a string describing the meaning of the `error' code.
165  */
166 char* errormsg(int error);
167 
168 #endif	/* _VMMAPI_H_ */
169