smp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. /* we notice need_resched on exit */
  91. break;
  92. case PPC_MSG_CALL_FUNC_SINGLE:
  93. generic_smp_call_function_single_interrupt();
  94. break;
  95. case PPC_MSG_DEBUGGER_BREAK:
  96. if (crash_ipi_function_ptr) {
  97. crash_ipi_function_ptr(get_irq_regs());
  98. break;
  99. }
  100. #ifdef CONFIG_DEBUGGER
  101. debugger_ipi(get_irq_regs());
  102. break;
  103. #endif /* CONFIG_DEBUGGER */
  104. /* FALLTHROUGH */
  105. default:
  106. printk("SMP %d: smp_message_recv(): unknown msg %d\n",
  107. smp_processor_id(), msg);
  108. break;
  109. }
  110. }
  111. void smp_send_reschedule(int cpu)
  112. {
  113. if (likely(smp_ops))
  114. smp_ops->message_pass(cpu, PPC_MSG_RESCHEDULE);
  115. }
  116. void arch_send_call_function_single_ipi(int cpu)
  117. {
  118. smp_ops->message_pass(cpu, PPC_MSG_CALL_FUNC_SINGLE);
  119. }
  120. void arch_send_call_function_ipi(cpumask_t mask)
  121. {
  122. unsigned int cpu;
  123. for_each_cpu_mask(cpu, mask)
  124. smp_ops->message_pass(cpu, PPC_MSG_CALL_FUNCTION);
  125. }
  126. #ifdef CONFIG_DEBUGGER
  127. void smp_send_debugger_break(int cpu)
  128. {
  129. if (likely(smp_ops))
  130. smp_ops->message_pass(cpu, PPC_MSG_DEBUGGER_BREAK);
  131. }
  132. #endif
  133. #ifdef CONFIG_KEXEC
  134. void crash_send_ipi(void (*crash_ipi_callback)(struct pt_regs *))
  135. {
  136. crash_ipi_function_ptr = crash_ipi_callback;
  137. if (crash_ipi_callback && smp_ops) {
  138. mb();
  139. smp_ops->message_pass(MSG_ALL_BUT_SELF, PPC_MSG_DEBUGGER_BREAK);
  140. }
  141. }
  142. #endif
  143. static void stop_this_cpu(void *dummy)
  144. {
  145. local_irq_disable();
  146. while (1)
  147. ;
  148. }
  149. void smp_send_stop(void)
  150. {
  151. smp_call_function(stop_this_cpu, NULL, 0);
  152. }
  153. struct thread_info *current_set[NR_CPUS];
  154. static void __devinit smp_store_cpu_info(int id)
  155. {
  156. per_cpu(pvr, id) = mfspr(SPRN_PVR);
  157. }
  158. static void __init smp_create_idle(unsigned int cpu)
  159. {
  160. struct task_struct *p;
  161. /* create a process for the processor */
  162. p = fork_idle(cpu);
  163. if (IS_ERR(p))
  164. panic("failed fork for CPU %u: %li", cpu, PTR_ERR(p));
  165. #ifdef CONFIG_PPC64
  166. paca[cpu].__current = p;
  167. paca[cpu].kstack = (unsigned long) task_thread_info(p)
  168. + THREAD_SIZE - STACK_FRAME_OVERHEAD;
  169. #endif
  170. current_set[cpu] = task_thread_info(p);
  171. task_thread_info(p)->cpu = cpu;
  172. }
  173. void __init smp_prepare_cpus(unsigned int max_cpus)
  174. {
  175. unsigned int cpu;
  176. DBG("smp_prepare_cpus\n");
  177. /*
  178. * setup_cpu may need to be called on the boot cpu. We havent
  179. * spun any cpus up but lets be paranoid.
  180. */
  181. BUG_ON(boot_cpuid != smp_processor_id());
  182. /* Fixup boot cpu */
  183. smp_store_cpu_info(boot_cpuid);
  184. cpu_callin_map[boot_cpuid] = 1;
  185. if (smp_ops)
  186. max_cpus = smp_ops->probe();
  187. else
  188. max_cpus = 1;
  189. smp_space_timers(max_cpus);
  190. for_each_possible_cpu(cpu)
  191. if (cpu != boot_cpuid)
  192. smp_create_idle(cpu);
  193. }
  194. void __devinit smp_prepare_boot_cpu(void)
  195. {
  196. BUG_ON(smp_processor_id() != boot_cpuid);
  197. cpu_set(boot_cpuid, cpu_online_map);
  198. cpu_set(boot_cpuid, per_cpu(cpu_sibling_map, boot_cpuid));
  199. cpu_set(boot_cpuid, per_cpu(cpu_core_map, boot_cpuid));
  200. #ifdef CONFIG_PPC64
  201. paca[boot_cpuid].__current = current;
  202. #endif
  203. current_set[boot_cpuid] = task_thread_info(current);
  204. }
  205. #ifdef CONFIG_HOTPLUG_CPU
  206. /* State of each CPU during hotplug phases */
  207. DEFINE_PER_CPU(int, cpu_state) = { 0 };
  208. int generic_cpu_disable(void)
  209. {
  210. unsigned int cpu = smp_processor_id();
  211. if (cpu == boot_cpuid)
  212. return -EBUSY;
  213. cpu_clear(cpu, cpu_online_map);
  214. #ifdef CONFIG_PPC64
  215. vdso_data->processorCount--;
  216. fixup_irqs(cpu_online_map);
  217. #endif
  218. return 0;
  219. }
  220. int generic_cpu_enable(unsigned int cpu)
  221. {
  222. /* Do the normal bootup if we haven't
  223. * already bootstrapped. */
  224. if (system_state != SYSTEM_RUNNING)
  225. return -ENOSYS;
  226. /* get the target out of it's holding state */
  227. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  228. smp_wmb();
  229. while (!cpu_online(cpu))
  230. cpu_relax();
  231. #ifdef CONFIG_PPC64
  232. fixup_irqs(cpu_online_map);
  233. /* counter the irq disable in fixup_irqs */
  234. local_irq_enable();
  235. #endif
  236. return 0;
  237. }
  238. void generic_cpu_die(unsigned int cpu)
  239. {
  240. int i;
  241. for (i = 0; i < 100; i++) {
  242. smp_rmb();
  243. if (per_cpu(cpu_state, cpu) == CPU_DEAD)
  244. return;
  245. msleep(100);
  246. }
  247. printk(KERN_ERR "CPU%d didn't die...\n", cpu);
  248. }
  249. void generic_mach_cpu_die(void)
  250. {
  251. unsigned int cpu;
  252. local_irq_disable();
  253. cpu = smp_processor_id();
  254. printk(KERN_DEBUG "CPU%d offline\n", cpu);
  255. __get_cpu_var(cpu_state) = CPU_DEAD;
  256. smp_wmb();
  257. while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE)
  258. cpu_relax();
  259. cpu_set(cpu, cpu_online_map);
  260. local_irq_enable();
  261. }
  262. #endif
  263. static int __devinit cpu_enable(unsigned int cpu)
  264. {
  265. if (smp_ops && smp_ops->cpu_enable)
  266. return smp_ops->cpu_enable(cpu);
  267. return -ENOSYS;
  268. }
  269. int __cpuinit __cpu_up(unsigned int cpu)
  270. {
  271. int c;
  272. secondary_ti = current_set[cpu];
  273. if (!cpu_enable(cpu))
  274. return 0;
  275. if (smp_ops == NULL ||
  276. (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu)))
  277. return -EINVAL;
  278. /* Make sure callin-map entry is 0 (can be leftover a CPU
  279. * hotplug
  280. */
  281. cpu_callin_map[cpu] = 0;
  282. /* The information for processor bringup must
  283. * be written out to main store before we release
  284. * the processor.
  285. */
  286. smp_mb();
  287. /* wake up cpus */
  288. DBG("smp: kicking cpu %d\n", cpu);
  289. smp_ops->kick_cpu(cpu);
  290. /*
  291. * wait to see if the cpu made a callin (is actually up).
  292. * use this value that I found through experimentation.
  293. * -- Cort
  294. */
  295. if (system_state < SYSTEM_RUNNING)
  296. for (c = 50000; c && !cpu_callin_map[cpu]; c--)
  297. udelay(100);
  298. #ifdef CONFIG_HOTPLUG_CPU
  299. else
  300. /*
  301. * CPUs can take much longer to come up in the
  302. * hotplug case. Wait five seconds.
  303. */
  304. for (c = 25; c && !cpu_callin_map[cpu]; c--) {
  305. msleep(200);
  306. }
  307. #endif
  308. if (!cpu_callin_map[cpu]) {
  309. printk("Processor %u is stuck.\n", cpu);
  310. return -ENOENT;
  311. }
  312. printk("Processor %u found.\n", cpu);
  313. if (smp_ops->give_timebase)
  314. smp_ops->give_timebase();
  315. /* Wait until cpu puts itself in the online map */
  316. while (!cpu_online(cpu))
  317. cpu_relax();
  318. return 0;
  319. }
  320. /* Return the value of the reg property corresponding to the given
  321. * logical cpu.
  322. */
  323. int cpu_to_core_id(int cpu)
  324. {
  325. struct device_node *np;
  326. const int *reg;
  327. int id = -1;
  328. np = of_get_cpu_node(cpu, NULL);
  329. if (!np)
  330. goto out;
  331. reg = of_get_property(np, "reg", NULL);
  332. if (!reg)
  333. goto out;
  334. id = *reg;
  335. out:
  336. of_node_put(np);
  337. return id;
  338. }
  339. /* Must be called when no change can occur to cpu_present_map,
  340. * i.e. during cpu online or offline.
  341. */
  342. static struct device_node *cpu_to_l2cache(int cpu)
  343. {
  344. struct device_node *np;
  345. const phandle *php;
  346. phandle ph;
  347. if (!cpu_present(cpu))
  348. return NULL;
  349. np = of_get_cpu_node(cpu, NULL);
  350. if (np == NULL)
  351. return NULL;
  352. php = of_get_property(np, "l2-cache", NULL);
  353. if (php == NULL)
  354. return NULL;
  355. ph = *php;
  356. of_node_put(np);
  357. return of_find_node_by_phandle(ph);
  358. }
  359. /* Activate a secondary processor. */
  360. int __devinit start_secondary(void *unused)
  361. {
  362. unsigned int cpu = smp_processor_id();
  363. struct device_node *l2_cache;
  364. int i, base;
  365. atomic_inc(&init_mm.mm_count);
  366. current->active_mm = &init_mm;
  367. smp_store_cpu_info(cpu);
  368. set_dec(tb_ticks_per_jiffy);
  369. preempt_disable();
  370. cpu_callin_map[cpu] = 1;
  371. smp_ops->setup_cpu(cpu);
  372. if (smp_ops->take_timebase)
  373. smp_ops->take_timebase();
  374. if (system_state > SYSTEM_BOOTING)
  375. snapshot_timebase();
  376. secondary_cpu_time_init();
  377. ipi_call_lock();
  378. notify_cpu_starting(cpu);
  379. cpu_set(cpu, cpu_online_map);
  380. /* Update sibling maps */
  381. base = cpu_first_thread_in_core(cpu);
  382. for (i = 0; i < threads_per_core; i++) {
  383. if (cpu_is_offline(base + i))
  384. continue;
  385. cpu_set(cpu, per_cpu(cpu_sibling_map, base + i));
  386. cpu_set(base + i, per_cpu(cpu_sibling_map, cpu));
  387. /* cpu_core_map should be a superset of
  388. * cpu_sibling_map even if we don't have cache
  389. * information, so update the former here, too.
  390. */
  391. cpu_set(cpu, per_cpu(cpu_core_map, base +i));
  392. cpu_set(base + i, per_cpu(cpu_core_map, cpu));
  393. }
  394. l2_cache = cpu_to_l2cache(cpu);
  395. for_each_online_cpu(i) {
  396. struct device_node *np = cpu_to_l2cache(i);
  397. if (!np)
  398. continue;
  399. if (np == l2_cache) {
  400. cpu_set(cpu, per_cpu(cpu_core_map, i));
  401. cpu_set(i, per_cpu(cpu_core_map, cpu));
  402. }
  403. of_node_put(np);
  404. }
  405. of_node_put(l2_cache);
  406. ipi_call_unlock();
  407. local_irq_enable();
  408. cpu_idle();
  409. return 0;
  410. }
  411. int setup_profiling_timer(unsigned int multiplier)
  412. {
  413. return 0;
  414. }
  415. void __init smp_cpus_done(unsigned int max_cpus)
  416. {
  417. cpumask_t old_mask;
  418. /* We want the setup_cpu() here to be called from CPU 0, but our
  419. * init thread may have been "borrowed" by another CPU in the meantime
  420. * se we pin us down to CPU 0 for a short while
  421. */
  422. old_mask = current->cpus_allowed;
  423. set_cpus_allowed(current, cpumask_of_cpu(boot_cpuid));
  424. if (smp_ops)
  425. smp_ops->setup_cpu(boot_cpuid);
  426. set_cpus_allowed(current, old_mask);
  427. snapshot_timebases();
  428. dump_numa_cpu_topology();
  429. }
  430. #ifdef CONFIG_HOTPLUG_CPU
  431. int __cpu_disable(void)
  432. {
  433. struct device_node *l2_cache;
  434. int cpu = smp_processor_id();
  435. int base, i;
  436. int err;
  437. if (!smp_ops->cpu_disable)
  438. return -ENOSYS;
  439. err = smp_ops->cpu_disable();
  440. if (err)
  441. return err;
  442. /* Update sibling maps */
  443. base = cpu_first_thread_in_core(cpu);
  444. for (i = 0; i < threads_per_core; i++) {
  445. cpu_clear(cpu, per_cpu(cpu_sibling_map, base + i));
  446. cpu_clear(base + i, per_cpu(cpu_sibling_map, cpu));
  447. cpu_clear(cpu, per_cpu(cpu_core_map, base +i));
  448. cpu_clear(base + i, per_cpu(cpu_core_map, cpu));
  449. }
  450. l2_cache = cpu_to_l2cache(cpu);
  451. for_each_present_cpu(i) {
  452. struct device_node *np = cpu_to_l2cache(i);
  453. if (!np)
  454. continue;
  455. if (np == l2_cache) {
  456. cpu_clear(cpu, per_cpu(cpu_core_map, i));
  457. cpu_clear(i, per_cpu(cpu_core_map, cpu));
  458. }
  459. of_node_put(np);
  460. }
  461. of_node_put(l2_cache);
  462. return 0;
  463. }
  464. void __cpu_die(unsigned int cpu)
  465. {
  466. if (smp_ops->cpu_die)
  467. smp_ops->cpu_die(cpu);
  468. }
  469. #endif