2 * QEMU Sparc SLAVIO timer controller emulation
4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu-timer.h"
31 #define DPRINTF(fmt, args...) \
32 do { printf("TIMER: " fmt , ##args); } while (0)
34 #define DPRINTF(fmt, args...)
38 * Registers of hardware timer in sun4m.
40 * This is the timer/counter part of chip STP2001 (Slave I/O), also
41 * produced as NCR89C105. See
42 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
44 * The 31-bit counter is incremented every 500ns by bit 9. Bits 8..0
45 * are zero. Bit 31 is 1 when count has been reached.
47 * Per-CPU timers interrupt local CPU, system timer uses normal
54 typedef struct SLAVIO_TIMERState {
57 uint32_t count, counthigh, reached;
61 struct SLAVIO_TIMERState *master;
64 unsigned int num_slaves;
65 struct SLAVIO_TIMERState *slave[MAX_CPUS];
69 #define TIMER_MAXADDR 0x1f
70 #define SYS_TIMER_SIZE 0x14
71 #define CPU_TIMER_SIZE 0x10
73 #define SYS_TIMER_OFFSET 0x10000ULL
74 #define CPU_TIMER_OFFSET(cpu) (0x1000ULL * cpu)
77 #define TIMER_COUNTER 1
78 #define TIMER_COUNTER_NORST 2
79 #define TIMER_STATUS 3
82 #define TIMER_COUNT_MASK32 0xfffffe00
83 #define TIMER_LIMIT_MASK32 0x7fffffff
84 #define TIMER_MAX_COUNT64 0x7ffffffffffffe00ULL
85 #define TIMER_MAX_COUNT32 0x7ffffe00ULL
86 #define TIMER_REACHED 0x80000000
87 #define TIMER_PERIOD 500ULL // 500ns
88 #define LIMIT_TO_PERIODS(l) ((l) >> 9)
89 #define PERIODS_TO_LIMIT(l) ((l) << 9)
91 static int slavio_timer_is_user(SLAVIO_TIMERState *s)
93 return s->master && (s->master->slave_mode & (1 << s->slave_index));
96 // Update count, set irq, update expire_time
97 // Convert from ptimer countdown units
98 static void slavio_timer_get_out(SLAVIO_TIMERState *s)
100 uint64_t count, limit;
102 if (s->limit == 0) /* free-run processor or system counter */
103 limit = TIMER_MAX_COUNT32;
107 count = limit - PERIODS_TO_LIMIT(ptimer_get_count(s->timer));
108 DPRINTF("get_out: limit %" PRIx64 " count %x%08x\n", s->limit,
109 s->counthigh, s->count);
110 s->count = count & TIMER_COUNT_MASK32;
111 s->counthigh = count >> 32;
115 static void slavio_timer_irq(void *opaque)
117 SLAVIO_TIMERState *s = opaque;
119 slavio_timer_get_out(s);
120 DPRINTF("callback: count %x%08x\n", s->counthigh, s->count);
121 if (!slavio_timer_is_user(s)) {
122 s->reached = TIMER_REACHED;
123 qemu_irq_raise(s->irq);
127 static uint32_t slavio_timer_mem_readl(void *opaque, target_phys_addr_t addr)
129 SLAVIO_TIMERState *s = opaque;
132 saddr = (addr & TIMER_MAXADDR) >> 2;
135 // read limit (system counter mode) or read most signifying
136 // part of counter (user mode)
137 if (slavio_timer_is_user(s)) {
138 // read user timer MSW
139 slavio_timer_get_out(s);
144 qemu_irq_lower(s->irq);
146 ret = s->limit & TIMER_LIMIT_MASK32;
150 // read counter and reached bit (system mode) or read lsbits
151 // of counter (user mode)
152 slavio_timer_get_out(s);
153 if (slavio_timer_is_user(s)) // read user timer LSW
154 ret = s->count & TIMER_COUNT_MASK32;
156 ret = (s->count & TIMER_MAX_COUNT32) | s->reached;
159 // only available in processor counter/timer
160 // read start/stop status
164 // only available in system counter
165 // read user/system mode
169 DPRINTF("invalid read address " TARGET_FMT_plx "\n", addr);
173 DPRINTF("read " TARGET_FMT_plx " = %08x\n", addr, ret);
178 static void slavio_timer_mem_writel(void *opaque, target_phys_addr_t addr,
181 SLAVIO_TIMERState *s = opaque;
184 DPRINTF("write " TARGET_FMT_plx " %08x\n", addr, val);
185 saddr = (addr & TIMER_MAXADDR) >> 2;
188 if (slavio_timer_is_user(s)) {
189 // set user counter MSW, reset counter
190 qemu_irq_lower(s->irq);
191 s->limit = TIMER_MAX_COUNT64;
192 DPRINTF("processor %d user timer reset\n", s->slave_index);
193 ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(s->limit), 1);
195 // set limit, reset counter
196 qemu_irq_lower(s->irq);
197 s->limit = val & TIMER_MAX_COUNT32;
198 if (s->limit == 0) /* free-run */
199 ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
201 ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(s->limit), 1);
205 if (slavio_timer_is_user(s)) {
206 // set user counter LSW, reset counter
207 qemu_irq_lower(s->irq);
208 s->limit = TIMER_MAX_COUNT64;
209 DPRINTF("processor %d user timer reset\n", s->slave_index);
210 ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(s->limit), 1);
212 DPRINTF("not user timer\n");
214 case TIMER_COUNTER_NORST:
215 // set limit without resetting counter
216 s->limit = val & TIMER_MAX_COUNT32;
217 if (s->limit == 0) /* free-run */
218 ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 0);
220 ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(s->limit), 0);
223 if (slavio_timer_is_user(s)) {
224 // start/stop user counter
225 if ((val & 1) && !s->running) {
226 DPRINTF("processor %d user timer started\n", s->slave_index);
227 ptimer_run(s->timer, 0);
229 } else if (!(val & 1) && s->running) {
230 DPRINTF("processor %d user timer stopped\n", s->slave_index);
231 ptimer_stop(s->timer);
237 if (s->master == NULL) {
240 for (i = 0; i < s->num_slaves; i++) {
241 if (val & (1 << i)) {
242 qemu_irq_lower(s->slave[i]->irq);
243 s->slave[i]->limit = -1ULL;
245 if ((val & (1 << i)) != (s->slave_mode & (1 << i))) {
246 ptimer_stop(s->slave[i]->timer);
247 ptimer_set_limit(s->slave[i]->timer,
248 LIMIT_TO_PERIODS(s->slave[i]->limit), 1);
249 DPRINTF("processor %d timer changed\n",
250 s->slave[i]->slave_index);
251 ptimer_run(s->slave[i]->timer, 0);
254 s->slave_mode = val & ((1 << s->num_slaves) - 1);
256 DPRINTF("not system timer\n");
259 DPRINTF("invalid write address " TARGET_FMT_plx "\n", addr);
264 static CPUReadMemoryFunc *slavio_timer_mem_read[3] = {
265 slavio_timer_mem_readl,
266 slavio_timer_mem_readl,
267 slavio_timer_mem_readl,
270 static CPUWriteMemoryFunc *slavio_timer_mem_write[3] = {
271 slavio_timer_mem_writel,
272 slavio_timer_mem_writel,
273 slavio_timer_mem_writel,
276 static void slavio_timer_save(QEMUFile *f, void *opaque)
278 SLAVIO_TIMERState *s = opaque;
280 qemu_put_be64s(f, &s->limit);
281 qemu_put_be32s(f, &s->count);
282 qemu_put_be32s(f, &s->counthigh);
283 qemu_put_be32(f, 0); // Was irq
284 qemu_put_be32s(f, &s->reached);
285 qemu_put_be32s(f, &s->running);
286 qemu_put_be32s(f, 0); // Was mode
287 qemu_put_ptimer(f, s->timer);
290 static int slavio_timer_load(QEMUFile *f, void *opaque, int version_id)
292 SLAVIO_TIMERState *s = opaque;
298 qemu_get_be64s(f, &s->limit);
299 qemu_get_be32s(f, &s->count);
300 qemu_get_be32s(f, &s->counthigh);
301 qemu_get_be32s(f, &tmp); // Was irq
302 qemu_get_be32s(f, &s->reached);
303 qemu_get_be32s(f, &s->running);
304 qemu_get_be32s(f, &tmp); // Was mode
305 qemu_get_ptimer(f, s->timer);
310 static void slavio_timer_reset(void *opaque)
312 SLAVIO_TIMERState *s = opaque;
318 ptimer_set_limit(s->timer, LIMIT_TO_PERIODS(TIMER_MAX_COUNT32), 1);
319 ptimer_run(s->timer, 0);
321 qemu_irq_lower(s->irq);
324 static SLAVIO_TIMERState *slavio_timer_init(target_phys_addr_t addr,
326 SLAVIO_TIMERState *master,
329 int slavio_timer_io_memory;
330 SLAVIO_TIMERState *s;
333 s = qemu_mallocz(sizeof(SLAVIO_TIMERState));
338 s->slave_index = slave_index;
339 bh = qemu_bh_new(slavio_timer_irq, s);
340 s->timer = ptimer_init(bh);
341 ptimer_set_period(s->timer, TIMER_PERIOD);
343 slavio_timer_io_memory = cpu_register_io_memory(0, slavio_timer_mem_read,
344 slavio_timer_mem_write, s);
346 cpu_register_physical_memory(addr, CPU_TIMER_SIZE,
347 slavio_timer_io_memory);
349 cpu_register_physical_memory(addr, SYS_TIMER_SIZE,
350 slavio_timer_io_memory);
351 register_savevm("slavio_timer", addr, 2, slavio_timer_save,
352 slavio_timer_load, s);
353 qemu_register_reset(slavio_timer_reset, s);
354 slavio_timer_reset(s);
359 void slavio_timer_init_all(target_phys_addr_t base, qemu_irq master_irq,
360 qemu_irq *cpu_irqs, unsigned int num_cpus)
362 SLAVIO_TIMERState *master;
365 master = slavio_timer_init(base + SYS_TIMER_OFFSET, master_irq, NULL, 0);
367 master->num_slaves = num_cpus;
369 for (i = 0; i < MAX_CPUS; i++) {
370 master->slave[i] = slavio_timer_init(base + (target_phys_addr_t)
372 cpu_irqs[i], master, i);