smp.c 14 KB

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