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 #include <linux/slab.h>
15 
16 #include <asm/sn/addrs.h>
17 #include <asm/sn/intr.h>
18 #include <asm/sn/pcibus_provider_defs.h>
19 #include <asm/sn/pcidev.h>
20 #include <asm/sn/nodepda.h>
21 
22 struct sn_msi_info {
23 	u64 pci_addr;
24 	struct sn_irq_info *sn_irq_info;
25 };
26 
27 static struct sn_msi_info sn_msi_info[NR_IRQS];
28 
29 static struct irq_chip sn_msi_chip;
30 
sn_teardown_msi_irq(unsigned int irq)31 void sn_teardown_msi_irq(unsigned int irq)
32 {
33 	nasid_t nasid;
34 	int widget;
35 	struct pci_dev *pdev;
36 	struct pcidev_info *sn_pdev;
37 	struct sn_irq_info *sn_irq_info;
38 	struct pcibus_bussoft *bussoft;
39 	struct sn_pcibus_provider *provider;
40 
41 	sn_irq_info = sn_msi_info[irq].sn_irq_info;
42 	if (sn_irq_info == NULL || sn_irq_info->irq_int_bit >= 0)
43 		return;
44 
45 	sn_pdev = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
46 	pdev = sn_pdev->pdi_linux_pcidev;
47 	provider = SN_PCIDEV_BUSPROVIDER(pdev);
48 
49 	(*provider->dma_unmap)(pdev,
50 			       sn_msi_info[irq].pci_addr,
51 			       PCI_DMA_FROMDEVICE);
52 	sn_msi_info[irq].pci_addr = 0;
53 
54 	bussoft = SN_PCIDEV_BUSSOFT(pdev);
55 	nasid = NASID_GET(bussoft->bs_base);
56 	widget = (nasid & 1) ?
57 			TIO_SWIN_WIDGETNUM(bussoft->bs_base) :
58 			SWIN_WIDGETNUM(bussoft->bs_base);
59 
60 	sn_intr_free(nasid, widget, sn_irq_info);
61 	sn_msi_info[irq].sn_irq_info = NULL;
62 
63 	destroy_irq(irq);
64 }
65 
sn_setup_msi_irq(struct pci_dev * pdev,struct msi_desc * entry)66 int sn_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *entry)
67 {
68 	struct msi_msg msg;
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 	int irq;
77 
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 	irq = create_irq();
88 	if (irq < 0)
89 		return irq;
90 
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 	irq_set_msi_desc(irq, entry);
148 	pci_write_msi_msg(irq, &msg);
149 	irq_set_chip_and_handler(irq, &sn_msi_chip, handle_edge_irq);
150 
151 	return 0;
152 }
153 
154 #ifdef CONFIG_SMP
sn_set_msi_irq_affinity(struct irq_data * data,const struct cpumask * cpu_mask,bool force)155 static int sn_set_msi_irq_affinity(struct irq_data *data,
156 				   const struct cpumask *cpu_mask, bool force)
157 {
158 	struct msi_msg msg;
159 	int slice;
160 	nasid_t nasid;
161 	u64 bus_addr;
162 	struct pci_dev *pdev;
163 	struct pcidev_info *sn_pdev;
164 	struct sn_irq_info *sn_irq_info;
165 	struct sn_irq_info *new_irq_info;
166 	struct sn_pcibus_provider *provider;
167 	unsigned int cpu, irq = data->irq;
168 
169 	cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
170 	sn_irq_info = sn_msi_info[irq].sn_irq_info;
171 	if (sn_irq_info == NULL || sn_irq_info->irq_int_bit >= 0)
172 		return -1;
173 
174 	/*
175 	 * Release XIO resources for the old MSI PCI address
176 	 */
177 
178 	__get_cached_msi_msg(irq_data_get_msi_desc(data), &msg);
179 	sn_pdev = (struct pcidev_info *)sn_irq_info->irq_pciioinfo;
180 	pdev = sn_pdev->pdi_linux_pcidev;
181 	provider = SN_PCIDEV_BUSPROVIDER(pdev);
182 
183 	bus_addr = (u64)(msg.address_hi) << 32 | (u64)(msg.address_lo);
184 	(*provider->dma_unmap)(pdev, bus_addr, PCI_DMA_FROMDEVICE);
185 	sn_msi_info[irq].pci_addr = 0;
186 
187 	nasid = cpuid_to_nasid(cpu);
188 	slice = cpuid_to_slice(cpu);
189 
190 	new_irq_info = sn_retarget_vector(sn_irq_info, nasid, slice);
191 	sn_msi_info[irq].sn_irq_info = new_irq_info;
192 	if (new_irq_info == NULL)
193 		return -1;
194 
195 	/*
196 	 * Map the xio address into bus space
197 	 */
198 
199 	bus_addr = (*provider->dma_map_consistent)(pdev,
200 					new_irq_info->irq_xtalkaddr,
201 					sizeof(new_irq_info->irq_xtalkaddr),
202 					SN_DMA_MSI|SN_DMA_ADDR_XIO);
203 
204 	sn_msi_info[irq].pci_addr = bus_addr;
205 	msg.address_hi = (u32)(bus_addr >> 32);
206 	msg.address_lo = (u32)(bus_addr & 0x00000000ffffffff);
207 
208 	pci_write_msi_msg(irq, &msg);
209 	cpumask_copy(irq_data_get_affinity_mask(data), cpu_mask);
210 
211 	return 0;
212 }
213 #endif /* CONFIG_SMP */
214 
sn_ack_msi_irq(struct irq_data * data)215 static void sn_ack_msi_irq(struct irq_data *data)
216 {
217 	irq_move_irq(data);
218 	ia64_eoi();
219 }
220 
sn_msi_retrigger_irq(struct irq_data * data)221 static int sn_msi_retrigger_irq(struct irq_data *data)
222 {
223 	unsigned int vector = data->irq;
224 	ia64_resend_irq(vector);
225 
226 	return 1;
227 }
228 
229 static struct irq_chip sn_msi_chip = {
230 	.name			= "PCI-MSI",
231 	.irq_mask		= pci_msi_mask_irq,
232 	.irq_unmask		= pci_msi_unmask_irq,
233 	.irq_ack		= sn_ack_msi_irq,
234 #ifdef CONFIG_SMP
235 	.irq_set_affinity	= sn_set_msi_irq_affinity,
236 #endif
237 	.irq_retrigger		= sn_msi_retrigger_irq,
238 };
239