smpboot.c 25 KB

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