ppc4xx_hsta_msi.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. /* We don't support MSI-X */
  40. if (type == PCI_CAP_ID_MSIX) {
  41. pr_debug("%s: MSI-X not supported.\n", __func__);
  42. return -EINVAL;
  43. }
  44. list_for_each_entry(entry, &dev->msi_list, list) {
  45. irq = msi_bitmap_alloc_hwirqs(&ppc4xx_hsta_msi.bmp, 1);
  46. if (irq < 0) {
  47. pr_debug("%s: Failed to allocate msi interrupt\n",
  48. __func__);
  49. return irq;
  50. }
  51. hwirq = ppc4xx_hsta_msi.irq_map[irq];
  52. if (hwirq == NO_IRQ) {
  53. pr_err("%s: Failed mapping irq %d\n", __func__, irq);
  54. return -EINVAL;
  55. }
  56. /*
  57. * HSTA generates interrupts on writes to 128-bit aligned
  58. * addresses.
  59. */
  60. addr = ppc4xx_hsta_msi.address + irq*0x10;
  61. msg.address_hi = upper_32_bits(addr);
  62. msg.address_lo = lower_32_bits(addr);
  63. /* Data is not used by the HSTA. */
  64. msg.data = 0;
  65. pr_debug("%s: Setup irq %d (0x%0llx)\n", __func__, hwirq,
  66. (((u64) msg.address_hi) << 32) | msg.address_lo);
  67. if (irq_set_msi_desc(hwirq, entry)) {
  68. pr_err(
  69. "%s: Invalid hwirq %d specified in device tree\n",
  70. __func__, hwirq);
  71. msi_bitmap_free_hwirqs(&ppc4xx_hsta_msi.bmp, irq, 1);
  72. return -EINVAL;
  73. }
  74. write_msi_msg(hwirq, &msg);
  75. }
  76. return 0;
  77. }
  78. static int hsta_find_hwirq_offset(int hwirq)
  79. {
  80. int irq;
  81. /* Find the offset given the hwirq */
  82. for (irq = 0; irq < ppc4xx_hsta_msi.irq_count; irq++)
  83. if (ppc4xx_hsta_msi.irq_map[irq] == hwirq)
  84. return irq;
  85. return -EINVAL;
  86. }
  87. static void hsta_teardown_msi_irqs(struct pci_dev *dev)
  88. {
  89. struct msi_desc *entry;
  90. int irq;
  91. list_for_each_entry(entry, &dev->msi_list, list) {
  92. if (entry->irq == NO_IRQ)
  93. continue;
  94. irq = hsta_find_hwirq_offset(entry->irq);
  95. /* entry->irq should always be in irq_map */
  96. BUG_ON(irq < 0);
  97. irq_set_msi_desc(entry->irq, NULL);
  98. msi_bitmap_free_hwirqs(&ppc4xx_hsta_msi.bmp, irq, 1);
  99. pr_debug("%s: Teardown IRQ %u (index %u)\n", __func__,
  100. entry->irq, irq);
  101. }
  102. }
  103. static int hsta_msi_probe(struct platform_device *pdev)
  104. {
  105. struct device *dev = &pdev->dev;
  106. struct resource *mem;
  107. int irq, ret, irq_count;
  108. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  109. if (IS_ERR(mem)) {
  110. dev_err(dev, "Unable to get mmio space\n");
  111. return -EINVAL;
  112. }
  113. irq_count = of_irq_count(dev->of_node);
  114. if (!irq_count) {
  115. dev_err(dev, "Unable to find IRQ range\n");
  116. return -EINVAL;
  117. }
  118. ppc4xx_hsta_msi.dev = dev;
  119. ppc4xx_hsta_msi.address = mem->start;
  120. ppc4xx_hsta_msi.data = ioremap(mem->start, resource_size(mem));
  121. ppc4xx_hsta_msi.irq_count = irq_count;
  122. if (IS_ERR(ppc4xx_hsta_msi.data)) {
  123. dev_err(dev, "Unable to map memory\n");
  124. return -ENOMEM;
  125. }
  126. ret = msi_bitmap_alloc(&ppc4xx_hsta_msi.bmp, irq_count, dev->of_node);
  127. if (ret)
  128. goto out;
  129. ppc4xx_hsta_msi.irq_map = kmalloc(sizeof(int) * irq_count, GFP_KERNEL);
  130. if (IS_ERR(ppc4xx_hsta_msi.irq_map)) {
  131. ret = -ENOMEM;
  132. goto out1;
  133. }
  134. /* Setup a mapping from irq offsets to hardware irq numbers */
  135. for (irq = 0; irq < irq_count; irq++) {
  136. ppc4xx_hsta_msi.irq_map[irq] =
  137. irq_of_parse_and_map(dev->of_node, irq);
  138. if (ppc4xx_hsta_msi.irq_map[irq] == NO_IRQ) {
  139. dev_err(dev, "Unable to map IRQ\n");
  140. ret = -EINVAL;
  141. goto out2;
  142. }
  143. }
  144. ppc_md.setup_msi_irqs = hsta_setup_msi_irqs;
  145. ppc_md.teardown_msi_irqs = hsta_teardown_msi_irqs;
  146. return 0;
  147. out2:
  148. kfree(ppc4xx_hsta_msi.irq_map);
  149. out1:
  150. msi_bitmap_free(&ppc4xx_hsta_msi.bmp);
  151. out:
  152. iounmap(ppc4xx_hsta_msi.data);
  153. return ret;
  154. }
  155. static const struct of_device_id hsta_msi_ids[] = {
  156. {
  157. .compatible = "ibm,hsta-msi",
  158. },
  159. {}
  160. };
  161. static struct platform_driver hsta_msi_driver = {
  162. .probe = hsta_msi_probe,
  163. .driver = {
  164. .name = "hsta-msi",
  165. .owner = THIS_MODULE,
  166. .of_match_table = hsta_msi_ids,
  167. },
  168. };
  169. static int hsta_msi_init(void)
  170. {
  171. return platform_driver_register(&hsta_msi_driver);
  172. }
  173. subsys_initcall(hsta_msi_init);