slavio_timer.c 14.1 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"
B
bellard 已提交
28 29 30

//#define DEBUG_TIMER

B
bellard 已提交
31
#ifdef DEBUG_TIMER
32 33
#define DPRINTF(fmt, ...)                                       \
    do { printf("TIMER: " fmt , ## __VA_ARGS__); } while (0)
B
bellard 已提交
34
#else
35
#define DPRINTF(fmt, ...) do {} while (0)
B
bellard 已提交
36 37
#endif

B
bellard 已提交
38 39 40 41 42 43
/*
 * 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
44
 *
B
bellard 已提交
45 46 47
 * 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 已提交
48 49 50
 * Per-CPU timers interrupt local CPU, system timer uses normal
 * interrupt routing.
 *
B
bellard 已提交
51 52
 */

B
blueswir1 已提交
53 54
#define MAX_CPUS 16

B
Blue Swirl 已提交
55
typedef struct CPUTimerState {
56
    qemu_irq irq;
57 58 59
    ptimer_state *timer;
    uint32_t count, counthigh, reached;
    uint64_t limit;
B
blueswir1 已提交
60
    // processor only
B
blueswir1 已提交
61
    uint32_t running;
B
Blue Swirl 已提交
62 63 64 65 66 67 68
} CPUTimerState;

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

B
Blue Swirl 已提交
71 72 73 74 75
typedef struct TimerContext {
    SLAVIO_TIMERState *s;
    unsigned int timer_index; /* 0 for system, 1 ... MAX_CPUS for CPU timers */
} TimerContext;

B
blueswir1 已提交
76
#define SYS_TIMER_SIZE 0x14
B
blueswir1 已提交
77
#define CPU_TIMER_SIZE 0x10
B
bellard 已提交
78

79 80 81 82 83 84 85 86 87 88 89 90
#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
91 92
#define LIMIT_TO_PERIODS(l) (((l) >> 9) - 1)
#define PERIODS_TO_LIMIT(l) (((l) + 1) << 9)
93

B
Blue Swirl 已提交
94
static int slavio_timer_is_user(TimerContext *tc)
B
blueswir1 已提交
95
{
B
Blue Swirl 已提交
96 97 98 99
    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 已提交
100 101
}

B
bellard 已提交
102
// Update count, set irq, update expire_time
103
// Convert from ptimer countdown units
B
Blue Swirl 已提交
104
static void slavio_timer_get_out(CPUTimerState *t)
B
bellard 已提交
105
{
106
    uint64_t count, limit;
B
bellard 已提交
107

B
Blue Swirl 已提交
108
    if (t->limit == 0) { /* free-run system or processor counter */
109
        limit = TIMER_MAX_COUNT32;
B
Blue Swirl 已提交
110 111 112
    } else {
        limit = t->limit;
    }
B
Blue Swirl 已提交
113 114
    count = limit - PERIODS_TO_LIMIT(ptimer_get_count(t->timer));

B
Blue Swirl 已提交
115 116 117 118
    DPRINTF("get_out: limit %" PRIx64 " count %x%08x\n", t->limit, t->counthigh,
            t->count);
    t->count = count & TIMER_COUNT_MASK32;
    t->counthigh = count >> 32;
B
bellard 已提交
119 120 121 122 123
}

// timer callback
static void slavio_timer_irq(void *opaque)
{
B
Blue Swirl 已提交
124 125 126 127 128 129
    TimerContext *tc = opaque;
    SLAVIO_TIMERState *s = tc->s;
    CPUTimerState *t = &s->cputimer[tc->timer_index];

    slavio_timer_get_out(t);
    DPRINTF("callback: count %x%08x\n", t->counthigh, t->count);
130 131 132 133
    /* if limit is 0 (free-run), there will be no match */
    if (t->limit != 0) {
        t->reached = TIMER_REACHED;
    }
B
Blue Swirl 已提交
134 135
    /* there is no interrupt if user timer or free-run */
    if (!slavio_timer_is_user(tc) && t->limit != 0) {
B
Blue Swirl 已提交
136 137
        qemu_irq_raise(t->irq);
    }
B
bellard 已提交
138 139
}

A
Anthony Liguori 已提交
140
static uint32_t slavio_timer_mem_readl(void *opaque, target_phys_addr_t addr)
B
bellard 已提交
141
{
B
Blue Swirl 已提交
142 143
    TimerContext *tc = opaque;
    SLAVIO_TIMERState *s = tc->s;
144
    uint32_t saddr, ret;
B
Blue Swirl 已提交
145 146
    unsigned int timer_index = tc->timer_index;
    CPUTimerState *t = &s->cputimer[timer_index];
B
bellard 已提交
147

B
blueswir1 已提交
148
    saddr = addr >> 2;
B
bellard 已提交
149
    switch (saddr) {
150
    case TIMER_LIMIT:
B
blueswir1 已提交
151 152
        // read limit (system counter mode) or read most signifying
        // part of counter (user mode)
B
Blue Swirl 已提交
153
        if (slavio_timer_is_user(tc)) {
B
blueswir1 已提交
154
            // read user timer MSW
B
Blue Swirl 已提交
155 156
            slavio_timer_get_out(t);
            ret = t->counthigh | t->reached;
B
blueswir1 已提交
157 158
        } else {
            // read limit
B
blueswir1 已提交
159
            // clear irq
B
Blue Swirl 已提交
160 161 162
            qemu_irq_lower(t->irq);
            t->reached = 0;
            ret = t->limit & TIMER_LIMIT_MASK32;
B
blueswir1 已提交
163
        }
164
        break;
165
    case TIMER_COUNTER:
B
blueswir1 已提交
166 167
        // read counter and reached bit (system mode) or read lsbits
        // of counter (user mode)
B
Blue Swirl 已提交
168 169 170 171 172 173 174
        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;
        }
175
        break;
176
    case TIMER_STATUS:
B
blueswir1 已提交
177
        // only available in processor counter/timer
B
blueswir1 已提交
178
        // read start/stop status
B
Blue Swirl 已提交
179 180 181 182 183
        if (timer_index > 0) {
            ret = t->running;
        } else {
            ret = 0;
        }
184
        break;
185
    case TIMER_MODE:
B
blueswir1 已提交
186
        // only available in system counter
B
blueswir1 已提交
187
        // read user/system mode
B
Blue Swirl 已提交
188
        ret = s->cputimer_mode;
189
        break;
B
bellard 已提交
190
    default:
B
blueswir1 已提交
191
        DPRINTF("invalid read address " TARGET_FMT_plx "\n", addr);
192 193
        ret = 0;
        break;
B
bellard 已提交
194
    }
195 196 197
    DPRINTF("read " TARGET_FMT_plx " = %08x\n", addr, ret);

    return ret;
B
bellard 已提交
198 199
}

A
Anthony Liguori 已提交
200
static void slavio_timer_mem_writel(void *opaque, target_phys_addr_t addr,
201
                                    uint32_t val)
B
bellard 已提交
202
{
B
Blue Swirl 已提交
203 204
    TimerContext *tc = opaque;
    SLAVIO_TIMERState *s = tc->s;
B
bellard 已提交
205
    uint32_t saddr;
B
Blue Swirl 已提交
206 207
    unsigned int timer_index = tc->timer_index;
    CPUTimerState *t = &s->cputimer[timer_index];
B
bellard 已提交
208

209
    DPRINTF("write " TARGET_FMT_plx " %08x\n", addr, val);
B
blueswir1 已提交
210
    saddr = addr >> 2;
B
bellard 已提交
211
    switch (saddr) {
212
    case TIMER_LIMIT:
B
Blue Swirl 已提交
213
        if (slavio_timer_is_user(tc)) {
214 215
            uint64_t count;

B
blueswir1 已提交
216
            // set user counter MSW, reset counter
B
Blue Swirl 已提交
217 218 219 220
            t->limit = TIMER_MAX_COUNT64;
            t->counthigh = val & (TIMER_MAX_COUNT64 >> 32);
            t->reached = 0;
            count = ((uint64_t)t->counthigh << 32) | t->count;
221
            DPRINTF("processor %d user timer set to %016" PRIx64 "\n",
B
Blue Swirl 已提交
222
                    timer_index, count);
B
Blue Swirl 已提交
223
            ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
B
blueswir1 已提交
224 225
        } else {
            // set limit, reset counter
B
Blue Swirl 已提交
226 227 228 229 230
            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 已提交
231
                                     LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
B
Blue Swirl 已提交
232 233 234
                } else {
                    ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 1);
                }
B
blueswir1 已提交
235
            }
B
blueswir1 已提交
236
        }
