slavio_timer.c 13.7 KB
Newer Older
B
bellard 已提交
1 2 3
/*
 * QEMU Sparc SLAVIO timer controller emulation
 *
B
bellard 已提交
4
 * Copyright (c) 2003-2005 Fabrice Bellard
5
 *
B
bellard 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
24

P
pbrook 已提交
25 26
#include "sun4m.h"
#include "qemu-timer.h"
27
#include "sysbus.h"
28
#include "trace.h"
B
bellard 已提交
29

B
bellard 已提交
30 31 32 33 34 35
/*
 * Registers of hardware timer in sun4m.
 *
 * This is the timer/counter part of chip STP2001 (Slave I/O), also
 * produced as NCR89C105. See
 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
36
 *
B
bellard 已提交
37 38 39
 * The 31-bit counter is incremented every 500ns by bit 9. Bits 8..0
 * are zero. Bit 31 is 1 when count has been reached.
 *
B
bellard 已提交
40 41 42
 * Per-CPU timers interrupt local CPU, system timer uses normal
 * interrupt routing.
 *
B
bellard 已提交
43 44
 */

B
blueswir1 已提交
45 46
#define MAX_CPUS 16

B
Blue Swirl 已提交
47
typedef struct CPUTimerState {
48
    qemu_irq irq;
49 50
    ptimer_state *timer;
    uint32_t count, counthigh, reached;
51
    /* processor only */
B
blueswir1 已提交
52
    uint32_t running;
53
    uint64_t limit;
B
Blue Swirl 已提交
54 55 56 57 58 59
} CPUTimerState;

typedef struct SLAVIO_TIMERState {
    SysBusDevice busdev;
    uint32_t num_cpus;
    uint32_t cputimer_mode;
60
    CPUTimerState cputimer[MAX_CPUS + 1];
B
bellard 已提交
61 62
} SLAVIO_TIMERState;

B
Blue Swirl 已提交
63 64 65 66 67
typedef struct TimerContext {
    SLAVIO_TIMERState *s;
    unsigned int timer_index; /* 0 for system, 1 ... MAX_CPUS for CPU timers */
} TimerContext;

B
blueswir1 已提交
68
#define SYS_TIMER_SIZE 0x14
B
blueswir1 已提交
69
#define CPU_TIMER_SIZE 0x10
B
bellard 已提交
70

71 72 73 74 75 76 77 78 79 80 81 82
#define TIMER_LIMIT         0
#define TIMER_COUNTER       1
#define TIMER_COUNTER_NORST 2
#define TIMER_STATUS        3
#define TIMER_MODE          4

#define TIMER_COUNT_MASK32 0xfffffe00
#define TIMER_LIMIT_MASK32 0x7fffffff
#define TIMER_MAX_COUNT64  0x7ffffffffffffe00ULL
#define TIMER_MAX_COUNT32  0x7ffffe00ULL
#define TIMER_REACHED      0x80000000
#define TIMER_PERIOD       500ULL // 500ns
83 84
#define LIMIT_TO_PERIODS(l) (((l) >> 9) - 1)
#define PERIODS_TO_LIMIT(l) (((l) + 1) << 9)
85

B
Blue Swirl 已提交
86
static int slavio_timer_is_user(TimerContext *tc)
B
blueswir1 已提交
87
{
B
Blue Swirl 已提交
88 89 90 91
    SLAVIO_TIMERState *s = tc->s;
    unsigned int timer_index = tc->timer_index;

    return timer_index != 0 && (s->cputimer_mode & (1 << (timer_index - 1)));
B
blueswir1 已提交
92 93
}

