smpboot.c 31 KB

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