smpboot.c 34 KB

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