1 /* 2 * Virtual hardware watchdog. 3 * 4 * Copyright (C) 2009 Red Hat Inc. 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, see <http://www.gnu.org/licenses/>. 18 * 19 * By Richard W.M. Jones (rjones@redhat.com). 20 */ 21 22 #include "qemu/osdep.h" 23 #include "qemu/module.h" 24 #include "qemu/timer.h" 25 #include "sysemu/watchdog.h" 26 #include "hw/isa/isa.h" 27 #include "migration/vmstate.h" 28 #include "qom/object.h" 29 30 /*#define IB700_DEBUG 1*/ 31 32 #ifdef IB700_DEBUG 33 #define ib700_debug(fs,...) \ 34 fprintf(stderr,"ib700: %s: "fs,__func__,##__VA_ARGS__) 35 #else 36 #define ib700_debug(fs,...) 37 #endif 38 39 #define TYPE_IB700 "ib700" 40 typedef struct IB700state IB700State; 41 #define IB700(obj) OBJECT_CHECK(IB700State, (obj), TYPE_IB700) 42 43 struct IB700state { 44 ISADevice parent_obj; 45 46 QEMUTimer *timer; 47 48 PortioList port_list; 49 }; 50 51 /* This is the timer. We use a global here because the watchdog 52 * code ensures there is only one watchdog (it is located at a fixed, 53 * unchangeable IO port, so there could only ever be one anyway). 54 */ 55 56 /* A write to this register enables the timer. */ 57 static void ib700_write_enable_reg(void *vp, uint32_t addr, uint32_t data) 58 { 59 IB700State *s = vp; 60 static int time_map[] = { 61 30, 28, 26, 24, 22, 20, 18, 16, 62 14, 12, 10, 8, 6, 4, 2, 0 63 }; 64 int64_t timeout; 65 66 ib700_debug("addr = %x, data = %x\n", addr, data); 67 68 timeout = (int64_t) time_map[data & 0xF] * NANOSECONDS_PER_SECOND; 69 timer_mod(s->timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + timeout); 70 } 71 72 /* A write (of any value) to this register disables the timer. */ 73 static void ib700_write_disable_reg(void *vp, uint32_t addr, uint32_t data) 74 { 75 IB700State *s = vp; 76 77 ib700_debug("addr = %x, data = %x\n", addr, data); 78 79 timer_del(s->timer); 80 } 81 82 /* This is called when the watchdog expires. */ 83 static void ib700_timer_expired(void *vp) 84 { 85 IB700State *s = vp; 86 87 ib700_debug("watchdog expired\n"); 88 89 watchdog_perform_action(); 90 timer_del(s->timer); 91 } 92 93 static const VMStateDescription vmstate_ib700 = { 94 .name = "ib700_wdt", 95 .version_id = 0, 96 .minimum_version_id = 0, 97 .fields = (VMStateField[]) { 98 VMSTATE_TIMER_PTR(timer, IB700State), 99 VMSTATE_END_OF_LIST() 100 } 101 }; 102 103 static const MemoryRegionPortio wdt_portio_list[] = { 104 { 0x441, 2, 1, .write = ib700_write_disable_reg, }, 105 { 0x443, 2, 1, .write = ib700_write_enable_reg, }, 106 PORTIO_END_OF_LIST(), 107 }; 108 109 static void wdt_ib700_realize(DeviceState *dev, Error **errp) 110 { 111 IB700State *s = IB700(dev); 112 113 ib700_debug("watchdog init\n"); 114 115 s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ib700_timer_expired, s); 116 117 portio_list_init(&s->port_list, OBJECT(s), wdt_portio_list, s, "ib700"); 118 portio_list_add(&s->port_list, isa_address_space_io(&s->parent_obj), 0); 119 } 120 121 static void wdt_ib700_reset(DeviceState *dev) 122 { 123 IB700State *s = IB700(dev); 124 125 ib700_debug("watchdog reset\n"); 126 127 timer_del(s->timer); 128 } 129 130 static WatchdogTimerModel model = { 131 .wdt_name = "ib700", 132 .wdt_description = "iBASE 700", 133 }; 134 135 static void wdt_ib700_class_init(ObjectClass *klass, void *data) 136 { 137 DeviceClass *dc = DEVICE_CLASS(klass); 138 139 dc->realize = wdt_ib700_realize; 140 dc->reset = wdt_ib700_reset; 141 dc->vmsd = &vmstate_ib700; 142 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 143 } 144 145 static const TypeInfo wdt_ib700_info = { 146 .name = TYPE_IB700, 147 .parent = TYPE_ISA_DEVICE, 148 .instance_size = sizeof(IB700State), 149 .class_init = wdt_ib700_class_init, 150 }; 151 152 static void wdt_ib700_register_types(void) 153 { 154 watchdog_add_model(&model); 155 type_register_static(&wdt_ib700_info); 156 } 157 158 type_init(wdt_ib700_register_types) 159