Fix rtl8139 checksum calculation, by Tim Deegan.
[qemu.git] / hw / slavio_intctl.c
CommitLineData
e80cfcfc
FB
1/*
2 * QEMU Sparc SLAVIO interrupt controller emulation
3 *
66321a11 4 * Copyright (c) 2003-2005 Fabrice Bellard
e80cfcfc
FB
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24#include "vl.h"
25//#define DEBUG_IRQ_COUNT
66321a11
FB
26//#define DEBUG_IRQ
27
28#ifdef DEBUG_IRQ
29#define DPRINTF(fmt, args...) \
30do { printf("IRQ: " fmt , ##args); } while (0)
31#else
32#define DPRINTF(fmt, args...)
33#endif
e80cfcfc
FB
34
35/*
36 * Registers of interrupt controller in sun4m.
37 *
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
41 *
42 * There is a system master controller and one for each cpu.
43 *
44 */
45
46#define MAX_CPUS 16
b3a23197 47#define MAX_PILS 16
e80cfcfc
FB
48
49typedef struct SLAVIO_INTCTLState {
50 uint32_t intreg_pending[MAX_CPUS];
51 uint32_t intregm_pending;
52 uint32_t intregm_disabled;
53 uint32_t target_cpu;
54#ifdef DEBUG_IRQ_COUNT
55 uint64_t irq_count[32];
56#endif
b3a23197 57 qemu_irq *cpu_irqs[MAX_CPUS];
e0353fe2 58 const uint32_t *intbit_to_level;
d7edfd27 59 uint32_t cputimer_bit;
b3a23197 60 uint32_t pil_out[MAX_CPUS];
e80cfcfc
FB
61} SLAVIO_INTCTLState;
62
63#define INTCTL_MAXADDR 0xf
5aca8c3b 64#define INTCTL_SIZE (INTCTL_MAXADDR + 1)
c6fdf5fc 65#define INTCTLM_MAXADDR 0x13
5aca8c3b 66#define INTCTLM_SIZE (INTCTLM_MAXADDR + 1)
c6fdf5fc 67#define INTCTLM_MASK 0x1f
66321a11 68static void slavio_check_interrupts(void *opaque);
e80cfcfc
FB
69
70// per-cpu interrupt controller
71static uint32_t slavio_intctl_mem_readl(void *opaque, target_phys_addr_t addr)
72{
73 SLAVIO_INTCTLState *s = opaque;
dd4131b3 74 uint32_t saddr, ret;
e80cfcfc
FB
75 int cpu;
76
77 cpu = (addr & (MAX_CPUS - 1) * TARGET_PAGE_SIZE) >> 12;
78 saddr = (addr & INTCTL_MAXADDR) >> 2;
79 switch (saddr) {
80 case 0:
dd4131b3
BS
81 ret = s->intreg_pending[cpu];
82 break;
e80cfcfc 83 default:
dd4131b3
BS
84 ret = 0;
85 break;
e80cfcfc 86 }
dd4131b3
BS
87 DPRINTF("read cpu %d reg 0x%x = %x\n", addr, ret);
88
89 return ret;
e80cfcfc
FB
90}
91
92static void slavio_intctl_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
93{
94 SLAVIO_INTCTLState *s = opaque;
95 uint32_t saddr;
96 int cpu;
97
98 cpu = (addr & (MAX_CPUS - 1) * TARGET_PAGE_SIZE) >> 12;
99 saddr = (addr & INTCTL_MAXADDR) >> 2;
dd4131b3 100 DPRINTF("write cpu %d reg 0x%x = %x\n", cpu, addr, val);
e80cfcfc
FB
101 switch (saddr) {
102 case 1: // clear pending softints
103 if (val & 0x4000)
104 val |= 80000000;
105 val &= 0xfffe0000;
106 s->intreg_pending[cpu] &= ~val;
66321a11 107 DPRINTF("Cleared cpu %d irq mask %x, curmask %x\n", cpu, val, s->intreg_pending[cpu]);
e80cfcfc
FB
108 break;
109 case 2: // set softint
110 val &= 0xfffe0000;
111 s->intreg_pending[cpu] |= val;
ba3c64fb 112 slavio_check_interrupts(s);
66321a11 113 DPRINTF("Set cpu %d irq mask %x, curmask %x\n", cpu, val, s->intreg_pending[cpu]);
e80cfcfc
FB
114 break;
115 default:
116 break;
117 }
118}
119
120static CPUReadMemoryFunc *slavio_intctl_mem_read[3] = {
121 slavio_intctl_mem_readl,
122 slavio_intctl_mem_readl,
123 slavio_intctl_mem_readl,
124};
125
126static CPUWriteMemoryFunc *slavio_intctl_mem_write[3] = {
127 slavio_intctl_mem_writel,
128 slavio_intctl_mem_writel,
129 slavio_intctl_mem_writel,
130};
131
132// master system interrupt controller
133static uint32_t slavio_intctlm_mem_readl(void *opaque, target_phys_addr_t addr)
134{
135 SLAVIO_INTCTLState *s = opaque;
dd4131b3 136 uint32_t saddr, ret;
e80cfcfc
FB
137
138 saddr = (addr & INTCTLM_MAXADDR) >> 2;
139 switch (saddr) {
140 case 0:
dd4131b3
BS
141 ret = s->intregm_pending & 0x7fffffff;
142 break;
e80cfcfc 143 case 1:
dd4131b3
BS
144 ret = s->intregm_disabled;
145 break;
e80cfcfc 146 case 4:
dd4131b3
BS
147 ret = s->target_cpu;
148 break;
e80cfcfc 149 default:
dd4131b3
BS
150 ret = 0;
151 break;
e80cfcfc 152 }
dd4131b3
BS
153 DPRINTF("read system reg 0x%x = %x\n", addr, ret);
154
155 return ret;
e80cfcfc
FB
156}
157
158static void slavio_intctlm_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
159{
160 SLAVIO_INTCTLState *s = opaque;
161 uint32_t saddr;
162
c6fdf5fc 163 saddr = (addr & INTCTLM_MASK) >> 2;
dd4131b3 164 DPRINTF("write system reg 0x%x = %x\n", addr, val);
e80cfcfc
FB
165 switch (saddr) {
166 case 2: // clear (enable)
6bae7071 167 // Force clear unused bits
3475187d 168 val &= ~0x4fb2007f;
e80cfcfc 169 s->intregm_disabled &= ~val;
66321a11
FB
170 DPRINTF("Enabled master irq mask %x, curmask %x\n", val, s->intregm_disabled);
171 slavio_check_interrupts(s);
e80cfcfc
FB
172 break;
173 case 3: // set (disable, clear pending)
6bae7071 174 // Force clear unused bits
3475187d 175 val &= ~0x4fb2007f;
e80cfcfc
FB
176 s->intregm_disabled |= val;
177 s->intregm_pending &= ~val;
66321a11 178 DPRINTF("Disabled master irq mask %x, curmask %x\n", val, s->intregm_disabled);
e80cfcfc
FB
179 break;
180 case 4:
181 s->target_cpu = val & (MAX_CPUS - 1);
66321a11 182 DPRINTF("Set master irq cpu %d\n", s->target_cpu);
e80cfcfc
FB
183 break;
184 default:
185 break;
186 }
187}
188
189static CPUReadMemoryFunc *slavio_intctlm_mem_read[3] = {
190 slavio_intctlm_mem_readl,
191 slavio_intctlm_mem_readl,
192 slavio_intctlm_mem_readl,
193};
194
195static CPUWriteMemoryFunc *slavio_intctlm_mem_write[3] = {
196 slavio_intctlm_mem_writel,
197 slavio_intctlm_mem_writel,
198 slavio_intctlm_mem_writel,
199};
200
201void slavio_pic_info(void *opaque)
202{
203 SLAVIO_INTCTLState *s = opaque;
204 int i;
205
206 for (i = 0; i < MAX_CPUS; i++) {
207 term_printf("per-cpu %d: pending 0x%08x\n", i, s->intreg_pending[i]);
208 }
209 term_printf("master: pending 0x%08x, disabled 0x%08x\n", s->intregm_pending, s->intregm_disabled);
210}
211
212void slavio_irq_info(void *opaque)
213{
214#ifndef DEBUG_IRQ_COUNT
215 term_printf("irq statistic code not compiled.\n");
216#else
217 SLAVIO_INTCTLState *s = opaque;
218 int i;
219 int64_t count;
220
221 term_printf("IRQ statistics:\n");
222 for (i = 0; i < 32; i++) {
223 count = s->irq_count[i];
224 if (count > 0)
26a76461 225 term_printf("%2d: %" PRId64 "\n", i, count);
e80cfcfc
FB
226 }
227#endif
228}
229
b3a23197
BS
230static void raise_pil(SLAVIO_INTCTLState *s, unsigned int pil,
231 unsigned int cpu)
232{
233 qemu_irq irq;
234 unsigned int oldmax;
235
236 irq = s->cpu_irqs[cpu][pil];
237
238#ifdef DEBUG_IRQ_COUNT
239 s->irq_count[pil]++;
240#endif
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;
245 if (pil > 0)
246 qemu_irq_raise(irq);
247 DPRINTF("cpu %d pil %d\n", cpu, pil);
248}
249
66321a11
FB
250static void slavio_check_interrupts(void *opaque)
251{
252 SLAVIO_INTCTLState *s = opaque;
253 uint32_t pending = s->intregm_pending;
ba3c64fb 254 unsigned int i, j, max = 0;
66321a11
FB
255
256 pending &= ~s->intregm_disabled;
257
b3a23197 258 DPRINTF("pending %x disabled %x\n", pending, s->intregm_disabled);
ba3c64fb
FB
259 for (i = 0; i < MAX_CPUS; i++) {
260 max = 0;
b3a23197
BS
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];
267 }
268 }
269 }
ba3c64fb
FB
270 for (j = 17; j < 32; j++) {
271 if (s->intreg_pending[i] & (1 << j)) {
272 if (max < j - 16)
273 max = j - 16;
274 }
275 }
b3a23197 276 raise_pil(s, max, i);
ba3c64fb 277 }
66321a11
FB
278}
279
e80cfcfc
FB
280/*
281 * "irq" here is the bit number in the system interrupt register to
282 * separate serial and keyboard interrupts sharing a level.
283 */
d7edfd27 284static void slavio_set_irq(void *opaque, int irq, int level)
e80cfcfc
FB
285{
286 SLAVIO_INTCTLState *s = opaque;
b3a23197
BS
287 uint32_t mask = 1 << irq;
288 uint32_t pil = s->intbit_to_level[irq];
289
290 DPRINTF("Set cpu %d irq %d -> pil %d level %d\n", s->target_cpu, irq, pil,
291 level);
292 if (pil > 0) {
293 if (level) {
294 s->intregm_pending |= mask;
295 s->intreg_pending[s->target_cpu] |= 1 << pil;
296 } else {
297 s->intregm_pending &= ~mask;
298 s->intreg_pending[s->target_cpu] &= ~(1 << pil);
299 }
300 slavio_check_interrupts(s);
e80cfcfc
FB
301 }
302}
303
d7edfd27 304static void slavio_set_timer_irq_cpu(void *opaque, int cpu, int level)
ba3c64fb
FB
305{
306 SLAVIO_INTCTLState *s = opaque;
307
b3a23197 308 DPRINTF("Set cpu %d local timer level %d\n", cpu, level);
d7edfd27 309
b3a23197 310 if (level)
d7edfd27 311 s->intreg_pending[cpu] |= s->cputimer_bit;
b3a23197 312 else
d7edfd27 313 s->intreg_pending[cpu] &= ~s->cputimer_bit;
d7edfd27 314
ba3c64fb
FB
315 slavio_check_interrupts(s);
316}
317
e80cfcfc
FB
318static void slavio_intctl_save(QEMUFile *f, void *opaque)
319{
320 SLAVIO_INTCTLState *s = opaque;
321 int i;
322
323 for (i = 0; i < MAX_CPUS; i++) {
324 qemu_put_be32s(f, &s->intreg_pending[i]);
325 }
326 qemu_put_be32s(f, &s->intregm_pending);
327 qemu_put_be32s(f, &s->intregm_disabled);
328 qemu_put_be32s(f, &s->target_cpu);
329}
330
331static int slavio_intctl_load(QEMUFile *f, void *opaque, int version_id)
332{
333 SLAVIO_INTCTLState *s = opaque;
334 int i;
335
336 if (version_id != 1)
337 return -EINVAL;
338
339 for (i = 0; i < MAX_CPUS; i++) {
340 qemu_get_be32s(f, &s->intreg_pending[i]);
341 }
342 qemu_get_be32s(f, &s->intregm_pending);
343 qemu_get_be32s(f, &s->intregm_disabled);
344 qemu_get_be32s(f, &s->target_cpu);
345 return 0;
346}
347
348static void slavio_intctl_reset(void *opaque)
349{
350 SLAVIO_INTCTLState *s = opaque;
351 int i;
352
353 for (i = 0; i < MAX_CPUS; i++) {
354 s->intreg_pending[i] = 0;
355 }
6bae7071 356 s->intregm_disabled = ~0xffb2007f;
e80cfcfc
FB
357 s->intregm_pending = 0;
358 s->target_cpu = 0;
359}
360
5dcb6b91 361void *slavio_intctl_init(target_phys_addr_t addr, target_phys_addr_t addrg,
d537cf6c 362 const uint32_t *intbit_to_level,
d7edfd27 363 qemu_irq **irq, qemu_irq **cpu_irq,
b3a23197 364 qemu_irq **parent_irq, unsigned int cputimer)
e80cfcfc
FB
365{
366 int slavio_intctl_io_memory, slavio_intctlm_io_memory, i;
367 SLAVIO_INTCTLState *s;
368
369 s = qemu_mallocz(sizeof(SLAVIO_INTCTLState));
370 if (!s)
371 return NULL;
372
e0353fe2 373 s->intbit_to_level = intbit_to_level;
e80cfcfc
FB
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);
5aca8c3b
BS
376 cpu_register_physical_memory(addr + i * TARGET_PAGE_SIZE, INTCTL_SIZE,
377 slavio_intctl_io_memory);
b3a23197 378 s->cpu_irqs[i] = parent_irq[i];
e80cfcfc
FB
379 }
380
381 slavio_intctlm_io_memory = cpu_register_io_memory(0, slavio_intctlm_mem_read, slavio_intctlm_mem_write, s);
5aca8c3b 382 cpu_register_physical_memory(addrg, INTCTLM_SIZE, slavio_intctlm_io_memory);
e80cfcfc
FB
383
384 register_savevm("slavio_intctl", addr, 1, slavio_intctl_save, slavio_intctl_load, s);
385 qemu_register_reset(slavio_intctl_reset, s);
d537cf6c 386 *irq = qemu_allocate_irqs(slavio_set_irq, s, 32);
d7edfd27
BS
387
388 *cpu_irq = qemu_allocate_irqs(slavio_set_timer_irq_cpu, s, MAX_CPUS);
389 s->cputimer_bit = 1 << s->intbit_to_level[cputimer];
e80cfcfc
FB
390 slavio_intctl_reset(s);
391 return s;
392}
393
This page took 0.163874 seconds and 4 git commands to generate.