1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/types.h> 29 #include <sys/stat.h> 30 #include <sys/sunndi.h> 31 #include <sys/pci.h> 32 #include <sys/pci_impl.h> 33 #include <sys/pci_cfgspace.h> 34 #include <sys/memlist.h> 35 #include <sys/bootconf.h> 36 #include <io/pci/mps_table.h> 37 #include <sys/pci_cfgspace.h> 38 #include <sys/pci_cfgspace_impl.h> 39 #include <sys/psw.h> 40 #include "../../../../common/pci/pci_strings.h" 41 #include <io/pciex/pcie_ck804_boot.h> 42 #include <sys/acpi/acpi.h> 43 #include <sys/acpica.h> 44 45 #define pci_getb (*pci_getb_func) 46 #define pci_getw (*pci_getw_func) 47 #define pci_getl (*pci_getl_func) 48 #define pci_putb (*pci_putb_func) 49 #define pci_putw (*pci_putw_func) 50 #define pci_putl (*pci_putl_func) 51 #define dcmn_err if (pci_boot_debug) cmn_err 52 53 #define CONFIG_INFO 0 54 #define CONFIG_UPDATE 1 55 #define CONFIG_NEW 2 56 #define CONFIG_FIX 3 57 #define COMPAT_BUFSIZE 512 58 59 /* See AMD-8111 Datasheet Rev 3.03, Page 149: */ 60 #define LPC_IO_CONTROL_REG_1 0x40 61 #define AMD8111_ENABLENMI (uint8_t)0x80 62 #define VENID_AMD 0x1022 63 #define DEVID_AMD8111_LPC 0x7468 64 65 struct pci_fixundo { 66 uint8_t bus; 67 uint8_t dev; 68 uint8_t fn; 69 void (*undofn)(uint8_t, uint8_t, uint8_t); 70 struct pci_fixundo *next; 71 }; 72 73 extern int pci_bios_nbus; 74 static uchar_t max_dev_pci = 32; /* PCI standard */ 75 int pci_boot_debug = 0; 76 extern struct memlist *find_bus_res(int, int); 77 static struct pci_fixundo *undolist = NULL; 78 79 /* 80 * Module prototypes 81 */ 82 static void enumerate_bus_devs(uchar_t bus, int config_op); 83 static void create_root_bus_dip(uchar_t bus); 84 static dev_info_t *process_devfunc(uchar_t, uchar_t, uchar_t, uchar_t, 85 ushort_t, int); 86 static void add_compatible(dev_info_t *, ushort_t, ushort_t, 87 ushort_t, ushort_t, uchar_t, uint_t, int); 88 static int add_reg_props(dev_info_t *, uchar_t, uchar_t, uchar_t, int, int); 89 static void add_ppb_props(dev_info_t *, uchar_t, uchar_t, uchar_t, int); 90 static void add_model_prop(dev_info_t *, uint_t); 91 static void add_bus_range_prop(int); 92 static void add_bus_slot_names_prop(int); 93 static void add_ppb_ranges_prop(int); 94 static void add_bus_available_prop(int); 95 static void alloc_res_array(); 96 97 extern int pci_slot_names_prop(int, char *, int); 98 99 /* set non-zero to force PCI peer-bus renumbering */ 100 int pci_bus_always_renumber = 0; 101 102 /* get the subordinate bus # for a root/peer bus */ 103 static int 104 pci_root_subbus(int bus, uchar_t *subbus) 105 { 106 ACPI_HANDLE hdl; 107 ACPI_BUFFER rb; 108 ACPI_RESOURCE *rp; 109 int rv; 110 111 if (pci_bus_res[bus].dip == NULL) { 112 /* non-used bus # */ 113 return (AE_ERROR); 114 } 115 if (acpica_find_pciobj(pci_bus_res[bus].dip, &hdl) != AE_OK) { 116 cmn_err(CE_WARN, "!No ACPI obj for bus%d, ACPI OFF?\n", bus); 117 return (AE_ERROR); 118 } 119 120 rb.Length = ACPI_ALLOCATE_BUFFER; 121 if (AcpiGetCurrentResources(hdl, &rb) != AE_OK) { 122 cmn_err(CE_WARN, "!_CRS failed on pci%d\n", bus); 123 return (AE_ERROR); 124 } 125 126 rv = AE_ERROR; 127 128 for (rp = rb.Pointer; rp->Type != ACPI_RESOURCE_TYPE_END_TAG; 129 rp = ACPI_NEXT_RESOURCE(rp)) { 130 131 switch (rp->Type) { 132 case ACPI_RESOURCE_TYPE_ADDRESS16: 133 if (rp->Data.Address.ResourceType 134 != ACPI_BUS_NUMBER_RANGE) 135 continue; 136 *subbus = (uchar_t)rp->Data.Address16.Maximum; 137 dcmn_err(CE_NOTE, "Address16,subbus=%d\n", *subbus); 138 break; 139 case ACPI_RESOURCE_TYPE_ADDRESS32: 140 if (rp->Data.Address.ResourceType 141 != ACPI_BUS_NUMBER_RANGE) 142 continue; 143 *subbus = (uchar_t)rp->Data.Address32.Maximum; 144 dcmn_err(CE_NOTE, "Address32,subbus=%d\n", *subbus); 145 break; 146 case ACPI_RESOURCE_TYPE_ADDRESS64: 147 if (rp->Data.Address.ResourceType 148 != ACPI_BUS_NUMBER_RANGE) 149 continue; 150 *subbus = (uchar_t)rp->Data.Address64.Maximum; 151 dcmn_err(CE_NOTE, "Address64,subbus=%d\n", *subbus); 152 break; 153 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64: 154 if (rp->Data.Address.ResourceType 155 != ACPI_BUS_NUMBER_RANGE) 156 continue; 157 *subbus = (uchar_t)rp->Data.ExtAddress64.Maximum; 158 dcmn_err(CE_NOTE, "ExtAdr64,subbus=%d\n", *subbus); 159 break; 160 default: 161 dcmn_err(CE_NOTE, "rp->Type=%d\n", rp->Type); 162 continue; 163 } 164 165 /* found the bus-range resource */ 166 dcmn_err(CE_NOTE, "pci%d, subbus=%d\n", bus, *subbus); 167 rv = AE_OK; 168 169 /* This breaks out of the resource scanning loop */ 170 break; 171 } 172 173 AcpiOsFree(rb.Pointer); 174 if (rv != AE_OK) 175 cmn_err(CE_NOTE, "!No bus-range resource for pci%d\n", bus); 176 177 return (rv); 178 179 } 180 181 /* 182 * Enumerate all PCI devices 183 */ 184 void 185 pci_setup_tree() 186 { 187 uchar_t i, root_bus_addr = 0; 188 189 alloc_res_array(); 190 for (i = 0; i <= pci_bios_nbus; i++) { 191 pci_bus_res[i].par_bus = (uchar_t)-1; 192 pci_bus_res[i].root_addr = (uchar_t)-1; 193 pci_bus_res[i].sub_bus = i; 194 } 195 196 pci_bus_res[0].root_addr = root_bus_addr++; 197 create_root_bus_dip(0); 198 enumerate_bus_devs(0, CONFIG_INFO); 199 200 /* 201 * Now enumerate peer busses 202 * 203 * We loop till pci_bios_nbus. On most systems, there is 204 * one more bus at the high end, which implements the ISA 205 * compatibility bus. We don't care about that. 206 * 207 * Note: In the old (bootconf) enumeration, the peer bus 208 * address did not use the bus number, and there were 209 * too many peer busses created. The root_bus_addr is 210 * used to maintain the old peer bus address assignment. 211 * However, we stop enumerating phantom peers with no 212 * device below. 213 */ 214 for (i = 1; i <= pci_bios_nbus; i++) { 215 if (pci_bus_res[i].dip == NULL) { 216 pci_bus_res[i].root_addr = root_bus_addr++; 217 } 218 enumerate_bus_devs(i, CONFIG_INFO); 219 220 /* add slot-names property for named pci hot-plug slots */ 221 add_bus_slot_names_prop(i); 222 } 223 224 } 225 226 /* 227 * >0 = present, 0 = not present, <0 = error 228 */ 229 static int 230 pci_bbn_present(int bus) 231 { 232 ACPI_HANDLE hdl; 233 ACPI_BUFFER rb; 234 int rv; 235 236 /* no dip means no _BBN */ 237 if (pci_bus_res[bus].dip == NULL) 238 return (0); 239 240 rv = acpica_find_pciobj(pci_bus_res[bus].dip, &hdl); 241 if (rv != AE_OK) 242 return (-1); 243 244 rb.Length = ACPI_ALLOCATE_BUFFER; 245 246 rv = AcpiEvaluateObject(hdl, "_BBN", NULL, &rb); 247 248 if (rb.Length > 0) 249 AcpiOsFree(rb.Pointer); 250 251 if (rv == AE_OK) 252 return (1); 253 else if (rv == AE_NOT_FOUND) 254 return (0); 255 else 256 return (-1); 257 } 258 259 /* 260 * Return non-zero if any PCI bus in the system has an associated 261 * _BBN object, 0 otherwise. 262 */ 263 static int 264 pci_roots_have_bbn(void) 265 { 266 int i; 267 268 /* 269 * Scan the PCI busses and look for at least 1 _BBN 270 */ 271 for (i = 0; i <= pci_bios_nbus; i++) { 272 /* skip non-root (peer) PCI busses */ 273 if (pci_bus_res[i].par_bus != (uchar_t)-1) 274 continue; 275 276 if (pci_bbn_present(i) > 0) 277 return (1); 278 } 279 return (0); 280 281 } 282 283 /* 284 * return non-zero if the machine is one on which we renumber 285 * the internal pci unit-addresses 286 */ 287 static int 288 pci_bus_renumber() 289 { 290 ACPI_TABLE_HEADER *fadt; 291 292 if (pci_bus_always_renumber) 293 return (1); 294 295 /* get the FADT */ 296 if (AcpiGetFirmwareTable(FADT_SIG, 1, ACPI_LOGICAL_ADDRESSING, 297 (ACPI_TABLE_HEADER **)&fadt) != AE_OK) 298 return (0); 299 300 /* compare OEM Table ID to "SUNm31" */ 301 if (strncmp("SUNm31", fadt->OemId, 6)) 302 return (0); 303 else 304 return (1); 305 } 306 307 /* 308 * Initial enumeration of the physical PCI bus hierarchy can 309 * leave 'gaps' in the order of peer PCI bus unit-addresses. 310 * Systems with more than one peer PCI bus *must* have an ACPI 311 * _BBN object associated with each peer bus; use the presence 312 * of this object to remove gaps in the numbering of the peer 313 * PCI bus unit-addresses - only peer busses with an associated 314 * _BBN are counted. 315 */ 316 static void 317 pci_renumber_root_busses(void) 318 { 319 int pci_regs[] = {0, 0, 0}; 320 int i, root_addr = 0; 321 322 /* 323 * Currently, we only enable the re-numbering on specific 324 * Sun machines; this is a work-around for the more complicated 325 * issue of upgrade changing physical device paths 326 */ 327 if (!pci_bus_renumber()) 328 return; 329 330 /* 331 * If we find no _BBN objects at all, we either don't need 332 * to do anything or can't do anything anyway 333 */ 334 if (!pci_roots_have_bbn()) 335 return; 336 337 for (i = 0; i <= pci_bios_nbus; i++) { 338 /* skip non-root (peer) PCI busses */ 339 if (pci_bus_res[i].par_bus != (uchar_t)-1) 340 continue; 341 342 if (pci_bbn_present(i) < 1) { 343 pci_bus_res[i].root_addr = (uchar_t)-1; 344 continue; 345 } 346 347 ASSERT(pci_bus_res[i].dip != NULL); 348 if (pci_bus_res[i].root_addr != root_addr) { 349 /* update reg property for node */ 350 pci_bus_res[i].root_addr = root_addr; 351 pci_regs[0] = pci_bus_res[i].root_addr; 352 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, 353 pci_bus_res[i].dip, "reg", (int *)pci_regs, 3); 354 } 355 root_addr++; 356 } 357 } 358 359 void 360 pci_reprogram(void) 361 { 362 int i, pci_reconfig = 1; 363 char *onoff; 364 365 /* 366 * Excise phantom roots if possible 367 */ 368 pci_renumber_root_busses(); 369 370 /* add bus-range property for root/peer bus nodes */ 371 for (i = 0; i <= pci_bios_nbus; i++) { 372 if (pci_bus_res[i].par_bus == (uchar_t)-1) { 373 uchar_t subbus; 374 if (pci_root_subbus(i, &subbus) == AE_OK) 375 pci_bus_res[i].sub_bus = subbus; 376 add_bus_range_prop(i); 377 } 378 } 379 380 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(), 381 DDI_PROP_DONTPASS, "pci-reprog", &onoff) == DDI_SUCCESS) { 382 if (strcmp(onoff, "off") == 0) { 383 pci_reconfig = 0; 384 cmn_err(CE_NOTE, "pci device reprogramming disabled"); 385 } 386 ddi_prop_free(onoff); 387 } 388 389 for (i = 0; i <= pci_bios_nbus; i++) { 390 /* configure devices not configured by bios */ 391 if (pci_reconfig) 392 enumerate_bus_devs(i, CONFIG_NEW); 393 /* All dev programmed, so we can create available prop */ 394 add_bus_available_prop(i); 395 } 396 } 397 398 /* 399 * Create top-level bus dips, i.e. /pci@0,0, /pci@1,0... 400 */ 401 static void 402 create_root_bus_dip(uchar_t bus) 403 { 404 int pci_regs[] = {0, 0, 0}; 405 dev_info_t *dip; 406 407 ASSERT(pci_bus_res[bus].par_bus == (uchar_t)-1); 408 409 ndi_devi_alloc_sleep(ddi_root_node(), "pci", 410 (pnode_t)DEVI_SID_NODEID, &dip); 411 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 412 "#address-cells", 3); 413 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 414 "#size-cells", 2); 415 pci_regs[0] = pci_bus_res[bus].root_addr; 416 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, 417 "reg", (int *)pci_regs, 3); 418 419 /* 420 * If system has PCIe bus, then create different properties 421 */ 422 if (create_pcie_root_bus(bus, dip) == B_FALSE) 423 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 424 "device_type", "pci"); 425 426 (void) ndi_devi_bind_driver(dip, 0); 427 pci_bus_res[bus].dip = dip; 428 pci_bus_res[bus].pmem_space = find_bus_res(bus, PREFETCH_TYPE); 429 pci_bus_res[bus].mem_space = find_bus_res(bus, MEM_TYPE); 430 pci_bus_res[bus].io_ports = find_bus_res(bus, IO_TYPE); 431 432 if (bus != 0) 433 return; 434 435 /* 436 * Special treatment of bus 0: 437 * If no resource from MPSPEC/HRT, copy pcimem from boot 438 * and make io space the entire range. There is no difference 439 * between prefetchable memory or not. 440 */ 441 if (pci_bus_res[0].mem_space == NULL) 442 pci_bus_res[0].mem_space = 443 memlist_dup(bootops->boot_mem->pcimem); 444 if (pci_bus_res[0].io_ports == NULL) 445 memlist_insert(&pci_bus_res[0].io_ports, 0, 0x10000); 446 } 447 448 /* 449 * For any fixed configuration (often compatability) pci devices 450 * and those with their own expansion rom, create device nodes 451 * to hold the already configured device details. 452 */ 453 void 454 enumerate_bus_devs(uchar_t bus, int config_op) 455 { 456 uchar_t dev, func, nfunc, header; 457 ushort_t venid; 458 dev_info_t *dip; 459 struct pci_devfunc { 460 struct pci_devfunc *next; 461 dev_info_t *dip; 462 uchar_t bus; 463 uchar_t dev; 464 uchar_t func; 465 } *devlist = NULL, *entry; 466 467 if (config_op == CONFIG_NEW) { 468 dcmn_err(CE_NOTE, "configuring pci bus 0x%x", bus); 469 } else if (config_op == CONFIG_FIX) { 470 dcmn_err(CE_NOTE, "fixing devices on pci bus 0x%x", bus); 471 } else 472 dcmn_err(CE_NOTE, "enumerating pci bus 0x%x", bus); 473 474 for (dev = 0; dev < max_dev_pci; dev++) { 475 nfunc = 1; 476 for (func = 0; func < nfunc; func++) { 477 478 dcmn_err(CE_NOTE, "probing dev 0x%x, func 0x%x", 479 dev, func); 480 481 venid = pci_getw(bus, dev, func, PCI_CONF_VENID); 482 483 if ((venid == 0xffff) || (venid == 0)) { 484 /* no function at this address */ 485 continue; 486 } 487 488 header = pci_getb(bus, dev, func, PCI_CONF_HEADER); 489 if (header == 0xff) { 490 continue; /* illegal value */ 491 } 492 493 /* 494 * according to some mail from Microsoft posted 495 * to the pci-drivers alias, their only requirement 496 * for a multifunction device is for the 1st 497 * function to have to PCI_HEADER_MULTI bit set. 498 */ 499 if ((func == 0) && (header & PCI_HEADER_MULTI)) { 500 nfunc = 8; 501 } 502 503 if (config_op == CONFIG_FIX) { 504 /* 505 * If we're processing PCI fixes, no dip 506 * will be returned. 507 */ 508 (void) process_devfunc(bus, dev, func, header, 509 venid, config_op); 510 511 } else if (config_op == CONFIG_INFO) { 512 /* 513 * Create the node, unconditionally, on the 514 * first pass only. It may still need 515 * resource assignment, which will be 516 * done on the second, CONFIG_NEW, pass. 517 */ 518 dip = process_devfunc(bus, dev, func, header, 519 venid, config_op); 520 /* 521 * If dip isn't null, put on a list to 522 * save for reprogramming when config_op 523 * is CONFIG_NEW. 524 */ 525 526 if (dip) { 527 entry = kmem_alloc(sizeof (*entry), 528 KM_SLEEP); 529 entry->dip = dip; 530 entry->dev = dev; 531 entry->func = func; 532 entry->next = devlist; 533 devlist = entry; 534 } 535 } 536 } 537 } 538 539 if (config_op == CONFIG_NEW) { 540 devlist = (struct pci_devfunc *)pci_bus_res[bus].privdata; 541 while (devlist) { 542 entry = devlist; 543 devlist = entry->next; 544 cmn_err(CE_NOTE, 545 "!reprogram pci device [%d/%d/%d] (%s)", 546 bus, entry->dev, entry->func, 547 ddi_driver_name(entry->dip)); 548 (void) add_reg_props(entry->dip, bus, entry->dev, 549 entry->func, CONFIG_UPDATE, 0); 550 kmem_free(entry, sizeof (*entry)); 551 } 552 pci_bus_res[bus].privdata = NULL; 553 } else if (config_op != CONFIG_FIX) { 554 pci_bus_res[bus].privdata = devlist; 555 } 556 } 557 558 static int 559 check_pciide_prop(uchar_t revid, ushort_t venid, ushort_t devid, 560 ushort_t subvenid, ushort_t subdevid) 561 { 562 static int prop_exist = -1; 563 static char *pciide_str; 564 char compat[32]; 565 566 if (prop_exist == -1) { 567 prop_exist = (ddi_prop_lookup_string(DDI_DEV_T_ANY, 568 ddi_root_node(), DDI_PROP_DONTPASS, "pci-ide", 569 &pciide_str) == DDI_SUCCESS); 570 } 571 572 if (!prop_exist) 573 return (0); 574 575 /* compare property value against various forms of compatible */ 576 if (subvenid) { 577 (void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x.%x", 578 venid, devid, subvenid, subdevid, revid); 579 if (strcmp(pciide_str, compat) == 0) 580 return (1); 581 582 (void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x", 583 venid, devid, subvenid, subdevid); 584 if (strcmp(pciide_str, compat) == 0) 585 return (1); 586 587 (void) snprintf(compat, sizeof (compat), "pci%x,%x", 588 subvenid, subdevid); 589 if (strcmp(pciide_str, compat) == 0) 590 return (1); 591 } 592 (void) snprintf(compat, sizeof (compat), "pci%x,%x.%x", 593 venid, devid, revid); 594 if (strcmp(pciide_str, compat) == 0) 595 return (1); 596 597 (void) snprintf(compat, sizeof (compat), "pci%x,%x", venid, devid); 598 if (strcmp(pciide_str, compat) == 0) 599 return (1); 600 601 return (0); 602 } 603 604 static int 605 is_pciide(uchar_t basecl, uchar_t subcl, uchar_t revid, 606 ushort_t venid, ushort_t devid, ushort_t subvenid, ushort_t subdevid) 607 { 608 struct ide_table { /* table for PCI_MASS_OTHER */ 609 ushort_t venid; 610 ushort_t devid; 611 } *entry; 612 613 /* XXX SATA devices: need a way to add dynamically */ 614 static struct ide_table ide_other[] = { 615 {0x1095, 0x3112}, 616 {0x1095, 0x3114}, 617 {0x1095, 0x3512}, 618 {0, 0} 619 }; 620 621 if (basecl != PCI_CLASS_MASS) 622 return (0); 623 624 if (subcl == PCI_MASS_IDE) { 625 return (1); 626 } 627 628 if (subcl != PCI_MASS_OTHER && subcl != PCI_MASS_SATA) { 629 return (0); 630 } 631 632 entry = &ide_other[0]; 633 while (entry->venid) { 634 if (entry->venid == venid && entry->devid == devid) 635 return (1); 636 entry++; 637 } 638 return (check_pciide_prop(revid, venid, devid, subvenid, subdevid)); 639 } 640 641 static int 642 is_display(uint_t classcode) 643 { 644 static uint_t disp_classes[] = { 645 0x000100, 646 0x030000, 647 0x030001 648 }; 649 int i, nclasses = sizeof (disp_classes) / sizeof (uint_t); 650 651 for (i = 0; i < nclasses; i++) { 652 if (classcode == disp_classes[i]) 653 return (1); 654 } 655 return (0); 656 } 657 658 static void 659 add_undofix_entry(uint8_t bus, uint8_t dev, uint8_t fn, 660 void (*undofn)(uint8_t, uint8_t, uint8_t)) 661 { 662 struct pci_fixundo *newundo; 663 664 newundo = kmem_alloc(sizeof (struct pci_fixundo), KM_SLEEP); 665 666 /* 667 * Adding an item to this list means that we must turn its NMIENABLE 668 * bit back on at a later time. 669 */ 670 newundo->bus = bus; 671 newundo->dev = dev; 672 newundo->fn = fn; 673 newundo->undofn = undofn; 674 newundo->next = undolist; 675 676 /* add to the undo list in LIFO order */ 677 undolist = newundo; 678 } 679 680 void 681 add_pci_fixes(void) 682 { 683 int i; 684 685 for (i = 0; i <= pci_bios_nbus; i++) { 686 /* 687 * For each bus, apply needed fixes to the appropriate devices. 688 * This must be done before the main enumeration loop because 689 * some fixes must be applied to devices normally encountered 690 * later in the pci scan (e.g. if a fix to device 7 must be 691 * applied before scanning device 6, applying fixes in the 692 * normal enumeration loop would obviously be too late). 693 */ 694 enumerate_bus_devs(i, CONFIG_FIX); 695 } 696 } 697 698 void 699 undo_pci_fixes(void) 700 { 701 struct pci_fixundo *nextundo; 702 uint8_t bus, dev, fn; 703 704 /* 705 * All fixes in the undo list are performed unconditionally. Future 706 * fixes may require selective undo. 707 */ 708 while (undolist != NULL) { 709 710 bus = undolist->bus; 711 dev = undolist->dev; 712 fn = undolist->fn; 713 714 (*(undolist->undofn))(bus, dev, fn); 715 716 nextundo = undolist->next; 717 kmem_free(undolist, sizeof (struct pci_fixundo)); 718 undolist = nextundo; 719 } 720 } 721 722 static void 723 undo_amd8111_pci_fix(uint8_t bus, uint8_t dev, uint8_t fn) 724 { 725 uint8_t val8; 726 727 val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1); 728 /* 729 * The NMIONERR bit is turned back on to allow the SMM BIOS 730 * to handle more critical PCI errors (e.g. PERR#). 731 */ 732 val8 |= AMD8111_ENABLENMI; 733 pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8); 734 } 735 736 static void 737 pci_fix_amd8111(uint8_t bus, uint8_t dev, uint8_t fn) 738 { 739 uint8_t val8; 740 741 val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1); 742 743 if ((val8 & AMD8111_ENABLENMI) == 0) 744 return; 745 746 /* 747 * We reset NMIONERR in the LPC because master-abort on the PCI 748 * bridge side of the 8111 will cause NMI, which might cause SMI, 749 * which sometimes prevents all devices from being enumerated. 750 */ 751 val8 &= ~AMD8111_ENABLENMI; 752 753 pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8); 754 755 add_undofix_entry(bus, dev, fn, undo_amd8111_pci_fix); 756 } 757 758 static dev_info_t * 759 process_devfunc(uchar_t bus, uchar_t dev, uchar_t func, uchar_t header, 760 ushort_t vendorid, int config_op) 761 { 762 char nodename[32], unitaddr[5]; 763 dev_info_t *dip; 764 uchar_t basecl, subcl, intr, revid; 765 ushort_t subvenid, subdevid, status; 766 ushort_t slot_num; 767 uint_t classcode, revclass; 768 int reprogram = 0, pciide; 769 int power[2] = {1, 1}; 770 int pciex = 0; 771 ushort_t is_pci_bridge = 0; 772 773 ushort_t deviceid = pci_getw(bus, dev, func, PCI_CONF_DEVID); 774 775 switch (header & PCI_HEADER_TYPE_M) { 776 case PCI_HEADER_ZERO: 777 subvenid = pci_getw(bus, dev, func, PCI_CONF_SUBVENID); 778 subdevid = pci_getw(bus, dev, func, PCI_CONF_SUBSYSID); 779 break; 780 case PCI_HEADER_CARDBUS: 781 subvenid = pci_getw(bus, dev, func, PCI_CBUS_SUBVENID); 782 subdevid = pci_getw(bus, dev, func, PCI_CBUS_SUBSYSID); 783 break; 784 default: 785 subvenid = 0; 786 subdevid = 0; 787 break; 788 } 789 790 if (config_op == CONFIG_FIX) { 791 if (vendorid == VENID_AMD && deviceid == DEVID_AMD8111_LPC) { 792 pci_fix_amd8111(bus, dev, func); 793 } 794 return (NULL); 795 } 796 797 /* XXX should be use generic names? derive from class? */ 798 revclass = pci_getl(bus, dev, func, PCI_CONF_REVID); 799 classcode = revclass >> 8; 800 revid = revclass & 0xff; 801 802 /* figure out if this is pci-ide */ 803 basecl = classcode >> 16; 804 subcl = (classcode >> 8) & 0xff; 805 pciide = is_pciide(basecl, subcl, revid, vendorid, deviceid, 806 subvenid, subdevid); 807 808 if (pciide) 809 (void) snprintf(nodename, sizeof (nodename), "pci-ide"); 810 else if (is_display(classcode)) 811 (void) snprintf(nodename, sizeof (nodename), "display"); 812 else if (subvenid != 0) 813 (void) snprintf(nodename, sizeof (nodename), 814 "pci%x,%x", subvenid, subdevid); 815 else 816 (void) snprintf(nodename, sizeof (nodename), 817 "pci%x,%x", vendorid, deviceid); 818 819 /* make sure parent bus dip has been created */ 820 if (pci_bus_res[bus].dip == NULL) { 821 create_root_bus_dip(bus); 822 } 823 824 ndi_devi_alloc_sleep(pci_bus_res[bus].dip, nodename, 825 DEVI_SID_NODEID, &dip); 826 827 if (check_if_device_is_pciex(dip, bus, dev, func, &slot_num, 828 &is_pci_bridge) == B_TRUE) 829 pciex = 1; 830 831 /* add properties */ 832 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "device-id", deviceid); 833 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "vendor-id", vendorid); 834 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "revision-id", revid); 835 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 836 "class-code", classcode); 837 if (func == 0) 838 (void) snprintf(unitaddr, sizeof (unitaddr), "%x", dev); 839 else 840 (void) snprintf(unitaddr, sizeof (unitaddr), 841 "%x,%x", dev, func); 842 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 843 "unit-address", unitaddr); 844 845 /* add device_type for display nodes */ 846 if (is_display(classcode)) { 847 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 848 "device_type", "display"); 849 } 850 /* add special stuff for header type */ 851 if ((header & PCI_HEADER_TYPE_M) == PCI_HEADER_ZERO) { 852 uchar_t mingrant = pci_getb(bus, dev, func, PCI_CONF_MIN_G); 853 uchar_t maxlatency = pci_getb(bus, dev, func, PCI_CONF_MAX_L); 854 855 if (subvenid != 0) { 856 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 857 "subsystem-id", subdevid); 858 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 859 "subsystem-vendor-id", subvenid); 860 } 861 if (!pciex) 862 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 863 "min-grant", mingrant); 864 if (!pciex) 865 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 866 "max-latency", maxlatency); 867 } 868 869 /* interrupt, record if not 0 */ 870 intr = pci_getb(bus, dev, func, PCI_CONF_IPIN); 871 if (intr != 0) 872 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 873 "interrupts", intr); 874 875 /* 876 * Add support for 133 mhz pci eventually 877 */ 878 status = pci_getw(bus, dev, func, PCI_CONF_STAT); 879 880 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 881 "devsel-speed", (status & PCI_STAT_DEVSELT) >> 9); 882 if (!pciex && (status & PCI_STAT_FBBC)) 883 (void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip, 884 "fast-back-to-back"); 885 if (!pciex && (status & PCI_STAT_66MHZ)) 886 (void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip, 887 "66mhz-capable"); 888 if (status & PCI_STAT_UDF) 889 (void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip, 890 "udf-supported"); 891 if (pciex && slot_num) 892 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 893 "physical-slot#", slot_num); 894 895 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, 896 "power-consumption", power, 2); 897 898 if ((basecl == PCI_CLASS_BRIDGE) && (subcl == PCI_BRIDGE_PCI)) 899 add_ppb_props(dip, bus, dev, func, pciex); 900 901 /* check for ck8-04 based PCI ISA bridge only */ 902 if (NVIDIA_IS_LPC_BRIDGE(vendorid, deviceid) && (dev == 1) && 903 (func == 0)) 904 add_ck804_isa_bridge_props(dip, bus, dev, func); 905 906 if (pciex && is_pci_bridge) 907 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model", 908 (char *)"PCIe-PCI bridge"); 909 else 910 add_model_prop(dip, classcode); 911 912 add_compatible(dip, subvenid, subdevid, vendorid, deviceid, 913 revid, classcode, pciex); 914 reprogram = add_reg_props(dip, bus, dev, func, config_op, pciide); 915 (void) ndi_devi_bind_driver(dip, 0); 916 917 /* special handling for pci-ide */ 918 if (pciide) { 919 dev_info_t *cdip; 920 921 /* 922 * Create properties specified by P1275 Working Group 923 * Proposal #414 Version 1 924 */ 925 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 926 "device_type", "pci-ide"); 927 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 928 "#address-cells", 1); 929 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 930 "#size-cells", 0); 931 932 /* allocate two child nodes */ 933 ndi_devi_alloc_sleep(dip, "ide", 934 (pnode_t)DEVI_SID_NODEID, &cdip); 935 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip, 936 "reg", 0); 937 (void) ndi_devi_bind_driver(cdip, 0); 938 ndi_devi_alloc_sleep(dip, "ide", 939 (pnode_t)DEVI_SID_NODEID, &cdip); 940 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip, 941 "reg", 1); 942 (void) ndi_devi_bind_driver(cdip, 0); 943 944 reprogram = 0; /* don't reprogram pci-ide bridge */ 945 } 946 947 948 if (reprogram) 949 return (dip); 950 return (NULL); 951 } 952 953 /* 954 * Set the compatible property to a value compliant with 955 * rev 2.1 of the IEEE1275 PCI binding. 956 * (Also used for PCI-Express devices). 957 * 958 * pciVVVV,DDDD.SSSS.ssss.RR (0) 959 * pciVVVV,DDDD.SSSS.ssss (1) 960 * pciSSSS,ssss (2) 961 * pciVVVV,DDDD.RR (3) 962 * pciVVVV,DDDD (4) 963 * pciclass,CCSSPP (5) 964 * pciclass,CCSS (6) 965 * 966 * The Subsystem (SSSS) forms are not inserted if 967 * subsystem-vendor-id is 0. 968 * 969 * NOTE: For PCI-Express devices "pci" is replaced with "pciex" in 0-6 above 970 * property 2 is not created as per "1275 bindings for PCI Express Interconnect" 971 * 972 * Set with setprop and \x00 between each 973 * to generate the encoded string array form. 974 */ 975 void 976 add_compatible(dev_info_t *dip, ushort_t subvenid, ushort_t subdevid, 977 ushort_t vendorid, ushort_t deviceid, uchar_t revid, uint_t classcode, 978 int pciex) 979 { 980 int i = 0; 981 int size = COMPAT_BUFSIZE; 982 char *compat[13]; 983 char *buf, *curr; 984 985 curr = buf = kmem_alloc(size, KM_SLEEP); 986 987 if (pciex) { 988 if (subvenid) { 989 compat[i++] = curr; /* form 0 */ 990 (void) snprintf(curr, size, "pciex%x,%x.%x.%x.%x", 991 vendorid, deviceid, subvenid, subdevid, revid); 992 size -= strlen(curr) + 1; 993 curr += strlen(curr) + 1; 994 995 compat[i++] = curr; /* form 1 */ 996 (void) snprintf(curr, size, "pciex%x,%x.%x.%x", 997 vendorid, deviceid, subvenid, subdevid); 998 size -= strlen(curr) + 1; 999 curr += strlen(curr) + 1; 1000 1001 } 1002 compat[i++] = curr; /* form 3 */ 1003 (void) snprintf(curr, size, "pciex%x,%x.%x", 1004 vendorid, deviceid, revid); 1005 size -= strlen(curr) + 1; 1006 curr += strlen(curr) + 1; 1007 1008 compat[i++] = curr; /* form 4 */ 1009 (void) snprintf(curr, size, "pciex%x,%x", vendorid, deviceid); 1010 size -= strlen(curr) + 1; 1011 curr += strlen(curr) + 1; 1012 1013 compat[i++] = curr; /* form 5 */ 1014 (void) snprintf(curr, size, "pciexclass,%06x", classcode); 1015 size -= strlen(curr) + 1; 1016 curr += strlen(curr) + 1; 1017 1018 compat[i++] = curr; /* form 6 */ 1019 (void) snprintf(curr, size, "pciexclass,%04x", 1020 (classcode >> 8)); 1021 size -= strlen(curr) + 1; 1022 curr += strlen(curr) + 1; 1023 } 1024 1025 if (subvenid) { 1026 compat[i++] = curr; /* form 0 */ 1027 (void) snprintf(curr, size, "pci%x,%x.%x.%x.%x", 1028 vendorid, deviceid, subvenid, subdevid, revid); 1029 size -= strlen(curr) + 1; 1030 curr += strlen(curr) + 1; 1031 1032 compat[i++] = curr; /* form 1 */ 1033 (void) snprintf(curr, size, "pci%x,%x.%x.%x", 1034 vendorid, deviceid, subvenid, subdevid); 1035 size -= strlen(curr) + 1; 1036 curr += strlen(curr) + 1; 1037 1038 compat[i++] = curr; /* form 2 */ 1039 (void) snprintf(curr, size, "pci%x,%x", subvenid, subdevid); 1040 size -= strlen(curr) + 1; 1041 curr += strlen(curr) + 1; 1042 } 1043 compat[i++] = curr; /* form 3 */ 1044 (void) snprintf(curr, size, "pci%x,%x.%x", vendorid, deviceid, revid); 1045 size -= strlen(curr) + 1; 1046 curr += strlen(curr) + 1; 1047 1048 compat[i++] = curr; /* form 4 */ 1049 (void) snprintf(curr, size, "pci%x,%x", vendorid, deviceid); 1050 size -= strlen(curr) + 1; 1051 curr += strlen(curr) + 1; 1052 1053 compat[i++] = curr; /* form 5 */ 1054 (void) snprintf(curr, size, "pciclass,%06x", classcode); 1055 size -= strlen(curr) + 1; 1056 curr += strlen(curr) + 1; 1057 1058 compat[i++] = curr; /* form 6 */ 1059 (void) snprintf(curr, size, "pciclass,%04x", (classcode >> 8)); 1060 size -= strlen(curr) + 1; 1061 curr += strlen(curr) + 1; 1062 1063 (void) ndi_prop_update_string_array(DDI_DEV_T_NONE, dip, 1064 "compatible", compat, i); 1065 kmem_free(buf, COMPAT_BUFSIZE); 1066 } 1067 1068 /* 1069 * Adjust the reg properties for a dual channel PCI-IDE device. 1070 * 1071 * NOTE: don't do anything that changes the order of the hard-decodes 1072 * and programmed BARs. The kernel driver depends on these values 1073 * being in this order regardless of whether they're for a 'native' 1074 * mode BAR or not. 1075 */ 1076 /* 1077 * config info for pci-ide devices 1078 */ 1079 static struct { 1080 uchar_t native_mask; /* 0 == 'compatibility' mode, 1 == native */ 1081 uchar_t bar_offset; /* offset for alt status register */ 1082 ushort_t addr; /* compatibility mode base address */ 1083 ushort_t length; /* number of ports for this BAR */ 1084 } pciide_bar[] = { 1085 { 0x01, 0, 0x1f0, 8 }, /* primary lower BAR */ 1086 { 0x01, 2, 0x3f6, 1 }, /* primary upper BAR */ 1087 { 0x04, 0, 0x170, 8 }, /* secondary lower BAR */ 1088 { 0x04, 2, 0x376, 1 } /* secondary upper BAR */ 1089 }; 1090 1091 static int 1092 pciIdeAdjustBAR(uchar_t progcl, int index, uint_t *basep, uint_t *lenp) 1093 { 1094 int hard_decode = 0; 1095 1096 /* 1097 * Adjust the base and len for the BARs of the PCI-IDE 1098 * device's primary and secondary controllers. The first 1099 * two BARs are for the primary controller and the next 1100 * two BARs are for the secondary controller. The fifth 1101 * and sixth bars are never adjusted. 1102 */ 1103 if (index >= 0 && index <= 3) { 1104 *lenp = pciide_bar[index].length; 1105 1106 if (progcl & pciide_bar[index].native_mask) { 1107 *basep += pciide_bar[index].bar_offset; 1108 } else { 1109 *basep = pciide_bar[index].addr; 1110 hard_decode = 1; 1111 } 1112 } 1113 1114 /* 1115 * if either base or len is zero make certain both are zero 1116 */ 1117 if (*basep == 0 || *lenp == 0) { 1118 *basep = 0; 1119 *lenp = 0; 1120 hard_decode = 0; 1121 } 1122 1123 return (hard_decode); 1124 } 1125 1126 1127 /* 1128 * Add the "reg" and "assigned-addresses" property 1129 */ 1130 static int 1131 add_reg_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func, 1132 int config_op, int pciide) 1133 { 1134 uchar_t baseclass, subclass, progclass, header; 1135 ushort_t bar_sz; 1136 uint_t value = 0, len, devloc; 1137 uint_t base, base_hi, type; 1138 ushort_t offset, end; 1139 int max_basereg, j, reprogram = 0; 1140 uint_t phys_hi; 1141 struct memlist **io_res, **mres, **mem_res, **pmem_res; 1142 uint16_t cmd_reg; 1143 1144 pci_regspec_t regs[16] = {{0}}; 1145 pci_regspec_t assigned[15] = {{0}}; 1146 int nreg, nasgn, enable = 0; 1147 1148 io_res = &pci_bus_res[bus].io_ports; 1149 mem_res = &pci_bus_res[bus].mem_space; 1150 if (bus == 0) /* for bus 0, there is only mem_space */ 1151 pmem_res = mem_res; 1152 else 1153 pmem_res = &pci_bus_res[bus].pmem_space; 1154 1155 devloc = (uint_t)bus << 16 | (uint_t)dev << 11 | (uint_t)func << 8; 1156 regs[0].pci_phys_hi = devloc; 1157 nreg = 1; /* rest of regs[0] is all zero */ 1158 nasgn = 0; 1159 1160 baseclass = pci_getb(bus, dev, func, PCI_CONF_BASCLASS); 1161 subclass = pci_getb(bus, dev, func, PCI_CONF_SUBCLASS); 1162 progclass = pci_getb(bus, dev, func, PCI_CONF_PROGCLASS); 1163 header = pci_getb(bus, dev, func, PCI_CONF_HEADER) & PCI_HEADER_TYPE_M; 1164 /* Fetch PCI command, disable I/O and memory */ 1165 cmd_reg = pci_getw(bus, dev, func, PCI_CONF_COMM); 1166 pci_putw(bus, dev, func, PCI_CONF_COMM, 1167 cmd_reg & ~(PCI_COMM_IO | PCI_COMM_MAE)); 1168 1169 switch (header) { 1170 case PCI_HEADER_ZERO: 1171 max_basereg = PCI_BASE_NUM; 1172 break; 1173 case PCI_HEADER_PPB: 1174 max_basereg = PCI_BCNF_BASE_NUM; 1175 break; 1176 case PCI_HEADER_CARDBUS: 1177 max_basereg = PCI_CBUS_BASE_NUM; 1178 break; 1179 default: 1180 max_basereg = 0; 1181 break; 1182 } 1183 1184 /* 1185 * Create the register property by saving the current 1186 * value of the base register. Disable memory/io, then 1187 * write 0xffffffff to the base register. Read the 1188 * value back to determine the required size of the 1189 * address space. Restore the base register 1190 * contents. 1191 */ 1192 end = PCI_CONF_BASE0 + max_basereg * sizeof (uint_t); 1193 for (j = 0, offset = PCI_CONF_BASE0; offset < end; 1194 j++, offset += bar_sz) { 1195 int hard_decode = 0; 1196 1197 /* determine the size of the address space */ 1198 base = pci_getl(bus, dev, func, offset); 1199 pci_putl(bus, dev, func, offset, 0xffffffff); 1200 value = pci_getl(bus, dev, func, offset); 1201 pci_putl(bus, dev, func, offset, base); 1202 1203 /* construct phys hi,med.lo, size hi, lo */ 1204 if ((pciide && j < 4) || (base & PCI_BASE_SPACE_IO)) { 1205 /* i/o space */ 1206 bar_sz = PCI_BAR_SZ_32; 1207 value &= PCI_BASE_IO_ADDR_M; 1208 len = ((value ^ (value-1)) + 1) >> 1; 1209 1210 /* XXX Adjust first 4 IDE registers */ 1211 if (pciide) { 1212 if (subclass != PCI_MASS_IDE) 1213 progclass = (PCI_IDE_IF_NATIVE_PRI | 1214 PCI_IDE_IF_NATIVE_SEC); 1215 hard_decode = pciIdeAdjustBAR(progclass, j, 1216 &base, &len); 1217 } else if (value == 0) { 1218 /* skip base regs with size of 0 */ 1219 continue; 1220 } 1221 1222 regs[nreg].pci_size_low = 1223 assigned[nasgn].pci_size_low = len; 1224 if (!hard_decode) { 1225 regs[nreg].pci_phys_hi = 1226 (PCI_ADDR_IO | devloc) + offset; 1227 } else { 1228 regs[nreg].pci_phys_hi = 1229 (PCI_RELOCAT_B | PCI_ADDR_IO | devloc) + 1230 offset; 1231 regs[nreg].pci_phys_low = 1232 base & PCI_BASE_IO_ADDR_M; 1233 } 1234 assigned[nasgn].pci_phys_hi = 1235 (PCI_RELOCAT_B | PCI_ADDR_IO | devloc) + offset; 1236 type = base & (~PCI_BASE_IO_ADDR_M); 1237 base &= PCI_BASE_IO_ADDR_M; 1238 1239 /* 1240 * first pass - gather what's there 1241 * update/second pass - adjust/allocate regions 1242 * config - allocate regions 1243 */ 1244 if (config_op == CONFIG_INFO) { /* first pass */ 1245 /* take out of the resource map of the bus */ 1246 if (*io_res && base != 0) 1247 (void) memlist_remove(io_res, 1248 (uint64_t)base, (uint64_t)len); 1249 else if (*io_res) 1250 reprogram = 1; 1251 } else if (*io_res && base == 0) { 1252 base = (uint_t)memlist_find(io_res, 1253 (uint64_t)len, (uint64_t)0x4); 1254 if (base != 0) { 1255 /* XXX need to worry about 64-bit? */ 1256 pci_putl(bus, dev, func, offset, 1257 base | type); 1258 base = pci_getl(bus, dev, func, offset); 1259 base &= PCI_BASE_IO_ADDR_M; 1260 } 1261 if (base == 0) { 1262 cmn_err(CE_WARN, "failed to program" 1263 " IO space [%d/%d/%d] BAR@0x%x" 1264 " length 0x%x", 1265 bus, dev, func, offset, len); 1266 } else 1267 enable |= PCI_COMM_IO; 1268 } 1269 assigned[nasgn].pci_phys_low = base; 1270 nreg++, nasgn++; 1271 1272 } else { 1273 /* memory space */ 1274 if ((base & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL) { 1275 bar_sz = PCI_BAR_SZ_64; 1276 base_hi = pci_getl(bus, dev, func, offset + 4); 1277 phys_hi = PCI_ADDR_MEM64; 1278 } else { 1279 bar_sz = PCI_BAR_SZ_32; 1280 base_hi = 0; 1281 phys_hi = PCI_ADDR_MEM32; 1282 } 1283 1284 /* skip base regs with size of 0 */ 1285 value &= PCI_BASE_M_ADDR_M; 1286 1287 if (value == 0) { 1288 continue; 1289 } 1290 len = ((value ^ (value-1)) + 1) >> 1; 1291 regs[nreg].pci_size_low = 1292 assigned[nasgn].pci_size_low = len; 1293 1294 phys_hi |= (devloc | offset); 1295 if (base & PCI_BASE_PREF_M) { 1296 mres = pmem_res; 1297 phys_hi |= PCI_PREFETCH_B; 1298 } else { 1299 mres = mem_res; 1300 } 1301 regs[nreg].pci_phys_hi = 1302 assigned[nasgn].pci_phys_hi = phys_hi; 1303 assigned[nasgn].pci_phys_hi |= PCI_RELOCAT_B; 1304 assigned[nasgn].pci_phys_mid = base_hi; 1305 type = base & ~PCI_BASE_M_ADDR_M; 1306 base &= PCI_BASE_M_ADDR_M; 1307 1308 if (config_op == CONFIG_INFO) { 1309 /* take out of the resource map of the bus */ 1310 if (*mres && base != 0) { 1311 (void) memlist_remove(mres, 1312 (uint64_t)base, (uint64_t)len); 1313 } else if (*mres) 1314 reprogram = 1; 1315 } else if (*mres && base == 0) { 1316 base = (uint_t)memlist_find(mres, 1317 (uint64_t)len, (uint64_t)0x1000); 1318 if (base != NULL) { 1319 pci_putl(bus, dev, func, offset, 1320 base | type); 1321 base = pci_getl(bus, dev, func, offset); 1322 base &= PCI_BASE_M_ADDR_M; 1323 } 1324 1325 if (base == 0) { 1326 cmn_err(CE_WARN, "failed to program " 1327 "mem space [%d/%d/%d] BAR@0x%x" 1328 " length 0x%x", 1329 bus, dev, func, offset, len); 1330 } else 1331 enable |= PCI_COMM_MAE; 1332 } 1333 assigned[nasgn].pci_phys_low = base; 1334 nreg++, nasgn++; 1335 } 1336 } 1337 switch (header) { 1338 case PCI_HEADER_ZERO: 1339 offset = PCI_CONF_ROM; 1340 break; 1341 case PCI_HEADER_PPB: 1342 offset = PCI_BCNF_ROM; 1343 break; 1344 default: /* including PCI_HEADER_CARDBUS */ 1345 goto done; 1346 } 1347 1348 /* 1349 * Add the expansion rom memory space 1350 * Determine the size of the ROM base reg; don't write reserved bits 1351 * ROM isn't in the PCI memory space. 1352 */ 1353 base = pci_getl(bus, dev, func, offset); 1354 pci_putl(bus, dev, func, offset, PCI_BASE_ROM_ADDR_M); 1355 value = pci_getl(bus, dev, func, offset); 1356 pci_putl(bus, dev, func, offset, base); 1357 if (value & PCI_BASE_ROM_ENABLE) 1358 value &= PCI_BASE_ROM_ADDR_M; 1359 else 1360 value = 0; 1361 1362 if (value != 0) { 1363 regs[nreg].pci_phys_hi = (PCI_ADDR_MEM32 | devloc) + offset; 1364 assigned[nasgn].pci_phys_hi = (PCI_RELOCAT_B | 1365 PCI_ADDR_MEM32 | devloc) + offset; 1366 base &= PCI_BASE_ROM_ADDR_M; 1367 assigned[nasgn].pci_phys_low = base; 1368 len = ((value ^ (value-1)) + 1) >> 1; 1369 regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = len; 1370 nreg++, nasgn++; 1371 /* take it out of the memory resource */ 1372 if (*mem_res && base != 0) 1373 (void) memlist_remove(mem_res, 1374 (uint64_t)base, (uint64_t)len); 1375 } 1376 1377 /* 1378 * The following are ISA resources. There are not part 1379 * of the PCI local bus resources. So don't attempt to 1380 * do resource accounting against PCI. 1381 */ 1382 1383 /* add the three hard-decode, aliased address spaces for VGA */ 1384 if ((baseclass == PCI_CLASS_DISPLAY && subclass == PCI_DISPLAY_VGA) || 1385 (baseclass == PCI_CLASS_NONE && subclass == PCI_NONE_VGA)) { 1386 1387 /* VGA hard decode 0x3b0-0x3bb */ 1388 regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi = 1389 (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc); 1390 regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3b0; 1391 regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0xc; 1392 nreg++, nasgn++; 1393 1394 /* VGA hard decode 0x3c0-0x3df */ 1395 regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi = 1396 (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc); 1397 regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3c0; 1398 regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x20; 1399 nreg++, nasgn++; 1400 1401 /* Video memory */ 1402 regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi = 1403 (PCI_RELOCAT_B | PCI_ADDR_MEM32 | devloc); 1404 regs[nreg].pci_phys_low = 1405 assigned[nasgn].pci_phys_low = 0xa0000; 1406 regs[nreg].pci_size_low = 1407 assigned[nasgn].pci_size_low = 0x20000; 1408 nreg++, nasgn++; 1409 } 1410 1411 /* add the hard-decode, aliased address spaces for 8514 */ 1412 if ((baseclass == PCI_CLASS_DISPLAY) && 1413 (subclass == PCI_DISPLAY_VGA) && 1414 (progclass & PCI_DISPLAY_IF_8514)) { 1415 1416 /* hard decode 0x2e8 */ 1417 regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi = 1418 (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc); 1419 regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2e8; 1420 regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x1; 1421 nreg++, nasgn++; 1422 1423 /* hard decode 0x2ea-0x2ef */ 1424 regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi = 1425 (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc); 1426 regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2ea; 1427 regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x6; 1428 nreg++, nasgn++; 1429 } 1430 1431 done: 1432 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg", 1433 (int *)regs, nreg * sizeof (pci_regspec_t) / sizeof (int)); 1434 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, 1435 "assigned-addresses", 1436 (int *)assigned, nasgn * sizeof (pci_regspec_t) / sizeof (int)); 1437 if (config_op == CONFIG_NEW && enable) { 1438 cmn_err(CE_NOTE, 1439 "!enable PCI device [%d/%d/%d]", bus, dev, func); 1440 cmd_reg |= (enable | PCI_COMM_ME); 1441 } 1442 /* restore device enables */ 1443 pci_putw(bus, dev, func, PCI_CONF_COMM, cmd_reg); 1444 return (reprogram); 1445 } 1446 1447 static void 1448 add_ppb_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func, 1449 int pciex) 1450 { 1451 char *dev_type; 1452 int i; 1453 uint_t val, io_range[2], mem_range[2], pmem_range[2]; 1454 uchar_t secbus = pci_getb(bus, dev, func, PCI_BCNF_SECBUS); 1455 uchar_t subbus = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS); 1456 ASSERT(secbus <= subbus); 1457 1458 /* 1459 * Some BIOSes lie about max pci busses, we allow for 1460 * such mistakes here 1461 */ 1462 if (subbus > pci_bios_nbus) { 1463 pci_bios_nbus = subbus; 1464 alloc_res_array(); 1465 } 1466 1467 ASSERT(pci_bus_res[secbus].dip == NULL); 1468 pci_bus_res[secbus].dip = dip; 1469 pci_bus_res[secbus].par_bus = bus; 1470 1471 dev_type = pciex ? "pciex" : "pci"; 1472 1473 /* setup bus number hierarchy */ 1474 pci_bus_res[secbus].sub_bus = subbus; 1475 if (subbus > pci_bus_res[bus].sub_bus) 1476 pci_bus_res[bus].sub_bus = subbus; 1477 for (i = secbus + 1; i <= subbus; i++) 1478 pci_bus_res[i].par_bus = bus; 1479 1480 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 1481 "device_type", dev_type); 1482 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 1483 "#address-cells", 3); 1484 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 1485 "#size-cells", 2); 1486 1487 /* 1488 * According to PPB spec, the base register should be programmed 1489 * with a value bigger than the limit register when there are 1490 * no resources available. This applies to io, memory, and 1491 * prefetchable memory. 1492 */ 1493 /* io range */ 1494 val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_BASE_LOW); 1495 io_range[0] = ((val & 0xf0) << 8); 1496 val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW); 1497 io_range[1] = ((val & 0xf0) << 8) | 0xFFF; 1498 if (io_range[0] != 0 && io_range[0] < io_range[1]) { 1499 memlist_insert(&pci_bus_res[secbus].io_ports, 1500 (uint64_t)io_range[0], 1501 (uint64_t)(io_range[1] - io_range[0] + 1)); 1502 if (pci_bus_res[bus].io_ports != NULL) { 1503 (void) memlist_remove(&pci_bus_res[bus].io_ports, 1504 (uint64_t)io_range[0], 1505 (uint64_t)(io_range[1] - io_range[0] + 1)); 1506 } 1507 dcmn_err(CE_NOTE, "bus %d io-range: 0x%x-%x", 1508 secbus, io_range[0], io_range[1]); 1509 /* if 32-bit supported, make sure upper bits are not set */ 1510 if ((val & 0xf) == 1 && 1511 pci_getw(bus, dev, func, PCI_BCNF_IO_BASE_HI)) { 1512 cmn_err(CE_NOTE, "unsupported 32-bit IO address on" 1513 " pci-pci bridge [%d/%d/%d]", bus, dev, func); 1514 } 1515 } 1516 1517 /* mem range */ 1518 val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_BASE); 1519 mem_range[0] = ((val & 0xFFF0) << 16); 1520 val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_LIMIT); 1521 mem_range[1] = ((val & 0xFFF0) << 16) | 0xFFFFF; 1522 if (mem_range[0] != 0 && mem_range[0] < mem_range[1]) { 1523 memlist_insert(&pci_bus_res[secbus].mem_space, 1524 (uint64_t)mem_range[0], 1525 (uint64_t)(mem_range[1] - mem_range[0] + 1)); 1526 /* remove from parent resouce list */ 1527 if (pci_bus_res[bus].mem_space != NULL) { 1528 (void) memlist_remove(&pci_bus_res[bus].mem_space, 1529 (uint64_t)mem_range[0], 1530 (uint64_t)(mem_range[1] - mem_range[0] + 1)); 1531 } 1532 dcmn_err(CE_NOTE, "bus %d mem-range: 0x%x-%x", 1533 secbus, mem_range[0], mem_range[1]); 1534 } 1535 1536 /* prefetchable memory range */ 1537 val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_BASE_LOW); 1538 pmem_range[0] = ((val & 0xFFF0) << 16); 1539 val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_LIMIT_LOW); 1540 pmem_range[1] = ((val & 0xFFF0) << 16) | 0xFFFFF; 1541 if (pmem_range[0] != 0 && pmem_range[0] < pmem_range[1]) { 1542 memlist_insert(&pci_bus_res[secbus].pmem_space, 1543 (uint64_t)pmem_range[0], 1544 (uint64_t)(pmem_range[1] - pmem_range[0] + 1)); 1545 if (pci_bus_res[bus].pmem_space != NULL) { 1546 (void) memlist_remove(&pci_bus_res[bus].pmem_space, 1547 (uint64_t)pmem_range[0], 1548 (uint64_t)(pmem_range[1] - pmem_range[0] + 1)); 1549 } 1550 dcmn_err(CE_NOTE, "bus %d pmem-range: 0x%x-%x", 1551 secbus, pmem_range[0], pmem_range[1]); 1552 /* if 64-bit supported, make sure upper bits are not set */ 1553 if ((val & 0xf) == 1 && 1554 pci_getl(bus, dev, func, PCI_BCNF_PF_BASE_HIGH)) { 1555 cmn_err(CE_NOTE, "unsupported 64-bit prefetch memory on" 1556 " pci-pci bridge [%d/%d/%d]", bus, dev, func); 1557 } 1558 } 1559 1560 add_bus_range_prop(secbus); 1561 add_ppb_ranges_prop(secbus); 1562 } 1563 1564 extern const struct pci_class_strings_s class_pci[]; 1565 extern int class_pci_items; 1566 1567 static void 1568 add_model_prop(dev_info_t *dip, uint_t classcode) 1569 { 1570 const char *desc; 1571 int i; 1572 uchar_t baseclass = classcode >> 16; 1573 uchar_t subclass = (classcode >> 8) & 0xff; 1574 uchar_t progclass = classcode & 0xff; 1575 1576 if ((baseclass == PCI_CLASS_MASS) && (subclass == PCI_MASS_IDE)) { 1577 desc = "IDE controller"; 1578 } else { 1579 for (desc = 0, i = 0; i < class_pci_items; i++) { 1580 if ((baseclass == class_pci[i].base_class) && 1581 (subclass == class_pci[i].sub_class) && 1582 (progclass == class_pci[i].prog_class)) { 1583 desc = class_pci[i].actual_desc; 1584 break; 1585 } 1586 } 1587 if (i == class_pci_items) 1588 desc = "Unknown class of pci/pnpbios device"; 1589 } 1590 1591 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model", 1592 (char *)desc); 1593 } 1594 1595 static void 1596 add_bus_range_prop(int bus) 1597 { 1598 int bus_range[2]; 1599 1600 if (pci_bus_res[bus].dip == NULL) 1601 return; 1602 bus_range[0] = bus; 1603 bus_range[1] = pci_bus_res[bus].sub_bus; 1604 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip, 1605 "bus-range", (int *)bus_range, 2); 1606 } 1607 1608 /* 1609 * Add slot-names property for any named pci hot-plug slots 1610 */ 1611 static void 1612 add_bus_slot_names_prop(int bus) 1613 { 1614 char slotprop[256]; 1615 int len; 1616 1617 len = pci_slot_names_prop(bus, slotprop, sizeof (slotprop)); 1618 if (len > 0) { 1619 if (pci_bus_res[bus].dip == NULL) 1620 create_root_bus_dip(bus); 1621 ASSERT(pci_bus_res[bus].dip); 1622 ASSERT((len % sizeof (int)) == 0); 1623 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, 1624 pci_bus_res[bus].dip, "slot-names", 1625 (int *)slotprop, len / sizeof (int)); 1626 } 1627 } 1628 1629 static int 1630 memlist_to_range(ppb_ranges_t *rp, struct memlist *entry, int type) 1631 { 1632 if (entry == NULL) 1633 return (0); 1634 1635 /* assume 32-bit addresses */ 1636 rp->child_high = rp->parent_high = type; 1637 rp->child_mid = rp->parent_mid = 0; 1638 rp->child_low = rp->parent_low = (uint32_t)entry->address; 1639 rp->size_high = 0; 1640 rp->size_low = (uint32_t)entry->size; 1641 return (1); 1642 } 1643 1644 static void 1645 add_ppb_ranges_prop(int bus) 1646 { 1647 int i = 0; 1648 ppb_ranges_t *rp; 1649 1650 rp = kmem_alloc(3 * sizeof (*rp), KM_SLEEP); 1651 1652 i = memlist_to_range(&rp[0], pci_bus_res[bus].io_ports, 1653 PCI_ADDR_IO | PCI_REG_REL_M); 1654 i += memlist_to_range(&rp[i], pci_bus_res[bus].mem_space, 1655 PCI_ADDR_MEM32 | PCI_REG_REL_M); 1656 i += memlist_to_range(&rp[i], pci_bus_res[bus].pmem_space, 1657 PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M); 1658 1659 if (i != 0) 1660 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, 1661 pci_bus_res[bus].dip, "ranges", (int *)rp, 1662 i * sizeof (ppb_ranges_t) / sizeof (int)); 1663 kmem_free(rp, 3 * sizeof (*rp)); 1664 } 1665 1666 static int 1667 memlist_to_spec(struct pci_phys_spec *sp, struct memlist *list, int type) 1668 { 1669 int i = 0; 1670 1671 while (list) { 1672 /* assume 32-bit addresses */ 1673 sp->pci_phys_hi = type; 1674 sp->pci_phys_mid = 0; 1675 sp->pci_phys_low = (uint32_t)list->address; 1676 sp->pci_size_hi = 0; 1677 sp->pci_size_low = (uint32_t)list->size; 1678 1679 list = list->next; 1680 sp++, i++; 1681 } 1682 return (i); 1683 } 1684 1685 static void 1686 add_bus_available_prop(int bus) 1687 { 1688 int i, count; 1689 struct pci_phys_spec *sp; 1690 1691 count = memlist_count(pci_bus_res[bus].io_ports) + 1692 memlist_count(pci_bus_res[bus].mem_space) + 1693 memlist_count(pci_bus_res[bus].pmem_space); 1694 1695 if (count == 0) /* nothing available */ 1696 return; 1697 1698 sp = kmem_alloc(count * sizeof (*sp), KM_SLEEP); 1699 i = memlist_to_spec(&sp[0], pci_bus_res[bus].io_ports, 1700 PCI_ADDR_IO | PCI_REG_REL_M); 1701 i += memlist_to_spec(&sp[i], pci_bus_res[bus].mem_space, 1702 PCI_ADDR_MEM32 | PCI_REG_REL_M); 1703 i += memlist_to_spec(&sp[i], pci_bus_res[bus].pmem_space, 1704 PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M); 1705 ASSERT(i == count); 1706 1707 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip, 1708 "available", (int *)sp, 1709 i * sizeof (struct pci_phys_spec) / sizeof (int)); 1710 kmem_free(sp, count * sizeof (*sp)); 1711 } 1712 1713 static void 1714 alloc_res_array(void) 1715 { 1716 static int array_max = 0; 1717 int old_max; 1718 void *old_res; 1719 1720 if (array_max > pci_bios_nbus + 1) 1721 return; /* array is big enough */ 1722 1723 old_max = array_max; 1724 old_res = pci_bus_res; 1725 1726 if (array_max == 0) 1727 array_max = 16; /* start with a reasonable number */ 1728 1729 while (array_max < pci_bios_nbus + 1) 1730 array_max <<= 1; 1731 pci_bus_res = (struct pci_bus_resource *)kmem_zalloc( 1732 array_max * sizeof (struct pci_bus_resource), KM_SLEEP); 1733 1734 if (old_res) { /* copy content and free old array */ 1735 bcopy(old_res, pci_bus_res, 1736 old_max * sizeof (struct pci_bus_resource)); 1737 kmem_free(old_res, old_max * sizeof (struct pci_bus_resource)); 1738 } 1739 } 1740