smp.c 13 KB

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