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