hw/isa/Kconfig: Add missing dependency VIA VT82C686 -> APM
[qemu/ar7.git] / hw / nios2 / boot.c
blobd9969ac14828673573265a05261553256ce25fc9
1 /*
2 * Nios2 kernel loader
4 * Copyright (c) 2016 Marek Vasut <marek.vasut@gmail.com>
6 * Based on microblaze kernel loader
8 * Copyright (c) 2012 Peter Crosthwaite <peter.crosthwaite@petalogix.com>
9 * Copyright (c) 2012 PetaLogix
10 * Copyright (c) 2009 Edgar E. Iglesias.
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 * THE SOFTWARE.
31 #include "qemu/osdep.h"
32 #include "qemu/units.h"
33 #include "qemu-common.h"
34 #include "qemu/datadir.h"
35 #include "cpu.h"
36 #include "qemu/option.h"
37 #include "qemu/config-file.h"
38 #include "qemu/error-report.h"
39 #include "sysemu/device_tree.h"
40 #include "sysemu/reset.h"
41 #include "sysemu/sysemu.h"
42 #include "hw/boards.h"
43 #include "hw/loader.h"
44 #include "elf.h"
46 #include "boot.h"
48 #define NIOS2_MAGIC 0x534f494e
50 static struct nios2_boot_info {
51 void (*machine_cpu_reset)(Nios2CPU *);
52 uint32_t bootstrap_pc;
53 uint32_t cmdline;
54 uint32_t initrd_start;
55 uint32_t initrd_end;
56 uint32_t fdt;
57 } boot_info;
59 static void main_cpu_reset(void *opaque)
61 Nios2CPU *cpu = opaque;
62 CPUState *cs = CPU(cpu);
63 CPUNios2State *env = &cpu->env;
65 cpu_reset(CPU(cpu));
67 env->regs[R_ARG0] = NIOS2_MAGIC;
68 env->regs[R_ARG1] = boot_info.initrd_start;
69 env->regs[R_ARG2] = boot_info.fdt;
70 env->regs[R_ARG3] = boot_info.cmdline;
72 cpu_set_pc(cs, boot_info.bootstrap_pc);
73 if (boot_info.machine_cpu_reset) {
74 boot_info.machine_cpu_reset(cpu);
78 static uint64_t translate_kernel_address(void *opaque, uint64_t addr)
80 return addr - 0xc0000000LL;
83 static int nios2_load_dtb(struct nios2_boot_info bi, const uint32_t ramsize,
84 const char *kernel_cmdline, const char *dtb_filename)
86 int fdt_size;
87 void *fdt = NULL;
88 int r;
90 if (dtb_filename) {
91 fdt = load_device_tree(dtb_filename, &fdt_size);
93 if (!fdt) {
94 return 0;
97 if (kernel_cmdline) {
98 r = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
99 kernel_cmdline);
100 if (r < 0) {
101 fprintf(stderr, "couldn't set /chosen/bootargs\n");
105 if (bi.initrd_start) {
106 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
107 translate_kernel_address(NULL, bi.initrd_start));
109 qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
110 translate_kernel_address(NULL, bi.initrd_end));
113 cpu_physical_memory_write(bi.fdt, fdt, fdt_size);
114 g_free(fdt);
115 return fdt_size;
118 void nios2_load_kernel(Nios2CPU *cpu, hwaddr ddr_base,
119 uint32_t ramsize,
120 const char *initrd_filename,
121 const char *dtb_filename,
122 void (*machine_cpu_reset)(Nios2CPU *))
124 const char *kernel_filename;
125 const char *kernel_cmdline;
126 const char *dtb_arg;
127 char *filename = NULL;
129 kernel_filename = current_machine->kernel_filename;
130 kernel_cmdline = current_machine->kernel_cmdline;
131 dtb_arg = current_machine->dtb;
132 /* default to pcbios dtb as passed by machine_init */
133 if (!dtb_arg) {
134 filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, dtb_filename);
137 boot_info.machine_cpu_reset = machine_cpu_reset;
138 qemu_register_reset(main_cpu_reset, cpu);
140 if (kernel_filename) {
141 int kernel_size, fdt_size;
142 uint64_t entry, high;
143 int big_endian = 0;
145 #ifdef TARGET_WORDS_BIGENDIAN
146 big_endian = 1;
147 #endif
149 /* Boots a kernel elf binary. */
150 kernel_size = load_elf(kernel_filename, NULL, NULL, NULL,
151 &entry, NULL, &high, NULL,
152 big_endian, EM_ALTERA_NIOS2, 0, 0);
153 if ((uint32_t)entry == 0xc0000000) {
155 * The Nios II processor reference guide documents that the
156 * kernel is placed at virtual memory address 0xc0000000,
157 * and we've got something that points there. Reload it
158 * and adjust the entry to get the address in physical RAM.
160 kernel_size = load_elf(kernel_filename, NULL,
161 translate_kernel_address, NULL,
162 &entry, NULL, NULL, NULL,
163 big_endian, EM_ALTERA_NIOS2, 0, 0);
164 boot_info.bootstrap_pc = ddr_base + 0xc0000000 +
165 (entry & 0x07ffffff);
166 } else {
167 /* Use the entry point in the ELF image. */
168 boot_info.bootstrap_pc = (uint32_t)entry;
171 /* If it wasn't an ELF image, try an u-boot image. */
172 if (kernel_size < 0) {
173 hwaddr uentry, loadaddr = LOAD_UIMAGE_LOADADDR_INVALID;
175 kernel_size = load_uimage(kernel_filename, &uentry, &loadaddr, 0,
176 NULL, NULL);
177 boot_info.bootstrap_pc = uentry;
178 high = loadaddr + kernel_size;
181 /* Not an ELF image nor an u-boot image, try a RAW image. */
182 if (kernel_size < 0) {
183 kernel_size = load_image_targphys(kernel_filename, ddr_base,
184 ramsize);
185 boot_info.bootstrap_pc = ddr_base;
186 high = ddr_base + kernel_size;
189 high = ROUND_UP(high, 1 * MiB);
191 /* If initrd is available, it goes after the kernel, aligned to 1M. */
192 if (initrd_filename) {
193 int initrd_size;
194 uint32_t initrd_offset;
196 boot_info.initrd_start = high;
197 initrd_offset = boot_info.initrd_start - ddr_base;
199 initrd_size = load_ramdisk(initrd_filename,
200 boot_info.initrd_start,
201 ramsize - initrd_offset);
202 if (initrd_size < 0) {
203 initrd_size = load_image_targphys(initrd_filename,
204 boot_info.initrd_start,
205 ramsize - initrd_offset);
207 if (initrd_size < 0) {
208 error_report("could not load initrd '%s'",
209 initrd_filename);
210 exit(EXIT_FAILURE);
212 high += initrd_size;
214 high = ROUND_UP(high, 4);
215 boot_info.initrd_end = high;
217 /* Device tree must be placed right after initrd (if available) */
218 boot_info.fdt = high;
219 fdt_size = nios2_load_dtb(boot_info, ramsize, kernel_cmdline,
220 /* Preference a -dtb argument */
221 dtb_arg ? dtb_arg : filename);
222 high += fdt_size;
224 /* Kernel command is at the end, 4k aligned. */
225 boot_info.cmdline = ROUND_UP(high, 4 * KiB);
226 if (kernel_cmdline && strlen(kernel_cmdline)) {
227 pstrcpy_targphys("cmdline", boot_info.cmdline, 256, kernel_cmdline);
230 g_free(filename);