Merge remote-tracking branch 'qemu-kvm-tmp/memory/core' into staging
[qemu.git] / hw / wdt_i6300esb.c
blob20d86731865b20d9c663a5b8c0b95b041848eb7d
1 /*
2 * Virtual hardware watchdog.
4 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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/>.
19 * By Richard W.M. Jones (rjones@redhat.com).
22 #include <inttypes.h>
24 #include "qemu-common.h"
25 #include "qemu-timer.h"
26 #include "watchdog.h"
27 #include "hw.h"
28 #include "pci.h"
30 /*#define I6300ESB_DEBUG 1*/
32 #ifdef I6300ESB_DEBUG
33 #define i6300esb_debug(fs,...) \
34 fprintf(stderr,"i6300esb: %s: "fs,__func__,##__VA_ARGS__)
35 #else
36 #define i6300esb_debug(fs,...)
37 #endif
39 /* PCI configuration registers */
40 #define ESB_CONFIG_REG 0x60 /* Config register */
41 #define ESB_LOCK_REG 0x68 /* WDT lock register */
43 /* Memory mapped registers (offset from base address) */
44 #define ESB_TIMER1_REG 0x00 /* Timer1 value after each reset */
45 #define ESB_TIMER2_REG 0x04 /* Timer2 value after each reset */
46 #define ESB_GINTSR_REG 0x08 /* General Interrupt Status Register */
47 #define ESB_RELOAD_REG 0x0c /* Reload register */
49 /* Lock register bits */
50 #define ESB_WDT_FUNC (0x01 << 2) /* Watchdog functionality */
51 #define ESB_WDT_ENABLE (0x01 << 1) /* Enable WDT */
52 #define ESB_WDT_LOCK (0x01 << 0) /* Lock (nowayout) */
54 /* Config register bits */
55 #define ESB_WDT_REBOOT (0x01 << 5) /* Enable reboot on timeout */
56 #define ESB_WDT_FREQ (0x01 << 2) /* Decrement frequency */
57 #define ESB_WDT_INTTYPE (0x11 << 0) /* Interrupt type on timer1 timeout */
59 /* Reload register bits */
60 #define ESB_WDT_RELOAD (0x01 << 8) /* prevent timeout */
62 /* Magic constants */
63 #define ESB_UNLOCK1 0x80 /* Step 1 to unlock reset registers */
64 #define ESB_UNLOCK2 0x86 /* Step 2 to unlock reset registers */
66 /* Device state. */
67 struct I6300State {
68 PCIDevice dev;
69 MemoryRegion io_mem;
71 int reboot_enabled; /* "Reboot" on timer expiry. The real action
72 * performed depends on the -watchdog-action
73 * param passed on QEMU command line.
75 int clock_scale; /* Clock scale. */
76 #define CLOCK_SCALE_1KHZ 0
77 #define CLOCK_SCALE_1MHZ 1
79 int int_type; /* Interrupt type generated. */
80 #define INT_TYPE_IRQ 0 /* APIC 1, INT 10 */
81 #define INT_TYPE_SMI 2
82 #define INT_TYPE_DISABLED 3
84 int free_run; /* If true, reload timer on expiry. */
85 int locked; /* If true, enabled field cannot be changed. */
86 int enabled; /* If true, watchdog is enabled. */
88 QEMUTimer *timer; /* The actual watchdog timer. */
90 uint32_t timer1_preload; /* Values preloaded into timer1, timer2. */
91 uint32_t timer2_preload;
92 int stage; /* Stage (1 or 2). */
94 int unlock_state; /* Guest writes 0x80, 0x86 to unlock the
95 * registers, and we transition through
96 * states 0 -> 1 -> 2 when this happens.
99 int previous_reboot_flag; /* If the watchdog caused the previous
100 * reboot, this flag will be set.
104 typedef struct I6300State I6300State;
106 /* This function is called when the watchdog has either been enabled
107 * (hence it starts counting down) or has been keep-alived.
109 static void i6300esb_restart_timer(I6300State *d, int stage)
111 int64_t timeout;
113 if (!d->enabled)
114 return;
116 d->stage = stage;
118 if (d->stage <= 1)
119 timeout = d->timer1_preload;
120 else
121 timeout = d->timer2_preload;
123 if (d->clock_scale == CLOCK_SCALE_1KHZ)
124 timeout <<= 15;
125 else
126 timeout <<= 5;
128 /* Get the timeout in units of ticks_per_sec. */
129 timeout = get_ticks_per_sec() * timeout / 33000000;
131 i6300esb_debug("stage %d, timeout %" PRIi64 "\n", d->stage, timeout);
133 qemu_mod_timer(d->timer, qemu_get_clock_ns(vm_clock) + timeout);
136 /* This is called when the guest disables the watchdog. */
137 static void i6300esb_disable_timer(I6300State *d)
139 i6300esb_debug("timer disabled\n");
141 qemu_del_timer(d->timer);
144 static void i6300esb_reset(DeviceState *dev)
146 PCIDevice *pdev = DO_UPCAST(PCIDevice, qdev, dev);
147 I6300State *d = DO_UPCAST(I6300State, dev, pdev);
149 i6300esb_debug("I6300State = %p\n", d);
151 i6300esb_disable_timer(d);
153 /* NB: Don't change d->previous_reboot_flag in this function. */
155 d->reboot_enabled = 1;
156 d->clock_scale = CLOCK_SCALE_1KHZ;
157 d->int_type = INT_TYPE_IRQ;
158 d->free_run = 0;
159 d->locked = 0;
160 d->enabled = 0;
161 d->timer1_preload = 0xfffff;
162 d->timer2_preload = 0xfffff;
163 d->stage = 1;
164 d->unlock_state = 0;
167 /* This function is called when the watchdog expires. Note that
168 * the hardware has two timers, and so expiry happens in two stages.
169 * If d->stage == 1 then we perform the first stage action (usually,
170 * sending an interrupt) and then restart the timer again for the
171 * second stage. If the second stage expires then the watchdog
172 * really has run out.
174 static void i6300esb_timer_expired(void *vp)
176 I6300State *d = vp;
178 i6300esb_debug("stage %d\n", d->stage);
180 if (d->stage == 1) {
181 /* What to do at the end of stage 1? */
182 switch (d->int_type) {
183 case INT_TYPE_IRQ:
184 fprintf(stderr, "i6300esb_timer_expired: I would send APIC 1 INT 10 here if I knew how (XXX)\n");
185 break;
186 case INT_TYPE_SMI:
187 fprintf(stderr, "i6300esb_timer_expired: I would send SMI here if I knew how (XXX)\n");
188 break;
191 /* Start the second stage. */
192 i6300esb_restart_timer(d, 2);
193 } else {
194 /* Second stage expired, reboot for real. */
195 if (d->reboot_enabled) {
196 d->previous_reboot_flag = 1;
197 watchdog_perform_action(); /* This reboots, exits, etc */
198 i6300esb_reset(&d->dev.qdev);
201 /* In "free running mode" we start stage 1 again. */
202 if (d->free_run)
203 i6300esb_restart_timer(d, 1);
207 static void i6300esb_config_write(PCIDevice *dev, uint32_t addr,
208 uint32_t data, int len)
210 I6300State *d = DO_UPCAST(I6300State, dev, dev);
211 int old;
213 i6300esb_debug("addr = %x, data = %x, len = %d\n", addr, data, len);
215 if (addr == ESB_CONFIG_REG && len == 2) {
216 d->reboot_enabled = (data & ESB_WDT_REBOOT) == 0;
217 d->clock_scale =
218 (data & ESB_WDT_FREQ) != 0 ? CLOCK_SCALE_1MHZ : CLOCK_SCALE_1KHZ;
219 d->int_type = (data & ESB_WDT_INTTYPE);
220 } else if (addr == ESB_LOCK_REG && len == 1) {
221 if (!d->locked) {
222 d->locked = (data & ESB_WDT_LOCK) != 0;
223 d->free_run = (data & ESB_WDT_FUNC) != 0;
224 old = d->enabled;
225 d->enabled = (data & ESB_WDT_ENABLE) != 0;
226 if (!old && d->enabled) /* Enabled transitioned from 0 -> 1 */
227 i6300esb_restart_timer(d, 1);
228 else if (!d->enabled)
229 i6300esb_disable_timer(d);
231 } else {
232 pci_default_write_config(dev, addr, data, len);
236 static uint32_t i6300esb_config_read(PCIDevice *dev, uint32_t addr, int len)
238 I6300State *d = DO_UPCAST(I6300State, dev, dev);
239 uint32_t data;
241 i6300esb_debug ("addr = %x, len = %d\n", addr, len);
243 if (addr == ESB_CONFIG_REG && len == 2) {
244 data =
245 (d->reboot_enabled ? 0 : ESB_WDT_REBOOT) |
246 (d->clock_scale == CLOCK_SCALE_1MHZ ? ESB_WDT_FREQ : 0) |
247 d->int_type;
248 return data;
249 } else if (addr == ESB_LOCK_REG && len == 1) {
250 data =
251 (d->free_run ? ESB_WDT_FUNC : 0) |
252 (d->locked ? ESB_WDT_LOCK : 0) |
253 (d->enabled ? ESB_WDT_ENABLE : 0);
254 return data;
255 } else {
256 return pci_default_read_config(dev, addr, len);
260 static uint32_t i6300esb_mem_readb(void *vp, target_phys_addr_t addr)
262 i6300esb_debug ("addr = %x\n", (int) addr);
264 return 0;
267 static uint32_t i6300esb_mem_readw(void *vp, target_phys_addr_t addr)
269 uint32_t data = 0;
270 I6300State *d = vp;
272 i6300esb_debug("addr = %x\n", (int) addr);
274 if (addr == 0xc) {
275 /* The previous reboot flag is really bit 9, but there is
276 * a bug in the Linux driver where it thinks it's bit 12.
277 * Set both.
279 data = d->previous_reboot_flag ? 0x1200 : 0;
282 return data;
285 static uint32_t i6300esb_mem_readl(void *vp, target_phys_addr_t addr)
287 i6300esb_debug("addr = %x\n", (int) addr);
289 return 0;
292 static void i6300esb_mem_writeb(void *vp, target_phys_addr_t addr, uint32_t val)
294 I6300State *d = vp;
296 i6300esb_debug("addr = %x, val = %x\n", (int) addr, val);
298 if (addr == 0xc && val == 0x80)
299 d->unlock_state = 1;
300 else if (addr == 0xc && val == 0x86 && d->unlock_state == 1)
301 d->unlock_state = 2;
304 static void i6300esb_mem_writew(void *vp, target_phys_addr_t addr, uint32_t val)
306 I6300State *d = vp;
308 i6300esb_debug("addr = %x, val = %x\n", (int) addr, val);
310 if (addr == 0xc && val == 0x80)
311 d->unlock_state = 1;
312 else if (addr == 0xc && val == 0x86 && d->unlock_state == 1)
313 d->unlock_state = 2;
314 else {
315 if (d->unlock_state == 2) {
316 if (addr == 0xc) {
317 if ((val & 0x100) != 0)
318 /* This is the "ping" from the userspace watchdog in
319 * the guest ...
321 i6300esb_restart_timer(d, 1);
323 /* Setting bit 9 resets the previous reboot flag.
324 * There's a bug in the Linux driver where it sets
325 * bit 12 instead.
327 if ((val & 0x200) != 0 || (val & 0x1000) != 0) {
328 d->previous_reboot_flag = 0;
332 d->unlock_state = 0;
337 static void i6300esb_mem_writel(void *vp, target_phys_addr_t addr, uint32_t val)
339 I6300State *d = vp;
341 i6300esb_debug ("addr = %x, val = %x\n", (int) addr, val);
343 if (addr == 0xc && val == 0x80)
344 d->unlock_state = 1;
345 else if (addr == 0xc && val == 0x86 && d->unlock_state == 1)
346 d->unlock_state = 2;
347 else {
348 if (d->unlock_state == 2) {
349 if (addr == 0)
350 d->timer1_preload = val & 0xfffff;
351 else if (addr == 4)
352 d->timer2_preload = val & 0xfffff;
354 d->unlock_state = 0;
359 static const MemoryRegionOps i6300esb_ops = {
360 .old_mmio = {
361 .read = {
362 i6300esb_mem_readb,
363 i6300esb_mem_readw,
364 i6300esb_mem_readl,
366 .write = {
367 i6300esb_mem_writeb,
368 i6300esb_mem_writew,
369 i6300esb_mem_writel,
372 .endianness = DEVICE_NATIVE_ENDIAN,
375 static const VMStateDescription vmstate_i6300esb = {
376 .name = "i6300esb_wdt",
377 .version_id = sizeof(I6300State),
378 .minimum_version_id = sizeof(I6300State),
379 .minimum_version_id_old = sizeof(I6300State),
380 .fields = (VMStateField []) {
381 VMSTATE_PCI_DEVICE(dev, I6300State),
382 VMSTATE_INT32(reboot_enabled, I6300State),
383 VMSTATE_INT32(clock_scale, I6300State),
384 VMSTATE_INT32(int_type, I6300State),
385 VMSTATE_INT32(free_run, I6300State),
386 VMSTATE_INT32(locked, I6300State),
387 VMSTATE_INT32(enabled, I6300State),
388 VMSTATE_TIMER(timer, I6300State),
389 VMSTATE_UINT32(timer1_preload, I6300State),
390 VMSTATE_UINT32(timer2_preload, I6300State),
391 VMSTATE_INT32(stage, I6300State),
392 VMSTATE_INT32(unlock_state, I6300State),
393 VMSTATE_INT32(previous_reboot_flag, I6300State),
394 VMSTATE_END_OF_LIST()
398 static int i6300esb_init(PCIDevice *dev)
400 I6300State *d = DO_UPCAST(I6300State, dev, dev);
402 i6300esb_debug("I6300State = %p\n", d);
404 d->timer = qemu_new_timer_ns(vm_clock, i6300esb_timer_expired, d);
405 d->previous_reboot_flag = 0;
407 memory_region_init_io(&d->io_mem, &i6300esb_ops, d, "i6300esb", 0x10);
408 pci_register_bar(&d->dev, 0, 0, &d->io_mem);
409 /* qemu_register_coalesced_mmio (addr, 0x10); ? */
411 return 0;
414 static int i6300esb_exit(PCIDevice *dev)
416 I6300State *d = DO_UPCAST(I6300State, dev, dev);
418 memory_region_destroy(&d->io_mem);
420 return 0;
423 static WatchdogTimerModel model = {
424 .wdt_name = "i6300esb",
425 .wdt_description = "Intel 6300ESB",
428 static PCIDeviceInfo i6300esb_info = {
429 .qdev.name = "i6300esb",
430 .qdev.size = sizeof(I6300State),
431 .qdev.vmsd = &vmstate_i6300esb,
432 .qdev.reset = i6300esb_reset,
433 .config_read = i6300esb_config_read,
434 .config_write = i6300esb_config_write,
435 .init = i6300esb_init,
436 .exit = i6300esb_exit,
437 .vendor_id = PCI_VENDOR_ID_INTEL,
438 .device_id = PCI_DEVICE_ID_INTEL_ESB_9,
439 .class_id = PCI_CLASS_SYSTEM_OTHER,
442 static void i6300esb_register_devices(void)
444 watchdog_add_model(&model);
445 pci_qdev_register(&i6300esb_info);
448 device_init(i6300esb_register_devices);