B
bellard 已提交
94
// Update count, set irq, update expire_time
95
// Convert from ptimer countdown units
B
Blue Swirl 已提交
96
static void slavio_timer_get_out(CPUTimerState *t)
B
bellard 已提交
97
{
98
    uint64_t count, limit;
B
bellard 已提交
99

B
Blue Swirl 已提交
100
    if (t->limit == 0) { /* free-run system or processor counter */
101
        limit = TIMER_MAX_COUNT32;
B
Blue Swirl 已提交
102 103 104
    } else {
        limit = t->limit;
    }
B
Blue Swirl 已提交
105 106
    count = limit - PERIODS_TO_LIMIT(ptimer_get_count(t->timer));

107
    trace_slavio_timer_get_out(t->limit, t->counthigh, t->count);
B
Blue Swirl 已提交
108 109
    t->count = count & TIMER_COUNT_MASK32;
    t->counthigh = count >> 32;
B
bellard 已提交
110 111 112 113 114
}

// timer callback
static void slavio_timer_irq(void *opaque)
{
B
Blue Swirl 已提交
115 116 117 118 119
    TimerContext *tc = opaque;
    SLAVIO_TIMERState *s = tc->s;
    CPUTimerState *t = &s->cputimer[tc->timer_index];

    slavio_timer_get_out(t);
120
    trace_slavio_timer_irq(t->counthigh, t->count);
121 122 123 124
    /* if limit is 0 (free-run), there will be no match */
    if (t->limit != 0) {
        t->reached = TIMER_REACHED;
    }
B
Blue Swirl 已提交
125 126
    /* there is no interrupt if user timer or free-run */
    if (!slavio_timer_is_user(tc) && t->limit != 0) {
B
Blue Swirl 已提交
127 128
        qemu_irq_raise(t->irq);
    }
B
bellard 已提交
129 130
}

A
Anthony Liguori 已提交
131
static uint32_t slavio_timer_mem_readl(void *opaque, target_phys_addr_t addr)
B
bellard 已提交
132
{
B
Blue Swirl 已提交
133 134
    TimerContext *tc = opaque;
    SLAVIO_TIMERState *s = tc->s;
135
    uint32_t saddr, ret;
B
Blue Swirl 已提交
136 137
    unsigned int timer_index = tc->timer_index;
    CPUTimerState *t = &s->cputimer[timer_index];
B
bellard 已提交
138

B
blueswir1 已提交
139
    saddr = addr >> 2;
B
bellard 已提交
140
    switch (saddr) {
141
    case TIMER_LIMIT:
B
blueswir1 已提交
142 143
        // read limit (system counter mode) or read most signifying
        // part of counter (user mode)
B
Blue Swirl 已提交
144
        if (slavio_timer_is_user(tc)) {
B
blueswir1 已提交
145
            // read user timer MSW
B
Blue Swirl 已提交
146 147
            slavio_timer_get_out(t);
            ret = t->counthigh | t->reached;
B
blueswir1 已提交
148 149
        } else {
            // read limit
B
blueswir1 已提交
150
            // clear irq
B
Blue Swirl 已提交
151 152 153
            qemu_irq_lower(t->irq);
            t->reached = 0;
            ret = t->limit & TIMER_LIMIT_MASK32;
B
blueswir1 已提交
154
        }
155
        break;
156
    case TIMER_COUNTER:
B
blueswir1 已提交
157 158
        // read counter and reached bit (system mode) or read lsbits
        // of counter (user mode)
B
Blue Swirl 已提交
159 160 161 162 163 164 165
        slavio_timer_get_out(t);
        if (slavio_timer_is_user(tc)) { // read user timer LSW
            ret = t->count & TIMER_MAX_COUNT64;
        } else { // read limit
            ret = (t->count & TIMER_MAX_COUNT32) |
                t->reached;
        }
166
        break;
167
    case TIMER_STATUS:
B
blueswir1 已提交
168
        // only available in processor counter/timer
B
blueswir1 已提交
169
        // read start/stop status
B
Blue Swirl 已提交
170 171 172 173 174
        if (timer_index > 0) {
            ret = t->running;
        } else {
            ret = 0;
        }
175
        break;
176
    case TIMER_MODE:
B
blueswir1 已提交
177
        // only available in system counter
B
blueswir1 已提交
178
        // read user/system mode
B
Blue Swirl 已提交
179
        ret = s->cputimer_mode;
180
        break;
B
bellard 已提交
181
    default:
182
        trace_slavio_timer_mem_readl_invalid(addr);
183 184
        ret = 0;
        break;
B
bellard 已提交
185
    }
186
    trace_slavio_timer_mem_readl(addr, ret);
187
    return ret;
B
bellard 已提交
188 189
}

