blob: ab519de5d8fef6120a5d7a2379769b358a4799ba [file] [log] [blame]
bellard80cabfa2004-03-14 12:20:30 +00001/*
2 * QEMU 8259 interrupt controller emulation
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard80cabfa2004-03-14 12:20:30 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard80cabfa2004-03-14 12:20:30 +00006 * 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 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw.h"
25#include "pc.h"
26#include "isa.h"
aliguori376253e2009-03-05 23:01:23 +000027#include "monitor.h"
Blue Swirl0bf9e312009-07-20 17:19:25 +000028#include "qemu-timer.h"
bellard80cabfa2004-03-14 12:20:30 +000029
30/* debug PIC */
31//#define DEBUG_PIC
32
Blue Swirl8ac02ff2010-05-29 20:23:19 +000033#ifdef DEBUG_PIC
34#define DPRINTF(fmt, ...) \
35 do { printf("pic: " fmt , ## __VA_ARGS__); } while (0)
36#else
37#define DPRINTF(fmt, ...)
38#endif
39
bellardb41a2cd2004-03-14 21:46:48 +000040//#define DEBUG_IRQ_LATENCY
bellard4a0fb71e2004-05-21 11:39:07 +000041//#define DEBUG_IRQ_COUNT
bellardb41a2cd2004-03-14 21:46:48 +000042
Jan Kiszkac17725f2011-10-07 09:19:51 +020043struct PicState {
Jan Kiszka747c70a2011-10-07 09:19:53 +020044 ISADevice dev;
bellard80cabfa2004-03-14 12:20:30 +000045 uint8_t last_irr; /* edge detection */
46 uint8_t irr; /* interrupt request register */
47 uint8_t imr; /* interrupt mask register */
48 uint8_t isr; /* interrupt service register */
49 uint8_t priority_add; /* highest irq priority */
50 uint8_t irq_base;
51 uint8_t read_reg_select;
52 uint8_t poll;
53 uint8_t special_mask;
54 uint8_t init_state;
55 uint8_t auto_eoi;
56 uint8_t rotate_on_auto_eoi;
57 uint8_t special_fully_nested_mode;
58 uint8_t init4; /* true if 4 byte init */
ths20531522007-04-01 18:26:11 +000059 uint8_t single_mode; /* true if slave pic is not initialized */
bellard660de332004-05-20 12:41:21 +000060 uint8_t elcr; /* PIIX edge/trigger selection*/
61 uint8_t elcr_mask;
Jan Kiszka747c70a2011-10-07 09:19:53 +020062 qemu_irq int_out[1];
63 uint32_t master; /* reflects /SP input pin */
64 uint32_t iobase;
65 uint32_t elcr_addr;
Richard Henderson098d3142011-08-10 15:28:16 -070066 MemoryRegion base_io;
67 MemoryRegion elcr_io;
bellard3de388f2005-07-02 18:11:44 +000068};
bellard80cabfa2004-03-14 12:20:30 +000069
Jan Kiszka81a02f92011-10-07 09:19:54 +020070#if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
bellard4a0fb71e2004-05-21 11:39:07 +000071static int irq_level[16];
72#endif
73#ifdef DEBUG_IRQ_COUNT
74static uint64_t irq_count[16];
75#endif
Jan Kiszka747c70a2011-10-07 09:19:53 +020076#ifdef DEBUG_IRQ_LATENCY
77static int64_t irq_time[16];
78#endif
Jan Kiszkac17725f2011-10-07 09:19:51 +020079PicState *isa_pic;
80static PicState *slave_pic;
bellard4a0fb71e2004-05-21 11:39:07 +000081
bellard80cabfa2004-03-14 12:20:30 +000082/* return the highest priority found in mask (highest = smallest
83 number). Return 8 if no irq */
Jan Kiszka5dcd35e2011-10-07 09:19:38 +020084static int get_priority(PicState *s, int mask)
bellard80cabfa2004-03-14 12:20:30 +000085{
86 int priority;
Jan Kiszka81a02f92011-10-07 09:19:54 +020087
88 if (mask == 0) {
bellard80cabfa2004-03-14 12:20:30 +000089 return 8;
Jan Kiszka81a02f92011-10-07 09:19:54 +020090 }
bellard80cabfa2004-03-14 12:20:30 +000091 priority = 0;
Jan Kiszka81a02f92011-10-07 09:19:54 +020092 while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0) {
bellard80cabfa2004-03-14 12:20:30 +000093 priority++;
Jan Kiszka81a02f92011-10-07 09:19:54 +020094 }
bellard80cabfa2004-03-14 12:20:30 +000095 return priority;
96}
97
98/* return the pic wanted interrupt. return -1 if none */
99static int pic_get_irq(PicState *s)
100{
101 int mask, cur_priority, priority;
102
103 mask = s->irr & ~s->imr;
104 priority = get_priority(s, mask);
Jan Kiszka81a02f92011-10-07 09:19:54 +0200105 if (priority == 8) {
bellard80cabfa2004-03-14 12:20:30 +0000106 return -1;
Jan Kiszka81a02f92011-10-07 09:19:54 +0200107 }
bellard80cabfa2004-03-14 12:20:30 +0000108 /* compute current priority. If special fully nested mode on the
109 master, the IRQ coming from the slave is not taken into account
110 for the priority computation. */
111 mask = s->isr;
Jan Kiszka81a02f92011-10-07 09:19:54 +0200112 if (s->special_mask) {
balrog84678712008-07-19 09:18:48 +0000113 mask &= ~s->imr;
Jan Kiszka81a02f92011-10-07 09:19:54 +0200114 }
Jan Kiszka25985392011-10-07 09:19:50 +0200115 if (s->special_fully_nested_mode && s->master) {
bellard80cabfa2004-03-14 12:20:30 +0000116 mask &= ~(1 << 2);
Jan Kiszka25985392011-10-07 09:19:50 +0200117 }
bellard80cabfa2004-03-14 12:20:30 +0000118 cur_priority = get_priority(s, mask);
119 if (priority < cur_priority) {
120 /* higher priority found: an irq should be generated */
121 return (priority + s->priority_add) & 7;
122 } else {
123 return -1;
124 }
125}
126
Jan Kiszkab76750c2011-10-07 09:19:46 +0200127/* Update INT output. Must be called every time the output may have changed. */
128static void pic_update_irq(PicState *s)
bellard80cabfa2004-03-14 12:20:30 +0000129{
Jan Kiszkab76750c2011-10-07 09:19:46 +0200130 int irq;
bellard80cabfa2004-03-14 12:20:30 +0000131
Jan Kiszkab76750c2011-10-07 09:19:46 +0200132 irq = pic_get_irq(s);
bellard80cabfa2004-03-14 12:20:30 +0000133 if (irq >= 0) {
Jan Kiszkab76750c2011-10-07 09:19:46 +0200134 DPRINTF("pic%d: imr=%x irr=%x padd=%d\n",
Jan Kiszka25985392011-10-07 09:19:50 +0200135 s->master ? 0 : 1, s->imr, s->irr, s->priority_add);
Jan Kiszka747c70a2011-10-07 09:19:53 +0200136 qemu_irq_raise(s->int_out[0]);
Jan Kiszkad96e1732011-10-07 09:19:37 +0200137 } else {
Jan Kiszka747c70a2011-10-07 09:19:53 +0200138 qemu_irq_lower(s->int_out[0]);
ths4de9b242007-01-24 01:47:51 +0000139 }
bellard80cabfa2004-03-14 12:20:30 +0000140}
141
Jan Kiszka62026012011-10-07 09:19:40 +0200142/* set irq level. If an edge is detected, then the IRR is set to 1 */
Jan Kiszka747c70a2011-10-07 09:19:53 +0200143static void pic_set_irq(void *opaque, int irq, int level)
Jan Kiszka62026012011-10-07 09:19:40 +0200144{
Jan Kiszka747c70a2011-10-07 09:19:53 +0200145 PicState *s = opaque;
146 int mask = 1 << irq;
147
148#if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT) || \
149 defined(DEBUG_IRQ_LATENCY)
150 int irq_index = s->master ? irq : irq + 8;
151#endif
152#if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
153 if (level != irq_level[irq_index]) {
154 DPRINTF("pic_set_irq: irq=%d level=%d\n", irq_index, level);
155 irq_level[irq_index] = level;
156#ifdef DEBUG_IRQ_COUNT
157 if (level == 1) {
158 irq_count[irq_index]++;
159 }
160#endif
161 }
162#endif
163#ifdef DEBUG_IRQ_LATENCY
164 if (level) {
165 irq_time[irq_index] = qemu_get_clock_ns(vm_clock);
166 }
167#endif
168
Jan Kiszka62026012011-10-07 09:19:40 +0200169 if (s->elcr & mask) {
170 /* level triggered */
171 if (level) {
172 s->irr |= mask;
173 s->last_irr |= mask;
174 } else {
175 s->irr &= ~mask;
176 s->last_irr &= ~mask;
177 }
178 } else {
179 /* edge triggered */
180 if (level) {
181 if ((s->last_irr & mask) == 0) {
182 s->irr |= mask;
183 }
184 s->last_irr |= mask;
185 } else {
186 s->last_irr &= ~mask;
187 }
188 }
Jan Kiszkab76750c2011-10-07 09:19:46 +0200189 pic_update_irq(s);
Jan Kiszka62026012011-10-07 09:19:40 +0200190}
191
bellard80cabfa2004-03-14 12:20:30 +0000192/* acknowledge interrupt 'irq' */
Jan Kiszka5dcd35e2011-10-07 09:19:38 +0200193static void pic_intack(PicState *s, int irq)
bellard80cabfa2004-03-14 12:20:30 +0000194{
195 if (s->auto_eoi) {
Jan Kiszka81a02f92011-10-07 09:19:54 +0200196 if (s->rotate_on_auto_eoi) {
bellard80cabfa2004-03-14 12:20:30 +0000197 s->priority_add = (irq + 1) & 7;
Jan Kiszka81a02f92011-10-07 09:19:54 +0200198 }
bellard80cabfa2004-03-14 12:20:30 +0000199 } else {
200 s->isr |= (1 << irq);
201 }
bellard0ecf89a2004-09-29 21:55:52 +0000202 /* We don't clear a level sensitive interrupt here */
Jan Kiszka81a02f92011-10-07 09:19:54 +0200203 if (!(s->elcr & (1 << irq))) {
bellard0ecf89a2004-09-29 21:55:52 +0000204 s->irr &= ~(1 << irq);
Jan Kiszka81a02f92011-10-07 09:19:54 +0200205 }
Jan Kiszkab76750c2011-10-07 09:19:46 +0200206 pic_update_irq(s);
bellard80cabfa2004-03-14 12:20:30 +0000207}
208
Jan Kiszkac17725f2011-10-07 09:19:51 +0200209int pic_read_irq(PicState *s)
bellard80cabfa2004-03-14 12:20:30 +0000210{
211 int irq, irq2, intno;
212
Jan Kiszkac17725f2011-10-07 09:19:51 +0200213 irq = pic_get_irq(s);
bellard15aeac32004-05-20 16:12:05 +0000214 if (irq >= 0) {
bellard15aeac32004-05-20 16:12:05 +0000215 if (irq == 2) {
Jan Kiszkac17725f2011-10-07 09:19:51 +0200216 irq2 = pic_get_irq(slave_pic);
bellard15aeac32004-05-20 16:12:05 +0000217 if (irq2 >= 0) {
Jan Kiszkac17725f2011-10-07 09:19:51 +0200218 pic_intack(slave_pic, irq2);
bellard15aeac32004-05-20 16:12:05 +0000219 } else {
220 /* spurious IRQ on slave controller */
221 irq2 = 7;
222 }
Jan Kiszkac17725f2011-10-07 09:19:51 +0200223 intno = slave_pic->irq_base + irq2;
bellard15aeac32004-05-20 16:12:05 +0000224 } else {
Jan Kiszkac17725f2011-10-07 09:19:51 +0200225 intno = s->irq_base + irq;
bellard15aeac32004-05-20 16:12:05 +0000226 }
Jan Kiszkac17725f2011-10-07 09:19:51 +0200227 pic_intack(s, irq);
bellard15aeac32004-05-20 16:12:05 +0000228 } else {
229 /* spurious IRQ on host controller */
230 irq = 7;
Jan Kiszkac17725f2011-10-07 09:19:51 +0200231 intno = s->irq_base + irq;
bellard15aeac32004-05-20 16:12:05 +0000232 }
ths3b46e622007-09-17 08:09:54 +0000233
Jan Kiszka78ef2b62011-10-07 09:19:43 +0200234#if defined(DEBUG_PIC) || defined(DEBUG_IRQ_LATENCY)
235 if (irq == 2) {
236 irq = irq2 + 8;
237 }
238#endif
bellard80cabfa2004-03-14 12:20:30 +0000239#ifdef DEBUG_IRQ_LATENCY
ths5fafdf22007-09-16 21:08:06 +0000240 printf("IRQ%d latency=%0.3fus\n",
241 irq,
Paolo Bonzini74475452011-03-11 16:47:48 +0100242 (double)(qemu_get_clock_ns(vm_clock) -
Juan Quintela6ee093c2009-09-10 03:04:26 +0200243 irq_time[irq]) * 1000000.0 / get_ticks_per_sec());
bellard80cabfa2004-03-14 12:20:30 +0000244#endif
Blue Swirl8ac02ff2010-05-29 20:23:19 +0000245 DPRINTF("pic_interrupt: irq=%d\n", irq);
bellard80cabfa2004-03-14 12:20:30 +0000246 return intno;
247}
248
Jan Kiszka86fbf972011-10-07 09:19:45 +0200249static void pic_init_reset(PicState *s)
bellardd7d02e32004-06-20 12:58:36 +0000250{
bellard3de388f2005-07-02 18:11:44 +0000251 s->last_irr = 0;
252 s->irr = 0;
253 s->imr = 0;
254 s->isr = 0;
255 s->priority_add = 0;
256 s->irq_base = 0;
257 s->read_reg_select = 0;
258 s->poll = 0;
259 s->special_mask = 0;
260 s->init_state = 0;
261 s->auto_eoi = 0;
262 s->rotate_on_auto_eoi = 0;
263 s->special_fully_nested_mode = 0;
264 s->init4 = 0;
ths20531522007-04-01 18:26:11 +0000265 s->single_mode = 0;
bellard4dbe19e2006-04-29 15:52:14 +0000266 /* Note: ELCR is not reset */
Jan Kiszkab76750c2011-10-07 09:19:46 +0200267 pic_update_irq(s);
bellardd7d02e32004-06-20 12:58:36 +0000268}
269
Jan Kiszka747c70a2011-10-07 09:19:53 +0200270static void pic_reset(DeviceState *dev)
Jan Kiszka86fbf972011-10-07 09:19:45 +0200271{
Jan Kiszka747c70a2011-10-07 09:19:53 +0200272 PicState *s = container_of(dev, PicState, dev.qdev);
Jan Kiszka86fbf972011-10-07 09:19:45 +0200273
274 pic_init_reset(s);
275 s->elcr = 0;
276}
277
Richard Henderson098d3142011-08-10 15:28:16 -0700278static void pic_ioport_write(void *opaque, target_phys_addr_t addr64,
279 uint64_t val64, unsigned size)
bellard80cabfa2004-03-14 12:20:30 +0000280{
bellardb41a2cd2004-03-14 21:46:48 +0000281 PicState *s = opaque;
Richard Henderson098d3142011-08-10 15:28:16 -0700282 uint32_t addr = addr64;
283 uint32_t val = val64;
bellardd7d02e32004-06-20 12:58:36 +0000284 int priority, cmd, irq;
bellard80cabfa2004-03-14 12:20:30 +0000285
Blue Swirl8ac02ff2010-05-29 20:23:19 +0000286 DPRINTF("write: addr=0x%02x val=0x%02x\n", addr, val);
bellard80cabfa2004-03-14 12:20:30 +0000287 if (addr == 0) {
288 if (val & 0x10) {
Jan Kiszka86fbf972011-10-07 09:19:45 +0200289 pic_init_reset(s);
bellard80cabfa2004-03-14 12:20:30 +0000290 s->init_state = 1;
291 s->init4 = val & 1;
ths20531522007-04-01 18:26:11 +0000292 s->single_mode = val & 2;
Jan Kiszka81a02f92011-10-07 09:19:54 +0200293 if (val & 0x08) {
bellard80cabfa2004-03-14 12:20:30 +0000294 hw_error("level sensitive irq not supported");
Jan Kiszka81a02f92011-10-07 09:19:54 +0200295 }
bellard80cabfa2004-03-14 12:20:30 +0000296 } else if (val & 0x08) {
Jan Kiszka81a02f92011-10-07 09:19:54 +0200297 if (val & 0x04) {
bellard80cabfa2004-03-14 12:20:30 +0000298 s->poll = 1;
Jan Kiszka81a02f92011-10-07 09:19:54 +0200299 }
300 if (val & 0x02) {
bellard80cabfa2004-03-14 12:20:30 +0000301 s->read_reg_select = val & 1;
Jan Kiszka81a02f92011-10-07 09:19:54 +0200302 }
303 if (val & 0x40) {
bellard80cabfa2004-03-14 12:20:30 +0000304 s->special_mask = (val >> 5) & 1;
Jan Kiszka81a02f92011-10-07 09:19:54 +0200305 }
bellard80cabfa2004-03-14 12:20:30 +0000306 } else {
307 cmd = val >> 5;
Jan Kiszka81a02f92011-10-07 09:19:54 +0200308 switch (cmd) {
bellard80cabfa2004-03-14 12:20:30 +0000309 case 0:
310 case 4:
311 s->rotate_on_auto_eoi = cmd >> 2;
312 break;
313 case 1: /* end of interrupt */
314 case 5:
315 priority = get_priority(s, s->isr);
316 if (priority != 8) {
317 irq = (priority + s->priority_add) & 7;
318 s->isr &= ~(1 << irq);
Jan Kiszka81a02f92011-10-07 09:19:54 +0200319 if (cmd == 5) {
bellard80cabfa2004-03-14 12:20:30 +0000320 s->priority_add = (irq + 1) & 7;
Jan Kiszka81a02f92011-10-07 09:19:54 +0200321 }
Jan Kiszkab76750c2011-10-07 09:19:46 +0200322 pic_update_irq(s);
bellard80cabfa2004-03-14 12:20:30 +0000323 }
324 break;
325 case 3:
326 irq = val & 7;
327 s->isr &= ~(1 << irq);
Jan Kiszkab76750c2011-10-07 09:19:46 +0200328 pic_update_irq(s);
bellard80cabfa2004-03-14 12:20:30 +0000329 break;
330 case 6:
331 s->priority_add = (val + 1) & 7;
Jan Kiszkab76750c2011-10-07 09:19:46 +0200332 pic_update_irq(s);
bellard80cabfa2004-03-14 12:20:30 +0000333 break;
334 case 7:
335 irq = val & 7;
336 s->isr &= ~(1 << irq);
337 s->priority_add = (irq + 1) & 7;
Jan Kiszkab76750c2011-10-07 09:19:46 +0200338 pic_update_irq(s);
bellard80cabfa2004-03-14 12:20:30 +0000339 break;
340 default:
341 /* no operation */
342 break;
343 }
344 }
345 } else {
Jan Kiszka81a02f92011-10-07 09:19:54 +0200346 switch (s->init_state) {
bellard80cabfa2004-03-14 12:20:30 +0000347 case 0:
348 /* normal mode */
349 s->imr = val;
Jan Kiszkab76750c2011-10-07 09:19:46 +0200350 pic_update_irq(s);
bellard80cabfa2004-03-14 12:20:30 +0000351 break;
352 case 1:
353 s->irq_base = val & 0xf8;
ths2bb081f2007-07-31 23:12:09 +0000354 s->init_state = s->single_mode ? (s->init4 ? 3 : 0) : 2;
bellard80cabfa2004-03-14 12:20:30 +0000355 break;
356 case 2:
357 if (s->init4) {
358 s->init_state = 3;
359 } else {
360 s->init_state = 0;
361 }
362 break;
363 case 3:
364 s->special_fully_nested_mode = (val >> 4) & 1;
365 s->auto_eoi = (val >> 1) & 1;
366 s->init_state = 0;
367 break;
368 }
369 }
370}
371
Jan Kiszkafc1a5e02011-10-07 09:19:48 +0200372static uint64_t pic_ioport_read(void *opaque, target_phys_addr_t addr,
Richard Henderson098d3142011-08-10 15:28:16 -0700373 unsigned size)
bellard80cabfa2004-03-14 12:20:30 +0000374{
bellardb41a2cd2004-03-14 21:46:48 +0000375 PicState *s = opaque;
bellard80cabfa2004-03-14 12:20:30 +0000376 int ret;
377
bellard80cabfa2004-03-14 12:20:30 +0000378 if (s->poll) {
Jan Kiszka8d484ca2011-10-07 09:19:47 +0200379 ret = pic_get_irq(s);
380 if (ret >= 0) {
381 pic_intack(s, ret);
382 ret |= 0x80;
383 } else {
384 ret = 0;
385 }
bellard80cabfa2004-03-14 12:20:30 +0000386 s->poll = 0;
387 } else {
388 if (addr == 0) {
Jan Kiszka81a02f92011-10-07 09:19:54 +0200389 if (s->read_reg_select) {
bellard80cabfa2004-03-14 12:20:30 +0000390 ret = s->isr;
Jan Kiszka81a02f92011-10-07 09:19:54 +0200391 } else {
bellard80cabfa2004-03-14 12:20:30 +0000392 ret = s->irr;
Jan Kiszka81a02f92011-10-07 09:19:54 +0200393 }
bellard80cabfa2004-03-14 12:20:30 +0000394 } else {
395 ret = s->imr;
396 }
397 }
Richard Henderson098d3142011-08-10 15:28:16 -0700398 DPRINTF("read: addr=0x%02x val=0x%02x\n", addr, ret);
bellard80cabfa2004-03-14 12:20:30 +0000399 return ret;
400}
401
Jan Kiszkac17725f2011-10-07 09:19:51 +0200402int pic_get_output(PicState *s)
Jan Kiszkad96e1732011-10-07 09:19:37 +0200403{
Jan Kiszkac17725f2011-10-07 09:19:51 +0200404 return (pic_get_irq(s) >= 0);
Jan Kiszkad96e1732011-10-07 09:19:37 +0200405}
406
Richard Henderson098d3142011-08-10 15:28:16 -0700407static void elcr_ioport_write(void *opaque, target_phys_addr_t addr,
408 uint64_t val, unsigned size)
bellard660de332004-05-20 12:41:21 +0000409{
410 PicState *s = opaque;
411 s->elcr = val & s->elcr_mask;
412}
413
Richard Henderson098d3142011-08-10 15:28:16 -0700414static uint64_t elcr_ioport_read(void *opaque, target_phys_addr_t addr,
415 unsigned size)
bellard660de332004-05-20 12:41:21 +0000416{
417 PicState *s = opaque;
418 return s->elcr;
419}
420
Juan Quintela77eea832009-09-10 03:04:35 +0200421static const VMStateDescription vmstate_pic = {
422 .name = "i8259",
423 .version_id = 1,
424 .minimum_version_id = 1,
425 .minimum_version_id_old = 1,
Jan Kiszka81a02f92011-10-07 09:19:54 +0200426 .fields = (VMStateField[]) {
Juan Quintela77eea832009-09-10 03:04:35 +0200427 VMSTATE_UINT8(last_irr, PicState),
428 VMSTATE_UINT8(irr, PicState),
429 VMSTATE_UINT8(imr, PicState),
430 VMSTATE_UINT8(isr, PicState),
431 VMSTATE_UINT8(priority_add, PicState),
432 VMSTATE_UINT8(irq_base, PicState),
433 VMSTATE_UINT8(read_reg_select, PicState),
434 VMSTATE_UINT8(poll, PicState),
435 VMSTATE_UINT8(special_mask, PicState),
436 VMSTATE_UINT8(init_state, PicState),
437 VMSTATE_UINT8(auto_eoi, PicState),
438 VMSTATE_UINT8(rotate_on_auto_eoi, PicState),
439 VMSTATE_UINT8(special_fully_nested_mode, PicState),
440 VMSTATE_UINT8(init4, PicState),
441 VMSTATE_UINT8(single_mode, PicState),
442 VMSTATE_UINT8(elcr, PicState),
443 VMSTATE_END_OF_LIST()
444 }
445};
bellardb0a21b52004-03-31 18:58:38 +0000446
Richard Henderson098d3142011-08-10 15:28:16 -0700447static const MemoryRegionOps pic_base_ioport_ops = {
448 .read = pic_ioport_read,
449 .write = pic_ioport_write,
450 .impl = {
451 .min_access_size = 1,
452 .max_access_size = 1,
453 },
454};
455
456static const MemoryRegionOps pic_elcr_ioport_ops = {
457 .read = elcr_ioport_read,
458 .write = elcr_ioport_write,
459 .impl = {
460 .min_access_size = 1,
461 .max_access_size = 1,
462 },
463};
464
Jan Kiszka747c70a2011-10-07 09:19:53 +0200465static int pic_initfn(ISADevice *dev)
bellardb0a21b52004-03-31 18:58:38 +0000466{
Jan Kiszka747c70a2011-10-07 09:19:53 +0200467 PicState *s = DO_UPCAST(PicState, dev, dev);
Jan Kiszka2e2b2272011-10-07 09:19:41 +0200468
Richard Henderson098d3142011-08-10 15:28:16 -0700469 memory_region_init_io(&s->base_io, &pic_base_ioport_ops, s, "pic", 2);
470 memory_region_init_io(&s->elcr_io, &pic_elcr_ioport_ops, s, "elcr", 1);
471
Jan Kiszka747c70a2011-10-07 09:19:53 +0200472 isa_register_ioport(NULL, &s->base_io, s->iobase);
473 if (s->elcr_addr != -1) {
474 isa_register_ioport(NULL, &s->elcr_io, s->elcr_addr);
bellard660de332004-05-20 12:41:21 +0000475 }
Richard Henderson098d3142011-08-10 15:28:16 -0700476
Jan Kiszka747c70a2011-10-07 09:19:53 +0200477 qdev_init_gpio_out(&dev->qdev, s->int_out, ARRAY_SIZE(s->int_out));
478 qdev_init_gpio_in(&dev->qdev, pic_set_irq, 8);
479
480 qdev_set_legacy_instance_id(&dev->qdev, s->iobase, 1);
481
482 return 0;
bellardb0a21b52004-03-31 18:58:38 +0000483}
484
aliguori376253e2009-03-05 23:01:23 +0000485void pic_info(Monitor *mon)
bellardba91cd82004-04-25 18:03:53 +0000486{
487 int i;
488 PicState *s;
ths3b46e622007-09-17 08:09:54 +0000489
Jan Kiszka81a02f92011-10-07 09:19:54 +0200490 if (!isa_pic) {
bellard3de388f2005-07-02 18:11:44 +0000491 return;
Jan Kiszka81a02f92011-10-07 09:19:54 +0200492 }
Jan Kiszkac17725f2011-10-07 09:19:51 +0200493 for (i = 0; i < 2; i++) {
494 s = i == 0 ? isa_pic : slave_pic;
aliguori376253e2009-03-05 23:01:23 +0000495 monitor_printf(mon, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d "
496 "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n",
497 i, s->irr, s->imr, s->isr, s->priority_add,
498 s->irq_base, s->read_reg_select, s->elcr,
499 s->special_fully_nested_mode);
bellardba91cd82004-04-25 18:03:53 +0000500 }
501}
502
aliguori376253e2009-03-05 23:01:23 +0000503void irq_info(Monitor *mon)
bellard4a0fb71e2004-05-21 11:39:07 +0000504{
505#ifndef DEBUG_IRQ_COUNT
aliguori376253e2009-03-05 23:01:23 +0000506 monitor_printf(mon, "irq statistic code not compiled.\n");
bellard4a0fb71e2004-05-21 11:39:07 +0000507#else
508 int i;
509 int64_t count;
510
aliguori376253e2009-03-05 23:01:23 +0000511 monitor_printf(mon, "IRQ statistics:\n");
bellard4a0fb71e2004-05-21 11:39:07 +0000512 for (i = 0; i < 16; i++) {
513 count = irq_count[i];
Jan Kiszka81a02f92011-10-07 09:19:54 +0200514 if (count > 0) {
aliguori376253e2009-03-05 23:01:23 +0000515 monitor_printf(mon, "%2d: %" PRId64 "\n", i, count);
Jan Kiszka81a02f92011-10-07 09:19:54 +0200516 }
bellard4a0fb71e2004-05-21 11:39:07 +0000517 }
518#endif
519}
bellardba91cd82004-04-25 18:03:53 +0000520
pbrookd537cf62007-04-07 18:14:41 +0000521qemu_irq *i8259_init(qemu_irq parent_irq)
bellard80cabfa2004-03-14 12:20:30 +0000522{
Jan Kiszka747c70a2011-10-07 09:19:53 +0200523 qemu_irq *irq_set;
524 ISADevice *dev;
525 int i;
pbrookd537cf62007-04-07 18:14:41 +0000526
Jan Kiszka747c70a2011-10-07 09:19:53 +0200527 irq_set = g_malloc(ISA_NUM_IRQS * sizeof(qemu_irq));
Jan Kiszkac17725f2011-10-07 09:19:51 +0200528
Jan Kiszka747c70a2011-10-07 09:19:53 +0200529 dev = isa_create("isa-i8259");
530 qdev_prop_set_uint32(&dev->qdev, "iobase", 0x20);
531 qdev_prop_set_uint32(&dev->qdev, "elcr_addr", 0x4d0);
532 qdev_prop_set_uint8(&dev->qdev, "elcr_mask", 0xf8);
533 qdev_prop_set_bit(&dev->qdev, "master", true);
534 qdev_init_nofail(&dev->qdev);
Jan Kiszkac17725f2011-10-07 09:19:51 +0200535
Jan Kiszka747c70a2011-10-07 09:19:53 +0200536 qdev_connect_gpio_out(&dev->qdev, 0, parent_irq);
537 for (i = 0 ; i < 8; i++) {
538 irq_set[i] = qdev_get_gpio_in(&dev->qdev, i);
539 }
Jan Kiszkac17725f2011-10-07 09:19:51 +0200540
Jan Kiszka747c70a2011-10-07 09:19:53 +0200541 isa_pic = DO_UPCAST(PicState, dev, dev);
542
543 dev = isa_create("isa-i8259");
544 qdev_prop_set_uint32(&dev->qdev, "iobase", 0xa0);
545 qdev_prop_set_uint32(&dev->qdev, "elcr_addr", 0x4d1);
546 qdev_prop_set_uint8(&dev->qdev, "elcr_mask", 0xde);
547 qdev_init_nofail(&dev->qdev);
548
549 qdev_connect_gpio_out(&dev->qdev, 0, irq_set[2]);
550 for (i = 0 ; i < 8; i++) {
551 irq_set[i + 8] = qdev_get_gpio_in(&dev->qdev, i);
552 }
553
554 slave_pic = DO_UPCAST(PicState, dev, dev);
555
556 return irq_set;
bellard80cabfa2004-03-14 12:20:30 +0000557}
Jan Kiszka747c70a2011-10-07 09:19:53 +0200558
559static ISADeviceInfo i8259_info = {
560 .qdev.name = "isa-i8259",
561 .qdev.size = sizeof(PicState),
562 .qdev.vmsd = &vmstate_pic,
563 .qdev.reset = pic_reset,
564 .qdev.no_user = 1,
565 .init = pic_initfn,
566 .qdev.props = (Property[]) {
567 DEFINE_PROP_HEX32("iobase", PicState, iobase, -1),
568 DEFINE_PROP_HEX32("elcr_addr", PicState, elcr_addr, -1),
569 DEFINE_PROP_HEX8("elcr_mask", PicState, elcr_mask, -1),
570 DEFINE_PROP_BIT("master", PicState, master, 0, false),
571 DEFINE_PROP_END_OF_LIST(),
572 },
573};
574
575static void pic_register(void)
576{
577 isa_qdev_register(&i8259_info);
578}
579device_init(pic_register)