2
0

mps2-tz.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. /*
  2. * ARM V2M MPS2 board emulation, trustzone aware FPGA images
  3. *
  4. * Copyright (c) 2017 Linaro Limited
  5. * Written by Peter Maydell
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 or
  9. * (at your option) any later version.
  10. */
  11. /* The MPS2 and MPS2+ dev boards are FPGA based (the 2+ has a bigger
  12. * FPGA but is otherwise the same as the 2). Since the CPU itself
  13. * and most of the devices are in the FPGA, the details of the board
  14. * as seen by the guest depend significantly on the FPGA image.
  15. * This source file covers the following FPGA images, for TrustZone cores:
  16. * "mps2-an505" -- Cortex-M33 as documented in ARM Application Note AN505
  17. * "mps2-an521" -- Dual Cortex-M33 as documented in Application Note AN521
  18. *
  19. * Links to the TRM for the board itself and to the various Application
  20. * Notes which document the FPGA images can be found here:
  21. * https://developer.arm.com/products/system-design/development-boards/fpga-prototyping-boards/mps2
  22. *
  23. * Board TRM:
  24. * http://infocenter.arm.com/help/topic/com.arm.doc.100112_0200_06_en/versatile_express_cortex_m_prototyping_systems_v2m_mps2_and_v2m_mps2plus_technical_reference_100112_0200_06_en.pdf
  25. * Application Note AN505:
  26. * http://infocenter.arm.com/help/topic/com.arm.doc.dai0505b/index.html
  27. * Application Note AN521:
  28. * http://infocenter.arm.com/help/topic/com.arm.doc.dai0521c/index.html
  29. *
  30. * The AN505 defers to the Cortex-M33 processor ARMv8M IoT Kit FVP User Guide
  31. * (ARM ECM0601256) for the details of some of the device layout:
  32. * http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.ecm0601256/index.html
  33. * Similarly, the AN521 uses the SSE-200, and the SSE-200 TRM defines
  34. * most of the device layout:
  35. * http://infocenter.arm.com/help/topic/com.arm.doc.101104_0100_00_en/corelink_sse200_subsystem_for_embedded_technical_reference_manual_101104_0100_00_en.pdf
  36. *
  37. */
  38. #include "qemu/osdep.h"
  39. #include "qemu/units.h"
  40. #include "qapi/error.h"
  41. #include "qemu/error-report.h"
  42. #include "hw/arm/boot.h"
  43. #include "hw/arm/armv7m.h"
  44. #include "hw/or-irq.h"
  45. #include "hw/boards.h"
  46. #include "exec/address-spaces.h"
  47. #include "sysemu/sysemu.h"
  48. #include "hw/misc/unimp.h"
  49. #include "hw/char/cmsdk-apb-uart.h"
  50. #include "hw/timer/cmsdk-apb-timer.h"
  51. #include "hw/misc/mps2-scc.h"
  52. #include "hw/misc/mps2-fpgaio.h"
  53. #include "hw/misc/tz-mpc.h"
  54. #include "hw/misc/tz-msc.h"
  55. #include "hw/arm/armsse.h"
  56. #include "hw/dma/pl080.h"
  57. #include "hw/ssi/pl022.h"
  58. #include "hw/net/lan9118.h"
  59. #include "net/net.h"
  60. #include "hw/core/split-irq.h"
  61. #define MPS2TZ_NUMIRQ 92
  62. typedef enum MPS2TZFPGAType {
  63. FPGA_AN505,
  64. FPGA_AN521,
  65. } MPS2TZFPGAType;
  66. typedef struct {
  67. MachineClass parent;
  68. MPS2TZFPGAType fpga_type;
  69. uint32_t scc_id;
  70. const char *armsse_type;
  71. } MPS2TZMachineClass;
  72. typedef struct {
  73. MachineState parent;
  74. ARMSSE iotkit;
  75. MemoryRegion psram;
  76. MemoryRegion ssram[3];
  77. MemoryRegion ssram1_m;
  78. MPS2SCC scc;
  79. MPS2FPGAIO fpgaio;
  80. TZPPC ppc[5];
  81. TZMPC ssram_mpc[3];
  82. PL022State spi[5];
  83. UnimplementedDeviceState i2c[4];
  84. UnimplementedDeviceState i2s_audio;
  85. UnimplementedDeviceState gpio[4];
  86. UnimplementedDeviceState gfx;
  87. PL080State dma[4];
  88. TZMSC msc[4];
  89. CMSDKAPBUART uart[5];
  90. SplitIRQ sec_resp_splitter;
  91. qemu_or_irq uart_irq_orgate;
  92. DeviceState *lan9118;
  93. SplitIRQ cpu_irq_splitter[MPS2TZ_NUMIRQ];
  94. } MPS2TZMachineState;
  95. #define TYPE_MPS2TZ_MACHINE "mps2tz"
  96. #define TYPE_MPS2TZ_AN505_MACHINE MACHINE_TYPE_NAME("mps2-an505")
  97. #define TYPE_MPS2TZ_AN521_MACHINE MACHINE_TYPE_NAME("mps2-an521")
  98. #define MPS2TZ_MACHINE(obj) \
  99. OBJECT_CHECK(MPS2TZMachineState, obj, TYPE_MPS2TZ_MACHINE)
  100. #define MPS2TZ_MACHINE_GET_CLASS(obj) \
  101. OBJECT_GET_CLASS(MPS2TZMachineClass, obj, TYPE_MPS2TZ_MACHINE)
  102. #define MPS2TZ_MACHINE_CLASS(klass) \
  103. OBJECT_CLASS_CHECK(MPS2TZMachineClass, klass, TYPE_MPS2TZ_MACHINE)
  104. /* Main SYSCLK frequency in Hz */
  105. #define SYSCLK_FRQ 20000000
  106. /* Create an alias of an entire original MemoryRegion @orig
  107. * located at @base in the memory map.
  108. */
  109. static void make_ram_alias(MemoryRegion *mr, const char *name,
  110. MemoryRegion *orig, hwaddr base)
  111. {
  112. memory_region_init_alias(mr, NULL, name, orig, 0,
  113. memory_region_size(orig));
  114. memory_region_add_subregion(get_system_memory(), base, mr);
  115. }
  116. static qemu_irq get_sse_irq_in(MPS2TZMachineState *mms, int irqno)
  117. {
  118. /* Return a qemu_irq which will signal IRQ n to all CPUs in the SSE. */
  119. MPS2TZMachineClass *mmc = MPS2TZ_MACHINE_GET_CLASS(mms);
  120. assert(irqno < MPS2TZ_NUMIRQ);
  121. switch (mmc->fpga_type) {
  122. case FPGA_AN505:
  123. return qdev_get_gpio_in_named(DEVICE(&mms->iotkit), "EXP_IRQ", irqno);
  124. case FPGA_AN521:
  125. return qdev_get_gpio_in(DEVICE(&mms->cpu_irq_splitter[irqno]), 0);
  126. default:
  127. g_assert_not_reached();
  128. }
  129. }
  130. /* Most of the devices in the AN505 FPGA image sit behind
  131. * Peripheral Protection Controllers. These data structures
  132. * define the layout of which devices sit behind which PPCs.
  133. * The devfn for each port is a function which creates, configures
  134. * and initializes the device, returning the MemoryRegion which
  135. * needs to be plugged into the downstream end of the PPC port.
  136. */
  137. typedef MemoryRegion *MakeDevFn(MPS2TZMachineState *mms, void *opaque,
  138. const char *name, hwaddr size);
  139. typedef struct PPCPortInfo {
  140. const char *name;
  141. MakeDevFn *devfn;
  142. void *opaque;
  143. hwaddr addr;
  144. hwaddr size;
  145. } PPCPortInfo;
  146. typedef struct PPCInfo {
  147. const char *name;
  148. PPCPortInfo ports[TZ_NUM_PORTS];
  149. } PPCInfo;
  150. static MemoryRegion *make_unimp_dev(MPS2TZMachineState *mms,
  151. void *opaque,
  152. const char *name, hwaddr size)
  153. {
  154. /* Initialize, configure and realize a TYPE_UNIMPLEMENTED_DEVICE,
  155. * and return a pointer to its MemoryRegion.
  156. */
  157. UnimplementedDeviceState *uds = opaque;
  158. sysbus_init_child_obj(OBJECT(mms), name, uds,
  159. sizeof(UnimplementedDeviceState),
  160. TYPE_UNIMPLEMENTED_DEVICE);
  161. qdev_prop_set_string(DEVICE(uds), "name", name);
  162. qdev_prop_set_uint64(DEVICE(uds), "size", size);
  163. object_property_set_bool(OBJECT(uds), true, "realized", &error_fatal);
  164. return sysbus_mmio_get_region(SYS_BUS_DEVICE(uds), 0);
  165. }
  166. static MemoryRegion *make_uart(MPS2TZMachineState *mms, void *opaque,
  167. const char *name, hwaddr size)
  168. {
  169. CMSDKAPBUART *uart = opaque;
  170. int i = uart - &mms->uart[0];
  171. int rxirqno = i * 2;
  172. int txirqno = i * 2 + 1;
  173. int combirqno = i + 10;
  174. SysBusDevice *s;
  175. DeviceState *orgate_dev = DEVICE(&mms->uart_irq_orgate);
  176. sysbus_init_child_obj(OBJECT(mms), name, uart, sizeof(mms->uart[0]),
  177. TYPE_CMSDK_APB_UART);
  178. qdev_prop_set_chr(DEVICE(uart), "chardev", serial_hd(i));
  179. qdev_prop_set_uint32(DEVICE(uart), "pclk-frq", SYSCLK_FRQ);
  180. object_property_set_bool(OBJECT(uart), true, "realized", &error_fatal);
  181. s = SYS_BUS_DEVICE(uart);
  182. sysbus_connect_irq(s, 0, get_sse_irq_in(mms, txirqno));
  183. sysbus_connect_irq(s, 1, get_sse_irq_in(mms, rxirqno));
  184. sysbus_connect_irq(s, 2, qdev_get_gpio_in(orgate_dev, i * 2));
  185. sysbus_connect_irq(s, 3, qdev_get_gpio_in(orgate_dev, i * 2 + 1));
  186. sysbus_connect_irq(s, 4, get_sse_irq_in(mms, combirqno));
  187. return sysbus_mmio_get_region(SYS_BUS_DEVICE(uart), 0);
  188. }
  189. static MemoryRegion *make_scc(MPS2TZMachineState *mms, void *opaque,
  190. const char *name, hwaddr size)
  191. {
  192. MPS2SCC *scc = opaque;
  193. DeviceState *sccdev;
  194. MPS2TZMachineClass *mmc = MPS2TZ_MACHINE_GET_CLASS(mms);
  195. sysbus_init_child_obj(OBJECT(mms), "scc", scc,
  196. sizeof(mms->scc), TYPE_MPS2_SCC);
  197. sccdev = DEVICE(scc);
  198. qdev_prop_set_uint32(sccdev, "scc-cfg4", 0x2);
  199. qdev_prop_set_uint32(sccdev, "scc-aid", 0x00200008);
  200. qdev_prop_set_uint32(sccdev, "scc-id", mmc->scc_id);
  201. object_property_set_bool(OBJECT(scc), true, "realized", &error_fatal);
  202. return sysbus_mmio_get_region(SYS_BUS_DEVICE(sccdev), 0);
  203. }
  204. static MemoryRegion *make_fpgaio(MPS2TZMachineState *mms, void *opaque,
  205. const char *name, hwaddr size)
  206. {
  207. MPS2FPGAIO *fpgaio = opaque;
  208. sysbus_init_child_obj(OBJECT(mms), "fpgaio", fpgaio,
  209. sizeof(mms->fpgaio), TYPE_MPS2_FPGAIO);
  210. object_property_set_bool(OBJECT(fpgaio), true, "realized", &error_fatal);
  211. return sysbus_mmio_get_region(SYS_BUS_DEVICE(fpgaio), 0);
  212. }
  213. static MemoryRegion *make_eth_dev(MPS2TZMachineState *mms, void *opaque,
  214. const char *name, hwaddr size)
  215. {
  216. SysBusDevice *s;
  217. NICInfo *nd = &nd_table[0];
  218. /* In hardware this is a LAN9220; the LAN9118 is software compatible
  219. * except that it doesn't support the checksum-offload feature.
  220. */
  221. qemu_check_nic_model(nd, "lan9118");
  222. mms->lan9118 = qdev_create(NULL, TYPE_LAN9118);
  223. qdev_set_nic_properties(mms->lan9118, nd);
  224. qdev_init_nofail(mms->lan9118);
  225. s = SYS_BUS_DEVICE(mms->lan9118);
  226. sysbus_connect_irq(s, 0, get_sse_irq_in(mms, 16));
  227. return sysbus_mmio_get_region(s, 0);
  228. }
  229. static MemoryRegion *make_mpc(MPS2TZMachineState *mms, void *opaque,
  230. const char *name, hwaddr size)
  231. {
  232. TZMPC *mpc = opaque;
  233. int i = mpc - &mms->ssram_mpc[0];
  234. MemoryRegion *ssram = &mms->ssram[i];
  235. MemoryRegion *upstream;
  236. char *mpcname = g_strdup_printf("%s-mpc", name);
  237. static uint32_t ramsize[] = { 0x00400000, 0x00200000, 0x00200000 };
  238. static uint32_t rambase[] = { 0x00000000, 0x28000000, 0x28200000 };
  239. memory_region_init_ram(ssram, NULL, name, ramsize[i], &error_fatal);
  240. sysbus_init_child_obj(OBJECT(mms), mpcname, mpc, sizeof(mms->ssram_mpc[0]),
  241. TYPE_TZ_MPC);
  242. object_property_set_link(OBJECT(mpc), OBJECT(ssram),
  243. "downstream", &error_fatal);
  244. object_property_set_bool(OBJECT(mpc), true, "realized", &error_fatal);
  245. /* Map the upstream end of the MPC into system memory */
  246. upstream = sysbus_mmio_get_region(SYS_BUS_DEVICE(mpc), 1);
  247. memory_region_add_subregion(get_system_memory(), rambase[i], upstream);
  248. /* and connect its interrupt to the IoTKit */
  249. qdev_connect_gpio_out_named(DEVICE(mpc), "irq", 0,
  250. qdev_get_gpio_in_named(DEVICE(&mms->iotkit),
  251. "mpcexp_status", i));
  252. /* The first SSRAM is a special case as it has an alias; accesses to
  253. * the alias region at 0x00400000 must also go to the MPC upstream.
  254. */
  255. if (i == 0) {
  256. make_ram_alias(&mms->ssram1_m, "mps.ssram1_m", upstream, 0x00400000);
  257. }
  258. g_free(mpcname);
  259. /* Return the register interface MR for our caller to map behind the PPC */
  260. return sysbus_mmio_get_region(SYS_BUS_DEVICE(mpc), 0);
  261. }
  262. static MemoryRegion *make_dma(MPS2TZMachineState *mms, void *opaque,
  263. const char *name, hwaddr size)
  264. {
  265. PL080State *dma = opaque;
  266. int i = dma - &mms->dma[0];
  267. SysBusDevice *s;
  268. char *mscname = g_strdup_printf("%s-msc", name);
  269. TZMSC *msc = &mms->msc[i];
  270. DeviceState *iotkitdev = DEVICE(&mms->iotkit);
  271. MemoryRegion *msc_upstream;
  272. MemoryRegion *msc_downstream;
  273. /*
  274. * Each DMA device is a PL081 whose transaction master interface
  275. * is guarded by a Master Security Controller. The downstream end of
  276. * the MSC connects to the IoTKit AHB Slave Expansion port, so the
  277. * DMA devices can see all devices and memory that the CPU does.
  278. */
  279. sysbus_init_child_obj(OBJECT(mms), mscname, msc, sizeof(*msc), TYPE_TZ_MSC);
  280. msc_downstream = sysbus_mmio_get_region(SYS_BUS_DEVICE(&mms->iotkit), 0);
  281. object_property_set_link(OBJECT(msc), OBJECT(msc_downstream),
  282. "downstream", &error_fatal);
  283. object_property_set_link(OBJECT(msc), OBJECT(mms),
  284. "idau", &error_fatal);
  285. object_property_set_bool(OBJECT(msc), true, "realized", &error_fatal);
  286. qdev_connect_gpio_out_named(DEVICE(msc), "irq", 0,
  287. qdev_get_gpio_in_named(iotkitdev,
  288. "mscexp_status", i));
  289. qdev_connect_gpio_out_named(iotkitdev, "mscexp_clear", i,
  290. qdev_get_gpio_in_named(DEVICE(msc),
  291. "irq_clear", 0));
  292. qdev_connect_gpio_out_named(iotkitdev, "mscexp_ns", i,
  293. qdev_get_gpio_in_named(DEVICE(msc),
  294. "cfg_nonsec", 0));
  295. qdev_connect_gpio_out(DEVICE(&mms->sec_resp_splitter),
  296. ARRAY_SIZE(mms->ppc) + i,
  297. qdev_get_gpio_in_named(DEVICE(msc),
  298. "cfg_sec_resp", 0));
  299. msc_upstream = sysbus_mmio_get_region(SYS_BUS_DEVICE(msc), 0);
  300. sysbus_init_child_obj(OBJECT(mms), name, dma, sizeof(*dma), TYPE_PL081);
  301. object_property_set_link(OBJECT(dma), OBJECT(msc_upstream),
  302. "downstream", &error_fatal);
  303. object_property_set_bool(OBJECT(dma), true, "realized", &error_fatal);
  304. s = SYS_BUS_DEVICE(dma);
  305. /* Wire up DMACINTR, DMACINTERR, DMACINTTC */
  306. sysbus_connect_irq(s, 0, get_sse_irq_in(mms, 58 + i * 3));
  307. sysbus_connect_irq(s, 1, get_sse_irq_in(mms, 56 + i * 3));
  308. sysbus_connect_irq(s, 2, get_sse_irq_in(mms, 57 + i * 3));
  309. g_free(mscname);
  310. return sysbus_mmio_get_region(s, 0);
  311. }
  312. static MemoryRegion *make_spi(MPS2TZMachineState *mms, void *opaque,
  313. const char *name, hwaddr size)
  314. {
  315. /*
  316. * The AN505 has five PL022 SPI controllers.
  317. * One of these should have the LCD controller behind it; the others
  318. * are connected only to the FPGA's "general purpose SPI connector"
  319. * or "shield" expansion connectors.
  320. * Note that if we do implement devices behind SPI, the chip select
  321. * lines are set via the "MISC" register in the MPS2 FPGAIO device.
  322. */
  323. PL022State *spi = opaque;
  324. int i = spi - &mms->spi[0];
  325. SysBusDevice *s;
  326. sysbus_init_child_obj(OBJECT(mms), name, spi, sizeof(mms->spi[0]),
  327. TYPE_PL022);
  328. object_property_set_bool(OBJECT(spi), true, "realized", &error_fatal);
  329. s = SYS_BUS_DEVICE(spi);
  330. sysbus_connect_irq(s, 0, get_sse_irq_in(mms, 51 + i));
  331. return sysbus_mmio_get_region(s, 0);
  332. }
  333. static void mps2tz_common_init(MachineState *machine)
  334. {
  335. MPS2TZMachineState *mms = MPS2TZ_MACHINE(machine);
  336. MPS2TZMachineClass *mmc = MPS2TZ_MACHINE_GET_CLASS(mms);
  337. MachineClass *mc = MACHINE_GET_CLASS(machine);
  338. MemoryRegion *system_memory = get_system_memory();
  339. DeviceState *iotkitdev;
  340. DeviceState *dev_splitter;
  341. int i;
  342. if (strcmp(machine->cpu_type, mc->default_cpu_type) != 0) {
  343. error_report("This board can only be used with CPU %s",
  344. mc->default_cpu_type);
  345. exit(1);
  346. }
  347. sysbus_init_child_obj(OBJECT(machine), "iotkit", &mms->iotkit,
  348. sizeof(mms->iotkit), mmc->armsse_type);
  349. iotkitdev = DEVICE(&mms->iotkit);
  350. object_property_set_link(OBJECT(&mms->iotkit), OBJECT(system_memory),
  351. "memory", &error_abort);
  352. qdev_prop_set_uint32(iotkitdev, "EXP_NUMIRQ", MPS2TZ_NUMIRQ);
  353. qdev_prop_set_uint32(iotkitdev, "MAINCLK", SYSCLK_FRQ);
  354. object_property_set_bool(OBJECT(&mms->iotkit), true, "realized",
  355. &error_fatal);
  356. /*
  357. * The AN521 needs us to create splitters to feed the IRQ inputs
  358. * for each CPU in the SSE-200 from each device in the board.
  359. */
  360. if (mmc->fpga_type == FPGA_AN521) {
  361. for (i = 0; i < MPS2TZ_NUMIRQ; i++) {
  362. char *name = g_strdup_printf("mps2-irq-splitter%d", i);
  363. SplitIRQ *splitter = &mms->cpu_irq_splitter[i];
  364. object_initialize_child(OBJECT(machine), name,
  365. splitter, sizeof(*splitter),
  366. TYPE_SPLIT_IRQ, &error_fatal, NULL);
  367. g_free(name);
  368. object_property_set_int(OBJECT(splitter), 2, "num-lines",
  369. &error_fatal);
  370. object_property_set_bool(OBJECT(splitter), true, "realized",
  371. &error_fatal);
  372. qdev_connect_gpio_out(DEVICE(splitter), 0,
  373. qdev_get_gpio_in_named(DEVICE(&mms->iotkit),
  374. "EXP_IRQ", i));
  375. qdev_connect_gpio_out(DEVICE(splitter), 1,
  376. qdev_get_gpio_in_named(DEVICE(&mms->iotkit),
  377. "EXP_CPU1_IRQ", i));
  378. }
  379. }
  380. /* The sec_resp_cfg output from the IoTKit must be split into multiple
  381. * lines, one for each of the PPCs we create here, plus one per MSC.
  382. */
  383. object_initialize_child(OBJECT(machine), "sec-resp-splitter",
  384. &mms->sec_resp_splitter,
  385. sizeof(mms->sec_resp_splitter),
  386. TYPE_SPLIT_IRQ, &error_abort, NULL);
  387. object_property_set_int(OBJECT(&mms->sec_resp_splitter),
  388. ARRAY_SIZE(mms->ppc) + ARRAY_SIZE(mms->msc),
  389. "num-lines", &error_fatal);
  390. object_property_set_bool(OBJECT(&mms->sec_resp_splitter), true,
  391. "realized", &error_fatal);
  392. dev_splitter = DEVICE(&mms->sec_resp_splitter);
  393. qdev_connect_gpio_out_named(iotkitdev, "sec_resp_cfg", 0,
  394. qdev_get_gpio_in(dev_splitter, 0));
  395. /* The IoTKit sets up much of the memory layout, including
  396. * the aliases between secure and non-secure regions in the
  397. * address space. The FPGA itself contains:
  398. *
  399. * 0x00000000..0x003fffff SSRAM1
  400. * 0x00400000..0x007fffff alias of SSRAM1
  401. * 0x28000000..0x283fffff 4MB SSRAM2 + SSRAM3
  402. * 0x40100000..0x4fffffff AHB Master Expansion 1 interface devices
  403. * 0x80000000..0x80ffffff 16MB PSRAM
  404. */
  405. /* The FPGA images have an odd combination of different RAMs,
  406. * because in hardware they are different implementations and
  407. * connected to different buses, giving varying performance/size
  408. * tradeoffs. For QEMU they're all just RAM, though. We arbitrarily
  409. * call the 16MB our "system memory", as it's the largest lump.
  410. */
  411. memory_region_allocate_system_memory(&mms->psram,
  412. NULL, "mps.ram", 16 * MiB);
  413. memory_region_add_subregion(system_memory, 0x80000000, &mms->psram);
  414. /* The overflow IRQs for all UARTs are ORed together.
  415. * Tx, Rx and "combined" IRQs are sent to the NVIC separately.
  416. * Create the OR gate for this.
  417. */
  418. object_initialize_child(OBJECT(mms), "uart-irq-orgate",
  419. &mms->uart_irq_orgate, sizeof(mms->uart_irq_orgate),
  420. TYPE_OR_IRQ, &error_abort, NULL);
  421. object_property_set_int(OBJECT(&mms->uart_irq_orgate), 10, "num-lines",
  422. &error_fatal);
  423. object_property_set_bool(OBJECT(&mms->uart_irq_orgate), true,
  424. "realized", &error_fatal);
  425. qdev_connect_gpio_out(DEVICE(&mms->uart_irq_orgate), 0,
  426. get_sse_irq_in(mms, 15));
  427. /* Most of the devices in the FPGA are behind Peripheral Protection
  428. * Controllers. The required order for initializing things is:
  429. * + initialize the PPC
  430. * + initialize, configure and realize downstream devices
  431. * + connect downstream device MemoryRegions to the PPC
  432. * + realize the PPC
  433. * + map the PPC's MemoryRegions to the places in the address map
  434. * where the downstream devices should appear
  435. * + wire up the PPC's control lines to the IoTKit object
  436. */
  437. const PPCInfo ppcs[] = { {
  438. .name = "apb_ppcexp0",
  439. .ports = {
  440. { "ssram-0", make_mpc, &mms->ssram_mpc[0], 0x58007000, 0x1000 },
  441. { "ssram-1", make_mpc, &mms->ssram_mpc[1], 0x58008000, 0x1000 },
  442. { "ssram-2", make_mpc, &mms->ssram_mpc[2], 0x58009000, 0x1000 },
  443. },
  444. }, {
  445. .name = "apb_ppcexp1",
  446. .ports = {
  447. { "spi0", make_spi, &mms->spi[0], 0x40205000, 0x1000 },
  448. { "spi1", make_spi, &mms->spi[1], 0x40206000, 0x1000 },
  449. { "spi2", make_spi, &mms->spi[2], 0x40209000, 0x1000 },
  450. { "spi3", make_spi, &mms->spi[3], 0x4020a000, 0x1000 },
  451. { "spi4", make_spi, &mms->spi[4], 0x4020b000, 0x1000 },
  452. { "uart0", make_uart, &mms->uart[0], 0x40200000, 0x1000 },
  453. { "uart1", make_uart, &mms->uart[1], 0x40201000, 0x1000 },
  454. { "uart2", make_uart, &mms->uart[2], 0x40202000, 0x1000 },
  455. { "uart3", make_uart, &mms->uart[3], 0x40203000, 0x1000 },
  456. { "uart4", make_uart, &mms->uart[4], 0x40204000, 0x1000 },
  457. { "i2c0", make_unimp_dev, &mms->i2c[0], 0x40207000, 0x1000 },
  458. { "i2c1", make_unimp_dev, &mms->i2c[1], 0x40208000, 0x1000 },
  459. { "i2c2", make_unimp_dev, &mms->i2c[2], 0x4020c000, 0x1000 },
  460. { "i2c3", make_unimp_dev, &mms->i2c[3], 0x4020d000, 0x1000 },
  461. },
  462. }, {
  463. .name = "apb_ppcexp2",
  464. .ports = {
  465. { "scc", make_scc, &mms->scc, 0x40300000, 0x1000 },
  466. { "i2s-audio", make_unimp_dev, &mms->i2s_audio,
  467. 0x40301000, 0x1000 },
  468. { "fpgaio", make_fpgaio, &mms->fpgaio, 0x40302000, 0x1000 },
  469. },
  470. }, {
  471. .name = "ahb_ppcexp0",
  472. .ports = {
  473. { "gfx", make_unimp_dev, &mms->gfx, 0x41000000, 0x140000 },
  474. { "gpio0", make_unimp_dev, &mms->gpio[0], 0x40100000, 0x1000 },
  475. { "gpio1", make_unimp_dev, &mms->gpio[1], 0x40101000, 0x1000 },
  476. { "gpio2", make_unimp_dev, &mms->gpio[2], 0x40102000, 0x1000 },
  477. { "gpio3", make_unimp_dev, &mms->gpio[3], 0x40103000, 0x1000 },
  478. { "eth", make_eth_dev, NULL, 0x42000000, 0x100000 },
  479. },
  480. }, {
  481. .name = "ahb_ppcexp1",
  482. .ports = {
  483. { "dma0", make_dma, &mms->dma[0], 0x40110000, 0x1000 },
  484. { "dma1", make_dma, &mms->dma[1], 0x40111000, 0x1000 },
  485. { "dma2", make_dma, &mms->dma[2], 0x40112000, 0x1000 },
  486. { "dma3", make_dma, &mms->dma[3], 0x40113000, 0x1000 },
  487. },
  488. },
  489. };
  490. for (i = 0; i < ARRAY_SIZE(ppcs); i++) {
  491. const PPCInfo *ppcinfo = &ppcs[i];
  492. TZPPC *ppc = &mms->ppc[i];
  493. DeviceState *ppcdev;
  494. int port;
  495. char *gpioname;
  496. sysbus_init_child_obj(OBJECT(machine), ppcinfo->name, ppc,
  497. sizeof(TZPPC), TYPE_TZ_PPC);
  498. ppcdev = DEVICE(ppc);
  499. for (port = 0; port < TZ_NUM_PORTS; port++) {
  500. const PPCPortInfo *pinfo = &ppcinfo->ports[port];
  501. MemoryRegion *mr;
  502. char *portname;
  503. if (!pinfo->devfn) {
  504. continue;
  505. }
  506. mr = pinfo->devfn(mms, pinfo->opaque, pinfo->name, pinfo->size);
  507. portname = g_strdup_printf("port[%d]", port);
  508. object_property_set_link(OBJECT(ppc), OBJECT(mr),
  509. portname, &error_fatal);
  510. g_free(portname);
  511. }
  512. object_property_set_bool(OBJECT(ppc), true, "realized", &error_fatal);
  513. for (port = 0; port < TZ_NUM_PORTS; port++) {
  514. const PPCPortInfo *pinfo = &ppcinfo->ports[port];
  515. if (!pinfo->devfn) {
  516. continue;
  517. }
  518. sysbus_mmio_map(SYS_BUS_DEVICE(ppc), port, pinfo->addr);
  519. gpioname = g_strdup_printf("%s_nonsec", ppcinfo->name);
  520. qdev_connect_gpio_out_named(iotkitdev, gpioname, port,
  521. qdev_get_gpio_in_named(ppcdev,
  522. "cfg_nonsec",
  523. port));
  524. g_free(gpioname);
  525. gpioname = g_strdup_printf("%s_ap", ppcinfo->name);
  526. qdev_connect_gpio_out_named(iotkitdev, gpioname, port,
  527. qdev_get_gpio_in_named(ppcdev,
  528. "cfg_ap", port));
  529. g_free(gpioname);
  530. }
  531. gpioname = g_strdup_printf("%s_irq_enable", ppcinfo->name);
  532. qdev_connect_gpio_out_named(iotkitdev, gpioname, 0,
  533. qdev_get_gpio_in_named(ppcdev,
  534. "irq_enable", 0));
  535. g_free(gpioname);
  536. gpioname = g_strdup_printf("%s_irq_clear", ppcinfo->name);
  537. qdev_connect_gpio_out_named(iotkitdev, gpioname, 0,
  538. qdev_get_gpio_in_named(ppcdev,
  539. "irq_clear", 0));
  540. g_free(gpioname);
  541. gpioname = g_strdup_printf("%s_irq_status", ppcinfo->name);
  542. qdev_connect_gpio_out_named(ppcdev, "irq", 0,
  543. qdev_get_gpio_in_named(iotkitdev,
  544. gpioname, 0));
  545. g_free(gpioname);
  546. qdev_connect_gpio_out(dev_splitter, i,
  547. qdev_get_gpio_in_named(ppcdev,
  548. "cfg_sec_resp", 0));
  549. }
  550. create_unimplemented_device("FPGA NS PC", 0x48007000, 0x1000);
  551. armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename, 0x400000);
  552. }
  553. static void mps2_tz_idau_check(IDAUInterface *ii, uint32_t address,
  554. int *iregion, bool *exempt, bool *ns, bool *nsc)
  555. {
  556. /*
  557. * The MPS2 TZ FPGA images have IDAUs in them which are connected to
  558. * the Master Security Controllers. Thes have the same logic as
  559. * is used by the IoTKit for the IDAU connected to the CPU, except
  560. * that MSCs don't care about the NSC attribute.
  561. */
  562. int region = extract32(address, 28, 4);
  563. *ns = !(region & 1);
  564. *nsc = false;
  565. /* 0xe0000000..0xe00fffff and 0xf0000000..0xf00fffff are exempt */
  566. *exempt = (address & 0xeff00000) == 0xe0000000;
  567. *iregion = region;
  568. }
  569. static void mps2tz_class_init(ObjectClass *oc, void *data)
  570. {
  571. MachineClass *mc = MACHINE_CLASS(oc);
  572. IDAUInterfaceClass *iic = IDAU_INTERFACE_CLASS(oc);
  573. mc->init = mps2tz_common_init;
  574. iic->check = mps2_tz_idau_check;
  575. }
  576. static void mps2tz_an505_class_init(ObjectClass *oc, void *data)
  577. {
  578. MachineClass *mc = MACHINE_CLASS(oc);
  579. MPS2TZMachineClass *mmc = MPS2TZ_MACHINE_CLASS(oc);
  580. mc->desc = "ARM MPS2 with AN505 FPGA image for Cortex-M33";
  581. mc->default_cpus = 1;
  582. mc->min_cpus = mc->default_cpus;
  583. mc->max_cpus = mc->default_cpus;
  584. mmc->fpga_type = FPGA_AN505;
  585. mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m33");
  586. mmc->scc_id = 0x41045050;
  587. mmc->armsse_type = TYPE_IOTKIT;
  588. }
  589. static void mps2tz_an521_class_init(ObjectClass *oc, void *data)
  590. {
  591. MachineClass *mc = MACHINE_CLASS(oc);
  592. MPS2TZMachineClass *mmc = MPS2TZ_MACHINE_CLASS(oc);
  593. mc->desc = "ARM MPS2 with AN521 FPGA image for dual Cortex-M33";
  594. mc->default_cpus = 2;
  595. mc->min_cpus = mc->default_cpus;
  596. mc->max_cpus = mc->default_cpus;
  597. mmc->fpga_type = FPGA_AN521;
  598. mc->default_cpu_type = ARM_CPU_TYPE_NAME("cortex-m33");
  599. mmc->scc_id = 0x41045210;
  600. mmc->armsse_type = TYPE_SSE200;
  601. }
  602. static const TypeInfo mps2tz_info = {
  603. .name = TYPE_MPS2TZ_MACHINE,
  604. .parent = TYPE_MACHINE,
  605. .abstract = true,
  606. .instance_size = sizeof(MPS2TZMachineState),
  607. .class_size = sizeof(MPS2TZMachineClass),
  608. .class_init = mps2tz_class_init,
  609. .interfaces = (InterfaceInfo[]) {
  610. { TYPE_IDAU_INTERFACE },
  611. { }
  612. },
  613. };
  614. static const TypeInfo mps2tz_an505_info = {
  615. .name = TYPE_MPS2TZ_AN505_MACHINE,
  616. .parent = TYPE_MPS2TZ_MACHINE,
  617. .class_init = mps2tz_an505_class_init,
  618. };
  619. static const TypeInfo mps2tz_an521_info = {
  620. .name = TYPE_MPS2TZ_AN521_MACHINE,
  621. .parent = TYPE_MPS2TZ_MACHINE,
  622. .class_init = mps2tz_an521_class_init,
  623. };
  624. static void mps2tz_machine_init(void)
  625. {
  626. type_register_static(&mps2tz_info);
  627. type_register_static(&mps2tz_an505_info);
  628. type_register_static(&mps2tz_an521_info);
  629. }
  630. type_init(mps2tz_machine_init);