From 262459806433f838bb2e54cc4733c0887328a170 Mon Sep 17 00:00:00 2001 From: Jessica Clarke Date: Sat, 7 Aug 2021 19:27:32 +0100 Subject: [PATCH] pci_pci: Support growing windows in bus_adjust_resource for NEW_PCIB If we allocate a new window for a bridge rather than reusing an existing one set up by firmware to cover all the devices then the new window only includes the range needed for the first device to allocate the resource. If a request comes in to adjust this resource in order to extend a downstream window for another device then this will fail as the rman doesn't have any space, so we must first grow the bridge's own window. This is needed to support successfully attaching more than one PCI device on SiFive's HiFive Unmatched, which has the following topology: Root Port <---> Bridge <---> Bridge <-+-> Bridge <---> (Unused) (pcib0) (pcib1) (pcib2) | (pcib3) +-> Bridge <---> xHCI | (pcib4) +-> Bridge <---> M.2 E-key | (pcib5) +-> Bridge <---> M.2 M-key | (pcib6) +-> Bridge <---> x16 slot (pcib7) Without this, the xHCI endpoint successfully attaches but NVMe M.2 M-key endpoint fails to attach as, when its adjacent bridge (pcib6) attempts to allocate a window from its parent (pcib2) on the other side of the switch, its parent attempts to grow its own window by calling bus_adjust_resource on its own parent (pcib1) which fails to call the root port device (pcib0) to request more memory to grow its own window. Had the root port been directly connected to the switch without the bridge in the middle then the existing code would have worked, but the extra hop broke it. Reviewed by: jhb MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D31035 --- sys/dev/pci/pci_pci.c | 77 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 15 deletions(-) diff --git a/sys/dev/pci/pci_pci.c b/sys/dev/pci/pci_pci.c index d6fbb06a61ac..83106eaa455b 100644 --- a/sys/dev/pci/pci_pci.c +++ b/sys/dev/pci/pci_pci.c @@ -148,6 +148,30 @@ static int pci_clear_pcib; SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0, "Clear firmware-assigned resources for PCI-PCI bridge I/O windows."); +/* + * Get the corresponding window if this resource from a child device was + * sub-allocated from one of our window resource managers. + */ +static struct pcib_window * +pcib_get_resource_window(struct pcib_softc *sc, int type, struct resource *r) +{ + switch (type) { + case SYS_RES_IOPORT: + if (rman_is_region_manager(r, &sc->io.rman)) + return (&sc->io); + break; + case SYS_RES_MEMORY: + /* Prefetchable resources may live in either memory rman. */ + if (rman_get_flags(r) & RF_PREFETCHABLE && + rman_is_region_manager(r, &sc->pmem.rman)) + return (&sc->pmem); + if (rman_is_region_manager(r, &sc->mem.rman)) + return (&sc->mem); + break; + } + return (NULL); +} + /* * Is a resource from a child device sub-allocated from one of our * resource managers? @@ -156,21 +180,11 @@ static int pcib_is_resource_managed(struct pcib_softc *sc, int type, struct resource *r) { - switch (type) { #ifdef PCI_RES_BUS - case PCI_RES_BUS: + if (type == PCI_RES_BUS) return (rman_is_region_manager(r, &sc->bus.rman)); #endif - case SYS_RES_IOPORT: - return (rman_is_region_manager(r, &sc->io.rman)); - case SYS_RES_MEMORY: - /* Prefetchable resources may live in either memory rman. */ - if (rman_get_flags(r) & RF_PREFETCHABLE && - rman_is_region_manager(r, &sc->pmem.rman)) - return (1); - return (rman_is_region_manager(r, &sc->mem.rman)); - } - return (0); + return (pcib_get_resource_window(sc, type, r) != NULL); } static int @@ -2330,11 +2344,44 @@ pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r, rman_res_t start, rman_res_t end) { struct pcib_softc *sc; + struct pcib_window *w; + int error; sc = device_get_softc(bus); - if (pcib_is_resource_managed(sc, type, r)) - return (rman_adjust_resource(r, start, end)); - return (bus_generic_adjust_resource(bus, child, type, r, start, end)); + + /* + * If the resource wasn't sub-allocated from one of our region + * managers then just pass the request up. + */ + if (!pcib_is_resource_managed(sc, type, r)) + return (bus_generic_adjust_resource(bus, child, type, r, + start, end)); + +#ifdef PCI_RES_BUS + if (type != PCI_RES_BUS) +#endif + { + /* + * Resource is managed and not a secondary bus number, must + * be from one of our windows. + */ + w = pcib_get_resource_window(sc, type, r); + KASSERT(w != NULL, + ("%s: no window for resource (%#jx-%#jx) type %d", + __func__, rman_get_start(r), rman_get_end(r), type)); + + /* + * If our window isn't big enough to grow the sub-allocation + * then we need to expand the window. + */ + if (start < w->base || end > w->limit) { + error = pcib_expand_window(sc, w, type, start, end); + if (error != 0) + return (error); + } + } + + return (rman_adjust_resource(r, start, end)); } int