irq-gic-v2m.c 13 KB

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