xref: /openbmc/qemu/hw/watchdog/wdt_diag288.c (revision 42f7a448)
1 /*
2  * watchdog device diag288 support
3  *
4  * Copyright IBM, Corp. 2015
5  *
6  * Authors:
7  *  Xu Wang <gesaint@linux.vnet.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or (at your
10  * option) any later version.  See the COPYING file in the top-level directory.
11  *
12  */
13 
14 #include "sysemu/watchdog.h"
15 #include "hw/sysbus.h"
16 #include "qemu/timer.h"
17 #include "hw/watchdog/wdt_diag288.h"
18 
19 static WatchdogTimerModel model = {
20     .wdt_name = TYPE_WDT_DIAG288,
21     .wdt_description = "diag288 device for s390x platform",
22 };
23 
24 static const VMStateDescription vmstate_diag288 = {
25     .name = "vmstate_diag288",
26     .version_id = 0,
27     .minimum_version_id = 0,
28     .fields = (VMStateField[]) {
29         VMSTATE_TIMER_PTR(timer, DIAG288State),
30         VMSTATE_BOOL(enabled, DIAG288State),
31         VMSTATE_END_OF_LIST()
32     }
33 };
34 
35 static void wdt_diag288_reset(DeviceState *dev)
36 {
37     DIAG288State *diag288 = DIAG288(dev);
38 
39     diag288->enabled = false;
40     timer_del(diag288->timer);
41 }
42 
43 static void diag288_reset(void *opaque)
44 {
45     DeviceState *diag288 = opaque;
46 
47     wdt_diag288_reset(diag288);
48 }
49 
50 static void diag288_timer_expired(void *dev)
51 {
52     qemu_log_mask(CPU_LOG_RESET, "Watchdog timer expired.\n");
53     watchdog_perform_action();
54     /* Reset the watchdog only if the guest was notified about expiry. */
55     switch (get_watchdog_action()) {
56     case WDT_DEBUG:
57     case WDT_NONE:
58     case WDT_PAUSE:
59          return;
60     }
61     wdt_diag288_reset(dev);
62 }
63 
64 static int wdt_diag288_handle_timer(DIAG288State *diag288,
65                                      uint64_t func, uint64_t timeout)
66 {
67     switch (func) {
68     case WDT_DIAG288_INIT:
69         diag288->enabled = true;
70         /* fall through */
71     case WDT_DIAG288_CHANGE:
72         if (!diag288->enabled) {
73             return -1;
74         }
75         timer_mod(diag288->timer,
76                   qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
77                   timeout * get_ticks_per_sec());
78         break;
79     case WDT_DIAG288_CANCEL:
80         if (!diag288->enabled) {
81             return -1;
82         }
83         diag288->enabled = false;
84         timer_del(diag288->timer);
85         break;
86     default:
87         return -1;
88     }
89 
90     return 0;
91 }
92 
93 static void wdt_diag288_realize(DeviceState *dev, Error **errp)
94 {
95     DIAG288State *diag288 = DIAG288(dev);
96 
97     qemu_register_reset(diag288_reset, diag288);
98     diag288->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, diag288_timer_expired,
99                                   dev);
100 }
101 
102 static void wdt_diag288_unrealize(DeviceState *dev, Error **errp)
103 {
104     DIAG288State *diag288 = DIAG288(dev);
105 
106     timer_del(diag288->timer);
107     timer_free(diag288->timer);
108 }
109 
110 static void wdt_diag288_class_init(ObjectClass *klass, void *data)
111 {
112     DeviceClass *dc = DEVICE_CLASS(klass);
113     DIAG288Class *diag288 = DIAG288_CLASS(klass);
114 
115     dc->realize = wdt_diag288_realize;
116     dc->unrealize = wdt_diag288_unrealize;
117     dc->reset = wdt_diag288_reset;
118     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
119     dc->vmsd = &vmstate_diag288;
120     diag288->handle_timer = wdt_diag288_handle_timer;
121 }
122 
123 static const TypeInfo wdt_diag288_info = {
124     .class_init = wdt_diag288_class_init,
125     .parent = TYPE_DEVICE,
126     .name  = TYPE_WDT_DIAG288,
127     .instance_size  = sizeof(DIAG288State),
128     .class_size = sizeof(DIAG288Class),
129 };
130 
131 static void wdt_diag288_register_types(void)
132 {
133     watchdog_add_model(&model);
134     type_register_static(&wdt_diag288_info);
135 }
136 
137 type_init(wdt_diag288_register_types)
138