Home | History | Annotate | Line # | Download | only in pci
pciconf.c revision 1.49
      1  1.49  jmcneill /*	$NetBSD: pciconf.c,v 1.49 2020/10/10 15:22:15 jmcneill Exp $	*/
      2   1.1    briggs 
      3   1.1    briggs /*
      4   1.1    briggs  * Copyright 2001 Wasabi Systems, Inc.
      5   1.1    briggs  * All rights reserved.
      6   1.1    briggs  *
      7   1.1    briggs  * Written by Allen Briggs for Wasabi Systems, Inc.
      8   1.1    briggs  *
      9   1.1    briggs  * Redistribution and use in source and binary forms, with or without
     10   1.1    briggs  * modification, are permitted provided that the following conditions
     11   1.1    briggs  * are met:
     12   1.1    briggs  * 1. Redistributions of source code must retain the above copyright
     13   1.1    briggs  *    notice, this list of conditions and the following disclaimer.
     14   1.1    briggs  * 2. Redistributions in binary form must reproduce the above copyright
     15   1.1    briggs  *    notice, this list of conditions and the following disclaimer in the
     16   1.1    briggs  *    documentation and/or other materials provided with the distribution.
     17   1.1    briggs  * 3. All advertising materials mentioning features or use of this software
     18   1.1    briggs  *    must display the following acknowledgement:
     19   1.1    briggs  *      This product includes software developed for the NetBSD Project by
     20   1.1    briggs  *      Wasabi Systems, Inc.
     21   1.1    briggs  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22   1.1    briggs  *    or promote products derived from this software without specific prior
     23   1.1    briggs  *    written permission.
     24   1.1    briggs  *
     25   1.1    briggs  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26   1.1    briggs  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27   1.1    briggs  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28   1.1    briggs  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29   1.1    briggs  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30   1.1    briggs  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31   1.1    briggs  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32   1.1    briggs  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33   1.1    briggs  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34   1.1    briggs  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35   1.1    briggs  * POSSIBILITY OF SUCH DAMAGE.
     36   1.1    briggs  */
     37   1.1    briggs /*
     38   1.1    briggs  * Derived in part from code from PMON/2000 (http://pmon.groupbsd.org/).
     39   1.1    briggs  */
     40   1.1    briggs 
     41   1.2    briggs /*
     42   1.2    briggs  * To do:
     43  1.10   thorpej  *    - Perform all data structure allocation dynamically, don't have
     44  1.10   thorpej  *	statically-sized arrays ("oops, you lose because you have too
     45  1.10   thorpej  *	many slots filled!")
     46   1.7   thorpej  *    - Do this in 2 passes, with an MD hook to control the behavior:
     47   1.7   thorpej  *		(1) Configure the bus (possibly including expansion
     48   1.7   thorpej  *		    ROMs.
     49   1.7   thorpej  *		(2) Another pass to disable expansion ROMs if they're
     50   1.7   thorpej  *		    mapped (since you're not supposed to leave them
     51   1.7   thorpej  *		    mapped when you're not using them).
     52   1.7   thorpej  *	This would facilitate MD code executing the expansion ROMs
     53   1.7   thorpej  *	if necessary (possibly with an x86 emulator) to configure
     54   1.7   thorpej  *	devices (e.g. VGA cards).
     55   1.2    briggs  *    - Deal with "anything can be hot-plugged" -- i.e., carry configuration
     56   1.8    briggs  *	information around & be able to reconfigure on the fly
     57   1.2    briggs  *    - Deal with segments (See IA64 System Abstraction Layer)
     58   1.2    briggs  *    - Deal with subtractive bridges (& non-spec positive/subtractive decode)
     59   1.2    briggs  *    - Deal with ISA/VGA/VGA palette snooping
     60   1.2    briggs  *    - Deal with device capabilities on bridges
     61   1.8    briggs  *    - Worry about changing a bridge to/from transparency
     62   1.8    briggs  * From thorpej (05/25/01)
     63   1.8    briggs  *    - Try to handle devices that are already configured (perhaps using that
     64   1.8    briggs  *      as a hint to where we put other devices)
     65   1.2    briggs  */
     66  1.13     lukem 
     67  1.13     lukem #include <sys/cdefs.h>
     68  1.49  jmcneill __KERNEL_RCSID(0, "$NetBSD: pciconf.c,v 1.49 2020/10/10 15:22:15 jmcneill Exp $");
     69   1.2    briggs 
     70   1.1    briggs #include "opt_pci.h"
     71   1.1    briggs 
     72   1.1    briggs #include <sys/param.h>
     73   1.1    briggs #include <sys/queue.h>
     74   1.1    briggs #include <sys/systm.h>
     75   1.1    briggs #include <sys/malloc.h>
     76  1.32      matt #include <sys/kmem.h>
     77  1.47   thorpej #include <sys/vmem.h>
     78   1.1    briggs 
     79   1.1    briggs #include <dev/pci/pcivar.h>
     80   1.1    briggs #include <dev/pci/pciconf.h>
     81   1.1    briggs #include <dev/pci/pcidevs.h>
     82  1.22    briggs #include <dev/pci/pccbbreg.h>
     83   1.1    briggs 
     84  1.48   thorpej int pci_conf_debug = 0;
     85   1.1    briggs 
     86   1.1    briggs #if !defined(MIN)
     87   1.1    briggs #define	MIN(a,b) (((a)<(b))?(a):(b))
     88   1.1    briggs #define	MAX(a,b) (((a)>(b))?(a):(b))
     89   1.1    briggs #endif
     90   1.1    briggs 
     91   1.1    briggs /* per-bus constants. */
     92  1.10   thorpej #define MAX_CONF_DEV	32			/* Arbitrary */
     93   1.1    briggs #define MAX_CONF_MEM	(3 * MAX_CONF_DEV)	/* Avg. 3 per device -- Arb. */
     94   1.8    briggs #define MAX_CONF_IO	(3 * MAX_CONF_DEV)	/* Avg. 1 per device -- Arb. */
     95   1.1    briggs 
     96   1.1    briggs struct _s_pciconf_bus_t;			/* Forward declaration */
     97   1.1    briggs 
     98  1.47   thorpej struct pciconf_resource {
     99  1.47   thorpej 	vmem_t		*arena;
    100  1.47   thorpej 	bus_addr_t	min_addr;
    101  1.47   thorpej 	bus_addr_t	max_addr;
    102  1.47   thorpej 	bus_size_t	total_size;
    103  1.47   thorpej };
    104  1.47   thorpej 
    105  1.47   thorpej #define	PCICONF_RESOURCE_NTYPES	3
    106  1.47   thorpej CTASSERT(PCICONF_RESOURCE_IO < PCICONF_RESOURCE_NTYPES);
    107  1.47   thorpej CTASSERT(PCICONF_RESOURCE_MEM < PCICONF_RESOURCE_NTYPES);
    108  1.47   thorpej CTASSERT(PCICONF_RESOURCE_PREFETCHABLE_MEM < PCICONF_RESOURCE_NTYPES);
    109  1.47   thorpej 
    110  1.47   thorpej static const char *pciconf_resource_names[] = {
    111  1.47   thorpej 	[PCICONF_RESOURCE_IO]			=	"pci-io",
    112  1.47   thorpej 	[PCICONF_RESOURCE_MEM]			=	"pci-mem",
    113  1.47   thorpej 	[PCICONF_RESOURCE_PREFETCHABLE_MEM]	=	"pci-pmem",
    114  1.47   thorpej };
    115  1.47   thorpej 
    116  1.47   thorpej struct pciconf_resources {
    117  1.47   thorpej 	struct pciconf_resource resources[PCICONF_RESOURCE_NTYPES];
    118  1.47   thorpej };
    119  1.47   thorpej 
    120  1.49  jmcneill struct pciconf_resource_rsvd {
    121  1.49  jmcneill 	int		type;
    122  1.49  jmcneill 	uint64_t	start;
    123  1.49  jmcneill 	bus_size_t	size;
    124  1.49  jmcneill 	LIST_ENTRY(pciconf_resource_rsvd) next;
    125  1.49  jmcneill };
    126  1.49  jmcneill 
    127  1.49  jmcneill static LIST_HEAD(, pciconf_resource_rsvd) pciconf_resource_reservations =
    128  1.49  jmcneill     LIST_HEAD_INITIALIZER(pciconf_resource_reservations);
    129  1.49  jmcneill 
    130   1.1    briggs typedef struct _s_pciconf_dev_t {
    131   1.1    briggs 	int		ipin;
    132   1.1    briggs 	int		iline;
    133   1.1    briggs 	int		min_gnt;
    134   1.1    briggs 	int		max_lat;
    135   1.2    briggs 	int		enable;
    136   1.1    briggs 	pcitag_t	tag;
    137   1.1    briggs 	pci_chipset_tag_t	pc;
    138   1.1    briggs 	struct _s_pciconf_bus_t	*ppb;		/* I am really a bridge */
    139   1.1    briggs } pciconf_dev_t;
    140   1.1    briggs 
    141   1.1    briggs typedef struct _s_pciconf_win_t {
    142   1.1    briggs 	pciconf_dev_t	*dev;
    143   1.1    briggs 	int		reg;			/* 0 for busses */
    144   1.1    briggs 	int		align;
    145   1.1    briggs 	int		prefetch;
    146  1.39   msaitoh 	uint64_t	size;
    147  1.39   msaitoh 	uint64_t	address;
    148   1.1    briggs } pciconf_win_t;
    149   1.1    briggs 
    150   1.1    briggs typedef struct _s_pciconf_bus_t {
    151   1.1    briggs 	int		busno;
    152   1.1    briggs 	int		next_busno;
    153   1.1    briggs 	int		last_busno;
    154   1.1    briggs 	int		max_mingnt;
    155   1.1    briggs 	int		min_maxlat;
    156  1.14   thorpej 	int		cacheline_size;
    157   1.1    briggs 	int		prefetch;
    158   1.1    briggs 	int		fast_b2b;
    159   1.1    briggs 	int		freq_66;
    160   1.1    briggs 	int		def_ltim;
    161   1.1    briggs 	int		max_ltim;
    162   1.1    briggs 	int		bandwidth_used;
    163   1.1    briggs 	int		swiz;
    164   1.2    briggs 	int		io_32bit;
    165   1.2    briggs 	int		pmem_64bit;
    166  1.44   thorpej 	int		mem_64bit;
    167  1.36      matt 	int		io_align;
    168  1.36      matt 	int		mem_align;
    169  1.36      matt 	int		pmem_align;
    170   1.1    briggs 
    171   1.1    briggs 	int		ndevs;
    172   1.1    briggs 	pciconf_dev_t	device[MAX_CONF_DEV];
    173   1.1    briggs 
    174   1.1    briggs 	/* These should be sorted in order of decreasing size */
    175   1.1    briggs 	int		nmemwin;
    176   1.1    briggs 	pciconf_win_t	pcimemwin[MAX_CONF_MEM];
    177   1.1    briggs 	int		niowin;
    178   1.1    briggs 	pciconf_win_t	pciiowin[MAX_CONF_IO];
    179   1.1    briggs 
    180   1.1    briggs 	bus_size_t	io_total;
    181   1.1    briggs 	bus_size_t	mem_total;
    182   1.1    briggs 	bus_size_t	pmem_total;
    183   1.1    briggs 
    184  1.47   thorpej 	struct pciconf_resource io_res;
    185  1.47   thorpej 	struct pciconf_resource mem_res;
    186  1.47   thorpej 	struct pciconf_resource pmem_res;
    187   1.1    briggs 
    188   1.1    briggs 	pci_chipset_tag_t	pc;
    189   1.1    briggs 	struct _s_pciconf_bus_t *parent_bus;
    190   1.1    briggs } pciconf_bus_t;
    191   1.1    briggs 
    192   1.1    briggs static int	probe_bus(pciconf_bus_t *);
    193   1.1    briggs static void	alloc_busno(pciconf_bus_t *, pciconf_bus_t *);
    194  1.18    simonb static void	set_busreg(pci_chipset_tag_t, pcitag_t, int, int, int);
    195   1.4    simonb static int	pci_do_device_query(pciconf_bus_t *, pcitag_t, int, int, int);
    196   1.1    briggs static int	setup_iowins(pciconf_bus_t *);
    197   1.1    briggs static int	setup_memwins(pciconf_bus_t *);
    198   1.1    briggs static int	configure_bridge(pciconf_dev_t *);
    199   1.1    briggs static int	configure_bus(pciconf_bus_t *);
    200  1.47   thorpej static uint64_t	pci_allocate_range(struct pciconf_resource *, uint64_t, int,
    201  1.47   thorpej 		    bool);
    202   1.1    briggs static pciconf_win_t	*get_io_desc(pciconf_bus_t *, bus_size_t);
    203   1.1    briggs static pciconf_win_t	*get_mem_desc(pciconf_bus_t *, bus_size_t);
    204   1.1    briggs static pciconf_bus_t	*query_bus(pciconf_bus_t *, pciconf_dev_t *, int);
    205   1.1    briggs 
    206   1.1    briggs static void	print_tag(pci_chipset_tag_t, pcitag_t);
    207   1.1    briggs 
    208  1.47   thorpej static vmem_t *
    209  1.47   thorpej create_vmem_arena(const char *name, bus_addr_t start, bus_size_t size,
    210  1.47   thorpej     int flags)
    211  1.47   thorpej {
    212  1.47   thorpej 	KASSERT(start < VMEM_ADDR_MAX);
    213  1.47   thorpej 	KASSERT(size == 0 ||
    214  1.47   thorpej 		(VMEM_ADDR_MAX - start) >= (size - 1));
    215  1.47   thorpej 
    216  1.47   thorpej 	return vmem_create(name, start, size,
    217  1.47   thorpej 			   1,		/*quantum*/
    218  1.47   thorpej 			   NULL,	/*importfn*/
    219  1.47   thorpej 			   NULL,	/*releasefn*/
    220  1.47   thorpej 			   NULL,	/*source*/
    221  1.47   thorpej 			   0,		/*qcache_max*/
    222  1.47   thorpej 			   flags,
    223  1.47   thorpej 			   IPL_NONE);
    224  1.47   thorpej }
    225  1.47   thorpej 
    226  1.47   thorpej static int
    227  1.47   thorpej init_range_resource(struct pciconf_resource *r, const char *name,
    228  1.47   thorpej     bus_addr_t start, bus_addr_t size)
    229  1.47   thorpej {
    230  1.47   thorpej 	r->arena = create_vmem_arena(name, start, size, VM_NOSLEEP);
    231  1.47   thorpej 	if (r->arena == NULL)
    232  1.47   thorpej 		return ENOMEM;
    233  1.47   thorpej 
    234  1.47   thorpej 	r->min_addr = start;
    235  1.47   thorpej 	r->max_addr = start + (size - 1);
    236  1.47   thorpej 	r->total_size = size;
    237  1.47   thorpej 
    238  1.47   thorpej 	return 0;
    239  1.47   thorpej }
    240  1.47   thorpej 
    241  1.47   thorpej static void
    242  1.47   thorpej fini_range_resource(struct pciconf_resource *r)
    243  1.47   thorpej {
    244  1.47   thorpej 	if (r->arena) {
    245  1.47   thorpej 		vmem_xfreeall(r->arena);
    246  1.47   thorpej 		vmem_destroy(r->arena);
    247  1.47   thorpej 	}
    248  1.47   thorpej 	memset(r, 0, sizeof(*r));
    249  1.47   thorpej }
    250  1.47   thorpej 
    251   1.1    briggs static void
    252   1.1    briggs print_tag(pci_chipset_tag_t pc, pcitag_t tag)
    253   1.1    briggs {
    254   1.1    briggs 	int	bus, dev, func;
    255   1.1    briggs 
    256   1.1    briggs 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    257   1.1    briggs 	printf("PCI: bus %d, device %d, function %d: ", bus, dev, func);
    258   1.1    briggs }
    259   1.1    briggs 
    260  1.44   thorpej #ifdef _LP64
    261  1.44   thorpej #define	__used_only_lp64	__unused
    262  1.44   thorpej #else
    263  1.44   thorpej #define	__used_only_lp64	/* nothing */
    264  1.44   thorpej #endif /* _LP64 */
    265  1.44   thorpej 
    266   1.1    briggs /************************************************************************/
    267   1.1    briggs /************************************************************************/
    268   1.1    briggs /***********************   Bus probing routines   ***********************/
    269   1.1    briggs /************************************************************************/
    270   1.1    briggs /************************************************************************/
    271   1.1    briggs static pciconf_win_t *
    272   1.1    briggs get_io_desc(pciconf_bus_t *pb, bus_size_t size)
    273   1.1    briggs {
    274   1.1    briggs 	int	i, n;
    275   1.1    briggs 
    276   1.1    briggs 	n = pb->niowin;
    277  1.40   msaitoh 	for (i = n; i > 0 && size > pb->pciiowin[i-1].size; i--)
    278   1.1    briggs 		pb->pciiowin[i] = pb->pciiowin[i-1]; /* struct copy */
    279   1.1    briggs 	return &pb->pciiowin[i];
    280   1.1    briggs }
    281   1.1    briggs 
    282   1.1    briggs static pciconf_win_t *
    283   1.1    briggs get_mem_desc(pciconf_bus_t *pb, bus_size_t size)
    284   1.1    briggs {
    285   1.1    briggs 	int	i, n;
    286   1.1    briggs 
    287   1.1    briggs 	n = pb->nmemwin;
    288  1.40   msaitoh 	for (i = n; i > 0 && size > pb->pcimemwin[i-1].size; i--)
    289   1.1    briggs 		pb->pcimemwin[i] = pb->pcimemwin[i-1]; /* struct copy */
    290   1.1    briggs 	return &pb->pcimemwin[i];
    291   1.1    briggs }
    292   1.1    briggs 
    293   1.1    briggs /*
    294   1.1    briggs  * Set up bus common stuff, then loop over devices & functions.
    295   1.1    briggs  * If we find something, call pci_do_device_query()).
    296   1.1    briggs  */
    297   1.1    briggs static int
    298   1.1    briggs probe_bus(pciconf_bus_t *pb)
    299   1.1    briggs {
    300  1.33    dyoung 	int device;
    301  1.33    dyoung 	uint8_t devs[32];
    302  1.33    dyoung 	int i, n;
    303   1.1    briggs 
    304   1.1    briggs 	pb->ndevs = 0;
    305   1.1    briggs 	pb->niowin = 0;
    306   1.1    briggs 	pb->nmemwin = 0;
    307   1.1    briggs 	pb->freq_66 = 1;
    308  1.21  augustss #ifdef PCICONF_NO_FAST_B2B
    309  1.21  augustss 	pb->fast_b2b = 0;
    310  1.21  augustss #else
    311   1.1    briggs 	pb->fast_b2b = 1;
    312  1.21  augustss #endif
    313   1.1    briggs 	pb->prefetch = 1;
    314   1.1    briggs 	pb->max_mingnt = 0;	/* we are looking for the maximum */
    315   1.1    briggs 	pb->min_maxlat = 0x100;	/* we are looking for the minimum */
    316   1.1    briggs 	pb->bandwidth_used = 0;
    317   1.4    simonb 
    318  1.33    dyoung 	n = pci_bus_devorder(pb->pc, pb->busno, devs, __arraycount(devs));
    319  1.33    dyoung 	for (i = 0; i < n; i++) {
    320   1.1    briggs 		pcitag_t tag;
    321   1.1    briggs 		pcireg_t id, bhlcr;
    322   1.1    briggs 		int function, nfunction;
    323   1.4    simonb 		int confmode;
    324   1.1    briggs 
    325  1.33    dyoung 		device = devs[i];
    326  1.33    dyoung 
    327   1.1    briggs 		tag = pci_make_tag(pb->pc, pb->busno, device, 0);
    328   1.1    briggs 		if (pci_conf_debug) {
    329   1.1    briggs 			print_tag(pb->pc, tag);
    330   1.1    briggs 		}
    331   1.1    briggs 		id = pci_conf_read(pb->pc, tag, PCI_ID_REG);
    332   1.1    briggs 
    333   1.4    simonb 		if (pci_conf_debug) {
    334   1.4    simonb 			printf("id=%x: Vendor=%x, Product=%x\n",
    335  1.40   msaitoh 			    id, PCI_VENDOR(id), PCI_PRODUCT(id));
    336   1.4    simonb 		}
    337   1.1    briggs 		/* Invalid vendor ID value? */
    338   1.1    briggs 		if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    339   1.1    briggs 			continue;
    340   1.1    briggs 
    341   1.1    briggs 		bhlcr = pci_conf_read(pb->pc, tag, PCI_BHLC_REG);
    342   1.1    briggs 		nfunction = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
    343  1.40   msaitoh 		for (function = 0; function < nfunction; function++) {
    344   1.1    briggs 			tag = pci_make_tag(pb->pc, pb->busno, device, function);
    345   1.1    briggs 			id = pci_conf_read(pb->pc, tag, PCI_ID_REG);
    346   1.1    briggs 			if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    347   1.1    briggs 				continue;
    348  1.40   msaitoh 			if (pb->ndevs + 1 < MAX_CONF_DEV) {
    349   1.1    briggs 				if (pci_conf_debug) {
    350   1.1    briggs 					print_tag(pb->pc, tag);
    351   1.3   thorpej 					printf("Found dev 0x%04x 0x%04x -- "
    352   1.3   thorpej 					    "really probing.\n",
    353   1.3   thorpej 					PCI_VENDOR(id), PCI_PRODUCT(id));
    354   1.1    briggs 				}
    355   1.4    simonb #ifdef __HAVE_PCI_CONF_HOOK
    356   1.4    simonb 				confmode = pci_conf_hook(pb->pc, pb->busno,
    357   1.4    simonb 				    device, function, id);
    358   1.4    simonb 				if (confmode == 0)
    359   1.4    simonb 					continue;
    360   1.4    simonb #else
    361   1.6   thorpej 				/*
    362   1.6   thorpej 				 * Don't enable expansion ROMS -- some cards
    363   1.6   thorpej 				 * share address decoders between the EXPROM
    364   1.6   thorpej 				 * and PCI memory space, and enabling the ROM
    365   1.6   thorpej 				 * when not needed will cause all sorts of
    366   1.6   thorpej 				 * lossage.
    367   1.6   thorpej 				 */
    368  1.28   gdamore 				confmode = PCI_CONF_DEFAULT;
    369   1.4    simonb #endif
    370   1.1    briggs 				if (pci_do_device_query(pb, tag, device,
    371   1.4    simonb 				    function, confmode))
    372   1.1    briggs 					return -1;
    373   1.1    briggs 				pb->ndevs++;
    374   1.1    briggs 			}
    375   1.1    briggs 		}
    376   1.1    briggs 	}
    377   1.1    briggs 	return 0;
    378   1.1    briggs }
    379   1.1    briggs 
    380   1.1    briggs static void
    381   1.1    briggs alloc_busno(pciconf_bus_t *parent, pciconf_bus_t *pb)
    382   1.1    briggs {
    383   1.1    briggs 	pb->busno = parent->next_busno;
    384  1.17  augustss 	pb->next_busno = pb->busno + 1;
    385  1.17  augustss }
    386  1.17  augustss 
    387  1.17  augustss static void
    388  1.17  augustss set_busreg(pci_chipset_tag_t pc, pcitag_t tag, int prim, int sec, int sub)
    389  1.17  augustss {
    390  1.17  augustss 	pcireg_t	busreg;
    391  1.17  augustss 
    392  1.41   msaitoh 	busreg  =  __SHIFTIN(prim, PCI_BRIDGE_BUS_PRIMARY);
    393  1.41   msaitoh 	busreg |=  __SHIFTIN(sec,  PCI_BRIDGE_BUS_SECONDARY);
    394  1.41   msaitoh 	busreg |=  __SHIFTIN(sub,  PCI_BRIDGE_BUS_SUBORDINATE);
    395  1.17  augustss 	pci_conf_write(pc, tag, PCI_BRIDGE_BUS_REG, busreg);
    396   1.1    briggs }
    397   1.1    briggs 
    398   1.1    briggs static pciconf_bus_t *
    399   1.1    briggs query_bus(pciconf_bus_t *parent, pciconf_dev_t *pd, int dev)
    400   1.1    briggs {
    401   1.1    briggs 	pciconf_bus_t	*pb;
    402  1.17  augustss 	pcireg_t	io, pmem;
    403   1.1    briggs 	pciconf_win_t	*pi, *pm;
    404   1.1    briggs 
    405  1.42       chs 	pb = kmem_zalloc(sizeof (pciconf_bus_t), KM_SLEEP);
    406  1.14   thorpej 	pb->cacheline_size = parent->cacheline_size;
    407   1.1    briggs 	pb->parent_bus = parent;
    408   1.1    briggs 	alloc_busno(parent, pb);
    409   1.1    briggs 
    410  1.36      matt 	pb->mem_align = 0x100000;	/* 1M alignment */
    411  1.36      matt 	pb->pmem_align = 0x100000;	/* 1M alignment */
    412  1.36      matt 	pb->io_align = 0x1000;		/* 4K alignment */
    413  1.36      matt 
    414  1.17  augustss 	set_busreg(parent->pc, pd->tag, parent->busno, pb->busno, 0xff);
    415   1.1    briggs 
    416   1.1    briggs 	pb->swiz = parent->swiz + dev;
    417   1.1    briggs 
    418  1.47   thorpej 	memset(&pb->io_res, 0, sizeof(pb->io_res));
    419  1.47   thorpej 	memset(&pb->mem_res, 0, sizeof(pb->mem_res));
    420  1.47   thorpej 	memset(&pb->pmem_res, 0, sizeof(pb->pmem_res));
    421  1.47   thorpej 
    422   1.1    briggs 	pb->pc = parent->pc;
    423   1.1    briggs 	pb->io_total = pb->mem_total = pb->pmem_total = 0;
    424   1.1    briggs 
    425   1.2    briggs 	pb->io_32bit = 0;
    426   1.2    briggs 	if (parent->io_32bit) {
    427  1.11   thorpej 		io = pci_conf_read(parent->pc, pd->tag, PCI_BRIDGE_STATIO_REG);
    428  1.40   msaitoh 		if (PCI_BRIDGE_IO_32BITS(io))
    429   1.2    briggs 			pb->io_32bit = 1;
    430   1.2    briggs 	}
    431   1.2    briggs 
    432   1.2    briggs 	pb->pmem_64bit = 0;
    433   1.2    briggs 	if (parent->pmem_64bit) {
    434  1.11   thorpej 		pmem = pci_conf_read(parent->pc, pd->tag,
    435   1.2    briggs 		    PCI_BRIDGE_PREFETCHMEM_REG);
    436  1.40   msaitoh 		if (PCI_BRIDGE_PREFETCHMEM_64BITS(pmem))
    437   1.2    briggs 			pb->pmem_64bit = 1;
    438   1.2    briggs 	}
    439   1.2    briggs 
    440  1.44   thorpej 	/* Bridges only forward a 32-bit range of non-prefetcable memory. */
    441  1.44   thorpej 	pb->mem_64bit = 0;
    442  1.44   thorpej 
    443   1.1    briggs 	if (probe_bus(pb)) {
    444   1.1    briggs 		printf("Failed to probe bus %d\n", pb->busno);
    445   1.1    briggs 		goto err;
    446   1.1    briggs 	}
    447   1.1    briggs 
    448  1.17  augustss 	/* We have found all subordinate busses now, reprogram busreg. */
    449  1.40   msaitoh 	pb->last_busno = pb->next_busno - 1;
    450  1.17  augustss 	parent->next_busno = pb->next_busno;
    451  1.17  augustss 	set_busreg(parent->pc, pd->tag, parent->busno, pb->busno,
    452  1.17  augustss 		   pb->last_busno);
    453  1.17  augustss 	if (pci_conf_debug)
    454  1.17  augustss 		printf("PCI bus bridge (parent %d) covers busses %d-%d\n",
    455  1.17  augustss 			parent->busno, pb->busno, pb->last_busno);
    456  1.17  augustss 
    457   1.1    briggs 	if (pb->io_total > 0) {
    458   1.1    briggs 		if (parent->niowin >= MAX_CONF_IO) {
    459  1.35      matt 			printf("pciconf: too many (%d) I/O windows\n",
    460  1.35      matt 			    parent->niowin);
    461   1.1    briggs 			goto err;
    462   1.1    briggs 		}
    463  1.36      matt 		pb->io_total |= pb->io_align - 1; /* Round up */
    464   1.1    briggs 		pi = get_io_desc(parent, pb->io_total);
    465   1.1    briggs 		pi->dev = pd;
    466   1.1    briggs 		pi->reg = 0;
    467   1.1    briggs 		pi->size = pb->io_total;
    468  1.36      matt 		pi->align = pb->io_align;	/* 4K min alignment */
    469  1.36      matt 		if (parent->io_align < pb->io_align)
    470  1.36      matt 			parent->io_align = pb->io_align;
    471   1.1    briggs 		pi->prefetch = 0;
    472   1.1    briggs 		parent->niowin++;
    473   1.1    briggs 		parent->io_total += pb->io_total;
    474   1.1    briggs 	}
    475   1.1    briggs 
    476   1.1    briggs 	if (pb->mem_total > 0) {
    477   1.1    briggs 		if (parent->nmemwin >= MAX_CONF_MEM) {
    478  1.35      matt 			printf("pciconf: too many (%d) MEM windows\n",
    479  1.35      matt 			     parent->nmemwin);
    480   1.1    briggs 			goto err;
    481   1.1    briggs 		}
    482  1.40   msaitoh 		pb->mem_total |= pb->mem_align - 1; /* Round up */
    483   1.1    briggs 		pm = get_mem_desc(parent, pb->mem_total);
    484   1.1    briggs 		pm->dev = pd;
    485   1.1    briggs 		pm->reg = 0;
    486   1.1    briggs 		pm->size = pb->mem_total;
    487  1.36      matt 		pm->align = pb->mem_align;	/* 1M min alignment */
    488  1.36      matt 		if (parent->mem_align < pb->mem_align)
    489  1.36      matt 			parent->mem_align = pb->mem_align;
    490   1.1    briggs 		pm->prefetch = 0;
    491   1.1    briggs 		parent->nmemwin++;
    492   1.1    briggs 		parent->mem_total += pb->mem_total;
    493   1.1    briggs 	}
    494   1.1    briggs 
    495   1.1    briggs 	if (pb->pmem_total > 0) {
    496   1.1    briggs 		if (parent->nmemwin >= MAX_CONF_MEM) {
    497  1.10   thorpej 			printf("pciconf: too many MEM windows\n");
    498   1.1    briggs 			goto err;
    499   1.1    briggs 		}
    500  1.40   msaitoh 		pb->pmem_total |= pb->pmem_align - 1; /* Round up */
    501   1.1    briggs 		pm = get_mem_desc(parent, pb->pmem_total);
    502   1.1    briggs 		pm->dev = pd;
    503   1.1    briggs 		pm->reg = 0;
    504   1.1    briggs 		pm->size = pb->pmem_total;
    505  1.36      matt 		pm->align = pb->pmem_align;	/* 1M alignment */
    506  1.36      matt 		if (parent->pmem_align < pb->pmem_align)
    507  1.36      matt 			parent->pmem_align = pb->pmem_align;
    508   1.1    briggs 		pm->prefetch = 1;
    509   1.1    briggs 		parent->nmemwin++;
    510   1.1    briggs 		parent->pmem_total += pb->pmem_total;
    511   1.1    briggs 	}
    512   1.1    briggs 
    513   1.1    briggs 	return pb;
    514   1.1    briggs err:
    515  1.32      matt 	kmem_free(pb, sizeof(*pb));
    516   1.1    briggs 	return NULL;
    517   1.1    briggs }
    518   1.1    briggs 
    519  1.49  jmcneill static bool
    520  1.49  jmcneill pci_resource_is_reserved(int type, uint64_t addr, uint64_t size)
    521  1.49  jmcneill {
    522  1.49  jmcneill 	struct pciconf_resource_rsvd *rsvd;
    523  1.49  jmcneill 
    524  1.49  jmcneill 	LIST_FOREACH(rsvd, &pciconf_resource_reservations, next) {
    525  1.49  jmcneill 		if (rsvd->type != type)
    526  1.49  jmcneill 			continue;
    527  1.49  jmcneill 		if (rsvd->start <= addr + size && rsvd->start + rsvd->size >= addr)
    528  1.49  jmcneill 			return true;
    529  1.49  jmcneill 	}
    530  1.49  jmcneill 
    531  1.49  jmcneill 	return false;
    532  1.49  jmcneill }
    533  1.49  jmcneill 
    534  1.49  jmcneill static bool
    535  1.49  jmcneill pci_device_is_reserved(pciconf_bus_t *pb, pcitag_t tag)
    536  1.49  jmcneill {
    537  1.49  jmcneill 	pcireg_t base, base64, mask, mask64;
    538  1.49  jmcneill 	uint64_t addr, size;
    539  1.49  jmcneill 	int br, width;
    540  1.49  jmcneill 
    541  1.49  jmcneill 	/*
    542  1.49  jmcneill 	 * Look to see if this device is enabled and one of the resources
    543  1.49  jmcneill 	 * is already in use (firmware configured console device). If so,
    544  1.49  jmcneill 	 * skip resource assignment and use firmware values.
    545  1.49  jmcneill 	 */
    546  1.49  jmcneill 	width = 4;
    547  1.49  jmcneill 	for (br = PCI_MAPREG_START; br < PCI_MAPREG_END; br += width) {
    548  1.49  jmcneill 
    549  1.49  jmcneill 		base = pci_conf_read(pb->pc, tag, br);
    550  1.49  jmcneill 		pci_conf_write(pb->pc, tag, br, 0xffffffff);
    551  1.49  jmcneill 		mask = pci_conf_read(pb->pc, tag, br);
    552  1.49  jmcneill 		pci_conf_write(pb->pc, tag, br, base);
    553  1.49  jmcneill 		width = 4;
    554  1.49  jmcneill 
    555  1.49  jmcneill 		switch (PCI_MAPREG_TYPE(base)) {
    556  1.49  jmcneill 		case PCI_MAPREG_TYPE_IO:
    557  1.49  jmcneill 			addr = PCI_MAPREG_IO_ADDR(base);
    558  1.49  jmcneill 			size = PCI_MAPREG_IO_SIZE(mask);
    559  1.49  jmcneill 			if (pci_resource_is_reserved(PCI_CONF_MAP_IO, addr, size))
    560  1.49  jmcneill 				return true;
    561  1.49  jmcneill 			break;
    562  1.49  jmcneill 		case PCI_MAPREG_TYPE_MEM:
    563  1.49  jmcneill 			if (PCI_MAPREG_MEM_TYPE(base) == PCI_MAPREG_MEM_TYPE_64BIT) {
    564  1.49  jmcneill 				base64 = pci_conf_read(pb->pc, tag, br + 4);
    565  1.49  jmcneill 				pci_conf_write(pb->pc, tag, br + 4, 0xffffffff);
    566  1.49  jmcneill 				mask64 = pci_conf_read(pb->pc, tag, br + 4);
    567  1.49  jmcneill 				pci_conf_write(pb->pc, tag, br + 4, base64);
    568  1.49  jmcneill 				addr = (uint64_t)PCI_MAPREG_MEM64_ADDR(
    569  1.49  jmcneill 				      (((uint64_t)base64) << 32) | base);
    570  1.49  jmcneill 				size = (uint64_t)PCI_MAPREG_MEM64_SIZE(
    571  1.49  jmcneill 				      (((uint64_t)mask64) << 32) | mask);
    572  1.49  jmcneill 				width = 8;
    573  1.49  jmcneill 			} else {
    574  1.49  jmcneill 				addr = PCI_MAPREG_MEM_ADDR(base);
    575  1.49  jmcneill 				size = PCI_MAPREG_MEM_SIZE(mask);
    576  1.49  jmcneill 			}
    577  1.49  jmcneill 			if (pci_resource_is_reserved(PCI_CONF_MAP_MEM, addr, size))
    578  1.49  jmcneill 				return true;
    579  1.49  jmcneill 			break;
    580  1.49  jmcneill 		}
    581  1.49  jmcneill 	}
    582  1.49  jmcneill 
    583  1.49  jmcneill 	return false;
    584  1.49  jmcneill }
    585  1.49  jmcneill 
    586   1.1    briggs static int
    587  1.39   msaitoh pci_do_device_query(pciconf_bus_t *pb, pcitag_t tag, int dev, int func,
    588  1.39   msaitoh     int mode)
    589   1.1    briggs {
    590   1.1    briggs 	pciconf_dev_t	*pd;
    591   1.1    briggs 	pciconf_win_t	*pi, *pm;
    592  1.39   msaitoh 	pcireg_t	classreg, cmd, icr, bhlc, bar, mask, bar64, mask64,
    593  1.39   msaitoh 	    busreg;
    594  1.39   msaitoh 	uint64_t	size;
    595  1.22    briggs 	int		br, width, reg_start, reg_end;
    596   1.1    briggs 
    597   1.1    briggs 	pd = &pb->device[pb->ndevs];
    598   1.1    briggs 	pd->pc = pb->pc;
    599   1.1    briggs 	pd->tag = tag;
    600   1.1    briggs 	pd->ppb = NULL;
    601   1.4    simonb 	pd->enable = mode;
    602   1.1    briggs 
    603  1.37      matt 	classreg = pci_conf_read(pb->pc, tag, PCI_CLASS_REG);
    604   1.1    briggs 
    605   1.1    briggs 	cmd = pci_conf_read(pb->pc, tag, PCI_COMMAND_STATUS_REG);
    606  1.32      matt 	bhlc = pci_conf_read(pb->pc, tag, PCI_BHLC_REG);
    607   1.1    briggs 
    608  1.37      matt 	if (PCI_CLASS(classreg) != PCI_CLASS_BRIDGE
    609  1.32      matt 	    && PCI_HDRTYPE_TYPE(bhlc) != PCI_HDRTYPE_PPB) {
    610   1.1    briggs 		cmd &= ~(PCI_COMMAND_MASTER_ENABLE |
    611   1.1    briggs 		    PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE);
    612   1.1    briggs 		pci_conf_write(pb->pc, tag, PCI_COMMAND_STATUS_REG, cmd);
    613   1.3   thorpej 	} else if (pci_conf_debug) {
    614   1.3   thorpej 		print_tag(pb->pc, tag);
    615   1.3   thorpej 		printf("device is a bridge; not clearing enables\n");
    616   1.1    briggs 	}
    617   1.1    briggs 
    618   1.1    briggs 	if ((cmd & PCI_STATUS_BACKTOBACK_SUPPORT) == 0)
    619   1.1    briggs 		pb->fast_b2b = 0;
    620   1.1    briggs 
    621   1.1    briggs 	if ((cmd & PCI_STATUS_66MHZ_SUPPORT) == 0)
    622   1.1    briggs 		pb->freq_66 = 0;
    623   1.1    briggs 
    624  1.22    briggs 	switch (PCI_HDRTYPE_TYPE(bhlc)) {
    625  1.22    briggs 	case PCI_HDRTYPE_DEVICE:
    626  1.22    briggs 		reg_start = PCI_MAPREG_START;
    627  1.22    briggs 		reg_end = PCI_MAPREG_END;
    628  1.22    briggs 		break;
    629  1.22    briggs 	case PCI_HDRTYPE_PPB:
    630   1.1    briggs 		pd->ppb = query_bus(pb, pd, dev);
    631   1.1    briggs 		if (pd->ppb == NULL)
    632   1.1    briggs 			return -1;
    633   1.1    briggs 		return 0;
    634  1.22    briggs 	case PCI_HDRTYPE_PCB:
    635  1.22    briggs 		reg_start = PCI_MAPREG_START;
    636  1.22    briggs 		reg_end = PCI_MAPREG_PCB_END;
    637  1.22    briggs 
    638  1.22    briggs 		busreg = pci_conf_read(pb->pc, tag, PCI_BUSNUM);
    639  1.22    briggs 		busreg  =  (busreg & 0xff000000) |
    640  1.41   msaitoh 		    __SHIFTIN(pb->busno, PCI_BRIDGE_BUS_PRIMARY) |
    641  1.41   msaitoh 		    __SHIFTIN(pb->next_busno, PCI_BRIDGE_BUS_SECONDARY) |
    642  1.41   msaitoh 		    __SHIFTIN(pb->next_busno, PCI_BRIDGE_BUS_SUBORDINATE);
    643  1.22    briggs 		pci_conf_write(pb->pc, tag, PCI_BUSNUM, busreg);
    644  1.22    briggs 
    645  1.24    simonb 		pb->next_busno++;
    646  1.22    briggs 		break;
    647  1.22    briggs 	default:
    648  1.22    briggs 		return -1;
    649   1.1    briggs 	}
    650   1.1    briggs 
    651   1.1    briggs 	icr = pci_conf_read(pb->pc, tag, PCI_INTERRUPT_REG);
    652   1.1    briggs 	pd->ipin = PCI_INTERRUPT_PIN(icr);
    653   1.1    briggs 	pd->iline = PCI_INTERRUPT_LINE(icr);
    654   1.1    briggs 	pd->min_gnt = PCI_MIN_GNT(icr);
    655   1.1    briggs 	pd->max_lat = PCI_MAX_LAT(icr);
    656   1.1    briggs 	if (pd->iline || pd->ipin) {
    657   1.8    briggs 		pci_conf_interrupt(pb->pc, pb->busno, dev, pd->ipin, pb->swiz,
    658   1.1    briggs 		    &pd->iline);
    659   1.1    briggs 		icr &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT);
    660   1.1    briggs 		icr |= (pd->iline << PCI_INTERRUPT_LINE_SHIFT);
    661   1.1    briggs 		pci_conf_write(pb->pc, tag, PCI_INTERRUPT_REG, icr);
    662   1.1    briggs 	}
    663   1.1    briggs 
    664   1.1    briggs 	if (pd->min_gnt != 0 || pd->max_lat != 0) {
    665   1.1    briggs 		if (pd->min_gnt != 0 && pd->min_gnt > pb->max_mingnt)
    666   1.1    briggs 			pb->max_mingnt = pd->min_gnt;
    667   1.1    briggs 
    668   1.1    briggs 		if (pd->max_lat != 0 && pd->max_lat < pb->min_maxlat)
    669   1.1    briggs 			pb->min_maxlat = pd->max_lat;
    670   1.1    briggs 
    671   1.1    briggs 		pb->bandwidth_used += pd->min_gnt * 4000000 /
    672   1.1    briggs 				(pd->min_gnt + pd->max_lat);
    673   1.1    briggs 	}
    674   1.1    briggs 
    675  1.49  jmcneill 	if (PCI_HDRTYPE_TYPE(bhlc) == PCI_HDRTYPE_DEVICE &&
    676  1.49  jmcneill 	    pci_device_is_reserved(pb, tag)) {
    677  1.49  jmcneill 		/*
    678  1.49  jmcneill 		 * Device already configured by firmware.
    679  1.49  jmcneill 		 */
    680  1.49  jmcneill 		return 0;
    681  1.49  jmcneill 	}
    682  1.49  jmcneill 
    683   1.1    briggs 	width = 4;
    684  1.22    briggs 	for (br = reg_start; br < reg_end; br += width) {
    685   1.3   thorpej #if 0
    686   1.8    briggs /* XXX Should only ignore if IDE not in legacy mode? */
    687  1.37      matt 		if (PCI_CLASS(classreg) == PCI_CLASS_MASS_STORAGE &&
    688  1.37      matt 		    PCI_SUBCLASS(classreg) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
    689   1.1    briggs 			break;
    690   1.1    briggs 		}
    691   1.3   thorpej #endif
    692   1.1    briggs 		bar = pci_conf_read(pb->pc, tag, br);
    693   1.3   thorpej 		pci_conf_write(pb->pc, tag, br, 0xffffffff);
    694   1.1    briggs 		mask = pci_conf_read(pb->pc, tag, br);
    695   1.1    briggs 		pci_conf_write(pb->pc, tag, br, bar);
    696   1.1    briggs 		width = 4;
    697   1.1    briggs 
    698   1.8    briggs 		if (   (mode & PCI_CONF_MAP_IO)
    699   1.8    briggs 		    && (PCI_MAPREG_TYPE(mask) == PCI_MAPREG_TYPE_IO)) {
    700   1.8    briggs 			/*
    701   1.8    briggs 			 * Upper 16 bits must be one.  Devices may hardwire
    702   1.8    briggs 			 * them to zero, though, per PCI 2.2, 6.2.5.1, p 203.
    703   1.8    briggs 			 */
    704   1.3   thorpej 			mask |= 0xffff0000;
    705   1.3   thorpej 
    706   1.3   thorpej 			size = PCI_MAPREG_IO_SIZE(mask);
    707   1.3   thorpej 			if (size == 0) {
    708   1.3   thorpej 				if (pci_conf_debug) {
    709   1.3   thorpej 					print_tag(pb->pc, tag);
    710   1.3   thorpej 					printf("I/O BAR 0x%x is void\n", br);
    711   1.3   thorpej 				}
    712   1.3   thorpej 				continue;
    713   1.3   thorpej 			}
    714   1.1    briggs 
    715   1.1    briggs 			if (pb->niowin >= MAX_CONF_IO) {
    716  1.10   thorpej 				printf("pciconf: too many I/O windows\n");
    717   1.1    briggs 				return -1;
    718   1.1    briggs 			}
    719   1.1    briggs 
    720   1.1    briggs 			pi = get_io_desc(pb, size);
    721   1.1    briggs 			pi->dev = pd;
    722   1.1    briggs 			pi->reg = br;
    723  1.43   msaitoh 			pi->size = (uint64_t)size;
    724   1.1    briggs 			pi->align = 4;
    725  1.36      matt 			if (pb->io_align < pi->size)
    726  1.36      matt 				pb->io_align = pi->size;
    727   1.1    briggs 			pi->prefetch = 0;
    728   1.1    briggs 			if (pci_conf_debug) {
    729   1.1    briggs 				print_tag(pb->pc, tag);
    730  1.23       scw 				printf("Register 0x%x, I/O size %" PRIu64 "\n",
    731   1.1    briggs 				    br, pi->size);
    732   1.1    briggs 			}
    733   1.1    briggs 			pb->niowin++;
    734   1.1    briggs 			pb->io_total += size;
    735   1.4    simonb 		} else if ((mode & PCI_CONF_MAP_MEM)
    736   1.4    simonb 			   && (PCI_MAPREG_TYPE(mask) == PCI_MAPREG_TYPE_MEM)) {
    737   1.1    briggs 			switch (PCI_MAPREG_MEM_TYPE(mask)) {
    738   1.1    briggs 			case PCI_MAPREG_MEM_TYPE_32BIT:
    739   1.1    briggs 			case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    740  1.43   msaitoh 				size = (uint64_t)PCI_MAPREG_MEM_SIZE(mask);
    741   1.1    briggs 				break;
    742   1.1    briggs 			case PCI_MAPREG_MEM_TYPE_64BIT:
    743   1.1    briggs 				bar64 = pci_conf_read(pb->pc, tag, br + 4);
    744   1.1    briggs 				pci_conf_write(pb->pc, tag, br + 4, 0xffffffff);
    745   1.1    briggs 				mask64 = pci_conf_read(pb->pc, tag, br + 4);
    746   1.1    briggs 				pci_conf_write(pb->pc, tag, br + 4, bar64);
    747  1.43   msaitoh 				size = (uint64_t)PCI_MAPREG_MEM64_SIZE(
    748  1.43   msaitoh 				      (((uint64_t)mask64) << 32) | mask);
    749   1.1    briggs 				width = 8;
    750  1.16    briggs 				break;
    751   1.1    briggs 			default:
    752   1.1    briggs 				print_tag(pb->pc, tag);
    753   1.1    briggs 				printf("reserved mapping type 0x%x\n",
    754   1.1    briggs 					PCI_MAPREG_MEM_TYPE(mask));
    755   1.1    briggs 				continue;
    756   1.1    briggs 			}
    757   1.1    briggs 
    758   1.3   thorpej 			if (size == 0) {
    759   1.3   thorpej 				if (pci_conf_debug) {
    760   1.3   thorpej 					print_tag(pb->pc, tag);
    761   1.3   thorpej 					printf("MEM%d BAR 0x%x is void\n",
    762   1.3   thorpej 					    PCI_MAPREG_MEM_TYPE(mask) ==
    763   1.3   thorpej 						PCI_MAPREG_MEM_TYPE_64BIT ?
    764   1.3   thorpej 						64 : 32, br);
    765   1.3   thorpej 				}
    766   1.3   thorpej 				continue;
    767  1.16    briggs 			} else {
    768  1.16    briggs 				if (pci_conf_debug) {
    769  1.16    briggs 					print_tag(pb->pc, tag);
    770  1.36      matt 					printf("MEM%d BAR 0x%x has size %#lx\n",
    771  1.16    briggs 					    PCI_MAPREG_MEM_TYPE(mask) ==
    772  1.16    briggs 						PCI_MAPREG_MEM_TYPE_64BIT ?
    773  1.43   msaitoh 						64 : 32,
    774  1.43   msaitoh 					    br, (unsigned long)size);
    775  1.16    briggs 				}
    776   1.3   thorpej 			}
    777   1.3   thorpej 
    778   1.1    briggs 			if (pb->nmemwin >= MAX_CONF_MEM) {
    779  1.10   thorpej 				printf("pciconf: too many memory windows\n");
    780   1.1    briggs 				return -1;
    781   1.1    briggs 			}
    782   1.1    briggs 
    783   1.1    briggs 			pm = get_mem_desc(pb, size);
    784   1.1    briggs 			pm->dev = pd;
    785   1.1    briggs 			pm->reg = br;
    786   1.1    briggs 			pm->size = size;
    787   1.1    briggs 			pm->align = 4;
    788   1.1    briggs 			pm->prefetch = PCI_MAPREG_MEM_PREFETCHABLE(mask);
    789   1.1    briggs 			if (pci_conf_debug) {
    790   1.1    briggs 				print_tag(pb->pc, tag);
    791  1.23       scw 				printf("Register 0x%x, memory size %"
    792  1.23       scw 				    PRIu64 "\n", br, pm->size);
    793   1.1    briggs 			}
    794   1.1    briggs 			pb->nmemwin++;
    795   1.1    briggs 			if (pm->prefetch) {
    796   1.1    briggs 				pb->pmem_total += size;
    797  1.36      matt 				if (pb->pmem_align < pm->size)
    798  1.36      matt 					pb->pmem_align = pm->size;
    799   1.1    briggs 			} else {
    800   1.1    briggs 				pb->mem_total += size;
    801  1.36      matt 				if (pb->mem_align < pm->size)
    802  1.36      matt 					pb->mem_align = pm->size;
    803   1.1    briggs 			}
    804   1.1    briggs 		}
    805   1.1    briggs 	}
    806   1.1    briggs 
    807   1.4    simonb 	if (mode & PCI_CONF_MAP_ROM) {
    808   1.4    simonb 		bar = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
    809   1.4    simonb 		pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM, 0xfffffffe);
    810   1.4    simonb 		mask = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
    811   1.4    simonb 		pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM, bar);
    812   1.4    simonb 
    813   1.4    simonb 		if (mask != 0 && mask != 0xffffffff) {
    814   1.4    simonb 			if (pb->nmemwin >= MAX_CONF_MEM) {
    815  1.10   thorpej 				printf("pciconf: too many memory windows\n");
    816   1.4    simonb 				return -1;
    817   1.4    simonb 			}
    818  1.43   msaitoh 			size = (uint64_t)PCI_MAPREG_MEM_SIZE(mask);
    819   1.1    briggs 
    820   1.4    simonb 			pm = get_mem_desc(pb, size);
    821   1.4    simonb 			pm->dev = pd;
    822   1.4    simonb 			pm->reg = PCI_MAPREG_ROM;
    823   1.4    simonb 			pm->size = size;
    824   1.4    simonb 			pm->align = 4;
    825  1.44   thorpej 			pm->prefetch = 0;
    826   1.4    simonb 			if (pci_conf_debug) {
    827   1.4    simonb 				print_tag(pb->pc, tag);
    828  1.23       scw 				printf("Expansion ROM memory size %"
    829  1.23       scw 				    PRIu64 "\n", pm->size);
    830   1.4    simonb 			}
    831   1.4    simonb 			pb->nmemwin++;
    832  1.44   thorpej 			if (pm->prefetch) {
    833  1.44   thorpej 				pb->pmem_total += size;
    834  1.44   thorpej 				if (pb->pmem_align < pm->size)
    835  1.44   thorpej 					pb->pmem_align = pm->size;
    836  1.44   thorpej 			} else {
    837  1.44   thorpej 				pb->mem_total += size;
    838  1.44   thorpej 				if (pb->mem_align < pm->size)
    839  1.44   thorpej 					pb->mem_align = pm->size;
    840  1.44   thorpej 			}
    841   1.1    briggs 		}
    842   1.8    briggs 	} else {
    843  1.28   gdamore 		/* Don't enable ROMs if we aren't going to map them. */
    844  1.28   gdamore 		mode &= ~PCI_CONF_ENABLE_ROM;
    845  1.28   gdamore 		pd->enable &= ~PCI_CONF_ENABLE_ROM;
    846  1.28   gdamore 	}
    847  1.28   gdamore 
    848  1.28   gdamore 	if (!(mode & PCI_CONF_ENABLE_ROM)) {
    849   1.8    briggs 		/* Ensure ROM is disabled */
    850   1.8    briggs 		bar = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
    851   1.8    briggs 		pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM,
    852   1.8    briggs 		    bar & ~PCI_MAPREG_ROM_ENABLE);
    853   1.1    briggs 	}
    854   1.1    briggs 
    855   1.1    briggs 	return 0;
    856   1.1    briggs }
    857   1.1    briggs 
    858   1.1    briggs /************************************************************************/
    859   1.1    briggs /************************************************************************/
    860   1.1    briggs /********************   Bus configuration routines   ********************/
    861   1.1    briggs /************************************************************************/
    862   1.1    briggs /************************************************************************/
    863  1.39   msaitoh static uint64_t
    864  1.47   thorpej pci_allocate_range(struct pciconf_resource * const r, const uint64_t amt,
    865  1.44   thorpej 		   const int align, const bool ok64 __used_only_lp64)
    866   1.1    briggs {
    867  1.47   thorpej 	vmem_size_t const size = (vmem_size_t) amt;
    868  1.47   thorpej 	vmem_addr_t result;
    869  1.47   thorpej 	int error;
    870  1.44   thorpej 
    871  1.44   thorpej #ifdef _LP64
    872  1.44   thorpej 	/*
    873  1.44   thorpej 	 * If a 64-bit range IS OK, then we prefer allocating above 4GB.
    874  1.44   thorpej 	 *
    875  1.47   thorpej 	 * XXX We guard this with _LP64 because vmem uses uintptr_t
    876  1.44   thorpej 	 * internally.
    877  1.44   thorpej 	 */
    878  1.44   thorpej 	if (!ok64) {
    879  1.47   thorpej 		error = vmem_xalloc(r->arena, size, align, 0, 0,
    880  1.47   thorpej 				    VMEM_ADDR_MIN, 0xffffffffUL,
    881  1.47   thorpej 				    VM_BESTFIT | VM_NOSLEEP,
    882  1.47   thorpej 				    &result);
    883  1.47   thorpej 	} else {
    884  1.47   thorpej 		error = vmem_xalloc(r->arena, size, align, 0, 0,
    885  1.47   thorpej 				    (1UL << 32), VMEM_ADDR_MAX,
    886  1.47   thorpej 				    VM_BESTFIT | VM_NOSLEEP,
    887  1.47   thorpej 				    &result);
    888  1.47   thorpej 		if (error) {
    889  1.47   thorpej 			error = vmem_xalloc(r->arena, size, align, 0, 0,
    890  1.47   thorpej 					    VMEM_ADDR_MIN, VMEM_ADDR_MAX,
    891  1.47   thorpej 					    VM_BESTFIT | VM_NOSLEEP,
    892  1.47   thorpej 					    &result);
    893  1.44   thorpej 		}
    894  1.44   thorpej 	}
    895  1.47   thorpej #else
    896  1.47   thorpej 	error = vmem_xalloc(r->arena, size, align, 0, 0,
    897  1.47   thorpej 			    VMEM_ADDR_MIN, 0xffffffffUL,
    898  1.47   thorpej 			    VM_BESTFIT | VM_NOSLEEP,
    899  1.47   thorpej 			    &result);
    900  1.44   thorpej #endif /* _L64 */
    901  1.44   thorpej 
    902  1.47   thorpej 	if (error)
    903  1.36      matt 		return ~0ULL;
    904  1.47   thorpej 
    905  1.47   thorpej 	return result;
    906   1.1    briggs }
    907   1.1    briggs 
    908   1.1    briggs static int
    909   1.1    briggs setup_iowins(pciconf_bus_t *pb)
    910   1.1    briggs {
    911   1.1    briggs 	pciconf_win_t	*pi;
    912   1.1    briggs 	pciconf_dev_t	*pd;
    913  1.47   thorpej 	int error;
    914   1.1    briggs 
    915  1.40   msaitoh 	for (pi = pb->pciiowin; pi < &pb->pciiowin[pb->niowin]; pi++) {
    916   1.1    briggs 		if (pi->size == 0)
    917   1.1    briggs 			continue;
    918   1.1    briggs 
    919   1.1    briggs 		pd = pi->dev;
    920  1.47   thorpej 		if (pb->io_res.arena == NULL) {
    921  1.46  jmcneill 			/* Bus has no IO ranges, disable IO BAR */
    922  1.46  jmcneill 			pi->address = 0;
    923  1.46  jmcneill 			pd->enable &= ~PCI_CONF_ENABLE_IO;
    924  1.46  jmcneill 			goto write_ioaddr;
    925  1.46  jmcneill 		}
    926  1.47   thorpej 		pi->address = pci_allocate_range(&pb->io_res, pi->size,
    927  1.44   thorpej 		    pi->align, false);
    928  1.36      matt 		if (~pi->address == 0) {
    929   1.1    briggs 			print_tag(pd->pc, pd->tag);
    930  1.23       scw 			printf("Failed to allocate PCI I/O space (%"
    931  1.23       scw 			    PRIu64 " req)\n", pi->size);
    932   1.1    briggs 			return -1;
    933   1.1    briggs 		}
    934   1.1    briggs 		if (pd->ppb && pi->reg == 0) {
    935  1.47   thorpej 			error = init_range_resource(&pd->ppb->io_res,
    936  1.47   thorpej 			    "ppb-io", pi->address, pi->size);
    937  1.47   thorpej 			if (error) {
    938   1.1    briggs 				print_tag(pd->pc, pd->tag);
    939  1.47   thorpej 				printf("Failed to alloc I/O arena for bus %d\n",
    940   1.1    briggs 				    pd->ppb->busno);
    941   1.1    briggs 				return -1;
    942   1.1    briggs 			}
    943   1.1    briggs 			continue;
    944   1.1    briggs 		}
    945  1.26   tsutsui 		if (!pb->io_32bit && pi->address > 0xFFFF) {
    946  1.26   tsutsui 			pi->address = 0;
    947  1.26   tsutsui 			pd->enable &= ~PCI_CONF_ENABLE_IO;
    948  1.26   tsutsui 		} else {
    949  1.26   tsutsui 			pd->enable |= PCI_CONF_ENABLE_IO;
    950  1.26   tsutsui 		}
    951  1.46  jmcneill write_ioaddr:
    952   1.1    briggs 		if (pci_conf_debug) {
    953   1.1    briggs 			print_tag(pd->pc, pd->tag);
    954  1.23       scw 			printf("Putting %" PRIu64 " I/O bytes @ %#" PRIx64
    955  1.23       scw 			    " (reg %x)\n", pi->size, pi->address, pi->reg);
    956   1.1    briggs 		}
    957   1.1    briggs 		pci_conf_write(pd->pc, pd->tag, pi->reg,
    958   1.1    briggs 		    PCI_MAPREG_IO_ADDR(pi->address) | PCI_MAPREG_TYPE_IO);
    959   1.1    briggs 	}
    960   1.1    briggs 	return 0;
    961   1.1    briggs }
    962   1.1    briggs 
    963   1.1    briggs static int
    964   1.1    briggs setup_memwins(pciconf_bus_t *pb)
    965   1.1    briggs {
    966   1.1    briggs 	pciconf_win_t	*pm;
    967   1.1    briggs 	pciconf_dev_t	*pd;
    968   1.1    briggs 	pcireg_t	base;
    969  1.47   thorpej 	struct pciconf_resource *r;
    970  1.44   thorpej 	bool		ok64;
    971  1.47   thorpej 	int		error;
    972   1.1    briggs 
    973  1.40   msaitoh 	for (pm = pb->pcimemwin; pm < &pb->pcimemwin[pb->nmemwin]; pm++) {
    974   1.1    briggs 		if (pm->size == 0)
    975   1.1    briggs 			continue;
    976   1.1    briggs 
    977  1.44   thorpej 		ok64 = false;
    978   1.1    briggs 		pd = pm->dev;
    979  1.44   thorpej 		if (pm->prefetch) {
    980  1.47   thorpej 			r = &pb->pmem_res;
    981  1.44   thorpej 			ok64 = pb->pmem_64bit;
    982  1.44   thorpej 		} else {
    983  1.47   thorpej 			r = &pb->mem_res;
    984  1.44   thorpej 			ok64 = pb->mem_64bit && pd->ppb == NULL;
    985  1.44   thorpej 		}
    986  1.44   thorpej 
    987  1.44   thorpej 		/*
    988  1.44   thorpej 		 * We need to figure out if the memory BAR is 64-bit
    989  1.44   thorpej 		 * capable or not.  If it's not, then we need to constrain
    990  1.44   thorpej 		 * the address allocation.
    991  1.44   thorpej 		 */
    992  1.44   thorpej 		if (pm->reg == PCI_MAPREG_ROM) {
    993  1.44   thorpej 			ok64 = false;
    994  1.44   thorpej 		} else if (ok64) {
    995  1.44   thorpej 			base = pci_conf_read(pd->pc, pd->tag, pm->reg);
    996  1.44   thorpej 			ok64 = PCI_MAPREG_MEM_TYPE(base) ==
    997  1.44   thorpej 			    PCI_MAPREG_MEM_TYPE_64BIT;
    998  1.44   thorpej 		}
    999  1.44   thorpej 
   1000  1.47   thorpej 		pm->address = pci_allocate_range(r, pm->size, pm->align,
   1001  1.44   thorpej 						 ok64);
   1002  1.36      matt 		if (~pm->address == 0) {
   1003   1.1    briggs 			print_tag(pd->pc, pd->tag);
   1004   1.1    briggs 			printf(
   1005  1.23       scw 			   "Failed to allocate PCI memory space (%" PRIu64
   1006  1.44   thorpej 			   " req, prefetch=%d ok64=%d)\n", pm->size,
   1007  1.44   thorpej 			   pm->prefetch, (int)ok64);
   1008   1.1    briggs 			return -1;
   1009   1.1    briggs 		}
   1010   1.1    briggs 		if (pd->ppb && pm->reg == 0) {
   1011  1.47   thorpej 			const char *name = pm->prefetch ? "ppb-pmem"
   1012  1.47   thorpej 							: "ppb-mem";
   1013  1.47   thorpej 			r = pm->prefetch ? &pd->ppb->pmem_res
   1014  1.47   thorpej 					 : &pd->ppb->mem_res;
   1015  1.47   thorpej 			error = init_range_resource(r, name,
   1016  1.47   thorpej 			    pm->address, pm->size);
   1017  1.47   thorpej 			if (error) {
   1018   1.1    briggs 				print_tag(pd->pc, pd->tag);
   1019  1.47   thorpej 				printf("Failed to alloc MEM arena for bus %d\n",
   1020   1.1    briggs 				    pd->ppb->busno);
   1021   1.1    briggs 				return -1;
   1022   1.1    briggs 			}
   1023   1.1    briggs 			continue;
   1024   1.1    briggs 		}
   1025  1.44   thorpej 		if (!ok64 && pm->address > 0xFFFFFFFFULL) {
   1026   1.2    briggs 			pm->address = 0;
   1027  1.26   tsutsui 			pd->enable &= ~PCI_CONF_ENABLE_MEM;
   1028  1.39   msaitoh 		} else
   1029   1.8    briggs 			pd->enable |= PCI_CONF_ENABLE_MEM;
   1030  1.39   msaitoh 
   1031   1.1    briggs 		if (pm->reg != PCI_MAPREG_ROM) {
   1032   1.1    briggs 			if (pci_conf_debug) {
   1033   1.1    briggs 				print_tag(pd->pc, pd->tag);
   1034   1.1    briggs 				printf(
   1035  1.23       scw 				    "Putting %" PRIu64 " MEM bytes @ %#"
   1036  1.23       scw 				    PRIx64 " (reg %x)\n", pm->size,
   1037  1.23       scw 				    pm->address, pm->reg);
   1038   1.1    briggs 			}
   1039   1.1    briggs 			base = pci_conf_read(pd->pc, pd->tag, pm->reg);
   1040   1.1    briggs 			base = PCI_MAPREG_MEM_ADDR(pm->address) |
   1041   1.1    briggs 			    PCI_MAPREG_MEM_TYPE(base);
   1042   1.1    briggs 			pci_conf_write(pd->pc, pd->tag, pm->reg, base);
   1043   1.1    briggs 			if (PCI_MAPREG_MEM_TYPE(base) ==
   1044   1.1    briggs 			    PCI_MAPREG_MEM_TYPE_64BIT) {
   1045   1.1    briggs 				base = (pcireg_t)
   1046   1.1    briggs 				    (PCI_MAPREG_MEM64_ADDR(pm->address) >> 32);
   1047   1.1    briggs 				pci_conf_write(pd->pc, pd->tag, pm->reg + 4,
   1048   1.1    briggs 				    base);
   1049   1.1    briggs 			}
   1050   1.1    briggs 		}
   1051   1.1    briggs 	}
   1052  1.40   msaitoh 	for (pm = pb->pcimemwin; pm < &pb->pcimemwin[pb->nmemwin]; pm++) {
   1053   1.1    briggs 		if (pm->reg == PCI_MAPREG_ROM && pm->address != -1) {
   1054   1.1    briggs 			pd = pm->dev;
   1055  1.29   gdamore 			if (!(pd->enable & PCI_CONF_MAP_ROM))
   1056  1.28   gdamore 				continue;
   1057   1.1    briggs 			if (pci_conf_debug) {
   1058   1.1    briggs 				print_tag(pd->pc, pd->tag);
   1059   1.1    briggs 				printf(
   1060  1.23       scw 				    "Putting %" PRIu64 " ROM bytes @ %#"
   1061  1.23       scw 				    PRIx64 " (reg %x)\n", pm->size,
   1062  1.23       scw 				    pm->address, pm->reg);
   1063   1.1    briggs 			}
   1064  1.29   gdamore 			base = (pcireg_t) pm->address;
   1065  1.29   gdamore 			if (pd->enable & PCI_CONF_ENABLE_ROM)
   1066  1.29   gdamore 				base |= PCI_MAPREG_ROM_ENABLE;
   1067  1.29   gdamore 
   1068   1.1    briggs 			pci_conf_write(pd->pc, pd->tag, pm->reg, base);
   1069   1.1    briggs 		}
   1070   1.1    briggs 	}
   1071   1.1    briggs 	return 0;
   1072   1.1    briggs }
   1073   1.1    briggs 
   1074  1.44   thorpej static bool
   1075  1.47   thorpej constrain_bridge_mem_range(struct pciconf_resource * const r,
   1076  1.44   thorpej 			   u_long * const base,
   1077  1.44   thorpej 			   u_long * const limit,
   1078  1.44   thorpej 			   const bool ok64 __used_only_lp64)
   1079  1.44   thorpej {
   1080  1.44   thorpej 
   1081  1.47   thorpej 	*base = r->min_addr;
   1082  1.47   thorpej 	*limit = r->max_addr;
   1083  1.44   thorpej 
   1084  1.44   thorpej #ifdef _LP64
   1085  1.44   thorpej 	if (!ok64) {
   1086  1.47   thorpej 		if (r->min_addr >= (1UL << 32)) {
   1087  1.44   thorpej 			return true;
   1088  1.44   thorpej 		}
   1089  1.47   thorpej 		if (r->max_addr > 0xffffffffUL) {
   1090  1.44   thorpej 			*limit = 0xffffffffUL;
   1091  1.44   thorpej 		}
   1092  1.44   thorpej 	}
   1093  1.44   thorpej #endif /* _LP64 */
   1094  1.44   thorpej 
   1095  1.44   thorpej 	return false;
   1096  1.44   thorpej }
   1097  1.44   thorpej 
   1098   1.1    briggs /*
   1099   1.1    briggs  * Configure I/O, memory, and prefetcable memory spaces, then make
   1100   1.1    briggs  * a call to configure_bus().
   1101   1.1    briggs  */
   1102   1.1    briggs static int
   1103   1.1    briggs configure_bridge(pciconf_dev_t *pd)
   1104   1.1    briggs {
   1105   1.1    briggs 	unsigned long	io_base, io_limit, mem_base, mem_limit;
   1106   1.1    briggs 	pciconf_bus_t	*pb;
   1107   1.1    briggs 	pcireg_t	io, iohigh, mem, cmd;
   1108   1.1    briggs 	int		rv;
   1109  1.38   msaitoh 	bool		isprefetchmem64;
   1110  1.44   thorpej 	bool		bad_range;
   1111   1.1    briggs 
   1112   1.1    briggs 	pb = pd->ppb;
   1113   1.1    briggs 	/* Configure I/O base & limit*/
   1114  1.47   thorpej 	if (pb->io_res.arena) {
   1115  1.47   thorpej 		io_base = pb->io_res.min_addr;
   1116  1.47   thorpej 		io_limit = pb->io_res.max_addr;
   1117   1.2    briggs 	} else {
   1118   1.2    briggs 		io_base  = 0x1000;	/* 4K */
   1119   1.2    briggs 		io_limit = 0x0000;
   1120   1.1    briggs 	}
   1121   1.2    briggs 	if (pb->io_32bit) {
   1122  1.41   msaitoh 		iohigh = __SHIFTIN(io_base >> 16, PCI_BRIDGE_IOHIGH_BASE) |
   1123  1.41   msaitoh 		    __SHIFTIN(io_limit >> 16, PCI_BRIDGE_IOHIGH_LIMIT);
   1124   1.2    briggs 	} else {
   1125   1.2    briggs 		if (io_limit > 0xFFFF) {
   1126   1.2    briggs 			printf("Bus %d bridge does not support 32-bit I/O.  ",
   1127   1.2    briggs 			    pb->busno);
   1128   1.2    briggs 			printf("Disabling I/O accesses\n");
   1129   1.2    briggs 			io_base  = 0x1000;	/* 4K */
   1130   1.2    briggs 			io_limit = 0x0000;
   1131   1.2    briggs 		}
   1132   1.2    briggs 		iohigh = 0;
   1133   1.2    briggs 	}
   1134   1.9    briggs 	io = pci_conf_read(pb->pc, pd->tag, PCI_BRIDGE_STATIO_REG) &
   1135  1.41   msaitoh 	    PCI_BRIDGE_STATIO_STATUS;
   1136  1.41   msaitoh 	io |= __SHIFTIN((io_base >> 8) & PCI_BRIDGE_STATIO_IOADDR,
   1137  1.41   msaitoh 	    PCI_BRIDGE_STATIO_IOBASE);
   1138  1.41   msaitoh 	io |= __SHIFTIN((io_limit >> 8) & PCI_BRIDGE_STATIO_IOADDR,
   1139  1.41   msaitoh 	    PCI_BRIDGE_STATIO_IOLIMIT);
   1140   1.2    briggs 	pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_STATIO_REG, io);
   1141   1.2    briggs 	pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_IOHIGH_REG, iohigh);
   1142   1.1    briggs 
   1143   1.1    briggs 	/* Configure mem base & limit */
   1144  1.44   thorpej 	bad_range = false;
   1145  1.47   thorpej 	if (pb->mem_res.arena) {
   1146  1.47   thorpej 		bad_range = constrain_bridge_mem_range(&pb->mem_res,
   1147  1.44   thorpej 						       &mem_base,
   1148  1.44   thorpej 						       &mem_limit,
   1149  1.44   thorpej 						       false);
   1150   1.2    briggs 	} else {
   1151   1.2    briggs 		mem_base  = 0x100000;	/* 1M */
   1152   1.2    briggs 		mem_limit = 0x000000;
   1153   1.1    briggs 	}
   1154  1.44   thorpej 	if (bad_range) {
   1155   1.2    briggs 		printf("Bus %d bridge MEM range out of range.  ", pb->busno);
   1156   1.2    briggs 		printf("Disabling MEM accesses\n");
   1157   1.2    briggs 		mem_base  = 0x100000;	/* 1M */
   1158   1.2    briggs 		mem_limit = 0x000000;
   1159   1.2    briggs 	}
   1160  1.41   msaitoh 	mem = __SHIFTIN((mem_base >> 16) & PCI_BRIDGE_MEMORY_ADDR,
   1161  1.41   msaitoh 	    PCI_BRIDGE_MEMORY_BASE);
   1162  1.41   msaitoh 	mem |= __SHIFTIN((mem_limit >> 16) & PCI_BRIDGE_MEMORY_ADDR,
   1163  1.41   msaitoh 	    PCI_BRIDGE_MEMORY_LIMIT);
   1164   1.2    briggs 	pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_MEMORY_REG, mem);
   1165   1.1    briggs 
   1166   1.1    briggs 	/* Configure prefetchable mem base & limit */
   1167  1.44   thorpej 	mem = pci_conf_read(pb->pc, pd->tag, PCI_BRIDGE_PREFETCHMEM_REG);
   1168  1.44   thorpej 	isprefetchmem64 = PCI_BRIDGE_PREFETCHMEM_64BITS(mem);
   1169  1.44   thorpej 	bad_range = false;
   1170  1.47   thorpej 	if (pb->pmem_res.arena) {
   1171  1.47   thorpej 		bad_range = constrain_bridge_mem_range(&pb->pmem_res,
   1172  1.44   thorpej 						       &mem_base,
   1173  1.44   thorpej 						       &mem_limit,
   1174  1.44   thorpej 						       isprefetchmem64);
   1175   1.2    briggs 	} else {
   1176   1.2    briggs 		mem_base  = 0x100000;	/* 1M */
   1177   1.2    briggs 		mem_limit = 0x000000;
   1178   1.1    briggs 	}
   1179  1.44   thorpej 	if (bad_range) {
   1180   1.2    briggs 		printf("Bus %d bridge does not support 64-bit PMEM.  ",
   1181   1.2    briggs 		    pb->busno);
   1182   1.2    briggs 		printf("Disabling prefetchable-MEM accesses\n");
   1183   1.2    briggs 		mem_base  = 0x100000;	/* 1M */
   1184   1.2    briggs 		mem_limit = 0x000000;
   1185   1.2    briggs 	}
   1186  1.41   msaitoh 	mem = __SHIFTIN((mem_base >> 16) & PCI_BRIDGE_PREFETCHMEM_ADDR,
   1187  1.41   msaitoh 	    PCI_BRIDGE_PREFETCHMEM_BASE);
   1188  1.41   msaitoh 	mem |= __SHIFTIN((mem_limit >> 16) & PCI_BRIDGE_PREFETCHMEM_ADDR,
   1189  1.41   msaitoh 	    PCI_BRIDGE_PREFETCHMEM_LIMIT);
   1190   1.2    briggs 	pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_PREFETCHMEM_REG, mem);
   1191   1.2    briggs 	/*
   1192   1.2    briggs 	 * XXX -- 64-bit systems need a lot more than just this...
   1193   1.2    briggs 	 */
   1194  1.38   msaitoh 	if (isprefetchmem64) {
   1195  1.39   msaitoh 		mem_base  = (uint64_t)mem_base  >> 32;
   1196  1.39   msaitoh 		mem_limit = (uint64_t)mem_limit >> 32;
   1197  1.41   msaitoh 		pci_conf_write(pb->pc, pd->tag,
   1198  1.41   msaitoh 		    PCI_BRIDGE_PREFETCHBASEUP32_REG, mem_base & 0xffffffff);
   1199  1.41   msaitoh 		pci_conf_write(pb->pc, pd->tag,
   1200  1.41   msaitoh 		    PCI_BRIDGE_PREFETCHLIMITUP32_REG, mem_limit & 0xffffffff);
   1201  1.32      matt 	}
   1202   1.1    briggs 
   1203   1.1    briggs 	rv = configure_bus(pb);
   1204   1.1    briggs 
   1205  1.47   thorpej 	fini_range_resource(&pb->io_res);
   1206  1.47   thorpej 	fini_range_resource(&pb->mem_res);
   1207  1.47   thorpej 	fini_range_resource(&pb->pmem_res);
   1208  1.47   thorpej 
   1209   1.1    briggs 	if (rv == 0) {
   1210   1.1    briggs 		cmd = pci_conf_read(pd->pc, pd->tag, PCI_BRIDGE_CONTROL_REG);
   1211  1.41   msaitoh 		cmd &= ~PCI_BRIDGE_CONTROL; /* Clear control bit first */
   1212  1.41   msaitoh 		cmd |= PCI_BRIDGE_CONTROL_PERE | PCI_BRIDGE_CONTROL_SERR;
   1213  1.41   msaitoh 		if (pb->fast_b2b)
   1214  1.41   msaitoh 			cmd |= PCI_BRIDGE_CONTROL_SECFASTB2B;
   1215  1.41   msaitoh 
   1216   1.1    briggs 		pci_conf_write(pd->pc, pd->tag, PCI_BRIDGE_CONTROL_REG, cmd);
   1217   1.1    briggs 		cmd = pci_conf_read(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG);
   1218   1.1    briggs 		cmd |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE;
   1219   1.1    briggs 		pci_conf_write(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG, cmd);
   1220   1.1    briggs 	}
   1221   1.1    briggs 
   1222   1.1    briggs 	return rv;
   1223   1.1    briggs }
   1224   1.1    briggs 
   1225   1.1    briggs /*
   1226   1.1    briggs  * Calculate latency values, allocate I/O and MEM segments, then set them
   1227   1.1    briggs  * up.  If a PCI-PCI bridge is found, configure the bridge separately,
   1228   1.1    briggs  * which will cause a recursive call back here.
   1229   1.1    briggs  */
   1230   1.1    briggs static int
   1231   1.1    briggs configure_bus(pciconf_bus_t *pb)
   1232   1.1    briggs {
   1233   1.1    briggs 	pciconf_dev_t	*pd;
   1234   1.8    briggs 	int		def_ltim, max_ltim, band, bus_mhz;
   1235   1.1    briggs 
   1236  1.20    simonb 	if (pb->ndevs == 0) {
   1237  1.20    simonb 		if (pci_conf_debug)
   1238  1.20    simonb 			printf("PCI bus %d - no devices\n", pb->busno);
   1239  1.39   msaitoh 		return 1;
   1240  1.20    simonb 	}
   1241   1.8    briggs 	bus_mhz = pb->freq_66 ? 66 : 33;
   1242   1.8    briggs 	max_ltim = pb->max_mingnt * bus_mhz / 4;	/* cvt to cycle count */
   1243  1.30    briggs 	band = 4000000;					/* 0.25us cycles/sec */
   1244   1.1    briggs 	if (band < pb->bandwidth_used) {
   1245  1.31     gavan 		printf("PCI bus %d: Warning: Total bandwidth exceeded!? (%d)\n",
   1246  1.31     gavan 		    pb->busno, pb->bandwidth_used);
   1247   1.1    briggs 		def_ltim = -1;
   1248   1.1    briggs 	} else {
   1249   1.1    briggs 		def_ltim = (band - pb->bandwidth_used) / pb->ndevs;
   1250   1.1    briggs 		if (def_ltim > pb->min_maxlat)
   1251   1.1    briggs 			def_ltim = pb->min_maxlat;
   1252   1.8    briggs 		def_ltim = def_ltim * bus_mhz / 4;
   1253   1.1    briggs 	}
   1254   1.1    briggs 	def_ltim = (def_ltim + 7) & ~7;
   1255   1.1    briggs 	max_ltim = (max_ltim + 7) & ~7;
   1256   1.1    briggs 
   1257  1.43   msaitoh 	pb->def_ltim = MIN(def_ltim, 255);
   1258  1.43   msaitoh 	pb->max_ltim = MIN(MAX(max_ltim, def_ltim), 255);
   1259   1.1    briggs 
   1260   1.1    briggs 	/*
   1261   1.1    briggs 	 * Now we have what we need to initialize the devices.
   1262   1.1    briggs 	 * It would probably be better if we could allocate all of these
   1263   1.1    briggs 	 * for all busses at once, but "not right now".  First, get a list
   1264   1.1    briggs 	 * of free memory ranges from the m.d. system.
   1265   1.1    briggs 	 */
   1266   1.1    briggs 	if (setup_iowins(pb) || setup_memwins(pb)) {
   1267  1.36      matt 		printf("PCI bus configuration failed: "
   1268  1.36      matt 		"unable to assign all I/O and memory ranges.\n");
   1269   1.1    briggs 		return -1;
   1270   1.1    briggs 	}
   1271   1.1    briggs 
   1272   1.1    briggs 	/*
   1273   1.1    briggs 	 * Configure the latency for the devices, and enable them.
   1274   1.1    briggs 	 */
   1275  1.40   msaitoh 	for (pd = pb->device; pd < &pb->device[pb->ndevs]; pd++) {
   1276  1.37      matt 		pcireg_t cmd, classreg, misc;
   1277   1.1    briggs 		int	ltim;
   1278   1.1    briggs 
   1279   1.1    briggs 		if (pci_conf_debug) {
   1280   1.1    briggs 			print_tag(pd->pc, pd->tag);
   1281   1.1    briggs 			printf("Configuring device.\n");
   1282   1.1    briggs 		}
   1283  1.37      matt 		classreg = pci_conf_read(pd->pc, pd->tag, PCI_CLASS_REG);
   1284   1.1    briggs 		misc = pci_conf_read(pd->pc, pd->tag, PCI_BHLC_REG);
   1285   1.1    briggs 		cmd = pci_conf_read(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG);
   1286  1.26   tsutsui 		if (pd->enable & PCI_CONF_ENABLE_PARITY)
   1287  1.26   tsutsui 			cmd |= PCI_COMMAND_PARITY_ENABLE;
   1288  1.26   tsutsui 		if (pd->enable & PCI_CONF_ENABLE_SERR)
   1289  1.26   tsutsui 			cmd |= PCI_COMMAND_SERR_ENABLE;
   1290   1.1    briggs 		if (pb->fast_b2b)
   1291   1.1    briggs 			cmd |= PCI_COMMAND_BACKTOBACK_ENABLE;
   1292  1.37      matt 		if (PCI_CLASS(classreg) != PCI_CLASS_BRIDGE ||
   1293  1.37      matt 		    PCI_SUBCLASS(classreg) != PCI_SUBCLASS_BRIDGE_PCI) {
   1294   1.8    briggs 			if (pd->enable & PCI_CONF_ENABLE_IO)
   1295   1.8    briggs 				cmd |= PCI_COMMAND_IO_ENABLE;
   1296   1.8    briggs 			if (pd->enable & PCI_CONF_ENABLE_MEM)
   1297   1.8    briggs 				cmd |= PCI_COMMAND_MEM_ENABLE;
   1298   1.8    briggs 			if (pd->enable & PCI_CONF_ENABLE_BM)
   1299   1.8    briggs 				cmd |= PCI_COMMAND_MASTER_ENABLE;
   1300   1.8    briggs 			ltim = pd->min_gnt * bus_mhz / 4;
   1301   1.1    briggs 			ltim = MIN (MAX (pb->def_ltim, ltim), pb->max_ltim);
   1302   1.1    briggs 		} else {
   1303   1.8    briggs 			cmd |= PCI_COMMAND_MASTER_ENABLE;
   1304   1.1    briggs 			ltim = MIN (pb->def_ltim, pb->max_ltim);
   1305   1.1    briggs 		}
   1306  1.26   tsutsui 		if ((pd->enable &
   1307  1.43   msaitoh 		    (PCI_CONF_ENABLE_MEM | PCI_CONF_ENABLE_IO)) == 0) {
   1308   1.2    briggs 			print_tag(pd->pc, pd->tag);
   1309   1.2    briggs 			printf("Disabled due to lack of resources.\n");
   1310   1.2    briggs 			cmd &= ~(PCI_COMMAND_MASTER_ENABLE |
   1311   1.2    briggs 			    PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE);
   1312   1.2    briggs 		}
   1313   1.1    briggs 		pci_conf_write(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG, cmd);
   1314   1.1    briggs 
   1315  1.14   thorpej 		misc &= ~((PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT) |
   1316  1.14   thorpej 		    (PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT));
   1317  1.14   thorpej 		misc |= (ltim & PCI_LATTIMER_MASK) << PCI_LATTIMER_SHIFT;
   1318  1.15    kleink 		misc |= ((pb->cacheline_size >> 2) & PCI_CACHELINE_MASK) <<
   1319  1.14   thorpej 		    PCI_CACHELINE_SHIFT;
   1320   1.1    briggs 		pci_conf_write(pd->pc, pd->tag, PCI_BHLC_REG, misc);
   1321   1.1    briggs 
   1322   1.1    briggs 		if (pd->ppb) {
   1323   1.1    briggs 			if (configure_bridge(pd) < 0)
   1324   1.1    briggs 				return -1;
   1325   1.1    briggs 			continue;
   1326   1.1    briggs 		}
   1327   1.1    briggs 	}
   1328   1.1    briggs 
   1329  1.39   msaitoh 	if (pci_conf_debug)
   1330   1.1    briggs 		printf("PCI bus %d configured\n", pb->busno);
   1331   1.1    briggs 
   1332   1.1    briggs 	return 0;
   1333   1.1    briggs }
   1334   1.1    briggs 
   1335  1.44   thorpej static bool
   1336  1.47   thorpej mem_region_ok64(struct pciconf_resource * const r __used_only_lp64)
   1337  1.44   thorpej {
   1338  1.44   thorpej 	bool rv = false;
   1339  1.44   thorpej 
   1340  1.44   thorpej #ifdef _LP64
   1341  1.44   thorpej 	/*
   1342  1.47   thorpej 	 * XXX We need to guard this with _LP64 because vmem uses
   1343  1.47   thorpej 	 * uintptr_t internally.
   1344  1.44   thorpej 	 */
   1345  1.47   thorpej 	vmem_size_t result;
   1346  1.47   thorpej 	if (vmem_xalloc(r->arena, 1/*size*/, 1/*align*/, 0/*phase*/,
   1347  1.47   thorpej 			0/*nocross*/, (1UL << 32), VMEM_ADDR_MAX,
   1348  1.47   thorpej 			VM_INSTANTFIT | VM_NOSLEEP, &result) == 0) {
   1349  1.47   thorpej 		vmem_free(r->arena, result, 1);
   1350  1.44   thorpej 		rv = true;
   1351  1.44   thorpej 	}
   1352  1.44   thorpej #endif /* _LP64 */
   1353  1.44   thorpej 
   1354  1.44   thorpej 	return rv;
   1355  1.44   thorpej }
   1356  1.44   thorpej 
   1357   1.1    briggs /*
   1358  1.47   thorpej  * pciconf_resource_init:
   1359  1.47   thorpej  *
   1360  1.47   thorpej  *	Allocate and initilize a pci configuration resources container.
   1361  1.47   thorpej  */
   1362  1.47   thorpej struct pciconf_resources *
   1363  1.47   thorpej pciconf_resource_init(void)
   1364  1.47   thorpej {
   1365  1.47   thorpej 	struct pciconf_resources *rs;
   1366  1.47   thorpej 
   1367  1.47   thorpej 	rs = kmem_zalloc(sizeof(*rs), KM_SLEEP);
   1368  1.47   thorpej 
   1369  1.47   thorpej 	return (rs);
   1370  1.47   thorpej }
   1371  1.47   thorpej 
   1372  1.47   thorpej /*
   1373  1.47   thorpej  * pciconf_resource_fini:
   1374  1.47   thorpej  *
   1375  1.47   thorpej  *	Dispose of a pci configuration resources container.
   1376  1.47   thorpej  */
   1377  1.47   thorpej void
   1378  1.47   thorpej pciconf_resource_fini(struct pciconf_resources *rs)
   1379  1.47   thorpej {
   1380  1.47   thorpej 	int i;
   1381  1.47   thorpej 
   1382  1.47   thorpej 	for (i = 0; i < PCICONF_RESOURCE_NTYPES; i++) {
   1383  1.47   thorpej 		fini_range_resource(&rs->resources[i]);
   1384  1.47   thorpej 	}
   1385  1.47   thorpej 
   1386  1.47   thorpej 	kmem_free(rs, sizeof(*rs));
   1387  1.47   thorpej }
   1388  1.47   thorpej 
   1389  1.47   thorpej /*
   1390  1.47   thorpej  * pciconf_resource_add:
   1391  1.47   thorpej  *
   1392  1.47   thorpej  *	Add a pci configuration resource to a container.
   1393  1.47   thorpej  */
   1394  1.47   thorpej int
   1395  1.47   thorpej pciconf_resource_add(struct pciconf_resources *rs, int type,
   1396  1.47   thorpej     bus_addr_t start, bus_size_t size)
   1397  1.47   thorpej {
   1398  1.47   thorpej 	bus_addr_t end = start + (size - 1);
   1399  1.47   thorpej 	struct pciconf_resource *r;
   1400  1.49  jmcneill 	struct pciconf_resource_rsvd *rsvd;
   1401  1.49  jmcneill 	int error, rsvd_type, align;
   1402  1.49  jmcneill 	vmem_addr_t result;
   1403  1.47   thorpej 	bool first;
   1404  1.47   thorpej 
   1405  1.47   thorpej 	if (size == 0 || end <= start)
   1406  1.47   thorpej 		return EINVAL;
   1407  1.47   thorpej 
   1408  1.47   thorpej 	if (type < 0 || type >= PCICONF_RESOURCE_NTYPES)
   1409  1.47   thorpej 		return EINVAL;
   1410  1.47   thorpej 
   1411  1.47   thorpej 	r = &rs->resources[type];
   1412  1.47   thorpej 
   1413  1.47   thorpej 	first = r->arena == NULL;
   1414  1.47   thorpej 	if (first) {
   1415  1.47   thorpej 		r->arena = create_vmem_arena(pciconf_resource_names[type],
   1416  1.47   thorpej 		    0, 0, VM_SLEEP);
   1417  1.47   thorpej 		r->min_addr = VMEM_ADDR_MAX;
   1418  1.47   thorpej 		r->max_addr = VMEM_ADDR_MIN;
   1419  1.47   thorpej 	}
   1420  1.47   thorpej 
   1421  1.47   thorpej 	error = vmem_add(r->arena, start, size, VM_SLEEP);
   1422  1.47   thorpej 	if (error == 0) {
   1423  1.47   thorpej 		if (start < r->min_addr)
   1424  1.47   thorpej 			r->min_addr = start;
   1425  1.47   thorpej 		if (end > r->max_addr)
   1426  1.47   thorpej 			r->max_addr = end;
   1427  1.47   thorpej 	}
   1428  1.47   thorpej 
   1429  1.47   thorpej 	r->total_size += size;
   1430  1.47   thorpej 
   1431  1.49  jmcneill 	switch (type) {
   1432  1.49  jmcneill 	case PCICONF_RESOURCE_IO:
   1433  1.49  jmcneill 		rsvd_type = PCI_CONF_MAP_IO;
   1434  1.49  jmcneill 		align = 0x1000;
   1435  1.49  jmcneill 		break;
   1436  1.49  jmcneill 	case PCICONF_RESOURCE_MEM:
   1437  1.49  jmcneill 	case PCICONF_RESOURCE_PREFETCHABLE_MEM:
   1438  1.49  jmcneill 		rsvd_type = PCI_CONF_MAP_MEM;
   1439  1.49  jmcneill 		align = 0x100000;
   1440  1.49  jmcneill 		break;
   1441  1.49  jmcneill 	default:
   1442  1.49  jmcneill 		rsvd_type = 0;
   1443  1.49  jmcneill 		align = 0;
   1444  1.49  jmcneill 		break;
   1445  1.49  jmcneill 	}
   1446  1.49  jmcneill 
   1447  1.49  jmcneill 	/*
   1448  1.49  jmcneill 	 * Exclude reserved ranges from available resources
   1449  1.49  jmcneill 	 */
   1450  1.49  jmcneill 	LIST_FOREACH(rsvd, &pciconf_resource_reservations, next) {
   1451  1.49  jmcneill 		if (rsvd->type != rsvd_type)
   1452  1.49  jmcneill 			continue;
   1453  1.49  jmcneill 		/*
   1454  1.49  jmcneill 		 * The reserved range may not be within our resource window.
   1455  1.49  jmcneill 		 * That's fine, so ignore the error.
   1456  1.49  jmcneill 		 */
   1457  1.49  jmcneill 		(void)vmem_xalloc(r->arena, rsvd->size, align, 0, 0,
   1458  1.49  jmcneill 				  rsvd->start, rsvd->start + rsvd->size,
   1459  1.49  jmcneill 				  VM_BESTFIT | VM_NOSLEEP,
   1460  1.49  jmcneill 				  &result);
   1461  1.49  jmcneill 	}
   1462  1.49  jmcneill 
   1463  1.47   thorpej 	return 0;
   1464  1.47   thorpej }
   1465  1.47   thorpej 
   1466  1.47   thorpej /*
   1467  1.49  jmcneill  * pciconf_resource_reserve:
   1468  1.49  jmcneill  *
   1469  1.49  jmcneill  *	Mark a pci configuration resource as in-use. Devices
   1470  1.49  jmcneill  *	already configured to use these resources are skipped
   1471  1.49  jmcneill  *	during resource assignment.
   1472  1.49  jmcneill  */
   1473  1.49  jmcneill void
   1474  1.49  jmcneill pciconf_resource_reserve(int type, bus_addr_t start, bus_size_t size)
   1475  1.49  jmcneill {
   1476  1.49  jmcneill 	struct pciconf_resource_rsvd *rsvd;
   1477  1.49  jmcneill 
   1478  1.49  jmcneill 	rsvd = kmem_zalloc(sizeof(*rsvd), KM_SLEEP);
   1479  1.49  jmcneill 	rsvd->type = type;
   1480  1.49  jmcneill 	rsvd->start = start;
   1481  1.49  jmcneill 	rsvd->size = size;
   1482  1.49  jmcneill 	LIST_INSERT_HEAD(&pciconf_resource_reservations, rsvd, next);
   1483  1.49  jmcneill }
   1484  1.49  jmcneill 
   1485  1.49  jmcneill /*
   1486   1.1    briggs  * Let's configure the PCI bus.
   1487   1.1    briggs  * This consists of basically scanning for all existing devices,
   1488   1.1    briggs  * identifying their needs, and then making another pass over them
   1489   1.1    briggs  * to set:
   1490   1.1    briggs  *	1. I/O addresses
   1491   1.1    briggs  *	2. Memory addresses (Prefetchable and not)
   1492   1.1    briggs  *	3. PCI command register
   1493   1.1    briggs  *	4. The latency part of the PCI BHLC (BIST (Built-In Self Test),
   1494   1.1    briggs  *	    Header type, Latency timer, Cache line size) register
   1495   1.1    briggs  *
   1496   1.1    briggs  * The command register is set to enable fast back-to-back transactions
   1497  1.25     perry  * if the host bridge says it can handle it.  We also configure
   1498   1.1    briggs  * Master Enable, SERR enable, parity enable, and (if this is not a
   1499   1.1    briggs  * PCI-PCI bridge) the I/O and Memory spaces.  Apparently some devices
   1500   1.1    briggs  * will not report some I/O space.
   1501   1.1    briggs  *
   1502   1.1    briggs  * The latency is computed to be a "fair share" of the bus bandwidth.
   1503   1.1    briggs  * The bus bandwidth variable is initialized to the number of PCI cycles
   1504   1.1    briggs  * in one second.  The number of cycles taken for one transaction by each
   1505   1.1    briggs  * device (MAX_LAT + MIN_GNT) is then subtracted from the bandwidth.
   1506   1.1    briggs  * Care is taken to ensure that the latency timer won't be set such that
   1507   1.1    briggs  * it would exceed the critical time for any device.
   1508   1.1    briggs  *
   1509   1.1    briggs  * This is complicated somewhat due to the presence of bridges.  PCI-PCI
   1510   1.1    briggs  * bridges are probed and configured recursively.
   1511   1.1    briggs  */
   1512   1.1    briggs int
   1513  1.47   thorpej pci_configure_bus(pci_chipset_tag_t pc, struct pciconf_resources *rs,
   1514  1.47   thorpej     int firstbus, int cacheline_size)
   1515   1.1    briggs {
   1516   1.1    briggs 	pciconf_bus_t	*pb;
   1517   1.1    briggs 	int		rv;
   1518   1.1    briggs 
   1519  1.42       chs 	pb = kmem_zalloc(sizeof (pciconf_bus_t), KM_SLEEP);
   1520  1.12   thorpej 	pb->busno = firstbus;
   1521   1.1    briggs 	pb->next_busno = pb->busno + 1;
   1522   1.1    briggs 	pb->last_busno = 255;
   1523  1.14   thorpej 	pb->cacheline_size = cacheline_size;
   1524   1.1    briggs 	pb->parent_bus = NULL;
   1525   1.1    briggs 	pb->swiz = 0;
   1526   1.2    briggs 	pb->io_32bit = 1;
   1527  1.47   thorpej 	pb->io_res = rs->resources[PCICONF_RESOURCE_IO];
   1528  1.47   thorpej 
   1529  1.47   thorpej 	pb->mem_res = rs->resources[PCICONF_RESOURCE_MEM];
   1530  1.47   thorpej 	if (pb->mem_res.arena == NULL)
   1531  1.47   thorpej 		pb->mem_res = rs->resources[PCICONF_RESOURCE_PREFETCHABLE_MEM];
   1532  1.47   thorpej 
   1533  1.47   thorpej 	pb->pmem_res = rs->resources[PCICONF_RESOURCE_PREFETCHABLE_MEM];
   1534  1.47   thorpej 	if (pb->pmem_res.arena == NULL)
   1535  1.47   thorpej 		pb->pmem_res = rs->resources[PCICONF_RESOURCE_MEM];
   1536  1.39   msaitoh 
   1537  1.44   thorpej 	/*
   1538  1.47   thorpej 	 * Probe the memory region arenas to see if allocation of
   1539  1.47   thorpej 	 * 64-bit addresses is possible.
   1540  1.44   thorpej 	 */
   1541  1.47   thorpej 	pb->mem_64bit = mem_region_ok64(&pb->mem_res);
   1542  1.47   thorpej 	pb->pmem_64bit = mem_region_ok64(&pb->pmem_res);
   1543  1.44   thorpej 
   1544   1.1    briggs 	pb->pc = pc;
   1545   1.1    briggs 	pb->io_total = pb->mem_total = pb->pmem_total = 0;
   1546   1.1    briggs 
   1547   1.1    briggs 	rv = probe_bus(pb);
   1548  1.40   msaitoh 	pb->last_busno = pb->next_busno - 1;
   1549  1.39   msaitoh 	if (rv == 0)
   1550   1.1    briggs 		rv = configure_bus(pb);
   1551   1.1    briggs 
   1552   1.1    briggs 	/*
   1553   1.1    briggs 	 * All done!
   1554   1.1    briggs 	 */
   1555  1.32      matt 	kmem_free(pb, sizeof(*pb));
   1556   1.1    briggs 	return rv;
   1557   1.1    briggs }
   1558