ics-opal.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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, irq_data_get_affinity_mask(d), 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 (irq_data_get_msi_desc(d))
  65. pci_msi_unmask_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. __be16 oserver;
  97. int16_t server;
  98. int8_t priority;
  99. int64_t rc;
  100. int wanted_server;
  101. if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
  102. return -1;
  103. rc = opal_get_xive(hw_irq, &oserver, &priority);
  104. if (rc != OPAL_SUCCESS) {
  105. pr_err("%s: opal_get_xive(irq=%d [hw 0x%x]) error %lld\n",
  106. __func__, d->irq, hw_irq, rc);
  107. return -1;
  108. }
  109. server = be16_to_cpu(oserver);
  110. wanted_server = xics_get_irq_server(d->irq, cpumask, 1);
  111. if (wanted_server < 0) {
  112. pr_warning("%s: No online cpus in the mask %*pb for irq %d\n",
  113. __func__, cpumask_pr_args(cpumask), d->irq);
  114. return -1;
  115. }
  116. server = ics_opal_mangle_server(wanted_server);
  117. pr_devel("ics-hal: set-affinity irq %d [hw 0x%x] server: 0x%x/0x%x\n",
  118. d->irq, hw_irq, wanted_server, server);
  119. rc = opal_set_xive(hw_irq, server, priority);
  120. if (rc != OPAL_SUCCESS) {
  121. pr_err("%s: opal_set_xive(irq=%d [hw 0x%x] server=%x)"
  122. " error %lld\n",
  123. __func__, d->irq, hw_irq, server, rc);
  124. return -1;
  125. }
  126. return IRQ_SET_MASK_OK;
  127. }
  128. static struct irq_chip ics_opal_irq_chip = {
  129. .name = "OPAL ICS",
  130. .irq_startup = ics_opal_startup,
  131. .irq_mask = ics_opal_mask_irq,
  132. .irq_unmask = ics_opal_unmask_irq,
  133. .irq_eoi = NULL, /* Patched at init time */
  134. .irq_set_affinity = ics_opal_set_affinity,
  135. .irq_set_type = xics_set_irq_type,
  136. .irq_retrigger = xics_retrigger,
  137. };
  138. static int ics_opal_map(struct ics *ics, unsigned int virq);
  139. static void ics_opal_mask_unknown(struct ics *ics, unsigned long vec);
  140. static long ics_opal_get_server(struct ics *ics, unsigned long vec);
  141. static int ics_opal_host_match(struct ics *ics, struct device_node *node)
  142. {
  143. return 1;
  144. }
  145. /* Only one global & state struct ics */
  146. static struct ics ics_hal = {
  147. .map = ics_opal_map,
  148. .mask_unknown = ics_opal_mask_unknown,
  149. .get_server = ics_opal_get_server,
  150. .host_match = ics_opal_host_match,
  151. };
  152. static int ics_opal_map(struct ics *ics, unsigned int virq)
  153. {
  154. unsigned int hw_irq = (unsigned int)virq_to_hw(virq);
  155. int64_t rc;
  156. __be16 server;
  157. int8_t priority;
  158. if (WARN_ON(hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS))
  159. return -EINVAL;
  160. /* Check if HAL knows about this interrupt */
  161. rc = opal_get_xive(hw_irq, &server, &priority);
  162. if (rc != OPAL_SUCCESS)
  163. return -ENXIO;
  164. irq_set_chip_and_handler(virq, &ics_opal_irq_chip, handle_fasteoi_irq);
  165. irq_set_chip_data(virq, &ics_hal);
  166. return 0;
  167. }
  168. static void ics_opal_mask_unknown(struct ics *ics, unsigned long vec)
  169. {
  170. int64_t rc;
  171. __be16 server;
  172. int8_t priority;
  173. /* Check if HAL knows about this interrupt */
  174. rc = opal_get_xive(vec, &server, &priority);
  175. if (rc != OPAL_SUCCESS)
  176. return;
  177. ics_opal_mask_real_irq(vec);
  178. }
  179. static long ics_opal_get_server(struct ics *ics, unsigned long vec)
  180. {
  181. int64_t rc;
  182. __be16 server;
  183. int8_t priority;
  184. /* Check if HAL knows about this interrupt */
  185. rc = opal_get_xive(vec, &server, &priority);
  186. if (rc != OPAL_SUCCESS)
  187. return -1;
  188. return ics_opal_unmangle_server(be16_to_cpu(server));
  189. }
  190. int __init ics_opal_init(void)
  191. {
  192. if (!firmware_has_feature(FW_FEATURE_OPAL))
  193. return -ENODEV;
  194. /* We need to patch our irq chip's EOI to point to the
  195. * right ICP
  196. */
  197. ics_opal_irq_chip.irq_eoi = icp_ops->eoi;
  198. /* Register ourselves */
  199. xics_register_ics(&ics_hal);
  200. pr_info("ICS OPAL backend registered\n");
  201. return 0;
  202. }