2
0

armsse.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. /*
  2. * Arm SSE (Subsystems for Embedded): IoTKit
  3. *
  4. * Copyright (c) 2018 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. #include "qemu/osdep.h"
  12. #include "qemu/log.h"
  13. #include "qemu/module.h"
  14. #include "qemu/bitops.h"
  15. #include "qapi/error.h"
  16. #include "trace.h"
  17. #include "hw/sysbus.h"
  18. #include "migration/vmstate.h"
  19. #include "hw/registerfields.h"
  20. #include "hw/arm/armsse.h"
  21. #include "hw/arm/boot.h"
  22. #include "hw/irq.h"
  23. /* Format of the System Information block SYS_CONFIG register */
  24. typedef enum SysConfigFormat {
  25. IoTKitFormat,
  26. SSE200Format,
  27. } SysConfigFormat;
  28. struct ARMSSEInfo {
  29. const char *name;
  30. int sram_banks;
  31. int num_cpus;
  32. uint32_t sys_version;
  33. uint32_t cpuwait_rst;
  34. SysConfigFormat sys_config_format;
  35. bool has_mhus;
  36. bool has_ppus;
  37. bool has_cachectrl;
  38. bool has_cpusecctrl;
  39. bool has_cpuid;
  40. Property *props;
  41. };
  42. static Property iotkit_properties[] = {
  43. DEFINE_PROP_LINK("memory", ARMSSE, board_memory, TYPE_MEMORY_REGION,
  44. MemoryRegion *),
  45. DEFINE_PROP_UINT32("EXP_NUMIRQ", ARMSSE, exp_numirq, 64),
  46. DEFINE_PROP_UINT32("MAINCLK", ARMSSE, mainclk_frq, 0),
  47. DEFINE_PROP_UINT32("SRAM_ADDR_WIDTH", ARMSSE, sram_addr_width, 15),
  48. DEFINE_PROP_UINT32("init-svtor", ARMSSE, init_svtor, 0x10000000),
  49. DEFINE_PROP_BOOL("CPU0_FPU", ARMSSE, cpu_fpu[0], true),
  50. DEFINE_PROP_BOOL("CPU0_DSP", ARMSSE, cpu_dsp[0], true),
  51. DEFINE_PROP_END_OF_LIST()
  52. };
  53. static Property armsse_properties[] = {
  54. DEFINE_PROP_LINK("memory", ARMSSE, board_memory, TYPE_MEMORY_REGION,
  55. MemoryRegion *),
  56. DEFINE_PROP_UINT32("EXP_NUMIRQ", ARMSSE, exp_numirq, 64),
  57. DEFINE_PROP_UINT32("MAINCLK", ARMSSE, mainclk_frq, 0),
  58. DEFINE_PROP_UINT32("SRAM_ADDR_WIDTH", ARMSSE, sram_addr_width, 15),
  59. DEFINE_PROP_UINT32("init-svtor", ARMSSE, init_svtor, 0x10000000),
  60. DEFINE_PROP_BOOL("CPU0_FPU", ARMSSE, cpu_fpu[0], false),
  61. DEFINE_PROP_BOOL("CPU0_DSP", ARMSSE, cpu_dsp[0], false),
  62. DEFINE_PROP_BOOL("CPU1_FPU", ARMSSE, cpu_fpu[1], true),
  63. DEFINE_PROP_BOOL("CPU1_DSP", ARMSSE, cpu_dsp[1], true),
  64. DEFINE_PROP_END_OF_LIST()
  65. };
  66. static const ARMSSEInfo armsse_variants[] = {
  67. {
  68. .name = TYPE_IOTKIT,
  69. .sram_banks = 1,
  70. .num_cpus = 1,
  71. .sys_version = 0x41743,
  72. .cpuwait_rst = 0,
  73. .sys_config_format = IoTKitFormat,
  74. .has_mhus = false,
  75. .has_ppus = false,
  76. .has_cachectrl = false,
  77. .has_cpusecctrl = false,
  78. .has_cpuid = false,
  79. .props = iotkit_properties,
  80. },
  81. {
  82. .name = TYPE_SSE200,
  83. .sram_banks = 4,
  84. .num_cpus = 2,
  85. .sys_version = 0x22041743,
  86. .cpuwait_rst = 2,
  87. .sys_config_format = SSE200Format,
  88. .has_mhus = true,
  89. .has_ppus = true,
  90. .has_cachectrl = true,
  91. .has_cpusecctrl = true,
  92. .has_cpuid = true,
  93. .props = armsse_properties,
  94. },
  95. };
  96. static uint32_t armsse_sys_config_value(ARMSSE *s, const ARMSSEInfo *info)
  97. {
  98. /* Return the SYS_CONFIG value for this SSE */
  99. uint32_t sys_config;
  100. switch (info->sys_config_format) {
  101. case IoTKitFormat:
  102. sys_config = 0;
  103. sys_config = deposit32(sys_config, 0, 4, info->sram_banks);
  104. sys_config = deposit32(sys_config, 4, 4, s->sram_addr_width - 12);
  105. break;
  106. case SSE200Format:
  107. sys_config = 0;
  108. sys_config = deposit32(sys_config, 0, 4, info->sram_banks);
  109. sys_config = deposit32(sys_config, 4, 5, s->sram_addr_width);
  110. sys_config = deposit32(sys_config, 24, 4, 2);
  111. if (info->num_cpus > 1) {
  112. sys_config = deposit32(sys_config, 10, 1, 1);
  113. sys_config = deposit32(sys_config, 20, 4, info->sram_banks - 1);
  114. sys_config = deposit32(sys_config, 28, 4, 2);
  115. }
  116. break;
  117. default:
  118. g_assert_not_reached();
  119. }
  120. return sys_config;
  121. }
  122. /* Clock frequency in HZ of the 32KHz "slow clock" */
  123. #define S32KCLK (32 * 1000)
  124. /* Is internal IRQ n shared between CPUs in a multi-core SSE ? */
  125. static bool irq_is_common[32] = {
  126. [0 ... 5] = true,
  127. /* 6, 7: per-CPU MHU interrupts */
  128. [8 ... 12] = true,
  129. /* 13: per-CPU icache interrupt */
  130. /* 14: reserved */
  131. [15 ... 20] = true,
  132. /* 21: reserved */
  133. [22 ... 26] = true,
  134. /* 27: reserved */
  135. /* 28, 29: per-CPU CTI interrupts */
  136. /* 30, 31: reserved */
  137. };
  138. /*
  139. * Create an alias region in @container of @size bytes starting at @base
  140. * which mirrors the memory starting at @orig.
  141. */
  142. static void make_alias(ARMSSE *s, MemoryRegion *mr, MemoryRegion *container,
  143. const char *name, hwaddr base, hwaddr size, hwaddr orig)
  144. {
  145. memory_region_init_alias(mr, NULL, name, container, orig, size);
  146. /* The alias is even lower priority than unimplemented_device regions */
  147. memory_region_add_subregion_overlap(container, base, mr, -1500);
  148. }
  149. static void irq_status_forwarder(void *opaque, int n, int level)
  150. {
  151. qemu_irq destirq = opaque;
  152. qemu_set_irq(destirq, level);
  153. }
  154. static void nsccfg_handler(void *opaque, int n, int level)
  155. {
  156. ARMSSE *s = ARMSSE(opaque);
  157. s->nsccfg = level;
  158. }
  159. static void armsse_forward_ppc(ARMSSE *s, const char *ppcname, int ppcnum)
  160. {
  161. /* Each of the 4 AHB and 4 APB PPCs that might be present in a
  162. * system using the ARMSSE has a collection of control lines which
  163. * are provided by the security controller and which we want to
  164. * expose as control lines on the ARMSSE device itself, so the
  165. * code using the ARMSSE can wire them up to the PPCs.
  166. */
  167. SplitIRQ *splitter = &s->ppc_irq_splitter[ppcnum];
  168. DeviceState *armssedev = DEVICE(s);
  169. DeviceState *dev_secctl = DEVICE(&s->secctl);
  170. DeviceState *dev_splitter = DEVICE(splitter);
  171. char *name;
  172. name = g_strdup_printf("%s_nonsec", ppcname);
  173. qdev_pass_gpios(dev_secctl, armssedev, name);
  174. g_free(name);
  175. name = g_strdup_printf("%s_ap", ppcname);
  176. qdev_pass_gpios(dev_secctl, armssedev, name);
  177. g_free(name);
  178. name = g_strdup_printf("%s_irq_enable", ppcname);
  179. qdev_pass_gpios(dev_secctl, armssedev, name);
  180. g_free(name);
  181. name = g_strdup_printf("%s_irq_clear", ppcname);
  182. qdev_pass_gpios(dev_secctl, armssedev, name);
  183. g_free(name);
  184. /* irq_status is a little more tricky, because we need to
  185. * split it so we can send it both to the security controller
  186. * and to our OR gate for the NVIC interrupt line.
  187. * Connect up the splitter's outputs, and create a GPIO input
  188. * which will pass the line state to the input splitter.
  189. */
  190. name = g_strdup_printf("%s_irq_status", ppcname);
  191. qdev_connect_gpio_out(dev_splitter, 0,
  192. qdev_get_gpio_in_named(dev_secctl,
  193. name, 0));
  194. qdev_connect_gpio_out(dev_splitter, 1,
  195. qdev_get_gpio_in(DEVICE(&s->ppc_irq_orgate), ppcnum));
  196. s->irq_status_in[ppcnum] = qdev_get_gpio_in(dev_splitter, 0);
  197. qdev_init_gpio_in_named_with_opaque(armssedev, irq_status_forwarder,
  198. s->irq_status_in[ppcnum], name, 1);
  199. g_free(name);
  200. }
  201. static void armsse_forward_sec_resp_cfg(ARMSSE *s)
  202. {
  203. /* Forward the 3rd output from the splitter device as a
  204. * named GPIO output of the armsse object.
  205. */
  206. DeviceState *dev = DEVICE(s);
  207. DeviceState *dev_splitter = DEVICE(&s->sec_resp_splitter);
  208. qdev_init_gpio_out_named(dev, &s->sec_resp_cfg, "sec_resp_cfg", 1);
  209. s->sec_resp_cfg_in = qemu_allocate_irq(irq_status_forwarder,
  210. s->sec_resp_cfg, 1);
  211. qdev_connect_gpio_out(dev_splitter, 2, s->sec_resp_cfg_in);
  212. }
  213. static void armsse_init(Object *obj)
  214. {
  215. ARMSSE *s = ARMSSE(obj);
  216. ARMSSEClass *asc = ARMSSE_GET_CLASS(obj);
  217. const ARMSSEInfo *info = asc->info;
  218. int i;
  219. assert(info->sram_banks <= MAX_SRAM_BANKS);
  220. assert(info->num_cpus <= SSE_MAX_CPUS);
  221. memory_region_init(&s->container, obj, "armsse-container", UINT64_MAX);
  222. for (i = 0; i < info->num_cpus; i++) {
  223. /*
  224. * We put each CPU in its own cluster as they are logically
  225. * distinct and may be configured differently.
  226. */
  227. char *name;
  228. name = g_strdup_printf("cluster%d", i);
  229. object_initialize_child(obj, name, &s->cluster[i], TYPE_CPU_CLUSTER);
  230. qdev_prop_set_uint32(DEVICE(&s->cluster[i]), "cluster-id", i);
  231. g_free(name);
  232. name = g_strdup_printf("armv7m%d", i);
  233. object_initialize_child(OBJECT(&s->cluster[i]), name, &s->armv7m[i],
  234. TYPE_ARMV7M);
  235. qdev_prop_set_string(DEVICE(&s->armv7m[i]), "cpu-type",
  236. ARM_CPU_TYPE_NAME("cortex-m33"));
  237. g_free(name);
  238. name = g_strdup_printf("arm-sse-cpu-container%d", i);
  239. memory_region_init(&s->cpu_container[i], obj, name, UINT64_MAX);
  240. g_free(name);
  241. if (i > 0) {
  242. name = g_strdup_printf("arm-sse-container-alias%d", i);
  243. memory_region_init_alias(&s->container_alias[i - 1], obj,
  244. name, &s->container, 0, UINT64_MAX);
  245. g_free(name);
  246. }
  247. }
  248. object_initialize_child(obj, "secctl", &s->secctl, TYPE_IOTKIT_SECCTL);
  249. object_initialize_child(obj, "apb-ppc0", &s->apb_ppc0, TYPE_TZ_PPC);
  250. object_initialize_child(obj, "apb-ppc1", &s->apb_ppc1, TYPE_TZ_PPC);
  251. for (i = 0; i < info->sram_banks; i++) {
  252. char *name = g_strdup_printf("mpc%d", i);
  253. object_initialize_child(obj, name, &s->mpc[i], TYPE_TZ_MPC);
  254. g_free(name);
  255. }
  256. object_initialize_child(obj, "mpc-irq-orgate", &s->mpc_irq_orgate,
  257. TYPE_OR_IRQ);
  258. for (i = 0; i < IOTS_NUM_EXP_MPC + info->sram_banks; i++) {
  259. char *name = g_strdup_printf("mpc-irq-splitter-%d", i);
  260. SplitIRQ *splitter = &s->mpc_irq_splitter[i];
  261. object_initialize_child(obj, name, splitter, TYPE_SPLIT_IRQ);
  262. g_free(name);
  263. }
  264. object_initialize_child(obj, "timer0", &s->timer0, TYPE_CMSDK_APB_TIMER);
  265. object_initialize_child(obj, "timer1", &s->timer1, TYPE_CMSDK_APB_TIMER);
  266. object_initialize_child(obj, "s32ktimer", &s->s32ktimer,
  267. TYPE_CMSDK_APB_TIMER);
  268. object_initialize_child(obj, "dualtimer", &s->dualtimer,
  269. TYPE_CMSDK_APB_DUALTIMER);
  270. object_initialize_child(obj, "s32kwatchdog", &s->s32kwatchdog,
  271. TYPE_CMSDK_APB_WATCHDOG);
  272. object_initialize_child(obj, "nswatchdog", &s->nswatchdog,
  273. TYPE_CMSDK_APB_WATCHDOG);
  274. object_initialize_child(obj, "swatchdog", &s->swatchdog,
  275. TYPE_CMSDK_APB_WATCHDOG);
  276. object_initialize_child(obj, "armsse-sysctl", &s->sysctl,
  277. TYPE_IOTKIT_SYSCTL);
  278. object_initialize_child(obj, "armsse-sysinfo", &s->sysinfo,
  279. TYPE_IOTKIT_SYSINFO);
  280. if (info->has_mhus) {
  281. object_initialize_child(obj, "mhu0", &s->mhu[0], TYPE_ARMSSE_MHU);
  282. object_initialize_child(obj, "mhu1", &s->mhu[1], TYPE_ARMSSE_MHU);
  283. }
  284. if (info->has_ppus) {
  285. for (i = 0; i < info->num_cpus; i++) {
  286. char *name = g_strdup_printf("CPU%dCORE_PPU", i);
  287. int ppuidx = CPU0CORE_PPU + i;
  288. object_initialize_child(obj, name, &s->ppu[ppuidx],
  289. TYPE_UNIMPLEMENTED_DEVICE);
  290. g_free(name);
  291. }
  292. object_initialize_child(obj, "DBG_PPU", &s->ppu[DBG_PPU],
  293. TYPE_UNIMPLEMENTED_DEVICE);
  294. for (i = 0; i < info->sram_banks; i++) {
  295. char *name = g_strdup_printf("RAM%d_PPU", i);
  296. int ppuidx = RAM0_PPU + i;
  297. object_initialize_child(obj, name, &s->ppu[ppuidx],
  298. TYPE_UNIMPLEMENTED_DEVICE);
  299. g_free(name);
  300. }
  301. }
  302. if (info->has_cachectrl) {
  303. for (i = 0; i < info->num_cpus; i++) {
  304. char *name = g_strdup_printf("cachectrl%d", i);
  305. object_initialize_child(obj, name, &s->cachectrl[i],
  306. TYPE_UNIMPLEMENTED_DEVICE);
  307. g_free(name);
  308. }
  309. }
  310. if (info->has_cpusecctrl) {
  311. for (i = 0; i < info->num_cpus; i++) {
  312. char *name = g_strdup_printf("cpusecctrl%d", i);
  313. object_initialize_child(obj, name, &s->cpusecctrl[i],
  314. TYPE_UNIMPLEMENTED_DEVICE);
  315. g_free(name);
  316. }
  317. }
  318. if (info->has_cpuid) {
  319. for (i = 0; i < info->num_cpus; i++) {
  320. char *name = g_strdup_printf("cpuid%d", i);
  321. object_initialize_child(obj, name, &s->cpuid[i],
  322. TYPE_ARMSSE_CPUID);
  323. g_free(name);
  324. }
  325. }
  326. object_initialize_child(obj, "nmi-orgate", &s->nmi_orgate, TYPE_OR_IRQ);
  327. object_initialize_child(obj, "ppc-irq-orgate", &s->ppc_irq_orgate,
  328. TYPE_OR_IRQ);
  329. object_initialize_child(obj, "sec-resp-splitter", &s->sec_resp_splitter,
  330. TYPE_SPLIT_IRQ);
  331. for (i = 0; i < ARRAY_SIZE(s->ppc_irq_splitter); i++) {
  332. char *name = g_strdup_printf("ppc-irq-splitter-%d", i);
  333. SplitIRQ *splitter = &s->ppc_irq_splitter[i];
  334. object_initialize_child(obj, name, splitter, TYPE_SPLIT_IRQ);
  335. g_free(name);
  336. }
  337. if (info->num_cpus > 1) {
  338. for (i = 0; i < ARRAY_SIZE(s->cpu_irq_splitter); i++) {
  339. if (irq_is_common[i]) {
  340. char *name = g_strdup_printf("cpu-irq-splitter%d", i);
  341. SplitIRQ *splitter = &s->cpu_irq_splitter[i];
  342. object_initialize_child(obj, name, splitter, TYPE_SPLIT_IRQ);
  343. g_free(name);
  344. }
  345. }
  346. }
  347. }
  348. static void armsse_exp_irq(void *opaque, int n, int level)
  349. {
  350. qemu_irq *irqarray = opaque;
  351. qemu_set_irq(irqarray[n], level);
  352. }
  353. static void armsse_mpcexp_status(void *opaque, int n, int level)
  354. {
  355. ARMSSE *s = ARMSSE(opaque);
  356. qemu_set_irq(s->mpcexp_status_in[n], level);
  357. }
  358. static qemu_irq armsse_get_common_irq_in(ARMSSE *s, int irqno)
  359. {
  360. /*
  361. * Return a qemu_irq which can be used to signal IRQ n to
  362. * all CPUs in the SSE.
  363. */
  364. ARMSSEClass *asc = ARMSSE_GET_CLASS(s);
  365. const ARMSSEInfo *info = asc->info;
  366. assert(irq_is_common[irqno]);
  367. if (info->num_cpus == 1) {
  368. /* Only one CPU -- just connect directly to it */
  369. return qdev_get_gpio_in(DEVICE(&s->armv7m[0]), irqno);
  370. } else {
  371. /* Connect to the splitter which feeds all CPUs */
  372. return qdev_get_gpio_in(DEVICE(&s->cpu_irq_splitter[irqno]), 0);
  373. }
  374. }
  375. static void map_ppu(ARMSSE *s, int ppuidx, const char *name, hwaddr addr)
  376. {
  377. /* Map a PPU unimplemented device stub */
  378. DeviceState *dev = DEVICE(&s->ppu[ppuidx]);
  379. qdev_prop_set_string(dev, "name", name);
  380. qdev_prop_set_uint64(dev, "size", 0x1000);
  381. sysbus_realize(SYS_BUS_DEVICE(dev), &error_fatal);
  382. sysbus_mmio_map(SYS_BUS_DEVICE(&s->ppu[ppuidx]), 0, addr);
  383. }
  384. static void armsse_realize(DeviceState *dev, Error **errp)
  385. {
  386. ARMSSE *s = ARMSSE(dev);
  387. ARMSSEClass *asc = ARMSSE_GET_CLASS(dev);
  388. const ARMSSEInfo *info = asc->info;
  389. int i;
  390. MemoryRegion *mr;
  391. Error *err = NULL;
  392. SysBusDevice *sbd_apb_ppc0;
  393. SysBusDevice *sbd_secctl;
  394. DeviceState *dev_apb_ppc0;
  395. DeviceState *dev_apb_ppc1;
  396. DeviceState *dev_secctl;
  397. DeviceState *dev_splitter;
  398. uint32_t addr_width_max;
  399. if (!s->board_memory) {
  400. error_setg(errp, "memory property was not set");
  401. return;
  402. }
  403. if (!s->mainclk_frq) {
  404. error_setg(errp, "MAINCLK property was not set");
  405. return;
  406. }
  407. assert(info->num_cpus <= SSE_MAX_CPUS);
  408. /* max SRAM_ADDR_WIDTH: 24 - log2(SRAM_NUM_BANK) */
  409. assert(is_power_of_2(info->sram_banks));
  410. addr_width_max = 24 - ctz32(info->sram_banks);
  411. if (s->sram_addr_width < 1 || s->sram_addr_width > addr_width_max) {
  412. error_setg(errp, "SRAM_ADDR_WIDTH must be between 1 and %d",
  413. addr_width_max);
  414. return;
  415. }
  416. /* Handling of which devices should be available only to secure
  417. * code is usually done differently for M profile than for A profile.
  418. * Instead of putting some devices only into the secure address space,
  419. * devices exist in both address spaces but with hard-wired security
  420. * permissions that will cause the CPU to fault for non-secure accesses.
  421. *
  422. * The ARMSSE has an IDAU (Implementation Defined Access Unit),
  423. * which specifies hard-wired security permissions for different
  424. * areas of the physical address space. For the ARMSSE IDAU, the
  425. * top 4 bits of the physical address are the IDAU region ID, and
  426. * if bit 28 (ie the lowest bit of the ID) is 0 then this is an NS
  427. * region, otherwise it is an S region.
  428. *
  429. * The various devices and RAMs are generally all mapped twice,
  430. * once into a region that the IDAU defines as secure and once
  431. * into a non-secure region. They sit behind either a Memory
  432. * Protection Controller (for RAM) or a Peripheral Protection
  433. * Controller (for devices), which allow a more fine grained
  434. * configuration of whether non-secure accesses are permitted.
  435. *
  436. * (The other place that guest software can configure security
  437. * permissions is in the architected SAU (Security Attribution
  438. * Unit), which is entirely inside the CPU. The IDAU can upgrade
  439. * the security attributes for a region to more restrictive than
  440. * the SAU specifies, but cannot downgrade them.)
  441. *
  442. * 0x10000000..0x1fffffff alias of 0x00000000..0x0fffffff
  443. * 0x20000000..0x2007ffff 32KB FPGA block RAM
  444. * 0x30000000..0x3fffffff alias of 0x20000000..0x2fffffff
  445. * 0x40000000..0x4000ffff base peripheral region 1
  446. * 0x40010000..0x4001ffff CPU peripherals (none for ARMSSE)
  447. * 0x40020000..0x4002ffff system control element peripherals
  448. * 0x40080000..0x400fffff base peripheral region 2
  449. * 0x50000000..0x5fffffff alias of 0x40000000..0x4fffffff
  450. */
  451. memory_region_add_subregion_overlap(&s->container, 0, s->board_memory, -2);
  452. for (i = 0; i < info->num_cpus; i++) {
  453. DeviceState *cpudev = DEVICE(&s->armv7m[i]);
  454. Object *cpuobj = OBJECT(&s->armv7m[i]);
  455. int j;
  456. char *gpioname;
  457. qdev_prop_set_uint32(cpudev, "num-irq", s->exp_numirq + 32);
  458. /*
  459. * In real hardware the initial Secure VTOR is set from the INITSVTOR*
  460. * registers in the IoT Kit System Control Register block. In QEMU
  461. * we set the initial value here, and also the reset value of the
  462. * sysctl register, from this object's QOM init-svtor property.
  463. * If the guest changes the INITSVTOR* registers at runtime then the
  464. * code in iotkit-sysctl.c will update the CPU init-svtor property
  465. * (which will then take effect on the next CPU warm-reset).
  466. *
  467. * Note that typically a board using the SSE-200 will have a system
  468. * control processor whose boot firmware initializes the INITSVTOR*
  469. * registers before powering up the CPUs. QEMU doesn't emulate
  470. * the control processor, so instead we behave in the way that the
  471. * firmware does: the initial value should be set by the board code
  472. * (using the init-svtor property on the ARMSSE object) to match
  473. * whatever its firmware does.
  474. */
  475. qdev_prop_set_uint32(cpudev, "init-svtor", s->init_svtor);
  476. /*
  477. * CPUs start powered down if the corresponding bit in the CPUWAIT
  478. * register is 1. In real hardware the CPUWAIT register reset value is
  479. * a configurable property of the SSE-200 (via the CPUWAIT0_RST and
  480. * CPUWAIT1_RST parameters), but since all the boards we care about
  481. * start CPU0 and leave CPU1 powered off, we hard-code that in
  482. * info->cpuwait_rst for now. We can add QOM properties for this
  483. * later if necessary.
  484. */
  485. if (extract32(info->cpuwait_rst, i, 1)) {
  486. if (!object_property_set_bool(cpuobj, "start-powered-off", true,
  487. errp)) {
  488. return;
  489. }
  490. }
  491. if (!s->cpu_fpu[i]) {
  492. if (!object_property_set_bool(cpuobj, "vfp", false, errp)) {
  493. return;
  494. }
  495. }
  496. if (!s->cpu_dsp[i]) {
  497. if (!object_property_set_bool(cpuobj, "dsp", false, errp)) {
  498. return;
  499. }
  500. }
  501. if (i > 0) {
  502. memory_region_add_subregion_overlap(&s->cpu_container[i], 0,
  503. &s->container_alias[i - 1], -1);
  504. } else {
  505. memory_region_add_subregion_overlap(&s->cpu_container[i], 0,
  506. &s->container, -1);
  507. }
  508. object_property_set_link(cpuobj, "memory",
  509. OBJECT(&s->cpu_container[i]), &error_abort);
  510. object_property_set_link(cpuobj, "idau", OBJECT(s), &error_abort);
  511. if (!sysbus_realize(SYS_BUS_DEVICE(cpuobj), errp)) {
  512. return;
  513. }
  514. /*
  515. * The cluster must be realized after the armv7m container, as
  516. * the container's CPU object is only created on realize, and the
  517. * CPU must exist and have been parented into the cluster before
  518. * the cluster is realized.
  519. */
  520. if (!qdev_realize(DEVICE(&s->cluster[i]), NULL, errp)) {
  521. return;
  522. }
  523. /* Connect EXP_IRQ/EXP_CPUn_IRQ GPIOs to the NVIC's lines 32 and up */
  524. s->exp_irqs[i] = g_new(qemu_irq, s->exp_numirq);
  525. for (j = 0; j < s->exp_numirq; j++) {
  526. s->exp_irqs[i][j] = qdev_get_gpio_in(cpudev, j + 32);
  527. }
  528. if (i == 0) {
  529. gpioname = g_strdup("EXP_IRQ");
  530. } else {
  531. gpioname = g_strdup_printf("EXP_CPU%d_IRQ", i);
  532. }
  533. qdev_init_gpio_in_named_with_opaque(dev, armsse_exp_irq,
  534. s->exp_irqs[i],
  535. gpioname, s->exp_numirq);
  536. g_free(gpioname);
  537. }
  538. /* Wire up the splitters that connect common IRQs to all CPUs */
  539. if (info->num_cpus > 1) {
  540. for (i = 0; i < ARRAY_SIZE(s->cpu_irq_splitter); i++) {
  541. if (irq_is_common[i]) {
  542. Object *splitter = OBJECT(&s->cpu_irq_splitter[i]);
  543. DeviceState *devs = DEVICE(splitter);
  544. int cpunum;
  545. if (!object_property_set_int(splitter, "num-lines",
  546. info->num_cpus, errp)) {
  547. return;
  548. }
  549. if (!qdev_realize(DEVICE(splitter), NULL, errp)) {
  550. return;
  551. }
  552. for (cpunum = 0; cpunum < info->num_cpus; cpunum++) {
  553. DeviceState *cpudev = DEVICE(&s->armv7m[cpunum]);
  554. qdev_connect_gpio_out(devs, cpunum,
  555. qdev_get_gpio_in(cpudev, i));
  556. }
  557. }
  558. }
  559. }
  560. /* Set up the big aliases first */
  561. make_alias(s, &s->alias1, &s->container, "alias 1",
  562. 0x10000000, 0x10000000, 0x00000000);
  563. make_alias(s, &s->alias2, &s->container,
  564. "alias 2", 0x30000000, 0x10000000, 0x20000000);
  565. /* The 0x50000000..0x5fffffff region is not a pure alias: it has
  566. * a few extra devices that only appear there (generally the
  567. * control interfaces for the protection controllers).
  568. * We implement this by mapping those devices over the top of this
  569. * alias MR at a higher priority. Some of the devices in this range
  570. * are per-CPU, so we must put this alias in the per-cpu containers.
  571. */
  572. for (i = 0; i < info->num_cpus; i++) {
  573. make_alias(s, &s->alias3[i], &s->cpu_container[i],
  574. "alias 3", 0x50000000, 0x10000000, 0x40000000);
  575. }
  576. /* Security controller */
  577. if (!sysbus_realize(SYS_BUS_DEVICE(&s->secctl), errp)) {
  578. return;
  579. }
  580. sbd_secctl = SYS_BUS_DEVICE(&s->secctl);
  581. dev_secctl = DEVICE(&s->secctl);
  582. sysbus_mmio_map(sbd_secctl, 0, 0x50080000);
  583. sysbus_mmio_map(sbd_secctl, 1, 0x40080000);
  584. s->nsc_cfg_in = qemu_allocate_irq(nsccfg_handler, s, 1);
  585. qdev_connect_gpio_out_named(dev_secctl, "nsc_cfg", 0, s->nsc_cfg_in);
  586. /* The sec_resp_cfg output from the security controller must be split into
  587. * multiple lines, one for each of the PPCs within the ARMSSE and one
  588. * that will be an output from the ARMSSE to the system.
  589. */
  590. if (!object_property_set_int(OBJECT(&s->sec_resp_splitter),
  591. "num-lines", 3, errp)) {
  592. return;
  593. }
  594. if (!qdev_realize(DEVICE(&s->sec_resp_splitter), NULL, errp)) {
  595. return;
  596. }
  597. dev_splitter = DEVICE(&s->sec_resp_splitter);
  598. qdev_connect_gpio_out_named(dev_secctl, "sec_resp_cfg", 0,
  599. qdev_get_gpio_in(dev_splitter, 0));
  600. /* Each SRAM bank lives behind its own Memory Protection Controller */
  601. for (i = 0; i < info->sram_banks; i++) {
  602. char *ramname = g_strdup_printf("armsse.sram%d", i);
  603. SysBusDevice *sbd_mpc;
  604. uint32_t sram_bank_size = 1 << s->sram_addr_width;
  605. memory_region_init_ram(&s->sram[i], NULL, ramname,
  606. sram_bank_size, &err);
  607. g_free(ramname);
  608. if (err) {
  609. error_propagate(errp, err);
  610. return;
  611. }
  612. object_property_set_link(OBJECT(&s->mpc[i]), "downstream",
  613. OBJECT(&s->sram[i]), &error_abort);
  614. if (!sysbus_realize(SYS_BUS_DEVICE(&s->mpc[i]), errp)) {
  615. return;
  616. }
  617. /* Map the upstream end of the MPC into the right place... */
  618. sbd_mpc = SYS_BUS_DEVICE(&s->mpc[i]);
  619. memory_region_add_subregion(&s->container,
  620. 0x20000000 + i * sram_bank_size,
  621. sysbus_mmio_get_region(sbd_mpc, 1));
  622. /* ...and its register interface */
  623. memory_region_add_subregion(&s->container, 0x50083000 + i * 0x1000,
  624. sysbus_mmio_get_region(sbd_mpc, 0));
  625. }
  626. /* We must OR together lines from the MPC splitters to go to the NVIC */
  627. if (!object_property_set_int(OBJECT(&s->mpc_irq_orgate), "num-lines",
  628. IOTS_NUM_EXP_MPC + info->sram_banks,
  629. errp)) {
  630. return;
  631. }
  632. if (!qdev_realize(DEVICE(&s->mpc_irq_orgate), NULL, errp)) {
  633. return;
  634. }
  635. qdev_connect_gpio_out(DEVICE(&s->mpc_irq_orgate), 0,
  636. armsse_get_common_irq_in(s, 9));
  637. /* Devices behind APB PPC0:
  638. * 0x40000000: timer0
  639. * 0x40001000: timer1
  640. * 0x40002000: dual timer
  641. * 0x40003000: MHU0 (SSE-200 only)
  642. * 0x40004000: MHU1 (SSE-200 only)
  643. * We must configure and realize each downstream device and connect
  644. * it to the appropriate PPC port; then we can realize the PPC and
  645. * map its upstream ends to the right place in the container.
  646. */
  647. qdev_prop_set_uint32(DEVICE(&s->timer0), "pclk-frq", s->mainclk_frq);
  648. if (!sysbus_realize(SYS_BUS_DEVICE(&s->timer0), errp)) {
  649. return;
  650. }
  651. sysbus_connect_irq(SYS_BUS_DEVICE(&s->timer0), 0,
  652. armsse_get_common_irq_in(s, 3));
  653. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->timer0), 0);
  654. object_property_set_link(OBJECT(&s->apb_ppc0), "port[0]", OBJECT(mr),
  655. &error_abort);
  656. qdev_prop_set_uint32(DEVICE(&s->timer1), "pclk-frq", s->mainclk_frq);
  657. if (!sysbus_realize(SYS_BUS_DEVICE(&s->timer1), errp)) {
  658. return;
  659. }
  660. sysbus_connect_irq(SYS_BUS_DEVICE(&s->timer1), 0,
  661. armsse_get_common_irq_in(s, 4));
  662. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->timer1), 0);
  663. object_property_set_link(OBJECT(&s->apb_ppc0), "port[1]", OBJECT(mr),
  664. &error_abort);
  665. qdev_prop_set_uint32(DEVICE(&s->dualtimer), "pclk-frq", s->mainclk_frq);
  666. if (!sysbus_realize(SYS_BUS_DEVICE(&s->dualtimer), errp)) {
  667. return;
  668. }
  669. sysbus_connect_irq(SYS_BUS_DEVICE(&s->dualtimer), 0,
  670. armsse_get_common_irq_in(s, 5));
  671. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->dualtimer), 0);
  672. object_property_set_link(OBJECT(&s->apb_ppc0), "port[2]", OBJECT(mr),
  673. &error_abort);
  674. if (info->has_mhus) {
  675. /*
  676. * An SSE-200 with only one CPU should have only one MHU created,
  677. * with the region where the second MHU usually is being RAZ/WI.
  678. * We don't implement that SSE-200 config; if we want to support
  679. * it then this code needs to be enhanced to handle creating the
  680. * RAZ/WI region instead of the second MHU.
  681. */
  682. assert(info->num_cpus == ARRAY_SIZE(s->mhu));
  683. for (i = 0; i < ARRAY_SIZE(s->mhu); i++) {
  684. char *port;
  685. int cpunum;
  686. SysBusDevice *mhu_sbd = SYS_BUS_DEVICE(&s->mhu[i]);
  687. if (!sysbus_realize(SYS_BUS_DEVICE(&s->mhu[i]), errp)) {
  688. return;
  689. }
  690. port = g_strdup_printf("port[%d]", i + 3);
  691. mr = sysbus_mmio_get_region(mhu_sbd, 0);
  692. object_property_set_link(OBJECT(&s->apb_ppc0), port, OBJECT(mr),
  693. &error_abort);
  694. g_free(port);
  695. /*
  696. * Each MHU has an irq line for each CPU:
  697. * MHU 0 irq line 0 -> CPU 0 IRQ 6
  698. * MHU 0 irq line 1 -> CPU 1 IRQ 6
  699. * MHU 1 irq line 0 -> CPU 0 IRQ 7
  700. * MHU 1 irq line 1 -> CPU 1 IRQ 7
  701. */
  702. for (cpunum = 0; cpunum < info->num_cpus; cpunum++) {
  703. DeviceState *cpudev = DEVICE(&s->armv7m[cpunum]);
  704. sysbus_connect_irq(mhu_sbd, cpunum,
  705. qdev_get_gpio_in(cpudev, 6 + i));
  706. }
  707. }
  708. }
  709. if (!sysbus_realize(SYS_BUS_DEVICE(&s->apb_ppc0), errp)) {
  710. return;
  711. }
  712. sbd_apb_ppc0 = SYS_BUS_DEVICE(&s->apb_ppc0);
  713. dev_apb_ppc0 = DEVICE(&s->apb_ppc0);
  714. mr = sysbus_mmio_get_region(sbd_apb_ppc0, 0);
  715. memory_region_add_subregion(&s->container, 0x40000000, mr);
  716. mr = sysbus_mmio_get_region(sbd_apb_ppc0, 1);
  717. memory_region_add_subregion(&s->container, 0x40001000, mr);
  718. mr = sysbus_mmio_get_region(sbd_apb_ppc0, 2);
  719. memory_region_add_subregion(&s->container, 0x40002000, mr);
  720. if (info->has_mhus) {
  721. mr = sysbus_mmio_get_region(sbd_apb_ppc0, 3);
  722. memory_region_add_subregion(&s->container, 0x40003000, mr);
  723. mr = sysbus_mmio_get_region(sbd_apb_ppc0, 4);
  724. memory_region_add_subregion(&s->container, 0x40004000, mr);
  725. }
  726. for (i = 0; i < IOTS_APB_PPC0_NUM_PORTS; i++) {
  727. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc0_nonsec", i,
  728. qdev_get_gpio_in_named(dev_apb_ppc0,
  729. "cfg_nonsec", i));
  730. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc0_ap", i,
  731. qdev_get_gpio_in_named(dev_apb_ppc0,
  732. "cfg_ap", i));
  733. }
  734. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc0_irq_enable", 0,
  735. qdev_get_gpio_in_named(dev_apb_ppc0,
  736. "irq_enable", 0));
  737. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc0_irq_clear", 0,
  738. qdev_get_gpio_in_named(dev_apb_ppc0,
  739. "irq_clear", 0));
  740. qdev_connect_gpio_out(dev_splitter, 0,
  741. qdev_get_gpio_in_named(dev_apb_ppc0,
  742. "cfg_sec_resp", 0));
  743. /* All the PPC irq lines (from the 2 internal PPCs and the 8 external
  744. * ones) are sent individually to the security controller, and also
  745. * ORed together to give a single combined PPC interrupt to the NVIC.
  746. */
  747. if (!object_property_set_int(OBJECT(&s->ppc_irq_orgate),
  748. "num-lines", NUM_PPCS, errp)) {
  749. return;
  750. }
  751. if (!qdev_realize(DEVICE(&s->ppc_irq_orgate), NULL, errp)) {
  752. return;
  753. }
  754. qdev_connect_gpio_out(DEVICE(&s->ppc_irq_orgate), 0,
  755. armsse_get_common_irq_in(s, 10));
  756. /*
  757. * 0x40010000 .. 0x4001ffff (and the 0x5001000... secure-only alias):
  758. * private per-CPU region (all these devices are SSE-200 only):
  759. * 0x50010000: L1 icache control registers
  760. * 0x50011000: CPUSECCTRL (CPU local security control registers)
  761. * 0x4001f000 and 0x5001f000: CPU_IDENTITY register block
  762. */
  763. if (info->has_cachectrl) {
  764. for (i = 0; i < info->num_cpus; i++) {
  765. char *name = g_strdup_printf("cachectrl%d", i);
  766. MemoryRegion *mr;
  767. qdev_prop_set_string(DEVICE(&s->cachectrl[i]), "name", name);
  768. g_free(name);
  769. qdev_prop_set_uint64(DEVICE(&s->cachectrl[i]), "size", 0x1000);
  770. if (!sysbus_realize(SYS_BUS_DEVICE(&s->cachectrl[i]), errp)) {
  771. return;
  772. }
  773. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->cachectrl[i]), 0);
  774. memory_region_add_subregion(&s->cpu_container[i], 0x50010000, mr);
  775. }
  776. }
  777. if (info->has_cpusecctrl) {
  778. for (i = 0; i < info->num_cpus; i++) {
  779. char *name = g_strdup_printf("CPUSECCTRL%d", i);
  780. MemoryRegion *mr;
  781. qdev_prop_set_string(DEVICE(&s->cpusecctrl[i]), "name", name);
  782. g_free(name);
  783. qdev_prop_set_uint64(DEVICE(&s->cpusecctrl[i]), "size", 0x1000);
  784. if (!sysbus_realize(SYS_BUS_DEVICE(&s->cpusecctrl[i]), errp)) {
  785. return;
  786. }
  787. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->cpusecctrl[i]), 0);
  788. memory_region_add_subregion(&s->cpu_container[i], 0x50011000, mr);
  789. }
  790. }
  791. if (info->has_cpuid) {
  792. for (i = 0; i < info->num_cpus; i++) {
  793. MemoryRegion *mr;
  794. qdev_prop_set_uint32(DEVICE(&s->cpuid[i]), "CPUID", i);
  795. if (!sysbus_realize(SYS_BUS_DEVICE(&s->cpuid[i]), errp)) {
  796. return;
  797. }
  798. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->cpuid[i]), 0);
  799. memory_region_add_subregion(&s->cpu_container[i], 0x4001F000, mr);
  800. }
  801. }
  802. /* 0x40020000 .. 0x4002ffff : ARMSSE system control peripheral region */
  803. /* Devices behind APB PPC1:
  804. * 0x4002f000: S32K timer
  805. */
  806. qdev_prop_set_uint32(DEVICE(&s->s32ktimer), "pclk-frq", S32KCLK);
  807. if (!sysbus_realize(SYS_BUS_DEVICE(&s->s32ktimer), errp)) {
  808. return;
  809. }
  810. sysbus_connect_irq(SYS_BUS_DEVICE(&s->s32ktimer), 0,
  811. armsse_get_common_irq_in(s, 2));
  812. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->s32ktimer), 0);
  813. object_property_set_link(OBJECT(&s->apb_ppc1), "port[0]", OBJECT(mr),
  814. &error_abort);
  815. if (!sysbus_realize(SYS_BUS_DEVICE(&s->apb_ppc1), errp)) {
  816. return;
  817. }
  818. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->apb_ppc1), 0);
  819. memory_region_add_subregion(&s->container, 0x4002f000, mr);
  820. dev_apb_ppc1 = DEVICE(&s->apb_ppc1);
  821. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc1_nonsec", 0,
  822. qdev_get_gpio_in_named(dev_apb_ppc1,
  823. "cfg_nonsec", 0));
  824. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc1_ap", 0,
  825. qdev_get_gpio_in_named(dev_apb_ppc1,
  826. "cfg_ap", 0));
  827. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc1_irq_enable", 0,
  828. qdev_get_gpio_in_named(dev_apb_ppc1,
  829. "irq_enable", 0));
  830. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc1_irq_clear", 0,
  831. qdev_get_gpio_in_named(dev_apb_ppc1,
  832. "irq_clear", 0));
  833. qdev_connect_gpio_out(dev_splitter, 1,
  834. qdev_get_gpio_in_named(dev_apb_ppc1,
  835. "cfg_sec_resp", 0));
  836. if (!object_property_set_int(OBJECT(&s->sysinfo), "SYS_VERSION",
  837. info->sys_version, errp)) {
  838. return;
  839. }
  840. if (!object_property_set_int(OBJECT(&s->sysinfo), "SYS_CONFIG",
  841. armsse_sys_config_value(s, info), errp)) {
  842. return;
  843. }
  844. if (!sysbus_realize(SYS_BUS_DEVICE(&s->sysinfo), errp)) {
  845. return;
  846. }
  847. /* System information registers */
  848. sysbus_mmio_map(SYS_BUS_DEVICE(&s->sysinfo), 0, 0x40020000);
  849. /* System control registers */
  850. object_property_set_int(OBJECT(&s->sysctl), "SYS_VERSION",
  851. info->sys_version, &error_abort);
  852. object_property_set_int(OBJECT(&s->sysctl), "CPUWAIT_RST",
  853. info->cpuwait_rst, &error_abort);
  854. object_property_set_int(OBJECT(&s->sysctl), "INITSVTOR0_RST",
  855. s->init_svtor, &error_abort);
  856. object_property_set_int(OBJECT(&s->sysctl), "INITSVTOR1_RST",
  857. s->init_svtor, &error_abort);
  858. if (!sysbus_realize(SYS_BUS_DEVICE(&s->sysctl), errp)) {
  859. return;
  860. }
  861. sysbus_mmio_map(SYS_BUS_DEVICE(&s->sysctl), 0, 0x50021000);
  862. if (info->has_ppus) {
  863. /* CPUnCORE_PPU for each CPU */
  864. for (i = 0; i < info->num_cpus; i++) {
  865. char *name = g_strdup_printf("CPU%dCORE_PPU", i);
  866. map_ppu(s, CPU0CORE_PPU + i, name, 0x50023000 + i * 0x2000);
  867. /*
  868. * We don't support CPU debug so don't create the
  869. * CPU0DEBUG_PPU at 0x50024000 and 0x50026000.
  870. */
  871. g_free(name);
  872. }
  873. map_ppu(s, DBG_PPU, "DBG_PPU", 0x50029000);
  874. for (i = 0; i < info->sram_banks; i++) {
  875. char *name = g_strdup_printf("RAM%d_PPU", i);
  876. map_ppu(s, RAM0_PPU + i, name, 0x5002a000 + i * 0x1000);
  877. g_free(name);
  878. }
  879. }
  880. /* This OR gate wires together outputs from the secure watchdogs to NMI */
  881. if (!object_property_set_int(OBJECT(&s->nmi_orgate), "num-lines", 2,
  882. errp)) {
  883. return;
  884. }
  885. if (!qdev_realize(DEVICE(&s->nmi_orgate), NULL, errp)) {
  886. return;
  887. }
  888. qdev_connect_gpio_out(DEVICE(&s->nmi_orgate), 0,
  889. qdev_get_gpio_in_named(DEVICE(&s->armv7m), "NMI", 0));
  890. qdev_prop_set_uint32(DEVICE(&s->s32kwatchdog), "wdogclk-frq", S32KCLK);
  891. if (!sysbus_realize(SYS_BUS_DEVICE(&s->s32kwatchdog), errp)) {
  892. return;
  893. }
  894. sysbus_connect_irq(SYS_BUS_DEVICE(&s->s32kwatchdog), 0,
  895. qdev_get_gpio_in(DEVICE(&s->nmi_orgate), 0));
  896. sysbus_mmio_map(SYS_BUS_DEVICE(&s->s32kwatchdog), 0, 0x5002e000);
  897. /* 0x40080000 .. 0x4008ffff : ARMSSE second Base peripheral region */
  898. qdev_prop_set_uint32(DEVICE(&s->nswatchdog), "wdogclk-frq", s->mainclk_frq);
  899. if (!sysbus_realize(SYS_BUS_DEVICE(&s->nswatchdog), errp)) {
  900. return;
  901. }
  902. sysbus_connect_irq(SYS_BUS_DEVICE(&s->nswatchdog), 0,
  903. armsse_get_common_irq_in(s, 1));
  904. sysbus_mmio_map(SYS_BUS_DEVICE(&s->nswatchdog), 0, 0x40081000);
  905. qdev_prop_set_uint32(DEVICE(&s->swatchdog), "wdogclk-frq", s->mainclk_frq);
  906. if (!sysbus_realize(SYS_BUS_DEVICE(&s->swatchdog), errp)) {
  907. return;
  908. }
  909. sysbus_connect_irq(SYS_BUS_DEVICE(&s->swatchdog), 0,
  910. qdev_get_gpio_in(DEVICE(&s->nmi_orgate), 1));
  911. sysbus_mmio_map(SYS_BUS_DEVICE(&s->swatchdog), 0, 0x50081000);
  912. for (i = 0; i < ARRAY_SIZE(s->ppc_irq_splitter); i++) {
  913. Object *splitter = OBJECT(&s->ppc_irq_splitter[i]);
  914. if (!object_property_set_int(splitter, "num-lines", 2, errp)) {
  915. return;
  916. }
  917. if (!qdev_realize(DEVICE(splitter), NULL, errp)) {
  918. return;
  919. }
  920. }
  921. for (i = 0; i < IOTS_NUM_AHB_EXP_PPC; i++) {
  922. char *ppcname = g_strdup_printf("ahb_ppcexp%d", i);
  923. armsse_forward_ppc(s, ppcname, i);
  924. g_free(ppcname);
  925. }
  926. for (i = 0; i < IOTS_NUM_APB_EXP_PPC; i++) {
  927. char *ppcname = g_strdup_printf("apb_ppcexp%d", i);
  928. armsse_forward_ppc(s, ppcname, i + IOTS_NUM_AHB_EXP_PPC);
  929. g_free(ppcname);
  930. }
  931. for (i = NUM_EXTERNAL_PPCS; i < NUM_PPCS; i++) {
  932. /* Wire up IRQ splitter for internal PPCs */
  933. DeviceState *devs = DEVICE(&s->ppc_irq_splitter[i]);
  934. char *gpioname = g_strdup_printf("apb_ppc%d_irq_status",
  935. i - NUM_EXTERNAL_PPCS);
  936. TZPPC *ppc = (i == NUM_EXTERNAL_PPCS) ? &s->apb_ppc0 : &s->apb_ppc1;
  937. qdev_connect_gpio_out(devs, 0,
  938. qdev_get_gpio_in_named(dev_secctl, gpioname, 0));
  939. qdev_connect_gpio_out(devs, 1,
  940. qdev_get_gpio_in(DEVICE(&s->ppc_irq_orgate), i));
  941. qdev_connect_gpio_out_named(DEVICE(ppc), "irq", 0,
  942. qdev_get_gpio_in(devs, 0));
  943. g_free(gpioname);
  944. }
  945. /* Wire up the splitters for the MPC IRQs */
  946. for (i = 0; i < IOTS_NUM_EXP_MPC + info->sram_banks; i++) {
  947. SplitIRQ *splitter = &s->mpc_irq_splitter[i];
  948. DeviceState *dev_splitter = DEVICE(splitter);
  949. if (!object_property_set_int(OBJECT(splitter), "num-lines", 2,
  950. errp)) {
  951. return;
  952. }
  953. if (!qdev_realize(DEVICE(splitter), NULL, errp)) {
  954. return;
  955. }
  956. if (i < IOTS_NUM_EXP_MPC) {
  957. /* Splitter input is from GPIO input line */
  958. s->mpcexp_status_in[i] = qdev_get_gpio_in(dev_splitter, 0);
  959. qdev_connect_gpio_out(dev_splitter, 0,
  960. qdev_get_gpio_in_named(dev_secctl,
  961. "mpcexp_status", i));
  962. } else {
  963. /* Splitter input is from our own MPC */
  964. qdev_connect_gpio_out_named(DEVICE(&s->mpc[i - IOTS_NUM_EXP_MPC]),
  965. "irq", 0,
  966. qdev_get_gpio_in(dev_splitter, 0));
  967. qdev_connect_gpio_out(dev_splitter, 0,
  968. qdev_get_gpio_in_named(dev_secctl,
  969. "mpc_status", 0));
  970. }
  971. qdev_connect_gpio_out(dev_splitter, 1,
  972. qdev_get_gpio_in(DEVICE(&s->mpc_irq_orgate), i));
  973. }
  974. /* Create GPIO inputs which will pass the line state for our
  975. * mpcexp_irq inputs to the correct splitter devices.
  976. */
  977. qdev_init_gpio_in_named(dev, armsse_mpcexp_status, "mpcexp_status",
  978. IOTS_NUM_EXP_MPC);
  979. armsse_forward_sec_resp_cfg(s);
  980. /* Forward the MSC related signals */
  981. qdev_pass_gpios(dev_secctl, dev, "mscexp_status");
  982. qdev_pass_gpios(dev_secctl, dev, "mscexp_clear");
  983. qdev_pass_gpios(dev_secctl, dev, "mscexp_ns");
  984. qdev_connect_gpio_out_named(dev_secctl, "msc_irq", 0,
  985. armsse_get_common_irq_in(s, 11));
  986. /*
  987. * Expose our container region to the board model; this corresponds
  988. * to the AHB Slave Expansion ports which allow bus master devices
  989. * (eg DMA controllers) in the board model to make transactions into
  990. * devices in the ARMSSE.
  991. */
  992. sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->container);
  993. system_clock_scale = NANOSECONDS_PER_SECOND / s->mainclk_frq;
  994. }
  995. static void armsse_idau_check(IDAUInterface *ii, uint32_t address,
  996. int *iregion, bool *exempt, bool *ns, bool *nsc)
  997. {
  998. /*
  999. * For ARMSSE systems the IDAU responses are simple logical functions
  1000. * of the address bits. The NSC attribute is guest-adjustable via the
  1001. * NSCCFG register in the security controller.
  1002. */
  1003. ARMSSE *s = ARMSSE(ii);
  1004. int region = extract32(address, 28, 4);
  1005. *ns = !(region & 1);
  1006. *nsc = (region == 1 && (s->nsccfg & 1)) || (region == 3 && (s->nsccfg & 2));
  1007. /* 0xe0000000..0xe00fffff and 0xf0000000..0xf00fffff are exempt */
  1008. *exempt = (address & 0xeff00000) == 0xe0000000;
  1009. *iregion = region;
  1010. }
  1011. static const VMStateDescription armsse_vmstate = {
  1012. .name = "iotkit",
  1013. .version_id = 1,
  1014. .minimum_version_id = 1,
  1015. .fields = (VMStateField[]) {
  1016. VMSTATE_UINT32(nsccfg, ARMSSE),
  1017. VMSTATE_END_OF_LIST()
  1018. }
  1019. };
  1020. static void armsse_reset(DeviceState *dev)
  1021. {
  1022. ARMSSE *s = ARMSSE(dev);
  1023. s->nsccfg = 0;
  1024. }
  1025. static void armsse_class_init(ObjectClass *klass, void *data)
  1026. {
  1027. DeviceClass *dc = DEVICE_CLASS(klass);
  1028. IDAUInterfaceClass *iic = IDAU_INTERFACE_CLASS(klass);
  1029. ARMSSEClass *asc = ARMSSE_CLASS(klass);
  1030. const ARMSSEInfo *info = data;
  1031. dc->realize = armsse_realize;
  1032. dc->vmsd = &armsse_vmstate;
  1033. device_class_set_props(dc, info->props);
  1034. dc->reset = armsse_reset;
  1035. iic->check = armsse_idau_check;
  1036. asc->info = info;
  1037. }
  1038. static const TypeInfo armsse_info = {
  1039. .name = TYPE_ARMSSE,
  1040. .parent = TYPE_SYS_BUS_DEVICE,
  1041. .instance_size = sizeof(ARMSSE),
  1042. .instance_init = armsse_init,
  1043. .abstract = true,
  1044. .interfaces = (InterfaceInfo[]) {
  1045. { TYPE_IDAU_INTERFACE },
  1046. { }
  1047. }
  1048. };
  1049. static void armsse_register_types(void)
  1050. {
  1051. int i;
  1052. type_register_static(&armsse_info);
  1053. for (i = 0; i < ARRAY_SIZE(armsse_variants); i++) {
  1054. TypeInfo ti = {
  1055. .name = armsse_variants[i].name,
  1056. .parent = TYPE_ARMSSE,
  1057. .class_init = armsse_class_init,
  1058. .class_data = (void *)&armsse_variants[i],
  1059. };
  1060. type_register(&ti);
  1061. }
  1062. }
  1063. type_init(armsse_register_types);