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
31 * Code for dealing with the BIOS in x86 PC systems.
34 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
41 #include <machine/md_var.h>
42 #include <machine/segments.h>
43 #include <machine/stdarg.h>
44 #include <machine/tss.h>
45 #include <machine/vmparam.h>
46 #include <machine/pc/bios.h>
47 #include <isa/pnpreg.h>
49 #define BIOS_START 0xe0000
50 #define BIOS_SIZE 0x20000
52 /* exported lookup results */
53 struct bios32_SDentry PCIbios = {entry : 0};
54 struct PnPBIOS_table *PnPBIOStable = 0;
56 static u_int bios32_SDCI = 0;
58 /* start fairly early */
59 static void bios32_init(void *junk);
60 SYSINIT(bios32, SI_SUB_CPU, SI_ORDER_ANY, bios32_init, NULL);
62 static void pnpbios_scan(void);
63 static char *pnp_eisaformat(u_int8_t *data);
69 * Locate various bios32 entities.
72 bios32_init(void *junk)
75 struct bios32_SDheader *sdh;
76 struct PnPBIOS_table *pt;
81 * BIOS32 Service Directory
84 /* look for the signature */
85 if ((sigaddr = bios_sigsearch(0, "_32_", 4, 16, 0)) != 0) {
87 /* get a virtual pointer to the structure */
88 sdh = (struct bios32_SDheader *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
89 for (cv = (u_int8_t *)sdh, ck = 0, i = 0; i < (sdh->len * 16); i++) {
92 /* If checksum is OK, enable use of the entrypoint */
93 if ((ck == 0) && (sdh->entry < (BIOS_START + BIOS_SIZE))) {
94 bios32_SDCI = BIOS_PADDRTOVADDR(sdh->entry);
96 printf("bios32: Found BIOS32 Service Directory header at %p\n", sdh);
97 printf("bios32: Entry = 0x%x (%x) Rev = %d Len = %d\n",
98 sdh->entry, bios32_SDCI, sdh->revision, sdh->len);
100 /* See if there's a PCI BIOS entrypoint here */
101 PCIbios.ident.id = 0x49435024; /* PCI systems should have this */
102 if (!bios32_SDlookup(&PCIbios) && bootverbose)
103 printf("pcibios: PCI BIOS entry at 0x%x\n", PCIbios.entry);
105 printf("bios32: Bad BIOS32 Service Directory\n");
112 if ((sigaddr = bios_sigsearch(0, "$PnP", 4, 16, 0)) != 0) {
114 /* get a virtual pointer to the structure */
115 pt = (struct PnPBIOS_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
116 for (cv = (u_int8_t *)pt, ck = 0, i = 0; i < pt->len; i++) {
119 /* If checksum is OK, enable use of the entrypoint */
123 printf("pnpbios: Found PnP BIOS data at %p\n", pt);
124 printf("pnpbios: Entry = %x:%x Rev = %d.%d\n",
125 pt->pmentrybase, pt->pmentryoffset, pt->version >> 4, pt->version & 0xf);
126 if ((pt->control & 0x3) == 0x01)
127 printf("pnpbios: Event flag at %x\n", pt->evflagaddr);
128 if (pt->oemdevid != 0)
129 printf("pnpbios: OEM ID %x\n", pt->oemdevid);
134 printf("pnpbios: Bad PnP BIOS data checksum\n");
139 /* look for other know signatures */
140 printf("Other BIOS signatures found:\n");
141 printf("ACPI: %08x\n", bios_sigsearch(0, "RSD PTR ", 8, 16, 0));
148 * Query the BIOS32 Service Directory for the service named in (ent),
149 * returns nonzero if the lookup fails. The caller must fill in
150 * (ent->ident), the remainder are populated on a successful lookup.
153 bios32_SDlookup(struct bios32_SDentry *ent)
155 struct bios_regs args;
157 if (bios32_SDCI == 0)
160 args.eax = ent->ident.id; /* set up arguments */
161 args.ebx = args.ecx = args.edx = 0;
162 bios32(&args, bios32_SDCI, GSEL(GCODE_SEL, SEL_KPL));
163 if ((args.eax & 0xff) == 0) { /* success? */
164 ent->base = args.ebx;
166 ent->entry = args.edx;
167 return (0); /* all OK */
169 return (1); /* failed */
176 * Search some or all of the BIOS region for a signature string.
178 * (start) Optional offset returned from this function
179 * (for searching for multiple matches), or NULL
180 * to start the search from the base of the BIOS.
181 * Note that this will be a _physical_ address in
182 * the range 0xe0000 - 0xfffff.
183 * (sig) is a pointer to the byte(s) of the signature.
184 * (siglen) number of bytes in the signature.
185 * (paralen) signature paragraph (alignment) size.
186 * (sigofs) offset of the signature within the paragraph.
188 * Returns the _physical_ address of the found signature, 0 if the
189 * signature was not found.
193 bios_sigsearch(u_int32_t start, u_char *sig, int siglen, int paralen, int sigofs)
197 /* compute the starting address */
198 if ((start >= BIOS_START) && (start <= (BIOS_START + BIOS_SIZE))) {
199 sp = (char *)BIOS_PADDRTOVADDR(start);
200 } else if (start == 0) {
201 sp = (char *)BIOS_PADDRTOVADDR(BIOS_START);
203 return 0; /* bogus start address */
206 /* compute the end address */
207 end = (u_char *)BIOS_PADDRTOVADDR(BIOS_START + BIOS_SIZE);
210 while ((sp + sigofs + siglen) < end) {
213 if (!bcmp(sp + sigofs, sig, siglen)) {
214 /* convert back to physical address */
215 return((u_int32_t)BIOS_VADDRTOPADDR(sp));
223 * do not staticize, used by bioscall.s
234 } bioscall_vector; /* bios jump vector */
237 set_bios_selectors(struct bios_segments *seg, int flags)
239 struct soft_segment_descriptor ssd = {
240 0, /* segment base address (overwritten) */
241 0, /* length (overwritten) */
242 SDT_MEMERA, /* segment type (overwritten) */
243 0, /* priority level */
244 1, /* descriptor present */
246 1, /* descriptor size (overwritten) */
247 0 /* granularity == byte units */
249 union descriptor *p_gdt;
257 ssd.ssd_base = seg->code32.base;
258 ssd.ssd_limit = seg->code32.limit;
259 ssdtosd(&ssd, &p_gdt[GBIOSCODE32_SEL].sd);
262 if (flags & BIOSCODE_FLAG) {
263 ssd.ssd_base = seg->code16.base;
264 ssd.ssd_limit = seg->code16.limit;
265 ssdtosd(&ssd, &p_gdt[GBIOSCODE16_SEL].sd);
268 ssd.ssd_type = SDT_MEMRWA;
269 if (flags & BIOSDATA_FLAG) {
270 ssd.ssd_base = seg->data.base;
271 ssd.ssd_limit = seg->data.limit;
272 ssdtosd(&ssd, &p_gdt[GBIOSDATA_SEL].sd);
275 if (flags & BIOSUTIL_FLAG) {
276 ssd.ssd_base = seg->util.base;
277 ssd.ssd_limit = seg->util.limit;
278 ssdtosd(&ssd, &p_gdt[GBIOSUTIL_SEL].sd);
281 if (flags & BIOSARGS_FLAG) {
282 ssd.ssd_base = seg->args.base;
283 ssd.ssd_limit = seg->args.limit;
284 ssdtosd(&ssd, &p_gdt[GBIOSARGS_SEL].sd);
289 extern void bios16_jmp(void);
292 * this routine is really greedy with selectors, and uses 5:
294 * 32-bit code selector: to return to kernel
295 * 16-bit code selector: for running code
296 * data selector: for 16-bit data
297 * util selector: extra utility selector
298 * args selector: to handle pointers
300 * the util selector is set from the util16 entry in bios16_args, if a
301 * "U" specifier is seen.
303 * See <machine/pc/bios.h> for description of format specifiers
306 bios16(struct bios_args *args, char *fmt, ...)
308 char *p, *stack, *stack_top;
310 int flags = BIOSCODE_FLAG | BIOSDATA_FLAG;
311 u_int i, arg_start, arg_end;
314 arg_start = 0xffffffff;
318 * Some BIOS entrypoints attempt to copy the largest-case
319 * argument frame (in order to generalise handling for
320 * different entry types). If our argument frame is
321 * smaller than this, the BIOS will reach off the top of
322 * our constructed stack segment. Pad the top of the stack
323 * with some garbage to avoid this.
325 stack = (caddr_t)PAGE_SIZE - 32;
328 for (p = fmt; p && *p; p++) {
330 case 'p': /* 32-bit pointer */
331 i = va_arg(ap, u_int);
332 arg_start = min(arg_start, i);
333 arg_end = max(arg_end, i);
334 flags |= BIOSARGS_FLAG;
338 case 'i': /* 32-bit integer */
339 i = va_arg(ap, u_int);
343 case 'U': /* 16-bit selector */
344 flags |= BIOSUTIL_FLAG;
346 case 'D': /* 16-bit selector */
347 case 'C': /* 16-bit selector */
351 case 's': /* 16-bit integer */
352 i = va_arg(ap, u_short);
361 if (flags & BIOSARGS_FLAG) {
362 if (arg_end - arg_start > ctob(16))
364 args->seg.args.base = arg_start;
365 args->seg.args.limit = 0xffff;
368 args->seg.code32.base = (u_int)&bios16_jmp & PG_FRAME;
369 args->seg.code32.limit = 0xffff;
371 ptd = (u_int *)rcr3();
372 if (ptd == IdlePTD) {
374 * no page table, so create one and install it.
376 pte = (u_int *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
377 ptd = (u_int *)((u_int)ptd + KERNBASE);
378 *ptd = vtophys(pte) | PG_RW | PG_V;
381 * this is a user-level page table
383 pte = (u_int *)&PTmap;
386 * install pointer to page 0. we don't need to flush the tlb,
387 * since there should not be a previous mapping for page 0.
389 *pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V;
393 for (p = fmt; p && *p; p++) {
395 case 'p': /* 32-bit pointer */
396 i = va_arg(ap, u_int);
397 *(u_int *)stack = (i - arg_start) |
398 (GSEL(GBIOSARGS_SEL, SEL_KPL) << 16);
402 case 'i': /* 32-bit integer */
403 i = va_arg(ap, u_int);
408 case 'U': /* 16-bit selector */
409 *(u_short *)stack = GSEL(GBIOSUTIL_SEL, SEL_KPL);
413 case 'D': /* 16-bit selector */
414 *(u_short *)stack = GSEL(GBIOSDATA_SEL, SEL_KPL);
418 case 'C': /* 16-bit selector */
419 *(u_short *)stack = GSEL(GBIOSCODE16_SEL, SEL_KPL);
423 case 's': /* 16-bit integer */
424 i = va_arg(ap, u_short);
425 *(u_short *)stack = i;
434 set_bios_selectors(&args->seg, flags);
435 bioscall_vector.vec16.offset = (u_short)args->entry;
436 bioscall_vector.vec16.segment = GSEL(GBIOSCODE16_SEL, SEL_KPL);
438 i = bios16_call(&args->r, stack_top);
440 if (pte == (u_int *)&PTmap) {
441 *pte = 0; /* remove entry */
443 *ptd = 0; /* remove page table */
444 free(pte, M_TEMP); /* ... and free it */
448 * XXX only needs to be invlpg(0) but that doesn't work on the 386
456 * PnP BIOS interface; enumerate devices only known to the system
457 * BIOS and save information about them for later use.
467 #define PNPATTR_NODISABLE (1<<0) /* can't be disabled */
468 #define PNPATTR_NOCONFIG (1<<1) /* can't be configured */
469 #define PNPATTR_OUTPUT (1<<2) /* can be primary output */
470 #define PNPATTR_INPUT (1<<3) /* can be primary input */
471 #define PNPATTR_BOOTABLE (1<<4) /* can be booted from */
472 #define PNPATTR_DOCK (1<<5) /* is a docking station */
473 #define PNPATTR_REMOVEABLE (1<<6) /* device is removeable */
474 #define PNPATTR_CONFIG_STATIC 0x00
475 #define PNPATTR_CONFIG_DYNAMIC 0x07
476 #define PNPATTR_CONFIG_DYNONLY 0x17
477 /* device-specific data comes here */
479 } __attribute__ ((packed));
481 /* We have to cluster arguments within a 64k range for the bios16 call */
482 struct pnp_sysdevargs
485 struct pnp_sysdev node;
489 * Quiz the PnP BIOS, build a list of PNP IDs and resource data.
494 struct PnPBIOS_table *pt = PnPBIOStable;
495 struct bios_args args;
496 struct pnp_sysdev *pd;
497 struct pnp_sysdevargs *pda;
498 u_int16_t ndevs, bigdev;
500 u_int8_t *devnodebuf, tag;
501 u_int32_t *devid, *compid;
504 /* no PnP BIOS information */
508 bzero(&args, sizeof(args));
509 args.seg.code16.base = BIOS_PADDRTOVADDR(pt->pmentrybase);
510 args.seg.code16.limit = 0xffff; /* XXX ? */
511 args.seg.data.base = BIOS_PADDRTOVADDR(pt->pmdataseg);
512 args.seg.data.limit = 0xffff;
513 args.entry = pt->pmentryoffset;
515 if ((error = bios16(&args, PNP_COUNT_DEVNODES, &ndevs, &bigdev)) || (args.r.eax & 0xff))
516 printf("pnpbios: error %d/%x getting device count/size limit\n", error, args.r.eax);
517 ndevs &= 0xff; /* clear high byte garbage */
519 printf("pnpbios: %d devices, largest %d bytes\n", ndevs, bigdev);
521 devnodebuf = malloc(bigdev + (sizeof(struct pnp_sysdevargs) - sizeof(struct pnp_sysdev)),
523 pda = (struct pnp_sysdevargs *)devnodebuf;
526 for (currdev = 0, left = ndevs; (currdev != 0xff) && (left > 0); left--) {
530 /* get current configuration */
531 if ((error = bios16(&args, PNP_GET_DEVNODE, &pda->next, &pda->node, (u_int16_t)1))) {
532 printf("pnpbios: error %d making BIOS16 call\n", error);
535 if ((error = (args.r.eax & 0xff))) {
537 printf("pnpbios: %s 0x%x fetching node %d\n", error & 0x80 ? "error" : "warning", error, currdev);
542 if (pd->size < sizeof(struct pnp_sysdev)) {
543 printf("pnpbios: bogus system node data, aborting scan\n");
547 /* Find device IDs */
551 /* look for a compatible device ID too */
552 left = pd->size - sizeof(struct pnp_sysdev);
555 tag = pd->devdata[idx++];
556 if (PNP_RES_TYPE(tag) == 0) {
558 switch (PNP_SRES_NUM(tag)) {
559 case PNP_TAG_COMPAT_DEVICE:
560 compid = (u_int32_t *)(pd->devdata + idx);
562 printf("pnpbios: node %d compat ID 0x%08x\n", pd->handle, *compid);
568 idx += PNP_SRES_LEN(tag);
572 /* Large resource, skip it */
573 idx += *(u_int16_t *)(pd->devdata + idx) + 2;
576 printf("pnpbios: handle %d device ID %s (%08x)",
577 pd->handle, pnp_eisaformat((u_int8_t *)devid), *devid);
579 printf(" compat ID %s (%08x)",
580 pnp_eisaformat((u_int8_t *)compid), *compid);
586 /* XXX should be somewhere else */
588 pnp_eisaformat(u_int8_t *data)
590 static char idbuf[8];
591 const char hextoascii[] = "0123456789abcdef";
593 idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
594 idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
595 idbuf[2] = '@' + (data[1] & 0x1f);
596 idbuf[3] = hextoascii[(data[2] >> 4)];
597 idbuf[4] = hextoascii[(data[2] & 0xf)];
598 idbuf[5] = hextoascii[(data[3] >> 4)];
599 idbuf[6] = hextoascii[(data[3] & 0xf)];