smpboot.c 20 KB

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