2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
21 #include <devices/8259a.h>
22 #include <palacios/vmm_intr.h>
23 #include <palacios/vmm_types.h>
24 #include <palacios/vmm.h>
28 #define PrintDebug(fmt, args...)
32 typedef enum {RESET, ICW1, ICW2, ICW3, ICW4, READY} pic_state_t;
34 static const uint_t MASTER_PORT1 = 0x20;
35 static const uint_t MASTER_PORT2 = 0x21;
36 static const uint_t SLAVE_PORT1 = 0xA0;
37 static const uint_t SLAVE_PORT2 = 0xA1;
39 #define IS_ICW1(x) (((x & 0x10) >> 4) == 0x1)
40 #define IS_OCW2(x) (((x & 0x18) >> 3) == 0x0)
41 #define IS_OCW3(x) (((x & 0x18) >> 3) == 0x1)
45 uint_t ic4 : 1; // ICW4 has to be read
46 uint_t sngl : 1; // single (only one PIC)
47 uint_t adi : 1; // call address interval
48 uint_t ltim : 1; // level interrupt mode
60 // Each bit that is set indicates that the IR input has a slave
72 // The ID is the Slave device ID
79 uint_t uPM : 1; // 1=x86
80 uint_t AEOI : 1; // Automatic End of Interrupt
81 uint_t M_S : 1; // only if buffered 1=master,0=slave
82 uint_t BUF : 1; // buffered mode
83 uint_t SFNM : 1; // special fully nexted mode
101 uint_t cw_code : 2; // should be 00
111 uint_t cw_code : 2; // should be 01
118 struct pic_internal {
146 pic_state_t master_state;
147 pic_state_t slave_state;
151 static void DumpPICState(struct pic_internal *p)
154 PrintDebug("8259 PIC: master_state=0x%x\n",p->master_state);
155 PrintDebug("8259 PIC: master_irr=0x%x\n",p->master_irr);
156 PrintDebug("8259 PIC: master_isr=0x%x\n",p->master_isr);
157 PrintDebug("8259 PIC: master_imr=0x%x\n",p->master_imr);
159 PrintDebug("8259 PIC: master_ocw2=0x%x\n",p->master_ocw2);
160 PrintDebug("8259 PIC: master_ocw3=0x%x\n",p->master_ocw3);
162 PrintDebug("8259 PIC: master_icw1=0x%x\n",p->master_icw1);
163 PrintDebug("8259 PIC: master_icw2=0x%x\n",p->master_icw2);
164 PrintDebug("8259 PIC: master_icw3=0x%x\n",p->master_icw3);
165 PrintDebug("8259 PIC: master_icw4=0x%x\n",p->master_icw4);
167 PrintDebug("8259 PIC: slave_state=0x%x\n",p->slave_state);
168 PrintDebug("8259 PIC: slave_irr=0x%x\n",p->slave_irr);
169 PrintDebug("8259 PIC: slave_isr=0x%x\n",p->slave_isr);
170 PrintDebug("8259 PIC: slave_imr=0x%x\n",p->slave_imr);
172 PrintDebug("8259 PIC: slave_ocw2=0x%x\n",p->slave_ocw2);
173 PrintDebug("8259 PIC: slave_ocw3=0x%x\n",p->slave_ocw3);
175 PrintDebug("8259 PIC: slave_icw1=0x%x\n",p->slave_icw1);
176 PrintDebug("8259 PIC: slave_icw2=0x%x\n",p->slave_icw2);
177 PrintDebug("8259 PIC: slave_icw3=0x%x\n",p->slave_icw3);
178 PrintDebug("8259 PIC: slave_icw4=0x%x\n",p->slave_icw4);
183 static int pic_raise_intr(void * private_data, int irq) {
184 struct pic_internal * state = (struct pic_internal*)private_data;
188 state->master_irr |= 0x04; // PAD
191 PrintDebug("8259 PIC: Raising irq %d in the PIC\n", irq);
194 state->master_irr |= 0x01 << irq;
195 } else if ((irq > 7) && (irq < 16)) {
196 state->slave_irr |= 0x01 << (irq - 8); // PAD if -7 then irq 15=no irq
198 PrintDebug("8259 PIC: Invalid IRQ raised (%d)\n", irq);
206 static int pic_lower_intr(void *private_data, int irq) {
208 struct pic_internal *state = (struct pic_internal*)private_data;
210 PrintDebug("[pic_lower_intr] IRQ line %d now low\n", irq);
213 state->master_irr &= ~(1 << irq);
214 if ((state->master_irr & ~(state->master_imr)) == 0) {
215 PrintDebug("\t\tFIXME: Master maybe should do sth\n");
217 } else if ((irq > 7) && (irq < 16)) {
219 state->slave_irr &= ~(1 << (irq - 8));
220 if ((state->slave_irr & (~(state->slave_imr))) == 0) {
221 PrintDebug("\t\tFIXME: Slave maybe should do sth\n");
229 static int pic_intr_pending(void * private_data) {
230 struct pic_internal * state = (struct pic_internal*)private_data;
232 if ((state->master_irr & ~(state->master_imr)) ||
233 (state->slave_irr & ~(state->slave_imr))) {
240 static int pic_get_intr_number(void * private_data) {
241 struct pic_internal * state = (struct pic_internal *)private_data;
245 PrintDebug("8259 PIC: getnum: master_irr: 0x%x master_imr: 0x%x\n", state->master_irr, state->master_imr);
246 PrintDebug("8259 PIC: getnum: slave_irr: 0x%x slave_imr: 0x%x\n", state->slave_irr, state->slave_imr);
248 for (i = 0; i < 16; i++) {
250 if (((state->master_irr & ~(state->master_imr)) >> i) == 0x01) {
251 //state->master_isr |= (0x1 << i);
253 //state->master_irr &= ~(0x1 << i);
254 PrintDebug("8259 PIC: IRQ: %d, master_icw2: %x\n", i, state->master_icw2);
255 irq= i + state->master_icw2;
259 if (((state->slave_irr & ~(state->slave_imr)) >> (i - 8)) == 0x01) {
260 //state->slave_isr |= (0x1 << (i - 8));
261 //state->slave_irr &= ~(0x1 << (i - 8));
262 PrintDebug("8259 PIC: IRQ: %d, slave_icw2: %x\n", i, state->slave_icw2);
263 irq= (i - 8) + state->slave_icw2;
269 if ((i == 15) || (i == 6)) {
282 /* The IRQ number is the number returned by pic_get_intr_number(), not the pin number */
283 static int pic_begin_irq(void * private_data, int irq) {
284 struct pic_internal * state = (struct pic_internal*)private_data;
286 if ((irq >= state->master_icw2) && (irq <= state->master_icw2 + 7)) {
288 } else if ((irq >= state->slave_icw2) && (irq <= state->slave_icw2 + 7)) {
292 // PrintError("8259 PIC: Could not find IRQ (0x%x) to Begin\n",irq);
297 if (((state->master_irr & ~(state->master_imr)) >> irq) == 0x01) {
298 state->master_isr |= (0x1 << irq);
299 state->master_irr &= ~(0x1 << irq);
302 state->slave_isr |= (0x1 << (irq - 8));
303 state->slave_irr &= ~(0x1 << (irq - 8));
311 static int pic_end_irq(void * private_data, int irq) {
318 static struct intr_ctrl_ops intr_ops = {
319 .intr_pending = pic_intr_pending,
320 .get_intr_number = pic_get_intr_number,
321 .raise_intr = pic_raise_intr,
322 .begin_irq = pic_begin_irq,
323 .lower_intr = pic_lower_intr,
330 static int read_master_port1(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
331 struct pic_internal * state = (struct pic_internal*)dev->private_data;
334 PrintError("8259 PIC: Invalid Read length (rd_Master1)\n");
338 if ((state->master_ocw3 & 0x03) == 0x02) {
339 *(uchar_t *)dst = state->master_irr;
340 } else if ((state->master_ocw3 & 0x03) == 0x03) {
341 *(uchar_t *)dst = state->master_isr;
349 static int read_master_port2(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
350 struct pic_internal * state = (struct pic_internal*)dev->private_data;
353 PrintError("8259 PIC: Invalid Read length (rd_Master2)\n");
357 *(uchar_t *)dst = state->master_imr;
363 static int read_slave_port1(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
364 struct pic_internal * state = (struct pic_internal*)dev->private_data;
367 PrintError("8259 PIC: Invalid Read length (rd_Slave1)\n");
371 if ((state->slave_ocw3 & 0x03) == 0x02) {
372 *(uchar_t*)dst = state->slave_irr;
373 } else if ((state->slave_ocw3 & 0x03) == 0x03) {
374 *(uchar_t *)dst = state->slave_isr;
382 static int read_slave_port2(ushort_t port, void * dst, uint_t length, struct vm_device * dev) {
383 struct pic_internal * state = (struct pic_internal*)dev->private_data;
386 PrintError("8259 PIC: Invalid Read length (rd_Slave2)\n");
390 *(uchar_t *)dst = state->slave_imr;
396 static int write_master_port1(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
397 struct pic_internal * state = (struct pic_internal*)dev->private_data;
398 uchar_t cw = *(uchar_t *)src;
400 PrintDebug("8259 PIC: Write master port 1 with 0x%x\n",cw);
403 PrintError("8259 PIC: Invalid Write length (wr_Master1)\n");
409 PrintDebug("8259 PIC: Setting ICW1 = %x (wr_Master1)\n", cw);
411 state->master_icw1 = cw;
412 state->master_state = ICW2;
414 } else if (state->master_state == READY) {
416 // handle the EOI here
417 struct ocw2 * cw2 = (struct ocw2*)&cw;
419 PrintDebug("8259 PIC: Handling OCW2 = %x (wr_Master1)\n", cw);
421 if ((cw2->EOI) && (!cw2->R) && (cw2->SL)) {
423 state->master_isr &= ~(0x01 << cw2->level);
424 } else if ((cw2->EOI) & (!cw2->R) && (!cw2->SL)) {
427 PrintDebug("8259 PIC: Pre ISR = %x (wr_Master1)\n", state->master_isr);
428 for (i = 0; i < 8; i++) {
429 if (state->master_isr & (0x01 << i)) {
430 state->master_isr &= ~(0x01 << i);
434 PrintDebug("8259 PIC: Post ISR = %x (wr_Master1)\n", state->master_isr);
436 PrintError("8259 PIC: Command not handled, or in error (wr_Master1)\n");
440 state->master_ocw2 = cw;
441 } else if (IS_OCW3(cw)) {
442 PrintDebug("8259 PIC: Handling OCW3 = %x (wr_Master1)\n", cw);
443 state->master_ocw3 = cw;
445 PrintError("8259 PIC: Invalid OCW to PIC (wr_Master1)\n");
446 PrintError("8259 PIC: CW=%x\n", cw);
450 PrintError("8259 PIC: Invalid PIC State (wr_Master1)\n");
451 PrintError("8259 PIC: CW=%x\n", cw);
458 static int write_master_port2(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
459 struct pic_internal * state = (struct pic_internal*)dev->private_data;
460 uchar_t cw = *(uchar_t *)src;
462 PrintDebug("8259 PIC: Write master port 2 with 0x%x\n",cw);
465 PrintError("8259 PIC: Invalid Write length (wr_Master2)\n");
469 if (state->master_state == ICW2) {
470 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
472 PrintDebug("8259 PIC: Setting ICW2 = %x (wr_Master2)\n", cw);
473 state->master_icw2 = cw;
475 if (cw1->sngl == 0) {
476 state->master_state = ICW3;
477 } else if (cw1->ic4 == 1) {
478 state->master_state = ICW4;
480 state->master_state = READY;
483 } else if (state->master_state == ICW3) {
484 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
486 PrintDebug("8259 PIC: Setting ICW3 = %x (wr_Master2)\n", cw);
488 state->master_icw3 = cw;
491 state->master_state = ICW4;
493 state->master_state = READY;
496 } else if (state->master_state == ICW4) {
497 PrintDebug("8259 PIC: Setting ICW4 = %x (wr_Master2)\n", cw);
498 state->master_icw4 = cw;
499 state->master_state = READY;
500 } else if (state->master_state == READY) {
501 PrintDebug("8259 PIC: Setting IMR = %x (wr_Master2)\n", cw);
502 state->master_imr = cw;
505 PrintError("8259 PIC: Invalid master PIC State (wr_Master2)\n");
512 static int write_slave_port1(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
513 struct pic_internal * state = (struct pic_internal*)dev->private_data;
514 uchar_t cw = *(uchar_t *)src;
516 PrintDebug("8259 PIC: Write slave port 1 with 0x%x\n",cw);
520 PrintError("8259 PIC: Invalid Write length (wr_Slave1)\n");
525 PrintDebug("8259 PIC: Setting ICW1 = %x (wr_Slave1)\n", cw);
526 state->slave_icw1 = cw;
527 state->slave_state = ICW2;
528 } else if (state->slave_state == READY) {
530 // handle the EOI here
531 struct ocw2 * cw2 = (struct ocw2 *)&cw;
533 PrintDebug("8259 PIC: Setting OCW2 = %x (wr_Slave1)\n", cw);
535 if ((cw2->EOI) && (!cw2->R) && (cw2->SL)) {
537 state->slave_isr &= ~(0x01 << cw2->level);
538 } else if ((cw2->EOI) & (!cw2->R) && (!cw2->SL)) {
541 PrintDebug("8259 PIC: Pre ISR = %x (wr_Slave1)\n", state->slave_isr);
542 for (i = 0; i < 8; i++) {
543 if (state->slave_isr & (0x01 << i)) {
544 state->slave_isr &= ~(0x01 << i);
548 PrintDebug("8259 PIC: Post ISR = %x (wr_Slave1)\n", state->slave_isr);
550 PrintError("8259 PIC: Command not handled or invalid (wr_Slave1)\n");
554 state->slave_ocw2 = cw;
555 } else if (IS_OCW3(cw)) {
556 // Basically sets the IRR/ISR read flag
557 PrintDebug("8259 PIC: Setting OCW3 = %x (wr_Slave1)\n", cw);
558 state->slave_ocw3 = cw;
560 PrintError("8259 PIC: Invalid command work (wr_Slave1)\n");
564 PrintError("8259 PIC: Invalid State writing (wr_Slave1)\n");
571 static int write_slave_port2(ushort_t port, void * src, uint_t length, struct vm_device * dev) {
572 struct pic_internal * state = (struct pic_internal*)dev->private_data;
573 uchar_t cw = *(uchar_t *)src;
575 PrintDebug("8259 PIC: Write slave port 2 with 0x%x\n",cw);
578 PrintError("8259 PIC: Invalid write length (wr_Slave2)\n");
582 if (state->slave_state == ICW2) {
583 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
585 PrintDebug("8259 PIC: Setting ICW2 = %x (wr_Slave2)\n", cw);
587 state->slave_icw2 = cw;
589 if (cw1->sngl == 0) {
590 state->slave_state = ICW3;
591 } else if (cw1->ic4 == 1) {
592 state->slave_state = ICW4;
594 state->slave_state = READY;
597 } else if (state->slave_state == ICW3) {
598 struct icw1 * cw1 = (struct icw1 *)&(state->master_icw1);
600 PrintDebug("8259 PIC: Setting ICW3 = %x (wr_Slave2)\n", cw);
602 state->slave_icw3 = cw;
605 state->slave_state = ICW4;
607 state->slave_state = READY;
610 } else if (state->slave_state == ICW4) {
611 PrintDebug("8259 PIC: Setting ICW4 = %x (wr_Slave2)\n", cw);
612 state->slave_icw4 = cw;
613 state->slave_state = READY;
614 } else if (state->slave_state == READY) {
615 PrintDebug("8259 PIC: Setting IMR = %x (wr_Slave2)\n", cw);
616 state->slave_imr = cw;
618 PrintError("8259 PIC: Invalid State at write (wr_Slave2)\n");
632 static int pic_init(struct vm_device * dev) {
633 struct pic_internal * state = (struct pic_internal*)dev->private_data;
635 v3_register_intr_controller(dev->vm, &intr_ops, state);
637 state->master_irr = 0;
638 state->master_isr = 0;
639 state->master_icw1 = 0;
640 state->master_icw2 = 0;
641 state->master_icw3 = 0;
642 state->master_icw4 = 0;
643 state->master_imr = 0;
644 state->master_ocw2 = 0;
645 state->master_ocw3 = 0x02;
646 state->master_state = ICW1;
649 state->slave_irr = 0;
650 state->slave_isr = 0;
651 state->slave_icw1 = 0;
652 state->slave_icw2 = 0;
653 state->slave_icw3 = 0;
654 state->slave_icw4 = 0;
655 state->slave_imr = 0;
656 state->slave_ocw2 = 0;
657 state->slave_ocw3 = 0x02;
658 state->slave_state = ICW1;
661 v3_dev_hook_io(dev, MASTER_PORT1, &read_master_port1, &write_master_port1);
662 v3_dev_hook_io(dev, MASTER_PORT2, &read_master_port2, &write_master_port2);
663 v3_dev_hook_io(dev, SLAVE_PORT1, &read_slave_port1, &write_slave_port1);
664 v3_dev_hook_io(dev, SLAVE_PORT2, &read_slave_port2, &write_slave_port2);
670 static int pic_deinit(struct vm_device * dev) {
671 v3_dev_unhook_io(dev, MASTER_PORT1);
672 v3_dev_unhook_io(dev, MASTER_PORT2);
673 v3_dev_unhook_io(dev, SLAVE_PORT1);
674 v3_dev_unhook_io(dev, SLAVE_PORT2);
685 static struct vm_device_ops dev_ops = {
687 .deinit = pic_deinit,
694 struct vm_device * v3_create_pic() {
695 struct pic_internal * state = NULL;
696 state = (struct pic_internal *)V3_Malloc(sizeof(struct pic_internal));
697 V3_ASSERT(state != NULL);
699 struct vm_device *device = v3_create_device("8259A", &dev_ops, state);