1d771ca1cSSong Gao /* SPDX-License-Identifier: GPL-2.0-or-later */ 2d771ca1cSSong Gao /* 3d771ca1cSSong Gao * LoongArch boot helper functions. 4d771ca1cSSong Gao * 5d771ca1cSSong Gao * Copyright (c) 2023 Loongson Technology Corporation Limited 6d771ca1cSSong Gao */ 7d771ca1cSSong Gao 8d771ca1cSSong Gao #include "qemu/osdep.h" 9d771ca1cSSong Gao #include "qemu/units.h" 10d771ca1cSSong Gao #include "target/loongarch/cpu.h" 11d771ca1cSSong Gao #include "hw/loongarch/virt.h" 12d771ca1cSSong Gao #include "hw/loader.h" 13d771ca1cSSong Gao #include "elf.h" 14d771ca1cSSong Gao #include "qemu/error-report.h" 15d771ca1cSSong Gao #include "sysemu/reset.h" 16d771ca1cSSong Gao #include "sysemu/qtest.h" 17d771ca1cSSong Gao 1806068504SSong Gao ram_addr_t initrd_offset; 1906068504SSong Gao uint64_t initrd_size; 2006068504SSong Gao 217e0510d7SSong Gao static const unsigned int slave_boot_code[] = { 227e0510d7SSong Gao /* Configure reset ebase. */ 237e0510d7SSong Gao 0x0400302c, /* csrwr $t0, LOONGARCH_CSR_EENTRY */ 247e0510d7SSong Gao 257e0510d7SSong Gao /* Disable interrupt. */ 267e0510d7SSong Gao 0x0380100c, /* ori $t0, $zero,0x4 */ 277e0510d7SSong Gao 0x04000180, /* csrxchg $zero, $t0, LOONGARCH_CSR_CRMD */ 287e0510d7SSong Gao 297e0510d7SSong Gao /* Clear mailbox. */ 307e0510d7SSong Gao 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 317e0510d7SSong Gao 0x038081ad, /* ori $t1, $t1, CORE_BUF_20 */ 327e0510d7SSong Gao 0x06481da0, /* iocsrwr.d $zero, $t1 */ 337e0510d7SSong Gao 347e0510d7SSong Gao /* Enable IPI interrupt. */ 357e0510d7SSong Gao 0x1400002c, /* lu12i.w $t0, 1(0x1) */ 367e0510d7SSong Gao 0x0400118c, /* csrxchg $t0, $t0, LOONGARCH_CSR_ECFG */ 377e0510d7SSong Gao 0x02fffc0c, /* addi.d $t0, $r0,-1(0xfff) */ 387e0510d7SSong Gao 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 397e0510d7SSong Gao 0x038011ad, /* ori $t1, $t1, CORE_EN_OFF */ 407e0510d7SSong Gao 0x064819ac, /* iocsrwr.w $t0, $t1 */ 417e0510d7SSong Gao 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 427e0510d7SSong Gao 0x038081ad, /* ori $t1, $t1, CORE_BUF_20 */ 437e0510d7SSong Gao 447e0510d7SSong Gao /* Wait for wakeup <.L11>: */ 457e0510d7SSong Gao 0x06488000, /* idle 0x0 */ 467e0510d7SSong Gao 0x03400000, /* andi $zero, $zero, 0x0 */ 477e0510d7SSong Gao 0x064809ac, /* iocsrrd.w $t0, $t1 */ 487e0510d7SSong Gao 0x43fff59f, /* beqz $t0, -12(0x7ffff4) # 48 <.L11> */ 497e0510d7SSong Gao 507e0510d7SSong Gao /* Read and clear IPI interrupt. */ 517e0510d7SSong Gao 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 527e0510d7SSong Gao 0x064809ac, /* iocsrrd.w $t0, $t1 */ 537e0510d7SSong Gao 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 547e0510d7SSong Gao 0x038031ad, /* ori $t1, $t1, CORE_CLEAR_OFF */ 557e0510d7SSong Gao 0x064819ac, /* iocsrwr.w $t0, $t1 */ 567e0510d7SSong Gao 577e0510d7SSong Gao /* Disable IPI interrupt. */ 587e0510d7SSong Gao 0x1400002c, /* lu12i.w $t0, 1(0x1) */ 597e0510d7SSong Gao 0x04001180, /* csrxchg $zero, $t0, LOONGARCH_CSR_ECFG */ 607e0510d7SSong Gao 617e0510d7SSong Gao /* Read mail buf and jump to specified entry */ 627e0510d7SSong Gao 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 637e0510d7SSong Gao 0x038081ad, /* ori $t1, $t1, CORE_BUF_20 */ 647e0510d7SSong Gao 0x06480dac, /* iocsrrd.d $t0, $t1 */ 657e0510d7SSong Gao 0x00150181, /* move $ra, $t0 */ 667e0510d7SSong Gao 0x4c000020, /* jirl $zero, $ra,0 */ 677e0510d7SSong Gao }; 687e0510d7SSong Gao 69252b8e68SSong Gao static inline void *guidcpy(void *dst, const void *src) 70252b8e68SSong Gao { 71252b8e68SSong Gao return memcpy(dst, src, sizeof(efi_guid_t)); 72252b8e68SSong Gao } 73252b8e68SSong Gao 74252b8e68SSong Gao static void init_efi_boot_memmap(struct efi_system_table *systab, 75252b8e68SSong Gao void *p, void *start) 76252b8e68SSong Gao { 77252b8e68SSong Gao unsigned i; 78252b8e68SSong Gao struct efi_boot_memmap *boot_memmap = p; 79252b8e68SSong Gao efi_guid_t tbl_guid = LINUX_EFI_BOOT_MEMMAP_GUID; 80252b8e68SSong Gao 81252b8e68SSong Gao /* efi_configuration_table 1 */ 82252b8e68SSong Gao guidcpy(&systab->tables[0].guid, &tbl_guid); 83252b8e68SSong Gao systab->tables[0].table = (struct efi_configuration_table *)(p - start); 84252b8e68SSong Gao systab->nr_tables = 1; 85252b8e68SSong Gao 86252b8e68SSong Gao boot_memmap->desc_size = sizeof(efi_memory_desc_t); 87252b8e68SSong Gao boot_memmap->desc_ver = 1; 88252b8e68SSong Gao boot_memmap->map_size = 0; 89252b8e68SSong Gao 90252b8e68SSong Gao efi_memory_desc_t *map = p + sizeof(struct efi_boot_memmap); 91252b8e68SSong Gao for (i = 0; i < memmap_entries; i++) { 92252b8e68SSong Gao map = (void *)boot_memmap + sizeof(*map); 93252b8e68SSong Gao map[i].type = memmap_table[i].type; 94252b8e68SSong Gao map[i].phys_addr = ROUND_UP(memmap_table[i].address, 64 * KiB); 95252b8e68SSong Gao map[i].num_pages = ROUND_DOWN(memmap_table[i].address + 96252b8e68SSong Gao memmap_table[i].length - map[i].phys_addr, 64 * KiB); 97252b8e68SSong Gao p += sizeof(efi_memory_desc_t); 98252b8e68SSong Gao } 99252b8e68SSong Gao } 100252b8e68SSong Gao 10106068504SSong Gao static void init_efi_initrd_table(struct efi_system_table *systab, 10206068504SSong Gao void *p, void *start) 10306068504SSong Gao { 10406068504SSong Gao efi_guid_t tbl_guid = LINUX_EFI_INITRD_MEDIA_GUID; 10506068504SSong Gao struct efi_initrd *initrd_table = p; 10606068504SSong Gao 10706068504SSong Gao /* efi_configuration_table 2 */ 10806068504SSong Gao guidcpy(&systab->tables[1].guid, &tbl_guid); 10906068504SSong Gao systab->tables[1].table = (struct efi_configuration_table *)(p - start); 11006068504SSong Gao systab->nr_tables = 2; 11106068504SSong Gao 11206068504SSong Gao initrd_table->base = initrd_offset; 11306068504SSong Gao initrd_table->size = initrd_size; 11406068504SSong Gao } 11506068504SSong Gao 116*60423851SSong Gao static void init_efi_fdt_table(struct efi_system_table *systab) 117*60423851SSong Gao { 118*60423851SSong Gao efi_guid_t tbl_guid = DEVICE_TREE_GUID; 119*60423851SSong Gao 120*60423851SSong Gao /* efi_configuration_table 3 */ 121*60423851SSong Gao guidcpy(&systab->tables[2].guid, &tbl_guid); 122*60423851SSong Gao systab->tables[2].table = (void *)FDT_BASE; 123*60423851SSong Gao systab->nr_tables = 3; 124*60423851SSong Gao } 125*60423851SSong Gao 1264216baa9SSong Gao static void init_systab(struct loongarch_boot_info *info, void *p, void *start) 1274216baa9SSong Gao { 128252b8e68SSong Gao void *bp_tables_start; 1294216baa9SSong Gao struct efi_system_table *systab = p; 1304216baa9SSong Gao 1314216baa9SSong Gao info->a2 = p - start; 1324216baa9SSong Gao 1334216baa9SSong Gao systab->hdr.signature = EFI_SYSTEM_TABLE_SIGNATURE; 1344216baa9SSong Gao systab->hdr.revision = EFI_SPECIFICATION_VERSION; 1354216baa9SSong Gao systab->hdr.revision = sizeof(struct efi_system_table), 1364216baa9SSong Gao systab->fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8; 1374216baa9SSong Gao systab->runtime = 0; 1384216baa9SSong Gao systab->boottime = 0; 1394216baa9SSong Gao systab->nr_tables = 0; 1404216baa9SSong Gao 1414216baa9SSong Gao p += ROUND_UP(sizeof(struct efi_system_table), 64 * KiB); 1424216baa9SSong Gao 1434216baa9SSong Gao systab->tables = p; 144252b8e68SSong Gao bp_tables_start = p; 145252b8e68SSong Gao 146252b8e68SSong Gao init_efi_boot_memmap(systab, p, start); 147252b8e68SSong Gao p += ROUND_UP(sizeof(struct efi_boot_memmap) + 148252b8e68SSong Gao sizeof(efi_memory_desc_t) * memmap_entries, 64 * KiB); 14906068504SSong Gao init_efi_initrd_table(systab, p, start); 15006068504SSong Gao p += ROUND_UP(sizeof(struct efi_initrd), 64 * KiB); 151*60423851SSong Gao init_efi_fdt_table(systab); 152252b8e68SSong Gao 153252b8e68SSong Gao systab->tables = (struct efi_configuration_table *)(bp_tables_start - start); 1544216baa9SSong Gao } 1554216baa9SSong Gao 15658ee60d2SSong Gao static void init_cmdline(struct loongarch_boot_info *info, void *p, void *start) 15758ee60d2SSong Gao { 15858ee60d2SSong Gao hwaddr cmdline_addr = p - start; 15958ee60d2SSong Gao 16058ee60d2SSong Gao info->a0 = 1; 16158ee60d2SSong Gao info->a1 = cmdline_addr; 16258ee60d2SSong Gao 16358ee60d2SSong Gao memcpy(p, info->kernel_cmdline, COMMAND_LINE_SIZE); 16458ee60d2SSong Gao } 16558ee60d2SSong Gao 166d771ca1cSSong Gao static uint64_t cpu_loongarch_virt_to_phys(void *opaque, uint64_t addr) 167d771ca1cSSong Gao { 168d771ca1cSSong Gao return addr & MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS); 169d771ca1cSSong Gao } 170d771ca1cSSong Gao 171d771ca1cSSong Gao static int64_t load_kernel_info(struct loongarch_boot_info *info) 172d771ca1cSSong Gao { 17306068504SSong Gao uint64_t kernel_entry, kernel_low, kernel_high; 174d771ca1cSSong Gao ssize_t kernel_size; 175d771ca1cSSong Gao 176d771ca1cSSong Gao kernel_size = load_elf(info->kernel_filename, NULL, 177d771ca1cSSong Gao cpu_loongarch_virt_to_phys, NULL, 178d771ca1cSSong Gao &kernel_entry, &kernel_low, 179d771ca1cSSong Gao &kernel_high, NULL, 0, 180d771ca1cSSong Gao EM_LOONGARCH, 1, 0); 181d771ca1cSSong Gao 182d771ca1cSSong Gao if (kernel_size < 0) { 183d771ca1cSSong Gao error_report("could not load kernel '%s': %s", 184d771ca1cSSong Gao info->kernel_filename, 185d771ca1cSSong Gao load_elf_strerror(kernel_size)); 186d771ca1cSSong Gao exit(1); 187d771ca1cSSong Gao } 18802307a67SSong Gao 18902307a67SSong Gao if (info->initrd_filename) { 19002307a67SSong Gao initrd_size = get_image_size(info->initrd_filename); 19102307a67SSong Gao if (initrd_size > 0) { 19202307a67SSong Gao initrd_offset = ROUND_UP(kernel_high + 4 * kernel_size, 64 * KiB); 19302307a67SSong Gao 19402307a67SSong Gao if (initrd_offset + initrd_size > info->ram_size) { 19502307a67SSong Gao error_report("memory too small for initial ram disk '%s'", 19602307a67SSong Gao info->initrd_filename); 19702307a67SSong Gao exit(1); 19802307a67SSong Gao } 19902307a67SSong Gao 20002307a67SSong Gao initrd_size = load_image_targphys(info->initrd_filename, initrd_offset, 20102307a67SSong Gao info->ram_size - initrd_offset); 20202307a67SSong Gao } 20302307a67SSong Gao 20402307a67SSong Gao if (initrd_size == (target_ulong)-1) { 20502307a67SSong Gao error_report("could not load initial ram disk '%s'", 20602307a67SSong Gao info->initrd_filename); 20702307a67SSong Gao exit(1); 20802307a67SSong Gao } 20902307a67SSong Gao } else { 21002307a67SSong Gao initrd_size = 0; 21102307a67SSong Gao } 21202307a67SSong Gao 213d771ca1cSSong Gao return kernel_entry; 214d771ca1cSSong Gao } 215d771ca1cSSong Gao 216d771ca1cSSong Gao static void reset_load_elf(void *opaque) 217d771ca1cSSong Gao { 218d771ca1cSSong Gao LoongArchCPU *cpu = opaque; 219d771ca1cSSong Gao CPULoongArchState *env = &cpu->env; 220d771ca1cSSong Gao 221d771ca1cSSong Gao cpu_reset(CPU(cpu)); 222d771ca1cSSong Gao if (env->load_elf) { 22358ee60d2SSong Gao if (cpu == LOONGARCH_CPU(first_cpu)) { 22458ee60d2SSong Gao env->gpr[4] = env->boot_info->a0; 22558ee60d2SSong Gao env->gpr[5] = env->boot_info->a1; 2264216baa9SSong Gao env->gpr[6] = env->boot_info->a2; 22758ee60d2SSong Gao } 228d771ca1cSSong Gao cpu_set_pc(CPU(cpu), env->elf_address); 229d771ca1cSSong Gao } 230d771ca1cSSong Gao } 231d771ca1cSSong Gao 232d771ca1cSSong Gao static void fw_cfg_add_kernel_info(struct loongarch_boot_info *info, 233d771ca1cSSong Gao FWCfgState *fw_cfg) 234d771ca1cSSong Gao { 235d771ca1cSSong Gao /* 236d771ca1cSSong Gao * Expose the kernel, the command line, and the initrd in fw_cfg. 237d771ca1cSSong Gao * We don't process them here at all, it's all left to the 238d771ca1cSSong Gao * firmware. 239d771ca1cSSong Gao */ 240d771ca1cSSong Gao load_image_to_fw_cfg(fw_cfg, 241d771ca1cSSong Gao FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA, 242d771ca1cSSong Gao info->kernel_filename, 243d771ca1cSSong Gao false); 244d771ca1cSSong Gao 245d771ca1cSSong Gao if (info->initrd_filename) { 246d771ca1cSSong Gao load_image_to_fw_cfg(fw_cfg, 247d771ca1cSSong Gao FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA, 248d771ca1cSSong Gao info->initrd_filename, false); 249d771ca1cSSong Gao } 250d771ca1cSSong Gao 251d771ca1cSSong Gao if (info->kernel_cmdline) { 252d771ca1cSSong Gao fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, 253d771ca1cSSong Gao strlen(info->kernel_cmdline) + 1); 254d771ca1cSSong Gao fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, 255d771ca1cSSong Gao info->kernel_cmdline); 256d771ca1cSSong Gao } 257d771ca1cSSong Gao } 258d771ca1cSSong Gao 259d771ca1cSSong Gao static void loongarch_firmware_boot(LoongArchMachineState *lams, 260d771ca1cSSong Gao struct loongarch_boot_info *info) 261d771ca1cSSong Gao { 262d771ca1cSSong Gao fw_cfg_add_kernel_info(info, lams->fw_cfg); 263d771ca1cSSong Gao } 264d771ca1cSSong Gao 26558ee60d2SSong Gao static void init_boot_rom(struct loongarch_boot_info *info, void *p) 26658ee60d2SSong Gao { 26758ee60d2SSong Gao void *start = p; 26858ee60d2SSong Gao 26958ee60d2SSong Gao init_cmdline(info, p, start); 27058ee60d2SSong Gao p += COMMAND_LINE_SIZE; 2714216baa9SSong Gao 2724216baa9SSong Gao init_systab(info, p, start); 27358ee60d2SSong Gao } 27458ee60d2SSong Gao 275d771ca1cSSong Gao static void loongarch_direct_kernel_boot(struct loongarch_boot_info *info) 276d771ca1cSSong Gao { 27758ee60d2SSong Gao void *p, *bp; 278d771ca1cSSong Gao int64_t kernel_addr = 0; 279d771ca1cSSong Gao LoongArchCPU *lacpu; 280d771ca1cSSong Gao CPUState *cs; 281d771ca1cSSong Gao 282d771ca1cSSong Gao if (info->kernel_filename) { 283d771ca1cSSong Gao kernel_addr = load_kernel_info(info); 284d771ca1cSSong Gao } else { 285d771ca1cSSong Gao if(!qtest_enabled()) { 286d771ca1cSSong Gao error_report("Need kernel filename\n"); 287d771ca1cSSong Gao exit(1); 288d771ca1cSSong Gao } 289d771ca1cSSong Gao } 290d771ca1cSSong Gao 29158ee60d2SSong Gao /* Load cmdline and system tables at [0 - 1 MiB] */ 29258ee60d2SSong Gao p = g_malloc0(1 * MiB); 29358ee60d2SSong Gao bp = p; 29458ee60d2SSong Gao init_boot_rom(info, p); 29558ee60d2SSong Gao rom_add_blob_fixed_as("boot_info", bp, 1 * MiB, 0, &address_space_memory); 29658ee60d2SSong Gao 2977e0510d7SSong Gao /* Load slave boot code at pflash0 . */ 2987e0510d7SSong Gao void *boot_code = g_malloc0(VIRT_FLASH0_SIZE); 2997e0510d7SSong Gao memcpy(boot_code, &slave_boot_code, sizeof(slave_boot_code)); 3007e0510d7SSong Gao rom_add_blob_fixed("boot_code", boot_code, VIRT_FLASH0_SIZE, VIRT_FLASH0_BASE); 3017e0510d7SSong Gao 302d771ca1cSSong Gao CPU_FOREACH(cs) { 303d771ca1cSSong Gao lacpu = LOONGARCH_CPU(cs); 304d771ca1cSSong Gao lacpu->env.load_elf = true; 3057e0510d7SSong Gao if (cs == first_cpu) { 306d771ca1cSSong Gao lacpu->env.elf_address = kernel_addr; 3077e0510d7SSong Gao } else { 3087e0510d7SSong Gao lacpu->env.elf_address = VIRT_FLASH0_BASE; 309d771ca1cSSong Gao } 3107e0510d7SSong Gao lacpu->env.boot_info = info; 3117e0510d7SSong Gao } 3127e0510d7SSong Gao 3137e0510d7SSong Gao g_free(boot_code); 31458ee60d2SSong Gao g_free(bp); 315d771ca1cSSong Gao } 316d771ca1cSSong Gao 317d771ca1cSSong Gao void loongarch_load_kernel(MachineState *ms, struct loongarch_boot_info *info) 318d771ca1cSSong Gao { 319d771ca1cSSong Gao LoongArchMachineState *lams = LOONGARCH_MACHINE(ms); 320d771ca1cSSong Gao int i; 321d771ca1cSSong Gao 322d771ca1cSSong Gao /* register reset function */ 323d771ca1cSSong Gao for (i = 0; i < ms->smp.cpus; i++) { 324d771ca1cSSong Gao qemu_register_reset(reset_load_elf, LOONGARCH_CPU(qemu_get_cpu(i))); 325d771ca1cSSong Gao } 326d771ca1cSSong Gao 327d771ca1cSSong Gao info->kernel_filename = ms->kernel_filename; 328d771ca1cSSong Gao info->kernel_cmdline = ms->kernel_cmdline; 329d771ca1cSSong Gao info->initrd_filename = ms->initrd_filename; 330d771ca1cSSong Gao 331d771ca1cSSong Gao if (lams->bios_loaded) { 332d771ca1cSSong Gao loongarch_firmware_boot(lams, info); 333d771ca1cSSong Gao } else { 334d771ca1cSSong Gao loongarch_direct_kernel_boot(info); 335d771ca1cSSong Gao } 336d771ca1cSSong Gao } 337