smp.c 19 KB

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