Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The Hafnium Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "hf/arch/init.h" |
| 18 | |
| 19 | #include <stdalign.h> |
| 20 | #include <stddef.h> |
| 21 | |
| 22 | #include "hf/api.h" |
| 23 | #include "hf/boot_flow.h" |
| 24 | #include "hf/boot_params.h" |
| 25 | #include "hf/cpio.h" |
| 26 | #include "hf/cpu.h" |
| 27 | #include "hf/dlog.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 28 | #include "hf/fdt_handler.h" |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 29 | #include "hf/load.h" |
| 30 | #include "hf/mm.h" |
| 31 | #include "hf/mpool.h" |
| 32 | #include "hf/panic.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 33 | #include "hf/plat/boot_flow.h" |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 34 | #include "hf/plat/console.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 35 | #include "hf/plat/iommu.h" |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 36 | #include "hf/std.h" |
| 37 | #include "hf/vm.h" |
| 38 | |
| 39 | #include "vmapi/hf/call.h" |
| 40 | |
| 41 | alignas(alignof( |
| 42 | struct mm_page_table)) char ptable_buf[sizeof(struct mm_page_table) * |
| 43 | HEAP_PAGES]; |
| 44 | |
| 45 | static struct mpool ppool; |
| 46 | |
| 47 | /** |
| 48 | * Performs one-time initialisation of memory management for the hypervisor. |
| 49 | * |
| 50 | * This is the only C code entry point called with MMU and caching disabled. The |
| 51 | * page table returned is used to set up the MMU and caches for all subsequent |
| 52 | * code. |
| 53 | */ |
| 54 | void one_time_init_mm(void) |
| 55 | { |
| 56 | /* Make sure the console is initialised before calling dlog. */ |
| 57 | plat_console_init(); |
| 58 | |
| 59 | dlog("Initialising hafnium\n"); |
| 60 | |
| 61 | mpool_init(&ppool, sizeof(struct mm_page_table)); |
| 62 | mpool_add_chunk(&ppool, ptable_buf, sizeof(ptable_buf)); |
| 63 | |
| 64 | if (!mm_init(&ppool)) { |
| 65 | panic("mm_init failed"); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Performs one-time initialisation of the hypervisor. |
| 71 | */ |
| 72 | void one_time_init(void) |
| 73 | { |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 74 | struct fdt_header *fdt; |
| 75 | struct fdt_node fdt_root; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 76 | struct manifest manifest; |
| 77 | struct boot_params params; |
| 78 | struct boot_params_update update; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 79 | struct memiter cpio; |
| 80 | void *initrd; |
| 81 | size_t i; |
| 82 | struct mm_stage1_locked mm_stage1_locked; |
| 83 | |
| 84 | arch_one_time_init(); |
| 85 | |
| 86 | /* Enable locks now that mm is initialised. */ |
| 87 | dlog_enable_lock(); |
| 88 | mpool_enable_locks(); |
| 89 | |
| 90 | mm_stage1_locked = mm_lock_stage1(); |
| 91 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 92 | fdt = fdt_map(mm_stage1_locked, plat_boot_flow_get_fdt_addr(), |
| 93 | &fdt_root, &ppool); |
| 94 | if (fdt == NULL) { |
| 95 | panic("Unable to map FDT.\n"); |
| 96 | } |
| 97 | |
| 98 | if (!fdt_find_child(&fdt_root, "")) { |
| 99 | panic("Unable to find FDT root node.\n"); |
| 100 | } |
| 101 | |
| 102 | if (!boot_flow_init(&fdt_root, &manifest, ¶ms)) { |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 103 | panic("Could not parse data from FDT."); |
| 104 | } |
| 105 | |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 106 | if (!plat_iommu_init(&fdt_root, mm_stage1_locked, &ppool)) { |
| 107 | panic("Could not initialize IOMMUs."); |
| 108 | } |
| 109 | |
| 110 | if (!fdt_unmap(mm_stage1_locked, fdt, &ppool)) { |
| 111 | panic("Unable to unmap FDT.\n"); |
| 112 | } |
| 113 | |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 114 | cpu_module_init(params.cpu_ids, params.cpu_count); |
| 115 | |
| 116 | for (i = 0; i < params.mem_ranges_count; ++i) { |
| 117 | dlog("Memory range: %#x - %#x\n", |
| 118 | pa_addr(params.mem_ranges[i].begin), |
| 119 | pa_addr(params.mem_ranges[i].end) - 1); |
| 120 | } |
| 121 | |
| 122 | dlog("Ramdisk range: %#x - %#x\n", pa_addr(params.initrd_begin), |
| 123 | pa_addr(params.initrd_end) - 1); |
| 124 | |
| 125 | /* Map initrd in, and initialise cpio parser. */ |
| 126 | initrd = mm_identity_map(mm_stage1_locked, params.initrd_begin, |
| 127 | params.initrd_end, MM_MODE_R, &ppool); |
| 128 | if (!initrd) { |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 129 | panic("Unable to map initrd."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | memiter_init(&cpio, initrd, |
| 133 | pa_difference(params.initrd_begin, params.initrd_end)); |
| 134 | |
| 135 | /* Load all VMs. */ |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 136 | update.reserved_ranges_count = 0; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 137 | if (!load_vms(mm_stage1_locked, &manifest, &cpio, ¶ms, &update, |
| 138 | &ppool)) { |
| 139 | panic("Unable to load VMs."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 140 | } |
| 141 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 142 | if (!boot_flow_update(mm_stage1_locked, &manifest, &update, &cpio, |
| 143 | &ppool)) { |
Andrew Scull | c377107 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 144 | panic("Unable to update boot flow."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | mm_defrag(mm_stage1_locked, &ppool); |
| 148 | mm_unlock_stage1(&mm_stage1_locked); |
| 149 | |
| 150 | /* Initialise the API page pool. ppool will be empty from now on. */ |
| 151 | api_init(&ppool); |
| 152 | |
| 153 | /* Enable TLB invalidation for VM page table updates. */ |
| 154 | mm_vm_enable_invalidation(); |
| 155 | |
| 156 | dlog("Hafnium initialisation completed\n"); |
| 157 | } |