initial import

internal commit: 14ac2bc2299032fa6714d1fefa7cf0987b3e3085

Signed-off-by: Eddie Dong <eddie.dong@intel.com>
This commit is contained in:
Eddie Dong
2018-03-07 20:57:14 +08:00
committed by Jack Ren
commit f4cd4338fd
156 changed files with 41265 additions and 0 deletions

263
boot/acpi.c Normal file
View File

@@ -0,0 +1,263 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
*
* Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
* Copyright (c) 2018 Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <hypervisor.h>
#include <hv_lib.h>
#include <cpu.h>
#include <acrn_common.h>
#include <bsp_extern.h>
#include <hv_arch.h>
#include <hv_debug.h>
#include "acpi.h"
#define ACPI_SIG_RSDP "RSD PTR " /* Root System Description Ptr */
#define ACPI_OEM_ID_SIZE 6
#define ACPI_SIG_MADT "APIC" /* Multiple APIC Description Table */
#define ACPI_SIG_DMAR "DMAR"
#define RSDP_CHECKSUM_LENGTH 20
#define ACPI_NAME_SIZE 4
#define ACPI_MADT_TYPE_LOCAL_APIC 0
#define ACPI_MADT_ENABLED 1
#define ACPI_OEM_TABLE_ID_SIZE 8
struct acpi_table_rsdp {
/* ACPI signature, contains "RSD PTR " */
char signature[8];
/* ACPI 1.0 checksum */
uint8_t checksum;
/* OEM identification */
char oem_id[ACPI_OEM_ID_SIZE];
/* Must be (0) for ACPI 1.0 or (2) for ACPI 2.0+ */
uint8_t revision;
/* 32-bit physical address of the RSDT */
uint32_t rsdt_physical_address;
/* Table length in bytes, including header (ACPI 2.0+) */
uint32_t length;
/* 64-bit physical address of the XSDT (ACPI 2.0+) */
uint64_t xsdt_physical_address;
/* Checksum of entire table (ACPI 2.0+) */
uint8_t extended_checksum;
/* Reserved, must be zero */
uint8_t reserved[3];
};
struct acpi_table_rsdt {
/* Common ACPI table header */
struct acpi_table_header header;
/* Array of pointers to ACPI tables */
uint32_t table_offset_entry[1];
} __packed;
struct acpi_table_xsdt {
/* Common ACPI table header */
struct acpi_table_header header;
/* Array of pointers to ACPI tables */
uint64_t table_offset_entry[1];
} __packed;
struct acpi_subtable_header {
uint8_t type;
uint8_t length;
};
struct acpi_table_madt {
/* Common ACPI table header */
struct acpi_table_header header;
/* Physical address of local APIC */
uint32_t address;
uint32_t flags;
};
struct acpi_madt_local_apic {
struct acpi_subtable_header header;
/* ACPI processor id */
uint8_t processor_id;
/* Processor's local APIC id */
uint8_t id;
uint32_t lapic_flags;
};
static void *global_rsdp;
static uint64_t madt;
static struct acpi_table_rsdp*
biosacpi_search_rsdp(char *base, int length)
{
struct acpi_table_rsdp *rsdp;
uint8_t *cp, sum;
int ofs, idx;
/* search on 16-byte boundaries */
for (ofs = 0; ofs < length; ofs += 16) {
rsdp = (struct acpi_table_rsdp *)(base + ofs);
/* compare signature, validate checksum */
if (!strncmp(rsdp->signature, ACPI_SIG_RSDP,
strnlen_s(ACPI_SIG_RSDP, 8))) {
cp = (uint8_t *)rsdp;
sum = 0;
for (idx = 0; idx < RSDP_CHECKSUM_LENGTH; idx++)
sum += *(cp + idx);
if (sum != 0)
continue;
return rsdp;
}
}
return NULL;
}
static void *get_rsdp(void)
{
struct acpi_table_rsdp *rsdp = NULL;
uint16_t *addr;
/* EBDA is addressed by the 16 bit pointer at 0x40E */
addr = (uint16_t *)0x40E;
rsdp = biosacpi_search_rsdp((char *)((uint64_t)(*addr << 4)), 0x400);
if (rsdp != NULL)
return rsdp;
/* Check the upper memory BIOS space, 0xe0000 - 0xfffff. */
rsdp = biosacpi_search_rsdp((char *)0xe0000, 0x20000);
if (rsdp != NULL)
return rsdp;
return rsdp;
}
static int
probe_table(uint64_t address, const char *sig)
{
struct acpi_table_header *table = (struct acpi_table_header *)address;
if (strncmp(table->signature, sig, ACPI_NAME_SIZE) != 0)
return 0;
return 1;
}
uint64_t get_acpi_tbl(char *sig)
{
struct acpi_table_rsdp *rsdp;
struct acpi_table_rsdt *rsdt;
struct acpi_table_xsdt *xsdt;
uint64_t addr = 0;
int i, count;
rsdp = (struct acpi_table_rsdp *)global_rsdp;
if (rsdp->revision >= 2 && rsdp->xsdt_physical_address) {
/*
* AcpiOsGetRootPointer only verifies the checksum for
* the version 1.0 portion of the RSDP. Version 2.0 has
* an additional checksum that we verify first.
*/
xsdt = (struct acpi_table_xsdt *)(rsdp->xsdt_physical_address);
count = (xsdt->header.length -
sizeof(struct acpi_table_header)) /
sizeof(uint64_t);
for (i = 0; i < count; i++) {
if (probe_table(xsdt->table_offset_entry[i], sig)) {
addr = xsdt->table_offset_entry[i];
break;
}
}
} else {
/* Root table is an RSDT (32-bit physical addresses) */
rsdt = (struct acpi_table_rsdt *)
((void *)(uint64_t)rsdp->rsdt_physical_address);
count = (rsdt->header.length -
sizeof(struct acpi_table_header)) /
sizeof(uint32_t);
for (i = 0; i < count; i++) {
if (probe_table(rsdt->table_offset_entry[i], sig)) {
addr = rsdt->table_offset_entry[i];
break;
}
}
}
return addr;
}
static int _parse_madt(uint64_t madt, uint8_t *lapic_id_base)
{
int pcpu_id = 0;
struct acpi_madt_local_apic *processor;
struct acpi_table_madt *madt_ptr;
void *first;
void *end;
struct acpi_subtable_header *entry;
madt_ptr = (struct acpi_table_madt *)madt;
first = madt_ptr + 1;
end = (char *)madt_ptr + madt_ptr->header.length;
for (entry = first; (void *)entry < end; ) {
if (entry->length < sizeof(struct acpi_subtable_header))
continue;
if (entry->type == ACPI_MADT_TYPE_LOCAL_APIC) {
processor = (struct acpi_madt_local_apic *)entry;
if (processor->lapic_flags & ACPI_MADT_ENABLED) {
*lapic_id_base++ = processor->id;
pcpu_id++;
}
}
entry = (struct acpi_subtable_header *)
(((uint64_t)entry) + entry->length);
}
return pcpu_id;
}
/* The lapic_id info gotten from madt will be returned in lapic_id_base */
int parse_madt(uint8_t *lapic_id_base)
{
global_rsdp = get_rsdp();
ASSERT(global_rsdp != NULL, "fail to get rsdp");
madt = get_acpi_tbl(ACPI_SIG_MADT);
ASSERT(madt != 0, "fail to get madt");
return _parse_madt(madt, lapic_id_base);
}
uint64_t get_dmar_table(void)
{
return get_acpi_tbl(ACPI_SIG_DMAR);
}

