smpboot.c 36 KB

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