smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * SMP support for ppc.
  3. *
  4. * Written by Cort Dougan (cort@cs.nmt.edu) borrowing a great
  5. * deal of code from the sparc and intel versions.
  6. *
  7. * Copyright (C) 1999 Cort Dougan <cort@cs.nmt.edu>
  8. *
  9. * PowerPC-64 Support added by Dave Engebretsen, Peter Bergner, and
  10. * Mike Corrigan {engebret|bergner|mikec}@us.ibm.com
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #undef DEBUG
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/sched.h>
  21. #include <linux/smp.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/delay.h>
  24. #include <linux/init.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/cache.h>
  27. #include <linux/err.h>
  28. #include <linux/sysdev.h>
  29. #include <linux/cpu.h>
  30. #include <linux/notifier.h>
  31. #include <linux/topology.h>
  32. #include <asm/ptrace.h>
  33. #include <asm/atomic.h>
  34. #include <asm/irq.h>
  35. #include <asm/page.h>
  36. #include <asm/pgtable.h>
  37. #include <asm/prom.h>
  38. #include <asm/smp.h>
  39. #include <asm/time.h>
  40. #include <asm/machdep.h>
  41. #include <asm/cputhreads.h>
  42. #include <asm/cputable.h>
  43. #include <asm/system.h>
  44. #include <asm/mpic.h>
  45. #include <asm/vdso_datapage.h>
  46. #ifdef CONFIG_PPC64
  47. #include <asm/paca.h>
  48. #endif
  49. #ifdef DEBUG
  50. #include <asm/udbg.h>
  51. #define DBG(fmt...) udbg_printf(fmt)
  52. #else
  53. #define DBG(fmt...)
  54. #endif
  55. int smp_hw_index[NR_CPUS];
  56. struct thread_info *secondary_ti;
  57. cpumask_t cpu_possible_map = CPU_MASK_NONE;
  58. cpumask_t cpu_online_map = CPU_MASK_NONE;
  59. DEFINE_PER_CPU(cpumask_t, cpu_sibling_map) = CPU_MASK_NONE;
  60. DEFINE_PER_CPU(cpumask_t, cpu_core_map) = CPU_MASK_NONE;
  61. EXPORT_SYMBOL(cpu_online_map);
  62. EXPORT_SYMBOL(cpu_possible_map);
  63. EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
  64. EXPORT_PER_CPU_SYMBOL(cpu_core_map);
  65. /* SMP operations for this machine */
  66. struct smp_ops_t *smp_ops;
  67. static volatile unsigned int cpu_callin_map[NR_CPUS];
  68. int smt_enabled_at_boot = 1;
  69. static void (*crash_ipi_function_ptr)(struct pt_regs *) = NULL;
  70. #ifdef CONFIG_PPC64
  71. void __devinit smp_generic_kick_cpu(int nr)
  72. {
  73. BUG_ON(nr < 0 || nr >= NR_CPUS);
  74. /*
  75. * The processor is currently spinning, waiting for the
  76. * cpu_start field to become non-zero After we set cpu_start,
  77. * the processor will continue on to secondary_start
  78. */
  79. paca[nr].cpu_start = 1;
  80. smp_mb();
  81. }
  82. #endif
  83. void smp_message_recv(int msg)
  84. {
  85. switch(msg) {
  86. case PPC_MSG_CALL_FUNCTION:
  87. generic_smp_call_function_interrupt();
  88. break;
  89. case PPC_MSG_RESCHEDULE:
  90. /* XXX Do we have to do this? */
  91. set_need_resched();
  92. break;
  93. case PPC_MSG_CALL_FUNC_SINGLE:
  94. generic_smp_call_function_single_interrupt();
  95. break;
  96. case PPC_MSG_DEBUGGER_BREAK:
  97. if (crash_ipi_function_ptr) {
  98. crash_ipi_function_ptr(get_irq_regs());
  99. break;
  100. }
  101. #ifdef CONFIG_DEBUGGER
  102. debugger_ipi(get_irq_regs());
  103. break;
  104. #endif /* CONFIG_DEBUGGER */
  105. /* FALLTHROUGH */
  106. default:
  107. printk("SMP %d: smp_message_recv(): unknown msg %d\n",
  108. smp_processor_id(), msg);
  109. break;
  110. }
  111. }
  112. void smp_send_reschedule(int cpu)
  113. {
  114. if (likely(smp_ops))
  115. smp_ops->message_pass(cpu, PPC_MSG_RESCHEDULE);
  116. }
  117. void arch_send_call_function_single_ipi(int cpu)
  118. {
  119. smp_ops->message_pass(cpu, PPC_MSG_CALL_FUNC_SINGLE);
  120. }
  121. void arch_send_call_function_ipi(cpumask_t mask)
  122. {
  123. unsigned int cpu;
  124. for_each_cpu_mask(cpu, mask)
  125. smp_ops->message_pass(cpu, PPC_MSG_CALL_FUNCTION);
  126. }
  127. #ifdef CONFIG_DEBUGGER
  128. void smp_send_debugger_break(int cpu)
  129. {
  130. if (likely(smp_ops))
  131. smp_ops->message_pass(cpu, PPC_MSG_DEBUGGER_BREAK);
  132. }
  133. #endif
  134. #ifdef CONFIG_KEXEC
  135. void crash_send_ipi(void (*crash_ipi_callback)(struct pt_regs *))
  136. {
  137. crash_ipi_function_ptr = crash_ipi_callback;
  138. if (crash_ipi_callback && smp_ops) {
  139. mb();
  140. smp_ops->message_pass(MSG_ALL_BUT_SELF, PPC_MSG_DEBUGGER_BREAK);
  141. }
  142. }
  143. #endif
  144. static void stop_this_cpu(void *dummy)
  145. {
  146. local_irq_disable();
  147. while (1)
  148. ;
  149. }
  150. void smp_send_stop(void)
  151. {
  152. smp_call_function(stop_this_cpu, NULL, 0);
  153. }
  154. struct thread_info *current_set[NR_CPUS];
  155. static void __devinit smp_store_cpu_info(int id)
  156. {
  157. per_cpu(pvr, id) = mfspr(SPRN_PVR);
  158. }
  159. static void __init smp_create_idle(unsigned int cpu)
  160. {
  161. struct task_struct *p;
  162. /* create a process for the processor */
  163. p = fork_idle(cpu);
  164. if (IS_ERR(p))
  165. panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
  166. #ifdef CONFIG_PPC64
  167. paca[cpu].__current = p;
  168. paca[cpu].kstack = (unsigned long) task_thread_info(p)
  169. + THREAD_SIZE - STACK_FRAME_OVERHEAD;
  170. #endif
  171. current_set[cpu] = task_thread_info(p);
  172. task_thread_info(p)->cpu = cpu;
  173. }
  174. void __init smp_prepare_cpus(unsigned int max_cpus)
  175. {
  176. unsigned int cpu;
  177. DBG("smp_prepare_cpus\n");
  178. /*
  179. * setup_cpu may need to be called on the boot cpu. We havent
  180. * spun any cpus up but lets be paranoid.
  181. */
  182. BUG_ON(boot_cpuid != smp_processor_id());
  183. /* Fixup boot cpu */
  184. smp_store_cpu_info(boot_cpuid);
  185. cpu_callin_map[boot_cpuid] = 1;
  186. if (smp_ops)
  187. max_cpus = smp_ops->probe();
  188. else
  189. max_cpus = 1;
  190. smp_space_timers(max_cpus);
  191. for_each_possible_cpu(cpu)
  192. if (cpu != boot_cpuid)
  193. smp_create_idle(cpu);
  194. }
  195. void __devinit smp_prepare_boot_cpu(void)
  196. {
  197. BUG_ON(smp_processor_id() != boot_cpuid);
  198. cpu_set(boot_cpuid, cpu_online_map);
  199. cpu_set(boot_cpuid, per_cpu(cpu_sibling_map, boot_cpuid));
  200. cpu_set(boot_cpuid, per_cpu(cpu_core_map, boot_cpuid));
  201. #ifdef CONFIG_PPC64
  202. paca[boot_cpuid].__current = current;
  203. #endif
  204. current_set[boot_cpuid] = task_thread_info(current);
  205. }
  206. #ifdef CONFIG_HOTPLUG_CPU
  207. /* State of each CPU during hotplug phases */
  208. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  209. int generic_cpu_disable(void)
  210. {
  211. unsigned int cpu = smp_processor_id();
  212. if (cpu == boot_cpuid)
  213. return -EBUSY;
  214. cpu_clear(cpu, cpu_online_map);
  215. #ifdef CONFIG_PPC64
  216. vdso_data->processorCount--;
  217. fixup_irqs(cpu_online_map);
  218. #endif
  219. return 0;
  220. }
  221. int generic_cpu_enable(unsigned int cpu)
  222. {
  223. /* Do the normal bootup if we haven't
  224. * already bootstrapped. */
  225. if (system_state != SYSTEM_RUNNING)
  226. return -ENOSYS;
  227. /* get the target out of it's holding state */
  228. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  229. smp_wmb();
  230. while (!cpu_online(cpu))
  231. cpu_relax();
  232. #ifdef CONFIG_PPC64
  233. fixup_irqs(cpu_online_map);
  234. /* counter the irq disable in fixup_irqs */
  235. local_irq_enable();
  236. #endif
  237. return 0;
  238. }
  239. void generic_cpu_die(unsigned int cpu)
  240. {
  241. int i;
  242. for (i = 0; i < 100; i++) {
  243. smp_rmb();
  244. if (per_cpu(cpu_state, cpu) == CPU_DEAD)
  245. return;
  246. msleep(100);
  247. }
  248. printk(KERN_ERR "CPU%d didn't die...\n", cpu);
  249. }
  250. void generic_mach_cpu_die(void)
  251. {
  252. unsigned int cpu;
  253. local_irq_disable();
  254. cpu = smp_processor_id();
  255. printk(KERN_DEBUG "CPU%d offline\n", cpu);
  256. __get_cpu_var(cpu_state) = CPU_DEAD;
  257. smp_wmb();
  258. while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE)
  259. cpu_relax();
  260. cpu_set(cpu, cpu_online_map);
  261. local_irq_enable();
  262. }
  263. #endif
  264. static int __devinit cpu_enable(unsigned int cpu)
  265. {
  266. if (smp_ops && smp_ops->cpu_enable)
  267. return smp_ops->cpu_enable(cpu);
  268. return -ENOSYS;
  269. }
  270. int __cpuinit __cpu_up(unsigned int cpu)
  271. {
  272. int c;
  273. secondary_ti = current_set[cpu];
  274. if (!cpu_enable(cpu))
  275. return 0;
  276. if (smp_ops == NULL ||
  277. (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu)))
  278. return -EINVAL;
  279. /* Make sure callin-map entry is 0 (can be leftover a CPU
  280. * hotplug
  281. */
  282. cpu_callin_map[cpu] = 0;
  283. /* The information for processor bringup must
  284. * be written out to main store before we release
  285. * the processor.
  286. */
  287. smp_mb();
  288. /* wake up cpus */
  289. DBG("smp: kicking cpu %d\n", cpu);
  290. smp_ops->kick_cpu(cpu);
  291. /*
  292. * wait to see if the cpu made a callin (is actually up).
  293. * use this value that I found through experimentation.
  294. * -- Cort
  295. */
  296. if (system_state < SYSTEM_RUNNING)
  297. for (c = 50000; c && !cpu_callin_map[cpu]; c--)
  298. udelay(100);
  299. #ifdef CONFIG_HOTPLUG_CPU
  300. else
  301. /*
  302. * CPUs can take much longer to come up in the
  303. * hotplug case. Wait five seconds.
  304. */
  305. for (c = 25; c && !cpu_callin_map[cpu]; c--) {
  306. msleep(200);
  307. }
  308. #endif
  309. if (!cpu_callin_map[cpu]) {
  310. printk("Processor %u is stuck.\n", cpu);
  311. return -ENOENT;
  312. }
  313. printk("Processor %u found.\n", cpu);
  314. if (smp_ops->give_timebase)
  315. smp_ops->give_timebase();
  316. /* Wait until cpu puts itself in the online map */
  317. while (!cpu_online(cpu))
  318. cpu_relax();
  319. return 0;
  320. }
  321. /* Return the value of the reg property corresponding to the given
  322. * logical cpu.
  323. */
  324. int cpu_to_core_id(int cpu)
  325. {
  326. struct device_node *np;
  327. const int *reg;
  328. int id = -1;
  329. np = of_get_cpu_node(cpu, NULL);
  330. if (!np)
  331. goto out;
  332. reg = of_get_property(np, "reg", NULL);
  333. if (!reg)
  334. goto out;
  335. id = *reg;
  336. out:
  337. of_node_put(np);
  338. return id;
  339. }
  340. /* Must be called when no change can occur to cpu_present_map,
  341. * i.e. during cpu online or offline.
  342. */
  343. static struct device_node *cpu_to_l2cache(int cpu)
  344. {
  345. struct device_node *np;
  346. const phandle *php;
  347. phandle ph;
  348. if (!cpu_present(cpu))
  349. return NULL;
  350. np = of_get_cpu_node(cpu, NULL);
  351. if (np == NULL)
  352. return NULL;
  353. php = of_get_property(np, "l2-cache", NULL);
  354. if (php == NULL)
  355. return NULL;
  356. ph = *php;
  357. of_node_put(np);
  358. return of_find_node_by_phandle(ph);
  359. }
  360. /* Activate a secondary processor. */
  361. int __devinit start_secondary(void *unused)
  362. {
  363. unsigned int cpu = smp_processor_id();
  364. struct device_node *l2_cache;
  365. int i, base;
  366. atomic_inc(&init_mm.mm_count);
  367. current->active_mm = &init_mm;
  368. smp_store_cpu_info(cpu);
  369. set_dec(tb_ticks_per_jiffy);
  370. preempt_disable();
  371. cpu_callin_map[cpu] = 1;
  372. smp_ops->setup_cpu(cpu);
  373. if (smp_ops->take_timebase)
  374. smp_ops->take_timebase();
  375. if (system_state > SYSTEM_BOOTING)
  376. snapshot_timebase();
  377. secondary_cpu_time_init();
  378. ipi_call_lock();
  379. notify_cpu_starting(cpu);
  380. cpu_set(cpu, cpu_online_map);
  381. /* Update sibling maps */
  382. base = cpu_first_thread_in_core(cpu);
  383. for (i = 0; i < threads_per_core; i++) {
  384. if (cpu_is_offline(base + i))
  385. continue;
  386. cpu_set(cpu, per_cpu(cpu_sibling_map, base + i));
  387. cpu_set(base + i, per_cpu(cpu_sibling_map, cpu));
  388. /* cpu_core_map should be a superset of
  389. * cpu_sibling_map even if we don't have cache
  390. * information, so update the former here, too.
  391. */
  392. cpu_set(cpu, per_cpu(cpu_core_map, base +i));
  393. cpu_set(base + i, per_cpu(cpu_core_map, cpu));
  394. }
  395. l2_cache = cpu_to_l2cache(cpu);
  396. for_each_online_cpu(i) {
  397. struct device_node *np = cpu_to_l2cache(i);
  398. if (!np)
  399. continue;
  400. if (np == l2_cache) {
  401. cpu_set(cpu, per_cpu(cpu_core_map, i));
  402. cpu_set(i, per_cpu(cpu_core_map, cpu));
  403. }
  404. of_node_put(np);
  405. }
  406. of_node_put(l2_cache);
  407. ipi_call_unlock();
  408. local_irq_enable();
  409. cpu_idle();
  410. return 0;
  411. }
  412. int setup_profiling_timer(unsigned int multiplier)
  413. {
  414. return 0;
  415. }
  416. void __init smp_cpus_done(unsigned int max_cpus)
  417. {
  418. cpumask_t old_mask;
  419. /* We want the setup_cpu() here to be called from CPU 0, but our
  420. * init thread may have been "borrowed" by another CPU in the meantime
  421. * se we pin us down to CPU 0 for a short while
  422. */
  423. old_mask = current->cpus_allowed;
  424. set_cpus_allowed(current, cpumask_of_cpu(boot_cpuid));
  425. if (smp_ops)
  426. smp_ops->setup_cpu(boot_cpuid);
  427. set_cpus_allowed(current, old_mask);
  428. snapshot_timebases();
  429. dump_numa_cpu_topology();
  430. }
  431. #ifdef CONFIG_HOTPLUG_CPU
  432. int __cpu_disable(void)
  433. {
  434. struct device_node *l2_cache;
  435. int cpu = smp_processor_id();
  436. int base, i;
  437. int err;
  438. if (!smp_ops->cpu_disable)
  439. return -ENOSYS;
  440. err = smp_ops->cpu_disable();
  441. if (err)
  442. return err;
  443. /* Update sibling maps */
  444. base = cpu_first_thread_in_core(cpu);
  445. for (i = 0; i < threads_per_core; i++) {
  446. cpu_clear(cpu, per_cpu(cpu_sibling_map, base + i));
  447. cpu_clear(base + i, per_cpu(cpu_sibling_map, cpu));
  448. cpu_clear(cpu, per_cpu(cpu_core_map, base +i));
  449. cpu_clear(base + i, per_cpu(cpu_core_map, cpu));
  450. }
  451. l2_cache = cpu_to_l2cache(cpu);
  452. for_each_present_cpu(i) {
  453. struct device_node *np = cpu_to_l2cache(i);
  454. if (!np)
  455. continue;
  456. if (np == l2_cache) {
  457. cpu_clear(cpu, per_cpu(cpu_core_map, i));
  458. cpu_clear(i, per_cpu(cpu_core_map, cpu));
  459. }
  460. of_node_put(np);
  461. }
  462. of_node_put(l2_cache);
  463. return 0;
  464. }
  465. void __cpu_die(unsigned int cpu)
  466. {
  467. if (smp_ops->cpu_die)
  468. smp_ops->cpu_die(cpu);
  469. }
  470. #endif