smp.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. * Functions related to starting/stopping cpus.
  81. */
  82. /* Handler to start the current cpu. */
  83. static void smp_start_cpu_interrupt(void)
  84. {
  85. get_irq_regs()->pc = start_cpu_function_addr;
  86. }
  87. /* Handler to stop the current cpu. */
  88. static void smp_stop_cpu_interrupt(void)
  89. {
  90. set_cpu_online(smp_processor_id(), 0);
  91. arch_local_irq_disable_all();
  92. for (;;)
  93. asm("nap");
  94. }
  95. /* This function calls the 'stop' function on all other CPUs in the system. */
  96. void smp_send_stop(void)
  97. {
  98. stopping_cpus = 1;
  99. send_IPI_allbutself(MSG_TAG_STOP_CPU);
  100. }
  101. /*
  102. * Dispatch code called from hv_message_intr() for HV_MSG_TILE hv messages.
  103. */
  104. void evaluate_message(int tag)
  105. {
  106. switch (tag) {
  107. case MSG_TAG_START_CPU: /* Start up a cpu */
  108. smp_start_cpu_interrupt();
  109. break;
  110. case MSG_TAG_STOP_CPU: /* Sent to shut down slave CPU's */
  111. smp_stop_cpu_interrupt();
  112. break;
  113. case MSG_TAG_CALL_FUNCTION_MANY: /* Call function on cpumask */
  114. generic_smp_call_function_interrupt();
  115. break;
  116. case MSG_TAG_CALL_FUNCTION_SINGLE: /* Call function on one other CPU */
  117. generic_smp_call_function_single_interrupt();
  118. break;
  119. default:
  120. panic("Unknown IPI message tag %d", tag);
  121. break;
  122. }
  123. }
  124. /*
  125. * flush_icache_range() code uses smp_call_function().
  126. */
  127. struct ipi_flush {
  128. unsigned long start;
  129. unsigned long end;
  130. };
  131. static void ipi_flush_icache_range(void *info)
  132. {
  133. struct ipi_flush *flush = (struct ipi_flush *) info;
  134. __flush_icache_range(flush->start, flush->end);
  135. }
  136. void flush_icache_range(unsigned long start, unsigned long end)
  137. {
  138. struct ipi_flush flush = { start, end };
  139. preempt_disable();
  140. on_each_cpu(ipi_flush_icache_range, &flush, 1);
  141. preempt_enable();
  142. }
  143. /* Called when smp_send_reschedule() triggers IRQ_RESCHEDULE. */
  144. static irqreturn_t handle_reschedule_ipi(int irq, void *token)
  145. {
  146. __get_cpu_var(irq_stat).irq_resched_count++;
  147. scheduler_ipi();
  148. return IRQ_HANDLED;
  149. }
  150. static struct irqaction resched_action = {
  151. .handler = handle_reschedule_ipi,
  152. .name = "resched",
  153. .dev_id = handle_reschedule_ipi /* unique token */,
  154. };
  155. void __init ipi_init(void)
  156. {
  157. #if CHIP_HAS_IPI()
  158. int cpu;
  159. /* Map IPI trigger MMIO addresses. */
  160. for_each_possible_cpu(cpu) {
  161. HV_Coord tile;
  162. HV_PTE pte;
  163. unsigned long offset;
  164. tile.x = cpu_x(cpu);
  165. tile.y = cpu_y(cpu);
  166. if (hv_get_ipi_pte(tile, KERNEL_PL, &pte) != 0)
  167. panic("Failed to initialize IPI for cpu %d\n", cpu);
  168. offset = hv_pte_get_pfn(pte) << PAGE_SHIFT;
  169. ipi_mappings[cpu] = ioremap_prot(offset, PAGE_SIZE, pte);
  170. }
  171. #endif
  172. /* Bind handle_reschedule_ipi() to IRQ_RESCHEDULE. */
  173. tile_irq_activate(IRQ_RESCHEDULE, TILE_IRQ_PERCPU);
  174. BUG_ON(setup_irq(IRQ_RESCHEDULE, &resched_action));
  175. }
  176. #if CHIP_HAS_IPI()
  177. void smp_send_reschedule(int cpu)
  178. {
  179. WARN_ON(cpu_is_offline(cpu));
  180. /*
  181. * We just want to do an MMIO store. The traditional writeq()
  182. * functions aren't really correct here, since they're always
  183. * directed at the PCI shim. For now, just do a raw store,
  184. * casting away the __iomem attribute.
  185. */
  186. ((unsigned long __force *)ipi_mappings[cpu])[IRQ_RESCHEDULE] = 0;
  187. }
  188. #else
  189. void smp_send_reschedule(int cpu)
  190. {
  191. HV_Coord coord;
  192. WARN_ON(cpu_is_offline(cpu));
  193. coord.y = cpu_y(cpu);
  194. coord.x = cpu_x(cpu);
  195. hv_trigger_ipi(coord, IRQ_RESCHEDULE);
  196. }
  197. #endif /* CHIP_HAS_IPI() */