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