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 "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" 19 20 static WatchdogTimerModel model = { 21 .wdt_name = TYPE_WDT_DIAG288, 22 .wdt_description = "diag288 device for s390x platform", 23 }; 24 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() 33 } 34 }; 35 36 static void wdt_diag288_reset(DeviceState *dev) 37 { 38 DIAG288State *diag288 = DIAG288(dev); 39 40 diag288->enabled = false; 41 timer_del(diag288->timer); 42 } 43 44 static void diag288_reset(void *opaque) 45 { 46 DeviceState *diag288 = opaque; 47 48 wdt_diag288_reset(diag288); 49 } 50 51 static void diag288_timer_expired(void *dev) 52 { 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); 65 } 66 watchdog_perform_action(); 67 } 68 69 static int wdt_diag288_handle_timer(DIAG288State *diag288, 70 uint64_t func, uint64_t timeout) 71 { 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; 79 } 80 timer_mod(diag288->timer, 81 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 82 timeout * get_ticks_per_sec()); 83 break; 84 case WDT_DIAG288_CANCEL: 85 if (!diag288->enabled) { 86 return -1; 87 } 88 diag288->enabled = false; 89 timer_del(diag288->timer); 90 break; 91 default: 92 return -1; 93 } 94 95 return 0; 96 } 97 98 static void wdt_diag288_realize(DeviceState *dev, Error **errp) 99 { 100 DIAG288State *diag288 = DIAG288(dev); 101 102 qemu_register_reset(diag288_reset, diag288); 103 diag288->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, diag288_timer_expired, 104 dev); 105 } 106 107 static void wdt_diag288_unrealize(DeviceState *dev, Error **errp) 108 { 109 DIAG288State *diag288 = DIAG288(dev); 110 111 timer_del(diag288->timer); 112 timer_free(diag288->timer); 113 } 114 115 static void wdt_diag288_class_init(ObjectClass *klass, void *data) 116 { 117 DeviceClass *dc = DEVICE_CLASS(klass); 118 DIAG288Class *diag288 = DIAG288_CLASS(klass); 119 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; 126 } 127 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), 134 }; 135 136 static void wdt_diag288_register_types(void) 137 { 138 watchdog_add_model(&model); 139 type_register_static(&wdt_diag288_info); 140 } 141 142 type_init(wdt_diag288_register_types) 143