4 * Copyright (c) 2006-2007 CodeSourcery.
5 * Written by Paul Brook
7 * This code is licensed under the GPL.
16 #define KERNEL_ARGS_ADDR 0x100
17 #define KERNEL_LOAD_ADDR 0x00010000
18 #define INITRD_LOAD_ADDR 0x00d00000
20 /* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
21 static uint32_t bootloader
[] = {
22 0xe3a00000, /* mov r0, #0 */
23 0xe59f1004, /* ldr r1, [pc, #4] */
24 0xe59f2004, /* ldr r2, [pc, #4] */
25 0xe59ff004, /* ldr pc, [pc, #4] */
27 0, /* Address of kernel args. Set by integratorcp_init. */
28 0 /* Kernel entry point. Set by integratorcp_init. */
31 /* Handling for secondary CPU boot in a multicore system.
32 * Unlike the uniprocessor/primary CPU boot, this is platform
33 * dependent. The default code here is based on the secondary
34 * CPU boot protocol used on realview/vexpress boards, with
35 * some parameterisation to increase its flexibility.
36 * QEMU platform models for which this code is not appropriate
37 * should override write_secondary_boot and secondary_cpu_reset_hook
40 * This code enables the interrupt controllers for the secondary
41 * CPUs and then puts all the secondary CPUs into a loop waiting
42 * for an interprocessor interrupt and polling a configurable
43 * location for the kernel secondary CPU entry point.
45 static uint32_t smpboot
[] = {
46 0xe59f201c, /* ldr r2, gic_cpu_if */
47 0xe59f001c, /* ldr r0, startaddr */
48 0xe3a01001, /* mov r1, #1 */
49 0xe5821000, /* str r1, [r2] */
51 0xe5901000, /* ldr r1, [r0] */
52 0xe1110001, /* tst r1, r1 */
53 0x0afffffb, /* beq <wfi> */
54 0xe12fff11, /* bx r1 */
55 0, /* gic_cpu_if: base address of GIC CPU interface */
56 0 /* bootreg: Boot register address is held here */
59 static void default_write_secondary(CPUState
*env
,
60 const struct arm_boot_info
*info
)
63 smpboot
[ARRAY_SIZE(smpboot
) - 1] = info
->smp_bootreg_addr
;
64 smpboot
[ARRAY_SIZE(smpboot
) - 2] = info
->gic_cpu_if_addr
;
65 for (n
= 0; n
< ARRAY_SIZE(smpboot
); n
++) {
66 smpboot
[n
] = tswap32(smpboot
[n
]);
68 rom_add_blob_fixed("smpboot", smpboot
, sizeof(smpboot
),
69 info
->smp_loader_start
);
72 static void default_reset_secondary(CPUState
*env
,
73 const struct arm_boot_info
*info
)
75 stl_phys_notdirty(info
->smp_bootreg_addr
, 0);
76 env
->regs
[15] = info
->smp_loader_start
;
79 #define WRITE_WORD(p, value) do { \
80 stl_phys_notdirty(p, value); \
84 static void set_kernel_args(const struct arm_boot_info
*info
)
86 int initrd_size
= info
->initrd_size
;
87 target_phys_addr_t base
= info
->loader_start
;
90 p
= base
+ KERNEL_ARGS_ADDR
;
93 WRITE_WORD(p
, 0x54410001);
95 WRITE_WORD(p
, 0x1000);
98 /* TODO: handle multiple chips on one ATAG list */
100 WRITE_WORD(p
, 0x54410002);
101 WRITE_WORD(p
, info
->ram_size
);
102 WRITE_WORD(p
, info
->loader_start
);
106 WRITE_WORD(p
, 0x54420005);
107 WRITE_WORD(p
, info
->loader_start
+ INITRD_LOAD_ADDR
);
108 WRITE_WORD(p
, initrd_size
);
110 if (info
->kernel_cmdline
&& *info
->kernel_cmdline
) {
114 cmdline_size
= strlen(info
->kernel_cmdline
);
115 cpu_physical_memory_write(p
+ 8, (void *)info
->kernel_cmdline
,
117 cmdline_size
= (cmdline_size
>> 2) + 1;
118 WRITE_WORD(p
, cmdline_size
+ 2);
119 WRITE_WORD(p
, 0x54410009);
120 p
+= cmdline_size
* 4;
122 if (info
->atag_board
) {
125 uint8_t atag_board_buf
[0x1000];
127 atag_board_len
= (info
->atag_board(info
, atag_board_buf
) + 3) & ~3;
128 WRITE_WORD(p
, (atag_board_len
+ 8) >> 2);
129 WRITE_WORD(p
, 0x414f4d50);
130 cpu_physical_memory_write(p
, atag_board_buf
, atag_board_len
);
138 static void set_kernel_args_old(const struct arm_boot_info
*info
)
140 target_phys_addr_t p
;
142 int initrd_size
= info
->initrd_size
;
143 target_phys_addr_t base
= info
->loader_start
;
145 /* see linux/include/asm-arm/setup.h */
146 p
= base
+ KERNEL_ARGS_ADDR
;
150 WRITE_WORD(p
, info
->ram_size
/ 4096);
153 #define FLAG_READONLY 1
154 #define FLAG_RDLOAD 4
155 #define FLAG_RDPROMPT 8
157 WRITE_WORD(p
, FLAG_READONLY
| FLAG_RDLOAD
| FLAG_RDPROMPT
);
159 WRITE_WORD(p
, (31 << 8) | 0); /* /dev/mtdblock0 */
168 /* memc_control_reg */
170 /* unsigned char sounddefault */
171 /* unsigned char adfsdrives */
172 /* unsigned char bytes_per_char_h */
173 /* unsigned char bytes_per_char_v */
175 /* pages_in_bank[4] */
184 WRITE_WORD(p
, info
->loader_start
+ INITRD_LOAD_ADDR
);
188 WRITE_WORD(p
, initrd_size
);
193 /* system_serial_low */
195 /* system_serial_high */
199 /* zero unused fields */
200 while (p
< base
+ KERNEL_ARGS_ADDR
+ 256 + 1024) {
203 s
= info
->kernel_cmdline
;
205 cpu_physical_memory_write(p
, (void *)s
, strlen(s
) + 1);
211 static void do_cpu_reset(void *opaque
)
213 CPUState
*env
= opaque
;
214 const struct arm_boot_info
*info
= env
->boot_info
;
218 if (!info
->is_linux
) {
219 /* Jump to the entry point. */
220 env
->regs
[15] = info
->entry
& 0xfffffffe;
221 env
->thumb
= info
->entry
& 1;
223 if (env
== first_cpu
) {
224 env
->regs
[15] = info
->loader_start
;
226 set_kernel_args_old(info
);
228 set_kernel_args(info
);
231 info
->secondary_cpu_reset_hook(env
, info
);
237 void arm_load_kernel(CPUState
*env
, struct arm_boot_info
*info
)
244 target_phys_addr_t entry
;
247 /* Load the kernel. */
248 if (!info
->kernel_filename
) {
249 fprintf(stderr
, "Kernel image must be specified\n");
253 if (!info
->secondary_cpu_reset_hook
) {
254 info
->secondary_cpu_reset_hook
= default_reset_secondary
;
256 if (!info
->write_secondary_boot
) {
257 info
->write_secondary_boot
= default_write_secondary
;
260 if (info
->nb_cpus
== 0)
263 #ifdef TARGET_WORDS_BIGENDIAN
269 /* Assume that raw images are linux kernels, and ELF images are not. */
270 kernel_size
= load_elf(info
->kernel_filename
, NULL
, NULL
, &elf_entry
,
271 NULL
, NULL
, big_endian
, ELF_MACHINE
, 1);
273 if (kernel_size
< 0) {
274 kernel_size
= load_uimage(info
->kernel_filename
, &entry
, NULL
,
277 if (kernel_size
< 0) {
278 entry
= info
->loader_start
+ KERNEL_LOAD_ADDR
;
279 kernel_size
= load_image_targphys(info
->kernel_filename
, entry
,
280 ram_size
- KERNEL_LOAD_ADDR
);
283 if (kernel_size
< 0) {
284 fprintf(stderr
, "qemu: could not load kernel '%s'\n",
285 info
->kernel_filename
);
290 if (info
->initrd_filename
) {
291 initrd_size
= load_image_targphys(info
->initrd_filename
,
294 ram_size
- INITRD_LOAD_ADDR
);
295 if (initrd_size
< 0) {
296 fprintf(stderr
, "qemu: could not load initrd '%s'\n",
297 info
->initrd_filename
);
303 bootloader
[4] = info
->board_id
;
304 bootloader
[5] = info
->loader_start
+ KERNEL_ARGS_ADDR
;
305 bootloader
[6] = entry
;
306 for (n
= 0; n
< sizeof(bootloader
) / 4; n
++) {
307 bootloader
[n
] = tswap32(bootloader
[n
]);
309 rom_add_blob_fixed("bootloader", bootloader
, sizeof(bootloader
),
311 if (info
->nb_cpus
> 1) {
312 info
->write_secondary_boot(env
, info
);
314 info
->initrd_size
= initrd_size
;
316 info
->is_linux
= is_linux
;
318 for (; env
; env
= env
->next_cpu
) {
319 env
->boot_info
= info
;
320 qemu_register_reset(do_cpu_reset
, env
);