irq-mvebu-odmi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (C) 2016 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. #define pr_fmt(fmt) "GIC-ODMI: " fmt
  11. #include <linux/irq.h>
  12. #include <linux/irqchip.h>
  13. #include <linux/irqdomain.h>
  14. #include <linux/kernel.h>
  15. #include <linux/msi.h>
  16. #include <linux/of_address.h>
  17. #include <linux/slab.h>
  18. #include "irq-msi-lib.h"
  19. #include <dt-bindings/interrupt-controller/arm-gic.h>
  20. #define GICP_ODMIN_SET 0x40
  21. #define GICP_ODMI_INT_NUM_SHIFT 12
  22. #define GICP_ODMIN_GM_EP_R0 0x110
  23. #define GICP_ODMIN_GM_EP_R1 0x114
  24. #define GICP_ODMIN_GM_EA_R0 0x108
  25. #define GICP_ODMIN_GM_EA_R1 0x118
  26. /*
  27. * We don't support the group events, so we simply have 8 interrupts
  28. * per frame.
  29. */
  30. #define NODMIS_SHIFT 3
  31. #define NODMIS_PER_FRAME (1 << NODMIS_SHIFT)
  32. #define NODMIS_MASK (NODMIS_PER_FRAME - 1)
  33. struct odmi_data {
  34. struct resource res;
  35. void __iomem *base;
  36. unsigned int spi_base;
  37. };
  38. static struct odmi_data *odmis;
  39. static unsigned long *odmis_bm;
  40. static unsigned int odmis_count;
  41. /* Protects odmis_bm */
  42. static DEFINE_SPINLOCK(odmis_bm_lock);
  43. static void odmi_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
  44. {
  45. struct odmi_data *odmi;
  46. phys_addr_t addr;
  47. unsigned int odmin;
  48. if (WARN_ON(d->hwirq >= odmis_count * NODMIS_PER_FRAME))
  49. return;
  50. odmi = &odmis[d->hwirq >> NODMIS_SHIFT];
  51. odmin = d->hwirq & NODMIS_MASK;
  52. addr = odmi->res.start + GICP_ODMIN_SET;
  53. msg->address_hi = upper_32_bits(addr);
  54. msg->address_lo = lower_32_bits(addr);
  55. msg->data = odmin << GICP_ODMI_INT_NUM_SHIFT;
  56. }
  57. static struct irq_chip odmi_irq_chip = {
  58. .name = "ODMI",
  59. .irq_mask = irq_chip_mask_parent,
  60. .irq_unmask = irq_chip_unmask_parent,
  61. .irq_eoi = irq_chip_eoi_parent,
  62. .irq_set_affinity = irq_chip_set_affinity_parent,
  63. .irq_compose_msi_msg = odmi_compose_msi_msg,
  64. };
  65. static int odmi_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
  66. unsigned int nr_irqs, void *args)
  67. {
  68. struct odmi_data *odmi = NULL;
  69. struct irq_fwspec fwspec;
  70. struct irq_data *d;
  71. unsigned int hwirq, odmin;
  72. int ret;
  73. spin_lock(&odmis_bm_lock);
  74. hwirq = find_first_zero_bit(odmis_bm, NODMIS_PER_FRAME * odmis_count);
  75. if (hwirq >= NODMIS_PER_FRAME * odmis_count) {
  76. spin_unlock(&odmis_bm_lock);
  77. return -ENOSPC;
  78. }
  79. __set_bit(hwirq, odmis_bm);
  80. spin_unlock(&odmis_bm_lock);
  81. odmi = &odmis[hwirq >> NODMIS_SHIFT];
  82. odmin = hwirq & NODMIS_MASK;
  83. fwspec.fwnode = domain->parent->fwnode;
  84. fwspec.param_count = 3;
  85. fwspec.param[0] = GIC_SPI;
  86. fwspec.param[1] = odmi->spi_base - 32 + odmin;
  87. fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
  88. ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  89. if (ret) {
  90. pr_err("Cannot allocate parent IRQ\n");
  91. spin_lock(&odmis_bm_lock);
  92. __clear_bit(odmin, odmis_bm);
  93. spin_unlock(&odmis_bm_lock);
  94. return ret;
  95. }
  96. /* Configure the interrupt line to be edge */
  97. d = irq_domain_get_irq_data(domain->parent, virq);
  98. d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
  99. irq_domain_set_hwirq_and_chip(domain, virq, hwirq,
  100. &odmi_irq_chip, NULL);
  101. return 0;
  102. }
  103. static void odmi_irq_domain_free(struct irq_domain *domain,
  104. unsigned int virq, unsigned int nr_irqs)
  105. {
  106. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  107. if (d->hwirq >= odmis_count * NODMIS_PER_FRAME) {
  108. pr_err("Failed to teardown msi. Invalid hwirq %lu\n", d->hwirq);
  109. return;
  110. }
  111. irq_domain_free_irqs_parent(domain, virq, nr_irqs);
  112. /* Actually free the MSI */
  113. spin_lock(&odmis_bm_lock);
  114. __clear_bit(d->hwirq, odmis_bm);
  115. spin_unlock(&odmis_bm_lock);
  116. }
  117. static const struct irq_domain_ops odmi_domain_ops = {
  118. .select = msi_lib_irq_domain_select,
  119. .alloc = odmi_irq_domain_alloc,
  120. .free = odmi_irq_domain_free,
  121. };
  122. #define ODMI_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \
  123. MSI_FLAG_USE_DEF_CHIP_OPS)
  124. #define ODMI_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK)
  125. static const struct msi_parent_ops odmi_msi_parent_ops = {
  126. .supported_flags = ODMI_MSI_FLAGS_SUPPORTED,
  127. .required_flags = ODMI_MSI_FLAGS_REQUIRED,
  128. .bus_select_token = DOMAIN_BUS_GENERIC_MSI,
  129. .bus_select_mask = MATCH_PLATFORM_MSI,
  130. .prefix = "ODMI-",
  131. .init_dev_msi_info = msi_lib_init_dev_msi_info,
  132. };
  133. static int __init mvebu_odmi_init(struct device_node *node,
  134. struct device_node *parent)
  135. {
  136. struct irq_domain *parent_domain, *inner_domain;
  137. int ret, i;
  138. if (of_property_read_u32(node, "marvell,odmi-frames", &odmis_count))
  139. return -EINVAL;
  140. odmis = kcalloc(odmis_count, sizeof(struct odmi_data), GFP_KERNEL);
  141. if (!odmis)
  142. return -ENOMEM;
  143. odmis_bm = bitmap_zalloc(odmis_count * NODMIS_PER_FRAME, GFP_KERNEL);
  144. if (!odmis_bm) {
  145. ret = -ENOMEM;
  146. goto err_alloc;
  147. }
  148. for (i = 0; i < odmis_count; i++) {
  149. struct odmi_data *odmi = &odmis[i];
  150. ret = of_address_to_resource(node, i, &odmi->res);
  151. if (ret)
  152. goto err_unmap;
  153. odmi->base = of_io_request_and_map(node, i, "odmi");
  154. if (IS_ERR(odmi->base)) {
  155. ret = PTR_ERR(odmi->base);
  156. goto err_unmap;
  157. }
  158. if (of_property_read_u32_index(node, "marvell,spi-base",
  159. i, &odmi->spi_base)) {
  160. ret = -EINVAL;
  161. goto err_unmap;
  162. }
  163. }
  164. parent_domain = irq_find_host(parent);
  165. inner_domain = irq_domain_create_hierarchy(parent_domain, 0,
  166. odmis_count * NODMIS_PER_FRAME,
  167. of_node_to_fwnode(node),
  168. &odmi_domain_ops, NULL);
  169. if (!inner_domain) {
  170. ret = -ENOMEM;
  171. goto err_unmap;
  172. }
  173. irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_GENERIC_MSI);
  174. inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_PARENT;
  175. inner_domain->msi_parent_ops = &odmi_msi_parent_ops;
  176. return 0;
  177. err_unmap:
  178. for (i = 0; i < odmis_count; i++) {
  179. struct odmi_data *odmi = &odmis[i];
  180. if (odmi->base && !IS_ERR(odmi->base))
  181. iounmap(odmis[i].base);
  182. }
  183. bitmap_free(odmis_bm);
  184. err_alloc:
  185. kfree(odmis);
  186. return ret;
  187. }
  188. IRQCHIP_DECLARE(mvebu_odmi, "marvell,odmi-controller", mvebu_odmi_init);