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