biosmem.c revision 119880
155342Snyan/*-
255342Snyan * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
355342Snyan * All rights reserved.
453207Snyan *
555342Snyan * Redistribution and use in source and binary forms, with or without
655342Snyan * modification, are permitted provided that the following conditions
755342Snyan * are met:
855342Snyan * 1. Redistributions of source code must retain the above copyright
955342Snyan *    notice, this list of conditions and the following disclaimer.
1055342Snyan * 2. Redistributions in binary form must reproduce the above copyright
1155342Snyan *    notice, this list of conditions and the following disclaimer in the
1255342Snyan *    documentation and/or other materials provided with the distribution.
1355342Snyan *
1455342Snyan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1555342Snyan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1655342Snyan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1755342Snyan * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1855342Snyan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1955342Snyan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2055342Snyan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2155342Snyan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2255342Snyan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2355342Snyan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2455342Snyan * SUCH DAMAGE.
2543561Skato */
2643561Skato
27119880Sobrien#include <sys/cdefs.h>
28119880Sobrien__FBSDID("$FreeBSD: head/sys/boot/pc98/libpc98/biosmem.c 119880 2003-09-08 09:11:32Z obrien $");
29119880Sobrien
3043561Skato/*
3143561Skato * Obtain memory configuration information from the BIOS
3243561Skato */
3343561Skato#include <stand.h>
3468358Snyan#include "libi386.h"
3543561Skato#include "btxv86.h"
3643561Skato
3743561Skatovm_offset_t	memtop;
3855342Snyanu_int32_t	bios_basemem, bios_extmem;
3943561Skato
4055342Snyan#ifndef PC98
4155342Snyan#define SMAPSIG	0x534D4150
4255342Snyan
4355342Snyanstruct smap {
4455342Snyan    u_int64_t	base;
4555342Snyan    u_int64_t	length;
4655342Snyan    u_int32_t	type;
47103870Salfred} __packed;
4855342Snyan
4955342Snyanstatic struct smap smap;
5055342Snyan#endif
5155342Snyan
5255342Snyanvoid
5355342Snyanbios_getmem(void)
5443561Skato{
5555342Snyan
5643561Skato#ifdef PC98
5755342Snyan    bios_basemem = ((*(u_char *)PTOV(0xA1501) & 0x07) + 1) * 128 * 1024;
5855342Snyan    bios_extmem = *(u_char *)PTOV(0xA1401) * 128 * 1024 +
5955342Snyan	*(u_int16_t *)PTOV(0xA1594) * 1024 * 1024;
6043561Skato#else
6155342Snyan    /* Parse system memory map */
6255342Snyan    v86.ebx = 0;
6355342Snyan    do {
6455342Snyan	v86.ctl = V86_FLAGS;
6555342Snyan	v86.addr = 0x15;		/* int 0x15 function 0xe820*/
6655342Snyan	v86.eax = 0xe820;
6755342Snyan	v86.ecx = sizeof(struct smap);
6855342Snyan	v86.edx = SMAPSIG;
6955342Snyan	v86.es = VTOPSEG(&smap);
7055342Snyan	v86.edi = VTOPOFF(&smap);
7155342Snyan	v86int();
7255342Snyan	if ((v86.efl & 1) || (v86.eax != SMAPSIG))
7355342Snyan	    break;
7455342Snyan	/* look for a low-memory segment that's large enough */
7555342Snyan	if ((smap.type == 1) && (smap.base == 0) && (smap.length >= (512 * 1024)))
7655342Snyan	    bios_basemem = smap.length;
7755342Snyan	/* look for the first segment in 'extended' memory */
7855342Snyan	if ((smap.type == 1) && (smap.base == 0x100000)) {
7955342Snyan	    bios_extmem = smap.length;
8055342Snyan	}
8155342Snyan    } while (v86.ebx != 0);
8243561Skato
8355342Snyan    /* Fall back to the old compatibility function for base memory */
8455342Snyan    if (bios_basemem == 0) {
8555342Snyan	v86.ctl = 0;
8655342Snyan	v86.addr = 0x12;		/* int 0x12 */
8755342Snyan	v86int();
8855342Snyan
8955342Snyan	bios_basemem = (v86.eax & 0xffff) * 1024;
9055342Snyan    }
9143561Skato
9255342Snyan    /* Fall back through several compatibility functions for extended memory */
9355342Snyan    if (bios_extmem == 0) {
9455342Snyan	v86.ctl = V86_FLAGS;
9555342Snyan	v86.addr = 0x15;		/* int 0x15 function 0xe801*/
9655342Snyan	v86.eax = 0xe801;
9755342Snyan	v86int();
9855342Snyan	if (!(v86.efl & 1)) {
9955342Snyan	    bios_extmem = ((v86.ecx & 0xffff) + ((v86.edx & 0xffff) * 64)) * 1024;
10055342Snyan	}
10155342Snyan    }
10255342Snyan    if (bios_extmem == 0) {
10355342Snyan	v86.ctl = 0;
10455342Snyan	v86.addr = 0x15;		/* int 0x15 function 0x88*/
10555342Snyan	v86.eax = 0x8800;
10655342Snyan	v86int();
10755342Snyan	bios_extmem = (v86.eax & 0xffff) * 1024;
10855342Snyan    }
10943561Skato#endif
11053207Snyan
11155342Snyan    /* Set memtop to actual top of memory */
11255342Snyan    memtop = 0x100000 + bios_extmem;
11343561Skato
11455342Snyan}
11555342Snyan
116