blob: 40fbb229fe7d79118d41d2a7bd26f84f46243141 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel IO-APIC support for multi-Pentium hosts.
3 *
4 * Copyright (C) 1997, 1998, 1999, 2000 Ingo Molnar, Hajnalka Szabo
5 *
6 * Many thanks to Stig Venaas for trying out countless experimental
7 * patches and reporting/debugging problems patiently!
8 *
9 * (c) 1999, Multiple IO-APIC support, developed by
10 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and
11 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>,
12 * further tested and cleaned up by Zach Brown <zab@redhat.com>
13 * and Ingo Molnar <mingo@redhat.com>
14 *
15 * Fixes
16 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
17 * thanks to Eric Gilmore
18 * and Rolf G. Tews
19 * for testing these extensively
20 * Paul Diefenbaugh : Added full ACPI support
21 */
22
23#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/interrupt.h>
25#include <linux/init.h>
26#include <linux/delay.h>
27#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/mc146818rtc.h>
29#include <linux/compiler.h>
30#include <linux/acpi.h>
Alexey Dobriyan129f6942005-06-23 00:08:33 -070031#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/sysdev.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070033#include <linux/pci.h>
Eric W. Biederman3b7d1922006-10-04 02:16:59 -070034#include <linux/msi.h>
Eric W. Biederman95d77882006-10-04 02:17:01 -070035#include <linux/htirq.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +020037#include <linux/kthread.h>
Julia Lawall1d16b532008-01-30 13:32:19 +010038#include <linux/jiffies.h> /* time_after() */
Ashok Raj54d5d422005-09-06 15:16:15 -070039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/io.h>
41#include <asm/smp.h>
42#include <asm/desc.h>
43#include <asm/timer.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070044#include <asm/i8259.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020045#include <asm/nmi.h>
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -070046#include <asm/msidef.h>
Eric W. Biederman8b955b02006-10-04 02:16:55 -070047#include <asm/hypertransport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#include <mach_apic.h>
Andi Kleen874c4fe2006-09-26 10:52:26 +020050#include <mach_apicdef.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052int (*ioapic_renumber_irq)(int ioapic, int irq);
53atomic_t irq_mis_count;
54
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -080055/* Where if anywhere is the i8259 connect in external int mode */
56static struct { int pin, apic; } ioapic_i8259 = { -1, -1 };
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static DEFINE_SPINLOCK(ioapic_lock);
Jan Beulich0a1ad602006-06-26 13:56:43 +020059static DEFINE_SPINLOCK(vector_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Maciej W. Rozycki35542c52008-05-21 22:10:22 +010061int timer_through_8259 __initdata;
Andi Kleenf9262c12006-03-08 17:57:25 -080062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063/*
64 * Is the SiS APIC rmw bug present ?
65 * -1 = don't know, 0 = no, 1 = yes
66 */
67int sis_apic_bug = -1;
68
69/*
70 * # of IRQ routing registers
71 */
72int nr_ioapic_registers[MAX_IO_APICS];
73
Alexey Starikovskiy9f640cc2008-04-04 23:41:13 +040074/* I/O APIC entries */
75struct mpc_config_ioapic mp_ioapics[MAX_IO_APICS];
76int nr_ioapics;
77
Alexey Starikovskiy584f7342008-04-04 23:41:32 +040078/* MP IRQ source entries */
79struct mpc_config_intsrc mp_irqs[MAX_IRQ_SOURCES];
80
81/* # of MP IRQ source entries */
82int mp_irq_entries;
83
Rusty Russell1a3f2392006-09-26 10:52:32 +020084static int disable_timer_pin_1 __initdata;
Chuck Ebbert66759a02005-09-12 18:49:25 +020085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/*
87 * Rough estimation of how many shared IRQs there are, can
88 * be changed anytime.
89 */
90#define MAX_PLUS_SHARED_IRQS NR_IRQS
91#define PIN_MAP_SIZE (MAX_PLUS_SHARED_IRQS + NR_IRQS)
92
93/*
94 * This is performance-critical, we want to do it O(1)
95 *
96 * the indexing order of this array favors 1:1 mappings
97 * between pins and IRQs.
98 */
99
100static struct irq_pin_list {
101 int apic, pin, next;
102} irq_2_pin[PIN_MAP_SIZE];
103
Linus Torvalds130fe052006-11-01 09:11:00 -0800104struct io_apic {
105 unsigned int index;
106 unsigned int unused[3];
107 unsigned int data;
108};
109
110static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx)
111{
112 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx)
113 + (mp_ioapics[idx].mpc_apicaddr & ~PAGE_MASK);
114}
115
116static inline unsigned int io_apic_read(unsigned int apic, unsigned int reg)
117{
118 struct io_apic __iomem *io_apic = io_apic_base(apic);
119 writel(reg, &io_apic->index);
120 return readl(&io_apic->data);
121}
122
123static inline void io_apic_write(unsigned int apic, unsigned int reg, unsigned int value)
124{
125 struct io_apic __iomem *io_apic = io_apic_base(apic);
126 writel(reg, &io_apic->index);
127 writel(value, &io_apic->data);
128}
129
130/*
131 * Re-write a value: to be used for read-modify-write
132 * cycles where the read already set up the index register.
133 *
134 * Older SiS APIC requires we rewrite the index register
135 */
136static inline void io_apic_modify(unsigned int apic, unsigned int reg, unsigned int value)
137{
Al Virocb468982007-02-09 16:39:25 +0000138 volatile struct io_apic __iomem *io_apic = io_apic_base(apic);
Linus Torvalds130fe052006-11-01 09:11:00 -0800139 if (sis_apic_bug)
140 writel(reg, &io_apic->index);
141 writel(value, &io_apic->data);
142}
143
Andi Kleencf4c6a22006-09-26 10:52:30 +0200144union entry_union {
145 struct { u32 w1, w2; };
146 struct IO_APIC_route_entry entry;
147};
148
149static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin)
150{
151 union entry_union eu;
152 unsigned long flags;
153 spin_lock_irqsave(&ioapic_lock, flags);
154 eu.w1 = io_apic_read(apic, 0x10 + 2 * pin);
155 eu.w2 = io_apic_read(apic, 0x11 + 2 * pin);
156 spin_unlock_irqrestore(&ioapic_lock, flags);
157 return eu.entry;
158}
159
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800160/*
161 * When we write a new IO APIC routing entry, we need to write the high
162 * word first! If the mask bit in the low word is clear, we will enable
163 * the interrupt, and we need to make sure the entry is fully populated
164 * before that happens.
165 */
Andi Kleend15512f2006-12-07 02:14:07 +0100166static void
167__ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
168{
169 union entry_union eu;
170 eu.entry = e;
171 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
172 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
173}
174
Andi Kleencf4c6a22006-09-26 10:52:30 +0200175static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e)
176{
177 unsigned long flags;
Andi Kleencf4c6a22006-09-26 10:52:30 +0200178 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleend15512f2006-12-07 02:14:07 +0100179 __ioapic_write_entry(apic, pin, e);
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800180 spin_unlock_irqrestore(&ioapic_lock, flags);
181}
182
183/*
184 * When we mask an IO APIC routing entry, we need to write the low
185 * word first, in order to set the mask bit before we change the
186 * high bits!
187 */
188static void ioapic_mask_entry(int apic, int pin)
189{
190 unsigned long flags;
191 union entry_union eu = { .entry.mask = 1 };
192
193 spin_lock_irqsave(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +0200194 io_apic_write(apic, 0x10 + 2*pin, eu.w1);
195 io_apic_write(apic, 0x11 + 2*pin, eu.w2);
196 spin_unlock_irqrestore(&ioapic_lock, flags);
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199/*
200 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are
201 * shared ISA-space IRQs, so we have to support them. We are super
202 * fast in the common case, and fast for shared ISA-space IRQs.
203 */
204static void add_pin_to_irq(unsigned int irq, int apic, int pin)
205{
206 static int first_free_entry = NR_IRQS;
207 struct irq_pin_list *entry = irq_2_pin + irq;
208
209 while (entry->next)
210 entry = irq_2_pin + entry->next;
211
212 if (entry->pin != -1) {
213 entry->next = first_free_entry;
214 entry = irq_2_pin + entry->next;
215 if (++first_free_entry >= PIN_MAP_SIZE)
216 panic("io_apic.c: whoops");
217 }
218 entry->apic = apic;
219 entry->pin = pin;
220}
221
222/*
223 * Reroute an IRQ to a different pin.
224 */
225static void __init replace_pin_at_irq(unsigned int irq,
226 int oldapic, int oldpin,
227 int newapic, int newpin)
228{
229 struct irq_pin_list *entry = irq_2_pin + irq;
230
231 while (1) {
232 if (entry->apic == oldapic && entry->pin == oldpin) {
233 entry->apic = newapic;
234 entry->pin = newpin;
235 }
236 if (!entry->next)
237 break;
238 entry = irq_2_pin + entry->next;
239 }
240}
241
242static void __modify_IO_APIC_irq (unsigned int irq, unsigned long enable, unsigned long disable)
243{
244 struct irq_pin_list *entry = irq_2_pin + irq;
245 unsigned int pin, reg;
246
247 for (;;) {
248 pin = entry->pin;
249 if (pin == -1)
250 break;
251 reg = io_apic_read(entry->apic, 0x10 + pin*2);
252 reg &= ~disable;
253 reg |= enable;
254 io_apic_modify(entry->apic, 0x10 + pin*2, reg);
255 if (!entry->next)
256 break;
257 entry = irq_2_pin + entry->next;
258 }
259}
260
261/* mask = 1 */
262static void __mask_IO_APIC_irq (unsigned int irq)
263{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400264 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
267/* mask = 0 */
268static void __unmask_IO_APIC_irq (unsigned int irq)
269{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400270 __modify_IO_APIC_irq(irq, 0, IO_APIC_REDIR_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
273/* mask = 1, trigger = 0 */
274static void __mask_and_edge_IO_APIC_irq (unsigned int irq)
275{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400276 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_MASKED,
277 IO_APIC_REDIR_LEVEL_TRIGGER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
280/* mask = 0, trigger = 1 */
281static void __unmask_and_level_IO_APIC_irq (unsigned int irq)
282{
Cyrill Gorcunov46b3b4e2008-06-07 19:53:57 +0400283 __modify_IO_APIC_irq(irq, IO_APIC_REDIR_LEVEL_TRIGGER,
284 IO_APIC_REDIR_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287static void mask_IO_APIC_irq (unsigned int irq)
288{
289 unsigned long flags;
290
291 spin_lock_irqsave(&ioapic_lock, flags);
292 __mask_IO_APIC_irq(irq);
293 spin_unlock_irqrestore(&ioapic_lock, flags);
294}
295
296static void unmask_IO_APIC_irq (unsigned int irq)
297{
298 unsigned long flags;
299
300 spin_lock_irqsave(&ioapic_lock, flags);
301 __unmask_IO_APIC_irq(irq);
302 spin_unlock_irqrestore(&ioapic_lock, flags);
303}
304
305static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin)
306{
307 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /* Check delivery_mode to be sure we're not clearing an SMI pin */
Andi Kleencf4c6a22006-09-26 10:52:30 +0200310 entry = ioapic_read_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (entry.delivery_mode == dest_SMI)
312 return;
313
314 /*
315 * Disable it in the IO-APIC irq-routing table:
316 */
Linus Torvaldsf9dadfa2006-11-01 10:05:35 -0800317 ioapic_mask_entry(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
320static void clear_IO_APIC (void)
321{
322 int apic, pin;
323
324 for (apic = 0; apic < nr_ioapics; apic++)
325 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++)
326 clear_IO_APIC_pin(apic, pin);
327}
328
Ashok Raj54d5d422005-09-06 15:16:15 -0700329#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static void set_ioapic_affinity_irq(unsigned int irq, cpumask_t cpumask)
331{
332 unsigned long flags;
333 int pin;
334 struct irq_pin_list *entry = irq_2_pin + irq;
335 unsigned int apicid_value;
Ashok Raj54d5d422005-09-06 15:16:15 -0700336 cpumask_t tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Ashok Raj54d5d422005-09-06 15:16:15 -0700338 cpus_and(tmp, cpumask, cpu_online_map);
339 if (cpus_empty(tmp))
340 tmp = TARGET_CPUS;
341
342 cpus_and(cpumask, tmp, CPU_MASK_ALL);
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 apicid_value = cpu_mask_to_apicid(cpumask);
345 /* Prepare to do the io_apic_write */
346 apicid_value = apicid_value << 24;
347 spin_lock_irqsave(&ioapic_lock, flags);
348 for (;;) {
349 pin = entry->pin;
350 if (pin == -1)
351 break;
352 io_apic_write(entry->apic, 0x10 + 1 + pin*2, apicid_value);
353 if (!entry->next)
354 break;
355 entry = irq_2_pin + entry->next;
356 }
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -0700357 irq_desc[irq].affinity = cpumask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 spin_unlock_irqrestore(&ioapic_lock, flags);
359}
360
361#if defined(CONFIG_IRQBALANCE)
362# include <asm/processor.h> /* kernel_thread() */
363# include <linux/kernel_stat.h> /* kstat */
364# include <linux/slab.h> /* kmalloc() */
Julia Lawall1d16b532008-01-30 13:32:19 +0100365# include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367#define IRQBALANCE_CHECK_ARCH -999
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700368#define MAX_BALANCED_IRQ_INTERVAL (5*HZ)
369#define MIN_BALANCED_IRQ_INTERVAL (HZ/2)
370#define BALANCED_IRQ_MORE_DELTA (HZ/10)
371#define BALANCED_IRQ_LESS_DELTA (HZ)
372
373static int irqbalance_disabled __read_mostly = IRQBALANCE_CHECK_ARCH;
374static int physical_balance __read_mostly;
375static long balanced_irq_interval __read_mostly = MAX_BALANCED_IRQ_INTERVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377static struct irq_cpu_info {
378 unsigned long * last_irq;
379 unsigned long * irq_delta;
380 unsigned long irq;
381} irq_cpu_data[NR_CPUS];
382
383#define CPU_IRQ(cpu) (irq_cpu_data[cpu].irq)
384#define LAST_CPU_IRQ(cpu,irq) (irq_cpu_data[cpu].last_irq[irq])
385#define IRQ_DELTA(cpu,irq) (irq_cpu_data[cpu].irq_delta[irq])
386
387#define IDLE_ENOUGH(cpu,now) \
388 (idle_cpu(cpu) && ((now) - per_cpu(irq_stat, (cpu)).idle_timestamp > 1))
389
390#define IRQ_ALLOWED(cpu, allowed_mask) cpu_isset(cpu, allowed_mask)
391
Mike Travisd5a74302007-10-16 01:24:05 -0700392#define CPU_TO_PACKAGEINDEX(i) (first_cpu(per_cpu(cpu_sibling_map, i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700394static cpumask_t balance_irq_affinity[NR_IRQS] = {
395 [0 ... NR_IRQS-1] = CPU_MASK_ALL
396};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700398void set_balance_irq_affinity(unsigned int irq, cpumask_t mask)
399{
400 balance_irq_affinity[irq] = mask;
401}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403static unsigned long move(int curr_cpu, cpumask_t allowed_mask,
404 unsigned long now, int direction)
405{
406 int search_idle = 1;
407 int cpu = curr_cpu;
408
409 goto inside;
410
411 do {
412 if (unlikely(cpu == curr_cpu))
413 search_idle = 0;
414inside:
415 if (direction == 1) {
416 cpu++;
417 if (cpu >= NR_CPUS)
418 cpu = 0;
419 } else {
420 cpu--;
421 if (cpu == -1)
422 cpu = NR_CPUS-1;
423 }
424 } while (!cpu_online(cpu) || !IRQ_ALLOWED(cpu,allowed_mask) ||
425 (search_idle && !IDLE_ENOUGH(cpu,now)));
426
427 return cpu;
428}
429
430static inline void balance_irq(int cpu, int irq)
431{
432 unsigned long now = jiffies;
433 cpumask_t allowed_mask;
434 unsigned int new_cpu;
435
436 if (irqbalance_disabled)
437 return;
438
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700439 cpus_and(allowed_mask, cpu_online_map, balance_irq_affinity[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 new_cpu = move(cpu, allowed_mask, now, 1);
441 if (cpu != new_cpu) {
Ashok Raj54d5d422005-09-06 15:16:15 -0700442 set_pending_irq(irq, cpumask_of_cpu(new_cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444}
445
446static inline void rotate_irqs_among_cpus(unsigned long useful_load_threshold)
447{
448 int i, j;
Stefan Richteredc2cbf2007-07-21 17:11:40 +0200449
Andrew Morton394e3902006-03-23 03:01:05 -0800450 for_each_online_cpu(i) {
451 for (j = 0; j < NR_IRQS; j++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!irq_desc[j].action)
453 continue;
454 /* Is it a significant load ? */
455 if (IRQ_DELTA(CPU_TO_PACKAGEINDEX(i),j) <
456 useful_load_threshold)
457 continue;
458 balance_irq(i, j);
459 }
460 }
461 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
462 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
463 return;
464}
465
466static void do_irq_balance(void)
467{
468 int i, j;
469 unsigned long max_cpu_irq = 0, min_cpu_irq = (~0);
470 unsigned long move_this_load = 0;
471 int max_loaded = 0, min_loaded = 0;
472 int load;
473 unsigned long useful_load_threshold = balanced_irq_interval + 10;
474 int selected_irq;
475 int tmp_loaded, first_attempt = 1;
476 unsigned long tmp_cpu_irq;
477 unsigned long imbalance = 0;
478 cpumask_t allowed_mask, target_cpu_mask, tmp;
479
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800480 for_each_possible_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 int package_index;
482 CPU_IRQ(i) = 0;
483 if (!cpu_online(i))
484 continue;
485 package_index = CPU_TO_PACKAGEINDEX(i);
486 for (j = 0; j < NR_IRQS; j++) {
487 unsigned long value_now, delta;
Thomas Gleixner950f4422007-02-16 01:27:24 -0800488 /* Is this an active IRQ or balancing disabled ? */
489 if (!irq_desc[j].action || irq_balancing_disabled(j))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 continue;
491 if ( package_index == i )
492 IRQ_DELTA(package_index,j) = 0;
493 /* Determine the total count per processor per IRQ */
494 value_now = (unsigned long) kstat_cpu(i).irqs[j];
495
496 /* Determine the activity per processor per IRQ */
497 delta = value_now - LAST_CPU_IRQ(i,j);
498
499 /* Update last_cpu_irq[][] for the next time */
500 LAST_CPU_IRQ(i,j) = value_now;
501
502 /* Ignore IRQs whose rate is less than the clock */
503 if (delta < useful_load_threshold)
504 continue;
505 /* update the load for the processor or package total */
506 IRQ_DELTA(package_index,j) += delta;
507
508 /* Keep track of the higher numbered sibling as well */
509 if (i != package_index)
510 CPU_IRQ(i) += delta;
511 /*
512 * We have sibling A and sibling B in the package
513 *
514 * cpu_irq[A] = load for cpu A + load for cpu B
515 * cpu_irq[B] = load for cpu B
516 */
517 CPU_IRQ(package_index) += delta;
518 }
519 }
520 /* Find the least loaded processor package */
Andrew Morton394e3902006-03-23 03:01:05 -0800521 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (i != CPU_TO_PACKAGEINDEX(i))
523 continue;
524 if (min_cpu_irq > CPU_IRQ(i)) {
525 min_cpu_irq = CPU_IRQ(i);
526 min_loaded = i;
527 }
528 }
529 max_cpu_irq = ULONG_MAX;
530
531tryanothercpu:
532 /* Look for heaviest loaded processor.
533 * We may come back to get the next heaviest loaded processor.
534 * Skip processors with trivial loads.
535 */
536 tmp_cpu_irq = 0;
537 tmp_loaded = -1;
Andrew Morton394e3902006-03-23 03:01:05 -0800538 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (i != CPU_TO_PACKAGEINDEX(i))
540 continue;
541 if (max_cpu_irq <= CPU_IRQ(i))
542 continue;
543 if (tmp_cpu_irq < CPU_IRQ(i)) {
544 tmp_cpu_irq = CPU_IRQ(i);
545 tmp_loaded = i;
546 }
547 }
548
549 if (tmp_loaded == -1) {
550 /* In the case of small number of heavy interrupt sources,
551 * loading some of the cpus too much. We use Ingo's original
552 * approach to rotate them around.
553 */
554 if (!first_attempt && imbalance >= useful_load_threshold) {
555 rotate_irqs_among_cpus(useful_load_threshold);
556 return;
557 }
558 goto not_worth_the_effort;
559 }
560
561 first_attempt = 0; /* heaviest search */
562 max_cpu_irq = tmp_cpu_irq; /* load */
563 max_loaded = tmp_loaded; /* processor */
564 imbalance = (max_cpu_irq - min_cpu_irq) / 2;
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 /* if imbalance is less than approx 10% of max load, then
567 * observe diminishing returns action. - quit
568 */
Stefan Richteredc2cbf2007-07-21 17:11:40 +0200569 if (imbalance < (max_cpu_irq >> 3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 goto not_worth_the_effort;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572tryanotherirq:
573 /* if we select an IRQ to move that can't go where we want, then
574 * see if there is another one to try.
575 */
576 move_this_load = 0;
577 selected_irq = -1;
578 for (j = 0; j < NR_IRQS; j++) {
579 /* Is this an active IRQ? */
580 if (!irq_desc[j].action)
581 continue;
582 if (imbalance <= IRQ_DELTA(max_loaded,j))
583 continue;
584 /* Try to find the IRQ that is closest to the imbalance
585 * without going over.
586 */
587 if (move_this_load < IRQ_DELTA(max_loaded,j)) {
588 move_this_load = IRQ_DELTA(max_loaded,j);
589 selected_irq = j;
590 }
591 }
592 if (selected_irq == -1) {
593 goto tryanothercpu;
594 }
595
596 imbalance = move_this_load;
597
Simon Arlott27b46d72007-10-20 01:13:56 +0200598 /* For physical_balance case, we accumulated both load
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 * values in the one of the siblings cpu_irq[],
600 * to use the same code for physical and logical processors
601 * as much as possible.
602 *
603 * NOTE: the cpu_irq[] array holds the sum of the load for
604 * sibling A and sibling B in the slot for the lowest numbered
605 * sibling (A), _AND_ the load for sibling B in the slot for
606 * the higher numbered sibling.
607 *
608 * We seek the least loaded sibling by making the comparison
609 * (A+B)/2 vs B
610 */
611 load = CPU_IRQ(min_loaded) >> 1;
Mike Travisd5a74302007-10-16 01:24:05 -0700612 for_each_cpu_mask(j, per_cpu(cpu_sibling_map, min_loaded)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (load > CPU_IRQ(j)) {
614 /* This won't change cpu_sibling_map[min_loaded] */
615 load = CPU_IRQ(j);
616 min_loaded = j;
617 }
618 }
619
Zhang Yanmin1b61b912006-06-23 02:04:22 -0700620 cpus_and(allowed_mask,
621 cpu_online_map,
622 balance_irq_affinity[selected_irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 target_cpu_mask = cpumask_of_cpu(min_loaded);
624 cpus_and(tmp, target_cpu_mask, allowed_mask);
625
626 if (!cpus_empty(tmp)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 /* mark for change destination */
Ashok Raj54d5d422005-09-06 15:16:15 -0700628 set_pending_irq(selected_irq, cpumask_of_cpu(min_loaded));
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /* Since we made a change, come back sooner to
631 * check for more variation.
632 */
633 balanced_irq_interval = max((long)MIN_BALANCED_IRQ_INTERVAL,
634 balanced_irq_interval - BALANCED_IRQ_LESS_DELTA);
635 return;
636 }
637 goto tryanotherirq;
638
639not_worth_the_effort:
640 /*
641 * if we did not find an IRQ to move, then adjust the time interval
642 * upward
643 */
644 balanced_irq_interval = min((long)MAX_BALANCED_IRQ_INTERVAL,
645 balanced_irq_interval + BALANCED_IRQ_MORE_DELTA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return;
647}
648
649static int balanced_irq(void *unused)
650{
651 int i;
652 unsigned long prev_balance_time = jiffies;
653 long time_remaining = balanced_irq_interval;
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 /* push everything to CPU 0 to give us a starting point. */
656 for (i = 0 ; i < NR_IRQS ; i++) {
Ingo Molnarcd916d32006-06-29 02:24:42 -0700657 irq_desc[i].pending_mask = cpumask_of_cpu(0);
Ashok Raj54d5d422005-09-06 15:16:15 -0700658 set_pending_irq(i, cpumask_of_cpu(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700661 set_freezable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 for ( ; ; ) {
Nishanth Aravamudan52e6e632005-09-10 00:27:26 -0700663 time_remaining = schedule_timeout_interruptible(time_remaining);
Christoph Lameter3e1d1d22005-06-24 23:13:50 -0700664 try_to_freeze();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (time_after(jiffies,
666 prev_balance_time+balanced_irq_interval)) {
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700667 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 do_irq_balance();
669 prev_balance_time = jiffies;
670 time_remaining = balanced_irq_interval;
Zwane Mwaikambof3705132005-06-25 14:54:50 -0700671 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673 }
674 return 0;
675}
676
677static int __init balanced_irq_init(void)
678{
679 int i;
680 struct cpuinfo_x86 *c;
681 cpumask_t tmp;
682
683 cpus_shift_right(tmp, cpu_online_map, 2);
684 c = &boot_cpu_data;
685 /* When not overwritten by the command line ask subarchitecture. */
686 if (irqbalance_disabled == IRQBALANCE_CHECK_ARCH)
687 irqbalance_disabled = NO_BALANCE_IRQ;
688 if (irqbalance_disabled)
689 return 0;
690
691 /* disable irqbalance completely if there is only one processor online */
692 if (num_online_cpus() < 2) {
693 irqbalance_disabled = 1;
694 return 0;
695 }
696 /*
697 * Enable physical balance only if more than 1 physical processor
698 * is present
699 */
700 if (smp_num_siblings > 1 && !cpus_empty(tmp))
701 physical_balance = 1;
702
Andrew Morton394e3902006-03-23 03:01:05 -0800703 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 irq_cpu_data[i].irq_delta = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
705 irq_cpu_data[i].last_irq = kmalloc(sizeof(unsigned long) * NR_IRQS, GFP_KERNEL);
706 if (irq_cpu_data[i].irq_delta == NULL || irq_cpu_data[i].last_irq == NULL) {
707 printk(KERN_ERR "balanced_irq_init: out of memory");
708 goto failed;
709 }
710 memset(irq_cpu_data[i].irq_delta,0,sizeof(unsigned long) * NR_IRQS);
711 memset(irq_cpu_data[i].last_irq,0,sizeof(unsigned long) * NR_IRQS);
712 }
713
714 printk(KERN_INFO "Starting balanced_irq\n");
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +0200715 if (!IS_ERR(kthread_run(balanced_irq, NULL, "kirqd")))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return 0;
Eric W. Biedermanf26d6a22007-05-02 19:27:19 +0200717 printk(KERN_ERR "balanced_irq_init: failed to spawn balanced_irq");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718failed:
KAMEZAWA Hiroyukic8912592006-03-28 01:56:39 -0800719 for_each_possible_cpu(i) {
Jesper Juhl4ae66732005-06-25 14:58:48 -0700720 kfree(irq_cpu_data[i].irq_delta);
Andrew Morton394e3902006-03-23 03:01:05 -0800721 irq_cpu_data[i].irq_delta = NULL;
Jesper Juhl4ae66732005-06-25 14:58:48 -0700722 kfree(irq_cpu_data[i].last_irq);
Andrew Morton394e3902006-03-23 03:01:05 -0800723 irq_cpu_data[i].last_irq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725 return 0;
726}
727
Andrew Mortonc2481cc2007-04-08 16:04:04 -0700728int __devinit irqbalance_disable(char *str)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
730 irqbalance_disabled = 1;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800731 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734__setup("noirqbalance", irqbalance_disable);
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736late_initcall(balanced_irq_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737#endif /* CONFIG_IRQBALANCE */
Ashok Raj54d5d422005-09-06 15:16:15 -0700738#endif /* CONFIG_SMP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740#ifndef CONFIG_SMP
Harvey Harrison75604d72008-01-30 13:31:17 +0100741void send_IPI_self(int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 unsigned int cfg;
744
745 /*
746 * Wait for idle.
747 */
748 apic_wait_icr_idle();
749 cfg = APIC_DM_FIXED | APIC_DEST_SELF | vector | APIC_DEST_LOGICAL;
750 /*
751 * Send the IPI. The write to APIC_ICR fires this off.
752 */
753 apic_write_around(APIC_ICR, cfg);
754}
755#endif /* !CONFIG_SMP */
756
757
758/*
759 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to
760 * specific CPU-side IRQs.
761 */
762
763#define MAX_PIRQS 8
764static int pirq_entries [MAX_PIRQS];
765static int pirqs_enabled;
766int skip_ioapic_setup;
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768static int __init ioapic_pirq_setup(char *str)
769{
770 int i, max;
771 int ints[MAX_PIRQS+1];
772
773 get_options(str, ARRAY_SIZE(ints), ints);
774
775 for (i = 0; i < MAX_PIRQS; i++)
776 pirq_entries[i] = -1;
777
778 pirqs_enabled = 1;
779 apic_printk(APIC_VERBOSE, KERN_INFO
780 "PIRQ redirection, working around broken MP-BIOS.\n");
781 max = MAX_PIRQS;
782 if (ints[0] < MAX_PIRQS)
783 max = ints[0];
784
785 for (i = 0; i < max; i++) {
786 apic_printk(APIC_VERBOSE, KERN_DEBUG
787 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]);
788 /*
789 * PIRQs are mapped upside down, usually.
790 */
791 pirq_entries[MAX_PIRQS-i-1] = ints[i+1];
792 }
793 return 1;
794}
795
796__setup("pirq=", ioapic_pirq_setup);
797
798/*
799 * Find the IRQ entry number of a certain pin.
800 */
801static int find_irq_entry(int apic, int pin, int type)
802{
803 int i;
804
805 for (i = 0; i < mp_irq_entries; i++)
806 if (mp_irqs[i].mpc_irqtype == type &&
807 (mp_irqs[i].mpc_dstapic == mp_ioapics[apic].mpc_apicid ||
808 mp_irqs[i].mpc_dstapic == MP_APIC_ALL) &&
809 mp_irqs[i].mpc_dstirq == pin)
810 return i;
811
812 return -1;
813}
814
815/*
816 * Find the pin to which IRQ[irq] (ISA) is connected
817 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800818static int __init find_isa_irq_pin(int irq, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
820 int i;
821
822 for (i = 0; i < mp_irq_entries; i++) {
823 int lbus = mp_irqs[i].mpc_srcbus;
824
Alexey Starikovskiyd27e2b82008-03-20 14:54:18 +0300825 if (test_bit(lbus, mp_bus_not_pci) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 (mp_irqs[i].mpc_irqtype == type) &&
827 (mp_irqs[i].mpc_srcbusirq == irq))
828
829 return mp_irqs[i].mpc_dstirq;
830 }
831 return -1;
832}
833
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800834static int __init find_isa_irq_apic(int irq, int type)
835{
836 int i;
837
838 for (i = 0; i < mp_irq_entries; i++) {
839 int lbus = mp_irqs[i].mpc_srcbus;
840
Alexey Starikovskiy73b29612008-03-20 14:54:24 +0300841 if (test_bit(lbus, mp_bus_not_pci) &&
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -0800842 (mp_irqs[i].mpc_irqtype == type) &&
843 (mp_irqs[i].mpc_srcbusirq == irq))
844 break;
845 }
846 if (i < mp_irq_entries) {
847 int apic;
848 for(apic = 0; apic < nr_ioapics; apic++) {
849 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic)
850 return apic;
851 }
852 }
853
854 return -1;
855}
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857/*
858 * Find a specific PCI IRQ entry.
859 * Not an __init, possibly needed by modules
860 */
861static int pin_2_irq(int idx, int apic, int pin);
862
863int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin)
864{
865 int apic, i, best_guess = -1;
866
867 apic_printk(APIC_DEBUG, "querying PCI -> IRQ mapping bus:%d, "
868 "slot:%d, pin:%d.\n", bus, slot, pin);
869 if (mp_bus_id_to_pci_bus[bus] == -1) {
870 printk(KERN_WARNING "PCI BIOS passed nonexistent PCI bus %d!\n", bus);
871 return -1;
872 }
873 for (i = 0; i < mp_irq_entries; i++) {
874 int lbus = mp_irqs[i].mpc_srcbus;
875
876 for (apic = 0; apic < nr_ioapics; apic++)
877 if (mp_ioapics[apic].mpc_apicid == mp_irqs[i].mpc_dstapic ||
878 mp_irqs[i].mpc_dstapic == MP_APIC_ALL)
879 break;
880
Alexey Starikovskiy47cab822008-03-20 14:54:30 +0300881 if (!test_bit(lbus, mp_bus_not_pci) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 !mp_irqs[i].mpc_irqtype &&
883 (bus == lbus) &&
884 (slot == ((mp_irqs[i].mpc_srcbusirq >> 2) & 0x1f))) {
885 int irq = pin_2_irq(i,apic,mp_irqs[i].mpc_dstirq);
886
887 if (!(apic || IO_APIC_IRQ(irq)))
888 continue;
889
890 if (pin == (mp_irqs[i].mpc_srcbusirq & 3))
891 return irq;
892 /*
893 * Use the first all-but-pin matching entry as a
894 * best-guess fuzzy result for broken mptables.
895 */
896 if (best_guess < 0)
897 best_guess = irq;
898 }
899 }
900 return best_guess;
901}
Alexey Dobriyan129f6942005-06-23 00:08:33 -0700902EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904/*
905 * This function currently is only a helper for the i386 smp boot process where
906 * we need to reprogram the ioredtbls to cater for the cpus which have come online
907 * so mask in all cases should simply be TARGET_CPUS
908 */
Ashok Raj54d5d422005-09-06 15:16:15 -0700909#ifdef CONFIG_SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910void __init setup_ioapic_dest(void)
911{
912 int pin, ioapic, irq, irq_entry;
913
914 if (skip_ioapic_setup == 1)
915 return;
916
917 for (ioapic = 0; ioapic < nr_ioapics; ioapic++) {
918 for (pin = 0; pin < nr_ioapic_registers[ioapic]; pin++) {
919 irq_entry = find_irq_entry(ioapic, pin, mp_INT);
920 if (irq_entry == -1)
921 continue;
922 irq = pin_2_irq(irq_entry, ioapic, pin);
923 set_ioapic_affinity_irq(irq, TARGET_CPUS);
924 }
925
926 }
927}
Ashok Raj54d5d422005-09-06 15:16:15 -0700928#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300930#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931/*
932 * EISA Edge/Level control register, ELCR
933 */
934static int EISA_ELCR(unsigned int irq)
935{
936 if (irq < 16) {
937 unsigned int port = 0x4d0 + (irq >> 3);
938 return (inb(port) >> (irq & 7)) & 1;
939 }
940 apic_printk(APIC_VERBOSE, KERN_INFO
941 "Broken MPtable reports ISA irq %d\n", irq);
942 return 0;
943}
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +0300944#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300946/* ISA interrupts are always polarity zero edge triggered,
947 * when listed as conforming in the MP table. */
948
949#define default_ISA_trigger(idx) (0)
950#define default_ISA_polarity(idx) (0)
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952/* EISA interrupts are always polarity zero and can be edge or level
953 * trigger depending on the ELCR value. If an interrupt is listed as
954 * EISA conforming in the MP table, that means its trigger type must
955 * be read in from the ELCR */
956
957#define default_EISA_trigger(idx) (EISA_ELCR(mp_irqs[idx].mpc_srcbusirq))
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300958#define default_EISA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960/* PCI interrupts are always polarity one level triggered,
961 * when listed as conforming in the MP table. */
962
963#define default_PCI_trigger(idx) (1)
964#define default_PCI_polarity(idx) (1)
965
966/* MCA interrupts are always polarity zero level triggered,
967 * when listed as conforming in the MP table. */
968
969#define default_MCA_trigger(idx) (1)
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300970#define default_MCA_polarity(idx) default_ISA_polarity(idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Shaohua Li61fd47e2007-11-17 01:05:28 -0500972static int MPBIOS_polarity(int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
974 int bus = mp_irqs[idx].mpc_srcbus;
975 int polarity;
976
977 /*
978 * Determine IRQ line polarity (high active or low active):
979 */
980 switch (mp_irqs[idx].mpc_irqflag & 3)
981 {
982 case 0: /* conforms, ie. bus-type dependent polarity */
983 {
Alexey Starikovskiy67288012008-03-20 14:54:36 +0300984 polarity = test_bit(bus, mp_bus_not_pci)?
985 default_ISA_polarity(idx):
986 default_PCI_polarity(idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 break;
988 }
989 case 1: /* high active */
990 {
991 polarity = 0;
992 break;
993 }
994 case 2: /* reserved */
995 {
996 printk(KERN_WARNING "broken BIOS!!\n");
997 polarity = 1;
998 break;
999 }
1000 case 3: /* low active */
1001 {
1002 polarity = 1;
1003 break;
1004 }
1005 default: /* invalid */
1006 {
1007 printk(KERN_WARNING "broken BIOS!!\n");
1008 polarity = 1;
1009 break;
1010 }
1011 }
1012 return polarity;
1013}
1014
1015static int MPBIOS_trigger(int idx)
1016{
1017 int bus = mp_irqs[idx].mpc_srcbus;
1018 int trigger;
1019
1020 /*
1021 * Determine IRQ trigger mode (edge or level sensitive):
1022 */
1023 switch ((mp_irqs[idx].mpc_irqflag>>2) & 3)
1024 {
1025 case 0: /* conforms, ie. bus-type dependent */
1026 {
Alexey Starikovskiy9c0076c2008-03-20 14:54:43 +03001027 trigger = test_bit(bus, mp_bus_not_pci)?
1028 default_ISA_trigger(idx):
1029 default_PCI_trigger(idx);
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001030#if defined(CONFIG_EISA) || defined(CONFIG_MCA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 switch (mp_bus_id_to_type[bus])
1032 {
1033 case MP_BUS_ISA: /* ISA pin */
1034 {
Alexey Starikovskiy9c0076c2008-03-20 14:54:43 +03001035 /* set before the switch */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 break;
1037 }
1038 case MP_BUS_EISA: /* EISA pin */
1039 {
1040 trigger = default_EISA_trigger(idx);
1041 break;
1042 }
1043 case MP_BUS_PCI: /* PCI pin */
1044 {
Alexey Starikovskiy9c0076c2008-03-20 14:54:43 +03001045 /* set before the switch */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 break;
1047 }
1048 case MP_BUS_MCA: /* MCA pin */
1049 {
1050 trigger = default_MCA_trigger(idx);
1051 break;
1052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 default:
1054 {
1055 printk(KERN_WARNING "broken BIOS!!\n");
1056 trigger = 1;
1057 break;
1058 }
1059 }
Alexey Starikovskiyc0a282c2008-03-20 14:55:02 +03001060#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 break;
1062 }
1063 case 1: /* edge */
1064 {
1065 trigger = 0;
1066 break;
1067 }
1068 case 2: /* reserved */
1069 {
1070 printk(KERN_WARNING "broken BIOS!!\n");
1071 trigger = 1;
1072 break;
1073 }
1074 case 3: /* level */
1075 {
1076 trigger = 1;
1077 break;
1078 }
1079 default: /* invalid */
1080 {
1081 printk(KERN_WARNING "broken BIOS!!\n");
1082 trigger = 0;
1083 break;
1084 }
1085 }
1086 return trigger;
1087}
1088
1089static inline int irq_polarity(int idx)
1090{
1091 return MPBIOS_polarity(idx);
1092}
1093
1094static inline int irq_trigger(int idx)
1095{
1096 return MPBIOS_trigger(idx);
1097}
1098
1099static int pin_2_irq(int idx, int apic, int pin)
1100{
1101 int irq, i;
1102 int bus = mp_irqs[idx].mpc_srcbus;
1103
1104 /*
1105 * Debugging check, we are in big trouble if this message pops up!
1106 */
1107 if (mp_irqs[idx].mpc_dstirq != pin)
1108 printk(KERN_ERR "broken BIOS or MPTABLE parser, ayiee!!\n");
1109
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001110 if (test_bit(bus, mp_bus_not_pci))
1111 irq = mp_irqs[idx].mpc_srcbusirq;
1112 else {
1113 /*
1114 * PCI IRQs are mapped in order
1115 */
1116 i = irq = 0;
1117 while (i < apic)
1118 irq += nr_ioapic_registers[i++];
1119 irq += pin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Alexey Starikovskiy643befe2008-03-20 14:54:49 +03001121 /*
1122 * For MPS mode, so far only needed by ES7000 platform
1123 */
1124 if (ioapic_renumber_irq)
1125 irq = ioapic_renumber_irq(apic, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
1128 /*
1129 * PCI IRQ command line redirection. Yes, limits are hardcoded.
1130 */
1131 if ((pin >= 16) && (pin <= 23)) {
1132 if (pirq_entries[pin-16] != -1) {
1133 if (!pirq_entries[pin-16]) {
1134 apic_printk(APIC_VERBOSE, KERN_DEBUG
1135 "disabling PIRQ%d\n", pin-16);
1136 } else {
1137 irq = pirq_entries[pin-16];
1138 apic_printk(APIC_VERBOSE, KERN_DEBUG
1139 "using PIRQ%d -> IRQ %d\n",
1140 pin-16, irq);
1141 }
1142 }
1143 }
1144 return irq;
1145}
1146
1147static inline int IO_APIC_irq_trigger(int irq)
1148{
1149 int apic, idx, pin;
1150
1151 for (apic = 0; apic < nr_ioapics; apic++) {
1152 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1153 idx = find_irq_entry(apic,pin,mp_INT);
1154 if ((idx != -1) && (irq == pin_2_irq(idx,apic,pin)))
1155 return irq_trigger(idx);
1156 }
1157 }
1158 /*
1159 * nonexistent IRQs are edge default
1160 */
1161 return 0;
1162}
1163
1164/* irq_vectors is indexed by the sum of all RTEs in all I/O APICs. */
Adrian Bunk7e95b592006-12-07 02:14:11 +01001165static u8 irq_vector[NR_IRQ_VECTORS] __read_mostly = { FIRST_DEVICE_VECTOR , 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001167static int __assign_irq_vector(int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
Eric W. Biederman8339f002007-01-29 13:19:05 -07001169 static int current_vector = FIRST_DEVICE_VECTOR, current_offset = 0;
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001170 int vector, offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001172 BUG_ON((unsigned)irq >= NR_IRQ_VECTORS);
Jan Beulich0a1ad602006-06-26 13:56:43 +02001173
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001174 if (irq_vector[irq] > 0)
1175 return irq_vector[irq];
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001176
Jan Beulich0a1ad602006-06-26 13:56:43 +02001177 vector = current_vector;
Eric W. Biederman8339f002007-01-29 13:19:05 -07001178 offset = current_offset;
1179next:
1180 vector += 8;
1181 if (vector >= FIRST_SYSTEM_VECTOR) {
1182 offset = (offset + 1) % 8;
1183 vector = FIRST_DEVICE_VECTOR + offset;
1184 }
1185 if (vector == current_vector)
1186 return -ENOSPC;
Rusty Russelldbeb2be2007-10-19 20:35:03 +02001187 if (test_and_set_bit(vector, used_vectors))
Eric W. Biederman8339f002007-01-29 13:19:05 -07001188 goto next;
Eric W. Biederman8339f002007-01-29 13:19:05 -07001189
1190 current_vector = vector;
1191 current_offset = offset;
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001192 irq_vector[irq] = vector;
Jan Beulich0a1ad602006-06-26 13:56:43 +02001193
1194 return vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001197static int assign_irq_vector(int irq)
1198{
1199 unsigned long flags;
1200 int vector;
1201
1202 spin_lock_irqsave(&vector_lock, flags);
1203 vector = __assign_irq_vector(irq);
1204 spin_unlock_irqrestore(&vector_lock, flags);
1205
1206 return vector;
1207}
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001208static struct irq_chip ioapic_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210#define IOAPIC_AUTO -1
1211#define IOAPIC_EDGE 0
1212#define IOAPIC_LEVEL 1
1213
Ingo Molnard1bef4e2006-06-29 02:24:36 -07001214static void ioapic_register_intr(int irq, int vector, unsigned long trigger)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Jan Beulich6ebcc002006-06-26 13:56:46 +02001216 if ((trigger == IOAPIC_AUTO && IO_APIC_irq_trigger(irq)) ||
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001217 trigger == IOAPIC_LEVEL) {
1218 irq_desc[irq].status |= IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -07001219 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1220 handle_fasteoi_irq, "fasteoi");
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001221 } else {
1222 irq_desc[irq].status &= ~IRQ_LEVEL;
Ingo Molnara460e742006-10-17 00:10:03 -07001223 set_irq_chip_and_handler_name(irq, &ioapic_chip,
1224 handle_edge_irq, "edge");
Thomas Gleixnercc75b922007-08-12 15:46:36 +00001225 }
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001226 set_intr_gate(vector, interrupt[irq]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
1229static void __init setup_IO_APIC_irqs(void)
1230{
1231 struct IO_APIC_route_entry entry;
1232 int apic, pin, idx, irq, first_notcon = 1, vector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n");
1235
1236 for (apic = 0; apic < nr_ioapics; apic++) {
1237 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
1238
1239 /*
1240 * add it to the IO-APIC irq-routing table:
1241 */
1242 memset(&entry,0,sizeof(entry));
1243
1244 entry.delivery_mode = INT_DELIVERY_MODE;
1245 entry.dest_mode = INT_DEST_MODE;
1246 entry.mask = 0; /* enable IRQ */
1247 entry.dest.logical.logical_dest =
1248 cpu_mask_to_apicid(TARGET_CPUS);
1249
1250 idx = find_irq_entry(apic,pin,mp_INT);
1251 if (idx == -1) {
1252 if (first_notcon) {
1253 apic_printk(APIC_VERBOSE, KERN_DEBUG
1254 " IO-APIC (apicid-pin) %d-%d",
1255 mp_ioapics[apic].mpc_apicid,
1256 pin);
1257 first_notcon = 0;
1258 } else
1259 apic_printk(APIC_VERBOSE, ", %d-%d",
1260 mp_ioapics[apic].mpc_apicid, pin);
1261 continue;
1262 }
1263
Yinghai Lu20d225b2007-10-17 18:04:41 +02001264 if (!first_notcon) {
1265 apic_printk(APIC_VERBOSE, " not connected.\n");
1266 first_notcon = 1;
1267 }
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 entry.trigger = irq_trigger(idx);
1270 entry.polarity = irq_polarity(idx);
1271
1272 if (irq_trigger(idx)) {
1273 entry.trigger = 1;
1274 entry.mask = 1;
1275 }
1276
1277 irq = pin_2_irq(idx, apic, pin);
1278 /*
1279 * skip adding the timer int on secondary nodes, which causes
1280 * a small but painful rift in the time-space continuum
1281 */
1282 if (multi_timer_check(apic, irq))
1283 continue;
1284 else
1285 add_pin_to_irq(irq, apic, pin);
1286
1287 if (!apic && !IO_APIC_IRQ(irq))
1288 continue;
1289
1290 if (IO_APIC_IRQ(irq)) {
1291 vector = assign_irq_vector(irq);
1292 entry.vector = vector;
1293 ioapic_register_intr(irq, vector, IOAPIC_AUTO);
1294
1295 if (!apic && (irq < 16))
1296 disable_8259A_irq(irq);
1297 }
Akinobu Mitaa2249cb2008-04-05 22:39:05 +09001298 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300 }
1301
1302 if (!first_notcon)
1303 apic_printk(APIC_VERBOSE, " not connected.\n");
1304}
1305
1306/*
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001307 * Set up the timer pin, possibly with the 8259A-master behind.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 */
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001309static void __init setup_timer_IRQ0_pin(unsigned int apic, unsigned int pin,
1310 int vector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311{
1312 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
1314 memset(&entry,0,sizeof(entry));
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 /*
1317 * We use logical delivery to get the timer IRQ
1318 * to the first CPU.
1319 */
1320 entry.dest_mode = INT_DEST_MODE;
Maciej W. Rozycki03be7502008-05-27 21:19:45 +01001321 entry.mask = 1; /* mask IRQ now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
1323 entry.delivery_mode = INT_DELIVERY_MODE;
1324 entry.polarity = 0;
1325 entry.trigger = 0;
1326 entry.vector = vector;
1327
1328 /*
1329 * The timer IRQ doesn't have to know that behind the
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01001330 * scene we may have a 8259A-master in AEOI mode ...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 */
Maciej W. Rozyckif0825262008-05-27 21:19:16 +01001332 ioapic_register_intr(0, vector, IOAPIC_EDGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 /*
1335 * Add it to the IO-APIC irq-routing table:
1336 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001337 ioapic_write_entry(apic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340void __init print_IO_APIC(void)
1341{
1342 int apic, i;
1343 union IO_APIC_reg_00 reg_00;
1344 union IO_APIC_reg_01 reg_01;
1345 union IO_APIC_reg_02 reg_02;
1346 union IO_APIC_reg_03 reg_03;
1347 unsigned long flags;
1348
1349 if (apic_verbosity == APIC_QUIET)
1350 return;
1351
1352 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries);
1353 for (i = 0; i < nr_ioapics; i++)
1354 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n",
1355 mp_ioapics[i].mpc_apicid, nr_ioapic_registers[i]);
1356
1357 /*
1358 * We are a bit conservative about what we expect. We have to
1359 * know about every hardware change ASAP.
1360 */
1361 printk(KERN_INFO "testing the IO APIC.......................\n");
1362
1363 for (apic = 0; apic < nr_ioapics; apic++) {
1364
1365 spin_lock_irqsave(&ioapic_lock, flags);
1366 reg_00.raw = io_apic_read(apic, 0);
1367 reg_01.raw = io_apic_read(apic, 1);
1368 if (reg_01.bits.version >= 0x10)
1369 reg_02.raw = io_apic_read(apic, 2);
1370 if (reg_01.bits.version >= 0x20)
1371 reg_03.raw = io_apic_read(apic, 3);
1372 spin_unlock_irqrestore(&ioapic_lock, flags);
1373
1374 printk(KERN_DEBUG "IO APIC #%d......\n", mp_ioapics[apic].mpc_apicid);
1375 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw);
1376 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID);
1377 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type);
1378 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 printk(KERN_DEBUG ".... register #01: %08X\n", reg_01.raw);
1381 printk(KERN_DEBUG "....... : max redirection entries: %04X\n", reg_01.bits.entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ);
1384 printk(KERN_DEBUG "....... : IO APIC version: %04X\n", reg_01.bits.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 /*
1387 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02,
1388 * but the value of reg_02 is read as the previous read register
1389 * value, so ignore it if reg_02 == reg_01.
1390 */
1391 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) {
1392 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw);
1393 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395
1396 /*
1397 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02
1398 * or reg_03, but the value of reg_0[23] is read as the previous read
1399 * register value, so ignore it if reg_03 == reg_0[12].
1400 */
1401 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw &&
1402 reg_03.raw != reg_01.raw) {
1403 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw);
1404 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406
1407 printk(KERN_DEBUG ".... IRQ redirection table:\n");
1408
1409 printk(KERN_DEBUG " NR Log Phy Mask Trig IRR Pol"
1410 " Stat Dest Deli Vect: \n");
1411
1412 for (i = 0; i <= reg_01.bits.entries; i++) {
1413 struct IO_APIC_route_entry entry;
1414
Andi Kleencf4c6a22006-09-26 10:52:30 +02001415 entry = ioapic_read_entry(apic, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 printk(KERN_DEBUG " %02x %03X %02X ",
1418 i,
1419 entry.dest.logical.logical_dest,
1420 entry.dest.physical.physical_dest
1421 );
1422
1423 printk("%1d %1d %1d %1d %1d %1d %1d %02X\n",
1424 entry.mask,
1425 entry.trigger,
1426 entry.irr,
1427 entry.polarity,
1428 entry.delivery_status,
1429 entry.dest_mode,
1430 entry.delivery_mode,
1431 entry.vector
1432 );
1433 }
1434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 printk(KERN_DEBUG "IRQ to pin mappings:\n");
1436 for (i = 0; i < NR_IRQS; i++) {
1437 struct irq_pin_list *entry = irq_2_pin + i;
1438 if (entry->pin < 0)
1439 continue;
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001440 printk(KERN_DEBUG "IRQ%d ", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 for (;;) {
1442 printk("-> %d:%d", entry->apic, entry->pin);
1443 if (!entry->next)
1444 break;
1445 entry = irq_2_pin + entry->next;
1446 }
1447 printk("\n");
1448 }
1449
1450 printk(KERN_INFO ".................................... done.\n");
1451
1452 return;
1453}
1454
1455#if 0
1456
1457static void print_APIC_bitfield (int base)
1458{
1459 unsigned int v;
1460 int i, j;
1461
1462 if (apic_verbosity == APIC_QUIET)
1463 return;
1464
1465 printk(KERN_DEBUG "0123456789abcdef0123456789abcdef\n" KERN_DEBUG);
1466 for (i = 0; i < 8; i++) {
1467 v = apic_read(base + i*0x10);
1468 for (j = 0; j < 32; j++) {
1469 if (v & (1<<j))
1470 printk("1");
1471 else
1472 printk("0");
1473 }
1474 printk("\n");
1475 }
1476}
1477
1478void /*__init*/ print_local_APIC(void * dummy)
1479{
1480 unsigned int v, ver, maxlvt;
1481
1482 if (apic_verbosity == APIC_QUIET)
1483 return;
1484
1485 printk("\n" KERN_DEBUG "printing local APIC contents on CPU#%d/%d:\n",
1486 smp_processor_id(), hard_smp_processor_id());
Jack Steiner05f2d122008-03-28 14:12:02 -05001487 printk(KERN_INFO "... APIC ID: %08x (%01x)\n", v,
1488 GET_APIC_ID(read_apic_id()));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 v = apic_read(APIC_LVR);
1490 printk(KERN_INFO "... APIC VERSION: %08x\n", v);
1491 ver = GET_APIC_VERSION(v);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001492 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 v = apic_read(APIC_TASKPRI);
1495 printk(KERN_DEBUG "... APIC TASKPRI: %08x (%02x)\n", v, v & APIC_TPRI_MASK);
1496
1497 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1498 v = apic_read(APIC_ARBPRI);
1499 printk(KERN_DEBUG "... APIC ARBPRI: %08x (%02x)\n", v,
1500 v & APIC_ARBPRI_MASK);
1501 v = apic_read(APIC_PROCPRI);
1502 printk(KERN_DEBUG "... APIC PROCPRI: %08x\n", v);
1503 }
1504
1505 v = apic_read(APIC_EOI);
1506 printk(KERN_DEBUG "... APIC EOI: %08x\n", v);
1507 v = apic_read(APIC_RRR);
1508 printk(KERN_DEBUG "... APIC RRR: %08x\n", v);
1509 v = apic_read(APIC_LDR);
1510 printk(KERN_DEBUG "... APIC LDR: %08x\n", v);
1511 v = apic_read(APIC_DFR);
1512 printk(KERN_DEBUG "... APIC DFR: %08x\n", v);
1513 v = apic_read(APIC_SPIV);
1514 printk(KERN_DEBUG "... APIC SPIV: %08x\n", v);
1515
1516 printk(KERN_DEBUG "... APIC ISR field:\n");
1517 print_APIC_bitfield(APIC_ISR);
1518 printk(KERN_DEBUG "... APIC TMR field:\n");
1519 print_APIC_bitfield(APIC_TMR);
1520 printk(KERN_DEBUG "... APIC IRR field:\n");
1521 print_APIC_bitfield(APIC_IRR);
1522
1523 if (APIC_INTEGRATED(ver)) { /* !82489DX */
1524 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1525 apic_write(APIC_ESR, 0);
1526 v = apic_read(APIC_ESR);
1527 printk(KERN_DEBUG "... APIC ESR: %08x\n", v);
1528 }
1529
1530 v = apic_read(APIC_ICR);
1531 printk(KERN_DEBUG "... APIC ICR: %08x\n", v);
1532 v = apic_read(APIC_ICR2);
1533 printk(KERN_DEBUG "... APIC ICR2: %08x\n", v);
1534
1535 v = apic_read(APIC_LVTT);
1536 printk(KERN_DEBUG "... APIC LVTT: %08x\n", v);
1537
1538 if (maxlvt > 3) { /* PC is LVT#4. */
1539 v = apic_read(APIC_LVTPC);
1540 printk(KERN_DEBUG "... APIC LVTPC: %08x\n", v);
1541 }
1542 v = apic_read(APIC_LVT0);
1543 printk(KERN_DEBUG "... APIC LVT0: %08x\n", v);
1544 v = apic_read(APIC_LVT1);
1545 printk(KERN_DEBUG "... APIC LVT1: %08x\n", v);
1546
1547 if (maxlvt > 2) { /* ERR is LVT#3. */
1548 v = apic_read(APIC_LVTERR);
1549 printk(KERN_DEBUG "... APIC LVTERR: %08x\n", v);
1550 }
1551
1552 v = apic_read(APIC_TMICT);
1553 printk(KERN_DEBUG "... APIC TMICT: %08x\n", v);
1554 v = apic_read(APIC_TMCCT);
1555 printk(KERN_DEBUG "... APIC TMCCT: %08x\n", v);
1556 v = apic_read(APIC_TDCR);
1557 printk(KERN_DEBUG "... APIC TDCR: %08x\n", v);
1558 printk("\n");
1559}
1560
1561void print_all_local_APICs (void)
1562{
1563 on_each_cpu(print_local_APIC, NULL, 1, 1);
1564}
1565
1566void /*__init*/ print_PIC(void)
1567{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 unsigned int v;
1569 unsigned long flags;
1570
1571 if (apic_verbosity == APIC_QUIET)
1572 return;
1573
1574 printk(KERN_DEBUG "\nprinting PIC contents\n");
1575
1576 spin_lock_irqsave(&i8259A_lock, flags);
1577
1578 v = inb(0xa1) << 8 | inb(0x21);
1579 printk(KERN_DEBUG "... PIC IMR: %04x\n", v);
1580
1581 v = inb(0xa0) << 8 | inb(0x20);
1582 printk(KERN_DEBUG "... PIC IRR: %04x\n", v);
1583
1584 outb(0x0b,0xa0);
1585 outb(0x0b,0x20);
1586 v = inb(0xa0) << 8 | inb(0x20);
1587 outb(0x0a,0xa0);
1588 outb(0x0a,0x20);
1589
1590 spin_unlock_irqrestore(&i8259A_lock, flags);
1591
1592 printk(KERN_DEBUG "... PIC ISR: %04x\n", v);
1593
1594 v = inb(0x4d1) << 8 | inb(0x4d0);
1595 printk(KERN_DEBUG "... PIC ELCR: %04x\n", v);
1596}
1597
1598#endif /* 0 */
1599
1600static void __init enable_IO_APIC(void)
1601{
1602 union IO_APIC_reg_01 reg_01;
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001603 int i8259_apic, i8259_pin;
1604 int i, apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 unsigned long flags;
1606
1607 for (i = 0; i < PIN_MAP_SIZE; i++) {
1608 irq_2_pin[i].pin = -1;
1609 irq_2_pin[i].next = 0;
1610 }
1611 if (!pirqs_enabled)
1612 for (i = 0; i < MAX_PIRQS; i++)
1613 pirq_entries[i] = -1;
1614
1615 /*
1616 * The number of IO-APIC IRQ registers (== #pins):
1617 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001618 for (apic = 0; apic < nr_ioapics; apic++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 spin_lock_irqsave(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001620 reg_01.raw = io_apic_read(apic, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 spin_unlock_irqrestore(&ioapic_lock, flags);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001622 nr_ioapic_registers[apic] = reg_01.bits.entries+1;
1623 }
1624 for(apic = 0; apic < nr_ioapics; apic++) {
1625 int pin;
1626 /* See if any of the pins is in ExtINT mode */
Eric W. Biederman1008fdd2006-01-11 22:46:06 +01001627 for (pin = 0; pin < nr_ioapic_registers[apic]; pin++) {
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001628 struct IO_APIC_route_entry entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02001629 entry = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001630
1631
1632 /* If the interrupt line is enabled and in ExtInt mode
1633 * I have found the pin where the i8259 is connected.
1634 */
1635 if ((entry.mask == 0) && (entry.delivery_mode == dest_ExtINT)) {
1636 ioapic_i8259.apic = apic;
1637 ioapic_i8259.pin = pin;
1638 goto found_i8259;
1639 }
1640 }
1641 }
1642 found_i8259:
1643 /* Look to see what if the MP table has reported the ExtINT */
1644 /* If we could not find the appropriate pin by looking at the ioapic
1645 * the i8259 probably is not connected the ioapic but give the
1646 * mptable a chance anyway.
1647 */
1648 i8259_pin = find_isa_irq_pin(0, mp_ExtINT);
1649 i8259_apic = find_isa_irq_apic(0, mp_ExtINT);
1650 /* Trust the MP table if nothing is setup in the hardware */
1651 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) {
1652 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n");
1653 ioapic_i8259.pin = i8259_pin;
1654 ioapic_i8259.apic = i8259_apic;
1655 }
1656 /* Complain if the MP table and the hardware disagree */
1657 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) &&
1658 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0))
1659 {
1660 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662
1663 /*
1664 * Do not trust the IO-APIC being empty at bootup
1665 */
1666 clear_IO_APIC();
1667}
1668
1669/*
1670 * Not an __init, needed by the reboot code
1671 */
1672void disable_IO_APIC(void)
1673{
1674 /*
1675 * Clear the IO-APIC before rebooting:
1676 */
1677 clear_IO_APIC();
1678
Eric W. Biederman650927e2005-06-25 14:57:44 -07001679 /*
Karsten Wiese0b968d22005-09-09 12:59:04 +02001680 * If the i8259 is routed through an IOAPIC
Eric W. Biederman650927e2005-06-25 14:57:44 -07001681 * Put that IOAPIC in virtual wire mode
Karsten Wiese0b968d22005-09-09 12:59:04 +02001682 * so legacy interrupts can be delivered.
Eric W. Biederman650927e2005-06-25 14:57:44 -07001683 */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001684 if (ioapic_i8259.pin != -1) {
Eric W. Biederman650927e2005-06-25 14:57:44 -07001685 struct IO_APIC_route_entry entry;
Eric W. Biederman650927e2005-06-25 14:57:44 -07001686
1687 memset(&entry, 0, sizeof(entry));
1688 entry.mask = 0; /* Enabled */
1689 entry.trigger = 0; /* Edge */
1690 entry.irr = 0;
1691 entry.polarity = 0; /* High */
1692 entry.delivery_status = 0;
1693 entry.dest_mode = 0; /* Physical */
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001694 entry.delivery_mode = dest_ExtINT; /* ExtInt */
Eric W. Biederman650927e2005-06-25 14:57:44 -07001695 entry.vector = 0;
Vivek Goyal76865c32006-01-06 00:12:19 -08001696 entry.dest.physical.physical_dest =
Jack Steiner05f2d122008-03-28 14:12:02 -05001697 GET_APIC_ID(read_apic_id());
Eric W. Biederman650927e2005-06-25 14:57:44 -07001698
1699 /*
1700 * Add it to the IO-APIC irq-routing table:
1701 */
Andi Kleencf4c6a22006-09-26 10:52:30 +02001702 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry);
Eric W. Biederman650927e2005-06-25 14:57:44 -07001703 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08001704 disconnect_bsp_APIC(ioapic_i8259.pin != -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705}
1706
1707/*
1708 * function to set the IO-APIC physical IDs based on the
1709 * values stored in the MPC table.
1710 *
1711 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999
1712 */
1713
1714#ifndef CONFIG_X86_NUMAQ
1715static void __init setup_ioapic_ids_from_mpc(void)
1716{
1717 union IO_APIC_reg_00 reg_00;
1718 physid_mask_t phys_id_present_map;
1719 int apic;
1720 int i;
1721 unsigned char old_id;
1722 unsigned long flags;
1723
1724 /*
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001725 * Don't check I/O APIC IDs for xAPIC systems. They have
1726 * no meaning without the serial APIC bus.
1727 */
Shaohua Li7c5c1e42006-03-23 02:59:53 -08001728 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
1729 || APIC_XAPIC(apic_version[boot_cpu_physical_apicid]))
Natalie Protasevichca05fea2005-06-23 00:08:22 -07001730 return;
1731 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 * This is broken; anything with a real cpu count has to
1733 * circumvent this idiocy regardless.
1734 */
1735 phys_id_present_map = ioapic_phys_id_map(phys_cpu_present_map);
1736
1737 /*
1738 * Set the IOAPIC ID to the value stored in the MPC table.
1739 */
1740 for (apic = 0; apic < nr_ioapics; apic++) {
1741
1742 /* Read the register 0 value */
1743 spin_lock_irqsave(&ioapic_lock, flags);
1744 reg_00.raw = io_apic_read(apic, 0);
1745 spin_unlock_irqrestore(&ioapic_lock, flags);
1746
1747 old_id = mp_ioapics[apic].mpc_apicid;
1748
1749 if (mp_ioapics[apic].mpc_apicid >= get_physical_broadcast()) {
1750 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID is %d in the MPC table!...\n",
1751 apic, mp_ioapics[apic].mpc_apicid);
1752 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1753 reg_00.bits.ID);
1754 mp_ioapics[apic].mpc_apicid = reg_00.bits.ID;
1755 }
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 /*
1758 * Sanity check, is the ID really free? Every APIC in a
1759 * system must have a unique ID or we get lots of nice
1760 * 'stuck on smp_invalidate_needed IPI wait' messages.
1761 */
1762 if (check_apicid_used(phys_id_present_map,
1763 mp_ioapics[apic].mpc_apicid)) {
1764 printk(KERN_ERR "BIOS bug, IO-APIC#%d ID %d is already used!...\n",
1765 apic, mp_ioapics[apic].mpc_apicid);
1766 for (i = 0; i < get_physical_broadcast(); i++)
1767 if (!physid_isset(i, phys_id_present_map))
1768 break;
1769 if (i >= get_physical_broadcast())
1770 panic("Max APIC ID exceeded!\n");
1771 printk(KERN_ERR "... fixing up to %d. (tell your hw vendor)\n",
1772 i);
1773 physid_set(i, phys_id_present_map);
1774 mp_ioapics[apic].mpc_apicid = i;
1775 } else {
1776 physid_mask_t tmp;
1777 tmp = apicid_to_cpu_present(mp_ioapics[apic].mpc_apicid);
1778 apic_printk(APIC_VERBOSE, "Setting %d in the "
1779 "phys_id_present_map\n",
1780 mp_ioapics[apic].mpc_apicid);
1781 physids_or(phys_id_present_map, phys_id_present_map, tmp);
1782 }
1783
1784
1785 /*
1786 * We need to adjust the IRQ routing table
1787 * if the ID changed.
1788 */
1789 if (old_id != mp_ioapics[apic].mpc_apicid)
1790 for (i = 0; i < mp_irq_entries; i++)
1791 if (mp_irqs[i].mpc_dstapic == old_id)
1792 mp_irqs[i].mpc_dstapic
1793 = mp_ioapics[apic].mpc_apicid;
1794
1795 /*
1796 * Read the right value from the MPC table and
1797 * write it into the ID register.
1798 */
1799 apic_printk(APIC_VERBOSE, KERN_INFO
1800 "...changing IO-APIC physical APIC ID to %d ...",
1801 mp_ioapics[apic].mpc_apicid);
1802
1803 reg_00.bits.ID = mp_ioapics[apic].mpc_apicid;
1804 spin_lock_irqsave(&ioapic_lock, flags);
1805 io_apic_write(apic, 0, reg_00.raw);
1806 spin_unlock_irqrestore(&ioapic_lock, flags);
1807
1808 /*
1809 * Sanity check
1810 */
1811 spin_lock_irqsave(&ioapic_lock, flags);
1812 reg_00.raw = io_apic_read(apic, 0);
1813 spin_unlock_irqrestore(&ioapic_lock, flags);
1814 if (reg_00.bits.ID != mp_ioapics[apic].mpc_apicid)
1815 printk("could not set ID!\n");
1816 else
1817 apic_printk(APIC_VERBOSE, " ok.\n");
1818 }
1819}
1820#else
1821static void __init setup_ioapic_ids_from_mpc(void) { }
1822#endif
1823
Zachary Amsden7ce0bcf2007-02-13 13:26:21 +01001824int no_timer_check __initdata;
Zachary Amsden8542b202006-12-07 02:14:09 +01001825
1826static int __init notimercheck(char *s)
1827{
1828 no_timer_check = 1;
1829 return 1;
1830}
1831__setup("no_timer_check", notimercheck);
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833/*
1834 * There is a nasty bug in some older SMP boards, their mptable lies
1835 * about the timer IRQ. We do the following to work around the situation:
1836 *
1837 * - timer IRQ defaults to IO-APIC IRQ
1838 * - if this function detects that timer IRQs are defunct, then we fall
1839 * back to ISA timer IRQs
1840 */
Adrian Bunkf0a7a5c2007-07-21 17:10:29 +02001841static int __init timer_irq_works(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842{
1843 unsigned long t1 = jiffies;
Ingo Molnar4aae0702007-12-18 18:05:58 +01001844 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845
Zachary Amsden8542b202006-12-07 02:14:09 +01001846 if (no_timer_check)
1847 return 1;
1848
Ingo Molnar4aae0702007-12-18 18:05:58 +01001849 local_save_flags(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 local_irq_enable();
1851 /* Let ten ticks pass... */
1852 mdelay((10 * 1000) / HZ);
Ingo Molnar4aae0702007-12-18 18:05:58 +01001853 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855 /*
1856 * Expect a few ticks at least, to be sure some possible
1857 * glue logic does not lock up after one or two first
1858 * ticks in a non-ExtINT mode. Also the local APIC
1859 * might have cached one ExtINT interrupt. Finally, at
1860 * least one tick may be lost due to delays.
1861 */
Julia Lawall1d16b532008-01-30 13:32:19 +01001862 if (time_after(jiffies, t1 + 4))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 return 1;
1864
1865 return 0;
1866}
1867
1868/*
1869 * In the SMP+IOAPIC case it might happen that there are an unspecified
1870 * number of pending IRQ events unhandled. These cases are very rare,
1871 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much
1872 * better to do it this way as thus we do not have to be aware of
1873 * 'pending' interrupts in the IRQ path, except at this point.
1874 */
1875/*
1876 * Edge triggered needs to resend any interrupt
1877 * that was delayed but this is now handled in the device
1878 * independent code.
1879 */
1880
1881/*
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001882 * Startup quirk:
1883 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 * Starting up a edge-triggered IO-APIC interrupt is
1885 * nasty - we need to make sure that we get the edge.
1886 * If it is already asserted for some reason, we need
1887 * return 1 to indicate that is was pending.
1888 *
1889 * This is not complete - we should be able to fake
1890 * an edge even if it isn't on the 8259A...
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001891 *
1892 * (We do this for level-triggered IRQs too - it cannot hurt.)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001894static unsigned int startup_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
1896 int was_pending = 0;
1897 unsigned long flags;
1898
1899 spin_lock_irqsave(&ioapic_lock, flags);
1900 if (irq < 16) {
1901 disable_8259A_irq(irq);
1902 if (i8259A_irq_pending(irq))
1903 was_pending = 1;
1904 }
1905 __unmask_IO_APIC_irq(irq);
1906 spin_unlock_irqrestore(&ioapic_lock, flags);
1907
1908 return was_pending;
1909}
1910
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001911static void ack_ioapic_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001913 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 ack_APIC_irq();
1915}
1916
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001917static void ack_ioapic_quirk_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
1919 unsigned long v;
1920 int i;
1921
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001922 move_native_irq(irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923/*
1924 * It appears there is an erratum which affects at least version 0x11
1925 * of I/O APIC (that's the 82093AA and cores integrated into various
1926 * chipsets). Under certain conditions a level-triggered interrupt is
1927 * erroneously delivered as edge-triggered one but the respective IRR
1928 * bit gets set nevertheless. As a result the I/O unit expects an EOI
1929 * message but it will never arrive and further interrupts are blocked
1930 * from the source. The exact reason is so far unknown, but the
1931 * phenomenon was observed when two consecutive interrupt requests
1932 * from a given source get delivered to the same CPU and the source is
1933 * temporarily disabled in between.
1934 *
1935 * A workaround is to simulate an EOI message manually. We achieve it
1936 * by setting the trigger mode to edge and then to level when the edge
1937 * trigger mode gets detected in the TMR of a local APIC for a
1938 * level-triggered interrupt. We mask the source for the time of the
1939 * operation to prevent an edge-triggered interrupt escaping meanwhile.
1940 * The idea is from Manfred Spraul. --macro
1941 */
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001942 i = irq_vector[irq];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1));
1945
1946 ack_APIC_irq();
1947
1948 if (!(v & (1 << (i & 0x1f)))) {
1949 atomic_inc(&irq_mis_count);
1950 spin_lock(&ioapic_lock);
1951 __mask_and_edge_IO_APIC_irq(irq);
1952 __unmask_and_level_IO_APIC_irq(irq);
1953 spin_unlock(&ioapic_lock);
1954 }
1955}
1956
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001957static int ioapic_retrigger_irq(unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
Eric W. Biedermanb940d222006-10-08 07:43:46 -06001959 send_IPI_self(irq_vector[irq]);
Ingo Molnarc0ad90a2006-06-29 02:24:44 -07001960
1961 return 1;
1962}
1963
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07001964static struct irq_chip ioapic_chip __read_mostly = {
1965 .name = "IO-APIC",
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001966 .startup = startup_ioapic_irq,
1967 .mask = mask_IO_APIC_irq,
1968 .unmask = unmask_IO_APIC_irq,
1969 .ack = ack_ioapic_irq,
1970 .eoi = ack_ioapic_quirk_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07001971#ifdef CONFIG_SMP
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001972 .set_affinity = set_ioapic_affinity_irq,
Ashok Raj54d5d422005-09-06 15:16:15 -07001973#endif
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07001974 .retrigger = ioapic_retrigger_irq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975};
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978static inline void init_IO_APIC_traps(void)
1979{
1980 int irq;
1981
1982 /*
1983 * NOTE! The local APIC isn't very good at handling
1984 * multiple interrupts at the same interrupt level.
1985 * As the interrupt level is determined by taking the
1986 * vector number and shifting that right by 4, we
1987 * want to spread these out a bit so that they don't
1988 * all fall in the same interrupt level.
1989 *
1990 * Also, we've got to be careful not to trash gate
1991 * 0x80, because int 0x80 is hm, kind of importantish. ;)
1992 */
1993 for (irq = 0; irq < NR_IRQS ; irq++) {
Akinobu Mitaaddfc662008-04-05 22:39:07 +09001994 if (IO_APIC_IRQ(irq) && !irq_vector[irq]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 /*
1996 * Hmm.. We don't have an entry for this,
1997 * so default to an old-fashioned 8259
1998 * interrupt if we can..
1999 */
2000 if (irq < 16)
2001 make_8259A_irq(irq);
2002 else
2003 /* Strange. Oh, well.. */
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002004 irq_desc[irq].chip = &no_irq_chip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
2006 }
2007}
2008
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002009/*
2010 * The local APIC irq-chip implementation:
2011 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002013static void ack_apic(unsigned int irq)
2014{
2015 ack_APIC_irq();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
2017
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002018static void mask_lapic_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
2020 unsigned long v;
2021
2022 v = apic_read(APIC_LVT0);
2023 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
2024}
2025
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002026static void unmask_lapic_irq (unsigned int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027{
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002028 unsigned long v;
2029
2030 v = apic_read(APIC_LVT0);
2031 apic_write_around(APIC_LVT0, v & ~APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002034static struct irq_chip lapic_chip __read_mostly = {
Maciej W. Rozycki9a1c6192008-05-27 21:19:09 +01002035 .name = "local-APIC",
Ingo Molnarf5b9ed72006-10-04 02:16:26 -07002036 .mask = mask_lapic_irq,
2037 .unmask = unmask_lapic_irq,
2038 .eoi = ack_apic,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039};
2040
Jan Beuliche9427102008-01-30 13:31:24 +01002041static void __init setup_nmi(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042{
2043 /*
2044 * Dirty trick to enable the NMI watchdog ...
2045 * We put the 8259A master into AEOI mode and
2046 * unmask on all local APICs LVT0 as NMI.
2047 *
2048 * The idea to use the 8259A in AEOI mode ('8259A Virtual Wire')
2049 * is from Maciej W. Rozycki - so we do not have to EOI from
2050 * the NMI handler or the timer interrupt.
2051 */
2052 apic_printk(APIC_VERBOSE, KERN_INFO "activating NMI Watchdog ...");
2053
Jan Beuliche9427102008-01-30 13:31:24 +01002054 enable_NMI_through_LVT0();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
2056 apic_printk(APIC_VERBOSE, " done.\n");
2057}
2058
2059/*
2060 * This looks a bit hackish but it's about the only one way of sending
2061 * a few INTA cycles to 8259As and any associated glue logic. ICR does
2062 * not support the ExtINT mode, unfortunately. We need to send these
2063 * cycles as some i82489DX-based boards have glue logic that keeps the
2064 * 8259A interrupt line asserted until INTA. --macro
2065 */
Jacek Luczak28acf282008-04-12 17:41:12 +02002066static inline void __init unlock_ExtINT_logic(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002068 int apic, pin, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 struct IO_APIC_route_entry entry0, entry1;
2070 unsigned char save_control, save_freq_select;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002072 pin = find_isa_irq_pin(8, mp_INT);
Adrian Bunk956fb532006-12-07 02:14:11 +01002073 if (pin == -1) {
2074 WARN_ON_ONCE(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return;
Adrian Bunk956fb532006-12-07 02:14:11 +01002076 }
2077 apic = find_isa_irq_apic(8, mp_INT);
2078 if (apic == -1) {
2079 WARN_ON_ONCE(1);
2080 return;
2081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Andi Kleencf4c6a22006-09-26 10:52:30 +02002083 entry0 = ioapic_read_entry(apic, pin);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002084 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 memset(&entry1, 0, sizeof(entry1));
2087
2088 entry1.dest_mode = 0; /* physical delivery */
2089 entry1.mask = 0; /* unmask IRQ now */
2090 entry1.dest.physical.physical_dest = hard_smp_processor_id();
2091 entry1.delivery_mode = dest_ExtINT;
2092 entry1.polarity = entry0.polarity;
2093 entry1.trigger = 0;
2094 entry1.vector = 0;
2095
Andi Kleencf4c6a22006-09-26 10:52:30 +02002096 ioapic_write_entry(apic, pin, entry1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097
2098 save_control = CMOS_READ(RTC_CONTROL);
2099 save_freq_select = CMOS_READ(RTC_FREQ_SELECT);
2100 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6,
2101 RTC_FREQ_SELECT);
2102 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL);
2103
2104 i = 100;
2105 while (i-- > 0) {
2106 mdelay(10);
2107 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF)
2108 i -= 10;
2109 }
2110
2111 CMOS_WRITE(save_control, RTC_CONTROL);
2112 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002113 clear_IO_APIC_pin(apic, pin);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114
Andi Kleencf4c6a22006-09-26 10:52:30 +02002115 ioapic_write_entry(apic, pin, entry0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
2118/*
2119 * This code may look a bit paranoid, but it's supposed to cooperate with
2120 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ
2121 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast
2122 * fanatically on his truly buggy board.
2123 */
Zachary Amsden8542b202006-12-07 02:14:09 +01002124static inline void __init check_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125{
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002126 int apic1, pin1, apic2, pin2;
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002127 int no_pin1 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 int vector;
Ingo Molnara0176e22008-06-16 12:44:17 +02002129 unsigned int ver;
Ingo Molnar4aae0702007-12-18 18:05:58 +01002130 unsigned long flags;
2131
2132 local_irq_save(flags);
Maciej W. Rozyckid4d25de2007-11-26 20:42:19 +01002133
Ingo Molnara0176e22008-06-16 12:44:17 +02002134 ver = apic_read(APIC_LVR);
2135 ver = GET_APIC_VERSION(ver);
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 /*
2138 * get/set the timer IRQ vector:
2139 */
2140 disable_8259A_irq(0);
2141 vector = assign_irq_vector(0);
2142 set_intr_gate(vector, interrupt[0]);
2143
2144 /*
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002145 * As IRQ0 is to be enabled in the 8259A, the virtual
2146 * wire has to be disabled in the local APIC. Also
2147 * timer interrupts need to be acknowledged manually in
2148 * the 8259A for the i82489DX when using the NMI
2149 * watchdog as that APIC treats NMIs as level-triggered.
2150 * The AEOI mode will finish them in the 8259A
2151 * automatically.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 */
2153 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT);
2154 init_8259A(1);
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002155 timer_ack = (nmi_watchdog == NMI_IO_APIC && !APIC_INTEGRATED(ver));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002157 pin1 = find_isa_irq_pin(0, mp_INT);
2158 apic1 = find_isa_irq_apic(0, mp_INT);
2159 pin2 = ioapic_i8259.pin;
2160 apic2 = ioapic_i8259.apic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002162 printk(KERN_INFO "..TIMER: vector=0x%02X apic1=%d pin1=%d apic2=%d pin2=%d\n",
2163 vector, apic1, pin1, apic2, pin2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002165 /*
2166 * Some BIOS writers are clueless and report the ExtINTA
2167 * I/O APIC input from the cascaded 8259A as the timer
2168 * interrupt input. So just in case, if only one pin
2169 * was found above, try it both directly and through the
2170 * 8259A.
2171 */
2172 if (pin1 == -1) {
2173 pin1 = pin2;
2174 apic1 = apic2;
2175 no_pin1 = 1;
2176 } else if (pin2 == -1) {
2177 pin2 = pin1;
2178 apic2 = apic1;
2179 }
2180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (pin1 != -1) {
2182 /*
2183 * Ok, does IRQ0 through the IOAPIC work?
2184 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002185 if (no_pin1) {
2186 add_pin_to_irq(0, apic1, pin1);
2187 setup_timer_IRQ0_pin(apic1, pin1, vector);
2188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 unmask_IO_APIC_irq(0);
2190 if (timer_irq_works()) {
2191 if (nmi_watchdog == NMI_IO_APIC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 setup_nmi();
2193 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 }
Chuck Ebbert66759a02005-09-12 18:49:25 +02002195 if (disable_timer_pin_1 > 0)
2196 clear_IO_APIC_pin(0, pin1);
Ingo Molnar4aae0702007-12-18 18:05:58 +01002197 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002199 clear_IO_APIC_pin(apic1, pin1);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002200 if (!no_pin1)
2201 printk(KERN_ERR "..MP-BIOS bug: "
2202 "8254 timer not connected to IO-APIC\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002204 printk(KERN_INFO "...trying to set up timer (IRQ0) "
2205 "through the 8259A ... ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 printk("\n..... (found pin %d) ...", pin2);
2207 /*
2208 * legacy devices should be connected to IO APIC #0
2209 */
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002210 replace_pin_at_irq(0, apic1, pin1, apic2, pin2);
Maciej W. Rozyckif7633ce2008-05-27 21:19:34 +01002211 setup_timer_IRQ0_pin(apic2, pin2, vector);
Maciej W. Rozycki24742ec2008-05-27 21:19:40 +01002212 unmask_IO_APIC_irq(0);
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002213 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if (timer_irq_works()) {
2215 printk("works.\n");
Maciej W. Rozycki35542c52008-05-21 22:10:22 +01002216 timer_through_8259 = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (nmi_watchdog == NMI_IO_APIC) {
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002218 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 setup_nmi();
Maciej W. Rozycki60134eb2008-05-21 22:09:34 +01002220 enable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 }
Ingo Molnar4aae0702007-12-18 18:05:58 +01002222 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 }
2224 /*
2225 * Cleanup, just in case ...
2226 */
Maciej W. Rozyckiecd29472008-05-21 22:09:19 +01002227 disable_8259A_irq(0);
Eric W. Biedermanfcfd6362005-10-30 14:59:39 -08002228 clear_IO_APIC_pin(apic2, pin2);
Maciej W. Rozycki691874f2008-05-27 21:19:51 +01002229 printk(" failed.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231
2232 if (nmi_watchdog == NMI_IO_APIC) {
2233 printk(KERN_WARNING "timer doesn't work through the IO-APIC - disabling NMI Watchdog!\n");
Cyrill Gorcunov067fa0f2008-05-29 22:32:30 +04002234 nmi_watchdog = NMI_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
Maciej W. Rozyckid11d5792008-05-21 22:09:11 +01002236 timer_ack = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 printk(KERN_INFO "...trying to set up timer as Virtual Wire IRQ...");
2239
Ingo Molnara460e742006-10-17 00:10:03 -07002240 set_irq_chip_and_handler_name(0, &lapic_chip, handle_fasteoi_irq,
Maciej W. Rozycki2e188932007-02-13 13:26:20 +01002241 "fasteoi");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 apic_write_around(APIC_LVT0, APIC_DM_FIXED | vector); /* Fixed mode */
2243 enable_8259A_irq(0);
2244
2245 if (timer_irq_works()) {
2246 printk(" works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002247 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
Maciej W. Rozyckie67465f2008-05-21 22:09:26 +01002249 disable_8259A_irq(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 apic_write_around(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | vector);
2251 printk(" failed.\n");
2252
2253 printk(KERN_INFO "...trying to set up timer as ExtINT IRQ...");
2254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 init_8259A(0);
2256 make_8259A_irq(0);
2257 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
2258
2259 unlock_ExtINT_logic();
2260
2261 if (timer_irq_works()) {
2262 printk(" works.\n");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002263 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
2265 printk(" failed :(.\n");
2266 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a "
2267 "report. Then try booting with the 'noapic' option");
Ingo Molnar4aae0702007-12-18 18:05:58 +01002268out:
2269 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270}
2271
2272/*
2273 *
2274 * IRQ's that are handled by the PIC in the MPS IOAPIC case.
2275 * - IRQ2 is the cascade IRQ, and cannot be a io-apic IRQ.
2276 * Linux doesn't really care, as it's not actually used
2277 * for any interrupt handling anyway.
2278 */
2279#define PIC_IRQS (1 << PIC_CASCADE_IR)
2280
2281void __init setup_IO_APIC(void)
2282{
Rusty Russelldbeb2be2007-10-19 20:35:03 +02002283 int i;
2284
2285 /* Reserve all the system vectors. */
2286 for (i = FIRST_SYSTEM_VECTOR; i < NR_VECTORS; i++)
2287 set_bit(i, used_vectors);
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 enable_IO_APIC();
2290
2291 if (acpi_ioapic)
2292 io_apic_irqs = ~0; /* all IRQs go through IOAPIC */
2293 else
2294 io_apic_irqs = ~PIC_IRQS;
2295
2296 printk("ENABLING IO-APIC IRQs\n");
2297
2298 /*
2299 * Set up IO-APIC IRQ routing.
2300 */
2301 if (!acpi_ioapic)
2302 setup_ioapic_ids_from_mpc();
2303 sync_Arb_IDs();
2304 setup_IO_APIC_irqs();
2305 init_IO_APIC_traps();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08002306 check_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (!acpi_ioapic)
2308 print_IO_APIC();
2309}
2310
2311/*
2312 * Called after all the initialization is done. If we didnt find any
2313 * APIC bugs then we can allow the modify fast path
2314 */
2315
2316static int __init io_apic_bug_finalize(void)
2317{
2318 if(sis_apic_bug == -1)
2319 sis_apic_bug = 0;
2320 return 0;
2321}
2322
2323late_initcall(io_apic_bug_finalize);
2324
2325struct sysfs_ioapic_data {
2326 struct sys_device dev;
2327 struct IO_APIC_route_entry entry[0];
2328};
2329static struct sysfs_ioapic_data * mp_ioapic_data[MAX_IO_APICS];
2330
Pavel Machek438510f2005-04-16 15:25:24 -07002331static int ioapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
2333 struct IO_APIC_route_entry *entry;
2334 struct sysfs_ioapic_data *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 int i;
2336
2337 data = container_of(dev, struct sysfs_ioapic_data, dev);
2338 entry = data->entry;
Andi Kleencf4c6a22006-09-26 10:52:30 +02002339 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++)
2340 entry[i] = ioapic_read_entry(dev->id, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 return 0;
2343}
2344
2345static int ioapic_resume(struct sys_device *dev)
2346{
2347 struct IO_APIC_route_entry *entry;
2348 struct sysfs_ioapic_data *data;
2349 unsigned long flags;
2350 union IO_APIC_reg_00 reg_00;
2351 int i;
2352
2353 data = container_of(dev, struct sysfs_ioapic_data, dev);
2354 entry = data->entry;
2355
2356 spin_lock_irqsave(&ioapic_lock, flags);
2357 reg_00.raw = io_apic_read(dev->id, 0);
2358 if (reg_00.bits.ID != mp_ioapics[dev->id].mpc_apicid) {
2359 reg_00.bits.ID = mp_ioapics[dev->id].mpc_apicid;
2360 io_apic_write(dev->id, 0, reg_00.raw);
2361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 spin_unlock_irqrestore(&ioapic_lock, flags);
Andi Kleencf4c6a22006-09-26 10:52:30 +02002363 for (i = 0; i < nr_ioapic_registers[dev->id]; i ++)
2364 ioapic_write_entry(dev->id, i, entry[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
2366 return 0;
2367}
2368
2369static struct sysdev_class ioapic_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002370 .name = "ioapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 .suspend = ioapic_suspend,
2372 .resume = ioapic_resume,
2373};
2374
2375static int __init ioapic_init_sysfs(void)
2376{
2377 struct sys_device * dev;
2378 int i, size, error = 0;
2379
2380 error = sysdev_class_register(&ioapic_sysdev_class);
2381 if (error)
2382 return error;
2383
2384 for (i = 0; i < nr_ioapics; i++ ) {
2385 size = sizeof(struct sys_device) + nr_ioapic_registers[i]
2386 * sizeof(struct IO_APIC_route_entry);
2387 mp_ioapic_data[i] = kmalloc(size, GFP_KERNEL);
2388 if (!mp_ioapic_data[i]) {
2389 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2390 continue;
2391 }
2392 memset(mp_ioapic_data[i], 0, size);
2393 dev = &mp_ioapic_data[i]->dev;
2394 dev->id = i;
2395 dev->cls = &ioapic_sysdev_class;
2396 error = sysdev_register(dev);
2397 if (error) {
2398 kfree(mp_ioapic_data[i]);
2399 mp_ioapic_data[i] = NULL;
2400 printk(KERN_ERR "Can't suspend/resume IOAPIC %d\n", i);
2401 continue;
2402 }
2403 }
2404
2405 return 0;
2406}
2407
2408device_initcall(ioapic_init_sysfs);
2409
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002410/*
Eric W. Biederman95d77882006-10-04 02:17:01 -07002411 * Dynamic irq allocate and deallocation
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002412 */
2413int create_irq(void)
2414{
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002415 /* Allocate an unused irq */
Andi Kleen306a22c2006-12-09 21:33:36 +01002416 int irq, new, vector = 0;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002417 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002418
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002419 irq = -ENOSPC;
2420 spin_lock_irqsave(&vector_lock, flags);
2421 for (new = (NR_IRQS - 1); new >= 0; new--) {
2422 if (platform_legacy_irq(new))
2423 continue;
2424 if (irq_vector[new] != 0)
2425 continue;
2426 vector = __assign_irq_vector(new);
2427 if (likely(vector > 0))
2428 irq = new;
2429 break;
2430 }
2431 spin_unlock_irqrestore(&vector_lock, flags);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002432
Eric W. Biedermanace80ab2006-10-04 02:16:47 -07002433 if (irq >= 0) {
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002434 set_intr_gate(vector, interrupt[irq]);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002435 dynamic_irq_init(irq);
2436 }
2437 return irq;
2438}
2439
2440void destroy_irq(unsigned int irq)
2441{
2442 unsigned long flags;
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002443
2444 dynamic_irq_cleanup(irq);
2445
2446 spin_lock_irqsave(&vector_lock, flags);
PJ Waskiewicz9d9ad4b2008-04-25 17:58:52 -07002447 clear_bit(irq_vector[irq], used_vectors);
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002448 irq_vector[irq] = 0;
2449 spin_unlock_irqrestore(&vector_lock, flags);
2450}
Eric W. Biederman3fc471e2006-10-04 02:16:39 -07002451
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002452/*
Simon Arlott27b46d72007-10-20 01:13:56 +02002453 * MSI message composition
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002454 */
2455#ifdef CONFIG_PCI_MSI
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002456static int msi_compose_msg(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002457{
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002458 int vector;
2459 unsigned dest;
2460
2461 vector = assign_irq_vector(irq);
2462 if (vector >= 0) {
2463 dest = cpu_mask_to_apicid(TARGET_CPUS);
2464
2465 msg->address_hi = MSI_ADDR_BASE_HI;
2466 msg->address_lo =
2467 MSI_ADDR_BASE_LO |
2468 ((INT_DEST_MODE == 0) ?
2469 MSI_ADDR_DEST_MODE_PHYSICAL:
2470 MSI_ADDR_DEST_MODE_LOGICAL) |
2471 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2472 MSI_ADDR_REDIRECTION_CPU:
2473 MSI_ADDR_REDIRECTION_LOWPRI) |
2474 MSI_ADDR_DEST_ID(dest);
2475
2476 msg->data =
2477 MSI_DATA_TRIGGER_EDGE |
2478 MSI_DATA_LEVEL_ASSERT |
2479 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2480 MSI_DATA_DELIVERY_FIXED:
2481 MSI_DATA_DELIVERY_LOWPRI) |
2482 MSI_DATA_VECTOR(vector);
2483 }
2484 return vector;
2485}
2486
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002487#ifdef CONFIG_SMP
2488static void set_msi_irq_affinity(unsigned int irq, cpumask_t mask)
2489{
2490 struct msi_msg msg;
2491 unsigned int dest;
2492 cpumask_t tmp;
2493 int vector;
2494
2495 cpus_and(tmp, mask, cpu_online_map);
2496 if (cpus_empty(tmp))
2497 tmp = TARGET_CPUS;
2498
2499 vector = assign_irq_vector(irq);
2500 if (vector < 0)
2501 return;
2502
2503 dest = cpu_mask_to_apicid(mask);
2504
2505 read_msi_msg(irq, &msg);
2506
2507 msg.data &= ~MSI_DATA_VECTOR_MASK;
2508 msg.data |= MSI_DATA_VECTOR(vector);
2509 msg.address_lo &= ~MSI_ADDR_DEST_ID_MASK;
2510 msg.address_lo |= MSI_ADDR_DEST_ID(dest);
2511
2512 write_msi_msg(irq, &msg);
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -07002513 irq_desc[irq].affinity = mask;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002514}
2515#endif /* CONFIG_SMP */
2516
2517/*
2518 * IRQ Chip for MSI PCI/PCI-X/PCI-Express Devices,
2519 * which implement the MSI or MSI-X Capability Structure.
2520 */
2521static struct irq_chip msi_chip = {
2522 .name = "PCI-MSI",
2523 .unmask = unmask_msi_irq,
2524 .mask = mask_msi_irq,
2525 .ack = ack_ioapic_irq,
2526#ifdef CONFIG_SMP
2527 .set_affinity = set_msi_irq_affinity,
2528#endif
2529 .retrigger = ioapic_retrigger_irq,
2530};
2531
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002532int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002533{
2534 struct msi_msg msg;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002535 int irq, ret;
2536 irq = create_irq();
2537 if (irq < 0)
2538 return irq;
2539
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002540 ret = msi_compose_msg(dev, irq, &msg);
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002541 if (ret < 0) {
2542 destroy_irq(irq);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002543 return ret;
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002544 }
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002545
Michael Ellerman7fe37302007-04-18 19:39:21 +10002546 set_irq_msi(irq, desc);
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002547 write_msi_msg(irq, &msg);
2548
Ingo Molnara460e742006-10-17 00:10:03 -07002549 set_irq_chip_and_handler_name(irq, &msi_chip, handle_edge_irq,
2550 "edge");
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002551
Michael Ellerman7fe37302007-04-18 19:39:21 +10002552 return 0;
Eric W. Biederman3b7d1922006-10-04 02:16:59 -07002553}
2554
2555void arch_teardown_msi_irq(unsigned int irq)
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002556{
Eric W. Biedermanf7feaca2007-01-28 12:56:37 -07002557 destroy_irq(irq);
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002558}
2559
Eric W. Biederman2d3fcc12006-10-04 02:16:43 -07002560#endif /* CONFIG_PCI_MSI */
2561
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002562/*
2563 * Hypertransport interrupt support
2564 */
2565#ifdef CONFIG_HT_IRQ
2566
2567#ifdef CONFIG_SMP
2568
2569static void target_ht_irq(unsigned int irq, unsigned int dest)
2570{
Eric W. Biedermanec683072006-11-08 17:44:57 -08002571 struct ht_irq_msg msg;
2572 fetch_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002573
Eric W. Biedermanec683072006-11-08 17:44:57 -08002574 msg.address_lo &= ~(HT_IRQ_LOW_DEST_ID_MASK);
2575 msg.address_hi &= ~(HT_IRQ_HIGH_DEST_ID_MASK);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002576
Eric W. Biedermanec683072006-11-08 17:44:57 -08002577 msg.address_lo |= HT_IRQ_LOW_DEST_ID(dest);
2578 msg.address_hi |= HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002579
Eric W. Biedermanec683072006-11-08 17:44:57 -08002580 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002581}
2582
2583static void set_ht_irq_affinity(unsigned int irq, cpumask_t mask)
2584{
2585 unsigned int dest;
2586 cpumask_t tmp;
2587
2588 cpus_and(tmp, mask, cpu_online_map);
2589 if (cpus_empty(tmp))
2590 tmp = TARGET_CPUS;
2591
2592 cpus_and(mask, tmp, CPU_MASK_ALL);
2593
2594 dest = cpu_mask_to_apicid(mask);
2595
2596 target_ht_irq(irq, dest);
Eric W. Biederman9f0a5ba2007-02-23 04:13:55 -07002597 irq_desc[irq].affinity = mask;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002598}
2599#endif
2600
Aneesh Kumar K.Vc37e1082006-10-11 01:20:43 -07002601static struct irq_chip ht_irq_chip = {
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002602 .name = "PCI-HT",
2603 .mask = mask_ht_irq,
2604 .unmask = unmask_ht_irq,
2605 .ack = ack_ioapic_irq,
2606#ifdef CONFIG_SMP
2607 .set_affinity = set_ht_irq_affinity,
2608#endif
2609 .retrigger = ioapic_retrigger_irq,
2610};
2611
2612int arch_setup_ht_irq(unsigned int irq, struct pci_dev *dev)
2613{
2614 int vector;
2615
2616 vector = assign_irq_vector(irq);
2617 if (vector >= 0) {
Eric W. Biedermanec683072006-11-08 17:44:57 -08002618 struct ht_irq_msg msg;
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002619 unsigned dest;
2620 cpumask_t tmp;
2621
2622 cpus_clear(tmp);
2623 cpu_set(vector >> 8, tmp);
2624 dest = cpu_mask_to_apicid(tmp);
2625
Eric W. Biedermanec683072006-11-08 17:44:57 -08002626 msg.address_hi = HT_IRQ_HIGH_DEST_ID(dest);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002627
Eric W. Biedermanec683072006-11-08 17:44:57 -08002628 msg.address_lo =
2629 HT_IRQ_LOW_BASE |
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002630 HT_IRQ_LOW_DEST_ID(dest) |
2631 HT_IRQ_LOW_VECTOR(vector) |
2632 ((INT_DEST_MODE == 0) ?
2633 HT_IRQ_LOW_DM_PHYSICAL :
2634 HT_IRQ_LOW_DM_LOGICAL) |
2635 HT_IRQ_LOW_RQEOI_EDGE |
2636 ((INT_DELIVERY_MODE != dest_LowestPrio) ?
2637 HT_IRQ_LOW_MT_FIXED :
2638 HT_IRQ_LOW_MT_ARBITRATED) |
2639 HT_IRQ_LOW_IRQ_MASKED;
2640
Eric W. Biedermanec683072006-11-08 17:44:57 -08002641 write_ht_irq_msg(irq, &msg);
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002642
Ingo Molnara460e742006-10-17 00:10:03 -07002643 set_irq_chip_and_handler_name(irq, &ht_irq_chip,
2644 handle_edge_irq, "edge");
Eric W. Biederman8b955b02006-10-04 02:16:55 -07002645 }
2646 return vector;
2647}
2648#endif /* CONFIG_HT_IRQ */
2649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650/* --------------------------------------------------------------------------
2651 ACPI-based IOAPIC Configuration
2652 -------------------------------------------------------------------------- */
2653
Len Brown888ba6c2005-08-24 12:07:20 -04002654#ifdef CONFIG_ACPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
2656int __init io_apic_get_unique_id (int ioapic, int apic_id)
2657{
2658 union IO_APIC_reg_00 reg_00;
2659 static physid_mask_t apic_id_map = PHYSID_MASK_NONE;
2660 physid_mask_t tmp;
2661 unsigned long flags;
2662 int i = 0;
2663
2664 /*
2665 * The P4 platform supports up to 256 APIC IDs on two separate APIC
2666 * buses (one for LAPICs, one for IOAPICs), where predecessors only
2667 * supports up to 16 on one shared APIC bus.
2668 *
2669 * TBD: Expand LAPIC/IOAPIC support on P4-class systems to take full
2670 * advantage of new APIC bus architecture.
2671 */
2672
2673 if (physids_empty(apic_id_map))
2674 apic_id_map = ioapic_phys_id_map(phys_cpu_present_map);
2675
2676 spin_lock_irqsave(&ioapic_lock, flags);
2677 reg_00.raw = io_apic_read(ioapic, 0);
2678 spin_unlock_irqrestore(&ioapic_lock, flags);
2679
2680 if (apic_id >= get_physical_broadcast()) {
2681 printk(KERN_WARNING "IOAPIC[%d]: Invalid apic_id %d, trying "
2682 "%d\n", ioapic, apic_id, reg_00.bits.ID);
2683 apic_id = reg_00.bits.ID;
2684 }
2685
2686 /*
2687 * Every APIC in a system must have a unique ID or we get lots of nice
2688 * 'stuck on smp_invalidate_needed IPI wait' messages.
2689 */
2690 if (check_apicid_used(apic_id_map, apic_id)) {
2691
2692 for (i = 0; i < get_physical_broadcast(); i++) {
2693 if (!check_apicid_used(apic_id_map, i))
2694 break;
2695 }
2696
2697 if (i == get_physical_broadcast())
2698 panic("Max apic_id exceeded!\n");
2699
2700 printk(KERN_WARNING "IOAPIC[%d]: apic_id %d already used, "
2701 "trying %d\n", ioapic, apic_id, i);
2702
2703 apic_id = i;
2704 }
2705
2706 tmp = apicid_to_cpu_present(apic_id);
2707 physids_or(apic_id_map, apic_id_map, tmp);
2708
2709 if (reg_00.bits.ID != apic_id) {
2710 reg_00.bits.ID = apic_id;
2711
2712 spin_lock_irqsave(&ioapic_lock, flags);
2713 io_apic_write(ioapic, 0, reg_00.raw);
2714 reg_00.raw = io_apic_read(ioapic, 0);
2715 spin_unlock_irqrestore(&ioapic_lock, flags);
2716
2717 /* Sanity check */
Andreas Deresch6070f9e2006-02-26 04:18:34 +01002718 if (reg_00.bits.ID != apic_id) {
2719 printk("IOAPIC[%d]: Unable to change apic_id!\n", ioapic);
2720 return -1;
2721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
2723
2724 apic_printk(APIC_VERBOSE, KERN_INFO
2725 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id);
2726
2727 return apic_id;
2728}
2729
2730
2731int __init io_apic_get_version (int ioapic)
2732{
2733 union IO_APIC_reg_01 reg_01;
2734 unsigned long flags;
2735
2736 spin_lock_irqsave(&ioapic_lock, flags);
2737 reg_01.raw = io_apic_read(ioapic, 1);
2738 spin_unlock_irqrestore(&ioapic_lock, flags);
2739
2740 return reg_01.bits.version;
2741}
2742
2743
2744int __init io_apic_get_redir_entries (int ioapic)
2745{
2746 union IO_APIC_reg_01 reg_01;
2747 unsigned long flags;
2748
2749 spin_lock_irqsave(&ioapic_lock, flags);
2750 reg_01.raw = io_apic_read(ioapic, 1);
2751 spin_unlock_irqrestore(&ioapic_lock, flags);
2752
2753 return reg_01.bits.entries;
2754}
2755
2756
2757int io_apic_set_pci_routing (int ioapic, int pin, int irq, int edge_level, int active_high_low)
2758{
2759 struct IO_APIC_route_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 if (!IO_APIC_IRQ(irq)) {
2762 printk(KERN_ERR "IOAPIC[%d]: Invalid reference to IRQ 0\n",
2763 ioapic);
2764 return -EINVAL;
2765 }
2766
2767 /*
2768 * Generate a PCI IRQ routing entry and program the IOAPIC accordingly.
2769 * Note that we mask (disable) IRQs now -- these get enabled when the
2770 * corresponding device driver registers for this IRQ.
2771 */
2772
2773 memset(&entry,0,sizeof(entry));
2774
2775 entry.delivery_mode = INT_DELIVERY_MODE;
2776 entry.dest_mode = INT_DEST_MODE;
2777 entry.dest.logical.logical_dest = cpu_mask_to_apicid(TARGET_CPUS);
2778 entry.trigger = edge_level;
2779 entry.polarity = active_high_low;
2780 entry.mask = 1;
2781
2782 /*
2783 * IRQs < 16 are already in the irq_2_pin[] map
2784 */
2785 if (irq >= 16)
2786 add_pin_to_irq(irq, ioapic, pin);
2787
2788 entry.vector = assign_irq_vector(irq);
2789
2790 apic_printk(APIC_DEBUG, KERN_DEBUG "IOAPIC[%d]: Set PCI routing entry "
2791 "(%d-%d -> 0x%x -> IRQ %d Mode:%i Active:%i)\n", ioapic,
2792 mp_ioapics[ioapic].mpc_apicid, pin, entry.vector, irq,
2793 edge_level, active_high_low);
2794
2795 ioapic_register_intr(irq, entry.vector, edge_level);
2796
2797 if (!ioapic && (irq < 16))
2798 disable_8259A_irq(irq);
2799
Akinobu Mitaa2249cb2008-04-05 22:39:05 +09002800 ioapic_write_entry(ioapic, pin, entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802 return 0;
2803}
2804
Shaohua Li61fd47e2007-11-17 01:05:28 -05002805int acpi_get_override_irq(int bus_irq, int *trigger, int *polarity)
2806{
2807 int i;
2808
2809 if (skip_ioapic_setup)
2810 return -1;
2811
2812 for (i = 0; i < mp_irq_entries; i++)
2813 if (mp_irqs[i].mpc_irqtype == mp_INT &&
2814 mp_irqs[i].mpc_srcbusirq == bus_irq)
2815 break;
2816 if (i >= mp_irq_entries)
2817 return -1;
2818
2819 *trigger = irq_trigger(i);
2820 *polarity = irq_polarity(i);
2821 return 0;
2822}
2823
Len Brown888ba6c2005-08-24 12:07:20 -04002824#endif /* CONFIG_ACPI */
Rusty Russell1a3f2392006-09-26 10:52:32 +02002825
2826static int __init parse_disable_timer_pin_1(char *arg)
2827{
2828 disable_timer_pin_1 = 1;
2829 return 0;
2830}
2831early_param("disable_timer_pin_1", parse_disable_timer_pin_1);
2832
2833static int __init parse_enable_timer_pin_1(char *arg)
2834{
2835 disable_timer_pin_1 = -1;
2836 return 0;
2837}
2838early_param("enable_timer_pin_1", parse_enable_timer_pin_1);
2839
2840static int __init parse_noapic(char *arg)
2841{
2842 /* disable IO-APIC */
2843 disable_ioapic_setup();
2844 return 0;
2845}
2846early_param("noapic", parse_noapic);