A
Anthony Liguori 已提交
190
static void slavio_timer_mem_writel(void *opaque, target_phys_addr_t addr,
191
                                    uint32_t val)
B
bellard 已提交
192
{
B
Blue Swirl 已提交
193 194
    TimerContext *tc = opaque;
    SLAVIO_TIMERState *s = tc->s;
B
bellard 已提交
195
    uint32_t saddr;
B
Blue Swirl 已提交
196 197
    unsigned int timer_index = tc->timer_index;
    CPUTimerState *t = &s->cputimer[timer_index];
B
bellard 已提交
198

199
    trace_slavio_timer_mem_writel(addr, val);
B
blueswir1 已提交
200
    saddr = addr >> 2;
B
bellard 已提交
201
    switch (saddr) {
202
    case TIMER_LIMIT:
B
Blue Swirl 已提交
203
        if (slavio_timer_is_user(tc)) {
204 205
            uint64_t count;

B
blueswir1 已提交
206
            // set user counter MSW, reset counter
B
Blue Swirl 已提交
207 208 209 210
            t->limit = TIMER_MAX_COUNT64;
            t->counthigh = val & (TIMER_MAX_COUNT64 >> 32);
            t->reached = 0;
            count = ((uint64_t)t->counthigh << 32) | t->count;
211
            trace_slavio_timer_mem_writel_limit(timer_index, count);
B
Blue Swirl 已提交
212
            ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
B
blueswir1 已提交
213 214
        } else {
            // set limit, reset counter
B
Blue Swirl 已提交
215 216 217 218 219
            qemu_irq_lower(t->irq);
            t->limit = val & TIMER_MAX_COUNT32;
            if (t->timer) {
                if (t->limit == 0) { /* free-run */
                    ptimer_set_limit(t->timer,
B
blueswir1 已提交
220
                                     LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
B
Blue Swirl 已提交
221 222 223
                } else {
                    ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 1);
                }
B
blueswir1 已提交
224
            }
B
blueswir1 已提交
225
        }
B
blueswir1 已提交
226
        break;
227
    case TIMER_COUNTER:
B
Blue Swirl 已提交
228
        if (slavio_timer_is_user(tc)) {
229 230
            uint64_t count;

B
blueswir1 已提交
231
            // set user counter LSW, reset counter
B
Blue Swirl 已提交
232 233 234 235
            t->limit = TIMER_MAX_COUNT64;
            t->count = val & TIMER_MAX_COUNT64;
            t->reached = 0;
            count = ((uint64_t)t->counthigh) << 32 | t->count;
236
            trace_slavio_timer_mem_writel_limit(timer_index, count);
B
Blue Swirl 已提交
237
            ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
238 239 240
        } else {
            trace_slavio_timer_mem_writel_counter_invalid();
        }
B
blueswir1 已提交
241
        break;
242
    case TIMER_COUNTER_NORST:
B
blueswir1 已提交
243
        // set limit without resetting counter
B
Blue Swirl 已提交
244
        t->limit = val & TIMER_MAX_COUNT32;
B
Blue Swirl 已提交
245 246 247 248
        if (t->limit == 0) { /* free-run */
            ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 0);
        } else {
            ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 0);
B
blueswir1 已提交
249
        }
B
blueswir1 已提交
250
        break;
251
    case TIMER_STATUS:
B
Blue Swirl 已提交
252
        if (slavio_timer_is_user(tc)) {
B
blueswir1 已提交
253
            // start/stop user counter
B
Blue Swirl 已提交
254
            if ((val & 1) && !t->running) {
255
                trace_slavio_timer_mem_writel_status_start(timer_index);
B
Blue Swirl 已提交
256
                ptimer_run(t->timer, 0);
B
Blue Swirl 已提交
257 258
                t->running = 1;
            } else if (!(val & 1) && t->running) {
259
                trace_slavio_timer_mem_writel_status_stop(timer_index);
B
Blue Swirl 已提交
260
                ptimer_stop(t->timer);
B
Blue Swirl 已提交
261
                t->running = 0;
B
blueswir1 已提交
262 263 264
            }
        }
        break;
