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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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/acpi_iort.h>
  18. #include <linux/msi.h>
  19. #include <linux/of.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_pci.h>
  22. static void its_mask_msi_irq(struct irq_data *d)
  23. {
  24. pci_msi_mask_irq(d);
  25. irq_chip_mask_parent(d);
  26. }
  27. static void its_unmask_msi_irq(struct irq_data *d)
  28. {
  29. pci_msi_unmask_irq(d);
  30. irq_chip_unmask_parent(d);
  31. }
  32. static struct irq_chip its_msi_irq_chip = {
  33. .name = "ITS-MSI",
  34. .irq_unmask = its_unmask_msi_irq,
  35. .irq_mask = its_mask_msi_irq,
  36. .irq_eoi = irq_chip_eoi_parent,
  37. .irq_write_msi_msg = pci_msi_domain_write_msg,
  38. };
  39. static int its_pci_msi_vec_count(struct pci_dev *pdev, void *data)
  40. {
  41. int msi, msix, *count = data;
  42. msi = max(pci_msi_vec_count(pdev), 0);
  43. msix = max(pci_msix_vec_count(pdev), 0);
  44. *count += max(msi, msix);
  45. return 0;
  46. }
  47. static int its_get_pci_alias(struct pci_dev *pdev, u16 alias, void *data)
  48. {
  49. struct pci_dev **alias_dev = data;
  50. *alias_dev = pdev;
  51. return 0;
  52. }
  53. static int its_pci_msi_prepare(struct irq_domain *domain, struct device *dev,
  54. int nvec, msi_alloc_info_t *info)
  55. {
  56. struct pci_dev *pdev, *alias_dev;
  57. struct msi_domain_info *msi_info;
  58. int alias_count = 0;
  59. if (!dev_is_pci(dev))
  60. return -EINVAL;
  61. msi_info = msi_get_domain_info(domain->parent);
  62. pdev = to_pci_dev(dev);
  63. /*
  64. * If pdev is downstream of any aliasing bridges, take an upper
  65. * bound of how many other vectors could map to the same DevID.
  66. */
  67. pci_for_each_dma_alias(pdev, its_get_pci_alias, &alias_dev);
  68. if (alias_dev != pdev && alias_dev->subordinate)
  69. pci_walk_bus(alias_dev->subordinate, its_pci_msi_vec_count,
  70. &alias_count);
  71. /* ITS specific DeviceID, as the core ITS ignores dev. */
  72. info->scratchpad[0].ul = pci_msi_domain_get_msi_rid(domain, pdev);
  73. return msi_info->ops->msi_prepare(domain->parent,
  74. dev, max(nvec, 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_one(struct fwnode_handle *handle,
  90. const char *name)
  91. {
  92. struct irq_domain *parent;
  93. parent = irq_find_matching_fwnode(handle, DOMAIN_BUS_NEXUS);
  94. if (!parent || !msi_get_domain_info(parent)) {
  95. pr_err("%s: Unable to locate ITS domain\n", name);
  96. return -ENXIO;
  97. }
  98. if (!pci_msi_create_irq_domain(handle, &its_pci_msi_domain_info,
  99. parent)) {
  100. pr_err("%s: Unable to create PCI domain\n", name);
  101. return -ENOMEM;
  102. }
  103. return 0;
  104. }
  105. static int __init its_pci_of_msi_init(void)
  106. {
  107. struct device_node *np;
  108. for (np = of_find_matching_node(NULL, its_device_id); np;
  109. np = of_find_matching_node(np, its_device_id)) {
  110. if (!of_property_read_bool(np, "msi-controller"))
  111. continue;
  112. if (its_pci_msi_init_one(of_node_to_fwnode(np), np->full_name))
  113. continue;
  114. pr_info("PCI/MSI: %pOF domain created\n", np);
  115. }
  116. return 0;
  117. }
  118. #ifdef CONFIG_ACPI
  119. static int __init
  120. its_pci_msi_parse_madt(struct acpi_subtable_header *header,
  121. const unsigned long end)
  122. {
  123. struct acpi_madt_generic_translator *its_entry;
  124. struct fwnode_handle *dom_handle;
  125. const char *node_name;
  126. int err = -ENXIO;
  127. its_entry = (struct acpi_madt_generic_translator *)header;
  128. node_name = kasprintf(GFP_KERNEL, "ITS@0x%lx",
  129. (long)its_entry->base_address);
  130. dom_handle = iort_find_domain_token(its_entry->translation_id);
  131. if (!dom_handle) {
  132. pr_err("%s: Unable to locate ITS domain handle\n", node_name);
  133. goto out;
  134. }
  135. err = its_pci_msi_init_one(dom_handle, node_name);
  136. if (!err)
  137. pr_info("PCI/MSI: %s domain created\n", node_name);
  138. out:
  139. kfree(node_name);
  140. return err;
  141. }
  142. static int __init its_pci_acpi_msi_init(void)
  143. {
  144. acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
  145. its_pci_msi_parse_madt, 0);
  146. return 0;
  147. }
  148. #else
  149. static int __init its_pci_acpi_msi_init(void)
  150. {
  151. return 0;
  152. }
  153. #endif
  154. static int __init its_pci_msi_init(void)
  155. {
  156. its_pci_of_msi_init();
  157. its_pci_acpi_msi_init();
  158. return 0;
  159. }
  160. early_initcall(its_pci_msi_init);