smp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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. const char *enable_method;
  162. struct device_node *dn = NULL;
  163. unsigned int i, cpu = 1;
  164. bool bootcpu_valid = false;
  165. while ((dn = of_find_node_by_type(dn, "cpu"))) {
  166. const u32 *cell;
  167. u64 hwid;
  168. /*
  169. * A cpu node with missing "reg" property is
  170. * considered invalid to build a cpu_logical_map
  171. * entry.
  172. */
  173. cell = of_get_property(dn, "reg", NULL);
  174. if (!cell) {
  175. pr_err("%s: missing reg property\n", dn->full_name);
  176. goto next;
  177. }
  178. hwid = of_read_number(cell, of_n_addr_cells(dn));
  179. /*
  180. * Non affinity bits must be set to 0 in the DT
  181. */
  182. if (hwid & ~MPIDR_HWID_BITMASK) {
  183. pr_err("%s: invalid reg property\n", dn->full_name);
  184. goto next;
  185. }
  186. /*
  187. * Duplicate MPIDRs are a recipe for disaster. Scan
  188. * all initialized entries and check for
  189. * duplicates. If any is found just ignore the cpu.
  190. * cpu_logical_map was initialized to INVALID_HWID to
  191. * avoid matching valid MPIDR values.
  192. */
  193. for (i = 1; (i < cpu) && (i < NR_CPUS); i++) {
  194. if (cpu_logical_map(i) == hwid) {
  195. pr_err("%s: duplicate cpu reg properties in the DT\n",
  196. dn->full_name);
  197. goto next;
  198. }
  199. }
  200. /*
  201. * The numbering scheme requires that the boot CPU
  202. * must be assigned logical id 0. Record it so that
  203. * the logical map built from DT is validated and can
  204. * be used.
  205. */
  206. if (hwid == cpu_logical_map(0)) {
  207. if (bootcpu_valid) {
  208. pr_err("%s: duplicate boot cpu reg property in DT\n",
  209. dn->full_name);
  210. goto next;
  211. }
  212. bootcpu_valid = true;
  213. /*
  214. * cpu_logical_map has already been
  215. * initialized and the boot cpu doesn't need
  216. * the enable-method so continue without
  217. * incrementing cpu.
  218. */
  219. continue;
  220. }
  221. if (cpu >= NR_CPUS)
  222. goto next;
  223. /*
  224. * We currently support only the "spin-table" enable-method.
  225. */
  226. enable_method = of_get_property(dn, "enable-method", NULL);
  227. if (!enable_method) {
  228. pr_err("%s: missing enable-method property\n",
  229. dn->full_name);
  230. goto next;
  231. }
  232. cpu_ops[cpu] = cpu_get_ops(enable_method);
  233. if (!cpu_ops[cpu]) {
  234. pr_err("%s: invalid enable-method property: %s\n",
  235. dn->full_name, enable_method);
  236. goto next;
  237. }
  238. if (cpu_ops[cpu]->cpu_init(dn, cpu))
  239. goto next;
  240. pr_debug("cpu logical map 0x%llx\n", hwid);
  241. cpu_logical_map(cpu) = hwid;
  242. next:
  243. cpu++;
  244. }
  245. /* sanity check */
  246. if (cpu > NR_CPUS)
  247. pr_warning("no. of cores (%d) greater than configured maximum of %d - clipping\n",
  248. cpu, NR_CPUS);
  249. if (!bootcpu_valid) {
  250. pr_err("DT missing boot CPU MPIDR, not enabling secondaries\n");
  251. return;
  252. }
  253. /*
  254. * All the cpus that made it to the cpu_logical_map have been
  255. * validated so set them as possible cpus.
  256. */
  257. for (i = 0; i < NR_CPUS; i++)
  258. if (cpu_logical_map(i) != INVALID_HWID)
  259. set_cpu_possible(i, true);
  260. }
  261. void __init smp_prepare_cpus(unsigned int max_cpus)
  262. {
  263. int err;
  264. unsigned int cpu, ncores = num_possible_cpus();
  265. /*
  266. * are we trying to boot more cores than exist?
  267. */
  268. if (max_cpus > ncores)
  269. max_cpus = ncores;
  270. /* Don't bother if we're effectively UP */
  271. if (max_cpus <= 1)
  272. return;
  273. /*
  274. * Initialise the present map (which describes the set of CPUs
  275. * actually populated at the present time) and release the
  276. * secondaries from the bootloader.
  277. *
  278. * Make sure we online at most (max_cpus - 1) additional CPUs.
  279. */
  280. max_cpus--;
  281. for_each_possible_cpu(cpu) {
  282. if (max_cpus == 0)
  283. break;
  284. if (cpu == smp_processor_id())
  285. continue;
  286. if (!cpu_ops[cpu])
  287. continue;
  288. err = cpu_ops[cpu]->cpu_prepare(cpu);
  289. if (err)
  290. continue;
  291. set_cpu_present(cpu, true);
  292. max_cpus--;
  293. }
  294. }
  295. void __init set_smp_cross_call(void (*fn)(const struct cpumask *, unsigned int))
  296. {
  297. smp_cross_call = fn;
  298. }
  299. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  300. {
  301. smp_cross_call(mask, IPI_CALL_FUNC);
  302. }
  303. void arch_send_call_function_single_ipi(int cpu)
  304. {
  305. smp_cross_call(cpumask_of(cpu), IPI_CALL_FUNC_SINGLE);
  306. }
  307. static const char *ipi_types[NR_IPI] = {
  308. #define S(x,s) [x - IPI_RESCHEDULE] = s
  309. S(IPI_RESCHEDULE, "Rescheduling interrupts"),
  310. S(IPI_CALL_FUNC, "Function call interrupts"),
  311. S(IPI_CALL_FUNC_SINGLE, "Single function call interrupts"),
  312. S(IPI_CPU_STOP, "CPU stop interrupts"),
  313. };
  314. void show_ipi_list(struct seq_file *p, int prec)
  315. {
  316. unsigned int cpu, i;
  317. for (i = 0; i < NR_IPI; i++) {
  318. seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i + IPI_RESCHEDULE,
  319. prec >= 4 ? " " : "");
  320. for_each_present_cpu(cpu)
  321. seq_printf(p, "%10u ",
  322. __get_irq_stat(cpu, ipi_irqs[i]));
  323. seq_printf(p, " %s\n", ipi_types[i]);
  324. }
  325. }
  326. u64 smp_irq_stat_cpu(unsigned int cpu)
  327. {
  328. u64 sum = 0;
  329. int i;
  330. for (i = 0; i < NR_IPI; i++)
  331. sum += __get_irq_stat(cpu, ipi_irqs[i]);
  332. return sum;
  333. }
  334. static DEFINE_RAW_SPINLOCK(stop_lock);
  335. /*
  336. * ipi_cpu_stop - handle IPI from smp_send_stop()
  337. */
  338. static void ipi_cpu_stop(unsigned int cpu)
  339. {
  340. if (system_state == SYSTEM_BOOTING ||
  341. system_state == SYSTEM_RUNNING) {
  342. raw_spin_lock(&stop_lock);
  343. pr_crit("CPU%u: stopping\n", cpu);
  344. dump_stack();
  345. raw_spin_unlock(&stop_lock);
  346. }
  347. set_cpu_online(cpu, false);
  348. local_fiq_disable();
  349. local_irq_disable();
  350. while (1)
  351. cpu_relax();
  352. }
  353. /*
  354. * Main handler for inter-processor interrupts
  355. */
  356. void handle_IPI(int ipinr, struct pt_regs *regs)
  357. {
  358. unsigned int cpu = smp_processor_id();
  359. struct pt_regs *old_regs = set_irq_regs(regs);
  360. if (ipinr >= IPI_RESCHEDULE && ipinr < IPI_RESCHEDULE + NR_IPI)
  361. __inc_irq_stat(cpu, ipi_irqs[ipinr - IPI_RESCHEDULE]);
  362. switch (ipinr) {
  363. case IPI_RESCHEDULE:
  364. scheduler_ipi();
  365. break;
  366. case IPI_CALL_FUNC:
  367. irq_enter();
  368. generic_smp_call_function_interrupt();
  369. irq_exit();
  370. break;
  371. case IPI_CALL_FUNC_SINGLE:
  372. irq_enter();
  373. generic_smp_call_function_single_interrupt();
  374. irq_exit();
  375. break;
  376. case IPI_CPU_STOP:
  377. irq_enter();
  378. ipi_cpu_stop(cpu);
  379. irq_exit();
  380. break;
  381. default:
  382. pr_crit("CPU%u: Unknown IPI message 0x%x\n", cpu, ipinr);
  383. break;
  384. }
  385. set_irq_regs(old_regs);
  386. }
  387. void smp_send_reschedule(int cpu)
  388. {
  389. smp_cross_call(cpumask_of(cpu), IPI_RESCHEDULE);
  390. }
  391. void smp_send_stop(void)
  392. {
  393. unsigned long timeout;
  394. if (num_online_cpus() > 1) {
  395. cpumask_t mask;
  396. cpumask_copy(&mask, cpu_online_mask);
  397. cpu_clear(smp_processor_id(), mask);
  398. smp_cross_call(&mask, IPI_CPU_STOP);
  399. }
  400. /* Wait up to one second for other CPUs to stop */
  401. timeout = USEC_PER_SEC;
  402. while (num_online_cpus() > 1 && timeout--)
  403. udelay(1);
  404. if (num_online_cpus() > 1)
  405. pr_warning("SMP: failed to stop secondary CPUs\n");
  406. }
  407. /*
  408. * not supported here
  409. */
  410. int setup_profiling_timer(unsigned int multiplier)
  411. {
  412. return -EINVAL;
  413. }