Home | History | Annotate | Line # | Download | only in pci
pci_resource.c revision 1.2
      1  1.2  riastrad /* $NetBSD: pci_resource.c,v 1.2 2022/10/15 20:11:00 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.2  riastrad __KERNEL_RCSID(0, "$NetBSD: pci_resource.c,v 1.2 2022/10/15 20:11:00 riastradh 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.1  jmcneill static void	pci_resource_scan_bus(struct pci_resources *,
    140  1.1  jmcneill     					  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.1  jmcneill 			pci_resource_scan_bus(pr, pd, sec_bus);
    528  1.1  jmcneill 		}
    529  1.1  jmcneill 	}
    530  1.1  jmcneill 
    531  1.1  jmcneill 	return true;
    532  1.1  jmcneill }
    533  1.1  jmcneill 
    534  1.1  jmcneill /*
    535  1.1  jmcneill  * pci_resource_scan_bus --
    536  1.1  jmcneill  *
    537  1.1  jmcneill  *   Enumerate devices on a bus, recursively.
    538  1.1  jmcneill  */
    539  1.1  jmcneill static void
    540  1.1  jmcneill pci_resource_scan_bus(struct pci_resources *pr,
    541  1.1  jmcneill     struct pci_device *bridge_dev, uint8_t busno)
    542  1.1  jmcneill {
    543  1.1  jmcneill 	struct pci_bus *pb;
    544  1.1  jmcneill 	uint8_t devno, funcno;
    545  1.1  jmcneill 	uint8_t nfunc;
    546  1.1  jmcneill 
    547  1.1  jmcneill 	KASSERT(busno >= pr->pr_startbus);
    548  1.1  jmcneill 	KASSERT(busno <= pr->pr_endbus);
    549  1.1  jmcneill 
    550  1.1  jmcneill 	if (PCICONF_RES_BUS(pr, busno) != NULL) {
    551  1.1  jmcneill 		/*
    552  1.1  jmcneill 		 * Firmware has configured more than one bridge with the
    553  1.1  jmcneill 		 * same secondary bus number.
    554  1.1  jmcneill 		 */
    555  1.1  jmcneill 		panic("Bus %u already scanned (firmware bug!)", busno);
    556  1.1  jmcneill 		return;
    557  1.1  jmcneill 	}
    558  1.1  jmcneill 
    559  1.1  jmcneill 	pb = pci_new_bus(pr, busno, bridge_dev);
    560  1.1  jmcneill 	PCICONF_RES_BUS(pr, busno) = pb;
    561  1.1  jmcneill 
    562  1.1  jmcneill 	for (devno = 0; devno < PCI_MAX_DEVICE; devno++) {
    563  1.1  jmcneill 		if (!pci_resource_scan_device(pr, pb, devno, 0)) {
    564  1.1  jmcneill 			continue;
    565  1.1  jmcneill 		}
    566  1.1  jmcneill 		pb->pb_lastdevno = devno;
    567  1.1  jmcneill 
    568  1.1  jmcneill 		nfunc = pci_resource_device_functions(pr, busno, devno);
    569  1.1  jmcneill 		for (funcno = 1; funcno < nfunc; funcno++) {
    570  1.1  jmcneill 			pci_resource_scan_device(pr, pb, devno, funcno);
    571  1.1  jmcneill 		}
    572  1.1  jmcneill 	}
    573  1.1  jmcneill }
    574  1.1  jmcneill 
    575  1.1  jmcneill /*
    576  1.1  jmcneill  * pci_resource_claim --
    577  1.1  jmcneill  *
    578  1.1  jmcneill  *   Claim a resource from a vmem arena. This is called to inform the
    579  1.1  jmcneill  *   resource manager about resources already configured by system firmware.
    580  1.1  jmcneill  */
    581  1.1  jmcneill static int
    582  1.1  jmcneill pci_resource_claim(vmem_t *arena, vmem_addr_t start, vmem_addr_t end)
    583  1.1  jmcneill {
    584  1.1  jmcneill 	KASSERT(end >= start);
    585  1.1  jmcneill 
    586  1.1  jmcneill 	return vmem_xalloc(arena, end - start + 1, 0, 0, 0, start, end,
    587  1.1  jmcneill 	    VM_BESTFIT | VM_NOSLEEP, NULL);
    588  1.1  jmcneill }
    589  1.1  jmcneill 
    590  1.1  jmcneill /*
    591  1.1  jmcneill  * pci_resource_alloc --
    592  1.1  jmcneill  *
    593  1.1  jmcneill  *   Allocate a resource from a vmem arena. This is called when configuring
    594  1.1  jmcneill  *   devices that were not already configured by system firmware.
    595  1.1  jmcneill  */
    596  1.1  jmcneill static int
    597  1.1  jmcneill pci_resource_alloc(vmem_t *arena, vmem_size_t size, vmem_size_t align,
    598  1.1  jmcneill     uint64_t *base)
    599  1.1  jmcneill {
    600  1.1  jmcneill 	vmem_addr_t addr;
    601  1.1  jmcneill 	int error;
    602  1.1  jmcneill 
    603  1.1  jmcneill 	KASSERT(size != 0);
    604  1.1  jmcneill 
    605  1.1  jmcneill 	error = vmem_xalloc(arena, size, align, 0, 0, VMEM_ADDR_MIN,
    606  1.1  jmcneill 	    VMEM_ADDR_MAX, VM_BESTFIT | VM_NOSLEEP, &addr);
    607  1.1  jmcneill 	if (error == 0) {
    608  1.1  jmcneill 		*base = (uint64_t)addr;
    609  1.1  jmcneill 	}
    610  1.1  jmcneill 
    611  1.1  jmcneill 	return error;
    612  1.1  jmcneill }
    613  1.1  jmcneill 
    614  1.1  jmcneill /*
    615  1.1  jmcneill  * pci_resource_init_device --
    616  1.1  jmcneill  *
    617  1.1  jmcneill  *   Discover resources assigned by system firmware, notify the resource
    618  1.1  jmcneill  *   manager of these ranges, and determine if the device has additional
    619  1.1  jmcneill  *   resources that need to be allocated.
    620  1.1  jmcneill  */
    621  1.1  jmcneill static void
    622  1.1  jmcneill pci_resource_init_device(struct pci_resources *pr,
    623  1.1  jmcneill     struct pci_device *pd)
    624  1.1  jmcneill {
    625  1.1  jmcneill 	struct pci_iores *pi;
    626  1.1  jmcneill 	struct pci_bus *pb = pd->pd_bus;
    627  1.1  jmcneill 	vmem_t *res_io = pb->pb_res[PCI_RANGE_IO];
    628  1.1  jmcneill 	vmem_t *res_mem = pb->pb_res[PCI_RANGE_MEM];
    629  1.1  jmcneill 	vmem_t *res_pmem = pb->pb_res[PCI_RANGE_PMEM];
    630  1.1  jmcneill 	pcireg_t cmd;
    631  1.1  jmcneill 	u_int enabled, required;
    632  1.1  jmcneill 	u_int iores;
    633  1.1  jmcneill 	int error;
    634  1.1  jmcneill 
    635  1.1  jmcneill 	KASSERT(pd->pd_present);
    636  1.1  jmcneill 
    637  1.1  jmcneill 	if (IS_TEST_DEVICE(pd)) {
    638  1.1  jmcneill 		cmd = pci_conf_read(pr->pr_pc, pd->pd_tag,
    639  1.1  jmcneill 		    PCI_COMMAND_STATUS_REG);
    640  1.1  jmcneill 		cmd &= ~(PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_IO_ENABLE|
    641  1.1  jmcneill 			 PCI_COMMAND_MASTER_ENABLE);
    642  1.1  jmcneill 		pci_conf_write(pr->pr_pc, pd->pd_tag, PCI_COMMAND_STATUS_REG,
    643  1.1  jmcneill 		    cmd);
    644  1.1  jmcneill 	}
    645  1.1  jmcneill 
    646  1.1  jmcneill 	enabled = required = 0;
    647  1.1  jmcneill 	cmd = pci_conf_read(pr->pr_pc, pd->pd_tag, PCI_COMMAND_STATUS_REG);
    648  1.1  jmcneill 	if ((cmd & PCI_COMMAND_MEM_ENABLE) != 0) {
    649  1.1  jmcneill 		enabled |= __BIT(PCI_MAPREG_TYPE_MEM);
    650  1.1  jmcneill 	}
    651  1.1  jmcneill 	if ((cmd & PCI_COMMAND_IO_ENABLE) != 0) {
    652  1.1  jmcneill 		enabled |= __BIT(PCI_MAPREG_TYPE_IO);
    653  1.1  jmcneill 	}
    654  1.1  jmcneill 
    655  1.1  jmcneill 	for (iores = 0; iores < pd->pd_niores; iores++) {
    656  1.1  jmcneill 		pi = &pd->pd_iores[iores];
    657  1.1  jmcneill 
    658  1.1  jmcneill 		required |= __BIT(pi->pi_type);
    659  1.1  jmcneill 
    660  1.1  jmcneill 		if (IS_TEST_DEVICE(pd)) {
    661  1.1  jmcneill 			pci_conf_write(pr->pr_pc, pd->pd_tag,
    662  1.1  jmcneill 			    PCI_BAR(pi->pi_bar), 0);
    663  1.1  jmcneill 			continue;
    664  1.1  jmcneill 		}
    665  1.1  jmcneill 		if ((enabled & __BIT(pi->pi_type)) == 0) {
    666  1.1  jmcneill 			continue;
    667  1.1  jmcneill 		}
    668  1.1  jmcneill 
    669  1.1  jmcneill 		if (pi->pi_type == PCI_MAPREG_TYPE_IO) {
    670  1.1  jmcneill 			error = res_io == NULL ? ERANGE :
    671  1.1  jmcneill 			    pci_resource_claim(res_io, pi->pi_base,
    672  1.1  jmcneill 				pi->pi_base + pi->pi_size - 1);
    673  1.1  jmcneill 			if (error) {
    674  1.1  jmcneill 				DPRINT("PCI: " PCI_SBDF_FMT " [device] io "
    675  1.1  jmcneill 				       " %#" PRIx64 "-%#" PRIx64
    676  1.1  jmcneill 				       " invalid (%d)\n",
    677  1.1  jmcneill 				       PCI_SBDF_FMT_ARGS(pr, pd),
    678  1.1  jmcneill 				       pi->pi_base,
    679  1.1  jmcneill 				       pi->pi_base + pi->pi_size - 1,
    680  1.1  jmcneill 				       error);
    681  1.1  jmcneill 			}
    682  1.1  jmcneill 			continue;
    683  1.1  jmcneill 		}
    684  1.1  jmcneill 
    685  1.1  jmcneill 		KASSERT(pi->pi_type == PCI_MAPREG_TYPE_MEM);
    686  1.1  jmcneill 		error = ERANGE;
    687  1.1  jmcneill 		if (pi->pi_mem.prefetch && res_pmem != NULL) {
    688  1.1  jmcneill 			error = pci_resource_claim(res_pmem, pi->pi_base,
    689  1.1  jmcneill 			    pi->pi_base + pi->pi_size - 1);
    690  1.1  jmcneill 		}
    691  1.1  jmcneill 		if (error && res_mem != NULL) {
    692  1.1  jmcneill 			error = pci_resource_claim(res_mem, pi->pi_base,
    693  1.1  jmcneill 			    pi->pi_base + pi->pi_size - 1);
    694  1.1  jmcneill 		}
    695  1.1  jmcneill 		if (error) {
    696  1.1  jmcneill 			DPRINT("PCI: " PCI_SBDF_FMT " [device] mem"
    697  1.1  jmcneill 			       " (%sprefetchable)"
    698  1.1  jmcneill 			       " %#" PRIx64 "-%#" PRIx64
    699  1.1  jmcneill 			       " invalid (%d)\n",
    700  1.1  jmcneill 			       PCI_SBDF_FMT_ARGS(pr, pd),
    701  1.1  jmcneill 			       pi->pi_mem.prefetch ? "" : "non-",
    702  1.1  jmcneill 			       pi->pi_base,
    703  1.1  jmcneill 			       pi->pi_base + pi->pi_size - 1,
    704  1.1  jmcneill 			       error);
    705  1.1  jmcneill 		}
    706  1.1  jmcneill 	}
    707  1.1  jmcneill 
    708  1.1  jmcneill 	pd->pd_configured = (enabled & required) == required;
    709  1.1  jmcneill 
    710  1.1  jmcneill 	if (!pd->pd_configured) {
    711  1.1  jmcneill 		DPRINT("PCI: " PCI_SBDF_FMT " [device] "
    712  1.1  jmcneill 		       "not configured by firmware\n",
    713  1.1  jmcneill 		       PCI_SBDF_FMT_ARGS(pr, pd));
    714  1.1  jmcneill 	}
    715  1.1  jmcneill }
    716  1.1  jmcneill 
    717  1.1  jmcneill /*
    718  1.1  jmcneill  * pci_resource_init_bus --
    719  1.1  jmcneill  *
    720  1.1  jmcneill  *   Discover resources in use on a given bus, recursively.
    721  1.1  jmcneill  */
    722  1.1  jmcneill static void
    723  1.1  jmcneill pci_resource_init_bus(struct pci_resources *pr, uint8_t busno)
    724  1.1  jmcneill {
    725  1.1  jmcneill 	struct pci_bus *pb, *parent_bus;
    726  1.1  jmcneill 	struct pci_device *pd, *bridge;
    727  1.1  jmcneill 	uint8_t devno, funcno;
    728  1.1  jmcneill 	uint8_t nfunc;
    729  1.1  jmcneill 	int error;
    730  1.1  jmcneill 
    731  1.1  jmcneill 	KASSERT(busno >= pr->pr_startbus);
    732  1.1  jmcneill 	KASSERT(busno <= pr->pr_endbus);
    733  1.1  jmcneill 
    734  1.1  jmcneill 	pb = PCICONF_RES_BUS(pr, busno);
    735  1.1  jmcneill 	bridge = pb->pb_bridge;
    736  1.1  jmcneill 
    737  1.1  jmcneill 	KASSERT(pb != NULL);
    738  1.1  jmcneill 	KASSERT((busno == pr->pr_startbus) == (bridge == NULL));
    739  1.1  jmcneill 
    740  1.1  jmcneill 	if (bridge == NULL) {
    741  1.1  jmcneill 		/* Use resources provided by firmware. */
    742  1.1  jmcneill 		PCI_RANGE_FOREACH(prtype) {
    743  1.1  jmcneill 			pb->pb_res[prtype] = pr->pr_res[prtype];
    744  1.1  jmcneill 			pr->pr_res[prtype] = NULL;
    745  1.1  jmcneill 		}
    746  1.1  jmcneill 	} else {
    747  1.1  jmcneill 		/*
    748  1.1  jmcneill 		 * Using the resources configured in to the bridge by
    749  1.1  jmcneill 		 * firmware, claim the resources on the parent bus and
    750  1.1  jmcneill 		 * create a new vmem arena for the secondary bus.
    751  1.1  jmcneill 		 */
    752  1.1  jmcneill 		KASSERT(bridge->pd_bus != NULL);
    753  1.1  jmcneill 		parent_bus = bridge->pd_bus;
    754  1.1  jmcneill 		PCI_RANGE_FOREACH(prtype) {
    755  1.1  jmcneill 			if (parent_bus->pb_res[prtype] == NULL ||
    756  1.1  jmcneill 			    !bridge->pd_bridge.ranges[prtype].end) {
    757  1.1  jmcneill 				continue;
    758  1.1  jmcneill 			}
    759  1.1  jmcneill 			error = pci_resource_claim(
    760  1.1  jmcneill 			    parent_bus->pb_res[prtype],
    761  1.1  jmcneill 			    bridge->pd_bridge.ranges[prtype].start,
    762  1.1  jmcneill 			    bridge->pd_bridge.ranges[prtype].end);
    763  1.1  jmcneill 			if (error == 0) {
    764  1.1  jmcneill 				pb->pb_res[prtype] = pci_create_vmem(
    765  1.1  jmcneill 				    pci_resource_typename(prtype),
    766  1.1  jmcneill 				    bridge->pd_bridge.ranges[prtype].start,
    767  1.1  jmcneill 				    bridge->pd_bridge.ranges[prtype].end);
    768  1.1  jmcneill 				KASSERT(pb->pb_res[prtype] != NULL);
    769  1.1  jmcneill 			} else {
    770  1.1  jmcneill 				DPRINT("PCI: " PCI_SBDF_FMT " bridge (bus %u)"
    771  1.1  jmcneill 				       " %-4s %#" PRIx64 "-%#" PRIx64
    772  1.1  jmcneill 				       " invalid\n",
    773  1.1  jmcneill 				       PCI_SBDF_FMT_ARGS(pr, bridge), busno,
    774  1.1  jmcneill 				       pci_resource_typename(prtype),
    775  1.1  jmcneill 				       bridge->pd_bridge.ranges[prtype].start,
    776  1.1  jmcneill 				       bridge->pd_bridge.ranges[prtype].end);
    777  1.1  jmcneill 			}
    778  1.1  jmcneill 		}
    779  1.1  jmcneill 	}
    780  1.1  jmcneill 
    781  1.1  jmcneill 	for (devno = 0; devno <= pb->pb_lastdevno; devno++) {
    782  1.1  jmcneill 		KASSERT(devno < PCI_MAX_DEVICE);
    783  1.1  jmcneill 		nfunc = pci_resource_device_functions(pr, busno, devno);
    784  1.1  jmcneill 		for (funcno = 0; funcno < nfunc; funcno++) {
    785  1.1  jmcneill 			pd = PCICONF_BUS_DEVICE(pb, devno, funcno);
    786  1.1  jmcneill 			if (!pd->pd_present) {
    787  1.1  jmcneill 				continue;
    788  1.1  jmcneill 			}
    789  1.1  jmcneill 			if (pd->pd_ppb) {
    790  1.1  jmcneill 				uint8_t sec_bus = PCI_BRIDGE_BUS_NUM_SECONDARY(
    791  1.1  jmcneill 				    pd->pd_bridge.bridge_bus);
    792  1.1  jmcneill 				pci_resource_init_bus(pr, sec_bus);
    793  1.1  jmcneill 			}
    794  1.1  jmcneill 			pci_resource_init_device(pr, pd);
    795  1.1  jmcneill 		}
    796  1.1  jmcneill 	}
    797  1.1  jmcneill }
    798  1.1  jmcneill 
    799  1.1  jmcneill /*
    800  1.1  jmcneill  * pci_resource_probe --
    801  1.1  jmcneill  *
    802  1.1  jmcneill  *   Scan for PCI devices and initialize the resource manager.
    803  1.1  jmcneill  */
    804  1.1  jmcneill static void
    805  1.1  jmcneill pci_resource_probe(struct pci_resources *pr,
    806  1.1  jmcneill     const struct pci_resource_info *info)
    807  1.1  jmcneill {
    808  1.1  jmcneill 	uint8_t startbus = (uint8_t)info->ranges[PCI_RANGE_BUS].start;
    809  1.1  jmcneill 	uint8_t endbus = (uint8_t)info->ranges[PCI_RANGE_BUS].end;
    810  1.1  jmcneill 	u_int nbus;
    811  1.1  jmcneill 
    812  1.1  jmcneill 	KASSERT(startbus <= endbus);
    813  1.1  jmcneill 	KASSERT(pr->pr_bus == NULL);
    814  1.1  jmcneill 
    815  1.1  jmcneill 	nbus = endbus - startbus + 1;
    816  1.1  jmcneill 
    817  1.1  jmcneill 	pr->pr_pc = info->pc;
    818  1.1  jmcneill 	pr->pr_startbus = startbus;
    819  1.1  jmcneill 	pr->pr_endbus = endbus;
    820  1.1  jmcneill 	pr->pr_bus = kmem_zalloc(nbus * sizeof(struct pci_bus *), KM_SLEEP);
    821  1.1  jmcneill 	memcpy(pr->pr_ranges, info->ranges, sizeof(pr->pr_ranges));
    822  1.1  jmcneill 	PCI_RANGE_FOREACH(prtype) {
    823  1.1  jmcneill 		if (prtype == PCI_RANGE_BUS || info->ranges[prtype].end) {
    824  1.1  jmcneill 			pr->pr_res[prtype] = pci_create_vmem(
    825  1.1  jmcneill 			    pci_resource_typename(prtype),
    826  1.1  jmcneill 			    info->ranges[prtype].start,
    827  1.1  jmcneill 			    info->ranges[prtype].end);
    828  1.1  jmcneill 			KASSERT(pr->pr_res[prtype] != NULL);
    829  1.1  jmcneill 		}
    830  1.1  jmcneill 	}
    831  1.1  jmcneill 
    832  1.1  jmcneill 	/* Scan devices */
    833  1.1  jmcneill 	pci_resource_scan_bus(pr, NULL, pr->pr_startbus);
    834  1.1  jmcneill 
    835  1.1  jmcneill 	/*
    836  1.1  jmcneill 	 * Create per-bus resource pools and remove ranges that are already
    837  1.1  jmcneill 	 * in use by devices and downstream bridges.
    838  1.1  jmcneill 	 */
    839  1.1  jmcneill 	pci_resource_init_bus(pr, pr->pr_startbus);
    840  1.1  jmcneill }
    841  1.1  jmcneill 
    842  1.1  jmcneill /*
    843  1.1  jmcneill  * pci_resource_alloc_device --
    844  1.1  jmcneill  *
    845  1.1  jmcneill  *   Attempt to allocate resources for a given device.
    846  1.1  jmcneill  */
    847  1.1  jmcneill static void
    848  1.1  jmcneill pci_resource_alloc_device(struct pci_resources *pr, struct pci_device *pd)
    849  1.1  jmcneill {
    850  1.1  jmcneill 	struct pci_iores *pi;
    851  1.1  jmcneill 	vmem_t *arena;
    852  1.1  jmcneill 	pcireg_t cmd, ocmd, base;
    853  1.1  jmcneill 	uint64_t addr;
    854  1.1  jmcneill 	u_int enabled;
    855  1.1  jmcneill 	u_int res;
    856  1.1  jmcneill 	u_int align;
    857  1.1  jmcneill 	int error;
    858  1.1  jmcneill 
    859  1.1  jmcneill 	enabled = 0;
    860  1.1  jmcneill 	ocmd = cmd = pci_conf_read(pr->pr_pc, pd->pd_tag,
    861  1.1  jmcneill 	    PCI_COMMAND_STATUS_REG);
    862  1.1  jmcneill 	if ((cmd & PCI_COMMAND_MEM_ENABLE) != 0) {
    863  1.1  jmcneill 		enabled |= __BIT(PCI_MAPREG_TYPE_MEM);
    864  1.1  jmcneill 	}
    865  1.1  jmcneill 	if ((cmd & PCI_COMMAND_IO_ENABLE) != 0) {
    866  1.1  jmcneill 		enabled |= __BIT(PCI_MAPREG_TYPE_IO);
    867  1.1  jmcneill 	}
    868  1.1  jmcneill 
    869  1.1  jmcneill 	for (res = 0; res < pd->pd_niores; res++) {
    870  1.1  jmcneill 		pi = &pd->pd_iores[res];
    871  1.1  jmcneill 
    872  1.1  jmcneill 		if ((enabled & __BIT(pi->pi_type)) != 0) {
    873  1.1  jmcneill 			continue;
    874  1.1  jmcneill 		}
    875  1.1  jmcneill 
    876  1.1  jmcneill 		if (pi->pi_type == PCI_MAPREG_TYPE_IO) {
    877  1.1  jmcneill 			arena = pd->pd_bus->pb_res[PCI_RANGE_IO];
    878  1.1  jmcneill 			align = uimax(pi->pi_size, 4);
    879  1.1  jmcneill 		} else {
    880  1.1  jmcneill 			KASSERT(pi->pi_type == PCI_MAPREG_TYPE_MEM);
    881  1.1  jmcneill 			arena = NULL;
    882  1.1  jmcneill 			align = uimax(pi->pi_size, 16);
    883  1.1  jmcneill 			if (pi->pi_mem.prefetch) {
    884  1.1  jmcneill 				arena = pd->pd_bus->pb_res[PCI_RANGE_PMEM];
    885  1.1  jmcneill 			}
    886  1.1  jmcneill 			if (arena == NULL) {
    887  1.1  jmcneill 				arena = pd->pd_bus->pb_res[PCI_RANGE_MEM];
    888  1.1  jmcneill 			}
    889  1.1  jmcneill 		}
    890  1.1  jmcneill 		if (arena == NULL) {
    891  1.1  jmcneill 			DPRINT("PCI: " PCI_SBDF_FMT " BAR%u failed to"
    892  1.1  jmcneill 			       " allocate %#" PRIx64 " bytes (no arena)\n",
    893  1.1  jmcneill 			       PCI_SBDF_FMT_ARGS(pr, pd),
    894  1.1  jmcneill 			       pi->pi_bar, pi->pi_size);
    895  1.1  jmcneill 			return;
    896  1.1  jmcneill 		}
    897  1.1  jmcneill 		error = pci_resource_alloc(arena, pi->pi_size, align, &addr);
    898  1.1  jmcneill 		if (error != 0) {
    899  1.1  jmcneill 			DPRINT("PCI: " PCI_SBDF_FMT " BAR%u failed to"
    900  1.1  jmcneill 			       " allocate %#" PRIx64 " bytes (no space)\n",
    901  1.1  jmcneill 			       PCI_SBDF_FMT_ARGS(pr, pd),
    902  1.1  jmcneill 			       pi->pi_bar, pi->pi_size);
    903  1.1  jmcneill 			return;
    904  1.1  jmcneill 		}
    905  1.1  jmcneill 		DPRINT("PCI: " PCI_SBDF_FMT " BAR%u assigned range"
    906  1.1  jmcneill 		       " 0x%#" PRIx64 "-0x%#" PRIx64 "\n",
    907  1.1  jmcneill 		       PCI_SBDF_FMT_ARGS(pr, pd),
    908  1.1  jmcneill 		       pi->pi_bar, addr, addr + pi->pi_size - 1);
    909  1.1  jmcneill 
    910  1.1  jmcneill 		if (pi->pi_type == PCI_MAPREG_TYPE_IO) {
    911  1.1  jmcneill 			cmd |= PCI_COMMAND_IO_ENABLE;
    912  1.1  jmcneill 			pci_conf_write(pr->pr_pc, pd->pd_tag,
    913  1.1  jmcneill 			    PCI_BAR(pi->pi_bar),
    914  1.1  jmcneill 			    PCI_MAPREG_IO_ADDR(addr) | PCI_MAPREG_TYPE_IO);
    915  1.1  jmcneill 		} else {
    916  1.1  jmcneill 			cmd |= PCI_COMMAND_MEM_ENABLE;
    917  1.1  jmcneill 			base = pci_conf_read(pr->pr_pc, pd->pd_tag,
    918  1.1  jmcneill 			    PCI_BAR(pi->pi_bar));
    919  1.1  jmcneill 			base = PCI_MAPREG_MEM_ADDR(addr) |
    920  1.1  jmcneill 			    PCI_MAPREG_MEM_TYPE(base);
    921  1.1  jmcneill 			pci_conf_write(pr->pr_pc, pd->pd_tag,
    922  1.1  jmcneill 			    PCI_BAR(pi->pi_bar), base);
    923  1.1  jmcneill 			if (pi->pi_mem.memtype == PCI_MAPREG_MEM_TYPE_64BIT) {
    924  1.1  jmcneill                                 base = (pcireg_t)
    925  1.1  jmcneill                                     (PCI_MAPREG_MEM64_ADDR(addr) >> 32);
    926  1.1  jmcneill                                 pci_conf_write(pr->pr_pc, pd->pd_tag,
    927  1.1  jmcneill 				    PCI_BAR(pi->pi_bar + 1), base);
    928  1.1  jmcneill 			}
    929  1.1  jmcneill 		}
    930  1.1  jmcneill 	}
    931  1.1  jmcneill 
    932  1.1  jmcneill 	if (ocmd != cmd) {
    933  1.1  jmcneill 		pci_conf_write(pr->pr_pc, pd->pd_tag,
    934  1.1  jmcneill 		    PCI_COMMAND_STATUS_REG, cmd);
    935  1.1  jmcneill 	}
    936  1.1  jmcneill }
    937  1.1  jmcneill 
    938  1.1  jmcneill /*
    939  1.1  jmcneill  * pci_resource_alloc_bus --
    940  1.1  jmcneill  *
    941  1.1  jmcneill  *   Attempt to assign resources to all devices on a given bus, recursively.
    942  1.1  jmcneill  */
    943  1.1  jmcneill static void
    944  1.1  jmcneill pci_resource_alloc_bus(struct pci_resources *pr, uint8_t busno)
    945  1.1  jmcneill {
    946  1.1  jmcneill 	struct pci_bus *pb = PCICONF_RES_BUS(pr, busno);
    947  1.1  jmcneill 	struct pci_device *pd;
    948  1.1  jmcneill 	uint8_t devno, funcno;
    949  1.1  jmcneill 
    950  1.1  jmcneill 	for (devno = 0; devno <= pb->pb_lastdevno; devno++) {
    951  1.1  jmcneill 		for (funcno = 0; funcno < 8; funcno++) {
    952  1.1  jmcneill 			pd = PCICONF_BUS_DEVICE(pb, devno, funcno);
    953  1.1  jmcneill 			if (!pd->pd_present) {
    954  1.1  jmcneill 				if (funcno == 0) {
    955  1.1  jmcneill 					break;
    956  1.1  jmcneill 				}
    957  1.1  jmcneill 				continue;
    958  1.1  jmcneill 			}
    959  1.1  jmcneill 			if (!pd->pd_configured) {
    960  1.1  jmcneill 				pci_resource_alloc_device(pr, pd);
    961  1.1  jmcneill 			}
    962  1.1  jmcneill 			if (pd->pd_ppb) {
    963  1.1  jmcneill 				uint8_t sec_bus = PCI_BRIDGE_BUS_NUM_SECONDARY(
    964  1.1  jmcneill 				    pd->pd_bridge.bridge_bus);
    965  1.1  jmcneill 				pci_resource_alloc_bus(pr, sec_bus);
    966  1.1  jmcneill 			}
    967  1.1  jmcneill 		}
    968  1.1  jmcneill 	}
    969  1.1  jmcneill }
    970  1.1  jmcneill 
    971  1.1  jmcneill /*
    972  1.1  jmcneill  * pci_resource_init --
    973  1.1  jmcneill  *
    974  1.1  jmcneill  *   Public interface to PCI resource manager. Scans for available devices
    975  1.1  jmcneill  *   and assigns resources.
    976  1.1  jmcneill  */
    977  1.1  jmcneill void
    978  1.1  jmcneill pci_resource_init(const struct pci_resource_info *info)
    979  1.1  jmcneill {
    980  1.1  jmcneill 	struct pci_resources pr = {};
    981  1.1  jmcneill 
    982  1.1  jmcneill 	pci_resource_probe(&pr, info);
    983  1.1  jmcneill 	pci_resource_alloc_bus(&pr, pr.pr_startbus);
    984  1.1  jmcneill }
    985  1.1  jmcneill 
    986  1.1  jmcneill /*
    987  1.1  jmcneill  * pci_resource_typename --
    988  1.1  jmcneill  *
    989  1.1  jmcneill  *   Return a string description of a PCI range type.
    990  1.1  jmcneill  */
    991  1.1  jmcneill const char *
    992  1.1  jmcneill pci_resource_typename(enum pci_range_type prtype)
    993  1.1  jmcneill {
    994  1.1  jmcneill 	KASSERT(prtype < NUM_PCI_RANGES);
    995  1.1  jmcneill 	return pci_range_typenames[prtype];
    996  1.1  jmcneill }
    997