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