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