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