smpboot.c 36 KB

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