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