irq-gic-v3-mbi.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 ARM Limited, All Rights Reserved.
  4. * Author: Marc Zyngier <[email protected]>
  5. */
  6. #define pr_fmt(fmt) "GICv3: " fmt
  7. #include <linux/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. msi_alloc_info_t *info = args;
  75. struct mbi_range *mbi = NULL;
  76. int hwirq, offset, i, err = 0;
  77. mutex_lock(&mbi_lock);
  78. for (i = 0; i < mbi_range_nr; i++) {
  79. offset = bitmap_find_free_region(mbi_ranges[i].bm,
  80. mbi_ranges[i].nr_spis,
  81. get_count_order(nr_irqs));
  82. if (offset >= 0) {
  83. mbi = &mbi_ranges[i];
  84. break;
  85. }
  86. }
  87. mutex_unlock(&mbi_lock);
  88. if (!mbi)
  89. return -ENOSPC;
  90. hwirq = mbi->spi_start + offset;
  91. err = iommu_dma_prepare_msi(info->desc,
  92. mbi_phys_base + GICD_SETSPI_NSR);
  93. if (err)
  94. return err;
  95. for (i = 0; i < nr_irqs; i++) {
  96. err = mbi_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
  97. if (err)
  98. goto fail;
  99. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  100. &mbi_irq_chip, mbi);
  101. }
  102. return 0;
  103. fail:
  104. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  105. mbi_free_msi(mbi, hwirq, nr_irqs);
  106. return err;
  107. }
  108. static void mbi_irq_domain_free(struct irq_domain *domain,
  109. unsigned int virq, unsigned int nr_irqs)
  110. {
  111. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  112. struct mbi_range *mbi = irq_data_get_irq_chip_data(d);
  113. mbi_free_msi(mbi, d->hwirq, nr_irqs);
  114. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  115. }
  116. static const struct irq_domain_ops mbi_domain_ops = {
  117. .alloc = mbi_irq_domain_alloc,
  118. .free = mbi_irq_domain_free,
  119. };
  120. static void mbi_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  121. {
  122. msg[0].address_hi = upper_32_bits(mbi_phys_base + GICD_SETSPI_NSR);
  123. msg[0].address_lo = lower_32_bits(mbi_phys_base + GICD_SETSPI_NSR);
  124. msg[0].data = data->parent_data->hwirq;
  125. iommu_dma_compose_msi_msg(irq_data_get_msi_desc(data), msg);
  126. }
  127. #ifdef CONFIG_PCI_MSI
  128. /* PCI-specific irqchip */
  129. static void mbi_mask_msi_irq(struct irq_data *d)
  130. {
  131. pci_msi_mask_irq(d);
  132. irq_chip_mask_parent(d);
  133. }
  134. static void mbi_unmask_msi_irq(struct irq_data *d)
  135. {
  136. pci_msi_unmask_irq(d);
  137. irq_chip_unmask_parent(d);
  138. }
  139. static struct irq_chip mbi_msi_irq_chip = {
  140. .name = "MSI",
  141. .irq_mask = mbi_mask_msi_irq,
  142. .irq_unmask = mbi_unmask_msi_irq,
  143. .irq_eoi = irq_chip_eoi_parent,
  144. .irq_compose_msi_msg = mbi_compose_msi_msg,
  145. };
  146. static struct msi_domain_info mbi_msi_domain_info = {
  147. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  148. MSI_FLAG_PCI_MSIX | MSI_FLAG_MULTI_PCI_MSI),
  149. .chip = &mbi_msi_irq_chip,
  150. };
  151. static int mbi_allocate_pci_domain(struct irq_domain *nexus_domain,
  152. struct irq_domain **pci_domain)
  153. {
  154. *pci_domain = pci_msi_create_irq_domain(nexus_domain->parent->fwnode,
  155. &mbi_msi_domain_info,
  156. nexus_domain);
  157. if (!*pci_domain)
  158. return -ENOMEM;
  159. return 0;
  160. }
  161. #else
  162. static int mbi_allocate_pci_domain(struct irq_domain *nexus_domain,
  163. struct irq_domain **pci_domain)
  164. {
  165. *pci_domain = NULL;
  166. return 0;
  167. }
  168. #endif
  169. static void mbi_compose_mbi_msg(struct irq_data *data, struct msi_msg *msg)
  170. {
  171. mbi_compose_msi_msg(data, msg);
  172. msg[1].address_hi = upper_32_bits(mbi_phys_base + GICD_CLRSPI_NSR);
  173. msg[1].address_lo = lower_32_bits(mbi_phys_base + GICD_CLRSPI_NSR);
  174. msg[1].data = data->parent_data->hwirq;
  175. iommu_dma_compose_msi_msg(irq_data_get_msi_desc(data), &msg[1]);
  176. }
  177. /* Platform-MSI specific irqchip */
  178. static struct irq_chip mbi_pmsi_irq_chip = {
  179. .name = "pMSI",
  180. .irq_set_type = irq_chip_set_type_parent,
  181. .irq_compose_msi_msg = mbi_compose_mbi_msg,
  182. .flags = IRQCHIP_SUPPORTS_LEVEL_MSI,
  183. };
  184. static struct msi_domain_ops mbi_pmsi_ops = {
  185. };
  186. static struct msi_domain_info mbi_pmsi_domain_info = {
  187. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  188. MSI_FLAG_LEVEL_CAPABLE),
  189. .ops = &mbi_pmsi_ops,
  190. .chip = &mbi_pmsi_irq_chip,
  191. };
  192. static int mbi_allocate_domains(struct irq_domain *parent)
  193. {
  194. struct irq_domain *nexus_domain, *pci_domain, *plat_domain;
  195. int err;
  196. nexus_domain = irq_domain_create_tree(parent->fwnode,
  197. &mbi_domain_ops, NULL);
  198. if (!nexus_domain)
  199. return -ENOMEM;
  200. irq_domain_update_bus_token(nexus_domain, DOMAIN_BUS_NEXUS);
  201. nexus_domain->parent = parent;
  202. err = mbi_allocate_pci_domain(nexus_domain, &pci_domain);
  203. plat_domain = platform_msi_create_irq_domain(parent->fwnode,
  204. &mbi_pmsi_domain_info,
  205. nexus_domain);
  206. if (err || !plat_domain) {
  207. if (plat_domain)
  208. irq_domain_remove(plat_domain);
  209. if (pci_domain)
  210. irq_domain_remove(pci_domain);
  211. irq_domain_remove(nexus_domain);
  212. return -ENOMEM;
  213. }
  214. return 0;
  215. }
  216. int __init mbi_init(struct fwnode_handle *fwnode, struct irq_domain *parent)
  217. {
  218. struct device_node *np;
  219. const __be32 *reg;
  220. int ret, n;
  221. np = to_of_node(fwnode);
  222. if (!of_property_read_bool(np, "msi-controller"))
  223. return 0;
  224. n = of_property_count_elems_of_size(np, "mbi-ranges", sizeof(u32));
  225. if (n <= 0 || n % 2)
  226. return -EINVAL;
  227. mbi_range_nr = n / 2;
  228. mbi_ranges = kcalloc(mbi_range_nr, sizeof(*mbi_ranges), GFP_KERNEL);
  229. if (!mbi_ranges)
  230. return -ENOMEM;
  231. for (n = 0; n < mbi_range_nr; n++) {
  232. ret = of_property_read_u32_index(np, "mbi-ranges", n * 2,
  233. &mbi_ranges[n].spi_start);
  234. if (ret)
  235. goto err_free_mbi;
  236. ret = of_property_read_u32_index(np, "mbi-ranges", n * 2 + 1,
  237. &mbi_ranges[n].nr_spis);
  238. if (ret)
  239. goto err_free_mbi;
  240. mbi_ranges[n].bm = bitmap_zalloc(mbi_ranges[n].nr_spis, GFP_KERNEL);
  241. if (!mbi_ranges[n].bm) {
  242. ret = -ENOMEM;
  243. goto err_free_mbi;
  244. }
  245. pr_info("MBI range [%d:%d]\n", mbi_ranges[n].spi_start,
  246. mbi_ranges[n].spi_start + mbi_ranges[n].nr_spis - 1);
  247. }
  248. reg = of_get_property(np, "mbi-alias", NULL);
  249. if (reg) {
  250. mbi_phys_base = of_translate_address(np, reg);
  251. if (mbi_phys_base == (phys_addr_t)OF_BAD_ADDR) {
  252. ret = -ENXIO;
  253. goto err_free_mbi;
  254. }
  255. } else {
  256. struct resource res;
  257. if (of_address_to_resource(np, 0, &res)) {
  258. ret = -ENXIO;
  259. goto err_free_mbi;
  260. }
  261. mbi_phys_base = res.start;
  262. }
  263. pr_info("Using MBI frame %pa\n", &mbi_phys_base);
  264. ret = mbi_allocate_domains(parent);
  265. if (ret)
  266. goto err_free_mbi;
  267. return 0;
  268. err_free_mbi:
  269. if (mbi_ranges) {
  270. for (n = 0; n < mbi_range_nr; n++)
  271. bitmap_free(mbi_ranges[n].bm);
  272. kfree(mbi_ranges);
  273. }
  274. return ret;
  275. }