smpboot.c 36 KB

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