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