irq-gic-v3-mbi.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 ARM Limited, All Rights Reserved.
  4. * Author: Marc Zyngier <marc.zyngier@arm.com>
  5. */
  6. #define pr_fmt(fmt) "GICv3: " fmt
  7. #include <linux/dma-iommu.h>
  8. #include <linux/irq.h>
  9. #include <linux/irqdomain.h>
  10. #include <linux/kernel.h>
  11. #include <linux/msi.h>
  12. #include <linux/of_address.h>
  13. #include <linux/of_pci.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/irqchip/arm-gic-v3.h>
  17. struct mbi_range {
  18. u32 spi_start;
  19. u32 nr_spis;
  20. unsigned long *bm;
  21. };
  22. static DEFINE_MUTEX(mbi_lock);
  23. static phys_addr_t mbi_phys_base;
  24. static struct mbi_range *mbi_ranges;
  25. static unsigned int mbi_range_nr;
  26. static struct irq_chip mbi_irq_chip = {
  27. .name = "MBI",
  28. .irq_mask = irq_chip_mask_parent,
  29. .irq_unmask = irq_chip_unmask_parent,
  30. .irq_eoi = irq_chip_eoi_parent,
  31. .irq_set_type = irq_chip_set_type_parent,
  32. .irq_set_affinity = irq_chip_set_affinity_parent,
  33. };
  34. static int mbi_irq_gic_domain_alloc(struct irq_domain *domain,
  35. unsigned int virq,
  36. irq_hw_number_t hwirq)
  37. {
  38. struct irq_fwspec fwspec;
  39. struct irq_data *d;
  40. int err;
  41. /*
  42. * Using ACPI? There is no MBI support in the spec, you
  43. * shouldn't even be here.
  44. */
  45. if (!is_of_node(domain->parent->fwnode))
  46. return -EINVAL;
  47. /*
  48. * Let's default to edge. This is consistent with traditional
  49. * MSIs, and systems requiring level signaling will just
  50. * enforce the trigger on their own.
  51. */
  52. fwspec.fwnode = domain->parent->fwnode;
  53. fwspec.param_count = 3;
  54. fwspec.param[0] = 0;
  55. fwspec.param[1] = hwirq - 32;
  56. fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
  57. err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  58. if (err)
  59. return err;
  60. d = irq_domain_get_irq_data(domain->parent, virq);
  61. return d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
  62. }
  63. static void mbi_free_msi(struct mbi_range *mbi, unsigned int hwirq,
  64. int nr_irqs)
  65. {
  66. mutex_lock(&mbi_lock);
  67. bitmap_release_region(mbi->bm, hwirq - mbi->spi_start,
  68. get_count_order(nr_irqs));
  69. mutex_unlock(&mbi_lock);
  70. }
  71. static int mbi_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  72. unsigned int nr_irqs, void *args)
  73. {
  74. struct mbi_range *mbi = NULL;
  75. int hwirq, offset, i, err = 0;
  76. mutex_lock(&mbi_lock);
  77. for (i = 0; i < mbi_range_nr; i++) {
  78. offset = bitmap_find_free_region(mbi_ranges[i].bm,
  79. mbi_ranges[i].nr_spis,
  80. get_count_order(nr_irqs));
  81. if (offset >= 0) {
  82. mbi = &mbi_ranges[i];
  83. break;
  84. }
  85. }
  86. mutex_unlock(&mbi_lock);
  87. if (!mbi)
  88. return -ENOSPC;
  89. hwirq = mbi->spi_start + offset;
  90. for (i = 0; i < nr_irqs; i++) {
  91. err = mbi_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
  92. if (err)
  93. goto fail;
  94. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  95. &mbi_irq_chip, mbi);
  96. }
  97. return 0;
  98. fail:
  99. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  100. mbi_free_msi(mbi, hwirq, nr_irqs);
  101. return err;
  102. }
  103. static void mbi_irq_domain_free(struct irq_domain *domain,
  104. unsigned int virq, unsigned int nr_irqs)
  105. {
  106. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  107. struct mbi_range *mbi = irq_data_get_irq_chip_data(d);
  108. mbi_free_msi(mbi, d->hwirq, nr_irqs);
  109. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  110. }
  111. static const struct irq_domain_ops mbi_domain_ops = {
  112. .alloc = mbi_irq_domain_alloc,
  113. .free = mbi_irq_domain_free,
  114. };
  115. static void mbi_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  116. {
  117. msg[0].address_hi = upper_32_bits(mbi_phys_base + GICD_SETSPI_NSR);
  118. msg[0].address_lo = lower_32_bits(mbi_phys_base + GICD_SETSPI_NSR);
  119. msg[0].data = data->parent_data->hwirq;
  120. iommu_dma_map_msi_msg(data->irq, msg);
  121. }
  122. #ifdef CONFIG_PCI_MSI
  123. /* PCI-specific irqchip */
  124. static void mbi_mask_msi_irq(struct irq_data *d)
  125. {
  126. pci_msi_mask_irq(d);
  127. irq_chip_mask_parent(d);
  128. }
  129. static void mbi_unmask_msi_irq(struct irq_data *d)
  130. {
  131. pci_msi_unmask_irq(d);
  132. irq_chip_unmask_parent(d);
  133. }
  134. static struct irq_chip mbi_msi_irq_chip = {
  135. .name = "MSI",
  136. .irq_mask = mbi_mask_msi_irq,
  137. .irq_unmask = mbi_unmask_msi_irq,
  138. .irq_eoi = irq_chip_eoi_parent,
  139. .irq_compose_msi_msg = mbi_compose_msi_msg,
  140. .irq_write_msi_msg = pci_msi_domain_write_msg,
  141. };
  142. static struct msi_domain_info mbi_msi_domain_info = {
  143. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  144. MSI_FLAG_PCI_MSIX | MSI_FLAG_MULTI_PCI_MSI),
  145. .chip = &mbi_msi_irq_chip,
  146. };
  147. static int mbi_allocate_pci_domain(struct irq_domain *nexus_domain,
  148. struct irq_domain **pci_domain)
  149. {
  150. *pci_domain = pci_msi_create_irq_domain(nexus_domain->parent->fwnode,
  151. &mbi_msi_domain_info,
  152. nexus_domain);
  153. if (!*pci_domain)
  154. return -ENOMEM;
  155. return 0;
  156. }
  157. #else
  158. static int mbi_allocate_pci_domain(struct irq_domain *nexus_domain,
  159. struct irq_domain **pci_domain)
  160. {
  161. *pci_domain = NULL;
  162. return 0;
  163. }
  164. #endif
  165. static void mbi_compose_mbi_msg(struct irq_data *data, struct msi_msg *msg)
  166. {
  167. mbi_compose_msi_msg(data, msg);
  168. msg[1].address_hi = upper_32_bits(mbi_phys_base + GICD_CLRSPI_NSR);
  169. msg[1].address_lo = lower_32_bits(mbi_phys_base + GICD_CLRSPI_NSR);
  170. msg[1].data = data->parent_data->hwirq;
  171. iommu_dma_map_msi_msg(data->irq, &msg[1]);
  172. }
  173. /* Platform-MSI specific irqchip */
  174. static struct irq_chip mbi_pmsi_irq_chip = {
  175. .name = "pMSI",
  176. .irq_set_type = irq_chip_set_type_parent,
  177. .irq_compose_msi_msg = mbi_compose_mbi_msg,
  178. .flags = IRQCHIP_SUPPORTS_LEVEL_MSI,
  179. };
  180. static struct msi_domain_ops mbi_pmsi_ops = {
  181. };
  182. static struct msi_domain_info mbi_pmsi_domain_info = {
  183. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  184. MSI_FLAG_LEVEL_CAPABLE),
  185. .ops = &mbi_pmsi_ops,
  186. .chip = &mbi_pmsi_irq_chip,
  187. };
  188. static int mbi_allocate_domains(struct irq_domain *parent)
  189. {
  190. struct irq_domain *nexus_domain, *pci_domain, *plat_domain;
  191. int err;
  192. nexus_domain = irq_domain_create_tree(parent->fwnode,
  193. &mbi_domain_ops, NULL);
  194. if (!nexus_domain)
  195. return -ENOMEM;
  196. irq_domain_update_bus_token(nexus_domain, DOMAIN_BUS_NEXUS);
  197. nexus_domain->parent = parent;
  198. err = mbi_allocate_pci_domain(nexus_domain, &pci_domain);
  199. plat_domain = platform_msi_create_irq_domain(parent->fwnode,
  200. &mbi_pmsi_domain_info,
  201. nexus_domain);
  202. if (err || !plat_domain) {
  203. if (plat_domain)
  204. irq_domain_remove(plat_domain);
  205. if (pci_domain)
  206. irq_domain_remove(pci_domain);
  207. irq_domain_remove(nexus_domain);
  208. return -ENOMEM;
  209. }
  210. return 0;
  211. }
  212. int __init mbi_init(struct fwnode_handle *fwnode, struct irq_domain *parent)
  213. {
  214. struct device_node *np;
  215. const __be32 *reg;
  216. int ret, n;
  217. np = to_of_node(fwnode);
  218. if (!of_property_read_bool(np, "msi-controller"))
  219. return 0;
  220. n = of_property_count_elems_of_size(np, "mbi-ranges", sizeof(u32));
  221. if (n <= 0 || n % 2)
  222. return -EINVAL;
  223. mbi_range_nr = n / 2;
  224. mbi_ranges = kcalloc(mbi_range_nr, sizeof(*mbi_ranges), GFP_KERNEL);
  225. if (!mbi_ranges)
  226. return -ENOMEM;
  227. for (n = 0; n < mbi_range_nr; n++) {
  228. ret = of_property_read_u32_index(np, "mbi-ranges", n * 2,
  229. &mbi_ranges[n].spi_start);
  230. if (ret)
  231. goto err_free_mbi;
  232. ret = of_property_read_u32_index(np, "mbi-ranges", n * 2 + 1,
  233. &mbi_ranges[n].nr_spis);
  234. if (ret)
  235. goto err_free_mbi;
  236. mbi_ranges[n].bm = kcalloc(BITS_TO_LONGS(mbi_ranges[n].nr_spis),
  237. sizeof(long), GFP_KERNEL);
  238. if (!mbi_ranges[n].bm) {
  239. ret = -ENOMEM;
  240. goto err_free_mbi;
  241. }
  242. pr_info("MBI range [%d:%d]\n", mbi_ranges[n].spi_start,
  243. mbi_ranges[n].spi_start + mbi_ranges[n].nr_spis - 1);
  244. }
  245. reg = of_get_property(np, "mbi-alias", NULL);
  246. if (reg) {
  247. mbi_phys_base = of_translate_address(np, reg);
  248. if (mbi_phys_base == (phys_addr_t)OF_BAD_ADDR) {
  249. ret = -ENXIO;
  250. goto err_free_mbi;
  251. }
  252. } else {
  253. struct resource res;
  254. if (of_address_to_resource(np, 0, &res)) {
  255. ret = -ENXIO;
  256. goto err_free_mbi;
  257. }
  258. mbi_phys_base = res.start;
  259. }
  260. pr_info("Using MBI frame %pa\n", &mbi_phys_base);
  261. ret = mbi_allocate_domains(parent);
  262. if (ret)
  263. goto err_free_mbi;
  264. return 0;
  265. err_free_mbi:
  266. if (mbi_ranges) {
  267. for (n = 0; n < mbi_range_nr; n++)
  268. kfree(mbi_ranges[n].bm);
  269. kfree(mbi_ranges);
  270. }
  271. return ret;
  272. }