slavio_timer.c 12.0 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.
 */
P
pbrook 已提交
24 25 26
#include "hw.h"
#include "sun4m.h"
#include "qemu-timer.h"
B
bellard 已提交
27 28 29

//#define DEBUG_TIMER

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

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

B
blueswir1 已提交
52 53
#define MAX_CPUS 16

B
bellard 已提交
54
typedef struct SLAVIO_TIMERState {
55
    qemu_irq irq;
56 57 58
    ptimer_state *timer;
    uint32_t count, counthigh, reached;
    uint64_t limit;
B
blueswir1 已提交
59 60 61 62 63
    // processor only
    int running;
    struct SLAVIO_TIMERState *master;
    int slave_index;
    // system only
64
    unsigned int num_slaves;
B
blueswir1 已提交
65 66
    struct SLAVIO_TIMERState *slave[MAX_CPUS];
    uint32_t slave_mode;
B
bellard 已提交
67 68 69
} SLAVIO_TIMERState;

#define TIMER_MAXADDR 0x1f
B
blueswir1 已提交
70
#define SYS_TIMER_SIZE 0x14
B
blueswir1 已提交
71
#define CPU_TIMER_SIZE 0x10
B
bellard 已提交
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#define SYS_TIMER_OFFSET      0x10000ULL
#define CPU_TIMER_OFFSET(cpu) (0x1000ULL * cpu)

#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
#define LIMIT_TO_PERIODS(l) ((l) >> 9)
#define PERIODS_TO_LIMIT(l) ((l) << 9)

B
blueswir1 已提交
91 92 93 94 95
static int slavio_timer_is_user(SLAVIO_TIMERState *s)
{
    return s->master && (s->master->slave_mode & (1 << s->slave_index));
}

