icp-opal.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright 2016 IBM Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/types.h>
  10. #include <linux/kernel.h>
  11. #include <linux/irq.h>
  12. #include <linux/smp.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/cpu.h>
  15. #include <linux/of.h>
  16. #include <asm/smp.h>
  17. #include <asm/irq.h>
  18. #include <asm/errno.h>
  19. #include <asm/xics.h>
  20. #include <asm/io.h>
  21. #include <asm/opal.h>
  22. #include <asm/kvm_ppc.h>
  23. static void icp_opal_teardown_cpu(void)
  24. {
  25. int hw_cpu = hard_smp_processor_id();
  26. /* Clear any pending IPI */
  27. opal_int_set_mfrr(hw_cpu, 0xff);
  28. }
  29. static void icp_opal_flush_ipi(void)
  30. {
  31. /*
  32. * We take the ipi irq but and never return so we need to EOI the IPI,
  33. * but want to leave our priority 0.
  34. *
  35. * Should we check all the other interrupts too?
  36. * Should we be flagging idle loop instead?
  37. * Or creating some task to be scheduled?
  38. */
  39. if (opal_int_eoi((0x00 << 24) | XICS_IPI) > 0)
  40. force_external_irq_replay();
  41. }
  42. static unsigned int icp_opal_get_xirr(void)
  43. {
  44. unsigned int kvm_xirr;
  45. __be32 hw_xirr;
  46. int64_t rc;
  47. /* Handle an interrupt latched by KVM first */
  48. kvm_xirr = kvmppc_get_xics_latch();
  49. if (kvm_xirr)
  50. return kvm_xirr;
  51. /* Then ask OPAL */
  52. rc = opal_int_get_xirr(&hw_xirr, false);
  53. if (rc < 0)
  54. return 0;
  55. return be32_to_cpu(hw_xirr);
  56. }
  57. static unsigned int icp_opal_get_irq(void)
  58. {
  59. unsigned int xirr;
  60. unsigned int vec;
  61. unsigned int irq;
  62. xirr = icp_opal_get_xirr();
  63. vec = xirr & 0x00ffffff;
  64. if (vec == XICS_IRQ_SPURIOUS)
  65. return 0;
  66. irq = irq_find_mapping(xics_host, vec);
  67. if (likely(irq)) {
  68. xics_push_cppr(vec);
  69. return irq;
  70. }
  71. /* We don't have a linux mapping, so have rtas mask it. */
  72. xics_mask_unknown_vec(vec);
  73. /* We might learn about it later, so EOI it */
  74. if (opal_int_eoi(xirr) > 0)
  75. force_external_irq_replay();
  76. return 0;
  77. }
  78. static void icp_opal_set_cpu_priority(unsigned char cppr)
  79. {
  80. /*
  81. * Here be dragons. The caller has asked to allow only IPI's and not
  82. * external interrupts. But OPAL XIVE doesn't support that. So instead
  83. * of allowing no interrupts allow all. That's still not right, but
  84. * currently the only caller who does this is xics_migrate_irqs_away()
  85. * and it works in that case.
  86. */
  87. if (cppr >= DEFAULT_PRIORITY)
  88. cppr = LOWEST_PRIORITY;
  89. xics_set_base_cppr(cppr);
  90. opal_int_set_cppr(cppr);
  91. iosync();
  92. }
  93. static void icp_opal_eoi(struct irq_data *d)
  94. {
  95. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  96. int64_t rc;
  97. iosync();
  98. rc = opal_int_eoi((xics_pop_cppr() << 24) | hw_irq);
  99. /*
  100. * EOI tells us whether there are more interrupts to fetch.
  101. *
  102. * Some HW implementations might not be able to send us another
  103. * external interrupt in that case, so we force a replay.
  104. */
  105. if (rc > 0)
  106. force_external_irq_replay();
  107. }
  108. #ifdef CONFIG_SMP
  109. static void icp_opal_cause_ipi(int cpu, unsigned long data)
  110. {
  111. int hw_cpu = get_hard_smp_processor_id(cpu);
  112. kvmppc_set_host_ipi(cpu, 1);
  113. opal_int_set_mfrr(hw_cpu, IPI_PRIORITY);
  114. }
  115. static irqreturn_t icp_opal_ipi_action(int irq, void *dev_id)
  116. {
  117. int cpu = smp_processor_id();
  118. kvmppc_set_host_ipi(cpu, 0);
  119. opal_int_set_mfrr(get_hard_smp_processor_id(cpu), 0xff);
  120. return smp_ipi_demux();
  121. }
  122. /*
  123. * Called when an interrupt is received on an off-line CPU to
  124. * clear the interrupt, so that the CPU can go back to nap mode.
  125. */
  126. void icp_opal_flush_interrupt(void)
  127. {
  128. unsigned int xirr;
  129. unsigned int vec;
  130. do {
  131. xirr = icp_opal_get_xirr();
  132. vec = xirr & 0x00ffffff;
  133. if (vec == XICS_IRQ_SPURIOUS)
  134. break;
  135. if (vec == XICS_IPI) {
  136. /* Clear pending IPI */
  137. int cpu = smp_processor_id();
  138. kvmppc_set_host_ipi(cpu, 0);
  139. opal_int_set_mfrr(get_hard_smp_processor_id(cpu), 0xff);
  140. } else {
  141. pr_err("XICS: hw interrupt 0x%x to offline cpu, "
  142. "disabling\n", vec);
  143. xics_mask_unknown_vec(vec);
  144. }
  145. /* EOI the interrupt */
  146. } while (opal_int_eoi(xirr) > 0);
  147. }
  148. #endif /* CONFIG_SMP */
  149. static const struct icp_ops icp_opal_ops = {
  150. .get_irq = icp_opal_get_irq,
  151. .eoi = icp_opal_eoi,
  152. .set_priority = icp_opal_set_cpu_priority,
  153. .teardown_cpu = icp_opal_teardown_cpu,
  154. .flush_ipi = icp_opal_flush_ipi,
  155. #ifdef CONFIG_SMP
  156. .ipi_action = icp_opal_ipi_action,
  157. .cause_ipi = icp_opal_cause_ipi,
  158. #endif
  159. };
  160. int icp_opal_init(void)
  161. {
  162. struct device_node *np;
  163. np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
  164. if (!np)
  165. return -ENODEV;
  166. icp_ops = &icp_opal_ops;
  167. printk("XICS: Using OPAL ICP fallbacks\n");
  168. return 0;
  169. }