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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2015 Joyent, Inc. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/stat.h> 28 #include <sys/sysmacros.h> 29 #include <sys/sunndi.h> 30 #include <sys/pci.h> 31 #include <sys/pci_impl.h> 32 #include <sys/pcie_impl.h> 33 #include <sys/memlist.h> 34 #include <sys/bootconf.h> 35 #include <io/pci/mps_table.h> 36 #include <sys/pci_cfgacc.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 <sys/apic.h> 42 #include <io/pciex/pcie_nvidia.h> 43 #include <sys/hotplug/pci/pciehpc_acpi.h> 44 #include <sys/acpi/acpi.h> 45 #include <sys/acpica.h> 46 #include <sys/iommulib.h> 47 #include <sys/devcache.h> 48 #include <sys/pci_cfgacc_x86.h> 49 50 #define pci_getb (*pci_getb_func) 51 #define pci_getw (*pci_getw_func) 52 #define pci_getl (*pci_getl_func) 53 #define pci_putb (*pci_putb_func) 54 #define pci_putw (*pci_putw_func) 55 #define pci_putl (*pci_putl_func) 56 #define dcmn_err if (pci_boot_debug) cmn_err 57 58 #define CONFIG_INFO 0 59 #define CONFIG_UPDATE 1 60 #define CONFIG_NEW 2 61 #define CONFIG_FIX 3 62 #define COMPAT_BUFSIZE 512 63 64 #define PPB_IO_ALIGNMENT 0x1000 /* 4K aligned */ 65 #define PPB_MEM_ALIGNMENT 0x100000 /* 1M aligned */ 66 /* round down to nearest power of two */ 67 #define P2LE(align) \ 68 { \ 69 int i = 0; \ 70 while (align >>= 1) \ 71 i ++; \ 72 align = 1 << i; \ 73 } \ 74 75 /* for is_vga and list_is_vga_only */ 76 77 enum io_mem { 78 IO, 79 MEM 80 }; 81 82 /* See AMD-8111 Datasheet Rev 3.03, Page 149: */ 83 #define LPC_IO_CONTROL_REG_1 0x40 84 #define AMD8111_ENABLENMI (uint8_t)0x80 85 #define DEVID_AMD8111_LPC 0x7468 86 87 struct pci_fixundo { 88 uint8_t bus; 89 uint8_t dev; 90 uint8_t fn; 91 void (*undofn)(uint8_t, uint8_t, uint8_t); 92 struct pci_fixundo *next; 93 }; 94 95 struct pci_devfunc { 96 struct pci_devfunc *next; 97 dev_info_t *dip; 98 uchar_t dev; 99 uchar_t func; 100 boolean_t reprogram; /* this device needs to be reprogrammed */ 101 }; 102 103 extern int apic_nvidia_io_max; 104 extern int pseudo_isa; 105 extern int pci_bios_maxbus; 106 static uchar_t max_dev_pci = 32; /* PCI standard */ 107 int pci_boot_debug = 0; 108 extern struct memlist *find_bus_res(int, int); 109 static struct pci_fixundo *undolist = NULL; 110 static int num_root_bus = 0; /* count of root buses */ 111 extern volatile int acpi_resource_discovery; 112 extern uint64_t mcfg_mem_base; 113 extern void pci_cfgacc_add_workaround(uint16_t, uchar_t, uchar_t); 114 extern dev_info_t *pcie_get_rc_dip(dev_info_t *); 115 116 /* 117 * Module prototypes 118 */ 119 static void enumerate_bus_devs(uchar_t bus, int config_op); 120 static void create_root_bus_dip(uchar_t bus); 121 static void process_devfunc(uchar_t, uchar_t, uchar_t, uchar_t, 122 ushort_t, int); 123 static void add_compatible(dev_info_t *, ushort_t, ushort_t, 124 ushort_t, ushort_t, uchar_t, uint_t, int); 125 static int add_reg_props(dev_info_t *, uchar_t, uchar_t, uchar_t, int, int); 126 static void add_ppb_props(dev_info_t *, uchar_t, uchar_t, uchar_t, int, 127 ushort_t); 128 static void add_model_prop(dev_info_t *, uint_t); 129 static void add_bus_range_prop(int); 130 static void add_bus_slot_names_prop(int); 131 static void add_ranges_prop(int, int); 132 static void add_bus_available_prop(int); 133 static int get_pci_cap(uchar_t bus, uchar_t dev, uchar_t func, uint8_t cap_id); 134 static void fix_ppb_res(uchar_t, boolean_t); 135 static void alloc_res_array(); 136 static void create_ioapic_node(int bus, int dev, int fn, ushort_t vendorid, 137 ushort_t deviceid); 138 static void pciex_slot_names_prop(dev_info_t *, ushort_t); 139 static void populate_bus_res(uchar_t bus); 140 static void memlist_remove_list(struct memlist **list, 141 struct memlist *remove_list); 142 static void ck804_fix_aer_ptr(dev_info_t *, pcie_req_id_t); 143 144 static void pci_scan_bbn(void); 145 static int pci_unitaddr_cache_valid(void); 146 static int pci_bus_unitaddr(int); 147 static void pci_unitaddr_cache_create(void); 148 149 static int pci_cache_unpack_nvlist(nvf_handle_t, nvlist_t *, char *); 150 static int pci_cache_pack_nvlist(nvf_handle_t, nvlist_t **); 151 static void pci_cache_free_list(nvf_handle_t); 152 153 extern int pci_slot_names_prop(int, char *, int); 154 155 /* set non-zero to force PCI peer-bus renumbering */ 156 int pci_bus_always_renumber = 0; 157 158 /* 159 * used to register ISA resource usage which must not be made 160 * "available" from other PCI node' resource maps 161 */ 162 static struct { 163 struct memlist *io_used; 164 struct memlist *mem_used; 165 } isa_res; 166 167 /* 168 * PCI unit-address cache management 169 */ 170 static nvf_ops_t pci_unitaddr_cache_ops = { 171 "/etc/devices/pci_unitaddr_persistent", /* path to cache */ 172 pci_cache_unpack_nvlist, /* read in nvlist form */ 173 pci_cache_pack_nvlist, /* convert to nvlist form */ 174 pci_cache_free_list, /* free data list */ 175 NULL /* write complete callback */ 176 }; 177 178 typedef struct { 179 list_node_t pua_nodes; 180 int pua_index; 181 int pua_addr; 182 } pua_node_t; 183 184 nvf_handle_t puafd_handle; 185 int pua_cache_valid = 0; 186 187 188 /*ARGSUSED*/ 189 static ACPI_STATUS 190 pci_process_acpi_device(ACPI_HANDLE hdl, UINT32 level, void *ctx, void **rv) 191 { 192 ACPI_BUFFER rb; 193 ACPI_OBJECT ro; 194 ACPI_DEVICE_INFO *adi; 195 int busnum; 196 197 /* 198 * Use AcpiGetObjectInfo() to find the device _HID 199 * If not a PCI root-bus, ignore this device and continue 200 * the walk 201 */ 202 if (ACPI_FAILURE(AcpiGetObjectInfo(hdl, &adi))) 203 return (AE_OK); 204 205 if (!(adi->Valid & ACPI_VALID_HID)) { 206 AcpiOsFree(adi); 207 return (AE_OK); 208 } 209 210 if (strncmp(adi->HardwareId.String, PCI_ROOT_HID_STRING, 211 sizeof (PCI_ROOT_HID_STRING)) && 212 strncmp(adi->HardwareId.String, PCI_EXPRESS_ROOT_HID_STRING, 213 sizeof (PCI_EXPRESS_ROOT_HID_STRING))) { 214 AcpiOsFree(adi); 215 return (AE_OK); 216 } 217 218 AcpiOsFree(adi); 219 220 /* 221 * XXX: ancient Big Bear broken _BBN will result in two 222 * bus 0 _BBNs being found, so we need to handle duplicate 223 * bus 0 gracefully. However, broken _BBN does not 224 * hide a childless root-bridge so no need to work-around it 225 * here 226 */ 227 rb.Pointer = &ro; 228 rb.Length = sizeof (ro); 229 if (ACPI_SUCCESS(AcpiEvaluateObjectTyped(hdl, "_BBN", 230 NULL, &rb, ACPI_TYPE_INTEGER))) { 231 busnum = ro.Integer.Value; 232 233 /* 234 * Ignore invalid _BBN return values here (rather 235 * than panic) and emit a warning; something else 236 * may suffer failure as a result of the broken BIOS. 237 */ 238 if ((busnum < 0) || (busnum > pci_bios_maxbus)) { 239 dcmn_err(CE_NOTE, 240 "pci_process_acpi_device: invalid _BBN 0x%x\n", 241 busnum); 242 return (AE_CTRL_DEPTH); 243 } 244 245 /* PCI with valid _BBN */ 246 if (pci_bus_res[busnum].par_bus == (uchar_t)-1 && 247 pci_bus_res[busnum].dip == NULL) 248 create_root_bus_dip((uchar_t)busnum); 249 return (AE_CTRL_DEPTH); 250 } 251 252 /* PCI and no _BBN, continue walk */ 253 return (AE_OK); 254 } 255 256 /* 257 * Scan the ACPI namespace for all top-level instances of _BBN 258 * in order to discover childless root-bridges (which enumeration 259 * may not find; root-bridges are inferred by the existence of 260 * children). This scan should find all root-bridges that have 261 * been enumerated, and any childless root-bridges not enumerated. 262 * Root-bridge for bus 0 may not have a _BBN object. 263 */ 264 static void 265 pci_scan_bbn() 266 { 267 void *rv; 268 269 (void) AcpiGetDevices(NULL, pci_process_acpi_device, NULL, &rv); 270 } 271 272 static void 273 pci_unitaddr_cache_init(void) 274 { 275 276 puafd_handle = nvf_register_file(&pci_unitaddr_cache_ops); 277 ASSERT(puafd_handle); 278 279 list_create(nvf_list(puafd_handle), sizeof (pua_node_t), 280 offsetof(pua_node_t, pua_nodes)); 281 282 rw_enter(nvf_lock(puafd_handle), RW_WRITER); 283 (void) nvf_read_file(puafd_handle); 284 rw_exit(nvf_lock(puafd_handle)); 285 } 286 287 /* 288 * Format of /etc/devices/pci_unitaddr_persistent: 289 * 290 * The persistent record of unit-address assignments contains 291 * a list of name/value pairs, where name is a string representation 292 * of the "index value" of the PCI root-bus and the value is 293 * the assigned unit-address. 294 * 295 * The "index value" is simply the zero-based index of the PCI 296 * root-buses ordered by physical bus number; first PCI bus is 0, 297 * second is 1, and so on. 298 */ 299 300 /*ARGSUSED*/ 301 static int 302 pci_cache_unpack_nvlist(nvf_handle_t hdl, nvlist_t *nvl, char *name) 303 { 304 long index; 305 int32_t value; 306 nvpair_t *np; 307 pua_node_t *node; 308 309 np = NULL; 310 while ((np = nvlist_next_nvpair(nvl, np)) != NULL) { 311 /* name of nvpair is index value */ 312 if (ddi_strtol(nvpair_name(np), NULL, 10, &index) != 0) 313 continue; 314 315 if (nvpair_value_int32(np, &value) != 0) 316 continue; 317 318 node = kmem_zalloc(sizeof (pua_node_t), KM_SLEEP); 319 node->pua_index = index; 320 node->pua_addr = value; 321 list_insert_tail(nvf_list(hdl), node); 322 } 323 324 pua_cache_valid = 1; 325 return (DDI_SUCCESS); 326 } 327 328 static int 329 pci_cache_pack_nvlist(nvf_handle_t hdl, nvlist_t **ret_nvl) 330 { 331 int rval; 332 nvlist_t *nvl, *sub_nvl; 333 list_t *listp; 334 pua_node_t *pua; 335 char buf[13]; 336 337 ASSERT(RW_WRITE_HELD(nvf_lock(hdl))); 338 339 rval = nvlist_alloc(&nvl, NV_UNIQUE_NAME, KM_SLEEP); 340 if (rval != DDI_SUCCESS) { 341 nvf_error("%s: nvlist alloc error %d\n", 342 nvf_cache_name(hdl), rval); 343 return (DDI_FAILURE); 344 } 345 346 sub_nvl = NULL; 347 rval = nvlist_alloc(&sub_nvl, NV_UNIQUE_NAME, KM_SLEEP); 348 if (rval != DDI_SUCCESS) 349 goto error; 350 351 listp = nvf_list(hdl); 352 for (pua = list_head(listp); pua != NULL; 353 pua = list_next(listp, pua)) { 354 (void) snprintf(buf, sizeof (buf), "%d", pua->pua_index); 355 rval = nvlist_add_int32(sub_nvl, buf, pua->pua_addr); 356 if (rval != DDI_SUCCESS) 357 goto error; 358 } 359 360 rval = nvlist_add_nvlist(nvl, "table", sub_nvl); 361 if (rval != DDI_SUCCESS) 362 goto error; 363 nvlist_free(sub_nvl); 364 365 *ret_nvl = nvl; 366 return (DDI_SUCCESS); 367 368 error: 369 if (sub_nvl) 370 nvlist_free(sub_nvl); 371 ASSERT(nvl); 372 nvlist_free(nvl); 373 *ret_nvl = NULL; 374 return (DDI_FAILURE); 375 } 376 377 static void 378 pci_cache_free_list(nvf_handle_t hdl) 379 { 380 list_t *listp; 381 pua_node_t *pua; 382 383 ASSERT(RW_WRITE_HELD(nvf_lock(hdl))); 384 385 listp = nvf_list(hdl); 386 for (pua = list_head(listp); pua != NULL; 387 pua = list_next(listp, pua)) { 388 list_remove(listp, pua); 389 kmem_free(pua, sizeof (pua_node_t)); 390 } 391 } 392 393 394 static int 395 pci_unitaddr_cache_valid(void) 396 { 397 398 /* read only, no need for rw lock */ 399 return (pua_cache_valid); 400 } 401 402 403 static int 404 pci_bus_unitaddr(int index) 405 { 406 pua_node_t *pua; 407 list_t *listp; 408 int addr; 409 410 rw_enter(nvf_lock(puafd_handle), RW_READER); 411 412 addr = -1; /* default return if no match */ 413 listp = nvf_list(puafd_handle); 414 for (pua = list_head(listp); pua != NULL; 415 pua = list_next(listp, pua)) { 416 if (pua->pua_index == index) { 417 addr = pua->pua_addr; 418 break; 419 } 420 } 421 422 rw_exit(nvf_lock(puafd_handle)); 423 return (addr); 424 } 425 426 static void 427 pci_unitaddr_cache_create(void) 428 { 429 int i, index; 430 pua_node_t *node; 431 list_t *listp; 432 433 rw_enter(nvf_lock(puafd_handle), RW_WRITER); 434 435 index = 0; 436 listp = nvf_list(puafd_handle); 437 for (i = 0; i <= pci_bios_maxbus; i++) { 438 /* skip non-root (peer) PCI busses */ 439 if ((pci_bus_res[i].par_bus != (uchar_t)-1) || 440 (pci_bus_res[i].dip == NULL)) 441 continue; 442 node = kmem_zalloc(sizeof (pua_node_t), KM_SLEEP); 443 node->pua_index = index++; 444 node->pua_addr = pci_bus_res[i].root_addr; 445 list_insert_tail(listp, node); 446 } 447 448 (void) nvf_mark_dirty(puafd_handle); 449 rw_exit(nvf_lock(puafd_handle)); 450 nvf_wake_daemon(); 451 } 452 453 454 /* 455 * Enumerate all PCI devices 456 */ 457 void 458 pci_setup_tree(void) 459 { 460 uint_t i, root_bus_addr = 0; 461 462 alloc_res_array(); 463 for (i = 0; i <= pci_bios_maxbus; i++) { 464 pci_bus_res[i].par_bus = (uchar_t)-1; 465 pci_bus_res[i].root_addr = (uchar_t)-1; 466 pci_bus_res[i].sub_bus = i; 467 } 468 469 pci_bus_res[0].root_addr = root_bus_addr++; 470 create_root_bus_dip(0); 471 enumerate_bus_devs(0, CONFIG_INFO); 472 473 /* 474 * Now enumerate peer busses 475 * 476 * We loop till pci_bios_maxbus. On most systems, there is 477 * one more bus at the high end, which implements the ISA 478 * compatibility bus. We don't care about that. 479 * 480 * Note: In the old (bootconf) enumeration, the peer bus 481 * address did not use the bus number, and there were 482 * too many peer busses created. The root_bus_addr is 483 * used to maintain the old peer bus address assignment. 484 * However, we stop enumerating phantom peers with no 485 * device below. 486 */ 487 for (i = 1; i <= pci_bios_maxbus; i++) { 488 if (pci_bus_res[i].dip == NULL) { 489 pci_bus_res[i].root_addr = root_bus_addr++; 490 } 491 enumerate_bus_devs(i, CONFIG_INFO); 492 493 /* add slot-names property for named pci hot-plug slots */ 494 add_bus_slot_names_prop(i); 495 } 496 } 497 498 /* 499 * >0 = present, 0 = not present, <0 = error 500 */ 501 static int 502 pci_bbn_present(int bus) 503 { 504 ACPI_HANDLE hdl; 505 int rv; 506 507 /* no dip means no _BBN */ 508 if (pci_bus_res[bus].dip == NULL) 509 return (0); 510 511 rv = -1; /* default return value in case of error below */ 512 if (ACPI_SUCCESS(acpica_get_handle(pci_bus_res[bus].dip, &hdl))) { 513 switch (AcpiEvaluateObject(hdl, "_BBN", NULL, NULL)) { 514 case AE_OK: 515 rv = 1; 516 break; 517 case AE_NOT_FOUND: 518 rv = 0; 519 break; 520 default: 521 break; 522 } 523 } 524 525 return (rv); 526 } 527 528 /* 529 * Return non-zero if any PCI bus in the system has an associated 530 * _BBN object, 0 otherwise. 531 */ 532 static int 533 pci_roots_have_bbn(void) 534 { 535 int i; 536 537 /* 538 * Scan the PCI busses and look for at least 1 _BBN 539 */ 540 for (i = 0; i <= pci_bios_maxbus; i++) { 541 /* skip non-root (peer) PCI busses */ 542 if (pci_bus_res[i].par_bus != (uchar_t)-1) 543 continue; 544 545 if (pci_bbn_present(i) > 0) 546 return (1); 547 } 548 return (0); 549 550 } 551 552 /* 553 * return non-zero if the machine is one on which we renumber 554 * the internal pci unit-addresses 555 */ 556 static int 557 pci_bus_renumber() 558 { 559 ACPI_TABLE_HEADER *fadt; 560 561 if (pci_bus_always_renumber) 562 return (1); 563 564 /* get the FADT */ 565 if (AcpiGetTable(ACPI_SIG_FADT, 1, (ACPI_TABLE_HEADER **)&fadt) != 566 AE_OK) 567 return (0); 568 569 /* compare OEM Table ID to "SUNm31" */ 570 if (strncmp("SUNm31", fadt->OemId, 6)) 571 return (0); 572 else 573 return (1); 574 } 575 576 /* 577 * Initial enumeration of the physical PCI bus hierarchy can 578 * leave 'gaps' in the order of peer PCI bus unit-addresses. 579 * Systems with more than one peer PCI bus *must* have an ACPI 580 * _BBN object associated with each peer bus; use the presence 581 * of this object to remove gaps in the numbering of the peer 582 * PCI bus unit-addresses - only peer busses with an associated 583 * _BBN are counted. 584 */ 585 static void 586 pci_renumber_root_busses(void) 587 { 588 int pci_regs[] = {0, 0, 0}; 589 int i, root_addr = 0; 590 591 /* 592 * Currently, we only enable the re-numbering on specific 593 * Sun machines; this is a work-around for the more complicated 594 * issue of upgrade changing physical device paths 595 */ 596 if (!pci_bus_renumber()) 597 return; 598 599 /* 600 * If we find no _BBN objects at all, we either don't need 601 * to do anything or can't do anything anyway 602 */ 603 if (!pci_roots_have_bbn()) 604 return; 605 606 for (i = 0; i <= pci_bios_maxbus; i++) { 607 /* skip non-root (peer) PCI busses */ 608 if (pci_bus_res[i].par_bus != (uchar_t)-1) 609 continue; 610 611 if (pci_bbn_present(i) < 1) { 612 pci_bus_res[i].root_addr = (uchar_t)-1; 613 continue; 614 } 615 616 ASSERT(pci_bus_res[i].dip != NULL); 617 if (pci_bus_res[i].root_addr != root_addr) { 618 /* update reg property for node */ 619 pci_bus_res[i].root_addr = root_addr; 620 pci_regs[0] = pci_bus_res[i].root_addr; 621 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, 622 pci_bus_res[i].dip, "reg", (int *)pci_regs, 3); 623 } 624 root_addr++; 625 } 626 } 627 628 void 629 pci_register_isa_resources(int type, uint32_t base, uint32_t size) 630 { 631 (void) memlist_insert( 632 (type == 1) ? &isa_res.io_used : &isa_res.mem_used, 633 base, size); 634 } 635 636 /* 637 * Remove the resources which are already used by devices under a subtractive 638 * bridge from the bus's resources lists, because they're not available, and 639 * shouldn't be allocated to other buses. This is necessary because tracking 640 * resources for subtractive bridges is not complete. (Subtractive bridges only 641 * track some of their claimed resources, not "the rest of the address space" as 642 * they should, so that allocation to peer non-subtractive PPBs is easier. We 643 * need a fully-capable global resource allocator). 644 */ 645 static void 646 remove_subtractive_res() 647 { 648 int i, j; 649 struct memlist *list; 650 651 for (i = 0; i <= pci_bios_maxbus; i++) { 652 if (pci_bus_res[i].subtractive) { 653 /* remove used io ports */ 654 list = pci_bus_res[i].io_used; 655 while (list) { 656 for (j = 0; j <= pci_bios_maxbus; j++) 657 (void) memlist_remove( 658 &pci_bus_res[j].io_avail, 659 list->ml_address, list->ml_size); 660 list = list->ml_next; 661 } 662 /* remove used mem resource */ 663 list = pci_bus_res[i].mem_used; 664 while (list) { 665 for (j = 0; j <= pci_bios_maxbus; j++) { 666 (void) memlist_remove( 667 &pci_bus_res[j].mem_avail, 668 list->ml_address, list->ml_size); 669 (void) memlist_remove( 670 &pci_bus_res[j].pmem_avail, 671 list->ml_address, list->ml_size); 672 } 673 list = list->ml_next; 674 } 675 /* remove used prefetchable mem resource */ 676 list = pci_bus_res[i].pmem_used; 677 while (list) { 678 for (j = 0; j <= pci_bios_maxbus; j++) { 679 (void) memlist_remove( 680 &pci_bus_res[j].pmem_avail, 681 list->ml_address, list->ml_size); 682 (void) memlist_remove( 683 &pci_bus_res[j].mem_avail, 684 list->ml_address, list->ml_size); 685 } 686 list = list->ml_next; 687 } 688 } 689 } 690 } 691 692 /* 693 * Set up (or complete the setup of) the bus_avail resource list 694 */ 695 static void 696 setup_bus_res(int bus) 697 { 698 uchar_t par_bus; 699 700 if (pci_bus_res[bus].dip == NULL) /* unused bus */ 701 return; 702 703 /* 704 * Set up bus_avail if not already filled in by populate_bus_res() 705 */ 706 if (pci_bus_res[bus].bus_avail == NULL) { 707 ASSERT(pci_bus_res[bus].sub_bus >= bus); 708 memlist_insert(&pci_bus_res[bus].bus_avail, bus, 709 pci_bus_res[bus].sub_bus - bus + 1); 710 } 711 712 ASSERT(pci_bus_res[bus].bus_avail != NULL); 713 714 /* 715 * Remove resources from parent bus node if this is not a 716 * root bus. 717 */ 718 par_bus = pci_bus_res[bus].par_bus; 719 if (par_bus != (uchar_t)-1) { 720 ASSERT(pci_bus_res[par_bus].bus_avail != NULL); 721 memlist_remove_list(&pci_bus_res[par_bus].bus_avail, 722 pci_bus_res[bus].bus_avail); 723 } 724 725 /* remove self from bus_avail */; 726 (void) memlist_remove(&pci_bus_res[bus].bus_avail, bus, 1); 727 } 728 729 static uint64_t 730 get_parbus_io_res(uchar_t parbus, uchar_t bus, uint64_t size, uint64_t align) 731 { 732 uint64_t addr = 0; 733 uchar_t res_bus; 734 735 /* 736 * Skip root(peer) buses in multiple-root-bus systems when 737 * ACPI resource discovery was not successfully done. 738 */ 739 if ((pci_bus_res[parbus].par_bus == (uchar_t)-1) && 740 (num_root_bus > 1) && (acpi_resource_discovery <= 0)) 741 return (0); 742 743 res_bus = parbus; 744 while (pci_bus_res[res_bus].subtractive) { 745 if (pci_bus_res[res_bus].io_avail) 746 break; 747 res_bus = pci_bus_res[res_bus].par_bus; 748 if (res_bus == (uchar_t)-1) 749 break; /* root bus already */ 750 } 751 752 if (pci_bus_res[res_bus].io_avail) { 753 addr = memlist_find(&pci_bus_res[res_bus].io_avail, 754 size, align); 755 if (addr) { 756 memlist_insert(&pci_bus_res[res_bus].io_used, 757 addr, size); 758 759 /* free the old resource */ 760 memlist_free_all(&pci_bus_res[bus].io_avail); 761 memlist_free_all(&pci_bus_res[bus].io_used); 762 763 /* add the new resource */ 764 memlist_insert(&pci_bus_res[bus].io_avail, addr, size); 765 } 766 } 767 768 return (addr); 769 } 770 771 static uint64_t 772 get_parbus_mem_res(uchar_t parbus, uchar_t bus, uint64_t size, uint64_t align) 773 { 774 uint64_t addr = 0; 775 uchar_t res_bus; 776 777 /* 778 * Skip root(peer) buses in multiple-root-bus systems when 779 * ACPI resource discovery was not successfully done. 780 */ 781 if ((pci_bus_res[parbus].par_bus == (uchar_t)-1) && 782 (num_root_bus > 1) && (acpi_resource_discovery <= 0)) 783 return (0); 784 785 res_bus = parbus; 786 while (pci_bus_res[res_bus].subtractive) { 787 if (pci_bus_res[res_bus].mem_avail) 788 break; 789 res_bus = pci_bus_res[res_bus].par_bus; 790 if (res_bus == (uchar_t)-1) 791 break; /* root bus already */ 792 } 793 794 if (pci_bus_res[res_bus].mem_avail) { 795 addr = memlist_find(&pci_bus_res[res_bus].mem_avail, 796 size, align); 797 if (addr) { 798 memlist_insert(&pci_bus_res[res_bus].mem_used, 799 addr, size); 800 (void) memlist_remove(&pci_bus_res[res_bus].pmem_avail, 801 addr, size); 802 803 /* free the old resource */ 804 memlist_free_all(&pci_bus_res[bus].mem_avail); 805 memlist_free_all(&pci_bus_res[bus].mem_used); 806 807 /* add the new resource */ 808 memlist_insert(&pci_bus_res[bus].mem_avail, addr, size); 809 } 810 } 811 812 return (addr); 813 } 814 815 /* 816 * given a cap_id, return its cap_id location in config space 817 */ 818 static int 819 get_pci_cap(uchar_t bus, uchar_t dev, uchar_t func, uint8_t cap_id) 820 { 821 uint8_t curcap, cap_id_loc; 822 uint16_t status; 823 int location = -1; 824 825 /* 826 * Need to check the Status register for ECP support first. 827 * Also please note that for type 1 devices, the 828 * offset could change. Should support type 1 next. 829 */ 830 status = pci_getw(bus, dev, func, PCI_CONF_STAT); 831 if (!(status & PCI_STAT_CAP)) { 832 return (-1); 833 } 834 cap_id_loc = pci_getb(bus, dev, func, PCI_CONF_CAP_PTR); 835 836 /* Walk the list of capabilities */ 837 while (cap_id_loc && cap_id_loc != (uint8_t)-1) { 838 curcap = pci_getb(bus, dev, func, cap_id_loc); 839 840 if (curcap == cap_id) { 841 location = cap_id_loc; 842 break; 843 } 844 cap_id_loc = pci_getb(bus, dev, func, cap_id_loc + 1); 845 } 846 return (location); 847 } 848 849 /* 850 * Does this resource element live in the legacy VGA range? 851 */ 852 853 int 854 is_vga(struct memlist *elem, enum io_mem io) 855 { 856 857 if (io == IO) { 858 if ((elem->ml_address == 0x3b0 && elem->ml_size == 0xc) || 859 (elem->ml_address == 0x3c0 && elem->ml_size == 0x20)) 860 return (1); 861 } else { 862 if (elem->ml_address == 0xa0000 && elem->ml_size == 0x20000) 863 return (1); 864 } 865 return (0); 866 } 867 868 /* 869 * Does this entire resource list consist only of legacy VGA resources? 870 */ 871 872 int 873 list_is_vga_only(struct memlist *l, enum io_mem io) 874 { 875 do { 876 if (!is_vga(l, io)) 877 return (0); 878 } while ((l = l->ml_next) != NULL); 879 return (1); 880 } 881 882 /* 883 * Assign valid resources to unconfigured pci(e) bridges. We are trying 884 * to reprogram the bridge when its 885 * i) SECBUS == SUBBUS || 886 * ii) IOBASE > IOLIM || 887 * iii) MEMBASE > MEMLIM 888 * This must be done after one full pass through the PCI tree to collect 889 * all BIOS-configured resources, so that we know what resources are 890 * free and available to assign to the unconfigured PPBs. 891 */ 892 static void 893 fix_ppb_res(uchar_t secbus, boolean_t prog_sub) 894 { 895 uchar_t bus, dev, func; 896 uchar_t parbus, subbus; 897 uint_t io_base, io_limit, mem_base, mem_limit; 898 uint_t io_size, mem_size, io_align, mem_align; 899 uint64_t addr = 0; 900 int *regp = NULL; 901 uint_t reglen; 902 int rv, cap_ptr, physhi; 903 dev_info_t *dip; 904 uint16_t cmd_reg; 905 struct memlist *list, *scratch_list; 906 907 /* skip root (peer) PCI busses */ 908 if (pci_bus_res[secbus].par_bus == (uchar_t)-1) 909 return; 910 911 /* skip subtractive PPB when prog_sub is not TRUE */ 912 if (pci_bus_res[secbus].subtractive && !prog_sub) 913 return; 914 915 /* some entries may be empty due to discontiguous bus numbering */ 916 dip = pci_bus_res[secbus].dip; 917 if (dip == NULL) 918 return; 919 920 rv = ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, 921 "reg", ®p, ®len); 922 if (rv != DDI_PROP_SUCCESS || reglen == 0) 923 return; 924 physhi = regp[0]; 925 ddi_prop_free(regp); 926 927 func = (uchar_t)PCI_REG_FUNC_G(physhi); 928 dev = (uchar_t)PCI_REG_DEV_G(physhi); 929 bus = (uchar_t)PCI_REG_BUS_G(physhi); 930 931 /* 932 * If pcie bridge, check to see if link is enabled 933 */ 934 cap_ptr = get_pci_cap(bus, dev, func, PCI_CAP_ID_PCI_E); 935 if (cap_ptr != -1) { 936 cmd_reg = pci_getw(bus, dev, func, 937 (uint16_t)cap_ptr + PCIE_LINKCTL); 938 if (cmd_reg & PCIE_LINKCTL_LINK_DISABLE) { 939 dcmn_err(CE_NOTE, 940 "!fix_ppb_res: ppb[%x/%x/%x] link is disabled.\n", 941 bus, dev, func); 942 return; 943 } 944 } 945 946 subbus = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS); 947 parbus = pci_bus_res[secbus].par_bus; 948 ASSERT(parbus == bus); 949 cmd_reg = pci_getw(bus, dev, func, PCI_CONF_COMM); 950 951 /* 952 * If we have a Cardbus bridge, but no bus space 953 */ 954 if (pci_bus_res[secbus].num_cbb != 0 && 955 pci_bus_res[secbus].bus_avail == NULL) { 956 uchar_t range; 957 958 /* normally there are 2 buses under a cardbus bridge */ 959 range = pci_bus_res[secbus].num_cbb * 2; 960 961 /* 962 * Try to find and allocate a bus-range starting at subbus+1 963 * from the parent of the PPB. 964 */ 965 for (; range != 0; range--) { 966 if (memlist_find_with_startaddr( 967 &pci_bus_res[parbus].bus_avail, 968 subbus + 1, range, 1) != NULL) 969 break; /* find bus range resource at parent */ 970 } 971 if (range != 0) { 972 memlist_insert(&pci_bus_res[secbus].bus_avail, 973 subbus + 1, range); 974 subbus = subbus + range; 975 pci_bus_res[secbus].sub_bus = subbus; 976 pci_putb(bus, dev, func, PCI_BCNF_SUBBUS, subbus); 977 add_bus_range_prop(secbus); 978 979 cmn_err(CE_NOTE, "!reprogram bus-range on ppb" 980 "[%x/%x/%x]: %x ~ %x\n", bus, dev, func, 981 secbus, subbus); 982 } 983 } 984 985 /* 986 * Calculate required IO size and alignment 987 * If bus io_size is zero, we are going to assign 512 bytes per bus, 988 * otherwise, we'll choose the maximum value of such calculation and 989 * bus io_size. The size needs to be 4K aligned. 990 * 991 * We calculate alignment as the largest power of two less than the 992 * the sum of all children's IO size requirements, because this will 993 * align to the size of the largest child request within that size 994 * (which is always a power of two). 995 */ 996 io_size = (subbus - secbus + 1) * 0x200; 997 if (io_size < pci_bus_res[secbus].io_size) 998 io_size = pci_bus_res[secbus].io_size; 999 io_size = P2ROUNDUP(io_size, PPB_IO_ALIGNMENT); 1000 io_align = io_size; 1001 P2LE(io_align); 1002 1003 /* 1004 * Calculate required MEM size and alignment 1005 * If bus mem_size is zero, we are going to assign 1M bytes per bus, 1006 * otherwise, we'll choose the maximum value of such calculation and 1007 * bus mem_size. The size needs to be 1M aligned. 1008 * 1009 * For the alignment, refer to the I/O comment above. 1010 */ 1011 mem_size = (subbus - secbus + 1) * PPB_MEM_ALIGNMENT; 1012 if (mem_size < pci_bus_res[secbus].mem_size) { 1013 mem_size = pci_bus_res[secbus].mem_size; 1014 mem_size = P2ROUNDUP(mem_size, PPB_MEM_ALIGNMENT); 1015 } 1016 mem_align = mem_size; 1017 P2LE(mem_align); 1018 1019 /* Subtractive bridge */ 1020 if (pci_bus_res[secbus].subtractive && prog_sub) { 1021 /* 1022 * We program an arbitrary amount of I/O and memory resource 1023 * for the subtractive bridge so that child dynamic-resource- 1024 * allocating devices (such as Cardbus bridges) have a chance 1025 * of success. Until we have full-tree resource rebalancing, 1026 * dynamic resource allocation (thru busra) only looks at the 1027 * parent bridge, so all PPBs must have some allocatable 1028 * resource. For non-subtractive bridges, the resources come 1029 * from the base/limit register "windows", but subtractive 1030 * bridges often don't program those (since they don't need to). 1031 * If we put all the remaining resources on the subtractive 1032 * bridge, then peer non-subtractive bridges can't allocate 1033 * more space (even though this is probably most correct). 1034 * If we put the resources only on the parent, then allocations 1035 * from children of subtractive bridges will fail without 1036 * special-case code for bypassing the subtractive bridge. 1037 * This solution is the middle-ground temporary solution until 1038 * we have fully-capable resource allocation. 1039 */ 1040 1041 /* 1042 * Add an arbitrary I/O resource to the subtractive PPB 1043 */ 1044 if (pci_bus_res[secbus].io_avail == NULL) { 1045 addr = get_parbus_io_res(parbus, secbus, io_size, 1046 io_align); 1047 if (addr) { 1048 add_ranges_prop(secbus, 1); 1049 pci_bus_res[secbus].io_reprogram = 1050 pci_bus_res[parbus].io_reprogram; 1051 1052 cmn_err(CE_NOTE, "!add io-range on subtractive" 1053 " ppb[%x/%x/%x]: 0x%x ~ 0x%x\n", 1054 bus, dev, func, (uint32_t)addr, 1055 (uint32_t)addr + io_size - 1); 1056 } 1057 } 1058 /* 1059 * Add an arbitrary memory resource to the subtractive PPB 1060 */ 1061 if (pci_bus_res[secbus].mem_avail == NULL) { 1062 addr = get_parbus_mem_res(parbus, secbus, mem_size, 1063 mem_align); 1064 if (addr) { 1065 add_ranges_prop(secbus, 1); 1066 pci_bus_res[secbus].mem_reprogram = 1067 pci_bus_res[parbus].mem_reprogram; 1068 1069 cmn_err(CE_NOTE, "!add mem-range on " 1070 "subtractive ppb[%x/%x/%x]: 0x%x ~ 0x%x\n", 1071 bus, dev, func, (uint32_t)addr, 1072 (uint32_t)addr + mem_size - 1); 1073 } 1074 } 1075 1076 goto cmd_enable; 1077 } 1078 1079 /* 1080 * Check to see if we need to reprogram I/O space, either because the 1081 * parent bus needed reprogramming and so do we, or because I/O space is 1082 * disabled in base/limit or command register. 1083 */ 1084 io_base = pci_getb(bus, dev, func, PCI_BCNF_IO_BASE_LOW); 1085 io_limit = pci_getb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW); 1086 io_base = (io_base & 0xf0) << 8; 1087 io_limit = ((io_limit & 0xf0) << 8) | 0xfff; 1088 1089 /* Form list of all resources passed (avail + used) */ 1090 scratch_list = memlist_dup(pci_bus_res[secbus].io_avail); 1091 memlist_merge(&pci_bus_res[secbus].io_used, &scratch_list); 1092 1093 if ((pci_bus_res[parbus].io_reprogram || 1094 (io_base > io_limit) || 1095 (!(cmd_reg & PCI_COMM_IO))) && 1096 !list_is_vga_only(scratch_list, IO)) { 1097 if (pci_bus_res[secbus].io_used) { 1098 memlist_subsume(&pci_bus_res[secbus].io_used, 1099 &pci_bus_res[secbus].io_avail); 1100 } 1101 if (pci_bus_res[secbus].io_avail && 1102 (!pci_bus_res[parbus].io_reprogram) && 1103 (!pci_bus_res[parbus].subtractive)) { 1104 /* rechoose old io ports info */ 1105 list = pci_bus_res[secbus].io_avail; 1106 io_base = 0; 1107 do { 1108 if (is_vga(list, IO)) 1109 continue; 1110 if (!io_base) { 1111 io_base = (uint_t)list->ml_address; 1112 io_limit = (uint_t)list->ml_address + 1113 list->ml_size - 1; 1114 io_base = 1115 P2ALIGN(io_base, PPB_IO_ALIGNMENT); 1116 } else { 1117 if (list->ml_address + list->ml_size > 1118 io_limit) { 1119 io_limit = (uint_t) 1120 (list->ml_address + 1121 list->ml_size - 1); 1122 } 1123 } 1124 } while ((list = list->ml_next) != NULL); 1125 /* 4K aligned */ 1126 io_limit = P2ROUNDUP(io_limit, PPB_IO_ALIGNMENT) - 1; 1127 io_size = io_limit - io_base + 1; 1128 ASSERT(io_base <= io_limit); 1129 memlist_free_all(&pci_bus_res[secbus].io_avail); 1130 memlist_insert(&pci_bus_res[secbus].io_avail, 1131 io_base, io_size); 1132 memlist_insert(&pci_bus_res[parbus].io_used, 1133 io_base, io_size); 1134 (void) memlist_remove(&pci_bus_res[parbus].io_avail, 1135 io_base, io_size); 1136 pci_bus_res[secbus].io_reprogram = B_TRUE; 1137 } else { 1138 /* get new io ports from parent bus */ 1139 addr = get_parbus_io_res(parbus, secbus, io_size, 1140 io_align); 1141 if (addr) { 1142 io_base = addr; 1143 io_limit = addr + io_size - 1; 1144 pci_bus_res[secbus].io_reprogram = B_TRUE; 1145 } 1146 } 1147 if (pci_bus_res[secbus].io_reprogram) { 1148 /* reprogram PPB regs */ 1149 pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_LOW, 1150 (uchar_t)((io_base>>8) & 0xf0)); 1151 pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW, 1152 (uchar_t)((io_limit>>8) & 0xf0)); 1153 pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_HI, 0); 1154 pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_HI, 0); 1155 add_ranges_prop(secbus, 1); 1156 1157 cmn_err(CE_NOTE, "!reprogram io-range on" 1158 " ppb[%x/%x/%x]: 0x%x ~ 0x%x\n", 1159 bus, dev, func, io_base, io_limit); 1160 } 1161 } 1162 memlist_free_all(&scratch_list); 1163 1164 /* 1165 * Check memory space as we did I/O space. 1166 */ 1167 mem_base = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_BASE); 1168 mem_base = (mem_base & 0xfff0) << 16; 1169 mem_limit = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_LIMIT); 1170 mem_limit = ((mem_limit & 0xfff0) << 16) | 0xfffff; 1171 1172 scratch_list = memlist_dup(pci_bus_res[secbus].mem_avail); 1173 memlist_merge(&pci_bus_res[secbus].mem_used, &scratch_list); 1174 1175 if ((pci_bus_res[parbus].mem_reprogram || 1176 (mem_base > mem_limit) || 1177 (!(cmd_reg & PCI_COMM_MAE))) && 1178 !list_is_vga_only(scratch_list, MEM)) { 1179 if (pci_bus_res[secbus].mem_used) { 1180 memlist_subsume(&pci_bus_res[secbus].mem_used, 1181 &pci_bus_res[secbus].mem_avail); 1182 } 1183 if (pci_bus_res[secbus].mem_avail && 1184 (!pci_bus_res[parbus].mem_reprogram) && 1185 (!pci_bus_res[parbus].subtractive)) { 1186 /* rechoose old mem resource */ 1187 list = pci_bus_res[secbus].mem_avail; 1188 mem_base = 0; 1189 do { 1190 if (is_vga(list, MEM)) 1191 continue; 1192 if (mem_base == 0) { 1193 mem_base = (uint_t)list->ml_address; 1194 mem_base = P2ALIGN(mem_base, 1195 PPB_MEM_ALIGNMENT); 1196 mem_limit = (uint_t)(list->ml_address + 1197 list->ml_size - 1); 1198 } else { 1199 if ((list->ml_address + list->ml_size) > 1200 mem_limit) { 1201 mem_limit = (uint_t) 1202 (list->ml_address + 1203 list->ml_size - 1); 1204 } 1205 } 1206 } while ((list = list->ml_next) != NULL); 1207 mem_limit = P2ROUNDUP(mem_limit, PPB_MEM_ALIGNMENT) - 1; 1208 mem_size = mem_limit + 1 - mem_base; 1209 ASSERT(mem_base <= mem_limit); 1210 memlist_free_all(&pci_bus_res[secbus].mem_avail); 1211 memlist_insert(&pci_bus_res[secbus].mem_avail, 1212 mem_base, mem_size); 1213 memlist_insert(&pci_bus_res[parbus].mem_used, 1214 mem_base, mem_size); 1215 (void) memlist_remove(&pci_bus_res[parbus].mem_avail, 1216 mem_base, mem_size); 1217 pci_bus_res[secbus].mem_reprogram = B_TRUE; 1218 } else { 1219 /* get new mem resource from parent bus */ 1220 addr = get_parbus_mem_res(parbus, secbus, mem_size, 1221 mem_align); 1222 if (addr) { 1223 mem_base = addr; 1224 mem_limit = addr + mem_size - 1; 1225 pci_bus_res[secbus].mem_reprogram = B_TRUE; 1226 } 1227 } 1228 1229 if (pci_bus_res[secbus].mem_reprogram) { 1230 /* reprogram PPB MEM regs */ 1231 pci_putw(bus, dev, func, PCI_BCNF_MEM_BASE, 1232 (uint16_t)((mem_base>>16) & 0xfff0)); 1233 pci_putw(bus, dev, func, PCI_BCNF_MEM_LIMIT, 1234 (uint16_t)((mem_limit>>16) & 0xfff0)); 1235 /* 1236 * Disable PMEM window by setting base > limit. 1237 * We currently don't reprogram the PMEM like we've 1238 * done for I/O and MEM. (Devices that support prefetch 1239 * can use non-prefetch MEM.) Anyway, if the MEM access 1240 * bit is initially disabled by BIOS, we disable the 1241 * PMEM window manually by setting PMEM base > PMEM 1242 * limit here, in case there are incorrect values in 1243 * them from BIOS, so that we won't get in trouble once 1244 * the MEM access bit is enabled at the end of this 1245 * function. 1246 */ 1247 if (!(cmd_reg & PCI_COMM_MAE)) { 1248 pci_putw(bus, dev, func, PCI_BCNF_PF_BASE_LOW, 1249 0xfff0); 1250 pci_putw(bus, dev, func, PCI_BCNF_PF_LIMIT_LOW, 1251 0x0); 1252 pci_putl(bus, dev, func, PCI_BCNF_PF_BASE_HIGH, 1253 0xffffffff); 1254 pci_putl(bus, dev, func, PCI_BCNF_PF_LIMIT_HIGH, 1255 0x0); 1256 } 1257 1258 add_ranges_prop(secbus, 1); 1259 1260 cmn_err(CE_NOTE, "!reprogram mem-range on" 1261 " ppb[%x/%x/%x]: 0x%x ~ 0x%x\n", 1262 bus, dev, func, mem_base, mem_limit); 1263 } 1264 } 1265 memlist_free_all(&scratch_list); 1266 1267 cmd_enable: 1268 if (pci_bus_res[secbus].io_avail) 1269 cmd_reg |= PCI_COMM_IO | PCI_COMM_ME; 1270 if (pci_bus_res[secbus].mem_avail) 1271 cmd_reg |= PCI_COMM_MAE | PCI_COMM_ME; 1272 pci_putw(bus, dev, func, PCI_CONF_COMM, cmd_reg); 1273 } 1274 1275 void 1276 pci_reprogram(void) 1277 { 1278 int i, pci_reconfig = 1; 1279 char *onoff; 1280 int bus; 1281 1282 /* 1283 * Scan ACPI namespace for _BBN objects, make sure that 1284 * childless root-bridges appear in devinfo tree 1285 */ 1286 pci_scan_bbn(); 1287 pci_unitaddr_cache_init(); 1288 1289 /* 1290 * Fix-up unit-address assignments if cache is available 1291 */ 1292 if (pci_unitaddr_cache_valid()) { 1293 int pci_regs[] = {0, 0, 0}; 1294 int new_addr; 1295 int index = 0; 1296 1297 for (bus = 0; bus <= pci_bios_maxbus; bus++) { 1298 /* skip non-root (peer) PCI busses */ 1299 if ((pci_bus_res[bus].par_bus != (uchar_t)-1) || 1300 (pci_bus_res[bus].dip == NULL)) 1301 continue; 1302 1303 new_addr = pci_bus_unitaddr(index); 1304 if (pci_bus_res[bus].root_addr != new_addr) { 1305 /* update reg property for node */ 1306 pci_regs[0] = pci_bus_res[bus].root_addr = 1307 new_addr; 1308 (void) ndi_prop_update_int_array( 1309 DDI_DEV_T_NONE, pci_bus_res[bus].dip, 1310 "reg", (int *)pci_regs, 3); 1311 } 1312 index++; 1313 } 1314 } else { 1315 /* perform legacy processing */ 1316 pci_renumber_root_busses(); 1317 pci_unitaddr_cache_create(); 1318 } 1319 1320 /* 1321 * Do root-bus resource discovery 1322 */ 1323 for (bus = 0; bus <= pci_bios_maxbus; bus++) { 1324 /* skip non-root (peer) PCI busses */ 1325 if (pci_bus_res[bus].par_bus != (uchar_t)-1) 1326 continue; 1327 1328 /* 1329 * 1. find resources associated with this root bus 1330 */ 1331 populate_bus_res(bus); 1332 1333 1334 /* 1335 * 2. Remove used PCI and ISA resources from bus resource map 1336 */ 1337 1338 memlist_remove_list(&pci_bus_res[bus].io_avail, 1339 pci_bus_res[bus].io_used); 1340 memlist_remove_list(&pci_bus_res[bus].mem_avail, 1341 pci_bus_res[bus].mem_used); 1342 memlist_remove_list(&pci_bus_res[bus].pmem_avail, 1343 pci_bus_res[bus].pmem_used); 1344 memlist_remove_list(&pci_bus_res[bus].mem_avail, 1345 pci_bus_res[bus].pmem_used); 1346 memlist_remove_list(&pci_bus_res[bus].pmem_avail, 1347 pci_bus_res[bus].mem_used); 1348 1349 memlist_remove_list(&pci_bus_res[bus].io_avail, 1350 isa_res.io_used); 1351 memlist_remove_list(&pci_bus_res[bus].mem_avail, 1352 isa_res.mem_used); 1353 1354 /* 1355 * 3. Exclude <1M address range here in case below reserved 1356 * ranges for BIOS data area, ROM area etc are wrongly reported 1357 * in ACPI resource producer entries for PCI root bus. 1358 * 00000000 - 000003FF RAM 1359 * 00000400 - 000004FF BIOS data area 1360 * 00000500 - 0009FFFF RAM 1361 * 000A0000 - 000BFFFF VGA RAM 1362 * 000C0000 - 000FFFFF ROM area 1363 */ 1364 (void) memlist_remove(&pci_bus_res[bus].mem_avail, 0, 0x100000); 1365 (void) memlist_remove(&pci_bus_res[bus].pmem_avail, 1366 0, 0x100000); 1367 } 1368 1369 memlist_free_all(&isa_res.io_used); 1370 memlist_free_all(&isa_res.mem_used); 1371 1372 /* add bus-range property for root/peer bus nodes */ 1373 for (i = 0; i <= pci_bios_maxbus; i++) { 1374 /* create bus-range property on root/peer buses */ 1375 if (pci_bus_res[i].par_bus == (uchar_t)-1) 1376 add_bus_range_prop(i); 1377 1378 /* setup bus range resource on each bus */ 1379 setup_bus_res(i); 1380 } 1381 1382 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, ddi_root_node(), 1383 DDI_PROP_DONTPASS, "pci-reprog", &onoff) == DDI_SUCCESS) { 1384 if (strcmp(onoff, "off") == 0) { 1385 pci_reconfig = 0; 1386 cmn_err(CE_NOTE, "pci device reprogramming disabled"); 1387 } 1388 ddi_prop_free(onoff); 1389 } 1390 1391 remove_subtractive_res(); 1392 1393 /* reprogram the non-subtractive PPB */ 1394 if (pci_reconfig) 1395 for (i = 0; i <= pci_bios_maxbus; i++) 1396 fix_ppb_res(i, B_FALSE); 1397 1398 for (i = 0; i <= pci_bios_maxbus; i++) { 1399 /* configure devices not configured by BIOS */ 1400 if (pci_reconfig) { 1401 /* 1402 * Reprogram the subtractive PPB. At this time, all its 1403 * siblings should have got their resources already. 1404 */ 1405 if (pci_bus_res[i].subtractive) 1406 fix_ppb_res(i, B_TRUE); 1407 enumerate_bus_devs(i, CONFIG_NEW); 1408 } 1409 } 1410 1411 /* All dev programmed, so we can create available prop */ 1412 for (i = 0; i <= pci_bios_maxbus; i++) 1413 add_bus_available_prop(i); 1414 } 1415 1416 /* 1417 * populate bus resources 1418 */ 1419 static void 1420 populate_bus_res(uchar_t bus) 1421 { 1422 1423 /* scan BIOS structures */ 1424 pci_bus_res[bus].pmem_avail = find_bus_res(bus, PREFETCH_TYPE); 1425 pci_bus_res[bus].mem_avail = find_bus_res(bus, MEM_TYPE); 1426 pci_bus_res[bus].io_avail = find_bus_res(bus, IO_TYPE); 1427 pci_bus_res[bus].bus_avail = find_bus_res(bus, BUSRANGE_TYPE); 1428 1429 /* 1430 * attempt to initialize sub_bus from the largest range-end 1431 * in the bus_avail list 1432 */ 1433 if (pci_bus_res[bus].bus_avail != NULL) { 1434 struct memlist *entry; 1435 int current; 1436 1437 entry = pci_bus_res[bus].bus_avail; 1438 while (entry != NULL) { 1439 current = entry->ml_address + entry->ml_size - 1; 1440 if (current > pci_bus_res[bus].sub_bus) 1441 pci_bus_res[bus].sub_bus = current; 1442 entry = entry->ml_next; 1443 } 1444 } 1445 1446 if (bus == 0) { 1447 /* 1448 * Special treatment of bus 0: 1449 * If no IO/MEM resource from ACPI/MPSPEC/HRT, copy 1450 * pcimem from boot and make I/O space the entire range 1451 * starting at 0x100. 1452 */ 1453 if (pci_bus_res[0].mem_avail == NULL) 1454 pci_bus_res[0].mem_avail = 1455 memlist_dup(bootops->boot_mem->pcimem); 1456 /* Exclude 0x00 to 0xff of the I/O space, used by all PCs */ 1457 if (pci_bus_res[0].io_avail == NULL) 1458 memlist_insert(&pci_bus_res[0].io_avail, 0x100, 0xffff); 1459 } 1460 1461 /* 1462 * Create 'ranges' property here before any resources are 1463 * removed from the resource lists 1464 */ 1465 add_ranges_prop(bus, 0); 1466 } 1467 1468 1469 /* 1470 * Create top-level bus dips, i.e. /pci@0,0, /pci@1,0... 1471 */ 1472 static void 1473 create_root_bus_dip(uchar_t bus) 1474 { 1475 int pci_regs[] = {0, 0, 0}; 1476 dev_info_t *dip; 1477 1478 ASSERT(pci_bus_res[bus].par_bus == (uchar_t)-1); 1479 1480 num_root_bus++; 1481 ndi_devi_alloc_sleep(ddi_root_node(), "pci", 1482 (pnode_t)DEVI_SID_NODEID, &dip); 1483 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 1484 "#address-cells", 3); 1485 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 1486 "#size-cells", 2); 1487 pci_regs[0] = pci_bus_res[bus].root_addr; 1488 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, 1489 "reg", (int *)pci_regs, 3); 1490 1491 /* 1492 * If system has PCIe bus, then create different properties 1493 */ 1494 if (create_pcie_root_bus(bus, dip) == B_FALSE) 1495 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 1496 "device_type", "pci"); 1497 1498 (void) ndi_devi_bind_driver(dip, 0); 1499 pci_bus_res[bus].dip = dip; 1500 } 1501 1502 /* 1503 * For any fixed configuration (often compatability) pci devices 1504 * and those with their own expansion rom, create device nodes 1505 * to hold the already configured device details. 1506 */ 1507 void 1508 enumerate_bus_devs(uchar_t bus, int config_op) 1509 { 1510 uchar_t dev, func, nfunc, header; 1511 ushort_t venid; 1512 struct pci_devfunc *devlist = NULL, *entry; 1513 1514 if (config_op == CONFIG_NEW) { 1515 dcmn_err(CE_NOTE, "configuring pci bus 0x%x", bus); 1516 } else if (config_op == CONFIG_FIX) { 1517 dcmn_err(CE_NOTE, "fixing devices on pci bus 0x%x", bus); 1518 } else 1519 dcmn_err(CE_NOTE, "enumerating pci bus 0x%x", bus); 1520 1521 if (config_op == CONFIG_NEW) { 1522 devlist = (struct pci_devfunc *)pci_bus_res[bus].privdata; 1523 while (devlist) { 1524 entry = devlist; 1525 devlist = entry->next; 1526 if (entry->reprogram || 1527 pci_bus_res[bus].io_reprogram || 1528 pci_bus_res[bus].mem_reprogram) { 1529 /* reprogram device(s) */ 1530 (void) add_reg_props(entry->dip, bus, 1531 entry->dev, entry->func, CONFIG_NEW, 0); 1532 } 1533 kmem_free(entry, sizeof (*entry)); 1534 } 1535 pci_bus_res[bus].privdata = NULL; 1536 return; 1537 } 1538 1539 for (dev = 0; dev < max_dev_pci; dev++) { 1540 nfunc = 1; 1541 for (func = 0; func < nfunc; func++) { 1542 1543 dcmn_err(CE_NOTE, "probing dev 0x%x, func 0x%x", 1544 dev, func); 1545 1546 venid = pci_getw(bus, dev, func, PCI_CONF_VENID); 1547 1548 if ((venid == 0xffff) || (venid == 0)) { 1549 /* no function at this address */ 1550 continue; 1551 } 1552 1553 header = pci_getb(bus, dev, func, PCI_CONF_HEADER); 1554 if (header == 0xff) { 1555 continue; /* illegal value */ 1556 } 1557 1558 /* 1559 * according to some mail from Microsoft posted 1560 * to the pci-drivers alias, their only requirement 1561 * for a multifunction device is for the 1st 1562 * function to have to PCI_HEADER_MULTI bit set. 1563 */ 1564 if ((func == 0) && (header & PCI_HEADER_MULTI)) { 1565 nfunc = 8; 1566 } 1567 1568 if (config_op == CONFIG_FIX || 1569 config_op == CONFIG_INFO) { 1570 /* 1571 * Create the node, unconditionally, on the 1572 * first pass only. It may still need 1573 * resource assignment, which will be 1574 * done on the second, CONFIG_NEW, pass. 1575 */ 1576 process_devfunc(bus, dev, func, header, 1577 venid, config_op); 1578 1579 } 1580 } 1581 } 1582 1583 /* percolate bus used resources up through parents to root */ 1584 if (config_op == CONFIG_INFO) { 1585 int par_bus; 1586 1587 par_bus = pci_bus_res[bus].par_bus; 1588 while (par_bus != (uchar_t)-1) { 1589 pci_bus_res[par_bus].io_size += 1590 pci_bus_res[bus].io_size; 1591 pci_bus_res[par_bus].mem_size += 1592 pci_bus_res[bus].mem_size; 1593 1594 if (pci_bus_res[bus].io_used) 1595 memlist_merge(&pci_bus_res[bus].io_used, 1596 &pci_bus_res[par_bus].io_used); 1597 1598 if (pci_bus_res[bus].mem_used) 1599 memlist_merge(&pci_bus_res[bus].mem_used, 1600 &pci_bus_res[par_bus].mem_used); 1601 1602 if (pci_bus_res[bus].pmem_used) 1603 memlist_merge(&pci_bus_res[bus].pmem_used, 1604 &pci_bus_res[par_bus].pmem_used); 1605 1606 bus = par_bus; 1607 par_bus = pci_bus_res[par_bus].par_bus; 1608 } 1609 } 1610 } 1611 1612 static int 1613 check_pciide_prop(uchar_t revid, ushort_t venid, ushort_t devid, 1614 ushort_t subvenid, ushort_t subdevid) 1615 { 1616 static int prop_exist = -1; 1617 static char *pciide_str; 1618 char compat[32]; 1619 1620 if (prop_exist == -1) { 1621 prop_exist = (ddi_prop_lookup_string(DDI_DEV_T_ANY, 1622 ddi_root_node(), DDI_PROP_DONTPASS, "pci-ide", 1623 &pciide_str) == DDI_SUCCESS); 1624 } 1625 1626 if (!prop_exist) 1627 return (0); 1628 1629 /* compare property value against various forms of compatible */ 1630 if (subvenid) { 1631 (void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x.%x", 1632 venid, devid, subvenid, subdevid, revid); 1633 if (strcmp(pciide_str, compat) == 0) 1634 return (1); 1635 1636 (void) snprintf(compat, sizeof (compat), "pci%x,%x.%x.%x", 1637 venid, devid, subvenid, subdevid); 1638 if (strcmp(pciide_str, compat) == 0) 1639 return (1); 1640 1641 (void) snprintf(compat, sizeof (compat), "pci%x,%x", 1642 subvenid, subdevid); 1643 if (strcmp(pciide_str, compat) == 0) 1644 return (1); 1645 } 1646 (void) snprintf(compat, sizeof (compat), "pci%x,%x.%x", 1647 venid, devid, revid); 1648 if (strcmp(pciide_str, compat) == 0) 1649 return (1); 1650 1651 (void) snprintf(compat, sizeof (compat), "pci%x,%x", venid, devid); 1652 if (strcmp(pciide_str, compat) == 0) 1653 return (1); 1654 1655 return (0); 1656 } 1657 1658 static int 1659 is_pciide(uchar_t basecl, uchar_t subcl, uchar_t revid, 1660 ushort_t venid, ushort_t devid, ushort_t subvenid, ushort_t subdevid) 1661 { 1662 struct ide_table { /* table for PCI_MASS_OTHER */ 1663 ushort_t venid; 1664 ushort_t devid; 1665 } *entry; 1666 1667 /* XXX SATA and other devices: need a way to add dynamically */ 1668 static struct ide_table ide_other[] = { 1669 {0x1095, 0x3112}, 1670 {0x1095, 0x3114}, 1671 {0x1095, 0x3512}, 1672 {0x1095, 0x680}, /* Sil0680 */ 1673 {0x1283, 0x8211}, /* ITE 8211F is subcl PCI_MASS_OTHER */ 1674 {0, 0} 1675 }; 1676 1677 if (basecl != PCI_CLASS_MASS) 1678 return (0); 1679 1680 if (subcl == PCI_MASS_IDE) { 1681 return (1); 1682 } 1683 1684 if (check_pciide_prop(revid, venid, devid, subvenid, subdevid)) 1685 return (1); 1686 1687 if (subcl != PCI_MASS_OTHER && subcl != PCI_MASS_SATA) { 1688 return (0); 1689 } 1690 1691 entry = &ide_other[0]; 1692 while (entry->venid) { 1693 if (entry->venid == venid && entry->devid == devid) 1694 return (1); 1695 entry++; 1696 } 1697 return (0); 1698 } 1699 1700 static int 1701 is_display(uint_t classcode) 1702 { 1703 static uint_t disp_classes[] = { 1704 0x000100, 1705 0x030000, 1706 0x030001 1707 }; 1708 int i, nclasses = sizeof (disp_classes) / sizeof (uint_t); 1709 1710 for (i = 0; i < nclasses; i++) { 1711 if (classcode == disp_classes[i]) 1712 return (1); 1713 } 1714 return (0); 1715 } 1716 1717 static void 1718 add_undofix_entry(uint8_t bus, uint8_t dev, uint8_t fn, 1719 void (*undofn)(uint8_t, uint8_t, uint8_t)) 1720 { 1721 struct pci_fixundo *newundo; 1722 1723 newundo = kmem_alloc(sizeof (struct pci_fixundo), KM_SLEEP); 1724 1725 /* 1726 * Adding an item to this list means that we must turn its NMIENABLE 1727 * bit back on at a later time. 1728 */ 1729 newundo->bus = bus; 1730 newundo->dev = dev; 1731 newundo->fn = fn; 1732 newundo->undofn = undofn; 1733 newundo->next = undolist; 1734 1735 /* add to the undo list in LIFO order */ 1736 undolist = newundo; 1737 } 1738 1739 void 1740 add_pci_fixes(void) 1741 { 1742 int i; 1743 1744 for (i = 0; i <= pci_bios_maxbus; i++) { 1745 /* 1746 * For each bus, apply needed fixes to the appropriate devices. 1747 * This must be done before the main enumeration loop because 1748 * some fixes must be applied to devices normally encountered 1749 * later in the pci scan (e.g. if a fix to device 7 must be 1750 * applied before scanning device 6, applying fixes in the 1751 * normal enumeration loop would obviously be too late). 1752 */ 1753 enumerate_bus_devs(i, CONFIG_FIX); 1754 } 1755 } 1756 1757 void 1758 undo_pci_fixes(void) 1759 { 1760 struct pci_fixundo *nextundo; 1761 uint8_t bus, dev, fn; 1762 1763 /* 1764 * All fixes in the undo list are performed unconditionally. Future 1765 * fixes may require selective undo. 1766 */ 1767 while (undolist != NULL) { 1768 1769 bus = undolist->bus; 1770 dev = undolist->dev; 1771 fn = undolist->fn; 1772 1773 (*(undolist->undofn))(bus, dev, fn); 1774 1775 nextundo = undolist->next; 1776 kmem_free(undolist, sizeof (struct pci_fixundo)); 1777 undolist = nextundo; 1778 } 1779 } 1780 1781 static void 1782 undo_amd8111_pci_fix(uint8_t bus, uint8_t dev, uint8_t fn) 1783 { 1784 uint8_t val8; 1785 1786 val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1); 1787 /* 1788 * The NMIONERR bit is turned back on to allow the SMM BIOS 1789 * to handle more critical PCI errors (e.g. PERR#). 1790 */ 1791 val8 |= AMD8111_ENABLENMI; 1792 pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8); 1793 } 1794 1795 static void 1796 pci_fix_amd8111(uint8_t bus, uint8_t dev, uint8_t fn) 1797 { 1798 uint8_t val8; 1799 1800 val8 = pci_getb(bus, dev, fn, LPC_IO_CONTROL_REG_1); 1801 1802 if ((val8 & AMD8111_ENABLENMI) == 0) 1803 return; 1804 1805 /* 1806 * We reset NMIONERR in the LPC because master-abort on the PCI 1807 * bridge side of the 8111 will cause NMI, which might cause SMI, 1808 * which sometimes prevents all devices from being enumerated. 1809 */ 1810 val8 &= ~AMD8111_ENABLENMI; 1811 1812 pci_putb(bus, dev, fn, LPC_IO_CONTROL_REG_1, val8); 1813 1814 add_undofix_entry(bus, dev, fn, undo_amd8111_pci_fix); 1815 } 1816 1817 static void 1818 set_devpm_d0(uchar_t bus, uchar_t dev, uchar_t func) 1819 { 1820 uint16_t status; 1821 uint8_t header; 1822 uint8_t cap_ptr; 1823 uint8_t cap_id; 1824 uint16_t pmcsr; 1825 1826 status = pci_getw(bus, dev, func, PCI_CONF_STAT); 1827 if (!(status & PCI_STAT_CAP)) 1828 return; /* No capabilities list */ 1829 1830 header = pci_getb(bus, dev, func, PCI_CONF_HEADER) & PCI_HEADER_TYPE_M; 1831 if (header == PCI_HEADER_CARDBUS) 1832 cap_ptr = pci_getb(bus, dev, func, PCI_CBUS_CAP_PTR); 1833 else 1834 cap_ptr = pci_getb(bus, dev, func, PCI_CONF_CAP_PTR); 1835 /* 1836 * Walk the capabilities list searching for a PM entry. 1837 */ 1838 while (cap_ptr != PCI_CAP_NEXT_PTR_NULL && cap_ptr >= PCI_CAP_PTR_OFF) { 1839 cap_ptr &= PCI_CAP_PTR_MASK; 1840 cap_id = pci_getb(bus, dev, func, cap_ptr + PCI_CAP_ID); 1841 if (cap_id == PCI_CAP_ID_PM) { 1842 pmcsr = pci_getw(bus, dev, func, cap_ptr + PCI_PMCSR); 1843 pmcsr &= ~(PCI_PMCSR_STATE_MASK); 1844 pmcsr |= PCI_PMCSR_D0; /* D0 state */ 1845 pci_putw(bus, dev, func, cap_ptr + PCI_PMCSR, pmcsr); 1846 break; 1847 } 1848 cap_ptr = pci_getb(bus, dev, func, cap_ptr + PCI_CAP_NEXT_PTR); 1849 } 1850 1851 } 1852 1853 #define is_isa(bc, sc) \ 1854 (((bc) == PCI_CLASS_BRIDGE) && ((sc) == PCI_BRIDGE_ISA)) 1855 1856 static void 1857 process_devfunc(uchar_t bus, uchar_t dev, uchar_t func, uchar_t header, 1858 ushort_t vendorid, int config_op) 1859 { 1860 char nodename[32], unitaddr[5]; 1861 dev_info_t *dip; 1862 uchar_t basecl, subcl, progcl, intr, revid; 1863 ushort_t subvenid, subdevid, status; 1864 ushort_t slot_num; 1865 uint_t classcode, revclass; 1866 int reprogram = 0, pciide = 0; 1867 int power[2] = {1, 1}; 1868 int pciex = 0; 1869 ushort_t is_pci_bridge = 0; 1870 struct pci_devfunc *devlist = NULL, *entry = NULL; 1871 boolean_t slot_valid; 1872 gfx_entry_t *gfxp; 1873 pcie_req_id_t bdf; 1874 1875 ushort_t deviceid = pci_getw(bus, dev, func, PCI_CONF_DEVID); 1876 1877 switch (header & PCI_HEADER_TYPE_M) { 1878 case PCI_HEADER_ZERO: 1879 subvenid = pci_getw(bus, dev, func, PCI_CONF_SUBVENID); 1880 subdevid = pci_getw(bus, dev, func, PCI_CONF_SUBSYSID); 1881 break; 1882 case PCI_HEADER_CARDBUS: 1883 subvenid = pci_getw(bus, dev, func, PCI_CBUS_SUBVENID); 1884 subdevid = pci_getw(bus, dev, func, PCI_CBUS_SUBSYSID); 1885 /* Record the # of cardbus bridges found on the bus */ 1886 if (config_op == CONFIG_INFO) 1887 pci_bus_res[bus].num_cbb++; 1888 break; 1889 default: 1890 subvenid = 0; 1891 subdevid = 0; 1892 break; 1893 } 1894 1895 if (config_op == CONFIG_FIX) { 1896 if (vendorid == VENID_AMD && deviceid == DEVID_AMD8111_LPC) { 1897 pci_fix_amd8111(bus, dev, func); 1898 } 1899 return; 1900 } 1901 1902 /* XXX should be use generic names? derive from class? */ 1903 revclass = pci_getl(bus, dev, func, PCI_CONF_REVID); 1904 classcode = revclass >> 8; 1905 revid = revclass & 0xff; 1906 1907 /* figure out if this is pci-ide */ 1908 basecl = classcode >> 16; 1909 subcl = (classcode >> 8) & 0xff; 1910 progcl = classcode & 0xff; 1911 1912 1913 if (is_display(classcode)) 1914 (void) snprintf(nodename, sizeof (nodename), "display"); 1915 else if (!pseudo_isa && is_isa(basecl, subcl)) 1916 (void) snprintf(nodename, sizeof (nodename), "isa"); 1917 else if (subvenid != 0) 1918 (void) snprintf(nodename, sizeof (nodename), 1919 "pci%x,%x", subvenid, subdevid); 1920 else 1921 (void) snprintf(nodename, sizeof (nodename), 1922 "pci%x,%x", vendorid, deviceid); 1923 1924 /* make sure parent bus dip has been created */ 1925 if (pci_bus_res[bus].dip == NULL) 1926 create_root_bus_dip(bus); 1927 1928 ndi_devi_alloc_sleep(pci_bus_res[bus].dip, nodename, 1929 DEVI_SID_NODEID, &dip); 1930 1931 if (check_if_device_is_pciex(dip, bus, dev, func, &slot_valid, 1932 &slot_num, &is_pci_bridge) == B_TRUE) 1933 pciex = 1; 1934 1935 bdf = PCI_GETBDF(bus, dev, func); 1936 /* 1937 * Record BAD AMD bridges which don't support MMIO config access. 1938 */ 1939 if (IS_BAD_AMD_NTBRIDGE(vendorid, deviceid) || 1940 IS_AMD_8132_CHIP(vendorid, deviceid)) { 1941 uchar_t secbus = 0; 1942 uchar_t subbus = 0; 1943 1944 if ((basecl == PCI_CLASS_BRIDGE) && 1945 (subcl == PCI_BRIDGE_PCI)) { 1946 secbus = pci_getb(bus, dev, func, PCI_BCNF_SECBUS); 1947 subbus = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS); 1948 } 1949 pci_cfgacc_add_workaround(bdf, secbus, subbus); 1950 } 1951 1952 /* 1953 * Only populate bus_t if this device is sitting under a PCIE root 1954 * complex. Some particular machines have both a PCIE root complex and 1955 * a PCI hostbridge, in which case only devices under the PCIE root 1956 * complex will have their bus_t populated. 1957 */ 1958 if (pcie_get_rc_dip(dip) != NULL) { 1959 ck804_fix_aer_ptr(dip, bdf); 1960 (void) pcie_init_bus(dip, bdf, PCIE_BUS_INITIAL); 1961 } 1962 1963 /* add properties */ 1964 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "device-id", deviceid); 1965 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "vendor-id", vendorid); 1966 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, "revision-id", revid); 1967 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 1968 "class-code", classcode); 1969 if (func == 0) 1970 (void) snprintf(unitaddr, sizeof (unitaddr), "%x", dev); 1971 else 1972 (void) snprintf(unitaddr, sizeof (unitaddr), 1973 "%x,%x", dev, func); 1974 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 1975 "unit-address", unitaddr); 1976 1977 /* add device_type for display nodes */ 1978 if (is_display(classcode)) { 1979 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 1980 "device_type", "display"); 1981 } 1982 /* add special stuff for header type */ 1983 if ((header & PCI_HEADER_TYPE_M) == PCI_HEADER_ZERO) { 1984 uchar_t mingrant = pci_getb(bus, dev, func, PCI_CONF_MIN_G); 1985 uchar_t maxlatency = pci_getb(bus, dev, func, PCI_CONF_MAX_L); 1986 1987 if (subvenid != 0) { 1988 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 1989 "subsystem-id", subdevid); 1990 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 1991 "subsystem-vendor-id", subvenid); 1992 } 1993 if (!pciex) 1994 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 1995 "min-grant", mingrant); 1996 if (!pciex) 1997 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 1998 "max-latency", maxlatency); 1999 } 2000 2001 /* interrupt, record if not 0 */ 2002 intr = pci_getb(bus, dev, func, PCI_CONF_IPIN); 2003 if (intr != 0) 2004 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 2005 "interrupts", intr); 2006 2007 /* 2008 * Add support for 133 mhz pci eventually 2009 */ 2010 status = pci_getw(bus, dev, func, PCI_CONF_STAT); 2011 2012 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 2013 "devsel-speed", (status & PCI_STAT_DEVSELT) >> 9); 2014 if (!pciex && (status & PCI_STAT_FBBC)) 2015 (void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip, 2016 "fast-back-to-back"); 2017 if (!pciex && (status & PCI_STAT_66MHZ)) 2018 (void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip, 2019 "66mhz-capable"); 2020 if (status & PCI_STAT_UDF) 2021 (void) ndi_prop_create_boolean(DDI_DEV_T_NONE, dip, 2022 "udf-supported"); 2023 if (pciex && slot_valid) { 2024 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 2025 "physical-slot#", slot_num); 2026 if (!is_pci_bridge) 2027 pciex_slot_names_prop(dip, slot_num); 2028 } 2029 2030 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, 2031 "power-consumption", power, 2); 2032 2033 /* Set the device PM state to D0 */ 2034 set_devpm_d0(bus, dev, func); 2035 2036 if ((basecl == PCI_CLASS_BRIDGE) && (subcl == PCI_BRIDGE_PCI)) 2037 add_ppb_props(dip, bus, dev, func, pciex, is_pci_bridge); 2038 else { 2039 /* 2040 * Record the non-PPB devices on the bus for possible 2041 * reprogramming at 2nd bus enumeration. 2042 * Note: PPB reprogramming is done in fix_ppb_res() 2043 */ 2044 devlist = (struct pci_devfunc *)pci_bus_res[bus].privdata; 2045 entry = kmem_zalloc(sizeof (*entry), KM_SLEEP); 2046 entry->dip = dip; 2047 entry->dev = dev; 2048 entry->func = func; 2049 entry->next = devlist; 2050 pci_bus_res[bus].privdata = entry; 2051 } 2052 2053 if (IS_CLASS_IOAPIC(basecl, subcl, progcl)) { 2054 create_ioapic_node(bus, dev, func, vendorid, deviceid); 2055 } 2056 2057 /* check for NVIDIA CK8-04/MCP55 based LPC bridge */ 2058 if (NVIDIA_IS_LPC_BRIDGE(vendorid, deviceid) && (dev == 1) && 2059 (func == 0)) { 2060 add_nvidia_isa_bridge_props(dip, bus, dev, func); 2061 /* each LPC bridge has an integrated IOAPIC */ 2062 apic_nvidia_io_max++; 2063 } 2064 2065 if (pciex && is_pci_bridge) 2066 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model", 2067 (char *)"PCIe-PCI bridge"); 2068 else 2069 add_model_prop(dip, classcode); 2070 2071 add_compatible(dip, subvenid, subdevid, vendorid, deviceid, 2072 revid, classcode, pciex); 2073 2074 /* 2075 * See if this device is a controller that advertises 2076 * itself to be a standard ATA task file controller, or one that 2077 * has been hard coded. 2078 * 2079 * If it is, check if any other higher precedence driver listed in 2080 * driver_aliases will claim the node by calling 2081 * ddi_compatibile_driver_major. If so, clear pciide and do not 2082 * create a pci-ide node or any other special handling. 2083 * 2084 * If another driver does not bind, set the node name to pci-ide 2085 * and then let the special pci-ide handling for registers and 2086 * child pci-ide nodes proceed below. 2087 */ 2088 if (is_pciide(basecl, subcl, revid, vendorid, deviceid, 2089 subvenid, subdevid) == 1) { 2090 if (ddi_compatible_driver_major(dip, NULL) == (major_t)-1) { 2091 (void) ndi_devi_set_nodename(dip, "pci-ide", 0); 2092 pciide = 1; 2093 } 2094 } 2095 2096 DEVI_SET_PCI(dip); 2097 reprogram = add_reg_props(dip, bus, dev, func, config_op, pciide); 2098 (void) ndi_devi_bind_driver(dip, 0); 2099 2100 /* special handling for pci-ide */ 2101 if (pciide) { 2102 dev_info_t *cdip; 2103 2104 /* 2105 * Create properties specified by P1275 Working Group 2106 * Proposal #414 Version 1 2107 */ 2108 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 2109 "device_type", "pci-ide"); 2110 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 2111 "#address-cells", 1); 2112 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 2113 "#size-cells", 0); 2114 2115 /* allocate two child nodes */ 2116 ndi_devi_alloc_sleep(dip, "ide", 2117 (pnode_t)DEVI_SID_NODEID, &cdip); 2118 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip, 2119 "reg", 0); 2120 (void) ndi_devi_bind_driver(cdip, 0); 2121 ndi_devi_alloc_sleep(dip, "ide", 2122 (pnode_t)DEVI_SID_NODEID, &cdip); 2123 (void) ndi_prop_update_int(DDI_DEV_T_NONE, cdip, 2124 "reg", 1); 2125 (void) ndi_devi_bind_driver(cdip, 0); 2126 2127 reprogram = 0; /* don't reprogram pci-ide bridge */ 2128 } 2129 2130 if (is_display(classcode)) { 2131 gfxp = kmem_zalloc(sizeof (*gfxp), KM_SLEEP); 2132 gfxp->g_dip = dip; 2133 gfxp->g_prev = NULL; 2134 gfxp->g_next = gfx_devinfo_list; 2135 gfx_devinfo_list = gfxp; 2136 if (gfxp->g_next) 2137 gfxp->g_next->g_prev = gfxp; 2138 } 2139 2140 /* special handling for isa */ 2141 if (!pseudo_isa && is_isa(basecl, subcl)) { 2142 /* add device_type */ 2143 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 2144 "device_type", "isa"); 2145 } 2146 2147 if (reprogram && (entry != NULL)) 2148 entry->reprogram = B_TRUE; 2149 2150 } 2151 2152 /* 2153 * Some vendors do not use unique subsystem IDs in their products, which 2154 * makes the use of form 2 compatible names (pciSSSS,ssss) inappropriate. 2155 * Allow for these compatible forms to be excluded on a per-device basis. 2156 */ 2157 /*ARGSUSED*/ 2158 static boolean_t 2159 subsys_compat_exclude(ushort_t venid, ushort_t devid, ushort_t subvenid, 2160 ushort_t subdevid, uchar_t revid, uint_t classcode) 2161 { 2162 /* Nvidia display adapters */ 2163 if ((venid == 0x10de) && (is_display(classcode))) 2164 return (B_TRUE); 2165 2166 return (B_FALSE); 2167 } 2168 2169 /* 2170 * Set the compatible property to a value compliant with 2171 * rev 2.1 of the IEEE1275 PCI binding. 2172 * (Also used for PCI-Express devices). 2173 * 2174 * pciVVVV,DDDD.SSSS.ssss.RR (0) 2175 * pciVVVV,DDDD.SSSS.ssss (1) 2176 * pciSSSS,ssss (2) 2177 * pciVVVV,DDDD.RR (3) 2178 * pciVVVV,DDDD (4) 2179 * pciclass,CCSSPP (5) 2180 * pciclass,CCSS (6) 2181 * 2182 * The Subsystem (SSSS) forms are not inserted if 2183 * subsystem-vendor-id is 0. 2184 * 2185 * NOTE: For PCI-Express devices "pci" is replaced with "pciex" in 0-6 above 2186 * property 2 is not created as per "1275 bindings for PCI Express Interconnect" 2187 * 2188 * Set with setprop and \x00 between each 2189 * to generate the encoded string array form. 2190 */ 2191 void 2192 add_compatible(dev_info_t *dip, ushort_t subvenid, ushort_t subdevid, 2193 ushort_t vendorid, ushort_t deviceid, uchar_t revid, uint_t classcode, 2194 int pciex) 2195 { 2196 int i = 0; 2197 int size = COMPAT_BUFSIZE; 2198 char *compat[13]; 2199 char *buf, *curr; 2200 2201 curr = buf = kmem_alloc(size, KM_SLEEP); 2202 2203 if (pciex) { 2204 if (subvenid) { 2205 compat[i++] = curr; /* form 0 */ 2206 (void) snprintf(curr, size, "pciex%x,%x.%x.%x.%x", 2207 vendorid, deviceid, subvenid, subdevid, revid); 2208 size -= strlen(curr) + 1; 2209 curr += strlen(curr) + 1; 2210 2211 compat[i++] = curr; /* form 1 */ 2212 (void) snprintf(curr, size, "pciex%x,%x.%x.%x", 2213 vendorid, deviceid, subvenid, subdevid); 2214 size -= strlen(curr) + 1; 2215 curr += strlen(curr) + 1; 2216 2217 } 2218 compat[i++] = curr; /* form 3 */ 2219 (void) snprintf(curr, size, "pciex%x,%x.%x", 2220 vendorid, deviceid, revid); 2221 size -= strlen(curr) + 1; 2222 curr += strlen(curr) + 1; 2223 2224 compat[i++] = curr; /* form 4 */ 2225 (void) snprintf(curr, size, "pciex%x,%x", vendorid, deviceid); 2226 size -= strlen(curr) + 1; 2227 curr += strlen(curr) + 1; 2228 2229 compat[i++] = curr; /* form 5 */ 2230 (void) snprintf(curr, size, "pciexclass,%06x", classcode); 2231 size -= strlen(curr) + 1; 2232 curr += strlen(curr) + 1; 2233 2234 compat[i++] = curr; /* form 6 */ 2235 (void) snprintf(curr, size, "pciexclass,%04x", 2236 (classcode >> 8)); 2237 size -= strlen(curr) + 1; 2238 curr += strlen(curr) + 1; 2239 } 2240 2241 if (subvenid) { 2242 compat[i++] = curr; /* form 0 */ 2243 (void) snprintf(curr, size, "pci%x,%x.%x.%x.%x", 2244 vendorid, deviceid, subvenid, subdevid, revid); 2245 size -= strlen(curr) + 1; 2246 curr += strlen(curr) + 1; 2247 2248 compat[i++] = curr; /* form 1 */ 2249 (void) snprintf(curr, size, "pci%x,%x.%x.%x", 2250 vendorid, deviceid, subvenid, subdevid); 2251 size -= strlen(curr) + 1; 2252 curr += strlen(curr) + 1; 2253 2254 if (subsys_compat_exclude(vendorid, deviceid, subvenid, 2255 subdevid, revid, classcode) == B_FALSE) { 2256 compat[i++] = curr; /* form 2 */ 2257 (void) snprintf(curr, size, "pci%x,%x", subvenid, 2258 subdevid); 2259 size -= strlen(curr) + 1; 2260 curr += strlen(curr) + 1; 2261 } 2262 } 2263 compat[i++] = curr; /* form 3 */ 2264 (void) snprintf(curr, size, "pci%x,%x.%x", vendorid, deviceid, revid); 2265 size -= strlen(curr) + 1; 2266 curr += strlen(curr) + 1; 2267 2268 compat[i++] = curr; /* form 4 */ 2269 (void) snprintf(curr, size, "pci%x,%x", vendorid, deviceid); 2270 size -= strlen(curr) + 1; 2271 curr += strlen(curr) + 1; 2272 2273 compat[i++] = curr; /* form 5 */ 2274 (void) snprintf(curr, size, "pciclass,%06x", classcode); 2275 size -= strlen(curr) + 1; 2276 curr += strlen(curr) + 1; 2277 2278 compat[i++] = curr; /* form 6 */ 2279 (void) snprintf(curr, size, "pciclass,%04x", (classcode >> 8)); 2280 size -= strlen(curr) + 1; 2281 curr += strlen(curr) + 1; 2282 2283 (void) ndi_prop_update_string_array(DDI_DEV_T_NONE, dip, 2284 "compatible", compat, i); 2285 kmem_free(buf, COMPAT_BUFSIZE); 2286 } 2287 2288 /* 2289 * Adjust the reg properties for a dual channel PCI-IDE device. 2290 * 2291 * NOTE: don't do anything that changes the order of the hard-decodes 2292 * and programmed BARs. The kernel driver depends on these values 2293 * being in this order regardless of whether they're for a 'native' 2294 * mode BAR or not. 2295 */ 2296 /* 2297 * config info for pci-ide devices 2298 */ 2299 static struct { 2300 uchar_t native_mask; /* 0 == 'compatibility' mode, 1 == native */ 2301 uchar_t bar_offset; /* offset for alt status register */ 2302 ushort_t addr; /* compatibility mode base address */ 2303 ushort_t length; /* number of ports for this BAR */ 2304 } pciide_bar[] = { 2305 { 0x01, 0, 0x1f0, 8 }, /* primary lower BAR */ 2306 { 0x01, 2, 0x3f6, 1 }, /* primary upper BAR */ 2307 { 0x04, 0, 0x170, 8 }, /* secondary lower BAR */ 2308 { 0x04, 2, 0x376, 1 } /* secondary upper BAR */ 2309 }; 2310 2311 static int 2312 pciIdeAdjustBAR(uchar_t progcl, int index, uint_t *basep, uint_t *lenp) 2313 { 2314 int hard_decode = 0; 2315 2316 /* 2317 * Adjust the base and len for the BARs of the PCI-IDE 2318 * device's primary and secondary controllers. The first 2319 * two BARs are for the primary controller and the next 2320 * two BARs are for the secondary controller. The fifth 2321 * and sixth bars are never adjusted. 2322 */ 2323 if (index >= 0 && index <= 3) { 2324 *lenp = pciide_bar[index].length; 2325 2326 if (progcl & pciide_bar[index].native_mask) { 2327 *basep += pciide_bar[index].bar_offset; 2328 } else { 2329 *basep = pciide_bar[index].addr; 2330 hard_decode = 1; 2331 } 2332 } 2333 2334 /* 2335 * if either base or len is zero make certain both are zero 2336 */ 2337 if (*basep == 0 || *lenp == 0) { 2338 *basep = 0; 2339 *lenp = 0; 2340 hard_decode = 0; 2341 } 2342 2343 return (hard_decode); 2344 } 2345 2346 2347 /* 2348 * Add the "reg" and "assigned-addresses" property 2349 */ 2350 static int 2351 add_reg_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func, 2352 int config_op, int pciide) 2353 { 2354 uchar_t baseclass, subclass, progclass, header; 2355 ushort_t bar_sz; 2356 uint_t value = 0, len, devloc; 2357 uint_t base, base_hi, type; 2358 ushort_t offset, end; 2359 int max_basereg, j, reprogram = 0; 2360 uint_t phys_hi; 2361 struct memlist **io_avail, **io_used; 2362 struct memlist **mem_avail, **mem_used; 2363 struct memlist **pmem_avail, **pmem_used; 2364 uchar_t res_bus; 2365 2366 pci_regspec_t regs[16] = {{0}}; 2367 pci_regspec_t assigned[15] = {{0}}; 2368 int nreg, nasgn; 2369 2370 io_avail = &pci_bus_res[bus].io_avail; 2371 io_used = &pci_bus_res[bus].io_used; 2372 mem_avail = &pci_bus_res[bus].mem_avail; 2373 mem_used = &pci_bus_res[bus].mem_used; 2374 pmem_avail = &pci_bus_res[bus].pmem_avail; 2375 pmem_used = &pci_bus_res[bus].pmem_used; 2376 2377 devloc = (uint_t)bus << 16 | (uint_t)dev << 11 | (uint_t)func << 8; 2378 regs[0].pci_phys_hi = devloc; 2379 nreg = 1; /* rest of regs[0] is all zero */ 2380 nasgn = 0; 2381 2382 baseclass = pci_getb(bus, dev, func, PCI_CONF_BASCLASS); 2383 subclass = pci_getb(bus, dev, func, PCI_CONF_SUBCLASS); 2384 progclass = pci_getb(bus, dev, func, PCI_CONF_PROGCLASS); 2385 header = pci_getb(bus, dev, func, PCI_CONF_HEADER) & PCI_HEADER_TYPE_M; 2386 2387 switch (header) { 2388 case PCI_HEADER_ZERO: 2389 max_basereg = PCI_BASE_NUM; 2390 break; 2391 case PCI_HEADER_PPB: 2392 max_basereg = PCI_BCNF_BASE_NUM; 2393 break; 2394 case PCI_HEADER_CARDBUS: 2395 max_basereg = PCI_CBUS_BASE_NUM; 2396 reprogram = 1; 2397 break; 2398 default: 2399 max_basereg = 0; 2400 break; 2401 } 2402 2403 /* 2404 * Create the register property by saving the current 2405 * value of the base register. Write 0xffffffff to the 2406 * base register. Read the value back to determine the 2407 * required size of the address space. Restore the base 2408 * register contents. 2409 * 2410 * Do not disable I/O and memory access for bridges; this 2411 * has the side-effect of making the bridge transparent to 2412 * secondary-bus activity (see sections 4.1-4.3 of the 2413 * PCI-PCI Bridge Spec V1.2). For non-bridges, disable 2414 * I/O and memory access to avoid difficulty with USB 2415 * emulation (see OHCI spec1.0a appendix B 2416 * "Host Controller Mapping") 2417 */ 2418 end = PCI_CONF_BASE0 + max_basereg * sizeof (uint_t); 2419 for (j = 0, offset = PCI_CONF_BASE0; offset < end; 2420 j++, offset += bar_sz) { 2421 uint_t command; 2422 2423 /* determine the size of the address space */ 2424 base = pci_getl(bus, dev, func, offset); 2425 if (baseclass != PCI_CLASS_BRIDGE) { 2426 command = (uint_t)pci_getw(bus, dev, func, 2427 PCI_CONF_COMM); 2428 pci_putw(bus, dev, func, PCI_CONF_COMM, 2429 command & ~(PCI_COMM_MAE | PCI_COMM_IO)); 2430 } 2431 pci_putl(bus, dev, func, offset, 0xffffffff); 2432 value = pci_getl(bus, dev, func, offset); 2433 pci_putl(bus, dev, func, offset, base); 2434 if (baseclass != PCI_CLASS_BRIDGE) 2435 pci_putw(bus, dev, func, PCI_CONF_COMM, command); 2436 2437 /* construct phys hi,med.lo, size hi, lo */ 2438 if ((pciide && j < 4) || (base & PCI_BASE_SPACE_IO)) { 2439 int hard_decode = 0; 2440 2441 /* i/o space */ 2442 bar_sz = PCI_BAR_SZ_32; 2443 value &= PCI_BASE_IO_ADDR_M; 2444 len = ((value ^ (value-1)) + 1) >> 1; 2445 2446 /* XXX Adjust first 4 IDE registers */ 2447 if (pciide) { 2448 if (subclass != PCI_MASS_IDE) 2449 progclass = (PCI_IDE_IF_NATIVE_PRI | 2450 PCI_IDE_IF_NATIVE_SEC); 2451 hard_decode = pciIdeAdjustBAR(progclass, j, 2452 &base, &len); 2453 } else if (value == 0) { 2454 /* skip base regs with size of 0 */ 2455 continue; 2456 } 2457 2458 regs[nreg].pci_phys_hi = PCI_ADDR_IO | devloc | 2459 (hard_decode ? PCI_RELOCAT_B : offset); 2460 regs[nreg].pci_phys_low = hard_decode ? 2461 base & PCI_BASE_IO_ADDR_M : 0; 2462 assigned[nasgn].pci_phys_hi = 2463 PCI_RELOCAT_B | regs[nreg].pci_phys_hi; 2464 regs[nreg].pci_size_low = 2465 assigned[nasgn].pci_size_low = len; 2466 type = base & (~PCI_BASE_IO_ADDR_M); 2467 base &= PCI_BASE_IO_ADDR_M; 2468 /* 2469 * A device under a subtractive PPB can allocate 2470 * resources from its parent bus if there is no resource 2471 * available on its own bus. 2472 */ 2473 if ((config_op == CONFIG_NEW) && (*io_avail == NULL)) { 2474 res_bus = bus; 2475 while (pci_bus_res[res_bus].subtractive) { 2476 res_bus = pci_bus_res[res_bus].par_bus; 2477 if (res_bus == (uchar_t)-1) 2478 break; /* root bus already */ 2479 if (pci_bus_res[res_bus].io_avail) { 2480 io_avail = &pci_bus_res 2481 [res_bus].io_avail; 2482 break; 2483 } 2484 } 2485 } 2486 2487 /* 2488 * first pass - gather what's there 2489 * update/second pass - adjust/allocate regions 2490 * config - allocate regions 2491 */ 2492 if (config_op == CONFIG_INFO) { /* first pass */ 2493 /* take out of the resource map of the bus */ 2494 if (base != 0) { 2495 (void) memlist_remove(io_avail, base, 2496 len); 2497 memlist_insert(io_used, base, len); 2498 } else { 2499 reprogram = 1; 2500 } 2501 pci_bus_res[bus].io_size += len; 2502 } else if ((*io_avail && base == 0) || 2503 pci_bus_res[bus].io_reprogram) { 2504 base = (uint_t)memlist_find(io_avail, len, len); 2505 if (base != 0) { 2506 memlist_insert(io_used, base, len); 2507 /* XXX need to worry about 64-bit? */ 2508 pci_putl(bus, dev, func, offset, 2509 base | type); 2510 base = pci_getl(bus, dev, func, offset); 2511 base &= PCI_BASE_IO_ADDR_M; 2512 } 2513 if (base == 0) { 2514 cmn_err(CE_WARN, "failed to program" 2515 " IO space [%d/%d/%d] BAR@0x%x" 2516 " length 0x%x", 2517 bus, dev, func, offset, len); 2518 } 2519 } 2520 assigned[nasgn].pci_phys_low = base; 2521 nreg++, nasgn++; 2522 2523 } else { 2524 /* memory space */ 2525 if ((base & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL) { 2526 bar_sz = PCI_BAR_SZ_64; 2527 base_hi = pci_getl(bus, dev, func, offset + 4); 2528 phys_hi = PCI_ADDR_MEM64; 2529 } else { 2530 bar_sz = PCI_BAR_SZ_32; 2531 base_hi = 0; 2532 phys_hi = PCI_ADDR_MEM32; 2533 } 2534 2535 /* skip base regs with size of 0 */ 2536 value &= PCI_BASE_M_ADDR_M; 2537 2538 if (value == 0) 2539 continue; 2540 2541 len = ((value ^ (value-1)) + 1) >> 1; 2542 regs[nreg].pci_size_low = 2543 assigned[nasgn].pci_size_low = len; 2544 2545 phys_hi |= (devloc | offset); 2546 if (base & PCI_BASE_PREF_M) 2547 phys_hi |= PCI_PREFETCH_B; 2548 2549 /* 2550 * A device under a subtractive PPB can allocate 2551 * resources from its parent bus if there is no resource 2552 * available on its own bus. 2553 */ 2554 if ((config_op == CONFIG_NEW) && (*mem_avail == NULL)) { 2555 res_bus = bus; 2556 while (pci_bus_res[res_bus].subtractive) { 2557 res_bus = pci_bus_res[res_bus].par_bus; 2558 if (res_bus == (uchar_t)-1) 2559 break; /* root bus already */ 2560 mem_avail = 2561 &pci_bus_res[res_bus].mem_avail; 2562 pmem_avail = 2563 &pci_bus_res [res_bus].pmem_avail; 2564 /* 2565 * Break out as long as at least 2566 * mem_avail is available 2567 */ 2568 if ((*pmem_avail && 2569 (phys_hi & PCI_PREFETCH_B)) || 2570 *mem_avail) 2571 break; 2572 } 2573 } 2574 2575 regs[nreg].pci_phys_hi = 2576 assigned[nasgn].pci_phys_hi = phys_hi; 2577 assigned[nasgn].pci_phys_hi |= PCI_RELOCAT_B; 2578 assigned[nasgn].pci_phys_mid = base_hi; 2579 type = base & ~PCI_BASE_M_ADDR_M; 2580 base &= PCI_BASE_M_ADDR_M; 2581 2582 if (config_op == CONFIG_INFO) { 2583 /* take out of the resource map of the bus */ 2584 if (base != NULL) { 2585 /* remove from PMEM and MEM space */ 2586 (void) memlist_remove(mem_avail, 2587 base, len); 2588 (void) memlist_remove(pmem_avail, 2589 base, len); 2590 /* only note as used in correct map */ 2591 if (phys_hi & PCI_PREFETCH_B) 2592 memlist_insert(pmem_used, 2593 base, len); 2594 else 2595 memlist_insert(mem_used, 2596 base, len); 2597 } else { 2598 reprogram = 1; 2599 } 2600 pci_bus_res[bus].mem_size += len; 2601 } else if ((*mem_avail && base == NULL) || 2602 pci_bus_res[bus].mem_reprogram) { 2603 /* 2604 * When desired, attempt a prefetchable 2605 * allocation first 2606 */ 2607 if (phys_hi & PCI_PREFETCH_B) { 2608 base = (uint_t)memlist_find(pmem_avail, 2609 len, len); 2610 if (base != NULL) { 2611 memlist_insert(pmem_used, 2612 base, len); 2613 (void) memlist_remove(mem_avail, 2614 base, len); 2615 } 2616 } 2617 /* 2618 * If prefetchable allocation was not 2619 * desired, or failed, attempt ordinary 2620 * memory allocation 2621 */ 2622 if (base == NULL) { 2623 base = (uint_t)memlist_find(mem_avail, 2624 len, len); 2625 if (base != NULL) { 2626 memlist_insert(mem_used, 2627 base, len); 2628 (void) memlist_remove( 2629 pmem_avail, base, len); 2630 } 2631 } 2632 if (base != NULL) { 2633 pci_putl(bus, dev, func, offset, 2634 base | type); 2635 base = pci_getl(bus, dev, func, offset); 2636 base &= PCI_BASE_M_ADDR_M; 2637 } else 2638 cmn_err(CE_WARN, "failed to program " 2639 "mem space [%d/%d/%d] BAR@0x%x" 2640 " length 0x%x", 2641 bus, dev, func, offset, len); 2642 } 2643 assigned[nasgn].pci_phys_low = base; 2644 nreg++, nasgn++; 2645 } 2646 } 2647 switch (header) { 2648 case PCI_HEADER_ZERO: 2649 offset = PCI_CONF_ROM; 2650 break; 2651 case PCI_HEADER_PPB: 2652 offset = PCI_BCNF_ROM; 2653 break; 2654 default: /* including PCI_HEADER_CARDBUS */ 2655 goto done; 2656 } 2657 2658 /* 2659 * Add the expansion rom memory space 2660 * Determine the size of the ROM base reg; don't write reserved bits 2661 * ROM isn't in the PCI memory space. 2662 */ 2663 base = pci_getl(bus, dev, func, offset); 2664 pci_putl(bus, dev, func, offset, PCI_BASE_ROM_ADDR_M); 2665 value = pci_getl(bus, dev, func, offset); 2666 pci_putl(bus, dev, func, offset, base); 2667 if (value & PCI_BASE_ROM_ENABLE) 2668 value &= PCI_BASE_ROM_ADDR_M; 2669 else 2670 value = 0; 2671 2672 if (value != 0) { 2673 regs[nreg].pci_phys_hi = (PCI_ADDR_MEM32 | devloc) + offset; 2674 assigned[nasgn].pci_phys_hi = (PCI_RELOCAT_B | 2675 PCI_ADDR_MEM32 | devloc) + offset; 2676 base &= PCI_BASE_ROM_ADDR_M; 2677 assigned[nasgn].pci_phys_low = base; 2678 len = ((value ^ (value-1)) + 1) >> 1; 2679 regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = len; 2680 nreg++, nasgn++; 2681 /* take it out of the memory resource */ 2682 if (base != NULL) { 2683 (void) memlist_remove(mem_avail, base, len); 2684 memlist_insert(mem_used, base, len); 2685 pci_bus_res[bus].mem_size += len; 2686 } 2687 } 2688 2689 /* 2690 * Account for "legacy" (alias) video adapter resources 2691 */ 2692 2693 /* add the three hard-decode, aliased address spaces for VGA */ 2694 if ((baseclass == PCI_CLASS_DISPLAY && subclass == PCI_DISPLAY_VGA) || 2695 (baseclass == PCI_CLASS_NONE && subclass == PCI_NONE_VGA)) { 2696 2697 /* VGA hard decode 0x3b0-0x3bb */ 2698 regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi = 2699 (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc); 2700 regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3b0; 2701 regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0xc; 2702 nreg++, nasgn++; 2703 (void) memlist_remove(io_avail, 0x3b0, 0xc); 2704 memlist_insert(io_used, 0x3b0, 0xc); 2705 pci_bus_res[bus].io_size += 0xc; 2706 2707 /* VGA hard decode 0x3c0-0x3df */ 2708 regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi = 2709 (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc); 2710 regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x3c0; 2711 regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x20; 2712 nreg++, nasgn++; 2713 (void) memlist_remove(io_avail, 0x3c0, 0x20); 2714 memlist_insert(io_used, 0x3c0, 0x20); 2715 pci_bus_res[bus].io_size += 0x20; 2716 2717 /* Video memory */ 2718 regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi = 2719 (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_MEM32 | devloc); 2720 regs[nreg].pci_phys_low = 2721 assigned[nasgn].pci_phys_low = 0xa0000; 2722 regs[nreg].pci_size_low = 2723 assigned[nasgn].pci_size_low = 0x20000; 2724 nreg++, nasgn++; 2725 /* remove from MEM and PMEM space */ 2726 (void) memlist_remove(mem_avail, 0xa0000, 0x20000); 2727 (void) memlist_remove(pmem_avail, 0xa0000, 0x20000); 2728 memlist_insert(mem_used, 0xa0000, 0x20000); 2729 pci_bus_res[bus].mem_size += 0x20000; 2730 } 2731 2732 /* add the hard-decode, aliased address spaces for 8514 */ 2733 if ((baseclass == PCI_CLASS_DISPLAY) && 2734 (subclass == PCI_DISPLAY_VGA) && 2735 (progclass & PCI_DISPLAY_IF_8514)) { 2736 2737 /* hard decode 0x2e8 */ 2738 regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi = 2739 (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc); 2740 regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2e8; 2741 regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x1; 2742 nreg++, nasgn++; 2743 (void) memlist_remove(io_avail, 0x2e8, 0x1); 2744 memlist_insert(io_used, 0x2e8, 0x1); 2745 pci_bus_res[bus].io_size += 0x1; 2746 2747 /* hard decode 0x2ea-0x2ef */ 2748 regs[nreg].pci_phys_hi = assigned[nasgn].pci_phys_hi = 2749 (PCI_RELOCAT_B | PCI_ALIAS_B | PCI_ADDR_IO | devloc); 2750 regs[nreg].pci_phys_low = assigned[nasgn].pci_phys_low = 0x2ea; 2751 regs[nreg].pci_size_low = assigned[nasgn].pci_size_low = 0x6; 2752 nreg++, nasgn++; 2753 (void) memlist_remove(io_avail, 0x2ea, 0x6); 2754 memlist_insert(io_used, 0x2ea, 0x6); 2755 pci_bus_res[bus].io_size += 0x6; 2756 } 2757 2758 done: 2759 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "reg", 2760 (int *)regs, nreg * sizeof (pci_regspec_t) / sizeof (int)); 2761 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, 2762 "assigned-addresses", 2763 (int *)assigned, nasgn * sizeof (pci_regspec_t) / sizeof (int)); 2764 2765 return (reprogram); 2766 } 2767 2768 static void 2769 add_ppb_props(dev_info_t *dip, uchar_t bus, uchar_t dev, uchar_t func, 2770 int pciex, ushort_t is_pci_bridge) 2771 { 2772 char *dev_type; 2773 int i; 2774 uint_t val, io_range[2], mem_range[2], pmem_range[2]; 2775 uchar_t secbus = pci_getb(bus, dev, func, PCI_BCNF_SECBUS); 2776 uchar_t subbus = pci_getb(bus, dev, func, PCI_BCNF_SUBBUS); 2777 uchar_t progclass; 2778 2779 ASSERT(secbus <= subbus); 2780 2781 /* 2782 * Check if it's a subtractive PPB. 2783 */ 2784 progclass = pci_getb(bus, dev, func, PCI_CONF_PROGCLASS); 2785 if (progclass == PCI_BRIDGE_PCI_IF_SUBDECODE) 2786 pci_bus_res[secbus].subtractive = B_TRUE; 2787 2788 /* 2789 * Some BIOSes lie about max pci busses, we allow for 2790 * such mistakes here 2791 */ 2792 if (subbus > pci_bios_maxbus) { 2793 pci_bios_maxbus = subbus; 2794 alloc_res_array(); 2795 } 2796 2797 ASSERT(pci_bus_res[secbus].dip == NULL); 2798 pci_bus_res[secbus].dip = dip; 2799 pci_bus_res[secbus].par_bus = bus; 2800 2801 dev_type = (pciex && !is_pci_bridge) ? "pciex" : "pci"; 2802 2803 /* setup bus number hierarchy */ 2804 pci_bus_res[secbus].sub_bus = subbus; 2805 /* 2806 * Keep track of the largest subordinate bus number (this is essential 2807 * for peer busses because there is no other way of determining its 2808 * subordinate bus number). 2809 */ 2810 if (subbus > pci_bus_res[bus].sub_bus) 2811 pci_bus_res[bus].sub_bus = subbus; 2812 /* 2813 * Loop through subordinate busses, initializing their parent bus 2814 * field to this bridge's parent. The subordinate busses' parent 2815 * fields may very well be further refined later, as child bridges 2816 * are enumerated. (The value is to note that the subordinate busses 2817 * are not peer busses by changing their par_bus fields to anything 2818 * other than -1.) 2819 */ 2820 for (i = secbus + 1; i <= subbus; i++) 2821 pci_bus_res[i].par_bus = bus; 2822 2823 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, 2824 "device_type", dev_type); 2825 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 2826 "#address-cells", 3); 2827 (void) ndi_prop_update_int(DDI_DEV_T_NONE, dip, 2828 "#size-cells", 2); 2829 2830 /* 2831 * Collect bridge window specifications, and use them to populate 2832 * the "avail" resources for the bus. Not all of those resources will 2833 * end up being available; this is done top-down, and so the initial 2834 * collection of windows populates the 'ranges' property for the 2835 * bus node. Later, as children are found, resources are removed from 2836 * the 'avail' list, so that it becomes the freelist for 2837 * this point in the tree. ranges may be set again after bridge 2838 * reprogramming in fix_ppb_res(), in which case it's set from 2839 * used + avail. 2840 * 2841 * According to PPB spec, the base register should be programmed 2842 * with a value bigger than the limit register when there are 2843 * no resources available. This applies to io, memory, and 2844 * prefetchable memory. 2845 */ 2846 2847 /* 2848 * io range 2849 * We determine i/o windows that are left unconfigured by BIOS 2850 * through its i/o enable bit as Microsoft recommends OEMs to do. 2851 * If it is unset, we disable i/o and mark it for reconfiguration in 2852 * later passes by setting the base > limit 2853 */ 2854 val = (uint_t)pci_getw(bus, dev, func, PCI_CONF_COMM); 2855 if (val & PCI_COMM_IO) { 2856 val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_BASE_LOW); 2857 io_range[0] = ((val & 0xf0) << 8); 2858 val = (uint_t)pci_getb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW); 2859 io_range[1] = ((val & 0xf0) << 8) | 0xFFF; 2860 } else { 2861 io_range[0] = 0x9fff; 2862 io_range[1] = 0x1000; 2863 pci_putb(bus, dev, func, PCI_BCNF_IO_BASE_LOW, 2864 (uint8_t)((io_range[0] >> 8) & 0xf0)); 2865 pci_putb(bus, dev, func, PCI_BCNF_IO_LIMIT_LOW, 2866 (uint8_t)((io_range[1] >> 8) & 0xf0)); 2867 pci_putw(bus, dev, func, PCI_BCNF_IO_BASE_HI, 0); 2868 pci_putw(bus, dev, func, PCI_BCNF_IO_LIMIT_HI, 0); 2869 } 2870 2871 if (io_range[0] != 0 && io_range[0] < io_range[1]) { 2872 memlist_insert(&pci_bus_res[secbus].io_avail, 2873 (uint64_t)io_range[0], 2874 (uint64_t)(io_range[1] - io_range[0] + 1)); 2875 memlist_insert(&pci_bus_res[bus].io_used, 2876 (uint64_t)io_range[0], 2877 (uint64_t)(io_range[1] - io_range[0] + 1)); 2878 if (pci_bus_res[bus].io_avail != NULL) { 2879 (void) memlist_remove(&pci_bus_res[bus].io_avail, 2880 (uint64_t)io_range[0], 2881 (uint64_t)(io_range[1] - io_range[0] + 1)); 2882 } 2883 dcmn_err(CE_NOTE, "bus %d io-range: 0x%x-%x", 2884 secbus, io_range[0], io_range[1]); 2885 /* if 32-bit supported, make sure upper bits are not set */ 2886 if ((val & 0xf) == 1 && 2887 pci_getw(bus, dev, func, PCI_BCNF_IO_BASE_HI)) { 2888 cmn_err(CE_NOTE, "unsupported 32-bit IO address on" 2889 " pci-pci bridge [%d/%d/%d]", bus, dev, func); 2890 } 2891 } 2892 2893 /* mem range */ 2894 val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_BASE); 2895 mem_range[0] = ((val & 0xFFF0) << 16); 2896 val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_MEM_LIMIT); 2897 mem_range[1] = ((val & 0xFFF0) << 16) | 0xFFFFF; 2898 if (mem_range[0] != 0 && mem_range[0] < mem_range[1]) { 2899 memlist_insert(&pci_bus_res[secbus].mem_avail, 2900 (uint64_t)mem_range[0], 2901 (uint64_t)(mem_range[1] - mem_range[0] + 1)); 2902 memlist_insert(&pci_bus_res[bus].mem_used, 2903 (uint64_t)mem_range[0], 2904 (uint64_t)(mem_range[1] - mem_range[0] + 1)); 2905 /* remove from parent resource list */ 2906 (void) memlist_remove(&pci_bus_res[bus].mem_avail, 2907 (uint64_t)mem_range[0], 2908 (uint64_t)(mem_range[1] - mem_range[0] + 1)); 2909 (void) memlist_remove(&pci_bus_res[bus].pmem_avail, 2910 (uint64_t)mem_range[0], 2911 (uint64_t)(mem_range[1] - mem_range[0] + 1)); 2912 dcmn_err(CE_NOTE, "bus %d mem-range: 0x%x-%x", 2913 secbus, mem_range[0], mem_range[1]); 2914 } 2915 2916 /* prefetchable memory range */ 2917 val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_BASE_LOW); 2918 pmem_range[0] = ((val & 0xFFF0) << 16); 2919 val = (uint_t)pci_getw(bus, dev, func, PCI_BCNF_PF_LIMIT_LOW); 2920 pmem_range[1] = ((val & 0xFFF0) << 16) | 0xFFFFF; 2921 if (pmem_range[0] != 0 && pmem_range[0] < pmem_range[1]) { 2922 memlist_insert(&pci_bus_res[secbus].pmem_avail, 2923 (uint64_t)pmem_range[0], 2924 (uint64_t)(pmem_range[1] - pmem_range[0] + 1)); 2925 memlist_insert(&pci_bus_res[bus].pmem_used, 2926 (uint64_t)pmem_range[0], 2927 (uint64_t)(pmem_range[1] - pmem_range[0] + 1)); 2928 /* remove from parent resource list */ 2929 (void) memlist_remove(&pci_bus_res[bus].pmem_avail, 2930 (uint64_t)pmem_range[0], 2931 (uint64_t)(pmem_range[1] - pmem_range[0] + 1)); 2932 (void) memlist_remove(&pci_bus_res[bus].mem_avail, 2933 (uint64_t)pmem_range[0], 2934 (uint64_t)(pmem_range[1] - pmem_range[0] + 1)); 2935 dcmn_err(CE_NOTE, "bus %d pmem-range: 0x%x-%x", 2936 secbus, pmem_range[0], pmem_range[1]); 2937 /* if 64-bit supported, make sure upper bits are not set */ 2938 if ((val & 0xf) == 1 && 2939 pci_getl(bus, dev, func, PCI_BCNF_PF_BASE_HIGH)) { 2940 cmn_err(CE_NOTE, "unsupported 64-bit prefetch memory on" 2941 " pci-pci bridge [%d/%d/%d]", bus, dev, func); 2942 } 2943 } 2944 2945 /* 2946 * Add VGA legacy resources to the bridge's pci_bus_res if it 2947 * has VGA_ENABLE set. Note that we put them in 'avail', 2948 * because that's used to populate the ranges prop; they'll be 2949 * removed from there by the VGA device once it's found. Also, 2950 * remove them from the parent's available list and note them as 2951 * used in the parent. 2952 */ 2953 2954 if (pci_getw(bus, dev, func, PCI_BCNF_BCNTRL) & 2955 PCI_BCNF_BCNTRL_VGA_ENABLE) { 2956 2957 memlist_insert(&pci_bus_res[secbus].io_avail, 0x3b0, 0xc); 2958 2959 memlist_insert(&pci_bus_res[bus].io_used, 0x3b0, 0xc); 2960 if (pci_bus_res[bus].io_avail != NULL) { 2961 (void) memlist_remove(&pci_bus_res[bus].io_avail, 2962 0x3b0, 0xc); 2963 } 2964 2965 memlist_insert(&pci_bus_res[secbus].io_avail, 0x3c0, 0x20); 2966 2967 memlist_insert(&pci_bus_res[bus].io_used, 0x3c0, 0x20); 2968 if (pci_bus_res[bus].io_avail != NULL) { 2969 (void) memlist_remove(&pci_bus_res[bus].io_avail, 2970 0x3c0, 0x20); 2971 } 2972 2973 memlist_insert(&pci_bus_res[secbus].mem_avail, 0xa0000, 2974 0x20000); 2975 2976 memlist_insert(&pci_bus_res[bus].mem_used, 0xa0000, 0x20000); 2977 if (pci_bus_res[bus].mem_avail != NULL) { 2978 (void) memlist_remove(&pci_bus_res[bus].mem_avail, 2979 0xa0000, 0x20000); 2980 } 2981 } 2982 add_bus_range_prop(secbus); 2983 add_ranges_prop(secbus, 1); 2984 } 2985 2986 extern const struct pci_class_strings_s class_pci[]; 2987 extern int class_pci_items; 2988 2989 static void 2990 add_model_prop(dev_info_t *dip, uint_t classcode) 2991 { 2992 const char *desc; 2993 int i; 2994 uchar_t baseclass = classcode >> 16; 2995 uchar_t subclass = (classcode >> 8) & 0xff; 2996 uchar_t progclass = classcode & 0xff; 2997 2998 if ((baseclass == PCI_CLASS_MASS) && (subclass == PCI_MASS_IDE)) { 2999 desc = "IDE controller"; 3000 } else { 3001 for (desc = 0, i = 0; i < class_pci_items; i++) { 3002 if ((baseclass == class_pci[i].base_class) && 3003 (subclass == class_pci[i].sub_class) && 3004 (progclass == class_pci[i].prog_class)) { 3005 desc = class_pci[i].actual_desc; 3006 break; 3007 } 3008 } 3009 if (i == class_pci_items) 3010 desc = "Unknown class of pci/pnpbios device"; 3011 } 3012 3013 (void) ndi_prop_update_string(DDI_DEV_T_NONE, dip, "model", 3014 (char *)desc); 3015 } 3016 3017 static void 3018 add_bus_range_prop(int bus) 3019 { 3020 int bus_range[2]; 3021 3022 if (pci_bus_res[bus].dip == NULL) 3023 return; 3024 bus_range[0] = bus; 3025 bus_range[1] = pci_bus_res[bus].sub_bus; 3026 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip, 3027 "bus-range", (int *)bus_range, 2); 3028 } 3029 3030 /* 3031 * Add slot-names property for any named pci hot-plug slots 3032 */ 3033 static void 3034 add_bus_slot_names_prop(int bus) 3035 { 3036 char slotprop[256]; 3037 int len; 3038 extern int pci_irq_nroutes; 3039 char *slotcap_name; 3040 3041 /* 3042 * If no irq routing table, then go with the slot-names as set up 3043 * in pciex_slot_names_prop() from slot capability register (if any). 3044 */ 3045 if (pci_irq_nroutes == 0) 3046 return; 3047 3048 /* 3049 * Otherise delete the slot-names we already have and use the irq 3050 * routing table values as returned by pci_slot_names_prop() instead, 3051 * but keep any property of value "pcie0" as that can't be represented 3052 * in the irq routing table. 3053 */ 3054 if (pci_bus_res[bus].dip != NULL) { 3055 if (ddi_prop_lookup_string(DDI_DEV_T_ANY, pci_bus_res[bus].dip, 3056 DDI_PROP_DONTPASS, "slot-names", &slotcap_name) != 3057 DDI_SUCCESS || strcmp(slotcap_name, "pcie0") != 0) 3058 (void) ndi_prop_remove(DDI_DEV_T_NONE, 3059 pci_bus_res[bus].dip, "slot-names"); 3060 } 3061 3062 len = pci_slot_names_prop(bus, slotprop, sizeof (slotprop)); 3063 if (len > 0) { 3064 /* 3065 * Only create a peer bus node if this bus may be a peer bus. 3066 * It may be a peer bus if the dip is NULL and if par_bus is 3067 * -1 (par_bus is -1 if this bus was not found to be 3068 * subordinate to any PCI-PCI bridge). 3069 * If it's not a peer bus, then the ACPI BBN-handling code 3070 * will remove it later. 3071 */ 3072 if (pci_bus_res[bus].par_bus == (uchar_t)-1 && 3073 pci_bus_res[bus].dip == NULL) { 3074 3075 create_root_bus_dip(bus); 3076 } 3077 if (pci_bus_res[bus].dip != NULL) { 3078 ASSERT((len % sizeof (int)) == 0); 3079 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, 3080 pci_bus_res[bus].dip, "slot-names", 3081 (int *)slotprop, len / sizeof (int)); 3082 } else { 3083 cmn_err(CE_NOTE, "!BIOS BUG: Invalid bus number in PCI " 3084 "IRQ routing table; Not adding slot-names " 3085 "property for incorrect bus %d", bus); 3086 } 3087 } 3088 } 3089 3090 /* 3091 * Handle both PCI root and PCI-PCI bridge range properties; 3092 * non-zero 'ppb' argument select PCI-PCI bridges versus root. 3093 */ 3094 static void 3095 memlist_to_ranges(void **rp, struct memlist *entry, int type, int ppb) 3096 { 3097 ppb_ranges_t *ppb_rp = *rp; 3098 pci_ranges_t *pci_rp = *rp; 3099 3100 while (entry != NULL) { 3101 if (ppb) { 3102 ppb_rp->child_high = ppb_rp->parent_high = type; 3103 ppb_rp->child_mid = ppb_rp->parent_mid = 3104 (uint32_t)(entry->ml_address >> 32); /* XXX */ 3105 ppb_rp->child_low = ppb_rp->parent_low = 3106 (uint32_t)entry->ml_address; 3107 ppb_rp->size_high = 3108 (uint32_t)(entry->ml_size >> 32); /* XXX */ 3109 ppb_rp->size_low = (uint32_t)entry->ml_size; 3110 *rp = ++ppb_rp; 3111 } else { 3112 pci_rp->child_high = type; 3113 pci_rp->child_mid = pci_rp->parent_high = 3114 (uint32_t)(entry->ml_address >> 32); /* XXX */ 3115 pci_rp->child_low = pci_rp->parent_low = 3116 (uint32_t)entry->ml_address; 3117 pci_rp->size_high = 3118 (uint32_t)(entry->ml_size >> 32); /* XXX */ 3119 pci_rp->size_low = (uint32_t)entry->ml_size; 3120 *rp = ++pci_rp; 3121 } 3122 entry = entry->ml_next; 3123 } 3124 } 3125 3126 static void 3127 add_ranges_prop(int bus, int ppb) 3128 { 3129 int total, alloc_size; 3130 void *rp, *next_rp; 3131 struct memlist *iolist, *memlist, *pmemlist; 3132 3133 /* no devinfo node - unused bus, return */ 3134 if (pci_bus_res[bus].dip == NULL) 3135 return; 3136 3137 iolist = memlist = pmemlist = (struct memlist *)NULL; 3138 3139 memlist_merge(&pci_bus_res[bus].io_avail, &iolist); 3140 memlist_merge(&pci_bus_res[bus].io_used, &iolist); 3141 memlist_merge(&pci_bus_res[bus].mem_avail, &memlist); 3142 memlist_merge(&pci_bus_res[bus].mem_used, &memlist); 3143 memlist_merge(&pci_bus_res[bus].pmem_avail, &pmemlist); 3144 memlist_merge(&pci_bus_res[bus].pmem_used, &pmemlist); 3145 3146 total = memlist_count(iolist); 3147 total += memlist_count(memlist); 3148 total += memlist_count(pmemlist); 3149 3150 /* no property is created if no ranges are present */ 3151 if (total == 0) 3152 return; 3153 3154 alloc_size = total * 3155 (ppb ? sizeof (ppb_ranges_t) : sizeof (pci_ranges_t)); 3156 3157 next_rp = rp = kmem_alloc(alloc_size, KM_SLEEP); 3158 3159 memlist_to_ranges(&next_rp, iolist, PCI_ADDR_IO | PCI_REG_REL_M, ppb); 3160 memlist_to_ranges(&next_rp, memlist, 3161 PCI_ADDR_MEM32 | PCI_REG_REL_M, ppb); 3162 memlist_to_ranges(&next_rp, pmemlist, 3163 PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M, ppb); 3164 3165 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip, 3166 "ranges", (int *)rp, alloc_size / sizeof (int)); 3167 3168 kmem_free(rp, alloc_size); 3169 memlist_free_all(&iolist); 3170 memlist_free_all(&memlist); 3171 memlist_free_all(&pmemlist); 3172 } 3173 3174 static void 3175 memlist_remove_list(struct memlist **list, struct memlist *remove_list) 3176 { 3177 while (list && *list && remove_list) { 3178 (void) memlist_remove(list, remove_list->ml_address, 3179 remove_list->ml_size); 3180 remove_list = remove_list->ml_next; 3181 } 3182 } 3183 3184 static int 3185 memlist_to_spec(struct pci_phys_spec *sp, struct memlist *list, int type) 3186 { 3187 int i = 0; 3188 3189 while (list) { 3190 /* assume 32-bit addresses */ 3191 sp->pci_phys_hi = type; 3192 sp->pci_phys_mid = 0; 3193 sp->pci_phys_low = (uint32_t)list->ml_address; 3194 sp->pci_size_hi = 0; 3195 sp->pci_size_low = (uint32_t)list->ml_size; 3196 3197 list = list->ml_next; 3198 sp++, i++; 3199 } 3200 return (i); 3201 } 3202 3203 static void 3204 add_bus_available_prop(int bus) 3205 { 3206 int i, count; 3207 struct pci_phys_spec *sp; 3208 3209 /* no devinfo node - unused bus, return */ 3210 if (pci_bus_res[bus].dip == NULL) 3211 return; 3212 3213 count = memlist_count(pci_bus_res[bus].io_avail) + 3214 memlist_count(pci_bus_res[bus].mem_avail) + 3215 memlist_count(pci_bus_res[bus].pmem_avail); 3216 3217 if (count == 0) /* nothing available */ 3218 return; 3219 3220 sp = kmem_alloc(count * sizeof (*sp), KM_SLEEP); 3221 i = memlist_to_spec(&sp[0], pci_bus_res[bus].io_avail, 3222 PCI_ADDR_IO | PCI_REG_REL_M); 3223 i += memlist_to_spec(&sp[i], pci_bus_res[bus].mem_avail, 3224 PCI_ADDR_MEM32 | PCI_REG_REL_M); 3225 i += memlist_to_spec(&sp[i], pci_bus_res[bus].pmem_avail, 3226 PCI_ADDR_MEM32 | PCI_REG_REL_M | PCI_REG_PF_M); 3227 ASSERT(i == count); 3228 3229 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, pci_bus_res[bus].dip, 3230 "available", (int *)sp, 3231 i * sizeof (struct pci_phys_spec) / sizeof (int)); 3232 kmem_free(sp, count * sizeof (*sp)); 3233 } 3234 3235 static void 3236 alloc_res_array(void) 3237 { 3238 static int array_size = 0; 3239 int old_size; 3240 void *old_res; 3241 3242 if (array_size > pci_bios_maxbus + 1) 3243 return; /* array is big enough */ 3244 3245 old_size = array_size; 3246 old_res = pci_bus_res; 3247 3248 if (array_size == 0) 3249 array_size = 16; /* start with a reasonable number */ 3250 3251 while (array_size <= pci_bios_maxbus + 1) 3252 array_size <<= 1; 3253 pci_bus_res = (struct pci_bus_resource *)kmem_zalloc( 3254 array_size * sizeof (struct pci_bus_resource), KM_SLEEP); 3255 3256 if (old_res) { /* copy content and free old array */ 3257 bcopy(old_res, pci_bus_res, 3258 old_size * sizeof (struct pci_bus_resource)); 3259 kmem_free(old_res, old_size * sizeof (struct pci_bus_resource)); 3260 } 3261 } 3262 3263 static void 3264 create_ioapic_node(int bus, int dev, int fn, ushort_t vendorid, 3265 ushort_t deviceid) 3266 { 3267 static dev_info_t *ioapicsnode = NULL; 3268 static int numioapics = 0; 3269 dev_info_t *ioapic_node; 3270 uint64_t physaddr; 3271 uint32_t lobase, hibase = 0; 3272 3273 /* BAR 0 contains the IOAPIC's memory-mapped I/O address */ 3274 lobase = (*pci_getl_func)(bus, dev, fn, PCI_CONF_BASE0); 3275 3276 /* We (and the rest of the world) only support memory-mapped IOAPICs */ 3277 if ((lobase & PCI_BASE_SPACE_M) != PCI_BASE_SPACE_MEM) 3278 return; 3279 3280 if ((lobase & PCI_BASE_TYPE_M) == PCI_BASE_TYPE_ALL) 3281 hibase = (*pci_getl_func)(bus, dev, fn, PCI_CONF_BASE0 + 4); 3282 3283 lobase &= PCI_BASE_M_ADDR_M; 3284 3285 physaddr = (((uint64_t)hibase) << 32) | lobase; 3286 3287 /* 3288 * Create a nexus node for all IOAPICs under the root node. 3289 */ 3290 if (ioapicsnode == NULL) { 3291 if (ndi_devi_alloc(ddi_root_node(), IOAPICS_NODE_NAME, 3292 (pnode_t)DEVI_SID_NODEID, &ioapicsnode) != NDI_SUCCESS) { 3293 return; 3294 } 3295 (void) ndi_devi_online(ioapicsnode, 0); 3296 } 3297 3298 /* 3299 * Create a child node for this IOAPIC 3300 */ 3301 ioapic_node = ddi_add_child(ioapicsnode, IOAPICS_CHILD_NAME, 3302 DEVI_SID_NODEID, numioapics++); 3303 if (ioapic_node == NULL) { 3304 return; 3305 } 3306 3307 /* Vendor and Device ID */ 3308 (void) ndi_prop_update_int(DDI_DEV_T_NONE, ioapic_node, 3309 IOAPICS_PROP_VENID, vendorid); 3310 (void) ndi_prop_update_int(DDI_DEV_T_NONE, ioapic_node, 3311 IOAPICS_PROP_DEVID, deviceid); 3312 3313 /* device_type */ 3314 (void) ndi_prop_update_string(DDI_DEV_T_NONE, ioapic_node, 3315 "device_type", IOAPICS_DEV_TYPE); 3316 3317 /* reg */ 3318 (void) ndi_prop_update_int64(DDI_DEV_T_NONE, ioapic_node, 3319 "reg", physaddr); 3320 } 3321 3322 /* 3323 * NOTE: For PCIe slots, the name is generated from the slot number 3324 * information obtained from Slot Capabilities register. 3325 * For non-PCIe slots, it is generated based on the slot number 3326 * information in the PCI IRQ table. 3327 */ 3328 static void 3329 pciex_slot_names_prop(dev_info_t *dip, ushort_t slot_num) 3330 { 3331 char slotprop[256]; 3332 int len; 3333 3334 bzero(slotprop, sizeof (slotprop)); 3335 3336 /* set mask to 1 as there is only one slot (i.e dev 0) */ 3337 *(uint32_t *)slotprop = 1; 3338 len = 4; 3339 (void) snprintf(slotprop + len, sizeof (slotprop) - len, "pcie%d", 3340 slot_num); 3341 len += strlen(slotprop + len) + 1; 3342 len += len % 4; 3343 (void) ndi_prop_update_int_array(DDI_DEV_T_NONE, dip, "slot-names", 3344 (int *)slotprop, len / sizeof (int)); 3345 } 3346 3347 /* 3348 * Enable reporting of AER capability next pointer. 3349 * This needs to be done only for CK8-04 devices 3350 * by setting NV_XVR_VEND_CYA1 (offset 0xf40) bit 13 3351 * NOTE: BIOS is disabling this, it needs to be enabled temporarily 3352 * 3353 * This function is adapted from npe_ck804_fix_aer_ptr(), and is 3354 * called from pci_boot.c. 3355 */ 3356 static void 3357 ck804_fix_aer_ptr(dev_info_t *dip, pcie_req_id_t bdf) 3358 { 3359 dev_info_t *rcdip; 3360 ushort_t cya1; 3361 3362 rcdip = pcie_get_rc_dip(dip); 3363 ASSERT(rcdip != NULL); 3364 3365 if ((pci_cfgacc_get16(rcdip, bdf, PCI_CONF_VENID) == 3366 NVIDIA_VENDOR_ID) && 3367 (pci_cfgacc_get16(rcdip, bdf, PCI_CONF_DEVID) == 3368 NVIDIA_CK804_DEVICE_ID) && 3369 (pci_cfgacc_get8(rcdip, bdf, PCI_CONF_REVID) >= 3370 NVIDIA_CK804_AER_VALID_REVID)) { 3371 cya1 = pci_cfgacc_get16(rcdip, bdf, NVIDIA_CK804_VEND_CYA1_OFF); 3372 if (!(cya1 & ~NVIDIA_CK804_VEND_CYA1_ERPT_MASK)) 3373 (void) pci_cfgacc_put16(rcdip, bdf, 3374 NVIDIA_CK804_VEND_CYA1_OFF, 3375 cya1 | NVIDIA_CK804_VEND_CYA1_ERPT_VAL); 3376 } 3377 } 3378