ics-rtas.c 5.9 KB

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