smpboot.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. * Copyright 2001 Andi Kleen, SuSE Labs.
  7. *
  8. * Much of the core SMP work is based on previous work by Thomas Radke, to
  9. * whom a great many thanks are extended.
  10. *
  11. * Thanks to Intel for making available several different Pentium,
  12. * Pentium Pro and Pentium-II/Xeon MP machines.
  13. * Original development of Linux SMP code supported by Caldera.
  14. *
  15. * This code is released under the GNU General Public License version 2
  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 BogoMIP 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. * Andi Kleen : Changed for SMP boot into long mode.
  33. * Rusty Russell : Hacked into shape for new "hotplug" boot process.
  34. * Andi Kleen : Converted to new state machine.
  35. * Various cleanups.
  36. * Probably mostly hotplug CPU ready now.
  37. * Ashok Raj : CPU hotplug support
  38. */
  39. #include <linux/init.h>
  40. #include <linux/mm.h>
  41. #include <linux/kernel_stat.h>
  42. #include <linux/bootmem.h>
  43. #include <linux/thread_info.h>
  44. #include <linux/module.h>
  45. #include <linux/delay.h>
  46. #include <linux/mc146818rtc.h>
  47. #include <linux/smp.h>
  48. #include <linux/kdebug.h>
  49. #include <asm/mtrr.h>
  50. #include <asm/pgalloc.h>
  51. #include <asm/desc.h>
  52. #include <asm/tlbflush.h>
  53. #include <asm/proto.h>
  54. #include <asm/nmi.h>
  55. #include <asm/irq.h>
  56. #include <asm/hw_irq.h>
  57. #include <asm/numa.h>
  58. /* Number of siblings per CPU package */
  59. int smp_num_siblings = 1;
  60. EXPORT_SYMBOL(smp_num_siblings);
  61. /* Last level cache ID of each logical CPU */
  62. u8 cpu_llc_id[NR_CPUS] __cpuinitdata = {[0 ... NR_CPUS-1] = BAD_APICID};
  63. /* Bitmask of currently online CPUs */
  64. cpumask_t cpu_online_map __read_mostly;
  65. EXPORT_SYMBOL(cpu_online_map);
  66. /*
  67. * Private maps to synchronize booting between AP and BP.
  68. * Probably not needed anymore, but it makes for easier debugging. -AK
  69. */
  70. cpumask_t cpu_callin_map;
  71. cpumask_t cpu_callout_map;
  72. EXPORT_SYMBOL(cpu_callout_map);
  73. cpumask_t cpu_possible_map;
  74. EXPORT_SYMBOL(cpu_possible_map);
  75. /* Per CPU bogomips and other parameters */
  76. struct cpuinfo_x86 cpu_data[NR_CPUS] __cacheline_aligned;
  77. EXPORT_SYMBOL(cpu_data);
  78. /* Set when the idlers are all forked */
  79. int smp_threads_ready;
  80. /* representing HT siblings of each logical CPU */
  81. cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
  82. EXPORT_SYMBOL(cpu_sibling_map);
  83. /* representing HT and core siblings of each logical CPU */
  84. cpumask_t cpu_core_map[NR_CPUS] __read_mostly;
  85. EXPORT_SYMBOL(cpu_core_map);
  86. /*
  87. * Trampoline 80x86 program as an array.
  88. */
  89. extern unsigned char trampoline_data[];
  90. extern unsigned char trampoline_end[];
  91. /* State of each CPU */
  92. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  93. /*
  94. * Store all idle threads, this can be reused instead of creating
  95. * a new thread. Also avoids complicated thread destroy functionality
  96. * for idle threads.
  97. */
  98. struct task_struct *idle_thread_array[NR_CPUS] __cpuinitdata ;
  99. #define get_idle_for_cpu(x) (idle_thread_array[(x)])
  100. #define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
  101. /*
  102. * Currently trivial. Write the real->protected mode
  103. * bootstrap into the page concerned. The caller
  104. * has made sure it's suitably aligned.
  105. */
  106. static unsigned long __cpuinit setup_trampoline(void)
  107. {
  108. void *tramp = __va(SMP_TRAMPOLINE_BASE);
  109. memcpy(tramp, trampoline_data, trampoline_end - trampoline_data);
  110. return virt_to_phys(tramp);
  111. }
  112. /*
  113. * The bootstrap kernel entry code has set these up. Save them for
  114. * a given CPU
  115. */
  116. static void __cpuinit smp_store_cpu_info(int id)
  117. {
  118. struct cpuinfo_x86 *c = cpu_data + id;
  119. *c = boot_cpu_data;
  120. identify_cpu(c);
  121. print_cpu_info(c);
  122. }
  123. static atomic_t init_deasserted __cpuinitdata;
  124. /*
  125. * Report back to the Boot Processor.
  126. * Running on AP.
  127. */
  128. void __cpuinit smp_callin(void)
  129. {
  130. int cpuid, phys_id;
  131. unsigned long timeout;
  132. /*
  133. * If waken up by an INIT in an 82489DX configuration
  134. * we may get here before an INIT-deassert IPI reaches
  135. * our local APIC. We have to wait for the IPI or we'll
  136. * lock up on an APIC access.
  137. */
  138. while (!atomic_read(&init_deasserted))
  139. cpu_relax();
  140. /*
  141. * (This works even if the APIC is not enabled.)
  142. */
  143. phys_id = GET_APIC_ID(apic_read(APIC_ID));
  144. cpuid = smp_processor_id();
  145. if (cpu_isset(cpuid, cpu_callin_map)) {
  146. panic("smp_callin: phys CPU#%d, CPU#%d already present??\n",
  147. phys_id, cpuid);
  148. }
  149. Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpuid, phys_id);
  150. /*
  151. * STARTUP IPIs are fragile beasts as they might sometimes
  152. * trigger some glue motherboard logic. Complete APIC bus
  153. * silence for 1 second, this overestimates the time the
  154. * boot CPU is spending to send the up to 2 STARTUP IPIs
  155. * by a factor of two. This should be enough.
  156. */
  157. /*
  158. * Waiting 2s total for startup (udelay is not yet working)
  159. */
  160. timeout = jiffies + 2*HZ;
  161. while (time_before(jiffies, timeout)) {
  162. /*
  163. * Has the boot CPU finished it's STARTUP sequence?
  164. */
  165. if (cpu_isset(cpuid, cpu_callout_map))
  166. break;
  167. cpu_relax();
  168. }
  169. if (!time_before(jiffies, timeout)) {
  170. panic("smp_callin: CPU%d started up but did not get a callout!\n",
  171. cpuid);
  172. }
  173. /*
  174. * the boot CPU has finished the init stage and is spinning
  175. * on callin_map until we finish. We are free to set up this
  176. * CPU, first the APIC. (this is probably redundant on most
  177. * boards)
  178. */
  179. Dprintk("CALLIN, before setup_local_APIC().\n");
  180. setup_local_APIC();
  181. /*
  182. * Get our bogomips.
  183. *
  184. * Need to enable IRQs because it can take longer and then
  185. * the NMI watchdog might kill us.
  186. */
  187. local_irq_enable();
  188. calibrate_delay();
  189. local_irq_disable();
  190. Dprintk("Stack at about %p\n",&cpuid);
  191. disable_APIC_timer();
  192. /*
  193. * Save our processor parameters
  194. */
  195. smp_store_cpu_info(cpuid);
  196. /*
  197. * Allow the master to continue.
  198. */
  199. cpu_set(cpuid, cpu_callin_map);
  200. }
  201. /* maps the cpu to the sched domain representing multi-core */
  202. cpumask_t cpu_coregroup_map(int cpu)
  203. {
  204. struct cpuinfo_x86 *c = cpu_data + cpu;
  205. /*
  206. * For perf, we return last level cache shared map.
  207. * And for power savings, we return cpu_core_map
  208. */
  209. if (sched_mc_power_savings || sched_smt_power_savings)
  210. return cpu_core_map[cpu];
  211. else
  212. return c->llc_shared_map;
  213. }
  214. /* representing cpus for which sibling maps can be computed */
  215. static cpumask_t cpu_sibling_setup_map;
  216. static inline void set_cpu_sibling_map(int cpu)
  217. {
  218. int i;
  219. struct cpuinfo_x86 *c = cpu_data;
  220. cpu_set(cpu, cpu_sibling_setup_map);
  221. if (smp_num_siblings > 1) {
  222. for_each_cpu_mask(i, cpu_sibling_setup_map) {
  223. if (c[cpu].phys_proc_id == c[i].phys_proc_id &&
  224. c[cpu].cpu_core_id == c[i].cpu_core_id) {
  225. cpu_set(i, cpu_sibling_map[cpu]);
  226. cpu_set(cpu, cpu_sibling_map[i]);
  227. cpu_set(i, cpu_core_map[cpu]);
  228. cpu_set(cpu, cpu_core_map[i]);
  229. cpu_set(i, c[cpu].llc_shared_map);
  230. cpu_set(cpu, c[i].llc_shared_map);
  231. }
  232. }
  233. } else {
  234. cpu_set(cpu, cpu_sibling_map[cpu]);
  235. }
  236. cpu_set(cpu, c[cpu].llc_shared_map);
  237. if (current_cpu_data.x86_max_cores == 1) {
  238. cpu_core_map[cpu] = cpu_sibling_map[cpu];
  239. c[cpu].booted_cores = 1;
  240. return;
  241. }
  242. for_each_cpu_mask(i, cpu_sibling_setup_map) {
  243. if (cpu_llc_id[cpu] != BAD_APICID &&
  244. cpu_llc_id[cpu] == cpu_llc_id[i]) {
  245. cpu_set(i, c[cpu].llc_shared_map);
  246. cpu_set(cpu, c[i].llc_shared_map);
  247. }
  248. if (c[cpu].phys_proc_id == c[i].phys_proc_id) {
  249. cpu_set(i, cpu_core_map[cpu]);
  250. cpu_set(cpu, cpu_core_map[i]);
  251. /*
  252. * Does this new cpu bringup a new core?
  253. */
  254. if (cpus_weight(cpu_sibling_map[cpu]) == 1) {
  255. /*
  256. * for each core in package, increment
  257. * the booted_cores for this new cpu
  258. */
  259. if (first_cpu(cpu_sibling_map[i]) == i)
  260. c[cpu].booted_cores++;
  261. /*
  262. * increment the core count for all
  263. * the other cpus in this package
  264. */
  265. if (i != cpu)
  266. c[i].booted_cores++;
  267. } else if (i != cpu && !c[cpu].booted_cores)
  268. c[cpu].booted_cores = c[i].booted_cores;
  269. }
  270. }
  271. }
  272. /*
  273. * Setup code on secondary processor (after comming out of the trampoline)
  274. */
  275. void __cpuinit start_secondary(void)
  276. {
  277. /*
  278. * Dont put anything before smp_callin(), SMP
  279. * booting is too fragile that we want to limit the
  280. * things done here to the most necessary things.
  281. */
  282. cpu_init();
  283. preempt_disable();
  284. smp_callin();
  285. /* otherwise gcc will move up the smp_processor_id before the cpu_init */
  286. barrier();
  287. /*
  288. * Check TSC sync first:
  289. */
  290. check_tsc_sync_target();
  291. Dprintk("cpu %d: setting up apic clock\n", smp_processor_id());
  292. setup_secondary_APIC_clock();
  293. Dprintk("cpu %d: enabling apic timer\n", smp_processor_id());
  294. if (nmi_watchdog == NMI_IO_APIC) {
  295. disable_8259A_irq(0);
  296. enable_NMI_through_LVT0(NULL);
  297. enable_8259A_irq(0);
  298. }
  299. enable_APIC_timer();
  300. /*
  301. * The sibling maps must be set before turing the online map on for
  302. * this cpu
  303. */
  304. set_cpu_sibling_map(smp_processor_id());
  305. /*
  306. * We need to hold call_lock, so there is no inconsistency
  307. * between the time smp_call_function() determines number of
  308. * IPI receipients, and the time when the determination is made
  309. * for which cpus receive the IPI in genapic_flat.c. Holding this
  310. * lock helps us to not include this cpu in a currently in progress
  311. * smp_call_function().
  312. */
  313. lock_ipi_call_lock();
  314. spin_lock(&vector_lock);
  315. /* Setup the per cpu irq handling data structures */
  316. __setup_vector_irq(smp_processor_id());
  317. /*
  318. * Allow the master to continue.
  319. */
  320. cpu_set(smp_processor_id(), cpu_online_map);
  321. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  322. spin_unlock(&vector_lock);
  323. unlock_ipi_call_lock();
  324. cpu_idle();
  325. }
  326. extern volatile unsigned long init_rsp;
  327. extern void (*initial_code)(void);
  328. #ifdef APIC_DEBUG
  329. static void inquire_remote_apic(int apicid)
  330. {
  331. unsigned i, regs[] = { APIC_ID >> 4, APIC_LVR >> 4, APIC_SPIV >> 4 };
  332. char *names[] = { "ID", "VERSION", "SPIV" };
  333. int timeout;
  334. unsigned int status;
  335. printk(KERN_INFO "Inquiring remote APIC #%d...\n", apicid);
  336. for (i = 0; i < sizeof(regs) / sizeof(*regs); i++) {
  337. printk("... APIC #%d %s: ", apicid, names[i]);
  338. /*
  339. * Wait for idle.
  340. */
  341. status = safe_apic_wait_icr_idle();
  342. if (status)
  343. printk("a previous APIC delivery may have failed\n");
  344. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(apicid));
  345. apic_write(APIC_ICR, APIC_DM_REMRD | regs[i]);
  346. timeout = 0;
  347. do {
  348. udelay(100);
  349. status = apic_read(APIC_ICR) & APIC_ICR_RR_MASK;
  350. } while (status == APIC_ICR_RR_INPROG && timeout++ < 1000);
  351. switch (status) {
  352. case APIC_ICR_RR_VALID:
  353. status = apic_read(APIC_RRR);
  354. printk("%08x\n", status);
  355. break;
  356. default:
  357. printk("failed\n");
  358. }
  359. }
  360. }
  361. #endif
  362. /*
  363. * Kick the secondary to wake up.
  364. */
  365. static int __cpuinit wakeup_secondary_via_INIT(int phys_apicid, unsigned int start_rip)
  366. {
  367. unsigned long send_status, accept_status = 0;
  368. int maxlvt, num_starts, j;
  369. Dprintk("Asserting INIT.\n");
  370. /*
  371. * Turn INIT on target chip
  372. */
  373. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  374. /*
  375. * Send IPI
  376. */
  377. apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_INT_ASSERT
  378. | APIC_DM_INIT);
  379. Dprintk("Waiting for send to finish...\n");
  380. send_status = safe_apic_wait_icr_idle();
  381. mdelay(10);
  382. Dprintk("Deasserting INIT.\n");
  383. /* Target chip */
  384. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  385. /* Send IPI */
  386. apic_write(APIC_ICR, APIC_INT_LEVELTRIG | APIC_DM_INIT);
  387. Dprintk("Waiting for send to finish...\n");
  388. send_status = safe_apic_wait_icr_idle();
  389. mb();
  390. atomic_set(&init_deasserted, 1);
  391. num_starts = 2;
  392. /*
  393. * Run STARTUP IPI loop.
  394. */
  395. Dprintk("#startup loops: %d.\n", num_starts);
  396. maxlvt = get_maxlvt();
  397. for (j = 1; j <= num_starts; j++) {
  398. Dprintk("Sending STARTUP #%d.\n",j);
  399. apic_write(APIC_ESR, 0);
  400. apic_read(APIC_ESR);
  401. Dprintk("After apic_write.\n");
  402. /*
  403. * STARTUP IPI
  404. */
  405. /* Target chip */
  406. apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(phys_apicid));
  407. /* Boot on the stack */
  408. /* Kick the second */
  409. apic_write(APIC_ICR, APIC_DM_STARTUP | (start_rip >> 12));
  410. /*
  411. * Give the other CPU some time to accept the IPI.
  412. */
  413. udelay(300);
  414. Dprintk("Startup point 1.\n");
  415. Dprintk("Waiting for send to finish...\n");
  416. send_status = safe_apic_wait_icr_idle();
  417. /*
  418. * Give the other CPU some time to accept the IPI.
  419. */
  420. udelay(200);
  421. /*
  422. * Due to the Pentium erratum 3AP.
  423. */
  424. if (maxlvt > 3) {
  425. apic_write(APIC_ESR, 0);
  426. }
  427. accept_status = (apic_read(APIC_ESR) & 0xEF);
  428. if (send_status || accept_status)
  429. break;
  430. }
  431. Dprintk("After Startup.\n");
  432. if (send_status)
  433. printk(KERN_ERR "APIC never delivered???\n");
  434. if (accept_status)
  435. printk(KERN_ERR "APIC delivery error (%lx).\n", accept_status);
  436. return (send_status | accept_status);
  437. }
  438. struct create_idle {
  439. struct work_struct work;
  440. struct task_struct *idle;
  441. struct completion done;
  442. int cpu;
  443. };
  444. void do_fork_idle(struct work_struct *work)
  445. {
  446. struct create_idle *c_idle =
  447. container_of(work, struct create_idle, work);
  448. c_idle->idle = fork_idle(c_idle->cpu);
  449. complete(&c_idle->done);
  450. }
  451. /*
  452. * Boot one CPU.
  453. */
  454. static int __cpuinit do_boot_cpu(int cpu, int apicid)
  455. {
  456. unsigned long boot_error;
  457. int timeout;
  458. unsigned long start_rip;
  459. struct create_idle c_idle = {
  460. .work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
  461. .cpu = cpu,
  462. .done = COMPLETION_INITIALIZER_ONSTACK(c_idle.done),
  463. };
  464. /* allocate memory for gdts of secondary cpus. Hotplug is considered */
  465. if (!cpu_gdt_descr[cpu].address &&
  466. !(cpu_gdt_descr[cpu].address = get_zeroed_page(GFP_KERNEL))) {
  467. printk(KERN_ERR "Failed to allocate GDT for CPU %d\n", cpu);
  468. return -1;
  469. }
  470. /* Allocate node local memory for AP pdas */
  471. if (cpu_pda(cpu) == &boot_cpu_pda[cpu]) {
  472. struct x8664_pda *newpda, *pda;
  473. int node = cpu_to_node(cpu);
  474. pda = cpu_pda(cpu);
  475. newpda = kmalloc_node(sizeof (struct x8664_pda), GFP_ATOMIC,
  476. node);
  477. if (newpda) {
  478. memcpy(newpda, pda, sizeof (struct x8664_pda));
  479. cpu_pda(cpu) = newpda;
  480. } else
  481. printk(KERN_ERR
  482. "Could not allocate node local PDA for CPU %d on node %d\n",
  483. cpu, node);
  484. }
  485. alternatives_smp_switch(1);
  486. c_idle.idle = get_idle_for_cpu(cpu);
  487. if (c_idle.idle) {
  488. c_idle.idle->thread.rsp = (unsigned long) (((struct pt_regs *)
  489. (THREAD_SIZE + task_stack_page(c_idle.idle))) - 1);
  490. init_idle(c_idle.idle, cpu);
  491. goto do_rest;
  492. }
  493. /*
  494. * During cold boot process, keventd thread is not spun up yet.
  495. * When we do cpu hot-add, we create idle threads on the fly, we should
  496. * not acquire any attributes from the calling context. Hence the clean
  497. * way to create kernel_threads() is to do that from keventd().
  498. * We do the current_is_keventd() due to the fact that ACPI notifier
  499. * was also queuing to keventd() and when the caller is already running
  500. * in context of keventd(), we would end up with locking up the keventd
  501. * thread.
  502. */
  503. if (!keventd_up() || current_is_keventd())
  504. c_idle.work.func(&c_idle.work);
  505. else {
  506. schedule_work(&c_idle.work);
  507. wait_for_completion(&c_idle.done);
  508. }
  509. if (IS_ERR(c_idle.idle)) {
  510. printk("failed fork for CPU %d\n", cpu);
  511. return PTR_ERR(c_idle.idle);
  512. }
  513. set_idle_for_cpu(cpu, c_idle.idle);
  514. do_rest:
  515. cpu_pda(cpu)->pcurrent = c_idle.idle;
  516. start_rip = setup_trampoline();
  517. init_rsp = c_idle.idle->thread.rsp;
  518. per_cpu(init_tss,cpu).rsp0 = init_rsp;
  519. initial_code = start_secondary;
  520. clear_tsk_thread_flag(c_idle.idle, TIF_FORK);
  521. printk(KERN_INFO "Booting processor %d/%d APIC 0x%x\n", cpu,
  522. cpus_weight(cpu_present_map),
  523. apicid);
  524. /*
  525. * This grunge runs the startup process for
  526. * the targeted processor.
  527. */
  528. atomic_set(&init_deasserted, 0);
  529. Dprintk("Setting warm reset code and vector.\n");
  530. CMOS_WRITE(0xa, 0xf);
  531. local_flush_tlb();
  532. Dprintk("1.\n");
  533. *((volatile unsigned short *) phys_to_virt(0x469)) = start_rip >> 4;
  534. Dprintk("2.\n");
  535. *((volatile unsigned short *) phys_to_virt(0x467)) = start_rip & 0xf;
  536. Dprintk("3.\n");
  537. /*
  538. * Be paranoid about clearing APIC errors.
  539. */
  540. apic_write(APIC_ESR, 0);
  541. apic_read(APIC_ESR);
  542. /*
  543. * Status is now clean
  544. */
  545. boot_error = 0;
  546. /*
  547. * Starting actual IPI sequence...
  548. */
  549. boot_error = wakeup_secondary_via_INIT(apicid, start_rip);
  550. if (!boot_error) {
  551. /*
  552. * allow APs to start initializing.
  553. */
  554. Dprintk("Before Callout %d.\n", cpu);
  555. cpu_set(cpu, cpu_callout_map);
  556. Dprintk("After Callout %d.\n", cpu);
  557. /*
  558. * Wait 5s total for a response
  559. */
  560. for (timeout = 0; timeout < 50000; timeout++) {
  561. if (cpu_isset(cpu, cpu_callin_map))
  562. break; /* It has booted */
  563. udelay(100);
  564. }
  565. if (cpu_isset(cpu, cpu_callin_map)) {
  566. /* number CPUs logically, starting from 1 (BSP is 0) */
  567. Dprintk("CPU has booted.\n");
  568. } else {
  569. boot_error = 1;
  570. if (*((volatile unsigned char *)phys_to_virt(SMP_TRAMPOLINE_BASE))
  571. == 0xA5)
  572. /* trampoline started but...? */
  573. printk("Stuck ??\n");
  574. else
  575. /* trampoline code not run */
  576. printk("Not responding.\n");
  577. #ifdef APIC_DEBUG
  578. inquire_remote_apic(apicid);
  579. #endif
  580. }
  581. }
  582. if (boot_error) {
  583. cpu_clear(cpu, cpu_callout_map); /* was set here (do_boot_cpu()) */
  584. clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
  585. clear_node_cpumask(cpu); /* was set by numa_add_cpu */
  586. cpu_clear(cpu, cpu_present_map);
  587. cpu_clear(cpu, cpu_possible_map);
  588. x86_cpu_to_apicid[cpu] = BAD_APICID;
  589. x86_cpu_to_log_apicid[cpu] = BAD_APICID;
  590. return -EIO;
  591. }
  592. return 0;
  593. }
  594. cycles_t cacheflush_time;
  595. unsigned long cache_decay_ticks;
  596. /*
  597. * Cleanup possible dangling ends...
  598. */
  599. static __cpuinit void smp_cleanup_boot(void)
  600. {
  601. /*
  602. * Paranoid: Set warm reset code and vector here back
  603. * to default values.
  604. */
  605. CMOS_WRITE(0, 0xf);
  606. /*
  607. * Reset trampoline flag
  608. */
  609. *((volatile int *) phys_to_virt(0x467)) = 0;
  610. }
  611. /*
  612. * Fall back to non SMP mode after errors.
  613. *
  614. * RED-PEN audit/test this more. I bet there is more state messed up here.
  615. */
  616. static __init void disable_smp(void)
  617. {
  618. cpu_present_map = cpumask_of_cpu(0);
  619. cpu_possible_map = cpumask_of_cpu(0);
  620. if (smp_found_config)
  621. phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
  622. else
  623. phys_cpu_present_map = physid_mask_of_physid(0);
  624. cpu_set(0, cpu_sibling_map[0]);
  625. cpu_set(0, cpu_core_map[0]);
  626. }
  627. #ifdef CONFIG_HOTPLUG_CPU
  628. int additional_cpus __initdata = -1;
  629. /*
  630. * cpu_possible_map should be static, it cannot change as cpu's
  631. * are onlined, or offlined. The reason is per-cpu data-structures
  632. * are allocated by some modules at init time, and dont expect to
  633. * do this dynamically on cpu arrival/departure.
  634. * cpu_present_map on the other hand can change dynamically.
  635. * In case when cpu_hotplug is not compiled, then we resort to current
  636. * behaviour, which is cpu_possible == cpu_present.
  637. * - Ashok Raj
  638. *
  639. * Three ways to find out the number of additional hotplug CPUs:
  640. * - If the BIOS specified disabled CPUs in ACPI/mptables use that.
  641. * - The user can overwrite it with additional_cpus=NUM
  642. * - Otherwise don't reserve additional CPUs.
  643. * We do this because additional CPUs waste a lot of memory.
  644. * -AK
  645. */
  646. __init void prefill_possible_map(void)
  647. {
  648. int i;
  649. int possible;
  650. if (additional_cpus == -1) {
  651. if (disabled_cpus > 0)
  652. additional_cpus = disabled_cpus;
  653. else
  654. additional_cpus = 0;
  655. }
  656. possible = num_processors + additional_cpus;
  657. if (possible > NR_CPUS)
  658. possible = NR_CPUS;
  659. printk(KERN_INFO "SMP: Allowing %d CPUs, %d hotplug CPUs\n",
  660. possible,
  661. max_t(int, possible - num_processors, 0));
  662. for (i = 0; i < possible; i++)
  663. cpu_set(i, cpu_possible_map);
  664. }
  665. #endif
  666. /*
  667. * Various sanity checks.
  668. */
  669. static int __init smp_sanity_check(unsigned max_cpus)
  670. {
  671. if (!physid_isset(hard_smp_processor_id(), phys_cpu_present_map)) {
  672. printk("weird, boot CPU (#%d) not listed by the BIOS.\n",
  673. hard_smp_processor_id());
  674. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  675. }
  676. /*
  677. * If we couldn't find an SMP configuration at boot time,
  678. * get out of here now!
  679. */
  680. if (!smp_found_config) {
  681. printk(KERN_NOTICE "SMP motherboard not detected.\n");
  682. disable_smp();
  683. if (APIC_init_uniprocessor())
  684. printk(KERN_NOTICE "Local APIC not detected."
  685. " Using dummy APIC emulation.\n");
  686. return -1;
  687. }
  688. /*
  689. * Should not be necessary because the MP table should list the boot
  690. * CPU too, but we do it for the sake of robustness anyway.
  691. */
  692. if (!physid_isset(boot_cpu_id, phys_cpu_present_map)) {
  693. printk(KERN_NOTICE "weird, boot CPU (#%d) not listed by the BIOS.\n",
  694. boot_cpu_id);
  695. physid_set(hard_smp_processor_id(), phys_cpu_present_map);
  696. }
  697. /*
  698. * If we couldn't find a local APIC, then get out of here now!
  699. */
  700. if (!cpu_has_apic) {
  701. printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
  702. boot_cpu_id);
  703. printk(KERN_ERR "... forcing use of dummy APIC emulation. (tell your hw vendor)\n");
  704. nr_ioapics = 0;
  705. return -1;
  706. }
  707. /*
  708. * If SMP should be disabled, then really disable it!
  709. */
  710. if (!max_cpus) {
  711. printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
  712. nr_ioapics = 0;
  713. return -1;
  714. }
  715. return 0;
  716. }
  717. /*
  718. * Prepare for SMP bootup. The MP table or ACPI has been read
  719. * earlier. Just do some sanity checking here and enable APIC mode.
  720. */
  721. void __init smp_prepare_cpus(unsigned int max_cpus)
  722. {
  723. nmi_watchdog_default();
  724. current_cpu_data = boot_cpu_data;
  725. current_thread_info()->cpu = 0; /* needed? */
  726. set_cpu_sibling_map(0);
  727. if (smp_sanity_check(max_cpus) < 0) {
  728. printk(KERN_INFO "SMP disabled\n");
  729. disable_smp();
  730. return;
  731. }
  732. /*
  733. * Switch from PIC to APIC mode.
  734. */
  735. setup_local_APIC();
  736. if (GET_APIC_ID(apic_read(APIC_ID)) != boot_cpu_id) {
  737. panic("Boot APIC ID in local APIC unexpected (%d vs %d)",
  738. GET_APIC_ID(apic_read(APIC_ID)), boot_cpu_id);
  739. /* Or can we switch back to PIC here? */
  740. }
  741. /*
  742. * Now start the IO-APICs
  743. */
  744. if (!skip_ioapic_setup && nr_ioapics)
  745. setup_IO_APIC();
  746. else
  747. nr_ioapics = 0;
  748. /*
  749. * Set up local APIC timer on boot CPU.
  750. */
  751. setup_boot_APIC_clock();
  752. }
  753. /*
  754. * Early setup to make printk work.
  755. */
  756. void __init smp_prepare_boot_cpu(void)
  757. {
  758. int me = smp_processor_id();
  759. cpu_set(me, cpu_online_map);
  760. cpu_set(me, cpu_callout_map);
  761. per_cpu(cpu_state, me) = CPU_ONLINE;
  762. }
  763. /*
  764. * Entry point to boot a CPU.
  765. */
  766. int __cpuinit __cpu_up(unsigned int cpu)
  767. {
  768. int apicid = cpu_present_to_apicid(cpu);
  769. unsigned long flags;
  770. int err;
  771. WARN_ON(irqs_disabled());
  772. Dprintk("++++++++++++++++++++=_---CPU UP %u\n", cpu);
  773. if (apicid == BAD_APICID || apicid == boot_cpu_id ||
  774. !physid_isset(apicid, phys_cpu_present_map)) {
  775. printk("__cpu_up: bad cpu %d\n", cpu);
  776. return -EINVAL;
  777. }
  778. /*
  779. * Already booted CPU?
  780. */
  781. if (cpu_isset(cpu, cpu_callin_map)) {
  782. Dprintk("do_boot_cpu %d Already started\n", cpu);
  783. return -ENOSYS;
  784. }
  785. /*
  786. * Save current MTRR state in case it was changed since early boot
  787. * (e.g. by the ACPI SMI) to initialize new CPUs with MTRRs in sync:
  788. */
  789. mtrr_save_state();
  790. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  791. /* Boot it! */
  792. err = do_boot_cpu(cpu, apicid);
  793. if (err < 0) {
  794. Dprintk("do_boot_cpu failed %d\n", err);
  795. return err;
  796. }
  797. /* Unleash the CPU! */
  798. Dprintk("waiting for cpu %d\n", cpu);
  799. /*
  800. * Make sure and check TSC sync:
  801. */
  802. local_irq_save(flags);
  803. check_tsc_sync_source(cpu);
  804. local_irq_restore(flags);
  805. while (!cpu_isset(cpu, cpu_online_map))
  806. cpu_relax();
  807. err = 0;
  808. return err;
  809. }
  810. /*
  811. * Finish the SMP boot.
  812. */
  813. void __init smp_cpus_done(unsigned int max_cpus)
  814. {
  815. smp_cleanup_boot();
  816. setup_ioapic_dest();
  817. check_nmi_watchdog();
  818. }
  819. #ifdef CONFIG_HOTPLUG_CPU
  820. static void remove_siblinginfo(int cpu)
  821. {
  822. int sibling;
  823. struct cpuinfo_x86 *c = cpu_data;
  824. for_each_cpu_mask(sibling, cpu_core_map[cpu]) {
  825. cpu_clear(cpu, cpu_core_map[sibling]);
  826. /*
  827. * last thread sibling in this cpu core going down
  828. */
  829. if (cpus_weight(cpu_sibling_map[cpu]) == 1)
  830. c[sibling].booted_cores--;
  831. }
  832. for_each_cpu_mask(sibling, cpu_sibling_map[cpu])
  833. cpu_clear(cpu, cpu_sibling_map[sibling]);
  834. cpus_clear(cpu_sibling_map[cpu]);
  835. cpus_clear(cpu_core_map[cpu]);
  836. c[cpu].phys_proc_id = 0;
  837. c[cpu].cpu_core_id = 0;
  838. cpu_clear(cpu, cpu_sibling_setup_map);
  839. }
  840. void remove_cpu_from_maps(void)
  841. {
  842. int cpu = smp_processor_id();
  843. cpu_clear(cpu, cpu_callout_map);
  844. cpu_clear(cpu, cpu_callin_map);
  845. clear_bit(cpu, &cpu_initialized); /* was set by cpu_init() */
  846. clear_node_cpumask(cpu);
  847. }
  848. int __cpu_disable(void)
  849. {
  850. int cpu = smp_processor_id();
  851. /*
  852. * Perhaps use cpufreq to drop frequency, but that could go
  853. * into generic code.
  854. *
  855. * We won't take down the boot processor on i386 due to some
  856. * interrupts only being able to be serviced by the BSP.
  857. * Especially so if we're not using an IOAPIC -zwane
  858. */
  859. if (cpu == 0)
  860. return -EBUSY;
  861. if (nmi_watchdog == NMI_LOCAL_APIC)
  862. stop_apic_nmi_watchdog(NULL);
  863. clear_local_APIC();
  864. /*
  865. * HACK:
  866. * Allow any queued timer interrupts to get serviced
  867. * This is only a temporary solution until we cleanup
  868. * fixup_irqs as we do for IA64.
  869. */
  870. local_irq_enable();
  871. mdelay(1);
  872. local_irq_disable();
  873. remove_siblinginfo(cpu);
  874. spin_lock(&vector_lock);
  875. /* It's now safe to remove this processor from the online map */
  876. cpu_clear(cpu, cpu_online_map);
  877. spin_unlock(&vector_lock);
  878. remove_cpu_from_maps();
  879. fixup_irqs(cpu_online_map);
  880. return 0;
  881. }
  882. void __cpu_die(unsigned int cpu)
  883. {
  884. /* We don't do anything here: idle task is faking death itself. */
  885. unsigned int i;
  886. for (i = 0; i < 10; i++) {
  887. /* They ack this in play_dead by setting CPU_DEAD */
  888. if (per_cpu(cpu_state, cpu) == CPU_DEAD) {
  889. printk ("CPU %d is now offline\n", cpu);
  890. if (1 == num_online_cpus())
  891. alternatives_smp_switch(0);
  892. return;
  893. }
  894. msleep(100);
  895. }
  896. printk(KERN_ERR "CPU %u didn't die...\n", cpu);
  897. }
  898. static __init int setup_additional_cpus(char *s)
  899. {
  900. return s && get_option(&s, &additional_cpus) ? 0 : -EINVAL;
  901. }
  902. early_param("additional_cpus", setup_additional_cpus);
  903. #else /* ... !CONFIG_HOTPLUG_CPU */
  904. int __cpu_disable(void)
  905. {
  906. return -ENOSYS;
  907. }
  908. void __cpu_die(unsigned int cpu)
  909. {
  910. /* We said "no" in __cpu_disable */
  911. BUG();
  912. }
  913. #endif /* CONFIG_HOTPLUG_CPU */