smpboot.c 37 KB

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