B
blueswir1 已提交
237
        break;
238
    case TIMER_COUNTER:
B
Blue Swirl 已提交
239
        if (slavio_timer_is_user(tc)) {
240 241
            uint64_t count;

B
blueswir1 已提交
242
            // set user counter LSW, reset counter
B
Blue Swirl 已提交
243 244 245 246
            t->limit = TIMER_MAX_COUNT64;
            t->count = val & TIMER_MAX_COUNT64;
            t->reached = 0;
            count = ((uint64_t)t->counthigh) << 32 | t->count;
247
            DPRINTF("processor %d user timer set to %016" PRIx64 "\n",
B
Blue Swirl 已提交
248
                    timer_index, count);
B
Blue Swirl 已提交
249
            ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
B
blueswir1 已提交
250 251 252
        } else
            DPRINTF("not user timer\n");
        break;
253
    case TIMER_COUNTER_NORST:
B
blueswir1 已提交
254
        // set limit without resetting counter
B
Blue Swirl 已提交
255
        t->limit = val & TIMER_MAX_COUNT32;
B
Blue Swirl 已提交
256 257 258 259
        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 已提交
260
        }
B
blueswir1 已提交
261
        break;
262
    case TIMER_STATUS:
B
Blue Swirl 已提交
263
        if (slavio_timer_is_user(tc)) {
B
blueswir1 已提交
264
            // start/stop user counter
B
Blue Swirl 已提交
265 266 267
            if ((val & 1) && !t->running) {
                DPRINTF("processor %d user timer started\n",
                        timer_index);
B
Blue Swirl 已提交
268
                ptimer_run(t->timer, 0);
B
Blue Swirl 已提交
269 270 271 272
                t->running = 1;
            } else if (!(val & 1) && t->running) {
                DPRINTF("processor %d user timer stopped\n",
                        timer_index);
B
Blue Swirl 已提交
273
                ptimer_stop(t->timer);
B
Blue Swirl 已提交
274
                t->running = 0;
B
blueswir1 已提交
275 276 277
            }
        }
        break;
