From owner-dev-commits-src-all@freebsd.org Thu Jan 21 01:14:06 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 9817B4DFED6; Thu, 21 Jan 2021 01:14:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4DLktk3wGsz4ZJ6; Thu, 21 Jan 2021 01:14:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 79DAC22E6F; Thu, 21 Jan 2021 01:14:06 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 10L1E6ID039477; Thu, 21 Jan 2021 01:14:06 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 10L1E6YI039476; Thu, 21 Jan 2021 01:14:06 GMT (envelope-from git) Date: Thu, 21 Jan 2021 01:14:06 GMT Message-Id: <202101210114.10L1E6YI039476@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Jessica Clarke Subject: git: be79a2c60fec - main - virtio_mmio: Fix V1 device probing spec conformance (section 4.2.3.1.1) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jrtc27 X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: be79a2c60fec0b4ead6369e6ce420a664a0dfa9d Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 21 Jan 2021 01:14:06 -0000 The branch main has been updated by jrtc27: URL: https://cgit.FreeBSD.org/src/commit/?id=be79a2c60fec0b4ead6369e6ce420a664a0dfa9d commit be79a2c60fec0b4ead6369e6ce420a664a0dfa9d Author: Jessica Clarke AuthorDate: 2021-01-21 01:03:44 +0000 Commit: Jessica Clarke CommitDate: 2021-01-21 01:05:21 +0000 virtio_mmio: Fix V1 device probing spec conformance (section 4.2.3.1.1) We must check MagicValue not just Version before anything else, and then we must check DeviceID and immediately abort if zero (and this must not be an error). Do all this when probing rather than at the start of attaching as that's where this belongs, and provides a clear boundary between the device detection and device initialisation parts of the specified driver initialisation process. This also means we don't create empty device instances for placeholder devices, reducing clutter on systems that pre-allocate a large number, such as QEMU's AArch64 virt machine (which provides 32). Reviewed by: bryanv Differential Revision: https://reviews.freebsd.org/D28070 --- sys/dev/virtio/mmio/virtio_mmio.c | 50 ++++++++++++++++++++++++++++------ sys/dev/virtio/mmio/virtio_mmio.h | 2 ++ sys/dev/virtio/mmio/virtio_mmio_acpi.c | 3 +- sys/dev/virtio/mmio/virtio_mmio_fdt.c | 3 +- 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/sys/dev/virtio/mmio/virtio_mmio.c b/sys/dev/virtio/mmio/virtio_mmio.c index c16ecd40a250..862272b917df 100644 --- a/sys/dev/virtio/mmio/virtio_mmio.c +++ b/sys/dev/virtio/mmio/virtio_mmio.c @@ -171,6 +171,48 @@ DEFINE_CLASS_0(virtio_mmio, vtmmio_driver, vtmmio_methods, MODULE_VERSION(virtio_mmio, 1); +int +vtmmio_probe(device_t dev) +{ + struct vtmmio_softc *sc; + int rid; + uint32_t magic, version; + + sc = device_get_softc(dev); + + rid = 0; + sc->res[0] = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, + RF_ACTIVE); + if (sc->res[0] == NULL) { + device_printf(dev, "Cannot allocate memory window.\n"); + return (ENXIO); + } + + magic = vtmmio_read_config_4(sc, VIRTIO_MMIO_MAGIC_VALUE); + if (magic != VIRTIO_MMIO_MAGIC_VIRT) { + device_printf(dev, "Bad magic value %#x\n", magic); + bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->res[0]); + return (ENXIO); + } + + version = vtmmio_read_config_4(sc, VIRTIO_MMIO_VERSION); + if (version < 1 || version > 2) { + device_printf(dev, "Unsupported version: %#x\n", version); + bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->res[0]); + return (ENXIO); + } + + if (vtmmio_read_config_4(sc, VIRTIO_MMIO_DEVICE_ID) == 0) { + bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->res[0]); + return (ENXIO); + } + + bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->res[0]); + + device_set_desc(dev, "VirtIO MMIO adapter"); + return (BUS_PROBE_DEFAULT); +} + static int vtmmio_setup_intr(device_t dev, enum intr_type type) { @@ -225,14 +267,6 @@ vtmmio_attach(device_t dev) } sc->vtmmio_version = vtmmio_read_config_4(sc, VIRTIO_MMIO_VERSION); - if (sc->vtmmio_version < 1 || sc->vtmmio_version > 2) { - device_printf(dev, "Unsupported version: %x\n", - sc->vtmmio_version); - bus_release_resource(dev, SYS_RES_MEMORY, 0, - sc->res[0]); - sc->res[0] = NULL; - return (ENXIO); - } vtmmio_reset(sc); diff --git a/sys/dev/virtio/mmio/virtio_mmio.h b/sys/dev/virtio/mmio/virtio_mmio.h index ce60cd4fa173..5eb56d860780 100644 --- a/sys/dev/virtio/mmio/virtio_mmio.h +++ b/sys/dev/virtio/mmio/virtio_mmio.h @@ -55,6 +55,7 @@ struct vtmmio_softc { void *ih; }; +int vtmmio_probe(device_t); int vtmmio_attach(device_t); #define VIRTIO_MMIO_MAGIC_VALUE 0x000 @@ -84,6 +85,7 @@ int vtmmio_attach(device_t); #define VIRTIO_MMIO_QUEUE_USED_HIGH 0x0a4 /* requires version 2 */ #define VIRTIO_MMIO_CONFIG_GENERATION 0x100 /* requires version 2 */ #define VIRTIO_MMIO_CONFIG 0x100 +#define VIRTIO_MMIO_MAGIC_VIRT 0x74726976 #define VIRTIO_MMIO_INT_VRING (1 << 0) #define VIRTIO_MMIO_INT_CONFIG (1 << 1) #define VIRTIO_MMIO_VRING_ALIGN 4096 diff --git a/sys/dev/virtio/mmio/virtio_mmio_acpi.c b/sys/dev/virtio/mmio/virtio_mmio_acpi.c index bd737a6e53db..0cb34f5aa873 100644 --- a/sys/dev/virtio/mmio/virtio_mmio_acpi.c +++ b/sys/dev/virtio/mmio/virtio_mmio_acpi.c @@ -81,6 +81,5 @@ vtmmio_acpi_probe(device_t dev) if (!acpi_MatchHid(h, "LNRO0005")) return (ENXIO); - device_set_desc(dev, "VirtIO MMIO adapter"); - return (BUS_PROBE_DEFAULT); + return (vtmmio_probe(dev)); } diff --git a/sys/dev/virtio/mmio/virtio_mmio_fdt.c b/sys/dev/virtio/mmio/virtio_mmio_fdt.c index a6867176a07c..a188df707946 100644 --- a/sys/dev/virtio/mmio/virtio_mmio_fdt.c +++ b/sys/dev/virtio/mmio/virtio_mmio_fdt.c @@ -97,8 +97,7 @@ vtmmio_fdt_probe(device_t dev) if (!ofw_bus_is_compatible(dev, "virtio,mmio")) return (ENXIO); - device_set_desc(dev, "VirtIO MMIO adapter"); - return (BUS_PROBE_DEFAULT); + return (vtmmio_probe(dev)); } static int