irq-gic-v2m.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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),
  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. {
  136. int pos;
  137. pos = hwirq - v2m->spi_start;
  138. if (pos < 0 || pos >= v2m->nr_spis) {
  139. pr_err("Failed to teardown msi. Invalid hwirq %d\n", hwirq);
  140. return;
  141. }
  142. spin_lock(&v2m_lock);
  143. __clear_bit(pos, v2m->bm);
  144. spin_unlock(&v2m_lock);
  145. }
  146. static int gicv2m_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  147. unsigned int nr_irqs, void *args)
  148. {
  149. struct v2m_data *v2m = NULL, *tmp;
  150. int hwirq, offset, err = 0;
  151. spin_lock(&v2m_lock);
  152. list_for_each_entry(tmp, &v2m_nodes, entry) {
  153. offset = find_first_zero_bit(tmp->bm, tmp->nr_spis);
  154. if (offset < tmp->nr_spis) {
  155. __set_bit(offset, tmp->bm);
  156. v2m = tmp;
  157. break;
  158. }
  159. }
  160. spin_unlock(&v2m_lock);
  161. if (!v2m)
  162. return -ENOSPC;
  163. hwirq = v2m->spi_start + offset;
  164. err = gicv2m_irq_gic_domain_alloc(domain, virq, hwirq);
  165. if (err) {
  166. gicv2m_unalloc_msi(v2m, hwirq);
  167. return err;
  168. }
  169. irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  170. &gicv2m_irq_chip, v2m);
  171. return 0;
  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. BUG_ON(nr_irqs != 1);
  179. gicv2m_unalloc_msi(v2m, d->hwirq);
  180. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  181. }
  182. static const struct irq_domain_ops gicv2m_domain_ops = {
  183. .alloc = gicv2m_irq_domain_alloc,
  184. .free = gicv2m_irq_domain_free,
  185. };
  186. static bool is_msi_spi_valid(u32 base, u32 num)
  187. {
  188. if (base < V2M_MIN_SPI) {
  189. pr_err("Invalid MSI base SPI (base:%u)\n", base);
  190. return false;
  191. }
  192. if ((num == 0) || (base + num > V2M_MAX_SPI)) {
  193. pr_err("Number of SPIs (%u) exceed maximum (%u)\n",
  194. num, V2M_MAX_SPI - V2M_MIN_SPI + 1);
  195. return false;
  196. }
  197. return true;
  198. }
  199. static struct irq_chip gicv2m_pmsi_irq_chip = {
  200. .name = "pMSI",
  201. };
  202. static struct msi_domain_ops gicv2m_pmsi_ops = {
  203. };
  204. static struct msi_domain_info gicv2m_pmsi_domain_info = {
  205. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS),
  206. .ops = &gicv2m_pmsi_ops,
  207. .chip = &gicv2m_pmsi_irq_chip,
  208. };
  209. static void gicv2m_teardown(void)
  210. {
  211. struct v2m_data *v2m, *tmp;
  212. list_for_each_entry_safe(v2m, tmp, &v2m_nodes, entry) {
  213. list_del(&v2m->entry);
  214. kfree(v2m->bm);
  215. iounmap(v2m->base);
  216. of_node_put(to_of_node(v2m->fwnode));
  217. if (is_fwnode_irqchip(v2m->fwnode))
  218. irq_domain_free_fwnode(v2m->fwnode);
  219. kfree(v2m);
  220. }
  221. }
  222. static int gicv2m_allocate_domains(struct irq_domain *parent)
  223. {
  224. struct irq_domain *inner_domain, *pci_domain, *plat_domain;
  225. struct v2m_data *v2m;
  226. v2m = list_first_entry_or_null(&v2m_nodes, struct v2m_data, entry);
  227. if (!v2m)
  228. return 0;
  229. inner_domain = irq_domain_create_tree(v2m->fwnode,
  230. &gicv2m_domain_ops, v2m);
  231. if (!inner_domain) {
  232. pr_err("Failed to create GICv2m domain\n");
  233. return -ENOMEM;
  234. }
  235. inner_domain->bus_token = DOMAIN_BUS_NEXUS;
  236. inner_domain->parent = parent;
  237. pci_domain = pci_msi_create_irq_domain(v2m->fwnode,
  238. &gicv2m_msi_domain_info,
  239. inner_domain);
  240. plat_domain = platform_msi_create_irq_domain(v2m->fwnode,
  241. &gicv2m_pmsi_domain_info,
  242. inner_domain);
  243. if (!pci_domain || !plat_domain) {
  244. pr_err("Failed to create MSI domains\n");
  245. if (plat_domain)
  246. irq_domain_remove(plat_domain);
  247. if (pci_domain)
  248. irq_domain_remove(pci_domain);
  249. irq_domain_remove(inner_domain);
  250. return -ENOMEM;
  251. }
  252. return 0;
  253. }
  254. static int __init gicv2m_init_one(struct fwnode_handle *fwnode,
  255. u32 spi_start, u32 nr_spis,
  256. struct resource *res)
  257. {
  258. int ret;
  259. struct v2m_data *v2m;
  260. v2m = kzalloc(sizeof(struct v2m_data), GFP_KERNEL);
  261. if (!v2m) {
  262. pr_err("Failed to allocate struct v2m_data.\n");
  263. return -ENOMEM;
  264. }
  265. INIT_LIST_HEAD(&v2m->entry);
  266. v2m->fwnode = fwnode;
  267. memcpy(&v2m->res, res, sizeof(struct resource));
  268. v2m->base = ioremap(v2m->res.start, resource_size(&v2m->res));
  269. if (!v2m->base) {
  270. pr_err("Failed to map GICv2m resource\n");
  271. ret = -ENOMEM;
  272. goto err_free_v2m;
  273. }
  274. if (spi_start && nr_spis) {
  275. v2m->spi_start = spi_start;
  276. v2m->nr_spis = nr_spis;
  277. } else {
  278. u32 typer = readl_relaxed(v2m->base + V2M_MSI_TYPER);
  279. v2m->spi_start = V2M_MSI_TYPER_BASE_SPI(typer);
  280. v2m->nr_spis = V2M_MSI_TYPER_NUM_SPI(typer);
  281. }
  282. if (!is_msi_spi_valid(v2m->spi_start, v2m->nr_spis)) {
  283. ret = -EINVAL;
  284. goto err_iounmap;
  285. }
  286. /*
  287. * APM X-Gene GICv2m implementation has an erratum where
  288. * the MSI data needs to be the offset from the spi_start
  289. * in order to trigger the correct MSI interrupt. This is
  290. * different from the standard GICv2m implementation where
  291. * the MSI data is the absolute value within the range from
  292. * spi_start to (spi_start + num_spis).
  293. *
  294. * Broadom NS2 GICv2m implementation has an erratum where the MSI data
  295. * is 'spi_number - 32'
  296. */
  297. switch (readl_relaxed(v2m->base + V2M_MSI_IIDR)) {
  298. case XGENE_GICV2M_MSI_IIDR:
  299. v2m->flags |= GICV2M_NEEDS_SPI_OFFSET;
  300. v2m->spi_offset = v2m->spi_start;
  301. break;
  302. case BCM_NS2_GICV2M_MSI_IIDR:
  303. v2m->flags |= GICV2M_NEEDS_SPI_OFFSET;
  304. v2m->spi_offset = 32;
  305. break;
  306. }
  307. v2m->bm = kzalloc(sizeof(long) * BITS_TO_LONGS(v2m->nr_spis),
  308. GFP_KERNEL);
  309. if (!v2m->bm) {
  310. ret = -ENOMEM;
  311. goto err_iounmap;
  312. }
  313. list_add_tail(&v2m->entry, &v2m_nodes);
  314. pr_info("range%pR, SPI[%d:%d]\n", res,
  315. v2m->spi_start, (v2m->spi_start + v2m->nr_spis - 1));
  316. return 0;
  317. err_iounmap:
  318. iounmap(v2m->base);
  319. err_free_v2m:
  320. kfree(v2m);
  321. return ret;
  322. }
  323. static struct of_device_id gicv2m_device_id[] = {
  324. { .compatible = "arm,gic-v2m-frame", },
  325. {},
  326. };
  327. static int __init gicv2m_of_init(struct fwnode_handle *parent_handle,
  328. struct irq_domain *parent)
  329. {
  330. int ret = 0;
  331. struct device_node *node = to_of_node(parent_handle);
  332. struct device_node *child;
  333. for (child = of_find_matching_node(node, gicv2m_device_id); child;
  334. child = of_find_matching_node(child, gicv2m_device_id)) {
  335. u32 spi_start = 0, nr_spis = 0;
  336. struct resource res;
  337. if (!of_find_property(child, "msi-controller", NULL))
  338. continue;
  339. ret = of_address_to_resource(child, 0, &res);
  340. if (ret) {
  341. pr_err("Failed to allocate v2m resource.\n");
  342. break;
  343. }
  344. if (!of_property_read_u32(child, "arm,msi-base-spi",
  345. &spi_start) &&
  346. !of_property_read_u32(child, "arm,msi-num-spis", &nr_spis))
  347. pr_info("DT overriding V2M MSI_TYPER (base:%u, num:%u)\n",
  348. spi_start, nr_spis);
  349. ret = gicv2m_init_one(&child->fwnode, spi_start, nr_spis, &res);
  350. if (ret) {
  351. of_node_put(child);
  352. break;
  353. }
  354. }
  355. if (!ret)
  356. ret = gicv2m_allocate_domains(parent);
  357. if (ret)
  358. gicv2m_teardown();
  359. return ret;
  360. }
  361. #ifdef CONFIG_ACPI
  362. static int acpi_num_msi;
  363. static struct fwnode_handle *gicv2m_get_fwnode(struct device *dev)
  364. {
  365. struct v2m_data *data;
  366. if (WARN_ON(acpi_num_msi <= 0))
  367. return NULL;
  368. /* We only return the fwnode of the first MSI frame. */
  369. data = list_first_entry_or_null(&v2m_nodes, struct v2m_data, entry);
  370. if (!data)
  371. return NULL;
  372. return data->fwnode;
  373. }
  374. static int __init
  375. acpi_parse_madt_msi(struct acpi_subtable_header *header,
  376. const unsigned long end)
  377. {
  378. int ret;
  379. struct resource res;
  380. u32 spi_start = 0, nr_spis = 0;
  381. struct acpi_madt_generic_msi_frame *m;
  382. struct fwnode_handle *fwnode;
  383. m = (struct acpi_madt_generic_msi_frame *)header;
  384. if (BAD_MADT_ENTRY(m, end))
  385. return -EINVAL;
  386. res.start = m->base_address;
  387. res.end = m->base_address + SZ_4K - 1;
  388. res.flags = IORESOURCE_MEM;
  389. if (m->flags & ACPI_MADT_OVERRIDE_SPI_VALUES) {
  390. spi_start = m->spi_base;
  391. nr_spis = m->spi_count;
  392. pr_info("ACPI overriding V2M MSI_TYPER (base:%u, num:%u)\n",
  393. spi_start, nr_spis);
  394. }
  395. fwnode = irq_domain_alloc_fwnode((void *)m->base_address);
  396. if (!fwnode) {
  397. pr_err("Unable to allocate GICv2m domain token\n");
  398. return -EINVAL;
  399. }
  400. ret = gicv2m_init_one(fwnode, spi_start, nr_spis, &res);
  401. if (ret)
  402. irq_domain_free_fwnode(fwnode);
  403. return ret;
  404. }
  405. static int __init gicv2m_acpi_init(struct irq_domain *parent)
  406. {
  407. int ret;
  408. if (acpi_num_msi > 0)
  409. return 0;
  410. acpi_num_msi = acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_MSI_FRAME,
  411. acpi_parse_madt_msi, 0);
  412. if (acpi_num_msi <= 0)
  413. goto err_out;
  414. ret = gicv2m_allocate_domains(parent);
  415. if (ret)
  416. goto err_out;
  417. pci_msi_register_fwnode_provider(&gicv2m_get_fwnode);
  418. return 0;
  419. err_out:
  420. gicv2m_teardown();
  421. return -EINVAL;
  422. }
  423. #else /* CONFIG_ACPI */
  424. static int __init gicv2m_acpi_init(struct irq_domain *parent)
  425. {
  426. return -EINVAL;
  427. }
  428. #endif /* CONFIG_ACPI */
  429. int __init gicv2m_init(struct fwnode_handle *parent_handle,
  430. struct irq_domain *parent)
  431. {
  432. if (is_of_node(parent_handle))
  433. return gicv2m_of_init(parent_handle, parent);
  434. return gicv2m_acpi_init(parent);
  435. }