armv7m_nvic.c 16.9 KB
Newer Older
P
pbrook 已提交
1 2 3 4 5 6
/*
 * ARM Nested Vectored Interrupt Controller
 *
 * Copyright (c) 2006-2007 CodeSourcery.
 * Written by Paul Brook
 *
M
Matthew Fernandez 已提交
7
 * This code is licensed under the GPL.
P
pbrook 已提交
8 9 10 11 12
 *
 * The ARMv7M System controller is fairly tightly tied in with the
 * NVIC.  Much of that is also implemented here.
 */

P
Paul Brook 已提交
13
#include "sysbus.h"
P
pbrook 已提交
14 15
#include "qemu-timer.h"
#include "arm-misc.h"
A
Avi Kivity 已提交
16
#include "exec-memory.h"
P
pbrook 已提交
17 18 19 20 21 22 23 24 25

#define NVIC 1

static uint32_t nvic_readl(void *opaque, uint32_t offset);
static void nvic_writel(void *opaque, uint32_t offset, uint32_t value);

#include "arm_gic.c"

typedef struct {
P
Paul Brook 已提交
26
    gic_state gic;
P
pbrook 已提交
27 28 29 30 31 32
    struct {
        uint32_t control;
        uint32_t reload;
        int64_t tick;
        QEMUTimer *timer;
    } systick;
33 34 35
    MemoryRegion sysregmem;
    MemoryRegion gic_iomem_alias;
    MemoryRegion container;
36
    uint32_t num_irq;
P
pbrook 已提交
37 38
} nvic_state;

39 40 41 42
static const uint8_t nvic_id[] = {
    0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1
};

P
pbrook 已提交
43 44 45 46 47 48 49 50
/* qemu timers run at 1GHz.   We want something closer to 1MHz.  */
#define SYSTICK_SCALE 1000ULL

#define SYSTICK_ENABLE    (1 << 0)
#define SYSTICK_TICKINT   (1 << 1)
#define SYSTICK_CLKSOURCE (1 << 2)
#define SYSTICK_COUNTFLAG (1 << 16)

51 52
int system_clock_scale;

P
pbrook 已提交
53
/* Conversion factor from qemu timer to SysTick frequencies.  */
P
pbrook 已提交
54 55 56
static inline int64_t systick_scale(nvic_state *s)
{
    if (s->systick.control & SYSTICK_CLKSOURCE)
P
pbrook 已提交
57
        return system_clock_scale;
P
pbrook 已提交
58 59 60 61 62 63 64
    else
        return 1000;
}

static void systick_reload(nvic_state *s, int reset)
{
    if (reset)
65
        s->systick.tick = qemu_get_clock_ns(vm_clock);
P
pbrook 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    s->systick.tick += (s->systick.reload + 1) * systick_scale(s);
    qemu_mod_timer(s->systick.timer, s->systick.tick);
}

static void systick_timer_tick(void * opaque)
{
    nvic_state *s = (nvic_state *)opaque;
    s->systick.control |= SYSTICK_COUNTFLAG;
    if (s->systick.control & SYSTICK_TICKINT) {
        /* Trigger the interrupt.  */
        armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK);
    }
    if (s->systick.reload == 0) {
        s->systick.control &= ~SYSTICK_ENABLE;
    } else {
        systick_reload(s, 0);
    }
}

85 86 87 88 89 90 91 92
static void systick_reset(nvic_state *s)
{
    s->systick.control = 0;
    s->systick.reload = 0;
    s->systick.tick = 0;
    qemu_del_timer(s->systick.timer);
}

P
pbrook 已提交
93 94 95 96 97 98 99
/* The external routines use the hardware vector numbering, ie. the first
   IRQ is #16.  The internal GIC routines use #32 as the first IRQ.  */
