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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013-2015 ARM Limited, All Rights Reserved.
  4. * Author: Marc Zyngier <marc.zyngier@arm.com>
  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. .irq_write_msi_msg = pci_msi_domain_write_msg,
  28. };
  29. static int its_pci_msi_vec_count(struct pci_dev *pdev, void *data)
  30. {
  31. int msi, msix, *count = data;
  32. msi = max(pci_msi_vec_count(pdev), 0);
  33. msix = max(pci_msix_vec_count(pdev), 0);
  34. *count += max(msi, msix);
  35. return 0;
  36. }
  37. static int its_get_pci_alias(struct pci_dev *pdev, u16 alias, void *data)
  38. {
  39. struct pci_dev **alias_dev = data;
  40. *alias_dev = pdev;
  41. return 0;
  42. }
  43. static int its_pci_msi_prepare(struct irq_domain *domain, struct device *dev,
  44. int nvec, msi_alloc_info_t *info)
  45. {
  46. struct pci_dev *pdev, *alias_dev;
  47. struct msi_domain_info *msi_info;
  48. int alias_count = 0, minnvec = 1;
  49. if (!dev_is_pci(dev))
  50. return -EINVAL;
  51. msi_info = msi_get_domain_info(domain->parent);
  52. pdev = to_pci_dev(dev);
  53. /*
  54. * If pdev is downstream of any aliasing bridges, take an upper
  55. * bound of how many other vectors could map to the same DevID.
  56. */
  57. pci_for_each_dma_alias(pdev, its_get_pci_alias, &alias_dev);
  58. if (alias_dev != pdev && alias_dev->subordinate)
  59. pci_walk_bus(alias_dev->subordinate, its_pci_msi_vec_count,
  60. &alias_count);
  61. /* ITS specific DeviceID, as the core ITS ignores dev. */
  62. info->scratchpad[0].ul = pci_msi_domain_get_msi_rid(domain, pdev);
  63. /*
  64. * Always allocate a power of 2, and special case device 0 for
  65. * broken systems where the DevID is not wired (and all devices
  66. * appear as DevID 0). For that reason, we generously allocate a
  67. * minimum of 32 MSIs for DevID 0. If you want more because all
  68. * your devices are aliasing to DevID 0, consider fixing your HW.
  69. */
  70. nvec = max(nvec, alias_count);
  71. if (!info->scratchpad[0].ul)
  72. minnvec = 32;
  73. nvec = max_t(int, minnvec, roundup_pow_of_two(nvec));
  74. return msi_info->ops->msi_prepare(domain->parent, dev, nvec, 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_device_is_available(np))
  111. continue;
  112. if (!of_property_read_bool(np, "msi-controller"))
  113. continue;
  114. if (its_pci_msi_init_one(of_node_to_fwnode(np), np->full_name))
  115. continue;
  116. pr_info("PCI/MSI: %pOF domain created\n", np);
  117. }
  118. return 0;
  119. }
  120. #ifdef CONFIG_ACPI
  121. static int __init
  122. its_pci_msi_parse_madt(union acpi_subtable_headers *header,
  123. const unsigned long end)
  124. {
  125. struct acpi_madt_generic_translator *its_entry;
  126. struct fwnode_handle *dom_handle;
  127. const char *node_name;
  128. int err = -ENXIO;
  129. its_entry = (struct acpi_madt_generic_translator *)header;
  130. node_name = kasprintf(GFP_KERNEL, "ITS@0x%lx",
  131. (long)its_entry->base_address);
  132. dom_handle = iort_find_domain_token(its_entry->translation_id);
  133. if (!dom_handle) {
  134. pr_err("%s: Unable to locate ITS domain handle\n", node_name);
  135. goto out;
  136. }
  137. err = its_pci_msi_init_one(dom_handle, node_name);
  138. if (!err)
  139. pr_info("PCI/MSI: %s domain created\n", node_name);
  140. out:
  141. kfree(node_name);
  142. return err;
  143. }
  144. static int __init its_pci_acpi_msi_init(void)
  145. {
  146. acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
  147. its_pci_msi_parse_madt, 0);
  148. return 0;
  149. }
  150. #else
  151. static int __init its_pci_acpi_msi_init(void)
  152. {
  153. return 0;
  154. }
  155. #endif
  156. static int __init its_pci_msi_init(void)
  157. {
  158. its_pci_of_msi_init();
  159. its_pci_acpi_msi_init();
  160. return 0;
  161. }
  162. early_initcall(its_pci_msi_init);