root / hw / i8259.c @ b41a2cd1
History | View | Annotate | Download (10.2 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 <stdlib.h> |
25 |
#include <stdio.h> |
26 |
#include <stdarg.h> |
27 |
#include <string.h> |
28 |
#include <getopt.h> |
29 |
#include <inttypes.h> |
30 |
#include <unistd.h> |
31 |
#include <sys/mman.h> |
32 |
#include <fcntl.h> |
33 |
#include <signal.h> |
34 |
#include <time.h> |
35 |
#include <sys/time.h> |
36 |
#include <malloc.h> |
37 |
#include <termios.h> |
38 |
#include <sys/poll.h> |
39 |
#include <errno.h> |
40 |
#include <sys/wait.h> |
41 |
#include <netinet/in.h> |
42 |
|
43 |
#include "cpu.h" |
44 |
#include "vl.h" |
45 |
|
46 |
/* debug PIC */
|
47 |
//#define DEBUG_PIC
|
48 |
|
49 |
//#define DEBUG_IRQ_LATENCY
|
50 |
|
51 |
typedef struct PicState { |
52 |
uint8_t last_irr; /* edge detection */
|
53 |
uint8_t irr; /* interrupt request register */
|
54 |
uint8_t imr; /* interrupt mask register */
|
55 |
uint8_t isr; /* interrupt service register */
|
56 |
uint8_t priority_add; /* highest irq priority */
|
57 |
uint8_t irq_base; |
58 |
uint8_t read_reg_select; |
59 |
uint8_t poll; |
60 |
uint8_t special_mask; |
61 |
uint8_t init_state; |
62 |
uint8_t auto_eoi; |
63 |
uint8_t rotate_on_auto_eoi; |
64 |
uint8_t special_fully_nested_mode; |
65 |
uint8_t init4; /* true if 4 byte init */
|
66 |
} PicState; |
67 |
|
68 |
/* 0 is master pic, 1 is slave pic */
|
69 |
PicState pics[2];
|
70 |
int pic_irq_requested;
|
71 |
|
72 |
/* set irq level. If an edge is detected, then the IRR is set to 1 */
|
73 |
static inline void pic_set_irq1(PicState *s, int irq, int level) |
74 |
{ |
75 |
int mask;
|
76 |
mask = 1 << irq;
|
77 |
if (level) {
|
78 |
if ((s->last_irr & mask) == 0) |
79 |
s->irr |= mask; |
80 |
s->last_irr |= mask; |
81 |
} else {
|
82 |
s->last_irr &= ~mask; |
83 |
} |
84 |
} |
85 |
|
86 |
/* return the highest priority found in mask (highest = smallest
|
87 |
number). Return 8 if no irq */
|
88 |
static inline int get_priority(PicState *s, int mask) |
89 |
{ |
90 |
int priority;
|
91 |
if (mask == 0) |
92 |
return 8; |
93 |
priority = 0;
|
94 |
while ((mask & (1 << ((priority + s->priority_add) & 7))) == 0) |
95 |
priority++; |
96 |
return priority;
|
97 |
} |
98 |
|
99 |
/* return the pic wanted interrupt. return -1 if none */
|
100 |
static int pic_get_irq(PicState *s) |
101 |
{ |
102 |
int mask, cur_priority, priority;
|
103 |
|
104 |
mask = s->irr & ~s->imr; |
105 |
priority = get_priority(s, mask); |
106 |
if (priority == 8) |
107 |
return -1; |
108 |
/* 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; |
112 |
if (s->special_fully_nested_mode && s == &pics[0]) |
113 |
mask &= ~(1 << 2); |
114 |
cur_priority = get_priority(s, mask); |
115 |
if (priority < cur_priority) {
|
116 |
/* higher priority found: an irq should be generated */
|
117 |
return (priority + s->priority_add) & 7; |
118 |
} else {
|
119 |
return -1; |
120 |
} |
121 |
} |
122 |
|
123 |
/* raise irq to CPU if necessary. must be called every time the active
|
124 |
irq may change */
|
125 |
void pic_update_irq(void) |
126 |
{ |
127 |
int irq2, irq;
|
128 |
|
129 |
/* first look at slave pic */
|
130 |
irq2 = pic_get_irq(&pics[1]);
|
131 |
if (irq2 >= 0) { |
132 |
/* if irq request by slave pic, signal master PIC */
|
133 |
pic_set_irq1(&pics[0], 2, 1); |
134 |
pic_set_irq1(&pics[0], 2, 0); |
135 |
} |
136 |
/* look at requested irq */
|
137 |
irq = pic_get_irq(&pics[0]);
|
138 |
if (irq >= 0) { |
139 |
if (irq == 2) { |
140 |
/* from slave pic */
|
141 |
pic_irq_requested = 8 + irq2;
|
142 |
} else {
|
143 |
/* from master pic */
|
144 |
pic_irq_requested = irq; |
145 |
} |
146 |
#if defined(DEBUG_PIC)
|
147 |
{ |
148 |
int i;
|
149 |
for(i = 0; i < 2; i++) { |
150 |
printf("pic%d: imr=%x irr=%x padd=%d\n",
|
151 |
i, pics[i].imr, pics[i].irr, pics[i].priority_add); |
152 |
|
153 |
} |
154 |
} |
155 |
printf("pic: cpu_interrupt req=%d\n", pic_irq_requested);
|
156 |
#endif
|
157 |
cpu_interrupt(cpu_single_env, CPU_INTERRUPT_HARD); |
158 |
} |
159 |
} |
160 |
|
161 |
#ifdef DEBUG_IRQ_LATENCY
|
162 |
int64_t irq_time[16];
|
163 |
int64_t cpu_get_ticks(void);
|
164 |
#endif
|
165 |
#if defined(DEBUG_PIC)
|
166 |
int irq_level[16]; |
167 |
#endif
|
168 |
|
169 |
void pic_set_irq(int irq, int level) |
170 |
{ |
171 |
#if defined(DEBUG_PIC)
|
172 |
if (level != irq_level[irq]) {
|
173 |
printf("pic_set_irq: irq=%d level=%d\n", irq, level);
|
174 |
irq_level[irq] = level; |
175 |
} |
176 |
#endif
|
177 |
#ifdef DEBUG_IRQ_LATENCY
|
178 |
if (level) {
|
179 |
irq_time[irq] = cpu_get_ticks(); |
180 |
} |
181 |
#endif
|
182 |
pic_set_irq1(&pics[irq >> 3], irq & 7, level); |
183 |
pic_update_irq(); |
184 |
} |
185 |
|
186 |
/* acknowledge interrupt 'irq' */
|
187 |
static inline void pic_intack(PicState *s, int irq) |
188 |
{ |
189 |
if (s->auto_eoi) {
|
190 |
if (s->rotate_on_auto_eoi)
|
191 |
s->priority_add = (irq + 1) & 7; |
192 |
} else {
|
193 |
s->isr |= (1 << irq);
|
194 |
} |
195 |
s->irr &= ~(1 << irq);
|
196 |
} |
197 |
|
198 |
int cpu_x86_get_pic_interrupt(CPUState *env)
|
199 |
{ |
200 |
int irq, irq2, intno;
|
201 |
|
202 |
/* signal the pic that the irq was acked by the CPU */
|
203 |
irq = pic_irq_requested; |
204 |
#ifdef DEBUG_IRQ_LATENCY
|
205 |
printf("IRQ%d latency=%0.3fus\n",
|
206 |
irq, |
207 |
(double)(cpu_get_ticks() - irq_time[irq]) * 1000000.0 / ticks_per_sec); |
208 |
#endif
|
209 |
#if defined(DEBUG_PIC)
|
210 |
printf("pic_interrupt: irq=%d\n", irq);
|
211 |
#endif
|
212 |
|
213 |
if (irq >= 8) { |
214 |
irq2 = irq & 7;
|
215 |
pic_intack(&pics[1], irq2);
|
216 |
irq = 2;
|
217 |
intno = pics[1].irq_base + irq2;
|
218 |
} else {
|
219 |
intno = pics[0].irq_base + irq;
|
220 |
} |
221 |
pic_intack(&pics[0], irq);
|
222 |
return intno;
|
223 |
} |
224 |
|
225 |
static void pic_ioport_write(void *opaque, uint32_t addr, uint32_t val) |
226 |
{ |
227 |
PicState *s = opaque; |
228 |
int priority, cmd, irq;
|
229 |
|
230 |
#ifdef DEBUG_PIC
|
231 |
printf("pic_write: addr=0x%02x val=0x%02x\n", addr, val);
|
232 |
#endif
|
233 |
addr &= 1;
|
234 |
if (addr == 0) { |
235 |
if (val & 0x10) { |
236 |
/* init */
|
237 |
memset(s, 0, sizeof(PicState)); |
238 |
s->init_state = 1;
|
239 |
s->init4 = val & 1;
|
240 |
if (val & 0x02) |
241 |
hw_error("single mode not supported");
|
242 |
if (val & 0x08) |
243 |
hw_error("level sensitive irq not supported");
|
244 |
} else if (val & 0x08) { |
245 |
if (val & 0x04) |
246 |
s->poll = 1;
|
247 |
if (val & 0x02) |
248 |
s->read_reg_select = val & 1;
|
249 |
if (val & 0x40) |
250 |
s->special_mask = (val >> 5) & 1; |
251 |
} else {
|
252 |
cmd = val >> 5;
|
253 |
switch(cmd) {
|
254 |
case 0: |
255 |
case 4: |
256 |
s->rotate_on_auto_eoi = cmd >> 2;
|
257 |
break;
|
258 |
case 1: /* end of interrupt */ |
259 |
case 5: |
260 |
priority = get_priority(s, s->isr); |
261 |
if (priority != 8) { |
262 |
irq = (priority + s->priority_add) & 7;
|
263 |
s->isr &= ~(1 << irq);
|
264 |
if (cmd == 5) |
265 |
s->priority_add = (irq + 1) & 7; |
266 |
pic_update_irq(); |
267 |
} |
268 |
break;
|
269 |
case 3: |
270 |
irq = val & 7;
|
271 |
s->isr &= ~(1 << irq);
|
272 |
pic_update_irq(); |
273 |
break;
|
274 |
case 6: |
275 |
s->priority_add = (val + 1) & 7; |
276 |
pic_update_irq(); |
277 |
break;
|
278 |
case 7: |
279 |
irq = val & 7;
|
280 |
s->isr &= ~(1 << irq);
|
281 |
s->priority_add = (irq + 1) & 7; |
282 |
pic_update_irq(); |
283 |
break;
|
284 |
default:
|
285 |
/* no operation */
|
286 |
break;
|
287 |
} |
288 |
} |
289 |
} else {
|
290 |
switch(s->init_state) {
|
291 |
case 0: |
292 |
/* normal mode */
|
293 |
s->imr = val; |
294 |
pic_update_irq(); |
295 |
break;
|
296 |
case 1: |
297 |
s->irq_base = val & 0xf8;
|
298 |
s->init_state = 2;
|
299 |
break;
|
300 |
case 2: |
301 |
if (s->init4) {
|
302 |
s->init_state = 3;
|
303 |
} else {
|
304 |
s->init_state = 0;
|
305 |
} |
306 |
break;
|
307 |
case 3: |
308 |
s->special_fully_nested_mode = (val >> 4) & 1; |
309 |
s->auto_eoi = (val >> 1) & 1; |
310 |
s->init_state = 0;
|
311 |
break;
|
312 |
} |
313 |
} |
314 |
} |
315 |
|
316 |
static uint32_t pic_poll_read (PicState *s, uint32_t addr1)
|
317 |
{ |
318 |
int ret;
|
319 |
|
320 |
ret = pic_get_irq(s); |
321 |
if (ret >= 0) { |
322 |
if (addr1 >> 7) { |
323 |
pics[0].isr &= ~(1 << 2); |
324 |
pics[0].irr &= ~(1 << 2); |
325 |
} |
326 |
s->irr &= ~(1 << ret);
|
327 |
s->isr &= ~(1 << ret);
|
328 |
if (addr1 >> 7 || ret != 2) |
329 |
pic_update_irq(); |
330 |
} else {
|
331 |
ret = 0x07;
|
332 |
pic_update_irq(); |
333 |
} |
334 |
|
335 |
return ret;
|
336 |
} |
337 |
|
338 |
static uint32_t pic_ioport_read(void *opaque, uint32_t addr1) |
339 |
{ |
340 |
PicState *s = opaque; |
341 |
unsigned int addr; |
342 |
int ret;
|
343 |
|
344 |
addr = addr1; |
345 |
addr &= 1;
|
346 |
if (s->poll) {
|
347 |
ret = pic_poll_read(s, addr1); |
348 |
s->poll = 0;
|
349 |
} else {
|
350 |
if (addr == 0) { |
351 |
if (s->read_reg_select)
|
352 |
ret = s->isr; |
353 |
else
|
354 |
ret = s->irr; |
355 |
} else {
|
356 |
ret = s->imr; |
357 |
} |
358 |
} |
359 |
#ifdef DEBUG_PIC
|
360 |
printf("pic_read: addr=0x%02x val=0x%02x\n", addr1, ret);
|
361 |
#endif
|
362 |
return ret;
|
363 |
} |
364 |
|
365 |
/* memory mapped interrupt status */
|
366 |
uint32_t pic_intack_read(CPUState *env) |
367 |
{ |
368 |
int ret;
|
369 |
|
370 |
ret = pic_poll_read(&pics[0], 0x00); |
371 |
if (ret == 2) |
372 |
ret = pic_poll_read(&pics[1], 0x80) + 8; |
373 |
/* Prepare for ISR read */
|
374 |
pics[0].read_reg_select = 1; |
375 |
|
376 |
return ret;
|
377 |
} |
378 |
|
379 |
void pic_init(void) |
380 |
{ |
381 |
register_ioport_write(0x20, 2, 1, pic_ioport_write, &pics[0]); |
382 |
register_ioport_read(0x20, 2, 1, pic_ioport_read, &pics[0]); |
383 |
register_ioport_write(0xa0, 2, 1, pic_ioport_write, &pics[1]); |
384 |
register_ioport_read(0xa0, 2, 1, pic_ioport_read, &pics[1]); |
385 |
} |
386 |
|