raspi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 "hw/arm/boot.h"
  19. #include "hw/arm/bcm2836.h"
  20. #include "hw/arm/bcm2838.h"
  21. #include "hw/arm/raspi_platform.h"
  22. #include "hw/registerfields.h"
  23. #include "qemu/error-report.h"
  24. #include "hw/boards.h"
  25. #include "hw/loader.h"
  26. #include "hw/arm/boot.h"
  27. #include "qom/object.h"
  28. #define TYPE_RASPI_MACHINE MACHINE_TYPE_NAME("raspi-common")
  29. OBJECT_DECLARE_SIMPLE_TYPE(RaspiMachineState, RASPI_MACHINE)
  30. #define SMPBOOT_ADDR 0x300 /* this should leave enough space for ATAGS */
  31. #define MVBAR_ADDR 0x400 /* secure vectors */
  32. #define BOARDSETUP_ADDR (MVBAR_ADDR + 0x20) /* board setup code */
  33. #define FIRMWARE_ADDR_2 0x8000 /* Pi 2 loads kernel.img here by default */
  34. #define FIRMWARE_ADDR_3 0x80000 /* Pi 3 loads kernel.img here by default */
  35. #define SPINTABLE_ADDR 0xd8 /* Pi 3 bootloader spintable */
  36. struct RaspiMachineState {
  37. /*< private >*/
  38. RaspiBaseMachineState parent_obj;
  39. /*< public >*/
  40. BCM283XState soc;
  41. };
  42. /*
  43. * Board revision codes:
  44. * www.raspberrypi.org/documentation/hardware/raspberrypi/revision-codes/
  45. */
  46. FIELD(REV_CODE, REVISION, 0, 4);
  47. FIELD(REV_CODE, TYPE, 4, 8);
  48. FIELD(REV_CODE, PROCESSOR, 12, 4);
  49. FIELD(REV_CODE, MANUFACTURER, 16, 4);
  50. FIELD(REV_CODE, MEMORY_SIZE, 20, 3);
  51. FIELD(REV_CODE, STYLE, 23, 1);
  52. typedef enum RaspiProcessorId {
  53. PROCESSOR_ID_BCM2835 = 0,
  54. PROCESSOR_ID_BCM2836 = 1,
  55. PROCESSOR_ID_BCM2837 = 2,
  56. PROCESSOR_ID_BCM2838 = 3,
  57. } RaspiProcessorId;
  58. static const struct {
  59. const char *type;
  60. int cores_count;
  61. } soc_property[] = {
  62. [PROCESSOR_ID_BCM2835] = {TYPE_BCM2835, 1},
  63. [PROCESSOR_ID_BCM2836] = {TYPE_BCM2836, BCM283X_NCPUS},
  64. [PROCESSOR_ID_BCM2837] = {TYPE_BCM2837, BCM283X_NCPUS},
  65. [PROCESSOR_ID_BCM2838] = {TYPE_BCM2838, BCM283X_NCPUS},
  66. };
  67. uint64_t board_ram_size(uint32_t board_rev)
  68. {
  69. assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */
  70. return 256 * MiB << FIELD_EX32(board_rev, REV_CODE, MEMORY_SIZE);
  71. }
  72. static RaspiProcessorId board_processor_id(uint32_t board_rev)
  73. {
  74. int proc_id = FIELD_EX32(board_rev, REV_CODE, PROCESSOR);
  75. assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */
  76. assert(proc_id < ARRAY_SIZE(soc_property) && soc_property[proc_id].type);
  77. return proc_id;
  78. }
  79. const char *board_soc_type(uint32_t board_rev)
  80. {
  81. return soc_property[board_processor_id(board_rev)].type;
  82. }
  83. static int cores_count(uint32_t board_rev)
  84. {
  85. return soc_property[board_processor_id(board_rev)].cores_count;
  86. }
  87. static const char *board_type(uint32_t board_rev)
  88. {
  89. static const char *types[] = {
  90. "A", "B", "A+", "B+", "2B", "Alpha", "CM1", NULL, "3B", "Zero",
  91. "CM3", NULL, "Zero W", "3B+", "3A+", NULL, "CM3+", "4B",
  92. };
  93. assert(FIELD_EX32(board_rev, REV_CODE, STYLE)); /* Only new style */
  94. int bt = FIELD_EX32(board_rev, REV_CODE, TYPE);
  95. if (bt >= ARRAY_SIZE(types) || !types[bt]) {
  96. return "Unknown";
  97. }
  98. return types[bt];
  99. }
  100. static void write_smpboot(ARMCPU *cpu, const struct arm_boot_info *info)
  101. {
  102. static const ARMInsnFixup smpboot[] = {
  103. { 0xe1a0e00f }, /* mov lr, pc */
  104. { 0xe3a0fe00 + (BOARDSETUP_ADDR >> 4) }, /* mov pc, BOARDSETUP_ADDR */
  105. { 0xee100fb0 }, /* mrc p15, 0, r0, c0, c0, 5;get core ID */
  106. { 0xe7e10050 }, /* ubfx r0, r0, #0, #2 ;extract LSB */
  107. { 0xe59f5014 }, /* ldr r5, =0x400000CC ;load mbox base */
  108. { 0xe320f001 }, /* 1: yield */
  109. { 0xe7953200 }, /* ldr r3, [r5, r0, lsl #4] ;read mbox for our core */
  110. { 0xe3530000 }, /* cmp r3, #0 ;spin while zero */
  111. { 0x0afffffb }, /* beq 1b */
  112. { 0xe7853200 }, /* str r3, [r5, r0, lsl #4] ;clear mbox */
  113. { 0xe12fff13 }, /* bx r3 ;jump to target */
  114. { 0x400000cc }, /* (constant: mailbox 3 read/clear base) */
  115. { 0, FIXUP_TERMINATOR }
  116. };
  117. static const uint32_t fixupcontext[FIXUP_MAX] = { 0 };
  118. /* check that we don't overrun board setup vectors */
  119. QEMU_BUILD_BUG_ON(SMPBOOT_ADDR + sizeof(smpboot) > MVBAR_ADDR);
  120. /* check that board setup address is correctly relocated */
  121. QEMU_BUILD_BUG_ON((BOARDSETUP_ADDR & 0xf) != 0
  122. || (BOARDSETUP_ADDR >> 4) >= 0x100);
  123. arm_write_bootloader("raspi_smpboot", arm_boot_address_space(cpu, info),
  124. info->smp_loader_start, smpboot, fixupcontext);
  125. }
  126. static void write_smpboot64(ARMCPU *cpu, const struct arm_boot_info *info)
  127. {
  128. AddressSpace *as = arm_boot_address_space(cpu, info);
  129. /* Unlike the AArch32 version we don't need to call the board setup hook.
  130. * The mechanism for doing the spin-table is also entirely different.
  131. * We must have four 64-bit fields at absolute addresses
  132. * 0xd8, 0xe0, 0xe8, 0xf0 in RAM, which are the flag variables for
  133. * our CPUs, and which we must ensure are zero initialized before
  134. * the primary CPU goes into the kernel. We put these variables inside
  135. * a rom blob, so that the reset for ROM contents zeroes them for us.
  136. */
  137. static const ARMInsnFixup smpboot[] = {
  138. { 0xd2801b05 }, /* mov x5, 0xd8 */
  139. { 0xd53800a6 }, /* mrs x6, mpidr_el1 */
  140. { 0x924004c6 }, /* and x6, x6, #0x3 */
  141. { 0xd503205f }, /* spin: wfe */
  142. { 0xf86678a4 }, /* ldr x4, [x5,x6,lsl #3] */
  143. { 0xb4ffffc4 }, /* cbz x4, spin */
  144. { 0xd2800000 }, /* mov x0, #0x0 */
  145. { 0xd2800001 }, /* mov x1, #0x0 */
  146. { 0xd2800002 }, /* mov x2, #0x0 */
  147. { 0xd2800003 }, /* mov x3, #0x0 */
  148. { 0xd61f0080 }, /* br x4 */
  149. { 0, FIXUP_TERMINATOR }
  150. };
  151. static const uint32_t fixupcontext[FIXUP_MAX] = { 0 };
  152. static const uint64_t spintables[] = {
  153. 0, 0, 0, 0
  154. };
  155. arm_write_bootloader("raspi_smpboot", as, info->smp_loader_start,
  156. smpboot, fixupcontext);
  157. rom_add_blob_fixed_as("raspi_spintables", spintables, sizeof(spintables),
  158. SPINTABLE_ADDR, as);
  159. }
  160. static void write_board_setup(ARMCPU *cpu, const struct arm_boot_info *info)
  161. {
  162. arm_write_secure_board_setup_dummy_smc(cpu, info, MVBAR_ADDR);
  163. }
  164. static void reset_secondary(ARMCPU *cpu, const struct arm_boot_info *info)
  165. {
  166. CPUState *cs = CPU(cpu);
  167. cpu_set_pc(cs, info->smp_loader_start);
  168. }
  169. static void setup_boot(MachineState *machine, ARMCPU *cpu,
  170. RaspiProcessorId processor_id, size_t ram_size)
  171. {
  172. RaspiBaseMachineState *s = RASPI_BASE_MACHINE(machine);
  173. int r;
  174. s->binfo.ram_size = ram_size;
  175. if (processor_id <= PROCESSOR_ID_BCM2836) {
  176. /*
  177. * The BCM2835 and BCM2836 require some custom setup code to run
  178. * in Secure mode before booting a kernel (to set up the SMC vectors
  179. * so that we get a no-op SMC; this is used by Linux to call the
  180. * firmware for some cache maintenance operations.
  181. * The BCM2837 doesn't need this.
  182. */
  183. s->binfo.board_setup_addr = BOARDSETUP_ADDR;
  184. s->binfo.write_board_setup = write_board_setup;
  185. s->binfo.secure_board_setup = true;
  186. s->binfo.secure_boot = true;
  187. }
  188. /* BCM2836 and BCM2837 requires SMP setup */
  189. if (processor_id >= PROCESSOR_ID_BCM2836) {
  190. s->binfo.smp_loader_start = SMPBOOT_ADDR;
  191. if (processor_id == PROCESSOR_ID_BCM2836) {
  192. s->binfo.write_secondary_boot = write_smpboot;
  193. } else {
  194. s->binfo.write_secondary_boot = write_smpboot64;
  195. }
  196. s->binfo.secondary_cpu_reset_hook = reset_secondary;
  197. }
  198. /* If the user specified a "firmware" image (e.g. UEFI), we bypass
  199. * the normal Linux boot process
  200. */
  201. if (machine->firmware) {
  202. hwaddr firmware_addr = processor_id <= PROCESSOR_ID_BCM2836
  203. ? FIRMWARE_ADDR_2 : FIRMWARE_ADDR_3;
  204. /* load the firmware image (typically kernel.img) */
  205. r = load_image_targphys(machine->firmware, firmware_addr,
  206. ram_size - firmware_addr);
  207. if (r < 0) {
  208. error_report("Failed to load firmware from %s", machine->firmware);
  209. exit(1);
  210. }
  211. s->binfo.entry = firmware_addr;
  212. s->binfo.firmware_loaded = true;
  213. }
  214. arm_load_kernel(cpu, machine, &s->binfo);
  215. }
  216. void raspi_base_machine_init(MachineState *machine,
  217. BCM283XBaseState *soc)
  218. {
  219. RaspiBaseMachineClass *mc = RASPI_BASE_MACHINE_GET_CLASS(machine);
  220. uint32_t board_rev = mc->board_rev;
  221. uint64_t ram_size = board_ram_size(board_rev);
  222. uint32_t vcram_base, vcram_size;
  223. size_t boot_ram_size;
  224. DriveInfo *di;
  225. BlockBackend *blk;
  226. BusState *bus;
  227. DeviceState *carddev;
  228. if (machine->ram_size != ram_size) {
  229. char *size_str = size_to_str(ram_size);
  230. error_report("Invalid RAM size, should be %s", size_str);
  231. g_free(size_str);
  232. exit(1);
  233. }
  234. /* FIXME: Remove when we have custom CPU address space support */
  235. memory_region_add_subregion_overlap(get_system_memory(), 0,
  236. machine->ram, 0);
  237. /* Setup the SOC */
  238. object_property_add_const_link(OBJECT(soc), "ram", OBJECT(machine->ram));
  239. object_property_set_int(OBJECT(soc), "board-rev", board_rev,
  240. &error_abort);
  241. object_property_set_str(OBJECT(soc), "command-line",
  242. machine->kernel_cmdline, &error_abort);
  243. qdev_realize(DEVICE(soc), NULL, &error_fatal);
  244. /* Create and plug in the SD cards */
  245. di = drive_get(IF_SD, 0, 0);
  246. blk = di ? blk_by_legacy_dinfo(di) : NULL;
  247. bus = qdev_get_child_bus(DEVICE(soc), "sd-bus");
  248. if (bus == NULL) {
  249. error_report("No SD bus found in SOC object");
  250. exit(1);
  251. }
  252. carddev = qdev_new(TYPE_SD_CARD);
  253. qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal);
  254. qdev_realize_and_unref(carddev, bus, &error_fatal);
  255. vcram_size = object_property_get_uint(OBJECT(soc), "vcram-size",
  256. &error_abort);
  257. vcram_base = object_property_get_uint(OBJECT(soc), "vcram-base",
  258. &error_abort);
  259. if (vcram_base == 0) {
  260. vcram_base = ram_size - vcram_size;
  261. }
  262. boot_ram_size = MIN(vcram_base, UPPER_RAM_BASE - vcram_size);
  263. setup_boot(machine, &soc->cpu[0].core, board_processor_id(board_rev),
  264. boot_ram_size);
  265. }
  266. void raspi_machine_init(MachineState *machine)
  267. {
  268. RaspiMachineState *s = RASPI_MACHINE(machine);
  269. RaspiBaseMachineState *s_base = RASPI_BASE_MACHINE(machine);
  270. RaspiBaseMachineClass *mc = RASPI_BASE_MACHINE_GET_CLASS(machine);
  271. BCM283XState *soc = &s->soc;
  272. s_base->binfo.board_id = MACH_TYPE_BCM2708;
  273. object_initialize_child(OBJECT(machine), "soc", soc,
  274. board_soc_type(mc->board_rev));
  275. raspi_base_machine_init(machine, &soc->parent_obj);
  276. }
  277. void raspi_machine_class_common_init(MachineClass *mc,
  278. uint32_t board_rev)
  279. {
  280. mc->desc = g_strdup_printf("Raspberry Pi %s (revision 1.%u)",
  281. board_type(board_rev),
  282. FIELD_EX32(board_rev, REV_CODE, REVISION));
  283. mc->block_default_type = IF_SD;
  284. mc->no_parallel = 1;
  285. mc->no_floppy = 1;
  286. mc->no_cdrom = 1;
  287. mc->default_cpus = mc->min_cpus = mc->max_cpus = cores_count(board_rev);
  288. mc->default_ram_size = board_ram_size(board_rev);
  289. mc->default_ram_id = "ram";
  290. };
  291. static void raspi_machine_class_init(MachineClass *mc,
  292. uint32_t board_rev)
  293. {
  294. raspi_machine_class_common_init(mc, board_rev);
  295. mc->init = raspi_machine_init;
  296. };
  297. static void raspi0_machine_class_init(ObjectClass *oc, void *data)
  298. {
  299. MachineClass *mc = MACHINE_CLASS(oc);
  300. RaspiBaseMachineClass *rmc = RASPI_BASE_MACHINE_CLASS(oc);
  301. rmc->board_rev = 0x920092; /* Revision 1.2 */
  302. raspi_machine_class_init(mc, rmc->board_rev);
  303. };
  304. static void raspi1ap_machine_class_init(ObjectClass *oc, void *data)
  305. {
  306. MachineClass *mc = MACHINE_CLASS(oc);
  307. RaspiBaseMachineClass *rmc = RASPI_BASE_MACHINE_CLASS(oc);
  308. rmc->board_rev = 0x900021; /* Revision 1.1 */
  309. raspi_machine_class_init(mc, rmc->board_rev);
  310. };
  311. static void raspi2b_machine_class_init(ObjectClass *oc, void *data)
  312. {
  313. MachineClass *mc = MACHINE_CLASS(oc);
  314. RaspiBaseMachineClass *rmc = RASPI_BASE_MACHINE_CLASS(oc);
  315. rmc->board_rev = 0xa21041;
  316. raspi_machine_class_init(mc, rmc->board_rev);
  317. };
  318. #ifdef TARGET_AARCH64
  319. static void raspi3ap_machine_class_init(ObjectClass *oc, void *data)
  320. {
  321. MachineClass *mc = MACHINE_CLASS(oc);
  322. RaspiBaseMachineClass *rmc = RASPI_BASE_MACHINE_CLASS(oc);
  323. rmc->board_rev = 0x9020e0; /* Revision 1.0 */
  324. raspi_machine_class_init(mc, rmc->board_rev);
  325. };
  326. static void raspi3b_machine_class_init(ObjectClass *oc, void *data)
  327. {
  328. MachineClass *mc = MACHINE_CLASS(oc);
  329. RaspiBaseMachineClass *rmc = RASPI_BASE_MACHINE_CLASS(oc);
  330. rmc->board_rev = 0xa02082;
  331. raspi_machine_class_init(mc, rmc->board_rev);
  332. };
  333. #endif /* TARGET_AARCH64 */
  334. static const TypeInfo raspi_machine_types[] = {
  335. {
  336. .name = MACHINE_TYPE_NAME("raspi0"),
  337. .parent = TYPE_RASPI_MACHINE,
  338. .class_init = raspi0_machine_class_init,
  339. }, {
  340. .name = MACHINE_TYPE_NAME("raspi1ap"),
  341. .parent = TYPE_RASPI_MACHINE,
  342. .class_init = raspi1ap_machine_class_init,
  343. }, {
  344. .name = MACHINE_TYPE_NAME("raspi2b"),
  345. .parent = TYPE_RASPI_MACHINE,
  346. .class_init = raspi2b_machine_class_init,
  347. #ifdef TARGET_AARCH64
  348. }, {
  349. .name = MACHINE_TYPE_NAME("raspi3ap"),
  350. .parent = TYPE_RASPI_MACHINE,
  351. .class_init = raspi3ap_machine_class_init,
  352. }, {
  353. .name = MACHINE_TYPE_NAME("raspi3b"),
  354. .parent = TYPE_RASPI_MACHINE,
  355. .class_init = raspi3b_machine_class_init,
  356. #endif
  357. }, {
  358. .name = TYPE_RASPI_MACHINE,
  359. .parent = TYPE_RASPI_BASE_MACHINE,
  360. .instance_size = sizeof(RaspiMachineState),
  361. .abstract = true,
  362. }, {
  363. .name = TYPE_RASPI_BASE_MACHINE,
  364. .parent = TYPE_MACHINE,
  365. .instance_size = sizeof(RaspiBaseMachineState),
  366. .class_size = sizeof(RaspiBaseMachineClass),
  367. .abstract = true,
  368. }
  369. };
  370. DEFINE_TYPES(raspi_machine_types)