Merge master.kernel.org:/home/rmk/linux-2.6-serial
[powerpc.git] / arch / powerpc / platforms / iseries / irq.c
1 /*
2  * This module supports the iSeries PCI bus interrupt handling
3  * Copyright (C) 20yy  <Robert L Holtorf> <IBM Corp>
4  * Copyright (C) 2004-2005 IBM Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the:
18  * Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330,
20  * Boston, MA  02111-1307  USA
21  *
22  * Change Activity:
23  *   Created, December 13, 2000 by Wayne Holm
24  * End Change Activity
25  */
26 #include <linux/config.h>
27 #include <linux/pci.h>
28 #include <linux/init.h>
29 #include <linux/threads.h>
30 #include <linux/smp.h>
31 #include <linux/param.h>
32 #include <linux/string.h>
33 #include <linux/bootmem.h>
34 #include <linux/ide.h>
35 #include <linux/irq.h>
36 #include <linux/spinlock.h>
37
38 #include <asm/paca.h>
39 #include <asm/iseries/hv_types.h>
40 #include <asm/iseries/hv_lp_event.h>
41 #include <asm/iseries/hv_call_xm.h>
42 #include <asm/iseries/it_lp_queue.h>
43
44 #include "irq.h"
45 #include "call_pci.h"
46
47 #if defined(CONFIG_SMP)
48 extern void iSeries_smp_message_recv(struct pt_regs *);
49 #endif
50
51 enum pci_event_type {
52         pe_bus_created          = 0,    /* PHB has been created */
53         pe_bus_error            = 1,    /* PHB has failed */
54         pe_bus_failed           = 2,    /* Msg to Secondary, Primary failed bus */
55         pe_node_failed          = 4,    /* Multi-adapter bridge has failed */
56         pe_node_recovered       = 5,    /* Multi-adapter bridge has recovered */
57         pe_bus_recovered        = 12,   /* PHB has been recovered */
58         pe_unquiese_bus         = 18,   /* Secondary bus unqiescing */
59         pe_bridge_error         = 21,   /* Bridge Error */
60         pe_slot_interrupt       = 22    /* Slot interrupt */
61 };
62
63 struct pci_event {
64         struct HvLpEvent event;
65         union {
66                 u64 __align;            /* Align on an 8-byte boundary */
67                 struct {
68                         u32             fisr;
69                         HvBusNumber     bus_number;
70                         HvSubBusNumber  sub_bus_number;
71                         HvAgentId       dev_id;
72                 } slot;
73                 struct {
74                         HvBusNumber     bus_number;
75                         HvSubBusNumber  sub_bus_number;
76                 } bus;
77                 struct {
78                         HvBusNumber     bus_number;
79                         HvSubBusNumber  sub_bus_number;
80                         HvAgentId       dev_id;
81                 } node;
82         } data;
83 };
84
85 static DEFINE_SPINLOCK(pending_irqs_lock);
86 static int num_pending_irqs;
87 static int pending_irqs[NR_IRQS];
88
89 static void int_received(struct pci_event *event, struct pt_regs *regs)
90 {
91         int irq;
92
93         switch (event->event.xSubtype) {
94         case pe_slot_interrupt:
95                 irq = event->event.xCorrelationToken;
96                 if (irq < NR_IRQS) {
97                         spin_lock(&pending_irqs_lock);
98                         pending_irqs[irq]++;
99                         num_pending_irqs++;
100                         spin_unlock(&pending_irqs_lock);
101                 } else {
102                         printk(KERN_WARNING "int_received: bad irq number %d\n",
103                                         irq);
104                         HvCallPci_eoi(event->data.slot.bus_number,
105                                         event->data.slot.sub_bus_number,
106                                         event->data.slot.dev_id);
107                 }
108                 break;
109                 /* Ignore error recovery events for now */
110         case pe_bus_created:
111                 printk(KERN_INFO "int_received: system bus %d created\n",
112                         event->data.bus.bus_number);
113                 break;
114         case pe_bus_error:
115         case pe_bus_failed:
116                 printk(KERN_INFO "int_received: system bus %d failed\n",
117                         event->data.bus.bus_number);
118                 break;
119         case pe_bus_recovered:
120         case pe_unquiese_bus:
121                 printk(KERN_INFO "int_received: system bus %d recovered\n",
122                         event->data.bus.bus_number);
123                 break;
124         case pe_node_failed:
125         case pe_bridge_error:
126                 printk(KERN_INFO
127                         "int_received: multi-adapter bridge %d/%d/%d failed\n",
128                         event->data.node.bus_number,
129                         event->data.node.sub_bus_number,
130                         event->data.node.dev_id);
131                 break;
132         case pe_node_recovered:
133                 printk(KERN_INFO
134                         "int_received: multi-adapter bridge %d/%d/%d recovered\n",
135                         event->data.node.bus_number,
136                         event->data.node.sub_bus_number,
137                         event->data.node.dev_id);
138                 break;
139         default:
140                 printk(KERN_ERR
141                         "int_received: unrecognized event subtype 0x%x\n",
142                         event->event.xSubtype);
143                 break;
144         }
145 }
146
147 static void pci_event_handler(struct HvLpEvent *event, struct pt_regs *regs)
148 {
149         if (event && (event->xType == HvLpEvent_Type_PciIo)) {
150                 switch (event->xFlags.xFunction) {
151                 case HvLpEvent_Function_Int:
152                         int_received((struct pci_event *)event, regs);
153                         break;
154                 case HvLpEvent_Function_Ack:
155                         printk(KERN_ERR
156                                 "pci_event_handler: unexpected ack received\n");
157                         break;
158                 default:
159                         printk(KERN_ERR
160                                 "pci_event_handler: unexpected event function %d\n",
161                                 (int)event->xFlags.xFunction);
162                         break;
163                 }
164         } else if (event)
165                 printk(KERN_ERR
166                         "pci_event_handler: Unrecognized PCI event type 0x%x\n",
167                         (int)event->xType);
168         else
169                 printk(KERN_ERR "pci_event_handler: NULL event received\n");
170 }
171
172 /*
173  * This is called by init_IRQ.  set in ppc_md.init_IRQ by iSeries_setup.c
174  * It must be called before the bus walk.
175  */
176 void __init iSeries_init_IRQ(void)
177 {
178         /* Register PCI event handler and open an event path */
179         int ret;
180
181         ret = HvLpEvent_registerHandler(HvLpEvent_Type_PciIo,
182                         &pci_event_handler);
183         if (ret == 0) {
184                 ret = HvLpEvent_openPath(HvLpEvent_Type_PciIo, 0);
185                 if (ret != 0)
186                         printk(KERN_ERR "iseries_init_IRQ: open event path "
187                                         "failed with rc 0x%x\n", ret);
188         } else
189                 printk(KERN_ERR "iseries_init_IRQ: register handler "
190                                 "failed with rc 0x%x\n", ret);
191 }
192
193 #define REAL_IRQ_TO_SUBBUS(irq) (((irq) >> 14) & 0xff)
194 #define REAL_IRQ_TO_BUS(irq)    ((((irq) >> 6) & 0xff) + 1)
195 #define REAL_IRQ_TO_IDSEL(irq)  ((((irq) >> 3) & 7) + 1)
196 #define REAL_IRQ_TO_FUNC(irq)   ((irq) & 7)
197
198 /*
199  * This will be called by device drivers (via enable_IRQ)
200  * to enable INTA in the bridge interrupt status register.
201  */
202 static void iseries_enable_IRQ(unsigned int irq)
203 {
204         u32 bus, dev_id, function, mask;
205         const u32 sub_bus = 0;
206         unsigned int rirq = virt_irq_to_real_map[irq];
207
208         /* The IRQ has already been locked by the caller */
209         bus = REAL_IRQ_TO_BUS(rirq);
210         function = REAL_IRQ_TO_FUNC(rirq);
211         dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
212
213         /* Unmask secondary INTA */
214         mask = 0x80000000;
215         HvCallPci_unmaskInterrupts(bus, sub_bus, dev_id, mask);
216 }
217
218 /* This is called by iseries_activate_IRQs */
219 static unsigned int iseries_startup_IRQ(unsigned int irq)
220 {
221         u32 bus, dev_id, function, mask;
222         const u32 sub_bus = 0;
223         unsigned int rirq = virt_irq_to_real_map[irq];
224
225         bus = REAL_IRQ_TO_BUS(rirq);
226         function = REAL_IRQ_TO_FUNC(rirq);
227         dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
228
229         /* Link the IRQ number to the bridge */
230         HvCallXm_connectBusUnit(bus, sub_bus, dev_id, irq);
231
232         /* Unmask bridge interrupts in the FISR */
233         mask = 0x01010000 << function;
234         HvCallPci_unmaskFisr(bus, sub_bus, dev_id, mask);
235         iseries_enable_IRQ(irq);
236         return 0;
237 }
238
239 /*
240  * This is called out of iSeries_fixup to activate interrupt
241  * generation for usable slots
242  */
243 void __init iSeries_activate_IRQs()
244 {
245         int irq;
246         unsigned long flags;
247
248         for_each_irq (irq) {
249                 irq_desc_t *desc = get_irq_desc(irq);
250
251                 if (desc && desc->handler && desc->handler->startup) {
252                         spin_lock_irqsave(&desc->lock, flags);
253                         desc->handler->startup(irq);
254                         spin_unlock_irqrestore(&desc->lock, flags);
255                 }
256         }
257 }
258
259 /*  this is not called anywhere currently */
260 static void iseries_shutdown_IRQ(unsigned int irq)
261 {
262         u32 bus, dev_id, function, mask;
263         const u32 sub_bus = 0;
264         unsigned int rirq = virt_irq_to_real_map[irq];
265
266         /* irq should be locked by the caller */
267         bus = REAL_IRQ_TO_BUS(rirq);
268         function = REAL_IRQ_TO_FUNC(rirq);
269         dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
270
271         /* Invalidate the IRQ number in the bridge */
272         HvCallXm_connectBusUnit(bus, sub_bus, dev_id, 0);
273
274         /* Mask bridge interrupts in the FISR */
275         mask = 0x01010000 << function;
276         HvCallPci_maskFisr(bus, sub_bus, dev_id, mask);
277 }
278
279 /*
280  * This will be called by device drivers (via disable_IRQ)
281  * to disable INTA in the bridge interrupt status register.
282  */
283 static void iseries_disable_IRQ(unsigned int irq)
284 {
285         u32 bus, dev_id, function, mask;
286         const u32 sub_bus = 0;
287         unsigned int rirq = virt_irq_to_real_map[irq];
288
289         /* The IRQ has already been locked by the caller */
290         bus = REAL_IRQ_TO_BUS(rirq);
291         function = REAL_IRQ_TO_FUNC(rirq);
292         dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
293
294         /* Mask secondary INTA   */
295         mask = 0x80000000;
296         HvCallPci_maskInterrupts(bus, sub_bus, dev_id, mask);
297 }
298
299 static void iseries_end_IRQ(unsigned int irq)
300 {
301         unsigned int rirq = virt_irq_to_real_map[irq];
302
303         HvCallPci_eoi(REAL_IRQ_TO_BUS(rirq), REAL_IRQ_TO_SUBBUS(rirq),
304                 (REAL_IRQ_TO_IDSEL(rirq) << 4) + REAL_IRQ_TO_FUNC(rirq));
305 }
306
307 static hw_irq_controller iSeries_IRQ_handler = {
308         .typename = "iSeries irq controller",
309         .startup = iseries_startup_IRQ,
310         .shutdown = iseries_shutdown_IRQ,
311         .enable = iseries_enable_IRQ,
312         .disable = iseries_disable_IRQ,
313         .end = iseries_end_IRQ
314 };
315
316 /*
317  * This is called out of iSeries_scan_slot to allocate an IRQ for an EADS slot
318  * It calculates the irq value for the slot.
319  * Note that sub_bus is always 0 (at the moment at least).
320  */
321 int __init iSeries_allocate_IRQ(HvBusNumber bus,
322                 HvSubBusNumber sub_bus, HvAgentId dev_id)
323 {
324         int virtirq;
325         unsigned int realirq;
326         u8 idsel = (dev_id >> 4);
327         u8 function = dev_id & 7;
328
329         realirq = (((((sub_bus << 8) + (bus - 1)) << 3) + (idsel - 1)) << 3)
330                 + function;
331         virtirq = virt_irq_create_mapping(realirq);
332
333         irq_desc[virtirq].handler = &iSeries_IRQ_handler;
334         return virtirq;
335 }
336
337 /*
338  * Get the next pending IRQ.
339  */
340 int iSeries_get_irq(struct pt_regs *regs)
341 {
342         struct paca_struct *lpaca;
343         /* -2 means ignore this interrupt */
344         int irq = -2;
345
346         lpaca = get_paca();
347 #ifdef CONFIG_SMP
348         if (lpaca->lppaca.int_dword.fields.ipi_cnt) {
349                 lpaca->lppaca.int_dword.fields.ipi_cnt = 0;
350                 iSeries_smp_message_recv(regs);
351         }
352 #endif /* CONFIG_SMP */
353         if (hvlpevent_is_pending())
354                 process_hvlpevents(regs);
355
356         if (num_pending_irqs) {
357                 spin_lock(&pending_irqs_lock);
358                 for (irq = 0; irq < NR_IRQS; irq++) {
359                         if (pending_irqs[irq]) {
360                                 pending_irqs[irq]--;
361                                 num_pending_irqs--;
362                                 break;
363                         }
364                 }
365                 spin_unlock(&pending_irqs_lock);
366                 if (irq >= NR_IRQS)
367                         irq = -2;
368         }
369
370         return irq;
371 }