2 * Arm IoT Kit security controller
4 * Copyright (c) 2018 Linaro Limited
5 * Written by Peter Maydell
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.
12 #include "qemu/osdep.h"
14 #include "qapi/error.h"
16 #include "hw/sysbus.h"
17 #include "hw/registerfields.h"
18 #include "hw/misc/iotkit-secctl.h"
20 /* Registers in the secure privilege control block */
21 REG32(SECRESPCFG
, 0x10)
23 REG32(SECMPCINTSTATUS
, 0x1c)
24 REG32(SECPPCINTSTAT
, 0x20)
25 REG32(SECPPCINTCLR
, 0x24)
26 REG32(SECPPCINTEN
, 0x28)
27 REG32(SECMSCINTSTAT
, 0x30)
28 REG32(SECMSCINTCLR
, 0x34)
29 REG32(SECMSCINTEN
, 0x38)
30 REG32(BRGINTSTAT
, 0x40)
31 REG32(BRGINTCLR
, 0x44)
33 REG32(AHBNSPPC0
, 0x50)
34 REG32(AHBNSPPCEXP0
, 0x60)
35 REG32(AHBNSPPCEXP1
, 0x64)
36 REG32(AHBNSPPCEXP2
, 0x68)
37 REG32(AHBNSPPCEXP3
, 0x6c)
38 REG32(APBNSPPC0
, 0x70)
39 REG32(APBNSPPC1
, 0x74)
40 REG32(APBNSPPCEXP0
, 0x80)
41 REG32(APBNSPPCEXP1
, 0x84)
42 REG32(APBNSPPCEXP2
, 0x88)
43 REG32(APBNSPPCEXP3
, 0x8c)
44 REG32(AHBSPPPC0
, 0x90)
45 REG32(AHBSPPPCEXP0
, 0xa0)
46 REG32(AHBSPPPCEXP1
, 0xa4)
47 REG32(AHBSPPPCEXP2
, 0xa8)
48 REG32(AHBSPPPCEXP3
, 0xac)
49 REG32(APBSPPPC0
, 0xb0)
50 REG32(APBSPPPC1
, 0xb4)
51 REG32(APBSPPPCEXP0
, 0xc0)
52 REG32(APBSPPPCEXP1
, 0xc4)
53 REG32(APBSPPPCEXP2
, 0xc8)
54 REG32(APBSPPPCEXP3
, 0xcc)
69 /* Registers in the non-secure privilege control block */
70 REG32(AHBNSPPPC0
, 0x90)
71 REG32(AHBNSPPPCEXP0
, 0xa0)
72 REG32(AHBNSPPPCEXP1
, 0xa4)
73 REG32(AHBNSPPPCEXP2
, 0xa8)
74 REG32(AHBNSPPPCEXP3
, 0xac)
75 REG32(APBNSPPPC0
, 0xb0)
76 REG32(APBNSPPPC1
, 0xb4)
77 REG32(APBNSPPPCEXP0
, 0xc0)
78 REG32(APBNSPPPCEXP1
, 0xc4)
79 REG32(APBNSPPPCEXP2
, 0xc8)
80 REG32(APBNSPPPCEXP3
, 0xcc)
81 /* PID and CID registers are also present in the NS block */
83 static const uint8_t iotkit_secctl_s_idregs
[] = {
84 0x04, 0x00, 0x00, 0x00,
85 0x52, 0xb8, 0x0b, 0x00,
86 0x0d, 0xf0, 0x05, 0xb1,
89 static const uint8_t iotkit_secctl_ns_idregs
[] = {
90 0x04, 0x00, 0x00, 0x00,
91 0x53, 0xb8, 0x0b, 0x00,
92 0x0d, 0xf0, 0x05, 0xb1,
95 /* The register sets for the various PPCs (AHB internal, APB internal,
96 * AHB expansion, APB expansion) are all set up so that they are
97 * in 16-aligned blocks so offsets 0xN0, 0xN4, 0xN8, 0xNC are PPCs
98 * 0, 1, 2, 3 of that type, so we can convert a register address offset
99 * into an an index into a PPC array easily.
101 static inline int offset_to_ppc_idx(uint32_t offset
)
103 return extract32(offset
, 2, 2);
106 typedef void PerPPCFunction(IoTKitSecCtlPPC
*ppc
);
108 static void foreach_ppc(IoTKitSecCtl
*s
, PerPPCFunction
*fn
)
112 for (i
= 0; i
< IOTS_NUM_APB_PPC
; i
++) {
115 for (i
= 0; i
< IOTS_NUM_APB_EXP_PPC
; i
++) {
118 for (i
= 0; i
< IOTS_NUM_AHB_EXP_PPC
; i
++) {
123 static MemTxResult
iotkit_secctl_s_read(void *opaque
, hwaddr addr
,
125 unsigned size
, MemTxAttrs attrs
)
128 uint32_t offset
= addr
& ~0x3;
129 IoTKitSecCtl
*s
= IOTKIT_SECCTL(opaque
);
142 case A_SECMPCINTSTATUS
:
145 case A_SECPPCINTSTAT
:
146 r
= s
->secppcintstat
;
152 /* QEMU's bus fabric can never report errors as it doesn't buffer
153 * writes, so we never report bridge interrupts.
164 r
= s
->ahbexp
[offset_to_ppc_idx(offset
)].ns
;
168 r
= s
->apb
[offset_to_ppc_idx(offset
)].ns
;
174 r
= s
->apbexp
[offset_to_ppc_idx(offset
)].ns
;
180 r
= s
->apbexp
[offset_to_ppc_idx(offset
)].sp
;
184 r
= s
->apb
[offset_to_ppc_idx(offset
)].sp
;
190 r
= s
->apbexp
[offset_to_ppc_idx(offset
)].sp
;
192 case A_SECMSCINTSTAT
:
195 qemu_log_mask(LOG_UNIMP
,
196 "IoTKit SecCtl S block read: "
197 "unimplemented offset 0x%x\n", offset
);
212 r
= iotkit_secctl_s_idregs
[(offset
- A_PID4
) / 4];
217 qemu_log_mask(LOG_GUEST_ERROR
,
218 "IotKit SecCtl S block read: write-only offset 0x%x\n",
223 qemu_log_mask(LOG_GUEST_ERROR
,
224 "IotKit SecCtl S block read: bad offset 0x%x\n", offset
);
230 /* None of our registers are access-sensitive, so just pull the right
231 * byte out of the word read result.
233 r
= extract32(r
, (addr
& 3) * 8, size
* 8);
236 trace_iotkit_secctl_s_read(offset
, r
, size
);
241 static void iotkit_secctl_update_ppc_ap(IoTKitSecCtlPPC
*ppc
)
245 for (i
= 0; i
< ppc
->numports
; i
++) {
248 if (extract32(ppc
->ns
, i
, 1)) {
249 v
= extract32(ppc
->nsp
, i
, 1);
251 v
= extract32(ppc
->sp
, i
, 1);
253 qemu_set_irq(ppc
->ap
[i
], v
);
257 static void iotkit_secctl_ppc_ns_write(IoTKitSecCtlPPC
*ppc
, uint32_t value
)
261 ppc
->ns
= value
& MAKE_64BIT_MASK(0, ppc
->numports
);
262 for (i
= 0; i
< ppc
->numports
; i
++) {
263 qemu_set_irq(ppc
->nonsec
[i
], extract32(ppc
->ns
, i
, 1));
265 iotkit_secctl_update_ppc_ap(ppc
);
268 static void iotkit_secctl_ppc_sp_write(IoTKitSecCtlPPC
*ppc
, uint32_t value
)
270 ppc
->sp
= value
& MAKE_64BIT_MASK(0, ppc
->numports
);
271 iotkit_secctl_update_ppc_ap(ppc
);
274 static void iotkit_secctl_ppc_nsp_write(IoTKitSecCtlPPC
*ppc
, uint32_t value
)
276 ppc
->nsp
= value
& MAKE_64BIT_MASK(0, ppc
->numports
);
277 iotkit_secctl_update_ppc_ap(ppc
);
280 static void iotkit_secctl_ppc_update_irq_clear(IoTKitSecCtlPPC
*ppc
)
282 uint32_t value
= ppc
->parent
->secppcintstat
;
284 qemu_set_irq(ppc
->irq_clear
, extract32(value
, ppc
->irq_bit_offset
, 1));
287 static void iotkit_secctl_ppc_update_irq_enable(IoTKitSecCtlPPC
*ppc
)
289 uint32_t value
= ppc
->parent
->secppcinten
;
291 qemu_set_irq(ppc
->irq_enable
, extract32(value
, ppc
->irq_bit_offset
, 1));
294 static MemTxResult
iotkit_secctl_s_write(void *opaque
, hwaddr addr
,
296 unsigned size
, MemTxAttrs attrs
)
298 IoTKitSecCtl
*s
= IOTKIT_SECCTL(opaque
);
299 uint32_t offset
= addr
;
300 IoTKitSecCtlPPC
*ppc
;
302 trace_iotkit_secctl_s_write(offset
, value
, size
);
305 /* Byte and halfword writes are ignored */
306 qemu_log_mask(LOG_GUEST_ERROR
,
307 "IotKit SecCtl S block write: bad size, ignored\n");
313 s
->nsccfg
= value
& 3;
314 qemu_set_irq(s
->nsc_cfg_irq
, s
->nsccfg
);
318 s
->secrespcfg
= value
;
319 qemu_set_irq(s
->sec_resp_cfg
, s
->secrespcfg
);
323 foreach_ppc(s
, iotkit_secctl_ppc_update_irq_clear
);
326 s
->secppcinten
= value
& 0x00f000f3;
327 foreach_ppc(s
, iotkit_secctl_ppc_update_irq_enable
);
332 s
->brginten
= value
& 0xffff0000;
338 ppc
= &s
->ahbexp
[offset_to_ppc_idx(offset
)];
339 iotkit_secctl_ppc_ns_write(ppc
, value
);
343 ppc
= &s
->apb
[offset_to_ppc_idx(offset
)];
344 iotkit_secctl_ppc_ns_write(ppc
, value
);
350 ppc
= &s
->apbexp
[offset_to_ppc_idx(offset
)];
351 iotkit_secctl_ppc_ns_write(ppc
, value
);
357 ppc
= &s
->ahbexp
[offset_to_ppc_idx(offset
)];
358 iotkit_secctl_ppc_sp_write(ppc
, value
);
362 ppc
= &s
->apb
[offset_to_ppc_idx(offset
)];
363 iotkit_secctl_ppc_sp_write(ppc
, value
);
369 ppc
= &s
->apbexp
[offset_to_ppc_idx(offset
)];
370 iotkit_secctl_ppc_sp_write(ppc
, value
);
374 qemu_log_mask(LOG_UNIMP
,
375 "IoTKit SecCtl S block write: "
376 "unimplemented offset 0x%x\n", offset
);
378 case A_SECMPCINTSTATUS
:
379 case A_SECPPCINTSTAT
:
380 case A_SECMSCINTSTAT
:
397 qemu_log_mask(LOG_GUEST_ERROR
,
398 "IoTKit SecCtl S block write: "
399 "read-only offset 0x%x\n", offset
);
402 qemu_log_mask(LOG_GUEST_ERROR
,
403 "IotKit SecCtl S block write: bad offset 0x%x\n",
411 static MemTxResult
iotkit_secctl_ns_read(void *opaque
, hwaddr addr
,
413 unsigned size
, MemTxAttrs attrs
)
415 IoTKitSecCtl
*s
= IOTKIT_SECCTL(opaque
);
417 uint32_t offset
= addr
& ~0x3;
423 case A_AHBNSPPPCEXP0
:
424 case A_AHBNSPPPCEXP1
:
425 case A_AHBNSPPPCEXP2
:
426 case A_AHBNSPPPCEXP3
:
427 r
= s
->ahbexp
[offset_to_ppc_idx(offset
)].nsp
;
431 r
= s
->apb
[offset_to_ppc_idx(offset
)].nsp
;
433 case A_APBNSPPPCEXP0
:
434 case A_APBNSPPPCEXP1
:
435 case A_APBNSPPPCEXP2
:
436 case A_APBNSPPPCEXP3
:
437 r
= s
->apbexp
[offset_to_ppc_idx(offset
)].nsp
;
451 r
= iotkit_secctl_ns_idregs
[(offset
- A_PID4
) / 4];
454 qemu_log_mask(LOG_GUEST_ERROR
,
455 "IotKit SecCtl NS block write: bad offset 0x%x\n",
462 /* None of our registers are access-sensitive, so just pull the right
463 * byte out of the word read result.
465 r
= extract32(r
, (addr
& 3) * 8, size
* 8);
468 trace_iotkit_secctl_ns_read(offset
, r
, size
);
473 static MemTxResult
iotkit_secctl_ns_write(void *opaque
, hwaddr addr
,
475 unsigned size
, MemTxAttrs attrs
)
477 IoTKitSecCtl
*s
= IOTKIT_SECCTL(opaque
);
478 uint32_t offset
= addr
;
479 IoTKitSecCtlPPC
*ppc
;
481 trace_iotkit_secctl_ns_write(offset
, value
, size
);
484 /* Byte and halfword writes are ignored */
485 qemu_log_mask(LOG_GUEST_ERROR
,
486 "IotKit SecCtl NS block write: bad size, ignored\n");
491 case A_AHBNSPPPCEXP0
:
492 case A_AHBNSPPPCEXP1
:
493 case A_AHBNSPPPCEXP2
:
494 case A_AHBNSPPPCEXP3
:
495 ppc
= &s
->ahbexp
[offset_to_ppc_idx(offset
)];
496 iotkit_secctl_ppc_nsp_write(ppc
, value
);
500 ppc
= &s
->apb
[offset_to_ppc_idx(offset
)];
501 iotkit_secctl_ppc_nsp_write(ppc
, value
);
503 case A_APBNSPPPCEXP0
:
504 case A_APBNSPPPCEXP1
:
505 case A_APBNSPPPCEXP2
:
506 case A_APBNSPPPCEXP3
:
507 ppc
= &s
->apbexp
[offset_to_ppc_idx(offset
)];
508 iotkit_secctl_ppc_nsp_write(ppc
, value
);
523 qemu_log_mask(LOG_GUEST_ERROR
,
524 "IoTKit SecCtl NS block write: "
525 "read-only offset 0x%x\n", offset
);
528 qemu_log_mask(LOG_GUEST_ERROR
,
529 "IotKit SecCtl NS block write: bad offset 0x%x\n",
537 static const MemoryRegionOps iotkit_secctl_s_ops
= {
538 .read_with_attrs
= iotkit_secctl_s_read
,
539 .write_with_attrs
= iotkit_secctl_s_write
,
540 .endianness
= DEVICE_LITTLE_ENDIAN
,
541 .valid
.min_access_size
= 1,
542 .valid
.max_access_size
= 4,
543 .impl
.min_access_size
= 1,
544 .impl
.max_access_size
= 4,
547 static const MemoryRegionOps iotkit_secctl_ns_ops
= {
548 .read_with_attrs
= iotkit_secctl_ns_read
,
549 .write_with_attrs
= iotkit_secctl_ns_write
,
550 .endianness
= DEVICE_LITTLE_ENDIAN
,
551 .valid
.min_access_size
= 1,
552 .valid
.max_access_size
= 4,
553 .impl
.min_access_size
= 1,
554 .impl
.max_access_size
= 4,
557 static void iotkit_secctl_reset_ppc(IoTKitSecCtlPPC
*ppc
)
564 static void iotkit_secctl_reset(DeviceState
*dev
)
566 IoTKitSecCtl
*s
= IOTKIT_SECCTL(dev
);
568 s
->secppcintstat
= 0;
574 foreach_ppc(s
, iotkit_secctl_reset_ppc
);
577 static void iotkit_secctl_mpc_status(void *opaque
, int n
, int level
)
579 IoTKitSecCtl
*s
= IOTKIT_SECCTL(opaque
);
581 s
->mpcintstatus
= deposit32(s
->mpcintstatus
, 0, 1, !!level
);
584 static void iotkit_secctl_mpcexp_status(void *opaque
, int n
, int level
)
586 IoTKitSecCtl
*s
= IOTKIT_SECCTL(opaque
);
588 s
->mpcintstatus
= deposit32(s
->mpcintstatus
, n
+ 16, 1, !!level
);
591 static void iotkit_secctl_ppc_irqstatus(void *opaque
, int n
, int level
)
593 IoTKitSecCtlPPC
*ppc
= opaque
;
594 IoTKitSecCtl
*s
= IOTKIT_SECCTL(ppc
->parent
);
595 int irqbit
= ppc
->irq_bit_offset
+ n
;
597 s
->secppcintstat
= deposit32(s
->secppcintstat
, irqbit
, 1, level
);
600 static void iotkit_secctl_init_ppc(IoTKitSecCtl
*s
,
601 IoTKitSecCtlPPC
*ppc
,
607 DeviceState
*dev
= DEVICE(s
);
609 ppc
->numports
= numports
;
610 ppc
->irq_bit_offset
= irq_bit_offset
;
613 gpioname
= g_strdup_printf("%s_nonsec", name
);
614 qdev_init_gpio_out_named(dev
, ppc
->nonsec
, gpioname
, numports
);
616 gpioname
= g_strdup_printf("%s_ap", name
);
617 qdev_init_gpio_out_named(dev
, ppc
->ap
, gpioname
, numports
);
619 gpioname
= g_strdup_printf("%s_irq_enable", name
);
620 qdev_init_gpio_out_named(dev
, &ppc
->irq_enable
, gpioname
, 1);
622 gpioname
= g_strdup_printf("%s_irq_clear", name
);
623 qdev_init_gpio_out_named(dev
, &ppc
->irq_clear
, gpioname
, 1);
625 gpioname
= g_strdup_printf("%s_irq_status", name
);
626 qdev_init_gpio_in_named_with_opaque(dev
, iotkit_secctl_ppc_irqstatus
,
631 static void iotkit_secctl_init(Object
*obj
)
633 IoTKitSecCtl
*s
= IOTKIT_SECCTL(obj
);
634 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
635 DeviceState
*dev
= DEVICE(obj
);
638 iotkit_secctl_init_ppc(s
, &s
->apb
[0], "apb_ppc0",
639 IOTS_APB_PPC0_NUM_PORTS
, 0);
640 iotkit_secctl_init_ppc(s
, &s
->apb
[1], "apb_ppc1",
641 IOTS_APB_PPC1_NUM_PORTS
, 1);
643 for (i
= 0; i
< IOTS_NUM_APB_EXP_PPC
; i
++) {
644 IoTKitSecCtlPPC
*ppc
= &s
->apbexp
[i
];
645 char *ppcname
= g_strdup_printf("apb_ppcexp%d", i
);
646 iotkit_secctl_init_ppc(s
, ppc
, ppcname
, IOTS_PPC_NUM_PORTS
, 4 + i
);
649 for (i
= 0; i
< IOTS_NUM_AHB_EXP_PPC
; i
++) {
650 IoTKitSecCtlPPC
*ppc
= &s
->ahbexp
[i
];
651 char *ppcname
= g_strdup_printf("ahb_ppcexp%d", i
);
652 iotkit_secctl_init_ppc(s
, ppc
, ppcname
, IOTS_PPC_NUM_PORTS
, 20 + i
);
656 qdev_init_gpio_out_named(dev
, &s
->sec_resp_cfg
, "sec_resp_cfg", 1);
657 qdev_init_gpio_out_named(dev
, &s
->nsc_cfg_irq
, "nsc_cfg", 1);
659 qdev_init_gpio_in_named(dev
, iotkit_secctl_mpc_status
, "mpc_status", 1);
660 qdev_init_gpio_in_named(dev
, iotkit_secctl_mpcexp_status
,
661 "mpcexp_status", IOTS_NUM_EXP_MPC
);
663 memory_region_init_io(&s
->s_regs
, obj
, &iotkit_secctl_s_ops
,
664 s
, "iotkit-secctl-s-regs", 0x1000);
665 memory_region_init_io(&s
->ns_regs
, obj
, &iotkit_secctl_ns_ops
,
666 s
, "iotkit-secctl-ns-regs", 0x1000);
667 sysbus_init_mmio(sbd
, &s
->s_regs
);
668 sysbus_init_mmio(sbd
, &s
->ns_regs
);
671 static const VMStateDescription iotkit_secctl_ppc_vmstate
= {
672 .name
= "iotkit-secctl-ppc",
674 .minimum_version_id
= 1,
675 .fields
= (VMStateField
[]) {
676 VMSTATE_UINT32(ns
, IoTKitSecCtlPPC
),
677 VMSTATE_UINT32(sp
, IoTKitSecCtlPPC
),
678 VMSTATE_UINT32(nsp
, IoTKitSecCtlPPC
),
679 VMSTATE_END_OF_LIST()
683 static const VMStateDescription iotkit_secctl_mpcintstatus_vmstate
= {
684 .name
= "iotkit-secctl-mpcintstatus",
686 .minimum_version_id
= 1,
687 .fields
= (VMStateField
[]) {
688 VMSTATE_UINT32(mpcintstatus
, IoTKitSecCtl
),
689 VMSTATE_END_OF_LIST()
693 static const VMStateDescription iotkit_secctl_vmstate
= {
694 .name
= "iotkit-secctl",
696 .minimum_version_id
= 1,
697 .fields
= (VMStateField
[]) {
698 VMSTATE_UINT32(secppcintstat
, IoTKitSecCtl
),
699 VMSTATE_UINT32(secppcinten
, IoTKitSecCtl
),
700 VMSTATE_UINT32(secrespcfg
, IoTKitSecCtl
),
701 VMSTATE_UINT32(nsccfg
, IoTKitSecCtl
),
702 VMSTATE_UINT32(brginten
, IoTKitSecCtl
),
703 VMSTATE_STRUCT_ARRAY(apb
, IoTKitSecCtl
, IOTS_NUM_APB_PPC
, 1,
704 iotkit_secctl_ppc_vmstate
, IoTKitSecCtlPPC
),
705 VMSTATE_STRUCT_ARRAY(apbexp
, IoTKitSecCtl
, IOTS_NUM_APB_EXP_PPC
, 1,
706 iotkit_secctl_ppc_vmstate
, IoTKitSecCtlPPC
),
707 VMSTATE_STRUCT_ARRAY(ahbexp
, IoTKitSecCtl
, IOTS_NUM_AHB_EXP_PPC
, 1,
708 iotkit_secctl_ppc_vmstate
, IoTKitSecCtlPPC
),
709 VMSTATE_END_OF_LIST()
711 .subsections
= (const VMStateDescription
*[]) {
712 &iotkit_secctl_mpcintstatus_vmstate
,
717 static void iotkit_secctl_class_init(ObjectClass
*klass
, void *data
)
719 DeviceClass
*dc
= DEVICE_CLASS(klass
);
721 dc
->vmsd
= &iotkit_secctl_vmstate
;
722 dc
->reset
= iotkit_secctl_reset
;
725 static const TypeInfo iotkit_secctl_info
= {
726 .name
= TYPE_IOTKIT_SECCTL
,
727 .parent
= TYPE_SYS_BUS_DEVICE
,
728 .instance_size
= sizeof(IoTKitSecCtl
),
729 .instance_init
= iotkit_secctl_init
,
730 .class_init
= iotkit_secctl_class_init
,
733 static void iotkit_secctl_register_types(void)
735 type_register_static(&iotkit_secctl_info
);
738 type_init(iotkit_secctl_register_types
);