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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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, minnvec = 1;
  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. /*
  74. * Always allocate a power of 2, and special case device 0 for
  75. * broken systems where the DevID is not wired (and all devices
  76. * appear as DevID 0). For that reason, we generously allocate a
  77. * minimum of 32 MSIs for DevID 0. If you want more because all
  78. * your devices are aliasing to DevID 0, consider fixing your HW.
  79. */
  80. nvec = max(nvec, alias_count);
  81. if (!info->scratchpad[0].ul)
  82. minnvec = 32;
  83. nvec = max_t(int, minnvec, roundup_pow_of_two(nvec));
  84. return msi_info->ops->msi_prepare(domain->parent, dev, nvec, info);
  85. }
  86. static struct msi_domain_ops its_pci_msi_ops = {
  87. .msi_prepare = its_pci_msi_prepare,
  88. };
  89. static struct msi_domain_info its_pci_msi_domain_info = {
  90. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  91. MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX),
  92. .ops = &its_pci_msi_ops,
  93. .chip = &its_msi_irq_chip,
  94. };
  95. static struct of_device_id its_device_id[] = {
  96. { .compatible = "arm,gic-v3-its", },
  97. {},
  98. };
  99. static int __init its_pci_msi_init_one(struct fwnode_handle *handle,
  100. const char *name)
  101. {
  102. struct irq_domain *parent;
  103. parent = irq_find_matching_fwnode(handle, DOMAIN_BUS_NEXUS);
  104. if (!parent || !msi_get_domain_info(parent)) {
  105. pr_err("%s: Unable to locate ITS domain\n", name);
  106. return -ENXIO;
  107. }
  108. if (!pci_msi_create_irq_domain(handle, &its_pci_msi_domain_info,
  109. parent)) {
  110. pr_err("%s: Unable to create PCI domain\n", name);
  111. return -ENOMEM;
  112. }
  113. return 0;
  114. }
  115. static int __init its_pci_of_msi_init(void)
  116. {
  117. struct device_node *np;
  118. for (np = of_find_matching_node(NULL, its_device_id); np;
  119. np = of_find_matching_node(np, its_device_id)) {
  120. if (!of_device_is_available(np))
  121. continue;
  122. if (!of_property_read_bool(np, "msi-controller"))
  123. continue;
  124. if (its_pci_msi_init_one(of_node_to_fwnode(np), np->full_name))
  125. continue;
  126. pr_info("PCI/MSI: %pOF domain created\n", np);
  127. }
  128. return 0;
  129. }
  130. #ifdef CONFIG_ACPI
  131. static int __init
  132. its_pci_msi_parse_madt(struct acpi_subtable_header *header,
  133. const unsigned long end)
  134. {
  135. struct acpi_madt_generic_translator *its_entry;
  136. struct fwnode_handle *dom_handle;
  137. const char *node_name;
  138. int err = -ENXIO;
  139. its_entry = (struct acpi_madt_generic_translator *)header;
  140. node_name = kasprintf(GFP_KERNEL, "ITS@0x%lx",
  141. (long)its_entry->base_address);
  142. dom_handle = iort_find_domain_token(its_entry->translation_id);
  143. if (!dom_handle) {
  144. pr_err("%s: Unable to locate ITS domain handle\n", node_name);
  145. goto out;
  146. }
  147. err = its_pci_msi_init_one(dom_handle, node_name);
  148. if (!err)
  149. pr_info("PCI/MSI: %s domain created\n", node_name);
  150. out:
  151. kfree(node_name);
  152. return err;
  153. }
  154. static int __init its_pci_acpi_msi_init(void)
  155. {
  156. acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
  157. its_pci_msi_parse_madt, 0);
  158. return 0;
  159. }
  160. #else
  161. static int __init its_pci_acpi_msi_init(void)
  162. {
  163. return 0;
  164. }
  165. #endif
  166. static int __init its_pci_msi_init(void)
  167. {
  168. its_pci_of_msi_init();
  169. its_pci_acpi_msi_init();
  170. return 0;
  171. }
  172. early_initcall(its_pci_msi_init);