xref: /openbmc/qemu/hw/watchdog/wdt_diag288.c (revision 4921d0a7)
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/reset.h"
16 #include "sysemu/watchdog.h"
17 #include "qemu/timer.h"
18 #include "hw/watchdog/wdt_diag288.h"
19 #include "migration/vmstate.h"
20 #include "qemu/log.h"
21 
22 static const VMStateDescription vmstate_diag288 = {
23     .name = "vmstate_diag288",
24     .version_id = 0,
25     .minimum_version_id = 0,
26     .fields = (const VMStateField[]) {
27         VMSTATE_TIMER_PTR(timer, DIAG288State),
28         VMSTATE_BOOL(enabled, DIAG288State),
29         VMSTATE_END_OF_LIST()
30     }
31 };
32 
33 static void wdt_diag288_reset(DeviceState *dev)
34 {
35     DIAG288State *diag288 = DIAG288(dev);
36 
37     diag288->enabled = false;
38     timer_del(diag288->timer);
39 }
40 
41 static void diag288_reset(void *opaque)
42 {
43     DeviceState *diag288 = opaque;
44 
45     wdt_diag288_reset(diag288);
46 }
47 
48 static void diag288_timer_expired(void *dev)
49 {
50     qemu_log_mask(CPU_LOG_RESET, "Watchdog timer expired.\n");
51     /* Reset the watchdog only if the guest gets notified about
52      * expiry. watchdog_perform_action() may temporarily relinquish
53      * the BQL; reset before triggering the action to avoid races with
54      * diag288 instructions. */
55     switch (get_watchdog_action()) {
56     case WATCHDOG_ACTION_DEBUG:
57     case WATCHDOG_ACTION_NONE:
58     case WATCHDOG_ACTION_PAUSE:
59         break;
60     default:
61         wdt_diag288_reset(dev);
62     }
63     watchdog_perform_action();
64 }
65 
66 static int wdt_diag288_handle_timer(DIAG288State *diag288,
67                                      uint64_t func, uint64_t timeout)
68 {
69     switch (func) {
70     case WDT_DIAG288_INIT:
71         diag288->enabled = true;
72         /* fall through */
73     case WDT_DIAG288_CHANGE:
74         if (!diag288->enabled) {
75             return -1;
76         }
77         timer_mod(diag288->timer,
78                   qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
79                   timeout * NANOSECONDS_PER_SECOND);
80         break;
81     case WDT_DIAG288_CANCEL:
82         if (!diag288->enabled) {
83             return -1;
84         }
85         diag288->enabled = false;
86         timer_del(diag288->timer);
87         break;
88     default:
89         return -1;
90     }
91 
92     return 0;
93 }
94 
95 static void wdt_diag288_realize(DeviceState *dev, Error **errp)
96 {
97     DIAG288State *diag288 = DIAG288(dev);
98 
99     qemu_register_reset(diag288_reset, diag288);
100     diag288->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, diag288_timer_expired,
101                                   dev);
102 }
103 
104 static void wdt_diag288_unrealize(DeviceState *dev)
105 {
106     DIAG288State *diag288 = DIAG288(dev);
107 
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     dc->hotpluggable = false;
120     set_bit(DEVICE_CATEGORY_WATCHDOG, dc->categories);
121     dc->vmsd = &vmstate_diag288;
122     diag288->handle_timer = wdt_diag288_handle_timer;
123     dc->desc = "diag288 device for s390x platform";
124 }
125 
126 static const TypeInfo wdt_diag288_info = {
127     .class_init = wdt_diag288_class_init,
128     .parent = TYPE_DEVICE,
129     .name  = TYPE_WDT_DIAG288,
130     .instance_size  = sizeof(DIAG288State),
131     .class_size = sizeof(DIAG288Class),
132 };
133 
134 static void wdt_diag288_register_types(void)
135 {
136     type_register_static(&wdt_diag288_info);
137 }
138 
139 type_init(wdt_diag288_register_types)
140