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 | |
Andrew Walbran | 41a49d8 | 2020-01-10 17:46:38 +0000 | [diff] [blame] | 22 | #include "hf/arch/tee.h" |
| 23 | |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 24 | #include "hf/api.h" |
| 25 | #include "hf/boot_flow.h" |
| 26 | #include "hf/boot_params.h" |
| 27 | #include "hf/cpio.h" |
| 28 | #include "hf/cpu.h" |
| 29 | #include "hf/dlog.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 30 | #include "hf/fdt_handler.h" |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 31 | #include "hf/load.h" |
| 32 | #include "hf/mm.h" |
| 33 | #include "hf/mpool.h" |
| 34 | #include "hf/panic.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 35 | #include "hf/plat/boot_flow.h" |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 36 | #include "hf/plat/console.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 37 | #include "hf/plat/iommu.h" |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 38 | #include "hf/std.h" |
| 39 | #include "hf/vm.h" |
| 40 | |
| 41 | #include "vmapi/hf/call.h" |
| 42 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 43 | 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] | 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 | |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 59 | dlog_notice("Initialising hafnium\n"); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 60 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 61 | mpool_init(&ppool, MM_PPOOL_ENTRY_SIZE); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 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 | { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 74 | struct string manifest_fname = STRING_INIT("manifest.dtb"); |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 75 | struct fdt fdt; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 76 | struct manifest manifest; |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 77 | enum manifest_return_code manifest_ret; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 78 | struct boot_params params; |
| 79 | struct boot_params_update update; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 80 | struct memiter cpio; |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 81 | struct memiter manifest_it; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 82 | void *initrd; |
| 83 | size_t i; |
| 84 | struct mm_stage1_locked mm_stage1_locked; |
| 85 | |
| 86 | arch_one_time_init(); |
| 87 | |
| 88 | /* Enable locks now that mm is initialised. */ |
| 89 | dlog_enable_lock(); |
| 90 | mpool_enable_locks(); |
| 91 | |
| 92 | mm_stage1_locked = mm_lock_stage1(); |
| 93 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 94 | if (!fdt_map(&fdt, mm_stage1_locked, plat_boot_flow_get_fdt_addr(), |
| 95 | &ppool)) { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 96 | panic("Unable to map FDT."); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 97 | } |
| 98 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 99 | if (!boot_flow_get_params(¶ms, &fdt)) { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 100 | panic("Could not parse boot params."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 103 | for (i = 0; i < params.mem_ranges_count; ++i) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 104 | dlog_info("Memory range: %#x - %#x\n", |
| 105 | pa_addr(params.mem_ranges[i].begin), |
| 106 | pa_addr(params.mem_ranges[i].end) - 1); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 109 | dlog_info("Ramdisk range: %#x - %#x\n", pa_addr(params.initrd_begin), |
| 110 | pa_addr(params.initrd_end) - 1); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 111 | |
| 112 | /* Map initrd in, and initialise cpio parser. */ |
| 113 | initrd = mm_identity_map(mm_stage1_locked, params.initrd_begin, |
| 114 | params.initrd_end, MM_MODE_R, &ppool); |
| 115 | if (!initrd) { |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 116 | panic("Unable to map initrd."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | memiter_init(&cpio, initrd, |
| 120 | pa_difference(params.initrd_begin, params.initrd_end)); |
| 121 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 122 | if (!cpio_get_file(&cpio, &manifest_fname, &manifest_it)) { |
| 123 | panic("Could not find manifest in initrd."); |
| 124 | } |
| 125 | |
| 126 | manifest_ret = manifest_init(&manifest, &manifest_it); |
| 127 | if (manifest_ret != MANIFEST_SUCCESS) { |
| 128 | panic("Could not parse manifest: %s.", |
| 129 | manifest_strerror(manifest_ret)); |
| 130 | } |
| 131 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 132 | if (!plat_iommu_init(&fdt, mm_stage1_locked, &ppool)) { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 133 | panic("Could not initialize IOMMUs."); |
| 134 | } |
| 135 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 136 | if (!fdt_unmap(&fdt, mm_stage1_locked, &ppool)) { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 137 | panic("Unable to unmap FDT."); |
| 138 | } |
| 139 | |
| 140 | cpu_module_init(params.cpu_ids, params.cpu_count); |
| 141 | |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 142 | /* Load all VMs. */ |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 143 | update.reserved_ranges_count = 0; |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 144 | if (!load_vms(mm_stage1_locked, &manifest, &cpio, ¶ms, &update, |
| 145 | &ppool)) { |
| 146 | panic("Unable to load VMs."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 147 | } |
| 148 | |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 149 | if (!boot_flow_update(mm_stage1_locked, &manifest, &update, &cpio, |
| 150 | &ppool)) { |
Andrew Scull | c377107 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 151 | panic("Unable to update boot flow."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | mm_defrag(mm_stage1_locked, &ppool); |
| 155 | mm_unlock_stage1(&mm_stage1_locked); |
| 156 | |
| 157 | /* Initialise the API page pool. ppool will be empty from now on. */ |
| 158 | api_init(&ppool); |
| 159 | |
| 160 | /* Enable TLB invalidation for VM page table updates. */ |
| 161 | mm_vm_enable_invalidation(); |
| 162 | |
Andrew Walbran | 41a49d8 | 2020-01-10 17:46:38 +0000 | [diff] [blame] | 163 | if (manifest.spci_tee_enabled) { |
| 164 | /* Set up message buffers for TEE dispatcher. */ |
| 165 | arch_tee_init(); |
| 166 | } |
| 167 | |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 168 | dlog_info("Hafnium initialisation completed\n"); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 169 | } |