smp.c 13 KB

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