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