xref: /freebsd/sys/i386/i386/bios.c (revision 973802422912eea1aa797b97960488bf1f78ca0c)
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  */
27d561028dSMike Smith 
289676a785SDavid E. O'Brien #include <sys/cdefs.h>
299676a785SDavid E. O'Brien __FBSDID("$FreeBSD$");
309676a785SDavid E. O'Brien 
31d561028dSMike Smith /*
32d561028dSMike Smith  * Code for dealing with the BIOS in x86 PC systems.
33d561028dSMike Smith  */
34d561028dSMike Smith 
35586079ccSBruce Evans #include "opt_isa.h"
36586079ccSBruce Evans 
37d561028dSMike Smith #include <sys/param.h>
38d561028dSMike Smith #include <sys/systm.h>
39d561028dSMike Smith #include <sys/kernel.h>
40496027bfSMike Smith #include <sys/malloc.h>
41bb2b9030SDoug Rabson #include <sys/bus.h>
420bbc8826SJohn Baldwin #include <sys/pcpu.h>
43d561028dSMike Smith #include <vm/vm.h>
44d561028dSMike Smith #include <vm/pmap.h>
45d561028dSMike Smith #include <machine/md_var.h>
46496027bfSMike Smith #include <machine/segments.h>
47496027bfSMike Smith #include <machine/stdarg.h>
48496027bfSMike Smith #include <machine/vmparam.h>
49d561028dSMike Smith #include <machine/pc/bios.h>
50586079ccSBruce Evans #ifdef DEV_ISA
51abe0ccd7SPeter Wemm #include <isa/isavar.h>
526c233a71SPeter Wemm #include <isa/pnpreg.h>
53bb2b9030SDoug Rabson #include <isa/pnpvar.h>
54586079ccSBruce Evans #endif
55d561028dSMike Smith 
56d561028dSMike Smith #define BIOS_START	0xe0000
57d561028dSMike Smith #define BIOS_SIZE	0x20000
58d561028dSMike Smith 
59d561028dSMike Smith /* exported lookup results */
607d224206SJohn Baldwin struct bios32_SDentry		PCIbios;
617d224206SJohn Baldwin struct PnPBIOS_table		*PnPBIOStable;
62d561028dSMike Smith 
637d224206SJohn Baldwin static u_int			bios32_SDCI;
64d561028dSMike Smith 
65d561028dSMike Smith /* start fairly early */
66cb5f885bSMike Smith static void			bios32_init(void *junk);
67d561028dSMike Smith SYSINIT(bios32, SI_SUB_CPU, SI_ORDER_ANY, bios32_init, NULL);
68d561028dSMike Smith 
69d561028dSMike Smith /*
70d561028dSMike Smith  * bios32_init
71d561028dSMike Smith  *
72d561028dSMike Smith  * Locate various bios32 entities.
73d561028dSMike Smith  */
74d561028dSMike Smith static void
75d561028dSMike Smith bios32_init(void *junk)
76d561028dSMike Smith {
77d561028dSMike Smith     u_long			sigaddr;
78d561028dSMike Smith     struct bios32_SDheader	*sdh;
79cb5f885bSMike Smith     struct PnPBIOS_table	*pt;
80d561028dSMike Smith     u_int8_t			ck, *cv;
81d561028dSMike Smith     int				i;
82300451c4SMike Smith     char			*p;
83d561028dSMike Smith 
84d561028dSMike Smith     /*
85300451c4SMike Smith      * BIOS32 Service Directory, PCI BIOS
86d561028dSMike Smith      */
87d561028dSMike Smith 
88d561028dSMike Smith     /* look for the signature */
89d561028dSMike Smith     if ((sigaddr = bios_sigsearch(0, "_32_", 4, 16, 0)) != 0) {
90d561028dSMike Smith 
91d561028dSMike Smith 	/* get a virtual pointer to the structure */
92fc93c1bdSBruce Evans 	sdh = (struct bios32_SDheader *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
93d561028dSMike Smith 	for (cv = (u_int8_t *)sdh, ck = 0, i = 0; i < (sdh->len * 16); i++) {
94d561028dSMike Smith 	    ck += cv[i];
95d561028dSMike Smith 	}
96d561028dSMike Smith 	/* If checksum is OK, enable use of the entrypoint */
97391b5c4aSTakanori Watanabe 	if ((ck == 0) && (BIOS_START <= sdh->entry ) &&
98391b5c4aSTakanori Watanabe 	    (sdh->entry < (BIOS_START + BIOS_SIZE))) {
99496027bfSMike Smith 	    bios32_SDCI = BIOS_PADDRTOVADDR(sdh->entry);
100d561028dSMike Smith 	    if (bootverbose) {
101cb5f885bSMike Smith 		printf("bios32: Found BIOS32 Service Directory header at %p\n", sdh);
102cb5f885bSMike Smith 		printf("bios32: Entry = 0x%x (%x)  Rev = %d  Len = %d\n",
103d561028dSMike Smith 		       sdh->entry, bios32_SDCI, sdh->revision, sdh->len);
104d561028dSMike Smith 	    }
105300451c4SMike Smith 
106300451c4SMike Smith 	    /* Allow user override of PCI BIOS search */
107300451c4SMike Smith 	    if (((p = getenv("machdep.bios.pci")) == NULL) || strcmp(p, "disable")) {
108300451c4SMike Smith 
109d561028dSMike Smith 		/* See if there's a PCI BIOS entrypoint here */
110d561028dSMike Smith 		PCIbios.ident.id = 0x49435024;	/* PCI systems should have this */
111d561028dSMike Smith 		if (!bios32_SDlookup(&PCIbios) && bootverbose)
112300451c4SMike Smith 		    printf("pcibios: PCI BIOS entry at 0x%x+0x%x\n", PCIbios.base, PCIbios.entry);
113300451c4SMike Smith 	    }
114d786139cSMaxime Henrion 	    if (p != NULL)
115d786139cSMaxime Henrion 		    freeenv(p);
116d561028dSMike Smith 	} else {
117cb5f885bSMike Smith 	    printf("bios32: Bad BIOS32 Service Directory\n");
118d561028dSMike Smith 	}
119d561028dSMike Smith     }
120d561028dSMike Smith 
121d561028dSMike Smith     /*
122cb5f885bSMike Smith      * PnP BIOS
123300451c4SMike Smith      *
124300451c4SMike Smith      * Allow user override of PnP BIOS search
125cb5f885bSMike Smith      */
126300451c4SMike Smith     if ((((p = getenv("machdep.bios.pnp")) == NULL) || strcmp(p, "disable")) &&
127300451c4SMike Smith 	((sigaddr = bios_sigsearch(0, "$PnP", 4, 16, 0)) != 0)) {
128d561028dSMike Smith 
129d561028dSMike Smith 	/* get a virtual pointer to the structure */
130cb5f885bSMike Smith 	pt = (struct PnPBIOS_table *)(uintptr_t)BIOS_PADDRTOVADDR(sigaddr);
131cb5f885bSMike Smith 	for (cv = (u_int8_t *)pt, ck = 0, i = 0; i < pt->len; i++) {
132d561028dSMike Smith 	    ck += cv[i];
133d561028dSMike Smith 	}
134cb5f885bSMike Smith 	/* If checksum is OK, enable use of the entrypoint */
135d561028dSMike Smith 	if (ck == 0) {
136cb5f885bSMike Smith 	    PnPBIOStable = pt;
137d561028dSMike Smith 	    if (bootverbose) {
138cb5f885bSMike Smith 		printf("pnpbios: Found PnP BIOS data at %p\n", pt);
139cb5f885bSMike Smith 		printf("pnpbios: Entry = %x:%x  Rev = %d.%d\n",
140cb5f885bSMike Smith 		       pt->pmentrybase, pt->pmentryoffset, pt->version >> 4, pt->version & 0xf);
141cb5f885bSMike Smith 		if ((pt->control & 0x3) == 0x01)
142cb5f885bSMike Smith 		    printf("pnpbios: Event flag at %x\n", pt->evflagaddr);
143cb5f885bSMike Smith 		if (pt->oemdevid != 0)
144cb5f885bSMike Smith 		    printf("pnpbios: OEM ID %x\n", pt->oemdevid);
145cb5f885bSMike Smith 
146d561028dSMike Smith 	    }
147d561028dSMike Smith 	} else {
148cb5f885bSMike Smith 	    printf("pnpbios: Bad PnP BIOS data checksum\n");
149d561028dSMike Smith 	}
150d561028dSMike Smith     }
151d786139cSMaxime Henrion     if (p != NULL)
152d786139cSMaxime Henrion 	    freeenv(p);
15376cf257bSPoul-Henning Kamp     if (bootverbose) {
15476cf257bSPoul-Henning Kamp 	    /* look for other know signatures */
15576cf257bSPoul-Henning Kamp 	    printf("Other BIOS signatures found:\n");
15676cf257bSPoul-Henning Kamp     }
157d561028dSMike Smith }
158d561028dSMike Smith 
159d561028dSMike Smith /*
160d561028dSMike Smith  * bios32_SDlookup
161d561028dSMike Smith  *
162d561028dSMike Smith  * Query the BIOS32 Service Directory for the service named in (ent),
163d561028dSMike Smith  * returns nonzero if the lookup fails.  The caller must fill in
164d561028dSMike Smith  * (ent->ident), the remainder are populated on a successful lookup.
165d561028dSMike Smith  */
166d561028dSMike Smith int
167d561028dSMike Smith bios32_SDlookup(struct bios32_SDentry *ent)
168d561028dSMike Smith {
169496027bfSMike Smith     struct bios_regs args;
170d561028dSMike Smith 
171496027bfSMike Smith     if (bios32_SDCI == 0)
172496027bfSMike Smith 	return (1);
173d561028dSMike Smith 
174d561028dSMike Smith     args.eax = ent->ident.id;		/* set up arguments */
175d561028dSMike Smith     args.ebx = args.ecx = args.edx = 0;
176496027bfSMike Smith     bios32(&args, bios32_SDCI, GSEL(GCODE_SEL, SEL_KPL));
177d561028dSMike Smith     if ((args.eax & 0xff) == 0) {	/* success? */
178d561028dSMike Smith 	ent->base = args.ebx;
179d561028dSMike Smith 	ent->len = args.ecx;
180d561028dSMike Smith 	ent->entry = args.edx;
181300451c4SMike Smith 	ent->ventry = BIOS_PADDRTOVADDR(ent->base + ent->entry);
182d561028dSMike Smith 	return (0);			/* all OK */
183d561028dSMike Smith     }
184d561028dSMike Smith     return (1);				/* failed */
185d561028dSMike Smith }
186d561028dSMike Smith 
187496027bfSMike Smith 
188d561028dSMike Smith /*
189d561028dSMike Smith  * bios_sigsearch
190d561028dSMike Smith  *
191d561028dSMike Smith  * Search some or all of the BIOS region for a signature string.
192d561028dSMike Smith  *
193d561028dSMike Smith  * (start)	Optional offset returned from this function
194d561028dSMike Smith  *		(for searching for multiple matches), or NULL
195d561028dSMike Smith  *		to start the search from the base of the BIOS.
196d561028dSMike Smith  *		Note that this will be a _physical_ address in
197d561028dSMike Smith  *		the range 0xe0000 - 0xfffff.
198d561028dSMike Smith  * (sig)	is a pointer to the byte(s) of the signature.
199d561028dSMike Smith  * (siglen)	number of bytes in the signature.
200d561028dSMike Smith  * (paralen)	signature paragraph (alignment) size.
201d561028dSMike Smith  * (sigofs)	offset of the signature within the paragraph.
202d561028dSMike Smith  *
203d561028dSMike Smith  * Returns the _physical_ address of the found signature, 0 if the
204d561028dSMike Smith  * signature was not found.
205d561028dSMike Smith  */
206d561028dSMike Smith 
207d561028dSMike Smith u_int32_t
208d561028dSMike Smith bios_sigsearch(u_int32_t start, u_char *sig, int siglen, int paralen, int sigofs)
209d561028dSMike Smith {
210d561028dSMike Smith     u_char	*sp, *end;
211d561028dSMike Smith 
212d561028dSMike Smith     /* compute the starting address */
213d561028dSMike Smith     if ((start >= BIOS_START) && (start <= (BIOS_START + BIOS_SIZE))) {
214d561028dSMike Smith 	sp = (char *)BIOS_PADDRTOVADDR(start);
215d561028dSMike Smith     } else if (start == 0) {
216d561028dSMike Smith 	sp = (char *)BIOS_PADDRTOVADDR(BIOS_START);
217d561028dSMike Smith     } else {
218d561028dSMike Smith 	return 0;				/* bogus start address */
219d561028dSMike Smith     }
220d561028dSMike Smith 
221d561028dSMike Smith     /* compute the end address */
222d561028dSMike Smith     end = (u_char *)BIOS_PADDRTOVADDR(BIOS_START + BIOS_SIZE);
223d561028dSMike Smith 
224d561028dSMike Smith     /* loop searching */
225d561028dSMike Smith     while ((sp + sigofs + siglen) < end) {
226d561028dSMike Smith 
227d561028dSMike Smith 	/* compare here */
228c7a2b294SMike Smith 	if (!bcmp(sp + sigofs, sig, siglen)) {
229d561028dSMike Smith 	    /* convert back to physical address */
230d561028dSMike Smith 	    return((u_int32_t)BIOS_VADDRTOPADDR(sp));
231d561028dSMike Smith 	}
232d561028dSMike Smith 	sp += paralen;
233d561028dSMike Smith     }
234d561028dSMike Smith     return(0);
235d561028dSMike Smith }
236d561028dSMike Smith 
237496027bfSMike Smith /*
238496027bfSMike Smith  * do not staticize, used by bioscall.s
239496027bfSMike Smith  */
240496027bfSMike Smith union {
241496027bfSMike Smith     struct {
242496027bfSMike Smith 	u_short	offset;
243496027bfSMike Smith 	u_short	segment;
244496027bfSMike Smith     } vec16;
245496027bfSMike Smith     struct {
246496027bfSMike Smith 	u_int	offset;
247496027bfSMike Smith 	u_short	segment;
248496027bfSMike Smith     } vec32;
249496027bfSMike Smith } bioscall_vector;			/* bios jump vector */
250496027bfSMike Smith 
251496027bfSMike Smith void
252496027bfSMike Smith set_bios_selectors(struct bios_segments *seg, int flags)
253496027bfSMike Smith {
254496027bfSMike Smith     struct soft_segment_descriptor ssd = {
255496027bfSMike Smith 	0,			/* segment base address (overwritten) */
256496027bfSMike Smith 	0,			/* length (overwritten) */
257496027bfSMike Smith 	SDT_MEMERA,		/* segment type (overwritten) */
258496027bfSMike Smith 	0,			/* priority level */
259496027bfSMike Smith 	1,			/* descriptor present */
260496027bfSMike Smith 	0, 0,
261496027bfSMike Smith 	1,			/* descriptor size (overwritten) */
262496027bfSMike Smith 	0			/* granularity == byte units */
263496027bfSMike Smith     };
2647e42e2f8SJonathan Lemon     union descriptor *p_gdt;
265496027bfSMike Smith 
2667e42e2f8SJonathan Lemon #ifdef SMP
267ef73ae4bSJake Burkholder     p_gdt = &gdt[PCPU_GET(cpuid) * NGDT];
2687e42e2f8SJonathan Lemon #else
2697e42e2f8SJonathan Lemon     p_gdt = gdt;
2707e42e2f8SJonathan Lemon #endif
271496027bfSMike Smith 
272496027bfSMike Smith     ssd.ssd_base = seg->code32.base;
273496027bfSMike Smith     ssd.ssd_limit = seg->code32.limit;
2747e42e2f8SJonathan Lemon     ssdtosd(&ssd, &p_gdt[GBIOSCODE32_SEL].sd);
275496027bfSMike Smith 
276496027bfSMike Smith     ssd.ssd_def32 = 0;
277496027bfSMike Smith     if (flags & BIOSCODE_FLAG) {
278496027bfSMike Smith 	ssd.ssd_base = seg->code16.base;
279496027bfSMike Smith 	ssd.ssd_limit = seg->code16.limit;
2807e42e2f8SJonathan Lemon 	ssdtosd(&ssd, &p_gdt[GBIOSCODE16_SEL].sd);
281496027bfSMike Smith     }
282496027bfSMike Smith 
283496027bfSMike Smith     ssd.ssd_type = SDT_MEMRWA;
284496027bfSMike Smith     if (flags & BIOSDATA_FLAG) {
285496027bfSMike Smith 	ssd.ssd_base = seg->data.base;
286496027bfSMike Smith 	ssd.ssd_limit = seg->data.limit;
2877e42e2f8SJonathan Lemon 	ssdtosd(&ssd, &p_gdt[GBIOSDATA_SEL].sd);
288496027bfSMike Smith     }
289496027bfSMike Smith 
290496027bfSMike Smith     if (flags & BIOSUTIL_FLAG) {
291496027bfSMike Smith 	ssd.ssd_base = seg->util.base;
292496027bfSMike Smith 	ssd.ssd_limit = seg->util.limit;
2937e42e2f8SJonathan Lemon 	ssdtosd(&ssd, &p_gdt[GBIOSUTIL_SEL].sd);
294496027bfSMike Smith     }
295496027bfSMike Smith 
296496027bfSMike Smith     if (flags & BIOSARGS_FLAG) {
297496027bfSMike Smith 	ssd.ssd_base = seg->args.base;
298496027bfSMike Smith 	ssd.ssd_limit = seg->args.limit;
2997e42e2f8SJonathan Lemon 	ssdtosd(&ssd, &p_gdt[GBIOSARGS_SEL].sd);
300496027bfSMike Smith     }
301496027bfSMike Smith }
302496027bfSMike Smith 
303496027bfSMike Smith extern int vm86pa;
30468b7d21aSMike Smith extern void bios16_jmp(void);
305496027bfSMike Smith 
306496027bfSMike Smith /*
307496027bfSMike Smith  * this routine is really greedy with selectors, and uses 5:
308496027bfSMike Smith  *
309496027bfSMike Smith  * 32-bit code selector:	to return to kernel
310496027bfSMike Smith  * 16-bit code selector:	for running code
311496027bfSMike Smith  *        data selector:	for 16-bit data
312496027bfSMike Smith  *        util selector:	extra utility selector
313496027bfSMike Smith  *        args selector:	to handle pointers
314496027bfSMike Smith  *
315496027bfSMike Smith  * the util selector is set from the util16 entry in bios16_args, if a
316496027bfSMike Smith  * "U" specifier is seen.
317496027bfSMike Smith  *
318496027bfSMike Smith  * See <machine/pc/bios.h> for description of format specifiers
319496027bfSMike Smith  */
320496027bfSMike Smith int
321496027bfSMike Smith bios16(struct bios_args *args, char *fmt, ...)
322496027bfSMike Smith {
323496027bfSMike Smith     char	*p, *stack, *stack_top;
324496027bfSMike Smith     va_list 	ap;
325496027bfSMike Smith     int 	flags = BIOSCODE_FLAG | BIOSDATA_FLAG;
326496027bfSMike Smith     u_int 	i, arg_start, arg_end;
327f1b665c8SPeter Wemm     pt_entry_t	*pte;
328f1b665c8SPeter Wemm     pd_entry_t	*ptd;
329496027bfSMike Smith 
330496027bfSMike Smith     arg_start = 0xffffffff;
331496027bfSMike Smith     arg_end = 0;
332496027bfSMike Smith 
333e6a80efcSMike Smith     /*
334e6a80efcSMike Smith      * Some BIOS entrypoints attempt to copy the largest-case
335e6a80efcSMike Smith      * argument frame (in order to generalise handling for
336e6a80efcSMike Smith      * different entry types).  If our argument frame is
337e6a80efcSMike Smith      * smaller than this, the BIOS will reach off the top of
338e6a80efcSMike Smith      * our constructed stack segment.  Pad the top of the stack
339e6a80efcSMike Smith      * with some garbage to avoid this.
340e6a80efcSMike Smith      */
341e6a80efcSMike Smith     stack = (caddr_t)PAGE_SIZE - 32;
342e6a80efcSMike Smith 
343496027bfSMike Smith     va_start(ap, fmt);
344496027bfSMike Smith     for (p = fmt; p && *p; p++) {
345496027bfSMike Smith 	switch (*p) {
346496027bfSMike Smith 	case 'p':			/* 32-bit pointer */
347496027bfSMike Smith 	    i = va_arg(ap, u_int);
348496027bfSMike Smith 	    arg_start = min(arg_start, i);
349f996ef63SMike Smith 	    arg_end = max(arg_end, i);
350496027bfSMike Smith 	    flags |= BIOSARGS_FLAG;
351496027bfSMike Smith 	    stack -= 4;
352496027bfSMike Smith 	    break;
353496027bfSMike Smith 
354496027bfSMike Smith 	case 'i':			/* 32-bit integer */
355496027bfSMike Smith 	    i = va_arg(ap, u_int);
356496027bfSMike Smith 	    stack -= 4;
357496027bfSMike Smith 	    break;
358496027bfSMike Smith 
359496027bfSMike Smith 	case 'U':			/* 16-bit selector */
360496027bfSMike Smith 	    flags |= BIOSUTIL_FLAG;
361496027bfSMike Smith 	    /* FALLTHROUGH */
362496027bfSMike Smith 	case 'D':			/* 16-bit selector */
363496027bfSMike Smith 	case 'C':			/* 16-bit selector */
364cb5f885bSMike Smith 	    stack -= 2;
365cb5f885bSMike Smith 	    break;
366cb5f885bSMike Smith 
36774f168bfSBruce Evans 	case 's':			/* 16-bit integer passed as an int */
36874f168bfSBruce Evans 	    i = va_arg(ap, int);
369496027bfSMike Smith 	    stack -= 2;
370496027bfSMike Smith 	    break;
371496027bfSMike Smith 
372496027bfSMike Smith 	default:
373496027bfSMike Smith 	    return (EINVAL);
374496027bfSMike Smith 	}
375496027bfSMike Smith     }
376496027bfSMike Smith 
377496027bfSMike Smith     if (flags & BIOSARGS_FLAG) {
378496027bfSMike Smith 	if (arg_end - arg_start > ctob(16))
379496027bfSMike Smith 	    return (EACCES);
380496027bfSMike Smith 	args->seg.args.base = arg_start;
381f996ef63SMike Smith 	args->seg.args.limit = 0xffff;
382496027bfSMike Smith     }
383496027bfSMike Smith 
38468b7d21aSMike Smith     args->seg.code32.base = (u_int)&bios16_jmp & PG_FRAME;
385496027bfSMike Smith     args->seg.code32.limit = 0xffff;
386496027bfSMike Smith 
387f1b665c8SPeter Wemm     ptd = (pd_entry_t *)rcr3();
3887ab9b220SJake Burkholder #ifdef PAE
389618b80ddSPoul-Henning Kamp     if (ptd == IdlePDPT)
3907ab9b220SJake Burkholder #else
391618b80ddSPoul-Henning Kamp     if (ptd == IdlePTD)
3927ab9b220SJake Burkholder #endif
393618b80ddSPoul-Henning Kamp     {
394496027bfSMike Smith 	/*
395496027bfSMike Smith 	 * no page table, so create one and install it.
396496027bfSMike Smith 	 */
397a163d034SWarner Losh 	pte = (pt_entry_t *)malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
3987ab9b220SJake Burkholder 	ptd = (pd_entry_t *)((u_int)IdlePTD + KERNBASE);
3996ccf265bSPeter Wemm 	*pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V;
400496027bfSMike Smith 	*ptd = vtophys(pte) | PG_RW | PG_V;
401496027bfSMike Smith     } else {
402496027bfSMike Smith 	/*
403496027bfSMike Smith 	 * this is a user-level page table
404496027bfSMike Smith 	 */
405f1b665c8SPeter Wemm 	pte = PTmap;
406496027bfSMike Smith 	*pte = (vm86pa - PAGE_SIZE) | PG_RW | PG_V;
4076ccf265bSPeter Wemm     }
4086ccf265bSPeter Wemm     pmap_invalidate_all(kernel_pmap);	/* XXX insurance for now */
409496027bfSMike Smith 
410496027bfSMike Smith     stack_top = stack;
411496027bfSMike Smith     va_start(ap, fmt);
412496027bfSMike Smith     for (p = fmt; p && *p; p++) {
413496027bfSMike Smith 	switch (*p) {
414496027bfSMike Smith 	case 'p':			/* 32-bit pointer */
415496027bfSMike Smith 	    i = va_arg(ap, u_int);
416496027bfSMike Smith 	    *(u_int *)stack = (i - arg_start) |
417496027bfSMike Smith 		(GSEL(GBIOSARGS_SEL, SEL_KPL) << 16);
418496027bfSMike Smith 	    stack += 4;
419496027bfSMike Smith 	    break;
420496027bfSMike Smith 
421496027bfSMike Smith 	case 'i':			/* 32-bit integer */
422496027bfSMike Smith 	    i = va_arg(ap, u_int);
423496027bfSMike Smith 	    *(u_int *)stack = i;
424496027bfSMike Smith 	    stack += 4;
425496027bfSMike Smith 	    break;
426496027bfSMike Smith 
427496027bfSMike Smith 	case 'U':			/* 16-bit selector */
428496027bfSMike Smith 	    *(u_short *)stack = GSEL(GBIOSUTIL_SEL, SEL_KPL);
429496027bfSMike Smith 	    stack += 2;
430496027bfSMike Smith 	    break;
431496027bfSMike Smith 
432496027bfSMike Smith 	case 'D':			/* 16-bit selector */
433496027bfSMike Smith 	    *(u_short *)stack = GSEL(GBIOSDATA_SEL, SEL_KPL);
434496027bfSMike Smith 	    stack += 2;
435496027bfSMike Smith 	    break;
436496027bfSMike Smith 
437496027bfSMike Smith 	case 'C':			/* 16-bit selector */
438496027bfSMike Smith 	    *(u_short *)stack = GSEL(GBIOSCODE16_SEL, SEL_KPL);
439496027bfSMike Smith 	    stack += 2;
440496027bfSMike Smith 	    break;
441496027bfSMike Smith 
44274f168bfSBruce Evans 	case 's':			/* 16-bit integer passed as an int */
44374f168bfSBruce Evans 	    i = va_arg(ap, int);
444496027bfSMike Smith 	    *(u_short *)stack = i;
445496027bfSMike Smith 	    stack += 2;
446496027bfSMike Smith 	    break;
447496027bfSMike Smith 
448496027bfSMike Smith 	default:
449496027bfSMike Smith 	    return (EINVAL);
450496027bfSMike Smith 	}
451496027bfSMike Smith     }
452496027bfSMike Smith 
453496027bfSMike Smith     set_bios_selectors(&args->seg, flags);
454496027bfSMike Smith     bioscall_vector.vec16.offset = (u_short)args->entry;
455496027bfSMike Smith     bioscall_vector.vec16.segment = GSEL(GBIOSCODE16_SEL, SEL_KPL);
456496027bfSMike Smith 
457496027bfSMike Smith     i = bios16_call(&args->r, stack_top);
458496027bfSMike Smith 
459f1b665c8SPeter Wemm     if (pte == PTmap) {
460496027bfSMike Smith 	*pte = 0;			/* remove entry */
461496027bfSMike Smith 	/*
462496027bfSMike Smith 	 * XXX only needs to be invlpg(0) but that doesn't work on the 386
463496027bfSMike Smith 	 */
464f1b665c8SPeter Wemm 	pmap_invalidate_all(kernel_pmap);
4656ccf265bSPeter Wemm     } else {
4666ccf265bSPeter Wemm 	*ptd = 0;			/* remove page table */
4676ccf265bSPeter Wemm 	/*
4686ccf265bSPeter Wemm 	 * XXX only needs to be invlpg(0) but that doesn't work on the 386
4696ccf265bSPeter Wemm 	 */
4706ccf265bSPeter Wemm 	pmap_invalidate_all(kernel_pmap);
4716ccf265bSPeter Wemm 	free(pte, M_TEMP);		/* ... and free it */
4726ccf265bSPeter Wemm     }
473496027bfSMike Smith     return (i);
474496027bfSMike Smith }
475cb5f885bSMike Smith 
476586079ccSBruce Evans #ifdef DEV_ISA
477cb5f885bSMike Smith /*
478cb5f885bSMike Smith  * PnP BIOS interface; enumerate devices only known to the system
479cb5f885bSMike Smith  * BIOS and save information about them for later use.
480cb5f885bSMike Smith  */
481cb5f885bSMike Smith 
482cb5f885bSMike Smith struct pnp_sysdev
483cb5f885bSMike Smith {
484cb5f885bSMike Smith     u_int16_t	size;
485cb5f885bSMike Smith     u_int8_t	handle;
486cb5f885bSMike Smith     u_int32_t	devid;
487cb5f885bSMike Smith     u_int8_t	type[3];
488cb5f885bSMike Smith     u_int16_t	attrib;
489cb5f885bSMike Smith #define PNPATTR_NODISABLE	(1<<0)	/* can't be disabled */
490cb5f885bSMike Smith #define PNPATTR_NOCONFIG	(1<<1)	/* can't be configured */
491cb5f885bSMike Smith #define PNPATTR_OUTPUT		(1<<2)	/* can be primary output */
492cb5f885bSMike Smith #define PNPATTR_INPUT		(1<<3)	/* can be primary input */
493cb5f885bSMike Smith #define PNPATTR_BOOTABLE	(1<<4)	/* can be booted from */
494cb5f885bSMike Smith #define PNPATTR_DOCK		(1<<5)	/* is a docking station */
495cb5f885bSMike Smith #define PNPATTR_REMOVEABLE	(1<<6)	/* device is removeable */
496c3959391SKazutaka YOKOTA #define PNPATTR_CONFIG_STATIC	(0)
497c3959391SKazutaka YOKOTA #define PNPATTR_CONFIG_DYNAMIC	(1)
498c3959391SKazutaka YOKOTA #define PNPATTR_CONFIG_DYNONLY	(3)
499c3959391SKazutaka YOKOTA #define PNPATTR_CONFIG(a)	(((a) >> 7) & 0x3)
500cb5f885bSMike Smith     /* device-specific data comes here */
501cb5f885bSMike Smith     u_int8_t	devdata[0];
5024f492bfaSAlfred Perlstein } __packed;
503cb5f885bSMike Smith 
504cb5f885bSMike Smith /* We have to cluster arguments within a 64k range for the bios16 call */
505cb5f885bSMike Smith struct pnp_sysdevargs
506cb5f885bSMike Smith {
507cb5f885bSMike Smith     u_int16_t	next;
508cb5f885bSMike Smith     struct pnp_sysdev node;
509cb5f885bSMike Smith };
510cb5f885bSMike Smith 
511cb5f885bSMike Smith /*
512bb2b9030SDoug Rabson  * This function is called after the bus has assigned resource
513bb2b9030SDoug Rabson  * locations for a logical device.
514bb2b9030SDoug Rabson  */
515bb2b9030SDoug Rabson static void
516bb2b9030SDoug Rabson pnpbios_set_config(void *arg, struct isa_config *config, int enable)
517bb2b9030SDoug Rabson {
518bb2b9030SDoug Rabson }
519bb2b9030SDoug Rabson 
520bb2b9030SDoug Rabson /*
521cb5f885bSMike Smith  * Quiz the PnP BIOS, build a list of PNP IDs and resource data.
522cb5f885bSMike Smith  */
523cb5f885bSMike Smith static void
524bb2b9030SDoug Rabson pnpbios_identify(driver_t *driver, device_t parent)
525cb5f885bSMike Smith {
526cb5f885bSMike Smith     struct PnPBIOS_table	*pt = PnPBIOStable;
527cb5f885bSMike Smith     struct bios_args		args;
528cb5f885bSMike Smith     struct pnp_sysdev		*pd;
529cb5f885bSMike Smith     struct pnp_sysdevargs	*pda;
530cb5f885bSMike Smith     u_int16_t			ndevs, bigdev;
531cb5f885bSMike Smith     int				error, currdev;
532cb5f885bSMike Smith     u_int8_t			*devnodebuf, tag;
533cb5f885bSMike Smith     u_int32_t			*devid, *compid;
534cb5f885bSMike Smith     int				idx, left;
535bb2b9030SDoug Rabson     device_t			dev;
536cb5f885bSMike Smith 
537cb5f885bSMike Smith     /* no PnP BIOS information */
538cb5f885bSMike Smith     if (pt == NULL)
539cb5f885bSMike Smith 	return;
540cb5f885bSMike Smith 
541a91d0e1cSMike Smith     /* ACPI already active */
542a91d0e1cSMike Smith     if (devclass_get_softc(devclass_find("ACPI"), 0) != NULL)
543a91d0e1cSMike Smith 	return;
544a91d0e1cSMike Smith 
545a91d0e1cSMike Smith     /* get count of PnP devices */
546cb5f885bSMike Smith     bzero(&args, sizeof(args));
547cb5f885bSMike Smith     args.seg.code16.base = BIOS_PADDRTOVADDR(pt->pmentrybase);
548cb5f885bSMike Smith     args.seg.code16.limit = 0xffff;		/* XXX ? */
549cb5f885bSMike Smith     args.seg.data.base = BIOS_PADDRTOVADDR(pt->pmdataseg);
550cb5f885bSMike Smith     args.seg.data.limit = 0xffff;
551cb5f885bSMike Smith     args.entry = pt->pmentryoffset;
552cb5f885bSMike Smith 
553cb5f885bSMike Smith     if ((error = bios16(&args, PNP_COUNT_DEVNODES, &ndevs, &bigdev)) || (args.r.eax & 0xff))
554cb5f885bSMike Smith 	printf("pnpbios: error %d/%x getting device count/size limit\n", error, args.r.eax);
55503c6be5cSMike Smith     ndevs &= 0xff;				/* clear high byte garbage */
556cb5f885bSMike Smith     if (bootverbose)
557cb5f885bSMike Smith 	printf("pnpbios: %d devices, largest %d bytes\n", ndevs, bigdev);
558cb5f885bSMike Smith 
559cb5f885bSMike Smith     devnodebuf = malloc(bigdev + (sizeof(struct pnp_sysdevargs) - sizeof(struct pnp_sysdev)),
560cb5f885bSMike Smith 			M_DEVBUF, M_NOWAIT);
561cb5f885bSMike Smith     pda = (struct pnp_sysdevargs *)devnodebuf;
562cb5f885bSMike Smith     pd = &pda->node;
563cb5f885bSMike Smith 
564cb5f885bSMike Smith     for (currdev = 0, left = ndevs; (currdev != 0xff) && (left > 0); left--) {
565cb5f885bSMike Smith 
566cb5f885bSMike Smith 	bzero(pd, bigdev);
567cb5f885bSMike Smith 	pda->next = currdev;
568cb5f885bSMike Smith 	/* get current configuration */
56974f168bfSBruce Evans 	if ((error = bios16(&args, PNP_GET_DEVNODE, &pda->next, &pda->node, 1))) {
570cb5f885bSMike Smith 	    printf("pnpbios: error %d making BIOS16 call\n", error);
571cb5f885bSMike Smith 	    break;
572cb5f885bSMike Smith 	}
573cb5f885bSMike Smith 	if ((error = (args.r.eax & 0xff))) {
574cb5f885bSMike Smith 	    if (bootverbose)
575cb5f885bSMike Smith 		printf("pnpbios: %s 0x%x fetching node %d\n", error & 0x80 ? "error" : "warning", error, currdev);
576cb5f885bSMike Smith 	    if (error & 0x80)
577cb5f885bSMike Smith 		break;
578cb5f885bSMike Smith 	}
579cb5f885bSMike Smith 	currdev = pda->next;
580cb5f885bSMike Smith 	if (pd->size < sizeof(struct pnp_sysdev)) {
581cb5f885bSMike Smith 	    printf("pnpbios: bogus system node data, aborting scan\n");
582cb5f885bSMike Smith 	    break;
583cb5f885bSMike Smith 	}
584cb5f885bSMike Smith 
585642ba07aSMike Smith 	/*
58697380242SJohn Baldwin 	 * Ignore PICs so that we don't have to worry about the PICs
58797380242SJohn Baldwin 	 * claiming IRQs to prevent their use.  The PIC drivers
58897380242SJohn Baldwin 	 * already ensure that invalid IRQs are not used.
589642ba07aSMike Smith 	 */
5906eb9d785SMike Smith 	if (!strcmp(pnp_eisaformat(pd->devid), "PNP0000"))	/* ISA PIC */
591642ba07aSMike Smith 	    continue;
5926eb9d785SMike Smith 	if (!strcmp(pnp_eisaformat(pd->devid), "PNP0003"))	/* APIC */
593642ba07aSMike Smith 	    continue;
594642ba07aSMike Smith 
595bb2b9030SDoug Rabson 	/* Add the device and parse its resources */
596bb2b9030SDoug Rabson 	dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
597bb2b9030SDoug Rabson 	isa_set_vendorid(dev, pd->devid);
598bb2b9030SDoug Rabson 	isa_set_logicalid(dev, pd->devid);
599c3959391SKazutaka YOKOTA 	/*
600c3959391SKazutaka YOKOTA 	 * It appears that some PnP BIOS doesn't allow us to re-enable
601c3959391SKazutaka YOKOTA 	 * the embedded system device once it is disabled.  We shall
602c3959391SKazutaka YOKOTA 	 * mark all system device nodes as "cannot be disabled", regardless
603c3959391SKazutaka YOKOTA 	 * of actual settings in the device attribute byte.
604c3959391SKazutaka YOKOTA 	 * XXX
605c3959391SKazutaka YOKOTA 	isa_set_configattr(dev,
606c3959391SKazutaka YOKOTA 	    ((pd->attrib & PNPATTR_NODISABLE) ?  0 : ISACFGATTR_CANDISABLE) |
607c3959391SKazutaka YOKOTA 	    ((!(pd->attrib & PNPATTR_NOCONFIG) &&
608c3959391SKazutaka YOKOTA 		PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
609c3959391SKazutaka YOKOTA 		? ISACFGATTR_DYNAMIC : 0));
610c3959391SKazutaka YOKOTA 	 */
611c3959391SKazutaka YOKOTA 	isa_set_configattr(dev,
612c3959391SKazutaka YOKOTA 	    (!(pd->attrib & PNPATTR_NOCONFIG) &&
613c3959391SKazutaka YOKOTA 		PNPATTR_CONFIG(pd->attrib) != PNPATTR_CONFIG_STATIC)
614c3959391SKazutaka YOKOTA 		? ISACFGATTR_DYNAMIC : 0);
615bb2b9030SDoug Rabson 	ISA_SET_CONFIG_CALLBACK(parent, dev, pnpbios_set_config, 0);
616bb2b9030SDoug Rabson 	pnp_parse_resources(dev, &pd->devdata[0],
617c3959391SKazutaka YOKOTA 			    pd->size - sizeof(struct pnp_sysdev), 0);
618bb2b9030SDoug Rabson 	if (!device_get_desc(dev))
619bb2b9030SDoug Rabson 	    device_set_desc_copy(dev, pnp_eisaformat(pd->devid));
620bb2b9030SDoug Rabson 
621cb5f885bSMike Smith 	/* Find device IDs */
622cb5f885bSMike Smith 	devid = &pd->devid;
623cb5f885bSMike Smith 	compid = NULL;
624cb5f885bSMike Smith 
625cb5f885bSMike Smith 	/* look for a compatible device ID too */
626cb5f885bSMike Smith 	left = pd->size - sizeof(struct pnp_sysdev);
627cb5f885bSMike Smith 	idx = 0;
628cb5f885bSMike Smith 	while (idx < left) {
629cb5f885bSMike Smith 	    tag = pd->devdata[idx++];
630cb5f885bSMike Smith 	    if (PNP_RES_TYPE(tag) == 0) {
631cb5f885bSMike Smith 		/* Small resource */
632cb5f885bSMike Smith 		switch (PNP_SRES_NUM(tag)) {
6336c233a71SPeter Wemm 		case PNP_TAG_COMPAT_DEVICE:
634cb5f885bSMike Smith 		    compid = (u_int32_t *)(pd->devdata + idx);
635cb5f885bSMike Smith 		    if (bootverbose)
636cb5f885bSMike Smith 			printf("pnpbios: node %d compat ID 0x%08x\n", pd->handle, *compid);
637cb5f885bSMike Smith 		    /* FALLTHROUGH */
6386c233a71SPeter Wemm 		case PNP_TAG_END:
639cb5f885bSMike Smith 		    idx = left;
640cb5f885bSMike Smith 		    break;
641cb5f885bSMike Smith 		default:
642cb5f885bSMike Smith 		    idx += PNP_SRES_LEN(tag);
643cb5f885bSMike Smith 		    break;
644cb5f885bSMike Smith 		}
645cb5f885bSMike Smith 	    } else
646cb5f885bSMike Smith 		/* Large resource, skip it */
647cb5f885bSMike Smith 		idx += *(u_int16_t *)(pd->devdata + idx) + 2;
648cb5f885bSMike Smith 	}
649cb5f885bSMike Smith 	if (bootverbose) {
650cb5f885bSMike Smith 	    printf("pnpbios: handle %d device ID %s (%08x)",
651bb2b9030SDoug Rabson 		   pd->handle, pnp_eisaformat(*devid), *devid);
652cb5f885bSMike Smith 	    if (compid != NULL)
653cb5f885bSMike Smith 		printf(" compat ID %s (%08x)",
654bb2b9030SDoug Rabson 		       pnp_eisaformat(*compid), *compid);
655cb5f885bSMike Smith 	    printf("\n");
656cb5f885bSMike Smith 	}
657cb5f885bSMike Smith     }
658cb5f885bSMike Smith }
659cb5f885bSMike Smith 
660bb2b9030SDoug Rabson static device_method_t pnpbios_methods[] = {
661bb2b9030SDoug Rabson 	/* Device interface */
662bb2b9030SDoug Rabson 	DEVMETHOD(device_identify,	pnpbios_identify),
663cb5f885bSMike Smith 
664bb2b9030SDoug Rabson 	{ 0, 0 }
665bb2b9030SDoug Rabson };
666bb2b9030SDoug Rabson 
667bb2b9030SDoug Rabson static driver_t pnpbios_driver = {
668bb2b9030SDoug Rabson 	"pnpbios",
669bb2b9030SDoug Rabson 	pnpbios_methods,
670bb2b9030SDoug Rabson 	1,			/* no softc */
671bb2b9030SDoug Rabson };
672bb2b9030SDoug Rabson 
673bb2b9030SDoug Rabson static devclass_t pnpbios_devclass;
674bb2b9030SDoug Rabson 
675bb2b9030SDoug Rabson DRIVER_MODULE(pnpbios, isa, pnpbios_driver, pnpbios_devclass, 0, 0);
676586079ccSBruce Evans #endif /* DEV_ISA */
677