Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / seabios / src / hw / pic.c
1 // Helpers for working with i8259 interrupt controller.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
8 #include "biosvar.h" // SET_IVT
9 #include "config.h" // CONFIG_*
10 #include "output.h" // dprintf
11 #include "pic.h" // pic_*
12
13 u16
14 pic_irqmask_read(void)
15 {
16     return inb(PORT_PIC1_DATA) | (inb(PORT_PIC2_DATA) << 8);
17 }
18
19 void
20 pic_irqmask_write(u16 mask)
21 {
22     outb(mask, PORT_PIC1_DATA);
23     outb(mask >> 8, PORT_PIC2_DATA);
24 }
25
26 void
27 pic_irqmask_mask(u16 off, u16 on)
28 {
29     u8 pic1off = off, pic1on = on, pic2off = off>>8, pic2on = on>>8;
30     outb((inb(PORT_PIC1_DATA) & ~pic1off) | pic1on, PORT_PIC1_DATA);
31     outb((inb(PORT_PIC2_DATA) & ~pic2off) | pic2on, PORT_PIC2_DATA);
32 }
33
34 void
35 pic_reset(u8 irq0, u8 irq8)
36 {
37     // Send ICW1 (select OCW1 + will send ICW4)
38     outb(0x11, PORT_PIC1_CMD);
39     outb(0x11, PORT_PIC2_CMD);
40     // Send ICW2 (base irqs: 0x08-0x0f for irq0-7, 0x70-0x77 for irq8-15)
41     outb(irq0, PORT_PIC1_DATA);
42     outb(irq8, PORT_PIC2_DATA);
43     // Send ICW3 (cascaded pic ids)
44     outb(0x04, PORT_PIC1_DATA);
45     outb(0x02, PORT_PIC2_DATA);
46     // Send ICW4 (enable 8086 mode)
47     outb(0x01, PORT_PIC1_DATA);
48     outb(0x01, PORT_PIC2_DATA);
49     // Mask all irqs (except cascaded PIC2 irq)
50     pic_irqmask_write(PIC_IRQMASK_DEFAULT);
51 }
52
53 void
54 pic_setup(void)
55 {
56     dprintf(3, "init pic\n");
57     pic_reset(BIOS_HWIRQ0_VECTOR, BIOS_HWIRQ8_VECTOR);
58 }
59
60 void
61 enable_hwirq(int hwirq, struct segoff_s func)
62 {
63     pic_irqmask_mask(1 << hwirq, 0);
64     int vector;
65     if (hwirq < 8)
66         vector = BIOS_HWIRQ0_VECTOR + hwirq;
67     else
68         vector = BIOS_HWIRQ8_VECTOR + hwirq - 8;
69     SET_IVT(vector, func);
70 }
71
72 static u8
73 pic_isr1_read(void)
74 {
75     // 0x0b == select OCW1 + read ISR
76     outb(0x0b, PORT_PIC1_CMD);
77     return inb(PORT_PIC1_CMD);
78 }
79
80 static u8
81 pic_isr2_read(void)
82 {
83     // 0x0b == select OCW1 + read ISR
84     outb(0x0b, PORT_PIC2_CMD);
85     return inb(PORT_PIC2_CMD);
86 }
87
88 // Handler for otherwise unused hardware irqs.
89 void VISIBLE16
90 handle_hwpic1(struct bregs *regs)
91 {
92     dprintf(DEBUG_ISR_hwpic1, "handle_hwpic1 irq=%x\n", pic_isr1_read());
93     pic_eoi1();
94 }
95
96 void VISIBLE16
97 handle_hwpic2(struct bregs *regs)
98 {
99     dprintf(DEBUG_ISR_hwpic2, "handle_hwpic2 irq=%x\n", pic_isr2_read());
100     pic_eoi2();
101 }