pcie-altera-msi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Altera PCIe MSI support
  4. *
  5. * Author: Ley Foon Tan <lftan@altera.com>
  6. *
  7. * Copyright Altera Corporation (C) 2013-2015. All rights reserved
  8. */
  9. #include <linux/interrupt.h>
  10. #include <linux/irqchip/chained_irq.h>
  11. #include <linux/irqdomain.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/msi.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_pci.h>
  17. #include <linux/pci.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/slab.h>
  20. #define MSI_STATUS 0x0
  21. #define MSI_ERROR 0x4
  22. #define MSI_INTMASK 0x8
  23. #define MAX_MSI_VECTORS 32
  24. struct altera_msi {
  25. DECLARE_BITMAP(used, MAX_MSI_VECTORS);
  26. struct mutex lock; /* protect "used" bitmap */
  27. struct platform_device *pdev;
  28. struct irq_domain *msi_domain;
  29. struct irq_domain *inner_domain;
  30. void __iomem *csr_base;
  31. void __iomem *vector_base;
  32. phys_addr_t vector_phy;
  33. u32 num_of_vectors;
  34. int irq;
  35. };
  36. static inline void msi_writel(struct altera_msi *msi, const u32 value,
  37. const u32 reg)
  38. {
  39. writel_relaxed(value, msi->csr_base + reg);
  40. }
  41. static inline u32 msi_readl(struct altera_msi *msi, const u32 reg)
  42. {
  43. return readl_relaxed(msi->csr_base + reg);
  44. }
  45. static void altera_msi_isr(struct irq_desc *desc)
  46. {
  47. struct irq_chip *chip = irq_desc_get_chip(desc);
  48. struct altera_msi *msi;
  49. unsigned long status;
  50. u32 bit;
  51. int ret;
  52. chained_irq_enter(chip, desc);
  53. msi = irq_desc_get_handler_data(desc);
  54. while ((status = msi_readl(msi, MSI_STATUS)) != 0) {
  55. for_each_set_bit(bit, &status, msi->num_of_vectors) {
  56. /* Dummy read from vector to clear the interrupt */
  57. readl_relaxed(msi->vector_base + (bit * sizeof(u32)));
  58. ret = generic_handle_domain_irq(msi->inner_domain, bit);
  59. if (ret)
  60. dev_err_ratelimited(&msi->pdev->dev, "unexpected MSI\n");
  61. }
  62. }
  63. chained_irq_exit(chip, desc);
  64. }
  65. static struct irq_chip altera_msi_irq_chip = {
  66. .name = "Altera PCIe MSI",
  67. .irq_mask = pci_msi_mask_irq,
  68. .irq_unmask = pci_msi_unmask_irq,
  69. };
  70. static struct msi_domain_info altera_msi_domain_info = {
  71. .flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  72. MSI_FLAG_NO_AFFINITY | MSI_FLAG_PCI_MSIX,
  73. .chip = &altera_msi_irq_chip,
  74. };
  75. static void altera_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  76. {
  77. struct altera_msi *msi = irq_data_get_irq_chip_data(data);
  78. phys_addr_t addr = msi->vector_phy + (data->hwirq * sizeof(u32));
  79. msg->address_lo = lower_32_bits(addr);
  80. msg->address_hi = upper_32_bits(addr);
  81. msg->data = data->hwirq;
  82. dev_dbg(&msi->pdev->dev, "msi#%d address_hi %#x address_lo %#x\n",
  83. (int)data->hwirq, msg->address_hi, msg->address_lo);
  84. }
  85. static struct irq_chip altera_msi_bottom_irq_chip = {
  86. .name = "Altera MSI",
  87. .irq_compose_msi_msg = altera_compose_msi_msg,
  88. };
  89. static int altera_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  90. unsigned int nr_irqs, void *args)
  91. {
  92. struct altera_msi *msi = domain->host_data;
  93. unsigned long bit;
  94. u32 mask;
  95. WARN_ON(nr_irqs != 1);
  96. mutex_lock(&msi->lock);
  97. bit = find_first_zero_bit(msi->used, msi->num_of_vectors);
  98. if (bit >= msi->num_of_vectors) {
  99. mutex_unlock(&msi->lock);
  100. return -ENOSPC;
  101. }
  102. set_bit(bit, msi->used);
  103. mutex_unlock(&msi->lock);
  104. irq_domain_set_info(domain, virq, bit, &altera_msi_bottom_irq_chip,
  105. domain->host_data, handle_simple_irq,
  106. NULL, NULL);
  107. mask = msi_readl(msi, MSI_INTMASK);
  108. mask |= 1 << bit;
  109. msi_writel(msi, mask, MSI_INTMASK);
  110. return 0;
  111. }
  112. static void altera_irq_domain_free(struct irq_domain *domain,
  113. unsigned int virq, unsigned int nr_irqs)
  114. {
  115. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  116. struct altera_msi *msi = irq_data_get_irq_chip_data(d);
  117. u32 mask;
  118. mutex_lock(&msi->lock);
  119. if (!test_bit(d->hwirq, msi->used)) {
  120. dev_err(&msi->pdev->dev, "trying to free unused MSI#%lu\n",
  121. d->hwirq);
  122. } else {
  123. __clear_bit(d->hwirq, msi->used);
  124. mask = msi_readl(msi, MSI_INTMASK);
  125. mask &= ~(1 << d->hwirq);
  126. msi_writel(msi, mask, MSI_INTMASK);
  127. }
  128. mutex_unlock(&msi->lock);
  129. }
  130. static const struct irq_domain_ops msi_domain_ops = {
  131. .alloc = altera_irq_domain_alloc,
  132. .free = altera_irq_domain_free,
  133. };
  134. static int altera_allocate_domains(struct altera_msi *msi)
  135. {
  136. struct fwnode_handle *fwnode = of_node_to_fwnode(msi->pdev->dev.of_node);
  137. msi->inner_domain = irq_domain_add_linear(NULL, msi->num_of_vectors,
  138. &msi_domain_ops, msi);
  139. if (!msi->inner_domain) {
  140. dev_err(&msi->pdev->dev, "failed to create IRQ domain\n");
  141. return -ENOMEM;
  142. }
  143. msi->msi_domain = pci_msi_create_irq_domain(fwnode,
  144. &altera_msi_domain_info, msi->inner_domain);
  145. if (!msi->msi_domain) {
  146. dev_err(&msi->pdev->dev, "failed to create MSI domain\n");
  147. irq_domain_remove(msi->inner_domain);
  148. return -ENOMEM;
  149. }
  150. return 0;
  151. }
  152. static void altera_free_domains(struct altera_msi *msi)
  153. {
  154. irq_domain_remove(msi->msi_domain);
  155. irq_domain_remove(msi->inner_domain);
  156. }
  157. static void altera_msi_remove(struct platform_device *pdev)
  158. {
  159. struct altera_msi *msi = platform_get_drvdata(pdev);
  160. msi_writel(msi, 0, MSI_INTMASK);
  161. irq_set_chained_handler_and_data(msi->irq, NULL, NULL);
  162. altera_free_domains(msi);
  163. platform_set_drvdata(pdev, NULL);
  164. }
  165. static int altera_msi_probe(struct platform_device *pdev)
  166. {
  167. struct altera_msi *msi;
  168. struct device_node *np = pdev->dev.of_node;
  169. struct resource *res;
  170. int ret;
  171. msi = devm_kzalloc(&pdev->dev, sizeof(struct altera_msi),
  172. GFP_KERNEL);
  173. if (!msi)
  174. return -ENOMEM;
  175. mutex_init(&msi->lock);
  176. msi->pdev = pdev;
  177. msi->csr_base = devm_platform_ioremap_resource_byname(pdev, "csr");
  178. if (IS_ERR(msi->csr_base)) {
  179. dev_err(&pdev->dev, "failed to map csr memory\n");
  180. return PTR_ERR(msi->csr_base);
  181. }
  182. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  183. "vector_slave");
  184. msi->vector_base = devm_ioremap_resource(&pdev->dev, res);
  185. if (IS_ERR(msi->vector_base))
  186. return PTR_ERR(msi->vector_base);
  187. msi->vector_phy = res->start;
  188. if (of_property_read_u32(np, "num-vectors", &msi->num_of_vectors)) {
  189. dev_err(&pdev->dev, "failed to parse the number of vectors\n");
  190. return -EINVAL;
  191. }
  192. ret = altera_allocate_domains(msi);
  193. if (ret)
  194. return ret;
  195. msi->irq = platform_get_irq(pdev, 0);
  196. if (msi->irq < 0) {
  197. ret = msi->irq;
  198. goto err;
  199. }
  200. irq_set_chained_handler_and_data(msi->irq, altera_msi_isr, msi);
  201. platform_set_drvdata(pdev, msi);
  202. return 0;
  203. err:
  204. altera_msi_remove(pdev);
  205. return ret;
  206. }
  207. static const struct of_device_id altera_msi_of_match[] = {
  208. { .compatible = "altr,msi-1.0", NULL },
  209. { },
  210. };
  211. static struct platform_driver altera_msi_driver = {
  212. .driver = {
  213. .name = "altera-msi",
  214. .of_match_table = altera_msi_of_match,
  215. },
  216. .probe = altera_msi_probe,
  217. .remove_new = altera_msi_remove,
  218. };
  219. static int __init altera_msi_init(void)
  220. {
  221. return platform_driver_register(&altera_msi_driver);
  222. }
  223. static void __exit altera_msi_exit(void)
  224. {
  225. platform_driver_unregister(&altera_msi_driver);
  226. }
  227. subsys_initcall(altera_msi_init);
  228. MODULE_DEVICE_TABLE(of, altera_msi_of_match);
  229. module_exit(altera_msi_exit);
  230. MODULE_DESCRIPTION("Altera PCIe MSI support driver");
  231. MODULE_LICENSE("GPL v2");