msi: Make MSI useable more architectures
[linux-2.6.git] / arch / ia64 / sn / kernel / msi_sn.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 2006 Silicon Graphics, Inc.  All Rights Reserved.
7  */
8
9 #include <linux/types.h>
10 #include <linux/irq.h>
11 #include <linux/pci.h>
12 #include <linux/cpumask.h>
13 #include <linux/msi.h>
14
15 #include <asm/sn/addrs.h>
16 #include <asm/sn/intr.h>
17 #include <asm/sn/pcibus_provider_defs.h>
18 #include <asm/sn/pcidev.h>
19 #include <asm/sn/nodepda.h>
20
21 struct sn_msi_info {
22         u64 pci_addr;
23         struct sn_irq_info *sn_irq_info;
24 };
25
26 static struct sn_msi_info sn_msi_info[NR_IRQS];
27
28 static struct irq_chip sn_msi_chip;
29
30 void sn_teardown_msi_irq(unsigned int irq)
31 {
32         nasid_t nasid;
33         int widget;
34         struct pci_dev *pdev;
35         struct pcidev_info *sn_pdev;
36         struct sn_irq_info *sn_irq_info;
37         struct pcibus_bussoft *bussoft;
38         struct sn_pcibus_provider *provider;
39
40         sn_irq_info = sn_msi_info[irq].sn_irq_info;
41         if (sn_irq_info == NULL || sn_irq_info->irq_int_bit >= 0)
42                 return;
43
44         sn_pdev = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
45         pdev = sn_pdev->pdi_linux_pcidev;
46         provider = SN_PCIDEV_BUSPROVIDER(pdev);
47
48         (*provider->dma_unmap)(pdev,
49                                sn_msi_info[irq].pci_addr,
50                                PCI_DMA_FROMDEVICE);
51         sn_msi_info[irq].pci_addr = 0;
52
53         bussoft = SN_PCIDEV_BUSSOFT(pdev);
54         nasid = NASID_GET(bussoft->bs_base);
55         widget = (nasid & 1) ?
56                         TIO_SWIN_WIDGETNUM(bussoft->bs_base) :
57                         SWIN_WIDGETNUM(bussoft->bs_base);
58
59         sn_intr_free(nasid, widget, sn_irq_info);
60         sn_msi_info[irq].sn_irq_info = NULL;
61
62         destroy_irq(irq);
63 }
64
65 int sn_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *entry)
66 {
67         struct msi_msg msg;
68         int widget;
69         int status;
70         nasid_t nasid;
71         u64 bus_addr;
72         struct sn_irq_info *sn_irq_info;
73         struct pcibus_bussoft *bussoft = SN_PCIDEV_BUSSOFT(pdev);
74         struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
75         int irq;
76
77         if (!entry->msi_attrib.is_64)
78                 return -EINVAL;
79
80         if (bussoft == NULL)
81                 return -EINVAL;
82
83         if (provider == NULL || provider->dma_map_consistent == NULL)
84                 return -EINVAL;
85
86         irq = create_irq();
87         if (irq < 0)
88                 return irq;
89
90         set_irq_msi(irq, entry);
91         /*
92          * Set up the vector plumbing.  Let the prom (via sn_intr_alloc)
93          * decide which cpu to direct this msi at by default.
94          */
95
96         nasid = NASID_GET(bussoft->bs_base);
97         widget = (nasid & 1) ?
98                         TIO_SWIN_WIDGETNUM(bussoft->bs_base) :
99                         SWIN_WIDGETNUM(bussoft->bs_base);
100
101         sn_irq_info = kzalloc(sizeof(struct sn_irq_info), GFP_KERNEL);
102         if (! sn_irq_info) {
103                 destroy_irq(irq);
104                 return -ENOMEM;
105         }
106
107         status = sn_intr_alloc(nasid, widget, sn_irq_info, irq, -1, -1);
108         if (status) {
109                 kfree(sn_irq_info);
110                 destroy_irq(irq);
111                 return -ENOMEM;
112         }
113
114         sn_irq_info->irq_int_bit = -1;          /* mark this as an MSI irq */
115         sn_irq_fixup(pdev, sn_irq_info);
116
117         /* Prom probably should fill these in, but doesn't ... */
118         sn_irq_info->irq_bridge_type = bussoft->bs_asic_type;
119         sn_irq_info->irq_bridge = (void *)bussoft->bs_base;
120
121         /*
122          * Map the xio address into bus space
123          */
124         bus_addr = (*provider->dma_map_consistent)(pdev,
125                                         sn_irq_info->irq_xtalkaddr,
126                                         sizeof(sn_irq_info->irq_xtalkaddr),
127                                         SN_DMA_MSI|SN_DMA_ADDR_XIO);
128         if (! bus_addr) {
129                 sn_intr_free(nasid, widget, sn_irq_info);
130                 kfree(sn_irq_info);
131                 destroy_irq(irq);
132                 return -ENOMEM;
133         }
134
135         sn_msi_info[irq].sn_irq_info = sn_irq_info;
136         sn_msi_info[irq].pci_addr = bus_addr;
137
138         msg.address_hi = (u32)(bus_addr >> 32);
139         msg.address_lo = (u32)(bus_addr & 0x00000000ffffffff);
140
141         /*
142          * In the SN platform, bit 16 is a "send vector" bit which
143          * must be present in order to move the vector through the system.
144          */
145         msg.data = 0x100 + irq;
146
147         write_msi_msg(irq, &msg);
148         set_irq_chip_and_handler(irq, &sn_msi_chip, handle_edge_irq);
149
150         return irq;
151 }
152
153 #ifdef CONFIG_SMP
154 static void sn_set_msi_irq_affinity(unsigned int irq, cpumask_t cpu_mask)
155 {
156         struct msi_msg msg;
157         int slice;
158         nasid_t nasid;
159         u64 bus_addr;
160         struct pci_dev *pdev;
161         struct pcidev_info *sn_pdev;
162         struct sn_irq_info *sn_irq_info;
163         struct sn_irq_info *new_irq_info;
164         struct sn_pcibus_provider *provider;
165         unsigned int cpu;
166
167         cpu = first_cpu(cpu_mask);
168         sn_irq_info = sn_msi_info[irq].sn_irq_info;
169         if (sn_irq_info == NULL || sn_irq_info->irq_int_bit >= 0)
170                 return;
171
172         /*
173          * Release XIO resources for the old MSI PCI address
174          */
175
176         read_msi_msg(irq, &msg);
177         sn_pdev = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
178         pdev = sn_pdev->pdi_linux_pcidev;
179         provider = SN_PCIDEV_BUSPROVIDER(pdev);
180
181         bus_addr = (u64)(msg.address_hi) << 32 | (u64)(msg.address_lo);
182         (*provider->dma_unmap)(pdev, bus_addr, PCI_DMA_FROMDEVICE);
183         sn_msi_info[irq].pci_addr = 0;
184
185         nasid = cpuid_to_nasid(cpu);
186         slice = cpuid_to_slice(cpu);
187
188         new_irq_info = sn_retarget_vector(sn_irq_info, nasid, slice);
189         sn_msi_info[irq].sn_irq_info = new_irq_info;
190         if (new_irq_info == NULL)
191                 return;
192
193         /*
194          * Map the xio address into bus space
195          */
196
197         bus_addr = (*provider->dma_map_consistent)(pdev,
198                                         new_irq_info->irq_xtalkaddr,
199                                         sizeof(new_irq_info->irq_xtalkaddr),
200                                         SN_DMA_MSI|SN_DMA_ADDR_XIO);
201
202         sn_msi_info[irq].pci_addr = bus_addr;
203         msg.address_hi = (u32)(bus_addr >> 32);
204         msg.address_lo = (u32)(bus_addr & 0x00000000ffffffff);
205
206         write_msi_msg(irq, &msg);
207         set_native_irq_info(irq, cpu_mask);
208 }
209 #endif /* CONFIG_SMP */
210
211 static void sn_ack_msi_irq(unsigned int irq)
212 {
213         move_native_irq(irq);
214         ia64_eoi();
215 }
216
217 static int sn_msi_retrigger_irq(unsigned int irq)
218 {
219         unsigned int vector = irq;
220         ia64_resend_irq(vector);
221
222         return 1;
223 }
224
225 static struct irq_chip sn_msi_chip = {
226         .name           = "PCI-MSI",
227         .mask           = mask_msi_irq,
228         .unmask         = unmask_msi_irq,
229         .ack            = sn_ack_msi_irq,
230 #ifdef CONFIG_SMP
231         .set_affinity   = sn_set_msi_irq_affinity,
232 #endif
233         .retrigger      = sn_msi_retrigger_irq,
234 };