void armv7m_nvic_set_pending(void *opaque, int irq)
{
    nvic_state *s = (nvic_state *)opaque;
    if (irq >= 16)
        irq += 16;
P
Paul Brook 已提交
100
    gic_set_pending_private(&s->gic, 0, irq);
P
pbrook 已提交
101 102 103 104 105 106 107 108
}

/* Make pending IRQ active.  */
int armv7m_nvic_acknowledge_irq(void *opaque)
{
    nvic_state *s = (nvic_state *)opaque;
    uint32_t irq;

P
Paul Brook 已提交
109
    irq = gic_acknowledge_irq(&s->gic, 0);
P
pbrook 已提交
110
    if (irq == 1023)
P
Paul Brook 已提交
111
        hw_error("Interrupt but no vector\n");
P
pbrook 已提交
112 113 114 115 116 117 118 119 120 121
    if (irq >= 32)
        irq -= 16;
    return irq;
}

void armv7m_nvic_complete_irq(void *opaque, int irq)
{
    nvic_state *s = (nvic_state *)opaque;
    if (irq >= 16)
        irq += 16;
P
Paul Brook 已提交
122
    gic_complete_irq(&s->gic, 0, irq);
P
pbrook 已提交
123 124 125 126 127 128 129 130 131 132
}

static uint32_t nvic_readl(void *opaque, uint32_t offset)
{
    nvic_state *s = (nvic_state *)opaque;
    uint32_t val;
    int irq;

    switch (offset) {
    case 4: /* Interrupt Control Type.  */
133
        return (s->num_irq / 32) - 1;
P
pbrook 已提交
134 135 136 137 138 139 140 141 142 143 144
    case 0x10: /* SysTick Control and Status.  */
        val = s->systick.control;
        s->systick.control &= ~SYSTICK_COUNTFLAG;
        return val;
    case 0x14: /* SysTick Reload Value.  */
        return s->systick.reload;
    case 0x18: /* SysTick Current Value.  */
        {
            int64_t t;
            if ((s->systick.control & SYSTICK_ENABLE) == 0)
                return 0;
145
            t = qemu_get_clock_ns(vm_clock);
P
pbrook 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
            if (t >= s->systick.tick)
                return 0;
            val = ((s->systick.tick - (t + 1)) / systick_scale(s)) + 1;
            /* The interrupt in triggered when the timer reaches zero.
               However the counter is not reloaded until the next clock
               tick.  This is a hack to return zero during the first tick.  */
            if (val > s->systick.reload)
                val = 0;
            return val;
        }
    case 0x1c: /* SysTick Calibration Value.  */
        return 10000;
    case 0xd00: /* CPUID Base.  */
        return cpu_single_env->cp15.c0_cpuid;
    case 0xd04: /* Interrypt Control State.  */
        /* VECTACTIVE */
P
Paul Brook 已提交
162
        val = s->gic.running_irq[0];
P
pbrook 已提交
163 164 165 166 167 168
        if (val == 1023) {
            val = 0;
        } else if (val >= 32) {
            val -= 16;
        }
        /* RETTOBASE */
P
Paul Brook 已提交
169 170
        if (s->gic.running_irq[0] == 1023
                || s->gic.last_active[s->gic.running_irq[0]][0] == 1023) {
P
pbrook 已提交
171 172 173
            val |= (1 << 11);
        }
        /* VECTPENDING */
P
Paul Brook 已提交
174 175
        if (s->gic.current_pending[0] != 1023)
            val |= (s->gic.current_pending[0] << 12);
P
pbrook 已提交
176
        /* ISRPENDING */
177
        for (irq = 32; irq < s->num_irq; irq++) {
P
Paul Brook 已提交
178
            if (s->gic.irq_state[irq].pending) {
P
pbrook 已提交
179 180 181 182 183
                val |= (1 << 22);
                break;
            }
        }
        /* PENDSTSET */
P
Paul Brook 已提交
184
        if (s->gic.irq_state[ARMV7M_EXCP_SYSTICK].pending)
P
pbrook 已提交
185 186
            val |= (1 << 26);
        /* PENDSVSET */
P
Paul Brook 已提交
187
        if (s->gic.irq_state[ARMV7M_EXCP_PENDSV].pending)
P
pbrook 已提交
188 189
            val |= (1 << 28);
        /* NMIPENDSET */
P
Paul Brook 已提交
190
        if (s->gic.irq_state[ARMV7M_EXCP_NMI].pending)
P
pbrook 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
            val |= (1 << 31);
        return val;
    case 0xd08: /* Vector Table Offset.  */
        return cpu_single_env->v7m.vecbase;
    case 0xd0c: /* Application Interrupt/Reset Control.  */
        return 0xfa05000;
    case 0xd10: /* System Control.  */
        /* TODO: Implement SLEEPONEXIT.  */
        return 0;
    case 0xd14: /* Configuration Control.  */
        /* TODO: Implement Configuration Control bits.  */
        return 0;
    case 0xd18: case 0xd1c: case 0xd20: /* System Handler Priority.  */
        irq = offset - 0xd14;
        val = 0;
206 207 208 209
        val |= s->gic.priority1[irq++][0];
        val |= s->gic.priority1[irq++][0] << 8;
        val |= s->gic.priority1[irq++][0] << 16;
        val |= s->gic.priority1[irq][0] << 24;
P
pbrook 已提交
210 211 212
        return val;
    case 0xd24: /* System Handler Status.  */
        val = 0;
P
Paul Brook 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226
        if (s->gic.irq_state[ARMV7M_EXCP_MEM].active) val |= (1 << 0);
        if (s->gic.irq_state[ARMV7M_EXCP_BUS].active) val |= (1 << 1);
        if (s->gic.irq_state[ARMV7M_EXCP_USAGE].active) val |= (1 << 3);
        if (s->gic.irq_state[ARMV7M_EXCP_SVC].active) val |= (1 << 7);
        if (s->gic.irq_state[ARMV7M_EXCP_DEBUG].active) val |= (1 << 8);
        if (s->gic.irq_state[ARMV7M_EXCP_PENDSV].active) val |= (1 << 10);
        if (s->gic.irq_state[ARMV7M_EXCP_SYSTICK].active) val |= (1 << 11);
        if (s->gic.irq_state[ARMV7M_EXCP_USAGE].pending) val |= (1 << 12);
        if (s->gic.irq_state[ARMV7M_EXCP_MEM].pending) val |= (1 << 13);
        if (s->gic.irq_state[ARMV7M_EXCP_BUS].pending) val |= (1 << 14);
        if (s->gic.irq_state[ARMV7M_EXCP_SVC].pending) val |= (1 << 15);
        if (s->gic.irq_state[ARMV7M_EXCP_MEM].enabled) val |= (1 << 16);
        if (s->gic.irq_state[ARMV7M_EXCP_BUS].enabled) val |= (1 << 17);
        if (s->gic.irq_state[ARMV7M_EXCP_USAGE].enabled) val |= (1 << 18);
P
pbrook 已提交
227 228 229
        return val;
    case 0xd28: /* Configurable Fault Status.  */
        /* TODO: Implement Fault Status.  */
P
Paul Brook 已提交
230
        hw_error("Not implemented: Configurable Fault Status.");
P
pbrook 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
        return 0;
    case 0xd2c: /* Hard Fault Status.  */
    case 0xd30: /* Debug Fault Status.  */
    case 0xd34: /* Mem Manage Address.  */
    case 0xd38: /* Bus Fault Address.  */
    case 0xd3c: /* Aux Fault Status.  */
        /* TODO: Implement fault status registers.  */
        goto bad_reg;
    case 0xd40: /* PFR0.  */
        return 0x00000030;
    case 0xd44: /* PRF1.  */
        return 0x00000200;
    case 0xd48: /* DFR0.  */
        return 0x00100000;
    case 0xd4c: /* AFR0.  */
        return 0x00000000;
    case 0xd50: /* MMFR0.  */
        return 0x00000030;
    case 0xd54: /* MMFR1.  */
        return 0x00000000;
    case 0xd58: /* MMFR2.  */
        return 0x00000000;
    case 0xd5c: /* MMFR3.  */
        return 0x00000000;
    case 0xd60: /* ISAR0.  */
        return 0x01141110;
    case 0xd64: /* ISAR1.  */
        return 0x02111000;
    case 0xd68: /* ISAR2.  */
        return 0x21112231;
    case 0xd6c: /* ISAR3.  */
        return 0x01111110;
    case 0xd70: /* ISAR4.  */
        return 0x01310102;
    /* TODO: Implement debug registers.  */
    default:
    bad_reg:
P
Paul Brook 已提交
268
        hw_error("NVIC: Bad read offset 0x%x\n", offset);
P
pbrook 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281
    }
}

