ics-rtas.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include <linux/types.h>
  2. #include <linux/kernel.h>
  3. #include <linux/irq.h>
  4. #include <linux/smp.h>
  5. #include <linux/interrupt.h>
  6. #include <linux/init.h>
  7. #include <linux/cpu.h>
  8. #include <linux/of.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/msi.h>
  11. #include <asm/prom.h>
  12. #include <asm/smp.h>
  13. #include <asm/machdep.h>
  14. #include <asm/irq.h>
  15. #include <asm/errno.h>
  16. #include <asm/xics.h>
  17. #include <asm/rtas.h>
  18. /* RTAS service tokens */
  19. static int ibm_get_xive;
  20. static int ibm_set_xive;
  21. static int ibm_int_on;
  22. static int ibm_int_off;
  23. static int ics_rtas_map(struct ics *ics, unsigned int virq);
  24. static void ics_rtas_mask_unknown(struct ics *ics, unsigned long vec);
  25. static long ics_rtas_get_server(struct ics *ics, unsigned long vec);
  26. static int ics_rtas_host_match(struct ics *ics, struct device_node *node);
  27. /* Only one global & state struct ics */
  28. static struct ics ics_rtas = {
  29. .map = ics_rtas_map,
  30. .mask_unknown = ics_rtas_mask_unknown,
  31. .get_server = ics_rtas_get_server,
  32. .host_match = ics_rtas_host_match,
  33. };
  34. static void ics_rtas_unmask_irq(struct irq_data *d)
  35. {
  36. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  37. int call_status;
  38. int server;
  39. pr_devel("xics: unmask virq %d [hw 0x%x]\n", d->irq, hw_irq);
  40. if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
  41. return;
  42. server = xics_get_irq_server(d->irq, irq_data_get_affinity_mask(d), 0);
  43. call_status = rtas_call(ibm_set_xive, 3, 1, NULL, hw_irq, server,
  44. DEFAULT_PRIORITY);
  45. if (call_status != 0) {
  46. printk(KERN_ERR
  47. "%s: ibm_set_xive irq %u server %x returned %d\n",
  48. __func__, hw_irq, server, call_status);
  49. return;
  50. }
  51. /* Now unmask the interrupt (often a no-op) */
  52. call_status = rtas_call(ibm_int_on, 1, 1, NULL, hw_irq);
  53. if (call_status != 0) {
  54. printk(KERN_ERR "%s: ibm_int_on irq=%u returned %d\n",
  55. __func__, hw_irq, call_status);
  56. return;
  57. }
  58. }
  59. static unsigned int ics_rtas_startup(struct irq_data *d)
  60. {
  61. #ifdef CONFIG_PCI_MSI
  62. /*
  63. * The generic MSI code returns with the interrupt disabled on the
  64. * card, using the MSI mask bits. Firmware doesn't appear to unmask
  65. * at that level, so we do it here by hand.
  66. */
  67. if (irq_data_get_msi_desc(d))
  68. pci_msi_unmask_irq(d);
  69. #endif
  70. /* unmask it */
  71. ics_rtas_unmask_irq(d);
  72. return 0;
  73. }
  74. static void ics_rtas_mask_real_irq(unsigned int hw_irq)
  75. {
  76. int call_status;
  77. if (hw_irq == XICS_IPI)
  78. return;
  79. call_status = rtas_call(ibm_int_off, 1, 1, NULL, hw_irq);
  80. if (call_status != 0) {
  81. printk(KERN_ERR "%s: ibm_int_off irq=%u returned %d\n",
  82. __func__, hw_irq, call_status);
  83. return;
  84. }
  85. /* Have to set XIVE to 0xff to be able to remove a slot */
  86. call_status = rtas_call(ibm_set_xive, 3, 1, NULL, hw_irq,
  87. xics_default_server, 0xff);
  88. if (call_status != 0) {
  89. printk(KERN_ERR "%s: ibm_set_xive(0xff) irq=%u returned %d\n",
  90. __func__, hw_irq, call_status);
  91. return;
  92. }
  93. }
  94. static void ics_rtas_mask_irq(struct irq_data *d)
  95. {
  96. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  97. pr_devel("xics: mask virq %d [hw 0x%x]\n", d->irq, hw_irq);
  98. if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
  99. return;
  100. ics_rtas_mask_real_irq(hw_irq);
  101. }
  102. static int ics_rtas_set_affinity(struct irq_data *d,
  103. const struct cpumask *cpumask,
  104. bool force)
  105. {
  106. unsigned int hw_irq = (unsigned int)irqd_to_hwirq(d);
  107. int status;
  108. int xics_status[2];
  109. int irq_server;
  110. if (hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS)
  111. return -1;
  112. status = rtas_call(ibm_get_xive, 1, 3, xics_status, hw_irq);
  113. if (status) {
  114. printk(KERN_ERR "%s: ibm,get-xive irq=%u returns %d\n",
  115. __func__, hw_irq, status);
  116. return -1;
  117. }
  118. irq_server = xics_get_irq_server(d->irq, cpumask, 1);
  119. if (irq_server == -1) {
  120. pr_warning("%s: No online cpus in the mask %*pb for irq %d\n",
  121. __func__, cpumask_pr_args(cpumask), d->irq);
  122. return -1;
  123. }
  124. status = rtas_call(ibm_set_xive, 3, 1, NULL,
  125. hw_irq, irq_server, xics_status[1]);
  126. if (status) {
  127. printk(KERN_ERR "%s: ibm,set-xive irq=%u returns %d\n",
  128. __func__, hw_irq, status);
  129. return -1;
  130. }
  131. return IRQ_SET_MASK_OK;
  132. }
  133. static struct irq_chip ics_rtas_irq_chip = {
  134. .name = "XICS",
  135. .irq_startup = ics_rtas_startup,
  136. .irq_mask = ics_rtas_mask_irq,
  137. .irq_unmask = ics_rtas_unmask_irq,
  138. .irq_eoi = NULL, /* Patched at init time */
  139. .irq_set_affinity = ics_rtas_set_affinity,
  140. .irq_set_type = xics_set_irq_type,
  141. .irq_retrigger = xics_retrigger,
  142. };
  143. static int ics_rtas_map(struct ics *ics, unsigned int virq)
  144. {
  145. unsigned int hw_irq = (unsigned int)virq_to_hw(virq);
  146. int status[2];
  147. int rc;
  148. if (WARN_ON(hw_irq == XICS_IPI || hw_irq == XICS_IRQ_SPURIOUS))
  149. return -EINVAL;
  150. /* Check if RTAS knows about this interrupt */
  151. rc = rtas_call(ibm_get_xive, 1, 3, status, hw_irq);
  152. if (rc)
  153. return -ENXIO;
  154. irq_set_chip_and_handler(virq, &ics_rtas_irq_chip, handle_fasteoi_irq);
  155. irq_set_chip_data(virq, &ics_rtas);
  156. return 0;
  157. }
  158. static void ics_rtas_mask_unknown(struct ics *ics, unsigned long vec)
  159. {
  160. ics_rtas_mask_real_irq(vec);
  161. }
  162. static long ics_rtas_get_server(struct ics *ics, unsigned long vec)
  163. {
  164. int rc, status[2];
  165. rc = rtas_call(ibm_get_xive, 1, 3, status, vec);
  166. if (rc)
  167. return -1;
  168. return status[0];
  169. }
  170. static int ics_rtas_host_match(struct ics *ics, struct device_node *node)
  171. {
  172. /* IBM machines have interrupt parents of various funky types for things
  173. * like vdevices, events, etc... The trick we use here is to match
  174. * everything here except the legacy 8259 which is compatible "chrp,iic"
  175. */
  176. return !of_device_is_compatible(node, "chrp,iic");
  177. }
  178. __init int ics_rtas_init(void)
  179. {
  180. ibm_get_xive = rtas_token("ibm,get-xive");
  181. ibm_set_xive = rtas_token("ibm,set-xive");
  182. ibm_int_on = rtas_token("ibm,int-on");
  183. ibm_int_off = rtas_token("ibm,int-off");
  184. /* We enable the RTAS "ICS" if RTAS is present with the
  185. * appropriate tokens
  186. */
  187. if (ibm_get_xive == RTAS_UNKNOWN_SERVICE ||
  188. ibm_set_xive == RTAS_UNKNOWN_SERVICE)
  189. return -ENODEV;
  190. /* We need to patch our irq chip's EOI to point to the
  191. * right ICP
  192. */
  193. ics_rtas_irq_chip.irq_eoi = icp_ops->eoi;
  194. /* Register ourselves */
  195. xics_register_ics(&ics_rtas);
  196. return 0;
  197. }