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 * 111df7d1f9SAndrew Baumann * This code is licensed under the GNU GPLv2 and later. 121df7d1f9SAndrew Baumann */ 131df7d1f9SAndrew Baumann 14c964b660SPeter Maydell #include "qemu/osdep.h" 15ff3dcf28SPeter Maydell #include "qemu/units.h" 16da34e65cSMarkus Armbruster #include "qapi/error.h" 174771d756SPaolo Bonzini #include "cpu.h" 181df7d1f9SAndrew Baumann #include "hw/arm/bcm2836.h" 191df7d1f9SAndrew Baumann #include "qemu/error-report.h" 201df7d1f9SAndrew Baumann #include "hw/boards.h" 211df7d1f9SAndrew Baumann #include "hw/loader.h" 2212ec8bd5SPeter Maydell #include "hw/arm/boot.h" 231df7d1f9SAndrew Baumann #include "sysemu/sysemu.h" 241df7d1f9SAndrew Baumann 251df7d1f9SAndrew Baumann #define SMPBOOT_ADDR 0x300 /* this should leave enough space for ATAGS */ 261df7d1f9SAndrew Baumann #define MVBAR_ADDR 0x400 /* secure vectors */ 271df7d1f9SAndrew Baumann #define BOARDSETUP_ADDR (MVBAR_ADDR + 0x20) /* board setup code */ 28bade5816SPekka Enberg #define FIRMWARE_ADDR_2 0x8000 /* Pi 2 loads kernel.img here by default */ 29bade5816SPekka Enberg #define FIRMWARE_ADDR_3 0x80000 /* Pi 3 loads kernel.img here by default */ 30ff72cb6bSPeter Maydell #define SPINTABLE_ADDR 0xd8 /* Pi 3 bootloader spintable */ 311df7d1f9SAndrew Baumann 321df7d1f9SAndrew Baumann /* Table of Linux board IDs for different Pi versions */ 33bade5816SPekka Enberg static const int raspi_boardid[] = {[1] = 0xc42, [2] = 0xc43, [3] = 0xc44}; 341df7d1f9SAndrew Baumann 351df7d1f9SAndrew Baumann typedef struct RasPiState { 36926dcdf0SPeter Maydell BCM283XState soc; 371df7d1f9SAndrew Baumann MemoryRegion ram; 381df7d1f9SAndrew Baumann } RasPiState; 391df7d1f9SAndrew Baumann 401df7d1f9SAndrew Baumann static void write_smpboot(ARMCPU *cpu, const struct arm_boot_info *info) 411df7d1f9SAndrew Baumann { 421df7d1f9SAndrew Baumann static const uint32_t smpboot[] = { 431df7d1f9SAndrew Baumann 0xe1a0e00f, /* mov lr, pc */ 441df7d1f9SAndrew Baumann 0xe3a0fe00 + (BOARDSETUP_ADDR >> 4), /* mov pc, BOARDSETUP_ADDR */ 451df7d1f9SAndrew Baumann 0xee100fb0, /* mrc p15, 0, r0, c0, c0, 5;get core ID */ 461df7d1f9SAndrew Baumann 0xe7e10050, /* ubfx r0, r0, #0, #2 ;extract LSB */ 471df7d1f9SAndrew Baumann 0xe59f5014, /* ldr r5, =0x400000CC ;load mbox base */ 481df7d1f9SAndrew Baumann 0xe320f001, /* 1: yield */ 491df7d1f9SAndrew Baumann 0xe7953200, /* ldr r3, [r5, r0, lsl #4] ;read mbox for our core*/ 501df7d1f9SAndrew Baumann 0xe3530000, /* cmp r3, #0 ;spin while zero */ 511df7d1f9SAndrew Baumann 0x0afffffb, /* beq 1b */ 521df7d1f9SAndrew Baumann 0xe7853200, /* str r3, [r5, r0, lsl #4] ;clear mbox */ 531df7d1f9SAndrew Baumann 0xe12fff13, /* bx r3 ;jump to target */ 541df7d1f9SAndrew Baumann 0x400000cc, /* (constant: mailbox 3 read/clear base) */ 551df7d1f9SAndrew Baumann }; 561df7d1f9SAndrew Baumann 571df7d1f9SAndrew Baumann /* check that we don't overrun board setup vectors */ 581df7d1f9SAndrew Baumann QEMU_BUILD_BUG_ON(SMPBOOT_ADDR + sizeof(smpboot) > MVBAR_ADDR); 591df7d1f9SAndrew Baumann /* check that board setup address is correctly relocated */ 601df7d1f9SAndrew Baumann QEMU_BUILD_BUG_ON((BOARDSETUP_ADDR & 0xf) != 0 611df7d1f9SAndrew Baumann || (BOARDSETUP_ADDR >> 4) >= 0x100); 621df7d1f9SAndrew Baumann 63*0f073693SPhilippe Mathieu-Daudé rom_add_blob_fixed_as("raspi_smpboot", smpboot, sizeof(smpboot), 64*0f073693SPhilippe Mathieu-Daudé info->smp_loader_start, 65*0f073693SPhilippe Mathieu-Daudé arm_boot_address_space(cpu, info)); 661df7d1f9SAndrew Baumann } 671df7d1f9SAndrew Baumann 68ff72cb6bSPeter Maydell static void write_smpboot64(ARMCPU *cpu, const struct arm_boot_info *info) 69ff72cb6bSPeter Maydell { 70*0f073693SPhilippe Mathieu-Daudé AddressSpace *as = arm_boot_address_space(cpu, info); 71ff72cb6bSPeter Maydell /* Unlike the AArch32 version we don't need to call the board setup hook. 72ff72cb6bSPeter Maydell * The mechanism for doing the spin-table is also entirely different. 73ff72cb6bSPeter Maydell * We must have four 64-bit fields at absolute addresses 74ff72cb6bSPeter Maydell * 0xd8, 0xe0, 0xe8, 0xf0 in RAM, which are the flag variables for 75ff72cb6bSPeter Maydell * our CPUs, and which we must ensure are zero initialized before 76ff72cb6bSPeter Maydell * the primary CPU goes into the kernel. We put these variables inside 77ff72cb6bSPeter Maydell * a rom blob, so that the reset for ROM contents zeroes them for us. 78ff72cb6bSPeter Maydell */ 79ff72cb6bSPeter Maydell static const uint32_t smpboot[] = { 80ff72cb6bSPeter Maydell 0xd2801b05, /* mov x5, 0xd8 */ 81ff72cb6bSPeter Maydell 0xd53800a6, /* mrs x6, mpidr_el1 */ 82ff72cb6bSPeter Maydell 0x924004c6, /* and x6, x6, #0x3 */ 83ff72cb6bSPeter Maydell 0xd503205f, /* spin: wfe */ 84ff72cb6bSPeter Maydell 0xf86678a4, /* ldr x4, [x5,x6,lsl #3] */ 85ff72cb6bSPeter Maydell 0xb4ffffc4, /* cbz x4, spin */ 86ff72cb6bSPeter Maydell 0xd2800000, /* mov x0, #0x0 */ 87ff72cb6bSPeter Maydell 0xd2800001, /* mov x1, #0x0 */ 88ff72cb6bSPeter Maydell 0xd2800002, /* mov x2, #0x0 */ 89ff72cb6bSPeter Maydell 0xd2800003, /* mov x3, #0x0 */ 90ff72cb6bSPeter Maydell 0xd61f0080, /* br x4 */ 91ff72cb6bSPeter Maydell }; 92ff72cb6bSPeter Maydell 93ff72cb6bSPeter Maydell static const uint64_t spintables[] = { 94ff72cb6bSPeter Maydell 0, 0, 0, 0 95ff72cb6bSPeter Maydell }; 96ff72cb6bSPeter Maydell 97*0f073693SPhilippe Mathieu-Daudé rom_add_blob_fixed_as("raspi_smpboot", smpboot, sizeof(smpboot), 98*0f073693SPhilippe Mathieu-Daudé info->smp_loader_start, as); 99*0f073693SPhilippe Mathieu-Daudé rom_add_blob_fixed_as("raspi_spintables", spintables, sizeof(spintables), 100*0f073693SPhilippe Mathieu-Daudé SPINTABLE_ADDR, as); 101ff72cb6bSPeter Maydell } 102ff72cb6bSPeter Maydell 1031df7d1f9SAndrew Baumann static void write_board_setup(ARMCPU *cpu, const struct arm_boot_info *info) 1041df7d1f9SAndrew Baumann { 1051df7d1f9SAndrew Baumann arm_write_secure_board_setup_dummy_smc(cpu, info, MVBAR_ADDR); 1061df7d1f9SAndrew Baumann } 1071df7d1f9SAndrew Baumann 1081df7d1f9SAndrew Baumann static void reset_secondary(ARMCPU *cpu, const struct arm_boot_info *info) 1091df7d1f9SAndrew Baumann { 1101df7d1f9SAndrew Baumann CPUState *cs = CPU(cpu); 1111df7d1f9SAndrew Baumann cpu_set_pc(cs, info->smp_loader_start); 1121df7d1f9SAndrew Baumann } 1131df7d1f9SAndrew Baumann 1141df7d1f9SAndrew Baumann static void setup_boot(MachineState *machine, int version, size_t ram_size) 1151df7d1f9SAndrew Baumann { 1161df7d1f9SAndrew Baumann static struct arm_boot_info binfo; 1171df7d1f9SAndrew Baumann int r; 1181df7d1f9SAndrew Baumann 1191df7d1f9SAndrew Baumann binfo.board_id = raspi_boardid[version]; 1201df7d1f9SAndrew Baumann binfo.ram_size = ram_size; 121cc7d44c2SLike Xu binfo.nb_cpus = machine->smp.cpus; 12201e02f5aSPeter Maydell 12301e02f5aSPeter Maydell if (version <= 2) { 12401e02f5aSPeter Maydell /* The rpi1 and 2 require some custom setup code to run in Secure 12501e02f5aSPeter Maydell * mode before booting a kernel (to set up the SMC vectors so 12601e02f5aSPeter Maydell * that we get a no-op SMC; this is used by Linux to call the 12701e02f5aSPeter Maydell * firmware for some cache maintenance operations. 12801e02f5aSPeter Maydell * The rpi3 doesn't need this. 12901e02f5aSPeter Maydell */ 1301df7d1f9SAndrew Baumann binfo.board_setup_addr = BOARDSETUP_ADDR; 1311df7d1f9SAndrew Baumann binfo.write_board_setup = write_board_setup; 1321df7d1f9SAndrew Baumann binfo.secure_board_setup = true; 1331df7d1f9SAndrew Baumann binfo.secure_boot = true; 13401e02f5aSPeter Maydell } 1351df7d1f9SAndrew Baumann 136bade5816SPekka Enberg /* Pi2 and Pi3 requires SMP setup */ 137bade5816SPekka Enberg if (version >= 2) { 1381df7d1f9SAndrew Baumann binfo.smp_loader_start = SMPBOOT_ADDR; 139ff72cb6bSPeter Maydell if (version == 2) { 1401df7d1f9SAndrew Baumann binfo.write_secondary_boot = write_smpboot; 141ff72cb6bSPeter Maydell } else { 142ff72cb6bSPeter Maydell binfo.write_secondary_boot = write_smpboot64; 143ff72cb6bSPeter Maydell } 1441df7d1f9SAndrew Baumann binfo.secondary_cpu_reset_hook = reset_secondary; 1451df7d1f9SAndrew Baumann } 1461df7d1f9SAndrew Baumann 1471df7d1f9SAndrew Baumann /* If the user specified a "firmware" image (e.g. UEFI), we bypass 1481df7d1f9SAndrew Baumann * the normal Linux boot process 1491df7d1f9SAndrew Baumann */ 1501df7d1f9SAndrew Baumann if (machine->firmware) { 151bade5816SPekka Enberg hwaddr firmware_addr = version == 3 ? FIRMWARE_ADDR_3 : FIRMWARE_ADDR_2; 1521df7d1f9SAndrew Baumann /* load the firmware image (typically kernel.img) */ 153bade5816SPekka Enberg r = load_image_targphys(machine->firmware, firmware_addr, 154bade5816SPekka Enberg ram_size - firmware_addr); 1551df7d1f9SAndrew Baumann if (r < 0) { 1561df7d1f9SAndrew Baumann error_report("Failed to load firmware from %s", machine->firmware); 1571df7d1f9SAndrew Baumann exit(1); 1581df7d1f9SAndrew Baumann } 1591df7d1f9SAndrew Baumann 160bade5816SPekka Enberg binfo.entry = firmware_addr; 1611df7d1f9SAndrew Baumann binfo.firmware_loaded = true; 1621df7d1f9SAndrew Baumann } 1631df7d1f9SAndrew Baumann 1642744ece8STao Xu arm_load_kernel(ARM_CPU(first_cpu), machine, &binfo); 1651df7d1f9SAndrew Baumann } 1661df7d1f9SAndrew Baumann 167bade5816SPekka Enberg static void raspi_init(MachineState *machine, int version) 1681df7d1f9SAndrew Baumann { 1691df7d1f9SAndrew Baumann RasPiState *s = g_new0(RasPiState, 1); 1705e9c2a8dSGrégory ESTRADE uint32_t vcram_size; 171a55b53a2SAndrew Baumann DriveInfo *di; 172a55b53a2SAndrew Baumann BlockBackend *blk; 173a55b53a2SAndrew Baumann BusState *bus; 174a55b53a2SAndrew Baumann DeviceState *carddev; 1751df7d1f9SAndrew Baumann 176ff3dcf28SPeter Maydell if (machine->ram_size > 1 * GiB) { 177ff3dcf28SPeter Maydell error_report("Requested ram size is too large for this machine: " 178ff3dcf28SPeter Maydell "maximum is 1GB"); 179ff3dcf28SPeter Maydell exit(1); 180ff3dcf28SPeter Maydell } 181ff3dcf28SPeter Maydell 182d0313798SPhilippe Mathieu-Daudé object_initialize_child(OBJECT(machine), "soc", &s->soc, sizeof(s->soc), 183d0313798SPhilippe Mathieu-Daudé version == 3 ? TYPE_BCM2837 : TYPE_BCM2836, 184d0313798SPhilippe Mathieu-Daudé &error_abort, NULL); 1851df7d1f9SAndrew Baumann 1861df7d1f9SAndrew Baumann /* Allocate and map RAM */ 1871df7d1f9SAndrew Baumann memory_region_allocate_system_memory(&s->ram, OBJECT(machine), "ram", 1881df7d1f9SAndrew Baumann machine->ram_size); 1891df7d1f9SAndrew Baumann /* FIXME: Remove when we have custom CPU address space support */ 1901df7d1f9SAndrew Baumann memory_region_add_subregion_overlap(get_system_memory(), 0, &s->ram, 0); 1911df7d1f9SAndrew Baumann 1921df7d1f9SAndrew Baumann /* Setup the SOC */ 1931df7d1f9SAndrew Baumann object_property_add_const_link(OBJECT(&s->soc), "ram", OBJECT(&s->ram), 1941df7d1f9SAndrew Baumann &error_abort); 195cc7d44c2SLike Xu object_property_set_int(OBJECT(&s->soc), machine->smp.cpus, "enabled-cpus", 1961df7d1f9SAndrew Baumann &error_abort); 197bade5816SPekka Enberg int board_rev = version == 3 ? 0xa02082 : 0xa21041; 198bade5816SPekka Enberg object_property_set_int(OBJECT(&s->soc), board_rev, "board-rev", 199f0afa731SStephen Warren &error_abort); 2001df7d1f9SAndrew Baumann object_property_set_bool(OBJECT(&s->soc), true, "realized", &error_abort); 2011df7d1f9SAndrew Baumann 202a55b53a2SAndrew Baumann /* Create and plug in the SD cards */ 203a55b53a2SAndrew Baumann di = drive_get_next(IF_SD); 204a55b53a2SAndrew Baumann blk = di ? blk_by_legacy_dinfo(di) : NULL; 205a55b53a2SAndrew Baumann bus = qdev_get_child_bus(DEVICE(&s->soc), "sd-bus"); 206a55b53a2SAndrew Baumann if (bus == NULL) { 207a55b53a2SAndrew Baumann error_report("No SD bus found in SOC object"); 208a55b53a2SAndrew Baumann exit(1); 209a55b53a2SAndrew Baumann } 210a55b53a2SAndrew Baumann carddev = qdev_create(bus, TYPE_SD_CARD); 211a55b53a2SAndrew Baumann qdev_prop_set_drive(carddev, "drive", blk, &error_fatal); 212a55b53a2SAndrew Baumann object_property_set_bool(OBJECT(carddev), true, "realized", &error_fatal); 213a55b53a2SAndrew Baumann 214c5c6c47cSMarc-André Lureau vcram_size = object_property_get_uint(OBJECT(&s->soc), "vcram-size", 2155e9c2a8dSGrégory ESTRADE &error_abort); 216bade5816SPekka Enberg setup_boot(machine, version, machine->ram_size - vcram_size); 217bade5816SPekka Enberg } 218bade5816SPekka Enberg 219bade5816SPekka Enberg static void raspi2_init(MachineState *machine) 220bade5816SPekka Enberg { 221bade5816SPekka Enberg raspi_init(machine, 2); 2221df7d1f9SAndrew Baumann } 2231df7d1f9SAndrew Baumann 2241df7d1f9SAndrew Baumann static void raspi2_machine_init(MachineClass *mc) 2251df7d1f9SAndrew Baumann { 2261df7d1f9SAndrew Baumann mc->desc = "Raspberry Pi 2"; 2271df7d1f9SAndrew Baumann mc->init = raspi2_init; 2281df7d1f9SAndrew Baumann mc->block_default_type = IF_SD; 2291df7d1f9SAndrew Baumann mc->no_parallel = 1; 2301df7d1f9SAndrew Baumann mc->no_floppy = 1; 2311df7d1f9SAndrew Baumann mc->no_cdrom = 1; 232926dcdf0SPeter Maydell mc->max_cpus = BCM283X_NCPUS; 233926dcdf0SPeter Maydell mc->min_cpus = BCM283X_NCPUS; 234926dcdf0SPeter Maydell mc->default_cpus = BCM283X_NCPUS; 2352aee4107SPhilippe Mathieu-Daudé mc->default_ram_size = 1 * GiB; 2364672cbd7SPeter Maydell mc->ignore_memory_transaction_failures = true; 2371df7d1f9SAndrew Baumann }; 2381df7d1f9SAndrew Baumann DEFINE_MACHINE("raspi2", raspi2_machine_init) 2391c3db49dSPekka Enberg 2401c3db49dSPekka Enberg #ifdef TARGET_AARCH64 2411c3db49dSPekka Enberg static void raspi3_init(MachineState *machine) 2421c3db49dSPekka Enberg { 2431c3db49dSPekka Enberg raspi_init(machine, 3); 2441c3db49dSPekka Enberg } 2451c3db49dSPekka Enberg 2461c3db49dSPekka Enberg static void raspi3_machine_init(MachineClass *mc) 2471c3db49dSPekka Enberg { 2481c3db49dSPekka Enberg mc->desc = "Raspberry Pi 3"; 2491c3db49dSPekka Enberg mc->init = raspi3_init; 2501c3db49dSPekka Enberg mc->block_default_type = IF_SD; 2511c3db49dSPekka Enberg mc->no_parallel = 1; 2521c3db49dSPekka Enberg mc->no_floppy = 1; 2531c3db49dSPekka Enberg mc->no_cdrom = 1; 254926dcdf0SPeter Maydell mc->max_cpus = BCM283X_NCPUS; 255926dcdf0SPeter Maydell mc->min_cpus = BCM283X_NCPUS; 256926dcdf0SPeter Maydell mc->default_cpus = BCM283X_NCPUS; 2572aee4107SPhilippe Mathieu-Daudé mc->default_ram_size = 1 * GiB; 2581c3db49dSPekka Enberg } 2591c3db49dSPekka Enberg DEFINE_MACHINE("raspi3", raspi3_machine_init) 2601c3db49dSPekka Enberg #endif 261