smp.c 5.7 KB

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