278
    case TIMER_MODE:
B
Blue Swirl 已提交
279
        if (timer_index == 0) {
B
blueswir1 已提交
280 281
            unsigned int i;

B
Blue Swirl 已提交
282
            for (i = 0; i < s->num_cpus; i++) {
283
                unsigned int processor = 1 << i;
B
Blue Swirl 已提交
284
                CPUTimerState *curr_timer = &s->cputimer[i + 1];
285 286

                // check for a change in timer mode for this processor
B
Blue Swirl 已提交
287
                if ((val & processor) != (s->cputimer_mode & processor)) {
288
                    if (val & processor) { // counter -> user timer
B
Blue Swirl 已提交
289
                        qemu_irq_lower(curr_timer->irq);
290
                        // counters are always running
B
Blue Swirl 已提交
291 292
                        ptimer_stop(curr_timer->timer);
                        curr_timer->running = 0;
293
                        // user timer limit is always the same
B
Blue Swirl 已提交
294 295 296
                        curr_timer->limit = TIMER_MAX_COUNT64;
                        ptimer_set_limit(curr_timer->timer,
                                         LIMIT_TO_PERIODS(curr_timer->limit),
B
blueswir1 已提交
297
                                         1);
298 299
                        // set this processors user timer bit in config
                        // register
B
Blue Swirl 已提交
300
                        s->cputimer_mode |= processor;
301
                        DPRINTF("processor %d changed from counter to user "
B
Blue Swirl 已提交
302
                                "timer\n", timer_index);
303 304
                    } else { // user timer -> counter
                        // stop the user timer if it is running
B
Blue Swirl 已提交
305 306 307
                        if (curr_timer->running) {
                            ptimer_stop(curr_timer->timer);
                        }
308
                        // start the counter
B
Blue Swirl 已提交
309 310
                        ptimer_run(curr_timer->timer, 0);
                        curr_timer->running = 1;
311 312
                        // clear this processors user timer bit in config
                        // register
B
Blue Swirl 已提交
313
                        s->cputimer_mode &= ~processor;
314
                        DPRINTF("processor %d changed from user timer to "
B
Blue Swirl 已提交
315
                                "counter\n", timer_index);
316
                    }
B
blueswir1 已提交
317
                }
B
blueswir1 已提交
318
            }
B
Blue Swirl 已提交
319
        } else {
B
blueswir1 已提交
320
            DPRINTF("not system timer\n");
B
Blue Swirl 已提交
321
        }
