irq-gic-v3-its-pci-msi.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (C) 2013-2015 ARM Limited, All Rights Reserved.
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/msi.h>
  18. #include <linux/of.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/of_pci.h>
  21. static void its_mask_msi_irq(struct irq_data *d)
  22. {
  23. pci_msi_mask_irq(d);
  24. irq_chip_mask_parent(d);
  25. }
  26. static void its_unmask_msi_irq(struct irq_data *d)
  27. {
  28. pci_msi_unmask_irq(d);
  29. irq_chip_unmask_parent(d);
  30. }
  31. static struct irq_chip its_msi_irq_chip = {
  32. .name = "ITS-MSI",
  33. .irq_unmask = its_unmask_msi_irq,
  34. .irq_mask = its_mask_msi_irq,
  35. .irq_eoi = irq_chip_eoi_parent,
  36. .irq_write_msi_msg = pci_msi_domain_write_msg,
  37. };
  38. struct its_pci_alias {
  39. struct pci_dev *pdev;
  40. u32 count;
  41. };
  42. static int its_pci_msi_vec_count(struct pci_dev *pdev)
  43. {
  44. int msi, msix;
  45. msi = max(pci_msi_vec_count(pdev), 0);
  46. msix = max(pci_msix_vec_count(pdev), 0);
  47. return max(msi, msix);
  48. }
  49. static int its_get_pci_alias(struct pci_dev *pdev, u16 alias, void *data)
  50. {
  51. struct its_pci_alias *dev_alias = data;
  52. if (pdev != dev_alias->pdev)
  53. dev_alias->count += its_pci_msi_vec_count(pdev);
  54. return 0;
  55. }
  56. static int its_pci_msi_prepare(struct irq_domain *domain, struct device *dev,
  57. int nvec, msi_alloc_info_t *info)
  58. {
  59. struct pci_dev *pdev;
  60. struct its_pci_alias dev_alias;
  61. struct msi_domain_info *msi_info;
  62. if (!dev_is_pci(dev))
  63. return -EINVAL;
  64. msi_info = msi_get_domain_info(domain->parent);
  65. pdev = to_pci_dev(dev);
  66. dev_alias.pdev = pdev;
  67. dev_alias.count = nvec;
  68. pci_for_each_dma_alias(pdev, its_get_pci_alias, &dev_alias);
  69. /* ITS specific DeviceID, as the core ITS ignores dev. */
  70. info->scratchpad[0].ul = pci_msi_domain_get_msi_rid(domain, pdev);
  71. return msi_info->ops->msi_prepare(domain->parent,
  72. dev, dev_alias.count, info);
  73. }
  74. static struct msi_domain_ops its_pci_msi_ops = {
  75. .msi_prepare = its_pci_msi_prepare,
  76. };
  77. static struct msi_domain_info its_pci_msi_domain_info = {
  78. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  79. MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX),
  80. .ops = &its_pci_msi_ops,
  81. .chip = &its_msi_irq_chip,
  82. };
  83. static struct of_device_id its_device_id[] = {
  84. { .compatible = "arm,gic-v3-its", },
  85. {},
  86. };
  87. static int __init its_pci_msi_init(void)
  88. {
  89. struct device_node *np;
  90. struct irq_domain *parent;
  91. for (np = of_find_matching_node(NULL, its_device_id); np;
  92. np = of_find_matching_node(np, its_device_id)) {
  93. if (!of_property_read_bool(np, "msi-controller"))
  94. continue;
  95. parent = irq_find_matching_host(np, DOMAIN_BUS_NEXUS);
  96. if (!parent || !msi_get_domain_info(parent)) {
  97. pr_err("%s: unable to locate ITS domain\n",
  98. np->full_name);
  99. continue;
  100. }
  101. if (!pci_msi_create_irq_domain(of_node_to_fwnode(np),
  102. &its_pci_msi_domain_info,
  103. parent)) {
  104. pr_err("%s: unable to create PCI domain\n",
  105. np->full_name);
  106. continue;
  107. }
  108. pr_info("PCI/MSI: %s domain created\n", np->full_name);
  109. }
  110. return 0;
  111. }
  112. early_initcall(its_pci_msi_init);