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 * Raspberry Pi 3 emulation Copyright (c) 2018 Zoltán Baldaszti
9 * Upstream code cleanup (c) 2018 Pekka Enberg
11 * This code is licensed under the GNU GPLv2 and later.
14 #include "qemu/osdep.h"
15 #include "qapi/error.h"
16 #include "qemu-common.h"
18 #include "hw/arm/bcm2836.h"
19 #include "qemu/error-report.h"
20 #include "hw/boards.h"
21 #include "hw/loader.h"
22 #include "hw/arm/arm.h"
23 #include "sysemu/sysemu.h"
25 #define SMPBOOT_ADDR 0x300 /* this should leave enough space for ATAGS */
26 #define MVBAR_ADDR 0x400 /* secure vectors */
27 #define BOARDSETUP_ADDR (MVBAR_ADDR + 0x20) /* board setup code */
28 #define FIRMWARE_ADDR_2 0x8000 /* Pi 2 loads kernel.img here by default */
29 #define FIRMWARE_ADDR_3 0x80000 /* Pi 3 loads kernel.img here by default */
31 /* Table of Linux board IDs for different Pi versions */
32 static const int raspi_boardid
[] = {[1] = 0xc42, [2] = 0xc43, [3] = 0xc44};
34 typedef struct RasPiState
{
39 static void write_smpboot(ARMCPU
*cpu
, const struct arm_boot_info
*info
)
41 static const uint32_t smpboot
[] = {
42 0xe1a0e00f, /* mov lr, pc */
43 0xe3a0fe00 + (BOARDSETUP_ADDR
>> 4), /* mov pc, BOARDSETUP_ADDR */
44 0xee100fb0, /* mrc p15, 0, r0, c0, c0, 5;get core ID */
45 0xe7e10050, /* ubfx r0, r0, #0, #2 ;extract LSB */
46 0xe59f5014, /* ldr r5, =0x400000CC ;load mbox base */
47 0xe320f001, /* 1: yield */
48 0xe7953200, /* ldr r3, [r5, r0, lsl #4] ;read mbox for our core*/
49 0xe3530000, /* cmp r3, #0 ;spin while zero */
50 0x0afffffb, /* beq 1b */
51 0xe7853200, /* str r3, [r5, r0, lsl #4] ;clear mbox */
52 0xe12fff13, /* bx r3 ;jump to target */
53 0x400000cc, /* (constant: mailbox 3 read/clear base) */
56 /* check that we don't overrun board setup vectors */
57 QEMU_BUILD_BUG_ON(SMPBOOT_ADDR
+ sizeof(smpboot
) > MVBAR_ADDR
);
58 /* check that board setup address is correctly relocated */
59 QEMU_BUILD_BUG_ON((BOARDSETUP_ADDR
& 0xf) != 0
60 || (BOARDSETUP_ADDR
>> 4) >= 0x100);
62 rom_add_blob_fixed("raspi_smpboot", smpboot
, sizeof(smpboot
),
63 info
->smp_loader_start
);
66 static void write_board_setup(ARMCPU
*cpu
, const struct arm_boot_info
*info
)
68 arm_write_secure_board_setup_dummy_smc(cpu
, info
, MVBAR_ADDR
);
71 static void reset_secondary(ARMCPU
*cpu
, const struct arm_boot_info
*info
)
73 CPUState
*cs
= CPU(cpu
);
74 cpu_set_pc(cs
, info
->smp_loader_start
);
77 static void setup_boot(MachineState
*machine
, int version
, size_t ram_size
)
79 static struct arm_boot_info binfo
;
82 binfo
.board_id
= raspi_boardid
[version
];
83 binfo
.ram_size
= ram_size
;
84 binfo
.nb_cpus
= smp_cpus
;
85 binfo
.board_setup_addr
= BOARDSETUP_ADDR
;
86 binfo
.write_board_setup
= write_board_setup
;
87 binfo
.secure_board_setup
= true;
88 binfo
.secure_boot
= true;
90 /* Pi2 and Pi3 requires SMP setup */
92 binfo
.smp_loader_start
= SMPBOOT_ADDR
;
93 binfo
.write_secondary_boot
= write_smpboot
;
94 binfo
.secondary_cpu_reset_hook
= reset_secondary
;
97 /* If the user specified a "firmware" image (e.g. UEFI), we bypass
98 * the normal Linux boot process
100 if (machine
->firmware
) {
101 hwaddr firmware_addr
= version
== 3 ? FIRMWARE_ADDR_3
: FIRMWARE_ADDR_2
;
102 /* load the firmware image (typically kernel.img) */
103 r
= load_image_targphys(machine
->firmware
, firmware_addr
,
104 ram_size
- firmware_addr
);
106 error_report("Failed to load firmware from %s", machine
->firmware
);
110 binfo
.entry
= firmware_addr
;
111 binfo
.firmware_loaded
= true;
113 binfo
.kernel_filename
= machine
->kernel_filename
;
114 binfo
.kernel_cmdline
= machine
->kernel_cmdline
;
115 binfo
.initrd_filename
= machine
->initrd_filename
;
118 arm_load_kernel(ARM_CPU(first_cpu
), &binfo
);
121 static void raspi_init(MachineState
*machine
, int version
)
123 RasPiState
*s
= g_new0(RasPiState
, 1);
128 DeviceState
*carddev
;
130 object_initialize(&s
->soc
, sizeof(s
->soc
), TYPE_BCM2836
);
131 object_property_add_child(OBJECT(machine
), "soc", OBJECT(&s
->soc
),
134 /* Allocate and map RAM */
135 memory_region_allocate_system_memory(&s
->ram
, OBJECT(machine
), "ram",
137 /* FIXME: Remove when we have custom CPU address space support */
138 memory_region_add_subregion_overlap(get_system_memory(), 0, &s
->ram
, 0);
141 object_property_add_const_link(OBJECT(&s
->soc
), "ram", OBJECT(&s
->ram
),
143 object_property_set_str(OBJECT(&s
->soc
), machine
->cpu_type
, "cpu-type",
145 object_property_set_int(OBJECT(&s
->soc
), smp_cpus
, "enabled-cpus",
147 int board_rev
= version
== 3 ? 0xa02082 : 0xa21041;
148 object_property_set_int(OBJECT(&s
->soc
), board_rev
, "board-rev",
150 object_property_set_bool(OBJECT(&s
->soc
), true, "realized", &error_abort
);
152 /* Create and plug in the SD cards */
153 di
= drive_get_next(IF_SD
);
154 blk
= di
? blk_by_legacy_dinfo(di
) : NULL
;
155 bus
= qdev_get_child_bus(DEVICE(&s
->soc
), "sd-bus");
157 error_report("No SD bus found in SOC object");
160 carddev
= qdev_create(bus
, TYPE_SD_CARD
);
161 qdev_prop_set_drive(carddev
, "drive", blk
, &error_fatal
);
162 object_property_set_bool(OBJECT(carddev
), true, "realized", &error_fatal
);
164 vcram_size
= object_property_get_uint(OBJECT(&s
->soc
), "vcram-size",
166 setup_boot(machine
, version
, machine
->ram_size
- vcram_size
);
169 static void raspi2_init(MachineState
*machine
)
171 raspi_init(machine
, 2);
174 static void raspi2_machine_init(MachineClass
*mc
)
176 mc
->desc
= "Raspberry Pi 2";
177 mc
->init
= raspi2_init
;
178 mc
->block_default_type
= IF_SD
;
182 mc
->default_cpu_type
= ARM_CPU_TYPE_NAME("cortex-a15");
183 mc
->max_cpus
= BCM2836_NCPUS
;
184 mc
->min_cpus
= BCM2836_NCPUS
;
185 mc
->default_cpus
= BCM2836_NCPUS
;
186 mc
->default_ram_size
= 1024 * 1024 * 1024;
187 mc
->ignore_memory_transaction_failures
= true;
189 DEFINE_MACHINE("raspi2", raspi2_machine_init
)