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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 dev_id;
  41. u32 count;
  42. };
  43. static int its_pci_msi_vec_count(struct pci_dev *pdev)
  44. {
  45. int msi, msix;
  46. msi = max(pci_msi_vec_count(pdev), 0);
  47. msix = max(pci_msix_vec_count(pdev), 0);
  48. return max(msi, msix);
  49. }
  50. static int its_get_pci_alias(struct pci_dev *pdev, u16 alias, void *data)
  51. {
  52. struct its_pci_alias *dev_alias = data;
  53. dev_alias->dev_id = alias;
  54. if (pdev != dev_alias->pdev)
  55. dev_alias->count += its_pci_msi_vec_count(pdev);
  56. return 0;
  57. }
  58. static int its_pci_msi_prepare(struct irq_domain *domain, struct device *dev,
  59. int nvec, msi_alloc_info_t *info)
  60. {
  61. struct pci_dev *pdev;
  62. struct its_pci_alias dev_alias;
  63. struct msi_domain_info *msi_info;
  64. if (!dev_is_pci(dev))
  65. return -EINVAL;
  66. msi_info = msi_get_domain_info(domain->parent);
  67. pdev = to_pci_dev(dev);
  68. dev_alias.pdev = pdev;
  69. dev_alias.count = nvec;
  70. pci_for_each_dma_alias(pdev, its_get_pci_alias, &dev_alias);
  71. /* ITS specific DeviceID, as the core ITS ignores dev. */
  72. info->scratchpad[0].ul = dev_alias.dev_id;
  73. return msi_info->ops->msi_prepare(domain->parent,
  74. dev, dev_alias.count, info);
  75. }
  76. static struct msi_domain_ops its_pci_msi_ops = {
  77. .msi_prepare = its_pci_msi_prepare,
  78. };
  79. static struct msi_domain_info its_pci_msi_domain_info = {
  80. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  81. MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX),
  82. .ops = &its_pci_msi_ops,
  83. .chip = &its_msi_irq_chip,
  84. };
  85. static struct of_device_id its_device_id[] = {
  86. { .compatible = "arm,gic-v3-its", },
  87. {},
  88. };
  89. static int __init its_pci_msi_init(void)
  90. {
  91. struct device_node *np;
  92. struct irq_domain *parent;
  93. for (np = of_find_matching_node(NULL, its_device_id); np;
  94. np = of_find_matching_node(np, its_device_id)) {
  95. if (!of_property_read_bool(np, "msi-controller"))
  96. continue;
  97. parent = irq_find_matching_host(np, DOMAIN_BUS_NEXUS);
  98. if (!parent || !msi_get_domain_info(parent)) {
  99. pr_err("%s: unable to locate ITS domain\n",
  100. np->full_name);
  101. continue;
  102. }
  103. if (!pci_msi_create_irq_domain(np, &its_pci_msi_domain_info,
  104. parent)) {
  105. pr_err("%s: unable to create PCI domain\n",
  106. np->full_name);
  107. continue;
  108. }
  109. pr_info("PCI/MSI: %s domain created\n", np->full_name);
  110. }
  111. return 0;
  112. }
  113. early_initcall(its_pci_msi_init);