icp-hv.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright 2011 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. */
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/irq.h>
  13. #include <linux/smp.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/init.h>
  16. #include <linux/cpu.h>
  17. #include <linux/of.h>
  18. #include <asm/smp.h>
  19. #include <asm/irq.h>
  20. #include <asm/errno.h>
  21. #include <asm/xics.h>
  22. #include <asm/io.h>
  23. #include <asm/hvcall.h>
  24. static inline unsigned int icp_hv_get_xirr(unsigned char cppr)
  25. {
  26. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  27. long rc;
  28. unsigned int ret = XICS_IRQ_SPURIOUS;
  29. rc = plpar_hcall(H_XIRR, retbuf, cppr);
  30. if (rc == H_SUCCESS) {
  31. ret = (unsigned int)retbuf[0];
  32. } else {
  33. pr_err("%s: bad return code xirr cppr=0x%x returned %ld\n",
  34. __func__, cppr, rc);
  35. WARN_ON_ONCE(1);
  36. }
  37. return ret;
  38. }
  39. static inline void icp_hv_set_cppr(u8 value)
  40. {
  41. long rc = plpar_hcall_norets(H_CPPR, value);
  42. if (rc != H_SUCCESS) {
  43. pr_err("%s: bad return code cppr cppr=0x%x returned %ld\n",
  44. __func__, value, rc);
  45. WARN_ON_ONCE(1);
  46. }
  47. }
  48. static inline void icp_hv_set_xirr(unsigned int value)
  49. {
  50. long rc = plpar_hcall_norets(H_EOI, value);
  51. if (rc != H_SUCCESS) {
  52. pr_err("%s: bad return code eoi xirr=0x%x returned %ld\n",
  53. __func__, value, rc);
  54. WARN_ON_ONCE(1);
  55. icp_hv_set_cppr(value >> 24);
  56. }
  57. }
  58. static inline void icp_hv_set_qirr(int n_cpu , u8 value)
  59. {
  60. int hw_cpu = get_hard_smp_processor_id(n_cpu);
  61. long rc = plpar_hcall_norets(H_IPI, hw_cpu, value);
  62. if (rc != H_SUCCESS) {
  63. pr_err("%s: bad return code qirr cpu=%d hw_cpu=%d mfrr=0x%x "
  64. "returned %ld\n", __func__, n_cpu, hw_cpu, value, rc);
  65. WARN_ON_ONCE(1);
  66. }
  67. }
  68. static void icp_hv_eoi(struct irq_data *d)
  69. {
  70. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  71. iosync();
  72. icp_hv_set_xirr((xics_pop_cppr() << 24) | hw_irq);
  73. }
  74. static void icp_hv_teardown_cpu(void)
  75. {
  76. int cpu = smp_processor_id();
  77. /* Clear any pending IPI */
  78. icp_hv_set_qirr(cpu, 0xff);
  79. }
  80. static void icp_hv_flush_ipi(void)
  81. {
  82. /* We take the ipi irq but and never return so we
  83. * need to EOI the IPI, but want to leave our priority 0
  84. *
  85. * should we check all the other interrupts too?
  86. * should we be flagging idle loop instead?
  87. * or creating some task to be scheduled?
  88. */
  89. icp_hv_set_xirr((0x00 << 24) | XICS_IPI);
  90. }
  91. static unsigned int icp_hv_get_irq(void)
  92. {
  93. unsigned int xirr = icp_hv_get_xirr(xics_cppr_top());
  94. unsigned int vec = xirr & 0x00ffffff;
  95. unsigned int irq;
  96. if (vec == XICS_IRQ_SPURIOUS)
  97. return NO_IRQ;
  98. irq = irq_radix_revmap_lookup(xics_host, vec);
  99. if (likely(irq != NO_IRQ)) {
  100. xics_push_cppr(vec);
  101. return irq;
  102. }
  103. /* We don't have a linux mapping, so have rtas mask it. */
  104. xics_mask_unknown_vec(vec);
  105. /* We might learn about it later, so EOI it */
  106. icp_hv_set_xirr(xirr);
  107. return NO_IRQ;
  108. }
  109. static void icp_hv_set_cpu_priority(unsigned char cppr)
  110. {
  111. xics_set_base_cppr(cppr);
  112. icp_hv_set_cppr(cppr);
  113. iosync();
  114. }
  115. #ifdef CONFIG_SMP
  116. static void icp_hv_cause_ipi(int cpu, unsigned long data)
  117. {
  118. icp_hv_set_qirr(cpu, IPI_PRIORITY);
  119. }
  120. static irqreturn_t icp_hv_ipi_action(int irq, void *dev_id)
  121. {
  122. int cpu = smp_processor_id();
  123. icp_hv_set_qirr(cpu, 0xff);
  124. return smp_ipi_demux();
  125. }
  126. #endif /* CONFIG_SMP */
  127. static const struct icp_ops icp_hv_ops = {
  128. .get_irq = icp_hv_get_irq,
  129. .eoi = icp_hv_eoi,
  130. .set_priority = icp_hv_set_cpu_priority,
  131. .teardown_cpu = icp_hv_teardown_cpu,
  132. .flush_ipi = icp_hv_flush_ipi,
  133. #ifdef CONFIG_SMP
  134. .ipi_action = icp_hv_ipi_action,
  135. .cause_ipi = icp_hv_cause_ipi,
  136. #endif
  137. };
  138. int icp_hv_init(void)
  139. {
  140. struct device_node *np;
  141. np = of_find_compatible_node(NULL, NULL, "ibm,ppc-xicp");
  142. if (!np)
  143. np = of_find_node_by_type(NULL,
  144. "PowerPC-External-Interrupt-Presentation");
  145. if (!np)
  146. return -ENODEV;
  147. icp_ops = &icp_hv_ops;
  148. return 0;
  149. }