smpboot.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. __this_cpu_write(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_warn("couldn't reset init affinity (%ld)\n", rc);
  111. return 0;
  112. }
  113. late_initcall(reset_init_affinity);
  114. static struct cpumask cpu_started;
  115. /*
  116. * Activate a secondary processor. Very minimal; don't add anything
  117. * to this path without knowing what you're doing, since SMP booting
  118. * is pretty fragile.
  119. */
  120. static void start_secondary(void)
  121. {
  122. int cpuid;
  123. preempt_disable();
  124. cpuid = smp_processor_id();
  125. /* Set our thread pointer appropriately. */
  126. set_my_cpu_offset(__per_cpu_offset[cpuid]);
  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. __this_cpu_write(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_warn("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 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. set_cpu_online(smp_processor_id(), 1);
  167. __this_cpu_write(cpu_state, CPU_ONLINE);
  168. /* Set up tile-specific state for this cpu. */
  169. setup_cpu(0);
  170. /* Set up tile-timer clock-event device on this cpu */
  171. setup_tile_timer();
  172. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  173. }
  174. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  175. {
  176. /* Wait 5s total for all CPUs for them to come online */
  177. static int timeout;
  178. for (; !cpumask_test_cpu(cpu, &cpu_started); timeout++) {
  179. if (timeout >= 50000) {
  180. pr_info("skipping unresponsive cpu%d\n", cpu);
  181. local_irq_enable();
  182. return -EIO;
  183. }
  184. udelay(100);
  185. }
  186. local_irq_enable();
  187. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  188. /* Unleash the CPU! */
  189. send_IPI_single(cpu, MSG_TAG_START_CPU);
  190. while (!cpumask_test_cpu(cpu, cpu_online_mask))
  191. cpu_relax();
  192. return 0;
  193. }
  194. static void panic_start_cpu(void)
  195. {
  196. panic("Received a MSG_START_CPU IPI after boot finished.");
  197. }
  198. void __init smp_cpus_done(unsigned int max_cpus)
  199. {
  200. int cpu, next, rc;
  201. /* Reset the response to a (now illegal) MSG_START_CPU IPI. */
  202. start_cpu_function_addr = (unsigned long) &panic_start_cpu;
  203. cpumask_copy(&init_affinity, cpu_online_mask);
  204. /*
  205. * Pin ourselves to a single cpu in the initial affinity set
  206. * so that kernel mappings for the rootfs are not in the dataplane,
  207. * if set, and to avoid unnecessary migrating during bringup.
  208. * Use the last cpu just in case the whole chip has been
  209. * isolated from the scheduler, to keep init away from likely
  210. * more useful user code. This also ensures that work scheduled
  211. * via schedule_delayed_work() in the init routines will land
  212. * on this cpu.
  213. */
  214. for (cpu = cpumask_first(&init_affinity);
  215. (next = cpumask_next(cpu, &init_affinity)) < nr_cpu_ids;
  216. cpu = next)
  217. ;
  218. rc = sched_setaffinity(current->pid, cpumask_of(cpu));
  219. if (rc != 0)
  220. pr_err("Couldn't set init affinity to cpu %d (%d)\n", cpu, rc);
  221. }