xref: /freebsd/sys/i386/i386/bios.c (revision d786139c76ad8d7cd25f962f7e67d4af98685418)
1d561028dSMike Smith /*-
2d561028dSMike Smith  * Copyright (c) 1997 Michael Smith
3496027bfSMike Smith  * Copyright (c) 1998 Jonathan Lemon
4d561028dSMike Smith  * All rights reserved.
5d561028dSMike Smith  *
6d561028dSMike Smith  * Redistribution and use in source and binary forms, with or without
7d561028dSMike Smith  * modification, are permitted provided that the following conditions
8d561028dSMike Smith  * are met:
9d561028dSMike Smith  * 1. Redistributions of source code must retain the above copyright
10d561028dSMike Smith  *    notice, this list of conditions and the following disclaimer.
11d561028dSMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
12d561028dSMike Smith  *    notice, this list of conditions and the following disclaimer in the
13d561028dSMike Smith  *    documentation and/or other materials provided with the distribution.
14d561028dSMike Smith  *
15d561028dSMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16d561028dSMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17d561028dSMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18d561028dSMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19d561028dSMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20d561028dSMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21d561028dSMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22d561028dSMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23d561028dSMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24d561028dSMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25d561028dSMike Smith  * SUCH DAMAGE.
26d561028dSMike Smith  *
27c3aac50fSPeter Wemm  * $FreeBSD$
28d561028dSMike Smith  */
29d561028dSMike Smith 
30d561028dSMike Smith /*
31d561028dSMike Smith  * Code for dealing with the BIOS in x86 PC systems.
32d561028dSMike Smith  */
33d561028dSMike Smith 
34586079ccSBruce Evans #include "opt_isa.h"
35586079ccSBruce Evans 
36d561028dSMike Smith #include <sys/param.h>
37d561028dSMike Smith #include <sys/systm.h>
38d561028dSMike Smith #include <sys/kernel.h>
39496027bfSMike Smith #include <sys/malloc.h>
40bb2b9030SDoug Rabson #include <sys/bus.h>
410bbc8826SJohn Baldwin #include <sys/pcpu.h>
42d561028dSMike Smith #include <vm/vm.h>
43d561028dSMike Smith #include <vm/pmap.h>
44d561028dSMike Smith #include <machine/md_var.h>
45496027bfSMike Smith #include <machine/segments.h>
46496027bfSMike Smith #include <machine/stdarg.h>
47496027bfSMike Smith #include <machine/vmparam.h>
48d561028dSMike Smith #include <machine/pc/bios.h>
49586079ccSBruce Evans #ifdef DEV_ISA
50abe0ccd7SPeter Wemm #include <isa/isavar.h>
516c233a71SPeter Wemm #include <isa/pnpreg.h>
52bb2b9030SDoug Rabson #include <isa/pnpvar.h>
53586079ccSBruce Evans #endif
54d561028dSMike Smith 
55d561028dSMike Smith #define BIOS_START	0xe0000
56d561028dSMike Smith #define BIOS_SIZE	0x20000
57d561028dSMike Smith 
58d561028dSMike Smith /* exported lookup results */
59d561028dSMike Smith struct bios32_SDentry		PCIbios = {entry : 0};
60cb5f885bSMike Smith struct PnPBIOS_table		*PnPBIOStable = 0;
61d561028dSMike Smith 
62496027bfSMike Smith static u_int			bios32_SDCI = 0;
63d561028dSMike Smith 
64d561028dSMike Smith /* start fairly early */
65cb5f885bSMike Smith static void			bios32_init(void *junk);
66d561028dSMike Smith SYSINIT(bios32, SI_SUB_CPU, SI_ORDER_ANY, bios32_init, NULL);
67d561028dSMike Smith 
68d561028dSMike Smith /*
69d561028dSMike Smith  * bios32_init
70d561028dSMike Smith  *
71d561028dSMike Smith  * Locate various bios32 entities.
72d561028dSMike Smith  */
73d561028dSMike Smith static void
74d561028dSMike Smith bios32_init(void *junk)
75d561028dSMike Smith {
76d561028dSMike Smith     u_long			sigaddr;
77d561028dSMike Smith     struct bios32_SDheader	*sdh;
78cb5f885bSMike Smith     struct PnPBIOS_table	*pt;
79d561028dSMike Smith     u_int8_t			ck, *cv;
80d561028dSMike Smith     int				i;
81300451c4SMike Smith     char			*p;
82d561028dSMike Smith 
83d561028dSMike Smith     /*
84300451c4SMike Smith      * BIOS32 Service Directory, PCI BIOS
85d561028dSMike Smith      */
86d561028dSMike Smith 
87d561028dSMike Smith     /* look for the signature */
88d561028dSMike Smith     if ((sigaddr = bios_sigsearch(0, "_32_", 4, 16, 0)) != 0) {
89d561028dSMike Smith 
90d561028dSMike Smith 	/* get a virtual pointer to the structure */
91fc93c1bdSBruce Evans 	sdh = (struct bios32_SDheader *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
92d561028dSMike Smith 	for (cv = (u_int8_t *)sdh, ck = 0, i = 0; i < (sdh->len * 16); i++) {
93d561028dSMike Smith 	    ck += cv[i];
94d561028dSMike Smith 	}
95d561028dSMike Smith 	/* If checksum is OK, enable use of the entrypoint */
96391b5c4aSTakanori Watanabe 	if ((ck == 0) && (BIOS_START <= sdh->entry ) &&
97391b5c4aSTakanori Watanabe 	    (sdh->entry < (BIOS_START + BIOS_SIZE))) {
98496027bfSMike Smith 	    bios32_SDCI = BIOS_PADDRTOVADDR(sdh->entry);
99d561028dSMike Smith 	    if (bootverbose) {
100cb5f885bSMike Smith 		printf("bios32: Found BIOS32 Service Directory header at %p\n", sdh);
101cb5f885bSMike Smith 		printf("bios32: Entry = 0x%x (%x)  Rev = %d  Len = %d\n",
102d561028dSMike Smith 		       sdh->entry, bios32_SDCI, sdh->revision, sdh->len);
103d561028dSMike Smith 	    }
104300451c4SMike Smith 
105300451c4SMike Smith 	    /* Allow user override of PCI BIOS search */
106300451c4SMike Smith 	    if (((p = getenv("machdep.bios.pci")) == NULL) || strcmp(p, "disable")) {
107300451c4SMike Smith 
108d561028dSMike Smith 		/* See if there's a PCI BIOS entrypoint here */
109d561028dSMike Smith 		PCIbios.ident.id = 0x49435024;	/* PCI systems should have this */
110d561028dSMike Smith 		if (!bios32_SDlookup(&PCIbios) && bootverbose)
111300451c4SMike Smith 		    printf("pcibios: PCI BIOS entry at 0x%x+0x%x\n", PCIbios.base, PCIbios.entry);
112300451c4SMike Smith 	    }
113d786139cSMaxime Henrion 	    if (p != NULL)
114d786139cSMaxime Henrion 		    freeenv(p);
115d561028dSMike Smith 	} else {
116cb5f885bSMike Smith 	    printf("bios32: Bad BIOS32 Service Directory\n");
117d561028dSMike Smith 	}
118d561028dSMike Smith     }
119d561028dSMike Smith 
120d561028dSMike Smith     /*
121cb5f885bSMike Smith      * PnP BIOS
122300451c4SMike Smith      *
123300451c4SMike Smith      * Allow user override of PnP BIOS search
124cb5f885bSMike Smith      */
125300451c4SMike Smith     if ((((p = getenv("machdep.bios.pnp")) == NULL) || strcmp(p, "disable")) &&
126300451c4SMike Smith 	((sigaddr = bios_sigsearch(0, "$PnP", 4, 16, 0)) != 0)) {
127d561028dSMike Smith 
128d561028dSMike Smith 	/* get a virtual pointer to the structure */
129cb5f885bSMike Smith 	pt = (struct PnPBIOS_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
130cb5f885bSMike Smith 	for (cv = (u_int8_t *)pt, ck = 0, i = 0; i < pt->len; i++) {
131d561028dSMike Smith 	    ck += cv[i];
132d561028dSMike Smith 	}
133cb5f885bSMike Smith 	/* If checksum is OK, enable use of the entrypoint */
134d561028dSMike Smith 	if (ck == 0) {
135cb5f885bSMike Smith 	    PnPBIOStable = pt;
136d561028dSMike Smith 	    if (bootverbose) {
137cb5f885bSMike Smith 		printf("pnpbios: Found PnP BIOS data at %p\n", pt);
138cb5f885bSMike Smith 		printf("pnpbios: Entry = %x:%x  Rev = %d.%d\n",
139cb5f885bSMike Smith 		       pt->pmentrybase, pt->pmentryoffset, pt->version >> 4, pt->version & 0xf);
140cb5f885bSMike Smith 		if ((pt->control & 0x3) == 0x01)
141cb5f885bSMike Smith 		    printf("pnpbios: Event flag at %x\n", pt->evflagaddr);
142cb5f885bSMike Smith 		if (pt->oemdevid != 0)
143cb5f885bSMike Smith 		    printf("pnpbios: OEM ID %x\n", pt->oemdevid);
144cb5f885bSMike Smith 
145d561028dSMike Smith 	    }
146d561028dSMike Smith 	} else {
147cb5f885bSMike Smith 	    printf("pnpbios: Bad PnP BIOS data checksum\n");
148d561028dSMike Smith 	}
149d561028dSMike Smith     }
150d786139cSMaxime Henrion     if (p != NULL)
151d786139cSMaxime Henrion 	    freeenv(p);
15276cf257bSPoul-Henning Kamp     if (bootverbose) {
15376cf257bSPoul-Henning Kamp 	    /* look for other know signatures */
15476cf257bSPoul-Henning Kamp 	    printf("Other BIOS signatures found:\n");
15576cf257bSPoul-Henning Kamp     }
156d561028dSMike Smith }
157d561028dSMike Smith 
158d561028dSMike Smith /*
159d561028dSMike Smith  * bios32_SDlookup
160d561028dSMike Smith  *
161d561028dSMike Smith  * Query the BIOS32 Service Directory for the service named in (ent),
162d561028dSMike Smith  * returns nonzero if the lookup fails.  The caller must fill in
163d561028dSMike Smith  * (ent->ident), the remainder are populated on a successful lookup.
164d561028dSMike Smith  */
165d561028dSMike Smith int
166d561028dSMike Smith bios32_SDlookup(struct bios32_SDentry *ent)
167d561028dSMike Smith {
168496027bfSMike Smith     struct bios_regs args;
169d561028dSMike Smith 
170496027bfSMike Smith     if (bios32_SDCI == 0)
171496027bfSMike Smith 	return (1);
172d561028dSMike Smith 
173d561028dSMike Smith     args.eax = ent->ident.id;		/* set up arguments */
174d561028dSMike Smith     args.ebx = args.ecx = args.edx = 0;
175496027bfSMike Smith     bios32(&args, bios32_SDCI, GSEL(GCODE_SEL, SEL_KPL));
176d561028dSMike Smith     if ((args.eax & 0xff) == 0) {	/* success? */
177d561028dSMike Smith 	ent->base = args.ebx;
178d561028dSMike Smith 	ent->len = args.ecx;
179d561028dSMike Smith 	ent->entry = args.edx;
180300451c4SMike Smith 	ent->ventry = BIOS_PADDRTOVADDR(ent->base + ent->entry);
181d561028dSMike Smith 	return (0);			/* all OK */
182d561028dSMike Smith     }
183d561028dSMike Smith     return (1);				/* failed */
184d561028dSMike Smith }
185d561028dSMike Smith 
186496027bfSMike Smith 
187d561028dSMike Smith /*
188d561028dSMike Smith  * bios_sigsearch
189d561028dSMike Smith  *
190d561028dSMike Smith  * Search some or all of the BIOS region for a signature string.
191d561028dSMike Smith  *
192d561028dSMike Smith  * (start)	Optional offset returned from this function
193d561028dSMike Smith  *		(for searching for multiple matches), or NULL
194d561028dSMike Smith  *		to start the search from the base of the BIOS.
195d561028dSMike Smith  *		Note that this will be a _physical_ address in
196d561028dSMike Smith  *		the range 0xe0000 - 0xfffff.
197d561028dSMike Smith  * (sig)	is a pointer to the byte(s) of the signature.
198d561028dSMike Smith  * (siglen)	number of bytes in the signature.
199d561028dSMike Smith  * (paralen)	signature paragraph (alignment) size.
200d561028dSMike Smith  * (sigofs)	offset of the signature within the paragraph.
201d561028dSMike Smith  *
202d561028dSMike Smith  * Returns the _physical_ address of the found signature, 0 if the
203d561028dSMike Smith  * signature was not found.
204d561028dSMike Smith  */
205d561028dSMike Smith 
206d561028dSMike Smith u_int32_t
207d561028dSMike Smith bios_sigsearch(u_int32_t start, u_char *sig, int siglen, int paralen, int sigofs)
208d561028dSMike Smith {
209d561028dSMike Smith     u_char	*sp, *end;
210d561028dSMike Smith 
211d561028dSMike Smith     /* compute the starting address */
212d561028dSMike Smith     if ((start >= BIOS_START) && (start <= (BIOS_START + BIOS_SIZE))) {
213d561028dSMike Smith 	sp = (char *)BIOS_PADDRTOVADDR(start);
214d561028dSMike Smith     } else if (start == 0) {
215d561028dSMike Smith 	sp = (char *)BIOS_PADDRTOVADDR(BIOS_START);
216d561028dSMike Smith     } else {
217d561028dSMike Smith 	return 0;				/* bogus start address */
218d561028dSMike Smith     }
219d561028dSMike Smith 
220d561028dSMike Smith     /* compute the end address */
221d561028dSMike Smith     end = (u_char *)BIOS_PADDRTOVADDR(BIOS_START + BIOS_SIZE);
222d561028dSMike Smith 
223d561028dSMike Smith     /* loop searching */
224d561028dSMike Smith     while ((sp + sigofs + siglen) < end) {
225d561028dSMike Smith 
226d561028dSMike Smith 	/* compare here */
227c7a2b294SMike Smith 	if (!bcmp(sp + sigofs, sig, siglen)) {
228d561028dSMike Smith 	    /* convert back to physical address */
229d561028dSMike Smith 	    return((u_int32_t)BIOS_VADDRTOPADDR(sp));
230d561028dSMike Smith 	}
231d561028dSMike Smith 	sp += paralen;
232d561028dSMike Smith     }
233d561028dSMike Smith     return(0);
234d561028dSMike Smith }
235d561028dSMike Smith 
236496027bfSMike Smith /*
237496027bfSMike Smith  * do not staticize, used by bioscall.s
238496027bfSMike Smith  */
239496027bfSMike Smith union {
240496027bfSMike Smith     struct {
241496027bfSMike Smith 	u_short	offset;
242496027bfSMike Smith 	u_short	segment;
243496027bfSMike Smith     } vec16;
244496027bfSMike Smith     struct {
245496027bfSMike Smith 	u_int	offset;
246496027bfSMike Smith 	u_short	segment;
247496027bfSMike Smith     } vec32;
248496027bfSMike Smith } bioscall_vector;			/* bios jump vector */
249496027bfSMike Smith 
250496027bfSMike Smith void
251496027bfSMike Smith set_bios_selectors(struct bios_segments *seg, int flags)
252496027bfSMike Smith {
253496027bfSMike Smith     struct soft_segment_descriptor ssd = {
254496027bfSMike Smith 	0,			/* segment base address (overwritten) */
255496027bfSMike Smith 	0,			/* length (overwritten) */
256496027bfSMike Smith 	SDT_MEMERA,		/* segment type (overwritten) */
257496027bfSMike Smith 	0,			/* priority level */
258496027bfSMike Smith 	1,			/* descriptor present */
259496027bfSMike Smith 	0, 0,
260496027bfSMike Smith 	1,			/* descriptor size (overwritten) */
261496027bfSMike Smith 	0			/* granularity == byte units */
262496027bfSMike Smith     };
2637e42e2f8SJonathan Lemon     union descriptor *p_gdt;
264496027bfSMike Smith 
2657e42e2f8SJonathan Lemon #ifdef SMP
266ef73ae4bSJake Burkholder     p_gdt = &gdt[PCPU_GET(cpuid) * NGDT];
2677e42e2f8SJonathan Lemon #else
2687e42e2f8SJonathan Lemon     p_gdt = gdt;
2697e42e2f8SJonathan Lemon #endif
270496027bfSMike Smith 
271496027bfSMike Smith     ssd.ssd_base = seg->code32.base;
272496027bfSMike Smith     ssd.ssd_limit = seg->code32.limit;
2737e42e2f8SJonathan Lemon     ssdtosd(&ssd, &p_gdt[GBIOSCODE32_SEL].sd);
274496027bfSMike Smith 
275496027bfSMike Smith     ssd.ssd_def32 = 0;
276496027bfSMike Smith     if (flags & BIOSCODE_FLAG) {
277496027bfSMike Smith 	ssd.ssd_base = seg->code16.base;
278496027bfSMike Smith 	ssd.ssd_limit = seg->code16.limit;
2797e42e2f8SJonathan Lemon 	ssdtosd(&ssd, &p_gdt[GBIOSCODE16_SEL].sd);
280496027bfSMike Smith     }
281496027bfSMike Smith 
282496027bfSMike Smith     ssd.ssd_type = SDT_MEMRWA;
283496027bfSMike Smith     if (flags & BIOSDATA_FLAG) {
284496027bfSMike Smith 	ssd.ssd_base = seg->data.base;
285496027bfSMike Smith 	ssd.ssd_limit = seg->data.limit;
2867e42e2f8SJonathan Lemon 	ssdtosd(&ssd, &p_gdt[GBIOSDATA_SEL].sd);
287496027bfSMike Smith     }
288496027bfSMike Smith 
289496027bfSMike Smith     if (flags & BIOSUTIL_FLAG) {
290496027bfSMike Smith 	ssd.ssd_base = seg->util.base;
291496027bfSMike Smith 	ssd.ssd_limit = seg->util.limit;
2927e42e2f8SJonathan Lemon 	ssdtosd(&ssd, &p_gdt[GBIOSUTIL_SEL].sd);
293496027bfSMike Smith     }
294496027bfSMike Smith 
295496027bfSMike Smith     if (flags & BIOSARGS_FLAG) {
296496027bfSMike Smith 	ssd.ssd_base = seg->args.base;
297496027bfSMike Smith 	ssd.ssd_limit = seg->args.limit;
2987e42e2f8SJonathan Lemon 	ssdtosd(&ssd, &p_gdt[GBIOSARGS_SEL].sd);
299496027bfSMike Smith     }
300496027bfSMike Smith }
301496027bfSMike Smith 
302496027bfSMike Smith extern int vm86pa;
30368b7d21aSMike Smith extern void bios16_jmp(void);
304496027bfSMike Smith 
305496027bfSMike Smith /*
306496027bfSMike Smith  * this routine is really greedy with selectors, and uses 5:
307496027bfSMike Smith  *
308496027bfSMike Smith  * 32-bit code selector:	to return to kernel
309496027bfSMike Smith  * 16-bit code selector:	for running code
310496027bfSMike Smith  *        data selector:	for 16-bit data
311496027bfSMike Smith  *        util selector:	extra utility selector
312496027bfSMike Smith  *        args selector:	to handle pointers
313496027bfSMike Smith  *
314496027bfSMike Smith  * the util selector is set from the util16 entry in bios16_args, if a
315496027bfSMike Smith  * "U" specifier is seen.
316496027bfSMike Smith  *
317496027bfSMike Smith  * See <machine/pc/bios.h> for description of format specifiers
318496027bfSMike Smith  */
319496027bfSMike Smith int
320496027bfSMike Smith bios16(struct bios_args *args, char *fmt, ...)
321496027bfSMike Smith {
322496027bfSMike Smith     char	*p, *stack, *stack_top;
323496027bfSMike Smith     va_list 	ap;
324496027bfSMike Smith     int 	flags = BIOSCODE_FLAG | BIOSDATA_FLAG;
325496027bfSMike Smith     u_int 	i, arg_start, arg_end;
326d1693e17SPeter Wemm     u_int 	*pte, *ptd;
327496027bfSMike Smith 
328496027bfSMike Smith     arg_start = 0xffffffff;
329496027bfSMike Smith     arg_end = 0;
330496027bfSMike Smith 
331e6a80efcSMike Smith     /*
332e6a80efcSMike Smith      * Some BIOS entrypoints attempt to copy the largest-case
333e6a80efcSMike Smith      * argument frame (in order to generalise handling for
334e6a80efcSMike Smith      * different entry types).  If our argument frame is
335e6a80efcSMike Smith      * smaller than this, the BIOS will reach off the top of
336e6a80efcSMike Smith      * our constructed stack segment.  Pad the top of the stack
337e6a80efcSMike Smith      * with some garbage to avoid this.
338e6a80efcSMike Smith      */
339e6a80efcSMike Smith     stack = (caddr_t)PAGE_SIZE - 32;
340e6a80efcSMike Smith 
341496027bfSMike Smith     va_start(ap, fmt);
342496027bfSMike Smith     for (p = fmt; p && *p; p++) {
343496027bfSMike Smith 	switch (*p) {
344496027bfSMike Smith 	case 'p':			/* 32-bit pointer */
345496027bfSMike Smith 	    i = va_arg(ap, u_int);
346496027bfSMike Smith 	    arg_start = min(arg_start, i);
347f996ef63SMike Smith 	    arg_end = max(arg_end, i);
348496027bfSMike Smith 	    flags |= BIOSARGS_FLAG;
349496027bfSMike Smith 	    stack -= 4;
350496027bfSMike Smith 	    break;
351496027bfSMike Smith 
352496027bfSMike Smith 	case 'i':			/* 32-bit integer */
353496027bfSMike Smith 	    i = va_arg(ap, u_int);
354496027bfSMike Smith 	    stack -= 4;
355496027bfSMike Smith 	    break;
356496027bfSMike Smith 
357496027bfSMike Smith 	case 'U':			/* 16-bit selector */
358496027bfSMike Smith 	    flags |= BIOSUTIL_FLAG;
359496027bfSMike Smith 	    /* FALLTHROUGH */
360496027bfSMike Smith 	case 'D':			/* 16-bit selector */
361496027bfSMike Smith 	case 'C':			/* 16-bit selector */
362cb5f885bSMike Smith 	    stack -= 2;
363cb5f885bSMike Smith 	    break;
364cb5f885bSMike Smith 
365496027bfSMike Smith 	case 's':			/* 16-bit integer */
366496027bfSMike Smith 	    i = va_arg(ap, u_short);
367496027bfSMike Smith 	    stack -= 2;
368496027bfSMike Smith 	    break;
369496027bfSMike Smith 
370496027bfSMike Smith 	default:
371496027bfSMike Smith 	    return (EINVAL);
372496027bfSMike Smith 	}
373496027bfSMike Smith     }
374496027bfSMike Smith 
375496027bfSMike Smith     if (flags & BIOSARGS_FLAG) {
376496027bfSMike Smith 	if (arg_end - arg_start > ctob(16))
377496027bfSMike Smith 	    return (EACCES);
378496027bfSMike Smith 	args->seg.args.base = arg_start;
379f996ef63SMike Smith 	args->seg.args.limit = 0xffff;
380496027bfSMike Smith     }
381496027bfSMike Smith 
38268b7d21aSMike Smith     args->seg.code32.base = (u_int)&bios16_jmp & PG_FRAME;
383496027bfSMike Smith     args->seg.code32.limit = 0xffff;
384496027bfSMike Smith 
385d1693e17SPeter Wemm     ptd = (u_int *)rcr3();
38630d1ca27SPeter Wemm     if (ptd == (u_int *)IdlePTD) {
387496027bfSMike Smith 	/*
388496027bfSMike Smith 	 * no page table, so create one and install it.
389496027bfSMike Smith 	 */
390d1693e17SPeter Wemm 	pte = (u_int *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
391d1693e17SPeter Wemm 	ptd = (u_int *)((u_int)ptd + KERNBASE);
392496027bfSMike Smith 	*ptd = vtophys(pte) | PG_RW | PG_V;
393496027bfSMike Smith     } else {
394496027bfSMike Smith 	/*
395496027bfSMike Smith 	 * this is a user-level page table
396496027bfSMike Smith 	 */
397d1693e17SPeter Wemm 	pte = (u_int *)&PTmap;
398496027bfSMike Smith     }
399496027bfSMike Smith     /*
400496027bfSMike Smith      * install pointer to page 0.  we don't need to flush the tlb,
401496027bfSMike Smith      * since there should not be a previous mapping for page 0.
402496027bfSMike Smith      */
403496027bfSMike Smith     *pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V;
404496027bfSMike Smith 
405496027bfSMike Smith     stack_top = stack;
406496027bfSMike Smith     va_start(ap, fmt);
407496027bfSMike Smith     for (p = fmt; p && *p; p++) {
408496027bfSMike Smith 	switch (*p) {
409496027bfSMike Smith 	case 'p':			/* 32-bit pointer */
410496027bfSMike Smith 	    i = va_arg(ap, u_int);
411496027bfSMike Smith 	    *(u_int *)stack = (i - arg_start) |
412496027bfSMike Smith 		(GSEL(GBIOSARGS_SEL, SEL_KPL) << 16);
413496027bfSMike Smith 	    stack += 4;
414496027bfSMike Smith 	    break;
415496027bfSMike Smith 
416496027bfSMike Smith 	case 'i':			/* 32-bit integer */
417496027bfSMike Smith 	    i = va_arg(ap, u_int);
418496027bfSMike Smith 	    *(u_int *)stack = i;
419496027bfSMike Smith 	    stack += 4;
420496027bfSMike Smith 	    break;
421496027bfSMike Smith 
422496027bfSMike Smith 	case 'U':			/* 16-bit selector */
423496027bfSMike Smith 	    *(u_short *)stack = GSEL(GBIOSUTIL_SEL, SEL_KPL);
424496027bfSMike Smith 	    stack += 2;
425496027bfSMike Smith 	    break;
426496027bfSMike Smith 
427496027bfSMike Smith 	case 'D':			/* 16-bit selector */
428496027bfSMike Smith 	    *(u_short *)stack = GSEL(GBIOSDATA_SEL, SEL_KPL);
429496027bfSMike Smith 	    stack += 2;
430496027bfSMike Smith 	    break;
431496027bfSMike Smith 
432496027bfSMike Smith 	case 'C':			/* 16-bit selector */
433496027bfSMike Smith 	    *(u_short *)stack = GSEL(GBIOSCODE16_SEL, SEL_KPL);
434496027bfSMike Smith 	    stack += 2;
435496027bfSMike Smith 	    break;
436496027bfSMike Smith 
437496027bfSMike Smith 	case 's':			/* 16-bit integer */
438496027bfSMike Smith 	    i = va_arg(ap, u_short);
439496027bfSMike Smith 	    *(u_short *)stack = i;
440496027bfSMike Smith 	    stack += 2;
441496027bfSMike Smith 	    break;
442496027bfSMike Smith 
443496027bfSMike Smith 	default:
444496027bfSMike Smith 	    return (EINVAL);
445496027bfSMike Smith 	}
446496027bfSMike Smith     }
447496027bfSMike Smith 
448496027bfSMike Smith     set_bios_selectors(&args->seg, flags);
449496027bfSMike Smith     bioscall_vector.vec16.offset = (u_short)args->entry;
450496027bfSMike Smith     bioscall_vector.vec16.segment = GSEL(GBIOSCODE16_SEL, SEL_KPL);
451496027bfSMike Smith 
452496027bfSMike Smith     i = bios16_call(&args->r, stack_top);
453496027bfSMike Smith 
454d1693e17SPeter Wemm     if (pte == (u_int *)&PTmap) {
455496027bfSMike Smith 	*pte = 0;			/* remove entry */
456496027bfSMike Smith     } else {
457496027bfSMike Smith 	*ptd = 0;			/* remove page table */
458496027bfSMike Smith 	free(pte, M_TEMP);		/* ... and free it */
459496027bfSMike Smith     }
460d561028dSMike Smith 
461496027bfSMike Smith     /*
462496027bfSMike Smith      * XXX only needs to be invlpg(0) but that doesn't work on the 386
463496027bfSMike Smith      */
464496027bfSMike Smith     invltlb();
465496027bfSMike Smith 
466496027bfSMike Smith     return (i);
467496027bfSMike Smith }
468cb5f885bSMike Smith 
469586079ccSBruce Evans #ifdef DEV_ISA
470cb5f885bSMike Smith /*
471cb5f885bSMike Smith  * PnP BIOS interface; enumerate devices only known to the system
472cb5f885bSMike Smith  * BIOS and save information about them for later use.
473cb5f885bSMike Smith  */
474cb5f885bSMike Smith 
475cb5f885bSMike Smith struct pnp_sysdev
476cb5f885bSMike Smith {
477cb5f885bSMike Smith     u_int16_t	size;
478cb5f885bSMike Smith     u_int8_t	handle;
479cb5f885bSMike Smith     u_int32_t	devid;
480cb5f885bSMike Smith     u_int8_t	type[3];
481cb5f885bSMike Smith     u_int16_t	attrib;
482cb5f885bSMike Smith #define PNPATTR_NODISABLE	(1<<0)	/* can't be disabled */
483cb5f885bSMike Smith #define PNPATTR_NOCONFIG	(1<<1)	/* can't be configured */
484cb5f885bSMike Smith #define PNPATTR_OUTPUT		(1<<2)	/* can be primary output */
485cb5f885bSMike Smith #define PNPATTR_INPUT		(1<<3)	/* can be primary input */
486cb5f885bSMike Smith #define PNPATTR_BOOTABLE	(1<<4)	/* can be booted from */
487cb5f885bSMike Smith #define PNPATTR_DOCK		(1<<5)	/* is a docking station */
488cb5f885bSMike Smith #define PNPATTR_REMOVEABLE	(1<<6)	/* device is removeable */
489c3959391SKazutaka YOKOTA #define PNPATTR_CONFIG_STATIC	(0)
490c3959391SKazutaka YOKOTA #define PNPATTR_CONFIG_DYNAMIC	(1)
491c3959391SKazutaka YOKOTA #define PNPATTR_CONFIG_DYNONLY	(3)
492c3959391SKazutaka YOKOTA #define PNPATTR_CONFIG(a)	(((a) >> 7) & 0x3)
493cb5f885bSMike Smith     /* device-specific data comes here */
494cb5f885bSMike Smith     u_int8_t	devdata[0];
495cb5f885bSMike Smith } __attribute__ ((packed));
496cb5f885bSMike Smith 
497cb5f885bSMike Smith /* We have to cluster arguments within a 64k range for the bios16 call */
498cb5f885bSMike Smith struct pnp_sysdevargs
499cb5f885bSMike Smith {
500cb5f885bSMike Smith     u_int16_t	next;
501cb5f885bSMike Smith     struct pnp_sysdev node;
502cb5f885bSMike Smith };
503cb5f885bSMike Smith 
504cb5f885bSMike Smith /*
505bb2b9030SDoug Rabson  * This function is called after the bus has assigned resource
506bb2b9030SDoug Rabson  * locations for a logical device.
507bb2b9030SDoug Rabson  */
508bb2b9030SDoug Rabson static void
509bb2b9030SDoug Rabson pnpbios_set_config(void *arg, struct isa_config *config, int enable)
510bb2b9030SDoug Rabson {
511bb2b9030SDoug Rabson }
512bb2b9030SDoug Rabson 
513bb2b9030SDoug Rabson /*
514cb5f885bSMike Smith  * Quiz the PnP BIOS, build a list of PNP IDs and resource data.
515cb5f885bSMike Smith  */
516cb5f885bSMike Smith static void
517bb2b9030SDoug Rabson pnpbios_identify(driver_t *driver, device_t parent)
518cb5f885bSMike Smith {
519cb5f885bSMike Smith     struct PnPBIOS_table	*pt = PnPBIOStable;
520cb5f885bSMike Smith     struct bios_args		args;
521cb5f885bSMike Smith     struct pnp_sysdev		*pd;
522cb5f885bSMike Smith     struct pnp_sysdevargs	*pda;
523cb5f885bSMike Smith     u_int16_t			ndevs, bigdev;
524cb5f885bSMike Smith     int				error, currdev;
525cb5f885bSMike Smith     u_int8_t			*devnodebuf, tag;
526cb5f885bSMike Smith     u_int32_t			*devid, *compid;
527cb5f885bSMike Smith     int				idx, left;
528bb2b9030SDoug Rabson     device_t			dev;
529cb5f885bSMike Smith 
530cb5f885bSMike Smith     /* no PnP BIOS information */
531cb5f885bSMike Smith     if (pt == NULL)
532cb5f885bSMike Smith 	return;
533cb5f885bSMike Smith 
534a91d0e1cSMike Smith     /* ACPI already active */
535a91d0e1cSMike Smith     if (devclass_get_softc(devclass_find("ACPI"), 0) != NULL)
536a91d0e1cSMike Smith 	return;
537a91d0e1cSMike Smith 
538a91d0e1cSMike Smith     /* get count of PnP devices */
539cb5f885bSMike Smith     bzero(&args, sizeof(args));
540cb5f885bSMike Smith     args.seg.code16.base = BIOS_PADDRTOVADDR(pt->pmentrybase);
541cb5f885bSMike Smith     args.seg.code16.limit = 0xffff;		/* XXX ? */
542cb5f885bSMike Smith     args.seg.data.base = BIOS_PADDRTOVADDR(pt->pmdataseg);
543cb5f885bSMike Smith     args.seg.data.limit = 0xffff;
544cb5f885bSMike Smith     args.entry = pt->pmentryoffset;
545cb5f885bSMike Smith 
546cb5f885bSMike Smith     if ((error = bios16(&args, PNP_COUNT_DEVNODES, &ndevs, &bigdev)) || (args.r.eax & 0xff))
547cb5f885bSMike Smith 	printf("pnpbios: error %d/%x getting device count/size limit\n", error, args.r.eax);
54803c6be5cSMike Smith     ndevs &= 0xff;				/* clear high byte garbage */
549cb5f885bSMike Smith     if (bootverbose)
550cb5f885bSMike Smith 	printf("pnpbios: %d devices, largest %d bytes\n", ndevs, bigdev);
551cb5f885bSMike Smith 
552cb5f885bSMike Smith     devnodebuf = malloc(bigdev + (sizeof(struct pnp_sysdevargs) - sizeof(struct pnp_sysdev)),
553cb5f885bSMike Smith 			M_DEVBUF, M_NOWAIT);
554cb5f885bSMike Smith     pda = (struct pnp_sysdevargs *)devnodebuf;
555cb5f885bSMike Smith     pd = &pda->node;
556cb5f885bSMike Smith 
557cb5f885bSMike Smith     for (currdev = 0, left = ndevs; (currdev != 0xff) && (left > 0); left--) {
558cb5f885bSMike Smith 
559cb5f885bSMike Smith 	bzero(pd, bigdev);
560cb5f885bSMike Smith 	pda->next = currdev;
561cb5f885bSMike Smith 	/* get current configuration */
562cb5f885bSMike Smith 	if ((error = bios16(&args, PNP_GET_DEVNODE, &pda->next, &pda->node, (u_int16_t)1))) {
563cb5f885bSMike Smith 	    printf("pnpbios: error %d making BIOS16 call\n", error);
564cb5f885bSMike Smith 	    break;
565cb5f885bSMike Smith 	}
566cb5f885bSMike Smith 	if ((error = (args.r.eax & 0xff))) {
567cb5f885bSMike Smith 	    if (bootverbose)
568cb5f885bSMike Smith 		printf("pnpbios: %s 0x%x fetching node %d\n", error & 0x80 ? "error" : "warning", error, currdev);
569cb5f885bSMike Smith 	    if (error & 0x80)
570cb5f885bSMike Smith 		break;
571cb5f885bSMike Smith 	}
572cb5f885bSMike Smith 	currdev = pda->next;
573cb5f885bSMike Smith 	if (pd->size < sizeof(struct pnp_sysdev)) {
574cb5f885bSMike Smith 	    printf("pnpbios: bogus system node data, aborting scan\n");
575cb5f885bSMike Smith 	    break;
576cb5f885bSMike Smith 	}
577cb5f885bSMike Smith 
578642ba07aSMike Smith 	/*
579642ba07aSMike Smith 	 * If we are in APIC_IO mode, we should ignore the ISA PIC if it
580642ba07aSMike Smith 	 * shows up.  Likewise, in !APIC_IO mode, we should ignore the
581642ba07aSMike Smith 	 * APIC (less important).
582642ba07aSMike Smith 	 * This is significant because the ISA PIC will claim IRQ 2 (which
583642ba07aSMike Smith 	 * it uses for chaining), while in APIC mode this is a valid IRQ
584642ba07aSMike Smith 	 * available for general use.
585642ba07aSMike Smith 	 */
586642ba07aSMike Smith #ifdef APIC_IO
5876eb9d785SMike Smith 	if (!strcmp(pnp_eisaformat(pd->devid), "PNP0000"))	/* ISA PIC */
588642ba07aSMike Smith 	    continue;
589642ba07aSMike Smith #else
5906eb9d785SMike Smith 	if (!strcmp(pnp_eisaformat(pd->devid), "PNP0003"))	/* APIC */
591642ba07aSMike Smith 	    continue;
592642ba07aSMike Smith #endif
593642ba07aSMike Smith 
594bb2b9030SDoug Rabson 	/* Add the device and parse its resources */
595bb2b9030SDoug Rabson 	dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
596bb2b9030SDoug Rabson 	isa_set_vendorid(dev, pd->devid);
597bb2b9030SDoug Rabson 	isa_set_logicalid(dev, pd->devid);
598c3959391SKazutaka YOKOTA 	/*
599c3959391SKazutaka YOKOTA 	 * It appears that some PnP BIOS doesn't allow us to re-enable
600c3959391SKazutaka YOKOTA 	 * the embedded system device once it is disabled.  We shall
601c3959391SKazutaka YOKOTA 	 * mark all system device nodes as "cannot be disabled", regardless
602c3959391SKazutaka YOKOTA 	 * of actual settings in the device attribute byte.
603c3959391SKazutaka YOKOTA 	 * XXX
604c3959391SKazutaka YOKOTA 	isa_set_configattr(dev,
605c3959391SKazutaka YOKOTA 	    ((pd->attrib & PNPATTR_NODISABLE) ?  0 : ISACFGATTR_CANDISABLE) |
606c3959391SKazutaka YOKOTA 	    ((!(pd->attrib & PNPATTR_NOCONFIG) &&
607c3959391SKazutaka YOKOTA 		PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
608c3959391SKazutaka YOKOTA 		? ISACFGATTR_DYNAMIC : 0));
609c3959391SKazutaka YOKOTA 	 */
610c3959391SKazutaka YOKOTA 	isa_set_configattr(dev,
611c3959391SKazutaka YOKOTA 	    (!(pd->attrib & PNPATTR_NOCONFIG) &&
612c3959391SKazutaka YOKOTA 		PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
613c3959391SKazutaka YOKOTA 		? ISACFGATTR_DYNAMIC : 0);
614bb2b9030SDoug Rabson 	ISA_SET_CONFIG_CALLBACK(parent, dev, pnpbios_set_config, 0);
615bb2b9030SDoug Rabson 	pnp_parse_resources(dev, &pd->devdata[0],
616c3959391SKazutaka YOKOTA 			    pd->size - sizeof(struct pnp_sysdev), 0);
617bb2b9030SDoug Rabson 	if (!device_get_desc(dev))
618bb2b9030SDoug Rabson 	    device_set_desc_copy(dev, pnp_eisaformat(pd->devid));
619bb2b9030SDoug Rabson 
620cb5f885bSMike Smith 	/* Find device IDs */
621cb5f885bSMike Smith 	devid = &pd->devid;
622cb5f885bSMike Smith 	compid = NULL;
623cb5f885bSMike Smith 
624cb5f885bSMike Smith 	/* look for a compatible device ID too */
625cb5f885bSMike Smith 	left = pd->size - sizeof(struct pnp_sysdev);
626cb5f885bSMike Smith 	idx = 0;
627cb5f885bSMike Smith 	while (idx < left) {
628cb5f885bSMike Smith 	    tag = pd->devdata[idx++];
629cb5f885bSMike Smith 	    if (PNP_RES_TYPE(tag) == 0) {
630cb5f885bSMike Smith 		/* Small resource */
631cb5f885bSMike Smith 		switch (PNP_SRES_NUM(tag)) {
6326c233a71SPeter Wemm 		case PNP_TAG_COMPAT_DEVICE:
633cb5f885bSMike Smith 		    compid = (u_int32_t *)(pd->devdata + idx);
634cb5f885bSMike Smith 		    if (bootverbose)
635cb5f885bSMike Smith 			printf("pnpbios: node %d compat ID 0x%08x\n", pd->handle, *compid);
636cb5f885bSMike Smith 		    /* FALLTHROUGH */
6376c233a71SPeter Wemm 		case PNP_TAG_END:
638cb5f885bSMike Smith 		    idx = left;
639cb5f885bSMike Smith 		    break;
640cb5f885bSMike Smith 		default:
641cb5f885bSMike Smith 		    idx += PNP_SRES_LEN(tag);
642cb5f885bSMike Smith 		    break;
643cb5f885bSMike Smith 		}
644cb5f885bSMike Smith 	    } else
645cb5f885bSMike Smith 		/* Large resource, skip it */
646cb5f885bSMike Smith 		idx += *(u_int16_t *)(pd->devdata + idx) + 2;
647cb5f885bSMike Smith 	}
648cb5f885bSMike Smith 	if (bootverbose) {
649cb5f885bSMike Smith 	    printf("pnpbios: handle %d device ID %s (%08x)",
650bb2b9030SDoug Rabson 		   pd->handle, pnp_eisaformat(*devid), *devid);
651cb5f885bSMike Smith 	    if (compid != NULL)
652cb5f885bSMike Smith 		printf(" compat ID %s (%08x)",
653bb2b9030SDoug Rabson 		       pnp_eisaformat(*compid), *compid);
654cb5f885bSMike Smith 	    printf("\n");
655cb5f885bSMike Smith 	}
656cb5f885bSMike Smith     }
657cb5f885bSMike Smith }
658cb5f885bSMike Smith 
659bb2b9030SDoug Rabson static device_method_t pnpbios_methods[] = {
660bb2b9030SDoug Rabson 	/* Device interface */
661bb2b9030SDoug Rabson 	DEVMETHOD(device_identify,	pnpbios_identify),
662cb5f885bSMike Smith 
663bb2b9030SDoug Rabson 	{ 0, 0 }
664bb2b9030SDoug Rabson };
665bb2b9030SDoug Rabson 
666bb2b9030SDoug Rabson static driver_t pnpbios_driver = {
667bb2b9030SDoug Rabson 	"pnpbios",
668bb2b9030SDoug Rabson 	pnpbios_methods,
669bb2b9030SDoug Rabson 	1,			/* no softc */
670bb2b9030SDoug Rabson };
671bb2b9030SDoug Rabson 
672bb2b9030SDoug Rabson static devclass_t pnpbios_devclass;
673bb2b9030SDoug Rabson 
674bb2b9030SDoug Rabson DRIVER_MODULE(pnpbios, isa, pnpbios_driver, pnpbios_devclass, 0, 0);
675586079ccSBruce Evans #endif /* DEV_ISA */
676