xref: /freebsd/sys/dev/virtio/virtio.h (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2014, Bryan Venteicher <bryanv@FreeBSD.org>
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 unmodified, this list of conditions, and the following
12  *    disclaimer.
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.
16  *
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.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef _VIRTIO_H_
32 #define _VIRTIO_H_
33 
34 #include <dev/virtio/virtio_endian.h>
35 #include <dev/virtio/virtio_ids.h>
36 #include <dev/virtio/virtio_config.h>
37 
38 #ifdef _KERNEL
39 
40 struct sbuf;
41 struct vq_alloc_info;
42 
43 /*
44  * Each virtqueue indirect descriptor list must be physically contiguous.
45  * To allow us to malloc(9) each list individually, limit the number
46  * supported to what will fit in one page. With 4KB pages, this is a limit
47  * of 256 descriptors. If there is ever a need for more, we can switch to
48  * contigmalloc(9) for the larger allocations, similar to what
49  * bus_dmamem_alloc(9) does.
50  *
51  * Note the sizeof(struct vring_desc) is 16 bytes.
52  */
53 #define VIRTIO_MAX_INDIRECT ((int) (PAGE_SIZE / 16))
54 
55 /*
56  * VirtIO instance variables indices.
57  */
58 #define VIRTIO_IVAR_DEVTYPE		1
59 #define VIRTIO_IVAR_FEATURE_DESC	2
60 #define VIRTIO_IVAR_VENDOR		3
61 #define VIRTIO_IVAR_DEVICE		4
62 #define VIRTIO_IVAR_SUBVENDOR		5
63 #define VIRTIO_IVAR_SUBDEVICE		6
64 #define VIRTIO_IVAR_MODERN		7
65 
66 struct virtio_feature_desc {
67 	uint64_t	 vfd_val;
68 	const char	*vfd_str;
69 };
70 
71 #define VIRTIO_DRIVER_MODULE(name, driver, devclass, evh, arg)		\
72 	DRIVER_MODULE(name, virtio_mmio, driver, devclass, evh, arg);	\
73 	DRIVER_MODULE(name, virtio_pci, driver, devclass, evh, arg)
74 
75 struct virtio_pnp_match {
76 	uint32_t	 device_type;
77 	const char	*description;
78 };
79 #define VIRTIO_SIMPLE_PNPINFO(driver, devtype, desc)			\
80 	static const struct virtio_pnp_match driver ## _match = {	\
81 		.device_type = devtype,					\
82 		.description = desc,					\
83 	};								\
84 	MODULE_PNP_INFO("U32:device_type;D:#", virtio_mmio, driver,	\
85 	    &driver ## _match, 1);					\
86 	MODULE_PNP_INFO("U32:device_type;D:#", virtio_pci, driver,	\
87 	    &driver ## _match, 1)
88 #define VIRTIO_SIMPLE_PROBE(dev, driver)				\
89 	(virtio_simple_probe(dev, &driver ## _match))
90 
91 const char *virtio_device_name(uint16_t devid);
92 void	 virtio_describe(device_t dev, const char *msg,
93 	     uint64_t features, struct virtio_feature_desc *desc);
94 int	 virtio_describe_sbuf(struct sbuf *sb, uint64_t features,
95 	     struct virtio_feature_desc *desc);
96 uint64_t virtio_filter_transport_features(uint64_t features);
97 int	 virtio_bus_is_modern(device_t dev);
98 void	 virtio_read_device_config_array(device_t dev, bus_size_t offset,
99 	     void *dst, int size, int count);
100 
101 /*
102  * VirtIO Bus Methods.
103  */
104 void	 virtio_read_ivar(device_t dev, int ivar, uintptr_t *val);
105 void	 virtio_write_ivar(device_t dev, int ivar, uintptr_t val);
106 uint64_t virtio_negotiate_features(device_t dev, uint64_t child_features);
107 int	 virtio_finalize_features(device_t dev);
108 int	 virtio_alloc_virtqueues(device_t dev, int flags, int nvqs,
109 	     struct vq_alloc_info *info);
110 int	 virtio_setup_intr(device_t dev, enum intr_type type);
111 int	 virtio_with_feature(device_t dev, uint64_t feature);
112 void	 virtio_stop(device_t dev);
113 int	 virtio_config_generation(device_t dev);
114 int	 virtio_reinit(device_t dev, uint64_t features);
115 void	 virtio_reinit_complete(device_t dev);
116 int	 virtio_child_pnpinfo_str(device_t busdev, device_t child, char *buf,
117 	     size_t buflen);
118 
119 /*
120  * Read/write a variable amount from the device specific (ie, network)
121  * configuration region. This region is encoded in the same endian as
122  * the guest.
123  */
124 void	 virtio_read_device_config(device_t dev, bus_size_t offset,
125 	     void *dst, int length);
126 void	 virtio_write_device_config(device_t dev, bus_size_t offset,
127 	     void *src, int length);
128 
129 /* Inlined device specific read/write functions for common lengths. */
130 #define VIRTIO_RDWR_DEVICE_CONFIG(size, type)				\
131 static inline type							\
132 __CONCAT(virtio_read_dev_config_,size)(device_t dev,			\
133     bus_size_t offset)							\
134 {									\
135 	type val;							\
136 	virtio_read_device_config(dev, offset, &val, sizeof(type));	\
137 	return (val);							\
138 }									\
139 									\
140 static inline void							\
141 __CONCAT(virtio_write_dev_config_,size)(device_t dev,			\
142     bus_size_t offset, type val)					\
143 {									\
144 	virtio_write_device_config(dev, offset, &val, sizeof(type));	\
145 }
146 
147 VIRTIO_RDWR_DEVICE_CONFIG(1, uint8_t);
148 VIRTIO_RDWR_DEVICE_CONFIG(2, uint16_t);
149 VIRTIO_RDWR_DEVICE_CONFIG(4, uint32_t);
150 
151 #undef VIRTIO_RDWR_DEVICE_CONFIG
152 
153 #define VIRTIO_READ_IVAR(name, ivar)					\
154 static inline int							\
155 __CONCAT(virtio_get_,name)(device_t dev)				\
156 {									\
157 	uintptr_t val;							\
158 	virtio_read_ivar(dev, ivar, &val);				\
159 	return ((int) val);						\
160 }
161 
162 VIRTIO_READ_IVAR(device_type,	VIRTIO_IVAR_DEVTYPE);
163 VIRTIO_READ_IVAR(vendor,	VIRTIO_IVAR_VENDOR);
164 VIRTIO_READ_IVAR(device,	VIRTIO_IVAR_DEVICE);
165 VIRTIO_READ_IVAR(subvendor,	VIRTIO_IVAR_SUBVENDOR);
166 VIRTIO_READ_IVAR(subdevice,	VIRTIO_IVAR_SUBDEVICE);
167 VIRTIO_READ_IVAR(modern,	VIRTIO_IVAR_MODERN);
168 
169 #undef VIRTIO_READ_IVAR
170 
171 #define VIRTIO_WRITE_IVAR(name, ivar)					\
172 static inline void							\
173 __CONCAT(virtio_set_,name)(device_t dev, void *val)			\
174 {									\
175 	virtio_write_ivar(dev, ivar, (uintptr_t) val);			\
176 }
177 
178 VIRTIO_WRITE_IVAR(feature_desc,	VIRTIO_IVAR_FEATURE_DESC);
179 
180 #undef VIRTIO_WRITE_IVAR
181 
182 static inline int
183 virtio_simple_probe(device_t dev, const struct virtio_pnp_match *match)
184 {
185 
186 	if (virtio_get_device_type(dev) != match->device_type)
187 		return (ENXIO);
188 	device_set_desc(dev, match->description);
189 	return (BUS_PROBE_DEFAULT);
190 }
191 
192 #endif /* _KERNEL */
193 
194 #endif /* _VIRTIO_H_ */
195