Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The Hafnium Authors. |
| 3 | * |
Andrew Walbran | e959ec1 | 2020-06-17 15:01:09 +0100 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style |
| 5 | * license that can be found in the LICENSE file or at |
| 6 | * https://opensource.org/licenses/BSD-3-Clause. |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "hf/arch/init.h" |
| 10 | |
| 11 | #include <stdalign.h> |
| 12 | #include <stddef.h> |
| 13 | |
Olivier Deprez | 112d2b5 | 2020-09-30 07:39:23 +0200 | [diff] [blame] | 14 | #include "hf/arch/other_world.h" |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 15 | #include "hf/arch/plat/ffa.h" |
Andrew Walbran | 41a49d8 | 2020-01-10 17:46:38 +0000 | [diff] [blame] | 16 | |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 17 | #include "hf/api.h" |
| 18 | #include "hf/boot_flow.h" |
| 19 | #include "hf/boot_params.h" |
| 20 | #include "hf/cpio.h" |
| 21 | #include "hf/cpu.h" |
| 22 | #include "hf/dlog.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 23 | #include "hf/fdt_handler.h" |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 24 | #include "hf/load.h" |
J-Alves | 77b6f4f | 2023-03-15 11:34:49 +0000 | [diff] [blame] | 25 | #include "hf/manifest.h" |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 26 | #include "hf/mm.h" |
| 27 | #include "hf/mpool.h" |
| 28 | #include "hf/panic.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 29 | #include "hf/plat/boot_flow.h" |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 30 | #include "hf/plat/console.h" |
Madhukar Pappireddy | 72454a1 | 2021-08-03 12:21:46 -0500 | [diff] [blame] | 31 | #include "hf/plat/interrupts.h" |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 32 | #include "hf/plat/iommu.h" |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 33 | #include "hf/std.h" |
| 34 | #include "hf/vm.h" |
| 35 | |
| 36 | #include "vmapi/hf/call.h" |
| 37 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 38 | 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] | 39 | |
| 40 | static struct mpool ppool; |
| 41 | |
| 42 | /** |
| 43 | * Performs one-time initialisation of memory management for the hypervisor. |
| 44 | * |
| 45 | * This is the only C code entry point called with MMU and caching disabled. The |
| 46 | * page table returned is used to set up the MMU and caches for all subsequent |
| 47 | * code. |
| 48 | */ |
| 49 | void one_time_init_mm(void) |
| 50 | { |
| 51 | /* Make sure the console is initialised before calling dlog. */ |
| 52 | plat_console_init(); |
| 53 | |
Olivier Deprez | 55a189e | 2021-06-09 15:45:27 +0200 | [diff] [blame] | 54 | plat_ffa_log_init(); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 55 | |
Andrew Walbran | 5de9c3d | 2020-02-10 13:35:29 +0000 | [diff] [blame] | 56 | mpool_init(&ppool, MM_PPOOL_ENTRY_SIZE); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 57 | mpool_add_chunk(&ppool, ptable_buf, sizeof(ptable_buf)); |
| 58 | |
| 59 | if (!mm_init(&ppool)) { |
| 60 | panic("mm_init failed"); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Performs one-time initialisation of the hypervisor. |
| 66 | */ |
| 67 | void one_time_init(void) |
| 68 | { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 69 | struct string manifest_fname = STRING_INIT("manifest.dtb"); |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 70 | struct fdt fdt; |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 71 | enum manifest_return_code manifest_ret; |
J-Alves | df099be | 2024-05-13 12:47:33 +0100 | [diff] [blame^] | 72 | struct boot_params *params; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 73 | struct boot_params_update update; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 74 | struct memiter cpio; |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 75 | struct memiter manifest_it; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 76 | void *initrd; |
| 77 | size_t i; |
| 78 | struct mm_stage1_locked mm_stage1_locked; |
Olivier Deprez | 9364465 | 2022-09-09 11:01:12 +0200 | [diff] [blame] | 79 | struct manifest *manifest; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 80 | |
| 81 | arch_one_time_init(); |
| 82 | |
| 83 | /* Enable locks now that mm is initialised. */ |
| 84 | dlog_enable_lock(); |
| 85 | mpool_enable_locks(); |
| 86 | |
| 87 | mm_stage1_locked = mm_lock_stage1(); |
| 88 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 89 | if (!fdt_map(&fdt, mm_stage1_locked, plat_boot_flow_get_fdt_addr(), |
| 90 | &ppool)) { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 91 | panic("Unable to map FDT."); |
Andrew Scull | 3c25745 | 2019-11-26 13:32:50 +0000 | [diff] [blame] | 92 | } |
| 93 | |
J-Alves | df099be | 2024-05-13 12:47:33 +0100 | [diff] [blame^] | 94 | static_assert(sizeof(struct boot_params) <= MM_PPOOL_ENTRY_SIZE, |
| 95 | "The sizeof boot params must fit an entry of the mpool."); |
| 96 | params = (struct boot_params *)mpool_alloc(&ppool); |
| 97 | |
| 98 | if (params == NULL) { |
| 99 | panic("Could not use memory pool to allocate boot params."); |
| 100 | } |
| 101 | |
| 102 | if (!boot_flow_get_params(params, &fdt)) { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 103 | panic("Could not parse boot params."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 104 | } |
| 105 | |
J-Alves | df099be | 2024-05-13 12:47:33 +0100 | [diff] [blame^] | 106 | for (i = 0; i < params->mem_ranges_count; ++i) { |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 107 | dlog_info("Memory range: %#x - %#x\n", |
J-Alves | df099be | 2024-05-13 12:47:33 +0100 | [diff] [blame^] | 108 | pa_addr(params->mem_ranges[i].begin), |
| 109 | pa_addr(params->mem_ranges[i].end) - 1); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 112 | /* |
| 113 | * Hafnium manifest is either gathered from the ramdisk or passed |
| 114 | * directly to Hafnium entry point by the earlier bootloader stage. |
| 115 | * If the ramdisk start address is non-zero it hints the manifest |
| 116 | * shall be looked up from the ramdisk. If zero, assume the address |
| 117 | * passed to Hafnium entry point is the manifest address. |
| 118 | */ |
J-Alves | df099be | 2024-05-13 12:47:33 +0100 | [diff] [blame^] | 119 | if (pa_addr(params->initrd_begin)) { |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 120 | dlog_info("Ramdisk range: %#x - %#x\n", |
J-Alves | df099be | 2024-05-13 12:47:33 +0100 | [diff] [blame^] | 121 | pa_addr(params->initrd_begin), |
| 122 | pa_addr(params->initrd_end) - 1); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 123 | |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 124 | /* Map initrd in, and initialise cpio parser. */ |
J-Alves | df099be | 2024-05-13 12:47:33 +0100 | [diff] [blame^] | 125 | initrd = mm_identity_map(mm_stage1_locked, params->initrd_begin, |
| 126 | params->initrd_end, MM_MODE_R, &ppool); |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 127 | if (!initrd) { |
| 128 | panic("Unable to map initrd."); |
| 129 | } |
| 130 | |
J-Alves | df099be | 2024-05-13 12:47:33 +0100 | [diff] [blame^] | 131 | memiter_init(&cpio, initrd, |
| 132 | pa_difference(params->initrd_begin, |
| 133 | params->initrd_end)); |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 134 | |
| 135 | if (!cpio_get_file(&cpio, &manifest_fname, &manifest_it)) { |
| 136 | panic("Could not find manifest in initrd."); |
| 137 | } |
| 138 | } else { |
| 139 | manifest_it = fdt.buf; |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Dmitrii Martynov | 89190c9 | 2023-02-23 20:20:10 +0300 | [diff] [blame] | 142 | dlog_verbose("Manifest range: %#x - %#x (%d bytes)\n", manifest_it.next, |
| 143 | manifest_it.limit, manifest_it.limit - manifest_it.next); |
| 144 | if (!is_aligned(manifest_it.next, 4)) { |
| 145 | panic("Manifest not aligned."); |
| 146 | } |
| 147 | |
Olivier Deprez | 62d99e3 | 2020-01-09 15:58:07 +0100 | [diff] [blame] | 148 | manifest_ret = manifest_init(mm_stage1_locked, &manifest, &manifest_it, |
J-Alves | df099be | 2024-05-13 12:47:33 +0100 | [diff] [blame^] | 149 | params, &ppool); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 150 | |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 151 | if (manifest_ret != MANIFEST_SUCCESS) { |
| 152 | panic("Could not parse manifest: %s.", |
| 153 | manifest_strerror(manifest_ret)); |
| 154 | } |
| 155 | |
Olivier Deprez | 9364465 | 2022-09-09 11:01:12 +0200 | [diff] [blame] | 156 | plat_ffa_set_tee_enabled(manifest->ffa_tee_enabled); |
J-Alves | a09ac2d | 2022-06-07 13:46:59 +0100 | [diff] [blame] | 157 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 158 | if (!plat_iommu_init(&fdt, mm_stage1_locked, &ppool)) { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 159 | panic("Could not initialize IOMMUs."); |
| 160 | } |
| 161 | |
David Brazdil | b856be6 | 2020-03-25 10:14:55 +0000 | [diff] [blame] | 162 | if (!fdt_unmap(&fdt, mm_stage1_locked, &ppool)) { |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 163 | panic("Unable to unmap FDT."); |
| 164 | } |
| 165 | |
J-Alves | df099be | 2024-05-13 12:47:33 +0100 | [diff] [blame^] | 166 | cpu_module_init(params->cpu_ids, params->cpu_count); |
David Brazdil | a2358d4 | 2020-01-27 18:51:38 +0000 | [diff] [blame] | 167 | |
Madhukar Pappireddy | 72454a1 | 2021-08-03 12:21:46 -0500 | [diff] [blame] | 168 | if (!plat_interrupts_controller_driver_init(&fdt, mm_stage1_locked, |
| 169 | &ppool)) { |
| 170 | panic("Could not initialize Interrupt Controller driver."); |
| 171 | } |
| 172 | |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 173 | /* Load all VMs. */ |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 174 | update.reserved_ranges_count = 0; |
J-Alves | df099be | 2024-05-13 12:47:33 +0100 | [diff] [blame^] | 175 | if (!load_vms(mm_stage1_locked, manifest, &cpio, params, &update, |
Andrew Scull | 72b43c0 | 2019-09-18 13:53:45 +0100 | [diff] [blame] | 176 | &ppool)) { |
| 177 | panic("Unable to load VMs."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Olivier Deprez | 9364465 | 2022-09-09 11:01:12 +0200 | [diff] [blame] | 180 | if (!boot_flow_update(mm_stage1_locked, manifest, &update, &cpio, |
David Brazdil | e6f8322 | 2019-09-23 14:47:37 +0100 | [diff] [blame] | 181 | &ppool)) { |
Andrew Scull | c377107 | 2019-09-19 13:30:42 +0100 | [diff] [blame] | 182 | panic("Unable to update boot flow."); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 183 | } |
| 184 | |
J-Alves | df099be | 2024-05-13 12:47:33 +0100 | [diff] [blame^] | 185 | /* Free space allocated for the boot parameters. */ |
| 186 | mpool_free(&ppool, params); |
| 187 | |
Olivier Deprez | 9364465 | 2022-09-09 11:01:12 +0200 | [diff] [blame] | 188 | /* Now manifest parsing has completed free the resourses used. */ |
| 189 | manifest_deinit(&ppool); |
| 190 | |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 191 | mm_unlock_stage1(&mm_stage1_locked); |
| 192 | |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 193 | /* Enable TLB invalidation for VM page table updates. */ |
| 194 | mm_vm_enable_invalidation(); |
| 195 | |
Daniel Boulby | baeaf2e | 2021-12-09 11:42:36 +0000 | [diff] [blame] | 196 | /* Perform platform specfic FF-A initialization. */ |
Raghu Krishnamurthy | f5fec20 | 2022-09-30 07:25:10 -0700 | [diff] [blame] | 197 | plat_ffa_init(&ppool); |
| 198 | |
| 199 | /* Initialise the API page pool. ppool will be empty from now on. */ |
| 200 | api_init(&ppool); |
Andrew Walbran | 41a49d8 | 2020-01-10 17:46:38 +0000 | [diff] [blame] | 201 | |
Andrew Walbran | 17eebf9 | 2020-02-05 16:35:49 +0000 | [diff] [blame] | 202 | dlog_info("Hafnium initialisation completed\n"); |
Andrew Scull | b291056 | 2019-09-17 14:08:27 +0100 | [diff] [blame] | 203 | } |