irq-gic-v2m.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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/irq.h>
  17. #include <linux/irqdomain.h>
  18. #include <linux/kernel.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_pci.h>
  21. #include <linux/slab.h>
  22. #include <linux/spinlock.h>
  23. /*
  24. * MSI_TYPER:
  25. * [31:26] Reserved
  26. * [25:16] lowest SPI assigned to MSI
  27. * [15:10] Reserved
  28. * [9:0] Numer of SPIs assigned to MSI
  29. */
  30. #define V2M_MSI_TYPER 0x008
  31. #define V2M_MSI_TYPER_BASE_SHIFT 16
  32. #define V2M_MSI_TYPER_BASE_MASK 0x3FF
  33. #define V2M_MSI_TYPER_NUM_MASK 0x3FF
  34. #define V2M_MSI_SETSPI_NS 0x040
  35. #define V2M_MIN_SPI 32
  36. #define V2M_MAX_SPI 1019
  37. #define V2M_MSI_IIDR 0xFCC
  38. #define V2M_MSI_TYPER_BASE_SPI(x) \
  39. (((x) >> V2M_MSI_TYPER_BASE_SHIFT) & V2M_MSI_TYPER_BASE_MASK)
  40. #define V2M_MSI_TYPER_NUM_SPI(x) ((x) & V2M_MSI_TYPER_NUM_MASK)
  41. /* APM X-Gene with GICv2m MSI_IIDR register value */
  42. #define XGENE_GICV2M_MSI_IIDR 0x06000170
  43. /* List of flags for specific v2m implementation */
  44. #define GICV2M_NEEDS_SPI_OFFSET 0x00000001
  45. static LIST_HEAD(v2m_nodes);
  46. static DEFINE_SPINLOCK(v2m_lock);
  47. struct v2m_data {
  48. struct list_head entry;
  49. struct device_node *node;
  50. struct resource res; /* GICv2m resource */
  51. void __iomem *base; /* GICv2m virt address */
  52. u32 spi_start; /* The SPI number that MSIs start */
  53. u32 nr_spis; /* The number of SPIs for MSIs */
  54. unsigned long *bm; /* MSI vector bitmap */
  55. u32 flags; /* v2m flags for specific implementation */
  56. };
  57. static void gicv2m_mask_msi_irq(struct irq_data *d)
  58. {
  59. pci_msi_mask_irq(d);
  60. irq_chip_mask_parent(d);
  61. }
  62. static void gicv2m_unmask_msi_irq(struct irq_data *d)
  63. {
  64. pci_msi_unmask_irq(d);
  65. irq_chip_unmask_parent(d);
  66. }
  67. static struct irq_chip gicv2m_msi_irq_chip = {
  68. .name = "MSI",
  69. .irq_mask = gicv2m_mask_msi_irq,
  70. .irq_unmask = gicv2m_unmask_msi_irq,
  71. .irq_eoi = irq_chip_eoi_parent,
  72. .irq_write_msi_msg = pci_msi_domain_write_msg,
  73. };
  74. static struct msi_domain_info gicv2m_msi_domain_info = {
  75. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  76. MSI_FLAG_PCI_MSIX),
  77. .chip = &gicv2m_msi_irq_chip,
  78. };
  79. static int gicv2m_set_affinity(struct irq_data *irq_data,
  80. const struct cpumask *mask, bool force)
  81. {
  82. int ret;
  83. ret = irq_chip_set_affinity_parent(irq_data, mask, force);
  84. if (ret == IRQ_SET_MASK_OK)
  85. ret = IRQ_SET_MASK_OK_DONE;
  86. return ret;
  87. }
  88. static void gicv2m_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  89. {
  90. struct v2m_data *v2m = irq_data_get_irq_chip_data(data);
  91. phys_addr_t addr = v2m->res.start + V2M_MSI_SETSPI_NS;
  92. msg->address_hi = upper_32_bits(addr);
  93. msg->address_lo = lower_32_bits(addr);
  94. msg->data = data->hwirq;
  95. if (v2m->flags & GICV2M_NEEDS_SPI_OFFSET)
  96. msg->data -= v2m->spi_start;
  97. }
  98. static struct irq_chip gicv2m_irq_chip = {
  99. .name = "GICv2m",
  100. .irq_mask = irq_chip_mask_parent,
  101. .irq_unmask = irq_chip_unmask_parent,
  102. .irq_eoi = irq_chip_eoi_parent,
  103. .irq_set_affinity = gicv2m_set_affinity,
  104. .irq_compose_msi_msg = gicv2m_compose_msi_msg,
  105. };
  106. static int gicv2m_irq_gic_domain_alloc(struct irq_domain *domain,
  107. unsigned int virq,
  108. irq_hw_number_t hwirq)
  109. {
  110. struct irq_fwspec fwspec;
  111. struct irq_data *d;
  112. int err;
  113. if (is_of_node(domain->parent->fwnode)) {
  114. fwspec.fwnode = domain->parent->fwnode;
  115. fwspec.param_count = 3;
  116. fwspec.param[0] = 0;
  117. fwspec.param[1] = hwirq - 32;
  118. fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
  119. } else {
  120. return -EINVAL;
  121. }
  122. err = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  123. if (err)
  124. return err;
  125. /* Configure the interrupt line to be edge */
  126. d = irq_domain_get_irq_data(domain->parent, virq);
  127. d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
  128. return 0;
  129. }
  130. static void gicv2m_unalloc_msi(struct v2m_data *v2m, unsigned int hwirq)
  131. {
  132. int pos;
  133. pos = hwirq - v2m->spi_start;
  134. if (pos < 0 || pos >= v2m->nr_spis) {
  135. pr_err("Failed to teardown msi. Invalid hwirq %d\n", hwirq);
  136. return;
  137. }
  138. spin_lock(&v2m_lock);
  139. __clear_bit(pos, v2m->bm);
  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, err = 0;
  147. spin_lock(&v2m_lock);
  148. list_for_each_entry(tmp, &v2m_nodes, entry) {
  149. offset = find_first_zero_bit(tmp->bm, tmp->nr_spis);
  150. if (offset < tmp->nr_spis) {
  151. __set_bit(offset, tmp->bm);
  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. err = gicv2m_irq_gic_domain_alloc(domain, virq, hwirq);
  161. if (err) {
  162. gicv2m_unalloc_msi(v2m, hwirq);
  163. return err;
  164. }
  165. irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  166. &gicv2m_irq_chip, v2m);
  167. return 0;
  168. }
  169. static void gicv2m_irq_domain_free(struct irq_domain *domain,
  170. unsigned int virq, unsigned int nr_irqs)
  171. {
  172. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  173. struct v2m_data *v2m = irq_data_get_irq_chip_data(d);
  174. BUG_ON(nr_irqs != 1);
  175. gicv2m_unalloc_msi(v2m, d->hwirq);
  176. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  177. }
  178. static const struct irq_domain_ops gicv2m_domain_ops = {
  179. .alloc = gicv2m_irq_domain_alloc,
  180. .free = gicv2m_irq_domain_free,
  181. };
  182. static bool is_msi_spi_valid(u32 base, u32 num)
  183. {
  184. if (base < V2M_MIN_SPI) {
  185. pr_err("Invalid MSI base SPI (base:%u)\n", base);
  186. return false;
  187. }
  188. if ((num == 0) || (base + num > V2M_MAX_SPI)) {
  189. pr_err("Number of SPIs (%u) exceed maximum (%u)\n",
  190. num, V2M_MAX_SPI - V2M_MIN_SPI + 1);
  191. return false;
  192. }
  193. return true;
  194. }
  195. static struct irq_chip gicv2m_pmsi_irq_chip = {
  196. .name = "pMSI",
  197. };
  198. static struct msi_domain_ops gicv2m_pmsi_ops = {
  199. };
  200. static struct msi_domain_info gicv2m_pmsi_domain_info = {
  201. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS),
  202. .ops = &gicv2m_pmsi_ops,
  203. .chip = &gicv2m_pmsi_irq_chip,
  204. };
  205. static void gicv2m_teardown(void)
  206. {
  207. struct v2m_data *v2m, *tmp;
  208. list_for_each_entry_safe(v2m, tmp, &v2m_nodes, entry) {
  209. list_del(&v2m->entry);
  210. kfree(v2m->bm);
  211. iounmap(v2m->base);
  212. of_node_put(v2m->node);
  213. kfree(v2m);
  214. }
  215. }
  216. static int gicv2m_allocate_domains(struct irq_domain *parent)
  217. {
  218. struct irq_domain *inner_domain, *pci_domain, *plat_domain;
  219. struct v2m_data *v2m;
  220. v2m = list_first_entry_or_null(&v2m_nodes, struct v2m_data, entry);
  221. if (!v2m)
  222. return 0;
  223. inner_domain = irq_domain_create_tree(of_node_to_fwnode(v2m->node),
  224. &gicv2m_domain_ops, v2m);
  225. if (!inner_domain) {
  226. pr_err("Failed to create GICv2m domain\n");
  227. return -ENOMEM;
  228. }
  229. inner_domain->bus_token = DOMAIN_BUS_NEXUS;
  230. inner_domain->parent = parent;
  231. pci_domain = pci_msi_create_irq_domain(of_node_to_fwnode(v2m->node),
  232. &gicv2m_msi_domain_info,
  233. inner_domain);
  234. plat_domain = platform_msi_create_irq_domain(of_node_to_fwnode(v2m->node),
  235. &gicv2m_pmsi_domain_info,
  236. inner_domain);
  237. if (!pci_domain || !plat_domain) {
  238. pr_err("Failed to create MSI domains\n");
  239. if (plat_domain)
  240. irq_domain_remove(plat_domain);
  241. if (pci_domain)
  242. irq_domain_remove(pci_domain);
  243. irq_domain_remove(inner_domain);
  244. return -ENOMEM;
  245. }
  246. return 0;
  247. }
  248. static int __init gicv2m_init_one(struct device_node *node,
  249. struct irq_domain *parent)
  250. {
  251. int ret;
  252. struct v2m_data *v2m;
  253. v2m = kzalloc(sizeof(struct v2m_data), GFP_KERNEL);
  254. if (!v2m) {
  255. pr_err("Failed to allocate struct v2m_data.\n");
  256. return -ENOMEM;
  257. }
  258. INIT_LIST_HEAD(&v2m->entry);
  259. v2m->node = node;
  260. ret = of_address_to_resource(node, 0, &v2m->res);
  261. if (ret) {
  262. pr_err("Failed to allocate v2m resource.\n");
  263. goto err_free_v2m;
  264. }
  265. v2m->base = ioremap(v2m->res.start, resource_size(&v2m->res));
  266. if (!v2m->base) {
  267. pr_err("Failed to map GICv2m resource\n");
  268. ret = -ENOMEM;
  269. goto err_free_v2m;
  270. }
  271. if (!of_property_read_u32(node, "arm,msi-base-spi", &v2m->spi_start) &&
  272. !of_property_read_u32(node, "arm,msi-num-spis", &v2m->nr_spis)) {
  273. pr_info("Overriding V2M MSI_TYPER (base:%u, num:%u)\n",
  274. v2m->spi_start, v2m->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. if (readl_relaxed(v2m->base + V2M_MSI_IIDR) == XGENE_GICV2M_MSI_IIDR)
  293. v2m->flags |= GICV2M_NEEDS_SPI_OFFSET;
  294. v2m->bm = kzalloc(sizeof(long) * BITS_TO_LONGS(v2m->nr_spis),
  295. GFP_KERNEL);
  296. if (!v2m->bm) {
  297. ret = -ENOMEM;
  298. goto err_iounmap;
  299. }
  300. list_add_tail(&v2m->entry, &v2m_nodes);
  301. pr_info("Node %s: range[%#lx:%#lx], SPI[%d:%d]\n", node->name,
  302. (unsigned long)v2m->res.start, (unsigned long)v2m->res.end,
  303. v2m->spi_start, (v2m->spi_start + v2m->nr_spis));
  304. return 0;
  305. err_iounmap:
  306. iounmap(v2m->base);
  307. err_free_v2m:
  308. kfree(v2m);
  309. return ret;
  310. }
  311. static struct of_device_id gicv2m_device_id[] = {
  312. { .compatible = "arm,gic-v2m-frame", },
  313. {},
  314. };
  315. int __init gicv2m_of_init(struct device_node *node, struct irq_domain *parent)
  316. {
  317. int ret = 0;
  318. struct device_node *child;
  319. for (child = of_find_matching_node(node, gicv2m_device_id); child;
  320. child = of_find_matching_node(child, gicv2m_device_id)) {
  321. if (!of_find_property(child, "msi-controller", NULL))
  322. continue;
  323. ret = gicv2m_init_one(child, parent);
  324. if (ret) {
  325. of_node_put(node);
  326. break;
  327. }
  328. }
  329. if (!ret)
  330. ret = gicv2m_allocate_domains(parent);
  331. if (ret)
  332. gicv2m_teardown();
  333. return ret;
  334. }