Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / hw / watchdog / wdt_diag288.c
blobf54a35a0e356ed7441c31dcf84a25c76e7f2f03a
1 /*
2 * watchdog device diag288 support
4 * Copyright IBM, Corp. 2015
6 * Authors:
7 * Xu Wang <gesaint@linux.vnet.ibm.com>
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.
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"
20 static WatchdogTimerModel model = {
21 .wdt_name = TYPE_WDT_DIAG288,
22 .wdt_description = "diag288 device for s390x platform",
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()
36 static void wdt_diag288_reset(DeviceState *dev)
38 DIAG288State *diag288 = DIAG288(dev);
40 diag288->enabled = false;
41 timer_del(diag288->timer);
44 static void diag288_reset(void *opaque)
46 DeviceState *diag288 = opaque;
48 wdt_diag288_reset(diag288);
51 static void diag288_timer_expired(void *dev)
53 qemu_log_mask(CPU_LOG_RESET, "Watchdog timer expired.\n");
54 /* Reset the watchdog only if the guest gets notified about
55 * expiry. watchdog_perform_action() may temporarily relinquish
56 * the BQL; reset before triggering the action to avoid races with
57 * diag288 instructions. */
58 switch (get_watchdog_action()) {
59 case WDT_DEBUG:
60 case WDT_NONE:
61 case WDT_PAUSE:
62 break;
63 default:
64 wdt_diag288_reset(dev);
66 watchdog_perform_action();
69 static int wdt_diag288_handle_timer(DIAG288State *diag288,
70 uint64_t func, uint64_t timeout)
72 switch (func) {
73 case WDT_DIAG288_INIT:
74 diag288->enabled = true;
75 /* fall through */
76 case WDT_DIAG288_CHANGE:
77 if (!diag288->enabled) {
78 return -1;
80 timer_mod(diag288->timer,
81 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
82 timeout * NANOSECONDS_PER_SECOND);
83 break;
84 case WDT_DIAG288_CANCEL:
85 if (!diag288->enabled) {
86 return -1;
88 diag288->enabled = false;
89 timer_del(diag288->timer);
90 break;
91 default:
92 return -1;
95 return 0;
98 static void wdt_diag288_realize(DeviceState *dev, Error **errp)
100 DIAG288State *diag288 = DIAG288(dev);
102 qemu_register_reset(diag288_reset, diag288);
103 diag288->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, diag288_timer_expired,
104 dev);
107 static void wdt_diag288_unrealize(DeviceState *dev, Error **errp)
109 DIAG288State *diag288 = DIAG288(dev);
111 timer_del(diag288->timer);
112 timer_free(diag288->timer);
115 static void wdt_diag288_class_init(ObjectClass *klass, void *data)
117 DeviceClass *dc = DEVICE_CLASS(klass);
118 DIAG288Class *diag288 = DIAG288_CLASS(klass);
120 dc->realize = wdt_diag288_realize;
121 dc->unrealize = wdt_diag288_unrealize;
122 dc->reset = wdt_diag288_reset;
123 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
124 dc->vmsd = &vmstate_diag288;
125 diag288->handle_timer = wdt_diag288_handle_timer;
128 static const TypeInfo wdt_diag288_info = {
129 .class_init = wdt_diag288_class_init,
130 .parent = TYPE_DEVICE,
131 .name = TYPE_WDT_DIAG288,
132 .instance_size = sizeof(DIAG288State),
133 .class_size = sizeof(DIAG288Class),
136 static void wdt_diag288_register_types(void)
138 watchdog_add_model(&model);
139 type_register_static(&wdt_diag288_info);
142 type_init(wdt_diag288_register_types)