xref: /openbmc/qemu/hw/watchdog/wdt_diag288.c (revision becaeb72)
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_reset(void *opaque)
44 {
45     DeviceState *diag288 = opaque;
46 
47     wdt_diag288_reset(diag288);
48 }
49 
50 static void diag288_timer_expired(void *dev)
51 {
52     qemu_log_mask(CPU_LOG_RESET, "Watchdog timer expired.\n");
53     watchdog_perform_action();
54     wdt_diag288_reset(dev);
55 }
56 
57 static int wdt_diag288_handle_timer(DIAG288State *diag288,
58                                      uint64_t func, uint64_t timeout)
59 {
60     switch (func) {
61     case WDT_DIAG288_INIT:
62         diag288->enabled = true;
63         /* fall through */
64     case WDT_DIAG288_CHANGE:
65         if (!diag288->enabled) {
66             return -1;
67         }
68         timer_mod(diag288->timer,
69                   qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
70                   timeout * get_ticks_per_sec());
71         break;
72     case WDT_DIAG288_CANCEL:
73         if (!diag288->enabled) {
74             return -1;
75         }
76         diag288->enabled = false;
77         timer_del(diag288->timer);
78         break;
79     default:
80         return -1;
81     }
82 
83     return 0;
84 }
85 
86 static void wdt_diag288_realize(DeviceState *dev, Error **errp)
87 {
88     DIAG288State *diag288 = DIAG288(dev);
89 
90     qemu_register_reset(diag288_reset, diag288);
91     diag288->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, diag288_timer_expired,
92                                   dev);
93 }
94 
95 static void wdt_diag288_unrealize(DeviceState *dev, Error **errp)
96 {
97     DIAG288State *diag288 = DIAG288(dev);
98 
99     timer_del(diag288->timer);
100     timer_free(diag288->timer);
101 }
102 
103 static void wdt_diag288_class_init(ObjectClass *klass, void *data)
104 {
105     DeviceClass *dc = DEVICE_CLASS(klass);
106     DIAG288Class *diag288 = DIAG288_CLASS(klass);
107 
108     dc->realize = wdt_diag288_realize;
109     dc->unrealize = wdt_diag288_unrealize;
110     dc->reset = wdt_diag288_reset;
111     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
112     dc->vmsd = &vmstate_diag288;
113     diag288->handle_timer = wdt_diag288_handle_timer;
114 }
115 
116 static const TypeInfo wdt_diag288_info = {
117     .class_init = wdt_diag288_class_init,
118     .parent = TYPE_DEVICE,
119     .name  = TYPE_WDT_DIAG288,
120     .instance_size  = sizeof(DIAG288State),
121     .class_size = sizeof(DIAG288Class),
122 };
123 
124 static void wdt_diag288_register_types(void)
125 {
126     watchdog_add_model(&model);
127     type_register_static(&wdt_diag288_info);
128 }
129 
130 type_init(wdt_diag288_register_types)
131