static void nvic_writel(void *opaque, uint32_t offset, uint32_t value)
{
    nvic_state *s = (nvic_state *)opaque;
    uint32_t oldval;
    switch (offset) {
    case 0x10: /* SysTick Control and Status.  */
        oldval = s->systick.control;
        s->systick.control &= 0xfffffff8;
        s->systick.control |= value & 7;
        if ((oldval ^ value) & SYSTICK_ENABLE) {
282
            int64_t now = qemu_get_clock_ns(vm_clock);
P
pbrook 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
            if (value & SYSTICK_ENABLE) {
                if (s->systick.tick) {
                    s->systick.tick += now;
                    qemu_mod_timer(s->systick.timer, s->systick.tick);
                } else {
                    systick_reload(s, 1);
                }
            } else {
                qemu_del_timer(s->systick.timer);
                s->systick.tick -= now;
                if (s->systick.tick < 0)
                  s->systick.tick = 0;
            }
        } else if ((oldval ^ value) & SYSTICK_CLKSOURCE) {
            /* This is a hack. Force the timer to be reloaded
               when the reference clock is changed.  */
            systick_reload(s, 1);
        }
        break;
    case 0x14: /* SysTick Reload Value.  */
        s->systick.reload = value;
        break;
    case 0x18: /* SysTick Current Value.  Writes reload the timer.  */
        systick_reload(s, 1);
        s->systick.control &= ~SYSTICK_COUNTFLAG;
        break;
    case 0xd04: /* Interrupt Control State.  */
        if (value & (1 << 31)) {
            armv7m_nvic_set_pending(s, ARMV7M_EXCP_NMI);
        }
        if (value & (1 << 28)) {
            armv7m_nvic_set_pending(s, ARMV7M_EXCP_PENDSV);
        } else if (value & (1 << 27)) {
P
Paul Brook 已提交
316 317
            s->gic.irq_state[ARMV7M_EXCP_PENDSV].pending = 0;
            gic_update(&s->gic);
P
pbrook 已提交
318 319 320 321
        }
        if (value & (1 << 26)) {
            armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK);
        } else if (value & (1 << 25)) {
P
Paul Brook 已提交
322 323
            s->gic.irq_state[ARMV7M_EXCP_SYSTICK].pending = 0;
            gic_update(&s->gic);
P
pbrook 已提交
324 325 326 327 328 329 330 331
        }
        break;
    case 0xd08: /* Vector Table Offset.  */
        cpu_single_env->v7m.vecbase = value & 0xffffff80;
        break;
    case 0xd0c: /* Application Interrupt/Reset Control.  */
        if ((value >> 16) == 0x05fa) {
            if (value & 2) {
P
Paul Brook 已提交
332
                hw_error("VECTCLRACTIVE not implemented");
P
pbrook 已提交
333 334
            }
            if (value & 5) {
P
Paul Brook 已提交
335
                hw_error("System reset");
P
pbrook 已提交
336 337 338 339 340 341 342 343 344 345 346
            }
        }
        break;
    case 0xd10: /* System Control.  */
    case 0xd14: /* Configuration Control.  */
        /* TODO: Implement control registers.  */
        goto bad_reg;
    case 0xd18: case 0xd1c: case 0xd20: /* System Handler Priority.  */
        {
            int irq;
            irq = offset - 0xd14;
P
Paul Brook 已提交
347 348 349 350 351
            s->gic.priority1[irq++][0] = value & 0xff;
            s->gic.priority1[irq++][0] = (value >> 8) & 0xff;
            s->gic.priority1[irq++][0] = (value >> 16) & 0xff;
            s->gic.priority1[irq][0] = (value >> 24) & 0xff;
            gic_update(&s->gic);
P
pbrook 已提交
352 353 354 355 356
        }
        break;
    case 0xd24: /* System Handler Control.  */
        /* TODO: Real hardware allows you to set/clear the active bits
           under some circumstances.  We don't implement this.  */
P
Paul Brook 已提交
357 358 359
        s->gic.irq_state[ARMV7M_EXCP_MEM].enabled = (value & (1 << 16)) != 0;
        s->gic.irq_state[ARMV7M_EXCP_BUS].enabled = (value & (1 << 17)) != 0;
        s->gic.irq_state[ARMV7M_EXCP_USAGE].enabled = (value & (1 << 18)) != 0;
P
pbrook 已提交
360 361 362 363 364 365 366 367
        break;
    case 0xd28: /* Configurable Fault Status.  */
    case 0xd2c: /* Hard Fault Status.  */
    case 0xd30: /* Debug Fault Status.  */
    case 0xd34: /* Mem Manage Address.  */
    case 0xd38: /* Bus Fault Address.  */
    case 0xd3c: /* Aux Fault Status.  */
        goto bad_reg;
368 369 370 371 372
    case 0xf00: /* Software Triggered Interrupt Register */
        if ((value & 0x1ff) < s->num_irq) {
            gic_set_pending_private(&s->gic, 0, value & 0x1ff);
        }
        break;
P
pbrook 已提交
373 374
    default:
    bad_reg:
P
Paul Brook 已提交
375
        hw_error("NVIC: Bad write offset 0x%x\n", offset);
P
pbrook 已提交
376 377 378
    }
}

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
static uint64_t nvic_sysreg_read(void *opaque, target_phys_addr_t addr,
                                 unsigned size)
{
    /* At the moment we only support the ID registers for byte/word access.
     * This is not strictly correct as a few of the other registers also
     * allow byte access.
     */
    uint32_t offset = addr;
    if (offset >= 0xfe0) {
        if (offset & 3) {
            return 0;
        }
        return nvic_id[(offset - 0xfe0) >> 2];
    }
    if (size == 4) {
        return nvic_readl(opaque, offset);
    }
    hw_error("NVIC: Bad read of size %d at offset 0x%x\n", size, offset);
}

