smpboot.c 35 KB

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