Statistics
| Branch: | Revision:

root / hw / slavio_timer.c @ 7f132a21

History | View | Annotate | Download (14.1 kB)

1 e80cfcfc bellard
/*
2 e80cfcfc bellard
 * QEMU Sparc SLAVIO timer controller emulation
3 e80cfcfc bellard
 *
4 66321a11 bellard
 * Copyright (c) 2003-2005 Fabrice Bellard
5 5fafdf24 ths
 *
6 e80cfcfc bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 e80cfcfc bellard
 * of this software and associated documentation files (the "Software"), to deal
8 e80cfcfc bellard
 * in the Software without restriction, including without limitation the rights
9 e80cfcfc bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 e80cfcfc bellard
 * copies of the Software, and to permit persons to whom the Software is
11 e80cfcfc bellard
 * furnished to do so, subject to the following conditions:
12 e80cfcfc bellard
 *
13 e80cfcfc bellard
 * The above copyright notice and this permission notice shall be included in
14 e80cfcfc bellard
 * all copies or substantial portions of the Software.
15 e80cfcfc bellard
 *
16 e80cfcfc bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 e80cfcfc bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 e80cfcfc bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 e80cfcfc bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 e80cfcfc bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 e80cfcfc bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 e80cfcfc bellard
 * THE SOFTWARE.
23 e80cfcfc bellard
 */
