irq-gic-v2m.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * ARM GIC v2m MSI(-X) support
  3. * Support for Message Signaled Interrupts for systems that
  4. * implement ARM Generic Interrupt Controller: GICv2m.
  5. *
  6. * Copyright (C) 2014 Advanced Micro Devices, Inc.
  7. * Authors: Suravee Suthikulpanit <suravee.suthikulpanit@amd.com>
  8. * Harish Kasiviswanathan <harish.kasiviswanathan@amd.com>
  9. * Brandon Anderson <brandon.anderson@amd.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License version 2 as published
  13. * by the Free Software Foundation.
  14. */
  15. #define pr_fmt(fmt) "GICv2m: " fmt
  16. #include <linux/acpi.h>
  17. #include <linux/dma-iommu.h>
  18. #include <linux/irq.h>
  19. #include <linux/irqdomain.h>
  20. #include <linux/kernel.h>
  21. #include <linux/msi.h>
  22. #include <linux/of_address.h>
  23. #include <linux/of_pci.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/irqchip/arm-gic.h>
  27. /*
  28. * MSI_TYPER:
  29. * [31:26] Reserved
  30. * [25:16] lowest SPI assigned to MSI
  31. * [15:10] Reserved
  32. * [9:0] Numer of SPIs assigned to MSI
  33. */
  34. #define V2M_MSI_TYPER 0x008
  35. #define V2M_MSI_TYPER_BASE_SHIFT 16
  36. #define V2M_MSI_TYPER_BASE_MASK 0x3FF
  37. #define V2M_MSI_TYPER_NUM_MASK 0x3FF
  38. #define V2M_MSI_SETSPI_NS 0x040
  39. #define V2M_MIN_SPI 32
  40. #define V2M_MAX_SPI 1019
  41. #define V2M_MSI_IIDR 0xFCC
  42. #define V2M_MSI_TYPER_BASE_SPI(x) \
  43. (((x) >> V2M_MSI_TYPER_BASE_SHIFT) & V2M_MSI_TYPER_BASE_MASK)
  44. #define V2M_MSI_TYPER_NUM_SPI(x) ((x) & V2M_MSI_TYPER_NUM_MASK)
  45. /* APM X-Gene with GICv2m MSI_IIDR register value */
  46. #define XGENE_GICV2M_MSI_IIDR 0x06000170
  47. /* Broadcom NS2 GICv2m MSI_IIDR register value */
  48. #define BCM_NS2_GICV2M_MSI_IIDR 0x0000013f
  49. /* List of flags for specific v2m implementation */
  50. #define GICV2M_NEEDS_SPI_OFFSET 0x00000001
  51. static LIST_HEAD(v2m_nodes);
  52. static DEFINE_SPINLOCK(v2m_lock);
  53. struct v2m_data {
  54. struct list_head entry;
  55. struct fwnode_handle *fwnode;
  56. struct resource res; /* GICv2m resource */
  57. void __iomem *base; /* GICv2m virt address */
  58. u32 spi_start; /* The SPI number that MSIs start */
  59. u32 nr_spis; /* The number of SPIs for MSIs */
  60. u32 spi_offset; /* offset to be subtracted from SPI number */
  61. unsigned long *bm; /* MSI vector bitmap */
  62. u32 flags; /* v2m flags for specific implementation */
  63. };
  64. static void gicv2m_mask_msi_irq(struct irq_data *d)
  65. {
  66. pci_msi_mask_irq(d);
  67. irq_chip_mask_parent(d);
  68. }
  69. static void gicv2m_unmask_msi_irq(struct irq_data *d)
  70. {
  71. pci_msi_unmask_irq(d);
  72. irq_chip_unmask_parent(d);
  73. }
  74. static struct irq_chip gicv2m_msi_irq_chip = {
  75. .name = "MSI",
  76. .irq_mask = gicv2m_mask_msi_irq,
  77. .irq_unmask = gicv2m_unmask_msi_irq,
  78. .irq_eoi = irq_chip_eoi_parent,
  79. .irq_write_msi_msg = pci_msi_domain_write_msg,
  80. };
  81. static struct msi_domain_info gicv2m_msi_domain_info = {
  82. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  83. MSI_FLAG_PCI_MSIX | MSI_FLAG_MULTI_PCI_MSI),
  84. .chip = &gicv2m_msi_irq_chip,
  85. };
  86. static void gicv2m_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  87. {
  88. struct v2m_data *v2m = irq_data_get_irq_chip_data(data);
  89. phys_addr_t addr = v2m->res.start + V2M_MSI_SETSPI_NS;
  90. msg->address_hi = upper_32_bits(addr);
  91. msg->address_lo = lower_32_bits(addr);
  92. msg->data = data->hwirq;
  93. if (v2m->flags & GICV2M_NEEDS_SPI_OFFSET)
  94. msg->data -= v2m->spi_offset;
  95. iommu_dma_map_msi_msg(data->irq, msg);
  96. }
  97. static struct irq_chip gicv2m_irq_chip = {
  98. .name = "GICv2m",
  99. .irq_mask = irq_chip_mask_parent,
  100. .irq_unmask = irq_chip_unmask_parent,
  101. .irq_eoi = irq_chip_eoi_parent,
  102. .irq_set_affinity = irq_chip_set_affinity_parent,
  103. .irq_compose_msi_msg = gicv2m_compose_msi_msg,
  104. };
  105. static int gicv2m_irq_gic_domain_alloc(struct irq_domain *domain,
  106. unsigned int virq,
  107. irq_hw_number_t hwirq)
  108. {
  109. struct irq_fwspec fwspec;
  110. struct irq_data *d;
  111. int err;
  112. if (is_of_node(domain->parent->fwnode)) {
  113. fwspec.fwnode = domain->parent->fwnode;
  114. fwspec.param_count = 3;
  115. fwspec.param[0] = 0;
  116. fwspec.param[1] = hwirq - 32;
  117. fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
  118. } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
  119. fwspec.fwnode = domain->parent->fwnode;
  120. fwspec.param_count = 2;
  121. fwspec.param[0] = hwirq;
  122. fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
  123. } else {
  124. return -EINVAL;
  125. }
  126. err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  127. if (err)
  128. return err;
  129. /* Configure the interrupt line to be edge */
  130. d = irq_domain_get_irq_data(domain->parent, virq);
  131. d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
  132. return 0;
  133. }
  134. static void gicv2m_unalloc_msi(struct v2m_data *v2m, unsigned int hwirq,
  135. int nr_irqs)
  136. {
  137. spin_lock(&v2m_lock);
  138. bitmap_release_region(v2m->bm, hwirq - v2m->spi_start,
  139. get_count_order(nr_irqs));
  140. spin_unlock(&v2m_lock);
  141. }
  142. static int gicv2m_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  143. unsigned int nr_irqs, void *args)
  144. {
  145. struct v2m_data *v2m = NULL, *tmp;
  146. int hwirq, offset, i, err = 0;
  147. spin_lock(&v2m_lock);
  148. list_for_each_entry(tmp, &v2m_nodes, entry) {
  149. offset = bitmap_find_free_region(tmp->bm, tmp->nr_spis,
  150. get_count_order(nr_irqs));
  151. if (offset >= 0) {
  152. v2m = tmp;
  153. break;
  154. }
  155. }
  156. spin_unlock(&v2m_lock);
  157. if (!v2m)
  158. return -ENOSPC;
  159. hwirq = v2m->spi_start + offset;
  160. for (i = 0; i < nr_irqs; i++) {
  161. err = gicv2m_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
  162. if (err)
  163. goto fail;
  164. irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
  165. &gicv2m_irq_chip, v2m);
  166. }
  167. return 0;
  168. fail:
  169. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  170. gicv2m_unalloc_msi(v2m, hwirq, nr_irqs);
  171. return err;
  172. }
  173. static void gicv2m_irq_domain_free(struct irq_domain *domain,
  174. unsigned int virq, unsigned int nr_irqs)
  175. {
  176. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  177. struct v2m_data *v2m = irq_data_get_irq_chip_data(d);
  178. gicv2m_unalloc_msi(v2m, d->hwirq, nr_irqs);
  179. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  180. }
  181. static const struct irq_domain_ops gicv2m_domain_ops = {
  182. .alloc = gicv2m_irq_domain_alloc,
  183. .free = gicv2m_irq_domain_free,
  184. };
  185. static bool is_msi_spi_valid(u32 base, u32 num)
  186. {
  187. if (base < V2M_MIN_SPI) {
  188. pr_err("Invalid MSI base SPI (base:%u)\n", base);
  189. return false;
  190. }
  191. if ((num == 0) || (base + num > V2M_MAX_SPI)) {
  192. pr_err("Number of SPIs (%u) exceed maximum (%u)\n",
  193. num, V2M_MAX_SPI - V2M_MIN_SPI + 1);
  194. return false;
  195. }
  196. return true;
  197. }
  198. static struct irq_chip gicv2m_pmsi_irq_chip = {
  199. .name = "pMSI",
  200. };
  201. static struct msi_domain_ops gicv2m_pmsi_ops = {
  202. };
  203. static struct msi_domain_info gicv2m_pmsi_domain_info = {
  204. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS),
  205. .ops = &gicv2m_pmsi_ops,
  206. .chip = &gicv2m_pmsi_irq_chip,
  207. };
  208. static void gicv2m_teardown(void)
  209. {
  210. struct v2m_data *v2m, *tmp;
  211. list_for_each_entry_safe(v2m, tmp, &v2m_nodes, entry) {
  212. list_del(&v2m->entry);
  213. kfree(v2m->bm);
  214. iounmap(v2m->base);
  215. of_node_put(to_of_node(v2m->fwnode));
  216. if (is_fwnode_irqchip(v2m->fwnode))
  217. irq_domain_free_fwnode(v2m->fwnode);
  218. kfree(v2m);
  219. }
  220. }
  221. static int gicv2m_allocate_domains(struct irq_domain *parent)
  222. {
  223. struct irq_domain *inner_domain, *pci_domain, *plat_domain;
  224. struct v2m_data *v2m;
  225. v2m = list_first_entry_or_null(&v2m_nodes, struct v2m_data, entry);
  226. if (!v2m)
  227. return 0;
  228. inner_domain = irq_domain_create_tree(v2m->fwnode,
  229. &gicv2m_domain_ops, v2m);
  230. if (!inner_domain) {
  231. pr_err("Failed to create GICv2m domain\n");
  232. return -ENOMEM;
  233. }
  234. irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
  235. inner_domain->parent = parent;
  236. pci_domain = pci_msi_create_irq_domain(v2m->fwnode,
  237. &gicv2m_msi_domain_info,
  238. inner_domain);
  239. plat_domain = platform_msi_create_irq_domain(v2m->fwnode,
  240. &gicv2m_pmsi_domain_info,
  241. inner_domain);
  242. if (!pci_domain || !plat_domain) {
  243. pr_err("Failed to create MSI domains\n");
  244. if (plat_domain)
  245. irq_domain_remove(plat_domain);
  246. if (pci_domain)
  247. irq_domain_remove(pci_domain);
  248. irq_domain_remove(inner_domain);
  249. return -ENOMEM;
  250. }
  251. return 0;
  252. }
  253. static int __init gicv2m_init_one(struct fwnode_handle *fwnode,
  254. u32 spi_start, u32 nr_spis,
  255. struct resource *res)
  256. {
  257. int ret;
  258. struct v2m_data *v2m;
  259. v2m = kzalloc(sizeof(struct v2m_data), GFP_KERNEL);
  260. if (!v2m) {
  261. pr_err("Failed to allocate struct v2m_data.\n");
  262. return -ENOMEM;
  263. }
  264. INIT_LIST_HEAD(&v2m->entry);
  265. v2m->fwnode = fwnode;
  266. memcpy(&v2m->res, res, sizeof(struct resource));
  267. v2m->base = ioremap(v2m->res.start, resource_size(&v2m->res));
  268. if (!v2m->base) {
  269. pr_err("Failed to map GICv2m resource\n");
  270. ret = -ENOMEM;
  271. goto err_free_v2m;
  272. }
  273. if (spi_start && nr_spis) {
  274. v2m->spi_start = spi_start;
  275. v2m->nr_spis = nr_spis;
  276. } else {
  277. u32 typer = readl_relaxed(v2m->base + V2M_MSI_TYPER);
  278. v2m->spi_start = V2M_MSI_TYPER_BASE_SPI(typer);
  279. v2m->nr_spis = V2M_MSI_TYPER_NUM_SPI(typer);
  280. }
  281. if (!is_msi_spi_valid(v2m->spi_start, v2m->nr_spis)) {
  282. ret = -EINVAL;
  283. goto err_iounmap;
  284. }
  285. /*
  286. * APM X-Gene GICv2m implementation has an erratum where
  287. * the MSI data needs to be the offset from the spi_start
  288. * in order to trigger the correct MSI interrupt. This is
  289. * different from the standard GICv2m implementation where
  290. * the MSI data is the absolute value within the range from
  291. * spi_start to (spi_start + num_spis).
  292. *
  293. * Broadom NS2 GICv2m implementation has an erratum where the MSI data
  294. * is 'spi_number - 32'
  295. */
  296. switch (readl_relaxed(v2m->base + V2M_MSI_IIDR)) {
  297. case XGENE_GICV2M_MSI_IIDR:
  298. v2m->flags |= GICV2M_NEEDS_SPI_OFFSET;
  299. v2m->spi_offset = v2m->spi_start;
  300. break;
  301. case BCM_NS2_GICV2M_MSI_IIDR:
  302. v2m->flags |= GICV2M_NEEDS_SPI_OFFSET;
  303. v2m->spi_offset = 32;
  304. break;
  305. }
  306. v2m->bm = kcalloc(BITS_TO_LONGS(v2m->nr_spis), sizeof(long),
  307. GFP_KERNEL);
  308. if (!v2m->bm) {
  309. ret = -ENOMEM;
  310. goto err_iounmap;
  311. }
  312. list_add_tail(&v2m->entry, &v2m_nodes);
  313. pr_info("range%pR, SPI[%d:%d]\n", res,
  314. v2m->spi_start, (v2m->spi_start + v2m->nr_spis - 1));
  315. return 0;
  316. err_iounmap:
  317. iounmap(v2m->base);
  318. err_free_v2m:
  319. kfree(v2m);
  320. return ret;
  321. }
  322. static struct of_device_id gicv2m_device_id[] = {
  323. { .compatible = "arm,gic-v2m-frame", },
  324. {},
  325. };
  326. static int __init gicv2m_of_init(struct fwnode_handle *parent_handle,
  327. struct irq_domain *parent)
  328. {
  329. int ret = 0;
  330. struct device_node *node = to_of_node(parent_handle);
  331. struct device_node *child;
  332. for (child = of_find_matching_node(node, gicv2m_device_id); child;
  333. child = of_find_matching_node(child, gicv2m_device_id)) {
  334. u32 spi_start = 0, nr_spis = 0;
  335. struct resource res;
  336. if (!of_find_property(child, "msi-controller", NULL))
  337. continue;
  338. ret = of_address_to_resource(child, 0, &res);
  339. if (ret) {
  340. pr_err("Failed to allocate v2m resource.\n");
  341. break;
  342. }
  343. if (!of_property_read_u32(child, "arm,msi-base-spi",
  344. &spi_start) &&
  345. !of_property_read_u32(child, "arm,msi-num-spis", &nr_spis))
  346. pr_info("DT overriding V2M MSI_TYPER (base:%u, num:%u)\n",
  347. spi_start, nr_spis);
  348. ret = gicv2m_init_one(&child->fwnode, spi_start, nr_spis, &res);
  349. if (ret) {
  350. of_node_put(child);
  351. break;
  352. }
  353. }
  354. if (!ret)
  355. ret = gicv2m_allocate_domains(parent);
  356. if (ret)
  357. gicv2m_teardown();
  358. return ret;
  359. }
  360. #ifdef CONFIG_ACPI
  361. static int acpi_num_msi;
  362. static struct fwnode_handle *gicv2m_get_fwnode(struct device *dev)
  363. {
  364. struct v2m_data *data;
  365. if (WARN_ON(acpi_num_msi <= 0))
  366. return NULL;
  367. /* We only return the fwnode of the first MSI frame. */
  368. data = list_first_entry_or_null(&v2m_nodes, struct v2m_data, entry);
  369. if (!data)
  370. return NULL;
  371. return data->fwnode;
  372. }
  373. static int __init
  374. acpi_parse_madt_msi(struct acpi_subtable_header *header,
  375. const unsigned long end)
  376. {
  377. int ret;
  378. struct resource res;
  379. u32 spi_start = 0, nr_spis = 0;
  380. struct acpi_madt_generic_msi_frame *m;
  381. struct fwnode_handle *fwnode;
  382. m = (struct acpi_madt_generic_msi_frame *)header;
  383. if (BAD_MADT_ENTRY(m, end))
  384. return -EINVAL;
  385. res.start = m->base_address;
  386. res.end = m->base_address + SZ_4K - 1;
  387. res.flags = IORESOURCE_MEM;
  388. if (m->flags & ACPI_MADT_OVERRIDE_SPI_VALUES) {
  389. spi_start = m->spi_base;
  390. nr_spis = m->spi_count;
  391. pr_info("ACPI overriding V2M MSI_TYPER (base:%u, num:%u)\n",
  392. spi_start, nr_spis);
  393. }
  394. fwnode = irq_domain_alloc_fwnode((void *)m->base_address);
  395. if (!fwnode) {
  396. pr_err("Unable to allocate GICv2m domain token\n");
  397. return -EINVAL;
  398. }
  399. ret = gicv2m_init_one(fwnode, spi_start, nr_spis, &res);
  400. if (ret)
  401. irq_domain_free_fwnode(fwnode);
  402. return ret;
  403. }
  404. static int __init gicv2m_acpi_init(struct irq_domain *parent)
  405. {
  406. int ret;
  407. if (acpi_num_msi > 0)
  408. return 0;
  409. acpi_num_msi = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_MSI_FRAME,
  410. acpi_parse_madt_msi, 0);
  411. if (acpi_num_msi <= 0)
  412. goto err_out;
  413. ret = gicv2m_allocate_domains(parent);
  414. if (ret)
  415. goto err_out;
  416. pci_msi_register_fwnode_provider(&gicv2m_get_fwnode);
  417. return 0;
  418. err_out:
  419. gicv2m_teardown();
  420. return -EINVAL;
  421. }
  422. #else /* CONFIG_ACPI */
  423. static int __init gicv2m_acpi_init(struct irq_domain *parent)
  424. {
  425. return -EINVAL;
  426. }
  427. #endif /* CONFIG_ACPI */
  428. int __init gicv2m_init(struct fwnode_handle *parent_handle,
  429. struct irq_domain *parent)
  430. {
  431. if (is_of_node(parent_handle))
  432. return gicv2m_of_init(parent_handle, parent);
  433. return gicv2m_acpi_init(parent);
  434. }