blob: 2f649aff555177de5c2d82c7fca76b37c1cc2a99 [file] [log] [blame]
/*
* Initialize machine setup information and I/O.
*
* After running setup() unit tests may query how many cpus they have
* (nr_cpus), how much memory they have (PHYS_END - PHYS_OFFSET), may
* use dynamic memory allocation (malloc, etc.), printf, and exit.
* Finally, argc and argv are also ready to be passed to main().
*
* Copyright (C) 2014, Red Hat Inc, Andrew Jones <drjones@redhat.com>
*
* This work is licensed under the terms of the GNU LGPL, version 2.
*/
#include <libcflat.h>
#include <libfdt/libfdt.h>
#include <devicetree.h>
#include <memregions.h>
#include <alloc.h>
#include <alloc_phys.h>
#include <alloc_page.h>
#include <vmalloc.h>
#include <auxinfo.h>
#include <argv.h>
#include <asm/thread_info.h>
#include <asm/setup.h>
#include <asm/page.h>
#include <asm/processor.h>
#include <asm/smp.h>
#include <asm/timer.h>
#include <asm/psci.h>
#include "io.h"
#define MAX_DT_MEM_REGIONS 16
#define NR_EXTRA_MEM_REGIONS 64
#define NR_MEM_REGIONS (MAX_DT_MEM_REGIONS + NR_EXTRA_MEM_REGIONS)
extern unsigned long _text, _etext, _data, _edata;
char *initrd;
u32 initrd_size;
u64 cpus[NR_CPUS] = { [0 ... NR_CPUS-1] = (u64)~0 };
int nr_cpus;
static struct mem_region arm_mem_regions[NR_MEM_REGIONS + 1];
phys_addr_t __phys_offset = (phys_addr_t)-1, __phys_end = 0;
extern void exceptions_init(void);
extern void asm_mmu_disable(void);
int mpidr_to_cpu(uint64_t mpidr)
{
int i;
for (i = 0; i < nr_cpus; ++i)
if (cpus[i] == (mpidr & MPIDR_HWID_BITMASK))
return i;
return -1;
}
static void cpu_set_fdt(int fdtnode __unused, u64 regval, void *info __unused)
{
int cpu = nr_cpus++;
assert_msg(cpu < NR_CPUS, "Number cpus exceeds maximum supported (%d).", NR_CPUS);
cpus[cpu] = regval;
set_cpu_present(cpu, true);
}
#ifdef CONFIG_EFI
#include <acpi.h>
static int cpu_set_acpi(struct acpi_subtable_header *header)
{
int cpu = nr_cpus++;
struct acpi_madt_generic_interrupt *gicc = (void *)header;
assert_msg(cpu < NR_CPUS, "Number cpus exceeds maximum supported (%d).", NR_CPUS);
cpus[cpu] = gicc->arm_mpidr;
set_cpu_present(cpu, true);
return 0;
}
static void cpu_init_acpi(void)
{
acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_INTERRUPT, cpu_set_acpi);
}
#else
static void cpu_init_acpi(void)
{
assert_msg(false, "ACPI not available");
}
#endif
static void cpu_init(void)
{
int ret;
nr_cpus = 0;
if (dt_available()) {
ret = dt_for_each_cpu_node(cpu_set_fdt, NULL);
assert(ret == 0);
} else {
cpu_init_acpi();
}
set_cpu_online(0, true);
}
static void arm_memregions_add_assumed(void)
{
struct mem_region *code, *data;
/* Split the region with the code into two regions; code and data */
memregions_split((unsigned long)&_etext, &code, &data);
assert(code);
code->flags |= MR_F_CODE;
/*
* mach-virt I/O regions:
* - The first 1G (arm/arm64)
* - 512M at 256G (arm64, arm uses highmem=off)
* - 512G at 512G (arm64, arm uses highmem=off)
*/
memregions_add(&(struct mem_region){ 0, (1ul << 30), MR_F_IO });
#ifdef __aarch64__
memregions_add(&(struct mem_region){ (1ul << 38), (1ul << 38) | (1ul << 29), MR_F_IO });
memregions_add(&(struct mem_region){ (1ul << 39), (1ul << 40), MR_F_IO });
#endif
}
static void mem_allocator_init(phys_addr_t freemem_start, phys_addr_t freemem_end)
{
phys_addr_t base, top;
freemem_start = PAGE_ALIGN(freemem_start);
freemem_end &= PAGE_MASK;
phys_alloc_init(freemem_start, freemem_end - freemem_start);
phys_alloc_set_minimum_alignment(SMP_CACHE_BYTES);
phys_alloc_get_unused(&base, &top);
base = PAGE_ALIGN(base);
top &= PAGE_MASK;
assert(sizeof(long) == 8 || !(base >> 32));
if (sizeof(long) != 8 && (top >> 32) != 0)
top = ((uint64_t)1 << 32);
page_alloc_init_area(0, base >> PAGE_SHIFT, top >> PAGE_SHIFT);
page_alloc_ops_enable();
}
static void mem_init(phys_addr_t freemem_start)
{
struct mem_region *freemem, *r, mem = {
.start = (phys_addr_t)-1,
};
freemem = memregions_find(freemem_start);
assert(freemem && !(freemem->flags & (MR_F_IO | MR_F_CODE)));
for (r = mem_regions; r->end; ++r) {
if (!(r->flags & MR_F_IO)) {
if (r->start < mem.start)
mem.start = r->start;
if (r->end > mem.end)
mem.end = r->end;
}
}
assert(mem.end && !(mem.start & ~PHYS_MASK));
mem.end &= PHYS_MASK;
/* Check for holes */
r = memregions_find(mem.start);
while (r && r->end != mem.end)
r = memregions_find(r->end);
assert(r);
/* Ensure our selected freemem range is somewhere in our full range */
assert(freemem_start >= mem.start && freemem->end <= mem.end);
__phys_offset = mem.start; /* PHYS_OFFSET */
__phys_end = mem.end; /* PHYS_END */
mem_allocator_init(freemem_start, freemem->end);
}
static void freemem_push_fdt(void **freemem, const void *fdt)
{
u32 fdt_size;
int ret;
fdt_size = fdt_totalsize(fdt);
ret = fdt_move(fdt, *freemem, fdt_size);
assert(ret == 0);
ret = dt_init(*freemem);
assert(ret == 0);
*freemem += fdt_size;
}
static void freemem_push_dt_initrd(void **freemem)
{
const char *tmp;
int ret;
ret = dt_get_initrd(&tmp, &initrd_size);
assert(ret == 0 || ret == -FDT_ERR_NOTFOUND);
if (ret == 0) {
initrd = *freemem;
memmove(initrd, tmp, initrd_size);
*freemem += initrd_size;
}
}
static void initrd_setup(void)
{
char *env;
if (!initrd)
return;
/* environ is currently the only file in the initrd */
env = malloc(initrd_size);
memcpy(env, initrd, initrd_size);
setup_env(env, initrd_size);
}
void setup(const void *fdt, phys_addr_t freemem_start)
{
void *freemem;
const char *bootargs;
int ret;
assert(sizeof(long) == 8 || freemem_start < (3ul << 30));
freemem = (void *)(unsigned long)freemem_start;
freemem_push_fdt(&freemem, fdt);
freemem_push_dt_initrd(&freemem);
memregions_init(arm_mem_regions, NR_MEM_REGIONS);
memregions_add_dt_regions(MAX_DT_MEM_REGIONS);
arm_memregions_add_assumed();
mem_init(PAGE_ALIGN((unsigned long)freemem));
psci_set_conduit();
cpu_init();
/* cpu_init must be called before thread_info_init */
thread_info_init(current_thread_info(), 0);
/* mem_init must be called before io_init */
io_init();
timer_save_state();
ret = dt_get_bootargs(&bootargs);
assert(ret == 0 || ret == -FDT_ERR_NOTFOUND);
setup_args_progname(bootargs);
initrd_setup();
if (!(auxinfo.flags & AUXINFO_MMU_OFF))
setup_vm();
}
#ifdef CONFIG_EFI
#include <efi.h>
static efi_status_t setup_rsdp(efi_bootinfo_t *efi_bootinfo)
{
efi_status_t status;
struct acpi_table_rsdp *rsdp;
/*
* RSDP resides in an EFI_ACPI_RECLAIM_MEMORY region, which is not used
* by kvm-unit-tests arm64 memory allocator. So it is not necessary to
* copy the data structure to another memory region to prevent
* unintentional overwrite.
*/
status = efi_get_system_config_table(ACPI_20_TABLE_GUID, (void **)&rsdp);
if (status != EFI_SUCCESS)
return status;
set_efi_rsdp(rsdp);
return EFI_SUCCESS;
}
static efi_status_t efi_mem_init(efi_bootinfo_t *efi_bootinfo)
{
struct mem_region *freemem_mr = NULL, *code, *data;
phys_addr_t freemem_start;
void *freemem;
memregions_efi_init(&efi_bootinfo->mem_map, &freemem_mr);
if (!freemem_mr)
return EFI_OUT_OF_RESOURCES;
memregions_split((unsigned long)&_etext, &code, &data);
assert(code && (code->flags & MR_F_CODE));
if (data)
data->flags &= ~MR_F_CODE;
for (struct mem_region *m = mem_regions; m->end; ++m) {
if (m != code)
assert(!(m->flags & MR_F_CODE));
if (!(m->flags & MR_F_IO)) {
if (m->start < __phys_offset)
__phys_offset = m->start;
if (m->end > __phys_end)
__phys_end = m->end;
}
}
__phys_end &= PHYS_MASK;
freemem = (void *)PAGE_ALIGN(freemem_mr->start);
if (efi_bootinfo->fdt)
freemem_push_fdt(&freemem, efi_bootinfo->fdt);
freemem_start = PAGE_ALIGN((unsigned long)freemem);
assert(sizeof(long) == 8 || freemem_start < (3ul << 30));
asm_mmu_disable();
mem_allocator_init(freemem_start, freemem_mr->end);
return EFI_SUCCESS;
}
efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo)
{
efi_status_t status;
exceptions_init();
memregions_init(arm_mem_regions, NR_MEM_REGIONS);
status = efi_mem_init(efi_bootinfo);
if (status != EFI_SUCCESS) {
printf("Failed to initialize memory: ");
switch (status) {
case EFI_OUT_OF_RESOURCES:
printf("No free memory region\n");
break;
default:
printf("Unknown error\n");
break;
}
return status;
}
if (!dt_available()) {
status = setup_rsdp(efi_bootinfo);
if (status != EFI_SUCCESS) {
printf("Cannot find RSDP in EFI system table\n");
return status;
}
}
psci_set_conduit();
cpu_init();
/* cpu_init must be called before thread_info_init */
thread_info_init(current_thread_info(), 0);
/* mem_init must be called before io_init */
io_init();
timer_save_state();
initrd_setup();
if (!(auxinfo.flags & AUXINFO_MMU_OFF))
setup_vm();
return EFI_SUCCESS;
}
#endif