static void nvic_sysreg_write(void *opaque, target_phys_addr_t addr,
                              uint64_t value, unsigned size)
{
    uint32_t offset = addr;
    if (size == 4) {
        nvic_writel(opaque, offset, value);
        return;
    }
    hw_error("NVIC: Bad write of size %d at offset 0x%x\n", size, offset);
}

static const MemoryRegionOps nvic_sysreg_ops = {
    .read = nvic_sysreg_read,
    .write = nvic_sysreg_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
};

J
Juan Quintela 已提交
416 417 418 419 420 421 422 423 424 425 426 427 428
static const VMStateDescription vmstate_nvic = {
    .name = "armv7m_nvic",
    .version_id = 1,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField[]) {
        VMSTATE_UINT32(systick.control, nvic_state),
        VMSTATE_UINT32(systick.reload, nvic_state),
        VMSTATE_INT64(systick.tick, nvic_state),
        VMSTATE_TIMER(systick.timer, nvic_state),
        VMSTATE_END_OF_LIST()
    }
};
P
pbrook 已提交
429

430 431 432 433
static void armv7m_nvic_reset(DeviceState *dev)
{
    nvic_state *s = FROM_SYSBUSGIC(nvic_state, sysbus_from_qdev(dev));
    gic_reset(&s->gic.busdev.qdev);
434 435 436 437 438 439 440
    /* Common GIC reset resets to disabled; the NVIC doesn't have
     * per-CPU interfaces so mark our non-existent CPU interface
     * as enabled by default.
     */
    s->gic.cpu_enabled[0] = 1;
    /* The NVIC as a whole is always enabled. */
    s->gic.enabled = 1;
441 442 443
    systick_reset(s);
}

