Merge branch 'release' of master.kernel.org:/pub/scm/linux/kernel/git/aegl/linux-2.6
[linux-2.6] / 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         return;
63 }
64
65 int sn_setup_msi_irq(unsigned int irq, struct pci_dev *pdev)
66 {
67         struct msi_msg msg;
68         struct msi_desc *entry;
69         int widget;
70         int status;
71         nasid_t nasid;
72         u64 bus_addr;
73         struct sn_irq_info *sn_irq_info;
74         struct pcibus_bussoft *bussoft = SN_PCIDEV_BUSSOFT(pdev);
75         struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
76
77         entry = get_irq_data(irq);
78         if (!entry->msi_attrib.is_64)
79                 return -EINVAL;
80
81         if (bussoft == NULL)
82                 return -EINVAL;
83
84         if (provider == NULL || provider->dma_map_consistent == NULL)
85                 return -EINVAL;
86
87         /*
88          * Set up the vector plumbing.  Let the prom (via sn_intr_alloc)
89          * decide which cpu to direct this msi at by default.
90          */
91
92         nasid = NASID_GET(bussoft->bs_base);
93         widget = (nasid & 1) ?
94                         TIO_SWIN_WIDGETNUM(bussoft->bs_base) :
95                         SWIN_WIDGETNUM(bussoft->bs_base);
96
97         sn_irq_info = kzalloc(sizeof(struct sn_irq_info), GFP_KERNEL);
98         if (! sn_irq_info)
99                 return -ENOMEM;
100
101         status = sn_intr_alloc(nasid, widget, sn_irq_info, irq, -1, -1);
102         if (status) {
103                 kfree(sn_irq_info);
104                 return -ENOMEM;
105         }
106
107         sn_irq_info->irq_int_bit = -1;          /* mark this as an MSI irq */
108         sn_irq_fixup(pdev, sn_irq_info);
109
110         /* Prom probably should fill these in, but doesn't ... */
111         sn_irq_info->irq_bridge_type = bussoft->bs_asic_type;
112         sn_irq_info->irq_bridge = (void *)bussoft->bs_base;
113
114         /*
115          * Map the xio address into bus space
116          */
117         bus_addr = (*provider->dma_map_consistent)(pdev,
118                                         sn_irq_info->irq_xtalkaddr,
119                                         sizeof(sn_irq_info->irq_xtalkaddr),
120                                         SN_DMA_MSI|SN_DMA_ADDR_XIO);
121         if (! bus_addr) {
122                 sn_intr_free(nasid, widget, sn_irq_info);
123                 kfree(sn_irq_info);
124                 return -ENOMEM;
125         }
126
127         sn_msi_info[irq].sn_irq_info = sn_irq_info;
128         sn_msi_info[irq].pci_addr = bus_addr;
129
130         msg.address_hi = (u32)(bus_addr >> 32);
131         msg.address_lo = (u32)(bus_addr & 0x00000000ffffffff);
132
133         /*
134          * In the SN platform, bit 16 is a "send vector" bit which
135          * must be present in order to move the vector through the system.
136          */
137         msg.data = 0x100 + irq;
138
139         write_msi_msg(irq, &msg);
140         set_irq_chip_and_handler(irq, &sn_msi_chip, handle_edge_irq);
141
142         return 0;
143 }
144
145 #ifdef CONFIG_SMP
146 static void sn_set_msi_irq_affinity(unsigned int irq, cpumask_t cpu_mask)
147 {
148         struct msi_msg msg;
149         int slice;
150         nasid_t nasid;
151         u64 bus_addr;
152         struct pci_dev *pdev;
153         struct pcidev_info *sn_pdev;
154         struct sn_irq_info *sn_irq_info;
155         struct sn_irq_info *new_irq_info;
156         struct sn_pcibus_provider *provider;
157         unsigned int cpu;
158
159         cpu = first_cpu(cpu_mask);
160         sn_irq_info = sn_msi_info[irq].sn_irq_info;
161         if (sn_irq_info == NULL || sn_irq_info->irq_int_bit >= 0)
162                 return;
163
164         /*
165          * Release XIO resources for the old MSI PCI address
166          */
167
168         read_msi_msg(irq, &msg);
169         sn_pdev = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
170         pdev = sn_pdev->pdi_linux_pcidev;
171         provider = SN_PCIDEV_BUSPROVIDER(pdev);
172
173         bus_addr = (u64)(msg.address_hi) << 32 | (u64)(msg.address_lo);
174         (*provider->dma_unmap)(pdev, bus_addr, PCI_DMA_FROMDEVICE);
175         sn_msi_info[irq].pci_addr = 0;
176
177         nasid = cpuid_to_nasid(cpu);
178         slice = cpuid_to_slice(cpu);
179
180         new_irq_info = sn_retarget_vector(sn_irq_info, nasid, slice);
181         sn_msi_info[irq].sn_irq_info = new_irq_info;
182         if (new_irq_info == NULL)
183                 return;
184
185         /*
186          * Map the xio address into bus space
187          */
188
189         bus_addr = (*provider->dma_map_consistent)(pdev,
190                                         new_irq_info->irq_xtalkaddr,
191                                         sizeof(new_irq_info->irq_xtalkaddr),
192                                         SN_DMA_MSI|SN_DMA_ADDR_XIO);
193
194         sn_msi_info[irq].pci_addr = bus_addr;
195         msg.address_hi = (u32)(bus_addr >> 32);
196         msg.address_lo = (u32)(bus_addr & 0x00000000ffffffff);
197
198         write_msi_msg(irq, &msg);
199         set_native_irq_info(irq, cpu_mask);
200 }
201 #endif /* CONFIG_SMP */
202
203 static void sn_ack_msi_irq(unsigned int irq)
204 {
205         move_native_irq(irq);
206         ia64_eoi();
207 }
208
209 static int sn_msi_retrigger_irq(unsigned int irq)
210 {
211         unsigned int vector = irq;
212         ia64_resend_irq(vector);
213
214         return 1;
215 }
216
217 static struct irq_chip sn_msi_chip = {
218         .name           = "PCI-MSI",
219         .mask           = mask_msi_irq,
220         .unmask         = unmask_msi_irq,
221         .ack            = sn_ack_msi_irq,
222 #ifdef CONFIG_SMP
223         .set_affinity   = sn_set_msi_irq_affinity,
224 #endif
225         .retrigger      = sn_msi_retrigger_irq,
226 };