Module Name: src
Committed By: imil
Date: Thu Jan 2 10:34:33 UTC 2025
Modified Files:
src/sys/arch/amd64/amd64: amd64_mainbus.c
src/sys/arch/amd64/conf: files.amd64
Added Files:
src/share/man/man4: pv.4
src/sys/arch/x86/pv: files.pv pvbus.c pvvar.h
Log Message:
Trivial bus implementation inspired by OpenBSD's pv(4) to attach devices
that don't need nor rely on a PCI or ISA bus.
To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/share/man/man4/pv.4
cvs rdiff -u -r1.7 -r1.8 src/sys/arch/amd64/amd64/amd64_mainbus.c
cvs rdiff -u -r1.123 -r1.124 src/sys/arch/amd64/conf/files.amd64
cvs rdiff -u -r0 -r1.1 src/sys/arch/x86/pv/files.pv \
src/sys/arch/x86/pv/pvbus.c src/sys/arch/x86/pv/pvvar.h
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/sys/arch/amd64/amd64/amd64_mainbus.c
diff -u src/sys/arch/amd64/amd64/amd64_mainbus.c:1.7 src/sys/arch/amd64/amd64/amd64_mainbus.c:1.8
--- src/sys/arch/amd64/amd64/amd64_mainbus.c:1.7 Sat Aug 7 16:18:41 2021
+++ src/sys/arch/amd64/amd64/amd64_mainbus.c Thu Jan 2 10:34:33 2025
@@ -1,4 +1,4 @@
-/* $NetBSD: amd64_mainbus.c,v 1.7 2021/08/07 16:18:41 thorpej Exp $ */
+/* $NetBSD: amd64_mainbus.c,v 1.8 2025/01/02 10:34:33 imil Exp $ */
/* NetBSD: mainbus.c,v 1.39 2018/12/02 08:19:44 cherry Exp */
/*
@@ -32,7 +32,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: amd64_mainbus.c,v 1.7 2021/08/07 16:18:41 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: amd64_mainbus.c,v 1.8 2025/01/02 10:34:33 imil Exp $");
#include <sys/param.h>
#include <sys/systm.h>
@@ -50,6 +50,7 @@ __KERNEL_RCSID(0, "$NetBSD: amd64_mainbu
#include "isadma.h"
#include "acpica.h"
#include "ipmi.h"
+#include "pvbus.h"
#include "opt_acpi.h"
#include "opt_mpbios.h"
@@ -79,6 +80,9 @@ __KERNEL_RCSID(0, "$NetBSD: amd64_mainbu
#include <arch/x86/pci/msipic.h>
#endif /* __HAVE_PCI_MSI_MSIX */
#endif
+#if NPVBUS > 0
+#include <arch/x86/pv/pvvar.h>
+#endif
/*
* XXXfvdl ACPI
@@ -100,6 +104,9 @@ union amd64_mainbus_attach_args {
#if NIPMI > 0
struct ipmi_attach_args mba_ipmi;
#endif
+#if NPVBUS > 0
+ struct pvbus_attach_args mba_pvba;
+#endif
};
/*
@@ -155,7 +162,7 @@ amd64_mainbus_match(device_t parent, cfd
void
amd64_mainbus_attach(device_t parent, device_t self, void *aux)
{
-#if NISA > 0 || NPCI > 0 || NACPICA > 0 || NIPMI > 0
+#if NISA > 0 || NPCI > 0 || NACPICA > 0 || NIPMI > 0 || NPVBUS > 0
union amd64_mainbus_attach_args mba;
#endif
@@ -237,6 +244,21 @@ amd64_mainbus_attach(device_t parent, de
}
#endif
+#if NPVBUS > 0
+ /* add here more VM guests types that would benefit from a pv bus */
+ switch(vm_guest) {
+ /* FALLTHROUGH */
+ case VM_GUEST_GENPVH:
+ case VM_GUEST_KVM:
+ mba.mba_pvba.pvba_busname = "pvbus";
+ config_found(self, &mba.mba_pvba.pvba_busname, NULL,
+ CFARGS(.iattr = "pvbus"));
+ break;
+ default:
+ break;
+ }
+#endif
+
if (!pmf_device_register(self, NULL, NULL))
aprint_error_dev(self, "couldn't establish power handler\n");
}
Index: src/sys/arch/amd64/conf/files.amd64
diff -u src/sys/arch/amd64/conf/files.amd64:1.123 src/sys/arch/amd64/conf/files.amd64:1.124
--- src/sys/arch/amd64/conf/files.amd64:1.123 Fri Feb 23 22:10:39 2024
+++ src/sys/arch/amd64/conf/files.amd64 Thu Jan 2 10:34:33 2025
@@ -1,4 +1,4 @@
-# $NetBSD: files.amd64,v 1.123 2024/02/23 22:10:39 andvar Exp $
+# $NetBSD: files.amd64,v 1.124 2025/01/02 10:34:33 imil Exp $
#
# new style config file for amd64 architecture
#
@@ -94,7 +94,7 @@ include "dev/i2o/files.i2o"
# XXX BIOS32 only if something that uses it is configured!
device mainbus: isabus, pcibus, bios32, acpibus, cpubus, ioapicbus,
- ipmibus, hypervisorbus
+ ipmibus, hypervisorbus, pvbus
attach mainbus at root
file arch/amd64/amd64/amd64_mainbus.c mainbus & !xenpv
file arch/x86/x86/mainbus.c mainbus
@@ -200,4 +200,7 @@ file dev/acpi/vmbus_acpi.c vmbus_acpi
# VMEbus support
include "dev/vme/files.vme"
+# PVbus support
+include "arch/x86/pv/files.pv"
+
include "arch/amd64/conf/majors.amd64"
Added files:
Index: src/share/man/man4/pv.4
diff -u /dev/null src/share/man/man4/pv.4:1.1
--- /dev/null Thu Jan 2 10:34:33 2025
+++ src/share/man/man4/pv.4 Thu Jan 2 10:34:33 2025
@@ -0,0 +1,43 @@
+.\" $NetBSD: pv.4,v 1.1 2025/01/02 10:34:33 imil Exp $
+.\"
+.\" Copyright (c) 2024 The NetBSD Foundation, Inc.
+.\" All rights reserved.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\" notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\" notice, this list of conditions and the following disclaimer in the
+.\" documentation and/or other materials provided with the distribution.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+.\" POSSIBILITY OF SUCH DAMAGE.
+.\"
+.\" ported from OpenBSD
+.Dd January 2024
+.Dt PV 4
+.Os
+.Sh NAME
+.Nm pv
+.Nd paravirtual device tree root
+.Sh SYNOPSIS
+.Cd "pv* at pvbus?"
+.Sh DESCRIPTION
+.Nm
+driver is used on virtual machines that are running on hypervisors.
+It provides a pseudo-bus for all paravirtual devices that do not
+attach to a well-known bus like
+.Xr pci 4 .
+.Sh SEE ALSO
+.Xr virtio 4
Index: src/sys/arch/x86/pv/files.pv
diff -u /dev/null src/sys/arch/x86/pv/files.pv:1.1
--- /dev/null Thu Jan 2 10:34:33 2025
+++ src/sys/arch/x86/pv/files.pv Thu Jan 2 10:34:33 2025
@@ -0,0 +1,5 @@
+define pvbus {}
+
+device pv {}
+attach pv at pvbus
+file arch/x86/pv/pvbus.c pvbus needs-flag
Index: src/sys/arch/x86/pv/pvbus.c
diff -u /dev/null src/sys/arch/x86/pv/pvbus.c:1.1
--- /dev/null Thu Jan 2 10:34:33 2025
+++ src/sys/arch/x86/pv/pvbus.c Thu Jan 2 10:34:33 2025
@@ -0,0 +1,100 @@
+/* $NetBSD: pvbus.c,v 1.1 2025/01/02 10:34:33 imil Exp $ */
+
+/*-
+ * Copyright (c) 2024 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Emile 'iMil' Heitor.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/device.h>
+#include <sys/kernel.h>
+#include <sys/systm.h>
+
+#include <machine/bus_private.h>
+
+#include <arch/x86/pv/pvvar.h>
+
+static int _pv_dma_may_bounce(bus_dma_tag_t, bus_dmamap_t, int, int *);
+static int pv_match(device_t, cfdata_t, void *);
+static void pv_attach(device_t, device_t, void *);
+static int pv_submatch(device_t, cfdata_t, const int *, void *);
+
+struct x86_bus_dma_tag pvbus_bus_dma_tag = {
+ ._tag_needs_free = 0,
+ ._bounce_thresh = 0,
+ ._bounce_alloc_lo = 0,
+ ._bounce_alloc_hi = 0,
+ ._may_bounce = _pv_dma_may_bounce,
+};
+
+CFATTACH_DECL_NEW(pv, sizeof(struct pv_softc),
+ pv_match, pv_attach, NULL, NULL);
+
+static int
+_pv_dma_may_bounce(bus_dma_tag_t t, bus_dmamap_t map, int flags,
+ int *cookieflagsp)
+{
+ if ((map->_dm_size / PAGE_SIZE) > map->_dm_segcnt)
+ *cookieflagsp |= X86_DMA_MIGHT_NEED_BOUNCE;
+
+ return 0;
+}
+
+
+static int
+pv_match(device_t parent, cfdata_t match, void *aux)
+{
+ return 1;
+}
+
+static void
+pv_attach(device_t parent, device_t self, void *aux)
+{
+ struct pv_attach_args pvaa;
+
+ pvaa.pvaa_memt = x86_bus_space_mem;
+ pvaa.pvaa_dmat = &pvbus_bus_dma_tag;
+
+ aprint_naive("\n");
+ aprint_normal("\n");
+
+ config_found(self, &pvaa, NULL, CFARGS(.search = pv_submatch));
+}
+
+static int
+pv_submatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
+{
+ struct pv_attach_args *pvaa = aux;
+
+ if (config_probe(parent, cf, pvaa)) {
+ config_attach(parent, cf, pvaa, NULL, CFARGS_NONE);
+ return 0;
+ }
+ return 0;
+}
Index: src/sys/arch/x86/pv/pvvar.h
diff -u /dev/null src/sys/arch/x86/pv/pvvar.h:1.1
--- /dev/null Thu Jan 2 10:34:33 2025
+++ src/sys/arch/x86/pv/pvvar.h Thu Jan 2 10:34:33 2025
@@ -0,0 +1,48 @@
+/* $NetBSD: pvvar.h,v 1.1 2025/01/02 10:34:33 imil Exp $ */
+
+/*-
+ * Copyright (c) 2024 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Emile 'iMil' Heitor.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _PVBUS_PVVAR_H_
+#define _PVBUS_PVVAR_H_
+
+struct pv_softc {
+ device_t sc_dev;
+};
+
+struct pvbus_attach_args {
+ const char *pvba_busname;
+};
+
+struct pv_attach_args {
+ bus_space_tag_t pvaa_memt;
+ bus_dma_tag_t pvaa_dmat;
+};
+
+#endif