ics-opal.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * ICS backend for OPAL managed interrupts.
  4. *
  5. * Copyright 2011 IBM Corp.
  6. */
  7. #undef DEBUG
  8. #include <linux/types.h>
  9. #include <linux/kernel.h>
  10. #include <linux/irq.h>
  11. #include <linux/smp.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/init.h>
  14. #include <linux/cpu.h>
  15. #include <linux/of.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/msi.h>
  18. #include <asm/smp.h>
  19. #include <asm/machdep.h>
  20. #include <asm/irq.h>
  21. #include <asm/errno.h>
  22. #include <asm/xics.h>
  23. #include <asm/opal.h>
  24. #include <asm/firmware.h>
  25. static int ics_opal_mangle_server(int server)
  26. {
  27. /* No link for now */
  28. return server << 2;
  29. }
  30. static int ics_opal_unmangle_server(int server)
  31. {
  32. /* No link for now */
  33. return server >> 2;
  34. }
  35. static void ics_opal_unmask_irq(struct irq_data *d)
  36. {
  37. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  38. int64_t rc;
  39. int server;
  40. pr_devel("ics-hal: unmask virq %d [hw 0x%x]\n", d->irq, hw_irq);
  41. if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
  42. return;
  43. server = xics_get_irq_server(d->irq, irq_data_get_affinity_mask(d), 0);
  44. server = ics_opal_mangle_server(server);
  45. rc = opal_set_xive(hw_irq, server, DEFAULT_PRIORITY);
  46. if (rc != OPAL_SUCCESS)
  47. pr_err("%s: opal_set_xive(irq=%d [hw 0x%x] server=%x)"
  48. " error %lld\n",
  49. __func__, d->irq, hw_irq, server, rc);
  50. }
  51. static unsigned int ics_opal_startup(struct irq_data *d)
  52. {
  53. ics_opal_unmask_irq(d);
  54. return 0;
  55. }
  56. static void ics_opal_mask_real_irq(unsigned int hw_irq)
  57. {
  58. int server = ics_opal_mangle_server(xics_default_server);
  59. int64_t rc;
  60. if (hw_irq == XICS_IPI)
  61. return;
  62. /* Have to set XIVE to 0xff to be able to remove a slot */
  63. rc = opal_set_xive(hw_irq, server, 0xff);
  64. if (rc != OPAL_SUCCESS)
  65. pr_err("%s: opal_set_xive(0xff) irq=%u returned %lld\n",
  66. __func__, hw_irq, rc);
  67. }
  68. static void ics_opal_mask_irq(struct irq_data *d)
  69. {
  70. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  71. pr_devel("ics-hal: mask virq %d [hw 0x%x]\n", d->irq, hw_irq);
  72. if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
  73. return;
  74. ics_opal_mask_real_irq(hw_irq);
  75. }
  76. static int ics_opal_set_affinity(struct irq_data *d,
  77. const struct cpumask *cpumask,
  78. bool force)
  79. {
  80. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  81. __be16 oserver;
  82. int16_t server;
  83. int8_t priority;
  84. int64_t rc;
  85. int wanted_server;
  86. if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
  87. return -1;
  88. rc = opal_get_xive(hw_irq, &oserver, &priority);
  89. if (rc != OPAL_SUCCESS) {
  90. pr_err("%s: opal_get_xive(irq=%d [hw 0x%x]) error %lld\n",
  91. __func__, d->irq, hw_irq, rc);
  92. return -1;
  93. }
  94. server = be16_to_cpu(oserver);
  95. wanted_server = xics_get_irq_server(d->irq, cpumask, 1);
  96. if (wanted_server < 0) {
  97. pr_warn("%s: No online cpus in the mask %*pb for irq %d\n",
  98. __func__, cpumask_pr_args(cpumask), d->irq);
  99. return -1;
  100. }
  101. server = ics_opal_mangle_server(wanted_server);
  102. pr_debug("ics-hal: set-affinity irq %d [hw 0x%x] server: 0x%x/0x%x\n",
  103. d->irq, hw_irq, wanted_server, server);
  104. rc = opal_set_xive(hw_irq, server, priority);
  105. if (rc != OPAL_SUCCESS) {
  106. pr_err("%s: opal_set_xive(irq=%d [hw 0x%x] server=%x)"
  107. " error %lld\n",
  108. __func__, d->irq, hw_irq, server, rc);
  109. return -1;
  110. }
  111. return IRQ_SET_MASK_OK;
  112. }
  113. static struct irq_chip ics_opal_irq_chip = {
  114. .name = "OPAL ICS",
  115. .irq_startup = ics_opal_startup,
  116. .irq_mask = ics_opal_mask_irq,
  117. .irq_unmask = ics_opal_unmask_irq,
  118. .irq_eoi = NULL, /* Patched at init time */
  119. .irq_set_affinity = ics_opal_set_affinity,
  120. .irq_set_type = xics_set_irq_type,
  121. .irq_retrigger = xics_retrigger,
  122. };
  123. static int ics_opal_host_match(struct ics *ics, struct device_node *node)
  124. {
  125. return 1;
  126. }
  127. static int ics_opal_check(struct ics *ics, unsigned int hw_irq)
  128. {
  129. int64_t rc;
  130. __be16 server;
  131. int8_t priority;
  132. if (WARN_ON(hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS))
  133. return -EINVAL;
  134. /* Check if HAL knows about this interrupt */
  135. rc = opal_get_xive(hw_irq, &server, &priority);
  136. if (rc != OPAL_SUCCESS)
  137. return -ENXIO;
  138. return 0;
  139. }
  140. static void ics_opal_mask_unknown(struct ics *ics, unsigned long vec)
  141. {
  142. int64_t rc;
  143. __be16 server;
  144. int8_t priority;
  145. /* Check if HAL knows about this interrupt */
  146. rc = opal_get_xive(vec, &server, &priority);
  147. if (rc != OPAL_SUCCESS)
  148. return;
  149. ics_opal_mask_real_irq(vec);
  150. }
  151. static long ics_opal_get_server(struct ics *ics, unsigned long vec)
  152. {
  153. int64_t rc;
  154. __be16 server;
  155. int8_t priority;
  156. /* Check if HAL knows about this interrupt */
  157. rc = opal_get_xive(vec, &server, &priority);
  158. if (rc != OPAL_SUCCESS)
  159. return -1;
  160. return ics_opal_unmangle_server(be16_to_cpu(server));
  161. }
  162. /* Only one global & state struct ics */
  163. static struct ics ics_hal = {
  164. .check = ics_opal_check,
  165. .mask_unknown = ics_opal_mask_unknown,
  166. .get_server = ics_opal_get_server,
  167. .host_match = ics_opal_host_match,
  168. .chip = &ics_opal_irq_chip,
  169. };
  170. int __init ics_opal_init(void)
  171. {
  172. if (!firmware_has_feature(FW_FEATURE_OPAL))
  173. return -ENODEV;
  174. /* We need to patch our irq chip's EOI to point to the
  175. * right ICP
  176. */
  177. ics_opal_irq_chip.irq_eoi = icp_ops->eoi;
  178. /* Register ourselves */
  179. xics_register_ics(&ics_hal);
  180. pr_info("ICS OPAL backend registered\n");
  181. return 0;
  182. }