smpboot.c 37 KB

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