1 #include <devices/8259a.h>
2 #include <palacios/vmm_intr.h>
3 #include <palacios/vmm_types.h>
4 #include <palacios/vmm.h>
8 #define PrintDebug(fmt, args...)
9 //#define PrintPicTrace(_f, _a...) PrintTrace("\n8259a.c(%d) "_f, __LINE__, ## _a)
13 typedef enum {RESET, ICW1, ICW2, ICW3, ICW4, READY} pic_state_t;
15 static const uint_t MASTER_PORT1 = 0x20;
16 static const uint_t MASTER_PORT2 = 0x21;
17 static const uint_t SLAVE_PORT1 = 0xA0;
18 static const uint_t SLAVE_PORT2 = 0xA1;
20 #define IS_ICW1(x) (((x & 0x10) >> 4) == 0x1)
21 #define IS_OCW2(x) (((x & 0x18) >> 3) == 0x0)
22 #define IS_OCW3(x) (((x & 0x18) >> 3) == 0x1)
26 uint_t ic4 : 1; // ICW4 has to be read
27 uint_t sngl : 1; // single (only one PIC)
28 uint_t adi : 1; // call address interval
29 uint_t ltim : 1; // level interrupt mode
41 // Each bit that is set indicates that the IR input has a slave
53 // The ID is the Slave device ID
60 uint_t uPM : 1; // 1=x86
61 uint_t AEOI : 1; // Automatic End of Interrupt
62 uint_t M_S : 1; // only if buffered 1=master,0=slave
63 uint_t BUF : 1; // buffered mode
64 uint_t SFNM : 1; // special fully nexted mode
82 uint_t cw_code : 2; // should be 00
92 uint_t cw_code : 2; // should be 01
127 pic_state_t master_state;
128 pic_state_t slave_state;
132 static void DumpPICState(struct pic_internal *p)
135 PrintDebug("8259 PIC: master_state=0x%x\n",p->master_state);
136 PrintDebug("8259 PIC: master_irr=0x%x\n",p->master_irr);
137 PrintDebug("8259 PIC: master_isr=0x%x\n",p->master_isr);
138 PrintDebug("8259 PIC: master_imr=0x%x\n",p->master_imr);
140 PrintDebug("8259 PIC: master_ocw2=0x%x\n",p->master_ocw2);
141 PrintDebug("8259 PIC: master_ocw3=0x%x\n",p->master_ocw3);
143 PrintDebug("8259 PIC: master_icw1=0x%x\n",p->master_icw1);
144 PrintDebug("8259 PIC: master_icw2=0x%x\n",p->master_icw2);
145 PrintDebug("8259 PIC: master_icw3=0x%x\n",p->master_icw3);
146 PrintDebug("8259 PIC: master_icw4=0x%x\n",p->master_icw4);
148 PrintDebug("8259 PIC: slave_state=0x%x\n",p->slave_state);
149 PrintDebug("8259 PIC: slave_irr=0x%x\n",p->slave_irr);
150 PrintDebug("8259 PIC: slave_isr=0x%x\n",p->slave_isr);
151 PrintDebug("8259 PIC: slave_imr=0x%x\n",p->slave_imr);
153 PrintDebug("8259 PIC: slave_ocw2=0x%x\n",p->slave_ocw2);
154 PrintDebug("8259 PIC: slave_ocw3=0x%x\n",p->slave_ocw3);
156 PrintDebug("8259 PIC: slave_icw1=0x%x\n",p->slave_icw1);
157 PrintDebug("8259 PIC: slave_icw2=0x%x\n",p->slave_icw2);
158 PrintDebug("8259 PIC: slave_icw3=0x%x\n",p->slave_icw3);
159 PrintDebug("8259 PIC: slave_icw4=0x%x\n",p->slave_icw4);
164 static int pic_raise_intr(void * private_data, int irq) {
165 struct pic_internal * state = (struct pic_internal*)private_data;
169 state->master_irr |= 0x04; // PAD
172 PrintDebug("8259 PIC: Raising irq %d in the PIC\n", irq);
175 state->master_irr |= 0x01 << irq;
176 } else if ((irq > 7) && (irq < 16)) {
177 state->slave_irr |= 0x01 << (irq - 8); // PAD if -7 then irq 15=no irq
179 PrintError("8259 PIC: Invalid IRQ raised (%d)\n", irq);
189 static int pic_lower_intr(void *private_data, int irq_no) {
191 struct pic_internal *state = (struct pic_internal*)private_data;
193 PrintDebug("[pic_lower_intr] IRQ line %d now low\n", (unsigned) irq_no);
196 state->master_irr &= ~(1 << irq_no);
197 if ((state->master_irr & ~(state->master_imr)) == 0) {
198 PrintDebug("\t\tFIXME: Master maybe should do sth\n");
200 } else if ((irq_no > 7) && (irq_no <= 15)) {
202 state->slave_irr &= ~(1 << (irq_no - 8));
203 if ((state->slave_irr & (~(state->slave_imr))) == 0) {
204 PrintDebug("\t\tFIXME: Slave maybe should do sth\n");
212 static int pic_intr_pending(void * private_data) {
213 struct pic_internal * state = (struct pic_internal*)private_data;
215 if ((state->master_irr & ~(state->master_imr)) ||
216 (state->slave_irr & ~(state->slave_imr))) {
223 static int pic_get_intr_number(void * private_data) {
224 struct pic_internal * state = (struct pic_internal *)private_data;
228 PrintDebug("8259 PIC: getnum: master_irr: 0x%x master_imr: 0x%x\n", i, state->master_irr, state->master_imr);
229 PrintDebug("8259 PIC: getnum: slave_irr: 0x%x slave_imr: 0x%x\n", i, state->slave_irr, state->slave_imr);
231 for (i = 0; i < 16; i++) {
233 if (((state->master_irr & ~(state->master_imr)) >> i) == 0x01) {
234 //state->master_isr |= (0x1 << i);
236 //state->master_irr &= ~(0x1 << i);
237 PrintDebug("8259 PIC: IRQ: %d, master_icw2: %x\n", i, state->master_icw2);
238 irq= i + state->master_icw2;
242 if (((state->slave_irr & ~(state->slave_imr)) >> (i - 8)) == 0x01) {
243 //state->slave_isr |= (0x1 << (i - 8));
244 //state->slave_irr &= ~(0x1 << (i - 8));
245 PrintDebug("8259 PIC: IRQ: %d, slave_icw2: %x\n", i, state->slave_icw2);
246 irq= (i - 8) + state->slave_icw2;
252 if ((i == 15) || (i == 6)) {
265 /* The IRQ number is the number returned by pic_get_intr_number(), not the pin number */
266 static int pic_begin_irq(void * private_data, int irq) {
267 struct pic_internal * state = (struct pic_internal*)private_data;
269 if ((irq >= state->master_icw2) && (irq <= state->master_icw2 + 7)) {
271 } else if ((irq >= state->slave_icw2) && (irq <= state->slave_icw2 + 7)) {
275 PrintError("8259 PIC: Could not find IRQ (0x%x) to Begin\n",irq);
280 if (((state->master_irr & ~(state->master_imr)) >> irq) == 0x01) {
281 state->master_isr |= (0x1 << irq);
282 state->master_irr &= ~(0x1 << irq);
285 state->slave_isr |= (0x1 << (irq - 8));
286 state->slave_irr &= ~(0x1 << (irq - 8));
294 static int pic_end_irq(void * private_data, int irq) {
301 static struct intr_ctrl_ops intr_ops = {
302 .intr_pending = pic_intr_pending,
303 .get_intr_number = pic_get_intr_number,
304 .raise_intr = pic_raise_intr,
305 .begin_irq = pic_begin_irq,
306 .lower_intr = pic_lower_intr, //Zheng added
313 int read_master_port1(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
314 struct pic_internal * state = (struct pic_internal*)dev->private_data;
317 PrintError("8259 PIC: Invalid Read length (rd_Master1)\n");
321 if ((state->master_ocw3 & 0x03) == 0x02) {
322 *(uchar_t *)dst = state->master_irr;
323 } else if ((state->master_ocw3 & 0x03) == 0x03) {
324 *(uchar_t *)dst = state->master_isr;
332 int read_master_port2(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
333 struct pic_internal * state = (struct pic_internal*)dev->private_data;
336 PrintError("8259 PIC: Invalid Read length (rd_Master2)\n");
340 *(uchar_t *)dst = state->master_imr;
346 int read_slave_port1(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
347 struct pic_internal * state = (struct pic_internal*)dev->private_data;
350 PrintError("8259 PIC: Invalid Read length (rd_Slave1)\n");
354 if ((state->slave_ocw3 & 0x03) == 0x02) {
355 *(uchar_t*)dst = state->slave_irr;
356 } else if ((state->slave_ocw3 & 0x03) == 0x03) {
357 *(uchar_t *)dst = state->slave_isr;
365 int read_slave_port2(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
366 struct pic_internal * state = (struct pic_internal*)dev->private_data;
369 PrintError("8259 PIC: Invalid Read length (rd_Slave2)\n");
373 *(uchar_t *)dst = state->slave_imr;
379 int write_master_port1(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
380 struct pic_internal * state = (struct pic_internal*)dev->private_data;
381 uchar_t cw = *(uchar_t *)src;
383 PrintDebug("8259 PIC: Write master port 1 with 0x%x\n",cw);
386 PrintError("8259 PIC: Invalid Write length (wr_Master1)\n");
392 PrintDebug("8259 PIC: Setting ICW1 = %x (wr_Master1)\n", cw);
394 state->master_icw1 = cw;
395 state->master_state = ICW2;
397 } else if (state->master_state == READY) {
399 // handle the EOI here
400 struct ocw2 * cw2 = (struct ocw2*)&cw;
402 PrintDebug("8259 PIC: Handling OCW2 = %x (wr_Master1)\n", cw);
404 if ((cw2->EOI) && (!cw2->R) && (cw2->SL)) {
406 state->master_isr &= ~(0x01 << cw2->level);
407 } else if ((cw2->EOI) & (!cw2->R) && (!cw2->SL)) {
410 PrintDebug("8259 PIC: Pre ISR = %x (wr_Master1)\n", state->master_isr);
411 for (i = 0; i < 8; i++) {
412 if (state->master_isr & (0x01 << i)) {
413 state->master_isr &= ~(0x01 << i);
417 PrintDebug("8259 PIC: Post ISR = %x (wr_Master1)\n", state->master_isr);
419 PrintError("8259 PIC: Command not handled, or in error (wr_Master1)\n");
423 state->master_ocw2 = cw;
424 } else if (IS_OCW3(cw)) {
425 PrintDebug("8259 PIC: Handling OCW3 = %x (wr_Master1)\n", cw);
426 state->master_ocw3 = cw;
428 PrintError("8259 PIC: Invalid OCW to PIC (wr_Master1)\n");
429 PrintError("8259 PIC: CW=%x\n", cw);
433 PrintError("8259 PIC: Invalid PIC State (wr_Master1)\n");
434 PrintError("8259 PIC: CW=%x\n", cw);
441 int write_master_port2(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
442 struct pic_internal * state = (struct pic_internal*)dev->private_data;
443 uchar_t cw = *(uchar_t *)src;
445 PrintDebug("8259 PIC: Write master port 2 with 0x%x\n",cw);
448 PrintError("8259 PIC: Invalid Write length (wr_Master2)\n");
452 if (state->master_state == ICW2) {
453 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
455 PrintDebug("8259 PIC: Setting ICW2 = %x (wr_Master2)\n", cw);
456 state->master_icw2 = cw;
458 if (cw1->sngl == 0) {
459 state->master_state = ICW3;
460 } else if (cw1->ic4 == 1) {
461 state->master_state = ICW4;
463 state->master_state = READY;
466 } else if (state->master_state == ICW3) {
467 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
469 PrintDebug("8259 PIC: Setting ICW3 = %x (wr_Master2)\n", cw);
471 state->master_icw3 = cw;
474 state->master_state = ICW4;
476 state->master_state = READY;
479 } else if (state->master_state == ICW4) {
480 PrintDebug("8259 PIC: Setting ICW4 = %x (wr_Master2)\n", cw);
481 state->master_icw4 = cw;
482 state->master_state = READY;
483 } else if (state->master_state == READY) {
484 PrintDebug("8259 PIC: Setting IMR = %x (wr_Master2)\n", cw);
485 state->master_imr = cw;
488 PrintError("8259 PIC: Invalid master PIC State (wr_Master2)\n");
495 int write_slave_port1(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
496 struct pic_internal * state = (struct pic_internal*)dev->private_data;
497 uchar_t cw = *(uchar_t *)src;
499 PrintDebug("8259 PIC: Write slave port 1 with 0x%x\n",cw);
503 PrintError("8259 PIC: Invalid Write length (wr_Slave1)\n");
508 PrintDebug("8259 PIC: Setting ICW1 = %x (wr_Slave1)\n", cw);
509 state->slave_icw1 = cw;
510 state->slave_state = ICW2;
511 } else if (state->slave_state == READY) {
513 // handle the EOI here
514 struct ocw2 * cw2 = (struct ocw2 *)&cw;
516 PrintDebug("8259 PIC: Setting OCW2 = %x (wr_Slave1)\n", cw);
518 if ((cw2->EOI) && (!cw2->R) && (cw2->SL)) {
520 state->slave_isr &= ~(0x01 << cw2->level);
521 } else if ((cw2->EOI) & (!cw2->R) && (!cw2->SL)) {
524 PrintDebug("8259 PIC: Pre ISR = %x (wr_Slave1)\n", state->slave_isr);
525 for (i = 0; i < 8; i++) {
526 if (state->slave_isr & (0x01 << i)) {
527 state->slave_isr &= ~(0x01 << i);
531 PrintDebug("8259 PIC: Post ISR = %x (wr_Slave1)\n", state->slave_isr);
533 PrintError("8259 PIC: Command not handled or invalid (wr_Slave1)\n");
537 state->slave_ocw2 = cw;
538 } else if (IS_OCW3(cw)) {
539 // Basically sets the IRR/ISR read flag
540 PrintDebug("8259 PIC: Setting OCW3 = %x (wr_Slave1)\n", cw);
541 state->slave_ocw3 = cw;
543 PrintError("8259 PIC: Invalid command work (wr_Slave1)\n");
547 PrintError("8259 PIC: Invalid State writing (wr_Slave1)\n");
554 int write_slave_port2(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
555 struct pic_internal * state = (struct pic_internal*)dev->private_data;
556 uchar_t cw = *(uchar_t *)src;
558 PrintDebug("8259 PIC: Write slave port 2 with 0x%x\n",cw);
561 PrintError("8259 PIC: Invalid write length (wr_Slave2)\n");
565 if (state->slave_state == ICW2) {
566 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
568 PrintDebug("8259 PIC: Setting ICW2 = %x (wr_Slave2)\n", cw);
570 state->slave_icw2 = cw;
572 if (cw1->sngl == 0) {
573 state->slave_state = ICW3;
574 } else if (cw1->ic4 == 1) {
575 state->slave_state = ICW4;
577 state->slave_state = READY;
580 } else if (state->slave_state == ICW3) {
581 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
583 PrintDebug("8259 PIC: Setting ICW3 = %x (wr_Slave2)\n", cw);
585 state->slave_icw3 = cw;
588 state->slave_state = ICW4;
590 state->slave_state = READY;
593 } else if (state->slave_state == ICW4) {
594 PrintDebug("8259 PIC: Setting ICW4 = %x (wr_Slave2)\n", cw);
595 state->slave_icw4 = cw;
596 state->slave_state = READY;
597 } else if (state->slave_state == READY) {
598 PrintDebug("8259 PIC: Setting IMR = %x (wr_Slave2)\n", cw);
599 state->slave_imr = cw;
601 PrintError("8259 PIC: Invalid State at write (wr_Slave2)\n");
615 int pic_init(struct vm_device * dev) {
616 struct pic_internal * state = (struct pic_internal*)dev->private_data;
618 set_intr_controller(dev->vm, &intr_ops, state);
620 state->master_irr = 0;
621 state->master_isr = 0;
622 state->master_icw1 = 0;
623 state->master_icw2 = 0;
624 state->master_icw3 = 0;
625 state->master_icw4 = 0;
626 state->master_imr = 0;
627 state->master_ocw2 = 0;
628 state->master_ocw3 = 0x02;
629 state->master_state = ICW1;
632 state->slave_irr = 0;
633 state->slave_isr = 0;
634 state->slave_icw1 = 0;
635 state->slave_icw2 = 0;
636 state->slave_icw3 = 0;
637 state->slave_icw4 = 0;
638 state->slave_imr = 0;
639 state->slave_ocw2 = 0;
640 state->slave_ocw3 = 0x02;
641 state->slave_state = ICW1;
644 dev_hook_io(dev, MASTER_PORT1, &read_master_port1, &write_master_port1);
645 dev_hook_io(dev, MASTER_PORT2, &read_master_port2, &write_master_port2);
646 dev_hook_io(dev, SLAVE_PORT1, &read_slave_port1, &write_slave_port1);
647 dev_hook_io(dev, SLAVE_PORT2, &read_slave_port2, &write_slave_port2);
653 int pic_deinit(struct vm_device * dev) {
654 dev_unhook_io(dev, MASTER_PORT1);
655 dev_unhook_io(dev, MASTER_PORT2);
656 dev_unhook_io(dev, SLAVE_PORT1);
657 dev_unhook_io(dev, SLAVE_PORT2);
668 static struct vm_device_ops dev_ops = {
670 .deinit = pic_deinit,
677 struct vm_device * create_pic() {
678 struct pic_internal * state = NULL;
679 state = (struct pic_internal *)V3_Malloc(sizeof(struct pic_internal));
680 V3_ASSERT(state != NULL);
682 struct vm_device *device = create_device("8259A", &dev_ops, state);