icp-hv.c 3.9 KB

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