smp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/module.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/homecache.h>
  23. /*
  24. * We write to width and height with a single store in head_NN.S,
  25. * so make the variable aligned to "long".
  26. */
  27. HV_Topology smp_topology __write_once __aligned(sizeof(long));
  28. EXPORT_SYMBOL(smp_topology);
  29. #if CHIP_HAS_IPI()
  30. static unsigned long __iomem *ipi_mappings[NR_CPUS];
  31. #endif
  32. /*
  33. * Top-level send_IPI*() functions to send messages to other cpus.
  34. */
  35. /* Set by smp_send_stop() to avoid recursive panics. */
  36. static int stopping_cpus;
  37. static void __send_IPI_many(HV_Recipient *recip, int nrecip, int tag)
  38. {
  39. int sent = 0;
  40. while (sent < nrecip) {
  41. int rc = hv_send_message(recip, nrecip,
  42. (HV_VirtAddr)&tag, sizeof(tag));
  43. if (rc < 0) {
  44. if (!stopping_cpus) /* avoid recursive panic */
  45. panic("hv_send_message returned %d", rc);
  46. break;
  47. }
  48. WARN_ONCE(rc == 0, "hv_send_message() returned zero\n");
  49. sent += rc;
  50. }
  51. }
  52. void send_IPI_single(int cpu, int tag)
  53. {
  54. HV_Recipient recip = {
  55. .y = cpu / smp_width,
  56. .x = cpu % smp_width,
  57. .state = HV_TO_BE_SENT
  58. };
  59. __send_IPI_many(&recip, 1, tag);
  60. }
  61. void send_IPI_many(const struct cpumask *mask, int tag)
  62. {
  63. HV_Recipient recip[NR_CPUS];
  64. int cpu;
  65. int nrecip = 0;
  66. int my_cpu = smp_processor_id();
  67. for_each_cpu(cpu, mask) {
  68. HV_Recipient *r;
  69. BUG_ON(cpu == my_cpu);
  70. r = &recip[nrecip++];
  71. r->y = cpu / smp_width;
  72. r->x = cpu % smp_width;
  73. r->state = HV_TO_BE_SENT;
  74. }
  75. __send_IPI_many(recip, nrecip, tag);
  76. }
  77. void send_IPI_allbutself(int tag)
  78. {
  79. struct cpumask mask;
  80. cpumask_copy(&mask, cpu_online_mask);
  81. cpumask_clear_cpu(smp_processor_id(), &mask);
  82. send_IPI_many(&mask, tag);
  83. }
  84. /*
  85. * Functions related to starting/stopping cpus.
  86. */
  87. /* Handler to start the current cpu. */
  88. static void smp_start_cpu_interrupt(void)
  89. {
  90. get_irq_regs()->pc = start_cpu_function_addr;
  91. }
  92. /* Handler to stop the current cpu. */
  93. static void smp_stop_cpu_interrupt(void)
  94. {
  95. arch_local_irq_disable_all();
  96. set_cpu_online(smp_processor_id(), 0);
  97. for (;;)
  98. asm("nap; nop");
  99. }
  100. /* This function calls the 'stop' function on all other CPUs in the system. */
  101. void smp_send_stop(void)
  102. {
  103. stopping_cpus = 1;
  104. send_IPI_allbutself(MSG_TAG_STOP_CPU);
  105. }
  106. /* On panic, just wait; we may get an smp_send_stop() later on. */
  107. void panic_smp_self_stop(void)
  108. {
  109. while (1)
  110. asm("nap; nop");
  111. }
  112. /*
  113. * Dispatch code called from hv_message_intr() for HV_MSG_TILE hv messages.
  114. */
  115. void evaluate_message(int tag)
  116. {
  117. switch (tag) {
  118. case MSG_TAG_START_CPU: /* Start up a cpu */
  119. smp_start_cpu_interrupt();
  120. break;
  121. case MSG_TAG_STOP_CPU: /* Sent to shut down slave CPU's */
  122. smp_stop_cpu_interrupt();
  123. break;
  124. case MSG_TAG_CALL_FUNCTION_MANY: /* Call function on cpumask */
  125. generic_smp_call_function_interrupt();
  126. break;
  127. case MSG_TAG_CALL_FUNCTION_SINGLE: /* Call function on one other CPU */
  128. generic_smp_call_function_single_interrupt();
  129. break;
  130. default:
  131. panic("Unknown IPI message tag %d", tag);
  132. break;
  133. }
  134. }
  135. /*
  136. * flush_icache_range() code uses smp_call_function().
  137. */
  138. struct ipi_flush {
  139. unsigned long start;
  140. unsigned long end;
  141. };
  142. static void ipi_flush_icache_range(void *info)
  143. {
  144. struct ipi_flush *flush = (struct ipi_flush *) info;
  145. __flush_icache_range(flush->start, flush->end);
  146. }
  147. void flush_icache_range(unsigned long start, unsigned long end)
  148. {
  149. struct ipi_flush flush = { start, end };
  150. /* If invoked with irqs disabled, we can not issue IPIs. */
  151. if (irqs_disabled())
  152. flush_remote(0, HV_FLUSH_EVICT_L1I, NULL, 0, 0, 0,
  153. NULL, NULL, 0);
  154. else {
  155. preempt_disable();
  156. on_each_cpu(ipi_flush_icache_range, &flush, 1);
  157. preempt_enable();
  158. }
  159. }
  160. /* Called when smp_send_reschedule() triggers IRQ_RESCHEDULE. */
  161. static irqreturn_t handle_reschedule_ipi(int irq, void *token)
  162. {
  163. __get_cpu_var(irq_stat).irq_resched_count++;
  164. scheduler_ipi();
  165. return IRQ_HANDLED;
  166. }
  167. static struct irqaction resched_action = {
  168. .handler = handle_reschedule_ipi,
  169. .name = "resched",
  170. .dev_id = handle_reschedule_ipi /* unique token */,
  171. };
  172. void __init ipi_init(void)
  173. {
  174. #if CHIP_HAS_IPI()
  175. int cpu;
  176. /* Map IPI trigger MMIO addresses. */
  177. for_each_possible_cpu(cpu) {
  178. HV_Coord tile;
  179. HV_PTE pte;
  180. unsigned long offset;
  181. tile.x = cpu_x(cpu);
  182. tile.y = cpu_y(cpu);
  183. if (hv_get_ipi_pte(tile, KERNEL_PL, &pte) != 0)
  184. panic("Failed to initialize IPI for cpu %d\n", cpu);
  185. offset = PFN_PHYS(pte_pfn(pte));
  186. ipi_mappings[cpu] = ioremap_prot(offset, PAGE_SIZE, pte);
  187. }
  188. #endif
  189. /* Bind handle_reschedule_ipi() to IRQ_RESCHEDULE. */
  190. tile_irq_activate(IRQ_RESCHEDULE, TILE_IRQ_PERCPU);
  191. BUG_ON(setup_irq(IRQ_RESCHEDULE, &resched_action));
  192. }
  193. #if CHIP_HAS_IPI()
  194. void smp_send_reschedule(int cpu)
  195. {
  196. WARN_ON(cpu_is_offline(cpu));
  197. /*
  198. * We just want to do an MMIO store. The traditional writeq()
  199. * functions aren't really correct here, since they're always
  200. * directed at the PCI shim. For now, just do a raw store,
  201. * casting away the __iomem attribute.
  202. */
  203. ((unsigned long __force *)ipi_mappings[cpu])[IRQ_RESCHEDULE] = 0;
  204. }
  205. #else
  206. void smp_send_reschedule(int cpu)
  207. {
  208. HV_Coord coord;
  209. WARN_ON(cpu_is_offline(cpu));
  210. coord.y = cpu_y(cpu);
  211. coord.x = cpu_x(cpu);
  212. hv_trigger_ipi(coord, IRQ_RESCHEDULE);
  213. }
  214. #endif /* CHIP_HAS_IPI() */