smpboot.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/sched.h>
  19. #include <linux/kernel_stat.h>
  20. #include <linux/smp_lock.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/notifier.h>
  23. #include <linux/cpu.h>
  24. #include <linux/percpu.h>
  25. #include <linux/delay.h>
  26. #include <linux/err.h>
  27. #include <asm/mmu_context.h>
  28. #include <asm/tlbflush.h>
  29. #include <asm/sections.h>
  30. /*
  31. * This assembly function is provided in entry.S.
  32. * When called, it loops on a nap instruction forever.
  33. * FIXME: should be in a header somewhere.
  34. */
  35. extern void smp_nap(void);
  36. /* State of each CPU. */
  37. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  38. /* The messaging code jumps to this pointer during boot-up */
  39. unsigned long start_cpu_function_addr;
  40. /* Called very early during startup to mark boot cpu as online */
  41. void __init smp_prepare_boot_cpu(void)
  42. {
  43. int cpu = smp_processor_id();
  44. set_cpu_online(cpu, 1);
  45. set_cpu_present(cpu, 1);
  46. __get_cpu_var(cpu_state) = CPU_ONLINE;
  47. init_messaging();
  48. }
  49. static void start_secondary(void);
  50. /*
  51. * Called at the top of init() to launch all the other CPUs.
  52. * They run free to complete their initialization and then wait
  53. * until they get an IPI from the boot cpu to come online.
  54. */
  55. void __init smp_prepare_cpus(unsigned int max_cpus)
  56. {
  57. long rc;
  58. int cpu, cpu_count;
  59. int boot_cpu = smp_processor_id();
  60. current_thread_info()->cpu = boot_cpu;
  61. /*
  62. * Pin this task to the boot CPU while we bring up the others,
  63. * just to make sure we don't uselessly migrate as they come up.
  64. */
  65. rc = sched_setaffinity(current->pid, cpumask_of(boot_cpu));
  66. if (rc != 0)
  67. printk("Couldn't set init affinity to boot cpu (%ld)\n", rc);
  68. /* Print information about disabled and dataplane cpus. */
  69. print_disabled_cpus();
  70. /*
  71. * Tell the messaging subsystem how to respond to the
  72. * startup message. We use a level of indirection to avoid
  73. * confusing the linker with the fact that the messaging
  74. * subsystem is calling __init code.
  75. */
  76. start_cpu_function_addr = (unsigned long) &online_secondary;
  77. /* Set up thread context for all new processors. */
  78. cpu_count = 1;
  79. for (cpu = 0; cpu < NR_CPUS; ++cpu) {
  80. struct task_struct *idle;
  81. if (cpu == boot_cpu)
  82. continue;
  83. if (!cpu_possible(cpu)) {
  84. /*
  85. * Make this processor do nothing on boot.
  86. * Note that we don't give the boot_pc function
  87. * a stack, so it has to be assembly code.
  88. */
  89. per_cpu(boot_sp, cpu) = 0;
  90. per_cpu(boot_pc, cpu) = (unsigned long) smp_nap;
  91. continue;
  92. }
  93. /* Create a new idle thread to run start_secondary() */
  94. idle = fork_idle(cpu);
  95. if (IS_ERR(idle))
  96. panic("failed fork for CPU %d", cpu);
  97. idle->thread.pc = (unsigned long) start_secondary;
  98. /* Make this thread the boot thread for this processor */
  99. per_cpu(boot_sp, cpu) = task_ksp0(idle);
  100. per_cpu(boot_pc, cpu) = idle->thread.pc;
  101. ++cpu_count;
  102. }
  103. BUG_ON(cpu_count > (max_cpus ? max_cpus : 1));
  104. /* Fire up the other tiles, if any */
  105. init_cpu_present(cpu_possible_mask);
  106. if (cpumask_weight(cpu_present_mask) > 1) {
  107. mb(); /* make sure all data is visible to new processors */
  108. hv_start_all_tiles();
  109. }
  110. }
  111. static __initdata struct cpumask init_affinity;
  112. static __init int reset_init_affinity(void)
  113. {
  114. long rc = sched_setaffinity(current->pid, &init_affinity);
  115. if (rc != 0)
  116. printk(KERN_WARNING "couldn't reset init affinity (%ld)\n",
  117. rc);
  118. return 0;
  119. }
  120. late_initcall(reset_init_affinity);
  121. struct cpumask cpu_started __cpuinitdata;
  122. /*
  123. * Activate a secondary processor. Very minimal; don't add anything
  124. * to this path without knowing what you're doing, since SMP booting
  125. * is pretty fragile.
  126. */
  127. static void __cpuinit start_secondary(void)
  128. {
  129. int cpuid = smp_processor_id();
  130. /* Set our thread pointer appropriately. */
  131. set_my_cpu_offset(__per_cpu_offset[cpuid]);
  132. preempt_disable();
  133. /*
  134. * In large machines even this will slow us down, since we
  135. * will be contending for for the printk spinlock.
  136. */
  137. /* printk(KERN_DEBUG "Initializing CPU#%d\n", cpuid); */
  138. /* Initialize the current asid for our first page table. */
  139. __get_cpu_var(current_asid) = min_asid;
  140. /* Set up this thread as another owner of the init_mm */
  141. atomic_inc(&init_mm.mm_count);
  142. current->active_mm = &init_mm;
  143. if (current->mm)
  144. BUG();
  145. enter_lazy_tlb(&init_mm, current);
  146. /* Enable IRQs. */
  147. init_per_tile_IRQs();
  148. /* Allow hypervisor messages to be received */
  149. init_messaging();
  150. local_irq_enable();
  151. /* Indicate that we're ready to come up. */
  152. /* Must not do this before we're ready to receive messages */
  153. if (cpumask_test_and_set_cpu(cpuid, &cpu_started)) {
  154. printk(KERN_WARNING "CPU#%d already started!\n", cpuid);
  155. for (;;)
  156. local_irq_enable();
  157. }
  158. smp_nap();
  159. }
  160. void setup_mpls(void); /* from kernel/setup.c */
  161. void store_permanent_mappings(void);
  162. /*
  163. * Bring a secondary processor online.
  164. */
  165. void __cpuinit online_secondary()
  166. {
  167. /*
  168. * low-memory mappings have been cleared, flush them from
  169. * the local TLBs too.
  170. */
  171. local_flush_tlb();
  172. BUG_ON(in_interrupt());
  173. /* This must be done before setting cpu_online_mask */
  174. wmb();
  175. /*
  176. * We need to hold call_lock, so there is no inconsistency
  177. * between the time smp_call_function() determines number of
  178. * IPI recipients, and the time when the determination is made
  179. * for which cpus receive the IPI. Holding this
  180. * lock helps us to not include this cpu in a currently in progress
  181. * smp_call_function().
  182. */
  183. ipi_call_lock();
  184. set_cpu_online(smp_processor_id(), 1);
  185. ipi_call_unlock();
  186. __get_cpu_var(cpu_state) = CPU_ONLINE;
  187. /* Set up MPLs for this processor */
  188. setup_mpls();
  189. /* Set up tile-timer clock-event device on this cpu */
  190. setup_tile_timer();
  191. preempt_enable();
  192. store_permanent_mappings();
  193. cpu_idle();
  194. }
  195. int __cpuinit __cpu_up(unsigned int cpu)
  196. {
  197. /* Wait 5s total for all CPUs for them to come online */
  198. static int timeout;
  199. for (; !cpumask_test_cpu(cpu, &cpu_started); timeout++) {
  200. if (timeout >= 50000) {
  201. printk(KERN_INFO "skipping unresponsive cpu%d\n", cpu);
  202. local_irq_enable();
  203. return -EIO;
  204. }
  205. udelay(100);
  206. }
  207. local_irq_enable();
  208. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  209. /* Unleash the CPU! */
  210. send_IPI_single(cpu, MSG_TAG_START_CPU);
  211. while (!cpumask_test_cpu(cpu, cpu_online_mask))
  212. cpu_relax();
  213. return 0;
  214. }
  215. static void panic_start_cpu(void)
  216. {
  217. panic("Received a MSG_START_CPU IPI after boot finished.");
  218. }
  219. void __init smp_cpus_done(unsigned int max_cpus)
  220. {
  221. int cpu, next, rc;
  222. /* Reset the response to a (now illegal) MSG_START_CPU IPI. */
  223. start_cpu_function_addr = (unsigned long) &panic_start_cpu;
  224. cpumask_copy(&init_affinity, cpu_online_mask);
  225. /*
  226. * Pin ourselves to a single cpu in the initial affinity set
  227. * so that kernel mappings for the rootfs are not in the dataplane,
  228. * if set, and to avoid unnecessary migrating during bringup.
  229. * Use the last cpu just in case the whole chip has been
  230. * isolated from the scheduler, to keep init away from likely
  231. * more useful user code. This also ensures that work scheduled
  232. * via schedule_delayed_work() in the init routines will land
  233. * on this cpu.
  234. */
  235. for (cpu = cpumask_first(&init_affinity);
  236. (next = cpumask_next(cpu, &init_affinity)) < nr_cpu_ids;
  237. cpu = next)
  238. ;
  239. rc = sched_setaffinity(current->pid, cpumask_of(cpu));
  240. if (rc != 0)
  241. printk("Couldn't set init affinity to cpu %d (%d)\n", cpu, rc);
  242. }