Index: sys/arm64/include/xen/xen-os.h =================================================================== --- sys/arm64/include/xen/xen-os.h +++ sys/arm64/include/xen/xen-os.h @@ -39,6 +39,12 @@ #ifndef __ASSEMBLY__ +/* Right now the device-tree is the only implemented detection method */ +#define xen_early_probe() xen_dt_probe() + +/* Early initializer, returns success/failure identical to xen_domain() */ +extern int xen_dt_probe(void); + #define PCPU_ID_GET(cpu) (cpu) static inline bool Index: sys/conf/files =================================================================== --- sys/conf/files +++ sys/conf/files @@ -3535,6 +3535,7 @@ dev/xen/bus/xenpv.c optional xenhvm dev/xen/console/xen_console.c optional xenhvm dev/xen/control/control.c optional xenhvm +dev/xen/devicetree/xen-dt.c optional xenhvm fdt dev/xen/efi/pvefi.c optional xenhvm efirt dev/xen/grant_table/grant_table.c optional xenhvm dev/xen/netback/netback.c optional xenhvm Index: sys/dev/xen/devicetree/xen-dt.c =================================================================== --- /dev/null +++ sys/dev/xen/devicetree/xen-dt.c @@ -0,0 +1,172 @@ +/* + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2014 Julien Grall + * Copyright (c) 2021 Elliott Mitchell + * + * 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 AUTHOR 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 AUTHOR 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include + +#include +#include +#include +#include +#include + + +DPCPU_DEFINE(struct vcpu_info, vcpu_local_info); +DPCPU_DEFINE(struct vcpu_info *, vcpu_info); + +int +xen_dt_probe(void) +{ + /* + * Short-circuit extra attempts at looking for Xen + */ + if (xen_domain()) + return (1); + + /* + * The device tree contains the node /hypervisor with the compatible + * string "xen,xen" when the OS will run on top of Xen. + */ + if (ofw_bus_node_is_compatible(OF_finddevice("/hypervisor"), "xen,xen") == 0) + return (0); + + xen_domain_type = XEN_HVM_DOMAIN; + vm_guest = VM_GUEST_XEN; + setup_xen_features(); + + return (1); +} + +/* in case of console being disabled, probe again as a fallback */ +C_SYSINIT(xen_probe_fdt, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, (sysinit_cfunc_t)xen_dt_probe, NULL); + +/* xen_handle_shared_info() won't work during console probe, thus this */ +C_SYSINIT(xen_shared_info, SI_SUB_HYPERVISOR, SI_ORDER_SECOND, (sysinit_cfunc_t)xen_handle_shared_info, NULL); + + +struct xen_softc { + struct resource *intr; + void *cookie; + int rid; +}; + +struct xen_softc *xen_sc = NULL; + +static int +xen_probe(device_t dev) +{ + if (!ofw_bus_status_okay(dev)) + return (ENXIO); + + if (!ofw_bus_is_compatible(dev, "xen,xen")) + return (ENXIO); + + device_set_desc(dev, "Xen ARM device-tree"); + + return (BUS_PROBE_DEFAULT); +} + +static int +xen_attach(device_t dev) +{ + struct xen_softc *sc; + struct vcpu_register_vcpu_info info; + struct vcpu_info *vcpu_info; + vm_paddr_t phys; + int rc, cpu; + + if (xen_sc != NULL) + return (ENXIO); + + sc = device_get_softc(dev); + + /* TODO: Move to a proper function */ + vcpu_info = DPCPU_PTR(vcpu_local_info); + cpu = PCPU_GET(cpuid); + phys = vtophys(vcpu_info); + info.mfn = phys >> PAGE_SHIFT_4K; + info.offset = phys & PAGE_MASK_4K; + + rc = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info); + KASSERT(rc == 0, ("Unable to register cpu %u\n", cpu)); + DPCPU_SET(vcpu_info, vcpu_info); + + + /* Resources */ + sc->intr = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->rid, RF_ACTIVE); + if (sc->intr == NULL) { + panic("Unable to retrieve Xen event channel interrupt"); + return (ENXIO); + } + + xen_sc = sc; + + /* Setup and enable the event channel interrupt */ + if (bus_setup_intr(dev, sc->intr, INTR_TYPE_MISC|INTR_MPSAFE, + xen_intr_handle_upcall, NULL, sc, &sc->cookie)) { + panic("Could not setup event channel interrupt"); + return (ENXIO); + } + + return (0); +} + +static device_method_t xen_methods[] = { + DEVMETHOD(device_probe, xen_probe), + DEVMETHOD(device_attach, xen_attach), + { 0, 0 } +}; + +static driver_t xen_driver = { + "xen-dt", + xen_methods, + sizeof(struct xen_softc), +}; + +static devclass_t xen_devclass; + +DRIVER_MODULE(xen, simplebus, xen_driver, xen_devclass, 0, 0); +DRIVER_MODULE(xen, ofwbus, xen_driver, xen_devclass, 0, 0);