2 * QEMU Sparc SLAVIO interrupt 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
25 //#define DEBUG_IRQ_COUNT
29 #define DPRINTF(fmt, args...) \
30 do { printf("IRQ: " fmt , ##args); } while (0)
32 #define DPRINTF(fmt, args...)
36 * Registers of interrupt controller in sun4m.
38 * This is the interrupt controller part of chip STP2001 (Slave I/O), also
39 * produced as NCR89C105. See
40 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
42 * There is a system master controller and one for each cpu.
49 typedef struct SLAVIO_INTCTLState {
50 uint32_t intreg_pending[MAX_CPUS];
51 uint32_t intregm_pending;
52 uint32_t intregm_disabled;
54 #ifdef DEBUG_IRQ_COUNT
55 uint64_t irq_count[32];
57 qemu_irq *cpu_irqs[MAX_CPUS];
58 const uint32_t *intbit_to_level;
59 uint32_t cputimer_bit;
60 uint32_t pil_out[MAX_CPUS];
63 #define INTCTL_MAXADDR 0xf
64 #define INTCTL_SIZE (INTCTL_MAXADDR + 1)
65 #define INTCTLM_MAXADDR 0x13
66 #define INTCTLM_SIZE (INTCTLM_MAXADDR + 1)
67 #define INTCTLM_MASK 0x1f
68 static void slavio_check_interrupts(void *opaque);
70 // per-cpu interrupt controller
71 static uint32_t slavio_intctl_mem_readl(void *opaque, target_phys_addr_t addr)
73 SLAVIO_INTCTLState *s = opaque;
77 cpu = (addr & (MAX_CPUS - 1) * TARGET_PAGE_SIZE) >> 12;
78 saddr = (addr & INTCTL_MAXADDR) >> 2;
81 ret = s->intreg_pending[cpu];
87 DPRINTF("read cpu %d reg 0x%x = %x\n", addr, ret);
92 static void slavio_intctl_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
94 SLAVIO_INTCTLState *s = opaque;
98 cpu = (addr & (MAX_CPUS - 1) * TARGET_PAGE_SIZE) >> 12;
99 saddr = (addr & INTCTL_MAXADDR) >> 2;
100 DPRINTF("write cpu %d reg 0x%x = %x\n", cpu, addr, val);
102 case 1: // clear pending softints
106 s->intreg_pending[cpu] &= ~val;
107 DPRINTF("Cleared cpu %d irq mask %x, curmask %x\n", cpu, val, s->intreg_pending[cpu]);
109 case 2: // set softint
111 s->intreg_pending[cpu] |= val;
112 slavio_check_interrupts(s);
113 DPRINTF("Set cpu %d irq mask %x, curmask %x\n", cpu, val, s->intreg_pending[cpu]);
120 static CPUReadMemoryFunc *slavio_intctl_mem_read[3] = {
121 slavio_intctl_mem_readl,
122 slavio_intctl_mem_readl,
123 slavio_intctl_mem_readl,
126 static CPUWriteMemoryFunc *slavio_intctl_mem_write[3] = {
127 slavio_intctl_mem_writel,
128 slavio_intctl_mem_writel,
129 slavio_intctl_mem_writel,
132 // master system interrupt controller
133 static uint32_t slavio_intctlm_mem_readl(void *opaque, target_phys_addr_t addr)
135 SLAVIO_INTCTLState *s = opaque;
138 saddr = (addr & INTCTLM_MAXADDR) >> 2;
141 ret = s->intregm_pending & 0x7fffffff;
144 ret = s->intregm_disabled;
153 DPRINTF("read system reg 0x%x = %x\n", addr, ret);
158 static void slavio_intctlm_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
160 SLAVIO_INTCTLState *s = opaque;
163 saddr = (addr & INTCTLM_MASK) >> 2;
164 DPRINTF("write system reg 0x%x = %x\n", addr, val);
166 case 2: // clear (enable)
167 // Force clear unused bits
169 s->intregm_disabled &= ~val;
170 DPRINTF("Enabled master irq mask %x, curmask %x\n", val, s->intregm_disabled);
171 slavio_check_interrupts(s);
173 case 3: // set (disable, clear pending)
174 // Force clear unused bits
176 s->intregm_disabled |= val;
177 s->intregm_pending &= ~val;
178 DPRINTF("Disabled master irq mask %x, curmask %x\n", val, s->intregm_disabled);
181 s->target_cpu = val & (MAX_CPUS - 1);
182 DPRINTF("Set master irq cpu %d\n", s->target_cpu);
189 static CPUReadMemoryFunc *slavio_intctlm_mem_read[3] = {
190 slavio_intctlm_mem_readl,
191 slavio_intctlm_mem_readl,
192 slavio_intctlm_mem_readl,
195 static CPUWriteMemoryFunc *slavio_intctlm_mem_write[3] = {
196 slavio_intctlm_mem_writel,
197 slavio_intctlm_mem_writel,
198 slavio_intctlm_mem_writel,
201 void slavio_pic_info(void *opaque)
203 SLAVIO_INTCTLState *s = opaque;
206 for (i = 0; i < MAX_CPUS; i++) {
207 term_printf("per-cpu %d: pending 0x%08x\n", i, s->intreg_pending[i]);
209 term_printf("master: pending 0x%08x, disabled 0x%08x\n", s->intregm_pending, s->intregm_disabled);
212 void slavio_irq_info(void *opaque)
214 #ifndef DEBUG_IRQ_COUNT
215 term_printf("irq statistic code not compiled.\n");
217 SLAVIO_INTCTLState *s = opaque;
221 term_printf("IRQ statistics:\n");
222 for (i = 0; i < 32; i++) {
223 count = s->irq_count[i];
225 term_printf("%2d: %" PRId64 "\n", i, count);
230 static void raise_pil(SLAVIO_INTCTLState *s, unsigned int pil,
236 irq = s->cpu_irqs[cpu][pil];
238 #ifdef DEBUG_IRQ_COUNT
241 oldmax = s->pil_out[cpu];
242 if (oldmax > 0 && oldmax != pil)
243 qemu_irq_lower(s->cpu_irqs[cpu][oldmax]);
244 s->pil_out[cpu] = pil;
247 DPRINTF("cpu %d pil %d\n", cpu, pil);
250 static void slavio_check_interrupts(void *opaque)
252 SLAVIO_INTCTLState *s = opaque;
253 uint32_t pending = s->intregm_pending;
254 unsigned int i, j, max = 0;
256 pending &= ~s->intregm_disabled;
258 DPRINTF("pending %x disabled %x\n", pending, s->intregm_disabled);
259 for (i = 0; i < MAX_CPUS; i++) {
261 if (pending && !(s->intregm_disabled & 0x80000000) &&
262 (i == s->target_cpu)) {
263 for (j = 0; j < 32; j++) {
264 if (pending & (1 << j)) {
265 if (max < s->intbit_to_level[j])
266 max = s->intbit_to_level[j];
270 for (j = 17; j < 32; j++) {
271 if (s->intreg_pending[i] & (1 << j)) {
276 raise_pil(s, max, i);
281 * "irq" here is the bit number in the system interrupt register to
282 * separate serial and keyboard interrupts sharing a level.
284 static void slavio_set_irq(void *opaque, int irq, int level)
286 SLAVIO_INTCTLState *s = opaque;
287 uint32_t mask = 1 << irq;
288 uint32_t pil = s->intbit_to_level[irq];
290 DPRINTF("Set cpu %d irq %d -> pil %d level %d\n", s->target_cpu, irq, pil,
294 s->intregm_pending |= mask;
295 s->intreg_pending[s->target_cpu] |= 1 << pil;
297 s->intregm_pending &= ~mask;
298 s->intreg_pending[s->target_cpu] &= ~(1 << pil);
300 slavio_check_interrupts(s);
304 static void slavio_set_timer_irq_cpu(void *opaque, int cpu, int level)
306 SLAVIO_INTCTLState *s = opaque;
308 DPRINTF("Set cpu %d local timer level %d\n", cpu, level);
311 s->intreg_pending[cpu] |= s->cputimer_bit;
313 s->intreg_pending[cpu] &= ~s->cputimer_bit;
315 slavio_check_interrupts(s);
318 static void slavio_intctl_save(QEMUFile *f, void *opaque)
320 SLAVIO_INTCTLState *s = opaque;
323 for (i = 0; i < MAX_CPUS; i++) {
324 qemu_put_be32s(f, &s->intreg_pending[i]);
326 qemu_put_be32s(f, &s->intregm_pending);
327 qemu_put_be32s(f, &s->intregm_disabled);
328 qemu_put_be32s(f, &s->target_cpu);
331 static int slavio_intctl_load(QEMUFile *f, void *opaque, int version_id)
333 SLAVIO_INTCTLState *s = opaque;
339 for (i = 0; i < MAX_CPUS; i++) {
340 qemu_get_be32s(f, &s->intreg_pending[i]);
342 qemu_get_be32s(f, &s->intregm_pending);
343 qemu_get_be32s(f, &s->intregm_disabled);
344 qemu_get_be32s(f, &s->target_cpu);
348 static void slavio_intctl_reset(void *opaque)
350 SLAVIO_INTCTLState *s = opaque;
353 for (i = 0; i < MAX_CPUS; i++) {
354 s->intreg_pending[i] = 0;
356 s->intregm_disabled = ~0xffb2007f;
357 s->intregm_pending = 0;
361 void *slavio_intctl_init(target_phys_addr_t addr, target_phys_addr_t addrg,
362 const uint32_t *intbit_to_level,
363 qemu_irq **irq, qemu_irq **cpu_irq,
364 qemu_irq **parent_irq, unsigned int cputimer)
366 int slavio_intctl_io_memory, slavio_intctlm_io_memory, i;
367 SLAVIO_INTCTLState *s;
369 s = qemu_mallocz(sizeof(SLAVIO_INTCTLState));
373 s->intbit_to_level = intbit_to_level;
374 for (i = 0; i < MAX_CPUS; i++) {
375 slavio_intctl_io_memory = cpu_register_io_memory(0, slavio_intctl_mem_read, slavio_intctl_mem_write, s);
376 cpu_register_physical_memory(addr + i * TARGET_PAGE_SIZE, INTCTL_SIZE,
377 slavio_intctl_io_memory);
378 s->cpu_irqs[i] = parent_irq[i];
381 slavio_intctlm_io_memory = cpu_register_io_memory(0, slavio_intctlm_mem_read, slavio_intctlm_mem_write, s);
382 cpu_register_physical_memory(addrg, INTCTLM_SIZE, slavio_intctlm_io_memory);
384 register_savevm("slavio_intctl", addr, 1, slavio_intctl_save, slavio_intctl_load, s);
385 qemu_register_reset(slavio_intctl_reset, s);
386 *irq = qemu_allocate_irqs(slavio_set_irq, s, 32);
388 *cpu_irq = qemu_allocate_irqs(slavio_set_timer_irq_cpu, s, MAX_CPUS);
389 s->cputimer_bit = 1 << s->intbit_to_level[cputimer];
390 slavio_intctl_reset(s);