1 #include <devices/8259a.h>
2 #include <palacios/vmm_intr.h>
3 #include <palacios/vmm_types.h>
4 #include <palacios/vmm.h>
7 typedef enum {RESET, ICW1, ICW2, ICW3, ICW4, READY} pic_state_t;
9 static const uint_t MASTER_PORT1 = 0x20;
10 static const uint_t MASTER_PORT2 = 0x21;
11 static const uint_t SLAVE_PORT1 = 0xA0;
12 static const uint_t SLAVE_PORT2 = 0xA1;
14 #define IS_ICW1(x) (((x & 0x10) >> 4) == 0x1)
15 #define IS_OCW2(x) (((x & 0x18) >> 3) == 0x0)
16 #define IS_OCW3(x) (((x & 0x18) >> 3) == 0x1)
20 uint_t ic4 : 1; // ICW4 has to be read
21 uint_t sngl : 1; // single (only one PIC)
22 uint_t adi : 1; // call address interval
23 uint_t ltim : 1; // level interrupt mode
35 // Each bit that is set indicates that the IR input has a slave
47 // The ID is the Slave device ID
54 uint_t uPM : 1; // 1=x86
55 uint_t AEOI : 1; // Automatic End of Interrupt
56 uint_t M_S : 1; // only if buffered 1=master,0=slave
57 uint_t BUF : 1; // buffered mode
58 uint_t SFNM : 1; // special fully nexted mode
76 uint_t cw_code : 2; // should be 00
86 uint_t cw_code : 2; // should be 01
121 pic_state_t master_state;
122 pic_state_t slave_state;
127 static int pic_raise_intr(void * private_data, int irq) {
128 struct pic_internal * state = (struct pic_internal*)private_data;
134 PrintDebug("8259 PIC: Raising irq %d in the PIC\n", irq);
137 state->master_irr |= 0x01 << irq;
138 } else if ((irq > 7) && (irq < 16)) {
139 state->slave_irr |= 0x01 << (irq - 7);
141 PrintDebug("8259 PIC: Invalid IRQ raised (%d)\n", irq);
148 static int pic_intr_pending(void * private_data) {
149 struct pic_internal * state = (struct pic_internal*)private_data;
151 if ((state->master_irr & ~(state->master_imr)) ||
152 (state->slave_irr & ~(state->slave_imr))) {
159 static int pic_get_intr_number(void * private_data) {
160 struct pic_internal * state = (struct pic_internal*)private_data;
163 for (i = 0; i < 16; i++) {
165 if (((state->master_irr & ~(state->master_imr)) >> i) == 0x01) {
166 //state->master_isr |= (0x1 << i);
168 //state->master_irr &= ~(0x1 << i);
169 PrintDebug("8259 PIC: IRQ: %d, icw2: %x\n", i, state->master_icw2);
170 return i + state->master_icw2;
173 if (((state->slave_irr & ~(state->slave_imr)) >> (i - 8)) == 0x01) {
174 //state->slave_isr |= (0x1 << (i - 8));
175 //state->slave_irr &= ~(0x1 << (i - 8));
176 return (i - 8) + state->slave_icw2;
186 /* The IRQ number is the number returned by pic_get_intr_number(), not the pin number */
187 static int pic_begin_irq(void * private_data, int irq) {
188 struct pic_internal * state = (struct pic_internal*)private_data;
190 if ((irq >= state->master_icw2) && (irq <= state->master_icw2 + 7)) {
192 } else if ((irq >= state->slave_icw2) && (irq <= state->slave_icw2 + 7)) {
196 PrintDebug("8259 PIC: Could not find IRQ to Begin\n");
201 if (((state->master_irr & ~(state->master_imr)) >> irq) == 0x01) {
202 state->master_isr |= (0x1 << irq);
203 state->master_irr &= ~(0x1 << irq);
206 state->slave_isr |= (0x1 << (irq - 8));
207 state->slave_irr &= ~(0x1 << (irq - 8));
215 static int pic_end_irq(void * private_data, int irq) {
220 static struct intr_ctrl_ops intr_ops = {
221 .intr_pending = pic_intr_pending,
222 .get_intr_number = pic_get_intr_number,
223 .raise_intr = pic_raise_intr,
224 .begin_irq = pic_begin_irq,
230 int read_master_port1(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
231 struct pic_internal * state = (struct pic_internal*)dev->private_data;
234 PrintDebug("8259 PIC: Invalid Read length (rd_Master1)\n");
238 if ((state->master_ocw3 & 0x03) == 0x02) {
239 *(char *)dst = state->master_irr;
240 } else if ((state->master_ocw3 & 0x03) == 0x03) {
241 *(char *)dst = state->master_isr;
249 int read_master_port2(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
250 struct pic_internal * state = (struct pic_internal*)dev->private_data;
253 PrintDebug("8259 PIC: Invalid Read length (rd_Master2)\n");
257 *(char *)dst = state->master_imr;
263 int read_slave_port1(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
264 struct pic_internal * state = (struct pic_internal*)dev->private_data;
267 PrintDebug("8259 PIC: Invalid Read length (rd_Slave1)\n");
271 if ((state->slave_ocw3 & 0x03) == 0x02) {
272 *(char*)dst = state->slave_irr;
273 } else if ((state->slave_ocw3 & 0x03) == 0x03) {
274 *(char *)dst = state->slave_isr;
282 int read_slave_port2(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
283 struct pic_internal * state = (struct pic_internal*)dev->private_data;
286 PrintDebug("8259 PIC: Invalid Read length (rd_Slave2)\n");
290 *(char *)dst = state->slave_imr;
296 int write_master_port1(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
297 struct pic_internal * state = (struct pic_internal*)dev->private_data;
298 char cw = *(char *)src;
301 PrintDebug("8259 PIC: Invalid Write length (wr_Master1)\n");
306 state->master_icw1 = cw;
307 state->master_state = ICW2;
309 } else if (state->master_state == READY) {
311 // handle the EOI here
312 struct ocw2 * cw2 = (struct ocw2*)&cw;
315 if ((cw2->EOI) && (!cw2->R) && (cw2->SL)) {
317 state->master_isr &= ~(0x01 << cw2->level);
318 } else if ((cw2->EOI) & (!cw2->R) && (!cw2->SL)) {
321 PrintDebug("8259 PIC: Pre ISR = %x (wr_Master1)\n", state->master_isr);
322 for (i = 0; i < 8; i++) {
323 if (state->master_isr & (0x01 << i)) {
324 state->master_isr &= ~(0x01 << i);
328 PrintDebug("8259 PIC: Post ISR = %x (wr_Master1)\n", state->master_isr);
330 PrintDebug("8259 PIC: Command not handled, or in error (wr_Master1)\n");
334 state->master_ocw2 = cw;
335 } else if (IS_OCW3(cw)) {
336 state->master_ocw3 = cw;
338 PrintDebug("8259 PIC: Invalid OCW to PIC (wr_Master1)\n");
339 PrintDebug("8259 PIC: CW=%x\n", cw);
343 PrintDebug("8259 PIC: Invalid PIC State (wr_Master1)\n");
344 PrintDebug("8259 PIC: CW=%x\n", cw);
351 int write_master_port2(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
352 struct pic_internal * state = (struct pic_internal*)dev->private_data;
353 char cw = *(char *)src;
356 PrintDebug("8259 PIC: Invalid Write length (wr_Master2)\n");
360 if (state->master_state == ICW2) {
361 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
363 PrintDebug("8259 PIC: Setting ICW2 = %x (wr_Master2)\n", cw);
364 state->master_icw2 = cw;
366 if (cw1->sngl == 0) {
367 state->master_state = ICW3;
368 } else if (cw1->ic4 == 1) {
369 state->master_state = ICW4;
371 state->master_state = READY;
374 } else if (state->master_state == ICW3) {
375 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
377 state->master_icw3 = cw;
380 state->master_state = ICW4;
382 state->master_state = READY;
385 } else if (state->master_state == ICW4) {
386 state->master_icw4 = cw;
387 state->master_state = READY;
388 } else if (state->master_state == READY) {
389 state->master_imr = cw;
392 PrintDebug("8259 PIC: Invalid master PIC State (wr_Master2)\n");
399 int write_slave_port1(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
400 struct pic_internal * state = (struct pic_internal*)dev->private_data;
401 char cw = *(char *)src;
405 PrintDebug("8259 PIC: Invalid Write length (wr_Slave1)\n");
410 state->slave_icw1 = cw;
411 state->slave_state = ICW2;
412 } else if (state->slave_state == READY) {
414 // handle the EOI here
415 struct ocw2 * cw2 = (struct ocw2 *)&cw;
417 if ((cw2->EOI) && (!cw2->R) && (cw2->SL)) {
419 state->slave_isr &= ~(0x01 << cw2->level);
420 } else if ((cw2->EOI) & (!cw2->R) && (!cw2->SL)) {
423 PrintDebug("8259 PIC: Pre ISR = %x (wr_Slave1)\n", state->slave_isr);
424 for (i = 0; i < 8; i++) {
425 if (state->slave_isr & (0x01 << i)) {
426 state->slave_isr &= ~(0x01 << i);
430 PrintDebug("8259 PIC: Post ISR = %x (wr_Slave1)\n", state->slave_isr);
432 PrintDebug("8259 PIC: Command not handled or invalid (wr_Slave1)\n");
436 state->slave_ocw2 = cw;
437 } else if (IS_OCW3(cw)) {
438 // Basically sets the IRR/ISR read flag
439 state->slave_ocw3 = cw;
441 PrintDebug("8259 PIC: Invalid command work (wr_Slave1)\n");
445 PrintDebug("8259 PIC: Invalid State writing (wr_Slave1)\n");
452 int write_slave_port2(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
453 struct pic_internal * state = (struct pic_internal*)dev->private_data;
454 char cw = *(char *)src;
457 PrintDebug("8259 PIC: Invalid write length (wr_Slave2)\n");
461 if (state->slave_state == ICW2) {
462 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
464 state->slave_icw2 = cw;
466 if (cw1->sngl == 0) {
467 state->slave_state = ICW3;
468 } else if (cw1->ic4 == 1) {
469 state->slave_state = ICW4;
471 state->slave_state = READY;
474 } else if (state->slave_state == ICW3) {
475 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
477 state->slave_icw3 = cw;
480 state->slave_state = ICW4;
482 state->slave_state = READY;
485 } else if (state->slave_state == ICW4) {
486 state->slave_icw4 = cw;
487 state->slave_state = READY;
488 } else if (state->slave_state == READY) {
489 state->slave_imr = cw;
491 PrintDebug("8259 PIC: Invalid State at write (wr_Slave2)\n");
505 int pic_init(struct vm_device * dev) {
506 struct pic_internal * state = (struct pic_internal*)dev->private_data;
508 set_intr_controller(dev->vm, &intr_ops, state);
510 state->master_irr = 0;
511 state->master_isr = 0;
512 state->master_icw1 = 0;
513 state->master_icw2 = 0;
514 state->master_icw3 = 0;
515 state->master_icw4 = 0;
516 state->master_imr = 0;
517 state->master_ocw2 = 0;
518 state->master_ocw3 = 0x02;
519 state->master_state = ICW1;
522 state->slave_irr = 0;
523 state->slave_isr = 0;
524 state->slave_icw1 = 0;
525 state->slave_icw2 = 0;
526 state->slave_icw3 = 0;
527 state->slave_icw4 = 0;
528 state->slave_imr = 0;
529 state->slave_ocw2 = 0;
530 state->slave_ocw3 = 0x02;
531 state->slave_state = ICW1;
534 dev_hook_io(dev, MASTER_PORT1, &read_master_port1, &write_master_port1);
535 dev_hook_io(dev, MASTER_PORT2, &read_master_port2, &write_master_port2);
536 dev_hook_io(dev, SLAVE_PORT1, &read_slave_port1, &write_slave_port1);
537 dev_hook_io(dev, SLAVE_PORT2, &read_slave_port2, &write_slave_port2);
543 int pic_deinit(struct vm_device * dev) {
544 dev_unhook_io(dev, MASTER_PORT1);
545 dev_unhook_io(dev, MASTER_PORT2);
546 dev_unhook_io(dev, SLAVE_PORT1);
547 dev_unhook_io(dev, SLAVE_PORT2);
558 static struct vm_device_ops dev_ops = {
560 .deinit = pic_deinit,
567 struct vm_device * create_pic() {
568 struct pic_internal * state = NULL;
569 state = (struct pic_internal *)V3_Malloc(sizeof(struct pic_internal));
570 V3_ASSERT(state != NULL);
572 struct vm_device *device = create_device("8259A", &dev_ops, state);