]> git.proxmox.com Git - mirror_qemu.git/blame - hw/arm_boot.c
hw/vexpress.c: Instantiate the motherboard CLCD
[mirror_qemu.git] / hw / arm_boot.c
CommitLineData
5fafdf24 1/*
16406950
PB
2 * ARM kernel loader.
3 *
9ee6e8bb 4 * Copyright (c) 2006-2007 CodeSourcery.
16406950
PB
5 * Written by Paul Brook
6 *
8e31bf38 7 * This code is licensed under the GPL.
16406950
PB
8 */
9
87ecb68b
PB
10#include "hw.h"
11#include "arm-misc.h"
12#include "sysemu.h"
ca20cf32
BS
13#include "loader.h"
14#include "elf.h"
16406950
PB
15
16#define KERNEL_ARGS_ADDR 0x100
17#define KERNEL_LOAD_ADDR 0x00010000
756ba3b0 18#define INITRD_LOAD_ADDR 0x00d00000
16406950
PB
19
20/* The worlds second smallest bootloader. Set r0-r2, then jump to kernel. */
21static uint32_t bootloader[] = {
22 0xe3a00000, /* mov r0, #0 */
f8414cb5
PM
23 0xe59f1004, /* ldr r1, [pc, #4] */
24 0xe59f2004, /* ldr r2, [pc, #4] */
25 0xe59ff004, /* ldr pc, [pc, #4] */
26 0, /* Board ID */
16406950
PB
27 0, /* Address of kernel args. Set by integratorcp_init. */
28 0 /* Kernel entry point. Set by integratorcp_init. */
29};
30
9d5ba9bf
ML
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
38 * instead.
39 *
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.
44 */
9ee6e8bb 45static uint32_t smpboot[] = {
078758d0
EV
46 0xe59f201c, /* ldr r2, privbase */
47 0xe59f001c, /* ldr r0, startaddr */
48 0xe3a01001, /* mov r1, #1 */
49 0xe5821100, /* str r1, [r2, #256] */
9ee6e8bb
PB
50 0xe320f003, /* wfi */
51 0xe5901000, /* ldr r1, [r0] */
be0f204a
PB
52 0xe1110001, /* tst r1, r1 */
53 0x0afffffb, /* beq <wfi> */
f7c70325 54 0xe12fff11, /* bx r1 */
078758d0
EV
55 0, /* privbase: Private memory region base address. */
56 0 /* bootreg: Boot register address is held here */
9ee6e8bb
PB
57};
58
9d5ba9bf
ML
59static void default_write_secondary(CPUState *env,
60 const struct arm_boot_info *info)
61{
62 int n;
63 smpboot[ARRAY_SIZE(smpboot) - 1] = info->smp_bootreg_addr;
64 smpboot[ARRAY_SIZE(smpboot) - 2] = info->smp_priv_base;
65 for (n = 0; n < ARRAY_SIZE(smpboot); n++) {
66 smpboot[n] = tswap32(smpboot[n]);
67 }
68 rom_add_blob_fixed("smpboot", smpboot, sizeof(smpboot),
69 info->smp_loader_start);
70}
71
72static void default_reset_secondary(CPUState *env,
73 const struct arm_boot_info *info)
74{
75 stl_phys_notdirty(info->smp_bootreg_addr, 0);
76 env->regs[15] = info->smp_loader_start;
77}
78
52b43737
PB
79#define WRITE_WORD(p, value) do { \
80 stl_phys_notdirty(p, value); \
81 p += 4; \
82} while (0)
83
761c9eb0 84static void set_kernel_args(const struct arm_boot_info *info)
16406950 85{
761c9eb0
SW
86 int initrd_size = info->initrd_size;
87 target_phys_addr_t base = info->loader_start;
c227f099 88 target_phys_addr_t p;
16406950 89
52b43737 90 p = base + KERNEL_ARGS_ADDR;
16406950 91 /* ATAG_CORE */
52b43737
PB
92 WRITE_WORD(p, 5);
93 WRITE_WORD(p, 0x54410001);
94 WRITE_WORD(p, 1);
95 WRITE_WORD(p, 0x1000);
96 WRITE_WORD(p, 0);
16406950 97 /* ATAG_MEM */
f93eb9ff 98 /* TODO: handle multiple chips on one ATAG list */
52b43737
PB
99 WRITE_WORD(p, 4);
100 WRITE_WORD(p, 0x54410002);
101 WRITE_WORD(p, info->ram_size);
102 WRITE_WORD(p, info->loader_start);
16406950
PB
103 if (initrd_size) {
104 /* ATAG_INITRD2 */
52b43737
PB
105 WRITE_WORD(p, 4);
106 WRITE_WORD(p, 0x54420005);
107 WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR);
108 WRITE_WORD(p, initrd_size);
16406950 109 }
f93eb9ff 110 if (info->kernel_cmdline && *info->kernel_cmdline) {
16406950
PB
111 /* ATAG_CMDLINE */
112 int cmdline_size;
113
f93eb9ff 114 cmdline_size = strlen(info->kernel_cmdline);
52b43737
PB
115 cpu_physical_memory_write(p + 8, (void *)info->kernel_cmdline,
116 cmdline_size + 1);
16406950 117 cmdline_size = (cmdline_size >> 2) + 1;
52b43737
PB
118 WRITE_WORD(p, cmdline_size + 2);
119 WRITE_WORD(p, 0x54410009);
120 p += cmdline_size * 4;
16406950 121 }
f93eb9ff
AZ
122 if (info->atag_board) {
123 /* ATAG_BOARD */
124 int atag_board_len;
52b43737 125 uint8_t atag_board_buf[0x1000];
f93eb9ff 126
52b43737
PB
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);
f93eb9ff
AZ
131 p += atag_board_len;
132 }
16406950 133 /* ATAG_END */
52b43737
PB
134 WRITE_WORD(p, 0);
135 WRITE_WORD(p, 0);
16406950
PB
136}
137
761c9eb0 138static void set_kernel_args_old(const struct arm_boot_info *info)
2b8f2d41 139{
c227f099 140 target_phys_addr_t p;
52b43737 141 const char *s;
761c9eb0
SW
142 int initrd_size = info->initrd_size;
143 target_phys_addr_t base = info->loader_start;
2b8f2d41
AZ
144
145 /* see linux/include/asm-arm/setup.h */
52b43737 146 p = base + KERNEL_ARGS_ADDR;
2b8f2d41 147 /* page_size */
52b43737 148 WRITE_WORD(p, 4096);
2b8f2d41 149 /* nr_pages */
52b43737 150 WRITE_WORD(p, info->ram_size / 4096);
2b8f2d41 151 /* ramdisk_size */
52b43737 152 WRITE_WORD(p, 0);
2b8f2d41
AZ
153#define FLAG_READONLY 1
154#define FLAG_RDLOAD 4
155#define FLAG_RDPROMPT 8
156 /* flags */
52b43737 157 WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
2b8f2d41 158 /* rootdev */
52b43737 159 WRITE_WORD(p, (31 << 8) | 0); /* /dev/mtdblock0 */
2b8f2d41 160 /* video_num_cols */
52b43737 161 WRITE_WORD(p, 0);
2b8f2d41 162 /* video_num_rows */
52b43737 163 WRITE_WORD(p, 0);
2b8f2d41 164 /* video_x */
52b43737 165 WRITE_WORD(p, 0);
2b8f2d41 166 /* video_y */
52b43737 167 WRITE_WORD(p, 0);
2b8f2d41 168 /* memc_control_reg */
52b43737 169 WRITE_WORD(p, 0);
2b8f2d41
AZ
170 /* unsigned char sounddefault */
171 /* unsigned char adfsdrives */
172 /* unsigned char bytes_per_char_h */
173 /* unsigned char bytes_per_char_v */
52b43737 174 WRITE_WORD(p, 0);
2b8f2d41 175 /* pages_in_bank[4] */
52b43737
PB
176 WRITE_WORD(p, 0);
177 WRITE_WORD(p, 0);
178 WRITE_WORD(p, 0);
179 WRITE_WORD(p, 0);
2b8f2d41 180 /* pages_in_vram */
52b43737 181 WRITE_WORD(p, 0);
2b8f2d41
AZ
182 /* initrd_start */
183 if (initrd_size)
52b43737 184 WRITE_WORD(p, info->loader_start + INITRD_LOAD_ADDR);
2b8f2d41 185 else
52b43737 186 WRITE_WORD(p, 0);
2b8f2d41 187 /* initrd_size */
52b43737 188 WRITE_WORD(p, initrd_size);
2b8f2d41 189 /* rd_start */
52b43737 190 WRITE_WORD(p, 0);
2b8f2d41 191 /* system_rev */
52b43737 192 WRITE_WORD(p, 0);
2b8f2d41 193 /* system_serial_low */
52b43737 194 WRITE_WORD(p, 0);
2b8f2d41 195 /* system_serial_high */
52b43737 196 WRITE_WORD(p, 0);
2b8f2d41 197 /* mem_fclk_21285 */
52b43737 198 WRITE_WORD(p, 0);
2b8f2d41 199 /* zero unused fields */
52b43737
PB
200 while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
201 WRITE_WORD(p, 0);
202 }
203 s = info->kernel_cmdline;
204 if (s) {
205 cpu_physical_memory_write(p, (void *)s, strlen(s) + 1);
206 } else {
207 WRITE_WORD(p, 0);
208 }
2b8f2d41
AZ
209}
210
6ed221b6 211static void do_cpu_reset(void *opaque)
f2d74978
PB
212{
213 CPUState *env = opaque;
462a8bc6 214 const struct arm_boot_info *info = env->boot_info;
f2d74978
PB
215
216 cpu_reset(env);
217 if (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;
222 } else {
6ed221b6
AL
223 if (env == first_cpu) {
224 env->regs[15] = info->loader_start;
225 if (old_param) {
761c9eb0 226 set_kernel_args_old(info);
6ed221b6 227 } else {
761c9eb0 228 set_kernel_args(info);
6ed221b6 229 }
f2d74978 230 } else {
9d5ba9bf 231 info->secondary_cpu_reset_hook(env, info);
f2d74978
PB
232 }
233 }
234 }
f2d74978
PB
235}
236
f93eb9ff 237void arm_load_kernel(CPUState *env, struct arm_boot_info *info)
16406950
PB
238{
239 int kernel_size;
240 int initrd_size;
241 int n;
1c7b3754
PB
242 int is_linux = 0;
243 uint64_t elf_entry;
c227f099 244 target_phys_addr_t entry;
ca20cf32 245 int big_endian;
16406950
PB
246
247 /* Load the kernel. */
f93eb9ff 248 if (!info->kernel_filename) {
16406950
PB
249 fprintf(stderr, "Kernel image must be specified\n");
250 exit(1);
251 }
daf90626 252
9d5ba9bf
ML
253 if (!info->secondary_cpu_reset_hook) {
254 info->secondary_cpu_reset_hook = default_reset_secondary;
255 }
256 if (!info->write_secondary_boot) {
257 info->write_secondary_boot = default_write_secondary;
258 }
259
f2d74978
PB
260 if (info->nb_cpus == 0)
261 info->nb_cpus = 1;
f93eb9ff 262
ca20cf32
BS
263#ifdef TARGET_WORDS_BIGENDIAN
264 big_endian = 1;
265#else
266 big_endian = 0;
267#endif
268
1c7b3754 269 /* Assume that raw images are linux kernels, and ELF images are not. */
409dbce5
AJ
270 kernel_size = load_elf(info->kernel_filename, NULL, NULL, &elf_entry,
271 NULL, NULL, big_endian, ELF_MACHINE, 1);
1c7b3754
PB
272 entry = elf_entry;
273 if (kernel_size < 0) {
5a9154e0
AL
274 kernel_size = load_uimage(info->kernel_filename, &entry, NULL,
275 &is_linux);
1c7b3754
PB
276 }
277 if (kernel_size < 0) {
f93eb9ff 278 entry = info->loader_start + KERNEL_LOAD_ADDR;
3b760e04
PB
279 kernel_size = load_image_targphys(info->kernel_filename, entry,
280 ram_size - KERNEL_LOAD_ADDR);
1c7b3754
PB
281 is_linux = 1;
282 }
283 if (kernel_size < 0) {
f93eb9ff
AZ
284 fprintf(stderr, "qemu: could not load kernel '%s'\n",
285 info->kernel_filename);
1c7b3754
PB
286 exit(1);
287 }
f2d74978
PB
288 info->entry = entry;
289 if (is_linux) {
f93eb9ff 290 if (info->initrd_filename) {
3b760e04
PB
291 initrd_size = load_image_targphys(info->initrd_filename,
292 info->loader_start
293 + INITRD_LOAD_ADDR,
294 ram_size - INITRD_LOAD_ADDR);
daf90626
PB
295 if (initrd_size < 0) {
296 fprintf(stderr, "qemu: could not load initrd '%s'\n",
f93eb9ff 297 info->initrd_filename);
daf90626
PB
298 exit(1);
299 }
300 } else {
301 initrd_size = 0;
302 }
f8414cb5 303 bootloader[4] = info->board_id;
f93eb9ff 304 bootloader[5] = info->loader_start + KERNEL_ARGS_ADDR;
1c7b3754 305 bootloader[6] = entry;
52b43737 306 for (n = 0; n < sizeof(bootloader) / 4; n++) {
f2d74978 307 bootloader[n] = tswap32(bootloader[n]);
52b43737 308 }
f2d74978
PB
309 rom_add_blob_fixed("bootloader", bootloader, sizeof(bootloader),
310 info->loader_start);
52b43737 311 if (info->nb_cpus > 1) {
9d5ba9bf 312 info->write_secondary_boot(env, info);
52b43737 313 }
f2d74978 314 info->initrd_size = initrd_size;
16406950 315 }
f2d74978 316 info->is_linux = is_linux;
6ed221b6
AL
317
318 for (; env; env = env->next_cpu) {
319 env->boot_info = info;
320 qemu_register_reset(do_cpu_reset, env);
321 }
16406950 322}