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