smpboot.c 36 KB

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