24 c70c59ee Blue Swirl
25 87ecb68b pbrook
#include "sun4m.h"
26 87ecb68b pbrook
#include "qemu-timer.h"
27 c70c59ee Blue Swirl
#include "sysbus.h"
28 e80cfcfc bellard
29 e80cfcfc bellard
//#define DEBUG_TIMER
30 e80cfcfc bellard
31 66321a11 bellard
#ifdef DEBUG_TIMER
32 001faf32 Blue Swirl
#define DPRINTF(fmt, ...)                                       \
33 001faf32 Blue Swirl
    do { printf("TIMER: " fmt , ## __VA_ARGS__); } while (0)
34 66321a11 bellard
#else
35 001faf32 Blue Swirl
#define DPRINTF(fmt, ...) do {} while (0)
36 66321a11 bellard
#endif
37 66321a11 bellard
38 e80cfcfc bellard
/*
39 e80cfcfc bellard
 * Registers of hardware timer in sun4m.
40 e80cfcfc bellard
 *
41 e80cfcfc bellard
 * This is the timer/counter part of chip STP2001 (Slave I/O), also
42 e80cfcfc bellard
 * produced as NCR89C105. See
43 e80cfcfc bellard
 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
44 5fafdf24 ths
 *
45 e80cfcfc bellard
 * The 31-bit counter is incremented every 500ns by bit 9. Bits 8..0
46 e80cfcfc bellard
 * are zero. Bit 31 is 1 when count has been reached.
47 e80cfcfc bellard
 *
48 ba3c64fb bellard
 * Per-CPU timers interrupt local CPU, system timer uses normal
49 ba3c64fb bellard
 * interrupt routing.
50 ba3c64fb bellard
 *
51 e80cfcfc bellard
 */
52 e80cfcfc bellard
53 81732d19 blueswir1
#define MAX_CPUS 16
54 81732d19 blueswir1
55 7204ff9c Blue Swirl
typedef struct CPUTimerState {
56 d7edfd27 blueswir1
    qemu_irq irq;
57 8d05ea8a blueswir1
    ptimer_state *timer;
58 8d05ea8a blueswir1
    uint32_t count, counthigh, reached;
59 8d05ea8a blueswir1
    uint64_t limit;
60 115646b6 blueswir1
    // processor only
61 22548760 blueswir1
    uint32_t running;
62 7204ff9c Blue Swirl
} CPUTimerState;
63 7204ff9c Blue Swirl
64 7204ff9c Blue Swirl
typedef struct SLAVIO_TIMERState {
65 7204ff9c Blue Swirl
    SysBusDevice busdev;
66 7204ff9c Blue Swirl
    uint32_t num_cpus;
67 7204ff9c Blue Swirl
    CPUTimerState cputimer[MAX_CPUS + 1];
68 7204ff9c Blue Swirl
    uint32_t cputimer_mode;
69 e80cfcfc bellard
} SLAVIO_TIMERState;
70 e80cfcfc bellard
71 7204ff9c Blue Swirl
typedef struct TimerContext {
72 7204ff9c Blue Swirl
    SLAVIO_TIMERState *s;
73 7204ff9c Blue Swirl
    unsigned int timer_index; /* 0 for system, 1 ... MAX_CPUS for CPU timers */
74 7204ff9c Blue Swirl
} TimerContext;
75 7204ff9c Blue Swirl
76 115646b6 blueswir1
#define SYS_TIMER_SIZE 0x14
77 81732d19 blueswir1
#define CPU_TIMER_SIZE 0x10
78 e80cfcfc bellard
79 d2c38b24 blueswir1
#define TIMER_LIMIT         0
80 d2c38b24 blueswir1
#define TIMER_COUNTER       1
81 d2c38b24 blueswir1
#define TIMER_COUNTER_NORST 2
82 d2c38b24 blueswir1
#define TIMER_STATUS        3
83 d2c38b24 blueswir1
#define TIMER_MODE          4
84 d2c38b24 blueswir1
85 d2c38b24 blueswir1
#define TIMER_COUNT_MASK32 0xfffffe00
86 d2c38b24 blueswir1
#define TIMER_LIMIT_MASK32 0x7fffffff
87 d2c38b24 blueswir1
#define TIMER_MAX_COUNT64  0x7ffffffffffffe00ULL
88 d2c38b24 blueswir1
#define TIMER_MAX_COUNT32  0x7ffffe00ULL
89 d2c38b24 blueswir1
#define TIMER_REACHED      0x80000000
90 d2c38b24 blueswir1
#define TIMER_PERIOD       500ULL // 500ns
91 68fb89a2 Blue Swirl
#define LIMIT_TO_PERIODS(l) (((l) >> 9) - 1)
92 68fb89a2 Blue Swirl
#define PERIODS_TO_LIMIT(l) (((l) + 1) << 9)
93 d2c38b24 blueswir1
94 7204ff9c Blue Swirl
static int slavio_timer_is_user(TimerContext *tc)
95 115646b6 blueswir1
{
96 7204ff9c Blue Swirl
    SLAVIO_TIMERState *s = tc->s;
97 7204ff9c Blue Swirl
    unsigned int timer_index = tc->timer_index;
98 7204ff9c Blue Swirl
99 7204ff9c Blue Swirl
    return timer_index != 0 && (s->cputimer_mode & (1 << (timer_index - 1)));
100 115646b6 blueswir1
}
101 115646b6 blueswir1
102 e80cfcfc bellard
// Update count, set irq, update expire_time
103 8d05ea8a blueswir1
// Convert from ptimer countdown units
104 7204ff9c Blue Swirl
static void slavio_timer_get_out(CPUTimerState *t)
105 e80cfcfc bellard
{
106 bd7e2875 blueswir1
    uint64_t count, limit;
107 e80cfcfc bellard
108 7204ff9c Blue Swirl
    if (t->limit == 0) { /* free-run system or processor counter */
109 bd7e2875 blueswir1
        limit = TIMER_MAX_COUNT32;
110 7204ff9c Blue Swirl
    } else {
111 7204ff9c Blue Swirl
        limit = t->limit;
112 7204ff9c Blue Swirl
    }
113 9ebec28b Blue Swirl
    count = limit - PERIODS_TO_LIMIT(ptimer_get_count(t->timer));
114 9ebec28b Blue Swirl
115 7204ff9c Blue Swirl
    DPRINTF("get_out: limit %" PRIx64 " count %x%08x\n", t->limit, t->counthigh,
116 7204ff9c Blue Swirl
            t->count);
117 7204ff9c Blue Swirl
    t->count = count & TIMER_COUNT_MASK32;
118 7204ff9c Blue Swirl
    t->counthigh = count >> 32;
119 e80cfcfc bellard
}
120 e80cfcfc bellard
121 e80cfcfc bellard
// timer callback
122 e80cfcfc bellard
static void slavio_timer_irq(void *opaque)
123 e80cfcfc bellard
{
124 7204ff9c Blue Swirl
    TimerContext *tc = opaque;
125 7204ff9c Blue Swirl
    SLAVIO_TIMERState *s = tc->s;
126 7204ff9c Blue Swirl
    CPUTimerState *t = &s->cputimer[tc->timer_index];
127 7204ff9c Blue Swirl
128 7204ff9c Blue Swirl
    slavio_timer_get_out(t);
129 7204ff9c Blue Swirl
    DPRINTF("callback: count %x%08x\n", t->counthigh, t->count);
130 68fb89a2 Blue Swirl
    /* if limit is 0 (free-run), there will be no match */
131 68fb89a2 Blue Swirl
    if (t->limit != 0) {
132 68fb89a2 Blue Swirl
        t->reached = TIMER_REACHED;
133 68fb89a2 Blue Swirl
    }
134 452efba6 Blue Swirl
    /* there is no interrupt if user timer or free-run */
135 452efba6 Blue Swirl
    if (!slavio_timer_is_user(tc) && t->limit != 0) {
136 7204ff9c Blue Swirl
        qemu_irq_raise(t->irq);
137 7204ff9c Blue Swirl
    }
138 e80cfcfc bellard
}
139 e80cfcfc bellard
140 c227f099 Anthony Liguori
static uint32_t slavio_timer_mem_readl(void *opaque, target_phys_addr_t addr)
141 e80cfcfc bellard
{
142 7204ff9c Blue Swirl
    TimerContext *tc = opaque;
143 7204ff9c Blue Swirl
    SLAVIO_TIMERState *s = tc->s;
144 8d05ea8a blueswir1
    uint32_t saddr, ret;
145 7204ff9c Blue Swirl
    unsigned int timer_index = tc->timer_index;
146 7204ff9c Blue Swirl
    CPUTimerState *t = &s->cputimer[timer_index];
147 e80cfcfc bellard
148 e64d7d59 blueswir1
    saddr = addr >> 2;
149 e80cfcfc bellard
    switch (saddr) {
150 d2c38b24 blueswir1
    case TIMER_LIMIT:
151 f930d07e blueswir1
        // read limit (system counter mode) or read most signifying
152 f930d07e blueswir1
        // part of counter (user mode)
153 7204ff9c Blue Swirl
        if (slavio_timer_is_user(tc)) {
154 115646b6 blueswir1
            // read user timer MSW
155 7204ff9c Blue Swirl
            slavio_timer_get_out(t);
156 7204ff9c Blue Swirl
            ret = t->counthigh | t->reached;
157 115646b6 blueswir1
        } else {
158 115646b6 blueswir1
            // read limit
159 f930d07e blueswir1
            // clear irq
160 7204ff9c Blue Swirl
            qemu_irq_lower(t->irq);
161 7204ff9c Blue Swirl
            t->reached = 0;
162 7204ff9c Blue Swirl
            ret = t->limit & TIMER_LIMIT_MASK32;
163 f930d07e blueswir1
        }
164 8d05ea8a blueswir1
        break;
165 d2c38b24 blueswir1
    case TIMER_COUNTER:
166 f930d07e blueswir1
        // read counter and reached bit (system mode) or read lsbits
167 f930d07e blueswir1
        // of counter (user mode)
168 7204ff9c Blue Swirl
        slavio_timer_get_out(t);
169 7204ff9c Blue Swirl
        if (slavio_timer_is_user(tc)) { // read user timer LSW
170 7204ff9c Blue Swirl
            ret = t->count & TIMER_MAX_COUNT64;
171 7204ff9c Blue Swirl
        } else { // read limit
172 7204ff9c Blue Swirl
            ret = (t->count & TIMER_MAX_COUNT32) |
173 7204ff9c Blue Swirl
                t->reached;
174 7204ff9c Blue Swirl
        }
175 8d05ea8a blueswir1
        break;
176 d2c38b24 blueswir1
    case TIMER_STATUS:
177 115646b6 blueswir1
        // only available in processor counter/timer
178 f930d07e blueswir1
        // read start/stop status
179 7204ff9c Blue Swirl
        if (timer_index > 0) {
180 7204ff9c Blue Swirl
            ret = t->running;
181 7204ff9c Blue Swirl
        } else {
182 7204ff9c Blue Swirl
            ret = 0;
183 7204ff9c Blue Swirl
        }
184 8d05ea8a blueswir1
        break;
185 d2c38b24 blueswir1
    case TIMER_MODE:
186 115646b6 blueswir1
        // only available in system counter
187 f930d07e blueswir1
        // read user/system mode
188 7204ff9c Blue Swirl
        ret = s->cputimer_mode;
189 8d05ea8a blueswir1
        break;
190 e80cfcfc bellard
    default:
191 115646b6 blueswir1
        DPRINTF("invalid read address " TARGET_FMT_plx "\n", addr);
192 8d05ea8a blueswir1
        ret = 0;
193 8d05ea8a blueswir1
        break;
194 e80cfcfc bellard
    }
195 8d05ea8a blueswir1
    DPRINTF("read " TARGET_FMT_plx " = %08x\n", addr, ret);
196 8d05ea8a blueswir1
197 8d05ea8a blueswir1
    return ret;
198 e80cfcfc bellard
}
199 e80cfcfc bellard
200 c227f099 Anthony Liguori
static void slavio_timer_mem_writel(void *opaque, target_phys_addr_t addr,
201 d2c38b24 blueswir1
                                    uint32_t val)
202 e80cfcfc bellard
{
203 7204ff9c Blue Swirl
    TimerContext *tc = opaque;
204 7204ff9c Blue Swirl
    SLAVIO_TIMERState *s = tc->s;
205 e80cfcfc bellard
    uint32_t saddr;
206 7204ff9c Blue Swirl
    unsigned int timer_index = tc->timer_index;
207 7204ff9c Blue Swirl
    CPUTimerState *t = &s->cputimer[timer_index];
208 e80cfcfc bellard
209 8d05ea8a blueswir1
    DPRINTF("write " TARGET_FMT_plx " %08x\n", addr, val);
210 e64d7d59 blueswir1
    saddr = addr >> 2;
211 e80cfcfc bellard
    switch (saddr) {
212 d2c38b24 blueswir1
    case TIMER_LIMIT:
213 7204ff9c Blue Swirl
        if (slavio_timer_is_user(tc)) {
214 e1cb9502 blueswir1
            uint64_t count;
215 e1cb9502 blueswir1
216 115646b6 blueswir1
            // set user counter MSW, reset counter
217 7204ff9c Blue Swirl
            t->limit = TIMER_MAX_COUNT64;
218 7204ff9c Blue Swirl
            t->counthigh = val & (TIMER_MAX_COUNT64 >> 32);
219 7204ff9c Blue Swirl
            t->reached = 0;
220 7204ff9c Blue Swirl
            count = ((uint64_t)t->counthigh << 32) | t->count;
221 0bf9e31a Blue Swirl
            DPRINTF("processor %d user timer set to %016" PRIx64 "\n",
222 7204ff9c Blue Swirl
                    timer_index, count);
223 9ebec28b Blue Swirl
            ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
224 115646b6 blueswir1
        } else {
225 115646b6 blueswir1
            // set limit, reset counter
226 7204ff9c Blue Swirl
            qemu_irq_lower(t->irq);
227 7204ff9c Blue Swirl
            t->limit = val & TIMER_MAX_COUNT32;
228 7204ff9c Blue Swirl
            if (t->timer) {
229 7204ff9c Blue Swirl
                if (t->limit == 0) { /* free-run */
230 7204ff9c Blue Swirl
                    ptimer_set_limit(t->timer,
231 77f193da blueswir1
                                     LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
232 7204ff9c Blue Swirl
                } else {
233 7204ff9c Blue Swirl
                    ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 1);
234 7204ff9c Blue Swirl
                }
235 85e3023e blueswir1
            }
236 81732d19 blueswir1
        }
237 115646b6 blueswir1
        break;
238 d2c38b24 blueswir1
    case TIMER_COUNTER:
239 7204ff9c Blue Swirl
        if (slavio_timer_is_user(tc)) {
240 e1cb9502 blueswir1
            uint64_t count;
241 e1cb9502 blueswir1
242 115646b6 blueswir1
            // set user counter LSW, reset counter
243 7204ff9c Blue Swirl
            t->limit = TIMER_MAX_COUNT64;
244 7204ff9c Blue Swirl
            t->count = val & TIMER_MAX_COUNT64;
245 7204ff9c Blue Swirl
            t->reached = 0;
246 7204ff9c Blue Swirl
            count = ((uint64_t)t->counthigh) << 32 | t->count;
247 0bf9e31a Blue Swirl
            DPRINTF("processor %d user timer set to %016" PRIx64 "\n",
248 7204ff9c Blue Swirl
                    timer_index, count);
249 9ebec28b Blue Swirl
            ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
250 115646b6 blueswir1
        } else
251 115646b6 blueswir1
            DPRINTF("not user timer\n");
252 115646b6 blueswir1
        break;
253 d2c38b24 blueswir1
    case TIMER_COUNTER_NORST:
254 f930d07e blueswir1
        // set limit without resetting counter
255 7204ff9c Blue Swirl
        t->limit = val & TIMER_MAX_COUNT32;
256 9ebec28b Blue Swirl
        if (t->limit == 0) { /* free-run */
257 9ebec28b Blue Swirl
            ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 0);
258 9ebec28b Blue Swirl
        } else {
259 9ebec28b Blue Swirl
            ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 0);
260 85e3023e blueswir1
        }
261 f930d07e blueswir1
        break;
262 d2c38b24 blueswir1
    case TIMER_STATUS:
263 7204ff9c Blue Swirl
        if (slavio_timer_is_user(tc)) {
264 115646b6 blueswir1
            // start/stop user counter
265 7204ff9c Blue Swirl
            if ((val & 1) && !t->running) {
266 7204ff9c Blue Swirl
                DPRINTF("processor %d user timer started\n",
267 7204ff9c Blue Swirl
                        timer_index);
268 9ebec28b Blue Swirl
                ptimer_run(t->timer, 0);
269 7204ff9c Blue Swirl
                t->running = 1;
270 7204ff9c Blue Swirl
            } else if (!(val & 1) && t->running) {
271 7204ff9c Blue Swirl
                DPRINTF("processor %d user timer stopped\n",
272 7204ff9c Blue Swirl
                        timer_index);
273 9ebec28b Blue Swirl
                ptimer_stop(t->timer);
274 7204ff9c Blue Swirl
                t->running = 0;
275 f930d07e blueswir1
            }
276 f930d07e blueswir1
        }
277 f930d07e blueswir1
        break;
278 d2c38b24 blueswir1
    case TIMER_MODE:
279 7204ff9c Blue Swirl
        if (timer_index == 0) {
280 81732d19 blueswir1
            unsigned int i;
281 81732d19 blueswir1
282 7204ff9c Blue Swirl
            for (i = 0; i < s->num_cpus; i++) {
283 67e42751 blueswir1
                unsigned int processor = 1 << i;
284 7204ff9c Blue Swirl
                CPUTimerState *curr_timer = &s->cputimer[i + 1];
285 67e42751 blueswir1
286 67e42751 blueswir1
                // check for a change in timer mode for this processor
287 7204ff9c Blue Swirl
                if ((val & processor) != (s->cputimer_mode & processor)) {
288 67e42751 blueswir1
                    if (val & processor) { // counter -> user timer
289 7204ff9c Blue Swirl
                        qemu_irq_lower(curr_timer->irq);
290 67e42751 blueswir1
                        // counters are always running
291 7204ff9c Blue Swirl
                        ptimer_stop(curr_timer->timer);
292 7204ff9c Blue Swirl
                        curr_timer->running = 0;
293 67e42751 blueswir1
                        // user timer limit is always the same
294 7204ff9c Blue Swirl
                        curr_timer->limit = TIMER_MAX_COUNT64;
295 7204ff9c Blue Swirl
                        ptimer_set_limit(curr_timer->timer,
296 7204ff9c Blue Swirl
                                         LIMIT_TO_PERIODS(curr_timer->limit),
297 77f193da blueswir1
                                         1);
298 67e42751 blueswir1
                        // set this processors user timer bit in config
299 67e42751 blueswir1
                        // register
300 7204ff9c Blue Swirl
                        s->cputimer_mode |= processor;
301 67e42751 blueswir1
                        DPRINTF("processor %d changed from counter to user "
302 7204ff9c Blue Swirl
                                "timer\n", timer_index);
303 67e42751 blueswir1
                    } else { // user timer -> counter
304 67e42751 blueswir1
                        // stop the user timer if it is running
305 7204ff9c Blue Swirl
                        if (curr_timer->running) {
306 7204ff9c Blue Swirl
                            ptimer_stop(curr_timer->timer);
307 7204ff9c Blue Swirl
                        }
308 67e42751 blueswir1
                        // start the counter
309 7204ff9c Blue Swirl
                        ptimer_run(curr_timer->timer, 0);
310 7204ff9c Blue Swirl
                        curr_timer->running = 1;
311 67e42751 blueswir1
                        // clear this processors user timer bit in config
312 67e42751 blueswir1
                        // register
313 7204ff9c Blue Swirl
                        s->cputimer_mode &= ~processor;
314 67e42751 blueswir1
                        DPRINTF("processor %d changed from user timer to "
315 7204ff9c Blue Swirl
                                "counter\n", timer_index);
316 67e42751 blueswir1
                    }
317 115646b6 blueswir1
                }
318 81732d19 blueswir1
            }
319 7204ff9c Blue Swirl
        } else {
320 115646b6 blueswir1
            DPRINTF("not system timer\n");
321 7204ff9c Blue Swirl
        }
322 f930d07e blueswir1
        break;
323 e80cfcfc bellard
    default:
324 115646b6 blueswir1
        DPRINTF("invalid write address " TARGET_FMT_plx "\n", addr);
325 f930d07e blueswir1
        break;
326 e80cfcfc bellard
    }
327 e80cfcfc bellard
}
328 e80cfcfc bellard
329 d60efc6b Blue Swirl
static CPUReadMemoryFunc * const slavio_timer_mem_read[3] = {
330 7c560456 blueswir1
    NULL,
331 7c560456 blueswir1
    NULL,
332 e80cfcfc bellard
    slavio_timer_mem_readl,
333 e80cfcfc bellard
};
334 e80cfcfc bellard
335 d60efc6b Blue Swirl
static CPUWriteMemoryFunc * const slavio_timer_mem_write[3] = {
336 7c560456 blueswir1
    NULL,
337 7c560456 blueswir1
    NULL,
338 e80cfcfc bellard
    slavio_timer_mem_writel,
339 e80cfcfc bellard
};
340 e80cfcfc bellard
341 f4b19cd0 Blue Swirl
static const VMStateDescription vmstate_timer = {
342 f4b19cd0 Blue Swirl
    .name ="timer",
343 f4b19cd0 Blue Swirl
    .version_id = 3,
344 f4b19cd0 Blue Swirl
    .minimum_version_id = 3,
345 f4b19cd0 Blue Swirl
    .minimum_version_id_old = 3,
346 f4b19cd0 Blue Swirl
    .fields      = (VMStateField []) {
347 f4b19cd0 Blue Swirl
        VMSTATE_UINT64(limit, CPUTimerState),
348 f4b19cd0 Blue Swirl
        VMSTATE_UINT32(count, CPUTimerState),
349 f4b19cd0 Blue Swirl
        VMSTATE_UINT32(counthigh, CPUTimerState),
350 f4b19cd0 Blue Swirl
        VMSTATE_UINT32(reached, CPUTimerState),
351 f4b19cd0 Blue Swirl
        VMSTATE_UINT32(running, CPUTimerState),
352 f4b19cd0 Blue Swirl
        VMSTATE_PTIMER(timer, CPUTimerState),
353 f4b19cd0 Blue Swirl
        VMSTATE_END_OF_LIST()
354 7204ff9c Blue Swirl
    }
355 f4b19cd0 Blue Swirl
};
356 e80cfcfc bellard
357 f4b19cd0 Blue Swirl
static const VMStateDescription vmstate_slavio_timer = {
358 f4b19cd0 Blue Swirl
    .name ="slavio_timer",
359 f4b19cd0 Blue Swirl
    .version_id = 3,
360 f4b19cd0 Blue Swirl
    .minimum_version_id = 3,
361 f4b19cd0 Blue Swirl
    .minimum_version_id_old = 3,
362 f4b19cd0 Blue Swirl
    .fields      = (VMStateField []) {
363 f4b19cd0 Blue Swirl
        VMSTATE_STRUCT_ARRAY(cputimer, SLAVIO_TIMERState, MAX_CPUS + 1, 3,
364 f4b19cd0 Blue Swirl
                             vmstate_timer, CPUTimerState),
365 f4b19cd0 Blue Swirl
        VMSTATE_END_OF_LIST()
366 7204ff9c Blue Swirl
    }
367 f4b19cd0 Blue Swirl
};
368 e80cfcfc bellard
369 0e0bfeea Blue Swirl
static void slavio_timer_reset(DeviceState *d)
370 e80cfcfc bellard
{
371 0e0bfeea Blue Swirl
    SLAVIO_TIMERState *s = container_of(d, SLAVIO_TIMERState, busdev.qdev);
372 7204ff9c Blue Swirl
    unsigned int i;
373 7204ff9c Blue Swirl
    CPUTimerState *curr_timer;
374 7204ff9c Blue Swirl
375 7204ff9c Blue Swirl
    for (i = 0; i <= MAX_CPUS; i++) {
376 7204ff9c Blue Swirl
        curr_timer = &s->cputimer[i];
377 7204ff9c Blue Swirl
        curr_timer->limit = 0;
378 7204ff9c Blue Swirl
        curr_timer->count = 0;
379 7204ff9c Blue Swirl
        curr_timer->reached = 0;
380 7204ff9c Blue Swirl
        if (i < s->num_cpus) {
381 7204ff9c Blue Swirl
            ptimer_set_limit(curr_timer->timer,
382 7204ff9c Blue Swirl
                             LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
383 7204ff9c Blue Swirl
            ptimer_run(curr_timer->timer, 0);
384 7204ff9c Blue Swirl
        }
385 7204ff9c Blue Swirl
        curr_timer->running = 1;
386 85e3023e blueswir1
    }
387 7204ff9c Blue Swirl
    s->cputimer_mode = 0;
388 e80cfcfc bellard
}
389 e80cfcfc bellard
390 81a322d4 Gerd Hoffmann
static int slavio_timer_init1(SysBusDevice *dev)
391 c70c59ee Blue Swirl
{
392 c70c59ee Blue Swirl
    int io;
393 c70c59ee Blue Swirl
    SLAVIO_TIMERState *s = FROM_SYSBUS(SLAVIO_TIMERState, dev);
394 8d05ea8a blueswir1
    QEMUBH *bh;
395 7204ff9c Blue Swirl
    unsigned int i;
396 7204ff9c Blue Swirl
    TimerContext *tc;
397 e80cfcfc bellard
398 7204ff9c Blue Swirl
    for (i = 0; i <= MAX_CPUS; i++) {
399 7204ff9c Blue Swirl
        tc = qemu_mallocz(sizeof(TimerContext));
400 7204ff9c Blue Swirl
        tc->s = s;
401 7204ff9c Blue Swirl
        tc->timer_index = i;
402 c70c59ee Blue Swirl
403 7204ff9c Blue Swirl
        bh = qemu_bh_new(slavio_timer_irq, tc);
404 7204ff9c Blue Swirl
        s->cputimer[i].timer = ptimer_init(bh);
405 7204ff9c Blue Swirl
        ptimer_set_period(s->cputimer[i].timer, TIMER_PERIOD);
406 e80cfcfc bellard
407 7204ff9c Blue Swirl
        io = cpu_register_io_memory(slavio_timer_mem_read,
408 7204ff9c Blue Swirl
                                    slavio_timer_mem_write, tc);
409 7204ff9c Blue Swirl
        if (i == 0) {
410 7204ff9c Blue Swirl
            sysbus_init_mmio(dev, SYS_TIMER_SIZE, io);
411 7204ff9c Blue Swirl
        } else {
412 7204ff9c Blue Swirl
            sysbus_init_mmio(dev, CPU_TIMER_SIZE, io);
413 7204ff9c Blue Swirl
        }
414 7204ff9c Blue Swirl
415 7204ff9c Blue Swirl
        sysbus_init_irq(dev, &s->cputimer[i].irq);
416 c70c59ee Blue Swirl
    }
417 c70c59ee Blue Swirl
418 81a322d4 Gerd Hoffmann
    return 0;
419 81732d19 blueswir1
}
420 81732d19 blueswir1
421 c70c59ee Blue Swirl
static SysBusDeviceInfo slavio_timer_info = {
422 c70c59ee Blue Swirl
    .init = slavio_timer_init1,
423 c70c59ee Blue Swirl
    .qdev.name  = "slavio_timer",
424 c70c59ee Blue Swirl
    .qdev.size  = sizeof(SLAVIO_TIMERState),
425 0e0bfeea Blue Swirl
    .qdev.vmsd  = &vmstate_slavio_timer,
426 0e0bfeea Blue Swirl
    .qdev.reset = slavio_timer_reset,
427 ee6847d1 Gerd Hoffmann
    .qdev.props = (Property[]) {
428 18c637dc Gerd Hoffmann
        DEFINE_PROP_UINT32("num_cpus",  SLAVIO_TIMERState, num_cpus,  0),
429 18c637dc Gerd Hoffmann
        DEFINE_PROP_END_OF_LIST(),
430 c70c59ee Blue Swirl
    }
431 c70c59ee Blue Swirl
};
432 c70c59ee Blue Swirl
433 c70c59ee Blue Swirl
static void slavio_timer_register_devices(void)
434 c70c59ee Blue Swirl
{
435 c70c59ee Blue Swirl
    sysbus_register_withprop(&slavio_timer_info);
436 c70c59ee Blue Swirl
}
437 c70c59ee Blue Swirl
438 c70c59ee Blue Swirl
device_init(slavio_timer_register_devices)