smpboot.c 28 KB

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