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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * By Richard W.M. Jones (rjones@redhat.com).
26 #include "qemu-common.h"
27 #include "qemu-timer.h"
34 /*#define I6300ESB_DEBUG 1*/
37 #define i6300esb_debug(fs,...) \
38 fprintf(stderr,"i6300esb: %s: "fs,__func__,##__VA_ARGS__)
40 #define i6300esb_debug(fs,...)
43 #ifndef PCI_DEVICE_ID_INTEL_ESB_9
44 #define PCI_DEVICE_ID_INTEL_ESB_9 0x25ab
47 /* PCI configuration registers */
48 #define ESB_CONFIG_REG 0x60 /* Config register */
49 #define ESB_LOCK_REG 0x68 /* WDT lock register */
51 /* Memory mapped registers (offset from base address) */
52 #define ESB_TIMER1_REG 0x00 /* Timer1 value after each reset */
53 #define ESB_TIMER2_REG 0x04 /* Timer2 value after each reset */
54 #define ESB_GINTSR_REG 0x08 /* General Interrupt Status Register */
55 #define ESB_RELOAD_REG 0x0c /* Reload register */
57 /* Lock register bits */
58 #define ESB_WDT_FUNC (0x01 << 2) /* Watchdog functionality */
59 #define ESB_WDT_ENABLE (0x01 << 1) /* Enable WDT */
60 #define ESB_WDT_LOCK (0x01 << 0) /* Lock (nowayout) */
62 /* Config register bits */
63 #define ESB_WDT_REBOOT (0x01 << 5) /* Enable reboot on timeout */
64 #define ESB_WDT_FREQ (0x01 << 2) /* Decrement frequency */
65 #define ESB_WDT_INTTYPE (0x11 << 0) /* Interrupt type on timer1 timeout */
67 /* Reload register bits */
68 #define ESB_WDT_RELOAD (0x01 << 8) /* prevent timeout */
71 #define ESB_UNLOCK1 0x80 /* Step 1 to unlock reset registers */
72 #define ESB_UNLOCK2 0x86 /* Step 2 to unlock reset registers */
76 PCIDevice dev
; /* PCI device state, must be first field. */
78 int reboot_enabled
; /* "Reboot" on timer expiry. The real action
79 * performed depends on the -watchdog-action
80 * param passed on QEMU command line.
82 int clock_scale
; /* Clock scale. */
83 #define CLOCK_SCALE_1KHZ 0
84 #define CLOCK_SCALE_1MHZ 1
86 int int_type
; /* Interrupt type generated. */
87 #define INT_TYPE_IRQ 0 /* APIC 1, INT 10 */
88 #define INT_TYPE_SMI 2
89 #define INT_TYPE_DISABLED 3
91 int free_run
; /* If true, reload timer on expiry. */
92 int locked
; /* If true, enabled field cannot be changed. */
93 int enabled
; /* If true, watchdog is enabled. */
95 QEMUTimer
*timer
; /* The actual watchdog timer. */
97 uint32_t timer1_preload
; /* Values preloaded into timer1, timer2. */
98 uint32_t timer2_preload
;
99 int stage
; /* Stage (1 or 2). */
101 int unlock_state
; /* Guest writes 0x80, 0x86 to unlock the
102 * registers, and we transition through
103 * states 0 -> 1 -> 2 when this happens.
106 int previous_reboot_flag
; /* If the watchdog caused the previous
107 * reboot, this flag will be set.
111 typedef struct I6300State I6300State
;
113 /* This function is called when the watchdog has either been enabled
114 * (hence it starts counting down) or has been keep-alived.
116 static void i6300esb_restart_timer(I6300State
*d
, int stage
)
126 timeout
= d
->timer1_preload
;
128 timeout
= d
->timer2_preload
;
130 if (d
->clock_scale
== CLOCK_SCALE_1KHZ
)
135 /* Get the timeout in units of ticks_per_sec. */
136 timeout
= ticks_per_sec
* timeout
/ 33000000;
138 i6300esb_debug("stage %d, timeout %" PRIi64
"\n", d
->stage
, timeout
);
140 qemu_mod_timer(d
->timer
, qemu_get_clock(vm_clock
) + timeout
);
143 /* This is called when the guest disables the watchdog. */
144 static void i6300esb_disable_timer(I6300State
*d
)
146 i6300esb_debug("timer disabled\n");
148 qemu_del_timer(d
->timer
);
151 static void i6300esb_reset(I6300State
*d
)
153 /* XXX We should probably reset other parts of the state here,
154 * but we should also reset our state on general machine reset
155 * too. For now just disable the timer so it doesn't fire
156 * again after the reboot.
158 i6300esb_disable_timer(d
);
161 /* This function is called when the watchdog expires. Note that
162 * the hardware has two timers, and so expiry happens in two stages.
163 * If d->stage == 1 then we perform the first stage action (usually,
164 * sending an interrupt) and then restart the timer again for the
165 * second stage. If the second stage expires then the watchdog
166 * really has run out.
168 static void i6300esb_timer_expired(void *vp
)
170 I6300State
*d
= (I6300State
*) vp
;
172 i6300esb_debug("stage %d\n", d
->stage
);
175 /* What to do at the end of stage 1? */
176 switch (d
->int_type
) {
178 fprintf(stderr
, "i6300esb_timer_expired: I would send APIC 1 INT 10 here if I knew how (XXX)\n");
181 fprintf(stderr
, "i6300esb_timer_expired: I would send SMI here if I knew how (XXX)\n");
185 /* Start the second stage. */
186 i6300esb_restart_timer(d
, 2);
188 /* Second stage expired, reboot for real. */
189 if (d
->reboot_enabled
) {
190 d
->previous_reboot_flag
= 1;
191 watchdog_perform_action(); /* This reboots, exits, etc */
195 /* In "free running mode" we start stage 1 again. */
197 i6300esb_restart_timer(d
, 1);
201 static void i6300esb_config_write(PCIDevice
*dev
, uint32_t addr
,
202 uint32_t data
, int len
)
204 I6300State
*d
= (I6300State
*) dev
;
207 i6300esb_debug("addr = %x, data = %x, len = %d\n", addr
, data
, len
);
209 if (addr
== ESB_CONFIG_REG
&& len
== 2) {
210 d
->reboot_enabled
= (data
& ESB_WDT_REBOOT
) == 0;
212 (data
& ESB_WDT_FREQ
) != 0 ? CLOCK_SCALE_1MHZ
: CLOCK_SCALE_1KHZ
;
213 d
->int_type
= (data
& ESB_WDT_INTTYPE
);
214 } else if (addr
== ESB_LOCK_REG
&& len
== 1) {
216 d
->locked
= (data
& ESB_WDT_LOCK
) != 0;
217 d
->free_run
= (data
& ESB_WDT_FUNC
) != 0;
219 d
->enabled
= (data
& ESB_WDT_ENABLE
) != 0;
220 if (!old
&& d
->enabled
) /* Enabled transitioned from 0 -> 1 */
221 i6300esb_restart_timer(d
, 1);
222 else if (!d
->enabled
)
223 i6300esb_disable_timer(d
);
226 pci_default_write_config(dev
, addr
, data
, len
);
230 static uint32_t i6300esb_config_read(PCIDevice
*dev
, uint32_t addr
, int len
)
232 I6300State
*d
= (I6300State
*) dev
;
235 i6300esb_debug ("addr = %x, len = %d\n", addr
, len
);
237 if (addr
== ESB_CONFIG_REG
&& len
== 2) {
239 (d
->reboot_enabled
? 0 : ESB_WDT_REBOOT
) |
240 (d
->clock_scale
== CLOCK_SCALE_1MHZ
? ESB_WDT_FREQ
: 0) |
243 } else if (addr
== ESB_LOCK_REG
&& len
== 1) {
245 (d
->free_run
? ESB_WDT_FUNC
: 0) |
246 (d
->locked
? ESB_WDT_LOCK
: 0) |
247 (d
->enabled
? ESB_WDT_ENABLE
: 0);
250 return pci_default_read_config(dev
, addr
, len
);
254 static uint32_t i6300esb_mem_readb(void *vp
, target_phys_addr_t addr
)
256 i6300esb_debug ("addr = %x\n", (int) addr
);
261 static uint32_t i6300esb_mem_readw(void *vp
, target_phys_addr_t addr
)
264 I6300State
*d
= (I6300State
*) vp
;
266 i6300esb_debug("addr = %x\n", (int) addr
);
269 /* The previous reboot flag is really bit 9, but there is
270 * a bug in the Linux driver where it thinks it's bit 12.
273 data
= d
->previous_reboot_flag
? 0x1200 : 0;
279 static uint32_t i6300esb_mem_readl(void *vp
, target_phys_addr_t addr
)
281 i6300esb_debug("addr = %x\n", (int) addr
);
286 static void i6300esb_mem_writeb(void *vp
, target_phys_addr_t addr
, uint32_t val
)
288 I6300State
*d
= (I6300State
*) vp
;
290 i6300esb_debug("addr = %x, val = %x\n", (int) addr
, val
);
292 if (addr
== 0xc && val
== 0x80)
294 else if (addr
== 0xc && val
== 0x86 && d
->unlock_state
== 1)
298 static void i6300esb_mem_writew(void *vp
, target_phys_addr_t addr
, uint32_t val
)
300 I6300State
*d
= (I6300State
*) vp
;
302 i6300esb_debug("addr = %x, val = %x\n", (int) addr
, val
);
304 if (addr
== 0xc && val
== 0x80)
306 else if (addr
== 0xc && val
== 0x86 && d
->unlock_state
== 1)
309 if (d
->unlock_state
== 2) {
311 if ((val
& 0x100) != 0)
312 /* This is the "ping" from the userspace watchdog in
315 i6300esb_restart_timer(d
, 1);
317 /* Setting bit 9 resets the previous reboot flag.
318 * There's a bug in the Linux driver where it sets
321 if ((val
& 0x200) != 0 || (val
& 0x1000) != 0) {
322 d
->previous_reboot_flag
= 0;
331 static void i6300esb_mem_writel(void *vp
, target_phys_addr_t addr
, uint32_t val
)
333 I6300State
*d
= (I6300State
*) vp
;
335 i6300esb_debug ("addr = %x, val = %x\n", (int) addr
, val
);
337 if (addr
== 0xc && val
== 0x80)
339 else if (addr
== 0xc && val
== 0x86 && d
->unlock_state
== 1)
342 if (d
->unlock_state
== 2) {
344 d
->timer1_preload
= val
& 0xfffff;
346 d
->timer2_preload
= val
& 0xfffff;
353 static void i6300esb_map(PCIDevice
*dev
, int region_num
,
354 uint32_t addr
, uint32_t size
, int type
)
356 static CPUReadMemoryFunc
*mem_read
[3] = {
361 static CPUWriteMemoryFunc
*mem_write
[3] = {
366 I6300State
*d
= (I6300State
*) dev
;
369 i6300esb_debug("addr = %x, size = %x, type = %d\n", addr
, size
, type
);
371 io_mem
= cpu_register_io_memory(mem_read
, mem_write
, d
);
372 cpu_register_physical_memory (addr
, 0x10, io_mem
);
373 /* qemu_register_coalesced_mmio (addr, 0x10); ? */
376 static void i6300esb_save(QEMUFile
*f
, void *vp
)
378 I6300State
*d
= (I6300State
*) vp
;
380 pci_device_save(&d
->dev
, f
);
381 qemu_put_be32(f
, d
->reboot_enabled
);
382 qemu_put_be32(f
, d
->clock_scale
);
383 qemu_put_be32(f
, d
->int_type
);
384 qemu_put_be32(f
, d
->free_run
);
385 qemu_put_be32(f
, d
->locked
);
386 qemu_put_be32(f
, d
->enabled
);
387 qemu_put_timer(f
, d
->timer
);
388 qemu_put_be32(f
, d
->timer1_preload
);
389 qemu_put_be32(f
, d
->timer2_preload
);
390 qemu_put_be32(f
, d
->stage
);
391 qemu_put_be32(f
, d
->unlock_state
);
392 qemu_put_be32(f
, d
->previous_reboot_flag
);
395 static int i6300esb_load(QEMUFile
*f
, void *vp
, int version
)
397 I6300State
*d
= (I6300State
*) vp
;
399 if (version
!= sizeof (I6300State
))
402 pci_device_load(&d
->dev
, f
);
403 d
->reboot_enabled
= qemu_get_be32(f
);
404 d
->clock_scale
= qemu_get_be32(f
);
405 d
->int_type
= qemu_get_be32(f
);
406 d
->free_run
= qemu_get_be32(f
);
407 d
->locked
= qemu_get_be32(f
);
408 d
->enabled
= qemu_get_be32(f
);
409 qemu_get_timer(f
, d
->timer
);
410 d
->timer1_preload
= qemu_get_be32(f
);
411 d
->timer2_preload
= qemu_get_be32(f
);
412 d
->stage
= qemu_get_be32(f
);
413 d
->unlock_state
= qemu_get_be32(f
);
414 d
->previous_reboot_flag
= qemu_get_be32(f
);
419 /* Create and initialize a virtual Intel 6300ESB during PC creation. */
420 static void i6300esb_pc_init(PCIBus
*pci_bus
)
426 fprintf(stderr
, "wdt_i6300esb: no PCI bus in this machine\n");
431 pci_register_device (pci_bus
, "i6300esb_wdt", sizeof (I6300State
),
433 i6300esb_config_read
, i6300esb_config_write
);
435 d
->reboot_enabled
= 1;
436 d
->clock_scale
= CLOCK_SCALE_1KHZ
;
437 d
->int_type
= INT_TYPE_IRQ
;
441 d
->timer
= qemu_new_timer(vm_clock
, i6300esb_timer_expired
, d
);
442 d
->timer1_preload
= 0xfffff;
443 d
->timer2_preload
= 0xfffff;
446 d
->previous_reboot_flag
= 0;
448 pci_conf
= d
->dev
.config
;
449 pci_config_set_vendor_id(pci_conf
, PCI_VENDOR_ID_INTEL
);
450 pci_config_set_device_id(pci_conf
, PCI_DEVICE_ID_INTEL_ESB_9
);
451 pci_config_set_class(pci_conf
, PCI_CLASS_SYSTEM_OTHER
);
452 pci_conf
[0x0e] = 0x00;
454 pci_register_bar(&d
->dev
, 0, 0x10,
455 PCI_ADDRESS_SPACE_MEM
, i6300esb_map
);
457 register_savevm("i6300esb_wdt", -1, sizeof(I6300State
),
458 i6300esb_save
, i6300esb_load
, d
);
461 static WatchdogTimerModel model
= {
462 .wdt_name
= "i6300esb",
463 .wdt_description
= "Intel 6300ESB",
464 .wdt_pc_init
= i6300esb_pc_init
,
467 void wdt_i6300esb_init(void)
469 watchdog_add_model(&model
);