2 * ARM IoTKit system control element
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.
13 * This is a model of the "system control element" which is part of the
14 * Arm IoTKit and documented in
15 * https://developer.arm.com/documentation/ecm0601256/latest
16 * Specifically, it implements the "system control register" blocks.
19 #include "qemu/osdep.h"
20 #include "qemu/bitops.h"
22 #include "qemu/module.h"
23 #include "sysemu/runstate.h"
25 #include "qapi/error.h"
26 #include "hw/sysbus.h"
27 #include "migration/vmstate.h"
28 #include "hw/registerfields.h"
29 #include "hw/misc/iotkit-sysctl.h"
30 #include "hw/qdev-properties.h"
31 #include "hw/arm/armsse-version.h"
32 #include "target/arm/arm-powerctl.h"
33 #include "target/arm/cpu.h"
35 REG32(SECDBGSTAT
, 0x0)
40 REG32(SYSCLK_DIV
, 0x14)
41 REG32(CLOCK_FORCE
, 0x18)
42 REG32(RESET_SYNDROME
, 0x100)
43 REG32(RESET_MASK
, 0x104)
45 FIELD(SWRESET
, SWRESETREQ
, 9, 1)
47 REG32(INITSVTOR0
, 0x110)
48 FIELD(INITSVTOR0
, LOCK
, 0, 1)
49 FIELD(INITSVTOR0
, VTOR
, 7, 25)
50 REG32(INITSVTOR1
, 0x114)
52 REG32(NMI_ENABLE
, 0x11c) /* BUSWAIT in IoTKit */
56 FIELD(PWRCTRL
, PPU_ACCESS_UNLOCK
, 0, 1)
57 FIELD(PWRCTRL
, PPU_ACCESS_FILTER
, 1, 1)
58 REG32(PDCM_PD_SYS_SENSE
, 0x200)
59 REG32(PDCM_PD_CPU0_SENSE
, 0x204)
60 REG32(PDCM_PD_SRAM0_SENSE
, 0x20c)
61 REG32(PDCM_PD_SRAM1_SENSE
, 0x210)
62 REG32(PDCM_PD_SRAM2_SENSE
, 0x214) /* PDCM_PD_VMR0_SENSE on SSE300 */
63 REG32(PDCM_PD_SRAM3_SENSE
, 0x218) /* PDCM_PD_VMR1_SENSE on SSE300 */
78 static const int iotkit_sysctl_id
[] = {
79 0x04, 0x00, 0x00, 0x00, /* PID4..PID7 */
80 0x54, 0xb8, 0x0b, 0x00, /* PID0..PID3 */
81 0x0d, 0xf0, 0x05, 0xb1, /* CID0..CID3 */
84 /* Also used by the SSE300 */
85 static const int sse200_sysctl_id
[] = {
86 0x04, 0x00, 0x00, 0x00, /* PID4..PID7 */
87 0x54, 0xb8, 0x1b, 0x00, /* PID0..PID3 */
88 0x0d, 0xf0, 0x05, 0xb1, /* CID0..CID3 */
92 * Set the initial secure vector table offset address for the core.
93 * This will take effect when the CPU next resets.
95 static void set_init_vtor(uint64_t cpuid
, uint32_t vtor
)
97 Object
*cpuobj
= OBJECT(arm_get_cpu_by_id(cpuid
));
100 if (object_property_find(cpuobj
, "init-svtor")) {
101 object_property_set_uint(cpuobj
, "init-svtor", vtor
, &error_abort
);
106 static uint64_t iotkit_sysctl_read(void *opaque
, hwaddr offset
,
109 IoTKitSysCtl
*s
= IOTKIT_SYSCTL(opaque
);
117 switch (s
->sse_version
) {
125 g_assert_not_reached();
129 switch (s
->sse_version
) {
137 g_assert_not_reached();
141 switch (s
->sse_version
) {
149 g_assert_not_reached();
153 switch (s
->sse_version
) {
161 g_assert_not_reached();
164 case A_RESET_SYNDROME
:
165 r
= s
->reset_syndrome
;
177 switch (s
->sse_version
) {
186 g_assert_not_reached();
190 switch (s
->sse_version
) {
196 /* In SSE300 this is reserved (for INITSVTOR2) */
199 g_assert_not_reached();
203 switch (s
->sse_version
) {
205 /* In IoTKit this is named BUSWAIT but marked reserved, R/O, zero */
212 /* In SSE300 this is reserved (for INITSVTOR3) */
215 g_assert_not_reached();
219 switch (s
->sse_version
) {
225 /* In SSE300 this offset is CPUWAIT */
229 g_assert_not_reached();
233 switch (s
->sse_version
) {
240 /* In SSE300 this offset is is NMI_ENABLE */
244 g_assert_not_reached();
248 switch (s
->sse_version
) {
256 g_assert_not_reached();
259 case A_PDCM_PD_SYS_SENSE
:
260 switch (s
->sse_version
) {
265 r
= s
->pdcm_pd_sys_sense
;
268 g_assert_not_reached();
271 case A_PDCM_PD_CPU0_SENSE
:
272 switch (s
->sse_version
) {
277 r
= s
->pdcm_pd_cpu0_sense
;
280 g_assert_not_reached();
283 case A_PDCM_PD_SRAM0_SENSE
:
284 switch (s
->sse_version
) {
288 r
= s
->pdcm_pd_sram0_sense
;
293 g_assert_not_reached();
296 case A_PDCM_PD_SRAM1_SENSE
:
297 switch (s
->sse_version
) {
301 r
= s
->pdcm_pd_sram1_sense
;
306 g_assert_not_reached();
309 case A_PDCM_PD_SRAM2_SENSE
:
310 switch (s
->sse_version
) {
314 r
= s
->pdcm_pd_sram2_sense
;
317 r
= s
->pdcm_pd_vmr0_sense
;
320 g_assert_not_reached();
323 case A_PDCM_PD_SRAM3_SENSE
:
324 switch (s
->sse_version
) {
328 r
= s
->pdcm_pd_sram3_sense
;
331 r
= s
->pdcm_pd_vmr1_sense
;
334 g_assert_not_reached();
337 case A_PID4
... A_CID3
:
338 switch (s
->sse_version
) {
340 r
= iotkit_sysctl_id
[(offset
- A_PID4
) / 4];
344 r
= sse200_sysctl_id
[(offset
- A_PID4
) / 4];
347 g_assert_not_reached();
353 qemu_log_mask(LOG_GUEST_ERROR
,
354 "IoTKit SysCtl read: read of WO offset %x\n",
360 qemu_log_mask(LOG_GUEST_ERROR
,
361 "IoTKit SysCtl read: bad offset %x\n", (int)offset
);
365 trace_iotkit_sysctl_read(offset
, r
, size
);
369 static void cpuwait_write(IoTKitSysCtl
*s
, uint32_t value
)
371 int num_cpus
= (s
->sse_version
== ARMSSE_SSE300
) ? 1 : 2;
374 for (i
= 0; i
< num_cpus
; i
++) {
375 uint32_t mask
= 1 << i
;
376 if ((s
->cpuwait
& mask
) && !(value
& mask
)) {
377 /* Powering up CPU 0 */
378 arm_set_cpu_on_and_reset(i
);
384 static void iotkit_sysctl_write(void *opaque
, hwaddr offset
,
385 uint64_t value
, unsigned size
)
387 IoTKitSysCtl
*s
= IOTKIT_SYSCTL(opaque
);
389 trace_iotkit_sysctl_write(offset
, value
, size
);
392 * Most of the state here has to do with control of reset and
393 * similar kinds of power up -- for instance the guest can ask
394 * what the reason for the last reset was, or forbid reset for
395 * some causes (like the non-secure watchdog). Most of this is
396 * not relevant to QEMU, which doesn't really model anything other
397 * than a full power-on reset.
398 * We just model the registers as reads-as-written.
402 case A_RESET_SYNDROME
:
403 qemu_log_mask(LOG_UNIMP
,
404 "IoTKit SysCtl RESET_SYNDROME unimplemented\n");
405 s
->reset_syndrome
= value
;
408 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl RESET_MASK unimplemented\n");
409 s
->reset_mask
= value
;
413 * General retention register, which is only reset by a power-on
414 * reset. Technically this implementation is complete, since
415 * QEMU only supports power-on resets...
420 switch (s
->sse_version
) {
422 /* SSE300 has a LOCK bit which prevents further writes when set */
423 if (s
->initsvtor0
& R_INITSVTOR0_LOCK_MASK
) {
424 qemu_log_mask(LOG_GUEST_ERROR
,
425 "IoTKit INITSVTOR0 write when register locked\n");
428 s
->initsvtor0
= value
;
429 set_init_vtor(0, s
->initsvtor0
& R_INITSVTOR0_VTOR_MASK
);
433 s
->initsvtor0
= value
;
434 set_init_vtor(0, s
->initsvtor0
);
437 g_assert_not_reached();
441 switch (s
->sse_version
) {
444 cpuwait_write(s
, value
);
447 /* In SSE300 this is reserved (for INITSVTOR2) */
450 g_assert_not_reached();
454 switch (s
->sse_version
) {
457 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl WICCTRL unimplemented\n");
461 /* In SSE300 this offset is CPUWAIT */
462 cpuwait_write(s
, value
);
465 g_assert_not_reached();
470 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl SECDBGSET unimplemented\n");
471 s
->secure_debug
|= value
;
474 /* write-1-to-clear */
475 s
->secure_debug
&= ~value
;
478 /* One w/o bit to request a reset; all other bits reserved */
479 if (value
& R_SWRESET_SWRESETREQ_MASK
) {
480 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET
);
484 switch (s
->sse_version
) {
489 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl SCSECCTRL unimplemented\n");
490 s
->scsecctrl
= value
;
493 g_assert_not_reached();
497 switch (s
->sse_version
) {
502 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl FCLK_DIV unimplemented\n");
506 g_assert_not_reached();
510 switch (s
->sse_version
) {
515 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl SYSCLK_DIV unimplemented\n");
516 s
->sysclk_div
= value
;
519 g_assert_not_reached();
523 switch (s
->sse_version
) {
528 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl CLOCK_FORCE unimplemented\n");
529 s
->clock_force
= value
;
532 g_assert_not_reached();
536 switch (s
->sse_version
) {
540 s
->initsvtor1
= value
;
541 set_init_vtor(1, s
->initsvtor1
);
546 g_assert_not_reached();
550 switch (s
->sse_version
) {
554 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl EWCTRL unimplemented\n");
558 /* In SSE300 this offset is is NMI_ENABLE */
559 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl NMI_ENABLE unimplemented\n");
560 s
->nmi_enable
= value
;
563 g_assert_not_reached();
567 switch (s
->sse_version
) {
572 if (!(s
->pwrctrl
& R_PWRCTRL_PPU_ACCESS_UNLOCK_MASK
)) {
573 qemu_log_mask(LOG_GUEST_ERROR
,
574 "IoTKit PWRCTRL write when register locked\n");
580 g_assert_not_reached();
583 case A_PDCM_PD_SYS_SENSE
:
584 switch (s
->sse_version
) {
589 qemu_log_mask(LOG_UNIMP
,
590 "IoTKit SysCtl PDCM_PD_SYS_SENSE unimplemented\n");
591 s
->pdcm_pd_sys_sense
= value
;
594 g_assert_not_reached();
597 case A_PDCM_PD_CPU0_SENSE
:
598 switch (s
->sse_version
) {
603 qemu_log_mask(LOG_UNIMP
,
604 "IoTKit SysCtl PDCM_PD_CPU0_SENSE unimplemented\n");
605 s
->pdcm_pd_cpu0_sense
= value
;
608 g_assert_not_reached();
611 case A_PDCM_PD_SRAM0_SENSE
:
612 switch (s
->sse_version
) {
616 qemu_log_mask(LOG_UNIMP
,
617 "IoTKit SysCtl PDCM_PD_SRAM0_SENSE unimplemented\n");
618 s
->pdcm_pd_sram0_sense
= value
;
623 g_assert_not_reached();
626 case A_PDCM_PD_SRAM1_SENSE
:
627 switch (s
->sse_version
) {
631 qemu_log_mask(LOG_UNIMP
,
632 "IoTKit SysCtl PDCM_PD_SRAM1_SENSE unimplemented\n");
633 s
->pdcm_pd_sram1_sense
= value
;
638 g_assert_not_reached();
641 case A_PDCM_PD_SRAM2_SENSE
:
642 switch (s
->sse_version
) {
646 qemu_log_mask(LOG_UNIMP
,
647 "IoTKit SysCtl PDCM_PD_SRAM2_SENSE unimplemented\n");
648 s
->pdcm_pd_sram2_sense
= value
;
651 qemu_log_mask(LOG_UNIMP
,
652 "IoTKit SysCtl PDCM_PD_VMR0_SENSE unimplemented\n");
653 s
->pdcm_pd_vmr0_sense
= value
;
656 g_assert_not_reached();
659 case A_PDCM_PD_SRAM3_SENSE
:
660 switch (s
->sse_version
) {
664 qemu_log_mask(LOG_UNIMP
,
665 "IoTKit SysCtl PDCM_PD_SRAM3_SENSE unimplemented\n");
666 s
->pdcm_pd_sram3_sense
= value
;
669 qemu_log_mask(LOG_UNIMP
,
670 "IoTKit SysCtl PDCM_PD_VMR1_SENSE unimplemented\n");
671 s
->pdcm_pd_vmr1_sense
= value
;
674 g_assert_not_reached();
678 /* In IoTKit this is BUSWAIT: reserved, R/O, zero */
679 switch (s
->sse_version
) {
683 qemu_log_mask(LOG_UNIMP
, "IoTKit SysCtl NMI_ENABLE unimplemented\n");
684 s
->nmi_enable
= value
;
687 /* In SSE300 this is reserved (for INITSVTOR3) */
690 g_assert_not_reached();
694 case A_PID4
... A_CID3
:
696 qemu_log_mask(LOG_GUEST_ERROR
,
697 "IoTKit SysCtl write: write of RO offset %x\n",
702 qemu_log_mask(LOG_GUEST_ERROR
,
703 "IoTKit SysCtl write: bad offset %x\n", (int)offset
);
708 static const MemoryRegionOps iotkit_sysctl_ops
= {
709 .read
= iotkit_sysctl_read
,
710 .write
= iotkit_sysctl_write
,
711 .endianness
= DEVICE_LITTLE_ENDIAN
,
712 /* byte/halfword accesses are just zero-padded on reads and writes */
713 .impl
.min_access_size
= 4,
714 .impl
.max_access_size
= 4,
715 .valid
.min_access_size
= 1,
716 .valid
.max_access_size
= 4,
719 static void iotkit_sysctl_reset(DeviceState
*dev
)
721 IoTKitSysCtl
*s
= IOTKIT_SYSCTL(dev
);
723 trace_iotkit_sysctl_reset();
725 s
->reset_syndrome
= 1;
728 s
->initsvtor0
= s
->initsvtor0_rst
;
729 s
->initsvtor1
= s
->initsvtor1_rst
;
730 s
->cpuwait
= s
->cpuwait_rst
;
739 s
->pdcm_pd_sys_sense
= 0x7f;
740 s
->pdcm_pd_sram0_sense
= 0;
741 s
->pdcm_pd_sram1_sense
= 0;
742 s
->pdcm_pd_sram2_sense
= 0;
743 s
->pdcm_pd_sram3_sense
= 0;
744 s
->pdcm_pd_cpu0_sense
= 0;
745 s
->pdcm_pd_vmr0_sense
= 0;
746 s
->pdcm_pd_vmr1_sense
= 0;
749 static void iotkit_sysctl_init(Object
*obj
)
751 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
752 IoTKitSysCtl
*s
= IOTKIT_SYSCTL(obj
);
754 memory_region_init_io(&s
->iomem
, obj
, &iotkit_sysctl_ops
,
755 s
, "iotkit-sysctl", 0x1000);
756 sysbus_init_mmio(sbd
, &s
->iomem
);
759 static void iotkit_sysctl_realize(DeviceState
*dev
, Error
**errp
)
761 IoTKitSysCtl
*s
= IOTKIT_SYSCTL(dev
);
763 if (!armsse_version_valid(s
->sse_version
)) {
764 error_setg(errp
, "invalid sse-version value %d", s
->sse_version
);
769 static bool sse300_needed(void *opaque
)
771 IoTKitSysCtl
*s
= IOTKIT_SYSCTL(opaque
);
773 return s
->sse_version
== ARMSSE_SSE300
;
776 static const VMStateDescription iotkit_sysctl_sse300_vmstate
= {
777 .name
= "iotkit-sysctl/sse-300",
779 .minimum_version_id
= 1,
780 .needed
= sse300_needed
,
781 .fields
= (VMStateField
[]) {
782 VMSTATE_UINT32(pwrctrl
, IoTKitSysCtl
),
783 VMSTATE_UINT32(pdcm_pd_cpu0_sense
, IoTKitSysCtl
),
784 VMSTATE_UINT32(pdcm_pd_vmr0_sense
, IoTKitSysCtl
),
785 VMSTATE_UINT32(pdcm_pd_vmr1_sense
, IoTKitSysCtl
),
786 VMSTATE_END_OF_LIST()
790 static bool sse200_needed(void *opaque
)
792 IoTKitSysCtl
*s
= IOTKIT_SYSCTL(opaque
);
794 return s
->sse_version
!= ARMSSE_IOTKIT
;
797 static const VMStateDescription iotkit_sysctl_sse200_vmstate
= {
798 .name
= "iotkit-sysctl/sse-200",
800 .minimum_version_id
= 1,
801 .needed
= sse200_needed
,
802 .fields
= (VMStateField
[]) {
803 VMSTATE_UINT32(scsecctrl
, IoTKitSysCtl
),
804 VMSTATE_UINT32(fclk_div
, IoTKitSysCtl
),
805 VMSTATE_UINT32(sysclk_div
, IoTKitSysCtl
),
806 VMSTATE_UINT32(clock_force
, IoTKitSysCtl
),
807 VMSTATE_UINT32(initsvtor1
, IoTKitSysCtl
),
808 VMSTATE_UINT32(nmi_enable
, IoTKitSysCtl
),
809 VMSTATE_UINT32(pdcm_pd_sys_sense
, IoTKitSysCtl
),
810 VMSTATE_UINT32(pdcm_pd_sram0_sense
, IoTKitSysCtl
),
811 VMSTATE_UINT32(pdcm_pd_sram1_sense
, IoTKitSysCtl
),
812 VMSTATE_UINT32(pdcm_pd_sram2_sense
, IoTKitSysCtl
),
813 VMSTATE_UINT32(pdcm_pd_sram3_sense
, IoTKitSysCtl
),
814 VMSTATE_END_OF_LIST()
818 static const VMStateDescription iotkit_sysctl_vmstate
= {
819 .name
= "iotkit-sysctl",
821 .minimum_version_id
= 1,
822 .fields
= (VMStateField
[]) {
823 VMSTATE_UINT32(secure_debug
, IoTKitSysCtl
),
824 VMSTATE_UINT32(reset_syndrome
, IoTKitSysCtl
),
825 VMSTATE_UINT32(reset_mask
, IoTKitSysCtl
),
826 VMSTATE_UINT32(gretreg
, IoTKitSysCtl
),
827 VMSTATE_UINT32(initsvtor0
, IoTKitSysCtl
),
828 VMSTATE_UINT32(cpuwait
, IoTKitSysCtl
),
829 VMSTATE_UINT32(wicctrl
, IoTKitSysCtl
),
830 VMSTATE_END_OF_LIST()
832 .subsections
= (const VMStateDescription
*[]) {
833 &iotkit_sysctl_sse200_vmstate
,
834 &iotkit_sysctl_sse300_vmstate
,
839 static Property iotkit_sysctl_props
[] = {
840 DEFINE_PROP_UINT32("sse-version", IoTKitSysCtl
, sse_version
, 0),
841 DEFINE_PROP_UINT32("CPUWAIT_RST", IoTKitSysCtl
, cpuwait_rst
, 0),
842 DEFINE_PROP_UINT32("INITSVTOR0_RST", IoTKitSysCtl
, initsvtor0_rst
,
844 DEFINE_PROP_UINT32("INITSVTOR1_RST", IoTKitSysCtl
, initsvtor1_rst
,
846 DEFINE_PROP_END_OF_LIST()
849 static void iotkit_sysctl_class_init(ObjectClass
*klass
, void *data
)
851 DeviceClass
*dc
= DEVICE_CLASS(klass
);
853 dc
->vmsd
= &iotkit_sysctl_vmstate
;
854 dc
->reset
= iotkit_sysctl_reset
;
855 device_class_set_props(dc
, iotkit_sysctl_props
);
856 dc
->realize
= iotkit_sysctl_realize
;
859 static const TypeInfo iotkit_sysctl_info
= {
860 .name
= TYPE_IOTKIT_SYSCTL
,
861 .parent
= TYPE_SYS_BUS_DEVICE
,
862 .instance_size
= sizeof(IoTKitSysCtl
),
863 .instance_init
= iotkit_sysctl_init
,
864 .class_init
= iotkit_sysctl_class_init
,
867 static void iotkit_sysctl_register_types(void)
869 type_register_static(&iotkit_sysctl_info
);
872 type_init(iotkit_sysctl_register_types
);