Statistics
| Branch: | Revision:

root / hw / slavio_timer.c @ bf3bc4c4

History | View | Annotate | Download (14 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 49d4d9b6 Paolo Bonzini
#include "ptimer.h"
28 c70c59ee Blue Swirl
#include "sysbus.h"
29 97bf4851 Blue Swirl
#include "trace.h"
30 66321a11 bellard
31 e80cfcfc bellard
/*
32 e80cfcfc bellard
 * Registers of hardware timer in sun4m.
33 e80cfcfc bellard
 *
34 e80cfcfc bellard
 * This is the timer/counter part of chip STP2001 (Slave I/O), also
35 e80cfcfc bellard
 * produced as NCR89C105. See
36 e80cfcfc bellard
 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
37 5fafdf24 ths
 *
38 e80cfcfc bellard
 * The 31-bit counter is incremented every 500ns by bit 9. Bits 8..0
39 e80cfcfc bellard
 * are zero. Bit 31 is 1 when count has been reached.
40 e80cfcfc bellard
 *
41 ba3c64fb bellard
 * Per-CPU timers interrupt local CPU, system timer uses normal
42 ba3c64fb bellard
 * interrupt routing.
43 ba3c64fb bellard
 *
44 e80cfcfc bellard
 */
45 e80cfcfc bellard
46 81732d19 blueswir1
#define MAX_CPUS 16
47 81732d19 blueswir1
48 7204ff9c Blue Swirl
typedef struct CPUTimerState {
49 d7edfd27 blueswir1
    qemu_irq irq;
50 8d05ea8a blueswir1
    ptimer_state *timer;
51 8d05ea8a blueswir1
    uint32_t count, counthigh, reached;
52 f90074f4 Blue Swirl
    /* processor only */
53 22548760 blueswir1
    uint32_t running;
54 f90074f4 Blue Swirl
    uint64_t limit;
55 7204ff9c Blue Swirl
} CPUTimerState;
56 7204ff9c Blue Swirl
57 7204ff9c Blue Swirl
typedef struct SLAVIO_TIMERState {
58 7204ff9c Blue Swirl
    SysBusDevice busdev;
59 7204ff9c Blue Swirl
    uint32_t num_cpus;
60 7204ff9c Blue Swirl
    uint32_t cputimer_mode;
61 f90074f4 Blue Swirl
    CPUTimerState cputimer[MAX_CPUS + 1];
62 e80cfcfc bellard
} SLAVIO_TIMERState;
63 e80cfcfc bellard
64 7204ff9c Blue Swirl
typedef struct TimerContext {
65 a3d12d07 Benoît Canet
    MemoryRegion iomem;
66 7204ff9c Blue Swirl
    SLAVIO_TIMERState *s;
67 7204ff9c Blue Swirl
    unsigned int timer_index; /* 0 for system, 1 ... MAX_CPUS for CPU timers */
68 7204ff9c Blue Swirl
} TimerContext;
69 7204ff9c Blue Swirl
70 115646b6 blueswir1
#define SYS_TIMER_SIZE 0x14
71 81732d19 blueswir1
#define CPU_TIMER_SIZE 0x10
72 e80cfcfc bellard
73 d2c38b24 blueswir1
#define TIMER_LIMIT         0
74 d2c38b24 blueswir1
#define TIMER_COUNTER       1
75 d2c38b24 blueswir1
#define TIMER_COUNTER_NORST 2
76 d2c38b24 blueswir1
#define TIMER_STATUS        3
77 d2c38b24 blueswir1
#define TIMER_MODE          4
78 d2c38b24 blueswir1
79 d2c38b24 blueswir1
#define TIMER_COUNT_MASK32 0xfffffe00
80 d2c38b24 blueswir1
#define TIMER_LIMIT_MASK32 0x7fffffff
81 d2c38b24 blueswir1
#define TIMER_MAX_COUNT64  0x7ffffffffffffe00ULL
82 d2c38b24 blueswir1
#define TIMER_MAX_COUNT32  0x7ffffe00ULL
83 d2c38b24 blueswir1
#define TIMER_REACHED      0x80000000
84 d2c38b24 blueswir1
#define TIMER_PERIOD       500ULL // 500ns
85 68fb89a2 Blue Swirl
#define LIMIT_TO_PERIODS(l) (((l) >> 9) - 1)
86 68fb89a2 Blue Swirl
#define PERIODS_TO_LIMIT(l) (((l) + 1) << 9)
87 d2c38b24 blueswir1
88 7204ff9c Blue Swirl
static int slavio_timer_is_user(TimerContext *tc)
89 115646b6 blueswir1
{
90 7204ff9c Blue Swirl
    SLAVIO_TIMERState *s = tc->s;
91 7204ff9c Blue Swirl
    unsigned int timer_index = tc->timer_index;
92 7204ff9c Blue Swirl
93 7204ff9c Blue Swirl
    return timer_index != 0 && (s->cputimer_mode & (1 << (timer_index - 1)));
94 115646b6 blueswir1
}
95 115646b6 blueswir1
96 e80cfcfc bellard
// Update count, set irq, update expire_time
97 8d05ea8a blueswir1
// Convert from ptimer countdown units
98 7204ff9c Blue Swirl
static void slavio_timer_get_out(CPUTimerState *t)
99 e80cfcfc bellard
{
100 bd7e2875 blueswir1
    uint64_t count, limit;
101 e80cfcfc bellard
102 7204ff9c Blue Swirl
    if (t->limit == 0) { /* free-run system or processor counter */
103 bd7e2875 blueswir1
        limit = TIMER_MAX_COUNT32;
104 7204ff9c Blue Swirl
    } else {
105 7204ff9c Blue Swirl
        limit = t->limit;
106 7204ff9c Blue Swirl
    }
107 9ebec28b Blue Swirl
    count = limit - PERIODS_TO_LIMIT(ptimer_get_count(t->timer));
108 9ebec28b Blue Swirl
109 97bf4851 Blue Swirl
    trace_slavio_timer_get_out(t->limit, t->counthigh, t->count);
110 7204ff9c Blue Swirl
    t->count = count & TIMER_COUNT_MASK32;
111 7204ff9c Blue Swirl
    t->counthigh = count >> 32;
112 e80cfcfc bellard
}
113 e80cfcfc bellard
114 e80cfcfc bellard
// timer callback
115 e80cfcfc bellard
static void slavio_timer_irq(void *opaque)
116 e80cfcfc bellard
{
117 7204ff9c Blue Swirl
    TimerContext *tc = opaque;
118 7204ff9c Blue Swirl
    SLAVIO_TIMERState *s = tc->s;
119 7204ff9c Blue Swirl
    CPUTimerState *t = &s->cputimer[tc->timer_index];
120 7204ff9c Blue Swirl
121 7204ff9c Blue Swirl
    slavio_timer_get_out(t);
122 97bf4851 Blue Swirl
    trace_slavio_timer_irq(t->counthigh, t->count);
123 68fb89a2 Blue Swirl
    /* if limit is 0 (free-run), there will be no match */
124 68fb89a2 Blue Swirl
    if (t->limit != 0) {
125 68fb89a2 Blue Swirl
        t->reached = TIMER_REACHED;
126 68fb89a2 Blue Swirl
    }
127 452efba6 Blue Swirl
    /* there is no interrupt if user timer or free-run */
128 452efba6 Blue Swirl
    if (!slavio_timer_is_user(tc) && t->limit != 0) {
129 7204ff9c Blue Swirl
        qemu_irq_raise(t->irq);
130 7204ff9c Blue Swirl
    }
131 e80cfcfc bellard
}
132 e80cfcfc bellard
133 a8170e5e Avi Kivity
static uint64_t slavio_timer_mem_readl(void *opaque, hwaddr addr,
134 a3d12d07 Benoît Canet
                                       unsigned size)
135 e80cfcfc bellard
{
136 7204ff9c Blue Swirl
    TimerContext *tc = opaque;
137 7204ff9c Blue Swirl
    SLAVIO_TIMERState *s = tc->s;
138 8d05ea8a blueswir1
    uint32_t saddr, ret;
139 7204ff9c Blue Swirl
    unsigned int timer_index = tc->timer_index;
140 7204ff9c Blue Swirl
    CPUTimerState *t = &s->cputimer[timer_index];
141 e80cfcfc bellard
142 e64d7d59 blueswir1
    saddr = addr >> 2;
143 e80cfcfc bellard
    switch (saddr) {
144 d2c38b24 blueswir1
    case TIMER_LIMIT:
145 f930d07e blueswir1
        // read limit (system counter mode) or read most signifying
146 f930d07e blueswir1
        // part of counter (user mode)
147 7204ff9c Blue Swirl
        if (slavio_timer_is_user(tc)) {
148 115646b6 blueswir1
            // read user timer MSW
149 7204ff9c Blue Swirl
            slavio_timer_get_out(t);
150 7204ff9c Blue Swirl
            ret = t->counthigh | t->reached;
151 115646b6 blueswir1
        } else {
152 115646b6 blueswir1
            // read limit
153 f930d07e blueswir1
            // clear irq
154 7204ff9c Blue Swirl
            qemu_irq_lower(t->irq);
155 7204ff9c Blue Swirl
            t->reached = 0;
156 7204ff9c Blue Swirl
            ret = t->limit & TIMER_LIMIT_MASK32;
157 f930d07e blueswir1
        }
158 8d05ea8a blueswir1
        break;
159 d2c38b24 blueswir1
    case TIMER_COUNTER:
160 f930d07e blueswir1
        // read counter and reached bit (system mode) or read lsbits
161 f930d07e blueswir1
        // of counter (user mode)
162 7204ff9c Blue Swirl
        slavio_timer_get_out(t);
163 7204ff9c Blue Swirl
        if (slavio_timer_is_user(tc)) { // read user timer LSW
164 7204ff9c Blue Swirl
            ret = t->count & TIMER_MAX_COUNT64;
165 7204ff9c Blue Swirl
        } else { // read limit
166 7204ff9c Blue Swirl
            ret = (t->count & TIMER_MAX_COUNT32) |
167 7204ff9c Blue Swirl
                t->reached;
168 7204ff9c Blue Swirl
        }
169 8d05ea8a blueswir1
        break;
170 d2c38b24 blueswir1
    case TIMER_STATUS:
171 115646b6 blueswir1
        // only available in processor counter/timer
172 f930d07e blueswir1
        // read start/stop status
173 7204ff9c Blue Swirl
        if (timer_index > 0) {
174 7204ff9c Blue Swirl
            ret = t->running;
175 7204ff9c Blue Swirl
        } else {
176 7204ff9c Blue Swirl
            ret = 0;
177 7204ff9c Blue Swirl
        }
178 8d05ea8a blueswir1
        break;
179 d2c38b24 blueswir1
    case TIMER_MODE:
180 115646b6 blueswir1
        // only available in system counter
181 f930d07e blueswir1
        // read user/system mode
182 7204ff9c Blue Swirl
        ret = s->cputimer_mode;
183 8d05ea8a blueswir1
        break;
184 e80cfcfc bellard
    default:
185 97bf4851 Blue Swirl
        trace_slavio_timer_mem_readl_invalid(addr);
186 8d05ea8a blueswir1
        ret = 0;
187 8d05ea8a blueswir1
        break;
188 e80cfcfc bellard
    }
189 97bf4851 Blue Swirl
    trace_slavio_timer_mem_readl(addr, ret);
190 8d05ea8a blueswir1
    return ret;
191 e80cfcfc bellard
}
192 e80cfcfc bellard
193 a8170e5e Avi Kivity
static void slavio_timer_mem_writel(void *opaque, hwaddr addr,
194 a3d12d07 Benoît Canet
                                    uint64_t val, unsigned size)
195 e80cfcfc bellard
{
196 7204ff9c Blue Swirl
    TimerContext *tc = opaque;
197 7204ff9c Blue Swirl
    SLAVIO_TIMERState *s = tc->s;
198 e80cfcfc bellard
    uint32_t saddr;
199 7204ff9c Blue Swirl
    unsigned int timer_index = tc->timer_index;
200 7204ff9c Blue Swirl
    CPUTimerState *t = &s->cputimer[timer_index];
201 e80cfcfc bellard
202 97bf4851 Blue Swirl
    trace_slavio_timer_mem_writel(addr, val);
203 e64d7d59 blueswir1
    saddr = addr >> 2;
204 e80cfcfc bellard
    switch (saddr) {
205 d2c38b24 blueswir1
    case TIMER_LIMIT:
206 7204ff9c Blue Swirl
        if (slavio_timer_is_user(tc)) {
207 e1cb9502 blueswir1
            uint64_t count;
208 e1cb9502 blueswir1
209 115646b6 blueswir1
            // set user counter MSW, reset counter
210 7204ff9c Blue Swirl
            t->limit = TIMER_MAX_COUNT64;
211 7204ff9c Blue Swirl
            t->counthigh = val & (TIMER_MAX_COUNT64 >> 32);
212 7204ff9c Blue Swirl
            t->reached = 0;
213 7204ff9c Blue Swirl
            count = ((uint64_t)t->counthigh << 32) | t->count;
214 97bf4851 Blue Swirl
            trace_slavio_timer_mem_writel_limit(timer_index, count);
215 9ebec28b Blue Swirl
            ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
216 115646b6 blueswir1
        } else {
217 115646b6 blueswir1
            // set limit, reset counter
218 7204ff9c Blue Swirl
            qemu_irq_lower(t->irq);
219 7204ff9c Blue Swirl
            t->limit = val & TIMER_MAX_COUNT32;
220 7204ff9c Blue Swirl
            if (t->timer) {
221 7204ff9c Blue Swirl
                if (t->limit == 0) { /* free-run */
222 7204ff9c Blue Swirl
                    ptimer_set_limit(t->timer,
223 77f193da blueswir1
                                     LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
224 7204ff9c Blue Swirl
                } else {
225 7204ff9c Blue Swirl
                    ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 1);
226 7204ff9c Blue Swirl
                }
227 85e3023e blueswir1
            }
228 81732d19 blueswir1
        }
229 115646b6 blueswir1
        break;
230 d2c38b24 blueswir1
    case TIMER_COUNTER:
231 7204ff9c Blue Swirl
        if (slavio_timer_is_user(tc)) {
232 e1cb9502 blueswir1
            uint64_t count;
233 e1cb9502 blueswir1
234 115646b6 blueswir1
            // set user counter LSW, reset counter
235 7204ff9c Blue Swirl
            t->limit = TIMER_MAX_COUNT64;
236 7204ff9c Blue Swirl
            t->count = val & TIMER_MAX_COUNT64;
237 7204ff9c Blue Swirl
            t->reached = 0;
238 7204ff9c Blue Swirl
            count = ((uint64_t)t->counthigh) << 32 | t->count;
239 97bf4851 Blue Swirl
            trace_slavio_timer_mem_writel_limit(timer_index, count);
240 9ebec28b Blue Swirl
            ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
241 97bf4851 Blue Swirl
        } else {
242 97bf4851 Blue Swirl
            trace_slavio_timer_mem_writel_counter_invalid();
243 97bf4851 Blue Swirl
        }
244 115646b6 blueswir1
        break;
245 d2c38b24 blueswir1
    case TIMER_COUNTER_NORST:
246 f930d07e blueswir1
        // set limit without resetting counter
247 7204ff9c Blue Swirl
        t->limit = val & TIMER_MAX_COUNT32;
248 9ebec28b Blue Swirl
        if (t->limit == 0) { /* free-run */
249 9ebec28b Blue Swirl
            ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 0);
250 9ebec28b Blue Swirl
        } else {
251 9ebec28b Blue Swirl
            ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 0);
252 85e3023e blueswir1
        }
253 f930d07e blueswir1
        break;
254 d2c38b24 blueswir1
    case TIMER_STATUS:
255 7204ff9c Blue Swirl
        if (slavio_timer_is_user(tc)) {
256 115646b6 blueswir1
            // start/stop user counter
257 7204ff9c Blue Swirl
            if ((val & 1) && !t->running) {
258 97bf4851 Blue Swirl
                trace_slavio_timer_mem_writel_status_start(timer_index);
259 9ebec28b Blue Swirl
                ptimer_run(t->timer, 0);
260 7204ff9c Blue Swirl
                t->running = 1;
261 7204ff9c Blue Swirl
            } else if (!(val & 1) && t->running) {
262 97bf4851 Blue Swirl
                trace_slavio_timer_mem_writel_status_stop(timer_index);
263 9ebec28b Blue Swirl
                ptimer_stop(t->timer);
264 7204ff9c Blue Swirl
                t->running = 0;
265 f930d07e blueswir1
            }
266 f930d07e blueswir1
        }
267 f930d07e blueswir1
        break;
268 d2c38b24 blueswir1
    case TIMER_MODE:
269 7204ff9c Blue Swirl
        if (timer_index == 0) {
270 81732d19 blueswir1
            unsigned int i;
271 81732d19 blueswir1
272 7204ff9c Blue Swirl
            for (i = 0; i < s->num_cpus; i++) {
273 67e42751 blueswir1
                unsigned int processor = 1 << i;
274 7204ff9c Blue Swirl
                CPUTimerState *curr_timer = &s->cputimer[i + 1];
275 67e42751 blueswir1
276 67e42751 blueswir1
                // check for a change in timer mode for this processor
277 7204ff9c Blue Swirl
                if ((val & processor) != (s->cputimer_mode & processor)) {
278 67e42751 blueswir1
                    if (val & processor) { // counter -> user timer
279 7204ff9c Blue Swirl
                        qemu_irq_lower(curr_timer->irq);
280 67e42751 blueswir1
                        // counters are always running
281 7204ff9c Blue Swirl
                        ptimer_stop(curr_timer->timer);
282 7204ff9c Blue Swirl
                        curr_timer->running = 0;
283 67e42751 blueswir1
                        // user timer limit is always the same
284 7204ff9c Blue Swirl
                        curr_timer->limit = TIMER_MAX_COUNT64;
285 7204ff9c Blue Swirl
                        ptimer_set_limit(curr_timer->timer,
286 7204ff9c Blue Swirl
                                         LIMIT_TO_PERIODS(curr_timer->limit),
287 77f193da blueswir1
                                         1);
288 67e42751 blueswir1
                        // set this processors user timer bit in config
289 67e42751 blueswir1
                        // register
290 7204ff9c Blue Swirl
                        s->cputimer_mode |= processor;
291 97bf4851 Blue Swirl
                        trace_slavio_timer_mem_writel_mode_user(timer_index);
292 67e42751 blueswir1
                    } else { // user timer -> counter
293 67e42751 blueswir1
                        // stop the user timer if it is running
294 7204ff9c Blue Swirl
                        if (curr_timer->running) {
295 7204ff9c Blue Swirl
                            ptimer_stop(curr_timer->timer);
296 7204ff9c Blue Swirl
                        }
297 67e42751 blueswir1
                        // start the counter
298 7204ff9c Blue Swirl
                        ptimer_run(curr_timer->timer, 0);
299 7204ff9c Blue Swirl
                        curr_timer->running = 1;
300 67e42751 blueswir1
                        // clear this processors user timer bit in config
301 67e42751 blueswir1
                        // register
302 7204ff9c Blue Swirl
                        s->cputimer_mode &= ~processor;
303 97bf4851 Blue Swirl
                        trace_slavio_timer_mem_writel_mode_counter(timer_index);
304 67e42751 blueswir1
                    }
305 115646b6 blueswir1
                }
306 81732d19 blueswir1
            }
307 7204ff9c Blue Swirl
        } else {
308 97bf4851 Blue Swirl
            trace_slavio_timer_mem_writel_mode_invalid();
309 7204ff9c Blue Swirl
        }
310 f930d07e blueswir1
        break;
311 e80cfcfc bellard
    default:
312 97bf4851 Blue Swirl
        trace_slavio_timer_mem_writel_invalid(addr);
313 f930d07e blueswir1
        break;
314 e80cfcfc bellard
    }
