smp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /*
  2. * SMP initialisation and IPI support
  3. * Based on arch/arm/kernel/smp.c
  4. *
  5. * Copyright (C) 2012 ARM Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/init.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/sched.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/cache.h>
  25. #include <linux/profile.h>
  26. #include <linux/errno.h>
  27. #include <linux/mm.h>
  28. #include <linux/err.h>
  29. #include <linux/cpu.h>
  30. #include <linux/smp.h>
  31. #include <linux/seq_file.h>
  32. #include <linux/irq.h>
  33. #include <linux/percpu.h>
  34. #include <linux/clockchips.h>
  35. #include <linux/completion.h>
  36. #include <linux/of.h>
  37. #include <linux/irq_work.h>
  38. #include <asm/atomic.h>
  39. #include <asm/cacheflush.h>
  40. #include <asm/cpu.h>
  41. #include <asm/cputype.h>
  42. #include <asm/cpu_ops.h>
  43. #include <asm/mmu_context.h>
  44. #include <asm/pgtable.h>
  45. #include <asm/pgalloc.h>
  46. #include <asm/processor.h>
  47. #include <asm/smp_plat.h>
  48. #include <asm/sections.h>
  49. #include <asm/tlbflush.h>
  50. #include <asm/ptrace.h>
  51. #define CREATE_TRACE_POINTS
  52. #include <trace/events/ipi.h>
  53. /*
  54. * as from 2.5, kernels no longer have an init_tasks structure
  55. * so we need some other way of telling a new secondary core
  56. * where to place its SVC stack
  57. */
  58. struct secondary_data secondary_data;
  59. enum ipi_msg_type {
  60. IPI_RESCHEDULE,
  61. IPI_CALL_FUNC,
  62. IPI_CALL_FUNC_SINGLE,
  63. IPI_CPU_STOP,
  64. IPI_TIMER,
  65. IPI_IRQ_WORK,
  66. };
  67. /*
  68. * Boot a secondary CPU, and assign it the specified idle task.
  69. * This also gives us the initial stack to use for this CPU.
  70. */
  71. static int boot_secondary(unsigned int cpu, struct task_struct *idle)
  72. {
  73. if (cpu_ops[cpu]->cpu_boot)
  74. return cpu_ops[cpu]->cpu_boot(cpu);
  75. return -EOPNOTSUPP;
  76. }
  77. static DECLARE_COMPLETION(cpu_running);
  78. int __cpu_up(unsigned int cpu, struct task_struct *idle)
  79. {
  80. int ret;
  81. /*
  82. * We need to tell the secondary core where to find its stack and the
  83. * page tables.
  84. */
  85. secondary_data.stack = task_stack_page(idle) + THREAD_START_SP;
  86. __flush_dcache_area(&secondary_data, sizeof(secondary_data));
  87. /*
  88. * Now bring the CPU into our world.
  89. */
  90. ret = boot_secondary(cpu, idle);
  91. if (ret == 0) {
  92. /*
  93. * CPU was successfully started, wait for it to come online or
  94. * time out.
  95. */
  96. wait_for_completion_timeout(&cpu_running,
  97. msecs_to_jiffies(1000));
  98. if (!cpu_online(cpu)) {
  99. pr_crit("CPU%u: failed to come online\n", cpu);
  100. ret = -EIO;
  101. }
  102. } else {
  103. pr_err("CPU%u: failed to boot: %d\n", cpu, ret);
  104. }
  105. secondary_data.stack = NULL;
  106. return ret;
  107. }
  108. static void smp_store_cpu_info(unsigned int cpuid)
  109. {
  110. store_cpu_topology(cpuid);
  111. }
  112. /*
  113. * This is the secondary CPU boot entry. We're using this CPUs
  114. * idle thread stack, but a set of temporary page tables.
  115. */
  116. asmlinkage void secondary_start_kernel(void)
  117. {
  118. struct mm_struct *mm = &init_mm;
  119. unsigned int cpu = smp_processor_id();
  120. /*
  121. * All kernel threads share the same mm context; grab a
  122. * reference and switch to it.
  123. */
  124. atomic_inc(&mm->mm_count);
  125. current->active_mm = mm;
  126. cpumask_set_cpu(cpu, mm_cpumask(mm));
  127. set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
  128. printk("CPU%u: Booted secondary processor\n", cpu);
  129. /*
  130. * TTBR0 is only used for the identity mapping at this stage. Make it
  131. * point to zero page to avoid speculatively fetching new entries.
  132. */
  133. cpu_set_reserved_ttbr0();
  134. flush_tlb_all();
  135. preempt_disable();
  136. trace_hardirqs_off();
  137. if (cpu_ops[cpu]->cpu_postboot)
  138. cpu_ops[cpu]->cpu_postboot();
  139. /*
  140. * Log the CPU info before it is marked online and might get read.
  141. */
  142. cpuinfo_store_cpu();
  143. /*
  144. * Enable GIC and timers.
  145. */
  146. notify_cpu_starting(cpu);
  147. smp_store_cpu_info(cpu);
  148. /*
  149. * OK, now it's safe to let the boot CPU continue. Wait for
  150. * the CPU migration code to notice that the CPU is online
  151. * before we continue.
  152. */
  153. set_cpu_online(cpu, true);
  154. complete(&cpu_running);
  155. local_dbg_enable();
  156. local_irq_enable();
  157. local_async_enable();
  158. /*
  159. * OK, it's off to the idle thread for us
  160. */
  161. cpu_startup_entry(CPUHP_ONLINE);
  162. }
  163. #ifdef CONFIG_HOTPLUG_CPU
  164. static int op_cpu_disable(unsigned int cpu)
  165. {
  166. /*
  167. * If we don't have a cpu_die method, abort before we reach the point
  168. * of no return. CPU0 may not have an cpu_ops, so test for it.
  169. */
  170. if (!cpu_ops[cpu] || !cpu_ops[cpu]->cpu_die)
  171. return -EOPNOTSUPP;
  172. /*
  173. * We may need to abort a hot unplug for some other mechanism-specific
  174. * reason.
  175. */
  176. if (cpu_ops[cpu]->cpu_disable)
  177. return cpu_ops[cpu]->cpu_disable(cpu);
  178. return 0;
  179. }
  180. /*
  181. * __cpu_disable runs on the processor to be shutdown.
  182. */
  183. int __cpu_disable(void)
  184. {
  185. unsigned int cpu = smp_processor_id();
  186. int ret;
  187. ret = op_cpu_disable(cpu);
  188. if (ret)
  189. return ret;
  190. /*
  191. * Take this CPU offline. Once we clear this, we can't return,
  192. * and we must not schedule until we're ready to give up the cpu.
  193. */
  194. set_cpu_online(cpu, false);
  195. /*
  196. * OK - migrate IRQs away from this CPU
  197. */
  198. migrate_irqs();
  199. /*
  200. * Remove this CPU from the vm mask set of all processes.
  201. */
  202. clear_tasks_mm_cpumask(cpu);
  203. return 0;
  204. }
  205. static int op_cpu_kill(unsigned int cpu)
  206. {
  207. /*
  208. * If we have no means of synchronising with the dying CPU, then assume
  209. * that it is really dead. We can only wait for an arbitrary length of
  210. * time and hope that it's dead, so let's skip the wait and just hope.
  211. */
  212. if (!cpu_ops[cpu]->cpu_kill)
  213. return 1;
  214. return cpu_ops[cpu]->cpu_kill(cpu);
  215. }
  216. static DECLARE_COMPLETION(cpu_died);
  217. /*
  218. * called on the thread which is asking for a CPU to be shutdown -
  219. * waits until shutdown has completed, or it is timed out.
  220. */
  221. void __cpu_die(unsigned int cpu)
  222. {
  223. if (!wait_for_completion_timeout(&cpu_died, msecs_to_jiffies(5000))) {
  224. pr_crit("CPU%u: cpu didn't die\n", cpu);
  225. return;
  226. }
  227. pr_notice("CPU%u: shutdown\n", cpu);
  228. /*
  229. * Now that the dying CPU is beyond the point of no return w.r.t.
  230. * in-kernel synchronisation, try to get the firwmare to help us to
  231. * verify that it has really left the kernel before we consider
  232. * clobbering anything it might still be using.
  233. */
  234. if (!op_cpu_kill(cpu))
  235. pr_warn("CPU%d may not have shut down cleanly\n", cpu);
  236. }
  237. /*
  238. * Called from the idle thread for the CPU which has been shutdown.
  239. *
  240. * Note that we disable IRQs here, but do not re-enable them
  241. * before returning to the caller. This is also the behaviour
  242. * of the other hotplug-cpu capable cores, so presumably coming
  243. * out of idle fixes this.
  244. */
  245. void cpu_die(void)
  246. {
  247. unsigned int cpu = smp_processor_id();
  248. idle_task_exit();
  249. local_irq_disable();
  250. /* Tell __cpu_die() that this CPU is now safe to dispose of */
  251. complete(&cpu_died);
  252. /*
  253. * Actually shutdown the CPU. This must never fail. The specific hotplug
  254. * mechanism must perform all required cache maintenance to ensure that
  255. * no dirty lines are lost in the process of shutting down the CPU.
  256. */
  257. cpu_ops[cpu]->cpu_die(cpu);
  258. BUG();
  259. }
  260. #endif
  261. void __init smp_cpus_done(unsigned int max_cpus)
  262. {
  263. pr_info("SMP: Total of %d processors activated.\n", num_online_cpus());
  264. }
  265. void __init smp_prepare_boot_cpu(void)
  266. {
  267. set_my_cpu_offset(per_cpu_offset(smp_processor_id()));
  268. }
  269. /*
  270. * Enumerate the possible CPU set from the device tree and build the
  271. * cpu logical map array containing MPIDR values related to logical
  272. * cpus. Assumes that cpu_logical_map(0) has already been initialized.
  273. */
  274. void __init smp_init_cpus(void)
  275. {
  276. struct device_node *dn = NULL;
  277. unsigned int i, cpu = 1;
  278. bool bootcpu_valid = false;
  279. while ((dn = of_find_node_by_type(dn, "cpu"))) {
  280. const u32 *cell;
  281. u64 hwid;
  282. /*
  283. * A cpu node with missing "reg" property is
  284. * considered invalid to build a cpu_logical_map
  285. * entry.
  286. */
  287. cell = of_get_property(dn, "reg", NULL);
  288. if (!cell) {
  289. pr_err("%s: missing reg property\n", dn->full_name);
  290. goto next;
  291. }
  292. hwid = of_read_number(cell, of_n_addr_cells(dn));
  293. /*
  294. * Non affinity bits must be set to 0 in the DT
  295. */
  296. if (hwid & ~MPIDR_HWID_BITMASK) {
  297. pr_err("%s: invalid reg property\n", dn->full_name);
  298. goto next;
  299. }
  300. /*
  301. * Duplicate MPIDRs are a recipe for disaster. Scan
  302. * all initialized entries and check for
  303. * duplicates. If any is found just ignore the cpu.
  304. * cpu_logical_map was initialized to INVALID_HWID to
  305. * avoid matching valid MPIDR values.
  306. */
  307. for (i = 1; (i < cpu) && (i < NR_CPUS); i++) {
  308. if (cpu_logical_map(i) == hwid) {
  309. pr_err("%s: duplicate cpu reg properties in the DT\n",
  310. dn->full_name);
  311. goto next;
  312. }
  313. }
  314. /*
  315. * The numbering scheme requires that the boot CPU
  316. * must be assigned logical id 0. Record it so that
  317. * the logical map built from DT is validated and can
  318. * be used.
  319. */
  320. if (hwid == cpu_logical_map(0)) {
  321. if (bootcpu_valid) {
  322. pr_err("%s: duplicate boot cpu reg property in DT\n",
  323. dn->full_name);
  324. goto next;
  325. }
  326. bootcpu_valid = true;
  327. /*
  328. * cpu_logical_map has already been
  329. * initialized and the boot cpu doesn't need
  330. * the enable-method so continue without
  331. * incrementing cpu.
  332. */
  333. continue;
  334. }
  335. if (cpu >= NR_CPUS)
  336. goto next;
  337. if (cpu_read_ops(dn, cpu) != 0)
  338. goto next;
  339. if (cpu_ops[cpu]->cpu_init(dn, cpu))
  340. goto next;
  341. pr_debug("cpu logical map 0x%llx\n", hwid);
  342. cpu_logical_map(cpu) = hwid;
  343. next:
  344. cpu++;
  345. }
  346. /* sanity check */
  347. if (cpu > NR_CPUS)
  348. pr_warning("no. of cores (%d) greater than configured maximum of %d - clipping\n",
  349. cpu, NR_CPUS);
  350. if (!bootcpu_valid) {
  351. pr_err("DT missing boot CPU MPIDR, not enabling secondaries\n");
  352. return;
  353. }
  354. /*
  355. * All the cpus that made it to the cpu_logical_map have been
  356. * validated so set them as possible cpus.
  357. */
  358. for (i = 0; i < NR_CPUS; i++)
  359. if (cpu_logical_map(i) != INVALID_HWID)
  360. set_cpu_possible(i, true);
  361. }
  362. void __init smp_prepare_cpus(unsigned int max_cpus)
  363. {
  364. int err;
  365. unsigned int cpu, ncores = num_possible_cpus();
  366. init_cpu_topology();
  367. smp_store_cpu_info(smp_processor_id());
  368. /*
  369. * are we trying to boot more cores than exist?
  370. */
  371. if (max_cpus > ncores)
  372. max_cpus = ncores;
  373. /* Don't bother if we're effectively UP */
  374. if (max_cpus <= 1)
  375. return;
  376. /*
  377. * Initialise the present map (which describes the set of CPUs
  378. * actually populated at the present time) and release the
  379. * secondaries from the bootloader.
  380. *
  381. * Make sure we online at most (max_cpus - 1) additional CPUs.
  382. */
  383. max_cpus--;
  384. for_each_possible_cpu(cpu) {
  385. if (max_cpus == 0)
  386. break;
  387. if (cpu == smp_processor_id())
  388. continue;
  389. if (!cpu_ops[cpu])
  390. continue;
  391. err = cpu_ops[cpu]->cpu_prepare(cpu);
  392. if (err)
  393. continue;
  394. set_cpu_present(cpu, true);
  395. max_cpus--;
  396. }
  397. }
  398. void (*__smp_cross_call)(const struct cpumask *, unsigned int);
  399. void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int))
  400. {
  401. __smp_cross_call = fn;
  402. }
  403. static const char *ipi_types[NR_IPI] __tracepoint_string = {
  404. #define S(x,s) [x] = s
  405. S(IPI_RESCHEDULE, "Rescheduling interrupts"),
  406. S(IPI_CALL_FUNC, "Function call interrupts"),
  407. S(IPI_CALL_FUNC_SINGLE, "Single function call interrupts"),
  408. S(IPI_CPU_STOP, "CPU stop interrupts"),
  409. S(IPI_TIMER, "Timer broadcast interrupts"),
  410. S(IPI_IRQ_WORK, "IRQ work interrupts"),
  411. };
  412. static void smp_cross_call(const struct cpumask *target, unsigned int ipinr)
  413. {
  414. trace_ipi_raise(target, ipi_types[ipinr]);
  415. __smp_cross_call(target, ipinr);
  416. }
  417. void show_ipi_list(struct seq_file *p, int prec)
  418. {
  419. unsigned int cpu, i;
  420. for (i = 0; i < NR_IPI; i++) {
  421. seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i,
  422. prec >= 4 ? " " : "");
  423. for_each_online_cpu(cpu)
  424. seq_printf(p, "%10u ",
  425. __get_irq_stat(cpu, ipi_irqs[i]));
  426. seq_printf(p, " %s\n", ipi_types[i]);
  427. }
  428. }
  429. u64 smp_irq_stat_cpu(unsigned int cpu)
  430. {
  431. u64 sum = 0;
  432. int i;
  433. for (i = 0; i < NR_IPI; i++)
  434. sum += __get_irq_stat(cpu, ipi_irqs[i]);
  435. return sum;
  436. }
  437. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  438. {
  439. smp_cross_call(mask, IPI_CALL_FUNC);
  440. }
  441. void arch_send_call_function_single_ipi(int cpu)
  442. {
  443. smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
  444. }
  445. #ifdef CONFIG_IRQ_WORK
  446. void arch_irq_work_raise(void)
  447. {
  448. if (__smp_cross_call)
  449. smp_cross_call(cpumask_of(smp_processor_id()), IPI_IRQ_WORK);
  450. }
  451. #endif
  452. static DEFINE_RAW_SPINLOCK(stop_lock);
  453. /*
  454. * ipi_cpu_stop - handle IPI from smp_send_stop()
  455. */
  456. static void ipi_cpu_stop(unsigned int cpu)
  457. {
  458. if (system_state == SYSTEM_BOOTING ||
  459. system_state == SYSTEM_RUNNING) {
  460. raw_spin_lock(&stop_lock);
  461. pr_crit("CPU%u: stopping\n", cpu);
  462. dump_stack();
  463. raw_spin_unlock(&stop_lock);
  464. }
  465. set_cpu_online(cpu, false);
  466. local_irq_disable();
  467. while (1)
  468. cpu_relax();
  469. }
  470. /*
  471. * Main handler for inter-processor interrupts
  472. */
  473. void handle_IPI(int ipinr, struct pt_regs *regs)
  474. {
  475. unsigned int cpu = smp_processor_id();
  476. struct pt_regs *old_regs = set_irq_regs(regs);
  477. if ((unsigned)ipinr < NR_IPI) {
  478. trace_ipi_entry(ipi_types[ipinr]);
  479. __inc_irq_stat(cpu, ipi_irqs[ipinr]);
  480. }
  481. switch (ipinr) {
  482. case IPI_RESCHEDULE:
  483. scheduler_ipi();
  484. break;
  485. case IPI_CALL_FUNC:
  486. irq_enter();
  487. generic_smp_call_function_interrupt();
  488. irq_exit();
  489. break;
  490. case IPI_CALL_FUNC_SINGLE:
  491. irq_enter();
  492. generic_smp_call_function_single_interrupt();
  493. irq_exit();
  494. break;
  495. case IPI_CPU_STOP:
  496. irq_enter();
  497. ipi_cpu_stop(cpu);
  498. irq_exit();
  499. break;
  500. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  501. case IPI_TIMER:
  502. irq_enter();
  503. tick_receive_broadcast();
  504. irq_exit();
  505. break;
  506. #endif
  507. #ifdef CONFIG_IRQ_WORK
  508. case IPI_IRQ_WORK:
  509. irq_enter();
  510. irq_work_run();
  511. irq_exit();
  512. break;
  513. #endif
  514. default:
  515. pr_crit("CPU%u: Unknown IPI message 0x%x\n", cpu, ipinr);
  516. break;
  517. }
  518. if ((unsigned)ipinr < NR_IPI)
  519. trace_ipi_exit(ipi_types[ipinr]);
  520. set_irq_regs(old_regs);
  521. }
  522. void smp_send_reschedule(int cpu)
  523. {
  524. smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
  525. }
  526. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  527. void tick_broadcast(const struct cpumask *mask)
  528. {
  529. smp_cross_call(mask, IPI_TIMER);
  530. }
  531. #endif
  532. void smp_send_stop(void)
  533. {
  534. unsigned long timeout;
  535. if (num_online_cpus() > 1) {
  536. cpumask_t mask;
  537. cpumask_copy(&mask, cpu_online_mask);
  538. cpu_clear(smp_processor_id(), mask);
  539. smp_cross_call(&mask, IPI_CPU_STOP);
  540. }
  541. /* Wait up to one second for other CPUs to stop */
  542. timeout = USEC_PER_SEC;
  543. while (num_online_cpus() > 1 && timeout--)
  544. udelay(1);
  545. if (num_online_cpus() > 1)
  546. pr_warning("SMP: failed to stop secondary CPUs\n");
  547. }
  548. /*
  549. * not supported here
  550. */
  551. int setup_profiling_timer(unsigned int multiplier)
  552. {
  553. return -EINVAL;
  554. }