smp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. };
  61. /*
  62. * Boot a secondary CPU, and assign it the specified idle task.
  63. * This also gives us the initial stack to use for this CPU.
  64. */
  65. static int boot_secondary(unsigned int cpu, struct task_struct *idle)
  66. {
  67. if (cpu_ops[cpu]->cpu_boot)
  68. return cpu_ops[cpu]->cpu_boot(cpu);
  69. return -EOPNOTSUPP;
  70. }
  71. static DECLARE_COMPLETION(cpu_running);
  72. int __cpu_up(unsigned int cpu, struct task_struct *idle)
  73. {
  74. int ret;
  75. /*
  76. * We need to tell the secondary core where to find its stack and the
  77. * page tables.
  78. */
  79. secondary_data.stack = task_stack_page(idle) + THREAD_START_SP;
  80. __flush_dcache_area(&secondary_data, sizeof(secondary_data));
  81. /*
  82. * Now bring the CPU into our world.
  83. */
  84. ret = boot_secondary(cpu, idle);
  85. if (ret == 0) {
  86. /*
  87. * CPU was successfully started, wait for it to come online or
  88. * time out.
  89. */
  90. wait_for_completion_timeout(&cpu_running,
  91. msecs_to_jiffies(1000));
  92. if (!cpu_online(cpu)) {
  93. pr_crit("CPU%u: failed to come online\n", cpu);
  94. ret = -EIO;
  95. }
  96. } else {
  97. pr_err("CPU%u: failed to boot: %d\n", cpu, ret);
  98. }
  99. secondary_data.stack = NULL;
  100. return ret;
  101. }
  102. /*
  103. * This is the secondary CPU boot entry. We're using this CPUs
  104. * idle thread stack, but a set of temporary page tables.
  105. */
  106. asmlinkage void secondary_start_kernel(void)
  107. {
  108. struct mm_struct *mm = &init_mm;
  109. unsigned int cpu = smp_processor_id();
  110. printk("CPU%u: Booted secondary processor\n", cpu);
  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. /*
  119. * TTBR0 is only used for the identity mapping at this stage. Make it
  120. * point to zero page to avoid speculatively fetching new entries.
  121. */
  122. cpu_set_reserved_ttbr0();
  123. flush_tlb_all();
  124. preempt_disable();
  125. trace_hardirqs_off();
  126. if (cpu_ops[cpu]->cpu_postboot)
  127. cpu_ops[cpu]->cpu_postboot();
  128. /*
  129. * OK, now it's safe to let the boot CPU continue. Wait for
  130. * the CPU migration code to notice that the CPU is online
  131. * before we continue.
  132. */
  133. set_cpu_online(cpu, true);
  134. complete(&cpu_running);
  135. /*
  136. * Enable GIC and timers.
  137. */
  138. notify_cpu_starting(cpu);
  139. local_irq_enable();
  140. local_fiq_enable();
  141. /*
  142. * OK, it's off to the idle thread for us
  143. */
  144. cpu_startup_entry(CPUHP_ONLINE);
  145. }
  146. void __init smp_cpus_done(unsigned int max_cpus)
  147. {
  148. pr_info("SMP: Total of %d processors activated.\n", num_online_cpus());
  149. }
  150. void __init smp_prepare_boot_cpu(void)
  151. {
  152. }
  153. static void (*smp_cross_call)(const struct cpumask *, unsigned int);
  154. /*
  155. * Enumerate the possible CPU set from the device tree and build the
  156. * cpu logical map array containing MPIDR values related to logical
  157. * cpus. Assumes that cpu_logical_map(0) has already been initialized.
  158. */
  159. void __init smp_init_cpus(void)
  160. {
  161. struct device_node *dn = NULL;
  162. unsigned int i, cpu = 1;
  163. bool bootcpu_valid = false;
  164. while ((dn = of_find_node_by_type(dn, "cpu"))) {
  165. const u32 *cell;
  166. u64 hwid;
  167. /*
  168. * A cpu node with missing "reg" property is
  169. * considered invalid to build a cpu_logical_map
  170. * entry.
  171. */
  172. cell = of_get_property(dn, "reg", NULL);
  173. if (!cell) {
  174. pr_err("%s: missing reg property\n", dn->full_name);
  175. goto next;
  176. }
  177. hwid = of_read_number(cell, of_n_addr_cells(dn));
  178. /*
  179. * Non affinity bits must be set to 0 in the DT
  180. */
  181. if (hwid & ~MPIDR_HWID_BITMASK) {
  182. pr_err("%s: invalid reg property\n", dn->full_name);
  183. goto next;
  184. }
  185. /*
  186. * Duplicate MPIDRs are a recipe for disaster. Scan
  187. * all initialized entries and check for
  188. * duplicates. If any is found just ignore the cpu.
  189. * cpu_logical_map was initialized to INVALID_HWID to
  190. * avoid matching valid MPIDR values.
  191. */
  192. for (i = 1; (i < cpu) && (i < NR_CPUS); i++) {
  193. if (cpu_logical_map(i) == hwid) {
  194. pr_err("%s: duplicate cpu reg properties in the DT\n",
  195. dn->full_name);
  196. goto next;
  197. }
  198. }
  199. /*
  200. * The numbering scheme requires that the boot CPU
  201. * must be assigned logical id 0. Record it so that
  202. * the logical map built from DT is validated and can
  203. * be used.
  204. */
  205. if (hwid == cpu_logical_map(0)) {
  206. if (bootcpu_valid) {
  207. pr_err("%s: duplicate boot cpu reg property in DT\n",
  208. dn->full_name);
  209. goto next;
  210. }
  211. bootcpu_valid = true;
  212. /*
  213. * cpu_logical_map has already been
  214. * initialized and the boot cpu doesn't need
  215. * the enable-method so continue without
  216. * incrementing cpu.
  217. */
  218. continue;
  219. }
  220. if (cpu >= NR_CPUS)
  221. goto next;
  222. if (cpu_read_ops(dn, cpu) != 0)
  223. goto next;
  224. if (cpu_ops[cpu]->cpu_init(dn, cpu))
  225. goto next;
  226. pr_debug("cpu logical map 0x%llx\n", hwid);
  227. cpu_logical_map(cpu) = hwid;
  228. next:
  229. cpu++;
  230. }
  231. /* sanity check */
  232. if (cpu > NR_CPUS)
  233. pr_warning("no. of cores (%d) greater than configured maximum of %d - clipping\n",
  234. cpu, NR_CPUS);
  235. if (!bootcpu_valid) {
  236. pr_err("DT missing boot CPU MPIDR, not enabling secondaries\n");
  237. return;
  238. }
  239. /*
  240. * All the cpus that made it to the cpu_logical_map have been
  241. * validated so set them as possible cpus.
  242. */
  243. for (i = 0; i < NR_CPUS; i++)
  244. if (cpu_logical_map(i) != INVALID_HWID)
  245. set_cpu_possible(i, true);
  246. }
  247. void __init smp_prepare_cpus(unsigned int max_cpus)
  248. {
  249. int err;
  250. unsigned int cpu, ncores = num_possible_cpus();
  251. /*
  252. * are we trying to boot more cores than exist?
  253. */
  254. if (max_cpus > ncores)
  255. max_cpus = ncores;
  256. /* Don't bother if we're effectively UP */
  257. if (max_cpus <= 1)
  258. return;
  259. /*
  260. * Initialise the present map (which describes the set of CPUs
  261. * actually populated at the present time) and release the
  262. * secondaries from the bootloader.
  263. *
  264. * Make sure we online at most (max_cpus - 1) additional CPUs.
  265. */
  266. max_cpus--;
  267. for_each_possible_cpu(cpu) {
  268. if (max_cpus == 0)
  269. break;
  270. if (cpu == smp_processor_id())
  271. continue;
  272. if (!cpu_ops[cpu])
  273. continue;
  274. err = cpu_ops[cpu]->cpu_prepare(cpu);
  275. if (err)
  276. continue;
  277. set_cpu_present(cpu, true);
  278. max_cpus--;
  279. }
  280. }
  281. void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int))
  282. {
  283. smp_cross_call = fn;
  284. }
  285. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  286. {
  287. smp_cross_call(mask, IPI_CALL_FUNC);
  288. }
  289. void arch_send_call_function_single_ipi(int cpu)
  290. {
  291. smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
  292. }
  293. static const char *ipi_types[NR_IPI] = {
  294. #define S(x,s) [x - IPI_RESCHEDULE] = s
  295. S(IPI_RESCHEDULE, "Rescheduling interrupts"),
  296. S(IPI_CALL_FUNC, "Function call interrupts"),
  297. S(IPI_CALL_FUNC_SINGLE, "Single function call interrupts"),
  298. S(IPI_CPU_STOP, "CPU stop interrupts"),
  299. };
  300. void show_ipi_list(struct seq_file *p, int prec)
  301. {
  302. unsigned int cpu, i;
  303. for (i = 0; i < NR_IPI; i++) {
  304. seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i + IPI_RESCHEDULE,
  305. prec >= 4 ? " " : "");
  306. for_each_present_cpu(cpu)
  307. seq_printf(p, "%10u ",
  308. __get_irq_stat(cpu, ipi_irqs[i]));
  309. seq_printf(p, " %s\n", ipi_types[i]);
  310. }
  311. }
  312. u64 smp_irq_stat_cpu(unsigned int cpu)
  313. {
  314. u64 sum = 0;
  315. int i;
  316. for (i = 0; i < NR_IPI; i++)
  317. sum += __get_irq_stat(cpu, ipi_irqs[i]);
  318. return sum;
  319. }
  320. static DEFINE_RAW_SPINLOCK(stop_lock);
  321. /*
  322. * ipi_cpu_stop - handle IPI from smp_send_stop()
  323. */
  324. static void ipi_cpu_stop(unsigned int cpu)
  325. {
  326. if (system_state == SYSTEM_BOOTING ||
  327. system_state == SYSTEM_RUNNING) {
  328. raw_spin_lock(&stop_lock);
  329. pr_crit("CPU%u: stopping\n", cpu);
  330. dump_stack();
  331. raw_spin_unlock(&stop_lock);
  332. }
  333. set_cpu_online(cpu, false);
  334. local_fiq_disable();
  335. local_irq_disable();
  336. while (1)
  337. cpu_relax();
  338. }
  339. /*
  340. * Main handler for inter-processor interrupts
  341. */
  342. void handle_IPI(int ipinr, struct pt_regs *regs)
  343. {
  344. unsigned int cpu = smp_processor_id();
  345. struct pt_regs *old_regs = set_irq_regs(regs);
  346. if (ipinr >= IPI_RESCHEDULE && ipinr < IPI_RESCHEDULE + NR_IPI)
  347. __inc_irq_stat(cpu, ipi_irqs[ipinr - IPI_RESCHEDULE]);
  348. switch (ipinr) {
  349. case IPI_RESCHEDULE:
  350. scheduler_ipi();
  351. break;
  352. case IPI_CALL_FUNC:
  353. irq_enter();
  354. generic_smp_call_function_interrupt();
  355. irq_exit();
  356. break;
  357. case IPI_CALL_FUNC_SINGLE:
  358. irq_enter();
  359. generic_smp_call_function_single_interrupt();
  360. irq_exit();
  361. break;
  362. case IPI_CPU_STOP:
  363. irq_enter();
  364. ipi_cpu_stop(cpu);
  365. irq_exit();
  366. break;
  367. default:
  368. pr_crit("CPU%u: Unknown IPI message 0x%x\n", cpu, ipinr);
  369. break;
  370. }
  371. set_irq_regs(old_regs);
  372. }
  373. void smp_send_reschedule(int cpu)
  374. {
  375. smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
  376. }
  377. void smp_send_stop(void)
  378. {
  379. unsigned long timeout;
  380. if (num_online_cpus() > 1) {
  381. cpumask_t mask;
  382. cpumask_copy(&mask, cpu_online_mask);
  383. cpu_clear(smp_processor_id(), mask);
  384. smp_cross_call(&mask, IPI_CPU_STOP);
  385. }
  386. /* Wait up to one second for other CPUs to stop */
  387. timeout = USEC_PER_SEC;
  388. while (num_online_cpus() > 1 && timeout--)
  389. udelay(1);
  390. if (num_online_cpus() > 1)
  391. pr_warning("SMP: failed to stop secondary CPUs\n");
  392. }
  393. /*
  394. * not supported here
  395. */
  396. int setup_profiling_timer(unsigned int multiplier)
  397. {
  398. return -EINVAL;
  399. }