Pull bugzilla-1641 into release branch
[linux-2.6] / arch / ia64 / sn / kernel / irq.c
1 /*
2  * Platform dependent support for SGI SN
3  *
4  * This file is subject to the terms and conditions of the GNU General Public
5  * License.  See the file "COPYING" in the main directory of this archive
6  * for more details.
7  *
8  * Copyright (c) 2000-2006 Silicon Graphics, Inc.  All Rights Reserved.
9  */
10
11 #include <linux/irq.h>
12 #include <linux/spinlock.h>
13 #include <linux/init.h>
14 #include <asm/sn/addrs.h>
15 #include <asm/sn/arch.h>
16 #include <asm/sn/intr.h>
17 #include <asm/sn/pcibr_provider.h>
18 #include <asm/sn/pcibus_provider_defs.h>
19 #include <asm/sn/pcidev.h>
20 #include <asm/sn/shub_mmr.h>
21 #include <asm/sn/sn_sal.h>
22
23 static void force_interrupt(int irq);
24 static void register_intr_pda(struct sn_irq_info *sn_irq_info);
25 static void unregister_intr_pda(struct sn_irq_info *sn_irq_info);
26
27 int sn_force_interrupt_flag = 1;
28 extern int sn_ioif_inited;
29 struct list_head **sn_irq_lh;
30 static DEFINE_SPINLOCK(sn_irq_info_lock); /* non-IRQ lock */
31
32 u64 sn_intr_alloc(nasid_t local_nasid, int local_widget,
33                                      struct sn_irq_info *sn_irq_info,
34                                      int req_irq, nasid_t req_nasid,
35                                      int req_slice)
36 {
37         struct ia64_sal_retval ret_stuff;
38         ret_stuff.status = 0;
39         ret_stuff.v0 = 0;
40
41         SAL_CALL_NOLOCK(ret_stuff, (u64) SN_SAL_IOIF_INTERRUPT,
42                         (u64) SAL_INTR_ALLOC, (u64) local_nasid,
43                         (u64) local_widget, __pa(sn_irq_info), (u64) req_irq,
44                         (u64) req_nasid, (u64) req_slice);
45
46         return ret_stuff.status;
47 }
48
49 void sn_intr_free(nasid_t local_nasid, int local_widget,
50                                 struct sn_irq_info *sn_irq_info)
51 {
52         struct ia64_sal_retval ret_stuff;
53         ret_stuff.status = 0;
54         ret_stuff.v0 = 0;
55
56         SAL_CALL_NOLOCK(ret_stuff, (u64) SN_SAL_IOIF_INTERRUPT,
57                         (u64) SAL_INTR_FREE, (u64) local_nasid,
58                         (u64) local_widget, (u64) sn_irq_info->irq_irq,
59                         (u64) sn_irq_info->irq_cookie, 0, 0);
60 }
61
62 u64 sn_intr_redirect(nasid_t local_nasid, int local_widget,
63                       struct sn_irq_info *sn_irq_info,
64                       nasid_t req_nasid, int req_slice)
65 {
66         struct ia64_sal_retval ret_stuff;
67         ret_stuff.status = 0;
68         ret_stuff.v0 = 0;
69
70         SAL_CALL_NOLOCK(ret_stuff, (u64) SN_SAL_IOIF_INTERRUPT,
71                         (u64) SAL_INTR_REDIRECT, (u64) local_nasid,
72                         (u64) local_widget, __pa(sn_irq_info),
73                         (u64) req_nasid, (u64) req_slice, 0);
74
75         return ret_stuff.status;
76 }
77
78 static unsigned int sn_startup_irq(unsigned int irq)
79 {
80         return 0;
81 }
82
83 static void sn_shutdown_irq(unsigned int irq)
84 {
85 }
86
87 static void sn_disable_irq(unsigned int irq)
88 {
89 }
90
91 static void sn_enable_irq(unsigned int irq)
92 {
93 }
94
95 static void sn_ack_irq(unsigned int irq)
96 {
97         u64 event_occurred, mask;
98
99         irq = irq & 0xff;
100         event_occurred = HUB_L((u64*)LOCAL_MMR_ADDR(SH_EVENT_OCCURRED));
101         mask = event_occurred & SH_ALL_INT_MASK;
102         HUB_S((u64*)LOCAL_MMR_ADDR(SH_EVENT_OCCURRED_ALIAS), mask);
103         __set_bit(irq, (volatile void *)pda->sn_in_service_ivecs);
104
105         move_native_irq(irq);
106 }
107
108 static void sn_end_irq(unsigned int irq)
109 {
110         int ivec;
111         u64 event_occurred;
112
113         ivec = irq & 0xff;
114         if (ivec == SGI_UART_VECTOR) {
115                 event_occurred = HUB_L((u64*)LOCAL_MMR_ADDR (SH_EVENT_OCCURRED));
116                 /* If the UART bit is set here, we may have received an
117                  * interrupt from the UART that the driver missed.  To
118                  * make sure, we IPI ourselves to force us to look again.
119                  */
120                 if (event_occurred & SH_EVENT_OCCURRED_UART_INT_MASK) {
121                         platform_send_ipi(smp_processor_id(), SGI_UART_VECTOR,
122                                           IA64_IPI_DM_INT, 0);
123                 }
124         }
125         __clear_bit(ivec, (volatile void *)pda->sn_in_service_ivecs);
126         if (sn_force_interrupt_flag)
127                 force_interrupt(irq);
128 }
129
130 static void sn_irq_info_free(struct rcu_head *head);
131
132 struct sn_irq_info *sn_retarget_vector(struct sn_irq_info *sn_irq_info,
133                                        nasid_t nasid, int slice)
134 {
135         int vector;
136         int cpuid;
137 #ifdef CONFIG_SMP
138         int cpuphys;
139 #endif
140         int64_t bridge;
141         int local_widget, status;
142         nasid_t local_nasid;
143         struct sn_irq_info *new_irq_info;
144         struct sn_pcibus_provider *pci_provider;
145
146         bridge = (u64) sn_irq_info->irq_bridge;
147         if (!bridge) {
148                 return NULL; /* irq is not a device interrupt */
149         }
150
151         local_nasid = NASID_GET(bridge);
152
153         if (local_nasid & 1)
154                 local_widget = TIO_SWIN_WIDGETNUM(bridge);
155         else
156                 local_widget = SWIN_WIDGETNUM(bridge);
157         vector = sn_irq_info->irq_irq;
158
159         /* Make use of SAL_INTR_REDIRECT if PROM supports it */
160         status = sn_intr_redirect(local_nasid, local_widget, sn_irq_info, nasid, slice);
161         if (!status) {
162                 new_irq_info = sn_irq_info;
163                 goto finish_up;
164         }
165
166         /*
167          * PROM does not support SAL_INTR_REDIRECT, or it failed.
168          * Revert to old method.
169          */
170         new_irq_info = kmalloc(sizeof(struct sn_irq_info), GFP_ATOMIC);
171         if (new_irq_info == NULL)
172                 return NULL;
173
174         memcpy(new_irq_info, sn_irq_info, sizeof(struct sn_irq_info));
175
176         /* Free the old PROM new_irq_info structure */
177         sn_intr_free(local_nasid, local_widget, new_irq_info);
178         unregister_intr_pda(new_irq_info);
179
180         /* allocate a new PROM new_irq_info struct */
181         status = sn_intr_alloc(local_nasid, local_widget,
182                                new_irq_info, vector,
183                                nasid, slice);
184
185         /* SAL call failed */
186         if (status) {
187                 kfree(new_irq_info);
188                 return NULL;
189         }
190
191         register_intr_pda(new_irq_info);
192         spin_lock(&sn_irq_info_lock);
193         list_replace_rcu(&sn_irq_info->list, &new_irq_info->list);
194         spin_unlock(&sn_irq_info_lock);
195         call_rcu(&sn_irq_info->rcu, sn_irq_info_free);
196
197
198 finish_up:
199         /* Update kernels new_irq_info with new target info */
200         cpuid = nasid_slice_to_cpuid(new_irq_info->irq_nasid,
201                                      new_irq_info->irq_slice);
202         new_irq_info->irq_cpuid = cpuid;
203
204         pci_provider = sn_pci_provider[new_irq_info->irq_bridge_type];
205
206         /*
207          * If this represents a line interrupt, target it.  If it's
208          * an msi (irq_int_bit < 0), it's already targeted.
209          */
210         if (new_irq_info->irq_int_bit >= 0 &&
211             pci_provider && pci_provider->target_interrupt)
212                 (pci_provider->target_interrupt)(new_irq_info);
213
214 #ifdef CONFIG_SMP
215         cpuphys = cpu_physical_id(cpuid);
216         set_irq_affinity_info((vector & 0xff), cpuphys, 0);
217 #endif
218
219         return new_irq_info;
220 }
221
222 static void sn_set_affinity_irq(unsigned int irq, cpumask_t mask)
223 {
224         struct sn_irq_info *sn_irq_info, *sn_irq_info_safe;
225         nasid_t nasid;
226         int slice;
227
228         nasid = cpuid_to_nasid(first_cpu(mask));
229         slice = cpuid_to_slice(first_cpu(mask));
230
231         list_for_each_entry_safe(sn_irq_info, sn_irq_info_safe,
232                                  sn_irq_lh[irq], list)
233                 (void)sn_retarget_vector(sn_irq_info, nasid, slice);
234 }
235
236 static void
237 sn_mask_irq(unsigned int irq)
238 {
239 }
240
241 static void
242 sn_unmask_irq(unsigned int irq)
243 {
244 }
245
246 struct irq_chip irq_type_sn = {
247         .name           = "SN hub",
248         .startup        = sn_startup_irq,
249         .shutdown       = sn_shutdown_irq,
250         .enable         = sn_enable_irq,
251         .disable        = sn_disable_irq,
252         .ack            = sn_ack_irq,
253         .end            = sn_end_irq,
254         .mask           = sn_mask_irq,
255         .unmask         = sn_unmask_irq,
256         .set_affinity   = sn_set_affinity_irq
257 };
258
259 ia64_vector sn_irq_to_vector(int irq)
260 {
261         if (irq >= IA64_NUM_VECTORS)
262                 return 0;
263         return (ia64_vector)irq;
264 }
265
266 unsigned int sn_local_vector_to_irq(u8 vector)
267 {
268         return (CPU_VECTOR_TO_IRQ(smp_processor_id(), vector));
269 }
270
271 void sn_irq_init(void)
272 {
273         int i;
274         irq_desc_t *base_desc = irq_desc;
275
276         ia64_first_device_vector = IA64_SN2_FIRST_DEVICE_VECTOR;
277         ia64_last_device_vector = IA64_SN2_LAST_DEVICE_VECTOR;
278
279         for (i = 0; i < NR_IRQS; i++) {
280                 if (base_desc[i].chip == &no_irq_type) {
281                         base_desc[i].chip = &irq_type_sn;
282                 }
283         }
284 }
285
286 static void register_intr_pda(struct sn_irq_info *sn_irq_info)
287 {
288         int irq = sn_irq_info->irq_irq;
289         int cpu = sn_irq_info->irq_cpuid;
290
291         if (pdacpu(cpu)->sn_last_irq < irq) {
292                 pdacpu(cpu)->sn_last_irq = irq;
293         }
294
295         if (pdacpu(cpu)->sn_first_irq == 0 || pdacpu(cpu)->sn_first_irq > irq)
296                 pdacpu(cpu)->sn_first_irq = irq;
297 }
298
299 static void unregister_intr_pda(struct sn_irq_info *sn_irq_info)
300 {
301         int irq = sn_irq_info->irq_irq;
302         int cpu = sn_irq_info->irq_cpuid;
303         struct sn_irq_info *tmp_irq_info;
304         int i, foundmatch;
305
306         rcu_read_lock();
307         if (pdacpu(cpu)->sn_last_irq == irq) {
308                 foundmatch = 0;
309                 for (i = pdacpu(cpu)->sn_last_irq - 1;
310                      i && !foundmatch; i--) {
311                         list_for_each_entry_rcu(tmp_irq_info,
312                                                 sn_irq_lh[i],
313                                                 list) {
314                                 if (tmp_irq_info->irq_cpuid == cpu) {
315                                         foundmatch = 1;
316                                         break;
317                                 }
318                         }
319                 }
320                 pdacpu(cpu)->sn_last_irq = i;
321         }
322
323         if (pdacpu(cpu)->sn_first_irq == irq) {
324                 foundmatch = 0;
325                 for (i = pdacpu(cpu)->sn_first_irq + 1;
326                      i < NR_IRQS && !foundmatch; i++) {
327                         list_for_each_entry_rcu(tmp_irq_info,
328                                                 sn_irq_lh[i],
329                                                 list) {
330                                 if (tmp_irq_info->irq_cpuid == cpu) {
331                                         foundmatch = 1;
332                                         break;
333                                 }
334                         }
335                 }
336                 pdacpu(cpu)->sn_first_irq = ((i == NR_IRQS) ? 0 : i);
337         }
338         rcu_read_unlock();
339 }
340
341 static void sn_irq_info_free(struct rcu_head *head)
342 {
343         struct sn_irq_info *sn_irq_info;
344
345         sn_irq_info = container_of(head, struct sn_irq_info, rcu);
346         kfree(sn_irq_info);
347 }
348
349 void sn_irq_fixup(struct pci_dev *pci_dev, struct sn_irq_info *sn_irq_info)
350 {
351         nasid_t nasid = sn_irq_info->irq_nasid;
352         int slice = sn_irq_info->irq_slice;
353         int cpu = nasid_slice_to_cpuid(nasid, slice);
354 #ifdef CONFIG_SMP
355         int cpuphys;
356 #endif
357
358         pci_dev_get(pci_dev);
359         sn_irq_info->irq_cpuid = cpu;
360         sn_irq_info->irq_pciioinfo = SN_PCIDEV_INFO(pci_dev);
361
362         /* link it into the sn_irq[irq] list */
363         spin_lock(&sn_irq_info_lock);
364         list_add_rcu(&sn_irq_info->list, sn_irq_lh[sn_irq_info->irq_irq]);
365         reserve_irq_vector(sn_irq_info->irq_irq);
366         spin_unlock(&sn_irq_info_lock);
367
368         register_intr_pda(sn_irq_info);
369 #ifdef CONFIG_SMP
370         cpuphys = cpu_physical_id(cpu);
371         set_irq_affinity_info(sn_irq_info->irq_irq, cpuphys, 0);
372 #endif
373 }
374
375 void sn_irq_unfixup(struct pci_dev *pci_dev)
376 {
377         struct sn_irq_info *sn_irq_info;
378
379         /* Only cleanup IRQ stuff if this device has a host bus context */
380         if (!SN_PCIDEV_BUSSOFT(pci_dev))
381                 return;
382
383         sn_irq_info = SN_PCIDEV_INFO(pci_dev)->pdi_sn_irq_info;
384         if (!sn_irq_info)
385                 return;
386         if (!sn_irq_info->irq_irq) {
387                 kfree(sn_irq_info);
388                 return;
389         }
390
391         unregister_intr_pda(sn_irq_info);
392         spin_lock(&sn_irq_info_lock);
393         list_del_rcu(&sn_irq_info->list);
394         spin_unlock(&sn_irq_info_lock);
395         if (list_empty(sn_irq_lh[sn_irq_info->irq_irq]))
396                 free_irq_vector(sn_irq_info->irq_irq);
397         call_rcu(&sn_irq_info->rcu, sn_irq_info_free);
398         pci_dev_put(pci_dev);
399
400 }
401
402 static inline void
403 sn_call_force_intr_provider(struct sn_irq_info *sn_irq_info)
404 {
405         struct sn_pcibus_provider *pci_provider;
406
407         pci_provider = sn_pci_provider[sn_irq_info->irq_bridge_type];
408
409         /* Don't force an interrupt if the irq has been disabled */
410         if (!(irq_desc[sn_irq_info->irq_irq].status & IRQ_DISABLED) &&
411             pci_provider && pci_provider->force_interrupt)
412                 (*pci_provider->force_interrupt)(sn_irq_info);
413 }
414
415 static void force_interrupt(int irq)
416 {
417         struct sn_irq_info *sn_irq_info;
418
419         if (!sn_ioif_inited)
420                 return;
421
422         rcu_read_lock();
423         list_for_each_entry_rcu(sn_irq_info, sn_irq_lh[irq], list)
424                 sn_call_force_intr_provider(sn_irq_info);
425
426         rcu_read_unlock();
427 }
428
429 /*
430  * Check for lost interrupts.  If the PIC int_status reg. says that
431  * an interrupt has been sent, but not handled, and the interrupt
432  * is not pending in either the cpu irr regs or in the soft irr regs,
433  * and the interrupt is not in service, then the interrupt may have
434  * been lost.  Force an interrupt on that pin.  It is possible that
435  * the interrupt is in flight, so we may generate a spurious interrupt,
436  * but we should never miss a real lost interrupt.
437  */
438 static void sn_check_intr(int irq, struct sn_irq_info *sn_irq_info)
439 {
440         u64 regval;
441         struct pcidev_info *pcidev_info;
442         struct pcibus_info *pcibus_info;
443
444         /*
445          * Bridge types attached to TIO (anything but PIC) do not need this WAR
446          * since they do not target Shub II interrupt registers.  If that
447          * ever changes, this check needs to accomodate.
448          */
449         if (sn_irq_info->irq_bridge_type != PCIIO_ASIC_TYPE_PIC)
450                 return;
451
452         pcidev_info = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
453         if (!pcidev_info)
454                 return;
455
456         pcibus_info =
457             (struct pcibus_info *)pcidev_info->pdi_host_pcidev_info->
458             pdi_pcibus_info;
459         regval = pcireg_intr_status_get(pcibus_info);
460
461         if (!ia64_get_irr(irq_to_vector(irq))) {
462                 if (!test_bit(irq, pda->sn_in_service_ivecs)) {
463                         regval &= 0xff;
464                         if (sn_irq_info->irq_int_bit & regval &
465                             sn_irq_info->irq_last_intr) {
466                                 regval &= ~(sn_irq_info->irq_int_bit & regval);
467                                 sn_call_force_intr_provider(sn_irq_info);
468                         }
469                 }
470         }
471         sn_irq_info->irq_last_intr = regval;
472 }
473
474 void sn_lb_int_war_check(void)
475 {
476         struct sn_irq_info *sn_irq_info;
477         int i;
478
479         if (!sn_ioif_inited || pda->sn_first_irq == 0)
480                 return;
481
482         rcu_read_lock();
483         for (i = pda->sn_first_irq; i <= pda->sn_last_irq; i++) {
484                 list_for_each_entry_rcu(sn_irq_info, sn_irq_lh[i], list) {
485                         sn_check_intr(i, sn_irq_info);
486                 }
487         }
488         rcu_read_unlock();
489 }
490
491 void __init sn_irq_lh_init(void)
492 {
493         int i;
494
495         sn_irq_lh = kmalloc(sizeof(struct list_head *) * NR_IRQS, GFP_KERNEL);
496         if (!sn_irq_lh)
497                 panic("SN PCI INIT: Failed to allocate memory for PCI init\n");
498
499         for (i = 0; i < NR_IRQS; i++) {
500                 sn_irq_lh[i] = kmalloc(sizeof(struct list_head), GFP_KERNEL);
501                 if (!sn_irq_lh[i])
502                         panic("SN PCI INIT: Failed IRQ memory allocation\n");
503
504                 INIT_LIST_HEAD(sn_irq_lh[i]);
505         }
506 }