Statistics
| Branch: | Revision:

root / hw / wdt_i6300esb.c @ 28b70c9d

History | View | Annotate | Download (13.9 kB)

1 9dd986cc Richard W.M. Jones
/*
2 9dd986cc Richard W.M. Jones
 * Virtual hardware watchdog.
3 9dd986cc Richard W.M. Jones
 *
4 9dd986cc Richard W.M. Jones
 * Copyright (C) 2009 Red Hat Inc.
5 9dd986cc Richard W.M. Jones
 *
6 9dd986cc Richard W.M. Jones
 * This program is free software; you can redistribute it and/or
7 9dd986cc Richard W.M. Jones
 * modify it under the terms of the GNU General Public License
8 9dd986cc Richard W.M. Jones
 * as published by the Free Software Foundation; either version 2
9 9dd986cc Richard W.M. Jones
 * of the License, or (at your option) any later version.
10 9dd986cc Richard W.M. Jones
 *
11 9dd986cc Richard W.M. Jones
 * This program is distributed in the hope that it will be useful,
12 9dd986cc Richard W.M. Jones
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 9dd986cc Richard W.M. Jones
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 9dd986cc Richard W.M. Jones
 * GNU General Public License for more details.
15 9dd986cc Richard W.M. Jones
 *
16 9dd986cc Richard W.M. Jones
 * You should have received a copy of the GNU General Public License
17 8167ee88 Blue Swirl
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 9dd986cc Richard W.M. Jones
 *
19 9dd986cc Richard W.M. Jones
 * By Richard W.M. Jones (rjones@redhat.com).
20 9dd986cc Richard W.M. Jones
 */
