xref: /openbmc/qemu/hw/arm/boot.c (revision dc888dd43bea83b1ccc3d0554d5044179554a5f1)
153018216SPaolo Bonzini /*
253018216SPaolo Bonzini  * ARM kernel loader.
353018216SPaolo Bonzini  *
453018216SPaolo Bonzini  * Copyright (c) 2006-2007 CodeSourcery.
553018216SPaolo Bonzini  * Written by Paul Brook
653018216SPaolo Bonzini  *
753018216SPaolo Bonzini  * This code is licensed under the GPL.
853018216SPaolo Bonzini  */
953018216SPaolo Bonzini 
1012b16722SPeter Maydell #include "qemu/osdep.h"
112c65db5eSPaolo Bonzini #include "qemu/datadir.h"
12c0dbca36SAlistair Francis #include "qemu/error-report.h"
13da34e65cSMarkus Armbruster #include "qapi/error.h"
14b77257d7SGuenter Roeck #include <libfdt.h>
1512ec8bd5SPeter Maydell #include "hw/arm/boot.h"
16d8b1ae42SPeter Maydell #include "hw/arm/linux-boot-if.h"
17baf6b681SPeter Crosthwaite #include "sysemu/kvm.h"
1853018216SPaolo Bonzini #include "sysemu/sysemu.h"
199695200aSShannon Zhao #include "sysemu/numa.h"
202744ece8STao Xu #include "hw/boards.h"
2171e8a915SMarkus Armbruster #include "sysemu/reset.h"
2253018216SPaolo Bonzini #include "hw/loader.h"
2353018216SPaolo Bonzini #include "elf.h"
2453018216SPaolo Bonzini #include "sysemu/device_tree.h"
2553018216SPaolo Bonzini #include "qemu/config-file.h"
26922a01a0SMarkus Armbruster #include "qemu/option.h"
27ea358872SStewart Hildebrand #include "qemu/units.h"
2853018216SPaolo Bonzini 
294d9ebf75SMian M. Hamayun /* Kernel boot protocol is specified in the kernel docs
304d9ebf75SMian M. Hamayun  * Documentation/arm/Booting and Documentation/arm64/booting.txt
314d9ebf75SMian M. Hamayun  * They have different preferred image load offsets from system RAM base.
324d9ebf75SMian M. Hamayun  */
3353018216SPaolo Bonzini #define KERNEL_ARGS_ADDR   0x100
34f831f955SNick Hudson #define KERNEL_NOLOAD_ADDR 0x02000000
3553018216SPaolo Bonzini #define KERNEL_LOAD_ADDR   0x00010000
364d9ebf75SMian M. Hamayun #define KERNEL64_LOAD_ADDR 0x00080000
3753018216SPaolo Bonzini 
3868115ed5SArd Biesheuvel #define ARM64_TEXT_OFFSET_OFFSET    8
3968115ed5SArd Biesheuvel #define ARM64_MAGIC_OFFSET          56
4068115ed5SArd Biesheuvel 
41ea358872SStewart Hildebrand #define BOOTLOADER_MAX_SIZE         (4 * KiB)
42ea358872SStewart Hildebrand 
433b77f6c3SIgor Mammedov AddressSpace *arm_boot_address_space(ARMCPU *cpu,
449f43d4c3SPeter Maydell                                      const struct arm_boot_info *info)
459f43d4c3SPeter Maydell {
469f43d4c3SPeter Maydell     /* Return the address space to use for bootloader reads and writes.
479f43d4c3SPeter Maydell      * We prefer the secure address space if the CPU has it and we're
489f43d4c3SPeter Maydell      * going to boot the guest into it.
499f43d4c3SPeter Maydell      */
509f43d4c3SPeter Maydell     int asidx;
519f43d4c3SPeter Maydell     CPUState *cs = CPU(cpu);
529f43d4c3SPeter Maydell 
539f43d4c3SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_EL3) && info->secure_boot) {
549f43d4c3SPeter Maydell         asidx = ARMASIdx_S;
559f43d4c3SPeter Maydell     } else {
569f43d4c3SPeter Maydell         asidx = ARMASIdx_NS;
579f43d4c3SPeter Maydell     }
589f43d4c3SPeter Maydell 
599f43d4c3SPeter Maydell     return cpu_get_address_space(cs, asidx);
609f43d4c3SPeter Maydell }
619f43d4c3SPeter Maydell 
6247b1da81SPeter Maydell typedef enum {
6347b1da81SPeter Maydell     FIXUP_NONE = 0,     /* do nothing */
6447b1da81SPeter Maydell     FIXUP_TERMINATOR,   /* end of insns */
6547b1da81SPeter Maydell     FIXUP_BOARDID,      /* overwrite with board ID number */
6610b8ec73SPeter Crosthwaite     FIXUP_BOARD_SETUP,  /* overwrite with board specific setup code address */
67751ebc13SRicardo Perez Blanco     FIXUP_ARGPTR_LO,    /* overwrite with pointer to kernel args */
68751ebc13SRicardo Perez Blanco     FIXUP_ARGPTR_HI,    /* overwrite with pointer to kernel args (high half) */
69751ebc13SRicardo Perez Blanco     FIXUP_ENTRYPOINT_LO, /* overwrite with kernel entry point */
70751ebc13SRicardo Perez Blanco     FIXUP_ENTRYPOINT_HI, /* overwrite with kernel entry point (high half) */
7147b1da81SPeter Maydell     FIXUP_GIC_CPU_IF,   /* overwrite with GIC CPU interface address */
7247b1da81SPeter Maydell     FIXUP_BOOTREG,      /* overwrite with boot register address */
7347b1da81SPeter Maydell     FIXUP_DSB,          /* overwrite with correct DSB insn for cpu */
7447b1da81SPeter Maydell     FIXUP_MAX,
7547b1da81SPeter Maydell } FixupType;
7647b1da81SPeter Maydell 
7747b1da81SPeter Maydell typedef struct ARMInsnFixup {
7847b1da81SPeter Maydell     uint32_t insn;
7947b1da81SPeter Maydell     FixupType fixup;
8047b1da81SPeter Maydell } ARMInsnFixup;
8147b1da81SPeter Maydell 
824d9ebf75SMian M. Hamayun static const ARMInsnFixup bootloader_aarch64[] = {
834d9ebf75SMian M. Hamayun     { 0x580000c0 }, /* ldr x0, arg ; Load the lower 32-bits of DTB */
844d9ebf75SMian M. Hamayun     { 0xaa1f03e1 }, /* mov x1, xzr */
854d9ebf75SMian M. Hamayun     { 0xaa1f03e2 }, /* mov x2, xzr */
864d9ebf75SMian M. Hamayun     { 0xaa1f03e3 }, /* mov x3, xzr */
874d9ebf75SMian M. Hamayun     { 0x58000084 }, /* ldr x4, entry ; Load the lower 32-bits of kernel entry */
884d9ebf75SMian M. Hamayun     { 0xd61f0080 }, /* br x4      ; Jump to the kernel entry point */
89751ebc13SRicardo Perez Blanco     { 0, FIXUP_ARGPTR_LO }, /* arg: .word @DTB Lower 32-bits */
90751ebc13SRicardo Perez Blanco     { 0, FIXUP_ARGPTR_HI}, /* .word @DTB Higher 32-bits */
91751ebc13SRicardo Perez Blanco     { 0, FIXUP_ENTRYPOINT_LO }, /* entry: .word @Kernel Entry Lower 32-bits */
92751ebc13SRicardo Perez Blanco     { 0, FIXUP_ENTRYPOINT_HI }, /* .word @Kernel Entry Higher 32-bits */
934d9ebf75SMian M. Hamayun     { 0, FIXUP_TERMINATOR }
944d9ebf75SMian M. Hamayun };
954d9ebf75SMian M. Hamayun 
9610b8ec73SPeter Crosthwaite /* A very small bootloader: call the board-setup code (if needed),
9710b8ec73SPeter Crosthwaite  * set r0-r2, then jump to the kernel.
9810b8ec73SPeter Crosthwaite  * If we're not calling boot setup code then we don't copy across
9910b8ec73SPeter Crosthwaite  * the first BOOTLOADER_NO_BOARD_SETUP_OFFSET insns in this array.
10010b8ec73SPeter Crosthwaite  */
10110b8ec73SPeter Crosthwaite 
10247b1da81SPeter Maydell static const ARMInsnFixup bootloader[] = {
103b4850e5aSSylvain Garrigues     { 0xe28fe004 }, /* add     lr, pc, #4 */
10410b8ec73SPeter Crosthwaite     { 0xe51ff004 }, /* ldr     pc, [pc, #-4] */
10510b8ec73SPeter Crosthwaite     { 0, FIXUP_BOARD_SETUP },
10610b8ec73SPeter Crosthwaite #define BOOTLOADER_NO_BOARD_SETUP_OFFSET 3
10747b1da81SPeter Maydell     { 0xe3a00000 }, /* mov     r0, #0 */
10847b1da81SPeter Maydell     { 0xe59f1004 }, /* ldr     r1, [pc, #4] */
10947b1da81SPeter Maydell     { 0xe59f2004 }, /* ldr     r2, [pc, #4] */
11047b1da81SPeter Maydell     { 0xe59ff004 }, /* ldr     pc, [pc, #4] */
11147b1da81SPeter Maydell     { 0, FIXUP_BOARDID },
112751ebc13SRicardo Perez Blanco     { 0, FIXUP_ARGPTR_LO },
113751ebc13SRicardo Perez Blanco     { 0, FIXUP_ENTRYPOINT_LO },
11447b1da81SPeter Maydell     { 0, FIXUP_TERMINATOR }
11553018216SPaolo Bonzini };
11653018216SPaolo Bonzini 
11753018216SPaolo Bonzini /* Handling for secondary CPU boot in a multicore system.
11853018216SPaolo Bonzini  * Unlike the uniprocessor/primary CPU boot, this is platform
11953018216SPaolo Bonzini  * dependent. The default code here is based on the secondary
12053018216SPaolo Bonzini  * CPU boot protocol used on realview/vexpress boards, with
12153018216SPaolo Bonzini  * some parameterisation to increase its flexibility.
12253018216SPaolo Bonzini  * QEMU platform models for which this code is not appropriate
12353018216SPaolo Bonzini  * should override write_secondary_boot and secondary_cpu_reset_hook
12453018216SPaolo Bonzini  * instead.
12553018216SPaolo Bonzini  *
12653018216SPaolo Bonzini  * This code enables the interrupt controllers for the secondary
12753018216SPaolo Bonzini  * CPUs and then puts all the secondary CPUs into a loop waiting
12853018216SPaolo Bonzini  * for an interprocessor interrupt and polling a configurable
12953018216SPaolo Bonzini  * location for the kernel secondary CPU entry point.
13053018216SPaolo Bonzini  */
13153018216SPaolo Bonzini #define DSB_INSN 0xf57ff04f
13253018216SPaolo Bonzini #define CP15_DSB_INSN 0xee070f9a /* mcr cp15, 0, r0, c7, c10, 4 */
13353018216SPaolo Bonzini 
13447b1da81SPeter Maydell static const ARMInsnFixup smpboot[] = {
13547b1da81SPeter Maydell     { 0xe59f2028 }, /* ldr r2, gic_cpu_if */
13647b1da81SPeter Maydell     { 0xe59f0028 }, /* ldr r0, bootreg_addr */
13747b1da81SPeter Maydell     { 0xe3a01001 }, /* mov r1, #1 */
13847b1da81SPeter Maydell     { 0xe5821000 }, /* str r1, [r2] - set GICC_CTLR.Enable */
13947b1da81SPeter Maydell     { 0xe3a010ff }, /* mov r1, #0xff */
14047b1da81SPeter Maydell     { 0xe5821004 }, /* str r1, [r2, 4] - set GIC_PMR.Priority to 0xff */
14147b1da81SPeter Maydell     { 0, FIXUP_DSB },   /* dsb */
14247b1da81SPeter Maydell     { 0xe320f003 }, /* wfi */
14347b1da81SPeter Maydell     { 0xe5901000 }, /* ldr     r1, [r0] */
14447b1da81SPeter Maydell     { 0xe1110001 }, /* tst     r1, r1 */
14547b1da81SPeter Maydell     { 0x0afffffb }, /* beq     <wfi> */
14647b1da81SPeter Maydell     { 0xe12fff11 }, /* bx      r1 */
14747b1da81SPeter Maydell     { 0, FIXUP_GIC_CPU_IF }, /* gic_cpu_if: .word 0x.... */
14847b1da81SPeter Maydell     { 0, FIXUP_BOOTREG }, /* bootreg_addr: .word 0x.... */
14947b1da81SPeter Maydell     { 0, FIXUP_TERMINATOR }
15053018216SPaolo Bonzini };
15153018216SPaolo Bonzini 
15247b1da81SPeter Maydell static void write_bootloader(const char *name, hwaddr addr,
1539f43d4c3SPeter Maydell                              const ARMInsnFixup *insns, uint32_t *fixupcontext,
1549f43d4c3SPeter Maydell                              AddressSpace *as)
15547b1da81SPeter Maydell {
15647b1da81SPeter Maydell     /* Fix up the specified bootloader fragment and write it into
15747b1da81SPeter Maydell      * guest memory using rom_add_blob_fixed(). fixupcontext is
15847b1da81SPeter Maydell      * an array giving the values to write in for the fixup types
15947b1da81SPeter Maydell      * which write a value into the code array.
16047b1da81SPeter Maydell      */
16147b1da81SPeter Maydell     int i, len;
16247b1da81SPeter Maydell     uint32_t *code;
16347b1da81SPeter Maydell 
16447b1da81SPeter Maydell     len = 0;
16547b1da81SPeter Maydell     while (insns[len].fixup != FIXUP_TERMINATOR) {
16647b1da81SPeter Maydell         len++;
16747b1da81SPeter Maydell     }
16847b1da81SPeter Maydell 
16947b1da81SPeter Maydell     code = g_new0(uint32_t, len);
17047b1da81SPeter Maydell 
17147b1da81SPeter Maydell     for (i = 0; i < len; i++) {
17247b1da81SPeter Maydell         uint32_t insn = insns[i].insn;
17347b1da81SPeter Maydell         FixupType fixup = insns[i].fixup;
17447b1da81SPeter Maydell 
17547b1da81SPeter Maydell         switch (fixup) {
17647b1da81SPeter Maydell         case FIXUP_NONE:
17747b1da81SPeter Maydell             break;
17847b1da81SPeter Maydell         case FIXUP_BOARDID:
17910b8ec73SPeter Crosthwaite         case FIXUP_BOARD_SETUP:
180751ebc13SRicardo Perez Blanco         case FIXUP_ARGPTR_LO:
181751ebc13SRicardo Perez Blanco         case FIXUP_ARGPTR_HI:
182751ebc13SRicardo Perez Blanco         case FIXUP_ENTRYPOINT_LO:
183751ebc13SRicardo Perez Blanco         case FIXUP_ENTRYPOINT_HI:
18447b1da81SPeter Maydell         case FIXUP_GIC_CPU_IF:
18547b1da81SPeter Maydell         case FIXUP_BOOTREG:
18647b1da81SPeter Maydell         case FIXUP_DSB:
18747b1da81SPeter Maydell             insn = fixupcontext[fixup];
18847b1da81SPeter Maydell             break;
18947b1da81SPeter Maydell         default:
19047b1da81SPeter Maydell             abort();
19147b1da81SPeter Maydell         }
19247b1da81SPeter Maydell         code[i] = tswap32(insn);
19347b1da81SPeter Maydell     }
19447b1da81SPeter Maydell 
195ea358872SStewart Hildebrand     assert((len * sizeof(uint32_t)) < BOOTLOADER_MAX_SIZE);
196ea358872SStewart Hildebrand 
1979f43d4c3SPeter Maydell     rom_add_blob_fixed_as(name, code, len * sizeof(uint32_t), addr, as);
19847b1da81SPeter Maydell 
19947b1da81SPeter Maydell     g_free(code);
20047b1da81SPeter Maydell }
20147b1da81SPeter Maydell 
20253018216SPaolo Bonzini static void default_write_secondary(ARMCPU *cpu,
20353018216SPaolo Bonzini                                     const struct arm_boot_info *info)
20453018216SPaolo Bonzini {
20547b1da81SPeter Maydell     uint32_t fixupcontext[FIXUP_MAX];
2069f43d4c3SPeter Maydell     AddressSpace *as = arm_boot_address_space(cpu, info);
20747b1da81SPeter Maydell 
20847b1da81SPeter Maydell     fixupcontext[FIXUP_GIC_CPU_IF] = info->gic_cpu_if_addr;
20947b1da81SPeter Maydell     fixupcontext[FIXUP_BOOTREG] = info->smp_bootreg_addr;
21047b1da81SPeter Maydell     if (arm_feature(&cpu->env, ARM_FEATURE_V7)) {
21147b1da81SPeter Maydell         fixupcontext[FIXUP_DSB] = DSB_INSN;
21247b1da81SPeter Maydell     } else {
21347b1da81SPeter Maydell         fixupcontext[FIXUP_DSB] = CP15_DSB_INSN;
21453018216SPaolo Bonzini     }
21547b1da81SPeter Maydell 
21647b1da81SPeter Maydell     write_bootloader("smpboot", info->smp_loader_start,
2179f43d4c3SPeter Maydell                      smpboot, fixupcontext, as);
21853018216SPaolo Bonzini }
21953018216SPaolo Bonzini 
220716536a9SAndrew Baumann void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu,
221716536a9SAndrew Baumann                                             const struct arm_boot_info *info,
222716536a9SAndrew Baumann                                             hwaddr mvbar_addr)
223716536a9SAndrew Baumann {
2249f43d4c3SPeter Maydell     AddressSpace *as = arm_boot_address_space(cpu, info);
225716536a9SAndrew Baumann     int n;
226716536a9SAndrew Baumann     uint32_t mvbar_blob[] = {
227716536a9SAndrew Baumann         /* mvbar_addr: secure monitor vectors
228716536a9SAndrew Baumann          * Default unimplemented and unused vectors to spin. Makes it
229716536a9SAndrew Baumann          * easier to debug (as opposed to the CPU running away).
230716536a9SAndrew Baumann          */
231716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
232716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
233716536a9SAndrew Baumann         0xe1b0f00e, /* movs pc, lr ;SMC exception return */
234716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
235716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
236716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
237716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
238716536a9SAndrew Baumann         0xeafffffe, /* (spin) */
239716536a9SAndrew Baumann     };
240716536a9SAndrew Baumann     uint32_t board_setup_blob[] = {
241716536a9SAndrew Baumann         /* board setup addr */
24245c078f1SClement Deschamps         0xee110f51, /* mrc     p15, 0, r0, c1, c1, 2  ;read NSACR */
24345c078f1SClement Deschamps         0xe3800b03, /* orr     r0, #0xc00             ;set CP11, CP10 */
24445c078f1SClement Deschamps         0xee010f51, /* mcr     p15, 0, r0, c1, c1, 2  ;write NSACR */
245716536a9SAndrew Baumann         0xe3a00e00 + (mvbar_addr >> 4), /* mov r0, #mvbar_addr */
246716536a9SAndrew Baumann         0xee0c0f30, /* mcr     p15, 0, r0, c12, c0, 1 ;set MVBAR */
247716536a9SAndrew Baumann         0xee110f11, /* mrc     p15, 0, r0, c1 , c1, 0 ;read SCR */
248716536a9SAndrew Baumann         0xe3800031, /* orr     r0, #0x31              ;enable AW, FW, NS */
249716536a9SAndrew Baumann         0xee010f11, /* mcr     p15, 0, r0, c1, c1, 0  ;write SCR */
250716536a9SAndrew Baumann         0xe1a0100e, /* mov     r1, lr                 ;save LR across SMC */
251716536a9SAndrew Baumann         0xe1600070, /* smc     #0                     ;call monitor to flush SCR */
252716536a9SAndrew Baumann         0xe1a0f001, /* mov     pc, r1                 ;return */
253716536a9SAndrew Baumann     };
254716536a9SAndrew Baumann 
255716536a9SAndrew Baumann     /* check that mvbar_addr is correctly aligned and relocatable (using MOV) */
256716536a9SAndrew Baumann     assert((mvbar_addr & 0x1f) == 0 && (mvbar_addr >> 4) < 0x100);
257716536a9SAndrew Baumann 
258716536a9SAndrew Baumann     /* check that these blobs don't overlap */
259716536a9SAndrew Baumann     assert((mvbar_addr + sizeof(mvbar_blob) <= info->board_setup_addr)
260716536a9SAndrew Baumann           || (info->board_setup_addr + sizeof(board_setup_blob) <= mvbar_addr));
261716536a9SAndrew Baumann 
262716536a9SAndrew Baumann     for (n = 0; n < ARRAY_SIZE(mvbar_blob); n++) {
263716536a9SAndrew Baumann         mvbar_blob[n] = tswap32(mvbar_blob[n]);
264716536a9SAndrew Baumann     }
2659f43d4c3SPeter Maydell     rom_add_blob_fixed_as("board-setup-mvbar", mvbar_blob, sizeof(mvbar_blob),
2669f43d4c3SPeter Maydell                           mvbar_addr, as);
267716536a9SAndrew Baumann 
268716536a9SAndrew Baumann     for (n = 0; n < ARRAY_SIZE(board_setup_blob); n++) {
269716536a9SAndrew Baumann         board_setup_blob[n] = tswap32(board_setup_blob[n]);
270716536a9SAndrew Baumann     }
2719f43d4c3SPeter Maydell     rom_add_blob_fixed_as("board-setup", board_setup_blob,
2729f43d4c3SPeter Maydell                           sizeof(board_setup_blob), info->board_setup_addr, as);
273716536a9SAndrew Baumann }
274716536a9SAndrew Baumann 
27553018216SPaolo Bonzini static void default_reset_secondary(ARMCPU *cpu,
27653018216SPaolo Bonzini                                     const struct arm_boot_info *info)
27753018216SPaolo Bonzini {
2789f43d4c3SPeter Maydell     AddressSpace *as = arm_boot_address_space(cpu, info);
2794df81c6eSPeter Crosthwaite     CPUState *cs = CPU(cpu);
28053018216SPaolo Bonzini 
2819f43d4c3SPeter Maydell     address_space_stl_notdirty(as, info->smp_bootreg_addr,
28242874d3aSPeter Maydell                                0, MEMTXATTRS_UNSPECIFIED, NULL);
2834df81c6eSPeter Crosthwaite     cpu_set_pc(cs, info->smp_loader_start);
28453018216SPaolo Bonzini }
28553018216SPaolo Bonzini 
28683bfffecSPeter Maydell static inline bool have_dtb(const struct arm_boot_info *info)
28783bfffecSPeter Maydell {
28883bfffecSPeter Maydell     return info->dtb_filename || info->get_dtb;
28983bfffecSPeter Maydell }
29083bfffecSPeter Maydell 
29153018216SPaolo Bonzini #define WRITE_WORD(p, value) do { \
2929f43d4c3SPeter Maydell     address_space_stl_notdirty(as, p, value, \
29342874d3aSPeter Maydell                                MEMTXATTRS_UNSPECIFIED, NULL);  \
29453018216SPaolo Bonzini     p += 4;                       \
29553018216SPaolo Bonzini } while (0)
29653018216SPaolo Bonzini 
2979f43d4c3SPeter Maydell static void set_kernel_args(const struct arm_boot_info *info, AddressSpace *as)
29853018216SPaolo Bonzini {
29953018216SPaolo Bonzini     int initrd_size = info->initrd_size;
30053018216SPaolo Bonzini     hwaddr base = info->loader_start;
30153018216SPaolo Bonzini     hwaddr p;
30253018216SPaolo Bonzini 
30353018216SPaolo Bonzini     p = base + KERNEL_ARGS_ADDR;
30453018216SPaolo Bonzini     /* ATAG_CORE */
30553018216SPaolo Bonzini     WRITE_WORD(p, 5);
30653018216SPaolo Bonzini     WRITE_WORD(p, 0x54410001);
30753018216SPaolo Bonzini     WRITE_WORD(p, 1);
30853018216SPaolo Bonzini     WRITE_WORD(p, 0x1000);
30953018216SPaolo Bonzini     WRITE_WORD(p, 0);
31053018216SPaolo Bonzini     /* ATAG_MEM */
31153018216SPaolo Bonzini     /* TODO: handle multiple chips on one ATAG list */
31253018216SPaolo Bonzini     WRITE_WORD(p, 4);
31353018216SPaolo Bonzini     WRITE_WORD(p, 0x54410002);
31453018216SPaolo Bonzini     WRITE_WORD(p, info->ram_size);
31553018216SPaolo Bonzini     WRITE_WORD(p, info->loader_start);
31653018216SPaolo Bonzini     if (initrd_size) {
31753018216SPaolo Bonzini         /* ATAG_INITRD2 */
31853018216SPaolo Bonzini         WRITE_WORD(p, 4);
31953018216SPaolo Bonzini         WRITE_WORD(p, 0x54420005);
32053018216SPaolo Bonzini         WRITE_WORD(p, info->initrd_start);
32153018216SPaolo Bonzini         WRITE_WORD(p, initrd_size);
32253018216SPaolo Bonzini     }
32353018216SPaolo Bonzini     if (info->kernel_cmdline && *info->kernel_cmdline) {
32453018216SPaolo Bonzini         /* ATAG_CMDLINE */
32553018216SPaolo Bonzini         int cmdline_size;
32653018216SPaolo Bonzini 
32753018216SPaolo Bonzini         cmdline_size = strlen(info->kernel_cmdline);
3289f43d4c3SPeter Maydell         address_space_write(as, p + 8, MEMTXATTRS_UNSPECIFIED,
329b7cbebf2SPhilippe Mathieu-Daudé                             info->kernel_cmdline, cmdline_size + 1);
33053018216SPaolo Bonzini         cmdline_size = (cmdline_size >> 2) + 1;
33153018216SPaolo Bonzini         WRITE_WORD(p, cmdline_size + 2);
33253018216SPaolo Bonzini         WRITE_WORD(p, 0x54410009);
33353018216SPaolo Bonzini         p += cmdline_size * 4;
33453018216SPaolo Bonzini     }
33553018216SPaolo Bonzini     if (info->atag_board) {
33653018216SPaolo Bonzini         /* ATAG_BOARD */
33753018216SPaolo Bonzini         int atag_board_len;
33853018216SPaolo Bonzini         uint8_t atag_board_buf[0x1000];
33953018216SPaolo Bonzini 
34053018216SPaolo Bonzini         atag_board_len = (info->atag_board(info, atag_board_buf) + 3) & ~3;
34153018216SPaolo Bonzini         WRITE_WORD(p, (atag_board_len + 8) >> 2);
34253018216SPaolo Bonzini         WRITE_WORD(p, 0x414f4d50);
3439f43d4c3SPeter Maydell         address_space_write(as, p, MEMTXATTRS_UNSPECIFIED,
3449f43d4c3SPeter Maydell                             atag_board_buf, atag_board_len);
34553018216SPaolo Bonzini         p += atag_board_len;
34653018216SPaolo Bonzini     }
34753018216SPaolo Bonzini     /* ATAG_END */
34853018216SPaolo Bonzini     WRITE_WORD(p, 0);
34953018216SPaolo Bonzini     WRITE_WORD(p, 0);
35053018216SPaolo Bonzini }
35153018216SPaolo Bonzini 
3529f43d4c3SPeter Maydell static void set_kernel_args_old(const struct arm_boot_info *info,
3539f43d4c3SPeter Maydell                                 AddressSpace *as)
35453018216SPaolo Bonzini {
35553018216SPaolo Bonzini     hwaddr p;
35653018216SPaolo Bonzini     const char *s;
35753018216SPaolo Bonzini     int initrd_size = info->initrd_size;
35853018216SPaolo Bonzini     hwaddr base = info->loader_start;
35953018216SPaolo Bonzini 
36053018216SPaolo Bonzini     /* see linux/include/asm-arm/setup.h */
36153018216SPaolo Bonzini     p = base + KERNEL_ARGS_ADDR;
36253018216SPaolo Bonzini     /* page_size */
36353018216SPaolo Bonzini     WRITE_WORD(p, 4096);
36453018216SPaolo Bonzini     /* nr_pages */
36553018216SPaolo Bonzini     WRITE_WORD(p, info->ram_size / 4096);
36653018216SPaolo Bonzini     /* ramdisk_size */
36753018216SPaolo Bonzini     WRITE_WORD(p, 0);
36853018216SPaolo Bonzini #define FLAG_READONLY	1
36953018216SPaolo Bonzini #define FLAG_RDLOAD	4
37053018216SPaolo Bonzini #define FLAG_RDPROMPT	8
37153018216SPaolo Bonzini     /* flags */
37253018216SPaolo Bonzini     WRITE_WORD(p, FLAG_READONLY | FLAG_RDLOAD | FLAG_RDPROMPT);
37353018216SPaolo Bonzini     /* rootdev */
37453018216SPaolo Bonzini     WRITE_WORD(p, (31 << 8) | 0);	/* /dev/mtdblock0 */
37553018216SPaolo Bonzini     /* video_num_cols */
37653018216SPaolo Bonzini     WRITE_WORD(p, 0);
37753018216SPaolo Bonzini     /* video_num_rows */
37853018216SPaolo Bonzini     WRITE_WORD(p, 0);
37953018216SPaolo Bonzini     /* video_x */
38053018216SPaolo Bonzini     WRITE_WORD(p, 0);
38153018216SPaolo Bonzini     /* video_y */
38253018216SPaolo Bonzini     WRITE_WORD(p, 0);
38353018216SPaolo Bonzini     /* memc_control_reg */
38453018216SPaolo Bonzini     WRITE_WORD(p, 0);
38553018216SPaolo Bonzini     /* unsigned char sounddefault */
38653018216SPaolo Bonzini     /* unsigned char adfsdrives */
38753018216SPaolo Bonzini     /* unsigned char bytes_per_char_h */
38853018216SPaolo Bonzini     /* unsigned char bytes_per_char_v */
38953018216SPaolo Bonzini     WRITE_WORD(p, 0);
39053018216SPaolo Bonzini     /* pages_in_bank[4] */
39153018216SPaolo Bonzini     WRITE_WORD(p, 0);
39253018216SPaolo Bonzini     WRITE_WORD(p, 0);
39353018216SPaolo Bonzini     WRITE_WORD(p, 0);
39453018216SPaolo Bonzini     WRITE_WORD(p, 0);
39553018216SPaolo Bonzini     /* pages_in_vram */
39653018216SPaolo Bonzini     WRITE_WORD(p, 0);
39753018216SPaolo Bonzini     /* initrd_start */
39853018216SPaolo Bonzini     if (initrd_size) {
39953018216SPaolo Bonzini         WRITE_WORD(p, info->initrd_start);
40053018216SPaolo Bonzini     } else {
40153018216SPaolo Bonzini         WRITE_WORD(p, 0);
40253018216SPaolo Bonzini     }
40353018216SPaolo Bonzini     /* initrd_size */
40453018216SPaolo Bonzini     WRITE_WORD(p, initrd_size);
40553018216SPaolo Bonzini     /* rd_start */
40653018216SPaolo Bonzini     WRITE_WORD(p, 0);
40753018216SPaolo Bonzini     /* system_rev */
40853018216SPaolo Bonzini     WRITE_WORD(p, 0);
40953018216SPaolo Bonzini     /* system_serial_low */
41053018216SPaolo Bonzini     WRITE_WORD(p, 0);
41153018216SPaolo Bonzini     /* system_serial_high */
41253018216SPaolo Bonzini     WRITE_WORD(p, 0);
41353018216SPaolo Bonzini     /* mem_fclk_21285 */
41453018216SPaolo Bonzini     WRITE_WORD(p, 0);
41553018216SPaolo Bonzini     /* zero unused fields */
41653018216SPaolo Bonzini     while (p < base + KERNEL_ARGS_ADDR + 256 + 1024) {
41753018216SPaolo Bonzini         WRITE_WORD(p, 0);
41853018216SPaolo Bonzini     }
41953018216SPaolo Bonzini     s = info->kernel_cmdline;
42053018216SPaolo Bonzini     if (s) {
421b7cbebf2SPhilippe Mathieu-Daudé         address_space_write(as, p, MEMTXATTRS_UNSPECIFIED, s, strlen(s) + 1);
42253018216SPaolo Bonzini     } else {
42353018216SPaolo Bonzini         WRITE_WORD(p, 0);
42453018216SPaolo Bonzini     }
42553018216SPaolo Bonzini }
42653018216SPaolo Bonzini 
427f08ced69SShameer Kolothum static int fdt_add_memory_node(void *fdt, uint32_t acells, hwaddr mem_base,
428f08ced69SShameer Kolothum                                uint32_t scells, hwaddr mem_len,
429f08ced69SShameer Kolothum                                int numa_node_id)
430f08ced69SShameer Kolothum {
431f08ced69SShameer Kolothum     char *nodename;
432f08ced69SShameer Kolothum     int ret;
433f08ced69SShameer Kolothum 
434f08ced69SShameer Kolothum     nodename = g_strdup_printf("/memory@%" PRIx64, mem_base);
435f08ced69SShameer Kolothum     qemu_fdt_add_subnode(fdt, nodename);
436f08ced69SShameer Kolothum     qemu_fdt_setprop_string(fdt, nodename, "device_type", "memory");
437f08ced69SShameer Kolothum     ret = qemu_fdt_setprop_sized_cells(fdt, nodename, "reg", acells, mem_base,
438f08ced69SShameer Kolothum                                        scells, mem_len);
439f08ced69SShameer Kolothum     if (ret < 0) {
440f08ced69SShameer Kolothum         goto out;
441f08ced69SShameer Kolothum     }
442f08ced69SShameer Kolothum 
443f08ced69SShameer Kolothum     /* only set the NUMA ID if it is specified */
444f08ced69SShameer Kolothum     if (numa_node_id >= 0) {
445f08ced69SShameer Kolothum         ret = qemu_fdt_setprop_cell(fdt, nodename,
446f08ced69SShameer Kolothum                                     "numa-node-id", numa_node_id);
447f08ced69SShameer Kolothum     }
448f08ced69SShameer Kolothum out:
449f08ced69SShameer Kolothum     g_free(nodename);
450f08ced69SShameer Kolothum     return ret;
451f08ced69SShameer Kolothum }
452f08ced69SShameer Kolothum 
4534cbca7d9SAndrey Smirnov static void fdt_add_psci_node(void *fdt)
4544cbca7d9SAndrey Smirnov {
4554cbca7d9SAndrey Smirnov     uint32_t cpu_suspend_fn;
4564cbca7d9SAndrey Smirnov     uint32_t cpu_off_fn;
4574cbca7d9SAndrey Smirnov     uint32_t cpu_on_fn;
4584cbca7d9SAndrey Smirnov     uint32_t migrate_fn;
4594cbca7d9SAndrey Smirnov     ARMCPU *armcpu = ARM_CPU(qemu_get_cpu(0));
4604cbca7d9SAndrey Smirnov     const char *psci_method;
4614cbca7d9SAndrey Smirnov     int64_t psci_conduit;
462c39770cdSAndrey Smirnov     int rc;
4634cbca7d9SAndrey Smirnov 
4644cbca7d9SAndrey Smirnov     psci_conduit = object_property_get_int(OBJECT(armcpu),
4654cbca7d9SAndrey Smirnov                                            "psci-conduit",
4664cbca7d9SAndrey Smirnov                                            &error_abort);
4674cbca7d9SAndrey Smirnov     switch (psci_conduit) {
4684cbca7d9SAndrey Smirnov     case QEMU_PSCI_CONDUIT_DISABLED:
4694cbca7d9SAndrey Smirnov         return;
4704cbca7d9SAndrey Smirnov     case QEMU_PSCI_CONDUIT_HVC:
4714cbca7d9SAndrey Smirnov         psci_method = "hvc";
4724cbca7d9SAndrey Smirnov         break;
4734cbca7d9SAndrey Smirnov     case QEMU_PSCI_CONDUIT_SMC:
4744cbca7d9SAndrey Smirnov         psci_method = "smc";
4754cbca7d9SAndrey Smirnov         break;
4764cbca7d9SAndrey Smirnov     default:
4774cbca7d9SAndrey Smirnov         g_assert_not_reached();
4784cbca7d9SAndrey Smirnov     }
4794cbca7d9SAndrey Smirnov 
480c39770cdSAndrey Smirnov     /*
481c39770cdSAndrey Smirnov      * If /psci node is present in provided DTB, assume that no fixup
482c39770cdSAndrey Smirnov      * is necessary and all PSCI configuration should be taken as-is
483c39770cdSAndrey Smirnov      */
484c39770cdSAndrey Smirnov     rc = fdt_path_offset(fdt, "/psci");
485c39770cdSAndrey Smirnov     if (rc >= 0) {
486c39770cdSAndrey Smirnov         return;
487c39770cdSAndrey Smirnov     }
488c39770cdSAndrey Smirnov 
4894cbca7d9SAndrey Smirnov     qemu_fdt_add_subnode(fdt, "/psci");
4904cbca7d9SAndrey Smirnov     if (armcpu->psci_version == 2) {
4914cbca7d9SAndrey Smirnov         const char comp[] = "arm,psci-0.2\0arm,psci";
4924cbca7d9SAndrey Smirnov         qemu_fdt_setprop(fdt, "/psci", "compatible", comp, sizeof(comp));
4934cbca7d9SAndrey Smirnov 
4944cbca7d9SAndrey Smirnov         cpu_off_fn = QEMU_PSCI_0_2_FN_CPU_OFF;
4954cbca7d9SAndrey Smirnov         if (arm_feature(&armcpu->env, ARM_FEATURE_AARCH64)) {
4964cbca7d9SAndrey Smirnov             cpu_suspend_fn = QEMU_PSCI_0_2_FN64_CPU_SUSPEND;
4974cbca7d9SAndrey Smirnov             cpu_on_fn = QEMU_PSCI_0_2_FN64_CPU_ON;
4984cbca7d9SAndrey Smirnov             migrate_fn = QEMU_PSCI_0_2_FN64_MIGRATE;
4994cbca7d9SAndrey Smirnov         } else {
5004cbca7d9SAndrey Smirnov             cpu_suspend_fn = QEMU_PSCI_0_2_FN_CPU_SUSPEND;
5014cbca7d9SAndrey Smirnov             cpu_on_fn = QEMU_PSCI_0_2_FN_CPU_ON;
5024cbca7d9SAndrey Smirnov             migrate_fn = QEMU_PSCI_0_2_FN_MIGRATE;
5034cbca7d9SAndrey Smirnov         }
5044cbca7d9SAndrey Smirnov     } else {
5054cbca7d9SAndrey Smirnov         qemu_fdt_setprop_string(fdt, "/psci", "compatible", "arm,psci");
5064cbca7d9SAndrey Smirnov 
5074cbca7d9SAndrey Smirnov         cpu_suspend_fn = QEMU_PSCI_0_1_FN_CPU_SUSPEND;
5084cbca7d9SAndrey Smirnov         cpu_off_fn = QEMU_PSCI_0_1_FN_CPU_OFF;
5094cbca7d9SAndrey Smirnov         cpu_on_fn = QEMU_PSCI_0_1_FN_CPU_ON;
5104cbca7d9SAndrey Smirnov         migrate_fn = QEMU_PSCI_0_1_FN_MIGRATE;
5114cbca7d9SAndrey Smirnov     }
5124cbca7d9SAndrey Smirnov 
5134cbca7d9SAndrey Smirnov     /* We adopt the PSCI spec's nomenclature, and use 'conduit' to refer
5144cbca7d9SAndrey Smirnov      * to the instruction that should be used to invoke PSCI functions.
5154cbca7d9SAndrey Smirnov      * However, the device tree binding uses 'method' instead, so that is
5164cbca7d9SAndrey Smirnov      * what we should use here.
5174cbca7d9SAndrey Smirnov      */
5184cbca7d9SAndrey Smirnov     qemu_fdt_setprop_string(fdt, "/psci", "method", psci_method);
5194cbca7d9SAndrey Smirnov 
5204cbca7d9SAndrey Smirnov     qemu_fdt_setprop_cell(fdt, "/psci", "cpu_suspend", cpu_suspend_fn);
5214cbca7d9SAndrey Smirnov     qemu_fdt_setprop_cell(fdt, "/psci", "cpu_off", cpu_off_fn);
5224cbca7d9SAndrey Smirnov     qemu_fdt_setprop_cell(fdt, "/psci", "cpu_on", cpu_on_fn);
5234cbca7d9SAndrey Smirnov     qemu_fdt_setprop_cell(fdt, "/psci", "migrate", migrate_fn);
5244cbca7d9SAndrey Smirnov }
5254cbca7d9SAndrey Smirnov 
5263b77f6c3SIgor Mammedov int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
5272744ece8STao Xu                  hwaddr addr_limit, AddressSpace *as, MachineState *ms)
52853018216SPaolo Bonzini {
52953018216SPaolo Bonzini     void *fdt = NULL;
530e2eb3d29SEric Auger     int size, rc, n = 0;
53170976c41SPeter Maydell     uint32_t acells, scells;
5329695200aSShannon Zhao     unsigned int i;
5339695200aSShannon Zhao     hwaddr mem_base, mem_len;
534e2eb3d29SEric Auger     char **node_path;
535e2eb3d29SEric Auger     Error *err = NULL;
53653018216SPaolo Bonzini 
5370fb79851SJohn Rigby     if (binfo->dtb_filename) {
5380fb79851SJohn Rigby         char *filename;
53953018216SPaolo Bonzini         filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, binfo->dtb_filename);
54053018216SPaolo Bonzini         if (!filename) {
54153018216SPaolo Bonzini             fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
542c23045deSPeter Maydell             goto fail;
54353018216SPaolo Bonzini         }
54453018216SPaolo Bonzini 
54553018216SPaolo Bonzini         fdt = load_device_tree(filename, &size);
54653018216SPaolo Bonzini         if (!fdt) {
54753018216SPaolo Bonzini             fprintf(stderr, "Couldn't open dtb file %s\n", filename);
54853018216SPaolo Bonzini             g_free(filename);
549c23045deSPeter Maydell             goto fail;
55053018216SPaolo Bonzini         }
55153018216SPaolo Bonzini         g_free(filename);
552a554ecb4Szhanghailiang     } else {
5530fb79851SJohn Rigby         fdt = binfo->get_dtb(binfo, &size);
5540fb79851SJohn Rigby         if (!fdt) {
5550fb79851SJohn Rigby             fprintf(stderr, "Board was unable to create a dtb blob\n");
5560fb79851SJohn Rigby             goto fail;
5570fb79851SJohn Rigby         }
5580fb79851SJohn Rigby     }
55953018216SPaolo Bonzini 
560fee8ea12SArd Biesheuvel     if (addr_limit > addr && size > (addr_limit - addr)) {
561fee8ea12SArd Biesheuvel         /* Installing the device tree blob at addr would exceed addr_limit.
562fee8ea12SArd Biesheuvel          * Whether this constitutes failure is up to the caller to decide,
563fee8ea12SArd Biesheuvel          * so just return 0 as size, i.e., no error.
564fee8ea12SArd Biesheuvel          */
565fee8ea12SArd Biesheuvel         g_free(fdt);
566fee8ea12SArd Biesheuvel         return 0;
567fee8ea12SArd Biesheuvel     }
568fee8ea12SArd Biesheuvel 
56958e71097SEric Auger     acells = qemu_fdt_getprop_cell(fdt, "/", "#address-cells",
57058e71097SEric Auger                                    NULL, &error_fatal);
57158e71097SEric Auger     scells = qemu_fdt_getprop_cell(fdt, "/", "#size-cells",
57258e71097SEric Auger                                    NULL, &error_fatal);
57353018216SPaolo Bonzini     if (acells == 0 || scells == 0) {
57453018216SPaolo Bonzini         fprintf(stderr, "dtb file invalid (#address-cells or #size-cells 0)\n");
575c23045deSPeter Maydell         goto fail;
57653018216SPaolo Bonzini     }
57753018216SPaolo Bonzini 
578e4e34855SPhilippe Mathieu-Daudé     if (scells < 2 && binfo->ram_size >= 4 * GiB) {
57970976c41SPeter Maydell         /* This is user error so deserves a friendlier error message
58070976c41SPeter Maydell          * than the failure of setprop_sized_cells would provide
58170976c41SPeter Maydell          */
58253018216SPaolo Bonzini         fprintf(stderr, "qemu: dtb file not compatible with "
58353018216SPaolo Bonzini                 "RAM size > 4GB\n");
584c23045deSPeter Maydell         goto fail;
58553018216SPaolo Bonzini     }
58653018216SPaolo Bonzini 
587e2eb3d29SEric Auger     /* nop all root nodes matching /memory or /memory@unit-address */
588e2eb3d29SEric Auger     node_path = qemu_fdt_node_unit_path(fdt, "memory", &err);
589e2eb3d29SEric Auger     if (err) {
590e2eb3d29SEric Auger         error_report_err(err);
591e2eb3d29SEric Auger         goto fail;
592e2eb3d29SEric Auger     }
593e2eb3d29SEric Auger     while (node_path[n]) {
594e2eb3d29SEric Auger         if (g_str_has_prefix(node_path[n], "/memory")) {
595e2eb3d29SEric Auger             qemu_fdt_nop_node(fdt, node_path[n]);
596e2eb3d29SEric Auger         }
597e2eb3d29SEric Auger         n++;
598e2eb3d29SEric Auger     }
599e2eb3d29SEric Auger     g_strfreev(node_path);
600e2eb3d29SEric Auger 
60199abb725SGavin Shan     /*
60299abb725SGavin Shan      * We drop all the memory nodes which correspond to empty NUMA nodes
60399abb725SGavin Shan      * from the device tree, because the Linux NUMA binding document
60499abb725SGavin Shan      * states they should not be generated. Linux will get the NUMA node
60599abb725SGavin Shan      * IDs of the empty NUMA nodes from the distance map if they are needed.
60699abb725SGavin Shan      * This means QEMU users may be obliged to provide command lines which
60799abb725SGavin Shan      * configure distance maps when the empty NUMA node IDs are needed and
60899abb725SGavin Shan      * Linux's default distance map isn't sufficient.
60999abb725SGavin Shan      */
610aa570207STao Xu     if (ms->numa_state != NULL && ms->numa_state->num_nodes > 0) {
6119695200aSShannon Zhao         mem_base = binfo->loader_start;
612aa570207STao Xu         for (i = 0; i < ms->numa_state->num_nodes; i++) {
6137e721e7bSTao Xu             mem_len = ms->numa_state->nodes[i].node_mem;
61499abb725SGavin Shan             if (!mem_len) {
61599abb725SGavin Shan                 continue;
61699abb725SGavin Shan             }
61799abb725SGavin Shan 
618f08ced69SShameer Kolothum             rc = fdt_add_memory_node(fdt, acells, mem_base,
619f08ced69SShameer Kolothum                                      scells, mem_len, i);
6209695200aSShannon Zhao             if (rc < 0) {
621f08ced69SShameer Kolothum                 fprintf(stderr, "couldn't add /memory@%"PRIx64" node\n",
622f08ced69SShameer Kolothum                         mem_base);
6239695200aSShannon Zhao                 goto fail;
6249695200aSShannon Zhao             }
6259695200aSShannon Zhao 
6269695200aSShannon Zhao             mem_base += mem_len;
6279695200aSShannon Zhao         }
6289695200aSShannon Zhao     } else {
629f08ced69SShameer Kolothum         rc = fdt_add_memory_node(fdt, acells, binfo->loader_start,
630f08ced69SShameer Kolothum                                  scells, binfo->ram_size, -1);
63153018216SPaolo Bonzini         if (rc < 0) {
632f08ced69SShameer Kolothum             fprintf(stderr, "couldn't add /memory@%"PRIx64" node\n",
633f08ced69SShameer Kolothum                     binfo->loader_start);
634c23045deSPeter Maydell             goto fail;
63553018216SPaolo Bonzini         }
6369695200aSShannon Zhao     }
63753018216SPaolo Bonzini 
638b77257d7SGuenter Roeck     rc = fdt_path_offset(fdt, "/chosen");
639b77257d7SGuenter Roeck     if (rc < 0) {
640b77257d7SGuenter Roeck         qemu_fdt_add_subnode(fdt, "/chosen");
641b77257d7SGuenter Roeck     }
642b77257d7SGuenter Roeck 
6432744ece8STao Xu     if (ms->kernel_cmdline && *ms->kernel_cmdline) {
6445a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_string(fdt, "/chosen", "bootargs",
6452744ece8STao Xu                                      ms->kernel_cmdline);
64653018216SPaolo Bonzini         if (rc < 0) {
64753018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/bootargs\n");
648c23045deSPeter Maydell             goto fail;
64953018216SPaolo Bonzini         }
65053018216SPaolo Bonzini     }
65153018216SPaolo Bonzini 
65253018216SPaolo Bonzini     if (binfo->initrd_size) {
6535a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-start",
65453018216SPaolo Bonzini                                    binfo->initrd_start);
65553018216SPaolo Bonzini         if (rc < 0) {
65653018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/linux,initrd-start\n");
657c23045deSPeter Maydell             goto fail;
65853018216SPaolo Bonzini         }
65953018216SPaolo Bonzini 
6605a4348d1SPeter Crosthwaite         rc = qemu_fdt_setprop_cell(fdt, "/chosen", "linux,initrd-end",
66153018216SPaolo Bonzini                                    binfo->initrd_start + binfo->initrd_size);
66253018216SPaolo Bonzini         if (rc < 0) {
66353018216SPaolo Bonzini             fprintf(stderr, "couldn't set /chosen/linux,initrd-end\n");
664c23045deSPeter Maydell             goto fail;
66553018216SPaolo Bonzini         }
66653018216SPaolo Bonzini     }
6673b1cceb8SPeter Maydell 
6684cbca7d9SAndrey Smirnov     fdt_add_psci_node(fdt);
6694cbca7d9SAndrey Smirnov 
6703b1cceb8SPeter Maydell     if (binfo->modify_dtb) {
6713b1cceb8SPeter Maydell         binfo->modify_dtb(binfo, fdt);
6723b1cceb8SPeter Maydell     }
6733b1cceb8SPeter Maydell 
6745a4348d1SPeter Crosthwaite     qemu_fdt_dumpdtb(fdt, size);
67553018216SPaolo Bonzini 
6764c4bf654SArd Biesheuvel     /* Put the DTB into the memory map as a ROM image: this will ensure
6774c4bf654SArd Biesheuvel      * the DTB is copied again upon reset, even if addr points into RAM.
6784c4bf654SArd Biesheuvel      */
6799f43d4c3SPeter Maydell     rom_add_blob_fixed_as("dtb", fdt, size, addr, as);
68053018216SPaolo Bonzini 
681c23045deSPeter Maydell     g_free(fdt);
682c23045deSPeter Maydell 
683fee8ea12SArd Biesheuvel     return size;
684c23045deSPeter Maydell 
685c23045deSPeter Maydell fail:
686c23045deSPeter Maydell     g_free(fdt);
687c23045deSPeter Maydell     return -1;
68853018216SPaolo Bonzini }
68953018216SPaolo Bonzini 
69053018216SPaolo Bonzini static void do_cpu_reset(void *opaque)
69153018216SPaolo Bonzini {
69253018216SPaolo Bonzini     ARMCPU *cpu = opaque;
6934df81c6eSPeter Crosthwaite     CPUState *cs = CPU(cpu);
69453018216SPaolo Bonzini     CPUARMState *env = &cpu->env;
69553018216SPaolo Bonzini     const struct arm_boot_info *info = env->boot_info;
69653018216SPaolo Bonzini 
6974df81c6eSPeter Crosthwaite     cpu_reset(cs);
69853018216SPaolo Bonzini     if (info) {
69953018216SPaolo Bonzini         if (!info->is_linux) {
7009776f636SPeter Crosthwaite             int i;
70153018216SPaolo Bonzini             /* Jump to the entry point.  */
7024df81c6eSPeter Crosthwaite             uint64_t entry = info->entry;
7034df81c6eSPeter Crosthwaite 
7049776f636SPeter Crosthwaite             switch (info->endianness) {
7059776f636SPeter Crosthwaite             case ARM_ENDIANNESS_LE:
7069776f636SPeter Crosthwaite                 env->cp15.sctlr_el[1] &= ~SCTLR_E0E;
7079776f636SPeter Crosthwaite                 for (i = 1; i < 4; ++i) {
7089776f636SPeter Crosthwaite                     env->cp15.sctlr_el[i] &= ~SCTLR_EE;
7099776f636SPeter Crosthwaite                 }
7109776f636SPeter Crosthwaite                 env->uncached_cpsr &= ~CPSR_E;
7119776f636SPeter Crosthwaite                 break;
7129776f636SPeter Crosthwaite             case ARM_ENDIANNESS_BE8:
7139776f636SPeter Crosthwaite                 env->cp15.sctlr_el[1] |= SCTLR_E0E;
7149776f636SPeter Crosthwaite                 for (i = 1; i < 4; ++i) {
7159776f636SPeter Crosthwaite                     env->cp15.sctlr_el[i] |= SCTLR_EE;
7169776f636SPeter Crosthwaite                 }
7179776f636SPeter Crosthwaite                 env->uncached_cpsr |= CPSR_E;
7189776f636SPeter Crosthwaite                 break;
7199776f636SPeter Crosthwaite             case ARM_ENDIANNESS_BE32:
7209776f636SPeter Crosthwaite                 env->cp15.sctlr_el[1] |= SCTLR_B;
7219776f636SPeter Crosthwaite                 break;
7229776f636SPeter Crosthwaite             case ARM_ENDIANNESS_UNKNOWN:
7239776f636SPeter Crosthwaite                 break; /* Board's decision */
7249776f636SPeter Crosthwaite             default:
7259776f636SPeter Crosthwaite                 g_assert_not_reached();
7269776f636SPeter Crosthwaite             }
7279776f636SPeter Crosthwaite 
7284df81c6eSPeter Crosthwaite             cpu_set_pc(cs, entry);
72953018216SPaolo Bonzini         } else {
730c8e829b7SGreg Bellows             /* If we are booting Linux then we need to check whether we are
731c8e829b7SGreg Bellows              * booting into secure or non-secure state and adjust the state
732c8e829b7SGreg Bellows              * accordingly.  Out of reset, ARM is defined to be in secure state
733c8e829b7SGreg Bellows              * (SCR.NS = 0), we change that here if non-secure boot has been
734c8e829b7SGreg Bellows              * requested.
735c8e829b7SGreg Bellows              */
7365097227cSGreg Bellows             if (arm_feature(env, ARM_FEATURE_EL3)) {
7375097227cSGreg Bellows                 /* AArch64 is defined to come out of reset into EL3 if enabled.
7385097227cSGreg Bellows                  * If we are booting Linux then we need to adjust our EL as
7395097227cSGreg Bellows                  * Linux expects us to be in EL2 or EL1.  AArch32 resets into
7405097227cSGreg Bellows                  * SVC, which Linux expects, so no privilege/exception level to
7415097227cSGreg Bellows                  * adjust.
7425097227cSGreg Bellows                  */
7435097227cSGreg Bellows                 if (env->aarch64) {
74448d21a57SEdgar E. Iglesias                     env->cp15.scr_el3 |= SCR_RW;
7455097227cSGreg Bellows                     if (arm_feature(env, ARM_FEATURE_EL2)) {
74648d21a57SEdgar E. Iglesias                         env->cp15.hcr_el2 |= HCR_RW;
7475097227cSGreg Bellows                         env->pstate = PSTATE_MODE_EL2h;
7485097227cSGreg Bellows                     } else {
7495097227cSGreg Bellows                         env->pstate = PSTATE_MODE_EL1h;
7505097227cSGreg Bellows                     }
75124ac0d30SRichard Henderson                     if (cpu_isar_feature(aa64_pauth, cpu)) {
75224ac0d30SRichard Henderson                         env->cp15.scr_el3 |= SCR_API | SCR_APK;
75324ac0d30SRichard Henderson                     }
7547ad01d78SRichard Henderson                     if (cpu_isar_feature(aa64_mte, cpu)) {
7557ad01d78SRichard Henderson                         env->cp15.scr_el3 |= SCR_ATA;
7567ad01d78SRichard Henderson                     }
7573f0b5907SRémi Denis-Courmont                     if (cpu_isar_feature(aa64_sve, cpu)) {
7583f0b5907SRémi Denis-Courmont                         env->cp15.cptr_el[3] |= CPTR_EZ;
7593f0b5907SRémi Denis-Courmont                     }
76043118f43SPeter Maydell                     /* AArch64 kernels never boot in secure mode */
76143118f43SPeter Maydell                     assert(!info->secure_boot);
76243118f43SPeter Maydell                     /* This hook is only supported for AArch32 currently:
76343118f43SPeter Maydell                      * bootloader_aarch64[] will not call the hook, and
76443118f43SPeter Maydell                      * the code above has already dropped us into EL2 or EL1.
76543118f43SPeter Maydell                      */
76643118f43SPeter Maydell                     assert(!info->secure_board_setup);
7675097227cSGreg Bellows                 }
7685097227cSGreg Bellows 
769bda816f0SPeter Maydell                 if (arm_feature(env, ARM_FEATURE_EL2)) {
770bda816f0SPeter Maydell                     /* If we have EL2 then Linux expects the HVC insn to work */
771bda816f0SPeter Maydell                     env->cp15.scr_el3 |= SCR_HCE;
772bda816f0SPeter Maydell                 }
773bda816f0SPeter Maydell 
7745097227cSGreg Bellows                 /* Set to non-secure if not a secure boot */
775baf6b681SPeter Crosthwaite                 if (!info->secure_boot &&
776baf6b681SPeter Crosthwaite                     (cs != first_cpu || !info->secure_board_setup)) {
7775097227cSGreg Bellows                     /* Linux expects non-secure state */
778c8e829b7SGreg Bellows                     env->cp15.scr_el3 |= SCR_NS;
779ece628fcSPeter Maydell                     /* Set NSACR.{CP11,CP10} so NS can access the FPU */
780ece628fcSPeter Maydell                     env->cp15.nsacr |= 3 << 10;
781c8e829b7SGreg Bellows                 }
7825097227cSGreg Bellows             }
783c8e829b7SGreg Bellows 
784299953b9SPeter Maydell             if (!env->aarch64 && !info->secure_boot &&
785299953b9SPeter Maydell                 arm_feature(env, ARM_FEATURE_EL2)) {
786299953b9SPeter Maydell                 /*
787299953b9SPeter Maydell                  * This is an AArch32 boot not to Secure state, and
788299953b9SPeter Maydell                  * we have Hyp mode available, so boot the kernel into
789299953b9SPeter Maydell                  * Hyp mode. This is not how the CPU comes out of reset,
790299953b9SPeter Maydell                  * so we need to manually put it there.
791299953b9SPeter Maydell                  */
792299953b9SPeter Maydell                 cpsr_write(env, ARM_CPU_MODE_HYP, CPSR_M, CPSRWriteRaw);
793299953b9SPeter Maydell             }
794299953b9SPeter Maydell 
7954df81c6eSPeter Crosthwaite             if (cs == first_cpu) {
7969f43d4c3SPeter Maydell                 AddressSpace *as = arm_boot_address_space(cpu, info);
7979f43d4c3SPeter Maydell 
7984df81c6eSPeter Crosthwaite                 cpu_set_pc(cs, info->loader_start);
7994d9ebf75SMian M. Hamayun 
80083bfffecSPeter Maydell                 if (!have_dtb(info)) {
80153018216SPaolo Bonzini                     if (old_param) {
8029f43d4c3SPeter Maydell                         set_kernel_args_old(info, as);
80353018216SPaolo Bonzini                     } else {
8049f43d4c3SPeter Maydell                         set_kernel_args(info, as);
80553018216SPaolo Bonzini                     }
80653018216SPaolo Bonzini                 }
80753018216SPaolo Bonzini             } else {
80853018216SPaolo Bonzini                 info->secondary_cpu_reset_hook(cpu, info);
80953018216SPaolo Bonzini             }
81053018216SPaolo Bonzini         }
81198be6b7dSEdgar E. Iglesias         arm_rebuild_hflags(env);
81253018216SPaolo Bonzini     }
81353018216SPaolo Bonzini }
81453018216SPaolo Bonzini 
81507abe45cSLaszlo Ersek /**
81607abe45cSLaszlo Ersek  * load_image_to_fw_cfg() - Load an image file into an fw_cfg entry identified
81707abe45cSLaszlo Ersek  *                          by key.
81807abe45cSLaszlo Ersek  * @fw_cfg:         The firmware config instance to store the data in.
81907abe45cSLaszlo Ersek  * @size_key:       The firmware config key to store the size of the loaded
82007abe45cSLaszlo Ersek  *                  data under, with fw_cfg_add_i32().
82107abe45cSLaszlo Ersek  * @data_key:       The firmware config key to store the loaded data under,
82207abe45cSLaszlo Ersek  *                  with fw_cfg_add_bytes().
82307abe45cSLaszlo Ersek  * @image_name:     The name of the image file to load. If it is NULL, the
82407abe45cSLaszlo Ersek  *                  function returns without doing anything.
82507abe45cSLaszlo Ersek  * @try_decompress: Whether the image should be decompressed (gunzipped) before
82607abe45cSLaszlo Ersek  *                  adding it to fw_cfg. If decompression fails, the image is
82707abe45cSLaszlo Ersek  *                  loaded as-is.
82807abe45cSLaszlo Ersek  *
82907abe45cSLaszlo Ersek  * In case of failure, the function prints an error message to stderr and the
83007abe45cSLaszlo Ersek  * process exits with status 1.
83107abe45cSLaszlo Ersek  */
83207abe45cSLaszlo Ersek static void load_image_to_fw_cfg(FWCfgState *fw_cfg, uint16_t size_key,
83307abe45cSLaszlo Ersek                                  uint16_t data_key, const char *image_name,
83407abe45cSLaszlo Ersek                                  bool try_decompress)
83507abe45cSLaszlo Ersek {
83607abe45cSLaszlo Ersek     size_t size = -1;
83707abe45cSLaszlo Ersek     uint8_t *data;
83807abe45cSLaszlo Ersek 
83907abe45cSLaszlo Ersek     if (image_name == NULL) {
84007abe45cSLaszlo Ersek         return;
84107abe45cSLaszlo Ersek     }
84207abe45cSLaszlo Ersek 
84307abe45cSLaszlo Ersek     if (try_decompress) {
84407abe45cSLaszlo Ersek         size = load_image_gzipped_buffer(image_name,
84507abe45cSLaszlo Ersek                                          LOAD_IMAGE_MAX_GUNZIP_BYTES, &data);
84607abe45cSLaszlo Ersek     }
84707abe45cSLaszlo Ersek 
84807abe45cSLaszlo Ersek     if (size == (size_t)-1) {
84907abe45cSLaszlo Ersek         gchar *contents;
85007abe45cSLaszlo Ersek         gsize length;
85107abe45cSLaszlo Ersek 
85207abe45cSLaszlo Ersek         if (!g_file_get_contents(image_name, &contents, &length, NULL)) {
853c0dbca36SAlistair Francis             error_report("failed to load \"%s\"", image_name);
85407abe45cSLaszlo Ersek             exit(1);
85507abe45cSLaszlo Ersek         }
85607abe45cSLaszlo Ersek         size = length;
85707abe45cSLaszlo Ersek         data = (uint8_t *)contents;
85807abe45cSLaszlo Ersek     }
85907abe45cSLaszlo Ersek 
86007abe45cSLaszlo Ersek     fw_cfg_add_i32(fw_cfg, size_key, size);
86107abe45cSLaszlo Ersek     fw_cfg_add_bytes(fw_cfg, data_key, data, size);
86207abe45cSLaszlo Ersek }
86307abe45cSLaszlo Ersek 
864d8b1ae42SPeter Maydell static int do_arm_linux_init(Object *obj, void *opaque)
865d8b1ae42SPeter Maydell {
866d8b1ae42SPeter Maydell     if (object_dynamic_cast(obj, TYPE_ARM_LINUX_BOOT_IF)) {
867d8b1ae42SPeter Maydell         ARMLinuxBootIf *albif = ARM_LINUX_BOOT_IF(obj);
868d8b1ae42SPeter Maydell         ARMLinuxBootIfClass *albifc = ARM_LINUX_BOOT_IF_GET_CLASS(obj);
869d8b1ae42SPeter Maydell         struct arm_boot_info *info = opaque;
870d8b1ae42SPeter Maydell 
871d8b1ae42SPeter Maydell         if (albifc->arm_linux_init) {
872d8b1ae42SPeter Maydell             albifc->arm_linux_init(albif, info->secure_boot);
873d8b1ae42SPeter Maydell         }
874d8b1ae42SPeter Maydell     }
875d8b1ae42SPeter Maydell     return 0;
876d8b1ae42SPeter Maydell }
877d8b1ae42SPeter Maydell 
878a3f0ecfdSAdam Lackorzynski static int64_t arm_load_elf(struct arm_boot_info *info, uint64_t *pentry,
8799776f636SPeter Crosthwaite                             uint64_t *lowaddr, uint64_t *highaddr,
8809f43d4c3SPeter Maydell                             int elf_machine, AddressSpace *as)
8819776f636SPeter Crosthwaite {
8829776f636SPeter Crosthwaite     bool elf_is64;
8839776f636SPeter Crosthwaite     union {
8849776f636SPeter Crosthwaite         Elf32_Ehdr h32;
8859776f636SPeter Crosthwaite         Elf64_Ehdr h64;
8869776f636SPeter Crosthwaite     } elf_header;
8879776f636SPeter Crosthwaite     int data_swab = 0;
8889776f636SPeter Crosthwaite     bool big_endian;
889a3f0ecfdSAdam Lackorzynski     int64_t ret = -1;
8909776f636SPeter Crosthwaite     Error *err = NULL;
8919776f636SPeter Crosthwaite 
8929776f636SPeter Crosthwaite 
8939776f636SPeter Crosthwaite     load_elf_hdr(info->kernel_filename, &elf_header, &elf_is64, &err);
8949776f636SPeter Crosthwaite     if (err) {
89536f876ceSMarc-André Lureau         error_free(err);
8969776f636SPeter Crosthwaite         return ret;
8979776f636SPeter Crosthwaite     }
8989776f636SPeter Crosthwaite 
8999776f636SPeter Crosthwaite     if (elf_is64) {
9009776f636SPeter Crosthwaite         big_endian = elf_header.h64.e_ident[EI_DATA] == ELFDATA2MSB;
9019776f636SPeter Crosthwaite         info->endianness = big_endian ? ARM_ENDIANNESS_BE8
9029776f636SPeter Crosthwaite                                       : ARM_ENDIANNESS_LE;
9039776f636SPeter Crosthwaite     } else {
9049776f636SPeter Crosthwaite         big_endian = elf_header.h32.e_ident[EI_DATA] == ELFDATA2MSB;
9059776f636SPeter Crosthwaite         if (big_endian) {
9069776f636SPeter Crosthwaite             if (bswap32(elf_header.h32.e_flags) & EF_ARM_BE8) {
9079776f636SPeter Crosthwaite                 info->endianness = ARM_ENDIANNESS_BE8;
9089776f636SPeter Crosthwaite             } else {
9099776f636SPeter Crosthwaite                 info->endianness = ARM_ENDIANNESS_BE32;
9109776f636SPeter Crosthwaite                 /* In BE32, the CPU has a different view of the per-byte
9119776f636SPeter Crosthwaite                  * address map than the rest of the system. BE32 ELF files
9129776f636SPeter Crosthwaite                  * are organised such that they can be programmed through
9139776f636SPeter Crosthwaite                  * the CPU's per-word byte-reversed view of the world. QEMU
9149776f636SPeter Crosthwaite                  * however loads ELF files independently of the CPU. So
9159776f636SPeter Crosthwaite                  * tell the ELF loader to byte reverse the data for us.
9169776f636SPeter Crosthwaite                  */
9179776f636SPeter Crosthwaite                 data_swab = 2;
9189776f636SPeter Crosthwaite             }
9199776f636SPeter Crosthwaite         } else {
9209776f636SPeter Crosthwaite             info->endianness = ARM_ENDIANNESS_LE;
9219776f636SPeter Crosthwaite         }
9229776f636SPeter Crosthwaite     }
9239776f636SPeter Crosthwaite 
9244366e1dbSLiam Merwick     ret = load_elf_as(info->kernel_filename, NULL, NULL, NULL,
9256cdda0ffSAleksandar Markovic                       pentry, lowaddr, highaddr, NULL, big_endian, elf_machine,
9269f43d4c3SPeter Maydell                       1, data_swab, as);
9279776f636SPeter Crosthwaite     if (ret <= 0) {
9289776f636SPeter Crosthwaite         /* The header loaded but the image didn't */
9299776f636SPeter Crosthwaite         exit(1);
9309776f636SPeter Crosthwaite     }
9319776f636SPeter Crosthwaite 
9329776f636SPeter Crosthwaite     return ret;
9339776f636SPeter Crosthwaite }
9349776f636SPeter Crosthwaite 
93568115ed5SArd Biesheuvel static uint64_t load_aarch64_image(const char *filename, hwaddr mem_base,
9369f43d4c3SPeter Maydell                                    hwaddr *entry, AddressSpace *as)
93768115ed5SArd Biesheuvel {
93868115ed5SArd Biesheuvel     hwaddr kernel_load_offset = KERNEL64_LOAD_ADDR;
9395e6dbe1eSPeter Maydell     uint64_t kernel_size = 0;
94068115ed5SArd Biesheuvel     uint8_t *buffer;
94168115ed5SArd Biesheuvel     int size;
94268115ed5SArd Biesheuvel 
94368115ed5SArd Biesheuvel     /* On aarch64, it's the bootloader's job to uncompress the kernel. */
94468115ed5SArd Biesheuvel     size = load_image_gzipped_buffer(filename, LOAD_IMAGE_MAX_GUNZIP_BYTES,
94568115ed5SArd Biesheuvel                                      &buffer);
94668115ed5SArd Biesheuvel 
94768115ed5SArd Biesheuvel     if (size < 0) {
94868115ed5SArd Biesheuvel         gsize len;
94968115ed5SArd Biesheuvel 
95068115ed5SArd Biesheuvel         /* Load as raw file otherwise */
95168115ed5SArd Biesheuvel         if (!g_file_get_contents(filename, (char **)&buffer, &len, NULL)) {
95268115ed5SArd Biesheuvel             return -1;
95368115ed5SArd Biesheuvel         }
95468115ed5SArd Biesheuvel         size = len;
95568115ed5SArd Biesheuvel     }
95668115ed5SArd Biesheuvel 
95768115ed5SArd Biesheuvel     /* check the arm64 magic header value -- very old kernels may not have it */
95827640407SMarc-André Lureau     if (size > ARM64_MAGIC_OFFSET + 4 &&
95927640407SMarc-André Lureau         memcmp(buffer + ARM64_MAGIC_OFFSET, "ARM\x64", 4) == 0) {
96068115ed5SArd Biesheuvel         uint64_t hdrvals[2];
96168115ed5SArd Biesheuvel 
96268115ed5SArd Biesheuvel         /* The arm64 Image header has text_offset and image_size fields at 8 and
96368115ed5SArd Biesheuvel          * 16 bytes into the Image header, respectively. The text_offset field
96468115ed5SArd Biesheuvel          * is only valid if the image_size is non-zero.
96568115ed5SArd Biesheuvel          */
96668115ed5SArd Biesheuvel         memcpy(&hdrvals, buffer + ARM64_TEXT_OFFSET_OFFSET, sizeof(hdrvals));
9675e6dbe1eSPeter Maydell 
9685e6dbe1eSPeter Maydell         kernel_size = le64_to_cpu(hdrvals[1]);
9695e6dbe1eSPeter Maydell 
9705e6dbe1eSPeter Maydell         if (kernel_size != 0) {
97168115ed5SArd Biesheuvel             kernel_load_offset = le64_to_cpu(hdrvals[0]);
972ea358872SStewart Hildebrand 
973ea358872SStewart Hildebrand             /*
974ea358872SStewart Hildebrand              * We write our startup "bootloader" at the very bottom of RAM,
975ea358872SStewart Hildebrand              * so that bit can't be used for the image. Luckily the Image
976ea358872SStewart Hildebrand              * format specification is that the image requests only an offset
977ea358872SStewart Hildebrand              * from a 2MB boundary, not an absolute load address. So if the
978ea358872SStewart Hildebrand              * image requests an offset that might mean it overlaps with the
979ea358872SStewart Hildebrand              * bootloader, we can just load it starting at 2MB+offset rather
980ea358872SStewart Hildebrand              * than 0MB + offset.
981ea358872SStewart Hildebrand              */
982ea358872SStewart Hildebrand             if (kernel_load_offset < BOOTLOADER_MAX_SIZE) {
983ea358872SStewart Hildebrand                 kernel_load_offset += 2 * MiB;
984ea358872SStewart Hildebrand             }
98568115ed5SArd Biesheuvel         }
98668115ed5SArd Biesheuvel     }
98768115ed5SArd Biesheuvel 
9885e6dbe1eSPeter Maydell     /*
9895e6dbe1eSPeter Maydell      * Kernels before v3.17 don't populate the image_size field, and
9905e6dbe1eSPeter Maydell      * raw images have no header. For those our best guess at the size
9915e6dbe1eSPeter Maydell      * is the size of the Image file itself.
9925e6dbe1eSPeter Maydell      */
9935e6dbe1eSPeter Maydell     if (kernel_size == 0) {
9945e6dbe1eSPeter Maydell         kernel_size = size;
9955e6dbe1eSPeter Maydell     }
9965e6dbe1eSPeter Maydell 
99768115ed5SArd Biesheuvel     *entry = mem_base + kernel_load_offset;
9989f43d4c3SPeter Maydell     rom_add_blob_fixed_as(filename, buffer, size, *entry, as);
99968115ed5SArd Biesheuvel 
100068115ed5SArd Biesheuvel     g_free(buffer);
100168115ed5SArd Biesheuvel 
10025e6dbe1eSPeter Maydell     return kernel_size;
100368115ed5SArd Biesheuvel }
100468115ed5SArd Biesheuvel 
1005d33774eeSPeter Maydell static void arm_setup_direct_kernel_boot(ARMCPU *cpu,
1006d33774eeSPeter Maydell                                          struct arm_boot_info *info)
100753018216SPaolo Bonzini {
1008d33774eeSPeter Maydell     /* Set up for a direct boot of a kernel image file. */
1009c6faa758SArd Biesheuvel     CPUState *cs;
1010d33774eeSPeter Maydell     AddressSpace *as = arm_boot_address_space(cpu, info);
101153018216SPaolo Bonzini     int kernel_size;
101253018216SPaolo Bonzini     int initrd_size;
101353018216SPaolo Bonzini     int is_linux = 0;
1014d5fef92fSPeter Maydell     uint64_t elf_entry;
1015d5fef92fSPeter Maydell     /* Addresses of first byte used and first byte not used by the image */
101667505c11SPeter Maydell     uint64_t image_low_addr = 0, image_high_addr = 0;
1017da0af40dSPeter Maydell     int elf_machine;
101868115ed5SArd Biesheuvel     hwaddr entry;
10194d9ebf75SMian M. Hamayun     static const ARMInsnFixup *primary_loader;
1020e70af24bSPeter Maydell     uint64_t ram_end = info->loader_start + info->ram_size;
102153018216SPaolo Bonzini 
10224d9ebf75SMian M. Hamayun     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
10234d9ebf75SMian M. Hamayun         primary_loader = bootloader_aarch64;
1024da0af40dSPeter Maydell         elf_machine = EM_AARCH64;
10254d9ebf75SMian M. Hamayun     } else {
10264d9ebf75SMian M. Hamayun         primary_loader = bootloader;
102710b8ec73SPeter Crosthwaite         if (!info->write_board_setup) {
102810b8ec73SPeter Crosthwaite             primary_loader += BOOTLOADER_NO_BOARD_SETUP_OFFSET;
102910b8ec73SPeter Crosthwaite         }
1030da0af40dSPeter Maydell         elf_machine = EM_ARM;
10314d9ebf75SMian M. Hamayun     }
10324d9ebf75SMian M. Hamayun 
103353018216SPaolo Bonzini     if (!info->secondary_cpu_reset_hook) {
103453018216SPaolo Bonzini         info->secondary_cpu_reset_hook = default_reset_secondary;
103553018216SPaolo Bonzini     }
103653018216SPaolo Bonzini     if (!info->write_secondary_boot) {
103753018216SPaolo Bonzini         info->write_secondary_boot = default_write_secondary;
103853018216SPaolo Bonzini     }
103953018216SPaolo Bonzini 
104053018216SPaolo Bonzini     if (info->nb_cpus == 0)
104153018216SPaolo Bonzini         info->nb_cpus = 1;
104253018216SPaolo Bonzini 
104353018216SPaolo Bonzini     /* Assume that raw images are linux kernels, and ELF images are not.  */
1044d5fef92fSPeter Maydell     kernel_size = arm_load_elf(info, &elf_entry, &image_low_addr,
1045d5fef92fSPeter Maydell                                &image_high_addr, elf_machine, as);
104692df8450SArd Biesheuvel     if (kernel_size > 0 && have_dtb(info)) {
1047c3a42358SPeter Maydell         /*
1048c3a42358SPeter Maydell          * If there is still some room left at the base of RAM, try and put
104992df8450SArd Biesheuvel          * the DTB there like we do for images loaded with -bios or -pflash.
105092df8450SArd Biesheuvel          */
1051d5fef92fSPeter Maydell         if (image_low_addr > info->loader_start
1052d5fef92fSPeter Maydell             || image_high_addr < info->loader_start) {
1053c3a42358SPeter Maydell             /*
1054d5fef92fSPeter Maydell              * Set image_low_addr as address limit for arm_load_dtb if it may be
105592df8450SArd Biesheuvel              * pointing into RAM, otherwise pass '0' (no limit)
105692df8450SArd Biesheuvel              */
1057d5fef92fSPeter Maydell             if (image_low_addr < info->loader_start) {
1058d5fef92fSPeter Maydell                 image_low_addr = 0;
105992df8450SArd Biesheuvel             }
10603b77f6c3SIgor Mammedov             info->dtb_start = info->loader_start;
1061d5fef92fSPeter Maydell             info->dtb_limit = image_low_addr;
106292df8450SArd Biesheuvel         }
106392df8450SArd Biesheuvel     }
106453018216SPaolo Bonzini     entry = elf_entry;
106553018216SPaolo Bonzini     if (kernel_size < 0) {
1066f831f955SNick Hudson         uint64_t loadaddr = info->loader_start + KERNEL_NOLOAD_ADDR;
1067f831f955SNick Hudson         kernel_size = load_uimage_as(info->kernel_filename, &entry, &loadaddr,
10689f43d4c3SPeter Maydell                                      &is_linux, NULL, NULL, as);
106967505c11SPeter Maydell         if (kernel_size >= 0) {
107067505c11SPeter Maydell             image_low_addr = loadaddr;
107167505c11SPeter Maydell             image_high_addr = image_low_addr + kernel_size;
107267505c11SPeter Maydell         }
107353018216SPaolo Bonzini     }
10746f5d3cbeSRichard W.M. Jones     if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) && kernel_size < 0) {
107568115ed5SArd Biesheuvel         kernel_size = load_aarch64_image(info->kernel_filename,
10769f43d4c3SPeter Maydell                                          info->loader_start, &entry, as);
10776f5d3cbeSRichard W.M. Jones         is_linux = 1;
107867505c11SPeter Maydell         if (kernel_size >= 0) {
107967505c11SPeter Maydell             image_low_addr = entry;
108067505c11SPeter Maydell             image_high_addr = image_low_addr + kernel_size;
108167505c11SPeter Maydell         }
108268115ed5SArd Biesheuvel     } else if (kernel_size < 0) {
108368115ed5SArd Biesheuvel         /* 32-bit ARM */
108468115ed5SArd Biesheuvel         entry = info->loader_start + KERNEL_LOAD_ADDR;
10859f43d4c3SPeter Maydell         kernel_size = load_image_targphys_as(info->kernel_filename, entry,
1086e70af24bSPeter Maydell                                              ram_end - KERNEL_LOAD_ADDR, as);
108753018216SPaolo Bonzini         is_linux = 1;
108867505c11SPeter Maydell         if (kernel_size >= 0) {
108967505c11SPeter Maydell             image_low_addr = entry;
109067505c11SPeter Maydell             image_high_addr = image_low_addr + kernel_size;
109167505c11SPeter Maydell         }
109253018216SPaolo Bonzini     }
109353018216SPaolo Bonzini     if (kernel_size < 0) {
1094c0dbca36SAlistair Francis         error_report("could not load kernel '%s'", info->kernel_filename);
109553018216SPaolo Bonzini         exit(1);
109653018216SPaolo Bonzini     }
1097852dc64dSPeter Maydell 
1098852dc64dSPeter Maydell     if (kernel_size > info->ram_size) {
1099852dc64dSPeter Maydell         error_report("kernel '%s' is too large to fit in RAM "
1100852dc64dSPeter Maydell                      "(kernel size %d, RAM size %" PRId64 ")",
1101852dc64dSPeter Maydell                      info->kernel_filename, kernel_size, info->ram_size);
1102852dc64dSPeter Maydell         exit(1);
1103852dc64dSPeter Maydell     }
1104852dc64dSPeter Maydell 
110553018216SPaolo Bonzini     info->entry = entry;
1106e6b2b20dSPeter Maydell 
1107e6b2b20dSPeter Maydell     /*
1108e6b2b20dSPeter Maydell      * We want to put the initrd far enough into RAM that when the
1109e6b2b20dSPeter Maydell      * kernel is uncompressed it will not clobber the initrd. However
1110e6b2b20dSPeter Maydell      * on boards without much RAM we must ensure that we still leave
1111e6b2b20dSPeter Maydell      * enough room for a decent sized initrd, and on boards with large
1112e6b2b20dSPeter Maydell      * amounts of RAM we must avoid the initrd being so far up in RAM
1113e6b2b20dSPeter Maydell      * that it is outside lowmem and inaccessible to the kernel.
1114e6b2b20dSPeter Maydell      * So for boards with less  than 256MB of RAM we put the initrd
1115e6b2b20dSPeter Maydell      * halfway into RAM, and for boards with 256MB of RAM or more we put
1116e6b2b20dSPeter Maydell      * the initrd at 128MB.
1117e6b2b20dSPeter Maydell      * We also refuse to put the initrd somewhere that will definitely
1118e6b2b20dSPeter Maydell      * overlay the kernel we just loaded, though for kernel formats which
1119e6b2b20dSPeter Maydell      * don't tell us their exact size (eg self-decompressing 32-bit kernels)
1120e6b2b20dSPeter Maydell      * we might still make a bad choice here.
1121e6b2b20dSPeter Maydell      */
1122e6b2b20dSPeter Maydell     info->initrd_start = info->loader_start +
1123e4e34855SPhilippe Mathieu-Daudé         MIN(info->ram_size / 2, 128 * MiB);
112467505c11SPeter Maydell     if (image_high_addr) {
112567505c11SPeter Maydell         info->initrd_start = MAX(info->initrd_start, image_high_addr);
112667505c11SPeter Maydell     }
1127e6b2b20dSPeter Maydell     info->initrd_start = TARGET_PAGE_ALIGN(info->initrd_start);
1128e6b2b20dSPeter Maydell 
112953018216SPaolo Bonzini     if (is_linux) {
113047b1da81SPeter Maydell         uint32_t fixupcontext[FIXUP_MAX];
113147b1da81SPeter Maydell 
113253018216SPaolo Bonzini         if (info->initrd_filename) {
1133852dc64dSPeter Maydell 
1134852dc64dSPeter Maydell             if (info->initrd_start >= ram_end) {
1135852dc64dSPeter Maydell                 error_report("not enough space after kernel to load initrd");
1136852dc64dSPeter Maydell                 exit(1);
1137852dc64dSPeter Maydell             }
1138852dc64dSPeter Maydell 
11399f43d4c3SPeter Maydell             initrd_size = load_ramdisk_as(info->initrd_filename,
1140fd76663eSSoren Brinkmann                                           info->initrd_start,
1141e70af24bSPeter Maydell                                           ram_end - info->initrd_start, as);
1142fd76663eSSoren Brinkmann             if (initrd_size < 0) {
11439f43d4c3SPeter Maydell                 initrd_size = load_image_targphys_as(info->initrd_filename,
114453018216SPaolo Bonzini                                                      info->initrd_start,
1145e70af24bSPeter Maydell                                                      ram_end -
11469f43d4c3SPeter Maydell                                                      info->initrd_start,
11479f43d4c3SPeter Maydell                                                      as);
1148fd76663eSSoren Brinkmann             }
114953018216SPaolo Bonzini             if (initrd_size < 0) {
1150c0dbca36SAlistair Francis                 error_report("could not load initrd '%s'",
115153018216SPaolo Bonzini                              info->initrd_filename);
115253018216SPaolo Bonzini                 exit(1);
115353018216SPaolo Bonzini             }
1154b48b0640SAndrew Jones             if (info->initrd_start + initrd_size > ram_end) {
1155852dc64dSPeter Maydell                 error_report("could not load initrd '%s': "
1156852dc64dSPeter Maydell                              "too big to fit into RAM after the kernel",
1157852dc64dSPeter Maydell                              info->initrd_filename);
1158b48b0640SAndrew Jones                 exit(1);
1159852dc64dSPeter Maydell             }
116053018216SPaolo Bonzini         } else {
116153018216SPaolo Bonzini             initrd_size = 0;
116253018216SPaolo Bonzini         }
116353018216SPaolo Bonzini         info->initrd_size = initrd_size;
116453018216SPaolo Bonzini 
116547b1da81SPeter Maydell         fixupcontext[FIXUP_BOARDID] = info->board_id;
116610b8ec73SPeter Crosthwaite         fixupcontext[FIXUP_BOARD_SETUP] = info->board_setup_addr;
116753018216SPaolo Bonzini 
1168c3a42358SPeter Maydell         /*
1169c3a42358SPeter Maydell          * for device tree boot, we pass the DTB directly in r2. Otherwise
117053018216SPaolo Bonzini          * we point to the kernel args.
117153018216SPaolo Bonzini          */
117283bfffecSPeter Maydell         if (have_dtb(info)) {
117376e2aef3SAlexander Graf             hwaddr align;
117476e2aef3SAlexander Graf 
117576e2aef3SAlexander Graf             if (elf_machine == EM_AARCH64) {
117676e2aef3SAlexander Graf                 /*
117776e2aef3SAlexander Graf                  * Some AArch64 kernels on early bootup map the fdt region as
117876e2aef3SAlexander Graf                  *
117976e2aef3SAlexander Graf                  *   [ ALIGN_DOWN(fdt, 2MB) ... ALIGN_DOWN(fdt, 2MB) + 2MB ]
118076e2aef3SAlexander Graf                  *
118176e2aef3SAlexander Graf                  * Let's play safe and prealign it to 2MB to give us some space.
118253018216SPaolo Bonzini                  */
1183e4e34855SPhilippe Mathieu-Daudé                 align = 2 * MiB;
118476e2aef3SAlexander Graf             } else {
118576e2aef3SAlexander Graf                 /*
118676e2aef3SAlexander Graf                  * Some 32bit kernels will trash anything in the 4K page the
118776e2aef3SAlexander Graf                  * initrd ends in, so make sure the DTB isn't caught up in that.
118876e2aef3SAlexander Graf                  */
1189e4e34855SPhilippe Mathieu-Daudé                 align = 4 * KiB;
119076e2aef3SAlexander Graf             }
119176e2aef3SAlexander Graf 
119276e2aef3SAlexander Graf             /* Place the DTB after the initrd in memory with alignment. */
11933b77f6c3SIgor Mammedov             info->dtb_start = QEMU_ALIGN_UP(info->initrd_start + initrd_size,
11943b77f6c3SIgor Mammedov                                            align);
1195852dc64dSPeter Maydell             if (info->dtb_start >= ram_end) {
1196852dc64dSPeter Maydell                 error_report("Not enough space for DTB after kernel/initrd");
1197852dc64dSPeter Maydell                 exit(1);
1198852dc64dSPeter Maydell             }
1199751ebc13SRicardo Perez Blanco             fixupcontext[FIXUP_ARGPTR_LO] = info->dtb_start;
1200751ebc13SRicardo Perez Blanco             fixupcontext[FIXUP_ARGPTR_HI] = info->dtb_start >> 32;
120153018216SPaolo Bonzini         } else {
1202751ebc13SRicardo Perez Blanco             fixupcontext[FIXUP_ARGPTR_LO] =
1203751ebc13SRicardo Perez Blanco                 info->loader_start + KERNEL_ARGS_ADDR;
1204751ebc13SRicardo Perez Blanco             fixupcontext[FIXUP_ARGPTR_HI] =
1205751ebc13SRicardo Perez Blanco                 (info->loader_start + KERNEL_ARGS_ADDR) >> 32;
1206e4e34855SPhilippe Mathieu-Daudé             if (info->ram_size >= 4 * GiB) {
1207c0dbca36SAlistair Francis                 error_report("RAM size must be less than 4GB to boot"
120853018216SPaolo Bonzini                              " Linux kernel using ATAGS (try passing a device tree"
1209c0dbca36SAlistair Francis                              " using -dtb)");
121053018216SPaolo Bonzini                 exit(1);
121153018216SPaolo Bonzini             }
121253018216SPaolo Bonzini         }
1213751ebc13SRicardo Perez Blanco         fixupcontext[FIXUP_ENTRYPOINT_LO] = entry;
1214751ebc13SRicardo Perez Blanco         fixupcontext[FIXUP_ENTRYPOINT_HI] = entry >> 32;
121547b1da81SPeter Maydell 
121647b1da81SPeter Maydell         write_bootloader("bootloader", info->loader_start,
12179f43d4c3SPeter Maydell                          primary_loader, fixupcontext, as);
121847b1da81SPeter Maydell 
121953018216SPaolo Bonzini         if (info->nb_cpus > 1) {
122053018216SPaolo Bonzini             info->write_secondary_boot(cpu, info);
122153018216SPaolo Bonzini         }
122210b8ec73SPeter Crosthwaite         if (info->write_board_setup) {
122310b8ec73SPeter Crosthwaite             info->write_board_setup(cpu, info);
122410b8ec73SPeter Crosthwaite         }
1225d8b1ae42SPeter Maydell 
1226c3a42358SPeter Maydell         /*
1227c3a42358SPeter Maydell          * Notify devices which need to fake up firmware initialization
1228d8b1ae42SPeter Maydell          * that we're doing a direct kernel boot.
1229d8b1ae42SPeter Maydell          */
1230d8b1ae42SPeter Maydell         object_child_foreach_recursive(object_get_root(),
1231d8b1ae42SPeter Maydell                                        do_arm_linux_init, info);
123253018216SPaolo Bonzini     }
123353018216SPaolo Bonzini     info->is_linux = is_linux;
123453018216SPaolo Bonzini 
12350c949276SIgor Mammedov     for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) {
1236c6faa758SArd Biesheuvel         ARM_CPU(cs)->env.boot_info = info;
123753018216SPaolo Bonzini     }
1238d33774eeSPeter Maydell }
1239d33774eeSPeter Maydell 
12404c0f2687SPeter Maydell static void arm_setup_firmware_boot(ARMCPU *cpu, struct arm_boot_info *info)
1241d33774eeSPeter Maydell {
12424c0f2687SPeter Maydell     /* Set up for booting firmware (which might load a kernel via fw_cfg) */
1243d33774eeSPeter Maydell 
1244d33774eeSPeter Maydell     if (have_dtb(info)) {
1245d33774eeSPeter Maydell         /*
1246d33774eeSPeter Maydell          * If we have a device tree blob, but no kernel to supply it to (or
1247d33774eeSPeter Maydell          * the kernel is supposed to be loaded by the bootloader), copy the
1248d33774eeSPeter Maydell          * DTB to the base of RAM for the bootloader to pick up.
1249d33774eeSPeter Maydell          */
1250d33774eeSPeter Maydell         info->dtb_start = info->loader_start;
1251d33774eeSPeter Maydell     }
1252d33774eeSPeter Maydell 
1253d33774eeSPeter Maydell     if (info->kernel_filename) {
1254d33774eeSPeter Maydell         FWCfgState *fw_cfg;
1255d33774eeSPeter Maydell         bool try_decompressing_kernel;
1256d33774eeSPeter Maydell 
1257d33774eeSPeter Maydell         fw_cfg = fw_cfg_find();
1258dae25739SPeter Maydell 
1259dae25739SPeter Maydell         if (!fw_cfg) {
1260dae25739SPeter Maydell             error_report("This machine type does not support loading both "
1261dae25739SPeter Maydell                          "a guest firmware/BIOS image and a guest kernel at "
1262dae25739SPeter Maydell                          "the same time. You should change your QEMU command "
1263dae25739SPeter Maydell                          "line to specify one or the other, but not both.");
1264dae25739SPeter Maydell             exit(1);
1265dae25739SPeter Maydell         }
1266dae25739SPeter Maydell 
1267d33774eeSPeter Maydell         try_decompressing_kernel = arm_feature(&cpu->env,
1268d33774eeSPeter Maydell                                                ARM_FEATURE_AARCH64);
1269d33774eeSPeter Maydell 
1270d33774eeSPeter Maydell         /*
1271d33774eeSPeter Maydell          * Expose the kernel, the command line, and the initrd in fw_cfg.
1272d33774eeSPeter Maydell          * We don't process them here at all, it's all left to the
1273d33774eeSPeter Maydell          * firmware.
1274d33774eeSPeter Maydell          */
1275d33774eeSPeter Maydell         load_image_to_fw_cfg(fw_cfg,
1276d33774eeSPeter Maydell                              FW_CFG_KERNEL_SIZE, FW_CFG_KERNEL_DATA,
1277d33774eeSPeter Maydell                              info->kernel_filename,
1278d33774eeSPeter Maydell                              try_decompressing_kernel);
1279d33774eeSPeter Maydell         load_image_to_fw_cfg(fw_cfg,
1280d33774eeSPeter Maydell                              FW_CFG_INITRD_SIZE, FW_CFG_INITRD_DATA,
1281d33774eeSPeter Maydell                              info->initrd_filename, false);
1282d33774eeSPeter Maydell 
1283d33774eeSPeter Maydell         if (info->kernel_cmdline) {
1284d33774eeSPeter Maydell             fw_cfg_add_i32(fw_cfg, FW_CFG_CMDLINE_SIZE,
1285d33774eeSPeter Maydell                            strlen(info->kernel_cmdline) + 1);
1286d33774eeSPeter Maydell             fw_cfg_add_string(fw_cfg, FW_CFG_CMDLINE_DATA,
1287d33774eeSPeter Maydell                               info->kernel_cmdline);
1288d33774eeSPeter Maydell         }
1289d33774eeSPeter Maydell     }
1290d33774eeSPeter Maydell 
1291d33774eeSPeter Maydell     /*
1292d33774eeSPeter Maydell      * We will start from address 0 (typically a boot ROM image) in the
12932a5bdfc8SPeter Maydell      * same way as hardware. Leave env->boot_info NULL, so that
12942a5bdfc8SPeter Maydell      * do_cpu_reset() knows it does not need to alter the PC on reset.
1295d33774eeSPeter Maydell      */
12964c0f2687SPeter Maydell }
12974c0f2687SPeter Maydell 
12982744ece8STao Xu void arm_load_kernel(ARMCPU *cpu, MachineState *ms, struct arm_boot_info *info)
12994c0f2687SPeter Maydell {
13004c0f2687SPeter Maydell     CPUState *cs;
13014c0f2687SPeter Maydell     AddressSpace *as = arm_boot_address_space(cpu, info);
1302817e2db8SPeter Maydell     int boot_el;
1303817e2db8SPeter Maydell     CPUARMState *env = &cpu->env;
13044c0f2687SPeter Maydell 
13054c0f2687SPeter Maydell     /*
13064c0f2687SPeter Maydell      * CPU objects (unlike devices) are not automatically reset on system
13074c0f2687SPeter Maydell      * reset, so we must always register a handler to do so. If we're
13084c0f2687SPeter Maydell      * actually loading a kernel, the handler is also responsible for
13094c0f2687SPeter Maydell      * arranging that we start it correctly.
13104c0f2687SPeter Maydell      */
13114c0f2687SPeter Maydell     for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) {
13124c0f2687SPeter Maydell         qemu_register_reset(do_cpu_reset, ARM_CPU(cs));
13134c0f2687SPeter Maydell     }
13144c0f2687SPeter Maydell 
13154c0f2687SPeter Maydell     /*
13164c0f2687SPeter Maydell      * The board code is not supposed to set secure_board_setup unless
13174c0f2687SPeter Maydell      * running its code in secure mode is actually possible, and KVM
13184c0f2687SPeter Maydell      * doesn't support secure.
13194c0f2687SPeter Maydell      */
13204c0f2687SPeter Maydell     assert(!(info->secure_board_setup && kvm_enabled()));
13212744ece8STao Xu     info->kernel_filename = ms->kernel_filename;
13222744ece8STao Xu     info->kernel_cmdline = ms->kernel_cmdline;
13232744ece8STao Xu     info->initrd_filename = ms->initrd_filename;
1324f2ce39b4SPaolo Bonzini     info->dtb_filename = ms->dtb;
13254c0f2687SPeter Maydell     info->dtb_limit = 0;
13264c0f2687SPeter Maydell 
13274c0f2687SPeter Maydell     /* Load the kernel.  */
13284c0f2687SPeter Maydell     if (!info->kernel_filename || info->firmware_loaded) {
13294c0f2687SPeter Maydell         arm_setup_firmware_boot(cpu, info);
1330d33774eeSPeter Maydell     } else {
1331d33774eeSPeter Maydell         arm_setup_direct_kernel_boot(cpu, info);
1332d33774eeSPeter Maydell     }
133363a183edSEric Auger 
1334817e2db8SPeter Maydell     /*
1335817e2db8SPeter Maydell      * Disable the PSCI conduit if it is set up to target the same
1336817e2db8SPeter Maydell      * or a lower EL than the one we're going to start the guest code in.
1337817e2db8SPeter Maydell      * This logic needs to agree with the code in do_cpu_reset() which
1338817e2db8SPeter Maydell      * decides whether we're going to boot the guest in the highest
1339817e2db8SPeter Maydell      * supported exception level or in a lower one.
1340817e2db8SPeter Maydell      */
1341817e2db8SPeter Maydell 
1342*dc888dd4SPeter Maydell     /*
1343*dc888dd4SPeter Maydell      * If PSCI is enabled, then SMC calls all go to the PSCI handler and
1344*dc888dd4SPeter Maydell      * are never emulated to trap into guest code. It therefore does not
1345*dc888dd4SPeter Maydell      * make sense for the board to have a setup code fragment that runs
1346*dc888dd4SPeter Maydell      * in Secure, because this will probably need to itself issue an SMC of some
1347*dc888dd4SPeter Maydell      * kind as part of its operation.
1348*dc888dd4SPeter Maydell      */
1349*dc888dd4SPeter Maydell     assert(info->psci_conduit == QEMU_PSCI_CONDUIT_DISABLED ||
1350*dc888dd4SPeter Maydell            !info->secure_board_setup);
1351*dc888dd4SPeter Maydell 
1352817e2db8SPeter Maydell     /* Boot into highest supported EL ... */
1353817e2db8SPeter Maydell     if (arm_feature(env, ARM_FEATURE_EL3)) {
1354817e2db8SPeter Maydell         boot_el = 3;
1355817e2db8SPeter Maydell     } else if (arm_feature(env, ARM_FEATURE_EL2)) {
1356817e2db8SPeter Maydell         boot_el = 2;
1357817e2db8SPeter Maydell     } else {
1358817e2db8SPeter Maydell         boot_el = 1;
1359817e2db8SPeter Maydell     }
1360817e2db8SPeter Maydell     /* ...except that if we're booting Linux we adjust the EL we boot into */
1361817e2db8SPeter Maydell     if (info->is_linux && !info->secure_boot) {
1362817e2db8SPeter Maydell         boot_el = arm_feature(env, ARM_FEATURE_EL2) ? 2 : 1;
1363817e2db8SPeter Maydell     }
1364817e2db8SPeter Maydell 
1365817e2db8SPeter Maydell     if ((info->psci_conduit == QEMU_PSCI_CONDUIT_HVC && boot_el >= 2) ||
1366817e2db8SPeter Maydell         (info->psci_conduit == QEMU_PSCI_CONDUIT_SMC && boot_el == 3)) {
1367817e2db8SPeter Maydell         info->psci_conduit = QEMU_PSCI_CONDUIT_DISABLED;
1368817e2db8SPeter Maydell     }
1369817e2db8SPeter Maydell 
1370817e2db8SPeter Maydell     if (info->psci_conduit != QEMU_PSCI_CONDUIT_DISABLED) {
1371817e2db8SPeter Maydell         for (cs = first_cpu; cs; cs = CPU_NEXT(cs)) {
1372817e2db8SPeter Maydell             Object *cpuobj = OBJECT(cs);
1373817e2db8SPeter Maydell 
1374817e2db8SPeter Maydell             object_property_set_int(cpuobj, "psci-conduit", info->psci_conduit,
1375817e2db8SPeter Maydell                                     &error_abort);
1376817e2db8SPeter Maydell             /*
1377817e2db8SPeter Maydell              * Secondary CPUs start in PSCI powered-down state. Like the
1378817e2db8SPeter Maydell              * code in do_cpu_reset(), we assume first_cpu is the primary
1379817e2db8SPeter Maydell              * CPU.
1380817e2db8SPeter Maydell              */
1381817e2db8SPeter Maydell             if (cs != first_cpu) {
1382817e2db8SPeter Maydell                 object_property_set_bool(cpuobj, "start-powered-off", true,
1383817e2db8SPeter Maydell                                          &error_abort);
1384817e2db8SPeter Maydell             }
1385817e2db8SPeter Maydell         }
1386817e2db8SPeter Maydell     }
1387817e2db8SPeter Maydell 
1388817e2db8SPeter Maydell     /*
1389817e2db8SPeter Maydell      * arm_load_dtb() may add a PSCI node so it must be called after we have
1390817e2db8SPeter Maydell      * decided whether to enable PSCI and set the psci-conduit CPU properties.
1391817e2db8SPeter Maydell      */
13923b77f6c3SIgor Mammedov     if (!info->skip_dtb_autoload && have_dtb(info)) {
13932744ece8STao Xu         if (arm_load_dtb(info->dtb_start, info, info->dtb_limit, as, ms) < 0) {
13943b77f6c3SIgor Mammedov             exit(1);
13953b77f6c3SIgor Mammedov         }
13963b77f6c3SIgor Mammedov     }
1397ac9d32e3SEric Auger }
1398d8b1ae42SPeter Maydell 
1399d8b1ae42SPeter Maydell static const TypeInfo arm_linux_boot_if_info = {
1400d8b1ae42SPeter Maydell     .name = TYPE_ARM_LINUX_BOOT_IF,
1401d8b1ae42SPeter Maydell     .parent = TYPE_INTERFACE,
1402d8b1ae42SPeter Maydell     .class_size = sizeof(ARMLinuxBootIfClass),
1403d8b1ae42SPeter Maydell };
1404d8b1ae42SPeter Maydell 
1405d8b1ae42SPeter Maydell static void arm_linux_boot_register_types(void)
1406d8b1ae42SPeter Maydell {
1407d8b1ae42SPeter Maydell     type_register_static(&arm_linux_boot_if_info);
1408d8b1ae42SPeter Maydell }
1409d8b1ae42SPeter Maydell 
1410d8b1ae42SPeter Maydell type_init(arm_linux_boot_register_types)
1411