Merge remote-tracking branch 'remotes/dgilbert-gitlab/tags/pull-migration-20210726a...
[qemu/armbru.git] / hw / misc / virt_ctrl.c
blob3552d7a09abdd603ac5d25b11398fe5642b71eb3
1 /*
2 * SPDX-License-Identifer: GPL-2.0-or-later
4 * Virt system Controller
5 */
7 #include "qemu/osdep.h"
8 #include "hw/qdev-properties.h"
9 #include "hw/sysbus.h"
10 #include "migration/vmstate.h"
11 #include "qemu/log.h"
12 #include "trace.h"
13 #include "sysemu/runstate.h"
14 #include "hw/misc/virt_ctrl.h"
16 enum {
17 REG_FEATURES = 0x00,
18 REG_CMD = 0x04,
21 #define FEAT_POWER_CTRL 0x00000001
23 enum {
24 CMD_NOOP,
25 CMD_RESET,
26 CMD_HALT,
27 CMD_PANIC,
30 static uint64_t virt_ctrl_read(void *opaque, hwaddr addr, unsigned size)
32 VirtCtrlState *s = opaque;
33 uint64_t value = 0;
35 switch (addr) {
36 case REG_FEATURES:
37 value = FEAT_POWER_CTRL;
38 break;
39 default:
40 qemu_log_mask(LOG_UNIMP,
41 "%s: unimplemented register read 0x%02"HWADDR_PRIx"\n",
42 __func__, addr);
43 break;
46 trace_virt_ctrl_write(s, addr, size, value);
48 return value;
51 static void virt_ctrl_write(void *opaque, hwaddr addr, uint64_t value,
52 unsigned size)
54 VirtCtrlState *s = opaque;
56 trace_virt_ctrl_write(s, addr, size, value);
58 switch (addr) {
59 case REG_CMD:
60 switch (value) {
61 case CMD_NOOP:
62 break;
63 case CMD_RESET:
64 qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
65 break;
66 case CMD_HALT:
67 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
68 break;
69 case CMD_PANIC:
70 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_PANIC);
71 break;
73 break;
74 default:
75 qemu_log_mask(LOG_UNIMP,
76 "%s: unimplemented register write 0x%02"HWADDR_PRIx"\n",
77 __func__, addr);
78 break;
82 static const MemoryRegionOps virt_ctrl_ops = {
83 .read = virt_ctrl_read,
84 .write = virt_ctrl_write,
85 .endianness = DEVICE_NATIVE_ENDIAN,
86 .valid.max_access_size = 4,
87 .impl.max_access_size = 4,
90 static void virt_ctrl_reset(DeviceState *dev)
92 VirtCtrlState *s = VIRT_CTRL(dev);
94 trace_virt_ctrl_reset(s);
97 static void virt_ctrl_realize(DeviceState *dev, Error **errp)
99 VirtCtrlState *s = VIRT_CTRL(dev);
101 trace_virt_ctrl_instance_init(s);
103 memory_region_init_io(&s->iomem, OBJECT(s), &virt_ctrl_ops, s,
104 "virt-ctrl", 0x100);
107 static const VMStateDescription vmstate_virt_ctrl = {
108 .name = "virt-ctrl",
109 .version_id = 1,
110 .minimum_version_id = 1,
111 .fields = (VMStateField[]) {
112 VMSTATE_UINT32(irq_enabled, VirtCtrlState),
113 VMSTATE_END_OF_LIST()
117 static void virt_ctrl_instance_init(Object *obj)
119 SysBusDevice *dev = SYS_BUS_DEVICE(obj);
120 VirtCtrlState *s = VIRT_CTRL(obj);
122 trace_virt_ctrl_instance_init(s);
124 sysbus_init_mmio(dev, &s->iomem);
125 sysbus_init_irq(dev, &s->irq);
128 static void virt_ctrl_class_init(ObjectClass *oc, void *data)
130 DeviceClass *dc = DEVICE_CLASS(oc);
132 dc->reset = virt_ctrl_reset;
133 dc->realize = virt_ctrl_realize;
134 dc->vmsd = &vmstate_virt_ctrl;
137 static const TypeInfo virt_ctrl_info = {
138 .name = TYPE_VIRT_CTRL,
139 .parent = TYPE_SYS_BUS_DEVICE,
140 .class_init = virt_ctrl_class_init,
141 .instance_init = virt_ctrl_instance_init,
142 .instance_size = sizeof(VirtCtrlState),
145 static void virt_ctrl_register_types(void)
147 type_register_static(&virt_ctrl_info);
150 type_init(virt_ctrl_register_types)