]> Git Repo - qemu.git/blame - hw/microblaze/boot.c
spapr_pci: Allow VFIO devices to work on the normal PCI host bridge
[qemu.git] / hw / microblaze / boot.c
CommitLineData
d94e7434
PC
1/*
2 * Microblaze kernel loader
3 *
4 * Copyright (c) 2012 Peter Crosthwaite <[email protected]>
5 * Copyright (c) 2012 PetaLogix
6 * Copyright (c) 2009 Edgar E. Iglesias.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
1de7afc9
PB
27#include "qemu/option.h"
28#include "qemu/config-file.h"
ec426ff8 29#include "qemu/error-report.h"
d94e7434 30#include "qemu-common.h"
9c17d615 31#include "sysemu/device_tree.h"
7bccd940 32#include "sysemu/sysemu.h"
83c9f4ca 33#include "hw/loader.h"
d94e7434
PC
34#include "elf.h"
35
47b43a1f 36#include "boot.h"
d94e7434
PC
37
38static struct
39{
bf494367 40 void (*machine_cpu_reset)(MicroBlazeCPU *);
d94e7434
PC
41 uint32_t bootstrap_pc;
42 uint32_t cmdline;
ec426ff8
EI
43 uint32_t initrd_start;
44 uint32_t initrd_end;
d94e7434
PC
45 uint32_t fdt;
46} boot_info;
47
48static void main_cpu_reset(void *opaque)
49{
bf494367 50 MicroBlazeCPU *cpu = opaque;
691b9572 51 CPUState *cs = CPU(cpu);
bf494367 52 CPUMBState *env = &cpu->env;
d94e7434 53
691b9572 54 cpu_reset(cs);
d94e7434 55 env->regs[5] = boot_info.cmdline;
ec426ff8 56 env->regs[6] = boot_info.initrd_start;
d94e7434 57 env->regs[7] = boot_info.fdt;
691b9572 58 cpu_set_pc(cs, boot_info.bootstrap_pc);
d94e7434 59 if (boot_info.machine_cpu_reset) {
bf494367 60 boot_info.machine_cpu_reset(cpu);
d94e7434
PC
61 }
62}
63
a8170e5e 64static int microblaze_load_dtb(hwaddr addr,
d0b022a0 65 uint32_t ramsize,
ec426ff8
EI
66 uint32_t initrd_start,
67 uint32_t initrd_end,
d0b022a0
EI
68 const char *kernel_cmdline,
69 const char *dtb_filename)
d94e7434 70{
d94e7434 71 int fdt_size;
da71ebd1 72 void *fdt = NULL;
d94e7434
PC
73 int r;
74
da71ebd1
PC
75 if (dtb_filename) {
76 fdt = load_device_tree(dtb_filename, &fdt_size);
77 }
d94e7434 78 if (!fdt) {
da71ebd1 79 return 0;
d94e7434
PC
80 }
81
82 if (kernel_cmdline) {
5a4348d1
PC
83 r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
84 kernel_cmdline);
d94e7434
PC
85 if (r < 0) {
86 fprintf(stderr, "couldn't set /chosen/bootargs\n");
87 }
88 }
89
ec426ff8 90 if (initrd_start) {
5a4348d1
PC
91 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
92 initrd_start);
ec426ff8 93
5a4348d1
PC
94 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
95 initrd_end);
ec426ff8
EI
96 }
97
e1fe50dc 98 cpu_physical_memory_write(addr, fdt, fdt_size);
d94e7434
PC
99 return fdt_size;
100}
101
102static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
103{
104 return addr - 0x30000000LL;
105}
106
a8170e5e 107void microblaze_load_kernel(MicroBlazeCPU *cpu, hwaddr ddr_base,
ec426ff8
EI
108 uint32_t ramsize,
109 const char *initrd_filename,
110 const char *dtb_filename,
bf494367 111 void (*machine_cpu_reset)(MicroBlazeCPU *))
d94e7434 112{
d94e7434 113 QemuOpts *machine_opts;
7bccd940
MA
114 const char *kernel_filename;
115 const char *kernel_cmdline;
116 const char *dtb_arg;
4d850406 117 char *filename = NULL;
7bccd940
MA
118
119 machine_opts = qemu_get_machine_opts();
120 kernel_filename = qemu_opt_get(machine_opts, "kernel");
121 kernel_cmdline = qemu_opt_get(machine_opts, "append");
122 dtb_arg = qemu_opt_get(machine_opts, "dtb");
4d850406
GA
123 /* default to pcbios dtb as passed by machine_init */
124 if (!dtb_arg) {
125 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_filename);
d94e7434
PC
126 }
127
128 boot_info.machine_cpu_reset = machine_cpu_reset;
bf494367 129 qemu_register_reset(main_cpu_reset, cpu);
d94e7434
PC
130
131 if (kernel_filename) {
132 int kernel_size;
133 uint64_t entry, low, high;
134 uint32_t base32;
135 int big_endian = 0;
136
137#ifdef TARGET_WORDS_BIGENDIAN
138 big_endian = 1;
139#endif
140
141 /* Boots a kernel elf binary. */
142 kernel_size = load_elf(kernel_filename, NULL, NULL,
143 &entry, &low, &high,
f4fc2bbf 144 big_endian, EM_MICROBLAZE, 0);
d94e7434
PC
145 base32 = entry;
146 if (base32 == 0xc0000000) {
147 kernel_size = load_elf(kernel_filename, translate_kernel_address,
148 NULL, &entry, NULL, NULL,
f4fc2bbf 149 big_endian, EM_MICROBLAZE, 0);
d94e7434
PC
150 }
151 /* Always boot into physical ram. */
e5bfd640 152 boot_info.bootstrap_pc = (uint32_t)entry;
d94e7434
PC
153
154 /* If it wasn't an ELF image, try an u-boot image. */
155 if (kernel_size < 0) {
a8170e5e 156 hwaddr uentry, loadaddr;
d94e7434 157
25bda50a
MF
158 kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0,
159 NULL, NULL);
d94e7434
PC
160 boot_info.bootstrap_pc = uentry;
161 high = (loadaddr + kernel_size + 3) & ~3;
162 }
163
164 /* Not an ELF image nor an u-boot image, try a RAW image. */
165 if (kernel_size < 0) {
166 kernel_size = load_image_targphys(kernel_filename, ddr_base,
167 ram_size);
168 boot_info.bootstrap_pc = ddr_base;
169 high = (ddr_base + kernel_size + 3) & ~3;
170 }
171
ec426ff8
EI
172 if (initrd_filename) {
173 int initrd_size;
174 uint32_t initrd_offset;
175
176 high = ROUND_UP(high + kernel_size, 4);
177 boot_info.initrd_start = high;
178 initrd_offset = boot_info.initrd_start - ddr_base;
1b939d92
EI
179
180 initrd_size = load_ramdisk(initrd_filename,
181 boot_info.initrd_start,
182 ram_size - initrd_offset);
183 if (initrd_size < 0) {
184 initrd_size = load_image_targphys(initrd_filename,
185 boot_info.initrd_start,
186 ram_size - initrd_offset);
187 }
ec426ff8 188 if (initrd_size < 0) {
81b07353 189 error_report("qemu: could not load initrd '%s'",
ec426ff8
EI
190 initrd_filename);
191 exit(EXIT_FAILURE);
192 }
193 boot_info.initrd_end = boot_info.initrd_start + initrd_size;
194 high = ROUND_UP(high + initrd_size, 4);
195 }
196
d94e7434
PC
197 boot_info.cmdline = high + 4096;
198 if (kernel_cmdline && strlen(kernel_cmdline)) {
199 pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);
200 }
201 /* Provide a device-tree. */
202 boot_info.fdt = boot_info.cmdline + 4096;
d0b022a0 203 microblaze_load_dtb(boot_info.fdt, ram_size,
ec426ff8
EI
204 boot_info.initrd_start,
205 boot_info.initrd_end,
d0b022a0 206 kernel_cmdline,
4d850406
GA
207 /* Preference a -dtb argument */
208 dtb_arg ? dtb_arg : filename);
d94e7434 209 }
4d850406 210 g_free(filename);
d94e7434 211}
This page took 0.266667 seconds and 4 git commands to generate.