Home | History | Annotate | Line # | Download | only in pci
pciconf.c revision 1.5.2.4
      1  1.5.2.4  jdolecek /*	$NetBSD: pciconf.c,v 1.5.2.4 2002/09/06 08:45:24 jdolecek 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.5.2.1   thorpej  *    - Perform all data structure allocation dynamically, don't have
     44  1.5.2.1   thorpej  *	statically-sized arrays ("oops, you lose because you have too
     45  1.5.2.1   thorpej  *	many slots filled!")
     46  1.5.2.1   thorpej  *    - Do this in 2 passes, with an MD hook to control the behavior:
     47  1.5.2.1   thorpej  *		(1) Configure the bus (possibly including expansion
     48  1.5.2.1   thorpej  *		    ROMs.
     49  1.5.2.1   thorpej  *		(2) Another pass to disable expansion ROMs if they're
     50  1.5.2.1   thorpej  *		    mapped (since you're not supposed to leave them
     51  1.5.2.1   thorpej  *		    mapped when you're not using them).
     52  1.5.2.1   thorpej  *	This would facilitate MD code executing the expansion ROMs
     53  1.5.2.1   thorpej  *	if necessary (possibly with an x86 emulator) to configure
     54  1.5.2.1   thorpej  *	devices (e.g. VGA cards).
     55      1.2    briggs  *    - Deal with "anything can be hot-plugged" -- i.e., carry configuration
     56  1.5.2.1   thorpej  *	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.5.2.1   thorpej  *    - Worry about changing a bridge to/from transparency
     62  1.5.2.1   thorpej  * From thorpej (05/25/01)
     63  1.5.2.1   thorpej  *    - Try to handle devices that are already configured (perhaps using that
     64  1.5.2.1   thorpej  *      as a hint to where we put other devices)
     65      1.2    briggs  */
     66      1.2    briggs 
     67  1.5.2.2   thorpej #include <sys/cdefs.h>
     68  1.5.2.4  jdolecek __KERNEL_RCSID(0, "$NetBSD: pciconf.c,v 1.5.2.4 2002/09/06 08:45:24 jdolecek Exp $");
     69  1.5.2.2   thorpej 
     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.1    briggs 
     78      1.1    briggs #include <dev/pci/pcivar.h>
     79      1.1    briggs #include <dev/pci/pciconf.h>
     80      1.1    briggs #include <dev/pci/pcidevs.h>
     81      1.1    briggs 
     82      1.1    briggs int pci_conf_debug = 0;
     83      1.1    briggs 
     84      1.1    briggs #if !defined(MIN)
     85      1.1    briggs #define	MIN(a,b) (((a)<(b))?(a):(b))
     86      1.1    briggs #define	MAX(a,b) (((a)>(b))?(a):(b))
     87      1.1    briggs #endif
     88      1.1    briggs 
     89      1.1    briggs /* per-bus constants. */
     90  1.5.2.1   thorpej #define MAX_CONF_DEV	32			/* Arbitrary */
     91      1.1    briggs #define MAX_CONF_MEM	(3 * MAX_CONF_DEV)	/* Avg. 3 per device -- Arb. */
     92  1.5.2.1   thorpej #define MAX_CONF_IO	(3 * MAX_CONF_DEV)	/* Avg. 1 per device -- Arb. */
     93      1.1    briggs 
     94      1.1    briggs struct _s_pciconf_bus_t;			/* Forward declaration */
     95      1.1    briggs 
     96      1.1    briggs typedef struct _s_pciconf_dev_t {
     97      1.1    briggs 	int		ipin;
     98      1.1    briggs 	int		iline;
     99      1.1    briggs 	int		min_gnt;
    100      1.1    briggs 	int		max_lat;
    101      1.2    briggs 	int		enable;
    102      1.1    briggs 	pcitag_t	tag;
    103      1.1    briggs 	pci_chipset_tag_t	pc;
    104      1.1    briggs 	struct _s_pciconf_bus_t	*ppb;		/* I am really a bridge */
    105      1.1    briggs } pciconf_dev_t;
    106      1.1    briggs 
    107      1.1    briggs typedef struct _s_pciconf_win_t {
    108      1.1    briggs 	pciconf_dev_t	*dev;
    109      1.1    briggs 	int		reg;			/* 0 for busses */
    110      1.1    briggs 	int		align;
    111      1.1    briggs 	int		prefetch;
    112      1.1    briggs 	u_int64_t	size;
    113      1.1    briggs 	u_int64_t	address;
    114      1.1    briggs } pciconf_win_t;
    115      1.1    briggs 
    116      1.1    briggs typedef struct _s_pciconf_bus_t {
    117      1.1    briggs 	int		busno;
    118      1.1    briggs 	int		next_busno;
    119      1.1    briggs 	int		last_busno;
    120      1.1    briggs 	int		max_mingnt;
    121      1.1    briggs 	int		min_maxlat;
    122  1.5.2.2   thorpej 	int		cacheline_size;
    123      1.1    briggs 	int		prefetch;
    124      1.1    briggs 	int		fast_b2b;
    125      1.1    briggs 	int		freq_66;
    126      1.1    briggs 	int		def_ltim;
    127      1.1    briggs 	int		max_ltim;
    128      1.1    briggs 	int		bandwidth_used;
    129      1.1    briggs 	int		swiz;
    130      1.2    briggs 	int		io_32bit;
    131      1.2    briggs 	int		pmem_64bit;
    132      1.1    briggs 
    133      1.1    briggs 	int		ndevs;
    134      1.1    briggs 	pciconf_dev_t	device[MAX_CONF_DEV];
    135      1.1    briggs 
    136      1.1    briggs 	/* These should be sorted in order of decreasing size */
    137      1.1    briggs 	int		nmemwin;
    138      1.1    briggs 	pciconf_win_t	pcimemwin[MAX_CONF_MEM];
    139      1.1    briggs 	int		niowin;
    140      1.1    briggs 	pciconf_win_t	pciiowin[MAX_CONF_IO];
    141      1.1    briggs 
    142      1.1    briggs 	bus_size_t	io_total;
    143      1.1    briggs 	bus_size_t	mem_total;
    144      1.1    briggs 	bus_size_t	pmem_total;
    145      1.1    briggs 
    146      1.1    briggs 	struct extent	*ioext;
    147      1.1    briggs 	struct extent	*memext;
    148      1.1    briggs 	struct extent	*pmemext;
    149      1.1    briggs 
    150      1.1    briggs 	pci_chipset_tag_t	pc;
    151      1.1    briggs 	struct _s_pciconf_bus_t *parent_bus;
    152      1.1    briggs } pciconf_bus_t;
    153      1.1    briggs 
    154      1.1    briggs static int	probe_bus(pciconf_bus_t *);
    155      1.1    briggs static void	alloc_busno(pciconf_bus_t *, pciconf_bus_t *);
    156      1.4    simonb static int	pci_do_device_query(pciconf_bus_t *, pcitag_t, int, int, int);
    157      1.1    briggs static int	setup_iowins(pciconf_bus_t *);
    158      1.1    briggs static int	setup_memwins(pciconf_bus_t *);
    159      1.1    briggs static int	configure_bridge(pciconf_dev_t *);
    160      1.1    briggs static int	configure_bus(pciconf_bus_t *);
    161      1.1    briggs static u_int64_t	pci_allocate_range(struct extent *, u_int64_t, int);
    162      1.1    briggs static pciconf_win_t	*get_io_desc(pciconf_bus_t *, bus_size_t);
    163      1.1    briggs static pciconf_win_t	*get_mem_desc(pciconf_bus_t *, bus_size_t);
    164      1.1    briggs static pciconf_bus_t	*query_bus(pciconf_bus_t *, pciconf_dev_t *, int);
    165      1.1    briggs 
    166      1.1    briggs static void	print_tag(pci_chipset_tag_t, pcitag_t);
    167      1.1    briggs 
    168      1.1    briggs static void
    169      1.1    briggs print_tag(pci_chipset_tag_t pc, pcitag_t tag)
    170      1.1    briggs {
    171      1.1    briggs 	int	bus, dev, func;
    172      1.1    briggs 
    173      1.1    briggs 	pci_decompose_tag(pc, tag, &bus, &dev, &func);
    174      1.1    briggs 	printf("PCI: bus %d, device %d, function %d: ", bus, dev, func);
    175      1.1    briggs }
    176      1.1    briggs 
    177      1.1    briggs /************************************************************************/
    178      1.1    briggs /************************************************************************/
    179      1.1    briggs /***********************   Bus probing routines   ***********************/
    180      1.1    briggs /************************************************************************/
    181      1.1    briggs /************************************************************************/
    182      1.1    briggs static pciconf_win_t *
    183      1.1    briggs get_io_desc(pciconf_bus_t *pb, bus_size_t size)
    184      1.1    briggs {
    185      1.1    briggs 	int	i, n;
    186      1.1    briggs 
    187      1.1    briggs 	n = pb->niowin;
    188      1.1    briggs 	for (i=n; i > 0 && size > pb->pciiowin[i-1].size; i--)
    189      1.1    briggs 		pb->pciiowin[i] = pb->pciiowin[i-1]; /* struct copy */
    190      1.1    briggs 	return &pb->pciiowin[i];
    191      1.1    briggs }
    192      1.1    briggs 
    193      1.1    briggs static pciconf_win_t *
    194      1.1    briggs get_mem_desc(pciconf_bus_t *pb, bus_size_t size)
    195      1.1    briggs {
    196      1.1    briggs 	int	i, n;
    197      1.1    briggs 
    198      1.1    briggs 	n = pb->nmemwin;
    199      1.1    briggs 	for (i=n; i > 0 && size > pb->pcimemwin[i-1].size; i--)
    200      1.1    briggs 		pb->pcimemwin[i] = pb->pcimemwin[i-1]; /* struct copy */
    201      1.1    briggs 	return &pb->pcimemwin[i];
    202      1.1    briggs }
    203      1.1    briggs 
    204      1.1    briggs /*
    205      1.1    briggs  * Set up bus common stuff, then loop over devices & functions.
    206      1.1    briggs  * If we find something, call pci_do_device_query()).
    207      1.1    briggs  */
    208      1.1    briggs static int
    209      1.1    briggs probe_bus(pciconf_bus_t *pb)
    210      1.1    briggs {
    211      1.1    briggs 	int device, maxdevs;
    212  1.5.2.1   thorpej #ifdef __PCI_BUS_DEVORDER
    213  1.5.2.1   thorpej 	char devs[32];
    214  1.5.2.1   thorpej 	int  i;
    215  1.5.2.1   thorpej #endif
    216      1.1    briggs 
    217      1.1    briggs 	maxdevs = pci_bus_maxdevs(pb->pc, pb->busno);
    218      1.1    briggs 	pb->ndevs = 0;
    219      1.1    briggs 	pb->niowin = 0;
    220      1.1    briggs 	pb->nmemwin = 0;
    221      1.1    briggs 	pb->freq_66 = 1;
    222      1.1    briggs 	pb->fast_b2b = 1;
    223      1.1    briggs 	pb->prefetch = 1;
    224      1.1    briggs 	pb->max_mingnt = 0;	/* we are looking for the maximum */
    225      1.1    briggs 	pb->min_maxlat = 0x100;	/* we are looking for the minimum */
    226      1.1    briggs 	pb->bandwidth_used = 0;
    227      1.4    simonb 
    228  1.5.2.1   thorpej #ifdef __PCI_BUS_DEVORDER
    229  1.5.2.1   thorpej 	pci_bus_devorder(pb->pc, pb->busno, devs);
    230  1.5.2.1   thorpej 	for (i=0; (device=devs[i]) < 32 && device >= 0; i++) {
    231  1.5.2.1   thorpej #else
    232      1.1    briggs 	for (device=0; device < maxdevs; device++) {
    233  1.5.2.1   thorpej #endif
    234      1.1    briggs 		pcitag_t tag;
    235      1.1    briggs 		pcireg_t id, bhlcr;
    236      1.1    briggs 		int function, nfunction;
    237      1.4    simonb 		int confmode;
    238      1.1    briggs 
    239      1.1    briggs 		tag = pci_make_tag(pb->pc, pb->busno, device, 0);
    240      1.1    briggs 		if (pci_conf_debug) {
    241      1.1    briggs 			print_tag(pb->pc, tag);
    242      1.1    briggs 		}
    243      1.1    briggs 		id = pci_conf_read(pb->pc, tag, PCI_ID_REG);
    244      1.1    briggs 
    245      1.4    simonb 		if (pci_conf_debug) {
    246      1.4    simonb 			printf("id=%x: Vendor=%x, Product=%x\n",
    247      1.4    simonb 			    id, PCI_VENDOR(id),PCI_PRODUCT(id));
    248      1.4    simonb 		}
    249      1.1    briggs 		/* Invalid vendor ID value? */
    250      1.1    briggs 		if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    251      1.1    briggs 			continue;
    252      1.1    briggs 
    253      1.1    briggs 		bhlcr = pci_conf_read(pb->pc, tag, PCI_BHLC_REG);
    254      1.1    briggs 		nfunction = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
    255      1.1    briggs 		for (function = 0 ; function < nfunction ; function++) {
    256      1.1    briggs 			tag = pci_make_tag(pb->pc, pb->busno, device, function);
    257      1.1    briggs 			id = pci_conf_read(pb->pc, tag, PCI_ID_REG);
    258      1.1    briggs 			if (PCI_VENDOR(id) == PCI_VENDOR_INVALID)
    259      1.1    briggs 				continue;
    260      1.1    briggs 			if (pb->ndevs+1 < MAX_CONF_DEV) {
    261      1.1    briggs 				if (pci_conf_debug) {
    262      1.1    briggs 					print_tag(pb->pc, tag);
    263      1.3   thorpej 					printf("Found dev 0x%04x 0x%04x -- "
    264      1.3   thorpej 					    "really probing.\n",
    265      1.3   thorpej 					PCI_VENDOR(id), PCI_PRODUCT(id));
    266      1.1    briggs 				}
    267      1.4    simonb #ifdef __HAVE_PCI_CONF_HOOK
    268      1.4    simonb 				confmode = pci_conf_hook(pb->pc, pb->busno,
    269      1.4    simonb 				    device, function, id);
    270      1.4    simonb 				if (confmode == 0)
    271      1.4    simonb 					continue;
    272      1.4    simonb #else
    273  1.5.2.1   thorpej 				/*
    274  1.5.2.1   thorpej 				 * Don't enable expansion ROMS -- some cards
    275  1.5.2.1   thorpej 				 * share address decoders between the EXPROM
    276  1.5.2.1   thorpej 				 * and PCI memory space, and enabling the ROM
    277  1.5.2.1   thorpej 				 * when not needed will cause all sorts of
    278  1.5.2.1   thorpej 				 * lossage.
    279  1.5.2.1   thorpej 				 */
    280  1.5.2.1   thorpej 				confmode = PCI_CONF_ALL & ~PCI_CONF_MAP_ROM;
    281      1.4    simonb #endif
    282      1.1    briggs 				if (pci_do_device_query(pb, tag, device,
    283      1.4    simonb 				    function, confmode))
    284      1.1    briggs 					return -1;
    285      1.1    briggs 				pb->ndevs++;
    286      1.1    briggs 			}
    287      1.1    briggs 		}
    288      1.1    briggs 	}
    289      1.1    briggs 	return 0;
    290      1.1    briggs }
    291      1.1    briggs 
    292      1.1    briggs static void
    293      1.1    briggs alloc_busno(pciconf_bus_t *parent, pciconf_bus_t *pb)
    294      1.1    briggs {
    295      1.1    briggs 	pb->busno = parent->next_busno;
    296  1.5.2.4  jdolecek 	pb->next_busno = pb->busno + 1;
    297  1.5.2.4  jdolecek }
    298  1.5.2.4  jdolecek 
    299  1.5.2.4  jdolecek static void
    300  1.5.2.4  jdolecek set_busreg(pci_chipset_tag_t pc, pcitag_t tag, int prim, int sec, int sub)
    301  1.5.2.4  jdolecek {
    302  1.5.2.4  jdolecek 	pcireg_t	busreg;
    303  1.5.2.4  jdolecek 
    304  1.5.2.4  jdolecek 	busreg  =  prim << PCI_BRIDGE_BUS_PRIMARY_SHIFT;
    305  1.5.2.4  jdolecek 	busreg |=   sec << PCI_BRIDGE_BUS_SECONDARY_SHIFT;
    306  1.5.2.4  jdolecek 	busreg |=   sub << PCI_BRIDGE_BUS_SUBORDINATE_SHIFT;
    307  1.5.2.4  jdolecek 	pci_conf_write(pc, tag, PCI_BRIDGE_BUS_REG, busreg);
    308      1.1    briggs }
    309      1.1    briggs 
    310      1.1    briggs static pciconf_bus_t *
    311      1.1    briggs query_bus(pciconf_bus_t *parent, pciconf_dev_t *pd, int dev)
    312      1.1    briggs {
    313      1.1    briggs 	pciconf_bus_t	*pb;
    314  1.5.2.4  jdolecek 	pcireg_t	io, pmem;
    315      1.1    briggs 	pciconf_win_t	*pi, *pm;
    316      1.1    briggs 
    317      1.1    briggs 	pb = malloc (sizeof (pciconf_bus_t), M_DEVBUF, M_NOWAIT);
    318      1.1    briggs 	if (!pb)
    319      1.1    briggs 		panic("Unable to allocate memory for PCI configuration.");
    320      1.1    briggs 
    321  1.5.2.2   thorpej 	pb->cacheline_size = parent->cacheline_size;
    322      1.1    briggs 	pb->parent_bus = parent;
    323      1.1    briggs 	alloc_busno(parent, pb);
    324      1.1    briggs 
    325  1.5.2.4  jdolecek 	set_busreg(parent->pc, pd->tag, parent->busno, pb->busno, 0xff);
    326      1.1    briggs 
    327      1.1    briggs 	pb->swiz = parent->swiz + dev;
    328      1.1    briggs 
    329      1.1    briggs 	pb->ioext = NULL;
    330      1.1    briggs 	pb->memext = NULL;
    331      1.1    briggs 	pb->pmemext = NULL;
    332      1.1    briggs 	pb->pc = parent->pc;
    333      1.1    briggs 	pb->io_total = pb->mem_total = pb->pmem_total = 0;
    334      1.1    briggs 
    335      1.2    briggs 	pb->io_32bit = 0;
    336      1.2    briggs 	if (parent->io_32bit) {
    337  1.5.2.2   thorpej 		io = pci_conf_read(parent->pc, pd->tag, PCI_BRIDGE_STATIO_REG);
    338      1.2    briggs 		if (PCI_BRIDGE_IO_32BITS(io)) {
    339      1.2    briggs 			pb->io_32bit = 1;
    340      1.2    briggs 		}
    341      1.2    briggs 	}
    342      1.2    briggs 
    343      1.2    briggs 	pb->pmem_64bit = 0;
    344      1.2    briggs 	if (parent->pmem_64bit) {
    345  1.5.2.2   thorpej 		pmem = pci_conf_read(parent->pc, pd->tag,
    346      1.2    briggs 		    PCI_BRIDGE_PREFETCHMEM_REG);
    347      1.2    briggs 		if (PCI_BRIDGE_PREFETCHMEM_64BITS(pmem)) {
    348      1.2    briggs 			pb->pmem_64bit = 1;
    349      1.2    briggs 		}
    350      1.2    briggs 	}
    351      1.2    briggs 
    352      1.1    briggs 	if (probe_bus(pb)) {
    353      1.1    briggs 		printf("Failed to probe bus %d\n", pb->busno);
    354      1.1    briggs 		goto err;
    355      1.1    briggs 	}
    356      1.1    briggs 
    357  1.5.2.4  jdolecek 	/* We have found all subordinate busses now, reprogram busreg. */
    358  1.5.2.4  jdolecek 	pb->last_busno = pb->next_busno-1;
    359  1.5.2.4  jdolecek 	parent->next_busno = pb->next_busno;
    360  1.5.2.4  jdolecek 	set_busreg(parent->pc, pd->tag, parent->busno, pb->busno,
    361  1.5.2.4  jdolecek 		   pb->last_busno);
    362  1.5.2.4  jdolecek 	if (pci_conf_debug)
    363  1.5.2.4  jdolecek 		printf("PCI bus bridge (parent %d) covers busses %d-%d\n",
    364  1.5.2.4  jdolecek 			parent->busno, pb->busno, pb->last_busno);
    365  1.5.2.4  jdolecek 
    366      1.1    briggs 	if (pb->io_total > 0) {
    367      1.1    briggs 		if (parent->niowin >= MAX_CONF_IO) {
    368  1.5.2.1   thorpej 			printf("pciconf: too many I/O windows\n");
    369      1.1    briggs 			goto err;
    370      1.1    briggs 		}
    371      1.1    briggs 		pb->io_total |= 0xfff;	/* Round up */
    372      1.1    briggs 		pi = get_io_desc(parent, pb->io_total);
    373      1.1    briggs 		pi->dev = pd;
    374      1.1    briggs 		pi->reg = 0;
    375      1.1    briggs 		pi->size = pb->io_total;
    376      1.1    briggs 		pi->align = 0x1000;	/* 4K alignment */
    377      1.1    briggs 		pi->prefetch = 0;
    378      1.1    briggs 		parent->niowin++;
    379      1.1    briggs 		parent->io_total += pb->io_total;
    380      1.1    briggs 	}
    381      1.1    briggs 
    382      1.1    briggs 	if (pb->mem_total > 0) {
    383      1.1    briggs 		if (parent->nmemwin >= MAX_CONF_MEM) {
    384  1.5.2.1   thorpej 			printf("pciconf: too many MEM windows\n");
    385      1.1    briggs 			goto err;
    386      1.1    briggs 		}
    387      1.1    briggs 		pb->mem_total |= 0xfffff;	/* Round up */
    388      1.1    briggs 		pm = get_mem_desc(parent, pb->mem_total);
    389      1.1    briggs 		pm->dev = pd;
    390      1.1    briggs 		pm->reg = 0;
    391      1.1    briggs 		pm->size = pb->mem_total;
    392      1.1    briggs 		pm->align = 0x100000;	/* 1M alignment */
    393      1.1    briggs 		pm->prefetch = 0;
    394      1.1    briggs 		parent->nmemwin++;
    395      1.1    briggs 		parent->mem_total += pb->mem_total;
    396      1.1    briggs 	}
    397      1.1    briggs 
    398      1.1    briggs 	if (pb->pmem_total > 0) {
    399      1.1    briggs 		if (parent->nmemwin >= MAX_CONF_MEM) {
    400  1.5.2.1   thorpej 			printf("pciconf: too many MEM windows\n");
    401      1.1    briggs 			goto err;
    402      1.1    briggs 		}
    403      1.1    briggs 		pb->pmem_total |= 0xfffff;	/* Round up */
    404      1.1    briggs 		pm = get_mem_desc(parent, pb->pmem_total);
    405      1.1    briggs 		pm->dev = pd;
    406      1.1    briggs 		pm->reg = 0;
    407      1.1    briggs 		pm->size = pb->pmem_total;
    408      1.1    briggs 		pm->align = 0x100000;		/* 1M alignment */
    409      1.1    briggs 		pm->prefetch = 1;
    410      1.1    briggs 		parent->nmemwin++;
    411      1.1    briggs 		parent->pmem_total += pb->pmem_total;
    412      1.1    briggs 	}
    413      1.1    briggs 
    414      1.1    briggs 	return pb;
    415      1.1    briggs err:
    416      1.1    briggs 	free(pb, M_DEVBUF);
    417      1.1    briggs 	return NULL;
    418      1.1    briggs }
    419      1.1    briggs 
    420      1.1    briggs static int
    421      1.4    simonb pci_do_device_query(pciconf_bus_t *pb, pcitag_t tag, int dev, int func, int mode)
    422      1.1    briggs {
    423      1.1    briggs 	pciconf_dev_t	*pd;
    424      1.1    briggs 	pciconf_win_t	*pi, *pm;
    425      1.1    briggs 	pcireg_t	class, cmd, icr, bar, mask, bar64, mask64;
    426      1.1    briggs 	u_int64_t	size;
    427      1.1    briggs 	int		br, width;
    428      1.1    briggs 
    429      1.1    briggs 	pd = &pb->device[pb->ndevs];
    430      1.1    briggs 	pd->pc = pb->pc;
    431      1.1    briggs 	pd->tag = tag;
    432      1.1    briggs 	pd->ppb = NULL;
    433      1.4    simonb 	pd->enable = mode;
    434      1.1    briggs 
    435      1.1    briggs 	class = pci_conf_read(pb->pc, tag, PCI_CLASS_REG);
    436      1.1    briggs 
    437      1.1    briggs 	cmd = pci_conf_read(pb->pc, tag, PCI_COMMAND_STATUS_REG);
    438      1.1    briggs 
    439      1.1    briggs 	if (PCI_CLASS(class) != PCI_CLASS_BRIDGE) {
    440      1.1    briggs 		cmd &= ~(PCI_COMMAND_MASTER_ENABLE |
    441      1.1    briggs 		    PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE);
    442      1.1    briggs 		pci_conf_write(pb->pc, tag, PCI_COMMAND_STATUS_REG, cmd);
    443      1.3   thorpej 	} else if (pci_conf_debug) {
    444      1.3   thorpej 		print_tag(pb->pc, tag);
    445      1.3   thorpej 		printf("device is a bridge; not clearing enables\n");
    446      1.1    briggs 	}
    447      1.1    briggs 
    448      1.1    briggs 	if ((cmd & PCI_STATUS_BACKTOBACK_SUPPORT) == 0)
    449      1.1    briggs 		pb->fast_b2b = 0;
    450      1.1    briggs 
    451      1.1    briggs 	if ((cmd & PCI_STATUS_66MHZ_SUPPORT) == 0)
    452      1.1    briggs 		pb->freq_66 = 0;
    453      1.1    briggs 
    454      1.1    briggs 	if (   (PCI_CLASS(class) == PCI_CLASS_BRIDGE)
    455      1.1    briggs 	    && (PCI_SUBCLASS(class) == PCI_SUBCLASS_BRIDGE_PCI)) {
    456      1.1    briggs 		pd->ppb = query_bus(pb, pd, dev);
    457      1.1    briggs 		if (pd->ppb == NULL)
    458      1.1    briggs 			return -1;
    459      1.1    briggs 		return 0;
    460      1.1    briggs 	}
    461      1.1    briggs 
    462      1.1    briggs 	icr = pci_conf_read(pb->pc, tag, PCI_INTERRUPT_REG);
    463      1.1    briggs 	pd->ipin = PCI_INTERRUPT_PIN(icr);
    464      1.1    briggs 	pd->iline = PCI_INTERRUPT_LINE(icr);
    465      1.1    briggs 	pd->min_gnt = PCI_MIN_GNT(icr);
    466      1.1    briggs 	pd->max_lat = PCI_MAX_LAT(icr);
    467      1.1    briggs 	if (pd->iline || pd->ipin) {
    468  1.5.2.1   thorpej 		pci_conf_interrupt(pb->pc, pb->busno, dev, pd->ipin, pb->swiz,
    469      1.1    briggs 		    &pd->iline);
    470      1.1    briggs 		icr &= ~(PCI_INTERRUPT_LINE_MASK << PCI_INTERRUPT_LINE_SHIFT);
    471      1.1    briggs 		icr |= (pd->iline << PCI_INTERRUPT_LINE_SHIFT);
    472      1.1    briggs 		pci_conf_write(pb->pc, tag, PCI_INTERRUPT_REG, icr);
    473      1.1    briggs 	}
    474      1.1    briggs 
    475      1.1    briggs 	if (pd->min_gnt != 0 || pd->max_lat != 0) {
    476      1.1    briggs 		if (pd->min_gnt != 0 && pd->min_gnt > pb->max_mingnt)
    477      1.1    briggs 			pb->max_mingnt = pd->min_gnt;
    478      1.1    briggs 
    479      1.1    briggs 		if (pd->max_lat != 0 && pd->max_lat < pb->min_maxlat)
    480      1.1    briggs 			pb->min_maxlat = pd->max_lat;
    481      1.1    briggs 
    482      1.1    briggs 		pb->bandwidth_used += pd->min_gnt * 4000000 /
    483      1.1    briggs 				(pd->min_gnt + pd->max_lat);
    484      1.1    briggs 	}
    485      1.1    briggs 
    486      1.1    briggs 	width = 4;
    487      1.1    briggs 	for (br = PCI_MAPREG_START; br < PCI_MAPREG_END; br += width) {
    488      1.3   thorpej #if 0
    489  1.5.2.1   thorpej /* XXX Should only ignore if IDE not in legacy mode? */
    490      1.1    briggs 		if (PCI_CLASS(class) == PCI_CLASS_MASS_STORAGE &&
    491      1.1    briggs 		    PCI_SUBCLASS(class) == PCI_SUBCLASS_MASS_STORAGE_IDE) {
    492      1.1    briggs 			break;
    493      1.1    briggs 		}
    494      1.3   thorpej #endif
    495      1.1    briggs 		bar = pci_conf_read(pb->pc, tag, br);
    496      1.3   thorpej 		pci_conf_write(pb->pc, tag, br, 0xffffffff);
    497      1.1    briggs 		mask = pci_conf_read(pb->pc, tag, br);
    498      1.1    briggs 		pci_conf_write(pb->pc, tag, br, bar);
    499      1.1    briggs 		width = 4;
    500      1.1    briggs 
    501  1.5.2.1   thorpej 		if (   (mode & PCI_CONF_MAP_IO)
    502  1.5.2.1   thorpej 		    && (PCI_MAPREG_TYPE(mask) == PCI_MAPREG_TYPE_IO)) {
    503  1.5.2.1   thorpej 			/*
    504  1.5.2.1   thorpej 			 * Upper 16 bits must be one.  Devices may hardwire
    505  1.5.2.1   thorpej 			 * them to zero, though, per PCI 2.2, 6.2.5.1, p 203.
    506  1.5.2.1   thorpej 			 */
    507      1.3   thorpej 			mask |= 0xffff0000;
    508      1.3   thorpej 
    509      1.3   thorpej 			size = PCI_MAPREG_IO_SIZE(mask);
    510      1.3   thorpej 			if (size == 0) {
    511      1.3   thorpej 				if (pci_conf_debug) {
    512      1.3   thorpej 					print_tag(pb->pc, tag);
    513      1.3   thorpej 					printf("I/O BAR 0x%x is void\n", br);
    514      1.3   thorpej 				}
    515      1.3   thorpej 				continue;
    516      1.3   thorpej 			}
    517      1.1    briggs 
    518      1.1    briggs 			if (pb->niowin >= MAX_CONF_IO) {
    519  1.5.2.1   thorpej 				printf("pciconf: too many I/O windows\n");
    520      1.1    briggs 				return -1;
    521      1.1    briggs 			}
    522      1.1    briggs 
    523      1.1    briggs 			pi = get_io_desc(pb, size);
    524      1.1    briggs 			pi->dev = pd;
    525      1.1    briggs 			pi->reg = br;
    526      1.1    briggs 			pi->size = (u_int64_t) size;
    527      1.1    briggs 			pi->align = 4;
    528      1.1    briggs 			pi->prefetch = 0;
    529      1.1    briggs 			if (pci_conf_debug) {
    530      1.1    briggs 				print_tag(pb->pc, tag);
    531      1.3   thorpej 				printf("Register 0x%x, I/O size %llu\n",
    532      1.1    briggs 				    br, pi->size);
    533      1.1    briggs 			}
    534      1.1    briggs 			pb->niowin++;
    535      1.1    briggs 			pb->io_total += size;
    536      1.4    simonb 		} else if ((mode & PCI_CONF_MAP_MEM)
    537      1.4    simonb 			   && (PCI_MAPREG_TYPE(mask) == PCI_MAPREG_TYPE_MEM)) {
    538      1.1    briggs 			switch (PCI_MAPREG_MEM_TYPE(mask)) {
    539      1.1    briggs 			case PCI_MAPREG_MEM_TYPE_32BIT:
    540      1.1    briggs 			case PCI_MAPREG_MEM_TYPE_32BIT_1M:
    541      1.1    briggs 				size = (u_int64_t) PCI_MAPREG_MEM_SIZE(mask);
    542      1.1    briggs 				break;
    543      1.1    briggs 			case PCI_MAPREG_MEM_TYPE_64BIT:
    544      1.1    briggs 				bar64 = pci_conf_read(pb->pc, tag, br + 4);
    545      1.1    briggs 				pci_conf_write(pb->pc, tag, br + 4, 0xffffffff);
    546      1.1    briggs 				mask64 = pci_conf_read(pb->pc, tag, br + 4);
    547      1.1    briggs 				pci_conf_write(pb->pc, tag, br + 4, bar64);
    548      1.1    briggs 				size = (u_int64_t) PCI_MAPREG_MEM64_SIZE(
    549      1.1    briggs 				      (((u_int64_t) mask64) << 32) | mask);
    550      1.1    briggs 				width = 8;
    551  1.5.2.4  jdolecek 				break;
    552      1.1    briggs 			default:
    553      1.1    briggs 				print_tag(pb->pc, tag);
    554      1.1    briggs 				printf("reserved mapping type 0x%x\n",
    555      1.1    briggs 					PCI_MAPREG_MEM_TYPE(mask));
    556      1.1    briggs 				continue;
    557      1.1    briggs 			}
    558      1.1    briggs 
    559      1.3   thorpej 			if (size == 0) {
    560      1.3   thorpej 				if (pci_conf_debug) {
    561      1.3   thorpej 					print_tag(pb->pc, tag);
    562      1.3   thorpej 					printf("MEM%d BAR 0x%x is void\n",
    563      1.3   thorpej 					    PCI_MAPREG_MEM_TYPE(mask) ==
    564      1.3   thorpej 						PCI_MAPREG_MEM_TYPE_64BIT ?
    565      1.3   thorpej 						64 : 32, br);
    566      1.3   thorpej 				}
    567      1.3   thorpej 				continue;
    568  1.5.2.4  jdolecek 			} else {
    569  1.5.2.4  jdolecek 				if (pci_conf_debug) {
    570  1.5.2.4  jdolecek 					print_tag(pb->pc, tag);
    571  1.5.2.4  jdolecek 					printf("MEM%d BAR 0x%x has size %lx\n",
    572  1.5.2.4  jdolecek 					    PCI_MAPREG_MEM_TYPE(mask) ==
    573  1.5.2.4  jdolecek 						PCI_MAPREG_MEM_TYPE_64BIT ?
    574  1.5.2.4  jdolecek 						64 : 32, br, (unsigned long)size);
    575  1.5.2.4  jdolecek 				}
    576      1.3   thorpej 			}
    577      1.3   thorpej 
    578      1.1    briggs 			if (pb->nmemwin >= MAX_CONF_MEM) {
    579  1.5.2.1   thorpej 				printf("pciconf: too many memory windows\n");
    580      1.1    briggs 				return -1;
    581      1.1    briggs 			}
    582      1.1    briggs 
    583      1.1    briggs 			pm = get_mem_desc(pb, size);
    584      1.1    briggs 			pm->dev = pd;
    585      1.1    briggs 			pm->reg = br;
    586      1.1    briggs 			pm->size = size;
    587      1.1    briggs 			pm->align = 4;
    588      1.1    briggs 			pm->prefetch = PCI_MAPREG_MEM_PREFETCHABLE(mask);
    589      1.1    briggs 			if (pci_conf_debug) {
    590      1.1    briggs 				print_tag(pb->pc, tag);
    591      1.3   thorpej 				printf("Register 0x%x, memory size %llu\n",
    592      1.1    briggs 				    br, pm->size);
    593      1.1    briggs 			}
    594      1.1    briggs 			pb->nmemwin++;
    595      1.1    briggs 			if (pm->prefetch) {
    596      1.1    briggs 				pb->pmem_total += size;
    597      1.1    briggs 			} else {
    598      1.1    briggs 				pb->mem_total += size;
    599      1.1    briggs 			}
    600      1.1    briggs 		}
    601      1.1    briggs 	}
    602      1.1    briggs 
    603      1.4    simonb 	if (mode & PCI_CONF_MAP_ROM) {
    604      1.4    simonb 		bar = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
    605      1.4    simonb 		pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM, 0xfffffffe);
    606      1.4    simonb 		mask = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
    607      1.4    simonb 		pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM, bar);
    608      1.4    simonb 
    609      1.4    simonb 		if (mask != 0 && mask != 0xffffffff) {
    610      1.4    simonb 			if (pb->nmemwin >= MAX_CONF_MEM) {
    611  1.5.2.1   thorpej 				printf("pciconf: too many memory windows\n");
    612      1.4    simonb 				return -1;
    613      1.4    simonb 			}
    614      1.4    simonb 			size = (u_int64_t) PCI_MAPREG_MEM_SIZE(mask);
    615      1.1    briggs 
    616      1.4    simonb 			pm = get_mem_desc(pb, size);
    617      1.4    simonb 			pm->dev = pd;
    618      1.4    simonb 			pm->reg = PCI_MAPREG_ROM;
    619      1.4    simonb 			pm->size = size;
    620      1.4    simonb 			pm->align = 4;
    621      1.4    simonb 			pm->prefetch = 1;
    622      1.4    simonb 			if (pci_conf_debug) {
    623      1.4    simonb 				print_tag(pb->pc, tag);
    624      1.4    simonb 				printf("Expansion ROM memory size %llu\n", pm->size);
    625      1.4    simonb 			}
    626      1.4    simonb 			pb->nmemwin++;
    627      1.4    simonb 			pb->pmem_total += size;
    628      1.1    briggs 		}
    629  1.5.2.1   thorpej 	} else {
    630  1.5.2.1   thorpej 		/* Ensure ROM is disabled */
    631  1.5.2.1   thorpej 		bar = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
    632  1.5.2.1   thorpej 		pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM, 0xfffffffe);
    633  1.5.2.1   thorpej 		mask = pci_conf_read(pb->pc, tag, PCI_MAPREG_ROM);
    634  1.5.2.1   thorpej 		pci_conf_write(pb->pc, tag, PCI_MAPREG_ROM,
    635  1.5.2.1   thorpej 		    bar & ~PCI_MAPREG_ROM_ENABLE);
    636      1.1    briggs 	}
    637      1.1    briggs 
    638      1.1    briggs 	return 0;
    639      1.1    briggs }
    640      1.1    briggs 
    641      1.1    briggs /************************************************************************/
    642      1.1    briggs /************************************************************************/
    643      1.1    briggs /********************   Bus configuration routines   ********************/
    644      1.1    briggs /************************************************************************/
    645      1.1    briggs /************************************************************************/
    646      1.1    briggs static u_int64_t
    647      1.1    briggs pci_allocate_range(struct extent *ex, u_int64_t amt, int align)
    648      1.1    briggs {
    649      1.1    briggs 	int	r;
    650      1.1    briggs 	u_long	addr;
    651      1.1    briggs 
    652      1.1    briggs 	r = extent_alloc(ex, amt, align, 0, EX_NOWAIT, &addr);
    653      1.1    briggs 	if (r) {
    654      1.1    briggs 		addr = (u_long) -1;
    655      1.4    simonb 		printf("extent_alloc(%p, %llu, %d) returned %d\n",
    656      1.4    simonb 		    ex, amt, align, r);
    657      1.4    simonb 		extent_print(ex);
    658      1.1    briggs 	}
    659      1.1    briggs 	return (pcireg_t) addr;
    660      1.1    briggs }
    661      1.1    briggs 
    662      1.1    briggs static int
    663      1.1    briggs setup_iowins(pciconf_bus_t *pb)
    664      1.1    briggs {
    665      1.1    briggs 	pciconf_win_t	*pi;
    666      1.1    briggs 	pciconf_dev_t	*pd;
    667      1.1    briggs 
    668      1.1    briggs 	for (pi=pb->pciiowin; pi < &pb->pciiowin[pb->niowin] ; pi++) {
    669      1.1    briggs 		if (pi->size == 0)
    670      1.1    briggs 			continue;
    671      1.1    briggs 
    672      1.1    briggs 		pd = pi->dev;
    673      1.1    briggs 		pi->address = pci_allocate_range(pb->ioext, pi->size,
    674      1.1    briggs 		    pi->align);
    675      1.1    briggs 		if (pi->address == -1) {
    676      1.1    briggs 			print_tag(pd->pc, pd->tag);
    677      1.1    briggs 			printf("Failed to allocate PCI I/O space (%llu req)\n",
    678      1.1    briggs 			   pi->size);
    679      1.1    briggs 			return -1;
    680      1.1    briggs 		}
    681      1.2    briggs 		if (!pb->io_32bit && pi->address > 0xFFFF) {
    682      1.2    briggs 			pi->address = 0;
    683      1.2    briggs 			pd->enable = 0;
    684      1.2    briggs 		}
    685      1.1    briggs 		if (pd->ppb && pi->reg == 0) {
    686      1.1    briggs 			pd->ppb->ioext = extent_create("pciconf", pi->address,
    687      1.1    briggs 			    pi->address + pi->size, M_DEVBUF, NULL, 0,
    688      1.1    briggs 			    EX_NOWAIT);
    689      1.1    briggs 			if (pd->ppb->ioext == NULL) {
    690      1.1    briggs 				print_tag(pd->pc, pd->tag);
    691      1.1    briggs 				printf("Failed to alloc I/O ext. for bus %d\n",
    692      1.1    briggs 				    pd->ppb->busno);
    693      1.1    briggs 				return -1;
    694      1.1    briggs 			}
    695      1.1    briggs 			continue;
    696      1.1    briggs 		}
    697  1.5.2.1   thorpej 		pd->enable |= PCI_CONF_ENABLE_IO;
    698      1.1    briggs 		if (pci_conf_debug) {
    699      1.1    briggs 			print_tag(pd->pc, pd->tag);
    700      1.1    briggs 			printf("Putting %llu I/O bytes @ %#llx (reg %x)\n",
    701      1.1    briggs 			    pi->size, pi->address, pi->reg);
    702      1.1    briggs 		}
    703      1.1    briggs 		pci_conf_write(pd->pc, pd->tag, pi->reg,
    704      1.1    briggs 		    PCI_MAPREG_IO_ADDR(pi->address) | PCI_MAPREG_TYPE_IO);
    705      1.1    briggs 	}
    706      1.1    briggs 	return 0;
    707      1.1    briggs }
    708      1.1    briggs 
    709      1.1    briggs static int
    710      1.1    briggs setup_memwins(pciconf_bus_t *pb)
    711      1.1    briggs {
    712      1.1    briggs 	pciconf_win_t	*pm;
    713      1.1    briggs 	pciconf_dev_t	*pd;
    714      1.1    briggs 	pcireg_t	base;
    715      1.1    briggs 	struct extent	*ex;
    716      1.1    briggs 
    717      1.1    briggs 	for (pm=pb->pcimemwin; pm < &pb->pcimemwin[pb->nmemwin] ; pm++) {
    718      1.1    briggs 		if (pm->size == 0)
    719      1.1    briggs 			continue;
    720      1.1    briggs 
    721      1.1    briggs 		pd = pm->dev;
    722      1.1    briggs 		ex = (pm->prefetch) ? pb->pmemext : pb->memext;
    723      1.1    briggs 		pm->address = pci_allocate_range(ex, pm->size, pm->align);
    724      1.1    briggs 		if (pm->address == -1) {
    725      1.1    briggs 			print_tag(pd->pc, pd->tag);
    726      1.1    briggs 			printf(
    727      1.1    briggs 			   "Failed to allocate PCI memory space (%llu req)\n",
    728      1.1    briggs 			   pm->size);
    729      1.1    briggs 			return -1;
    730      1.1    briggs 		}
    731      1.1    briggs 		if (pd->ppb && pm->reg == 0) {
    732      1.1    briggs 			ex = extent_create("pciconf", pm->address,
    733      1.1    briggs 			    pm->address + pm->size, M_DEVBUF, NULL, 0,
    734      1.1    briggs 			    EX_NOWAIT);
    735      1.1    briggs 			if (ex == NULL) {
    736      1.1    briggs 				print_tag(pd->pc, pd->tag);
    737      1.1    briggs 				printf("Failed to alloc MEM ext. for bus %d\n",
    738      1.1    briggs 				    pd->ppb->busno);
    739      1.1    briggs 				return -1;
    740      1.1    briggs 			}
    741      1.1    briggs 			if (pm->prefetch) {
    742      1.1    briggs 				pd->ppb->pmemext = ex;
    743      1.1    briggs 			} else {
    744      1.1    briggs 				pd->ppb->memext = ex;
    745      1.1    briggs 			}
    746      1.1    briggs 			continue;
    747      1.1    briggs 		}
    748      1.2    briggs 		if (pm->prefetch && !pb->pmem_64bit &&
    749      1.2    briggs 		    pm->address > 0xFFFFFFFFULL) {
    750      1.2    briggs 			pm->address = 0;
    751      1.2    briggs 			pd->enable = 0;
    752  1.5.2.1   thorpej 		} else {
    753  1.5.2.1   thorpej 			pd->enable |= PCI_CONF_ENABLE_MEM;
    754      1.2    briggs 		}
    755      1.1    briggs 		if (pm->reg != PCI_MAPREG_ROM) {
    756      1.1    briggs 			if (pci_conf_debug) {
    757      1.1    briggs 				print_tag(pd->pc, pd->tag);
    758      1.1    briggs 				printf(
    759      1.1    briggs 				    "Putting %llu MEM bytes @ %#llx (reg %x)\n",
    760      1.1    briggs 				     pm->size, pm->address, pm->reg);
    761      1.1    briggs 			}
    762      1.1    briggs 			base = pci_conf_read(pd->pc, pd->tag, pm->reg);
    763      1.1    briggs 			base = PCI_MAPREG_MEM_ADDR(pm->address) |
    764      1.1    briggs 			    PCI_MAPREG_MEM_TYPE(base);
    765      1.1    briggs 			pci_conf_write(pd->pc, pd->tag, pm->reg, base);
    766      1.1    briggs 			if (PCI_MAPREG_MEM_TYPE(base) ==
    767      1.1    briggs 			    PCI_MAPREG_MEM_TYPE_64BIT) {
    768      1.1    briggs 				base = (pcireg_t)
    769      1.1    briggs 				    (PCI_MAPREG_MEM64_ADDR(pm->address) >> 32);
    770      1.1    briggs 				pci_conf_write(pd->pc, pd->tag, pm->reg + 4,
    771      1.1    briggs 				    base);
    772      1.1    briggs 			}
    773      1.1    briggs 		}
    774      1.1    briggs 	}
    775      1.1    briggs 	for (pm=pb->pcimemwin; pm < &pb->pcimemwin[pb->nmemwin] ; pm++) {
    776      1.1    briggs 		if (pm->reg == PCI_MAPREG_ROM && pm->address != -1) {
    777      1.1    briggs 			pd = pm->dev;
    778      1.1    briggs 			if (pci_conf_debug) {
    779      1.1    briggs 				print_tag(pd->pc, pd->tag);
    780      1.1    briggs 				printf(
    781      1.1    briggs 				    "Putting %llu ROM bytes @ %#llx (reg %x)\n",
    782      1.1    briggs 				    pm->size, pm->address, pm->reg);
    783      1.1    briggs 			}
    784  1.5.2.1   thorpej 			base = (pcireg_t) (pm->address | PCI_MAPREG_ROM_ENABLE);
    785      1.1    briggs 			pci_conf_write(pd->pc, pd->tag, pm->reg, base);
    786      1.1    briggs 		}
    787      1.1    briggs 	}
    788      1.1    briggs 	return 0;
    789      1.1    briggs }
    790      1.1    briggs 
    791      1.1    briggs /*
    792      1.1    briggs  * Configure I/O, memory, and prefetcable memory spaces, then make
    793      1.1    briggs  * a call to configure_bus().
    794      1.1    briggs  */
    795      1.1    briggs static int
    796      1.1    briggs configure_bridge(pciconf_dev_t *pd)
    797      1.1    briggs {
    798      1.1    briggs 	unsigned long	io_base, io_limit, mem_base, mem_limit;
    799      1.1    briggs 	pciconf_bus_t	*pb;
    800      1.1    briggs 	pcireg_t	io, iohigh, mem, cmd;
    801      1.1    briggs 	int		rv;
    802      1.1    briggs 
    803      1.1    briggs 	pb = pd->ppb;
    804      1.1    briggs 	/* Configure I/O base & limit*/
    805      1.1    briggs 	if (pb->ioext) {
    806      1.1    briggs 		io_base = pb->ioext->ex_start;
    807      1.1    briggs 		io_limit = pb->ioext->ex_end;
    808      1.2    briggs 	} else {
    809      1.2    briggs 		io_base  = 0x1000;	/* 4K */
    810      1.2    briggs 		io_limit = 0x0000;
    811      1.1    briggs 	}
    812      1.2    briggs 	if (pb->io_32bit) {
    813      1.2    briggs 		iohigh =
    814      1.2    briggs 		    ((io_base >> 16) << PCI_BRIDGE_IOHIGH_BASE_SHIFT) |
    815      1.2    briggs 		    ((io_limit >> 16) << PCI_BRIDGE_IOHIGH_LIMIT_SHIFT);
    816      1.2    briggs 	} else {
    817      1.2    briggs 		if (io_limit > 0xFFFF) {
    818      1.2    briggs 			printf("Bus %d bridge does not support 32-bit I/O.  ",
    819      1.2    briggs 			    pb->busno);
    820      1.2    briggs 			printf("Disabling I/O accesses\n");
    821      1.2    briggs 			io_base  = 0x1000;	/* 4K */
    822      1.2    briggs 			io_limit = 0x0000;
    823      1.2    briggs 		}
    824      1.2    briggs 		iohigh = 0;
    825      1.2    briggs 	}
    826  1.5.2.1   thorpej 	io = pci_conf_read(pb->pc, pd->tag, PCI_BRIDGE_STATIO_REG) &
    827  1.5.2.1   thorpej 	    (PCI_BRIDGE_STATIO_STATUS_MASK << PCI_BRIDGE_STATIO_STATUS_SHIFT);
    828      1.2    briggs 	io |= (((io_base >> 8) & PCI_BRIDGE_STATIO_IOBASE_MASK)
    829      1.2    briggs 	    << PCI_BRIDGE_STATIO_IOBASE_SHIFT);
    830      1.2    briggs 	io |= (((io_limit >> 8) & PCI_BRIDGE_STATIO_IOLIMIT_MASK)
    831      1.2    briggs 	    << PCI_BRIDGE_STATIO_IOLIMIT_SHIFT);
    832      1.2    briggs 	pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_STATIO_REG, io);
    833      1.2    briggs 	pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_IOHIGH_REG, iohigh);
    834      1.1    briggs 
    835      1.1    briggs 	/* Configure mem base & limit */
    836      1.1    briggs 	if (pb->memext) {
    837      1.1    briggs 		mem_base = pb->memext->ex_start;
    838      1.1    briggs 		mem_limit = pb->memext->ex_end;
    839      1.2    briggs 	} else {
    840      1.2    briggs 		mem_base  = 0x100000;	/* 1M */
    841      1.2    briggs 		mem_limit = 0x000000;
    842      1.1    briggs 	}
    843      1.2    briggs 	if (mem_limit > 0xFFFFFFFFULL) {
    844      1.2    briggs 		printf("Bus %d bridge MEM range out of range.  ", pb->busno);
    845      1.2    briggs 		printf("Disabling MEM accesses\n");
    846      1.2    briggs 		mem_base  = 0x100000;	/* 1M */
    847      1.2    briggs 		mem_limit = 0x000000;
    848      1.2    briggs 	}
    849      1.2    briggs 	mem = (((mem_base >> 20) & PCI_BRIDGE_MEMORY_BASE_MASK)
    850      1.2    briggs 	    << PCI_BRIDGE_MEMORY_BASE_SHIFT);
    851      1.2    briggs 	mem |= (((mem_limit >> 20) & PCI_BRIDGE_MEMORY_LIMIT_MASK)
    852      1.2    briggs 	    << PCI_BRIDGE_MEMORY_LIMIT_SHIFT);
    853      1.2    briggs 	pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_MEMORY_REG, mem);
    854      1.1    briggs 
    855      1.1    briggs 	/* Configure prefetchable mem base & limit */
    856      1.1    briggs 	if (pb->pmemext) {
    857      1.1    briggs 		mem_base = pb->pmemext->ex_start;
    858      1.1    briggs 		mem_limit = pb->pmemext->ex_end;
    859      1.2    briggs 	} else {
    860      1.2    briggs 		mem_base  = 0x100000;	/* 1M */
    861      1.2    briggs 		mem_limit = 0x000000;
    862      1.1    briggs 	}
    863      1.2    briggs 	mem = pci_conf_read(pb->pc, pd->tag, PCI_BRIDGE_PREFETCHMEM_REG);
    864      1.2    briggs 	if (!PCI_BRIDGE_PREFETCHMEM_64BITS(mem) && mem_limit > 0xFFFFFFFFULL) {
    865      1.2    briggs 		printf("Bus %d bridge does not support 64-bit PMEM.  ",
    866      1.2    briggs 		    pb->busno);
    867      1.2    briggs 		printf("Disabling prefetchable-MEM accesses\n");
    868      1.2    briggs 		mem_base  = 0x100000;	/* 1M */
    869      1.2    briggs 		mem_limit = 0x000000;
    870      1.2    briggs 	}
    871      1.2    briggs 	mem = (((mem_base >> 20) & PCI_BRIDGE_PREFETCHMEM_BASE_MASK)
    872      1.2    briggs 	    << PCI_BRIDGE_PREFETCHMEM_BASE_SHIFT);
    873      1.2    briggs 	mem |= (((mem_limit >> 20) & PCI_BRIDGE_PREFETCHMEM_LIMIT_MASK)
    874      1.2    briggs 	    << PCI_BRIDGE_PREFETCHMEM_LIMIT_SHIFT);
    875      1.2    briggs 	pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_PREFETCHMEM_REG, mem);
    876      1.2    briggs 	/*
    877      1.2    briggs 	 * XXX -- 64-bit systems need a lot more than just this...
    878      1.2    briggs 	 */
    879      1.2    briggs 	if (sizeof(u_long) > 4) {
    880      1.2    briggs 		mem_base  = (int64_t) mem_base  >> 32;
    881      1.2    briggs 		mem_limit = (int64_t) mem_limit >> 32;
    882      1.2    briggs 	}
    883      1.2    briggs 	pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_PREFETCHBASE32_REG,
    884      1.2    briggs 	    mem_base & 0xffffffff);
    885      1.2    briggs 	pci_conf_write(pb->pc, pd->tag, PCI_BRIDGE_PREFETCHLIMIT32_REG,
    886      1.2    briggs 	    mem_limit & 0xffffffff);
    887      1.1    briggs 
    888      1.1    briggs 	rv = configure_bus(pb);
    889      1.1    briggs 
    890      1.1    briggs 	if (pb->ioext)
    891      1.1    briggs 		extent_destroy(pb->ioext);
    892      1.1    briggs 	if (pb->memext)
    893      1.1    briggs 		extent_destroy(pb->memext);
    894      1.1    briggs 	if (pb->pmemext)
    895      1.1    briggs 		extent_destroy(pb->pmemext);
    896      1.1    briggs 	if (rv == 0) {
    897      1.1    briggs 		cmd = pci_conf_read(pd->pc, pd->tag, PCI_BRIDGE_CONTROL_REG);
    898      1.1    briggs 		cmd &= PCI_BRIDGE_CONTROL_MASK;
    899      1.1    briggs 		cmd |= (PCI_BRIDGE_CONTROL_PERE | PCI_BRIDGE_CONTROL_SERR)
    900      1.1    briggs 		    << PCI_BRIDGE_CONTROL_SHIFT;
    901      1.1    briggs 		if (pb->fast_b2b) {
    902      1.1    briggs 			cmd |= PCI_BRIDGE_CONTROL_SECFASTB2B
    903      1.1    briggs 			    << PCI_BRIDGE_CONTROL_SHIFT;
    904      1.1    briggs 		}
    905      1.1    briggs 		pci_conf_write(pd->pc, pd->tag, PCI_BRIDGE_CONTROL_REG, cmd);
    906      1.1    briggs 		cmd = pci_conf_read(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG);
    907      1.1    briggs 		cmd |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE;
    908      1.1    briggs 		pci_conf_write(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG, cmd);
    909      1.1    briggs 	}
    910      1.1    briggs 
    911      1.1    briggs 	return rv;
    912      1.1    briggs }
    913      1.1    briggs 
    914      1.1    briggs /*
    915      1.1    briggs  * Calculate latency values, allocate I/O and MEM segments, then set them
    916      1.1    briggs  * up.  If a PCI-PCI bridge is found, configure the bridge separately,
    917      1.1    briggs  * which will cause a recursive call back here.
    918      1.1    briggs  */
    919      1.1    briggs static int
    920      1.1    briggs configure_bus(pciconf_bus_t *pb)
    921      1.1    briggs {
    922      1.1    briggs 	pciconf_dev_t	*pd;
    923  1.5.2.1   thorpej 	int		def_ltim, max_ltim, band, bus_mhz;
    924      1.1    briggs 
    925  1.5.2.1   thorpej 	bus_mhz = pb->freq_66 ? 66 : 33;
    926  1.5.2.1   thorpej 	max_ltim = pb->max_mingnt * bus_mhz / 4;	/* cvt to cycle count */
    927      1.1    briggs 	band = 40000000;			/* 0.25us cycles/sec */
    928      1.1    briggs 	if (band < pb->bandwidth_used) {
    929      1.1    briggs 		printf("PCI bus %d: Warning: Total bandwidth exceeded!?\n",
    930      1.1    briggs 		    pb->busno);
    931      1.1    briggs 		def_ltim = -1;
    932      1.1    briggs 	} else {
    933      1.1    briggs 		def_ltim = (band - pb->bandwidth_used) / pb->ndevs;
    934      1.1    briggs 		if (def_ltim > pb->min_maxlat)
    935      1.1    briggs 			def_ltim = pb->min_maxlat;
    936  1.5.2.1   thorpej 		def_ltim = def_ltim * bus_mhz / 4;
    937      1.1    briggs 	}
    938      1.1    briggs 	def_ltim = (def_ltim + 7) & ~7;
    939      1.1    briggs 	max_ltim = (max_ltim + 7) & ~7;
    940      1.1    briggs 
    941      1.1    briggs 	pb->def_ltim = MIN( def_ltim, 255 );
    942      1.1    briggs 	pb->max_ltim = MIN( MAX(max_ltim, def_ltim ), 255 );
    943      1.1    briggs 
    944      1.1    briggs 	/*
    945      1.1    briggs 	 * Now we have what we need to initialize the devices.
    946      1.1    briggs 	 * It would probably be better if we could allocate all of these
    947      1.1    briggs 	 * for all busses at once, but "not right now".  First, get a list
    948      1.1    briggs 	 * of free memory ranges from the m.d. system.
    949      1.1    briggs 	 */
    950      1.1    briggs 	if (setup_iowins(pb) || setup_memwins(pb)) {
    951      1.1    briggs 		printf("PCI bus configuration failed: ");
    952      1.1    briggs 		printf("unable to assign all I/O and memory ranges.");
    953      1.1    briggs 		return -1;
    954      1.1    briggs 	}
    955      1.1    briggs 
    956      1.1    briggs 	/*
    957      1.1    briggs 	 * Configure the latency for the devices, and enable them.
    958      1.1    briggs 	 */
    959      1.1    briggs 	for (pd=pb->device ; pd < &pb->device[pb->ndevs] ; pd++) {
    960      1.1    briggs 		pcireg_t cmd, class, misc;
    961      1.1    briggs 		int	ltim;
    962      1.1    briggs 
    963      1.1    briggs 		if (pci_conf_debug) {
    964      1.1    briggs 			print_tag(pd->pc, pd->tag);
    965      1.1    briggs 			printf("Configuring device.\n");
    966      1.1    briggs 		}
    967      1.1    briggs 		class = pci_conf_read(pd->pc, pd->tag, PCI_CLASS_REG);
    968      1.1    briggs 		misc = pci_conf_read(pd->pc, pd->tag, PCI_BHLC_REG);
    969      1.1    briggs 		cmd = pci_conf_read(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG);
    970  1.5.2.1   thorpej 		cmd |= PCI_COMMAND_SERR_ENABLE | PCI_COMMAND_PARITY_ENABLE;
    971      1.1    briggs 		if (pb->fast_b2b)
    972      1.1    briggs 			cmd |= PCI_COMMAND_BACKTOBACK_ENABLE;
    973      1.1    briggs 		if (PCI_CLASS(class) != PCI_CLASS_BRIDGE ||
    974      1.1    briggs 		    PCI_SUBCLASS(class) != PCI_SUBCLASS_BRIDGE_PCI) {
    975  1.5.2.1   thorpej 			if (pd->enable & PCI_CONF_ENABLE_IO)
    976  1.5.2.1   thorpej 				cmd |= PCI_COMMAND_IO_ENABLE;
    977  1.5.2.1   thorpej 			if (pd->enable & PCI_CONF_ENABLE_MEM)
    978  1.5.2.1   thorpej 				cmd |= PCI_COMMAND_MEM_ENABLE;
    979  1.5.2.1   thorpej 			if (pd->enable & PCI_CONF_ENABLE_BM)
    980  1.5.2.1   thorpej 				cmd |= PCI_COMMAND_MASTER_ENABLE;
    981  1.5.2.1   thorpej 			ltim = pd->min_gnt * bus_mhz / 4;
    982      1.1    briggs 			ltim = MIN (MAX (pb->def_ltim, ltim), pb->max_ltim);
    983      1.1    briggs 		} else {
    984  1.5.2.1   thorpej 			cmd |= PCI_COMMAND_MASTER_ENABLE;
    985      1.1    briggs 			ltim = MIN (pb->def_ltim, pb->max_ltim);
    986      1.1    briggs 		}
    987  1.5.2.1   thorpej 		if (!(pd->enable)) {
    988      1.2    briggs 			print_tag(pd->pc, pd->tag);
    989      1.2    briggs 			printf("Disabled due to lack of resources.\n");
    990      1.2    briggs 			cmd &= ~(PCI_COMMAND_MASTER_ENABLE |
    991      1.2    briggs 			    PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE);
    992      1.2    briggs 		}
    993      1.1    briggs 		pci_conf_write(pd->pc, pd->tag, PCI_COMMAND_STATUS_REG, cmd);
    994      1.1    briggs 
    995  1.5.2.2   thorpej 		misc &= ~((PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT) |
    996  1.5.2.2   thorpej 		    (PCI_CACHELINE_MASK << PCI_CACHELINE_SHIFT));
    997  1.5.2.2   thorpej 		misc |= (ltim & PCI_LATTIMER_MASK) << PCI_LATTIMER_SHIFT;
    998  1.5.2.3  jdolecek 		misc |= ((pb->cacheline_size >> 2) & PCI_CACHELINE_MASK) <<
    999  1.5.2.2   thorpej 		    PCI_CACHELINE_SHIFT;
   1000      1.1    briggs 		pci_conf_write(pd->pc, pd->tag, PCI_BHLC_REG, misc);
   1001      1.1    briggs 
   1002      1.1    briggs 		if (pd->ppb) {
   1003      1.1    briggs 			if (configure_bridge(pd) < 0)
   1004      1.1    briggs 				return -1;
   1005      1.1    briggs 			continue;
   1006      1.1    briggs 		}
   1007      1.1    briggs 	}
   1008      1.1    briggs 
   1009      1.1    briggs 	if (pci_conf_debug) {
   1010      1.1    briggs 		printf("PCI bus %d configured\n", pb->busno);
   1011      1.1    briggs 	}
   1012      1.1    briggs 
   1013      1.1    briggs 	return 0;
   1014      1.1    briggs }
   1015      1.1    briggs 
   1016      1.1    briggs /*
   1017      1.1    briggs  * Let's configure the PCI bus.
   1018      1.1    briggs  * This consists of basically scanning for all existing devices,
   1019      1.1    briggs  * identifying their needs, and then making another pass over them
   1020      1.1    briggs  * to set:
   1021      1.1    briggs  *	1. I/O addresses
   1022      1.1    briggs  *	2. Memory addresses (Prefetchable and not)
   1023      1.1    briggs  *	3. PCI command register
   1024      1.1    briggs  *	4. The latency part of the PCI BHLC (BIST (Built-In Self Test),
   1025      1.1    briggs  *	    Header type, Latency timer, Cache line size) register
   1026      1.1    briggs  *
   1027      1.1    briggs  * The command register is set to enable fast back-to-back transactions
   1028      1.1    briggs  * if the host bridge says it can handle it.  We also configure
   1029      1.1    briggs  * Master Enable, SERR enable, parity enable, and (if this is not a
   1030      1.1    briggs  * PCI-PCI bridge) the I/O and Memory spaces.  Apparently some devices
   1031      1.1    briggs  * will not report some I/O space.
   1032      1.1    briggs  *
   1033      1.1    briggs  * The latency is computed to be a "fair share" of the bus bandwidth.
   1034      1.1    briggs  * The bus bandwidth variable is initialized to the number of PCI cycles
   1035      1.1    briggs  * in one second.  The number of cycles taken for one transaction by each
   1036      1.1    briggs  * device (MAX_LAT + MIN_GNT) is then subtracted from the bandwidth.
   1037      1.1    briggs  * Care is taken to ensure that the latency timer won't be set such that
   1038      1.1    briggs  * it would exceed the critical time for any device.
   1039      1.1    briggs  *
   1040      1.1    briggs  * This is complicated somewhat due to the presence of bridges.  PCI-PCI
   1041      1.1    briggs  * bridges are probed and configured recursively.
   1042      1.1    briggs  */
   1043      1.1    briggs int
   1044      1.1    briggs pci_configure_bus(pci_chipset_tag_t pc, struct extent *ioext,
   1045  1.5.2.2   thorpej     struct extent *memext, struct extent *pmemext, int firstbus,
   1046  1.5.2.2   thorpej     int cacheline_size)
   1047      1.1    briggs {
   1048      1.1    briggs 	pciconf_bus_t	*pb;
   1049      1.1    briggs 	int		rv;
   1050      1.1    briggs 
   1051      1.1    briggs 	pb = malloc (sizeof (pciconf_bus_t), M_DEVBUF, M_NOWAIT);
   1052  1.5.2.2   thorpej 	pb->busno = firstbus;
   1053      1.1    briggs 	pb->next_busno = pb->busno + 1;
   1054      1.1    briggs 	pb->last_busno = 255;
   1055  1.5.2.2   thorpej 	pb->cacheline_size = cacheline_size;
   1056      1.1    briggs 	pb->parent_bus = NULL;
   1057      1.1    briggs 	pb->swiz = 0;
   1058      1.2    briggs 	pb->io_32bit = 1;
   1059      1.2    briggs 	pb->pmem_64bit = 0;
   1060      1.1    briggs 	pb->ioext = ioext;
   1061      1.1    briggs 	pb->memext = memext;
   1062      1.1    briggs 	if (pmemext == NULL) {
   1063      1.1    briggs 		pb->pmemext = memext;
   1064      1.1    briggs 	} else {
   1065      1.1    briggs 		pb->pmemext = pmemext;
   1066      1.1    briggs 	}
   1067      1.1    briggs 	pb->pc = pc;
   1068      1.1    briggs 	pb->io_total = pb->mem_total = pb->pmem_total = 0;
   1069      1.1    briggs 
   1070      1.1    briggs 	rv = probe_bus(pb);
   1071  1.5.2.4  jdolecek 	pb->last_busno = pb->next_busno-1;
   1072      1.1    briggs 	if (rv == 0) {
   1073      1.1    briggs 		rv = configure_bus(pb);
   1074      1.1    briggs 	}
   1075      1.1    briggs 
   1076      1.1    briggs 	/*
   1077      1.1    briggs 	 * All done!
   1078      1.1    briggs 	 */
   1079      1.1    briggs 	free(pb, M_DEVBUF);
   1080      1.1    briggs 	return rv;
   1081      1.1    briggs }
   1082