2007-11-11 01:04:49 +01:00
|
|
|
/*
|
|
|
|
* ARM Nested Vectored Interrupt Controller
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006-2007 CodeSourcery.
|
|
|
|
* Written by Paul Brook
|
|
|
|
*
|
2011-06-26 04:21:35 +02:00
|
|
|
* This code is licensed under the GPL.
|
2007-11-11 01:04:49 +01:00
|
|
|
*
|
|
|
|
* The ARMv7M System controller is fairly tightly tied in with the
|
|
|
|
* NVIC. Much of that is also implemented here.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 19:17:05 +01:00
|
|
|
#include "qemu/osdep.h"
|
include/qemu/osdep.h: Don't include qapi/error.h
Commit 57cb38b included qapi/error.h into qemu/osdep.h to get the
Error typedef. Since then, we've moved to include qemu/osdep.h
everywhere. Its file comment explains: "To avoid getting into
possible circular include dependencies, this file should not include
any other QEMU headers, with the exceptions of config-host.h,
compiler.h, os-posix.h and os-win32.h, all of which are doing a
similar job to this file and are under similar constraints."
qapi/error.h doesn't do a similar job, and it doesn't adhere to
similar constraints: it includes qapi-types.h. That's in excess of
100KiB of crap most .c files don't actually need.
Add the typedef to qemu/typedefs.h, and include that instead of
qapi/error.h. Include qapi/error.h in .c files that need it and don't
get it now. Include qapi-types.h in qom/object.h for uint16List.
Update scripts/clean-includes accordingly. Update it further to match
reality: replace config.h by config-target.h, add sysemu/os-posix.h,
sysemu/os-win32.h. Update the list of includes in the qemu/osdep.h
comment quoted above similarly.
This reduces the number of objects depending on qapi/error.h from "all
of them" to less than a third. Unfortunately, the number depending on
qapi-types.h shrinks only a little. More work is needed for that one.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
[Fix compilation without the spice devel packages. - Paolo]
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2016-03-14 09:01:28 +01:00
|
|
|
#include "qapi/error.h"
|
2016-01-19 21:51:44 +01:00
|
|
|
#include "qemu-common.h"
|
2016-03-15 16:58:45 +01:00
|
|
|
#include "cpu.h"
|
2013-02-04 15:40:22 +01:00
|
|
|
#include "hw/sysbus.h"
|
2012-12-17 18:20:00 +01:00
|
|
|
#include "qemu/timer.h"
|
2013-04-09 16:26:55 +02:00
|
|
|
#include "hw/arm/arm.h"
|
2012-12-17 18:19:49 +01:00
|
|
|
#include "exec/address-spaces.h"
|
2013-03-18 17:36:02 +01:00
|
|
|
#include "gic_internal.h"
|
2015-12-15 13:16:16 +01:00
|
|
|
#include "qemu/log.h"
|
2007-11-11 01:04:49 +01:00
|
|
|
|
|
|
|
typedef struct {
|
2012-10-12 12:54:39 +02:00
|
|
|
GICState gic;
|
2007-11-11 01:04:49 +01:00
|
|
|
struct {
|
|
|
|
uint32_t control;
|
|
|
|
uint32_t reload;
|
|
|
|
int64_t tick;
|
|
|
|
QEMUTimer *timer;
|
|
|
|
} systick;
|
2012-05-02 18:49:39 +02:00
|
|
|
MemoryRegion sysregmem;
|
|
|
|
MemoryRegion gic_iomem_alias;
|
|
|
|
MemoryRegion container;
|
2012-01-17 11:54:07 +01:00
|
|
|
uint32_t num_irq;
|
2015-11-03 14:49:41 +01:00
|
|
|
qemu_irq sysresetreq;
|
2007-11-11 01:04:49 +01:00
|
|
|
} nvic_state;
|
|
|
|
|
2012-05-02 18:49:42 +02:00
|
|
|
#define TYPE_NVIC "armv7m_nvic"
|
|
|
|
/**
|
|
|
|
* NVICClass:
|
|
|
|
* @parent_reset: the parent class' reset handler.
|
|
|
|
*
|
|
|
|
* A model of the v7M NVIC and System Controller
|
|
|
|
*/
|
|
|
|
typedef struct NVICClass {
|
|
|
|
/*< private >*/
|
|
|
|
ARMGICClass parent_class;
|
|
|
|
/*< public >*/
|
2013-03-05 01:34:42 +01:00
|
|
|
DeviceRealize parent_realize;
|
2012-05-02 18:49:42 +02:00
|
|
|
void (*parent_reset)(DeviceState *dev);
|
|
|
|
} NVICClass;
|
|
|
|
|
|
|
|
#define NVIC_CLASS(klass) \
|
|
|
|
OBJECT_CLASS_CHECK(NVICClass, (klass), TYPE_NVIC)
|
|
|
|
#define NVIC_GET_CLASS(obj) \
|
|
|
|
OBJECT_GET_CLASS(NVICClass, (obj), TYPE_NVIC)
|
|
|
|
#define NVIC(obj) \
|
|
|
|
OBJECT_CHECK(nvic_state, (obj), TYPE_NVIC)
|
|
|
|
|
2012-05-02 18:49:39 +02:00
|
|
|
static const uint8_t nvic_id[] = {
|
|
|
|
0x00, 0xb0, 0x1b, 0x00, 0x0d, 0xe0, 0x05, 0xb1
|
|
|
|
};
|
|
|
|
|
2007-11-11 01:04:49 +01:00
|
|
|
/* 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)
|
|
|
|
|
2008-09-17 21:04:14 +02:00
|
|
|
int system_clock_scale;
|
|
|
|
|
2007-11-24 04:09:07 +01:00
|
|
|
/* Conversion factor from qemu timer to SysTick frequencies. */
|
2007-11-11 01:04:49 +01:00
|
|
|
static inline int64_t systick_scale(nvic_state *s)
|
|
|
|
{
|
|
|
|
if (s->systick.control & SYSTICK_CLKSOURCE)
|
2007-11-24 04:09:07 +01:00
|
|
|
return system_clock_scale;
|
2007-11-11 01:04:49 +01:00
|
|
|
else
|
|
|
|
return 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void systick_reload(nvic_state *s, int reset)
|
|
|
|
{
|
2015-05-12 12:57:16 +02:00
|
|
|
/* The Cortex-M3 Devices Generic User Guide says that "When the
|
|
|
|
* ENABLE bit is set to 1, the counter loads the RELOAD value from the
|
|
|
|
* SYST RVR register and then counts down". So, we need to check the
|
|
|
|
* ENABLE bit before reloading the value.
|
|
|
|
*/
|
|
|
|
if ((s->systick.control & SYSTICK_ENABLE) == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-11-11 01:04:49 +01:00
|
|
|
if (reset)
|
2013-08-21 17:03:08 +02:00
|
|
|
s->systick.tick = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
2007-11-11 01:04:49 +01:00
|
|
|
s->systick.tick += (s->systick.reload + 1) * systick_scale(s);
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_mod(s->systick.timer, s->systick.tick);
|
2007-11-11 01:04:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-13 13:39:09 +02:00
|
|
|
static void systick_reset(nvic_state *s)
|
|
|
|
{
|
|
|
|
s->systick.control = 0;
|
|
|
|
s->systick.reload = 0;
|
|
|
|
s->systick.tick = 0;
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_del(s->systick.timer);
|
2012-04-13 13:39:09 +02:00
|
|
|
}
|
|
|
|
|
2007-11-11 01:04:49 +01:00
|
|
|
/* 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;
|
2009-05-14 23:35:08 +02:00
|
|
|
gic_set_pending_private(&s->gic, 0, irq);
|
2007-11-11 01:04:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Make pending IRQ active. */
|
|
|
|
int armv7m_nvic_acknowledge_irq(void *opaque)
|
|
|
|
{
|
|
|
|
nvic_state *s = (nvic_state *)opaque;
|
|
|
|
uint32_t irq;
|
|
|
|
|
2015-05-12 12:57:18 +02:00
|
|
|
irq = gic_acknowledge_irq(&s->gic, 0, MEMTXATTRS_UNSPECIFIED);
|
2007-11-11 01:04:49 +01:00
|
|
|
if (irq == 1023)
|
2009-05-08 03:35:15 +02:00
|
|
|
hw_error("Interrupt but no vector\n");
|
2007-11-11 01:04:49 +01:00
|
|
|
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;
|
2015-05-12 12:57:18 +02:00
|
|
|
gic_complete_irq(&s->gic, 0, irq, MEMTXATTRS_UNSPECIFIED);
|
2007-11-11 01:04:49 +01:00
|
|
|
}
|
|
|
|
|
2012-10-30 08:45:07 +01:00
|
|
|
static uint32_t nvic_readl(nvic_state *s, uint32_t offset)
|
2007-11-11 01:04:49 +01:00
|
|
|
{
|
2013-05-27 05:17:50 +02:00
|
|
|
ARMCPU *cpu;
|
2007-11-11 01:04:49 +01:00
|
|
|
uint32_t val;
|
|
|
|
int irq;
|
|
|
|
|
|
|
|
switch (offset) {
|
|
|
|
case 4: /* Interrupt Control Type. */
|
2012-01-17 11:54:07 +01:00
|
|
|
return (s->num_irq / 32) - 1;
|
2007-11-11 01:04:49 +01:00
|
|
|
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;
|
2013-08-21 17:03:08 +02:00
|
|
|
t = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
2007-11-11 01:04:49 +01:00
|
|
|
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. */
|
2013-05-27 05:17:50 +02:00
|
|
|
cpu = ARM_CPU(current_cpu);
|
2014-05-01 16:24:44 +02:00
|
|
|
return cpu->midr;
|
2013-04-09 13:48:19 +02:00
|
|
|
case 0xd04: /* Interrupt Control State. */
|
2007-11-11 01:04:49 +01:00
|
|
|
/* VECTACTIVE */
|
2015-09-08 18:38:42 +02:00
|
|
|
cpu = ARM_CPU(current_cpu);
|
|
|
|
val = cpu->env.v7m.exception;
|
2007-11-11 01:04:49 +01:00
|
|
|
if (val == 1023) {
|
|
|
|
val = 0;
|
|
|
|
} else if (val >= 32) {
|
|
|
|
val -= 16;
|
|
|
|
}
|
|
|
|
/* VECTPENDING */
|
2009-05-14 23:35:08 +02:00
|
|
|
if (s->gic.current_pending[0] != 1023)
|
|
|
|
val |= (s->gic.current_pending[0] << 12);
|
2015-09-08 18:38:42 +02:00
|
|
|
/* ISRPENDING and RETTOBASE */
|
2012-01-17 11:54:07 +01:00
|
|
|
for (irq = 32; irq < s->num_irq; irq++) {
|
2009-05-14 23:35:08 +02:00
|
|
|
if (s->gic.irq_state[irq].pending) {
|
2007-11-11 01:04:49 +01:00
|
|
|
val |= (1 << 22);
|
|
|
|
break;
|
|
|
|
}
|
2015-09-08 18:38:42 +02:00
|
|
|
if (irq != cpu->env.v7m.exception && s->gic.irq_state[irq].active) {
|
|
|
|
val |= (1 << 11);
|
|
|
|
}
|
2007-11-11 01:04:49 +01:00
|
|
|
}
|
|
|
|
/* PENDSTSET */
|
2009-05-14 23:35:08 +02:00
|
|
|
if (s->gic.irq_state[ARMV7M_EXCP_SYSTICK].pending)
|
2007-11-11 01:04:49 +01:00
|
|
|
val |= (1 << 26);
|
|
|
|
/* PENDSVSET */
|
2009-05-14 23:35:08 +02:00
|
|
|
if (s->gic.irq_state[ARMV7M_EXCP_PENDSV].pending)
|
2007-11-11 01:04:49 +01:00
|
|
|
val |= (1 << 28);
|
|
|
|
/* NMIPENDSET */
|
2009-05-14 23:35:08 +02:00
|
|
|
if (s->gic.irq_state[ARMV7M_EXCP_NMI].pending)
|
2007-11-11 01:04:49 +01:00
|
|
|
val |= (1 << 31);
|
|
|
|
return val;
|
|
|
|
case 0xd08: /* Vector Table Offset. */
|
2013-05-27 05:17:50 +02:00
|
|
|
cpu = ARM_CPU(current_cpu);
|
|
|
|
return cpu->env.v7m.vecbase;
|
2007-11-11 01:04:49 +01:00
|
|
|
case 0xd0c: /* Application Interrupt/Reset Control. */
|
2014-06-19 19:06:27 +02:00
|
|
|
return 0xfa050000;
|
2007-11-11 01:04:49 +01:00
|
|
|
case 0xd10: /* System Control. */
|
|
|
|
/* TODO: Implement SLEEPONEXIT. */
|
|
|
|
return 0;
|
|
|
|
case 0xd14: /* Configuration Control. */
|
|
|
|
/* TODO: Implement Configuration Control bits. */
|
|
|
|
return 0;
|
|
|
|
case 0xd24: /* System Handler Status. */
|
|
|
|
val = 0;
|
2009-05-14 23:35:08 +02:00
|
|
|
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);
|
2007-11-11 01:04:49 +01:00
|
|
|
return val;
|
|
|
|
case 0xd28: /* Configurable Fault Status. */
|
|
|
|
/* TODO: Implement Fault Status. */
|
2012-10-30 08:45:10 +01:00
|
|
|
qemu_log_mask(LOG_UNIMP, "Configurable Fault Status unimplemented\n");
|
2007-11-11 01:04:49 +01:00
|
|
|
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. */
|
2012-10-30 08:45:10 +01:00
|
|
|
qemu_log_mask(LOG_UNIMP, "Fault status registers unimplemented\n");
|
|
|
|
return 0;
|
2007-11-11 01:04:49 +01:00
|
|
|
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:
|
2012-10-30 08:45:10 +01:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR, "NVIC: Bad read offset 0x%x\n", offset);
|
|
|
|
return 0;
|
2007-11-11 01:04:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-30 08:45:07 +01:00
|
|
|
static void nvic_writel(nvic_state *s, uint32_t offset, uint32_t value)
|
2007-11-11 01:04:49 +01:00
|
|
|
{
|
2013-05-27 05:17:50 +02:00
|
|
|
ARMCPU *cpu;
|
2007-11-11 01:04:49 +01:00
|
|
|
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) {
|
2013-08-21 17:03:08 +02:00
|
|
|
int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
2007-11-11 01:04:49 +01:00
|
|
|
if (value & SYSTICK_ENABLE) {
|
|
|
|
if (s->systick.tick) {
|
|
|
|
s->systick.tick += now;
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_mod(s->systick.timer, s->systick.tick);
|
2007-11-11 01:04:49 +01:00
|
|
|
} else {
|
|
|
|
systick_reload(s, 1);
|
|
|
|
}
|
|
|
|
} else {
|
2013-08-21 17:03:08 +02:00
|
|
|
timer_del(s->systick.timer);
|
2007-11-11 01:04:49 +01:00
|
|
|
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)) {
|
2009-05-14 23:35:08 +02:00
|
|
|
s->gic.irq_state[ARMV7M_EXCP_PENDSV].pending = 0;
|
|
|
|
gic_update(&s->gic);
|
2007-11-11 01:04:49 +01:00
|
|
|
}
|
|
|
|
if (value & (1 << 26)) {
|
|
|
|
armv7m_nvic_set_pending(s, ARMV7M_EXCP_SYSTICK);
|
|
|
|
} else if (value & (1 << 25)) {
|
2009-05-14 23:35:08 +02:00
|
|
|
s->gic.irq_state[ARMV7M_EXCP_SYSTICK].pending = 0;
|
|
|
|
gic_update(&s->gic);
|
2007-11-11 01:04:49 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xd08: /* Vector Table Offset. */
|
2013-05-27 05:17:50 +02:00
|
|
|
cpu = ARM_CPU(current_cpu);
|
|
|
|
cpu->env.v7m.vecbase = value & 0xffffff80;
|
2007-11-11 01:04:49 +01:00
|
|
|
break;
|
|
|
|
case 0xd0c: /* Application Interrupt/Reset Control. */
|
|
|
|
if ((value >> 16) == 0x05fa) {
|
2015-11-03 14:49:41 +01:00
|
|
|
if (value & 4) {
|
|
|
|
qemu_irq_pulse(s->sysresetreq);
|
|
|
|
}
|
2007-11-11 01:04:49 +01:00
|
|
|
if (value & 2) {
|
2012-10-30 08:45:10 +01:00
|
|
|
qemu_log_mask(LOG_UNIMP, "VECTCLRACTIVE unimplemented\n");
|
2007-11-11 01:04:49 +01:00
|
|
|
}
|
2015-11-03 14:49:41 +01:00
|
|
|
if (value & 1) {
|
2012-10-30 08:45:10 +01:00
|
|
|
qemu_log_mask(LOG_UNIMP, "AIRCR system reset unimplemented\n");
|
2007-11-11 01:04:49 +01:00
|
|
|
}
|
2014-06-19 19:06:27 +02:00
|
|
|
if (value & 0x700) {
|
|
|
|
qemu_log_mask(LOG_UNIMP, "PRIGROUP unimplemented\n");
|
|
|
|
}
|
2007-11-11 01:04:49 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 0xd10: /* System Control. */
|
|
|
|
case 0xd14: /* Configuration Control. */
|
|
|
|
/* TODO: Implement control registers. */
|
2012-10-30 08:45:10 +01:00
|
|
|
qemu_log_mask(LOG_UNIMP, "NVIC: SCR and CCR unimplemented\n");
|
|
|
|
break;
|
2007-11-11 01:04:49 +01:00
|
|
|
case 0xd24: /* System Handler Control. */
|
|
|
|
/* TODO: Real hardware allows you to set/clear the active bits
|
|
|
|
under some circumstances. We don't implement this. */
|
2009-05-14 23:35:08 +02:00
|
|
|
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;
|
2007-11-11 01:04:49 +01:00
|
|
|
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. */
|
2012-10-30 08:45:10 +01:00
|
|
|
qemu_log_mask(LOG_UNIMP,
|
|
|
|
"NVIC: fault status registers unimplemented\n");
|
|
|
|
break;
|
2012-05-02 18:49:39 +02:00
|
|
|
case 0xf00: /* Software Triggered Interrupt Register */
|
|
|
|
if ((value & 0x1ff) < s->num_irq) {
|
|
|
|
gic_set_pending_private(&s->gic, 0, value & 0x1ff);
|
|
|
|
}
|
|
|
|
break;
|
2007-11-11 01:04:49 +01:00
|
|
|
default:
|
2012-10-30 08:45:10 +01:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"NVIC: Bad write offset 0x%x\n", offset);
|
2007-11-11 01:04:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static uint64_t nvic_sysreg_read(void *opaque, hwaddr addr,
|
2012-05-02 18:49:39 +02:00
|
|
|
unsigned size)
|
|
|
|
{
|
2012-10-30 08:45:07 +01:00
|
|
|
nvic_state *s = (nvic_state *)opaque;
|
2012-05-02 18:49:39 +02:00
|
|
|
uint32_t offset = addr;
|
2012-10-30 08:45:07 +01:00
|
|
|
int i;
|
|
|
|
uint32_t val;
|
|
|
|
|
|
|
|
switch (offset) {
|
|
|
|
case 0xd18 ... 0xd23: /* System Handler Priority. */
|
|
|
|
val = 0;
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
val |= s->gic.priority1[(offset - 0xd14) + i][0] << (i * 8);
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
case 0xfe0 ... 0xfff: /* ID. */
|
2012-05-02 18:49:39 +02:00
|
|
|
if (offset & 3) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return nvic_id[(offset - 0xfe0) >> 2];
|
|
|
|
}
|
|
|
|
if (size == 4) {
|
2012-10-30 08:45:07 +01:00
|
|
|
return nvic_readl(s, offset);
|
2012-05-02 18:49:39 +02:00
|
|
|
}
|
2012-10-30 08:45:10 +01:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"NVIC: Bad read of size %d at offset 0x%x\n", size, offset);
|
|
|
|
return 0;
|
2012-05-02 18:49:39 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 12:30:10 +02:00
|
|
|
static void nvic_sysreg_write(void *opaque, hwaddr addr,
|
2012-05-02 18:49:39 +02:00
|
|
|
uint64_t value, unsigned size)
|
|
|
|
{
|
2012-10-30 08:45:07 +01:00
|
|
|
nvic_state *s = (nvic_state *)opaque;
|
2012-05-02 18:49:39 +02:00
|
|
|
uint32_t offset = addr;
|
2012-10-30 08:45:07 +01:00
|
|
|
int i;
|
|
|
|
|
|
|
|
switch (offset) {
|
|
|
|
case 0xd18 ... 0xd23: /* System Handler Priority. */
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
s->gic.priority1[(offset - 0xd14) + i][0] =
|
|
|
|
(value >> (i * 8)) & 0xff;
|
|
|
|
}
|
|
|
|
gic_update(&s->gic);
|
|
|
|
return;
|
|
|
|
}
|
2012-05-02 18:49:39 +02:00
|
|
|
if (size == 4) {
|
2012-10-30 08:45:07 +01:00
|
|
|
nvic_writel(s, offset, value);
|
2012-05-02 18:49:39 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-10-30 08:45:10 +01:00
|
|
|
qemu_log_mask(LOG_GUEST_ERROR,
|
|
|
|
"NVIC: Bad write of size %d at offset 0x%x\n", size, offset);
|
2012-05-02 18:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps nvic_sysreg_ops = {
|
|
|
|
.read = nvic_sysreg_read,
|
|
|
|
.write = nvic_sysreg_write,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
2010-12-02 02:17:33 +01:00
|
|
|
static const VMStateDescription vmstate_nvic = {
|
|
|
|
.name = "armv7m_nvic",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
2014-05-13 17:09:35 +02:00
|
|
|
.fields = (VMStateField[]) {
|
2010-12-02 02:17:33 +01:00
|
|
|
VMSTATE_UINT32(systick.control, nvic_state),
|
|
|
|
VMSTATE_UINT32(systick.reload, nvic_state),
|
|
|
|
VMSTATE_INT64(systick.tick, nvic_state),
|
2015-01-08 10:18:59 +01:00
|
|
|
VMSTATE_TIMER_PTR(systick.timer, nvic_state),
|
2010-12-02 02:17:33 +01:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
2008-07-02 18:48:32 +02:00
|
|
|
|
2012-04-13 13:39:09 +02:00
|
|
|
static void armv7m_nvic_reset(DeviceState *dev)
|
|
|
|
{
|
2012-05-02 18:49:42 +02:00
|
|
|
nvic_state *s = NVIC(dev);
|
|
|
|
NVICClass *nc = NVIC_GET_CLASS(s);
|
|
|
|
nc->parent_reset(dev);
|
2012-05-02 18:49:39 +02:00
|
|
|
/* Common GIC reset resets to disabled; the NVIC doesn't have
|
|
|
|
* per-CPU interfaces so mark our non-existent CPU interface
|
2012-12-11 12:30:37 +01:00
|
|
|
* as enabled by default, and with a priority mask which allows
|
|
|
|
* all interrupts through.
|
2012-05-02 18:49:39 +02:00
|
|
|
*/
|
2015-05-12 12:57:17 +02:00
|
|
|
s->gic.cpu_ctlr[0] = GICC_CTLR_EN_GRP0;
|
2012-12-11 12:30:37 +01:00
|
|
|
s->gic.priority_mask[0] = 0x100;
|
2012-05-02 18:49:39 +02:00
|
|
|
/* The NVIC as a whole is always enabled. */
|
2015-05-12 12:57:17 +02:00
|
|
|
s->gic.ctlr = 1;
|
2012-04-13 13:39:09 +02:00
|
|
|
systick_reset(s);
|
|
|
|
}
|
|
|
|
|
2013-03-05 01:34:42 +01:00
|
|
|
static void armv7m_nvic_realize(DeviceState *dev, Error **errp)
|
2007-11-11 01:04:49 +01:00
|
|
|
{
|
2012-05-02 18:49:42 +02:00
|
|
|
nvic_state *s = NVIC(dev);
|
|
|
|
NVICClass *nc = NVIC_GET_CLASS(s);
|
2014-04-25 12:44:23 +02:00
|
|
|
Error *local_err = NULL;
|
2007-11-11 01:04:49 +01:00
|
|
|
|
2012-05-02 18:49:39 +02:00
|
|
|
/* The NVIC always has only one CPU */
|
|
|
|
s->gic.num_cpu = 1;
|
2012-05-02 18:49:40 +02:00
|
|
|
/* Tell the common code we're an NVIC */
|
|
|
|
s->gic.revision = 0xffffffff;
|
2012-08-13 12:04:05 +02:00
|
|
|
s->num_irq = s->gic.num_irq;
|
2014-04-25 12:44:23 +02:00
|
|
|
nc->parent_realize(dev, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
error_propagate(errp, local_err);
|
2013-03-05 01:34:42 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-10-24 13:19:11 +02:00
|
|
|
gic_init_irqs_and_distributor(&s->gic);
|
2012-05-02 18:49:39 +02:00
|
|
|
/* 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.
|
|
|
|
*/
|
2013-06-07 03:25:08 +02:00
|
|
|
memory_region_init(&s->container, OBJECT(s), "nvic", 0x1000);
|
2012-05-02 18:49:39 +02:00
|
|
|
/* The system register region goes at the bottom of the priority
|
|
|
|
* stack as it covers the whole page.
|
|
|
|
*/
|
2013-06-07 03:25:08 +02:00
|
|
|
memory_region_init_io(&s->sysregmem, OBJECT(s), &nvic_sysreg_ops, s,
|
2012-05-02 18:49:39 +02:00
|
|
|
"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.
|
|
|
|
*/
|
2013-06-07 03:25:08 +02:00
|
|
|
memory_region_init_alias(&s->gic_iomem_alias, OBJECT(s),
|
|
|
|
"nvic-gic", &s->gic.iomem,
|
2012-05-02 18:49:39 +02:00
|
|
|
0x100, 0xc00);
|
2012-09-26 17:46:28 +02:00
|
|
|
memory_region_add_subregion_overlap(&s->container, 0x100,
|
|
|
|
&s->gic_iomem_alias, 1);
|
2012-05-02 18:49:39 +02:00
|
|
|
/* 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);
|
2013-08-21 17:03:08 +02:00
|
|
|
s->systick.timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, systick_timer_tick, s);
|
2007-11-11 01:04:49 +01:00
|
|
|
}
|
2009-05-14 23:35:08 +02:00
|
|
|
|
2012-08-13 12:04:05 +02:00
|
|
|
static void armv7m_nvic_instance_init(Object *obj)
|
|
|
|
{
|
|
|
|
/* We have a different default value for the num-irq property
|
|
|
|
* than our superclass. This function runs after qdev init
|
|
|
|
* has set the defaults from the Property array and before
|
|
|
|
* any user-specified property setting, so just modify the
|
2012-10-12 12:54:39 +02:00
|
|
|
* value in the GICState struct.
|
2012-08-13 12:04:05 +02:00
|
|
|
*/
|
2012-10-12 12:54:39 +02:00
|
|
|
GICState *s = ARM_GIC_COMMON(obj);
|
2015-11-03 14:49:41 +01:00
|
|
|
DeviceState *dev = DEVICE(obj);
|
|
|
|
nvic_state *nvic = NVIC(obj);
|
2011-12-08 04:34:16 +01:00
|
|
|
/* The ARM v7m may have anything from 0 to 496 external interrupt
|
|
|
|
* IRQ lines. We default to 64. Other boards may differ and should
|
2012-08-13 12:04:05 +02:00
|
|
|
* set the num-irq property appropriately.
|
2011-12-08 04:34:16 +01:00
|
|
|
*/
|
2012-08-13 12:04:05 +02:00
|
|
|
s->num_irq = 64;
|
2015-11-03 14:49:41 +01:00
|
|
|
qdev_init_gpio_out_named(dev, &nvic->sysresetreq, "SYSRESETREQ", 1);
|
2012-08-13 12:04:05 +02:00
|
|
|
}
|
2011-12-08 04:34:16 +01:00
|
|
|
|
2012-01-24 20:12:29 +01:00
|
|
|
static void armv7m_nvic_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2012-05-02 18:49:42 +02:00
|
|
|
NVICClass *nc = NVIC_CLASS(klass);
|
2011-12-08 04:34:16 +01:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-01-24 20:12:29 +01:00
|
|
|
|
2012-05-02 18:49:42 +02:00
|
|
|
nc->parent_reset = dc->reset;
|
2013-03-05 01:34:42 +01:00
|
|
|
nc->parent_realize = dc->realize;
|
2011-12-08 04:34:16 +01:00
|
|
|
dc->vmsd = &vmstate_nvic;
|
2012-04-13 13:39:09 +02:00
|
|
|
dc->reset = armv7m_nvic_reset;
|
2013-03-05 01:34:42 +01:00
|
|
|
dc->realize = armv7m_nvic_realize;
|
2012-01-24 20:12:29 +01:00
|
|
|
}
|
|
|
|
|
2013-01-10 16:19:07 +01:00
|
|
|
static const TypeInfo armv7m_nvic_info = {
|
2012-05-02 18:49:42 +02:00
|
|
|
.name = TYPE_NVIC,
|
|
|
|
.parent = TYPE_ARM_GIC_COMMON,
|
2012-08-13 12:04:05 +02:00
|
|
|
.instance_init = armv7m_nvic_instance_init,
|
2011-12-08 04:34:16 +01:00
|
|
|
.instance_size = sizeof(nvic_state),
|
|
|
|
.class_init = armv7m_nvic_class_init,
|
2012-05-02 18:49:42 +02:00
|
|
|
.class_size = sizeof(NVICClass),
|
2012-01-17 11:54:07 +01:00
|
|
|
};
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
static void armv7m_nvic_register_types(void)
|
2009-05-14 23:35:08 +02:00
|
|
|
{
|
2011-12-08 04:34:16 +01:00
|
|
|
type_register_static(&armv7m_nvic_info);
|
2009-05-14 23:35:08 +02:00
|
|
|
}
|
|
|
|
|
2012-02-09 15:20:55 +01:00
|
|
|
type_init(armv7m_nvic_register_types)
|