xref: /openbmc/qemu/hw/loongarch/boot.c (revision 252b8e68)
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*252b8e68SSong Gao static inline void *guidcpy(void *dst, const void *src)
67*252b8e68SSong Gao {
68*252b8e68SSong Gao     return memcpy(dst, src, sizeof(efi_guid_t));
69*252b8e68SSong Gao }
70*252b8e68SSong Gao 
71*252b8e68SSong Gao static void init_efi_boot_memmap(struct efi_system_table *systab,
72*252b8e68SSong Gao                                  void *p, void *start)
73*252b8e68SSong Gao {
74*252b8e68SSong Gao     unsigned i;
75*252b8e68SSong Gao     struct efi_boot_memmap *boot_memmap = p;
76*252b8e68SSong Gao     efi_guid_t tbl_guid = LINUX_EFI_BOOT_MEMMAP_GUID;
77*252b8e68SSong Gao 
78*252b8e68SSong Gao     /* efi_configuration_table 1 */
79*252b8e68SSong Gao     guidcpy(&systab->tables[0].guid, &tbl_guid);
80*252b8e68SSong Gao     systab->tables[0].table = (struct efi_configuration_table *)(p - start);
81*252b8e68SSong Gao     systab->nr_tables = 1;
82*252b8e68SSong Gao 
83*252b8e68SSong Gao     boot_memmap->desc_size = sizeof(efi_memory_desc_t);
84*252b8e68SSong Gao     boot_memmap->desc_ver = 1;
85*252b8e68SSong Gao     boot_memmap->map_size = 0;
86*252b8e68SSong Gao 
87*252b8e68SSong Gao     efi_memory_desc_t *map = p + sizeof(struct efi_boot_memmap);
88*252b8e68SSong Gao     for (i = 0; i < memmap_entries; i++) {
89*252b8e68SSong Gao         map = (void *)boot_memmap + sizeof(*map);
90*252b8e68SSong Gao         map[i].type = memmap_table[i].type;
91*252b8e68SSong Gao         map[i].phys_addr = ROUND_UP(memmap_table[i].address, 64 * KiB);
92*252b8e68SSong Gao         map[i].num_pages = ROUND_DOWN(memmap_table[i].address +
93*252b8e68SSong Gao                         memmap_table[i].length - map[i].phys_addr, 64 * KiB);
94*252b8e68SSong Gao         p += sizeof(efi_memory_desc_t);
95*252b8e68SSong Gao     }
96*252b8e68SSong Gao }
97*252b8e68SSong Gao 
984216baa9SSong Gao static void init_systab(struct loongarch_boot_info *info, void *p, void *start)
994216baa9SSong Gao {
100*252b8e68SSong Gao     void *bp_tables_start;
1014216baa9SSong Gao     struct efi_system_table *systab = p;
1024216baa9SSong Gao 
1034216baa9SSong Gao     info->a2 = p - start;
1044216baa9SSong Gao 
1054216baa9SSong Gao     systab->hdr.signature = EFI_SYSTEM_TABLE_SIGNATURE;
1064216baa9SSong Gao     systab->hdr.revision = EFI_SPECIFICATION_VERSION;
1074216baa9SSong Gao     systab->hdr.revision = sizeof(struct efi_system_table),
1084216baa9SSong Gao     systab->fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8;
1094216baa9SSong Gao     systab->runtime = 0;
1104216baa9SSong Gao     systab->boottime = 0;
1114216baa9SSong Gao     systab->nr_tables = 0;
1124216baa9SSong Gao 
1134216baa9SSong Gao     p += ROUND_UP(sizeof(struct efi_system_table), 64 * KiB);
1144216baa9SSong Gao 
1154216baa9SSong Gao     systab->tables = p;
116*252b8e68SSong Gao     bp_tables_start = p;
117*252b8e68SSong Gao 
118*252b8e68SSong Gao     init_efi_boot_memmap(systab, p, start);
119*252b8e68SSong Gao     p += ROUND_UP(sizeof(struct efi_boot_memmap) +
120*252b8e68SSong Gao                   sizeof(efi_memory_desc_t) * memmap_entries, 64 * KiB);
121*252b8e68SSong Gao 
122*252b8e68SSong Gao     systab->tables = (struct efi_configuration_table *)(bp_tables_start - start);
1234216baa9SSong Gao }
1244216baa9SSong Gao 
12558ee60d2SSong Gao static void init_cmdline(struct loongarch_boot_info *info, void *p, void *start)
12658ee60d2SSong Gao {
12758ee60d2SSong Gao     hwaddr cmdline_addr = p - start;
12858ee60d2SSong Gao 
12958ee60d2SSong Gao     info->a0 = 1;
13058ee60d2SSong Gao     info->a1 = cmdline_addr;
13158ee60d2SSong Gao 
13258ee60d2SSong Gao     memcpy(p, info->kernel_cmdline, COMMAND_LINE_SIZE);
13358ee60d2SSong Gao }
13458ee60d2SSong Gao 
135d771ca1cSSong Gao static uint64_t cpu_loongarch_virt_to_phys(void *opaque, uint64_t addr)
136d771ca1cSSong Gao {
137d771ca1cSSong Gao     return addr & MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
138d771ca1cSSong Gao }
139d771ca1cSSong Gao 
140d771ca1cSSong Gao static int64_t load_kernel_info(struct loongarch_boot_info *info)
141d771ca1cSSong Gao {
14202307a67SSong Gao     uint64_t kernel_entry, kernel_low, kernel_high, initrd_size;
14302307a67SSong Gao     ram_addr_t initrd_offset;
144d771ca1cSSong Gao     ssize_t kernel_size;
145d771ca1cSSong Gao 
146d771ca1cSSong Gao     kernel_size = load_elf(info->kernel_filename, NULL,
147d771ca1cSSong Gao                            cpu_loongarch_virt_to_phys, NULL,
148d771ca1cSSong Gao                            &kernel_entry, &kernel_low,
149d771ca1cSSong Gao                            &kernel_high, NULL, 0,
150d771ca1cSSong Gao                            EM_LOONGARCH, 1, 0);
151d771ca1cSSong Gao 
152d771ca1cSSong Gao     if (kernel_size < 0) {
153d771ca1cSSong Gao         error_report("could not load kernel '%s': %s",
154d771ca1cSSong Gao                      info->kernel_filename,
155d771ca1cSSong Gao                      load_elf_strerror(kernel_size));
156d771ca1cSSong Gao         exit(1);
157d771ca1cSSong Gao     }
15802307a67SSong Gao 
15902307a67SSong Gao     if (info->initrd_filename) {
16002307a67SSong Gao         initrd_size = get_image_size(info->initrd_filename);
16102307a67SSong Gao         if (initrd_size > 0) {
16202307a67SSong Gao             initrd_offset = ROUND_UP(kernel_high + 4 * kernel_size, 64 * KiB);
16302307a67SSong Gao 
16402307a67SSong Gao             if (initrd_offset + initrd_size > info->ram_size) {
16502307a67SSong Gao                 error_report("memory too small for initial ram disk '%s'",
16602307a67SSong Gao                              info->initrd_filename);
16702307a67SSong Gao                 exit(1);
16802307a67SSong Gao             }
16902307a67SSong Gao 
17002307a67SSong Gao             initrd_size = load_image_targphys(info->initrd_filename, initrd_offset,
17102307a67SSong Gao                                               info->ram_size - initrd_offset);
17202307a67SSong Gao         }
17302307a67SSong Gao 
17402307a67SSong Gao         if (initrd_size == (target_ulong)-1) {
17502307a67SSong Gao             error_report("could not load initial ram disk '%s'",
17602307a67SSong Gao                          info->initrd_filename);
17702307a67SSong Gao             exit(1);
17802307a67SSong Gao         }
17902307a67SSong Gao     } else {
18002307a67SSong Gao         initrd_size = 0;
18102307a67SSong Gao     }
18202307a67SSong Gao 
183d771ca1cSSong Gao     return kernel_entry;
184d771ca1cSSong Gao }
185d771ca1cSSong Gao 
186d771ca1cSSong Gao static void reset_load_elf(void *opaque)
187d771ca1cSSong Gao {
188d771ca1cSSong Gao     LoongArchCPU *cpu = opaque;
189d771ca1cSSong Gao     CPULoongArchState *env = &cpu->env;
190d771ca1cSSong Gao 
191d771ca1cSSong Gao     cpu_reset(CPU(cpu));
192d771ca1cSSong Gao     if (env->load_elf) {
19358ee60d2SSong Gao 	if (cpu == LOONGARCH_CPU(first_cpu)) {
19458ee60d2SSong Gao             env->gpr[4] = env->boot_info->a0;
19558ee60d2SSong Gao             env->gpr[5] = env->boot_info->a1;
1964216baa9SSong Gao             env->gpr[6] = env->boot_info->a2;
19758ee60d2SSong Gao         }
198d771ca1cSSong Gao         cpu_set_pc(CPU(cpu), env->elf_address);
199d771ca1cSSong Gao     }
200d771ca1cSSong Gao }
201d771ca1cSSong Gao 
202d771ca1cSSong Gao static void fw_cfg_add_kernel_info(struct loongarch_boot_info *info,
203d771ca1cSSong Gao                                    FWCfgState *fw_cfg)
204d771ca1cSSong Gao {
205d771ca1cSSong Gao     /*
206d771ca1cSSong Gao      * Expose the kernel, the command line, and the initrd in fw_cfg.
207d771ca1cSSong Gao      * We don't process them here at all, it's all left to the
208d771ca1cSSong Gao      * firmware.
209d771ca1cSSong Gao      */
210d771ca1cSSong Gao     load_image_to_fw_cfg(fw_cfg,
211d771ca1cSSong Gao                          FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
212d771ca1cSSong Gao                          info->kernel_filename,
213d771ca1cSSong Gao                          false);
214d771ca1cSSong Gao 
215d771ca1cSSong Gao     if (info->initrd_filename) {
216d771ca1cSSong Gao         load_image_to_fw_cfg(fw_cfg,
217d771ca1cSSong Gao                              FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
218d771ca1cSSong Gao                              info->initrd_filename, false);
219d771ca1cSSong Gao     }
220d771ca1cSSong Gao 
221d771ca1cSSong Gao     if (info->kernel_cmdline) {
222d771ca1cSSong Gao         fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
223d771ca1cSSong Gao                        strlen(info->kernel_cmdline) + 1);
224d771ca1cSSong Gao         fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
225d771ca1cSSong Gao                           info->kernel_cmdline);
226d771ca1cSSong Gao     }
227d771ca1cSSong Gao }
228d771ca1cSSong Gao 
229d771ca1cSSong Gao static void loongarch_firmware_boot(LoongArchMachineState *lams,
230d771ca1cSSong Gao                                     struct loongarch_boot_info *info)
231d771ca1cSSong Gao {
232d771ca1cSSong Gao     fw_cfg_add_kernel_info(info, lams->fw_cfg);
233d771ca1cSSong Gao }
234d771ca1cSSong Gao 
23558ee60d2SSong Gao static void init_boot_rom(struct loongarch_boot_info *info, void *p)
23658ee60d2SSong Gao {
23758ee60d2SSong Gao     void *start = p;
23858ee60d2SSong Gao 
23958ee60d2SSong Gao     init_cmdline(info, p, start);
24058ee60d2SSong Gao     p += COMMAND_LINE_SIZE;
2414216baa9SSong Gao 
2424216baa9SSong Gao     init_systab(info, p, start);
24358ee60d2SSong Gao }
24458ee60d2SSong Gao 
245d771ca1cSSong Gao static void loongarch_direct_kernel_boot(struct loongarch_boot_info *info)
246d771ca1cSSong Gao {
24758ee60d2SSong Gao     void *p, *bp;
248d771ca1cSSong Gao     int64_t kernel_addr = 0;
249d771ca1cSSong Gao     LoongArchCPU *lacpu;
250d771ca1cSSong Gao     CPUState *cs;
251d771ca1cSSong Gao 
252d771ca1cSSong Gao     if (info->kernel_filename) {
253d771ca1cSSong Gao         kernel_addr = load_kernel_info(info);
254d771ca1cSSong Gao     } else {
255d771ca1cSSong Gao         if(!qtest_enabled()) {
256d771ca1cSSong Gao             error_report("Need kernel filename\n");
257d771ca1cSSong Gao             exit(1);
258d771ca1cSSong Gao         }
259d771ca1cSSong Gao     }
260d771ca1cSSong Gao 
26158ee60d2SSong Gao     /* Load cmdline and system tables at [0 - 1 MiB] */
26258ee60d2SSong Gao     p = g_malloc0(1 * MiB);
26358ee60d2SSong Gao     bp = p;
26458ee60d2SSong Gao     init_boot_rom(info, p);
26558ee60d2SSong Gao     rom_add_blob_fixed_as("boot_info", bp, 1 * MiB, 0, &address_space_memory);
26658ee60d2SSong Gao 
2677e0510d7SSong Gao     /* Load slave boot code at pflash0 . */
2687e0510d7SSong Gao     void *boot_code = g_malloc0(VIRT_FLASH0_SIZE);
2697e0510d7SSong Gao     memcpy(boot_code, &slave_boot_code, sizeof(slave_boot_code));
2707e0510d7SSong Gao     rom_add_blob_fixed("boot_code", boot_code, VIRT_FLASH0_SIZE, VIRT_FLASH0_BASE);
2717e0510d7SSong Gao 
272d771ca1cSSong Gao     CPU_FOREACH(cs) {
273d771ca1cSSong Gao         lacpu = LOONGARCH_CPU(cs);
274d771ca1cSSong Gao         lacpu->env.load_elf = true;
2757e0510d7SSong Gao         if (cs == first_cpu) {
276d771ca1cSSong Gao             lacpu->env.elf_address = kernel_addr;
2777e0510d7SSong Gao         } else {
2787e0510d7SSong Gao             lacpu->env.elf_address = VIRT_FLASH0_BASE;
279d771ca1cSSong Gao         }
2807e0510d7SSong Gao         lacpu->env.boot_info = info;
2817e0510d7SSong Gao     }
2827e0510d7SSong Gao 
2837e0510d7SSong Gao     g_free(boot_code);
28458ee60d2SSong Gao     g_free(bp);
285d771ca1cSSong Gao }
286d771ca1cSSong Gao 
287d771ca1cSSong Gao void loongarch_load_kernel(MachineState *ms, struct loongarch_boot_info *info)
288d771ca1cSSong Gao {
289d771ca1cSSong Gao     LoongArchMachineState *lams = LOONGARCH_MACHINE(ms);
290d771ca1cSSong Gao     int i;
291d771ca1cSSong Gao 
292d771ca1cSSong Gao     /* register reset function */
293d771ca1cSSong Gao     for (i = 0; i < ms->smp.cpus; i++) {
294d771ca1cSSong Gao         qemu_register_reset(reset_load_elf, LOONGARCH_CPU(qemu_get_cpu(i)));
295d771ca1cSSong Gao     }
296d771ca1cSSong Gao 
297d771ca1cSSong Gao     info->kernel_filename = ms->kernel_filename;
298d771ca1cSSong Gao     info->kernel_cmdline = ms->kernel_cmdline;
299d771ca1cSSong Gao     info->initrd_filename = ms->initrd_filename;
300d771ca1cSSong Gao 
301d771ca1cSSong Gao     if (lams->bios_loaded) {
302d771ca1cSSong Gao         loongarch_firmware_boot(lams, info);
303d771ca1cSSong Gao     } else {
304d771ca1cSSong Gao         loongarch_direct_kernel_boot(info);
305d771ca1cSSong Gao     }
306d771ca1cSSong Gao }
307