smpboot.c 30 KB

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