smpboot.c 25 KB

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