diff --git a/share/man/man4/pv.4 b/share/man/man4/pv.4 new file mode 100644 index 00000000000..f25ba4a2475 --- /dev/null +++ b/share/man/man4/pv.4 @@ -0,0 +1,44 @@ +.\" $NetBSD$ +.\" +.\" 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 , diff --git a/sys/arch/amd64/amd64/amd64_mainbus.c b/sys/arch/amd64/amd64/amd64_mainbus.c index 8a1bb0c55c6..dd83d22fe53 100644 --- a/sys/arch/amd64/amd64/amd64_mainbus.c +++ b/sys/arch/amd64/amd64/amd64_mainbus.c @@ -50,6 +50,7 @@ __KERNEL_RCSID(0, "$NetBSD: amd64_mainbus.c,v 1.7 2021/08/07 16:18:41 thorpej Ex #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_mainbus.c,v 1.7 2021/08/07 16:18:41 thorpej Ex #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, cfdata_t match, void *aux) 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, device_t self, void *aux) } #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"); } diff --git a/sys/arch/amd64/conf/files.amd64 b/sys/arch/amd64/conf/files.amd64 index 98333e80a65..43a5fe775f9 100644 --- a/sys/arch/amd64/conf/files.amd64 +++ b/sys/arch/amd64/conf/files.amd64 @@ -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" diff --git a/sys/arch/x86/pv/files.pv b/sys/arch/x86/pv/files.pv new file mode 100644 index 00000000000..21954a0b55c --- /dev/null +++ b/sys/arch/x86/pv/files.pv @@ -0,0 +1,12 @@ +define pvbus {} + +device pv {} +attach pv at pvbus +file arch/x86/pv/pvbus.c pvbus needs-flag diff --git a/sys/arch/x86/pv/pvbus.c b/sys/arch/x86/pv/pvbus.c new file mode 100644 index 00000000000..b2c0e93d5b1 --- /dev/null +++ b/sys/arch/x86/pv/pvbus.c @@ -0,0 +1,100 @@ +/* $NetBSD$ */ + +/*- + * 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; +} diff --git a/sys/arch/x86/pv/pvvar.h b/sys/arch/x86/pv/pvvar.h new file mode 100644 index 00000000000..bba44b926dc --- /dev/null +++ b/sys/arch/x86/pv/pvvar.h @@ -0,0 +1,48 @@ +/* $NetBSD$ */ + +/*- + * 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