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 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 41 | alignas(MM_PPOOL_ENTRY_SIZE) char ptable_buf[MM_PPOOL_ENTRY_SIZE * HEAP_PAGES]; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 42 | |
| 43 | static struct mpool ppool; |
| 44 | |
| 45 | /** |
| 46 | * Performs one-time initialisation of memory management for the hypervisor. |
| 47 | * |
| 48 | * This is the only C code entry point called with MMU and caching disabled. The |
| 49 | * page table returned is used to set up the MMU and caches for all subsequent |
| 50 | * code. |
| 51 | */ |
| 52 | void one_time_init_mm(void) |
| 53 | { |
| 54 | /* Make sure the console is initialised before calling dlog. */ |
| 55 | plat_console_init(); |
| 56 | |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 57 | dlog_notice("Initialising hafnium\n"); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 58 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 59 | mpool_init(&ppool, MM_PPOOL_ENTRY_SIZE); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 60 | mpool_add_chunk(&ppool, ptable_buf, sizeof(ptable_buf)); |
| 61 | |
| 62 | if (!mm_init(&ppool)) { |
| 63 | panic("mm_init failed"); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Performs one-time initialisation of the hypervisor. |
| 69 | */ |
| 70 | void one_time_init(void) |
| 71 | { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 72 | struct string manifest_fname = STRING_INIT("manifest.dtb"); |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 73 | struct fdt fdt; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 74 | struct manifest manifest; |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 75 | enum manifest_return_code manifest_ret; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 76 | struct boot_params params; |
| 77 | struct boot_params_update update; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 78 | struct memiter cpio; |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 79 | struct memiter manifest_it; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 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 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 92 | if (!fdt_map(&fdt, mm_stage1_locked, plat_boot_flow_get_fdt_addr(), |
| 93 | &ppool)) { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 94 | panic("Unable to map FDT."); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 95 | } |
| 96 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 97 | if (!boot_flow_get_params(¶ms, &fdt)) { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 98 | panic("Could not parse boot params."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 101 | for (i = 0; i < params.mem_ranges_count; ++i) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 102 | dlog_info("Memory range: %#x - %#x\n", |
| 103 | pa_addr(params.mem_ranges[i].begin), |
| 104 | pa_addr(params.mem_ranges[i].end) - 1); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 107 | dlog_info("Ramdisk range: %#x - %#x\n", pa_addr(params.initrd_begin), |
| 108 | pa_addr(params.initrd_end) - 1); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 109 | |
| 110 | /* Map initrd in, and initialise cpio parser. */ |
| 111 | initrd = mm_identity_map(mm_stage1_locked, params.initrd_begin, |
| 112 | params.initrd_end, MM_MODE_R, &ppool); |
| 113 | if (!initrd) { |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 114 | panic("Unable to map initrd."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | memiter_init(&cpio, initrd, |
| 118 | pa_difference(params.initrd_begin, params.initrd_end)); |
| 119 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 120 | if (!cpio_get_file(&cpio, &manifest_fname, &manifest_it)) { |
| 121 | panic("Could not find manifest in initrd."); |
| 122 | } |
| 123 | |
| 124 | manifest_ret = manifest_init(&manifest, &manifest_it); |
| 125 | if (manifest_ret != MANIFEST_SUCCESS) { |
| 126 | panic("Could not parse manifest: %s.", |
| 127 | manifest_strerror(manifest_ret)); |
| 128 | } |
| 129 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 130 | if (!plat_iommu_init(&fdt, mm_stage1_locked, &ppool)) { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 131 | panic("Could not initialize IOMMUs."); |
| 132 | } |
| 133 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 134 | if (!fdt_unmap(&fdt, mm_stage1_locked, &ppool)) { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 135 | panic("Unable to unmap FDT."); |
| 136 | } |
| 137 | |
| 138 | cpu_module_init(params.cpu_ids, params.cpu_count); |
| 139 | |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 140 | /* Load all VMs. */ |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 141 | update.reserved_ranges_count = 0; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 142 | if (!load_vms(mm_stage1_locked, &manifest, &cpio, ¶ms, &update, |
| 143 | &ppool)) { |
| 144 | panic("Unable to load VMs."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 145 | } |
| 146 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 147 | if (!boot_flow_update(mm_stage1_locked, &manifest, &update, &cpio, |
| 148 | &ppool)) { |
Andrew Scull | c377107 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 149 | panic("Unable to update boot flow."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | mm_defrag(mm_stage1_locked, &ppool); |
| 153 | mm_unlock_stage1(&mm_stage1_locked); |
| 154 | |
| 155 | /* Initialise the API page pool. ppool will be empty from now on. */ |
| 156 | api_init(&ppool); |
| 157 | |
| 158 | /* Enable TLB invalidation for VM page table updates. */ |
| 159 | mm_vm_enable_invalidation(); |
| 160 | |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 161 | dlog_info("Hafnium initialisation completed\n"); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 162 | } |