irq-gic-v2m.c 14 KB

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