1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 
32 #include <machine/vmm.h>
33 #include <machine/vmm_dev.h>
34 #include <machine/vmm_snapshot.h>
35 
36 #include <assert.h>
37 #include <string.h>
38 
39 #include "vmmapi.h"
40 #include "internal.h"
41 
42 const char *vm_capstrmap[] = {
43 	[VM_CAP_MAX] = NULL,
44 };
45 
46 #define	VM_MD_IOCTLS		\
47 	VM_GET_VGIC_VERSION,	\
48 	VM_ATTACH_VGIC,		\
49 	VM_ASSERT_IRQ,		\
50 	VM_DEASSERT_IRQ,	\
51 	VM_RAISE_MSI
52 
53 const cap_ioctl_t vm_ioctl_cmds[] = {
54 	VM_COMMON_IOCTLS,
55 	VM_MD_IOCTLS,
56 };
57 size_t vm_ioctl_ncmds = nitems(vm_ioctl_cmds);
58 
59 int
60 vm_attach_vgic(struct vmctx *ctx, uint64_t dist_start, size_t dist_size,
61     uint64_t redist_start, size_t redist_size)
62 {
63 	struct vm_vgic_descr vgic;
64 	int error;
65 
66 	bzero(&vgic, sizeof(vgic));
67 	error = ioctl(ctx->fd, VM_GET_VGIC_VERSION, &vgic.ver);
68 	if (error != 0)
69 		return (error);
70 	assert(vgic.ver.version == 3);
71 	vgic.v3_regs.dist_start = dist_start;
72 	vgic.v3_regs.dist_size = dist_size;
73 	vgic.v3_regs.redist_start = redist_start;
74 	vgic.v3_regs.redist_size = redist_size;
75 
76 	return (ioctl(ctx->fd, VM_ATTACH_VGIC, &vgic));
77 }
78 
79 int
80 vm_assert_irq(struct vmctx *ctx, uint32_t irq)
81 {
82 	struct vm_irq vi;
83 
84 	bzero(&vi, sizeof(vi));
85 	vi.irq = irq;
86 
87 	return (ioctl(ctx->fd, VM_ASSERT_IRQ, &vi));
88 }
89 
90 int
91 vm_deassert_irq(struct vmctx *ctx, uint32_t irq)
92 {
93 	struct vm_irq vi;
94 
95 	bzero(&vi, sizeof(vi));
96 	vi.irq = irq;
97 
98 	return (ioctl(ctx->fd, VM_DEASSERT_IRQ, &vi));
99 }
100 
101 int
102 vm_raise_msi(struct vmctx *ctx, uint64_t addr, uint64_t msg,
103     int bus, int slot, int func)
104 {
105 	struct vm_msi vmsi;
106 
107 	bzero(&vmsi, sizeof(vmsi));
108 	vmsi.addr = addr;
109 	vmsi.msg = msg;
110 	vmsi.bus = bus;
111 	vmsi.slot = slot;
112 	vmsi.func = func;
113 
114 	return (ioctl(ctx->fd, VM_RAISE_MSI, &vmsi));
115 }
116 
117 int
118 vm_inject_exception(struct vcpu *vcpu, uint64_t esr, uint64_t far)
119 {
120 	struct vm_exception vmexc;
121 
122 	bzero(&vmexc, sizeof(vmexc));
123 	vmexc.esr = esr;
124 	vmexc.far = far;
125 
126 	return (vcpu_ioctl(vcpu, VM_INJECT_EXCEPTION, &vmexc));
127 }
128