1 /*- 2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3 * Copyright (c) 2016 Netflix, Inc 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 30 /* 31 * PnP enumerator using the PCI BIOS. 32 */ 33 34 #include <stand.h> 35 #include <machine/stdarg.h> 36 #include <machine/cpufunc.h> 37 #include <bootstrap.h> 38 #include <isapnp.h> 39 #include <btxv86.h> 40 #include "libi386.h" 41 #include "ficl.h" 42 43 /* 44 * Stupid PCI BIOS interface doesn't let you simply enumerate everything 45 * that's there, instead you have to ask it if it has something. 46 * 47 * So we have to scan by class code, subclass code and sometimes programming 48 * interface. 49 */ 50 51 struct pci_progif 52 { 53 int pi_code; 54 const char *pi_name; 55 }; 56 57 static struct pci_progif progif_null[] = { 58 {0x0, NULL}, 59 {-1, NULL} 60 }; 61 62 static struct pci_progif progif_display[] = { 63 {0x0, "VGA"}, 64 {0x1, "8514"}, 65 {-1, NULL} 66 }; 67 68 static struct pci_progif progif_ide[] = { 69 {0x00, NULL}, 70 {0x01, NULL}, 71 {0x02, NULL}, 72 {0x03, NULL}, 73 {0x04, NULL}, 74 {0x05, NULL}, 75 {0x06, NULL}, 76 {0x07, NULL}, 77 {0x08, NULL}, 78 {0x09, NULL}, 79 {0x0a, NULL}, 80 {0x0b, NULL}, 81 {0x0c, NULL}, 82 {0x0d, NULL}, 83 {0x0e, NULL}, 84 {0x0f, NULL}, 85 {0x80, NULL}, 86 {0x81, NULL}, 87 {0x82, NULL}, 88 {0x83, NULL}, 89 {0x84, NULL}, 90 {0x85, NULL}, 91 {0x86, NULL}, 92 {0x87, NULL}, 93 {0x88, NULL}, 94 {0x89, NULL}, 95 {0x8a, NULL}, 96 {0x8b, NULL}, 97 {0x8c, NULL}, 98 {0x8d, NULL}, 99 {0x8e, NULL}, 100 {0x8f, NULL}, 101 {-1, NULL} 102 }; 103 104 static struct pci_progif progif_serial[] = { 105 {0x0, "8250"}, 106 {0x1, "16450"}, 107 {0x2, "16550"}, 108 {-1, NULL} 109 }; 110 111 static struct pci_progif progif_parallel[] = { 112 {0x0, "Standard"}, 113 {0x1, "Bidirectional"}, 114 {0x2, "ECP"}, 115 {-1, NULL} 116 }; 117 118 static struct pci_progif progif_firewire[] = { 119 {0x10, "OHCI"}, 120 {-1, NULL} 121 }; 122 123 struct pci_subclass 124 { 125 int ps_subclass; 126 const char *ps_name; 127 struct pci_progif *ps_progif; /* if set, use for programming interface value(s) */ 128 }; 129 130 static struct pci_subclass subclass_old[] = { 131 {0x0, "Old non-VGA", progif_null}, 132 {0x1, "Old VGA", progif_null}, 133 {-1, NULL, NULL} 134 }; 135 136 static struct pci_subclass subclass_mass[] = { 137 {0x0, "SCSI", progif_null}, 138 {0x1, "IDE", progif_ide}, 139 {0x2, "Floppy disk", progif_null}, 140 {0x3, "IPI", progif_null}, 141 {0x4, "RAID", progif_null}, 142 {0x80, "mass storage", progif_null}, 143 {-1, NULL, NULL} 144 }; 145 146 static struct pci_subclass subclass_net[] = { 147 {0x0, "Ethernet", progif_null}, 148 {0x1, "Token ring", progif_null}, 149 {0x2, "FDDI", progif_null}, 150 {0x3, "ATM", progif_null}, 151 {0x80, "network", progif_null}, 152 {-1, NULL, NULL} 153 }; 154 155 static struct pci_subclass subclass_display[] = { 156 {0x0, NULL, progif_display}, 157 {0x1, "XGA", progif_null}, 158 {0x80, "other", progif_null}, 159 {-1, NULL, NULL} 160 }; 161 162 static struct pci_subclass subclass_comms[] = { 163 {0x0, "serial", progif_serial}, 164 {0x1, "parallel", progif_parallel}, 165 {0x80, "communications", progif_null}, 166 {-1, NULL, NULL} 167 }; 168 169 static struct pci_subclass subclass_serial[] = { 170 {0x0, "FireWire", progif_firewire}, 171 {0x1, "ACCESS.bus", progif_null}, 172 {0x2, "SSA", progif_null}, 173 {0x3, "USB", progif_null}, 174 {0x4, "Fibrechannel", progif_null}, 175 {-1, NULL, NULL} 176 }; 177 178 static struct pci_class 179 { 180 int pc_class; 181 const char *pc_name; 182 struct pci_subclass *pc_subclass; 183 } pci_classes[] = { 184 {0x0, "device", subclass_old}, 185 {0x1, "controller", subclass_mass}, 186 {0x2, "controller", subclass_net}, 187 {0x3, "display", subclass_display}, 188 {0x7, "controller", subclass_comms}, 189 {0xc, "controller", subclass_serial}, 190 {-1, NULL, NULL} 191 }; 192 193 194 static void biospci_enumerate(void); 195 static void biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc, struct pci_progif *ppi); 196 197 struct pnphandler biospcihandler = 198 { 199 "PCI BIOS", 200 biospci_enumerate 201 }; 202 203 #define PCI_BIOS_PRESENT 0xb101 204 #define FIND_PCI_DEVICE 0xb102 205 #define FIND_PCI_CLASS_CODE 0xb103 206 #define GENERATE_SPECIAL_CYCLE 0xb106 207 #define READ_CONFIG_BYTE 0xb108 208 #define READ_CONFIG_WORD 0xb109 209 #define READ_CONFIG_DWORD 0xb10a 210 #define WRITE_CONFIG_BYTE 0xb10b 211 #define WRITE_CONFIG_WORD 0xb10c 212 #define WRITE_CONFIG_DWORD 0xb10d 213 #define GET_IRQ_ROUTING_OPTIONS 0xb10e 214 #define SET_PCI_IRQ 0xb10f 215 216 #define PCI_INT 0x1a 217 218 #define PCI_SIGNATURE 0x20494350 /* AKA "PCI " */ 219 220 void 221 biospci_detect(void) 222 { 223 uint16_t version, hwcap, maxbus; 224 char buf[24]; 225 226 /* Find the PCI BIOS */ 227 v86.ctl = V86_FLAGS; 228 v86.addr = PCI_INT; 229 v86.eax = PCI_BIOS_PRESENT; 230 v86.edi = 0x0; 231 v86int(); 232 233 /* Check for OK response */ 234 if (V86_CY(v86.efl) || ((v86.eax & 0xff00) != 0) || 235 (v86.edx != PCI_SIGNATURE)) 236 return; 237 238 version = v86.ebx & 0xffff; 239 hwcap = v86.eax & 0xff; 240 maxbus = v86.ecx & 0xff; 241 #if 0 242 printf("PCI BIOS %d.%d%s%s maxbus %d\n", 243 bcd2bin((version >> 8) & 0xf), bcd2bin(version & 0xf), 244 (hwcap & 1) ? " config1" : "", (hwcap & 2) ? " config2" : "", 245 maxbus); 246 #endif 247 sprintf(buf, "%d", bcd2bin((version >> 8) & 0xf)); 248 setenv("pcibios.major", buf, 1); 249 sprintf(buf, "%d", bcd2bin(version & 0xf)); 250 setenv("pcibios.minor", buf, 1); 251 sprintf(buf, "%d", !!(hwcap & 1)); 252 setenv("pcibios.config1", buf, 1); 253 sprintf(buf, "%d", !!(hwcap & 2)); 254 setenv("pcibios.config2", buf, 1); 255 sprintf(buf, "%d", maxbus); 256 setenv("pcibios.maxbus", buf, 1); 257 258 } 259 260 static void 261 biospci_enumerate(void) 262 { 263 int device_index, err; 264 uint32_t locator, devid; 265 struct pci_class *pc; 266 struct pci_subclass *psc; 267 struct pci_progif *ppi; 268 269 /* Iterate over known classes */ 270 for (pc = pci_classes; pc->pc_class >= 0; pc++) { 271 /* Iterate over subclasses */ 272 for (psc = pc->pc_subclass; psc->ps_subclass >= 0; psc++) { 273 /* Iterate over programming interfaces */ 274 for (ppi = psc->ps_progif; ppi->pi_code >= 0; ppi++) { 275 276 /* Scan for matches */ 277 for (device_index = 0; ; device_index++) { 278 /* Look for a match */ 279 err = biospci_find_devclass((pc->pc_class << 16) 280 + (psc->ps_subclass << 8) + ppi->pi_code, 281 device_index, &locator); 282 if (err != 0) 283 break; 284 285 /* Read the device identifier from the nominated device */ 286 err = biospci_read_config(locator, 0, 2, &devid); 287 if (err != 0) 288 break; 289 290 /* We have the device ID, create a PnP object and save everything */ 291 biospci_addinfo(devid, pc, psc, ppi); 292 } 293 } 294 } 295 } 296 } 297 298 static void 299 biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc, struct pci_progif *ppi) 300 { 301 struct pnpinfo *pi; 302 char desc[80]; 303 304 305 /* build the description */ 306 desc[0] = 0; 307 if (ppi->pi_name != NULL) { 308 strcat(desc, ppi->pi_name); 309 strcat(desc, " "); 310 } 311 if (psc->ps_name != NULL) { 312 strcat(desc, psc->ps_name); 313 strcat(desc, " "); 314 } 315 if (pc->pc_name != NULL) 316 strcat(desc, pc->pc_name); 317 318 pi = pnp_allocinfo(); 319 pi->pi_desc = strdup(desc); 320 sprintf(desc,"0x%08x", devid); 321 pnp_addident(pi, desc); 322 pnp_addinfo(pi); 323 } 324 325 int 326 biospci_find_devclass(uint32_t class, int index, uint32_t *locator) 327 { 328 v86.ctl = V86_FLAGS; 329 v86.addr = PCI_INT; 330 v86.eax = FIND_PCI_CLASS_CODE; 331 v86.ecx = class; 332 v86.esi = index; 333 v86int(); 334 335 /* error */ 336 if (V86_CY(v86.efl) || (v86.eax & 0xff00)) 337 return (-1); 338 339 *locator = v86.ebx; 340 return (0); 341 } 342 343 static int 344 biospci_find_device(uint32_t devid, int index, uint32_t *locator) 345 { 346 v86.ctl = V86_FLAGS; 347 v86.addr = PCI_INT; 348 v86.eax = FIND_PCI_DEVICE; 349 v86.edx = devid & 0xffff; /* EDX - Vendor ID */ 350 v86.ecx = (devid >> 16) & 0xffff; /* ECX - Device ID */ 351 v86.esi = index; 352 v86int(); 353 354 /* error */ 355 if (V86_CY(v86.efl) || (v86.eax & 0xff00)) 356 return (-1); 357 358 *locator = v86.ebx; 359 return (0); 360 } 361 /* 362 * Configuration space access methods. 363 * width = 0(byte), 1(word) or 2(dword). 364 */ 365 int 366 biospci_write_config(uint32_t locator, int offset, int width, uint32_t val) 367 { 368 v86.ctl = V86_FLAGS; 369 v86.addr = PCI_INT; 370 v86.eax = WRITE_CONFIG_BYTE + width; 371 v86.ebx = locator; 372 v86.edi = offset; 373 v86.ecx = val; 374 v86int(); 375 376 /* error */ 377 if (V86_CY(v86.efl) || (v86.eax & 0xff00)) 378 return (-1); 379 380 return(0); 381 } 382 383 int 384 biospci_read_config(uint32_t locator, int offset, int width, uint32_t *val) 385 { 386 v86.ctl = V86_FLAGS; 387 v86.addr = PCI_INT; 388 v86.eax = READ_CONFIG_BYTE + width; 389 v86.ebx = locator; 390 v86.edi = offset; 391 v86int(); 392 393 /* error */ 394 if (V86_CY(v86.efl) || (v86.eax & 0xff00)) 395 return (-1); 396 397 *val = v86.ecx; 398 return (0); 399 } 400 401 uint32_t 402 biospci_locator(int8_t bus, uint8_t device, uint8_t function) 403 { 404 405 return ((bus << 8) | ((device & 0x1f) << 3) | (function & 0x7)); 406 } 407 408 /* 409 * Counts the number of instances of devid we have in the system, as least as 410 * far as the PCI BIOS is able to tell. 411 */ 412 static int 413 biospci_count_device_type(uint32_t devid) 414 { 415 int i; 416 417 for (i = 0; 1; i++) { 418 v86.ctl = V86_FLAGS; 419 v86.addr = PCI_INT; 420 v86.eax = FIND_PCI_DEVICE; 421 v86.edx = devid & 0xffff; /* EDX - Vendor ID */ 422 v86.ecx = (devid >> 16) & 0xffff; /* ECX - Device ID */ 423 v86.esi = i; 424 v86int(); 425 if (V86_CY(v86.efl) || (v86.eax & 0xff00)) 426 break; 427 428 } 429 return i; 430 } 431 432 /* 433 * pcibios-device-count (devid -- count) 434 * 435 * Returns the PCI BIOS' count of how many devices matching devid are 436 * in the system. devid is the 32-bit vendor + device. 437 */ 438 static void 439 ficlPciBiosCountDevices(ficlVm *pVM) 440 { 441 uint32_t devid; 442 int i; 443 444 FICL_STACK_CHECK(ficlVmGetDataStack(pVM), 1, 1); 445 446 devid = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 447 448 i = biospci_count_device_type(devid); 449 450 ficlStackPushInteger(ficlVmGetDataStack(pVM), i); 451 } 452 453 /* 454 * pcibios-write-config (locator offset width value -- ) 455 * 456 * Writes the specified config register. 457 * Locator is bus << 8 | device << 3 | fuction 458 * offset is the pci config register 459 * width is 0 for byte, 1 for word, 2 for dword 460 * value is the value to write 461 */ 462 static void 463 ficlPciBiosWriteConfig(ficlVm *pVM) 464 { 465 uint32_t value, width, offset, locator; 466 467 FICL_STACK_CHECK(ficlVmGetDataStack(pVM), 4, 0); 468 469 value = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 470 width = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 471 offset = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 472 locator = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 473 474 biospci_write_config(locator, offset, width, value); 475 } 476 477 /* 478 * pcibios-read-config (locator offset width -- value) 479 * 480 * Reads the specified config register. 481 * Locator is bus << 8 | device << 3 | fuction 482 * offset is the pci config register 483 * width is 0 for byte, 1 for word, 2 for dword 484 * value is the value to read from the register 485 */ 486 static void 487 ficlPciBiosReadConfig(ficlVm *pVM) 488 { 489 uint32_t value, width, offset, locator; 490 491 FICL_STACK_CHECK(ficlVmGetDataStack(pVM), 3, 1); 492 493 width = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 494 offset = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 495 locator = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 496 497 biospci_read_config(locator, offset, width, &value); 498 499 ficlStackPushInteger(ficlVmGetDataStack(pVM), value); 500 } 501 502 /* 503 * pcibios-find-devclass (class index -- locator) 504 * 505 * Finds the index'th instance of class in the pci tree. 506 * must be an exact match. 507 * class is the class to search for. 508 * index 0..N (set to 0, increment until error) 509 * 510 * Locator is bus << 8 | device << 3 | fuction (or -1 on error) 511 */ 512 static void 513 ficlPciBiosFindDevclass(ficlVm *pVM) 514 { 515 uint32_t index, class, locator; 516 517 FICL_STACK_CHECK(ficlVmGetDataStack(pVM), 2, 1); 518 519 index = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 520 class = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 521 522 if (biospci_find_devclass(class, index, &locator)) 523 locator = 0xffffffff; 524 525 ficlStackPushInteger(ficlVmGetDataStack(pVM), locator); 526 } 527 528 /* 529 * pcibios-find-device(devid index -- locator) 530 * 531 * Finds the index'th instance of devid in the pci tree. 532 * must be an exact match. 533 * class is the class to search for. 534 * index 0..N (set to 0, increment until error) 535 * 536 * Locator is bus << 8 | device << 3 | fuction (or -1 on error) 537 */ 538 static void 539 ficlPciBiosFindDevice(ficlVm *pVM) 540 { 541 uint32_t index, devid, locator; 542 543 FICL_STACK_CHECK(ficlVmGetDataStack(pVM), 2, 1); 544 545 index = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 546 devid = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 547 548 if (biospci_find_device(devid, index, &locator)) 549 locator = 0xffffffff; 550 551 ficlStackPushInteger(ficlVmGetDataStack(pVM), locator); 552 } 553 554 /* 555 * pcibios-locator(bus device function -- locator) 556 * 557 * converts bus, device, function to locator. 558 * 559 * Locator is bus << 8 | device << 3 | fuction 560 */ 561 static void 562 ficlPciBiosLocator(ficlVm *pVM) 563 { 564 uint32_t bus, device, function, locator; 565 566 FICL_STACK_CHECK(ficlVmGetDataStack(pVM), 3, 1); 567 568 function = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 569 device = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 570 bus = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 571 572 locator = biospci_locator(bus, device, function); 573 574 ficlStackPushInteger(ficlVmGetDataStack(pVM), locator); 575 } 576 577 /* 578 * Glue function to add the appropriate forth words to access pci bios 579 * functionality. 580 */ 581 static void 582 ficlCompilePciBios(ficlSystem *pSys) 583 { 584 ficlDictionary *dp = ficlSystemGetDictionary(pSys); 585 586 FICL_SYSTEM_ASSERT(pSys, dp); 587 588 ficlDictionarySetPrimitive(dp, "pcibios-device-count", 589 ficlPciBiosCountDevices, FICL_WORD_DEFAULT); 590 ficlDictionarySetPrimitive(dp, "pcibios-read-config", 591 ficlPciBiosReadConfig, FICL_WORD_DEFAULT); 592 ficlDictionarySetPrimitive(dp, "pcibios-write-config", 593 ficlPciBiosWriteConfig, FICL_WORD_DEFAULT); 594 ficlDictionarySetPrimitive(dp, "pcibios-find-devclass", 595 ficlPciBiosFindDevclass, FICL_WORD_DEFAULT); 596 ficlDictionarySetPrimitive(dp, "pcibios-find-device", 597 ficlPciBiosFindDevice, FICL_WORD_DEFAULT); 598 ficlDictionarySetPrimitive(dp, "pcibios-locator", ficlPciBiosLocator, 599 FICL_WORD_DEFAULT); 600 } 601 602 FICL_COMPILE_SET(ficlCompilePciBios); 603 604 /* 605 * outb ( port# c -- ) 606 * Store a byte to I/O port number port# 607 */ 608 static void 609 ficlOutb(ficlVm *pVM) 610 { 611 uint8_t c; 612 uint32_t port; 613 614 port = ficlStackPopUnsigned(ficlVmGetDataStack(pVM)); 615 c = ficlStackPopInteger(ficlVmGetDataStack(pVM)); 616 outb(port, c); 617 } 618 619 /* 620 * inb ( port# -- c ) 621 * Fetch a byte from I/O port number port# 622 */ 623 static void 624 ficlInb(ficlVm *pVM) 625 { 626 uint8_t c; 627 uint32_t port; 628 629 port = ficlStackPopUnsigned(ficlVmGetDataStack(pVM)); 630 c = inb(port); 631 ficlStackPushInteger(ficlVmGetDataStack(pVM), c); 632 } 633 634 static void 635 ficlCompileCpufunc(ficlSystem *pSys) 636 { 637 ficlDictionary *dp = ficlSystemGetDictionary(pSys); 638 639 FICL_SYSTEM_ASSERT(pSys, dp); 640 641 ficlDictionarySetPrimitive(dp, "outb", ficlOutb, FICL_WORD_DEFAULT); 642 ficlDictionarySetPrimitive(dp, "inb", ficlInb, FICL_WORD_DEFAULT); 643 } 644 645 FICL_COMPILE_SET(ficlCompileCpufunc); 646