ics-opal.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * ICS backend for OPAL managed interrupts.
  3. *
  4. * Copyright 2011 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #undef DEBUG
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/irq.h>
  15. #include <linux/smp.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/init.h>
  18. #include <linux/cpu.h>
  19. #include <linux/of.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/msi.h>
  22. #include <asm/prom.h>
  23. #include <asm/smp.h>
  24. #include <asm/machdep.h>
  25. #include <asm/irq.h>
  26. #include <asm/errno.h>
  27. #include <asm/xics.h>
  28. #include <asm/opal.h>
  29. #include <asm/firmware.h>
  30. static int ics_opal_mangle_server(int server)
  31. {
  32. /* No link for now */
  33. return server << 2;
  34. }
  35. static int ics_opal_unmangle_server(int server)
  36. {
  37. /* No link for now */
  38. return server >> 2;
  39. }
  40. static void ics_opal_unmask_irq(struct irq_data *d)
  41. {
  42. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  43. int64_t rc;
  44. int server;
  45. pr_devel("ics-hal: unmask virq %d [hw 0x%x]\n", d->irq, hw_irq);
  46. if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
  47. return;
  48. server = xics_get_irq_server(d->irq, d->affinity, 0);
  49. server = ics_opal_mangle_server(server);
  50. rc = opal_set_xive(hw_irq, server, DEFAULT_PRIORITY);
  51. if (rc != OPAL_SUCCESS)
  52. pr_err("%s: opal_set_xive(irq=%d [hw 0x%x] server=%x)"
  53. " error %lld\n",
  54. __func__, d->irq, hw_irq, server, rc);
  55. }
  56. static unsigned int ics_opal_startup(struct irq_data *d)
  57. {
  58. #ifdef CONFIG_PCI_MSI
  59. /*
  60. * The generic MSI code returns with the interrupt disabled on the
  61. * card, using the MSI mask bits. Firmware doesn't appear to unmask
  62. * at that level, so we do it here by hand.
  63. */
  64. if (d->msi_desc)
  65. unmask_msi_irq(d);
  66. #endif
  67. /* unmask it */
  68. ics_opal_unmask_irq(d);
  69. return 0;
  70. }
  71. static void ics_opal_mask_real_irq(unsigned int hw_irq)
  72. {
  73. int server = ics_opal_mangle_server(xics_default_server);
  74. int64_t rc;
  75. if (hw_irq == XICS_IPI)
  76. return;
  77. /* Have to set XIVE to 0xff to be able to remove a slot */
  78. rc = opal_set_xive(hw_irq, server, 0xff);
  79. if (rc != OPAL_SUCCESS)
  80. pr_err("%s: opal_set_xive(0xff) irq=%u returned %lld\n",
  81. __func__, hw_irq, rc);
  82. }
  83. static void ics_opal_mask_irq(struct irq_data *d)
  84. {
  85. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  86. pr_devel("ics-hal: mask virq %d [hw 0x%x]\n", d->irq, hw_irq);
  87. if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
  88. return;
  89. ics_opal_mask_real_irq(hw_irq);
  90. }
  91. static int ics_opal_set_affinity(struct irq_data *d,
  92. const struct cpumask *cpumask,
  93. bool force)
  94. {
  95. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  96. int16_t server;
  97. int8_t priority;
  98. int64_t rc;
  99. int wanted_server;
  100. if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
  101. return -1;
  102. rc = opal_get_xive(hw_irq, &server, &priority);
  103. if (rc != OPAL_SUCCESS) {
  104. pr_err("%s: opal_set_xive(irq=%d [hw 0x%x] server=%x)"
  105. " error %lld\n",
  106. __func__, d->irq, hw_irq, server, rc);
  107. return -1;
  108. }
  109. wanted_server = xics_get_irq_server(d->irq, cpumask, 1);
  110. if (wanted_server < 0) {
  111. char cpulist[128];
  112. cpumask_scnprintf(cpulist, sizeof(cpulist), cpumask);
  113. pr_warning("%s: No online cpus in the mask %s for irq %d\n",
  114. __func__, cpulist, d->irq);
  115. return -1;
  116. }
  117. server = ics_opal_mangle_server(wanted_server);
  118. pr_devel("ics-hal: set-affinity irq %d [hw 0x%x] server: 0x%x/0x%x\n",
  119. d->irq, hw_irq, wanted_server, server);
  120. rc = opal_set_xive(hw_irq, server, priority);
  121. if (rc != OPAL_SUCCESS) {
  122. pr_err("%s: opal_set_xive(irq=%d [hw 0x%x] server=%x)"
  123. " error %lld\n",
  124. __func__, d->irq, hw_irq, server, rc);
  125. return -1;
  126. }
  127. return 0;
  128. }
  129. static struct irq_chip ics_opal_irq_chip = {
  130. .name = "OPAL ICS",
  131. .irq_startup = ics_opal_startup,
  132. .irq_mask = ics_opal_mask_irq,
  133. .irq_unmask = ics_opal_unmask_irq,
  134. .irq_eoi = NULL, /* Patched at init time */
  135. .irq_set_affinity = ics_opal_set_affinity
  136. };
  137. static int ics_opal_map(struct ics *ics, unsigned int virq);
  138. static void ics_opal_mask_unknown(struct ics *ics, unsigned long vec);
  139. static long ics_opal_get_server(struct ics *ics, unsigned long vec);
  140. static int ics_opal_host_match(struct ics *ics, struct device_node *node)
  141. {
  142. return 1;
  143. }
  144. /* Only one global & state struct ics */
  145. static struct ics ics_hal = {
  146. .map = ics_opal_map,
  147. .mask_unknown = ics_opal_mask_unknown,
  148. .get_server = ics_opal_get_server,
  149. .host_match = ics_opal_host_match,
  150. };
  151. static int ics_opal_map(struct ics *ics, unsigned int virq)
  152. {
  153. unsigned int hw_irq = (unsigned int)virq_to_hw(virq);
  154. int64_t rc;
  155. int16_t server;
  156. int8_t priority;
  157. if (WARN_ON(hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS))
  158. return -EINVAL;
  159. /* Check if HAL knows about this interrupt */
  160. rc = opal_get_xive(hw_irq, &server, &priority);
  161. if (rc != OPAL_SUCCESS)
  162. return -ENXIO;
  163. irq_set_chip_and_handler(virq, &ics_opal_irq_chip, handle_fasteoi_irq);
  164. irq_set_chip_data(virq, &ics_hal);
  165. return 0;
  166. }
  167. static void ics_opal_mask_unknown(struct ics *ics, unsigned long vec)
  168. {
  169. int64_t rc;
  170. int16_t server;
  171. int8_t priority;
  172. /* Check if HAL knows about this interrupt */
  173. rc = opal_get_xive(vec, &server, &priority);
  174. if (rc != OPAL_SUCCESS)
  175. return;
  176. ics_opal_mask_real_irq(vec);
  177. }
  178. static long ics_opal_get_server(struct ics *ics, unsigned long vec)
  179. {
  180. int64_t rc;
  181. int16_t server;
  182. int8_t priority;
  183. /* Check if HAL knows about this interrupt */
  184. rc = opal_get_xive(vec, &server, &priority);
  185. if (rc != OPAL_SUCCESS)
  186. return -1;
  187. return ics_opal_unmangle_server(server);
  188. }
  189. int __init ics_opal_init(void)
  190. {
  191. if (!firmware_has_feature(FW_FEATURE_OPAL))
  192. return -ENODEV;
  193. /* We need to patch our irq chip's EOI to point to the
  194. * right ICP
  195. */
  196. ics_opal_irq_chip.irq_eoi = icp_ops->eoi;
  197. /* Register ourselves */
  198. xics_register_ics(&ics_hal);
  199. pr_info("ICS OPAL backend registered\n");
  200. return 0;
  201. }