315 e80cfcfc bellard
}
316 e80cfcfc bellard
317 a3d12d07 Benoît Canet
static const MemoryRegionOps slavio_timer_mem_ops = {
318 a3d12d07 Benoît Canet
    .read = slavio_timer_mem_readl,
319 a3d12d07 Benoît Canet
    .write = slavio_timer_mem_writel,
320 a3d12d07 Benoît Canet
    .endianness = DEVICE_NATIVE_ENDIAN,
321 a3d12d07 Benoît Canet
    .valid = {
322 a3d12d07 Benoît Canet
        .min_access_size = 4,
323 a3d12d07 Benoît Canet
        .max_access_size = 4,
324 a3d12d07 Benoît Canet
    },
325 e80cfcfc bellard
};
326 e80cfcfc bellard
327 f4b19cd0 Blue Swirl
static const VMStateDescription vmstate_timer = {
328 f4b19cd0 Blue Swirl
    .name ="timer",
329 f4b19cd0 Blue Swirl
    .version_id = 3,
330 f4b19cd0 Blue Swirl
    .minimum_version_id = 3,
331 f4b19cd0 Blue Swirl
    .minimum_version_id_old = 3,
332 f4b19cd0 Blue Swirl
    .fields      = (VMStateField []) {
333 f4b19cd0 Blue Swirl
        VMSTATE_UINT64(limit, CPUTimerState),
334 f4b19cd0 Blue Swirl
        VMSTATE_UINT32(count, CPUTimerState),
335 f4b19cd0 Blue Swirl
        VMSTATE_UINT32(counthigh, CPUTimerState),
336 f4b19cd0 Blue Swirl
        VMSTATE_UINT32(reached, CPUTimerState),
337 f4b19cd0 Blue Swirl
        VMSTATE_UINT32(running, CPUTimerState),
338 f4b19cd0 Blue Swirl
        VMSTATE_PTIMER(timer, CPUTimerState),
339 f4b19cd0 Blue Swirl
        VMSTATE_END_OF_LIST()
340 7204ff9c Blue Swirl
    }
341 f4b19cd0 Blue Swirl
};
342 e80cfcfc bellard
343 f4b19cd0 Blue Swirl
static const VMStateDescription vmstate_slavio_timer = {
344 f4b19cd0 Blue Swirl
    .name ="slavio_timer",
345 f4b19cd0 Blue Swirl
    .version_id = 3,
346 f4b19cd0 Blue Swirl
    .minimum_version_id = 3,
347 f4b19cd0 Blue Swirl
    .minimum_version_id_old = 3,
348 f4b19cd0 Blue Swirl
    .fields      = (VMStateField []) {
349 f4b19cd0 Blue Swirl
        VMSTATE_STRUCT_ARRAY(cputimer, SLAVIO_TIMERState, MAX_CPUS + 1, 3,
350 f4b19cd0 Blue Swirl
                             vmstate_timer, CPUTimerState),
351 f4b19cd0 Blue Swirl
        VMSTATE_END_OF_LIST()
352 7204ff9c Blue Swirl
    }
353 f4b19cd0 Blue Swirl
};
354 e80cfcfc bellard
355 0e0bfeea Blue Swirl
static void slavio_timer_reset(DeviceState *d)
356 e80cfcfc bellard
{
357 0e0bfeea Blue Swirl
    SLAVIO_TIMERState *s = container_of(d, SLAVIO_TIMERState, busdev.qdev);
358 7204ff9c Blue Swirl
    unsigned int i;
359 7204ff9c Blue Swirl
    CPUTimerState *curr_timer;
360 7204ff9c Blue Swirl
361 7204ff9c Blue Swirl
    for (i = 0; i <= MAX_CPUS; i++) {
362 7204ff9c Blue Swirl
        curr_timer = &s->cputimer[i];
363 7204ff9c Blue Swirl
        curr_timer->limit = 0;
364 7204ff9c Blue Swirl
        curr_timer->count = 0;
365 7204ff9c Blue Swirl
        curr_timer->reached = 0;
366 5933e8a9 Artyom Tarasenko
        if (i <= s->num_cpus) {
367 7204ff9c Blue Swirl
            ptimer_set_limit(curr_timer->timer,
368 7204ff9c Blue Swirl
                             LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
369 7204ff9c Blue Swirl
            ptimer_run(curr_timer->timer, 0);
370 5933e8a9 Artyom Tarasenko
            curr_timer->running = 1;
371 7204ff9c Blue Swirl
        }
372 85e3023e blueswir1
    }
373 7204ff9c Blue Swirl
    s->cputimer_mode = 0;
374 e80cfcfc bellard
}
375 e80cfcfc bellard
376 81a322d4 Gerd Hoffmann
static int slavio_timer_init1(SysBusDevice *dev)
377 c70c59ee Blue Swirl
{
378 c70c59ee Blue Swirl
    SLAVIO_TIMERState *s = FROM_SYSBUS(SLAVIO_TIMERState, dev);
379 8d05ea8a blueswir1
    QEMUBH *bh;
380 7204ff9c Blue Swirl
    unsigned int i;
381 7204ff9c Blue Swirl
    TimerContext *tc;
382 e80cfcfc bellard
383 7204ff9c Blue Swirl
    for (i = 0; i <= MAX_CPUS; i++) {
384 a3d12d07 Benoît Canet
        uint64_t size;
385 a3d12d07 Benoît Canet
        char timer_name[20];
386 a3d12d07 Benoît Canet
387 7267c094 Anthony Liguori
        tc = g_malloc0(sizeof(TimerContext));
388 7204ff9c Blue Swirl
        tc->s = s;
389 7204ff9c Blue Swirl
        tc->timer_index = i;
390 c70c59ee Blue Swirl
391 7204ff9c Blue Swirl
        bh = qemu_bh_new(slavio_timer_irq, tc);
392 7204ff9c Blue Swirl
        s->cputimer[i].timer = ptimer_init(bh);
393 7204ff9c Blue Swirl
        ptimer_set_period(s->cputimer[i].timer, TIMER_PERIOD);
394 e80cfcfc bellard
395 a3d12d07 Benoît Canet
        size = i == 0 ? SYS_TIMER_SIZE : CPU_TIMER_SIZE;
396 a3d12d07 Benoît Canet
        snprintf(timer_name, sizeof(timer_name), "timer-%i", i);
397 a3d12d07 Benoît Canet
        memory_region_init_io(&tc->iomem, &slavio_timer_mem_ops, tc,
398 a3d12d07 Benoît Canet
                              timer_name, size);
399 750ecd44 Avi Kivity
        sysbus_init_mmio(dev, &tc->iomem);
400 7204ff9c Blue Swirl
401 7204ff9c Blue Swirl
        sysbus_init_irq(dev, &s->cputimer[i].irq);
402 c70c59ee Blue Swirl
    }
403 c70c59ee Blue Swirl
404 81a322d4 Gerd Hoffmann
    return 0;
405 81732d19 blueswir1
}
406 81732d19 blueswir1
407 999e12bb Anthony Liguori
static Property slavio_timer_properties[] = {
408 999e12bb Anthony Liguori
    DEFINE_PROP_UINT32("num_cpus",  SLAVIO_TIMERState, num_cpus,  0),
409 999e12bb Anthony Liguori
    DEFINE_PROP_END_OF_LIST(),
410 999e12bb Anthony Liguori
};
411 999e12bb Anthony Liguori
412 999e12bb Anthony Liguori
static void slavio_timer_class_init(ObjectClass *klass, void *data)
413 999e12bb Anthony Liguori
{
414 39bffca2 Anthony Liguori
    DeviceClass *dc = DEVICE_CLASS(klass);
415 999e12bb Anthony Liguori
    SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
416 999e12bb Anthony Liguori
417 999e12bb Anthony Liguori
    k->init = slavio_timer_init1;
418 39bffca2 Anthony Liguori
    dc->reset = slavio_timer_reset;
419 39bffca2 Anthony Liguori
    dc->vmsd = &vmstate_slavio_timer;
420 39bffca2 Anthony Liguori
    dc->props = slavio_timer_properties;
421 999e12bb Anthony Liguori
}
422 999e12bb Anthony Liguori
423 39bffca2 Anthony Liguori
static TypeInfo slavio_timer_info = {
424 39bffca2 Anthony Liguori
    .name          = "slavio_timer",
425 39bffca2 Anthony Liguori
    .parent        = TYPE_SYS_BUS_DEVICE,
426 39bffca2 Anthony Liguori
    .instance_size = sizeof(SLAVIO_TIMERState),
427 39bffca2 Anthony Liguori
    .class_init    = slavio_timer_class_init,
428 c70c59ee Blue Swirl
};
429 c70c59ee Blue Swirl
430 83f7d43a Andreas Färber
static void slavio_timer_register_types(void)
431 c70c59ee Blue Swirl
{
432 39bffca2 Anthony Liguori
    type_register_static(&slavio_timer_info);
433 c70c59ee Blue Swirl
}
434 c70c59ee Blue Swirl
435 83f7d43a Andreas Färber
type_init(slavio_timer_register_types)