msi-xlp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*
  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. #include <linux/types.h>
  35. #include <linux/pci.h>
  36. #include <linux/kernel.h>
  37. #include <linux/init.h>
  38. #include <linux/msi.h>
  39. #include <linux/mm.h>
  40. #include <linux/irq.h>
  41. #include <linux/irqdesc.h>
  42. #include <linux/console.h>
  43. #include <asm/io.h>
  44. #include <asm/netlogic/interrupt.h>
  45. #include <asm/netlogic/haldefs.h>
  46. #include <asm/netlogic/common.h>
  47. #include <asm/netlogic/mips-extns.h>
  48. #include <asm/netlogic/xlp-hal/iomap.h>
  49. #include <asm/netlogic/xlp-hal/xlp.h>
  50. #include <asm/netlogic/xlp-hal/pic.h>
  51. #include <asm/netlogic/xlp-hal/pcibus.h>
  52. #include <asm/netlogic/xlp-hal/bridge.h>
  53. #define XLP_MSIVEC_PER_LINK 32
  54. #define XLP_MSIXVEC_TOTAL 32
  55. #define XLP_MSIXVEC_PER_LINK 8
  56. /* 128 MSI irqs per node, mapped starting at NLM_MSI_VEC_BASE */
  57. static inline int nlm_link_msiirq(int link, int msivec)
  58. {
  59. return NLM_MSI_VEC_BASE + link * XLP_MSIVEC_PER_LINK + msivec;
  60. }
  61. static inline int nlm_irq_msivec(int irq)
  62. {
  63. return irq % XLP_MSIVEC_PER_LINK;
  64. }
  65. static inline int nlm_irq_msilink(int irq)
  66. {
  67. return (irq % (XLP_MSIVEC_PER_LINK * PCIE_NLINKS)) /
  68. XLP_MSIVEC_PER_LINK;
  69. }
  70. /*
  71. * Only 32 MSI-X vectors are possible because there are only 32 PIC
  72. * interrupts for MSI. We split them statically and use 8 MSI-X vectors
  73. * per link - this keeps the allocation and lookup simple.
  74. */
  75. static inline int nlm_link_msixirq(int link, int bit)
  76. {
  77. return NLM_MSIX_VEC_BASE + link * XLP_MSIXVEC_PER_LINK + bit;
  78. }
  79. static inline int nlm_irq_msixvec(int irq)
  80. {
  81. return irq % XLP_MSIXVEC_TOTAL; /* works when given xirq */
  82. }
  83. static inline int nlm_irq_msixlink(int irq)
  84. {
  85. return nlm_irq_msixvec(irq) / XLP_MSIXVEC_PER_LINK;
  86. }
  87. /*
  88. * Per link MSI and MSI-X information, set as IRQ handler data for
  89. * MSI and MSI-X interrupts.
  90. */
  91. struct xlp_msi_data {
  92. struct nlm_soc_info *node;
  93. uint64_t lnkbase;
  94. uint32_t msi_enabled_mask;
  95. uint32_t msi_alloc_mask;
  96. uint32_t msix_alloc_mask;
  97. spinlock_t msi_lock;
  98. };
  99. /*
  100. * MSI Chip definitions
  101. *
  102. * On XLP, there is a PIC interrupt associated with each PCIe link on the
  103. * chip (which appears as a PCI bridge to us). This gives us 32 MSI irqa
  104. * per link and 128 overall.
  105. *
  106. * When a device connected to the link raises a MSI interrupt, we get a
  107. * link interrupt and we then have to look at PCIE_MSI_STATUS register at
  108. * the bridge to map it to the IRQ
  109. */
  110. static void xlp_msi_enable(struct irq_data *d)
  111. {
  112. struct xlp_msi_data *md = irq_data_get_irq_handler_data(d);
  113. unsigned long flags;
  114. int vec;
  115. vec = nlm_irq_msivec(d->irq);
  116. spin_lock_irqsave(&md->msi_lock, flags);
  117. md->msi_enabled_mask |= 1u << vec;
  118. nlm_write_reg(md->lnkbase, PCIE_MSI_EN, md->msi_enabled_mask);
  119. spin_unlock_irqrestore(&md->msi_lock, flags);
  120. }
  121. static void xlp_msi_disable(struct irq_data *d)
  122. {
  123. struct xlp_msi_data *md = irq_data_get_irq_handler_data(d);
  124. unsigned long flags;
  125. int vec;
  126. vec = nlm_irq_msivec(d->irq);
  127. spin_lock_irqsave(&md->msi_lock, flags);
  128. md->msi_enabled_mask &= ~(1u << vec);
  129. nlm_write_reg(md->lnkbase, PCIE_MSI_EN, md->msi_enabled_mask);
  130. spin_unlock_irqrestore(&md->msi_lock, flags);
  131. }
  132. static void xlp_msi_mask_ack(struct irq_data *d)
  133. {
  134. struct xlp_msi_data *md = irq_data_get_irq_handler_data(d);
  135. int link, vec;
  136. link = nlm_irq_msilink(d->irq);
  137. vec = nlm_irq_msivec(d->irq);
  138. xlp_msi_disable(d);
  139. /* Ack MSI on bridge */
  140. nlm_write_reg(md->lnkbase, PCIE_MSI_STATUS, 1u << vec);
  141. /* Ack at eirr and PIC */
  142. ack_c0_eirr(PIC_PCIE_LINK_MSI_IRQ(link));
  143. nlm_pic_ack(md->node->picbase, PIC_IRT_PCIE_LINK_INDEX(link));
  144. }
  145. static struct irq_chip xlp_msi_chip = {
  146. .name = "XLP-MSI",
  147. .irq_enable = xlp_msi_enable,
  148. .irq_disable = xlp_msi_disable,
  149. .irq_mask_ack = xlp_msi_mask_ack,
  150. .irq_unmask = xlp_msi_enable,
  151. };
  152. /*
  153. * The MSI-X interrupt handling is different from MSI, there are 32
  154. * MSI-X interrupts generated by the PIC and each of these correspond
  155. * to a MSI-X vector (0-31) that can be assigned.
  156. *
  157. * We divide the MSI-X vectors to 8 per link and do a per-link
  158. * allocation
  159. *
  160. * Enable and disable done using standard MSI functions.
  161. */
  162. static void xlp_msix_mask_ack(struct irq_data *d)
  163. {
  164. struct xlp_msi_data *md = irq_data_get_irq_handler_data(d);
  165. int link, msixvec;
  166. msixvec = nlm_irq_msixvec(d->irq);
  167. link = nlm_irq_msixlink(d->irq);
  168. mask_msi_irq(d);
  169. /* Ack MSI on bridge */
  170. nlm_write_reg(md->lnkbase, PCIE_MSIX_STATUS, 1u << msixvec);
  171. /* Ack at eirr and PIC */
  172. ack_c0_eirr(PIC_PCIE_MSIX_IRQ(link));
  173. nlm_pic_ack(md->node->picbase, PIC_IRT_PCIE_MSIX_INDEX(msixvec));
  174. }
  175. static struct irq_chip xlp_msix_chip = {
  176. .name = "XLP-MSIX",
  177. .irq_enable = unmask_msi_irq,
  178. .irq_disable = mask_msi_irq,
  179. .irq_mask_ack = xlp_msix_mask_ack,
  180. .irq_unmask = unmask_msi_irq,
  181. };
  182. void destroy_irq(unsigned int irq)
  183. {
  184. /* nothing to do yet */
  185. }
  186. void arch_teardown_msi_irq(unsigned int irq)
  187. {
  188. destroy_irq(irq);
  189. }
  190. /*
  191. * Setup a PCIe link for MSI. By default, the links are in
  192. * legacy interrupt mode. We will switch them to MSI mode
  193. * at the first MSI request.
  194. */
  195. static void xlp_config_link_msi(uint64_t lnkbase, int lirq, uint64_t msiaddr)
  196. {
  197. u32 val;
  198. val = nlm_read_reg(lnkbase, PCIE_INT_EN0);
  199. if ((val & 0x200) == 0) {
  200. val |= 0x200; /* MSI Interrupt enable */
  201. nlm_write_reg(lnkbase, PCIE_INT_EN0, val);
  202. }
  203. val = nlm_read_reg(lnkbase, 0x1); /* CMD */
  204. if ((val & 0x0400) == 0) {
  205. val |= 0x0400;
  206. nlm_write_reg(lnkbase, 0x1, val);
  207. }
  208. /* Update IRQ in the PCI irq reg */
  209. val = nlm_read_pci_reg(lnkbase, 0xf);
  210. val &= ~0x1fu;
  211. val |= (1 << 8) | lirq;
  212. nlm_write_pci_reg(lnkbase, 0xf, val);
  213. /* MSI addr */
  214. nlm_write_reg(lnkbase, PCIE_BRIDGE_MSI_ADDRH, msiaddr >> 32);
  215. nlm_write_reg(lnkbase, PCIE_BRIDGE_MSI_ADDRL, msiaddr & 0xffffffff);
  216. /* MSI cap for bridge */
  217. val = nlm_read_reg(lnkbase, PCIE_BRIDGE_MSI_CAP);
  218. if ((val & (1 << 16)) == 0) {
  219. val |= 0xb << 16; /* mmc32, msi enable */
  220. nlm_write_reg(lnkbase, PCIE_BRIDGE_MSI_CAP, val);
  221. }
  222. }
  223. /*
  224. * Allocate a MSI vector on a link
  225. */
  226. static int xlp_setup_msi(uint64_t lnkbase, int node, int link,
  227. struct msi_desc *desc)
  228. {
  229. struct xlp_msi_data *md;
  230. struct msi_msg msg;
  231. unsigned long flags;
  232. int msivec, irt, lirq, xirq, ret;
  233. uint64_t msiaddr;
  234. /* Get MSI data for the link */
  235. lirq = PIC_PCIE_LINK_MSI_IRQ(link);
  236. xirq = nlm_irq_to_xirq(node, nlm_link_msiirq(link, 0));
  237. md = irq_get_handler_data(xirq);
  238. msiaddr = MSI_LINK_ADDR(node, link);
  239. spin_lock_irqsave(&md->msi_lock, flags);
  240. if (md->msi_alloc_mask == 0) {
  241. /* switch the link IRQ to MSI range */
  242. xlp_config_link_msi(lnkbase, lirq, msiaddr);
  243. irt = PIC_IRT_PCIE_LINK_INDEX(link);
  244. nlm_setup_pic_irq(node, lirq, lirq, irt);
  245. nlm_pic_init_irt(nlm_get_node(node)->picbase, irt, lirq,
  246. node * nlm_threads_per_node(), 1 /*en */);
  247. }
  248. /* allocate a MSI vec, and tell the bridge about it */
  249. msivec = fls(md->msi_alloc_mask);
  250. if (msivec == XLP_MSIVEC_PER_LINK) {
  251. spin_unlock_irqrestore(&md->msi_lock, flags);
  252. return -ENOMEM;
  253. }
  254. md->msi_alloc_mask |= (1u << msivec);
  255. spin_unlock_irqrestore(&md->msi_lock, flags);
  256. msg.address_hi = msiaddr >> 32;
  257. msg.address_lo = msiaddr & 0xffffffff;
  258. msg.data = 0xc00 | msivec;
  259. xirq = xirq + msivec; /* msi mapped to global irq space */
  260. ret = irq_set_msi_desc(xirq, desc);
  261. if (ret < 0) {
  262. destroy_irq(xirq);
  263. return ret;
  264. }
  265. write_msi_msg(xirq, &msg);
  266. return 0;
  267. }
  268. /*
  269. * Switch a link to MSI-X mode
  270. */
  271. static void xlp_config_link_msix(uint64_t lnkbase, int lirq, uint64_t msixaddr)
  272. {
  273. u32 val;
  274. val = nlm_read_reg(lnkbase, 0x2C);
  275. if ((val & 0x80000000U) == 0) {
  276. val |= 0x80000000U;
  277. nlm_write_reg(lnkbase, 0x2C, val);
  278. }
  279. val = nlm_read_reg(lnkbase, PCIE_INT_EN0);
  280. if ((val & 0x200) == 0) {
  281. val |= 0x200; /* MSI Interrupt enable */
  282. nlm_write_reg(lnkbase, PCIE_INT_EN0, val);
  283. }
  284. val = nlm_read_reg(lnkbase, 0x1); /* CMD */
  285. if ((val & 0x0400) == 0) {
  286. val |= 0x0400;
  287. nlm_write_reg(lnkbase, 0x1, val);
  288. }
  289. /* Update IRQ in the PCI irq reg */
  290. val = nlm_read_pci_reg(lnkbase, 0xf);
  291. val &= ~0x1fu;
  292. val |= (1 << 8) | lirq;
  293. nlm_write_pci_reg(lnkbase, 0xf, val);
  294. /* MSI-X addresses */
  295. nlm_write_reg(lnkbase, PCIE_BRIDGE_MSIX_ADDR_BASE, msixaddr >> 8);
  296. nlm_write_reg(lnkbase, PCIE_BRIDGE_MSIX_ADDR_LIMIT,
  297. (msixaddr + MSI_ADDR_SZ) >> 8);
  298. }
  299. /*
  300. * Allocate a MSI-X vector
  301. */
  302. static int xlp_setup_msix(uint64_t lnkbase, int node, int link,
  303. struct msi_desc *desc)
  304. {
  305. struct xlp_msi_data *md;
  306. struct msi_msg msg;
  307. unsigned long flags;
  308. int t, msixvec, lirq, xirq, ret;
  309. uint64_t msixaddr;
  310. /* Get MSI data for the link */
  311. lirq = PIC_PCIE_MSIX_IRQ(link);
  312. xirq = nlm_irq_to_xirq(node, nlm_link_msixirq(link, 0));
  313. md = irq_get_handler_data(xirq);
  314. msixaddr = MSIX_LINK_ADDR(node, link);
  315. spin_lock_irqsave(&md->msi_lock, flags);
  316. /* switch the PCIe link to MSI-X mode at the first alloc */
  317. if (md->msix_alloc_mask == 0)
  318. xlp_config_link_msix(lnkbase, lirq, msixaddr);
  319. /* allocate a MSI-X vec, and tell the bridge about it */
  320. t = fls(md->msix_alloc_mask);
  321. if (t == XLP_MSIXVEC_PER_LINK) {
  322. spin_unlock_irqrestore(&md->msi_lock, flags);
  323. return -ENOMEM;
  324. }
  325. md->msix_alloc_mask |= (1u << t);
  326. spin_unlock_irqrestore(&md->msi_lock, flags);
  327. xirq += t;
  328. msixvec = nlm_irq_msixvec(xirq);
  329. msg.address_hi = msixaddr >> 32;
  330. msg.address_lo = msixaddr & 0xffffffff;
  331. msg.data = 0xc00 | msixvec;
  332. ret = irq_set_msi_desc(xirq, desc);
  333. if (ret < 0) {
  334. destroy_irq(xirq);
  335. return ret;
  336. }
  337. write_msi_msg(xirq, &msg);
  338. return 0;
  339. }
  340. int arch_setup_msi_irq(struct pci_dev *dev, struct msi_desc *desc)
  341. {
  342. struct pci_dev *lnkdev;
  343. uint64_t lnkbase;
  344. int node, link, slot;
  345. lnkdev = xlp_get_pcie_link(dev);
  346. if (lnkdev == NULL) {
  347. dev_err(&dev->dev, "Could not find bridge\n");
  348. return 1;
  349. }
  350. slot = PCI_SLOT(lnkdev->devfn);
  351. link = PCI_FUNC(lnkdev->devfn);
  352. node = slot / 8;
  353. lnkbase = nlm_get_pcie_base(node, link);
  354. if (desc->msi_attrib.is_msix)
  355. return xlp_setup_msix(lnkbase, node, link, desc);
  356. else
  357. return xlp_setup_msi(lnkbase, node, link, desc);
  358. }
  359. void __init xlp_init_node_msi_irqs(int node, int link)
  360. {
  361. struct nlm_soc_info *nodep;
  362. struct xlp_msi_data *md;
  363. int irq, i, irt, msixvec;
  364. pr_info("[%d %d] Init node PCI IRT\n", node, link);
  365. nodep = nlm_get_node(node);
  366. /* Alloc an MSI block for the link */
  367. md = kzalloc(sizeof(*md), GFP_KERNEL);
  368. spin_lock_init(&md->msi_lock);
  369. md->msi_enabled_mask = 0;
  370. md->msi_alloc_mask = 0;
  371. md->msix_alloc_mask = 0;
  372. md->node = nodep;
  373. md->lnkbase = nlm_get_pcie_base(node, link);
  374. /* extended space for MSI interrupts */
  375. irq = nlm_irq_to_xirq(node, nlm_link_msiirq(link, 0));
  376. for (i = irq; i < irq + XLP_MSIVEC_PER_LINK; i++) {
  377. irq_set_chip_and_handler(i, &xlp_msi_chip, handle_level_irq);
  378. irq_set_handler_data(i, md);
  379. }
  380. for (i = 0; i < XLP_MSIXVEC_PER_LINK; i++) {
  381. /* Initialize MSI-X irts to generate one interrupt per link */
  382. msixvec = link * XLP_MSIXVEC_PER_LINK + i;
  383. irt = PIC_IRT_PCIE_MSIX_INDEX(msixvec);
  384. nlm_pic_init_irt(nodep->picbase, irt, PIC_PCIE_MSIX_IRQ(link),
  385. node * nlm_threads_per_node(), 1 /* enable */);
  386. /* Initialize MSI-X extended irq space for the link */
  387. irq = nlm_irq_to_xirq(node, nlm_link_msixirq(link, i));
  388. irq_set_chip_and_handler(irq, &xlp_msix_chip, handle_level_irq);
  389. irq_set_handler_data(irq, md);
  390. }
  391. }
  392. void nlm_dispatch_msi(int node, int lirq)
  393. {
  394. struct xlp_msi_data *md;
  395. int link, i, irqbase;
  396. u32 status;
  397. link = lirq - PIC_PCIE_LINK_MSI_IRQ_BASE;
  398. irqbase = nlm_irq_to_xirq(node, nlm_link_msiirq(link, 0));
  399. md = irq_get_handler_data(irqbase);
  400. status = nlm_read_reg(md->lnkbase, PCIE_MSI_STATUS) &
  401. md->msi_enabled_mask;
  402. while (status) {
  403. i = __ffs(status);
  404. do_IRQ(irqbase + i);
  405. status &= status - 1;
  406. }
  407. }
  408. void nlm_dispatch_msix(int node, int lirq)
  409. {
  410. struct xlp_msi_data *md;
  411. int link, i, irqbase;
  412. u32 status;
  413. link = lirq - PIC_PCIE_MSIX_IRQ_BASE;
  414. irqbase = nlm_irq_to_xirq(node, nlm_link_msixirq(link, 0));
  415. md = irq_get_handler_data(irqbase);
  416. status = nlm_read_reg(md->lnkbase, PCIE_MSIX_STATUS);
  417. /* narrow it down to the MSI-x vectors for our link */
  418. status = (status >> (link * XLP_MSIXVEC_PER_LINK)) &
  419. ((1 << XLP_MSIXVEC_PER_LINK) - 1);
  420. while (status) {
  421. i = __ffs(status);
  422. do_IRQ(irqbase + i);
  423. status &= status - 1;
  424. }
  425. }