blob: c507c19a347515ff8bfb13dca749a5101d34ea8d [file] [log] [blame]
Corey Tabaka84697242009-03-26 02:32:01 -04001/*
2 * Copyright (c) 2009 Corey Tabaka
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23#include <sys/types.h>
24#include <debug.h>
25#include <err.h>
26#include <reg.h>
27#include <kernel/thread.h>
28#include <platform/interrupts.h>
29#include <arch/ops.h>
30#include <arch/x86.h>
31#include "platform_p.h"
Corey Tabakab3f6cac2009-04-01 17:35:12 -040032#include <platform/pc.h>
Corey Tabaka84697242009-03-26 02:32:01 -040033
34void x86_gpf_handler(struct x86_iframe *frame);
35void x86_invop_handler(struct x86_iframe *frame);
36void x86_unhandled_exception(struct x86_iframe *frame);
37
38#define PIC1 0x20
39#define PIC2 0xA0
40
41#define ICW1 0x11
42#define ICW4 0x01
43
44struct int_handler_struct {
45 int_handler handler;
46 void *arg;
47};
48
49static struct int_handler_struct int_handler_table[INT_VECTORS];
50
51/*
52 * Cached IRQ mask (enabled/disabled)
53 */
54static uint8_t irqMask[2];
55
56/*
57 * init the PICs and remap them
58 */
59static void map(uint32_t pic1, uint32_t pic2)
60{
61 /* send ICW1 */
62 outp(PIC1, ICW1);
63 outp(PIC2, ICW1);
64
65 /* send ICW2 */
66 outp(PIC1 + 1, pic1); /* remap */
67 outp(PIC2 + 1, pic2); /* pics */
68
69 /* send ICW3 */
70 outp(PIC1 + 1, 4); /* IRQ2 -> connection to slave */
71 outp(PIC2 + 1, 2);
72
73 /* send ICW4 */
74 outp(PIC1 + 1, 5);
75 outp(PIC2 + 1, 1);
76
77 /* disable all IRQs */
78 outp(PIC1 + 1, 0xff);
79 outp(PIC2 + 1, 0xff);
80
81 irqMask[0] = 0xff;
82 irqMask[1] = 0xff;
83}
84
85static void enable(unsigned int vector, bool enable)
86{
87 if (vector >= PIC1_BASE && vector < PIC1_BASE + 8) {
88 vector -= PIC1_BASE;
89
90 uint8_t bit = 1 << vector;
91
92 if (enable && (irqMask[0] & bit)) {
93 irqMask[0] = inp(PIC1 + 1);
94 irqMask[0] &= ~bit;
95 outp(PIC1 + 1, irqMask[0]);
96 irqMask[0] = inp(PIC1 + 1);
97 } else if (!enable && !(irqMask[0] & bit)) {
98 irqMask[0] = inp(PIC1 + 1);
99 irqMask[0] |= bit;
100 outp(PIC1 + 1, irqMask[0]);
101 irqMask[0] = inp(PIC1 + 1);
102 }
103 } else if (vector >= PIC2_BASE && vector < PIC2_BASE + 8) {
104 vector -= PIC2_BASE;
105
106 uint8_t bit = 1 << vector;
107
108 if (enable && (irqMask[1] & bit)) {
109 irqMask[1] = inp(PIC2 + 1);
110 irqMask[1] &= ~bit;
111 outp(PIC2 + 1, irqMask[1]);
112 irqMask[1] = inp(PIC2 + 1);
113 } else if (!enable && !(irqMask[1] & bit)) {
114 irqMask[1] = inp(PIC2 + 1);
115 irqMask[1] |= bit;
116 outp(PIC2 + 1, irqMask[1]);
117 irqMask[1] = inp(PIC2 + 1);
118 }
119
120 bit = 1 << (INT_PIC2 - PIC1_BASE);
121
122 if (irqMask[1] != 0xff && (irqMask[0] & bit)) {
123 irqMask[0] = inp(PIC1 + 1);
124 irqMask[0] &= ~bit;
125 outp(PIC1 + 1, irqMask[0]);
126 irqMask[0] = inp(PIC1 + 1);
127 } else if (irqMask[1] == 0 && !(irqMask[0] & bit)) {
128 irqMask[0] = inp(PIC1 + 1);
129 irqMask[0] |= bit;
130 outp(PIC1 + 1, irqMask[0]);
131 irqMask[0] = inp(PIC1 + 1);
132 }
133 } else {
134 //dprintf(DEBUG, "Invalid PIC interrupt: %02x\n", vector);
135 }
136}
137
138void issueEOI(unsigned int vector)
139{
140 if (vector >= PIC1_BASE && vector <= PIC1_BASE + 7) {
141 outp(PIC1, 0x20);
142 } else if (vector >= PIC2_BASE && vector <= PIC2_BASE + 7) {
143 outp(PIC2, 0x20);
144 outp(PIC1, 0x20); // must issue both for the second PIC
145 }
146}
147
148void platform_init_interrupts(void)
149{
150 // rebase the PIC out of the way of processor exceptions
151 map(PIC1_BASE, PIC2_BASE);
152}
153
154status_t mask_interrupt(unsigned int vector)
155{
156 if (vector >= INT_VECTORS)
157 return ERR_INVALID_ARGS;
158
159// dprintf(DEBUG, "%s: vector %d\n", __PRETTY_FUNCTION__, vector);
160
161 enter_critical_section();
162
163 enable(vector, false);
164
165 exit_critical_section();
166
167 return NO_ERROR;
168}
169
170
171void platform_mask_irqs(void)
172{
173 irqMask[0] = inp(PIC1 + 1);
174 irqMask[1] = inp(PIC2 + 1);
175
176 outp(PIC1 + 1, 0xff);
177 outp(PIC2 + 1, 0xff);
178
179 irqMask[0] = inp(PIC1 + 1);
180 irqMask[1] = inp(PIC2 + 1);
181}
182
183status_t unmask_interrupt(unsigned int vector)
184{
185 if (vector >= INT_VECTORS)
186 return ERR_INVALID_ARGS;
187
188// dprintf("%s: vector %d\n", __PRETTY_FUNCTION__, vector);
189
190 enter_critical_section();
191
192 enable(vector, true);
193
194 exit_critical_section();
195
196 return NO_ERROR;
197}
198
199enum handler_return platform_irq(struct x86_iframe *frame)
200{
201 // get the current vector
202 unsigned int vector = frame->vector;
203
204#if THREAD_STATS
205 thread_stats.interrupts++;
206#endif
207
208 // deliver the interrupt
209 enum handler_return ret = INT_NO_RESCHEDULE;
210
211 switch (vector) {
212 case INT_GP_FAULT:
213 x86_gpf_handler(frame);
214 break;
215
216 case INT_INVALID_OP:
217 x86_invop_handler(frame);
218 break;
219
220 case INT_DIVIDE_0:
221 case INT_DEBUG_EX:
222 case INT_DEV_NA_EX:
223 case INT_PAGE_FAULT:
224 case INT_STACK_FAULT:
225 case 3:
226 x86_unhandled_exception(frame);
227 break;
228
229 default:
230 if (int_handler_table[vector].handler)
231 ret = int_handler_table[vector].handler(int_handler_table[vector].arg);
232 }
233
234 // ack the interrupt
235 issueEOI(vector);
236
237 return ret;
238}
239
240void register_int_handler(unsigned int vector, int_handler handler, void *arg)
241{
242 if (vector >= INT_VECTORS)
243 panic("register_int_handler: vector out of range %d\n", vector);
244
245 enter_critical_section();
246
247 int_handler_table[vector].arg = arg;
248 int_handler_table[vector].handler = handler;
249
250 exit_critical_section();
251}
252
253