pcie-altera-msi.c 7.9 KB

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