smpboot.c 34 KB

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