444
static int armv7m_nvic_init(SysBusDevice *dev)
P
pbrook 已提交
445
{
P
Paul Brook 已提交
446
    nvic_state *s= FROM_SYSBUSGIC(nvic_state, dev);
P
pbrook 已提交
447

448 449
    /* The NVIC always has only one CPU */
    s->gic.num_cpu = 1;
450 451
    /* Tell the common code we're an NVIC */
    s->gic.revision = 0xffffffff;
452
    gic_init(&s->gic, s->num_irq);
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
    /* The NVIC and system controller register area looks like this:
     *  0..0xff : system control registers, including systick
     *  0x100..0xcff : GIC-like registers
     *  0xd00..0xfff : system control registers
     * We use overlaying to put the GIC like registers
     * over the top of the system control register region.
     */
    memory_region_init(&s->container, "nvic", 0x1000);
    /* The system register region goes at the bottom of the priority
     * stack as it covers the whole page.
     */
    memory_region_init_io(&s->sysregmem, &nvic_sysreg_ops, s,
                          "nvic_sysregs", 0x1000);
    memory_region_add_subregion(&s->container, 0, &s->sysregmem);
    /* Alias the GIC region so we can get only the section of it
     * we need, and layer it on top of the system register region.
     */
    memory_region_init_alias(&s->gic_iomem_alias, "nvic-gic", &s->gic.iomem,
                             0x100, 0xc00);
    memory_region_add_subregion_overlap(&s->container, 0x100, &s->gic.iomem, 1);
    /* Map the whole thing into system memory at the location required
     * by the v7M architecture.
     */
    memory_region_add_subregion(get_system_memory(), 0xe000e000, &s->container);
477
    s->systick.timer = qemu_new_timer_ns(vm_clock, systick_timer_tick, s);
478
    return 0;
P
pbrook 已提交
479
}
P
Paul Brook 已提交
480

