powerpc: partly merge iseries do_IRQ
[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/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 void int_received(struct pci_event *event, struct pt_regs *regs)
86 {
87         int irq;
88 #ifdef CONFIG_IRQSTACKS
89         struct thread_info *curtp, *irqtp;
90 #endif
91
92         switch (event->event.xSubtype) {
93         case pe_slot_interrupt:
94                 irq = event->event.xCorrelationToken;
95                 /* Dispatch the interrupt handlers for this irq */
96 #ifdef CONFIG_IRQSTACKS
97                 /* Switch to the irq stack to handle this */
98                 curtp = current_thread_info();
99                 irqtp = hardirq_ctx[smp_processor_id()];
100                 if (curtp != irqtp) {
101                         irqtp->task = curtp->task;
102                         irqtp->flags = 0;
103                         call___do_IRQ(irq, regs, irqtp);
104                         irqtp->task = NULL;
105                         if (irqtp->flags)
106                                 set_bits(irqtp->flags, &curtp->flags);
107                 } else
108 #endif
109                         __do_IRQ(irq, regs);
110                 break;
111                 /* Ignore error recovery events for now */
112         case pe_bus_created:
113                 printk(KERN_INFO "int_received: system bus %d created\n",
114                         event->data.bus.bus_number);
115                 break;
116         case pe_bus_error:
117         case pe_bus_failed:
118                 printk(KERN_INFO "int_received: system bus %d failed\n",
119                         event->data.bus.bus_number);
120                 break;
121         case pe_bus_recovered:
122         case pe_unquiese_bus:
123                 printk(KERN_INFO "int_received: system bus %d recovered\n",
124                         event->data.bus.bus_number);
125                 break;
126         case pe_node_failed:
127         case pe_bridge_error:
128                 printk(KERN_INFO
129                         "int_received: multi-adapter bridge %d/%d/%d failed\n",
130                         event->data.node.bus_number,
131                         event->data.node.sub_bus_number,
132                         event->data.node.dev_id);
133                 break;
134         case pe_node_recovered:
135                 printk(KERN_INFO
136                         "int_received: multi-adapter bridge %d/%d/%d recovered\n",
137                         event->data.node.bus_number,
138                         event->data.node.sub_bus_number,
139                         event->data.node.dev_id);
140                 break;
141         default:
142                 printk(KERN_ERR
143                         "int_received: unrecognized event subtype 0x%x\n",
144                         event->event.xSubtype);
145                 break;
146         }
147 }
148
149 static void pci_event_handler(struct HvLpEvent *event, struct pt_regs *regs)
150 {
151         if (event && (event->xType == HvLpEvent_Type_PciIo)) {
152                 switch (event->xFlags.xFunction) {
153                 case HvLpEvent_Function_Int:
154                         int_received((struct pci_event *)event, regs);
155                         break;
156                 case HvLpEvent_Function_Ack:
157                         printk(KERN_ERR
158                                 "pci_event_handler: unexpected ack received\n");
159                         break;
160                 default:
161                         printk(KERN_ERR
162                                 "pci_event_handler: unexpected event function %d\n",
163                                 (int)event->xFlags.xFunction);
164                         break;
165                 }
166         } else if (event)
167                 printk(KERN_ERR
168                         "pci_event_handler: Unrecognized PCI event type 0x%x\n",
169                         (int)event->xType);
170         else
171                 printk(KERN_ERR "pci_event_handler: NULL event received\n");
172 }
173
174 /*
175  * This is called by init_IRQ.  set in ppc_md.init_IRQ by iSeries_setup.c
176  * It must be called before the bus walk.
177  */
178 void __init iSeries_init_IRQ(void)
179 {
180         /* Register PCI event handler and open an event path */
181         int ret;
182
183         ret = HvLpEvent_registerHandler(HvLpEvent_Type_PciIo,
184                         &pci_event_handler);
185         if (ret == 0) {
186                 ret = HvLpEvent_openPath(HvLpEvent_Type_PciIo, 0);
187                 if (ret != 0)
188                         printk(KERN_ERR "iseries_init_IRQ: open event path "
189                                         "failed with rc 0x%x\n", ret);
190         } else
191                 printk(KERN_ERR "iseries_init_IRQ: register handler "
192                                 "failed with rc 0x%x\n", ret);
193 }
194
195 #define REAL_IRQ_TO_SUBBUS(irq) (((irq) >> 14) & 0xff)
196 #define REAL_IRQ_TO_BUS(irq)    ((((irq) >> 6) & 0xff) + 1)
197 #define REAL_IRQ_TO_IDSEL(irq)  ((((irq) >> 3) & 7) + 1)
198 #define REAL_IRQ_TO_FUNC(irq)   ((irq) & 7)
199
200 /*
201  * This will be called by device drivers (via enable_IRQ)
202  * to enable INTA in the bridge interrupt status register.
203  */
204 static void iseries_enable_IRQ(unsigned int irq)
205 {
206         u32 bus, dev_id, function, mask;
207         const u32 sub_bus = 0;
208         unsigned int rirq = virt_irq_to_real_map[irq];
209
210         /* The IRQ has already been locked by the caller */
211         bus = REAL_IRQ_TO_BUS(rirq);
212         function = REAL_IRQ_TO_FUNC(rirq);
213         dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
214
215         /* Unmask secondary INTA */
216         mask = 0x80000000;
217         HvCallPci_unmaskInterrupts(bus, sub_bus, dev_id, mask);
218 }
219
220 /* This is called by iseries_activate_IRQs */
221 static unsigned int iseries_startup_IRQ(unsigned int irq)
222 {
223         u32 bus, dev_id, function, mask;
224         const u32 sub_bus = 0;
225         unsigned int rirq = virt_irq_to_real_map[irq];
226
227         bus = REAL_IRQ_TO_BUS(rirq);
228         function = REAL_IRQ_TO_FUNC(rirq);
229         dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
230
231         /* Link the IRQ number to the bridge */
232         HvCallXm_connectBusUnit(bus, sub_bus, dev_id, irq);
233
234         /* Unmask bridge interrupts in the FISR */
235         mask = 0x01010000 << function;
236         HvCallPci_unmaskFisr(bus, sub_bus, dev_id, mask);
237         iseries_enable_IRQ(irq);
238         return 0;
239 }
240
241 /*
242  * This is called out of iSeries_fixup to activate interrupt
243  * generation for usable slots
244  */
245 void __init iSeries_activate_IRQs()
246 {
247         int irq;
248         unsigned long flags;
249
250         for_each_irq (irq) {
251                 irq_desc_t *desc = get_irq_desc(irq);
252
253                 if (desc && desc->handler && desc->handler->startup) {
254                         spin_lock_irqsave(&desc->lock, flags);
255                         desc->handler->startup(irq);
256                         spin_unlock_irqrestore(&desc->lock, flags);
257                 }
258         }
259 }
260
261 /*  this is not called anywhere currently */
262 static void iseries_shutdown_IRQ(unsigned int irq)
263 {
264         u32 bus, dev_id, function, mask;
265         const u32 sub_bus = 0;
266         unsigned int rirq = virt_irq_to_real_map[irq];
267
268         /* irq should be locked by the caller */
269         bus = REAL_IRQ_TO_BUS(rirq);
270         function = REAL_IRQ_TO_FUNC(rirq);
271         dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
272
273         /* Invalidate the IRQ number in the bridge */
274         HvCallXm_connectBusUnit(bus, sub_bus, dev_id, 0);
275
276         /* Mask bridge interrupts in the FISR */
277         mask = 0x01010000 << function;
278         HvCallPci_maskFisr(bus, sub_bus, dev_id, mask);
279 }
280
281 /*
282  * This will be called by device drivers (via disable_IRQ)
283  * to disable INTA in the bridge interrupt status register.
284  */
285 static void iseries_disable_IRQ(unsigned int irq)
286 {
287         u32 bus, dev_id, function, mask;
288         const u32 sub_bus = 0;
289         unsigned int rirq = virt_irq_to_real_map[irq];
290
291         /* The IRQ has already been locked by the caller */
292         bus = REAL_IRQ_TO_BUS(rirq);
293         function = REAL_IRQ_TO_FUNC(rirq);
294         dev_id = (REAL_IRQ_TO_IDSEL(rirq) << 4) + function;
295
296         /* Mask secondary INTA   */
297         mask = 0x80000000;
298         HvCallPci_maskInterrupts(bus, sub_bus, dev_id, mask);
299 }
300
301 static void iseries_end_IRQ(unsigned int irq)
302 {
303         unsigned int rirq = virt_irq_to_real_map[irq];
304
305         HvCallPci_eoi(REAL_IRQ_TO_BUS(rirq), REAL_IRQ_TO_SUBBUS(rirq),
306                 (REAL_IRQ_TO_IDSEL(rirq) << 4) + REAL_IRQ_TO_FUNC(rirq));
307 }
308
309 static hw_irq_controller iSeries_IRQ_handler = {
310         .typename = "iSeries irq controller",
311         .startup = iseries_startup_IRQ,
312         .shutdown = iseries_shutdown_IRQ,
313         .enable = iseries_enable_IRQ,
314         .disable = iseries_disable_IRQ,
315         .end = iseries_end_IRQ
316 };
317
318 /*
319  * This is called out of iSeries_scan_slot to allocate an IRQ for an EADS slot
320  * It calculates the irq value for the slot.
321  * Note that sub_bus is always 0 (at the moment at least).
322  */
323 int __init iSeries_allocate_IRQ(HvBusNumber bus,
324                 HvSubBusNumber sub_bus, HvAgentId dev_id)
325 {
326         int virtirq;
327         unsigned int realirq;
328         u8 idsel = (dev_id >> 4);
329         u8 function = dev_id & 7;
330
331         realirq = (((((sub_bus << 8) + (bus - 1)) << 3) + (idsel - 1)) << 3)
332                 + function;
333         virtirq = virt_irq_create_mapping(realirq);
334
335         irq_desc[virtirq].handler = &iSeries_IRQ_handler;
336         return virtirq;
337 }
338
339 /*
340  * Get the next pending IRQ.
341  */
342 int iSeries_get_irq(struct pt_regs *regs)
343 {
344         struct paca_struct *lpaca;
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         /* -2 means ignore this interrupt */
357         return -2;
358 }