4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
17 #include "device_tree.h"
19 #define KERNEL_ARGS_ADDR 0x100
20 #define KERNEL_LOAD_ADDR 0x00010000
21 #define INITRD_LOAD_ADDR 0x00d00000
23 /* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
24 static uint32_t bootloader
[] = {
25 0xe3a00000, /* mov r0, #0 */
26 0xe59f1004, /* ldr r1, [pc, #4] */
27 0xe59f2004, /* ldr r2, [pc, #4] */
28 0xe59ff004, /* ldr pc, [pc, #4] */
30 0, /* Address of kernel args. Set by integratorcp_init. */
31 0 /* Kernel entry point. Set by integratorcp_init. */
34 /* Handling for secondary CPU boot in a multicore system.
35 * Unlike the uniprocessor/primary CPU boot, this is platform
36 * dependent. The default code here is based on the secondary
37 * CPU boot protocol used on realview/vexpress boards, with
38 * some parameterisation to increase its flexibility.
39 * QEMU platform models for which this code is not appropriate
40 * should override write_secondary_boot and secondary_cpu_reset_hook
43 * This code enables the interrupt controllers for the secondary
44 * CPUs and then puts all the secondary CPUs into a loop waiting
45 * for an interprocessor interrupt and polling a configurable
46 * location for the kernel secondary CPU entry point.
48 static uint32_t smpboot
[] = {
49 0xe59f201c, /* ldr r2, gic_cpu_if */
50 0xe59f001c, /* ldr r0, startaddr */
51 0xe3a01001, /* mov r1, #1 */
52 0xe5821000, /* str r1, [r2] */
54 0xe5901000, /* ldr r1, [r0] */
55 0xe1110001, /* tst r1, r1 */
56 0x0afffffb, /* beq <wfi> */
57 0xe12fff11, /* bx r1 */
58 0, /* gic_cpu_if: base address of GIC CPU interface */
59 0 /* bootreg: Boot register address is held here */
62 static void default_write_secondary(ARMCPU
*cpu
,
63 const struct arm_boot_info
*info
)
66 smpboot
[ARRAY_SIZE(smpboot
) - 1] = info
->smp_bootreg_addr
;
67 smpboot
[ARRAY_SIZE(smpboot
) - 2] = info
->gic_cpu_if_addr
;
68 for (n
= 0; n
< ARRAY_SIZE(smpboot
); n
++) {
69 smpboot
[n
] = tswap32(smpboot
[n
]);
71 rom_add_blob_fixed("smpboot", smpboot
, sizeof(smpboot
),
72 info
->smp_loader_start
);
75 static void default_reset_secondary(ARMCPU
*cpu
,
76 const struct arm_boot_info
*info
)
78 CPUARMState
*env
= &cpu
->env
;
80 stl_phys_notdirty(info
->smp_bootreg_addr
, 0);
81 env
->regs
[15] = info
->smp_loader_start
;
84 #define WRITE_WORD(p, value) do { \
85 stl_phys_notdirty(p, value); \
89 static void set_kernel_args(const struct arm_boot_info
*info
)
91 int initrd_size
= info
->initrd_size
;
92 target_phys_addr_t base
= info
->loader_start
;
95 p
= base
+ KERNEL_ARGS_ADDR
;
98 WRITE_WORD(p
, 0x54410001);
100 WRITE_WORD(p
, 0x1000);
103 /* TODO: handle multiple chips on one ATAG list */
105 WRITE_WORD(p
, 0x54410002);
106 WRITE_WORD(p
, info
->ram_size
);
107 WRITE_WORD(p
, info
->loader_start
);
111 WRITE_WORD(p
, 0x54420005);
112 WRITE_WORD(p
, info
->loader_start
+ INITRD_LOAD_ADDR
);
113 WRITE_WORD(p
, initrd_size
);
115 if (info
->kernel_cmdline
&& *info
->kernel_cmdline
) {
119 cmdline_size
= strlen(info
->kernel_cmdline
);
120 cpu_physical_memory_write(p
+ 8, (void *)info
->kernel_cmdline
,
122 cmdline_size
= (cmdline_size
>> 2) + 1;
123 WRITE_WORD(p
, cmdline_size
+ 2);
124 WRITE_WORD(p
, 0x54410009);
125 p
+= cmdline_size
* 4;
127 if (info
->atag_board
) {
130 uint8_t atag_board_buf
[0x1000];
132 atag_board_len
= (info
->atag_board(info
, atag_board_buf
) + 3) & ~3;
133 WRITE_WORD(p
, (atag_board_len
+ 8) >> 2);
134 WRITE_WORD(p
, 0x414f4d50);
135 cpu_physical_memory_write(p
, atag_board_buf
, atag_board_len
);
143 static void set_kernel_args_old(const struct arm_boot_info
*info
)
145 target_phys_addr_t p
;
147 int initrd_size
= info
->initrd_size
;
148 target_phys_addr_t base
= info
->loader_start
;
150 /* see linux/include/asm-arm/setup.h */
151 p
= base
+ KERNEL_ARGS_ADDR
;
155 WRITE_WORD(p
, info
->ram_size
/ 4096);
158 #define FLAG_READONLY 1
159 #define FLAG_RDLOAD 4
160 #define FLAG_RDPROMPT 8
162 WRITE_WORD(p
, FLAG_READONLY
| FLAG_RDLOAD
| FLAG_RDPROMPT
);
164 WRITE_WORD(p
, (31 << 8) | 0); /* /dev/mtdblock0 */
173 /* memc_control_reg */
175 /* unsigned char sounddefault */
176 /* unsigned char adfsdrives */
177 /* unsigned char bytes_per_char_h */
178 /* unsigned char bytes_per_char_v */
180 /* pages_in_bank[4] */
189 WRITE_WORD(p
, info
->loader_start
+ INITRD_LOAD_ADDR
);
193 WRITE_WORD(p
, initrd_size
);
198 /* system_serial_low */
200 /* system_serial_high */
204 /* zero unused fields */
205 while (p
< base
+ KERNEL_ARGS_ADDR
+ 256 + 1024) {
208 s
= info
->kernel_cmdline
;
210 cpu_physical_memory_write(p
, (void *)s
, strlen(s
) + 1);
216 static int load_dtb(target_phys_addr_t addr
, const struct arm_boot_info
*binfo
)
219 uint32_t mem_reg_property
[] = { cpu_to_be32(binfo
->loader_start
),
220 cpu_to_be32(binfo
->ram_size
) };
225 filename
= qemu_find_file(QEMU_FILE_TYPE_BIOS
, binfo
->dtb_filename
);
227 fprintf(stderr
, "Couldn't open dtb file %s\n", binfo
->dtb_filename
);
231 fdt
= load_device_tree(filename
, &size
);
233 fprintf(stderr
, "Couldn't open dtb file %s\n", filename
);
239 rc
= qemu_devtree_setprop(fdt
, "/memory", "reg", mem_reg_property
,
240 sizeof(mem_reg_property
));
242 fprintf(stderr
, "couldn't set /memory/reg\n");
245 rc
= qemu_devtree_setprop_string(fdt
, "/chosen", "bootargs",
246 binfo
->kernel_cmdline
);
248 fprintf(stderr
, "couldn't set /chosen/bootargs\n");
251 if (binfo
->initrd_size
) {
252 rc
= qemu_devtree_setprop_cell(fdt
, "/chosen", "linux,initrd-start",
253 binfo
->loader_start
+ INITRD_LOAD_ADDR
);
255 fprintf(stderr
, "couldn't set /chosen/linux,initrd-start\n");
258 rc
= qemu_devtree_setprop_cell(fdt
, "/chosen", "linux,initrd-end",
259 binfo
->loader_start
+ INITRD_LOAD_ADDR
+
262 fprintf(stderr
, "couldn't set /chosen/linux,initrd-end\n");
266 cpu_physical_memory_write(addr
, fdt
, size
);
271 fprintf(stderr
, "Device tree requested, "
272 "but qemu was compiled without fdt support\n");
277 static void do_cpu_reset(void *opaque
)
279 ARMCPU
*cpu
= opaque
;
280 CPUARMState
*env
= &cpu
->env
;
281 const struct arm_boot_info
*info
= env
->boot_info
;
285 if (!info
->is_linux
) {
286 /* Jump to the entry point. */
287 env
->regs
[15] = info
->entry
& 0xfffffffe;
288 env
->thumb
= info
->entry
& 1;
290 if (env
== first_cpu
) {
291 env
->regs
[15] = info
->loader_start
;
292 if (!info
->dtb_filename
) {
294 set_kernel_args_old(info
);
296 set_kernel_args(info
);
300 info
->secondary_cpu_reset_hook(cpu
, info
);
306 void arm_load_kernel(ARMCPU
*cpu
, struct arm_boot_info
*info
)
308 CPUARMState
*env
= &cpu
->env
;
314 target_phys_addr_t entry
;
316 QemuOpts
*machine_opts
;
318 /* Load the kernel. */
319 if (!info
->kernel_filename
) {
320 fprintf(stderr
, "Kernel image must be specified\n");
324 machine_opts
= qemu_opts_find(qemu_find_opts("machine"), 0);
326 info
->dtb_filename
= qemu_opt_get(machine_opts
, "dtb");
328 info
->dtb_filename
= NULL
;
331 if (!info
->secondary_cpu_reset_hook
) {
332 info
->secondary_cpu_reset_hook
= default_reset_secondary
;
334 if (!info
->write_secondary_boot
) {
335 info
->write_secondary_boot
= default_write_secondary
;
338 if (info
->nb_cpus
== 0)
341 #ifdef TARGET_WORDS_BIGENDIAN
347 /* Assume that raw images are linux kernels, and ELF images are not. */
348 kernel_size
= load_elf(info
->kernel_filename
, NULL
, NULL
, &elf_entry
,
349 NULL
, NULL
, big_endian
, ELF_MACHINE
, 1);
351 if (kernel_size
< 0) {
352 kernel_size
= load_uimage(info
->kernel_filename
, &entry
, NULL
,
355 if (kernel_size
< 0) {
356 entry
= info
->loader_start
+ KERNEL_LOAD_ADDR
;
357 kernel_size
= load_image_targphys(info
->kernel_filename
, entry
,
358 ram_size
- KERNEL_LOAD_ADDR
);
361 if (kernel_size
< 0) {
362 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
363 info
->kernel_filename
);
368 if (info
->initrd_filename
) {
369 initrd_size
= load_image_targphys(info
->initrd_filename
,
372 ram_size
- INITRD_LOAD_ADDR
);
373 if (initrd_size
< 0) {
374 fprintf(stderr
, "qemu: could not load initrd '%s'\n",
375 info
->initrd_filename
);
381 info
->initrd_size
= initrd_size
;
383 bootloader
[4] = info
->board_id
;
385 /* for device tree boot, we pass the DTB directly in r2. Otherwise
386 * we point to the kernel args.
388 if (info
->dtb_filename
) {
389 /* Place the DTB after the initrd in memory */
390 target_phys_addr_t dtb_start
= TARGET_PAGE_ALIGN(info
->loader_start
393 if (load_dtb(dtb_start
, info
)) {
396 bootloader
[5] = dtb_start
;
398 bootloader
[5] = info
->loader_start
+ KERNEL_ARGS_ADDR
;
400 bootloader
[6] = entry
;
401 for (n
= 0; n
< sizeof(bootloader
) / 4; n
++) {
402 bootloader
[n
] = tswap32(bootloader
[n
]);
404 rom_add_blob_fixed("bootloader", bootloader
, sizeof(bootloader
),
406 if (info
->nb_cpus
> 1) {
407 info
->write_secondary_boot(cpu
, info
);
410 info
->is_linux
= is_linux
;
412 for (; env
; env
= env
->next_cpu
) {
413 cpu
= arm_env_get_cpu(env
);
414 env
->boot_info
= info
;
415 qemu_register_reset(do_cpu_reset
, cpu
);