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. 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. void send_IPI_single(int cpu, int tag)
  33. {
  34. HV_Recipient recip = {
  35. .y = cpu / smp_width,
  36. .x = cpu % smp_width,
  37. .state = HV_TO_BE_SENT
  38. };
  39. int rc = hv_send_message(&recip, 1, (HV_VirtAddr)&tag, sizeof(tag));
  40. BUG_ON(rc <= 0);
  41. }
  42. void send_IPI_many(const struct cpumask *mask, int tag)
  43. {
  44. HV_Recipient recip[NR_CPUS];
  45. int cpu, sent;
  46. int nrecip = 0;
  47. int my_cpu = smp_processor_id();
  48. for_each_cpu(cpu, mask) {
  49. HV_Recipient *r;
  50. BUG_ON(cpu == my_cpu);
  51. r = &recip[nrecip++];
  52. r->y = cpu / smp_width;
  53. r->x = cpu % smp_width;
  54. r->state = HV_TO_BE_SENT;
  55. }
  56. sent = 0;
  57. while (sent < nrecip) {
  58. int rc = hv_send_message(recip, nrecip,
  59. (HV_VirtAddr)&tag, sizeof(tag));
  60. if (rc <= 0) {
  61. if (!stopping_cpus) /* avoid recursive panic */
  62. panic("hv_send_message returned %d", rc);
  63. break;
  64. }
  65. sent += rc;
  66. }
  67. }
  68. void send_IPI_allbutself(int tag)
  69. {
  70. struct cpumask mask;
  71. cpumask_copy(&mask, cpu_online_mask);
  72. cpumask_clear_cpu(smp_processor_id(), &mask);
  73. send_IPI_many(&mask, tag);
  74. }
  75. /*
  76. * Provide smp_call_function_mask, but also run function locally
  77. * if specified in the mask.
  78. */
  79. void on_each_cpu_mask(const struct cpumask *mask, void (*func)(void *),
  80. void *info, bool wait)
  81. {
  82. int cpu = get_cpu();
  83. smp_call_function_many(mask, func, info, wait);
  84. if (cpumask_test_cpu(cpu, mask)) {
  85. local_irq_disable();
  86. func(info);
  87. local_irq_enable();
  88. }
  89. put_cpu();
  90. }
  91. /*
  92. * Functions related to starting/stopping cpus.
  93. */
  94. /* Handler to start the current cpu. */
  95. static void smp_start_cpu_interrupt(void)
  96. {
  97. get_irq_regs()->pc = start_cpu_function_addr;
  98. }
  99. /* Handler to stop the current cpu. */
  100. static void smp_stop_cpu_interrupt(void)
  101. {
  102. set_cpu_online(smp_processor_id(), 0);
  103. arch_local_irq_disable_all();
  104. for (;;)
  105. asm("nap");
  106. }
  107. /* This function calls the 'stop' function on all other CPUs in the system. */
  108. void smp_send_stop(void)
  109. {
  110. stopping_cpus = 1;
  111. send_IPI_allbutself(MSG_TAG_STOP_CPU);
  112. }
  113. /*
  114. * Dispatch code called from hv_message_intr() for HV_MSG_TILE hv messages.
  115. */
  116. void evaluate_message(int tag)
  117. {
  118. switch (tag) {
  119. case MSG_TAG_START_CPU: /* Start up a cpu */
  120. smp_start_cpu_interrupt();
  121. break;
  122. case MSG_TAG_STOP_CPU: /* Sent to shut down slave CPU's */
  123. smp_stop_cpu_interrupt();
  124. break;
  125. case MSG_TAG_CALL_FUNCTION_MANY: /* Call function on cpumask */
  126. generic_smp_call_function_interrupt();
  127. break;
  128. case MSG_TAG_CALL_FUNCTION_SINGLE: /* Call function on one other CPU */
  129. generic_smp_call_function_single_interrupt();
  130. break;
  131. default:
  132. panic("Unknown IPI message tag %d", tag);
  133. break;
  134. }
  135. }
  136. /*
  137. * flush_icache_range() code uses smp_call_function().
  138. */
  139. struct ipi_flush {
  140. unsigned long start;
  141. unsigned long end;
  142. };
  143. static void ipi_flush_icache_range(void *info)
  144. {
  145. struct ipi_flush *flush = (struct ipi_flush *) info;
  146. __flush_icache_range(flush->start, flush->end);
  147. }
  148. void flush_icache_range(unsigned long start, unsigned long end)
  149. {
  150. struct ipi_flush flush = { start, end };
  151. preempt_disable();
  152. on_each_cpu(ipi_flush_icache_range, &flush, 1);
  153. preempt_enable();
  154. }
  155. /* Called when smp_send_reschedule() triggers IRQ_RESCHEDULE. */
  156. static irqreturn_t handle_reschedule_ipi(int irq, void *token)
  157. {
  158. /*
  159. * Nothing to do here; when we return from interrupt, the
  160. * rescheduling will occur there. But do bump the interrupt
  161. * profiler count in the meantime.
  162. */
  163. __get_cpu_var(irq_stat).irq_resched_count++;
  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() */