blob: 1ef3ed607f47ef5fa68beee0de0dc0a8555ceffd [file] [log] [blame]
Jayachandran Cc24a8a72013-12-21 16:52:13 +05301/*
2 * Copyright (c) 2003-2012 Broadcom Corporation
3 * All Rights Reserved
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the Broadcom
9 * license below:
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
21 *
22 * THIS SOFTWARE IS PROVIDED BY BROADCOM ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
24 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
31 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
32 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35#include <linux/types.h>
36#include <linux/pci.h>
37#include <linux/kernel.h>
38#include <linux/init.h>
39#include <linux/msi.h>
40#include <linux/mm.h>
41#include <linux/irq.h>
42#include <linux/irqdesc.h>
43#include <linux/console.h>
44
45#include <asm/io.h>
46
47#include <asm/netlogic/interrupt.h>
48#include <asm/netlogic/haldefs.h>
49#include <asm/netlogic/common.h>
50#include <asm/netlogic/mips-extns.h>
51
52#include <asm/netlogic/xlp-hal/iomap.h>
53#include <asm/netlogic/xlp-hal/xlp.h>
54#include <asm/netlogic/xlp-hal/pic.h>
55#include <asm/netlogic/xlp-hal/pcibus.h>
56#include <asm/netlogic/xlp-hal/bridge.h>
57
58#define XLP_MSIVEC_PER_LINK 32
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +053059#define XLP_MSIXVEC_TOTAL (cpu_is_xlp9xx() ? 128 : 32)
60#define XLP_MSIXVEC_PER_LINK (cpu_is_xlp9xx() ? 32 : 8)
Jayachandran Cc24a8a72013-12-21 16:52:13 +053061
62/* 128 MSI irqs per node, mapped starting at NLM_MSI_VEC_BASE */
63static inline int nlm_link_msiirq(int link, int msivec)
64{
65 return NLM_MSI_VEC_BASE + link * XLP_MSIVEC_PER_LINK + msivec;
66}
67
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +053068/* get the link MSI vector from irq number */
Jayachandran Cc24a8a72013-12-21 16:52:13 +053069static inline int nlm_irq_msivec(int irq)
70{
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +053071 return (irq - NLM_MSI_VEC_BASE) % XLP_MSIVEC_PER_LINK;
Jayachandran Cc24a8a72013-12-21 16:52:13 +053072}
73
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +053074/* get the link from the irq number */
Jayachandran Cc24a8a72013-12-21 16:52:13 +053075static inline int nlm_irq_msilink(int irq)
76{
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +053077 int total_msivec = XLP_MSIVEC_PER_LINK * PCIE_NLINKS;
78
79 return ((irq - NLM_MSI_VEC_BASE) % total_msivec) /
80 XLP_MSIVEC_PER_LINK;
Jayachandran Cc24a8a72013-12-21 16:52:13 +053081}
82
83/*
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +053084 * For XLP 8xx/4xx/3xx/2xx, only 32 MSI-X vectors are possible because
85 * there are only 32 PIC interrupts for MSI. We split them statically
86 * and use 8 MSI-X vectors per link - this keeps the allocation and
87 * lookup simple.
88 * On XLP 9xx, there are 32 vectors per link, and the interrupts are
89 * not routed thru PIC, so we can use all 128 MSI-X vectors.
Jayachandran Cc24a8a72013-12-21 16:52:13 +053090 */
91static inline int nlm_link_msixirq(int link, int bit)
92{
93 return NLM_MSIX_VEC_BASE + link * XLP_MSIXVEC_PER_LINK + bit;
94}
95
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +053096/* get the link MSI vector from irq number */
Jayachandran Cc24a8a72013-12-21 16:52:13 +053097static inline int nlm_irq_msixvec(int irq)
98{
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +053099 return (irq - NLM_MSIX_VEC_BASE) % XLP_MSIXVEC_TOTAL;
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530100}
101
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530102/* get the link from MSIX vec */
103static inline int nlm_irq_msixlink(int msixvec)
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530104{
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530105 return msixvec / XLP_MSIXVEC_PER_LINK;
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530106}
107
108/*
109 * Per link MSI and MSI-X information, set as IRQ handler data for
110 * MSI and MSI-X interrupts.
111 */
112struct xlp_msi_data {
113 struct nlm_soc_info *node;
114 uint64_t lnkbase;
115 uint32_t msi_enabled_mask;
116 uint32_t msi_alloc_mask;
117 uint32_t msix_alloc_mask;
118 spinlock_t msi_lock;
119};
120
121/*
122 * MSI Chip definitions
123 *
124 * On XLP, there is a PIC interrupt associated with each PCIe link on the
125 * chip (which appears as a PCI bridge to us). This gives us 32 MSI irqa
126 * per link and 128 overall.
127 *
128 * When a device connected to the link raises a MSI interrupt, we get a
129 * link interrupt and we then have to look at PCIE_MSI_STATUS register at
130 * the bridge to map it to the IRQ
131 */
132static void xlp_msi_enable(struct irq_data *d)
133{
134 struct xlp_msi_data *md = irq_data_get_irq_handler_data(d);
135 unsigned long flags;
136 int vec;
137
138 vec = nlm_irq_msivec(d->irq);
139 spin_lock_irqsave(&md->msi_lock, flags);
140 md->msi_enabled_mask |= 1u << vec;
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530141 if (cpu_is_xlp9xx())
142 nlm_write_reg(md->lnkbase, PCIE_9XX_MSI_EN,
143 md->msi_enabled_mask);
144 else
145 nlm_write_reg(md->lnkbase, PCIE_MSI_EN, md->msi_enabled_mask);
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530146 spin_unlock_irqrestore(&md->msi_lock, flags);
147}
148
149static void xlp_msi_disable(struct irq_data *d)
150{
151 struct xlp_msi_data *md = irq_data_get_irq_handler_data(d);
152 unsigned long flags;
153 int vec;
154
155 vec = nlm_irq_msivec(d->irq);
156 spin_lock_irqsave(&md->msi_lock, flags);
157 md->msi_enabled_mask &= ~(1u << vec);
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530158 if (cpu_is_xlp9xx())
159 nlm_write_reg(md->lnkbase, PCIE_9XX_MSI_EN,
160 md->msi_enabled_mask);
161 else
162 nlm_write_reg(md->lnkbase, PCIE_MSI_EN, md->msi_enabled_mask);
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530163 spin_unlock_irqrestore(&md->msi_lock, flags);
164}
165
166static void xlp_msi_mask_ack(struct irq_data *d)
167{
168 struct xlp_msi_data *md = irq_data_get_irq_handler_data(d);
169 int link, vec;
170
171 link = nlm_irq_msilink(d->irq);
172 vec = nlm_irq_msivec(d->irq);
173 xlp_msi_disable(d);
174
175 /* Ack MSI on bridge */
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530176 if (cpu_is_xlp9xx())
177 nlm_write_reg(md->lnkbase, PCIE_9XX_MSI_STATUS, 1u << vec);
178 else
179 nlm_write_reg(md->lnkbase, PCIE_MSI_STATUS, 1u << vec);
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530180
181 /* Ack at eirr and PIC */
182 ack_c0_eirr(PIC_PCIE_LINK_MSI_IRQ(link));
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530183 if (cpu_is_xlp9xx())
184 nlm_pic_ack(md->node->picbase,
185 PIC_9XX_IRT_PCIE_LINK_INDEX(link));
186 else
187 nlm_pic_ack(md->node->picbase, PIC_IRT_PCIE_LINK_INDEX(link));
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530188}
189
190static struct irq_chip xlp_msi_chip = {
191 .name = "XLP-MSI",
192 .irq_enable = xlp_msi_enable,
193 .irq_disable = xlp_msi_disable,
194 .irq_mask_ack = xlp_msi_mask_ack,
195 .irq_unmask = xlp_msi_enable,
196};
197
198/*
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530199 * XLP8XX/4XX/3XX/2XX:
200 * The MSI-X interrupt handling is different from MSI, there are 32 MSI-X
201 * interrupts generated by the PIC and each of these correspond to a MSI-X
202 * vector (0-31) that can be assigned.
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530203 *
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530204 * We divide the MSI-X vectors to 8 per link and do a per-link allocation
205 *
206 * XLP9XX:
207 * 32 MSI-X vectors are available per link, and the interrupts are not routed
208 * thru the PIC. PIC ack not needed.
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530209 *
210 * Enable and disable done using standard MSI functions.
211 */
212static void xlp_msix_mask_ack(struct irq_data *d)
213{
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530214 struct xlp_msi_data *md;
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530215 int link, msixvec;
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530216 uint32_t status_reg, bit;
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530217
218 msixvec = nlm_irq_msixvec(d->irq);
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530219 link = nlm_irq_msixlink(msixvec);
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530220 mask_msi_irq(d);
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530221 md = irq_data_get_irq_handler_data(d);
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530222
223 /* Ack MSI on bridge */
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530224 if (cpu_is_xlp9xx()) {
225 status_reg = PCIE_9XX_MSIX_STATUSX(link);
226 bit = msixvec % XLP_MSIXVEC_PER_LINK;
227 } else {
228 status_reg = PCIE_MSIX_STATUS;
229 bit = msixvec;
230 }
231 nlm_write_reg(md->lnkbase, status_reg, 1u << bit);
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530232
233 /* Ack at eirr and PIC */
234 ack_c0_eirr(PIC_PCIE_MSIX_IRQ(link));
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530235 if (!cpu_is_xlp9xx())
236 nlm_pic_ack(md->node->picbase,
237 PIC_IRT_PCIE_MSIX_INDEX(msixvec));
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530238}
239
240static struct irq_chip xlp_msix_chip = {
241 .name = "XLP-MSIX",
242 .irq_enable = unmask_msi_irq,
243 .irq_disable = mask_msi_irq,
244 .irq_mask_ack = xlp_msix_mask_ack,
245 .irq_unmask = unmask_msi_irq,
246};
247
248void destroy_irq(unsigned int irq)
249{
250 /* nothing to do yet */
251}
252
253void arch_teardown_msi_irq(unsigned int irq)
254{
255 destroy_irq(irq);
256}
257
258/*
259 * Setup a PCIe link for MSI. By default, the links are in
260 * legacy interrupt mode. We will switch them to MSI mode
261 * at the first MSI request.
262 */
263static void xlp_config_link_msi(uint64_t lnkbase, int lirq, uint64_t msiaddr)
264{
265 u32 val;
266
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530267 if (cpu_is_xlp9xx()) {
268 val = nlm_read_reg(lnkbase, PCIE_9XX_INT_EN0);
269 if ((val & 0x200) == 0) {
270 val |= 0x200; /* MSI Interrupt enable */
271 nlm_write_reg(lnkbase, PCIE_9XX_INT_EN0, val);
272 }
273 } else {
274 val = nlm_read_reg(lnkbase, PCIE_INT_EN0);
275 if ((val & 0x200) == 0) {
276 val |= 0x200;
277 nlm_write_reg(lnkbase, PCIE_INT_EN0, val);
278 }
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530279 }
280
281 val = nlm_read_reg(lnkbase, 0x1); /* CMD */
282 if ((val & 0x0400) == 0) {
283 val |= 0x0400;
284 nlm_write_reg(lnkbase, 0x1, val);
285 }
286
287 /* Update IRQ in the PCI irq reg */
288 val = nlm_read_pci_reg(lnkbase, 0xf);
289 val &= ~0x1fu;
290 val |= (1 << 8) | lirq;
291 nlm_write_pci_reg(lnkbase, 0xf, val);
292
293 /* MSI addr */
294 nlm_write_reg(lnkbase, PCIE_BRIDGE_MSI_ADDRH, msiaddr >> 32);
295 nlm_write_reg(lnkbase, PCIE_BRIDGE_MSI_ADDRL, msiaddr & 0xffffffff);
296
297 /* MSI cap for bridge */
298 val = nlm_read_reg(lnkbase, PCIE_BRIDGE_MSI_CAP);
299 if ((val & (1 << 16)) == 0) {
300 val |= 0xb << 16; /* mmc32, msi enable */
301 nlm_write_reg(lnkbase, PCIE_BRIDGE_MSI_CAP, val);
302 }
303}
304
305/*
306 * Allocate a MSI vector on a link
307 */
308static int xlp_setup_msi(uint64_t lnkbase, int node, int link,
309 struct msi_desc *desc)
310{
311 struct xlp_msi_data *md;
312 struct msi_msg msg;
313 unsigned long flags;
314 int msivec, irt, lirq, xirq, ret;
315 uint64_t msiaddr;
316
317 /* Get MSI data for the link */
318 lirq = PIC_PCIE_LINK_MSI_IRQ(link);
319 xirq = nlm_irq_to_xirq(node, nlm_link_msiirq(link, 0));
320 md = irq_get_handler_data(xirq);
321 msiaddr = MSI_LINK_ADDR(node, link);
322
323 spin_lock_irqsave(&md->msi_lock, flags);
324 if (md->msi_alloc_mask == 0) {
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530325 xlp_config_link_msi(lnkbase, lirq, msiaddr);
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530326 /* switch the link IRQ to MSI range */
327 if (cpu_is_xlp9xx())
328 irt = PIC_9XX_IRT_PCIE_LINK_INDEX(link);
329 else
330 irt = PIC_IRT_PCIE_LINK_INDEX(link);
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530331 nlm_setup_pic_irq(node, lirq, lirq, irt);
332 nlm_pic_init_irt(nlm_get_node(node)->picbase, irt, lirq,
Jayachandran C98d48842013-12-21 16:52:26 +0530333 node * nlm_threads_per_node(), 1 /*en */);
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530334 }
335
336 /* allocate a MSI vec, and tell the bridge about it */
337 msivec = fls(md->msi_alloc_mask);
338 if (msivec == XLP_MSIVEC_PER_LINK) {
339 spin_unlock_irqrestore(&md->msi_lock, flags);
340 return -ENOMEM;
341 }
342 md->msi_alloc_mask |= (1u << msivec);
343 spin_unlock_irqrestore(&md->msi_lock, flags);
344
345 msg.address_hi = msiaddr >> 32;
346 msg.address_lo = msiaddr & 0xffffffff;
347 msg.data = 0xc00 | msivec;
348
349 xirq = xirq + msivec; /* msi mapped to global irq space */
350 ret = irq_set_msi_desc(xirq, desc);
351 if (ret < 0) {
352 destroy_irq(xirq);
353 return ret;
354 }
355
356 write_msi_msg(xirq, &msg);
357 return 0;
358}
359
360/*
361 * Switch a link to MSI-X mode
362 */
363static void xlp_config_link_msix(uint64_t lnkbase, int lirq, uint64_t msixaddr)
364{
365 u32 val;
366
367 val = nlm_read_reg(lnkbase, 0x2C);
368 if ((val & 0x80000000U) == 0) {
369 val |= 0x80000000U;
370 nlm_write_reg(lnkbase, 0x2C, val);
371 }
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530372
373 if (cpu_is_xlp9xx()) {
374 val = nlm_read_reg(lnkbase, PCIE_9XX_INT_EN0);
375 if ((val & 0x200) == 0) {
376 val |= 0x200; /* MSI Interrupt enable */
377 nlm_write_reg(lnkbase, PCIE_9XX_INT_EN0, val);
378 }
379 } else {
380 val = nlm_read_reg(lnkbase, PCIE_INT_EN0);
381 if ((val & 0x200) == 0) {
382 val |= 0x200; /* MSI Interrupt enable */
383 nlm_write_reg(lnkbase, PCIE_INT_EN0, val);
384 }
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530385 }
386
387 val = nlm_read_reg(lnkbase, 0x1); /* CMD */
388 if ((val & 0x0400) == 0) {
389 val |= 0x0400;
390 nlm_write_reg(lnkbase, 0x1, val);
391 }
392
393 /* Update IRQ in the PCI irq reg */
394 val = nlm_read_pci_reg(lnkbase, 0xf);
395 val &= ~0x1fu;
396 val |= (1 << 8) | lirq;
397 nlm_write_pci_reg(lnkbase, 0xf, val);
398
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530399 if (cpu_is_xlp9xx()) {
400 /* MSI-X addresses */
401 nlm_write_reg(lnkbase, PCIE_9XX_BRIDGE_MSIX_ADDR_BASE,
402 msixaddr >> 8);
403 nlm_write_reg(lnkbase, PCIE_9XX_BRIDGE_MSIX_ADDR_LIMIT,
404 (msixaddr + MSI_ADDR_SZ) >> 8);
405 } else {
406 /* MSI-X addresses */
407 nlm_write_reg(lnkbase, PCIE_BRIDGE_MSIX_ADDR_BASE,
408 msixaddr >> 8);
409 nlm_write_reg(lnkbase, PCIE_BRIDGE_MSIX_ADDR_LIMIT,
410 (msixaddr + MSI_ADDR_SZ) >> 8);
411 }
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530412}
413
414/*
415 * Allocate a MSI-X vector
416 */
417static int xlp_setup_msix(uint64_t lnkbase, int node, int link,
418 struct msi_desc *desc)
419{
420 struct xlp_msi_data *md;
421 struct msi_msg msg;
422 unsigned long flags;
423 int t, msixvec, lirq, xirq, ret;
424 uint64_t msixaddr;
425
426 /* Get MSI data for the link */
427 lirq = PIC_PCIE_MSIX_IRQ(link);
428 xirq = nlm_irq_to_xirq(node, nlm_link_msixirq(link, 0));
429 md = irq_get_handler_data(xirq);
430 msixaddr = MSIX_LINK_ADDR(node, link);
431
432 spin_lock_irqsave(&md->msi_lock, flags);
433 /* switch the PCIe link to MSI-X mode at the first alloc */
434 if (md->msix_alloc_mask == 0)
435 xlp_config_link_msix(lnkbase, lirq, msixaddr);
436
437 /* allocate a MSI-X vec, and tell the bridge about it */
438 t = fls(md->msix_alloc_mask);
439 if (t == XLP_MSIXVEC_PER_LINK) {
440 spin_unlock_irqrestore(&md->msi_lock, flags);
441 return -ENOMEM;
442 }
443 md->msix_alloc_mask |= (1u << t);
444 spin_unlock_irqrestore(&md->msi_lock, flags);
445
446 xirq += t;
447 msixvec = nlm_irq_msixvec(xirq);
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530448
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530449 msg.address_hi = msixaddr >> 32;
450 msg.address_lo = msixaddr & 0xffffffff;
451 msg.data = 0xc00 | msixvec;
452
453 ret = irq_set_msi_desc(xirq, desc);
454 if (ret < 0) {
455 destroy_irq(xirq);
456 return ret;
457 }
458
459 write_msi_msg(xirq, &msg);
460 return 0;
461}
462
463int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
464{
465 struct pci_dev *lnkdev;
466 uint64_t lnkbase;
467 int node, link, slot;
468
469 lnkdev = xlp_get_pcie_link(dev);
470 if (lnkdev == NULL) {
471 dev_err(&dev->dev, "Could not find bridge\n");
472 return 1;
473 }
474 slot = PCI_SLOT(lnkdev->devfn);
475 link = PCI_FUNC(lnkdev->devfn);
476 node = slot / 8;
477 lnkbase = nlm_get_pcie_base(node, link);
478
479 if (desc->msi_attrib.is_msix)
480 return xlp_setup_msix(lnkbase, node, link, desc);
481 else
482 return xlp_setup_msi(lnkbase, node, link, desc);
483}
484
485void __init xlp_init_node_msi_irqs(int node, int link)
486{
487 struct nlm_soc_info *nodep;
488 struct xlp_msi_data *md;
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530489 int irq, i, irt, msixvec, val;
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530490
491 pr_info("[%d %d] Init node PCI IRT\n", node, link);
492 nodep = nlm_get_node(node);
493
494 /* Alloc an MSI block for the link */
495 md = kzalloc(sizeof(*md), GFP_KERNEL);
496 spin_lock_init(&md->msi_lock);
497 md->msi_enabled_mask = 0;
498 md->msi_alloc_mask = 0;
499 md->msix_alloc_mask = 0;
500 md->node = nodep;
501 md->lnkbase = nlm_get_pcie_base(node, link);
502
503 /* extended space for MSI interrupts */
504 irq = nlm_irq_to_xirq(node, nlm_link_msiirq(link, 0));
505 for (i = irq; i < irq + XLP_MSIVEC_PER_LINK; i++) {
506 irq_set_chip_and_handler(i, &xlp_msi_chip, handle_level_irq);
507 irq_set_handler_data(i, md);
508 }
509
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530510 for (i = 0; i < XLP_MSIXVEC_PER_LINK ; i++) {
511 if (cpu_is_xlp9xx()) {
512 val = ((node * nlm_threads_per_node()) << 7 |
513 PIC_PCIE_MSIX_IRQ(link) << 1 | 0 << 0);
514 nlm_write_pcie_reg(md->lnkbase, PCIE_9XX_MSIX_VECX(i +
515 (link * XLP_MSIXVEC_PER_LINK)), val);
516 } else {
517 /* Initialize MSI-X irts to generate one interrupt
518 * per link
519 */
520 msixvec = link * XLP_MSIXVEC_PER_LINK + i;
521 irt = PIC_IRT_PCIE_MSIX_INDEX(msixvec);
522 nlm_pic_init_irt(nodep->picbase, irt,
523 PIC_PCIE_MSIX_IRQ(link),
524 node * nlm_threads_per_node(), 1);
525 }
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530526
527 /* Initialize MSI-X extended irq space for the link */
528 irq = nlm_irq_to_xirq(node, nlm_link_msixirq(link, i));
529 irq_set_chip_and_handler(irq, &xlp_msix_chip, handle_level_irq);
530 irq_set_handler_data(irq, md);
531 }
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530532}
533
534void nlm_dispatch_msi(int node, int lirq)
535{
536 struct xlp_msi_data *md;
537 int link, i, irqbase;
538 u32 status;
539
540 link = lirq - PIC_PCIE_LINK_MSI_IRQ_BASE;
541 irqbase = nlm_irq_to_xirq(node, nlm_link_msiirq(link, 0));
542 md = irq_get_handler_data(irqbase);
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530543 if (cpu_is_xlp9xx())
544 status = nlm_read_reg(md->lnkbase, PCIE_9XX_MSI_STATUS) &
545 md->msi_enabled_mask;
546 else
547 status = nlm_read_reg(md->lnkbase, PCIE_MSI_STATUS) &
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530548 md->msi_enabled_mask;
549 while (status) {
550 i = __ffs(status);
551 do_IRQ(irqbase + i);
552 status &= status - 1;
553 }
554}
555
556void nlm_dispatch_msix(int node, int lirq)
557{
558 struct xlp_msi_data *md;
559 int link, i, irqbase;
560 u32 status;
561
562 link = lirq - PIC_PCIE_MSIX_IRQ_BASE;
563 irqbase = nlm_irq_to_xirq(node, nlm_link_msixirq(link, 0));
564 md = irq_get_handler_data(irqbase);
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530565 if (cpu_is_xlp9xx())
566 status = nlm_read_reg(md->lnkbase, PCIE_9XX_MSIX_STATUSX(link));
567 else
568 status = nlm_read_reg(md->lnkbase, PCIE_MSIX_STATUS);
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530569
570 /* narrow it down to the MSI-x vectors for our link */
Ganesan Ramalingamd66f3f02014-05-09 16:35:49 +0530571 if (!cpu_is_xlp9xx())
572 status = (status >> (link * XLP_MSIXVEC_PER_LINK)) &
Jayachandran Cc24a8a72013-12-21 16:52:13 +0530573 ((1 << XLP_MSIXVEC_PER_LINK) - 1);
574
575 while (status) {
576 i = __ffs(status);
577 do_IRQ(irqbase + i);
578 status &= status - 1;
579 }
580}