PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/arch/powerpc/kernel/smp.c

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