265
    case TIMER_MODE:
B
Blue Swirl 已提交
266
        if (timer_index == 0) {
B
blueswir1 已提交
267 268
            unsigned int i;

B
Blue Swirl 已提交
269
            for (i = 0; i < s->num_cpus; i++) {
270
                unsigned int processor = 1 << i;
B
Blue Swirl 已提交
271
                CPUTimerState *curr_timer = &s->cputimer[i + 1];
272 273

                // check for a change in timer mode for this processor
B
Blue Swirl 已提交
274
                if ((val & processor) != (s->cputimer_mode & processor)) {
275
                    if (val & processor) { // counter -> user timer
B
Blue Swirl 已提交
276
                        qemu_irq_lower(curr_timer->irq);
277
                        // counters are always running
B
Blue Swirl 已提交
278 279
                        ptimer_stop(curr_timer->timer);
                        curr_timer->running = 0;
280
                        // user timer limit is always the same
B
Blue Swirl 已提交
281 282 283
                        curr_timer->limit = TIMER_MAX_COUNT64;
                        ptimer_set_limit(curr_timer->timer,
                                         LIMIT_TO_PERIODS(curr_timer->limit),
B
blueswir1 已提交
284
                                         1);
285 286
                        // set this processors user timer bit in config
                        // register
B
Blue Swirl 已提交
287
                        s->cputimer_mode |= processor;
288
                        trace_slavio_timer_mem_writel_mode_user(timer_index);
289 290
                    } else { // user timer -> counter
                        // stop the user timer if it is running
B
Blue Swirl 已提交
291 292 293
                        if (curr_timer->running) {
                            ptimer_stop(curr_timer->timer);
                        }
294
                        // start the counter
B
Blue Swirl 已提交
295 296
                        ptimer_run(curr_timer->timer, 0);
                        curr_timer->running = 1;
297 298
                        // clear this processors user timer bit in config
                        // register
B
Blue Swirl 已提交
299
                        s->cputimer_mode &= ~processor;
300
                        trace_slavio_timer_mem_writel_mode_counter(timer_index);
301
                    }
B
blueswir1 已提交
302
                }
B
blueswir1 已提交
303
            }
B
Blue Swirl 已提交
304
        } else {
305
            trace_slavio_timer_mem_writel_mode_invalid();
B
Blue Swirl 已提交
306
        }
B
blueswir1 已提交
307
        break;
B
bellard 已提交
308
    default:
309
        trace_slavio_timer_mem_writel_invalid(addr);
B
blueswir1 已提交
310
        break;
B
bellard 已提交
311 312 313
    }
}

314
static CPUReadMemoryFunc * const slavio_timer_mem_read[3] = {
315 316
    NULL,
    NULL,
B
bellard 已提交
317 318 319
    slavio_timer_mem_readl,
};

320
static CPUWriteMemoryFunc * const slavio_timer_mem_write[3] = {
321 322
    NULL,
    NULL,
B
bellard 已提交
323 324 325
    slavio_timer_mem_writel,
};

326 327 328 329 330 331 332 333 334 335 336 337 338
static const VMStateDescription vmstate_timer = {
    .name ="timer",
    .version_id = 3,
    .minimum_version_id = 3,
    .minimum_version_id_old = 3,
    .fields      = (VMStateField []) {
        VMSTATE_UINT64(limit, CPUTimerState),
        VMSTATE_UINT32(count, CPUTimerState),
        VMSTATE_UINT32(counthigh, CPUTimerState),
        VMSTATE_UINT32(reached, CPUTimerState),
        VMSTATE_UINT32(running, CPUTimerState),
        VMSTATE_PTIMER(timer, CPUTimerState),
        VMSTATE_END_OF_LIST()
B
Blue Swirl 已提交
339
    }
340
};
B
bellard 已提交
341

