xref: /qemu/include/hw/arm/boot.h (revision cbb95d496810023a485a3cfc1a096733b055ce89)
187ecb68bSpbrook /*
212ec8bd5SPeter Maydell  * ARM kernel loader.
387ecb68bSpbrook  *
487ecb68bSpbrook  * Copyright (c) 2006 CodeSourcery.
587ecb68bSpbrook  * Written by Paul Brook
687ecb68bSpbrook  *
78e31bf38SMatthew Fernandez  * This code is licensed under the LGPL.
887ecb68bSpbrook  *
987ecb68bSpbrook  */
1087ecb68bSpbrook 
1112ec8bd5SPeter Maydell #ifndef HW_ARM_BOOT_H
1212ec8bd5SPeter Maydell #define HW_ARM_BOOT_H
1387ecb68bSpbrook 
14fcf5ef2aSThomas Huth #include "target/arm/cpu-qom.h"
15ac9d32e3SEric Auger #include "qemu/notify.h"
167d6f78cfSAvi Kivity 
179776f636SPeter Crosthwaite typedef enum {
189776f636SPeter Crosthwaite     ARM_ENDIANNESS_UNKNOWN = 0,
199776f636SPeter Crosthwaite     ARM_ENDIANNESS_LE,
209776f636SPeter Crosthwaite     ARM_ENDIANNESS_BE8,
219776f636SPeter Crosthwaite     ARM_ENDIANNESS_BE32,
229776f636SPeter Crosthwaite } arm_endianness;
239776f636SPeter Crosthwaite 
243651c285SPeter Maydell /**
253651c285SPeter Maydell  * armv7m_load_kernel:
263651c285SPeter Maydell  * @cpu: CPU
273651c285SPeter Maydell  * @kernel_filename: file to load
28761c532aSPeter Maydell  * @mem_base: base address to load image at (should be where the
29761c532aSPeter Maydell  *            CPU expects to find its vector table on reset)
303651c285SPeter Maydell  * @mem_size: mem_size: maximum image size to load
313651c285SPeter Maydell  *
323651c285SPeter Maydell  * Load the guest image for an ARMv7M system. This must be called by
3338d81dafSPeter Maydell  * any ARMv7M board. (This is necessary to ensure that the CPU resets
3438d81dafSPeter Maydell  * correctly on system reset, as well as for kernel loading.)
353651c285SPeter Maydell  */
36761c532aSPeter Maydell void armv7m_load_kernel(ARMCPU *cpu, const char *kernel_filename,
37761c532aSPeter Maydell                         hwaddr mem_base, int mem_size);
3887ecb68bSpbrook 
3987ecb68bSpbrook /* arm_boot.c */
40f93eb9ffSbalrog struct arm_boot_info {
41de841deaSPeter Maydell     uint64_t ram_size;
42f93eb9ffSbalrog     const char *kernel_filename;
43f93eb9ffSbalrog     const char *kernel_cmdline;
44f93eb9ffSbalrog     const char *initrd_filename;
45412beee6SGrant Likely     const char *dtb_filename;
46a8170e5eSAvi Kivity     hwaddr loader_start;
473b77f6c3SIgor Mammedov     hwaddr dtb_start;
483b77f6c3SIgor Mammedov     hwaddr dtb_limit;
493b77f6c3SIgor Mammedov     /* If set to True, arm_load_kernel() will not load DTB.
503b77f6c3SIgor Mammedov      * It allows board to load DTB manually later.
513b77f6c3SIgor Mammedov      * (default: False)
523b77f6c3SIgor Mammedov      */
533b77f6c3SIgor Mammedov     bool skip_dtb_autoload;
549d5ba9bfSMark Langsdorf     /* multicore boards that use the default secondary core boot functions
559d5ba9bfSMark Langsdorf      * need to put the address of the secondary boot code, the boot reg,
569d5ba9bfSMark Langsdorf      * and the GIC address in the next 3 values, respectively. boards that
579d5ba9bfSMark Langsdorf      * have their own boot functions can use these values as they want.
589d5ba9bfSMark Langsdorf      */
59a8170e5eSAvi Kivity     hwaddr smp_loader_start;
60a8170e5eSAvi Kivity     hwaddr smp_bootreg_addr;
61a8170e5eSAvi Kivity     hwaddr gic_cpu_if_addr;
62f93eb9ffSbalrog     int board_id;
63c8e829b7SGreg Bellows     /* ARM machines that support the ARM Security Extensions use this field to
64c8e829b7SGreg Bellows      * control whether Linux is booted as secure(true) or non-secure(false).
65c8e829b7SGreg Bellows      */
66c8e829b7SGreg Bellows     bool secure_boot;
67462a8bc6SStefan Weil     int (*atag_board)(const struct arm_boot_info *info, void *p);
689d5ba9bfSMark Langsdorf     /* multicore boards that use the default secondary core boot functions
699d5ba9bfSMark Langsdorf      * can ignore these two function calls. If the default functions won't
709d5ba9bfSMark Langsdorf      * work, then write_secondary_boot() should write a suitable blob of
719b574c29SAndreas Färber      * code mimicking the secondary CPU startup process used by the board's
729d5ba9bfSMark Langsdorf      * boot loader/boot ROM code, and secondary_cpu_reset_hook() should
739b574c29SAndreas Färber      * perform any necessary CPU reset handling and set the PC for the
749d5ba9bfSMark Langsdorf      * secondary CPUs to point at this boot blob.
75d4a29ed6SPeter Maydell      *
76d4a29ed6SPeter Maydell      * These hooks won't be called if secondary CPUs are booting via
77d4a29ed6SPeter Maydell      * emulated PSCI (see psci_conduit below).
789d5ba9bfSMark Langsdorf      */
799543b0cdSAndreas Färber     void (*write_secondary_boot)(ARMCPU *cpu,
809d5ba9bfSMark Langsdorf                                  const struct arm_boot_info *info);
815d309320SAndreas Färber     void (*secondary_cpu_reset_hook)(ARMCPU *cpu,
829d5ba9bfSMark Langsdorf                                      const struct arm_boot_info *info);
830fb79851SJohn Rigby     /* if a board is able to create a dtb without a dtb file then it
840fb79851SJohn Rigby      * sets get_dtb. This will only be used if no dtb file is provided
850fb79851SJohn Rigby      * by the user. On success, sets *size to the length of the created
860fb79851SJohn Rigby      * dtb, and returns a pointer to it. (The caller must free this memory
870fb79851SJohn Rigby      * with g_free() when it has finished with it.) On failure, returns NULL.
880fb79851SJohn Rigby      */
890fb79851SJohn Rigby     void *(*get_dtb)(const struct arm_boot_info *info, int *size);
903b1cceb8SPeter Maydell     /* if a board needs to be able to modify a device tree provided by
913b1cceb8SPeter Maydell      * the user it should implement this hook.
923b1cceb8SPeter Maydell      */
933b1cceb8SPeter Maydell     void (*modify_dtb)(const struct arm_boot_info *info, void *fdt);
94817e2db8SPeter Maydell     /*
95817e2db8SPeter Maydell      * If a board wants to use the QEMU emulated-firmware PSCI support,
96817e2db8SPeter Maydell      * it should set this to QEMU_PSCI_CONDUIT_HVC or QEMU_PSCI_CONDUIT_SMC
97817e2db8SPeter Maydell      * as appropriate. arm_load_kernel() will set the psci-conduit and
98817e2db8SPeter Maydell      * start-powered-off properties on the CPUs accordingly.
99817e2db8SPeter Maydell      * Note that if the guest image is started at the same exception level
100817e2db8SPeter Maydell      * as the conduit specifies calls should go to (eg guest firmware booted
101817e2db8SPeter Maydell      * to EL3) then PSCI will not be enabled.
102817e2db8SPeter Maydell      */
103817e2db8SPeter Maydell     int psci_conduit;
104f2d74978SPaul Brook     /* Used internally by arm_boot.c */
105f2d74978SPaul Brook     int is_linux;
106fc53b7d4SPeter Maydell     hwaddr initrd_start;
107a8170e5eSAvi Kivity     hwaddr initrd_size;
108a8170e5eSAvi Kivity     hwaddr entry;
10907abe45cSLaszlo Ersek 
11007abe45cSLaszlo Ersek     /* Boot firmware has been loaded, typically at address 0, with -bios or
11107abe45cSLaszlo Ersek      * -pflash. It also implies that fw_cfg_find() will succeed.
11207abe45cSLaszlo Ersek      */
11307abe45cSLaszlo Ersek     bool firmware_loaded;
11410b8ec73SPeter Crosthwaite 
11510b8ec73SPeter Crosthwaite     /* Address at which board specific loader/setup code exists. If enabled,
11610b8ec73SPeter Crosthwaite      * this code-blob will run before anything else. It must return to the
11710b8ec73SPeter Crosthwaite      * caller via the link register. There is no stack set up. Enabled by
11810b8ec73SPeter Crosthwaite      * defining write_board_setup, which is responsible for loading the blob
11910b8ec73SPeter Crosthwaite      * to the specified address.
12010b8ec73SPeter Crosthwaite      */
12110b8ec73SPeter Crosthwaite     hwaddr board_setup_addr;
12210b8ec73SPeter Crosthwaite     void (*write_board_setup)(ARMCPU *cpu,
12310b8ec73SPeter Crosthwaite                               const struct arm_boot_info *info);
124baf6b681SPeter Crosthwaite 
12545c078f1SClement Deschamps     /*
12645c078f1SClement Deschamps      * If set, the board specific loader/setup blob will be run from secure
127baf6b681SPeter Crosthwaite      * mode, regardless of secure_boot. The blob becomes responsible for
12845c078f1SClement Deschamps      * changing to non-secure state if implementing a non-secure boot,
12945c078f1SClement Deschamps      * including setting up EL3/Secure registers such as the NSACR as
13045c078f1SClement Deschamps      * required by the Linux booting ABI before the switch to non-secure.
131baf6b681SPeter Crosthwaite      */
132baf6b681SPeter Crosthwaite     bool secure_board_setup;
1339776f636SPeter Crosthwaite 
1349776f636SPeter Crosthwaite     arm_endianness endianness;
135f93eb9ffSbalrog };
136ac9d32e3SEric Auger 
137ac9d32e3SEric Auger /**
138ac9d32e3SEric Auger  * arm_load_kernel - Loads memory with everything needed to boot
139ac9d32e3SEric Auger  *
140ac9d32e3SEric Auger  * @cpu: handle to the first CPU object
141ac9d32e3SEric Auger  * @info: handle to the boot info struct
142ac9d32e3SEric Auger  * Registers a machine init done notifier that copies to memory
143ac9d32e3SEric Auger  * everything needed to boot, depending on machine and user options:
144ac9d32e3SEric Auger  * kernel image, boot loaders, initrd, dtb. Also registers the CPU
145ac9d32e3SEric Auger  * reset handler.
146ac9d32e3SEric Auger  *
147ac9d32e3SEric Auger  * In case the machine file supports the platform bus device and its
148ac9d32e3SEric Auger  * dynamically instantiable sysbus devices, this function must be called
149ac9d32e3SEric Auger  * before sysbus-fdt arm_register_platform_bus_fdt_creator. Indeed the
150ac9d32e3SEric Auger  * machine init done notifiers are called in registration reverse order.
151ac9d32e3SEric Auger  */
1522744ece8STao Xu void arm_load_kernel(ARMCPU *cpu, MachineState *ms, struct arm_boot_info *info);
15387ecb68bSpbrook 
1543b77f6c3SIgor Mammedov AddressSpace *arm_boot_address_space(ARMCPU *cpu,
1553b77f6c3SIgor Mammedov                                      const struct arm_boot_info *info);
1563b77f6c3SIgor Mammedov 
1573b77f6c3SIgor Mammedov /**
1583b77f6c3SIgor Mammedov  * arm_load_dtb() - load a device tree binary image into memory
1593b77f6c3SIgor Mammedov  * @addr:       the address to load the image at
1603b77f6c3SIgor Mammedov  * @binfo:      struct describing the boot environment
1613b77f6c3SIgor Mammedov  * @addr_limit: upper limit of the available memory area at @addr
1623b77f6c3SIgor Mammedov  * @as:         address space to load image to
163*cbb95d49SPhilippe Mathieu-Daudé  * @cpu:        ARM CPU object
1643b77f6c3SIgor Mammedov  *
1653b77f6c3SIgor Mammedov  * Load a device tree supplied by the machine or by the user  with the
1663b77f6c3SIgor Mammedov  * '-dtb' command line option, and put it at offset @addr in target
1673b77f6c3SIgor Mammedov  * memory.
1683b77f6c3SIgor Mammedov  *
1693b77f6c3SIgor Mammedov  * If @addr_limit contains a meaningful value (i.e., it is strictly greater
1703b77f6c3SIgor Mammedov  * than @addr), the device tree is only loaded if its size does not exceed
1713b77f6c3SIgor Mammedov  * the limit.
1723b77f6c3SIgor Mammedov  *
1733b77f6c3SIgor Mammedov  * Returns: the size of the device tree image on success,
1743b77f6c3SIgor Mammedov  *          0 if the image size exceeds the limit,
1753b77f6c3SIgor Mammedov  *          -1 on errors.
1763b77f6c3SIgor Mammedov  *
1773b77f6c3SIgor Mammedov  * Note: Must not be called unless have_dtb(binfo) is true.
1783b77f6c3SIgor Mammedov  */
1793b77f6c3SIgor Mammedov int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
180*cbb95d49SPhilippe Mathieu-Daudé                  hwaddr addr_limit, AddressSpace *as, MachineState *ms,
181*cbb95d49SPhilippe Mathieu-Daudé                  ARMCPU *cpu);
1823b77f6c3SIgor Mammedov 
183716536a9SAndrew Baumann /* Write a secure board setup routine with a dummy handler for SMCs */
184716536a9SAndrew Baumann void arm_write_secure_board_setup_dummy_smc(ARMCPU *cpu,
185716536a9SAndrew Baumann                                             const struct arm_boot_info *info,
186716536a9SAndrew Baumann                                             hwaddr mvbar_addr);
187716536a9SAndrew Baumann 
1880fe43f0aSCédric Le Goater typedef enum {
1890fe43f0aSCédric Le Goater     FIXUP_NONE = 0,     /* do nothing */
1900fe43f0aSCédric Le Goater     FIXUP_TERMINATOR,   /* end of insns */
1910fe43f0aSCédric Le Goater     FIXUP_BOARDID,      /* overwrite with board ID number */
1920fe43f0aSCédric Le Goater     FIXUP_BOARD_SETUP,  /* overwrite with board specific setup code address */
1930fe43f0aSCédric Le Goater     FIXUP_ARGPTR_LO,    /* overwrite with pointer to kernel args */
1940fe43f0aSCédric Le Goater     FIXUP_ARGPTR_HI,    /* overwrite with pointer to kernel args (high half) */
1950fe43f0aSCédric Le Goater     FIXUP_ENTRYPOINT_LO, /* overwrite with kernel entry point */
1960fe43f0aSCédric Le Goater     FIXUP_ENTRYPOINT_HI, /* overwrite with kernel entry point (high half) */
1970fe43f0aSCédric Le Goater     FIXUP_GIC_CPU_IF,   /* overwrite with GIC CPU interface address */
1980fe43f0aSCédric Le Goater     FIXUP_BOOTREG,      /* overwrite with boot register address */
1990fe43f0aSCédric Le Goater     FIXUP_DSB,          /* overwrite with correct DSB insn for cpu */
2000fe43f0aSCédric Le Goater     FIXUP_MAX,
2010fe43f0aSCédric Le Goater } FixupType;
2020fe43f0aSCédric Le Goater 
2030fe43f0aSCédric Le Goater typedef struct ARMInsnFixup {
2040fe43f0aSCédric Le Goater     uint32_t insn;
2050fe43f0aSCédric Le Goater     FixupType fixup;
2060fe43f0aSCédric Le Goater } ARMInsnFixup;
2070fe43f0aSCédric Le Goater 
2080fe43f0aSCédric Le Goater /**
2090fe43f0aSCédric Le Goater  * arm_write_bootloader - write a bootloader to guest memory
2100fe43f0aSCédric Le Goater  * @name: name of the bootloader blob
2110fe43f0aSCédric Le Goater  * @as: AddressSpace to write the bootloader
2120fe43f0aSCédric Le Goater  * @addr: guest address to write it
2130fe43f0aSCédric Le Goater  * @insns: the blob to be loaded
2140fe43f0aSCédric Le Goater  * @fixupcontext: context to be used for any fixups in @insns
2150fe43f0aSCédric Le Goater  *
2160fe43f0aSCédric Le Goater  * Write a bootloader to guest memory at address @addr in the address
2170fe43f0aSCédric Le Goater  * space @as. @name is the name to use for the resulting ROM blob, so
2180fe43f0aSCédric Le Goater  * it should be unique in the system and reasonably identifiable for debugging.
2190fe43f0aSCédric Le Goater  *
2200fe43f0aSCédric Le Goater  * @insns must be an array of ARMInsnFixup structs, each of which has
2210fe43f0aSCédric Le Goater  * one 32-bit value to be written to the guest memory, and a fixup to be
2220fe43f0aSCédric Le Goater  * applied to the value. FIXUP_NONE (do nothing) is value 0, so effectively
2230fe43f0aSCédric Le Goater  * the fixup is optional when writing a struct initializer.
2240fe43f0aSCédric Le Goater  * The final entry in the array must be { 0, FIXUP_TERMINATOR }.
2250fe43f0aSCédric Le Goater  *
2260fe43f0aSCédric Le Goater  * All other supported fixup types have the semantics "ignore insn
2270fe43f0aSCédric Le Goater  * and instead use the value from the array element @fixupcontext[fixup]".
2280fe43f0aSCédric Le Goater  * The caller should therefore provide @fixupcontext as an array of
2290fe43f0aSCédric Le Goater  * size FIXUP_MAX whose elements have been initialized for at least
2300fe43f0aSCédric Le Goater  * the entries that @insns refers to.
2310fe43f0aSCédric Le Goater  */
2320fe43f0aSCédric Le Goater void arm_write_bootloader(const char *name,
2330fe43f0aSCédric Le Goater                           AddressSpace *as, hwaddr addr,
2340fe43f0aSCédric Le Goater                           const ARMInsnFixup *insns,
2350fe43f0aSCédric Le Goater                           const uint32_t *fixupcontext);
2360fe43f0aSCédric Le Goater 
23712ec8bd5SPeter Maydell #endif /* HW_ARM_BOOT_H */
238