481 482 483 484 485 486 487 488 489
static Property armv7m_nvic_properties[] = {
    /* The ARM v7m may have anything from 0 to 496 external interrupt
     * IRQ lines. We default to 64. Other boards may differ and should
     * set this property appropriately.
     */
    DEFINE_PROP_UINT32("num-irq", nvic_state, num_irq, 64),
    DEFINE_PROP_END_OF_LIST(),
};

490 491
static void armv7m_nvic_class_init(ObjectClass *klass, void *data)
{
492
    DeviceClass *dc = DEVICE_CLASS(klass);
493 494 495
    SysBusDeviceClass *sdc = SYS_BUS_DEVICE_CLASS(klass);

    sdc->init = armv7m_nvic_init;
496
    dc->vmsd  = &vmstate_nvic;
497
    dc->reset = armv7m_nvic_reset;
498
    dc->props = armv7m_nvic_properties;
499 500
}

501 502 503 504 505
static TypeInfo armv7m_nvic_info = {
    .name          = "armv7m_nvic",
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(nvic_state),
    .class_init    = armv7m_nvic_class_init,
506 507
};

A
Andreas Färber 已提交
508
static void armv7m_nvic_register_types(void)
P
Paul Brook 已提交
509
{
510
    type_register_static(&armv7m_nvic_info);
P
Paul Brook 已提交
511 512
}

A
Andreas Färber 已提交
513
type_init(armv7m_nvic_register_types)