smpboot.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404
  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. EXPORT_SYMBOL(phys_proc_id);
  65. /* Core ID of each logical CPU */
  66. int cpu_core_id[NR_CPUS] __read_mostly = {[0 ... NR_CPUS-1] = BAD_APICID};
  67. EXPORT_SYMBOL(cpu_core_id);
  68. cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
  69. EXPORT_SYMBOL(cpu_sibling_map);
  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. static inline void
  381. set_cpu_sibling_map(int cpu)
  382. {
  383. int i;
  384. if (smp_num_siblings > 1) {
  385. for (i = 0; i < NR_CPUS; i++) {
  386. if (!cpu_isset(i, cpu_callout_map))
  387. continue;
  388. if (cpu_core_id[cpu] == cpu_core_id[i]) {
  389. cpu_set(i, cpu_sibling_map[cpu]);
  390. cpu_set(cpu, cpu_sibling_map[i]);
  391. }
  392. }
  393. } else {
  394. cpu_set(cpu, cpu_sibling_map[cpu]);
  395. }
  396. if (current_cpu_data.x86_num_cores > 1) {
  397. for (i = 0; i < NR_CPUS; i++) {
  398. if (!cpu_isset(i, cpu_callout_map))
  399. continue;
  400. if (phys_proc_id[cpu] == phys_proc_id[i]) {
  401. cpu_set(i, cpu_core_map[cpu]);
  402. cpu_set(cpu, cpu_core_map[i]);
  403. }
  404. }
  405. } else {
  406. cpu_core_map[cpu] = cpu_sibling_map[cpu];
  407. }
  408. }
  409. /*
  410. * Activate a secondary processor.
  411. */
  412. static void __devinit start_secondary(void *unused)
  413. {
  414. /*
  415. * Dont put anything before smp_callin(), SMP
  416. * booting is too fragile that we want to limit the
  417. * things done here to the most necessary things.
  418. */
  419. cpu_init();
  420. smp_callin();
  421. while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
  422. rep_nop();
  423. setup_secondary_APIC_clock();
  424. if (nmi_watchdog == NMI_IO_APIC) {
  425. disable_8259A_irq(0);
  426. enable_NMI_through_LVT0(NULL);
  427. enable_8259A_irq(0);
  428. }
  429. enable_APIC_timer();
  430. /*
  431. * low-memory mappings have been cleared, flush them from
  432. * the local TLBs too.
  433. */
  434. local_flush_tlb();
  435. /* This must be done before setting cpu_online_map */
  436. set_cpu_sibling_map(raw_smp_processor_id());
  437. wmb();
  438. /*
  439. * We need to hold call_lock, so there is no inconsistency
  440. * between the time smp_call_function() determines number of
  441. * IPI receipients, and the time when the determination is made
  442. * for which cpus receive the IPI. Holding this
  443. * lock helps us to not include this cpu in a currently in progress
  444. * smp_call_function().
  445. */
  446. lock_ipi_call_lock();
  447. cpu_set(smp_processor_id(), cpu_online_map);
  448. unlock_ipi_call_lock();
  449. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  450. /* We can take interrupts now: we're officially "up". */
  451. local_irq_enable();
  452. wmb();
  453. cpu_idle();
  454. }
  455. /*
  456. * Everything has been set up for the secondary
  457. * CPUs - they just need to reload everything
  458. * from the task structure
  459. * This function must not return.
  460. */
  461. void __devinit initialize_secondary(void)
  462. {
  463. /*
  464. * We don't actually need to load the full TSS,
  465. * basically just the stack pointer and the eip.
  466. */
  467. asm volatile(
  468. "movl %0,%%esp\n\t"
  469. "jmp *%1"
  470. :
  471. :"r" (current->thread.esp),"r" (current->thread.eip));
  472. }
  473. extern struct {
  474. void * esp;
  475. unsigned short ss;
  476. } stack_start;
  477. #ifdef CONFIG_NUMA
  478. /* which logical CPUs are on which nodes */
  479. cpumask_t node_2_cpu_mask[MAX_NUMNODES] __read_mostly =
  480. { [0 ... MAX_NUMNODES-1] = CPU_MASK_NONE };
  481. /* which node each logical CPU is on */
  482. int cpu_2_node[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = 0 };
  483. EXPORT_SYMBOL(cpu_2_node);
  484. /* set up a mapping between cpu and node. */
  485. static inline void map_cpu_to_node(int cpu, int node)
  486. {
  487. printk("Mapping cpu %d to node %d\n", cpu, node);
  488. cpu_set(cpu, node_2_cpu_mask[node]);
  489. cpu_2_node[cpu] = node;
  490. }
  491. /* undo a mapping between cpu and node. */
  492. static inline void unmap_cpu_to_node(int cpu)
  493. {
  494. int node;
  495. printk("Unmapping cpu %d from all nodes\n", cpu);
  496. for (node = 0; node < MAX_NUMNODES; node ++)
  497. cpu_clear(cpu, node_2_cpu_mask[node]);
  498. cpu_2_node[cpu] = 0;
  499. }
  500. #else /* !CONFIG_NUMA */
  501. #define map_cpu_to_node(cpu, node) ({})
  502. #define unmap_cpu_to_node(cpu) ({})
  503. #endif /* CONFIG_NUMA */
  504. u8 cpu_2_logical_apicid[NR_CPUS] __read_mostly = { [0 ... NR_CPUS-1] = BAD_APICID };
  505. static void map_cpu_to_logical_apicid(void)
  506. {
  507. int cpu = smp_processor_id();
  508. int apicid = logical_smp_processor_id();
  509. cpu_2_logical_apicid[cpu] = apicid;
  510. map_cpu_to_node(cpu, apicid_to_node(apicid));
  511. }
  512. static void unmap_cpu_to_logical_apicid(int cpu)
  513. {
  514. cpu_2_logical_apicid[cpu] = BAD_APICID;
  515. unmap_cpu_to_node(cpu);
  516. }
  517. #if APIC_DEBUG
  518. static inline void __inquire_remote_apic(int apicid)
  519. {
  520. int i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  521. char *names[] = { "ID", "VERSION", "SPIV" };
  522. int timeout, status;
  523. printk("Inquiring remote APIC #%d...\n", apicid);
  524. for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
  525. printk("... APIC #%d %s: ", apicid, names[i]);
  526. /*
  527. * Wait for idle.
  528. */
  529. apic_wait_icr_idle();
  530. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  531. apic_write_around(APIC_ICR, APIC_DM_REMRD | regs[i]);
  532. timeout = 0;
  533. do {
  534. udelay(100);
  535. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  536. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  537. switch (status) {
  538. case APIC_ICR_RR_VALID:
  539. status = apic_read(APIC_RRR);
  540. printk("%08x\n", status);
  541. break;
  542. default:
  543. printk("failed\n");
  544. }
  545. }
  546. }
  547. #endif
  548. #ifdef WAKE_SECONDARY_VIA_NMI
  549. /*
  550. * Poke the other CPU in the eye via NMI to wake it up. Remember that the normal
  551. * INIT, INIT, STARTUP sequence will reset the chip hard for us, and this
  552. * won't ... remember to clear down the APIC, etc later.
  553. */
  554. static int __devinit
  555. wakeup_secondary_cpu(int logical_apicid, unsigned long start_eip)
  556. {
  557. unsigned long send_status = 0, accept_status = 0;
  558. int timeout, maxlvt;
  559. /* Target chip */
  560. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(logical_apicid));
  561. /* Boot on the stack */
  562. /* Kick the second */
  563. apic_write_around(APIC_ICR, APIC_DM_NMI | APIC_DEST_LOGICAL);
  564. Dprintk("Waiting for send to finish...\n");
  565. timeout = 0;
  566. do {
  567. Dprintk("+");
  568. udelay(100);
  569. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  570. } while (send_status && (timeout++ < 1000));
  571. /*
  572. * Give the other CPU some time to accept the IPI.
  573. */
  574. udelay(200);
  575. /*
  576. * Due to the Pentium erratum 3AP.
  577. */
  578. maxlvt = get_maxlvt();
  579. if (maxlvt > 3) {
  580. apic_read_around(APIC_SPIV);
  581. apic_write(APIC_ESR, 0);
  582. }
  583. accept_status = (apic_read(APIC_ESR) & 0xEF);
  584. Dprintk("NMI sent.\n");
  585. if (send_status)
  586. printk("APIC never delivered???\n");
  587. if (accept_status)
  588. printk("APIC delivery error (%lx).\n", accept_status);
  589. return (send_status | accept_status);
  590. }
  591. #endif /* WAKE_SECONDARY_VIA_NMI */
  592. #ifdef WAKE_SECONDARY_VIA_INIT
  593. static int __devinit
  594. wakeup_secondary_cpu(int phys_apicid, unsigned long start_eip)
  595. {
  596. unsigned long send_status = 0, accept_status = 0;
  597. int maxlvt, timeout, num_starts, j;
  598. /*
  599. * Be paranoid about clearing APIC errors.
  600. */
  601. if (APIC_INTEGRATED(apic_version[phys_apicid])) {
  602. apic_read_around(APIC_SPIV);
  603. apic_write(APIC_ESR, 0);
  604. apic_read(APIC_ESR);
  605. }
  606. Dprintk("Asserting INIT.\n");
  607. /*
  608. * Turn INIT on target chip
  609. */
  610. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  611. /*
  612. * Send IPI
  613. */
  614. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
  615. | APIC_DM_INIT);
  616. Dprintk("Waiting for send to finish...\n");
  617. timeout = 0;
  618. do {
  619. Dprintk("+");
  620. udelay(100);
  621. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  622. } while (send_status && (timeout++ < 1000));
  623. mdelay(10);
  624. Dprintk("Deasserting INIT.\n");
  625. /* Target chip */
  626. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  627. /* Send IPI */
  628. apic_write_around(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
  629. Dprintk("Waiting for send to finish...\n");
  630. timeout = 0;
  631. do {
  632. Dprintk("+");
  633. udelay(100);
  634. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  635. } while (send_status && (timeout++ < 1000));
  636. atomic_set(&init_deasserted, 1);
  637. /*
  638. * Should we send STARTUP IPIs ?
  639. *
  640. * Determine this based on the APIC version.
  641. * If we don't have an integrated APIC, don't send the STARTUP IPIs.
  642. */
  643. if (APIC_INTEGRATED(apic_version[phys_apicid]))
  644. num_starts = 2;
  645. else
  646. num_starts = 0;
  647. /*
  648. * Run STARTUP IPI loop.
  649. */
  650. Dprintk("#startup loops: %d.\n", num_starts);
  651. maxlvt = get_maxlvt();
  652. for (j = 1; j <= num_starts; j++) {
  653. Dprintk("Sending STARTUP #%d.\n",j);
  654. apic_read_around(APIC_SPIV);
  655. apic_write(APIC_ESR, 0);
  656. apic_read(APIC_ESR);
  657. Dprintk("After apic_write.\n");
  658. /*
  659. * STARTUP IPI
  660. */
  661. /* Target chip */
  662. apic_write_around(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  663. /* Boot on the stack */
  664. /* Kick the second */
  665. apic_write_around(APIC_ICR, APIC_DM_STARTUP
  666. | (start_eip >> 12));
  667. /*
  668. * Give the other CPU some time to accept the IPI.
  669. */
  670. udelay(300);
  671. Dprintk("Startup point 1.\n");
  672. Dprintk("Waiting for send to finish...\n");
  673. timeout = 0;
  674. do {
  675. Dprintk("+");
  676. udelay(100);
  677. send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
  678. } while (send_status && (timeout++ < 1000));
  679. /*
  680. * Give the other CPU some time to accept the IPI.
  681. */
  682. udelay(200);
  683. /*
  684. * Due to the Pentium erratum 3AP.
  685. */
  686. if (maxlvt > 3) {
  687. apic_read_around(APIC_SPIV);
  688. apic_write(APIC_ESR, 0);
  689. }
  690. accept_status = (apic_read(APIC_ESR) & 0xEF);
  691. if (send_status || accept_status)
  692. break;
  693. }
  694. Dprintk("After Startup.\n");
  695. if (send_status)
  696. printk("APIC never delivered???\n");
  697. if (accept_status)
  698. printk("APIC delivery error (%lx).\n", accept_status);
  699. return (send_status | accept_status);
  700. }
  701. #endif /* WAKE_SECONDARY_VIA_INIT */
  702. extern cpumask_t cpu_initialized;
  703. static inline int alloc_cpu_id(void)
  704. {
  705. cpumask_t tmp_map;
  706. int cpu;
  707. cpus_complement(tmp_map, cpu_present_map);
  708. cpu = first_cpu(tmp_map);
  709. if (cpu >= NR_CPUS)
  710. return -ENODEV;
  711. return cpu;
  712. }
  713. #ifdef CONFIG_HOTPLUG_CPU
  714. static struct task_struct * __devinitdata cpu_idle_tasks[NR_CPUS];
  715. static inline struct task_struct * alloc_idle_task(int cpu)
  716. {
  717. struct task_struct *idle;
  718. if ((idle = cpu_idle_tasks[cpu]) != NULL) {
  719. /* initialize thread_struct. we really want to avoid destroy
  720. * idle tread
  721. */
  722. idle->thread.esp = (unsigned long)(((struct pt_regs *)
  723. (THREAD_SIZE + (unsigned long) idle->thread_info)) - 1);
  724. init_idle(idle, cpu);
  725. return idle;
  726. }
  727. idle = fork_idle(cpu);
  728. if (!IS_ERR(idle))
  729. cpu_idle_tasks[cpu] = idle;
  730. return idle;
  731. }
  732. #else
  733. #define alloc_idle_task(cpu) fork_idle(cpu)
  734. #endif
  735. static int __devinit do_boot_cpu(int apicid, int cpu)
  736. /*
  737. * NOTE - on most systems this is a PHYSICAL apic ID, but on multiquad
  738. * (ie clustered apic addressing mode), this is a LOGICAL apic ID.
  739. * Returns zero if CPU booted OK, else error code from wakeup_secondary_cpu.
  740. */
  741. {
  742. struct task_struct *idle;
  743. unsigned long boot_error;
  744. int timeout;
  745. unsigned long start_eip;
  746. unsigned short nmi_high = 0, nmi_low = 0;
  747. ++cpucount;
  748. /*
  749. * We can't use kernel_thread since we must avoid to
  750. * reschedule the child.
  751. */
  752. idle = alloc_idle_task(cpu);
  753. if (IS_ERR(idle))
  754. panic("failed fork for CPU %d", cpu);
  755. idle->thread.eip = (unsigned long) start_secondary;
  756. /* start_eip had better be page-aligned! */
  757. start_eip = setup_trampoline();
  758. /* So we see what's up */
  759. printk("Booting processor %d/%d eip %lx\n", cpu, apicid, start_eip);
  760. /* Stack for startup_32 can be just as for start_secondary onwards */
  761. stack_start.esp = (void *) idle->thread.esp;
  762. irq_ctx_init(cpu);
  763. /*
  764. * This grunge runs the startup process for
  765. * the targeted processor.
  766. */
  767. atomic_set(&init_deasserted, 0);
  768. Dprintk("Setting warm reset code and vector.\n");
  769. store_NMI_vector(&nmi_high, &nmi_low);
  770. smpboot_setup_warm_reset_vector(start_eip);
  771. /*
  772. * Starting actual IPI sequence...
  773. */
  774. boot_error = wakeup_secondary_cpu(apicid, start_eip);
  775. if (!boot_error) {
  776. /*
  777. * allow APs to start initializing.
  778. */
  779. Dprintk("Before Callout %d.\n", cpu);
  780. cpu_set(cpu, cpu_callout_map);
  781. Dprintk("After Callout %d.\n", cpu);
  782. /*
  783. * Wait 5s total for a response
  784. */
  785. for (timeout = 0; timeout < 50000; timeout++) {
  786. if (cpu_isset(cpu, cpu_callin_map))
  787. break; /* It has booted */
  788. udelay(100);
  789. }
  790. if (cpu_isset(cpu, cpu_callin_map)) {
  791. /* number CPUs logically, starting from 1 (BSP is 0) */
  792. Dprintk("OK.\n");
  793. printk("CPU%d: ", cpu);
  794. print_cpu_info(&cpu_data[cpu]);
  795. Dprintk("CPU has booted.\n");
  796. } else {
  797. boot_error= 1;
  798. if (*((volatile unsigned char *)trampoline_base)
  799. == 0xA5)
  800. /* trampoline started but...? */
  801. printk("Stuck ??\n");
  802. else
  803. /* trampoline code not run */
  804. printk("Not responding.\n");
  805. inquire_remote_apic(apicid);
  806. }
  807. }
  808. if (boot_error) {
  809. /* Try to put things back the way they were before ... */
  810. unmap_cpu_to_logical_apicid(cpu);
  811. cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
  812. cpu_clear(cpu, cpu_initialized); /* was set by cpu_init() */
  813. cpucount--;
  814. } else {
  815. x86_cpu_to_apicid[cpu] = apicid;
  816. cpu_set(cpu, cpu_present_map);
  817. }
  818. /* mark "stuck" area as not stuck */
  819. *((volatile unsigned long *)trampoline_base) = 0;
  820. return boot_error;
  821. }
  822. #ifdef CONFIG_HOTPLUG_CPU
  823. void cpu_exit_clear(void)
  824. {
  825. int cpu = raw_smp_processor_id();
  826. idle_task_exit();
  827. cpucount --;
  828. cpu_uninit();
  829. irq_ctx_exit(cpu);
  830. cpu_clear(cpu, cpu_callout_map);
  831. cpu_clear(cpu, cpu_callin_map);
  832. cpu_clear(cpu, cpu_present_map);
  833. cpu_clear(cpu, smp_commenced_mask);
  834. unmap_cpu_to_logical_apicid(cpu);
  835. }
  836. struct warm_boot_cpu_info {
  837. struct completion *complete;
  838. int apicid;
  839. int cpu;
  840. };
  841. static void __devinit do_warm_boot_cpu(void *p)
  842. {
  843. struct warm_boot_cpu_info *info = p;
  844. do_boot_cpu(info->apicid, info->cpu);
  845. complete(info->complete);
  846. }
  847. int __devinit smp_prepare_cpu(int cpu)
  848. {
  849. DECLARE_COMPLETION(done);
  850. struct warm_boot_cpu_info info;
  851. struct work_struct task;
  852. int apicid, ret;
  853. lock_cpu_hotplug();
  854. apicid = x86_cpu_to_apicid[cpu];
  855. if (apicid == BAD_APICID) {
  856. ret = -ENODEV;
  857. goto exit;
  858. }
  859. info.complete = &done;
  860. info.apicid = apicid;
  861. info.cpu = cpu;
  862. INIT_WORK(&task, do_warm_boot_cpu, &info);
  863. tsc_sync_disabled = 1;
  864. /* init low mem mapping */
  865. clone_pgd_range(swapper_pg_dir, swapper_pg_dir + USER_PGD_PTRS,
  866. KERNEL_PGD_PTRS);
  867. flush_tlb_all();
  868. schedule_work(&task);
  869. wait_for_completion(&done);
  870. tsc_sync_disabled = 0;
  871. zap_low_mappings();
  872. ret = 0;
  873. exit:
  874. unlock_cpu_hotplug();
  875. return ret;
  876. }
  877. #endif
  878. static void smp_tune_scheduling (void)
  879. {
  880. unsigned long cachesize; /* kB */
  881. unsigned long bandwidth = 350; /* MB/s */
  882. /*
  883. * Rough estimation for SMP scheduling, this is the number of
  884. * cycles it takes for a fully memory-limited process to flush
  885. * the SMP-local cache.
  886. *
  887. * (For a P5 this pretty much means we will choose another idle
  888. * CPU almost always at wakeup time (this is due to the small
  889. * L1 cache), on PIIs it's around 50-100 usecs, depending on
  890. * the cache size)
  891. */
  892. if (!cpu_khz) {
  893. /*
  894. * this basically disables processor-affinity
  895. * scheduling on SMP without a TSC.
  896. */
  897. return;
  898. } else {
  899. cachesize = boot_cpu_data.x86_cache_size;
  900. if (cachesize == -1) {
  901. cachesize = 16; /* Pentiums, 2x8kB cache */
  902. bandwidth = 100;
  903. }
  904. }
  905. }
  906. /*
  907. * Cycle through the processors sending APIC IPIs to boot each.
  908. */
  909. static int boot_cpu_logical_apicid;
  910. /* Where the IO area was mapped on multiquad, always 0 otherwise */
  911. void *xquad_portio;
  912. #ifdef CONFIG_X86_NUMAQ
  913. EXPORT_SYMBOL(xquad_portio);
  914. #endif
  915. static void __init smp_boot_cpus(unsigned int max_cpus)
  916. {
  917. int apicid, cpu, bit, kicked;
  918. unsigned long bogosum = 0;
  919. /*
  920. * Setup boot CPU information
  921. */
  922. smp_store_cpu_info(0); /* Final full version of the data */
  923. printk("CPU%d: ", 0);
  924. print_cpu_info(&cpu_data[0]);
  925. boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
  926. boot_cpu_logical_apicid = logical_smp_processor_id();
  927. x86_cpu_to_apicid[0] = boot_cpu_physical_apicid;
  928. current_thread_info()->cpu = 0;
  929. smp_tune_scheduling();
  930. cpus_clear(cpu_sibling_map[0]);
  931. cpu_set(0, cpu_sibling_map[0]);
  932. cpus_clear(cpu_core_map[0]);
  933. cpu_set(0, cpu_core_map[0]);
  934. /*
  935. * If we couldn't find an SMP configuration at boot time,
  936. * get out of here now!
  937. */
  938. if (!smp_found_config && !acpi_lapic) {
  939. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  940. smpboot_clear_io_apic_irqs();
  941. phys_cpu_present_map = physid_mask_of_physid(0);
  942. if (APIC_init_uniprocessor())
  943. printk(KERN_NOTICE "Local APIC not detected."
  944. " Using dummy APIC emulation.\n");
  945. map_cpu_to_logical_apicid();
  946. cpu_set(0, cpu_sibling_map[0]);
  947. cpu_set(0, cpu_core_map[0]);
  948. return;
  949. }
  950. /*
  951. * Should not be necessary because the MP table should list the boot
  952. * CPU too, but we do it for the sake of robustness anyway.
  953. * Makes no sense to do this check in clustered apic mode, so skip it
  954. */
  955. if (!check_phys_apicid_present(boot_cpu_physical_apicid)) {
  956. printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
  957. boot_cpu_physical_apicid);
  958. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  959. }
  960. /*
  961. * If we couldn't find a local APIC, then get out of here now!
  962. */
  963. if (APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid]) && !cpu_has_apic) {
  964. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  965. boot_cpu_physical_apicid);
  966. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
  967. smpboot_clear_io_apic_irqs();
  968. phys_cpu_present_map = physid_mask_of_physid(0);
  969. cpu_set(0, cpu_sibling_map[0]);
  970. cpu_set(0, cpu_core_map[0]);
  971. return;
  972. }
  973. verify_local_APIC();
  974. /*
  975. * If SMP should be disabled, then really disable it!
  976. */
  977. if (!max_cpus) {
  978. smp_found_config = 0;
  979. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
  980. smpboot_clear_io_apic_irqs();
  981. phys_cpu_present_map = physid_mask_of_physid(0);
  982. cpu_set(0, cpu_sibling_map[0]);
  983. cpu_set(0, cpu_core_map[0]);
  984. return;
  985. }
  986. connect_bsp_APIC();
  987. setup_local_APIC();
  988. map_cpu_to_logical_apicid();
  989. setup_portio_remap();
  990. /*
  991. * Scan the CPU present map and fire up the other CPUs via do_boot_cpu
  992. *
  993. * In clustered apic mode, phys_cpu_present_map is a constructed thus:
  994. * bits 0-3 are quad0, 4-7 are quad1, etc. A perverse twist on the
  995. * clustered apic ID.
  996. */
  997. Dprintk("CPU present map: %lx\n", physids_coerce(phys_cpu_present_map));
  998. kicked = 1;
  999. for (bit = 0; kicked < NR_CPUS && bit < MAX_APICS; bit++) {
  1000. apicid = cpu_present_to_apicid(bit);
  1001. /*
  1002. * Don't even attempt to start the boot CPU!
  1003. */
  1004. if ((apicid == boot_cpu_apicid) || (apicid == BAD_APICID))
  1005. continue;
  1006. if (!check_apicid_present(bit))
  1007. continue;
  1008. if (max_cpus <= cpucount+1)
  1009. continue;
  1010. if (((cpu = alloc_cpu_id()) <= 0) || do_boot_cpu(apicid, cpu))
  1011. printk("CPU #%d not responding - cannot use it.\n",
  1012. apicid);
  1013. else
  1014. ++kicked;
  1015. }
  1016. /*
  1017. * Cleanup possible dangling ends...
  1018. */
  1019. smpboot_restore_warm_reset_vector();
  1020. /*
  1021. * Allow the user to impress friends.
  1022. */
  1023. Dprintk("Before bogomips.\n");
  1024. for (cpu = 0; cpu < NR_CPUS; cpu++)
  1025. if (cpu_isset(cpu, cpu_callout_map))
  1026. bogosum += cpu_data[cpu].loops_per_jiffy;
  1027. printk(KERN_INFO
  1028. "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
  1029. cpucount+1,
  1030. bogosum/(500000/HZ),
  1031. (bogosum/(5000/HZ))%100);
  1032. Dprintk("Before bogocount - setting activated=1.\n");
  1033. if (smp_b_stepping)
  1034. printk(KERN_WARNING "WARNING: SMP operation may be unreliable with B stepping processors.\n");
  1035. /*
  1036. * Don't taint if we are running SMP kernel on a single non-MP
  1037. * approved Athlon
  1038. */
  1039. if (tainted & TAINT_UNSAFE_SMP) {
  1040. if (cpucount)
  1041. printk (KERN_INFO "WARNING: This combination of AMD processors is not suitable for SMP.\n");
  1042. else
  1043. tainted &= ~TAINT_UNSAFE_SMP;
  1044. }
  1045. Dprintk("Boot done.\n");
  1046. /*
  1047. * construct cpu_sibling_map[], so that we can tell sibling CPUs
  1048. * efficiently.
  1049. */
  1050. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  1051. cpus_clear(cpu_sibling_map[cpu]);
  1052. cpus_clear(cpu_core_map[cpu]);
  1053. }
  1054. cpu_set(0, cpu_sibling_map[0]);
  1055. cpu_set(0, cpu_core_map[0]);
  1056. smpboot_setup_io_apic();
  1057. setup_boot_APIC_clock();
  1058. /*
  1059. * Synchronize the TSC with the AP
  1060. */
  1061. if (cpu_has_tsc && cpucount && cpu_khz)
  1062. synchronize_tsc_bp();
  1063. }
  1064. /* These are wrappers to interface to the new boot process. Someone
  1065. who understands all this stuff should rewrite it properly. --RR 15/Jul/02 */
  1066. void __init smp_prepare_cpus(unsigned int max_cpus)
  1067. {
  1068. smp_commenced_mask = cpumask_of_cpu(0);
  1069. cpu_callin_map = cpumask_of_cpu(0);
  1070. mb();
  1071. smp_boot_cpus(max_cpus);
  1072. }
  1073. void __devinit smp_prepare_boot_cpu(void)
  1074. {
  1075. cpu_set(smp_processor_id(), cpu_online_map);
  1076. cpu_set(smp_processor_id(), cpu_callout_map);
  1077. cpu_set(smp_processor_id(), cpu_present_map);
  1078. cpu_set(smp_processor_id(), cpu_possible_map);
  1079. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  1080. }
  1081. #ifdef CONFIG_HOTPLUG_CPU
  1082. static void
  1083. remove_siblinginfo(int cpu)
  1084. {
  1085. int sibling;
  1086. for_each_cpu_mask(sibling, cpu_sibling_map[cpu])
  1087. cpu_clear(cpu, cpu_sibling_map[sibling]);
  1088. for_each_cpu_mask(sibling, cpu_core_map[cpu])
  1089. cpu_clear(cpu, cpu_core_map[sibling]);
  1090. cpus_clear(cpu_sibling_map[cpu]);
  1091. cpus_clear(cpu_core_map[cpu]);
  1092. phys_proc_id[cpu] = BAD_APICID;
  1093. cpu_core_id[cpu] = BAD_APICID;
  1094. }
  1095. int __cpu_disable(void)
  1096. {
  1097. cpumask_t map = cpu_online_map;
  1098. int cpu = smp_processor_id();
  1099. /*
  1100. * Perhaps use cpufreq to drop frequency, but that could go
  1101. * into generic code.
  1102. *
  1103. * We won't take down the boot processor on i386 due to some
  1104. * interrupts only being able to be serviced by the BSP.
  1105. * Especially so if we're not using an IOAPIC -zwane
  1106. */
  1107. if (cpu == 0)
  1108. return -EBUSY;
  1109. /* We enable the timer again on the exit path of the death loop */
  1110. disable_APIC_timer();
  1111. /* Allow any queued timer interrupts to get serviced */
  1112. local_irq_enable();
  1113. mdelay(1);
  1114. local_irq_disable();
  1115. remove_siblinginfo(cpu);
  1116. cpu_clear(cpu, map);
  1117. fixup_irqs(map);
  1118. /* It's now safe to remove this processor from the online map */
  1119. cpu_clear(cpu, cpu_online_map);
  1120. return 0;
  1121. }
  1122. void __cpu_die(unsigned int cpu)
  1123. {
  1124. /* We don't do anything here: idle task is faking death itself. */
  1125. unsigned int i;
  1126. for (i = 0; i < 10; i++) {
  1127. /* They ack this in play_dead by setting CPU_DEAD */
  1128. if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
  1129. printk ("CPU %d is now offline\n", cpu);
  1130. return;
  1131. }
  1132. msleep(100);
  1133. }
  1134. printk(KERN_ERR "CPU %u didn't die...\n", cpu);
  1135. }
  1136. #else /* ... !CONFIG_HOTPLUG_CPU */
  1137. int __cpu_disable(void)
  1138. {
  1139. return -ENOSYS;
  1140. }
  1141. void __cpu_die(unsigned int cpu)
  1142. {
  1143. /* We said "no" in __cpu_disable */
  1144. BUG();
  1145. }
  1146. #endif /* CONFIG_HOTPLUG_CPU */
  1147. int __devinit __cpu_up(unsigned int cpu)
  1148. {
  1149. /* In case one didn't come up */
  1150. if (!cpu_isset(cpu, cpu_callin_map)) {
  1151. printk(KERN_DEBUG "skipping cpu%d, didn't come online\n", cpu);
  1152. local_irq_enable();
  1153. return -EIO;
  1154. }
  1155. local_irq_enable();
  1156. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  1157. /* Unleash the CPU! */
  1158. cpu_set(cpu, smp_commenced_mask);
  1159. while (!cpu_isset(cpu, cpu_online_map))
  1160. mb();
  1161. return 0;
  1162. }
  1163. void __init smp_cpus_done(unsigned int max_cpus)
  1164. {
  1165. #ifdef CONFIG_X86_IO_APIC
  1166. setup_ioapic_dest();
  1167. #endif
  1168. zap_low_mappings();
  1169. #ifndef CONFIG_HOTPLUG_CPU
  1170. /*
  1171. * Disable executability of the SMP trampoline:
  1172. */
  1173. set_kernel_exec((unsigned long)trampoline_base, trampoline_exec);
  1174. #endif
  1175. }
  1176. void __init smp_intr_init(void)
  1177. {
  1178. /*
  1179. * IRQ0 must be given a fixed assignment and initialized,
  1180. * because it's used before the IO-APIC is set up.
  1181. */
  1182. set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
  1183. /*
  1184. * The reschedule interrupt is a CPU-to-CPU reschedule-helper
  1185. * IPI, driven by wakeup.
  1186. */
  1187. set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
  1188. /* IPI for invalidation */
  1189. set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
  1190. /* IPI for generic function call */
  1191. set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
  1192. }