smp.c 13 KB

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