Home | History | Annotate | Line # | Download | only in pci
pciconf.c revision 1.46
      1  1.46  jmcneill /*	$NetBSD: pciconf.c,v 1.46 2020/02/02 14:45:14 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.46  jmcneill __KERNEL_RCSID(0, "$NetBSD: pciconf.c,v 1.46 2020/02/02 14:45:14 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/extent.h>
     74   1.1    briggs #include <sys/queue.h>
     75   1.1    briggs #include <sys/systm.h>
     76   1.1    briggs #include <sys/malloc.h>
     77  1.32      matt #include <sys/kmem.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.1    briggs 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.1    briggs typedef struct _s_pciconf_dev_t {
     99   1.1    briggs 	int		ipin;
    100   1.1    briggs 	int		iline;
    101   1.1    briggs 	int		min_gnt;
    102   1.1    briggs 	int		max_lat;
    103   1.2    briggs 	int		enable;
    104   1.1    briggs 	pcitag_t	tag;
    105   1.1    briggs 	pci_chipset_tag_t	pc;
    106   1.1    briggs 	struct _s_pciconf_bus_t	*ppb;		/* I am really a bridge */
    107   1.1    briggs } pciconf_dev_t;
    108   1.1    briggs 
    109   1.1    briggs typedef struct _s_pciconf_win_t {
    110   1.1    briggs 	pciconf_dev_t	*dev;
    111   1.1    briggs 	int		reg;			/* 0 for busses */
    112   1.1    briggs 	int		align;
    113   1.1    briggs 	int		prefetch;
    114  1.39   msaitoh 	uint64_t	size;
    115  1.39   msaitoh 	uint64_t	address;
    116   1.1    briggs } pciconf_win_t;
    117   1.1    briggs 
    118   1.1    briggs typedef struct _s_pciconf_bus_t {
    119   1.1    briggs 	int		busno;
    120   1.1    briggs 	int		next_busno;
    121   1.1    briggs 	int		last_busno;
    122   1.1    briggs 	int		max_mingnt;
    123   1.1    briggs 	int		min_maxlat;
    124  1.14   thorpej 	int		cacheline_size;
    125   1.1    briggs 	int		prefetch;
    126   1.1    briggs 	int		fast_b2b;
    127   1.1    briggs 	int		freq_66;
    128   1.1    briggs 	int		def_ltim;
    129   1.1    briggs 	int		max_ltim;
    130   1.1    briggs 	int		bandwidth_used;
    131   1.1    briggs 	int		swiz;
    132   1.2    briggs 	int		io_32bit;
    133   1.2    briggs 	int		pmem_64bit;
    134  1.44   thorpej 	int		mem_64bit;
    135  1.36      matt 	int		io_align;
    136  1.36      matt 	int		mem_align;
    137  1.36      matt 	int		pmem_align;
    138   1.1    briggs 
    139   1.1    briggs 	int		ndevs;
    140   1.1    briggs 	pciconf_dev_t	device[MAX_CONF_DEV];
    141   1.1    briggs 
    142   1.1    briggs 	/* These should be sorted in order of decreasing size */
    143   1.1    briggs 	int		nmemwin;
    144   1.1    briggs 	pciconf_win_t	pcimemwin[MAX_CONF_MEM];
    145   1.1    briggs 	int		niowin;
    146   1.1    briggs 	pciconf_win_t	pciiowin[MAX_CONF_IO];
    147   1.1    briggs 
    148   1.1    briggs 	bus_size_t	io_total;
    149   1.1    briggs 	bus_size_t	mem_total;
    150   1.1    briggs 	bus_size_t	pmem_total;
    151   1.1    briggs 
    152   1.1    briggs 	struct extent	*ioext;
    153   1.1    briggs 	struct extent	*memext;
    154   1.1    briggs 	struct extent	*pmemext;
    155   1.1    briggs 
    156   1.1    briggs 	pci_chipset_tag_t	pc;
    157   1.1    briggs 	struct _s_pciconf_bus_t *parent_bus;
    158   1.1    briggs } pciconf_bus_t;
    159   1.1    briggs 
    160   1.1    briggs static int	probe_bus(pciconf_bus_t *);
    161   1.1    briggs static void	alloc_busno(pciconf_bus_t *, pciconf_bus_t *);
    162  1.18    simonb static void	set_busreg(pci_chipset_tag_t, pcitag_t, int, int, int);
    163   1.4    simonb static int	pci_do_device_query(pciconf_bus_t *, pcitag_t, int, int, int);
    164   1.1    briggs static int	setup_iowins(pciconf_bus_t *);
    165   1.1    briggs static int	setup_memwins(pciconf_bus_t *);
    166   1.1    briggs static int	configure_bridge(pciconf_dev_t *);
    167   1.1    briggs static int	configure_bus(pciconf_bus_t *);
    168  1.44   thorpej static uint64_t	pci_allocate_range(struct extent *, uint64_t, int, bool);
    169   1.1    briggs static pciconf_win_t	*get_io_desc(pciconf_bus_t *, bus_size_t);
    170   1.1    briggs static pciconf_win_t	*get_mem_desc(pciconf_bus_t *, bus_size_t);
    171   1.1    briggs static pciconf_bus_t	*query_bus(pciconf_bus_t *, pciconf_dev_t *, int);
    172   1.1    briggs 
    173   1.1    briggs static void	print_tag(pci_chipset_tag_t, pcitag_t);
    174   1.1    briggs 
    175   1.1    briggs static void
    176   1.1    briggs print_tag(pci_chipset_tag_t pc, pcitag_t tag)
    177   1.1    briggs {
    178   1.1    briggs 	int	bus, dev, func;
    179   1.1    briggs 
    180   1.1    briggs 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    181   1.1    briggs 	printf("PCI: bus %d, device %d, function %d: ", bus, dev, func);
    182   1.1    briggs }
    183   1.1    briggs 
    184  1.44   thorpej #ifdef _LP64
    185  1.44   thorpej #define	__used_only_lp64	__unused
    186  1.44   thorpej #else
    187  1.44   thorpej #define	__used_only_lp64	/* nothing */
    188  1.44   thorpej #endif /* _LP64 */
    189  1.44   thorpej 
    190   1.1    briggs /************************************************************************/
    191   1.1    briggs /************************************************************************/
    192   1.1    briggs /***********************   Bus probing routines   ***********************/
    193   1.1    briggs /************************************************************************/
    194   1.1    briggs /************************************************************************/
    195   1.1    briggs static pciconf_win_t *
    196   1.1    briggs get_io_desc(pciconf_bus_t *pb, bus_size_t size)
    197   1.1    briggs {
    198   1.1    briggs 	int	i, n;
    199   1.1    briggs 
    200   1.1    briggs 	n = pb->niowin;
    201  1.40   msaitoh 	for (i = n; i > 0 && size > pb->pciiowin[i-1].size; i--)
    202   1.1    briggs 		pb->pciiowin[i] = pb->pciiowin[i-1]; /* struct copy */
    203   1.1    briggs 	return &pb->pciiowin[i];
    204   1.1    briggs }
    205   1.1    briggs 
    206   1.1    briggs static pciconf_win_t *
    207   1.1    briggs get_mem_desc(pciconf_bus_t *pb, bus_size_t size)
    208   1.1    briggs {
    209   1.1    briggs 	int	i, n;
    210   1.1    briggs 
    211   1.1    briggs 	n = pb->nmemwin;
    212  1.40   msaitoh 	for (i = n; i > 0 && size > pb->pcimemwin[i-1].size; i--)
    213   1.1    briggs 		pb->pcimemwin[i] = pb->pcimemwin[i-1]; /* struct copy */
    214   1.1    briggs 	return &pb->pcimemwin[i];
    215   1.1    briggs }
    216   1.1    briggs 
    217   1.1    briggs /*
    218   1.1    briggs  * Set up bus common stuff, then loop over devices & functions.
    219   1.1    briggs  * If we find something, call pci_do_device_query()).
    220   1.1    briggs  */
    221   1.1    briggs static int
    222   1.1    briggs probe_bus(pciconf_bus_t *pb)
    223   1.1    briggs {
    224  1.33    dyoung 	int device;
    225  1.33    dyoung 	uint8_t devs[32];
    226  1.33    dyoung 	int i, n;
    227   1.1    briggs 
    228   1.1    briggs 	pb->ndevs = 0;
    229   1.1    briggs 	pb->niowin = 0;
    230   1.1    briggs 	pb->nmemwin = 0;
    231   1.1    briggs 	pb->freq_66 = 1;
    232  1.21  augustss #ifdef PCICONF_NO_FAST_B2B
    233  1.21  augustss 	pb->fast_b2b = 0;
    234  1.21  augustss #else
    235   1.1    briggs 	pb->fast_b2b = 1;
    236  1.21  augustss #endif
    237   1.1    briggs 	pb->prefetch = 1;
    238   1.1    briggs 	pb->max_mingnt = 0;	/* we are looking for the maximum */
    239   1.1    briggs 	pb->min_maxlat = 0x100;	/* we are looking for the minimum */
    240   1.1    briggs 	pb->bandwidth_used = 0;
    241   1.4    simonb 
    242  1.33    dyoung 	n = pci_bus_devorder(pb->pc, pb->busno, devs, __arraycount(devs));
    243  1.33    dyoung 	for (i = 0; i < n; i++) {
    244   1.1    briggs 		pcitag_t tag;
    245   1.1    briggs 		pcireg_t id, bhlcr;
    246   1.1    briggs 		int function, nfunction;
    247   1.4    simonb 		int confmode;
    248   1.1    briggs 
    249  1.33    dyoung 		device = devs[i];
    250  1.33    dyoung 
    251   1.1    briggs 		tag = pci_make_tag(pb->pc, pb->busno, device, 0);
    252   1.1    briggs 		if (pci_conf_debug) {
    253   1.1    briggs 			print_tag(pb->pc, tag);
    254   1.1    briggs 		}
    255   1.1    briggs 		id = pci_conf_read(pb->pc, tag, PCI_ID_REG);
    256   1.1    briggs 
    257   1.4    simonb 		if (pci_conf_debug) {
    258   1.4    simonb 			printf("id=%x: Vendor=%x, Product=%x\n",
    259  1.40   msaitoh 			    id, PCI_VENDOR(id), PCI_PRODUCT(id));
    260   1.4    simonb 		}
    261   1.1    briggs 		/* Invalid vendor ID value? */
    262   1.1    briggs 		if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    263   1.1    briggs 			continue;
    264   1.1    briggs 
    265   1.1    briggs 		bhlcr = pci_conf_read(pb->pc, tag, PCI_BHLC_REG);
    266   1.1    briggs 		nfunction = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
    267  1.40   msaitoh 		for (function = 0; function < nfunction; function++) {
    268   1.1    briggs 			tag = pci_make_tag(pb->pc, pb->busno, device, function);
    269   1.1    briggs 			id = pci_conf_read(pb->pc, tag, PCI_ID_REG);
    270   1.1    briggs 			if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    271   1.1    briggs 				continue;
    272  1.40   msaitoh 			if (pb->ndevs + 1 < MAX_CONF_DEV) {
    273   1.1    briggs 				if (pci_conf_debug) {
    274   1.1    briggs 					print_tag(pb->pc, tag);
    275   1.3   thorpej 					printf("Found dev 0x%04x 0x%04x -- "
    276   1.3   thorpej 					    "really probing.\n",
    277   1.3   thorpej 					PCI_VENDOR(id), PCI_PRODUCT(id));
    278   1.1    briggs 				}
    279   1.4    simonb #ifdef __HAVE_PCI_CONF_HOOK
    280   1.4    simonb 				confmode = pci_conf_hook(pb->pc, pb->busno,
    281   1.4    simonb 				    device, function, id);
    282   1.4    simonb 				if (confmode == 0)
    283   1.4    simonb 					continue;
    284   1.4    simonb #else
    285   1.6   thorpej 				/*
    286   1.6   thorpej 				 * Don't enable expansion ROMS -- some cards
    287   1.6   thorpej 				 * share address decoders between the EXPROM
    288   1.6   thorpej 				 * and PCI memory space, and enabling the ROM
    289   1.6   thorpej 				 * when not needed will cause all sorts of
    290   1.6   thorpej 				 * lossage.
    291   1.6   thorpej 				 */
    292  1.28   gdamore 				confmode = PCI_CONF_DEFAULT;
    293   1.4    simonb #endif
    294   1.1    briggs 				if (pci_do_device_query(pb, tag, device,
    295   1.4    simonb 				    function, confmode))
    296   1.1    briggs 					return -1;
    297   1.1    briggs 				pb->ndevs++;
    298   1.1    briggs 			}
    299   1.1    briggs 		}
    300   1.1    briggs 	}
    301   1.1    briggs 	return 0;
    302   1.1    briggs }
    303   1.1    briggs 
    304   1.1    briggs static void
    305   1.1    briggs alloc_busno(pciconf_bus_t *parent, pciconf_bus_t *pb)
    306   1.1    briggs {
    307   1.1    briggs 	pb->busno = parent->next_busno;
    308  1.17  augustss 	pb->next_busno = pb->busno + 1;
    309  1.17  augustss }
    310  1.17  augustss 
    311  1.17  augustss static void
    312  1.17  augustss set_busreg(pci_chipset_tag_t pc, pcitag_t tag, int prim, int sec, int sub)
    313  1.17  augustss {
    314  1.17  augustss 	pcireg_t	busreg;
    315  1.17  augustss 
    316  1.41   msaitoh 	busreg  =  __SHIFTIN(prim, PCI_BRIDGE_BUS_PRIMARY);
    317  1.41   msaitoh 	busreg |=  __SHIFTIN(sec,  PCI_BRIDGE_BUS_SECONDARY);
    318  1.41   msaitoh 	busreg |=  __SHIFTIN(sub,  PCI_BRIDGE_BUS_SUBORDINATE);
    319  1.17  augustss 	pci_conf_write(pc, tag, PCI_BRIDGE_BUS_REG, busreg);
    320   1.1    briggs }
    321   1.1    briggs 
    322   1.1    briggs static pciconf_bus_t *
    323   1.1    briggs query_bus(pciconf_bus_t *parent, pciconf_dev_t *pd, int dev)
    324   1.1    briggs {
    325   1.1    briggs 	pciconf_bus_t	*pb;
    326  1.17  augustss 	pcireg_t	io, pmem;
    327   1.1    briggs 	pciconf_win_t	*pi, *pm;
    328   1.1    briggs 
    329  1.42       chs 	pb = kmem_zalloc(sizeof (pciconf_bus_t), KM_SLEEP);
    330  1.14   thorpej 	pb->cacheline_size = parent->cacheline_size;
    331   1.1    briggs 	pb->parent_bus = parent;
    332   1.1    briggs 	alloc_busno(parent, pb);
    333   1.1    briggs 
    334  1.36      matt 	pb->mem_align = 0x100000;	/* 1M alignment */
    335  1.36      matt 	pb->pmem_align = 0x100000;	/* 1M alignment */
    336  1.36      matt 	pb->io_align = 0x1000;		/* 4K alignment */
    337  1.36      matt 
    338  1.17  augustss 	set_busreg(parent->pc, pd->tag, parent->busno, pb->busno, 0xff);
    339   1.1    briggs 
    340   1.1    briggs 	pb->swiz = parent->swiz + dev;
    341   1.1    briggs 
    342   1.1    briggs 	pb->ioext = NULL;
    343   1.1    briggs 	pb->memext = NULL;
    344   1.1    briggs 	pb->pmemext = NULL;
    345   1.1    briggs 	pb->pc = parent->pc;
    346   1.1    briggs 	pb->io_total = pb->mem_total = pb->pmem_total = 0;
    347   1.1    briggs 
    348   1.2    briggs 	pb->io_32bit = 0;
    349   1.2    briggs 	if (parent->io_32bit) {
    350  1.11   thorpej 		io = pci_conf_read(parent->pc, pd->tag, PCI_BRIDGE_STATIO_REG);
    351  1.40   msaitoh 		if (PCI_BRIDGE_IO_32BITS(io))
    352   1.2    briggs 			pb->io_32bit = 1;
    353   1.2    briggs 	}
    354   1.2    briggs 
    355   1.2    briggs 	pb->pmem_64bit = 0;
    356   1.2    briggs 	if (parent->pmem_64bit) {
    357  1.11   thorpej 		pmem = pci_conf_read(parent->pc, pd->tag,
    358   1.2    briggs 		    PCI_BRIDGE_PREFETCHMEM_REG);
    359  1.40   msaitoh 		if (PCI_BRIDGE_PREFETCHMEM_64BITS(pmem))
    360   1.2    briggs 			pb->pmem_64bit = 1;
    361   1.2    briggs 	}
    362   1.2    briggs 
    363  1.44   thorpej 	/* Bridges only forward a 32-bit range of non-prefetcable memory. */
    364  1.44   thorpej 	pb->mem_64bit = 0;
    365  1.44   thorpej 
    366   1.1    briggs 	if (probe_bus(pb)) {
    367   1.1    briggs 		printf("Failed to probe bus %d\n", pb->busno);
    368   1.1    briggs 		goto err;
    369   1.1    briggs 	}
    370   1.1    briggs 
    371  1.17  augustss 	/* We have found all subordinate busses now, reprogram busreg. */
    372  1.40   msaitoh 	pb->last_busno = pb->next_busno - 1;
    373  1.17  augustss 	parent->next_busno = pb->next_busno;
    374  1.17  augustss 	set_busreg(parent->pc, pd->tag, parent->busno, pb->busno,
    375  1.17  augustss 		   pb->last_busno);
    376  1.17  augustss 	if (pci_conf_debug)
    377  1.17  augustss 		printf("PCI bus bridge (parent %d) covers busses %d-%d\n",
    378  1.17  augustss 			parent->busno, pb->busno, pb->last_busno);
    379  1.17  augustss 
    380   1.1    briggs 	if (pb->io_total > 0) {
    381   1.1    briggs 		if (parent->niowin >= MAX_CONF_IO) {
    382  1.35      matt 			printf("pciconf: too many (%d) I/O windows\n",
    383  1.35      matt 			    parent->niowin);
    384   1.1    briggs 			goto err;
    385   1.1    briggs 		}
    386  1.36      matt 		pb->io_total |= pb->io_align - 1; /* Round up */
    387   1.1    briggs 		pi = get_io_desc(parent, pb->io_total);
    388   1.1    briggs 		pi->dev = pd;
    389   1.1    briggs 		pi->reg = 0;
    390   1.1    briggs 		pi->size = pb->io_total;
    391  1.36      matt 		pi->align = pb->io_align;	/* 4K min alignment */
    392  1.36      matt 		if (parent->io_align < pb->io_align)
    393  1.36      matt 			parent->io_align = pb->io_align;
    394   1.1    briggs 		pi->prefetch = 0;
    395   1.1    briggs 		parent->niowin++;
    396   1.1    briggs 		parent->io_total += pb->io_total;
    397   1.1    briggs 	}
    398   1.1    briggs 
    399   1.1    briggs 	if (pb->mem_total > 0) {
    400   1.1    briggs 		if (parent->nmemwin >= MAX_CONF_MEM) {
    401  1.35      matt 			printf("pciconf: too many (%d) MEM windows\n",
    402  1.35      matt 			     parent->nmemwin);
    403   1.1    briggs 			goto err;
    404   1.1    briggs 		}
    405  1.40   msaitoh 		pb->mem_total |= pb->mem_align - 1; /* Round up */
    406   1.1    briggs 		pm = get_mem_desc(parent, pb->mem_total);
    407   1.1    briggs 		pm->dev = pd;
    408   1.1    briggs 		pm->reg = 0;
    409   1.1    briggs 		pm->size = pb->mem_total;
    410  1.36      matt 		pm->align = pb->mem_align;	/* 1M min alignment */
    411  1.36      matt 		if (parent->mem_align < pb->mem_align)
    412  1.36      matt 			parent->mem_align = pb->mem_align;
    413   1.1    briggs 		pm->prefetch = 0;
    414   1.1    briggs 		parent->nmemwin++;
    415   1.1    briggs 		parent->mem_total += pb->mem_total;
    416   1.1    briggs 	}
    417   1.1    briggs 
    418   1.1    briggs 	if (pb->pmem_total > 0) {
    419   1.1    briggs 		if (parent->nmemwin >= MAX_CONF_MEM) {
    420  1.10   thorpej 			printf("pciconf: too many MEM windows\n");
    421   1.1    briggs 			goto err;
    422   1.1    briggs 		}
    423  1.40   msaitoh 		pb->pmem_total |= pb->pmem_align - 1; /* Round up */
    424   1.1    briggs 		pm = get_mem_desc(parent, pb->pmem_total);
    425   1.1    briggs 		pm->dev = pd;
    426   1.1    briggs 		pm->reg = 0;
    427   1.1    briggs 		pm->size = pb->pmem_total;
    428  1.36      matt 		pm->align = pb->pmem_align;	/* 1M alignment */
    429  1.36      matt 		if (parent->pmem_align < pb->pmem_align)
    430  1.36      matt 			parent->pmem_align = pb->pmem_align;
    431   1.1    briggs 		pm->prefetch = 1;
    432   1.1    briggs 		parent->nmemwin++;
    433   1.1    briggs 		parent->pmem_total += pb->pmem_total;
    434   1.1    briggs 	}
    435   1.1    briggs 
    436   1.1    briggs 	return pb;
    437   1.1    briggs err:
    438  1.32      matt 	kmem_free(pb, sizeof(*pb));
    439   1.1    briggs 	return NULL;
    440   1.1    briggs }
    441   1.1    briggs 
    442   1.1    briggs static int
    443  1.39   msaitoh pci_do_device_query(pciconf_bus_t *pb, pcitag_t tag, int dev, int func,
    444  1.39   msaitoh     int mode)
    445   1.1    briggs {
    446   1.1    briggs 	pciconf_dev_t	*pd;
    447   1.1    briggs 	pciconf_win_t	*pi, *pm;
    448  1.39   msaitoh 	pcireg_t	classreg, cmd, icr, bhlc, bar, mask, bar64, mask64,
    449  1.39   msaitoh 	    busreg;
    450  1.39   msaitoh 	uint64_t	size;
    451  1.22    briggs 	int		br, width, reg_start, reg_end;
    452   1.1    briggs 
    453   1.1    briggs 	pd = &pb->device[pb->ndevs];
    454   1.1    briggs 	pd->pc = pb->pc;
    455   1.1    briggs 	pd->tag = tag;
    456   1.1    briggs 	pd->ppb = NULL;
    457   1.4    simonb 	pd->enable = mode;
    458   1.1    briggs 
    459  1.37      matt 	classreg = pci_conf_read(pb->pc, tag, PCI_CLASS_REG);
    460   1.1    briggs 
    461   1.1    briggs 	cmd = pci_conf_read(pb->pc, tag, PCI_COMMAND_STATUS_REG);
    462  1.32      matt 	bhlc = pci_conf_read(pb->pc, tag, PCI_BHLC_REG);
    463   1.1    briggs 
    464  1.37      matt 	if (PCI_CLASS(classreg) != PCI_CLASS_BRIDGE
    465  1.32      matt 	    && PCI_HDRTYPE_TYPE(bhlc) != PCI_HDRTYPE_PPB) {
    466   1.1    briggs 		cmd &= ~(PCI_COMMAND_MASTER_ENABLE |
    467   1.1    briggs 		    PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE);
    468   1.1    briggs 		pci_conf_write(pb->pc, tag, PCI_COMMAND_STATUS_REG, cmd);
    469   1.3   thorpej 	} else if (pci_conf_debug) {
    470   1.3   thorpej 		print_tag(pb->pc, tag);
    471   1.3   thorpej 		printf("device is a bridge; not clearing enables\n");
    472   1.1    briggs 	}
    473   1.1    briggs 
    474   1.1    briggs 	if ((cmd & PCI_STATUS_BACKTOBACK_SUPPORT) == 0)
    475   1.1    briggs 		pb->fast_b2b = 0;
    476   1.1    briggs 
    477   1.1    briggs 	if ((cmd & PCI_STATUS_66MHZ_SUPPORT) == 0)
    478   1.1    briggs 		pb->freq_66 = 0;
    479   1.1    briggs 
    480  1.22    briggs 	switch (PCI_HDRTYPE_TYPE(bhlc)) {
    481  1.22    briggs 	case PCI_HDRTYPE_DEVICE:
    482  1.22    briggs 		reg_start = PCI_MAPREG_START;
    483  1.22    briggs 		reg_end = PCI_MAPREG_END;
    484  1.22    briggs 		break;
    485  1.22    briggs 	case PCI_HDRTYPE_PPB:
    486   1.1    briggs 		pd->ppb = query_bus(pb, pd, dev);
    487   1.1    briggs 		if (pd->ppb == NULL)
    488   1.1    briggs 			return -1;
    489   1.1    briggs 		return 0;
    490  1.22    briggs 	case PCI_HDRTYPE_PCB:
    491  1.22    briggs 		reg_start = PCI_MAPREG_START;
    492  1.22    briggs 		reg_end = PCI_MAPREG_PCB_END;
    493  1.22    briggs 
    494  1.22    briggs 		busreg = pci_conf_read(pb->pc, tag, PCI_BUSNUM);
    495  1.22    briggs 		busreg  =  (busreg & 0xff000000) |
    496  1.41   msaitoh 		    __SHIFTIN(pb->busno, PCI_BRIDGE_BUS_PRIMARY) |
    497  1.41   msaitoh 		    __SHIFTIN(pb->next_busno, PCI_BRIDGE_BUS_SECONDARY) |
    498  1.41   msaitoh 		    __SHIFTIN(pb->next_busno, PCI_BRIDGE_BUS_SUBORDINATE);
    499  1.22    briggs 		pci_conf_write(pb->pc, tag, PCI_BUSNUM, busreg);
    500  1.22    briggs 
    501  1.24    simonb 		pb->next_busno++;
    502  1.22    briggs 		break;
    503  1.22    briggs 	default:
    504  1.22    briggs 		return -1;
    505   1.1    briggs 	}
    506   1.1    briggs 
    507   1.1    briggs 	icr = pci_conf_read(pb->pc, tag, PCI_INTERRUPT_REG);
    508   1.1    briggs 	pd->ipin = PCI_INTERRUPT_PIN(icr);
    509   1.1    briggs 	pd->iline = PCI_INTERRUPT_LINE(icr);
    510   1.1    briggs 	pd->min_gnt = PCI_MIN_GNT(icr);
    511   1.1    briggs 	pd->max_lat = PCI_MAX_LAT(icr);
    512   1.1    briggs 	if (pd->iline || pd->ipin) {
    513   1.8    briggs 		pci_conf_interrupt(pb->pc, pb->busno, dev, pd->ipin, pb->swiz,
    514   1.1    briggs 		    &pd->iline);
    515   1.1    briggs 		icr &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT);
    516   1.1    briggs 		icr |= (pd->iline << PCI_INTERRUPT_LINE_SHIFT);
    517   1.1    briggs 		pci_conf_write(pb->pc, tag, PCI_INTERRUPT_REG, icr);
    518   1.1    briggs 	}
    519   1.1    briggs 
    520   1.1    briggs 	if (pd->min_gnt != 0 || pd->max_lat != 0) {
    521   1.1    briggs 		if (pd->min_gnt != 0 && pd->min_gnt > pb->max_mingnt)
    522   1.1    briggs 			pb->max_mingnt = pd->min_gnt;
    523   1.1    briggs 
    524   1.1    briggs 		if (pd->max_lat != 0 && pd->max_lat < pb->min_maxlat)
    525   1.1    briggs 			pb->min_maxlat = pd->max_lat;
    526   1.1    briggs 
    527   1.1    briggs 		pb->bandwidth_used += pd->min_gnt * 4000000 /
    528   1.1    briggs 				(pd->min_gnt + pd->max_lat);
    529   1.1    briggs 	}
    530   1.1    briggs 
    531   1.1    briggs 	width = 4;
    532  1.22    briggs 	for (br = reg_start; br < reg_end; br += width) {
    533   1.3   thorpej #if 0
    534   1.8    briggs /* XXX Should only ignore if IDE not in legacy mode? */
    535  1.37      matt 		if (PCI_CLASS(classreg) == PCI_CLASS_MASS_STORAGE &&
    536  1.37      matt 		    PCI_SUBCLASS(classreg) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
    537   1.1    briggs 			break;
    538   1.1    briggs 		}
    539   1.3   thorpej #endif
    540   1.1    briggs 		bar = pci_conf_read(pb->pc, tag, br);
    541   1.3   thorpej 		pci_conf_write(pb->pc, tag, br, 0xffffffff);
    542   1.1    briggs 		mask = pci_conf_read(pb->pc, tag, br);
    543   1.1    briggs 		pci_conf_write(pb->pc, tag, br, bar);
    544   1.1    briggs 		width = 4;
    545   1.1    briggs 
    546   1.8    briggs 		if (   (mode & PCI_CONF_MAP_IO)
    547   1.8    briggs 		    && (PCI_MAPREG_TYPE(mask) == PCI_MAPREG_TYPE_IO)) {
    548   1.8    briggs 			/*
    549   1.8    briggs 			 * Upper 16 bits must be one.  Devices may hardwire
    550   1.8    briggs 			 * them to zero, though, per PCI 2.2, 6.2.5.1, p 203.
    551   1.8    briggs 			 */
    552   1.3   thorpej 			mask |= 0xffff0000;
    553   1.3   thorpej 
    554   1.3   thorpej 			size = PCI_MAPREG_IO_SIZE(mask);
    555   1.3   thorpej 			if (size == 0) {
    556   1.3   thorpej 				if (pci_conf_debug) {
    557   1.3   thorpej 					print_tag(pb->pc, tag);
    558   1.3   thorpej 					printf("I/O BAR 0x%x is void\n", br);
    559   1.3   thorpej 				}
    560   1.3   thorpej 				continue;
    561   1.3   thorpej 			}
    562   1.1    briggs 
    563   1.1    briggs 			if (pb->niowin >= MAX_CONF_IO) {
    564  1.10   thorpej 				printf("pciconf: too many I/O windows\n");
    565   1.1    briggs 				return -1;
    566   1.1    briggs 			}
    567   1.1    briggs 
    568   1.1    briggs 			pi = get_io_desc(pb, size);
    569   1.1    briggs 			pi->dev = pd;
    570   1.1    briggs 			pi->reg = br;
    571  1.43   msaitoh 			pi->size = (uint64_t)size;
    572   1.1    briggs 			pi->align = 4;
    573  1.36      matt 			if (pb->io_align < pi->size)
    574  1.36      matt 				pb->io_align = pi->size;
    575   1.1    briggs 			pi->prefetch = 0;
    576   1.1    briggs 			if (pci_conf_debug) {
    577   1.1    briggs 				print_tag(pb->pc, tag);
    578  1.23       scw 				printf("Register 0x%x, I/O size %" PRIu64 "\n",
    579   1.1    briggs 				    br, pi->size);
    580   1.1    briggs 			}
    581   1.1    briggs 			pb->niowin++;
    582   1.1    briggs 			pb->io_total += size;
    583   1.4    simonb 		} else if ((mode & PCI_CONF_MAP_MEM)
    584   1.4    simonb 			   && (PCI_MAPREG_TYPE(mask) == PCI_MAPREG_TYPE_MEM)) {
    585   1.1    briggs 			switch (PCI_MAPREG_MEM_TYPE(mask)) {
    586   1.1    briggs 			case PCI_MAPREG_MEM_TYPE_32BIT:
    587   1.1    briggs 			case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    588  1.43   msaitoh 				size = (uint64_t)PCI_MAPREG_MEM_SIZE(mask);
    589   1.1    briggs 				break;
    590   1.1    briggs 			case PCI_MAPREG_MEM_TYPE_64BIT:
    591   1.1    briggs 				bar64 = pci_conf_read(pb->pc, tag, br + 4);
    592   1.1    briggs 				pci_conf_write(pb->pc, tag, br + 4, 0xffffffff);
    593   1.1    briggs 				mask64 = pci_conf_read(pb->pc, tag, br + 4);
    594   1.1    briggs 				pci_conf_write(pb->pc, tag, br + 4, bar64);
    595  1.43   msaitoh 				size = (uint64_t)PCI_MAPREG_MEM64_SIZE(
    596  1.43   msaitoh 				      (((uint64_t)mask64) << 32) | mask);
    597   1.1    briggs 				width = 8;
    598  1.16    briggs 				break;
    599   1.1    briggs 			default:
    600   1.1    briggs 				print_tag(pb->pc, tag);
    601   1.1    briggs 				printf("reserved mapping type 0x%x\n",
    602   1.1    briggs 					PCI_MAPREG_MEM_TYPE(mask));
    603   1.1    briggs 				continue;
    604   1.1    briggs 			}
    605   1.1    briggs 
    606   1.3   thorpej 			if (size == 0) {
    607   1.3   thorpej 				if (pci_conf_debug) {
    608   1.3   thorpej 					print_tag(pb->pc, tag);
    609   1.3   thorpej 					printf("MEM%d BAR 0x%x is void\n",
    610   1.3   thorpej 					    PCI_MAPREG_MEM_TYPE(mask) ==
    611   1.3   thorpej 						PCI_MAPREG_MEM_TYPE_64BIT ?
    612   1.3   thorpej 						64 : 32, br);
    613   1.3   thorpej 				}
    614   1.3   thorpej 				continue;
    615  1.16    briggs 			} else {
    616  1.16    briggs 				if (pci_conf_debug) {
    617  1.16    briggs 					print_tag(pb->pc, tag);
    618  1.36      matt 					printf("MEM%d BAR 0x%x has size %#lx\n",
    619  1.16    briggs 					    PCI_MAPREG_MEM_TYPE(mask) ==
    620  1.16    briggs 						PCI_MAPREG_MEM_TYPE_64BIT ?
    621  1.43   msaitoh 						64 : 32,
    622  1.43   msaitoh 					    br, (unsigned long)size);
    623  1.16    briggs 				}
    624   1.3   thorpej 			}
    625   1.3   thorpej 
    626   1.1    briggs 			if (pb->nmemwin >= MAX_CONF_MEM) {
    627  1.10   thorpej 				printf("pciconf: too many memory windows\n");
    628   1.1    briggs 				return -1;
    629   1.1    briggs 			}
    630   1.1    briggs 
    631   1.1    briggs 			pm = get_mem_desc(pb, size);
    632   1.1    briggs 			pm->dev = pd;
    633   1.1    briggs 			pm->reg = br;
    634   1.1    briggs 			pm->size = size;
    635   1.1    briggs 			pm->align = 4;
    636   1.1    briggs 			pm->prefetch = PCI_MAPREG_MEM_PREFETCHABLE(mask);
    637   1.1    briggs 			if (pci_conf_debug) {
    638   1.1    briggs 				print_tag(pb->pc, tag);
    639  1.23       scw 				printf("Register 0x%x, memory size %"
    640  1.23       scw 				    PRIu64 "\n", br, pm->size);
    641   1.1    briggs 			}
    642   1.1    briggs 			pb->nmemwin++;
    643   1.1    briggs 			if (pm->prefetch) {
    644   1.1    briggs 				pb->pmem_total += size;
    645  1.36      matt 				if (pb->pmem_align < pm->size)
    646  1.36      matt 					pb->pmem_align = pm->size;
    647   1.1    briggs 			} else {
    648   1.1    briggs 				pb->mem_total += size;
    649  1.36      matt 				if (pb->mem_align < pm->size)
    650  1.36      matt 					pb->mem_align = pm->size;
    651   1.1    briggs 			}
    652   1.1    briggs 		}
    653   1.1    briggs 	}
    654   1.1    briggs 
    655   1.4    simonb 	if (mode & PCI_CONF_MAP_ROM) {
    656   1.4    simonb 		bar = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
    657   1.4    simonb 		pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM, 0xfffffffe);
    658   1.4    simonb 		mask = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
    659   1.4    simonb 		pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM, bar);
    660   1.4    simonb 
    661   1.4    simonb 		if (mask != 0 && mask != 0xffffffff) {
    662   1.4    simonb 			if (pb->nmemwin >= MAX_CONF_MEM) {
    663  1.10   thorpej 				printf("pciconf: too many memory windows\n");
    664   1.4    simonb 				return -1;
    665   1.4    simonb 			}
    666  1.43   msaitoh 			size = (uint64_t)PCI_MAPREG_MEM_SIZE(mask);
    667   1.1    briggs 
    668   1.4    simonb 			pm = get_mem_desc(pb, size);
    669   1.4    simonb 			pm->dev = pd;
    670   1.4    simonb 			pm->reg = PCI_MAPREG_ROM;
    671   1.4    simonb 			pm->size = size;
    672   1.4    simonb 			pm->align = 4;
    673  1.44   thorpej 			pm->prefetch = 0;
    674   1.4    simonb 			if (pci_conf_debug) {
    675   1.4    simonb 				print_tag(pb->pc, tag);
    676  1.23       scw 				printf("Expansion ROM memory size %"
    677  1.23       scw 				    PRIu64 "\n", pm->size);
    678   1.4    simonb 			}
    679   1.4    simonb 			pb->nmemwin++;
    680  1.44   thorpej 			if (pm->prefetch) {
    681  1.44   thorpej 				pb->pmem_total += size;
    682  1.44   thorpej 				if (pb->pmem_align < pm->size)
    683  1.44   thorpej 					pb->pmem_align = pm->size;
    684  1.44   thorpej 			} else {
    685  1.44   thorpej 				pb->mem_total += size;
    686  1.44   thorpej 				if (pb->mem_align < pm->size)
    687  1.44   thorpej 					pb->mem_align = pm->size;
    688  1.44   thorpej 			}
    689   1.1    briggs 		}
    690   1.8    briggs 	} else {
    691  1.28   gdamore 		/* Don't enable ROMs if we aren't going to map them. */
    692  1.28   gdamore 		mode &= ~PCI_CONF_ENABLE_ROM;
    693  1.28   gdamore 		pd->enable &= ~PCI_CONF_ENABLE_ROM;
    694  1.28   gdamore 	}
    695  1.28   gdamore 
    696  1.28   gdamore 	if (!(mode & PCI_CONF_ENABLE_ROM)) {
    697   1.8    briggs 		/* Ensure ROM is disabled */
    698   1.8    briggs 		bar = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
    699   1.8    briggs 		pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM,
    700   1.8    briggs 		    bar & ~PCI_MAPREG_ROM_ENABLE);
    701   1.1    briggs 	}
    702   1.1    briggs 
    703   1.1    briggs 	return 0;
    704   1.1    briggs }
    705   1.1    briggs 
    706   1.1    briggs /************************************************************************/
    707   1.1    briggs /************************************************************************/
    708   1.1    briggs /********************   Bus configuration routines   ********************/
    709   1.1    briggs /************************************************************************/
    710   1.1    briggs /************************************************************************/
    711  1.39   msaitoh static uint64_t
    712  1.44   thorpej pci_allocate_range(struct extent * const ex, const uint64_t amt,
    713  1.44   thorpej 		   const int align, const bool ok64 __used_only_lp64)
    714   1.1    briggs {
    715   1.1    briggs 	int	r;
    716   1.1    briggs 	u_long	addr;
    717   1.1    briggs 
    718  1.44   thorpej 	u_long end = ex->ex_end;
    719  1.44   thorpej 
    720  1.44   thorpej #ifdef _LP64
    721  1.44   thorpej 	/*
    722  1.44   thorpej 	 * If a 64-bit range is not OK:
    723  1.44   thorpej 	 * ==> If the start of the range is > 4GB, allocation not possible.
    724  1.44   thorpej 	 * ==> If the end of the range is > (4GB-1), constrain the end.
    725  1.44   thorpej 	 *
    726  1.44   thorpej 	 * If a 64-bit range IS OK, then we prefer allocating above 4GB.
    727  1.44   thorpej 	 *
    728  1.44   thorpej 	 * XXX We guard this with _LP64 because extent maps use u_long
    729  1.44   thorpej 	 * internally.
    730  1.44   thorpej 	 */
    731  1.44   thorpej 	if (!ok64) {
    732  1.44   thorpej 		if (ex->ex_start >= (1UL << 32)) {
    733  1.44   thorpej 			printf("PCI: 32-BIT RESTRICTION, RANGE BEGINS AT %#lx\n",
    734  1.44   thorpej 			    ex->ex_start);
    735  1.44   thorpej 			return ~0ULL;
    736  1.44   thorpej 		}
    737  1.44   thorpej 		if (end > 0xffffffffUL) {
    738  1.44   thorpej 			end = 0xffffffffUL;
    739  1.44   thorpej 		}
    740  1.44   thorpej 	} else if (end > (1UL << 32)) {
    741  1.44   thorpej 		u_long start4g = ex->ex_start;
    742  1.44   thorpej 		if (start4g < (1UL << 32)) {
    743  1.44   thorpej 			start4g = (1UL << 32);
    744  1.44   thorpej 		}
    745  1.44   thorpej 		r = extent_alloc_subregion(ex, start4g, end, amt, align, 0,
    746  1.44   thorpej 					   EX_NOWAIT, &addr);
    747  1.44   thorpej 		if (r == 0) {
    748  1.44   thorpej 			return addr;
    749  1.44   thorpej 		}
    750  1.44   thorpej 	}
    751  1.44   thorpej #endif /* _L64 */
    752  1.44   thorpej 
    753  1.44   thorpej 	r = extent_alloc_subregion(ex, ex->ex_start, end, amt, align, 0,
    754  1.44   thorpej 				   EX_NOWAIT, &addr);
    755   1.1    briggs 	if (r) {
    756  1.44   thorpej 		printf("extent_alloc_subregion(%p, %#lx, %#lx, %#" PRIx64 ", %#x) returned %d\n",
    757  1.44   thorpej 		    ex, ex->ex_start, end, amt, align, r);
    758   1.4    simonb 		extent_print(ex);
    759  1.36      matt 		return ~0ULL;
    760   1.1    briggs 	}
    761  1.36      matt 	return addr;
    762   1.1    briggs }
    763   1.1    briggs 
    764   1.1    briggs static int
    765   1.1    briggs setup_iowins(pciconf_bus_t *pb)
    766   1.1    briggs {
    767   1.1    briggs 	pciconf_win_t	*pi;
    768   1.1    briggs 	pciconf_dev_t	*pd;
    769   1.1    briggs 
    770  1.40   msaitoh 	for (pi = pb->pciiowin; pi < &pb->pciiowin[pb->niowin]; pi++) {
    771   1.1    briggs 		if (pi->size == 0)
    772   1.1    briggs 			continue;
    773   1.1    briggs 
    774   1.1    briggs 		pd = pi->dev;
    775  1.46  jmcneill 		if (pb->ioext == NULL) {
    776  1.46  jmcneill 			/* Bus has no IO ranges, disable IO BAR */
    777  1.46  jmcneill 			pi->address = 0;
    778  1.46  jmcneill 			pd->enable &= ~PCI_CONF_ENABLE_IO;
    779  1.46  jmcneill 			goto write_ioaddr;
    780  1.46  jmcneill 		}
    781   1.1    briggs 		pi->address = pci_allocate_range(pb->ioext, pi->size,
    782  1.44   thorpej 		    pi->align, false);
    783  1.36      matt 		if (~pi->address == 0) {
    784   1.1    briggs 			print_tag(pd->pc, pd->tag);
    785  1.23       scw 			printf("Failed to allocate PCI I/O space (%"
    786  1.23       scw 			    PRIu64 " req)\n", pi->size);
    787   1.1    briggs 			return -1;
    788   1.1    briggs 		}
    789   1.1    briggs 		if (pd->ppb && pi->reg == 0) {
    790   1.1    briggs 			pd->ppb->ioext = extent_create("pciconf", pi->address,
    791  1.34      para 			    pi->address + pi->size, NULL, 0,
    792   1.1    briggs 			    EX_NOWAIT);
    793   1.1    briggs 			if (pd->ppb->ioext == NULL) {
    794   1.1    briggs 				print_tag(pd->pc, pd->tag);
    795   1.1    briggs 				printf("Failed to alloc I/O ext. for bus %d\n",
    796   1.1    briggs 				    pd->ppb->busno);
    797   1.1    briggs 				return -1;
    798   1.1    briggs 			}
    799   1.1    briggs 			continue;
    800   1.1    briggs 		}
    801  1.26   tsutsui 		if (!pb->io_32bit && pi->address > 0xFFFF) {
    802  1.26   tsutsui 			pi->address = 0;
    803  1.26   tsutsui 			pd->enable &= ~PCI_CONF_ENABLE_IO;
    804  1.26   tsutsui 		} else {
    805  1.26   tsutsui 			pd->enable |= PCI_CONF_ENABLE_IO;
    806  1.26   tsutsui 		}
    807  1.46  jmcneill write_ioaddr:
    808   1.1    briggs 		if (pci_conf_debug) {
    809   1.1    briggs 			print_tag(pd->pc, pd->tag);
    810  1.23       scw 			printf("Putting %" PRIu64 " I/O bytes @ %#" PRIx64
    811  1.23       scw 			    " (reg %x)\n", pi->size, pi->address, pi->reg);
    812   1.1    briggs 		}
    813   1.1    briggs 		pci_conf_write(pd->pc, pd->tag, pi->reg,
    814   1.1    briggs 		    PCI_MAPREG_IO_ADDR(pi->address) | PCI_MAPREG_TYPE_IO);
    815   1.1    briggs 	}
    816   1.1    briggs 	return 0;
    817   1.1    briggs }
    818   1.1    briggs 
    819   1.1    briggs static int
    820   1.1    briggs setup_memwins(pciconf_bus_t *pb)
    821   1.1    briggs {
    822   1.1    briggs 	pciconf_win_t	*pm;
    823   1.1    briggs 	pciconf_dev_t	*pd;
    824   1.1    briggs 	pcireg_t	base;
    825   1.1    briggs 	struct extent	*ex;
    826  1.44   thorpej 	bool		ok64;
    827   1.1    briggs 
    828  1.40   msaitoh 	for (pm = pb->pcimemwin; pm < &pb->pcimemwin[pb->nmemwin]; pm++) {
    829   1.1    briggs 		if (pm->size == 0)
    830   1.1    briggs 			continue;
    831   1.1    briggs 
    832  1.44   thorpej 		ok64 = false;
    833   1.1    briggs 		pd = pm->dev;
    834  1.44   thorpej 		if (pm->prefetch) {
    835  1.44   thorpej 			ex = pb->pmemext;
    836  1.44   thorpej 			ok64 = pb->pmem_64bit;
    837  1.44   thorpej 		} else {
    838  1.44   thorpej 			ex = pb->memext;
    839  1.44   thorpej 			ok64 = pb->mem_64bit && pd->ppb == NULL;
    840  1.44   thorpej 		}
    841  1.44   thorpej 
    842  1.44   thorpej 		/*
    843  1.44   thorpej 		 * We need to figure out if the memory BAR is 64-bit
    844  1.44   thorpej 		 * capable or not.  If it's not, then we need to constrain
    845  1.44   thorpej 		 * the address allocation.
    846  1.44   thorpej 		 */
    847  1.44   thorpej 		if (pm->reg == PCI_MAPREG_ROM) {
    848  1.44   thorpej 			ok64 = false;
    849  1.44   thorpej 		} else if (ok64) {
    850  1.44   thorpej 			base = pci_conf_read(pd->pc, pd->tag, pm->reg);
    851  1.44   thorpej 			ok64 = PCI_MAPREG_MEM_TYPE(base) ==
    852  1.44   thorpej 			    PCI_MAPREG_MEM_TYPE_64BIT;
    853  1.44   thorpej 		}
    854  1.44   thorpej 
    855  1.44   thorpej 		pm->address = pci_allocate_range(ex, pm->size, pm->align,
    856  1.44   thorpej 						 ok64);
    857  1.36      matt 		if (~pm->address == 0) {
    858   1.1    briggs 			print_tag(pd->pc, pd->tag);
    859   1.1    briggs 			printf(
    860  1.23       scw 			   "Failed to allocate PCI memory space (%" PRIu64
    861  1.44   thorpej 			   " req, prefetch=%d ok64=%d)\n", pm->size,
    862  1.44   thorpej 			   pm->prefetch, (int)ok64);
    863   1.1    briggs 			return -1;
    864   1.1    briggs 		}
    865   1.1    briggs 		if (pd->ppb && pm->reg == 0) {
    866   1.1    briggs 			ex = extent_create("pciconf", pm->address,
    867  1.34      para 			    pm->address + pm->size, NULL, 0, EX_NOWAIT);
    868   1.1    briggs 			if (ex == NULL) {
    869   1.1    briggs 				print_tag(pd->pc, pd->tag);
    870   1.1    briggs 				printf("Failed to alloc MEM ext. for bus %d\n",
    871   1.1    briggs 				    pd->ppb->busno);
    872   1.1    briggs 				return -1;
    873   1.1    briggs 			}
    874  1.39   msaitoh 			if (pm->prefetch)
    875   1.1    briggs 				pd->ppb->pmemext = ex;
    876  1.39   msaitoh 			else
    877   1.1    briggs 				pd->ppb->memext = ex;
    878  1.39   msaitoh 
    879   1.1    briggs 			continue;
    880   1.1    briggs 		}
    881  1.44   thorpej 		if (!ok64 && pm->address > 0xFFFFFFFFULL) {
    882   1.2    briggs 			pm->address = 0;
    883  1.26   tsutsui 			pd->enable &= ~PCI_CONF_ENABLE_MEM;
    884  1.39   msaitoh 		} else
    885   1.8    briggs 			pd->enable |= PCI_CONF_ENABLE_MEM;
    886  1.39   msaitoh 
    887   1.1    briggs 		if (pm->reg != PCI_MAPREG_ROM) {
    888   1.1    briggs 			if (pci_conf_debug) {
    889   1.1    briggs 				print_tag(pd->pc, pd->tag);
    890   1.1    briggs 				printf(
    891  1.23       scw 				    "Putting %" PRIu64 " MEM bytes @ %#"
    892  1.23       scw 				    PRIx64 " (reg %x)\n", pm->size,
    893  1.23       scw 				    pm->address, pm->reg);
    894   1.1    briggs 			}
    895   1.1    briggs 			base = pci_conf_read(pd->pc, pd->tag, pm->reg);
    896   1.1    briggs 			base = PCI_MAPREG_MEM_ADDR(pm->address) |
    897   1.1    briggs 			    PCI_MAPREG_MEM_TYPE(base);
    898   1.1    briggs 			pci_conf_write(pd->pc, pd->tag, pm->reg, base);
    899   1.1    briggs 			if (PCI_MAPREG_MEM_TYPE(base) ==
    900   1.1    briggs 			    PCI_MAPREG_MEM_TYPE_64BIT) {
    901   1.1    briggs 				base = (pcireg_t)
    902   1.1    briggs 				    (PCI_MAPREG_MEM64_ADDR(pm->address) >> 32);
    903   1.1    briggs 				pci_conf_write(pd->pc, pd->tag, pm->reg + 4,
    904   1.1    briggs 				    base);
    905   1.1    briggs 			}
    906   1.1    briggs 		}
    907   1.1    briggs 	}
    908  1.40   msaitoh 	for (pm = pb->pcimemwin; pm < &pb->pcimemwin[pb->nmemwin]; pm++) {
    909   1.1    briggs 		if (pm->reg == PCI_MAPREG_ROM && pm->address != -1) {
    910   1.1    briggs 			pd = pm->dev;
    911  1.29   gdamore 			if (!(pd->enable & PCI_CONF_MAP_ROM))
    912  1.28   gdamore 				continue;
    913   1.1    briggs 			if (pci_conf_debug) {
    914   1.1    briggs 				print_tag(pd->pc, pd->tag);
    915   1.1    briggs 				printf(
    916  1.23       scw 				    "Putting %" PRIu64 " ROM bytes @ %#"
    917  1.23       scw 				    PRIx64 " (reg %x)\n", pm->size,
    918  1.23       scw 				    pm->address, pm->reg);
    919   1.1    briggs 			}
    920  1.29   gdamore 			base = (pcireg_t) pm->address;
    921  1.29   gdamore 			if (pd->enable & PCI_CONF_ENABLE_ROM)
    922  1.29   gdamore 				base |= PCI_MAPREG_ROM_ENABLE;
    923  1.29   gdamore 
    924   1.1    briggs 			pci_conf_write(pd->pc, pd->tag, pm->reg, base);
    925   1.1    briggs 		}
    926   1.1    briggs 	}
    927   1.1    briggs 	return 0;
    928   1.1    briggs }
    929   1.1    briggs 
    930  1.44   thorpej static bool
    931  1.44   thorpej constrain_bridge_mem_range(struct extent * const ex,
    932  1.44   thorpej 			   u_long * const base,
    933  1.44   thorpej 			   u_long * const limit,
    934  1.44   thorpej 			   const bool ok64 __used_only_lp64)
    935  1.44   thorpej {
    936  1.44   thorpej 
    937  1.44   thorpej 	*base = ex->ex_start;
    938  1.44   thorpej 	*limit = ex->ex_end;
    939  1.44   thorpej 
    940  1.44   thorpej #ifdef _LP64
    941  1.44   thorpej 	if (!ok64) {
    942  1.44   thorpej 		if (ex->ex_start >= (1UL << 32)) {
    943  1.44   thorpej 			return true;
    944  1.44   thorpej 		}
    945  1.44   thorpej 		if (ex->ex_end > 0xffffffffUL) {
    946  1.44   thorpej 			*limit = 0xffffffffUL;
    947  1.44   thorpej 		}
    948  1.44   thorpej 	}
    949  1.44   thorpej #endif /* _LP64 */
    950  1.44   thorpej 
    951  1.44   thorpej 	return false;
    952  1.44   thorpej }
    953  1.44   thorpej 
    954   1.1    briggs /*
    955   1.1    briggs  * Configure I/O, memory, and prefetcable memory spaces, then make
    956   1.1    briggs  * a call to configure_bus().
    957   1.1    briggs  */
    958   1.1    briggs static int
    959   1.1    briggs configure_bridge(pciconf_dev_t *pd)
    960   1.1    briggs {
    961   1.1    briggs 	unsigned long	io_base, io_limit, mem_base, mem_limit;
    962   1.1    briggs 	pciconf_bus_t	*pb;
    963   1.1    briggs 	pcireg_t	io, iohigh, mem, cmd;
    964   1.1    briggs 	int		rv;
    965  1.38   msaitoh 	bool		isprefetchmem64;
    966  1.44   thorpej 	bool		bad_range;
    967   1.1    briggs 
    968   1.1    briggs 	pb = pd->ppb;
    969   1.1    briggs 	/* Configure I/O base & limit*/
    970   1.1    briggs 	if (pb->ioext) {
    971   1.1    briggs 		io_base = pb->ioext->ex_start;
    972   1.1    briggs 		io_limit = pb->ioext->ex_end;
    973   1.2    briggs 	} else {
    974   1.2    briggs 		io_base  = 0x1000;	/* 4K */
    975   1.2    briggs 		io_limit = 0x0000;
    976   1.1    briggs 	}
    977   1.2    briggs 	if (pb->io_32bit) {
    978  1.41   msaitoh 		iohigh = __SHIFTIN(io_base >> 16, PCI_BRIDGE_IOHIGH_BASE) |
    979  1.41   msaitoh 		    __SHIFTIN(io_limit >> 16, PCI_BRIDGE_IOHIGH_LIMIT);
    980   1.2    briggs 	} else {
    981   1.2    briggs 		if (io_limit > 0xFFFF) {
    982   1.2    briggs 			printf("Bus %d bridge does not support 32-bit I/O.  ",
    983   1.2    briggs 			    pb->busno);
    984   1.2    briggs 			printf("Disabling I/O accesses\n");
    985   1.2    briggs 			io_base  = 0x1000;	/* 4K */
    986   1.2    briggs 			io_limit = 0x0000;
    987   1.2    briggs 		}
    988   1.2    briggs 		iohigh = 0;
    989   1.2    briggs 	}
    990   1.9    briggs 	io = pci_conf_read(pb->pc, pd->tag, PCI_BRIDGE_STATIO_REG) &
    991  1.41   msaitoh 	    PCI_BRIDGE_STATIO_STATUS;
    992  1.41   msaitoh 	io |= __SHIFTIN((io_base >> 8) & PCI_BRIDGE_STATIO_IOADDR,
    993  1.41   msaitoh 	    PCI_BRIDGE_STATIO_IOBASE);
    994  1.41   msaitoh 	io |= __SHIFTIN((io_limit >> 8) & PCI_BRIDGE_STATIO_IOADDR,
    995  1.41   msaitoh 	    PCI_BRIDGE_STATIO_IOLIMIT);
    996   1.2    briggs 	pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_STATIO_REG, io);
    997   1.2    briggs 	pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_IOHIGH_REG, iohigh);
    998   1.1    briggs 
    999   1.1    briggs 	/* Configure mem base & limit */
   1000  1.44   thorpej 	bad_range = false;
   1001   1.1    briggs 	if (pb->memext) {
   1002  1.44   thorpej 		bad_range = constrain_bridge_mem_range(pb->memext,
   1003  1.44   thorpej 						       &mem_base,
   1004  1.44   thorpej 						       &mem_limit,
   1005  1.44   thorpej 						       false);
   1006   1.2    briggs 	} else {
   1007   1.2    briggs 		mem_base  = 0x100000;	/* 1M */
   1008   1.2    briggs 		mem_limit = 0x000000;
   1009   1.1    briggs 	}
   1010  1.44   thorpej 	if (bad_range) {
   1011   1.2    briggs 		printf("Bus %d bridge MEM range out of range.  ", pb->busno);
   1012   1.2    briggs 		printf("Disabling MEM accesses\n");
   1013   1.2    briggs 		mem_base  = 0x100000;	/* 1M */
   1014   1.2    briggs 		mem_limit = 0x000000;
   1015   1.2    briggs 	}
   1016  1.41   msaitoh 	mem = __SHIFTIN((mem_base >> 16) & PCI_BRIDGE_MEMORY_ADDR,
   1017  1.41   msaitoh 	    PCI_BRIDGE_MEMORY_BASE);
   1018  1.41   msaitoh 	mem |= __SHIFTIN((mem_limit >> 16) & PCI_BRIDGE_MEMORY_ADDR,
   1019  1.41   msaitoh 	    PCI_BRIDGE_MEMORY_LIMIT);
   1020   1.2    briggs 	pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_MEMORY_REG, mem);
   1021   1.1    briggs 
   1022   1.1    briggs 	/* Configure prefetchable mem base & limit */
   1023  1.44   thorpej 	mem = pci_conf_read(pb->pc, pd->tag, PCI_BRIDGE_PREFETCHMEM_REG);
   1024  1.44   thorpej 	isprefetchmem64 = PCI_BRIDGE_PREFETCHMEM_64BITS(mem);
   1025  1.44   thorpej 	bad_range = false;
   1026   1.1    briggs 	if (pb->pmemext) {
   1027  1.44   thorpej 		bad_range = constrain_bridge_mem_range(pb->pmemext,
   1028  1.44   thorpej 						       &mem_base,
   1029  1.44   thorpej 						       &mem_limit,
   1030  1.44   thorpej 						       isprefetchmem64);
   1031   1.2    briggs 	} else {
   1032   1.2    briggs 		mem_base  = 0x100000;	/* 1M */
   1033   1.2    briggs 		mem_limit = 0x000000;
   1034   1.1    briggs 	}
   1035  1.44   thorpej 	if (bad_range) {
   1036   1.2    briggs 		printf("Bus %d bridge does not support 64-bit PMEM.  ",
   1037   1.2    briggs 		    pb->busno);
   1038   1.2    briggs 		printf("Disabling prefetchable-MEM accesses\n");
   1039   1.2    briggs 		mem_base  = 0x100000;	/* 1M */
   1040   1.2    briggs 		mem_limit = 0x000000;
   1041   1.2    briggs 	}
   1042  1.41   msaitoh 	mem = __SHIFTIN((mem_base >> 16) & PCI_BRIDGE_PREFETCHMEM_ADDR,
   1043  1.41   msaitoh 	    PCI_BRIDGE_PREFETCHMEM_BASE);
   1044  1.41   msaitoh 	mem |= __SHIFTIN((mem_limit >> 16) & PCI_BRIDGE_PREFETCHMEM_ADDR,
   1045  1.41   msaitoh 	    PCI_BRIDGE_PREFETCHMEM_LIMIT);
   1046   1.2    briggs 	pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_PREFETCHMEM_REG, mem);
   1047   1.2    briggs 	/*
   1048   1.2    briggs 	 * XXX -- 64-bit systems need a lot more than just this...
   1049   1.2    briggs 	 */
   1050  1.38   msaitoh 	if (isprefetchmem64) {
   1051  1.39   msaitoh 		mem_base  = (uint64_t)mem_base  >> 32;
   1052  1.39   msaitoh 		mem_limit = (uint64_t)mem_limit >> 32;
   1053  1.41   msaitoh 		pci_conf_write(pb->pc, pd->tag,
   1054  1.41   msaitoh 		    PCI_BRIDGE_PREFETCHBASEUP32_REG, mem_base & 0xffffffff);
   1055  1.41   msaitoh 		pci_conf_write(pb->pc, pd->tag,
   1056  1.41   msaitoh 		    PCI_BRIDGE_PREFETCHLIMITUP32_REG, mem_limit & 0xffffffff);
   1057  1.32      matt 	}
   1058   1.1    briggs 
   1059   1.1    briggs 	rv = configure_bus(pb);
   1060   1.1    briggs 
   1061   1.1    briggs 	if (pb->ioext)
   1062   1.1    briggs 		extent_destroy(pb->ioext);
   1063   1.1    briggs 	if (pb->memext)
   1064   1.1    briggs 		extent_destroy(pb->memext);
   1065   1.1    briggs 	if (pb->pmemext)
   1066   1.1    briggs 		extent_destroy(pb->pmemext);
   1067   1.1    briggs 	if (rv == 0) {
   1068   1.1    briggs 		cmd = pci_conf_read(pd->pc, pd->tag, PCI_BRIDGE_CONTROL_REG);
   1069  1.41   msaitoh 		cmd &= ~PCI_BRIDGE_CONTROL; /* Clear control bit first */
   1070  1.41   msaitoh 		cmd |= PCI_BRIDGE_CONTROL_PERE | PCI_BRIDGE_CONTROL_SERR;
   1071  1.41   msaitoh 		if (pb->fast_b2b)
   1072  1.41   msaitoh 			cmd |= PCI_BRIDGE_CONTROL_SECFASTB2B;
   1073  1.41   msaitoh 
   1074   1.1    briggs 		pci_conf_write(pd->pc, pd->tag, PCI_BRIDGE_CONTROL_REG, cmd);
   1075   1.1    briggs 		cmd = pci_conf_read(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG);
   1076   1.1    briggs 		cmd |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE;
   1077   1.1    briggs 		pci_conf_write(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG, cmd);
   1078   1.1    briggs 	}
   1079   1.1    briggs 
   1080   1.1    briggs 	return rv;
   1081   1.1    briggs }
   1082   1.1    briggs 
   1083   1.1    briggs /*
   1084   1.1    briggs  * Calculate latency values, allocate I/O and MEM segments, then set them
   1085   1.1    briggs  * up.  If a PCI-PCI bridge is found, configure the bridge separately,
   1086   1.1    briggs  * which will cause a recursive call back here.
   1087   1.1    briggs  */
   1088   1.1    briggs static int
   1089   1.1    briggs configure_bus(pciconf_bus_t *pb)
   1090   1.1    briggs {
   1091   1.1    briggs 	pciconf_dev_t	*pd;
   1092   1.8    briggs 	int		def_ltim, max_ltim, band, bus_mhz;
   1093   1.1    briggs 
   1094  1.20    simonb 	if (pb->ndevs == 0) {
   1095  1.20    simonb 		if (pci_conf_debug)
   1096  1.20    simonb 			printf("PCI bus %d - no devices\n", pb->busno);
   1097  1.39   msaitoh 		return 1;
   1098  1.20    simonb 	}
   1099   1.8    briggs 	bus_mhz = pb->freq_66 ? 66 : 33;
   1100   1.8    briggs 	max_ltim = pb->max_mingnt * bus_mhz / 4;	/* cvt to cycle count */
   1101  1.30    briggs 	band = 4000000;					/* 0.25us cycles/sec */
   1102   1.1    briggs 	if (band < pb->bandwidth_used) {
   1103  1.31     gavan 		printf("PCI bus %d: Warning: Total bandwidth exceeded!? (%d)\n",
   1104  1.31     gavan 		    pb->busno, pb->bandwidth_used);
   1105   1.1    briggs 		def_ltim = -1;
   1106   1.1    briggs 	} else {
   1107   1.1    briggs 		def_ltim = (band - pb->bandwidth_used) / pb->ndevs;
   1108   1.1    briggs 		if (def_ltim > pb->min_maxlat)
   1109   1.1    briggs 			def_ltim = pb->min_maxlat;
   1110   1.8    briggs 		def_ltim = def_ltim * bus_mhz / 4;
   1111   1.1    briggs 	}
   1112   1.1    briggs 	def_ltim = (def_ltim + 7) & ~7;
   1113   1.1    briggs 	max_ltim = (max_ltim + 7) & ~7;
   1114   1.1    briggs 
   1115  1.43   msaitoh 	pb->def_ltim = MIN(def_ltim, 255);
   1116  1.43   msaitoh 	pb->max_ltim = MIN(MAX(max_ltim, def_ltim), 255);
   1117   1.1    briggs 
   1118   1.1    briggs 	/*
   1119   1.1    briggs 	 * Now we have what we need to initialize the devices.
   1120   1.1    briggs 	 * It would probably be better if we could allocate all of these
   1121   1.1    briggs 	 * for all busses at once, but "not right now".  First, get a list
   1122   1.1    briggs 	 * of free memory ranges from the m.d. system.
   1123   1.1    briggs 	 */
   1124   1.1    briggs 	if (setup_iowins(pb) || setup_memwins(pb)) {
   1125  1.36      matt 		printf("PCI bus configuration failed: "
   1126  1.36      matt 		"unable to assign all I/O and memory ranges.\n");
   1127   1.1    briggs 		return -1;
   1128   1.1    briggs 	}
   1129   1.1    briggs 
   1130   1.1    briggs 	/*
   1131   1.1    briggs 	 * Configure the latency for the devices, and enable them.
   1132   1.1    briggs 	 */
   1133  1.40   msaitoh 	for (pd = pb->device; pd < &pb->device[pb->ndevs]; pd++) {
   1134  1.37      matt 		pcireg_t cmd, classreg, misc;
   1135   1.1    briggs 		int	ltim;
   1136   1.1    briggs 
   1137   1.1    briggs 		if (pci_conf_debug) {
   1138   1.1    briggs 			print_tag(pd->pc, pd->tag);
   1139   1.1    briggs 			printf("Configuring device.\n");
   1140   1.1    briggs 		}
   1141  1.37      matt 		classreg = pci_conf_read(pd->pc, pd->tag, PCI_CLASS_REG);
   1142   1.1    briggs 		misc = pci_conf_read(pd->pc, pd->tag, PCI_BHLC_REG);
   1143   1.1    briggs 		cmd = pci_conf_read(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG);
   1144  1.26   tsutsui 		if (pd->enable & PCI_CONF_ENABLE_PARITY)
   1145  1.26   tsutsui 			cmd |= PCI_COMMAND_PARITY_ENABLE;
   1146  1.26   tsutsui 		if (pd->enable & PCI_CONF_ENABLE_SERR)
   1147  1.26   tsutsui 			cmd |= PCI_COMMAND_SERR_ENABLE;
   1148   1.1    briggs 		if (pb->fast_b2b)
   1149   1.1    briggs 			cmd |= PCI_COMMAND_BACKTOBACK_ENABLE;
   1150  1.37      matt 		if (PCI_CLASS(classreg) != PCI_CLASS_BRIDGE ||
   1151  1.37      matt 		    PCI_SUBCLASS(classreg) != PCI_SUBCLASS_BRIDGE_PCI) {
   1152   1.8    briggs 			if (pd->enable & PCI_CONF_ENABLE_IO)
   1153   1.8    briggs 				cmd |= PCI_COMMAND_IO_ENABLE;
   1154   1.8    briggs 			if (pd->enable & PCI_CONF_ENABLE_MEM)
   1155   1.8    briggs 				cmd |= PCI_COMMAND_MEM_ENABLE;
   1156   1.8    briggs 			if (pd->enable & PCI_CONF_ENABLE_BM)
   1157   1.8    briggs 				cmd |= PCI_COMMAND_MASTER_ENABLE;
   1158   1.8    briggs 			ltim = pd->min_gnt * bus_mhz / 4;
   1159   1.1    briggs 			ltim = MIN (MAX (pb->def_ltim, ltim), pb->max_ltim);
   1160   1.1    briggs 		} else {
   1161   1.8    briggs 			cmd |= PCI_COMMAND_MASTER_ENABLE;
   1162   1.1    briggs 			ltim = MIN (pb->def_ltim, pb->max_ltim);
   1163   1.1    briggs 		}
   1164  1.26   tsutsui 		if ((pd->enable &
   1165  1.43   msaitoh 		    (PCI_CONF_ENABLE_MEM | PCI_CONF_ENABLE_IO)) == 0) {
   1166   1.2    briggs 			print_tag(pd->pc, pd->tag);
   1167   1.2    briggs 			printf("Disabled due to lack of resources.\n");
   1168   1.2    briggs 			cmd &= ~(PCI_COMMAND_MASTER_ENABLE |
   1169   1.2    briggs 			    PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE);
   1170   1.2    briggs 		}
   1171   1.1    briggs 		pci_conf_write(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG, cmd);
   1172   1.1    briggs 
   1173  1.14   thorpej 		misc &= ~((PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT) |
   1174  1.14   thorpej 		    (PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT));
   1175  1.14   thorpej 		misc |= (ltim & PCI_LATTIMER_MASK) << PCI_LATTIMER_SHIFT;
   1176  1.15    kleink 		misc |= ((pb->cacheline_size >> 2) & PCI_CACHELINE_MASK) <<
   1177  1.14   thorpej 		    PCI_CACHELINE_SHIFT;
   1178   1.1    briggs 		pci_conf_write(pd->pc, pd->tag, PCI_BHLC_REG, misc);
   1179   1.1    briggs 
   1180   1.1    briggs 		if (pd->ppb) {
   1181   1.1    briggs 			if (configure_bridge(pd) < 0)
   1182   1.1    briggs 				return -1;
   1183   1.1    briggs 			continue;
   1184   1.1    briggs 		}
   1185   1.1    briggs 	}
   1186   1.1    briggs 
   1187  1.39   msaitoh 	if (pci_conf_debug)
   1188   1.1    briggs 		printf("PCI bus %d configured\n", pb->busno);
   1189   1.1    briggs 
   1190   1.1    briggs 	return 0;
   1191   1.1    briggs }
   1192   1.1    briggs 
   1193  1.44   thorpej static bool
   1194  1.44   thorpej mem_region_ok64(struct extent * const ex __used_only_lp64)
   1195  1.44   thorpej {
   1196  1.44   thorpej 	bool rv = false;
   1197  1.44   thorpej 
   1198  1.44   thorpej #ifdef _LP64
   1199  1.44   thorpej 	/*
   1200  1.44   thorpej 	 * XXX We need to guard this with _LP64 because
   1201  1.44   thorpej 	 * extent maps use u_long internally.
   1202  1.44   thorpej 	 */
   1203  1.44   thorpej 	u_long addr64;
   1204  1.44   thorpej 	if (ex->ex_end > (1UL << 32) &&
   1205  1.45     skrll 	    extent_alloc_subregion(ex, MAX((1UL << 32), ex->ex_start),
   1206  1.44   thorpej 				   ex->ex_end,
   1207  1.44   thorpej 				   1 /* size */,
   1208  1.44   thorpej 				   1 /* alignment */,
   1209  1.44   thorpej 				   0 /* boundary */,
   1210  1.44   thorpej 				   EX_NOWAIT,
   1211  1.44   thorpej 				   &addr64) == 0) {
   1212  1.44   thorpej 		(void) extent_free(ex, addr64,
   1213  1.44   thorpej 				   1 /* size */,
   1214  1.44   thorpej 				   EX_NOWAIT);
   1215  1.44   thorpej 		rv = true;
   1216  1.44   thorpej 	}
   1217  1.44   thorpej #endif /* _LP64 */
   1218  1.44   thorpej 
   1219  1.44   thorpej 	return rv;
   1220  1.44   thorpej }
   1221  1.44   thorpej 
   1222   1.1    briggs /*
   1223   1.1    briggs  * Let's configure the PCI bus.
   1224   1.1    briggs  * This consists of basically scanning for all existing devices,
   1225   1.1    briggs  * identifying their needs, and then making another pass over them
   1226   1.1    briggs  * to set:
   1227   1.1    briggs  *	1. I/O addresses
   1228   1.1    briggs  *	2. Memory addresses (Prefetchable and not)
   1229   1.1    briggs  *	3. PCI command register
   1230   1.1    briggs  *	4. The latency part of the PCI BHLC (BIST (Built-In Self Test),
   1231   1.1    briggs  *	    Header type, Latency timer, Cache line size) register
   1232   1.1    briggs  *
   1233   1.1    briggs  * The command register is set to enable fast back-to-back transactions
   1234  1.25     perry  * if the host bridge says it can handle it.  We also configure
   1235   1.1    briggs  * Master Enable, SERR enable, parity enable, and (if this is not a
   1236   1.1    briggs  * PCI-PCI bridge) the I/O and Memory spaces.  Apparently some devices
   1237   1.1    briggs  * will not report some I/O space.
   1238   1.1    briggs  *
   1239   1.1    briggs  * The latency is computed to be a "fair share" of the bus bandwidth.
   1240   1.1    briggs  * The bus bandwidth variable is initialized to the number of PCI cycles
   1241   1.1    briggs  * in one second.  The number of cycles taken for one transaction by each
   1242   1.1    briggs  * device (MAX_LAT + MIN_GNT) is then subtracted from the bandwidth.
   1243   1.1    briggs  * Care is taken to ensure that the latency timer won't be set such that
   1244   1.1    briggs  * it would exceed the critical time for any device.
   1245   1.1    briggs  *
   1246   1.1    briggs  * This is complicated somewhat due to the presence of bridges.  PCI-PCI
   1247   1.1    briggs  * bridges are probed and configured recursively.
   1248   1.1    briggs  */
   1249   1.1    briggs int
   1250   1.1    briggs pci_configure_bus(pci_chipset_tag_t pc, struct extent *ioext,
   1251  1.14   thorpej     struct extent *memext, struct extent *pmemext, int firstbus,
   1252  1.14   thorpej     int cacheline_size)
   1253   1.1    briggs {
   1254   1.1    briggs 	pciconf_bus_t	*pb;
   1255   1.1    briggs 	int		rv;
   1256   1.1    briggs 
   1257  1.42       chs 	pb = kmem_zalloc(sizeof (pciconf_bus_t), KM_SLEEP);
   1258  1.12   thorpej 	pb->busno = firstbus;
   1259   1.1    briggs 	pb->next_busno = pb->busno + 1;
   1260   1.1    briggs 	pb->last_busno = 255;
   1261  1.14   thorpej 	pb->cacheline_size = cacheline_size;
   1262   1.1    briggs 	pb->parent_bus = NULL;
   1263   1.1    briggs 	pb->swiz = 0;
   1264   1.2    briggs 	pb->io_32bit = 1;
   1265   1.1    briggs 	pb->ioext = ioext;
   1266   1.1    briggs 	pb->memext = memext;
   1267  1.39   msaitoh 	if (pmemext == NULL)
   1268   1.1    briggs 		pb->pmemext = memext;
   1269  1.39   msaitoh 	else
   1270   1.1    briggs 		pb->pmemext = pmemext;
   1271  1.39   msaitoh 
   1272  1.44   thorpej 	/*
   1273  1.44   thorpej 	 * Probe the memory region extent maps to see
   1274  1.44   thorpej 	 * if allocation of 64-bit addresses is possible.
   1275  1.44   thorpej 	 */
   1276  1.44   thorpej 	pb->mem_64bit = mem_region_ok64(pb->memext);
   1277  1.44   thorpej 	pb->pmem_64bit = mem_region_ok64(pb->pmemext);
   1278  1.44   thorpej 
   1279   1.1    briggs 	pb->pc = pc;
   1280   1.1    briggs 	pb->io_total = pb->mem_total = pb->pmem_total = 0;
   1281   1.1    briggs 
   1282   1.1    briggs 	rv = probe_bus(pb);
   1283  1.40   msaitoh 	pb->last_busno = pb->next_busno - 1;
   1284  1.39   msaitoh 	if (rv == 0)
   1285   1.1    briggs 		rv = configure_bus(pb);
   1286   1.1    briggs 
   1287   1.1    briggs 	/*
   1288   1.1    briggs 	 * All done!
   1289   1.1    briggs 	 */
   1290  1.32      matt 	kmem_free(pb, sizeof(*pb));
   1291   1.1    briggs 	return rv;
   1292   1.1    briggs }
   1293