xref: /freebsd/sys/dev/virtio/virtio.c (revision d0b2dbfa)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011, 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 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35 #include <sys/sbuf.h>
36 
37 #include <machine/bus.h>
38 #include <machine/resource.h>
39 #include <sys/bus.h>
40 #include <sys/rman.h>
41 
42 #include <dev/virtio/virtio.h>
43 #include <dev/virtio/virtio_config.h>
44 #include <dev/virtio/virtqueue.h>
45 
46 #include "virtio_bus_if.h"
47 
48 static int virtio_modevent(module_t, int, void *);
49 static const char *virtio_feature_name(uint64_t, struct virtio_feature_desc *);
50 
51 static struct virtio_ident {
52 	uint16_t	devid;
53 	const char	*name;
54 } virtio_ident_table[] = {
55 	{ VIRTIO_ID_NETWORK,		"Network"			},
56 	{ VIRTIO_ID_BLOCK,		"Block"				},
57 	{ VIRTIO_ID_CONSOLE,		"Console"			},
58 	{ VIRTIO_ID_ENTROPY,		"Entropy"			},
59 	{ VIRTIO_ID_BALLOON,		"Balloon"			},
60 	{ VIRTIO_ID_IOMEMORY,		"IOMemory"			},
61 	{ VIRTIO_ID_RPMSG,		"Remote Processor Messaging"	},
62 	{ VIRTIO_ID_SCSI,		"SCSI"				},
63 	{ VIRTIO_ID_9P,			"9P Transport"			},
64 	{ VIRTIO_ID_RPROC_SERIAL,	"Remote Processor Serial"	},
65 	{ VIRTIO_ID_CAIF,		"CAIF"				},
66 	{ VIRTIO_ID_GPU,		"GPU"				},
67 	{ VIRTIO_ID_INPUT,		"Input"				},
68 	{ VIRTIO_ID_VSOCK,		"VSOCK Transport"		},
69 	{ VIRTIO_ID_CRYPTO,		"Crypto"			},
70 
71 	{ 0, NULL }
72 };
73 
74 /* Device independent features. */
75 static struct virtio_feature_desc virtio_common_feature_desc[] = {
76 	{ VIRTIO_F_NOTIFY_ON_EMPTY,	"NotifyOnEmpty"		}, /* Legacy */
77 	{ VIRTIO_F_ANY_LAYOUT,		"AnyLayout"		}, /* Legacy */
78 	{ VIRTIO_RING_F_INDIRECT_DESC,	"RingIndirectDesc"	},
79 	{ VIRTIO_RING_F_EVENT_IDX,	"RingEventIdx"		},
80 	{ VIRTIO_F_BAD_FEATURE,		"BadFeature"		}, /* Legacy */
81 	{ VIRTIO_F_VERSION_1,		"Version1"		},
82 	{ VIRTIO_F_IOMMU_PLATFORM,	"IOMMUPlatform"		},
83 
84 	{ 0, NULL }
85 };
86 
87 const char *
88 virtio_device_name(uint16_t devid)
89 {
90 	struct virtio_ident *ident;
91 
92 	for (ident = virtio_ident_table; ident->name != NULL; ident++) {
93 		if (ident->devid == devid)
94 			return (ident->name);
95 	}
96 
97 	return (NULL);
98 }
99 
100 static const char *
101 virtio_feature_name(uint64_t val, struct virtio_feature_desc *desc)
102 {
103 	int i, j;
104 	struct virtio_feature_desc *descs[2] = { desc,
105 	    virtio_common_feature_desc };
106 
107 	for (i = 0; i < 2; i++) {
108 		if (descs[i] == NULL)
109 			continue;
110 
111 		for (j = 0; descs[i][j].vfd_val != 0; j++) {
112 			if (val == descs[i][j].vfd_val)
113 				return (descs[i][j].vfd_str);
114 		}
115 	}
116 
117 	return (NULL);
118 }
119 
120 int
121 virtio_describe_sbuf(struct sbuf *sb, uint64_t features,
122     struct virtio_feature_desc *desc)
123 {
124 	const char *name;
125 	uint64_t val;
126 	int n;
127 
128 	sbuf_printf(sb, "%#jx", (uintmax_t) features);
129 
130 	for (n = 0, val = 1ULL << 63; val != 0; val >>= 1) {
131 		/*
132 		 * BAD_FEATURE is used to detect broken Linux clients
133 		 * and therefore is not applicable to FreeBSD.
134 		 */
135 		if (((features & val) == 0) || val == VIRTIO_F_BAD_FEATURE)
136 			continue;
137 
138 		if (n++ == 0)
139 			sbuf_cat(sb, " <");
140 		else
141 			sbuf_cat(sb, ",");
142 
143 		name = virtio_feature_name(val, desc);
144 		if (name == NULL)
145 			sbuf_printf(sb, "%#jx", (uintmax_t) val);
146 		else
147 			sbuf_cat(sb, name);
148 	}
149 
150 	if (n > 0)
151 		sbuf_cat(sb, ">");
152 
153 	return (sbuf_finish(sb));
154 }
155 
156 void
157 virtio_describe(device_t dev, const char *msg, uint64_t features,
158     struct virtio_feature_desc *desc)
159 {
160 	struct sbuf sb;
161 	char *buf;
162 	int error;
163 
164 	if ((buf = malloc(1024, M_TEMP, M_NOWAIT)) == NULL) {
165 		error = ENOMEM;
166 		goto out;
167 	}
168 
169 	sbuf_new(&sb, buf, 1024, SBUF_FIXEDLEN);
170 	sbuf_printf(&sb, "%s features: ", msg);
171 
172 	error = virtio_describe_sbuf(&sb, features, desc);
173 	if (error == 0)
174 		device_printf(dev, "%s\n", sbuf_data(&sb));
175 
176 	sbuf_delete(&sb);
177 	free(buf, M_TEMP);
178 
179 out:
180 	if (error != 0) {
181 		device_printf(dev, "%s features: %#jx\n", msg,
182 		    (uintmax_t) features);
183 	}
184 }
185 
186 uint64_t
187 virtio_filter_transport_features(uint64_t features)
188 {
189 	uint64_t transport, mask;
190 
191 	transport = (1ULL <<
192 	    (VIRTIO_TRANSPORT_F_END - VIRTIO_TRANSPORT_F_START)) - 1;
193 	transport <<= VIRTIO_TRANSPORT_F_START;
194 
195 	mask = -1ULL & ~transport;
196 	mask |= VIRTIO_RING_F_INDIRECT_DESC;
197 	mask |= VIRTIO_RING_F_EVENT_IDX;
198 	mask |= VIRTIO_F_VERSION_1;
199 
200 	return (features & mask);
201 }
202 
203 int
204 virtio_bus_is_modern(device_t dev)
205 {
206 	uintptr_t modern;
207 
208 	virtio_read_ivar(dev, VIRTIO_IVAR_MODERN, &modern);
209 	return (modern != 0);
210 }
211 
212 void
213 virtio_read_device_config_array(device_t dev, bus_size_t offset, void *dst,
214     int size, int count)
215 {
216 	int i, gen;
217 
218 	do {
219 		gen = virtio_config_generation(dev);
220 
221 		for (i = 0; i < count; i++) {
222 			virtio_read_device_config(dev, offset + i * size,
223 			    (uint8_t *) dst + i * size, size);
224 		}
225 	} while (gen != virtio_config_generation(dev));
226 }
227 
228 /*
229  * VirtIO bus method wrappers.
230  */
231 
232 void
233 virtio_read_ivar(device_t dev, int ivar, uintptr_t *val)
234 {
235 
236 	*val = -1;
237 	BUS_READ_IVAR(device_get_parent(dev), dev, ivar, val);
238 }
239 
240 void
241 virtio_write_ivar(device_t dev, int ivar, uintptr_t val)
242 {
243 
244 	BUS_WRITE_IVAR(device_get_parent(dev), dev, ivar, val);
245 }
246 
247 uint64_t
248 virtio_negotiate_features(device_t dev, uint64_t child_features)
249 {
250 
251 	return (VIRTIO_BUS_NEGOTIATE_FEATURES(device_get_parent(dev),
252 	    child_features));
253 }
254 
255 int
256 virtio_finalize_features(device_t dev)
257 {
258 
259 	return (VIRTIO_BUS_FINALIZE_FEATURES(device_get_parent(dev)));
260 }
261 
262 int
263 virtio_alloc_virtqueues(device_t dev, int flags, int nvqs,
264     struct vq_alloc_info *info)
265 {
266 
267 	return (VIRTIO_BUS_ALLOC_VIRTQUEUES(device_get_parent(dev), flags,
268 	    nvqs, info));
269 }
270 
271 int
272 virtio_setup_intr(device_t dev, enum intr_type type)
273 {
274 
275 	return (VIRTIO_BUS_SETUP_INTR(device_get_parent(dev), type));
276 }
277 
278 int
279 virtio_with_feature(device_t dev, uint64_t feature)
280 {
281 
282 	return (VIRTIO_BUS_WITH_FEATURE(device_get_parent(dev), feature));
283 }
284 
285 void
286 virtio_stop(device_t dev)
287 {
288 
289 	VIRTIO_BUS_STOP(device_get_parent(dev));
290 }
291 
292 int
293 virtio_reinit(device_t dev, uint64_t features)
294 {
295 
296 	return (VIRTIO_BUS_REINIT(device_get_parent(dev), features));
297 }
298 
299 void
300 virtio_reinit_complete(device_t dev)
301 {
302 
303 	VIRTIO_BUS_REINIT_COMPLETE(device_get_parent(dev));
304 }
305 
306 int
307 virtio_config_generation(device_t dev)
308 {
309 
310 	return (VIRTIO_BUS_CONFIG_GENERATION(device_get_parent(dev)));
311 }
312 
313 void
314 virtio_read_device_config(device_t dev, bus_size_t offset, void *dst, int len)
315 {
316 
317 	VIRTIO_BUS_READ_DEVICE_CONFIG(device_get_parent(dev),
318 	    offset, dst, len);
319 }
320 
321 void
322 virtio_write_device_config(device_t dev, bus_size_t offset, const void *dst, int len)
323 {
324 
325 	VIRTIO_BUS_WRITE_DEVICE_CONFIG(device_get_parent(dev),
326 	    offset, dst, len);
327 }
328 
329 int
330 virtio_child_pnpinfo(device_t busdev __unused, device_t child, struct sbuf *sb)
331 {
332 
333 	/*
334 	 * All of these PCI fields will be only 16 bits, but on the vtmmio bus
335 	 * the corresponding fields (only "vendor" and "device_type") are 32
336 	 * bits.  Many virtio drivers can attach below either bus.
337 	 * Gratuitously expand these two fields to 32-bits to allow sharing PNP
338 	 * match table data between the mostly-similar buses.
339 	 *
340 	 * Subdevice and device_type are redundant in both buses, so I don't
341 	 * see a lot of PNP utility in exposing the same value under a
342 	 * different name.
343 	 */
344 	sbuf_printf(sb, "vendor=0x%08x device=0x%04x subvendor=0x%04x "
345 	    "device_type=0x%08x", (unsigned)virtio_get_vendor(child),
346 	    (unsigned)virtio_get_device(child),
347 	    (unsigned)virtio_get_subvendor(child),
348 	    (unsigned)virtio_get_device_type(child));
349 	return (0);
350 }
351 
352 static int
353 virtio_modevent(module_t mod, int type, void *unused)
354 {
355 	int error;
356 
357 	switch (type) {
358 	case MOD_LOAD:
359 	case MOD_QUIESCE:
360 	case MOD_UNLOAD:
361 	case MOD_SHUTDOWN:
362 		error = 0;
363 		break;
364 	default:
365 		error = EOPNOTSUPP;
366 		break;
367 	}
368 
369 	return (error);
370 }
371 
372 static moduledata_t virtio_mod = {
373 	"virtio",
374 	virtio_modevent,
375 	0
376 };
377 
378 DECLARE_MODULE(virtio, virtio_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
379 MODULE_VERSION(virtio, 1);
380