199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <sys/cdefs.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Obtain memory configuration information from the BIOS
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/pc/bios.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "bootstrap.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "smbios.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "libi386.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "btxv86.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "smbios.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevm_offset_t memtop, memtop_copyin, high_heap_base;
199767f8919635c4928607450d9e0abb932109ceToomas Soomeuint32_t bios_basemem, bios_extmem, high_heap_size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic struct bios_smap_xattr smap;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Used to track which method was used to set BIOS memory
199767f8919635c4928607450d9e0abb932109ceToomas Soome * regions.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic uint8_t b_bios_probed;
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define B_BASEMEM_E820 0x1
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define B_BASEMEM_12 0x2
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define B_EXTMEM_E820 0x4
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define B_EXTMEM_E801 0x8
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define B_EXTMEM_8800 0x10
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The minimum amount of memory to reserve in bios_extmem for the heap.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define HEAP_MIN (64 * 1024 * 1024)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Products in this list need quirks to detect
199767f8919635c4928607450d9e0abb932109ceToomas Soome * memory correctly. You need both maker and product as
199767f8919635c4928607450d9e0abb932109ceToomas Soome * reported by smbios.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define BQ_DISTRUST_E820_EXTMEM 0x1 /* e820 might not return useful
199767f8919635c4928607450d9e0abb932109ceToomas Soome extended memory */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct bios_getmem_quirks {
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char* bios_vendor;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char* maker;
199767f8919635c4928607450d9e0abb932109ceToomas Soome const char* product;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int quirk;
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic struct bios_getmem_quirks quirks[] = {
199767f8919635c4928607450d9e0abb932109ceToomas Soome {"coreboot", "Acer", "Peppy", BQ_DISTRUST_E820_EXTMEM},
199767f8919635c4928607450d9e0abb932109ceToomas Soome {NULL, NULL, NULL, 0}
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomebios_getquirks(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int i;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (i=0; quirks[i].quirk != 0; ++i)
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (smbios_match(quirks[i].bios_vendor, quirks[i].maker,
199767f8919635c4928607450d9e0abb932109ceToomas Soome quirks[i].product))
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (quirks[i].quirk);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomebios_getmem(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint64_t size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Parse system memory map */
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ebx = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome do {
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ctl = V86_FLAGS;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.addr = 0x15; /* int 0x15 function 0xe820*/
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.eax = 0xe820;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ecx = sizeof(struct bios_smap_xattr);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.edx = SMAP_SIG;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.es = VTOPSEG(&smap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.edi = VTOPOFF(&smap);
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86int();
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((V86_CY(v86.efl)) || (v86.eax != SMAP_SIG))
199767f8919635c4928607450d9e0abb932109ceToomas Soome break;
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* look for a low-memory segment that's large enough */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0) &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome (smap.length >= (512 * 1024))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome bios_basemem = smap.length;
199767f8919635c4928607450d9e0abb932109ceToomas Soome b_bios_probed |= B_BASEMEM_E820;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* look for the first segment in 'extended' memory */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base == 0x100000) &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome !(bios_getquirks() & BQ_DISTRUST_E820_EXTMEM)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome bios_extmem = smap.length;
199767f8919635c4928607450d9e0abb932109ceToomas Soome b_bios_probed |= B_EXTMEM_E820;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Look for the largest segment in 'extended' memory beyond
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1MB but below 4GB.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((smap.type == SMAP_TYPE_MEMORY) && (smap.base > 0x100000) &&
199767f8919635c4928607450d9e0abb932109ceToomas Soome (smap.base < 0x100000000ull)) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = smap.length;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If this segment crosses the 4GB boundary, truncate it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (smap.base + size > 0x100000000ull)
199767f8919635c4928607450d9e0abb932109ceToomas Soome size = 0x100000000ull - smap.base;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (size > high_heap_size) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome high_heap_size = size;
199767f8919635c4928607450d9e0abb932109ceToomas Soome high_heap_base = smap.base;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome } while (v86.ebx != 0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Fall back to the old compatibility function for base memory */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bios_basemem == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ctl = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.addr = 0x12; /* int 0x12 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86int();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bios_basemem = (v86.eax & 0xffff) * 1024;
199767f8919635c4928607450d9e0abb932109ceToomas Soome b_bios_probed |= B_BASEMEM_12;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Fall back through several compatibility functions for extended memory */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bios_extmem == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ctl = V86_FLAGS;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.addr = 0x15; /* int 0x15 function 0xe801*/
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.eax = 0xe801;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86int();
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!(V86_CY(v86.efl))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Clear high_heap; it may end up overlapping
199767f8919635c4928607450d9e0abb932109ceToomas Soome * with the segment we're determining here.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Let the default "steal stuff from top of
199767f8919635c4928607450d9e0abb932109ceToomas Soome * bios_extmem" code below pick up on it.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome high_heap_size = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome high_heap_base = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * %cx is the number of 1KiB blocks between 1..16MiB.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * It can only be up to 0x3c00; if it's smaller then
199767f8919635c4928607450d9e0abb932109ceToomas Soome * there's a PC AT memory hole so we can't treat
199767f8919635c4928607450d9e0abb932109ceToomas Soome * it as contiguous.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome bios_extmem = (v86.ecx & 0xffff) * 1024;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bios_extmem == (1024 * 0x3c00))
199767f8919635c4928607450d9e0abb932109ceToomas Soome bios_extmem += (v86.edx & 0xffff) * 64 * 1024;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* truncate bios_extmem */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bios_extmem > 0x3ff00000)
199767f8919635c4928607450d9e0abb932109ceToomas Soome bios_extmem = 0x3ff00000;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome b_bios_probed |= B_EXTMEM_E801;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bios_extmem == 0) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.ctl = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.addr = 0x15; /* int 0x15 function 0x88*/
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86.eax = 0x8800;
199767f8919635c4928607450d9e0abb932109ceToomas Soome v86int();
199767f8919635c4928607450d9e0abb932109ceToomas Soome bios_extmem = (v86.eax & 0xffff) * 1024;
199767f8919635c4928607450d9e0abb932109ceToomas Soome b_bios_probed |= B_EXTMEM_8800;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Set memtop to actual top of memory */
199767f8919635c4928607450d9e0abb932109ceToomas Soome memtop = memtop_copyin = 0x100000 + bios_extmem;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * If we have extended memory and did not find a suitable heap
199767f8919635c4928607450d9e0abb932109ceToomas Soome * region in the SMAP, use the last 3MB of 'extended' memory as a
199767f8919635c4928607450d9e0abb932109ceToomas Soome * high heap candidate.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bios_extmem >= HEAP_MIN && high_heap_size < HEAP_MIN) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome high_heap_size = HEAP_MIN;
199767f8919635c4928607450d9e0abb932109ceToomas Soome high_heap_base = memtop - HEAP_MIN;
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef BOOT2
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomecommand_biosmem(int argc, char *argv[])
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome int bq = bios_getquirks();
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("bios_basemem: 0x%llx\n", (unsigned long long) bios_basemem);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("bios_extmem: 0x%llx\n", (unsigned long long) bios_extmem);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("memtop: 0x%llx\n", (unsigned long long) memtop);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("high_heap_base: 0x%llx\n", (unsigned long long) high_heap_base);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("high_heap_size: 0x%llx\n", (unsigned long long) high_heap_size);
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("bios_quirks: 0x%02x", bq);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (bq & BQ_DISTRUST_E820_EXTMEM)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" BQ_DISTRUST_E820_EXTMEM");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("b_bios_probed: 0x%02x", (int) b_bios_probed);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (b_bios_probed & B_BASEMEM_E820)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" B_BASEMEM_E820");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (b_bios_probed & B_BASEMEM_12)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" B_BASEMEM_12");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (b_bios_probed & B_EXTMEM_E820)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" B_EXTMEM_E820");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (b_bios_probed & B_EXTMEM_E801)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" B_EXTMEM_E801");
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (b_bios_probed & B_EXTMEM_8800)
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf(" B_EXTMEM_8800");
199767f8919635c4928607450d9e0abb932109ceToomas Soome printf("\n");
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (CMD_OK);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas SoomeCOMMAND_SET(biosmem, "biosmem", "show BIOS memory setup", command_biosmem);
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif