1 /*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
5 *
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
8 *
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
14 *
15 * Fixes
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
18 * and Rolf G. Tews
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
21 */
22
23 #include <xen/lib.h>
24 #include <xen/init.h>
25 #include <xen/irq.h>
26 #include <xen/delay.h>
27 #include <xen/param.h>
28 #include <xen/sched.h>
29 #include <xen/acpi.h>
30 #include <xen/keyhandler.h>
31 #include <xen/softirq.h>
32 #include <asm/mc146818rtc.h>
33 #include <asm/smp.h>
34 #include <asm/desc.h>
35 #include <asm/msi.h>
36 #include <asm/setup.h>
37 #include <mach_apic.h>
38 #include <io_ports.h>
39 #include <irq_vectors.h>
40 #include <public/physdev.h>
41 #include <xen/trace.h>
42
43 /* Where if anywhere is the i8259 connect in external int mode */
44 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
45
46 static DEFINE_SPINLOCK(ioapic_lock);
47
48 bool __read_mostly skip_ioapic_setup;
49 bool __initdata ioapic_ack_new = true;
50 bool __initdata ioapic_ack_forced;
51
52 /*
53 * # of IRQ routing registers
54 */
55 int __read_mostly nr_ioapic_entries[MAX_IO_APICS];
56 int __read_mostly nr_ioapics;
57
58 /*
59 * Rough estimation of how many shared IRQs there are, can
60 * be changed anytime.
61 */
62 #define MAX_PLUS_SHARED_IRQS nr_irqs_gsi
63 #define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + nr_irqs_gsi)
64
65
66 #define ioapic_has_eoi_reg(apic) (mp_ioapics[(apic)].mpc_apicver >= 0x20)
67
68 static int apic_pin_2_gsi_irq(int apic, int pin);
69
70 static vmask_t *__read_mostly vector_map[MAX_IO_APICS];
71
share_vector_maps(unsigned int src,unsigned int dst)72 static void share_vector_maps(unsigned int src, unsigned int dst)
73 {
74 unsigned int pin;
75
76 if (vector_map[src] == vector_map[dst])
77 return;
78
79 bitmap_or(vector_map[src]->_bits, vector_map[src]->_bits,
80 vector_map[dst]->_bits, X86_NR_VECTORS);
81
82 for (pin = 0; pin < nr_ioapic_entries[dst]; ++pin) {
83 int irq = apic_pin_2_gsi_irq(dst, pin);
84 struct irq_desc *desc;
85
86 if (irq < 0)
87 continue;
88 desc = irq_to_desc(irq);
89 if (desc->arch.used_vectors == vector_map[dst])
90 desc->arch.used_vectors = vector_map[src];
91 }
92
93 vector_map[dst] = vector_map[src];
94 }
95
96 /*
97 * This is performance-critical, we want to do it O(1)
98 *
99 * the indexing order of this array favors 1:1 mappings
100 * between pins and IRQs.
101 */
102
103 static struct irq_pin_list {
104 int apic, pin;
105 unsigned int next;
106 } *__read_mostly irq_2_pin;
107
108 static unsigned int irq_2_pin_free_entry;
109
110 /*
111 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
112 * shared ISA-space IRQs, so we have to support them. We are super
113 * fast in the common case, and fast for shared ISA-space IRQs.
114 */
add_pin_to_irq(unsigned int irq,int apic,int pin)115 static void add_pin_to_irq(unsigned int irq, int apic, int pin)
116 {
117 struct irq_pin_list *entry = irq_2_pin + irq;
118
119 while (entry->next) {
120 BUG_ON((entry->apic == apic) && (entry->pin == pin));
121 entry = irq_2_pin + entry->next;
122 }
123
124 BUG_ON((entry->apic == apic) && (entry->pin == pin));
125
126 if (entry->pin != -1) {
127 if (irq_2_pin_free_entry >= PIN_MAP_SIZE)
128 panic("io_apic.c: whoops\n");
129 entry->next = irq_2_pin_free_entry;
130 entry = irq_2_pin + entry->next;
131 irq_2_pin_free_entry = entry->next;
132 entry->next = 0;
133 }
134 entry->apic = apic;
135 entry->pin = pin;
136 share_vector_maps(irq_2_pin[irq].apic, apic);
137 }
138
remove_pin_from_irq(unsigned int irq,int apic,int pin)139 static void remove_pin_from_irq(unsigned int irq, int apic, int pin)
140 {
141 struct irq_pin_list *entry, *prev;
142
143 for (entry = &irq_2_pin[irq]; ; entry = &irq_2_pin[entry->next]) {
144 if ((entry->apic == apic) && (entry->pin == pin))
145 break;
146 BUG_ON(!entry->next);
147 }
148
149 entry->pin = entry->apic = -1;
150
151 if (entry != &irq_2_pin[irq]) {
152 /* Removed entry is not at head of list. */
153 prev = &irq_2_pin[irq];
154 while (&irq_2_pin[prev->next] != entry)
155 prev = &irq_2_pin[prev->next];
156 prev->next = entry->next;
157 } else if (entry->next) {
158 /* Removed entry is at head of multi-item list. */
159 prev = entry;
160 entry = &irq_2_pin[entry->next];
161 *prev = *entry;
162 entry->pin = entry->apic = -1;
163 } else
164 return;
165
166 entry->next = irq_2_pin_free_entry;
167 irq_2_pin_free_entry = entry - irq_2_pin;
168 }
169
170 /*
171 * Reroute an IRQ to a different pin.
172 */
replace_pin_at_irq(unsigned int irq,int oldapic,int oldpin,int newapic,int newpin)173 static void __init replace_pin_at_irq(unsigned int irq,
174 int oldapic, int oldpin,
175 int newapic, int newpin)
176 {
177 struct irq_pin_list *entry = irq_2_pin + irq;
178
179 while (1) {
180 if (entry->apic == oldapic && entry->pin == oldpin) {
181 entry->apic = newapic;
182 entry->pin = newpin;
183 share_vector_maps(oldapic, newapic);
184 }
185 if (!entry->next)
186 break;
187 entry = irq_2_pin + entry->next;
188 }
189 }
190
io_apic_get_used_vector_map(unsigned int irq)191 vmask_t *io_apic_get_used_vector_map(unsigned int irq)
192 {
193 struct irq_pin_list *entry = irq_2_pin + irq;
194
195 if (entry->pin == -1)
196 return NULL;
197
198 return vector_map[entry->apic];
199 }
200
alloc_ioapic_entries(void)201 struct IO_APIC_route_entry **alloc_ioapic_entries(void)
202 {
203 int apic;
204 struct IO_APIC_route_entry **ioapic_entries;
205
206 ioapic_entries = xmalloc_array(struct IO_APIC_route_entry *, nr_ioapics);
207 if (!ioapic_entries)
208 return 0;
209
210 for (apic = 0; apic < nr_ioapics; apic++) {
211 ioapic_entries[apic] =
212 xmalloc_array(struct IO_APIC_route_entry,
213 nr_ioapic_entries[apic]);
214 if (!ioapic_entries[apic] && nr_ioapic_entries[apic])
215 goto nomem;
216 }
217
218 return ioapic_entries;
219
220 nomem:
221 while (--apic >= 0)
222 xfree(ioapic_entries[apic]);
223 xfree(ioapic_entries);
224
225 return 0;
226 }
227
228 union entry_union {
229 struct { u32 w1, w2; };
230 struct IO_APIC_route_entry entry;
231 };
232
__ioapic_read_entry(unsigned int apic,unsigned int pin,bool raw)233 struct IO_APIC_route_entry __ioapic_read_entry(
234 unsigned int apic, unsigned int pin, bool raw)
235 {
236 unsigned int (*read)(unsigned int, unsigned int)
237 = raw ? __io_apic_read : io_apic_read;
238 union entry_union eu;
239 eu.w1 = (*read)(apic, 0x10 + 2 * pin);
240 eu.w2 = (*read)(apic, 0x11 + 2 * pin);
241 return eu.entry;
242 }
243
ioapic_read_entry(unsigned int apic,unsigned int pin,bool raw)244 static struct IO_APIC_route_entry ioapic_read_entry(
245 unsigned int apic, unsigned int pin, bool raw)
246 {
247 struct IO_APIC_route_entry entry;
248 unsigned long flags;
249
250 spin_lock_irqsave(&ioapic_lock, flags);
251 entry = __ioapic_read_entry(apic, pin, raw);
252 spin_unlock_irqrestore(&ioapic_lock, flags);
253 return entry;
254 }
255
__ioapic_write_entry(unsigned int apic,unsigned int pin,bool raw,struct IO_APIC_route_entry e)256 void __ioapic_write_entry(
257 unsigned int apic, unsigned int pin, bool raw,
258 struct IO_APIC_route_entry e)
259 {
260 void (*write)(unsigned int, unsigned int, unsigned int)
261 = raw ? __io_apic_write : io_apic_write;
262 union entry_union eu = { .entry = e };
263
264 (*write)(apic, 0x11 + 2*pin, eu.w2);
265 (*write)(apic, 0x10 + 2*pin, eu.w1);
266 }
267
ioapic_write_entry(unsigned int apic,unsigned int pin,bool raw,struct IO_APIC_route_entry e)268 static void ioapic_write_entry(
269 unsigned int apic, unsigned int pin, bool raw,
270 struct IO_APIC_route_entry e)
271 {
272 unsigned long flags;
273 spin_lock_irqsave(&ioapic_lock, flags);
274 __ioapic_write_entry(apic, pin, raw, e);
275 spin_unlock_irqrestore(&ioapic_lock, flags);
276 }
277
278 /* EOI an IO-APIC entry. Vector may be -1, indicating that it should be
279 * worked out using the pin. This function expects that the ioapic_lock is
280 * being held, and interrupts are disabled (or there is a good reason not
281 * to), and that if both pin and vector are passed, that they refer to the
282 * same redirection entry in the IO-APIC. */
__io_apic_eoi(unsigned int apic,unsigned int vector,unsigned int pin)283 static void __io_apic_eoi(unsigned int apic, unsigned int vector, unsigned int pin)
284 {
285 /* Prefer the use of the EOI register if available */
286 if ( ioapic_has_eoi_reg(apic) )
287 {
288 /* If vector is unknown, read it from the IO-APIC */
289 if ( vector == IRQ_VECTOR_UNASSIGNED )
290 vector = __ioapic_read_entry(apic, pin, TRUE).vector;
291
292 *(IO_APIC_BASE(apic)+16) = vector;
293 }
294 else
295 {
296 /* Else fake an EOI by switching to edge triggered mode
297 * and back */
298 struct IO_APIC_route_entry entry;
299 bool need_to_unmask = false;
300
301 entry = __ioapic_read_entry(apic, pin, TRUE);
302
303 if ( ! entry.mask )
304 {
305 /* If entry is not currently masked, mask it and make
306 * a note to unmask it later */
307 entry.mask = 1;
308 __ioapic_write_entry(apic, pin, TRUE, entry);
309 need_to_unmask = true;
310 }
311
312 /* Flip the trigger mode to edge and back */
313 entry.trigger = 0;
314 __ioapic_write_entry(apic, pin, TRUE, entry);
315 entry.trigger = 1;
316 __ioapic_write_entry(apic, pin, TRUE, entry);
317
318 if ( need_to_unmask )
319 {
320 /* Unmask if neccesary */
321 entry.mask = 0;
322 __ioapic_write_entry(apic, pin, TRUE, entry);
323 }
324 }
325 }
326
327 /*
328 * Saves all the IO-APIC RTE's
329 */
save_IO_APIC_setup(struct IO_APIC_route_entry ** ioapic_entries)330 int save_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
331 {
332 int apic, pin;
333
334 if (!ioapic_entries)
335 return -ENOMEM;
336
337 for (apic = 0; apic < nr_ioapics; apic++) {
338 if (!nr_ioapic_entries[apic])
339 continue;
340
341 if (!ioapic_entries[apic])
342 return -ENOMEM;
343
344 for (pin = 0; pin < nr_ioapic_entries[apic]; pin++)
345 ioapic_entries[apic][pin] = __ioapic_read_entry(apic, pin, 1);
346 }
347
348 return 0;
349 }
350
351 /*
352 * Mask all IO APIC entries.
353 */
mask_IO_APIC_setup(struct IO_APIC_route_entry ** ioapic_entries)354 void mask_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries)
355 {
356 int apic, pin;
357
358 if (!ioapic_entries)
359 return;
360
361 for (apic = 0; apic < nr_ioapics; apic++) {
362 if (!nr_ioapic_entries[apic])
363 continue;
364
365 if (!ioapic_entries[apic])
366 break;
367
368 for (pin = 0; pin < nr_ioapic_entries[apic]; pin++) {
369 struct IO_APIC_route_entry entry;
370
371 entry = ioapic_entries[apic][pin];
372 if (!entry.mask) {
373 entry.mask = 1;
374
375 ioapic_write_entry(apic, pin, 1, entry);
376 }
377 }
378 }
379 }
380
381 /*
382 * Restore IO APIC entries which was saved in ioapic_entries.
383 */
restore_IO_APIC_setup(struct IO_APIC_route_entry ** ioapic_entries,bool raw)384 int restore_IO_APIC_setup(struct IO_APIC_route_entry **ioapic_entries,
385 bool raw)
386 {
387 int apic, pin;
388
389 if (!ioapic_entries)
390 return -ENOMEM;
391
392 for (apic = 0; apic < nr_ioapics; apic++) {
393 if (!nr_ioapic_entries[apic])
394 continue;
395
396 if (!ioapic_entries[apic])
397 return -ENOMEM;
398
399 for (pin = 0; pin < nr_ioapic_entries[apic]; pin++)
400 ioapic_write_entry(apic, pin, raw, ioapic_entries[apic][pin]);
401 }
402
403 return 0;
404 }
405
free_ioapic_entries(struct IO_APIC_route_entry ** ioapic_entries)406 void free_ioapic_entries(struct IO_APIC_route_entry **ioapic_entries)
407 {
408 int apic;
409
410 for (apic = 0; apic < nr_ioapics; apic++)
411 xfree(ioapic_entries[apic]);
412
413 xfree(ioapic_entries);
414 }
415
modify_IO_APIC_irq(unsigned int irq,unsigned int enable,unsigned int disable)416 static void modify_IO_APIC_irq(unsigned int irq, unsigned int enable,
417 unsigned int disable)
418 {
419 struct irq_pin_list *entry = irq_2_pin + irq;
420 unsigned int pin, reg;
421
422 for (;;) {
423 pin = entry->pin;
424 if (pin == -1)
425 break;
426 reg = io_apic_read(entry->apic, 0x10 + pin*2);
427 reg &= ~disable;
428 reg |= enable;
429 io_apic_modify(entry->apic, 0x10 + pin*2, reg);
430 if (!entry->next)
431 break;
432 entry = irq_2_pin + entry->next;
433 }
434 }
435
436 /* mask = 1 */
__mask_IO_APIC_irq(unsigned int irq)437 static void __mask_IO_APIC_irq (unsigned int irq)
438 {
439 modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED, 0);
440 }
441
442 /* mask = 0 */
__unmask_IO_APIC_irq(unsigned int irq)443 static void __unmask_IO_APIC_irq (unsigned int irq)
444 {
445 modify_IO_APIC_irq(irq, 0, IO_APIC_REDIR_MASKED);
446 }
447
448 /* trigger = 0 */
__edge_IO_APIC_irq(unsigned int irq)449 static void __edge_IO_APIC_irq (unsigned int irq)
450 {
451 modify_IO_APIC_irq(irq, 0, IO_APIC_REDIR_LEVEL_TRIGGER);
452 }
453
454 /* trigger = 1 */
__level_IO_APIC_irq(unsigned int irq)455 static void __level_IO_APIC_irq (unsigned int irq)
456 {
457 modify_IO_APIC_irq(irq, IO_APIC_REDIR_LEVEL_TRIGGER, 0);
458 }
459
mask_IO_APIC_irq(struct irq_desc * desc)460 static void mask_IO_APIC_irq(struct irq_desc *desc)
461 {
462 unsigned long flags;
463
464 spin_lock_irqsave(&ioapic_lock, flags);
465 __mask_IO_APIC_irq(desc->irq);
466 spin_unlock_irqrestore(&ioapic_lock, flags);
467 }
468
unmask_IO_APIC_irq(struct irq_desc * desc)469 static void unmask_IO_APIC_irq(struct irq_desc *desc)
470 {
471 unsigned long flags;
472
473 spin_lock_irqsave(&ioapic_lock, flags);
474 __unmask_IO_APIC_irq(desc->irq);
475 spin_unlock_irqrestore(&ioapic_lock, flags);
476 }
477
__eoi_IO_APIC_irq(struct irq_desc * desc)478 static void __eoi_IO_APIC_irq(struct irq_desc *desc)
479 {
480 struct irq_pin_list *entry = irq_2_pin + desc->irq;
481 unsigned int pin, vector = desc->arch.vector;
482
483 for (;;) {
484 pin = entry->pin;
485 if (pin == -1)
486 break;
487 __io_apic_eoi(entry->apic, vector, pin);
488 if (!entry->next)
489 break;
490 entry = irq_2_pin + entry->next;
491 }
492 }
493
eoi_IO_APIC_irq(struct irq_desc * desc)494 static void eoi_IO_APIC_irq(struct irq_desc *desc)
495 {
496 unsigned long flags;
497 spin_lock_irqsave(&ioapic_lock, flags);
498 __eoi_IO_APIC_irq(desc);
499 spin_unlock_irqrestore(&ioapic_lock, flags);
500 }
501
clear_IO_APIC_pin(unsigned int apic,unsigned int pin)502 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
503 {
504 struct IO_APIC_route_entry entry;
505
506 /* Check delivery_mode to be sure we're not clearing an SMI pin */
507 entry = __ioapic_read_entry(apic, pin, false);
508 if (entry.delivery_mode == dest_SMI)
509 return;
510
511 /*
512 * Make sure the entry is masked and re-read the contents to check
513 * if it is a level triggered pin and if the remoteIRR is set.
514 */
515 if (!entry.mask) {
516 entry.mask = 1;
517 __ioapic_write_entry(apic, pin, false, entry);
518 }
519 entry = __ioapic_read_entry(apic, pin, true);
520
521 if (entry.irr) {
522 /* Make sure the trigger mode is set to level. */
523 if (!entry.trigger) {
524 entry = __ioapic_read_entry(apic, pin, false);
525 entry.trigger = 1;
526 __ioapic_write_entry(apic, pin, false, entry);
527 }
528 __io_apic_eoi(apic, entry.vector, pin);
529 }
530
531 /*
532 * Disable it in the IO-APIC irq-routing table:
533 */
534 memset(&entry, 0, sizeof(entry));
535 entry.mask = 1;
536 __ioapic_write_entry(apic, pin, false, entry);
537
538 entry = __ioapic_read_entry(apic, pin, true);
539 if (entry.irr)
540 printk(KERN_ERR "IO-APIC%02x-%u: Unable to reset IRR\n",
541 IO_APIC_ID(apic), pin);
542 }
543
clear_IO_APIC(void)544 static void clear_IO_APIC (void)
545 {
546 int apic, pin;
547
548 for (apic = 0; apic < nr_ioapics; apic++) {
549 for (pin = 0; pin < nr_ioapic_entries[apic]; pin++)
550 clear_IO_APIC_pin(apic, pin);
551 }
552 }
553
554 static void
set_ioapic_affinity_irq(struct irq_desc * desc,const cpumask_t * mask)555 set_ioapic_affinity_irq(struct irq_desc *desc, const cpumask_t *mask)
556 {
557 unsigned int dest;
558 int pin, irq;
559 struct irq_pin_list *entry;
560
561 irq = desc->irq;
562
563 spin_lock(&ioapic_lock);
564
565 dest = set_desc_affinity(desc, mask);
566 if (dest != BAD_APICID) {
567 if ( !iommu_intremap || !x2apic_enabled )
568 dest = SET_APIC_LOGICAL_ID(dest);
569 entry = irq_2_pin + irq;
570 for (;;) {
571 unsigned int data;
572 pin = entry->pin;
573 if (pin == -1)
574 break;
575
576 io_apic_write(entry->apic, 0x10 + 1 + pin*2, dest);
577 data = io_apic_read(entry->apic, 0x10 + pin*2);
578 data &= ~IO_APIC_REDIR_VECTOR_MASK;
579 data |= MASK_INSR(desc->arch.vector, IO_APIC_REDIR_VECTOR_MASK);
580 io_apic_modify(entry->apic, 0x10 + pin*2, data);
581
582 if (!entry->next)
583 break;
584 entry = irq_2_pin + entry->next;
585 }
586 }
587
588 spin_unlock(&ioapic_lock);
589 }
590
591 /*
592 * Find the IRQ entry number of a certain pin.
593 */
find_irq_entry(int apic,int pin,int type)594 static int find_irq_entry(int apic, int pin, int type)
595 {
596 int i;
597
598 for (i = 0; i < mp_irq_entries; i++)
599 if (mp_irqs[i].mpc_irqtype == type &&
600 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
601 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
602 mp_irqs[i].mpc_dstirq == pin)
603 return i;
604
605 return -1;
606 }
607
608 /*
609 * Find the pin to which IRQ[irq] (ISA) is connected
610 */
find_isa_irq_pin(int irq,int type)611 static int __init find_isa_irq_pin(int irq, int type)
612 {
613 int i;
614
615 for (i = 0; i < mp_irq_entries; i++) {
616 int lbus = mp_irqs[i].mpc_srcbus;
617
618 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
619 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
620 mp_bus_id_to_type[lbus] == MP_BUS_MCA ||
621 mp_bus_id_to_type[lbus] == MP_BUS_NEC98
622 ) &&
623 (mp_irqs[i].mpc_irqtype == type) &&
624 (mp_irqs[i].mpc_srcbusirq == irq))
625
626 return mp_irqs[i].mpc_dstirq;
627 }
628 return -1;
629 }
630
find_isa_irq_apic(int irq,int type)631 static int __init find_isa_irq_apic(int irq, int type)
632 {
633 int i;
634
635 for (i = 0; i < mp_irq_entries; i++) {
636 int lbus = mp_irqs[i].mpc_srcbus;
637
638 if ((mp_bus_id_to_type[lbus] == MP_BUS_ISA ||
639 mp_bus_id_to_type[lbus] == MP_BUS_EISA ||
640 mp_bus_id_to_type[lbus] == MP_BUS_MCA ||
641 mp_bus_id_to_type[lbus] == MP_BUS_NEC98
642 ) &&
643 (mp_irqs[i].mpc_irqtype == type) &&
644 (mp_irqs[i].mpc_srcbusirq == irq))
645 break;
646 }
647 if (i < mp_irq_entries) {
648 int apic;
649 for(apic = 0; apic < nr_ioapics; apic++) {
650 if (!nr_ioapic_entries[apic])
651 continue;
652 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
653 return apic;
654 }
655 }
656
657 return -1;
658 }
659
660 /*
661 * Find a specific PCI IRQ entry.
662 * Not an __init, possibly needed by modules
663 */
664 static int pin_2_irq(int idx, int apic, int pin);
665
666 /*
667 * This function currently is only a helper for the i386 smp boot process where
668 * we need to reprogram the ioredtbls to cater for the cpus which have come online
669 * so mask in all cases should simply be TARGET_CPUS
670 */
setup_ioapic_dest(void)671 void /*__init*/ setup_ioapic_dest(void)
672 {
673 int pin, ioapic, irq, irq_entry;
674
675 if (skip_ioapic_setup)
676 return;
677
678 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
679 for (pin = 0; pin < nr_ioapic_entries[ioapic]; pin++) {
680 struct irq_desc *desc;
681 unsigned long flags;
682
683 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
684 if (irq_entry == -1)
685 continue;
686 irq = pin_2_irq(irq_entry, ioapic, pin);
687 desc = irq_to_desc(irq);
688
689 spin_lock_irqsave(&desc->lock, flags);
690 BUG_ON(!cpumask_intersects(desc->arch.cpu_mask, &cpu_online_map));
691 set_ioapic_affinity_irq(desc, desc->arch.cpu_mask);
692 spin_unlock_irqrestore(&desc->lock, flags);
693 }
694 }
695 }
696
697 /*
698 * EISA Edge/Level control register, ELCR
699 */
EISA_ELCR(unsigned int irq)700 static int EISA_ELCR(unsigned int irq)
701 {
702 if (platform_legacy_irq(irq)) {
703 unsigned int port = 0x4d0 + (irq >> 3);
704 return (inb(port) >> (irq & 7)) & 1;
705 }
706 apic_printk(APIC_VERBOSE, KERN_INFO
707 "Broken MPtable reports ISA irq %d\n", irq);
708 return 0;
709 }
710
711 /* EISA interrupts are always polarity zero and can be edge or level
712 * trigger depending on the ELCR value. If an interrupt is listed as
713 * EISA conforming in the MP table, that means its trigger type must
714 * be read in from the ELCR */
715
716 #define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
717 #define default_EISA_polarity(idx) (0)
718
719 /* ISA interrupts are always polarity zero edge triggered,
720 * when listed as conforming in the MP table. */
721
722 #define default_ISA_trigger(idx) (0)
723 #define default_ISA_polarity(idx) (0)
724
725 /* PCI interrupts are always polarity one level triggered,
726 * when listed as conforming in the MP table. */
727
728 #define default_PCI_trigger(idx) (1)
729 #define default_PCI_polarity(idx) (1)
730
731 /* MCA interrupts are always polarity zero level triggered,
732 * when listed as conforming in the MP table. */
733
734 #define default_MCA_trigger(idx) (1)
735 #define default_MCA_polarity(idx) (0)
736
737 /* NEC98 interrupts are always polarity zero edge triggered,
738 * when listed as conforming in the MP table. */
739
740 #define default_NEC98_trigger(idx) (0)
741 #define default_NEC98_polarity(idx) (0)
742
MPBIOS_polarity(int idx)743 static int __init MPBIOS_polarity(int idx)
744 {
745 int bus = mp_irqs[idx].mpc_srcbus;
746 int polarity;
747
748 /*
749 * Determine IRQ line polarity (high active or low active):
750 */
751 switch (mp_irqs[idx].mpc_irqflag & 3)
752 {
753 case 0: /* conforms, ie. bus-type dependent polarity */
754 {
755 switch (mp_bus_id_to_type[bus])
756 {
757 case MP_BUS_ISA: /* ISA pin */
758 {
759 polarity = default_ISA_polarity(idx);
760 break;
761 }
762 case MP_BUS_EISA: /* EISA pin */
763 {
764 polarity = default_EISA_polarity(idx);
765 break;
766 }
767 case MP_BUS_PCI: /* PCI pin */
768 {
769 polarity = default_PCI_polarity(idx);
770 break;
771 }
772 case MP_BUS_MCA: /* MCA pin */
773 {
774 polarity = default_MCA_polarity(idx);
775 break;
776 }
777 case MP_BUS_NEC98: /* NEC 98 pin */
778 {
779 polarity = default_NEC98_polarity(idx);
780 break;
781 }
782 default:
783 {
784 printk(KERN_WARNING "broken BIOS!!\n");
785 polarity = 1;
786 break;
787 }
788 }
789 break;
790 }
791 case 1: /* high active */
792 {
793 polarity = 0;
794 break;
795 }
796 case 2: /* reserved */
797 {
798 printk(KERN_WARNING "broken BIOS!!\n");
799 polarity = 1;
800 break;
801 }
802 case 3: /* low active */
803 {
804 polarity = 1;
805 break;
806 }
807 default: /* invalid */
808 {
809 printk(KERN_WARNING "broken BIOS!!\n");
810 polarity = 1;
811 break;
812 }
813 }
814 return polarity;
815 }
816
MPBIOS_trigger(int idx)817 static int MPBIOS_trigger(int idx)
818 {
819 int bus = mp_irqs[idx].mpc_srcbus;
820 int trigger;
821
822 /*
823 * Determine IRQ trigger mode (edge or level sensitive):
824 */
825 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
826 {
827 case 0: /* conforms, ie. bus-type dependent */
828 {
829 switch (mp_bus_id_to_type[bus])
830 {
831 case MP_BUS_ISA: /* ISA pin */
832 {
833 trigger = default_ISA_trigger(idx);
834 break;
835 }
836 case MP_BUS_EISA: /* EISA pin */
837 {
838 trigger = default_EISA_trigger(idx);
839 break;
840 }
841 case MP_BUS_PCI: /* PCI pin */
842 {
843 trigger = default_PCI_trigger(idx);
844 break;
845 }
846 case MP_BUS_MCA: /* MCA pin */
847 {
848 trigger = default_MCA_trigger(idx);
849 break;
850 }
851 case MP_BUS_NEC98: /* NEC 98 pin */
852 {
853 trigger = default_NEC98_trigger(idx);
854 break;
855 }
856 default:
857 {
858 printk(KERN_WARNING "broken BIOS!!\n");
859 trigger = 1;
860 break;
861 }
862 }
863 break;
864 }
865 case 1: /* edge */
866 {
867 trigger = 0;
868 break;
869 }
870 case 2: /* reserved */
871 {
872 printk(KERN_WARNING "broken BIOS!!\n");
873 trigger = 1;
874 break;
875 }
876 case 3: /* level */
877 {
878 trigger = 1;
879 break;
880 }
881 default: /* invalid */
882 {
883 printk(KERN_WARNING "broken BIOS!!\n");
884 trigger = 0;
885 break;
886 }
887 }
888 return trigger;
889 }
890
irq_polarity(int idx)891 static inline int irq_polarity(int idx)
892 {
893 return MPBIOS_polarity(idx);
894 }
895
irq_trigger(int idx)896 static inline int irq_trigger(int idx)
897 {
898 return MPBIOS_trigger(idx);
899 }
900
pin_2_irq(int idx,int apic,int pin)901 static int pin_2_irq(int idx, int apic, int pin)
902 {
903 int irq, i;
904 int bus = mp_irqs[idx].mpc_srcbus;
905
906 /*
907 * Debugging check, we are in big trouble if this message pops up!
908 */
909 if (mp_irqs[idx].mpc_dstirq != pin)
910 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
911
912 switch (mp_bus_id_to_type[bus])
913 {
914 case MP_BUS_ISA: /* ISA pin */
915 case MP_BUS_EISA:
916 case MP_BUS_MCA:
917 case MP_BUS_NEC98:
918 {
919 irq = mp_irqs[idx].mpc_srcbusirq;
920 break;
921 }
922 case MP_BUS_PCI: /* PCI pin */
923 {
924 /*
925 * PCI IRQs are mapped in order
926 */
927 i = irq = 0;
928 while (i < apic)
929 irq += nr_ioapic_entries[i++];
930 irq += pin;
931 break;
932 }
933 default:
934 {
935 printk(KERN_ERR "unknown bus type %d.\n",bus);
936 irq = 0;
937 break;
938 }
939 }
940
941 return irq;
942 }
943
IO_APIC_irq_trigger(int irq)944 static inline int IO_APIC_irq_trigger(int irq)
945 {
946 int apic, idx, pin;
947
948 for (apic = 0; apic < nr_ioapics; apic++) {
949 for (pin = 0; pin < nr_ioapic_entries[apic]; pin++) {
950 idx = find_irq_entry(apic,pin,mp_INT);
951 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
952 return irq_trigger(idx);
953 }
954 }
955 /*
956 * nonexistent IRQs are edge default
957 */
958 return 0;
959 }
960
961 static struct hw_interrupt_type ioapic_level_type;
962 static hw_irq_controller ioapic_edge_type;
963
964 #define IOAPIC_AUTO -1
965 #define IOAPIC_EDGE 0
966 #define IOAPIC_LEVEL 1
967
968 #define SET_DEST(ent, mode, val) do { \
969 if (x2apic_enabled && iommu_intremap) \
970 (ent).dest.dest32 = (val); \
971 else \
972 (ent).dest.mode.mode##_dest = (val); \
973 } while (0)
974
ioapic_register_intr(int irq,unsigned long trigger)975 static inline void ioapic_register_intr(int irq, unsigned long trigger)
976 {
977 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
978 trigger == IOAPIC_LEVEL)
979 irq_desc[irq].handler = &ioapic_level_type;
980 else
981 irq_desc[irq].handler = &ioapic_edge_type;
982 }
983
setup_IO_APIC_irqs(void)984 static void __init setup_IO_APIC_irqs(void)
985 {
986 struct IO_APIC_route_entry entry;
987 int apic, pin, idx, irq, first_notcon = 1, vector;
988 unsigned long flags;
989
990 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
991
992 for (apic = 0; apic < nr_ioapics; apic++) {
993 for (pin = 0; pin < nr_ioapic_entries[apic]; pin++) {
994 /*
995 * add it to the IO-APIC irq-routing table:
996 */
997 memset(&entry,0,sizeof(entry));
998
999 entry.delivery_mode = INT_DELIVERY_MODE;
1000 entry.dest_mode = INT_DEST_MODE;
1001 entry.mask = 0; /* enable IRQ */
1002
1003 idx = find_irq_entry(apic,pin,mp_INT);
1004 if (idx == -1) {
1005 if (first_notcon) {
1006 apic_printk(APIC_VERBOSE, KERN_DEBUG
1007 " IO-APIC (apicid-pin) %d-%d",
1008 mp_ioapics[apic].mpc_apicid,
1009 pin);
1010 first_notcon = 0;
1011 } else
1012 apic_printk(APIC_VERBOSE, ", %d-%d",
1013 mp_ioapics[apic].mpc_apicid, pin);
1014 continue;
1015 }
1016
1017 entry.trigger = irq_trigger(idx);
1018 entry.polarity = irq_polarity(idx);
1019
1020 if (irq_trigger(idx)) {
1021 entry.trigger = 1;
1022 entry.mask = 1;
1023 }
1024
1025 irq = pin_2_irq(idx, apic, pin);
1026 /*
1027 * skip adding the timer int on secondary nodes, which causes
1028 * a small but painful rift in the time-space continuum
1029 */
1030 if (multi_timer_check(apic, irq))
1031 continue;
1032 else
1033 add_pin_to_irq(irq, apic, pin);
1034
1035 if (!IO_APIC_IRQ(irq))
1036 continue;
1037
1038 vector = assign_irq_vector(irq, NULL);
1039 BUG_ON(vector < 0);
1040 entry.vector = vector;
1041 ioapic_register_intr(irq, IOAPIC_AUTO);
1042
1043 if (platform_legacy_irq(irq))
1044 disable_8259A_irq(irq_to_desc(irq));
1045
1046 SET_DEST(entry, logical, cpu_mask_to_apicid(TARGET_CPUS));
1047 spin_lock_irqsave(&ioapic_lock, flags);
1048 __ioapic_write_entry(apic, pin, 0, entry);
1049 spin_unlock_irqrestore(&ioapic_lock, flags);
1050 }
1051 }
1052
1053 if (!first_notcon)
1054 apic_printk(APIC_VERBOSE, " not connected.\n");
1055 }
1056
1057 /*
1058 * Set up the 8259A-master output pin:
1059 */
setup_ExtINT_IRQ0_pin(unsigned int apic,unsigned int pin,int vector)1060 static void __init setup_ExtINT_IRQ0_pin(unsigned int apic, unsigned int pin, int vector)
1061 {
1062 struct IO_APIC_route_entry entry;
1063
1064 memset(&entry,0,sizeof(entry));
1065
1066 disable_8259A_irq(irq_to_desc(0));
1067
1068 /* mask LVT0 */
1069 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1070
1071 /*
1072 * We use logical delivery to get the timer IRQ
1073 * to the first CPU.
1074 */
1075 entry.dest_mode = INT_DEST_MODE;
1076 entry.mask = 0; /* unmask IRQ now */
1077 SET_DEST(entry, logical, cpu_mask_to_apicid(TARGET_CPUS));
1078 entry.delivery_mode = INT_DELIVERY_MODE;
1079 entry.polarity = 0;
1080 entry.trigger = 0;
1081 entry.vector = vector;
1082
1083 /*
1084 * The timer IRQ doesn't have to know that behind the
1085 * scene we have a 8259A-master in AEOI mode ...
1086 */
1087 irq_desc[0].handler = &ioapic_edge_type;
1088
1089 /*
1090 * Add it to the IO-APIC irq-routing table:
1091 */
1092 ioapic_write_entry(apic, pin, 0, entry);
1093
1094 enable_8259A_irq(irq_to_desc(0));
1095 }
1096
UNEXPECTED_IO_APIC(void)1097 static inline void UNEXPECTED_IO_APIC(void)
1098 {
1099 }
1100
__print_IO_APIC(bool boot)1101 static void /*__init*/ __print_IO_APIC(bool boot)
1102 {
1103 int apic, i;
1104 union IO_APIC_reg_00 reg_00;
1105 union IO_APIC_reg_01 reg_01;
1106 union IO_APIC_reg_02 reg_02;
1107 union IO_APIC_reg_03 reg_03;
1108 unsigned long flags;
1109
1110 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1111 for (i = 0; i < nr_ioapics; i++)
1112 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1113 mp_ioapics[i].mpc_apicid, nr_ioapic_entries[i]);
1114
1115 /*
1116 * We are a bit conservative about what we expect. We have to
1117 * know about every hardware change ASAP.
1118 */
1119 printk(KERN_INFO "testing the IO APIC.......................\n");
1120
1121 for (apic = 0; apic < nr_ioapics; apic++) {
1122 if ( !boot )
1123 process_pending_softirqs();
1124
1125 if (!nr_ioapic_entries[apic])
1126 continue;
1127
1128 spin_lock_irqsave(&ioapic_lock, flags);
1129 reg_00.raw = io_apic_read(apic, 0);
1130 reg_01.raw = io_apic_read(apic, 1);
1131 if (reg_01.bits.version >= 0x10)
1132 reg_02.raw = io_apic_read(apic, 2);
1133 if (reg_01.bits.version >= 0x20)
1134 reg_03.raw = io_apic_read(apic, 3);
1135 spin_unlock_irqrestore(&ioapic_lock, flags);
1136
1137 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
1138 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1139 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1140 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1141 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
1142 if (reg_00.bits.ID >= get_physical_broadcast())
1143 UNEXPECTED_IO_APIC();
1144 if (reg_00.bits.__reserved_1 || reg_00.bits.__reserved_2)
1145 UNEXPECTED_IO_APIC();
1146
1147 printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
1148 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
1149 if ( (reg_01.bits.entries != 0x0f) && /* older (Neptune) boards */
1150 (reg_01.bits.entries != 0x17) && /* typical ISA+PCI boards */
1151 (reg_01.bits.entries != 0x1b) && /* Compaq Proliant boards */
1152 (reg_01.bits.entries != 0x1f) && /* dual Xeon boards */
1153 (reg_01.bits.entries != 0x22) && /* bigger Xeon boards */
1154 (reg_01.bits.entries != 0x2E) &&
1155 (reg_01.bits.entries != 0x3F)
1156 )
1157 UNEXPECTED_IO_APIC();
1158
1159 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1160 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
1161 if ( (reg_01.bits.version != 0x01) && /* 82489DX IO-APICs */
1162 (reg_01.bits.version != 0x10) && /* oldest IO-APICs */
1163 (reg_01.bits.version != 0x11) && /* Pentium/Pro IO-APICs */
1164 (reg_01.bits.version != 0x13) && /* Xeon IO-APICs */
1165 (reg_01.bits.version != 0x20) /* Intel P64H (82806 AA) */
1166 )
1167 UNEXPECTED_IO_APIC();
1168 if (reg_01.bits.__reserved_1 || reg_01.bits.__reserved_2)
1169 UNEXPECTED_IO_APIC();
1170
1171 /*
1172 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1173 * but the value of reg_02 is read as the previous read register
1174 * value, so ignore it if reg_02 == reg_01.
1175 */
1176 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1177 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1178 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
1179 if (reg_02.bits.__reserved_1 || reg_02.bits.__reserved_2)
1180 UNEXPECTED_IO_APIC();
1181 }
1182
1183 /*
1184 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1185 * or reg_03, but the value of reg_0[23] is read as the previous read
1186 * register value, so ignore it if reg_03 == reg_0[12].
1187 */
1188 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1189 reg_03.raw != reg_01.raw) {
1190 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1191 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
1192 if (reg_03.bits.__reserved_1)
1193 UNEXPECTED_IO_APIC();
1194 }
1195
1196 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1197
1198 printk(KERN_DEBUG " NR %s Msk Trg IRR Pol Stat DstM DelM Vec\n",
1199 (x2apic_enabled && iommu_intremap) ? " DestID" : "Dst");
1200
1201 for (i = 0; i <= reg_01.bits.entries; i++) {
1202 struct IO_APIC_route_entry entry;
1203
1204 entry = ioapic_read_entry(apic, i, 0);
1205
1206 if ( x2apic_enabled && iommu_intremap )
1207 printk(KERN_DEBUG " %02x %08x", i, entry.dest.dest32);
1208 else
1209 printk(KERN_DEBUG " %02x %02x ", i,
1210 entry.dest.logical.logical_dest);
1211
1212 printk(" %d %d %d %d %d %d %d %02X\n",
1213 entry.mask,
1214 entry.trigger,
1215 entry.irr,
1216 entry.polarity,
1217 entry.delivery_status,
1218 entry.dest_mode,
1219 entry.delivery_mode,
1220 entry.vector
1221 );
1222 }
1223 }
1224 printk(KERN_INFO "Using vector-based indexing\n");
1225 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1226 for (i = 0; i < nr_irqs_gsi; i++) {
1227 struct irq_pin_list *entry = irq_2_pin + i;
1228
1229 if ( !boot && !(i & 0x1f) )
1230 process_pending_softirqs();
1231
1232 if (entry->pin < 0)
1233 continue;
1234 printk(KERN_DEBUG "IRQ%d ", irq_to_desc(i)->arch.vector);
1235 for (;;) {
1236 printk("-> %d:%d", entry->apic, entry->pin);
1237 if (!entry->next)
1238 break;
1239 entry = irq_2_pin + entry->next;
1240 }
1241 printk("\n");
1242 }
1243
1244 printk(KERN_INFO ".................................... done.\n");
1245
1246 return;
1247 }
1248
print_IO_APIC(void)1249 static void __init print_IO_APIC(void)
1250 {
1251 if (apic_verbosity != APIC_QUIET)
1252 __print_IO_APIC(1);
1253 }
1254
_print_IO_APIC_keyhandler(unsigned char key)1255 static void _print_IO_APIC_keyhandler(unsigned char key)
1256 {
1257 __print_IO_APIC(0);
1258 }
1259
enable_IO_APIC(void)1260 static void __init enable_IO_APIC(void)
1261 {
1262 int i8259_apic, i8259_pin;
1263 int i, apic;
1264
1265 /* Initialise dynamic irq_2_pin free list. */
1266 irq_2_pin = xzalloc_array(struct irq_pin_list, PIN_MAP_SIZE);
1267
1268 for (i = 0; i < PIN_MAP_SIZE; i++)
1269 irq_2_pin[i].pin = -1;
1270 for (i = irq_2_pin_free_entry = nr_irqs_gsi; i < PIN_MAP_SIZE; i++)
1271 irq_2_pin[i].next = i + 1;
1272
1273 if (directed_eoi_enabled) {
1274 for (apic = 0; apic < nr_ioapics; apic++) {
1275 if (!nr_ioapic_entries[apic])
1276 continue;
1277 vector_map[apic] = xzalloc(vmask_t);
1278 BUG_ON(!vector_map[apic]);
1279 }
1280 } else {
1281 vector_map[0] = xzalloc(vmask_t);
1282 BUG_ON(!vector_map[0]);
1283 for (apic = 1; apic < nr_ioapics; apic++)
1284 vector_map[apic] = vector_map[0];
1285 }
1286
1287 for(apic = 0; apic < nr_ioapics; apic++) {
1288 int pin;
1289 /* See if any of the pins is in ExtINT mode */
1290 for (pin = 0; pin < nr_ioapic_entries[apic]; pin++) {
1291 struct IO_APIC_route_entry entry = ioapic_read_entry(apic, pin, 0);
1292
1293 /* If the interrupt line is enabled and in ExtInt mode
1294 * I have found the pin where the i8259 is connected.
1295 */
1296 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1297 ioapic_i8259.apic = apic;
1298 ioapic_i8259.pin = pin;
1299 goto found_i8259;
1300 }
1301 }
1302 }
1303 found_i8259:
1304 /* Look to see what if the MP table has reported the ExtINT */
1305 /* If we could not find the appropriate pin by looking at the ioapic
1306 * the i8259 probably is not connected the ioapic but give the
1307 * mptable a chance anyway.
1308 */
1309 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1310 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1311 /* Trust the MP table if nothing is setup in the hardware */
1312 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1313 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1314 ioapic_i8259.pin = i8259_pin;
1315 ioapic_i8259.apic = i8259_apic;
1316 }
1317 /* Complain if the MP table and the hardware disagree */
1318 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1319 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1320 {
1321 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1322 }
1323
1324 /*
1325 * Do not trust the IO-APIC being empty at bootup
1326 */
1327 clear_IO_APIC();
1328 }
1329
1330 /*
1331 * Not an __init, needed by the reboot code
1332 */
disable_IO_APIC(void)1333 void disable_IO_APIC(void)
1334 {
1335 /*
1336 * Clear the IO-APIC before rebooting:
1337 */
1338 clear_IO_APIC();
1339
1340 /*
1341 * If the i8259 is routed through an IOAPIC
1342 * Put that IOAPIC in virtual wire mode
1343 * so legacy interrupts can be delivered.
1344 */
1345 if (ioapic_i8259.pin != -1) {
1346 struct IO_APIC_route_entry entry;
1347
1348 memset(&entry, 0, sizeof(entry));
1349 entry.mask = 0; /* Enabled */
1350 entry.trigger = 0; /* Edge */
1351 entry.irr = 0;
1352 entry.polarity = 0; /* High */
1353 entry.delivery_status = 0;
1354 entry.dest_mode = 0; /* Physical */
1355 entry.delivery_mode = dest_ExtINT; /* ExtInt */
1356 entry.vector = 0;
1357 SET_DEST(entry, physical, get_apic_id());
1358
1359 /*
1360 * Add it to the IO-APIC irq-routing table:
1361 */
1362 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, 0, entry);
1363 }
1364 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1365 }
1366
1367 /*
1368 * function to set the IO-APIC physical IDs based on the
1369 * values stored in the MPC table.
1370 *
1371 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1372 */
1373
setup_ioapic_ids_from_mpc(void)1374 static void __init setup_ioapic_ids_from_mpc(void)
1375 {
1376 union IO_APIC_reg_00 reg_00;
1377 static physid_mask_t __initdata phys_id_present_map;
1378 int apic;
1379 int i;
1380 unsigned char old_id;
1381 unsigned long flags;
1382
1383 /*
1384 * Don't check I/O APIC IDs for xAPIC systems. They have
1385 * no meaning without the serial APIC bus.
1386 */
1387 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1388 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
1389 return;
1390
1391 /*
1392 * This is broken; anything with a real cpu count has to
1393 * circumvent this idiocy regardless.
1394 */
1395 ioapic_phys_id_map(&phys_id_present_map);
1396
1397 /*
1398 * Set the IOAPIC ID to the value stored in the MPC table.
1399 */
1400 for (apic = 0; apic < nr_ioapics; apic++) {
1401 if (!nr_ioapic_entries[apic])
1402 continue;
1403
1404 /* Read the register 0 value */
1405 spin_lock_irqsave(&ioapic_lock, flags);
1406 reg_00.raw = io_apic_read(apic, 0);
1407 spin_unlock_irqrestore(&ioapic_lock, flags);
1408
1409 old_id = mp_ioapics[apic].mpc_apicid;
1410
1411 if (mp_ioapics[apic].mpc_apicid >= get_physical_broadcast()) {
1412 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1413 apic, mp_ioapics[apic].mpc_apicid);
1414 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1415 reg_00.bits.ID);
1416 mp_ioapics[apic].mpc_apicid = reg_00.bits.ID;
1417 }
1418
1419 /*
1420 * Sanity check, is the ID really free? Every APIC in a
1421 * system must have a unique ID or we get lots of nice
1422 * 'stuck on smp_invalidate_needed IPI wait' messages.
1423 */
1424 if (check_apicid_used(&phys_id_present_map,
1425 mp_ioapics[apic].mpc_apicid)) {
1426 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1427 apic, mp_ioapics[apic].mpc_apicid);
1428 for (i = 0; i < get_physical_broadcast(); i++)
1429 if (!physid_isset(i, phys_id_present_map))
1430 break;
1431 if (i >= get_physical_broadcast())
1432 panic("Max APIC ID exceeded\n");
1433 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1434 i);
1435 mp_ioapics[apic].mpc_apicid = i;
1436 } else {
1437 apic_printk(APIC_VERBOSE, "Setting %d in the "
1438 "phys_id_present_map\n",
1439 mp_ioapics[apic].mpc_apicid);
1440 }
1441 set_apicid(mp_ioapics[apic].mpc_apicid, &phys_id_present_map);
1442
1443 /*
1444 * We need to adjust the IRQ routing table
1445 * if the ID changed.
1446 */
1447 if (old_id != mp_ioapics[apic].mpc_apicid)
1448 for (i = 0; i < mp_irq_entries; i++)
1449 if (mp_irqs[i].mpc_dstapic == old_id)
1450 mp_irqs[i].mpc_dstapic
1451 = mp_ioapics[apic].mpc_apicid;
1452
1453 /*
1454 * Read the right value from the MPC table and
1455 * write it into the ID register.
1456 */
1457 apic_printk(APIC_VERBOSE, KERN_INFO
1458 "...changing IO-APIC physical APIC ID to %d ...",
1459 mp_ioapics[apic].mpc_apicid);
1460
1461 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1462 spin_lock_irqsave(&ioapic_lock, flags);
1463 io_apic_write(apic, 0, reg_00.raw);
1464 spin_unlock_irqrestore(&ioapic_lock, flags);
1465
1466 /*
1467 * Sanity check
1468 */
1469 spin_lock_irqsave(&ioapic_lock, flags);
1470 reg_00.raw = io_apic_read(apic, 0);
1471 spin_unlock_irqrestore(&ioapic_lock, flags);
1472 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1473 printk("could not set ID!\n");
1474 else
1475 apic_printk(APIC_VERBOSE, " ok.\n");
1476 }
1477 }
1478
1479 /*
1480 * There is a nasty bug in some older SMP boards, their mptable lies
1481 * about the timer IRQ. We do the following to work around the situation:
1482 *
1483 * - timer IRQ defaults to IO-APIC IRQ
1484 * - if this function detects that timer IRQs are defunct, then we fall
1485 * back to ISA timer IRQs
1486 */
timer_irq_works(void)1487 static int __init timer_irq_works(void)
1488 {
1489 unsigned long t1, flags;
1490
1491 t1 = ACCESS_ONCE(pit0_ticks);
1492
1493 local_save_flags(flags);
1494 local_irq_enable();
1495 /* Let ten ticks pass... */
1496 mdelay((10 * 1000) / HZ);
1497 local_irq_restore(flags);
1498
1499 /*
1500 * Expect a few ticks at least, to be sure some possible
1501 * glue logic does not lock up after one or two first
1502 * ticks in a non-ExtINT mode. Also the local APIC
1503 * might have cached one ExtINT interrupt. Finally, at
1504 * least one tick may be lost due to delays.
1505 */
1506 if ( (ACCESS_ONCE(pit0_ticks) - t1) > 4 )
1507 return 1;
1508
1509 return 0;
1510 }
1511
1512 /*
1513 * In the SMP+IOAPIC case it might happen that there are an unspecified
1514 * number of pending IRQ events unhandled. These cases are very rare,
1515 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1516 * better to do it this way as thus we do not have to be aware of
1517 * 'pending' interrupts in the IRQ path, except at this point.
1518 */
1519 /*
1520 * Edge triggered needs to resend any interrupt
1521 * that was delayed but this is now handled in the device
1522 * independent code.
1523 */
1524
1525 /*
1526 * Starting up a edge-triggered IO-APIC interrupt is
1527 * nasty - we need to make sure that we get the edge.
1528 * If it is already asserted for some reason, we need
1529 * return 1 to indicate that is was pending.
1530 *
1531 * This is not complete - we should be able to fake
1532 * an edge even if it isn't on the 8259A...
1533 */
startup_edge_ioapic_irq(struct irq_desc * desc)1534 static unsigned int startup_edge_ioapic_irq(struct irq_desc *desc)
1535 {
1536 int was_pending = 0;
1537 unsigned long flags;
1538
1539 spin_lock_irqsave(&ioapic_lock, flags);
1540 if (platform_legacy_irq(desc->irq)) {
1541 disable_8259A_irq(desc);
1542 if (i8259A_irq_pending(desc->irq))
1543 was_pending = 1;
1544 }
1545 __unmask_IO_APIC_irq(desc->irq);
1546 spin_unlock_irqrestore(&ioapic_lock, flags);
1547
1548 return was_pending;
1549 }
1550
1551 /*
1552 * Once we have recorded IRQ_PENDING already, we can mask the
1553 * interrupt for real. This prevents IRQ storms from unhandled
1554 * devices.
1555 */
ack_edge_ioapic_irq(struct irq_desc * desc)1556 static void ack_edge_ioapic_irq(struct irq_desc *desc)
1557 {
1558 irq_complete_move(desc);
1559 move_native_irq(desc);
1560
1561 if ((desc->status & (IRQ_PENDING | IRQ_DISABLED))
1562 == (IRQ_PENDING | IRQ_DISABLED))
1563 mask_IO_APIC_irq(desc);
1564 ack_APIC_irq();
1565 }
1566
1567 /*
1568 * Level triggered interrupts can just be masked,
1569 * and shutting down and starting up the interrupt
1570 * is the same as enabling and disabling them -- except
1571 * with a startup need to return a "was pending" value.
1572 *
1573 * Level triggered interrupts are special because we
1574 * do not touch any IO-APIC register while handling
1575 * them. We ack the APIC in the end-IRQ handler, not
1576 * in the start-IRQ-handler. Protection against reentrance
1577 * from the same interrupt is still provided, both by the
1578 * generic IRQ layer and by the fact that an unacked local
1579 * APIC does not accept IRQs.
1580 */
startup_level_ioapic_irq(struct irq_desc * desc)1581 static unsigned int startup_level_ioapic_irq(struct irq_desc *desc)
1582 {
1583 unmask_IO_APIC_irq(desc);
1584
1585 return 0; /* don't check for pending */
1586 }
1587
setup_ioapic_ack(const char * s)1588 static int __init setup_ioapic_ack(const char *s)
1589 {
1590 if ( !strcmp(s, "old") )
1591 {
1592 ioapic_ack_new = false;
1593 ioapic_ack_forced = true;
1594 }
1595 else if ( !strcmp(s, "new") )
1596 {
1597 ioapic_ack_new = true;
1598 ioapic_ack_forced = true;
1599 }
1600 else
1601 return -EINVAL;
1602
1603 return 0;
1604 }
1605 custom_param("ioapic_ack", setup_ioapic_ack);
1606
io_apic_level_ack_pending(unsigned int irq)1607 static bool io_apic_level_ack_pending(unsigned int irq)
1608 {
1609 struct irq_pin_list *entry;
1610 unsigned long flags;
1611
1612 spin_lock_irqsave(&ioapic_lock, flags);
1613 entry = &irq_2_pin[irq];
1614 for (;;) {
1615 unsigned int reg;
1616 int pin;
1617
1618 if (!entry)
1619 break;
1620
1621 pin = entry->pin;
1622 if (pin == -1)
1623 continue;
1624 reg = io_apic_read(entry->apic, 0x10 + pin*2);
1625 /* Is the remote IRR bit set? */
1626 if (reg & IO_APIC_REDIR_REMOTE_IRR) {
1627 spin_unlock_irqrestore(&ioapic_lock, flags);
1628 return 1;
1629 }
1630 if (!entry->next)
1631 break;
1632 entry = irq_2_pin + entry->next;
1633 }
1634 spin_unlock_irqrestore(&ioapic_lock, flags);
1635
1636 return 0;
1637 }
1638
mask_and_ack_level_ioapic_irq(struct irq_desc * desc)1639 static void mask_and_ack_level_ioapic_irq(struct irq_desc *desc)
1640 {
1641 unsigned long v;
1642 int i;
1643
1644 irq_complete_move(desc);
1645
1646 if ( !directed_eoi_enabled )
1647 mask_IO_APIC_irq(desc);
1648
1649 /*
1650 * It appears there is an erratum which affects at least version 0x11
1651 * of I/O APIC (that's the 82093AA and cores integrated into various
1652 * chipsets). Under certain conditions a level-triggered interrupt is
1653 * erroneously delivered as edge-triggered one but the respective IRR
1654 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1655 * message but it will never arrive and further interrupts are blocked
1656 * from the source. The exact reason is so far unknown, but the
1657 * phenomenon was observed when two consecutive interrupt requests
1658 * from a given source get delivered to the same CPU and the source is
1659 * temporarily disabled in between.
1660 *
1661 * A workaround is to simulate an EOI message manually. We achieve it
1662 * by setting the trigger mode to edge and then to level when the edge
1663 * trigger mode gets detected in the TMR of a local APIC for a
1664 * level-triggered interrupt. We mask the source for the time of the
1665 * operation to prevent an edge-triggered interrupt escaping meanwhile.
1666 * The idea is from Manfred Spraul. --macro
1667 */
1668 i = desc->arch.vector;
1669
1670 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1671
1672 ack_APIC_irq();
1673
1674 if ( directed_eoi_enabled )
1675 return;
1676
1677 if ((desc->status & IRQ_MOVE_PENDING) &&
1678 !io_apic_level_ack_pending(desc->irq))
1679 move_masked_irq(desc);
1680
1681 if ( !(v & (1 << (i & 0x1f))) ) {
1682 spin_lock(&ioapic_lock);
1683 __edge_IO_APIC_irq(desc->irq);
1684 __level_IO_APIC_irq(desc->irq);
1685 spin_unlock(&ioapic_lock);
1686 }
1687 }
1688
end_level_ioapic_irq_old(struct irq_desc * desc,u8 vector)1689 static void end_level_ioapic_irq_old(struct irq_desc *desc, u8 vector)
1690 {
1691 if ( directed_eoi_enabled )
1692 {
1693 if ( !(desc->status & (IRQ_DISABLED|IRQ_MOVE_PENDING)) )
1694 {
1695 eoi_IO_APIC_irq(desc);
1696 return;
1697 }
1698
1699 mask_IO_APIC_irq(desc);
1700 eoi_IO_APIC_irq(desc);
1701 if ( (desc->status & IRQ_MOVE_PENDING) &&
1702 !io_apic_level_ack_pending(desc->irq) )
1703 move_masked_irq(desc);
1704 }
1705
1706 if ( !(desc->status & IRQ_DISABLED) )
1707 unmask_IO_APIC_irq(desc);
1708 }
1709
end_level_ioapic_irq_new(struct irq_desc * desc,u8 vector)1710 static void end_level_ioapic_irq_new(struct irq_desc *desc, u8 vector)
1711 {
1712 /*
1713 * It appears there is an erratum which affects at least version 0x11
1714 * of I/O APIC (that's the 82093AA and cores integrated into various
1715 * chipsets). Under certain conditions a level-triggered interrupt is
1716 * erroneously delivered as edge-triggered one but the respective IRR
1717 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1718 * message but it will never arrive and further interrupts are blocked
1719 * from the source. The exact reason is so far unknown, but the
1720 * phenomenon was observed when two consecutive interrupt requests
1721 * from a given source get delivered to the same CPU and the source is
1722 * temporarily disabled in between.
1723 *
1724 * A workaround is to simulate an EOI message manually. We achieve it
1725 * by setting the trigger mode to edge and then to level when the edge
1726 * trigger mode gets detected in the TMR of a local APIC for a
1727 * level-triggered interrupt. We mask the source for the time of the
1728 * operation to prevent an edge-triggered interrupt escaping meanwhile.
1729 * The idea is from Manfred Spraul. --macro
1730 */
1731 unsigned int v, i = desc->arch.vector;
1732
1733 /* Manually EOI the old vector if we are moving to the new */
1734 if ( vector && i != vector )
1735 eoi_IO_APIC_irq(desc);
1736
1737 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1738
1739 end_nonmaskable_irq(desc, vector);
1740
1741 if ( (desc->status & IRQ_MOVE_PENDING) &&
1742 !io_apic_level_ack_pending(desc->irq) )
1743 move_native_irq(desc);
1744
1745 if (!(v & (1 << (i & 0x1f)))) {
1746 spin_lock(&ioapic_lock);
1747 __mask_IO_APIC_irq(desc->irq);
1748 __edge_IO_APIC_irq(desc->irq);
1749 __level_IO_APIC_irq(desc->irq);
1750 if ( !(desc->status & IRQ_DISABLED) )
1751 __unmask_IO_APIC_irq(desc->irq);
1752 spin_unlock(&ioapic_lock);
1753 }
1754 }
1755
1756 /*
1757 * Level and edge triggered IO-APIC interrupts need different handling,
1758 * so we use two separate IRQ descriptors. Edge triggered IRQs can be
1759 * handled with the level-triggered descriptor, but that one has slightly
1760 * more overhead. Level-triggered interrupts cannot be handled with the
1761 * edge-triggered handler, without risking IRQ storms and other ugly
1762 * races.
1763 */
1764 static hw_irq_controller ioapic_edge_type = {
1765 .typename = "IO-APIC-edge",
1766 .startup = startup_edge_ioapic_irq,
1767 .shutdown = irq_shutdown_none,
1768 .enable = unmask_IO_APIC_irq,
1769 .disable = irq_disable_none,
1770 .ack = ack_edge_ioapic_irq,
1771 .set_affinity = set_ioapic_affinity_irq,
1772 };
1773
1774 static struct hw_interrupt_type __read_mostly ioapic_level_type = {
1775 .typename = "IO-APIC-level",
1776 .startup = startup_level_ioapic_irq,
1777 .shutdown = mask_IO_APIC_irq,
1778 .enable = unmask_IO_APIC_irq,
1779 .disable = mask_IO_APIC_irq,
1780 .ack = mask_and_ack_level_ioapic_irq,
1781 .end = end_level_ioapic_irq_old,
1782 .set_affinity = set_ioapic_affinity_irq,
1783 };
1784
init_IO_APIC_traps(void)1785 static inline void init_IO_APIC_traps(void)
1786 {
1787 int irq;
1788 /* Xen: This is way simpler than the Linux implementation. */
1789 for (irq = 0; platform_legacy_irq(irq); irq++)
1790 if (IO_APIC_IRQ(irq) && !irq_to_vector(irq))
1791 make_8259A_irq(irq);
1792 }
1793
enable_lapic_irq(struct irq_desc * desc)1794 static void enable_lapic_irq(struct irq_desc *desc)
1795 {
1796 unsigned long v;
1797
1798 v = apic_read(APIC_LVT0);
1799 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
1800 }
1801
disable_lapic_irq(struct irq_desc * desc)1802 static void disable_lapic_irq(struct irq_desc *desc)
1803 {
1804 unsigned long v;
1805
1806 v = apic_read(APIC_LVT0);
1807 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1808 }
1809
ack_lapic_irq(struct irq_desc * desc)1810 static void ack_lapic_irq(struct irq_desc *desc)
1811 {
1812 ack_APIC_irq();
1813 }
1814
1815 static hw_irq_controller lapic_irq_type = {
1816 .typename = "local-APIC-edge",
1817 .startup = NULL, /* startup_irq() not used for IRQ0 */
1818 .shutdown = NULL, /* shutdown_irq() not used for IRQ0 */
1819 .enable = enable_lapic_irq,
1820 .disable = disable_lapic_irq,
1821 .ack = ack_lapic_irq,
1822 };
1823
1824 /*
1825 * This looks a bit hackish but it's about the only one way of sending
1826 * a few INTA cycles to 8259As and any associated glue logic. ICR does
1827 * not support the ExtINT mode, unfortunately. We need to send these
1828 * cycles as some i82489DX-based boards have glue logic that keeps the
1829 * 8259A interrupt line asserted until INTA. --macro
1830 */
unlock_ExtINT_logic(void)1831 static void __init unlock_ExtINT_logic(void)
1832 {
1833 int apic, pin, i;
1834 struct IO_APIC_route_entry entry0, entry1;
1835 unsigned char save_control, save_freq_select;
1836
1837 pin = find_isa_irq_pin(8, mp_INT);
1838 apic = find_isa_irq_apic(8, mp_INT);
1839 if ( pin == -1 || apic == -1 )
1840 return;
1841
1842 entry0 = ioapic_read_entry(apic, pin, 0);
1843 clear_IO_APIC_pin(apic, pin);
1844
1845 memset(&entry1, 0, sizeof(entry1));
1846
1847 entry1.dest_mode = 0; /* physical delivery */
1848 entry1.mask = 0; /* unmask IRQ now */
1849 SET_DEST(entry1, physical, get_apic_id());
1850 entry1.delivery_mode = dest_ExtINT;
1851 entry1.polarity = entry0.polarity;
1852 entry1.trigger = 0;
1853 entry1.vector = 0;
1854
1855 ioapic_write_entry(apic, pin, 0, entry1);
1856
1857 save_control = CMOS_READ(RTC_CONTROL);
1858 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
1859 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
1860 RTC_FREQ_SELECT);
1861 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
1862
1863 i = 100;
1864 while (i-- > 0) {
1865 mdelay(10);
1866 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
1867 i -= 10;
1868 }
1869
1870 CMOS_WRITE(save_control, RTC_CONTROL);
1871 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
1872 clear_IO_APIC_pin(apic, pin);
1873
1874 ioapic_write_entry(apic, pin, 0, entry0);
1875 }
1876
1877 /*
1878 * This code may look a bit paranoid, but it's supposed to cooperate with
1879 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
1880 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
1881 * fanatically on his truly buggy board.
1882 */
check_timer(void)1883 static void __init check_timer(void)
1884 {
1885 int apic1, pin1, apic2, pin2;
1886 int vector, ret;
1887 unsigned long flags;
1888
1889 local_irq_save(flags);
1890
1891 /*
1892 * get/set the timer IRQ vector:
1893 */
1894 disable_8259A_irq(irq_to_desc(0));
1895 vector = IRQ0_VECTOR;
1896 clear_irq_vector(0);
1897
1898 if ((ret = bind_irq_vector(0, vector, &cpumask_all)))
1899 printk(KERN_ERR"..IRQ0 is not set correctly with ioapic!!!, err:%d\n", ret);
1900
1901 irq_desc[0].status &= ~IRQ_DISABLED;
1902
1903 /*
1904 * Subtle, code in do_timer_interrupt() expects an AEOI
1905 * mode for the 8259A whenever interrupts are routed
1906 * through I/O APICs. Also IRQ0 has to be enabled in
1907 * the 8259A which implies the virtual wire has to be
1908 * disabled in the local APIC.
1909 */
1910 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
1911 init_8259A(1);
1912 /* XEN: Ripped out the legacy missed-tick logic, so below is not needed. */
1913 /*timer_ack = 1;*/
1914 /*enable_8259A_irq(irq_to_desc(0));*/
1915
1916 pin1 = find_isa_irq_pin(0, mp_INT);
1917 apic1 = find_isa_irq_apic(0, mp_INT);
1918 pin2 = ioapic_i8259.pin;
1919 apic2 = ioapic_i8259.apic;
1920
1921 printk(KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
1922 vector, apic1, pin1, apic2, pin2);
1923
1924 if (pin1 != -1) {
1925 /*
1926 * Ok, does IRQ0 through the IOAPIC work?
1927 */
1928 unmask_IO_APIC_irq(irq_to_desc(0));
1929 if (timer_irq_works()) {
1930 local_irq_restore(flags);
1931 return;
1932 }
1933 clear_IO_APIC_pin(apic1, pin1);
1934 printk(KERN_ERR "..MP-BIOS bug: 8254 timer not connected to "
1935 "IO-APIC\n");
1936 }
1937
1938 printk(KERN_INFO "...trying to set up timer (IRQ0) through the 8259A ... ");
1939 if (pin2 != -1) {
1940 printk("\n..... (found pin %d) ...", pin2);
1941 /*
1942 * legacy devices should be connected to IO APIC #0
1943 */
1944 setup_ExtINT_IRQ0_pin(apic2, pin2, vector);
1945 if (timer_irq_works()) {
1946 local_irq_restore(flags);
1947 printk("works.\n");
1948 if (pin1 != -1)
1949 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
1950 else
1951 add_pin_to_irq(0, apic2, pin2);
1952 return;
1953 }
1954 /*
1955 * Cleanup, just in case ...
1956 */
1957 clear_IO_APIC_pin(apic2, pin2);
1958 }
1959 printk(" failed.\n");
1960
1961 if (nmi_watchdog == NMI_IO_APIC) {
1962 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
1963 nmi_watchdog = 0;
1964 }
1965
1966 printk(KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
1967
1968 disable_8259A_irq(irq_to_desc(0));
1969 irq_desc[0].handler = &lapic_irq_type;
1970 apic_write(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
1971 enable_8259A_irq(irq_to_desc(0));
1972
1973 if (timer_irq_works()) {
1974 local_irq_restore(flags);
1975 printk(" works.\n");
1976 return;
1977 }
1978 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
1979 printk(" failed.\n");
1980
1981 printk(KERN_INFO "...trying to set up timer as ExtINT IRQ...");
1982
1983 /*timer_ack = 0;*/
1984 init_8259A(0);
1985 make_8259A_irq(0);
1986 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1987
1988 unlock_ExtINT_logic();
1989
1990 local_irq_restore(flags);
1991
1992 if (timer_irq_works()) {
1993 printk(" works.\n");
1994 return;
1995 }
1996 printk(" failed :(.\n");
1997 panic("IO-APIC + timer doesn't work! Boot with apic_verbosity=debug "
1998 "and send a report. Then try booting with the 'noapic' option\n");
1999 }
2000
2001 /*
2002 *
2003 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
2004 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
2005 * Linux doesn't really care, as it's not actually used
2006 * for any interrupt handling anyway.
2007 */
2008 #define PIC_IRQS (1 << PIC_CASCADE_IR)
2009
2010 static struct IO_APIC_route_entry *ioapic_pm_state;
2011
ioapic_pm_state_alloc(void)2012 static void __init ioapic_pm_state_alloc(void)
2013 {
2014 int i, nr_entry = 0;
2015
2016 for (i = 0; i < nr_ioapics; i++)
2017 nr_entry += nr_ioapic_entries[i];
2018
2019 ioapic_pm_state = _xmalloc(sizeof(struct IO_APIC_route_entry)*nr_entry,
2020 sizeof(struct IO_APIC_route_entry));
2021 BUG_ON(ioapic_pm_state == NULL);
2022 }
2023
setup_IO_APIC(void)2024 void __init setup_IO_APIC(void)
2025 {
2026 enable_IO_APIC();
2027
2028 if (acpi_ioapic)
2029 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
2030 else
2031 io_apic_irqs = ~PIC_IRQS;
2032
2033 printk("ENABLING IO-APIC IRQs\n");
2034 printk(" -> Using %s ACK method\n", ioapic_ack_new ? "new" : "old");
2035
2036 if (ioapic_ack_new) {
2037 ioapic_level_type.ack = irq_complete_move;
2038 ioapic_level_type.end = end_level_ioapic_irq_new;
2039 }
2040
2041 /*
2042 * Set up IO-APIC IRQ routing.
2043 */
2044 if (!acpi_ioapic)
2045 setup_ioapic_ids_from_mpc();
2046 sync_Arb_IDs();
2047 setup_IO_APIC_irqs();
2048 init_IO_APIC_traps();
2049 check_timer();
2050 print_IO_APIC();
2051 ioapic_pm_state_alloc();
2052
2053 register_keyhandler('z', _print_IO_APIC_keyhandler, "dump IOAPIC info", 1);
2054 }
2055
ioapic_suspend(void)2056 void ioapic_suspend(void)
2057 {
2058 struct IO_APIC_route_entry *entry = ioapic_pm_state;
2059 unsigned long flags;
2060 int apic, i;
2061
2062 spin_lock_irqsave(&ioapic_lock, flags);
2063 for (apic = 0; apic < nr_ioapics; apic++) {
2064 for (i = 0; i < nr_ioapic_entries[apic]; i ++, entry ++ ) {
2065 *(((int *)entry) + 1) = __io_apic_read(apic, 0x11 + 2 * i);
2066 *(((int *)entry) + 0) = __io_apic_read(apic, 0x10 + 2 * i);
2067 }
2068 }
2069 spin_unlock_irqrestore(&ioapic_lock, flags);
2070 }
2071
ioapic_resume(void)2072 void ioapic_resume(void)
2073 {
2074 struct IO_APIC_route_entry *entry = ioapic_pm_state;
2075 unsigned long flags;
2076 union IO_APIC_reg_00 reg_00;
2077 int i, apic;
2078
2079 spin_lock_irqsave(&ioapic_lock, flags);
2080 for (apic = 0; apic < nr_ioapics; apic++){
2081 if (!nr_ioapic_entries[apic])
2082 continue;
2083 reg_00.raw = __io_apic_read(apic, 0);
2084 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid) {
2085 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
2086 __io_apic_write(apic, 0, reg_00.raw);
2087 }
2088 for (i = 0; i < nr_ioapic_entries[apic]; i++, entry++) {
2089 __io_apic_write(apic, 0x11+2*i, *(((int *)entry)+1));
2090 __io_apic_write(apic, 0x10+2*i, *(((int *)entry)+0));
2091 }
2092 }
2093 spin_unlock_irqrestore(&ioapic_lock, flags);
2094 }
2095
2096 /* --------------------------------------------------------------------------
2097 ACPI-based IOAPIC Configuration
2098 -------------------------------------------------------------------------- */
2099
2100
io_apic_get_unique_id(int ioapic,int apic_id)2101 int __init io_apic_get_unique_id (int ioapic, int apic_id)
2102 {
2103 union IO_APIC_reg_00 reg_00;
2104 static physid_mask_t __initdata apic_id_map = PHYSID_MASK_NONE;
2105 unsigned long flags;
2106 int i = 0;
2107
2108 /*
2109 * The P4 platform supports up to 256 APIC IDs on two separate APIC
2110 * buses (one for LAPICs, one for IOAPICs), where predecessors only
2111 * supports up to 16 on one shared APIC bus.
2112 *
2113 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2114 * advantage of new APIC bus architecture.
2115 */
2116
2117 if (physids_empty(apic_id_map))
2118 ioapic_phys_id_map(&apic_id_map);
2119
2120 spin_lock_irqsave(&ioapic_lock, flags);
2121 reg_00.raw = io_apic_read(ioapic, 0);
2122 spin_unlock_irqrestore(&ioapic_lock, flags);
2123
2124 if (apic_id >= get_physical_broadcast()) {
2125 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2126 "%d\n", ioapic, apic_id, reg_00.bits.ID);
2127 apic_id = reg_00.bits.ID;
2128 }
2129
2130 /*
2131 * Every APIC in a system must have a unique ID or we get lots of nice
2132 * 'stuck on smp_invalidate_needed IPI wait' messages.
2133 */
2134 if (check_apicid_used(&apic_id_map, apic_id)) {
2135
2136 for (i = 0; i < get_physical_broadcast(); i++) {
2137 if (!check_apicid_used(&apic_id_map, i))
2138 break;
2139 }
2140
2141 if (i == get_physical_broadcast())
2142 panic("Max apic_id exceeded\n");
2143
2144 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2145 "trying %d\n", ioapic, apic_id, i);
2146
2147 apic_id = i;
2148 }
2149
2150 set_apicid(apic_id, &apic_id_map);
2151
2152 if (reg_00.bits.ID != apic_id) {
2153 reg_00.bits.ID = apic_id;
2154
2155 spin_lock_irqsave(&ioapic_lock, flags);
2156 io_apic_write(ioapic, 0, reg_00.raw);
2157 reg_00.raw = io_apic_read(ioapic, 0);
2158 spin_unlock_irqrestore(&ioapic_lock, flags);
2159
2160 /* Sanity check */
2161 if (reg_00.bits.ID != apic_id) {
2162 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
2163 return -1;
2164 }
2165 }
2166
2167 apic_printk(APIC_VERBOSE, KERN_INFO
2168 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2169
2170 return apic_id;
2171 }
2172
2173
io_apic_get_version(int ioapic)2174 int __init io_apic_get_version (int ioapic)
2175 {
2176 union IO_APIC_reg_01 reg_01;
2177 unsigned long flags;
2178
2179 spin_lock_irqsave(&ioapic_lock, flags);
2180 reg_01.raw = io_apic_read(ioapic, 1);
2181 spin_unlock_irqrestore(&ioapic_lock, flags);
2182
2183 return reg_01.bits.version;
2184 }
2185
2186
io_apic_get_redir_entries(int ioapic)2187 int __init io_apic_get_redir_entries (int ioapic)
2188 {
2189 union IO_APIC_reg_01 reg_01;
2190 unsigned long flags;
2191
2192 spin_lock_irqsave(&ioapic_lock, flags);
2193 reg_01.raw = io_apic_read(ioapic, 1);
2194 spin_unlock_irqrestore(&ioapic_lock, flags);
2195
2196 return reg_01.bits.entries;
2197 }
2198
2199
io_apic_set_pci_routing(int ioapic,int pin,int irq,int edge_level,int active_high_low)2200 int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
2201 {
2202 struct irq_desc *desc = irq_to_desc(irq);
2203 struct IO_APIC_route_entry entry;
2204 unsigned long flags;
2205 int vector;
2206
2207 if (!IO_APIC_IRQ(irq)) {
2208 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ %d\n",
2209 ioapic, irq);
2210 return -EINVAL;
2211 }
2212
2213 /*
2214 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2215 * Note that we mask (disable) IRQs now -- these get enabled when the
2216 * corresponding device driver registers for this IRQ.
2217 */
2218
2219 memset(&entry,0,sizeof(entry));
2220
2221 entry.delivery_mode = INT_DELIVERY_MODE;
2222 entry.dest_mode = INT_DEST_MODE;
2223 entry.trigger = edge_level;
2224 entry.polarity = active_high_low;
2225 entry.mask = 1;
2226
2227 /*
2228 * IRQs < 16 are already in the irq_2_pin[] map
2229 */
2230 if (!platform_legacy_irq(irq))
2231 add_pin_to_irq(irq, ioapic, pin);
2232
2233 vector = assign_irq_vector(irq, NULL);
2234 if (vector < 0)
2235 return vector;
2236 entry.vector = vector;
2237
2238 if (cpumask_intersects(desc->arch.cpu_mask, TARGET_CPUS)) {
2239 cpumask_t *mask = this_cpu(scratch_cpumask);
2240
2241 cpumask_and(mask, desc->arch.cpu_mask, TARGET_CPUS);
2242 SET_DEST(entry, logical, cpu_mask_to_apicid(mask));
2243 } else {
2244 printk(XENLOG_ERR "IRQ%d: no target CPU (%*pb vs %*pb)\n",
2245 irq, CPUMASK_PR(desc->arch.cpu_mask), CPUMASK_PR(TARGET_CPUS));
2246 desc->status |= IRQ_DISABLED;
2247 }
2248
2249 apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
2250 "(%d-%d -> %#x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
2251 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2252 edge_level, active_high_low);
2253
2254 ioapic_register_intr(irq, edge_level);
2255
2256 if (!ioapic && platform_legacy_irq(irq))
2257 disable_8259A_irq(desc);
2258
2259 spin_lock_irqsave(&ioapic_lock, flags);
2260 __ioapic_write_entry(ioapic, pin, 0, entry);
2261 spin_unlock(&ioapic_lock);
2262
2263 spin_lock(&desc->lock);
2264 if (!(desc->status & (IRQ_DISABLED | IRQ_GUEST)))
2265 desc->handler->startup(desc);
2266 spin_unlock_irqrestore(&desc->lock, flags);
2267
2268 return 0;
2269 }
2270
ioapic_physbase_to_id(unsigned long physbase)2271 static int ioapic_physbase_to_id(unsigned long physbase)
2272 {
2273 int apic;
2274 for ( apic = 0; apic < nr_ioapics; apic++ )
2275 {
2276 if ( !nr_ioapic_entries[apic] )
2277 continue;
2278 if ( mp_ioapics[apic].mpc_apicaddr == physbase )
2279 return apic;
2280 }
2281 return -EINVAL;
2282 }
2283
apic_pin_2_gsi_irq(int apic,int pin)2284 static int apic_pin_2_gsi_irq(int apic, int pin)
2285 {
2286 int idx;
2287
2288 if (apic < 0)
2289 return -EINVAL;
2290
2291 idx = find_irq_entry(apic, pin, mp_INT);
2292
2293 return idx >= 0 ? pin_2_irq(idx, apic, pin)
2294 : io_apic_gsi_base(apic) + pin;
2295 }
2296
ioapic_guest_read(unsigned long physbase,unsigned int reg,u32 * pval)2297 int ioapic_guest_read(unsigned long physbase, unsigned int reg, u32 *pval)
2298 {
2299 int apic;
2300 unsigned long flags;
2301
2302 if ( (apic = ioapic_physbase_to_id(physbase)) < 0 )
2303 return apic;
2304
2305 spin_lock_irqsave(&ioapic_lock, flags);
2306 *pval = io_apic_read(apic, reg);
2307 spin_unlock_irqrestore(&ioapic_lock, flags);
2308
2309 return 0;
2310 }
2311
2312 #define WARN_BOGUS_WRITE(f, a...) \
2313 dprintk(XENLOG_INFO, "IO-APIC: apic=%d, pin=%d, irq=%d\n" \
2314 XENLOG_INFO "IO-APIC: new_entry=%08x\n" \
2315 XENLOG_INFO "IO-APIC: " f "\n", \
2316 apic, pin, irq, *(u32 *)&rte, ##a )
2317
ioapic_guest_write(unsigned long physbase,unsigned int reg,u32 val)2318 int ioapic_guest_write(unsigned long physbase, unsigned int reg, u32 val)
2319 {
2320 int apic, pin, irq, ret, pirq;
2321 struct IO_APIC_route_entry rte = { 0 };
2322 unsigned long flags;
2323 struct irq_desc *desc;
2324
2325 if ( (apic = ioapic_physbase_to_id(physbase)) < 0 )
2326 return apic;
2327
2328 /* Only write to the first half of a route entry. */
2329 if ( (reg < 0x10) || (reg & 1) )
2330 return 0;
2331
2332 pin = (reg - 0x10) >> 1;
2333
2334 /* Write first half from guest; second half is target info. */
2335 *(u32 *)&rte = val;
2336
2337 /*
2338 * What about weird destination types?
2339 * SMI: Ignore? Ought to be set up by the BIOS.
2340 * NMI: Ignore? Watchdog functionality is Xen's concern.
2341 * INIT: Definitely ignore: probably a guest OS bug.
2342 * ExtINT: Ignore? Linux only asserts this at start of day.
2343 * For now, print a message and return an error. We can fix up on demand.
2344 */
2345 if ( rte.delivery_mode > dest_LowestPrio )
2346 {
2347 printk("ERROR: Attempt to write weird IOAPIC destination mode!\n");
2348 printk(" APIC=%d/%d, lo-reg=%x\n", apic, pin, val);
2349 return -EINVAL;
2350 }
2351
2352 /*
2353 * The guest does not know physical APIC arrangement (flat vs. cluster).
2354 * Apply genapic conventions for this platform.
2355 */
2356 rte.delivery_mode = INT_DELIVERY_MODE;
2357 rte.dest_mode = INT_DEST_MODE;
2358
2359 irq = apic_pin_2_gsi_irq(apic, pin);
2360 if ( irq < 0 )
2361 return irq;
2362
2363 desc = irq_to_desc(irq);
2364
2365 /*
2366 * Since PHYSDEVOP_alloc_irq_vector is dummy, rte.vector is the pirq
2367 * which corresponds to this ioapic pin, retrieve it for building
2368 * pirq and irq mapping. Where the GSI is greater than 256, we assume
2369 * that dom0 pirq == irq.
2370 */
2371 if ( !rte.mask )
2372 {
2373 pirq = (irq >= 256) ? irq : rte.vector;
2374 if ( pirq >= hardware_domain->nr_pirqs )
2375 return -EINVAL;
2376 }
2377 else
2378 pirq = -1;
2379
2380 if ( desc->action )
2381 {
2382 spin_lock_irqsave(&ioapic_lock, flags);
2383 ret = io_apic_read(apic, 0x10 + 2 * pin);
2384 spin_unlock_irqrestore(&ioapic_lock, flags);
2385 rte.vector = desc->arch.vector;
2386 if ( *(u32*)&rte != ret )
2387 WARN_BOGUS_WRITE("old_entry=%08x pirq=%d\n" XENLOG_INFO
2388 "IO-APIC: Attempt to modify IO-APIC pin for in-use IRQ!",
2389 ret, pirq);
2390 return 0;
2391 }
2392
2393 if ( desc->arch.vector <= 0 || desc->arch.vector > LAST_DYNAMIC_VECTOR ||
2394 desc->handler->enable == enable_8259A_irq )
2395 {
2396 int vector = desc->arch.vector;
2397
2398 if ( vector < FIRST_HIPRIORITY_VECTOR )
2399 add_pin_to_irq(irq, apic, pin);
2400 else
2401 desc->arch.vector = IRQ_VECTOR_UNASSIGNED;
2402 ret = assign_irq_vector(irq, NULL);
2403 if ( ret < 0 )
2404 {
2405 if ( vector < FIRST_HIPRIORITY_VECTOR )
2406 remove_pin_from_irq(irq, apic, pin);
2407 else
2408 desc->arch.vector = vector;
2409 return ret;
2410 }
2411
2412 printk(XENLOG_INFO "allocated vector %02x for irq %d\n", ret, irq);
2413 }
2414 if ( pirq >= 0 )
2415 {
2416 spin_lock(&hardware_domain->event_lock);
2417 ret = map_domain_pirq(hardware_domain, pirq, irq,
2418 MAP_PIRQ_TYPE_GSI, NULL);
2419 spin_unlock(&hardware_domain->event_lock);
2420 if ( ret < 0 )
2421 return ret;
2422 }
2423
2424 spin_lock_irqsave(&ioapic_lock, flags);
2425 /* Set the correct irq-handling type. */
2426 desc->handler = rte.trigger ?
2427 &ioapic_level_type: &ioapic_edge_type;
2428
2429 /* Mask iff level triggered. */
2430 rte.mask = rte.trigger;
2431 /* Set the vector field to the real vector! */
2432 rte.vector = desc->arch.vector;
2433
2434 if ( cpumask_intersects(desc->arch.cpu_mask, TARGET_CPUS) )
2435 {
2436 cpumask_t *mask = this_cpu(scratch_cpumask);
2437
2438 cpumask_and(mask, desc->arch.cpu_mask, TARGET_CPUS);
2439 SET_DEST(rte, logical, cpu_mask_to_apicid(mask));
2440 }
2441 else
2442 {
2443 gprintk(XENLOG_ERR, "IRQ%d: no target CPU (%*pb vs %*pb)\n",
2444 irq, CPUMASK_PR(desc->arch.cpu_mask), CPUMASK_PR(TARGET_CPUS));
2445 desc->status |= IRQ_DISABLED;
2446 rte.mask = 1;
2447 }
2448
2449 __ioapic_write_entry(apic, pin, 0, rte);
2450
2451 spin_unlock_irqrestore(&ioapic_lock, flags);
2452
2453 return 0;
2454 }
2455
delivery_mode_2_str(const enum ioapic_irq_destination_types mode)2456 static const char * delivery_mode_2_str(
2457 const enum ioapic_irq_destination_types mode)
2458 {
2459 switch ( mode )
2460 {
2461 case dest_Fixed: return "Fixed";
2462 case dest_LowestPrio: return "LoPri";
2463 case dest_SMI: return "SMI";
2464 case dest_NMI: return "NMI";
2465 case dest_INIT: return "INIT";
2466 case dest_ExtINT: return "ExINT";
2467 case dest__reserved_1:
2468 case dest__reserved_2: return "Resvd";
2469 default: return "INVAL";
2470 }
2471 }
2472
dump_ioapic_irq_info(void)2473 void dump_ioapic_irq_info(void)
2474 {
2475 struct irq_pin_list *entry;
2476 struct IO_APIC_route_entry rte;
2477 unsigned int irq, pin, printed = 0;
2478
2479 if ( !irq_2_pin )
2480 return;
2481
2482 for ( irq = 0; irq < nr_irqs_gsi; irq++ )
2483 {
2484 if ( !(irq & 0x1f) )
2485 process_pending_softirqs();
2486
2487 entry = &irq_2_pin[irq];
2488 if ( entry->pin == -1 )
2489 continue;
2490
2491 if ( !printed++ )
2492 printk("IO-APIC interrupt information:\n");
2493
2494 printk(" IRQ%3d Vec%3d:\n", irq, irq_to_vector(irq));
2495
2496 for ( ; ; )
2497 {
2498 pin = entry->pin;
2499
2500 printk(" Apic 0x%02x, Pin %2d: ", entry->apic, pin);
2501
2502 rte = ioapic_read_entry(entry->apic, pin, 0);
2503
2504 printk("vec=%02x delivery=%-5s dest=%c status=%d "
2505 "polarity=%d irr=%d trig=%c mask=%d dest_id:%0*x\n",
2506 rte.vector, delivery_mode_2_str(rte.delivery_mode),
2507 rte.dest_mode ? 'L' : 'P',
2508 rte.delivery_status, rte.polarity, rte.irr,
2509 rte.trigger ? 'L' : 'E', rte.mask,
2510 (x2apic_enabled && iommu_intremap) ? 8 : 2,
2511 (x2apic_enabled && iommu_intremap) ?
2512 rte.dest.dest32 : rte.dest.logical.logical_dest);
2513
2514 if ( entry->next == 0 )
2515 break;
2516 entry = &irq_2_pin[entry->next];
2517 }
2518 }
2519 }
2520
2521 static unsigned int __initdata max_gsi_irqs;
2522 integer_param("max_gsi_irqs", max_gsi_irqs);
2523
bad_ioapic_register(unsigned int idx)2524 static __init bool bad_ioapic_register(unsigned int idx)
2525 {
2526 union IO_APIC_reg_00 reg_00 = { .raw = io_apic_read(idx, 0) };
2527 union IO_APIC_reg_01 reg_01 = { .raw = io_apic_read(idx, 1) };
2528 union IO_APIC_reg_02 reg_02 = { .raw = io_apic_read(idx, 2) };
2529
2530 if ( reg_00.raw == -1 && reg_01.raw == -1 && reg_02.raw == -1 )
2531 {
2532 printk(KERN_WARNING "I/O APIC %#x registers return all ones, skipping!\n",
2533 mp_ioapics[idx].mpc_apicaddr);
2534 return true;
2535 }
2536
2537 return false;
2538 }
2539
ioapic_init_mappings(void)2540 static void __init ioapic_init_mappings(void)
2541 {
2542 unsigned int i, idx = FIX_IO_APIC_BASE_0;
2543
2544 nr_irqs_gsi = 0;
2545
2546 for ( i = 0; i < nr_ioapics; i++, idx++ )
2547 {
2548 union IO_APIC_reg_01 reg_01;
2549 paddr_t ioapic_phys = mp_ioapics[i].mpc_apicaddr;
2550
2551 if ( !ioapic_phys )
2552 {
2553 printk(KERN_ERR
2554 "WARNING: bogus zero IO-APIC address found in MPTABLE, disabling IO/APIC support!\n");
2555 smp_found_config = false;
2556 skip_ioapic_setup = true;
2557 break;
2558 }
2559
2560 set_fixmap_nocache(idx, ioapic_phys);
2561 apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08Lx (%08lx)\n",
2562 __fix_to_virt(idx), ioapic_phys);
2563
2564 if ( bad_ioapic_register(i) )
2565 {
2566 clear_fixmap(idx);
2567 continue;
2568 }
2569
2570 /* The number of IO-APIC IRQ registers (== #pins): */
2571 reg_01.raw = io_apic_read(i, 1);
2572 nr_ioapic_entries[i] = reg_01.bits.entries + 1;
2573 nr_irqs_gsi += nr_ioapic_entries[i];
2574
2575 if ( rangeset_add_singleton(mmio_ro_ranges,
2576 ioapic_phys >> PAGE_SHIFT) )
2577 printk(KERN_ERR "Failed to mark IO-APIC page %lx read-only\n",
2578 ioapic_phys);
2579 }
2580 }
2581
ioapic_init(void)2582 void __init ioapic_init(void)
2583 {
2584 if ( smp_found_config )
2585 ioapic_init_mappings();
2586
2587 nr_irqs_gsi = max(nr_irqs_gsi, highest_gsi() + 1);
2588
2589 if ( max_gsi_irqs == 0 )
2590 max_gsi_irqs = nr_irqs ? nr_irqs / 8 : PAGE_SIZE;
2591 else if ( nr_irqs != 0 && max_gsi_irqs > nr_irqs )
2592 {
2593 printk(XENLOG_WARNING "\"max_gsi_irqs=\" cannot be specified larger"
2594 " than \"nr_irqs=\"\n");
2595 max_gsi_irqs = nr_irqs;
2596 }
2597 if ( max_gsi_irqs < 16 )
2598 max_gsi_irqs = 16;
2599
2600 /* for PHYSDEVOP_pirq_eoi_gmfn guest assumptions */
2601 if ( max_gsi_irqs > PAGE_SIZE * 8 )
2602 max_gsi_irqs = PAGE_SIZE * 8;
2603
2604 if ( !smp_found_config || skip_ioapic_setup || nr_irqs_gsi < 16 )
2605 nr_irqs_gsi = 16;
2606 else if ( nr_irqs_gsi > max_gsi_irqs )
2607 {
2608 printk(XENLOG_WARNING "Limiting to %u GSI IRQs (found %u)\n",
2609 max_gsi_irqs, nr_irqs_gsi);
2610 nr_irqs_gsi = max_gsi_irqs;
2611 }
2612
2613 if ( nr_irqs == 0 )
2614 nr_irqs = cpu_has_apic ?
2615 max(0U + num_present_cpus() * NR_DYNAMIC_VECTORS,
2616 8 * nr_irqs_gsi) :
2617 nr_irqs_gsi;
2618 else if ( nr_irqs < 16 )
2619 nr_irqs = 16;
2620 printk(XENLOG_INFO "IRQ limits: %u GSI, %u MSI/MSI-X\n",
2621 nr_irqs_gsi, nr_irqs - nr_irqs_gsi);
2622 }
2623
arch_hwdom_irqs(domid_t domid)2624 unsigned int arch_hwdom_irqs(domid_t domid)
2625 {
2626 unsigned int n = fls(num_present_cpus());
2627
2628 if ( !domid )
2629 n = min(n, dom0_max_vcpus());
2630 n = min(nr_irqs_gsi + n * NR_DYNAMIC_VECTORS, nr_irqs);
2631
2632 /* Bounded by the domain pirq eoi bitmap gfn. */
2633 n = min_t(unsigned int, n, PAGE_SIZE * BITS_PER_BYTE);
2634
2635 printk("Dom%d has maximum %u PIRQs\n", domid, n);
2636
2637 return n;
2638 }
2639