2 * Copyright (c) 1997 Michael Smith
3 * Copyright (c) 1998 Jonathan Lemon
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * $FreeBSD: src/sys/i386/i386/bios.c,v 1.29.2.3 2001/07/19 18:07:35 imp Exp $
28 * $DragonFly: src/sys/platform/pc32/i386/bios.c,v 1.6 2003/11/07 18:28:52 dillon Exp $
32 * Code for dealing with the BIOS in x86 PC systems.
36 /*#include "opt_isa.h"*/
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
45 #include <machine/md_var.h>
46 #include <machine/globaldata.h>
47 #include <machine/thread.h>
48 #include <machine/segments.h>
49 #include <machine/stdarg.h>
50 #include <machine/vmparam.h>
51 #include <machine/pc/bios.h>
52 #include <bus/isa/pnpreg.h>
53 #include <bus/isa/pnpvar.h>
54 #include <bus/isa/isavar.h>
56 #define BIOS_START 0xe0000
57 #define BIOS_SIZE 0x20000
59 /* exported lookup results */
60 struct bios32_SDentry PCIbios;
61 struct PnPBIOS_table *PnPBIOStable;
63 static u_int bios32_SDCI;
65 /* start fairly early */
66 static void bios32_init(void *junk);
67 SYSINIT(bios32, SI_SUB_CPU, SI_ORDER_ANY, bios32_init, NULL);
72 * Locate various bios32 entities.
75 bios32_init(void *junk)
78 struct bios32_SDheader *sdh;
79 struct PnPBIOS_table *pt;
85 * BIOS32 Service Directory, PCI BIOS
88 /* look for the signature */
89 if ((sigaddr = bios_sigsearch(0, "_32_", 4, 16, 0)) != 0) {
91 /* get a virtual pointer to the structure */
92 sdh = (struct bios32_SDheader *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
93 for (cv = (u_int8_t *)sdh, ck = 0, i = 0; i < (sdh->len * 16); i++) {
96 /* If checksum is OK, enable use of the entrypoint */
97 if ((ck == 0) && BIOS_START <= sdh->entry &&
98 (sdh->entry < (BIOS_START + BIOS_SIZE))
100 bios32_SDCI = BIOS_PADDRTOVADDR(sdh->entry);
102 printf("bios32: Found BIOS32 Service Directory header at %p\n", sdh);
103 printf("bios32: Entry = 0x%x (%x) Rev = %d Len = %d\n",
104 sdh->entry, bios32_SDCI, sdh->revision, sdh->len);
107 /* Allow user override of PCI BIOS search */
108 if (((p = getenv("machdep.bios.pci")) == NULL) || strcmp(p, "disable")) {
109 /* See if there's a PCI BIOS entrypoint here */
110 PCIbios.ident.id = 0x49435024; /* PCI systems should have this */
111 if (!bios32_SDlookup(&PCIbios) && bootverbose)
112 printf("pcibios: PCI BIOS entry at 0x%x\n", PCIbios.entry);
117 printf("bios32: Bad BIOS32 Service Directory\n");
124 * Allow user override of PnP BIOS search
126 if (((p = getenv("machdep.bios.pnp")) == NULL || strcmp(p, "disable")) &&
127 (sigaddr = bios_sigsearch(0, "$PnP", 4, 16, 0)) != 0
129 /* get a virtual pointer to the structure */
130 pt = (struct PnPBIOS_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
131 for (cv = (u_int8_t *)pt, ck = 0, i = 0; i < pt->len; i++) {
134 /* If checksum is OK, enable use of the entrypoint */
138 printf("pnpbios: Found PnP BIOS data at %p\n", pt);
139 printf("pnpbios: Entry = %x:%x Rev = %d.%d\n",
140 pt->pmentrybase, pt->pmentryoffset, pt->version >> 4, pt->version & 0xf);
141 if ((pt->control & 0x3) == 0x01)
142 printf("pnpbios: Event flag at %x\n", pt->evflagaddr);
143 if (pt->oemdevid != 0)
144 printf("pnpbios: OEM ID %x\n", pt->oemdevid);
148 printf("pnpbios: Bad PnP BIOS data checksum\n");
154 /* look for other know signatures */
155 printf("Other BIOS signatures found:\n");
162 * Query the BIOS32 Service Directory for the service named in (ent),
163 * returns nonzero if the lookup fails. The caller must fill in
164 * (ent->ident), the remainder are populated on a successful lookup.
167 bios32_SDlookup(struct bios32_SDentry *ent)
169 struct bios_regs args;
171 if (bios32_SDCI == 0)
174 args.eax = ent->ident.id; /* set up arguments */
175 args.ebx = args.ecx = args.edx = 0;
176 bios32(&args, bios32_SDCI, GSEL(GCODE_SEL, SEL_KPL));
177 if ((args.eax & 0xff) == 0) { /* success? */
178 ent->base = args.ebx;
180 ent->entry = args.edx;
181 ent->ventry = BIOS_PADDRTOVADDR(ent->base + ent->entry);
182 return (0); /* all OK */
184 return (1); /* failed */
191 * Search some or all of the BIOS region for a signature string.
193 * (start) Optional offset returned from this function
194 * (for searching for multiple matches), or NULL
195 * to start the search from the base of the BIOS.
196 * Note that this will be a _physical_ address in
197 * the range 0xe0000 - 0xfffff.
198 * (sig) is a pointer to the byte(s) of the signature.
199 * (siglen) number of bytes in the signature.
200 * (paralen) signature paragraph (alignment) size.
201 * (sigofs) offset of the signature within the paragraph.
203 * Returns the _physical_ address of the found signature, 0 if the
204 * signature was not found.
208 bios_sigsearch(u_int32_t start, u_char *sig, int siglen, int paralen, int sigofs)
212 /* compute the starting address */
213 if ((start >= BIOS_START) && (start <= (BIOS_START + BIOS_SIZE))) {
214 sp = (char *)BIOS_PADDRTOVADDR(start);
215 } else if (start == 0) {
216 sp = (char *)BIOS_PADDRTOVADDR(BIOS_START);
218 return 0; /* bogus start address */
221 /* compute the end address */
222 end = (u_char *)BIOS_PADDRTOVADDR(BIOS_START + BIOS_SIZE);
225 while ((sp + sigofs + siglen) < end) {
228 if (!bcmp(sp + sigofs, sig, siglen)) {
229 /* convert back to physical address */
230 return((u_int32_t)BIOS_VADDRTOPADDR(sp));
238 * do not staticize, used by bioscall.s
249 } bioscall_vector; /* bios jump vector */
252 set_bios_selectors(struct bios_segments *seg, int flags)
254 struct soft_segment_descriptor ssd = {
255 0, /* segment base address (overwritten) */
256 0, /* length (overwritten) */
257 SDT_MEMERA, /* segment type (overwritten) */
258 0, /* priority level */
259 1, /* descriptor present */
261 1, /* descriptor size (overwritten) */
262 0 /* granularity == byte units */
264 union descriptor *p_gdt;
266 p_gdt = &gdt[mycpu->gd_cpuid * NGDT];
268 ssd.ssd_base = seg->code32.base;
269 ssd.ssd_limit = seg->code32.limit;
270 ssdtosd(&ssd, &p_gdt[GBIOSCODE32_SEL].sd);
273 if (flags & BIOSCODE_FLAG) {
274 ssd.ssd_base = seg->code16.base;
275 ssd.ssd_limit = seg->code16.limit;
276 ssdtosd(&ssd, &p_gdt[GBIOSCODE16_SEL].sd);
279 ssd.ssd_type = SDT_MEMRWA;
280 if (flags & BIOSDATA_FLAG) {
281 ssd.ssd_base = seg->data.base;
282 ssd.ssd_limit = seg->data.limit;
283 ssdtosd(&ssd, &p_gdt[GBIOSDATA_SEL].sd);
286 if (flags & BIOSUTIL_FLAG) {
287 ssd.ssd_base = seg->util.base;
288 ssd.ssd_limit = seg->util.limit;
289 ssdtosd(&ssd, &p_gdt[GBIOSUTIL_SEL].sd);
292 if (flags & BIOSARGS_FLAG) {
293 ssd.ssd_base = seg->args.base;
294 ssd.ssd_limit = seg->args.limit;
295 ssdtosd(&ssd, &p_gdt[GBIOSARGS_SEL].sd);
300 extern void bios16_jmp(void);
303 * this routine is really greedy with selectors, and uses 5:
305 * 32-bit code selector: to return to kernel
306 * 16-bit code selector: for running code
307 * data selector: for 16-bit data
308 * util selector: extra utility selector
309 * args selector: to handle pointers
311 * the util selector is set from the util16 entry in bios16_args, if a
312 * "U" specifier is seen.
314 * See <machine/pc/bios.h> for description of format specifiers
317 bios16(struct bios_args *args, char *fmt, ...)
319 char *p, *stack, *stack_top;
321 int flags = BIOSCODE_FLAG | BIOSDATA_FLAG;
322 u_int i, arg_start, arg_end;
326 arg_start = 0xffffffff;
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.
337 stack = (caddr_t)PAGE_SIZE - 32;
340 for (p = fmt; p && *p; 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;
350 case 'i': /* 32-bit integer */
351 i = va_arg(ap, u_int);
355 case 'U': /* 16-bit selector */
356 flags |= BIOSUTIL_FLAG;
358 case 'D': /* 16-bit selector */
359 case 'C': /* 16-bit selector */
363 case 's': /* 16-bit integer passed as an int */
373 if (flags & BIOSARGS_FLAG) {
374 if (arg_end - arg_start > ctob(16))
376 args->seg.args.base = arg_start;
377 args->seg.args.limit = 0xffff;
380 args->seg.code32.base = (u_int)&bios16_jmp & PG_FRAME;
381 args->seg.code32.limit = 0xffff;
383 ptd = (pd_entry_t *)rcr3();
384 if ((pd_entry_t)ptd == IdlePTD) {
386 * no page table, so create one and install it.
388 pte = malloc(PAGE_SIZE, M_TEMP, M_WAITOK|M_ZERO);
389 ptd = (pd_entry_t *)((vm_offset_t)ptd + KERNBASE);
390 *ptd = vtophys(pte) | PG_RW | PG_V;
393 * this is a user-level page table
398 * install pointer to page 0. flush the tlb for safety.
400 *pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V;
405 for (p = fmt; p && *p; 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);
414 case 'i': /* 32-bit integer */
415 i = va_arg(ap, u_int);
420 case 'U': /* 16-bit selector */
421 *(u_short *)stack = GSEL(GBIOSUTIL_SEL, SEL_KPL);
425 case 'D': /* 16-bit selector */
426 *(u_short *)stack = GSEL(GBIOSDATA_SEL, SEL_KPL);
430 case 'C': /* 16-bit selector */
431 *(u_short *)stack = GSEL(GBIOSCODE16_SEL, SEL_KPL);
435 case 's': /* 16-bit integer passed as an int */
437 *(u_short *)stack = i;
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);
450 i = bios16_call(&args->r, stack_top);
453 *pte = 0; /* remove entry */
455 *ptd = 0; /* remove page table */
456 free(pte, M_TEMP); /* ... and free it */
459 * XXX only needs to be invlpg(0) but that doesn't work on the 386
465 #ifdef PNPBIOS /* remove conditional later */
468 * PnP BIOS interface; enumerate devices only known to the system
469 * BIOS and save information about them for later use.
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 0x01
488 #define PNPATTR_CONFIG_DYNONLY 0x03
489 #define PNPATTR_CONFIG(a) (((a) >> 7) & 0x03)
490 /* device-specific data comes here */
494 /* We have to cluster arguments within a 64k range for the bios16 call */
495 struct pnp_sysdevargs
498 struct pnp_sysdev node;
502 * This function is called after the bus has assigned resource
503 * locations for a logical device.
506 pnpbios_set_config(void *arg, struct isa_config *config, int enable)
511 * Quiz the PnP BIOS, build a list of PNP IDs and resource data.
514 pnpbios_identify(driver_t *driver, device_t parent)
516 struct PnPBIOS_table *pt = PnPBIOStable;
517 struct bios_args args;
518 struct pnp_sysdev *pd;
519 struct pnp_sysdevargs *pda;
520 u_int16_t ndevs, bigdev;
522 u_int8_t *devnodebuf, tag;
523 u_int32_t *devid, *compid;
527 /* no PnP BIOS information */
531 /* ACPI already active */
532 if (devlass_get_softc(devclass_find("ACPI"), 0) != NULL)
535 bzero(&args, sizeof(args));
536 args.seg.code16.base = BIOS_PADDRTOVADDR(pt->pmentrybase);
537 args.seg.code16.limit = 0xffff; /* XXX ? */
538 args.seg.data.base = BIOS_PADDRTOVADDR(pt->pmdataseg);
539 args.seg.data.limit = 0xffff;
540 args.entry = pt->pmentryoffset;
542 if ((error = bios16(&args, PNP_COUNT_DEVNODES, &ndevs, &bigdev)) || (args.r.eax & 0xff))
543 printf("pnpbios: error %d/%x getting device count/size limit\n", error, args.r.eax);
544 ndevs &= 0xff; /* clear high byte garbage */
546 printf("pnpbios: %d devices, largest %d bytes\n", ndevs, bigdev);
548 devnodebuf = malloc(bigdev + (sizeof(struct pnp_sysdevargs) - sizeof(struct pnp_sysdev)), M_DEVBUF, M_NOWAIT);
549 pda = (struct pnp_sysdevargs *)devnodebuf;
552 for (currdev = 0, left = ndevs; (currdev != 0xff) && (left > 0); left--) {
555 /* get current configuration */
556 if ((error = bios16(&args, PNP_GET_DEVNODE, &pda->next, &pda->node, 1))) {
557 printf("pnpbios: error %d making BIOS16 call\n", error);
560 if ((error = (args.r.eax & 0xff))) {
562 printf("pnpbios: %s 0x%x fetching node %d\n", error & 0x80 ? "error" : "warning", error, currdev);
567 if (pd->size < sizeof(struct pnp_sysdev)) {
568 printf("pnpbios: bogus system node data, aborting scan\n");
573 * Ignore PICs so that we don't have to worry about the PICs
574 * claiming IRQs to prevent their use. The PIC drivers
575 * already ensure that invalid IRQs are not used.
577 if (!strcmp(pnp_eisaformat(pd->devid), "PNP0000")) /* ISA PIC */
579 if (!strcmp(pnp_eisaformat(pd->devid), "PNP0003")) /* APIC */
582 /* Add the device and parse its resources */
583 dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
584 isa_set_vendorid(dev, pd->devid);
585 isa_set_logicalid(dev, pd->devid);
588 * It appears that some PnP BIOS doesn't allow us to re-enable
589 * the embedded system device once it is disabled. We shall
590 * mark all system device nodes as "cannot be disabled", regardless
591 * of actual settings in the device attribute byte.
593 isa_set_configattr(dev,
594 ((pd->attrib & PNPATTR_NODISABLE) ? 0 : ISACFGATTR_CANDISABLE) |
595 ((!(pd->attrib & PNPATTR_NOCONFIG) &&
596 PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
597 ? ISACFGATTR_DYNAMIC : 0));
599 isa_set_configattr(dev,
600 (!(pd->attrib & PNPATTR_NOCONFIG) &&
601 PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
602 ? ISACFGATTR_DYNAMIC : 0);
604 ISA_SET_CONFIG_CALLBACK(parent, dev, pnpbios_set_config, 0);
605 pnp_parse_resources(dev, &pd->devdata[0],
606 pd->size - sizeof(struct pnp_sysdev));
607 if (!device_get_desc(dev))
608 device_set_desc_copy(dev, pnp_eisaformat(pd->devid));
610 /* Find device IDs */
614 /* look for a compatible device ID too */
615 left = pd->size - sizeof(struct pnp_sysdev);
618 tag = pd->devdata[idx++];
619 if (PNP_RES_TYPE(tag) == 0) {
621 switch (PNP_SRES_NUM(tag)) {
622 case PNP_TAG_COMPAT_DEVICE:
623 compid = (u_int32_t *)(pd->devdata + idx);
625 printf("pnpbios: node %d compat ID 0x%08x\n", pd->handle, *compid);
631 idx += PNP_SRES_LEN(tag);
635 /* Large resource, skip it */
636 idx += *(u_int16_t *)(pd->devdata + idx) + 2;
639 printf("pnpbios: handle %d device ID %s (%08x)",
640 pd->handle, pnp_eisaformat(*devid), *devid);
642 printf(" compat ID %s (%08x)",
643 pnp_eisaformat(*compid), *compid);
649 static device_method_t pnpbios_methods[] = {
650 /* Device interface */
651 DEVMETHOD(device_identify, pnpbios_identify),
656 static driver_t pnpbios_driver = {
662 static devclass_t pnpbios_devclass;
664 DRIVER_MODULE(pnpbios, isa, pnpbios_driver, pnpbios_devclass, 0, 0);