3d023856405a128b83c5c9309813443552118faf
[pandora-kernel.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                 if (hvlpevent_is_int(event))
151                         int_received((struct pci_event *)event, regs);
152                 else
153                         printk(KERN_ERR
154                                 "pci_event_handler: unexpected ack received\n");
155         } else if (event)
156                 printk(KERN_ERR
157                         "pci_event_handler: Unrecognized PCI event type 0x%x\n",
158                         (int)event->xType);
159         else
160                 printk(KERN_ERR "pci_event_handler: NULL event received\n");
161 }
162
163 /*
164  * This is called by init_IRQ.  set in ppc_md.init_IRQ by iSeries_setup.c
165  * It must be called before the bus walk.
166  */
167 void __init iSeries_init_IRQ(void)
168 {
169         /* Register PCI event handler and open an event path */
170         int ret;
171
172         ret = HvLpEvent_registerHandler(HvLpEvent_Type_PciIo,
173                         &pci_event_handler);
174         if (ret == 0) {
175                 ret = HvLpEvent_openPath(HvLpEvent_Type_PciIo, 0);
176                 if (ret != 0)
177                         printk(KERN_ERR "iseries_init_IRQ: open event path "
178                                         "failed with rc 0x%x\n", ret);
179         } else
180                 printk(KERN_ERR "iseries_init_IRQ: register handler "
181                                 "failed with rc 0x%x\n", ret);
182 }
183
184 #define REAL_IRQ_TO_SUBBUS(irq) (((irq) >> 14) & 0xff)
185 #define REAL_IRQ_TO_BUS(irq)    ((((irq) >> 6) & 0xff) + 1)
186 #define REAL_IRQ_TO_IDSEL(irq)  ((((irq) >> 3) & 7) + 1)
187 #define REAL_IRQ_TO_FUNC(irq)   ((irq) & 7)
188
189 /*
190  * This will be called by device drivers (via enable_IRQ)
191  * to enable INTA in the bridge interrupt status register.
192  */
193 static void iseries_enable_IRQ(unsigned int irq)
194 {
195         u32 bus, dev_id, function, mask;
196         const u32 sub_bus = 0;
197         unsigned int rirq = virt_irq_to_real_map[irq];
198
199         /* The IRQ has already been locked by the caller */
200         bus = REAL_IRQ_TO_BUS(rirq);
201         function = REAL_IRQ_TO_FUNC(rirq);
202         dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
203
204         /* Unmask secondary INTA */
205         mask = 0x80000000;
206         HvCallPci_unmaskInterrupts(bus, sub_bus, dev_id, mask);
207 }
208
209 /* This is called by iseries_activate_IRQs */
210 static unsigned int iseries_startup_IRQ(unsigned int irq)
211 {
212         u32 bus, dev_id, function, mask;
213         const u32 sub_bus = 0;
214         unsigned int rirq = virt_irq_to_real_map[irq];
215
216         bus = REAL_IRQ_TO_BUS(rirq);
217         function = REAL_IRQ_TO_FUNC(rirq);
218         dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
219
220         /* Link the IRQ number to the bridge */
221         HvCallXm_connectBusUnit(bus, sub_bus, dev_id, irq);
222
223         /* Unmask bridge interrupts in the FISR */
224         mask = 0x01010000 << function;
225         HvCallPci_unmaskFisr(bus, sub_bus, dev_id, mask);
226         iseries_enable_IRQ(irq);
227         return 0;
228 }
229
230 /*
231  * This is called out of iSeries_fixup to activate interrupt
232  * generation for usable slots
233  */
234 void __init iSeries_activate_IRQs()
235 {
236         int irq;
237         unsigned long flags;
238
239         for_each_irq (irq) {
240                 irq_desc_t *desc = get_irq_desc(irq);
241
242                 if (desc && desc->handler && desc->handler->startup) {
243                         spin_lock_irqsave(&desc->lock, flags);
244                         desc->handler->startup(irq);
245                         spin_unlock_irqrestore(&desc->lock, flags);
246                 }
247         }
248 }
249
250 /*  this is not called anywhere currently */
251 static void iseries_shutdown_IRQ(unsigned int irq)
252 {
253         u32 bus, dev_id, function, mask;
254         const u32 sub_bus = 0;
255         unsigned int rirq = virt_irq_to_real_map[irq];
256
257         /* irq should be locked by the caller */
258         bus = REAL_IRQ_TO_BUS(rirq);
259         function = REAL_IRQ_TO_FUNC(rirq);
260         dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
261
262         /* Invalidate the IRQ number in the bridge */
263         HvCallXm_connectBusUnit(bus, sub_bus, dev_id, 0);
264
265         /* Mask bridge interrupts in the FISR */
266         mask = 0x01010000 << function;
267         HvCallPci_maskFisr(bus, sub_bus, dev_id, mask);
268 }
269
270 /*
271  * This will be called by device drivers (via disable_IRQ)
272  * to disable INTA in the bridge interrupt status register.
273  */
274 static void iseries_disable_IRQ(unsigned int irq)
275 {
276         u32 bus, dev_id, function, mask;
277         const u32 sub_bus = 0;
278         unsigned int rirq = virt_irq_to_real_map[irq];
279
280         /* The IRQ has already been locked by the caller */
281         bus = REAL_IRQ_TO_BUS(rirq);
282         function = REAL_IRQ_TO_FUNC(rirq);
283         dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
284
285         /* Mask secondary INTA   */
286         mask = 0x80000000;
287         HvCallPci_maskInterrupts(bus, sub_bus, dev_id, mask);
288 }
289
290 static void iseries_end_IRQ(unsigned int irq)
291 {
292         unsigned int rirq = virt_irq_to_real_map[irq];
293
294         HvCallPci_eoi(REAL_IRQ_TO_BUS(rirq), REAL_IRQ_TO_SUBBUS(rirq),
295                 (REAL_IRQ_TO_IDSEL(rirq) << 4) + REAL_IRQ_TO_FUNC(rirq));
296 }
297
298 static hw_irq_controller iSeries_IRQ_handler = {
299         .typename = "iSeries irq controller",
300         .startup = iseries_startup_IRQ,
301         .shutdown = iseries_shutdown_IRQ,
302         .enable = iseries_enable_IRQ,
303         .disable = iseries_disable_IRQ,
304         .end = iseries_end_IRQ
305 };
306
307 /*
308  * This is called out of iSeries_scan_slot to allocate an IRQ for an EADS slot
309  * It calculates the irq value for the slot.
310  * Note that sub_bus is always 0 (at the moment at least).
311  */
312 int __init iSeries_allocate_IRQ(HvBusNumber bus,
313                 HvSubBusNumber sub_bus, HvAgentId dev_id)
314 {
315         int virtirq;
316         unsigned int realirq;
317         u8 idsel = (dev_id >> 4);
318         u8 function = dev_id & 7;
319
320         realirq = (((((sub_bus << 8) + (bus - 1)) << 3) + (idsel - 1)) << 3)
321                 + function;
322         virtirq = virt_irq_create_mapping(realirq);
323
324         irq_desc[virtirq].handler = &iSeries_IRQ_handler;
325         return virtirq;
326 }
327
328 /*
329  * Get the next pending IRQ.
330  */
331 int iSeries_get_irq(struct pt_regs *regs)
332 {
333         struct paca_struct *lpaca;
334         /* -2 means ignore this interrupt */
335         int irq = -2;
336
337         lpaca = get_paca();
338 #ifdef CONFIG_SMP
339         if (lpaca->lppaca.int_dword.fields.ipi_cnt) {
340                 lpaca->lppaca.int_dword.fields.ipi_cnt = 0;
341                 iSeries_smp_message_recv(regs);
342         }
343 #endif /* CONFIG_SMP */
344         if (hvlpevent_is_pending())
345                 process_hvlpevents(regs);
346
347         if (num_pending_irqs) {
348                 spin_lock(&pending_irqs_lock);
349                 for (irq = 0; irq < NR_IRQS; irq++) {
350                         if (pending_irqs[irq]) {
351                                 pending_irqs[irq]--;
352                                 num_pending_irqs--;
353                                 break;
354                         }
355                 }
356                 spin_unlock(&pending_irqs_lock);
357                 if (irq >= NR_IRQS)
358                         irq = -2;
359         }
360
361         return irq;
362 }