smpboot.c 31 KB

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