xref: /openbmc/qemu/hw/misc/pvpanic.c (revision f7160f32)
1 /*
2  * QEMU simulated pvpanic device.
3  *
4  * Copyright Fujitsu, Corp. 2013
5  *
6  * Authors:
7  *     Wen Congyang <wency@cn.fujitsu.com>
8  *     Hu Tao <hutao@cn.fujitsu.com>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2 or later.
11  * See the COPYING file in the top-level directory.
12  *
13  */
14 
15 #include "qemu/osdep.h"
16 #include "qemu/log.h"
17 #include "qemu/module.h"
18 #include "sysemu/runstate.h"
19 
20 #include "hw/nvram/fw_cfg.h"
21 #include "hw/qdev-properties.h"
22 #include "hw/misc/pvpanic.h"
23 
24 /* The bit of supported pv event, TODO: include uapi header and remove this */
25 #define PVPANIC_F_PANICKED      0
26 #define PVPANIC_F_CRASHLOADED   1
27 
28 /* The pv event value */
29 #define PVPANIC_PANICKED        (1 << PVPANIC_F_PANICKED)
30 #define PVPANIC_CRASHLOADED     (1 << PVPANIC_F_CRASHLOADED)
31 
32 #define ISA_PVPANIC_DEVICE(obj)    \
33     OBJECT_CHECK(PVPanicState, (obj), TYPE_PVPANIC)
34 
35 static void handle_event(int event)
36 {
37     static bool logged;
38 
39     if (event & ~(PVPANIC_PANICKED | PVPANIC_CRASHLOADED) && !logged) {
40         qemu_log_mask(LOG_GUEST_ERROR, "pvpanic: unknown event %#x.\n", event);
41         logged = true;
42     }
43 
44     if (event & PVPANIC_PANICKED) {
45         qemu_system_guest_panicked(NULL);
46         return;
47     }
48 
49     if (event & PVPANIC_CRASHLOADED) {
50         qemu_system_guest_crashloaded(NULL);
51         return;
52     }
53 }
54 
55 #include "hw/isa/isa.h"
56 
57 typedef struct PVPanicState {
58     ISADevice parent_obj;
59 
60     MemoryRegion io;
61     uint16_t ioport;
62 } PVPanicState;
63 
64 /* return supported events on read */
65 static uint64_t pvpanic_ioport_read(void *opaque, hwaddr addr, unsigned size)
66 {
67     return PVPANIC_PANICKED;
68 }
69 
70 static void pvpanic_ioport_write(void *opaque, hwaddr addr, uint64_t val,
71                                  unsigned size)
72 {
73     handle_event(val);
74 }
75 
76 static const MemoryRegionOps pvpanic_ops = {
77     .read = pvpanic_ioport_read,
78     .write = pvpanic_ioport_write,
79     .impl = {
80         .min_access_size = 1,
81         .max_access_size = 1,
82     },
83 };
84 
85 static void pvpanic_isa_initfn(Object *obj)
86 {
87     PVPanicState *s = ISA_PVPANIC_DEVICE(obj);
88 
89     memory_region_init_io(&s->io, OBJECT(s), &pvpanic_ops, s, "pvpanic", 1);
90 }
91 
92 static void pvpanic_isa_realizefn(DeviceState *dev, Error **errp)
93 {
94     ISADevice *d = ISA_DEVICE(dev);
95     PVPanicState *s = ISA_PVPANIC_DEVICE(dev);
96     FWCfgState *fw_cfg = fw_cfg_find();
97     uint16_t *pvpanic_port;
98 
99     if (!fw_cfg) {
100         return;
101     }
102 
103     pvpanic_port = g_malloc(sizeof(*pvpanic_port));
104     *pvpanic_port = cpu_to_le16(s->ioport);
105     fw_cfg_add_file(fw_cfg, "etc/pvpanic-port", pvpanic_port,
106                     sizeof(*pvpanic_port));
107 
108     isa_register_ioport(d, &s->io, s->ioport);
109 }
110 
111 static Property pvpanic_isa_properties[] = {
112     DEFINE_PROP_UINT16(PVPANIC_IOPORT_PROP, PVPanicState, ioport, 0x505),
113     DEFINE_PROP_END_OF_LIST(),
114 };
115 
116 static void pvpanic_isa_class_init(ObjectClass *klass, void *data)
117 {
118     DeviceClass *dc = DEVICE_CLASS(klass);
119 
120     dc->realize = pvpanic_isa_realizefn;
121     device_class_set_props(dc, pvpanic_isa_properties);
122     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
123 }
124 
125 static TypeInfo pvpanic_isa_info = {
126     .name          = TYPE_PVPANIC,
127     .parent        = TYPE_ISA_DEVICE,
128     .instance_size = sizeof(PVPanicState),
129     .instance_init = pvpanic_isa_initfn,
130     .class_init    = pvpanic_isa_class_init,
131 };
132 
133 static void pvpanic_register_types(void)
134 {
135     type_register_static(&pvpanic_isa_info);
136 }
137 
138 type_init(pvpanic_register_types)
139