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