xref: /freebsd/lib/libvmmapi/ppt.c (revision d59c7ea2701fe7b73b32eef49a7c712ef38de5a0)
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 
34 #include <string.h>
35 
36 #include "vmmapi.h"
37 #include "internal.h"
38 
39 int
40 vm_assign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
41 {
42 	struct vm_pptdev pptdev;
43 
44 	bzero(&pptdev, sizeof(pptdev));
45 	pptdev.bus = bus;
46 	pptdev.slot = slot;
47 	pptdev.func = func;
48 
49 	return (ioctl(ctx->fd, VM_BIND_PPTDEV, &pptdev));
50 }
51 
52 int
53 vm_unassign_pptdev(struct vmctx *ctx, int bus, int slot, int func)
54 {
55 	struct vm_pptdev pptdev;
56 
57 	bzero(&pptdev, sizeof(pptdev));
58 	pptdev.bus = bus;
59 	pptdev.slot = slot;
60 	pptdev.func = func;
61 
62 	return (ioctl(ctx->fd, VM_UNBIND_PPTDEV, &pptdev));
63 }
64 
65 int
66 vm_reset_pptdev(struct vmctx *ctx, int bus, int slot, int func)
67 {
68 	struct vm_pptdev pptdev;
69 
70 	bzero(&pptdev, sizeof(pptdev));
71 	pptdev.bus = bus;
72 	pptdev.slot = slot;
73 	pptdev.func = func;
74 
75 	return (ioctl(ctx->fd, VM_RESET_PPTDEV, &pptdev));
76 }
77 
78 int
79 vm_map_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
80     vm_paddr_t gpa, size_t len, vm_paddr_t hpa)
81 {
82 	struct vm_pptdev_mmio pptmmio;
83 
84 	bzero(&pptmmio, sizeof(pptmmio));
85 	pptmmio.bus = bus;
86 	pptmmio.slot = slot;
87 	pptmmio.func = func;
88 	pptmmio.gpa = gpa;
89 	pptmmio.len = len;
90 	pptmmio.hpa = hpa;
91 
92 	return (ioctl(ctx->fd, VM_MAP_PPTDEV_MMIO, &pptmmio));
93 }
94 
95 int
96 vm_unmap_pptdev_mmio(struct vmctx *ctx, int bus, int slot, int func,
97     vm_paddr_t gpa, size_t len)
98 {
99 	struct vm_pptdev_mmio pptmmio;
100 
101 	bzero(&pptmmio, sizeof(pptmmio));
102 	pptmmio.bus = bus;
103 	pptmmio.slot = slot;
104 	pptmmio.func = func;
105 	pptmmio.gpa = gpa;
106 	pptmmio.len = len;
107 
108 	return (ioctl(ctx->fd, VM_UNMAP_PPTDEV_MMIO, &pptmmio));
109 }
110 
111 int
112 vm_setup_pptdev_msi(struct vmctx *ctx, int bus, int slot, int func,
113     uint64_t addr, uint64_t msg, int numvec)
114 {
115 	struct vm_pptdev_msi pptmsi;
116 
117 	bzero(&pptmsi, sizeof(pptmsi));
118 	pptmsi.bus = bus;
119 	pptmsi.slot = slot;
120 	pptmsi.func = func;
121 	pptmsi.msg = msg;
122 	pptmsi.addr = addr;
123 	pptmsi.numvec = numvec;
124 
125 	return (ioctl(ctx->fd, VM_PPTDEV_MSI, &pptmsi));
126 }
127 
128 int
129 vm_setup_pptdev_msix(struct vmctx *ctx, int bus, int slot, int func,
130     int idx, uint64_t addr, uint64_t msg, uint32_t vector_control)
131 {
132 	struct vm_pptdev_msix pptmsix;
133 
134 	bzero(&pptmsix, sizeof(pptmsix));
135 	pptmsix.bus = bus;
136 	pptmsix.slot = slot;
137 	pptmsix.func = func;
138 	pptmsix.idx = idx;
139 	pptmsix.msg = msg;
140 	pptmsix.addr = addr;
141 	pptmsix.vector_control = vector_control;
142 
143 	return (ioctl(ctx->fd, VM_PPTDEV_MSIX, &pptmsix));
144 }
145 
146 int
147 vm_disable_pptdev_msix(struct vmctx *ctx, int bus, int slot, int func)
148 {
149 	struct vm_pptdev ppt;
150 
151 	bzero(&ppt, sizeof(ppt));
152 	ppt.bus = bus;
153 	ppt.slot = slot;
154 	ppt.func = func;
155 
156 	return (ioctl(ctx->fd, VM_PPTDEV_DISABLE_MSIX, &ppt));
157 }
158