xref: /openbmc/qemu/hw/loongarch/boot.c (revision d804ad98)
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 
1872674db0SPaolo Bonzini struct memmap_entry *memmap_table;
1972674db0SPaolo Bonzini unsigned memmap_entries;
2072674db0SPaolo Bonzini 
2106068504SSong Gao ram_addr_t initrd_offset;
2206068504SSong Gao uint64_t initrd_size;
2306068504SSong Gao 
247e0510d7SSong Gao static const unsigned int slave_boot_code[] = {
257e0510d7SSong Gao                   /* Configure reset ebase.                    */
267e0510d7SSong Gao     0x0400302c,   /* csrwr      $t0, LOONGARCH_CSR_EENTRY      */
277e0510d7SSong Gao 
287e0510d7SSong Gao                   /* Disable interrupt.                        */
297e0510d7SSong Gao     0x0380100c,   /* ori        $t0, $zero,0x4                 */
307e0510d7SSong Gao     0x04000180,   /* csrxchg    $zero, $t0, LOONGARCH_CSR_CRMD */
317e0510d7SSong Gao 
327e0510d7SSong Gao                   /* Clear mailbox.                            */
337e0510d7SSong Gao     0x1400002d,   /* lu12i.w    $t1, 1(0x1)                    */
347e0510d7SSong Gao     0x038081ad,   /* ori        $t1, $t1, CORE_BUF_20  */
357e0510d7SSong Gao     0x06481da0,   /* iocsrwr.d  $zero, $t1                     */
367e0510d7SSong Gao 
377e0510d7SSong Gao                   /* Enable IPI interrupt.                     */
387e0510d7SSong Gao     0x1400002c,   /* lu12i.w    $t0, 1(0x1)                    */
397e0510d7SSong Gao     0x0400118c,   /* csrxchg    $t0, $t0, LOONGARCH_CSR_ECFG   */
407e0510d7SSong Gao     0x02fffc0c,   /* addi.d     $t0, $r0,-1(0xfff)             */
417e0510d7SSong Gao     0x1400002d,   /* lu12i.w    $t1, 1(0x1)                    */
427e0510d7SSong Gao     0x038011ad,   /* ori        $t1, $t1, CORE_EN_OFF          */
437e0510d7SSong Gao     0x064819ac,   /* iocsrwr.w  $t0, $t1                       */
447e0510d7SSong Gao     0x1400002d,   /* lu12i.w    $t1, 1(0x1)                    */
457e0510d7SSong Gao     0x038081ad,   /* ori        $t1, $t1, CORE_BUF_20          */
467e0510d7SSong Gao 
477e0510d7SSong Gao                   /* Wait for wakeup  <.L11>:                  */
487e0510d7SSong Gao     0x06488000,   /* idle       0x0                            */
497e0510d7SSong Gao     0x03400000,   /* andi       $zero, $zero, 0x0              */
507e0510d7SSong Gao     0x064809ac,   /* iocsrrd.w  $t0, $t1                       */
517e0510d7SSong Gao     0x43fff59f,   /* beqz       $t0, -12(0x7ffff4) # 48 <.L11> */
527e0510d7SSong Gao 
537e0510d7SSong Gao                   /* Read and clear IPI interrupt.             */
547e0510d7SSong Gao     0x1400002d,   /* lu12i.w    $t1, 1(0x1)                    */
557e0510d7SSong Gao     0x064809ac,   /* iocsrrd.w  $t0, $t1                       */
567e0510d7SSong Gao     0x1400002d,   /* lu12i.w    $t1, 1(0x1)                    */
577e0510d7SSong Gao     0x038031ad,   /* ori        $t1, $t1, CORE_CLEAR_OFF       */
587e0510d7SSong Gao     0x064819ac,   /* iocsrwr.w  $t0, $t1                       */
597e0510d7SSong Gao 
607e0510d7SSong Gao                   /* Disable  IPI interrupt.                   */
617e0510d7SSong Gao     0x1400002c,   /* lu12i.w    $t0, 1(0x1)                    */
627e0510d7SSong Gao     0x04001180,   /* csrxchg    $zero, $t0, LOONGARCH_CSR_ECFG */
637e0510d7SSong Gao 
647e0510d7SSong Gao                   /* Read mail buf and jump to specified entry */
657e0510d7SSong Gao     0x1400002d,   /* lu12i.w    $t1, 1(0x1)                    */
667e0510d7SSong Gao     0x038081ad,   /* ori        $t1, $t1, CORE_BUF_20          */
677e0510d7SSong Gao     0x06480dac,   /* iocsrrd.d  $t0, $t1                       */
687e0510d7SSong Gao     0x00150181,   /* move       $ra, $t0                       */
697e0510d7SSong Gao     0x4c000020,   /* jirl       $zero, $ra,0                   */
707e0510d7SSong Gao };
717e0510d7SSong Gao 
72252b8e68SSong Gao static inline void *guidcpy(void *dst, const void *src)
73252b8e68SSong Gao {
74252b8e68SSong Gao     return memcpy(dst, src, sizeof(efi_guid_t));
75252b8e68SSong Gao }
76252b8e68SSong Gao 
77252b8e68SSong Gao static void init_efi_boot_memmap(struct efi_system_table *systab,
78252b8e68SSong Gao                                  void *p, void *start)
79252b8e68SSong Gao {
80252b8e68SSong Gao     unsigned i;
81252b8e68SSong Gao     struct efi_boot_memmap *boot_memmap = p;
82252b8e68SSong Gao     efi_guid_t tbl_guid = LINUX_EFI_BOOT_MEMMAP_GUID;
83252b8e68SSong Gao 
84252b8e68SSong Gao     /* efi_configuration_table 1 */
85252b8e68SSong Gao     guidcpy(&systab->tables[0].guid, &tbl_guid);
86252b8e68SSong Gao     systab->tables[0].table = (struct efi_configuration_table *)(p - start);
87252b8e68SSong Gao     systab->nr_tables = 1;
88252b8e68SSong Gao 
89252b8e68SSong Gao     boot_memmap->desc_size = sizeof(efi_memory_desc_t);
90252b8e68SSong Gao     boot_memmap->desc_ver = 1;
91252b8e68SSong Gao     boot_memmap->map_size = 0;
92252b8e68SSong Gao 
93252b8e68SSong Gao     efi_memory_desc_t *map = p + sizeof(struct efi_boot_memmap);
94252b8e68SSong Gao     for (i = 0; i < memmap_entries; i++) {
95252b8e68SSong Gao         map = (void *)boot_memmap + sizeof(*map);
96252b8e68SSong Gao         map[i].type = memmap_table[i].type;
97252b8e68SSong Gao         map[i].phys_addr = ROUND_UP(memmap_table[i].address, 64 * KiB);
98252b8e68SSong Gao         map[i].num_pages = ROUND_DOWN(memmap_table[i].address +
99252b8e68SSong Gao                         memmap_table[i].length - map[i].phys_addr, 64 * KiB);
100252b8e68SSong Gao         p += sizeof(efi_memory_desc_t);
101252b8e68SSong Gao     }
102252b8e68SSong Gao }
103252b8e68SSong Gao 
10406068504SSong Gao static void init_efi_initrd_table(struct efi_system_table *systab,
10506068504SSong Gao                                   void *p, void *start)
10606068504SSong Gao {
10706068504SSong Gao     efi_guid_t tbl_guid = LINUX_EFI_INITRD_MEDIA_GUID;
10806068504SSong Gao     struct efi_initrd *initrd_table  = p;
10906068504SSong Gao 
11006068504SSong Gao     /* efi_configuration_table 2 */
11106068504SSong Gao     guidcpy(&systab->tables[1].guid, &tbl_guid);
11206068504SSong Gao     systab->tables[1].table = (struct efi_configuration_table *)(p - start);
11306068504SSong Gao     systab->nr_tables = 2;
11406068504SSong Gao 
11506068504SSong Gao     initrd_table->base = initrd_offset;
11606068504SSong Gao     initrd_table->size = initrd_size;
11706068504SSong Gao }
11806068504SSong Gao 
11960423851SSong Gao static void init_efi_fdt_table(struct efi_system_table *systab)
12060423851SSong Gao {
12160423851SSong Gao     efi_guid_t tbl_guid = DEVICE_TREE_GUID;
12260423851SSong Gao 
12360423851SSong Gao     /* efi_configuration_table 3 */
12460423851SSong Gao     guidcpy(&systab->tables[2].guid, &tbl_guid);
12560423851SSong Gao     systab->tables[2].table = (void *)FDT_BASE;
12660423851SSong Gao     systab->nr_tables = 3;
12760423851SSong Gao }
12860423851SSong Gao 
1294216baa9SSong Gao static void init_systab(struct loongarch_boot_info *info, void *p, void *start)
1304216baa9SSong Gao {
131252b8e68SSong Gao     void *bp_tables_start;
1324216baa9SSong Gao     struct efi_system_table *systab = p;
1334216baa9SSong Gao 
1344216baa9SSong Gao     info->a2 = p - start;
1354216baa9SSong Gao 
1364216baa9SSong Gao     systab->hdr.signature = EFI_SYSTEM_TABLE_SIGNATURE;
1374216baa9SSong Gao     systab->hdr.revision = EFI_SPECIFICATION_VERSION;
1384216baa9SSong Gao     systab->hdr.revision = sizeof(struct efi_system_table),
1394216baa9SSong Gao     systab->fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8;
1404216baa9SSong Gao     systab->runtime = 0;
1414216baa9SSong Gao     systab->boottime = 0;
1424216baa9SSong Gao     systab->nr_tables = 0;
1434216baa9SSong Gao 
1444216baa9SSong Gao     p += ROUND_UP(sizeof(struct efi_system_table), 64 * KiB);
1454216baa9SSong Gao 
1464216baa9SSong Gao     systab->tables = p;
147252b8e68SSong Gao     bp_tables_start = p;
148252b8e68SSong Gao 
149252b8e68SSong Gao     init_efi_boot_memmap(systab, p, start);
150252b8e68SSong Gao     p += ROUND_UP(sizeof(struct efi_boot_memmap) +
151252b8e68SSong Gao                   sizeof(efi_memory_desc_t) * memmap_entries, 64 * KiB);
15206068504SSong Gao     init_efi_initrd_table(systab, p, start);
15306068504SSong Gao     p += ROUND_UP(sizeof(struct efi_initrd), 64 * KiB);
15460423851SSong Gao     init_efi_fdt_table(systab);
155252b8e68SSong Gao 
156252b8e68SSong Gao     systab->tables = (struct efi_configuration_table *)(bp_tables_start - start);
1574216baa9SSong Gao }
1584216baa9SSong Gao 
15958ee60d2SSong Gao static void init_cmdline(struct loongarch_boot_info *info, void *p, void *start)
16058ee60d2SSong Gao {
16158ee60d2SSong Gao     hwaddr cmdline_addr = p - start;
16258ee60d2SSong Gao 
16358ee60d2SSong Gao     info->a0 = 1;
16458ee60d2SSong Gao     info->a1 = cmdline_addr;
16558ee60d2SSong Gao 
16658ee60d2SSong Gao     memcpy(p, info->kernel_cmdline, COMMAND_LINE_SIZE);
16758ee60d2SSong Gao }
16858ee60d2SSong Gao 
169d771ca1cSSong Gao static uint64_t cpu_loongarch_virt_to_phys(void *opaque, uint64_t addr)
170d771ca1cSSong Gao {
171d771ca1cSSong Gao     return addr & MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
172d771ca1cSSong Gao }
173d771ca1cSSong Gao 
174d771ca1cSSong Gao static int64_t load_kernel_info(struct loongarch_boot_info *info)
175d771ca1cSSong Gao {
17606068504SSong Gao     uint64_t kernel_entry, kernel_low, kernel_high;
177d771ca1cSSong Gao     ssize_t kernel_size;
178d771ca1cSSong Gao 
179d771ca1cSSong Gao     kernel_size = load_elf(info->kernel_filename, NULL,
180d771ca1cSSong Gao                            cpu_loongarch_virt_to_phys, NULL,
181d771ca1cSSong Gao                            &kernel_entry, &kernel_low,
182d771ca1cSSong Gao                            &kernel_high, NULL, 0,
183d771ca1cSSong Gao                            EM_LOONGARCH, 1, 0);
184d771ca1cSSong Gao 
185d771ca1cSSong Gao     if (kernel_size < 0) {
186d771ca1cSSong Gao         error_report("could not load kernel '%s': %s",
187d771ca1cSSong Gao                      info->kernel_filename,
188d771ca1cSSong Gao                      load_elf_strerror(kernel_size));
189d771ca1cSSong Gao         exit(1);
190d771ca1cSSong Gao     }
19102307a67SSong Gao 
19202307a67SSong Gao     if (info->initrd_filename) {
19302307a67SSong Gao         initrd_size = get_image_size(info->initrd_filename);
19402307a67SSong Gao         if (initrd_size > 0) {
19502307a67SSong Gao             initrd_offset = ROUND_UP(kernel_high + 4 * kernel_size, 64 * KiB);
19602307a67SSong Gao 
19702307a67SSong Gao             if (initrd_offset + initrd_size > info->ram_size) {
19802307a67SSong Gao                 error_report("memory too small for initial ram disk '%s'",
19902307a67SSong Gao                              info->initrd_filename);
20002307a67SSong Gao                 exit(1);
20102307a67SSong Gao             }
20202307a67SSong Gao 
20302307a67SSong Gao             initrd_size = load_image_targphys(info->initrd_filename, initrd_offset,
20402307a67SSong Gao                                               info->ram_size - initrd_offset);
20502307a67SSong Gao         }
20602307a67SSong Gao 
20702307a67SSong Gao         if (initrd_size == (target_ulong)-1) {
20802307a67SSong Gao             error_report("could not load initial ram disk '%s'",
20902307a67SSong Gao                          info->initrd_filename);
21002307a67SSong Gao             exit(1);
21102307a67SSong Gao         }
21202307a67SSong Gao     } else {
21302307a67SSong Gao         initrd_size = 0;
21402307a67SSong Gao     }
21502307a67SSong Gao 
216d771ca1cSSong Gao     return kernel_entry;
217d771ca1cSSong Gao }
218d771ca1cSSong Gao 
219d771ca1cSSong Gao static void reset_load_elf(void *opaque)
220d771ca1cSSong Gao {
221d771ca1cSSong Gao     LoongArchCPU *cpu = opaque;
222d771ca1cSSong Gao     CPULoongArchState *env = &cpu->env;
223d771ca1cSSong Gao 
224d771ca1cSSong Gao     cpu_reset(CPU(cpu));
225d771ca1cSSong Gao     if (env->load_elf) {
22658ee60d2SSong Gao 	if (cpu == LOONGARCH_CPU(first_cpu)) {
22758ee60d2SSong Gao             env->gpr[4] = env->boot_info->a0;
22858ee60d2SSong Gao             env->gpr[5] = env->boot_info->a1;
2294216baa9SSong Gao             env->gpr[6] = env->boot_info->a2;
23058ee60d2SSong Gao         }
231d771ca1cSSong Gao         cpu_set_pc(CPU(cpu), env->elf_address);
232d771ca1cSSong Gao     }
233d771ca1cSSong Gao }
234d771ca1cSSong Gao 
235d771ca1cSSong Gao static void fw_cfg_add_kernel_info(struct loongarch_boot_info *info,
236d771ca1cSSong Gao                                    FWCfgState *fw_cfg)
237d771ca1cSSong Gao {
238d771ca1cSSong Gao     /*
239d771ca1cSSong Gao      * Expose the kernel, the command line, and the initrd in fw_cfg.
240d771ca1cSSong Gao      * We don't process them here at all, it's all left to the
241d771ca1cSSong Gao      * firmware.
242d771ca1cSSong Gao      */
243d771ca1cSSong Gao     load_image_to_fw_cfg(fw_cfg,
244d771ca1cSSong Gao                          FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
245d771ca1cSSong Gao                          info->kernel_filename,
246d771ca1cSSong Gao                          false);
247d771ca1cSSong Gao 
248d771ca1cSSong Gao     if (info->initrd_filename) {
249d771ca1cSSong Gao         load_image_to_fw_cfg(fw_cfg,
250d771ca1cSSong Gao                              FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
251d771ca1cSSong Gao                              info->initrd_filename, false);
252d771ca1cSSong Gao     }
253d771ca1cSSong Gao 
254d771ca1cSSong Gao     if (info->kernel_cmdline) {
255d771ca1cSSong Gao         fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
256d771ca1cSSong Gao                        strlen(info->kernel_cmdline) + 1);
257d771ca1cSSong Gao         fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
258d771ca1cSSong Gao                           info->kernel_cmdline);
259d771ca1cSSong Gao     }
260d771ca1cSSong Gao }
261d771ca1cSSong Gao 
262*d804ad98SBibo Mao static void loongarch_firmware_boot(LoongArchVirtMachineState *lvms,
263d771ca1cSSong Gao                                     struct loongarch_boot_info *info)
264d771ca1cSSong Gao {
265*d804ad98SBibo Mao     fw_cfg_add_kernel_info(info, lvms->fw_cfg);
266d771ca1cSSong Gao }
267d771ca1cSSong Gao 
26858ee60d2SSong Gao static void init_boot_rom(struct loongarch_boot_info *info, void *p)
26958ee60d2SSong Gao {
27058ee60d2SSong Gao     void *start = p;
27158ee60d2SSong Gao 
27258ee60d2SSong Gao     init_cmdline(info, p, start);
27358ee60d2SSong Gao     p += COMMAND_LINE_SIZE;
2744216baa9SSong Gao 
2754216baa9SSong Gao     init_systab(info, p, start);
27658ee60d2SSong Gao }
27758ee60d2SSong Gao 
278d771ca1cSSong Gao static void loongarch_direct_kernel_boot(struct loongarch_boot_info *info)
279d771ca1cSSong Gao {
28058ee60d2SSong Gao     void *p, *bp;
281d771ca1cSSong Gao     int64_t kernel_addr = 0;
282d771ca1cSSong Gao     LoongArchCPU *lacpu;
283d771ca1cSSong Gao     CPUState *cs;
284d771ca1cSSong Gao 
285d771ca1cSSong Gao     if (info->kernel_filename) {
286d771ca1cSSong Gao         kernel_addr = load_kernel_info(info);
287d771ca1cSSong Gao     } else {
288d771ca1cSSong Gao         if(!qtest_enabled()) {
289d771ca1cSSong Gao             error_report("Need kernel filename\n");
290d771ca1cSSong Gao             exit(1);
291d771ca1cSSong Gao         }
292d771ca1cSSong Gao     }
293d771ca1cSSong Gao 
29458ee60d2SSong Gao     /* Load cmdline and system tables at [0 - 1 MiB] */
29558ee60d2SSong Gao     p = g_malloc0(1 * MiB);
29658ee60d2SSong Gao     bp = p;
29758ee60d2SSong Gao     init_boot_rom(info, p);
29858ee60d2SSong Gao     rom_add_blob_fixed_as("boot_info", bp, 1 * MiB, 0, &address_space_memory);
29958ee60d2SSong Gao 
3007e0510d7SSong Gao     /* Load slave boot code at pflash0 . */
3017e0510d7SSong Gao     void *boot_code = g_malloc0(VIRT_FLASH0_SIZE);
3027e0510d7SSong Gao     memcpy(boot_code, &slave_boot_code, sizeof(slave_boot_code));
3037e0510d7SSong Gao     rom_add_blob_fixed("boot_code", boot_code, VIRT_FLASH0_SIZE, VIRT_FLASH0_BASE);
3047e0510d7SSong Gao 
305d771ca1cSSong Gao     CPU_FOREACH(cs) {
306d771ca1cSSong Gao         lacpu = LOONGARCH_CPU(cs);
307d771ca1cSSong Gao         lacpu->env.load_elf = true;
3087e0510d7SSong Gao         if (cs == first_cpu) {
309d771ca1cSSong Gao             lacpu->env.elf_address = kernel_addr;
3107e0510d7SSong Gao         } else {
3117e0510d7SSong Gao             lacpu->env.elf_address = VIRT_FLASH0_BASE;
312d771ca1cSSong Gao         }
3137e0510d7SSong Gao         lacpu->env.boot_info = info;
3147e0510d7SSong Gao     }
3157e0510d7SSong Gao 
3167e0510d7SSong Gao     g_free(boot_code);
31758ee60d2SSong Gao     g_free(bp);
318d771ca1cSSong Gao }
319d771ca1cSSong Gao 
320d771ca1cSSong Gao void loongarch_load_kernel(MachineState *ms, struct loongarch_boot_info *info)
321d771ca1cSSong Gao {
322*d804ad98SBibo Mao     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(ms);
323d771ca1cSSong Gao     int i;
324d771ca1cSSong Gao 
325d771ca1cSSong Gao     /* register reset function */
326d771ca1cSSong Gao     for (i = 0; i < ms->smp.cpus; i++) {
327d771ca1cSSong Gao         qemu_register_reset(reset_load_elf, LOONGARCH_CPU(qemu_get_cpu(i)));
328d771ca1cSSong Gao     }
329d771ca1cSSong Gao 
330d771ca1cSSong Gao     info->kernel_filename = ms->kernel_filename;
331d771ca1cSSong Gao     info->kernel_cmdline = ms->kernel_cmdline;
332d771ca1cSSong Gao     info->initrd_filename = ms->initrd_filename;
333d771ca1cSSong Gao 
334*d804ad98SBibo Mao     if (lvms->bios_loaded) {
335*d804ad98SBibo Mao         loongarch_firmware_boot(lvms, info);
336d771ca1cSSong Gao     } else {
337d771ca1cSSong Gao         loongarch_direct_kernel_boot(info);
338d771ca1cSSong Gao     }
339d771ca1cSSong Gao }
340