smpboot.c 36 KB

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