armsse.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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],
  230. sizeof(s->cluster[i]), TYPE_CPU_CLUSTER,
  231. &error_abort, NULL);
  232. qdev_prop_set_uint32(DEVICE(&s->cluster[i]), "cluster-id", i);
  233. g_free(name);
  234. name = g_strdup_printf("armv7m%d", i);
  235. sysbus_init_child_obj(OBJECT(&s->cluster[i]), name,
  236. &s->armv7m[i], sizeof(s->armv7m), TYPE_ARMV7M);
  237. qdev_prop_set_string(DEVICE(&s->armv7m[i]), "cpu-type",
  238. ARM_CPU_TYPE_NAME("cortex-m33"));
  239. g_free(name);
  240. name = g_strdup_printf("arm-sse-cpu-container%d", i);
  241. memory_region_init(&s->cpu_container[i], obj, name, UINT64_MAX);
  242. g_free(name);
  243. if (i > 0) {
  244. name = g_strdup_printf("arm-sse-container-alias%d", i);
  245. memory_region_init_alias(&s->container_alias[i - 1], obj,
  246. name, &s->container, 0, UINT64_MAX);
  247. g_free(name);
  248. }
  249. }
  250. sysbus_init_child_obj(obj, "secctl", &s->secctl, sizeof(s->secctl),
  251. TYPE_IOTKIT_SECCTL);
  252. sysbus_init_child_obj(obj, "apb-ppc0", &s->apb_ppc0, sizeof(s->apb_ppc0),
  253. TYPE_TZ_PPC);
  254. sysbus_init_child_obj(obj, "apb-ppc1", &s->apb_ppc1, sizeof(s->apb_ppc1),
  255. TYPE_TZ_PPC);
  256. for (i = 0; i < info->sram_banks; i++) {
  257. char *name = g_strdup_printf("mpc%d", i);
  258. sysbus_init_child_obj(obj, name, &s->mpc[i],
  259. sizeof(s->mpc[i]), TYPE_TZ_MPC);
  260. g_free(name);
  261. }
  262. object_initialize_child(obj, "mpc-irq-orgate", &s->mpc_irq_orgate,
  263. sizeof(s->mpc_irq_orgate), TYPE_OR_IRQ,
  264. &error_abort, NULL);
  265. for (i = 0; i < IOTS_NUM_EXP_MPC + info->sram_banks; i++) {
  266. char *name = g_strdup_printf("mpc-irq-splitter-%d", i);
  267. SplitIRQ *splitter = &s->mpc_irq_splitter[i];
  268. object_initialize_child(obj, name, splitter, sizeof(*splitter),
  269. TYPE_SPLIT_IRQ, &error_abort, NULL);
  270. g_free(name);
  271. }
  272. sysbus_init_child_obj(obj, "timer0", &s->timer0, sizeof(s->timer0),
  273. TYPE_CMSDK_APB_TIMER);
  274. sysbus_init_child_obj(obj, "timer1", &s->timer1, sizeof(s->timer1),
  275. TYPE_CMSDK_APB_TIMER);
  276. sysbus_init_child_obj(obj, "s32ktimer", &s->s32ktimer, sizeof(s->s32ktimer),
  277. TYPE_CMSDK_APB_TIMER);
  278. sysbus_init_child_obj(obj, "dualtimer", &s->dualtimer, sizeof(s->dualtimer),
  279. TYPE_CMSDK_APB_DUALTIMER);
  280. sysbus_init_child_obj(obj, "s32kwatchdog", &s->s32kwatchdog,
  281. sizeof(s->s32kwatchdog), TYPE_CMSDK_APB_WATCHDOG);
  282. sysbus_init_child_obj(obj, "nswatchdog", &s->nswatchdog,
  283. sizeof(s->nswatchdog), TYPE_CMSDK_APB_WATCHDOG);
  284. sysbus_init_child_obj(obj, "swatchdog", &s->swatchdog,
  285. sizeof(s->swatchdog), TYPE_CMSDK_APB_WATCHDOG);
  286. sysbus_init_child_obj(obj, "armsse-sysctl", &s->sysctl,
  287. sizeof(s->sysctl), TYPE_IOTKIT_SYSCTL);
  288. sysbus_init_child_obj(obj, "armsse-sysinfo", &s->sysinfo,
  289. sizeof(s->sysinfo), TYPE_IOTKIT_SYSINFO);
  290. if (info->has_mhus) {
  291. sysbus_init_child_obj(obj, "mhu0", &s->mhu[0], sizeof(s->mhu[0]),
  292. TYPE_ARMSSE_MHU);
  293. sysbus_init_child_obj(obj, "mhu1", &s->mhu[1], sizeof(s->mhu[1]),
  294. TYPE_ARMSSE_MHU);
  295. }
  296. if (info->has_ppus) {
  297. for (i = 0; i < info->num_cpus; i++) {
  298. char *name = g_strdup_printf("CPU%dCORE_PPU", i);
  299. int ppuidx = CPU0CORE_PPU + i;
  300. sysbus_init_child_obj(obj, name, &s->ppu[ppuidx],
  301. sizeof(s->ppu[ppuidx]),
  302. TYPE_UNIMPLEMENTED_DEVICE);
  303. g_free(name);
  304. }
  305. sysbus_init_child_obj(obj, "DBG_PPU", &s->ppu[DBG_PPU],
  306. sizeof(s->ppu[DBG_PPU]),
  307. TYPE_UNIMPLEMENTED_DEVICE);
  308. for (i = 0; i < info->sram_banks; i++) {
  309. char *name = g_strdup_printf("RAM%d_PPU", i);
  310. int ppuidx = RAM0_PPU + i;
  311. sysbus_init_child_obj(obj, name, &s->ppu[ppuidx],
  312. sizeof(s->ppu[ppuidx]),
  313. TYPE_UNIMPLEMENTED_DEVICE);
  314. g_free(name);
  315. }
  316. }
  317. if (info->has_cachectrl) {
  318. for (i = 0; i < info->num_cpus; i++) {
  319. char *name = g_strdup_printf("cachectrl%d", i);
  320. sysbus_init_child_obj(obj, name, &s->cachectrl[i],
  321. sizeof(s->cachectrl[i]),
  322. TYPE_UNIMPLEMENTED_DEVICE);
  323. g_free(name);
  324. }
  325. }
  326. if (info->has_cpusecctrl) {
  327. for (i = 0; i < info->num_cpus; i++) {
  328. char *name = g_strdup_printf("cpusecctrl%d", i);
  329. sysbus_init_child_obj(obj, name, &s->cpusecctrl[i],
  330. sizeof(s->cpusecctrl[i]),
  331. TYPE_UNIMPLEMENTED_DEVICE);
  332. g_free(name);
  333. }
  334. }
  335. if (info->has_cpuid) {
  336. for (i = 0; i < info->num_cpus; i++) {
  337. char *name = g_strdup_printf("cpuid%d", i);
  338. sysbus_init_child_obj(obj, name, &s->cpuid[i],
  339. sizeof(s->cpuid[i]),
  340. TYPE_ARMSSE_CPUID);
  341. g_free(name);
  342. }
  343. }
  344. object_initialize_child(obj, "nmi-orgate", &s->nmi_orgate,
  345. sizeof(s->nmi_orgate), TYPE_OR_IRQ,
  346. &error_abort, NULL);
  347. object_initialize_child(obj, "ppc-irq-orgate", &s->ppc_irq_orgate,
  348. sizeof(s->ppc_irq_orgate), TYPE_OR_IRQ,
  349. &error_abort, NULL);
  350. object_initialize_child(obj, "sec-resp-splitter", &s->sec_resp_splitter,
  351. sizeof(s->sec_resp_splitter), TYPE_SPLIT_IRQ,
  352. &error_abort, NULL);
  353. for (i = 0; i < ARRAY_SIZE(s->ppc_irq_splitter); i++) {
  354. char *name = g_strdup_printf("ppc-irq-splitter-%d", i);
  355. SplitIRQ *splitter = &s->ppc_irq_splitter[i];
  356. object_initialize_child(obj, name, splitter, sizeof(*splitter),
  357. TYPE_SPLIT_IRQ, &error_abort, NULL);
  358. g_free(name);
  359. }
  360. if (info->num_cpus > 1) {
  361. for (i = 0; i < ARRAY_SIZE(s->cpu_irq_splitter); i++) {
  362. if (irq_is_common[i]) {
  363. char *name = g_strdup_printf("cpu-irq-splitter%d", i);
  364. SplitIRQ *splitter = &s->cpu_irq_splitter[i];
  365. object_initialize_child(obj, name, splitter, sizeof(*splitter),
  366. TYPE_SPLIT_IRQ, &error_abort, NULL);
  367. g_free(name);
  368. }
  369. }
  370. }
  371. }
  372. static void armsse_exp_irq(void *opaque, int n, int level)
  373. {
  374. qemu_irq *irqarray = opaque;
  375. qemu_set_irq(irqarray[n], level);
  376. }
  377. static void armsse_mpcexp_status(void *opaque, int n, int level)
  378. {
  379. ARMSSE *s = ARMSSE(opaque);
  380. qemu_set_irq(s->mpcexp_status_in[n], level);
  381. }
  382. static qemu_irq armsse_get_common_irq_in(ARMSSE *s, int irqno)
  383. {
  384. /*
  385. * Return a qemu_irq which can be used to signal IRQ n to
  386. * all CPUs in the SSE.
  387. */
  388. ARMSSEClass *asc = ARMSSE_GET_CLASS(s);
  389. const ARMSSEInfo *info = asc->info;
  390. assert(irq_is_common[irqno]);
  391. if (info->num_cpus == 1) {
  392. /* Only one CPU -- just connect directly to it */
  393. return qdev_get_gpio_in(DEVICE(&s->armv7m[0]), irqno);
  394. } else {
  395. /* Connect to the splitter which feeds all CPUs */
  396. return qdev_get_gpio_in(DEVICE(&s->cpu_irq_splitter[irqno]), 0);
  397. }
  398. }
  399. static void map_ppu(ARMSSE *s, int ppuidx, const char *name, hwaddr addr)
  400. {
  401. /* Map a PPU unimplemented device stub */
  402. DeviceState *dev = DEVICE(&s->ppu[ppuidx]);
  403. qdev_prop_set_string(dev, "name", name);
  404. qdev_prop_set_uint64(dev, "size", 0x1000);
  405. qdev_init_nofail(dev);
  406. sysbus_mmio_map(SYS_BUS_DEVICE(&s->ppu[ppuidx]), 0, addr);
  407. }
  408. static void armsse_realize(DeviceState *dev, Error **errp)
  409. {
  410. ARMSSE *s = ARMSSE(dev);
  411. ARMSSEClass *asc = ARMSSE_GET_CLASS(dev);
  412. const ARMSSEInfo *info = asc->info;
  413. int i;
  414. MemoryRegion *mr;
  415. Error *err = NULL;
  416. SysBusDevice *sbd_apb_ppc0;
  417. SysBusDevice *sbd_secctl;
  418. DeviceState *dev_apb_ppc0;
  419. DeviceState *dev_apb_ppc1;
  420. DeviceState *dev_secctl;
  421. DeviceState *dev_splitter;
  422. uint32_t addr_width_max;
  423. if (!s->board_memory) {
  424. error_setg(errp, "memory property was not set");
  425. return;
  426. }
  427. if (!s->mainclk_frq) {
  428. error_setg(errp, "MAINCLK property was not set");
  429. return;
  430. }
  431. /* max SRAM_ADDR_WIDTH: 24 - log2(SRAM_NUM_BANK) */
  432. assert(is_power_of_2(info->sram_banks));
  433. addr_width_max = 24 - ctz32(info->sram_banks);
  434. if (s->sram_addr_width < 1 || s->sram_addr_width > addr_width_max) {
  435. error_setg(errp, "SRAM_ADDR_WIDTH must be between 1 and %d",
  436. addr_width_max);
  437. return;
  438. }
  439. /* Handling of which devices should be available only to secure
  440. * code is usually done differently for M profile than for A profile.
  441. * Instead of putting some devices only into the secure address space,
  442. * devices exist in both address spaces but with hard-wired security
  443. * permissions that will cause the CPU to fault for non-secure accesses.
  444. *
  445. * The ARMSSE has an IDAU (Implementation Defined Access Unit),
  446. * which specifies hard-wired security permissions for different
  447. * areas of the physical address space. For the ARMSSE IDAU, the
  448. * top 4 bits of the physical address are the IDAU region ID, and
  449. * if bit 28 (ie the lowest bit of the ID) is 0 then this is an NS
  450. * region, otherwise it is an S region.
  451. *
  452. * The various devices and RAMs are generally all mapped twice,
  453. * once into a region that the IDAU defines as secure and once
  454. * into a non-secure region. They sit behind either a Memory
  455. * Protection Controller (for RAM) or a Peripheral Protection
  456. * Controller (for devices), which allow a more fine grained
  457. * configuration of whether non-secure accesses are permitted.
  458. *
  459. * (The other place that guest software can configure security
  460. * permissions is in the architected SAU (Security Attribution
  461. * Unit), which is entirely inside the CPU. The IDAU can upgrade
  462. * the security attributes for a region to more restrictive than
  463. * the SAU specifies, but cannot downgrade them.)
  464. *
  465. * 0x10000000..0x1fffffff alias of 0x00000000..0x0fffffff
  466. * 0x20000000..0x2007ffff 32KB FPGA block RAM
  467. * 0x30000000..0x3fffffff alias of 0x20000000..0x2fffffff
  468. * 0x40000000..0x4000ffff base peripheral region 1
  469. * 0x40010000..0x4001ffff CPU peripherals (none for ARMSSE)
  470. * 0x40020000..0x4002ffff system control element peripherals
  471. * 0x40080000..0x400fffff base peripheral region 2
  472. * 0x50000000..0x5fffffff alias of 0x40000000..0x4fffffff
  473. */
  474. memory_region_add_subregion_overlap(&s->container, 0, s->board_memory, -2);
  475. for (i = 0; i < info->num_cpus; i++) {
  476. DeviceState *cpudev = DEVICE(&s->armv7m[i]);
  477. Object *cpuobj = OBJECT(&s->armv7m[i]);
  478. int j;
  479. char *gpioname;
  480. qdev_prop_set_uint32(cpudev, "num-irq", s->exp_numirq + 32);
  481. /*
  482. * In real hardware the initial Secure VTOR is set from the INITSVTOR*
  483. * registers in the IoT Kit System Control Register block. In QEMU
  484. * we set the initial value here, and also the reset value of the
  485. * sysctl register, from this object's QOM init-svtor property.
  486. * If the guest changes the INITSVTOR* registers at runtime then the
  487. * code in iotkit-sysctl.c will update the CPU init-svtor property
  488. * (which will then take effect on the next CPU warm-reset).
  489. *
  490. * Note that typically a board using the SSE-200 will have a system
  491. * control processor whose boot firmware initializes the INITSVTOR*
  492. * registers before powering up the CPUs. QEMU doesn't emulate
  493. * the control processor, so instead we behave in the way that the
  494. * firmware does: the initial value should be set by the board code
  495. * (using the init-svtor property on the ARMSSE object) to match
  496. * whatever its firmware does.
  497. */
  498. qdev_prop_set_uint32(cpudev, "init-svtor", s->init_svtor);
  499. /*
  500. * CPUs start powered down if the corresponding bit in the CPUWAIT
  501. * register is 1. In real hardware the CPUWAIT register reset value is
  502. * a configurable property of the SSE-200 (via the CPUWAIT0_RST and
  503. * CPUWAIT1_RST parameters), but since all the boards we care about
  504. * start CPU0 and leave CPU1 powered off, we hard-code that in
  505. * info->cpuwait_rst for now. We can add QOM properties for this
  506. * later if necessary.
  507. */
  508. if (extract32(info->cpuwait_rst, i, 1)) {
  509. object_property_set_bool(cpuobj, true, "start-powered-off", &err);
  510. if (err) {
  511. error_propagate(errp, err);
  512. return;
  513. }
  514. }
  515. if (!s->cpu_fpu[i]) {
  516. object_property_set_bool(cpuobj, false, "vfp", &err);
  517. if (err) {
  518. error_propagate(errp, err);
  519. return;
  520. }
  521. }
  522. if (!s->cpu_dsp[i]) {
  523. object_property_set_bool(cpuobj, false, "dsp", &err);
  524. if (err) {
  525. error_propagate(errp, err);
  526. return;
  527. }
  528. }
  529. if (i > 0) {
  530. memory_region_add_subregion_overlap(&s->cpu_container[i], 0,
  531. &s->container_alias[i - 1], -1);
  532. } else {
  533. memory_region_add_subregion_overlap(&s->cpu_container[i], 0,
  534. &s->container, -1);
  535. }
  536. object_property_set_link(cpuobj, OBJECT(&s->cpu_container[i]),
  537. "memory", &err);
  538. if (err) {
  539. error_propagate(errp, err);
  540. return;
  541. }
  542. object_property_set_link(cpuobj, OBJECT(s), "idau", &err);
  543. if (err) {
  544. error_propagate(errp, err);
  545. return;
  546. }
  547. object_property_set_bool(cpuobj, true, "realized", &err);
  548. if (err) {
  549. error_propagate(errp, err);
  550. return;
  551. }
  552. /*
  553. * The cluster must be realized after the armv7m container, as
  554. * the container's CPU object is only created on realize, and the
  555. * CPU must exist and have been parented into the cluster before
  556. * the cluster is realized.
  557. */
  558. object_property_set_bool(OBJECT(&s->cluster[i]),
  559. true, "realized", &err);
  560. if (err) {
  561. error_propagate(errp, err);
  562. return;
  563. }
  564. /* Connect EXP_IRQ/EXP_CPUn_IRQ GPIOs to the NVIC's lines 32 and up */
  565. s->exp_irqs[i] = g_new(qemu_irq, s->exp_numirq);
  566. for (j = 0; j < s->exp_numirq; j++) {
  567. s->exp_irqs[i][j] = qdev_get_gpio_in(cpudev, j + 32);
  568. }
  569. if (i == 0) {
  570. gpioname = g_strdup("EXP_IRQ");
  571. } else {
  572. gpioname = g_strdup_printf("EXP_CPU%d_IRQ", i);
  573. }
  574. qdev_init_gpio_in_named_with_opaque(dev, armsse_exp_irq,
  575. s->exp_irqs[i],
  576. gpioname, s->exp_numirq);
  577. g_free(gpioname);
  578. }
  579. /* Wire up the splitters that connect common IRQs to all CPUs */
  580. if (info->num_cpus > 1) {
  581. for (i = 0; i < ARRAY_SIZE(s->cpu_irq_splitter); i++) {
  582. if (irq_is_common[i]) {
  583. Object *splitter = OBJECT(&s->cpu_irq_splitter[i]);
  584. DeviceState *devs = DEVICE(splitter);
  585. int cpunum;
  586. object_property_set_int(splitter, info->num_cpus,
  587. "num-lines", &err);
  588. if (err) {
  589. error_propagate(errp, err);
  590. return;
  591. }
  592. object_property_set_bool(splitter, true, "realized", &err);
  593. if (err) {
  594. error_propagate(errp, err);
  595. return;
  596. }
  597. for (cpunum = 0; cpunum < info->num_cpus; cpunum++) {
  598. DeviceState *cpudev = DEVICE(&s->armv7m[cpunum]);
  599. qdev_connect_gpio_out(devs, cpunum,
  600. qdev_get_gpio_in(cpudev, i));
  601. }
  602. }
  603. }
  604. }
  605. /* Set up the big aliases first */
  606. make_alias(s, &s->alias1, &s->container, "alias 1",
  607. 0x10000000, 0x10000000, 0x00000000);
  608. make_alias(s, &s->alias2, &s->container,
  609. "alias 2", 0x30000000, 0x10000000, 0x20000000);
  610. /* The 0x50000000..0x5fffffff region is not a pure alias: it has
  611. * a few extra devices that only appear there (generally the
  612. * control interfaces for the protection controllers).
  613. * We implement this by mapping those devices over the top of this
  614. * alias MR at a higher priority. Some of the devices in this range
  615. * are per-CPU, so we must put this alias in the per-cpu containers.
  616. */
  617. for (i = 0; i < info->num_cpus; i++) {
  618. make_alias(s, &s->alias3[i], &s->cpu_container[i],
  619. "alias 3", 0x50000000, 0x10000000, 0x40000000);
  620. }
  621. /* Security controller */
  622. object_property_set_bool(OBJECT(&s->secctl), true, "realized", &err);
  623. if (err) {
  624. error_propagate(errp, err);
  625. return;
  626. }
  627. sbd_secctl = SYS_BUS_DEVICE(&s->secctl);
  628. dev_secctl = DEVICE(&s->secctl);
  629. sysbus_mmio_map(sbd_secctl, 0, 0x50080000);
  630. sysbus_mmio_map(sbd_secctl, 1, 0x40080000);
  631. s->nsc_cfg_in = qemu_allocate_irq(nsccfg_handler, s, 1);
  632. qdev_connect_gpio_out_named(dev_secctl, "nsc_cfg", 0, s->nsc_cfg_in);
  633. /* The sec_resp_cfg output from the security controller must be split into
  634. * multiple lines, one for each of the PPCs within the ARMSSE and one
  635. * that will be an output from the ARMSSE to the system.
  636. */
  637. object_property_set_int(OBJECT(&s->sec_resp_splitter), 3,
  638. "num-lines", &err);
  639. if (err) {
  640. error_propagate(errp, err);
  641. return;
  642. }
  643. object_property_set_bool(OBJECT(&s->sec_resp_splitter), true,
  644. "realized", &err);
  645. if (err) {
  646. error_propagate(errp, err);
  647. return;
  648. }
  649. dev_splitter = DEVICE(&s->sec_resp_splitter);
  650. qdev_connect_gpio_out_named(dev_secctl, "sec_resp_cfg", 0,
  651. qdev_get_gpio_in(dev_splitter, 0));
  652. /* Each SRAM bank lives behind its own Memory Protection Controller */
  653. for (i = 0; i < info->sram_banks; i++) {
  654. char *ramname = g_strdup_printf("armsse.sram%d", i);
  655. SysBusDevice *sbd_mpc;
  656. uint32_t sram_bank_size = 1 << s->sram_addr_width;
  657. memory_region_init_ram(&s->sram[i], NULL, ramname,
  658. sram_bank_size, &err);
  659. g_free(ramname);
  660. if (err) {
  661. error_propagate(errp, err);
  662. return;
  663. }
  664. object_property_set_link(OBJECT(&s->mpc[i]), OBJECT(&s->sram[i]),
  665. "downstream", &err);
  666. if (err) {
  667. error_propagate(errp, err);
  668. return;
  669. }
  670. object_property_set_bool(OBJECT(&s->mpc[i]), true, "realized", &err);
  671. if (err) {
  672. error_propagate(errp, err);
  673. return;
  674. }
  675. /* Map the upstream end of the MPC into the right place... */
  676. sbd_mpc = SYS_BUS_DEVICE(&s->mpc[i]);
  677. memory_region_add_subregion(&s->container,
  678. 0x20000000 + i * sram_bank_size,
  679. sysbus_mmio_get_region(sbd_mpc, 1));
  680. /* ...and its register interface */
  681. memory_region_add_subregion(&s->container, 0x50083000 + i * 0x1000,
  682. sysbus_mmio_get_region(sbd_mpc, 0));
  683. }
  684. /* We must OR together lines from the MPC splitters to go to the NVIC */
  685. object_property_set_int(OBJECT(&s->mpc_irq_orgate),
  686. IOTS_NUM_EXP_MPC + info->sram_banks,
  687. "num-lines", &err);
  688. if (err) {
  689. error_propagate(errp, err);
  690. return;
  691. }
  692. object_property_set_bool(OBJECT(&s->mpc_irq_orgate), true,
  693. "realized", &err);
  694. if (err) {
  695. error_propagate(errp, err);
  696. return;
  697. }
  698. qdev_connect_gpio_out(DEVICE(&s->mpc_irq_orgate), 0,
  699. armsse_get_common_irq_in(s, 9));
  700. /* Devices behind APB PPC0:
  701. * 0x40000000: timer0
  702. * 0x40001000: timer1
  703. * 0x40002000: dual timer
  704. * 0x40003000: MHU0 (SSE-200 only)
  705. * 0x40004000: MHU1 (SSE-200 only)
  706. * We must configure and realize each downstream device and connect
  707. * it to the appropriate PPC port; then we can realize the PPC and
  708. * map its upstream ends to the right place in the container.
  709. */
  710. qdev_prop_set_uint32(DEVICE(&s->timer0), "pclk-frq", s->mainclk_frq);
  711. object_property_set_bool(OBJECT(&s->timer0), true, "realized", &err);
  712. if (err) {
  713. error_propagate(errp, err);
  714. return;
  715. }
  716. sysbus_connect_irq(SYS_BUS_DEVICE(&s->timer0), 0,
  717. armsse_get_common_irq_in(s, 3));
  718. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->timer0), 0);
  719. object_property_set_link(OBJECT(&s->apb_ppc0), OBJECT(mr), "port[0]", &err);
  720. if (err) {
  721. error_propagate(errp, err);
  722. return;
  723. }
  724. qdev_prop_set_uint32(DEVICE(&s->timer1), "pclk-frq", s->mainclk_frq);
  725. object_property_set_bool(OBJECT(&s->timer1), true, "realized", &err);
  726. if (err) {
  727. error_propagate(errp, err);
  728. return;
  729. }
  730. sysbus_connect_irq(SYS_BUS_DEVICE(&s->timer1), 0,
  731. armsse_get_common_irq_in(s, 4));
  732. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->timer1), 0);
  733. object_property_set_link(OBJECT(&s->apb_ppc0), OBJECT(mr), "port[1]", &err);
  734. if (err) {
  735. error_propagate(errp, err);
  736. return;
  737. }
  738. qdev_prop_set_uint32(DEVICE(&s->dualtimer), "pclk-frq", s->mainclk_frq);
  739. object_property_set_bool(OBJECT(&s->dualtimer), true, "realized", &err);
  740. if (err) {
  741. error_propagate(errp, err);
  742. return;
  743. }
  744. sysbus_connect_irq(SYS_BUS_DEVICE(&s->dualtimer), 0,
  745. armsse_get_common_irq_in(s, 5));
  746. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->dualtimer), 0);
  747. object_property_set_link(OBJECT(&s->apb_ppc0), OBJECT(mr), "port[2]", &err);
  748. if (err) {
  749. error_propagate(errp, err);
  750. return;
  751. }
  752. if (info->has_mhus) {
  753. /*
  754. * An SSE-200 with only one CPU should have only one MHU created,
  755. * with the region where the second MHU usually is being RAZ/WI.
  756. * We don't implement that SSE-200 config; if we want to support
  757. * it then this code needs to be enhanced to handle creating the
  758. * RAZ/WI region instead of the second MHU.
  759. */
  760. assert(info->num_cpus == ARRAY_SIZE(s->mhu));
  761. for (i = 0; i < ARRAY_SIZE(s->mhu); i++) {
  762. char *port;
  763. int cpunum;
  764. SysBusDevice *mhu_sbd = SYS_BUS_DEVICE(&s->mhu[i]);
  765. object_property_set_bool(OBJECT(&s->mhu[i]), true,
  766. "realized", &err);
  767. if (err) {
  768. error_propagate(errp, err);
  769. return;
  770. }
  771. port = g_strdup_printf("port[%d]", i + 3);
  772. mr = sysbus_mmio_get_region(mhu_sbd, 0);
  773. object_property_set_link(OBJECT(&s->apb_ppc0), OBJECT(mr),
  774. port, &err);
  775. g_free(port);
  776. if (err) {
  777. error_propagate(errp, err);
  778. return;
  779. }
  780. /*
  781. * Each MHU has an irq line for each CPU:
  782. * MHU 0 irq line 0 -> CPU 0 IRQ 6
  783. * MHU 0 irq line 1 -> CPU 1 IRQ 6
  784. * MHU 1 irq line 0 -> CPU 0 IRQ 7
  785. * MHU 1 irq line 1 -> CPU 1 IRQ 7
  786. */
  787. for (cpunum = 0; cpunum < info->num_cpus; cpunum++) {
  788. DeviceState *cpudev = DEVICE(&s->armv7m[cpunum]);
  789. sysbus_connect_irq(mhu_sbd, cpunum,
  790. qdev_get_gpio_in(cpudev, 6 + i));
  791. }
  792. }
  793. }
  794. object_property_set_bool(OBJECT(&s->apb_ppc0), true, "realized", &err);
  795. if (err) {
  796. error_propagate(errp, err);
  797. return;
  798. }
  799. sbd_apb_ppc0 = SYS_BUS_DEVICE(&s->apb_ppc0);
  800. dev_apb_ppc0 = DEVICE(&s->apb_ppc0);
  801. mr = sysbus_mmio_get_region(sbd_apb_ppc0, 0);
  802. memory_region_add_subregion(&s->container, 0x40000000, mr);
  803. mr = sysbus_mmio_get_region(sbd_apb_ppc0, 1);
  804. memory_region_add_subregion(&s->container, 0x40001000, mr);
  805. mr = sysbus_mmio_get_region(sbd_apb_ppc0, 2);
  806. memory_region_add_subregion(&s->container, 0x40002000, mr);
  807. if (info->has_mhus) {
  808. mr = sysbus_mmio_get_region(sbd_apb_ppc0, 3);
  809. memory_region_add_subregion(&s->container, 0x40003000, mr);
  810. mr = sysbus_mmio_get_region(sbd_apb_ppc0, 4);
  811. memory_region_add_subregion(&s->container, 0x40004000, mr);
  812. }
  813. for (i = 0; i < IOTS_APB_PPC0_NUM_PORTS; i++) {
  814. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc0_nonsec", i,
  815. qdev_get_gpio_in_named(dev_apb_ppc0,
  816. "cfg_nonsec", i));
  817. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc0_ap", i,
  818. qdev_get_gpio_in_named(dev_apb_ppc0,
  819. "cfg_ap", i));
  820. }
  821. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc0_irq_enable", 0,
  822. qdev_get_gpio_in_named(dev_apb_ppc0,
  823. "irq_enable", 0));
  824. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc0_irq_clear", 0,
  825. qdev_get_gpio_in_named(dev_apb_ppc0,
  826. "irq_clear", 0));
  827. qdev_connect_gpio_out(dev_splitter, 0,
  828. qdev_get_gpio_in_named(dev_apb_ppc0,
  829. "cfg_sec_resp", 0));
  830. /* All the PPC irq lines (from the 2 internal PPCs and the 8 external
  831. * ones) are sent individually to the security controller, and also
  832. * ORed together to give a single combined PPC interrupt to the NVIC.
  833. */
  834. object_property_set_int(OBJECT(&s->ppc_irq_orgate),
  835. NUM_PPCS, "num-lines", &err);
  836. if (err) {
  837. error_propagate(errp, err);
  838. return;
  839. }
  840. object_property_set_bool(OBJECT(&s->ppc_irq_orgate), true,
  841. "realized", &err);
  842. if (err) {
  843. error_propagate(errp, err);
  844. return;
  845. }
  846. qdev_connect_gpio_out(DEVICE(&s->ppc_irq_orgate), 0,
  847. armsse_get_common_irq_in(s, 10));
  848. /*
  849. * 0x40010000 .. 0x4001ffff (and the 0x5001000... secure-only alias):
  850. * private per-CPU region (all these devices are SSE-200 only):
  851. * 0x50010000: L1 icache control registers
  852. * 0x50011000: CPUSECCTRL (CPU local security control registers)
  853. * 0x4001f000 and 0x5001f000: CPU_IDENTITY register block
  854. */
  855. if (info->has_cachectrl) {
  856. for (i = 0; i < info->num_cpus; i++) {
  857. char *name = g_strdup_printf("cachectrl%d", i);
  858. MemoryRegion *mr;
  859. qdev_prop_set_string(DEVICE(&s->cachectrl[i]), "name", name);
  860. g_free(name);
  861. qdev_prop_set_uint64(DEVICE(&s->cachectrl[i]), "size", 0x1000);
  862. object_property_set_bool(OBJECT(&s->cachectrl[i]), true,
  863. "realized", &err);
  864. if (err) {
  865. error_propagate(errp, err);
  866. return;
  867. }
  868. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->cachectrl[i]), 0);
  869. memory_region_add_subregion(&s->cpu_container[i], 0x50010000, mr);
  870. }
  871. }
  872. if (info->has_cpusecctrl) {
  873. for (i = 0; i < info->num_cpus; i++) {
  874. char *name = g_strdup_printf("CPUSECCTRL%d", i);
  875. MemoryRegion *mr;
  876. qdev_prop_set_string(DEVICE(&s->cpusecctrl[i]), "name", name);
  877. g_free(name);
  878. qdev_prop_set_uint64(DEVICE(&s->cpusecctrl[i]), "size", 0x1000);
  879. object_property_set_bool(OBJECT(&s->cpusecctrl[i]), true,
  880. "realized", &err);
  881. if (err) {
  882. error_propagate(errp, err);
  883. return;
  884. }
  885. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->cpusecctrl[i]), 0);
  886. memory_region_add_subregion(&s->cpu_container[i], 0x50011000, mr);
  887. }
  888. }
  889. if (info->has_cpuid) {
  890. for (i = 0; i < info->num_cpus; i++) {
  891. MemoryRegion *mr;
  892. qdev_prop_set_uint32(DEVICE(&s->cpuid[i]), "CPUID", i);
  893. object_property_set_bool(OBJECT(&s->cpuid[i]), true,
  894. "realized", &err);
  895. if (err) {
  896. error_propagate(errp, err);
  897. return;
  898. }
  899. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->cpuid[i]), 0);
  900. memory_region_add_subregion(&s->cpu_container[i], 0x4001F000, mr);
  901. }
  902. }
  903. /* 0x40020000 .. 0x4002ffff : ARMSSE system control peripheral region */
  904. /* Devices behind APB PPC1:
  905. * 0x4002f000: S32K timer
  906. */
  907. qdev_prop_set_uint32(DEVICE(&s->s32ktimer), "pclk-frq", S32KCLK);
  908. object_property_set_bool(OBJECT(&s->s32ktimer), true, "realized", &err);
  909. if (err) {
  910. error_propagate(errp, err);
  911. return;
  912. }
  913. sysbus_connect_irq(SYS_BUS_DEVICE(&s->s32ktimer), 0,
  914. armsse_get_common_irq_in(s, 2));
  915. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->s32ktimer), 0);
  916. object_property_set_link(OBJECT(&s->apb_ppc1), OBJECT(mr), "port[0]", &err);
  917. if (err) {
  918. error_propagate(errp, err);
  919. return;
  920. }
  921. object_property_set_bool(OBJECT(&s->apb_ppc1), true, "realized", &err);
  922. if (err) {
  923. error_propagate(errp, err);
  924. return;
  925. }
  926. mr = sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->apb_ppc1), 0);
  927. memory_region_add_subregion(&s->container, 0x4002f000, mr);
  928. dev_apb_ppc1 = DEVICE(&s->apb_ppc1);
  929. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc1_nonsec", 0,
  930. qdev_get_gpio_in_named(dev_apb_ppc1,
  931. "cfg_nonsec", 0));
  932. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc1_ap", 0,
  933. qdev_get_gpio_in_named(dev_apb_ppc1,
  934. "cfg_ap", 0));
  935. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc1_irq_enable", 0,
  936. qdev_get_gpio_in_named(dev_apb_ppc1,
  937. "irq_enable", 0));
  938. qdev_connect_gpio_out_named(dev_secctl, "apb_ppc1_irq_clear", 0,
  939. qdev_get_gpio_in_named(dev_apb_ppc1,
  940. "irq_clear", 0));
  941. qdev_connect_gpio_out(dev_splitter, 1,
  942. qdev_get_gpio_in_named(dev_apb_ppc1,
  943. "cfg_sec_resp", 0));
  944. object_property_set_int(OBJECT(&s->sysinfo), info->sys_version,
  945. "SYS_VERSION", &err);
  946. if (err) {
  947. error_propagate(errp, err);
  948. return;
  949. }
  950. object_property_set_int(OBJECT(&s->sysinfo),
  951. armsse_sys_config_value(s, info),
  952. "SYS_CONFIG", &err);
  953. if (err) {
  954. error_propagate(errp, err);
  955. return;
  956. }
  957. object_property_set_bool(OBJECT(&s->sysinfo), true, "realized", &err);
  958. if (err) {
  959. error_propagate(errp, err);
  960. return;
  961. }
  962. /* System information registers */
  963. sysbus_mmio_map(SYS_BUS_DEVICE(&s->sysinfo), 0, 0x40020000);
  964. /* System control registers */
  965. object_property_set_int(OBJECT(&s->sysctl), info->sys_version,
  966. "SYS_VERSION", &err);
  967. object_property_set_int(OBJECT(&s->sysctl), info->cpuwait_rst,
  968. "CPUWAIT_RST", &err);
  969. object_property_set_int(OBJECT(&s->sysctl), s->init_svtor,
  970. "INITSVTOR0_RST", &err);
  971. object_property_set_int(OBJECT(&s->sysctl), s->init_svtor,
  972. "INITSVTOR1_RST", &err);
  973. object_property_set_bool(OBJECT(&s->sysctl), true, "realized", &err);
  974. if (err) {
  975. error_propagate(errp, err);
  976. return;
  977. }
  978. sysbus_mmio_map(SYS_BUS_DEVICE(&s->sysctl), 0, 0x50021000);
  979. if (info->has_ppus) {
  980. /* CPUnCORE_PPU for each CPU */
  981. for (i = 0; i < info->num_cpus; i++) {
  982. char *name = g_strdup_printf("CPU%dCORE_PPU", i);
  983. map_ppu(s, CPU0CORE_PPU + i, name, 0x50023000 + i * 0x2000);
  984. /*
  985. * We don't support CPU debug so don't create the
  986. * CPU0DEBUG_PPU at 0x50024000 and 0x50026000.
  987. */
  988. g_free(name);
  989. }
  990. map_ppu(s, DBG_PPU, "DBG_PPU", 0x50029000);
  991. for (i = 0; i < info->sram_banks; i++) {
  992. char *name = g_strdup_printf("RAM%d_PPU", i);
  993. map_ppu(s, RAM0_PPU + i, name, 0x5002a000 + i * 0x1000);
  994. g_free(name);
  995. }
  996. }
  997. /* This OR gate wires together outputs from the secure watchdogs to NMI */
  998. object_property_set_int(OBJECT(&s->nmi_orgate), 2, "num-lines", &err);
  999. if (err) {
  1000. error_propagate(errp, err);
  1001. return;
  1002. }
  1003. object_property_set_bool(OBJECT(&s->nmi_orgate), true, "realized", &err);
  1004. if (err) {
  1005. error_propagate(errp, err);
  1006. return;
  1007. }
  1008. qdev_connect_gpio_out(DEVICE(&s->nmi_orgate), 0,
  1009. qdev_get_gpio_in_named(DEVICE(&s->armv7m), "NMI", 0));
  1010. qdev_prop_set_uint32(DEVICE(&s->s32kwatchdog), "wdogclk-frq", S32KCLK);
  1011. object_property_set_bool(OBJECT(&s->s32kwatchdog), true, "realized", &err);
  1012. if (err) {
  1013. error_propagate(errp, err);
  1014. return;
  1015. }
  1016. sysbus_connect_irq(SYS_BUS_DEVICE(&s->s32kwatchdog), 0,
  1017. qdev_get_gpio_in(DEVICE(&s->nmi_orgate), 0));
  1018. sysbus_mmio_map(SYS_BUS_DEVICE(&s->s32kwatchdog), 0, 0x5002e000);
  1019. /* 0x40080000 .. 0x4008ffff : ARMSSE second Base peripheral region */
  1020. qdev_prop_set_uint32(DEVICE(&s->nswatchdog), "wdogclk-frq", s->mainclk_frq);
  1021. object_property_set_bool(OBJECT(&s->nswatchdog), true, "realized", &err);
  1022. if (err) {
  1023. error_propagate(errp, err);
  1024. return;
  1025. }
  1026. sysbus_connect_irq(SYS_BUS_DEVICE(&s->nswatchdog), 0,
  1027. armsse_get_common_irq_in(s, 1));
  1028. sysbus_mmio_map(SYS_BUS_DEVICE(&s->nswatchdog), 0, 0x40081000);
  1029. qdev_prop_set_uint32(DEVICE(&s->swatchdog), "wdogclk-frq", s->mainclk_frq);
  1030. object_property_set_bool(OBJECT(&s->swatchdog), true, "realized", &err);
  1031. if (err) {
  1032. error_propagate(errp, err);
  1033. return;
  1034. }
  1035. sysbus_connect_irq(SYS_BUS_DEVICE(&s->swatchdog), 0,
  1036. qdev_get_gpio_in(DEVICE(&s->nmi_orgate), 1));
  1037. sysbus_mmio_map(SYS_BUS_DEVICE(&s->swatchdog), 0, 0x50081000);
  1038. for (i = 0; i < ARRAY_SIZE(s->ppc_irq_splitter); i++) {
  1039. Object *splitter = OBJECT(&s->ppc_irq_splitter[i]);
  1040. object_property_set_int(splitter, 2, "num-lines", &err);
  1041. if (err) {
  1042. error_propagate(errp, err);
  1043. return;
  1044. }
  1045. object_property_set_bool(splitter, true, "realized", &err);
  1046. if (err) {
  1047. error_propagate(errp, err);
  1048. return;
  1049. }
  1050. }
  1051. for (i = 0; i < IOTS_NUM_AHB_EXP_PPC; i++) {
  1052. char *ppcname = g_strdup_printf("ahb_ppcexp%d", i);
  1053. armsse_forward_ppc(s, ppcname, i);
  1054. g_free(ppcname);
  1055. }
  1056. for (i = 0; i < IOTS_NUM_APB_EXP_PPC; i++) {
  1057. char *ppcname = g_strdup_printf("apb_ppcexp%d", i);
  1058. armsse_forward_ppc(s, ppcname, i + IOTS_NUM_AHB_EXP_PPC);
  1059. g_free(ppcname);
  1060. }
  1061. for (i = NUM_EXTERNAL_PPCS; i < NUM_PPCS; i++) {
  1062. /* Wire up IRQ splitter for internal PPCs */
  1063. DeviceState *devs = DEVICE(&s->ppc_irq_splitter[i]);
  1064. char *gpioname = g_strdup_printf("apb_ppc%d_irq_status",
  1065. i - NUM_EXTERNAL_PPCS);
  1066. TZPPC *ppc = (i == NUM_EXTERNAL_PPCS) ? &s->apb_ppc0 : &s->apb_ppc1;
  1067. qdev_connect_gpio_out(devs, 0,
  1068. qdev_get_gpio_in_named(dev_secctl, gpioname, 0));
  1069. qdev_connect_gpio_out(devs, 1,
  1070. qdev_get_gpio_in(DEVICE(&s->ppc_irq_orgate), i));
  1071. qdev_connect_gpio_out_named(DEVICE(ppc), "irq", 0,
  1072. qdev_get_gpio_in(devs, 0));
  1073. g_free(gpioname);
  1074. }
  1075. /* Wire up the splitters for the MPC IRQs */
  1076. for (i = 0; i < IOTS_NUM_EXP_MPC + info->sram_banks; i++) {
  1077. SplitIRQ *splitter = &s->mpc_irq_splitter[i];
  1078. DeviceState *dev_splitter = DEVICE(splitter);
  1079. object_property_set_int(OBJECT(splitter), 2, "num-lines", &err);
  1080. if (err) {
  1081. error_propagate(errp, err);
  1082. return;
  1083. }
  1084. object_property_set_bool(OBJECT(splitter), true, "realized", &err);
  1085. if (err) {
  1086. error_propagate(errp, err);
  1087. return;
  1088. }
  1089. if (i < IOTS_NUM_EXP_MPC) {
  1090. /* Splitter input is from GPIO input line */
  1091. s->mpcexp_status_in[i] = qdev_get_gpio_in(dev_splitter, 0);
  1092. qdev_connect_gpio_out(dev_splitter, 0,
  1093. qdev_get_gpio_in_named(dev_secctl,
  1094. "mpcexp_status", i));
  1095. } else {
  1096. /* Splitter input is from our own MPC */
  1097. qdev_connect_gpio_out_named(DEVICE(&s->mpc[i - IOTS_NUM_EXP_MPC]),
  1098. "irq", 0,
  1099. qdev_get_gpio_in(dev_splitter, 0));
  1100. qdev_connect_gpio_out(dev_splitter, 0,
  1101. qdev_get_gpio_in_named(dev_secctl,
  1102. "mpc_status", 0));
  1103. }
  1104. qdev_connect_gpio_out(dev_splitter, 1,
  1105. qdev_get_gpio_in(DEVICE(&s->mpc_irq_orgate), i));
  1106. }
  1107. /* Create GPIO inputs which will pass the line state for our
  1108. * mpcexp_irq inputs to the correct splitter devices.
  1109. */
  1110. qdev_init_gpio_in_named(dev, armsse_mpcexp_status, "mpcexp_status",
  1111. IOTS_NUM_EXP_MPC);
  1112. armsse_forward_sec_resp_cfg(s);
  1113. /* Forward the MSC related signals */
  1114. qdev_pass_gpios(dev_secctl, dev, "mscexp_status");
  1115. qdev_pass_gpios(dev_secctl, dev, "mscexp_clear");
  1116. qdev_pass_gpios(dev_secctl, dev, "mscexp_ns");
  1117. qdev_connect_gpio_out_named(dev_secctl, "msc_irq", 0,
  1118. armsse_get_common_irq_in(s, 11));
  1119. /*
  1120. * Expose our container region to the board model; this corresponds
  1121. * to the AHB Slave Expansion ports which allow bus master devices
  1122. * (eg DMA controllers) in the board model to make transactions into
  1123. * devices in the ARMSSE.
  1124. */
  1125. sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->container);
  1126. system_clock_scale = NANOSECONDS_PER_SECOND / s->mainclk_frq;
  1127. }
  1128. static void armsse_idau_check(IDAUInterface *ii, uint32_t address,
  1129. int *iregion, bool *exempt, bool *ns, bool *nsc)
  1130. {
  1131. /*
  1132. * For ARMSSE systems the IDAU responses are simple logical functions
  1133. * of the address bits. The NSC attribute is guest-adjustable via the
  1134. * NSCCFG register in the security controller.
  1135. */
  1136. ARMSSE *s = ARMSSE(ii);
  1137. int region = extract32(address, 28, 4);
  1138. *ns = !(region & 1);
  1139. *nsc = (region == 1 && (s->nsccfg & 1)) || (region == 3 && (s->nsccfg & 2));
  1140. /* 0xe0000000..0xe00fffff and 0xf0000000..0xf00fffff are exempt */
  1141. *exempt = (address & 0xeff00000) == 0xe0000000;
  1142. *iregion = region;
  1143. }
  1144. static const VMStateDescription armsse_vmstate = {
  1145. .name = "iotkit",
  1146. .version_id = 1,
  1147. .minimum_version_id = 1,
  1148. .fields = (VMStateField[]) {
  1149. VMSTATE_UINT32(nsccfg, ARMSSE),
  1150. VMSTATE_END_OF_LIST()
  1151. }
  1152. };
  1153. static void armsse_reset(DeviceState *dev)
  1154. {
  1155. ARMSSE *s = ARMSSE(dev);
  1156. s->nsccfg = 0;
  1157. }
  1158. static void armsse_class_init(ObjectClass *klass, void *data)
  1159. {
  1160. DeviceClass *dc = DEVICE_CLASS(klass);
  1161. IDAUInterfaceClass *iic = IDAU_INTERFACE_CLASS(klass);
  1162. ARMSSEClass *asc = ARMSSE_CLASS(klass);
  1163. const ARMSSEInfo *info = data;
  1164. dc->realize = armsse_realize;
  1165. dc->vmsd = &armsse_vmstate;
  1166. dc->props = info->props;
  1167. dc->reset = armsse_reset;
  1168. iic->check = armsse_idau_check;
  1169. asc->info = info;
  1170. }
  1171. static const TypeInfo armsse_info = {
  1172. .name = TYPE_ARMSSE,
  1173. .parent = TYPE_SYS_BUS_DEVICE,
  1174. .instance_size = sizeof(ARMSSE),
  1175. .instance_init = armsse_init,
  1176. .abstract = true,
  1177. .interfaces = (InterfaceInfo[]) {
  1178. { TYPE_IDAU_INTERFACE },
  1179. { }
  1180. }
  1181. };
  1182. static void armsse_register_types(void)
  1183. {
  1184. int i;
  1185. type_register_static(&armsse_info);
  1186. for (i = 0; i < ARRAY_SIZE(armsse_variants); i++) {
  1187. TypeInfo ti = {
  1188. .name = armsse_variants[i].name,
  1189. .parent = TYPE_ARMSSE,
  1190. .class_init = armsse_class_init,
  1191. .class_data = (void *)&armsse_variants[i],
  1192. };
  1193. type_register(&ti);
  1194. }
  1195. }
  1196. type_init(armsse_register_types);