smpboot.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /*
  2. * SMP boot-related support
  3. *
  4. * Copyright (C) 1998-2003, 2005 Hewlett-Packard Co
  5. * David Mosberger-Tang <davidm@hpl.hp.com>
  6. * Copyright (C) 2001, 2004-2005 Intel Corp
  7. * Rohit Seth <rohit.seth@intel.com>
  8. * Suresh Siddha <suresh.b.siddha@intel.com>
  9. * Gordon Jin <gordon.jin@intel.com>
  10. * Ashok Raj <ashok.raj@intel.com>
  11. *
  12. * 01/05/16 Rohit Seth <rohit.seth@intel.com> Moved SMP booting functions from smp.c to here.
  13. * 01/04/27 David Mosberger <davidm@hpl.hp.com> Added ITC synching code.
  14. * 02/07/31 David Mosberger <davidm@hpl.hp.com> Switch over to hotplug-CPU boot-sequence.
  15. * smp_boot_cpus()/smp_commence() is replaced by
  16. * smp_prepare_cpus()/__cpu_up()/smp_cpus_done().
  17. * 04/06/21 Ashok Raj <ashok.raj@intel.com> Added CPU Hotplug Support
  18. * 04/12/26 Jin Gordon <gordon.jin@intel.com>
  19. * 04/12/26 Rohit Seth <rohit.seth@intel.com>
  20. * Add multi-threading and multi-core detection
  21. * 05/01/30 Suresh Siddha <suresh.b.siddha@intel.com>
  22. * Setup cpu_sibling_map and cpu_core_map
  23. */
  24. #include <linux/module.h>
  25. #include <linux/acpi.h>
  26. #include <linux/bootmem.h>
  27. #include <linux/cpu.h>
  28. #include <linux/delay.h>
  29. #include <linux/init.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/irq.h>
  32. #include <linux/kernel.h>
  33. #include <linux/kernel_stat.h>
  34. #include <linux/mm.h>
  35. #include <linux/notifier.h>
  36. #include <linux/smp.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/efi.h>
  39. #include <linux/percpu.h>
  40. #include <linux/bitops.h>
  41. #include <asm/atomic.h>
  42. #include <asm/cache.h>
  43. #include <asm/current.h>
  44. #include <asm/delay.h>
  45. #include <asm/ia32.h>
  46. #include <asm/io.h>
  47. #include <asm/irq.h>
  48. #include <asm/machvec.h>
  49. #include <asm/mca.h>
  50. #include <asm/page.h>
  51. #include <asm/pgalloc.h>
  52. #include <asm/pgtable.h>
  53. #include <asm/processor.h>
  54. #include <asm/ptrace.h>
  55. #include <asm/sal.h>
  56. #include <asm/system.h>
  57. #include <asm/tlbflush.h>
  58. #include <asm/unistd.h>
  59. #define SMP_DEBUG 0
  60. #if SMP_DEBUG
  61. #define Dprintk(x...) printk(x)
  62. #else
  63. #define Dprintk(x...)
  64. #endif
  65. #ifdef CONFIG_HOTPLUG_CPU
  66. #ifdef CONFIG_PERMIT_BSP_REMOVE
  67. #define bsp_remove_ok 1
  68. #else
  69. #define bsp_remove_ok 0
  70. #endif
  71. /*
  72. * Store all idle threads, this can be reused instead of creating
  73. * a new thread. Also avoids complicated thread destroy functionality
  74. * for idle threads.
  75. */
  76. struct task_struct *idle_thread_array[NR_CPUS];
  77. /*
  78. * Global array allocated for NR_CPUS at boot time
  79. */
  80. struct sal_to_os_boot sal_boot_rendez_state[NR_CPUS];
  81. /*
  82. * start_ap in head.S uses this to store current booting cpu
  83. * info.
  84. */
  85. struct sal_to_os_boot *sal_state_for_booting_cpu = &sal_boot_rendez_state[0];
  86. #define set_brendez_area(x) (sal_state_for_booting_cpu = &sal_boot_rendez_state[(x)]);
  87. #define get_idle_for_cpu(x) (idle_thread_array[(x)])
  88. #define set_idle_for_cpu(x,p) (idle_thread_array[(x)] = (p))
  89. #else
  90. #define get_idle_for_cpu(x) (NULL)
  91. #define set_idle_for_cpu(x,p)
  92. #define set_brendez_area(x)
  93. #endif
  94. /*
  95. * ITC synchronization related stuff:
  96. */
  97. #define MASTER (0)
  98. #define SLAVE (SMP_CACHE_BYTES/8)
  99. #define NUM_ROUNDS 64 /* magic value */
  100. #define NUM_ITERS 5 /* likewise */
  101. static DEFINE_SPINLOCK(itc_sync_lock);
  102. static volatile unsigned long go[SLAVE + 1];
  103. #define DEBUG_ITC_SYNC 0
  104. extern void __devinit calibrate_delay (void);
  105. extern void start_ap (void);
  106. extern unsigned long ia64_iobase;
  107. struct task_struct *task_for_booting_cpu;
  108. /*
  109. * State for each CPU
  110. */
  111. DEFINE_PER_CPU(int, cpu_state);
  112. /* Bitmasks of currently online, and possible CPUs */
  113. cpumask_t cpu_online_map;
  114. EXPORT_SYMBOL(cpu_online_map);
  115. cpumask_t cpu_possible_map = CPU_MASK_NONE;
  116. EXPORT_SYMBOL(cpu_possible_map);
  117. cpumask_t cpu_core_map[NR_CPUS] __cacheline_aligned;
  118. cpumask_t cpu_sibling_map[NR_CPUS] __cacheline_aligned;
  119. int smp_num_siblings = 1;
  120. int smp_num_cpucores = 1;
  121. /* which logical CPU number maps to which CPU (physical APIC ID) */
  122. volatile int ia64_cpu_to_sapicid[NR_CPUS];
  123. EXPORT_SYMBOL(ia64_cpu_to_sapicid);
  124. static volatile cpumask_t cpu_callin_map;
  125. struct smp_boot_data smp_boot_data __initdata;
  126. unsigned long ap_wakeup_vector = -1; /* External Int use to wakeup APs */
  127. char __initdata no_int_routing;
  128. unsigned char smp_int_redirect; /* are INT and IPI redirectable by the chipset? */
  129. #ifdef CONFIG_FORCE_CPEI_RETARGET
  130. #define CPEI_OVERRIDE_DEFAULT (1)
  131. #else
  132. #define CPEI_OVERRIDE_DEFAULT (0)
  133. #endif
  134. unsigned int force_cpei_retarget = CPEI_OVERRIDE_DEFAULT;
  135. static int __init
  136. cmdl_force_cpei(char *str)
  137. {
  138. int value=0;
  139. get_option (&str, &value);
  140. force_cpei_retarget = value;
  141. return 1;
  142. }
  143. __setup("force_cpei=", cmdl_force_cpei);
  144. static int __init
  145. nointroute (char *str)
  146. {
  147. no_int_routing = 1;
  148. printk ("no_int_routing on\n");
  149. return 1;
  150. }
  151. __setup("nointroute", nointroute);
  152. static void fix_b0_for_bsp(void)
  153. {
  154. #ifdef CONFIG_HOTPLUG_CPU
  155. int cpuid;
  156. static int fix_bsp_b0 = 1;
  157. cpuid = smp_processor_id();
  158. /*
  159. * Cache the b0 value on the first AP that comes up
  160. */
  161. if (!(fix_bsp_b0 && cpuid))
  162. return;
  163. sal_boot_rendez_state[0].br[0] = sal_boot_rendez_state[cpuid].br[0];
  164. printk ("Fixed BSP b0 value from CPU %d\n", cpuid);
  165. fix_bsp_b0 = 0;
  166. #endif
  167. }
  168. void
  169. sync_master (void *arg)
  170. {
  171. unsigned long flags, i;
  172. go[MASTER] = 0;
  173. local_irq_save(flags);
  174. {
  175. for (i = 0; i < NUM_ROUNDS*NUM_ITERS; ++i) {
  176. while (!go[MASTER])
  177. cpu_relax();
  178. go[MASTER] = 0;
  179. go[SLAVE] = ia64_get_itc();
  180. }
  181. }
  182. local_irq_restore(flags);
  183. }
  184. /*
  185. * Return the number of cycles by which our itc differs from the itc on the master
  186. * (time-keeper) CPU. A positive number indicates our itc is ahead of the master,
  187. * negative that it is behind.
  188. */
  189. static inline long
  190. get_delta (long *rt, long *master)
  191. {
  192. unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;
  193. unsigned long tcenter, t0, t1, tm;
  194. long i;
  195. for (i = 0; i < NUM_ITERS; ++i) {
  196. t0 = ia64_get_itc();
  197. go[MASTER] = 1;
  198. while (!(tm = go[SLAVE]))
  199. cpu_relax();
  200. go[SLAVE] = 0;
  201. t1 = ia64_get_itc();
  202. if (t1 - t0 < best_t1 - best_t0)
  203. best_t0 = t0, best_t1 = t1, best_tm = tm;
  204. }
  205. *rt = best_t1 - best_t0;
  206. *master = best_tm - best_t0;
  207. /* average best_t0 and best_t1 without overflow: */
  208. tcenter = (best_t0/2 + best_t1/2);
  209. if (best_t0 % 2 + best_t1 % 2 == 2)
  210. ++tcenter;
  211. return tcenter - best_tm;
  212. }
  213. /*
  214. * Synchronize ar.itc of the current (slave) CPU with the ar.itc of the MASTER CPU
  215. * (normally the time-keeper CPU). We use a closed loop to eliminate the possibility of
  216. * unaccounted-for errors (such as getting a machine check in the middle of a calibration
  217. * step). The basic idea is for the slave to ask the master what itc value it has and to
  218. * read its own itc before and after the master responds. Each iteration gives us three
  219. * timestamps:
  220. *
  221. * slave master
  222. *
  223. * t0 ---\
  224. * ---\
  225. * --->
  226. * tm
  227. * /---
  228. * /---
  229. * t1 <---
  230. *
  231. *
  232. * The goal is to adjust the slave's ar.itc such that tm falls exactly half-way between t0
  233. * and t1. If we achieve this, the clocks are synchronized provided the interconnect
  234. * between the slave and the master is symmetric. Even if the interconnect were
  235. * asymmetric, we would still know that the synchronization error is smaller than the
  236. * roundtrip latency (t0 - t1).
  237. *
  238. * When the interconnect is quiet and symmetric, this lets us synchronize the itc to
  239. * within one or two cycles. However, we can only *guarantee* that the synchronization is
  240. * accurate to within a round-trip time, which is typically in the range of several
  241. * hundred cycles (e.g., ~500 cycles). In practice, this means that the itc's are usually
  242. * almost perfectly synchronized, but we shouldn't assume that the accuracy is much better
  243. * than half a micro second or so.
  244. */
  245. void
  246. ia64_sync_itc (unsigned int master)
  247. {
  248. long i, delta, adj, adjust_latency = 0, done = 0;
  249. unsigned long flags, rt, master_time_stamp, bound;
  250. #if DEBUG_ITC_SYNC
  251. struct {
  252. long rt; /* roundtrip time */
  253. long master; /* master's timestamp */
  254. long diff; /* difference between midpoint and master's timestamp */
  255. long lat; /* estimate of itc adjustment latency */
  256. } t[NUM_ROUNDS];
  257. #endif
  258. /*
  259. * Make sure local timer ticks are disabled while we sync. If
  260. * they were enabled, we'd have to worry about nasty issues
  261. * like setting the ITC ahead of (or a long time before) the
  262. * next scheduled tick.
  263. */
  264. BUG_ON((ia64_get_itv() & (1 << 16)) == 0);
  265. go[MASTER] = 1;
  266. if (smp_call_function_single(master, sync_master, NULL, 1, 0) < 0) {
  267. printk(KERN_ERR "sync_itc: failed to get attention of CPU %u!\n", master);
  268. return;
  269. }
  270. while (go[MASTER])
  271. cpu_relax(); /* wait for master to be ready */
  272. spin_lock_irqsave(&itc_sync_lock, flags);
  273. {
  274. for (i = 0; i < NUM_ROUNDS; ++i) {
  275. delta = get_delta(&rt, &master_time_stamp);
  276. if (delta == 0) {
  277. done = 1; /* let's lock on to this... */
  278. bound = rt;
  279. }
  280. if (!done) {
  281. if (i > 0) {
  282. adjust_latency += -delta;
  283. adj = -delta + adjust_latency/4;
  284. } else
  285. adj = -delta;
  286. ia64_set_itc(ia64_get_itc() + adj);
  287. }
  288. #if DEBUG_ITC_SYNC
  289. t[i].rt = rt;
  290. t[i].master = master_time_stamp;
  291. t[i].diff = delta;
  292. t[i].lat = adjust_latency/4;
  293. #endif
  294. }
  295. }
  296. spin_unlock_irqrestore(&itc_sync_lock, flags);
  297. #if DEBUG_ITC_SYNC
  298. for (i = 0; i < NUM_ROUNDS; ++i)
  299. printk("rt=%5ld master=%5ld diff=%5ld adjlat=%5ld\n",
  300. t[i].rt, t[i].master, t[i].diff, t[i].lat);
  301. #endif
  302. printk(KERN_INFO "CPU %d: synchronized ITC with CPU %u (last diff %ld cycles, "
  303. "maxerr %lu cycles)\n", smp_processor_id(), master, delta, rt);
  304. }
  305. /*
  306. * Ideally sets up per-cpu profiling hooks. Doesn't do much now...
  307. */
  308. static inline void __devinit
  309. smp_setup_percpu_timer (void)
  310. {
  311. }
  312. static void __devinit
  313. smp_callin (void)
  314. {
  315. int cpuid, phys_id, itc_master;
  316. struct cpuinfo_ia64 *last_cpuinfo, *this_cpuinfo;
  317. extern void ia64_init_itm(void);
  318. extern volatile int time_keeper_id;
  319. #ifdef CONFIG_PERFMON
  320. extern void pfm_init_percpu(void);
  321. #endif
  322. cpuid = smp_processor_id();
  323. phys_id = hard_smp_processor_id();
  324. itc_master = time_keeper_id;
  325. if (cpu_online(cpuid)) {
  326. printk(KERN_ERR "huh, phys CPU#0x%x, CPU#0x%x already present??\n",
  327. phys_id, cpuid);
  328. BUG();
  329. }
  330. fix_b0_for_bsp();
  331. lock_ipi_calllock();
  332. cpu_set(cpuid, cpu_online_map);
  333. unlock_ipi_calllock();
  334. per_cpu(cpu_state, cpuid) = CPU_ONLINE;
  335. smp_setup_percpu_timer();
  336. ia64_mca_cmc_vector_setup(); /* Setup vector on AP */
  337. #ifdef CONFIG_PERFMON
  338. pfm_init_percpu();
  339. #endif
  340. local_irq_enable();
  341. if (!(sal_platform_features & IA64_SAL_PLATFORM_FEATURE_ITC_DRIFT)) {
  342. /*
  343. * Synchronize the ITC with the BP. Need to do this after irqs are
  344. * enabled because ia64_sync_itc() calls smp_call_function_single(), which
  345. * calls spin_unlock_bh(), which calls spin_unlock_bh(), which calls
  346. * local_bh_enable(), which bugs out if irqs are not enabled...
  347. */
  348. Dprintk("Going to syncup ITC with ITC Master.\n");
  349. ia64_sync_itc(itc_master);
  350. }
  351. /*
  352. * Get our bogomips.
  353. */
  354. ia64_init_itm();
  355. /*
  356. * Delay calibration can be skipped if new processor is identical to the
  357. * previous processor.
  358. */
  359. last_cpuinfo = cpu_data(cpuid - 1);
  360. this_cpuinfo = local_cpu_data;
  361. if (last_cpuinfo->itc_freq != this_cpuinfo->itc_freq ||
  362. last_cpuinfo->proc_freq != this_cpuinfo->proc_freq ||
  363. last_cpuinfo->features != this_cpuinfo->features ||
  364. last_cpuinfo->revision != this_cpuinfo->revision ||
  365. last_cpuinfo->family != this_cpuinfo->family ||
  366. last_cpuinfo->archrev != this_cpuinfo->archrev ||
  367. last_cpuinfo->model != this_cpuinfo->model)
  368. calibrate_delay();
  369. local_cpu_data->loops_per_jiffy = loops_per_jiffy;
  370. #ifdef CONFIG_IA32_SUPPORT
  371. ia32_gdt_init();
  372. #endif
  373. /*
  374. * Allow the master to continue.
  375. */
  376. cpu_set(cpuid, cpu_callin_map);
  377. Dprintk("Stack on CPU %d at about %p\n",cpuid, &cpuid);
  378. }
  379. /*
  380. * Activate a secondary processor. head.S calls this.
  381. */
  382. int __devinit
  383. start_secondary (void *unused)
  384. {
  385. /* Early console may use I/O ports */
  386. ia64_set_kr(IA64_KR_IO_BASE, __pa(ia64_iobase));
  387. Dprintk("start_secondary: starting CPU 0x%x\n", hard_smp_processor_id());
  388. efi_map_pal_code();
  389. cpu_init();
  390. preempt_disable();
  391. smp_callin();
  392. cpu_idle();
  393. return 0;
  394. }
  395. struct pt_regs * __devinit idle_regs(struct pt_regs *regs)
  396. {
  397. return NULL;
  398. }
  399. struct create_idle {
  400. struct work_struct work;
  401. struct task_struct *idle;
  402. struct completion done;
  403. int cpu;
  404. };
  405. void
  406. do_fork_idle(struct work_struct *work)
  407. {
  408. struct create_idle *c_idle =
  409. container_of(work, struct create_idle, work);
  410. c_idle->idle = fork_idle(c_idle->cpu);
  411. complete(&c_idle->done);
  412. }
  413. static int __devinit
  414. do_boot_cpu (int sapicid, int cpu)
  415. {
  416. int timeout;
  417. struct create_idle c_idle = {
  418. .work = __WORK_INITIALIZER(c_idle.work, do_fork_idle),
  419. .cpu = cpu,
  420. .done = COMPLETION_INITIALIZER(c_idle.done),
  421. };
  422. c_idle.idle = get_idle_for_cpu(cpu);
  423. if (c_idle.idle) {
  424. init_idle(c_idle.idle, cpu);
  425. goto do_rest;
  426. }
  427. /*
  428. * We can't use kernel_thread since we must avoid to reschedule the child.
  429. */
  430. if (!keventd_up() || current_is_keventd())
  431. c_idle.work.func(&c_idle.work);
  432. else {
  433. schedule_work(&c_idle.work);
  434. wait_for_completion(&c_idle.done);
  435. }
  436. if (IS_ERR(c_idle.idle))
  437. panic("failed fork for CPU %d", cpu);
  438. set_idle_for_cpu(cpu, c_idle.idle);
  439. do_rest:
  440. task_for_booting_cpu = c_idle.idle;
  441. Dprintk("Sending wakeup vector %lu to AP 0x%x/0x%x.\n", ap_wakeup_vector, cpu, sapicid);
  442. set_brendez_area(cpu);
  443. platform_send_ipi(cpu, ap_wakeup_vector, IA64_IPI_DM_INT, 0);
  444. /*
  445. * Wait 10s total for the AP to start
  446. */
  447. Dprintk("Waiting on callin_map ...");
  448. for (timeout = 0; timeout < 100000; timeout++) {
  449. if (cpu_isset(cpu, cpu_callin_map))
  450. break; /* It has booted */
  451. udelay(100);
  452. }
  453. Dprintk("\n");
  454. if (!cpu_isset(cpu, cpu_callin_map)) {
  455. printk(KERN_ERR "Processor 0x%x/0x%x is stuck.\n", cpu, sapicid);
  456. ia64_cpu_to_sapicid[cpu] = -1;
  457. cpu_clear(cpu, cpu_online_map); /* was set in smp_callin() */
  458. return -EINVAL;
  459. }
  460. return 0;
  461. }
  462. static int __init
  463. decay (char *str)
  464. {
  465. int ticks;
  466. get_option (&str, &ticks);
  467. return 1;
  468. }
  469. __setup("decay=", decay);
  470. /*
  471. * Initialize the logical CPU number to SAPICID mapping
  472. */
  473. void __init
  474. smp_build_cpu_map (void)
  475. {
  476. int sapicid, cpu, i;
  477. int boot_cpu_id = hard_smp_processor_id();
  478. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  479. ia64_cpu_to_sapicid[cpu] = -1;
  480. }
  481. ia64_cpu_to_sapicid[0] = boot_cpu_id;
  482. cpus_clear(cpu_present_map);
  483. cpu_set(0, cpu_present_map);
  484. cpu_set(0, cpu_possible_map);
  485. for (cpu = 1, i = 0; i < smp_boot_data.cpu_count; i++) {
  486. sapicid = smp_boot_data.cpu_phys_id[i];
  487. if (sapicid == boot_cpu_id)
  488. continue;
  489. cpu_set(cpu, cpu_present_map);
  490. cpu_set(cpu, cpu_possible_map);
  491. ia64_cpu_to_sapicid[cpu] = sapicid;
  492. cpu++;
  493. }
  494. }
  495. /*
  496. * Cycle through the APs sending Wakeup IPIs to boot each.
  497. */
  498. void __init
  499. smp_prepare_cpus (unsigned int max_cpus)
  500. {
  501. int boot_cpu_id = hard_smp_processor_id();
  502. /*
  503. * Initialize the per-CPU profiling counter/multiplier
  504. */
  505. smp_setup_percpu_timer();
  506. /*
  507. * We have the boot CPU online for sure.
  508. */
  509. cpu_set(0, cpu_online_map);
  510. cpu_set(0, cpu_callin_map);
  511. local_cpu_data->loops_per_jiffy = loops_per_jiffy;
  512. ia64_cpu_to_sapicid[0] = boot_cpu_id;
  513. printk(KERN_INFO "Boot processor id 0x%x/0x%x\n", 0, boot_cpu_id);
  514. current_thread_info()->cpu = 0;
  515. /*
  516. * If SMP should be disabled, then really disable it!
  517. */
  518. if (!max_cpus) {
  519. printk(KERN_INFO "SMP mode deactivated.\n");
  520. cpus_clear(cpu_online_map);
  521. cpus_clear(cpu_present_map);
  522. cpus_clear(cpu_possible_map);
  523. cpu_set(0, cpu_online_map);
  524. cpu_set(0, cpu_present_map);
  525. cpu_set(0, cpu_possible_map);
  526. return;
  527. }
  528. }
  529. void __devinit smp_prepare_boot_cpu(void)
  530. {
  531. cpu_set(smp_processor_id(), cpu_online_map);
  532. cpu_set(smp_processor_id(), cpu_callin_map);
  533. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  534. }
  535. #ifdef CONFIG_HOTPLUG_CPU
  536. static inline void
  537. clear_cpu_sibling_map(int cpu)
  538. {
  539. int i;
  540. for_each_cpu_mask(i, cpu_sibling_map[cpu])
  541. cpu_clear(cpu, cpu_sibling_map[i]);
  542. for_each_cpu_mask(i, cpu_core_map[cpu])
  543. cpu_clear(cpu, cpu_core_map[i]);
  544. cpu_sibling_map[cpu] = cpu_core_map[cpu] = CPU_MASK_NONE;
  545. }
  546. static void
  547. remove_siblinginfo(int cpu)
  548. {
  549. int last = 0;
  550. if (cpu_data(cpu)->threads_per_core == 1 &&
  551. cpu_data(cpu)->cores_per_socket == 1) {
  552. cpu_clear(cpu, cpu_core_map[cpu]);
  553. cpu_clear(cpu, cpu_sibling_map[cpu]);
  554. return;
  555. }
  556. last = (cpus_weight(cpu_core_map[cpu]) == 1 ? 1 : 0);
  557. /* remove it from all sibling map's */
  558. clear_cpu_sibling_map(cpu);
  559. }
  560. extern void fixup_irqs(void);
  561. int migrate_platform_irqs(unsigned int cpu)
  562. {
  563. int new_cpei_cpu;
  564. irq_desc_t *desc = NULL;
  565. cpumask_t mask;
  566. int retval = 0;
  567. /*
  568. * dont permit CPEI target to removed.
  569. */
  570. if (cpe_vector > 0 && is_cpu_cpei_target(cpu)) {
  571. printk ("CPU (%d) is CPEI Target\n", cpu);
  572. if (can_cpei_retarget()) {
  573. /*
  574. * Now re-target the CPEI to a different processor
  575. */
  576. new_cpei_cpu = any_online_cpu(cpu_online_map);
  577. mask = cpumask_of_cpu(new_cpei_cpu);
  578. set_cpei_target_cpu(new_cpei_cpu);
  579. desc = irq_desc + ia64_cpe_irq;
  580. /*
  581. * Switch for now, immediatly, we need to do fake intr
  582. * as other interrupts, but need to study CPEI behaviour with
  583. * polling before making changes.
  584. */
  585. if (desc) {
  586. desc->chip->disable(ia64_cpe_irq);
  587. desc->chip->set_affinity(ia64_cpe_irq, mask);
  588. desc->chip->enable(ia64_cpe_irq);
  589. printk ("Re-targetting CPEI to cpu %d\n", new_cpei_cpu);
  590. }
  591. }
  592. if (!desc) {
  593. printk ("Unable to retarget CPEI, offline cpu [%d] failed\n", cpu);
  594. retval = -EBUSY;
  595. }
  596. }
  597. return retval;
  598. }
  599. /* must be called with cpucontrol mutex held */
  600. int __cpu_disable(void)
  601. {
  602. int cpu = smp_processor_id();
  603. /*
  604. * dont permit boot processor for now
  605. */
  606. if (cpu == 0 && !bsp_remove_ok) {
  607. printk ("Your platform does not support removal of BSP\n");
  608. return (-EBUSY);
  609. }
  610. cpu_clear(cpu, cpu_online_map);
  611. if (migrate_platform_irqs(cpu)) {
  612. cpu_set(cpu, cpu_online_map);
  613. return (-EBUSY);
  614. }
  615. remove_siblinginfo(cpu);
  616. cpu_clear(cpu, cpu_online_map);
  617. fixup_irqs();
  618. local_flush_tlb_all();
  619. cpu_clear(cpu, cpu_callin_map);
  620. return 0;
  621. }
  622. void __cpu_die(unsigned int cpu)
  623. {
  624. unsigned int i;
  625. for (i = 0; i < 100; i++) {
  626. /* They ack this in play_dead by setting CPU_DEAD */
  627. if (per_cpu(cpu_state, cpu) == CPU_DEAD)
  628. {
  629. printk ("CPU %d is now offline\n", cpu);
  630. return;
  631. }
  632. msleep(100);
  633. }
  634. printk(KERN_ERR "CPU %u didn't die...\n", cpu);
  635. }
  636. #else /* !CONFIG_HOTPLUG_CPU */
  637. int __cpu_disable(void)
  638. {
  639. return -ENOSYS;
  640. }
  641. void __cpu_die(unsigned int cpu)
  642. {
  643. /* We said "no" in __cpu_disable */
  644. BUG();
  645. }
  646. #endif /* CONFIG_HOTPLUG_CPU */
  647. void
  648. smp_cpus_done (unsigned int dummy)
  649. {
  650. int cpu;
  651. unsigned long bogosum = 0;
  652. /*
  653. * Allow the user to impress friends.
  654. */
  655. for_each_online_cpu(cpu) {
  656. bogosum += cpu_data(cpu)->loops_per_jiffy;
  657. }
  658. printk(KERN_INFO "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
  659. (int)num_online_cpus(), bogosum/(500000/HZ), (bogosum/(5000/HZ))%100);
  660. }
  661. static inline void __devinit
  662. set_cpu_sibling_map(int cpu)
  663. {
  664. int i;
  665. for_each_online_cpu(i) {
  666. if ((cpu_data(cpu)->socket_id == cpu_data(i)->socket_id)) {
  667. cpu_set(i, cpu_core_map[cpu]);
  668. cpu_set(cpu, cpu_core_map[i]);
  669. if (cpu_data(cpu)->core_id == cpu_data(i)->core_id) {
  670. cpu_set(i, cpu_sibling_map[cpu]);
  671. cpu_set(cpu, cpu_sibling_map[i]);
  672. }
  673. }
  674. }
  675. }
  676. int __devinit
  677. __cpu_up (unsigned int cpu)
  678. {
  679. int ret;
  680. int sapicid;
  681. sapicid = ia64_cpu_to_sapicid[cpu];
  682. if (sapicid == -1)
  683. return -EINVAL;
  684. /*
  685. * Already booted cpu? not valid anymore since we dont
  686. * do idle loop tightspin anymore.
  687. */
  688. if (cpu_isset(cpu, cpu_callin_map))
  689. return -EINVAL;
  690. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  691. /* Processor goes to start_secondary(), sets online flag */
  692. ret = do_boot_cpu(sapicid, cpu);
  693. if (ret < 0)
  694. return ret;
  695. if (cpu_data(cpu)->threads_per_core == 1 &&
  696. cpu_data(cpu)->cores_per_socket == 1) {
  697. cpu_set(cpu, cpu_sibling_map[cpu]);
  698. cpu_set(cpu, cpu_core_map[cpu]);
  699. return 0;
  700. }
  701. set_cpu_sibling_map(cpu);
  702. return 0;
  703. }
  704. /*
  705. * Assume that CPU's have been discovered by some platform-dependent interface. For
  706. * SoftSDV/Lion, that would be ACPI.
  707. *
  708. * Setup of the IPI irq handler is done in irq.c:init_IRQ_SMP().
  709. */
  710. void __init
  711. init_smp_config(void)
  712. {
  713. struct fptr {
  714. unsigned long fp;
  715. unsigned long gp;
  716. } *ap_startup;
  717. long sal_ret;
  718. /* Tell SAL where to drop the AP's. */
  719. ap_startup = (struct fptr *) start_ap;
  720. sal_ret = ia64_sal_set_vectors(SAL_VECTOR_OS_BOOT_RENDEZ,
  721. ia64_tpa(ap_startup->fp), ia64_tpa(ap_startup->gp), 0, 0, 0, 0);
  722. if (sal_ret < 0)
  723. printk(KERN_ERR "SMP: Can't set SAL AP Boot Rendezvous: %s\n",
  724. ia64_sal_strerror(sal_ret));
  725. }
  726. /*
  727. * identify_siblings(cpu) gets called from identify_cpu. This populates the
  728. * information related to logical execution units in per_cpu_data structure.
  729. */
  730. void __devinit
  731. identify_siblings(struct cpuinfo_ia64 *c)
  732. {
  733. s64 status;
  734. u16 pltid;
  735. pal_logical_to_physical_t info;
  736. if (smp_num_cpucores == 1 && smp_num_siblings == 1)
  737. return;
  738. if ((status = ia64_pal_logical_to_phys(-1, &info)) != PAL_STATUS_SUCCESS) {
  739. printk(KERN_ERR "ia64_pal_logical_to_phys failed with %ld\n",
  740. status);
  741. return;
  742. }
  743. if ((status = ia64_sal_physical_id_info(&pltid)) != PAL_STATUS_SUCCESS) {
  744. printk(KERN_ERR "ia64_sal_pltid failed with %ld\n", status);
  745. return;
  746. }
  747. c->socket_id = (pltid << 8) | info.overview_ppid;
  748. c->cores_per_socket = info.overview_cpp;
  749. c->threads_per_core = info.overview_tpc;
  750. c->num_log = info.overview_num_log;
  751. c->core_id = info.log1_cid;
  752. c->thread_id = info.log1_tid;
  753. }
  754. /*
  755. * returns non zero, if multi-threading is enabled
  756. * on at least one physical package. Due to hotplug cpu
  757. * and (maxcpus=), all threads may not necessarily be enabled
  758. * even though the processor supports multi-threading.
  759. */
  760. int is_multithreading_enabled(void)
  761. {
  762. int i, j;
  763. for_each_present_cpu(i) {
  764. for_each_present_cpu(j) {
  765. if (j == i)
  766. continue;
  767. if ((cpu_data(j)->socket_id == cpu_data(i)->socket_id)) {
  768. if (cpu_data(j)->core_id == cpu_data(i)->core_id)
  769. return 1;
  770. }
  771. }
  772. }
  773. return 0;
  774. }
  775. EXPORT_SYMBOL_GPL(is_multithreading_enabled);