uv_irq.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * SGI UV IRQ functions
  7. *
  8. * Copyright (C) 2008 Silicon Graphics, Inc. All rights reserved.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/rbtree.h>
  12. #include <linux/slab.h>
  13. #include <linux/irq.h>
  14. #include <asm/apic.h>
  15. #include <asm/uv/uv_irq.h>
  16. #include <asm/uv/uv_hub.h>
  17. /* MMR offset and pnode of hub sourcing interrupts for a given irq */
  18. struct uv_irq_2_mmr_pnode{
  19. struct rb_node list;
  20. unsigned long offset;
  21. int pnode;
  22. int irq;
  23. };
  24. static DEFINE_SPINLOCK(uv_irq_lock);
  25. static struct rb_root uv_irq_root;
  26. static int uv_set_irq_affinity(struct irq_data *, const struct cpumask *, bool);
  27. static void uv_noop(struct irq_data *data) { }
  28. static void uv_ack_apic(struct irq_data *data)
  29. {
  30. ack_APIC_irq();
  31. }
  32. static struct irq_chip uv_irq_chip = {
  33. .name = "UV-CORE",
  34. .irq_mask = uv_noop,
  35. .irq_unmask = uv_noop,
  36. .irq_eoi = uv_ack_apic,
  37. .irq_set_affinity = uv_set_irq_affinity,
  38. };
  39. /*
  40. * Add offset and pnode information of the hub sourcing interrupts to the
  41. * rb tree for a specific irq.
  42. */
  43. static int uv_set_irq_2_mmr_info(int irq, unsigned long offset, unsigned blade)
  44. {
  45. struct rb_node **link = &uv_irq_root.rb_node;
  46. struct rb_node *parent = NULL;
  47. struct uv_irq_2_mmr_pnode *n;
  48. struct uv_irq_2_mmr_pnode *e;
  49. unsigned long irqflags;
  50. n = kmalloc_node(sizeof(struct uv_irq_2_mmr_pnode), GFP_KERNEL,
  51. uv_blade_to_memory_nid(blade));
  52. if (!n)
  53. return -ENOMEM;
  54. n->irq = irq;
  55. n->offset = offset;
  56. n->pnode = uv_blade_to_pnode(blade);
  57. spin_lock_irqsave(&uv_irq_lock, irqflags);
  58. /* Find the right place in the rbtree: */
  59. while (*link) {
  60. parent = *link;
  61. e = rb_entry(parent, struct uv_irq_2_mmr_pnode, list);
  62. if (unlikely(irq == e->irq)) {
  63. /* irq entry exists */
  64. e->pnode = uv_blade_to_pnode(blade);
  65. e->offset = offset;
  66. spin_unlock_irqrestore(&uv_irq_lock, irqflags);
  67. kfree(n);
  68. return 0;
  69. }
  70. if (irq < e->irq)
  71. link = &(*link)->rb_left;
  72. else
  73. link = &(*link)->rb_right;
  74. }
  75. /* Insert the node into the rbtree. */
  76. rb_link_node(&n->list, parent, link);
  77. rb_insert_color(&n->list, &uv_irq_root);
  78. spin_unlock_irqrestore(&uv_irq_lock, irqflags);
  79. return 0;
  80. }
  81. /* Retrieve offset and pnode information from the rb tree for a specific irq */
  82. int uv_irq_2_mmr_info(int irq, unsigned long *offset, int *pnode)
  83. {
  84. struct uv_irq_2_mmr_pnode *e;
  85. struct rb_node *n;
  86. unsigned long irqflags;
  87. spin_lock_irqsave(&uv_irq_lock, irqflags);
  88. n = uv_irq_root.rb_node;
  89. while (n) {
  90. e = rb_entry(n, struct uv_irq_2_mmr_pnode, list);
  91. if (e->irq == irq) {
  92. *offset = e->offset;
  93. *pnode = e->pnode;
  94. spin_unlock_irqrestore(&uv_irq_lock, irqflags);
  95. return 0;
  96. }
  97. if (irq < e->irq)
  98. n = n->rb_left;
  99. else
  100. n = n->rb_right;
  101. }
  102. spin_unlock_irqrestore(&uv_irq_lock, irqflags);
  103. return -1;
  104. }
  105. /*
  106. * Re-target the irq to the specified CPU and enable the specified MMR located
  107. * on the specified blade to allow the sending of MSIs to the specified CPU.
  108. */
  109. static int
  110. arch_enable_uv_irq(char *irq_name, unsigned int irq, int cpu, int mmr_blade,
  111. unsigned long mmr_offset, int limit)
  112. {
  113. const struct cpumask *eligible_cpu = cpumask_of(cpu);
  114. struct irq_cfg *cfg = irq_get_chip_data(irq);
  115. unsigned long mmr_value;
  116. struct uv_IO_APIC_route_entry *entry;
  117. int mmr_pnode, err;
  118. unsigned int dest;
  119. BUILD_BUG_ON(sizeof(struct uv_IO_APIC_route_entry) !=
  120. sizeof(unsigned long));
  121. err = assign_irq_vector(irq, cfg, eligible_cpu);
  122. if (err != 0)
  123. return err;
  124. err = apic->cpu_mask_to_apicid_and(eligible_cpu, eligible_cpu, &dest);
  125. if (err != 0)
  126. return err;
  127. if (limit == UV_AFFINITY_CPU)
  128. irq_set_status_flags(irq, IRQ_NO_BALANCING);
  129. else
  130. irq_set_status_flags(irq, IRQ_MOVE_PCNTXT);
  131. irq_set_chip_and_handler_name(irq, &uv_irq_chip, handle_percpu_irq,
  132. irq_name);
  133. mmr_value = 0;
  134. entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
  135. entry->vector = cfg->vector;
  136. entry->delivery_mode = apic->irq_delivery_mode;
  137. entry->dest_mode = apic->irq_dest_mode;
  138. entry->polarity = 0;
  139. entry->trigger = 0;
  140. entry->mask = 0;
  141. entry->dest = dest;
  142. mmr_pnode = uv_blade_to_pnode(mmr_blade);
  143. uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
  144. if (cfg->move_in_progress)
  145. send_cleanup_vector(cfg);
  146. return irq;
  147. }
  148. /*
  149. * Disable the specified MMR located on the specified blade so that MSIs are
  150. * longer allowed to be sent.
  151. */
  152. static void arch_disable_uv_irq(int mmr_pnode, unsigned long mmr_offset)
  153. {
  154. unsigned long mmr_value;
  155. struct uv_IO_APIC_route_entry *entry;
  156. BUILD_BUG_ON(sizeof(struct uv_IO_APIC_route_entry) !=
  157. sizeof(unsigned long));
  158. mmr_value = 0;
  159. entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
  160. entry->mask = 1;
  161. uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
  162. }
  163. static int
  164. uv_set_irq_affinity(struct irq_data *data, const struct cpumask *mask,
  165. bool force)
  166. {
  167. struct irq_cfg *cfg = data->chip_data;
  168. unsigned int dest;
  169. unsigned long mmr_value, mmr_offset;
  170. struct uv_IO_APIC_route_entry *entry;
  171. int mmr_pnode;
  172. if (__ioapic_set_affinity(data, mask, &dest))
  173. return -1;
  174. mmr_value = 0;
  175. entry = (struct uv_IO_APIC_route_entry *)&mmr_value;
  176. entry->vector = cfg->vector;
  177. entry->delivery_mode = apic->irq_delivery_mode;
  178. entry->dest_mode = apic->irq_dest_mode;
  179. entry->polarity = 0;
  180. entry->trigger = 0;
  181. entry->mask = 0;
  182. entry->dest = dest;
  183. /* Get previously stored MMR and pnode of hub sourcing interrupts */
  184. if (uv_irq_2_mmr_info(data->irq, &mmr_offset, &mmr_pnode))
  185. return -1;
  186. uv_write_global_mmr64(mmr_pnode, mmr_offset, mmr_value);
  187. if (cfg->move_in_progress)
  188. send_cleanup_vector(cfg);
  189. return IRQ_SET_MASK_OK_NOCOPY;
  190. }
  191. /*
  192. * Set up a mapping of an available irq and vector, and enable the specified
  193. * MMR that defines the MSI that is to be sent to the specified CPU when an
  194. * interrupt is raised.
  195. */
  196. int uv_setup_irq(char *irq_name, int cpu, int mmr_blade,
  197. unsigned long mmr_offset, int limit)
  198. {
  199. int irq, ret;
  200. irq = create_irq_nr(NR_IRQS_LEGACY, uv_blade_to_memory_nid(mmr_blade));
  201. if (irq <= 0)
  202. return -EBUSY;
  203. ret = arch_enable_uv_irq(irq_name, irq, cpu, mmr_blade, mmr_offset,
  204. limit);
  205. if (ret == irq)
  206. uv_set_irq_2_mmr_info(irq, mmr_offset, mmr_blade);
  207. else
  208. destroy_irq(irq);
  209. return ret;
  210. }
  211. EXPORT_SYMBOL_GPL(uv_setup_irq);
  212. /*
  213. * Tear down a mapping of an irq and vector, and disable the specified MMR that
  214. * defined the MSI that was to be sent to the specified CPU when an interrupt
  215. * was raised.
  216. *
  217. * Set mmr_blade and mmr_offset to what was passed in on uv_setup_irq().
  218. */
  219. void uv_teardown_irq(unsigned int irq)
  220. {
  221. struct uv_irq_2_mmr_pnode *e;
  222. struct rb_node *n;
  223. unsigned long irqflags;
  224. spin_lock_irqsave(&uv_irq_lock, irqflags);
  225. n = uv_irq_root.rb_node;
  226. while (n) {
  227. e = rb_entry(n, struct uv_irq_2_mmr_pnode, list);
  228. if (e->irq == irq) {
  229. arch_disable_uv_irq(e->pnode, e->offset);
  230. rb_erase(n, &uv_irq_root);
  231. kfree(e);
  232. break;
  233. }
  234. if (irq < e->irq)
  235. n = n->rb_left;
  236. else
  237. n = n->rb_right;
  238. }
  239. spin_unlock_irqrestore(&uv_irq_lock, irqflags);
  240. destroy_irq(irq);
  241. }
  242. EXPORT_SYMBOL_GPL(uv_teardown_irq);