blob: 2f241b8911f04c7a84a88655a0e1204748bf6d34 [file] [log] [blame]
/*
* Copyright 2018 The Hafnium Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "hf/arch/init.h"
#include <stdalign.h>
#include <stddef.h>
#include "hf/api.h"
#include "hf/boot_flow.h"
#include "hf/boot_params.h"
#include "hf/cpio.h"
#include "hf/cpu.h"
#include "hf/dlog.h"
#include "hf/fdt_handler.h"
#include "hf/load.h"
#include "hf/mm.h"
#include "hf/mpool.h"
#include "hf/panic.h"
#include "hf/plat/boot_flow.h"
#include "hf/plat/console.h"
#include "hf/plat/iommu.h"
#include "hf/std.h"
#include "hf/vm.h"
#include "vmapi/hf/call.h"
alignas(MM_PPOOL_ENTRY_SIZE) char ptable_buf[MM_PPOOL_ENTRY_SIZE * HEAP_PAGES];
static struct mpool ppool;
/**
* Performs one-time initialisation of memory management for the hypervisor.
*
* This is the only C code entry point called with MMU and caching disabled. The
* page table returned is used to set up the MMU and caches for all subsequent
* code.
*/
void one_time_init_mm(void)
{
/* Make sure the console is initialised before calling dlog. */
plat_console_init();
dlog_notice("Initialising hafnium\n");
mpool_init(&ppool, MM_PPOOL_ENTRY_SIZE);
mpool_add_chunk(&ppool, ptable_buf, sizeof(ptable_buf));
if (!mm_init(&ppool)) {
panic("mm_init failed");
}
}
/**
* Performs one-time initialisation of the hypervisor.
*/
void one_time_init(void)
{
struct string manifest_fname = STRING_INIT("manifest.dtb");
struct fdt fdt;
struct manifest manifest;
enum manifest_return_code manifest_ret;
struct boot_params params;
struct boot_params_update update;
struct memiter cpio;
struct memiter manifest_it;
void *initrd;
size_t i;
struct mm_stage1_locked mm_stage1_locked;
arch_one_time_init();
/* Enable locks now that mm is initialised. */
dlog_enable_lock();
mpool_enable_locks();
mm_stage1_locked = mm_lock_stage1();
if (!fdt_map(&fdt, mm_stage1_locked, plat_boot_flow_get_fdt_addr(),
&ppool)) {
panic("Unable to map FDT.");
}
if (!boot_flow_get_params(&params, &fdt)) {
panic("Could not parse boot params.");
}
for (i = 0; i < params.mem_ranges_count; ++i) {
dlog_info("Memory range: %#x - %#x\n",
pa_addr(params.mem_ranges[i].begin),
pa_addr(params.mem_ranges[i].end) - 1);
}
dlog_info("Ramdisk range: %#x - %#x\n", pa_addr(params.initrd_begin),
pa_addr(params.initrd_end) - 1);
/* Map initrd in, and initialise cpio parser. */
initrd = mm_identity_map(mm_stage1_locked, params.initrd_begin,
params.initrd_end, MM_MODE_R, &ppool);
if (!initrd) {
panic("Unable to map initrd.");
}
memiter_init(&cpio, initrd,
pa_difference(params.initrd_begin, params.initrd_end));
if (!cpio_get_file(&cpio, &manifest_fname, &manifest_it)) {
panic("Could not find manifest in initrd.");
}
manifest_ret = manifest_init(&manifest, &manifest_it);
if (manifest_ret != MANIFEST_SUCCESS) {
panic("Could not parse manifest: %s.",
manifest_strerror(manifest_ret));
}
if (!plat_iommu_init(&fdt, mm_stage1_locked, &ppool)) {
panic("Could not initialize IOMMUs.");
}
if (!fdt_unmap(&fdt, mm_stage1_locked, &ppool)) {
panic("Unable to unmap FDT.");
}
cpu_module_init(params.cpu_ids, params.cpu_count);
/* Load all VMs. */
update.reserved_ranges_count = 0;
if (!load_vms(mm_stage1_locked, &manifest, &cpio, &params, &update,
&ppool)) {
panic("Unable to load VMs.");
}
if (!boot_flow_update(mm_stage1_locked, &manifest, &update, &cpio,
&ppool)) {
panic("Unable to update boot flow.");
}
mm_defrag(mm_stage1_locked, &ppool);
mm_unlock_stage1(&mm_stage1_locked);
/* Initialise the API page pool. ppool will be empty from now on. */
api_init(&ppool);
/* Enable TLB invalidation for VM page table updates. */
mm_vm_enable_invalidation();
dlog_info("Hafnium initialisation completed\n");
}