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