smpboot.c 22 KB

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