smp.c 18 KB

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