2004-12-20 02:18:01 +03:00
|
|
|
/*
|
|
|
|
* QEMU Sparc SLAVIO timer controller emulation
|
|
|
|
*
|
2005-04-07 00:47:48 +04:00
|
|
|
* Copyright (c) 2003-2005 Fabrice Bellard
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2004-12-20 02:18:01 +03:00
|
|
|
* 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.
|
|
|
|
*/
|
2009-07-15 12:53:09 +04:00
|
|
|
|
2016-01-26 21:17:18 +03:00
|
|
|
#include "qemu/osdep.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/timer.h"
|
2019-08-12 08:23:42 +03:00
|
|
|
#include "hw/irq.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/ptimer.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2013-02-04 18:40:22 +04:00
|
|
|
#include "hw/sysbus.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2010-10-31 12:24:14 +03:00
|
|
|
#include "trace.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2005-04-07 00:47:48 +04:00
|
|
|
|
2004-12-20 02:18:01 +03:00
|
|
|
/*
|
|
|
|
* 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
|
2007-09-17 01:08:06 +04:00
|
|
|
*
|
2004-12-20 02:18:01 +03:00
|
|
|
* 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.
|
|
|
|
*
|
2005-12-05 23:31:52 +03:00
|
|
|
* Per-CPU timers interrupt local CPU, system timer uses normal
|
|
|
|
* interrupt routing.
|
|
|
|
*
|
2004-12-20 02:18:01 +03:00
|
|
|
*/
|
|
|
|
|
2007-10-06 15:25:43 +04:00
|
|
|
#define MAX_CPUS 16
|
|
|
|
|
2009-08-09 00:08:15 +04:00
|
|
|
typedef struct CPUTimerState {
|
2007-05-27 20:37:49 +04:00
|
|
|
qemu_irq irq;
|
2007-05-24 23:48:41 +04:00
|
|
|
ptimer_state *timer;
|
|
|
|
uint32_t count, counthigh, reached;
|
2011-08-07 23:00:23 +04:00
|
|
|
/* processor only */
|
2014-02-23 02:54:53 +04:00
|
|
|
uint32_t run;
|
2011-08-07 23:00:23 +04:00
|
|
|
uint64_t limit;
|
2009-08-09 00:08:15 +04:00
|
|
|
} CPUTimerState;
|
|
|
|
|
2013-07-27 17:24:22 +04:00
|
|
|
#define TYPE_SLAVIO_TIMER "slavio_timer"
|
2020-09-16 21:25:19 +03:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(SLAVIO_TIMERState, SLAVIO_TIMER)
|
2013-07-27 17:24:22 +04:00
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct SLAVIO_TIMERState {
|
2013-07-27 17:24:22 +04:00
|
|
|
SysBusDevice parent_obj;
|
|
|
|
|
2009-08-09 00:08:15 +04:00
|
|
|
uint32_t num_cpus;
|
|
|
|
uint32_t cputimer_mode;
|
2011-08-07 23:00:23 +04:00
|
|
|
CPUTimerState cputimer[MAX_CPUS + 1];
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2004-12-20 02:18:01 +03:00
|
|
|
|
2009-08-09 00:08:15 +04:00
|
|
|
typedef struct TimerContext {
|
2011-11-15 15:14:02 +04:00
|
|
|
MemoryRegion iomem;
|
2009-08-09 00:08:15 +04:00
|
|
|
SLAVIO_TIMERState *s;
|
|
|
|
unsigned int timer_index; /* 0 for system, 1 ... MAX_CPUS for CPU timers */
|
|
|
|
} TimerContext;
|
|
|
|
|
2007-10-07 14:00:55 +04:00
|
|
|
#define SYS_TIMER_SIZE 0x14
|
2007-10-06 15:25:43 +04:00
|
|
|
#define CPU_TIMER_SIZE 0x10
|
2004-12-20 02:18:01 +03:00
|
|
|
|
2007-12-01 18:58:22 +03:00
|
|
|
#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
|
2010-04-03 10:17:35 +04:00
|
|
|
#define LIMIT_TO_PERIODS(l) (((l) >> 9) - 1)
|
|
|
|
#define PERIODS_TO_LIMIT(l) (((l) + 1) << 9)
|
2007-12-01 18:58:22 +03:00
|
|
|
|
2009-08-09 00:08:15 +04:00
|
|
|
static int slavio_timer_is_user(TimerContext *tc)
|
2007-10-07 14:00:55 +04:00
|
|
|
{
|
2009-08-09 00:08:15 +04:00
|
|
|
SLAVIO_TIMERState *s = tc->s;
|
|
|
|
unsigned int timer_index = tc->timer_index;
|
|
|
|
|
|
|
|
return timer_index != 0 && (s->cputimer_mode & (1 << (timer_index - 1)));
|
2007-10-07 14:00:55 +04:00
|
|
|
}
|
|
|
|
|
2004-12-20 02:18:01 +03:00
|
|
|
// Update count, set irq, update expire_time
|
2007-05-24 23:48:41 +04:00
|
|
|
// Convert from ptimer countdown units
|
2009-08-09 00:08:15 +04:00
|
|
|
static void slavio_timer_get_out(CPUTimerState *t)
|
2004-12-20 02:18:01 +03:00
|
|
|
{
|
2007-12-19 20:58:24 +03:00
|
|
|
uint64_t count, limit;
|
2004-12-20 02:18:01 +03:00
|
|
|
|
2009-08-09 00:08:15 +04:00
|
|
|
if (t->limit == 0) { /* free-run system or processor counter */
|
2007-12-19 20:58:24 +03:00
|
|
|
limit = TIMER_MAX_COUNT32;
|
2009-08-09 00:08:15 +04:00
|
|
|
} else {
|
|
|
|
limit = t->limit;
|
|
|
|
}
|
2009-08-31 23:30:17 +04:00
|
|
|
count = limit - PERIODS_TO_LIMIT(ptimer_get_count(t->timer));
|
|
|
|
|
2010-10-31 12:24:14 +03:00
|
|
|
trace_slavio_timer_get_out(t->limit, t->counthigh, t->count);
|
2009-08-09 00:08:15 +04:00
|
|
|
t->count = count & TIMER_COUNT_MASK32;
|
|
|
|
t->counthigh = count >> 32;
|
2004-12-20 02:18:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// timer callback
|
|
|
|
static void slavio_timer_irq(void *opaque)
|
|
|
|
{
|
2009-08-09 00:08:15 +04:00
|
|
|
TimerContext *tc = opaque;
|
|
|
|
SLAVIO_TIMERState *s = tc->s;
|
|
|
|
CPUTimerState *t = &s->cputimer[tc->timer_index];
|
|
|
|
|
|
|
|
slavio_timer_get_out(t);
|
2010-10-31 12:24:14 +03:00
|
|
|
trace_slavio_timer_irq(t->counthigh, t->count);
|
2010-04-03 10:17:35 +04:00
|
|
|
/* if limit is 0 (free-run), there will be no match */
|
|
|
|
if (t->limit != 0) {
|
|
|
|
t->reached = TIMER_REACHED;
|
|
|
|
}
|
2010-01-24 17:28:21 +03:00
|
|
|
/* there is no interrupt if user timer or free-run */
|
|
|
|
if (!slavio_timer_is_user(tc) && t->limit != 0) {
|
2009-08-09 00:08:15 +04:00
|
|
|
qemu_irq_raise(t->irq);
|
|
|
|
}
|
2004-12-20 02:18:01 +03:00
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t slavio_timer_mem_readl(void *opaque, hwaddr addr,
|
2011-11-15 15:14:02 +04:00
|
|
|
unsigned size)
|
2004-12-20 02:18:01 +03:00
|
|
|
{
|
2009-08-09 00:08:15 +04:00
|
|
|
TimerContext *tc = opaque;
|
|
|
|
SLAVIO_TIMERState *s = tc->s;
|
2007-05-24 23:48:41 +04:00
|
|
|
uint32_t saddr, ret;
|
2009-08-09 00:08:15 +04:00
|
|
|
unsigned int timer_index = tc->timer_index;
|
|
|
|
CPUTimerState *t = &s->cputimer[timer_index];
|
2004-12-20 02:18:01 +03:00
|
|
|
|
2008-12-02 20:47:02 +03:00
|
|
|
saddr = addr >> 2;
|
2004-12-20 02:18:01 +03:00
|
|
|
switch (saddr) {
|
2007-12-01 18:58:22 +03:00
|
|
|
case TIMER_LIMIT:
|
2007-10-06 15:28:21 +04:00
|
|
|
// read limit (system counter mode) or read most signifying
|
|
|
|
// part of counter (user mode)
|
2009-08-09 00:08:15 +04:00
|
|
|
if (slavio_timer_is_user(tc)) {
|
2007-10-07 14:00:55 +04:00
|
|
|
// read user timer MSW
|
2009-08-09 00:08:15 +04:00
|
|
|
slavio_timer_get_out(t);
|
|
|
|
ret = t->counthigh | t->reached;
|
2007-10-07 14:00:55 +04:00
|
|
|
} else {
|
|
|
|
// read limit
|
2007-10-06 15:28:21 +04:00
|
|
|
// clear irq
|
2009-08-09 00:08:15 +04:00
|
|
|
qemu_irq_lower(t->irq);
|
|
|
|
t->reached = 0;
|
|
|
|
ret = t->limit & TIMER_LIMIT_MASK32;
|
2007-10-06 15:28:21 +04:00
|
|
|
}
|
2007-05-24 23:48:41 +04:00
|
|
|
break;
|
2007-12-01 18:58:22 +03:00
|
|
|
case TIMER_COUNTER:
|
2007-10-06 15:28:21 +04:00
|
|
|
// read counter and reached bit (system mode) or read lsbits
|
|
|
|
// of counter (user mode)
|
2009-08-09 00:08:15 +04:00
|
|
|
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;
|
|
|
|
}
|
2007-05-24 23:48:41 +04:00
|
|
|
break;
|
2007-12-01 18:58:22 +03:00
|
|
|
case TIMER_STATUS:
|
2007-10-07 14:00:55 +04:00
|
|
|
// only available in processor counter/timer
|
2007-10-06 15:28:21 +04:00
|
|
|
// read start/stop status
|
2009-08-09 00:08:15 +04:00
|
|
|
if (timer_index > 0) {
|
2014-02-23 02:54:53 +04:00
|
|
|
ret = t->run;
|
2009-08-09 00:08:15 +04:00
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
}
|
2007-05-24 23:48:41 +04:00
|
|
|
break;
|
2007-12-01 18:58:22 +03:00
|
|
|
case TIMER_MODE:
|
2007-10-07 14:00:55 +04:00
|
|
|
// only available in system counter
|
2007-10-06 15:28:21 +04:00
|
|
|
// read user/system mode
|
2009-08-09 00:08:15 +04:00
|
|
|
ret = s->cputimer_mode;
|
2007-05-24 23:48:41 +04:00
|
|
|
break;
|
2004-12-20 02:18:01 +03:00
|
|
|
default:
|
2010-10-31 12:24:14 +03:00
|
|
|
trace_slavio_timer_mem_readl_invalid(addr);
|
2007-05-24 23:48:41 +04:00
|
|
|
ret = 0;
|
|
|
|
break;
|
2004-12-20 02:18:01 +03:00
|
|
|
}
|
2010-10-31 12:24:14 +03:00
|
|
|
trace_slavio_timer_mem_readl(addr, ret);
|
2007-05-24 23:48:41 +04:00
|
|
|
return ret;
|
2004-12-20 02:18:01 +03:00
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void slavio_timer_mem_writel(void *opaque, hwaddr addr,
|
2011-11-15 15:14:02 +04:00
|
|
|
uint64_t val, unsigned size)
|
2004-12-20 02:18:01 +03:00
|
|
|
{
|
2009-08-09 00:08:15 +04:00
|
|
|
TimerContext *tc = opaque;
|
|
|
|
SLAVIO_TIMERState *s = tc->s;
|
2004-12-20 02:18:01 +03:00
|
|
|
uint32_t saddr;
|
2009-08-09 00:08:15 +04:00
|
|
|
unsigned int timer_index = tc->timer_index;
|
|
|
|
CPUTimerState *t = &s->cputimer[timer_index];
|
2004-12-20 02:18:01 +03:00
|
|
|
|
2010-10-31 12:24:14 +03:00
|
|
|
trace_slavio_timer_mem_writel(addr, val);
|
2008-12-02 20:47:02 +03:00
|
|
|
saddr = addr >> 2;
|
2004-12-20 02:18:01 +03:00
|
|
|
switch (saddr) {
|
2007-12-01 18:58:22 +03:00
|
|
|
case TIMER_LIMIT:
|
2019-10-21 16:43:57 +03:00
|
|
|
ptimer_transaction_begin(t->timer);
|
2009-08-09 00:08:15 +04:00
|
|
|
if (slavio_timer_is_user(tc)) {
|
2008-01-25 22:51:27 +03:00
|
|
|
uint64_t count;
|
|
|
|
|
2007-10-07 14:00:55 +04:00
|
|
|
// set user counter MSW, reset counter
|
2009-08-09 00:08:15 +04:00
|
|
|
t->limit = TIMER_MAX_COUNT64;
|
|
|
|
t->counthigh = val & (TIMER_MAX_COUNT64 >> 32);
|
|
|
|
t->reached = 0;
|
|
|
|
count = ((uint64_t)t->counthigh << 32) | t->count;
|
2010-10-31 12:24:14 +03:00
|
|
|
trace_slavio_timer_mem_writel_limit(timer_index, count);
|
2009-08-31 23:30:17 +04:00
|
|
|
ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
|
2007-10-07 14:00:55 +04:00
|
|
|
} else {
|
|
|
|
// set limit, reset counter
|
2009-08-09 00:08:15 +04:00
|
|
|
qemu_irq_lower(t->irq);
|
|
|
|
t->limit = val & TIMER_MAX_COUNT32;
|
2019-10-21 16:43:55 +03:00
|
|
|
if (t->limit == 0) { /* free-run */
|
|
|
|
ptimer_set_limit(t->timer,
|
|
|
|
LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
|
|
|
|
} else {
|
|
|
|
ptimer_set_limit(t->timer, LIMIT_TO_PERIODS(t->limit), 1);
|
2007-12-27 23:23:20 +03:00
|
|
|
}
|
2007-10-06 15:25:43 +04:00
|
|
|
}
|
2019-10-21 16:43:57 +03:00
|
|
|
ptimer_transaction_commit(t->timer);
|
2007-10-07 14:00:55 +04:00
|
|
|
break;
|
2007-12-01 18:58:22 +03:00
|
|
|
case TIMER_COUNTER:
|
2009-08-09 00:08:15 +04:00
|
|
|
if (slavio_timer_is_user(tc)) {
|
2008-01-25 22:51:27 +03:00
|
|
|
uint64_t count;
|
|
|
|
|
2007-10-07 14:00:55 +04:00
|
|
|
// set user counter LSW, reset counter
|
2009-08-09 00:08:15 +04:00
|
|
|
t->limit = TIMER_MAX_COUNT64;
|
|
|
|
t->count = val & TIMER_MAX_COUNT64;
|
|
|
|
t->reached = 0;
|
|
|
|
count = ((uint64_t)t->counthigh) << 32 | t->count;
|
2010-10-31 12:24:14 +03:00
|
|
|
trace_slavio_timer_mem_writel_limit(timer_index, count);
|
2019-10-21 16:43:57 +03:00
|
|
|
ptimer_transaction_begin(t->timer);
|
2009-08-31 23:30:17 +04:00
|
|
|
ptimer_set_count(t->timer, LIMIT_TO_PERIODS(t->limit - count));
|
2019-10-21 16:43:57 +03:00
|
|
|
ptimer_transaction_commit(t->timer);
|
2010-10-31 12:24:14 +03:00
|
|
|
} else {
|
|
|
|
trace_slavio_timer_mem_writel_counter_invalid();
|
|
|
|
}
|
2007-10-07 14:00:55 +04:00
|
|
|
break;
|
2007-12-01 18:58:22 +03:00
|
|
|
case TIMER_COUNTER_NORST:
|
2007-10-06 15:28:21 +04:00
|
|
|
// set limit without resetting counter
|
2009-08-09 00:08:15 +04:00
|
|
|
t->limit = val & TIMER_MAX_COUNT32;
|
2019-10-21 16:43:57 +03:00
|
|
|
ptimer_transaction_begin(t->timer);
|
2009-08-31 23:30:17 +04:00
|
|
|
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);
|
2007-12-27 23:23:20 +03:00
|
|
|
}
|
2019-10-21 16:43:57 +03:00
|
|
|
ptimer_transaction_commit(t->timer);
|
2007-10-06 15:28:21 +04:00
|
|
|
break;
|
2007-12-01 18:58:22 +03:00
|
|
|
case TIMER_STATUS:
|
2019-10-21 16:43:57 +03:00
|
|
|
ptimer_transaction_begin(t->timer);
|
2009-08-09 00:08:15 +04:00
|
|
|
if (slavio_timer_is_user(tc)) {
|
2007-10-07 14:00:55 +04:00
|
|
|
// start/stop user counter
|
2014-02-23 02:54:53 +04:00
|
|
|
if (val & 1) {
|
2010-10-31 12:24:14 +03:00
|
|
|
trace_slavio_timer_mem_writel_status_start(timer_index);
|
2009-08-31 23:30:17 +04:00
|
|
|
ptimer_run(t->timer, 0);
|
2014-02-23 02:54:53 +04:00
|
|
|
} else {
|
2010-10-31 12:24:14 +03:00
|
|
|
trace_slavio_timer_mem_writel_status_stop(timer_index);
|
2009-08-31 23:30:17 +04:00
|
|
|
ptimer_stop(t->timer);
|
2007-10-06 15:28:21 +04:00
|
|
|
}
|
|
|
|
}
|
2014-02-23 02:54:53 +04:00
|
|
|
t->run = val & 1;
|
2019-10-21 16:43:57 +03:00
|
|
|
ptimer_transaction_commit(t->timer);
|
2007-10-06 15:28:21 +04:00
|
|
|
break;
|
2007-12-01 18:58:22 +03:00
|
|
|
case TIMER_MODE:
|
2009-08-09 00:08:15 +04:00
|
|
|
if (timer_index == 0) {
|
2007-10-06 15:25:43 +04:00
|
|
|
unsigned int i;
|
|
|
|
|
2009-08-09 00:08:15 +04:00
|
|
|
for (i = 0; i < s->num_cpus; i++) {
|
2008-01-26 12:13:46 +03:00
|
|
|
unsigned int processor = 1 << i;
|
2009-08-09 00:08:15 +04:00
|
|
|
CPUTimerState *curr_timer = &s->cputimer[i + 1];
|
2008-01-26 12:13:46 +03:00
|
|
|
|
2019-10-21 16:43:57 +03:00
|
|
|
ptimer_transaction_begin(curr_timer->timer);
|
2008-01-26 12:13:46 +03:00
|
|
|
// check for a change in timer mode for this processor
|
2009-08-09 00:08:15 +04:00
|
|
|
if ((val & processor) != (s->cputimer_mode & processor)) {
|
2008-01-26 12:13:46 +03:00
|
|
|
if (val & processor) { // counter -> user timer
|
2009-08-09 00:08:15 +04:00
|
|
|
qemu_irq_lower(curr_timer->irq);
|
2008-01-26 12:13:46 +03:00
|
|
|
// counters are always running
|
2014-02-23 02:54:53 +04:00
|
|
|
if (!curr_timer->run) {
|
|
|
|
ptimer_stop(curr_timer->timer);
|
|
|
|
}
|
2008-01-26 12:13:46 +03:00
|
|
|
// user timer limit is always the same
|
2009-08-09 00:08:15 +04:00
|
|
|
curr_timer->limit = TIMER_MAX_COUNT64;
|
|
|
|
ptimer_set_limit(curr_timer->timer,
|
|
|
|
LIMIT_TO_PERIODS(curr_timer->limit),
|
2008-05-12 20:13:33 +04:00
|
|
|
1);
|
2008-01-26 12:13:46 +03:00
|
|
|
// set this processors user timer bit in config
|
|
|
|
// register
|
2009-08-09 00:08:15 +04:00
|
|
|
s->cputimer_mode |= processor;
|
2010-10-31 12:24:14 +03:00
|
|
|
trace_slavio_timer_mem_writel_mode_user(timer_index);
|
2008-01-26 12:13:46 +03:00
|
|
|
} else { // user timer -> counter
|
|
|
|
// start the counter
|
2009-08-09 00:08:15 +04:00
|
|
|
ptimer_run(curr_timer->timer, 0);
|
2008-01-26 12:13:46 +03:00
|
|
|
// clear this processors user timer bit in config
|
|
|
|
// register
|
2009-08-09 00:08:15 +04:00
|
|
|
s->cputimer_mode &= ~processor;
|
2010-10-31 12:24:14 +03:00
|
|
|
trace_slavio_timer_mem_writel_mode_counter(timer_index);
|
2008-01-26 12:13:46 +03:00
|
|
|
}
|
2007-10-07 14:00:55 +04:00
|
|
|
}
|
2019-10-21 16:43:57 +03:00
|
|
|
ptimer_transaction_commit(curr_timer->timer);
|
2007-10-06 15:25:43 +04:00
|
|
|
}
|
2009-08-09 00:08:15 +04:00
|
|
|
} else {
|
2010-10-31 12:24:14 +03:00
|
|
|
trace_slavio_timer_mem_writel_mode_invalid();
|
2009-08-09 00:08:15 +04:00
|
|
|
}
|
2007-10-06 15:28:21 +04:00
|
|
|
break;
|
2004-12-20 02:18:01 +03:00
|
|
|
default:
|
2010-10-31 12:24:14 +03:00
|
|
|
trace_slavio_timer_mem_writel_invalid(addr);
|
2007-10-06 15:28:21 +04:00
|
|
|
break;
|
2004-12-20 02:18:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-15 15:14:02 +04:00
|
|
|
static const MemoryRegionOps slavio_timer_mem_ops = {
|
|
|
|
.read = slavio_timer_mem_readl,
|
|
|
|
.write = slavio_timer_mem_writel,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
.valid = {
|
hw/timer/slavio_timer: Allow 64-bit accesses
Per the "NCR89C105 Chip Specification" referenced in the header:
Chip-level Address Map
------------------------------------------------------------------
| 1D0 0000 -> | Counter/Timers | W,D |
| 1DF FFFF | | |
...
The address map indicated the allowed accesses at each address.
[...] W indicates a word access, and D indicates a double-word
access.
The SLAVIO timer controller is implemented expecting 32-bit accesses.
Commit a3d12d073e1 restricted the memory accesses to 32-bit, while
the device allows 64-bit accesses.
This was not an issue until commit 5d971f9e67 which reverted
("memory: accept mismatching sizes in memory_region_access_valid").
Fix by renaming .valid MemoryRegionOps as .impl, and add the valid
access range (W -> 4, D -> 8).
Since commit 21786c7e598 ("memory: Log invalid memory accesses")
this class of bug can be quickly debugged displaying 'guest_errors'
accesses, as:
$ qemu-system-sparc -M SS-20 -m 256 -bios ss20_v2.25_rom -serial stdio -d guest_errors
Power-ON Reset
Invalid access at addr 0x0, size 8, region 'timer-1', reason: invalid size (min:4 max:4)
$ qemu-system-sparc -M SS-20 -m 256 -bios ss20_v2.25_rom -monitor stdio -S
(qemu) info mtree
address-space: memory
0000000000000000-ffffffffffffffff (prio 0, i/o): system
...
0000000ff1300000-0000000ff130000f (prio 0, i/o): timer-1
^^^^^^^^^ ^^^^^^^
\ memory region base address and name /
(qemu) info qtree
bus: main-system-bus
dev: slavio_timer, id "" <-- device type name
gpio-out "sysbus-irq" 17
num_cpus = 1 (0x1)
mmio 0000000ff1310000/0000000000000014
mmio 0000000ff1300000/0000000000000010 <--- base address
mmio 0000000ff1301000/0000000000000010
mmio 0000000ff1302000/0000000000000010
...
Reported-by: Yap KV <yapkv@yahoo.com>
Buglink: https://bugs.launchpad.net/bugs/1906905
Fixes: a3d12d073e1 ("slavio_timer: convert to memory API")
CC: qemu-stable@nongnu.org
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
Message-Id: <20201205150903.3062711-1-f4bug@amsat.org>
Signed-off-by: Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>
2020-12-05 18:09:03 +03:00
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 8,
|
|
|
|
},
|
|
|
|
.impl = {
|
2011-11-15 15:14:02 +04:00
|
|
|
.min_access_size = 4,
|
|
|
|
.max_access_size = 4,
|
|
|
|
},
|
2004-12-20 02:18:01 +03:00
|
|
|
};
|
|
|
|
|
2009-08-31 23:30:18 +04:00
|
|
|
static const VMStateDescription vmstate_timer = {
|
|
|
|
.name ="timer",
|
|
|
|
.version_id = 3,
|
|
|
|
.minimum_version_id = 3,
|
2023-12-21 06:16:37 +03:00
|
|
|
.fields = (const VMStateField[]) {
|
2009-08-31 23:30:18 +04:00
|
|
|
VMSTATE_UINT64(limit, CPUTimerState),
|
|
|
|
VMSTATE_UINT32(count, CPUTimerState),
|
|
|
|
VMSTATE_UINT32(counthigh, CPUTimerState),
|
|
|
|
VMSTATE_UINT32(reached, CPUTimerState),
|
2014-02-23 02:54:53 +04:00
|
|
|
VMSTATE_UINT32(run , CPUTimerState),
|
2009-08-31 23:30:18 +04:00
|
|
|
VMSTATE_PTIMER(timer, CPUTimerState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
2009-08-09 00:08:15 +04:00
|
|
|
}
|
2009-08-31 23:30:18 +04:00
|
|
|
};
|
2004-12-20 02:18:01 +03:00
|
|
|
|
2009-08-31 23:30:18 +04:00
|
|
|
static const VMStateDescription vmstate_slavio_timer = {
|
|
|
|
.name ="slavio_timer",
|
|
|
|
.version_id = 3,
|
|
|
|
.minimum_version_id = 3,
|
2023-12-21 06:16:37 +03:00
|
|
|
.fields = (const VMStateField[]) {
|
2009-08-31 23:30:18 +04:00
|
|
|
VMSTATE_STRUCT_ARRAY(cputimer, SLAVIO_TIMERState, MAX_CPUS + 1, 3,
|
|
|
|
vmstate_timer, CPUTimerState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
2009-08-09 00:08:15 +04:00
|
|
|
}
|
2009-08-31 23:30:18 +04:00
|
|
|
};
|
2004-12-20 02:18:01 +03:00
|
|
|
|
2009-10-24 21:35:13 +04:00
|
|
|
static void slavio_timer_reset(DeviceState *d)
|
2004-12-20 02:18:01 +03:00
|
|
|
{
|
2013-07-27 17:24:22 +04:00
|
|
|
SLAVIO_TIMERState *s = SLAVIO_TIMER(d);
|
2009-08-09 00:08:15 +04:00
|
|
|
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;
|
2010-08-02 21:58:21 +04:00
|
|
|
if (i <= s->num_cpus) {
|
2019-10-21 16:43:57 +03:00
|
|
|
ptimer_transaction_begin(curr_timer->timer);
|
2009-08-09 00:08:15 +04:00
|
|
|
ptimer_set_limit(curr_timer->timer,
|
|
|
|
LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
|
|
|
|
ptimer_run(curr_timer->timer, 0);
|
2014-02-23 02:54:53 +04:00
|
|
|
curr_timer->run = 1;
|
2019-10-21 16:43:57 +03:00
|
|
|
ptimer_transaction_commit(curr_timer->timer);
|
2009-08-09 00:08:15 +04:00
|
|
|
}
|
2007-12-27 23:23:20 +03:00
|
|
|
}
|
2009-08-09 00:08:15 +04:00
|
|
|
s->cputimer_mode = 0;
|
2004-12-20 02:18:01 +03:00
|
|
|
}
|
|
|
|
|
2017-05-25 16:34:49 +03:00
|
|
|
static void slavio_timer_init(Object *obj)
|
2009-07-15 12:53:09 +04:00
|
|
|
{
|
2017-05-25 16:34:49 +03:00
|
|
|
SLAVIO_TIMERState *s = SLAVIO_TIMER(obj);
|
|
|
|
SysBusDevice *dev = SYS_BUS_DEVICE(obj);
|
2009-08-09 00:08:15 +04:00
|
|
|
unsigned int i;
|
|
|
|
TimerContext *tc;
|
2004-12-20 02:18:01 +03:00
|
|
|
|
2009-08-09 00:08:15 +04:00
|
|
|
for (i = 0; i <= MAX_CPUS; i++) {
|
2011-11-15 15:14:02 +04:00
|
|
|
uint64_t size;
|
|
|
|
char timer_name[20];
|
|
|
|
|
2022-03-15 17:41:56 +03:00
|
|
|
tc = g_new0(TimerContext, 1);
|
2009-08-09 00:08:15 +04:00
|
|
|
tc->s = s;
|
|
|
|
tc->timer_index = i;
|
2009-07-15 12:53:09 +04:00
|
|
|
|
2019-10-21 16:43:57 +03:00
|
|
|
s->cputimer[i].timer = ptimer_init(slavio_timer_irq, tc,
|
2022-05-16 13:30:58 +03:00
|
|
|
PTIMER_POLICY_LEGACY);
|
2019-10-21 16:43:57 +03:00
|
|
|
ptimer_transaction_begin(s->cputimer[i].timer);
|
2009-08-09 00:08:15 +04:00
|
|
|
ptimer_set_period(s->cputimer[i].timer, TIMER_PERIOD);
|
2019-10-21 16:43:57 +03:00
|
|
|
ptimer_transaction_commit(s->cputimer[i].timer);
|
2004-12-20 02:18:01 +03:00
|
|
|
|
2011-11-15 15:14:02 +04:00
|
|
|
size = i == 0 ? SYS_TIMER_SIZE : CPU_TIMER_SIZE;
|
|
|
|
snprintf(timer_name, sizeof(timer_name), "timer-%i", i);
|
2017-05-25 16:34:49 +03:00
|
|
|
memory_region_init_io(&tc->iomem, obj, &slavio_timer_mem_ops, tc,
|
2011-11-15 15:14:02 +04:00
|
|
|
timer_name, size);
|
2011-11-27 13:38:10 +04:00
|
|
|
sysbus_init_mmio(dev, &tc->iomem);
|
2009-08-09 00:08:15 +04:00
|
|
|
|
|
|
|
sysbus_init_irq(dev, &s->cputimer[i].irq);
|
2009-07-15 12:53:09 +04:00
|
|
|
}
|
2007-10-06 15:25:43 +04:00
|
|
|
}
|
|
|
|
|
2012-01-24 23:12:29 +04:00
|
|
|
static Property slavio_timer_properties[] = {
|
|
|
|
DEFINE_PROP_UINT32("num_cpus", SLAVIO_TIMERState, num_cpus, 0),
|
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void slavio_timer_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
|
|
|
|
2024-09-13 17:31:44 +03:00
|
|
|
device_class_set_legacy_reset(dc, slavio_timer_reset);
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->vmsd = &vmstate_slavio_timer;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, slavio_timer_properties);
|
2012-01-24 23:12:29 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo slavio_timer_info = {
|
2013-07-27 17:24:22 +04:00
|
|
|
.name = TYPE_SLAVIO_TIMER,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_SYS_BUS_DEVICE,
|
|
|
|
.instance_size = sizeof(SLAVIO_TIMERState),
|
2017-05-25 16:34:49 +03:00
|
|
|
.instance_init = slavio_timer_init,
|
2011-12-08 07:34:16 +04:00
|
|
|
.class_init = slavio_timer_class_init,
|
2009-07-15 12:53:09 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void slavio_timer_register_types(void)
|
2009-07-15 12:53:09 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&slavio_timer_info);
|
2009-07-15 12:53:09 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(slavio_timer_register_types)
|