Merge Paulus' tree
[linux-2.6] / 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/ppcdebug.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
43 #include "irq.h"
44 #include "call_pci.h"
45
46 /* This maps virtual irq numbers to real irqs */
47 unsigned int virt_irq_to_real_map[NR_IRQS];
48
49 /* The next available virtual irq number */
50 /* Note: the pcnet32 driver assumes irq numbers < 2 aren't valid. :( */
51 static int next_virtual_irq = 2;
52
53 static long Pci_Interrupt_Count;
54 static long Pci_Event_Count;
55
56 enum XmPciLpEvent_Subtype {
57         XmPciLpEvent_BusCreated         = 0,    // PHB has been created
58         XmPciLpEvent_BusError           = 1,    // PHB has failed
59         XmPciLpEvent_BusFailed          = 2,    // Msg to Secondary, Primary failed bus
60         XmPciLpEvent_NodeFailed         = 4,    // Multi-adapter bridge has failed
61         XmPciLpEvent_NodeRecovered      = 5,    // Multi-adapter bridge has recovered
62         XmPciLpEvent_BusRecovered       = 12,   // PHB has been recovered
63         XmPciLpEvent_UnQuiesceBus       = 18,   // Secondary bus unqiescing
64         XmPciLpEvent_BridgeError        = 21,   // Bridge Error
65         XmPciLpEvent_SlotInterrupt      = 22    // Slot interrupt
66 };
67
68 struct XmPciLpEvent_BusInterrupt {
69         HvBusNumber     busNumber;
70         HvSubBusNumber  subBusNumber;
71 };
72
73 struct XmPciLpEvent_NodeInterrupt {
74         HvBusNumber     busNumber;
75         HvSubBusNumber  subBusNumber;
76         HvAgentId       deviceId;
77 };
78
79 struct XmPciLpEvent {
80         struct HvLpEvent hvLpEvent;
81
82         union {
83                 u64 alignData;                  // Align on an 8-byte boundary
84
85                 struct {
86                         u32             fisr;
87                         HvBusNumber     busNumber;
88                         HvSubBusNumber  subBusNumber;
89                         HvAgentId       deviceId;
90                 } slotInterrupt;
91
92                 struct XmPciLpEvent_BusInterrupt busFailed;
93                 struct XmPciLpEvent_BusInterrupt busRecovered;
94                 struct XmPciLpEvent_BusInterrupt busCreated;
95
96                 struct XmPciLpEvent_NodeInterrupt nodeFailed;
97                 struct XmPciLpEvent_NodeInterrupt nodeRecovered;
98
99         } eventData;
100
101 };
102
103 static void intReceived(struct XmPciLpEvent *eventParm,
104                 struct pt_regs *regsParm)
105 {
106         int irq;
107
108         ++Pci_Interrupt_Count;
109
110         switch (eventParm->hvLpEvent.xSubtype) {
111         case XmPciLpEvent_SlotInterrupt:
112                 irq = eventParm->hvLpEvent.xCorrelationToken;
113                 /* Dispatch the interrupt handlers for this irq */
114                 ppc_irq_dispatch_handler(regsParm, irq);
115                 HvCallPci_eoi(eventParm->eventData.slotInterrupt.busNumber,
116                         eventParm->eventData.slotInterrupt.subBusNumber,
117                         eventParm->eventData.slotInterrupt.deviceId);
118                 break;
119                 /* Ignore error recovery events for now */
120         case XmPciLpEvent_BusCreated:
121                 printk(KERN_INFO "intReceived: system bus %d created\n",
122                         eventParm->eventData.busCreated.busNumber);
123                 break;
124         case XmPciLpEvent_BusError:
125         case XmPciLpEvent_BusFailed:
126                 printk(KERN_INFO "intReceived: system bus %d failed\n",
127                         eventParm->eventData.busFailed.busNumber);
128                 break;
129         case XmPciLpEvent_BusRecovered:
130         case XmPciLpEvent_UnQuiesceBus:
131                 printk(KERN_INFO "intReceived: system bus %d recovered\n",
132                         eventParm->eventData.busRecovered.busNumber);
133                 break;
134         case XmPciLpEvent_NodeFailed:
135         case XmPciLpEvent_BridgeError:
136                 printk(KERN_INFO
137                         "intReceived: multi-adapter bridge %d/%d/%d failed\n",
138                         eventParm->eventData.nodeFailed.busNumber,
139                         eventParm->eventData.nodeFailed.subBusNumber,
140                         eventParm->eventData.nodeFailed.deviceId);
141                 break;
142         case XmPciLpEvent_NodeRecovered:
143                 printk(KERN_INFO
144                         "intReceived: multi-adapter bridge %d/%d/%d recovered\n",
145                         eventParm->eventData.nodeRecovered.busNumber,
146                         eventParm->eventData.nodeRecovered.subBusNumber,
147                         eventParm->eventData.nodeRecovered.deviceId);
148                 break;
149         default:
150                 printk(KERN_ERR
151                         "intReceived: unrecognized event subtype 0x%x\n",
152                         eventParm->hvLpEvent.xSubtype);
153                 break;
154         }
155 }
156
157 static void XmPciLpEvent_handler(struct HvLpEvent *eventParm,
158                 struct pt_regs *regsParm)
159 {
160 #ifdef CONFIG_PCI
161         ++Pci_Event_Count;
162
163         if (eventParm && (eventParm->xType == HvLpEvent_Type_PciIo)) {
164                 switch (eventParm->xFlags.xFunction) {
165                 case HvLpEvent_Function_Int:
166                         intReceived((struct XmPciLpEvent *)eventParm, regsParm);
167                         break;
168                 case HvLpEvent_Function_Ack:
169                         printk(KERN_ERR
170                                 "XmPciLpEvent_handler: unexpected ack received\n");
171                         break;
172                 default:
173                         printk(KERN_ERR
174                                 "XmPciLpEvent_handler: unexpected event function %d\n",
175                                 (int)eventParm->xFlags.xFunction);
176                         break;
177                 }
178         } else if (eventParm)
179                 printk(KERN_ERR
180                         "XmPciLpEvent_handler: Unrecognized PCI event type 0x%x\n",
181                         (int)eventParm->xType);
182         else
183                 printk(KERN_ERR "XmPciLpEvent_handler: NULL event received\n");
184 #endif
185 }
186
187 /*
188  * This is called by init_IRQ.  set in ppc_md.init_IRQ by iSeries_setup.c
189  * It must be called before the bus walk.
190  */
191 void __init iSeries_init_IRQ(void)
192 {
193         /* Register PCI event handler and open an event path */
194         int xRc;
195
196         xRc = HvLpEvent_registerHandler(HvLpEvent_Type_PciIo,
197                         &XmPciLpEvent_handler);
198         if (xRc == 0) {
199                 xRc = HvLpEvent_openPath(HvLpEvent_Type_PciIo, 0);
200                 if (xRc != 0)
201                         printk(KERN_ERR "iSeries_init_IRQ: open event path "
202                                         "failed with rc 0x%x\n", xRc);
203         } else
204                 printk(KERN_ERR "iSeries_init_IRQ: register handler "
205                                 "failed with rc 0x%x\n", xRc);
206 }
207
208 #define REAL_IRQ_TO_BUS(irq)    ((((irq) >> 6) & 0xff) + 1)
209 #define REAL_IRQ_TO_IDSEL(irq)  ((((irq) >> 3) & 7) + 1)
210 #define REAL_IRQ_TO_FUNC(irq)   ((irq) & 7)
211
212 /*
213  * This will be called by device drivers (via enable_IRQ)
214  * to enable INTA in the bridge interrupt status register.
215  */
216 static void iSeries_enable_IRQ(unsigned int irq)
217 {
218         u32 bus, deviceId, function, mask;
219         const u32 subBus = 0;
220         unsigned int rirq = virt_irq_to_real_map[irq];
221
222         /* The IRQ has already been locked by the caller */
223         bus = REAL_IRQ_TO_BUS(rirq);
224         function = REAL_IRQ_TO_FUNC(rirq);
225         deviceId = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
226
227         /* Unmask secondary INTA */
228         mask = 0x80000000;
229         HvCallPci_unmaskInterrupts(bus, subBus, deviceId, mask);
230         PPCDBG(PPCDBG_BUSWALK, "iSeries_enable_IRQ 0x%02X.%02X.%02X 0x%04X\n",
231                         bus, subBus, deviceId, irq);
232 }
233
234 /* This is called by iSeries_activate_IRQs */
235 static unsigned int iSeries_startup_IRQ(unsigned int irq)
236 {
237         u32 bus, deviceId, function, mask;
238         const u32 subBus = 0;
239         unsigned int rirq = virt_irq_to_real_map[irq];
240
241         bus = REAL_IRQ_TO_BUS(rirq);
242         function = REAL_IRQ_TO_FUNC(rirq);
243         deviceId = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
244
245         /* Link the IRQ number to the bridge */
246         HvCallXm_connectBusUnit(bus, subBus, deviceId, irq);
247
248         /* Unmask bridge interrupts in the FISR */
249         mask = 0x01010000 << function;
250         HvCallPci_unmaskFisr(bus, subBus, deviceId, mask);
251         iSeries_enable_IRQ(irq);
252         return 0;
253 }
254
255 /*
256  * This is called out of iSeries_fixup to activate interrupt
257  * generation for usable slots
258  */
259 void __init iSeries_activate_IRQs()
260 {
261         int irq;
262         unsigned long flags;
263
264         for_each_irq (irq) {
265                 irq_desc_t *desc = get_irq_desc(irq);
266
267                 if (desc && desc->handler && desc->handler->startup) {
268                         spin_lock_irqsave(&desc->lock, flags);
269                         desc->handler->startup(irq);
270                         spin_unlock_irqrestore(&desc->lock, flags);
271                 }
272         }
273 }
274
275 /*  this is not called anywhere currently */
276 static void iSeries_shutdown_IRQ(unsigned int irq)
277 {
278         u32 bus, deviceId, function, mask;
279         const u32 subBus = 0;
280         unsigned int rirq = virt_irq_to_real_map[irq];
281
282         /* irq should be locked by the caller */
283         bus = REAL_IRQ_TO_BUS(rirq);
284         function = REAL_IRQ_TO_FUNC(rirq);
285         deviceId = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
286
287         /* Invalidate the IRQ number in the bridge */
288         HvCallXm_connectBusUnit(bus, subBus, deviceId, 0);
289
290         /* Mask bridge interrupts in the FISR */
291         mask = 0x01010000 << function;
292         HvCallPci_maskFisr(bus, subBus, deviceId, mask);
293 }
294
295 /*
296  * This will be called by device drivers (via disable_IRQ)
297  * to disable INTA in the bridge interrupt status register.
298  */
299 static void iSeries_disable_IRQ(unsigned int irq)
300 {
301         u32 bus, deviceId, function, mask;
302         const u32 subBus = 0;
303         unsigned int rirq = virt_irq_to_real_map[irq];
304
305         /* The IRQ has already been locked by the caller */
306         bus = REAL_IRQ_TO_BUS(rirq);
307         function = REAL_IRQ_TO_FUNC(rirq);
308         deviceId = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
309
310         /* Mask secondary INTA   */
311         mask = 0x80000000;
312         HvCallPci_maskInterrupts(bus, subBus, deviceId, mask);
313         PPCDBG(PPCDBG_BUSWALK, "iSeries_disable_IRQ 0x%02X.%02X.%02X 0x%04X\n",
314                         bus, subBus, deviceId, irq);
315 }
316
317 /*
318  * Need to define this so ppc_irq_dispatch_handler will NOT call
319  * enable_IRQ at the end of interrupt handling.  However, this does
320  * nothing because there is not enough information provided to do
321  * the EOI HvCall.  This is done by XmPciLpEvent.c
322  */
323 static void iSeries_end_IRQ(unsigned int irq)
324 {
325 }
326
327 static hw_irq_controller iSeries_IRQ_handler = {
328         .typename = "iSeries irq controller",
329         .startup = iSeries_startup_IRQ,
330         .shutdown = iSeries_shutdown_IRQ,
331         .enable = iSeries_enable_IRQ,
332         .disable = iSeries_disable_IRQ,
333         .end = iSeries_end_IRQ
334 };
335
336 /*
337  * This is called out of iSeries_scan_slot to allocate an IRQ for an EADS slot
338  * It calculates the irq value for the slot.
339  * Note that subBusNumber is always 0 (at the moment at least).
340  */
341 int __init iSeries_allocate_IRQ(HvBusNumber busNumber,
342                 HvSubBusNumber subBusNumber, HvAgentId deviceId)
343 {
344         unsigned int realirq, virtirq;
345         u8 idsel = (deviceId >> 4);
346         u8 function = deviceId & 7;
347
348         virtirq = next_virtual_irq++;
349         realirq = ((busNumber - 1) << 6) + ((idsel - 1) << 3) + function;
350         virt_irq_to_real_map[virtirq] = realirq;
351
352         irq_desc[virtirq].handler = &iSeries_IRQ_handler;
353         return virtirq;
354 }
355
356 int virt_irq_create_mapping(unsigned int real_irq)
357 {
358         BUG(); /* Don't call this on iSeries, yet */
359
360         return 0;
361 }
362
363 void virt_irq_init(void)
364 {
365         return;
366 }