ppc4xx_hsta_msi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * MSI support for PPC4xx SoCs using High Speed Transfer Assist (HSTA) for
  3. * generation of the interrupt.
  4. *
  5. * Copyright © 2013 Alistair Popple <alistair@popple.id.au> IBM Corporation
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the
  9. * Free Software Foundation; either version 2 of the License, or (at your
  10. * option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/msi.h>
  15. #include <linux/of.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/pci.h>
  18. #include <linux/semaphore.h>
  19. #include <asm/msi_bitmap.h>
  20. struct ppc4xx_hsta_msi {
  21. struct device *dev;
  22. /* The ioremapped HSTA MSI IO space */
  23. u32 __iomem *data;
  24. /* Physical address of HSTA MSI IO space */
  25. u64 address;
  26. struct msi_bitmap bmp;
  27. /* An array mapping offsets to hardware IRQs */
  28. int *irq_map;
  29. /* Number of hwirqs supported */
  30. int irq_count;
  31. };
  32. static struct ppc4xx_hsta_msi ppc4xx_hsta_msi;
  33. static int hsta_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
  34. {
  35. struct msi_msg msg;
  36. struct msi_desc *entry;
  37. int irq, hwirq;
  38. u64 addr;
  39. list_for_each_entry(entry, &dev->msi_list, list) {
  40. irq = msi_bitmap_alloc_hwirqs(&ppc4xx_hsta_msi.bmp, 1);
  41. if (irq < 0) {
  42. pr_debug("%s: Failed to allocate msi interrupt\n",
  43. __func__);
  44. return irq;
  45. }
  46. hwirq = ppc4xx_hsta_msi.irq_map[irq];
  47. if (hwirq == NO_IRQ) {
  48. pr_err("%s: Failed mapping irq %d\n", __func__, irq);
  49. return -EINVAL;
  50. }
  51. /*
  52. * HSTA generates interrupts on writes to 128-bit aligned
  53. * addresses.
  54. */
  55. addr = ppc4xx_hsta_msi.address + irq*0x10;
  56. msg.address_hi = upper_32_bits(addr);
  57. msg.address_lo = lower_32_bits(addr);
  58. /* Data is not used by the HSTA. */
  59. msg.data = 0;
  60. pr_debug("%s: Setup irq %d (0x%0llx)\n", __func__, hwirq,
  61. (((u64) msg.address_hi) << 32) | msg.address_lo);
  62. if (irq_set_msi_desc(hwirq, entry)) {
  63. pr_err(
  64. "%s: Invalid hwirq %d specified in device tree\n",
  65. __func__, hwirq);
  66. msi_bitmap_free_hwirqs(&ppc4xx_hsta_msi.bmp, irq, 1);
  67. return -EINVAL;
  68. }
  69. write_msi_msg(hwirq, &msg);
  70. }
  71. return 0;
  72. }
  73. static int hsta_find_hwirq_offset(int hwirq)
  74. {
  75. int irq;
  76. /* Find the offset given the hwirq */
  77. for (irq = 0; irq < ppc4xx_hsta_msi.irq_count; irq++)
  78. if (ppc4xx_hsta_msi.irq_map[irq] == hwirq)
  79. return irq;
  80. return -EINVAL;
  81. }
  82. static void hsta_teardown_msi_irqs(struct pci_dev *dev)
  83. {
  84. struct msi_desc *entry;
  85. int irq;
  86. list_for_each_entry(entry, &dev->msi_list, list) {
  87. if (entry->irq == NO_IRQ)
  88. continue;
  89. irq = hsta_find_hwirq_offset(entry->irq);
  90. /* entry->irq should always be in irq_map */
  91. BUG_ON(irq < 0);
  92. irq_set_msi_desc(entry->irq, NULL);
  93. msi_bitmap_free_hwirqs(&ppc4xx_hsta_msi.bmp, irq, 1);
  94. pr_debug("%s: Teardown IRQ %u (index %u)\n", __func__,
  95. entry->irq, irq);
  96. }
  97. }
  98. static int hsta_msi_check_device(struct pci_dev *pdev, int nvec, int type)
  99. {
  100. /* We don't support MSI-X */
  101. if (type == PCI_CAP_ID_MSIX) {
  102. pr_debug("%s: MSI-X not supported.\n", __func__);
  103. return -EINVAL;
  104. }
  105. return 0;
  106. }
  107. static int hsta_msi_probe(struct platform_device *pdev)
  108. {
  109. struct device *dev = &pdev->dev;
  110. struct resource *mem;
  111. int irq, ret, irq_count;
  112. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  113. if (IS_ERR(mem)) {
  114. dev_err(dev, "Unable to get mmio space\n");
  115. return -EINVAL;
  116. }
  117. irq_count = of_irq_count(dev->of_node);
  118. if (!irq_count) {
  119. dev_err(dev, "Unable to find IRQ range\n");
  120. return -EINVAL;
  121. }
  122. ppc4xx_hsta_msi.dev = dev;
  123. ppc4xx_hsta_msi.address = mem->start;
  124. ppc4xx_hsta_msi.data = ioremap(mem->start, resource_size(mem));
  125. ppc4xx_hsta_msi.irq_count = irq_count;
  126. if (IS_ERR(ppc4xx_hsta_msi.data)) {
  127. dev_err(dev, "Unable to map memory\n");
  128. return -ENOMEM;
  129. }
  130. ret = msi_bitmap_alloc(&ppc4xx_hsta_msi.bmp, irq_count, dev->of_node);
  131. if (ret)
  132. goto out;
  133. ppc4xx_hsta_msi.irq_map = kmalloc(sizeof(int) * irq_count, GFP_KERNEL);
  134. if (IS_ERR(ppc4xx_hsta_msi.irq_map)) {
  135. ret = -ENOMEM;
  136. goto out1;
  137. }
  138. /* Setup a mapping from irq offsets to hardware irq numbers */
  139. for (irq = 0; irq < irq_count; irq++) {
  140. ppc4xx_hsta_msi.irq_map[irq] =
  141. irq_of_parse_and_map(dev->of_node, irq);
  142. if (ppc4xx_hsta_msi.irq_map[irq] == NO_IRQ) {
  143. dev_err(dev, "Unable to map IRQ\n");
  144. ret = -EINVAL;
  145. goto out2;
  146. }
  147. }
  148. ppc_md.setup_msi_irqs = hsta_setup_msi_irqs;
  149. ppc_md.teardown_msi_irqs = hsta_teardown_msi_irqs;
  150. ppc_md.msi_check_device = hsta_msi_check_device;
  151. return 0;
  152. out2:
  153. kfree(ppc4xx_hsta_msi.irq_map);
  154. out1:
  155. msi_bitmap_free(&ppc4xx_hsta_msi.bmp);
  156. out:
  157. iounmap(ppc4xx_hsta_msi.data);
  158. return ret;
  159. }
  160. static const struct of_device_id hsta_msi_ids[] = {
  161. {
  162. .compatible = "ibm,hsta-msi",
  163. },
  164. {}
  165. };
  166. static struct platform_driver hsta_msi_driver = {
  167. .probe = hsta_msi_probe,
  168. .driver = {
  169. .name = "hsta-msi",
  170. .owner = THIS_MODULE,
  171. .of_match_table = hsta_msi_ids,
  172. },
  173. };
  174. static int hsta_msi_init(void)
  175. {
  176. return platform_driver_register(&hsta_msi_driver);
  177. }
  178. subsys_initcall(hsta_msi_init);