342 343 344 345 346 347 348 349 350
static const VMStateDescription vmstate_slavio_timer = {
    .name ="slavio_timer",
    .version_id = 3,
    .minimum_version_id = 3,
    .minimum_version_id_old = 3,
    .fields      = (VMStateField []) {
        VMSTATE_STRUCT_ARRAY(cputimer, SLAVIO_TIMERState, MAX_CPUS + 1, 3,
                             vmstate_timer, CPUTimerState),
        VMSTATE_END_OF_LIST()
B
Blue Swirl 已提交
351
    }
352
};
B
bellard 已提交
353

354
static void slavio_timer_reset(DeviceState *d)
B
bellard 已提交
355
{
356
    SLAVIO_TIMERState *s = container_of(d, SLAVIO_TIMERState, busdev.qdev);
B
Blue Swirl 已提交
357 358 359 360 361 362 363 364
    unsigned int i;
    CPUTimerState *curr_timer;

    for (i = 0; i <= MAX_CPUS; i++) {
        curr_timer = &s->cputimer[i];
        curr_timer->limit = 0;
        curr_timer->count = 0;
        curr_timer->reached = 0;
365
        if (i <= s->num_cpus) {
B
Blue Swirl 已提交
366 367 368
            ptimer_set_limit(curr_timer->timer,
                             LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
            ptimer_run(curr_timer->timer, 0);
369
            curr_timer->running = 1;
B
Blue Swirl 已提交
370
        }
B
blueswir1 已提交
371
    }
B
Blue Swirl 已提交
372
    s->cputimer_mode = 0;
B
bellard 已提交
373 374
}

375
static int slavio_timer_init1(SysBusDevice *dev)
376 377 378
{
    int io;
    SLAVIO_TIMERState *s = FROM_SYSBUS(SLAVIO_TIMERState, dev);
379
    QEMUBH *bh;
B
Blue Swirl 已提交
380 381
    unsigned int i;
    TimerContext *tc;
B
bellard 已提交
382

B
Blue Swirl 已提交
383
    for (i = 0; i <= MAX_CPUS; i++) {
384
        tc = g_malloc0(sizeof(TimerContext));
B
Blue Swirl 已提交
385 386
        tc->s = s;
        tc->timer_index = i;
387

B
Blue Swirl 已提交
388 389 390
        bh = qemu_bh_new(slavio_timer_irq, tc);
        s->cputimer[i].timer = ptimer_init(bh);
        ptimer_set_period(s->cputimer[i].timer, TIMER_PERIOD);
B
bellard 已提交
391

B
Blue Swirl 已提交
392
        io = cpu_register_io_memory(slavio_timer_mem_read,
393 394
                                    slavio_timer_mem_write, tc,
                                    DEVICE_NATIVE_ENDIAN);
B
Blue Swirl 已提交
395 396 397 398 399 400 401
        if (i == 0) {
            sysbus_init_mmio(dev, SYS_TIMER_SIZE, io);
        } else {
            sysbus_init_mmio(dev, CPU_TIMER_SIZE, io);
        }

        sysbus_init_irq(dev, &s->cputimer[i].irq);
402 403
    }

404
    return 0;
B
blueswir1 已提交
405 406
}

407 408 409 410
static SysBusDeviceInfo slavio_timer_info = {
    .init = slavio_timer_init1,
    .qdev.name  = "slavio_timer",
    .qdev.size  = sizeof(SLAVIO_TIMERState),
411 412
    .qdev.vmsd  = &vmstate_slavio_timer,
    .qdev.reset = slavio_timer_reset,
G
Gerd Hoffmann 已提交
413
    .qdev.props = (Property[]) {
414 415
        DEFINE_PROP_UINT32("num_cpus",  SLAVIO_TIMERState, num_cpus,  0),
        DEFINE_PROP_END_OF_LIST(),
416 417 418 419 420 421 422 423 424
    }
};

static void slavio_timer_register_devices(void)
{
    sysbus_register_withprop(&slavio_timer_info);
}

device_init(slavio_timer_register_devices)