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" 191df7d1f9SAndrew Baumann #include "hw/arm/bcm2836.h" 20cd6c9977SPhilippe Mathieu-Daudé #include "hw/registerfields.h" 211df7d1f9SAndrew Baumann #include "qemu/error-report.h" 221df7d1f9SAndrew Baumann #include "hw/boards.h" 231df7d1f9SAndrew Baumann #include "hw/loader.h" 2412ec8bd5SPeter Maydell #include "hw/arm/boot.h" 25db1015e9SEduardo Habkost #include "qom/object.h" 261df7d1f9SAndrew Baumann 271df7d1f9SAndrew Baumann #define SMPBOOT_ADDR 0x300 /* this should leave enough space for ATAGS */ 281df7d1f9SAndrew Baumann #define MVBAR_ADDR 0x400 /* secure vectors */ 291df7d1f9SAndrew Baumann #define BOARDSETUP_ADDR (MVBAR_ADDR + 0x20) /* board setup code */ 30bade5816SPekka Enberg #define FIRMWARE_ADDR_2 0x8000 /* Pi 2 loads kernel.img here by default */ 31bade5816SPekka Enberg #define FIRMWARE_ADDR_3 0x80000 /* Pi 3 loads kernel.img here by default */ 32ff72cb6bSPeter Maydell #define SPINTABLE_ADDR 0xd8 /* Pi 3 bootloader spintable */ 331df7d1f9SAndrew Baumann 34918c81a5SPhilippe Mathieu-Daudé /* Registered machine type (matches RPi Foundation bootloader and U-Boot) */ 35918c81a5SPhilippe Mathieu-Daudé #define MACH_TYPE_BCM2708 3138 361df7d1f9SAndrew Baumann 37db1015e9SEduardo Habkost struct RaspiMachineState { 38cb57df6fSPhilippe Mathieu-Daudé /*< private >*/ 39cb57df6fSPhilippe Mathieu-Daudé MachineState parent_obj; 40cb57df6fSPhilippe Mathieu-Daudé /*< public >*/ 41926dcdf0SPeter Maydell BCM283XState soc; 420f15c6e3SPhilippe Mathieu-Daudé struct arm_boot_info binfo; 43db1015e9SEduardo Habkost }; 44db1015e9SEduardo Habkost typedef struct RaspiMachineState RaspiMachineState; 45cb57df6fSPhilippe Mathieu-Daudé 46db1015e9SEduardo Habkost struct RaspiMachineClass { 47cb57df6fSPhilippe Mathieu-Daudé /*< private >*/ 48cb57df6fSPhilippe Mathieu-Daudé MachineClass parent_obj; 49cb57df6fSPhilippe Mathieu-Daudé /*< public >*/ 50c318c66cSPhilippe Mathieu-Daudé uint32_t board_rev; 51db1015e9SEduardo Habkost }; 52db1015e9SEduardo Habkost typedef struct RaspiMachineClass RaspiMachineClass; 53cb57df6fSPhilippe Mathieu-Daudé 54cb57df6fSPhilippe Mathieu-Daudé #define TYPE_RASPI_MACHINE MACHINE_TYPE_NAME("raspi-common") 558110fa1dSEduardo Habkost DECLARE_OBJ_CHECKERS(RaspiMachineState, RaspiMachineClass, 568110fa1dSEduardo Habkost RASPI_MACHINE, TYPE_RASPI_MACHINE) 57cb57df6fSPhilippe Mathieu-Daudé 581df7d1f9SAndrew Baumann 59cd6c9977SPhilippe Mathieu-Daudé /* 60cd6c9977SPhilippe Mathieu-Daudé * Board revision codes: 61cd6c9977SPhilippe Mathieu-Daudé * www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/ 62cd6c9977SPhilippe Mathieu-Daudé */ 63cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, REVISION, 0, 4); 64cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, TYPE, 4, 8); 65cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, PROCESSOR, 12, 4); 66cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, MANUFACTURER, 16, 4); 67cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, MEMORY_SIZE, 20, 3); 68cd6c9977SPhilippe Mathieu-Daudé FIELD(REV_CODE, STYLE, 23, 1); 69cd6c9977SPhilippe Mathieu-Daudé 70696788d6SPhilippe Mathieu-Daudé typedef enum RaspiProcessorId { 71df6cf08dSPhilippe Mathieu-Daudé PROCESSOR_ID_BCM2835 = 0, 72696788d6SPhilippe Mathieu-Daudé PROCESSOR_ID_BCM2836 = 1, 73696788d6SPhilippe Mathieu-Daudé PROCESSOR_ID_BCM2837 = 2, 74696788d6SPhilippe Mathieu-Daudé } RaspiProcessorId; 75696788d6SPhilippe Mathieu-Daudé 76696788d6SPhilippe Mathieu-Daudé static const struct { 77696788d6SPhilippe Mathieu-Daudé const char *type; 78696788d6SPhilippe Mathieu-Daudé int cores_count; 79696788d6SPhilippe Mathieu-Daudé } soc_property[] = { 80df6cf08dSPhilippe Mathieu-Daudé [PROCESSOR_ID_BCM2835] = {TYPE_BCM2835, 1}, 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 199cdfaa57dSPhilippe Mathieu-Daudé static void setup_boot(MachineState *machine, RaspiProcessorId processor_id, 200cdfaa57dSPhilippe 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; 20701e02f5aSPeter Maydell 208cdfaa57dSPhilippe Mathieu-Daudé if (processor_id <= PROCESSOR_ID_BCM2836) { 209cdfaa57dSPhilippe Mathieu-Daudé /* 210cdfaa57dSPhilippe Mathieu-Daudé * The BCM2835 and BCM2836 require some custom setup code to run 211cdfaa57dSPhilippe Mathieu-Daudé * in Secure mode before booting a kernel (to set up the SMC vectors 212cdfaa57dSPhilippe Mathieu-Daudé * so that we get a no-op SMC; this is used by Linux to call the 21301e02f5aSPeter Maydell * firmware for some cache maintenance operations. 214cdfaa57dSPhilippe Mathieu-Daudé * The BCM2837 doesn't need this. 21501e02f5aSPeter Maydell */ 2160f15c6e3SPhilippe Mathieu-Daudé s->binfo.board_setup_addr = BOARDSETUP_ADDR; 2170f15c6e3SPhilippe Mathieu-Daudé s->binfo.write_board_setup = write_board_setup; 2180f15c6e3SPhilippe Mathieu-Daudé s->binfo.secure_board_setup = true; 2190f15c6e3SPhilippe Mathieu-Daudé s->binfo.secure_boot = true; 22001e02f5aSPeter Maydell } 2211df7d1f9SAndrew Baumann 222cdfaa57dSPhilippe Mathieu-Daudé /* BCM2836 and BCM2837 requires SMP setup */ 223cdfaa57dSPhilippe Mathieu-Daudé if (processor_id >= PROCESSOR_ID_BCM2836) { 2240f15c6e3SPhilippe Mathieu-Daudé s->binfo.smp_loader_start = SMPBOOT_ADDR; 225cdfaa57dSPhilippe Mathieu-Daudé if (processor_id == PROCESSOR_ID_BCM2836) { 2260f15c6e3SPhilippe Mathieu-Daudé s->binfo.write_secondary_boot = write_smpboot; 227ff72cb6bSPeter Maydell } else { 2280f15c6e3SPhilippe Mathieu-Daudé s->binfo.write_secondary_boot = write_smpboot64; 229ff72cb6bSPeter Maydell } 2300f15c6e3SPhilippe Mathieu-Daudé s->binfo.secondary_cpu_reset_hook = reset_secondary; 2311df7d1f9SAndrew Baumann } 2321df7d1f9SAndrew Baumann 2331df7d1f9SAndrew Baumann /* If the user specified a "firmware" image (e.g. UEFI), we bypass 2341df7d1f9SAndrew Baumann * the normal Linux boot process 2351df7d1f9SAndrew Baumann */ 2361df7d1f9SAndrew Baumann if (machine->firmware) { 2371af70269SPhilippe Mathieu-Daudé hwaddr firmware_addr = processor_id <= PROCESSOR_ID_BCM2836 2381af70269SPhilippe Mathieu-Daudé ? FIRMWARE_ADDR_2 : FIRMWARE_ADDR_3; 2391df7d1f9SAndrew Baumann /* load the firmware image (typically kernel.img) */ 240bade5816SPekka Enberg r = load_image_targphys(machine->firmware, firmware_addr, 241bade5816SPekka Enberg ram_size - firmware_addr); 2421df7d1f9SAndrew Baumann if (r < 0) { 2431df7d1f9SAndrew Baumann error_report("Failed to load firmware from %s", machine->firmware); 2441df7d1f9SAndrew Baumann exit(1); 2451df7d1f9SAndrew Baumann } 2461df7d1f9SAndrew Baumann 2470f15c6e3SPhilippe Mathieu-Daudé s->binfo.entry = firmware_addr; 2480f15c6e3SPhilippe Mathieu-Daudé s->binfo.firmware_loaded = true; 2491df7d1f9SAndrew Baumann } 2501df7d1f9SAndrew Baumann 2510f15c6e3SPhilippe Mathieu-Daudé arm_load_kernel(&s->soc.cpu[0].core, machine, &s->binfo); 2521df7d1f9SAndrew Baumann } 2531df7d1f9SAndrew Baumann 25413c4e2c0SPhilippe Mathieu-Daudé static void raspi_machine_init(MachineState *machine) 2551df7d1f9SAndrew Baumann { 256c318c66cSPhilippe Mathieu-Daudé RaspiMachineClass *mc = RASPI_MACHINE_GET_CLASS(machine); 257cb57df6fSPhilippe Mathieu-Daudé RaspiMachineState *s = RASPI_MACHINE(machine); 258c318c66cSPhilippe Mathieu-Daudé uint32_t board_rev = mc->board_rev; 259f5bb124eSPhilippe Mathieu-Daudé uint64_t ram_size = board_ram_size(board_rev); 2605e9c2a8dSGrégory ESTRADE uint32_t vcram_size; 261a55b53a2SAndrew Baumann DriveInfo *di; 262a55b53a2SAndrew Baumann BlockBackend *blk; 263a55b53a2SAndrew Baumann BusState *bus; 264a55b53a2SAndrew Baumann DeviceState *carddev; 2651df7d1f9SAndrew Baumann 266f5bb124eSPhilippe Mathieu-Daudé if (machine->ram_size != ram_size) { 267f5bb124eSPhilippe Mathieu-Daudé char *size_str = size_to_str(ram_size); 268f5bb124eSPhilippe Mathieu-Daudé error_report("Invalid RAM size, should be %s", size_str); 269f5bb124eSPhilippe Mathieu-Daudé g_free(size_str); 270ff3dcf28SPeter Maydell exit(1); 271ff3dcf28SPeter Maydell } 272ff3dcf28SPeter Maydell 2731df7d1f9SAndrew Baumann /* FIXME: Remove when we have custom CPU address space support */ 274a4317ae8SIgor Mammedov memory_region_add_subregion_overlap(get_system_memory(), 0, 275a4317ae8SIgor Mammedov machine->ram, 0); 2761df7d1f9SAndrew Baumann 2771df7d1f9SAndrew Baumann /* Setup the SOC */ 2789fc7fc4dSMarkus Armbruster object_initialize_child(OBJECT(machine), "soc", &s->soc, 2799fc7fc4dSMarkus Armbruster board_soc_type(board_rev)); 280d2623129SMarkus Armbruster object_property_add_const_link(OBJECT(&s->soc), "ram", OBJECT(machine->ram)); 2815325cc34SMarkus Armbruster object_property_set_int(OBJECT(&s->soc), "board-rev", board_rev, 282f0afa731SStephen Warren &error_abort); 283*f802ff1eSDaniel Bertalan object_property_set_str(OBJECT(&s->soc), "command-line", 284*f802ff1eSDaniel Bertalan machine->kernel_cmdline, &error_abort); 28533c20e3cSPeter Maydell qdev_realize(DEVICE(&s->soc), NULL, &error_fatal); 2861df7d1f9SAndrew Baumann 287a55b53a2SAndrew Baumann /* Create and plug in the SD cards */ 28864eaa820SMarkus Armbruster di = drive_get(IF_SD, 0, 0); 289a55b53a2SAndrew Baumann blk = di ? blk_by_legacy_dinfo(di) : NULL; 290a55b53a2SAndrew Baumann bus = qdev_get_child_bus(DEVICE(&s->soc), "sd-bus"); 291a55b53a2SAndrew Baumann if (bus == NULL) { 292a55b53a2SAndrew Baumann error_report("No SD bus found in SOC object"); 293a55b53a2SAndrew Baumann exit(1); 294a55b53a2SAndrew Baumann } 2953e80f690SMarkus Armbruster carddev = qdev_new(TYPE_SD_CARD); 296934df912SMarkus Armbruster qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal); 2973e80f690SMarkus Armbruster qdev_realize_and_unref(carddev, bus, &error_fatal); 298a55b53a2SAndrew Baumann 299c5c6c47cSMarc-André Lureau vcram_size = object_property_get_uint(OBJECT(&s->soc), "vcram-size", 3005e9c2a8dSGrégory ESTRADE &error_abort); 301cdfaa57dSPhilippe Mathieu-Daudé setup_boot(machine, board_processor_id(mc->board_rev), 302cdfaa57dSPhilippe Mathieu-Daudé machine->ram_size - vcram_size); 303bade5816SPekka Enberg } 304bade5816SPekka Enberg 305f0eeb4b6SPhilippe Mathieu-Daudé static void raspi_machine_class_common_init(MachineClass *mc, 306f0eeb4b6SPhilippe Mathieu-Daudé uint32_t board_rev) 3071df7d1f9SAndrew Baumann { 30862f06f71SPhilippe Mathieu-Daudé mc->desc = g_strdup_printf("Raspberry Pi %s (revision 1.%u)", 30962f06f71SPhilippe Mathieu-Daudé board_type(board_rev), 31062f06f71SPhilippe Mathieu-Daudé FIELD_EX32(board_rev, REV_CODE, REVISION)); 31113c4e2c0SPhilippe Mathieu-Daudé mc->init = raspi_machine_init; 3121df7d1f9SAndrew Baumann mc->block_default_type = IF_SD; 3131df7d1f9SAndrew Baumann mc->no_parallel = 1; 3141df7d1f9SAndrew Baumann mc->no_floppy = 1; 3151df7d1f9SAndrew Baumann mc->no_cdrom = 1; 316759f0f87SPhilippe Mathieu-Daudé mc->default_cpus = mc->min_cpus = mc->max_cpus = cores_count(board_rev); 317975f3402SPhilippe Mathieu-Daudé mc->default_ram_size = board_ram_size(board_rev); 318a4317ae8SIgor Mammedov mc->default_ram_id = "ram"; 319a03bde36SPhilippe Mathieu-Daudé }; 320cb57df6fSPhilippe Mathieu-Daudé 3213c8f9927SPhilippe Mathieu-Daudé static void raspi0_machine_class_init(ObjectClass *oc, void *data) 3223c8f9927SPhilippe Mathieu-Daudé { 3233c8f9927SPhilippe Mathieu-Daudé MachineClass *mc = MACHINE_CLASS(oc); 3243c8f9927SPhilippe Mathieu-Daudé RaspiMachineClass *rmc = RASPI_MACHINE_CLASS(oc); 3253c8f9927SPhilippe Mathieu-Daudé 3263c8f9927SPhilippe Mathieu-Daudé rmc->board_rev = 0x920092; /* Revision 1.2 */ 3273c8f9927SPhilippe Mathieu-Daudé raspi_machine_class_common_init(mc, rmc->board_rev); 3283c8f9927SPhilippe Mathieu-Daudé }; 3293c8f9927SPhilippe Mathieu-Daudé 330ac6bc6ebSPhilippe Mathieu-Daudé static void raspi1ap_machine_class_init(ObjectClass *oc, void *data) 331ac6bc6ebSPhilippe Mathieu-Daudé { 332ac6bc6ebSPhilippe Mathieu-Daudé MachineClass *mc = MACHINE_CLASS(oc); 333ac6bc6ebSPhilippe Mathieu-Daudé RaspiMachineClass *rmc = RASPI_MACHINE_CLASS(oc); 334ac6bc6ebSPhilippe Mathieu-Daudé 335ac6bc6ebSPhilippe Mathieu-Daudé rmc->board_rev = 0x900021; /* Revision 1.1 */ 336ac6bc6ebSPhilippe Mathieu-Daudé raspi_machine_class_common_init(mc, rmc->board_rev); 337ac6bc6ebSPhilippe Mathieu-Daudé }; 338ac6bc6ebSPhilippe Mathieu-Daudé 339f0eeb4b6SPhilippe Mathieu-Daudé static void raspi2b_machine_class_init(ObjectClass *oc, void *data) 340f0eeb4b6SPhilippe Mathieu-Daudé { 341f0eeb4b6SPhilippe Mathieu-Daudé MachineClass *mc = MACHINE_CLASS(oc); 342f0eeb4b6SPhilippe Mathieu-Daudé RaspiMachineClass *rmc = RASPI_MACHINE_CLASS(oc); 343f0eeb4b6SPhilippe Mathieu-Daudé 344f0eeb4b6SPhilippe Mathieu-Daudé rmc->board_rev = 0xa21041; 345f0eeb4b6SPhilippe Mathieu-Daudé raspi_machine_class_common_init(mc, rmc->board_rev); 346f0eeb4b6SPhilippe Mathieu-Daudé }; 347f0eeb4b6SPhilippe Mathieu-Daudé 348f0eeb4b6SPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64 3495be94252SPhilippe Mathieu-Daudé static void raspi3ap_machine_class_init(ObjectClass *oc, void *data) 3505be94252SPhilippe Mathieu-Daudé { 3515be94252SPhilippe Mathieu-Daudé MachineClass *mc = MACHINE_CLASS(oc); 3525be94252SPhilippe Mathieu-Daudé RaspiMachineClass *rmc = RASPI_MACHINE_CLASS(oc); 3535be94252SPhilippe Mathieu-Daudé 3545be94252SPhilippe Mathieu-Daudé rmc->board_rev = 0x9020e0; /* Revision 1.0 */ 3555be94252SPhilippe Mathieu-Daudé raspi_machine_class_common_init(mc, rmc->board_rev); 3565be94252SPhilippe Mathieu-Daudé }; 3575be94252SPhilippe Mathieu-Daudé 358f0eeb4b6SPhilippe Mathieu-Daudé static void raspi3b_machine_class_init(ObjectClass *oc, void *data) 359f0eeb4b6SPhilippe Mathieu-Daudé { 360f0eeb4b6SPhilippe Mathieu-Daudé MachineClass *mc = MACHINE_CLASS(oc); 361f0eeb4b6SPhilippe Mathieu-Daudé RaspiMachineClass *rmc = RASPI_MACHINE_CLASS(oc); 362f0eeb4b6SPhilippe Mathieu-Daudé 363f0eeb4b6SPhilippe Mathieu-Daudé rmc->board_rev = 0xa02082; 364f0eeb4b6SPhilippe Mathieu-Daudé raspi_machine_class_common_init(mc, rmc->board_rev); 365f0eeb4b6SPhilippe Mathieu-Daudé }; 366f0eeb4b6SPhilippe Mathieu-Daudé #endif /* TARGET_AARCH64 */ 367f0eeb4b6SPhilippe Mathieu-Daudé 368cb57df6fSPhilippe Mathieu-Daudé static const TypeInfo raspi_machine_types[] = { 369cb57df6fSPhilippe Mathieu-Daudé { 3703c8f9927SPhilippe Mathieu-Daudé .name = MACHINE_TYPE_NAME("raspi0"), 3713c8f9927SPhilippe Mathieu-Daudé .parent = TYPE_RASPI_MACHINE, 3723c8f9927SPhilippe Mathieu-Daudé .class_init = raspi0_machine_class_init, 3733c8f9927SPhilippe Mathieu-Daudé }, { 374ac6bc6ebSPhilippe Mathieu-Daudé .name = MACHINE_TYPE_NAME("raspi1ap"), 375ac6bc6ebSPhilippe Mathieu-Daudé .parent = TYPE_RASPI_MACHINE, 376ac6bc6ebSPhilippe Mathieu-Daudé .class_init = raspi1ap_machine_class_init, 377ac6bc6ebSPhilippe Mathieu-Daudé }, { 378aa35ec22SPhilippe Mathieu-Daudé .name = MACHINE_TYPE_NAME("raspi2b"), 379cb57df6fSPhilippe Mathieu-Daudé .parent = TYPE_RASPI_MACHINE, 380f0eeb4b6SPhilippe Mathieu-Daudé .class_init = raspi2b_machine_class_init, 381cb57df6fSPhilippe Mathieu-Daudé #ifdef TARGET_AARCH64 382cb57df6fSPhilippe Mathieu-Daudé }, { 3835be94252SPhilippe Mathieu-Daudé .name = MACHINE_TYPE_NAME("raspi3ap"), 3845be94252SPhilippe Mathieu-Daudé .parent = TYPE_RASPI_MACHINE, 3855be94252SPhilippe Mathieu-Daudé .class_init = raspi3ap_machine_class_init, 3865be94252SPhilippe Mathieu-Daudé }, { 387aa35ec22SPhilippe Mathieu-Daudé .name = MACHINE_TYPE_NAME("raspi3b"), 388cb57df6fSPhilippe Mathieu-Daudé .parent = TYPE_RASPI_MACHINE, 389f0eeb4b6SPhilippe Mathieu-Daudé .class_init = raspi3b_machine_class_init, 390cb57df6fSPhilippe Mathieu-Daudé #endif 391cb57df6fSPhilippe Mathieu-Daudé }, { 392cb57df6fSPhilippe Mathieu-Daudé .name = TYPE_RASPI_MACHINE, 393cb57df6fSPhilippe Mathieu-Daudé .parent = TYPE_MACHINE, 394cb57df6fSPhilippe Mathieu-Daudé .instance_size = sizeof(RaspiMachineState), 395cb57df6fSPhilippe Mathieu-Daudé .class_size = sizeof(RaspiMachineClass), 396cb57df6fSPhilippe Mathieu-Daudé .abstract = true, 397cb57df6fSPhilippe Mathieu-Daudé } 398cb57df6fSPhilippe Mathieu-Daudé }; 399cb57df6fSPhilippe Mathieu-Daudé 400cb57df6fSPhilippe Mathieu-Daudé DEFINE_TYPES(raspi_machine_types) 401