irq-mvebu-gicp.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright (C) 2017 Marvell
  3. *
  4. * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/io.h>
  11. #include <linux/irq.h>
  12. #include <linux/irqdomain.h>
  13. #include <linux/msi.h>
  14. #include <linux/of.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/platform_device.h>
  18. #include <dt-bindings/interrupt-controller/arm-gic.h>
  19. #include "irq-mvebu-gicp.h"
  20. #define GICP_SETSPI_NSR_OFFSET 0x0
  21. #define GICP_CLRSPI_NSR_OFFSET 0x8
  22. struct mvebu_gicp_spi_range {
  23. unsigned int start;
  24. unsigned int count;
  25. };
  26. struct mvebu_gicp {
  27. struct mvebu_gicp_spi_range *spi_ranges;
  28. unsigned int spi_ranges_cnt;
  29. unsigned int spi_cnt;
  30. unsigned long *spi_bitmap;
  31. spinlock_t spi_lock;
  32. struct resource *res;
  33. struct device *dev;
  34. };
  35. static int gicp_idx_to_spi(struct mvebu_gicp *gicp, int idx)
  36. {
  37. int i;
  38. for (i = 0; i < gicp->spi_ranges_cnt; i++) {
  39. struct mvebu_gicp_spi_range *r = &gicp->spi_ranges[i];
  40. if (idx < r->count)
  41. return r->start + idx;
  42. idx -= r->count;
  43. }
  44. return -EINVAL;
  45. }
  46. int mvebu_gicp_get_doorbells(struct device_node *dn, phys_addr_t *setspi,
  47. phys_addr_t *clrspi)
  48. {
  49. struct platform_device *pdev;
  50. struct mvebu_gicp *gicp;
  51. pdev = of_find_device_by_node(dn);
  52. if (!pdev)
  53. return -ENODEV;
  54. gicp = platform_get_drvdata(pdev);
  55. if (!gicp)
  56. return -ENODEV;
  57. *setspi = gicp->res->start + GICP_SETSPI_NSR_OFFSET;
  58. *clrspi = gicp->res->start + GICP_CLRSPI_NSR_OFFSET;
  59. return 0;
  60. }
  61. static void gicp_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  62. {
  63. struct mvebu_gicp *gicp = data->chip_data;
  64. phys_addr_t setspi = gicp->res->start + GICP_SETSPI_NSR_OFFSET;
  65. msg->data = data->hwirq;
  66. msg->address_lo = lower_32_bits(setspi);
  67. msg->address_hi = upper_32_bits(setspi);
  68. }
  69. static struct irq_chip gicp_irq_chip = {
  70. .name = "GICP",
  71. .irq_mask = irq_chip_mask_parent,
  72. .irq_unmask = irq_chip_unmask_parent,
  73. .irq_eoi = irq_chip_eoi_parent,
  74. .irq_set_affinity = irq_chip_set_affinity_parent,
  75. .irq_set_type = irq_chip_set_type_parent,
  76. .irq_compose_msi_msg = gicp_compose_msi_msg,
  77. };
  78. static int gicp_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  79. unsigned int nr_irqs, void *args)
  80. {
  81. struct mvebu_gicp *gicp = domain->host_data;
  82. struct irq_fwspec fwspec;
  83. unsigned int hwirq;
  84. int ret;
  85. spin_lock(&gicp->spi_lock);
  86. hwirq = find_first_zero_bit(gicp->spi_bitmap, gicp->spi_cnt);
  87. if (hwirq == gicp->spi_cnt) {
  88. spin_unlock(&gicp->spi_lock);
  89. return -ENOSPC;
  90. }
  91. __set_bit(hwirq, gicp->spi_bitmap);
  92. spin_unlock(&gicp->spi_lock);
  93. fwspec.fwnode = domain->parent->fwnode;
  94. fwspec.param_count = 3;
  95. fwspec.param[0] = GIC_SPI;
  96. fwspec.param[1] = gicp_idx_to_spi(gicp, hwirq) - 32;
  97. /*
  98. * Assume edge rising for now, it will be properly set when
  99. * ->set_type() is called
  100. */
  101. fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
  102. ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  103. if (ret) {
  104. dev_err(gicp->dev, "Cannot allocate parent IRQ\n");
  105. goto free_hwirq;
  106. }
  107. ret = irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  108. &gicp_irq_chip, gicp);
  109. if (ret)
  110. goto free_irqs_parent;
  111. return 0;
  112. free_irqs_parent:
  113. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  114. free_hwirq:
  115. spin_lock(&gicp->spi_lock);
  116. __clear_bit(hwirq, gicp->spi_bitmap);
  117. spin_unlock(&gicp->spi_lock);
  118. return ret;
  119. }
  120. static void gicp_irq_domain_free(struct irq_domain *domain,
  121. unsigned int virq, unsigned int nr_irqs)
  122. {
  123. struct mvebu_gicp *gicp = domain->host_data;
  124. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  125. if (d->hwirq >= gicp->spi_cnt) {
  126. dev_err(gicp->dev, "Invalid hwirq %lu\n", d->hwirq);
  127. return;
  128. }
  129. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  130. spin_lock(&gicp->spi_lock);
  131. __clear_bit(d->hwirq, gicp->spi_bitmap);
  132. spin_unlock(&gicp->spi_lock);
  133. }
  134. static const struct irq_domain_ops gicp_domain_ops = {
  135. .alloc = gicp_irq_domain_alloc,
  136. .free = gicp_irq_domain_free,
  137. };
  138. static struct irq_chip gicp_msi_irq_chip = {
  139. .name = "GICP",
  140. .irq_set_type = irq_chip_set_type_parent,
  141. };
  142. static struct msi_domain_ops gicp_msi_ops = {
  143. };
  144. static struct msi_domain_info gicp_msi_domain_info = {
  145. .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS),
  146. .ops = &gicp_msi_ops,
  147. .chip = &gicp_msi_irq_chip,
  148. };
  149. static int mvebu_gicp_probe(struct platform_device *pdev)
  150. {
  151. struct mvebu_gicp *gicp;
  152. struct irq_domain *inner_domain, *plat_domain, *parent_domain;
  153. struct device_node *node = pdev->dev.of_node;
  154. struct device_node *irq_parent_dn;
  155. int ret, i;
  156. gicp = devm_kzalloc(&pdev->dev, sizeof(*gicp), GFP_KERNEL);
  157. if (!gicp)
  158. return -ENOMEM;
  159. gicp->dev = &pdev->dev;
  160. gicp->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  161. if (!gicp->res)
  162. return -ENODEV;
  163. ret = of_property_count_u32_elems(node, "marvell,spi-ranges");
  164. if (ret < 0)
  165. return ret;
  166. gicp->spi_ranges_cnt = ret / 2;
  167. gicp->spi_ranges =
  168. devm_kzalloc(&pdev->dev,
  169. gicp->spi_ranges_cnt *
  170. sizeof(struct mvebu_gicp_spi_range),
  171. GFP_KERNEL);
  172. if (!gicp->spi_ranges)
  173. return -ENOMEM;
  174. for (i = 0; i < gicp->spi_ranges_cnt; i++) {
  175. of_property_read_u32_index(node, "marvell,spi-ranges",
  176. i * 2,
  177. &gicp->spi_ranges[i].start);
  178. of_property_read_u32_index(node, "marvell,spi-ranges",
  179. i * 2 + 1,
  180. &gicp->spi_ranges[i].count);
  181. gicp->spi_cnt += gicp->spi_ranges[i].count;
  182. }
  183. gicp->spi_bitmap = devm_kzalloc(&pdev->dev,
  184. BITS_TO_LONGS(gicp->spi_cnt) * sizeof(long),
  185. GFP_KERNEL);
  186. if (!gicp->spi_bitmap)
  187. return -ENOMEM;
  188. irq_parent_dn = of_irq_find_parent(node);
  189. if (!irq_parent_dn) {
  190. dev_err(&pdev->dev, "failed to find parent IRQ node\n");
  191. return -ENODEV;
  192. }
  193. parent_domain = irq_find_host(irq_parent_dn);
  194. if (!parent_domain) {
  195. dev_err(&pdev->dev, "failed to find parent IRQ domain\n");
  196. return -ENODEV;
  197. }
  198. inner_domain = irq_domain_create_hierarchy(parent_domain, 0,
  199. gicp->spi_cnt,
  200. of_node_to_fwnode(node),
  201. &gicp_domain_ops, gicp);
  202. if (!inner_domain)
  203. return -ENOMEM;
  204. plat_domain = platform_msi_create_irq_domain(of_node_to_fwnode(node),
  205. &gicp_msi_domain_info,
  206. inner_domain);
  207. if (!plat_domain) {
  208. irq_domain_remove(inner_domain);
  209. return -ENOMEM;
  210. }
  211. platform_set_drvdata(pdev, gicp);
  212. return 0;
  213. }
  214. static const struct of_device_id mvebu_gicp_of_match[] = {
  215. { .compatible = "marvell,ap806-gicp", },
  216. {},
  217. };
  218. static struct platform_driver mvebu_gicp_driver = {
  219. .probe = mvebu_gicp_probe,
  220. .driver = {
  221. .name = "mvebu-gicp",
  222. .of_match_table = mvebu_gicp_of_match,
  223. },
  224. };
  225. builtin_platform_driver(mvebu_gicp_driver);