smp.c 13 KB

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