icp-opal.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. xics_set_base_cppr(cppr);
  81. opal_int_set_cppr(cppr);
  82. iosync();
  83. }
  84. static void icp_opal_eoi(struct irq_data *d)
  85. {
  86. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  87. int64_t rc;
  88. iosync();
  89. rc = opal_int_eoi((xics_pop_cppr() << 24) | hw_irq);
  90. /*
  91. * EOI tells us whether there are more interrupts to fetch.
  92. *
  93. * Some HW implementations might not be able to send us another
  94. * external interrupt in that case, so we force a replay.
  95. */
  96. if (rc > 0)
  97. force_external_irq_replay();
  98. }
  99. #ifdef CONFIG_SMP
  100. static void icp_opal_cause_ipi(int cpu, unsigned long data)
  101. {
  102. int hw_cpu = get_hard_smp_processor_id(cpu);
  103. opal_int_set_mfrr(hw_cpu, IPI_PRIORITY);
  104. }
  105. static irqreturn_t icp_opal_ipi_action(int irq, void *dev_id)
  106. {
  107. int hw_cpu = hard_smp_processor_id();
  108. opal_int_set_mfrr(hw_cpu, 0xff);
  109. return smp_ipi_demux();
  110. }
  111. #endif /* CONFIG_SMP */
  112. static const struct icp_ops icp_opal_ops = {
  113. .get_irq = icp_opal_get_irq,
  114. .eoi = icp_opal_eoi,
  115. .set_priority = icp_opal_set_cpu_priority,
  116. .teardown_cpu = icp_opal_teardown_cpu,
  117. .flush_ipi = icp_opal_flush_ipi,
  118. #ifdef CONFIG_SMP
  119. .ipi_action = icp_opal_ipi_action,
  120. .cause_ipi = icp_opal_cause_ipi,
  121. #endif
  122. };
  123. int icp_opal_init(void)
  124. {
  125. struct device_node *np;
  126. np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
  127. if (!np)
  128. return -ENODEV;
  129. icp_ops = &icp_opal_ops;
  130. printk("XICS: Using OPAL ICP fallbacks\n");
  131. return 0;
  132. }