2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 1997 Michael Smith
5 * Copyright (c) 1998 Jonathan Lemon
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
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) {
92 /* get a virtual pointer to the structure */
93 sdh = (struct bios32_SDheader *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
94 for (cv = (u_int8_t *)sdh, ck = 0, i = 0; i < (sdh->len * 16); i++) {
97 /* If checksum is OK, enable use of the entrypoint */
98 if ((ck == 0) && (BIOS_START <= sdh->entry ) &&
99 (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 = kern_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+0x%x\n", PCIbios.base, PCIbios.entry);
117 printf("bios32: Bad BIOS32 Service Directory\n");
124 * Allow user override of PnP BIOS search
126 if ((((p = kern_getenv("machdep.bios.pnp")) == NULL) || strcmp(p, "disable")) &&
127 ((sigaddr = bios_sigsearch(0, "$PnP", 4, 16, 0)) != 0)) {
128 /* get a virtual pointer to the structure */
129 pt = (struct PnPBIOS_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
130 for (cv = (u_int8_t *)pt, ck = 0, i = 0; i < pt->len; i++) {
133 /* If checksum is OK, enable use of the entrypoint */
137 printf("pnpbios: Found PnP BIOS data at %p\n", pt);
138 printf("pnpbios: Entry = %x:%x Rev = %d.%d\n",
139 pt->pmentrybase, pt->pmentryoffset, pt->version >> 4, pt->version & 0xf);
140 if ((pt->control & 0x3) == 0x01)
141 printf("pnpbios: Event flag at %x\n", pt->evflagaddr);
142 if (pt->oemdevid != 0)
143 printf("pnpbios: OEM ID %x\n", pt->oemdevid);
147 printf("pnpbios: Bad PnP BIOS data checksum\n");
153 /* look for other know signatures */
154 printf("Other BIOS signatures found:\n");
161 * Query the BIOS32 Service Directory for the service named in (ent),
162 * returns nonzero if the lookup fails. The caller must fill in
163 * (ent->ident), the remainder are populated on a successful lookup.
166 bios32_SDlookup(struct bios32_SDentry *ent)
168 struct bios_regs args;
170 if (bios32_SDCI == 0)
173 args.eax = ent->ident.id; /* set up arguments */
174 args.ebx = args.ecx = args.edx = 0;
175 bios32(&args, bios32_SDCI, GSEL(GCODE_SEL, SEL_KPL));
176 if ((args.eax & 0xff) == 0) { /* success? */
177 ent->base = args.ebx;
179 ent->entry = args.edx;
180 ent->ventry = BIOS_PADDRTOVADDR(ent->base + ent->entry);
181 return (0); /* all OK */
183 return (1); /* failed */
189 * Search some or all of the BIOS region for a signature string.
191 * (start) Optional offset returned from this function
192 * (for searching for multiple matches), or NULL
193 * to start the search from the base of the BIOS.
194 * Note that this will be a _physical_ address in
195 * the range 0xe0000 - 0xfffff.
196 * (sig) is a pointer to the byte(s) of the signature.
197 * (siglen) number of bytes in the signature.
198 * (paralen) signature paragraph (alignment) size.
199 * (sigofs) offset of the signature within the paragraph.
201 * Returns the _physical_ address of the found signature, 0 if the
202 * signature was not found.
206 bios_sigsearch(u_int32_t start, u_char *sig, int siglen, int paralen, int sigofs)
210 /* compute the starting address */
211 if ((start >= BIOS_START) && (start <= (BIOS_START + BIOS_SIZE))) {
212 sp = (char *)BIOS_PADDRTOVADDR(start);
213 } else if (start == 0) {
214 sp = (char *)BIOS_PADDRTOVADDR(BIOS_START);
216 return 0; /* bogus start address */
219 /* compute the end address */
220 end = (u_char *)BIOS_PADDRTOVADDR(BIOS_START + BIOS_SIZE);
223 while ((sp + sigofs + siglen) < end) {
225 if (!bcmp(sp + sigofs, sig, siglen)) {
226 /* convert back to physical address */
227 return((u_int32_t)BIOS_VADDRTOPADDR(sp));
235 * do not staticize, used by bioscall.s
246 } bioscall_vector; /* bios jump vector */
249 set_bios_selectors(struct bios_segments *seg, int flags)
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 */
258 1, /* descriptor size (overwritten) */
259 0 /* granularity == byte units */
261 union descriptor *p_gdt;
264 p_gdt = &gdt[PCPU_GET(cpuid) * NGDT];
269 ssd.ssd_base = seg->code32.base;
270 ssd.ssd_limit = seg->code32.limit;
271 ssdtosd(&ssd, &p_gdt[GBIOSCODE32_SEL].sd);
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);
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);
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);
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);
301 extern u_long vm86phystk;
302 extern void bios16_jmp(void);
305 * this routine is really greedy with selectors, and uses 5:
307 * 32-bit code selector: to return to kernel
308 * 16-bit code selector: for running code
309 * data selector: for 16-bit data
310 * util selector: extra utility selector
311 * args selector: to handle pointers
313 * the util selector is set from the util16 entry in bios16_args, if a
314 * "U" specifier is seen.
316 * See <machine/pc/bios.h> for description of format specifiers
319 bios16(struct bios_args *args, char *fmt, ...)
321 char *p, *stack, *stack_top;
323 int flags = BIOSCODE_FLAG | BIOSDATA_FLAG;
324 u_int i, arg_start, arg_end;
325 void *bios16_pmap_handle;
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 */
375 if (flags & BIOSARGS_FLAG) {
376 if (arg_end - arg_start > ctob(16))
378 args->seg.args.base = arg_start;
379 args->seg.args.limit = 0xffff;
382 args->seg.code32.base = pmap_pg_frame((u_int)&bios16_jmp);
383 args->seg.code32.limit = 0xffff;
385 bios16_pmap_handle = pmap_bios16_enter();
389 for (p = fmt; p && *p; p++) {
391 case 'p': /* 32-bit pointer */
392 i = va_arg(ap, u_int);
393 *(u_int *)stack = (i - arg_start) |
394 (GSEL(GBIOSARGS_SEL, SEL_KPL) << 16);
398 case 'i': /* 32-bit integer */
399 i = va_arg(ap, u_int);
404 case 'U': /* 16-bit selector */
405 *(u_short *)stack = GSEL(GBIOSUTIL_SEL, SEL_KPL);
409 case 'D': /* 16-bit selector */
410 *(u_short *)stack = GSEL(GBIOSDATA_SEL, SEL_KPL);
414 case 'C': /* 16-bit selector */
415 *(u_short *)stack = GSEL(GBIOSCODE16_SEL, SEL_KPL);
419 case 's': /* 16-bit integer passed as an int */
421 *(u_short *)stack = i;
432 set_bios_selectors(&args->seg, flags);
433 bioscall_vector.vec16.offset = (u_short)args->entry;
434 bioscall_vector.vec16.segment = GSEL(GBIOSCODE16_SEL, SEL_KPL);
436 i = bios16_call(&args->r, stack_top);
437 pmap_bios16_leave(bios16_pmap_handle);
442 bios_oem_strings(struct bios_oem *oem, u_char *buffer, size_t maxlen)
445 struct bios_oem_signature *sig;
447 u_char c, *s, *se, *str, *bios_str;
448 size_t i, off, len, tot;
450 if ( !oem || !buffer || maxlen<2 )
453 sig = oem->signature;
457 from = oem->range.from;
459 if ( (to<=from) || (from<BIOS_START) || (to>(BIOS_START+BIOS_SIZE)) )
462 while (sig->anchor != NULL) {
467 /* make sure offset doesn't go beyond bios area */
468 if ( (to+off)>(BIOS_START+BIOS_SIZE) ||
469 ((from+off)<BIOS_START) ) {
470 printf("sys/i386/i386/bios.c: sig '%s' "
471 "from 0x%0x to 0x%0x offset %d "
472 "out of BIOS bounds 0x%0x - 0x%0x\n",
474 BIOS_START, BIOS_START+BIOS_SIZE);
477 /* make sure we don't overrun return buffer */
478 if (idx + tot > maxlen - 1) {
479 printf("sys/i386/i386/bios.c: sig '%s' "
480 "idx %d + tot %d = %d > maxlen-1 %d\n",
481 str, idx, tot, idx+tot, maxlen-1);
485 s = (u_char *)BIOS_PADDRTOVADDR(from);
486 se = (u_char *)BIOS_PADDRTOVADDR(to-len);
488 if (!bcmp(str, s, len)) {
494 * store pretty version of totlen bytes of bios string with
495 * given offset; 0x20 - 0x7E are printable; uniquify spaces
498 for (i=0; i<tot; i++) {
500 if ( (c < 0x20) || (c > 0x7E) )
505 } else if ( (c != ' ') ||
506 ((c == ' ') && (buffer[idx-1] != ' ')) )
512 /* remove a final trailing space */
513 if ( (idx > 1) && (buffer[idx-1] == ' ') )
521 * PnP BIOS interface; enumerate devices only known to the system
522 * BIOS and save information about them for later use.
532 #define PNPATTR_NODISABLE (1<<0) /* can't be disabled */
533 #define PNPATTR_NOCONFIG (1<<1) /* can't be configured */
534 #define PNPATTR_OUTPUT (1<<2) /* can be primary output */
535 #define PNPATTR_INPUT (1<<3) /* can be primary input */
536 #define PNPATTR_BOOTABLE (1<<4) /* can be booted from */
537 #define PNPATTR_DOCK (1<<5) /* is a docking station */
538 #define PNPATTR_REMOVEABLE (1<<6) /* device is removeable */
539 #define PNPATTR_CONFIG_STATIC (0)
540 #define PNPATTR_CONFIG_DYNAMIC (1)
541 #define PNPATTR_CONFIG_DYNONLY (3)
542 #define PNPATTR_CONFIG(a) (((a) >> 7) & 0x3)
543 /* device-specific data comes here */
547 /* We have to cluster arguments within a 64k range for the bios16 call */
548 struct pnp_sysdevargs
551 struct pnp_sysdev node;
555 * This function is called after the bus has assigned resource
556 * locations for a logical device.
559 pnpbios_set_config(void *arg, struct isa_config *config, int enable)
564 * Quiz the PnP BIOS, build a list of PNP IDs and resource data.
567 pnpbios_identify(driver_t *driver, device_t parent)
569 struct PnPBIOS_table *pt = PnPBIOStable;
570 struct bios_args args;
571 struct pnp_sysdev *pd;
572 struct pnp_sysdevargs *pda;
573 u_int16_t ndevs, bigdev;
575 u_int8_t *devnodebuf, tag;
576 u_int32_t *devid, *compid;
580 /* no PnP BIOS information */
584 /* Check to see if ACPI is already active. */
585 dev = devclass_get_device(devclass_find("acpi"), 0);
586 if (dev != NULL && device_is_attached(dev))
589 /* get count of PnP devices */
590 bzero(&args, sizeof(args));
591 args.seg.code16.base = BIOS_PADDRTOVADDR(pt->pmentrybase);
592 args.seg.code16.limit = 0xffff; /* XXX ? */
593 args.seg.data.base = BIOS_PADDRTOVADDR(pt->pmdataseg);
594 args.seg.data.limit = 0xffff;
595 args.entry = pt->pmentryoffset;
597 if ((error = bios16(&args, PNP_COUNT_DEVNODES, &ndevs, &bigdev)) || (args.r.eax & 0xff)) {
598 printf("pnpbios: error %d/%x getting device count/size limit\n", error, args.r.eax);
601 ndevs &= 0xff; /* clear high byte garbage */
603 printf("pnpbios: %d devices, largest %d bytes\n", ndevs, bigdev);
605 devnodebuf = malloc(bigdev + (sizeof(struct pnp_sysdevargs) - sizeof(struct pnp_sysdev)),
607 if (devnodebuf == NULL) {
608 printf("pnpbios: cannot allocate memory, bailing\n");
611 pda = (struct pnp_sysdevargs *)devnodebuf;
614 for (currdev = 0, left = ndevs; (currdev != 0xff) && (left > 0); left--) {
617 /* get current configuration */
618 if ((error = bios16(&args, PNP_GET_DEVNODE, &pda->next, &pda->node, 1))) {
619 printf("pnpbios: error %d making BIOS16 call\n", error);
622 if ((error = (args.r.eax & 0xff))) {
624 printf("pnpbios: %s 0x%x fetching node %d\n", error & 0x80 ? "error" : "warning", error, currdev);
629 if (pd->size < sizeof(struct pnp_sysdev)) {
630 printf("pnpbios: bogus system node data, aborting scan\n");
635 * Ignore PICs so that we don't have to worry about the PICs
636 * claiming IRQs to prevent their use. The PIC drivers
637 * already ensure that invalid IRQs are not used.
639 if (!strcmp(pnp_eisaformat(pd->devid), "PNP0000")) /* ISA PIC */
641 if (!strcmp(pnp_eisaformat(pd->devid), "PNP0003")) /* APIC */
644 /* Add the device and parse its resources */
645 dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNPBIOS, NULL, -1);
646 isa_set_vendorid(dev, pd->devid);
647 isa_set_logicalid(dev, pd->devid);
649 * It appears that some PnP BIOS doesn't allow us to re-enable
650 * the embedded system device once it is disabled. We shall
651 * mark all system device nodes as "cannot be disabled", regardless
652 * of actual settings in the device attribute byte.
654 isa_set_configattr(dev,
655 ((pd->attrib & PNPATTR_NODISABLE) ? 0 : ISACFGATTR_CANDISABLE) |
656 ((!(pd->attrib & PNPATTR_NOCONFIG) &&
657 PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
658 ? ISACFGATTR_DYNAMIC : 0));
660 isa_set_configattr(dev,
661 (!(pd->attrib & PNPATTR_NOCONFIG) &&
662 PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
663 ? ISACFGATTR_DYNAMIC : 0);
664 isa_set_pnpbios_handle(dev, pd->handle);
665 ISA_SET_CONFIG_CALLBACK(parent, dev, pnpbios_set_config, 0);
666 pnp_parse_resources(dev, &pd->devdata[0],
667 pd->size - sizeof(struct pnp_sysdev), 0);
668 if (!device_get_desc(dev))
669 device_set_desc_copy(dev, pnp_eisaformat(pd->devid));
671 /* Find device IDs */
675 /* look for a compatible device ID too */
676 left = pd->size - sizeof(struct pnp_sysdev);
679 tag = pd->devdata[idx++];
680 if (PNP_RES_TYPE(tag) == 0) {
682 switch (PNP_SRES_NUM(tag)) {
683 case PNP_TAG_COMPAT_DEVICE:
684 compid = (u_int32_t *)(pd->devdata + idx);
686 printf("pnpbios: node %d compat ID 0x%08x\n", pd->handle, *compid);
692 idx += PNP_SRES_LEN(tag);
696 /* Large resource, skip it */
697 idx += *(u_int16_t *)(pd->devdata + idx) + 2;
700 printf("pnpbios: handle %d device ID %s (%08x)",
701 pd->handle, pnp_eisaformat(*devid), *devid);
703 printf(" compat ID %s (%08x)",
704 pnp_eisaformat(*compid), *compid);
710 static device_method_t pnpbios_methods[] = {
711 /* Device interface */
712 DEVMETHOD(device_identify, pnpbios_identify),
716 static driver_t pnpbios_driver = {
722 DRIVER_MODULE(pnpbios, isa, pnpbios_driver, 0, 0);