smp.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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/export.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/device.h>
  29. #include <linux/cpu.h>
  30. #include <linux/notifier.h>
  31. #include <linux/topology.h>
  32. #include <asm/ptrace.h>
  33. #include <linux/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/mpic.h>
  44. #include <asm/vdso_datapage.h>
  45. #ifdef CONFIG_PPC64
  46. #include <asm/paca.h>
  47. #endif
  48. #include <asm/vdso.h>
  49. #include <asm/debug.h>
  50. #ifdef DEBUG
  51. #include <asm/udbg.h>
  52. #define DBG(fmt...) udbg_printf(fmt)
  53. #else
  54. #define DBG(fmt...)
  55. #endif
  56. #ifdef CONFIG_HOTPLUG_CPU
  57. /* State of each CPU during hotplug phases */
  58. static DEFINE_PER_CPU(int, cpu_state) = { 0 };
  59. #endif
  60. struct thread_info *secondary_ti;
  61. DEFINE_PER_CPU(cpumask_var_t, cpu_sibling_map);
  62. DEFINE_PER_CPU(cpumask_var_t, cpu_core_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. /* Can't be static due to PowerMac hackery */
  68. volatile unsigned int cpu_callin_map[NR_CPUS];
  69. int smt_enabled_at_boot = 1;
  70. static void (*crash_ipi_function_ptr)(struct pt_regs *) = NULL;
  71. /*
  72. * Returns 1 if the specified cpu should be brought up during boot.
  73. * Used to inhibit booting threads if they've been disabled or
  74. * limited on the command line
  75. */
  76. int smp_generic_cpu_bootable(unsigned int nr)
  77. {
  78. /* Special case - we inhibit secondary thread startup
  79. * during boot if the user requests it.
  80. */
  81. if (system_state == SYSTEM_BOOTING && cpu_has_feature(CPU_FTR_SMT)) {
  82. if (!smt_enabled_at_boot && cpu_thread_in_core(nr) != 0)
  83. return 0;
  84. if (smt_enabled_at_boot
  85. && cpu_thread_in_core(nr) >= smt_enabled_at_boot)
  86. return 0;
  87. }
  88. return 1;
  89. }
  90. #ifdef CONFIG_PPC64
  91. int smp_generic_kick_cpu(int nr)
  92. {
  93. BUG_ON(nr < 0 || nr >= NR_CPUS);
  94. /*
  95. * The processor is currently spinning, waiting for the
  96. * cpu_start field to become non-zero After we set cpu_start,
  97. * the processor will continue on to secondary_start
  98. */
  99. if (!paca[nr].cpu_start) {
  100. paca[nr].cpu_start = 1;
  101. smp_mb();
  102. return 0;
  103. }
  104. #ifdef CONFIG_HOTPLUG_CPU
  105. /*
  106. * Ok it's not there, so it might be soft-unplugged, let's
  107. * try to bring it back
  108. */
  109. generic_set_cpu_up(nr);
  110. smp_wmb();
  111. smp_send_reschedule(nr);
  112. #endif /* CONFIG_HOTPLUG_CPU */
  113. return 0;
  114. }
  115. #endif /* CONFIG_PPC64 */
  116. static irqreturn_t call_function_action(int irq, void *data)
  117. {
  118. generic_smp_call_function_interrupt();
  119. return IRQ_HANDLED;
  120. }
  121. static irqreturn_t reschedule_action(int irq, void *data)
  122. {
  123. scheduler_ipi();
  124. return IRQ_HANDLED;
  125. }
  126. static irqreturn_t call_function_single_action(int irq, void *data)
  127. {
  128. generic_smp_call_function_single_interrupt();
  129. return IRQ_HANDLED;
  130. }
  131. static irqreturn_t debug_ipi_action(int irq, void *data)
  132. {
  133. if (crash_ipi_function_ptr) {
  134. crash_ipi_function_ptr(get_irq_regs());
  135. return IRQ_HANDLED;
  136. }
  137. #ifdef CONFIG_DEBUGGER
  138. debugger_ipi(get_irq_regs());
  139. #endif /* CONFIG_DEBUGGER */
  140. return IRQ_HANDLED;
  141. }
  142. static irq_handler_t smp_ipi_action[] = {
  143. [PPC_MSG_CALL_FUNCTION] = call_function_action,
  144. [PPC_MSG_RESCHEDULE] = reschedule_action,
  145. [PPC_MSG_CALL_FUNC_SINGLE] = call_function_single_action,
  146. [PPC_MSG_DEBUGGER_BREAK] = debug_ipi_action,
  147. };
  148. const char *smp_ipi_name[] = {
  149. [PPC_MSG_CALL_FUNCTION] = "ipi call function",
  150. [PPC_MSG_RESCHEDULE] = "ipi reschedule",
  151. [PPC_MSG_CALL_FUNC_SINGLE] = "ipi call function single",
  152. [PPC_MSG_DEBUGGER_BREAK] = "ipi debugger",
  153. };
  154. /* optional function to request ipi, for controllers with >= 4 ipis */
  155. int smp_request_message_ipi(int virq, int msg)
  156. {
  157. int err;
  158. if (msg < 0 || msg > PPC_MSG_DEBUGGER_BREAK) {
  159. return -EINVAL;
  160. }
  161. #if !defined(CONFIG_DEBUGGER) && !defined(CONFIG_KEXEC)
  162. if (msg == PPC_MSG_DEBUGGER_BREAK) {
  163. return 1;
  164. }
  165. #endif
  166. err = request_irq(virq, smp_ipi_action[msg],
  167. IRQF_PERCPU | IRQF_NO_THREAD | IRQF_NO_SUSPEND,
  168. smp_ipi_name[msg], NULL);
  169. WARN(err < 0, "unable to request_irq %d for %s (rc %d)\n",
  170. virq, smp_ipi_name[msg], err);
  171. return err;
  172. }
  173. #ifdef CONFIG_PPC_SMP_MUXED_IPI
  174. struct cpu_messages {
  175. int messages; /* current messages */
  176. unsigned long data; /* data for cause ipi */
  177. };
  178. static DEFINE_PER_CPU_SHARED_ALIGNED(struct cpu_messages, ipi_message);
  179. void smp_muxed_ipi_set_data(int cpu, unsigned long data)
  180. {
  181. struct cpu_messages *info = &per_cpu(ipi_message, cpu);
  182. info->data = data;
  183. }
  184. void smp_muxed_ipi_message_pass(int cpu, int msg)
  185. {
  186. struct cpu_messages *info = &per_cpu(ipi_message, cpu);
  187. char *message = (char *)&info->messages;
  188. /*
  189. * Order previous accesses before accesses in the IPI handler.
  190. */
  191. smp_mb();
  192. message[msg] = 1;
  193. /*
  194. * cause_ipi functions are required to include a full barrier
  195. * before doing whatever causes the IPI.
  196. */
  197. smp_ops->cause_ipi(cpu, info->data);
  198. }
  199. #ifdef __BIG_ENDIAN__
  200. #define IPI_MESSAGE(A) (1 << (24 - 8 * (A)))
  201. #else
  202. #define IPI_MESSAGE(A) (1 << (8 * (A)))
  203. #endif
  204. irqreturn_t smp_ipi_demux(void)
  205. {
  206. struct cpu_messages *info = &__get_cpu_var(ipi_message);
  207. unsigned int all;
  208. mb(); /* order any irq clear */
  209. do {
  210. all = xchg(&info->messages, 0);
  211. if (all & IPI_MESSAGE(PPC_MSG_CALL_FUNCTION))
  212. generic_smp_call_function_interrupt();
  213. if (all & IPI_MESSAGE(PPC_MSG_RESCHEDULE))
  214. scheduler_ipi();
  215. if (all & IPI_MESSAGE(PPC_MSG_CALL_FUNC_SINGLE))
  216. generic_smp_call_function_single_interrupt();
  217. if (all & IPI_MESSAGE(PPC_MSG_DEBUGGER_BREAK))
  218. debug_ipi_action(0, NULL);
  219. } while (info->messages);
  220. return IRQ_HANDLED;
  221. }
  222. #endif /* CONFIG_PPC_SMP_MUXED_IPI */
  223. static inline void do_message_pass(int cpu, int msg)
  224. {
  225. if (smp_ops->message_pass)
  226. smp_ops->message_pass(cpu, msg);
  227. #ifdef CONFIG_PPC_SMP_MUXED_IPI
  228. else
  229. smp_muxed_ipi_message_pass(cpu, msg);
  230. #endif
  231. }
  232. void smp_send_reschedule(int cpu)
  233. {
  234. if (likely(smp_ops))
  235. do_message_pass(cpu, PPC_MSG_RESCHEDULE);
  236. }
  237. EXPORT_SYMBOL_GPL(smp_send_reschedule);
  238. void arch_send_call_function_single_ipi(int cpu)
  239. {
  240. do_message_pass(cpu, PPC_MSG_CALL_FUNC_SINGLE);
  241. }
  242. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  243. {
  244. unsigned int cpu;
  245. for_each_cpu(cpu, mask)
  246. do_message_pass(cpu, PPC_MSG_CALL_FUNCTION);
  247. }
  248. #if defined(CONFIG_DEBUGGER) || defined(CONFIG_KEXEC)
  249. void smp_send_debugger_break(void)
  250. {
  251. int cpu;
  252. int me = raw_smp_processor_id();
  253. if (unlikely(!smp_ops))
  254. return;
  255. for_each_online_cpu(cpu)
  256. if (cpu != me)
  257. do_message_pass(cpu, PPC_MSG_DEBUGGER_BREAK);
  258. }
  259. #endif
  260. #ifdef CONFIG_KEXEC
  261. void crash_send_ipi(void (*crash_ipi_callback)(struct pt_regs *))
  262. {
  263. crash_ipi_function_ptr = crash_ipi_callback;
  264. if (crash_ipi_callback) {
  265. mb();
  266. smp_send_debugger_break();
  267. }
  268. }
  269. #endif
  270. static void stop_this_cpu(void *dummy)
  271. {
  272. /* Remove this CPU */
  273. set_cpu_online(smp_processor_id(), false);
  274. local_irq_disable();
  275. while (1)
  276. ;
  277. }
  278. void smp_send_stop(void)
  279. {
  280. smp_call_function(stop_this_cpu, NULL, 0);
  281. }
  282. struct thread_info *current_set[NR_CPUS];
  283. static void smp_store_cpu_info(int id)
  284. {
  285. per_cpu(cpu_pvr, id) = mfspr(SPRN_PVR);
  286. #ifdef CONFIG_PPC_FSL_BOOK3E
  287. per_cpu(next_tlbcam_idx, id)
  288. = (mfspr(SPRN_TLB1CFG) & TLBnCFG_N_ENTRY) - 1;
  289. #endif
  290. }
  291. void __init smp_prepare_cpus(unsigned int max_cpus)
  292. {
  293. unsigned int cpu;
  294. DBG("smp_prepare_cpus\n");
  295. /*
  296. * setup_cpu may need to be called on the boot cpu. We havent
  297. * spun any cpus up but lets be paranoid.
  298. */
  299. BUG_ON(boot_cpuid != smp_processor_id());
  300. /* Fixup boot cpu */
  301. smp_store_cpu_info(boot_cpuid);
  302. cpu_callin_map[boot_cpuid] = 1;
  303. for_each_possible_cpu(cpu) {
  304. zalloc_cpumask_var_node(&per_cpu(cpu_sibling_map, cpu),
  305. GFP_KERNEL, cpu_to_node(cpu));
  306. zalloc_cpumask_var_node(&per_cpu(cpu_core_map, cpu),
  307. GFP_KERNEL, cpu_to_node(cpu));
  308. }
  309. cpumask_set_cpu(boot_cpuid, cpu_sibling_mask(boot_cpuid));
  310. cpumask_set_cpu(boot_cpuid, cpu_core_mask(boot_cpuid));
  311. if (smp_ops)
  312. if (smp_ops->probe)
  313. max_cpus = smp_ops->probe();
  314. else
  315. max_cpus = NR_CPUS;
  316. else
  317. max_cpus = 1;
  318. }
  319. void smp_prepare_boot_cpu(void)
  320. {
  321. BUG_ON(smp_processor_id() != boot_cpuid);
  322. #ifdef CONFIG_PPC64
  323. paca[boot_cpuid].__current = current;
  324. #endif
  325. current_set[boot_cpuid] = task_thread_info(current);
  326. }
  327. #ifdef CONFIG_HOTPLUG_CPU
  328. int generic_cpu_disable(void)
  329. {
  330. unsigned int cpu = smp_processor_id();
  331. if (cpu == boot_cpuid)
  332. return -EBUSY;
  333. set_cpu_online(cpu, false);
  334. #ifdef CONFIG_PPC64
  335. vdso_data->processorCount--;
  336. #endif
  337. migrate_irqs();
  338. return 0;
  339. }
  340. void generic_cpu_die(unsigned int cpu)
  341. {
  342. int i;
  343. for (i = 0; i < 100; i++) {
  344. smp_rmb();
  345. if (per_cpu(cpu_state, cpu) == CPU_DEAD)
  346. return;
  347. msleep(100);
  348. }
  349. printk(KERN_ERR "CPU%d didn't die...\n", cpu);
  350. }
  351. void generic_mach_cpu_die(void)
  352. {
  353. unsigned int cpu;
  354. local_irq_disable();
  355. idle_task_exit();
  356. cpu = smp_processor_id();
  357. printk(KERN_DEBUG "CPU%d offline\n", cpu);
  358. __get_cpu_var(cpu_state) = CPU_DEAD;
  359. smp_wmb();
  360. while (__get_cpu_var(cpu_state) != CPU_UP_PREPARE)
  361. cpu_relax();
  362. }
  363. void generic_set_cpu_dead(unsigned int cpu)
  364. {
  365. per_cpu(cpu_state, cpu) = CPU_DEAD;
  366. }
  367. /*
  368. * The cpu_state should be set to CPU_UP_PREPARE in kick_cpu(), otherwise
  369. * the cpu_state is always CPU_DEAD after calling generic_set_cpu_dead(),
  370. * which makes the delay in generic_cpu_die() not happen.
  371. */
  372. void generic_set_cpu_up(unsigned int cpu)
  373. {
  374. per_cpu(cpu_state, cpu) = CPU_UP_PREPARE;
  375. }
  376. int generic_check_cpu_restart(unsigned int cpu)
  377. {
  378. return per_cpu(cpu_state, cpu) == CPU_UP_PREPARE;
  379. }
  380. static atomic_t secondary_inhibit_count;
  381. /*
  382. * Don't allow secondary CPU threads to come online
  383. */
  384. void inhibit_secondary_onlining(void)
  385. {
  386. /*
  387. * This makes secondary_inhibit_count stable during cpu
  388. * online/offline operations.
  389. */
  390. get_online_cpus();
  391. atomic_inc(&secondary_inhibit_count);
  392. put_online_cpus();
  393. }
  394. EXPORT_SYMBOL_GPL(inhibit_secondary_onlining);
  395. /*
  396. * Allow secondary CPU threads to come online again
  397. */
  398. void uninhibit_secondary_onlining(void)
  399. {
  400. get_online_cpus();
  401. atomic_dec(&secondary_inhibit_count);
  402. put_online_cpus();
  403. }
  404. EXPORT_SYMBOL_GPL(uninhibit_secondary_onlining);
  405. static int secondaries_inhibited(void)
  406. {
  407. return atomic_read(&secondary_inhibit_count);
  408. }
  409. #else /* HOTPLUG_CPU */
  410. #define secondaries_inhibited() 0
  411. #endif
  412. static void cpu_idle_thread_init(unsigned int cpu, struct task_struct *idle)
  413. {
  414. struct thread_info *ti = task_thread_info(idle);
  415. #ifdef CONFIG_PPC64
  416. paca[cpu].__current = idle;
  417. paca[cpu].kstack = (unsigned long)ti + THREAD_SIZE - STACK_FRAME_OVERHEAD;
  418. #endif
  419. ti->cpu = cpu;
  420. secondary_ti = current_set[cpu] = ti;
  421. }
  422. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  423. {
  424. int rc, c;
  425. /*
  426. * Don't allow secondary threads to come online if inhibited
  427. */
  428. if (threads_per_core > 1 && secondaries_inhibited() &&
  429. cpu % threads_per_core != 0)
  430. return -EBUSY;
  431. if (smp_ops == NULL ||
  432. (smp_ops->cpu_bootable && !smp_ops->cpu_bootable(cpu)))
  433. return -EINVAL;
  434. cpu_idle_thread_init(cpu, tidle);
  435. /* Make sure callin-map entry is 0 (can be leftover a CPU
  436. * hotplug
  437. */
  438. cpu_callin_map[cpu] = 0;
  439. /* The information for processor bringup must
  440. * be written out to main store before we release
  441. * the processor.
  442. */
  443. smp_mb();
  444. /* wake up cpus */
  445. DBG("smp: kicking cpu %d\n", cpu);
  446. rc = smp_ops->kick_cpu(cpu);
  447. if (rc) {
  448. pr_err("smp: failed starting cpu %d (rc %d)\n", cpu, rc);
  449. return rc;
  450. }
  451. /*
  452. * wait to see if the cpu made a callin (is actually up).
  453. * use this value that I found through experimentation.
  454. * -- Cort
  455. */
  456. if (system_state < SYSTEM_RUNNING)
  457. for (c = 50000; c && !cpu_callin_map[cpu]; c--)
  458. udelay(100);
  459. #ifdef CONFIG_HOTPLUG_CPU
  460. else
  461. /*
  462. * CPUs can take much longer to come up in the
  463. * hotplug case. Wait five seconds.
  464. */
  465. for (c = 5000; c && !cpu_callin_map[cpu]; c--)
  466. msleep(1);
  467. #endif
  468. if (!cpu_callin_map[cpu]) {
  469. printk(KERN_ERR "Processor %u is stuck.\n", cpu);
  470. return -ENOENT;
  471. }
  472. DBG("Processor %u found.\n", cpu);
  473. if (smp_ops->give_timebase)
  474. smp_ops->give_timebase();
  475. /* Wait until cpu puts itself in the online map */
  476. while (!cpu_online(cpu))
  477. cpu_relax();
  478. return 0;
  479. }
  480. /* Return the value of the reg property corresponding to the given
  481. * logical cpu.
  482. */
  483. int cpu_to_core_id(int cpu)
  484. {
  485. struct device_node *np;
  486. const int *reg;
  487. int id = -1;
  488. np = of_get_cpu_node(cpu, NULL);
  489. if (!np)
  490. goto out;
  491. reg = of_get_property(np, "reg", NULL);
  492. if (!reg)
  493. goto out;
  494. id = *reg;
  495. out:
  496. of_node_put(np);
  497. return id;
  498. }
  499. /* Return the value of the chip-id property corresponding
  500. * to the given logical cpu.
  501. */
  502. int cpu_to_chip_id(int cpu)
  503. {
  504. struct device_node *np;
  505. np = of_get_cpu_node(cpu, NULL);
  506. if (!np)
  507. return -1;
  508. of_node_put(np);
  509. return of_get_ibm_chip_id(np);
  510. }
  511. EXPORT_SYMBOL(cpu_to_chip_id);
  512. /* Helper routines for cpu to core mapping */
  513. int cpu_core_index_of_thread(int cpu)
  514. {
  515. return cpu >> threads_shift;
  516. }
  517. EXPORT_SYMBOL_GPL(cpu_core_index_of_thread);
  518. int cpu_first_thread_of_core(int core)
  519. {
  520. return core << threads_shift;
  521. }
  522. EXPORT_SYMBOL_GPL(cpu_first_thread_of_core);
  523. static void traverse_siblings_chip_id(int cpu, bool add, int chipid)
  524. {
  525. const struct cpumask *mask;
  526. struct device_node *np;
  527. int i, plen;
  528. const __be32 *prop;
  529. mask = add ? cpu_online_mask : cpu_present_mask;
  530. for_each_cpu(i, mask) {
  531. np = of_get_cpu_node(i, NULL);
  532. if (!np)
  533. continue;
  534. prop = of_get_property(np, "ibm,chip-id", &plen);
  535. if (prop && plen == sizeof(int) &&
  536. of_read_number(prop, 1) == chipid) {
  537. if (add) {
  538. cpumask_set_cpu(cpu, cpu_core_mask(i));
  539. cpumask_set_cpu(i, cpu_core_mask(cpu));
  540. } else {
  541. cpumask_clear_cpu(cpu, cpu_core_mask(i));
  542. cpumask_clear_cpu(i, cpu_core_mask(cpu));
  543. }
  544. }
  545. of_node_put(np);
  546. }
  547. }
  548. /* Must be called when no change can occur to cpu_present_mask,
  549. * i.e. during cpu online or offline.
  550. */
  551. static struct device_node *cpu_to_l2cache(int cpu)
  552. {
  553. struct device_node *np;
  554. struct device_node *cache;
  555. if (!cpu_present(cpu))
  556. return NULL;
  557. np = of_get_cpu_node(cpu, NULL);
  558. if (np == NULL)
  559. return NULL;
  560. cache = of_find_next_cache_node(np);
  561. of_node_put(np);
  562. return cache;
  563. }
  564. static void traverse_core_siblings(int cpu, bool add)
  565. {
  566. struct device_node *l2_cache, *np;
  567. const struct cpumask *mask;
  568. int i, chip, plen;
  569. const __be32 *prop;
  570. /* First see if we have ibm,chip-id properties in cpu nodes */
  571. np = of_get_cpu_node(cpu, NULL);
  572. if (np) {
  573. chip = -1;
  574. prop = of_get_property(np, "ibm,chip-id", &plen);
  575. if (prop && plen == sizeof(int))
  576. chip = of_read_number(prop, 1);
  577. of_node_put(np);
  578. if (chip >= 0) {
  579. traverse_siblings_chip_id(cpu, add, chip);
  580. return;
  581. }
  582. }
  583. l2_cache = cpu_to_l2cache(cpu);
  584. mask = add ? cpu_online_mask : cpu_present_mask;
  585. for_each_cpu(i, mask) {
  586. np = cpu_to_l2cache(i);
  587. if (!np)
  588. continue;
  589. if (np == l2_cache) {
  590. if (add) {
  591. cpumask_set_cpu(cpu, cpu_core_mask(i));
  592. cpumask_set_cpu(i, cpu_core_mask(cpu));
  593. } else {
  594. cpumask_clear_cpu(cpu, cpu_core_mask(i));
  595. cpumask_clear_cpu(i, cpu_core_mask(cpu));
  596. }
  597. }
  598. of_node_put(np);
  599. }
  600. of_node_put(l2_cache);
  601. }
  602. /* Activate a secondary processor. */
  603. void start_secondary(void *unused)
  604. {
  605. unsigned int cpu = smp_processor_id();
  606. int i, base;
  607. atomic_inc(&init_mm.mm_count);
  608. current->active_mm = &init_mm;
  609. smp_store_cpu_info(cpu);
  610. set_dec(tb_ticks_per_jiffy);
  611. preempt_disable();
  612. cpu_callin_map[cpu] = 1;
  613. if (smp_ops->setup_cpu)
  614. smp_ops->setup_cpu(cpu);
  615. if (smp_ops->take_timebase)
  616. smp_ops->take_timebase();
  617. secondary_cpu_time_init();
  618. #ifdef CONFIG_PPC64
  619. if (system_state == SYSTEM_RUNNING)
  620. vdso_data->processorCount++;
  621. vdso_getcpu_init();
  622. #endif
  623. /* Update sibling maps */
  624. base = cpu_first_thread_sibling(cpu);
  625. for (i = 0; i < threads_per_core; i++) {
  626. if (cpu_is_offline(base + i) && (cpu != base + i))
  627. continue;
  628. cpumask_set_cpu(cpu, cpu_sibling_mask(base + i));
  629. cpumask_set_cpu(base + i, cpu_sibling_mask(cpu));
  630. /* cpu_core_map should be a superset of
  631. * cpu_sibling_map even if we don't have cache
  632. * information, so update the former here, too.
  633. */
  634. cpumask_set_cpu(cpu, cpu_core_mask(base + i));
  635. cpumask_set_cpu(base + i, cpu_core_mask(cpu));
  636. }
  637. traverse_core_siblings(cpu, true);
  638. smp_wmb();
  639. notify_cpu_starting(cpu);
  640. set_cpu_online(cpu, true);
  641. local_irq_enable();
  642. cpu_startup_entry(CPUHP_ONLINE);
  643. BUG();
  644. }
  645. int setup_profiling_timer(unsigned int multiplier)
  646. {
  647. return 0;
  648. }
  649. void __init smp_cpus_done(unsigned int max_cpus)
  650. {
  651. cpumask_var_t old_mask;
  652. /* We want the setup_cpu() here to be called from CPU 0, but our
  653. * init thread may have been "borrowed" by another CPU in the meantime
  654. * se we pin us down to CPU 0 for a short while
  655. */
  656. alloc_cpumask_var(&old_mask, GFP_NOWAIT);
  657. cpumask_copy(old_mask, tsk_cpus_allowed(current));
  658. set_cpus_allowed_ptr(current, cpumask_of(boot_cpuid));
  659. if (smp_ops && smp_ops->setup_cpu)
  660. smp_ops->setup_cpu(boot_cpuid);
  661. set_cpus_allowed_ptr(current, old_mask);
  662. free_cpumask_var(old_mask);
  663. if (smp_ops && smp_ops->bringup_done)
  664. smp_ops->bringup_done();
  665. dump_numa_cpu_topology();
  666. }
  667. int arch_sd_sibling_asym_packing(void)
  668. {
  669. if (cpu_has_feature(CPU_FTR_ASYM_SMT)) {
  670. printk_once(KERN_INFO "Enabling Asymmetric SMT scheduling\n");
  671. return SD_ASYM_PACKING;
  672. }
  673. return 0;
  674. }
  675. #ifdef CONFIG_HOTPLUG_CPU
  676. int __cpu_disable(void)
  677. {
  678. int cpu = smp_processor_id();
  679. int base, i;
  680. int err;
  681. if (!smp_ops->cpu_disable)
  682. return -ENOSYS;
  683. err = smp_ops->cpu_disable();
  684. if (err)
  685. return err;
  686. /* Update sibling maps */
  687. base = cpu_first_thread_sibling(cpu);
  688. for (i = 0; i < threads_per_core; i++) {
  689. cpumask_clear_cpu(cpu, cpu_sibling_mask(base + i));
  690. cpumask_clear_cpu(base + i, cpu_sibling_mask(cpu));
  691. cpumask_clear_cpu(cpu, cpu_core_mask(base + i));
  692. cpumask_clear_cpu(base + i, cpu_core_mask(cpu));
  693. }
  694. traverse_core_siblings(cpu, false);
  695. return 0;
  696. }
  697. void __cpu_die(unsigned int cpu)
  698. {
  699. if (smp_ops->cpu_die)
  700. smp_ops->cpu_die(cpu);
  701. }
  702. static DEFINE_MUTEX(powerpc_cpu_hotplug_driver_mutex);
  703. void cpu_hotplug_driver_lock()
  704. {
  705. mutex_lock(&powerpc_cpu_hotplug_driver_mutex);
  706. }
  707. void cpu_hotplug_driver_unlock()
  708. {
  709. mutex_unlock(&powerpc_cpu_hotplug_driver_mutex);
  710. }
  711. void cpu_die(void)
  712. {
  713. if (ppc_md.cpu_die)
  714. ppc_md.cpu_die();
  715. /* If we return, we re-enter start_secondary */
  716. start_secondary_resume();
  717. }
  718. #endif