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

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