ics-rtas.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/types.h>
  3. #include <linux/kernel.h>
  4. #include <linux/irq.h>
  5. #include <linux/smp.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/init.h>
  8. #include <linux/cpu.h>
  9. #include <linux/of.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/msi.h>
  12. #include <asm/smp.h>
  13. #include <asm/machdep.h>
  14. #include <asm/irq.h>
  15. #include <asm/errno.h>
  16. #include <asm/xics.h>
  17. #include <asm/rtas.h>
  18. /* RTAS service tokens */
  19. static int ibm_get_xive;
  20. static int ibm_set_xive;
  21. static int ibm_int_on;
  22. static int ibm_int_off;
  23. static void ics_rtas_unmask_irq(struct irq_data *d)
  24. {
  25. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  26. int call_status;
  27. int server;
  28. pr_devel("xics: unmask virq %d [hw 0x%x]\n", d->irq, hw_irq);
  29. if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
  30. return;
  31. server = xics_get_irq_server(d->irq, irq_data_get_affinity_mask(d), 0);
  32. call_status = rtas_call(ibm_set_xive, 3, 1, NULL, hw_irq, server,
  33. DEFAULT_PRIORITY);
  34. if (call_status != 0) {
  35. printk(KERN_ERR
  36. "%s: ibm_set_xive irq %u server %x returned %d\n",
  37. __func__, hw_irq, server, call_status);
  38. return;
  39. }
  40. /* Now unmask the interrupt (often a no-op) */
  41. call_status = rtas_call(ibm_int_on, 1, 1, NULL, hw_irq);
  42. if (call_status != 0) {
  43. printk(KERN_ERR "%s: ibm_int_on irq=%u returned %d\n",
  44. __func__, hw_irq, call_status);
  45. return;
  46. }
  47. }
  48. static unsigned int ics_rtas_startup(struct irq_data *d)
  49. {
  50. /* unmask it */
  51. ics_rtas_unmask_irq(d);
  52. return 0;
  53. }
  54. static void ics_rtas_mask_real_irq(unsigned int hw_irq)
  55. {
  56. int call_status;
  57. if (hw_irq == XICS_IPI)
  58. return;
  59. call_status = rtas_call(ibm_int_off, 1, 1, NULL, hw_irq);
  60. if (call_status != 0) {
  61. printk(KERN_ERR "%s: ibm_int_off irq=%u returned %d\n",
  62. __func__, hw_irq, call_status);
  63. return;
  64. }
  65. /* Have to set XIVE to 0xff to be able to remove a slot */
  66. call_status = rtas_call(ibm_set_xive, 3, 1, NULL, hw_irq,
  67. xics_default_server, 0xff);
  68. if (call_status != 0) {
  69. printk(KERN_ERR "%s: ibm_set_xive(0xff) irq=%u returned %d\n",
  70. __func__, hw_irq, call_status);
  71. return;
  72. }
  73. }
  74. static void ics_rtas_mask_irq(struct irq_data *d)
  75. {
  76. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  77. pr_devel("xics: mask virq %d [hw 0x%x]\n", d->irq, hw_irq);
  78. if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
  79. return;
  80. ics_rtas_mask_real_irq(hw_irq);
  81. }
  82. static int ics_rtas_set_affinity(struct irq_data *d,
  83. const struct cpumask *cpumask,
  84. bool force)
  85. {
  86. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  87. int status;
  88. int xics_status[2];
  89. int irq_server;
  90. if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
  91. return -1;
  92. status = rtas_call(ibm_get_xive, 1, 3, xics_status, hw_irq);
  93. if (status) {
  94. printk(KERN_ERR "%s: ibm,get-xive irq=%u returns %d\n",
  95. __func__, hw_irq, status);
  96. return -1;
  97. }
  98. irq_server = xics_get_irq_server(d->irq, cpumask, 1);
  99. if (irq_server == -1) {
  100. pr_warn("%s: No online cpus in the mask %*pb for irq %d\n",
  101. __func__, cpumask_pr_args(cpumask), d->irq);
  102. return -1;
  103. }
  104. pr_debug("%s: irq %d [hw 0x%x] server: 0x%x\n", __func__, d->irq,
  105. hw_irq, irq_server);
  106. status = rtas_call(ibm_set_xive, 3, 1, NULL,
  107. hw_irq, irq_server, xics_status[1]);
  108. if (status) {
  109. printk(KERN_ERR "%s: ibm,set-xive irq=%u returns %d\n",
  110. __func__, hw_irq, status);
  111. return -1;
  112. }
  113. return IRQ_SET_MASK_OK;
  114. }
  115. static struct irq_chip ics_rtas_irq_chip = {
  116. .name = "XICS",
  117. .irq_startup = ics_rtas_startup,
  118. .irq_mask = ics_rtas_mask_irq,
  119. .irq_unmask = ics_rtas_unmask_irq,
  120. .irq_eoi = NULL, /* Patched at init time */
  121. .irq_set_affinity = ics_rtas_set_affinity,
  122. .irq_set_type = xics_set_irq_type,
  123. .irq_retrigger = xics_retrigger,
  124. };
  125. static int ics_rtas_check(struct ics *ics, unsigned int hw_irq)
  126. {
  127. int status[2];
  128. int rc;
  129. if (WARN_ON(hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS))
  130. return -EINVAL;
  131. /* Check if RTAS knows about this interrupt */
  132. rc = rtas_call(ibm_get_xive, 1, 3, status, hw_irq);
  133. if (rc)
  134. return -ENXIO;
  135. return 0;
  136. }
  137. static void ics_rtas_mask_unknown(struct ics *ics, unsigned long vec)
  138. {
  139. ics_rtas_mask_real_irq(vec);
  140. }
  141. static long ics_rtas_get_server(struct ics *ics, unsigned long vec)
  142. {
  143. int rc, status[2];
  144. rc = rtas_call(ibm_get_xive, 1, 3, status, vec);
  145. if (rc)
  146. return -1;
  147. return status[0];
  148. }
  149. static int ics_rtas_host_match(struct ics *ics, struct device_node *node)
  150. {
  151. /* IBM machines have interrupt parents of various funky types for things
  152. * like vdevices, events, etc... The trick we use here is to match
  153. * everything here except the legacy 8259 which is compatible "chrp,iic"
  154. */
  155. return !of_device_is_compatible(node, "chrp,iic");
  156. }
  157. /* Only one global & state struct ics */
  158. static struct ics ics_rtas = {
  159. .check = ics_rtas_check,
  160. .mask_unknown = ics_rtas_mask_unknown,
  161. .get_server = ics_rtas_get_server,
  162. .host_match = ics_rtas_host_match,
  163. .chip = &ics_rtas_irq_chip,
  164. };
  165. __init int ics_rtas_init(void)
  166. {
  167. ibm_get_xive = rtas_token("ibm,get-xive");
  168. ibm_set_xive = rtas_token("ibm,set-xive");
  169. ibm_int_on = rtas_token("ibm,int-on");
  170. ibm_int_off = rtas_token("ibm,int-off");
  171. /* We enable the RTAS "ICS" if RTAS is present with the
  172. * appropriate tokens
  173. */
  174. if (ibm_get_xive == RTAS_UNKNOWN_SERVICE ||
  175. ibm_set_xive == RTAS_UNKNOWN_SERVICE)
  176. return -ENODEV;
  177. /* We need to patch our irq chip's EOI to point to the
  178. * right ICP
  179. */
  180. ics_rtas_irq_chip.irq_eoi = icp_ops->eoi;
  181. /* Register ourselves */
  182. xics_register_ics(&ics_rtas);
  183. return 0;
  184. }