These changes are the raw update to qemu-2.6.
[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     if (!CONFIG_HARDWARE_IRQ)
17         return 0;
18     return inb(PORT_PIC1_DATA) | (inb(PORT_PIC2_DATA) << 8);
19 }
20
21 void
22 pic_irqmask_write(u16 mask)
23 {
24     if (!CONFIG_HARDWARE_IRQ)
25         return;
26     outb(mask, PORT_PIC1_DATA);
27     outb(mask >> 8, PORT_PIC2_DATA);
28 }
29
30 void
31 pic_irqmask_mask(u16 off, u16 on)
32 {
33     if (!CONFIG_HARDWARE_IRQ)
34         return;
35     u8 pic1off = off, pic1on = on, pic2off = off>>8, pic2on = on>>8;
36     outb((inb(PORT_PIC1_DATA) & ~pic1off) | pic1on, PORT_PIC1_DATA);
37     outb((inb(PORT_PIC2_DATA) & ~pic2off) | pic2on, PORT_PIC2_DATA);
38 }
39
40 void
41 pic_reset(u8 irq0, u8 irq8)
42 {
43     if (!CONFIG_HARDWARE_IRQ)
44         return;
45     // Send ICW1 (select OCW1 + will send ICW4)
46     outb(0x11, PORT_PIC1_CMD);
47     outb(0x11, PORT_PIC2_CMD);
48     // Send ICW2 (base irqs: 0x08-0x0f for irq0-7, 0x70-0x77 for irq8-15)
49     outb(irq0, PORT_PIC1_DATA);
50     outb(irq8, PORT_PIC2_DATA);
51     // Send ICW3 (cascaded pic ids)
52     outb(0x04, PORT_PIC1_DATA);
53     outb(0x02, PORT_PIC2_DATA);
54     // Send ICW4 (enable 8086 mode)
55     outb(0x01, PORT_PIC1_DATA);
56     outb(0x01, PORT_PIC2_DATA);
57     // Mask all irqs (except cascaded PIC2 irq)
58     pic_irqmask_write(PIC_IRQMASK_DEFAULT);
59 }
60
61 void
62 pic_setup(void)
63 {
64     dprintf(3, "init pic\n");
65     pic_reset(BIOS_HWIRQ0_VECTOR, BIOS_HWIRQ8_VECTOR);
66 }
67
68 void
69 enable_hwirq(int hwirq, struct segoff_s func)
70 {
71     if (!CONFIG_HARDWARE_IRQ)
72         return;
73     pic_irqmask_mask(1 << hwirq, 0);
74     int vector;
75     if (hwirq < 8)
76         vector = BIOS_HWIRQ0_VECTOR + hwirq;
77     else
78         vector = BIOS_HWIRQ8_VECTOR + hwirq - 8;
79     SET_IVT(vector, func);
80 }
81
82 static u8
83 pic_isr1_read(void)
84 {
85     if (!CONFIG_HARDWARE_IRQ)
86         return 0;
87     // 0x0b == select OCW1 + read ISR
88     outb(0x0b, PORT_PIC1_CMD);
89     return inb(PORT_PIC1_CMD);
90 }
91
92 static u8
93 pic_isr2_read(void)
94 {
95     if (!CONFIG_HARDWARE_IRQ)
96         return 0;
97     // 0x0b == select OCW1 + read ISR
98     outb(0x0b, PORT_PIC2_CMD);
99     return inb(PORT_PIC2_CMD);
100 }
101
102 // Handler for otherwise unused hardware irqs.
103 void VISIBLE16
104 handle_hwpic1(struct bregs *regs)
105 {
106     dprintf(DEBUG_ISR_hwpic1, "handle_hwpic1 irq=%x\n", pic_isr1_read());
107     pic_eoi1();
108 }
109
110 void VISIBLE16
111 handle_hwpic2(struct bregs *regs)
112 {
113     dprintf(DEBUG_ISR_hwpic2, "handle_hwpic2 irq=%x\n", pic_isr2_read());
114     pic_eoi2();
115 }