smpboot.c 29 KB

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