smpboot.c 37 KB

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