pcie-altera-msi.c 7.6 KB

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