2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011, Bryan Venteicher <bryanv@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
44 #include <dev/virtio/virtio.h>
45 #include <dev/virtio/virtio_config.h>
46 #include <dev/virtio/virtqueue.h>
48 #include "virtio_bus_if.h"
50 static int virtio_modevent(module_t, int, void *);
51 static const char *virtio_feature_name(uint64_t, struct virtio_feature_desc *);
53 static struct virtio_ident {
56 } virtio_ident_table[] = {
57 { VIRTIO_ID_NETWORK, "Network" },
58 { VIRTIO_ID_BLOCK, "Block" },
59 { VIRTIO_ID_CONSOLE, "Console" },
60 { VIRTIO_ID_ENTROPY, "Entropy" },
61 { VIRTIO_ID_BALLOON, "Balloon" },
62 { VIRTIO_ID_IOMEMORY, "IOMemory" },
63 { VIRTIO_ID_RPMSG, "Remote Processor Messaging" },
64 { VIRTIO_ID_SCSI, "SCSI" },
65 { VIRTIO_ID_9P, "9P Transport" },
66 { VIRTIO_ID_RPROC_SERIAL, "Remote Processor Serial" },
67 { VIRTIO_ID_CAIF, "CAIF" },
68 { VIRTIO_ID_GPU, "GPU" },
69 { VIRTIO_ID_INPUT, "Input" },
70 { VIRTIO_ID_VSOCK, "VSOCK Transport" },
71 { VIRTIO_ID_CRYPTO, "Crypto" },
75 /* Device independent features. */
76 static struct virtio_feature_desc virtio_common_feature_desc[] = {
77 { VIRTIO_F_NOTIFY_ON_EMPTY, "NotifyOnEmpty" },
78 { VIRTIO_RING_F_INDIRECT_DESC, "RingIndirect" },
79 { VIRTIO_RING_F_EVENT_IDX, "EventIdx" },
80 { VIRTIO_F_BAD_FEATURE, "BadFeature" },
81 { VIRTIO_F_VERSION_1, "Version1" },
86 virtio_device_name(uint16_t devid)
88 struct virtio_ident *ident;
90 for (ident = virtio_ident_table; ident->name != NULL; ident++) {
91 if (ident->devid == devid)
99 virtio_feature_name(uint64_t val, struct virtio_feature_desc *desc)
102 struct virtio_feature_desc *descs[2] = { desc,
103 virtio_common_feature_desc };
105 for (i = 0; i < 2; i++) {
106 if (descs[i] == NULL)
109 for (j = 0; descs[i][j].vfd_val != 0; j++) {
110 if (val == descs[i][j].vfd_val)
111 return (descs[i][j].vfd_str);
119 virtio_describe(device_t dev, const char *msg,
120 uint64_t features, struct virtio_feature_desc *desc)
128 if ((buf = malloc(512, M_TEMP, M_NOWAIT)) == NULL) {
129 device_printf(dev, "%s features: %#jx\n", msg, (uintmax_t) features);
133 sbuf_new(&sb, buf, 512, SBUF_FIXEDLEN);
134 sbuf_printf(&sb, "%s features: %#jx", msg, (uintmax_t) features);
136 for (n = 0, val = 1ULL << 63; val != 0; val >>= 1) {
138 * BAD_FEATURE is used to detect broken Linux clients
139 * and therefore is not applicable to FreeBSD.
141 if (((features & val) == 0) || val == VIRTIO_F_BAD_FEATURE)
149 name = virtio_feature_name(val, desc);
151 sbuf_printf(&sb, "%#jx", (uintmax_t) val);
159 if (sbuf_finish(&sb) == 0)
160 device_printf(dev, "%s\n", sbuf_data(&sb));
167 * VirtIO bus method wrappers.
171 virtio_read_ivar(device_t dev, int ivar, uintptr_t *val)
175 BUS_READ_IVAR(device_get_parent(dev), dev, ivar, val);
179 virtio_write_ivar(device_t dev, int ivar, uintptr_t val)
182 BUS_WRITE_IVAR(device_get_parent(dev), dev, ivar, val);
186 virtio_negotiate_features(device_t dev, uint64_t child_features)
189 return (VIRTIO_BUS_NEGOTIATE_FEATURES(device_get_parent(dev),
194 virtio_alloc_virtqueues(device_t dev, int flags, int nvqs,
195 struct vq_alloc_info *info)
198 return (VIRTIO_BUS_ALLOC_VIRTQUEUES(device_get_parent(dev), flags,
203 virtio_setup_intr(device_t dev, enum intr_type type)
206 return (VIRTIO_BUS_SETUP_INTR(device_get_parent(dev), type));
210 virtio_with_feature(device_t dev, uint64_t feature)
213 return (VIRTIO_BUS_WITH_FEATURE(device_get_parent(dev), feature));
217 virtio_stop(device_t dev)
220 VIRTIO_BUS_STOP(device_get_parent(dev));
224 virtio_reinit(device_t dev, uint64_t features)
227 return (VIRTIO_BUS_REINIT(device_get_parent(dev), features));
231 virtio_reinit_complete(device_t dev)
234 VIRTIO_BUS_REINIT_COMPLETE(device_get_parent(dev));
238 virtio_config_generation(device_t dev)
241 return (VIRTIO_BUS_CONFIG_GENERATION(device_get_parent(dev)));
245 virtio_read_device_config(device_t dev, bus_size_t offset, void *dst, int len)
248 VIRTIO_BUS_READ_DEVICE_CONFIG(device_get_parent(dev),
253 virtio_write_device_config(device_t dev, bus_size_t offset, void *dst, int len)
256 VIRTIO_BUS_WRITE_DEVICE_CONFIG(device_get_parent(dev),
261 virtio_child_pnpinfo_str(device_t busdev __unused, device_t child, char *buf,
266 * All of these PCI fields will be only 16 bits, but on the vtmmio bus
267 * the corresponding fields (only "vendor" and "device_type") are 32
268 * bits. Many virtio drivers can attach below either bus.
269 * Gratuitously expand these two fields to 32-bits to allow sharing PNP
270 * match table data between the mostly-similar buses.
272 * Subdevice and device_type are redundant in both buses, so I don't
273 * see a lot of PNP utility in exposing the same value under a
276 snprintf(buf, buflen, "vendor=0x%08x device=0x%04x subvendor=0x%04x "
277 "device_type=0x%08x", (unsigned)virtio_get_vendor(child),
278 (unsigned)virtio_get_device(child),
279 (unsigned)virtio_get_subvendor(child),
280 (unsigned)virtio_get_device_type(child));
285 virtio_modevent(module_t mod, int type, void *unused)
304 static moduledata_t virtio_mod = {
310 DECLARE_MODULE(virtio, virtio_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
311 MODULE_VERSION(virtio, 1);