biosmem.c revision 55211
1234353Sdim/*-
2193323Sed * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3193323Sed * All rights reserved.
4193323Sed *
5193323Sed * Redistribution and use in source and binary forms, with or without
6193323Sed * modification, are permitted provided that the following conditions
7193323Sed * are met:
8193323Sed * 1. Redistributions of source code must retain the above copyright
9193323Sed *    notice, this list of conditions and the following disclaimer.
10193323Sed * 2. Redistributions in binary form must reproduce the above copyright
11193323Sed *    notice, this list of conditions and the following disclaimer in the
12193323Sed *    documentation and/or other materials provided with the distribution.
13193323Sed *
14280031Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15280031Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16193323Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17198090Srdivacky * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23195340Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24195340Sed * SUCH DAMAGE.
25195340Sed *
26195340Sed * $FreeBSD: head/sys/boot/i386/libi386/biosmem.c 55211 1999-12-29 09:54:46Z msmith $
27195340Sed */
28234353Sdim
29276479Sdim/*
30234353Sdim * Obtain memory configuration information from the BIOS
31198090Srdivacky */
32198090Srdivacky#include <stand.h>
33276479Sdim#include "btxv86.h"
34198090Srdivacky
35195340Sedvm_offset_t	memtop;
36195340Sedu_int32_t	bios_basemem, bios_extmem;
37195340Sed
38195340Sed#define SMAPSIG	0x534D4150
39276479Sdim
40280031Sdimstruct smap {
41280031Sdim    u_int64_t	base;
42280031Sdim    u_int64_t	length;
43280031Sdim    u_int32_t	type;
44195340Sed} __attribute__ ((packed));
45195340Sed
46193323Sedstatic struct smap smap;
47193323Sed
48193323Sedvoid
49bios_getmem(void)
50{
51
52    /* Parse system memory map */
53    v86.ebx = 0;
54    do {
55	v86.ctl = V86_FLAGS;
56	v86.addr = 0x15;		/* int 0x15 function 0xe820*/
57	v86.eax = 0xe820;
58	v86.ecx = sizeof(struct smap);
59	v86.edx = SMAPSIG;
60	v86.es = VTOPSEG(&smap);
61	v86.edi = VTOPOFF(&smap);
62	v86int();
63	if ((v86.efl & 1) || (v86.eax != SMAPSIG))
64	    break;
65	/* look for a low-memory segment that's large enough */
66	if ((smap.type == 1) && (smap.base == 0) && (smap.length >= (512 * 1024)))
67	    bios_basemem = smap.length;
68	/* look for the first segment in 'extended' memory */
69	if ((smap.type == 1) && (smap.base == 0x100000)) {
70	    bios_extmem = smap.length;
71	}
72    } while (v86.ebx != 0);
73
74    /* Fall back to the old compatibility function for base memory */
75    if (bios_basemem == 0) {
76	v86.ctl = 0;
77	v86.addr = 0x12;		/* int 0x12 */
78	v86int();
79
80	bios_basemem = (v86.eax & 0xffff) * 1024;
81    }
82
83    /* Fall back through several compatibility functions for extended memory */
84    if (bios_extmem == 0) {
85	v86.ctl = V86_FLAGS;
86	v86.addr = 0x15;		/* int 0x15 function 0xe801*/
87	v86.eax = 0xe801;
88	v86int();
89	if (!(v86.efl & 1)) {
90	    bios_extmem = ((v86.ecx & 0xffff) + ((v86.edx & 0xffff) * 64)) * 1024;
91	}
92    }
93    if (bios_extmem == 0) {
94	v86.ctl = 0;
95	v86.addr = 0x15;		/* int 0x15 function 0x88*/
96	v86.eax = 0x8800;
97	v86int();
98	bios_extmem = (v86.eax & 0xffff) * 1024;
99    }
100
101    /* Set memtop to actual top of memory */
102    memtop = 0x100000 + bios_extmem;
103
104}
105
106