smpboot.c 29 KB

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