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 "sysemu/reset.h"
16 #include "sysemu/qtest.h"
17
18 struct memmap_entry *memmap_table;
19 unsigned memmap_entries;
20
21 ram_addr_t initrd_offset;
22 uint64_t initrd_size;
23
24 static const unsigned int slave_boot_code[] = {
25 /* Configure reset ebase. */
26 0x0400302c, /* csrwr $t0, LOONGARCH_CSR_EENTRY */
27
28 /* Disable interrupt. */
29 0x0380100c, /* ori $t0, $zero,0x4 */
30 0x04000180, /* csrxchg $zero, $t0, LOONGARCH_CSR_CRMD */
31
32 /* Clear mailbox. */
33 0x1400002d, /* lu12i.w $t1, 1(0x1) */
34 0x038081ad, /* ori $t1, $t1, CORE_BUF_20 */
35 0x06481da0, /* iocsrwr.d $zero, $t1 */
36
37 /* Enable IPI interrupt. */
38 0x1400002c, /* lu12i.w $t0, 1(0x1) */
39 0x0400118c, /* csrxchg $t0, $t0, LOONGARCH_CSR_ECFG */
40 0x02fffc0c, /* addi.d $t0, $r0,-1(0xfff) */
41 0x1400002d, /* lu12i.w $t1, 1(0x1) */
42 0x038011ad, /* ori $t1, $t1, CORE_EN_OFF */
43 0x064819ac, /* iocsrwr.w $t0, $t1 */
44 0x1400002d, /* lu12i.w $t1, 1(0x1) */
45 0x038081ad, /* ori $t1, $t1, CORE_BUF_20 */
46
47 /* Wait for wakeup <.L11>: */
48 0x06488000, /* idle 0x0 */
49 0x03400000, /* andi $zero, $zero, 0x0 */
50 0x064809ac, /* iocsrrd.w $t0, $t1 */
51 0x43fff59f, /* beqz $t0, -12(0x7ffff4) # 48 <.L11> */
52
53 /* Read and clear IPI interrupt. */
54 0x1400002d, /* lu12i.w $t1, 1(0x1) */
55 0x064809ac, /* iocsrrd.w $t0, $t1 */
56 0x1400002d, /* lu12i.w $t1, 1(0x1) */
57 0x038031ad, /* ori $t1, $t1, CORE_CLEAR_OFF */
58 0x064819ac, /* iocsrwr.w $t0, $t1 */
59
60 /* Disable IPI interrupt. */
61 0x1400002c, /* lu12i.w $t0, 1(0x1) */
62 0x04001180, /* csrxchg $zero, $t0, LOONGARCH_CSR_ECFG */
63
64 /* Read mail buf and jump to specified entry */
65 0x1400002d, /* lu12i.w $t1, 1(0x1) */
66 0x038081ad, /* ori $t1, $t1, CORE_BUF_20 */
67 0x06480dac, /* iocsrrd.d $t0, $t1 */
68 0x00150181, /* move $ra, $t0 */
69 0x4c000020, /* jirl $zero, $ra,0 */
70 };
71
guidcpy(void * dst,const void * src)72 static inline void *guidcpy(void *dst, const void *src)
73 {
74 return memcpy(dst, src, sizeof(efi_guid_t));
75 }
76
init_efi_boot_memmap(struct efi_system_table * systab,void * p,void * start)77 static void init_efi_boot_memmap(struct efi_system_table *systab,
78 void *p, void *start)
79 {
80 unsigned i;
81 struct efi_boot_memmap *boot_memmap = p;
82 efi_guid_t tbl_guid = LINUX_EFI_BOOT_MEMMAP_GUID;
83
84 /* efi_configuration_table 1 */
85 guidcpy(&systab->tables[0].guid, &tbl_guid);
86 systab->tables[0].table = (struct efi_configuration_table *)(p - start);
87 systab->nr_tables = 1;
88
89 boot_memmap->desc_size = sizeof(efi_memory_desc_t);
90 boot_memmap->desc_ver = 1;
91 boot_memmap->map_size = 0;
92
93 efi_memory_desc_t *map = p + sizeof(struct efi_boot_memmap);
94 for (i = 0; i < memmap_entries; i++) {
95 map = (void *)boot_memmap + sizeof(*map);
96 map[i].type = memmap_table[i].type;
97 map[i].phys_addr = ROUND_UP(memmap_table[i].address, 64 * KiB);
98 map[i].num_pages = ROUND_DOWN(memmap_table[i].address +
99 memmap_table[i].length - map[i].phys_addr, 64 * KiB);
100 p += sizeof(efi_memory_desc_t);
101 }
102 }
103
init_efi_initrd_table(struct efi_system_table * systab,void * p,void * start)104 static void init_efi_initrd_table(struct efi_system_table *systab,
105 void *p, void *start)
106 {
107 efi_guid_t tbl_guid = LINUX_EFI_INITRD_MEDIA_GUID;
108 struct efi_initrd *initrd_table = p;
109
110 /* efi_configuration_table 2 */
111 guidcpy(&systab->tables[1].guid, &tbl_guid);
112 systab->tables[1].table = (struct efi_configuration_table *)(p - start);
113 systab->nr_tables = 2;
114
115 initrd_table->base = initrd_offset;
116 initrd_table->size = initrd_size;
117 }
118
init_efi_fdt_table(struct efi_system_table * systab)119 static void init_efi_fdt_table(struct efi_system_table *systab)
120 {
121 efi_guid_t tbl_guid = DEVICE_TREE_GUID;
122
123 /* efi_configuration_table 3 */
124 guidcpy(&systab->tables[2].guid, &tbl_guid);
125 systab->tables[2].table = (void *)FDT_BASE;
126 systab->nr_tables = 3;
127 }
128
init_systab(struct loongarch_boot_info * info,void * p,void * start)129 static void init_systab(struct loongarch_boot_info *info, void *p, void *start)
130 {
131 void *bp_tables_start;
132 struct efi_system_table *systab = p;
133
134 info->a2 = p - start;
135
136 systab->hdr.signature = EFI_SYSTEM_TABLE_SIGNATURE;
137 systab->hdr.revision = EFI_SPECIFICATION_VERSION;
138 systab->hdr.revision = sizeof(struct efi_system_table),
139 systab->fw_revision = FW_VERSION << 16 | FW_PATCHLEVEL << 8;
140 systab->runtime = 0;
141 systab->boottime = 0;
142 systab->nr_tables = 0;
143
144 p += ROUND_UP(sizeof(struct efi_system_table), 64 * KiB);
145
146 systab->tables = p;
147 bp_tables_start = p;
148
149 init_efi_boot_memmap(systab, p, start);
150 p += ROUND_UP(sizeof(struct efi_boot_memmap) +
151 sizeof(efi_memory_desc_t) * memmap_entries, 64 * KiB);
152 init_efi_initrd_table(systab, p, start);
153 p += ROUND_UP(sizeof(struct efi_initrd), 64 * KiB);
154 init_efi_fdt_table(systab);
155
156 systab->tables = (struct efi_configuration_table *)(bp_tables_start - start);
157 }
158
init_cmdline(struct loongarch_boot_info * info,void * p,void * start)159 static void init_cmdline(struct loongarch_boot_info *info, void *p, void *start)
160 {
161 hwaddr cmdline_addr = p - start;
162
163 info->a0 = 1;
164 info->a1 = cmdline_addr;
165
166 g_strlcpy(p, info->kernel_cmdline, COMMAND_LINE_SIZE);
167 }
168
cpu_loongarch_virt_to_phys(void * opaque,uint64_t addr)169 static uint64_t cpu_loongarch_virt_to_phys(void *opaque, uint64_t addr)
170 {
171 return addr & MAKE_64BIT_MASK(0, TARGET_PHYS_ADDR_SPACE_BITS);
172 }
173
load_kernel_info(struct loongarch_boot_info * info)174 static int64_t load_kernel_info(struct loongarch_boot_info *info)
175 {
176 uint64_t kernel_entry, kernel_low, kernel_high;
177 ssize_t kernel_size;
178
179 kernel_size = load_elf(info->kernel_filename, NULL,
180 cpu_loongarch_virt_to_phys, NULL,
181 &kernel_entry, &kernel_low,
182 &kernel_high, NULL, 0,
183 EM_LOONGARCH, 1, 0);
184
185 if (kernel_size < 0) {
186 error_report("could not load kernel '%s': %s",
187 info->kernel_filename,
188 load_elf_strerror(kernel_size));
189 exit(1);
190 }
191
192 if (info->initrd_filename) {
193 initrd_size = get_image_size(info->initrd_filename);
194 if (initrd_size > 0) {
195 initrd_offset = ROUND_UP(kernel_high + 4 * kernel_size, 64 * KiB);
196
197 if (initrd_offset + initrd_size > info->ram_size) {
198 error_report("memory too small for initial ram disk '%s'",
199 info->initrd_filename);
200 exit(1);
201 }
202
203 initrd_size = load_image_targphys(info->initrd_filename, initrd_offset,
204 info->ram_size - initrd_offset);
205 }
206
207 if (initrd_size == (target_ulong)-1) {
208 error_report("could not load initial ram disk '%s'",
209 info->initrd_filename);
210 exit(1);
211 }
212 } else {
213 initrd_size = 0;
214 }
215
216 return kernel_entry;
217 }
218
reset_load_elf(void * opaque)219 static void reset_load_elf(void *opaque)
220 {
221 LoongArchCPU *cpu = opaque;
222 CPULoongArchState *env = &cpu->env;
223
224 cpu_reset(CPU(cpu));
225 if (env->load_elf) {
226 if (cpu == LOONGARCH_CPU(first_cpu)) {
227 env->gpr[4] = env->boot_info->a0;
228 env->gpr[5] = env->boot_info->a1;
229 env->gpr[6] = env->boot_info->a2;
230 }
231 cpu_set_pc(CPU(cpu), env->elf_address);
232 }
233 }
234
fw_cfg_add_kernel_info(struct loongarch_boot_info * info,FWCfgState * fw_cfg)235 static void fw_cfg_add_kernel_info(struct loongarch_boot_info *info,
236 FWCfgState *fw_cfg)
237 {
238 /*
239 * Expose the kernel, the command line, and the initrd in fw_cfg.
240 * We don't process them here at all, it's all left to the
241 * firmware.
242 */
243 load_image_to_fw_cfg(fw_cfg,
244 FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
245 info->kernel_filename,
246 false);
247
248 if (info->initrd_filename) {
249 load_image_to_fw_cfg(fw_cfg,
250 FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
251 info->initrd_filename, false);
252 }
253
254 if (info->kernel_cmdline) {
255 fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
256 strlen(info->kernel_cmdline) + 1);
257 fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
258 info->kernel_cmdline);
259 }
260 }
261
loongarch_firmware_boot(LoongArchVirtMachineState * lvms,struct loongarch_boot_info * info)262 static void loongarch_firmware_boot(LoongArchVirtMachineState *lvms,
263 struct loongarch_boot_info *info)
264 {
265 fw_cfg_add_kernel_info(info, lvms->fw_cfg);
266 }
267
init_boot_rom(struct loongarch_boot_info * info,void * p)268 static void init_boot_rom(struct loongarch_boot_info *info, void *p)
269 {
270 void *start = p;
271
272 init_cmdline(info, p, start);
273 p += COMMAND_LINE_SIZE;
274
275 init_systab(info, p, start);
276 }
277
loongarch_direct_kernel_boot(struct loongarch_boot_info * info)278 static void loongarch_direct_kernel_boot(struct loongarch_boot_info *info)
279 {
280 void *p, *bp;
281 int64_t kernel_addr = 0;
282 LoongArchCPU *lacpu;
283 CPUState *cs;
284
285 if (info->kernel_filename) {
286 kernel_addr = load_kernel_info(info);
287 } else {
288 if(!qtest_enabled()) {
289 error_report("Need kernel filename\n");
290 exit(1);
291 }
292 }
293
294 /* Load cmdline and system tables at [0 - 1 MiB] */
295 p = g_malloc0(1 * MiB);
296 bp = p;
297 init_boot_rom(info, p);
298 rom_add_blob_fixed_as("boot_info", bp, 1 * MiB, 0, &address_space_memory);
299
300 /* Load slave boot code at pflash0 . */
301 void *boot_code = g_malloc0(VIRT_FLASH0_SIZE);
302 memcpy(boot_code, &slave_boot_code, sizeof(slave_boot_code));
303 rom_add_blob_fixed("boot_code", boot_code, VIRT_FLASH0_SIZE, VIRT_FLASH0_BASE);
304
305 CPU_FOREACH(cs) {
306 lacpu = LOONGARCH_CPU(cs);
307 lacpu->env.load_elf = true;
308 if (cs == first_cpu) {
309 lacpu->env.elf_address = kernel_addr;
310 } else {
311 lacpu->env.elf_address = VIRT_FLASH0_BASE;
312 }
313 lacpu->env.boot_info = info;
314 }
315
316 g_free(boot_code);
317 g_free(bp);
318 }
319
loongarch_load_kernel(MachineState * ms,struct loongarch_boot_info * info)320 void loongarch_load_kernel(MachineState *ms, struct loongarch_boot_info *info)
321 {
322 LoongArchVirtMachineState *lvms = LOONGARCH_VIRT_MACHINE(ms);
323 int i;
324
325 /* register reset function */
326 for (i = 0; i < ms->smp.cpus; i++) {
327 qemu_register_reset(reset_load_elf, LOONGARCH_CPU(qemu_get_cpu(i)));
328 }
329
330 info->kernel_filename = ms->kernel_filename;
331 info->kernel_cmdline = ms->kernel_cmdline;
332 info->initrd_filename = ms->initrd_filename;
333
334 if (lvms->bios_loaded) {
335 loongarch_firmware_boot(lvms, info);
336 } else {
337 loongarch_direct_kernel_boot(info);
338 }
339 }
340