smpboot.c 36 KB

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