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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. struct its_pci_alias {
  40. struct pci_dev *pdev;
  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. if (pdev != dev_alias->pdev)
  54. dev_alias->count += its_pci_msi_vec_count(pdev);
  55. return 0;
  56. }
  57. static int its_pci_msi_prepare(struct irq_domain *domain, struct device *dev,
  58. int nvec, msi_alloc_info_t *info)
  59. {
  60. struct pci_dev *pdev;
  61. struct its_pci_alias dev_alias;
  62. struct msi_domain_info *msi_info;
  63. if (!dev_is_pci(dev))
  64. return -EINVAL;
  65. msi_info = msi_get_domain_info(domain->parent);
  66. pdev = to_pci_dev(dev);
  67. dev_alias.pdev = pdev;
  68. dev_alias.count = nvec;
  69. pci_for_each_dma_alias(pdev, its_get_pci_alias, &dev_alias);
  70. /* ITS specific DeviceID, as the core ITS ignores dev. */
  71. info->scratchpad[0].ul = pci_msi_domain_get_msi_rid(domain, pdev);
  72. return msi_info->ops->msi_prepare(domain->parent,
  73. dev, dev_alias.count, info);
  74. }
  75. static struct msi_domain_ops its_pci_msi_ops = {
  76. .msi_prepare = its_pci_msi_prepare,
  77. };
  78. static struct msi_domain_info its_pci_msi_domain_info = {
  79. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  80. MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX),
  81. .ops = &its_pci_msi_ops,
  82. .chip = &its_msi_irq_chip,
  83. };
  84. static struct of_device_id its_device_id[] = {
  85. { .compatible = "arm,gic-v3-its", },
  86. {},
  87. };
  88. static int __init its_pci_msi_init_one(struct fwnode_handle *handle,
  89. const char *name)
  90. {
  91. struct irq_domain *parent;
  92. parent = irq_find_matching_fwnode(handle, DOMAIN_BUS_NEXUS);
  93. if (!parent || !msi_get_domain_info(parent)) {
  94. pr_err("%s: Unable to locate ITS domain\n", name);
  95. return -ENXIO;
  96. }
  97. if (!pci_msi_create_irq_domain(handle, &its_pci_msi_domain_info,
  98. parent)) {
  99. pr_err("%s: Unable to create PCI domain\n", name);
  100. return -ENOMEM;
  101. }
  102. return 0;
  103. }
  104. static int __init its_pci_of_msi_init(void)
  105. {
  106. struct device_node *np;
  107. for (np = of_find_matching_node(NULL, its_device_id); np;
  108. np = of_find_matching_node(np, its_device_id)) {
  109. if (!of_device_is_available(np))
  110. continue;
  111. if (!of_property_read_bool(np, "msi-controller"))
  112. continue;
  113. if (its_pci_msi_init_one(of_node_to_fwnode(np), np->full_name))
  114. continue;
  115. pr_info("PCI/MSI: %s domain created\n", np->full_name);
  116. }
  117. return 0;
  118. }
  119. #ifdef CONFIG_ACPI
  120. static int __init
  121. its_pci_msi_parse_madt(struct acpi_subtable_header *header,
  122. const unsigned long end)
  123. {
  124. struct acpi_madt_generic_translator *its_entry;
  125. struct fwnode_handle *dom_handle;
  126. const char *node_name;
  127. int err = -ENXIO;
  128. its_entry = (struct acpi_madt_generic_translator *)header;
  129. node_name = kasprintf(GFP_KERNEL, "ITS@0x%lx",
  130. (long)its_entry->base_address);
  131. dom_handle = iort_find_domain_token(its_entry->translation_id);
  132. if (!dom_handle) {
  133. pr_err("%s: Unable to locate ITS domain handle\n", node_name);
  134. goto out;
  135. }
  136. err = its_pci_msi_init_one(dom_handle, node_name);
  137. if (!err)
  138. pr_info("PCI/MSI: %s domain created\n", node_name);
  139. out:
  140. kfree(node_name);
  141. return err;
  142. }
  143. static int __init its_pci_acpi_msi_init(void)
  144. {
  145. acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
  146. its_pci_msi_parse_madt, 0);
  147. return 0;
  148. }
  149. #else
  150. static int __init its_pci_acpi_msi_init(void)
  151. {
  152. return 0;
  153. }
  154. #endif
  155. static int __init its_pci_msi_init(void)
  156. {
  157. its_pci_of_msi_init();
  158. its_pci_acpi_msi_init();
  159. return 0;
  160. }
  161. early_initcall(its_pci_msi_init);