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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * Code for dealing with the BIOS in x86 PC systems.
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
46 #include <machine/md_var.h>
47 #include <machine/segments.h>
48 #include <machine/stdarg.h>
49 #include <machine/vmparam.h>
50 #include <machine/pc/bios.h>
52 #include <isa/isavar.h>
53 #include <isa/pnpreg.h>
54 #include <isa/pnpvar.h>
57 #define BIOS_START 0xe0000
58 #define BIOS_SIZE 0x20000
60 /* exported lookup results */
61 struct bios32_SDentry PCIbios;
63 static struct PnPBIOS_table *PnPBIOStable;
65 static u_int bios32_SDCI;
67 /* start fairly early */
68 static void bios32_init(void *junk);
69 SYSINIT(bios32, SI_SUB_CPU, SI_ORDER_ANY, bios32_init, NULL);
74 * Locate various bios32 entities.
77 bios32_init(void *junk)
80 struct bios32_SDheader *sdh;
81 struct PnPBIOS_table *pt;
87 * BIOS32 Service Directory, PCI BIOS
90 /* look for the signature */
91 if ((sigaddr = bios_sigsearch(0, "_32_", 4, 16, 0)) != 0) {
93 /* get a virtual pointer to the structure */
94 sdh = (struct bios32_SDheader *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
95 for (cv = (u_int8_t *)sdh, ck = 0, i = 0; i < (sdh->len * 16); i++) {
98 /* If checksum is OK, enable use of the entrypoint */
99 if ((ck == 0) && (BIOS_START <= sdh->entry ) &&
100 (sdh->entry < (BIOS_START + BIOS_SIZE))) {
101 bios32_SDCI = BIOS_PADDRTOVADDR(sdh->entry);
103 printf("bios32: Found BIOS32 Service Directory header at %p\n", sdh);
104 printf("bios32: Entry = 0x%x (%x) Rev = %d Len = %d\n",
105 sdh->entry, bios32_SDCI, sdh->revision, sdh->len);
108 /* Allow user override of PCI BIOS search */
109 if (((p = getenv("machdep.bios.pci")) == NULL) || strcmp(p, "disable")) {
111 /* See if there's a PCI BIOS entrypoint here */
112 PCIbios.ident.id = 0x49435024; /* PCI systems should have this */
113 if (!bios32_SDlookup(&PCIbios) && bootverbose)
114 printf("pcibios: PCI BIOS entry at 0x%x+0x%x\n", PCIbios.base, PCIbios.entry);
119 printf("bios32: Bad BIOS32 Service Directory\n");
126 * Allow user override of PnP BIOS search
128 if ((((p = getenv("machdep.bios.pnp")) == NULL) || strcmp(p, "disable")) &&
129 ((sigaddr = bios_sigsearch(0, "$PnP", 4, 16, 0)) != 0)) {
131 /* get a virtual pointer to the structure */
132 pt = (struct PnPBIOS_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
133 for (cv = (u_int8_t *)pt, ck = 0, i = 0; i < pt->len; i++) {
136 /* If checksum is OK, enable use of the entrypoint */
140 printf("pnpbios: Found PnP BIOS data at %p\n", pt);
141 printf("pnpbios: Entry = %x:%x Rev = %d.%d\n",
142 pt->pmentrybase, pt->pmentryoffset, pt->version >> 4, pt->version & 0xf);
143 if ((pt->control & 0x3) == 0x01)
144 printf("pnpbios: Event flag at %x\n", pt->evflagaddr);
145 if (pt->oemdevid != 0)
146 printf("pnpbios: OEM ID %x\n", pt->oemdevid);
150 printf("pnpbios: Bad PnP BIOS data checksum\n");
156 /* look for other know signatures */
157 printf("Other BIOS signatures found:\n");
164 * Query the BIOS32 Service Directory for the service named in (ent),
165 * returns nonzero if the lookup fails. The caller must fill in
166 * (ent->ident), the remainder are populated on a successful lookup.
169 bios32_SDlookup(struct bios32_SDentry *ent)
171 struct bios_regs args;
173 if (bios32_SDCI == 0)
176 args.eax = ent->ident.id; /* set up arguments */
177 args.ebx = args.ecx = args.edx = 0;
178 bios32(&args, bios32_SDCI, GSEL(GCODE_SEL, SEL_KPL));
179 if ((args.eax & 0xff) == 0) { /* success? */
180 ent->base = args.ebx;
182 ent->entry = args.edx;
183 ent->ventry = BIOS_PADDRTOVADDR(ent->base + ent->entry);
184 return (0); /* all OK */
186 return (1); /* failed */
193 * Search some or all of the BIOS region for a signature string.
195 * (start) Optional offset returned from this function
196 * (for searching for multiple matches), or NULL
197 * to start the search from the base of the BIOS.
198 * Note that this will be a _physical_ address in
199 * the range 0xe0000 - 0xfffff.
200 * (sig) is a pointer to the byte(s) of the signature.
201 * (siglen) number of bytes in the signature.
202 * (paralen) signature paragraph (alignment) size.
203 * (sigofs) offset of the signature within the paragraph.
205 * Returns the _physical_ address of the found signature, 0 if the
206 * signature was not found.
210 bios_sigsearch(u_int32_t start, u_char *sig, int siglen, int paralen, int sigofs)
214 /* compute the starting address */
215 if ((start >= BIOS_START) && (start <= (BIOS_START + BIOS_SIZE))) {
216 sp = (char *)BIOS_PADDRTOVADDR(start);
217 } else if (start == 0) {
218 sp = (char *)BIOS_PADDRTOVADDR(BIOS_START);
220 return 0; /* bogus start address */
223 /* compute the end address */
224 end = (u_char *)BIOS_PADDRTOVADDR(BIOS_START + BIOS_SIZE);
227 while ((sp + sigofs + siglen) < end) {
230 if (!bcmp(sp + sigofs, sig, siglen)) {
231 /* convert back to physical address */
232 return((u_int32_t)BIOS_VADDRTOPADDR(sp));
240 * do not staticize, used by bioscall.s
251 } bioscall_vector; /* bios jump vector */
254 set_bios_selectors(struct bios_segments *seg, int flags)
256 struct soft_segment_descriptor ssd = {
257 0, /* segment base address (overwritten) */
258 0, /* length (overwritten) */
259 SDT_MEMERA, /* segment type (overwritten) */
260 0, /* priority level */
261 1, /* descriptor present */
263 1, /* descriptor size (overwritten) */
264 0 /* granularity == byte units */
266 union descriptor *p_gdt;
269 p_gdt = &gdt[PCPU_GET(cpuid) * NGDT];
274 ssd.ssd_base = seg->code32.base;
275 ssd.ssd_limit = seg->code32.limit;
276 ssdtosd(&ssd, &p_gdt[GBIOSCODE32_SEL].sd);
279 if (flags & BIOSCODE_FLAG) {
280 ssd.ssd_base = seg->code16.base;
281 ssd.ssd_limit = seg->code16.limit;
282 ssdtosd(&ssd, &p_gdt[GBIOSCODE16_SEL].sd);
285 ssd.ssd_type = SDT_MEMRWA;
286 if (flags & BIOSDATA_FLAG) {
287 ssd.ssd_base = seg->data.base;
288 ssd.ssd_limit = seg->data.limit;
289 ssdtosd(&ssd, &p_gdt[GBIOSDATA_SEL].sd);
292 if (flags & BIOSUTIL_FLAG) {
293 ssd.ssd_base = seg->util.base;
294 ssd.ssd_limit = seg->util.limit;
295 ssdtosd(&ssd, &p_gdt[GBIOSUTIL_SEL].sd);
298 if (flags & BIOSARGS_FLAG) {
299 ssd.ssd_base = seg->args.base;
300 ssd.ssd_limit = seg->args.limit;
301 ssdtosd(&ssd, &p_gdt[GBIOSARGS_SEL].sd);
306 extern void bios16_jmp(void);
309 * this routine is really greedy with selectors, and uses 5:
311 * 32-bit code selector: to return to kernel
312 * 16-bit code selector: for running code
313 * data selector: for 16-bit data
314 * util selector: extra utility selector
315 * args selector: to handle pointers
317 * the util selector is set from the util16 entry in bios16_args, if a
318 * "U" specifier is seen.
320 * See <machine/pc/bios.h> for description of format specifiers
323 bios16(struct bios_args *args, char *fmt, ...)
325 char *p, *stack, *stack_top;
327 int flags = BIOSCODE_FLAG | BIOSDATA_FLAG;
328 u_int i, arg_start, arg_end;
332 arg_start = 0xffffffff;
336 * Some BIOS entrypoints attempt to copy the largest-case
337 * argument frame (in order to generalise handling for
338 * different entry types). If our argument frame is
339 * smaller than this, the BIOS will reach off the top of
340 * our constructed stack segment. Pad the top of the stack
341 * with some garbage to avoid this.
343 stack = (caddr_t)PAGE_SIZE - 32;
346 for (p = fmt; p && *p; p++) {
348 case 'p': /* 32-bit pointer */
349 i = va_arg(ap, u_int);
350 arg_start = min(arg_start, i);
351 arg_end = max(arg_end, i);
352 flags |= BIOSARGS_FLAG;
356 case 'i': /* 32-bit integer */
357 i = va_arg(ap, u_int);
361 case 'U': /* 16-bit selector */
362 flags |= BIOSUTIL_FLAG;
364 case 'D': /* 16-bit selector */
365 case 'C': /* 16-bit selector */
369 case 's': /* 16-bit integer passed as an int */
381 if (flags & BIOSARGS_FLAG) {
382 if (arg_end - arg_start > ctob(16))
384 args->seg.args.base = arg_start;
385 args->seg.args.limit = 0xffff;
388 args->seg.code32.base = (u_int)&bios16_jmp & PG_FRAME;
389 args->seg.code32.limit = 0xffff;
391 ptd = (pd_entry_t *)rcr3();
399 * no page table, so create one and install it.
401 pte = (pt_entry_t *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
402 ptd = (pd_entry_t *)((u_int)IdlePTD + KERNBASE);
403 *pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V;
404 *ptd = vtophys(pte) | PG_RW | PG_V;
407 * this is a user-level page table
410 *pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V;
412 pmap_invalidate_all(kernel_pmap); /* XXX insurance for now */
416 for (p = fmt; p && *p; p++) {
418 case 'p': /* 32-bit pointer */
419 i = va_arg(ap, u_int);
420 *(u_int *)stack = (i - arg_start) |
421 (GSEL(GBIOSARGS_SEL, SEL_KPL) << 16);
425 case 'i': /* 32-bit integer */
426 i = va_arg(ap, u_int);
431 case 'U': /* 16-bit selector */
432 *(u_short *)stack = GSEL(GBIOSUTIL_SEL, SEL_KPL);
436 case 'D': /* 16-bit selector */
437 *(u_short *)stack = GSEL(GBIOSDATA_SEL, SEL_KPL);
441 case 'C': /* 16-bit selector */
442 *(u_short *)stack = GSEL(GBIOSCODE16_SEL, SEL_KPL);
446 case 's': /* 16-bit integer passed as an int */
448 *(u_short *)stack = i;
459 set_bios_selectors(&args->seg, flags);
460 bioscall_vector.vec16.offset = (u_short)args->entry;
461 bioscall_vector.vec16.segment = GSEL(GBIOSCODE16_SEL, SEL_KPL);
463 i = bios16_call(&args->r, stack_top);
466 *pte = 0; /* remove entry */
468 * XXX only needs to be invlpg(0) but that doesn't work on the 386
470 pmap_invalidate_all(kernel_pmap);
472 *ptd = 0; /* remove page table */
474 * XXX only needs to be invlpg(0) but that doesn't work on the 386
476 pmap_invalidate_all(kernel_pmap);
477 free(pte, M_TEMP); /* ... and free it */
483 bios_oem_strings(struct bios_oem *oem, u_char *buffer, size_t maxlen)
486 struct bios_oem_signature *sig;
488 u_char c, *s, *se, *str, *bios_str;
489 size_t i, off, len, tot;
491 if ( !oem || !buffer || maxlen<2 )
494 sig = oem->signature;
498 from = oem->range.from;
500 if ( (to<=from) || (from<BIOS_START) || (to>(BIOS_START+BIOS_SIZE)) )
503 while (sig->anchor != NULL) {
508 /* make sure offset doesn't go beyond bios area */
509 if ( (to+off)>(BIOS_START+BIOS_SIZE) ||
510 ((from+off)<BIOS_START) ) {
511 printf("sys/i386/i386/bios.c: sig '%s' "
512 "from 0x%0x to 0x%0x offset %d "
513 "out of BIOS bounds 0x%0x - 0x%0x\n",
515 BIOS_START, BIOS_START+BIOS_SIZE);
518 /* make sure we don't overrun return buffer */
519 if (idx + tot > maxlen - 1) {
520 printf("sys/i386/i386/bios.c: sig '%s' "
521 "idx %d + tot %d = %d > maxlen-1 %d\n",
522 str, idx, tot, idx+tot, maxlen-1);
526 s = (u_char *)BIOS_PADDRTOVADDR(from);
527 se = (u_char *)BIOS_PADDRTOVADDR(to-len);
529 if (!bcmp(str, s, len)) {
535 * store pretty version of totlen bytes of bios string with
536 * given offset; 0x20 - 0x7E are printable; uniquify spaces
539 for (i=0; i<tot; i++) {
541 if ( (c < 0x20) || (c > 0x7E) )
546 } else if ( (c != ' ') ||
547 ((c == ' ') && (buffer[idx-1] != ' ')) )
553 /* remove a final trailing space */
554 if ( (idx > 1) && (buffer[idx-1] == ' ') )
562 * PnP BIOS interface; enumerate devices only known to the system
563 * BIOS and save information about them for later use.
573 #define PNPATTR_NODISABLE (1<<0) /* can't be disabled */
574 #define PNPATTR_NOCONFIG (1<<1) /* can't be configured */
575 #define PNPATTR_OUTPUT (1<<2) /* can be primary output */
576 #define PNPATTR_INPUT (1<<3) /* can be primary input */
577 #define PNPATTR_BOOTABLE (1<<4) /* can be booted from */
578 #define PNPATTR_DOCK (1<<5) /* is a docking station */
579 #define PNPATTR_REMOVEABLE (1<<6) /* device is removeable */
580 #define PNPATTR_CONFIG_STATIC (0)
581 #define PNPATTR_CONFIG_DYNAMIC (1)
582 #define PNPATTR_CONFIG_DYNONLY (3)
583 #define PNPATTR_CONFIG(a) (((a) >> 7) & 0x3)
584 /* device-specific data comes here */
588 /* We have to cluster arguments within a 64k range for the bios16 call */
589 struct pnp_sysdevargs
592 struct pnp_sysdev node;
596 * This function is called after the bus has assigned resource
597 * locations for a logical device.
600 pnpbios_set_config(void *arg, struct isa_config *config, int enable)
605 * Quiz the PnP BIOS, build a list of PNP IDs and resource data.
608 pnpbios_identify(driver_t *driver, device_t parent)
610 struct PnPBIOS_table *pt = PnPBIOStable;
611 struct bios_args args;
612 struct pnp_sysdev *pd;
613 struct pnp_sysdevargs *pda;
614 u_int16_t ndevs, bigdev;
616 u_int8_t *devnodebuf, tag;
617 u_int32_t *devid, *compid;
621 /* no PnP BIOS information */
625 /* Check to see if ACPI is already active. */
626 dev = devclass_get_device(devclass_find("acpi"), 0);
627 if (dev != NULL && device_is_attached(dev))
630 /* get count of PnP devices */
631 bzero(&args, sizeof(args));
632 args.seg.code16.base = BIOS_PADDRTOVADDR(pt->pmentrybase);
633 args.seg.code16.limit = 0xffff; /* XXX ? */
634 args.seg.data.base = BIOS_PADDRTOVADDR(pt->pmdataseg);
635 args.seg.data.limit = 0xffff;
636 args.entry = pt->pmentryoffset;
638 if ((error = bios16(&args, PNP_COUNT_DEVNODES, &ndevs, &bigdev)) || (args.r.eax & 0xff)) {
639 printf("pnpbios: error %d/%x getting device count/size limit\n", error, args.r.eax);
642 ndevs &= 0xff; /* clear high byte garbage */
644 printf("pnpbios: %d devices, largest %d bytes\n", ndevs, bigdev);
646 devnodebuf = malloc(bigdev + (sizeof(struct pnp_sysdevargs) - sizeof(struct pnp_sysdev)),
648 if (devnodebuf == NULL) {
649 printf("pnpbios: cannot allocate memory, bailing\n");
652 pda = (struct pnp_sysdevargs *)devnodebuf;
655 for (currdev = 0, left = ndevs; (currdev != 0xff) && (left > 0); left--) {
659 /* get current configuration */
660 if ((error = bios16(&args, PNP_GET_DEVNODE, &pda->next, &pda->node, 1))) {
661 printf("pnpbios: error %d making BIOS16 call\n", error);
664 if ((error = (args.r.eax & 0xff))) {
666 printf("pnpbios: %s 0x%x fetching node %d\n", error & 0x80 ? "error" : "warning", error, currdev);
671 if (pd->size < sizeof(struct pnp_sysdev)) {
672 printf("pnpbios: bogus system node data, aborting scan\n");
677 * Ignore PICs so that we don't have to worry about the PICs
678 * claiming IRQs to prevent their use. The PIC drivers
679 * already ensure that invalid IRQs are not used.
681 if (!strcmp(pnp_eisaformat(pd->devid), "PNP0000")) /* ISA PIC */
683 if (!strcmp(pnp_eisaformat(pd->devid), "PNP0003")) /* APIC */
686 /* Add the device and parse its resources */
687 dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNPBIOS, NULL, -1);
688 isa_set_vendorid(dev, pd->devid);
689 isa_set_logicalid(dev, pd->devid);
691 * It appears that some PnP BIOS doesn't allow us to re-enable
692 * the embedded system device once it is disabled. We shall
693 * mark all system device nodes as "cannot be disabled", regardless
694 * of actual settings in the device attribute byte.
696 isa_set_configattr(dev,
697 ((pd->attrib & PNPATTR_NODISABLE) ? 0 : ISACFGATTR_CANDISABLE) |
698 ((!(pd->attrib & PNPATTR_NOCONFIG) &&
699 PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
700 ? ISACFGATTR_DYNAMIC : 0));
702 isa_set_configattr(dev,
703 (!(pd->attrib & PNPATTR_NOCONFIG) &&
704 PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
705 ? ISACFGATTR_DYNAMIC : 0);
706 isa_set_pnpbios_handle(dev, pd->handle);
707 ISA_SET_CONFIG_CALLBACK(parent, dev, pnpbios_set_config, 0);
708 pnp_parse_resources(dev, &pd->devdata[0],
709 pd->size - sizeof(struct pnp_sysdev), 0);
710 if (!device_get_desc(dev))
711 device_set_desc_copy(dev, pnp_eisaformat(pd->devid));
713 /* Find device IDs */
717 /* look for a compatible device ID too */
718 left = pd->size - sizeof(struct pnp_sysdev);
721 tag = pd->devdata[idx++];
722 if (PNP_RES_TYPE(tag) == 0) {
724 switch (PNP_SRES_NUM(tag)) {
725 case PNP_TAG_COMPAT_DEVICE:
726 compid = (u_int32_t *)(pd->devdata + idx);
728 printf("pnpbios: node %d compat ID 0x%08x\n", pd->handle, *compid);
734 idx += PNP_SRES_LEN(tag);
738 /* Large resource, skip it */
739 idx += *(u_int16_t *)(pd->devdata + idx) + 2;
742 printf("pnpbios: handle %d device ID %s (%08x)",
743 pd->handle, pnp_eisaformat(*devid), *devid);
745 printf(" compat ID %s (%08x)",
746 pnp_eisaformat(*compid), *compid);
752 static device_method_t pnpbios_methods[] = {
753 /* Device interface */
754 DEVMETHOD(device_identify, pnpbios_identify),
759 static driver_t pnpbios_driver = {
765 static devclass_t pnpbios_devclass;
767 DRIVER_MODULE(pnpbios, isa, pnpbios_driver, pnpbios_devclass, 0, 0);