B
bellard 已提交
96
// Update count, set irq, update expire_time
97
// Convert from ptimer countdown units
B
bellard 已提交
98 99
static void slavio_timer_get_out(SLAVIO_TIMERState *s)
{
100
    uint64_t count, limit;
B
bellard 已提交
101

102 103 104 105 106
    if (s->limit == 0) /* free-run processor or system counter */
        limit = TIMER_MAX_COUNT32;
    else
        limit = s->limit;

B
blueswir1 已提交
107 108 109 110 111
    if (s->timer)
        count = limit - PERIODS_TO_LIMIT(ptimer_get_count(s->timer));
    else
        count = 0;

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

// timer callback
static void slavio_timer_irq(void *opaque)
{
    SLAVIO_TIMERState *s = opaque;

    slavio_timer_get_out(s);
124
    DPRINTF("callback: count %x%08x\n", s->counthigh, s->count);
B
blueswir1 已提交
125
    if (!slavio_timer_is_user(s)) {
126
        s->reached = TIMER_REACHED;
B
blueswir1 已提交
127
        qemu_irq_raise(s->irq);
B
blueswir1 已提交
128
    }
B
bellard 已提交
129 130 131 132 133
}

static uint32_t slavio_timer_mem_readl(void *opaque, target_phys_addr_t addr)
{
    SLAVIO_TIMERState *s = opaque;
134
    uint32_t saddr, ret;
B
bellard 已提交
135 136 137

    saddr = (addr & TIMER_MAXADDR) >> 2;
    switch (saddr) {
138
    case TIMER_LIMIT:
B
blueswir1 已提交
139 140
        // read limit (system counter mode) or read most signifying
        // part of counter (user mode)
B
blueswir1 已提交
141 142 143 144 145 146
        if (slavio_timer_is_user(s)) {
            // read user timer MSW
            slavio_timer_get_out(s);
            ret = s->counthigh;
        } else {
            // read limit
B
blueswir1 已提交
147
            // clear irq
148
            qemu_irq_lower(s->irq);
B
blueswir1 已提交
149
            s->reached = 0;
150
            ret = s->limit & TIMER_LIMIT_MASK32;
B
blueswir1 已提交
151
        }
152
        break;
153
    case TIMER_COUNTER:
B
blueswir1 已提交
154 155 156
        // read counter and reached bit (system mode) or read lsbits
        // of counter (user mode)
        slavio_timer_get_out(s);
B
blueswir1 已提交
157
        if (slavio_timer_is_user(s)) // read user timer LSW
158
            ret = s->count & TIMER_COUNT_MASK32;
B
blueswir1 已提交
159
        else // read limit
160
            ret = (s->count & TIMER_MAX_COUNT32) | s->reached;
161
        break;
162
    case TIMER_STATUS:
B
blueswir1 已提交
163
        // only available in processor counter/timer
B
blueswir1 已提交
164
        // read start/stop status
B
blueswir1 已提交
165
        ret = s->running;
166
        break;
167
    case TIMER_MODE:
B
blueswir1 已提交
168
        // only available in system counter
B
blueswir1 已提交
169
        // read user/system mode
B
blueswir1 已提交
170
        ret = s->slave_mode;
171
        break;
B
bellard 已提交
172
    default:
B
blueswir1 已提交
173
        DPRINTF("invalid read address " TARGET_FMT_plx "\n", addr);
174 175
        ret = 0;
        break;
B
bellard 已提交
176
    }
177 178 179
    DPRINTF("read " TARGET_FMT_plx " = %08x\n", addr, ret);

    return ret;
B
bellard 已提交
180 181
}

182 183
static void slavio_timer_mem_writel(void *opaque, target_phys_addr_t addr,
                                    uint32_t val)
B
bellard 已提交
184 185 186 187
{
    SLAVIO_TIMERState *s = opaque;
    uint32_t saddr;

188
    DPRINTF("write " TARGET_FMT_plx " %08x\n", addr, val);
B
bellard 已提交
189 190
    saddr = (addr & TIMER_MAXADDR) >> 2;
    switch (saddr) {
191
    case TIMER_LIMIT:
B
blueswir1 已提交
192 193
        if (slavio_timer_is_user(s)) {
            // set user counter MSW, reset counter
B
blueswir1 已提交
194
            qemu_irq_lower(s->irq);
195
            s->limit = TIMER_MAX_COUNT64;
B
blueswir1 已提交
196
            DPRINTF("processor %d user timer reset\n", s->slave_index);
B
blueswir1 已提交
197 198
            if (s->timer)
                ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(s->limit), 1);
B
blueswir1 已提交
199 200 201
        } else {
            // set limit, reset counter
            qemu_irq_lower(s->irq);
202
            s->limit = val & TIMER_MAX_COUNT32;
B
blueswir1 已提交
203 204 205 206 207 208
            if (s->timer) {
                if (s->limit == 0) /* free-run */
                    ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
                else
                    ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(s->limit), 1);
            }
B
blueswir1 已提交
209
        }
B
blueswir1 已提交
210
        break;
211
    case TIMER_COUNTER:
B
blueswir1 已提交
212 213 214
        if (slavio_timer_is_user(s)) {
            // set user counter LSW, reset counter
            qemu_irq_lower(s->irq);
215
            s->limit = TIMER_MAX_COUNT64;
B
blueswir1 已提交
216
            DPRINTF("processor %d user timer reset\n", s->slave_index);
B
blueswir1 已提交
217 218
            if (s->timer)
                ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(s->limit), 1);
B
blueswir1 已提交
219 220 221
        } else
            DPRINTF("not user timer\n");
        break;
222
    case TIMER_COUNTER_NORST:
B
blueswir1 已提交
223
        // set limit without resetting counter
224
        s->limit = val & TIMER_MAX_COUNT32;
B
blueswir1 已提交
225 226 227 228 229 230
        if (s->timer) {
            if (s->limit == 0)	/* free-run */
                ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 0);
            else
                ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(s->limit), 0);
        }
