1 /*- 2 * Copyright (c) 1997 Michael Smith 3 * Copyright (c) 1998 Jonathan Lemon 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 * $FreeBSD$ 28 */ 29 30 /* 31 * Code for dealing with the BIOS in x86 PC systems. 32 */ 33 34 #include "isa.h" 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/bus.h> 41 #include <vm/vm.h> 42 #include <vm/pmap.h> 43 #include <machine/md_var.h> 44 #include <machine/segments.h> 45 #include <machine/stdarg.h> 46 #include <machine/vmparam.h> 47 #include <machine/pc/bios.h> 48 #include <isa/pnpreg.h> 49 #include <isa/pnpvar.h> 50 #if NISA > 0 51 #include <isa/isavar.h> 52 #endif 53 54 #define BIOS_START 0xe0000 55 #define BIOS_SIZE 0x20000 56 57 /* exported lookup results */ 58 struct bios32_SDentry PCIbios = {entry : 0}; 59 struct PnPBIOS_table *PnPBIOStable = 0; 60 61 static u_int bios32_SDCI = 0; 62 63 /* start fairly early */ 64 static void bios32_init(void *junk); 65 SYSINIT(bios32, SI_SUB_CPU, SI_ORDER_ANY, bios32_init, NULL); 66 67 /* 68 * bios32_init 69 * 70 * Locate various bios32 entities. 71 */ 72 static void 73 bios32_init(void *junk) 74 { 75 u_long sigaddr; 76 struct bios32_SDheader *sdh; 77 struct PnPBIOS_table *pt; 78 u_int8_t ck, *cv; 79 int i; 80 char *p; 81 82 /* 83 * BIOS32 Service Directory, PCI BIOS 84 */ 85 86 /* look for the signature */ 87 if ((sigaddr = bios_sigsearch(0, "_32_", 4, 16, 0)) != 0) { 88 89 /* get a virtual pointer to the structure */ 90 sdh = (struct bios32_SDheader *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr); 91 for (cv = (u_int8_t *)sdh, ck = 0, i = 0; i < (sdh->len * 16); i++) { 92 ck += cv[i]; 93 } 94 /* If checksum is OK, enable use of the entrypoint */ 95 if ((ck == 0) && (sdh->entry < (BIOS_START + BIOS_SIZE))) { 96 bios32_SDCI = BIOS_PADDRTOVADDR(sdh->entry); 97 if (bootverbose) { 98 printf("bios32: Found BIOS32 Service Directory header at %p\n", sdh); 99 printf("bios32: Entry = 0x%x (%x) Rev = %d Len = %d\n", 100 sdh->entry, bios32_SDCI, sdh->revision, sdh->len); 101 } 102 103 #ifndef PC98 104 /* Allow user override of PCI BIOS search */ 105 if (((p = getenv("machdep.bios.pci")) == NULL) || strcmp(p, "disable")) { 106 107 /* See if there's a PCI BIOS entrypoint here */ 108 PCIbios.ident.id = 0x49435024; /* PCI systems should have this */ 109 if (!bios32_SDlookup(&PCIbios) && bootverbose) 110 printf("pcibios: PCI BIOS entry at 0x%x+0x%x\n", PCIbios.base, PCIbios.entry); 111 } 112 #endif 113 } else { 114 printf("bios32: Bad BIOS32 Service Directory\n"); 115 } 116 } 117 118 /* 119 * PnP BIOS 120 * 121 * Allow user override of PnP BIOS search 122 */ 123 if ((((p = getenv("machdep.bios.pnp")) == NULL) || strcmp(p, "disable")) && 124 ((sigaddr = bios_sigsearch(0, "$PnP", 4, 16, 0)) != 0)) { 125 126 /* get a virtual pointer to the structure */ 127 pt = (struct PnPBIOS_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr); 128 for (cv = (u_int8_t *)pt, ck = 0, i = 0; i < pt->len; i++) { 129 ck += cv[i]; 130 } 131 /* If checksum is OK, enable use of the entrypoint */ 132 if (ck == 0) { 133 PnPBIOStable = pt; 134 if (bootverbose) { 135 printf("pnpbios: Found PnP BIOS data at %p\n", pt); 136 printf("pnpbios: Entry = %x:%x Rev = %d.%d\n", 137 pt->pmentrybase, pt->pmentryoffset, pt->version >> 4, pt->version & 0xf); 138 if ((pt->control & 0x3) == 0x01) 139 printf("pnpbios: Event flag at %x\n", pt->evflagaddr); 140 if (pt->oemdevid != 0) 141 printf("pnpbios: OEM ID %x\n", pt->oemdevid); 142 143 } 144 } else { 145 printf("pnpbios: Bad PnP BIOS data checksum\n"); 146 } 147 } 148 149 if (bootverbose) { 150 /* look for other know signatures */ 151 printf("Other BIOS signatures found:\n"); 152 printf("ACPI: %08x\n", bios_sigsearch(0, "RSD PTR ", 8, 16, 0)); 153 } 154 } 155 156 /* 157 * bios32_SDlookup 158 * 159 * Query the BIOS32 Service Directory for the service named in (ent), 160 * returns nonzero if the lookup fails. The caller must fill in 161 * (ent->ident), the remainder are populated on a successful lookup. 162 */ 163 int 164 bios32_SDlookup(struct bios32_SDentry *ent) 165 { 166 struct bios_regs args; 167 168 if (bios32_SDCI == 0) 169 return (1); 170 171 args.eax = ent->ident.id; /* set up arguments */ 172 args.ebx = args.ecx = args.edx = 0; 173 bios32(&args, bios32_SDCI, GSEL(GCODE_SEL, SEL_KPL)); 174 if ((args.eax & 0xff) == 0) { /* success? */ 175 ent->base = args.ebx; 176 ent->len = args.ecx; 177 ent->entry = args.edx; 178 ent->ventry = BIOS_PADDRTOVADDR(ent->base + ent->entry); 179 return (0); /* all OK */ 180 } 181 return (1); /* failed */ 182 } 183 184 185 /* 186 * bios_sigsearch 187 * 188 * Search some or all of the BIOS region for a signature string. 189 * 190 * (start) Optional offset returned from this function 191 * (for searching for multiple matches), or NULL 192 * to start the search from the base of the BIOS. 193 * Note that this will be a _physical_ address in 194 * the range 0xe0000 - 0xfffff. 195 * (sig) is a pointer to the byte(s) of the signature. 196 * (siglen) number of bytes in the signature. 197 * (paralen) signature paragraph (alignment) size. 198 * (sigofs) offset of the signature within the paragraph. 199 * 200 * Returns the _physical_ address of the found signature, 0 if the 201 * signature was not found. 202 */ 203 204 u_int32_t 205 bios_sigsearch(u_int32_t start, u_char *sig, int siglen, int paralen, int sigofs) 206 { 207 u_char *sp, *end; 208 209 /* compute the starting address */ 210 if ((start >= BIOS_START) && (start <= (BIOS_START + BIOS_SIZE))) { 211 sp = (char *)BIOS_PADDRTOVADDR(start); 212 } else if (start == 0) { 213 sp = (char *)BIOS_PADDRTOVADDR(BIOS_START); 214 } else { 215 return 0; /* bogus start address */ 216 } 217 218 /* compute the end address */ 219 end = (u_char *)BIOS_PADDRTOVADDR(BIOS_START + BIOS_SIZE); 220 221 /* loop searching */ 222 while ((sp + sigofs + siglen) < end) { 223 224 /* compare here */ 225 if (!bcmp(sp + sigofs, sig, siglen)) { 226 /* convert back to physical address */ 227 return((u_int32_t)BIOS_VADDRTOPADDR(sp)); 228 } 229 sp += paralen; 230 } 231 return(0); 232 } 233 234 /* 235 * do not staticize, used by bioscall.s 236 */ 237 union { 238 struct { 239 u_short offset; 240 u_short segment; 241 } vec16; 242 struct { 243 u_int offset; 244 u_short segment; 245 } vec32; 246 } bioscall_vector; /* bios jump vector */ 247 248 void 249 set_bios_selectors(struct bios_segments *seg, int flags) 250 { 251 struct soft_segment_descriptor ssd = { 252 0, /* segment base address (overwritten) */ 253 0, /* length (overwritten) */ 254 SDT_MEMERA, /* segment type (overwritten) */ 255 0, /* priority level */ 256 1, /* descriptor present */ 257 0, 0, 258 1, /* descriptor size (overwritten) */ 259 0 /* granularity == byte units */ 260 }; 261 union descriptor *p_gdt; 262 263 #ifdef SMP 264 p_gdt = &gdt[cpuid * NGDT]; 265 #else 266 p_gdt = gdt; 267 #endif 268 269 ssd.ssd_base = seg->code32.base; 270 ssd.ssd_limit = seg->code32.limit; 271 ssdtosd(&ssd, &p_gdt[GBIOSCODE32_SEL].sd); 272 273 ssd.ssd_def32 = 0; 274 if (flags & BIOSCODE_FLAG) { 275 ssd.ssd_base = seg->code16.base; 276 ssd.ssd_limit = seg->code16.limit; 277 ssdtosd(&ssd, &p_gdt[GBIOSCODE16_SEL].sd); 278 } 279 280 ssd.ssd_type = SDT_MEMRWA; 281 if (flags & BIOSDATA_FLAG) { 282 ssd.ssd_base = seg->data.base; 283 ssd.ssd_limit = seg->data.limit; 284 ssdtosd(&ssd, &p_gdt[GBIOSDATA_SEL].sd); 285 } 286 287 if (flags & BIOSUTIL_FLAG) { 288 ssd.ssd_base = seg->util.base; 289 ssd.ssd_limit = seg->util.limit; 290 ssdtosd(&ssd, &p_gdt[GBIOSUTIL_SEL].sd); 291 } 292 293 if (flags & BIOSARGS_FLAG) { 294 ssd.ssd_base = seg->args.base; 295 ssd.ssd_limit = seg->args.limit; 296 ssdtosd(&ssd, &p_gdt[GBIOSARGS_SEL].sd); 297 } 298 } 299 300 extern int vm86pa; 301 extern void bios16_jmp(void); 302 303 /* 304 * this routine is really greedy with selectors, and uses 5: 305 * 306 * 32-bit code selector: to return to kernel 307 * 16-bit code selector: for running code 308 * data selector: for 16-bit data 309 * util selector: extra utility selector 310 * args selector: to handle pointers 311 * 312 * the util selector is set from the util16 entry in bios16_args, if a 313 * "U" specifier is seen. 314 * 315 * See <machine/pc/bios.h> for description of format specifiers 316 */ 317 int 318 bios16(struct bios_args *args, char *fmt, ...) 319 { 320 char *p, *stack, *stack_top; 321 va_list ap; 322 int flags = BIOSCODE_FLAG | BIOSDATA_FLAG; 323 u_int i, arg_start, arg_end; 324 u_int *pte, *ptd; 325 326 arg_start = 0xffffffff; 327 arg_end = 0; 328 329 /* 330 * Some BIOS entrypoints attempt to copy the largest-case 331 * argument frame (in order to generalise handling for 332 * different entry types). If our argument frame is 333 * smaller than this, the BIOS will reach off the top of 334 * our constructed stack segment. Pad the top of the stack 335 * with some garbage to avoid this. 336 */ 337 stack = (caddr_t)PAGE_SIZE - 32; 338 339 va_start(ap, fmt); 340 for (p = fmt; p && *p; p++) { 341 switch (*p) { 342 case 'p': /* 32-bit pointer */ 343 i = va_arg(ap, u_int); 344 arg_start = min(arg_start, i); 345 arg_end = max(arg_end, i); 346 flags |= BIOSARGS_FLAG; 347 stack -= 4; 348 break; 349 350 case 'i': /* 32-bit integer */ 351 i = va_arg(ap, u_int); 352 stack -= 4; 353 break; 354 355 case 'U': /* 16-bit selector */ 356 flags |= BIOSUTIL_FLAG; 357 /* FALLTHROUGH */ 358 case 'D': /* 16-bit selector */ 359 case 'C': /* 16-bit selector */ 360 stack -= 2; 361 break; 362 363 case 's': /* 16-bit integer */ 364 i = va_arg(ap, u_short); 365 stack -= 2; 366 break; 367 368 default: 369 return (EINVAL); 370 } 371 } 372 373 if (flags & BIOSARGS_FLAG) { 374 if (arg_end - arg_start > ctob(16)) 375 return (EACCES); 376 args->seg.args.base = arg_start; 377 args->seg.args.limit = 0xffff; 378 } 379 380 args->seg.code32.base = (u_int)&bios16_jmp & PG_FRAME; 381 args->seg.code32.limit = 0xffff; 382 383 ptd = (u_int *)rcr3(); 384 if (ptd == IdlePTD) { 385 /* 386 * no page table, so create one and install it. 387 */ 388 pte = (u_int *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK); 389 ptd = (u_int *)((u_int)ptd + KERNBASE); 390 *ptd = vtophys(pte) | PG_RW | PG_V; 391 } else { 392 /* 393 * this is a user-level page table 394 */ 395 pte = (u_int *)&PTmap; 396 } 397 /* 398 * install pointer to page 0. we don't need to flush the tlb, 399 * since there should not be a previous mapping for page 0. 400 */ 401 *pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V; 402 403 stack_top = stack; 404 va_start(ap, fmt); 405 for (p = fmt; p && *p; p++) { 406 switch (*p) { 407 case 'p': /* 32-bit pointer */ 408 i = va_arg(ap, u_int); 409 *(u_int *)stack = (i - arg_start) | 410 (GSEL(GBIOSARGS_SEL, SEL_KPL) << 16); 411 stack += 4; 412 break; 413 414 case 'i': /* 32-bit integer */ 415 i = va_arg(ap, u_int); 416 *(u_int *)stack = i; 417 stack += 4; 418 break; 419 420 case 'U': /* 16-bit selector */ 421 *(u_short *)stack = GSEL(GBIOSUTIL_SEL, SEL_KPL); 422 stack += 2; 423 break; 424 425 case 'D': /* 16-bit selector */ 426 *(u_short *)stack = GSEL(GBIOSDATA_SEL, SEL_KPL); 427 stack += 2; 428 break; 429 430 case 'C': /* 16-bit selector */ 431 *(u_short *)stack = GSEL(GBIOSCODE16_SEL, SEL_KPL); 432 stack += 2; 433 break; 434 435 case 's': /* 16-bit integer */ 436 i = va_arg(ap, u_short); 437 *(u_short *)stack = i; 438 stack += 2; 439 break; 440 441 default: 442 return (EINVAL); 443 } 444 } 445 446 set_bios_selectors(&args->seg, flags); 447 bioscall_vector.vec16.offset = (u_short)args->entry; 448 bioscall_vector.vec16.segment = GSEL(GBIOSCODE16_SEL, SEL_KPL); 449 450 i = bios16_call(&args->r, stack_top); 451 452 if (pte == (u_int *)&PTmap) { 453 *pte = 0; /* remove entry */ 454 } else { 455 *ptd = 0; /* remove page table */ 456 free(pte, M_TEMP); /* ... and free it */ 457 } 458 459 /* 460 * XXX only needs to be invlpg(0) but that doesn't work on the 386 461 */ 462 invltlb(); 463 464 return (i); 465 } 466 467 /* 468 * PnP BIOS interface; enumerate devices only known to the system 469 * BIOS and save information about them for later use. 470 */ 471 472 struct pnp_sysdev 473 { 474 u_int16_t size; 475 u_int8_t handle; 476 u_int32_t devid; 477 u_int8_t type[3]; 478 u_int16_t attrib; 479 #define PNPATTR_NODISABLE (1<<0) /* can't be disabled */ 480 #define PNPATTR_NOCONFIG (1<<1) /* can't be configured */ 481 #define PNPATTR_OUTPUT (1<<2) /* can be primary output */ 482 #define PNPATTR_INPUT (1<<3) /* can be primary input */ 483 #define PNPATTR_BOOTABLE (1<<4) /* can be booted from */ 484 #define PNPATTR_DOCK (1<<5) /* is a docking station */ 485 #define PNPATTR_REMOVEABLE (1<<6) /* device is removeable */ 486 #define PNPATTR_CONFIG_STATIC 0x00 487 #define PNPATTR_CONFIG_DYNAMIC 0x07 488 #define PNPATTR_CONFIG_DYNONLY 0x17 489 /* device-specific data comes here */ 490 u_int8_t devdata[0]; 491 } __attribute__ ((packed)); 492 493 /* We have to cluster arguments within a 64k range for the bios16 call */ 494 struct pnp_sysdevargs 495 { 496 u_int16_t next; 497 struct pnp_sysdev node; 498 }; 499 500 /* 501 * This function is called after the bus has assigned resource 502 * locations for a logical device. 503 */ 504 static void 505 pnpbios_set_config(void *arg, struct isa_config *config, int enable) 506 { 507 } 508 509 /* 510 * Quiz the PnP BIOS, build a list of PNP IDs and resource data. 511 */ 512 static void 513 pnpbios_identify(driver_t *driver, device_t parent) 514 { 515 struct PnPBIOS_table *pt = PnPBIOStable; 516 struct bios_args args; 517 struct pnp_sysdev *pd; 518 struct pnp_sysdevargs *pda; 519 u_int16_t ndevs, bigdev; 520 int error, currdev; 521 u_int8_t *devnodebuf, tag; 522 u_int32_t *devid, *compid; 523 int idx, left; 524 device_t dev; 525 526 /* no PnP BIOS information */ 527 if (pt == NULL) 528 return; 529 530 bzero(&args, sizeof(args)); 531 args.seg.code16.base = BIOS_PADDRTOVADDR(pt->pmentrybase); 532 args.seg.code16.limit = 0xffff; /* XXX ? */ 533 args.seg.data.base = BIOS_PADDRTOVADDR(pt->pmdataseg); 534 args.seg.data.limit = 0xffff; 535 args.entry = pt->pmentryoffset; 536 537 if ((error = bios16(&args, PNP_COUNT_DEVNODES, &ndevs, &bigdev)) || (args.r.eax & 0xff)) 538 printf("pnpbios: error %d/%x getting device count/size limit\n", error, args.r.eax); 539 ndevs &= 0xff; /* clear high byte garbage */ 540 if (bootverbose) 541 printf("pnpbios: %d devices, largest %d bytes\n", ndevs, bigdev); 542 543 devnodebuf = malloc(bigdev + (sizeof(struct pnp_sysdevargs) - sizeof(struct pnp_sysdev)), 544 M_DEVBUF, M_NOWAIT); 545 pda = (struct pnp_sysdevargs *)devnodebuf; 546 pd = &pda->node; 547 548 for (currdev = 0, left = ndevs; (currdev != 0xff) && (left > 0); left--) { 549 550 bzero(pd, bigdev); 551 pda->next = currdev; 552 /* get current configuration */ 553 if ((error = bios16(&args, PNP_GET_DEVNODE, &pda->next, &pda->node, (u_int16_t)1))) { 554 printf("pnpbios: error %d making BIOS16 call\n", error); 555 break; 556 } 557 if ((error = (args.r.eax & 0xff))) { 558 if (bootverbose) 559 printf("pnpbios: %s 0x%x fetching node %d\n", error & 0x80 ? "error" : "warning", error, currdev); 560 if (error & 0x80) 561 break; 562 } 563 currdev = pda->next; 564 if (pd->size < sizeof(struct pnp_sysdev)) { 565 printf("pnpbios: bogus system node data, aborting scan\n"); 566 break; 567 } 568 569 /* 570 * If we are in APIC_IO mode, we should ignore the ISA PIC if it 571 * shows up. Likewise, in !APIC_IO mode, we should ignore the 572 * APIC (less important). 573 * This is significant because the ISA PIC will claim IRQ 2 (which 574 * it uses for chaining), while in APIC mode this is a valid IRQ 575 * available for general use. 576 */ 577 #ifdef APIC_IO 578 if (!strcmp(pnp_eisaformat(pd->devid), "PNP0000")) /* ISA PIC */ 579 continue; 580 #else 581 if (!strcmp(pnp_eisaformat(pd->devid), "PNP0003")) /* APIC */ 582 continue; 583 #endif 584 585 /* Add the device and parse its resources */ 586 dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1); 587 isa_set_vendorid(dev, pd->devid); 588 isa_set_logicalid(dev, pd->devid); 589 ISA_SET_CONFIG_CALLBACK(parent, dev, pnpbios_set_config, 0); 590 pnp_parse_resources(dev, &pd->devdata[0], 591 pd->size - sizeof(struct pnp_sysdev), 592 isa_get_vendorid(dev), isa_get_logicalid(dev), 0); 593 if (!device_get_desc(dev)) 594 device_set_desc_copy(dev, pnp_eisaformat(pd->devid)); 595 596 /* Find device IDs */ 597 devid = &pd->devid; 598 compid = NULL; 599 600 /* look for a compatible device ID too */ 601 left = pd->size - sizeof(struct pnp_sysdev); 602 idx = 0; 603 while (idx < left) { 604 tag = pd->devdata[idx++]; 605 if (PNP_RES_TYPE(tag) == 0) { 606 /* Small resource */ 607 switch (PNP_SRES_NUM(tag)) { 608 case PNP_TAG_COMPAT_DEVICE: 609 compid = (u_int32_t *)(pd->devdata + idx); 610 if (bootverbose) 611 printf("pnpbios: node %d compat ID 0x%08x\n", pd->handle, *compid); 612 /* FALLTHROUGH */ 613 case PNP_TAG_END: 614 idx = left; 615 break; 616 default: 617 idx += PNP_SRES_LEN(tag); 618 break; 619 } 620 } else 621 /* Large resource, skip it */ 622 idx += *(u_int16_t *)(pd->devdata + idx) + 2; 623 } 624 if (bootverbose) { 625 printf("pnpbios: handle %d device ID %s (%08x)", 626 pd->handle, pnp_eisaformat(*devid), *devid); 627 if (compid != NULL) 628 printf(" compat ID %s (%08x)", 629 pnp_eisaformat(*compid), *compid); 630 printf("\n"); 631 } 632 } 633 } 634 635 static device_method_t pnpbios_methods[] = { 636 /* Device interface */ 637 DEVMETHOD(device_identify, pnpbios_identify), 638 639 { 0, 0 } 640 }; 641 642 static driver_t pnpbios_driver = { 643 "pnpbios", 644 pnpbios_methods, 645 1, /* no softc */ 646 }; 647 648 static devclass_t pnpbios_devclass; 649 650 DRIVER_MODULE(pnpbios, isa, pnpbios_driver, pnpbios_devclass, 0, 0); 651