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*58ee60d2SSong Gao static void init_cmdline(struct loongarch_boot_info *info, void *p, void *start) 67*58ee60d2SSong Gao { 68*58ee60d2SSong Gao hwaddr cmdline_addr = p - start; 69*58ee60d2SSong Gao 70*58ee60d2SSong Gao info->a0 = 1; 71*58ee60d2SSong Gao info->a1 = cmdline_addr; 72*58ee60d2SSong Gao 73*58ee60d2SSong Gao memcpy(p, info->kernel_cmdline, COMMAND_LINE_SIZE); 74*58ee60d2SSong Gao } 75*58ee60d2SSong Gao 76d771ca1cSSong Gao static uint64_t cpu_loongarch_virt_to_phys(void *opaque, uint64_t addr) 77d771ca1cSSong Gao { 78d771ca1cSSong Gao return addr & MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS); 79d771ca1cSSong Gao } 80d771ca1cSSong Gao 81d771ca1cSSong Gao static int64_t load_kernel_info(struct loongarch_boot_info *info) 82d771ca1cSSong Gao { 8302307a67SSong Gao uint64_t kernel_entry, kernel_low, kernel_high, initrd_size; 8402307a67SSong Gao ram_addr_t initrd_offset; 85d771ca1cSSong Gao ssize_t kernel_size; 86d771ca1cSSong Gao 87d771ca1cSSong Gao kernel_size = load_elf(info->kernel_filename, NULL, 88d771ca1cSSong Gao cpu_loongarch_virt_to_phys, NULL, 89d771ca1cSSong Gao &kernel_entry, &kernel_low, 90d771ca1cSSong Gao &kernel_high, NULL, 0, 91d771ca1cSSong Gao EM_LOONGARCH, 1, 0); 92d771ca1cSSong Gao 93d771ca1cSSong Gao if (kernel_size < 0) { 94d771ca1cSSong Gao error_report("could not load kernel '%s': %s", 95d771ca1cSSong Gao info->kernel_filename, 96d771ca1cSSong Gao load_elf_strerror(kernel_size)); 97d771ca1cSSong Gao exit(1); 98d771ca1cSSong Gao } 9902307a67SSong Gao 10002307a67SSong Gao if (info->initrd_filename) { 10102307a67SSong Gao initrd_size = get_image_size(info->initrd_filename); 10202307a67SSong Gao if (initrd_size > 0) { 10302307a67SSong Gao initrd_offset = ROUND_UP(kernel_high + 4 * kernel_size, 64 * KiB); 10402307a67SSong Gao 10502307a67SSong Gao if (initrd_offset + initrd_size > info->ram_size) { 10602307a67SSong Gao error_report("memory too small for initial ram disk '%s'", 10702307a67SSong Gao info->initrd_filename); 10802307a67SSong Gao exit(1); 10902307a67SSong Gao } 11002307a67SSong Gao 11102307a67SSong Gao initrd_size = load_image_targphys(info->initrd_filename, initrd_offset, 11202307a67SSong Gao info->ram_size - initrd_offset); 11302307a67SSong Gao } 11402307a67SSong Gao 11502307a67SSong Gao if (initrd_size == (target_ulong)-1) { 11602307a67SSong Gao error_report("could not load initial ram disk '%s'", 11702307a67SSong Gao info->initrd_filename); 11802307a67SSong Gao exit(1); 11902307a67SSong Gao } 12002307a67SSong Gao } else { 12102307a67SSong Gao initrd_size = 0; 12202307a67SSong Gao } 12302307a67SSong Gao 124d771ca1cSSong Gao return kernel_entry; 125d771ca1cSSong Gao } 126d771ca1cSSong Gao 127d771ca1cSSong Gao static void reset_load_elf(void *opaque) 128d771ca1cSSong Gao { 129d771ca1cSSong Gao LoongArchCPU *cpu = opaque; 130d771ca1cSSong Gao CPULoongArchState *env = &cpu->env; 131d771ca1cSSong Gao 132d771ca1cSSong Gao cpu_reset(CPU(cpu)); 133d771ca1cSSong Gao if (env->load_elf) { 134*58ee60d2SSong Gao if (cpu == LOONGARCH_CPU(first_cpu)) { 135*58ee60d2SSong Gao env->gpr[4] = env->boot_info->a0; 136*58ee60d2SSong Gao env->gpr[5] = env->boot_info->a1; 137*58ee60d2SSong Gao } 138d771ca1cSSong Gao cpu_set_pc(CPU(cpu), env->elf_address); 139d771ca1cSSong Gao } 140d771ca1cSSong Gao } 141d771ca1cSSong Gao 142d771ca1cSSong Gao static void fw_cfg_add_kernel_info(struct loongarch_boot_info *info, 143d771ca1cSSong Gao FWCfgState *fw_cfg) 144d771ca1cSSong Gao { 145d771ca1cSSong Gao /* 146d771ca1cSSong Gao * Expose the kernel, the command line, and the initrd in fw_cfg. 147d771ca1cSSong Gao * We don't process them here at all, it's all left to the 148d771ca1cSSong Gao * firmware. 149d771ca1cSSong Gao */ 150d771ca1cSSong Gao load_image_to_fw_cfg(fw_cfg, 151d771ca1cSSong Gao FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA, 152d771ca1cSSong Gao info->kernel_filename, 153d771ca1cSSong Gao false); 154d771ca1cSSong Gao 155d771ca1cSSong Gao if (info->initrd_filename) { 156d771ca1cSSong Gao load_image_to_fw_cfg(fw_cfg, 157d771ca1cSSong Gao FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA, 158d771ca1cSSong Gao info->initrd_filename, false); 159d771ca1cSSong Gao } 160d771ca1cSSong Gao 161d771ca1cSSong Gao if (info->kernel_cmdline) { 162d771ca1cSSong Gao fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE, 163d771ca1cSSong Gao strlen(info->kernel_cmdline) + 1); 164d771ca1cSSong Gao fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA, 165d771ca1cSSong Gao info->kernel_cmdline); 166d771ca1cSSong Gao } 167d771ca1cSSong Gao } 168d771ca1cSSong Gao 169d771ca1cSSong Gao static void loongarch_firmware_boot(LoongArchMachineState *lams, 170d771ca1cSSong Gao struct loongarch_boot_info *info) 171d771ca1cSSong Gao { 172d771ca1cSSong Gao fw_cfg_add_kernel_info(info, lams->fw_cfg); 173d771ca1cSSong Gao } 174d771ca1cSSong Gao 175*58ee60d2SSong Gao static void init_boot_rom(struct loongarch_boot_info *info, void *p) 176*58ee60d2SSong Gao { 177*58ee60d2SSong Gao void *start = p; 178*58ee60d2SSong Gao 179*58ee60d2SSong Gao init_cmdline(info, p, start); 180*58ee60d2SSong Gao p += COMMAND_LINE_SIZE; 181*58ee60d2SSong Gao } 182*58ee60d2SSong Gao 183d771ca1cSSong Gao static void loongarch_direct_kernel_boot(struct loongarch_boot_info *info) 184d771ca1cSSong Gao { 185*58ee60d2SSong Gao void *p, *bp; 186d771ca1cSSong Gao int64_t kernel_addr = 0; 187d771ca1cSSong Gao LoongArchCPU *lacpu; 188d771ca1cSSong Gao CPUState *cs; 189d771ca1cSSong Gao 190d771ca1cSSong Gao if (info->kernel_filename) { 191d771ca1cSSong Gao kernel_addr = load_kernel_info(info); 192d771ca1cSSong Gao } else { 193d771ca1cSSong Gao if(!qtest_enabled()) { 194d771ca1cSSong Gao error_report("Need kernel filename\n"); 195d771ca1cSSong Gao exit(1); 196d771ca1cSSong Gao } 197d771ca1cSSong Gao } 198d771ca1cSSong Gao 199*58ee60d2SSong Gao /* Load cmdline and system tables at [0 - 1 MiB] */ 200*58ee60d2SSong Gao p = g_malloc0(1 * MiB); 201*58ee60d2SSong Gao bp = p; 202*58ee60d2SSong Gao init_boot_rom(info, p); 203*58ee60d2SSong Gao rom_add_blob_fixed_as("boot_info", bp, 1 * MiB, 0, &address_space_memory); 204*58ee60d2SSong Gao 2057e0510d7SSong Gao /* Load slave boot code at pflash0 . */ 2067e0510d7SSong Gao void *boot_code = g_malloc0(VIRT_FLASH0_SIZE); 2077e0510d7SSong Gao memcpy(boot_code, &slave_boot_code, sizeof(slave_boot_code)); 2087e0510d7SSong Gao rom_add_blob_fixed("boot_code", boot_code, VIRT_FLASH0_SIZE, VIRT_FLASH0_BASE); 2097e0510d7SSong Gao 210d771ca1cSSong Gao CPU_FOREACH(cs) { 211d771ca1cSSong Gao lacpu = LOONGARCH_CPU(cs); 212d771ca1cSSong Gao lacpu->env.load_elf = true; 2137e0510d7SSong Gao if (cs == first_cpu) { 214d771ca1cSSong Gao lacpu->env.elf_address = kernel_addr; 2157e0510d7SSong Gao } else { 2167e0510d7SSong Gao lacpu->env.elf_address = VIRT_FLASH0_BASE; 217d771ca1cSSong Gao } 2187e0510d7SSong Gao lacpu->env.boot_info = info; 2197e0510d7SSong Gao } 2207e0510d7SSong Gao 2217e0510d7SSong Gao g_free(boot_code); 222*58ee60d2SSong Gao g_free(bp); 223d771ca1cSSong Gao } 224d771ca1cSSong Gao 225d771ca1cSSong Gao void loongarch_load_kernel(MachineState *ms, struct loongarch_boot_info *info) 226d771ca1cSSong Gao { 227d771ca1cSSong Gao LoongArchMachineState *lams = LOONGARCH_MACHINE(ms); 228d771ca1cSSong Gao int i; 229d771ca1cSSong Gao 230d771ca1cSSong Gao /* register reset function */ 231d771ca1cSSong Gao for (i = 0; i < ms->smp.cpus; i++) { 232d771ca1cSSong Gao qemu_register_reset(reset_load_elf, LOONGARCH_CPU(qemu_get_cpu(i))); 233d771ca1cSSong Gao } 234d771ca1cSSong Gao 235d771ca1cSSong Gao info->kernel_filename = ms->kernel_filename; 236d771ca1cSSong Gao info->kernel_cmdline = ms->kernel_cmdline; 237d771ca1cSSong Gao info->initrd_filename = ms->initrd_filename; 238d771ca1cSSong Gao 239d771ca1cSSong Gao if (lams->bios_loaded) { 240d771ca1cSSong Gao loongarch_firmware_boot(lams, info); 241d771ca1cSSong Gao } else { 242d771ca1cSSong Gao loongarch_direct_kernel_boot(info); 243d771ca1cSSong Gao } 244d771ca1cSSong Gao } 245