21 9dd986cc Richard W.M. Jones
22 9dd986cc Richard W.M. Jones
#include <inttypes.h>
23 9dd986cc Richard W.M. Jones
24 9dd986cc Richard W.M. Jones
#include "qemu-common.h"
25 9dd986cc Richard W.M. Jones
#include "qemu-timer.h"
26 9dd986cc Richard W.M. Jones
#include "watchdog.h"
27 9dd986cc Richard W.M. Jones
#include "hw.h"
28 9dd986cc Richard W.M. Jones
#include "pci.h"
29 9dd986cc Richard W.M. Jones
30 9dd986cc Richard W.M. Jones
/*#define I6300ESB_DEBUG 1*/
31 9dd986cc Richard W.M. Jones
32 9dd986cc Richard W.M. Jones
#ifdef I6300ESB_DEBUG
33 9dd986cc Richard W.M. Jones
#define i6300esb_debug(fs,...) \
34 9dd986cc Richard W.M. Jones
    fprintf(stderr,"i6300esb: %s: "fs,__func__,##__VA_ARGS__)
35 9dd986cc Richard W.M. Jones
#else
36 9dd986cc Richard W.M. Jones
#define i6300esb_debug(fs,...)
37 9dd986cc Richard W.M. Jones
#endif
38 9dd986cc Richard W.M. Jones
39 9dd986cc Richard W.M. Jones
/* PCI configuration registers */
40 9dd986cc Richard W.M. Jones
#define ESB_CONFIG_REG  0x60            /* Config register                   */
41 9dd986cc Richard W.M. Jones
#define ESB_LOCK_REG    0x68            /* WDT lock register                 */
42 9dd986cc Richard W.M. Jones
43 9dd986cc Richard W.M. Jones
/* Memory mapped registers (offset from base address) */
44 9dd986cc Richard W.M. Jones
#define ESB_TIMER1_REG  0x00            /* Timer1 value after each reset     */
45 9dd986cc Richard W.M. Jones
#define ESB_TIMER2_REG  0x04            /* Timer2 value after each reset     */
46 9dd986cc Richard W.M. Jones
#define ESB_GINTSR_REG  0x08            /* General Interrupt Status Register */
47 9dd986cc Richard W.M. Jones
#define ESB_RELOAD_REG  0x0c            /* Reload register                   */
48 9dd986cc Richard W.M. Jones
49 9dd986cc Richard W.M. Jones
/* Lock register bits */
50 9dd986cc Richard W.M. Jones
#define ESB_WDT_FUNC    (0x01 << 2)   /* Watchdog functionality            */
51 9dd986cc Richard W.M. Jones
#define ESB_WDT_ENABLE  (0x01 << 1)   /* Enable WDT                        */
52 9dd986cc Richard W.M. Jones
#define ESB_WDT_LOCK    (0x01 << 0)   /* Lock (nowayout)                   */
53 9dd986cc Richard W.M. Jones
54 9dd986cc Richard W.M. Jones
/* Config register bits */
55 9dd986cc Richard W.M. Jones
#define ESB_WDT_REBOOT  (0x01 << 5)   /* Enable reboot on timeout          */
56 9dd986cc Richard W.M. Jones
#define ESB_WDT_FREQ    (0x01 << 2)   /* Decrement frequency               */
57 9dd986cc Richard W.M. Jones
#define ESB_WDT_INTTYPE (0x11 << 0)   /* Interrupt type on timer1 timeout  */
58 9dd986cc Richard W.M. Jones
59 9dd986cc Richard W.M. Jones
/* Reload register bits */
60 9dd986cc Richard W.M. Jones
#define ESB_WDT_RELOAD  (0x01 << 8)    /* prevent timeout                   */
61 9dd986cc Richard W.M. Jones
62 9dd986cc Richard W.M. Jones
/* Magic constants */
63 9dd986cc Richard W.M. Jones
#define ESB_UNLOCK1     0x80            /* Step 1 to unlock reset registers  */
64 9dd986cc Richard W.M. Jones
#define ESB_UNLOCK2     0x86            /* Step 2 to unlock reset registers  */
65 9dd986cc Richard W.M. Jones
66 9dd986cc Richard W.M. Jones
/* Device state. */
67 9dd986cc Richard W.M. Jones
struct I6300State {
68 9d472d51 Markus Armbruster
    PCIDevice dev;
69 d9c6ebd1 Avi Kivity
    MemoryRegion io_mem;
70 9dd986cc Richard W.M. Jones
71 9dd986cc Richard W.M. Jones
    int reboot_enabled;         /* "Reboot" on timer expiry.  The real action
72 9dd986cc Richard W.M. Jones
                                 * performed depends on the -watchdog-action
73 9dd986cc Richard W.M. Jones
                                 * param passed on QEMU command line.
74 9dd986cc Richard W.M. Jones
                                 */
75 9dd986cc Richard W.M. Jones
    int clock_scale;            /* Clock scale. */
76 9dd986cc Richard W.M. Jones
#define CLOCK_SCALE_1KHZ 0
77 9dd986cc Richard W.M. Jones
#define CLOCK_SCALE_1MHZ 1
78 9dd986cc Richard W.M. Jones
79 9dd986cc Richard W.M. Jones
    int int_type;               /* Interrupt type generated. */
80 9dd986cc Richard W.M. Jones
#define INT_TYPE_IRQ 0          /* APIC 1, INT 10 */
81 9dd986cc Richard W.M. Jones
#define INT_TYPE_SMI 2
82 9dd986cc Richard W.M. Jones
#define INT_TYPE_DISABLED 3
83 9dd986cc Richard W.M. Jones
84 9dd986cc Richard W.M. Jones
    int free_run;               /* If true, reload timer on expiry. */
85 9dd986cc Richard W.M. Jones
    int locked;                 /* If true, enabled field cannot be changed. */
86 9dd986cc Richard W.M. Jones
    int enabled;                /* If true, watchdog is enabled. */
87 9dd986cc Richard W.M. Jones
88 9dd986cc Richard W.M. Jones
    QEMUTimer *timer;           /* The actual watchdog timer. */
89 9dd986cc Richard W.M. Jones
90 9dd986cc Richard W.M. Jones
    uint32_t timer1_preload;    /* Values preloaded into timer1, timer2. */
91 9dd986cc Richard W.M. Jones
    uint32_t timer2_preload;
92 9dd986cc Richard W.M. Jones
    int stage;                  /* Stage (1 or 2). */
93 9dd986cc Richard W.M. Jones
94 9dd986cc Richard W.M. Jones
    int unlock_state;           /* Guest writes 0x80, 0x86 to unlock the
95 9dd986cc Richard W.M. Jones
                                 * registers, and we transition through
96 9dd986cc Richard W.M. Jones
                                 * states 0 -> 1 -> 2 when this happens.
97 9dd986cc Richard W.M. Jones
                                 */
98 9dd986cc Richard W.M. Jones
99 9dd986cc Richard W.M. Jones
    int previous_reboot_flag;   /* If the watchdog caused the previous
100 9dd986cc Richard W.M. Jones
                                 * reboot, this flag will be set.
101 9dd986cc Richard W.M. Jones
                                 */
102 9dd986cc Richard W.M. Jones
};
103 9dd986cc Richard W.M. Jones
104 9dd986cc Richard W.M. Jones
typedef struct I6300State I6300State;
105 9dd986cc Richard W.M. Jones
106 9dd986cc Richard W.M. Jones
/* This function is called when the watchdog has either been enabled
107 9dd986cc Richard W.M. Jones
 * (hence it starts counting down) or has been keep-alived.
108 9dd986cc Richard W.M. Jones
 */
109 9dd986cc Richard W.M. Jones
static void i6300esb_restart_timer(I6300State *d, int stage)
110 9dd986cc Richard W.M. Jones
{
111 9dd986cc Richard W.M. Jones
    int64_t timeout;
112 9dd986cc Richard W.M. Jones
113 9dd986cc Richard W.M. Jones
    if (!d->enabled)
114 9dd986cc Richard W.M. Jones
        return;
115 9dd986cc Richard W.M. Jones
116 9dd986cc Richard W.M. Jones
    d->stage = stage;
117 9dd986cc Richard W.M. Jones
118 9dd986cc Richard W.M. Jones
    if (d->stage <= 1)
119 9dd986cc Richard W.M. Jones
        timeout = d->timer1_preload;
120 9dd986cc Richard W.M. Jones
    else
121 9dd986cc Richard W.M. Jones
        timeout = d->timer2_preload;
122 9dd986cc Richard W.M. Jones
123 9dd986cc Richard W.M. Jones
    if (d->clock_scale == CLOCK_SCALE_1KHZ)
124 9dd986cc Richard W.M. Jones
        timeout <<= 15;
125 9dd986cc Richard W.M. Jones
    else
126 9dd986cc Richard W.M. Jones
        timeout <<= 5;
127 9dd986cc Richard W.M. Jones
128 9dd986cc Richard W.M. Jones
    /* Get the timeout in units of ticks_per_sec. */
129 6ee093c9 Juan Quintela
    timeout = get_ticks_per_sec() * timeout / 33000000;
130 9dd986cc Richard W.M. Jones
131 9dd986cc Richard W.M. Jones
    i6300esb_debug("stage %d, timeout %" PRIi64 "\n", d->stage, timeout);
132 9dd986cc Richard W.M. Jones
133 74475455 Paolo Bonzini
    qemu_mod_timer(d->timer, qemu_get_clock_ns(vm_clock) + timeout);
134 9dd986cc Richard W.M. Jones
}
135 9dd986cc Richard W.M. Jones
136 9dd986cc Richard W.M. Jones
/* This is called when the guest disables the watchdog. */
137 9dd986cc Richard W.M. Jones
static void i6300esb_disable_timer(I6300State *d)
138 9dd986cc Richard W.M. Jones
{
139 9dd986cc Richard W.M. Jones
    i6300esb_debug("timer disabled\n");
140 9dd986cc Richard W.M. Jones
141 9dd986cc Richard W.M. Jones
    qemu_del_timer(d->timer);
142 9dd986cc Richard W.M. Jones
}
143 9dd986cc Richard W.M. Jones
144 fa82e9c3 Bernhard Kohl
static void i6300esb_reset(DeviceState *dev)
145 9dd986cc Richard W.M. Jones
{
146 40021f08 Anthony Liguori
    PCIDevice *pdev = PCI_DEVICE(dev);
147 fa82e9c3 Bernhard Kohl
    I6300State *d = DO_UPCAST(I6300State, dev, pdev);
148 fa82e9c3 Bernhard Kohl
149 fa82e9c3 Bernhard Kohl
    i6300esb_debug("I6300State = %p\n", d);
150 fa82e9c3 Bernhard Kohl
151 9dd986cc Richard W.M. Jones
    i6300esb_disable_timer(d);
152 fa82e9c3 Bernhard Kohl
153 36888c63 Richard W.M. Jones
    /* NB: Don't change d->previous_reboot_flag in this function. */
154 36888c63 Richard W.M. Jones
155 fa82e9c3 Bernhard Kohl
    d->reboot_enabled = 1;
156 fa82e9c3 Bernhard Kohl
    d->clock_scale = CLOCK_SCALE_1KHZ;
157 fa82e9c3 Bernhard Kohl
    d->int_type = INT_TYPE_IRQ;
158 fa82e9c3 Bernhard Kohl
    d->free_run = 0;
159 fa82e9c3 Bernhard Kohl
    d->locked = 0;
160 fa82e9c3 Bernhard Kohl
    d->enabled = 0;
161 fa82e9c3 Bernhard Kohl
    d->timer1_preload = 0xfffff;
162 fa82e9c3 Bernhard Kohl
    d->timer2_preload = 0xfffff;
163 fa82e9c3 Bernhard Kohl
    d->stage = 1;
164 fa82e9c3 Bernhard Kohl
    d->unlock_state = 0;
165 9dd986cc Richard W.M. Jones
}
166 9dd986cc Richard W.M. Jones
167 9dd986cc Richard W.M. Jones
/* This function is called when the watchdog expires.  Note that
168 9dd986cc Richard W.M. Jones
 * the hardware has two timers, and so expiry happens in two stages.
169 9dd986cc Richard W.M. Jones
 * If d->stage == 1 then we perform the first stage action (usually,
170 9dd986cc Richard W.M. Jones
 * sending an interrupt) and then restart the timer again for the
171 9dd986cc Richard W.M. Jones
 * second stage.  If the second stage expires then the watchdog
172 9dd986cc Richard W.M. Jones
 * really has run out.
173 9dd986cc Richard W.M. Jones
 */
174 9dd986cc Richard W.M. Jones
static void i6300esb_timer_expired(void *vp)
175 9dd986cc Richard W.M. Jones
{
176 4f423e81 Juan Quintela
    I6300State *d = vp;
177 9dd986cc Richard W.M. Jones
178 9dd986cc Richard W.M. Jones
    i6300esb_debug("stage %d\n", d->stage);
179 9dd986cc Richard W.M. Jones
180 9dd986cc Richard W.M. Jones
    if (d->stage == 1) {
181 9dd986cc Richard W.M. Jones
        /* What to do at the end of stage 1? */
182 9dd986cc Richard W.M. Jones
        switch (d->int_type) {
183 9dd986cc Richard W.M. Jones
        case INT_TYPE_IRQ:
184 9dd986cc Richard W.M. Jones
            fprintf(stderr, "i6300esb_timer_expired: I would send APIC 1 INT 10 here if I knew how (XXX)\n");
185 9dd986cc Richard W.M. Jones
            break;
186 9dd986cc Richard W.M. Jones
        case INT_TYPE_SMI:
187 9dd986cc Richard W.M. Jones
            fprintf(stderr, "i6300esb_timer_expired: I would send SMI here if I knew how (XXX)\n");
188 9dd986cc Richard W.M. Jones
            break;
189 9dd986cc Richard W.M. Jones
        }
190 9dd986cc Richard W.M. Jones
191 9dd986cc Richard W.M. Jones
        /* Start the second stage. */
192 9dd986cc Richard W.M. Jones
        i6300esb_restart_timer(d, 2);
193 9dd986cc Richard W.M. Jones
    } else {
194 9dd986cc Richard W.M. Jones
        /* Second stage expired, reboot for real. */
195 9dd986cc Richard W.M. Jones
        if (d->reboot_enabled) {
196 9dd986cc Richard W.M. Jones
            d->previous_reboot_flag = 1;
197 9dd986cc Richard W.M. Jones
            watchdog_perform_action(); /* This reboots, exits, etc */
198 36888c63 Richard W.M. Jones
            i6300esb_reset(&d->dev.qdev);
199 9dd986cc Richard W.M. Jones
        }
200 9dd986cc Richard W.M. Jones
201 9dd986cc Richard W.M. Jones
        /* In "free running mode" we start stage 1 again. */
202 9dd986cc Richard W.M. Jones
        if (d->free_run)
203 9dd986cc Richard W.M. Jones
            i6300esb_restart_timer(d, 1);
204 9dd986cc Richard W.M. Jones
    }
205 9dd986cc Richard W.M. Jones
}
206 9dd986cc Richard W.M. Jones
207 9dd986cc Richard W.M. Jones
static void i6300esb_config_write(PCIDevice *dev, uint32_t addr,
208 9dd986cc Richard W.M. Jones
                                  uint32_t data, int len)
209 9dd986cc Richard W.M. Jones
{
210 d03f09cc Markus Armbruster
    I6300State *d = DO_UPCAST(I6300State, dev, dev);
211 9dd986cc Richard W.M. Jones
    int old;
212 9dd986cc Richard W.M. Jones
213 9dd986cc Richard W.M. Jones
    i6300esb_debug("addr = %x, data = %x, len = %d\n", addr, data, len);
214 9dd986cc Richard W.M. Jones
215 9dd986cc Richard W.M. Jones
    if (addr == ESB_CONFIG_REG && len == 2) {
216 9dd986cc Richard W.M. Jones
        d->reboot_enabled = (data & ESB_WDT_REBOOT) == 0;
217 9dd986cc Richard W.M. Jones
        d->clock_scale =
218 9dd986cc Richard W.M. Jones
            (data & ESB_WDT_FREQ) != 0 ? CLOCK_SCALE_1MHZ : CLOCK_SCALE_1KHZ;
219 9dd986cc Richard W.M. Jones
        d->int_type = (data & ESB_WDT_INTTYPE);
220 9dd986cc Richard W.M. Jones
    } else if (addr == ESB_LOCK_REG && len == 1) {
221 9dd986cc Richard W.M. Jones
        if (!d->locked) {
222 9dd986cc Richard W.M. Jones
            d->locked = (data & ESB_WDT_LOCK) != 0;
223 9dd986cc Richard W.M. Jones
            d->free_run = (data & ESB_WDT_FUNC) != 0;
224 9dd986cc Richard W.M. Jones
            old = d->enabled;
225 9dd986cc Richard W.M. Jones
            d->enabled = (data & ESB_WDT_ENABLE) != 0;
226 9dd986cc Richard W.M. Jones
            if (!old && d->enabled) /* Enabled transitioned from 0 -> 1 */
227 9dd986cc Richard W.M. Jones
                i6300esb_restart_timer(d, 1);
228 9dd986cc Richard W.M. Jones
            else if (!d->enabled)
229 9dd986cc Richard W.M. Jones
                i6300esb_disable_timer(d);
230 9dd986cc Richard W.M. Jones
        }
231 9dd986cc Richard W.M. Jones
    } else {
232 9dd986cc Richard W.M. Jones
        pci_default_write_config(dev, addr, data, len);
233 9dd986cc Richard W.M. Jones
    }
234 9dd986cc Richard W.M. Jones
}
235 9dd986cc Richard W.M. Jones
236 9dd986cc Richard W.M. Jones
static uint32_t i6300esb_config_read(PCIDevice *dev, uint32_t addr, int len)
237 9dd986cc Richard W.M. Jones
{
238 d03f09cc Markus Armbruster
    I6300State *d = DO_UPCAST(I6300State, dev, dev);
239 9dd986cc Richard W.M. Jones
    uint32_t data;
240 9dd986cc Richard W.M. Jones
241 9dd986cc Richard W.M. Jones
    i6300esb_debug ("addr = %x, len = %d\n", addr, len);
242 9dd986cc Richard W.M. Jones
243 9dd986cc Richard W.M. Jones
    if (addr == ESB_CONFIG_REG && len == 2) {
244 9dd986cc Richard W.M. Jones
        data =
245 9dd986cc Richard W.M. Jones
            (d->reboot_enabled ? 0 : ESB_WDT_REBOOT) |
246 9dd986cc Richard W.M. Jones
            (d->clock_scale == CLOCK_SCALE_1MHZ ? ESB_WDT_FREQ : 0) |
247 9dd986cc Richard W.M. Jones
            d->int_type;
248 9dd986cc Richard W.M. Jones
        return data;
249 9dd986cc Richard W.M. Jones
    } else if (addr == ESB_LOCK_REG && len == 1) {
250 9dd986cc Richard W.M. Jones
        data =
251 9dd986cc Richard W.M. Jones
            (d->free_run ? ESB_WDT_FUNC : 0) |
252 9dd986cc Richard W.M. Jones
            (d->locked ? ESB_WDT_LOCK : 0) |
253 9dd986cc Richard W.M. Jones
            (d->enabled ? ESB_WDT_ENABLE : 0);
254 9dd986cc Richard W.M. Jones
        return data;
255 9dd986cc Richard W.M. Jones
    } else {
256 9dd986cc Richard W.M. Jones
        return pci_default_read_config(dev, addr, len);
257 9dd986cc Richard W.M. Jones
    }
258 9dd986cc Richard W.M. Jones
}
259 9dd986cc Richard W.M. Jones
260 c227f099 Anthony Liguori
static uint32_t i6300esb_mem_readb(void *vp, target_phys_addr_t addr)
261 9dd986cc Richard W.M. Jones
{
262 9dd986cc Richard W.M. Jones
    i6300esb_debug ("addr = %x\n", (int) addr);
263 9dd986cc Richard W.M. Jones
264 9dd986cc Richard W.M. Jones
    return 0;
265 9dd986cc Richard W.M. Jones
}
266 9dd986cc Richard W.M. Jones
267 c227f099 Anthony Liguori
static uint32_t i6300esb_mem_readw(void *vp, target_phys_addr_t addr)
268 9dd986cc Richard W.M. Jones
{
269 9dd986cc Richard W.M. Jones
    uint32_t data = 0;
270 4f423e81 Juan Quintela
    I6300State *d = vp;
271 9dd986cc Richard W.M. Jones
272 9dd986cc Richard W.M. Jones
    i6300esb_debug("addr = %x\n", (int) addr);
273 9dd986cc Richard W.M. Jones
274 9dd986cc Richard W.M. Jones
    if (addr == 0xc) {
275 9dd986cc Richard W.M. Jones
        /* The previous reboot flag is really bit 9, but there is
276 9dd986cc Richard W.M. Jones
         * a bug in the Linux driver where it thinks it's bit 12.
277 9dd986cc Richard W.M. Jones
         * Set both.
278 9dd986cc Richard W.M. Jones
         */
279 9dd986cc Richard W.M. Jones
        data = d->previous_reboot_flag ? 0x1200 : 0;
280 9dd986cc Richard W.M. Jones
    }
281 9dd986cc Richard W.M. Jones
282 9dd986cc Richard W.M. Jones
    return data;
283 9dd986cc Richard W.M. Jones
}
284 9dd986cc Richard W.M. Jones
285 c227f099 Anthony Liguori
static uint32_t i6300esb_mem_readl(void *vp, target_phys_addr_t addr)
286 9dd986cc Richard W.M. Jones
{
287 9dd986cc Richard W.M. Jones
    i6300esb_debug("addr = %x\n", (int) addr);
288 9dd986cc Richard W.M. Jones
289 9dd986cc Richard W.M. Jones
    return 0;
290 9dd986cc Richard W.M. Jones
}
291 9dd986cc Richard W.M. Jones
292 c227f099 Anthony Liguori
static void i6300esb_mem_writeb(void *vp, target_phys_addr_t addr, uint32_t val)
293 9dd986cc Richard W.M. Jones
{
294 4f423e81 Juan Quintela
    I6300State *d = vp;
295 9dd986cc Richard W.M. Jones
296 9dd986cc Richard W.M. Jones
    i6300esb_debug("addr = %x, val = %x\n", (int) addr, val);
297 9dd986cc Richard W.M. Jones
298 9dd986cc Richard W.M. Jones
    if (addr == 0xc && val == 0x80)
299 9dd986cc Richard W.M. Jones
        d->unlock_state = 1;
300 9dd986cc Richard W.M. Jones
    else if (addr == 0xc && val == 0x86 && d->unlock_state == 1)
301 9dd986cc Richard W.M. Jones
        d->unlock_state = 2;
302 9dd986cc Richard W.M. Jones
}
303 9dd986cc Richard W.M. Jones
304 c227f099 Anthony Liguori
static void i6300esb_mem_writew(void *vp, target_phys_addr_t addr, uint32_t val)
305 9dd986cc Richard W.M. Jones
{
306 4f423e81 Juan Quintela
    I6300State *d = vp;
307 9dd986cc Richard W.M. Jones
308 9dd986cc Richard W.M. Jones
    i6300esb_debug("addr = %x, val = %x\n", (int) addr, val);
309 9dd986cc Richard W.M. Jones
310 9dd986cc Richard W.M. Jones
    if (addr == 0xc && val == 0x80)
311 9dd986cc Richard W.M. Jones
        d->unlock_state = 1;
312 9dd986cc Richard W.M. Jones
    else if (addr == 0xc && val == 0x86 && d->unlock_state == 1)
313 9dd986cc Richard W.M. Jones
        d->unlock_state = 2;
314 9dd986cc Richard W.M. Jones
    else {
315 9dd986cc Richard W.M. Jones
        if (d->unlock_state == 2) {
316 9dd986cc Richard W.M. Jones
            if (addr == 0xc) {
317 9dd986cc Richard W.M. Jones
                if ((val & 0x100) != 0)
318 9dd986cc Richard W.M. Jones
                    /* This is the "ping" from the userspace watchdog in
319 9dd986cc Richard W.M. Jones
                     * the guest ...
320 9dd986cc Richard W.M. Jones
                     */
321 9dd986cc Richard W.M. Jones
                    i6300esb_restart_timer(d, 1);
322 9dd986cc Richard W.M. Jones
323 9dd986cc Richard W.M. Jones
                /* Setting bit 9 resets the previous reboot flag.
324 9dd986cc Richard W.M. Jones
                 * There's a bug in the Linux driver where it sets
325 9dd986cc Richard W.M. Jones
                 * bit 12 instead.
326 9dd986cc Richard W.M. Jones
                 */
327 9dd986cc Richard W.M. Jones
                if ((val & 0x200) != 0 || (val & 0x1000) != 0) {
328 9dd986cc Richard W.M. Jones
                    d->previous_reboot_flag = 0;
329 9dd986cc Richard W.M. Jones
                }
330 9dd986cc Richard W.M. Jones
            }
331 9dd986cc Richard W.M. Jones
332 9dd986cc Richard W.M. Jones
            d->unlock_state = 0;
333 9dd986cc Richard W.M. Jones
        }
334 9dd986cc Richard W.M. Jones
    }
335 9dd986cc Richard W.M. Jones
}
336 9dd986cc Richard W.M. Jones
337 c227f099 Anthony Liguori
static void i6300esb_mem_writel(void *vp, target_phys_addr_t addr, uint32_t val)
338 9dd986cc Richard W.M. Jones
{
339 4f423e81 Juan Quintela
    I6300State *d = vp;
340 9dd986cc Richard W.M. Jones
341 9dd986cc Richard W.M. Jones
    i6300esb_debug ("addr = %x, val = %x\n", (int) addr, val);
342 9dd986cc Richard W.M. Jones
343 9dd986cc Richard W.M. Jones
    if (addr == 0xc && val == 0x80)
344 9dd986cc Richard W.M. Jones
        d->unlock_state = 1;
345 9dd986cc Richard W.M. Jones
    else if (addr == 0xc && val == 0x86 && d->unlock_state == 1)
346 9dd986cc Richard W.M. Jones
        d->unlock_state = 2;
347 9dd986cc Richard W.M. Jones
    else {
348 9dd986cc Richard W.M. Jones
        if (d->unlock_state == 2) {
349 9dd986cc Richard W.M. Jones
            if (addr == 0)
350 9dd986cc Richard W.M. Jones
                d->timer1_preload = val & 0xfffff;
351 9dd986cc Richard W.M. Jones
            else if (addr == 4)
352 9dd986cc Richard W.M. Jones
                d->timer2_preload = val & 0xfffff;
353 9dd986cc Richard W.M. Jones
354 9dd986cc Richard W.M. Jones
            d->unlock_state = 0;
355 9dd986cc Richard W.M. Jones
        }
356 9dd986cc Richard W.M. Jones
    }
357 9dd986cc Richard W.M. Jones
}
358 9dd986cc Richard W.M. Jones
359 d9c6ebd1 Avi Kivity
static const MemoryRegionOps i6300esb_ops = {
360 d9c6ebd1 Avi Kivity
    .old_mmio = {
361 d9c6ebd1 Avi Kivity
        .read = {
362 d9c6ebd1 Avi Kivity
            i6300esb_mem_readb,
363 d9c6ebd1 Avi Kivity
            i6300esb_mem_readw,
364 d9c6ebd1 Avi Kivity
            i6300esb_mem_readl,
365 d9c6ebd1 Avi Kivity
        },
366 d9c6ebd1 Avi Kivity
        .write = {
367 d9c6ebd1 Avi Kivity
            i6300esb_mem_writeb,
368 d9c6ebd1 Avi Kivity
            i6300esb_mem_writew,
369 d9c6ebd1 Avi Kivity
            i6300esb_mem_writel,
370 d9c6ebd1 Avi Kivity
        },
371 d9c6ebd1 Avi Kivity
    },
372 d9c6ebd1 Avi Kivity
    .endianness = DEVICE_NATIVE_ENDIAN,
373 d9c6ebd1 Avi Kivity
};
374 d9c6ebd1 Avi Kivity
375 95c90a0e Juan Quintela
static const VMStateDescription vmstate_i6300esb = {
376 95c90a0e Juan Quintela
    .name = "i6300esb_wdt",
377 95c90a0e Juan Quintela
    .version_id = sizeof(I6300State),
378 95c90a0e Juan Quintela
    .minimum_version_id = sizeof(I6300State),
379 95c90a0e Juan Quintela
    .minimum_version_id_old = sizeof(I6300State),
380 95c90a0e Juan Quintela
    .fields      = (VMStateField []) {
381 95c90a0e Juan Quintela
        VMSTATE_PCI_DEVICE(dev, I6300State),
382 95c90a0e Juan Quintela
        VMSTATE_INT32(reboot_enabled, I6300State),
383 95c90a0e Juan Quintela
        VMSTATE_INT32(clock_scale, I6300State),
384 95c90a0e Juan Quintela
        VMSTATE_INT32(int_type, I6300State),
385 95c90a0e Juan Quintela
        VMSTATE_INT32(free_run, I6300State),
386 95c90a0e Juan Quintela
        VMSTATE_INT32(locked, I6300State),
387 95c90a0e Juan Quintela
        VMSTATE_INT32(enabled, I6300State),
388 95c90a0e Juan Quintela
        VMSTATE_TIMER(timer, I6300State),
389 95c90a0e Juan Quintela
        VMSTATE_UINT32(timer1_preload, I6300State),
390 95c90a0e Juan Quintela
        VMSTATE_UINT32(timer2_preload, I6300State),
391 95c90a0e Juan Quintela
        VMSTATE_INT32(stage, I6300State),
392 95c90a0e Juan Quintela
        VMSTATE_INT32(unlock_state, I6300State),
393 95c90a0e Juan Quintela
        VMSTATE_INT32(previous_reboot_flag, I6300State),
394 95c90a0e Juan Quintela
        VMSTATE_END_OF_LIST()
395 95c90a0e Juan Quintela
    }
396 95c90a0e Juan Quintela
};
397 9dd986cc Richard W.M. Jones
398 81a322d4 Gerd Hoffmann
static int i6300esb_init(PCIDevice *dev)
399 9dd986cc Richard W.M. Jones
{
400 d03f09cc Markus Armbruster
    I6300State *d = DO_UPCAST(I6300State, dev, dev);
401 9dd986cc Richard W.M. Jones
402 fa82e9c3 Bernhard Kohl
    i6300esb_debug("I6300State = %p\n", d);
403 fa82e9c3 Bernhard Kohl
404 74475455 Paolo Bonzini
    d->timer = qemu_new_timer_ns(vm_clock, i6300esb_timer_expired, d);
405 36888c63 Richard W.M. Jones
    d->previous_reboot_flag = 0;
406 9dd986cc Richard W.M. Jones
407 d9c6ebd1 Avi Kivity
    memory_region_init_io(&d->io_mem, &i6300esb_ops, d, "i6300esb", 0x10);
408 e824b2cc Avi Kivity
    pci_register_bar(&d->dev, 0, 0, &d->io_mem);
409 22f3647b Avi Kivity
    /* qemu_register_coalesced_mmio (addr, 0x10); ? */
410 9dd986cc Richard W.M. Jones
411 81a322d4 Gerd Hoffmann
    return 0;
412 9dd986cc Richard W.M. Jones
}
413 9dd986cc Richard W.M. Jones
414 d9c6ebd1 Avi Kivity
static int i6300esb_exit(PCIDevice *dev)
415 d9c6ebd1 Avi Kivity
{
416 d9c6ebd1 Avi Kivity
    I6300State *d = DO_UPCAST(I6300State, dev, dev);
417 d9c6ebd1 Avi Kivity
418 d9c6ebd1 Avi Kivity
    memory_region_destroy(&d->io_mem);
419 d9c6ebd1 Avi Kivity
420 d9c6ebd1 Avi Kivity
    return 0;
421 d9c6ebd1 Avi Kivity
}
422 d9c6ebd1 Avi Kivity
423 9dd986cc Richard W.M. Jones
static WatchdogTimerModel model = {
424 9dd986cc Richard W.M. Jones
    .wdt_name = "i6300esb",
425 9dd986cc Richard W.M. Jones
    .wdt_description = "Intel 6300ESB",
426 9dd986cc Richard W.M. Jones
};
427 9dd986cc Richard W.M. Jones
428 40021f08 Anthony Liguori
static void i6300esb_class_init(ObjectClass *klass, void *data)
429 40021f08 Anthony Liguori
{
430 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
431 40021f08 Anthony Liguori
    PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
432 40021f08 Anthony Liguori
433 40021f08 Anthony Liguori
    k->config_read = i6300esb_config_read;
434 40021f08 Anthony Liguori
    k->config_write = i6300esb_config_write;
435 40021f08 Anthony Liguori
    k->init = i6300esb_init;
436 40021f08 Anthony Liguori
    k->exit = i6300esb_exit;
437 40021f08 Anthony Liguori
    k->vendor_id = PCI_VENDOR_ID_INTEL;
438 40021f08 Anthony Liguori
    k->device_id = PCI_DEVICE_ID_INTEL_ESB_9;
439 40021f08 Anthony Liguori
    k->class_id = PCI_CLASS_SYSTEM_OTHER;
440 39bffca2 Anthony Liguori
    dc->reset = i6300esb_reset;
441 39bffca2 Anthony Liguori
    dc->vmsd = &vmstate_i6300esb;
442 40021f08 Anthony Liguori
}
443 40021f08 Anthony Liguori
444 39bffca2 Anthony Liguori
static TypeInfo i6300esb_info = {
445 39bffca2 Anthony Liguori
    .name          = "i6300esb",
446 39bffca2 Anthony Liguori
    .parent        = TYPE_PCI_DEVICE,
447 39bffca2 Anthony Liguori
    .instance_size = sizeof(I6300State),
448 39bffca2 Anthony Liguori
    .class_init    = i6300esb_class_init,
449 09aaa160 Markus Armbruster
};
450 09aaa160 Markus Armbruster
451 83f7d43a Andreas Färber
static void i6300esb_register_types(void)
452 9dd986cc Richard W.M. Jones
{
453 9dd986cc Richard W.M. Jones
    watchdog_add_model(&model);
454 39bffca2 Anthony Liguori
    type_register_static(&i6300esb_info);
455 9dd986cc Richard W.M. Jones
}
456 09aaa160 Markus Armbruster
457 83f7d43a Andreas Färber
type_init(i6300esb_register_types)