raspi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 work is licensed under the terms of the GNU GPL, version 2 or later.
  12. * See the COPYING file in the top-level directory.
  13. */
  14. #include "qemu/osdep.h"
  15. #include "qemu/units.h"
  16. #include "qemu/cutils.h"
  17. #include "qapi/error.h"
  18. #include "cpu.h"
  19. #include "hw/arm/bcm2836.h"
  20. #include "hw/registerfields.h"
  21. #include "qemu/error-report.h"
  22. #include "hw/boards.h"
  23. #include "hw/loader.h"
  24. #include "hw/arm/boot.h"
  25. #include "sysemu/sysemu.h"
  26. #define SMPBOOT_ADDR 0x300 /* this should leave enough space for ATAGS */
  27. #define MVBAR_ADDR 0x400 /* secure vectors */
  28. #define BOARDSETUP_ADDR (MVBAR_ADDR + 0x20) /* board setup code */
  29. #define FIRMWARE_ADDR_2 0x8000 /* Pi 2 loads kernel.img here by default */
  30. #define FIRMWARE_ADDR_3 0x80000 /* Pi 3 loads kernel.img here by default */
  31. #define SPINTABLE_ADDR 0xd8 /* Pi 3 bootloader spintable */
  32. /* Registered machine type (matches RPi Foundation bootloader and U-Boot) */
  33. #define MACH_TYPE_BCM2708 3138
  34. typedef struct RaspiMachineState {
  35. /*< private >*/
  36. MachineState parent_obj;
  37. /*< public >*/
  38. BCM283XState soc;
  39. } RaspiMachineState;
  40. typedef struct RaspiMachineClass {
  41. /*< private >*/
  42. MachineClass parent_obj;
  43. /*< public >*/
  44. uint32_t board_rev;
  45. } RaspiMachineClass;
  46. #define TYPE_RASPI_MACHINE MACHINE_TYPE_NAME("raspi-common")
  47. #define RASPI_MACHINE(obj) \
  48. OBJECT_CHECK(RaspiMachineState, (obj), TYPE_RASPI_MACHINE)
  49. #define RASPI_MACHINE_CLASS(klass) \
  50. OBJECT_CLASS_CHECK(RaspiMachineClass, (klass), TYPE_RASPI_MACHINE)
  51. #define RASPI_MACHINE_GET_CLASS(obj) \
  52. OBJECT_GET_CLASS(RaspiMachineClass, (obj), TYPE_RASPI_MACHINE)
  53. /*
  54. * Board revision codes:
  55. * www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/
  56. */
  57. FIELD(REV_CODE, REVISION, 0, 4);
  58. FIELD(REV_CODE, TYPE, 4, 8);
  59. FIELD(REV_CODE, PROCESSOR, 12, 4);
  60. FIELD(REV_CODE, MANUFACTURER, 16, 4);
  61. FIELD(REV_CODE, MEMORY_SIZE, 20, 3);
  62. FIELD(REV_CODE, STYLE, 23, 1);
  63. static uint64_t board_ram_size(uint32_t board_rev)
  64. {
  65. assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */
  66. return 256 * MiB << FIELD_EX32(board_rev, REV_CODE, MEMORY_SIZE);
  67. }
  68. static int board_processor_id(uint32_t board_rev)
  69. {
  70. assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */
  71. return FIELD_EX32(board_rev, REV_CODE, PROCESSOR);
  72. }
  73. static int board_version(uint32_t board_rev)
  74. {
  75. return board_processor_id(board_rev) + 1;
  76. }
  77. static const char *board_soc_type(uint32_t board_rev)
  78. {
  79. static const char *soc_types[] = {
  80. NULL, TYPE_BCM2836, TYPE_BCM2837,
  81. };
  82. int proc_id = board_processor_id(board_rev);
  83. if (proc_id >= ARRAY_SIZE(soc_types) || !soc_types[proc_id]) {
  84. error_report("Unsupported processor id '%d' (board revision: 0x%x)",
  85. proc_id, board_rev);
  86. exit(1);
  87. }
  88. return soc_types[proc_id];
  89. }
  90. static int cores_count(uint32_t board_rev)
  91. {
  92. static const int soc_cores_count[] = {
  93. 0, BCM283X_NCPUS, BCM283X_NCPUS,
  94. };
  95. int proc_id = board_processor_id(board_rev);
  96. if (proc_id >= ARRAY_SIZE(soc_cores_count) || !soc_cores_count[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_cores_count[proc_id];
  102. }
  103. static const char *board_type(uint32_t board_rev)
  104. {
  105. static const char *types[] = {
  106. "A", "B", "A+", "B+", "2B", "Alpha", "CM1", NULL, "3B", "Zero",
  107. "CM3", NULL, "Zero W", "3B+", "3A+", NULL, "CM3+", "4B",
  108. };
  109. assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */
  110. int bt = FIELD_EX32(board_rev, REV_CODE, TYPE);
  111. if (bt >= ARRAY_SIZE(types) || !types[bt]) {
  112. return "Unknown";
  113. }
  114. return types[bt];
  115. }
  116. static void write_smpboot(ARMCPU *cpu, const struct arm_boot_info *info)
  117. {
  118. static const uint32_t smpboot[] = {
  119. 0xe1a0e00f, /* mov lr, pc */
  120. 0xe3a0fe00 + (BOARDSETUP_ADDR >> 4), /* mov pc, BOARDSETUP_ADDR */
  121. 0xee100fb0, /* mrc p15, 0, r0, c0, c0, 5;get core ID */
  122. 0xe7e10050, /* ubfx r0, r0, #0, #2 ;extract LSB */
  123. 0xe59f5014, /* ldr r5, =0x400000CC ;load mbox base */
  124. 0xe320f001, /* 1: yield */
  125. 0xe7953200, /* ldr r3, [r5, r0, lsl #4] ;read mbox for our core*/
  126. 0xe3530000, /* cmp r3, #0 ;spin while zero */
  127. 0x0afffffb, /* beq 1b */
  128. 0xe7853200, /* str r3, [r5, r0, lsl #4] ;clear mbox */
  129. 0xe12fff13, /* bx r3 ;jump to target */
  130. 0x400000cc, /* (constant: mailbox 3 read/clear base) */
  131. };
  132. /* check that we don't overrun board setup vectors */
  133. QEMU_BUILD_BUG_ON(SMPBOOT_ADDR + sizeof(smpboot) > MVBAR_ADDR);
  134. /* check that board setup address is correctly relocated */
  135. QEMU_BUILD_BUG_ON((BOARDSETUP_ADDR & 0xf) != 0
  136. || (BOARDSETUP_ADDR >> 4) >= 0x100);
  137. rom_add_blob_fixed_as("raspi_smpboot", smpboot, sizeof(smpboot),
  138. info->smp_loader_start,
  139. arm_boot_address_space(cpu, info));
  140. }
  141. static void write_smpboot64(ARMCPU *cpu, const struct arm_boot_info *info)
  142. {
  143. AddressSpace *as = arm_boot_address_space(cpu, info);
  144. /* Unlike the AArch32 version we don't need to call the board setup hook.
  145. * The mechanism for doing the spin-table is also entirely different.
  146. * We must have four 64-bit fields at absolute addresses
  147. * 0xd8, 0xe0, 0xe8, 0xf0 in RAM, which are the flag variables for
  148. * our CPUs, and which we must ensure are zero initialized before
  149. * the primary CPU goes into the kernel. We put these variables inside
  150. * a rom blob, so that the reset for ROM contents zeroes them for us.
  151. */
  152. static const uint32_t smpboot[] = {
  153. 0xd2801b05, /* mov x5, 0xd8 */
  154. 0xd53800a6, /* mrs x6, mpidr_el1 */
  155. 0x924004c6, /* and x6, x6, #0x3 */
  156. 0xd503205f, /* spin: wfe */
  157. 0xf86678a4, /* ldr x4, [x5,x6,lsl #3] */
  158. 0xb4ffffc4, /* cbz x4, spin */
  159. 0xd2800000, /* mov x0, #0x0 */
  160. 0xd2800001, /* mov x1, #0x0 */
  161. 0xd2800002, /* mov x2, #0x0 */
  162. 0xd2800003, /* mov x3, #0x0 */
  163. 0xd61f0080, /* br x4 */
  164. };
  165. static const uint64_t spintables[] = {
  166. 0, 0, 0, 0
  167. };
  168. rom_add_blob_fixed_as("raspi_smpboot", smpboot, sizeof(smpboot),
  169. info->smp_loader_start, as);
  170. rom_add_blob_fixed_as("raspi_spintables", spintables, sizeof(spintables),
  171. SPINTABLE_ADDR, as);
  172. }
  173. static void write_board_setup(ARMCPU *cpu, const struct arm_boot_info *info)
  174. {
  175. arm_write_secure_board_setup_dummy_smc(cpu, info, MVBAR_ADDR);
  176. }
  177. static void reset_secondary(ARMCPU *cpu, const struct arm_boot_info *info)
  178. {
  179. CPUState *cs = CPU(cpu);
  180. cpu_set_pc(cs, info->smp_loader_start);
  181. }
  182. static void setup_boot(MachineState *machine, int version, size_t ram_size)
  183. {
  184. static struct arm_boot_info binfo;
  185. int r;
  186. binfo.board_id = MACH_TYPE_BCM2708;
  187. binfo.ram_size = ram_size;
  188. binfo.nb_cpus = machine->smp.cpus;
  189. if (version <= 2) {
  190. /* The rpi1 and 2 require some custom setup code to run in Secure
  191. * mode before booting a kernel (to set up the SMC vectors so
  192. * that we get a no-op SMC; this is used by Linux to call the
  193. * firmware for some cache maintenance operations.
  194. * The rpi3 doesn't need this.
  195. */
  196. binfo.board_setup_addr = BOARDSETUP_ADDR;
  197. binfo.write_board_setup = write_board_setup;
  198. binfo.secure_board_setup = true;
  199. binfo.secure_boot = true;
  200. }
  201. /* Pi2 and Pi3 requires SMP setup */
  202. if (version >= 2) {
  203. binfo.smp_loader_start = SMPBOOT_ADDR;
  204. if (version == 2) {
  205. binfo.write_secondary_boot = write_smpboot;
  206. } else {
  207. binfo.write_secondary_boot = write_smpboot64;
  208. }
  209. binfo.secondary_cpu_reset_hook = reset_secondary;
  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) {
  215. hwaddr firmware_addr = version == 3 ? FIRMWARE_ADDR_3 : FIRMWARE_ADDR_2;
  216. /* load the firmware image (typically kernel.img) */
  217. r = load_image_targphys(machine->firmware, firmware_addr,
  218. ram_size - firmware_addr);
  219. if (r < 0) {
  220. error_report("Failed to load firmware from %s", machine->firmware);
  221. exit(1);
  222. }
  223. binfo.entry = firmware_addr;
  224. binfo.firmware_loaded = true;
  225. }
  226. arm_load_kernel(ARM_CPU(first_cpu), machine, &binfo);
  227. }
  228. static void raspi_machine_init(MachineState *machine)
  229. {
  230. RaspiMachineClass *mc = RASPI_MACHINE_GET_CLASS(machine);
  231. RaspiMachineState *s = RASPI_MACHINE(machine);
  232. uint32_t board_rev = mc->board_rev;
  233. int version = board_version(board_rev);
  234. uint64_t ram_size = board_ram_size(board_rev);
  235. uint32_t vcram_size;
  236. DriveInfo *di;
  237. BlockBackend *blk;
  238. BusState *bus;
  239. DeviceState *carddev;
  240. if (machine->ram_size != ram_size) {
  241. char *size_str = size_to_str(ram_size);
  242. error_report("Invalid RAM size, should be %s", size_str);
  243. g_free(size_str);
  244. exit(1);
  245. }
  246. /* FIXME: Remove when we have custom CPU address space support */
  247. memory_region_add_subregion_overlap(get_system_memory(), 0,
  248. machine->ram, 0);
  249. /* Setup the SOC */
  250. object_initialize_child(OBJECT(machine), "soc", &s->soc,
  251. board_soc_type(board_rev));
  252. object_property_add_const_link(OBJECT(&s->soc), "ram", OBJECT(machine->ram));
  253. object_property_set_int(OBJECT(&s->soc), "board-rev", board_rev,
  254. &error_abort);
  255. qdev_realize(DEVICE(&s->soc), NULL, &error_abort);
  256. /* Create and plug in the SD cards */
  257. di = drive_get_next(IF_SD);
  258. blk = di ? blk_by_legacy_dinfo(di) : NULL;
  259. bus = qdev_get_child_bus(DEVICE(&s->soc), "sd-bus");
  260. if (bus == NULL) {
  261. error_report("No SD bus found in SOC object");
  262. exit(1);
  263. }
  264. carddev = qdev_new(TYPE_SD_CARD);
  265. qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal);
  266. qdev_realize_and_unref(carddev, bus, &error_fatal);
  267. vcram_size = object_property_get_uint(OBJECT(&s->soc), "vcram-size",
  268. &error_abort);
  269. setup_boot(machine, version, machine->ram_size - vcram_size);
  270. }
  271. static void raspi_machine_class_init(ObjectClass *oc, void *data)
  272. {
  273. MachineClass *mc = MACHINE_CLASS(oc);
  274. RaspiMachineClass *rmc = RASPI_MACHINE_CLASS(oc);
  275. uint32_t board_rev = (uint32_t)(uintptr_t)data;
  276. rmc->board_rev = board_rev;
  277. mc->desc = g_strdup_printf("Raspberry Pi %s", board_type(board_rev));
  278. mc->init = raspi_machine_init;
  279. mc->block_default_type = IF_SD;
  280. mc->no_parallel = 1;
  281. mc->no_floppy = 1;
  282. mc->no_cdrom = 1;
  283. mc->default_cpus = mc->min_cpus = mc->max_cpus = cores_count(board_rev);
  284. mc->default_ram_size = board_ram_size(board_rev);
  285. mc->default_ram_id = "ram";
  286. if (board_version(board_rev) == 2) {
  287. mc->ignore_memory_transaction_failures = true;
  288. }
  289. };
  290. static const TypeInfo raspi_machine_types[] = {
  291. {
  292. .name = MACHINE_TYPE_NAME("raspi2"),
  293. .parent = TYPE_RASPI_MACHINE,
  294. .class_init = raspi_machine_class_init,
  295. .class_data = (void *)0xa21041,
  296. #ifdef TARGET_AARCH64
  297. }, {
  298. .name = MACHINE_TYPE_NAME("raspi3"),
  299. .parent = TYPE_RASPI_MACHINE,
  300. .class_init = raspi_machine_class_init,
  301. .class_data = (void *)0xa02082,
  302. #endif
  303. }, {
  304. .name = TYPE_RASPI_MACHINE,
  305. .parent = TYPE_MACHINE,
  306. .instance_size = sizeof(RaspiMachineState),
  307. .class_size = sizeof(RaspiMachineClass),
  308. .abstract = true,
  309. }
  310. };
  311. DEFINE_TYPES(raspi_machine_types)