irq-ls-scfg-msi.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Freescale SCFG MSI(-X) support
  3. *
  4. * Copyright (C) 2016 Freescale Semiconductor.
  5. *
  6. * Author: Minghuan Lian <Minghuan.Lian@nxp.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/msi.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/irqchip/chained_irq.h>
  18. #include <linux/irqdomain.h>
  19. #include <linux/of_pci.h>
  20. #include <linux/of_platform.h>
  21. #include <linux/spinlock.h>
  22. #define MSI_MAX_IRQS 32
  23. #define MSI_IBS_SHIFT 3
  24. #define MSIR 4
  25. struct ls_scfg_msi {
  26. spinlock_t lock;
  27. struct platform_device *pdev;
  28. struct irq_domain *parent;
  29. struct irq_domain *msi_domain;
  30. void __iomem *regs;
  31. phys_addr_t msiir_addr;
  32. int irq;
  33. DECLARE_BITMAP(used, MSI_MAX_IRQS);
  34. };
  35. static struct irq_chip ls_scfg_msi_irq_chip = {
  36. .name = "MSI",
  37. .irq_mask = pci_msi_mask_irq,
  38. .irq_unmask = pci_msi_unmask_irq,
  39. };
  40. static struct msi_domain_info ls_scfg_msi_domain_info = {
  41. .flags = (MSI_FLAG_USE_DEF_DOM_OPS |
  42. MSI_FLAG_USE_DEF_CHIP_OPS |
  43. MSI_FLAG_PCI_MSIX),
  44. .chip = &ls_scfg_msi_irq_chip,
  45. };
  46. static void ls_scfg_msi_compose_msg(struct irq_data *data, struct msi_msg *msg)
  47. {
  48. struct ls_scfg_msi *msi_data = irq_data_get_irq_chip_data(data);
  49. msg->address_hi = upper_32_bits(msi_data->msiir_addr);
  50. msg->address_lo = lower_32_bits(msi_data->msiir_addr);
  51. msg->data = data->hwirq << MSI_IBS_SHIFT;
  52. }
  53. static int ls_scfg_msi_set_affinity(struct irq_data *irq_data,
  54. const struct cpumask *mask, bool force)
  55. {
  56. return -EINVAL;
  57. }
  58. static struct irq_chip ls_scfg_msi_parent_chip = {
  59. .name = "SCFG",
  60. .irq_compose_msi_msg = ls_scfg_msi_compose_msg,
  61. .irq_set_affinity = ls_scfg_msi_set_affinity,
  62. };
  63. static int ls_scfg_msi_domain_irq_alloc(struct irq_domain *domain,
  64. unsigned int virq,
  65. unsigned int nr_irqs,
  66. void *args)
  67. {
  68. struct ls_scfg_msi *msi_data = domain->host_data;
  69. int pos, err = 0;
  70. WARN_ON(nr_irqs != 1);
  71. spin_lock(&msi_data->lock);
  72. pos = find_first_zero_bit(msi_data->used, MSI_MAX_IRQS);
  73. if (pos < MSI_MAX_IRQS)
  74. __set_bit(pos, msi_data->used);
  75. else
  76. err = -ENOSPC;
  77. spin_unlock(&msi_data->lock);
  78. if (err)
  79. return err;
  80. irq_domain_set_info(domain, virq, pos,
  81. &ls_scfg_msi_parent_chip, msi_data,
  82. handle_simple_irq, NULL, NULL);
  83. return 0;
  84. }
  85. static void ls_scfg_msi_domain_irq_free(struct irq_domain *domain,
  86. unsigned int virq, unsigned int nr_irqs)
  87. {
  88. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  89. struct ls_scfg_msi *msi_data = irq_data_get_irq_chip_data(d);
  90. int pos;
  91. pos = d->hwirq;
  92. if (pos < 0 || pos >= MSI_MAX_IRQS) {
  93. pr_err("failed to teardown msi. Invalid hwirq %d\n", pos);
  94. return;
  95. }
  96. spin_lock(&msi_data->lock);
  97. __clear_bit(pos, msi_data->used);
  98. spin_unlock(&msi_data->lock);
  99. }
  100. static const struct irq_domain_ops ls_scfg_msi_domain_ops = {
  101. .alloc = ls_scfg_msi_domain_irq_alloc,
  102. .free = ls_scfg_msi_domain_irq_free,
  103. };
  104. static void ls_scfg_msi_irq_handler(struct irq_desc *desc)
  105. {
  106. struct ls_scfg_msi *msi_data = irq_desc_get_handler_data(desc);
  107. unsigned long val;
  108. int pos, virq;
  109. chained_irq_enter(irq_desc_get_chip(desc), desc);
  110. val = ioread32be(msi_data->regs + MSIR);
  111. for_each_set_bit(pos, &val, MSI_MAX_IRQS) {
  112. virq = irq_find_mapping(msi_data->parent, (31 - pos));
  113. if (virq)
  114. generic_handle_irq(virq);
  115. }
  116. chained_irq_exit(irq_desc_get_chip(desc), desc);
  117. }
  118. static int ls_scfg_msi_domains_init(struct ls_scfg_msi *msi_data)
  119. {
  120. /* Initialize MSI domain parent */
  121. msi_data->parent = irq_domain_add_linear(NULL,
  122. MSI_MAX_IRQS,
  123. &ls_scfg_msi_domain_ops,
  124. msi_data);
  125. if (!msi_data->parent) {
  126. dev_err(&msi_data->pdev->dev, "failed to create IRQ domain\n");
  127. return -ENOMEM;
  128. }
  129. msi_data->msi_domain = pci_msi_create_irq_domain(
  130. of_node_to_fwnode(msi_data->pdev->dev.of_node),
  131. &ls_scfg_msi_domain_info,
  132. msi_data->parent);
  133. if (!msi_data->msi_domain) {
  134. dev_err(&msi_data->pdev->dev, "failed to create MSI domain\n");
  135. irq_domain_remove(msi_data->parent);
  136. return -ENOMEM;
  137. }
  138. return 0;
  139. }
  140. static int ls_scfg_msi_probe(struct platform_device *pdev)
  141. {
  142. struct ls_scfg_msi *msi_data;
  143. struct resource *res;
  144. int ret;
  145. msi_data = devm_kzalloc(&pdev->dev, sizeof(*msi_data), GFP_KERNEL);
  146. if (!msi_data)
  147. return -ENOMEM;
  148. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  149. msi_data->regs = devm_ioremap_resource(&pdev->dev, res);
  150. if (IS_ERR(msi_data->regs)) {
  151. dev_err(&pdev->dev, "failed to initialize 'regs'\n");
  152. return PTR_ERR(msi_data->regs);
  153. }
  154. msi_data->msiir_addr = res->start;
  155. msi_data->irq = platform_get_irq(pdev, 0);
  156. if (msi_data->irq <= 0) {
  157. dev_err(&pdev->dev, "failed to get MSI irq\n");
  158. return -ENODEV;
  159. }
  160. msi_data->pdev = pdev;
  161. spin_lock_init(&msi_data->lock);
  162. ret = ls_scfg_msi_domains_init(msi_data);
  163. if (ret)
  164. return ret;
  165. irq_set_chained_handler_and_data(msi_data->irq,
  166. ls_scfg_msi_irq_handler,
  167. msi_data);
  168. platform_set_drvdata(pdev, msi_data);
  169. return 0;
  170. }
  171. static int ls_scfg_msi_remove(struct platform_device *pdev)
  172. {
  173. struct ls_scfg_msi *msi_data = platform_get_drvdata(pdev);
  174. irq_set_chained_handler_and_data(msi_data->irq, NULL, NULL);
  175. irq_domain_remove(msi_data->msi_domain);
  176. irq_domain_remove(msi_data->parent);
  177. platform_set_drvdata(pdev, NULL);
  178. return 0;
  179. }
  180. static const struct of_device_id ls_scfg_msi_id[] = {
  181. { .compatible = "fsl,1s1021a-msi", },
  182. { .compatible = "fsl,1s1043a-msi", },
  183. {},
  184. };
  185. static struct platform_driver ls_scfg_msi_driver = {
  186. .driver = {
  187. .name = "ls-scfg-msi",
  188. .of_match_table = ls_scfg_msi_id,
  189. },
  190. .probe = ls_scfg_msi_probe,
  191. .remove = ls_scfg_msi_remove,
  192. };
  193. module_platform_driver(ls_scfg_msi_driver);
  194. MODULE_AUTHOR("Minghuan Lian <Minghuan.Lian@nxp.com>");
  195. MODULE_DESCRIPTION("Freescale Layerscape SCFG MSI controller driver");
  196. MODULE_LICENSE("GPL v2");