2 * watchdog device diag288 support
4 * Copyright IBM, Corp. 2015
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"
21 static WatchdogTimerModel model
= {
22 .wdt_name
= TYPE_WDT_DIAG288
,
23 .wdt_description
= "diag288 device for s390x platform",
26 static const VMStateDescription vmstate_diag288
= {
27 .name
= "vmstate_diag288",
29 .minimum_version_id
= 0,
30 .fields
= (VMStateField
[]) {
31 VMSTATE_TIMER_PTR(timer
, DIAG288State
),
32 VMSTATE_BOOL(enabled
, DIAG288State
),
37 static void wdt_diag288_reset(DeviceState
*dev
)
39 DIAG288State
*diag288
= DIAG288(dev
);
41 diag288
->enabled
= false;
42 timer_del(diag288
->timer
);
45 static void diag288_reset(void *opaque
)
47 DeviceState
*diag288
= opaque
;
49 wdt_diag288_reset(diag288
);
52 static void diag288_timer_expired(void *dev
)
54 qemu_log_mask(CPU_LOG_RESET
, "Watchdog timer expired.\n");
55 /* Reset the watchdog only if the guest gets notified about
56 * expiry. watchdog_perform_action() may temporarily relinquish
57 * the BQL; reset before triggering the action to avoid races with
58 * diag288 instructions. */
59 switch (get_watchdog_action()) {
65 wdt_diag288_reset(dev
);
67 watchdog_perform_action();
70 static int wdt_diag288_handle_timer(DIAG288State
*diag288
,
71 uint64_t func
, uint64_t timeout
)
74 case WDT_DIAG288_INIT
:
75 diag288
->enabled
= true;
77 case WDT_DIAG288_CHANGE
:
78 if (!diag288
->enabled
) {
81 timer_mod(diag288
->timer
,
82 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
83 timeout
* NANOSECONDS_PER_SECOND
);
85 case WDT_DIAG288_CANCEL
:
86 if (!diag288
->enabled
) {
89 diag288
->enabled
= false;
90 timer_del(diag288
->timer
);
99 static void wdt_diag288_realize(DeviceState
*dev
, Error
**errp
)
101 DIAG288State
*diag288
= DIAG288(dev
);
103 qemu_register_reset(diag288_reset
, diag288
);
104 diag288
->timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
, diag288_timer_expired
,
108 static void wdt_diag288_unrealize(DeviceState
*dev
, Error
**errp
)
110 DIAG288State
*diag288
= DIAG288(dev
);
112 timer_del(diag288
->timer
);
113 timer_free(diag288
->timer
);
116 static void wdt_diag288_class_init(ObjectClass
*klass
, void *data
)
118 DeviceClass
*dc
= DEVICE_CLASS(klass
);
119 DIAG288Class
*diag288
= DIAG288_CLASS(klass
);
121 dc
->realize
= wdt_diag288_realize
;
122 dc
->unrealize
= wdt_diag288_unrealize
;
123 dc
->reset
= wdt_diag288_reset
;
124 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
125 dc
->vmsd
= &vmstate_diag288
;
126 diag288
->handle_timer
= wdt_diag288_handle_timer
;
129 static const TypeInfo wdt_diag288_info
= {
130 .class_init
= wdt_diag288_class_init
,
131 .parent
= TYPE_DEVICE
,
132 .name
= TYPE_WDT_DIAG288
,
133 .instance_size
= sizeof(DIAG288State
),
134 .class_size
= sizeof(DIAG288Class
),
137 static void wdt_diag288_register_types(void)
139 watchdog_add_model(&model
);
140 type_register_static(&wdt_diag288_info
);
143 type_init(wdt_diag288_register_types
)