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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013-2015 ARM Limited, All Rights Reserved.
  4. * Author: Marc Zyngier <[email protected]>
  5. */
  6. #include <linux/acpi_iort.h>
  7. #include <linux/pci.h>
  8. #include <linux/msi.h>
  9. #include <linux/of.h>
  10. #include <linux/of_irq.h>
  11. #include <linux/of_pci.h>
  12. static void its_mask_msi_irq(struct irq_data *d)
  13. {
  14. pci_msi_mask_irq(d);
  15. irq_chip_mask_parent(d);
  16. }
  17. static void its_unmask_msi_irq(struct irq_data *d)
  18. {
  19. pci_msi_unmask_irq(d);
  20. irq_chip_unmask_parent(d);
  21. }
  22. static struct irq_chip its_msi_irq_chip = {
  23. .name = "ITS-MSI",
  24. .irq_unmask = its_unmask_msi_irq,
  25. .irq_mask = its_mask_msi_irq,
  26. .irq_eoi = irq_chip_eoi_parent,
  27. };
  28. static int its_pci_msi_vec_count(struct pci_dev *pdev, void *data)
  29. {
  30. int msi, msix, *count = data;
  31. msi = max(pci_msi_vec_count(pdev), 0);
  32. msix = max(pci_msix_vec_count(pdev), 0);
  33. *count += max(msi, msix);
  34. return 0;
  35. }
  36. static int its_get_pci_alias(struct pci_dev *pdev, u16 alias, void *data)
  37. {
  38. struct pci_dev **alias_dev = data;
  39. *alias_dev = pdev;
  40. return 0;
  41. }
  42. static int its_pci_msi_prepare(struct irq_domain *domain, struct device *dev,
  43. int nvec, msi_alloc_info_t *info)
  44. {
  45. struct pci_dev *pdev, *alias_dev;
  46. struct msi_domain_info *msi_info;
  47. int alias_count = 0, minnvec = 1;
  48. if (!dev_is_pci(dev))
  49. return -EINVAL;
  50. msi_info = msi_get_domain_info(domain->parent);
  51. pdev = to_pci_dev(dev);
  52. /*
  53. * If pdev is downstream of any aliasing bridges, take an upper
  54. * bound of how many other vectors could map to the same DevID.
  55. * Also tell the ITS that the signalling will come from a proxy
  56. * device, and that special allocation rules apply.
  57. */
  58. pci_for_each_dma_alias(pdev, its_get_pci_alias, &alias_dev);
  59. if (alias_dev != pdev) {
  60. if (alias_dev->subordinate)
  61. pci_walk_bus(alias_dev->subordinate,
  62. its_pci_msi_vec_count, &alias_count);
  63. info->flags |= MSI_ALLOC_FLAGS_PROXY_DEVICE;
  64. }
  65. /* ITS specific DeviceID, as the core ITS ignores dev. */
  66. info->scratchpad[0].ul = pci_msi_domain_get_msi_rid(domain, pdev);
  67. /*
  68. * Always allocate a power of 2, and special case device 0 for
  69. * broken systems where the DevID is not wired (and all devices
  70. * appear as DevID 0). For that reason, we generously allocate a
  71. * minimum of 32 MSIs for DevID 0. If you want more because all
  72. * your devices are aliasing to DevID 0, consider fixing your HW.
  73. */
  74. nvec = max(nvec, alias_count);
  75. if (!info->scratchpad[0].ul)
  76. minnvec = 32;
  77. nvec = max_t(int, minnvec, roundup_pow_of_two(nvec));
  78. return msi_info->ops->msi_prepare(domain->parent, dev, nvec, info);
  79. }
  80. static struct msi_domain_ops its_pci_msi_ops = {
  81. .msi_prepare = its_pci_msi_prepare,
  82. };
  83. static struct msi_domain_info its_pci_msi_domain_info = {
  84. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  85. MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX),
  86. .ops = &its_pci_msi_ops,
  87. .chip = &its_msi_irq_chip,
  88. };
  89. static struct of_device_id its_device_id[] = {
  90. { .compatible = "arm,gic-v3-its", },
  91. {},
  92. };
  93. static int __init its_pci_msi_init_one(struct fwnode_handle *handle,
  94. const char *name)
  95. {
  96. struct irq_domain *parent;
  97. parent = irq_find_matching_fwnode(handle, DOMAIN_BUS_NEXUS);
  98. if (!parent || !msi_get_domain_info(parent)) {
  99. pr_err("%s: Unable to locate ITS domain\n", name);
  100. return -ENXIO;
  101. }
  102. if (!pci_msi_create_irq_domain(handle, &its_pci_msi_domain_info,
  103. parent)) {
  104. pr_err("%s: Unable to create PCI domain\n", name);
  105. return -ENOMEM;
  106. }
  107. return 0;
  108. }
  109. static int __init its_pci_of_msi_init(void)
  110. {
  111. struct device_node *np;
  112. for (np = of_find_matching_node(NULL, its_device_id); np;
  113. np = of_find_matching_node(np, its_device_id)) {
  114. if (!of_device_is_available(np))
  115. continue;
  116. if (!of_property_read_bool(np, "msi-controller"))
  117. continue;
  118. if (its_pci_msi_init_one(of_node_to_fwnode(np), np->full_name))
  119. continue;
  120. pr_info("PCI/MSI: %pOF domain created\n", np);
  121. }
  122. return 0;
  123. }
  124. #ifdef CONFIG_ACPI
  125. static int __init
  126. its_pci_msi_parse_madt(union acpi_subtable_headers *header,
  127. const unsigned long end)
  128. {
  129. struct acpi_madt_generic_translator *its_entry;
  130. struct fwnode_handle *dom_handle;
  131. const char *node_name;
  132. int err = -ENXIO;
  133. its_entry = (struct acpi_madt_generic_translator *)header;
  134. node_name = kasprintf(GFP_KERNEL, "ITS@0x%lx",
  135. (long)its_entry->base_address);
  136. dom_handle = iort_find_domain_token(its_entry->translation_id);
  137. if (!dom_handle) {
  138. pr_err("%s: Unable to locate ITS domain handle\n", node_name);
  139. goto out;
  140. }
  141. err = its_pci_msi_init_one(dom_handle, node_name);
  142. if (!err)
  143. pr_info("PCI/MSI: %s domain created\n", node_name);
  144. out:
  145. kfree(node_name);
  146. return err;
  147. }
  148. static int __init its_pci_acpi_msi_init(void)
  149. {
  150. acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
  151. its_pci_msi_parse_madt, 0);
  152. return 0;
  153. }
  154. #else
  155. static int __init its_pci_acpi_msi_init(void)
  156. {
  157. return 0;
  158. }
  159. #endif
  160. static int __init its_pci_msi_init(void)
  161. {
  162. its_pci_of_msi_init();
  163. its_pci_acpi_msi_init();
  164. return 0;
  165. }
  166. early_initcall(its_pci_msi_init);