biosmem.c revision 173118
1227825Stheraven/*-
2227825Stheraven * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3227825Stheraven * All rights reserved.
4227825Stheraven *
5227825Stheraven * Redistribution and use in source and binary forms, with or without
6227825Stheraven * modification, are permitted provided that the following conditions
7227825Stheraven * are met:
8227825Stheraven * 1. Redistributions of source code must retain the above copyright
9227825Stheraven *    notice, this list of conditions and the following disclaimer.
10227825Stheraven * 2. Redistributions in binary form must reproduce the above copyright
11227825Stheraven *    notice, this list of conditions and the following disclaimer in the
12227825Stheraven *    documentation and/or other materials provided with the distribution.
13227825Stheraven *
14227825Stheraven * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15227825Stheraven * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16227825Stheraven * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17227825Stheraven * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18227825Stheraven * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19227825Stheraven * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20227825Stheraven * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21227825Stheraven * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22227825Stheraven * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23227825Stheraven * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24227825Stheraven * SUCH DAMAGE.
25227825Stheraven */
26227825Stheraven
27227825Stheraven#include <sys/cdefs.h>
28227825Stheraven__FBSDID("$FreeBSD: head/sys/boot/i386/libi386/biosmem.c 173118 2007-10-28 21:23:49Z jhb $");
29227825Stheraven
30227825Stheraven/*
31227825Stheraven * Obtain memory configuration information from the BIOS
32227825Stheraven */
33227825Stheraven#include <stand.h>
34227825Stheraven#include <machine/pc/bios.h>
35227825Stheraven#include "libi386.h"
36227825Stheraven#include "btxv86.h"
37227825Stheraven
38227825Stheravenvm_offset_t	memtop, memtop_copyin;
39227825Stheravenu_int32_t	bios_basemem, bios_extmem;
40227825Stheraven
41227825Stheravenstatic struct bios_smap smap;
42227825Stheraven
43227825Stheravenvoid
44227825Stheravenbios_getmem(void)
45227825Stheraven{
46227825Stheraven
47227825Stheraven    /* Parse system memory map */
48227825Stheraven    v86.ebx = 0;
49227825Stheraven    do {
50227825Stheraven	v86.ctl = V86_FLAGS;
51227825Stheraven	v86.addr = 0x15;		/* int 0x15 function 0xe820*/
52227825Stheraven	v86.eax = 0xe820;
53227825Stheraven	v86.ecx = sizeof(struct bios_smap);
54227825Stheraven	v86.edx = SMAP_SIG;
55227825Stheraven	v86.es = VTOPSEG(&smap);
56227825Stheraven	v86.edi = VTOPOFF(&smap);
57227825Stheraven	v86int();
58227825Stheraven	if ((v86.efl & 1) || (v86.eax != SMAP_SIG))
59227825Stheraven	    break;
60227825Stheraven	/* look for a low-memory segment that's large enough */
61227825Stheraven	if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
62227825Stheraven	    (smap.length >= (512 * 1024)))
63227825Stheraven	    bios_basemem = smap.length;
64227825Stheraven	/* look for the first segment in 'extended' memory */
65227825Stheraven	if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0x100000)) {
66227825Stheraven	    bios_extmem = smap.length;
67227825Stheraven	}
68227825Stheraven    } while (v86.ebx != 0);
69227825Stheraven
70227825Stheraven    /* Fall back to the old compatibility function for base memory */
71227825Stheraven    if (bios_basemem == 0) {
72227825Stheraven	v86.ctl = 0;
73227825Stheraven	v86.addr = 0x12;		/* int 0x12 */
74227825Stheraven	v86int();
75227825Stheraven
76227825Stheraven	bios_basemem = (v86.eax & 0xffff) * 1024;
77227825Stheraven    }
78227825Stheraven
79227825Stheraven    /* Fall back through several compatibility functions for extended memory */
80227825Stheraven    if (bios_extmem == 0) {
81227825Stheraven	v86.ctl = V86_FLAGS;
82227825Stheraven	v86.addr = 0x15;		/* int 0x15 function 0xe801*/
83227825Stheraven	v86.eax = 0xe801;
84227825Stheraven	v86int();
85227825Stheraven	if (!(v86.efl & 1)) {
86227825Stheraven	    bios_extmem = ((v86.ecx & 0xffff) + ((v86.edx & 0xffff) * 64)) * 1024;
87227825Stheraven	}
88227825Stheraven    }
89227825Stheraven    if (bios_extmem == 0) {
90227825Stheraven	v86.ctl = 0;
91227825Stheraven	v86.addr = 0x15;		/* int 0x15 function 0x88*/
92227825Stheraven	v86.eax = 0x8800;
93227825Stheraven	v86int();
94227825Stheraven	bios_extmem = (v86.eax & 0xffff) * 1024;
95227825Stheraven    }
96227825Stheraven
97227825Stheraven    /* Set memtop to actual top of memory */
98227825Stheraven    memtop = memtop_copyin = 0x100000 + bios_extmem;
99227825Stheraven
100227825Stheraven}
101227825Stheraven
102227825Stheraven