smp.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  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, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. *
  14. * TILE SMP support routines.
  15. */
  16. #include <linux/smp.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/io.h>
  19. #include <linux/irq.h>
  20. #include <linux/irq_work.h>
  21. #include <linux/module.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/homecache.h>
  24. /*
  25. * We write to width and height with a single store in head_NN.S,
  26. * so make the variable aligned to "long".
  27. */
  28. HV_Topology smp_topology __write_once __aligned(sizeof(long));
  29. EXPORT_SYMBOL(smp_topology);
  30. #if CHIP_HAS_IPI()
  31. static unsigned long __iomem *ipi_mappings[NR_CPUS];
  32. #endif
  33. /* Does messaging work correctly to the local cpu? */
  34. bool self_interrupt_ok;
  35. /*
  36. * Top-level send_IPI*() functions to send messages to other cpus.
  37. */
  38. /* Set by smp_send_stop() to avoid recursive panics. */
  39. static int stopping_cpus;
  40. static void __send_IPI_many(HV_Recipient *recip, int nrecip, int tag)
  41. {
  42. int sent = 0;
  43. while (sent < nrecip) {
  44. int rc = hv_send_message(recip, nrecip,
  45. (HV_VirtAddr)&tag, sizeof(tag));
  46. if (rc < 0) {
  47. if (!stopping_cpus) /* avoid recursive panic */
  48. panic("hv_send_message returned %d", rc);
  49. break;
  50. }
  51. WARN_ONCE(rc == 0, "hv_send_message() returned zero\n");
  52. sent += rc;
  53. }
  54. }
  55. void send_IPI_single(int cpu, int tag)
  56. {
  57. HV_Recipient recip = {
  58. .y = cpu / smp_width,
  59. .x = cpu % smp_width,
  60. .state = HV_TO_BE_SENT
  61. };
  62. __send_IPI_many(&recip, 1, tag);
  63. }
  64. void send_IPI_many(const struct cpumask *mask, int tag)
  65. {
  66. HV_Recipient recip[NR_CPUS];
  67. int cpu;
  68. int nrecip = 0;
  69. int my_cpu = smp_processor_id();
  70. for_each_cpu(cpu, mask) {
  71. HV_Recipient *r;
  72. BUG_ON(cpu == my_cpu);
  73. r = &recip[nrecip++];
  74. r->y = cpu / smp_width;
  75. r->x = cpu % smp_width;
  76. r->state = HV_TO_BE_SENT;
  77. }
  78. __send_IPI_many(recip, nrecip, tag);
  79. }
  80. void send_IPI_allbutself(int tag)
  81. {
  82. struct cpumask mask;
  83. cpumask_copy(&mask, cpu_online_mask);
  84. cpumask_clear_cpu(smp_processor_id(), &mask);
  85. send_IPI_many(&mask, tag);
  86. }
  87. /*
  88. * Functions related to starting/stopping cpus.
  89. */
  90. /* Handler to start the current cpu. */
  91. static void smp_start_cpu_interrupt(void)
  92. {
  93. get_irq_regs()->pc = start_cpu_function_addr;
  94. }
  95. /* Handler to stop the current cpu. */
  96. static void smp_stop_cpu_interrupt(void)
  97. {
  98. arch_local_irq_disable_all();
  99. set_cpu_online(smp_processor_id(), 0);
  100. for (;;)
  101. asm("nap; nop");
  102. }
  103. /* This function calls the 'stop' function on all other CPUs in the system. */
  104. void smp_send_stop(void)
  105. {
  106. stopping_cpus = 1;
  107. send_IPI_allbutself(MSG_TAG_STOP_CPU);
  108. }
  109. /* On panic, just wait; we may get an smp_send_stop() later on. */
  110. void panic_smp_self_stop(void)
  111. {
  112. while (1)
  113. asm("nap; nop");
  114. }
  115. /*
  116. * Dispatch code called from hv_message_intr() for HV_MSG_TILE hv messages.
  117. */
  118. void evaluate_message(int tag)
  119. {
  120. switch (tag) {
  121. case MSG_TAG_START_CPU: /* Start up a cpu */
  122. smp_start_cpu_interrupt();
  123. break;
  124. case MSG_TAG_STOP_CPU: /* Sent to shut down slave CPU's */
  125. smp_stop_cpu_interrupt();
  126. break;
  127. case MSG_TAG_CALL_FUNCTION_MANY: /* Call function on cpumask */
  128. generic_smp_call_function_interrupt();
  129. break;
  130. case MSG_TAG_CALL_FUNCTION_SINGLE: /* Call function on one other CPU */
  131. generic_smp_call_function_single_interrupt();
  132. break;
  133. case MSG_TAG_IRQ_WORK: /* Invoke IRQ work */
  134. irq_work_run();
  135. break;
  136. default:
  137. panic("Unknown IPI message tag %d", tag);
  138. break;
  139. }
  140. }
  141. /*
  142. * flush_icache_range() code uses smp_call_function().
  143. */
  144. struct ipi_flush {
  145. unsigned long start;
  146. unsigned long end;
  147. };
  148. static void ipi_flush_icache_range(void *info)
  149. {
  150. struct ipi_flush *flush = (struct ipi_flush *) info;
  151. __flush_icache_range(flush->start, flush->end);
  152. }
  153. void flush_icache_range(unsigned long start, unsigned long end)
  154. {
  155. struct ipi_flush flush = { start, end };
  156. /* If invoked with irqs disabled, we can not issue IPIs. */
  157. if (irqs_disabled())
  158. flush_remote(0, HV_FLUSH_EVICT_L1I, NULL, 0, 0, 0,
  159. NULL, NULL, 0);
  160. else {
  161. preempt_disable();
  162. on_each_cpu(ipi_flush_icache_range, &flush, 1);
  163. preempt_enable();
  164. }
  165. }
  166. EXPORT_SYMBOL(flush_icache_range);
  167. #ifdef CONFIG_IRQ_WORK
  168. void arch_irq_work_raise(void)
  169. {
  170. if (arch_irq_work_has_interrupt())
  171. send_IPI_single(smp_processor_id(), MSG_TAG_IRQ_WORK);
  172. }
  173. #endif
  174. /* Called when smp_send_reschedule() triggers IRQ_RESCHEDULE. */
  175. static irqreturn_t handle_reschedule_ipi(int irq, void *token)
  176. {
  177. __this_cpu_inc(irq_stat.irq_resched_count);
  178. scheduler_ipi();
  179. return IRQ_HANDLED;
  180. }
  181. static struct irqaction resched_action = {
  182. .handler = handle_reschedule_ipi,
  183. .name = "resched",
  184. .dev_id = handle_reschedule_ipi /* unique token */,
  185. };
  186. void __init ipi_init(void)
  187. {
  188. int cpu = smp_processor_id();
  189. HV_Recipient recip = { .y = cpu_y(cpu), .x = cpu_x(cpu),
  190. .state = HV_TO_BE_SENT };
  191. int tag = MSG_TAG_CALL_FUNCTION_SINGLE;
  192. /*
  193. * Test if we can message ourselves for arch_irq_work_raise.
  194. * This functionality is only available in the Tilera hypervisor
  195. * in versions 4.3.4 and following.
  196. */
  197. if (hv_send_message(&recip, 1, (HV_VirtAddr)&tag, sizeof(tag)) == 1)
  198. self_interrupt_ok = true;
  199. else
  200. pr_warn("Older hypervisor: disabling fast irq_work_raise\n");
  201. #if CHIP_HAS_IPI()
  202. /* Map IPI trigger MMIO addresses. */
  203. for_each_possible_cpu(cpu) {
  204. HV_Coord tile;
  205. HV_PTE pte;
  206. unsigned long offset;
  207. tile.x = cpu_x(cpu);
  208. tile.y = cpu_y(cpu);
  209. if (hv_get_ipi_pte(tile, KERNEL_PL, &pte) != 0)
  210. panic("Failed to initialize IPI for cpu %d\n", cpu);
  211. offset = PFN_PHYS(pte_pfn(pte));
  212. ipi_mappings[cpu] = ioremap_prot(offset, PAGE_SIZE, pte);
  213. }
  214. #endif
  215. /* Bind handle_reschedule_ipi() to IRQ_RESCHEDULE. */
  216. tile_irq_activate(IRQ_RESCHEDULE, TILE_IRQ_PERCPU);
  217. BUG_ON(setup_irq(IRQ_RESCHEDULE, &resched_action));
  218. }
  219. #if CHIP_HAS_IPI()
  220. void smp_send_reschedule(int cpu)
  221. {
  222. WARN_ON(cpu_is_offline(cpu));
  223. /*
  224. * We just want to do an MMIO store. The traditional writeq()
  225. * functions aren't really correct here, since they're always
  226. * directed at the PCI shim. For now, just do a raw store,
  227. * casting away the __iomem attribute.
  228. */
  229. ((unsigned long __force *)ipi_mappings[cpu])[IRQ_RESCHEDULE] = 0;
  230. }
  231. #else
  232. void smp_send_reschedule(int cpu)
  233. {
  234. HV_Coord coord;
  235. WARN_ON(cpu_is_offline(cpu));
  236. coord.y = cpu_y(cpu);
  237. coord.x = cpu_x(cpu);
  238. hv_trigger_ipi(coord, IRQ_RESCHEDULE);
  239. }
  240. #endif /* CHIP_HAS_IPI() */