2011-12-05 19:47:49 +04:00
|
|
|
/*
|
|
|
|
* Private peripheral timer/watchdog blocks for ARM 11MPCore and A9MP
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006-2007 CodeSourcery.
|
|
|
|
* Copyright (c) 2011 Linaro Limited
|
|
|
|
* Written by Paul Brook, Peter Maydell
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:05 +03:00
|
|
|
#include "qemu/osdep.h"
|
2016-10-24 18:26:53 +03:00
|
|
|
#include "hw/ptimer.h"
|
2013-06-30 22:30:27 +04:00
|
|
|
#include "hw/timer/arm_mptimer.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 11:01:28 +03:00
|
|
|
#include "qapi/error.h"
|
2016-10-24 18:26:53 +03:00
|
|
|
#include "qemu/main-loop.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2013-06-16 19:10:28 +04:00
|
|
|
#include "qom/cpu.h"
|
2011-12-05 19:47:49 +04:00
|
|
|
|
2016-10-24 18:26:53 +03:00
|
|
|
#define PTIMER_POLICY \
|
|
|
|
(PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD | \
|
|
|
|
PTIMER_POLICY_CONTINUOUS_TRIGGER | \
|
|
|
|
PTIMER_POLICY_NO_IMMEDIATE_TRIGGER | \
|
|
|
|
PTIMER_POLICY_NO_IMMEDIATE_RELOAD | \
|
|
|
|
PTIMER_POLICY_NO_COUNTER_ROUND_DOWN)
|
|
|
|
|
2011-12-05 19:47:49 +04:00
|
|
|
/* This device implements the per-cpu private timer and watchdog block
|
|
|
|
* which is used in both the ARM11MPCore and Cortex-A9MP.
|
|
|
|
*/
|
|
|
|
|
2013-02-28 22:23:13 +04:00
|
|
|
static inline int get_current_cpu(ARMMPTimerState *s)
|
2011-12-05 19:47:49 +04:00
|
|
|
{
|
2016-10-24 18:26:53 +03:00
|
|
|
int cpu_id = current_cpu ? current_cpu->cpu_index : 0;
|
|
|
|
|
|
|
|
if (cpu_id >= s->num_cpu) {
|
2011-12-05 19:47:49 +04:00
|
|
|
hw_error("arm_mptimer: num-cpu %d but this cpu is %d!\n",
|
2016-10-24 18:26:53 +03:00
|
|
|
s->num_cpu, cpu_id);
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
2016-10-24 18:26:53 +03:00
|
|
|
|
|
|
|
return cpu_id;
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
|
|
|
|
2013-02-28 22:23:13 +04:00
|
|
|
static inline void timerblock_update_irq(TimerBlock *tb)
|
2011-12-05 19:47:49 +04:00
|
|
|
{
|
2015-07-06 04:27:12 +03:00
|
|
|
qemu_set_irq(tb->irq, tb->status && (tb->control & 4));
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return conversion factor from mpcore timer ticks to qemu timer ticks. */
|
2016-10-24 18:26:53 +03:00
|
|
|
static inline uint32_t timerblock_scale(uint32_t control)
|
2011-12-05 19:47:49 +04:00
|
|
|
{
|
2016-10-24 18:26:53 +03:00
|
|
|
return (((control >> 8) & 0xff) + 1) * 10;
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
|
|
|
|
2016-10-24 18:26:53 +03:00
|
|
|
static inline void timerblock_set_count(struct ptimer_state *timer,
|
|
|
|
uint32_t control, uint64_t *count)
|
2011-12-05 19:47:49 +04:00
|
|
|
{
|
2016-10-24 18:26:53 +03:00
|
|
|
/* PTimer would trigger interrupt for periodic timer when counter set
|
|
|
|
* to 0, MPtimer under certain condition only.
|
|
|
|
*/
|
|
|
|
if ((control & 3) == 3 && (control & 0xff00) == 0 && *count == 0) {
|
|
|
|
*count = ptimer_get_limit(timer);
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
2016-10-24 18:26:53 +03:00
|
|
|
ptimer_set_count(timer, *count);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void timerblock_run(struct ptimer_state *timer,
|
|
|
|
uint32_t control, uint32_t load)
|
|
|
|
{
|
|
|
|
if ((control & 1) && ((control & 0xff00) || load != 0)) {
|
|
|
|
ptimer_run(timer, !(control & 2));
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void timerblock_tick(void *opaque)
|
|
|
|
{
|
2013-02-28 22:23:13 +04:00
|
|
|
TimerBlock *tb = (TimerBlock *)opaque;
|
2016-10-24 18:26:53 +03:00
|
|
|
/* Periodic timer with load = 0 and prescaler != 0 would re-trigger
|
|
|
|
* IRQ after one period, otherwise it either stops or wraps around.
|
|
|
|
*/
|
|
|
|
if ((tb->control & 2) && (tb->control & 0xff00) == 0 &&
|
|
|
|
ptimer_get_limit(tb->timer) == 0) {
|
|
|
|
ptimer_stop(tb->timer);
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
2016-10-24 18:26:53 +03:00
|
|
|
tb->status = 1;
|
2011-12-05 19:47:49 +04:00
|
|
|
timerblock_update_irq(tb);
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t timerblock_read(void *opaque, hwaddr addr,
|
2011-12-05 19:47:49 +04:00
|
|
|
unsigned size)
|
|
|
|
{
|
2013-02-28 22:23:13 +04:00
|
|
|
TimerBlock *tb = (TimerBlock *)opaque;
|
2011-12-05 19:47:49 +04:00
|
|
|
switch (addr) {
|
|
|
|
case 0: /* Load */
|
2016-10-24 18:26:53 +03:00
|
|
|
return ptimer_get_limit(tb->timer);
|
2011-12-05 19:47:49 +04:00
|
|
|
case 4: /* Counter. */
|
2016-10-24 18:26:53 +03:00
|
|
|
return ptimer_get_count(tb->timer);
|
2011-12-05 19:47:49 +04:00
|
|
|
case 8: /* Control. */
|
|
|
|
return tb->control;
|
|
|
|
case 12: /* Interrupt status. */
|
|
|
|
return tb->status;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void timerblock_write(void *opaque, hwaddr addr,
|
2011-12-05 19:47:49 +04:00
|
|
|
uint64_t value, unsigned size)
|
|
|
|
{
|
2013-02-28 22:23:13 +04:00
|
|
|
TimerBlock *tb = (TimerBlock *)opaque;
|
2016-10-24 18:26:53 +03:00
|
|
|
uint32_t control = tb->control;
|
2011-12-05 19:47:49 +04:00
|
|
|
switch (addr) {
|
|
|
|
case 0: /* Load */
|
2016-10-24 18:26:53 +03:00
|
|
|
/* Setting load to 0 stops the timer without doing the tick if
|
|
|
|
* prescaler = 0.
|
|
|
|
*/
|
|
|
|
if ((control & 1) && (control & 0xff00) == 0 && value == 0) {
|
|
|
|
ptimer_stop(tb->timer);
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
2016-10-24 18:26:53 +03:00
|
|
|
ptimer_set_limit(tb->timer, value, 1);
|
|
|
|
timerblock_run(tb->timer, control, value);
|
|
|
|
break;
|
|
|
|
case 4: /* Counter. */
|
|
|
|
/* Setting counter to 0 stops the one-shot timer, or periodic with
|
|
|
|
* load = 0, without doing the tick if prescaler = 0.
|
|
|
|
*/
|
|
|
|
if ((control & 1) && (control & 0xff00) == 0 && value == 0 &&
|
|
|
|
(!(control & 2) || ptimer_get_limit(tb->timer) == 0)) {
|
|
|
|
ptimer_stop(tb->timer);
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
2016-10-24 18:26:53 +03:00
|
|
|
timerblock_set_count(tb->timer, control, &value);
|
|
|
|
timerblock_run(tb->timer, control, value);
|
2011-12-05 19:47:49 +04:00
|
|
|
break;
|
|
|
|
case 8: /* Control. */
|
2016-10-24 18:26:53 +03:00
|
|
|
if ((control & 3) != (value & 3)) {
|
|
|
|
ptimer_stop(tb->timer);
|
|
|
|
}
|
|
|
|
if ((control & 0xff00) != (value & 0xff00)) {
|
|
|
|
ptimer_set_period(tb->timer, timerblock_scale(value));
|
|
|
|
}
|
2015-07-06 01:47:47 +03:00
|
|
|
if (value & 1) {
|
2016-10-24 18:26:53 +03:00
|
|
|
uint64_t count = ptimer_get_count(tb->timer);
|
|
|
|
/* Re-load periodic timer counter if needed. */
|
|
|
|
if ((value & 2) && count == 0) {
|
|
|
|
timerblock_set_count(tb->timer, value, &count);
|
2015-07-06 01:47:47 +03:00
|
|
|
}
|
2016-10-24 18:26:53 +03:00
|
|
|
timerblock_run(tb->timer, value, count);
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
2016-10-24 18:26:53 +03:00
|
|
|
tb->control = value;
|
2011-12-05 19:47:49 +04:00
|
|
|
break;
|
|
|
|
case 12: /* Interrupt status. */
|
|
|
|
tb->status &= ~value;
|
|
|
|
timerblock_update_irq(tb);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wrapper functions to implement the "read timer/watchdog for
|
|
|
|
* the current CPU" memory regions.
|
|
|
|
*/
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t arm_thistimer_read(void *opaque, hwaddr addr,
|
2011-12-05 19:47:49 +04:00
|
|
|
unsigned size)
|
|
|
|
{
|
2013-02-28 22:23:13 +04:00
|
|
|
ARMMPTimerState *s = (ARMMPTimerState *)opaque;
|
2011-12-05 19:47:49 +04:00
|
|
|
int id = get_current_cpu(s);
|
2013-02-28 22:23:13 +04:00
|
|
|
return timerblock_read(&s->timerblock[id], addr, size);
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void arm_thistimer_write(void *opaque, hwaddr addr,
|
2011-12-05 19:47:49 +04:00
|
|
|
uint64_t value, unsigned size)
|
|
|
|
{
|
2013-02-28 22:23:13 +04:00
|
|
|
ARMMPTimerState *s = (ARMMPTimerState *)opaque;
|
2011-12-05 19:47:49 +04:00
|
|
|
int id = get_current_cpu(s);
|
2013-02-28 22:23:13 +04:00
|
|
|
timerblock_write(&s->timerblock[id], addr, value, size);
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static const MemoryRegionOps arm_thistimer_ops = {
|
|
|
|
.read = arm_thistimer_read,
|
|
|
|
.write = arm_thistimer_write,
|
|
|
|
.valid = {
|
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 4,
|
|
|
|
},
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const MemoryRegionOps timerblock_ops = {
|
|
|
|
.read = timerblock_read,
|
|
|
|
.write = timerblock_write,
|
|
|
|
.valid = {
|
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 4,
|
|
|
|
},
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
};
|
|
|
|
|
2013-02-28 22:23:13 +04:00
|
|
|
static void timerblock_reset(TimerBlock *tb)
|
2011-12-05 19:47:49 +04:00
|
|
|
{
|
|
|
|
tb->control = 0;
|
|
|
|
tb->status = 0;
|
2012-04-20 19:38:52 +04:00
|
|
|
if (tb->timer) {
|
2016-10-24 18:26:53 +03:00
|
|
|
ptimer_stop(tb->timer);
|
|
|
|
ptimer_set_limit(tb->timer, 0, 1);
|
|
|
|
ptimer_set_period(tb->timer, timerblock_scale(0));
|
2012-04-20 19:38:52 +04:00
|
|
|
}
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void arm_mptimer_reset(DeviceState *dev)
|
|
|
|
{
|
2013-06-30 21:37:10 +04:00
|
|
|
ARMMPTimerState *s = ARM_MPTIMER(dev);
|
2011-12-05 19:47:49 +04:00
|
|
|
int i;
|
2013-06-30 21:37:10 +04:00
|
|
|
|
2011-12-05 19:47:49 +04:00
|
|
|
for (i = 0; i < ARRAY_SIZE(s->timerblock); i++) {
|
|
|
|
timerblock_reset(&s->timerblock[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-30 21:42:55 +04:00
|
|
|
static void arm_mptimer_init(Object *obj)
|
2011-12-05 19:47:49 +04:00
|
|
|
{
|
2013-06-30 21:42:55 +04:00
|
|
|
ARMMPTimerState *s = ARM_MPTIMER(obj);
|
|
|
|
|
|
|
|
memory_region_init_io(&s->iomem, obj, &arm_thistimer_ops, s,
|
|
|
|
"arm_mptimer_timer", 0x20);
|
|
|
|
sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->iomem);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void arm_mptimer_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
|
2013-06-30 21:37:10 +04:00
|
|
|
ARMMPTimerState *s = ARM_MPTIMER(dev);
|
2011-12-05 19:47:49 +04:00
|
|
|
int i;
|
2013-06-30 21:37:10 +04:00
|
|
|
|
2013-06-30 22:30:27 +04:00
|
|
|
if (s->num_cpu < 1 || s->num_cpu > ARM_MPTIMER_MAX_CPUS) {
|
2015-12-17 19:35:11 +03:00
|
|
|
error_setg(errp, "num-cpu must be between 1 and %d",
|
|
|
|
ARM_MPTIMER_MAX_CPUS);
|
|
|
|
return;
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
2013-02-28 22:23:13 +04:00
|
|
|
/* We implement one timer block per CPU, and expose multiple MMIO regions:
|
2011-12-05 19:47:49 +04:00
|
|
|
* * region 0 is "timer for this core"
|
2013-02-28 22:23:13 +04:00
|
|
|
* * region 1 is "timer for core 0"
|
|
|
|
* * region 2 is "timer for core 1"
|
2011-12-05 19:47:49 +04:00
|
|
|
* and so on.
|
|
|
|
* The outgoing interrupt lines are
|
|
|
|
* * timer for core 0
|
|
|
|
* * timer for core 1
|
|
|
|
* and so on.
|
|
|
|
*/
|
2013-02-28 22:23:13 +04:00
|
|
|
for (i = 0; i < s->num_cpu; i++) {
|
2013-02-28 22:23:13 +04:00
|
|
|
TimerBlock *tb = &s->timerblock[i];
|
2016-10-24 18:26:53 +03:00
|
|
|
QEMUBH *bh = qemu_bh_new(timerblock_tick, tb);
|
|
|
|
tb->timer = ptimer_init(bh, PTIMER_POLICY);
|
2013-06-30 21:42:55 +04:00
|
|
|
sysbus_init_irq(sbd, &tb->irq);
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&tb->iomem, OBJECT(s), &timerblock_ops, tb,
|
2011-12-05 19:47:49 +04:00
|
|
|
"arm_mptimer_timerblock", 0x20);
|
2013-06-30 21:42:55 +04:00
|
|
|
sysbus_init_mmio(sbd, &tb->iomem);
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_timerblock = {
|
|
|
|
.name = "arm_mptimer_timerblock",
|
2016-10-24 18:26:53 +03:00
|
|
|
.version_id = 3,
|
|
|
|
.minimum_version_id = 3,
|
2011-12-05 19:47:49 +04:00
|
|
|
.fields = (VMStateField[]) {
|
2013-02-28 22:23:13 +04:00
|
|
|
VMSTATE_UINT32(control, TimerBlock),
|
|
|
|
VMSTATE_UINT32(status, TimerBlock),
|
2016-10-24 18:26:53 +03:00
|
|
|
VMSTATE_PTIMER(timer, TimerBlock),
|
2011-12-05 19:47:49 +04:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_arm_mptimer = {
|
|
|
|
.name = "arm_mptimer",
|
2016-10-24 18:26:53 +03:00
|
|
|
.version_id = 3,
|
|
|
|
.minimum_version_id = 3,
|
2011-12-05 19:47:49 +04:00
|
|
|
.fields = (VMStateField[]) {
|
2013-02-28 22:23:13 +04:00
|
|
|
VMSTATE_STRUCT_VARRAY_UINT32(timerblock, ARMMPTimerState, num_cpu,
|
2016-10-24 18:26:53 +03:00
|
|
|
3, vmstate_timerblock, TimerBlock),
|
2011-12-05 19:47:49 +04:00
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-08 07:34:16 +04:00
|
|
|
static Property arm_mptimer_properties[] = {
|
2013-02-28 22:23:13 +04:00
|
|
|
DEFINE_PROP_UINT32("num-cpu", ARMMPTimerState, num_cpu, 0),
|
2011-12-08 07:34:16 +04:00
|
|
|
DEFINE_PROP_END_OF_LIST()
|
|
|
|
};
|
|
|
|
|
2012-01-24 23:12:29 +04:00
|
|
|
static void arm_mptimer_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-01-24 23:12:29 +04:00
|
|
|
|
2013-06-30 21:42:55 +04:00
|
|
|
dc->realize = arm_mptimer_realize;
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->vmsd = &vmstate_arm_mptimer;
|
|
|
|
dc->reset = arm_mptimer_reset;
|
|
|
|
dc->props = arm_mptimer_properties;
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo arm_mptimer_info = {
|
2013-06-30 21:37:10 +04:00
|
|
|
.name = TYPE_ARM_MPTIMER,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
2013-02-28 22:23:13 +04:00
|
|
|
.instance_size = sizeof(ARMMPTimerState),
|
2013-06-30 21:42:55 +04:00
|
|
|
.instance_init = arm_mptimer_init,
|
2011-12-08 07:34:16 +04:00
|
|
|
.class_init = arm_mptimer_class_init,
|
2011-12-05 19:47:49 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void arm_mptimer_register_types(void)
|
2011-12-05 19:47:49 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&arm_mptimer_info);
|
2011-12-05 19:47:49 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(arm_mptimer_register_types)
|