smpboot.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  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. *
  7. * Much of the core SMP work is based on previous work by Thomas Radke, to
  8. * whom a great many thanks are extended.
  9. *
  10. * Thanks to Intel for making available several different Pentium,
  11. * Pentium Pro and Pentium-II/Xeon MP machines.
  12. * Original development of Linux SMP code supported by Caldera.
  13. *
  14. * This code is released under the GNU General Public License version 2 or
  15. * later.
  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 BogoMIPS 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. * Martin J. Bligh : Added support for multi-quad systems
  33. * Dave Jones : Report invalid combinations of Athlon CPUs.
  34. * Rusty Russell : Hacked into shape for new "hotplug" boot process. */
  35. /* SMP boot always wants to use real time delay to allow sufficient time for
  36. * the APs to come online */
  37. #define USE_REAL_TIME_DELAY
  38. #include <linux/module.h>
  39. #include <linux/init.h>
  40. #include <linux/kernel.h>
  41. #include <linux/mm.h>
  42. #include <linux/sched.h>
  43. #include <linux/kernel_stat.h>
  44. #include <linux/smp_lock.h>
  45. #include <linux/bootmem.h>
  46. #include <linux/notifier.h>
  47. #include <linux/cpu.h>
  48. #include <linux/percpu.h>
  49. #include <linux/delay.h>
  50. #include <linux/mc146818rtc.h>
  51. #include <asm/tlbflush.h>
  52. #include <asm/desc.h>
  53. #include <asm/arch_hooks.h>
  54. #include <asm/nmi.h>
  55. #include <asm/pda.h>
  56. #include <asm/genapic.h>
  57. #include <mach_apic.h>
  58. #include <mach_wakecpu.h>
  59. #include <smpboot_hooks.h>
  60. #include <asm/vmi.h>
  61. /* Set if we find a B stepping CPU */
  62. static int __devinitdata smp_b_stepping;
  63. /* Number of siblings per CPU package */
  64. int smp_num_siblings = 1;
  65. EXPORT_SYMBOL(smp_num_siblings);
  66. /* Last level cache ID of each logical CPU */
  67. int cpu_llc_id[NR_CPUS] __cpuinitdata = {[0 ... NR_CPUS-1] = BAD_APICID};
  68. /* representing HT siblings of each logical CPU */
  69. cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
  70. EXPORT_SYMBOL(cpu_sibling_map);
  71. /* representing HT and core siblings of each logical CPU */
  72. cpumask_t cpu_core_map[NR_CPUS] __read_mostly;
  73. EXPORT_SYMBOL(cpu_core_map);
  74. /* bitmap of online cpus */
  75. cpumask_t cpu_online_map __read_mostly;
  76. EXPORT_SYMBOL(cpu_online_map);
  77. cpumask_t cpu_callin_map;
  78. cpumask_t cpu_callout_map;
  79. EXPORT_SYMBOL(cpu_callout_map);
  80. cpumask_t cpu_possible_map;
  81. EXPORT_SYMBOL(cpu_possible_map);
  82. static cpumask_t smp_commenced_mask;
  83. /* TSC's upper 32 bits can't be written in eariler CPU (before prescott), there
  84. * is no way to resync one AP against BP. TBD: for prescott and above, we
  85. * should use IA64's algorithm
  86. */
  87. static int __devinitdata tsc_sync_disabled;
  88. /* Per CPU bogomips and other parameters */
  89. struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
  90. EXPORT_SYMBOL(cpu_data);
  91. u8 x86_cpu_to_apicid[NR_CPUS] __read_mostly =
  92. { [0 ... NR_CPUS-1] = 0xff };
  93. EXPORT_SYMBOL(x86_cpu_to_apicid);
  94. u8 apicid_2_node[MAX_APICID];
  95. /*
  96. * Trampoline 80x86 program as an array.
  97. */
  98. extern unsigned char trampoline_data [];
  99. extern unsigned char trampoline_end [];
  100. static unsigned char *trampoline_base;
  101. static int trampoline_exec;
  102. static void map_cpu_to_logical_apicid(void);
  103. /* State of each CPU. */
  104. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  105. /*
  106. * Currently trivial. Write the real->protected mode
  107. * bootstrap into the page concerned. The caller
  108. * has made sure it's suitably aligned.
  109. */
  110. static unsigned long __devinit setup_trampoline(void)
  111. {
  112. memcpy(trampoline_base, trampoline_data, trampoline_end - trampoline_data);
  113. return virt_to_phys(trampoline_base);
  114. }
  115. /*
  116. * We are called very early to get the low memory for the
  117. * SMP bootup trampoline page.
  118. */
  119. void __init smp_alloc_memory(void)
  120. {
  121. trampoline_base = (void *) alloc_bootmem_low_pages(PAGE_SIZE);
  122. /*
  123. * Has to be in very low memory so we can execute
  124. * real-mode AP code.
  125. */
  126. if (__pa(trampoline_base) >= 0x9F000)
  127. BUG();
  128. /*
  129. * Make the SMP trampoline executable:
  130. */
  131. trampoline_exec = set_kernel_exec((unsigned long)trampoline_base, 1);
  132. }
  133. /*
  134. * The bootstrap kernel entry code has set these up. Save them for
  135. * a given CPU
  136. */
  137. static void __cpuinit smp_store_cpu_info(int id)
  138. {
  139. struct cpuinfo_x86 *c = cpu_data + id;
  140. *c = boot_cpu_data;
  141. if (id!=0)
  142. identify_cpu(c);
  143. /*
  144. * Mask B, Pentium, but not Pentium MMX
  145. */
  146. if (c->x86_vendor == X86_VENDOR_INTEL &&
  147. c->x86 == 5 &&
  148. c->x86_mask >= 1 && c->x86_mask <= 4 &&
  149. c->x86_model <= 3)
  150. /*
  151. * Remember we have B step Pentia with bugs
  152. */
  153. smp_b_stepping = 1;
  154. /*
  155. * Certain Athlons might work (for various values of 'work') in SMP
  156. * but they are not certified as MP capable.
  157. */
  158. if ((c->x86_vendor == X86_VENDOR_AMD) && (c->x86 == 6)) {
  159. if (num_possible_cpus() == 1)
  160. goto valid_k7;
  161. /* Athlon 660/661 is valid. */
  162. if ((c->x86_model==6) && ((c->x86_mask==0) || (c->x86_mask==1)))
  163. goto valid_k7;
  164. /* Duron 670 is valid */
  165. if ((c->x86_model==7) && (c->x86_mask==0))
  166. goto valid_k7;
  167. /*
  168. * Athlon 662, Duron 671, and Athlon >model 7 have capability bit.
  169. * It's worth noting that the A5 stepping (662) of some Athlon XP's
  170. * have the MP bit set.
  171. * See http://www.heise.de/newsticker/data/jow-18.10.01-000 for more.
  172. */
  173. if (((c->x86_model==6) && (c->x86_mask>=2)) ||
  174. ((c->x86_model==7) && (c->x86_mask>=1)) ||
  175. (c->x86_model> 7))
  176. if (cpu_has_mp)
  177. goto valid_k7;
  178. /* If we get here, it's not a certified SMP capable AMD system. */
  179. add_taint(TAINT_UNSAFE_SMP);
  180. }
  181. valid_k7:
  182. ;
  183. }
  184. /*
  185. * TSC synchronization.
  186. *
  187. * We first check whether all CPUs have their TSC's synchronized,
  188. * then we print a warning if not, and always resync.
  189. */
  190. static struct {
  191. atomic_t start_flag;
  192. atomic_t count_start;
  193. atomic_t count_stop;
  194. unsigned long long values[NR_CPUS];
  195. } tsc __cpuinitdata = {
  196. .start_flag = ATOMIC_INIT(0),
  197. .count_start = ATOMIC_INIT(0),
  198. .count_stop = ATOMIC_INIT(0),
  199. };
  200. #define NR_LOOPS 5
  201. static void __init synchronize_tsc_bp(void)
  202. {
  203. int i;
  204. unsigned long long t0;
  205. unsigned long long sum, avg;
  206. long long delta;
  207. unsigned int one_usec;
  208. int buggy = 0;
  209. printk(KERN_INFO "checking TSC synchronization across %u CPUs: ", num_booting_cpus());
  210. /* convert from kcyc/sec to cyc/usec */
  211. one_usec = cpu_khz / 1000;
  212. atomic_set(&tsc.start_flag, 1);
  213. wmb();
  214. /*
  215. * We loop a few times to get a primed instruction cache,
  216. * then the last pass is more or less synchronized and
  217. * the BP and APs set their cycle counters to zero all at
  218. * once. This reduces the chance of having random offsets
  219. * between the processors, and guarantees that the maximum
  220. * delay between the cycle counters is never bigger than
  221. * the latency of information-passing (cachelines) between
  222. * two CPUs.
  223. */
  224. for (i = 0; i < NR_LOOPS; i++) {
  225. /*
  226. * all APs synchronize but they loop on '== num_cpus'
  227. */
  228. while (atomic_read(&tsc.count_start) != num_booting_cpus()-1)
  229. cpu_relax();
  230. atomic_set(&tsc.count_stop, 0);
  231. wmb();
  232. /*
  233. * this lets the APs save their current TSC:
  234. */
  235. atomic_inc(&tsc.count_start);
  236. rdtscll(tsc.values[smp_processor_id()]);
  237. /*
  238. * We clear the TSC in the last loop:
  239. */
  240. if (i == NR_LOOPS-1)
  241. write_tsc(0, 0);
  242. /*
  243. * Wait for all APs to leave the synchronization point:
  244. */
  245. while (atomic_read(&tsc.count_stop) != num_booting_cpus()-1)
  246. cpu_relax();
  247. atomic_set(&tsc.count_start, 0);
  248. wmb();
  249. atomic_inc(&tsc.count_stop);
  250. }
  251. sum = 0;
  252. for (i = 0; i < NR_CPUS; i++) {
  253. if (cpu_isset(i, cpu_callout_map)) {
  254. t0 = tsc.values[i];
  255. sum += t0;
  256. }
  257. }
  258. avg = sum;
  259. do_div(avg, num_booting_cpus());
  260. for (i = 0; i < NR_CPUS; i++) {
  261. if (!cpu_isset(i, cpu_callout_map))
  262. continue;
  263. delta = tsc.values[i] - avg;
  264. if (delta < 0)
  265. delta = -delta;
  266. /*
  267. * We report bigger than 2 microseconds clock differences.
  268. */
  269. if (delta > 2*one_usec) {
  270. long long realdelta;
  271. if (!buggy) {
  272. buggy = 1;
  273. printk("\n");
  274. }
  275. realdelta = delta;
  276. do_div(realdelta, one_usec);
  277. if (tsc.values[i] < avg)
  278. realdelta = -realdelta;
  279. if (realdelta)
  280. printk(KERN_INFO "CPU#%d had %Ld usecs TSC "
  281. "skew, fixed it up.\n", i, realdelta);
  282. }
  283. }
  284. if (!buggy)
  285. printk("passed.\n");
  286. }
  287. static void __cpuinit synchronize_tsc_ap(void)
  288. {
  289. int i;
  290. /*
  291. * Not every cpu is online at the time
  292. * this gets called, so we first wait for the BP to
  293. * finish SMP initialization:
  294. */
  295. while (!atomic_read(&tsc.start_flag))
  296. cpu_relax();
  297. for (i = 0; i < NR_LOOPS; i++) {
  298. atomic_inc(&tsc.count_start);
  299. while (atomic_read(&tsc.count_start) != num_booting_cpus())
  300. cpu_relax();
  301. rdtscll(tsc.values[smp_processor_id()]);
  302. if (i == NR_LOOPS-1)
  303. write_tsc(0, 0);
  304. atomic_inc(&tsc.count_stop);
  305. while (atomic_read(&tsc.count_stop) != num_booting_cpus())
  306. cpu_relax();
  307. }
  308. }
  309. #undef NR_LOOPS
  310. extern void calibrate_delay(void);
  311. static atomic_t init_deasserted;
  312. static void __cpuinit smp_callin(void)
  313. {
  314. int cpuid, phys_id;
  315. unsigned long timeout;
  316. /*
  317. * If waken up by an INIT in an 82489DX configuration
  318. * we may get here before an INIT-deassert IPI reaches
  319. * our local APIC. We have to wait for the IPI or we'll
  320. * lock up on an APIC access.
  321. */
  322. wait_for_init_deassert(&init_deasserted);
  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. printk("huh, phys CPU#%d, CPU#%d already present??\n",
  330. phys_id, cpuid);
  331. BUG();
  332. }
  333. Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
  334. /*
  335. * STARTUP IPIs are fragile beasts as they might sometimes
  336. * trigger some glue motherboard logic. Complete APIC bus
  337. * silence for 1 second, this overestimates the time the
  338. * boot CPU is spending to send the up to 2 STARTUP IPIs
  339. * by a factor of two. This should be enough.
  340. */
  341. /*
  342. * Waiting 2s total for startup (udelay is not yet working)
  343. */
  344. timeout = jiffies + 2*HZ;
  345. while (time_before(jiffies, timeout)) {
  346. /*
  347. * Has the boot CPU finished it's STARTUP sequence?
  348. */
  349. if (cpu_isset(cpuid, cpu_callout_map))
  350. break;
  351. rep_nop();
  352. }
  353. if (!time_before(jiffies, timeout)) {
  354. printk("BUG: CPU%d started up but did not get a callout!\n",
  355. cpuid);
  356. BUG();
  357. }
  358. /*
  359. * the boot CPU has finished the init stage and is spinning
  360. * on callin_map until we finish. We are free to set up this
  361. * CPU, first the APIC. (this is probably redundant on most
  362. * boards)
  363. */
  364. Dprintk("CALLIN, before setup_local_APIC().\n");
  365. smp_callin_clear_local_apic();
  366. setup_local_APIC();
  367. map_cpu_to_logical_apicid();
  368. /*
  369. * Get our bogomips.
  370. */
  371. calibrate_delay();
  372. Dprintk("Stack at about %p\n",&cpuid);
  373. /*
  374. * Save our processor parameters
  375. */
  376. smp_store_cpu_info(cpuid);
  377. disable_APIC_timer();
  378. /*
  379. * Allow the master to continue.
  380. */
  381. cpu_set(cpuid, cpu_callin_map);
  382. /*
  383. * Synchronize the TSC with the BP
  384. */
  385. if (cpu_has_tsc && cpu_khz && !tsc_sync_disabled)
  386. synchronize_tsc_ap();
  387. }
  388. static int cpucount;
  389. /* maps the cpu to the sched domain representing multi-core */
  390. cpumask_t cpu_coregroup_map(int cpu)
  391. {
  392. struct cpuinfo_x86 *c = cpu_data + cpu;
  393. /*
  394. * For perf, we return last level cache shared map.
  395. * And for power savings, we return cpu_core_map
  396. */
  397. if (sched_mc_power_savings || sched_smt_power_savings)
  398. return cpu_core_map[cpu];
  399. else
  400. return c->llc_shared_map;
  401. }
  402. /* representing cpus for which sibling maps can be computed */
  403. static cpumask_t cpu_sibling_setup_map;
  404. static inline void
  405. set_cpu_sibling_map(int cpu)
  406. {
  407. int i;
  408. struct cpuinfo_x86 *c = cpu_data;
  409. cpu_set(cpu, cpu_sibling_setup_map);
  410. if (smp_num_siblings > 1) {
  411. for_each_cpu_mask(i, cpu_sibling_setup_map) {
  412. if (c[cpu].phys_proc_id == c[i].phys_proc_id &&
  413. c[cpu].cpu_core_id == c[i].cpu_core_id) {
  414. cpu_set(i, cpu_sibling_map[cpu]);
  415. cpu_set(cpu, cpu_sibling_map[i]);
  416. cpu_set(i, cpu_core_map[cpu]);
  417. cpu_set(cpu, cpu_core_map[i]);
  418. cpu_set(i, c[cpu].llc_shared_map);
  419. cpu_set(cpu, c[i].llc_shared_map);
  420. }
  421. }
  422. } else {
  423. cpu_set(cpu, cpu_sibling_map[cpu]);
  424. }
  425. cpu_set(cpu, c[cpu].llc_shared_map);
  426. if (current_cpu_data.x86_max_cores == 1) {
  427. cpu_core_map[cpu] = cpu_sibling_map[cpu];
  428. c[cpu].booted_cores = 1;
  429. return;
  430. }
  431. for_each_cpu_mask(i, cpu_sibling_setup_map) {
  432. if (cpu_llc_id[cpu] != BAD_APICID &&
  433. cpu_llc_id[cpu] == cpu_llc_id[i]) {
  434. cpu_set(i, c[cpu].llc_shared_map);
  435. cpu_set(cpu, c[i].llc_shared_map);
  436. }
  437. if (c[cpu].phys_proc_id == c[i].phys_proc_id) {
  438. cpu_set(i, cpu_core_map[cpu]);
  439. cpu_set(cpu, cpu_core_map[i]);
  440. /*
  441. * Does this new cpu bringup a new core?
  442. */
  443. if (cpus_weight(cpu_sibling_map[cpu]) == 1) {
  444. /*
  445. * for each core in package, increment
  446. * the booted_cores for this new cpu
  447. */
  448. if (first_cpu(cpu_sibling_map[i]) == i)
  449. c[cpu].booted_cores++;
  450. /*
  451. * increment the core count for all
  452. * the other cpus in this package
  453. */
  454. if (i != cpu)
  455. c[i].booted_cores++;
  456. } else if (i != cpu && !c[cpu].booted_cores)
  457. c[cpu].booted_cores = c[i].booted_cores;
  458. }
  459. }
  460. }
  461. /*
  462. * Activate a secondary processor.
  463. */
  464. static void __cpuinit start_secondary(void *unused)
  465. {
  466. /*
  467. * Don't put *anything* before secondary_cpu_init(), SMP
  468. * booting is too fragile that we want to limit the
  469. * things done here to the most necessary things.
  470. */
  471. #ifdef CONFIG_VMI
  472. vmi_bringup();
  473. #endif
  474. secondary_cpu_init();
  475. preempt_disable();
  476. smp_callin();
  477. while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
  478. rep_nop();
  479. setup_secondary_clock();
  480. if (nmi_watchdog == NMI_IO_APIC) {
  481. disable_8259A_irq(0);
  482. enable_NMI_through_LVT0(NULL);
  483. enable_8259A_irq(0);
  484. }
  485. enable_APIC_timer();
  486. /*
  487. * low-memory mappings have been cleared, flush them from
  488. * the local TLBs too.
  489. */
  490. local_flush_tlb();
  491. /* This must be done before setting cpu_online_map */
  492. set_cpu_sibling_map(raw_smp_processor_id());
  493. wmb();
  494. /*
  495. * We need to hold call_lock, so there is no inconsistency
  496. * between the time smp_call_function() determines number of
  497. * IPI receipients, and the time when the determination is made
  498. * for which cpus receive the IPI. Holding this
  499. * lock helps us to not include this cpu in a currently in progress
  500. * smp_call_function().
  501. */
  502. lock_ipi_call_lock();
  503. cpu_set(smp_processor_id(), cpu_online_map);
  504. unlock_ipi_call_lock();
  505. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  506. /* We can take interrupts now: we're officially "up". */
  507. local_irq_enable();
  508. wmb();
  509. cpu_idle();
  510. }
  511. /*
  512. * Everything has been set up for the secondary
  513. * CPUs - they just need to reload everything
  514. * from the task structure
  515. * This function must not return.
  516. */
  517. void __devinit initialize_secondary(void)
  518. {
  519. /*
  520. * switch to the per CPU GDT we already set up
  521. * in do_boot_cpu()
  522. */
  523. cpu_set_gdt(current_thread_info()->cpu);
  524. /*
  525. * We don't actually need to load the full TSS,
  526. * basically just the stack pointer and the eip.
  527. */
  528. asm volatile(
  529. "movl %0,%%esp\n\t"
  530. "jmp *%1"
  531. :
  532. :"m" (current->thread.esp),"m" (current->thread.eip));
  533. }
  534. /* Static state in head.S used to set up a CPU */
  535. extern struct {
  536. void * esp;
  537. unsigned short ss;
  538. } stack_start;
  539. extern struct i386_pda *start_pda;
  540. #ifdef CONFIG_NUMA
  541. /* which logical CPUs are on which nodes */
  542. cpumask_t node_2_cpu_mask[MAX_NUMNODES] __read_mostly =
  543. { [0 ... MAX_NUMNODES-1] = CPU_MASK_NONE };
  544. EXPORT_SYMBOL(node_2_cpu_mask);
  545. /* which node each logical CPU is on */
  546. int cpu_2_node[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = 0 };
  547. EXPORT_SYMBOL(cpu_2_node);
  548. /* set up a mapping between cpu and node. */
  549. static inline void map_cpu_to_node(int cpu, int node)
  550. {
  551. printk("Mapping cpu %d to node %d\n", cpu, node);
  552. cpu_set(cpu, node_2_cpu_mask[node]);
  553. cpu_2_node[cpu] = node;
  554. }
  555. /* undo a mapping between cpu and node. */
  556. static inline void unmap_cpu_to_node(int cpu)
  557. {
  558. int node;
  559. printk("Unmapping cpu %d from all nodes\n", cpu);
  560. for (node = 0; node < MAX_NUMNODES; node ++)
  561. cpu_clear(cpu, node_2_cpu_mask[node]);
  562. cpu_2_node[cpu] = 0;
  563. }
  564. #else /* !CONFIG_NUMA */
  565. #define map_cpu_to_node(cpu, node) ({})
  566. #define unmap_cpu_to_node(cpu) ({})
  567. #endif /* CONFIG_NUMA */
  568. u8 cpu_2_logical_apicid[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = BAD_APICID };
  569. static void map_cpu_to_logical_apicid(void)
  570. {
  571. int cpu = smp_processor_id();
  572. int apicid = logical_smp_processor_id();
  573. int node = apicid_to_node(apicid);
  574. if (!node_online(node))
  575. node = first_online_node;
  576. cpu_2_logical_apicid[cpu] = apicid;
  577. map_cpu_to_node(cpu, node);
  578. }
  579. static void unmap_cpu_to_logical_apicid(int cpu)
  580. {
  581. cpu_2_logical_apicid[cpu] = BAD_APICID;
  582. unmap_cpu_to_node(cpu);
  583. }
  584. #if APIC_DEBUG
  585. static inline void __inquire_remote_apic(int apicid)
  586. {
  587. int i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  588. char *names[] = { "ID", "VERSION", "SPIV" };
  589. int timeout, status;
  590. printk("Inquiring remote APIC #%d...\n", apicid);
  591. for (i = 0; i < ARRAY_SIZE(regs); i++) {
  592. printk("... APIC #%d %s: ", apicid, names[i]);
  593. /*
  594. * Wait for idle.
  595. */
  596. apic_wait_icr_idle();
  597. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  598. apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
  599. timeout = 0;
  600. do {
  601. udelay(100);
  602. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  603. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  604. switch (status) {
  605. case APIC_ICR_RR_VALID:
  606. status = apic_read(APIC_RRR);
  607. printk("%08x\n", status);
  608. break;
  609. default:
  610. printk("failed\n");
  611. }
  612. }
  613. }
  614. #endif
  615. #ifdef WAKE_SECONDARY_VIA_NMI
  616. /*
  617. * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
  618. * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
  619. * won't ... remember to clear down the APIC, etc later.
  620. */
  621. static int __devinit
  622. wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
  623. {
  624. unsigned long send_status = 0, accept_status = 0;
  625. int timeout, maxlvt;
  626. /* Target chip */
  627. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
  628. /* Boot on the stack */
  629. /* Kick the second */
  630. apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
  631. Dprintk("Waiting for send to finish...\n");
  632. timeout = 0;
  633. do {
  634. Dprintk("+");
  635. udelay(100);
  636. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  637. } while (send_status && (timeout++ < 1000));
  638. /*
  639. * Give the other CPU some time to accept the IPI.
  640. */
  641. udelay(200);
  642. /*
  643. * Due to the Pentium erratum 3AP.
  644. */
  645. maxlvt = get_maxlvt();
  646. if (maxlvt > 3) {
  647. apic_read_around(APIC_SPIV);
  648. apic_write(APIC_ESR, 0);
  649. }
  650. accept_status = (apic_read(APIC_ESR) & 0xEF);
  651. Dprintk("NMI sent.\n");
  652. if (send_status)
  653. printk("APIC never delivered???\n");
  654. if (accept_status)
  655. printk("APIC delivery error (%lx).\n", accept_status);
  656. return (send_status | accept_status);
  657. }
  658. #endif /* WAKE_SECONDARY_VIA_NMI */
  659. #ifdef WAKE_SECONDARY_VIA_INIT
  660. static int __devinit
  661. wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
  662. {
  663. unsigned long send_status = 0, accept_status = 0;
  664. int maxlvt, timeout, num_starts, j;
  665. /*
  666. * Be paranoid about clearing APIC errors.
  667. */
  668. if (APIC_INTEGRATED(apic_version[phys_apicid])) {
  669. apic_read_around(APIC_SPIV);
  670. apic_write(APIC_ESR, 0);
  671. apic_read(APIC_ESR);
  672. }
  673. Dprintk("Asserting INIT.\n");
  674. /*
  675. * Turn INIT on target chip
  676. */
  677. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  678. /*
  679. * Send IPI
  680. */
  681. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
  682. | APIC_DM_INIT);
  683. Dprintk("Waiting for send to finish...\n");
  684. timeout = 0;
  685. do {
  686. Dprintk("+");
  687. udelay(100);
  688. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  689. } while (send_status && (timeout++ < 1000));
  690. mdelay(10);
  691. Dprintk("Deasserting INIT.\n");
  692. /* Target chip */
  693. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  694. /* Send IPI */
  695. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
  696. Dprintk("Waiting for send to finish...\n");
  697. timeout = 0;
  698. do {
  699. Dprintk("+");
  700. udelay(100);
  701. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  702. } while (send_status && (timeout++ < 1000));
  703. atomic_set(&init_deasserted, 1);
  704. /*
  705. * Should we send STARTUP IPIs ?
  706. *
  707. * Determine this based on the APIC version.
  708. * If we don't have an integrated APIC, don't send the STARTUP IPIs.
  709. */
  710. if (APIC_INTEGRATED(apic_version[phys_apicid]))
  711. num_starts = 2;
  712. else
  713. num_starts = 0;
  714. /*
  715. * Paravirt / VMI wants a startup IPI hook here to set up the
  716. * target processor state.
  717. */
  718. startup_ipi_hook(phys_apicid, (unsigned long) start_secondary,
  719. (unsigned long) stack_start.esp);
  720. /*
  721. * Run STARTUP IPI loop.
  722. */
  723. Dprintk("#startup loops: %d.\n", num_starts);
  724. maxlvt = get_maxlvt();
  725. for (j = 1; j <= num_starts; j++) {
  726. Dprintk("Sending STARTUP #%d.\n",j);
  727. apic_read_around(APIC_SPIV);
  728. apic_write(APIC_ESR, 0);
  729. apic_read(APIC_ESR);
  730. Dprintk("After apic_write.\n");
  731. /*
  732. * STARTUP IPI
  733. */
  734. /* Target chip */
  735. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  736. /* Boot on the stack */
  737. /* Kick the second */
  738. apic_write_around(APIC_ICR, APIC_DM_STARTUP
  739. | (start_eip >> 12));
  740. /*
  741. * Give the other CPU some time to accept the IPI.
  742. */
  743. udelay(300);
  744. Dprintk("Startup point 1.\n");
  745. Dprintk("Waiting for send to finish...\n");
  746. timeout = 0;
  747. do {
  748. Dprintk("+");
  749. udelay(100);
  750. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  751. } while (send_status && (timeout++ < 1000));
  752. /*
  753. * Give the other CPU some time to accept the IPI.
  754. */
  755. udelay(200);
  756. /*
  757. * Due to the Pentium erratum 3AP.
  758. */
  759. if (maxlvt > 3) {
  760. apic_read_around(APIC_SPIV);
  761. apic_write(APIC_ESR, 0);
  762. }
  763. accept_status = (apic_read(APIC_ESR) & 0xEF);
  764. if (send_status || accept_status)
  765. break;
  766. }
  767. Dprintk("After Startup.\n");
  768. if (send_status)
  769. printk("APIC never delivered???\n");
  770. if (accept_status)
  771. printk("APIC delivery error (%lx).\n", accept_status);
  772. return (send_status | accept_status);
  773. }
  774. #endif /* WAKE_SECONDARY_VIA_INIT */
  775. extern cpumask_t cpu_initialized;
  776. static inline int alloc_cpu_id(void)
  777. {
  778. cpumask_t tmp_map;
  779. int cpu;
  780. cpus_complement(tmp_map, cpu_present_map);
  781. cpu = first_cpu(tmp_map);
  782. if (cpu >= NR_CPUS)
  783. return -ENODEV;
  784. return cpu;
  785. }
  786. #ifdef CONFIG_HOTPLUG_CPU
  787. static struct task_struct * __devinitdata cpu_idle_tasks[NR_CPUS];
  788. static inline struct task_struct * alloc_idle_task(int cpu)
  789. {
  790. struct task_struct *idle;
  791. if ((idle = cpu_idle_tasks[cpu]) != NULL) {
  792. /* initialize thread_struct. we really want to avoid destroy
  793. * idle tread
  794. */
  795. idle->thread.esp = (unsigned long)task_pt_regs(idle);
  796. init_idle(idle, cpu);
  797. return idle;
  798. }
  799. idle = fork_idle(cpu);
  800. if (!IS_ERR(idle))
  801. cpu_idle_tasks[cpu] = idle;
  802. return idle;
  803. }
  804. #else
  805. #define alloc_idle_task(cpu) fork_idle(cpu)
  806. #endif
  807. static int __cpuinit do_boot_cpu(int apicid, int cpu)
  808. /*
  809. * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
  810. * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
  811. * Returns zero if CPU booted OK, else error code from wakeup_secondary_cpu.
  812. */
  813. {
  814. struct task_struct *idle;
  815. unsigned long boot_error;
  816. int timeout;
  817. unsigned long start_eip;
  818. unsigned short nmi_high = 0, nmi_low = 0;
  819. /*
  820. * We can't use kernel_thread since we must avoid to
  821. * reschedule the child.
  822. */
  823. idle = alloc_idle_task(cpu);
  824. if (IS_ERR(idle))
  825. panic("failed fork for CPU %d", cpu);
  826. /* Pre-allocate and initialize the CPU's GDT and PDA so it
  827. doesn't have to do any memory allocation during the
  828. delicate CPU-bringup phase. */
  829. if (!init_gdt(cpu, idle)) {
  830. printk(KERN_INFO "Couldn't allocate GDT/PDA for CPU %d\n", cpu);
  831. return -1; /* ? */
  832. }
  833. idle->thread.eip = (unsigned long) start_secondary;
  834. /* start_eip had better be page-aligned! */
  835. start_eip = setup_trampoline();
  836. ++cpucount;
  837. alternatives_smp_switch(1);
  838. /* So we see what's up */
  839. printk("Booting processor %d/%d eip %lx\n", cpu, apicid, start_eip);
  840. /* Stack for startup_32 can be just as for start_secondary onwards */
  841. stack_start.esp = (void *) idle->thread.esp;
  842. irq_ctx_init(cpu);
  843. x86_cpu_to_apicid[cpu] = apicid;
  844. /*
  845. * This grunge runs the startup process for
  846. * the targeted processor.
  847. */
  848. atomic_set(&init_deasserted, 0);
  849. Dprintk("Setting warm reset code and vector.\n");
  850. store_NMI_vector(&nmi_high, &nmi_low);
  851. smpboot_setup_warm_reset_vector(start_eip);
  852. /*
  853. * Starting actual IPI sequence...
  854. */
  855. boot_error = wakeup_secondary_cpu(apicid, start_eip);
  856. if (!boot_error) {
  857. /*
  858. * allow APs to start initializing.
  859. */
  860. Dprintk("Before Callout %d.\n", cpu);
  861. cpu_set(cpu, cpu_callout_map);
  862. Dprintk("After Callout %d.\n", cpu);
  863. /*
  864. * Wait 5s total for a response
  865. */
  866. for (timeout = 0; timeout < 50000; timeout++) {
  867. if (cpu_isset(cpu, cpu_callin_map))
  868. break; /* It has booted */
  869. udelay(100);
  870. }
  871. if (cpu_isset(cpu, cpu_callin_map)) {
  872. /* number CPUs logically, starting from 1 (BSP is 0) */
  873. Dprintk("OK.\n");
  874. printk("CPU%d: ", cpu);
  875. print_cpu_info(&cpu_data[cpu]);
  876. Dprintk("CPU has booted.\n");
  877. } else {
  878. boot_error= 1;
  879. if (*((volatile unsigned char *)trampoline_base)
  880. == 0xA5)
  881. /* trampoline started but...? */
  882. printk("Stuck ??\n");
  883. else
  884. /* trampoline code not run */
  885. printk("Not responding.\n");
  886. inquire_remote_apic(apicid);
  887. }
  888. }
  889. if (boot_error) {
  890. /* Try to put things back the way they were before ... */
  891. unmap_cpu_to_logical_apicid(cpu);
  892. cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
  893. cpu_clear(cpu, cpu_initialized); /* was set by cpu_init() */
  894. cpucount--;
  895. } else {
  896. x86_cpu_to_apicid[cpu] = apicid;
  897. cpu_set(cpu, cpu_present_map);
  898. }
  899. /* mark "stuck" area as not stuck */
  900. *((volatile unsigned long *)trampoline_base) = 0;
  901. return boot_error;
  902. }
  903. #ifdef CONFIG_HOTPLUG_CPU
  904. void cpu_exit_clear(void)
  905. {
  906. int cpu = raw_smp_processor_id();
  907. idle_task_exit();
  908. cpucount --;
  909. cpu_uninit();
  910. irq_ctx_exit(cpu);
  911. cpu_clear(cpu, cpu_callout_map);
  912. cpu_clear(cpu, cpu_callin_map);
  913. cpu_clear(cpu, smp_commenced_mask);
  914. unmap_cpu_to_logical_apicid(cpu);
  915. }
  916. struct warm_boot_cpu_info {
  917. struct completion *complete;
  918. struct work_struct task;
  919. int apicid;
  920. int cpu;
  921. };
  922. static void __cpuinit do_warm_boot_cpu(struct work_struct *work)
  923. {
  924. struct warm_boot_cpu_info *info =
  925. container_of(work, struct warm_boot_cpu_info, task);
  926. do_boot_cpu(info->apicid, info->cpu);
  927. complete(info->complete);
  928. }
  929. static int __cpuinit __smp_prepare_cpu(int cpu)
  930. {
  931. DECLARE_COMPLETION_ONSTACK(done);
  932. struct warm_boot_cpu_info info;
  933. int apicid, ret;
  934. struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
  935. apicid = x86_cpu_to_apicid[cpu];
  936. if (apicid == BAD_APICID) {
  937. ret = -ENODEV;
  938. goto exit;
  939. }
  940. /*
  941. * the CPU isn't initialized at boot time, allocate gdt table here.
  942. * cpu_init will initialize it
  943. */
  944. if (!cpu_gdt_descr->address) {
  945. cpu_gdt_descr->address = get_zeroed_page(GFP_KERNEL);
  946. if (!cpu_gdt_descr->address)
  947. printk(KERN_CRIT "CPU%d failed to allocate GDT\n", cpu);
  948. ret = -ENOMEM;
  949. goto exit;
  950. }
  951. info.complete = &done;
  952. info.apicid = apicid;
  953. info.cpu = cpu;
  954. INIT_WORK(&info.task, do_warm_boot_cpu);
  955. tsc_sync_disabled = 1;
  956. /* init low mem mapping */
  957. clone_pgd_range(swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
  958. min_t(unsigned long, KERNEL_PGD_PTRS, USER_PGD_PTRS));
  959. flush_tlb_all();
  960. schedule_work(&info.task);
  961. wait_for_completion(&done);
  962. tsc_sync_disabled = 0;
  963. zap_low_mappings();
  964. ret = 0;
  965. exit:
  966. return ret;
  967. }
  968. #endif
  969. static void smp_tune_scheduling(void)
  970. {
  971. unsigned long cachesize; /* kB */
  972. if (cpu_khz) {
  973. cachesize = boot_cpu_data.x86_cache_size;
  974. if (cachesize > 0)
  975. max_cache_size = cachesize * 1024;
  976. }
  977. }
  978. /*
  979. * Cycle through the processors sending APIC IPIs to boot each.
  980. */
  981. static int boot_cpu_logical_apicid;
  982. /* Where the IO area was mapped on multiquad, always 0 otherwise */
  983. void *xquad_portio;
  984. #ifdef CONFIG_X86_NUMAQ
  985. EXPORT_SYMBOL(xquad_portio);
  986. #endif
  987. static void __init smp_boot_cpus(unsigned int max_cpus)
  988. {
  989. int apicid, cpu, bit, kicked;
  990. unsigned long bogosum = 0;
  991. /*
  992. * Setup boot CPU information
  993. */
  994. smp_store_cpu_info(0); /* Final full version of the data */
  995. printk("CPU%d: ", 0);
  996. print_cpu_info(&cpu_data[0]);
  997. boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
  998. boot_cpu_logical_apicid = logical_smp_processor_id();
  999. x86_cpu_to_apicid[0] = boot_cpu_physical_apicid;
  1000. current_thread_info()->cpu = 0;
  1001. smp_tune_scheduling();
  1002. set_cpu_sibling_map(0);
  1003. /*
  1004. * If we couldn't find an SMP configuration at boot time,
  1005. * get out of here now!
  1006. */
  1007. if (!smp_found_config && !acpi_lapic) {
  1008. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  1009. smpboot_clear_io_apic_irqs();
  1010. phys_cpu_present_map = physid_mask_of_physid(0);
  1011. if (APIC_init_uniprocessor())
  1012. printk(KERN_NOTICE "Local APIC not detected."
  1013. " Using dummy APIC emulation.\n");
  1014. map_cpu_to_logical_apicid();
  1015. cpu_set(0, cpu_sibling_map[0]);
  1016. cpu_set(0, cpu_core_map[0]);
  1017. return;
  1018. }
  1019. /*
  1020. * Should not be necessary because the MP table should list the boot
  1021. * CPU too, but we do it for the sake of robustness anyway.
  1022. * Makes no sense to do this check in clustered apic mode, so skip it
  1023. */
  1024. if (!check_phys_apicid_present(boot_cpu_physical_apicid)) {
  1025. printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
  1026. boot_cpu_physical_apicid);
  1027. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  1028. }
  1029. /*
  1030. * If we couldn't find a local APIC, then get out of here now!
  1031. */
  1032. if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) && !cpu_has_apic) {
  1033. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  1034. boot_cpu_physical_apicid);
  1035. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
  1036. smpboot_clear_io_apic_irqs();
  1037. phys_cpu_present_map = physid_mask_of_physid(0);
  1038. cpu_set(0, cpu_sibling_map[0]);
  1039. cpu_set(0, cpu_core_map[0]);
  1040. return;
  1041. }
  1042. verify_local_APIC();
  1043. /*
  1044. * If SMP should be disabled, then really disable it!
  1045. */
  1046. if (!max_cpus) {
  1047. smp_found_config = 0;
  1048. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
  1049. smpboot_clear_io_apic_irqs();
  1050. phys_cpu_present_map = physid_mask_of_physid(0);
  1051. cpu_set(0, cpu_sibling_map[0]);
  1052. cpu_set(0, cpu_core_map[0]);
  1053. return;
  1054. }
  1055. connect_bsp_APIC();
  1056. setup_local_APIC();
  1057. map_cpu_to_logical_apicid();
  1058. setup_portio_remap();
  1059. /*
  1060. * Scan the CPU present map and fire up the other CPUs via do_boot_cpu
  1061. *
  1062. * In clustered apic mode, phys_cpu_present_map is a constructed thus:
  1063. * bits 0-3 are quad0, 4-7 are quad1, etc. A perverse twist on the
  1064. * clustered apic ID.
  1065. */
  1066. Dprintk("CPU present map: %lx\n", physids_coerce(phys_cpu_present_map));
  1067. kicked = 1;
  1068. for (bit = 0; kicked < NR_CPUS && bit < MAX_APICS; bit++) {
  1069. apicid = cpu_present_to_apicid(bit);
  1070. /*
  1071. * Don't even attempt to start the boot CPU!
  1072. */
  1073. if ((apicid == boot_cpu_apicid) || (apicid == BAD_APICID))
  1074. continue;
  1075. if (!check_apicid_present(bit))
  1076. continue;
  1077. if (max_cpus <= cpucount+1)
  1078. continue;
  1079. if (((cpu = alloc_cpu_id()) <= 0) || do_boot_cpu(apicid, cpu))
  1080. printk("CPU #%d not responding - cannot use it.\n",
  1081. apicid);
  1082. else
  1083. ++kicked;
  1084. }
  1085. /*
  1086. * Cleanup possible dangling ends...
  1087. */
  1088. smpboot_restore_warm_reset_vector();
  1089. /*
  1090. * Allow the user to impress friends.
  1091. */
  1092. Dprintk("Before bogomips.\n");
  1093. for (cpu = 0; cpu < NR_CPUS; cpu++)
  1094. if (cpu_isset(cpu, cpu_callout_map))
  1095. bogosum += cpu_data[cpu].loops_per_jiffy;
  1096. printk(KERN_INFO
  1097. "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
  1098. cpucount+1,
  1099. bogosum/(500000/HZ),
  1100. (bogosum/(5000/HZ))%100);
  1101. Dprintk("Before bogocount - setting activated=1.\n");
  1102. if (smp_b_stepping)
  1103. printk(KERN_WARNING "WARNING: SMP operation may be unreliable with B stepping processors.\n");
  1104. /*
  1105. * Don't taint if we are running SMP kernel on a single non-MP
  1106. * approved Athlon
  1107. */
  1108. if (tainted & TAINT_UNSAFE_SMP) {
  1109. if (cpucount)
  1110. printk (KERN_INFO "WARNING: This combination of AMD processors is not suitable for SMP.\n");
  1111. else
  1112. tainted &= ~TAINT_UNSAFE_SMP;
  1113. }
  1114. Dprintk("Boot done.\n");
  1115. /*
  1116. * construct cpu_sibling_map[], so that we can tell sibling CPUs
  1117. * efficiently.
  1118. */
  1119. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  1120. cpus_clear(cpu_sibling_map[cpu]);
  1121. cpus_clear(cpu_core_map[cpu]);
  1122. }
  1123. cpu_set(0, cpu_sibling_map[0]);
  1124. cpu_set(0, cpu_core_map[0]);
  1125. smpboot_setup_io_apic();
  1126. setup_boot_clock();
  1127. /*
  1128. * Synchronize the TSC with the AP
  1129. */
  1130. if (cpu_has_tsc && cpucount && cpu_khz)
  1131. synchronize_tsc_bp();
  1132. }
  1133. /* These are wrappers to interface to the new boot process. Someone
  1134. who understands all this stuff should rewrite it properly. --RR 15/Jul/02 */
  1135. void __init smp_prepare_cpus(unsigned int max_cpus)
  1136. {
  1137. smp_commenced_mask = cpumask_of_cpu(0);
  1138. cpu_callin_map = cpumask_of_cpu(0);
  1139. mb();
  1140. smp_boot_cpus(max_cpus);
  1141. }
  1142. void __devinit smp_prepare_boot_cpu(void)
  1143. {
  1144. cpu_set(smp_processor_id(), cpu_online_map);
  1145. cpu_set(smp_processor_id(), cpu_callout_map);
  1146. cpu_set(smp_processor_id(), cpu_present_map);
  1147. cpu_set(smp_processor_id(), cpu_possible_map);
  1148. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  1149. }
  1150. #ifdef CONFIG_HOTPLUG_CPU
  1151. static void
  1152. remove_siblinginfo(int cpu)
  1153. {
  1154. int sibling;
  1155. struct cpuinfo_x86 *c = cpu_data;
  1156. for_each_cpu_mask(sibling, cpu_core_map[cpu]) {
  1157. cpu_clear(cpu, cpu_core_map[sibling]);
  1158. /*
  1159. * last thread sibling in this cpu core going down
  1160. */
  1161. if (cpus_weight(cpu_sibling_map[cpu]) == 1)
  1162. c[sibling].booted_cores--;
  1163. }
  1164. for_each_cpu_mask(sibling, cpu_sibling_map[cpu])
  1165. cpu_clear(cpu, cpu_sibling_map[sibling]);
  1166. cpus_clear(cpu_sibling_map[cpu]);
  1167. cpus_clear(cpu_core_map[cpu]);
  1168. c[cpu].phys_proc_id = 0;
  1169. c[cpu].cpu_core_id = 0;
  1170. cpu_clear(cpu, cpu_sibling_setup_map);
  1171. }
  1172. int __cpu_disable(void)
  1173. {
  1174. cpumask_t map = cpu_online_map;
  1175. int cpu = smp_processor_id();
  1176. /*
  1177. * Perhaps use cpufreq to drop frequency, but that could go
  1178. * into generic code.
  1179. *
  1180. * We won't take down the boot processor on i386 due to some
  1181. * interrupts only being able to be serviced by the BSP.
  1182. * Especially so if we're not using an IOAPIC -zwane
  1183. */
  1184. if (cpu == 0)
  1185. return -EBUSY;
  1186. if (nmi_watchdog == NMI_LOCAL_APIC)
  1187. stop_apic_nmi_watchdog(NULL);
  1188. clear_local_APIC();
  1189. /* Allow any queued timer interrupts to get serviced */
  1190. local_irq_enable();
  1191. mdelay(1);
  1192. local_irq_disable();
  1193. remove_siblinginfo(cpu);
  1194. cpu_clear(cpu, map);
  1195. fixup_irqs(map);
  1196. /* It's now safe to remove this processor from the online map */
  1197. cpu_clear(cpu, cpu_online_map);
  1198. return 0;
  1199. }
  1200. void __cpu_die(unsigned int cpu)
  1201. {
  1202. /* We don't do anything here: idle task is faking death itself. */
  1203. unsigned int i;
  1204. for (i = 0; i < 10; i++) {
  1205. /* They ack this in play_dead by setting CPU_DEAD */
  1206. if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
  1207. printk ("CPU %d is now offline\n", cpu);
  1208. if (1 == num_online_cpus())
  1209. alternatives_smp_switch(0);
  1210. return;
  1211. }
  1212. msleep(100);
  1213. }
  1214. printk(KERN_ERR "CPU %u didn't die...\n", cpu);
  1215. }
  1216. #else /* ... !CONFIG_HOTPLUG_CPU */
  1217. int __cpu_disable(void)
  1218. {
  1219. return -ENOSYS;
  1220. }
  1221. void __cpu_die(unsigned int cpu)
  1222. {
  1223. /* We said "no" in __cpu_disable */
  1224. BUG();
  1225. }
  1226. #endif /* CONFIG_HOTPLUG_CPU */
  1227. int __cpuinit __cpu_up(unsigned int cpu)
  1228. {
  1229. #ifdef CONFIG_HOTPLUG_CPU
  1230. int ret=0;
  1231. /*
  1232. * We do warm boot only on cpus that had booted earlier
  1233. * Otherwise cold boot is all handled from smp_boot_cpus().
  1234. * cpu_callin_map is set during AP kickstart process. Its reset
  1235. * when a cpu is taken offline from cpu_exit_clear().
  1236. */
  1237. if (!cpu_isset(cpu, cpu_callin_map))
  1238. ret = __smp_prepare_cpu(cpu);
  1239. if (ret)
  1240. return -EIO;
  1241. #endif
  1242. /* In case one didn't come up */
  1243. if (!cpu_isset(cpu, cpu_callin_map)) {
  1244. printk(KERN_DEBUG "skipping cpu%d, didn't come online\n", cpu);
  1245. local_irq_enable();
  1246. return -EIO;
  1247. }
  1248. local_irq_enable();
  1249. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  1250. /* Unleash the CPU! */
  1251. cpu_set(cpu, smp_commenced_mask);
  1252. while (!cpu_isset(cpu, cpu_online_map))
  1253. cpu_relax();
  1254. #ifdef CONFIG_X86_GENERICARCH
  1255. if (num_online_cpus() > 8 && genapic == &apic_default)
  1256. panic("Default flat APIC routing can't be used with > 8 cpus\n");
  1257. #endif
  1258. return 0;
  1259. }
  1260. void __init smp_cpus_done(unsigned int max_cpus)
  1261. {
  1262. #ifdef CONFIG_X86_IO_APIC
  1263. setup_ioapic_dest();
  1264. #endif
  1265. zap_low_mappings();
  1266. #ifndef CONFIG_HOTPLUG_CPU
  1267. /*
  1268. * Disable executability of the SMP trampoline:
  1269. */
  1270. set_kernel_exec((unsigned long)trampoline_base, trampoline_exec);
  1271. #endif
  1272. }
  1273. void __init smp_intr_init(void)
  1274. {
  1275. /*
  1276. * IRQ0 must be given a fixed assignment and initialized,
  1277. * because it's used before the IO-APIC is set up.
  1278. */
  1279. set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
  1280. /*
  1281. * The reschedule interrupt is a CPU-to-CPU reschedule-helper
  1282. * IPI, driven by wakeup.
  1283. */
  1284. set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
  1285. /* IPI for invalidation */
  1286. set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
  1287. /* IPI for generic function call */
  1288. set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
  1289. }
  1290. /*
  1291. * If the BIOS enumerates physical processors before logical,
  1292. * maxcpus=N at enumeration-time can be used to disable HT.
  1293. */
  1294. static int __init parse_maxcpus(char *arg)
  1295. {
  1296. extern unsigned int maxcpus;
  1297. maxcpus = simple_strtoul(arg, NULL, 0);
  1298. return 0;
  1299. }
  1300. early_param("maxcpus", parse_maxcpus);