irq-gic-v2m.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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_TYPER_BASE_SPI(x) \
  38. (((x) >> V2M_MSI_TYPER_BASE_SHIFT) & V2M_MSI_TYPER_BASE_MASK)
  39. #define V2M_MSI_TYPER_NUM_SPI(x) ((x) & V2M_MSI_TYPER_NUM_MASK)
  40. struct v2m_data {
  41. spinlock_t msi_cnt_lock;
  42. struct msi_controller mchip;
  43. struct resource res; /* GICv2m resource */
  44. void __iomem *base; /* GICv2m virt address */
  45. u32 spi_start; /* The SPI number that MSIs start */
  46. u32 nr_spis; /* The number of SPIs for MSIs */
  47. unsigned long *bm; /* MSI vector bitmap */
  48. struct irq_domain *domain;
  49. };
  50. static void gicv2m_mask_msi_irq(struct irq_data *d)
  51. {
  52. pci_msi_mask_irq(d);
  53. irq_chip_mask_parent(d);
  54. }
  55. static void gicv2m_unmask_msi_irq(struct irq_data *d)
  56. {
  57. pci_msi_unmask_irq(d);
  58. irq_chip_unmask_parent(d);
  59. }
  60. static struct irq_chip gicv2m_msi_irq_chip = {
  61. .name = "MSI",
  62. .irq_mask = gicv2m_mask_msi_irq,
  63. .irq_unmask = gicv2m_unmask_msi_irq,
  64. .irq_eoi = irq_chip_eoi_parent,
  65. .irq_write_msi_msg = pci_msi_domain_write_msg,
  66. };
  67. static struct msi_domain_info gicv2m_msi_domain_info = {
  68. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
  69. MSI_FLAG_PCI_MSIX),
  70. .chip = &gicv2m_msi_irq_chip,
  71. };
  72. static int gicv2m_set_affinity(struct irq_data *irq_data,
  73. const struct cpumask *mask, bool force)
  74. {
  75. int ret;
  76. ret = irq_chip_set_affinity_parent(irq_data, mask, force);
  77. if (ret == IRQ_SET_MASK_OK)
  78. ret = IRQ_SET_MASK_OK_DONE;
  79. return ret;
  80. }
  81. static void gicv2m_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  82. {
  83. struct v2m_data *v2m = irq_data_get_irq_chip_data(data);
  84. phys_addr_t addr = v2m->res.start + V2M_MSI_SETSPI_NS;
  85. msg->address_hi = (u32) (addr >> 32);
  86. msg->address_lo = (u32) (addr);
  87. msg->data = data->hwirq;
  88. }
  89. static struct irq_chip gicv2m_irq_chip = {
  90. .name = "GICv2m",
  91. .irq_mask = irq_chip_mask_parent,
  92. .irq_unmask = irq_chip_unmask_parent,
  93. .irq_eoi = irq_chip_eoi_parent,
  94. .irq_set_affinity = gicv2m_set_affinity,
  95. .irq_compose_msi_msg = gicv2m_compose_msi_msg,
  96. };
  97. static int gicv2m_irq_gic_domain_alloc(struct irq_domain *domain,
  98. unsigned int virq,
  99. irq_hw_number_t hwirq)
  100. {
  101. struct of_phandle_args args;
  102. struct irq_data *d;
  103. int err;
  104. args.np = domain->parent->of_node;
  105. args.args_count = 3;
  106. args.args[0] = 0;
  107. args.args[1] = hwirq - 32;
  108. args.args[2] = IRQ_TYPE_EDGE_RISING;
  109. err = irq_domain_alloc_irqs_parent(domain, virq, 1, &args);
  110. if (err)
  111. return err;
  112. /* Configure the interrupt line to be edge */
  113. d = irq_domain_get_irq_data(domain->parent, virq);
  114. d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
  115. return 0;
  116. }
  117. static void gicv2m_unalloc_msi(struct v2m_data *v2m, unsigned int hwirq)
  118. {
  119. int pos;
  120. pos = hwirq - v2m->spi_start;
  121. if (pos < 0 || pos >= v2m->nr_spis) {
  122. pr_err("Failed to teardown msi. Invalid hwirq %d\n", hwirq);
  123. return;
  124. }
  125. spin_lock(&v2m->msi_cnt_lock);
  126. __clear_bit(pos, v2m->bm);
  127. spin_unlock(&v2m->msi_cnt_lock);
  128. }
  129. static int gicv2m_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  130. unsigned int nr_irqs, void *args)
  131. {
  132. struct v2m_data *v2m = domain->host_data;
  133. int hwirq, offset, err = 0;
  134. spin_lock(&v2m->msi_cnt_lock);
  135. offset = find_first_zero_bit(v2m->bm, v2m->nr_spis);
  136. if (offset < v2m->nr_spis)
  137. __set_bit(offset, v2m->bm);
  138. else
  139. err = -ENOSPC;
  140. spin_unlock(&v2m->msi_cnt_lock);
  141. if (err)
  142. return err;
  143. hwirq = v2m->spi_start + offset;
  144. err = gicv2m_irq_gic_domain_alloc(domain, virq, hwirq);
  145. if (err) {
  146. gicv2m_unalloc_msi(v2m, hwirq);
  147. return err;
  148. }
  149. irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  150. &gicv2m_irq_chip, v2m);
  151. return 0;
  152. }
  153. static void gicv2m_irq_domain_free(struct irq_domain *domain,
  154. unsigned int virq, unsigned int nr_irqs)
  155. {
  156. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  157. struct v2m_data *v2m = irq_data_get_irq_chip_data(d);
  158. BUG_ON(nr_irqs != 1);
  159. gicv2m_unalloc_msi(v2m, d->hwirq);
  160. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  161. }
  162. static const struct irq_domain_ops gicv2m_domain_ops = {
  163. .alloc = gicv2m_irq_domain_alloc,
  164. .free = gicv2m_irq_domain_free,
  165. };
  166. static bool is_msi_spi_valid(u32 base, u32 num)
  167. {
  168. if (base < V2M_MIN_SPI) {
  169. pr_err("Invalid MSI base SPI (base:%u)\n", base);
  170. return false;
  171. }
  172. if ((num == 0) || (base + num > V2M_MAX_SPI)) {
  173. pr_err("Number of SPIs (%u) exceed maximum (%u)\n",
  174. num, V2M_MAX_SPI - V2M_MIN_SPI + 1);
  175. return false;
  176. }
  177. return true;
  178. }
  179. static int __init gicv2m_init_one(struct device_node *node,
  180. struct irq_domain *parent)
  181. {
  182. int ret;
  183. struct v2m_data *v2m;
  184. v2m = kzalloc(sizeof(struct v2m_data), GFP_KERNEL);
  185. if (!v2m) {
  186. pr_err("Failed to allocate struct v2m_data.\n");
  187. return -ENOMEM;
  188. }
  189. ret = of_address_to_resource(node, 0, &v2m->res);
  190. if (ret) {
  191. pr_err("Failed to allocate v2m resource.\n");
  192. goto err_free_v2m;
  193. }
  194. v2m->base = ioremap(v2m->res.start, resource_size(&v2m->res));
  195. if (!v2m->base) {
  196. pr_err("Failed to map GICv2m resource\n");
  197. ret = -ENOMEM;
  198. goto err_free_v2m;
  199. }
  200. if (!of_property_read_u32(node, "arm,msi-base-spi", &v2m->spi_start) &&
  201. !of_property_read_u32(node, "arm,msi-num-spis", &v2m->nr_spis)) {
  202. pr_info("Overriding V2M MSI_TYPER (base:%u, num:%u)\n",
  203. v2m->spi_start, v2m->nr_spis);
  204. } else {
  205. u32 typer = readl_relaxed(v2m->base + V2M_MSI_TYPER);
  206. v2m->spi_start = V2M_MSI_TYPER_BASE_SPI(typer);
  207. v2m->nr_spis = V2M_MSI_TYPER_NUM_SPI(typer);
  208. }
  209. if (!is_msi_spi_valid(v2m->spi_start, v2m->nr_spis)) {
  210. ret = -EINVAL;
  211. goto err_iounmap;
  212. }
  213. v2m->bm = kzalloc(sizeof(long) * BITS_TO_LONGS(v2m->nr_spis),
  214. GFP_KERNEL);
  215. if (!v2m->bm) {
  216. ret = -ENOMEM;
  217. goto err_iounmap;
  218. }
  219. v2m->domain = irq_domain_add_tree(NULL, &gicv2m_domain_ops, v2m);
  220. if (!v2m->domain) {
  221. pr_err("Failed to create GICv2m domain\n");
  222. ret = -ENOMEM;
  223. goto err_free_bm;
  224. }
  225. v2m->domain->parent = parent;
  226. v2m->mchip.of_node = node;
  227. v2m->mchip.domain = pci_msi_create_irq_domain(node,
  228. &gicv2m_msi_domain_info,
  229. v2m->domain);
  230. if (!v2m->mchip.domain) {
  231. pr_err("Failed to create MSI domain\n");
  232. ret = -ENOMEM;
  233. goto err_free_domains;
  234. }
  235. spin_lock_init(&v2m->msi_cnt_lock);
  236. ret = of_pci_msi_chip_add(&v2m->mchip);
  237. if (ret) {
  238. pr_err("Failed to add msi_chip.\n");
  239. goto err_free_domains;
  240. }
  241. pr_info("Node %s: range[%#lx:%#lx], SPI[%d:%d]\n", node->name,
  242. (unsigned long)v2m->res.start, (unsigned long)v2m->res.end,
  243. v2m->spi_start, (v2m->spi_start + v2m->nr_spis));
  244. return 0;
  245. err_free_domains:
  246. if (v2m->mchip.domain)
  247. irq_domain_remove(v2m->mchip.domain);
  248. if (v2m->domain)
  249. irq_domain_remove(v2m->domain);
  250. err_free_bm:
  251. kfree(v2m->bm);
  252. err_iounmap:
  253. iounmap(v2m->base);
  254. err_free_v2m:
  255. kfree(v2m);
  256. return ret;
  257. }
  258. static struct of_device_id gicv2m_device_id[] = {
  259. { .compatible = "arm,gic-v2m-frame", },
  260. {},
  261. };
  262. int __init gicv2m_of_init(struct device_node *node, struct irq_domain *parent)
  263. {
  264. int ret = 0;
  265. struct device_node *child;
  266. for (child = of_find_matching_node(node, gicv2m_device_id); child;
  267. child = of_find_matching_node(child, gicv2m_device_id)) {
  268. if (!of_find_property(child, "msi-controller", NULL))
  269. continue;
  270. ret = gicv2m_init_one(child, parent);
  271. if (ret) {
  272. of_node_put(node);
  273. break;
  274. }
  275. }
  276. return ret;
  277. }