2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
3 * Upstreaming code cleanup [including bcm2835_*] (c) 2013 Jan Petrous
5 * Rasperry Pi 2 emulation Copyright (c) 2015, Microsoft
6 * Written by Andrew Baumann
8 * This code is licensed under the GNU GPLv2 and later.
11 /* Based on versatilepb.c, copyright terms below. */
14 * ARM Versatile Platform/Application Baseboard System emulation.
16 * Copyright (c) 2005-2007 CodeSourcery.
17 * Written by Paul Brook
19 * This code is licensed under the GPL.
23 #include "hw/arm/bcm2835.h"
25 >>>>>>> upstreaming-raspi
26 #include "hw/arm/bcm2836.h"
27 #include "qemu/error-report.h"
28 #include "hw/boards.h"
29 #include "hw/loader.h"
30 #include "hw/arm/arm.h"
31 #include "sysemu/sysemu.h"
33 #include "hw/arm/raspi_platform.h"
35 >>>>>>> upstreaming-raspi
37 #define SMPBOOT_ADDR 0x300 /* this should leave enough space for ATAGS */
38 #define MVBAR_ADDR 0x400 /* secure vectors */
39 #define BOARDSETUP_ADDR (MVBAR_ADDR + 0x20) /* board setup code */
40 #define FIRMWARE_ADDR 0x8000 /* Pi loads kernel.img here by default */
42 /* Table of Linux board IDs for different Pi versions */
43 static const int raspi_boardid[] = {[1] = 0xc42, [2] = 0xc43};
45 typedef struct RaspiMachineState {
51 >>>>>>> upstreaming-raspi
57 static void write_smpboot(ARMCPU *cpu, const struct arm_boot_info *info)
59 static const uint32_t smpboot[] = {
60 0xE1A0E00F, /* mov lr, pc */
61 0xE3A0FE42, /* mov pc, #0x420 ;call BOARDSETUP_ADDR */
62 0xEE100FB0, /* mrc p15, 0, r0, c0, c0, 5;get core ID */
63 0xE7E10050, /* ubfx r0, r0, #0, #2 ;extract LSB */
64 0xE59F5014, /* ldr r5, =0x400000CC ;load mbox base */
65 0xE320F001, /* 1: yield */
66 0xE7953200, /* ldr r3, [r5, r0, lsl #4] ;read mbox for our core*/
67 0xE3530000, /* cmp r3, #0 ;spin while zero */
68 0x0AFFFFFB, /* beq 1b */
69 0xE7853200, /* str r3, [r5, r0, lsl #4] ;clear mbox */
70 0xE12FFF13, /* bx r3 ;jump to target */
71 0x400000CC, /* (constant: mailbox 3 read/clear base) */
74 assert(SMPBOOT_ADDR + sizeof(smpboot) <= MVBAR_ADDR);
75 rom_add_blob_fixed("raspi_smpboot", smpboot, sizeof(smpboot),
76 info->smp_loader_start);
79 static void write_board_setup(ARMCPU *cpu, const struct arm_boot_info *info)
81 static const uint32_t board_setup[] = {
82 /* MVBAR_ADDR: secure monitor vectors */
83 0xEAFFFFFE, /* (spin) */
84 0xEAFFFFFE, /* (spin) */
85 0xE1B0F00E, /* movs pc, lr ;SMC exception return */
86 0xEAFFFFFE, /* (spin) */
87 0xEAFFFFFE, /* (spin) */
88 0xEAFFFFFE, /* (spin) */
89 0xEAFFFFFE, /* (spin) */
90 0xEAFFFFFE, /* (spin) */
92 0xE3A00B01, /* mov r0, #0x400 ;MVBAR_ADDR */
93 0xEE0C0F30, /* mcr p15, 0, r0, c12, c0, 1 ;set MVBAR */
94 0xE3A00031, /* mov r0, #0x31 ;enable AW, FW, NS */
95 0xEE010F11, /* mcr p15, 0, r0, c1, c1, 0 ;write SCR */
96 0xE1A0100E, /* mov r1, lr ;save LR across SMC */
97 0xE1600070, /* smc #0 ;monitor call */
98 0xE1A0F001, /* mov pc, r1 ;return */
101 rom_add_blob_fixed("raspi_boardsetup", board_setup, sizeof(board_setup),
105 static void reset_secondary(ARMCPU *cpu, const struct arm_boot_info *info)
107 CPUState *cs = CPU(cpu);
108 cpu_set_pc(cs, info->smp_loader_start);
111 static void setup_boot(MachineState *machine, int version, size_t ram_size)
113 static struct arm_boot_info binfo;
116 binfo.board_id = raspi_boardid[version];
117 binfo.ram_size = ram_size;
118 binfo.nb_cpus = smp_cpus;
119 binfo.board_setup_addr = BOARDSETUP_ADDR;
120 binfo.write_board_setup = write_board_setup;
121 binfo.secure_board_setup = true;
122 binfo.secure_boot = true;
125 /* Pi2 requires SMP setup code */
127 /* Pi2 requires SMP setup */
128 >>>>>>> upstreaming-raspi
130 binfo.smp_loader_start = SMPBOOT_ADDR;
131 binfo.write_secondary_boot = write_smpboot;
132 binfo.secondary_cpu_reset_hook = reset_secondary;
135 /* If the user specified a "firmware" image (e.g. UEFI), we bypass
136 the normal Linux boot process */
137 if (machine->firmware) {
138 /* load the firmware image (typically kernel.img) */
139 r = load_image_targphys(machine->firmware, FIRMWARE_ADDR,
140 ram_size - FIRMWARE_ADDR);
142 error_report("Failed to load firmware from %s", machine->firmware);
146 /* set variables so arm_load_kernel does the right thing */
147 binfo.entry = FIRMWARE_ADDR;
148 binfo.firmware_loaded = true;
150 /* Just let arm_load_kernel do everything for us... */
151 binfo.kernel_filename = machine->kernel_filename;
152 binfo.kernel_cmdline = machine->kernel_cmdline;
153 binfo.initrd_filename = machine->initrd_filename;
156 arm_load_kernel(ARM_CPU(first_cpu), &binfo);
160 static void raspi_machine_init(MachineState *machine, int version)
162 RaspiMachineState *s = g_new0(RaspiMachineState, 1);
165 /* Initialise the relevant SOC */
166 assert(version == 1 || version == 2);
169 object_initialize(&s->soc.pi1, sizeof(s->soc.pi1), TYPE_BCM2835);
172 object_initialize(&s->soc.pi2, sizeof(s->soc.pi2), TYPE_BCM2836);
177 static void raspi2_init(MachineState *machine)
179 RaspiMachineState *s = g_new0(RaspiMachineState, 1);
181 /* Initialise the SOC */
182 object_initialize(&s->soc.pi2, sizeof(s->soc.pi2), TYPE_BCM2836);
183 >>>>>>> upstreaming-raspi
184 object_property_add_child(OBJECT(machine), "soc", &s->soc.obj,
187 /* Allocate and map RAM */
188 memory_region_allocate_system_memory(&s->ram, OBJECT(machine), "ram",
190 memory_region_add_subregion_overlap(get_system_memory(), 0, &s->ram, 0);
194 object_property_set_bool(&s->soc.obj, true, "realized", &error_abort);
196 vcram_size = object_property_get_int(&s->soc.obj, "vcram-size",
200 setup_boot(machine, version, machine->ram_size - vcram_size);
203 static void raspi1_init(MachineState *machine)
205 raspi_machine_init(machine, 1);
208 static void raspi2_init(MachineState *machine)
210 raspi_machine_init(machine, 2);
213 static void raspi1_machine_init(MachineClass *mc)
215 mc->desc = "Raspberry Pi";
216 mc->init = raspi1_init;
218 object_property_add_const_link(&s->soc.obj, "ram", OBJECT(&s->ram),
220 object_property_set_bool(&s->soc.obj, true, "realized", &error_abort);
223 setup_boot(machine, 2, machine->ram_size);
226 static void raspi2_machine_init(MachineClass *mc)
228 mc->desc = "Raspberry Pi 2";
229 mc->init = raspi2_init;
230 >>>>>>> upstreaming-raspi
231 mc->block_default_type = IF_SD;
236 mc->default_ram_size = 512 * 1024 * 1024;
238 DEFINE_MACHINE("raspi", raspi1_machine_init)
240 static void raspi2_machine_init(MachineClass *mc)
242 raspi1_machine_init(mc);
243 mc->desc = "Raspberry Pi 2";
244 mc->init = raspi2_init;
245 mc->max_cpus = BCM2836_NCPUS;
246 mc->default_ram_size = 1024 * 1024 * 1024;
248 mc->max_cpus = BCM2836_NCPUS;
249 /* XXX: Temporary restriction in RAM size from the full 1GB. Since
250 * we do not yet support the framebuffer / GPU, we need to limit
251 * RAM usable by the OS to sit below the peripherals. */
252 mc->default_ram_size = 0x3F000000; /* BCM2836_PERI_BASE */
253 >>>>>>> upstreaming-raspi
255 DEFINE_MACHINE("raspi2", raspi2_machine_init)