1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * LoongArch boot helper functions. 4 * 5 * Copyright (c) 2023 Loongson Technology Corporation Limited 6 */ 7 8 #include "qemu/osdep.h" 9 #include "qemu/units.h" 10 #include "target/loongarch/cpu.h" 11 #include "hw/loongarch/virt.h" 12 #include "hw/loader.h" 13 #include "elf.h" 14 #include "qemu/error-report.h" 15 #include "sysemu/reset.h" 16 #include "sysemu/qtest.h" 17 18 ram_addr_t initrd_offset; 19 uint64_t initrd_size; 20 21 static const unsigned int slave_boot_code[] = { 22 /* Configure reset ebase. */ 23 0x0400302c, /* csrwr $t0, LOONGARCH_CSR_EENTRY */ 24 25 /* Disable interrupt. */ 26 0x0380100c, /* ori $t0, $zero,0x4 */ 27 0x04000180, /* csrxchg $zero, $t0, LOONGARCH_CSR_CRMD */ 28 29 /* Clear mailbox. */ 30 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 31 0x038081ad, /* ori $t1, $t1, CORE_BUF_20 */ 32 0x06481da0, /* iocsrwr.d $zero, $t1 */ 33 34 /* Enable IPI interrupt. */ 35 0x1400002c, /* lu12i.w $t0, 1(0x1) */ 36 0x0400118c, /* csrxchg $t0, $t0, LOONGARCH_CSR_ECFG */ 37 0x02fffc0c, /* addi.d $t0, $r0,-1(0xfff) */ 38 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 39 0x038011ad, /* ori $t1, $t1, CORE_EN_OFF */ 40 0x064819ac, /* iocsrwr.w $t0, $t1 */ 41 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 42 0x038081ad, /* ori $t1, $t1, CORE_BUF_20 */ 43 44 /* Wait for wakeup <.L11>: */ 45 0x06488000, /* idle 0x0 */ 46 0x03400000, /* andi $zero, $zero, 0x0 */ 47 0x064809ac, /* iocsrrd.w $t0, $t1 */ 48 0x43fff59f, /* beqz $t0, -12(0x7ffff4) # 48 <.L11> */ 49 50 /* Read and clear IPI interrupt. */ 51 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 52 0x064809ac, /* iocsrrd.w $t0, $t1 */ 53 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 54 0x038031ad, /* ori $t1, $t1, CORE_CLEAR_OFF */ 55 0x064819ac, /* iocsrwr.w $t0, $t1 */ 56 57 /* Disable IPI interrupt. */ 58 0x1400002c, /* lu12i.w $t0, 1(0x1) */ 59 0x04001180, /* csrxchg $zero, $t0, LOONGARCH_CSR_ECFG */ 60 61 /* Read mail buf and jump to specified entry */ 62 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 63 0x038081ad, /* ori $t1, $t1, CORE_BUF_20 */ 64 0x06480dac, /* iocsrrd.d $t0, $t1 */ 65 0x00150181, /* move $ra, $t0 */ 66 0x4c000020, /* jirl $zero, $ra,0 */ 67 }; 68 69 static inline void *guidcpy(void *dst, const void *src) 70 { 71 return memcpy(dst, src, sizeof(efi_guid_t)); 72 } 73 74 static void init_efi_boot_memmap(struct efi_system_table *systab, 75 void *p, void *start) 76 { 77 unsigned i; 78 struct efi_boot_memmap *boot_memmap = p; 79 efi_guid_t tbl_guid = LINUX_EFI_BOOT_MEMMAP_GUID; 80 81 /* efi_configuration_table 1 */ 82 guidcpy(&systab->tables[0].guid, &tbl_guid); 83 systab->tables[0].table = (struct efi_configuration_table *)(p - start); 84 systab->nr_tables = 1; 85 86 boot_memmap->desc_size = sizeof(efi_memory_desc_t); 87 boot_memmap->desc_ver = 1; 88 boot_memmap->map_size = 0; 89 90 efi_memory_desc_t *map = p + sizeof(struct efi_boot_memmap); 91 for (i = 0; i < memmap_entries; i++) { 92 map = (void *)boot_memmap + sizeof(*map); 93 map[i].type = memmap_table[i].type; 94 map[i].phys_addr = ROUND_UP(memmap_table[i].address, 64 * KiB); 95 map[i].num_pages = ROUND_DOWN(memmap_table[i].address + 96 memmap_table[i].length - map[i].phys_addr, 64 * KiB); 97 p += sizeof(efi_memory_desc_t); 98 } 99 } 100 101 static void init_efi_initrd_table(struct efi_system_table *systab, 102 void *p, void *start) 103 { 104 efi_guid_t tbl_guid = LINUX_EFI_INITRD_MEDIA_GUID; 105 struct efi_initrd *initrd_table = p; 106 107 /* efi_configuration_table 2 */ 108 guidcpy(&systab->tables[1].guid, &tbl_guid); 109 systab->tables[1].table = (struct efi_configuration_table *)(p - start); 110 systab->nr_tables = 2; 111 112 initrd_table->base = initrd_offset; 113 initrd_table->size = initrd_size; 114 } 115 116 static void init_efi_fdt_table(struct efi_system_table *systab) 117 { 118 efi_guid_t tbl_guid = DEVICE_TREE_GUID; 119 120 /* efi_configuration_table 3 */ 121 guidcpy(&systab->tables[2].guid, &tbl_guid); 122 systab->tables[2].table = (void *)FDT_BASE; 123 systab->nr_tables = 3; 124 } 125 126 static void init_systab(struct loongarch_boot_info *info, void *p, void *start) 127 { 128 void *bp_tables_start; 129 struct efi_system_table *systab = p; 130 131 info->a2 = p - start; 132 133 systab->hdr.signature = EFI_SYSTEM_TABLE_SIGNATURE; 134 systab->hdr.revision = EFI_SPECIFICATION_VERSION; 135 systab->hdr.revision = sizeof(struct efi_system_table), 136 systab->fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8; 137 systab->runtime = 0; 138 systab->boottime = 0; 139 systab->nr_tables = 0; 140 141 p += ROUND_UP(sizeof(struct efi_system_table), 64 * KiB); 142 143 systab->tables = p; 144 bp_tables_start = p; 145 146 init_efi_boot_memmap(systab, p, start); 147 p += ROUND_UP(sizeof(struct efi_boot_memmap) + 148 sizeof(efi_memory_desc_t) * memmap_entries, 64 * KiB); 149 init_efi_initrd_table(systab, p, start); 150 p += ROUND_UP(sizeof(struct efi_initrd), 64 * KiB); 151 init_efi_fdt_table(systab); 152 153 systab->tables = (struct efi_configuration_table *)(bp_tables_start - start); 154 } 155 156 static void init_cmdline(struct loongarch_boot_info *info, void *p, void *start) 157 { 158 hwaddr cmdline_addr = p - start; 159 160 info->a0 = 1; 161 info->a1 = cmdline_addr; 162 163 memcpy(p, info->kernel_cmdline, COMMAND_LINE_SIZE); 164 } 165 166 static uint64_t cpu_loongarch_virt_to_phys(void *opaque, uint64_t addr) 167 { 168 return addr & MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS); 169 } 170 171 static int64_t load_kernel_info(struct loongarch_boot_info *info) 172 { 173 uint64_t kernel_entry, kernel_low, kernel_high; 174 ssize_t kernel_size; 175 176 kernel_size = load_elf(info->kernel_filename, NULL, 177 cpu_loongarch_virt_to_phys, NULL, 178 &kernel_entry, &kernel_low, 179 &kernel_high, NULL, 0, 180 EM_LOONGARCH, 1, 0); 181 182 if (kernel_size < 0) { 183 error_report("could not load kernel '%s': %s", 184 info->kernel_filename, 185 load_elf_strerror(kernel_size)); 186 exit(1); 187 } 188 189 if (info->initrd_filename) { 190 initrd_size = get_image_size(info->initrd_filename); 191 if (initrd_size > 0) { 192 initrd_offset = ROUND_UP(kernel_high + 4 * kernel_size, 64 * KiB); 193 194 if (initrd_offset + initrd_size > info->ram_size) { 195 error_report("memory too small for initial ram disk '%s'", 196 info->initrd_filename); 197 exit(1); 198 } 199 200 initrd_size = load_image_targphys(info->initrd_filename, initrd_offset, 201 info->ram_size - initrd_offset); 202 } 203 204 if (initrd_size == (target_ulong)-1) { 205 error_report("could not load initial ram disk '%s'", 206 info->initrd_filename); 207 exit(1); 208 } 209 } else { 210 initrd_size = 0; 211 } 212 213 return kernel_entry; 214 } 215 216 static void reset_load_elf(void *opaque) 217 { 218 LoongArchCPU *cpu = opaque; 219 CPULoongArchState *env = &cpu->env; 220 221 cpu_reset(CPU(cpu)); 222 if (env->load_elf) { 223 if (cpu == LOONGARCH_CPU(first_cpu)) { 224 env->gpr[4] = env->boot_info->a0; 225 env->gpr[5] = env->boot_info->a1; 226 env->gpr[6] = env->boot_info->a2; 227 } 228 cpu_set_pc(CPU(cpu), env->elf_address); 229 } 230 } 231 232 static void fw_cfg_add_kernel_info(struct loongarch_boot_info *info, 233 FWCfgState *fw_cfg) 234 { 235 /* 236 * Expose the kernel, the command line, and the initrd in fw_cfg. 237 * We don't process them here at all, it's all left to the 238 * firmware. 239 */ 240 load_image_to_fw_cfg(fw_cfg, 241 FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA, 242 info->kernel_filename, 243 false); 244 245 if (info->initrd_filename) { 246 load_image_to_fw_cfg(fw_cfg, 247 FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA, 248 info->initrd_filename, false); 249 } 250 251 if (info->kernel_cmdline) { 252 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, 253 strlen(info->kernel_cmdline) + 1); 254 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, 255 info->kernel_cmdline); 256 } 257 } 258 259 static void loongarch_firmware_boot(LoongArchMachineState *lams, 260 struct loongarch_boot_info *info) 261 { 262 fw_cfg_add_kernel_info(info, lams->fw_cfg); 263 } 264 265 static void init_boot_rom(struct loongarch_boot_info *info, void *p) 266 { 267 void *start = p; 268 269 init_cmdline(info, p, start); 270 p += COMMAND_LINE_SIZE; 271 272 init_systab(info, p, start); 273 } 274 275 static void loongarch_direct_kernel_boot(struct loongarch_boot_info *info) 276 { 277 void *p, *bp; 278 int64_t kernel_addr = 0; 279 LoongArchCPU *lacpu; 280 CPUState *cs; 281 282 if (info->kernel_filename) { 283 kernel_addr = load_kernel_info(info); 284 } else { 285 if(!qtest_enabled()) { 286 error_report("Need kernel filename\n"); 287 exit(1); 288 } 289 } 290 291 /* Load cmdline and system tables at [0 - 1 MiB] */ 292 p = g_malloc0(1 * MiB); 293 bp = p; 294 init_boot_rom(info, p); 295 rom_add_blob_fixed_as("boot_info", bp, 1 * MiB, 0, &address_space_memory); 296 297 /* Load slave boot code at pflash0 . */ 298 void *boot_code = g_malloc0(VIRT_FLASH0_SIZE); 299 memcpy(boot_code, &slave_boot_code, sizeof(slave_boot_code)); 300 rom_add_blob_fixed("boot_code", boot_code, VIRT_FLASH0_SIZE, VIRT_FLASH0_BASE); 301 302 CPU_FOREACH(cs) { 303 lacpu = LOONGARCH_CPU(cs); 304 lacpu->env.load_elf = true; 305 if (cs == first_cpu) { 306 lacpu->env.elf_address = kernel_addr; 307 } else { 308 lacpu->env.elf_address = VIRT_FLASH0_BASE; 309 } 310 lacpu->env.boot_info = info; 311 } 312 313 g_free(boot_code); 314 g_free(bp); 315 } 316 317 void loongarch_load_kernel(MachineState *ms, struct loongarch_boot_info *info) 318 { 319 LoongArchMachineState *lams = LOONGARCH_MACHINE(ms); 320 int i; 321 322 /* register reset function */ 323 for (i = 0; i < ms->smp.cpus; i++) { 324 qemu_register_reset(reset_load_elf, LOONGARCH_CPU(qemu_get_cpu(i))); 325 } 326 327 info->kernel_filename = ms->kernel_filename; 328 info->kernel_cmdline = ms->kernel_cmdline; 329 info->initrd_filename = ms->initrd_filename; 330 331 if (lams->bios_loaded) { 332 loongarch_firmware_boot(lams, info); 333 } else { 334 loongarch_direct_kernel_boot(info); 335 } 336 } 337