]> git.proxmox.com Git - mirror_qemu.git/blame - hw/arm/raspi.c
hw/arm/raspi: Make board_rev a field of RaspiMachineClass
[mirror_qemu.git] / hw / arm / raspi.c
CommitLineData
1df7d1f9
AB
1/*
2 * Raspberry Pi emulation (c) 2012 Gregory Estrade
3 * Upstreaming code cleanup [including bcm2835_*] (c) 2013 Jan Petrous
4 *
5 * Rasperry Pi 2 emulation Copyright (c) 2015, Microsoft
6 * Written by Andrew Baumann
7 *
bade5816
PE
8 * Raspberry Pi 3 emulation Copyright (c) 2018 Zoltán Baldaszti
9 * Upstream code cleanup (c) 2018 Pekka Enberg
10 *
1df7d1f9
AB
11 * This code is licensed under the GNU GPLv2 and later.
12 */
13
c964b660 14#include "qemu/osdep.h"
ff3dcf28 15#include "qemu/units.h"
f5bb124e 16#include "qemu/cutils.h"
da34e65c 17#include "qapi/error.h"
4771d756 18#include "cpu.h"
1df7d1f9 19#include "hw/arm/bcm2836.h"
cd6c9977 20#include "hw/registerfields.h"
1df7d1f9
AB
21#include "qemu/error-report.h"
22#include "hw/boards.h"
23#include "hw/loader.h"
12ec8bd5 24#include "hw/arm/boot.h"
1df7d1f9
AB
25#include "sysemu/sysemu.h"
26
27#define SMPBOOT_ADDR 0x300 /* this should leave enough space for ATAGS */
28#define MVBAR_ADDR 0x400 /* secure vectors */
29#define BOARDSETUP_ADDR (MVBAR_ADDR + 0x20) /* board setup code */
bade5816
PE
30#define FIRMWARE_ADDR_2 0x8000 /* Pi 2 loads kernel.img here by default */
31#define FIRMWARE_ADDR_3 0x80000 /* Pi 3 loads kernel.img here by default */
ff72cb6b 32#define SPINTABLE_ADDR 0xd8 /* Pi 3 bootloader spintable */
1df7d1f9 33
918c81a5
PMD
34/* Registered machine type (matches RPi Foundation bootloader and U-Boot) */
35#define MACH_TYPE_BCM2708 3138
1df7d1f9 36
cb57df6f
PMD
37typedef struct RaspiMachineState {
38 /*< private >*/
39 MachineState parent_obj;
40 /*< public >*/
926dcdf0 41 BCM283XState soc;
1df7d1f9 42 MemoryRegion ram;
cb57df6f
PMD
43} RaspiMachineState;
44
45typedef struct RaspiMachineClass {
46 /*< private >*/
47 MachineClass parent_obj;
48 /*< public >*/
c318c66c 49 uint32_t board_rev;
cb57df6f
PMD
50} RaspiMachineClass;
51
52#define TYPE_RASPI_MACHINE MACHINE_TYPE_NAME("raspi-common")
53#define RASPI_MACHINE(obj) \
54 OBJECT_CHECK(RaspiMachineState, (obj), TYPE_RASPI_MACHINE)
55
56#define RASPI_MACHINE_CLASS(klass) \
57 OBJECT_CLASS_CHECK(RaspiMachineClass, (klass), TYPE_RASPI_MACHINE)
58#define RASPI_MACHINE_GET_CLASS(obj) \
59 OBJECT_GET_CLASS(RaspiMachineClass, (obj), TYPE_RASPI_MACHINE)
1df7d1f9 60
cd6c9977
PMD
61/*
62 * Board revision codes:
63 * www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/
64 */
65FIELD(REV_CODE, REVISION, 0, 4);
66FIELD(REV_CODE, TYPE, 4, 8);
67FIELD(REV_CODE, PROCESSOR, 12, 4);
68FIELD(REV_CODE, MANUFACTURER, 16, 4);
69FIELD(REV_CODE, MEMORY_SIZE, 20, 3);
70FIELD(REV_CODE, STYLE, 23, 1);
71
f5bb124e
PMD
72static uint64_t board_ram_size(uint32_t board_rev)
73{
74 assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */
75 return 256 * MiB << FIELD_EX32(board_rev, REV_CODE, MEMORY_SIZE);
76}
77
cd6c9977
PMD
78static int board_processor_id(uint32_t board_rev)
79{
80 assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */
81 return FIELD_EX32(board_rev, REV_CODE, PROCESSOR);
82}
83
84static int board_version(uint32_t board_rev)
85{
86 return board_processor_id(board_rev) + 1;
87}
88
2e664b45
PMD
89static const char *board_soc_type(uint32_t board_rev)
90{
91 static const char *soc_types[] = {
92 NULL, TYPE_BCM2836, TYPE_BCM2837,
93 };
94 int proc_id = board_processor_id(board_rev);
95
96 if (proc_id >= ARRAY_SIZE(soc_types) || !soc_types[proc_id]) {
97 error_report("Unsupported processor id '%d' (board revision: 0x%x)",
98 proc_id, board_rev);
99 exit(1);
100 }
101 return soc_types[proc_id];
102}
103
1df7d1f9
AB
104static void write_smpboot(ARMCPU *cpu, const struct arm_boot_info *info)
105{
106 static const uint32_t smpboot[] = {
107 0xe1a0e00f, /* mov lr, pc */
108 0xe3a0fe00 + (BOARDSETUP_ADDR >> 4), /* mov pc, BOARDSETUP_ADDR */
109 0xee100fb0, /* mrc p15, 0, r0, c0, c0, 5;get core ID */
110 0xe7e10050, /* ubfx r0, r0, #0, #2 ;extract LSB */
111 0xe59f5014, /* ldr r5, =0x400000CC ;load mbox base */
112 0xe320f001, /* 1: yield */
113 0xe7953200, /* ldr r3, [r5, r0, lsl #4] ;read mbox for our core*/
114 0xe3530000, /* cmp r3, #0 ;spin while zero */
115 0x0afffffb, /* beq 1b */
116 0xe7853200, /* str r3, [r5, r0, lsl #4] ;clear mbox */
117 0xe12fff13, /* bx r3 ;jump to target */
118 0x400000cc, /* (constant: mailbox 3 read/clear base) */
119 };
120
121 /* check that we don't overrun board setup vectors */
122 QEMU_BUILD_BUG_ON(SMPBOOT_ADDR + sizeof(smpboot) > MVBAR_ADDR);
123 /* check that board setup address is correctly relocated */
124 QEMU_BUILD_BUG_ON((BOARDSETUP_ADDR & 0xf) != 0
125 || (BOARDSETUP_ADDR >> 4) >= 0x100);
126
0f073693
PMD
127 rom_add_blob_fixed_as("raspi_smpboot", smpboot, sizeof(smpboot),
128 info->smp_loader_start,
129 arm_boot_address_space(cpu, info));
1df7d1f9
AB
130}
131
ff72cb6b
PM
132static void write_smpboot64(ARMCPU *cpu, const struct arm_boot_info *info)
133{
0f073693 134 AddressSpace *as = arm_boot_address_space(cpu, info);
ff72cb6b
PM
135 /* Unlike the AArch32 version we don't need to call the board setup hook.
136 * The mechanism for doing the spin-table is also entirely different.
137 * We must have four 64-bit fields at absolute addresses
138 * 0xd8, 0xe0, 0xe8, 0xf0 in RAM, which are the flag variables for
139 * our CPUs, and which we must ensure are zero initialized before
140 * the primary CPU goes into the kernel. We put these variables inside
141 * a rom blob, so that the reset for ROM contents zeroes them for us.
142 */
143 static const uint32_t smpboot[] = {
144 0xd2801b05, /* mov x5, 0xd8 */
145 0xd53800a6, /* mrs x6, mpidr_el1 */
146 0x924004c6, /* and x6, x6, #0x3 */
147 0xd503205f, /* spin: wfe */
148 0xf86678a4, /* ldr x4, [x5,x6,lsl #3] */
149 0xb4ffffc4, /* cbz x4, spin */
150 0xd2800000, /* mov x0, #0x0 */
151 0xd2800001, /* mov x1, #0x0 */
152 0xd2800002, /* mov x2, #0x0 */
153 0xd2800003, /* mov x3, #0x0 */
154 0xd61f0080, /* br x4 */
155 };
156
157 static const uint64_t spintables[] = {
158 0, 0, 0, 0
159 };
160
0f073693
PMD
161 rom_add_blob_fixed_as("raspi_smpboot", smpboot, sizeof(smpboot),
162 info->smp_loader_start, as);
163 rom_add_blob_fixed_as("raspi_spintables", spintables, sizeof(spintables),
164 SPINTABLE_ADDR, as);
ff72cb6b
PM
165}
166
1df7d1f9
AB
167static void write_board_setup(ARMCPU *cpu, const struct arm_boot_info *info)
168{
169 arm_write_secure_board_setup_dummy_smc(cpu, info, MVBAR_ADDR);
170}
171
172static void reset_secondary(ARMCPU *cpu, const struct arm_boot_info *info)
173{
174 CPUState *cs = CPU(cpu);
175 cpu_set_pc(cs, info->smp_loader_start);
176}
177
178static void setup_boot(MachineState *machine, int version, size_t ram_size)
179{
180 static struct arm_boot_info binfo;
181 int r;
182
918c81a5 183 binfo.board_id = MACH_TYPE_BCM2708;
1df7d1f9 184 binfo.ram_size = ram_size;
cc7d44c2 185 binfo.nb_cpus = machine->smp.cpus;
01e02f5a
PM
186
187 if (version <= 2) {
188 /* The rpi1 and 2 require some custom setup code to run in Secure
189 * mode before booting a kernel (to set up the SMC vectors so
190 * that we get a no-op SMC; this is used by Linux to call the
191 * firmware for some cache maintenance operations.
192 * The rpi3 doesn't need this.
193 */
194 binfo.board_setup_addr = BOARDSETUP_ADDR;
195 binfo.write_board_setup = write_board_setup;
196 binfo.secure_board_setup = true;
197 binfo.secure_boot = true;
198 }
1df7d1f9 199
bade5816
PE
200 /* Pi2 and Pi3 requires SMP setup */
201 if (version >= 2) {
1df7d1f9 202 binfo.smp_loader_start = SMPBOOT_ADDR;
ff72cb6b
PM
203 if (version == 2) {
204 binfo.write_secondary_boot = write_smpboot;
205 } else {
206 binfo.write_secondary_boot = write_smpboot64;
207 }
1df7d1f9
AB
208 binfo.secondary_cpu_reset_hook = reset_secondary;
209 }
210
211 /* If the user specified a "firmware" image (e.g. UEFI), we bypass
212 * the normal Linux boot process
213 */
214 if (machine->firmware) {
bade5816 215 hwaddr firmware_addr = version == 3 ? FIRMWARE_ADDR_3 : FIRMWARE_ADDR_2;
1df7d1f9 216 /* load the firmware image (typically kernel.img) */
bade5816
PE
217 r = load_image_targphys(machine->firmware, firmware_addr,
218 ram_size - firmware_addr);
1df7d1f9
AB
219 if (r < 0) {
220 error_report("Failed to load firmware from %s", machine->firmware);
221 exit(1);
222 }
223
bade5816 224 binfo.entry = firmware_addr;
1df7d1f9 225 binfo.firmware_loaded = true;
1df7d1f9
AB
226 }
227
2744ece8 228 arm_load_kernel(ARM_CPU(first_cpu), machine, &binfo);
1df7d1f9
AB
229}
230
c318c66c 231static void raspi_init(MachineState *machine)
1df7d1f9 232{
c318c66c 233 RaspiMachineClass *mc = RASPI_MACHINE_GET_CLASS(machine);
cb57df6f 234 RaspiMachineState *s = RASPI_MACHINE(machine);
c318c66c 235 uint32_t board_rev = mc->board_rev;
cd6c9977 236 int version = board_version(board_rev);
f5bb124e 237 uint64_t ram_size = board_ram_size(board_rev);
5e9c2a8d 238 uint32_t vcram_size;
a55b53a2
AB
239 DriveInfo *di;
240 BlockBackend *blk;
241 BusState *bus;
242 DeviceState *carddev;
1df7d1f9 243
f5bb124e
PMD
244 if (machine->ram_size != ram_size) {
245 char *size_str = size_to_str(ram_size);
246 error_report("Invalid RAM size, should be %s", size_str);
247 g_free(size_str);
ff3dcf28
PM
248 exit(1);
249 }
250
1df7d1f9
AB
251 /* Allocate and map RAM */
252 memory_region_allocate_system_memory(&s->ram, OBJECT(machine), "ram",
253 machine->ram_size);
254 /* FIXME: Remove when we have custom CPU address space support */
255 memory_region_add_subregion_overlap(get_system_memory(), 0, &s->ram, 0);
256
257 /* Setup the SOC */
cc360632
PMD
258 object_initialize_child(OBJECT(machine), "soc", &s->soc, sizeof(s->soc),
259 board_soc_type(board_rev), &error_abort, NULL);
1df7d1f9
AB
260 object_property_add_const_link(OBJECT(&s->soc), "ram", OBJECT(&s->ram),
261 &error_abort);
bade5816 262 object_property_set_int(OBJECT(&s->soc), board_rev, "board-rev",
f0afa731 263 &error_abort);
1df7d1f9
AB
264 object_property_set_bool(OBJECT(&s->soc), true, "realized", &error_abort);
265
a55b53a2
AB
266 /* Create and plug in the SD cards */
267 di = drive_get_next(IF_SD);
268 blk = di ? blk_by_legacy_dinfo(di) : NULL;
269 bus = qdev_get_child_bus(DEVICE(&s->soc), "sd-bus");
270 if (bus == NULL) {
271 error_report("No SD bus found in SOC object");
272 exit(1);
273 }
274 carddev = qdev_create(bus, TYPE_SD_CARD);
275 qdev_prop_set_drive(carddev, "drive", blk, &error_fatal);
276 object_property_set_bool(OBJECT(carddev), true, "realized", &error_fatal);
277
c5c6c47c
MAL
278 vcram_size = object_property_get_uint(OBJECT(&s->soc), "vcram-size",
279 &error_abort);
bade5816
PE
280 setup_boot(machine, version, machine->ram_size - vcram_size);
281}
282
283static void raspi2_init(MachineState *machine)
284{
c318c66c 285 raspi_init(machine);
1df7d1f9
AB
286}
287
cb57df6f 288static void raspi2_machine_class_init(ObjectClass *oc, void *data)
1df7d1f9 289{
cb57df6f 290 MachineClass *mc = MACHINE_CLASS(oc);
c318c66c
PMD
291 RaspiMachineClass *rmc = RASPI_MACHINE_CLASS(oc);
292 uint32_t board_rev = (uint32_t)(uintptr_t)data;
cb57df6f 293
c318c66c 294 rmc->board_rev = board_rev;
fc78a990 295 mc->desc = "Raspberry Pi 2B";
1df7d1f9
AB
296 mc->init = raspi2_init;
297 mc->block_default_type = IF_SD;
298 mc->no_parallel = 1;
299 mc->no_floppy = 1;
300 mc->no_cdrom = 1;
926dcdf0
PM
301 mc->max_cpus = BCM283X_NCPUS;
302 mc->min_cpus = BCM283X_NCPUS;
303 mc->default_cpus = BCM283X_NCPUS;
2aee4107 304 mc->default_ram_size = 1 * GiB;
4672cbd7 305 mc->ignore_memory_transaction_failures = true;
1df7d1f9 306};
1c3db49d
PE
307
308#ifdef TARGET_AARCH64
309static void raspi3_init(MachineState *machine)
310{
c318c66c 311 raspi_init(machine);
1c3db49d
PE
312}
313
cb57df6f 314static void raspi3_machine_class_init(ObjectClass *oc, void *data)
1c3db49d 315{
cb57df6f 316 MachineClass *mc = MACHINE_CLASS(oc);
c318c66c
PMD
317 RaspiMachineClass *rmc = RASPI_MACHINE_CLASS(oc);
318 uint32_t board_rev = (uint32_t)(uintptr_t)data;
cb57df6f 319
c318c66c 320 rmc->board_rev = board_rev;
fc78a990 321 mc->desc = "Raspberry Pi 3B";
1c3db49d
PE
322 mc->init = raspi3_init;
323 mc->block_default_type = IF_SD;
324 mc->no_parallel = 1;
325 mc->no_floppy = 1;
326 mc->no_cdrom = 1;
926dcdf0
PM
327 mc->max_cpus = BCM283X_NCPUS;
328 mc->min_cpus = BCM283X_NCPUS;
329 mc->default_cpus = BCM283X_NCPUS;
2aee4107 330 mc->default_ram_size = 1 * GiB;
1c3db49d 331}
1c3db49d 332#endif
cb57df6f
PMD
333
334static const TypeInfo raspi_machine_types[] = {
335 {
336 .name = MACHINE_TYPE_NAME("raspi2"),
337 .parent = TYPE_RASPI_MACHINE,
338 .class_init = raspi2_machine_class_init,
c318c66c 339 .class_data = (void *)0xa21041,
cb57df6f
PMD
340#ifdef TARGET_AARCH64
341 }, {
342 .name = MACHINE_TYPE_NAME("raspi3"),
343 .parent = TYPE_RASPI_MACHINE,
344 .class_init = raspi3_machine_class_init,
c318c66c 345 .class_data = (void *)0xa02082,
cb57df6f
PMD
346#endif
347 }, {
348 .name = TYPE_RASPI_MACHINE,
349 .parent = TYPE_MACHINE,
350 .instance_size = sizeof(RaspiMachineState),
351 .class_size = sizeof(RaspiMachineClass),
352 .abstract = true,
353 }
354};
355
356DEFINE_TYPES(raspi_machine_types)