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