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