1bb0d0a8eSMike Smith /*-
2718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3718cf2ccSPedro F. Giffuni *
4bb0d0a8eSMike Smith * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
5bb0d0a8eSMike Smith * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
6bb0d0a8eSMike Smith * Copyright (c) 2000 BSDi
7bb0d0a8eSMike Smith * All rights reserved.
8bb0d0a8eSMike Smith *
9bb0d0a8eSMike Smith * Redistribution and use in source and binary forms, with or without
10bb0d0a8eSMike Smith * modification, are permitted provided that the following conditions
11bb0d0a8eSMike Smith * are met:
12bb0d0a8eSMike Smith * 1. Redistributions of source code must retain the above copyright
13bb0d0a8eSMike Smith * notice, this list of conditions and the following disclaimer.
14bb0d0a8eSMike Smith * 2. Redistributions in binary form must reproduce the above copyright
15bb0d0a8eSMike Smith * notice, this list of conditions and the following disclaimer in the
16bb0d0a8eSMike Smith * documentation and/or other materials provided with the distribution.
17bb0d0a8eSMike Smith * 3. The name of the author may not be used to endorse or promote products
18bb0d0a8eSMike Smith * derived from this software without specific prior written permission.
19bb0d0a8eSMike Smith *
20bb0d0a8eSMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21bb0d0a8eSMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22bb0d0a8eSMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23bb0d0a8eSMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24bb0d0a8eSMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25bb0d0a8eSMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26bb0d0a8eSMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27bb0d0a8eSMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28bb0d0a8eSMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29bb0d0a8eSMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30bb0d0a8eSMike Smith * SUCH DAMAGE.
31bb0d0a8eSMike Smith */
32bb0d0a8eSMike Smith
33aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
34bb0d0a8eSMike Smith /*
35bb0d0a8eSMike Smith * PCI:PCI bridge support.
36bb0d0a8eSMike Smith */
37bb0d0a8eSMike Smith
3882cb5c3bSJohn Baldwin #include "opt_pci.h"
3982cb5c3bSJohn Baldwin
40bb0d0a8eSMike Smith #include <sys/param.h>
41bb0d0a8eSMike Smith #include <sys/bus.h>
4283c41143SJohn Baldwin #include <sys/kernel.h>
43e2e050c8SConrad Meyer #include <sys/lock.h>
4483c41143SJohn Baldwin #include <sys/malloc.h>
4583c41143SJohn Baldwin #include <sys/module.h>
46e2e050c8SConrad Meyer #include <sys/mutex.h>
475db2a4a8SKonstantin Belousov #include <sys/pciio.h>
48a8b354a8SWarner Losh #include <sys/rman.h>
491c54ff33SMatthew N. Dodd #include <sys/sysctl.h>
5083c41143SJohn Baldwin #include <sys/systm.h>
5182cb5c3bSJohn Baldwin #include <sys/taskqueue.h>
52bb0d0a8eSMike Smith
5338d8c994SWarner Losh #include <dev/pci/pcivar.h>
5438d8c994SWarner Losh #include <dev/pci/pcireg.h>
5562508c53SJohn Baldwin #include <dev/pci/pci_private.h>
5638d8c994SWarner Losh #include <dev/pci/pcib_private.h>
57bb0d0a8eSMike Smith
58bb0d0a8eSMike Smith #include "pcib_if.h"
59bb0d0a8eSMike Smith
60bb0d0a8eSMike Smith static int pcib_probe(device_t dev);
61e36af292SJung-uk Kim static int pcib_resume(device_t dev);
6203719c65SJohn Baldwin
6303719c65SJohn Baldwin static bus_child_present_t pcib_child_present;
6403719c65SJohn Baldwin static bus_alloc_resource_t pcib_alloc_resource;
6503719c65SJohn Baldwin static bus_adjust_resource_t pcib_adjust_resource;
6603719c65SJohn Baldwin static bus_release_resource_t pcib_release_resource;
67b377ff81SJohn Baldwin static bus_activate_resource_t pcib_activate_resource;
68b377ff81SJohn Baldwin static bus_deactivate_resource_t pcib_deactivate_resource;
69b377ff81SJohn Baldwin static bus_map_resource_t pcib_map_resource;
70b377ff81SJohn Baldwin static bus_unmap_resource_t pcib_unmap_resource;
7103719c65SJohn Baldwin static int pcib_reset_child(device_t dev, device_t child, int flags);
7203719c65SJohn Baldwin
7362508c53SJohn Baldwin static int pcib_power_for_sleep(device_t pcib, device_t dev,
7462508c53SJohn Baldwin int *pstate);
75d7be980dSAndrew Turner static int pcib_ari_get_id(device_t pcib, device_t dev,
76d7be980dSAndrew Turner enum pci_id_type type, uintptr_t *id);
7755d3ea17SRyan Stone static uint32_t pcib_read_config(device_t dev, u_int b, u_int s,
7855d3ea17SRyan Stone u_int f, u_int reg, int width);
7955d3ea17SRyan Stone static void pcib_write_config(device_t dev, u_int b, u_int s,
8055d3ea17SRyan Stone u_int f, u_int reg, uint32_t val, int width);
8155d3ea17SRyan Stone static int pcib_ari_maxslots(device_t dev);
8255d3ea17SRyan Stone static int pcib_ari_maxfuncs(device_t dev);
8355d3ea17SRyan Stone static int pcib_try_enable_ari(device_t pcib, device_t dev);
842397d2d8SRyan Stone static int pcib_ari_enabled(device_t pcib);
852397d2d8SRyan Stone static void pcib_ari_decode_rid(device_t pcib, uint16_t rid,
862397d2d8SRyan Stone int *bus, int *slot, int *func);
8782cb5c3bSJohn Baldwin #ifdef PCI_HP
88fa3b03d3SAlexander Motin static void pcib_pcie_ab_timeout(void *arg, int pending);
89fa3b03d3SAlexander Motin static void pcib_pcie_cc_timeout(void *arg, int pending);
90fa3b03d3SAlexander Motin static void pcib_pcie_dll_timeout(void *arg, int pending);
9182cb5c3bSJohn Baldwin #endif
921ffd07bdSJohn Baldwin static int pcib_request_feature_default(device_t pcib, device_t dev,
934cb67729SWarner Losh enum pci_feature feature);
94bb0d0a8eSMike Smith
95bb0d0a8eSMike Smith static device_method_t pcib_methods[] = {
96bb0d0a8eSMike Smith /* Device interface */
97bb0d0a8eSMike Smith DEVMETHOD(device_probe, pcib_probe),
98bb0d0a8eSMike Smith DEVMETHOD(device_attach, pcib_attach),
996f33eaa5SJohn Baldwin DEVMETHOD(device_detach, pcib_detach),
100bb0d0a8eSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown),
1018415a654SJessica Clarke DEVMETHOD(device_suspend, bus_generic_suspend),
102e36af292SJung-uk Kim DEVMETHOD(device_resume, pcib_resume),
103bb0d0a8eSMike Smith
104bb0d0a8eSMike Smith /* Bus interface */
10582cb5c3bSJohn Baldwin DEVMETHOD(bus_child_present, pcib_child_present),
106bb0d0a8eSMike Smith DEVMETHOD(bus_read_ivar, pcib_read_ivar),
107bb0d0a8eSMike Smith DEVMETHOD(bus_write_ivar, pcib_write_ivar),
108bb0d0a8eSMike Smith DEVMETHOD(bus_alloc_resource, pcib_alloc_resource),
10983c41143SJohn Baldwin DEVMETHOD(bus_adjust_resource, pcib_adjust_resource),
11083c41143SJohn Baldwin DEVMETHOD(bus_release_resource, pcib_release_resource),
111b377ff81SJohn Baldwin DEVMETHOD(bus_activate_resource, pcib_activate_resource),
112b377ff81SJohn Baldwin DEVMETHOD(bus_deactivate_resource, pcib_deactivate_resource),
113b377ff81SJohn Baldwin DEVMETHOD(bus_map_resource, pcib_map_resource),
114b377ff81SJohn Baldwin DEVMETHOD(bus_unmap_resource, pcib_unmap_resource),
115bb0d0a8eSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
116bb0d0a8eSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
1175db2a4a8SKonstantin Belousov DEVMETHOD(bus_reset_child, pcib_reset_child),
118bb0d0a8eSMike Smith
119bb0d0a8eSMike Smith /* pcib interface */
12055d3ea17SRyan Stone DEVMETHOD(pcib_maxslots, pcib_ari_maxslots),
12155d3ea17SRyan Stone DEVMETHOD(pcib_maxfuncs, pcib_ari_maxfuncs),
122bb0d0a8eSMike Smith DEVMETHOD(pcib_read_config, pcib_read_config),
123bb0d0a8eSMike Smith DEVMETHOD(pcib_write_config, pcib_write_config),
124bb0d0a8eSMike Smith DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt),
1259bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msi, pcib_alloc_msi),
1269bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msi, pcib_release_msi),
1279bf4c9c1SJohn Baldwin DEVMETHOD(pcib_alloc_msix, pcib_alloc_msix),
1289bf4c9c1SJohn Baldwin DEVMETHOD(pcib_release_msix, pcib_release_msix),
129e706f7f0SJohn Baldwin DEVMETHOD(pcib_map_msi, pcib_map_msi),
13062508c53SJohn Baldwin DEVMETHOD(pcib_power_for_sleep, pcib_power_for_sleep),
131d7be980dSAndrew Turner DEVMETHOD(pcib_get_id, pcib_ari_get_id),
13255d3ea17SRyan Stone DEVMETHOD(pcib_try_enable_ari, pcib_try_enable_ari),
1332397d2d8SRyan Stone DEVMETHOD(pcib_ari_enabled, pcib_ari_enabled),
1342397d2d8SRyan Stone DEVMETHOD(pcib_decode_rid, pcib_ari_decode_rid),
1351ffd07bdSJohn Baldwin DEVMETHOD(pcib_request_feature, pcib_request_feature_default),
136bb0d0a8eSMike Smith
1374b7ec270SMarius Strobl DEVMETHOD_END
138bb0d0a8eSMike Smith };
139bb0d0a8eSMike Smith
14004dda605SJohn Baldwin DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc));
14197a41013SJohn Baldwin EARLY_DRIVER_MODULE(pcib, pci, pcib_driver, NULL, NULL, BUS_PASS_BUS);
142bb0d0a8eSMike Smith
1430070c94bSJohn Baldwin SYSCTL_DECL(_hw_pci);
1440070c94bSJohn Baldwin
1450070c94bSJohn Baldwin static int pci_clear_pcib;
1460070c94bSJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0,
1470070c94bSJohn Baldwin "Clear firmware-assigned resources for PCI-PCI bridge I/O windows.");
14883c41143SJohn Baldwin
14983c41143SJohn Baldwin /*
15026245980SJessica Clarke * Get the corresponding window if this resource from a child device was
15126245980SJessica Clarke * sub-allocated from one of our window resource managers.
15226245980SJessica Clarke */
15326245980SJessica Clarke static struct pcib_window *
pcib_get_resource_window(struct pcib_softc * sc,struct resource * r)154582b84cdSJohn Baldwin pcib_get_resource_window(struct pcib_softc *sc, struct resource *r)
15526245980SJessica Clarke {
156582b84cdSJohn Baldwin switch (rman_get_type(r)) {
15726245980SJessica Clarke case SYS_RES_IOPORT:
15826245980SJessica Clarke if (rman_is_region_manager(r, &sc->io.rman))
15926245980SJessica Clarke return (&sc->io);
16026245980SJessica Clarke break;
16126245980SJessica Clarke case SYS_RES_MEMORY:
16226245980SJessica Clarke /* Prefetchable resources may live in either memory rman. */
16326245980SJessica Clarke if (rman_get_flags(r) & RF_PREFETCHABLE &&
16426245980SJessica Clarke rman_is_region_manager(r, &sc->pmem.rman))
16526245980SJessica Clarke return (&sc->pmem);
16626245980SJessica Clarke if (rman_is_region_manager(r, &sc->mem.rman))
16726245980SJessica Clarke return (&sc->mem);
16826245980SJessica Clarke break;
16926245980SJessica Clarke }
17026245980SJessica Clarke return (NULL);
17126245980SJessica Clarke }
17226245980SJessica Clarke
17326245980SJessica Clarke /*
17483c41143SJohn Baldwin * Is a resource from a child device sub-allocated from one of our
17583c41143SJohn Baldwin * resource managers?
17683c41143SJohn Baldwin */
17783c41143SJohn Baldwin static int
pcib_is_resource_managed(struct pcib_softc * sc,struct resource * r)178582b84cdSJohn Baldwin pcib_is_resource_managed(struct pcib_softc *sc, struct resource *r)
17983c41143SJohn Baldwin {
18083c41143SJohn Baldwin
181582b84cdSJohn Baldwin if (rman_get_type(r) == PCI_RES_BUS)
1824edef187SJohn Baldwin return (rman_is_region_manager(r, &sc->bus.rman));
183582b84cdSJohn Baldwin return (pcib_get_resource_window(sc, r) != NULL);
18483c41143SJohn Baldwin }
18583c41143SJohn Baldwin
18683c41143SJohn Baldwin static int
pcib_is_window_open(struct pcib_window * pw)18783c41143SJohn Baldwin pcib_is_window_open(struct pcib_window *pw)
18883c41143SJohn Baldwin {
18983c41143SJohn Baldwin
19083c41143SJohn Baldwin return (pw->valid && pw->base < pw->limit);
19183c41143SJohn Baldwin }
19283c41143SJohn Baldwin
19383c41143SJohn Baldwin /*
19483c41143SJohn Baldwin * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and
19583c41143SJohn Baldwin * handle for the resource, we could pass RF_ACTIVE up to the PCI bus
19683c41143SJohn Baldwin * when allocating the resource windows and rely on the PCI bus driver
19783c41143SJohn Baldwin * to do this for us.
19883c41143SJohn Baldwin */
19983c41143SJohn Baldwin static void
pcib_activate_window(struct pcib_softc * sc,int type)20083c41143SJohn Baldwin pcib_activate_window(struct pcib_softc *sc, int type)
20183c41143SJohn Baldwin {
20283c41143SJohn Baldwin
20383c41143SJohn Baldwin PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type);
20483c41143SJohn Baldwin }
20583c41143SJohn Baldwin
20683c41143SJohn Baldwin static void
pcib_write_windows(struct pcib_softc * sc,int mask)20783c41143SJohn Baldwin pcib_write_windows(struct pcib_softc *sc, int mask)
20883c41143SJohn Baldwin {
20983c41143SJohn Baldwin device_t dev;
21083c41143SJohn Baldwin uint32_t val;
21183c41143SJohn Baldwin
21283c41143SJohn Baldwin dev = sc->dev;
21383c41143SJohn Baldwin if (sc->io.valid && mask & WIN_IO) {
21483c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
21583c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
21683c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEH_1,
21783c41143SJohn Baldwin sc->io.base >> 16, 2);
21883c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITH_1,
21983c41143SJohn Baldwin sc->io.limit >> 16, 2);
22083c41143SJohn Baldwin }
22183c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1);
22283c41143SJohn Baldwin pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1);
22383c41143SJohn Baldwin }
22483c41143SJohn Baldwin
22583c41143SJohn Baldwin if (mask & WIN_MEM) {
22683c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2);
22783c41143SJohn Baldwin pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2);
22883c41143SJohn Baldwin }
22983c41143SJohn Baldwin
23083c41143SJohn Baldwin if (sc->pmem.valid && mask & WIN_PMEM) {
23183c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
23283c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
23383c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEH_1,
23483c41143SJohn Baldwin sc->pmem.base >> 32, 4);
23583c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITH_1,
23683c41143SJohn Baldwin sc->pmem.limit >> 32, 4);
23783c41143SJohn Baldwin }
23883c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2);
23983c41143SJohn Baldwin pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2);
24083c41143SJohn Baldwin }
24183c41143SJohn Baldwin }
24283c41143SJohn Baldwin
243c825d4dcSJohn Baldwin /*
244c825d4dcSJohn Baldwin * This is used to reject I/O port allocations that conflict with an
245c825d4dcSJohn Baldwin * ISA alias range.
246c825d4dcSJohn Baldwin */
247c825d4dcSJohn Baldwin static int
pcib_is_isa_range(struct pcib_softc * sc,rman_res_t start,rman_res_t end,rman_res_t count)2482dd1bdf1SJustin Hibbits pcib_is_isa_range(struct pcib_softc *sc, rman_res_t start, rman_res_t end,
2492dd1bdf1SJustin Hibbits rman_res_t count)
250c825d4dcSJohn Baldwin {
2512dd1bdf1SJustin Hibbits rman_res_t next_alias;
252c825d4dcSJohn Baldwin
253c825d4dcSJohn Baldwin if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE))
254c825d4dcSJohn Baldwin return (0);
255c825d4dcSJohn Baldwin
256c825d4dcSJohn Baldwin /* Only check fixed ranges for overlap. */
257c825d4dcSJohn Baldwin if (start + count - 1 != end)
258c825d4dcSJohn Baldwin return (0);
259c825d4dcSJohn Baldwin
260c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */
261c825d4dcSJohn Baldwin if (start >= 65536)
262c825d4dcSJohn Baldwin return (0);
263c825d4dcSJohn Baldwin
264c825d4dcSJohn Baldwin /* Check for overlap with 0x000 - 0x0ff as a special case. */
265c825d4dcSJohn Baldwin if (start < 0x100)
266c825d4dcSJohn Baldwin goto alias;
267c825d4dcSJohn Baldwin
268c825d4dcSJohn Baldwin /*
269c825d4dcSJohn Baldwin * If the start address is an alias, the range is an alias.
270c825d4dcSJohn Baldwin * Otherwise, compute the start of the next alias range and
271c825d4dcSJohn Baldwin * check if it is before the end of the candidate range.
272c825d4dcSJohn Baldwin */
273c825d4dcSJohn Baldwin if ((start & 0x300) != 0)
274c825d4dcSJohn Baldwin goto alias;
275c825d4dcSJohn Baldwin next_alias = (start & ~0x3fful) | 0x100;
276c825d4dcSJohn Baldwin if (next_alias <= end)
277c825d4dcSJohn Baldwin goto alias;
278c825d4dcSJohn Baldwin return (0);
279c825d4dcSJohn Baldwin
280c825d4dcSJohn Baldwin alias:
281c825d4dcSJohn Baldwin if (bootverbose)
282c825d4dcSJohn Baldwin device_printf(sc->dev,
283da1b038aSJustin Hibbits "I/O range %#jx-%#jx overlaps with an ISA alias\n", start,
284c825d4dcSJohn Baldwin end);
285c825d4dcSJohn Baldwin return (1);
286c825d4dcSJohn Baldwin }
287c825d4dcSJohn Baldwin
288c825d4dcSJohn Baldwin static void
pcib_add_window_resources(struct pcib_window * w,struct resource ** res,int count)289c825d4dcSJohn Baldwin pcib_add_window_resources(struct pcib_window *w, struct resource **res,
290c825d4dcSJohn Baldwin int count)
291c825d4dcSJohn Baldwin {
292c825d4dcSJohn Baldwin struct resource **newarray;
293c825d4dcSJohn Baldwin int error, i;
294c825d4dcSJohn Baldwin
295c825d4dcSJohn Baldwin newarray = malloc(sizeof(struct resource *) * (w->count + count),
296c825d4dcSJohn Baldwin M_DEVBUF, M_WAITOK);
297c825d4dcSJohn Baldwin if (w->res != NULL)
298c825d4dcSJohn Baldwin bcopy(w->res, newarray, sizeof(struct resource *) * w->count);
299c825d4dcSJohn Baldwin bcopy(res, newarray + w->count, sizeof(struct resource *) * count);
300c825d4dcSJohn Baldwin free(w->res, M_DEVBUF);
301c825d4dcSJohn Baldwin w->res = newarray;
302c825d4dcSJohn Baldwin w->count += count;
303c825d4dcSJohn Baldwin
304c825d4dcSJohn Baldwin for (i = 0; i < count; i++) {
305c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, rman_get_start(res[i]),
306c825d4dcSJohn Baldwin rman_get_end(res[i]));
307c825d4dcSJohn Baldwin if (error)
308c825d4dcSJohn Baldwin panic("Failed to add resource to rman");
309c825d4dcSJohn Baldwin }
310c825d4dcSJohn Baldwin }
311c825d4dcSJohn Baldwin
3122dd1bdf1SJustin Hibbits typedef void (nonisa_callback)(rman_res_t start, rman_res_t end, void *arg);
313c825d4dcSJohn Baldwin
314c825d4dcSJohn Baldwin static void
pcib_walk_nonisa_ranges(rman_res_t start,rman_res_t end,nonisa_callback * cb,void * arg)3152dd1bdf1SJustin Hibbits pcib_walk_nonisa_ranges(rman_res_t start, rman_res_t end, nonisa_callback *cb,
316c825d4dcSJohn Baldwin void *arg)
317c825d4dcSJohn Baldwin {
3182dd1bdf1SJustin Hibbits rman_res_t next_end;
319c825d4dcSJohn Baldwin
320c825d4dcSJohn Baldwin /*
321c825d4dcSJohn Baldwin * If start is within an ISA alias range, move up to the start
322c825d4dcSJohn Baldwin * of the next non-alias range. As a special case, addresses
323c825d4dcSJohn Baldwin * in the range 0x000 - 0x0ff should also be skipped since
324c825d4dcSJohn Baldwin * those are used for various system I/O devices in ISA
325c825d4dcSJohn Baldwin * systems.
326c825d4dcSJohn Baldwin */
327c825d4dcSJohn Baldwin if (start <= 65535) {
328c825d4dcSJohn Baldwin if (start < 0x100 || (start & 0x300) != 0) {
329c825d4dcSJohn Baldwin start &= ~0x3ff;
330c825d4dcSJohn Baldwin start += 0x400;
331c825d4dcSJohn Baldwin }
332c825d4dcSJohn Baldwin }
333c825d4dcSJohn Baldwin
334c825d4dcSJohn Baldwin /* ISA aliases are only in the lower 64KB of I/O space. */
335c825d4dcSJohn Baldwin while (start <= MIN(end, 65535)) {
336c825d4dcSJohn Baldwin next_end = MIN(start | 0xff, end);
337c825d4dcSJohn Baldwin cb(start, next_end, arg);
338c825d4dcSJohn Baldwin start += 0x400;
339c825d4dcSJohn Baldwin }
340c825d4dcSJohn Baldwin
341c825d4dcSJohn Baldwin if (start <= end)
342c825d4dcSJohn Baldwin cb(start, end, arg);
343c825d4dcSJohn Baldwin }
344c825d4dcSJohn Baldwin
345c825d4dcSJohn Baldwin static void
count_ranges(rman_res_t start,rman_res_t end,void * arg)3462dd1bdf1SJustin Hibbits count_ranges(rman_res_t start, rman_res_t end, void *arg)
347c825d4dcSJohn Baldwin {
348c825d4dcSJohn Baldwin int *countp;
349c825d4dcSJohn Baldwin
350c825d4dcSJohn Baldwin countp = arg;
351c825d4dcSJohn Baldwin (*countp)++;
352c825d4dcSJohn Baldwin }
353c825d4dcSJohn Baldwin
354c825d4dcSJohn Baldwin struct alloc_state {
355c825d4dcSJohn Baldwin struct resource **res;
356c825d4dcSJohn Baldwin struct pcib_softc *sc;
357c825d4dcSJohn Baldwin int count, error;
358c825d4dcSJohn Baldwin };
359c825d4dcSJohn Baldwin
360c825d4dcSJohn Baldwin static void
alloc_ranges(rman_res_t start,rman_res_t end,void * arg)3612dd1bdf1SJustin Hibbits alloc_ranges(rman_res_t start, rman_res_t end, void *arg)
362c825d4dcSJohn Baldwin {
363c825d4dcSJohn Baldwin struct alloc_state *as;
364c825d4dcSJohn Baldwin struct pcib_window *w;
365c825d4dcSJohn Baldwin int rid;
366c825d4dcSJohn Baldwin
367c825d4dcSJohn Baldwin as = arg;
368c825d4dcSJohn Baldwin if (as->error != 0)
369c825d4dcSJohn Baldwin return;
370c825d4dcSJohn Baldwin
371c825d4dcSJohn Baldwin w = &as->sc->io;
372c825d4dcSJohn Baldwin rid = w->reg;
373c825d4dcSJohn Baldwin if (bootverbose)
374c825d4dcSJohn Baldwin device_printf(as->sc->dev,
375da1b038aSJustin Hibbits "allocating non-ISA range %#jx-%#jx\n", start, end);
376c825d4dcSJohn Baldwin as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT,
377b377ff81SJohn Baldwin &rid, start, end, end - start + 1, RF_ACTIVE | RF_UNMAPPED);
378c825d4dcSJohn Baldwin if (as->res[as->count] == NULL)
379c825d4dcSJohn Baldwin as->error = ENXIO;
380c825d4dcSJohn Baldwin else
381c825d4dcSJohn Baldwin as->count++;
382c825d4dcSJohn Baldwin }
383c825d4dcSJohn Baldwin
384c825d4dcSJohn Baldwin static int
pcib_alloc_nonisa_ranges(struct pcib_softc * sc,rman_res_t start,rman_res_t end)3852dd1bdf1SJustin Hibbits pcib_alloc_nonisa_ranges(struct pcib_softc *sc, rman_res_t start, rman_res_t end)
386c825d4dcSJohn Baldwin {
387c825d4dcSJohn Baldwin struct alloc_state as;
388c825d4dcSJohn Baldwin int i, new_count;
389c825d4dcSJohn Baldwin
390c825d4dcSJohn Baldwin /* First, see how many ranges we need. */
391c825d4dcSJohn Baldwin new_count = 0;
392c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count);
393c825d4dcSJohn Baldwin
394c825d4dcSJohn Baldwin /* Second, allocate the ranges. */
395c825d4dcSJohn Baldwin as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF,
396c825d4dcSJohn Baldwin M_WAITOK);
397c825d4dcSJohn Baldwin as.sc = sc;
398c825d4dcSJohn Baldwin as.count = 0;
399c825d4dcSJohn Baldwin as.error = 0;
400c825d4dcSJohn Baldwin pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as);
401c825d4dcSJohn Baldwin if (as.error != 0) {
402c825d4dcSJohn Baldwin for (i = 0; i < as.count; i++)
403c825d4dcSJohn Baldwin bus_release_resource(sc->dev, SYS_RES_IOPORT,
404c825d4dcSJohn Baldwin sc->io.reg, as.res[i]);
405c825d4dcSJohn Baldwin free(as.res, M_DEVBUF);
406c825d4dcSJohn Baldwin return (as.error);
407c825d4dcSJohn Baldwin }
408c825d4dcSJohn Baldwin KASSERT(as.count == new_count, ("%s: count mismatch", __func__));
409c825d4dcSJohn Baldwin
410c825d4dcSJohn Baldwin /* Third, add the ranges to the window. */
411c825d4dcSJohn Baldwin pcib_add_window_resources(&sc->io, as.res, as.count);
412c825d4dcSJohn Baldwin free(as.res, M_DEVBUF);
413c825d4dcSJohn Baldwin return (0);
414c825d4dcSJohn Baldwin }
415c825d4dcSJohn Baldwin
41683c41143SJohn Baldwin static void
pcib_alloc_window(struct pcib_softc * sc,struct pcib_window * w,int type,int flags,pci_addr_t max_address)41783c41143SJohn Baldwin pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type,
41883c41143SJohn Baldwin int flags, pci_addr_t max_address)
41983c41143SJohn Baldwin {
420c825d4dcSJohn Baldwin struct resource *res;
42183c41143SJohn Baldwin char buf[64];
42283c41143SJohn Baldwin int error, rid;
42383c41143SJohn Baldwin
42489977ce2SJustin Hibbits if (max_address != (rman_res_t)max_address)
425534ccd7bSJustin Hibbits max_address = ~0;
42683c41143SJohn Baldwin w->rman.rm_start = 0;
42783c41143SJohn Baldwin w->rman.rm_end = max_address;
42883c41143SJohn Baldwin w->rman.rm_type = RMAN_ARRAY;
42983c41143SJohn Baldwin snprintf(buf, sizeof(buf), "%s %s window",
43083c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name);
43183c41143SJohn Baldwin w->rman.rm_descr = strdup(buf, M_DEVBUF);
43283c41143SJohn Baldwin error = rman_init(&w->rman);
43383c41143SJohn Baldwin if (error)
43483c41143SJohn Baldwin panic("Failed to initialize %s %s rman",
43583c41143SJohn Baldwin device_get_nameunit(sc->dev), w->name);
43683c41143SJohn Baldwin
43783c41143SJohn Baldwin if (!pcib_is_window_open(w))
43883c41143SJohn Baldwin return;
43983c41143SJohn Baldwin
44083c41143SJohn Baldwin if (w->base > max_address || w->limit > max_address) {
44183c41143SJohn Baldwin device_printf(sc->dev,
44283c41143SJohn Baldwin "initial %s window has too many bits, ignoring\n", w->name);
44383c41143SJohn Baldwin return;
44483c41143SJohn Baldwin }
445c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE)
446c825d4dcSJohn Baldwin (void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit);
447c825d4dcSJohn Baldwin else {
44883c41143SJohn Baldwin rid = w->reg;
449c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit,
450b377ff81SJohn Baldwin w->limit - w->base + 1, flags | RF_ACTIVE | RF_UNMAPPED);
451c825d4dcSJohn Baldwin if (res != NULL)
452c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1);
453c825d4dcSJohn Baldwin }
45483c41143SJohn Baldwin if (w->res == NULL) {
45583c41143SJohn Baldwin device_printf(sc->dev,
45683c41143SJohn Baldwin "failed to allocate initial %s window: %#jx-%#jx\n",
45783c41143SJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
45883c41143SJohn Baldwin w->base = max_address;
45983c41143SJohn Baldwin w->limit = 0;
46083c41143SJohn Baldwin pcib_write_windows(sc, w->mask);
46183c41143SJohn Baldwin return;
46283c41143SJohn Baldwin }
46383c41143SJohn Baldwin pcib_activate_window(sc, type);
46483c41143SJohn Baldwin }
46583c41143SJohn Baldwin
46683c41143SJohn Baldwin /*
46783c41143SJohn Baldwin * Initialize I/O windows.
46883c41143SJohn Baldwin */
46983c41143SJohn Baldwin static void
pcib_probe_windows(struct pcib_softc * sc)47083c41143SJohn Baldwin pcib_probe_windows(struct pcib_softc *sc)
47183c41143SJohn Baldwin {
47283c41143SJohn Baldwin pci_addr_t max;
47383c41143SJohn Baldwin device_t dev;
47483c41143SJohn Baldwin uint32_t val;
47583c41143SJohn Baldwin
47683c41143SJohn Baldwin dev = sc->dev;
47783c41143SJohn Baldwin
4780070c94bSJohn Baldwin if (pci_clear_pcib) {
479809923caSJustin Hibbits pcib_bridge_init(dev);
4800070c94bSJohn Baldwin }
4810070c94bSJohn Baldwin
48283c41143SJohn Baldwin /* Determine if the I/O port window is implemented. */
48383c41143SJohn Baldwin val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
48483c41143SJohn Baldwin if (val == 0) {
48583c41143SJohn Baldwin /*
48683c41143SJohn Baldwin * If 'val' is zero, then only 16-bits of I/O space
48783c41143SJohn Baldwin * are supported.
48883c41143SJohn Baldwin */
48983c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
49083c41143SJohn Baldwin if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) {
49183c41143SJohn Baldwin sc->io.valid = 1;
49283c41143SJohn Baldwin pci_write_config(dev, PCIR_IOBASEL_1, 0, 1);
49383c41143SJohn Baldwin }
49483c41143SJohn Baldwin } else
49583c41143SJohn Baldwin sc->io.valid = 1;
49683c41143SJohn Baldwin
49783c41143SJohn Baldwin /* Read the existing I/O port window. */
49883c41143SJohn Baldwin if (sc->io.valid) {
49983c41143SJohn Baldwin sc->io.reg = PCIR_IOBASEL_1;
50083c41143SJohn Baldwin sc->io.step = 12;
50183c41143SJohn Baldwin sc->io.mask = WIN_IO;
50283c41143SJohn Baldwin sc->io.name = "I/O port";
50383c41143SJohn Baldwin if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
50483c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE(
50583c41143SJohn Baldwin pci_read_config(dev, PCIR_IOBASEH_1, 2), val);
50683c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT(
50783c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITH_1, 2),
50883c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1));
50983c41143SJohn Baldwin max = 0xffffffff;
51083c41143SJohn Baldwin } else {
51183c41143SJohn Baldwin sc->io.base = PCI_PPBIOBASE(0, val);
51283c41143SJohn Baldwin sc->io.limit = PCI_PPBIOLIMIT(0,
51383c41143SJohn Baldwin pci_read_config(dev, PCIR_IOLIMITL_1, 1));
51483c41143SJohn Baldwin max = 0xffff;
51583c41143SJohn Baldwin }
51683c41143SJohn Baldwin pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max);
51783c41143SJohn Baldwin }
51883c41143SJohn Baldwin
51983c41143SJohn Baldwin /* Read the existing memory window. */
52083c41143SJohn Baldwin sc->mem.valid = 1;
52183c41143SJohn Baldwin sc->mem.reg = PCIR_MEMBASE_1;
52283c41143SJohn Baldwin sc->mem.step = 20;
52383c41143SJohn Baldwin sc->mem.mask = WIN_MEM;
52483c41143SJohn Baldwin sc->mem.name = "memory";
52583c41143SJohn Baldwin sc->mem.base = PCI_PPBMEMBASE(0,
52683c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMBASE_1, 2));
52783c41143SJohn Baldwin sc->mem.limit = PCI_PPBMEMLIMIT(0,
52883c41143SJohn Baldwin pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
52983c41143SJohn Baldwin pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff);
53083c41143SJohn Baldwin
53183c41143SJohn Baldwin /* Determine if the prefetchable memory window is implemented. */
53283c41143SJohn Baldwin val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
53383c41143SJohn Baldwin if (val == 0) {
53483c41143SJohn Baldwin /*
53583c41143SJohn Baldwin * If 'val' is zero, then only 32-bits of memory space
53683c41143SJohn Baldwin * are supported.
53783c41143SJohn Baldwin */
53883c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
53983c41143SJohn Baldwin if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) {
54083c41143SJohn Baldwin sc->pmem.valid = 1;
54183c41143SJohn Baldwin pci_write_config(dev, PCIR_PMBASEL_1, 0, 2);
54283c41143SJohn Baldwin }
54383c41143SJohn Baldwin } else
54483c41143SJohn Baldwin sc->pmem.valid = 1;
54583c41143SJohn Baldwin
54683c41143SJohn Baldwin /* Read the existing prefetchable memory window. */
54783c41143SJohn Baldwin if (sc->pmem.valid) {
54883c41143SJohn Baldwin sc->pmem.reg = PCIR_PMBASEL_1;
54983c41143SJohn Baldwin sc->pmem.step = 20;
55083c41143SJohn Baldwin sc->pmem.mask = WIN_PMEM;
55183c41143SJohn Baldwin sc->pmem.name = "prefetch";
55283c41143SJohn Baldwin if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
55383c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE(
55483c41143SJohn Baldwin pci_read_config(dev, PCIR_PMBASEH_1, 4), val);
55583c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT(
55683c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITH_1, 4),
55783c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2));
55883c41143SJohn Baldwin max = 0xffffffffffffffff;
55983c41143SJohn Baldwin } else {
56083c41143SJohn Baldwin sc->pmem.base = PCI_PPBMEMBASE(0, val);
56183c41143SJohn Baldwin sc->pmem.limit = PCI_PPBMEMLIMIT(0,
56283c41143SJohn Baldwin pci_read_config(dev, PCIR_PMLIMITL_1, 2));
56383c41143SJohn Baldwin max = 0xffffffff;
56483c41143SJohn Baldwin }
56583c41143SJohn Baldwin pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY,
56683c41143SJohn Baldwin RF_PREFETCHABLE, max);
56783c41143SJohn Baldwin }
56883c41143SJohn Baldwin }
56983c41143SJohn Baldwin
5706f33eaa5SJohn Baldwin static void
pcib_release_window(struct pcib_softc * sc,struct pcib_window * w,int type)5716f33eaa5SJohn Baldwin pcib_release_window(struct pcib_softc *sc, struct pcib_window *w, int type)
5726f33eaa5SJohn Baldwin {
5736f33eaa5SJohn Baldwin device_t dev;
5746f33eaa5SJohn Baldwin int error, i;
5756f33eaa5SJohn Baldwin
5766f33eaa5SJohn Baldwin if (!w->valid)
5776f33eaa5SJohn Baldwin return;
5786f33eaa5SJohn Baldwin
5796f33eaa5SJohn Baldwin dev = sc->dev;
5806f33eaa5SJohn Baldwin error = rman_fini(&w->rman);
5816f33eaa5SJohn Baldwin if (error) {
5826f33eaa5SJohn Baldwin device_printf(dev, "failed to release %s rman\n", w->name);
5836f33eaa5SJohn Baldwin return;
5846f33eaa5SJohn Baldwin }
5856f33eaa5SJohn Baldwin free(__DECONST(char *, w->rman.rm_descr), M_DEVBUF);
5866f33eaa5SJohn Baldwin
5876f33eaa5SJohn Baldwin for (i = 0; i < w->count; i++) {
5886f33eaa5SJohn Baldwin error = bus_free_resource(dev, type, w->res[i]);
5896f33eaa5SJohn Baldwin if (error)
5906f33eaa5SJohn Baldwin device_printf(dev,
5916f33eaa5SJohn Baldwin "failed to release %s resource: %d\n", w->name,
5926f33eaa5SJohn Baldwin error);
5936f33eaa5SJohn Baldwin }
5946f33eaa5SJohn Baldwin free(w->res, M_DEVBUF);
5956f33eaa5SJohn Baldwin }
5966f33eaa5SJohn Baldwin
5976f33eaa5SJohn Baldwin static void
pcib_free_windows(struct pcib_softc * sc)5986f33eaa5SJohn Baldwin pcib_free_windows(struct pcib_softc *sc)
5996f33eaa5SJohn Baldwin {
6006f33eaa5SJohn Baldwin
6016f33eaa5SJohn Baldwin pcib_release_window(sc, &sc->pmem, SYS_RES_MEMORY);
6026f33eaa5SJohn Baldwin pcib_release_window(sc, &sc->mem, SYS_RES_MEMORY);
6036f33eaa5SJohn Baldwin pcib_release_window(sc, &sc->io, SYS_RES_IOPORT);
6046f33eaa5SJohn Baldwin }
6056f33eaa5SJohn Baldwin
6064edef187SJohn Baldwin /*
6074edef187SJohn Baldwin * Allocate a suitable secondary bus for this bridge if needed and
6084edef187SJohn Baldwin * initialize the resource manager for the secondary bus range. Note
6094edef187SJohn Baldwin * that the minimum count is a desired value and this may allocate a
6104edef187SJohn Baldwin * smaller range.
6114edef187SJohn Baldwin */
6124edef187SJohn Baldwin void
pcib_setup_secbus(device_t dev,struct pcib_secbus * bus,int min_count)6134edef187SJohn Baldwin pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count)
6144edef187SJohn Baldwin {
6154edef187SJohn Baldwin char buf[64];
616ad6f36f8SJohn Baldwin int error, rid, sec_reg;
6174edef187SJohn Baldwin
6184edef187SJohn Baldwin switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) {
6194edef187SJohn Baldwin case PCIM_HDRTYPE_BRIDGE:
620ad6f36f8SJohn Baldwin sec_reg = PCIR_SECBUS_1;
6214edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_1;
6224edef187SJohn Baldwin break;
6234edef187SJohn Baldwin case PCIM_HDRTYPE_CARDBUS:
624ad6f36f8SJohn Baldwin sec_reg = PCIR_SECBUS_2;
6254edef187SJohn Baldwin bus->sub_reg = PCIR_SUBBUS_2;
6264edef187SJohn Baldwin break;
6274edef187SJohn Baldwin default:
6284edef187SJohn Baldwin panic("not a PCI bridge");
6294edef187SJohn Baldwin }
630ad6f36f8SJohn Baldwin bus->sec = pci_read_config(dev, sec_reg, 1);
631ad6f36f8SJohn Baldwin bus->sub = pci_read_config(dev, bus->sub_reg, 1);
6324edef187SJohn Baldwin bus->dev = dev;
6334edef187SJohn Baldwin bus->rman.rm_start = 0;
6344edef187SJohn Baldwin bus->rman.rm_end = PCI_BUSMAX;
6354edef187SJohn Baldwin bus->rman.rm_type = RMAN_ARRAY;
6364edef187SJohn Baldwin snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev));
6374edef187SJohn Baldwin bus->rman.rm_descr = strdup(buf, M_DEVBUF);
6384edef187SJohn Baldwin error = rman_init(&bus->rman);
6394edef187SJohn Baldwin if (error)
6404edef187SJohn Baldwin panic("Failed to initialize %s bus number rman",
6414edef187SJohn Baldwin device_get_nameunit(dev));
6424edef187SJohn Baldwin
6434edef187SJohn Baldwin /*
6444edef187SJohn Baldwin * Allocate a bus range. This will return an existing bus range
6454edef187SJohn Baldwin * if one exists, or a new bus range if one does not.
6464edef187SJohn Baldwin */
6474edef187SJohn Baldwin rid = 0;
648c47476d7SJustin Hibbits bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid,
64936efc64aSJohn Baldwin min_count, RF_ACTIVE);
6504edef187SJohn Baldwin if (bus->res == NULL) {
6514edef187SJohn Baldwin /*
6524edef187SJohn Baldwin * Fall back to just allocating a range of a single bus
6534edef187SJohn Baldwin * number.
6544edef187SJohn Baldwin */
655c47476d7SJustin Hibbits bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid,
65636efc64aSJohn Baldwin 1, RF_ACTIVE);
6574edef187SJohn Baldwin } else if (rman_get_size(bus->res) < min_count)
6584edef187SJohn Baldwin /*
6594edef187SJohn Baldwin * Attempt to grow the existing range to satisfy the
6604edef187SJohn Baldwin * minimum desired count.
6614edef187SJohn Baldwin */
6624edef187SJohn Baldwin (void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res,
6634edef187SJohn Baldwin rman_get_start(bus->res), rman_get_start(bus->res) +
6644edef187SJohn Baldwin min_count - 1);
6654edef187SJohn Baldwin
6664edef187SJohn Baldwin /*
6674edef187SJohn Baldwin * Add the initial resource to the rman.
6684edef187SJohn Baldwin */
6694edef187SJohn Baldwin if (bus->res != NULL) {
6704edef187SJohn Baldwin error = rman_manage_region(&bus->rman, rman_get_start(bus->res),
6714edef187SJohn Baldwin rman_get_end(bus->res));
6724edef187SJohn Baldwin if (error)
6734edef187SJohn Baldwin panic("Failed to add resource to rman");
6744edef187SJohn Baldwin bus->sec = rman_get_start(bus->res);
6754edef187SJohn Baldwin bus->sub = rman_get_end(bus->res);
6764edef187SJohn Baldwin }
6774edef187SJohn Baldwin }
6784edef187SJohn Baldwin
6796f33eaa5SJohn Baldwin void
pcib_free_secbus(device_t dev,struct pcib_secbus * bus)6806f33eaa5SJohn Baldwin pcib_free_secbus(device_t dev, struct pcib_secbus *bus)
6816f33eaa5SJohn Baldwin {
6826f33eaa5SJohn Baldwin int error;
6836f33eaa5SJohn Baldwin
6846f33eaa5SJohn Baldwin error = rman_fini(&bus->rman);
6856f33eaa5SJohn Baldwin if (error) {
6866f33eaa5SJohn Baldwin device_printf(dev, "failed to release bus number rman\n");
6876f33eaa5SJohn Baldwin return;
6886f33eaa5SJohn Baldwin }
6896f33eaa5SJohn Baldwin free(__DECONST(char *, bus->rman.rm_descr), M_DEVBUF);
6906f33eaa5SJohn Baldwin
6916f33eaa5SJohn Baldwin error = bus_free_resource(dev, PCI_RES_BUS, bus->res);
6926f33eaa5SJohn Baldwin if (error)
6936f33eaa5SJohn Baldwin device_printf(dev,
6946f33eaa5SJohn Baldwin "failed to release bus numbers resource: %d\n", error);
6956f33eaa5SJohn Baldwin }
6966f33eaa5SJohn Baldwin
6974edef187SJohn Baldwin static struct resource *
pcib_suballoc_bus(struct pcib_secbus * bus,device_t child,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)6984edef187SJohn Baldwin pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid,
6992dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
7004edef187SJohn Baldwin {
7014edef187SJohn Baldwin struct resource *res;
7024edef187SJohn Baldwin
7034edef187SJohn Baldwin res = rman_reserve_resource(&bus->rman, start, end, count, flags,
7044edef187SJohn Baldwin child);
7054edef187SJohn Baldwin if (res == NULL)
7064edef187SJohn Baldwin return (NULL);
7074edef187SJohn Baldwin
7084edef187SJohn Baldwin if (bootverbose)
7094edef187SJohn Baldwin device_printf(bus->dev,
710da1b038aSJustin Hibbits "allocated bus range (%ju-%ju) for rid %d of %s\n",
7114edef187SJohn Baldwin rman_get_start(res), rman_get_end(res), *rid,
7124edef187SJohn Baldwin pcib_child_name(child));
7134edef187SJohn Baldwin rman_set_rid(res, *rid);
7141b9bcfffSJohn Baldwin rman_set_type(res, PCI_RES_BUS);
7154edef187SJohn Baldwin return (res);
7164edef187SJohn Baldwin }
7174edef187SJohn Baldwin
7184edef187SJohn Baldwin /*
7194edef187SJohn Baldwin * Attempt to grow the secondary bus range. This is much simpler than
7204edef187SJohn Baldwin * for I/O windows as the range can only be grown by increasing
7214edef187SJohn Baldwin * subbus.
7224edef187SJohn Baldwin */
7234edef187SJohn Baldwin static int
pcib_grow_subbus(struct pcib_secbus * bus,rman_res_t new_end)7242dd1bdf1SJustin Hibbits pcib_grow_subbus(struct pcib_secbus *bus, rman_res_t new_end)
7254edef187SJohn Baldwin {
7262dd1bdf1SJustin Hibbits rman_res_t old_end;
7274edef187SJohn Baldwin int error;
7284edef187SJohn Baldwin
7294edef187SJohn Baldwin old_end = rman_get_end(bus->res);
7304edef187SJohn Baldwin KASSERT(new_end > old_end, ("attempt to shrink subbus"));
7314edef187SJohn Baldwin error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res,
7324edef187SJohn Baldwin rman_get_start(bus->res), new_end);
7334edef187SJohn Baldwin if (error)
7344edef187SJohn Baldwin return (error);
7354edef187SJohn Baldwin if (bootverbose)
736da1b038aSJustin Hibbits device_printf(bus->dev, "grew bus range to %ju-%ju\n",
7374edef187SJohn Baldwin rman_get_start(bus->res), rman_get_end(bus->res));
7384edef187SJohn Baldwin error = rman_manage_region(&bus->rman, old_end + 1,
7394edef187SJohn Baldwin rman_get_end(bus->res));
7404edef187SJohn Baldwin if (error)
7414edef187SJohn Baldwin panic("Failed to add resource to rman");
7424edef187SJohn Baldwin bus->sub = rman_get_end(bus->res);
7434edef187SJohn Baldwin pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1);
7444edef187SJohn Baldwin return (0);
7454edef187SJohn Baldwin }
7464edef187SJohn Baldwin
7474edef187SJohn Baldwin struct resource *
pcib_alloc_subbus(struct pcib_secbus * bus,device_t child,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)7484edef187SJohn Baldwin pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid,
7492dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
7504edef187SJohn Baldwin {
7514edef187SJohn Baldwin struct resource *res;
7522dd1bdf1SJustin Hibbits rman_res_t start_free, end_free, new_end;
7534edef187SJohn Baldwin
7544edef187SJohn Baldwin /*
7554edef187SJohn Baldwin * First, see if the request can be satisified by the existing
7564edef187SJohn Baldwin * bus range.
7574edef187SJohn Baldwin */
7584edef187SJohn Baldwin res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags);
7594edef187SJohn Baldwin if (res != NULL)
7604edef187SJohn Baldwin return (res);
7614edef187SJohn Baldwin
7624edef187SJohn Baldwin /*
7634edef187SJohn Baldwin * Figure out a range to grow the bus range. First, find the
7644edef187SJohn Baldwin * first bus number after the last allocated bus in the rman and
7654edef187SJohn Baldwin * enforce that as a minimum starting point for the range.
7664edef187SJohn Baldwin */
7674edef187SJohn Baldwin if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 ||
7684edef187SJohn Baldwin end_free != bus->sub)
7694edef187SJohn Baldwin start_free = bus->sub + 1;
7704edef187SJohn Baldwin if (start_free < start)
7714edef187SJohn Baldwin start_free = start;
7724edef187SJohn Baldwin new_end = start_free + count - 1;
7734edef187SJohn Baldwin
7744edef187SJohn Baldwin /*
7754edef187SJohn Baldwin * See if this new range would satisfy the request if it
7764edef187SJohn Baldwin * succeeds.
7774edef187SJohn Baldwin */
7784edef187SJohn Baldwin if (new_end > end)
7794edef187SJohn Baldwin return (NULL);
7804edef187SJohn Baldwin
7814edef187SJohn Baldwin /* Finally, attempt to grow the existing resource. */
7824edef187SJohn Baldwin if (bootverbose) {
7834edef187SJohn Baldwin device_printf(bus->dev,
784da1b038aSJustin Hibbits "attempting to grow bus range for %ju buses\n", count);
785da1b038aSJustin Hibbits printf("\tback candidate range: %ju-%ju\n", start_free,
7864edef187SJohn Baldwin new_end);
7874edef187SJohn Baldwin }
7884edef187SJohn Baldwin if (pcib_grow_subbus(bus, new_end) == 0)
7894edef187SJohn Baldwin return (pcib_suballoc_bus(bus, child, rid, start, end, count,
7904edef187SJohn Baldwin flags));
7914edef187SJohn Baldwin return (NULL);
7924edef187SJohn Baldwin }
7934edef187SJohn Baldwin
79482cb5c3bSJohn Baldwin #ifdef PCI_HP
79582cb5c3bSJohn Baldwin /*
79682cb5c3bSJohn Baldwin * PCI-express HotPlug support.
79782cb5c3bSJohn Baldwin */
79825a57bd6SJohn Baldwin static int pci_enable_pcie_hp = 1;
79925a57bd6SJohn Baldwin SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_hp, CTLFLAG_RDTUN,
80025a57bd6SJohn Baldwin &pci_enable_pcie_hp, 0,
80125a57bd6SJohn Baldwin "Enable support for native PCI-express HotPlug.");
80225a57bd6SJohn Baldwin
803*9be42ee6SColin Percival static sbintime_t pcie_hp_detach_timeout = 5 * SBT_1S;
804*9be42ee6SColin Percival SYSCTL_SBINTIME_MSEC(_hw_pci, OID_AUTO, pcie_hp_detach_timeout, CTLFLAG_RWTUN,
805*9be42ee6SColin Percival &pcie_hp_detach_timeout,
806*9be42ee6SColin Percival "Attention Button delay for PCI-express Eject.");
807*9be42ee6SColin Percival
80882cb5c3bSJohn Baldwin static void
pcib_probe_hotplug(struct pcib_softc * sc)80982cb5c3bSJohn Baldwin pcib_probe_hotplug(struct pcib_softc *sc)
81082cb5c3bSJohn Baldwin {
81182cb5c3bSJohn Baldwin device_t dev;
81237290148SEric van Gyzen uint32_t link_cap;
813991d431fSEric van Gyzen uint16_t link_sta, slot_sta;
81482cb5c3bSJohn Baldwin
81525a57bd6SJohn Baldwin if (!pci_enable_pcie_hp)
81625a57bd6SJohn Baldwin return;
81725a57bd6SJohn Baldwin
81882cb5c3bSJohn Baldwin dev = sc->dev;
81982cb5c3bSJohn Baldwin if (pci_find_cap(dev, PCIY_EXPRESS, NULL) != 0)
82082cb5c3bSJohn Baldwin return;
82182cb5c3bSJohn Baldwin
82282cb5c3bSJohn Baldwin if (!(pcie_read_config(dev, PCIER_FLAGS, 2) & PCIEM_FLAGS_SLOT))
82382cb5c3bSJohn Baldwin return;
82482cb5c3bSJohn Baldwin
82582cb5c3bSJohn Baldwin sc->pcie_slot_cap = pcie_read_config(dev, PCIER_SLOT_CAP, 4);
82682cb5c3bSJohn Baldwin
827991d431fSEric van Gyzen if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_HPC) == 0)
8282611037cSJohn Baldwin return;
82937290148SEric van Gyzen link_cap = pcie_read_config(dev, PCIER_LINK_CAP, 4);
83037290148SEric van Gyzen if ((link_cap & PCIEM_LINK_CAP_DL_ACTIVE) == 0)
8312ffb582aSJohn Baldwin return;
8322611037cSJohn Baldwin
833991d431fSEric van Gyzen /*
834991d431fSEric van Gyzen * Some devices report that they have an MRL when they actually
835991d431fSEric van Gyzen * do not. Since they always report that the MRL is open, child
836991d431fSEric van Gyzen * devices would be ignored. Try to detect these devices and
837991d431fSEric van Gyzen * ignore their claim of HotPlug support.
838991d431fSEric van Gyzen *
839991d431fSEric van Gyzen * If there is an open MRL but the Data Link Layer is active,
840991d431fSEric van Gyzen * the MRL is not real.
841991d431fSEric van Gyzen */
84237290148SEric van Gyzen if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) != 0) {
843991d431fSEric van Gyzen link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
844991d431fSEric van Gyzen slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
845991d431fSEric van Gyzen if ((slot_sta & PCIEM_SLOT_STA_MRLSS) != 0 &&
846991d431fSEric van Gyzen (link_sta & PCIEM_LINK_STA_DL_ACTIVE) != 0) {
847991d431fSEric van Gyzen return;
848991d431fSEric van Gyzen }
849991d431fSEric van Gyzen }
850991d431fSEric van Gyzen
85128586889SWarner Losh /*
85228586889SWarner Losh * Now that we're sure we want to do hot plug, ask the
85328586889SWarner Losh * firmware, if any, if that's OK.
85428586889SWarner Losh */
8551ffd07bdSJohn Baldwin if (pcib_request_feature(dev, PCI_FEATURE_HP) != 0) {
85628586889SWarner Losh if (bootverbose)
85728586889SWarner Losh device_printf(dev, "Unable to activate hot plug feature.\n");
85828586889SWarner Losh return;
85928586889SWarner Losh }
86028586889SWarner Losh
86182cb5c3bSJohn Baldwin sc->flags |= PCIB_HOTPLUG;
86282cb5c3bSJohn Baldwin }
86382cb5c3bSJohn Baldwin
86482cb5c3bSJohn Baldwin /*
86582cb5c3bSJohn Baldwin * Send a HotPlug command to the slot control register. If this slot
86607454911SJohn Baldwin * uses command completion interrupts and a previous command is still
86707454911SJohn Baldwin * in progress, then the command is dropped. Once the previous
86807454911SJohn Baldwin * command completes or times out, pcib_pcie_hotplug_update() will be
86907454911SJohn Baldwin * invoked to post a new command based on the slot's state at that
87007454911SJohn Baldwin * time.
87182cb5c3bSJohn Baldwin */
87282cb5c3bSJohn Baldwin static void
pcib_pcie_hotplug_command(struct pcib_softc * sc,uint16_t val,uint16_t mask)87382cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(struct pcib_softc *sc, uint16_t val, uint16_t mask)
87482cb5c3bSJohn Baldwin {
87582cb5c3bSJohn Baldwin device_t dev;
87682cb5c3bSJohn Baldwin uint16_t ctl, new;
87782cb5c3bSJohn Baldwin
87882cb5c3bSJohn Baldwin dev = sc->dev;
87982cb5c3bSJohn Baldwin
88007454911SJohn Baldwin if (sc->flags & PCIB_HOTPLUG_CMD_PENDING)
88107454911SJohn Baldwin return;
88207454911SJohn Baldwin
88382cb5c3bSJohn Baldwin ctl = pcie_read_config(dev, PCIER_SLOT_CTL, 2);
88482cb5c3bSJohn Baldwin new = (ctl & ~mask) | val;
88507454911SJohn Baldwin if (new == ctl)
88607454911SJohn Baldwin return;
887991d431fSEric van Gyzen if (bootverbose)
888991d431fSEric van Gyzen device_printf(dev, "HotPlug command: %04x -> %04x\n", ctl, new);
88907454911SJohn Baldwin pcie_write_config(dev, PCIER_SLOT_CTL, new, 2);
8906f33eaa5SJohn Baldwin if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS) &&
8916f33eaa5SJohn Baldwin (ctl & new) & PCIEM_SLOT_CTL_CCIE) {
89282cb5c3bSJohn Baldwin sc->flags |= PCIB_HOTPLUG_CMD_PENDING;
89382cb5c3bSJohn Baldwin if (!cold)
894b23314ecSJohn Baldwin taskqueue_enqueue_timeout(taskqueue_bus,
895fa3b03d3SAlexander Motin &sc->pcie_cc_task, hz);
89682cb5c3bSJohn Baldwin }
89782cb5c3bSJohn Baldwin }
89882cb5c3bSJohn Baldwin
89982cb5c3bSJohn Baldwin static void
pcib_pcie_hotplug_command_completed(struct pcib_softc * sc)90082cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(struct pcib_softc *sc)
90182cb5c3bSJohn Baldwin {
90282cb5c3bSJohn Baldwin device_t dev;
90382cb5c3bSJohn Baldwin
90482cb5c3bSJohn Baldwin dev = sc->dev;
90582cb5c3bSJohn Baldwin
90682cb5c3bSJohn Baldwin if (bootverbose)
90782cb5c3bSJohn Baldwin device_printf(dev, "Command Completed\n");
90882cb5c3bSJohn Baldwin if (!(sc->flags & PCIB_HOTPLUG_CMD_PENDING))
90982cb5c3bSJohn Baldwin return;
910b23314ecSJohn Baldwin taskqueue_cancel_timeout(taskqueue_bus, &sc->pcie_cc_task, NULL);
91182cb5c3bSJohn Baldwin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING;
9126f33eaa5SJohn Baldwin wakeup(sc);
91382cb5c3bSJohn Baldwin }
91482cb5c3bSJohn Baldwin
91582cb5c3bSJohn Baldwin /*
91682cb5c3bSJohn Baldwin * Returns true if a card is fully inserted from the user's
91782cb5c3bSJohn Baldwin * perspective. It may not yet be ready for access, but the driver
91882cb5c3bSJohn Baldwin * can now start enabling access if necessary.
91982cb5c3bSJohn Baldwin */
92082cb5c3bSJohn Baldwin static bool
pcib_hotplug_inserted(struct pcib_softc * sc)92182cb5c3bSJohn Baldwin pcib_hotplug_inserted(struct pcib_softc *sc)
92282cb5c3bSJohn Baldwin {
92382cb5c3bSJohn Baldwin
92482cb5c3bSJohn Baldwin /* Pretend the card isn't present if a detach is forced. */
92582cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACHING)
92682cb5c3bSJohn Baldwin return (false);
92782cb5c3bSJohn Baldwin
92882cb5c3bSJohn Baldwin /* Card must be present in the slot. */
92982cb5c3bSJohn Baldwin if ((sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS) == 0)
93082cb5c3bSJohn Baldwin return (false);
93182cb5c3bSJohn Baldwin
93282cb5c3bSJohn Baldwin /* A power fault implicitly turns off power to the slot. */
93382cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD)
93482cb5c3bSJohn Baldwin return (false);
93582cb5c3bSJohn Baldwin
93682cb5c3bSJohn Baldwin /* If the MRL is disengaged, the slot is powered off. */
93782cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP &&
93882cb5c3bSJohn Baldwin (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS) != 0)
93982cb5c3bSJohn Baldwin return (false);
94082cb5c3bSJohn Baldwin
94182cb5c3bSJohn Baldwin return (true);
94282cb5c3bSJohn Baldwin }
94382cb5c3bSJohn Baldwin
94482cb5c3bSJohn Baldwin /*
94582cb5c3bSJohn Baldwin * Returns -1 if the card is fully inserted, powered, and ready for
94682cb5c3bSJohn Baldwin * access. Otherwise, returns 0.
94782cb5c3bSJohn Baldwin */
94882cb5c3bSJohn Baldwin static int
pcib_hotplug_present(struct pcib_softc * sc)94982cb5c3bSJohn Baldwin pcib_hotplug_present(struct pcib_softc *sc)
95082cb5c3bSJohn Baldwin {
95182cb5c3bSJohn Baldwin
95282cb5c3bSJohn Baldwin /* Card must be inserted. */
95382cb5c3bSJohn Baldwin if (!pcib_hotplug_inserted(sc))
95482cb5c3bSJohn Baldwin return (0);
95582cb5c3bSJohn Baldwin
95682cb5c3bSJohn Baldwin /* Require the Data Link Layer to be active. */
95782cb5c3bSJohn Baldwin if (!(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE))
95882cb5c3bSJohn Baldwin return (0);
95982cb5c3bSJohn Baldwin
96082cb5c3bSJohn Baldwin return (-1);
96182cb5c3bSJohn Baldwin }
96282cb5c3bSJohn Baldwin
963a58536b9SAlexander Motin static int pci_enable_pcie_ei = 0;
964a58536b9SAlexander Motin SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_ei, CTLFLAG_RWTUN,
965a58536b9SAlexander Motin &pci_enable_pcie_ei, 0,
966a58536b9SAlexander Motin "Enable support for PCI-express Electromechanical Interlock.");
967a58536b9SAlexander Motin
96882cb5c3bSJohn Baldwin static void
pcib_pcie_hotplug_update(struct pcib_softc * sc,uint16_t val,uint16_t mask,bool schedule_task)96982cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(struct pcib_softc *sc, uint16_t val, uint16_t mask,
97082cb5c3bSJohn Baldwin bool schedule_task)
97182cb5c3bSJohn Baldwin {
972a1566487SEric van Gyzen bool card_inserted, ei_engaged;
97382cb5c3bSJohn Baldwin
974991d431fSEric van Gyzen /* Clear DETACHING if Presence Detect has cleared. */
97582cb5c3bSJohn Baldwin if ((sc->pcie_slot_sta & (PCIEM_SLOT_STA_PDC | PCIEM_SLOT_STA_PDS)) ==
97682cb5c3bSJohn Baldwin PCIEM_SLOT_STA_PDC)
97782cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACHING;
97882cb5c3bSJohn Baldwin
97982cb5c3bSJohn Baldwin card_inserted = pcib_hotplug_inserted(sc);
98082cb5c3bSJohn Baldwin
98182cb5c3bSJohn Baldwin /* Turn the power indicator on if a card is inserted. */
98282cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PIP) {
98382cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_PIC;
98482cb5c3bSJohn Baldwin if (card_inserted)
98582cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_ON;
98682cb5c3bSJohn Baldwin else if (sc->flags & PCIB_DETACH_PENDING)
98782cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_BLINK;
98882cb5c3bSJohn Baldwin else
98982cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PI_OFF;
99082cb5c3bSJohn Baldwin }
99182cb5c3bSJohn Baldwin
99282cb5c3bSJohn Baldwin /* Turn the power on via the Power Controller if a card is inserted. */
99382cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) {
99482cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_PCC;
99582cb5c3bSJohn Baldwin if (card_inserted)
99682cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PC_ON;
99782cb5c3bSJohn Baldwin else
99882cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PC_OFF;
99982cb5c3bSJohn Baldwin }
100082cb5c3bSJohn Baldwin
100182cb5c3bSJohn Baldwin /*
100282cb5c3bSJohn Baldwin * If a card is inserted, enable the Electromechanical
100382cb5c3bSJohn Baldwin * Interlock. If a card is not inserted (or we are in the
100482cb5c3bSJohn Baldwin * process of detaching), disable the Electromechanical
100582cb5c3bSJohn Baldwin * Interlock.
100682cb5c3bSJohn Baldwin */
1007a58536b9SAlexander Motin if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_EIP) &&
1008a58536b9SAlexander Motin pci_enable_pcie_ei) {
100982cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_EIC;
1010a1566487SEric van Gyzen ei_engaged = (sc->pcie_slot_sta & PCIEM_SLOT_STA_EIS) != 0;
1011a1566487SEric van Gyzen if (card_inserted != ei_engaged)
101282cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_EIC;
101382cb5c3bSJohn Baldwin }
101482cb5c3bSJohn Baldwin
101582cb5c3bSJohn Baldwin /*
101682cb5c3bSJohn Baldwin * Start a timer to see if the Data Link Layer times out.
1017991d431fSEric van Gyzen * Note that we only start the timer if Presence Detect or MRL Sensor
101882cb5c3bSJohn Baldwin * changed on this interrupt. Stop any scheduled timer if
101982cb5c3bSJohn Baldwin * the Data Link Layer is active.
102082cb5c3bSJohn Baldwin */
102182cb5c3bSJohn Baldwin if (card_inserted &&
102282cb5c3bSJohn Baldwin !(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) &&
1023991d431fSEric van Gyzen sc->pcie_slot_sta &
1024991d431fSEric van Gyzen (PCIEM_SLOT_STA_MRLSC | PCIEM_SLOT_STA_PDC)) {
102582cb5c3bSJohn Baldwin if (cold)
102682cb5c3bSJohn Baldwin device_printf(sc->dev,
102782cb5c3bSJohn Baldwin "Data Link Layer inactive\n");
102882cb5c3bSJohn Baldwin else
1029b23314ecSJohn Baldwin taskqueue_enqueue_timeout(taskqueue_bus,
1030fa3b03d3SAlexander Motin &sc->pcie_dll_task, hz);
103182cb5c3bSJohn Baldwin } else if (sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE)
1032b23314ecSJohn Baldwin taskqueue_cancel_timeout(taskqueue_bus, &sc->pcie_dll_task,
1033fa3b03d3SAlexander Motin NULL);
103482cb5c3bSJohn Baldwin
103582cb5c3bSJohn Baldwin pcib_pcie_hotplug_command(sc, val, mask);
103682cb5c3bSJohn Baldwin
103782cb5c3bSJohn Baldwin /*
1038a1566487SEric van Gyzen * During attach the child "pci" device is added synchronously;
103982cb5c3bSJohn Baldwin * otherwise, the task is scheduled to manage the child
104082cb5c3bSJohn Baldwin * device.
104182cb5c3bSJohn Baldwin */
104282cb5c3bSJohn Baldwin if (schedule_task &&
104382cb5c3bSJohn Baldwin (pcib_hotplug_present(sc) != 0) != (sc->child != NULL))
1044b23314ecSJohn Baldwin taskqueue_enqueue(taskqueue_bus, &sc->pcie_hp_task);
104582cb5c3bSJohn Baldwin }
104682cb5c3bSJohn Baldwin
104782cb5c3bSJohn Baldwin static void
pcib_pcie_intr_hotplug(void * arg)10488a1926c5SWarner Losh pcib_pcie_intr_hotplug(void *arg)
104982cb5c3bSJohn Baldwin {
105082cb5c3bSJohn Baldwin struct pcib_softc *sc;
105182cb5c3bSJohn Baldwin device_t dev;
1052e0235fd3SColin Percival uint16_t old_slot_sta;
105382cb5c3bSJohn Baldwin
105482cb5c3bSJohn Baldwin sc = arg;
105582cb5c3bSJohn Baldwin dev = sc->dev;
105613d700adSScott Long PCIB_HP_LOCK(sc);
1057e0235fd3SColin Percival old_slot_sta = sc->pcie_slot_sta;
105882cb5c3bSJohn Baldwin sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
105982cb5c3bSJohn Baldwin
106082cb5c3bSJohn Baldwin /* Clear the events just reported. */
106182cb5c3bSJohn Baldwin pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2);
106282cb5c3bSJohn Baldwin
1063991d431fSEric van Gyzen if (bootverbose)
1064991d431fSEric van Gyzen device_printf(dev, "HotPlug interrupt: %#x\n",
1065991d431fSEric van Gyzen sc->pcie_slot_sta);
1066991d431fSEric van Gyzen
106782cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_ABP) {
106882cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) {
106982cb5c3bSJohn Baldwin device_printf(dev,
107082cb5c3bSJohn Baldwin "Attention Button Pressed: Detach Cancelled\n");
107182cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING;
1072b23314ecSJohn Baldwin taskqueue_cancel_timeout(taskqueue_bus,
1073fa3b03d3SAlexander Motin &sc->pcie_ab_task, NULL);
1074e0235fd3SColin Percival } else if (old_slot_sta & PCIEM_SLOT_STA_PDS) {
1075e0235fd3SColin Percival /* Only initiate detach sequence if device present. */
1076*9be42ee6SColin Percival if (pcie_hp_detach_timeout != 0) {
107782cb5c3bSJohn Baldwin device_printf(dev,
1078*9be42ee6SColin Percival "Attention Button Pressed: Detaching in %ld ms\n",
1079*9be42ee6SColin Percival (long)(pcie_hp_detach_timeout / SBT_1MS));
108082cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACH_PENDING;
1081*9be42ee6SColin Percival taskqueue_enqueue_timeout_sbt(taskqueue_bus,
1082*9be42ee6SColin Percival &sc->pcie_ab_task, pcie_hp_detach_timeout,
1083*9be42ee6SColin Percival SBT_1S, 0);
1084*9be42ee6SColin Percival } else {
1085*9be42ee6SColin Percival sc->flags |= PCIB_DETACHING;
1086*9be42ee6SColin Percival }
108782cb5c3bSJohn Baldwin }
108882cb5c3bSJohn Baldwin }
108982cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD)
109082cb5c3bSJohn Baldwin device_printf(dev, "Power Fault Detected\n");
109182cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSC)
109282cb5c3bSJohn Baldwin device_printf(dev, "MRL Sensor Changed to %s\n",
109382cb5c3bSJohn Baldwin sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS ? "open" :
109482cb5c3bSJohn Baldwin "closed");
109582cb5c3bSJohn Baldwin if (bootverbose && sc->pcie_slot_sta & PCIEM_SLOT_STA_PDC)
1096991d431fSEric van Gyzen device_printf(dev, "Presence Detect Changed to %s\n",
109782cb5c3bSJohn Baldwin sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS ? "card present" :
109882cb5c3bSJohn Baldwin "empty");
109982cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_CC)
110082cb5c3bSJohn Baldwin pcib_pcie_hotplug_command_completed(sc);
110182cb5c3bSJohn Baldwin if (sc->pcie_slot_sta & PCIEM_SLOT_STA_DLLSC) {
110282cb5c3bSJohn Baldwin sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
110382cb5c3bSJohn Baldwin if (bootverbose)
110482cb5c3bSJohn Baldwin device_printf(dev,
110582cb5c3bSJohn Baldwin "Data Link Layer State Changed to %s\n",
110682cb5c3bSJohn Baldwin sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE ?
110782cb5c3bSJohn Baldwin "active" : "inactive");
110882cb5c3bSJohn Baldwin }
110982cb5c3bSJohn Baldwin
111082cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true);
111113d700adSScott Long PCIB_HP_UNLOCK(sc);
111282cb5c3bSJohn Baldwin }
111382cb5c3bSJohn Baldwin
111482cb5c3bSJohn Baldwin static void
pcib_pcie_hotplug_task(void * context,int pending)111582cb5c3bSJohn Baldwin pcib_pcie_hotplug_task(void *context, int pending)
111682cb5c3bSJohn Baldwin {
111782cb5c3bSJohn Baldwin struct pcib_softc *sc;
111882cb5c3bSJohn Baldwin device_t dev;
111982cb5c3bSJohn Baldwin
112082cb5c3bSJohn Baldwin sc = context;
112113d700adSScott Long PCIB_HP_LOCK(sc);
112282cb5c3bSJohn Baldwin dev = sc->dev;
112382cb5c3bSJohn Baldwin if (pcib_hotplug_present(sc) != 0) {
112482cb5c3bSJohn Baldwin if (sc->child == NULL) {
11255b56413dSWarner Losh sc->child = device_add_child(dev, "pci", DEVICE_UNIT_ANY);
112618250ec6SJohn Baldwin bus_attach_children(dev);
112782cb5c3bSJohn Baldwin }
112882cb5c3bSJohn Baldwin } else {
112982cb5c3bSJohn Baldwin if (sc->child != NULL) {
113082cb5c3bSJohn Baldwin if (device_delete_child(dev, sc->child) == 0)
113182cb5c3bSJohn Baldwin sc->child = NULL;
113282cb5c3bSJohn Baldwin }
113382cb5c3bSJohn Baldwin }
113413d700adSScott Long PCIB_HP_UNLOCK(sc);
113582cb5c3bSJohn Baldwin }
113682cb5c3bSJohn Baldwin
113782cb5c3bSJohn Baldwin static void
pcib_pcie_ab_timeout(void * arg,int pending)1138fa3b03d3SAlexander Motin pcib_pcie_ab_timeout(void *arg, int pending)
113982cb5c3bSJohn Baldwin {
1140fa3b03d3SAlexander Motin struct pcib_softc *sc = arg;
114182cb5c3bSJohn Baldwin
1142fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc);
114382cb5c3bSJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) {
114482cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACHING;
114582cb5c3bSJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING;
114682cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true);
114782cb5c3bSJohn Baldwin }
1148fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc);
114982cb5c3bSJohn Baldwin }
115082cb5c3bSJohn Baldwin
115182cb5c3bSJohn Baldwin static void
pcib_pcie_cc_timeout(void * arg,int pending)1152fa3b03d3SAlexander Motin pcib_pcie_cc_timeout(void *arg, int pending)
115382cb5c3bSJohn Baldwin {
1154fa3b03d3SAlexander Motin struct pcib_softc *sc = arg;
1155fa3b03d3SAlexander Motin device_t dev = sc->dev;
11566f33eaa5SJohn Baldwin uint16_t sta;
115782cb5c3bSJohn Baldwin
1158fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc);
11596f33eaa5SJohn Baldwin sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
11606f33eaa5SJohn Baldwin if (!(sta & PCIEM_SLOT_STA_CC)) {
116121e51c82SAlexander Motin device_printf(dev, "HotPlug Command Timed Out\n");
116221e51c82SAlexander Motin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING;
11636f33eaa5SJohn Baldwin } else {
11646f33eaa5SJohn Baldwin device_printf(dev,
11656f33eaa5SJohn Baldwin "Missed HotPlug interrupt waiting for Command Completion\n");
11668a1926c5SWarner Losh pcib_pcie_intr_hotplug(sc);
116782cb5c3bSJohn Baldwin }
1168fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc);
116982cb5c3bSJohn Baldwin }
117082cb5c3bSJohn Baldwin
117182cb5c3bSJohn Baldwin static void
pcib_pcie_dll_timeout(void * arg,int pending)1172fa3b03d3SAlexander Motin pcib_pcie_dll_timeout(void *arg, int pending)
117382cb5c3bSJohn Baldwin {
1174fa3b03d3SAlexander Motin struct pcib_softc *sc = arg;
1175fa3b03d3SAlexander Motin device_t dev = sc->dev;
117682cb5c3bSJohn Baldwin uint16_t sta;
117782cb5c3bSJohn Baldwin
1178fa3b03d3SAlexander Motin PCIB_HP_LOCK(sc);
117982cb5c3bSJohn Baldwin sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
118082cb5c3bSJohn Baldwin if (!(sta & PCIEM_LINK_STA_DL_ACTIVE)) {
118182cb5c3bSJohn Baldwin device_printf(dev,
118282cb5c3bSJohn Baldwin "Timed out waiting for Data Link Layer Active\n");
118382cb5c3bSJohn Baldwin sc->flags |= PCIB_DETACHING;
118482cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, 0, 0, true);
118582cb5c3bSJohn Baldwin } else if (sta != sc->pcie_link_sta) {
118682cb5c3bSJohn Baldwin device_printf(dev,
118782cb5c3bSJohn Baldwin "Missed HotPlug interrupt waiting for DLL Active\n");
11888a1926c5SWarner Losh pcib_pcie_intr_hotplug(sc);
118982cb5c3bSJohn Baldwin }
1190fa3b03d3SAlexander Motin PCIB_HP_UNLOCK(sc);
119182cb5c3bSJohn Baldwin }
119282cb5c3bSJohn Baldwin
119382cb5c3bSJohn Baldwin static int
pcib_alloc_pcie_irq(struct pcib_softc * sc)119482cb5c3bSJohn Baldwin pcib_alloc_pcie_irq(struct pcib_softc *sc)
119582cb5c3bSJohn Baldwin {
119682cb5c3bSJohn Baldwin device_t dev;
1197e6b83836SJohn Baldwin int count, error, mem_rid, rid;
119882cb5c3bSJohn Baldwin
119982cb5c3bSJohn Baldwin rid = -1;
120082cb5c3bSJohn Baldwin dev = sc->dev;
120182cb5c3bSJohn Baldwin
120282cb5c3bSJohn Baldwin /*
120382cb5c3bSJohn Baldwin * For simplicity, only use MSI-X if there is a single message.
120482cb5c3bSJohn Baldwin * To support a device with multiple messages we would have to
120582cb5c3bSJohn Baldwin * use remap intr if the MSI number is not 0.
120682cb5c3bSJohn Baldwin */
120782cb5c3bSJohn Baldwin count = pci_msix_count(dev);
120882cb5c3bSJohn Baldwin if (count == 1) {
1209e6b83836SJohn Baldwin mem_rid = pci_msix_table_bar(dev);
1210e6b83836SJohn Baldwin sc->pcie_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
1211e6b83836SJohn Baldwin &mem_rid, RF_ACTIVE);
1212e6b83836SJohn Baldwin if (sc->pcie_mem == NULL) {
1213e6b83836SJohn Baldwin device_printf(dev,
1214e6b83836SJohn Baldwin "Failed to allocate BAR for MSI-X table\n");
1215e6b83836SJohn Baldwin } else {
121682cb5c3bSJohn Baldwin error = pci_alloc_msix(dev, &count);
121782cb5c3bSJohn Baldwin if (error == 0)
121882cb5c3bSJohn Baldwin rid = 1;
121982cb5c3bSJohn Baldwin }
1220e6b83836SJohn Baldwin }
122182cb5c3bSJohn Baldwin
122282cb5c3bSJohn Baldwin if (rid < 0 && pci_msi_count(dev) > 0) {
122382cb5c3bSJohn Baldwin count = 1;
122482cb5c3bSJohn Baldwin error = pci_alloc_msi(dev, &count);
122582cb5c3bSJohn Baldwin if (error == 0)
122682cb5c3bSJohn Baldwin rid = 1;
122782cb5c3bSJohn Baldwin }
122882cb5c3bSJohn Baldwin
122982cb5c3bSJohn Baldwin if (rid < 0)
123082cb5c3bSJohn Baldwin rid = 0;
123182cb5c3bSJohn Baldwin
123282cb5c3bSJohn Baldwin sc->pcie_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1233f14f0051SChuck Tuffli RF_ACTIVE | RF_SHAREABLE);
123482cb5c3bSJohn Baldwin if (sc->pcie_irq == NULL) {
123582cb5c3bSJohn Baldwin device_printf(dev,
123682cb5c3bSJohn Baldwin "Failed to allocate interrupt for PCI-e events\n");
123782cb5c3bSJohn Baldwin if (rid > 0)
123882cb5c3bSJohn Baldwin pci_release_msi(dev);
123982cb5c3bSJohn Baldwin return (ENXIO);
124082cb5c3bSJohn Baldwin }
124182cb5c3bSJohn Baldwin
124213d700adSScott Long error = bus_setup_intr(dev, sc->pcie_irq, INTR_TYPE_MISC|INTR_MPSAFE,
12438a1926c5SWarner Losh NULL, pcib_pcie_intr_hotplug, sc, &sc->pcie_ihand);
124482cb5c3bSJohn Baldwin if (error) {
124582cb5c3bSJohn Baldwin device_printf(dev, "Failed to setup PCI-e interrupt handler\n");
124682cb5c3bSJohn Baldwin bus_release_resource(dev, SYS_RES_IRQ, rid, sc->pcie_irq);
124782cb5c3bSJohn Baldwin if (rid > 0)
124882cb5c3bSJohn Baldwin pci_release_msi(dev);
124982cb5c3bSJohn Baldwin return (error);
125082cb5c3bSJohn Baldwin }
125182cb5c3bSJohn Baldwin return (0);
125282cb5c3bSJohn Baldwin }
125382cb5c3bSJohn Baldwin
12546f33eaa5SJohn Baldwin static int
pcib_release_pcie_irq(struct pcib_softc * sc)12556f33eaa5SJohn Baldwin pcib_release_pcie_irq(struct pcib_softc *sc)
12566f33eaa5SJohn Baldwin {
12576f33eaa5SJohn Baldwin device_t dev;
12586f33eaa5SJohn Baldwin int error;
12596f33eaa5SJohn Baldwin
12606f33eaa5SJohn Baldwin dev = sc->dev;
12616f33eaa5SJohn Baldwin error = bus_teardown_intr(dev, sc->pcie_irq, sc->pcie_ihand);
12626f33eaa5SJohn Baldwin if (error)
12636f33eaa5SJohn Baldwin return (error);
12646f33eaa5SJohn Baldwin error = bus_free_resource(dev, SYS_RES_IRQ, sc->pcie_irq);
12656f33eaa5SJohn Baldwin if (error)
12666f33eaa5SJohn Baldwin return (error);
1267e6b83836SJohn Baldwin error = pci_release_msi(dev);
1268e6b83836SJohn Baldwin if (error)
1269e6b83836SJohn Baldwin return (error);
1270e6b83836SJohn Baldwin if (sc->pcie_mem != NULL)
1271e6b83836SJohn Baldwin error = bus_free_resource(dev, SYS_RES_MEMORY, sc->pcie_mem);
1272e6b83836SJohn Baldwin return (error);
12736f33eaa5SJohn Baldwin }
12746f33eaa5SJohn Baldwin
127582cb5c3bSJohn Baldwin static void
pcib_setup_hotplug(struct pcib_softc * sc)127682cb5c3bSJohn Baldwin pcib_setup_hotplug(struct pcib_softc *sc)
127782cb5c3bSJohn Baldwin {
127882cb5c3bSJohn Baldwin device_t dev;
127982cb5c3bSJohn Baldwin uint16_t mask, val;
128082cb5c3bSJohn Baldwin
128182cb5c3bSJohn Baldwin dev = sc->dev;
128282cb5c3bSJohn Baldwin TASK_INIT(&sc->pcie_hp_task, 0, pcib_pcie_hotplug_task, sc);
1283b23314ecSJohn Baldwin TIMEOUT_TASK_INIT(taskqueue_bus, &sc->pcie_ab_task, 0,
1284fa3b03d3SAlexander Motin pcib_pcie_ab_timeout, sc);
1285b23314ecSJohn Baldwin TIMEOUT_TASK_INIT(taskqueue_bus, &sc->pcie_cc_task, 0,
1286fa3b03d3SAlexander Motin pcib_pcie_cc_timeout, sc);
1287b23314ecSJohn Baldwin TIMEOUT_TASK_INIT(taskqueue_bus, &sc->pcie_dll_task, 0,
1288fa3b03d3SAlexander Motin pcib_pcie_dll_timeout, sc);
1289c6df6f53SWarner Losh sc->pcie_hp_lock = bus_topo_mtx();
129082cb5c3bSJohn Baldwin
129182cb5c3bSJohn Baldwin /* Allocate IRQ. */
129282cb5c3bSJohn Baldwin if (pcib_alloc_pcie_irq(sc) != 0)
129382cb5c3bSJohn Baldwin return;
129482cb5c3bSJohn Baldwin
129582cb5c3bSJohn Baldwin sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
129682cb5c3bSJohn Baldwin sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
129782cb5c3bSJohn Baldwin
12986f33eaa5SJohn Baldwin /* Clear any events previously pending. */
12996f33eaa5SJohn Baldwin pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2);
13006f33eaa5SJohn Baldwin
130182cb5c3bSJohn Baldwin /* Enable HotPlug events. */
130282cb5c3bSJohn Baldwin mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE |
130382cb5c3bSJohn Baldwin PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE |
130482cb5c3bSJohn Baldwin PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE;
130537290148SEric van Gyzen val = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | PCIEM_SLOT_CTL_PDCE;
130682cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_APB)
130782cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_ABPE;
130882cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP)
130982cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_PFDE;
131082cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP)
131182cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_MRLSCE;
131282cb5c3bSJohn Baldwin if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS))
131382cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_CCIE;
131482cb5c3bSJohn Baldwin
131582cb5c3bSJohn Baldwin /* Turn the attention indicator off. */
131682cb5c3bSJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) {
131782cb5c3bSJohn Baldwin mask |= PCIEM_SLOT_CTL_AIC;
131882cb5c3bSJohn Baldwin val |= PCIEM_SLOT_CTL_AI_OFF;
131982cb5c3bSJohn Baldwin }
132082cb5c3bSJohn Baldwin
132182cb5c3bSJohn Baldwin pcib_pcie_hotplug_update(sc, val, mask, false);
132282cb5c3bSJohn Baldwin }
13236f33eaa5SJohn Baldwin
13246f33eaa5SJohn Baldwin static int
pcib_detach_hotplug(struct pcib_softc * sc)13256f33eaa5SJohn Baldwin pcib_detach_hotplug(struct pcib_softc *sc)
13266f33eaa5SJohn Baldwin {
13276f33eaa5SJohn Baldwin uint16_t mask, val;
13286f33eaa5SJohn Baldwin int error;
13296f33eaa5SJohn Baldwin
13306f33eaa5SJohn Baldwin /* Disable the card in the slot and force it to detach. */
13316f33eaa5SJohn Baldwin if (sc->flags & PCIB_DETACH_PENDING) {
13326f33eaa5SJohn Baldwin sc->flags &= ~PCIB_DETACH_PENDING;
1333b23314ecSJohn Baldwin taskqueue_cancel_timeout(taskqueue_bus, &sc->pcie_ab_task,
1334fa3b03d3SAlexander Motin NULL);
13356f33eaa5SJohn Baldwin }
13366f33eaa5SJohn Baldwin sc->flags |= PCIB_DETACHING;
13376f33eaa5SJohn Baldwin
13386f33eaa5SJohn Baldwin if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) {
1339b23314ecSJohn Baldwin taskqueue_cancel_timeout(taskqueue_bus, &sc->pcie_cc_task,
1340fa3b03d3SAlexander Motin NULL);
13416f33eaa5SJohn Baldwin tsleep(sc, 0, "hpcmd", hz);
13426f33eaa5SJohn Baldwin sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING;
13436f33eaa5SJohn Baldwin }
13446f33eaa5SJohn Baldwin
13456f33eaa5SJohn Baldwin /* Disable HotPlug events. */
13466f33eaa5SJohn Baldwin mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE |
13476f33eaa5SJohn Baldwin PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE |
13486f33eaa5SJohn Baldwin PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE;
13496f33eaa5SJohn Baldwin val = 0;
13506f33eaa5SJohn Baldwin
13516f33eaa5SJohn Baldwin /* Turn the attention indicator off. */
13526f33eaa5SJohn Baldwin if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) {
13536f33eaa5SJohn Baldwin mask |= PCIEM_SLOT_CTL_AIC;
13546f33eaa5SJohn Baldwin val |= PCIEM_SLOT_CTL_AI_OFF;
13556f33eaa5SJohn Baldwin }
13566f33eaa5SJohn Baldwin
13576f33eaa5SJohn Baldwin pcib_pcie_hotplug_update(sc, val, mask, false);
13586f33eaa5SJohn Baldwin
13596f33eaa5SJohn Baldwin error = pcib_release_pcie_irq(sc);
13606f33eaa5SJohn Baldwin if (error)
13616f33eaa5SJohn Baldwin return (error);
1362b23314ecSJohn Baldwin taskqueue_drain(taskqueue_bus, &sc->pcie_hp_task);
1363b23314ecSJohn Baldwin taskqueue_drain_timeout(taskqueue_bus, &sc->pcie_ab_task);
1364b23314ecSJohn Baldwin taskqueue_drain_timeout(taskqueue_bus, &sc->pcie_cc_task);
1365b23314ecSJohn Baldwin taskqueue_drain_timeout(taskqueue_bus, &sc->pcie_dll_task);
13666f33eaa5SJohn Baldwin return (0);
13676f33eaa5SJohn Baldwin }
136882cb5c3bSJohn Baldwin #endif
136982cb5c3bSJohn Baldwin
1370e36af292SJung-uk Kim /*
1371e36af292SJung-uk Kim * Restore previous bridge configuration.
1372e36af292SJung-uk Kim */
1373e36af292SJung-uk Kim static void
pcib_cfg_restore(struct pcib_softc * sc)1374e36af292SJung-uk Kim pcib_cfg_restore(struct pcib_softc *sc)
1375e36af292SJung-uk Kim {
137683c41143SJohn Baldwin pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM);
1377e36af292SJung-uk Kim }
1378e36af292SJung-uk Kim
1379e36af292SJung-uk Kim /*
1380bb0d0a8eSMike Smith * Generic device interface
1381bb0d0a8eSMike Smith */
1382bb0d0a8eSMike Smith static int
pcib_probe(device_t dev)1383bb0d0a8eSMike Smith pcib_probe(device_t dev)
1384bb0d0a8eSMike Smith {
1385bb0d0a8eSMike Smith if ((pci_get_class(dev) == PCIC_BRIDGE) &&
1386bb0d0a8eSMike Smith (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
1387bb0d0a8eSMike Smith device_set_desc(dev, "PCI-PCI bridge");
1388b7cbd25bSMarcel Moolenaar return(-10000);
1389bb0d0a8eSMike Smith }
1390bb0d0a8eSMike Smith return(ENXIO);
1391bb0d0a8eSMike Smith }
1392bb0d0a8eSMike Smith
13936f0d5884SJohn Baldwin void
pcib_attach_common(device_t dev)13946f0d5884SJohn Baldwin pcib_attach_common(device_t dev)
1395bb0d0a8eSMike Smith {
1396bb0d0a8eSMike Smith struct pcib_softc *sc;
1397abf07f13SWarner Losh struct sysctl_ctx_list *sctx;
1398abf07f13SWarner Losh struct sysctl_oid *soid;
1399c825d4dcSJohn Baldwin int comma;
1400bb0d0a8eSMike Smith
1401bb0d0a8eSMike Smith sc = device_get_softc(dev);
1402bb0d0a8eSMike Smith sc->dev = dev;
1403bb0d0a8eSMike Smith
14044fa59183SMike Smith /*
14054fa59183SMike Smith * Get current bridge configuration.
14064fa59183SMike Smith */
140755aaf894SMarius Strobl sc->domain = pci_get_domain(dev);
1408ad6f36f8SJohn Baldwin sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
14094fa59183SMike Smith
14104fa59183SMike Smith /*
14114edef187SJohn Baldwin * The primary bus register should always be the bus of the
14124edef187SJohn Baldwin * parent.
14134edef187SJohn Baldwin */
14144edef187SJohn Baldwin sc->pribus = pci_get_bus(dev);
14154edef187SJohn Baldwin pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1);
14164edef187SJohn Baldwin
14174edef187SJohn Baldwin /*
1418abf07f13SWarner Losh * Setup sysctl reporting nodes
1419abf07f13SWarner Losh */
1420abf07f13SWarner Losh sctx = device_get_sysctl_ctx(dev);
1421abf07f13SWarner Losh soid = device_get_sysctl_tree(dev);
1422abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
1423abf07f13SWarner Losh CTLFLAG_RD, &sc->domain, 0, "Domain number");
1424abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
1425abf07f13SWarner Losh CTLFLAG_RD, &sc->pribus, 0, "Primary bus number");
1426abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
14274edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number");
1428abf07f13SWarner Losh SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
14294edef187SJohn Baldwin CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number");
1430abf07f13SWarner Losh
1431abf07f13SWarner Losh /*
14324fa59183SMike Smith * Quirk handling.
14334fa59183SMike Smith */
14344fa59183SMike Smith switch (pci_get_devid(dev)) {
1435e4b59fc5SWarner Losh /*
1436e4b59fc5SWarner Losh * The i82380FB mobile docking controller is a PCI-PCI bridge,
1437e4b59fc5SWarner Losh * and it is a subtractive bridge. However, the ProgIf is wrong
1438e4b59fc5SWarner Losh * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
14394718610dSZbigniew Bodek * happen. There are also Toshiba and Cavium ThunderX bridges
14404718610dSZbigniew Bodek * that behave this way.
1441e4b59fc5SWarner Losh */
14424718610dSZbigniew Bodek case 0xa002177d: /* Cavium ThunderX */
1443e4b59fc5SWarner Losh case 0x124b8086: /* Intel 82380FB Mobile */
1444e4b59fc5SWarner Losh case 0x060513d7: /* Toshiba ???? */
1445e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE;
1446e4b59fc5SWarner Losh break;
1447e4b59fc5SWarner Losh }
1448e4b59fc5SWarner Losh
144922bf1c7fSJohn Baldwin if (pci_msi_device_blacklisted(dev))
145022bf1c7fSJohn Baldwin sc->flags |= PCIB_DISABLE_MSI;
145122bf1c7fSJohn Baldwin
145268e9cbd3SMarius Strobl if (pci_msix_device_blacklisted(dev))
145368e9cbd3SMarius Strobl sc->flags |= PCIB_DISABLE_MSIX;
145468e9cbd3SMarius Strobl
1455e4b59fc5SWarner Losh /*
1456e4b59fc5SWarner Losh * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
1457e4b59fc5SWarner Losh * but have a ProgIF of 0x80. The 82801 family (AA, AB, BAM/CAM,
1458e4b59fc5SWarner Losh * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
1459e4b59fc5SWarner Losh * This means they act as if they were subtractively decoding
1460e4b59fc5SWarner Losh * bridges and pass all transactions. Mark them and real ProgIf 1
1461e4b59fc5SWarner Losh * parts as subtractive.
1462e4b59fc5SWarner Losh */
1463e4b59fc5SWarner Losh if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
1464657d9f9fSJohn Baldwin pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
1465e4b59fc5SWarner Losh sc->flags |= PCIB_SUBTRACTIVE;
1466e4b59fc5SWarner Losh
146782cb5c3bSJohn Baldwin #ifdef PCI_HP
146882cb5c3bSJohn Baldwin pcib_probe_hotplug(sc);
146982cb5c3bSJohn Baldwin #endif
14704edef187SJohn Baldwin pcib_setup_secbus(dev, &sc->bus, 1);
147183c41143SJohn Baldwin pcib_probe_windows(sc);
147282cb5c3bSJohn Baldwin #ifdef PCI_HP
147382cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG)
147482cb5c3bSJohn Baldwin pcib_setup_hotplug(sc);
147582cb5c3bSJohn Baldwin #endif
1476bb0d0a8eSMike Smith if (bootverbose) {
147755aaf894SMarius Strobl device_printf(dev, " domain %d\n", sc->domain);
14784edef187SJohn Baldwin device_printf(dev, " secondary bus %d\n", sc->bus.sec);
14794edef187SJohn Baldwin device_printf(dev, " subordinate bus %d\n", sc->bus.sub);
148083c41143SJohn Baldwin if (pcib_is_window_open(&sc->io))
148183c41143SJohn Baldwin device_printf(dev, " I/O decode 0x%jx-0x%jx\n",
148283c41143SJohn Baldwin (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit);
148383c41143SJohn Baldwin if (pcib_is_window_open(&sc->mem))
148483c41143SJohn Baldwin device_printf(dev, " memory decode 0x%jx-0x%jx\n",
148583c41143SJohn Baldwin (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit);
148683c41143SJohn Baldwin if (pcib_is_window_open(&sc->pmem))
148783c41143SJohn Baldwin device_printf(dev, " prefetched decode 0x%jx-0x%jx\n",
148883c41143SJohn Baldwin (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit);
1489c825d4dcSJohn Baldwin if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) ||
1490c825d4dcSJohn Baldwin sc->flags & PCIB_SUBTRACTIVE) {
1491c825d4dcSJohn Baldwin device_printf(dev, " special decode ");
1492c825d4dcSJohn Baldwin comma = 0;
1493c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) {
1494c825d4dcSJohn Baldwin printf("ISA");
1495c825d4dcSJohn Baldwin comma = 1;
1496c825d4dcSJohn Baldwin }
1497c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) {
1498c825d4dcSJohn Baldwin printf("%sVGA", comma ? ", " : "");
1499c825d4dcSJohn Baldwin comma = 1;
1500c825d4dcSJohn Baldwin }
1501e4b59fc5SWarner Losh if (sc->flags & PCIB_SUBTRACTIVE)
1502c825d4dcSJohn Baldwin printf("%ssubtractive", comma ? ", " : "");
1503c825d4dcSJohn Baldwin printf("\n");
1504c825d4dcSJohn Baldwin }
1505bb0d0a8eSMike Smith }
1506bb0d0a8eSMike Smith
1507bb0d0a8eSMike Smith /*
1508ef888152SJohn Baldwin * Always enable busmastering on bridges so that transactions
1509ef888152SJohn Baldwin * initiated on the secondary bus are passed through to the
1510ef888152SJohn Baldwin * primary bus.
1511ef888152SJohn Baldwin */
1512ef888152SJohn Baldwin pci_enable_busmaster(dev);
15136f0d5884SJohn Baldwin }
1514bb0d0a8eSMike Smith
151582cb5c3bSJohn Baldwin #ifdef PCI_HP
151682cb5c3bSJohn Baldwin static int
pcib_present(struct pcib_softc * sc)151782cb5c3bSJohn Baldwin pcib_present(struct pcib_softc *sc)
151882cb5c3bSJohn Baldwin {
151982cb5c3bSJohn Baldwin
152082cb5c3bSJohn Baldwin if (sc->flags & PCIB_HOTPLUG)
152182cb5c3bSJohn Baldwin return (pcib_hotplug_present(sc) != 0);
152282cb5c3bSJohn Baldwin return (1);
152382cb5c3bSJohn Baldwin }
152482cb5c3bSJohn Baldwin #endif
152582cb5c3bSJohn Baldwin
152638906aedSJohn Baldwin int
pcib_attach_child(device_t dev)152767e7d085SJohn Baldwin pcib_attach_child(device_t dev)
15286f0d5884SJohn Baldwin {
15296f0d5884SJohn Baldwin struct pcib_softc *sc;
15306f0d5884SJohn Baldwin
15316f0d5884SJohn Baldwin sc = device_get_softc(dev);
153267e7d085SJohn Baldwin if (sc->bus.sec == 0) {
153367e7d085SJohn Baldwin /* no secondary bus; we should have fixed this */
153467e7d085SJohn Baldwin return(0);
153567e7d085SJohn Baldwin }
153667e7d085SJohn Baldwin
153782cb5c3bSJohn Baldwin #ifdef PCI_HP
153882cb5c3bSJohn Baldwin if (!pcib_present(sc)) {
153982cb5c3bSJohn Baldwin /* An empty HotPlug slot, so don't add a PCI bus yet. */
154082cb5c3bSJohn Baldwin return (0);
154182cb5c3bSJohn Baldwin }
154282cb5c3bSJohn Baldwin #endif
154382cb5c3bSJohn Baldwin
15445b56413dSWarner Losh sc->child = device_add_child(dev, "pci", DEVICE_UNIT_ANY);
154518250ec6SJohn Baldwin bus_attach_children(dev);
154618250ec6SJohn Baldwin return (0);
1547bb0d0a8eSMike Smith }
1548bb0d0a8eSMike Smith
154967e7d085SJohn Baldwin int
pcib_attach(device_t dev)155067e7d085SJohn Baldwin pcib_attach(device_t dev)
155167e7d085SJohn Baldwin {
155267e7d085SJohn Baldwin
155367e7d085SJohn Baldwin pcib_attach_common(dev);
155467e7d085SJohn Baldwin return (pcib_attach_child(dev));
1555bb0d0a8eSMike Smith }
1556bb0d0a8eSMike Smith
15576f0d5884SJohn Baldwin int
pcib_detach(device_t dev)15586f33eaa5SJohn Baldwin pcib_detach(device_t dev)
15596f33eaa5SJohn Baldwin {
15606f33eaa5SJohn Baldwin struct pcib_softc *sc;
15616f33eaa5SJohn Baldwin int error;
15626f33eaa5SJohn Baldwin
15636f33eaa5SJohn Baldwin sc = device_get_softc(dev);
1564e9d38570SJohn Baldwin error = bus_detach_children(dev);
15656f33eaa5SJohn Baldwin if (error)
15666f33eaa5SJohn Baldwin return (error);
15676f33eaa5SJohn Baldwin #ifdef PCI_HP
15686f33eaa5SJohn Baldwin if (sc->flags & PCIB_HOTPLUG) {
15696f33eaa5SJohn Baldwin error = pcib_detach_hotplug(sc);
15706f33eaa5SJohn Baldwin if (error)
15716f33eaa5SJohn Baldwin return (error);
15726f33eaa5SJohn Baldwin }
15736f33eaa5SJohn Baldwin #endif
15746f33eaa5SJohn Baldwin error = device_delete_children(dev);
15756f33eaa5SJohn Baldwin if (error)
15766f33eaa5SJohn Baldwin return (error);
15776f33eaa5SJohn Baldwin pcib_free_windows(sc);
15786f33eaa5SJohn Baldwin pcib_free_secbus(dev, &sc->bus);
15796f33eaa5SJohn Baldwin return (0);
15806f33eaa5SJohn Baldwin }
15816f33eaa5SJohn Baldwin
15826f33eaa5SJohn Baldwin int
pcib_resume(device_t dev)1583e36af292SJung-uk Kim pcib_resume(device_t dev)
1584e36af292SJung-uk Kim {
1585e36af292SJung-uk Kim
1586e36af292SJung-uk Kim pcib_cfg_restore(device_get_softc(dev));
1587cffd37daSAndriy Gapon
1588cffd37daSAndriy Gapon /*
1589cffd37daSAndriy Gapon * Restore the Command register only after restoring the windows.
1590cffd37daSAndriy Gapon * The bridge should not be claiming random windows.
1591cffd37daSAndriy Gapon */
1592cffd37daSAndriy Gapon pci_write_config(dev, PCIR_COMMAND, pci_get_cmdreg(dev), 2);
1593e36af292SJung-uk Kim return (bus_generic_resume(dev));
1594e36af292SJung-uk Kim }
1595e36af292SJung-uk Kim
1596809923caSJustin Hibbits void
pcib_bridge_init(device_t dev)1597809923caSJustin Hibbits pcib_bridge_init(device_t dev)
1598809923caSJustin Hibbits {
1599809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
1600809923caSJustin Hibbits pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2);
1601809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1);
1602809923caSJustin Hibbits pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2);
1603809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2);
1604809923caSJustin Hibbits pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2);
1605809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
1606809923caSJustin Hibbits pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4);
1607809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2);
1608809923caSJustin Hibbits pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4);
1609809923caSJustin Hibbits }
1610809923caSJustin Hibbits
1611e36af292SJung-uk Kim int
pcib_child_present(device_t dev,device_t child)161282cb5c3bSJohn Baldwin pcib_child_present(device_t dev, device_t child)
161382cb5c3bSJohn Baldwin {
161482cb5c3bSJohn Baldwin #ifdef PCI_HP
161582cb5c3bSJohn Baldwin struct pcib_softc *sc = device_get_softc(dev);
161682cb5c3bSJohn Baldwin int retval;
161782cb5c3bSJohn Baldwin
161882cb5c3bSJohn Baldwin retval = bus_child_present(dev);
161982cb5c3bSJohn Baldwin if (retval != 0 && sc->flags & PCIB_HOTPLUG)
162082cb5c3bSJohn Baldwin retval = pcib_hotplug_present(sc);
162182cb5c3bSJohn Baldwin return (retval);
162282cb5c3bSJohn Baldwin #else
162382cb5c3bSJohn Baldwin return (bus_child_present(dev));
162482cb5c3bSJohn Baldwin #endif
162582cb5c3bSJohn Baldwin }
162682cb5c3bSJohn Baldwin
162782cb5c3bSJohn Baldwin int
pcib_read_ivar(device_t dev,device_t child,int which,uintptr_t * result)1628bb0d0a8eSMike Smith pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1629bb0d0a8eSMike Smith {
1630bb0d0a8eSMike Smith struct pcib_softc *sc = device_get_softc(dev);
1631bb0d0a8eSMike Smith
1632bb0d0a8eSMike Smith switch (which) {
163355aaf894SMarius Strobl case PCIB_IVAR_DOMAIN:
163455aaf894SMarius Strobl *result = sc->domain;
163555aaf894SMarius Strobl return(0);
1636bb0d0a8eSMike Smith case PCIB_IVAR_BUS:
16374edef187SJohn Baldwin *result = sc->bus.sec;
1638bb0d0a8eSMike Smith return(0);
1639bb0d0a8eSMike Smith }
1640bb0d0a8eSMike Smith return(ENOENT);
1641bb0d0a8eSMike Smith }
1642bb0d0a8eSMike Smith
16436f0d5884SJohn Baldwin int
pcib_write_ivar(device_t dev,device_t child,int which,uintptr_t value)1644bb0d0a8eSMike Smith pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1645bb0d0a8eSMike Smith {
1646bb0d0a8eSMike Smith
1647bb0d0a8eSMike Smith switch (which) {
164855aaf894SMarius Strobl case PCIB_IVAR_DOMAIN:
164955aaf894SMarius Strobl return(EINVAL);
1650bb0d0a8eSMike Smith case PCIB_IVAR_BUS:
16514edef187SJohn Baldwin return(EINVAL);
1652bb0d0a8eSMike Smith }
1653bb0d0a8eSMike Smith return(ENOENT);
1654bb0d0a8eSMike Smith }
1655bb0d0a8eSMike Smith
165683c41143SJohn Baldwin /*
165783c41143SJohn Baldwin * Attempt to allocate a resource from the existing resources assigned
165883c41143SJohn Baldwin * to a window.
165983c41143SJohn Baldwin */
166083c41143SJohn Baldwin static struct resource *
pcib_suballoc_resource(struct pcib_softc * sc,struct pcib_window * w,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)166183c41143SJohn Baldwin pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w,
16622dd1bdf1SJustin Hibbits device_t child, int type, int *rid, rman_res_t start, rman_res_t end,
16632dd1bdf1SJustin Hibbits rman_res_t count, u_int flags)
166483c41143SJohn Baldwin {
166583c41143SJohn Baldwin struct resource *res;
166683c41143SJohn Baldwin
166783c41143SJohn Baldwin if (!pcib_is_window_open(w))
166883c41143SJohn Baldwin return (NULL);
166983c41143SJohn Baldwin
167083c41143SJohn Baldwin res = rman_reserve_resource(&w->rman, start, end, count,
167183c41143SJohn Baldwin flags & ~RF_ACTIVE, child);
167283c41143SJohn Baldwin if (res == NULL)
167383c41143SJohn Baldwin return (NULL);
167483c41143SJohn Baldwin
167583c41143SJohn Baldwin if (bootverbose)
167683c41143SJohn Baldwin device_printf(sc->dev,
1677da1b038aSJustin Hibbits "allocated %s range (%#jx-%#jx) for rid %x of %s\n",
167883c41143SJohn Baldwin w->name, rman_get_start(res), rman_get_end(res), *rid,
167983c41143SJohn Baldwin pcib_child_name(child));
168083c41143SJohn Baldwin rman_set_rid(res, *rid);
16811b9bcfffSJohn Baldwin rman_set_type(res, type);
168283c41143SJohn Baldwin
168383c41143SJohn Baldwin if (flags & RF_ACTIVE) {
168483c41143SJohn Baldwin if (bus_activate_resource(child, type, *rid, res) != 0) {
168583c41143SJohn Baldwin rman_release_resource(res);
168683c41143SJohn Baldwin return (NULL);
168783c41143SJohn Baldwin }
168883c41143SJohn Baldwin }
168983c41143SJohn Baldwin
169083c41143SJohn Baldwin return (res);
169183c41143SJohn Baldwin }
169283c41143SJohn Baldwin
1693c825d4dcSJohn Baldwin /* Allocate a fresh resource range for an unconfigured window. */
1694c825d4dcSJohn Baldwin static int
pcib_alloc_new_window(struct pcib_softc * sc,struct pcib_window * w,int type,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)1695c825d4dcSJohn Baldwin pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type,
16962dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
1697c825d4dcSJohn Baldwin {
1698c825d4dcSJohn Baldwin struct resource *res;
16992dd1bdf1SJustin Hibbits rman_res_t base, limit, wmask;
1700c825d4dcSJohn Baldwin int rid;
1701c825d4dcSJohn Baldwin
1702c825d4dcSJohn Baldwin /*
1703c825d4dcSJohn Baldwin * If this is an I/O window on a bridge with ISA enable set
1704c825d4dcSJohn Baldwin * and the start address is below 64k, then try to allocate an
1705c825d4dcSJohn Baldwin * initial window of 0x1000 bytes long starting at address
1706c825d4dcSJohn Baldwin * 0xf000 and walking down. Note that if the original request
1707c825d4dcSJohn Baldwin * was larger than the non-aliased range size of 0x100 our
1708c825d4dcSJohn Baldwin * caller would have raised the start address up to 64k
1709c825d4dcSJohn Baldwin * already.
1710c825d4dcSJohn Baldwin */
1711c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1712c825d4dcSJohn Baldwin start < 65536) {
1713c825d4dcSJohn Baldwin for (base = 0xf000; (long)base >= 0; base -= 0x1000) {
1714c825d4dcSJohn Baldwin limit = base + 0xfff;
1715c825d4dcSJohn Baldwin
1716c825d4dcSJohn Baldwin /*
1717c825d4dcSJohn Baldwin * Skip ranges that wouldn't work for the
1718c825d4dcSJohn Baldwin * original request. Note that the actual
1719c825d4dcSJohn Baldwin * window that overlaps are the non-alias
1720c825d4dcSJohn Baldwin * ranges within [base, limit], so this isn't
1721c825d4dcSJohn Baldwin * quite a simple comparison.
1722c825d4dcSJohn Baldwin */
1723c825d4dcSJohn Baldwin if (start + count > limit - 0x400)
1724c825d4dcSJohn Baldwin continue;
1725c825d4dcSJohn Baldwin if (base == 0) {
1726c825d4dcSJohn Baldwin /*
1727c825d4dcSJohn Baldwin * The first open region for the window at
1728c825d4dcSJohn Baldwin * 0 is 0x400-0x4ff.
1729c825d4dcSJohn Baldwin */
1730c825d4dcSJohn Baldwin if (end - count + 1 < 0x400)
1731c825d4dcSJohn Baldwin continue;
1732c825d4dcSJohn Baldwin } else {
1733c825d4dcSJohn Baldwin if (end - count + 1 < base)
1734c825d4dcSJohn Baldwin continue;
1735c825d4dcSJohn Baldwin }
1736c825d4dcSJohn Baldwin
1737c825d4dcSJohn Baldwin if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) {
1738c825d4dcSJohn Baldwin w->base = base;
1739c825d4dcSJohn Baldwin w->limit = limit;
1740c825d4dcSJohn Baldwin return (0);
1741c825d4dcSJohn Baldwin }
1742c825d4dcSJohn Baldwin }
1743c825d4dcSJohn Baldwin return (ENOSPC);
1744c825d4dcSJohn Baldwin }
1745c825d4dcSJohn Baldwin
174689977ce2SJustin Hibbits wmask = ((rman_res_t)1 << w->step) - 1;
1747c825d4dcSJohn Baldwin if (RF_ALIGNMENT(flags) < w->step) {
1748c825d4dcSJohn Baldwin flags &= ~RF_ALIGNMENT_MASK;
1749c825d4dcSJohn Baldwin flags |= RF_ALIGNMENT_LOG2(w->step);
1750c825d4dcSJohn Baldwin }
1751c825d4dcSJohn Baldwin start &= ~wmask;
1752c825d4dcSJohn Baldwin end |= wmask;
175389977ce2SJustin Hibbits count = roundup2(count, (rman_res_t)1 << w->step);
1754c825d4dcSJohn Baldwin rid = w->reg;
1755c825d4dcSJohn Baldwin res = bus_alloc_resource(sc->dev, type, &rid, start, end, count,
1756b377ff81SJohn Baldwin flags | RF_ACTIVE | RF_UNMAPPED);
1757c825d4dcSJohn Baldwin if (res == NULL)
1758c825d4dcSJohn Baldwin return (ENOSPC);
1759c825d4dcSJohn Baldwin pcib_add_window_resources(w, &res, 1);
1760c825d4dcSJohn Baldwin pcib_activate_window(sc, type);
1761c825d4dcSJohn Baldwin w->base = rman_get_start(res);
1762c825d4dcSJohn Baldwin w->limit = rman_get_end(res);
1763c825d4dcSJohn Baldwin return (0);
1764c825d4dcSJohn Baldwin }
1765c825d4dcSJohn Baldwin
1766c825d4dcSJohn Baldwin /* Try to expand an existing window to the requested base and limit. */
1767c825d4dcSJohn Baldwin static int
pcib_expand_window(struct pcib_softc * sc,struct pcib_window * w,int type,rman_res_t base,rman_res_t limit)1768c825d4dcSJohn Baldwin pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type,
17692dd1bdf1SJustin Hibbits rman_res_t base, rman_res_t limit)
1770c825d4dcSJohn Baldwin {
1771c825d4dcSJohn Baldwin struct resource *res;
1772c825d4dcSJohn Baldwin int error, i, force_64k_base;
1773c825d4dcSJohn Baldwin
1774c825d4dcSJohn Baldwin KASSERT(base <= w->base && limit >= w->limit,
1775c825d4dcSJohn Baldwin ("attempting to shrink window"));
1776c825d4dcSJohn Baldwin
1777c825d4dcSJohn Baldwin /*
1778c825d4dcSJohn Baldwin * XXX: pcib_grow_window() doesn't try to do this anyway and
1779c825d4dcSJohn Baldwin * the error handling for all the edge cases would be tedious.
1780c825d4dcSJohn Baldwin */
1781c825d4dcSJohn Baldwin KASSERT(limit == w->limit || base == w->base,
1782c825d4dcSJohn Baldwin ("attempting to grow both ends of a window"));
1783c825d4dcSJohn Baldwin
1784c825d4dcSJohn Baldwin /*
1785c825d4dcSJohn Baldwin * Yet more special handling for requests to expand an I/O
1786c825d4dcSJohn Baldwin * window behind an ISA-enabled bridge. Since I/O windows
1787c825d4dcSJohn Baldwin * have to grow in 0x1000 increments and the end of the 0xffff
1788c825d4dcSJohn Baldwin * range is an alias, growing a window below 64k will always
1789c825d4dcSJohn Baldwin * result in allocating new resources and never adjusting an
1790c825d4dcSJohn Baldwin * existing resource.
1791c825d4dcSJohn Baldwin */
1792c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1793c825d4dcSJohn Baldwin (limit <= 65535 || (base <= 65535 && base != w->base))) {
1794c825d4dcSJohn Baldwin KASSERT(limit == w->limit || limit <= 65535,
1795c825d4dcSJohn Baldwin ("attempting to grow both ends across 64k ISA alias"));
1796c825d4dcSJohn Baldwin
1797c825d4dcSJohn Baldwin if (base != w->base)
1798c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1);
1799c825d4dcSJohn Baldwin else
1800c825d4dcSJohn Baldwin error = pcib_alloc_nonisa_ranges(sc, w->limit + 1,
1801c825d4dcSJohn Baldwin limit);
1802c825d4dcSJohn Baldwin if (error == 0) {
1803c825d4dcSJohn Baldwin w->base = base;
1804c825d4dcSJohn Baldwin w->limit = limit;
1805c825d4dcSJohn Baldwin }
1806c825d4dcSJohn Baldwin return (error);
1807c825d4dcSJohn Baldwin }
1808c825d4dcSJohn Baldwin
1809c825d4dcSJohn Baldwin /*
1810c825d4dcSJohn Baldwin * Find the existing resource to adjust. Usually there is only one,
1811c825d4dcSJohn Baldwin * but for an ISA-enabled bridge we might be growing the I/O window
1812c825d4dcSJohn Baldwin * above 64k and need to find the existing resource that maps all
1813c825d4dcSJohn Baldwin * of the area above 64k.
1814c825d4dcSJohn Baldwin */
1815c825d4dcSJohn Baldwin for (i = 0; i < w->count; i++) {
1816c825d4dcSJohn Baldwin if (rman_get_end(w->res[i]) == w->limit)
1817c825d4dcSJohn Baldwin break;
1818c825d4dcSJohn Baldwin }
1819c825d4dcSJohn Baldwin KASSERT(i != w->count, ("did not find existing resource"));
1820c825d4dcSJohn Baldwin res = w->res[i];
1821c825d4dcSJohn Baldwin
1822c825d4dcSJohn Baldwin /*
1823c825d4dcSJohn Baldwin * Usually the resource we found should match the window's
1824c825d4dcSJohn Baldwin * existing range. The one exception is the ISA-enabled case
1825c825d4dcSJohn Baldwin * mentioned above in which case the resource should start at
1826c825d4dcSJohn Baldwin * 64k.
1827c825d4dcSJohn Baldwin */
1828c825d4dcSJohn Baldwin if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1829c825d4dcSJohn Baldwin w->base <= 65535) {
1830c825d4dcSJohn Baldwin KASSERT(rman_get_start(res) == 65536,
1831c825d4dcSJohn Baldwin ("existing resource mismatch"));
1832c825d4dcSJohn Baldwin force_64k_base = 1;
1833c825d4dcSJohn Baldwin } else {
1834c825d4dcSJohn Baldwin KASSERT(w->base == rman_get_start(res),
1835c825d4dcSJohn Baldwin ("existing resource mismatch"));
1836c825d4dcSJohn Baldwin force_64k_base = 0;
1837c825d4dcSJohn Baldwin }
1838c825d4dcSJohn Baldwin
1839c825d4dcSJohn Baldwin error = bus_adjust_resource(sc->dev, type, res, force_64k_base ?
1840c825d4dcSJohn Baldwin rman_get_start(res) : base, limit);
1841c825d4dcSJohn Baldwin if (error)
1842c825d4dcSJohn Baldwin return (error);
1843c825d4dcSJohn Baldwin
1844c825d4dcSJohn Baldwin /* Add the newly allocated region to the resource manager. */
1845c825d4dcSJohn Baldwin if (w->base != base) {
1846c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, base, w->base - 1);
1847c825d4dcSJohn Baldwin w->base = base;
1848c825d4dcSJohn Baldwin } else {
1849c825d4dcSJohn Baldwin error = rman_manage_region(&w->rman, w->limit + 1, limit);
1850c825d4dcSJohn Baldwin w->limit = limit;
1851c825d4dcSJohn Baldwin }
1852c825d4dcSJohn Baldwin if (error) {
1853c825d4dcSJohn Baldwin if (bootverbose)
1854c825d4dcSJohn Baldwin device_printf(sc->dev,
1855c825d4dcSJohn Baldwin "failed to expand %s resource manager\n", w->name);
1856c825d4dcSJohn Baldwin (void)bus_adjust_resource(sc->dev, type, res, force_64k_base ?
1857c825d4dcSJohn Baldwin rman_get_start(res) : w->base, w->limit);
1858c825d4dcSJohn Baldwin }
1859c825d4dcSJohn Baldwin return (error);
1860c825d4dcSJohn Baldwin }
1861c825d4dcSJohn Baldwin
186283c41143SJohn Baldwin /*
186383c41143SJohn Baldwin * Attempt to grow a window to make room for a given resource request.
186483c41143SJohn Baldwin */
186583c41143SJohn Baldwin static int
pcib_grow_window(struct pcib_softc * sc,struct pcib_window * w,int type,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)186683c41143SJohn Baldwin pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type,
18672dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
186883c41143SJohn Baldwin {
18692dd1bdf1SJustin Hibbits rman_res_t align, start_free, end_free, front, back, wmask;
1870c825d4dcSJohn Baldwin int error;
187183c41143SJohn Baldwin
187283c41143SJohn Baldwin /*
187383c41143SJohn Baldwin * Clamp the desired resource range to the maximum address
187483c41143SJohn Baldwin * this window supports. Reject impossible requests.
1875c825d4dcSJohn Baldwin *
1876c825d4dcSJohn Baldwin * For I/O port requests behind a bridge with the ISA enable
1877c825d4dcSJohn Baldwin * bit set, force large allocations to start above 64k.
187883c41143SJohn Baldwin */
187983c41143SJohn Baldwin if (!w->valid)
188083c41143SJohn Baldwin return (EINVAL);
1881c825d4dcSJohn Baldwin if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 &&
1882c825d4dcSJohn Baldwin start < 65536)
1883c825d4dcSJohn Baldwin start = 65536;
188483c41143SJohn Baldwin if (end > w->rman.rm_end)
188583c41143SJohn Baldwin end = w->rman.rm_end;
188683c41143SJohn Baldwin if (start + count - 1 > end || start + count < start)
188783c41143SJohn Baldwin return (EINVAL);
188889977ce2SJustin Hibbits wmask = ((rman_res_t)1 << w->step) - 1;
188983c41143SJohn Baldwin
189083c41143SJohn Baldwin /*
189183c41143SJohn Baldwin * If there is no resource at all, just try to allocate enough
189283c41143SJohn Baldwin * aligned space for this resource.
189383c41143SJohn Baldwin */
189483c41143SJohn Baldwin if (w->res == NULL) {
1895c825d4dcSJohn Baldwin error = pcib_alloc_new_window(sc, w, type, start, end, count,
1896c825d4dcSJohn Baldwin flags);
1897c825d4dcSJohn Baldwin if (error) {
189883c41143SJohn Baldwin if (bootverbose)
189983c41143SJohn Baldwin device_printf(sc->dev,
1900da1b038aSJustin Hibbits "failed to allocate initial %s window (%#jx-%#jx,%#jx)\n",
190183c41143SJohn Baldwin w->name, start, end, count);
190283c41143SJohn Baldwin return (error);
190383c41143SJohn Baldwin }
1904c825d4dcSJohn Baldwin if (bootverbose)
1905c825d4dcSJohn Baldwin device_printf(sc->dev,
1906c825d4dcSJohn Baldwin "allocated initial %s window of %#jx-%#jx\n",
1907c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
190883c41143SJohn Baldwin goto updatewin;
190983c41143SJohn Baldwin }
191083c41143SJohn Baldwin
191183c41143SJohn Baldwin /*
191283c41143SJohn Baldwin * See if growing the window would help. Compute the minimum
191383c41143SJohn Baldwin * amount of address space needed on both the front and back
191483c41143SJohn Baldwin * ends of the existing window to satisfy the allocation.
191583c41143SJohn Baldwin *
191683c41143SJohn Baldwin * For each end, build a candidate region adjusting for the
191783c41143SJohn Baldwin * required alignment, etc. If there is a free region at the
191883c41143SJohn Baldwin * edge of the window, grow from the inner edge of the free
191983c41143SJohn Baldwin * region. Otherwise grow from the window boundary.
192083c41143SJohn Baldwin *
1921c825d4dcSJohn Baldwin * Growing an I/O window below 64k for a bridge with the ISA
1922c825d4dcSJohn Baldwin * enable bit doesn't require any special magic as the step
1923c825d4dcSJohn Baldwin * size of an I/O window (1k) always includes multiple
1924c825d4dcSJohn Baldwin * non-alias ranges when it is grown in either direction.
1925c825d4dcSJohn Baldwin *
192683c41143SJohn Baldwin * XXX: Special case: if w->res is completely empty and the
192783c41143SJohn Baldwin * request size is larger than w->res, we should find the
192883c41143SJohn Baldwin * optimal aligned buffer containing w->res and allocate that.
192983c41143SJohn Baldwin */
193083c41143SJohn Baldwin if (bootverbose)
193183c41143SJohn Baldwin device_printf(sc->dev,
1932da1b038aSJustin Hibbits "attempting to grow %s window for (%#jx-%#jx,%#jx)\n",
193383c41143SJohn Baldwin w->name, start, end, count);
193489977ce2SJustin Hibbits align = (rman_res_t)1 << RF_ALIGNMENT(flags);
1935c825d4dcSJohn Baldwin if (start < w->base) {
193683c41143SJohn Baldwin if (rman_first_free_region(&w->rman, &start_free, &end_free) !=
1937c825d4dcSJohn Baldwin 0 || start_free != w->base)
1938c825d4dcSJohn Baldwin end_free = w->base;
193983c41143SJohn Baldwin if (end_free > end)
1940ddac8cc9SJohn Baldwin end_free = end + 1;
194183c41143SJohn Baldwin
194283c41143SJohn Baldwin /* Move end_free down until it is properly aligned. */
194383c41143SJohn Baldwin end_free &= ~(align - 1);
1944a49dcb46SJohn Baldwin end_free--;
1945a49dcb46SJohn Baldwin front = end_free - (count - 1);
194683c41143SJohn Baldwin
194783c41143SJohn Baldwin /*
194883c41143SJohn Baldwin * The resource would now be allocated at (front,
194983c41143SJohn Baldwin * end_free). Ensure that fits in the (start, end)
195083c41143SJohn Baldwin * bounds. end_free is checked above. If 'front' is
195183c41143SJohn Baldwin * ok, ensure it is properly aligned for this window.
195283c41143SJohn Baldwin * Also check for underflow.
195383c41143SJohn Baldwin */
195483c41143SJohn Baldwin if (front >= start && front <= end_free) {
195583c41143SJohn Baldwin if (bootverbose)
1956da1b038aSJustin Hibbits printf("\tfront candidate range: %#jx-%#jx\n",
195783c41143SJohn Baldwin front, end_free);
1958a7b5acacSJohn Baldwin front &= ~wmask;
1959c825d4dcSJohn Baldwin front = w->base - front;
196083c41143SJohn Baldwin } else
196183c41143SJohn Baldwin front = 0;
196283c41143SJohn Baldwin } else
196383c41143SJohn Baldwin front = 0;
1964c825d4dcSJohn Baldwin if (end > w->limit) {
196583c41143SJohn Baldwin if (rman_last_free_region(&w->rman, &start_free, &end_free) !=
1966c825d4dcSJohn Baldwin 0 || end_free != w->limit)
1967c825d4dcSJohn Baldwin start_free = w->limit + 1;
196883c41143SJohn Baldwin if (start_free < start)
196983c41143SJohn Baldwin start_free = start;
197083c41143SJohn Baldwin
197183c41143SJohn Baldwin /* Move start_free up until it is properly aligned. */
197283c41143SJohn Baldwin start_free = roundup2(start_free, align);
1973a49dcb46SJohn Baldwin back = start_free + count - 1;
197483c41143SJohn Baldwin
197583c41143SJohn Baldwin /*
197683c41143SJohn Baldwin * The resource would now be allocated at (start_free,
197783c41143SJohn Baldwin * back). Ensure that fits in the (start, end)
197883c41143SJohn Baldwin * bounds. start_free is checked above. If 'back' is
197983c41143SJohn Baldwin * ok, ensure it is properly aligned for this window.
198083c41143SJohn Baldwin * Also check for overflow.
198183c41143SJohn Baldwin */
198283c41143SJohn Baldwin if (back <= end && start_free <= back) {
198383c41143SJohn Baldwin if (bootverbose)
1984da1b038aSJustin Hibbits printf("\tback candidate range: %#jx-%#jx\n",
198583c41143SJohn Baldwin start_free, back);
1986a7b5acacSJohn Baldwin back |= wmask;
1987c825d4dcSJohn Baldwin back -= w->limit;
198883c41143SJohn Baldwin } else
198983c41143SJohn Baldwin back = 0;
199083c41143SJohn Baldwin } else
199183c41143SJohn Baldwin back = 0;
199283c41143SJohn Baldwin
199383c41143SJohn Baldwin /*
199483c41143SJohn Baldwin * Try to allocate the smallest needed region first.
199583c41143SJohn Baldwin * If that fails, fall back to the other region.
199683c41143SJohn Baldwin */
199783c41143SJohn Baldwin error = ENOSPC;
199883c41143SJohn Baldwin while (front != 0 || back != 0) {
199983c41143SJohn Baldwin if (front != 0 && (front <= back || back == 0)) {
2000c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base - front,
2001c825d4dcSJohn Baldwin w->limit);
200283c41143SJohn Baldwin if (error == 0)
200383c41143SJohn Baldwin break;
200483c41143SJohn Baldwin front = 0;
200583c41143SJohn Baldwin } else {
2006c825d4dcSJohn Baldwin error = pcib_expand_window(sc, w, type, w->base,
2007c825d4dcSJohn Baldwin w->limit + back);
200883c41143SJohn Baldwin if (error == 0)
200983c41143SJohn Baldwin break;
201083c41143SJohn Baldwin back = 0;
201183c41143SJohn Baldwin }
201283c41143SJohn Baldwin }
201383c41143SJohn Baldwin
201483c41143SJohn Baldwin if (error)
201583c41143SJohn Baldwin return (error);
201683c41143SJohn Baldwin if (bootverbose)
2017c825d4dcSJohn Baldwin device_printf(sc->dev, "grew %s window to %#jx-%#jx\n",
2018c825d4dcSJohn Baldwin w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
201983c41143SJohn Baldwin
202083c41143SJohn Baldwin updatewin:
2021c825d4dcSJohn Baldwin /* Write the new window. */
2022a7b5acacSJohn Baldwin KASSERT((w->base & wmask) == 0, ("start address is not aligned"));
2023a7b5acacSJohn Baldwin KASSERT((w->limit & wmask) == wmask, ("end address is not aligned"));
202483c41143SJohn Baldwin pcib_write_windows(sc, w->mask);
202583c41143SJohn Baldwin return (0);
202683c41143SJohn Baldwin }
202783c41143SJohn Baldwin
202883c41143SJohn Baldwin /*
202983c41143SJohn Baldwin * We have to trap resource allocation requests and ensure that the bridge
203083c41143SJohn Baldwin * is set up to, or capable of handling them.
203183c41143SJohn Baldwin */
203203719c65SJohn Baldwin static struct resource *
pcib_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)203383c41143SJohn Baldwin pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
20342dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
203583c41143SJohn Baldwin {
203683c41143SJohn Baldwin struct pcib_softc *sc;
203783c41143SJohn Baldwin struct resource *r;
203883c41143SJohn Baldwin
203983c41143SJohn Baldwin sc = device_get_softc(dev);
204083c41143SJohn Baldwin
204183c41143SJohn Baldwin /*
204283c41143SJohn Baldwin * VGA resources are decoded iff the VGA enable bit is set in
204383c41143SJohn Baldwin * the bridge control register. VGA resources do not fall into
204483c41143SJohn Baldwin * the resource windows and are passed up to the parent.
204583c41143SJohn Baldwin */
204683c41143SJohn Baldwin if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) ||
204783c41143SJohn Baldwin (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) {
204883c41143SJohn Baldwin if (sc->bridgectl & PCIB_BCR_VGA_ENABLE)
204983c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type,
205083c41143SJohn Baldwin rid, start, end, count, flags));
205183c41143SJohn Baldwin else
205283c41143SJohn Baldwin return (NULL);
205383c41143SJohn Baldwin }
205483c41143SJohn Baldwin
205583c41143SJohn Baldwin switch (type) {
20564edef187SJohn Baldwin case PCI_RES_BUS:
20574edef187SJohn Baldwin return (pcib_alloc_subbus(&sc->bus, child, rid, start, end,
20584edef187SJohn Baldwin count, flags));
205983c41143SJohn Baldwin case SYS_RES_IOPORT:
2060c825d4dcSJohn Baldwin if (pcib_is_isa_range(sc, start, end, count))
2061c825d4dcSJohn Baldwin return (NULL);
206283c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start,
206383c41143SJohn Baldwin end, count, flags);
2064a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
206583c41143SJohn Baldwin break;
206683c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->io, type, start, end, count,
206783c41143SJohn Baldwin flags) == 0)
206883c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->io, child, type,
206983c41143SJohn Baldwin rid, start, end, count, flags);
207083c41143SJohn Baldwin break;
207183c41143SJohn Baldwin case SYS_RES_MEMORY:
207283c41143SJohn Baldwin /*
207383c41143SJohn Baldwin * For prefetchable resources, prefer the prefetchable
207483c41143SJohn Baldwin * memory window, but fall back to the regular memory
207583c41143SJohn Baldwin * window if that fails. Try both windows before
207683c41143SJohn Baldwin * attempting to grow a window in case the firmware
207783c41143SJohn Baldwin * has used a range in the regular memory window to
207883c41143SJohn Baldwin * map a prefetchable BAR.
207983c41143SJohn Baldwin */
208083c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) {
208183c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child, type,
208283c41143SJohn Baldwin rid, start, end, count, flags);
208383c41143SJohn Baldwin if (r != NULL)
208483c41143SJohn Baldwin break;
208583c41143SJohn Baldwin }
208683c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid,
208783c41143SJohn Baldwin start, end, count, flags);
2088a6c82265SMarius Strobl if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
208983c41143SJohn Baldwin break;
209083c41143SJohn Baldwin if (flags & RF_PREFETCHABLE) {
209183c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->pmem, type, start, end,
209283c41143SJohn Baldwin count, flags) == 0) {
209383c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->pmem, child,
209483c41143SJohn Baldwin type, rid, start, end, count, flags);
209583c41143SJohn Baldwin if (r != NULL)
209683c41143SJohn Baldwin break;
209783c41143SJohn Baldwin }
209883c41143SJohn Baldwin }
209983c41143SJohn Baldwin if (pcib_grow_window(sc, &sc->mem, type, start, end, count,
210083c41143SJohn Baldwin flags & ~RF_PREFETCHABLE) == 0)
210183c41143SJohn Baldwin r = pcib_suballoc_resource(sc, &sc->mem, child, type,
210283c41143SJohn Baldwin rid, start, end, count, flags);
210383c41143SJohn Baldwin break;
210483c41143SJohn Baldwin default:
210583c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid,
210683c41143SJohn Baldwin start, end, count, flags));
210783c41143SJohn Baldwin }
210883c41143SJohn Baldwin
210983c41143SJohn Baldwin /*
211083c41143SJohn Baldwin * If attempts to suballocate from the window fail but this is a
211183c41143SJohn Baldwin * subtractive bridge, pass the request up the tree.
211283c41143SJohn Baldwin */
211383c41143SJohn Baldwin if (sc->flags & PCIB_SUBTRACTIVE && r == NULL)
211483c41143SJohn Baldwin return (bus_generic_alloc_resource(dev, child, type, rid,
211583c41143SJohn Baldwin start, end, count, flags));
211683c41143SJohn Baldwin return (r);
211783c41143SJohn Baldwin }
211883c41143SJohn Baldwin
211903719c65SJohn Baldwin static int
pcib_adjust_resource(device_t bus,device_t child,struct resource * r,rman_res_t start,rman_res_t end)2120fef01f04SJohn Baldwin pcib_adjust_resource(device_t bus, device_t child, struct resource *r,
21212dd1bdf1SJustin Hibbits rman_res_t start, rman_res_t end)
212283c41143SJohn Baldwin {
212383c41143SJohn Baldwin struct pcib_softc *sc;
212426245980SJessica Clarke struct pcib_window *w;
212515cb3b54SAlexander Motin rman_res_t wmask;
2126fef01f04SJohn Baldwin int error, type;
212783c41143SJohn Baldwin
212883c41143SJohn Baldwin sc = device_get_softc(bus);
2129fef01f04SJohn Baldwin type = rman_get_type(r);
213026245980SJessica Clarke
213126245980SJessica Clarke /*
213226245980SJessica Clarke * If the resource wasn't sub-allocated from one of our region
213326245980SJessica Clarke * managers then just pass the request up.
213426245980SJessica Clarke */
2135582b84cdSJohn Baldwin if (!pcib_is_resource_managed(sc, r))
2136fef01f04SJohn Baldwin return (bus_generic_adjust_resource(bus, child, r, start, end));
213726245980SJessica Clarke
213831776afdSJessica Clarke if (type == PCI_RES_BUS) {
213931776afdSJessica Clarke /*
214031776afdSJessica Clarke * If our bus range isn't big enough to grow the sub-allocation
214131776afdSJessica Clarke * then we need to grow our bus range. Any request that would
214231776afdSJessica Clarke * require us to decrease the start of our own bus range is
214331776afdSJessica Clarke * invalid, we can only extend the end; ignore such requests
214431776afdSJessica Clarke * and let rman_adjust_resource fail below.
214531776afdSJessica Clarke */
214631776afdSJessica Clarke if (start >= sc->bus.sec && end > sc->bus.sub) {
214731776afdSJessica Clarke error = pcib_grow_subbus(&sc->bus, end);
214831776afdSJessica Clarke if (error != 0)
214931776afdSJessica Clarke return (error);
215031776afdSJessica Clarke }
2151e1d442a6SJessica Clarke } else {
215226245980SJessica Clarke /*
215326245980SJessica Clarke * Resource is managed and not a secondary bus number, must
215426245980SJessica Clarke * be from one of our windows.
215526245980SJessica Clarke */
2156582b84cdSJohn Baldwin w = pcib_get_resource_window(sc, r);
215726245980SJessica Clarke KASSERT(w != NULL,
215826245980SJessica Clarke ("%s: no window for resource (%#jx-%#jx) type %d",
215926245980SJessica Clarke __func__, rman_get_start(r), rman_get_end(r), type));
216026245980SJessica Clarke
216126245980SJessica Clarke /*
216226245980SJessica Clarke * If our window isn't big enough to grow the sub-allocation
216326245980SJessica Clarke * then we need to expand the window.
216426245980SJessica Clarke */
216526245980SJessica Clarke if (start < w->base || end > w->limit) {
216615cb3b54SAlexander Motin wmask = ((rman_res_t)1 << w->step) - 1;
216715cb3b54SAlexander Motin error = pcib_expand_window(sc, w, type,
216815cb3b54SAlexander Motin MIN(start & ~wmask, w->base),
216915cb3b54SAlexander Motin MAX(end | wmask, w->limit));
217026245980SJessica Clarke if (error != 0)
217126245980SJessica Clarke return (error);
217215cb3b54SAlexander Motin if (bootverbose)
217315cb3b54SAlexander Motin device_printf(sc->dev,
217415cb3b54SAlexander Motin "grew %s window to %#jx-%#jx\n",
217515cb3b54SAlexander Motin w->name, (uintmax_t)w->base,
217615cb3b54SAlexander Motin (uintmax_t)w->limit);
217715cb3b54SAlexander Motin pcib_write_windows(sc, w->mask);
217826245980SJessica Clarke }
217926245980SJessica Clarke }
218026245980SJessica Clarke
218183c41143SJohn Baldwin return (rman_adjust_resource(r, start, end));
218283c41143SJohn Baldwin }
218383c41143SJohn Baldwin
218403719c65SJohn Baldwin static int
pcib_release_resource(device_t dev,device_t child,struct resource * r)21859dbf5b0eSJohn Baldwin pcib_release_resource(device_t dev, device_t child, struct resource *r)
218683c41143SJohn Baldwin {
218783c41143SJohn Baldwin struct pcib_softc *sc;
218883c41143SJohn Baldwin int error;
218983c41143SJohn Baldwin
219083c41143SJohn Baldwin sc = device_get_softc(dev);
2191582b84cdSJohn Baldwin if (pcib_is_resource_managed(sc, r)) {
219283c41143SJohn Baldwin if (rman_get_flags(r) & RF_ACTIVE) {
21939dbf5b0eSJohn Baldwin error = bus_deactivate_resource(child, r);
219483c41143SJohn Baldwin if (error)
219583c41143SJohn Baldwin return (error);
219683c41143SJohn Baldwin }
219783c41143SJohn Baldwin return (rman_release_resource(r));
219883c41143SJohn Baldwin }
21999dbf5b0eSJohn Baldwin return (bus_generic_release_resource(dev, child, r));
220083c41143SJohn Baldwin }
2201b377ff81SJohn Baldwin
2202b377ff81SJohn Baldwin static int
pcib_activate_resource(device_t dev,device_t child,struct resource * r)22032baed46eSJohn Baldwin pcib_activate_resource(device_t dev, device_t child, struct resource *r)
2204b377ff81SJohn Baldwin {
2205b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev);
2206b377ff81SJohn Baldwin struct resource_map map;
22072baed46eSJohn Baldwin int error, type;
2208b377ff81SJohn Baldwin
2209582b84cdSJohn Baldwin if (!pcib_is_resource_managed(sc, r))
22102baed46eSJohn Baldwin return (bus_generic_activate_resource(dev, child, r));
2211b377ff81SJohn Baldwin
2212b377ff81SJohn Baldwin error = rman_activate_resource(r);
2213b377ff81SJohn Baldwin if (error != 0)
2214b377ff81SJohn Baldwin return (error);
2215b377ff81SJohn Baldwin
22162baed46eSJohn Baldwin type = rman_get_type(r);
2217b377ff81SJohn Baldwin if ((rman_get_flags(r) & RF_UNMAPPED) == 0 &&
2218b377ff81SJohn Baldwin (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) {
2219d77f2092SJohn Baldwin error = BUS_MAP_RESOURCE(dev, child, r, NULL, &map);
2220b377ff81SJohn Baldwin if (error != 0) {
2221b377ff81SJohn Baldwin rman_deactivate_resource(r);
2222b377ff81SJohn Baldwin return (error);
2223b377ff81SJohn Baldwin }
2224b377ff81SJohn Baldwin
2225b377ff81SJohn Baldwin rman_set_mapping(r, &map);
2226b377ff81SJohn Baldwin }
2227b377ff81SJohn Baldwin return (0);
2228b377ff81SJohn Baldwin }
2229b377ff81SJohn Baldwin
2230b377ff81SJohn Baldwin static int
pcib_deactivate_resource(device_t dev,device_t child,struct resource * r)22312baed46eSJohn Baldwin pcib_deactivate_resource(device_t dev, device_t child, struct resource *r)
2232b377ff81SJohn Baldwin {
2233b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev);
2234b377ff81SJohn Baldwin struct resource_map map;
22352baed46eSJohn Baldwin int error, type;
2236b377ff81SJohn Baldwin
2237582b84cdSJohn Baldwin if (!pcib_is_resource_managed(sc, r))
22382baed46eSJohn Baldwin return (bus_generic_deactivate_resource(dev, child, r));
2239b377ff81SJohn Baldwin
2240b377ff81SJohn Baldwin error = rman_deactivate_resource(r);
2241b377ff81SJohn Baldwin if (error != 0)
2242b377ff81SJohn Baldwin return (error);
2243b377ff81SJohn Baldwin
22442baed46eSJohn Baldwin type = rman_get_type(r);
2245b377ff81SJohn Baldwin if ((rman_get_flags(r) & RF_UNMAPPED) == 0 &&
2246b377ff81SJohn Baldwin (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) {
2247b377ff81SJohn Baldwin rman_get_mapping(r, &map);
2248d77f2092SJohn Baldwin BUS_UNMAP_RESOURCE(dev, child, r, &map);
2249b377ff81SJohn Baldwin }
2250b377ff81SJohn Baldwin return (0);
2251b377ff81SJohn Baldwin }
2252b377ff81SJohn Baldwin
2253b377ff81SJohn Baldwin static struct resource *
pcib_find_parent_resource(struct pcib_window * w,struct resource * r)2254b377ff81SJohn Baldwin pcib_find_parent_resource(struct pcib_window *w, struct resource *r)
2255b377ff81SJohn Baldwin {
2256b377ff81SJohn Baldwin for (int i = 0; i < w->count; i++) {
2257b377ff81SJohn Baldwin if (rman_get_start(w->res[i]) <= rman_get_start(r) &&
2258b377ff81SJohn Baldwin rman_get_end(w->res[i]) >= rman_get_end(r))
2259b377ff81SJohn Baldwin return (w->res[i]);
2260b377ff81SJohn Baldwin }
2261b377ff81SJohn Baldwin return (NULL);
2262b377ff81SJohn Baldwin }
2263b377ff81SJohn Baldwin
2264b377ff81SJohn Baldwin static int
pcib_map_resource(device_t dev,device_t child,struct resource * r,struct resource_map_request * argsp,struct resource_map * map)2265d77f2092SJohn Baldwin pcib_map_resource(device_t dev, device_t child, struct resource *r,
2266b377ff81SJohn Baldwin struct resource_map_request *argsp, struct resource_map *map)
2267b377ff81SJohn Baldwin {
2268b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev);
2269b377ff81SJohn Baldwin struct resource_map_request args;
2270b377ff81SJohn Baldwin struct pcib_window *w;
2271b377ff81SJohn Baldwin struct resource *pres;
2272b377ff81SJohn Baldwin rman_res_t length, start;
2273b377ff81SJohn Baldwin int error;
2274b377ff81SJohn Baldwin
2275582b84cdSJohn Baldwin w = pcib_get_resource_window(sc, r);
2276b377ff81SJohn Baldwin if (w == NULL)
2277d77f2092SJohn Baldwin return (bus_generic_map_resource(dev, child, r, argsp, map));
2278b377ff81SJohn Baldwin
2279b377ff81SJohn Baldwin /* Resources must be active to be mapped. */
2280b377ff81SJohn Baldwin if (!(rman_get_flags(r) & RF_ACTIVE))
2281b377ff81SJohn Baldwin return (ENXIO);
2282b377ff81SJohn Baldwin
2283b377ff81SJohn Baldwin resource_init_map_request(&args);
2284b377ff81SJohn Baldwin error = resource_validate_map_request(r, argsp, &args, &start, &length);
2285b377ff81SJohn Baldwin if (error)
2286b377ff81SJohn Baldwin return (error);
2287b377ff81SJohn Baldwin
2288b377ff81SJohn Baldwin pres = pcib_find_parent_resource(w, r);
2289b377ff81SJohn Baldwin if (pres == NULL)
2290b377ff81SJohn Baldwin return (ENOENT);
2291b377ff81SJohn Baldwin
2292b377ff81SJohn Baldwin args.offset = start - rman_get_start(pres);
2293b377ff81SJohn Baldwin args.length = length;
229498056127SJohn Baldwin return (bus_map_resource(dev, pres, &args, map));
2295b377ff81SJohn Baldwin }
2296b377ff81SJohn Baldwin
2297b377ff81SJohn Baldwin static int
pcib_unmap_resource(device_t dev,device_t child,struct resource * r,struct resource_map * map)2298d77f2092SJohn Baldwin pcib_unmap_resource(device_t dev, device_t child, struct resource *r,
2299b377ff81SJohn Baldwin struct resource_map *map)
2300b377ff81SJohn Baldwin {
2301b377ff81SJohn Baldwin struct pcib_softc *sc = device_get_softc(dev);
2302b377ff81SJohn Baldwin struct pcib_window *w;
230398056127SJohn Baldwin struct resource *pres;
2304b377ff81SJohn Baldwin
2305582b84cdSJohn Baldwin w = pcib_get_resource_window(sc, r);
230698056127SJohn Baldwin if (w == NULL)
2307d77f2092SJohn Baldwin return (bus_generic_unmap_resource(dev, child, r, map));
230898056127SJohn Baldwin
230998056127SJohn Baldwin pres = pcib_find_parent_resource(w, r);
231098056127SJohn Baldwin if (pres == NULL)
231198056127SJohn Baldwin return (ENOENT);
231298056127SJohn Baldwin return (bus_unmap_resource(dev, pres, map));
2313b377ff81SJohn Baldwin }
2314bb0d0a8eSMike Smith
2315bb0d0a8eSMike Smith /*
231655d3ea17SRyan Stone * If ARI is enabled on this downstream port, translate the function number
231755d3ea17SRyan Stone * to the non-ARI slot/function. The downstream port will convert it back in
231855d3ea17SRyan Stone * hardware. If ARI is not enabled slot and func are not modified.
231955d3ea17SRyan Stone */
232055d3ea17SRyan Stone static __inline void
pcib_xlate_ari(device_t pcib,int bus,int * slot,int * func)232155d3ea17SRyan Stone pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func)
232255d3ea17SRyan Stone {
232355d3ea17SRyan Stone struct pcib_softc *sc;
232455d3ea17SRyan Stone int ari_func;
232555d3ea17SRyan Stone
232655d3ea17SRyan Stone sc = device_get_softc(pcib);
232755d3ea17SRyan Stone ari_func = *func;
232855d3ea17SRyan Stone
232955d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) {
233055d3ea17SRyan Stone KASSERT(*slot == 0,
233155d3ea17SRyan Stone ("Non-zero slot number with ARI enabled!"));
233255d3ea17SRyan Stone *slot = PCIE_ARI_SLOT(ari_func);
233355d3ea17SRyan Stone *func = PCIE_ARI_FUNC(ari_func);
233455d3ea17SRyan Stone }
233555d3ea17SRyan Stone }
233655d3ea17SRyan Stone
233755d3ea17SRyan Stone static void
pcib_enable_ari(struct pcib_softc * sc,uint32_t pcie_pos)233855d3ea17SRyan Stone pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos)
233955d3ea17SRyan Stone {
234055d3ea17SRyan Stone uint32_t ctl2;
234155d3ea17SRyan Stone
234255d3ea17SRyan Stone ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4);
234355d3ea17SRyan Stone ctl2 |= PCIEM_CTL2_ARI;
234455d3ea17SRyan Stone pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4);
234555d3ea17SRyan Stone
234655d3ea17SRyan Stone sc->flags |= PCIB_ENABLE_ARI;
234755d3ea17SRyan Stone }
234855d3ea17SRyan Stone
234955d3ea17SRyan Stone /*
2350bb0d0a8eSMike Smith * PCIB interface.
2351bb0d0a8eSMike Smith */
23526f0d5884SJohn Baldwin int
pcib_maxslots(device_t dev)2353bb0d0a8eSMike Smith pcib_maxslots(device_t dev)
2354bb0d0a8eSMike Smith {
23555502348dSJustin Hibbits #if !defined(__amd64__) && !defined(__i386__)
23568b92ad43SJustin Hibbits uint32_t pcie_pos;
23578b92ad43SJustin Hibbits uint16_t val;
23588b92ad43SJustin Hibbits
23598b92ad43SJustin Hibbits /*
23608b92ad43SJustin Hibbits * If this is a PCIe rootport or downstream switch port, there's only
23618b92ad43SJustin Hibbits * one slot permitted.
23628b92ad43SJustin Hibbits */
23638b92ad43SJustin Hibbits if (pci_find_cap(dev, PCIY_EXPRESS, &pcie_pos) == 0) {
23648b92ad43SJustin Hibbits val = pci_read_config(dev, pcie_pos + PCIER_FLAGS, 2);
23658b92ad43SJustin Hibbits val &= PCIEM_FLAGS_TYPE;
23668b92ad43SJustin Hibbits if (val == PCIEM_TYPE_ROOT_PORT ||
23678b92ad43SJustin Hibbits val == PCIEM_TYPE_DOWNSTREAM_PORT)
23688b92ad43SJustin Hibbits return (0);
23698b92ad43SJustin Hibbits }
23705502348dSJustin Hibbits #endif
23714fa59183SMike Smith return (PCI_SLOTMAX);
2372bb0d0a8eSMike Smith }
2373bb0d0a8eSMike Smith
237455d3ea17SRyan Stone static int
pcib_ari_maxslots(device_t dev)237555d3ea17SRyan Stone pcib_ari_maxslots(device_t dev)
237655d3ea17SRyan Stone {
237755d3ea17SRyan Stone struct pcib_softc *sc;
237855d3ea17SRyan Stone
237955d3ea17SRyan Stone sc = device_get_softc(dev);
238055d3ea17SRyan Stone
238155d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI)
238255d3ea17SRyan Stone return (PCIE_ARI_SLOTMAX);
238355d3ea17SRyan Stone else
23848b92ad43SJustin Hibbits return (pcib_maxslots(dev));
238555d3ea17SRyan Stone }
238655d3ea17SRyan Stone
238755d3ea17SRyan Stone static int
pcib_ari_maxfuncs(device_t dev)238855d3ea17SRyan Stone pcib_ari_maxfuncs(device_t dev)
238955d3ea17SRyan Stone {
239055d3ea17SRyan Stone struct pcib_softc *sc;
239155d3ea17SRyan Stone
239255d3ea17SRyan Stone sc = device_get_softc(dev);
239355d3ea17SRyan Stone
239455d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI)
239555d3ea17SRyan Stone return (PCIE_ARI_FUNCMAX);
239655d3ea17SRyan Stone else
239755d3ea17SRyan Stone return (PCI_FUNCMAX);
239855d3ea17SRyan Stone }
239955d3ea17SRyan Stone
24002397d2d8SRyan Stone static void
pcib_ari_decode_rid(device_t pcib,uint16_t rid,int * bus,int * slot,int * func)24012397d2d8SRyan Stone pcib_ari_decode_rid(device_t pcib, uint16_t rid, int *bus, int *slot,
24022397d2d8SRyan Stone int *func)
24032397d2d8SRyan Stone {
24042397d2d8SRyan Stone struct pcib_softc *sc;
24052397d2d8SRyan Stone
24062397d2d8SRyan Stone sc = device_get_softc(pcib);
24072397d2d8SRyan Stone
24082397d2d8SRyan Stone *bus = PCI_RID2BUS(rid);
24092397d2d8SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) {
24102397d2d8SRyan Stone *slot = PCIE_ARI_RID2SLOT(rid);
24112397d2d8SRyan Stone *func = PCIE_ARI_RID2FUNC(rid);
24122397d2d8SRyan Stone } else {
24132397d2d8SRyan Stone *slot = PCI_RID2SLOT(rid);
24142397d2d8SRyan Stone *func = PCI_RID2FUNC(rid);
24152397d2d8SRyan Stone }
24162397d2d8SRyan Stone }
24172397d2d8SRyan Stone
2418bb0d0a8eSMike Smith /*
2419bb0d0a8eSMike Smith * Since we are a child of a PCI bus, its parent must support the pcib interface.
2420bb0d0a8eSMike Smith */
242155d3ea17SRyan Stone static uint32_t
pcib_read_config(device_t dev,u_int b,u_int s,u_int f,u_int reg,int width)2422795dceffSWarner Losh pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width)
2423bb0d0a8eSMike Smith {
242482cb5c3bSJohn Baldwin #ifdef PCI_HP
242582cb5c3bSJohn Baldwin struct pcib_softc *sc;
242655d3ea17SRyan Stone
242782cb5c3bSJohn Baldwin sc = device_get_softc(dev);
242882cb5c3bSJohn Baldwin if (!pcib_present(sc)) {
242982cb5c3bSJohn Baldwin switch (width) {
243082cb5c3bSJohn Baldwin case 2:
243182cb5c3bSJohn Baldwin return (0xffff);
243282cb5c3bSJohn Baldwin case 1:
243382cb5c3bSJohn Baldwin return (0xff);
243482cb5c3bSJohn Baldwin default:
243582cb5c3bSJohn Baldwin return (0xffffffff);
243682cb5c3bSJohn Baldwin }
243782cb5c3bSJohn Baldwin }
243882cb5c3bSJohn Baldwin #endif
243955d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f);
244055d3ea17SRyan Stone return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s,
244155d3ea17SRyan Stone f, reg, width));
2442bb0d0a8eSMike Smith }
2443bb0d0a8eSMike Smith
244455d3ea17SRyan Stone static void
pcib_write_config(device_t dev,u_int b,u_int s,u_int f,u_int reg,uint32_t val,int width)2445795dceffSWarner Losh pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width)
2446bb0d0a8eSMike Smith {
244782cb5c3bSJohn Baldwin #ifdef PCI_HP
244882cb5c3bSJohn Baldwin struct pcib_softc *sc;
244955d3ea17SRyan Stone
245082cb5c3bSJohn Baldwin sc = device_get_softc(dev);
245182cb5c3bSJohn Baldwin if (!pcib_present(sc))
245282cb5c3bSJohn Baldwin return;
245382cb5c3bSJohn Baldwin #endif
245455d3ea17SRyan Stone pcib_xlate_ari(dev, b, &s, &f);
245555d3ea17SRyan Stone PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f,
245655d3ea17SRyan Stone reg, val, width);
2457bb0d0a8eSMike Smith }
2458bb0d0a8eSMike Smith
2459bb0d0a8eSMike Smith /*
2460bb0d0a8eSMike Smith * Route an interrupt across a PCI bridge.
2461bb0d0a8eSMike Smith */
24622c2d1d07SBenno Rice int
pcib_route_interrupt(device_t pcib,device_t dev,int pin)2463bb0d0a8eSMike Smith pcib_route_interrupt(device_t pcib, device_t dev, int pin)
2464bb0d0a8eSMike Smith {
2465bb0d0a8eSMike Smith device_t bus;
2466bb0d0a8eSMike Smith int parent_intpin;
2467bb0d0a8eSMike Smith int intnum;
2468bb0d0a8eSMike Smith
2469bb0d0a8eSMike Smith /*
2470bb0d0a8eSMike Smith *
2471bb0d0a8eSMike Smith * The PCI standard defines a swizzle of the child-side device/intpin to
2472bb0d0a8eSMike Smith * the parent-side intpin as follows.
2473bb0d0a8eSMike Smith *
2474bb0d0a8eSMike Smith * device = device on child bus
2475bb0d0a8eSMike Smith * child_intpin = intpin on child bus slot (0-3)
2476bb0d0a8eSMike Smith * parent_intpin = intpin on parent bus slot (0-3)
2477bb0d0a8eSMike Smith *
2478bb0d0a8eSMike Smith * parent_intpin = (device + child_intpin) % 4
2479bb0d0a8eSMike Smith */
2480cdc95e1bSBernd Walter parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
2481bb0d0a8eSMike Smith
2482bb0d0a8eSMike Smith /*
2483bb0d0a8eSMike Smith * Our parent is a PCI bus. Its parent must export the pcib interface
2484bb0d0a8eSMike Smith * which includes the ability to route interrupts.
2485bb0d0a8eSMike Smith */
2486bb0d0a8eSMike Smith bus = device_get_parent(pcib);
2487bb0d0a8eSMike Smith intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
248839981fedSJohn Baldwin if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
2489c6a121abSJohn Baldwin device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
2490c6a121abSJohn Baldwin pci_get_slot(dev), 'A' + pin - 1, intnum);
24918046c4b9SMike Smith }
2492bb0d0a8eSMike Smith return(intnum);
2493bb0d0a8eSMike Smith }
2494b173edafSJohn Baldwin
2495e706f7f0SJohn Baldwin /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
24969bf4c9c1SJohn Baldwin int
pcib_alloc_msi(device_t pcib,device_t dev,int count,int maxcount,int * irqs)24979bf4c9c1SJohn Baldwin pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
24989bf4c9c1SJohn Baldwin {
2499bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib);
25009bf4c9c1SJohn Baldwin device_t bus;
25019bf4c9c1SJohn Baldwin
250222bf1c7fSJohn Baldwin if (sc->flags & PCIB_DISABLE_MSI)
250322bf1c7fSJohn Baldwin return (ENXIO);
25049bf4c9c1SJohn Baldwin bus = device_get_parent(pcib);
25059bf4c9c1SJohn Baldwin return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
25069bf4c9c1SJohn Baldwin irqs));
25079bf4c9c1SJohn Baldwin }
25089bf4c9c1SJohn Baldwin
2509e706f7f0SJohn Baldwin /* Pass request to release MSI/MSI-X messages up to the parent bridge. */
25109bf4c9c1SJohn Baldwin int
pcib_release_msi(device_t pcib,device_t dev,int count,int * irqs)25119bf4c9c1SJohn Baldwin pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
25129bf4c9c1SJohn Baldwin {
25139bf4c9c1SJohn Baldwin device_t bus;
25149bf4c9c1SJohn Baldwin
25159bf4c9c1SJohn Baldwin bus = device_get_parent(pcib);
25169bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
25179bf4c9c1SJohn Baldwin }
25189bf4c9c1SJohn Baldwin
25199bf4c9c1SJohn Baldwin /* Pass request to alloc an MSI-X message up to the parent bridge. */
25209bf4c9c1SJohn Baldwin int
pcib_alloc_msix(device_t pcib,device_t dev,int * irq)2521e706f7f0SJohn Baldwin pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
25229bf4c9c1SJohn Baldwin {
2523bd82bbb1SAndrew Gallatin struct pcib_softc *sc = device_get_softc(pcib);
25249bf4c9c1SJohn Baldwin device_t bus;
25259bf4c9c1SJohn Baldwin
252668e9cbd3SMarius Strobl if (sc->flags & PCIB_DISABLE_MSIX)
252722bf1c7fSJohn Baldwin return (ENXIO);
25289bf4c9c1SJohn Baldwin bus = device_get_parent(pcib);
2529e706f7f0SJohn Baldwin return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
25305fe82bcaSJohn Baldwin }
25315fe82bcaSJohn Baldwin
25329bf4c9c1SJohn Baldwin /* Pass request to release an MSI-X message up to the parent bridge. */
25339bf4c9c1SJohn Baldwin int
pcib_release_msix(device_t pcib,device_t dev,int irq)25349bf4c9c1SJohn Baldwin pcib_release_msix(device_t pcib, device_t dev, int irq)
25359bf4c9c1SJohn Baldwin {
25369bf4c9c1SJohn Baldwin device_t bus;
25379bf4c9c1SJohn Baldwin
25389bf4c9c1SJohn Baldwin bus = device_get_parent(pcib);
25399bf4c9c1SJohn Baldwin return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
25409bf4c9c1SJohn Baldwin }
25419bf4c9c1SJohn Baldwin
2542e706f7f0SJohn Baldwin /* Pass request to map MSI/MSI-X message up to parent bridge. */
2543e706f7f0SJohn Baldwin int
pcib_map_msi(device_t pcib,device_t dev,int irq,uint64_t * addr,uint32_t * data)2544e706f7f0SJohn Baldwin pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
2545e706f7f0SJohn Baldwin uint32_t *data)
2546e706f7f0SJohn Baldwin {
2547e706f7f0SJohn Baldwin device_t bus;
25484522ac77SLuoqi Chen int error;
2549e706f7f0SJohn Baldwin
2550e706f7f0SJohn Baldwin bus = device_get_parent(pcib);
25514522ac77SLuoqi Chen error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
25524522ac77SLuoqi Chen if (error)
25534522ac77SLuoqi Chen return (error);
25544522ac77SLuoqi Chen
25554522ac77SLuoqi Chen pci_ht_map_msi(pcib, *addr);
25564522ac77SLuoqi Chen return (0);
2557e706f7f0SJohn Baldwin }
2558e706f7f0SJohn Baldwin
255962508c53SJohn Baldwin /* Pass request for device power state up to parent bridge. */
256062508c53SJohn Baldwin int
pcib_power_for_sleep(device_t pcib,device_t dev,int * pstate)256162508c53SJohn Baldwin pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate)
256262508c53SJohn Baldwin {
256362508c53SJohn Baldwin device_t bus;
256462508c53SJohn Baldwin
256562508c53SJohn Baldwin bus = device_get_parent(pcib);
256662508c53SJohn Baldwin return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate));
256762508c53SJohn Baldwin }
25685605a99eSRyan Stone
25692397d2d8SRyan Stone static int
pcib_ari_enabled(device_t pcib)25702397d2d8SRyan Stone pcib_ari_enabled(device_t pcib)
25712397d2d8SRyan Stone {
25722397d2d8SRyan Stone struct pcib_softc *sc;
25732397d2d8SRyan Stone
25742397d2d8SRyan Stone sc = device_get_softc(pcib);
25752397d2d8SRyan Stone
25762397d2d8SRyan Stone return ((sc->flags & PCIB_ENABLE_ARI) != 0);
25772397d2d8SRyan Stone }
25782397d2d8SRyan Stone
2579d7be980dSAndrew Turner static int
pcib_ari_get_id(device_t pcib,device_t dev,enum pci_id_type type,uintptr_t * id)2580d7be980dSAndrew Turner pcib_ari_get_id(device_t pcib, device_t dev, enum pci_id_type type,
2581d7be980dSAndrew Turner uintptr_t *id)
258255d3ea17SRyan Stone {
258355d3ea17SRyan Stone struct pcib_softc *sc;
25841e43b18cSAndrew Turner device_t bus_dev;
258555d3ea17SRyan Stone uint8_t bus, slot, func;
258655d3ea17SRyan Stone
25871e43b18cSAndrew Turner if (type != PCI_ID_RID) {
25881e43b18cSAndrew Turner bus_dev = device_get_parent(pcib);
25891e43b18cSAndrew Turner return (PCIB_GET_ID(device_get_parent(bus_dev), dev, type, id));
25901e43b18cSAndrew Turner }
2591d7be980dSAndrew Turner
259255d3ea17SRyan Stone sc = device_get_softc(pcib);
259355d3ea17SRyan Stone
259455d3ea17SRyan Stone if (sc->flags & PCIB_ENABLE_ARI) {
259555d3ea17SRyan Stone bus = pci_get_bus(dev);
259655d3ea17SRyan Stone func = pci_get_function(dev);
259755d3ea17SRyan Stone
2598d7be980dSAndrew Turner *id = (PCI_ARI_RID(bus, func));
259955d3ea17SRyan Stone } else {
260055d3ea17SRyan Stone bus = pci_get_bus(dev);
260155d3ea17SRyan Stone slot = pci_get_slot(dev);
260255d3ea17SRyan Stone func = pci_get_function(dev);
260355d3ea17SRyan Stone
2604d7be980dSAndrew Turner *id = (PCI_RID(bus, slot, func));
260555d3ea17SRyan Stone }
2606d7be980dSAndrew Turner
2607d7be980dSAndrew Turner return (0);
260855d3ea17SRyan Stone }
260955d3ea17SRyan Stone
261055d3ea17SRyan Stone /*
261155d3ea17SRyan Stone * Check that the downstream port (pcib) and the endpoint device (dev) both
261255d3ea17SRyan Stone * support ARI. If so, enable it and return 0, otherwise return an error.
261355d3ea17SRyan Stone */
261455d3ea17SRyan Stone static int
pcib_try_enable_ari(device_t pcib,device_t dev)261555d3ea17SRyan Stone pcib_try_enable_ari(device_t pcib, device_t dev)
261655d3ea17SRyan Stone {
261755d3ea17SRyan Stone struct pcib_softc *sc;
261855d3ea17SRyan Stone int error;
261955d3ea17SRyan Stone uint32_t cap2;
262055d3ea17SRyan Stone int ari_cap_off;
262155d3ea17SRyan Stone uint32_t ari_ver;
262255d3ea17SRyan Stone uint32_t pcie_pos;
262355d3ea17SRyan Stone
262455d3ea17SRyan Stone sc = device_get_softc(pcib);
262555d3ea17SRyan Stone
262655d3ea17SRyan Stone /*
262755d3ea17SRyan Stone * ARI is controlled in a register in the PCIe capability structure.
262855d3ea17SRyan Stone * If the downstream port does not have the PCIe capability structure
262955d3ea17SRyan Stone * then it does not support ARI.
263055d3ea17SRyan Stone */
263155d3ea17SRyan Stone error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos);
263255d3ea17SRyan Stone if (error != 0)
263355d3ea17SRyan Stone return (ENODEV);
263455d3ea17SRyan Stone
263555d3ea17SRyan Stone /* Check that the PCIe port advertises ARI support. */
263655d3ea17SRyan Stone cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4);
263755d3ea17SRyan Stone if (!(cap2 & PCIEM_CAP2_ARI))
263855d3ea17SRyan Stone return (ENODEV);
263955d3ea17SRyan Stone
264055d3ea17SRyan Stone /*
264155d3ea17SRyan Stone * Check that the endpoint device advertises ARI support via the ARI
264255d3ea17SRyan Stone * extended capability structure.
264355d3ea17SRyan Stone */
264455d3ea17SRyan Stone error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off);
264555d3ea17SRyan Stone if (error != 0)
264655d3ea17SRyan Stone return (ENODEV);
264755d3ea17SRyan Stone
264855d3ea17SRyan Stone /*
264955d3ea17SRyan Stone * Finally, check that the endpoint device supports the same version
265055d3ea17SRyan Stone * of ARI that we do.
265155d3ea17SRyan Stone */
265255d3ea17SRyan Stone ari_ver = pci_read_config(dev, ari_cap_off, 4);
265355d3ea17SRyan Stone if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) {
265455d3ea17SRyan Stone if (bootverbose)
265555d3ea17SRyan Stone device_printf(pcib,
265655d3ea17SRyan Stone "Unsupported version of ARI (%d) detected\n",
265755d3ea17SRyan Stone PCI_EXTCAP_VER(ari_ver));
265855d3ea17SRyan Stone
265955d3ea17SRyan Stone return (ENXIO);
266055d3ea17SRyan Stone }
266155d3ea17SRyan Stone
266255d3ea17SRyan Stone pcib_enable_ari(sc, pcie_pos);
266355d3ea17SRyan Stone
266455d3ea17SRyan Stone return (0);
266555d3ea17SRyan Stone }
26664cb67729SWarner Losh
266728586889SWarner Losh int
pcib_request_feature_allow(device_t pcib,device_t dev,enum pci_feature feature)266828586889SWarner Losh pcib_request_feature_allow(device_t pcib, device_t dev,
266928586889SWarner Losh enum pci_feature feature)
267028586889SWarner Losh {
267128586889SWarner Losh /*
26725914c62eSGavin Atkinson * No host firmware we have to negotiate with, so we allow
267328586889SWarner Losh * every valid feature requested.
267428586889SWarner Losh */
267528586889SWarner Losh switch (feature) {
267628586889SWarner Losh case PCI_FEATURE_AER:
267728586889SWarner Losh case PCI_FEATURE_HP:
267828586889SWarner Losh break;
267928586889SWarner Losh default:
268028586889SWarner Losh return (EINVAL);
268128586889SWarner Losh }
268228586889SWarner Losh
268328586889SWarner Losh return (0);
268428586889SWarner Losh }
268528586889SWarner Losh
26861ffd07bdSJohn Baldwin int
pcib_request_feature(device_t dev,enum pci_feature feature)26871ffd07bdSJohn Baldwin pcib_request_feature(device_t dev, enum pci_feature feature)
26881ffd07bdSJohn Baldwin {
26891ffd07bdSJohn Baldwin
26901ffd07bdSJohn Baldwin /*
26911ffd07bdSJohn Baldwin * Invoke PCIB_REQUEST_FEATURE of this bridge first in case
26921ffd07bdSJohn Baldwin * the firmware overrides the method of PCI-PCI bridges.
26931ffd07bdSJohn Baldwin */
26941ffd07bdSJohn Baldwin return (PCIB_REQUEST_FEATURE(dev, dev, feature));
26951ffd07bdSJohn Baldwin }
26961ffd07bdSJohn Baldwin
26974cb67729SWarner Losh /*
26984cb67729SWarner Losh * Pass the request to use this PCI feature up the tree. Either there's a
26994cb67729SWarner Losh * firmware like ACPI that's using this feature that will approve (or deny) the
27004cb67729SWarner Losh * request to take it over, or the platform has no such firmware, in which case
27014cb67729SWarner Losh * the request will be approved. If the request is approved, the OS is expected
27024cb67729SWarner Losh * to make use of the feature or render it harmless.
27034cb67729SWarner Losh */
27044cb67729SWarner Losh static int
pcib_request_feature_default(device_t pcib,device_t dev,enum pci_feature feature)27051ffd07bdSJohn Baldwin pcib_request_feature_default(device_t pcib, device_t dev,
27061ffd07bdSJohn Baldwin enum pci_feature feature)
27074cb67729SWarner Losh {
27084cb67729SWarner Losh device_t bus;
27094cb67729SWarner Losh
27104cb67729SWarner Losh /*
27114cb67729SWarner Losh * Our parent is necessarily a pci bus. Its parent will either be
27124cb67729SWarner Losh * another pci bridge (which passes it up) or a host bridge that can
27134cb67729SWarner Losh * approve or reject the request.
27144cb67729SWarner Losh */
27154cb67729SWarner Losh bus = device_get_parent(pcib);
27164cb67729SWarner Losh return (PCIB_REQUEST_FEATURE(device_get_parent(bus), dev, feature));
27174cb67729SWarner Losh }
27185db2a4a8SKonstantin Belousov
27195db2a4a8SKonstantin Belousov static int
pcib_reset_child(device_t dev,device_t child,int flags)27205db2a4a8SKonstantin Belousov pcib_reset_child(device_t dev, device_t child, int flags)
27215db2a4a8SKonstantin Belousov {
27225db2a4a8SKonstantin Belousov struct pci_devinfo *pdinfo;
27235db2a4a8SKonstantin Belousov int error;
27245db2a4a8SKonstantin Belousov
27255db2a4a8SKonstantin Belousov error = 0;
27265db2a4a8SKonstantin Belousov if (dev == NULL || device_get_parent(child) != dev)
27275db2a4a8SKonstantin Belousov goto out;
27285db2a4a8SKonstantin Belousov error = ENXIO;
27295db2a4a8SKonstantin Belousov if (device_get_devclass(child) != devclass_find("pci"))
27305db2a4a8SKonstantin Belousov goto out;
27315db2a4a8SKonstantin Belousov pdinfo = device_get_ivars(dev);
27325db2a4a8SKonstantin Belousov if (pdinfo->cfg.pcie.pcie_location != 0 &&
27335db2a4a8SKonstantin Belousov (pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_DOWNSTREAM_PORT ||
27345db2a4a8SKonstantin Belousov pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_ROOT_PORT)) {
27355db2a4a8SKonstantin Belousov error = bus_helper_reset_prepare(child, flags);
27365db2a4a8SKonstantin Belousov if (error == 0) {
27375db2a4a8SKonstantin Belousov error = pcie_link_reset(dev,
27385db2a4a8SKonstantin Belousov pdinfo->cfg.pcie.pcie_location);
27395db2a4a8SKonstantin Belousov /* XXXKIB call _post even if error != 0 ? */
27405db2a4a8SKonstantin Belousov bus_helper_reset_post(child, flags);
27415db2a4a8SKonstantin Belousov }
27425db2a4a8SKonstantin Belousov }
27435db2a4a8SKonstantin Belousov out:
27445db2a4a8SKonstantin Belousov return (error);
27455db2a4a8SKonstantin Belousov }
2746