7aedc2ac8c28a1a2d03a9b7a814130e6db9dcc74
[linux-2.6.git] / drivers / pci / msi-altix.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/pci.h>
11 #include <linux/cpumask.h>
12
13 #include <asm/sn/addrs.h>
14 #include <asm/sn/intr.h>
15 #include <asm/sn/pcibus_provider_defs.h>
16 #include <asm/sn/pcidev.h>
17 #include <asm/sn/nodepda.h>
18
19 #include "msi.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;
27
28 static void
29 sn_msi_teardown(unsigned int irq)
30 {
31         nasid_t nasid;
32         int widget;
33         struct pci_dev *pdev;
34         struct pcidev_info *sn_pdev;
35         struct sn_irq_info *sn_irq_info;
36         struct pcibus_bussoft *bussoft;
37         struct sn_pcibus_provider *provider;
38
39         sn_irq_info = sn_msi_info[irq].sn_irq_info;
40         if (sn_irq_info == NULL || sn_irq_info->irq_int_bit >= 0)
41                 return;
42
43         sn_pdev = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
44         pdev = sn_pdev->pdi_linux_pcidev;
45         provider = SN_PCIDEV_BUSPROVIDER(pdev);
46
47         (*provider->dma_unmap)(pdev,
48                                sn_msi_info[irq].pci_addr,
49                                PCI_DMA_FROMDEVICE);
50         sn_msi_info[irq].pci_addr = 0;
51
52         bussoft = SN_PCIDEV_BUSSOFT(pdev);
53         nasid = NASID_GET(bussoft->bs_base);
54         widget = (nasid & 1) ?
55                         TIO_SWIN_WIDGETNUM(bussoft->bs_base) :
56                         SWIN_WIDGETNUM(bussoft->bs_base);
57
58         sn_intr_free(nasid, widget, sn_irq_info);
59         sn_msi_info[irq].sn_irq_info = NULL;
60
61         return;
62 }
63
64 int
65 sn_msi_setup(struct pci_dev *pdev, unsigned int irq, struct msi_msg *msg)
66 {
67         int widget;
68         int status;
69         nasid_t nasid;
70         u64 bus_addr;
71         struct sn_irq_info *sn_irq_info;
72         struct pcibus_bussoft *bussoft = SN_PCIDEV_BUSSOFT(pdev);
73         struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
74
75         if (bussoft == NULL)
76                 return -EINVAL;
77
78         if (provider == NULL || provider->dma_map_consistent == NULL)
79                 return -EINVAL;
80
81         /*
82          * Set up the vector plumbing.  Let the prom (via sn_intr_alloc)
83          * decide which cpu to direct this msi at by default.
84          */
85
86         nasid = NASID_GET(bussoft->bs_base);
87         widget = (nasid & 1) ?
88                         TIO_SWIN_WIDGETNUM(bussoft->bs_base) :
89                         SWIN_WIDGETNUM(bussoft->bs_base);
90
91         sn_irq_info = kzalloc(sizeof(struct sn_irq_info), GFP_KERNEL);
92         if (! sn_irq_info)
93                 return -ENOMEM;
94
95         status = sn_intr_alloc(nasid, widget, sn_irq_info, irq, -1, -1);
96         if (status) {
97                 kfree(sn_irq_info);
98                 return -ENOMEM;
99         }
100
101         sn_irq_info->irq_int_bit = -1;          /* mark this as an MSI irq */
102         sn_irq_fixup(pdev, sn_irq_info);
103
104         /* Prom probably should fill these in, but doesn't ... */
105         sn_irq_info->irq_bridge_type = bussoft->bs_asic_type;
106         sn_irq_info->irq_bridge = (void *)bussoft->bs_base;
107
108         /*
109          * Map the xio address into bus space
110          */
111         bus_addr = (*provider->dma_map_consistent)(pdev,
112                                         sn_irq_info->irq_xtalkaddr,
113                                         sizeof(sn_irq_info->irq_xtalkaddr),
114                                         SN_DMA_MSI|SN_DMA_ADDR_XIO);
115         if (! bus_addr) {
116                 sn_intr_free(nasid, widget, sn_irq_info);
117                 kfree(sn_irq_info);
118                 return -ENOMEM;
119         }
120
121         sn_msi_info[irq].sn_irq_info = sn_irq_info;
122         sn_msi_info[irq].pci_addr = bus_addr;
123
124         msg->address_hi = (u32)(bus_addr >> 32);
125         msg->address_lo = (u32)(bus_addr & 0x00000000ffffffff);
126
127         /*
128          * In the SN platform, bit 16 is a "send vector" bit which
129          * must be present in order to move the vector through the system.
130          */
131         msg->data = 0x100 + irq;
132
133 #ifdef CONFIG_SMP
134         set_irq_affinity_info(irq, sn_irq_info->irq_cpuid, 0);
135 #endif
136
137         return 0;
138 }
139
140 static void
141 sn_msi_target(unsigned int irq, cpumask_t cpu_mask, struct msi_msg *msg)
142 {
143         int slice;
144         nasid_t nasid;
145         u64 bus_addr;
146         struct pci_dev *pdev;
147         struct pcidev_info *sn_pdev;
148         struct sn_irq_info *sn_irq_info;
149         struct sn_irq_info *new_irq_info;
150         struct sn_pcibus_provider *provider;
151         unsigned int cpu;
152
153         cpu = first_cpu(cpu_mask);
154         sn_irq_info = sn_msi_info[irq].sn_irq_info;
155         if (sn_irq_info == NULL || sn_irq_info->irq_int_bit >= 0)
156                 return;
157
158         /*
159          * Release XIO resources for the old MSI PCI address
160          */
161
162         sn_pdev = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
163         pdev = sn_pdev->pdi_linux_pcidev;
164         provider = SN_PCIDEV_BUSPROVIDER(pdev);
165
166         bus_addr = (u64)(msg->address_hi) << 32 | (u64)(msg->address_lo);
167         (*provider->dma_unmap)(pdev, bus_addr, PCI_DMA_FROMDEVICE);
168         sn_msi_info[irq].pci_addr = 0;
169
170         nasid = cpuid_to_nasid(cpu);
171         slice = cpuid_to_slice(cpu);
172
173         new_irq_info = sn_retarget_vector(sn_irq_info, nasid, slice);
174         sn_msi_info[irq].sn_irq_info = new_irq_info;
175         if (new_irq_info == NULL)
176                 return;
177
178         /*
179          * Map the xio address into bus space
180          */
181
182         bus_addr = (*provider->dma_map_consistent)(pdev,
183                                         new_irq_info->irq_xtalkaddr,
184                                         sizeof(new_irq_info->irq_xtalkaddr),
185                                         SN_DMA_MSI|SN_DMA_ADDR_XIO);
186
187         sn_msi_info[irq].pci_addr = bus_addr;
188         msg->address_hi = (u32)(bus_addr >> 32);
189         msg->address_lo = (u32)(bus_addr & 0x00000000ffffffff);
190 }
191
192 struct msi_ops sn_msi_ops = {
193         .needs_64bit_address = 1,
194         .setup = sn_msi_setup,
195         .teardown = sn_msi_teardown,
196 #ifdef CONFIG_SMP
197         .target = sn_msi_target,
198 #endif
199 };
200
201 int
202 sn_msi_init(void)
203 {
204         sn_msi_info =
205                 kzalloc(sizeof(struct sn_msi_info) * NR_IRQS, GFP_KERNEL);
206         if (! sn_msi_info)
207                 return -ENOMEM;
208
209         msi_register(&sn_msi_ops);
210         return 0;
211 }