360
boot/dmar_parse.c Normal file
View File

@@ -0,0 +1,360 @@
/*
* Copyright (C) 2018 Intel Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "bsp_cfg.h"
#ifdef CONFIG_DMAR_PARSE_ENABLED
#include <hypervisor.h>
#include <hv_lib.h>
#include <acrn_common.h>
#include <hv_arch.h>
#include <hv_debug.h>
#include "vtd.h"
#include "acpi.h"
#define PCI_CONFIG_ADDRESS 0xcf8
#define PCI_CONFIG_DATA 0xcfc
#define PCI_CONFIG_ACCESS_EN 0x80000000
enum acpi_dmar_type {
ACPI_DMAR_TYPE_HARDWARE_UNIT = 0,
ACPI_DMAR_TYPE_RESERVED_MEMORY = 1,
ACPI_DMAR_TYPE_ROOT_ATS = 2,
ACPI_DMAR_TYPE_HARDWARE_AFFINITY = 3,
ACPI_DMAR_TYPE_NAMESPACE = 4,
ACPI_DMAR_TYPE_RESERVED = 5
};
/* Values for entry_type in ACPI_DMAR_DEVICE_SCOPE - device types */
enum acpi_dmar_scope_type {
ACPI_DMAR_SCOPE_TYPE_NOT_USED = 0,
ACPI_DMAR_SCOPE_TYPE_ENDPOINT = 1,
ACPI_DMAR_SCOPE_TYPE_BRIDGE = 2,
ACPI_DMAR_SCOPE_TYPE_IOAPIC = 3,
ACPI_DMAR_SCOPE_TYPE_HPET = 4,
ACPI_DMAR_SCOPE_TYPE_NAMESPACE = 5,
ACPI_DMAR_SCOPE_TYPE_RESERVED = 6 /* 6 and greater are reserved */
};
struct acpi_table_dmar {
/* Common ACPI table header */
struct acpi_table_header header;
/* Host address Width */
uint8_t width;
uint8_t flags;
uint8_t reserved[10];
};
/* DMAR subtable header */
struct acpi_dmar_header {
uint16_t type;
uint16_t length;
};
struct acpi_dmar_hardware_unit {
struct acpi_dmar_header header;
uint8_t flags;
uint8_t reserved;
uint16_t segment;
/* register base address */
uint64_t address;
};
struct find_iter_args {
int i;
struct acpi_dmar_hardware_unit *res;
};
struct acpi_dmar_pci_path {
uint8_t device;
uint8_t function;
};
struct acpi_dmar_device_scope {
uint8_t entry_type;
uint8_t length;
uint16_t reserved;
uint8_t enumeration_id;
uint8_t bus;
};
typedef int (*dmar_iter_t)(struct acpi_dmar_header*, void*);
static struct dmar_info dmar_info_parsed;
static int dmar_unit_cnt;
static void
dmar_iterate_tbl(dmar_iter_t iter, void *arg)
{
struct acpi_table_dmar *dmar_tbl;
struct acpi_dmar_header *dmar_header;
char *ptr, *ptr_end;
dmar_tbl = (struct acpi_table_dmar *)get_dmar_table();
ASSERT(dmar_tbl != NULL, "");
ptr = (char *)dmar_tbl + sizeof(*dmar_tbl);
ptr_end = (char *)dmar_tbl + dmar_tbl->header.length;
for (;;) {
if (ptr >= ptr_end)
break;
dmar_header = (struct acpi_dmar_header *)ptr;
if (dmar_header->length <= 0) {
pr_err("drhd: corrupted DMAR table, l %d\n",
dmar_header->length);
break;
}
ptr += dmar_header->length;
if (!iter(dmar_header, arg))
break;
}
}
static int
drhd_count_iter(struct acpi_dmar_header *dmar_header, __unused void *arg)
{
if (dmar_header->type == ACPI_DMAR_TYPE_HARDWARE_UNIT)
dmar_unit_cnt++;
return 1;
}
static int
drhd_find_iter(struct acpi_dmar_header *dmar_header, void *arg)
{
struct find_iter_args *args;
if (dmar_header->type != ACPI_DMAR_TYPE_HARDWARE_UNIT)
return 1;
args = arg;
if (args->i == 0) {
args->res = (struct acpi_dmar_hardware_unit *)dmar_header;
return 0;
}
args->i--;
return 1;
}
static struct acpi_dmar_hardware_unit *
drhd_find_by_index(int idx)
{
struct find_iter_args args;
args.i = idx;
args.res = NULL;
dmar_iterate_tbl(drhd_find_iter, &args);
return args.res;
}
static uint8_t get_secondary_bus(uint8_t bus, uint8_t dev, uint8_t func)
{
uint32_t data;
io_write_long(PCI_CONFIG_ACCESS_EN | (bus << 16) | (dev << 11) |
(func << 8) | 0x18, PCI_CONFIG_ADDRESS);
data = io_read_long(PCI_CONFIG_DATA);
return (data >> 8) & 0xff;
}
static uint16_t
dmar_path_bdf(int path_len, int busno,
const struct acpi_dmar_pci_path *path)
{
int i;
uint8_t bus;
uint8_t dev;
uint8_t fun;
bus = (uint8_t)busno;
dev = path->device;
fun = path->function;
for (i = 1; i < path_len; i++) {
bus = get_secondary_bus(bus, dev, fun);
dev = path[i].device;
fun = path[i].function;
}
return (bus << 8 | DEVFUN(dev, fun));
}
static int
handle_dmar_devscope(struct dmar_dev_scope *dev_scope,
void *addr, int remaining)
{
int path_len;
uint16_t bdf;
struct acpi_dmar_pci_path *path;
struct acpi_dmar_device_scope *apci_devscope = addr;
if (remaining < (int)sizeof(struct acpi_dmar_device_scope))
return -1;
if (remaining < apci_devscope->length)
return -1;
path = (struct acpi_dmar_pci_path *)(apci_devscope + 1);
path_len = (apci_devscope->length -
sizeof(struct acpi_dmar_device_scope)) /
sizeof(struct acpi_dmar_pci_path);
bdf = dmar_path_bdf(path_len, apci_devscope->bus, path);
dev_scope->bus = (bdf >> 8) & 0xff;
dev_scope->devfun = bdf & 0xff;
return apci_devscope->length;
}
static uint32_t
get_drhd_dev_scope_cnt(struct acpi_dmar_hardware_unit *drhd)
{
struct acpi_dmar_device_scope *scope;
char *start;
char *end;
uint32_t count = 0;
start = (char *)drhd + sizeof(struct acpi_dmar_hardware_unit);
end = (char *)drhd + drhd->header.length;
while (start < end) {
scope = (struct acpi_dmar_device_scope *)start;
if (scope->entry_type == ACPI_DMAR_SCOPE_TYPE_ENDPOINT ||
scope->entry_type == ACPI_DMAR_SCOPE_TYPE_BRIDGE ||
scope->entry_type == ACPI_DMAR_SCOPE_TYPE_NAMESPACE)
count++;
start += scope->length;
}
return count;
}
static int
handle_one_drhd(struct acpi_dmar_hardware_unit *acpi_drhd,
struct dmar_drhd *drhd)
{
struct dmar_dev_scope *dev_scope;
struct acpi_dmar_device_scope *ads;
int remaining, consumed;
char *cp;
uint32_t dev_count;
drhd->segment = acpi_drhd->segment;
drhd->flags = acpi_drhd->flags;
drhd->reg_base_addr = acpi_drhd->address;
if (drhd->flags & DRHD_FLAG_INCLUDE_PCI_ALL_MASK) {
drhd->dev_cnt = 0;
drhd->devices = NULL;
return 0;
}
dev_count = get_drhd_dev_scope_cnt(acpi_drhd);
drhd->dev_cnt = dev_count;
if (dev_count) {
drhd->devices =
calloc(dev_count, sizeof(struct dmar_dev_scope));
ASSERT(drhd->devices, "");
} else {
drhd->devices = NULL;
return 0;
}
remaining = acpi_drhd->header.length -
sizeof(struct acpi_dmar_hardware_unit);
dev_scope = drhd->devices;
while (remaining > 0) {
cp = (char *)acpi_drhd + acpi_drhd->header.length - remaining;
consumed = handle_dmar_devscope(dev_scope, cp, remaining);
if (((drhd->segment << 16) |
(dev_scope->bus << 8) |
dev_scope->devfun) == CONFIG_GPU_SBDF) {
ASSERT(dev_count == 1, "no dedicated iommu for gpu");
drhd->ignore = true;
}
if (consumed <= 0)
break;
remaining -= consumed;
/* skip IOAPIC & HPET */
ads = (struct acpi_dmar_device_scope *)cp;
if (ads->entry_type != ACPI_DMAR_SCOPE_TYPE_IOAPIC &&
ads->entry_type != ACPI_DMAR_SCOPE_TYPE_HPET)
dev_scope++;
else
pr_dbg("drhd: skip dev_scope type %d",
ads->entry_type);
}
return 0;
}
int parse_dmar_table(void)
{
int i;
struct acpi_dmar_hardware_unit *acpi_drhd;
/* find out how many dmar units */
dmar_iterate_tbl(drhd_count_iter, NULL);
/* alloc memory for dmar uint */
dmar_info_parsed.drhd_units =
calloc(dmar_unit_cnt, sizeof(struct dmar_drhd));
ASSERT(dmar_info_parsed.drhd_units, "");
dmar_info_parsed.drhd_count = dmar_unit_cnt;
for (i = 0; i < dmar_unit_cnt; i++) {
acpi_drhd = drhd_find_by_index(i);
if (acpi_drhd == NULL)
continue;
if (acpi_drhd->flags & DRHD_FLAG_INCLUDE_PCI_ALL_MASK)
ASSERT((i+1) == dmar_unit_cnt,
"drhd with flags set should be the last one");
handle_one_drhd(acpi_drhd, &dmar_info_parsed.drhd_units[i]);
}
return 0;
}
struct dmar_info *get_dmar_info(void)
{
parse_dmar_table();
return &dmar_info_parsed;
}
#endif

58
boot/include/acpi.h Normal file
View File

@@ -0,0 +1,58 @@
/*
* Copyright (C) 2018 Intel Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef ACPI_H
#define ACPI_H
struct acpi_table_header {
/* ASCII table signature */
char signature[4];
/* Length of table in bytes, including this header */
uint32_t length;
/* ACPI Specification minor version number */
uint8_t revision;
/* To make sum of entire table == 0 */
uint8_t checksum;
/* ASCII OEM identification */
char oem_id[6];
/* ASCII OEM table identification */
char oem_table_id[8];
/* OEM revision number */
uint32_t oem_revision;
/* ASCII ASL compiler vendor ID */
char asl_compiler_id[4];
/* ASL compiler version */
uint32_t asl_compiler_revision;
};
int parse_madt(uint8_t *lapic_id_base);
uint64_t get_dmar_table(void);
#endif /* !ACPI_H */