Index: sys/arm64/cavium/thunder_pcie_acpi.c =================================================================== --- /dev/null +++ sys/arm64/cavium/thunder_pcie_acpi.c @@ -0,0 +1,121 @@ +/*- + * Copyright (c) 2016 The FreeBSD Foundation + * All rights reserved. + * + * This software was developed by Andrew Turner under + * sponsorship from the FreeBSD Foundation. + * + * 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 "opt_platform.h" + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include +#include +#include +#include + +#include "thunder_pcie_common.h" + +#include "pcib_if.h" + +DECLARE_CLASS(generic_pcie_acpi_driver); +int pci_host_generic_acpi_attach(device_t); + +static device_probe_t thunder_pcie_acpi_probe; +static device_attach_t thunder_pcie_acpi_attach; + +static device_method_t thunder_pcie_acpi_methods[] = { + /* Device interface */ + DEVMETHOD(device_probe, thunder_pcie_acpi_probe), + DEVMETHOD(device_attach, thunder_pcie_acpi_attach), + + /* End */ + DEVMETHOD_END +}; + +DEFINE_CLASS_1(pcib, thunder_pcie_acpi_driver, thunder_pcie_acpi_methods, + sizeof(struct generic_pcie_core_softc), generic_pcie_acpi_driver); + +static devclass_t thunder_pcie_acpi_devclass; + +DRIVER_MODULE(thunder_pcib, acpi, thunder_pcie_acpi_driver, + thunder_pcie_acpi_devclass, 0, 0); + +static int +thunder_pcie_acpi_probe(device_t dev) +{ + ACPI_DEVICE_INFO *devinfo; + ACPI_HANDLE h; + int root, uid; + + /* Check if we're running on Cavium ThunderX */ + if (!CPU_MATCH(CPU_IMPL_MASK | CPU_PART_MASK, + CPU_IMPL_CAVIUM, CPU_PART_THUNDER, 0, 0)) + return (ENXIO); + + if (acpi_disabled("pcib") || + (h = acpi_get_handle(dev)) == NULL || + ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)) || + ACPI_FAILURE(acpi_GetInteger(h, "_UID", &uid))) + return (ENXIO); + root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0; + AcpiOsFree(devinfo); + if (!root) + return (ENXIO); + + device_set_desc(dev, "Cavium Integrated PCI/PCI-E Controller"); + return (BUS_PROBE_DEFAULT); +} + +static int +thunder_pcie_acpi_attach(device_t dev) +{ + struct generic_pcie_core_softc *sc; + + sc = device_get_softc(dev); + thunder_pcie_identify_ecam(dev, &sc->ecam); + sc->coherent = 1; + + return (pci_host_generic_acpi_attach(dev)); +} Index: sys/arm64/cavium/thunder_pcie_pem_acpi.c =================================================================== --- /dev/null +++ sys/arm64/cavium/thunder_pcie_pem_acpi.c @@ -0,0 +1,178 @@ +/*- + * Copyright (c) 2016 The FreeBSD Foundation + * All rights reserved. + * + * This software was developed by Andrew Turner under + * sponsorship from the FreeBSD Foundation. + * + * 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 "opt_platform.h" + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include +#include +#include +#include + +#include + +#include "thunder_pcie_common.h" +#include "thunder_pcie_pem.h" + +#include "pcib_if.h" + +static device_probe_t thunder_pem_acpi_probe; +static pcib_alloc_msix_t thunder_pem_acpi_alloc_msix; +static pcib_release_msix_t thunder_pem_acpi_release_msix; +static pcib_alloc_msi_t thunder_pem_acpi_alloc_msi; +static pcib_release_msi_t thunder_pem_acpi_release_msi; +static pcib_map_msi_t thunder_pem_acpi_map_msi; +static pcib_get_id_t thunder_pem_acpi_get_id; + +static device_method_t thunder_pem_acpi_methods[] = { + /* Device interface */ + DEVMETHOD(device_probe, thunder_pem_acpi_probe), + + /* pcib interface */ + DEVMETHOD(pcib_alloc_msix, thunder_pem_acpi_alloc_msix), + DEVMETHOD(pcib_release_msix, thunder_pem_acpi_release_msix), + DEVMETHOD(pcib_alloc_msi, thunder_pem_acpi_alloc_msi), + DEVMETHOD(pcib_release_msi, thunder_pem_acpi_release_msi), + DEVMETHOD(pcib_map_msi, thunder_pem_acpi_map_msi), + DEVMETHOD(pcib_get_id, thunder_pem_acpi_get_id), + + /* End */ + DEVMETHOD_END +}; + +DEFINE_CLASS_1(pcib, thunder_pem_acpi_driver, thunder_pem_acpi_methods, + sizeof(struct thunder_pem_softc), thunder_pem_driver); + +static devclass_t thunder_pem_acpi_devclass; + +DRIVER_MODULE(thunder_pem, acpi, thunder_pem_acpi_driver, + thunder_pem_acpi_devclass, 0, 0); + +static int +thunder_pem_acpi_probe(device_t dev) +{ + ACPI_DEVICE_INFO *devinfo; + ACPI_HANDLE h; + int root, uid; + + /* Check if we're running on Cavium ThunderX */ + if (!CPU_MATCH(CPU_IMPL_MASK | CPU_PART_MASK, + CPU_IMPL_CAVIUM, CPU_PART_THUNDER, 0, 0)) + return (ENXIO); + + if (acpi_disabled("pcib") || + (h = acpi_get_handle(dev)) == NULL || + ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)) || + ACPI_FAILURE(acpi_GetInteger(h, "_UID", &uid))) + return (ENXIO); + root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0; + AcpiOsFree(devinfo); + if (!root) + return (ENXIO); + + device_set_desc(dev, THUNDER_PEM_DESC); + return (BUS_PROBE_DEFAULT); +} + +static int +thunder_pem_acpi_alloc_msi(device_t pci, device_t child, int count, + int maxcount, int *irqs) +{ + + return (intr_alloc_msi(pci, child, 0, count, maxcount, irqs)); +} + +static int +thunder_pem_acpi_release_msi(device_t pci, device_t child, int count, int *irqs) +{ + + return (intr_release_msi(pci, child, 0, count, irqs)); +} + +static int +thunder_pem_acpi_alloc_msix(device_t pci, device_t child, int *irq) +{ + + return (intr_alloc_msix(pci, child, 0, irq)); +} + +static int +thunder_pem_acpi_release_msix(device_t pci, device_t child, int irq) +{ + + return (intr_release_msix(pci, child, 0, irq)); +} + +static int +thunder_pem_acpi_map_msi(device_t pci, device_t child, int irq, uint64_t *addr, + uint32_t *data) +{ + + return (intr_map_msi(pci, child, 0, irq, addr, data)); +} + +static int +thunder_pem_acpi_get_id(device_t dev, device_t child, enum pci_id_type type, + uintptr_t *id) +{ + int err; + + if (type == PCI_ID_MSI) { + err = pcib_get_id(dev, child, PCI_ID_RID, id); + if (err != 0) + return (err); + *id |= 3 << 16; + return (0); + } + + if (type != PCI_ID_MSI) + return (pcib_get_id(dev, child, type, id)); + + return (0); +} Index: sys/conf/files.arm64 =================================================================== --- sys/conf/files.arm64 +++ sys/conf/files.arm64 @@ -115,8 +115,10 @@ arm64/arm64/unwind.c optional ddb | kdtrace_hooks | stack arm64/arm64/vfp.c standard arm64/arm64/vm_machdep.c standard +arm64/cavium/thunder_pcie_acpi.c optional soc_cavm_thunderx pci acpi arm64/cavium/thunder_pcie_fdt.c optional soc_cavm_thunderx pci fdt arm64/cavium/thunder_pcie_pem.c optional soc_cavm_thunderx pci +arm64/cavium/thunder_pcie_pem_acpi.c optional soc_cavm_thunderx pci acpi arm64/cavium/thunder_pcie_pem_fdt.c optional soc_cavm_thunderx pci fdt arm64/cavium/thunder_pcie_common.c optional soc_cavm_thunderx pci arm64/cloudabi64/cloudabi64_sysvec.c optional compat_cloudabi64 @@ -158,6 +160,7 @@ dev/ofw/ofw_cpu.c optional fdt dev/ofw/ofwpci.c optional fdt pci dev/pci/pci_host_generic.c optional pci +dev/pci/pci_host_generic_acpi.c optional pci acpi dev/pci/pci_host_generic_fdt.c optional pci fdt dev/psci/psci.c optional psci dev/psci/psci_arm64.S optional psci Index: sys/dev/acpica/acpi.c =================================================================== --- sys/dev/acpica/acpi.c +++ sys/dev/acpica/acpi.c @@ -1901,6 +1901,29 @@ alloc++; } } +#elif defined(__aarch64__) +static void +acpi_enable_pcie(device_t child, int segment) +{ + ACPI_TABLE_HEADER *hdr; + ACPI_MCFG_ALLOCATION *alloc, *end; + ACPI_STATUS status; + + status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr); + if (ACPI_FAILURE(status)) + return; + + end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length); + alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1); + while (alloc < end) { + if (alloc->PciSegment == segment) { + bus_set_resource(child, SYS_RES_MEMORY, 0, + alloc->Address, 0x40000000); + return; + } + alloc++; + } +} #endif /* @@ -1990,6 +2013,10 @@ static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) { +#ifdef __aarch64__ + ACPI_DEVICE_INFO *devinfo; + int segment; +#endif struct acpi_prw_data prw; ACPI_OBJECT_TYPE type; ACPI_HANDLE h; @@ -2083,6 +2110,15 @@ * device not to have any resources. */ acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); +#ifdef __aarch64__ + AcpiGetObjectInfo(handle, &devinfo); + if ((devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0) { + if (ACPI_SUCCESS(acpi_GetInteger(handle, "_SEG", &segment))) { + acpi_enable_pcie(child, segment); + } + } + AcpiOsFree(devinfo); +#endif break; } } Index: sys/dev/pci/pci_host_generic_acpi.c =================================================================== --- /dev/null +++ sys/dev/pci/pci_host_generic_acpi.c @@ -0,0 +1,326 @@ +/*- + * Copyright (c) 2015 Ruslan Bukin + * Copyright (c) 2014 The FreeBSD Foundation + * All rights reserved. + * + * This software was developed by Semihalf under + * the sponsorship of the FreeBSD Foundation. + * + * 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. + */ + +/* Generic ECAM PCIe driver */ + +#include +__FBSDID("$FreeBSD$"); + +#include "opt_platform.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include +#include +#include +#include + +#include +#include +#include + +#include "pcib_if.h" + +int pci_host_generic_acpi_attach(device_t); + +/* Assembling ECAM Configuration Address */ +#define PCIE_BUS_SHIFT 20 +#define PCIE_SLOT_SHIFT 15 +#define PCIE_FUNC_SHIFT 12 +#define PCIE_BUS_MASK 0xFF +#define PCIE_SLOT_MASK 0x1F +#define PCIE_FUNC_MASK 0x07 +#define PCIE_REG_MASK 0xFFF + +#define PCIE_ADDR_OFFSET(bus, slot, func, reg) \ + ((((bus) & PCIE_BUS_MASK) << PCIE_BUS_SHIFT) | \ + (((slot) & PCIE_SLOT_MASK) << PCIE_SLOT_SHIFT) | \ + (((func) & PCIE_FUNC_MASK) << PCIE_FUNC_SHIFT) | \ + ((reg) & PCIE_REG_MASK)) + +#define PCI_IO_WINDOW_OFFSET 0x1000 + +#define SPACE_CODE_SHIFT 24 +#define SPACE_CODE_MASK 0x3 +#define SPACE_CODE_IO_SPACE 0x1 +#define PROPS_CELL_SIZE 1 +#define PCI_ADDR_CELL_SIZE 2 + +/* Forward prototypes */ + +static int generic_pcie_acpi_probe(device_t dev); +static uint32_t generic_pcie_acpi_read_config(device_t dev, u_int bus, + u_int slot, u_int func, u_int reg, int bytes); +static void generic_pcie_acpi_write_config(device_t dev, u_int bus, u_int slot, + u_int func, u_int reg, uint32_t val, int bytes); +static int generic_pcie_acpi_release_resource(device_t dev, device_t child, + int type, int rid, struct resource *res); + +static int +generic_pcie_acpi_probe(device_t dev) +{ + ACPI_DEVICE_INFO *devinfo; + ACPI_HANDLE h; + int root; + + if (acpi_disabled("pcib") || (h = acpi_get_handle(dev)) == NULL || + ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo))) + return (ENXIO); + root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0; + AcpiOsFree(devinfo); + if (!root) + return (ENXIO); + + device_set_desc(dev, "Generic PCI host controller"); + return (BUS_PROBE_GENERIC); +} + +int +pci_host_generic_acpi_attach(device_t dev) +{ + struct generic_pcie_core_softc *sc; + uint32_t cca; + int error; + + sc = device_get_softc(dev); + + /* Check if the device is cache-coherent */ + if (ACPI_SUCCESS(acpi_GetInteger(acpi_get_handle(dev), "_CCA", &cca))) + sc->coherent = cca; + + error = pci_host_generic_core_attach(dev); + if (error != 0) + return (error); + + device_add_child(dev, "pci", -1); + return (bus_generic_attach(dev)); +} + +static int +generic_pcie_acpi_route_interrupt(device_t bus, device_t dev, int pin) +{ + + /* TODO: Read the PCI Routing Table (_PRT) for this information */ + return (PCI_INVALID_IRQ); +} + +static struct rman * +generic_pcie_acpi_rman(struct generic_pcie_core_softc *sc, int type) +{ + + switch (type) { + case SYS_RES_IOPORT: + return (&sc->io_rman); + case SYS_RES_MEMORY: + return (&sc->mem_rman); + default: + break; + } + + return (NULL); +} + +static struct resource * +pci_host_generic_acpi_alloc_resource(device_t dev, device_t child, int type, + int *rid, rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) +{ +#if defined(NEW_PCIB) && defined(PCI_RES_BUS) + struct generic_pcie_core_softc *sc; + + if (type == PCI_RES_BUS) { + sc = device_get_softc(dev); + return (pci_domain_alloc_bus(sc->ecam, child, rid, start, end, + count, flags)); + } +#endif + + return (bus_generic_alloc_resource(dev, child, type, rid, start, end, + count, flags)); +} + +static int +generic_pcie_acpi_activate_resource(device_t dev, device_t child, int type, + int rid, struct resource *r) +{ + struct generic_pcie_core_softc *sc; + int res; + + sc = device_get_softc(dev); + + if ((res = rman_activate_resource(r)) != 0) + return (res); + + return (bus_generic_activate_resource(dev, child, type, rid, r)); +} + +static int +generic_pcie_acpi_deactivate_resource(device_t dev, device_t child, int type, + int rid, struct resource *r) +{ + int res; + + if ((res = rman_deactivate_resource(r)) != 0) + return (res); + + switch(type) { + case SYS_RES_IOPORT: + case SYS_RES_MEMORY: + res = BUS_DEACTIVATE_RESOURCE(device_get_parent(dev), child, + type, rid, r); + break; + default: + break; + } + + return (res); +} + +static int +generic_pcie_acpi_alloc_msi(device_t pci, device_t child, int count, + int maxcount, int *irqs) +{ + +#if defined(INTRNG) + return (intr_alloc_msi(pci, child, 1, count, maxcount, irqs)); +#else + return (ENXIO); +#endif +} + +static int +generic_pcie_acpi_release_msi(device_t pci, device_t child, int count, + int *irqs) +{ + +#if defined(INTRNG) + return (intr_release_msi(pci, child, 1, count, irqs)); +#else + return (ENXIO); +#endif +} + +static int +generic_pcie_acpi_map_msi(device_t pci, device_t child, int irq, uint64_t *addr, + uint32_t *data) +{ + +#if defined(INTRNG) + return (intr_map_msi(pci, child, 1, irq, addr, data)); +#else + return (ENXIO); +#endif +} + +static int +generic_pcie_acpi_alloc_msix(device_t pci, device_t child, int *irq) +{ + +#if defined(INTRNG) + return (intr_alloc_msix(pci, child, 1, irq)); +#else + return (ENXIO); +#endif +} + +static int +generic_pcie_acpi_release_msix(device_t pci, device_t child, int irq) +{ + +#if defined(INTRNG) + return (intr_release_msix(pci, child, 1, irq)); +#else + return (ENXIO); +#endif +} + +static int +generic_pcie_acpi_get_id(device_t pci, device_t child, enum pci_id_type type, + uintptr_t *id) +{ + struct generic_pcie_core_softc *sc; + int err; + + /* Use the PCI RID to find the MSI ID */ + if (type == PCI_ID_MSI) { + sc = device_get_softc(pci); + type = PCI_ID_RID; + err = pcib_get_id(pci, child, type, id); + if (err != 0) + return (err); + *id |= sc->ecam << 16; + return (0); + } + + return (pcib_get_id(pci, child, type, id)); +} + +static device_method_t generic_pcie_acpi_methods[] = { + /* Device interface */ + DEVMETHOD(device_probe, generic_pcie_acpi_probe), + DEVMETHOD(device_attach, pci_host_generic_acpi_attach), + + /* Bus interface */ + DEVMETHOD(bus_alloc_resource, pci_host_generic_acpi_alloc_resource), + DEVMETHOD(bus_activate_resource, generic_pcie_acpi_activate_resource), + DEVMETHOD(bus_deactivate_resource, generic_pcie_acpi_deactivate_resource), + + /* pcib interface */ + DEVMETHOD(pcib_route_interrupt, generic_pcie_acpi_route_interrupt), + DEVMETHOD(pcib_alloc_msi, generic_pcie_acpi_alloc_msi), + DEVMETHOD(pcib_release_msi, generic_pcie_acpi_release_msi), + DEVMETHOD(pcib_alloc_msix, generic_pcie_acpi_alloc_msix), + DEVMETHOD(pcib_release_msix, generic_pcie_acpi_release_msix), + DEVMETHOD(pcib_map_msi, generic_pcie_acpi_map_msi), + DEVMETHOD(pcib_get_id, generic_pcie_acpi_get_id), + + DEVMETHOD_END +}; + +DEFINE_CLASS_1(pcib, generic_pcie_acpi_driver, generic_pcie_acpi_methods, + sizeof(struct generic_pcie_core_softc), generic_pcie_core_driver); + +static devclass_t generic_pcie_acpi_devclass; + +DRIVER_MODULE(pcib, acpi, generic_pcie_acpi_driver, generic_pcie_acpi_devclass, + 0, 0);