smpboot.c 7.4 KB

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