199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 2001 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#include <stand.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <machine/stdarg.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <bootstrap.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <btxv86.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "libi386.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "platform/acfreebsd.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "acconfig.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define ACPI_SYSTEM_XFACE
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "actypes.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include "actbl.h"
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Detect ACPI and export information about the ACPI BIOS into the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * environment.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic ACPI_TABLE_RSDP *biosacpi_find_rsdp(void);
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic ACPI_TABLE_RSDP *biosacpi_search_rsdp(char *base, int length);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define RSDP_CHECKSUM_LENGTH 20
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomevoid
199767f8919635c4928607450d9e0abb932109ceToomas Soomebiosacpi_detect(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome ACPI_TABLE_RSDP *rsdp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char buf[24];
199767f8919635c4928607450d9e0abb932109ceToomas Soome int revision;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* locate and validate the RSDP */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((rsdp = biosacpi_find_rsdp()) == NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* export values from the RSDP */
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "0x%08x", (unsigned int)VTOP(rsdp));
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("acpi.rsdp", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome revision = rsdp->Revision;
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (revision == 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome revision = 1;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "%d", revision);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("acpi.revision", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome strncpy(buf, rsdp->OemId, sizeof(rsdp->OemId));
199767f8919635c4928607450d9e0abb932109ceToomas Soome buf[sizeof(rsdp->OemId)] = '\0';
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("acpi.oem", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "0x%08x", rsdp->RsdtPhysicalAddress);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("acpi.rsdt", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (revision >= 2) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* XXX extended checksum? */
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "0x%016llx",
199767f8919635c4928607450d9e0abb932109ceToomas Soome (unsigned long long)rsdp->XsdtPhysicalAddress);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("acpi.xsdt", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome sprintf(buf, "%d", rsdp->Length);
199767f8919635c4928607450d9e0abb932109ceToomas Soome setenv("acpi.xsdt_length", buf, 1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Find the RSDP in low memory. See section 5.2.2 of the ACPI spec.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic ACPI_TABLE_RSDP *
199767f8919635c4928607450d9e0abb932109ceToomas Soomebiosacpi_find_rsdp(void)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome ACPI_TABLE_RSDP *rsdp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome uint16_t *addr;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* EBDA is the 1 KB addressed by the 16 bit pointer at 0x40E. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome addr = (uint16_t *)PTOV(0x40E);
199767f8919635c4928607450d9e0abb932109ceToomas Soome rsdp = biosacpi_search_rsdp((char *)(intptr_t)(*addr << 4), 0x400);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (rsdp != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rsdp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* Check the upper memory BIOS space, 0xe0000 - 0xfffff. */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((rsdp = biosacpi_search_rsdp((char *)0xe0000, 0x20000)) != NULL)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (rsdp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic ACPI_TABLE_RSDP *
199767f8919635c4928607450d9e0abb932109ceToomas Soomebiosacpi_search_rsdp(char *base, int length)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome ACPI_TABLE_RSDP *rsdp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int8_t *cp, sum;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int ofs, idx;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* search on 16-byte boundaries */
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (ofs = 0; ofs < length; ofs += 16) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome rsdp = (ACPI_TABLE_RSDP *)PTOV(base + ofs);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /* compare signature, validate checksum */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (!strncmp(rsdp->Signature, ACPI_SIG_RSDP, strlen(ACPI_SIG_RSDP))) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome cp = (u_int8_t *)rsdp;
199767f8919635c4928607450d9e0abb932109ceToomas Soome sum = 0;
199767f8919635c4928607450d9e0abb932109ceToomas Soome for (idx = 0; idx < RSDP_CHECKSUM_LENGTH; idx++)
199767f8919635c4928607450d9e0abb932109ceToomas Soome sum += *(cp + idx);
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (sum != 0)
199767f8919635c4928607450d9e0abb932109ceToomas Soome continue;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(rsdp);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome return(NULL);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}