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