fsl_msi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /*
  2. * Copyright (C) 2007-2011 Freescale Semiconductor, Inc.
  3. *
  4. * Author: Tony Li <tony.li@freescale.com>
  5. * Jason Jin <Jason.jin@freescale.com>
  6. *
  7. * The hwirq alloc and free code reuse from sysdev/mpic_msi.c
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; version 2 of the
  12. * License.
  13. *
  14. */
  15. #include <linux/irq.h>
  16. #include <linux/bootmem.h>
  17. #include <linux/msi.h>
  18. #include <linux/pci.h>
  19. #include <linux/slab.h>
  20. #include <linux/of_platform.h>
  21. #include <sysdev/fsl_soc.h>
  22. #include <asm/prom.h>
  23. #include <asm/hw_irq.h>
  24. #include <asm/ppc-pci.h>
  25. #include <asm/mpic.h>
  26. #include <asm/fsl_hcalls.h>
  27. #include "fsl_msi.h"
  28. #include "fsl_pci.h"
  29. LIST_HEAD(msi_head);
  30. struct fsl_msi_feature {
  31. u32 fsl_pic_ip;
  32. u32 msiir_offset; /* Offset of MSIIR, relative to start of MSIR bank */
  33. };
  34. struct fsl_msi_cascade_data {
  35. struct fsl_msi *msi_data;
  36. int index;
  37. };
  38. static inline u32 fsl_msi_read(u32 __iomem *base, unsigned int reg)
  39. {
  40. return in_be32(base + (reg >> 2));
  41. }
  42. /*
  43. * We do not need this actually. The MSIR register has been read once
  44. * in the cascade interrupt. So, this MSI interrupt has been acked
  45. */
  46. static void fsl_msi_end_irq(struct irq_data *d)
  47. {
  48. }
  49. static struct irq_chip fsl_msi_chip = {
  50. .irq_mask = mask_msi_irq,
  51. .irq_unmask = unmask_msi_irq,
  52. .irq_ack = fsl_msi_end_irq,
  53. .name = "FSL-MSI",
  54. };
  55. static int fsl_msi_host_map(struct irq_domain *h, unsigned int virq,
  56. irq_hw_number_t hw)
  57. {
  58. struct fsl_msi *msi_data = h->host_data;
  59. struct irq_chip *chip = &fsl_msi_chip;
  60. irq_set_status_flags(virq, IRQ_TYPE_EDGE_FALLING);
  61. irq_set_chip_data(virq, msi_data);
  62. irq_set_chip_and_handler(virq, chip, handle_edge_irq);
  63. return 0;
  64. }
  65. static const struct irq_domain_ops fsl_msi_host_ops = {
  66. .map = fsl_msi_host_map,
  67. };
  68. static int fsl_msi_init_allocator(struct fsl_msi *msi_data)
  69. {
  70. int rc;
  71. rc = msi_bitmap_alloc(&msi_data->bitmap, NR_MSI_IRQS,
  72. msi_data->irqhost->of_node);
  73. if (rc)
  74. return rc;
  75. rc = msi_bitmap_reserve_dt_hwirqs(&msi_data->bitmap);
  76. if (rc < 0) {
  77. msi_bitmap_free(&msi_data->bitmap);
  78. return rc;
  79. }
  80. return 0;
  81. }
  82. static int fsl_msi_check_device(struct pci_dev *pdev, int nvec, int type)
  83. {
  84. if (type == PCI_CAP_ID_MSIX)
  85. pr_debug("fslmsi: MSI-X untested, trying anyway.\n");
  86. return 0;
  87. }
  88. static void fsl_teardown_msi_irqs(struct pci_dev *pdev)
  89. {
  90. struct msi_desc *entry;
  91. struct fsl_msi *msi_data;
  92. list_for_each_entry(entry, &pdev->msi_list, list) {
  93. if (entry->irq == NO_IRQ)
  94. continue;
  95. msi_data = irq_get_chip_data(entry->irq);
  96. irq_set_msi_desc(entry->irq, NULL);
  97. msi_bitmap_free_hwirqs(&msi_data->bitmap,
  98. virq_to_hw(entry->irq), 1);
  99. irq_dispose_mapping(entry->irq);
  100. }
  101. return;
  102. }
  103. static void fsl_compose_msi_msg(struct pci_dev *pdev, int hwirq,
  104. struct msi_msg *msg,
  105. struct fsl_msi *fsl_msi_data)
  106. {
  107. struct fsl_msi *msi_data = fsl_msi_data;
  108. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  109. u64 address; /* Physical address of the MSIIR */
  110. int len;
  111. const u64 *reg;
  112. /* If the msi-address-64 property exists, then use it */
  113. reg = of_get_property(hose->dn, "msi-address-64", &len);
  114. if (reg && (len == sizeof(u64)))
  115. address = be64_to_cpup(reg);
  116. else
  117. address = fsl_pci_immrbar_base(hose) + msi_data->msiir_offset;
  118. msg->address_lo = lower_32_bits(address);
  119. msg->address_hi = upper_32_bits(address);
  120. msg->data = hwirq;
  121. pr_debug("%s: allocated srs: %d, ibs: %d\n",
  122. __func__, hwirq / IRQS_PER_MSI_REG, hwirq % IRQS_PER_MSI_REG);
  123. }
  124. static int fsl_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  125. {
  126. struct pci_controller *hose = pci_bus_to_host(pdev->bus);
  127. struct device_node *np;
  128. phandle phandle = 0;
  129. int rc, hwirq = -ENOMEM;
  130. unsigned int virq;
  131. struct msi_desc *entry;
  132. struct msi_msg msg;
  133. struct fsl_msi *msi_data;
  134. /*
  135. * If the PCI node has an fsl,msi property, then we need to use it
  136. * to find the specific MSI.
  137. */
  138. np = of_parse_phandle(hose->dn, "fsl,msi", 0);
  139. if (np) {
  140. if (of_device_is_compatible(np, "fsl,mpic-msi") ||
  141. of_device_is_compatible(np, "fsl,vmpic-msi"))
  142. phandle = np->phandle;
  143. else {
  144. dev_err(&pdev->dev,
  145. "node %s has an invalid fsl,msi phandle %u\n",
  146. hose->dn->full_name, np->phandle);
  147. return -EINVAL;
  148. }
  149. }
  150. list_for_each_entry(entry, &pdev->msi_list, list) {
  151. /*
  152. * Loop over all the MSI devices until we find one that has an
  153. * available interrupt.
  154. */
  155. list_for_each_entry(msi_data, &msi_head, list) {
  156. /*
  157. * If the PCI node has an fsl,msi property, then we
  158. * restrict our search to the corresponding MSI node.
  159. * The simplest way is to skip over MSI nodes with the
  160. * wrong phandle. Under the Freescale hypervisor, this
  161. * has the additional benefit of skipping over MSI
  162. * nodes that are not mapped in the PAMU.
  163. */
  164. if (phandle && (phandle != msi_data->phandle))
  165. continue;
  166. hwirq = msi_bitmap_alloc_hwirqs(&msi_data->bitmap, 1);
  167. if (hwirq >= 0)
  168. break;
  169. }
  170. if (hwirq < 0) {
  171. rc = hwirq;
  172. dev_err(&pdev->dev, "could not allocate MSI interrupt\n");
  173. goto out_free;
  174. }
  175. virq = irq_create_mapping(msi_data->irqhost, hwirq);
  176. if (virq == NO_IRQ) {
  177. dev_err(&pdev->dev, "fail mapping hwirq %i\n", hwirq);
  178. msi_bitmap_free_hwirqs(&msi_data->bitmap, hwirq, 1);
  179. rc = -ENOSPC;
  180. goto out_free;
  181. }
  182. /* chip_data is msi_data via host->hostdata in host->map() */
  183. irq_set_msi_desc(virq, entry);
  184. fsl_compose_msi_msg(pdev, hwirq, &msg, msi_data);
  185. write_msi_msg(virq, &msg);
  186. }
  187. return 0;
  188. out_free:
  189. /* free by the caller of this function */
  190. return rc;
  191. }
  192. static void fsl_msi_cascade(unsigned int irq, struct irq_desc *desc)
  193. {
  194. struct irq_chip *chip = irq_desc_get_chip(desc);
  195. struct irq_data *idata = irq_desc_get_irq_data(desc);
  196. unsigned int cascade_irq;
  197. struct fsl_msi *msi_data;
  198. int msir_index = -1;
  199. u32 msir_value = 0;
  200. u32 intr_index;
  201. u32 have_shift = 0;
  202. struct fsl_msi_cascade_data *cascade_data;
  203. cascade_data = irq_get_handler_data(irq);
  204. msi_data = cascade_data->msi_data;
  205. raw_spin_lock(&desc->lock);
  206. if ((msi_data->feature & FSL_PIC_IP_MASK) == FSL_PIC_IP_IPIC) {
  207. if (chip->irq_mask_ack)
  208. chip->irq_mask_ack(idata);
  209. else {
  210. chip->irq_mask(idata);
  211. chip->irq_ack(idata);
  212. }
  213. }
  214. if (unlikely(irqd_irq_inprogress(idata)))
  215. goto unlock;
  216. msir_index = cascade_data->index;
  217. if (msir_index >= NR_MSI_REG)
  218. cascade_irq = NO_IRQ;
  219. irqd_set_chained_irq_inprogress(idata);
  220. switch (msi_data->feature & FSL_PIC_IP_MASK) {
  221. case FSL_PIC_IP_MPIC:
  222. msir_value = fsl_msi_read(msi_data->msi_regs,
  223. msir_index * 0x10);
  224. break;
  225. case FSL_PIC_IP_IPIC:
  226. msir_value = fsl_msi_read(msi_data->msi_regs, msir_index * 0x4);
  227. break;
  228. #ifdef CONFIG_EPAPR_PARAVIRT
  229. case FSL_PIC_IP_VMPIC: {
  230. unsigned int ret;
  231. ret = fh_vmpic_get_msir(virq_to_hw(irq), &msir_value);
  232. if (ret) {
  233. pr_err("fsl-msi: fh_vmpic_get_msir() failed for "
  234. "irq %u (ret=%u)\n", irq, ret);
  235. msir_value = 0;
  236. }
  237. break;
  238. }
  239. #endif
  240. }
  241. while (msir_value) {
  242. intr_index = ffs(msir_value) - 1;
  243. cascade_irq = irq_linear_revmap(msi_data->irqhost,
  244. msir_index * IRQS_PER_MSI_REG +
  245. intr_index + have_shift);
  246. if (cascade_irq != NO_IRQ)
  247. generic_handle_irq(cascade_irq);
  248. have_shift += intr_index + 1;
  249. msir_value = msir_value >> (intr_index + 1);
  250. }
  251. irqd_clr_chained_irq_inprogress(idata);
  252. switch (msi_data->feature & FSL_PIC_IP_MASK) {
  253. case FSL_PIC_IP_MPIC:
  254. case FSL_PIC_IP_VMPIC:
  255. chip->irq_eoi(idata);
  256. break;
  257. case FSL_PIC_IP_IPIC:
  258. if (!irqd_irq_disabled(idata) && chip->irq_unmask)
  259. chip->irq_unmask(idata);
  260. break;
  261. }
  262. unlock:
  263. raw_spin_unlock(&desc->lock);
  264. }
  265. static int fsl_of_msi_remove(struct platform_device *ofdev)
  266. {
  267. struct fsl_msi *msi = platform_get_drvdata(ofdev);
  268. int virq, i;
  269. struct fsl_msi_cascade_data *cascade_data;
  270. if (msi->list.prev != NULL)
  271. list_del(&msi->list);
  272. for (i = 0; i < NR_MSI_REG; i++) {
  273. virq = msi->msi_virqs[i];
  274. if (virq != NO_IRQ) {
  275. cascade_data = irq_get_handler_data(virq);
  276. kfree(cascade_data);
  277. irq_dispose_mapping(virq);
  278. }
  279. }
  280. if (msi->bitmap.bitmap)
  281. msi_bitmap_free(&msi->bitmap);
  282. if ((msi->feature & FSL_PIC_IP_MASK) != FSL_PIC_IP_VMPIC)
  283. iounmap(msi->msi_regs);
  284. kfree(msi);
  285. return 0;
  286. }
  287. static int fsl_msi_setup_hwirq(struct fsl_msi *msi, struct platform_device *dev,
  288. int offset, int irq_index)
  289. {
  290. struct fsl_msi_cascade_data *cascade_data = NULL;
  291. int virt_msir;
  292. virt_msir = irq_of_parse_and_map(dev->dev.of_node, irq_index);
  293. if (virt_msir == NO_IRQ) {
  294. dev_err(&dev->dev, "%s: Cannot translate IRQ index %d\n",
  295. __func__, irq_index);
  296. return 0;
  297. }
  298. cascade_data = kzalloc(sizeof(struct fsl_msi_cascade_data), GFP_KERNEL);
  299. if (!cascade_data) {
  300. dev_err(&dev->dev, "No memory for MSI cascade data\n");
  301. return -ENOMEM;
  302. }
  303. msi->msi_virqs[irq_index] = virt_msir;
  304. cascade_data->index = offset;
  305. cascade_data->msi_data = msi;
  306. irq_set_handler_data(virt_msir, cascade_data);
  307. irq_set_chained_handler(virt_msir, fsl_msi_cascade);
  308. return 0;
  309. }
  310. static const struct of_device_id fsl_of_msi_ids[];
  311. static int fsl_of_msi_probe(struct platform_device *dev)
  312. {
  313. const struct of_device_id *match;
  314. struct fsl_msi *msi;
  315. struct resource res;
  316. int err, i, j, irq_index, count;
  317. int rc;
  318. const u32 *p;
  319. const struct fsl_msi_feature *features;
  320. int len;
  321. u32 offset;
  322. static const u32 all_avail[] = { 0, NR_MSI_IRQS };
  323. match = of_match_device(fsl_of_msi_ids, &dev->dev);
  324. if (!match)
  325. return -EINVAL;
  326. features = match->data;
  327. printk(KERN_DEBUG "Setting up Freescale MSI support\n");
  328. msi = kzalloc(sizeof(struct fsl_msi), GFP_KERNEL);
  329. if (!msi) {
  330. dev_err(&dev->dev, "No memory for MSI structure\n");
  331. return -ENOMEM;
  332. }
  333. platform_set_drvdata(dev, msi);
  334. msi->irqhost = irq_domain_add_linear(dev->dev.of_node,
  335. NR_MSI_IRQS, &fsl_msi_host_ops, msi);
  336. if (msi->irqhost == NULL) {
  337. dev_err(&dev->dev, "No memory for MSI irqhost\n");
  338. err = -ENOMEM;
  339. goto error_out;
  340. }
  341. /*
  342. * Under the Freescale hypervisor, the msi nodes don't have a 'reg'
  343. * property. Instead, we use hypercalls to access the MSI.
  344. */
  345. if ((features->fsl_pic_ip & FSL_PIC_IP_MASK) != FSL_PIC_IP_VMPIC) {
  346. err = of_address_to_resource(dev->dev.of_node, 0, &res);
  347. if (err) {
  348. dev_err(&dev->dev, "invalid resource for node %s\n",
  349. dev->dev.of_node->full_name);
  350. goto error_out;
  351. }
  352. msi->msi_regs = ioremap(res.start, resource_size(&res));
  353. if (!msi->msi_regs) {
  354. err = -ENOMEM;
  355. dev_err(&dev->dev, "could not map node %s\n",
  356. dev->dev.of_node->full_name);
  357. goto error_out;
  358. }
  359. msi->msiir_offset =
  360. features->msiir_offset + (res.start & 0xfffff);
  361. }
  362. msi->feature = features->fsl_pic_ip;
  363. /*
  364. * Remember the phandle, so that we can match with any PCI nodes
  365. * that have an "fsl,msi" property.
  366. */
  367. msi->phandle = dev->dev.of_node->phandle;
  368. rc = fsl_msi_init_allocator(msi);
  369. if (rc) {
  370. dev_err(&dev->dev, "Error allocating MSI bitmap\n");
  371. goto error_out;
  372. }
  373. p = of_get_property(dev->dev.of_node, "msi-available-ranges", &len);
  374. if (p && len % (2 * sizeof(u32)) != 0) {
  375. dev_err(&dev->dev, "%s: Malformed msi-available-ranges property\n",
  376. __func__);
  377. err = -EINVAL;
  378. goto error_out;
  379. }
  380. if (!p) {
  381. p = all_avail;
  382. len = sizeof(all_avail);
  383. }
  384. for (irq_index = 0, i = 0; i < len / (2 * sizeof(u32)); i++) {
  385. if (p[i * 2] % IRQS_PER_MSI_REG ||
  386. p[i * 2 + 1] % IRQS_PER_MSI_REG) {
  387. printk(KERN_WARNING "%s: %s: msi available range of %u at %u is not IRQ-aligned\n",
  388. __func__, dev->dev.of_node->full_name,
  389. p[i * 2 + 1], p[i * 2]);
  390. err = -EINVAL;
  391. goto error_out;
  392. }
  393. offset = p[i * 2] / IRQS_PER_MSI_REG;
  394. count = p[i * 2 + 1] / IRQS_PER_MSI_REG;
  395. for (j = 0; j < count; j++, irq_index++) {
  396. err = fsl_msi_setup_hwirq(msi, dev, offset + j, irq_index);
  397. if (err)
  398. goto error_out;
  399. }
  400. }
  401. list_add_tail(&msi->list, &msi_head);
  402. /* The multiple setting ppc_md.setup_msi_irqs will not harm things */
  403. if (!ppc_md.setup_msi_irqs) {
  404. ppc_md.setup_msi_irqs = fsl_setup_msi_irqs;
  405. ppc_md.teardown_msi_irqs = fsl_teardown_msi_irqs;
  406. ppc_md.msi_check_device = fsl_msi_check_device;
  407. } else if (ppc_md.setup_msi_irqs != fsl_setup_msi_irqs) {
  408. dev_err(&dev->dev, "Different MSI driver already installed!\n");
  409. err = -ENODEV;
  410. goto error_out;
  411. }
  412. return 0;
  413. error_out:
  414. fsl_of_msi_remove(dev);
  415. return err;
  416. }
  417. static const struct fsl_msi_feature mpic_msi_feature = {
  418. .fsl_pic_ip = FSL_PIC_IP_MPIC,
  419. .msiir_offset = 0x140,
  420. };
  421. static const struct fsl_msi_feature ipic_msi_feature = {
  422. .fsl_pic_ip = FSL_PIC_IP_IPIC,
  423. .msiir_offset = 0x38,
  424. };
  425. static const struct fsl_msi_feature vmpic_msi_feature = {
  426. .fsl_pic_ip = FSL_PIC_IP_VMPIC,
  427. .msiir_offset = 0,
  428. };
  429. static const struct of_device_id fsl_of_msi_ids[] = {
  430. {
  431. .compatible = "fsl,mpic-msi",
  432. .data = &mpic_msi_feature,
  433. },
  434. {
  435. .compatible = "fsl,ipic-msi",
  436. .data = &ipic_msi_feature,
  437. },
  438. #ifdef CONFIG_EPAPR_PARAVIRT
  439. {
  440. .compatible = "fsl,vmpic-msi",
  441. .data = &vmpic_msi_feature,
  442. },
  443. #endif
  444. {}
  445. };
  446. static struct platform_driver fsl_of_msi_driver = {
  447. .driver = {
  448. .name = "fsl-msi",
  449. .owner = THIS_MODULE,
  450. .of_match_table = fsl_of_msi_ids,
  451. },
  452. .probe = fsl_of_msi_probe,
  453. .remove = fsl_of_msi_remove,
  454. };
  455. static __init int fsl_of_msi_init(void)
  456. {
  457. return platform_driver_register(&fsl_of_msi_driver);
  458. }
  459. subsys_initcall(fsl_of_msi_init);