root / hw / i8259.c @ a8b7063b
History | View | Annotate | Download (15.3 kB)
1 | 80cabfad | bellard | /*
|
---|---|---|---|
2 | 80cabfad | bellard | * QEMU 8259 interrupt controller emulation
|
3 | 5fafdf24 | ths | *
|
4 | 80cabfad | bellard | * Copyright (c) 2003-2004 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | 80cabfad | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 80cabfad | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 80cabfad | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 80cabfad | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 80cabfad | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 80cabfad | bellard | * furnished to do so, subject to the following conditions:
|
12 | 80cabfad | bellard | *
|
13 | 80cabfad | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 80cabfad | bellard | * all copies or substantial portions of the Software.
|
15 | 80cabfad | bellard | *
|
16 | 80cabfad | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 80cabfad | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 80cabfad | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 80cabfad | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 80cabfad | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 80cabfad | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 80cabfad | bellard | * THE SOFTWARE.
|
23 | 80cabfad | bellard | */
|
24 | 87ecb68b | pbrook | #include "hw.h" |
25 | 87ecb68b | pbrook | #include "pc.h" |
26 | 87ecb68b | pbrook | #include "isa.h" |
27 | 376253ec | aliguori | #include "monitor.h" |
28 | 80cabfad | bellard | |
29 | 80cabfad | bellard | /* debug PIC */
|
30 | 80cabfad | bellard | //#define DEBUG_PIC
|
31 | 80cabfad | bellard | |
32 | b41a2cd1 | bellard | //#define DEBUG_IRQ_LATENCY
|
33 | 4a0fb71e | bellard | //#define DEBUG_IRQ_COUNT
|
34 | b41a2cd1 | bellard | |
35 | 80cabfad | bellard | typedef struct PicState { |
36 | 80cabfad | bellard | uint8_t last_irr; /* edge detection */
|
37 | 80cabfad | bellard | uint8_t irr; /* interrupt request register */
|
38 | 80cabfad | bellard | uint8_t imr; /* interrupt mask register */
|
39 | 80cabfad | bellard | uint8_t isr; /* interrupt service register */
|
40 | 80cabfad | bellard | uint8_t priority_add; /* highest irq priority */
|
41 | 80cabfad | bellard | uint8_t irq_base; |
42 | 80cabfad | bellard | uint8_t read_reg_select; |
43 | 80cabfad | bellard | uint8_t poll; |
44 | 80cabfad | bellard | uint8_t special_mask; |
45 | 80cabfad | bellard | uint8_t init_state; |
46 | 80cabfad | bellard | uint8_t auto_eoi; |
47 | 80cabfad | bellard | uint8_t rotate_on_auto_eoi; |
48 | 80cabfad | bellard | uint8_t special_fully_nested_mode; |
49 | 80cabfad | bellard | uint8_t init4; /* true if 4 byte init */
|
50 | 2053152b | ths | uint8_t single_mode; /* true if slave pic is not initialized */
|
51 | 660de336 | bellard | uint8_t elcr; /* PIIX edge/trigger selection*/
|
52 | 660de336 | bellard | uint8_t elcr_mask; |
53 | 3de388f6 | bellard | PicState2 *pics_state; |
54 | 80cabfad | bellard | } PicState; |
55 | 80cabfad | bellard | |
56 | 3de388f6 | bellard | struct PicState2 {
|
57 | 3de388f6 | bellard | /* 0 is master pic, 1 is slave pic */
|
58 | 3de388f6 | bellard | /* XXX: better separation between the two pics */
|
59 | 3de388f6 | bellard | PicState pics[2];
|
60 | d537cf6c | pbrook | qemu_irq parent_irq; |
61 | 3de388f6 | bellard | void *irq_request_opaque;
|
62 | d592d303 | bellard | /* IOAPIC callback support */
|
63 | d592d303 | bellard | SetIRQFunc *alt_irq_func; |
64 | d592d303 | bellard | void *alt_irq_opaque;
|
65 | 3de388f6 | bellard | }; |
66 | 80cabfad | bellard | |
67 | 4a0fb71e | bellard | #if defined(DEBUG_PIC) || defined (DEBUG_IRQ_COUNT)
|
68 | 4a0fb71e | bellard | static int irq_level[16]; |
69 | 4a0fb71e | bellard | #endif
|
70 | 4a0fb71e | bellard | #ifdef DEBUG_IRQ_COUNT
|
71 | 4a0fb71e | bellard | static uint64_t irq_count[16]; |
72 | 4a0fb71e | bellard | #endif
|
73 | 4a0fb71e | bellard | |
74 | 80cabfad | bellard | /* set irq level. If an edge is detected, then the IRR is set to 1 */
|
75 | 80cabfad | bellard | static inline void pic_set_irq1(PicState *s, int irq, int level) |
76 | 80cabfad | bellard | { |
77 | 80cabfad | bellard | int mask;
|
78 | 80cabfad | bellard | mask = 1 << irq;
|
79 | 660de336 | bellard | if (s->elcr & mask) {
|
80 | 660de336 | bellard | /* level triggered */
|
81 | 660de336 | bellard | if (level) {
|
82 | 80cabfad | bellard | s->irr |= mask; |
83 | 660de336 | bellard | s->last_irr |= mask; |
84 | 660de336 | bellard | } else {
|
85 | 660de336 | bellard | s->irr &= ~mask; |
86 | 660de336 | bellard | s->last_irr &= ~mask; |
87 | 660de336 | bellard | } |
88 | 80cabfad | bellard | } else {
|
89 | 660de336 | bellard | /* edge triggered */
|
90 | 660de336 | bellard | if (level) {
|
91 | 660de336 | bellard | if ((s->last_irr & mask) == 0) |
92 | 660de336 | bellard | s->irr |= mask; |
93 | 660de336 | bellard | s->last_irr |= mask; |
94 | 660de336 | bellard | } else {
|
95 | 660de336 | bellard | s->last_irr &= ~mask; |
96 | 660de336 | bellard | } |
97 | 80cabfad | bellard | } |
98 | 80cabfad | bellard | } |
99 | 80cabfad | bellard | |
100 | 80cabfad | bellard | /* return the highest priority found in mask (highest = smallest
|
101 | 80cabfad | bellard | number). Return 8 if no irq */
|
102 | 80cabfad | bellard | static inline int get_priority(PicState *s, int mask) |
103 | 80cabfad | bellard | { |
104 | 80cabfad | bellard | int priority;
|
105 | 80cabfad | bellard | if (mask == 0) |
106 | 80cabfad | bellard | return 8; |
107 | 80cabfad | bellard | priority = 0;
|
108 | 80cabfad | bellard | while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0) |
109 | 80cabfad | bellard | priority++; |
110 | 80cabfad | bellard | return priority;
|
111 | 80cabfad | bellard | } |
112 | 80cabfad | bellard | |
113 | 80cabfad | bellard | /* return the pic wanted interrupt. return -1 if none */
|
114 | 80cabfad | bellard | static int pic_get_irq(PicState *s) |
115 | 80cabfad | bellard | { |
116 | 80cabfad | bellard | int mask, cur_priority, priority;
|
117 | 80cabfad | bellard | |
118 | 80cabfad | bellard | mask = s->irr & ~s->imr; |
119 | 80cabfad | bellard | priority = get_priority(s, mask); |
120 | 80cabfad | bellard | if (priority == 8) |
121 | 80cabfad | bellard | return -1; |
122 | 80cabfad | bellard | /* compute current priority. If special fully nested mode on the
|
123 | 80cabfad | bellard | master, the IRQ coming from the slave is not taken into account
|
124 | 80cabfad | bellard | for the priority computation. */
|
125 | 80cabfad | bellard | mask = s->isr; |
126 | 84678711 | balrog | if (s->special_mask)
|
127 | 84678711 | balrog | mask &= ~s->imr; |
128 | 3de388f6 | bellard | if (s->special_fully_nested_mode && s == &s->pics_state->pics[0]) |
129 | 80cabfad | bellard | mask &= ~(1 << 2); |
130 | 80cabfad | bellard | cur_priority = get_priority(s, mask); |
131 | 80cabfad | bellard | if (priority < cur_priority) {
|
132 | 80cabfad | bellard | /* higher priority found: an irq should be generated */
|
133 | 80cabfad | bellard | return (priority + s->priority_add) & 7; |
134 | 80cabfad | bellard | } else {
|
135 | 80cabfad | bellard | return -1; |
136 | 80cabfad | bellard | } |
137 | 80cabfad | bellard | } |
138 | 80cabfad | bellard | |
139 | 80cabfad | bellard | /* raise irq to CPU if necessary. must be called every time the active
|
140 | 80cabfad | bellard | irq may change */
|
141 | 3de388f6 | bellard | /* XXX: should not export it, but it is needed for an APIC kludge */
|
142 | 3de388f6 | bellard | void pic_update_irq(PicState2 *s)
|
143 | 80cabfad | bellard | { |
144 | 80cabfad | bellard | int irq2, irq;
|
145 | 80cabfad | bellard | |
146 | 80cabfad | bellard | /* first look at slave pic */
|
147 | 3de388f6 | bellard | irq2 = pic_get_irq(&s->pics[1]);
|
148 | 80cabfad | bellard | if (irq2 >= 0) { |
149 | 80cabfad | bellard | /* if irq request by slave pic, signal master PIC */
|
150 | 3de388f6 | bellard | pic_set_irq1(&s->pics[0], 2, 1); |
151 | 3de388f6 | bellard | pic_set_irq1(&s->pics[0], 2, 0); |
152 | 80cabfad | bellard | } |
153 | 80cabfad | bellard | /* look at requested irq */
|
154 | 3de388f6 | bellard | irq = pic_get_irq(&s->pics[0]);
|
155 | 80cabfad | bellard | if (irq >= 0) { |
156 | 80cabfad | bellard | #if defined(DEBUG_PIC)
|
157 | 80cabfad | bellard | { |
158 | 80cabfad | bellard | int i;
|
159 | 80cabfad | bellard | for(i = 0; i < 2; i++) { |
160 | 5fafdf24 | ths | printf("pic%d: imr=%x irr=%x padd=%d\n",
|
161 | 5fafdf24 | ths | i, s->pics[i].imr, s->pics[i].irr, |
162 | 3de388f6 | bellard | s->pics[i].priority_add); |
163 | 3b46e624 | ths | |
164 | 80cabfad | bellard | } |
165 | 80cabfad | bellard | } |
166 | 2444ca41 | bellard | printf("pic: cpu_interrupt\n");
|
167 | 80cabfad | bellard | #endif
|
168 | d537cf6c | pbrook | qemu_irq_raise(s->parent_irq); |
169 | 80cabfad | bellard | } |
170 | 4de9b249 | ths | |
171 | 4de9b249 | ths | /* all targets should do this rather than acking the IRQ in the cpu */
|
172 | 29463b24 | aurel32 | #if defined(TARGET_MIPS) || defined(TARGET_PPC) || defined(TARGET_ALPHA)
|
173 | 4de9b249 | ths | else {
|
174 | d537cf6c | pbrook | qemu_irq_lower(s->parent_irq); |
175 | 4de9b249 | ths | } |
176 | 4de9b249 | ths | #endif
|
177 | 80cabfad | bellard | } |
178 | 80cabfad | bellard | |
179 | 80cabfad | bellard | #ifdef DEBUG_IRQ_LATENCY
|
180 | 80cabfad | bellard | int64_t irq_time[16];
|
181 | 80cabfad | bellard | #endif
|
182 | 80cabfad | bellard | |
183 | 9596ebb7 | pbrook | static void i8259_set_irq(void *opaque, int irq, int level) |
184 | 80cabfad | bellard | { |
185 | 3de388f6 | bellard | PicState2 *s = opaque; |
186 | 3de388f6 | bellard | |
187 | 4a0fb71e | bellard | #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
|
188 | 80cabfad | bellard | if (level != irq_level[irq]) {
|
189 | 4a0fb71e | bellard | #if defined(DEBUG_PIC)
|
190 | d537cf6c | pbrook | printf("i8259_set_irq: irq=%d level=%d\n", irq, level);
|
191 | 4a0fb71e | bellard | #endif
|
192 | 80cabfad | bellard | irq_level[irq] = level; |
193 | 4a0fb71e | bellard | #ifdef DEBUG_IRQ_COUNT
|
194 | 4a0fb71e | bellard | if (level == 1) |
195 | 4a0fb71e | bellard | irq_count[irq]++; |
196 | 4a0fb71e | bellard | #endif
|
197 | 80cabfad | bellard | } |
198 | 80cabfad | bellard | #endif
|
199 | 80cabfad | bellard | #ifdef DEBUG_IRQ_LATENCY
|
200 | 80cabfad | bellard | if (level) {
|
201 | 2444ca41 | bellard | irq_time[irq] = qemu_get_clock(vm_clock); |
202 | 80cabfad | bellard | } |
203 | 80cabfad | bellard | #endif
|
204 | 3de388f6 | bellard | pic_set_irq1(&s->pics[irq >> 3], irq & 7, level); |
205 | d592d303 | bellard | /* used for IOAPIC irqs */
|
206 | d592d303 | bellard | if (s->alt_irq_func)
|
207 | d592d303 | bellard | s->alt_irq_func(s->alt_irq_opaque, irq, level); |
208 | 3de388f6 | bellard | pic_update_irq(s); |
209 | 80cabfad | bellard | } |
210 | 80cabfad | bellard | |
211 | 80cabfad | bellard | /* acknowledge interrupt 'irq' */
|
212 | 80cabfad | bellard | static inline void pic_intack(PicState *s, int irq) |
213 | 80cabfad | bellard | { |
214 | 80cabfad | bellard | if (s->auto_eoi) {
|
215 | 80cabfad | bellard | if (s->rotate_on_auto_eoi)
|
216 | 80cabfad | bellard | s->priority_add = (irq + 1) & 7; |
217 | 80cabfad | bellard | } else {
|
218 | 80cabfad | bellard | s->isr |= (1 << irq);
|
219 | 80cabfad | bellard | } |
220 | 0ecf89aa | bellard | /* We don't clear a level sensitive interrupt here */
|
221 | 0ecf89aa | bellard | if (!(s->elcr & (1 << irq))) |
222 | 0ecf89aa | bellard | s->irr &= ~(1 << irq);
|
223 | 80cabfad | bellard | } |
224 | 80cabfad | bellard | |
225 | 3de388f6 | bellard | int pic_read_irq(PicState2 *s)
|
226 | 80cabfad | bellard | { |
227 | 80cabfad | bellard | int irq, irq2, intno;
|
228 | 80cabfad | bellard | |
229 | 3de388f6 | bellard | irq = pic_get_irq(&s->pics[0]);
|
230 | 15aeac38 | bellard | if (irq >= 0) { |
231 | 3de388f6 | bellard | pic_intack(&s->pics[0], irq);
|
232 | 15aeac38 | bellard | if (irq == 2) { |
233 | 3de388f6 | bellard | irq2 = pic_get_irq(&s->pics[1]);
|
234 | 15aeac38 | bellard | if (irq2 >= 0) { |
235 | 3de388f6 | bellard | pic_intack(&s->pics[1], irq2);
|
236 | 15aeac38 | bellard | } else {
|
237 | 15aeac38 | bellard | /* spurious IRQ on slave controller */
|
238 | 15aeac38 | bellard | irq2 = 7;
|
239 | 15aeac38 | bellard | } |
240 | 3de388f6 | bellard | intno = s->pics[1].irq_base + irq2;
|
241 | 15aeac38 | bellard | irq = irq2 + 8;
|
242 | 15aeac38 | bellard | } else {
|
243 | 3de388f6 | bellard | intno = s->pics[0].irq_base + irq;
|
244 | 15aeac38 | bellard | } |
245 | 15aeac38 | bellard | } else {
|
246 | 15aeac38 | bellard | /* spurious IRQ on host controller */
|
247 | 15aeac38 | bellard | irq = 7;
|
248 | 3de388f6 | bellard | intno = s->pics[0].irq_base + irq;
|
249 | 15aeac38 | bellard | } |
250 | 3de388f6 | bellard | pic_update_irq(s); |
251 | 3b46e624 | ths | |
252 | 80cabfad | bellard | #ifdef DEBUG_IRQ_LATENCY
|
253 | 5fafdf24 | ths | printf("IRQ%d latency=%0.3fus\n",
|
254 | 5fafdf24 | ths | irq, |
255 | 2444ca41 | bellard | (double)(qemu_get_clock(vm_clock) - irq_time[irq]) * 1000000.0 / ticks_per_sec); |
256 | 80cabfad | bellard | #endif
|
257 | 80cabfad | bellard | #if defined(DEBUG_PIC)
|
258 | 80cabfad | bellard | printf("pic_interrupt: irq=%d\n", irq);
|
259 | 80cabfad | bellard | #endif
|
260 | 80cabfad | bellard | return intno;
|
261 | 80cabfad | bellard | } |
262 | 80cabfad | bellard | |
263 | d7d02e3c | bellard | static void pic_reset(void *opaque) |
264 | d7d02e3c | bellard | { |
265 | d7d02e3c | bellard | PicState *s = opaque; |
266 | d7d02e3c | bellard | |
267 | 3de388f6 | bellard | s->last_irr = 0;
|
268 | 3de388f6 | bellard | s->irr = 0;
|
269 | 3de388f6 | bellard | s->imr = 0;
|
270 | 3de388f6 | bellard | s->isr = 0;
|
271 | 3de388f6 | bellard | s->priority_add = 0;
|
272 | 3de388f6 | bellard | s->irq_base = 0;
|
273 | 3de388f6 | bellard | s->read_reg_select = 0;
|
274 | 3de388f6 | bellard | s->poll = 0;
|
275 | 3de388f6 | bellard | s->special_mask = 0;
|
276 | 3de388f6 | bellard | s->init_state = 0;
|
277 | 3de388f6 | bellard | s->auto_eoi = 0;
|
278 | 3de388f6 | bellard | s->rotate_on_auto_eoi = 0;
|
279 | 3de388f6 | bellard | s->special_fully_nested_mode = 0;
|
280 | 3de388f6 | bellard | s->init4 = 0;
|
281 | 2053152b | ths | s->single_mode = 0;
|
282 | 4dbe19e1 | bellard | /* Note: ELCR is not reset */
|
283 | d7d02e3c | bellard | } |
284 | d7d02e3c | bellard | |
285 | b41a2cd1 | bellard | static void pic_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
286 | 80cabfad | bellard | { |
287 | b41a2cd1 | bellard | PicState *s = opaque; |
288 | d7d02e3c | bellard | int priority, cmd, irq;
|
289 | 80cabfad | bellard | |
290 | 80cabfad | bellard | #ifdef DEBUG_PIC
|
291 | 80cabfad | bellard | printf("pic_write: addr=0x%02x val=0x%02x\n", addr, val);
|
292 | 80cabfad | bellard | #endif
|
293 | 80cabfad | bellard | addr &= 1;
|
294 | 80cabfad | bellard | if (addr == 0) { |
295 | 80cabfad | bellard | if (val & 0x10) { |
296 | 80cabfad | bellard | /* init */
|
297 | d7d02e3c | bellard | pic_reset(s); |
298 | b54ad049 | bellard | /* deassert a pending interrupt */
|
299 | d537cf6c | pbrook | qemu_irq_lower(s->pics_state->parent_irq); |
300 | 80cabfad | bellard | s->init_state = 1;
|
301 | 80cabfad | bellard | s->init4 = val & 1;
|
302 | 2053152b | ths | s->single_mode = val & 2;
|
303 | 80cabfad | bellard | if (val & 0x08) |
304 | 80cabfad | bellard | hw_error("level sensitive irq not supported");
|
305 | 80cabfad | bellard | } else if (val & 0x08) { |
306 | 80cabfad | bellard | if (val & 0x04) |
307 | 80cabfad | bellard | s->poll = 1;
|
308 | 80cabfad | bellard | if (val & 0x02) |
309 | 80cabfad | bellard | s->read_reg_select = val & 1;
|
310 | 80cabfad | bellard | if (val & 0x40) |
311 | 80cabfad | bellard | s->special_mask = (val >> 5) & 1; |
312 | 80cabfad | bellard | } else {
|
313 | 80cabfad | bellard | cmd = val >> 5;
|
314 | 80cabfad | bellard | switch(cmd) {
|
315 | 80cabfad | bellard | case 0: |
316 | 80cabfad | bellard | case 4: |
317 | 80cabfad | bellard | s->rotate_on_auto_eoi = cmd >> 2;
|
318 | 80cabfad | bellard | break;
|
319 | 80cabfad | bellard | case 1: /* end of interrupt */ |
320 | 80cabfad | bellard | case 5: |
321 | 80cabfad | bellard | priority = get_priority(s, s->isr); |
322 | 80cabfad | bellard | if (priority != 8) { |
323 | 80cabfad | bellard | irq = (priority + s->priority_add) & 7;
|
324 | 80cabfad | bellard | s->isr &= ~(1 << irq);
|
325 | 80cabfad | bellard | if (cmd == 5) |
326 | 80cabfad | bellard | s->priority_add = (irq + 1) & 7; |
327 | 3de388f6 | bellard | pic_update_irq(s->pics_state); |
328 | 80cabfad | bellard | } |
329 | 80cabfad | bellard | break;
|
330 | 80cabfad | bellard | case 3: |
331 | 80cabfad | bellard | irq = val & 7;
|
332 | 80cabfad | bellard | s->isr &= ~(1 << irq);
|
333 | 3de388f6 | bellard | pic_update_irq(s->pics_state); |
334 | 80cabfad | bellard | break;
|
335 | 80cabfad | bellard | case 6: |
336 | 80cabfad | bellard | s->priority_add = (val + 1) & 7; |
337 | 3de388f6 | bellard | pic_update_irq(s->pics_state); |
338 | 80cabfad | bellard | break;
|
339 | 80cabfad | bellard | case 7: |
340 | 80cabfad | bellard | irq = val & 7;
|
341 | 80cabfad | bellard | s->isr &= ~(1 << irq);
|
342 | 80cabfad | bellard | s->priority_add = (irq + 1) & 7; |
343 | 3de388f6 | bellard | pic_update_irq(s->pics_state); |
344 | 80cabfad | bellard | break;
|
345 | 80cabfad | bellard | default:
|
346 | 80cabfad | bellard | /* no operation */
|
347 | 80cabfad | bellard | break;
|
348 | 80cabfad | bellard | } |
349 | 80cabfad | bellard | } |
350 | 80cabfad | bellard | } else {
|
351 | 80cabfad | bellard | switch(s->init_state) {
|
352 | 80cabfad | bellard | case 0: |
353 | 80cabfad | bellard | /* normal mode */
|
354 | 80cabfad | bellard | s->imr = val; |
355 | 3de388f6 | bellard | pic_update_irq(s->pics_state); |
356 | 80cabfad | bellard | break;
|
357 | 80cabfad | bellard | case 1: |
358 | 80cabfad | bellard | s->irq_base = val & 0xf8;
|
359 | 2bb081f7 | ths | s->init_state = s->single_mode ? (s->init4 ? 3 : 0) : 2; |
360 | 80cabfad | bellard | break;
|
361 | 80cabfad | bellard | case 2: |
362 | 80cabfad | bellard | if (s->init4) {
|
363 | 80cabfad | bellard | s->init_state = 3;
|
364 | 80cabfad | bellard | } else {
|
365 | 80cabfad | bellard | s->init_state = 0;
|
366 | 80cabfad | bellard | } |
367 | 80cabfad | bellard | break;
|
368 | 80cabfad | bellard | case 3: |
369 | 80cabfad | bellard | s->special_fully_nested_mode = (val >> 4) & 1; |
370 | 80cabfad | bellard | s->auto_eoi = (val >> 1) & 1; |
371 | 80cabfad | bellard | s->init_state = 0;
|
372 | 80cabfad | bellard | break;
|
373 | 80cabfad | bellard | } |
374 | 80cabfad | bellard | } |
375 | 80cabfad | bellard | } |
376 | 80cabfad | bellard | |
377 | 80cabfad | bellard | static uint32_t pic_poll_read (PicState *s, uint32_t addr1)
|
378 | 80cabfad | bellard | { |
379 | 80cabfad | bellard | int ret;
|
380 | 80cabfad | bellard | |
381 | 80cabfad | bellard | ret = pic_get_irq(s); |
382 | 80cabfad | bellard | if (ret >= 0) { |
383 | 80cabfad | bellard | if (addr1 >> 7) { |
384 | 3de388f6 | bellard | s->pics_state->pics[0].isr &= ~(1 << 2); |
385 | 3de388f6 | bellard | s->pics_state->pics[0].irr &= ~(1 << 2); |
386 | 80cabfad | bellard | } |
387 | 80cabfad | bellard | s->irr &= ~(1 << ret);
|
388 | 80cabfad | bellard | s->isr &= ~(1 << ret);
|
389 | 80cabfad | bellard | if (addr1 >> 7 || ret != 2) |
390 | 3de388f6 | bellard | pic_update_irq(s->pics_state); |
391 | 80cabfad | bellard | } else {
|
392 | 80cabfad | bellard | ret = 0x07;
|
393 | 3de388f6 | bellard | pic_update_irq(s->pics_state); |
394 | 80cabfad | bellard | } |
395 | 80cabfad | bellard | |
396 | 80cabfad | bellard | return ret;
|
397 | 80cabfad | bellard | } |
398 | 80cabfad | bellard | |
399 | b41a2cd1 | bellard | static uint32_t pic_ioport_read(void *opaque, uint32_t addr1) |
400 | 80cabfad | bellard | { |
401 | b41a2cd1 | bellard | PicState *s = opaque; |
402 | 80cabfad | bellard | unsigned int addr; |
403 | 80cabfad | bellard | int ret;
|
404 | 80cabfad | bellard | |
405 | 80cabfad | bellard | addr = addr1; |
406 | 80cabfad | bellard | addr &= 1;
|
407 | 80cabfad | bellard | if (s->poll) {
|
408 | 80cabfad | bellard | ret = pic_poll_read(s, addr1); |
409 | 80cabfad | bellard | s->poll = 0;
|
410 | 80cabfad | bellard | } else {
|
411 | 80cabfad | bellard | if (addr == 0) { |
412 | 80cabfad | bellard | if (s->read_reg_select)
|
413 | 80cabfad | bellard | ret = s->isr; |
414 | 80cabfad | bellard | else
|
415 | 80cabfad | bellard | ret = s->irr; |
416 | 80cabfad | bellard | } else {
|
417 | 80cabfad | bellard | ret = s->imr; |
418 | 80cabfad | bellard | } |
419 | 80cabfad | bellard | } |
420 | 80cabfad | bellard | #ifdef DEBUG_PIC
|
421 | 80cabfad | bellard | printf("pic_read: addr=0x%02x val=0x%02x\n", addr1, ret);
|
422 | 80cabfad | bellard | #endif
|
423 | 80cabfad | bellard | return ret;
|
424 | 80cabfad | bellard | } |
425 | 80cabfad | bellard | |
426 | 80cabfad | bellard | /* memory mapped interrupt status */
|
427 | 3de388f6 | bellard | /* XXX: may be the same than pic_read_irq() */
|
428 | 3de388f6 | bellard | uint32_t pic_intack_read(PicState2 *s) |
429 | 80cabfad | bellard | { |
430 | 80cabfad | bellard | int ret;
|
431 | 80cabfad | bellard | |
432 | 3de388f6 | bellard | ret = pic_poll_read(&s->pics[0], 0x00); |
433 | 80cabfad | bellard | if (ret == 2) |
434 | 3de388f6 | bellard | ret = pic_poll_read(&s->pics[1], 0x80) + 8; |
435 | 80cabfad | bellard | /* Prepare for ISR read */
|
436 | 3de388f6 | bellard | s->pics[0].read_reg_select = 1; |
437 | 3b46e624 | ths | |
438 | 80cabfad | bellard | return ret;
|
439 | 80cabfad | bellard | } |
440 | 80cabfad | bellard | |
441 | 660de336 | bellard | static void elcr_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
442 | 660de336 | bellard | { |
443 | 660de336 | bellard | PicState *s = opaque; |
444 | 660de336 | bellard | s->elcr = val & s->elcr_mask; |
445 | 660de336 | bellard | } |
446 | 660de336 | bellard | |
447 | 660de336 | bellard | static uint32_t elcr_ioport_read(void *opaque, uint32_t addr1) |
448 | 660de336 | bellard | { |
449 | 660de336 | bellard | PicState *s = opaque; |
450 | 660de336 | bellard | return s->elcr;
|
451 | 660de336 | bellard | } |
452 | 660de336 | bellard | |
453 | b0a21b53 | bellard | static void pic_save(QEMUFile *f, void *opaque) |
454 | b0a21b53 | bellard | { |
455 | b0a21b53 | bellard | PicState *s = opaque; |
456 | 3b46e624 | ths | |
457 | b0a21b53 | bellard | qemu_put_8s(f, &s->last_irr); |
458 | b0a21b53 | bellard | qemu_put_8s(f, &s->irr); |
459 | b0a21b53 | bellard | qemu_put_8s(f, &s->imr); |
460 | b0a21b53 | bellard | qemu_put_8s(f, &s->isr); |
461 | b0a21b53 | bellard | qemu_put_8s(f, &s->priority_add); |
462 | b0a21b53 | bellard | qemu_put_8s(f, &s->irq_base); |
463 | b0a21b53 | bellard | qemu_put_8s(f, &s->read_reg_select); |
464 | b0a21b53 | bellard | qemu_put_8s(f, &s->poll); |
465 | b0a21b53 | bellard | qemu_put_8s(f, &s->special_mask); |
466 | b0a21b53 | bellard | qemu_put_8s(f, &s->init_state); |
467 | b0a21b53 | bellard | qemu_put_8s(f, &s->auto_eoi); |
468 | b0a21b53 | bellard | qemu_put_8s(f, &s->rotate_on_auto_eoi); |
469 | b0a21b53 | bellard | qemu_put_8s(f, &s->special_fully_nested_mode); |
470 | b0a21b53 | bellard | qemu_put_8s(f, &s->init4); |
471 | 2053152b | ths | qemu_put_8s(f, &s->single_mode); |
472 | 660de336 | bellard | qemu_put_8s(f, &s->elcr); |
473 | b0a21b53 | bellard | } |
474 | b0a21b53 | bellard | |
475 | b0a21b53 | bellard | static int pic_load(QEMUFile *f, void *opaque, int version_id) |
476 | b0a21b53 | bellard | { |
477 | b0a21b53 | bellard | PicState *s = opaque; |
478 | 3b46e624 | ths | |
479 | b0a21b53 | bellard | if (version_id != 1) |
480 | b0a21b53 | bellard | return -EINVAL;
|
481 | b0a21b53 | bellard | |
482 | b0a21b53 | bellard | qemu_get_8s(f, &s->last_irr); |
483 | b0a21b53 | bellard | qemu_get_8s(f, &s->irr); |
484 | b0a21b53 | bellard | qemu_get_8s(f, &s->imr); |
485 | b0a21b53 | bellard | qemu_get_8s(f, &s->isr); |
486 | b0a21b53 | bellard | qemu_get_8s(f, &s->priority_add); |
487 | b0a21b53 | bellard | qemu_get_8s(f, &s->irq_base); |
488 | b0a21b53 | bellard | qemu_get_8s(f, &s->read_reg_select); |
489 | b0a21b53 | bellard | qemu_get_8s(f, &s->poll); |
490 | b0a21b53 | bellard | qemu_get_8s(f, &s->special_mask); |
491 | b0a21b53 | bellard | qemu_get_8s(f, &s->init_state); |
492 | b0a21b53 | bellard | qemu_get_8s(f, &s->auto_eoi); |
493 | b0a21b53 | bellard | qemu_get_8s(f, &s->rotate_on_auto_eoi); |
494 | b0a21b53 | bellard | qemu_get_8s(f, &s->special_fully_nested_mode); |
495 | b0a21b53 | bellard | qemu_get_8s(f, &s->init4); |
496 | 2053152b | ths | qemu_get_8s(f, &s->single_mode); |
497 | 660de336 | bellard | qemu_get_8s(f, &s->elcr); |
498 | b0a21b53 | bellard | return 0; |
499 | b0a21b53 | bellard | } |
500 | b0a21b53 | bellard | |
501 | b0a21b53 | bellard | /* XXX: add generic master/slave system */
|
502 | 660de336 | bellard | static void pic_init1(int io_addr, int elcr_addr, PicState *s) |
503 | b0a21b53 | bellard | { |
504 | b0a21b53 | bellard | register_ioport_write(io_addr, 2, 1, pic_ioport_write, s); |
505 | b0a21b53 | bellard | register_ioport_read(io_addr, 2, 1, pic_ioport_read, s); |
506 | 660de336 | bellard | if (elcr_addr >= 0) { |
507 | 660de336 | bellard | register_ioport_write(elcr_addr, 1, 1, elcr_ioport_write, s); |
508 | 660de336 | bellard | register_ioport_read(elcr_addr, 1, 1, elcr_ioport_read, s); |
509 | 660de336 | bellard | } |
510 | b0a21b53 | bellard | register_savevm("i8259", io_addr, 1, pic_save, pic_load, s); |
511 | 8217606e | Jan Kiszka | qemu_register_reset(pic_reset, 0, s);
|
512 | b0a21b53 | bellard | } |
513 | b0a21b53 | bellard | |
514 | 376253ec | aliguori | void pic_info(Monitor *mon)
|
515 | ba91cd80 | bellard | { |
516 | ba91cd80 | bellard | int i;
|
517 | ba91cd80 | bellard | PicState *s; |
518 | 3b46e624 | ths | |
519 | 3de388f6 | bellard | if (!isa_pic)
|
520 | 3de388f6 | bellard | return;
|
521 | ba91cd80 | bellard | |
522 | ba91cd80 | bellard | for(i=0;i<2;i++) { |
523 | 3de388f6 | bellard | s = &isa_pic->pics[i]; |
524 | 376253ec | aliguori | monitor_printf(mon, "pic%d: irr=%02x imr=%02x isr=%02x hprio=%d "
|
525 | 376253ec | aliguori | "irq_base=%02x rr_sel=%d elcr=%02x fnm=%d\n",
|
526 | 376253ec | aliguori | i, s->irr, s->imr, s->isr, s->priority_add, |
527 | 376253ec | aliguori | s->irq_base, s->read_reg_select, s->elcr, |
528 | 376253ec | aliguori | s->special_fully_nested_mode); |
529 | ba91cd80 | bellard | } |
530 | ba91cd80 | bellard | } |
531 | ba91cd80 | bellard | |
532 | 376253ec | aliguori | void irq_info(Monitor *mon)
|
533 | 4a0fb71e | bellard | { |
534 | 4a0fb71e | bellard | #ifndef DEBUG_IRQ_COUNT
|
535 | 376253ec | aliguori | monitor_printf(mon, "irq statistic code not compiled.\n");
|
536 | 4a0fb71e | bellard | #else
|
537 | 4a0fb71e | bellard | int i;
|
538 | 4a0fb71e | bellard | int64_t count; |
539 | 4a0fb71e | bellard | |
540 | 376253ec | aliguori | monitor_printf(mon, "IRQ statistics:\n");
|
541 | 4a0fb71e | bellard | for (i = 0; i < 16; i++) { |
542 | 4a0fb71e | bellard | count = irq_count[i]; |
543 | 4a0fb71e | bellard | if (count > 0) |
544 | 376253ec | aliguori | monitor_printf(mon, "%2d: %" PRId64 "\n", i, count); |
545 | 4a0fb71e | bellard | } |
546 | 4a0fb71e | bellard | #endif
|
547 | 4a0fb71e | bellard | } |
548 | ba91cd80 | bellard | |
549 | d537cf6c | pbrook | qemu_irq *i8259_init(qemu_irq parent_irq) |
550 | 80cabfad | bellard | { |
551 | 3de388f6 | bellard | PicState2 *s; |
552 | d537cf6c | pbrook | |
553 | 3de388f6 | bellard | s = qemu_mallocz(sizeof(PicState2));
|
554 | 3de388f6 | bellard | pic_init1(0x20, 0x4d0, &s->pics[0]); |
555 | 3de388f6 | bellard | pic_init1(0xa0, 0x4d1, &s->pics[1]); |
556 | 3de388f6 | bellard | s->pics[0].elcr_mask = 0xf8; |
557 | 3de388f6 | bellard | s->pics[1].elcr_mask = 0xde; |
558 | d537cf6c | pbrook | s->parent_irq = parent_irq; |
559 | 3de388f6 | bellard | s->pics[0].pics_state = s;
|
560 | 3de388f6 | bellard | s->pics[1].pics_state = s;
|
561 | d537cf6c | pbrook | isa_pic = s; |
562 | d537cf6c | pbrook | return qemu_allocate_irqs(i8259_set_irq, s, 16); |
563 | 80cabfad | bellard | } |
564 | d592d303 | bellard | |
565 | d592d303 | bellard | void pic_set_alt_irq_func(PicState2 *s, SetIRQFunc *alt_irq_func,
|
566 | d592d303 | bellard | void *alt_irq_opaque)
|
567 | d592d303 | bellard | { |
568 | d592d303 | bellard | s->alt_irq_func = alt_irq_func; |
569 | d592d303 | bellard | s->alt_irq_opaque = alt_irq_opaque; |
570 | d592d303 | bellard | } |