B
blueswir1 已提交
322
        break;
B
bellard 已提交
323
    default:
B
blueswir1 已提交
324
        DPRINTF("invalid write address " TARGET_FMT_plx "\n", addr);
B
blueswir1 已提交
325
        break;
B
bellard 已提交
326 327 328
    }
}

329
static CPUReadMemoryFunc * const slavio_timer_mem_read[3] = {
330 331
    NULL,
    NULL,
B
bellard 已提交
332 333 334
    slavio_timer_mem_readl,
};

335
static CPUWriteMemoryFunc * const slavio_timer_mem_write[3] = {
336 337
    NULL,
    NULL,
B
bellard 已提交
338 339 340
    slavio_timer_mem_writel,
};

341 342 343 344 345 346 347 348 349 350 351 352 353
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 已提交
354
    }
355
};
B
bellard 已提交
356

357 358 359 360 361 362 363 364 365
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 已提交
366
    }
367
};
B
bellard 已提交
368

369
static void slavio_timer_reset(DeviceState *d)
B
bellard 已提交
370
{
371
    SLAVIO_TIMERState *s = container_of(d, SLAVIO_TIMERState, busdev.qdev);
B
Blue Swirl 已提交
372 373 374 375 376 377 378 379 380 381 382 383 384 385
    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;
        if (i < s->num_cpus) {
            ptimer_set_limit(curr_timer->timer,
                             LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
            ptimer_run(curr_timer->timer, 0);
        }
        curr_timer->running = 1;
B
blueswir1 已提交
386
    }
B
Blue Swirl 已提交
387
    s->cputimer_mode = 0;
B
bellard 已提交
388 389
}

390
static int slavio_timer_init1(SysBusDevice *dev)
391 392 393
{
    int io;
    SLAVIO_TIMERState *s = FROM_SYSBUS(SLAVIO_TIMERState, dev);
394
    QEMUBH *bh;
B
Blue Swirl 已提交
395 396
    unsigned int i;
    TimerContext *tc;
B
bellard 已提交
397

B
Blue Swirl 已提交
398 399 400 401
    for (i = 0; i <= MAX_CPUS; i++) {
        tc = qemu_mallocz(sizeof(TimerContext));
        tc->s = s;
        tc->timer_index = i;
402

B
Blue Swirl 已提交
403 404 405
        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 已提交
406

B
Blue Swirl 已提交
407 408 409 410 411 412 413 414 415
        io = cpu_register_io_memory(slavio_timer_mem_read,
                                    slavio_timer_mem_write, tc);
        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);
416 417
    }

418
    return 0;
B
blueswir1 已提交
419 420
}

421 422 423 424
static SysBusDeviceInfo slavio_timer_info = {
    .init = slavio_timer_init1,
    .qdev.name  = "slavio_timer",
    .qdev.size  = sizeof(SLAVIO_TIMERState),
425 426
    .qdev.vmsd  = &vmstate_slavio_timer,
    .qdev.reset = slavio_timer_reset,
G
Gerd Hoffmann 已提交
427
    .qdev.props = (Property[]) {
428 429
        DEFINE_PROP_UINT32("num_cpus",  SLAVIO_TIMERState, num_cpus,  0),
        DEFINE_PROP_END_OF_LIST(),
430 431 432 433 434 435 436 437 438
    }
};

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

device_init(slavio_timer_register_devices)