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 * 8 * Raspberry Pi 3 emulation Copyright (c) 2018 Zoltán Baldaszti 9 * Upstream code cleanup (c) 2018 Pekka Enberg 10 * 11 * This code is licensed under the GNU GPLv2 and later. 12 */ 13 14 #include "qemu/osdep.h" 15 #include "qapi/error.h" 16 #include "qemu-common.h" 17 #include "cpu.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" 24 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 */ 30 31 /* Table of Linux board IDs for different Pi versions */ 32 static const int raspi_boardid[] = {[1] = 0xc42, [2] = 0xc43, [3] = 0xc44}; 33 34 typedef struct RasPiState { 35 BCM2836State soc; 36 MemoryRegion ram; 37 } RasPiState; 38 39 static void write_smpboot(ARMCPU *cpu, const struct arm_boot_info *info) 40 { 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) */ 54 }; 55 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); 61 62 rom_add_blob_fixed("raspi_smpboot", smpboot, sizeof(smpboot), 63 info->smp_loader_start); 64 } 65 66 static void write_board_setup(ARMCPU *cpu, const struct arm_boot_info *info) 67 { 68 arm_write_secure_board_setup_dummy_smc(cpu, info, MVBAR_ADDR); 69 } 70 71 static void reset_secondary(ARMCPU *cpu, const struct arm_boot_info *info) 72 { 73 CPUState *cs = CPU(cpu); 74 cpu_set_pc(cs, info->smp_loader_start); 75 } 76 77 static void setup_boot(MachineState *machine, int version, size_t ram_size) 78 { 79 static struct arm_boot_info binfo; 80 int r; 81 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; 89 90 /* Pi2 and Pi3 requires SMP setup */ 91 if (version >= 2) { 92 binfo.smp_loader_start = SMPBOOT_ADDR; 93 binfo.write_secondary_boot = write_smpboot; 94 binfo.secondary_cpu_reset_hook = reset_secondary; 95 } 96 97 /* If the user specified a "firmware" image (e.g. UEFI), we bypass 98 * the normal Linux boot process 99 */ 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); 105 if (r < 0) { 106 error_report("Failed to load firmware from %s", machine->firmware); 107 exit(1); 108 } 109 110 binfo.entry = firmware_addr; 111 binfo.firmware_loaded = true; 112 } else { 113 binfo.kernel_filename = machine->kernel_filename; 114 binfo.kernel_cmdline = machine->kernel_cmdline; 115 binfo.initrd_filename = machine->initrd_filename; 116 } 117 118 arm_load_kernel(ARM_CPU(first_cpu), &binfo); 119 } 120 121 static void raspi_init(MachineState *machine, int version) 122 { 123 RasPiState *s = g_new0(RasPiState, 1); 124 uint32_t vcram_size; 125 DriveInfo *di; 126 BlockBackend *blk; 127 BusState *bus; 128 DeviceState *carddev; 129 130 object_initialize(&s->soc, sizeof(s->soc), TYPE_BCM2836); 131 object_property_add_child(OBJECT(machine), "soc", OBJECT(&s->soc), 132 &error_abort); 133 134 /* Allocate and map RAM */ 135 memory_region_allocate_system_memory(&s->ram, OBJECT(machine), "ram", 136 machine->ram_size); 137 /* FIXME: Remove when we have custom CPU address space support */ 138 memory_region_add_subregion_overlap(get_system_memory(), 0, &s->ram, 0); 139 140 /* Setup the SOC */ 141 object_property_add_const_link(OBJECT(&s->soc), "ram", OBJECT(&s->ram), 142 &error_abort); 143 object_property_set_str(OBJECT(&s->soc), machine->cpu_type, "cpu-type", 144 &error_abort); 145 object_property_set_int(OBJECT(&s->soc), smp_cpus, "enabled-cpus", 146 &error_abort); 147 int board_rev = version == 3 ? 0xa02082 : 0xa21041; 148 object_property_set_int(OBJECT(&s->soc), board_rev, "board-rev", 149 &error_abort); 150 object_property_set_bool(OBJECT(&s->soc), true, "realized", &error_abort); 151 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"); 156 if (bus == NULL) { 157 error_report("No SD bus found in SOC object"); 158 exit(1); 159 } 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); 163 164 vcram_size = object_property_get_uint(OBJECT(&s->soc), "vcram-size", 165 &error_abort); 166 setup_boot(machine, version, machine->ram_size - vcram_size); 167 } 168 169 static void raspi2_init(MachineState *machine) 170 { 171 raspi_init(machine, 2); 172 } 173 174 static void raspi2_machine_init(MachineClass *mc) 175 { 176 mc->desc = "Raspberry Pi 2"; 177 mc->init = raspi2_init; 178 mc->block_default_type = IF_SD; 179 mc->no_parallel = 1; 180 mc->no_floppy = 1; 181 mc->no_cdrom = 1; 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; 188 }; 189 DEFINE_MACHINE("raspi2", raspi2_machine_init) 190 191 #ifdef TARGET_AARCH64 192 static void raspi3_init(MachineState *machine) 193 { 194 raspi_init(machine, 3); 195 } 196 197 static void raspi3_machine_init(MachineClass *mc) 198 { 199 mc->desc = "Raspberry Pi 3"; 200 mc->init = raspi3_init; 201 mc->block_default_type = IF_SD; 202 mc->no_parallel = 1; 203 mc->no_floppy = 1; 204 mc->no_cdrom = 1; 205 mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-a53"); 206 mc->max_cpus = BCM2836_NCPUS; 207 mc->min_cpus = BCM2836_NCPUS; 208 mc->default_cpus = BCM2836_NCPUS; 209 mc->default_ram_size = 1024 * 1024 * 1024; 210 } 211 DEFINE_MACHINE("raspi3", raspi3_machine_init) 212 #endif 213