root / hw / i8259.c @ 660de336
History | View | Annotate | Download (12.4 kB)
1 |
/*
|
---|---|
2 |
* QEMU 8259 interrupt controller emulation
|
3 |
*
|
4 |
* Copyright (c) 2003-2004 Fabrice Bellard
|
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 |
|
26 |
/* debug PIC */
|
27 |
//#define DEBUG_PIC
|
28 |
|
29 |
//#define DEBUG_IRQ_LATENCY
|
30 |
|
31 |
typedef struct PicState { |
32 |
uint8_t last_irr; /* edge detection */
|
33 |
uint8_t irr; /* interrupt request register */
|
34 |
uint8_t imr; /* interrupt mask register */
|
35 |
uint8_t isr; /* interrupt service register */
|
36 |
uint8_t priority_add; /* highest irq priority */
|
37 |
uint8_t irq_base; |
38 |
uint8_t read_reg_select; |
39 |
uint8_t poll; |
40 |
uint8_t special_mask; |
41 |
uint8_t init_state; |
42 |
uint8_t auto_eoi; |
43 |
uint8_t rotate_on_auto_eoi; |
44 |
uint8_t special_fully_nested_mode; |
45 |
uint8_t init4; /* true if 4 byte init */
|
46 |
uint8_t elcr; /* PIIX edge/trigger selection*/
|
47 |
uint8_t elcr_mask; |
48 |
} PicState; |
49 |
|
50 |
/* 0 is master pic, 1 is slave pic */
|
51 |
static PicState pics[2]; |
52 |
static int pic_irq_requested; |
53 |
|
54 |
/* set irq level. If an edge is detected, then the IRR is set to 1 */
|
55 |
static inline void pic_set_irq1(PicState *s, int irq, int level) |
56 |
{ |
57 |
int mask;
|
58 |
mask = 1 << irq;
|
59 |
if (s->elcr & mask) {
|
60 |
/* level triggered */
|
61 |
if (level) {
|
62 |
s->irr |= mask; |
63 |
s->last_irr |= mask; |
64 |
} else {
|
65 |
s->irr &= ~mask; |
66 |
s->last_irr &= ~mask; |
67 |
} |
68 |
} else {
|
69 |
/* edge triggered */
|
70 |
if (level) {
|
71 |
if ((s->last_irr & mask) == 0) |
72 |
s->irr |= mask; |
73 |
s->last_irr |= mask; |
74 |
} else {
|
75 |
s->last_irr &= ~mask; |
76 |
} |
77 |
} |
78 |
} |
79 |
|
80 |
/* return the highest priority found in mask (highest = smallest
|
81 |
number). Return 8 if no irq */
|
82 |
static inline int get_priority(PicState *s, int mask) |
83 |
{ |
84 |
int priority;
|
85 |
if (mask == 0) |
86 |
return 8; |
87 |
priority = 0;
|
88 |
while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0) |
89 |
priority++; |
90 |
return priority;
|
91 |
} |
92 |
|
93 |
/* return the pic wanted interrupt. return -1 if none */
|
94 |
static int pic_get_irq(PicState *s) |
95 |
{ |
96 |
int mask, cur_priority, priority;
|
97 |
|
98 |
mask = s->irr & ~s->imr; |
99 |
priority = get_priority(s, mask); |
100 |
if (priority == 8) |
101 |
return -1; |
102 |
/* compute current priority. If special fully nested mode on the
|
103 |
master, the IRQ coming from the slave is not taken into account
|
104 |
for the priority computation. */
|
105 |
mask = s->isr; |
106 |
if (s->special_fully_nested_mode && s == &pics[0]) |
107 |
mask &= ~(1 << 2); |
108 |
cur_priority = get_priority(s, mask); |
109 |
if (priority < cur_priority) {
|
110 |
/* higher priority found: an irq should be generated */
|
111 |
return (priority + s->priority_add) & 7; |
112 |
} else {
|
113 |
return -1; |
114 |
} |
115 |
} |
116 |
|
117 |
/* raise irq to CPU if necessary. must be called every time the active
|
118 |
irq may change */
|
119 |
static void pic_update_irq(void) |
120 |
{ |
121 |
int irq2, irq;
|
122 |
|
123 |
/* first look at slave pic */
|
124 |
irq2 = pic_get_irq(&pics[1]);
|
125 |
if (irq2 >= 0) { |
126 |
/* if irq request by slave pic, signal master PIC */
|
127 |
pic_set_irq1(&pics[0], 2, 1); |
128 |
pic_set_irq1(&pics[0], 2, 0); |
129 |
} |
130 |
/* look at requested irq */
|
131 |
irq = pic_get_irq(&pics[0]);
|
132 |
if (irq >= 0) { |
133 |
if (irq == 2) { |
134 |
/* from slave pic */
|
135 |
pic_irq_requested = 8 + irq2;
|
136 |
} else {
|
137 |
/* from master pic */
|
138 |
pic_irq_requested = irq; |
139 |
} |
140 |
#if defined(DEBUG_PIC)
|
141 |
{ |
142 |
int i;
|
143 |
for(i = 0; i < 2; i++) { |
144 |
printf("pic%d: imr=%x irr=%x padd=%d\n",
|
145 |
i, pics[i].imr, pics[i].irr, pics[i].priority_add); |
146 |
|
147 |
} |
148 |
} |
149 |
printf("pic: cpu_interrupt req=%d\n", pic_irq_requested);
|
150 |
#endif
|
151 |
cpu_interrupt(cpu_single_env, CPU_INTERRUPT_HARD); |
152 |
} |
153 |
} |
154 |
|
155 |
#ifdef DEBUG_IRQ_LATENCY
|
156 |
int64_t irq_time[16];
|
157 |
#endif
|
158 |
#if defined(DEBUG_PIC)
|
159 |
int irq_level[16]; |
160 |
#endif
|
161 |
|
162 |
void pic_set_irq(int irq, int level) |
163 |
{ |
164 |
#if defined(DEBUG_PIC)
|
165 |
if (level != irq_level[irq]) {
|
166 |
printf("pic_set_irq: irq=%d level=%d\n", irq, level);
|
167 |
irq_level[irq] = level; |
168 |
} |
169 |
#endif
|
170 |
#ifdef DEBUG_IRQ_LATENCY
|
171 |
if (level) {
|
172 |
irq_time[irq] = cpu_get_ticks(); |
173 |
} |
174 |
#endif
|
175 |
pic_set_irq1(&pics[irq >> 3], irq & 7, level); |
176 |
pic_update_irq(); |
177 |
} |
178 |
|
179 |
/* acknowledge interrupt 'irq' */
|
180 |
static inline void pic_intack(PicState *s, int irq) |
181 |
{ |
182 |
if (s->auto_eoi) {
|
183 |
if (s->rotate_on_auto_eoi)
|
184 |
s->priority_add = (irq + 1) & 7; |
185 |
} else {
|
186 |
s->isr |= (1 << irq);
|
187 |
} |
188 |
s->irr &= ~(1 << irq);
|
189 |
} |
190 |
|
191 |
int cpu_get_pic_interrupt(CPUState *env)
|
192 |
{ |
193 |
int irq, irq2, intno;
|
194 |
|
195 |
/* signal the pic that the irq was acked by the CPU */
|
196 |
irq = pic_irq_requested; |
197 |
#ifdef DEBUG_IRQ_LATENCY
|
198 |
printf("IRQ%d latency=%0.3fus\n",
|
199 |
irq, |
200 |
(double)(cpu_get_ticks() - irq_time[irq]) * 1000000.0 / ticks_per_sec); |
201 |
#endif
|
202 |
#if defined(DEBUG_PIC)
|
203 |
printf("pic_interrupt: irq=%d\n", irq);
|
204 |
#endif
|
205 |
|
206 |
if (irq >= 8) { |
207 |
irq2 = irq & 7;
|
208 |
pic_intack(&pics[1], irq2);
|
209 |
irq = 2;
|
210 |
intno = pics[1].irq_base + irq2;
|
211 |
} else {
|
212 |
intno = pics[0].irq_base + irq;
|
213 |
} |
214 |
pic_intack(&pics[0], irq);
|
215 |
pic_update_irq(); |
216 |
return intno;
|
217 |
} |
218 |
|
219 |
static void pic_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
220 |
{ |
221 |
PicState *s = opaque; |
222 |
int priority, cmd, irq, tmp;
|
223 |
|
224 |
#ifdef DEBUG_PIC
|
225 |
printf("pic_write: addr=0x%02x val=0x%02x\n", addr, val);
|
226 |
#endif
|
227 |
addr &= 1;
|
228 |
if (addr == 0) { |
229 |
if (val & 0x10) { |
230 |
/* init */
|
231 |
tmp = s->elcr_mask; |
232 |
memset(s, 0, sizeof(PicState)); |
233 |
s->elcr_mask = tmp; |
234 |
|
235 |
s->init_state = 1;
|
236 |
s->init4 = val & 1;
|
237 |
if (val & 0x02) |
238 |
hw_error("single mode not supported");
|
239 |
if (val & 0x08) |
240 |
hw_error("level sensitive irq not supported");
|
241 |
} else if (val & 0x08) { |
242 |
if (val & 0x04) |
243 |
s->poll = 1;
|
244 |
if (val & 0x02) |
245 |
s->read_reg_select = val & 1;
|
246 |
if (val & 0x40) |
247 |
s->special_mask = (val >> 5) & 1; |
248 |
} else {
|
249 |
cmd = val >> 5;
|
250 |
switch(cmd) {
|
251 |
case 0: |
252 |
case 4: |
253 |
s->rotate_on_auto_eoi = cmd >> 2;
|
254 |
break;
|
255 |
case 1: /* end of interrupt */ |
256 |
case 5: |
257 |
priority = get_priority(s, s->isr); |
258 |
if (priority != 8) { |
259 |
irq = (priority + s->priority_add) & 7;
|
260 |
s->isr &= ~(1 << irq);
|
261 |
if (cmd == 5) |
262 |
s->priority_add = (irq + 1) & 7; |
263 |
pic_update_irq(); |
264 |
} |
265 |
break;
|
266 |
case 3: |
267 |
irq = val & 7;
|
268 |
s->isr &= ~(1 << irq);
|
269 |
pic_update_irq(); |
270 |
break;
|
271 |
case 6: |
272 |
s->priority_add = (val + 1) & 7; |
273 |
pic_update_irq(); |
274 |
break;
|
275 |
case 7: |
276 |
irq = val & 7;
|
277 |
s->isr &= ~(1 << irq);
|
278 |
s->priority_add = (irq + 1) & 7; |
279 |
pic_update_irq(); |
280 |
break;
|
281 |
default:
|
282 |
/* no operation */
|
283 |
break;
|
284 |
} |
285 |
} |
286 |
} else {
|
287 |
switch(s->init_state) {
|
288 |
case 0: |
289 |
/* normal mode */
|
290 |
s->imr = val; |
291 |
pic_update_irq(); |
292 |
break;
|
293 |
case 1: |
294 |
s->irq_base = val & 0xf8;
|
295 |
s->init_state = 2;
|
296 |
break;
|
297 |
case 2: |
298 |
if (s->init4) {
|
299 |
s->init_state = 3;
|
300 |
} else {
|
301 |
s->init_state = 0;
|
302 |
} |
303 |
break;
|
304 |
case 3: |
305 |
s->special_fully_nested_mode = (val >> 4) & 1; |
306 |
s->auto_eoi = (val >> 1) & 1; |
307 |
s->init_state = 0;
|
308 |
break;
|
309 |
} |
310 |
} |
311 |
} |
312 |
|
313 |
static uint32_t pic_poll_read (PicState *s, uint32_t addr1)
|
314 |
{ |
315 |
int ret;
|
316 |
|
317 |
ret = pic_get_irq(s); |
318 |
if (ret >= 0) { |
319 |
if (addr1 >> 7) { |
320 |
pics[0].isr &= ~(1 << 2); |
321 |
pics[0].irr &= ~(1 << 2); |
322 |
} |
323 |
s->irr &= ~(1 << ret);
|
324 |
s->isr &= ~(1 << ret);
|
325 |
if (addr1 >> 7 || ret != 2) |
326 |
pic_update_irq(); |
327 |
} else {
|
328 |
ret = 0x07;
|
329 |
pic_update_irq(); |
330 |
} |
331 |
|
332 |
return ret;
|
333 |
} |
334 |
|
335 |
static uint32_t pic_ioport_read(void *opaque, uint32_t addr1) |
336 |
{ |
337 |
PicState *s = opaque; |
338 |
unsigned int addr; |
339 |
int ret;
|
340 |
|
341 |
addr = addr1; |
342 |
addr &= 1;
|
343 |
if (s->poll) {
|
344 |
ret = pic_poll_read(s, addr1); |
345 |
s->poll = 0;
|
346 |
} else {
|
347 |
if (addr == 0) { |
348 |
if (s->read_reg_select)
|
349 |
ret = s->isr; |
350 |
else
|
351 |
ret = s->irr; |
352 |
} else {
|
353 |
ret = s->imr; |
354 |
} |
355 |
} |
356 |
#ifdef DEBUG_PIC
|
357 |
printf("pic_read: addr=0x%02x val=0x%02x\n", addr1, ret);
|
358 |
#endif
|
359 |
return ret;
|
360 |
} |
361 |
|
362 |
/* memory mapped interrupt status */
|
363 |
uint32_t pic_intack_read(CPUState *env) |
364 |
{ |
365 |
int ret;
|
366 |
|
367 |
ret = pic_poll_read(&pics[0], 0x00); |
368 |
if (ret == 2) |
369 |
ret = pic_poll_read(&pics[1], 0x80) + 8; |
370 |
/* Prepare for ISR read */
|
371 |
pics[0].read_reg_select = 1; |
372 |
|
373 |
return ret;
|
374 |
} |
375 |
|
376 |
static void elcr_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
377 |
{ |
378 |
PicState *s = opaque; |
379 |
s->elcr = val & s->elcr_mask; |
380 |
} |
381 |
|
382 |
static uint32_t elcr_ioport_read(void *opaque, uint32_t addr1) |
383 |
{ |
384 |
PicState *s = opaque; |
385 |
return s->elcr;
|
386 |
} |
387 |
|
388 |
static void pic_save(QEMUFile *f, void *opaque) |
389 |
{ |
390 |
PicState *s = opaque; |
391 |
|
392 |
qemu_put_8s(f, &s->last_irr); |
393 |
qemu_put_8s(f, &s->irr); |
394 |
qemu_put_8s(f, &s->imr); |
395 |
qemu_put_8s(f, &s->isr); |
396 |
qemu_put_8s(f, &s->priority_add); |
397 |
qemu_put_8s(f, &s->irq_base); |
398 |
qemu_put_8s(f, &s->read_reg_select); |
399 |
qemu_put_8s(f, &s->poll); |
400 |
qemu_put_8s(f, &s->special_mask); |
401 |
qemu_put_8s(f, &s->init_state); |
402 |
qemu_put_8s(f, &s->auto_eoi); |
403 |
qemu_put_8s(f, &s->rotate_on_auto_eoi); |
404 |
qemu_put_8s(f, &s->special_fully_nested_mode); |
405 |
qemu_put_8s(f, &s->init4); |
406 |
qemu_put_8s(f, &s->elcr); |
407 |
} |
408 |
|
409 |
static int pic_load(QEMUFile *f, void *opaque, int version_id) |
410 |
{ |
411 |
PicState *s = opaque; |
412 |
|
413 |
if (version_id != 1) |
414 |
return -EINVAL;
|
415 |
|
416 |
qemu_get_8s(f, &s->last_irr); |
417 |
qemu_get_8s(f, &s->irr); |
418 |
qemu_get_8s(f, &s->imr); |
419 |
qemu_get_8s(f, &s->isr); |
420 |
qemu_get_8s(f, &s->priority_add); |
421 |
qemu_get_8s(f, &s->irq_base); |
422 |
qemu_get_8s(f, &s->read_reg_select); |
423 |
qemu_get_8s(f, &s->poll); |
424 |
qemu_get_8s(f, &s->special_mask); |
425 |
qemu_get_8s(f, &s->init_state); |
426 |
qemu_get_8s(f, &s->auto_eoi); |
427 |
qemu_get_8s(f, &s->rotate_on_auto_eoi); |
428 |
qemu_get_8s(f, &s->special_fully_nested_mode); |
429 |
qemu_get_8s(f, &s->init4); |
430 |
qemu_get_8s(f, &s->elcr); |
431 |
return 0; |
432 |
} |
433 |
|
434 |
/* XXX: add generic master/slave system */
|
435 |
static void pic_init1(int io_addr, int elcr_addr, PicState *s) |
436 |
{ |
437 |
register_ioport_write(io_addr, 2, 1, pic_ioport_write, s); |
438 |
register_ioport_read(io_addr, 2, 1, pic_ioport_read, s); |
439 |
if (elcr_addr >= 0) { |
440 |
register_ioport_write(elcr_addr, 1, 1, elcr_ioport_write, s); |
441 |
register_ioport_read(elcr_addr, 1, 1, elcr_ioport_read, s); |
442 |
} |
443 |
register_savevm("i8259", io_addr, 1, pic_save, pic_load, s); |
444 |
} |
445 |
|
446 |
void pic_info(void) |
447 |
{ |
448 |
int i;
|
449 |
PicState *s; |
450 |
|
451 |
for(i=0;i<2;i++) { |
452 |
s = &pics[i]; |
453 |
term_printf("pic%d: irr=%02x imr=%02x isr=%02x hprio=%d irq_base=%02x rr_sel=%d elcr=%02x\n",
|
454 |
i, s->irr, s->imr, s->isr, s->priority_add, |
455 |
s->irq_base, s->read_reg_select, s->elcr); |
456 |
} |
457 |
} |
458 |
|
459 |
|
460 |
void pic_init(void) |
461 |
{ |
462 |
pic_init1(0x20, 0x4d0, &pics[0]); |
463 |
pic_init1(0xa0, 0x4d1, &pics[1]); |
464 |
pics[0].elcr_mask = 0xf8; |
465 |
pics[1].elcr_mask = 0xde; |
466 |
} |
467 |
|