smpboot.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. /*
  2. * x86 SMP booting functions
  3. *
  4. * (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
  5. * (c) 1998, 1999, 2000 Ingo Molnar <mingo@redhat.com>
  6. * Copyright 2001 Andi Kleen, SuSE Labs.
  7. *
  8. * Much of the core SMP work is based on previous work by Thomas Radke, to
  9. * whom a great many thanks are extended.
  10. *
  11. * Thanks to Intel for making available several different Pentium,
  12. * Pentium Pro and Pentium-II/Xeon MP machines.
  13. * Original development of Linux SMP code supported by Caldera.
  14. *
  15. * This code is released under the GNU General Public License version 2
  16. *
  17. * Fixes
  18. * Felix Koop : NR_CPUS used properly
  19. * Jose Renau : Handle single CPU case.
  20. * Alan Cox : By repeated request 8) - Total BogoMIP report.
  21. * Greg Wright : Fix for kernel stacks panic.
  22. * Erich Boleyn : MP v1.4 and additional changes.
  23. * Matthias Sattler : Changes for 2.1 kernel map.
  24. * Michel Lespinasse : Changes for 2.1 kernel map.
  25. * Michael Chastain : Change trampoline.S to gnu as.
  26. * Alan Cox : Dumb bug: 'B' step PPro's are fine
  27. * Ingo Molnar : Added APIC timers, based on code
  28. * from Jose Renau
  29. * Ingo Molnar : various cleanups and rewrites
  30. * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug.
  31. * Maciej W. Rozycki : Bits for genuine 82489DX APICs
  32. * Andi Kleen : Changed for SMP boot into long mode.
  33. * Rusty Russell : Hacked into shape for new "hotplug" boot process.
  34. * Andi Kleen : Converted to new state machine.
  35. * Various cleanups.
  36. * Probably mostly hotplug CPU ready now.
  37. * Ashok Raj : CPU hotplug support
  38. */
  39. #include <linux/init.h>
  40. #include <linux/mm.h>
  41. #include <linux/kernel_stat.h>
  42. #include <linux/smp_lock.h>
  43. #include <linux/bootmem.h>
  44. #include <linux/thread_info.h>
  45. #include <linux/module.h>
  46. #include <linux/delay.h>
  47. #include <linux/mc146818rtc.h>
  48. #include <asm/mtrr.h>
  49. #include <asm/pgalloc.h>
  50. #include <asm/desc.h>
  51. #include <asm/kdebug.h>
  52. #include <asm/tlbflush.h>
  53. #include <asm/proto.h>
  54. #include <asm/nmi.h>
  55. #include <asm/irq.h>
  56. #include <asm/hw_irq.h>
  57. #include <asm/numa.h>
  58. /* Number of siblings per CPU package */
  59. int smp_num_siblings = 1;
  60. EXPORT_SYMBOL(smp_num_siblings);
  61. /* Last level cache ID of each logical CPU */
  62. u8 cpu_llc_id[NR_CPUS] __cpuinitdata = {[0 ... NR_CPUS-1] = BAD_APICID};
  63. EXPORT_SYMBOL(cpu_llc_id);
  64. /* Bitmask of currently online CPUs */
  65. cpumask_t cpu_online_map __read_mostly;
  66. EXPORT_SYMBOL(cpu_online_map);
  67. /*
  68. * Private maps to synchronize booting between AP and BP.
  69. * Probably not needed anymore, but it makes for easier debugging. -AK
  70. */
  71. cpumask_t cpu_callin_map;
  72. cpumask_t cpu_callout_map;
  73. EXPORT_SYMBOL(cpu_callout_map);
  74. cpumask_t cpu_possible_map;
  75. EXPORT_SYMBOL(cpu_possible_map);
  76. /* Per CPU bogomips and other parameters */
  77. struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
  78. EXPORT_SYMBOL(cpu_data);
  79. /* Set when the idlers are all forked */
  80. int smp_threads_ready;
  81. /* representing HT siblings of each logical CPU */
  82. cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
  83. EXPORT_SYMBOL(cpu_sibling_map);
  84. /* representing HT and core siblings of each logical CPU */
  85. cpumask_t cpu_core_map[NR_CPUS] __read_mostly;
  86. EXPORT_SYMBOL(cpu_core_map);
  87. /*
  88. * Trampoline 80x86 program as an array.
  89. */
  90. extern unsigned char trampoline_data[];
  91. extern unsigned char trampoline_end[];
  92. /* State of each CPU */
  93. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  94. /*
  95. * Store all idle threads, this can be reused instead of creating
  96. * a new thread. Also avoids complicated thread destroy functionality
  97. * for idle threads.
  98. */
  99. struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
  100. #define get_idle_for_cpu(x) (idle_thread_array[(x)])
  101. #define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
  102. /*
  103. * Currently trivial. Write the real->protected mode
  104. * bootstrap into the page concerned. The caller
  105. * has made sure it's suitably aligned.
  106. */
  107. static unsigned long __cpuinit setup_trampoline(void)
  108. {
  109. void *tramp = __va(SMP_TRAMPOLINE_BASE);
  110. memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);
  111. return virt_to_phys(tramp);
  112. }
  113. /*
  114. * The bootstrap kernel entry code has set these up. Save them for
  115. * a given CPU
  116. */
  117. static void __cpuinit smp_store_cpu_info(int id)
  118. {
  119. struct cpuinfo_x86 *c = cpu_data + id;
  120. *c = boot_cpu_data;
  121. identify_cpu(c);
  122. print_cpu_info(c);
  123. }
  124. /*
  125. * New Funky TSC sync algorithm borrowed from IA64.
  126. * Main advantage is that it doesn't reset the TSCs fully and
  127. * in general looks more robust and it works better than my earlier
  128. * attempts. I believe it was written by David Mosberger. Some minor
  129. * adjustments for x86-64 by me -AK
  130. *
  131. * Original comment reproduced below.
  132. *
  133. * Synchronize TSC of the current (slave) CPU with the TSC of the
  134. * MASTER CPU (normally the time-keeper CPU). We use a closed loop to
  135. * eliminate the possibility of unaccounted-for errors (such as
  136. * getting a machine check in the middle of a calibration step). The
  137. * basic idea is for the slave to ask the master what itc value it has
  138. * and to read its own itc before and after the master responds. Each
  139. * iteration gives us three timestamps:
  140. *
  141. * slave master
  142. *
  143. * t0 ---\
  144. * ---\
  145. * --->
  146. * tm
  147. * /---
  148. * /---
  149. * t1 <---
  150. *
  151. *
  152. * The goal is to adjust the slave's TSC such that tm falls exactly
  153. * half-way between t0 and t1. If we achieve this, the clocks are
  154. * synchronized provided the interconnect between the slave and the
  155. * master is symmetric. Even if the interconnect were asymmetric, we
  156. * would still know that the synchronization error is smaller than the
  157. * roundtrip latency (t0 - t1).
  158. *
  159. * When the interconnect is quiet and symmetric, this lets us
  160. * synchronize the TSC to within one or two cycles. However, we can
  161. * only *guarantee* that the synchronization is accurate to within a
  162. * round-trip time, which is typically in the range of several hundred
  163. * cycles (e.g., ~500 cycles). In practice, this means that the TSCs
  164. * are usually almost perfectly synchronized, but we shouldn't assume
  165. * that the accuracy is much better than half a micro second or so.
  166. *
  167. * [there are other errors like the latency of RDTSC and of the
  168. * WRMSR. These can also account to hundreds of cycles. So it's
  169. * probably worse. It claims 153 cycles error on a dual Opteron,
  170. * but I suspect the numbers are actually somewhat worse -AK]
  171. */
  172. #define MASTER 0
  173. #define SLAVE (SMP_CACHE_BYTES/8)
  174. /* Intentionally don't use cpu_relax() while TSC synchronization
  175. because we don't want to go into funky power save modi or cause
  176. hypervisors to schedule us away. Going to sleep would likely affect
  177. latency and low latency is the primary objective here. -AK */
  178. #define no_cpu_relax() barrier()
  179. static __cpuinitdata DEFINE_SPINLOCK(tsc_sync_lock);
  180. static volatile __cpuinitdata unsigned long go[SLAVE + 1];
  181. static int notscsync __cpuinitdata;
  182. #undef DEBUG_TSC_SYNC
  183. #define NUM_ROUNDS 64 /* magic value */
  184. #define NUM_ITERS 5 /* likewise */
  185. /* Callback on boot CPU */
  186. static __cpuinit void sync_master(void *arg)
  187. {
  188. unsigned long flags, i;
  189. go[MASTER] = 0;
  190. local_irq_save(flags);
  191. {
  192. for (i = 0; i < NUM_ROUNDS*NUM_ITERS; ++i) {
  193. while (!go[MASTER])
  194. no_cpu_relax();
  195. go[MASTER] = 0;
  196. rdtscll(go[SLAVE]);
  197. }
  198. }
  199. local_irq_restore(flags);
  200. }
  201. /*
  202. * Return the number of cycles by which our tsc differs from the tsc
  203. * on the master (time-keeper) CPU. A positive number indicates our
  204. * tsc is ahead of the master, negative that it is behind.
  205. */
  206. static inline long
  207. get_delta(long *rt, long *master)
  208. {
  209. unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;
  210. unsigned long tcenter, t0, t1, tm;
  211. int i;
  212. for (i = 0; i < NUM_ITERS; ++i) {
  213. rdtscll(t0);
  214. go[MASTER] = 1;
  215. while (!(tm = go[SLAVE]))
  216. no_cpu_relax();
  217. go[SLAVE] = 0;
  218. rdtscll(t1);
  219. if (t1 - t0 < best_t1 - best_t0)
  220. best_t0 = t0, best_t1 = t1, best_tm = tm;
  221. }
  222. *rt = best_t1 - best_t0;
  223. *master = best_tm - best_t0;
  224. /* average best_t0 and best_t1 without overflow: */
  225. tcenter = (best_t0/2 + best_t1/2);
  226. if (best_t0 % 2 + best_t1 % 2 == 2)
  227. ++tcenter;
  228. return tcenter - best_tm;
  229. }
  230. static __cpuinit void sync_tsc(unsigned int master)
  231. {
  232. int i, done = 0;
  233. long delta, adj, adjust_latency = 0;
  234. unsigned long flags, rt, master_time_stamp, bound;
  235. #ifdef DEBUG_TSC_SYNC
  236. static struct syncdebug {
  237. long rt; /* roundtrip time */
  238. long master; /* master's timestamp */
  239. long diff; /* difference between midpoint and master's timestamp */
  240. long lat; /* estimate of tsc adjustment latency */
  241. } t[NUM_ROUNDS] __cpuinitdata;
  242. #endif
  243. printk(KERN_INFO "CPU %d: Syncing TSC to CPU %u.\n",
  244. smp_processor_id(), master);
  245. go[MASTER] = 1;
  246. /* It is dangerous to broadcast IPI as cpus are coming up,
  247. * as they may not be ready to accept them. So since
  248. * we only need to send the ipi to the boot cpu direct
  249. * the message, and avoid the race.
  250. */
  251. smp_call_function_single(master, sync_master, NULL, 1, 0);
  252. while (go[MASTER]) /* wait for master to be ready */
  253. no_cpu_relax();
  254. spin_lock_irqsave(&tsc_sync_lock, flags);
  255. {
  256. for (i = 0; i < NUM_ROUNDS; ++i) {
  257. delta = get_delta(&rt, &master_time_stamp);
  258. if (delta == 0) {
  259. done = 1; /* let's lock on to this... */
  260. bound = rt;
  261. }
  262. if (!done) {
  263. unsigned long t;
  264. if (i > 0) {
  265. adjust_latency += -delta;
  266. adj = -delta + adjust_latency/4;
  267. } else
  268. adj = -delta;
  269. rdtscll(t);
  270. wrmsrl(MSR_IA32_TSC, t + adj);
  271. }
  272. #ifdef DEBUG_TSC_SYNC
  273. t[i].rt = rt;
  274. t[i].master = master_time_stamp;
  275. t[i].diff = delta;
  276. t[i].lat = adjust_latency/4;
  277. #endif
  278. }
  279. }
  280. spin_unlock_irqrestore(&tsc_sync_lock, flags);
  281. #ifdef DEBUG_TSC_SYNC
  282. for (i = 0; i < NUM_ROUNDS; ++i)
  283. printk("rt=%5ld master=%5ld diff=%5ld adjlat=%5ld\n",
  284. t[i].rt, t[i].master, t[i].diff, t[i].lat);
  285. #endif
  286. printk(KERN_INFO
  287. "CPU %d: synchronized TSC with CPU %u (last diff %ld cycles, "
  288. "maxerr %lu cycles)\n",
  289. smp_processor_id(), master, delta, rt);
  290. }
  291. static void __cpuinit tsc_sync_wait(void)
  292. {
  293. /*
  294. * When the CPU has synchronized TSCs assume the BIOS
  295. * or the hardware already synced. Otherwise we could
  296. * mess up a possible perfect synchronization with a
  297. * not-quite-perfect algorithm.
  298. */
  299. if (notscsync || !cpu_has_tsc || !unsynchronized_tsc())
  300. return;
  301. sync_tsc(0);
  302. }
  303. static __init int notscsync_setup(char *s)
  304. {
  305. notscsync = 1;
  306. return 1;
  307. }
  308. __setup("notscsync", notscsync_setup);
  309. static atomic_t init_deasserted __cpuinitdata;
  310. /*
  311. * Report back to the Boot Processor.
  312. * Running on AP.
  313. */
  314. void __cpuinit smp_callin(void)
  315. {
  316. int cpuid, phys_id;
  317. unsigned long timeout;
  318. /*
  319. * If waken up by an INIT in an 82489DX configuration
  320. * we may get here before an INIT-deassert IPI reaches
  321. * our local APIC. We have to wait for the IPI or we'll
  322. * lock up on an APIC access.
  323. */
  324. while (!atomic_read(&init_deasserted))
  325. cpu_relax();
  326. /*
  327. * (This works even if the APIC is not enabled.)
  328. */
  329. phys_id = GET_APIC_ID(apic_read(APIC_ID));
  330. cpuid = smp_processor_id();
  331. if (cpu_isset(cpuid, cpu_callin_map)) {
  332. panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
  333. phys_id, cpuid);
  334. }
  335. Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
  336. /*
  337. * STARTUP IPIs are fragile beasts as they might sometimes
  338. * trigger some glue motherboard logic. Complete APIC bus
  339. * silence for 1 second, this overestimates the time the
  340. * boot CPU is spending to send the up to 2 STARTUP IPIs
  341. * by a factor of two. This should be enough.
  342. */
  343. /*
  344. * Waiting 2s total for startup (udelay is not yet working)
  345. */
  346. timeout = jiffies + 2*HZ;
  347. while (time_before(jiffies, timeout)) {
  348. /*
  349. * Has the boot CPU finished it's STARTUP sequence?
  350. */
  351. if (cpu_isset(cpuid, cpu_callout_map))
  352. break;
  353. cpu_relax();
  354. }
  355. if (!time_before(jiffies, timeout)) {
  356. panic("smp_callin: CPU%d started up but did not get a callout!\n",
  357. cpuid);
  358. }
  359. /*
  360. * the boot CPU has finished the init stage and is spinning
  361. * on callin_map until we finish. We are free to set up this
  362. * CPU, first the APIC. (this is probably redundant on most
  363. * boards)
  364. */
  365. Dprintk("CALLIN, before setup_local_APIC().\n");
  366. setup_local_APIC();
  367. /*
  368. * Get our bogomips.
  369. *
  370. * Need to enable IRQs because it can take longer and then
  371. * the NMI watchdog might kill us.
  372. */
  373. local_irq_enable();
  374. calibrate_delay();
  375. local_irq_disable();
  376. Dprintk("Stack at about %p\n",&cpuid);
  377. disable_APIC_timer();
  378. /*
  379. * Save our processor parameters
  380. */
  381. smp_store_cpu_info(cpuid);
  382. /*
  383. * Allow the master to continue.
  384. */
  385. cpu_set(cpuid, cpu_callin_map);
  386. }
  387. /* maps the cpu to the sched domain representing multi-core */
  388. cpumask_t cpu_coregroup_map(int cpu)
  389. {
  390. struct cpuinfo_x86 *c = cpu_data + cpu;
  391. /*
  392. * For perf, we return last level cache shared map.
  393. * And for power savings, we return cpu_core_map
  394. */
  395. if (sched_mc_power_savings || sched_smt_power_savings)
  396. return cpu_core_map[cpu];
  397. else
  398. return c->llc_shared_map;
  399. }
  400. /* representing cpus for which sibling maps can be computed */
  401. static cpumask_t cpu_sibling_setup_map;
  402. static inline void set_cpu_sibling_map(int cpu)
  403. {
  404. int i;
  405. struct cpuinfo_x86 *c = cpu_data;
  406. cpu_set(cpu, cpu_sibling_setup_map);
  407. if (smp_num_siblings > 1) {
  408. for_each_cpu_mask(i, cpu_sibling_setup_map) {
  409. if (c[cpu].phys_proc_id == c[i].phys_proc_id &&
  410. c[cpu].cpu_core_id == c[i].cpu_core_id) {
  411. cpu_set(i, cpu_sibling_map[cpu]);
  412. cpu_set(cpu, cpu_sibling_map[i]);
  413. cpu_set(i, cpu_core_map[cpu]);
  414. cpu_set(cpu, cpu_core_map[i]);
  415. cpu_set(i, c[cpu].llc_shared_map);
  416. cpu_set(cpu, c[i].llc_shared_map);
  417. }
  418. }
  419. } else {
  420. cpu_set(cpu, cpu_sibling_map[cpu]);
  421. }
  422. cpu_set(cpu, c[cpu].llc_shared_map);
  423. if (current_cpu_data.x86_max_cores == 1) {
  424. cpu_core_map[cpu] = cpu_sibling_map[cpu];
  425. c[cpu].booted_cores = 1;
  426. return;
  427. }
  428. for_each_cpu_mask(i, cpu_sibling_setup_map) {
  429. if (cpu_llc_id[cpu] != BAD_APICID &&
  430. cpu_llc_id[cpu] == cpu_llc_id[i]) {
  431. cpu_set(i, c[cpu].llc_shared_map);
  432. cpu_set(cpu, c[i].llc_shared_map);
  433. }
  434. if (c[cpu].phys_proc_id == c[i].phys_proc_id) {
  435. cpu_set(i, cpu_core_map[cpu]);
  436. cpu_set(cpu, cpu_core_map[i]);
  437. /*
  438. * Does this new cpu bringup a new core?
  439. */
  440. if (cpus_weight(cpu_sibling_map[cpu]) == 1) {
  441. /*
  442. * for each core in package, increment
  443. * the booted_cores for this new cpu
  444. */
  445. if (first_cpu(cpu_sibling_map[i]) == i)
  446. c[cpu].booted_cores++;
  447. /*
  448. * increment the core count for all
  449. * the other cpus in this package
  450. */
  451. if (i != cpu)
  452. c[i].booted_cores++;
  453. } else if (i != cpu && !c[cpu].booted_cores)
  454. c[cpu].booted_cores = c[i].booted_cores;
  455. }
  456. }
  457. }
  458. /*
  459. * Setup code on secondary processor (after comming out of the trampoline)
  460. */
  461. void __cpuinit start_secondary(void)
  462. {
  463. /*
  464. * Dont put anything before smp_callin(), SMP
  465. * booting is too fragile that we want to limit the
  466. * things done here to the most necessary things.
  467. */
  468. cpu_init();
  469. preempt_disable();
  470. smp_callin();
  471. /* otherwise gcc will move up the smp_processor_id before the cpu_init */
  472. barrier();
  473. Dprintk("cpu %d: setting up apic clock\n", smp_processor_id());
  474. setup_secondary_APIC_clock();
  475. Dprintk("cpu %d: enabling apic timer\n", smp_processor_id());
  476. if (nmi_watchdog == NMI_IO_APIC) {
  477. disable_8259A_irq(0);
  478. enable_NMI_through_LVT0(NULL);
  479. enable_8259A_irq(0);
  480. }
  481. enable_APIC_timer();
  482. /*
  483. * The sibling maps must be set before turing the online map on for
  484. * this cpu
  485. */
  486. set_cpu_sibling_map(smp_processor_id());
  487. /*
  488. * Wait for TSC sync to not schedule things before.
  489. * We still process interrupts, which could see an inconsistent
  490. * time in that window unfortunately.
  491. * Do this here because TSC sync has global unprotected state.
  492. */
  493. tsc_sync_wait();
  494. /*
  495. * We need to hold call_lock, so there is no inconsistency
  496. * between the time smp_call_function() determines number of
  497. * IPI receipients, and the time when the determination is made
  498. * for which cpus receive the IPI in genapic_flat.c. Holding this
  499. * lock helps us to not include this cpu in a currently in progress
  500. * smp_call_function().
  501. */
  502. lock_ipi_call_lock();
  503. /*
  504. * Allow the master to continue.
  505. */
  506. cpu_set(smp_processor_id(), cpu_online_map);
  507. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  508. unlock_ipi_call_lock();
  509. cpu_idle();
  510. }
  511. extern volatile unsigned long init_rsp;
  512. extern void (*initial_code)(void);
  513. #ifdef APIC_DEBUG
  514. static void inquire_remote_apic(int apicid)
  515. {
  516. unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  517. char *names[] = { "ID", "VERSION", "SPIV" };
  518. int timeout, status;
  519. printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
  520. for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
  521. printk("... APIC #%d %s: ", apicid, names[i]);
  522. /*
  523. * Wait for idle.
  524. */
  525. apic_wait_icr_idle();
  526. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  527. apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]);
  528. timeout = 0;
  529. do {
  530. udelay(100);
  531. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  532. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  533. switch (status) {
  534. case APIC_ICR_RR_VALID:
  535. status = apic_read(APIC_RRR);
  536. printk("%08x\n", status);
  537. break;
  538. default:
  539. printk("failed\n");
  540. }
  541. }
  542. }
  543. #endif
  544. /*
  545. * Kick the secondary to wake up.
  546. */
  547. static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
  548. {
  549. unsigned long send_status = 0, accept_status = 0;
  550. int maxlvt, timeout, num_starts, j;
  551. Dprintk("Asserting INIT.\n");
  552. /*
  553. * Turn INIT on target chip
  554. */
  555. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  556. /*
  557. * Send IPI
  558. */
  559. apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
  560. | APIC_DM_INIT);
  561. Dprintk("Waiting for send to finish...\n");
  562. timeout = 0;
  563. do {
  564. Dprintk("+");
  565. udelay(100);
  566. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  567. } while (send_status && (timeout++ < 1000));
  568. mdelay(10);
  569. Dprintk("Deasserting INIT.\n");
  570. /* Target chip */
  571. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  572. /* Send IPI */
  573. apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
  574. Dprintk("Waiting for send to finish...\n");
  575. timeout = 0;
  576. do {
  577. Dprintk("+");
  578. udelay(100);
  579. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  580. } while (send_status && (timeout++ < 1000));
  581. mb();
  582. atomic_set(&init_deasserted, 1);
  583. num_starts = 2;
  584. /*
  585. * Run STARTUP IPI loop.
  586. */
  587. Dprintk("#startup loops: %d.\n", num_starts);
  588. maxlvt = get_maxlvt();
  589. for (j = 1; j <= num_starts; j++) {
  590. Dprintk("Sending STARTUP #%d.\n",j);
  591. apic_write(APIC_ESR, 0);
  592. apic_read(APIC_ESR);
  593. Dprintk("After apic_write.\n");
  594. /*
  595. * STARTUP IPI
  596. */
  597. /* Target chip */
  598. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  599. /* Boot on the stack */
  600. /* Kick the second */
  601. apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12));
  602. /*
  603. * Give the other CPU some time to accept the IPI.
  604. */
  605. udelay(300);
  606. Dprintk("Startup point 1.\n");
  607. Dprintk("Waiting for send to finish...\n");
  608. timeout = 0;
  609. do {
  610. Dprintk("+");
  611. udelay(100);
  612. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  613. } while (send_status && (timeout++ < 1000));
  614. /*
  615. * Give the other CPU some time to accept the IPI.
  616. */
  617. udelay(200);
  618. /*
  619. * Due to the Pentium erratum 3AP.
  620. */
  621. if (maxlvt > 3) {
  622. apic_write(APIC_ESR, 0);
  623. }
  624. accept_status = (apic_read(APIC_ESR) & 0xEF);
  625. if (send_status || accept_status)
  626. break;
  627. }
  628. Dprintk("After Startup.\n");
  629. if (send_status)
  630. printk(KERN_ERR "APIC never delivered???\n");
  631. if (accept_status)
  632. printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
  633. return (send_status | accept_status);
  634. }
  635. struct create_idle {
  636. struct task_struct *idle;
  637. struct completion done;
  638. int cpu;
  639. };
  640. void do_fork_idle(void *_c_idle)
  641. {
  642. struct create_idle *c_idle = _c_idle;
  643. c_idle->idle = fork_idle(c_idle->cpu);
  644. complete(&c_idle->done);
  645. }
  646. /*
  647. * Boot one CPU.
  648. */
  649. static int __cpuinit do_boot_cpu(int cpu, int apicid)
  650. {
  651. unsigned long boot_error;
  652. int timeout;
  653. unsigned long start_rip;
  654. struct create_idle c_idle = {
  655. .cpu = cpu,
  656. .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
  657. };
  658. DECLARE_WORK(work, do_fork_idle, &c_idle);
  659. /* allocate memory for gdts of secondary cpus. Hotplug is considered */
  660. if (!cpu_gdt_descr[cpu].address &&
  661. !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
  662. printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
  663. return -1;
  664. }
  665. /* Allocate node local memory for AP pdas */
  666. if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
  667. struct x8664_pda *newpda, *pda;
  668. int node = cpu_to_node(cpu);
  669. pda = cpu_pda(cpu);
  670. newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
  671. node);
  672. if (newpda) {
  673. memcpy(newpda, pda, sizeof (struct x8664_pda));
  674. cpu_pda(cpu) = newpda;
  675. } else
  676. printk(KERN_ERR
  677. "Could not allocate node local PDA for CPU %d on node %d\n",
  678. cpu, node);
  679. }
  680. alternatives_smp_switch(1);
  681. c_idle.idle = get_idle_for_cpu(cpu);
  682. if (c_idle.idle) {
  683. c_idle.idle->thread.rsp = (unsigned long) (((struct pt_regs *)
  684. (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
  685. init_idle(c_idle.idle, cpu);
  686. goto do_rest;
  687. }
  688. /*
  689. * During cold boot process, keventd thread is not spun up yet.
  690. * When we do cpu hot-add, we create idle threads on the fly, we should
  691. * not acquire any attributes from the calling context. Hence the clean
  692. * way to create kernel_threads() is to do that from keventd().
  693. * We do the current_is_keventd() due to the fact that ACPI notifier
  694. * was also queuing to keventd() and when the caller is already running
  695. * in context of keventd(), we would end up with locking up the keventd
  696. * thread.
  697. */
  698. if (!keventd_up() || current_is_keventd())
  699. work.func(work.data);
  700. else {
  701. schedule_work(&work);
  702. wait_for_completion(&c_idle.done);
  703. }
  704. if (IS_ERR(c_idle.idle)) {
  705. printk("failed fork for CPU %d\n", cpu);
  706. return PTR_ERR(c_idle.idle);
  707. }
  708. set_idle_for_cpu(cpu, c_idle.idle);
  709. do_rest:
  710. cpu_pda(cpu)->pcurrent = c_idle.idle;
  711. start_rip = setup_trampoline();
  712. init_rsp = c_idle.idle->thread.rsp;
  713. per_cpu(init_tss,cpu).rsp0 = init_rsp;
  714. initial_code = start_secondary;
  715. clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
  716. printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
  717. cpus_weight(cpu_present_map),
  718. apicid);
  719. /*
  720. * This grunge runs the startup process for
  721. * the targeted processor.
  722. */
  723. atomic_set(&init_deasserted, 0);
  724. Dprintk("Setting warm reset code and vector.\n");
  725. CMOS_WRITE(0xa, 0xf);
  726. local_flush_tlb();
  727. Dprintk("1.\n");
  728. *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
  729. Dprintk("2.\n");
  730. *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
  731. Dprintk("3.\n");
  732. /*
  733. * Be paranoid about clearing APIC errors.
  734. */
  735. apic_write(APIC_ESR, 0);
  736. apic_read(APIC_ESR);
  737. /*
  738. * Status is now clean
  739. */
  740. boot_error = 0;
  741. /*
  742. * Starting actual IPI sequence...
  743. */
  744. boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
  745. if (!boot_error) {
  746. /*
  747. * allow APs to start initializing.
  748. */
  749. Dprintk("Before Callout %d.\n", cpu);
  750. cpu_set(cpu, cpu_callout_map);
  751. Dprintk("After Callout %d.\n", cpu);
  752. /*
  753. * Wait 5s total for a response
  754. */
  755. for (timeout = 0; timeout < 50000; timeout++) {
  756. if (cpu_isset(cpu, cpu_callin_map))
  757. break; /* It has booted */
  758. udelay(100);
  759. }
  760. if (cpu_isset(cpu, cpu_callin_map)) {
  761. /* number CPUs logically, starting from 1 (BSP is 0) */
  762. Dprintk("CPU has booted.\n");
  763. } else {
  764. boot_error = 1;
  765. if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
  766. == 0xA5)
  767. /* trampoline started but...? */
  768. printk("Stuck ??\n");
  769. else
  770. /* trampoline code not run */
  771. printk("Not responding.\n");
  772. #ifdef APIC_DEBUG
  773. inquire_remote_apic(apicid);
  774. #endif
  775. }
  776. }
  777. if (boot_error) {
  778. cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
  779. clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
  780. clear_node_cpumask(cpu); /* was set by numa_add_cpu */
  781. cpu_clear(cpu, cpu_present_map);
  782. cpu_clear(cpu, cpu_possible_map);
  783. x86_cpu_to_apicid[cpu] = BAD_APICID;
  784. x86_cpu_to_log_apicid[cpu] = BAD_APICID;
  785. return -EIO;
  786. }
  787. return 0;
  788. }
  789. cycles_t cacheflush_time;
  790. unsigned long cache_decay_ticks;
  791. /*
  792. * Cleanup possible dangling ends...
  793. */
  794. static __cpuinit void smp_cleanup_boot(void)
  795. {
  796. /*
  797. * Paranoid: Set warm reset code and vector here back
  798. * to default values.
  799. */
  800. CMOS_WRITE(0, 0xf);
  801. /*
  802. * Reset trampoline flag
  803. */
  804. *((volatile int *) phys_to_virt(0x467)) = 0;
  805. }
  806. /*
  807. * Fall back to non SMP mode after errors.
  808. *
  809. * RED-PEN audit/test this more. I bet there is more state messed up here.
  810. */
  811. static __init void disable_smp(void)
  812. {
  813. cpu_present_map = cpumask_of_cpu(0);
  814. cpu_possible_map = cpumask_of_cpu(0);
  815. if (smp_found_config)
  816. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
  817. else
  818. phys_cpu_present_map = physid_mask_of_physid(0);
  819. cpu_set(0, cpu_sibling_map[0]);
  820. cpu_set(0, cpu_core_map[0]);
  821. }
  822. #ifdef CONFIG_HOTPLUG_CPU
  823. int additional_cpus __initdata = -1;
  824. /*
  825. * cpu_possible_map should be static, it cannot change as cpu's
  826. * are onlined, or offlined. The reason is per-cpu data-structures
  827. * are allocated by some modules at init time, and dont expect to
  828. * do this dynamically on cpu arrival/departure.
  829. * cpu_present_map on the other hand can change dynamically.
  830. * In case when cpu_hotplug is not compiled, then we resort to current
  831. * behaviour, which is cpu_possible == cpu_present.
  832. * - Ashok Raj
  833. *
  834. * Three ways to find out the number of additional hotplug CPUs:
  835. * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
  836. * - The user can overwrite it with additional_cpus=NUM
  837. * - Otherwise don't reserve additional CPUs.
  838. * We do this because additional CPUs waste a lot of memory.
  839. * -AK
  840. */
  841. __init void prefill_possible_map(void)
  842. {
  843. int i;
  844. int possible;
  845. if (additional_cpus == -1) {
  846. if (disabled_cpus > 0)
  847. additional_cpus = disabled_cpus;
  848. else
  849. additional_cpus = 0;
  850. }
  851. possible = num_processors + additional_cpus;
  852. if (possible > NR_CPUS)
  853. possible = NR_CPUS;
  854. printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
  855. possible,
  856. max_t(int, possible - num_processors, 0));
  857. for (i = 0; i < possible; i++)
  858. cpu_set(i, cpu_possible_map);
  859. }
  860. #endif
  861. /*
  862. * Various sanity checks.
  863. */
  864. static int __init smp_sanity_check(unsigned max_cpus)
  865. {
  866. if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
  867. printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
  868. hard_smp_processor_id());
  869. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  870. }
  871. /*
  872. * If we couldn't find an SMP configuration at boot time,
  873. * get out of here now!
  874. */
  875. if (!smp_found_config) {
  876. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  877. disable_smp();
  878. if (APIC_init_uniprocessor())
  879. printk(KERN_NOTICE "Local APIC not detected."
  880. " Using dummy APIC emulation.\n");
  881. return -1;
  882. }
  883. /*
  884. * Should not be necessary because the MP table should list the boot
  885. * CPU too, but we do it for the sake of robustness anyway.
  886. */
  887. if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
  888. printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
  889. boot_cpu_id);
  890. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  891. }
  892. /*
  893. * If we couldn't find a local APIC, then get out of here now!
  894. */
  895. if (!cpu_has_apic) {
  896. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  897. boot_cpu_id);
  898. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
  899. nr_ioapics = 0;
  900. return -1;
  901. }
  902. /*
  903. * If SMP should be disabled, then really disable it!
  904. */
  905. if (!max_cpus) {
  906. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
  907. nr_ioapics = 0;
  908. return -1;
  909. }
  910. return 0;
  911. }
  912. /*
  913. * Prepare for SMP bootup. The MP table or ACPI has been read
  914. * earlier. Just do some sanity checking here and enable APIC mode.
  915. */
  916. void __init smp_prepare_cpus(unsigned int max_cpus)
  917. {
  918. nmi_watchdog_default();
  919. current_cpu_data = boot_cpu_data;
  920. current_thread_info()->cpu = 0; /* needed? */
  921. set_cpu_sibling_map(0);
  922. if (smp_sanity_check(max_cpus) < 0) {
  923. printk(KERN_INFO "SMP disabled\n");
  924. disable_smp();
  925. return;
  926. }
  927. /*
  928. * Switch from PIC to APIC mode.
  929. */
  930. connect_bsp_APIC();
  931. setup_local_APIC();
  932. if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
  933. panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
  934. GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
  935. /* Or can we switch back to PIC here? */
  936. }
  937. /*
  938. * Now start the IO-APICs
  939. */
  940. if (!skip_ioapic_setup && nr_ioapics)
  941. setup_IO_APIC();
  942. else
  943. nr_ioapics = 0;
  944. /*
  945. * Set up local APIC timer on boot CPU.
  946. */
  947. setup_boot_APIC_clock();
  948. }
  949. /*
  950. * Early setup to make printk work.
  951. */
  952. void __init smp_prepare_boot_cpu(void)
  953. {
  954. int me = smp_processor_id();
  955. cpu_set(me, cpu_online_map);
  956. cpu_set(me, cpu_callout_map);
  957. per_cpu(cpu_state, me) = CPU_ONLINE;
  958. }
  959. /*
  960. * Entry point to boot a CPU.
  961. */
  962. int __cpuinit __cpu_up(unsigned int cpu)
  963. {
  964. int err;
  965. int apicid = cpu_present_to_apicid(cpu);
  966. WARN_ON(irqs_disabled());
  967. Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
  968. if (apicid == BAD_APICID || apicid == boot_cpu_id ||
  969. !physid_isset(apicid, phys_cpu_present_map)) {
  970. printk("__cpu_up: bad cpu %d\n", cpu);
  971. return -EINVAL;
  972. }
  973. /*
  974. * Already booted CPU?
  975. */
  976. if (cpu_isset(cpu, cpu_callin_map)) {
  977. Dprintk("do_boot_cpu %d Already started\n", cpu);
  978. return -ENOSYS;
  979. }
  980. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  981. /* Boot it! */
  982. err = do_boot_cpu(cpu, apicid);
  983. if (err < 0) {
  984. Dprintk("do_boot_cpu failed %d\n", err);
  985. return err;
  986. }
  987. /* Unleash the CPU! */
  988. Dprintk("waiting for cpu %d\n", cpu);
  989. while (!cpu_isset(cpu, cpu_online_map))
  990. cpu_relax();
  991. err = 0;
  992. return err;
  993. }
  994. /*
  995. * Finish the SMP boot.
  996. */
  997. void __init smp_cpus_done(unsigned int max_cpus)
  998. {
  999. smp_cleanup_boot();
  1000. #ifdef CONFIG_X86_IO_APIC
  1001. setup_ioapic_dest();
  1002. #endif
  1003. check_nmi_watchdog();
  1004. }
  1005. #ifdef CONFIG_HOTPLUG_CPU
  1006. static void remove_siblinginfo(int cpu)
  1007. {
  1008. int sibling;
  1009. struct cpuinfo_x86 *c = cpu_data;
  1010. for_each_cpu_mask(sibling, cpu_core_map[cpu]) {
  1011. cpu_clear(cpu, cpu_core_map[sibling]);
  1012. /*
  1013. * last thread sibling in this cpu core going down
  1014. */
  1015. if (cpus_weight(cpu_sibling_map[cpu]) == 1)
  1016. c[sibling].booted_cores--;
  1017. }
  1018. for_each_cpu_mask(sibling, cpu_sibling_map[cpu])
  1019. cpu_clear(cpu, cpu_sibling_map[sibling]);
  1020. cpus_clear(cpu_sibling_map[cpu]);
  1021. cpus_clear(cpu_core_map[cpu]);
  1022. c[cpu].phys_proc_id = 0;
  1023. c[cpu].cpu_core_id = 0;
  1024. cpu_clear(cpu, cpu_sibling_setup_map);
  1025. }
  1026. void remove_cpu_from_maps(void)
  1027. {
  1028. int cpu = smp_processor_id();
  1029. cpu_clear(cpu, cpu_callout_map);
  1030. cpu_clear(cpu, cpu_callin_map);
  1031. clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
  1032. clear_node_cpumask(cpu);
  1033. }
  1034. int __cpu_disable(void)
  1035. {
  1036. int cpu = smp_processor_id();
  1037. /*
  1038. * Perhaps use cpufreq to drop frequency, but that could go
  1039. * into generic code.
  1040. *
  1041. * We won't take down the boot processor on i386 due to some
  1042. * interrupts only being able to be serviced by the BSP.
  1043. * Especially so if we're not using an IOAPIC -zwane
  1044. */
  1045. if (cpu == 0)
  1046. return -EBUSY;
  1047. clear_local_APIC();
  1048. /*
  1049. * HACK:
  1050. * Allow any queued timer interrupts to get serviced
  1051. * This is only a temporary solution until we cleanup
  1052. * fixup_irqs as we do for IA64.
  1053. */
  1054. local_irq_enable();
  1055. mdelay(1);
  1056. local_irq_disable();
  1057. remove_siblinginfo(cpu);
  1058. /* It's now safe to remove this processor from the online map */
  1059. cpu_clear(cpu, cpu_online_map);
  1060. remove_cpu_from_maps();
  1061. fixup_irqs(cpu_online_map);
  1062. return 0;
  1063. }
  1064. void __cpu_die(unsigned int cpu)
  1065. {
  1066. /* We don't do anything here: idle task is faking death itself. */
  1067. unsigned int i;
  1068. for (i = 0; i < 10; i++) {
  1069. /* They ack this in play_dead by setting CPU_DEAD */
  1070. if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
  1071. printk ("CPU %d is now offline\n", cpu);
  1072. if (1 == num_online_cpus())
  1073. alternatives_smp_switch(0);
  1074. return;
  1075. }
  1076. msleep(100);
  1077. }
  1078. printk(KERN_ERR "CPU %u didn't die...\n", cpu);
  1079. }
  1080. __init int setup_additional_cpus(char *s)
  1081. {
  1082. return get_option(&s, &additional_cpus);
  1083. }
  1084. __setup("additional_cpus=", setup_additional_cpus);
  1085. #else /* ... !CONFIG_HOTPLUG_CPU */
  1086. int __cpu_disable(void)
  1087. {
  1088. return -ENOSYS;
  1089. }
  1090. void __cpu_die(unsigned int cpu)
  1091. {
  1092. /* We said "no" in __cpu_disable */
  1093. BUG();
  1094. }
  1095. #endif /* CONFIG_HOTPLUG_CPU */