Home | History | Annotate | Line # | Download | only in pci
pci_resource.c revision 1.6
      1  1.6  riastrad /* $NetBSD: pci_resource.c,v 1.6 2025/03/03 19:02:30 riastradh Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2022 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  jmcneill  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  jmcneill  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  jmcneill  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  jmcneill  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  1.1  jmcneill  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  1.1  jmcneill  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  1.1  jmcneill  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  1.1  jmcneill  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  jmcneill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  jmcneill  * SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill /*
     30  1.1  jmcneill  * pci_resource.c --
     31  1.1  jmcneill  *
     32  1.1  jmcneill  * Scan current PCI resource allocations and attempt to assign resources
     33  1.1  jmcneill  * to devices that are not configured WITHOUT changing any configuration
     34  1.1  jmcneill  * performed by system firmware.
     35  1.1  jmcneill  */
     36  1.1  jmcneill 
     37  1.1  jmcneill #include <sys/cdefs.h>
     38  1.6  riastrad __KERNEL_RCSID(0, "$NetBSD: pci_resource.c,v 1.6 2025/03/03 19:02:30 riastradh Exp $");
     39  1.1  jmcneill 
     40  1.1  jmcneill #include <sys/param.h>
     41  1.6  riastrad #include <sys/types.h>
     42  1.6  riastrad 
     43  1.1  jmcneill #include <sys/bus.h>
     44  1.6  riastrad #include <sys/kmem.h>
     45  1.6  riastrad #include <sys/queue.h>
     46  1.1  jmcneill #include <sys/systm.h>
     47  1.1  jmcneill #include <sys/vmem.h>
     48  1.1  jmcneill 
     49  1.1  jmcneill #include <dev/pci/pcireg.h>
     50  1.1  jmcneill #include <dev/pci/pcivar.h>
     51  1.1  jmcneill #include <dev/pci/pcidevs.h>
     52  1.1  jmcneill #include <dev/pci/pci_resource.h>
     53  1.1  jmcneill 
     54  1.1  jmcneill #define	DPRINT		aprint_debug
     55  1.1  jmcneill 
     56  1.1  jmcneill #if defined(PCI_RESOURCE_TEST_VENDOR_ID) && \
     57  1.1  jmcneill     defined(PCI_RESOURCE_TEST_PRODUCT_ID)
     58  1.1  jmcneill #define IS_TEST_DEVICE(_pd)						      \
     59  1.1  jmcneill 	(PCI_VENDOR(pd->pd_id) == PCI_RESOURCE_TEST_VENDOR_ID &&	      \
     60  1.1  jmcneill 	 PCI_PRODUCT(pd->pd_id) == PCI_RESOURCE_TEST_PRODUCT_ID)
     61  1.1  jmcneill #else
     62  1.1  jmcneill #define IS_TEST_DEVICE(_pd)	0
     63  1.1  jmcneill #endif
     64  1.1  jmcneill 
     65  1.1  jmcneill #define	PCI_MAX_DEVICE	32
     66  1.1  jmcneill #define	PCI_MAX_FUNC	8
     67  1.1  jmcneill 
     68  1.1  jmcneill #define	PCI_MAX_IORES	6
     69  1.1  jmcneill 
     70  1.1  jmcneill #define	PCI_RANGE_FOREACH(_type)					      \
     71  1.1  jmcneill 	for (u_int _type = PCI_RANGE_BUS; _type < NUM_PCI_RANGES; _type++)
     72  1.1  jmcneill 
     73  1.1  jmcneill static const char *pci_range_typenames[NUM_PCI_RANGES] = {
     74  1.1  jmcneill 	[PCI_RANGE_BUS]  = "bus",
     75  1.1  jmcneill 	[PCI_RANGE_IO]   = "io",
     76  1.1  jmcneill 	[PCI_RANGE_MEM]  = "mem",
     77  1.1  jmcneill 	[PCI_RANGE_PMEM] = "pmem",
     78  1.1  jmcneill };
     79  1.1  jmcneill 
     80  1.1  jmcneill struct pci_bus;
     81  1.1  jmcneill 
     82  1.1  jmcneill struct pci_iores {
     83  1.1  jmcneill 	uint64_t	pi_base;	/* Base address */
     84  1.1  jmcneill 	uint64_t	pi_size;	/* Resource size */
     85  1.1  jmcneill 	uint8_t		pi_type;	/* PCI_MAPREG_TYPE_* */
     86  1.1  jmcneill 	u_int		pi_bar;		/* PCI bar number */
     87  1.1  jmcneill 	union {
     88  1.1  jmcneill 		struct {
     89  1.1  jmcneill 			uint8_t		memtype;
     90  1.1  jmcneill 			bool		prefetch;
     91  1.1  jmcneill 		} pi_mem;
     92  1.1  jmcneill 	};
     93  1.1  jmcneill };
     94  1.1  jmcneill 
     95  1.1  jmcneill struct pci_device {
     96  1.1  jmcneill 	bool		pd_present;	/* Device is present */
     97  1.1  jmcneill 	bool		pd_configured;	/* Device is configured */
     98  1.1  jmcneill 	struct pci_bus *pd_bus;	/* Parent bus */
     99  1.1  jmcneill 	uint8_t		pd_devno;	/* Device number */
    100  1.1  jmcneill 	uint8_t		pd_funcno;	/* Function number */
    101  1.1  jmcneill 	pcitag_t	pd_tag;		/* PCI tag */
    102  1.1  jmcneill 
    103  1.1  jmcneill 	pcireg_t	pd_id;		/* Vendor ID, Device ID */
    104  1.1  jmcneill 	pcireg_t	pd_class;	/* Revision ID, Class Code */
    105  1.1  jmcneill 	pcireg_t	pd_bhlc;	/* BIST, Header Type, Primary Latency
    106  1.1  jmcneill 					 * Timer, Cache Line Size */
    107  1.1  jmcneill 
    108  1.1  jmcneill 	struct pci_iores pd_iores[PCI_MAX_IORES];
    109  1.1  jmcneill 	u_int		pd_niores;
    110  1.1  jmcneill 
    111  1.1  jmcneill 	bool		pd_ppb;		/* PCI-PCI bridge */
    112  1.1  jmcneill 	union {
    113  1.1  jmcneill 		struct {
    114  1.1  jmcneill 			pcireg_t	bridge_bus;
    115  1.6  riastrad 			struct pci_resource_arena *ranges[NUM_PCI_RANGES];
    116  1.1  jmcneill 		} pd_bridge;
    117  1.1  jmcneill 	};
    118  1.1  jmcneill };
    119  1.1  jmcneill 
    120  1.1  jmcneill struct pci_bus {
    121  1.1  jmcneill 	uint8_t		pb_busno;	/* Bus number */
    122  1.1  jmcneill 	struct pci_device *pb_bridge; /* Parent bridge, or NULL */
    123  1.1  jmcneill 
    124  1.1  jmcneill 	struct pci_device pb_device[PCI_MAX_DEVICE * PCI_MAX_FUNC];
    125  1.1  jmcneill 					/* Devices on bus */
    126  1.1  jmcneill 	u_int		pb_lastdevno;	/* Last device found */
    127  1.1  jmcneill 
    128  1.6  riastrad 	/* XXX Nothing seems to use pb_ranges? */
    129  1.6  riastrad 	struct pci_resource_arena *pb_ranges[NUM_PCI_RANGES];
    130  1.6  riastrad 	struct pci_resource_arena *pb_res[NUM_PCI_RANGES];
    131  1.1  jmcneill };
    132  1.1  jmcneill 
    133  1.1  jmcneill struct pci_resources {
    134  1.1  jmcneill 	struct pci_bus **pr_bus;	/* Bus list */
    135  1.1  jmcneill 	pci_chipset_tag_t pr_pc;	/* Chipset tag */
    136  1.1  jmcneill 	uint8_t		pr_startbus;	/* First bus number */
    137  1.1  jmcneill 	uint8_t		pr_endbus;	/* Last bus number */
    138  1.1  jmcneill 
    139  1.6  riastrad 	struct pci_resource_arena *pr_ranges[NUM_PCI_RANGES];
    140  1.6  riastrad };
    141  1.6  riastrad 
    142  1.6  riastrad struct pci_resource_arena {
    143  1.6  riastrad 	vmem_t					*vmem;
    144  1.6  riastrad 	SLIST_HEAD(, pci_resource_range)	list;
    145  1.6  riastrad };
    146  1.6  riastrad 
    147  1.6  riastrad struct pci_resource_range {
    148  1.6  riastrad 	uint64_t			start;
    149  1.6  riastrad 	uint64_t			end;
    150  1.6  riastrad 	SLIST_ENTRY(pci_resource_range)	entry;
    151  1.1  jmcneill };
    152  1.1  jmcneill 
    153  1.5  jmcneill static int	pci_resource_scan_bus(struct pci_resources *,
    154  1.3  riastrad 		    struct pci_device *, uint8_t);
    155  1.1  jmcneill 
    156  1.1  jmcneill #define	PCI_SBDF_FMT			"%04x:%02x:%02x.%u"
    157  1.1  jmcneill #define	PCI_SBDF_FMT_ARGS(_pr, _pd)	\
    158  1.1  jmcneill 	pci_get_segment((_pr)->pr_pc),	\
    159  1.1  jmcneill 	(_pd)->pd_bus->pb_busno,	\
    160  1.1  jmcneill 	(_pd)->pd_devno,		\
    161  1.1  jmcneill 	(_pd)->pd_funcno
    162  1.1  jmcneill 
    163  1.1  jmcneill #define	PCICONF_RES_BUS(_pr, _busno)				\
    164  1.1  jmcneill 	((_pr)->pr_bus[(_busno) - (_pr)->pr_startbus])
    165  1.1  jmcneill #define	PCICONF_BUS_DEVICE(_pb, _devno, _funcno)		\
    166  1.1  jmcneill 	(&(_pb)->pb_device[(_devno) * PCI_MAX_FUNC + (_funcno)])
    167  1.1  jmcneill 
    168  1.6  riastrad static void
    169  1.6  riastrad pci_resource_arena_add_range(struct pci_resource_arena **arenas,
    170  1.6  riastrad     enum pci_range_type type, uint64_t start, uint64_t end)
    171  1.6  riastrad {
    172  1.6  riastrad 	struct pci_resource_arena *arena;
    173  1.6  riastrad 	struct pci_resource_range *new, *range, *prev;
    174  1.6  riastrad 	int error;
    175  1.6  riastrad 
    176  1.6  riastrad 	/*
    177  1.6  riastrad 	 * Create an arena if we haven't already.
    178  1.6  riastrad 	 */
    179  1.6  riastrad 	if ((arena = arenas[type]) == NULL) {
    180  1.6  riastrad 		arena = arenas[type] = kmem_zalloc(sizeof(*arenas[type]),
    181  1.6  riastrad 		    KM_SLEEP);
    182  1.6  riastrad 		arena->vmem = vmem_create(pci_resource_typename(type),
    183  1.6  riastrad 		    0, 0, 1, NULL, NULL, NULL, 0, VM_SLEEP, IPL_NONE);
    184  1.6  riastrad 		SLIST_INIT(&arena->list);
    185  1.6  riastrad 	}
    186  1.6  riastrad 
    187  1.6  riastrad 	/*
    188  1.6  riastrad 	 * Warn if this is a bus range and there already is a bus
    189  1.6  riastrad 	 * range, or if the start/end are bad.  The other types of
    190  1.6  riastrad 	 * ranges can have more than one range and larger addresses.
    191  1.6  riastrad 	 *
    192  1.6  riastrad 	 * XXX Not accurate: some machines do have multiple bus ranges.
    193  1.6  riastrad 	 * But currently this logic can't handle that -- requires some
    194  1.6  riastrad 	 * extra work to iterate over all the bus ranges.  TBD.
    195  1.6  riastrad 	 */
    196  1.6  riastrad 	if (type == PCI_RANGE_BUS &&
    197  1.6  riastrad 	    (start > UINT8_MAX || end > UINT8_MAX ||
    198  1.6  riastrad 		!SLIST_EMPTY(&arena->list))) {
    199  1.6  riastrad 		aprint_error("PCI: unexpected bus range"
    200  1.6  riastrad 		    " %" PRIu64 "-%" PRIu64 ", ignoring\n",
    201  1.6  riastrad 		    start, end);
    202  1.6  riastrad 		return;
    203  1.6  riastrad 	}
    204  1.6  riastrad 
    205  1.6  riastrad 	/*
    206  1.6  riastrad 	 * Reserve the range in the vmem for allocation.  If there's
    207  1.6  riastrad 	 * already an overlapping range, just drop this one.
    208  1.6  riastrad 	 */
    209  1.6  riastrad 	error = vmem_add(arena->vmem, start, end - start + 1, VM_SLEEP);
    210  1.6  riastrad 	if (error) {
    211  1.6  riastrad 		/* XXX show some more context */
    212  1.6  riastrad 		aprint_error("overlapping %s range: %#" PRIx64 "-%#" PRIx64 ","
    213  1.6  riastrad 		    " discarding\n",
    214  1.6  riastrad 		    pci_resource_typename(type), start, end);
    215  1.6  riastrad 		return;
    216  1.6  riastrad 	}
    217  1.6  riastrad 
    218  1.6  riastrad 	/*
    219  1.6  riastrad 	 * Add an entry to the list so we can iterate over them, in
    220  1.6  riastrad 	 * ascending address order for the sake of legible printing.
    221  1.6  riastrad 	 * (We don't expect to have so many entries that the linear
    222  1.6  riastrad 	 * time of insertion will cause trouble.)
    223  1.6  riastrad 	 */
    224  1.6  riastrad 	new = kmem_zalloc(sizeof(*new), KM_SLEEP);
    225  1.6  riastrad 	new->start = start;
    226  1.6  riastrad 	new->end = end;
    227  1.6  riastrad 	prev = NULL;
    228  1.6  riastrad 	SLIST_FOREACH(range, &arena->list, entry) {
    229  1.6  riastrad 		if (new->start < range->start)
    230  1.6  riastrad 			break;
    231  1.6  riastrad 		prev = range;
    232  1.6  riastrad 	}
    233  1.6  riastrad 	if (prev) {
    234  1.6  riastrad 		SLIST_INSERT_AFTER(prev, new, entry);
    235  1.6  riastrad 	} else {
    236  1.6  riastrad 		SLIST_INSERT_HEAD(&arena->list, new, entry);
    237  1.6  riastrad 	}
    238  1.6  riastrad }
    239  1.6  riastrad 
    240  1.1  jmcneill /*
    241  1.6  riastrad  * pci_resource_add_range --
    242  1.1  jmcneill  *
    243  1.6  riastrad  *   Add a contiguous range of addresses (inclusive of both bounds) for
    244  1.6  riastrad  *   the specified type of resource.
    245  1.1  jmcneill  */
    246  1.6  riastrad void
    247  1.6  riastrad pci_resource_add_range(struct pci_resource_info *info,
    248  1.6  riastrad     enum pci_range_type type, uint64_t start, uint64_t end)
    249  1.1  jmcneill {
    250  1.1  jmcneill 
    251  1.6  riastrad 	pci_resource_arena_add_range(info->ranges, type, start, end);
    252  1.1  jmcneill }
    253  1.1  jmcneill 
    254  1.1  jmcneill /*
    255  1.1  jmcneill  * pci_new_bus --
    256  1.1  jmcneill  *
    257  1.1  jmcneill  *   Create a new PCI bus and initialize its resource ranges.
    258  1.1  jmcneill  */
    259  1.1  jmcneill static struct pci_bus *
    260  1.1  jmcneill pci_new_bus(struct pci_resources *pr, uint8_t busno, struct pci_device *bridge)
    261  1.1  jmcneill {
    262  1.1  jmcneill 	struct pci_bus *pb;
    263  1.6  riastrad 	struct pci_resource_arena **ranges;
    264  1.1  jmcneill 
    265  1.1  jmcneill 	pb = kmem_zalloc(sizeof(*pb), KM_SLEEP);
    266  1.1  jmcneill 	pb->pb_busno = busno;
    267  1.1  jmcneill 	pb->pb_bridge = bridge;
    268  1.1  jmcneill 	if (bridge == NULL) {
    269  1.1  jmcneill 		/*
    270  1.1  jmcneill 		 * No additional constraints on resource allocations for
    271  1.1  jmcneill 		 * the root bus.
    272  1.1  jmcneill 		 */
    273  1.1  jmcneill 		ranges = pr->pr_ranges;
    274  1.1  jmcneill 	} else {
    275  1.1  jmcneill 		/*
    276  1.1  jmcneill 		 * Resource allocations for this bus are constrained by the
    277  1.1  jmcneill 		 * bridge forwarding settings.
    278  1.1  jmcneill 		 */
    279  1.1  jmcneill 		ranges = bridge->pd_bridge.ranges;
    280  1.1  jmcneill 	}
    281  1.1  jmcneill 	memcpy(pb->pb_ranges, ranges, sizeof(pb->pb_ranges));
    282  1.1  jmcneill 
    283  1.1  jmcneill 	return pb;
    284  1.1  jmcneill }
    285  1.1  jmcneill 
    286  1.1  jmcneill /*
    287  1.1  jmcneill  * pci_resource_device_functions --
    288  1.1  jmcneill  *
    289  1.1  jmcneill  *   Returns the number of PCI functions for a a given bus and device.
    290  1.1  jmcneill  */
    291  1.1  jmcneill static uint8_t
    292  1.1  jmcneill pci_resource_device_functions(struct pci_resources *pr,
    293  1.1  jmcneill     uint8_t busno, uint8_t devno)
    294  1.1  jmcneill {
    295  1.1  jmcneill 	struct pci_bus *pb;
    296  1.1  jmcneill 	struct pci_device *pd;
    297  1.1  jmcneill 
    298  1.1  jmcneill 	pb = PCICONF_RES_BUS(pr, busno);
    299  1.1  jmcneill 	pd = PCICONF_BUS_DEVICE(pb, devno, 0);
    300  1.1  jmcneill 	if (!pd->pd_present) {
    301  1.1  jmcneill 		return 0;
    302  1.1  jmcneill 	}
    303  1.1  jmcneill 
    304  1.1  jmcneill 	return PCI_HDRTYPE_MULTIFN(pd->pd_bhlc) ? 8 : 1;
    305  1.1  jmcneill }
    306  1.1  jmcneill 
    307  1.1  jmcneill /*
    308  1.1  jmcneill  * pci_resource_device_print --
    309  1.1  jmcneill  *
    310  1.1  jmcneill  *   Log details about a device.
    311  1.1  jmcneill  */
    312  1.1  jmcneill static void
    313  1.1  jmcneill pci_resource_device_print(struct pci_resources *pr,
    314  1.1  jmcneill     struct pci_device *pd)
    315  1.1  jmcneill {
    316  1.1  jmcneill 	struct pci_iores *pi;
    317  1.6  riastrad 	struct pci_resource_range *range;
    318  1.1  jmcneill 	u_int res;
    319  1.1  jmcneill 
    320  1.1  jmcneill 	DPRINT("PCI: " PCI_SBDF_FMT " %04x:%04x %02x 0x%06x",
    321  1.1  jmcneill 	       PCI_SBDF_FMT_ARGS(pr, pd),
    322  1.1  jmcneill 	       PCI_VENDOR(pd->pd_id), PCI_PRODUCT(pd->pd_id),
    323  1.1  jmcneill 	       PCI_REVISION(pd->pd_class), (pd->pd_class >> 8) & 0xffffff);
    324  1.1  jmcneill 
    325  1.1  jmcneill 	switch (PCI_HDRTYPE_TYPE(pd->pd_bhlc)) {
    326  1.1  jmcneill 	case PCI_HDRTYPE_DEVICE:
    327  1.1  jmcneill 		DPRINT(" (device)\n");
    328  1.1  jmcneill 		break;
    329  1.1  jmcneill 	case PCI_HDRTYPE_PPB:
    330  1.1  jmcneill 		DPRINT(" (bridge %u -> %u-%u)\n",
    331  1.1  jmcneill 		    PCI_BRIDGE_BUS_NUM_PRIMARY(pd->pd_bridge.bridge_bus),
    332  1.1  jmcneill 		    PCI_BRIDGE_BUS_NUM_SECONDARY(pd->pd_bridge.bridge_bus),
    333  1.1  jmcneill 		    PCI_BRIDGE_BUS_NUM_SUBORDINATE(pd->pd_bridge.bridge_bus));
    334  1.1  jmcneill 
    335  1.6  riastrad 		if (pd->pd_bridge.ranges[PCI_RANGE_IO]) {
    336  1.6  riastrad 			SLIST_FOREACH(range,
    337  1.6  riastrad 			    &pd->pd_bridge.ranges[PCI_RANGE_IO]->list,
    338  1.6  riastrad 			    entry) {
    339  1.6  riastrad 				DPRINT("PCI: " PCI_SBDF_FMT
    340  1.6  riastrad 				    " [bridge] window io "
    341  1.6  riastrad 				    " %#" PRIx64 "-%#" PRIx64
    342  1.6  riastrad 				    "\n",
    343  1.6  riastrad 				    PCI_SBDF_FMT_ARGS(pr, pd),
    344  1.6  riastrad 				    range->start,
    345  1.6  riastrad 				    range->end);
    346  1.6  riastrad 			}
    347  1.6  riastrad 		}
    348  1.6  riastrad 		if (pd->pd_bridge.ranges[PCI_RANGE_MEM]) {
    349  1.6  riastrad 			SLIST_FOREACH(range,
    350  1.6  riastrad 			    &pd->pd_bridge.ranges[PCI_RANGE_MEM]->list,
    351  1.6  riastrad 			    entry) {
    352  1.6  riastrad 				DPRINT("PCI: " PCI_SBDF_FMT
    353  1.6  riastrad 				    " [bridge] window mem"
    354  1.6  riastrad 				    " %#" PRIx64 "-%#" PRIx64
    355  1.6  riastrad 				    " (non-prefetchable)\n",
    356  1.6  riastrad 				    PCI_SBDF_FMT_ARGS(pr, pd),
    357  1.6  riastrad 				    range->start,
    358  1.6  riastrad 				    range->end);
    359  1.6  riastrad 			}
    360  1.6  riastrad 		}
    361  1.6  riastrad 		if (pd->pd_bridge.ranges[PCI_RANGE_PMEM]) {
    362  1.6  riastrad 			SLIST_FOREACH(range,
    363  1.6  riastrad 			    &pd->pd_bridge.ranges[PCI_RANGE_PMEM]->list,
    364  1.6  riastrad 			    entry) {
    365  1.6  riastrad 				DPRINT("PCI: " PCI_SBDF_FMT
    366  1.6  riastrad 				    " [bridge] window mem"
    367  1.6  riastrad 				    " %#" PRIx64 "-%#" PRIx64
    368  1.6  riastrad 				    " (prefetchable)\n",
    369  1.6  riastrad 				    PCI_SBDF_FMT_ARGS(pr, pd),
    370  1.6  riastrad 				    range->start,
    371  1.6  riastrad 				    range->end);
    372  1.6  riastrad 			}
    373  1.1  jmcneill 		}
    374  1.1  jmcneill 
    375  1.1  jmcneill 		break;
    376  1.1  jmcneill 	default:
    377  1.1  jmcneill 		DPRINT(" (0x%02x)\n", PCI_HDRTYPE_TYPE(pd->pd_bhlc));
    378  1.1  jmcneill 	}
    379  1.1  jmcneill 
    380  1.1  jmcneill 	for (res = 0; res < pd->pd_niores; res++) {
    381  1.1  jmcneill 		pi = &pd->pd_iores[res];
    382  1.1  jmcneill 
    383  1.1  jmcneill 		DPRINT("PCI: " PCI_SBDF_FMT
    384  1.1  jmcneill 		       " [device] resource BAR%u: %s @ %#" PRIx64 " size %#"
    385  1.1  jmcneill 		       PRIx64,
    386  1.1  jmcneill 		       PCI_SBDF_FMT_ARGS(pr, pd), pi->pi_bar,
    387  1.1  jmcneill 		       pi->pi_type == PCI_MAPREG_TYPE_MEM ? "mem" : "io ",
    388  1.1  jmcneill 		       pi->pi_base, pi->pi_size);
    389  1.1  jmcneill 
    390  1.1  jmcneill 		if (pi->pi_type == PCI_MAPREG_TYPE_MEM) {
    391  1.1  jmcneill 			switch (pi->pi_mem.memtype) {
    392  1.1  jmcneill 			case PCI_MAPREG_MEM_TYPE_32BIT:
    393  1.1  jmcneill 				DPRINT(", 32-bit");
    394  1.1  jmcneill 				break;
    395  1.1  jmcneill 			case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    396  1.1  jmcneill 				DPRINT(", 32-bit (1M)");
    397  1.1  jmcneill 				break;
    398  1.1  jmcneill 			case PCI_MAPREG_MEM_TYPE_64BIT:
    399  1.1  jmcneill 				DPRINT(", 64-bit");
    400  1.1  jmcneill 				break;
    401  1.1  jmcneill 			}
    402  1.1  jmcneill 			DPRINT(" %sprefetchable",
    403  1.1  jmcneill 			    pi->pi_mem.prefetch ? "" : "non-");
    404  1.1  jmcneill 		}
    405  1.1  jmcneill 		DPRINT("\n");
    406  1.1  jmcneill 	}
    407  1.1  jmcneill }
    408  1.1  jmcneill 
    409  1.1  jmcneill /*
    410  1.1  jmcneill  * pci_resource_scan_bar --
    411  1.1  jmcneill  *
    412  1.1  jmcneill  *   Determine the current BAR configuration for a given device.
    413  1.1  jmcneill  */
    414  1.1  jmcneill static void
    415  1.1  jmcneill pci_resource_scan_bar(struct pci_resources *pr,
    416  1.1  jmcneill     struct pci_device *pd, pcireg_t mapreg_start, pcireg_t mapreg_end,
    417  1.1  jmcneill     bool is_ppb)
    418  1.1  jmcneill {
    419  1.1  jmcneill 	pci_chipset_tag_t pc = pr->pr_pc;
    420  1.1  jmcneill 	pcitag_t tag = pd->pd_tag;
    421  1.1  jmcneill 	pcireg_t mapreg = mapreg_start;
    422  1.1  jmcneill 	pcireg_t ocmd, cmd, bar[2], mask[2];
    423  1.1  jmcneill 	uint64_t addr, size;
    424  1.1  jmcneill 	struct pci_iores *pi;
    425  1.1  jmcneill 
    426  1.1  jmcneill 	if (!is_ppb) {
    427  1.1  jmcneill 		ocmd = cmd = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
    428  1.1  jmcneill 		cmd &= ~(PCI_COMMAND_MASTER_ENABLE |
    429  1.1  jmcneill 			 PCI_COMMAND_MEM_ENABLE |
    430  1.1  jmcneill 			 PCI_COMMAND_IO_ENABLE);
    431  1.1  jmcneill 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, cmd);
    432  1.1  jmcneill 	}
    433  1.1  jmcneill 
    434  1.1  jmcneill 	while (mapreg < mapreg_end) {
    435  1.1  jmcneill 		u_int width = 4;
    436  1.1  jmcneill 
    437  1.1  jmcneill 		bar[0] = pci_conf_read(pc, tag, mapreg);
    438  1.1  jmcneill 		pci_conf_write(pc, tag, mapreg, 0xffffffff);
    439  1.1  jmcneill 		mask[0] = pci_conf_read(pc, tag, mapreg);
    440  1.1  jmcneill 		pci_conf_write(pc, tag, mapreg, bar[0]);
    441  1.1  jmcneill 
    442  1.1  jmcneill 		switch (PCI_MAPREG_TYPE(mask[0])) {
    443  1.1  jmcneill 		case PCI_MAPREG_TYPE_MEM:
    444  1.1  jmcneill 			switch (PCI_MAPREG_MEM_TYPE(mask[0])) {
    445  1.1  jmcneill 			case PCI_MAPREG_MEM_TYPE_32BIT:
    446  1.1  jmcneill 			case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    447  1.1  jmcneill 				size = PCI_MAPREG_MEM_SIZE(mask[0]);
    448  1.1  jmcneill 				addr = PCI_MAPREG_MEM_ADDR(bar[0]);
    449  1.1  jmcneill 				break;
    450  1.1  jmcneill 			case PCI_MAPREG_MEM_TYPE_64BIT:
    451  1.1  jmcneill 				bar[1] = pci_conf_read(pc, tag, mapreg + 4);
    452  1.1  jmcneill 				pci_conf_write(pc, tag, mapreg + 4, 0xffffffff);
    453  1.1  jmcneill 				mask[1] = pci_conf_read(pc, tag, mapreg + 4);
    454  1.1  jmcneill 				pci_conf_write(pc, tag, mapreg + 4, bar[1]);
    455  1.1  jmcneill 
    456  1.1  jmcneill 				size = PCI_MAPREG_MEM64_SIZE(
    457  1.1  jmcneill 				    ((uint64_t)mask[1] << 32) | mask[0]);
    458  1.1  jmcneill 				addr = PCI_MAPREG_MEM64_ADDR(
    459  1.1  jmcneill 				    ((uint64_t)bar[1] << 32) | bar[0]);
    460  1.1  jmcneill 				width = 8;
    461  1.1  jmcneill 				break;
    462  1.1  jmcneill 			default:
    463  1.1  jmcneill 				size = 0;
    464  1.1  jmcneill 			}
    465  1.1  jmcneill 			if (size > 0) {
    466  1.1  jmcneill 				pi = &pd->pd_iores[pd->pd_niores++];
    467  1.1  jmcneill 				pi->pi_type = PCI_MAPREG_TYPE_MEM;
    468  1.1  jmcneill 				pi->pi_base = addr;
    469  1.1  jmcneill 				pi->pi_size = size;
    470  1.1  jmcneill 				pi->pi_bar = (mapreg - mapreg_start) / 4;
    471  1.1  jmcneill 				pi->pi_mem.memtype =
    472  1.1  jmcneill 				    PCI_MAPREG_MEM_TYPE(mask[0]);
    473  1.1  jmcneill 				pi->pi_mem.prefetch =
    474  1.1  jmcneill 				    PCI_MAPREG_MEM_PREFETCHABLE(mask[0]);
    475  1.1  jmcneill 			}
    476  1.1  jmcneill 			break;
    477  1.1  jmcneill 		case PCI_MAPREG_TYPE_IO:
    478  1.1  jmcneill 			size = PCI_MAPREG_IO_SIZE(mask[0] | 0xffff0000);
    479  1.1  jmcneill 			addr = PCI_MAPREG_IO_ADDR(bar[0]);
    480  1.1  jmcneill 			if (size > 0) {
    481  1.1  jmcneill 				pi = &pd->pd_iores[pd->pd_niores++];
    482  1.1  jmcneill 				pi->pi_type = PCI_MAPREG_TYPE_IO;
    483  1.1  jmcneill 				pi->pi_base = addr;
    484  1.1  jmcneill 				pi->pi_size = size;
    485  1.1  jmcneill 				pi->pi_bar = (mapreg - mapreg_start) / 4;
    486  1.1  jmcneill 			}
    487  1.1  jmcneill 			break;
    488  1.1  jmcneill 		}
    489  1.1  jmcneill 
    490  1.1  jmcneill 		KASSERT(pd->pd_niores <= PCI_MAX_IORES);
    491  1.1  jmcneill 
    492  1.1  jmcneill 		mapreg += width;
    493  1.1  jmcneill 	}
    494  1.1  jmcneill 
    495  1.1  jmcneill 	if (!is_ppb) {
    496  1.1  jmcneill 		pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, ocmd);
    497  1.1  jmcneill 	}
    498  1.1  jmcneill }
    499  1.1  jmcneill 
    500  1.1  jmcneill /*
    501  1.1  jmcneill  * pci_resource_scan_bridge --
    502  1.1  jmcneill  *
    503  1.1  jmcneill  *   Determine the current configuration of a PCI-PCI bridge.
    504  1.1  jmcneill  */
    505  1.1  jmcneill static void
    506  1.1  jmcneill pci_resource_scan_bridge(struct pci_resources *pr,
    507  1.1  jmcneill     struct pci_device *pd)
    508  1.1  jmcneill {
    509  1.1  jmcneill 	pci_chipset_tag_t pc = pr->pr_pc;
    510  1.1  jmcneill 	pcitag_t tag = pd->pd_tag;
    511  1.1  jmcneill 	pcireg_t res, reshigh;
    512  1.6  riastrad 	uint64_t iostart, ioend;
    513  1.6  riastrad 	uint64_t memstart, memend;
    514  1.6  riastrad 	uint64_t pmemstart, pmemend;
    515  1.1  jmcneill 
    516  1.1  jmcneill 	pd->pd_ppb = true;
    517  1.1  jmcneill 
    518  1.1  jmcneill 	res = pci_conf_read(pc, tag, PCI_BRIDGE_BUS_REG);
    519  1.1  jmcneill 	pd->pd_bridge.bridge_bus = res;
    520  1.6  riastrad 	pci_resource_arena_add_range(pd->pd_bridge.ranges,
    521  1.6  riastrad 	    PCI_RANGE_BUS,
    522  1.6  riastrad 	    PCI_BRIDGE_BUS_NUM_SECONDARY(res),
    523  1.6  riastrad 	    PCI_BRIDGE_BUS_NUM_SUBORDINATE(res));
    524  1.1  jmcneill 
    525  1.1  jmcneill 	res = pci_conf_read(pc, tag, PCI_BRIDGE_STATIO_REG);
    526  1.6  riastrad 	iostart = PCI_BRIDGE_STATIO_IOBASE_ADDR(res);
    527  1.6  riastrad 	ioend = PCI_BRIDGE_STATIO_IOLIMIT_ADDR(res);
    528  1.1  jmcneill 	if (PCI_BRIDGE_IO_32BITS(res)) {
    529  1.1  jmcneill 		reshigh = pci_conf_read(pc, tag, PCI_BRIDGE_IOHIGH_REG);
    530  1.6  riastrad 		iostart |= __SHIFTOUT(reshigh, PCI_BRIDGE_IOHIGH_BASE) << 16;
    531  1.6  riastrad 		ioend |= __SHIFTOUT(reshigh, PCI_BRIDGE_IOHIGH_LIMIT) << 16;
    532  1.6  riastrad 	}
    533  1.6  riastrad 	if (iostart < ioend) {
    534  1.6  riastrad 		pci_resource_arena_add_range(pd->pd_bridge.ranges,
    535  1.6  riastrad 		    PCI_RANGE_IO, iostart, ioend);
    536  1.1  jmcneill 	}
    537  1.1  jmcneill 
    538  1.1  jmcneill 	res = pci_conf_read(pc, tag, PCI_BRIDGE_MEMORY_REG);
    539  1.6  riastrad 	memstart = PCI_BRIDGE_MEMORY_BASE_ADDR(res);
    540  1.6  riastrad 	memend = PCI_BRIDGE_MEMORY_LIMIT_ADDR(res);
    541  1.6  riastrad 	if (memstart < memend) {
    542  1.6  riastrad 		pci_resource_arena_add_range(pd->pd_bridge.ranges,
    543  1.6  riastrad 		    PCI_RANGE_MEM, memstart, memend);
    544  1.1  jmcneill 	}
    545  1.1  jmcneill 
    546  1.1  jmcneill 	res = pci_conf_read(pc, tag, PCI_BRIDGE_PREFETCHMEM_REG);
    547  1.6  riastrad 	pmemstart = PCI_BRIDGE_PREFETCHMEM_BASE_ADDR(res);
    548  1.6  riastrad 	pmemend = PCI_BRIDGE_PREFETCHMEM_LIMIT_ADDR(res);
    549  1.1  jmcneill 	if (PCI_BRIDGE_PREFETCHMEM_64BITS(res)) {
    550  1.1  jmcneill 		reshigh = pci_conf_read(pc, tag,
    551  1.1  jmcneill 		    PCI_BRIDGE_PREFETCHBASEUP32_REG);
    552  1.6  riastrad 		pmemstart |= (uint64_t)reshigh << 32;
    553  1.1  jmcneill 		reshigh = pci_conf_read(pc, tag,
    554  1.1  jmcneill 		    PCI_BRIDGE_PREFETCHLIMITUP32_REG);
    555  1.6  riastrad 		pmemend |= (uint64_t)reshigh << 32;
    556  1.1  jmcneill 	}
    557  1.6  riastrad 	if (pmemstart < pmemend) {
    558  1.6  riastrad 		pci_resource_arena_add_range(pd->pd_bridge.ranges,
    559  1.6  riastrad 		    PCI_RANGE_PMEM, pmemstart, pmemend);
    560  1.1  jmcneill 	}
    561  1.1  jmcneill }
    562  1.1  jmcneill 
    563  1.1  jmcneill /*
    564  1.1  jmcneill  * pci_resource_scan_device --
    565  1.1  jmcneill  *
    566  1.1  jmcneill  *   Determine the current configuration of a PCI device.
    567  1.1  jmcneill  */
    568  1.1  jmcneill static bool
    569  1.1  jmcneill pci_resource_scan_device(struct pci_resources *pr,
    570  1.1  jmcneill     struct pci_bus *parent_bus, uint8_t devno, uint8_t funcno)
    571  1.1  jmcneill {
    572  1.1  jmcneill 	struct pci_device *pd;
    573  1.1  jmcneill 	pcitag_t tag;
    574  1.1  jmcneill 	pcireg_t id, bridge_bus;
    575  1.1  jmcneill 	uint8_t sec_bus;
    576  1.1  jmcneill 
    577  1.1  jmcneill 	tag = pci_make_tag(pr->pr_pc, parent_bus->pb_busno, devno, funcno);
    578  1.1  jmcneill 	id = pci_conf_read(pr->pr_pc, tag, PCI_ID_REG);
    579  1.1  jmcneill 	if (PCI_VENDOR(id) == PCI_VENDOR_INVALID) {
    580  1.1  jmcneill 		return false;
    581  1.1  jmcneill 	}
    582  1.1  jmcneill 
    583  1.1  jmcneill 	pd = PCICONF_BUS_DEVICE(parent_bus, devno, funcno);
    584  1.1  jmcneill 	pd->pd_present = true;
    585  1.1  jmcneill 	pd->pd_bus = parent_bus;
    586  1.1  jmcneill 	pd->pd_tag = tag;
    587  1.1  jmcneill 	pd->pd_devno = devno;
    588  1.1  jmcneill 	pd->pd_funcno = funcno;
    589  1.1  jmcneill 	pd->pd_id = id;
    590  1.1  jmcneill 	pd->pd_class = pci_conf_read(pr->pr_pc, tag, PCI_CLASS_REG);
    591  1.1  jmcneill 	pd->pd_bhlc = pci_conf_read(pr->pr_pc, tag, PCI_BHLC_REG);
    592  1.1  jmcneill 
    593  1.1  jmcneill 	switch (PCI_HDRTYPE_TYPE(pd->pd_bhlc)) {
    594  1.1  jmcneill 	case PCI_HDRTYPE_DEVICE:
    595  1.1  jmcneill 		pci_resource_scan_bar(pr, pd, PCI_MAPREG_START,
    596  1.1  jmcneill 		    PCI_MAPREG_END, false);
    597  1.1  jmcneill 		break;
    598  1.1  jmcneill 	case PCI_HDRTYPE_PPB:
    599  1.1  jmcneill 		pci_resource_scan_bar(pr, pd, PCI_MAPREG_START,
    600  1.1  jmcneill 		    PCI_MAPREG_PPB_END, true);
    601  1.1  jmcneill 		pci_resource_scan_bridge(pr, pd);
    602  1.1  jmcneill 		break;
    603  1.1  jmcneill 	}
    604  1.1  jmcneill 
    605  1.1  jmcneill 	pci_resource_device_print(pr, pd);
    606  1.1  jmcneill 
    607  1.1  jmcneill 	if (PCI_HDRTYPE_TYPE(pd->pd_bhlc) == PCI_HDRTYPE_PPB &&
    608  1.1  jmcneill 	    PCI_CLASS(pd->pd_class) == PCI_CLASS_BRIDGE &&
    609  1.1  jmcneill 	    PCI_SUBCLASS(pd->pd_class) == PCI_SUBCLASS_BRIDGE_PCI) {
    610  1.1  jmcneill 		bridge_bus = pci_conf_read(pr->pr_pc, tag, PCI_BRIDGE_BUS_REG);
    611  1.1  jmcneill 		sec_bus = PCI_BRIDGE_BUS_NUM_SECONDARY(bridge_bus);
    612  1.1  jmcneill 		if (sec_bus <= pr->pr_endbus) {
    613  1.5  jmcneill 			if (pci_resource_scan_bus(pr, pd, sec_bus) != 0) {
    614  1.5  jmcneill 				DPRINT("PCI: " PCI_SBDF_FMT " bus %u "
    615  1.5  jmcneill 				       "already scanned (firmware bug!)\n",
    616  1.5  jmcneill 				       PCI_SBDF_FMT_ARGS(pr, pd), sec_bus);
    617  1.5  jmcneill 			}
    618  1.1  jmcneill 		}
    619  1.1  jmcneill 	}
    620  1.1  jmcneill 
    621  1.1  jmcneill 	return true;
    622  1.1  jmcneill }
    623  1.1  jmcneill 
    624  1.1  jmcneill /*
    625  1.1  jmcneill  * pci_resource_scan_bus --
    626  1.1  jmcneill  *
    627  1.1  jmcneill  *   Enumerate devices on a bus, recursively.
    628  1.1  jmcneill  */
    629  1.5  jmcneill static int
    630  1.1  jmcneill pci_resource_scan_bus(struct pci_resources *pr,
    631  1.1  jmcneill     struct pci_device *bridge_dev, uint8_t busno)
    632  1.1  jmcneill {
    633  1.1  jmcneill 	struct pci_bus *pb;
    634  1.1  jmcneill 	uint8_t devno, funcno;
    635  1.1  jmcneill 	uint8_t nfunc;
    636  1.1  jmcneill 
    637  1.1  jmcneill 	KASSERT(busno >= pr->pr_startbus);
    638  1.1  jmcneill 	KASSERT(busno <= pr->pr_endbus);
    639  1.1  jmcneill 
    640  1.1  jmcneill 	if (PCICONF_RES_BUS(pr, busno) != NULL) {
    641  1.1  jmcneill 		/*
    642  1.1  jmcneill 		 * Firmware has configured more than one bridge with the
    643  1.1  jmcneill 		 * same secondary bus number.
    644  1.1  jmcneill 		 */
    645  1.5  jmcneill 		return EINVAL;
    646  1.1  jmcneill 	}
    647  1.1  jmcneill 
    648  1.1  jmcneill 	pb = pci_new_bus(pr, busno, bridge_dev);
    649  1.1  jmcneill 	PCICONF_RES_BUS(pr, busno) = pb;
    650  1.1  jmcneill 
    651  1.1  jmcneill 	for (devno = 0; devno < PCI_MAX_DEVICE; devno++) {
    652  1.1  jmcneill 		if (!pci_resource_scan_device(pr, pb, devno, 0)) {
    653  1.1  jmcneill 			continue;
    654  1.1  jmcneill 		}
    655  1.1  jmcneill 		pb->pb_lastdevno = devno;
    656  1.1  jmcneill 
    657  1.1  jmcneill 		nfunc = pci_resource_device_functions(pr, busno, devno);
    658  1.1  jmcneill 		for (funcno = 1; funcno < nfunc; funcno++) {
    659  1.1  jmcneill 			pci_resource_scan_device(pr, pb, devno, funcno);
    660  1.1  jmcneill 		}
    661  1.1  jmcneill 	}
    662  1.5  jmcneill 
    663  1.5  jmcneill 	return 0;
    664  1.1  jmcneill }
    665  1.1  jmcneill 
    666  1.1  jmcneill /*
    667  1.1  jmcneill  * pci_resource_claim --
    668  1.1  jmcneill  *
    669  1.1  jmcneill  *   Claim a resource from a vmem arena. This is called to inform the
    670  1.1  jmcneill  *   resource manager about resources already configured by system firmware.
    671  1.1  jmcneill  */
    672  1.1  jmcneill static int
    673  1.6  riastrad pci_resource_claim(struct pci_resource_arena *arena,
    674  1.6  riastrad     vmem_addr_t start, vmem_addr_t end)
    675  1.1  jmcneill {
    676  1.1  jmcneill 	KASSERT(end >= start);
    677  1.1  jmcneill 
    678  1.6  riastrad 	return vmem_xalloc(arena->vmem, end - start + 1, 0, 0, 0, start, end,
    679  1.1  jmcneill 	    VM_BESTFIT | VM_NOSLEEP, NULL);
    680  1.1  jmcneill }
    681  1.1  jmcneill 
    682  1.1  jmcneill /*
    683  1.1  jmcneill  * pci_resource_alloc --
    684  1.1  jmcneill  *
    685  1.1  jmcneill  *   Allocate a resource from a vmem arena. This is called when configuring
    686  1.1  jmcneill  *   devices that were not already configured by system firmware.
    687  1.1  jmcneill  */
    688  1.3  riastrad static int
    689  1.6  riastrad pci_resource_alloc(struct pci_resource_arena *arena, vmem_size_t size,
    690  1.6  riastrad     vmem_size_t align,
    691  1.1  jmcneill     uint64_t *base)
    692  1.1  jmcneill {
    693  1.1  jmcneill 	vmem_addr_t addr;
    694  1.1  jmcneill 	int error;
    695  1.1  jmcneill 
    696  1.1  jmcneill 	KASSERT(size != 0);
    697  1.1  jmcneill 
    698  1.6  riastrad 	error = vmem_xalloc(arena->vmem, size, align, 0, 0, VMEM_ADDR_MIN,
    699  1.1  jmcneill 	    VMEM_ADDR_MAX, VM_BESTFIT | VM_NOSLEEP, &addr);
    700  1.1  jmcneill 	if (error == 0) {
    701  1.1  jmcneill 		*base = (uint64_t)addr;
    702  1.1  jmcneill 	}
    703  1.1  jmcneill 
    704  1.1  jmcneill 	return error;
    705  1.1  jmcneill }
    706  1.1  jmcneill 
    707  1.1  jmcneill /*
    708  1.1  jmcneill  * pci_resource_init_device --
    709  1.1  jmcneill  *
    710  1.1  jmcneill  *   Discover resources assigned by system firmware, notify the resource
    711  1.1  jmcneill  *   manager of these ranges, and determine if the device has additional
    712  1.1  jmcneill  *   resources that need to be allocated.
    713  1.1  jmcneill  */
    714  1.1  jmcneill static void
    715  1.1  jmcneill pci_resource_init_device(struct pci_resources *pr,
    716  1.1  jmcneill     struct pci_device *pd)
    717  1.1  jmcneill {
    718  1.1  jmcneill 	struct pci_iores *pi;
    719  1.1  jmcneill 	struct pci_bus *pb = pd->pd_bus;
    720  1.6  riastrad 	struct pci_resource_arena *res_io = pb->pb_res[PCI_RANGE_IO];
    721  1.6  riastrad 	struct pci_resource_arena *res_mem = pb->pb_res[PCI_RANGE_MEM];
    722  1.6  riastrad 	struct pci_resource_arena *res_pmem = pb->pb_res[PCI_RANGE_PMEM];
    723  1.1  jmcneill 	pcireg_t cmd;
    724  1.1  jmcneill 	u_int enabled, required;
    725  1.1  jmcneill 	u_int iores;
    726  1.1  jmcneill 	int error;
    727  1.1  jmcneill 
    728  1.1  jmcneill 	KASSERT(pd->pd_present);
    729  1.1  jmcneill 
    730  1.1  jmcneill 	if (IS_TEST_DEVICE(pd)) {
    731  1.1  jmcneill 		cmd = pci_conf_read(pr->pr_pc, pd->pd_tag,
    732  1.1  jmcneill 		    PCI_COMMAND_STATUS_REG);
    733  1.1  jmcneill 		cmd &= ~(PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_IO_ENABLE|
    734  1.1  jmcneill 			 PCI_COMMAND_MASTER_ENABLE);
    735  1.1  jmcneill 		pci_conf_write(pr->pr_pc, pd->pd_tag, PCI_COMMAND_STATUS_REG,
    736  1.1  jmcneill 		    cmd);
    737  1.1  jmcneill 	}
    738  1.1  jmcneill 
    739  1.1  jmcneill 	enabled = required = 0;
    740  1.1  jmcneill 	cmd = pci_conf_read(pr->pr_pc, pd->pd_tag, PCI_COMMAND_STATUS_REG);
    741  1.1  jmcneill 	if ((cmd & PCI_COMMAND_MEM_ENABLE) != 0) {
    742  1.1  jmcneill 		enabled |= __BIT(PCI_MAPREG_TYPE_MEM);
    743  1.1  jmcneill 	}
    744  1.1  jmcneill 	if ((cmd & PCI_COMMAND_IO_ENABLE) != 0) {
    745  1.1  jmcneill 		enabled |= __BIT(PCI_MAPREG_TYPE_IO);
    746  1.1  jmcneill 	}
    747  1.1  jmcneill 
    748  1.1  jmcneill 	for (iores = 0; iores < pd->pd_niores; iores++) {
    749  1.1  jmcneill 		pi = &pd->pd_iores[iores];
    750  1.1  jmcneill 
    751  1.1  jmcneill 		required |= __BIT(pi->pi_type);
    752  1.1  jmcneill 
    753  1.1  jmcneill 		if (IS_TEST_DEVICE(pd)) {
    754  1.1  jmcneill 			pci_conf_write(pr->pr_pc, pd->pd_tag,
    755  1.1  jmcneill 			    PCI_BAR(pi->pi_bar), 0);
    756  1.1  jmcneill 			continue;
    757  1.1  jmcneill 		}
    758  1.1  jmcneill 		if ((enabled & __BIT(pi->pi_type)) == 0) {
    759  1.1  jmcneill 			continue;
    760  1.1  jmcneill 		}
    761  1.1  jmcneill 
    762  1.1  jmcneill 		if (pi->pi_type == PCI_MAPREG_TYPE_IO) {
    763  1.1  jmcneill 			error = res_io == NULL ? ERANGE :
    764  1.1  jmcneill 			    pci_resource_claim(res_io, pi->pi_base,
    765  1.1  jmcneill 				pi->pi_base + pi->pi_size - 1);
    766  1.1  jmcneill 			if (error) {
    767  1.1  jmcneill 				DPRINT("PCI: " PCI_SBDF_FMT " [device] io "
    768  1.1  jmcneill 				       " %#" PRIx64 "-%#" PRIx64
    769  1.1  jmcneill 				       " invalid (%d)\n",
    770  1.1  jmcneill 				       PCI_SBDF_FMT_ARGS(pr, pd),
    771  1.1  jmcneill 				       pi->pi_base,
    772  1.1  jmcneill 				       pi->pi_base + pi->pi_size - 1,
    773  1.1  jmcneill 				       error);
    774  1.1  jmcneill 			}
    775  1.1  jmcneill 			continue;
    776  1.1  jmcneill 		}
    777  1.1  jmcneill 
    778  1.1  jmcneill 		KASSERT(pi->pi_type == PCI_MAPREG_TYPE_MEM);
    779  1.1  jmcneill 		error = ERANGE;
    780  1.4  jmcneill 		if (pi->pi_mem.prefetch) {
    781  1.4  jmcneill 			/*
    782  1.4  jmcneill 			 * Prefetchable memory must be allocated from the
    783  1.4  jmcneill 			 * bridge's prefetchable region.
    784  1.4  jmcneill 			 */
    785  1.4  jmcneill 			if (res_pmem != NULL) {
    786  1.4  jmcneill 				error = pci_resource_claim(res_pmem, pi->pi_base,
    787  1.4  jmcneill 				    pi->pi_base + pi->pi_size - 1);
    788  1.4  jmcneill 			}
    789  1.4  jmcneill 		} else if (pi->pi_mem.memtype == PCI_MAPREG_MEM_TYPE_64BIT) {
    790  1.4  jmcneill 			/*
    791  1.4  jmcneill 			 * Non-prefetchable 64-bit memory can be allocated from
    792  1.4  jmcneill 			 * any range. Prefer allocations from the prefetchable
    793  1.4  jmcneill 			 * region to save 32-bit only resources for 32-bit BARs.
    794  1.4  jmcneill 			 */
    795  1.4  jmcneill 			if (res_pmem != NULL) {
    796  1.4  jmcneill 				error = pci_resource_claim(res_pmem, pi->pi_base,
    797  1.4  jmcneill 				    pi->pi_base + pi->pi_size - 1);
    798  1.4  jmcneill 			}
    799  1.4  jmcneill 			if (error && res_mem != NULL) {
    800  1.4  jmcneill 				error = pci_resource_claim(res_mem, pi->pi_base,
    801  1.4  jmcneill 				    pi->pi_base + pi->pi_size - 1);
    802  1.4  jmcneill 			}
    803  1.4  jmcneill 		} else {
    804  1.4  jmcneill 			/*
    805  1.4  jmcneill 			 * Non-prefetchable 32-bit memory can be allocated from
    806  1.4  jmcneill 			 * any range, provided that the range is below 4GB. Try
    807  1.4  jmcneill 			 * the non-prefetchable range first, and if that fails,
    808  1.4  jmcneill 			 * make one last attempt at allocating from the
    809  1.4  jmcneill 			 * prefetchable range in case the platform provides
    810  1.4  jmcneill 			 * memory below 4GB.
    811  1.4  jmcneill 			 */
    812  1.4  jmcneill 			if (res_mem != NULL) {
    813  1.4  jmcneill 				error = pci_resource_claim(res_mem, pi->pi_base,
    814  1.4  jmcneill 				    pi->pi_base + pi->pi_size - 1);
    815  1.4  jmcneill 			}
    816  1.4  jmcneill 			if (error && res_pmem != NULL) {
    817  1.4  jmcneill 				error = pci_resource_claim(res_pmem, pi->pi_base,
    818  1.4  jmcneill 				    pi->pi_base + pi->pi_size - 1);
    819  1.4  jmcneill 			}
    820  1.1  jmcneill 		}
    821  1.1  jmcneill 		if (error) {
    822  1.1  jmcneill 			DPRINT("PCI: " PCI_SBDF_FMT " [device] mem"
    823  1.1  jmcneill 			       " (%sprefetchable)"
    824  1.1  jmcneill 			       " %#" PRIx64 "-%#" PRIx64
    825  1.1  jmcneill 			       " invalid (%d)\n",
    826  1.1  jmcneill 			       PCI_SBDF_FMT_ARGS(pr, pd),
    827  1.1  jmcneill 			       pi->pi_mem.prefetch ? "" : "non-",
    828  1.1  jmcneill 			       pi->pi_base,
    829  1.1  jmcneill 			       pi->pi_base + pi->pi_size - 1,
    830  1.1  jmcneill 			       error);
    831  1.1  jmcneill 		}
    832  1.1  jmcneill 	}
    833  1.1  jmcneill 
    834  1.1  jmcneill 	pd->pd_configured = (enabled & required) == required;
    835  1.1  jmcneill 
    836  1.1  jmcneill 	if (!pd->pd_configured) {
    837  1.1  jmcneill 		DPRINT("PCI: " PCI_SBDF_FMT " [device] "
    838  1.1  jmcneill 		       "not configured by firmware\n",
    839  1.1  jmcneill 		       PCI_SBDF_FMT_ARGS(pr, pd));
    840  1.1  jmcneill 	}
    841  1.1  jmcneill }
    842  1.1  jmcneill 
    843  1.1  jmcneill /*
    844  1.1  jmcneill  * pci_resource_init_bus --
    845  1.1  jmcneill  *
    846  1.1  jmcneill  *   Discover resources in use on a given bus, recursively.
    847  1.1  jmcneill  */
    848  1.1  jmcneill static void
    849  1.1  jmcneill pci_resource_init_bus(struct pci_resources *pr, uint8_t busno)
    850  1.1  jmcneill {
    851  1.1  jmcneill 	struct pci_bus *pb, *parent_bus;
    852  1.1  jmcneill 	struct pci_device *pd, *bridge;
    853  1.1  jmcneill 	uint8_t devno, funcno;
    854  1.1  jmcneill 	uint8_t nfunc;
    855  1.1  jmcneill 	int error;
    856  1.1  jmcneill 
    857  1.1  jmcneill 	KASSERT(busno >= pr->pr_startbus);
    858  1.1  jmcneill 	KASSERT(busno <= pr->pr_endbus);
    859  1.1  jmcneill 
    860  1.1  jmcneill 	pb = PCICONF_RES_BUS(pr, busno);
    861  1.1  jmcneill 	bridge = pb->pb_bridge;
    862  1.1  jmcneill 
    863  1.1  jmcneill 	KASSERT(pb != NULL);
    864  1.1  jmcneill 	KASSERT((busno == pr->pr_startbus) == (bridge == NULL));
    865  1.1  jmcneill 
    866  1.1  jmcneill 	if (bridge == NULL) {
    867  1.1  jmcneill 		/* Use resources provided by firmware. */
    868  1.1  jmcneill 		PCI_RANGE_FOREACH(prtype) {
    869  1.6  riastrad 			pb->pb_res[prtype] = pr->pr_ranges[prtype];
    870  1.6  riastrad 			pr->pr_ranges[prtype] = NULL;
    871  1.1  jmcneill 		}
    872  1.1  jmcneill 	} else {
    873  1.1  jmcneill 		/*
    874  1.1  jmcneill 		 * Using the resources configured in to the bridge by
    875  1.1  jmcneill 		 * firmware, claim the resources on the parent bus and
    876  1.1  jmcneill 		 * create a new vmem arena for the secondary bus.
    877  1.1  jmcneill 		 */
    878  1.1  jmcneill 		KASSERT(bridge->pd_bus != NULL);
    879  1.1  jmcneill 		parent_bus = bridge->pd_bus;
    880  1.1  jmcneill 		PCI_RANGE_FOREACH(prtype) {
    881  1.6  riastrad 			struct pci_resource_range *range;
    882  1.6  riastrad 
    883  1.1  jmcneill 			if (parent_bus->pb_res[prtype] == NULL ||
    884  1.6  riastrad 			    bridge->pd_bridge.ranges[prtype] == NULL) {
    885  1.1  jmcneill 				continue;
    886  1.1  jmcneill 			}
    887  1.6  riastrad 			SLIST_FOREACH(range,
    888  1.6  riastrad 			    &bridge->pd_bridge.ranges[prtype]->list,
    889  1.6  riastrad 			    entry) {
    890  1.6  riastrad 				error = pci_resource_claim(
    891  1.6  riastrad 				    parent_bus->pb_res[prtype],
    892  1.6  riastrad 				    range->start, range->end);
    893  1.6  riastrad 				if (error) {
    894  1.6  riastrad 					DPRINT("PCI: " PCI_SBDF_FMT
    895  1.6  riastrad 					    " bridge (bus %u)"
    896  1.6  riastrad 					    " %-4s %#" PRIx64 "-%#" PRIx64
    897  1.6  riastrad 					    " invalid\n",
    898  1.6  riastrad 					    PCI_SBDF_FMT_ARGS(pr, bridge),
    899  1.6  riastrad 					    busno,
    900  1.6  riastrad 					    pci_resource_typename(prtype),
    901  1.6  riastrad 					    range->start, range->end);
    902  1.6  riastrad 					continue;
    903  1.6  riastrad 				}
    904  1.6  riastrad 				pci_resource_arena_add_range(
    905  1.6  riastrad 				    pb->pb_res, prtype,
    906  1.6  riastrad 				    range->start, range->end);
    907  1.1  jmcneill 				KASSERT(pb->pb_res[prtype] != NULL);
    908  1.1  jmcneill 			}
    909  1.1  jmcneill 		}
    910  1.1  jmcneill 	}
    911  1.1  jmcneill 
    912  1.1  jmcneill 	for (devno = 0; devno <= pb->pb_lastdevno; devno++) {
    913  1.1  jmcneill 		KASSERT(devno < PCI_MAX_DEVICE);
    914  1.1  jmcneill 		nfunc = pci_resource_device_functions(pr, busno, devno);
    915  1.1  jmcneill 		for (funcno = 0; funcno < nfunc; funcno++) {
    916  1.1  jmcneill 			pd = PCICONF_BUS_DEVICE(pb, devno, funcno);
    917  1.1  jmcneill 			if (!pd->pd_present) {
    918  1.1  jmcneill 				continue;
    919  1.1  jmcneill 			}
    920  1.1  jmcneill 			if (pd->pd_ppb) {
    921  1.1  jmcneill 				uint8_t sec_bus = PCI_BRIDGE_BUS_NUM_SECONDARY(
    922  1.1  jmcneill 				    pd->pd_bridge.bridge_bus);
    923  1.1  jmcneill 				pci_resource_init_bus(pr, sec_bus);
    924  1.1  jmcneill 			}
    925  1.1  jmcneill 			pci_resource_init_device(pr, pd);
    926  1.1  jmcneill 		}
    927  1.1  jmcneill 	}
    928  1.1  jmcneill }
    929  1.1  jmcneill 
    930  1.1  jmcneill /*
    931  1.1  jmcneill  * pci_resource_probe --
    932  1.1  jmcneill  *
    933  1.1  jmcneill  *   Scan for PCI devices and initialize the resource manager.
    934  1.1  jmcneill  */
    935  1.1  jmcneill static void
    936  1.1  jmcneill pci_resource_probe(struct pci_resources *pr,
    937  1.1  jmcneill     const struct pci_resource_info *info)
    938  1.1  jmcneill {
    939  1.6  riastrad 	struct pci_resource_arena *busarena = info->ranges[PCI_RANGE_BUS];
    940  1.6  riastrad 	struct pci_resource_range *busrange = SLIST_FIRST(&busarena->list);
    941  1.6  riastrad 	uint8_t startbus = (uint8_t)busrange->start;
    942  1.6  riastrad 	uint8_t endbus = (uint8_t)busrange->end;
    943  1.1  jmcneill 	u_int nbus;
    944  1.1  jmcneill 
    945  1.1  jmcneill 	KASSERT(startbus <= endbus);
    946  1.1  jmcneill 	KASSERT(pr->pr_bus == NULL);
    947  1.1  jmcneill 
    948  1.1  jmcneill 	nbus = endbus - startbus + 1;
    949  1.1  jmcneill 
    950  1.1  jmcneill 	pr->pr_pc = info->pc;
    951  1.1  jmcneill 	pr->pr_startbus = startbus;
    952  1.1  jmcneill 	pr->pr_endbus = endbus;
    953  1.1  jmcneill 	pr->pr_bus = kmem_zalloc(nbus * sizeof(struct pci_bus *), KM_SLEEP);
    954  1.1  jmcneill 	memcpy(pr->pr_ranges, info->ranges, sizeof(pr->pr_ranges));
    955  1.1  jmcneill 
    956  1.1  jmcneill 	/* Scan devices */
    957  1.1  jmcneill 	pci_resource_scan_bus(pr, NULL, pr->pr_startbus);
    958  1.1  jmcneill 
    959  1.1  jmcneill 	/*
    960  1.1  jmcneill 	 * Create per-bus resource pools and remove ranges that are already
    961  1.1  jmcneill 	 * in use by devices and downstream bridges.
    962  1.1  jmcneill 	 */
    963  1.1  jmcneill 	pci_resource_init_bus(pr, pr->pr_startbus);
    964  1.1  jmcneill }
    965  1.1  jmcneill 
    966  1.1  jmcneill /*
    967  1.1  jmcneill  * pci_resource_alloc_device --
    968  1.1  jmcneill  *
    969  1.1  jmcneill  *   Attempt to allocate resources for a given device.
    970  1.1  jmcneill  */
    971  1.1  jmcneill static void
    972  1.1  jmcneill pci_resource_alloc_device(struct pci_resources *pr, struct pci_device *pd)
    973  1.1  jmcneill {
    974  1.1  jmcneill 	struct pci_iores *pi;
    975  1.6  riastrad 	struct pci_resource_arena *arena;
    976  1.1  jmcneill 	pcireg_t cmd, ocmd, base;
    977  1.1  jmcneill 	uint64_t addr;
    978  1.1  jmcneill 	u_int enabled;
    979  1.1  jmcneill 	u_int res;
    980  1.1  jmcneill 	u_int align;
    981  1.1  jmcneill 	int error;
    982  1.1  jmcneill 
    983  1.1  jmcneill 	enabled = 0;
    984  1.1  jmcneill 	ocmd = cmd = pci_conf_read(pr->pr_pc, pd->pd_tag,
    985  1.1  jmcneill 	    PCI_COMMAND_STATUS_REG);
    986  1.1  jmcneill 	if ((cmd & PCI_COMMAND_MEM_ENABLE) != 0) {
    987  1.1  jmcneill 		enabled |= __BIT(PCI_MAPREG_TYPE_MEM);
    988  1.1  jmcneill 	}
    989  1.1  jmcneill 	if ((cmd & PCI_COMMAND_IO_ENABLE) != 0) {
    990  1.1  jmcneill 		enabled |= __BIT(PCI_MAPREG_TYPE_IO);
    991  1.1  jmcneill 	}
    992  1.1  jmcneill 
    993  1.1  jmcneill 	for (res = 0; res < pd->pd_niores; res++) {
    994  1.1  jmcneill 		pi = &pd->pd_iores[res];
    995  1.1  jmcneill 
    996  1.1  jmcneill 		if ((enabled & __BIT(pi->pi_type)) != 0) {
    997  1.1  jmcneill 			continue;
    998  1.1  jmcneill 		}
    999  1.1  jmcneill 
   1000  1.1  jmcneill 		if (pi->pi_type == PCI_MAPREG_TYPE_IO) {
   1001  1.1  jmcneill 			arena = pd->pd_bus->pb_res[PCI_RANGE_IO];
   1002  1.1  jmcneill 			align = uimax(pi->pi_size, 4);
   1003  1.1  jmcneill 		} else {
   1004  1.1  jmcneill 			KASSERT(pi->pi_type == PCI_MAPREG_TYPE_MEM);
   1005  1.1  jmcneill 			arena = NULL;
   1006  1.1  jmcneill 			align = uimax(pi->pi_size, 16);
   1007  1.1  jmcneill 			if (pi->pi_mem.prefetch) {
   1008  1.1  jmcneill 				arena = pd->pd_bus->pb_res[PCI_RANGE_PMEM];
   1009  1.1  jmcneill 			}
   1010  1.1  jmcneill 			if (arena == NULL) {
   1011  1.1  jmcneill 				arena = pd->pd_bus->pb_res[PCI_RANGE_MEM];
   1012  1.1  jmcneill 			}
   1013  1.1  jmcneill 		}
   1014  1.1  jmcneill 		if (arena == NULL) {
   1015  1.1  jmcneill 			DPRINT("PCI: " PCI_SBDF_FMT " BAR%u failed to"
   1016  1.1  jmcneill 			       " allocate %#" PRIx64 " bytes (no arena)\n",
   1017  1.1  jmcneill 			       PCI_SBDF_FMT_ARGS(pr, pd),
   1018  1.1  jmcneill 			       pi->pi_bar, pi->pi_size);
   1019  1.1  jmcneill 			return;
   1020  1.1  jmcneill 		}
   1021  1.1  jmcneill 		error = pci_resource_alloc(arena, pi->pi_size, align, &addr);
   1022  1.1  jmcneill 		if (error != 0) {
   1023  1.1  jmcneill 			DPRINT("PCI: " PCI_SBDF_FMT " BAR%u failed to"
   1024  1.1  jmcneill 			       " allocate %#" PRIx64 " bytes (no space)\n",
   1025  1.1  jmcneill 			       PCI_SBDF_FMT_ARGS(pr, pd),
   1026  1.1  jmcneill 			       pi->pi_bar, pi->pi_size);
   1027  1.1  jmcneill 			return;
   1028  1.1  jmcneill 		}
   1029  1.1  jmcneill 		DPRINT("PCI: " PCI_SBDF_FMT " BAR%u assigned range"
   1030  1.6  riastrad 		       " %#" PRIx64 "-%#" PRIx64 "\n",
   1031  1.1  jmcneill 		       PCI_SBDF_FMT_ARGS(pr, pd),
   1032  1.1  jmcneill 		       pi->pi_bar, addr, addr + pi->pi_size - 1);
   1033  1.1  jmcneill 
   1034  1.1  jmcneill 		if (pi->pi_type == PCI_MAPREG_TYPE_IO) {
   1035  1.1  jmcneill 			cmd |= PCI_COMMAND_IO_ENABLE;
   1036  1.1  jmcneill 			pci_conf_write(pr->pr_pc, pd->pd_tag,
   1037  1.1  jmcneill 			    PCI_BAR(pi->pi_bar),
   1038  1.1  jmcneill 			    PCI_MAPREG_IO_ADDR(addr) | PCI_MAPREG_TYPE_IO);
   1039  1.1  jmcneill 		} else {
   1040  1.1  jmcneill 			cmd |= PCI_COMMAND_MEM_ENABLE;
   1041  1.1  jmcneill 			base = pci_conf_read(pr->pr_pc, pd->pd_tag,
   1042  1.1  jmcneill 			    PCI_BAR(pi->pi_bar));
   1043  1.1  jmcneill 			base = PCI_MAPREG_MEM_ADDR(addr) |
   1044  1.1  jmcneill 			    PCI_MAPREG_MEM_TYPE(base);
   1045  1.1  jmcneill 			pci_conf_write(pr->pr_pc, pd->pd_tag,
   1046  1.1  jmcneill 			    PCI_BAR(pi->pi_bar), base);
   1047  1.1  jmcneill 			if (pi->pi_mem.memtype == PCI_MAPREG_MEM_TYPE_64BIT) {
   1048  1.3  riastrad 				base = (pcireg_t)
   1049  1.3  riastrad 				    (PCI_MAPREG_MEM64_ADDR(addr) >> 32);
   1050  1.3  riastrad 				pci_conf_write(pr->pr_pc, pd->pd_tag,
   1051  1.1  jmcneill 				    PCI_BAR(pi->pi_bar + 1), base);
   1052  1.1  jmcneill 			}
   1053  1.1  jmcneill 		}
   1054  1.1  jmcneill 	}
   1055  1.1  jmcneill 
   1056  1.1  jmcneill 	if (ocmd != cmd) {
   1057  1.1  jmcneill 		pci_conf_write(pr->pr_pc, pd->pd_tag,
   1058  1.1  jmcneill 		    PCI_COMMAND_STATUS_REG, cmd);
   1059  1.1  jmcneill 	}
   1060  1.1  jmcneill }
   1061  1.1  jmcneill 
   1062  1.1  jmcneill /*
   1063  1.1  jmcneill  * pci_resource_alloc_bus --
   1064  1.1  jmcneill  *
   1065  1.1  jmcneill  *   Attempt to assign resources to all devices on a given bus, recursively.
   1066  1.1  jmcneill  */
   1067  1.1  jmcneill static void
   1068  1.1  jmcneill pci_resource_alloc_bus(struct pci_resources *pr, uint8_t busno)
   1069  1.1  jmcneill {
   1070  1.1  jmcneill 	struct pci_bus *pb = PCICONF_RES_BUS(pr, busno);
   1071  1.1  jmcneill 	struct pci_device *pd;
   1072  1.1  jmcneill 	uint8_t devno, funcno;
   1073  1.1  jmcneill 
   1074  1.1  jmcneill 	for (devno = 0; devno <= pb->pb_lastdevno; devno++) {
   1075  1.1  jmcneill 		for (funcno = 0; funcno < 8; funcno++) {
   1076  1.1  jmcneill 			pd = PCICONF_BUS_DEVICE(pb, devno, funcno);
   1077  1.1  jmcneill 			if (!pd->pd_present) {
   1078  1.1  jmcneill 				if (funcno == 0) {
   1079  1.1  jmcneill 					break;
   1080  1.1  jmcneill 				}
   1081  1.1  jmcneill 				continue;
   1082  1.1  jmcneill 			}
   1083  1.1  jmcneill 			if (!pd->pd_configured) {
   1084  1.1  jmcneill 				pci_resource_alloc_device(pr, pd);
   1085  1.1  jmcneill 			}
   1086  1.1  jmcneill 			if (pd->pd_ppb) {
   1087  1.1  jmcneill 				uint8_t sec_bus = PCI_BRIDGE_BUS_NUM_SECONDARY(
   1088  1.1  jmcneill 				    pd->pd_bridge.bridge_bus);
   1089  1.1  jmcneill 				pci_resource_alloc_bus(pr, sec_bus);
   1090  1.1  jmcneill 			}
   1091  1.1  jmcneill 		}
   1092  1.1  jmcneill 	}
   1093  1.1  jmcneill }
   1094  1.1  jmcneill 
   1095  1.1  jmcneill /*
   1096  1.1  jmcneill  * pci_resource_init --
   1097  1.1  jmcneill  *
   1098  1.1  jmcneill  *   Public interface to PCI resource manager. Scans for available devices
   1099  1.1  jmcneill  *   and assigns resources.
   1100  1.1  jmcneill  */
   1101  1.1  jmcneill void
   1102  1.1  jmcneill pci_resource_init(const struct pci_resource_info *info)
   1103  1.1  jmcneill {
   1104  1.1  jmcneill 	struct pci_resources pr = {};
   1105  1.1  jmcneill 
   1106  1.6  riastrad 	if (info->ranges[PCI_RANGE_BUS] == NULL) {
   1107  1.6  riastrad 		aprint_error("PCI: no buses\n");
   1108  1.6  riastrad 		return;
   1109  1.6  riastrad 	}
   1110  1.6  riastrad 	KASSERT(!SLIST_EMPTY(&info->ranges[PCI_RANGE_BUS]->list));
   1111  1.1  jmcneill 	pci_resource_probe(&pr, info);
   1112  1.1  jmcneill 	pci_resource_alloc_bus(&pr, pr.pr_startbus);
   1113  1.1  jmcneill }
   1114  1.1  jmcneill 
   1115  1.1  jmcneill /*
   1116  1.1  jmcneill  * pci_resource_typename --
   1117  1.1  jmcneill  *
   1118  1.1  jmcneill  *   Return a string description of a PCI range type.
   1119  1.1  jmcneill  */
   1120  1.1  jmcneill const char *
   1121  1.1  jmcneill pci_resource_typename(enum pci_range_type prtype)
   1122  1.1  jmcneill {
   1123  1.1  jmcneill 	KASSERT(prtype < NUM_PCI_RANGES);
   1124  1.1  jmcneill 	return pci_range_typenames[prtype];
   1125  1.1  jmcneill }
   1126