xref: /linux/arch/x86/kernel/apic/io_apic.c (revision 6084a6e23c971ef703229ee1aec68d01688578d6)
1 /*
2  *	Intel IO-APIC support for multi-Pentium hosts.
3  *
4  *	Copyright (C) 1997, 1998, 1999, 2000, 2009 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 <linux/mm.h>
24 #include <linux/interrupt.h>
25 #include <linux/init.h>
26 #include <linux/delay.h>
27 #include <linux/sched.h>
28 #include <linux/pci.h>
29 #include <linux/mc146818rtc.h>
30 #include <linux/compiler.h>
31 #include <linux/acpi.h>
32 #include <linux/module.h>
33 #include <linux/syscore_ops.h>
34 #include <linux/msi.h>
35 #include <linux/htirq.h>
36 #include <linux/freezer.h>
37 #include <linux/kthread.h>
38 #include <linux/jiffies.h>	/* time_after() */
39 #include <linux/slab.h>
40 #include <linux/bootmem.h>
41 #include <linux/dmar.h>
42 #include <linux/hpet.h>
43 
44 #include <asm/idle.h>
45 #include <asm/io.h>
46 #include <asm/smp.h>
47 #include <asm/cpu.h>
48 #include <asm/desc.h>
49 #include <asm/proto.h>
50 #include <asm/acpi.h>
51 #include <asm/dma.h>
52 #include <asm/timer.h>
53 #include <asm/i8259.h>
54 #include <asm/msidef.h>
55 #include <asm/hypertransport.h>
56 #include <asm/setup.h>
57 #include <asm/irq_remapping.h>
58 #include <asm/hpet.h>
59 #include <asm/hw_irq.h>
60 
61 #include <asm/apic.h>
62 
63 #define __apicdebuginit(type) static type __init
64 
65 #define	for_each_ioapic(idx)		\
66 	for ((idx) = 0; (idx) < nr_ioapics; (idx)++)
67 #define	for_each_ioapic_reverse(idx)	\
68 	for ((idx) = nr_ioapics - 1; (idx) >= 0; (idx)--)
69 #define	for_each_pin(idx, pin)		\
70 	for ((pin) = 0; (pin) < ioapics[(idx)].nr_registers; (pin)++)
71 #define	for_each_ioapic_pin(idx, pin)	\
72 	for_each_ioapic((idx))		\
73 		for_each_pin((idx), (pin))
74 
75 #define for_each_irq_pin(entry, head) \
76 	for (entry = head; entry; entry = entry->next)
77 
78 /*
79  *      Is the SiS APIC rmw bug present ?
80  *      -1 = don't know, 0 = no, 1 = yes
81  */
82 int sis_apic_bug = -1;
83 
84 static DEFINE_RAW_SPINLOCK(ioapic_lock);
85 static DEFINE_RAW_SPINLOCK(vector_lock);
86 
87 static struct ioapic {
88 	/*
89 	 * # of IRQ routing registers
90 	 */
91 	int nr_registers;
92 	/*
93 	 * Saved state during suspend/resume, or while enabling intr-remap.
94 	 */
95 	struct IO_APIC_route_entry *saved_registers;
96 	/* I/O APIC config */
97 	struct mpc_ioapic mp_config;
98 	/* IO APIC gsi routing info */
99 	struct mp_ioapic_gsi  gsi_config;
100 	DECLARE_BITMAP(pin_programmed, MP_MAX_IOAPIC_PIN + 1);
101 } ioapics[MAX_IO_APICS];
102 
103 #define mpc_ioapic_ver(ioapic_idx)	ioapics[ioapic_idx].mp_config.apicver
104 
105 int mpc_ioapic_id(int ioapic_idx)
106 {
107 	return ioapics[ioapic_idx].mp_config.apicid;
108 }
109 
110 unsigned int mpc_ioapic_addr(int ioapic_idx)
111 {
112 	return ioapics[ioapic_idx].mp_config.apicaddr;
113 }
114 
115 struct mp_ioapic_gsi *mp_ioapic_gsi_routing(int ioapic_idx)
116 {
117 	return &ioapics[ioapic_idx].gsi_config;
118 }
119 
120 static inline int mp_ioapic_pin_count(int ioapic)
121 {
122 	struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic);
123 
124 	return gsi_cfg->gsi_end - gsi_cfg->gsi_base + 1;
125 }
126 
127 u32 mp_pin_to_gsi(int ioapic, int pin)
128 {
129 	return mp_ioapic_gsi_routing(ioapic)->gsi_base + pin;
130 }
131 
132 /* Initialize all legacy IRQs and all pins on the first IOAPIC at boot */
133 static inline int mp_init_irq_at_boot(int ioapic, int irq)
134 {
135 	return ioapic == 0 || (irq >= 0 && irq < NR_IRQS_LEGACY);
136 }
137 
138 int nr_ioapics;
139 
140 /* The one past the highest gsi number used */
141 u32 gsi_top;
142 
143 /* MP IRQ source entries */
144 struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES];
145 
146 /* # of MP IRQ source entries */
147 int mp_irq_entries;
148 
149 #ifdef CONFIG_EISA
150 int mp_bus_id_to_type[MAX_MP_BUSSES];
151 #endif
152 
153 DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES);
154 
155 int skip_ioapic_setup;
156 
157 /**
158  * disable_ioapic_support() - disables ioapic support at runtime
159  */
160 void disable_ioapic_support(void)
161 {
162 #ifdef CONFIG_PCI
163 	noioapicquirk = 1;
164 	noioapicreroute = -1;
165 #endif
166 	skip_ioapic_setup = 1;
167 }
168 
169 static int __init parse_noapic(char *str)
170 {
171 	/* disable IO-APIC */
172 	disable_ioapic_support();
173 	return 0;
174 }
175 early_param("noapic", parse_noapic);
176 
177 static int io_apic_setup_irq_pin(unsigned int irq, int node,
178 				 struct io_apic_irq_attr *attr);
179 
180 /* Will be called in mpparse/acpi/sfi codes for saving IRQ info */
181 void mp_save_irq(struct mpc_intsrc *m)
182 {
183 	int i;
184 
185 	apic_printk(APIC_VERBOSE, "Int: type %d, pol %d, trig %d, bus %02x,"
186 		" IRQ %02x, APIC ID %x, APIC INT %02x\n",
187 		m->irqtype, m->irqflag & 3, (m->irqflag >> 2) & 3, m->srcbus,
188 		m->srcbusirq, m->dstapic, m->dstirq);
189 
190 	for (i = 0; i < mp_irq_entries; i++) {
191 		if (!memcmp(&mp_irqs[i], m, sizeof(*m)))
192 			return;
193 	}
194 
195 	memcpy(&mp_irqs[mp_irq_entries], m, sizeof(*m));
196 	if (++mp_irq_entries == MAX_IRQ_SOURCES)
197 		panic("Max # of irq sources exceeded!!\n");
198 }
199 
200 struct irq_pin_list {
201 	int apic, pin;
202 	struct irq_pin_list *next;
203 };
204 
205 static struct irq_pin_list *alloc_irq_pin_list(int node)
206 {
207 	return kzalloc_node(sizeof(struct irq_pin_list), GFP_KERNEL, node);
208 }
209 
210 
211 /* irq_cfg is indexed by the sum of all RTEs in all I/O APICs. */
212 static struct irq_cfg irq_cfgx[NR_IRQS_LEGACY];
213 
214 int __init arch_early_irq_init(void)
215 {
216 	struct irq_cfg *cfg;
217 	int count, node, i;
218 
219 	if (!legacy_pic->nr_legacy_irqs)
220 		io_apic_irqs = ~0UL;
221 
222 	for_each_ioapic(i) {
223 		ioapics[i].saved_registers =
224 			kzalloc(sizeof(struct IO_APIC_route_entry) *
225 				ioapics[i].nr_registers, GFP_KERNEL);
226 		if (!ioapics[i].saved_registers)
227 			pr_err("IOAPIC %d: suspend/resume impossible!\n", i);
228 	}
229 
230 	cfg = irq_cfgx;
231 	count = ARRAY_SIZE(irq_cfgx);
232 	node = cpu_to_node(0);
233 
234 	for (i = 0; i < count; i++) {
235 		irq_set_chip_data(i, &cfg[i]);
236 		zalloc_cpumask_var_node(&cfg[i].domain, GFP_KERNEL, node);
237 		zalloc_cpumask_var_node(&cfg[i].old_domain, GFP_KERNEL, node);
238 		/*
239 		 * For legacy IRQ's, start with assigning irq0 to irq15 to
240 		 * IRQ0_VECTOR to IRQ15_VECTOR for all cpu's.
241 		 */
242 		if (i < legacy_pic->nr_legacy_irqs) {
243 			cfg[i].vector = IRQ0_VECTOR + i;
244 			cpumask_setall(cfg[i].domain);
245 		}
246 	}
247 
248 	return 0;
249 }
250 
251 static inline struct irq_cfg *irq_cfg(unsigned int irq)
252 {
253 	return irq_get_chip_data(irq);
254 }
255 
256 static struct irq_cfg *alloc_irq_cfg(unsigned int irq, int node)
257 {
258 	struct irq_cfg *cfg;
259 
260 	cfg = kzalloc_node(sizeof(*cfg), GFP_KERNEL, node);
261 	if (!cfg)
262 		return NULL;
263 	if (!zalloc_cpumask_var_node(&cfg->domain, GFP_KERNEL, node))
264 		goto out_cfg;
265 	if (!zalloc_cpumask_var_node(&cfg->old_domain, GFP_KERNEL, node))
266 		goto out_domain;
267 	return cfg;
268 out_domain:
269 	free_cpumask_var(cfg->domain);
270 out_cfg:
271 	kfree(cfg);
272 	return NULL;
273 }
274 
275 static void free_irq_cfg(unsigned int at, struct irq_cfg *cfg)
276 {
277 	if (!cfg)
278 		return;
279 	irq_set_chip_data(at, NULL);
280 	free_cpumask_var(cfg->domain);
281 	free_cpumask_var(cfg->old_domain);
282 	kfree(cfg);
283 }
284 
285 static struct irq_cfg *alloc_irq_and_cfg_at(unsigned int at, int node)
286 {
287 	int res = irq_alloc_desc_at(at, node);
288 	struct irq_cfg *cfg;
289 
290 	if (res < 0) {
291 		if (res != -EEXIST)
292 			return NULL;
293 		cfg = irq_cfg(at);
294 		if (cfg)
295 			return cfg;
296 	}
297 
298 	cfg = alloc_irq_cfg(at, node);
299 	if (cfg)
300 		irq_set_chip_data(at, cfg);
301 	else
302 		irq_free_desc(at);
303 	return cfg;
304 }
305 
306 struct io_apic {
307 	unsigned int index;
308 	unsigned int unused[3];
309 	unsigned int data;
310 	unsigned int unused2[11];
311 	unsigned int eoi;
312 };
313 
314 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
315 {
316 	return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
317 		+ (mpc_ioapic_addr(idx) & ~PAGE_MASK);
318 }
319 
320 void io_apic_eoi(unsigned int apic, unsigned int vector)
321 {
322 	struct io_apic __iomem *io_apic = io_apic_base(apic);
323 	writel(vector, &io_apic->eoi);
324 }
325 
326 unsigned int native_io_apic_read(unsigned int apic, unsigned int reg)
327 {
328 	struct io_apic __iomem *io_apic = io_apic_base(apic);
329 	writel(reg, &io_apic->index);
330 	return readl(&io_apic->data);
331 }
332 
333 void native_io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
334 {
335 	struct io_apic __iomem *io_apic = io_apic_base(apic);
336 
337 	writel(reg, &io_apic->index);
338 	writel(value, &io_apic->data);
339 }
340 
341 /*
342  * Re-write a value: to be used for read-modify-write
343  * cycles where the read already set up the index register.
344  *
345  * Older SiS APIC requires we rewrite the index register
346  */
347 void native_io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
348 {
349 	struct io_apic __iomem *io_apic = io_apic_base(apic);
350 
351 	if (sis_apic_bug)
352 		writel(reg, &io_apic->index);
353 	writel(value, &io_apic->data);
354 }
355 
356 union entry_union {
357 	struct { u32 w1, w2; };
358 	struct IO_APIC_route_entry entry;
359 };
360 
361 static struct IO_APIC_route_entry __ioapic_read_entry(int apic, int pin)
362 {
363 	union entry_union eu;
364 
365 	eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
366 	eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
367 
368 	return eu.entry;
369 }
370 
371 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
372 {
373 	union entry_union eu;
374 	unsigned long flags;
375 
376 	raw_spin_lock_irqsave(&ioapic_lock, flags);
377 	eu.entry = __ioapic_read_entry(apic, pin);
378 	raw_spin_unlock_irqrestore(&ioapic_lock, flags);
379 
380 	return eu.entry;
381 }
382 
383 /*
384  * When we write a new IO APIC routing entry, we need to write the high
385  * word first! If the mask bit in the low word is clear, we will enable
386  * the interrupt, and we need to make sure the entry is fully populated
387  * before that happens.
388  */
389 static void __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
390 {
391 	union entry_union eu = {{0, 0}};
392 
393 	eu.entry = e;
394 	io_apic_write(apic, 0x11 + 2*pin, eu.w2);
395 	io_apic_write(apic, 0x10 + 2*pin, eu.w1);
396 }
397 
398 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
399 {
400 	unsigned long flags;
401 
402 	raw_spin_lock_irqsave(&ioapic_lock, flags);
403 	__ioapic_write_entry(apic, pin, e);
404 	raw_spin_unlock_irqrestore(&ioapic_lock, flags);
405 }
406 
407 /*
408  * When we mask an IO APIC routing entry, we need to write the low
409  * word first, in order to set the mask bit before we change the
410  * high bits!
411  */
412 static void ioapic_mask_entry(int apic, int pin)
413 {
414 	unsigned long flags;
415 	union entry_union eu = { .entry.mask = 1 };
416 
417 	raw_spin_lock_irqsave(&ioapic_lock, flags);
418 	io_apic_write(apic, 0x10 + 2*pin, eu.w1);
419 	io_apic_write(apic, 0x11 + 2*pin, eu.w2);
420 	raw_spin_unlock_irqrestore(&ioapic_lock, flags);
421 }
422 
423 /*
424  * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
425  * shared ISA-space IRQs, so we have to support them. We are super
426  * fast in the common case, and fast for shared ISA-space IRQs.
427  */
428 static int __add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
429 {
430 	struct irq_pin_list **last, *entry;
431 
432 	/* don't allow duplicates */
433 	last = &cfg->irq_2_pin;
434 	for_each_irq_pin(entry, cfg->irq_2_pin) {
435 		if (entry->apic == apic && entry->pin == pin)
436 			return 0;
437 		last = &entry->next;
438 	}
439 
440 	entry = alloc_irq_pin_list(node);
441 	if (!entry) {
442 		pr_err("can not alloc irq_pin_list (%d,%d,%d)\n",
443 		       node, apic, pin);
444 		return -ENOMEM;
445 	}
446 	entry->apic = apic;
447 	entry->pin = pin;
448 
449 	*last = entry;
450 	return 0;
451 }
452 
453 static void add_pin_to_irq_node(struct irq_cfg *cfg, int node, int apic, int pin)
454 {
455 	if (__add_pin_to_irq_node(cfg, node, apic, pin))
456 		panic("IO-APIC: failed to add irq-pin. Can not proceed\n");
457 }
458 
459 /*
460  * Reroute an IRQ to a different pin.
461  */
462 static void __init replace_pin_at_irq_node(struct irq_cfg *cfg, int node,
463 					   int oldapic, int oldpin,
464 					   int newapic, int newpin)
465 {
466 	struct irq_pin_list *entry;
467 
468 	for_each_irq_pin(entry, cfg->irq_2_pin) {
469 		if (entry->apic == oldapic && entry->pin == oldpin) {
470 			entry->apic = newapic;
471 			entry->pin = newpin;
472 			/* every one is different, right? */
473 			return;
474 		}
475 	}
476 
477 	/* old apic/pin didn't exist, so just add new ones */
478 	add_pin_to_irq_node(cfg, node, newapic, newpin);
479 }
480 
481 static void __io_apic_modify_irq(struct irq_pin_list *entry,
482 				 int mask_and, int mask_or,
483 				 void (*final)(struct irq_pin_list *entry))
484 {
485 	unsigned int reg, pin;
486 
487 	pin = entry->pin;
488 	reg = io_apic_read(entry->apic, 0x10 + pin * 2);
489 	reg &= mask_and;
490 	reg |= mask_or;
491 	io_apic_modify(entry->apic, 0x10 + pin * 2, reg);
492 	if (final)
493 		final(entry);
494 }
495 
496 static void io_apic_modify_irq(struct irq_cfg *cfg,
497 			       int mask_and, int mask_or,
498 			       void (*final)(struct irq_pin_list *entry))
499 {
500 	struct irq_pin_list *entry;
501 
502 	for_each_irq_pin(entry, cfg->irq_2_pin)
503 		__io_apic_modify_irq(entry, mask_and, mask_or, final);
504 }
505 
506 static void io_apic_sync(struct irq_pin_list *entry)
507 {
508 	/*
509 	 * Synchronize the IO-APIC and the CPU by doing
510 	 * a dummy read from the IO-APIC
511 	 */
512 	struct io_apic __iomem *io_apic;
513 
514 	io_apic = io_apic_base(entry->apic);
515 	readl(&io_apic->data);
516 }
517 
518 static void mask_ioapic(struct irq_cfg *cfg)
519 {
520 	unsigned long flags;
521 
522 	raw_spin_lock_irqsave(&ioapic_lock, flags);
523 	io_apic_modify_irq(cfg, ~0, IO_APIC_REDIR_MASKED, &io_apic_sync);
524 	raw_spin_unlock_irqrestore(&ioapic_lock, flags);
525 }
526 
527 static void mask_ioapic_irq(struct irq_data *data)
528 {
529 	mask_ioapic(data->chip_data);
530 }
531 
532 static void __unmask_ioapic(struct irq_cfg *cfg)
533 {
534 	io_apic_modify_irq(cfg, ~IO_APIC_REDIR_MASKED, 0, NULL);
535 }
536 
537 static void unmask_ioapic(struct irq_cfg *cfg)
538 {
539 	unsigned long flags;
540 
541 	raw_spin_lock_irqsave(&ioapic_lock, flags);
542 	__unmask_ioapic(cfg);
543 	raw_spin_unlock_irqrestore(&ioapic_lock, flags);
544 }
545 
546 static void unmask_ioapic_irq(struct irq_data *data)
547 {
548 	unmask_ioapic(data->chip_data);
549 }
550 
551 /*
552  * IO-APIC versions below 0x20 don't support EOI register.
553  * For the record, here is the information about various versions:
554  *     0Xh     82489DX
555  *     1Xh     I/OAPIC or I/O(x)APIC which are not PCI 2.2 Compliant
556  *     2Xh     I/O(x)APIC which is PCI 2.2 Compliant
557  *     30h-FFh Reserved
558  *
559  * Some of the Intel ICH Specs (ICH2 to ICH5) documents the io-apic
560  * version as 0x2. This is an error with documentation and these ICH chips
561  * use io-apic's of version 0x20.
562  *
563  * For IO-APIC's with EOI register, we use that to do an explicit EOI.
564  * Otherwise, we simulate the EOI message manually by changing the trigger
565  * mode to edge and then back to level, with RTE being masked during this.
566  */
567 void native_eoi_ioapic_pin(int apic, int pin, int vector)
568 {
569 	if (mpc_ioapic_ver(apic) >= 0x20) {
570 		io_apic_eoi(apic, vector);
571 	} else {
572 		struct IO_APIC_route_entry entry, entry1;
573 
574 		entry = entry1 = __ioapic_read_entry(apic, pin);
575 
576 		/*
577 		 * Mask the entry and change the trigger mode to edge.
578 		 */
579 		entry1.mask = 1;
580 		entry1.trigger = IOAPIC_EDGE;
581 
582 		__ioapic_write_entry(apic, pin, entry1);
583 
584 		/*
585 		 * Restore the previous level triggered entry.
586 		 */
587 		__ioapic_write_entry(apic, pin, entry);
588 	}
589 }
590 
591 void eoi_ioapic_irq(unsigned int irq, struct irq_cfg *cfg)
592 {
593 	struct irq_pin_list *entry;
594 	unsigned long flags;
595 
596 	raw_spin_lock_irqsave(&ioapic_lock, flags);
597 	for_each_irq_pin(entry, cfg->irq_2_pin)
598 		x86_io_apic_ops.eoi_ioapic_pin(entry->apic, entry->pin,
599 					       cfg->vector);
600 	raw_spin_unlock_irqrestore(&ioapic_lock, flags);
601 }
602 
603 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
604 {
605 	struct IO_APIC_route_entry entry;
606 
607 	/* Check delivery_mode to be sure we're not clearing an SMI pin */
608 	entry = ioapic_read_entry(apic, pin);
609 	if (entry.delivery_mode == dest_SMI)
610 		return;
611 
612 	/*
613 	 * Make sure the entry is masked and re-read the contents to check
614 	 * if it is a level triggered pin and if the remote-IRR is set.
615 	 */
616 	if (!entry.mask) {
617 		entry.mask = 1;
618 		ioapic_write_entry(apic, pin, entry);
619 		entry = ioapic_read_entry(apic, pin);
620 	}
621 
622 	if (entry.irr) {
623 		unsigned long flags;
624 
625 		/*
626 		 * Make sure the trigger mode is set to level. Explicit EOI
627 		 * doesn't clear the remote-IRR if the trigger mode is not
628 		 * set to level.
629 		 */
630 		if (!entry.trigger) {
631 			entry.trigger = IOAPIC_LEVEL;
632 			ioapic_write_entry(apic, pin, entry);
633 		}
634 
635 		raw_spin_lock_irqsave(&ioapic_lock, flags);
636 		x86_io_apic_ops.eoi_ioapic_pin(apic, pin, entry.vector);
637 		raw_spin_unlock_irqrestore(&ioapic_lock, flags);
638 	}
639 
640 	/*
641 	 * Clear the rest of the bits in the IO-APIC RTE except for the mask
642 	 * bit.
643 	 */
644 	ioapic_mask_entry(apic, pin);
645 	entry = ioapic_read_entry(apic, pin);
646 	if (entry.irr)
647 		pr_err("Unable to reset IRR for apic: %d, pin :%d\n",
648 		       mpc_ioapic_id(apic), pin);
649 }
650 
651 static void clear_IO_APIC (void)
652 {
653 	int apic, pin;
654 
655 	for_each_ioapic_pin(apic, pin)
656 		clear_IO_APIC_pin(apic, pin);
657 }
658 
659 #ifdef CONFIG_X86_32
660 /*
661  * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
662  * specific CPU-side IRQs.
663  */
664 
665 #define MAX_PIRQS 8
666 static int pirq_entries[MAX_PIRQS] = {
667 	[0 ... MAX_PIRQS - 1] = -1
668 };
669 
670 static int __init ioapic_pirq_setup(char *str)
671 {
672 	int i, max;
673 	int ints[MAX_PIRQS+1];
674 
675 	get_options(str, ARRAY_SIZE(ints), ints);
676 
677 	apic_printk(APIC_VERBOSE, KERN_INFO
678 			"PIRQ redirection, working around broken MP-BIOS.\n");
679 	max = MAX_PIRQS;
680 	if (ints[0] < MAX_PIRQS)
681 		max = ints[0];
682 
683 	for (i = 0; i < max; i++) {
684 		apic_printk(APIC_VERBOSE, KERN_DEBUG
685 				"... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
686 		/*
687 		 * PIRQs are mapped upside down, usually.
688 		 */
689 		pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
690 	}
691 	return 1;
692 }
693 
694 __setup("pirq=", ioapic_pirq_setup);
695 #endif /* CONFIG_X86_32 */
696 
697 /*
698  * Saves all the IO-APIC RTE's
699  */
700 int save_ioapic_entries(void)
701 {
702 	int apic, pin;
703 	int err = 0;
704 
705 	for_each_ioapic(apic) {
706 		if (!ioapics[apic].saved_registers) {
707 			err = -ENOMEM;
708 			continue;
709 		}
710 
711 		for_each_pin(apic, pin)
712 			ioapics[apic].saved_registers[pin] =
713 				ioapic_read_entry(apic, pin);
714 	}
715 
716 	return err;
717 }
718 
719 /*
720  * Mask all IO APIC entries.
721  */
722 void mask_ioapic_entries(void)
723 {
724 	int apic, pin;
725 
726 	for_each_ioapic(apic) {
727 		if (!ioapics[apic].saved_registers)
728 			continue;
729 
730 		for_each_pin(apic, pin) {
731 			struct IO_APIC_route_entry entry;
732 
733 			entry = ioapics[apic].saved_registers[pin];
734 			if (!entry.mask) {
735 				entry.mask = 1;
736 				ioapic_write_entry(apic, pin, entry);
737 			}
738 		}
739 	}
740 }
741 
742 /*
743  * Restore IO APIC entries which was saved in the ioapic structure.
744  */
745 int restore_ioapic_entries(void)
746 {
747 	int apic, pin;
748 
749 	for_each_ioapic(apic) {
750 		if (!ioapics[apic].saved_registers)
751 			continue;
752 
753 		for_each_pin(apic, pin)
754 			ioapic_write_entry(apic, pin,
755 					   ioapics[apic].saved_registers[pin]);
756 	}
757 	return 0;
758 }
759 
760 /*
761  * Find the IRQ entry number of a certain pin.
762  */
763 static int find_irq_entry(int ioapic_idx, int pin, int type)
764 {
765 	int i;
766 
767 	for (i = 0; i < mp_irq_entries; i++)
768 		if (mp_irqs[i].irqtype == type &&
769 		    (mp_irqs[i].dstapic == mpc_ioapic_id(ioapic_idx) ||
770 		     mp_irqs[i].dstapic == MP_APIC_ALL) &&
771 		    mp_irqs[i].dstirq == pin)
772 			return i;
773 
774 	return -1;
775 }
776 
777 /*
778  * Find the pin to which IRQ[irq] (ISA) is connected
779  */
780 static int __init find_isa_irq_pin(int irq, int type)
781 {
782 	int i;
783 
784 	for (i = 0; i < mp_irq_entries; i++) {
785 		int lbus = mp_irqs[i].srcbus;
786 
787 		if (test_bit(lbus, mp_bus_not_pci) &&
788 		    (mp_irqs[i].irqtype == type) &&
789 		    (mp_irqs[i].srcbusirq == irq))
790 
791 			return mp_irqs[i].dstirq;
792 	}
793 	return -1;
794 }
795 
796 static int __init find_isa_irq_apic(int irq, int type)
797 {
798 	int i;
799 
800 	for (i = 0; i < mp_irq_entries; i++) {
801 		int lbus = mp_irqs[i].srcbus;
802 
803 		if (test_bit(lbus, mp_bus_not_pci) &&
804 		    (mp_irqs[i].irqtype == type) &&
805 		    (mp_irqs[i].srcbusirq == irq))
806 			break;
807 	}
808 
809 	if (i < mp_irq_entries) {
810 		int ioapic_idx;
811 
812 		for_each_ioapic(ioapic_idx)
813 			if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic)
814 				return ioapic_idx;
815 	}
816 
817 	return -1;
818 }
819 
820 #ifdef CONFIG_EISA
821 /*
822  * EISA Edge/Level control register, ELCR
823  */
824 static int EISA_ELCR(unsigned int irq)
825 {
826 	if (irq < legacy_pic->nr_legacy_irqs) {
827 		unsigned int port = 0x4d0 + (irq >> 3);
828 		return (inb(port) >> (irq & 7)) & 1;
829 	}
830 	apic_printk(APIC_VERBOSE, KERN_INFO
831 			"Broken MPtable reports ISA irq %d\n", irq);
832 	return 0;
833 }
834 
835 #endif
836 
837 /* ISA interrupts are always polarity zero edge triggered,
838  * when listed as conforming in the MP table. */
839 
840 #define default_ISA_trigger(idx)	(0)
841 #define default_ISA_polarity(idx)	(0)
842 
843 /* EISA interrupts are always polarity zero and can be edge or level
844  * trigger depending on the ELCR value.  If an interrupt is listed as
845  * EISA conforming in the MP table, that means its trigger type must
846  * be read in from the ELCR */
847 
848 #define default_EISA_trigger(idx)	(EISA_ELCR(mp_irqs[idx].srcbusirq))
849 #define default_EISA_polarity(idx)	default_ISA_polarity(idx)
850 
851 /* PCI interrupts are always polarity one level triggered,
852  * when listed as conforming in the MP table. */
853 
854 #define default_PCI_trigger(idx)	(1)
855 #define default_PCI_polarity(idx)	(1)
856 
857 static int irq_polarity(int idx)
858 {
859 	int bus = mp_irqs[idx].srcbus;
860 	int polarity;
861 
862 	/*
863 	 * Determine IRQ line polarity (high active or low active):
864 	 */
865 	switch (mp_irqs[idx].irqflag & 3)
866 	{
867 		case 0: /* conforms, ie. bus-type dependent polarity */
868 			if (test_bit(bus, mp_bus_not_pci))
869 				polarity = default_ISA_polarity(idx);
870 			else
871 				polarity = default_PCI_polarity(idx);
872 			break;
873 		case 1: /* high active */
874 		{
875 			polarity = 0;
876 			break;
877 		}
878 		case 2: /* reserved */
879 		{
880 			pr_warn("broken BIOS!!\n");
881 			polarity = 1;
882 			break;
883 		}
884 		case 3: /* low active */
885 		{
886 			polarity = 1;
887 			break;
888 		}
889 		default: /* invalid */
890 		{
891 			pr_warn("broken BIOS!!\n");
892 			polarity = 1;
893 			break;
894 		}
895 	}
896 	return polarity;
897 }
898 
899 static int irq_trigger(int idx)
900 {
901 	int bus = mp_irqs[idx].srcbus;
902 	int trigger;
903 
904 	/*
905 	 * Determine IRQ trigger mode (edge or level sensitive):
906 	 */
907 	switch ((mp_irqs[idx].irqflag>>2) & 3)
908 	{
909 		case 0: /* conforms, ie. bus-type dependent */
910 			if (test_bit(bus, mp_bus_not_pci))
911 				trigger = default_ISA_trigger(idx);
912 			else
913 				trigger = default_PCI_trigger(idx);
914 #ifdef CONFIG_EISA
915 			switch (mp_bus_id_to_type[bus]) {
916 				case MP_BUS_ISA: /* ISA pin */
917 				{
918 					/* set before the switch */
919 					break;
920 				}
921 				case MP_BUS_EISA: /* EISA pin */
922 				{
923 					trigger = default_EISA_trigger(idx);
924 					break;
925 				}
926 				case MP_BUS_PCI: /* PCI pin */
927 				{
928 					/* set before the switch */
929 					break;
930 				}
931 				default:
932 				{
933 					pr_warn("broken BIOS!!\n");
934 					trigger = 1;
935 					break;
936 				}
937 			}
938 #endif
939 			break;
940 		case 1: /* edge */
941 		{
942 			trigger = 0;
943 			break;
944 		}
945 		case 2: /* reserved */
946 		{
947 			pr_warn("broken BIOS!!\n");
948 			trigger = 1;
949 			break;
950 		}
951 		case 3: /* level */
952 		{
953 			trigger = 1;
954 			break;
955 		}
956 		default: /* invalid */
957 		{
958 			pr_warn("broken BIOS!!\n");
959 			trigger = 0;
960 			break;
961 		}
962 	}
963 	return trigger;
964 }
965 
966 static int pin_2_irq(int idx, int apic, int pin)
967 {
968 	int irq;
969 	int bus = mp_irqs[idx].srcbus;
970 	struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(apic);
971 
972 	/*
973 	 * Debugging check, we are in big trouble if this message pops up!
974 	 */
975 	if (mp_irqs[idx].dstirq != pin)
976 		pr_err("broken BIOS or MPTABLE parser, ayiee!!\n");
977 
978 	if (test_bit(bus, mp_bus_not_pci)) {
979 		irq = mp_irqs[idx].srcbusirq;
980 	} else {
981 		u32 gsi = gsi_cfg->gsi_base + pin;
982 
983 		if (gsi >= NR_IRQS_LEGACY)
984 			irq = gsi;
985 		else
986 			irq = gsi_top + gsi;
987 	}
988 
989 #ifdef CONFIG_X86_32
990 	/*
991 	 * PCI IRQ command line redirection. Yes, limits are hardcoded.
992 	 */
993 	if ((pin >= 16) && (pin <= 23)) {
994 		if (pirq_entries[pin-16] != -1) {
995 			if (!pirq_entries[pin-16]) {
996 				apic_printk(APIC_VERBOSE, KERN_DEBUG
997 						"disabling PIRQ%d\n", pin-16);
998 			} else {
999 				irq = pirq_entries[pin-16];
1000 				apic_printk(APIC_VERBOSE, KERN_DEBUG
1001 						"using PIRQ%d -> IRQ %d\n",
1002 						pin-16, irq);
1003 			}
1004 		}
1005 	}
1006 #endif
1007 
1008 	return irq;
1009 }
1010 
1011 /*
1012  * Find a specific PCI IRQ entry.
1013  * Not an __init, possibly needed by modules
1014  */
1015 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin,
1016 				struct io_apic_irq_attr *irq_attr)
1017 {
1018 	int irq, i, best_guess = -1;
1019 
1020 	apic_printk(APIC_DEBUG,
1021 		    "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n",
1022 		    bus, slot, pin);
1023 	if (test_bit(bus, mp_bus_not_pci)) {
1024 		apic_printk(APIC_VERBOSE,
1025 			    "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
1026 		return -1;
1027 	}
1028 
1029 	for (i = 0; i < mp_irq_entries; i++) {
1030 		int lbus = mp_irqs[i].srcbus;
1031 		int ioapic_idx, found = 0;
1032 
1033 		if (bus != lbus || mp_irqs[i].irqtype != mp_INT ||
1034 		    slot != ((mp_irqs[i].srcbusirq >> 2) & 0x1f))
1035 			continue;
1036 
1037 		for_each_ioapic(ioapic_idx)
1038 			if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic ||
1039 			    mp_irqs[i].dstapic == MP_APIC_ALL) {
1040 				found = 1;
1041 				break;
1042 			}
1043 		if (!found)
1044 			continue;
1045 
1046 		/* Skip ISA IRQs */
1047 		irq = pin_2_irq(i, ioapic_idx, mp_irqs[i].dstirq);
1048 		if (ioapic_idx == 0 && !IO_APIC_IRQ(irq))
1049 			continue;
1050 
1051 		if (pin == (mp_irqs[i].srcbusirq & 3)) {
1052 			set_io_apic_irq_attr(irq_attr, ioapic_idx,
1053 					     mp_irqs[i].dstirq,
1054 					     irq_trigger(i),
1055 					     irq_polarity(i));
1056 			return irq;
1057 		}
1058 		/*
1059 		 * Use the first all-but-pin matching entry as a
1060 		 * best-guess fuzzy result for broken mptables.
1061 		 */
1062 		if (best_guess < 0) {
1063 			set_io_apic_irq_attr(irq_attr, ioapic_idx,
1064 					     mp_irqs[i].dstirq,
1065 					     irq_trigger(i),
1066 					     irq_polarity(i));
1067 			best_guess = irq;
1068 		}
1069 	}
1070 	return best_guess;
1071 }
1072 EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
1073 
1074 void lock_vector_lock(void)
1075 {
1076 	/* Used to the online set of cpus does not change
1077 	 * during assign_irq_vector.
1078 	 */
1079 	raw_spin_lock(&vector_lock);
1080 }
1081 
1082 void unlock_vector_lock(void)
1083 {
1084 	raw_spin_unlock(&vector_lock);
1085 }
1086 
1087 static int
1088 __assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
1089 {
1090 	/*
1091 	 * NOTE! The local APIC isn't very good at handling
1092 	 * multiple interrupts at the same interrupt level.
1093 	 * As the interrupt level is determined by taking the
1094 	 * vector number and shifting that right by 4, we
1095 	 * want to spread these out a bit so that they don't
1096 	 * all fall in the same interrupt level.
1097 	 *
1098 	 * Also, we've got to be careful not to trash gate
1099 	 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1100 	 */
1101 	static int current_vector = FIRST_EXTERNAL_VECTOR + VECTOR_OFFSET_START;
1102 	static int current_offset = VECTOR_OFFSET_START % 16;
1103 	int cpu, err;
1104 	cpumask_var_t tmp_mask;
1105 
1106 	if (cfg->move_in_progress)
1107 		return -EBUSY;
1108 
1109 	if (!alloc_cpumask_var(&tmp_mask, GFP_ATOMIC))
1110 		return -ENOMEM;
1111 
1112 	/* Only try and allocate irqs on cpus that are present */
1113 	err = -ENOSPC;
1114 	cpumask_clear(cfg->old_domain);
1115 	cpu = cpumask_first_and(mask, cpu_online_mask);
1116 	while (cpu < nr_cpu_ids) {
1117 		int new_cpu, vector, offset;
1118 
1119 		apic->vector_allocation_domain(cpu, tmp_mask, mask);
1120 
1121 		if (cpumask_subset(tmp_mask, cfg->domain)) {
1122 			err = 0;
1123 			if (cpumask_equal(tmp_mask, cfg->domain))
1124 				break;
1125 			/*
1126 			 * New cpumask using the vector is a proper subset of
1127 			 * the current in use mask. So cleanup the vector
1128 			 * allocation for the members that are not used anymore.
1129 			 */
1130 			cpumask_andnot(cfg->old_domain, cfg->domain, tmp_mask);
1131 			cfg->move_in_progress =
1132 			   cpumask_intersects(cfg->old_domain, cpu_online_mask);
1133 			cpumask_and(cfg->domain, cfg->domain, tmp_mask);
1134 			break;
1135 		}
1136 
1137 		vector = current_vector;
1138 		offset = current_offset;
1139 next:
1140 		vector += 16;
1141 		if (vector >= first_system_vector) {
1142 			offset = (offset + 1) % 16;
1143 			vector = FIRST_EXTERNAL_VECTOR + offset;
1144 		}
1145 
1146 		if (unlikely(current_vector == vector)) {
1147 			cpumask_or(cfg->old_domain, cfg->old_domain, tmp_mask);
1148 			cpumask_andnot(tmp_mask, mask, cfg->old_domain);
1149 			cpu = cpumask_first_and(tmp_mask, cpu_online_mask);
1150 			continue;
1151 		}
1152 
1153 		if (test_bit(vector, used_vectors))
1154 			goto next;
1155 
1156 		for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask) {
1157 			if (per_cpu(vector_irq, new_cpu)[vector] > VECTOR_UNDEFINED)
1158 				goto next;
1159 		}
1160 		/* Found one! */
1161 		current_vector = vector;
1162 		current_offset = offset;
1163 		if (cfg->vector) {
1164 			cpumask_copy(cfg->old_domain, cfg->domain);
1165 			cfg->move_in_progress =
1166 			   cpumask_intersects(cfg->old_domain, cpu_online_mask);
1167 		}
1168 		for_each_cpu_and(new_cpu, tmp_mask, cpu_online_mask)
1169 			per_cpu(vector_irq, new_cpu)[vector] = irq;
1170 		cfg->vector = vector;
1171 		cpumask_copy(cfg->domain, tmp_mask);
1172 		err = 0;
1173 		break;
1174 	}
1175 	free_cpumask_var(tmp_mask);
1176 	return err;
1177 }
1178 
1179 int assign_irq_vector(int irq, struct irq_cfg *cfg, const struct cpumask *mask)
1180 {
1181 	int err;
1182 	unsigned long flags;
1183 
1184 	raw_spin_lock_irqsave(&vector_lock, flags);
1185 	err = __assign_irq_vector(irq, cfg, mask);
1186 	raw_spin_unlock_irqrestore(&vector_lock, flags);
1187 	return err;
1188 }
1189 
1190 static void __clear_irq_vector(int irq, struct irq_cfg *cfg)
1191 {
1192 	int cpu, vector;
1193 
1194 	BUG_ON(!cfg->vector);
1195 
1196 	vector = cfg->vector;
1197 	for_each_cpu_and(cpu, cfg->domain, cpu_online_mask)
1198 		per_cpu(vector_irq, cpu)[vector] = VECTOR_UNDEFINED;
1199 
1200 	cfg->vector = 0;
1201 	cpumask_clear(cfg->domain);
1202 
1203 	if (likely(!cfg->move_in_progress))
1204 		return;
1205 	for_each_cpu_and(cpu, cfg->old_domain, cpu_online_mask) {
1206 		for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
1207 			if (per_cpu(vector_irq, cpu)[vector] != irq)
1208 				continue;
1209 			per_cpu(vector_irq, cpu)[vector] = VECTOR_UNDEFINED;
1210 			break;
1211 		}
1212 	}
1213 	cfg->move_in_progress = 0;
1214 }
1215 
1216 void __setup_vector_irq(int cpu)
1217 {
1218 	/* Initialize vector_irq on a new cpu */
1219 	int irq, vector;
1220 	struct irq_cfg *cfg;
1221 
1222 	/*
1223 	 * vector_lock will make sure that we don't run into irq vector
1224 	 * assignments that might be happening on another cpu in parallel,
1225 	 * while we setup our initial vector to irq mappings.
1226 	 */
1227 	raw_spin_lock(&vector_lock);
1228 	/* Mark the inuse vectors */
1229 	for_each_active_irq(irq) {
1230 		cfg = irq_cfg(irq);
1231 		if (!cfg)
1232 			continue;
1233 
1234 		if (!cpumask_test_cpu(cpu, cfg->domain))
1235 			continue;
1236 		vector = cfg->vector;
1237 		per_cpu(vector_irq, cpu)[vector] = irq;
1238 	}
1239 	/* Mark the free vectors */
1240 	for (vector = 0; vector < NR_VECTORS; ++vector) {
1241 		irq = per_cpu(vector_irq, cpu)[vector];
1242 		if (irq <= VECTOR_UNDEFINED)
1243 			continue;
1244 
1245 		cfg = irq_cfg(irq);
1246 		if (!cpumask_test_cpu(cpu, cfg->domain))
1247 			per_cpu(vector_irq, cpu)[vector] = VECTOR_UNDEFINED;
1248 	}
1249 	raw_spin_unlock(&vector_lock);
1250 }
1251 
1252 static struct irq_chip ioapic_chip;
1253 
1254 #ifdef CONFIG_X86_32
1255 static inline int IO_APIC_irq_trigger(int irq)
1256 {
1257 	int apic, idx, pin;
1258 
1259 	for_each_ioapic_pin(apic, pin) {
1260 		idx = find_irq_entry(apic, pin, mp_INT);
1261 		if ((idx != -1) && (irq == pin_2_irq(idx, apic, pin)))
1262 			return irq_trigger(idx);
1263 	}
1264 	/*
1265          * nonexistent IRQs are edge default
1266          */
1267 	return 0;
1268 }
1269 #else
1270 static inline int IO_APIC_irq_trigger(int irq)
1271 {
1272 	return 1;
1273 }
1274 #endif
1275 
1276 static void ioapic_register_intr(unsigned int irq, struct irq_cfg *cfg,
1277 				 unsigned long trigger)
1278 {
1279 	struct irq_chip *chip = &ioapic_chip;
1280 	irq_flow_handler_t hdl;
1281 	bool fasteoi;
1282 
1283 	if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
1284 	    trigger == IOAPIC_LEVEL) {
1285 		irq_set_status_flags(irq, IRQ_LEVEL);
1286 		fasteoi = true;
1287 	} else {
1288 		irq_clear_status_flags(irq, IRQ_LEVEL);
1289 		fasteoi = false;
1290 	}
1291 
1292 	if (setup_remapped_irq(irq, cfg, chip))
1293 		fasteoi = trigger != 0;
1294 
1295 	hdl = fasteoi ? handle_fasteoi_irq : handle_edge_irq;
1296 	irq_set_chip_and_handler_name(irq, chip, hdl,
1297 				      fasteoi ? "fasteoi" : "edge");
1298 }
1299 
1300 int native_setup_ioapic_entry(int irq, struct IO_APIC_route_entry *entry,
1301 			      unsigned int destination, int vector,
1302 			      struct io_apic_irq_attr *attr)
1303 {
1304 	memset(entry, 0, sizeof(*entry));
1305 
1306 	entry->delivery_mode = apic->irq_delivery_mode;
1307 	entry->dest_mode     = apic->irq_dest_mode;
1308 	entry->dest	     = destination;
1309 	entry->vector	     = vector;
1310 	entry->mask	     = 0;			/* enable IRQ */
1311 	entry->trigger	     = attr->trigger;
1312 	entry->polarity	     = attr->polarity;
1313 
1314 	/*
1315 	 * Mask level triggered irqs.
1316 	 * Use IRQ_DELAYED_DISABLE for edge triggered irqs.
1317 	 */
1318 	if (attr->trigger)
1319 		entry->mask = 1;
1320 
1321 	return 0;
1322 }
1323 
1324 static void setup_ioapic_irq(unsigned int irq, struct irq_cfg *cfg,
1325 				struct io_apic_irq_attr *attr)
1326 {
1327 	struct IO_APIC_route_entry entry;
1328 	unsigned int dest;
1329 
1330 	if (!IO_APIC_IRQ(irq))
1331 		return;
1332 
1333 	if (assign_irq_vector(irq, cfg, apic->target_cpus()))
1334 		return;
1335 
1336 	if (apic->cpu_mask_to_apicid_and(cfg->domain, apic->target_cpus(),
1337 					 &dest)) {
1338 		pr_warn("Failed to obtain apicid for ioapic %d, pin %d\n",
1339 			mpc_ioapic_id(attr->ioapic), attr->ioapic_pin);
1340 		__clear_irq_vector(irq, cfg);
1341 
1342 		return;
1343 	}
1344 
1345 	apic_printk(APIC_VERBOSE,KERN_DEBUG
1346 		    "IOAPIC[%d]: Set routing entry (%d-%d -> 0x%x -> "
1347 		    "IRQ %d Mode:%i Active:%i Dest:%d)\n",
1348 		    attr->ioapic, mpc_ioapic_id(attr->ioapic), attr->ioapic_pin,
1349 		    cfg->vector, irq, attr->trigger, attr->polarity, dest);
1350 
1351 	if (x86_io_apic_ops.setup_entry(irq, &entry, dest, cfg->vector, attr)) {
1352 		pr_warn("Failed to setup ioapic entry for ioapic  %d, pin %d\n",
1353 			mpc_ioapic_id(attr->ioapic), attr->ioapic_pin);
1354 		__clear_irq_vector(irq, cfg);
1355 
1356 		return;
1357 	}
1358 
1359 	ioapic_register_intr(irq, cfg, attr->trigger);
1360 	if (irq < legacy_pic->nr_legacy_irqs)
1361 		legacy_pic->mask(irq);
1362 
1363 	ioapic_write_entry(attr->ioapic, attr->ioapic_pin, entry);
1364 }
1365 
1366 static bool __init io_apic_pin_not_connected(int idx, int ioapic_idx, int pin)
1367 {
1368 	if (idx != -1)
1369 		return false;
1370 
1371 	apic_printk(APIC_VERBOSE, KERN_DEBUG " apic %d pin %d not connected\n",
1372 		    mpc_ioapic_id(ioapic_idx), pin);
1373 	return true;
1374 }
1375 
1376 static void __init __io_apic_setup_irqs(unsigned int ioapic_idx)
1377 {
1378 	int idx, node = cpu_to_node(0);
1379 	struct io_apic_irq_attr attr;
1380 	unsigned int pin, irq;
1381 
1382 	for_each_pin(ioapic_idx, pin) {
1383 		idx = find_irq_entry(ioapic_idx, pin, mp_INT);
1384 		if (io_apic_pin_not_connected(idx, ioapic_idx, pin))
1385 			continue;
1386 
1387 		irq = pin_2_irq(idx, ioapic_idx, pin);
1388 		if (!mp_init_irq_at_boot(ioapic_idx, irq))
1389 			continue;
1390 
1391 		/*
1392 		 * Skip the timer IRQ if there's a quirk handler
1393 		 * installed and if it returns 1:
1394 		 */
1395 		if (apic->multi_timer_check &&
1396 		    apic->multi_timer_check(ioapic_idx, irq))
1397 			continue;
1398 
1399 		set_io_apic_irq_attr(&attr, ioapic_idx, pin, irq_trigger(idx),
1400 				     irq_polarity(idx));
1401 
1402 		io_apic_setup_irq_pin(irq, node, &attr);
1403 	}
1404 }
1405 
1406 static void __init setup_IO_APIC_irqs(void)
1407 {
1408 	unsigned int ioapic_idx;
1409 
1410 	apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1411 
1412 	for_each_ioapic(ioapic_idx)
1413 		__io_apic_setup_irqs(ioapic_idx);
1414 }
1415 
1416 /*
1417  * for the gsi that is not in first ioapic
1418  * but could not use acpi_register_gsi()
1419  * like some special sci in IBM x3330
1420  */
1421 void setup_IO_APIC_irq_extra(u32 gsi)
1422 {
1423 	int ioapic_idx = 0, pin, idx, irq, node = cpu_to_node(0);
1424 	struct io_apic_irq_attr attr;
1425 
1426 	/*
1427 	 * Convert 'gsi' to 'ioapic.pin'.
1428 	 */
1429 	ioapic_idx = mp_find_ioapic(gsi);
1430 	if (ioapic_idx < 0)
1431 		return;
1432 
1433 	pin = mp_find_ioapic_pin(ioapic_idx, gsi);
1434 	idx = find_irq_entry(ioapic_idx, pin, mp_INT);
1435 	if (idx == -1)
1436 		return;
1437 
1438 	irq = pin_2_irq(idx, ioapic_idx, pin);
1439 	if (mp_init_irq_at_boot(ioapic_idx, irq))
1440 		return;
1441 
1442 	set_io_apic_irq_attr(&attr, ioapic_idx, pin, irq_trigger(idx),
1443 			     irq_polarity(idx));
1444 
1445 	io_apic_setup_irq_pin_once(irq, node, &attr);
1446 }
1447 
1448 /*
1449  * Set up the timer pin, possibly with the 8259A-master behind.
1450  */
1451 static void __init setup_timer_IRQ0_pin(unsigned int ioapic_idx,
1452 					unsigned int pin, int vector)
1453 {
1454 	struct IO_APIC_route_entry entry;
1455 	unsigned int dest;
1456 
1457 	memset(&entry, 0, sizeof(entry));
1458 
1459 	/*
1460 	 * We use logical delivery to get the timer IRQ
1461 	 * to the first CPU.
1462 	 */
1463 	if (unlikely(apic->cpu_mask_to_apicid_and(apic->target_cpus(),
1464 						  apic->target_cpus(), &dest)))
1465 		dest = BAD_APICID;
1466 
1467 	entry.dest_mode = apic->irq_dest_mode;
1468 	entry.mask = 0;			/* don't mask IRQ for edge */
1469 	entry.dest = dest;
1470 	entry.delivery_mode = apic->irq_delivery_mode;
1471 	entry.polarity = 0;
1472 	entry.trigger = 0;
1473 	entry.vector = vector;
1474 
1475 	/*
1476 	 * The timer IRQ doesn't have to know that behind the
1477 	 * scene we may have a 8259A-master in AEOI mode ...
1478 	 */
1479 	irq_set_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq,
1480 				      "edge");
1481 
1482 	/*
1483 	 * Add it to the IO-APIC irq-routing table:
1484 	 */
1485 	ioapic_write_entry(ioapic_idx, pin, entry);
1486 }
1487 
1488 void native_io_apic_print_entries(unsigned int apic, unsigned int nr_entries)
1489 {
1490 	int i;
1491 
1492 	pr_debug(" NR Dst Mask Trig IRR Pol Stat Dmod Deli Vect:\n");
1493 
1494 	for (i = 0; i <= nr_entries; i++) {
1495 		struct IO_APIC_route_entry entry;
1496 
1497 		entry = ioapic_read_entry(apic, i);
1498 
1499 		pr_debug(" %02x %02X  ", i, entry.dest);
1500 		pr_cont("%1d    %1d    %1d   %1d   %1d    "
1501 			"%1d    %1d    %02X\n",
1502 			entry.mask,
1503 			entry.trigger,
1504 			entry.irr,
1505 			entry.polarity,
1506 			entry.delivery_status,
1507 			entry.dest_mode,
1508 			entry.delivery_mode,
1509 			entry.vector);
1510 	}
1511 }
1512 
1513 void intel_ir_io_apic_print_entries(unsigned int apic,
1514 				    unsigned int nr_entries)
1515 {
1516 	int i;
1517 
1518 	pr_debug(" NR Indx Fmt Mask Trig IRR Pol Stat Indx2 Zero Vect:\n");
1519 
1520 	for (i = 0; i <= nr_entries; i++) {
1521 		struct IR_IO_APIC_route_entry *ir_entry;
1522 		struct IO_APIC_route_entry entry;
1523 
1524 		entry = ioapic_read_entry(apic, i);
1525 
1526 		ir_entry = (struct IR_IO_APIC_route_entry *)&entry;
1527 
1528 		pr_debug(" %02x %04X ", i, ir_entry->index);
1529 		pr_cont("%1d   %1d    %1d    %1d   %1d   "
1530 			"%1d    %1d     %X    %02X\n",
1531 			ir_entry->format,
1532 			ir_entry->mask,
1533 			ir_entry->trigger,
1534 			ir_entry->irr,
1535 			ir_entry->polarity,
1536 			ir_entry->delivery_status,
1537 			ir_entry->index2,
1538 			ir_entry->zero,
1539 			ir_entry->vector);
1540 	}
1541 }
1542 
1543 void ioapic_zap_locks(void)
1544 {
1545 	raw_spin_lock_init(&ioapic_lock);
1546 }
1547 
1548 __apicdebuginit(void) print_IO_APIC(int ioapic_idx)
1549 {
1550 	union IO_APIC_reg_00 reg_00;
1551 	union IO_APIC_reg_01 reg_01;
1552 	union IO_APIC_reg_02 reg_02;
1553 	union IO_APIC_reg_03 reg_03;
1554 	unsigned long flags;
1555 
1556 	raw_spin_lock_irqsave(&ioapic_lock, flags);
1557 	reg_00.raw = io_apic_read(ioapic_idx, 0);
1558 	reg_01.raw = io_apic_read(ioapic_idx, 1);
1559 	if (reg_01.bits.version >= 0x10)
1560 		reg_02.raw = io_apic_read(ioapic_idx, 2);
1561 	if (reg_01.bits.version >= 0x20)
1562 		reg_03.raw = io_apic_read(ioapic_idx, 3);
1563 	raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1564 
1565 	printk(KERN_DEBUG "IO APIC #%d......\n", mpc_ioapic_id(ioapic_idx));
1566 	printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1567 	printk(KERN_DEBUG ".......    : physical APIC id: %02X\n", reg_00.bits.ID);
1568 	printk(KERN_DEBUG ".......    : Delivery Type: %X\n", reg_00.bits.delivery_type);
1569 	printk(KERN_DEBUG ".......    : LTS          : %X\n", reg_00.bits.LTS);
1570 
1571 	printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)&reg_01);
1572 	printk(KERN_DEBUG ".......     : max redirection entries: %02X\n",
1573 		reg_01.bits.entries);
1574 
1575 	printk(KERN_DEBUG ".......     : PRQ implemented: %X\n", reg_01.bits.PRQ);
1576 	printk(KERN_DEBUG ".......     : IO APIC version: %02X\n",
1577 		reg_01.bits.version);
1578 
1579 	/*
1580 	 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1581 	 * but the value of reg_02 is read as the previous read register
1582 	 * value, so ignore it if reg_02 == reg_01.
1583 	 */
1584 	if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1585 		printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1586 		printk(KERN_DEBUG ".......     : arbitration: %02X\n", reg_02.bits.arbitration);
1587 	}
1588 
1589 	/*
1590 	 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1591 	 * or reg_03, but the value of reg_0[23] is read as the previous read
1592 	 * register value, so ignore it if reg_03 == reg_0[12].
1593 	 */
1594 	if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1595 	    reg_03.raw != reg_01.raw) {
1596 		printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1597 		printk(KERN_DEBUG ".......     : Boot DT    : %X\n", reg_03.bits.boot_DT);
1598 	}
1599 
1600 	printk(KERN_DEBUG ".... IRQ redirection table:\n");
1601 
1602 	x86_io_apic_ops.print_entries(ioapic_idx, reg_01.bits.entries);
1603 }
1604 
1605 __apicdebuginit(void) print_IO_APICs(void)
1606 {
1607 	int ioapic_idx;
1608 	struct irq_cfg *cfg;
1609 	unsigned int irq;
1610 	struct irq_chip *chip;
1611 
1612 	printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1613 	for_each_ioapic(ioapic_idx)
1614 		printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1615 		       mpc_ioapic_id(ioapic_idx),
1616 		       ioapics[ioapic_idx].nr_registers);
1617 
1618 	/*
1619 	 * We are a bit conservative about what we expect.  We have to
1620 	 * know about every hardware change ASAP.
1621 	 */
1622 	printk(KERN_INFO "testing the IO APIC.......................\n");
1623 
1624 	for_each_ioapic(ioapic_idx)
1625 		print_IO_APIC(ioapic_idx);
1626 
1627 	printk(KERN_DEBUG "IRQ to pin mappings:\n");
1628 	for_each_active_irq(irq) {
1629 		struct irq_pin_list *entry;
1630 
1631 		chip = irq_get_chip(irq);
1632 		if (chip != &ioapic_chip)
1633 			continue;
1634 
1635 		cfg = irq_cfg(irq);
1636 		if (!cfg)
1637 			continue;
1638 		entry = cfg->irq_2_pin;
1639 		if (!entry)
1640 			continue;
1641 		printk(KERN_DEBUG "IRQ%d ", irq);
1642 		for_each_irq_pin(entry, cfg->irq_2_pin)
1643 			pr_cont("-> %d:%d", entry->apic, entry->pin);
1644 		pr_cont("\n");
1645 	}
1646 
1647 	printk(KERN_INFO ".................................... done.\n");
1648 }
1649 
1650 __apicdebuginit(void) print_APIC_field(int base)
1651 {
1652 	int i;
1653 
1654 	printk(KERN_DEBUG);
1655 
1656 	for (i = 0; i < 8; i++)
1657 		pr_cont("%08x", apic_read(base + i*0x10));
1658 
1659 	pr_cont("\n");
1660 }
1661 
1662 __apicdebuginit(void) print_local_APIC(void *dummy)
1663 {
1664 	unsigned int i, v, ver, maxlvt;
1665 	u64 icr;
1666 
1667 	printk(KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1668 		smp_processor_id(), hard_smp_processor_id());
1669 	v = apic_read(APIC_ID);
1670 	printk(KERN_INFO "... APIC ID:      %08x (%01x)\n", v, read_apic_id());
1671 	v = apic_read(APIC_LVR);
1672 	printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1673 	ver = GET_APIC_VERSION(v);
1674 	maxlvt = lapic_get_maxlvt();
1675 
1676 	v = apic_read(APIC_TASKPRI);
1677 	printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1678 
1679 	if (APIC_INTEGRATED(ver)) {                     /* !82489DX */
1680 		if (!APIC_XAPIC(ver)) {
1681 			v = apic_read(APIC_ARBPRI);
1682 			printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1683 			       v & APIC_ARBPRI_MASK);
1684 		}
1685 		v = apic_read(APIC_PROCPRI);
1686 		printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1687 	}
1688 
1689 	/*
1690 	 * Remote read supported only in the 82489DX and local APIC for
1691 	 * Pentium processors.
1692 	 */
1693 	if (!APIC_INTEGRATED(ver) || maxlvt == 3) {
1694 		v = apic_read(APIC_RRR);
1695 		printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1696 	}
1697 
1698 	v = apic_read(APIC_LDR);
1699 	printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1700 	if (!x2apic_enabled()) {
1701 		v = apic_read(APIC_DFR);
1702 		printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1703 	}
1704 	v = apic_read(APIC_SPIV);
1705 	printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1706 
1707 	printk(KERN_DEBUG "... APIC ISR field:\n");
1708 	print_APIC_field(APIC_ISR);
1709 	printk(KERN_DEBUG "... APIC TMR field:\n");
1710 	print_APIC_field(APIC_TMR);
1711 	printk(KERN_DEBUG "... APIC IRR field:\n");
1712 	print_APIC_field(APIC_IRR);
1713 
1714 	if (APIC_INTEGRATED(ver)) {             /* !82489DX */
1715 		if (maxlvt > 3)         /* Due to the Pentium erratum 3AP. */
1716 			apic_write(APIC_ESR, 0);
1717 
1718 		v = apic_read(APIC_ESR);
1719 		printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1720 	}
1721 
1722 	icr = apic_icr_read();
1723 	printk(KERN_DEBUG "... APIC ICR: %08x\n", (u32)icr);
1724 	printk(KERN_DEBUG "... APIC ICR2: %08x\n", (u32)(icr >> 32));
1725 
1726 	v = apic_read(APIC_LVTT);
1727 	printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1728 
1729 	if (maxlvt > 3) {                       /* PC is LVT#4. */
1730 		v = apic_read(APIC_LVTPC);
1731 		printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1732 	}
1733 	v = apic_read(APIC_LVT0);
1734 	printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1735 	v = apic_read(APIC_LVT1);
1736 	printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1737 
1738 	if (maxlvt > 2) {			/* ERR is LVT#3. */
1739 		v = apic_read(APIC_LVTERR);
1740 		printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1741 	}
1742 
1743 	v = apic_read(APIC_TMICT);
1744 	printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1745 	v = apic_read(APIC_TMCCT);
1746 	printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1747 	v = apic_read(APIC_TDCR);
1748 	printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1749 
1750 	if (boot_cpu_has(X86_FEATURE_EXTAPIC)) {
1751 		v = apic_read(APIC_EFEAT);
1752 		maxlvt = (v >> 16) & 0xff;
1753 		printk(KERN_DEBUG "... APIC EFEAT: %08x\n", v);
1754 		v = apic_read(APIC_ECTRL);
1755 		printk(KERN_DEBUG "... APIC ECTRL: %08x\n", v);
1756 		for (i = 0; i < maxlvt; i++) {
1757 			v = apic_read(APIC_EILVTn(i));
1758 			printk(KERN_DEBUG "... APIC EILVT%d: %08x\n", i, v);
1759 		}
1760 	}
1761 	pr_cont("\n");
1762 }
1763 
1764 __apicdebuginit(void) print_local_APICs(int maxcpu)
1765 {
1766 	int cpu;
1767 
1768 	if (!maxcpu)
1769 		return;
1770 
1771 	preempt_disable();
1772 	for_each_online_cpu(cpu) {
1773 		if (cpu >= maxcpu)
1774 			break;
1775 		smp_call_function_single(cpu, print_local_APIC, NULL, 1);
1776 	}
1777 	preempt_enable();
1778 }
1779 
1780 __apicdebuginit(void) print_PIC(void)
1781 {
1782 	unsigned int v;
1783 	unsigned long flags;
1784 
1785 	if (!legacy_pic->nr_legacy_irqs)
1786 		return;
1787 
1788 	printk(KERN_DEBUG "\nprinting PIC contents\n");
1789 
1790 	raw_spin_lock_irqsave(&i8259A_lock, flags);
1791 
1792 	v = inb(0xa1) << 8 | inb(0x21);
1793 	printk(KERN_DEBUG "... PIC  IMR: %04x\n", v);
1794 
1795 	v = inb(0xa0) << 8 | inb(0x20);
1796 	printk(KERN_DEBUG "... PIC  IRR: %04x\n", v);
1797 
1798 	outb(0x0b,0xa0);
1799 	outb(0x0b,0x20);
1800 	v = inb(0xa0) << 8 | inb(0x20);
1801 	outb(0x0a,0xa0);
1802 	outb(0x0a,0x20);
1803 
1804 	raw_spin_unlock_irqrestore(&i8259A_lock, flags);
1805 
1806 	printk(KERN_DEBUG "... PIC  ISR: %04x\n", v);
1807 
1808 	v = inb(0x4d1) << 8 | inb(0x4d0);
1809 	printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1810 }
1811 
1812 static int __initdata show_lapic = 1;
1813 static __init int setup_show_lapic(char *arg)
1814 {
1815 	int num = -1;
1816 
1817 	if (strcmp(arg, "all") == 0) {
1818 		show_lapic = CONFIG_NR_CPUS;
1819 	} else {
1820 		get_option(&arg, &num);
1821 		if (num >= 0)
1822 			show_lapic = num;
1823 	}
1824 
1825 	return 1;
1826 }
1827 __setup("show_lapic=", setup_show_lapic);
1828 
1829 __apicdebuginit(int) print_ICs(void)
1830 {
1831 	if (apic_verbosity == APIC_QUIET)
1832 		return 0;
1833 
1834 	print_PIC();
1835 
1836 	/* don't print out if apic is not there */
1837 	if (!cpu_has_apic && !apic_from_smp_config())
1838 		return 0;
1839 
1840 	print_local_APICs(show_lapic);
1841 	print_IO_APICs();
1842 
1843 	return 0;
1844 }
1845 
1846 late_initcall(print_ICs);
1847 
1848 
1849 /* Where if anywhere is the i8259 connect in external int mode */
1850 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
1851 
1852 void __init enable_IO_APIC(void)
1853 {
1854 	int i8259_apic, i8259_pin;
1855 	int apic, pin;
1856 
1857 	if (!legacy_pic->nr_legacy_irqs)
1858 		return;
1859 
1860 	for_each_ioapic_pin(apic, pin) {
1861 		/* See if any of the pins is in ExtINT mode */
1862 		struct IO_APIC_route_entry entry = ioapic_read_entry(apic, pin);
1863 
1864 		/* If the interrupt line is enabled and in ExtInt mode
1865 		 * I have found the pin where the i8259 is connected.
1866 		 */
1867 		if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1868 			ioapic_i8259.apic = apic;
1869 			ioapic_i8259.pin  = pin;
1870 			goto found_i8259;
1871 		}
1872 	}
1873  found_i8259:
1874 	/* Look to see what if the MP table has reported the ExtINT */
1875 	/* If we could not find the appropriate pin by looking at the ioapic
1876 	 * the i8259 probably is not connected the ioapic but give the
1877 	 * mptable a chance anyway.
1878 	 */
1879 	i8259_pin  = find_isa_irq_pin(0, mp_ExtINT);
1880 	i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1881 	/* Trust the MP table if nothing is setup in the hardware */
1882 	if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1883 		printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1884 		ioapic_i8259.pin  = i8259_pin;
1885 		ioapic_i8259.apic = i8259_apic;
1886 	}
1887 	/* Complain if the MP table and the hardware disagree */
1888 	if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1889 		(i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1890 	{
1891 		printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
1892 	}
1893 
1894 	/*
1895 	 * Do not trust the IO-APIC being empty at bootup
1896 	 */
1897 	clear_IO_APIC();
1898 }
1899 
1900 void native_disable_io_apic(void)
1901 {
1902 	/*
1903 	 * If the i8259 is routed through an IOAPIC
1904 	 * Put that IOAPIC in virtual wire mode
1905 	 * so legacy interrupts can be delivered.
1906 	 */
1907 	if (ioapic_i8259.pin != -1) {
1908 		struct IO_APIC_route_entry entry;
1909 
1910 		memset(&entry, 0, sizeof(entry));
1911 		entry.mask            = 0; /* Enabled */
1912 		entry.trigger         = 0; /* Edge */
1913 		entry.irr             = 0;
1914 		entry.polarity        = 0; /* High */
1915 		entry.delivery_status = 0;
1916 		entry.dest_mode       = 0; /* Physical */
1917 		entry.delivery_mode   = dest_ExtINT; /* ExtInt */
1918 		entry.vector          = 0;
1919 		entry.dest            = read_apic_id();
1920 
1921 		/*
1922 		 * Add it to the IO-APIC irq-routing table:
1923 		 */
1924 		ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
1925 	}
1926 
1927 	if (cpu_has_apic || apic_from_smp_config())
1928 		disconnect_bsp_APIC(ioapic_i8259.pin != -1);
1929 
1930 }
1931 
1932 /*
1933  * Not an __init, needed by the reboot code
1934  */
1935 void disable_IO_APIC(void)
1936 {
1937 	/*
1938 	 * Clear the IO-APIC before rebooting:
1939 	 */
1940 	clear_IO_APIC();
1941 
1942 	if (!legacy_pic->nr_legacy_irqs)
1943 		return;
1944 
1945 	x86_io_apic_ops.disable();
1946 }
1947 
1948 #ifdef CONFIG_X86_32
1949 /*
1950  * function to set the IO-APIC physical IDs based on the
1951  * values stored in the MPC table.
1952  *
1953  * by Matt Domsch <Matt_Domsch@dell.com>  Tue Dec 21 12:25:05 CST 1999
1954  */
1955 void __init setup_ioapic_ids_from_mpc_nocheck(void)
1956 {
1957 	union IO_APIC_reg_00 reg_00;
1958 	physid_mask_t phys_id_present_map;
1959 	int ioapic_idx;
1960 	int i;
1961 	unsigned char old_id;
1962 	unsigned long flags;
1963 
1964 	/*
1965 	 * This is broken; anything with a real cpu count has to
1966 	 * circumvent this idiocy regardless.
1967 	 */
1968 	apic->ioapic_phys_id_map(&phys_cpu_present_map, &phys_id_present_map);
1969 
1970 	/*
1971 	 * Set the IOAPIC ID to the value stored in the MPC table.
1972 	 */
1973 	for_each_ioapic(ioapic_idx) {
1974 		/* Read the register 0 value */
1975 		raw_spin_lock_irqsave(&ioapic_lock, flags);
1976 		reg_00.raw = io_apic_read(ioapic_idx, 0);
1977 		raw_spin_unlock_irqrestore(&ioapic_lock, flags);
1978 
1979 		old_id = mpc_ioapic_id(ioapic_idx);
1980 
1981 		if (mpc_ioapic_id(ioapic_idx) >= get_physical_broadcast()) {
1982 			printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1983 				ioapic_idx, mpc_ioapic_id(ioapic_idx));
1984 			printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1985 				reg_00.bits.ID);
1986 			ioapics[ioapic_idx].mp_config.apicid = reg_00.bits.ID;
1987 		}
1988 
1989 		/*
1990 		 * Sanity check, is the ID really free? Every APIC in a
1991 		 * system must have a unique ID or we get lots of nice
1992 		 * 'stuck on smp_invalidate_needed IPI wait' messages.
1993 		 */
1994 		if (apic->check_apicid_used(&phys_id_present_map,
1995 					    mpc_ioapic_id(ioapic_idx))) {
1996 			printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1997 				ioapic_idx, mpc_ioapic_id(ioapic_idx));
1998 			for (i = 0; i < get_physical_broadcast(); i++)
1999 				if (!physid_isset(i, phys_id_present_map))
2000 					break;
2001 			if (i >= get_physical_broadcast())
2002 				panic("Max APIC ID exceeded!\n");
2003 			printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
2004 				i);
2005 			physid_set(i, phys_id_present_map);
2006 			ioapics[ioapic_idx].mp_config.apicid = i;
2007 		} else {
2008 			physid_mask_t tmp;
2009 			apic->apicid_to_cpu_present(mpc_ioapic_id(ioapic_idx),
2010 						    &tmp);
2011 			apic_printk(APIC_VERBOSE, "Setting %d in the "
2012 					"phys_id_present_map\n",
2013 					mpc_ioapic_id(ioapic_idx));
2014 			physids_or(phys_id_present_map, phys_id_present_map, tmp);
2015 		}
2016 
2017 		/*
2018 		 * We need to adjust the IRQ routing table
2019 		 * if the ID changed.
2020 		 */
2021 		if (old_id != mpc_ioapic_id(ioapic_idx))
2022 			for (i = 0; i < mp_irq_entries; i++)
2023 				if (mp_irqs[i].dstapic == old_id)
2024 					mp_irqs[i].dstapic
2025 						= mpc_ioapic_id(ioapic_idx);
2026 
2027 		/*
2028 		 * Update the ID register according to the right value
2029 		 * from the MPC table if they are different.
2030 		 */
2031 		if (mpc_ioapic_id(ioapic_idx) == reg_00.bits.ID)
2032 			continue;
2033 
2034 		apic_printk(APIC_VERBOSE, KERN_INFO
2035 			"...changing IO-APIC physical APIC ID to %d ...",
2036 			mpc_ioapic_id(ioapic_idx));
2037 
2038 		reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
2039 		raw_spin_lock_irqsave(&ioapic_lock, flags);
2040 		io_apic_write(ioapic_idx, 0, reg_00.raw);
2041 		raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2042 
2043 		/*
2044 		 * Sanity check
2045 		 */
2046 		raw_spin_lock_irqsave(&ioapic_lock, flags);
2047 		reg_00.raw = io_apic_read(ioapic_idx, 0);
2048 		raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2049 		if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx))
2050 			pr_cont("could not set ID!\n");
2051 		else
2052 			apic_printk(APIC_VERBOSE, " ok.\n");
2053 	}
2054 }
2055 
2056 void __init setup_ioapic_ids_from_mpc(void)
2057 {
2058 
2059 	if (acpi_ioapic)
2060 		return;
2061 	/*
2062 	 * Don't check I/O APIC IDs for xAPIC systems.  They have
2063 	 * no meaning without the serial APIC bus.
2064 	 */
2065 	if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
2066 		|| APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
2067 		return;
2068 	setup_ioapic_ids_from_mpc_nocheck();
2069 }
2070 #endif
2071 
2072 int no_timer_check __initdata;
2073 
2074 static int __init notimercheck(char *s)
2075 {
2076 	no_timer_check = 1;
2077 	return 1;
2078 }
2079 __setup("no_timer_check", notimercheck);
2080 
2081 /*
2082  * There is a nasty bug in some older SMP boards, their mptable lies
2083  * about the timer IRQ. We do the following to work around the situation:
2084  *
2085  *	- timer IRQ defaults to IO-APIC IRQ
2086  *	- if this function detects that timer IRQs are defunct, then we fall
2087  *	  back to ISA timer IRQs
2088  */
2089 static int __init timer_irq_works(void)
2090 {
2091 	unsigned long t1 = jiffies;
2092 	unsigned long flags;
2093 
2094 	if (no_timer_check)
2095 		return 1;
2096 
2097 	local_save_flags(flags);
2098 	local_irq_enable();
2099 	/* Let ten ticks pass... */
2100 	mdelay((10 * 1000) / HZ);
2101 	local_irq_restore(flags);
2102 
2103 	/*
2104 	 * Expect a few ticks at least, to be sure some possible
2105 	 * glue logic does not lock up after one or two first
2106 	 * ticks in a non-ExtINT mode.  Also the local APIC
2107 	 * might have cached one ExtINT interrupt.  Finally, at
2108 	 * least one tick may be lost due to delays.
2109 	 */
2110 
2111 	/* jiffies wrap? */
2112 	if (time_after(jiffies, t1 + 4))
2113 		return 1;
2114 	return 0;
2115 }
2116 
2117 /*
2118  * In the SMP+IOAPIC case it might happen that there are an unspecified
2119  * number of pending IRQ events unhandled. These cases are very rare,
2120  * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
2121  * better to do it this way as thus we do not have to be aware of
2122  * 'pending' interrupts in the IRQ path, except at this point.
2123  */
2124 /*
2125  * Edge triggered needs to resend any interrupt
2126  * that was delayed but this is now handled in the device
2127  * independent code.
2128  */
2129 
2130 /*
2131  * Starting up a edge-triggered IO-APIC interrupt is
2132  * nasty - we need to make sure that we get the edge.
2133  * If it is already asserted for some reason, we need
2134  * return 1 to indicate that is was pending.
2135  *
2136  * This is not complete - we should be able to fake
2137  * an edge even if it isn't on the 8259A...
2138  */
2139 
2140 static unsigned int startup_ioapic_irq(struct irq_data *data)
2141 {
2142 	int was_pending = 0, irq = data->irq;
2143 	unsigned long flags;
2144 
2145 	raw_spin_lock_irqsave(&ioapic_lock, flags);
2146 	if (irq < legacy_pic->nr_legacy_irqs) {
2147 		legacy_pic->mask(irq);
2148 		if (legacy_pic->irq_pending(irq))
2149 			was_pending = 1;
2150 	}
2151 	__unmask_ioapic(data->chip_data);
2152 	raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2153 
2154 	return was_pending;
2155 }
2156 
2157 static int ioapic_retrigger_irq(struct irq_data *data)
2158 {
2159 	struct irq_cfg *cfg = data->chip_data;
2160 	unsigned long flags;
2161 	int cpu;
2162 
2163 	raw_spin_lock_irqsave(&vector_lock, flags);
2164 	cpu = cpumask_first_and(cfg->domain, cpu_online_mask);
2165 	apic->send_IPI_mask(cpumask_of(cpu), cfg->vector);
2166 	raw_spin_unlock_irqrestore(&vector_lock, flags);
2167 
2168 	return 1;
2169 }
2170 
2171 /*
2172  * Level and edge triggered IO-APIC interrupts need different handling,
2173  * so we use two separate IRQ descriptors. Edge triggered IRQs can be
2174  * handled with the level-triggered descriptor, but that one has slightly
2175  * more overhead. Level-triggered interrupts cannot be handled with the
2176  * edge-triggered handler, without risking IRQ storms and other ugly
2177  * races.
2178  */
2179 
2180 #ifdef CONFIG_SMP
2181 void send_cleanup_vector(struct irq_cfg *cfg)
2182 {
2183 	cpumask_var_t cleanup_mask;
2184 
2185 	if (unlikely(!alloc_cpumask_var(&cleanup_mask, GFP_ATOMIC))) {
2186 		unsigned int i;
2187 		for_each_cpu_and(i, cfg->old_domain, cpu_online_mask)
2188 			apic->send_IPI_mask(cpumask_of(i), IRQ_MOVE_CLEANUP_VECTOR);
2189 	} else {
2190 		cpumask_and(cleanup_mask, cfg->old_domain, cpu_online_mask);
2191 		apic->send_IPI_mask(cleanup_mask, IRQ_MOVE_CLEANUP_VECTOR);
2192 		free_cpumask_var(cleanup_mask);
2193 	}
2194 	cfg->move_in_progress = 0;
2195 }
2196 
2197 asmlinkage __visible void smp_irq_move_cleanup_interrupt(void)
2198 {
2199 	unsigned vector, me;
2200 
2201 	ack_APIC_irq();
2202 	irq_enter();
2203 	exit_idle();
2204 
2205 	me = smp_processor_id();
2206 	for (vector = FIRST_EXTERNAL_VECTOR; vector < NR_VECTORS; vector++) {
2207 		int irq;
2208 		unsigned int irr;
2209 		struct irq_desc *desc;
2210 		struct irq_cfg *cfg;
2211 		irq = __this_cpu_read(vector_irq[vector]);
2212 
2213 		if (irq <= VECTOR_UNDEFINED)
2214 			continue;
2215 
2216 		desc = irq_to_desc(irq);
2217 		if (!desc)
2218 			continue;
2219 
2220 		cfg = irq_cfg(irq);
2221 		if (!cfg)
2222 			continue;
2223 
2224 		raw_spin_lock(&desc->lock);
2225 
2226 		/*
2227 		 * Check if the irq migration is in progress. If so, we
2228 		 * haven't received the cleanup request yet for this irq.
2229 		 */
2230 		if (cfg->move_in_progress)
2231 			goto unlock;
2232 
2233 		if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
2234 			goto unlock;
2235 
2236 		irr = apic_read(APIC_IRR + (vector / 32 * 0x10));
2237 		/*
2238 		 * Check if the vector that needs to be cleanedup is
2239 		 * registered at the cpu's IRR. If so, then this is not
2240 		 * the best time to clean it up. Lets clean it up in the
2241 		 * next attempt by sending another IRQ_MOVE_CLEANUP_VECTOR
2242 		 * to myself.
2243 		 */
2244 		if (irr  & (1 << (vector % 32))) {
2245 			apic->send_IPI_self(IRQ_MOVE_CLEANUP_VECTOR);
2246 			goto unlock;
2247 		}
2248 		__this_cpu_write(vector_irq[vector], VECTOR_UNDEFINED);
2249 unlock:
2250 		raw_spin_unlock(&desc->lock);
2251 	}
2252 
2253 	irq_exit();
2254 }
2255 
2256 static void __irq_complete_move(struct irq_cfg *cfg, unsigned vector)
2257 {
2258 	unsigned me;
2259 
2260 	if (likely(!cfg->move_in_progress))
2261 		return;
2262 
2263 	me = smp_processor_id();
2264 
2265 	if (vector == cfg->vector && cpumask_test_cpu(me, cfg->domain))
2266 		send_cleanup_vector(cfg);
2267 }
2268 
2269 static void irq_complete_move(struct irq_cfg *cfg)
2270 {
2271 	__irq_complete_move(cfg, ~get_irq_regs()->orig_ax);
2272 }
2273 
2274 void irq_force_complete_move(int irq)
2275 {
2276 	struct irq_cfg *cfg = irq_cfg(irq);
2277 
2278 	if (!cfg)
2279 		return;
2280 
2281 	__irq_complete_move(cfg, cfg->vector);
2282 }
2283 #else
2284 static inline void irq_complete_move(struct irq_cfg *cfg) { }
2285 #endif
2286 
2287 static void __target_IO_APIC_irq(unsigned int irq, unsigned int dest, struct irq_cfg *cfg)
2288 {
2289 	int apic, pin;
2290 	struct irq_pin_list *entry;
2291 	u8 vector = cfg->vector;
2292 
2293 	for_each_irq_pin(entry, cfg->irq_2_pin) {
2294 		unsigned int reg;
2295 
2296 		apic = entry->apic;
2297 		pin = entry->pin;
2298 
2299 		io_apic_write(apic, 0x11 + pin*2, dest);
2300 		reg = io_apic_read(apic, 0x10 + pin*2);
2301 		reg &= ~IO_APIC_REDIR_VECTOR_MASK;
2302 		reg |= vector;
2303 		io_apic_modify(apic, 0x10 + pin*2, reg);
2304 	}
2305 }
2306 
2307 /*
2308  * Either sets data->affinity to a valid value, and returns
2309  * ->cpu_mask_to_apicid of that in dest_id, or returns -1 and
2310  * leaves data->affinity untouched.
2311  */
2312 int __ioapic_set_affinity(struct irq_data *data, const struct cpumask *mask,
2313 			  unsigned int *dest_id)
2314 {
2315 	struct irq_cfg *cfg = data->chip_data;
2316 	unsigned int irq = data->irq;
2317 	int err;
2318 
2319 	if (!config_enabled(CONFIG_SMP))
2320 		return -EPERM;
2321 
2322 	if (!cpumask_intersects(mask, cpu_online_mask))
2323 		return -EINVAL;
2324 
2325 	err = assign_irq_vector(irq, cfg, mask);
2326 	if (err)
2327 		return err;
2328 
2329 	err = apic->cpu_mask_to_apicid_and(mask, cfg->domain, dest_id);
2330 	if (err) {
2331 		if (assign_irq_vector(irq, cfg, data->affinity))
2332 			pr_err("Failed to recover vector for irq %d\n", irq);
2333 		return err;
2334 	}
2335 
2336 	cpumask_copy(data->affinity, mask);
2337 
2338 	return 0;
2339 }
2340 
2341 
2342 int native_ioapic_set_affinity(struct irq_data *data,
2343 			       const struct cpumask *mask,
2344 			       bool force)
2345 {
2346 	unsigned int dest, irq = data->irq;
2347 	unsigned long flags;
2348 	int ret;
2349 
2350 	if (!config_enabled(CONFIG_SMP))
2351 		return -EPERM;
2352 
2353 	raw_spin_lock_irqsave(&ioapic_lock, flags);
2354 	ret = __ioapic_set_affinity(data, mask, &dest);
2355 	if (!ret) {
2356 		/* Only the high 8 bits are valid. */
2357 		dest = SET_APIC_LOGICAL_ID(dest);
2358 		__target_IO_APIC_irq(irq, dest, data->chip_data);
2359 		ret = IRQ_SET_MASK_OK_NOCOPY;
2360 	}
2361 	raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2362 	return ret;
2363 }
2364 
2365 static void ack_apic_edge(struct irq_data *data)
2366 {
2367 	irq_complete_move(data->chip_data);
2368 	irq_move_irq(data);
2369 	ack_APIC_irq();
2370 }
2371 
2372 atomic_t irq_mis_count;
2373 
2374 #ifdef CONFIG_GENERIC_PENDING_IRQ
2375 static bool io_apic_level_ack_pending(struct irq_cfg *cfg)
2376 {
2377 	struct irq_pin_list *entry;
2378 	unsigned long flags;
2379 
2380 	raw_spin_lock_irqsave(&ioapic_lock, flags);
2381 	for_each_irq_pin(entry, cfg->irq_2_pin) {
2382 		unsigned int reg;
2383 		int pin;
2384 
2385 		pin = entry->pin;
2386 		reg = io_apic_read(entry->apic, 0x10 + pin*2);
2387 		/* Is the remote IRR bit set? */
2388 		if (reg & IO_APIC_REDIR_REMOTE_IRR) {
2389 			raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2390 			return true;
2391 		}
2392 	}
2393 	raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2394 
2395 	return false;
2396 }
2397 
2398 static inline bool ioapic_irqd_mask(struct irq_data *data, struct irq_cfg *cfg)
2399 {
2400 	/* If we are moving the irq we need to mask it */
2401 	if (unlikely(irqd_is_setaffinity_pending(data))) {
2402 		mask_ioapic(cfg);
2403 		return true;
2404 	}
2405 	return false;
2406 }
2407 
2408 static inline void ioapic_irqd_unmask(struct irq_data *data,
2409 				      struct irq_cfg *cfg, bool masked)
2410 {
2411 	if (unlikely(masked)) {
2412 		/* Only migrate the irq if the ack has been received.
2413 		 *
2414 		 * On rare occasions the broadcast level triggered ack gets
2415 		 * delayed going to ioapics, and if we reprogram the
2416 		 * vector while Remote IRR is still set the irq will never
2417 		 * fire again.
2418 		 *
2419 		 * To prevent this scenario we read the Remote IRR bit
2420 		 * of the ioapic.  This has two effects.
2421 		 * - On any sane system the read of the ioapic will
2422 		 *   flush writes (and acks) going to the ioapic from
2423 		 *   this cpu.
2424 		 * - We get to see if the ACK has actually been delivered.
2425 		 *
2426 		 * Based on failed experiments of reprogramming the
2427 		 * ioapic entry from outside of irq context starting
2428 		 * with masking the ioapic entry and then polling until
2429 		 * Remote IRR was clear before reprogramming the
2430 		 * ioapic I don't trust the Remote IRR bit to be
2431 		 * completey accurate.
2432 		 *
2433 		 * However there appears to be no other way to plug
2434 		 * this race, so if the Remote IRR bit is not
2435 		 * accurate and is causing problems then it is a hardware bug
2436 		 * and you can go talk to the chipset vendor about it.
2437 		 */
2438 		if (!io_apic_level_ack_pending(cfg))
2439 			irq_move_masked_irq(data);
2440 		unmask_ioapic(cfg);
2441 	}
2442 }
2443 #else
2444 static inline bool ioapic_irqd_mask(struct irq_data *data, struct irq_cfg *cfg)
2445 {
2446 	return false;
2447 }
2448 static inline void ioapic_irqd_unmask(struct irq_data *data,
2449 				      struct irq_cfg *cfg, bool masked)
2450 {
2451 }
2452 #endif
2453 
2454 static void ack_apic_level(struct irq_data *data)
2455 {
2456 	struct irq_cfg *cfg = data->chip_data;
2457 	int i, irq = data->irq;
2458 	unsigned long v;
2459 	bool masked;
2460 
2461 	irq_complete_move(cfg);
2462 	masked = ioapic_irqd_mask(data, cfg);
2463 
2464 	/*
2465 	 * It appears there is an erratum which affects at least version 0x11
2466 	 * of I/O APIC (that's the 82093AA and cores integrated into various
2467 	 * chipsets).  Under certain conditions a level-triggered interrupt is
2468 	 * erroneously delivered as edge-triggered one but the respective IRR
2469 	 * bit gets set nevertheless.  As a result the I/O unit expects an EOI
2470 	 * message but it will never arrive and further interrupts are blocked
2471 	 * from the source.  The exact reason is so far unknown, but the
2472 	 * phenomenon was observed when two consecutive interrupt requests
2473 	 * from a given source get delivered to the same CPU and the source is
2474 	 * temporarily disabled in between.
2475 	 *
2476 	 * A workaround is to simulate an EOI message manually.  We achieve it
2477 	 * by setting the trigger mode to edge and then to level when the edge
2478 	 * trigger mode gets detected in the TMR of a local APIC for a
2479 	 * level-triggered interrupt.  We mask the source for the time of the
2480 	 * operation to prevent an edge-triggered interrupt escaping meanwhile.
2481 	 * The idea is from Manfred Spraul.  --macro
2482 	 *
2483 	 * Also in the case when cpu goes offline, fixup_irqs() will forward
2484 	 * any unhandled interrupt on the offlined cpu to the new cpu
2485 	 * destination that is handling the corresponding interrupt. This
2486 	 * interrupt forwarding is done via IPI's. Hence, in this case also
2487 	 * level-triggered io-apic interrupt will be seen as an edge
2488 	 * interrupt in the IRR. And we can't rely on the cpu's EOI
2489 	 * to be broadcasted to the IO-APIC's which will clear the remoteIRR
2490 	 * corresponding to the level-triggered interrupt. Hence on IO-APIC's
2491 	 * supporting EOI register, we do an explicit EOI to clear the
2492 	 * remote IRR and on IO-APIC's which don't have an EOI register,
2493 	 * we use the above logic (mask+edge followed by unmask+level) from
2494 	 * Manfred Spraul to clear the remote IRR.
2495 	 */
2496 	i = cfg->vector;
2497 	v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
2498 
2499 	/*
2500 	 * We must acknowledge the irq before we move it or the acknowledge will
2501 	 * not propagate properly.
2502 	 */
2503 	ack_APIC_irq();
2504 
2505 	/*
2506 	 * Tail end of clearing remote IRR bit (either by delivering the EOI
2507 	 * message via io-apic EOI register write or simulating it using
2508 	 * mask+edge followed by unnask+level logic) manually when the
2509 	 * level triggered interrupt is seen as the edge triggered interrupt
2510 	 * at the cpu.
2511 	 */
2512 	if (!(v & (1 << (i & 0x1f)))) {
2513 		atomic_inc(&irq_mis_count);
2514 
2515 		eoi_ioapic_irq(irq, cfg);
2516 	}
2517 
2518 	ioapic_irqd_unmask(data, cfg, masked);
2519 }
2520 
2521 static struct irq_chip ioapic_chip __read_mostly = {
2522 	.name			= "IO-APIC",
2523 	.irq_startup		= startup_ioapic_irq,
2524 	.irq_mask		= mask_ioapic_irq,
2525 	.irq_unmask		= unmask_ioapic_irq,
2526 	.irq_ack		= ack_apic_edge,
2527 	.irq_eoi		= ack_apic_level,
2528 	.irq_set_affinity	= native_ioapic_set_affinity,
2529 	.irq_retrigger		= ioapic_retrigger_irq,
2530 };
2531 
2532 static inline void init_IO_APIC_traps(void)
2533 {
2534 	struct irq_cfg *cfg;
2535 	unsigned int irq;
2536 
2537 	for_each_active_irq(irq) {
2538 		cfg = irq_cfg(irq);
2539 		if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) {
2540 			/*
2541 			 * Hmm.. We don't have an entry for this,
2542 			 * so default to an old-fashioned 8259
2543 			 * interrupt if we can..
2544 			 */
2545 			if (irq < legacy_pic->nr_legacy_irqs)
2546 				legacy_pic->make_irq(irq);
2547 			else
2548 				/* Strange. Oh, well.. */
2549 				irq_set_chip(irq, &no_irq_chip);
2550 		}
2551 	}
2552 }
2553 
2554 /*
2555  * The local APIC irq-chip implementation:
2556  */
2557 
2558 static void mask_lapic_irq(struct irq_data *data)
2559 {
2560 	unsigned long v;
2561 
2562 	v = apic_read(APIC_LVT0);
2563 	apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
2564 }
2565 
2566 static void unmask_lapic_irq(struct irq_data *data)
2567 {
2568 	unsigned long v;
2569 
2570 	v = apic_read(APIC_LVT0);
2571 	apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED);
2572 }
2573 
2574 static void ack_lapic_irq(struct irq_data *data)
2575 {
2576 	ack_APIC_irq();
2577 }
2578 
2579 static struct irq_chip lapic_chip __read_mostly = {
2580 	.name		= "local-APIC",
2581 	.irq_mask	= mask_lapic_irq,
2582 	.irq_unmask	= unmask_lapic_irq,
2583 	.irq_ack	= ack_lapic_irq,
2584 };
2585 
2586 static void lapic_register_intr(int irq)
2587 {
2588 	irq_clear_status_flags(irq, IRQ_LEVEL);
2589 	irq_set_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq,
2590 				      "edge");
2591 }
2592 
2593 /*
2594  * This looks a bit hackish but it's about the only one way of sending
2595  * a few INTA cycles to 8259As and any associated glue logic.  ICR does
2596  * not support the ExtINT mode, unfortunately.  We need to send these
2597  * cycles as some i82489DX-based boards have glue logic that keeps the
2598  * 8259A interrupt line asserted until INTA.  --macro
2599  */
2600 static inline void __init unlock_ExtINT_logic(void)
2601 {
2602 	int apic, pin, i;
2603 	struct IO_APIC_route_entry entry0, entry1;
2604 	unsigned char save_control, save_freq_select;
2605 
2606 	pin  = find_isa_irq_pin(8, mp_INT);
2607 	if (pin == -1) {
2608 		WARN_ON_ONCE(1);
2609 		return;
2610 	}
2611 	apic = find_isa_irq_apic(8, mp_INT);
2612 	if (apic == -1) {
2613 		WARN_ON_ONCE(1);
2614 		return;
2615 	}
2616 
2617 	entry0 = ioapic_read_entry(apic, pin);
2618 	clear_IO_APIC_pin(apic, pin);
2619 
2620 	memset(&entry1, 0, sizeof(entry1));
2621 
2622 	entry1.dest_mode = 0;			/* physical delivery */
2623 	entry1.mask = 0;			/* unmask IRQ now */
2624 	entry1.dest = hard_smp_processor_id();
2625 	entry1.delivery_mode = dest_ExtINT;
2626 	entry1.polarity = entry0.polarity;
2627 	entry1.trigger = 0;
2628 	entry1.vector = 0;
2629 
2630 	ioapic_write_entry(apic, pin, entry1);
2631 
2632 	save_control = CMOS_READ(RTC_CONTROL);
2633 	save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2634 	CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2635 		   RTC_FREQ_SELECT);
2636 	CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2637 
2638 	i = 100;
2639 	while (i-- > 0) {
2640 		mdelay(10);
2641 		if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2642 			i -= 10;
2643 	}
2644 
2645 	CMOS_WRITE(save_control, RTC_CONTROL);
2646 	CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
2647 	clear_IO_APIC_pin(apic, pin);
2648 
2649 	ioapic_write_entry(apic, pin, entry0);
2650 }
2651 
2652 static int disable_timer_pin_1 __initdata;
2653 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */
2654 static int __init disable_timer_pin_setup(char *arg)
2655 {
2656 	disable_timer_pin_1 = 1;
2657 	return 0;
2658 }
2659 early_param("disable_timer_pin_1", disable_timer_pin_setup);
2660 
2661 /*
2662  * This code may look a bit paranoid, but it's supposed to cooperate with
2663  * a wide range of boards and BIOS bugs.  Fortunately only the timer IRQ
2664  * is so screwy.  Thanks to Brian Perkins for testing/hacking this beast
2665  * fanatically on his truly buggy board.
2666  *
2667  * FIXME: really need to revamp this for all platforms.
2668  */
2669 static inline void __init check_timer(void)
2670 {
2671 	struct irq_cfg *cfg = irq_cfg(0);
2672 	int node = cpu_to_node(0);
2673 	int apic1, pin1, apic2, pin2;
2674 	unsigned long flags;
2675 	int no_pin1 = 0;
2676 
2677 	local_irq_save(flags);
2678 
2679 	/*
2680 	 * get/set the timer IRQ vector:
2681 	 */
2682 	legacy_pic->mask(0);
2683 	assign_irq_vector(0, cfg, apic->target_cpus());
2684 
2685 	/*
2686 	 * As IRQ0 is to be enabled in the 8259A, the virtual
2687 	 * wire has to be disabled in the local APIC.  Also
2688 	 * timer interrupts need to be acknowledged manually in
2689 	 * the 8259A for the i82489DX when using the NMI
2690 	 * watchdog as that APIC treats NMIs as level-triggered.
2691 	 * The AEOI mode will finish them in the 8259A
2692 	 * automatically.
2693 	 */
2694 	apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2695 	legacy_pic->init(1);
2696 
2697 	pin1  = find_isa_irq_pin(0, mp_INT);
2698 	apic1 = find_isa_irq_apic(0, mp_INT);
2699 	pin2  = ioapic_i8259.pin;
2700 	apic2 = ioapic_i8259.apic;
2701 
2702 	apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X "
2703 		    "apic1=%d pin1=%d apic2=%d pin2=%d\n",
2704 		    cfg->vector, apic1, pin1, apic2, pin2);
2705 
2706 	/*
2707 	 * Some BIOS writers are clueless and report the ExtINTA
2708 	 * I/O APIC input from the cascaded 8259A as the timer
2709 	 * interrupt input.  So just in case, if only one pin
2710 	 * was found above, try it both directly and through the
2711 	 * 8259A.
2712 	 */
2713 	if (pin1 == -1) {
2714 		panic_if_irq_remap("BIOS bug: timer not connected to IO-APIC");
2715 		pin1 = pin2;
2716 		apic1 = apic2;
2717 		no_pin1 = 1;
2718 	} else if (pin2 == -1) {
2719 		pin2 = pin1;
2720 		apic2 = apic1;
2721 	}
2722 
2723 	if (pin1 != -1) {
2724 		/*
2725 		 * Ok, does IRQ0 through the IOAPIC work?
2726 		 */
2727 		if (no_pin1) {
2728 			add_pin_to_irq_node(cfg, node, apic1, pin1);
2729 			setup_timer_IRQ0_pin(apic1, pin1, cfg->vector);
2730 		} else {
2731 			/* for edge trigger, setup_ioapic_irq already
2732 			 * leave it unmasked.
2733 			 * so only need to unmask if it is level-trigger
2734 			 * do we really have level trigger timer?
2735 			 */
2736 			int idx;
2737 			idx = find_irq_entry(apic1, pin1, mp_INT);
2738 			if (idx != -1 && irq_trigger(idx))
2739 				unmask_ioapic(cfg);
2740 		}
2741 		if (timer_irq_works()) {
2742 			if (disable_timer_pin_1 > 0)
2743 				clear_IO_APIC_pin(0, pin1);
2744 			goto out;
2745 		}
2746 		panic_if_irq_remap("timer doesn't work through Interrupt-remapped IO-APIC");
2747 		local_irq_disable();
2748 		clear_IO_APIC_pin(apic1, pin1);
2749 		if (!no_pin1)
2750 			apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: "
2751 				    "8254 timer not connected to IO-APIC\n");
2752 
2753 		apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer "
2754 			    "(IRQ0) through the 8259A ...\n");
2755 		apic_printk(APIC_QUIET, KERN_INFO
2756 			    "..... (found apic %d pin %d) ...\n", apic2, pin2);
2757 		/*
2758 		 * legacy devices should be connected to IO APIC #0
2759 		 */
2760 		replace_pin_at_irq_node(cfg, node, apic1, pin1, apic2, pin2);
2761 		setup_timer_IRQ0_pin(apic2, pin2, cfg->vector);
2762 		legacy_pic->unmask(0);
2763 		if (timer_irq_works()) {
2764 			apic_printk(APIC_QUIET, KERN_INFO "....... works.\n");
2765 			goto out;
2766 		}
2767 		/*
2768 		 * Cleanup, just in case ...
2769 		 */
2770 		local_irq_disable();
2771 		legacy_pic->mask(0);
2772 		clear_IO_APIC_pin(apic2, pin2);
2773 		apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n");
2774 	}
2775 
2776 	apic_printk(APIC_QUIET, KERN_INFO
2777 		    "...trying to set up timer as Virtual Wire IRQ...\n");
2778 
2779 	lapic_register_intr(0);
2780 	apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector);	/* Fixed mode */
2781 	legacy_pic->unmask(0);
2782 
2783 	if (timer_irq_works()) {
2784 		apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2785 		goto out;
2786 	}
2787 	local_irq_disable();
2788 	legacy_pic->mask(0);
2789 	apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector);
2790 	apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n");
2791 
2792 	apic_printk(APIC_QUIET, KERN_INFO
2793 		    "...trying to set up timer as ExtINT IRQ...\n");
2794 
2795 	legacy_pic->init(0);
2796 	legacy_pic->make_irq(0);
2797 	apic_write(APIC_LVT0, APIC_DM_EXTINT);
2798 
2799 	unlock_ExtINT_logic();
2800 
2801 	if (timer_irq_works()) {
2802 		apic_printk(APIC_QUIET, KERN_INFO "..... works.\n");
2803 		goto out;
2804 	}
2805 	local_irq_disable();
2806 	apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n");
2807 	if (x2apic_preenabled)
2808 		apic_printk(APIC_QUIET, KERN_INFO
2809 			    "Perhaps problem with the pre-enabled x2apic mode\n"
2810 			    "Try booting with x2apic and interrupt-remapping disabled in the bios.\n");
2811 	panic("IO-APIC + timer doesn't work!  Boot with apic=debug and send a "
2812 		"report.  Then try booting with the 'noapic' option.\n");
2813 out:
2814 	local_irq_restore(flags);
2815 }
2816 
2817 /*
2818  * Traditionally ISA IRQ2 is the cascade IRQ, and is not available
2819  * to devices.  However there may be an I/O APIC pin available for
2820  * this interrupt regardless.  The pin may be left unconnected, but
2821  * typically it will be reused as an ExtINT cascade interrupt for
2822  * the master 8259A.  In the MPS case such a pin will normally be
2823  * reported as an ExtINT interrupt in the MP table.  With ACPI
2824  * there is no provision for ExtINT interrupts, and in the absence
2825  * of an override it would be treated as an ordinary ISA I/O APIC
2826  * interrupt, that is edge-triggered and unmasked by default.  We
2827  * used to do this, but it caused problems on some systems because
2828  * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using
2829  * the same ExtINT cascade interrupt to drive the local APIC of the
2830  * bootstrap processor.  Therefore we refrain from routing IRQ2 to
2831  * the I/O APIC in all cases now.  No actual device should request
2832  * it anyway.  --macro
2833  */
2834 #define PIC_IRQS	(1UL << PIC_CASCADE_IR)
2835 
2836 void __init setup_IO_APIC(void)
2837 {
2838 
2839 	/*
2840 	 * calling enable_IO_APIC() is moved to setup_local_APIC for BP
2841 	 */
2842 	io_apic_irqs = legacy_pic->nr_legacy_irqs ? ~PIC_IRQS : ~0UL;
2843 
2844 	apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n");
2845 	/*
2846          * Set up IO-APIC IRQ routing.
2847          */
2848 	x86_init.mpparse.setup_ioapic_ids();
2849 
2850 	sync_Arb_IDs();
2851 	setup_IO_APIC_irqs();
2852 	init_IO_APIC_traps();
2853 	if (legacy_pic->nr_legacy_irqs)
2854 		check_timer();
2855 }
2856 
2857 /*
2858  *      Called after all the initialization is done. If we didn't find any
2859  *      APIC bugs then we can allow the modify fast path
2860  */
2861 
2862 static int __init io_apic_bug_finalize(void)
2863 {
2864 	if (sis_apic_bug == -1)
2865 		sis_apic_bug = 0;
2866 	return 0;
2867 }
2868 
2869 late_initcall(io_apic_bug_finalize);
2870 
2871 static void resume_ioapic_id(int ioapic_idx)
2872 {
2873 	unsigned long flags;
2874 	union IO_APIC_reg_00 reg_00;
2875 
2876 	raw_spin_lock_irqsave(&ioapic_lock, flags);
2877 	reg_00.raw = io_apic_read(ioapic_idx, 0);
2878 	if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx)) {
2879 		reg_00.bits.ID = mpc_ioapic_id(ioapic_idx);
2880 		io_apic_write(ioapic_idx, 0, reg_00.raw);
2881 	}
2882 	raw_spin_unlock_irqrestore(&ioapic_lock, flags);
2883 }
2884 
2885 static void ioapic_resume(void)
2886 {
2887 	int ioapic_idx;
2888 
2889 	for_each_ioapic_reverse(ioapic_idx)
2890 		resume_ioapic_id(ioapic_idx);
2891 
2892 	restore_ioapic_entries();
2893 }
2894 
2895 static struct syscore_ops ioapic_syscore_ops = {
2896 	.suspend = save_ioapic_entries,
2897 	.resume = ioapic_resume,
2898 };
2899 
2900 static int __init ioapic_init_ops(void)
2901 {
2902 	register_syscore_ops(&ioapic_syscore_ops);
2903 
2904 	return 0;
2905 }
2906 
2907 device_initcall(ioapic_init_ops);
2908 
2909 /*
2910  * Dynamic irq allocate and deallocation. Should be replaced by irq domains!
2911  */
2912 int arch_setup_hwirq(unsigned int irq, int node)
2913 {
2914 	struct irq_cfg *cfg;
2915 	unsigned long flags;
2916 	int ret;
2917 
2918 	cfg = alloc_irq_cfg(irq, node);
2919 	if (!cfg)
2920 		return -ENOMEM;
2921 
2922 	raw_spin_lock_irqsave(&vector_lock, flags);
2923 	ret = __assign_irq_vector(irq, cfg, apic->target_cpus());
2924 	raw_spin_unlock_irqrestore(&vector_lock, flags);
2925 
2926 	if (!ret)
2927 		irq_set_chip_data(irq, cfg);
2928 	else
2929 		free_irq_cfg(irq, cfg);
2930 	return ret;
2931 }
2932 
2933 void arch_teardown_hwirq(unsigned int irq)
2934 {
2935 	struct irq_cfg *cfg = irq_cfg(irq);
2936 	unsigned long flags;
2937 
2938 	free_remapped_irq(irq);
2939 	raw_spin_lock_irqsave(&vector_lock, flags);
2940 	__clear_irq_vector(irq, cfg);
2941 	raw_spin_unlock_irqrestore(&vector_lock, flags);
2942 	free_irq_cfg(irq, cfg);
2943 }
2944 
2945 /*
2946  * MSI message composition
2947  */
2948 void native_compose_msi_msg(struct pci_dev *pdev,
2949 			    unsigned int irq, unsigned int dest,
2950 			    struct msi_msg *msg, u8 hpet_id)
2951 {
2952 	struct irq_cfg *cfg = irq_cfg(irq);
2953 
2954 	msg->address_hi = MSI_ADDR_BASE_HI;
2955 
2956 	if (x2apic_enabled())
2957 		msg->address_hi |= MSI_ADDR_EXT_DEST_ID(dest);
2958 
2959 	msg->address_lo =
2960 		MSI_ADDR_BASE_LO |
2961 		((apic->irq_dest_mode == 0) ?
2962 			MSI_ADDR_DEST_MODE_PHYSICAL:
2963 			MSI_ADDR_DEST_MODE_LOGICAL) |
2964 		((apic->irq_delivery_mode != dest_LowestPrio) ?
2965 			MSI_ADDR_REDIRECTION_CPU:
2966 			MSI_ADDR_REDIRECTION_LOWPRI) |
2967 		MSI_ADDR_DEST_ID(dest);
2968 
2969 	msg->data =
2970 		MSI_DATA_TRIGGER_EDGE |
2971 		MSI_DATA_LEVEL_ASSERT |
2972 		((apic->irq_delivery_mode != dest_LowestPrio) ?
2973 			MSI_DATA_DELIVERY_FIXED:
2974 			MSI_DATA_DELIVERY_LOWPRI) |
2975 		MSI_DATA_VECTOR(cfg->vector);
2976 }
2977 
2978 #ifdef CONFIG_PCI_MSI
2979 static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq,
2980 			   struct msi_msg *msg, u8 hpet_id)
2981 {
2982 	struct irq_cfg *cfg;
2983 	int err;
2984 	unsigned dest;
2985 
2986 	if (disable_apic)
2987 		return -ENXIO;
2988 
2989 	cfg = irq_cfg(irq);
2990 	err = assign_irq_vector(irq, cfg, apic->target_cpus());
2991 	if (err)
2992 		return err;
2993 
2994 	err = apic->cpu_mask_to_apicid_and(cfg->domain,
2995 					   apic->target_cpus(), &dest);
2996 	if (err)
2997 		return err;
2998 
2999 	x86_msi.compose_msi_msg(pdev, irq, dest, msg, hpet_id);
3000 
3001 	return 0;
3002 }
3003 
3004 static int
3005 msi_set_affinity(struct irq_data *data, const struct cpumask *mask, bool force)
3006 {
3007 	struct irq_cfg *cfg = data->chip_data;
3008 	struct msi_msg msg;
3009 	unsigned int dest;
3010 	int ret;
3011 
3012 	ret = __ioapic_set_affinity(data, mask, &dest);
3013 	if (ret)
3014 		return ret;
3015 
3016 	__get_cached_msi_msg(data->msi_desc, &msg);
3017 
3018 	msg.data &= ~MSI_DATA_VECTOR_MASK;
3019 	msg.data |= MSI_DATA_VECTOR(cfg->vector);
3020 	msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3021 	msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3022 
3023 	__write_msi_msg(data->msi_desc, &msg);
3024 
3025 	return IRQ_SET_MASK_OK_NOCOPY;
3026 }
3027 
3028 /*
3029  * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
3030  * which implement the MSI or MSI-X Capability Structure.
3031  */
3032 static struct irq_chip msi_chip = {
3033 	.name			= "PCI-MSI",
3034 	.irq_unmask		= unmask_msi_irq,
3035 	.irq_mask		= mask_msi_irq,
3036 	.irq_ack		= ack_apic_edge,
3037 	.irq_set_affinity	= msi_set_affinity,
3038 	.irq_retrigger		= ioapic_retrigger_irq,
3039 };
3040 
3041 int setup_msi_irq(struct pci_dev *dev, struct msi_desc *msidesc,
3042 		  unsigned int irq_base, unsigned int irq_offset)
3043 {
3044 	struct irq_chip *chip = &msi_chip;
3045 	struct msi_msg msg;
3046 	unsigned int irq = irq_base + irq_offset;
3047 	int ret;
3048 
3049 	ret = msi_compose_msg(dev, irq, &msg, -1);
3050 	if (ret < 0)
3051 		return ret;
3052 
3053 	irq_set_msi_desc_off(irq_base, irq_offset, msidesc);
3054 
3055 	/*
3056 	 * MSI-X message is written per-IRQ, the offset is always 0.
3057 	 * MSI message denotes a contiguous group of IRQs, written for 0th IRQ.
3058 	 */
3059 	if (!irq_offset)
3060 		write_msi_msg(irq, &msg);
3061 
3062 	setup_remapped_irq(irq, irq_cfg(irq), chip);
3063 
3064 	irq_set_chip_and_handler_name(irq, chip, handle_edge_irq, "edge");
3065 
3066 	dev_printk(KERN_DEBUG, &dev->dev, "irq %d for MSI/MSI-X\n", irq);
3067 
3068 	return 0;
3069 }
3070 
3071 int native_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
3072 {
3073 	struct msi_desc *msidesc;
3074 	unsigned int irq;
3075 	int node, ret;
3076 
3077 	/* Multiple MSI vectors only supported with interrupt remapping */
3078 	if (type == PCI_CAP_ID_MSI && nvec > 1)
3079 		return 1;
3080 
3081 	node = dev_to_node(&dev->dev);
3082 
3083 	list_for_each_entry(msidesc, &dev->msi_list, list) {
3084 		irq = irq_alloc_hwirq(node);
3085 		if (!irq)
3086 			return -ENOSPC;
3087 
3088 		ret = setup_msi_irq(dev, msidesc, irq, 0);
3089 		if (ret < 0) {
3090 			irq_free_hwirq(irq);
3091 			return ret;
3092 		}
3093 
3094 	}
3095 	return 0;
3096 }
3097 
3098 void native_teardown_msi_irq(unsigned int irq)
3099 {
3100 	irq_free_hwirq(irq);
3101 }
3102 
3103 #ifdef CONFIG_DMAR_TABLE
3104 static int
3105 dmar_msi_set_affinity(struct irq_data *data, const struct cpumask *mask,
3106 		      bool force)
3107 {
3108 	struct irq_cfg *cfg = data->chip_data;
3109 	unsigned int dest, irq = data->irq;
3110 	struct msi_msg msg;
3111 	int ret;
3112 
3113 	ret = __ioapic_set_affinity(data, mask, &dest);
3114 	if (ret)
3115 		return ret;
3116 
3117 	dmar_msi_read(irq, &msg);
3118 
3119 	msg.data &= ~MSI_DATA_VECTOR_MASK;
3120 	msg.data |= MSI_DATA_VECTOR(cfg->vector);
3121 	msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3122 	msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3123 	msg.address_hi = MSI_ADDR_BASE_HI | MSI_ADDR_EXT_DEST_ID(dest);
3124 
3125 	dmar_msi_write(irq, &msg);
3126 
3127 	return IRQ_SET_MASK_OK_NOCOPY;
3128 }
3129 
3130 static struct irq_chip dmar_msi_type = {
3131 	.name			= "DMAR_MSI",
3132 	.irq_unmask		= dmar_msi_unmask,
3133 	.irq_mask		= dmar_msi_mask,
3134 	.irq_ack		= ack_apic_edge,
3135 	.irq_set_affinity	= dmar_msi_set_affinity,
3136 	.irq_retrigger		= ioapic_retrigger_irq,
3137 };
3138 
3139 int arch_setup_dmar_msi(unsigned int irq)
3140 {
3141 	int ret;
3142 	struct msi_msg msg;
3143 
3144 	ret = msi_compose_msg(NULL, irq, &msg, -1);
3145 	if (ret < 0)
3146 		return ret;
3147 	dmar_msi_write(irq, &msg);
3148 	irq_set_chip_and_handler_name(irq, &dmar_msi_type, handle_edge_irq,
3149 				      "edge");
3150 	return 0;
3151 }
3152 #endif
3153 
3154 #ifdef CONFIG_HPET_TIMER
3155 
3156 static int hpet_msi_set_affinity(struct irq_data *data,
3157 				 const struct cpumask *mask, bool force)
3158 {
3159 	struct irq_cfg *cfg = data->chip_data;
3160 	struct msi_msg msg;
3161 	unsigned int dest;
3162 	int ret;
3163 
3164 	ret = __ioapic_set_affinity(data, mask, &dest);
3165 	if (ret)
3166 		return ret;
3167 
3168 	hpet_msi_read(data->handler_data, &msg);
3169 
3170 	msg.data &= ~MSI_DATA_VECTOR_MASK;
3171 	msg.data |= MSI_DATA_VECTOR(cfg->vector);
3172 	msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
3173 	msg.address_lo |= MSI_ADDR_DEST_ID(dest);
3174 
3175 	hpet_msi_write(data->handler_data, &msg);
3176 
3177 	return IRQ_SET_MASK_OK_NOCOPY;
3178 }
3179 
3180 static struct irq_chip hpet_msi_type = {
3181 	.name = "HPET_MSI",
3182 	.irq_unmask = hpet_msi_unmask,
3183 	.irq_mask = hpet_msi_mask,
3184 	.irq_ack = ack_apic_edge,
3185 	.irq_set_affinity = hpet_msi_set_affinity,
3186 	.irq_retrigger = ioapic_retrigger_irq,
3187 };
3188 
3189 int default_setup_hpet_msi(unsigned int irq, unsigned int id)
3190 {
3191 	struct irq_chip *chip = &hpet_msi_type;
3192 	struct msi_msg msg;
3193 	int ret;
3194 
3195 	ret = msi_compose_msg(NULL, irq, &msg, id);
3196 	if (ret < 0)
3197 		return ret;
3198 
3199 	hpet_msi_write(irq_get_handler_data(irq), &msg);
3200 	irq_set_status_flags(irq, IRQ_MOVE_PCNTXT);
3201 	setup_remapped_irq(irq, irq_cfg(irq), chip);
3202 
3203 	irq_set_chip_and_handler_name(irq, chip, handle_edge_irq, "edge");
3204 	return 0;
3205 }
3206 #endif
3207 
3208 #endif /* CONFIG_PCI_MSI */
3209 /*
3210  * Hypertransport interrupt support
3211  */
3212 #ifdef CONFIG_HT_IRQ
3213 
3214 static void target_ht_irq(unsigned int irq, unsigned int dest, u8 vector)
3215 {
3216 	struct ht_irq_msg msg;
3217 	fetch_ht_irq_msg(irq, &msg);
3218 
3219 	msg.address_lo &= ~(HT_IRQ_LOW_VECTOR_MASK | HT_IRQ_LOW_DEST_ID_MASK);
3220 	msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
3221 
3222 	msg.address_lo |= HT_IRQ_LOW_VECTOR(vector) | HT_IRQ_LOW_DEST_ID(dest);
3223 	msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
3224 
3225 	write_ht_irq_msg(irq, &msg);
3226 }
3227 
3228 static int
3229 ht_set_affinity(struct irq_data *data, const struct cpumask *mask, bool force)
3230 {
3231 	struct irq_cfg *cfg = data->chip_data;
3232 	unsigned int dest;
3233 	int ret;
3234 
3235 	ret = __ioapic_set_affinity(data, mask, &dest);
3236 	if (ret)
3237 		return ret;
3238 
3239 	target_ht_irq(data->irq, dest, cfg->vector);
3240 	return IRQ_SET_MASK_OK_NOCOPY;
3241 }
3242 
3243 static struct irq_chip ht_irq_chip = {
3244 	.name			= "PCI-HT",
3245 	.irq_mask		= mask_ht_irq,
3246 	.irq_unmask		= unmask_ht_irq,
3247 	.irq_ack		= ack_apic_edge,
3248 	.irq_set_affinity	= ht_set_affinity,
3249 	.irq_retrigger		= ioapic_retrigger_irq,
3250 };
3251 
3252 int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
3253 {
3254 	struct irq_cfg *cfg;
3255 	struct ht_irq_msg msg;
3256 	unsigned dest;
3257 	int err;
3258 
3259 	if (disable_apic)
3260 		return -ENXIO;
3261 
3262 	cfg = irq_cfg(irq);
3263 	err = assign_irq_vector(irq, cfg, apic->target_cpus());
3264 	if (err)
3265 		return err;
3266 
3267 	err = apic->cpu_mask_to_apicid_and(cfg->domain,
3268 					   apic->target_cpus(), &dest);
3269 	if (err)
3270 		return err;
3271 
3272 	msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
3273 
3274 	msg.address_lo =
3275 		HT_IRQ_LOW_BASE |
3276 		HT_IRQ_LOW_DEST_ID(dest) |
3277 		HT_IRQ_LOW_VECTOR(cfg->vector) |
3278 		((apic->irq_dest_mode == 0) ?
3279 			HT_IRQ_LOW_DM_PHYSICAL :
3280 			HT_IRQ_LOW_DM_LOGICAL) |
3281 		HT_IRQ_LOW_RQEOI_EDGE |
3282 		((apic->irq_delivery_mode != dest_LowestPrio) ?
3283 			HT_IRQ_LOW_MT_FIXED :
3284 			HT_IRQ_LOW_MT_ARBITRATED) |
3285 		HT_IRQ_LOW_IRQ_MASKED;
3286 
3287 	write_ht_irq_msg(irq, &msg);
3288 
3289 	irq_set_chip_and_handler_name(irq, &ht_irq_chip,
3290 				      handle_edge_irq, "edge");
3291 
3292 	dev_printk(KERN_DEBUG, &dev->dev, "irq %d for HT\n", irq);
3293 
3294 	return 0;
3295 }
3296 #endif /* CONFIG_HT_IRQ */
3297 
3298 static int
3299 io_apic_setup_irq_pin(unsigned int irq, int node, struct io_apic_irq_attr *attr)
3300 {
3301 	struct irq_cfg *cfg = alloc_irq_and_cfg_at(irq, node);
3302 	int ret;
3303 
3304 	if (!cfg)
3305 		return -EINVAL;
3306 	ret = __add_pin_to_irq_node(cfg, node, attr->ioapic, attr->ioapic_pin);
3307 	if (!ret)
3308 		setup_ioapic_irq(irq, cfg, attr);
3309 	return ret;
3310 }
3311 
3312 int io_apic_setup_irq_pin_once(unsigned int irq, int node,
3313 			       struct io_apic_irq_attr *attr)
3314 {
3315 	unsigned int ioapic_idx = attr->ioapic, pin = attr->ioapic_pin;
3316 	int ret;
3317 	struct IO_APIC_route_entry orig_entry;
3318 
3319 	/* Avoid redundant programming */
3320 	if (test_bit(pin, ioapics[ioapic_idx].pin_programmed)) {
3321 		pr_debug("Pin %d-%d already programmed\n", mpc_ioapic_id(ioapic_idx), pin);
3322 		orig_entry = ioapic_read_entry(attr->ioapic, pin);
3323 		if (attr->trigger == orig_entry.trigger && attr->polarity == orig_entry.polarity)
3324 			return 0;
3325 		return -EBUSY;
3326 	}
3327 	ret = io_apic_setup_irq_pin(irq, node, attr);
3328 	if (!ret)
3329 		set_bit(pin, ioapics[ioapic_idx].pin_programmed);
3330 	return ret;
3331 }
3332 
3333 static int __init io_apic_get_redir_entries(int ioapic)
3334 {
3335 	union IO_APIC_reg_01	reg_01;
3336 	unsigned long flags;
3337 
3338 	raw_spin_lock_irqsave(&ioapic_lock, flags);
3339 	reg_01.raw = io_apic_read(ioapic, 1);
3340 	raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3341 
3342 	/* The register returns the maximum index redir index
3343 	 * supported, which is one less than the total number of redir
3344 	 * entries.
3345 	 */
3346 	return reg_01.bits.entries + 1;
3347 }
3348 
3349 unsigned int arch_dynirq_lower_bound(unsigned int from)
3350 {
3351 	unsigned int min = gsi_top + NR_IRQS_LEGACY;
3352 
3353 	return from < min ? min : from;
3354 }
3355 
3356 int __init arch_probe_nr_irqs(void)
3357 {
3358 	int nr;
3359 
3360 	if (nr_irqs > (NR_VECTORS * nr_cpu_ids))
3361 		nr_irqs = NR_VECTORS * nr_cpu_ids;
3362 
3363 	nr = (gsi_top + NR_IRQS_LEGACY) + 8 * nr_cpu_ids;
3364 #if defined(CONFIG_PCI_MSI) || defined(CONFIG_HT_IRQ)
3365 	/*
3366 	 * for MSI and HT dyn irq
3367 	 */
3368 	nr += (gsi_top + NR_IRQS_LEGACY) * 16;
3369 #endif
3370 	if (nr < nr_irqs)
3371 		nr_irqs = nr;
3372 
3373 	return NR_IRQS_LEGACY;
3374 }
3375 
3376 int io_apic_set_pci_routing(struct device *dev, int irq,
3377 			    struct io_apic_irq_attr *irq_attr)
3378 {
3379 	int node;
3380 
3381 	if (!IO_APIC_IRQ(irq)) {
3382 		apic_printk(APIC_QUIET,KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
3383 			    irq_attr->ioapic);
3384 		return -EINVAL;
3385 	}
3386 
3387 	node = dev ? dev_to_node(dev) : cpu_to_node(0);
3388 
3389 	return io_apic_setup_irq_pin_once(irq, node, irq_attr);
3390 }
3391 
3392 #ifdef CONFIG_X86_32
3393 static int __init io_apic_get_unique_id(int ioapic, int apic_id)
3394 {
3395 	union IO_APIC_reg_00 reg_00;
3396 	static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
3397 	physid_mask_t tmp;
3398 	unsigned long flags;
3399 	int i = 0;
3400 
3401 	/*
3402 	 * The P4 platform supports up to 256 APIC IDs on two separate APIC
3403 	 * buses (one for LAPICs, one for IOAPICs), where predecessors only
3404 	 * supports up to 16 on one shared APIC bus.
3405 	 *
3406 	 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
3407 	 *      advantage of new APIC bus architecture.
3408 	 */
3409 
3410 	if (physids_empty(apic_id_map))
3411 		apic->ioapic_phys_id_map(&phys_cpu_present_map, &apic_id_map);
3412 
3413 	raw_spin_lock_irqsave(&ioapic_lock, flags);
3414 	reg_00.raw = io_apic_read(ioapic, 0);
3415 	raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3416 
3417 	if (apic_id >= get_physical_broadcast()) {
3418 		printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
3419 			"%d\n", ioapic, apic_id, reg_00.bits.ID);
3420 		apic_id = reg_00.bits.ID;
3421 	}
3422 
3423 	/*
3424 	 * Every APIC in a system must have a unique ID or we get lots of nice
3425 	 * 'stuck on smp_invalidate_needed IPI wait' messages.
3426 	 */
3427 	if (apic->check_apicid_used(&apic_id_map, apic_id)) {
3428 
3429 		for (i = 0; i < get_physical_broadcast(); i++) {
3430 			if (!apic->check_apicid_used(&apic_id_map, i))
3431 				break;
3432 		}
3433 
3434 		if (i == get_physical_broadcast())
3435 			panic("Max apic_id exceeded!\n");
3436 
3437 		printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
3438 			"trying %d\n", ioapic, apic_id, i);
3439 
3440 		apic_id = i;
3441 	}
3442 
3443 	apic->apicid_to_cpu_present(apic_id, &tmp);
3444 	physids_or(apic_id_map, apic_id_map, tmp);
3445 
3446 	if (reg_00.bits.ID != apic_id) {
3447 		reg_00.bits.ID = apic_id;
3448 
3449 		raw_spin_lock_irqsave(&ioapic_lock, flags);
3450 		io_apic_write(ioapic, 0, reg_00.raw);
3451 		reg_00.raw = io_apic_read(ioapic, 0);
3452 		raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3453 
3454 		/* Sanity check */
3455 		if (reg_00.bits.ID != apic_id) {
3456 			pr_err("IOAPIC[%d]: Unable to change apic_id!\n",
3457 			       ioapic);
3458 			return -1;
3459 		}
3460 	}
3461 
3462 	apic_printk(APIC_VERBOSE, KERN_INFO
3463 			"IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
3464 
3465 	return apic_id;
3466 }
3467 
3468 static u8 __init io_apic_unique_id(u8 id)
3469 {
3470 	if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
3471 	    !APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
3472 		return io_apic_get_unique_id(nr_ioapics, id);
3473 	else
3474 		return id;
3475 }
3476 #else
3477 static u8 __init io_apic_unique_id(u8 id)
3478 {
3479 	int i;
3480 	DECLARE_BITMAP(used, 256);
3481 
3482 	bitmap_zero(used, 256);
3483 	for_each_ioapic(i)
3484 		__set_bit(mpc_ioapic_id(i), used);
3485 	if (!test_bit(id, used))
3486 		return id;
3487 	return find_first_zero_bit(used, 256);
3488 }
3489 #endif
3490 
3491 static int __init io_apic_get_version(int ioapic)
3492 {
3493 	union IO_APIC_reg_01	reg_01;
3494 	unsigned long flags;
3495 
3496 	raw_spin_lock_irqsave(&ioapic_lock, flags);
3497 	reg_01.raw = io_apic_read(ioapic, 1);
3498 	raw_spin_unlock_irqrestore(&ioapic_lock, flags);
3499 
3500 	return reg_01.bits.version;
3501 }
3502 
3503 int acpi_get_override_irq(u32 gsi, int *trigger, int *polarity)
3504 {
3505 	int ioapic, pin, idx;
3506 
3507 	if (skip_ioapic_setup)
3508 		return -1;
3509 
3510 	ioapic = mp_find_ioapic(gsi);
3511 	if (ioapic < 0)
3512 		return -1;
3513 
3514 	pin = mp_find_ioapic_pin(ioapic, gsi);
3515 	if (pin < 0)
3516 		return -1;
3517 
3518 	idx = find_irq_entry(ioapic, pin, mp_INT);
3519 	if (idx < 0)
3520 		return -1;
3521 
3522 	*trigger = irq_trigger(idx);
3523 	*polarity = irq_polarity(idx);
3524 	return 0;
3525 }
3526 
3527 /*
3528  * This function currently is only a helper for the i386 smp boot process where
3529  * we need to reprogram the ioredtbls to cater for the cpus which have come online
3530  * so mask in all cases should simply be apic->target_cpus()
3531  */
3532 #ifdef CONFIG_SMP
3533 void __init setup_ioapic_dest(void)
3534 {
3535 	int pin, ioapic, irq, irq_entry;
3536 	const struct cpumask *mask;
3537 	struct irq_data *idata;
3538 
3539 	if (skip_ioapic_setup == 1)
3540 		return;
3541 
3542 	for_each_ioapic_pin(ioapic, pin) {
3543 		irq_entry = find_irq_entry(ioapic, pin, mp_INT);
3544 		if (irq_entry == -1)
3545 			continue;
3546 
3547 		irq = pin_2_irq(irq_entry, ioapic, pin);
3548 		if (!mp_init_irq_at_boot(ioapic, irq))
3549 			continue;
3550 
3551 		idata = irq_get_irq_data(irq);
3552 
3553 		/*
3554 		 * Honour affinities which have been set in early boot
3555 		 */
3556 		if (!irqd_can_balance(idata) || irqd_affinity_was_set(idata))
3557 			mask = idata->affinity;
3558 		else
3559 			mask = apic->target_cpus();
3560 
3561 		x86_io_apic_ops.set_affinity(idata, mask, false);
3562 	}
3563 
3564 }
3565 #endif
3566 
3567 #define IOAPIC_RESOURCE_NAME_SIZE 11
3568 
3569 static struct resource *ioapic_resources;
3570 
3571 static struct resource * __init ioapic_setup_resources(void)
3572 {
3573 	unsigned long n;
3574 	struct resource *res;
3575 	char *mem;
3576 	int i, num = 0;
3577 
3578 	for_each_ioapic(i)
3579 		num++;
3580 	if (num == 0)
3581 		return NULL;
3582 
3583 	n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource);
3584 	n *= num;
3585 
3586 	mem = alloc_bootmem(n);
3587 	res = (void *)mem;
3588 
3589 	mem += sizeof(struct resource) * num;
3590 
3591 	num = 0;
3592 	for_each_ioapic(i) {
3593 		res[num].name = mem;
3594 		res[num].flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3595 		snprintf(mem, IOAPIC_RESOURCE_NAME_SIZE, "IOAPIC %u", i);
3596 		mem += IOAPIC_RESOURCE_NAME_SIZE;
3597 		num++;
3598 	}
3599 
3600 	ioapic_resources = res;
3601 
3602 	return res;
3603 }
3604 
3605 void __init native_io_apic_init_mappings(void)
3606 {
3607 	unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
3608 	struct resource *ioapic_res;
3609 	int i;
3610 
3611 	ioapic_res = ioapic_setup_resources();
3612 	for_each_ioapic(i) {
3613 		if (smp_found_config) {
3614 			ioapic_phys = mpc_ioapic_addr(i);
3615 #ifdef CONFIG_X86_32
3616 			if (!ioapic_phys) {
3617 				printk(KERN_ERR
3618 				       "WARNING: bogus zero IO-APIC "
3619 				       "address found in MPTABLE, "
3620 				       "disabling IO/APIC support!\n");
3621 				smp_found_config = 0;
3622 				skip_ioapic_setup = 1;
3623 				goto fake_ioapic_page;
3624 			}
3625 #endif
3626 		} else {
3627 #ifdef CONFIG_X86_32
3628 fake_ioapic_page:
3629 #endif
3630 			ioapic_phys = (unsigned long)alloc_bootmem_pages(PAGE_SIZE);
3631 			ioapic_phys = __pa(ioapic_phys);
3632 		}
3633 		set_fixmap_nocache(idx, ioapic_phys);
3634 		apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08lx (%08lx)\n",
3635 			__fix_to_virt(idx) + (ioapic_phys & ~PAGE_MASK),
3636 			ioapic_phys);
3637 		idx++;
3638 
3639 		ioapic_res->start = ioapic_phys;
3640 		ioapic_res->end = ioapic_phys + IO_APIC_SLOT_SIZE - 1;
3641 		ioapic_res++;
3642 	}
3643 }
3644 
3645 void __init ioapic_insert_resources(void)
3646 {
3647 	int i;
3648 	struct resource *r = ioapic_resources;
3649 
3650 	if (!r) {
3651 		if (nr_ioapics > 0)
3652 			printk(KERN_ERR
3653 				"IO APIC resources couldn't be allocated.\n");
3654 		return;
3655 	}
3656 
3657 	for_each_ioapic(i) {
3658 		insert_resource(&iomem_resource, r);
3659 		r++;
3660 	}
3661 }
3662 
3663 int mp_find_ioapic(u32 gsi)
3664 {
3665 	int i;
3666 
3667 	if (nr_ioapics == 0)
3668 		return -1;
3669 
3670 	/* Find the IOAPIC that manages this GSI. */
3671 	for_each_ioapic(i) {
3672 		struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(i);
3673 		if (gsi >= gsi_cfg->gsi_base && gsi <= gsi_cfg->gsi_end)
3674 			return i;
3675 	}
3676 
3677 	printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi);
3678 	return -1;
3679 }
3680 
3681 int mp_find_ioapic_pin(int ioapic, u32 gsi)
3682 {
3683 	struct mp_ioapic_gsi *gsi_cfg;
3684 
3685 	if (WARN_ON(ioapic < 0))
3686 		return -1;
3687 
3688 	gsi_cfg = mp_ioapic_gsi_routing(ioapic);
3689 	if (WARN_ON(gsi > gsi_cfg->gsi_end))
3690 		return -1;
3691 
3692 	return gsi - gsi_cfg->gsi_base;
3693 }
3694 
3695 static __init int bad_ioapic(unsigned long address)
3696 {
3697 	if (nr_ioapics >= MAX_IO_APICS) {
3698 		pr_warn("WARNING: Max # of I/O APICs (%d) exceeded (found %d), skipping\n",
3699 			MAX_IO_APICS, nr_ioapics);
3700 		return 1;
3701 	}
3702 	if (!address) {
3703 		pr_warn("WARNING: Bogus (zero) I/O APIC address found in table, skipping!\n");
3704 		return 1;
3705 	}
3706 	return 0;
3707 }
3708 
3709 static __init int bad_ioapic_register(int idx)
3710 {
3711 	union IO_APIC_reg_00 reg_00;
3712 	union IO_APIC_reg_01 reg_01;
3713 	union IO_APIC_reg_02 reg_02;
3714 
3715 	reg_00.raw = io_apic_read(idx, 0);
3716 	reg_01.raw = io_apic_read(idx, 1);
3717 	reg_02.raw = io_apic_read(idx, 2);
3718 
3719 	if (reg_00.raw == -1 && reg_01.raw == -1 && reg_02.raw == -1) {
3720 		pr_warn("I/O APIC 0x%x registers return all ones, skipping!\n",
3721 			mpc_ioapic_addr(idx));
3722 		return 1;
3723 	}
3724 
3725 	return 0;
3726 }
3727 
3728 void __init mp_register_ioapic(int id, u32 address, u32 gsi_base)
3729 {
3730 	int idx = 0;
3731 	int entries;
3732 	struct mp_ioapic_gsi *gsi_cfg;
3733 
3734 	if (bad_ioapic(address))
3735 		return;
3736 
3737 	idx = nr_ioapics;
3738 
3739 	ioapics[idx].mp_config.type = MP_IOAPIC;
3740 	ioapics[idx].mp_config.flags = MPC_APIC_USABLE;
3741 	ioapics[idx].mp_config.apicaddr = address;
3742 
3743 	set_fixmap_nocache(FIX_IO_APIC_BASE_0 + idx, address);
3744 
3745 	if (bad_ioapic_register(idx)) {
3746 		clear_fixmap(FIX_IO_APIC_BASE_0 + idx);
3747 		return;
3748 	}
3749 
3750 	ioapics[idx].mp_config.apicid = io_apic_unique_id(id);
3751 	ioapics[idx].mp_config.apicver = io_apic_get_version(idx);
3752 
3753 	/*
3754 	 * Build basic GSI lookup table to facilitate gsi->io_apic lookups
3755 	 * and to prevent reprogramming of IOAPIC pins (PCI GSIs).
3756 	 */
3757 	entries = io_apic_get_redir_entries(idx);
3758 	gsi_cfg = mp_ioapic_gsi_routing(idx);
3759 	gsi_cfg->gsi_base = gsi_base;
3760 	gsi_cfg->gsi_end = gsi_base + entries - 1;
3761 
3762 	/*
3763 	 * The number of IO-APIC IRQ registers (== #pins):
3764 	 */
3765 	ioapics[idx].nr_registers = entries;
3766 
3767 	if (gsi_cfg->gsi_end >= gsi_top)
3768 		gsi_top = gsi_cfg->gsi_end + 1;
3769 
3770 	pr_info("IOAPIC[%d]: apic_id %d, version %d, address 0x%x, GSI %d-%d\n",
3771 		idx, mpc_ioapic_id(idx),
3772 		mpc_ioapic_ver(idx), mpc_ioapic_addr(idx),
3773 		gsi_cfg->gsi_base, gsi_cfg->gsi_end);
3774 
3775 	nr_ioapics++;
3776 }
3777 
3778 /* Enable IOAPIC early just for system timer */
3779 void __init pre_init_apic_IRQ0(void)
3780 {
3781 	struct io_apic_irq_attr attr = { 0, 0, 0, 0 };
3782 
3783 	printk(KERN_INFO "Early APIC setup for system timer0\n");
3784 #ifndef CONFIG_SMP
3785 	physid_set_mask_of_physid(boot_cpu_physical_apicid,
3786 					 &phys_cpu_present_map);
3787 #endif
3788 	setup_local_APIC();
3789 
3790 	io_apic_setup_irq_pin(0, 0, &attr);
3791 	irq_set_chip_and_handler_name(0, &ioapic_chip, handle_edge_irq,
3792 				      "edge");
3793 }
3794