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