biosmem.c revision 103870
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. 2555342Snyan * 2653207Snyan * $FreeBSD: head/sys/boot/pc98/libpc98/biosmem.c 103870 2002-09-23 18:54:32Z alfred $ 2743561Skato */ 2843561Skato 2943561Skato/* 3043561Skato * Obtain memory configuration information from the BIOS 3143561Skato */ 3243561Skato#include <stand.h> 3368358Snyan#include "libi386.h" 3443561Skato#include "btxv86.h" 3543561Skato 3643561Skatovm_offset_t memtop; 3755342Snyanu_int32_t bios_basemem, bios_extmem; 3843561Skato 3955342Snyan#ifndef PC98 4055342Snyan#define SMAPSIG 0x534D4150 4155342Snyan 4255342Snyanstruct smap { 4355342Snyan u_int64_t base; 4455342Snyan u_int64_t length; 4555342Snyan u_int32_t type; 46103870Salfred} __packed; 4755342Snyan 4855342Snyanstatic struct smap smap; 4955342Snyan#endif 5055342Snyan 5155342Snyanvoid 5255342Snyanbios_getmem(void) 5343561Skato{ 5455342Snyan 5543561Skato#ifdef PC98 5655342Snyan bios_basemem = ((*(u_char *)PTOV(0xA1501) & 0x07) + 1) * 128 * 1024; 5755342Snyan bios_extmem = *(u_char *)PTOV(0xA1401) * 128 * 1024 + 5855342Snyan *(u_int16_t *)PTOV(0xA1594) * 1024 * 1024; 5943561Skato#else 6055342Snyan /* Parse system memory map */ 6155342Snyan v86.ebx = 0; 6255342Snyan do { 6355342Snyan v86.ctl = V86_FLAGS; 6455342Snyan v86.addr = 0x15; /* int 0x15 function 0xe820*/ 6555342Snyan v86.eax = 0xe820; 6655342Snyan v86.ecx = sizeof(struct smap); 6755342Snyan v86.edx = SMAPSIG; 6855342Snyan v86.es = VTOPSEG(&smap); 6955342Snyan v86.edi = VTOPOFF(&smap); 7055342Snyan v86int(); 7155342Snyan if ((v86.efl & 1) || (v86.eax != SMAPSIG)) 7255342Snyan break; 7355342Snyan /* look for a low-memory segment that's large enough */ 7455342Snyan if ((smap.type == 1) && (smap.base == 0) && (smap.length >= (512 * 1024))) 7555342Snyan bios_basemem = smap.length; 7655342Snyan /* look for the first segment in 'extended' memory */ 7755342Snyan if ((smap.type == 1) && (smap.base == 0x100000)) { 7855342Snyan bios_extmem = smap.length; 7955342Snyan } 8055342Snyan } while (v86.ebx != 0); 8143561Skato 8255342Snyan /* Fall back to the old compatibility function for base memory */ 8355342Snyan if (bios_basemem == 0) { 8455342Snyan v86.ctl = 0; 8555342Snyan v86.addr = 0x12; /* int 0x12 */ 8655342Snyan v86int(); 8755342Snyan 8855342Snyan bios_basemem = (v86.eax & 0xffff) * 1024; 8955342Snyan } 9043561Skato 9155342Snyan /* Fall back through several compatibility functions for extended memory */ 9255342Snyan if (bios_extmem == 0) { 9355342Snyan v86.ctl = V86_FLAGS; 9455342Snyan v86.addr = 0x15; /* int 0x15 function 0xe801*/ 9555342Snyan v86.eax = 0xe801; 9655342Snyan v86int(); 9755342Snyan if (!(v86.efl & 1)) { 9855342Snyan bios_extmem = ((v86.ecx & 0xffff) + ((v86.edx & 0xffff) * 64)) * 1024; 9955342Snyan } 10055342Snyan } 10155342Snyan if (bios_extmem == 0) { 10255342Snyan v86.ctl = 0; 10355342Snyan v86.addr = 0x15; /* int 0x15 function 0x88*/ 10455342Snyan v86.eax = 0x8800; 10555342Snyan v86int(); 10655342Snyan bios_extmem = (v86.eax & 0xffff) * 1024; 10755342Snyan } 10843561Skato#endif 10953207Snyan 11055342Snyan /* Set memtop to actual top of memory */ 11155342Snyan memtop = 0x100000 + bios_extmem; 11243561Skato 11355342Snyan} 11455342Snyan 115