vserver 1.9.5.x5
[linux-2.6.git] / arch / ia64 / kernel / iosapic.c
1 /*
2  * I/O SAPIC support.
3  *
4  * Copyright (C) 1999 Intel Corp.
5  * Copyright (C) 1999 Asit Mallick <asit.k.mallick@intel.com>
6  * Copyright (C) 2000-2002 J.I. Lee <jung-ik.lee@intel.com>
7  * Copyright (C) 1999-2000, 2002-2003 Hewlett-Packard Co.
8  *      David Mosberger-Tang <davidm@hpl.hp.com>
9  * Copyright (C) 1999 VA Linux Systems
10  * Copyright (C) 1999,2000 Walt Drummond <drummond@valinux.com>
11  *
12  * 00/04/19     D. Mosberger    Rewritten to mirror more closely the x86 I/O APIC code.
13  *                              In particular, we now have separate handlers for edge
14  *                              and level triggered interrupts.
15  * 00/10/27     Asit Mallick, Goutham Rao <goutham.rao@intel.com> IRQ vector allocation
16  *                              PCI to vector mapping, shared PCI interrupts.
17  * 00/10/27     D. Mosberger    Document things a bit more to make them more understandable.
18  *                              Clean up much of the old IOSAPIC cruft.
19  * 01/07/27     J.I. Lee        PCI irq routing, Platform/Legacy interrupts and fixes for
20  *                              ACPI S5(SoftOff) support.
21  * 02/01/23     J.I. Lee        iosapic pgm fixes for PCI irq routing from _PRT
22  * 02/01/07     E. Focht        <efocht@ess.nec.de> Redirectable interrupt vectors in
23  *                              iosapic_set_affinity(), initializations for
24  *                              /proc/irq/#/smp_affinity
25  * 02/04/02     P. Diefenbaugh  Cleaned up ACPI PCI IRQ routing.
26  * 02/04/18     J.I. Lee        bug fix in iosapic_init_pci_irq
27  * 02/04/30     J.I. Lee        bug fix in find_iosapic to fix ACPI PCI IRQ to IOSAPIC mapping
28  *                              error
29  * 02/07/29     T. Kochi        Allocate interrupt vectors dynamically
30  * 02/08/04     T. Kochi        Cleaned up terminology (irq, global system interrupt, vector, etc.)
31  * 02/09/20     D. Mosberger    Simplified by taking advantage of ACPI's pci_irq code.
32  * 03/02/19     B. Helgaas      Make pcat_compat system-wide, not per-IOSAPIC.
33  *                              Remove iosapic_address & gsi_base from external interfaces.
34  *                              Rationalize __init/__devinit attributes.
35  * 04/12/04 Ashok Raj   <ashok.raj@intel.com> Intel Corporation 2004
36  *                              Updated to work with irq migration necessary for CPU Hotplug
37  */
38 /*
39  * Here is what the interrupt logic between a PCI device and the kernel looks like:
40  *
41  * (1) A PCI device raises one of the four interrupt pins (INTA, INTB, INTC, INTD).  The
42  *     device is uniquely identified by its bus--, and slot-number (the function
43  *     number does not matter here because all functions share the same interrupt
44  *     lines).
45  *
46  * (2) The motherboard routes the interrupt line to a pin on a IOSAPIC controller.
47  *     Multiple interrupt lines may have to share the same IOSAPIC pin (if they're level
48  *     triggered and use the same polarity).  Each interrupt line has a unique Global
49  *     System Interrupt (GSI) number which can be calculated as the sum of the controller's
50  *     base GSI number and the IOSAPIC pin number to which the line connects.
51  *
52  * (3) The IOSAPIC uses an internal routing table entries (RTEs) to map the IOSAPIC pin
53  *     into the IA-64 interrupt vector.  This interrupt vector is then sent to the CPU.
54  *
55  * (4) The kernel recognizes an interrupt as an IRQ.  The IRQ interface is used as
56  *     architecture-independent interrupt handling mechanism in Linux.  As an
57  *     IRQ is a number, we have to have IA-64 interrupt vector number <-> IRQ number
58  *     mapping.  On smaller systems, we use one-to-one mapping between IA-64 vector and
59  *     IRQ.  A platform can implement platform_irq_to_vector(irq) and
60  *     platform_local_vector_to_irq(vector) APIs to differentiate the mapping.
61  *     Please see also include/asm-ia64/hw_irq.h for those APIs.
62  *
63  * To sum up, there are three levels of mappings involved:
64  *
65  *      PCI pin -> global system interrupt (GSI) -> IA-64 vector <-> IRQ
66  *
67  * Note: The term "IRQ" is loosely used everywhere in Linux kernel to describe interrupts.
68  * Now we use "IRQ" only for Linux IRQ's.  ISA IRQ (isa_irq) is the only exception in this
69  * source code.
70  */
71 #include <linux/config.h>
72
73 #include <linux/acpi.h>
74 #include <linux/init.h>
75 #include <linux/irq.h>
76 #include <linux/kernel.h>
77 #include <linux/list.h>
78 #include <linux/pci.h>
79 #include <linux/smp.h>
80 #include <linux/smp_lock.h>
81 #include <linux/string.h>
82
83 #include <asm/delay.h>
84 #include <asm/hw_irq.h>
85 #include <asm/io.h>
86 #include <asm/iosapic.h>
87 #include <asm/machvec.h>
88 #include <asm/processor.h>
89 #include <asm/ptrace.h>
90 #include <asm/system.h>
91
92
93 #undef DEBUG_INTERRUPT_ROUTING
94 #undef OVERRIDE_DEBUG
95
96 #ifdef DEBUG_INTERRUPT_ROUTING
97 #define DBG(fmt...)     printk(fmt)
98 #else
99 #define DBG(fmt...)
100 #endif
101
102 static DEFINE_SPINLOCK(iosapic_lock);
103
104 /* These tables map IA-64 vectors to the IOSAPIC pin that generates this vector. */
105
106 static struct iosapic_intr_info {
107         char __iomem    *addr;          /* base address of IOSAPIC */
108         u32             low32;          /* current value of low word of Redirection table entry */
109         unsigned int    gsi_base;       /* first GSI assigned to this IOSAPIC */
110         char            rte_index;      /* IOSAPIC RTE index (-1 => not an IOSAPIC interrupt) */
111         unsigned char   dmode   : 3;    /* delivery mode (see iosapic.h) */
112         unsigned char   polarity: 1;    /* interrupt polarity (see iosapic.h) */
113         unsigned char   trigger : 1;    /* trigger mode (see iosapic.h) */
114         int             refcnt;         /* reference counter */
115 } iosapic_intr_info[IA64_NUM_VECTORS];
116
117 static struct iosapic {
118         char __iomem    *addr;          /* base address of IOSAPIC */
119         unsigned int    gsi_base;       /* first GSI assigned to this IOSAPIC */
120         unsigned short  num_rte;        /* number of RTE in this IOSAPIC */
121 #ifdef CONFIG_NUMA
122         unsigned short  node;           /* numa node association via pxm */
123 #endif
124 } iosapic_lists[NR_IOSAPICS];
125
126 static int num_iosapic;
127
128 static unsigned char pcat_compat __initdata;    /* 8259 compatibility flag */
129
130
131 /*
132  * Find an IOSAPIC associated with a GSI
133  */
134 static inline int
135 find_iosapic (unsigned int gsi)
136 {
137         int i;
138
139         for (i = 0; i < num_iosapic; i++) {
140                 if ((unsigned) (gsi - iosapic_lists[i].gsi_base) < iosapic_lists[i].num_rte)
141                         return i;
142         }
143
144         return -1;
145 }
146
147 static inline int
148 _gsi_to_vector (unsigned int gsi)
149 {
150         struct iosapic_intr_info *info;
151
152         for (info = iosapic_intr_info; info < iosapic_intr_info + IA64_NUM_VECTORS; ++info)
153                 if (info->gsi_base + info->rte_index == gsi)
154                         return info - iosapic_intr_info;
155         return -1;
156 }
157
158 /*
159  * Translate GSI number to the corresponding IA-64 interrupt vector.  If no
160  * entry exists, return -1.
161  */
162 inline int
163 gsi_to_vector (unsigned int gsi)
164 {
165         return _gsi_to_vector(gsi);
166 }
167
168 int
169 gsi_to_irq (unsigned int gsi)
170 {
171         /*
172          * XXX fix me: this assumes an identity mapping vetween IA-64 vector and Linux irq
173          * numbers...
174          */
175         return _gsi_to_vector(gsi);
176 }
177
178 static void
179 set_rte (unsigned int vector, unsigned int dest, int mask)
180 {
181         unsigned long pol, trigger, dmode;
182         u32 low32, high32;
183         char __iomem *addr;
184         int rte_index;
185         char redir;
186
187         DBG(KERN_DEBUG"IOSAPIC: routing vector %d to 0x%x\n", vector, dest);
188
189         rte_index = iosapic_intr_info[vector].rte_index;
190         if (rte_index < 0)
191                 return;         /* not an IOSAPIC interrupt */
192
193         addr    = iosapic_intr_info[vector].addr;
194         pol     = iosapic_intr_info[vector].polarity;
195         trigger = iosapic_intr_info[vector].trigger;
196         dmode   = iosapic_intr_info[vector].dmode;
197         vector &= (~IA64_IRQ_REDIRECTED);
198
199         redir = (dmode == IOSAPIC_LOWEST_PRIORITY) ? 1 : 0;
200
201 #ifdef CONFIG_SMP
202         {
203                 unsigned int irq;
204
205                 for (irq = 0; irq < NR_IRQS; ++irq)
206                         if (irq_to_vector(irq) == vector) {
207                                 set_irq_affinity_info(irq, (int)(dest & 0xffff), redir);
208                                 break;
209                         }
210         }
211 #endif
212
213         low32 = ((pol << IOSAPIC_POLARITY_SHIFT) |
214                  (trigger << IOSAPIC_TRIGGER_SHIFT) |
215                  (dmode << IOSAPIC_DELIVERY_SHIFT) |
216                  ((mask ? 1 : 0) << IOSAPIC_MASK_SHIFT) |
217                  vector);
218
219         /* dest contains both id and eid */
220         high32 = (dest << IOSAPIC_DEST_SHIFT);
221
222         iosapic_write(addr, IOSAPIC_RTE_HIGH(rte_index), high32);
223         iosapic_write(addr, IOSAPIC_RTE_LOW(rte_index), low32);
224         iosapic_intr_info[vector].low32 = low32;
225 }
226
227 static void
228 nop (unsigned int vector)
229 {
230         /* do nothing... */
231 }
232
233 static void
234 mask_irq (unsigned int irq)
235 {
236         unsigned long flags;
237         char __iomem *addr;
238         u32 low32;
239         int rte_index;
240         ia64_vector vec = irq_to_vector(irq);
241
242         addr = iosapic_intr_info[vec].addr;
243         rte_index = iosapic_intr_info[vec].rte_index;
244
245         if (rte_index < 0)
246                 return;                 /* not an IOSAPIC interrupt! */
247
248         spin_lock_irqsave(&iosapic_lock, flags);
249         {
250                 /* set only the mask bit */
251                 low32 = iosapic_intr_info[vec].low32 |= IOSAPIC_MASK;
252                 iosapic_write(addr, IOSAPIC_RTE_LOW(rte_index), low32);
253         }
254         spin_unlock_irqrestore(&iosapic_lock, flags);
255 }
256
257 static void
258 unmask_irq (unsigned int irq)
259 {
260         unsigned long flags;
261         char __iomem *addr;
262         u32 low32;
263         int rte_index;
264         ia64_vector vec = irq_to_vector(irq);
265
266         addr = iosapic_intr_info[vec].addr;
267         rte_index = iosapic_intr_info[vec].rte_index;
268         if (rte_index < 0)
269                 return;                 /* not an IOSAPIC interrupt! */
270
271         spin_lock_irqsave(&iosapic_lock, flags);
272         {
273                 low32 = iosapic_intr_info[vec].low32 &= ~IOSAPIC_MASK;
274                 iosapic_write(addr, IOSAPIC_RTE_LOW(rte_index), low32);
275         }
276         spin_unlock_irqrestore(&iosapic_lock, flags);
277 }
278
279
280 static void
281 iosapic_set_affinity (unsigned int irq, cpumask_t mask)
282 {
283 #ifdef CONFIG_SMP
284         unsigned long flags;
285         u32 high32, low32;
286         int dest, rte_index;
287         char __iomem *addr;
288         int redir = (irq & IA64_IRQ_REDIRECTED) ? 1 : 0;
289         ia64_vector vec;
290
291         irq &= (~IA64_IRQ_REDIRECTED);
292         vec = irq_to_vector(irq);
293
294         if (cpus_empty(mask))
295                 return;
296
297         dest = cpu_physical_id(first_cpu(mask));
298
299         rte_index = iosapic_intr_info[vec].rte_index;
300         addr = iosapic_intr_info[vec].addr;
301
302         if (rte_index < 0)
303                 return;                 /* not an IOSAPIC interrupt */
304
305         set_irq_affinity_info(irq, dest, redir);
306
307         /* dest contains both id and eid */
308         high32 = dest << IOSAPIC_DEST_SHIFT;
309
310         spin_lock_irqsave(&iosapic_lock, flags);
311         {
312                 low32 = iosapic_intr_info[vec].low32 & ~(7 << IOSAPIC_DELIVERY_SHIFT);
313
314                 if (redir)
315                         /* change delivery mode to lowest priority */
316                         low32 |= (IOSAPIC_LOWEST_PRIORITY << IOSAPIC_DELIVERY_SHIFT);
317                 else
318                         /* change delivery mode to fixed */
319                         low32 |= (IOSAPIC_FIXED << IOSAPIC_DELIVERY_SHIFT);
320
321                 iosapic_intr_info[vec].low32 = low32;
322                 iosapic_write(addr, IOSAPIC_RTE_HIGH(rte_index), high32);
323                 iosapic_write(addr, IOSAPIC_RTE_LOW(rte_index), low32);
324         }
325         spin_unlock_irqrestore(&iosapic_lock, flags);
326 #endif
327 }
328
329 /*
330  * Handlers for level-triggered interrupts.
331  */
332
333 static unsigned int
334 iosapic_startup_level_irq (unsigned int irq)
335 {
336         unmask_irq(irq);
337         return 0;
338 }
339
340 static void
341 iosapic_end_level_irq (unsigned int irq)
342 {
343         ia64_vector vec = irq_to_vector(irq);
344
345         move_irq(irq);
346         iosapic_eoi(iosapic_intr_info[vec].addr, vec);
347 }
348
349 #define iosapic_shutdown_level_irq      mask_irq
350 #define iosapic_enable_level_irq        unmask_irq
351 #define iosapic_disable_level_irq       mask_irq
352 #define iosapic_ack_level_irq           nop
353
354 struct hw_interrupt_type irq_type_iosapic_level = {
355         .typename =     "IO-SAPIC-level",
356         .startup =      iosapic_startup_level_irq,
357         .shutdown =     iosapic_shutdown_level_irq,
358         .enable =       iosapic_enable_level_irq,
359         .disable =      iosapic_disable_level_irq,
360         .ack =          iosapic_ack_level_irq,
361         .end =          iosapic_end_level_irq,
362         .set_affinity = iosapic_set_affinity
363 };
364
365 /*
366  * Handlers for edge-triggered interrupts.
367  */
368
369 static unsigned int
370 iosapic_startup_edge_irq (unsigned int irq)
371 {
372         unmask_irq(irq);
373         /*
374          * IOSAPIC simply drops interrupts pended while the
375          * corresponding pin was masked, so we can't know if an
376          * interrupt is pending already.  Let's hope not...
377          */
378         return 0;
379 }
380
381 static void
382 iosapic_ack_edge_irq (unsigned int irq)
383 {
384         irq_desc_t *idesc = irq_descp(irq);
385
386         move_irq(irq);
387         /*
388          * Once we have recorded IRQ_PENDING already, we can mask the
389          * interrupt for real. This prevents IRQ storms from unhandled
390          * devices.
391          */
392         if ((idesc->status & (IRQ_PENDING|IRQ_DISABLED)) == (IRQ_PENDING|IRQ_DISABLED))
393                 mask_irq(irq);
394 }
395
396 #define iosapic_enable_edge_irq         unmask_irq
397 #define iosapic_disable_edge_irq        nop
398 #define iosapic_end_edge_irq            nop
399
400 struct hw_interrupt_type irq_type_iosapic_edge = {
401         .typename =     "IO-SAPIC-edge",
402         .startup =      iosapic_startup_edge_irq,
403         .shutdown =     iosapic_disable_edge_irq,
404         .enable =       iosapic_enable_edge_irq,
405         .disable =      iosapic_disable_edge_irq,
406         .ack =          iosapic_ack_edge_irq,
407         .end =          iosapic_end_edge_irq,
408         .set_affinity = iosapic_set_affinity
409 };
410
411 unsigned int
412 iosapic_version (char __iomem *addr)
413 {
414         /*
415          * IOSAPIC Version Register return 32 bit structure like:
416          * {
417          *      unsigned int version   : 8;
418          *      unsigned int reserved1 : 8;
419          *      unsigned int max_redir : 8;
420          *      unsigned int reserved2 : 8;
421          * }
422          */
423         return iosapic_read(addr, IOSAPIC_VERSION);
424 }
425
426 /*
427  * if the given vector is already owned by other,
428  *  assign a new vector for the other and make the vector available
429  */
430 static void __init
431 iosapic_reassign_vector (int vector)
432 {
433         int new_vector;
434
435         if (iosapic_intr_info[vector].rte_index >= 0 || iosapic_intr_info[vector].addr
436             || iosapic_intr_info[vector].gsi_base || iosapic_intr_info[vector].dmode
437             || iosapic_intr_info[vector].polarity || iosapic_intr_info[vector].trigger)
438         {
439                 new_vector = assign_irq_vector(AUTO_ASSIGN);
440                 printk(KERN_INFO "Reassigning vector %d to %d\n", vector, new_vector);
441                 memcpy(&iosapic_intr_info[new_vector], &iosapic_intr_info[vector],
442                        sizeof(struct iosapic_intr_info));
443                 memset(&iosapic_intr_info[vector], 0, sizeof(struct iosapic_intr_info));
444                 iosapic_intr_info[vector].rte_index = -1;
445         }
446 }
447
448 static void
449 register_intr (unsigned int gsi, int vector, unsigned char delivery,
450                unsigned long polarity, unsigned long trigger)
451 {
452         irq_desc_t *idesc;
453         struct hw_interrupt_type *irq_type;
454         int rte_index;
455         int index;
456         unsigned long gsi_base;
457         void __iomem *iosapic_address;
458
459         index = find_iosapic(gsi);
460         if (index < 0) {
461                 printk(KERN_WARNING "%s: No IOSAPIC for GSI %u\n", __FUNCTION__, gsi);
462                 return;
463         }
464
465         iosapic_address = iosapic_lists[index].addr;
466         gsi_base = iosapic_lists[index].gsi_base;
467
468         rte_index = gsi - gsi_base;
469         iosapic_intr_info[vector].rte_index = rte_index;
470         iosapic_intr_info[vector].polarity = polarity;
471         iosapic_intr_info[vector].dmode    = delivery;
472         iosapic_intr_info[vector].addr     = iosapic_address;
473         iosapic_intr_info[vector].gsi_base = gsi_base;
474         iosapic_intr_info[vector].trigger  = trigger;
475         iosapic_intr_info[vector].refcnt++;
476
477         if (trigger == IOSAPIC_EDGE)
478                 irq_type = &irq_type_iosapic_edge;
479         else
480                 irq_type = &irq_type_iosapic_level;
481
482         idesc = irq_descp(vector);
483         if (idesc->handler != irq_type) {
484                 if (idesc->handler != &no_irq_type)
485                         printk(KERN_WARNING "%s: changing vector %d from %s to %s\n",
486                                __FUNCTION__, vector, idesc->handler->typename, irq_type->typename);
487                 idesc->handler = irq_type;
488         }
489 }
490
491 static unsigned int
492 get_target_cpu (unsigned int gsi, int vector)
493 {
494 #ifdef CONFIG_SMP
495         static int cpu = -1;
496
497         /*
498          * If the platform supports redirection via XTP, let it
499          * distribute interrupts.
500          */
501         if (smp_int_redirect & SMP_IRQ_REDIRECTION)
502                 return hard_smp_processor_id();
503
504         /*
505          * Some interrupts (ACPI SCI, for instance) are registered
506          * before the BSP is marked as online.
507          */
508         if (!cpu_online(smp_processor_id()))
509                 return hard_smp_processor_id();
510
511 #ifdef CONFIG_NUMA
512         {
513                 int num_cpus, cpu_index, iosapic_index, numa_cpu, i = 0;
514                 cpumask_t cpu_mask;
515
516                 iosapic_index = find_iosapic(gsi);
517                 if (iosapic_index < 0 ||
518                     iosapic_lists[iosapic_index].node == MAX_NUMNODES)
519                         goto skip_numa_setup;
520
521                 cpu_mask = node_to_cpumask(iosapic_lists[iosapic_index].node);
522
523                 for_each_cpu_mask(numa_cpu, cpu_mask) {
524                         if (!cpu_online(numa_cpu))
525                                 cpu_clear(numa_cpu, cpu_mask);
526                 }
527
528                 num_cpus = cpus_weight(cpu_mask);
529
530                 if (!num_cpus)
531                         goto skip_numa_setup;
532
533                 /* Use vector assigment to distribute across cpus in node */
534                 cpu_index = vector % num_cpus;
535
536                 for (numa_cpu = first_cpu(cpu_mask) ; i < cpu_index ; i++)
537                         numa_cpu = next_cpu(numa_cpu, cpu_mask);
538
539                 if (numa_cpu != NR_CPUS)
540                         return cpu_physical_id(numa_cpu);
541         }
542 skip_numa_setup:
543 #endif
544         /*
545          * Otherwise, round-robin interrupt vectors across all the
546          * processors.  (It'd be nice if we could be smarter in the
547          * case of NUMA.)
548          */
549         do {
550                 if (++cpu >= NR_CPUS)
551                         cpu = 0;
552         } while (!cpu_online(cpu));
553
554         return cpu_physical_id(cpu);
555 #else
556         return hard_smp_processor_id();
557 #endif
558 }
559
560 /*
561  * ACPI can describe IOSAPIC interrupts via static tables and namespace
562  * methods.  This provides an interface to register those interrupts and
563  * program the IOSAPIC RTE.
564  */
565 int
566 iosapic_register_intr (unsigned int gsi,
567                        unsigned long polarity, unsigned long trigger)
568 {
569         int vector;
570         unsigned int dest;
571         unsigned long flags;
572
573         /*
574          * If this GSI has already been registered (i.e., it's a
575          * shared interrupt, or we lost a race to register it),
576          * don't touch the RTE.
577          */
578         spin_lock_irqsave(&iosapic_lock, flags);
579         {
580                 vector = gsi_to_vector(gsi);
581                 if (vector > 0) {
582                         iosapic_intr_info[vector].refcnt++;
583                         spin_unlock_irqrestore(&iosapic_lock, flags);
584                         return vector;
585                 }
586
587                 vector = assign_irq_vector(AUTO_ASSIGN);
588                 dest = get_target_cpu(gsi, vector);
589                 register_intr(gsi, vector, IOSAPIC_LOWEST_PRIORITY,
590                         polarity, trigger);
591
592                 set_rte(vector, dest, 1);
593         }
594         spin_unlock_irqrestore(&iosapic_lock, flags);
595
596         printk(KERN_INFO "GSI %u (%s, %s) -> CPU %d (0x%04x) vector %d\n",
597                gsi, (trigger == IOSAPIC_EDGE ? "edge" : "level"),
598                (polarity == IOSAPIC_POL_HIGH ? "high" : "low"),
599                cpu_logical_id(dest), dest, vector);
600
601         return vector;
602 }
603
604 #ifdef CONFIG_ACPI_DEALLOCATE_IRQ
605 void
606 iosapic_unregister_intr (unsigned int gsi)
607 {
608         unsigned long flags;
609         int irq, vector;
610         irq_desc_t *idesc;
611         int rte_index;
612         unsigned long trigger, polarity;
613
614         /*
615          * If the irq associated with the gsi is not found,
616          * iosapic_unregister_intr() is unbalanced. We need to check
617          * this again after getting locks.
618          */
619         irq = gsi_to_irq(gsi);
620         if (irq < 0) {
621                 printk(KERN_ERR "iosapic_unregister_intr(%u) unbalanced\n", gsi);
622                 WARN_ON(1);
623                 return;
624         }
625         vector = irq_to_vector(irq);
626
627         idesc = irq_descp(irq);
628         spin_lock_irqsave(&idesc->lock, flags);
629         spin_lock(&iosapic_lock);
630         {
631                 rte_index = iosapic_intr_info[vector].rte_index;
632                 if (rte_index < 0) {
633                         spin_unlock(&iosapic_lock);
634                         spin_unlock_irqrestore(&idesc->lock, flags);
635                         printk(KERN_ERR "iosapic_unregister_intr(%u) unbalanced\n", gsi);
636                         WARN_ON(1);
637                         return;
638                 }
639
640                 if (--iosapic_intr_info[vector].refcnt > 0) {
641                         spin_unlock(&iosapic_lock);
642                         spin_unlock_irqrestore(&idesc->lock, flags);
643                         return;
644                 }
645
646                 /*
647                  * If interrupt handlers still exist on the irq
648                  * associated with the gsi, don't unregister the
649                  * interrupt.
650                  */
651                 if (idesc->action) {
652                         iosapic_intr_info[vector].refcnt++;
653                         spin_unlock(&iosapic_lock);
654                         spin_unlock_irqrestore(&idesc->lock, flags);
655                         printk(KERN_WARNING "Cannot unregister GSI. IRQ %u is still in use.\n", irq);
656                         return;
657                 }
658
659                 /* Clear the interrupt controller descriptor. */
660                 idesc->handler = &no_irq_type;
661
662                 trigger  = iosapic_intr_info[vector].trigger;
663                 polarity = iosapic_intr_info[vector].polarity;
664
665                 /* Clear the interrupt information. */
666                 memset(&iosapic_intr_info[vector], 0, sizeof(struct iosapic_intr_info));
667                 iosapic_intr_info[vector].rte_index = -1;       /* mark as unused */
668         }
669         spin_unlock(&iosapic_lock);
670         spin_unlock_irqrestore(&idesc->lock, flags);
671
672         /* Free the interrupt vector */
673         free_irq_vector(vector);
674
675         printk(KERN_INFO "GSI %u (%s, %s) -> vector %d unregisterd.\n",
676                gsi, (trigger == IOSAPIC_EDGE ? "edge" : "level"),
677                (polarity == IOSAPIC_POL_HIGH ? "high" : "low"),
678                vector);
679 }
680 #endif /* CONFIG_ACPI_DEALLOCATE_IRQ */
681
682 /*
683  * ACPI calls this when it finds an entry for a platform interrupt.
684  * Note that the irq_base and IOSAPIC address must be set in iosapic_init().
685  */
686 int __init
687 iosapic_register_platform_intr (u32 int_type, unsigned int gsi,
688                                 int iosapic_vector, u16 eid, u16 id,
689                                 unsigned long polarity, unsigned long trigger)
690 {
691         static const char * const name[] = {"unknown", "PMI", "INIT", "CPEI"};
692         unsigned char delivery;
693         int vector, mask = 0;
694         unsigned int dest = ((id << 8) | eid) & 0xffff;
695
696         switch (int_type) {
697               case ACPI_INTERRUPT_PMI:
698                 vector = iosapic_vector;
699                 /*
700                  * since PMI vector is alloc'd by FW(ACPI) not by kernel,
701                  * we need to make sure the vector is available
702                  */
703                 iosapic_reassign_vector(vector);
704                 delivery = IOSAPIC_PMI;
705                 break;
706               case ACPI_INTERRUPT_INIT:
707                 vector = assign_irq_vector(AUTO_ASSIGN);
708                 delivery = IOSAPIC_INIT;
709                 break;
710               case ACPI_INTERRUPT_CPEI:
711                 vector = IA64_CPE_VECTOR;
712                 delivery = IOSAPIC_LOWEST_PRIORITY;
713                 mask = 1;
714                 break;
715               default:
716                 printk(KERN_ERR "iosapic_register_platform_irq(): invalid int type 0x%x\n", int_type);
717                 return -1;
718         }
719
720         register_intr(gsi, vector, delivery, polarity, trigger);
721
722         printk(KERN_INFO "PLATFORM int %s (0x%x): GSI %u (%s, %s) -> CPU %d (0x%04x) vector %d\n",
723                int_type < ARRAY_SIZE(name) ? name[int_type] : "unknown",
724                int_type, gsi, (trigger == IOSAPIC_EDGE ? "edge" : "level"),
725                (polarity == IOSAPIC_POL_HIGH ? "high" : "low"),
726                cpu_logical_id(dest), dest, vector);
727
728         set_rte(vector, dest, mask);
729         return vector;
730 }
731
732
733 /*
734  * ACPI calls this when it finds an entry for a legacy ISA IRQ override.
735  * Note that the gsi_base and IOSAPIC address must be set in iosapic_init().
736  */
737 void __init
738 iosapic_override_isa_irq (unsigned int isa_irq, unsigned int gsi,
739                           unsigned long polarity,
740                           unsigned long trigger)
741 {
742         int vector;
743         unsigned int dest = hard_smp_processor_id();
744
745         vector = isa_irq_to_vector(isa_irq);
746
747         register_intr(gsi, vector, IOSAPIC_LOWEST_PRIORITY, polarity, trigger);
748
749         DBG("ISA: IRQ %u -> GSI %u (%s,%s) -> CPU %d (0x%04x) vector %d\n",
750             isa_irq, gsi, trigger == IOSAPIC_EDGE ? "edge" : "level",
751             polarity == IOSAPIC_POL_HIGH ? "high" : "low",
752             cpu_logical_id(dest), dest, vector);
753
754         set_rte(vector, dest, 1);
755 }
756
757 void __init
758 iosapic_system_init (int system_pcat_compat)
759 {
760         int vector;
761
762         for (vector = 0; vector < IA64_NUM_VECTORS; ++vector)
763                 iosapic_intr_info[vector].rte_index = -1;       /* mark as unused */
764
765         pcat_compat = system_pcat_compat;
766         if (pcat_compat) {
767                 /*
768                  * Disable the compatibility mode interrupts (8259 style), needs IN/OUT support
769                  * enabled.
770                  */
771                 printk(KERN_INFO "%s: Disabling PC-AT compatible 8259 interrupts\n", __FUNCTION__);
772                 outb(0xff, 0xA1);
773                 outb(0xff, 0x21);
774         }
775 }
776
777 void __init
778 iosapic_init (unsigned long phys_addr, unsigned int gsi_base)
779 {
780         int num_rte;
781         unsigned int isa_irq, ver;
782         char __iomem *addr;
783
784         addr = ioremap(phys_addr, 0);
785         ver = iosapic_version(addr);
786
787         /*
788          * The MAX_REDIR register holds the highest input pin
789          * number (starting from 0).
790          * We add 1 so that we can use it for number of pins (= RTEs)
791          */
792         num_rte = ((ver >> 16) & 0xff) + 1;
793
794         iosapic_lists[num_iosapic].addr = addr;
795         iosapic_lists[num_iosapic].gsi_base = gsi_base;
796         iosapic_lists[num_iosapic].num_rte = num_rte;
797 #ifdef CONFIG_NUMA
798         iosapic_lists[num_iosapic].node = MAX_NUMNODES;
799 #endif
800         num_iosapic++;
801
802         if ((gsi_base == 0) && pcat_compat) {
803                 /*
804                  * Map the legacy ISA devices into the IOSAPIC data.  Some of these may
805                  * get reprogrammed later on with data from the ACPI Interrupt Source
806                  * Override table.
807                  */
808                 for (isa_irq = 0; isa_irq < 16; ++isa_irq)
809                         iosapic_override_isa_irq(isa_irq, isa_irq, IOSAPIC_POL_HIGH, IOSAPIC_EDGE);
810         }
811 }
812
813 #ifdef CONFIG_NUMA
814 void __init
815 map_iosapic_to_node(unsigned int gsi_base, int node)
816 {
817         int index;
818
819         index = find_iosapic(gsi_base);
820         if (index < 0) {
821                 printk(KERN_WARNING "%s: No IOSAPIC for GSI %u\n",
822                        __FUNCTION__, gsi_base);
823                 return;
824         }
825         iosapic_lists[index].node = node;
826         return;
827 }
828 #endif