xref: /qemu/hw/loongarch/boot.c (revision 513823e7521a09ed7ad1e32e6454bac3b2cbf52d)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * LoongArch boot helper functions.
4  *
5  * Copyright (c) 2023 Loongson Technology Corporation Limited
6  */
7 
8 #include "qemu/osdep.h"
9 #include "qemu/units.h"
10 #include "target/loongarch/cpu.h"
11 #include "hw/loongarch/virt.h"
12 #include "hw/loader.h"
13 #include "elf.h"
14 #include "qemu/error-report.h"
15 #include "system/reset.h"
16 #include "system/qtest.h"
17 
18 /*
19  * Linux Image Format
20  * https://docs.kernel.org/arch/loongarch/booting.html
21  */
22 #define LINUX_PE_MAGIC  0x818223cd
23 #define MZ_MAGIC        0x5a4d /* "MZ" */
24 
25 struct loongarch_linux_hdr {
26     uint32_t mz_magic;
27     uint32_t res0;
28     uint64_t kernel_entry;
29     uint64_t kernel_size;
30     uint64_t load_offset;
31     uint64_t res1;
32     uint64_t res2;
33     uint64_t res3;
34     uint32_t linux_pe_magic;
35     uint32_t pe_header_offset;
36 } QEMU_PACKED;
37 
38 struct memmap_entry *memmap_table;
39 unsigned memmap_entries;
40 
41 ram_addr_t initrd_offset;
42 uint64_t initrd_size;
43 
44 static const unsigned int slave_boot_code[] = {
45                   /* Configure reset ebase.                    */
46     0x0400302c,   /* csrwr      $t0, LOONGARCH_CSR_EENTRY      */
47 
48                   /* Disable interrupt.                        */
49     0x0380100c,   /* ori        $t0, $zero,0x4                 */
50     0x04000180,   /* csrxchg    $zero, $t0, LOONGARCH_CSR_CRMD */
51 
52                   /* Clear mailbox.                            */
53     0x1400002d,   /* lu12i.w    $t1, 1(0x1)                    */
54     0x038081ad,   /* ori        $t1, $t1, CORE_BUF_20  */
55     0x06481da0,   /* iocsrwr.d  $zero, $t1                     */
56 
57                   /* Enable IPI interrupt.                     */
58     0x1400002c,   /* lu12i.w    $t0, 1(0x1)                    */
59     0x0400118c,   /* csrxchg    $t0, $t0, LOONGARCH_CSR_ECFG   */
60     0x02fffc0c,   /* addi.d     $t0, $r0,-1(0xfff)             */
61     0x1400002d,   /* lu12i.w    $t1, 1(0x1)                    */
62     0x038011ad,   /* ori        $t1, $t1, CORE_EN_OFF          */
63     0x064819ac,   /* iocsrwr.w  $t0, $t1                       */
64     0x1400002d,   /* lu12i.w    $t1, 1(0x1)                    */
65     0x038081ad,   /* ori        $t1, $t1, CORE_BUF_20          */
66 
67                   /* Wait for wakeup  <.L11>:                  */
68     0x06488000,   /* idle       0x0                            */
69     0x03400000,   /* andi       $zero, $zero, 0x0              */
70     0x064809ac,   /* iocsrrd.w  $t0, $t1                       */
71     0x43fff59f,   /* beqz       $t0, -12(0x7ffff4) # 48 <.L11> */
72 
73                   /* Read and clear IPI interrupt.             */
74     0x1400002d,   /* lu12i.w    $t1, 1(0x1)                    */
75     0x064809ac,   /* iocsrrd.w  $t0, $t1                       */
76     0x1400002d,   /* lu12i.w    $t1, 1(0x1)                    */
77     0x038031ad,   /* ori        $t1, $t1, CORE_CLEAR_OFF       */
78     0x064819ac,   /* iocsrwr.w  $t0, $t1                       */
79 
80                   /* Disable  IPI interrupt.                   */
81     0x1400002c,   /* lu12i.w    $t0, 1(0x1)                    */
82     0x04001180,   /* csrxchg    $zero, $t0, LOONGARCH_CSR_ECFG */
83 
84                   /* Read mail buf and jump to specified entry */
85     0x1400002d,   /* lu12i.w    $t1, 1(0x1)                    */
86     0x038081ad,   /* ori        $t1, $t1, CORE_BUF_20          */
87     0x06480dac,   /* iocsrrd.d  $t0, $t1                       */
88     0x00150181,   /* move       $ra, $t0                       */
89     0x4c000020,   /* jirl       $zero, $ra,0                   */
90 };
91 
92 static inline void *guidcpy(void *dst, const void *src)
93 {
94     return memcpy(dst, src, sizeof(efi_guid_t));
95 }
96 
97 static void init_efi_boot_memmap(struct efi_system_table *systab,
98                                  void *p, void *start)
99 {
100     unsigned i;
101     struct efi_boot_memmap *boot_memmap = p;
102     efi_guid_t tbl_guid = LINUX_EFI_BOOT_MEMMAP_GUID;
103 
104     /* efi_configuration_table 1 */
105     guidcpy(&systab->tables[0].guid, &tbl_guid);
106     systab->tables[0].table = (struct efi_configuration_table *)(p - start);
107     systab->nr_tables = 1;
108 
109     boot_memmap->desc_size = sizeof(efi_memory_desc_t);
110     boot_memmap->desc_ver = 1;
111     boot_memmap->map_size = 0;
112 
113     efi_memory_desc_t *map = p + sizeof(struct efi_boot_memmap);
114     for (i = 0; i < memmap_entries; i++) {
115         map = (void *)boot_memmap + sizeof(*map);
116         map[i].type = memmap_table[i].type;
117         map[i].phys_addr = ROUND_UP(memmap_table[i].address, 64 * KiB);
118         map[i].num_pages = ROUND_DOWN(memmap_table[i].address +
119                         memmap_table[i].length - map[i].phys_addr, 64 * KiB);
120         p += sizeof(efi_memory_desc_t);
121     }
122 }
123 
124 static void init_efi_initrd_table(struct efi_system_table *systab,
125                                   void *p, void *start)
126 {
127     efi_guid_t tbl_guid = LINUX_EFI_INITRD_MEDIA_GUID;
128     struct efi_initrd *initrd_table  = p;
129 
130     /* efi_configuration_table 2 */
131     guidcpy(&systab->tables[1].guid, &tbl_guid);
132     systab->tables[1].table = (struct efi_configuration_table *)(p - start);
133     systab->nr_tables = 2;
134 
135     initrd_table->base = initrd_offset;
136     initrd_table->size = initrd_size;
137 }
138 
139 static void init_efi_fdt_table(struct efi_system_table *systab)
140 {
141     efi_guid_t tbl_guid = DEVICE_TREE_GUID;
142 
143     /* efi_configuration_table 3 */
144     guidcpy(&systab->tables[2].guid, &tbl_guid);
145     systab->tables[2].table = (void *)FDT_BASE;
146     systab->nr_tables = 3;
147 }
148 
149 static void init_systab(struct loongarch_boot_info *info, void *p, void *start)
150 {
151     void *bp_tables_start;
152     struct efi_system_table *systab = p;
153 
154     info->a2 = p - start;
155 
156     systab->hdr.signature = EFI_SYSTEM_TABLE_SIGNATURE;
157     systab->hdr.revision = EFI_SPECIFICATION_VERSION;
158     systab->hdr.revision = sizeof(struct efi_system_table),
159     systab->fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8;
160     systab->runtime = 0;
161     systab->boottime = 0;
162     systab->nr_tables = 0;
163 
164     p += ROUND_UP(sizeof(struct efi_system_table), 64 * KiB);
165 
166     systab->tables = p;
167     bp_tables_start = p;
168 
169     init_efi_boot_memmap(systab, p, start);
170     p += ROUND_UP(sizeof(struct efi_boot_memmap) +
171                   sizeof(efi_memory_desc_t) * memmap_entries, 64 * KiB);
172     init_efi_initrd_table(systab, p, start);
173     p += ROUND_UP(sizeof(struct efi_initrd), 64 * KiB);
174     init_efi_fdt_table(systab);
175 
176     systab->tables = (struct efi_configuration_table *)(bp_tables_start - start);
177 }
178 
179 static void init_cmdline(struct loongarch_boot_info *info, void *p, void *start)
180 {
181     hwaddr cmdline_addr = p - start;
182 
183     info->a0 = 1;
184     info->a1 = cmdline_addr;
185 
186     g_strlcpy(p, info->kernel_cmdline, COMMAND_LINE_SIZE);
187 }
188 
189 static uint64_t cpu_loongarch_virt_to_phys(void *opaque, uint64_t addr)
190 {
191     return addr & MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
192 }
193 
194 static int64_t load_loongarch_linux_image(const char *filename,
195                                           uint64_t *kernel_entry,
196                                           uint64_t *kernel_low,
197                                           uint64_t *kernel_high)
198 {
199     gsize len;
200     ssize_t size;
201     uint8_t *buffer;
202     struct loongarch_linux_hdr *hdr;
203 
204     /* Load as raw file otherwise */
205     if (!g_file_get_contents(filename, (char **)&buffer, &len, NULL)) {
206         return -1;
207     }
208     size = len;
209 
210     /* Unpack the image if it is a EFI zboot image */
211     if (unpack_efi_zboot_image(&buffer, &size) < 0) {
212         g_free(buffer);
213         return -1;
214     }
215 
216     hdr = (struct loongarch_linux_hdr *)buffer;
217 
218     if (extract32(le32_to_cpu(hdr->mz_magic), 0, 16) != MZ_MAGIC ||
219         le32_to_cpu(hdr->linux_pe_magic) != LINUX_PE_MAGIC) {
220         g_free(buffer);
221         return -1;
222     }
223 
224     /* Early kernel versions may have those fields in virtual address */
225     *kernel_entry = extract64(le64_to_cpu(hdr->kernel_entry),
226                               0, TARGET_PHYS_ADDR_SPACE_BITS);
227     *kernel_low = extract64(le64_to_cpu(hdr->load_offset),
228                             0, TARGET_PHYS_ADDR_SPACE_BITS);
229     *kernel_high = *kernel_low + size;
230 
231     rom_add_blob_fixed(filename, buffer, size, *kernel_low);
232 
233     g_free(buffer);
234 
235     return size;
236 }
237 
238 static int64_t load_kernel_info(struct loongarch_boot_info *info)
239 {
240     uint64_t kernel_entry, kernel_low, kernel_high;
241     ssize_t kernel_size;
242 
243     kernel_size = load_elf(info->kernel_filename, NULL,
244                            cpu_loongarch_virt_to_phys, NULL,
245                            &kernel_entry, &kernel_low,
246                            &kernel_high, NULL, ELFDATA2LSB,
247                            EM_LOONGARCH, 1, 0);
248     if (kernel_size < 0) {
249         kernel_size = load_loongarch_linux_image(info->kernel_filename,
250                                                  &kernel_entry, &kernel_low,
251                                                  &kernel_high);
252     }
253 
254     if (kernel_size < 0) {
255         error_report("could not load kernel '%s': %s",
256                      info->kernel_filename,
257                      load_elf_strerror(kernel_size));
258         exit(1);
259     }
260 
261     if (info->initrd_filename) {
262         initrd_size = get_image_size(info->initrd_filename);
263         if (initrd_size > 0) {
264             initrd_offset = ROUND_UP(kernel_high + 4 * kernel_size, 64 * KiB);
265 
266             if (initrd_offset + initrd_size > info->ram_size) {
267                 error_report("memory too small for initial ram disk '%s'",
268                              info->initrd_filename);
269                 exit(1);
270             }
271 
272             initrd_size = load_image_targphys(info->initrd_filename, initrd_offset,
273                                               info->ram_size - initrd_offset);
274         }
275 
276         if (initrd_size == (target_ulong)-1) {
277             error_report("could not load initial ram disk '%s'",
278                          info->initrd_filename);
279             exit(1);
280         }
281     } else {
282         initrd_size = 0;
283     }
284 
285     return kernel_entry;
286 }
287 
288 static void reset_load_elf(void *opaque)
289 {
290     LoongArchCPU *cpu = opaque;
291     CPULoongArchState *env = &cpu->env;
292 
293     cpu_reset(CPU(cpu));
294     if (env->load_elf) {
295         if (cpu == LOONGARCH_CPU(first_cpu)) {
296             env->gpr[4] = env->boot_info->a0;
297             env->gpr[5] = env->boot_info->a1;
298             env->gpr[6] = env->boot_info->a2;
299         }
300         cpu_set_pc(CPU(cpu), env->elf_address);
301     }
302 }
303 
304 static void fw_cfg_add_kernel_info(struct loongarch_boot_info *info,
305                                    FWCfgState *fw_cfg)
306 {
307     /*
308      * Expose the kernel, the command line, and the initrd in fw_cfg.
309      * We don't process them here at all, it's all left to the
310      * firmware.
311      */
312     load_image_to_fw_cfg(fw_cfg,
313                          FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
314                          info->kernel_filename,
315                          false);
316 
317     if (info->initrd_filename) {
318         load_image_to_fw_cfg(fw_cfg,
319                              FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
320                              info->initrd_filename, false);
321     }
322 
323     if (info->kernel_cmdline) {
324         fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
325                        strlen(info->kernel_cmdline) + 1);
326         fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
327                           info->kernel_cmdline);
328     }
329 }
330 
331 static void loongarch_firmware_boot(LoongArchVirtMachineState *lvms,
332                                     struct loongarch_boot_info *info)
333 {
334     fw_cfg_add_kernel_info(info, lvms->fw_cfg);
335 }
336 
337 static void init_boot_rom(struct loongarch_boot_info *info, void *p)
338 {
339     void *start = p;
340 
341     init_cmdline(info, p, start);
342     p += COMMAND_LINE_SIZE;
343 
344     init_systab(info, p, start);
345 }
346 
347 static void loongarch_direct_kernel_boot(struct loongarch_boot_info *info)
348 {
349     void *p, *bp;
350     int64_t kernel_addr = VIRT_FLASH0_BASE;
351     LoongArchCPU *lacpu;
352     CPUState *cs;
353 
354     if (info->kernel_filename) {
355         kernel_addr = load_kernel_info(info);
356     } else {
357         if (!qtest_enabled()) {
358             warn_report("No kernel provided, booting from flash drive.");
359         }
360     }
361 
362     /* Load cmdline and system tables at [0 - 1 MiB] */
363     p = g_malloc0(1 * MiB);
364     bp = p;
365     init_boot_rom(info, p);
366     rom_add_blob_fixed_as("boot_info", bp, 1 * MiB, 0, &address_space_memory);
367 
368     /* Load slave boot code at pflash0 . */
369     void *boot_code = g_malloc0(VIRT_FLASH0_SIZE);
370     memcpy(boot_code, &slave_boot_code, sizeof(slave_boot_code));
371     rom_add_blob_fixed("boot_code", boot_code, VIRT_FLASH0_SIZE, VIRT_FLASH0_BASE);
372 
373     CPU_FOREACH(cs) {
374         lacpu = LOONGARCH_CPU(cs);
375         lacpu->env.load_elf = true;
376         if (cs == first_cpu) {
377             lacpu->env.elf_address = kernel_addr;
378         } else {
379             lacpu->env.elf_address = VIRT_FLASH0_BASE;
380         }
381         lacpu->env.boot_info = info;
382     }
383 
384     g_free(boot_code);
385     g_free(bp);
386 }
387 
388 void loongarch_load_kernel(MachineState *ms, struct loongarch_boot_info *info)
389 {
390     LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(ms);
391     int i;
392 
393     /* register reset function */
394     for (i = 0; i < ms->smp.cpus; i++) {
395         qemu_register_reset(reset_load_elf, LOONGARCH_CPU(qemu_get_cpu(i)));
396     }
397 
398     info->kernel_filename = ms->kernel_filename;
399     info->kernel_cmdline = ms->kernel_cmdline;
400     info->initrd_filename = ms->initrd_filename;
401 
402     if (lvms->bios_loaded) {
403         loongarch_firmware_boot(lvms, info);
404     } else {
405         loongarch_direct_kernel_boot(info);
406     }
407 }
408