B
blueswir1 已提交
231
        break;
232
    case TIMER_STATUS:
B
blueswir1 已提交
233 234 235 236
        if (slavio_timer_is_user(s)) {
            // start/stop user counter
            if ((val & 1) && !s->running) {
                DPRINTF("processor %d user timer started\n", s->slave_index);
B
blueswir1 已提交
237 238
                if (s->timer)
                    ptimer_run(s->timer, 0);
B
blueswir1 已提交
239 240 241
                s->running = 1;
            } else if (!(val & 1) && s->running) {
                DPRINTF("processor %d user timer stopped\n", s->slave_index);
B
blueswir1 已提交
242 243
                if (s->timer)
                    ptimer_stop(s->timer);
B
blueswir1 已提交
244
                s->running = 0;
B
blueswir1 已提交
245 246 247
            }
        }
        break;
248
    case TIMER_MODE:
B
blueswir1 已提交
249
        if (s->master == NULL) {
B
blueswir1 已提交
250 251
            unsigned int i;

252
            for (i = 0; i < s->num_slaves; i++) {
B
blueswir1 已提交
253 254 255
                if (val & (1 << i)) {
                    qemu_irq_lower(s->slave[i]->irq);
                    s->slave[i]->limit = -1ULL;
B
blueswir1 已提交
256 257
                } else {
                    ptimer_stop(s->slave[i]->timer);
B
blueswir1 已提交
258
                }
B
blueswir1 已提交
259 260
                if ((val & (1 << i)) != (s->slave_mode & (1 << i))) {
                    ptimer_stop(s->slave[i]->timer);
261 262 263 264
                    ptimer_set_limit(s->slave[i]->timer,
                                     LIMIT_TO_PERIODS(s->slave[i]->limit), 1);
                    DPRINTF("processor %d timer changed\n",
                            s->slave[i]->slave_index);
B
blueswir1 已提交
265 266
                    ptimer_run(s->slave[i]->timer, 0);
                }
B
blueswir1 已提交
267
            }
268
            s->slave_mode = val & ((1 << s->num_slaves) - 1);
B
blueswir1 已提交
269 270
        } else
            DPRINTF("not system timer\n");
B
blueswir1 已提交
271
        break;
B
bellard 已提交
272
    default:
B
blueswir1 已提交
273
        DPRINTF("invalid write address " TARGET_FMT_plx "\n", addr);
B
blueswir1 已提交
274
        break;
B
bellard 已提交
275 276 277 278
    }
}

static CPUReadMemoryFunc *slavio_timer_mem_read[3] = {
279 280
    NULL,
    NULL,
B
bellard 已提交
281 282 283 284
    slavio_timer_mem_readl,
};

static CPUWriteMemoryFunc *slavio_timer_mem_write[3] = {
285 286
    NULL,
    NULL,
B
bellard 已提交
287 288 289 290 291 292 293
    slavio_timer_mem_writel,
};

static void slavio_timer_save(QEMUFile *f, void *opaque)
{
    SLAVIO_TIMERState *s = opaque;

294
    qemu_put_be64s(f, &s->limit);
B
bellard 已提交
295 296 297
    qemu_put_be32s(f, &s->count);
    qemu_put_be32s(f, &s->counthigh);
    qemu_put_be32s(f, &s->reached);
B
blueswir1 已提交
298
    qemu_put_be32s(f, &s->running);
B
blueswir1 已提交
299 300
    if (s->timer)
        qemu_put_ptimer(f, s->timer);
B
bellard 已提交
301 302 303 304 305
}

