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