smpboot.c 31 KB

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