Debian Codegraph @

i386/i386at/ioapic.c

raw
1 /*
2 * Copyright (C) 2019 Free Software Foundation, Inc.
3 *
4 * This file is part of GNU Mach.
5 *
6 * GNU Mach is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * GNU Mach is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 */
20
21 #include <sys/types.h>
22 #include <i386/ipl.h>
23 #include <machine/irq.h>
24 #include <i386/fpu.h>
25 #include <i386/hardclock.h>
26 #include <i386at/kd.h>
27 #include <i386at/idt.h>
28 #include <i386/pio.h>
29 #include <i386/pit.h>
30 #include <i386/pic.h> /* only for macros */
31 #include <i386/smp.h>
32 #include <mach/machine.h>
33 #include <kern/printf.h>
34 #include <kern/timer.h>
35 #include <kern/lock.h>
36 #include <device/irq_status.h>
37
38 static int has_irq_specific_eoi = 0;
39 int timer_pin;
40 int pic_mode = ACPI_PICMODE_APIC;
41
42 uint32_t lapic_timer_val = 0;
43 uint32_t calibrated_ticks = 0;
44
45 spl_t curr_ipl[NCPUS] = {0};
46 int spl_init = 0;
47
48 def_simple_lock_irq_data(static, ioapic_lock) /* Lock for non-atomic window accesses to ioapic */
49
50 int iunit[NINTR] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
51 16, 17, 18, 19, 20, 21, 22, 23,
52 24, 25, 26, 27, 28, 29, 30, 31,
53 /* 2nd IOAPIC */
54 32, 33, 34, 35, 36, 37, 38, 39,
55 40, 41, 42, 43, 44, 45, 46, 47,
56 48, 49, 50, 51, 52, 53, 54, 55,
57 56, 57, 58, 59, 60, 61, 62, 63 };
58
59 interrupt_handler_fn ivect[NINTR] = {
60 /* 00 */ (interrupt_handler_fn)hardclock,
61 /* 01 */ kdintr, /* kdintr, ... */
62 /* 02 */ intnull,
63 /* 03 */ intnull, /* lnpoll, comintr, ... */
64
65 /* 04 */ intnull, /* comintr, ... */
66 /* 05 */ intnull, /* comintr, wtintr, ... */
67 /* 06 */ intnull, /* fdintr, ... */
68 /* 07 */ intnull, /* qdintr, ... */
69
70 /* 08 */ intnull,
71 /* 09 */ intnull, /* ether */
72 /* 10 */ intnull,
73 /* 11 */ intnull,
74
75 /* 12 */ intnull,
76 /* 13 */ fpintr, /* always */
77 /* 14 */ intnull, /* hdintr, ... */
78 /* 15 */ intnull, /* ??? */
79
80 /* 16 */ intnull, /* PIRQA */
81 /* 17 */ intnull, /* PIRQB */
82 /* 18 */ intnull, /* PIRQC */
83 /* 19 */ intnull, /* PIRQD */
84 /* 20 */ intnull, /* PIRQE */
85 /* 21 */ intnull, /* PIRQF */
86 /* 22 */ intnull, /* PIRQG */
87 /* 23 */ intnull, /* PIRQH */
88
89 /* 24 */ intnull,
90 /* 25 */ intnull,
91 /* 26 */ intnull,
92 /* 27 */ intnull,
93 /* 28 */ intnull,
94 /* 29 */ intnull,
95 /* 30 */ intnull,
96 /* 31 */ intnull,
97
98 /* 32 */ intnull,
99 /* 33 */ intnull,
100 /* 34 */ intnull,
101 /* 35 */ intnull,
102 /* 36 */ intnull,
103 /* 37 */ intnull,
104 /* 38 */ intnull,
105 /* 39 */ intnull,
106 /* 40 */ intnull,
107 /* 41 */ intnull,
108 /* 42 */ intnull,
109 /* 43 */ intnull,
110 /* 44 */ intnull,
111 /* 45 */ intnull,
112 /* 46 */ intnull,
113 /* 47 */ intnull,
114 /* 48 */ intnull,
115 /* 49 */ intnull,
116 /* 50 */ intnull,
117 /* 51 */ intnull,
118 /* 52 */ intnull,
119 /* 53 */ intnull,
120 /* 54 */ intnull,
121 /* 55 */ intnull,
122
123 /* 56 */ intnull,
124 /* 57 */ intnull,
125 /* 58 */ intnull,
126 /* 59 */ intnull,
127 /* 60 */ intnull,
128 /* 61 */ intnull,
129 /* 62 */ intnull,
130 /* 63 */ intnull,
131 };
132
133 struct irqinfo irqinfo[NINTR];
134
135 void
136 picdisable(void)
137 {
138 int i;
139
140 asm("cli");
141 for (i = 0; i < NCPUS; i++)
142 curr_ipl[i] = SPLHI;
143
144 /*
145 ** Disable PIC
146 */
147 outb ( PIC_SLAVE_OCW, PICS_MASK );
148 outb ( PIC_MASTER_OCW, PICM_MASK );
149 }
150
151 void
152 intnull(int unit_dev)
153 {
154 printf("intnull(%d)\n", unit_dev);
155 }
156
157 static uint32_t
158 ioapic_read(uint8_t id, uint8_t reg)
159 {
160 volatile ApicIoUnit *ioapic = apic_get_ioapic(id)->ioapic;
161 ioapic->select.r = reg;
162 return ioapic->window.r;
163 }
164
165 static void
166 ioapic_write(uint8_t id, uint8_t reg, uint32_t value)
167 {
168 volatile ApicIoUnit *ioapic = apic_get_ioapic(id)->ioapic;
169 ioapic->select.r = reg;
170 ioapic->window.r = value;
171 }
172
173 static void
174 ioapic_read_entry(int apic, int pin, struct ioapic_route_entry *e)
175 {
176 union ioapic_route_entry_union entry;
177
178 entry.lo = ioapic_read(apic, APIC_IO_REDIR_LOW(pin));
179 entry.hi = ioapic_read(apic, APIC_IO_REDIR_HIGH(pin));
180
181 *e = entry.both;
182 }
183
184 /* Write the high word first because mask bit is in low word */
185 static void
186 ioapic_write_entry(int apic, int pin, struct ioapic_route_entry e)
187 {
188 union ioapic_route_entry_union entry = {{0, 0}};
189
190 entry.both = e;
191 ioapic_write(apic, APIC_IO_REDIR_HIGH(pin), entry.hi);
192 ioapic_write(apic, APIC_IO_REDIR_LOW(pin), entry.lo);
193 }
194
195 /* When toggling the interrupt via mask, write low word only */
196 static void
197 ioapic_toggle_entry(int apic, int pin, int mask)
198 {
199 union ioapic_route_entry_union entry;
200
201 spl_t s = simple_lock_irq(&ioapic_lock);
202 ioapic_read_entry(apic, pin, &entry.both);
203 entry.both.mask = mask & 0x1;
204 ioapic_write(apic, APIC_IO_REDIR_LOW(pin), entry.lo);
205 simple_unlock_irq(s, &ioapic_lock);
206 }
207
208 static int
209 ioapic_version(int apic)
210 {
211 return (ioapic_read(apic, APIC_IO_VERSION) >> APIC_IO_VERSION_SHIFT) & 0xff;
212 }
213
214 static int
215 ioapic_gsis(int apic)
216 {
217 return ((ioapic_read(apic, APIC_IO_VERSION) >> APIC_IO_ENTRIES_SHIFT) & 0xff) + 1;
218 }
219
220 static void timer_expiry_callback(void *arg)
221 {
222 volatile int *done = arg;
223 *done = 1;
224 }
225
226 static uint32_t
227 timer_measure_10x_apic_hz(void)
228 {
229 volatile int done = 0;
230 uint32_t start = 0xffffffff;
231 timeout_data_t tmp_timer;
232 tmp_timer.fcn = timer_expiry_callback;
233 tmp_timer.param = (void *)&done;
234 tmp_timer.set = 0;
235
236 printf("timer calibration...");
237
238 /* Set APIC timer */
239 lapic->init_count.r = start;
240
241 /* Delay for 10 ticks (10 * 1/hz seconds) */
242 set_timeout(&tmp_timer, 10);
243 do {
244 cpu_pause();
245 } while (!done);
246
247 /* Stop APIC timer */
248 lapic->lvt_timer.r |= LAPIC_DISABLE;
249
250 printf(" done\n");
251
252 return start - lapic->cur_count.r;
253 }
254
255 void
256 calibrate_lapic_timer(void)
257 {
258 spl_t s;
259
260 /* Set one-shot timer */
261 lapic->divider_config.r = LAPIC_TIMER_DIVIDE_2;
262 lapic->lvt_timer.r = IOAPIC_INT_BASE;
263
264 /* Measure number of APIC timer ticks in 10 mach ticks
265 * divide by 10 because we want to know how many in 1 tick */
266 if (!calibrated_ticks) {
267 s = splhigh();
268 spl0();
269 calibrated_ticks = timer_measure_10x_apic_hz() / 10;
270 splx(s);
271 }
272 }
273
274 void
275 lapic_enable_timer(void)
276 {
277 /* Set up counter */
278 lapic->init_count.r = calibrated_ticks;
279 lapic->divider_config.r = LAPIC_TIMER_DIVIDE_2;
280
281 /* Set the timer to interrupt periodically on remapped timer GSI */
282 lapic->lvt_timer.r = IOAPIC_INT_BASE | LAPIC_TIMER_PERIODIC;
283
284 /* Some buggy hardware requires this set again */
285 lapic->divider_config.r = LAPIC_TIMER_DIVIDE_2;
286
287 /* Enable interrupts for the first time */
288 printf("LAPIC timer configured on cpu%d\n", cpu_number());
289 }
290
291 void
292 ioapic_toggle(int pin, int mask)
293 {
294 int apic = 0;
295 ioapic_toggle_entry(apic, pin, mask);
296 }
297
298 void
299 ioapic_irq_eoi(int pin)
300 {
301 /* FIXME: multiple ioapics (not always zero) */
302 int apic = 0;
303 union ioapic_route_entry_union oldentry, entry;
304
305 if (pin == 0)
306 goto skip_specific_eoi;
307
308 spl_t s = simple_lock_irq(&ioapic_lock);
309
310 if (!has_irq_specific_eoi) {
311 // XXX Linux conditions on trigger mode: if (irqinfo[pin].trigger) {
312 /* Workaround for old IOAPICs with no specific EOI */
313
314 /* Mask the pin and change to edge triggered */
315 ioapic_read_entry(apic, pin, &entry.both);
316 oldentry = entry;
317 entry.both.mask = IOAPIC_MASK_DISABLED;
318 entry.both.trigger = IOAPIC_EDGE_TRIGGERED;
319 ioapic_write_entry(apic, pin, entry.both);
320
321 /* Restore level entry */
322 ioapic_write_entry(apic, pin, oldentry.both);
323 //}
324 } else {
325 volatile ApicIoUnit *ioapic = apic_get_ioapic(apic)->ioapic;
326 ioapic->eoi.r = irqinfo[pin].vector;
327 }
328
329 simple_unlock_irq(s, &ioapic_lock);
330
331 skip_specific_eoi:
332 lapic_eoi ();
333 }
334
335 static unsigned int
336 override_irq(IrqOverrideData *override, union ioapic_route_entry_union *entry)
337 {
338 if (override->flags & APIC_IRQ_OVERRIDE_TRIGGER_MASK) {
339 entry->both.trigger = (override->flags & APIC_IRQ_OVERRIDE_LEVEL_TRIGGERED) ?
340 IOAPIC_LEVEL_TRIGGERED : IOAPIC_EDGE_TRIGGERED;
341 } else {
342 if (override->bus == 0) {
343 /* ISA is edge-triggered by default */
344 entry->both.trigger = IOAPIC_EDGE_TRIGGERED;
345 } else {
346 entry->both.trigger = IOAPIC_LEVEL_TRIGGERED;
347 }
348 }
349
350 if (override->flags & APIC_IRQ_OVERRIDE_POLARITY_MASK) {
351 entry->both.polarity = (override->flags & APIC_IRQ_OVERRIDE_ACTIVE_LOW) ?
352 IOAPIC_ACTIVE_LOW : IOAPIC_ACTIVE_HIGH;
353 } else {
354 if (override->bus == 0) {
355 /* EISA is active-low for level-triggered interrupts */
356 if (entry->both.trigger == IOAPIC_LEVEL_TRIGGERED) {
357 entry->both.polarity = IOAPIC_ACTIVE_LOW;
358 } else {
359 entry->both.polarity = IOAPIC_ACTIVE_HIGH;
360 }
361 }
362 }
363 printf("IRQ override: pin=%d gsi=%d trigger=%s polarity=%s\n",
364 override->irq, override->gsi,
365 entry->both.trigger == IOAPIC_LEVEL_TRIGGERED ? "LEVEL" : "EDGE",
366 entry->both.polarity == IOAPIC_ACTIVE_LOW ? "LOW" : "HIGH");
367
368 return override->gsi;
369 }
370
371 void
372 ioapic_configure(void)
373 {
374 /* Assume first IO APIC maps to GSI base 0 */
375 int gsi, apic = 0, pin;
376 IrqOverrideData *irq_over;
377 int timer_gsi;
378 int version = ioapic_version(apic);
379 int ngsis = ioapic_gsis(apic);
380 int ngsis2 = 0;
381
382 if (version >= 0x20) {
383 has_irq_specific_eoi = 1;
384 }
385
386 printf("IOAPIC version 0x%x\n", version);
387
388 /* Disable IOAPIC interrupts and set spurious interrupt */
389 lapic->spurious_vector.r = IOAPIC_SPURIOUS_BASE;
390
391 union ioapic_route_entry_union entry = {{0, 0}};
392
393 entry.both.delvmode = IOAPIC_FIXED;
394 entry.both.destmode = IOAPIC_PHYSICAL;
395 entry.both.mask = IOAPIC_MASK_DISABLED;
396 /* This does not use apic_id_mask because
397 * the IOAPIC seems to use what is actually set
398 * in lapic's apic_id register.
399 */
400 entry.both.dest = lapic->apic_id.r >> 24;
401
402 /* ISA legacy IRQs */
403 for (pin = 0; pin < 14; pin++) {
404 gsi = pin;
405
406 /* Legacy interrupts are on rising edge */
407 entry.both.trigger = IOAPIC_EDGE_TRIGGERED;
408 entry.both.polarity = IOAPIC_ACTIVE_HIGH;
409
410 if ((irq_over = acpi_get_irq_override(pin))) {
411 gsi = override_irq(irq_over, &entry);
412 }
413 entry.both.vector = IOAPIC_INT_BASE + gsi;
414 ioapic_write_entry(apic, pin, entry.both);
415
416 irqinfo[pin].vector = entry.both.vector;
417 irqinfo[pin].trigger = entry.both.trigger;
418
419 /* Set initial state to masked */
420 mask_irq(pin);
421
422 /* Timer workaround for x86 */
423 if (pin == 0) {
424 /* Save timer info */
425 timer_gsi = gsi;
426 } else {
427 /* Remap timer irq */
428 if (gsi == timer_gsi) {
429 timer_pin = pin;
430 /* Remap GSI base to timer pin so ivect[0] is the timer */
431 entry.both.vector = IOAPIC_INT_BASE;
432 ioapic_write_entry(apic, timer_pin, entry.both);
433 /* Mask the duplicate pin 0 as we will be using timer_pin */
434 mask_irq(0);
435 }
436 }
437 }
438
439 /* 14,15 and PCI IRQs PIRQ A-H */
440 for (pin = 14; pin < ngsis; pin++) {
441 gsi = pin;
442
443 /* ACPI interrupts default to level-triggered active-low */
444 entry.both.trigger = IOAPIC_LEVEL_TRIGGERED;
445 entry.both.polarity = IOAPIC_ACTIVE_LOW;
446
447 if ((irq_over = acpi_get_irq_override(pin))) {
448 gsi = override_irq(irq_over, &entry);
449 }
450 entry.both.vector = IOAPIC_INT_BASE + gsi;
451 ioapic_write_entry(apic, pin, entry.both);
452
453 irqinfo[pin].vector = entry.both.vector;
454 irqinfo[pin].trigger = entry.both.trigger;
455
456 /* Set initial state to masked */
457 mask_irq(pin);
458 }
459
460 printf("IOAPIC 0 configured with GSI 0-%d\n", ngsis - 1);
461
462 /* Second IOAPIC */
463 if (apic_get_num_ioapics() > 1) {
464 apic = 1;
465 ngsis2 = ioapic_gsis(apic);
466
467 for (pin = 0; pin < ngsis2; pin++) {
468 gsi = pin + ngsis;
469
470 /* Defaults */
471 entry.both.trigger = IOAPIC_LEVEL_TRIGGERED;
472 entry.both.polarity = IOAPIC_ACTIVE_LOW;
473
474 if ((irq_over = acpi_get_irq_override(pin + ngsis))) {
475 gsi = override_irq(irq_over, &entry);
476 }
477 entry.both.vector = IOAPIC_INT_BASE + gsi;
478 ioapic_write_entry(apic, pin, entry.both);
479
480 irqinfo[pin + ngsis].vector = entry.both.vector;
481 irqinfo[pin + ngsis].trigger = entry.both.trigger;
482
483 /* Set initial state to masked */
484 mask_irq(pin + ngsis);
485 }
486
487 printf("IOAPIC 1 configured with GSI %d-%d\n", ngsis, ngsis + ngsis2 - 1);
488 }
489
490 /* Start the IO APIC receiving interrupts */
491 lapic_setup();
492 lapic_enable();
493 }