static int slavio_timer_load(QEMUFile *f, void *opaque, int version_id)
{
    SLAVIO_TIMERState *s = opaque;
306

B
blueswir1 已提交
307
    if (version_id != 3)
B
bellard 已提交
308 309
        return -EINVAL;

310
    qemu_get_be64s(f, &s->limit);
B
bellard 已提交
311 312 313
    qemu_get_be32s(f, &s->count);
    qemu_get_be32s(f, &s->counthigh);
    qemu_get_be32s(f, &s->reached);
B
blueswir1 已提交
314
    qemu_get_be32s(f, &s->running);
B
blueswir1 已提交
315 316
    if (s->timer)
        qemu_get_ptimer(f, s->timer);
317

B
bellard 已提交
318 319 320 321 322 323 324
    return 0;
}

static void slavio_timer_reset(void *opaque)
{
    SLAVIO_TIMERState *s = opaque;

325
    s->limit = 0;
B
bellard 已提交
326 327
    s->count = 0;
    s->reached = 0;
328
    s->slave_mode = 0;
B
blueswir1 已提交
329 330 331 332
    if (!s->master || s->slave_index < s->master->num_slaves) {
        ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
        ptimer_run(s->timer, 0);
    }
B
blueswir1 已提交
333
    s->running = 1;
334
    qemu_irq_lower(s->irq);
B
bellard 已提交
335 336
}

B
blueswir1 已提交
337
static SLAVIO_TIMERState *slavio_timer_init(target_phys_addr_t addr,
B
blueswir1 已提交
338 339 340
                                            qemu_irq irq,
                                            SLAVIO_TIMERState *master,
                                            int slave_index)
B
bellard 已提交
341 342 343
{
    int slavio_timer_io_memory;
    SLAVIO_TIMERState *s;
344
    QEMUBH *bh;
B
bellard 已提交
345 346 347

    s = qemu_mallocz(sizeof(SLAVIO_TIMERState));
    if (!s)
B
blueswir1 已提交
348
        return s;
B
bellard 已提交
349
    s->irq = irq;
B
blueswir1 已提交
350 351
    s->master = master;
    s->slave_index = slave_index;
B
blueswir1 已提交
352 353 354 355 356
    if (!master || slave_index < master->num_slaves) {
        bh = qemu_bh_new(slavio_timer_irq, s);
        s->timer = ptimer_init(bh);
        ptimer_set_period(s->timer, TIMER_PERIOD);
    }
B
bellard 已提交
357 358

    slavio_timer_io_memory = cpu_register_io_memory(0, slavio_timer_mem_read,
B
blueswir1 已提交
359
                                                    slavio_timer_mem_write, s);
B
blueswir1 已提交
360
    if (master)
361 362
        cpu_register_physical_memory(addr, CPU_TIMER_SIZE,
                                     slavio_timer_io_memory);
B
blueswir1 已提交
363
    else
364 365
        cpu_register_physical_memory(addr, SYS_TIMER_SIZE,
                                     slavio_timer_io_memory);
B
blueswir1 已提交
366
    register_savevm("slavio_timer", addr, 3, slavio_timer_save,
367
                    slavio_timer_load, s);
B
bellard 已提交
368 369
    qemu_register_reset(slavio_timer_reset, s);
    slavio_timer_reset(s);
B
blueswir1 已提交
370 371 372 373 374

    return s;
}

void slavio_timer_init_all(target_phys_addr_t base, qemu_irq master_irq,
375
                           qemu_irq *cpu_irqs, unsigned int num_cpus)
B
blueswir1 已提交
376 377 378 379
{
    SLAVIO_TIMERState *master;
    unsigned int i;

380
    master = slavio_timer_init(base + SYS_TIMER_OFFSET, master_irq, NULL, 0);
B
blueswir1 已提交
381

382 383
    master->num_slaves = num_cpus;

B
blueswir1 已提交
384 385
    for (i = 0; i < MAX_CPUS; i++) {
        master->slave[i] = slavio_timer_init(base + (target_phys_addr_t)
386
                                             CPU_TIMER_OFFSET(i),
B
blueswir1 已提交
387
                                             cpu_irqs[i], master, i);
B
blueswir1 已提交
388
    }
B
bellard 已提交
389
}