irq-gic-v3-its-platform-msi.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/device.h>
  19. #include <linux/msi.h>
  20. #include <linux/of.h>
  21. #include <linux/of_irq.h>
  22. static struct irq_chip its_pmsi_irq_chip = {
  23. .name = "ITS-pMSI",
  24. };
  25. static int of_pmsi_get_dev_id(struct irq_domain *domain, struct device *dev,
  26. u32 *dev_id)
  27. {
  28. int ret, index = 0;
  29. /* Suck the DeviceID out of the msi-parent property */
  30. do {
  31. struct of_phandle_args args;
  32. ret = of_parse_phandle_with_args(dev->of_node,
  33. "msi-parent", "#msi-cells",
  34. index, &args);
  35. if (args.np == irq_domain_get_of_node(domain)) {
  36. if (WARN_ON(args.args_count != 1))
  37. return -EINVAL;
  38. *dev_id = args.args[0];
  39. break;
  40. }
  41. index++;
  42. } while (!ret);
  43. return ret;
  44. }
  45. int __weak iort_pmsi_get_dev_id(struct device *dev, u32 *dev_id)
  46. {
  47. return -1;
  48. }
  49. static int its_pmsi_prepare(struct irq_domain *domain, struct device *dev,
  50. int nvec, msi_alloc_info_t *info)
  51. {
  52. struct msi_domain_info *msi_info;
  53. u32 dev_id;
  54. int ret;
  55. msi_info = msi_get_domain_info(domain->parent);
  56. if (dev->of_node)
  57. ret = of_pmsi_get_dev_id(domain, dev, &dev_id);
  58. else
  59. ret = iort_pmsi_get_dev_id(dev, &dev_id);
  60. if (ret)
  61. return ret;
  62. /* ITS specific DeviceID, as the core ITS ignores dev. */
  63. info->scratchpad[0].ul = dev_id;
  64. /* Allocate at least 32 MSIs, and always as a power of 2 */
  65. nvec = max_t(int, 32, roundup_pow_of_two(nvec));
  66. return msi_info->ops->msi_prepare(domain->parent,
  67. dev, nvec, info);
  68. }
  69. static struct msi_domain_ops its_pmsi_ops = {
  70. .msi_prepare = its_pmsi_prepare,
  71. };
  72. static struct msi_domain_info its_pmsi_domain_info = {
  73. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS),
  74. .ops = &its_pmsi_ops,
  75. .chip = &its_pmsi_irq_chip,
  76. };
  77. static const struct of_device_id its_device_id[] = {
  78. { .compatible = "arm,gic-v3-its", },
  79. {},
  80. };
  81. static int __init its_pmsi_init_one(struct fwnode_handle *fwnode,
  82. const char *name)
  83. {
  84. struct irq_domain *parent;
  85. parent = irq_find_matching_fwnode(fwnode, DOMAIN_BUS_NEXUS);
  86. if (!parent || !msi_get_domain_info(parent)) {
  87. pr_err("%s: unable to locate ITS domain\n", name);
  88. return -ENXIO;
  89. }
  90. if (!platform_msi_create_irq_domain(fwnode, &its_pmsi_domain_info,
  91. parent)) {
  92. pr_err("%s: unable to create platform domain\n", name);
  93. return -ENXIO;
  94. }
  95. pr_info("Platform MSI: %s domain created\n", name);
  96. return 0;
  97. }
  98. #ifdef CONFIG_ACPI
  99. static int __init
  100. its_pmsi_parse_madt(struct acpi_subtable_header *header,
  101. const unsigned long end)
  102. {
  103. struct acpi_madt_generic_translator *its_entry;
  104. struct fwnode_handle *domain_handle;
  105. const char *node_name;
  106. int err = -ENXIO;
  107. its_entry = (struct acpi_madt_generic_translator *)header;
  108. node_name = kasprintf(GFP_KERNEL, "ITS@0x%lx",
  109. (long)its_entry->base_address);
  110. domain_handle = iort_find_domain_token(its_entry->translation_id);
  111. if (!domain_handle) {
  112. pr_err("%s: Unable to locate ITS domain handle\n", node_name);
  113. goto out;
  114. }
  115. err = its_pmsi_init_one(domain_handle, node_name);
  116. out:
  117. kfree(node_name);
  118. return err;
  119. }
  120. static void __init its_pmsi_acpi_init(void)
  121. {
  122. acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
  123. its_pmsi_parse_madt, 0);
  124. }
  125. #else
  126. static inline void its_pmsi_acpi_init(void) { }
  127. #endif
  128. static void __init its_pmsi_of_init(void)
  129. {
  130. struct device_node *np;
  131. for (np = of_find_matching_node(NULL, its_device_id); np;
  132. np = of_find_matching_node(np, its_device_id)) {
  133. if (!of_device_is_available(np))
  134. continue;
  135. if (!of_property_read_bool(np, "msi-controller"))
  136. continue;
  137. its_pmsi_init_one(of_node_to_fwnode(np), np->full_name);
  138. }
  139. }
  140. static int __init its_pmsi_init(void)
  141. {
  142. its_pmsi_of_init();
  143. its_pmsi_acpi_init();
  144. return 0;
  145. }
  146. early_initcall(its_pmsi_init);