xref: /qemu/hw/arm/raspi.c (revision cdfaa57dcb53ba012439765a1462247dfda8595d)
11df7d1f9SAndrew Baumann /*
21df7d1f9SAndrew Baumann  * Raspberry Pi emulation (c) 2012 Gregory Estrade
31df7d1f9SAndrew Baumann  * Upstreaming code cleanup [including bcm2835_*] (c) 2013 Jan Petrous
41df7d1f9SAndrew Baumann  *
51df7d1f9SAndrew Baumann  * Rasperry Pi 2 emulation Copyright (c) 2015, Microsoft
61df7d1f9SAndrew Baumann  * Written by Andrew Baumann
71df7d1f9SAndrew Baumann  *
8bade5816SPekka Enberg  * Raspberry Pi 3 emulation Copyright (c) 2018 Zoltán Baldaszti
9bade5816SPekka Enberg  * Upstream code cleanup (c) 2018 Pekka Enberg
10bade5816SPekka Enberg  *
116111a0c0SPhilippe Mathieu-Daudé  * This work is licensed under the terms of the GNU GPL, version 2 or later.
126111a0c0SPhilippe Mathieu-Daudé  * See the COPYING file in the top-level directory.
131df7d1f9SAndrew Baumann  */
141df7d1f9SAndrew Baumann 
15c964b660SPeter Maydell #include "qemu/osdep.h"
16ff3dcf28SPeter Maydell #include "qemu/units.h"
17f5bb124eSPhilippe Mathieu-Daudé #include "qemu/cutils.h"
18da34e65cSMarkus Armbruster #include "qapi/error.h"
194771d756SPaolo Bonzini #include "cpu.h"
201df7d1f9SAndrew Baumann #include "hw/arm/bcm2836.h"
21cd6c9977SPhilippe Mathieu-Daudé #include "hw/registerfields.h"
221df7d1f9SAndrew Baumann #include "qemu/error-report.h"
231df7d1f9SAndrew Baumann #include "hw/boards.h"
241df7d1f9SAndrew Baumann #include "hw/loader.h"
2512ec8bd5SPeter Maydell #include "hw/arm/boot.h"
261df7d1f9SAndrew Baumann #include "sysemu/sysemu.h"
27db1015e9SEduardo Habkost #include "qom/object.h"
281df7d1f9SAndrew Baumann 
291df7d1f9SAndrew Baumann #define SMPBOOT_ADDR    0x300 /* this should leave enough space for ATAGS */
301df7d1f9SAndrew Baumann #define MVBAR_ADDR      0x400 /* secure vectors */
311df7d1f9SAndrew Baumann #define BOARDSETUP_ADDR (MVBAR_ADDR + 0x20) /* board setup code */
32bade5816SPekka Enberg #define FIRMWARE_ADDR_2 0x8000 /* Pi 2 loads kernel.img here by default */
33bade5816SPekka Enberg #define FIRMWARE_ADDR_3 0x80000 /* Pi 3 loads kernel.img here by default */
34ff72cb6bSPeter Maydell #define SPINTABLE_ADDR  0xd8 /* Pi 3 bootloader spintable */
351df7d1f9SAndrew Baumann 
36918c81a5SPhilippe Mathieu-Daudé /* Registered machine type (matches RPi Foundation bootloader and U-Boot) */
37918c81a5SPhilippe Mathieu-Daudé #define MACH_TYPE_BCM2708   3138
381df7d1f9SAndrew Baumann 
39db1015e9SEduardo Habkost struct RaspiMachineState {
40cb57df6fSPhilippe Mathieu-Daudé     /*< private >*/
41cb57df6fSPhilippe Mathieu-Daudé     MachineState parent_obj;
42cb57df6fSPhilippe Mathieu-Daudé     /*< public >*/
43926dcdf0SPeter Maydell     BCM283XState soc;
440f15c6e3SPhilippe Mathieu-Daudé     struct arm_boot_info binfo;
45db1015e9SEduardo Habkost };
46db1015e9SEduardo Habkost typedef struct RaspiMachineState RaspiMachineState;
47cb57df6fSPhilippe Mathieu-Daudé 
48db1015e9SEduardo Habkost struct RaspiMachineClass {
49cb57df6fSPhilippe Mathieu-Daudé     /*< private >*/
50cb57df6fSPhilippe Mathieu-Daudé     MachineClass parent_obj;
51cb57df6fSPhilippe Mathieu-Daudé     /*< public >*/
52c318c66cSPhilippe Mathieu-Daudé     uint32_t board_rev;
53db1015e9SEduardo Habkost };
54db1015e9SEduardo Habkost typedef struct RaspiMachineClass RaspiMachineClass;
55cb57df6fSPhilippe Mathieu-Daudé 
56cb57df6fSPhilippe Mathieu-Daudé #define TYPE_RASPI_MACHINE       MACHINE_TYPE_NAME("raspi-common")
578110fa1dSEduardo Habkost DECLARE_OBJ_CHECKERS(RaspiMachineState, RaspiMachineClass,
588110fa1dSEduardo Habkost                      RASPI_MACHINE, TYPE_RASPI_MACHINE)
59cb57df6fSPhilippe Mathieu-Daudé 
601df7d1f9SAndrew Baumann 
61cd6c9977SPhilippe Mathieu-Daudé /*
62cd6c9977SPhilippe Mathieu-Daudé  * Board revision codes:
63cd6c9977SPhilippe Mathieu-Daudé  * www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/
64cd6c9977SPhilippe Mathieu-Daudé  */
65cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, REVISION,           0, 4);
66cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, TYPE,               4, 8);
67cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, PROCESSOR,         12, 4);
68cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, MANUFACTURER,      16, 4);
69cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, MEMORY_SIZE,       20, 3);
70cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, STYLE,             23, 1);
71cd6c9977SPhilippe Mathieu-Daudé 
72696788d6SPhilippe Mathieu-Daudé typedef enum RaspiProcessorId {
73696788d6SPhilippe Mathieu-Daudé     PROCESSOR_ID_BCM2836 = 1,
74696788d6SPhilippe Mathieu-Daudé     PROCESSOR_ID_BCM2837 = 2,
75696788d6SPhilippe Mathieu-Daudé } RaspiProcessorId;
76696788d6SPhilippe Mathieu-Daudé 
77696788d6SPhilippe Mathieu-Daudé static const struct {
78696788d6SPhilippe Mathieu-Daudé     const char *type;
79696788d6SPhilippe Mathieu-Daudé     int cores_count;
80696788d6SPhilippe Mathieu-Daudé } soc_property[] = {
81696788d6SPhilippe Mathieu-Daudé     [PROCESSOR_ID_BCM2836] = {TYPE_BCM2836, BCM283X_NCPUS},
82696788d6SPhilippe Mathieu-Daudé     [PROCESSOR_ID_BCM2837] = {TYPE_BCM2837, BCM283X_NCPUS},
83696788d6SPhilippe Mathieu-Daudé };
84696788d6SPhilippe Mathieu-Daudé 
85f5bb124eSPhilippe Mathieu-Daudé static uint64_t board_ram_size(uint32_t board_rev)
86f5bb124eSPhilippe Mathieu-Daudé {
87f5bb124eSPhilippe Mathieu-Daudé     assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */
88f5bb124eSPhilippe Mathieu-Daudé     return 256 * MiB << FIELD_EX32(board_rev, REV_CODE, MEMORY_SIZE);
89f5bb124eSPhilippe Mathieu-Daudé }
90f5bb124eSPhilippe Mathieu-Daudé 
91696788d6SPhilippe Mathieu-Daudé static RaspiProcessorId board_processor_id(uint32_t board_rev)
92cd6c9977SPhilippe Mathieu-Daudé {
93696788d6SPhilippe Mathieu-Daudé     int proc_id = FIELD_EX32(board_rev, REV_CODE, PROCESSOR);
94696788d6SPhilippe Mathieu-Daudé 
95cd6c9977SPhilippe Mathieu-Daudé     assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */
96696788d6SPhilippe Mathieu-Daudé     assert(proc_id < ARRAY_SIZE(soc_property) && soc_property[proc_id].type);
97696788d6SPhilippe Mathieu-Daudé 
98696788d6SPhilippe Mathieu-Daudé     return proc_id;
99cd6c9977SPhilippe Mathieu-Daudé }
100cd6c9977SPhilippe Mathieu-Daudé 
1012e664b45SPhilippe Mathieu-Daudé static const char *board_soc_type(uint32_t board_rev)
1022e664b45SPhilippe Mathieu-Daudé {
103696788d6SPhilippe Mathieu-Daudé     return soc_property[board_processor_id(board_rev)].type;
1042e664b45SPhilippe Mathieu-Daudé }
1052e664b45SPhilippe Mathieu-Daudé 
106759f0f87SPhilippe Mathieu-Daudé static int cores_count(uint32_t board_rev)
107759f0f87SPhilippe Mathieu-Daudé {
108696788d6SPhilippe Mathieu-Daudé     return soc_property[board_processor_id(board_rev)].cores_count;
109759f0f87SPhilippe Mathieu-Daudé }
110759f0f87SPhilippe Mathieu-Daudé 
11198b541e1SPhilippe Mathieu-Daudé static const char *board_type(uint32_t board_rev)
11298b541e1SPhilippe Mathieu-Daudé {
11398b541e1SPhilippe Mathieu-Daudé     static const char *types[] = {
11498b541e1SPhilippe Mathieu-Daudé         "A", "B", "A+", "B+", "2B", "Alpha", "CM1", NULL, "3B", "Zero",
11598b541e1SPhilippe Mathieu-Daudé         "CM3", NULL, "Zero W", "3B+", "3A+", NULL, "CM3+", "4B",
11698b541e1SPhilippe Mathieu-Daudé     };
11798b541e1SPhilippe Mathieu-Daudé     assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */
11898b541e1SPhilippe Mathieu-Daudé     int bt = FIELD_EX32(board_rev, REV_CODE, TYPE);
11998b541e1SPhilippe Mathieu-Daudé     if (bt >= ARRAY_SIZE(types) || !types[bt]) {
12098b541e1SPhilippe Mathieu-Daudé         return "Unknown";
12198b541e1SPhilippe Mathieu-Daudé     }
12298b541e1SPhilippe Mathieu-Daudé     return types[bt];
12398b541e1SPhilippe Mathieu-Daudé }
12498b541e1SPhilippe Mathieu-Daudé 
1251df7d1f9SAndrew Baumann static void write_smpboot(ARMCPU *cpu, const struct arm_boot_info *info)
1261df7d1f9SAndrew Baumann {
1271df7d1f9SAndrew Baumann     static const uint32_t smpboot[] = {
1281df7d1f9SAndrew Baumann         0xe1a0e00f, /*    mov     lr, pc */
1291df7d1f9SAndrew Baumann         0xe3a0fe00 + (BOARDSETUP_ADDR >> 4), /* mov pc, BOARDSETUP_ADDR */
1301df7d1f9SAndrew Baumann         0xee100fb0, /*    mrc     p15, 0, r0, c0, c0, 5;get core ID */
1311df7d1f9SAndrew Baumann         0xe7e10050, /*    ubfx    r0, r0, #0, #2       ;extract LSB */
1321df7d1f9SAndrew Baumann         0xe59f5014, /*    ldr     r5, =0x400000CC      ;load mbox base */
1331df7d1f9SAndrew Baumann         0xe320f001, /* 1: yield */
1341df7d1f9SAndrew Baumann         0xe7953200, /*    ldr     r3, [r5, r0, lsl #4] ;read mbox for our core*/
1351df7d1f9SAndrew Baumann         0xe3530000, /*    cmp     r3, #0               ;spin while zero */
1361df7d1f9SAndrew Baumann         0x0afffffb, /*    beq     1b */
1371df7d1f9SAndrew Baumann         0xe7853200, /*    str     r3, [r5, r0, lsl #4] ;clear mbox */
1381df7d1f9SAndrew Baumann         0xe12fff13, /*    bx      r3                   ;jump to target */
1391df7d1f9SAndrew Baumann         0x400000cc, /* (constant: mailbox 3 read/clear base) */
1401df7d1f9SAndrew Baumann     };
1411df7d1f9SAndrew Baumann 
1421df7d1f9SAndrew Baumann     /* check that we don't overrun board setup vectors */
1431df7d1f9SAndrew Baumann     QEMU_BUILD_BUG_ON(SMPBOOT_ADDR + sizeof(smpboot) > MVBAR_ADDR);
1441df7d1f9SAndrew Baumann     /* check that board setup address is correctly relocated */
1451df7d1f9SAndrew Baumann     QEMU_BUILD_BUG_ON((BOARDSETUP_ADDR & 0xf) != 0
1461df7d1f9SAndrew Baumann                       || (BOARDSETUP_ADDR >> 4) >= 0x100);
1471df7d1f9SAndrew Baumann 
1480f073693SPhilippe Mathieu-Daudé     rom_add_blob_fixed_as("raspi_smpboot", smpboot, sizeof(smpboot),
1490f073693SPhilippe Mathieu-Daudé                           info->smp_loader_start,
1500f073693SPhilippe Mathieu-Daudé                           arm_boot_address_space(cpu, info));
1511df7d1f9SAndrew Baumann }
1521df7d1f9SAndrew Baumann 
153ff72cb6bSPeter Maydell static void write_smpboot64(ARMCPU *cpu, const struct arm_boot_info *info)
154ff72cb6bSPeter Maydell {
1550f073693SPhilippe Mathieu-Daudé     AddressSpace *as = arm_boot_address_space(cpu, info);
156ff72cb6bSPeter Maydell     /* Unlike the AArch32 version we don't need to call the board setup hook.
157ff72cb6bSPeter Maydell      * The mechanism for doing the spin-table is also entirely different.
158ff72cb6bSPeter Maydell      * We must have four 64-bit fields at absolute addresses
159ff72cb6bSPeter Maydell      * 0xd8, 0xe0, 0xe8, 0xf0 in RAM, which are the flag variables for
160ff72cb6bSPeter Maydell      * our CPUs, and which we must ensure are zero initialized before
161ff72cb6bSPeter Maydell      * the primary CPU goes into the kernel. We put these variables inside
162ff72cb6bSPeter Maydell      * a rom blob, so that the reset for ROM contents zeroes them for us.
163ff72cb6bSPeter Maydell      */
164ff72cb6bSPeter Maydell     static const uint32_t smpboot[] = {
165ff72cb6bSPeter Maydell         0xd2801b05, /*        mov     x5, 0xd8 */
166ff72cb6bSPeter Maydell         0xd53800a6, /*        mrs     x6, mpidr_el1 */
167ff72cb6bSPeter Maydell         0x924004c6, /*        and     x6, x6, #0x3 */
168ff72cb6bSPeter Maydell         0xd503205f, /* spin:  wfe */
169ff72cb6bSPeter Maydell         0xf86678a4, /*        ldr     x4, [x5,x6,lsl #3] */
170ff72cb6bSPeter Maydell         0xb4ffffc4, /*        cbz     x4, spin */
171ff72cb6bSPeter Maydell         0xd2800000, /*        mov     x0, #0x0 */
172ff72cb6bSPeter Maydell         0xd2800001, /*        mov     x1, #0x0 */
173ff72cb6bSPeter Maydell         0xd2800002, /*        mov     x2, #0x0 */
174ff72cb6bSPeter Maydell         0xd2800003, /*        mov     x3, #0x0 */
175ff72cb6bSPeter Maydell         0xd61f0080, /*        br      x4 */
176ff72cb6bSPeter Maydell     };
177ff72cb6bSPeter Maydell 
178ff72cb6bSPeter Maydell     static const uint64_t spintables[] = {
179ff72cb6bSPeter Maydell         0, 0, 0, 0
180ff72cb6bSPeter Maydell     };
181ff72cb6bSPeter Maydell 
1820f073693SPhilippe Mathieu-Daudé     rom_add_blob_fixed_as("raspi_smpboot", smpboot, sizeof(smpboot),
1830f073693SPhilippe Mathieu-Daudé                           info->smp_loader_start, as);
1840f073693SPhilippe Mathieu-Daudé     rom_add_blob_fixed_as("raspi_spintables", spintables, sizeof(spintables),
1850f073693SPhilippe Mathieu-Daudé                           SPINTABLE_ADDR, as);
186ff72cb6bSPeter Maydell }
187ff72cb6bSPeter Maydell 
1881df7d1f9SAndrew Baumann static void write_board_setup(ARMCPU *cpu, const struct arm_boot_info *info)
1891df7d1f9SAndrew Baumann {
1901df7d1f9SAndrew Baumann     arm_write_secure_board_setup_dummy_smc(cpu, info, MVBAR_ADDR);
1911df7d1f9SAndrew Baumann }
1921df7d1f9SAndrew Baumann 
1931df7d1f9SAndrew Baumann static void reset_secondary(ARMCPU *cpu, const struct arm_boot_info *info)
1941df7d1f9SAndrew Baumann {
1951df7d1f9SAndrew Baumann     CPUState *cs = CPU(cpu);
1961df7d1f9SAndrew Baumann     cpu_set_pc(cs, info->smp_loader_start);
1971df7d1f9SAndrew Baumann }
1981df7d1f9SAndrew Baumann 
199*cdfaa57dSPhilippe Mathieu-Daudé static void setup_boot(MachineState *machine, RaspiProcessorId processor_id,
200*cdfaa57dSPhilippe Mathieu-Daudé                        size_t ram_size)
2011df7d1f9SAndrew Baumann {
20202058e4bSPhilippe Mathieu-Daudé     RaspiMachineState *s = RASPI_MACHINE(machine);
2031df7d1f9SAndrew Baumann     int r;
2041df7d1f9SAndrew Baumann 
2050f15c6e3SPhilippe Mathieu-Daudé     s->binfo.board_id = MACH_TYPE_BCM2708;
2060f15c6e3SPhilippe Mathieu-Daudé     s->binfo.ram_size = ram_size;
2070f15c6e3SPhilippe Mathieu-Daudé     s->binfo.nb_cpus = machine->smp.cpus;
20801e02f5aSPeter Maydell 
209*cdfaa57dSPhilippe Mathieu-Daudé     if (processor_id <= PROCESSOR_ID_BCM2836) {
210*cdfaa57dSPhilippe Mathieu-Daudé         /*
211*cdfaa57dSPhilippe Mathieu-Daudé          * The BCM2835 and BCM2836 require some custom setup code to run
212*cdfaa57dSPhilippe Mathieu-Daudé          * in Secure mode before booting a kernel (to set up the SMC vectors
213*cdfaa57dSPhilippe Mathieu-Daudé          * so that we get a no-op SMC; this is used by Linux to call the
21401e02f5aSPeter Maydell          * firmware for some cache maintenance operations.
215*cdfaa57dSPhilippe Mathieu-Daudé          * The BCM2837 doesn't need this.
21601e02f5aSPeter Maydell          */
2170f15c6e3SPhilippe Mathieu-Daudé         s->binfo.board_setup_addr = BOARDSETUP_ADDR;
2180f15c6e3SPhilippe Mathieu-Daudé         s->binfo.write_board_setup = write_board_setup;
2190f15c6e3SPhilippe Mathieu-Daudé         s->binfo.secure_board_setup = true;
2200f15c6e3SPhilippe Mathieu-Daudé         s->binfo.secure_boot = true;
22101e02f5aSPeter Maydell     }
2221df7d1f9SAndrew Baumann 
223*cdfaa57dSPhilippe Mathieu-Daudé     /* BCM2836 and BCM2837 requires SMP setup */
224*cdfaa57dSPhilippe Mathieu-Daudé     if (processor_id >= PROCESSOR_ID_BCM2836) {
2250f15c6e3SPhilippe Mathieu-Daudé         s->binfo.smp_loader_start = SMPBOOT_ADDR;
226*cdfaa57dSPhilippe Mathieu-Daudé         if (processor_id == PROCESSOR_ID_BCM2836) {
2270f15c6e3SPhilippe Mathieu-Daudé             s->binfo.write_secondary_boot = write_smpboot;
228ff72cb6bSPeter Maydell         } else {
2290f15c6e3SPhilippe Mathieu-Daudé             s->binfo.write_secondary_boot = write_smpboot64;
230ff72cb6bSPeter Maydell         }
2310f15c6e3SPhilippe Mathieu-Daudé         s->binfo.secondary_cpu_reset_hook = reset_secondary;
2321df7d1f9SAndrew Baumann     }
2331df7d1f9SAndrew Baumann 
2341df7d1f9SAndrew Baumann     /* If the user specified a "firmware" image (e.g. UEFI), we bypass
2351df7d1f9SAndrew Baumann      * the normal Linux boot process
2361df7d1f9SAndrew Baumann      */
2371df7d1f9SAndrew Baumann     if (machine->firmware) {
2381af70269SPhilippe Mathieu-Daudé         hwaddr firmware_addr = processor_id <= PROCESSOR_ID_BCM2836
2391af70269SPhilippe Mathieu-Daudé                              ? FIRMWARE_ADDR_2 : FIRMWARE_ADDR_3;
2401df7d1f9SAndrew Baumann         /* load the firmware image (typically kernel.img) */
241bade5816SPekka Enberg         r = load_image_targphys(machine->firmware, firmware_addr,
242bade5816SPekka Enberg                                 ram_size - firmware_addr);
2431df7d1f9SAndrew Baumann         if (r < 0) {
2441df7d1f9SAndrew Baumann             error_report("Failed to load firmware from %s", machine->firmware);
2451df7d1f9SAndrew Baumann             exit(1);
2461df7d1f9SAndrew Baumann         }
2471df7d1f9SAndrew Baumann 
2480f15c6e3SPhilippe Mathieu-Daudé         s->binfo.entry = firmware_addr;
2490f15c6e3SPhilippe Mathieu-Daudé         s->binfo.firmware_loaded = true;
2501df7d1f9SAndrew Baumann     }
2511df7d1f9SAndrew Baumann 
2520f15c6e3SPhilippe Mathieu-Daudé     arm_load_kernel(&s->soc.cpu[0].core, machine, &s->binfo);
2531df7d1f9SAndrew Baumann }
2541df7d1f9SAndrew Baumann 
25513c4e2c0SPhilippe Mathieu-Daudé static void raspi_machine_init(MachineState *machine)
2561df7d1f9SAndrew Baumann {
257c318c66cSPhilippe Mathieu-Daudé     RaspiMachineClass *mc = RASPI_MACHINE_GET_CLASS(machine);
258cb57df6fSPhilippe Mathieu-Daudé     RaspiMachineState *s = RASPI_MACHINE(machine);
259c318c66cSPhilippe Mathieu-Daudé     uint32_t board_rev = mc->board_rev;
260f5bb124eSPhilippe Mathieu-Daudé     uint64_t ram_size = board_ram_size(board_rev);
2615e9c2a8dSGrégory ESTRADE     uint32_t vcram_size;
262a55b53a2SAndrew Baumann     DriveInfo *di;
263a55b53a2SAndrew Baumann     BlockBackend *blk;
264a55b53a2SAndrew Baumann     BusState *bus;
265a55b53a2SAndrew Baumann     DeviceState *carddev;
2661df7d1f9SAndrew Baumann 
267f5bb124eSPhilippe Mathieu-Daudé     if (machine->ram_size != ram_size) {
268f5bb124eSPhilippe Mathieu-Daudé         char *size_str = size_to_str(ram_size);
269f5bb124eSPhilippe Mathieu-Daudé         error_report("Invalid RAM size, should be %s", size_str);
270f5bb124eSPhilippe Mathieu-Daudé         g_free(size_str);
271ff3dcf28SPeter Maydell         exit(1);
272ff3dcf28SPeter Maydell     }
273ff3dcf28SPeter Maydell 
2741df7d1f9SAndrew Baumann     /* FIXME: Remove when we have custom CPU address space support */
275a4317ae8SIgor Mammedov     memory_region_add_subregion_overlap(get_system_memory(), 0,
276a4317ae8SIgor Mammedov                                         machine->ram, 0);
2771df7d1f9SAndrew Baumann 
2781df7d1f9SAndrew Baumann     /* Setup the SOC */
2799fc7fc4dSMarkus Armbruster     object_initialize_child(OBJECT(machine), "soc", &s->soc,
2809fc7fc4dSMarkus Armbruster                             board_soc_type(board_rev));
281d2623129SMarkus Armbruster     object_property_add_const_link(OBJECT(&s->soc), "ram", OBJECT(machine->ram));
2825325cc34SMarkus Armbruster     object_property_set_int(OBJECT(&s->soc), "board-rev", board_rev,
283f0afa731SStephen Warren                             &error_abort);
284ce189ab2SMarkus Armbruster     qdev_realize(DEVICE(&s->soc), NULL, &error_abort);
2851df7d1f9SAndrew Baumann 
286a55b53a2SAndrew Baumann     /* Create and plug in the SD cards */
287a55b53a2SAndrew Baumann     di = drive_get_next(IF_SD);
288a55b53a2SAndrew Baumann     blk = di ? blk_by_legacy_dinfo(di) : NULL;
289a55b53a2SAndrew Baumann     bus = qdev_get_child_bus(DEVICE(&s->soc), "sd-bus");
290a55b53a2SAndrew Baumann     if (bus == NULL) {
291a55b53a2SAndrew Baumann         error_report("No SD bus found in SOC object");
292a55b53a2SAndrew Baumann         exit(1);
293a55b53a2SAndrew Baumann     }
2943e80f690SMarkus Armbruster     carddev = qdev_new(TYPE_SD_CARD);
295934df912SMarkus Armbruster     qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal);
2963e80f690SMarkus Armbruster     qdev_realize_and_unref(carddev, bus, &error_fatal);
297a55b53a2SAndrew Baumann 
298c5c6c47cSMarc-André Lureau     vcram_size = object_property_get_uint(OBJECT(&s->soc), "vcram-size",
2995e9c2a8dSGrégory ESTRADE                                           &error_abort);
300*cdfaa57dSPhilippe Mathieu-Daudé     setup_boot(machine, board_processor_id(mc->board_rev),
301*cdfaa57dSPhilippe Mathieu-Daudé                machine->ram_size - vcram_size);
302bade5816SPekka Enberg }
303bade5816SPekka Enberg 
304f0eeb4b6SPhilippe Mathieu-Daudé static void raspi_machine_class_common_init(MachineClass *mc,
305f0eeb4b6SPhilippe Mathieu-Daudé                                             uint32_t board_rev)
3061df7d1f9SAndrew Baumann {
30762f06f71SPhilippe Mathieu-Daudé     mc->desc = g_strdup_printf("Raspberry Pi %s (revision 1.%u)",
30862f06f71SPhilippe Mathieu-Daudé                                board_type(board_rev),
30962f06f71SPhilippe Mathieu-Daudé                                FIELD_EX32(board_rev, REV_CODE, REVISION));
31013c4e2c0SPhilippe Mathieu-Daudé     mc->init = raspi_machine_init;
3111df7d1f9SAndrew Baumann     mc->block_default_type = IF_SD;
3121df7d1f9SAndrew Baumann     mc->no_parallel = 1;
3131df7d1f9SAndrew Baumann     mc->no_floppy = 1;
3141df7d1f9SAndrew Baumann     mc->no_cdrom = 1;
315759f0f87SPhilippe Mathieu-Daudé     mc->default_cpus = mc->min_cpus = mc->max_cpus = cores_count(board_rev);
316975f3402SPhilippe Mathieu-Daudé     mc->default_ram_size = board_ram_size(board_rev);
317a4317ae8SIgor Mammedov     mc->default_ram_id = "ram";
318a03bde36SPhilippe Mathieu-Daudé };
319cb57df6fSPhilippe Mathieu-Daudé 
320f0eeb4b6SPhilippe Mathieu-Daudé static void raspi2b_machine_class_init(ObjectClass *oc, void *data)
321f0eeb4b6SPhilippe Mathieu-Daudé {
322f0eeb4b6SPhilippe Mathieu-Daudé     MachineClass *mc = MACHINE_CLASS(oc);
323f0eeb4b6SPhilippe Mathieu-Daudé     RaspiMachineClass *rmc = RASPI_MACHINE_CLASS(oc);
324f0eeb4b6SPhilippe Mathieu-Daudé 
325aa35ec22SPhilippe Mathieu-Daudé     mc->alias = "raspi2";
326f0eeb4b6SPhilippe Mathieu-Daudé     rmc->board_rev = 0xa21041;
327f0eeb4b6SPhilippe Mathieu-Daudé     raspi_machine_class_common_init(mc, rmc->board_rev);
328f0eeb4b6SPhilippe Mathieu-Daudé };
329f0eeb4b6SPhilippe Mathieu-Daudé 
330f0eeb4b6SPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64
331f0eeb4b6SPhilippe Mathieu-Daudé static void raspi3b_machine_class_init(ObjectClass *oc, void *data)
332f0eeb4b6SPhilippe Mathieu-Daudé {
333f0eeb4b6SPhilippe Mathieu-Daudé     MachineClass *mc = MACHINE_CLASS(oc);
334f0eeb4b6SPhilippe Mathieu-Daudé     RaspiMachineClass *rmc = RASPI_MACHINE_CLASS(oc);
335f0eeb4b6SPhilippe Mathieu-Daudé 
336aa35ec22SPhilippe Mathieu-Daudé     mc->alias = "raspi3";
337f0eeb4b6SPhilippe Mathieu-Daudé     rmc->board_rev = 0xa02082;
338f0eeb4b6SPhilippe Mathieu-Daudé     raspi_machine_class_common_init(mc, rmc->board_rev);
339f0eeb4b6SPhilippe Mathieu-Daudé };
340f0eeb4b6SPhilippe Mathieu-Daudé #endif /* TARGET_AARCH64 */
341f0eeb4b6SPhilippe Mathieu-Daudé 
342cb57df6fSPhilippe Mathieu-Daudé static const TypeInfo raspi_machine_types[] = {
343cb57df6fSPhilippe Mathieu-Daudé     {
344aa35ec22SPhilippe Mathieu-Daudé         .name           = MACHINE_TYPE_NAME("raspi2b"),
345cb57df6fSPhilippe Mathieu-Daudé         .parent         = TYPE_RASPI_MACHINE,
346f0eeb4b6SPhilippe Mathieu-Daudé         .class_init     = raspi2b_machine_class_init,
347cb57df6fSPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64
348cb57df6fSPhilippe Mathieu-Daudé     }, {
349aa35ec22SPhilippe Mathieu-Daudé         .name           = MACHINE_TYPE_NAME("raspi3b"),
350cb57df6fSPhilippe Mathieu-Daudé         .parent         = TYPE_RASPI_MACHINE,
351f0eeb4b6SPhilippe Mathieu-Daudé         .class_init     = raspi3b_machine_class_init,
352cb57df6fSPhilippe Mathieu-Daudé #endif
353cb57df6fSPhilippe Mathieu-Daudé     }, {
354cb57df6fSPhilippe Mathieu-Daudé         .name           = TYPE_RASPI_MACHINE,
355cb57df6fSPhilippe Mathieu-Daudé         .parent         = TYPE_MACHINE,
356cb57df6fSPhilippe Mathieu-Daudé         .instance_size  = sizeof(RaspiMachineState),
357cb57df6fSPhilippe Mathieu-Daudé         .class_size     = sizeof(RaspiMachineClass),
358cb57df6fSPhilippe Mathieu-Daudé         .abstract       = true,
359cb57df6fSPhilippe Mathieu-Daudé     }
360cb57df6fSPhilippe Mathieu-Daudé };
361cb57df6fSPhilippe Mathieu-Daudé 
362cb57df6fSPhilippe Mathieu-Daudé DEFINE_TYPES(raspi_machine_types)
363