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 187e0510d7SSong Gao static const unsigned int slave_boot_code[] = { 197e0510d7SSong Gao /* Configure reset ebase. */ 207e0510d7SSong Gao 0x0400302c, /* csrwr $t0, LOONGARCH_CSR_EENTRY */ 217e0510d7SSong Gao 227e0510d7SSong Gao /* Disable interrupt. */ 237e0510d7SSong Gao 0x0380100c, /* ori $t0, $zero,0x4 */ 247e0510d7SSong Gao 0x04000180, /* csrxchg $zero, $t0, LOONGARCH_CSR_CRMD */ 257e0510d7SSong Gao 267e0510d7SSong Gao /* Clear mailbox. */ 277e0510d7SSong Gao 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 287e0510d7SSong Gao 0x038081ad, /* ori $t1, $t1, CORE_BUF_20 */ 297e0510d7SSong Gao 0x06481da0, /* iocsrwr.d $zero, $t1 */ 307e0510d7SSong Gao 317e0510d7SSong Gao /* Enable IPI interrupt. */ 327e0510d7SSong Gao 0x1400002c, /* lu12i.w $t0, 1(0x1) */ 337e0510d7SSong Gao 0x0400118c, /* csrxchg $t0, $t0, LOONGARCH_CSR_ECFG */ 347e0510d7SSong Gao 0x02fffc0c, /* addi.d $t0, $r0,-1(0xfff) */ 357e0510d7SSong Gao 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 367e0510d7SSong Gao 0x038011ad, /* ori $t1, $t1, CORE_EN_OFF */ 377e0510d7SSong Gao 0x064819ac, /* iocsrwr.w $t0, $t1 */ 387e0510d7SSong Gao 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 397e0510d7SSong Gao 0x038081ad, /* ori $t1, $t1, CORE_BUF_20 */ 407e0510d7SSong Gao 417e0510d7SSong Gao /* Wait for wakeup <.L11>: */ 427e0510d7SSong Gao 0x06488000, /* idle 0x0 */ 437e0510d7SSong Gao 0x03400000, /* andi $zero, $zero, 0x0 */ 447e0510d7SSong Gao 0x064809ac, /* iocsrrd.w $t0, $t1 */ 457e0510d7SSong Gao 0x43fff59f, /* beqz $t0, -12(0x7ffff4) # 48 <.L11> */ 467e0510d7SSong Gao 477e0510d7SSong Gao /* Read and clear IPI interrupt. */ 487e0510d7SSong Gao 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 497e0510d7SSong Gao 0x064809ac, /* iocsrrd.w $t0, $t1 */ 507e0510d7SSong Gao 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 517e0510d7SSong Gao 0x038031ad, /* ori $t1, $t1, CORE_CLEAR_OFF */ 527e0510d7SSong Gao 0x064819ac, /* iocsrwr.w $t0, $t1 */ 537e0510d7SSong Gao 547e0510d7SSong Gao /* Disable IPI interrupt. */ 557e0510d7SSong Gao 0x1400002c, /* lu12i.w $t0, 1(0x1) */ 567e0510d7SSong Gao 0x04001180, /* csrxchg $zero, $t0, LOONGARCH_CSR_ECFG */ 577e0510d7SSong Gao 587e0510d7SSong Gao /* Read mail buf and jump to specified entry */ 597e0510d7SSong Gao 0x1400002d, /* lu12i.w $t1, 1(0x1) */ 607e0510d7SSong Gao 0x038081ad, /* ori $t1, $t1, CORE_BUF_20 */ 617e0510d7SSong Gao 0x06480dac, /* iocsrrd.d $t0, $t1 */ 627e0510d7SSong Gao 0x00150181, /* move $ra, $t0 */ 637e0510d7SSong Gao 0x4c000020, /* jirl $zero, $ra,0 */ 647e0510d7SSong Gao }; 657e0510d7SSong Gao 66*4216baa9SSong Gao static void init_systab(struct loongarch_boot_info *info, void *p, void *start) 67*4216baa9SSong Gao { 68*4216baa9SSong Gao struct efi_system_table *systab = p; 69*4216baa9SSong Gao 70*4216baa9SSong Gao info->a2 = p - start; 71*4216baa9SSong Gao 72*4216baa9SSong Gao systab->hdr.signature = EFI_SYSTEM_TABLE_SIGNATURE; 73*4216baa9SSong Gao systab->hdr.revision = EFI_SPECIFICATION_VERSION; 74*4216baa9SSong Gao systab->hdr.revision = sizeof(struct efi_system_table), 75*4216baa9SSong Gao systab->fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8; 76*4216baa9SSong Gao systab->runtime = 0; 77*4216baa9SSong Gao systab->boottime = 0; 78*4216baa9SSong Gao systab->nr_tables = 0; 79*4216baa9SSong Gao 80*4216baa9SSong Gao p += ROUND_UP(sizeof(struct efi_system_table), 64 * KiB); 81*4216baa9SSong Gao 82*4216baa9SSong Gao systab->tables = p; 83*4216baa9SSong Gao } 84*4216baa9SSong Gao 8558ee60d2SSong Gao static void init_cmdline(struct loongarch_boot_info *info, void *p, void *start) 8658ee60d2SSong Gao { 8758ee60d2SSong Gao hwaddr cmdline_addr = p - start; 8858ee60d2SSong Gao 8958ee60d2SSong Gao info->a0 = 1; 9058ee60d2SSong Gao info->a1 = cmdline_addr; 9158ee60d2SSong Gao 9258ee60d2SSong Gao memcpy(p, info->kernel_cmdline, COMMAND_LINE_SIZE); 9358ee60d2SSong Gao } 9458ee60d2SSong Gao 95d771ca1cSSong Gao static uint64_t cpu_loongarch_virt_to_phys(void *opaque, uint64_t addr) 96d771ca1cSSong Gao { 97d771ca1cSSong Gao return addr & MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS); 98d771ca1cSSong Gao } 99d771ca1cSSong Gao 100d771ca1cSSong Gao static int64_t load_kernel_info(struct loongarch_boot_info *info) 101d771ca1cSSong Gao { 10202307a67SSong Gao uint64_t kernel_entry, kernel_low, kernel_high, initrd_size; 10302307a67SSong Gao ram_addr_t initrd_offset; 104d771ca1cSSong Gao ssize_t kernel_size; 105d771ca1cSSong Gao 106d771ca1cSSong Gao kernel_size = load_elf(info->kernel_filename, NULL, 107d771ca1cSSong Gao cpu_loongarch_virt_to_phys, NULL, 108d771ca1cSSong Gao &kernel_entry, &kernel_low, 109d771ca1cSSong Gao &kernel_high, NULL, 0, 110d771ca1cSSong Gao EM_LOONGARCH, 1, 0); 111d771ca1cSSong Gao 112d771ca1cSSong Gao if (kernel_size < 0) { 113d771ca1cSSong Gao error_report("could not load kernel '%s': %s", 114d771ca1cSSong Gao info->kernel_filename, 115d771ca1cSSong Gao load_elf_strerror(kernel_size)); 116d771ca1cSSong Gao exit(1); 117d771ca1cSSong Gao } 11802307a67SSong Gao 11902307a67SSong Gao if (info->initrd_filename) { 12002307a67SSong Gao initrd_size = get_image_size(info->initrd_filename); 12102307a67SSong Gao if (initrd_size > 0) { 12202307a67SSong Gao initrd_offset = ROUND_UP(kernel_high + 4 * kernel_size, 64 * KiB); 12302307a67SSong Gao 12402307a67SSong Gao if (initrd_offset + initrd_size > info->ram_size) { 12502307a67SSong Gao error_report("memory too small for initial ram disk '%s'", 12602307a67SSong Gao info->initrd_filename); 12702307a67SSong Gao exit(1); 12802307a67SSong Gao } 12902307a67SSong Gao 13002307a67SSong Gao initrd_size = load_image_targphys(info->initrd_filename, initrd_offset, 13102307a67SSong Gao info->ram_size - initrd_offset); 13202307a67SSong Gao } 13302307a67SSong Gao 13402307a67SSong Gao if (initrd_size == (target_ulong)-1) { 13502307a67SSong Gao error_report("could not load initial ram disk '%s'", 13602307a67SSong Gao info->initrd_filename); 13702307a67SSong Gao exit(1); 13802307a67SSong Gao } 13902307a67SSong Gao } else { 14002307a67SSong Gao initrd_size = 0; 14102307a67SSong Gao } 14202307a67SSong Gao 143d771ca1cSSong Gao return kernel_entry; 144d771ca1cSSong Gao } 145d771ca1cSSong Gao 146d771ca1cSSong Gao static void reset_load_elf(void *opaque) 147d771ca1cSSong Gao { 148d771ca1cSSong Gao LoongArchCPU *cpu = opaque; 149d771ca1cSSong Gao CPULoongArchState *env = &cpu->env; 150d771ca1cSSong Gao 151d771ca1cSSong Gao cpu_reset(CPU(cpu)); 152d771ca1cSSong Gao if (env->load_elf) { 15358ee60d2SSong Gao if (cpu == LOONGARCH_CPU(first_cpu)) { 15458ee60d2SSong Gao env->gpr[4] = env->boot_info->a0; 15558ee60d2SSong Gao env->gpr[5] = env->boot_info->a1; 156*4216baa9SSong Gao env->gpr[6] = env->boot_info->a2; 15758ee60d2SSong Gao } 158d771ca1cSSong Gao cpu_set_pc(CPU(cpu), env->elf_address); 159d771ca1cSSong Gao } 160d771ca1cSSong Gao } 161d771ca1cSSong Gao 162d771ca1cSSong Gao static void fw_cfg_add_kernel_info(struct loongarch_boot_info *info, 163d771ca1cSSong Gao FWCfgState *fw_cfg) 164d771ca1cSSong Gao { 165d771ca1cSSong Gao /* 166d771ca1cSSong Gao * Expose the kernel, the command line, and the initrd in fw_cfg. 167d771ca1cSSong Gao * We don't process them here at all, it's all left to the 168d771ca1cSSong Gao * firmware. 169d771ca1cSSong Gao */ 170d771ca1cSSong Gao load_image_to_fw_cfg(fw_cfg, 171d771ca1cSSong Gao FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA, 172d771ca1cSSong Gao info->kernel_filename, 173d771ca1cSSong Gao false); 174d771ca1cSSong Gao 175d771ca1cSSong Gao if (info->initrd_filename) { 176d771ca1cSSong Gao load_image_to_fw_cfg(fw_cfg, 177d771ca1cSSong Gao FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA, 178d771ca1cSSong Gao info->initrd_filename, false); 179d771ca1cSSong Gao } 180d771ca1cSSong Gao 181d771ca1cSSong Gao if (info->kernel_cmdline) { 182d771ca1cSSong Gao fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, 183d771ca1cSSong Gao strlen(info->kernel_cmdline) + 1); 184d771ca1cSSong Gao fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, 185d771ca1cSSong Gao info->kernel_cmdline); 186d771ca1cSSong Gao } 187d771ca1cSSong Gao } 188d771ca1cSSong Gao 189d771ca1cSSong Gao static void loongarch_firmware_boot(LoongArchMachineState *lams, 190d771ca1cSSong Gao struct loongarch_boot_info *info) 191d771ca1cSSong Gao { 192d771ca1cSSong Gao fw_cfg_add_kernel_info(info, lams->fw_cfg); 193d771ca1cSSong Gao } 194d771ca1cSSong Gao 19558ee60d2SSong Gao static void init_boot_rom(struct loongarch_boot_info *info, void *p) 19658ee60d2SSong Gao { 19758ee60d2SSong Gao void *start = p; 19858ee60d2SSong Gao 19958ee60d2SSong Gao init_cmdline(info, p, start); 20058ee60d2SSong Gao p += COMMAND_LINE_SIZE; 201*4216baa9SSong Gao 202*4216baa9SSong Gao init_systab(info, p, start); 20358ee60d2SSong Gao } 20458ee60d2SSong Gao 205d771ca1cSSong Gao static void loongarch_direct_kernel_boot(struct loongarch_boot_info *info) 206d771ca1cSSong Gao { 20758ee60d2SSong Gao void *p, *bp; 208d771ca1cSSong Gao int64_t kernel_addr = 0; 209d771ca1cSSong Gao LoongArchCPU *lacpu; 210d771ca1cSSong Gao CPUState *cs; 211d771ca1cSSong Gao 212d771ca1cSSong Gao if (info->kernel_filename) { 213d771ca1cSSong Gao kernel_addr = load_kernel_info(info); 214d771ca1cSSong Gao } else { 215d771ca1cSSong Gao if(!qtest_enabled()) { 216d771ca1cSSong Gao error_report("Need kernel filename\n"); 217d771ca1cSSong Gao exit(1); 218d771ca1cSSong Gao } 219d771ca1cSSong Gao } 220d771ca1cSSong Gao 22158ee60d2SSong Gao /* Load cmdline and system tables at [0 - 1 MiB] */ 22258ee60d2SSong Gao p = g_malloc0(1 * MiB); 22358ee60d2SSong Gao bp = p; 22458ee60d2SSong Gao init_boot_rom(info, p); 22558ee60d2SSong Gao rom_add_blob_fixed_as("boot_info", bp, 1 * MiB, 0, &address_space_memory); 22658ee60d2SSong Gao 2277e0510d7SSong Gao /* Load slave boot code at pflash0 . */ 2287e0510d7SSong Gao void *boot_code = g_malloc0(VIRT_FLASH0_SIZE); 2297e0510d7SSong Gao memcpy(boot_code, &slave_boot_code, sizeof(slave_boot_code)); 2307e0510d7SSong Gao rom_add_blob_fixed("boot_code", boot_code, VIRT_FLASH0_SIZE, VIRT_FLASH0_BASE); 2317e0510d7SSong Gao 232d771ca1cSSong Gao CPU_FOREACH(cs) { 233d771ca1cSSong Gao lacpu = LOONGARCH_CPU(cs); 234d771ca1cSSong Gao lacpu->env.load_elf = true; 2357e0510d7SSong Gao if (cs == first_cpu) { 236d771ca1cSSong Gao lacpu->env.elf_address = kernel_addr; 2377e0510d7SSong Gao } else { 2387e0510d7SSong Gao lacpu->env.elf_address = VIRT_FLASH0_BASE; 239d771ca1cSSong Gao } 2407e0510d7SSong Gao lacpu->env.boot_info = info; 2417e0510d7SSong Gao } 2427e0510d7SSong Gao 2437e0510d7SSong Gao g_free(boot_code); 24458ee60d2SSong Gao g_free(bp); 245d771ca1cSSong Gao } 246d771ca1cSSong Gao 247d771ca1cSSong Gao void loongarch_load_kernel(MachineState *ms, struct loongarch_boot_info *info) 248d771ca1cSSong Gao { 249d771ca1cSSong Gao LoongArchMachineState *lams = LOONGARCH_MACHINE(ms); 250d771ca1cSSong Gao int i; 251d771ca1cSSong Gao 252d771ca1cSSong Gao /* register reset function */ 253d771ca1cSSong Gao for (i = 0; i < ms->smp.cpus; i++) { 254d771ca1cSSong Gao qemu_register_reset(reset_load_elf, LOONGARCH_CPU(qemu_get_cpu(i))); 255d771ca1cSSong Gao } 256d771ca1cSSong Gao 257d771ca1cSSong Gao info->kernel_filename = ms->kernel_filename; 258d771ca1cSSong Gao info->kernel_cmdline = ms->kernel_cmdline; 259d771ca1cSSong Gao info->initrd_filename = ms->initrd_filename; 260d771ca1cSSong Gao 261d771ca1cSSong Gao if (lams->bios_loaded) { 262d771ca1cSSong Gao loongarch_firmware_boot(lams, info); 263d771ca1cSSong Gao } else { 264d771ca1cSSong Gao loongarch_direct_kernel_boot(info); 265d771ca1cSSong Gao } 266d771ca1cSSong Gao } 267