smp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * Xtensa SMP support functions.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2008 - 2013 Tensilica Inc.
  9. *
  10. * Chris Zankel <chris@zankel.net>
  11. * Joe Taylor <joe@tensilica.com>
  12. * Pete Delaney <piet@tensilica.com
  13. */
  14. #include <linux/cpu.h>
  15. #include <linux/cpumask.h>
  16. #include <linux/delay.h>
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/irqdomain.h>
  20. #include <linux/irq.h>
  21. #include <linux/kdebug.h>
  22. #include <linux/module.h>
  23. #include <linux/sched/mm.h>
  24. #include <linux/sched/hotplug.h>
  25. #include <linux/sched/task_stack.h>
  26. #include <linux/reboot.h>
  27. #include <linux/seq_file.h>
  28. #include <linux/smp.h>
  29. #include <linux/thread_info.h>
  30. #include <asm/cacheflush.h>
  31. #include <asm/kdebug.h>
  32. #include <asm/mmu_context.h>
  33. #include <asm/mxregs.h>
  34. #include <asm/platform.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/traps.h>
  37. #ifdef CONFIG_SMP
  38. # if XCHAL_HAVE_S32C1I == 0
  39. # error "The S32C1I option is required for SMP."
  40. # endif
  41. #endif
  42. static void system_invalidate_dcache_range(unsigned long start,
  43. unsigned long size);
  44. static void system_flush_invalidate_dcache_range(unsigned long start,
  45. unsigned long size);
  46. /* IPI (Inter Process Interrupt) */
  47. #define IPI_IRQ 0
  48. static irqreturn_t ipi_interrupt(int irq, void *dev_id);
  49. static struct irqaction ipi_irqaction = {
  50. .handler = ipi_interrupt,
  51. .flags = IRQF_PERCPU,
  52. .name = "ipi",
  53. };
  54. void ipi_init(void)
  55. {
  56. unsigned irq = irq_create_mapping(NULL, IPI_IRQ);
  57. setup_irq(irq, &ipi_irqaction);
  58. }
  59. static inline unsigned int get_core_count(void)
  60. {
  61. /* Bits 18..21 of SYSCFGID contain the core count minus 1. */
  62. unsigned int syscfgid = get_er(SYSCFGID);
  63. return ((syscfgid >> 18) & 0xf) + 1;
  64. }
  65. static inline int get_core_id(void)
  66. {
  67. /* Bits 0...18 of SYSCFGID contain the core id */
  68. unsigned int core_id = get_er(SYSCFGID);
  69. return core_id & 0x3fff;
  70. }
  71. void __init smp_prepare_cpus(unsigned int max_cpus)
  72. {
  73. unsigned i;
  74. for (i = 0; i < max_cpus; ++i)
  75. set_cpu_present(i, true);
  76. }
  77. void __init smp_init_cpus(void)
  78. {
  79. unsigned i;
  80. unsigned int ncpus = get_core_count();
  81. unsigned int core_id = get_core_id();
  82. pr_info("%s: Core Count = %d\n", __func__, ncpus);
  83. pr_info("%s: Core Id = %d\n", __func__, core_id);
  84. for (i = 0; i < ncpus; ++i)
  85. set_cpu_possible(i, true);
  86. }
  87. void __init smp_prepare_boot_cpu(void)
  88. {
  89. unsigned int cpu = smp_processor_id();
  90. BUG_ON(cpu != 0);
  91. cpu_asid_cache(cpu) = ASID_USER_FIRST;
  92. }
  93. void __init smp_cpus_done(unsigned int max_cpus)
  94. {
  95. }
  96. static int boot_secondary_processors = 1; /* Set with xt-gdb via .xt-gdb */
  97. static DECLARE_COMPLETION(cpu_running);
  98. void secondary_start_kernel(void)
  99. {
  100. struct mm_struct *mm = &init_mm;
  101. unsigned int cpu = smp_processor_id();
  102. init_mmu();
  103. #ifdef CONFIG_DEBUG_KERNEL
  104. if (boot_secondary_processors == 0) {
  105. pr_debug("%s: boot_secondary_processors:%d; Hanging cpu:%d\n",
  106. __func__, boot_secondary_processors, cpu);
  107. for (;;)
  108. __asm__ __volatile__ ("waiti " __stringify(LOCKLEVEL));
  109. }
  110. pr_debug("%s: boot_secondary_processors:%d; Booting cpu:%d\n",
  111. __func__, boot_secondary_processors, cpu);
  112. #endif
  113. /* Init EXCSAVE1 */
  114. secondary_trap_init();
  115. /* All kernel threads share the same mm context. */
  116. mmget(mm);
  117. mmgrab(mm);
  118. current->active_mm = mm;
  119. cpumask_set_cpu(cpu, mm_cpumask(mm));
  120. enter_lazy_tlb(mm, current);
  121. preempt_disable();
  122. trace_hardirqs_off();
  123. calibrate_delay();
  124. notify_cpu_starting(cpu);
  125. secondary_init_irq();
  126. local_timer_setup(cpu);
  127. set_cpu_online(cpu, true);
  128. local_irq_enable();
  129. complete(&cpu_running);
  130. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  131. }
  132. static void mx_cpu_start(void *p)
  133. {
  134. unsigned cpu = (unsigned)p;
  135. unsigned long run_stall_mask = get_er(MPSCORE);
  136. set_er(run_stall_mask & ~(1u << cpu), MPSCORE);
  137. pr_debug("%s: cpu: %d, run_stall_mask: %lx ---> %lx\n",
  138. __func__, cpu, run_stall_mask, get_er(MPSCORE));
  139. }
  140. static void mx_cpu_stop(void *p)
  141. {
  142. unsigned cpu = (unsigned)p;
  143. unsigned long run_stall_mask = get_er(MPSCORE);
  144. set_er(run_stall_mask | (1u << cpu), MPSCORE);
  145. pr_debug("%s: cpu: %d, run_stall_mask: %lx ---> %lx\n",
  146. __func__, cpu, run_stall_mask, get_er(MPSCORE));
  147. }
  148. #ifdef CONFIG_HOTPLUG_CPU
  149. unsigned long cpu_start_id __cacheline_aligned;
  150. #endif
  151. unsigned long cpu_start_ccount;
  152. static int boot_secondary(unsigned int cpu, struct task_struct *ts)
  153. {
  154. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  155. unsigned long ccount;
  156. int i;
  157. #ifdef CONFIG_HOTPLUG_CPU
  158. cpu_start_id = cpu;
  159. system_flush_invalidate_dcache_range(
  160. (unsigned long)&cpu_start_id, sizeof(cpu_start_id));
  161. #endif
  162. smp_call_function_single(0, mx_cpu_start, (void *)cpu, 1);
  163. for (i = 0; i < 2; ++i) {
  164. do
  165. ccount = get_ccount();
  166. while (!ccount);
  167. cpu_start_ccount = ccount;
  168. while (time_before(jiffies, timeout)) {
  169. mb();
  170. if (!cpu_start_ccount)
  171. break;
  172. }
  173. if (cpu_start_ccount) {
  174. smp_call_function_single(0, mx_cpu_stop,
  175. (void *)cpu, 1);
  176. cpu_start_ccount = 0;
  177. return -EIO;
  178. }
  179. }
  180. return 0;
  181. }
  182. int __cpu_up(unsigned int cpu, struct task_struct *idle)
  183. {
  184. int ret = 0;
  185. if (cpu_asid_cache(cpu) == 0)
  186. cpu_asid_cache(cpu) = ASID_USER_FIRST;
  187. start_info.stack = (unsigned long)task_pt_regs(idle);
  188. wmb();
  189. pr_debug("%s: Calling wakeup_secondary(cpu:%d, idle:%p, sp: %08lx)\n",
  190. __func__, cpu, idle, start_info.stack);
  191. ret = boot_secondary(cpu, idle);
  192. if (ret == 0) {
  193. wait_for_completion_timeout(&cpu_running,
  194. msecs_to_jiffies(1000));
  195. if (!cpu_online(cpu))
  196. ret = -EIO;
  197. }
  198. if (ret)
  199. pr_err("CPU %u failed to boot\n", cpu);
  200. return ret;
  201. }
  202. #ifdef CONFIG_HOTPLUG_CPU
  203. /*
  204. * __cpu_disable runs on the processor to be shutdown.
  205. */
  206. int __cpu_disable(void)
  207. {
  208. unsigned int cpu = smp_processor_id();
  209. /*
  210. * Take this CPU offline. Once we clear this, we can't return,
  211. * and we must not schedule until we're ready to give up the cpu.
  212. */
  213. set_cpu_online(cpu, false);
  214. /*
  215. * OK - migrate IRQs away from this CPU
  216. */
  217. migrate_irqs();
  218. /*
  219. * Flush user cache and TLB mappings, and then remove this CPU
  220. * from the vm mask set of all processes.
  221. */
  222. local_flush_cache_all();
  223. local_flush_tlb_all();
  224. invalidate_page_directory();
  225. clear_tasks_mm_cpumask(cpu);
  226. return 0;
  227. }
  228. static void platform_cpu_kill(unsigned int cpu)
  229. {
  230. smp_call_function_single(0, mx_cpu_stop, (void *)cpu, true);
  231. }
  232. /*
  233. * called on the thread which is asking for a CPU to be shutdown -
  234. * waits until shutdown has completed, or it is timed out.
  235. */
  236. void __cpu_die(unsigned int cpu)
  237. {
  238. unsigned long timeout = jiffies + msecs_to_jiffies(1000);
  239. while (time_before(jiffies, timeout)) {
  240. system_invalidate_dcache_range((unsigned long)&cpu_start_id,
  241. sizeof(cpu_start_id));
  242. if (cpu_start_id == -cpu) {
  243. platform_cpu_kill(cpu);
  244. return;
  245. }
  246. }
  247. pr_err("CPU%u: unable to kill\n", cpu);
  248. }
  249. void arch_cpu_idle_dead(void)
  250. {
  251. cpu_die();
  252. }
  253. /*
  254. * Called from the idle thread for the CPU which has been shutdown.
  255. *
  256. * Note that we disable IRQs here, but do not re-enable them
  257. * before returning to the caller. This is also the behaviour
  258. * of the other hotplug-cpu capable cores, so presumably coming
  259. * out of idle fixes this.
  260. */
  261. void __ref cpu_die(void)
  262. {
  263. idle_task_exit();
  264. local_irq_disable();
  265. __asm__ __volatile__(
  266. " movi a2, cpu_restart\n"
  267. " jx a2\n");
  268. }
  269. #endif /* CONFIG_HOTPLUG_CPU */
  270. enum ipi_msg_type {
  271. IPI_RESCHEDULE = 0,
  272. IPI_CALL_FUNC,
  273. IPI_CPU_STOP,
  274. IPI_MAX
  275. };
  276. static const struct {
  277. const char *short_text;
  278. const char *long_text;
  279. } ipi_text[] = {
  280. { .short_text = "RES", .long_text = "Rescheduling interrupts" },
  281. { .short_text = "CAL", .long_text = "Function call interrupts" },
  282. { .short_text = "DIE", .long_text = "CPU shutdown interrupts" },
  283. };
  284. struct ipi_data {
  285. unsigned long ipi_count[IPI_MAX];
  286. };
  287. static DEFINE_PER_CPU(struct ipi_data, ipi_data);
  288. static void send_ipi_message(const struct cpumask *callmask,
  289. enum ipi_msg_type msg_id)
  290. {
  291. int index;
  292. unsigned long mask = 0;
  293. for_each_cpu(index, callmask)
  294. if (index != smp_processor_id())
  295. mask |= 1 << index;
  296. set_er(mask, MIPISET(msg_id));
  297. }
  298. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  299. {
  300. send_ipi_message(mask, IPI_CALL_FUNC);
  301. }
  302. void arch_send_call_function_single_ipi(int cpu)
  303. {
  304. send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC);
  305. }
  306. void smp_send_reschedule(int cpu)
  307. {
  308. send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
  309. }
  310. void smp_send_stop(void)
  311. {
  312. struct cpumask targets;
  313. cpumask_copy(&targets, cpu_online_mask);
  314. cpumask_clear_cpu(smp_processor_id(), &targets);
  315. send_ipi_message(&targets, IPI_CPU_STOP);
  316. }
  317. static void ipi_cpu_stop(unsigned int cpu)
  318. {
  319. set_cpu_online(cpu, false);
  320. machine_halt();
  321. }
  322. irqreturn_t ipi_interrupt(int irq, void *dev_id)
  323. {
  324. unsigned int cpu = smp_processor_id();
  325. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  326. unsigned int msg;
  327. unsigned i;
  328. msg = get_er(MIPICAUSE(cpu));
  329. for (i = 0; i < IPI_MAX; i++)
  330. if (msg & (1 << i)) {
  331. set_er(1 << i, MIPICAUSE(cpu));
  332. ++ipi->ipi_count[i];
  333. }
  334. if (msg & (1 << IPI_RESCHEDULE))
  335. scheduler_ipi();
  336. if (msg & (1 << IPI_CALL_FUNC))
  337. generic_smp_call_function_interrupt();
  338. if (msg & (1 << IPI_CPU_STOP))
  339. ipi_cpu_stop(cpu);
  340. return IRQ_HANDLED;
  341. }
  342. void show_ipi_list(struct seq_file *p, int prec)
  343. {
  344. unsigned int cpu;
  345. unsigned i;
  346. for (i = 0; i < IPI_MAX; ++i) {
  347. seq_printf(p, "%*s:", prec, ipi_text[i].short_text);
  348. for_each_online_cpu(cpu)
  349. seq_printf(p, " %10lu",
  350. per_cpu(ipi_data, cpu).ipi_count[i]);
  351. seq_printf(p, " %s\n", ipi_text[i].long_text);
  352. }
  353. }
  354. int setup_profiling_timer(unsigned int multiplier)
  355. {
  356. pr_debug("setup_profiling_timer %d\n", multiplier);
  357. return 0;
  358. }
  359. /* TLB flush functions */
  360. struct flush_data {
  361. struct vm_area_struct *vma;
  362. unsigned long addr1;
  363. unsigned long addr2;
  364. };
  365. static void ipi_flush_tlb_all(void *arg)
  366. {
  367. local_flush_tlb_all();
  368. }
  369. void flush_tlb_all(void)
  370. {
  371. on_each_cpu(ipi_flush_tlb_all, NULL, 1);
  372. }
  373. static void ipi_flush_tlb_mm(void *arg)
  374. {
  375. local_flush_tlb_mm(arg);
  376. }
  377. void flush_tlb_mm(struct mm_struct *mm)
  378. {
  379. on_each_cpu(ipi_flush_tlb_mm, mm, 1);
  380. }
  381. static void ipi_flush_tlb_page(void *arg)
  382. {
  383. struct flush_data *fd = arg;
  384. local_flush_tlb_page(fd->vma, fd->addr1);
  385. }
  386. void flush_tlb_page(struct vm_area_struct *vma, unsigned long addr)
  387. {
  388. struct flush_data fd = {
  389. .vma = vma,
  390. .addr1 = addr,
  391. };
  392. on_each_cpu(ipi_flush_tlb_page, &fd, 1);
  393. }
  394. static void ipi_flush_tlb_range(void *arg)
  395. {
  396. struct flush_data *fd = arg;
  397. local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
  398. }
  399. void flush_tlb_range(struct vm_area_struct *vma,
  400. unsigned long start, unsigned long end)
  401. {
  402. struct flush_data fd = {
  403. .vma = vma,
  404. .addr1 = start,
  405. .addr2 = end,
  406. };
  407. on_each_cpu(ipi_flush_tlb_range, &fd, 1);
  408. }
  409. static void ipi_flush_tlb_kernel_range(void *arg)
  410. {
  411. struct flush_data *fd = arg;
  412. local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
  413. }
  414. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  415. {
  416. struct flush_data fd = {
  417. .addr1 = start,
  418. .addr2 = end,
  419. };
  420. on_each_cpu(ipi_flush_tlb_kernel_range, &fd, 1);
  421. }
  422. /* Cache flush functions */
  423. static void ipi_flush_cache_all(void *arg)
  424. {
  425. local_flush_cache_all();
  426. }
  427. void flush_cache_all(void)
  428. {
  429. on_each_cpu(ipi_flush_cache_all, NULL, 1);
  430. }
  431. static void ipi_flush_cache_page(void *arg)
  432. {
  433. struct flush_data *fd = arg;
  434. local_flush_cache_page(fd->vma, fd->addr1, fd->addr2);
  435. }
  436. void flush_cache_page(struct vm_area_struct *vma,
  437. unsigned long address, unsigned long pfn)
  438. {
  439. struct flush_data fd = {
  440. .vma = vma,
  441. .addr1 = address,
  442. .addr2 = pfn,
  443. };
  444. on_each_cpu(ipi_flush_cache_page, &fd, 1);
  445. }
  446. static void ipi_flush_cache_range(void *arg)
  447. {
  448. struct flush_data *fd = arg;
  449. local_flush_cache_range(fd->vma, fd->addr1, fd->addr2);
  450. }
  451. void flush_cache_range(struct vm_area_struct *vma,
  452. unsigned long start, unsigned long end)
  453. {
  454. struct flush_data fd = {
  455. .vma = vma,
  456. .addr1 = start,
  457. .addr2 = end,
  458. };
  459. on_each_cpu(ipi_flush_cache_range, &fd, 1);
  460. }
  461. static void ipi_flush_icache_range(void *arg)
  462. {
  463. struct flush_data *fd = arg;
  464. local_flush_icache_range(fd->addr1, fd->addr2);
  465. }
  466. void flush_icache_range(unsigned long start, unsigned long end)
  467. {
  468. struct flush_data fd = {
  469. .addr1 = start,
  470. .addr2 = end,
  471. };
  472. on_each_cpu(ipi_flush_icache_range, &fd, 1);
  473. }
  474. EXPORT_SYMBOL(flush_icache_range);
  475. /* ------------------------------------------------------------------------- */
  476. static void ipi_invalidate_dcache_range(void *arg)
  477. {
  478. struct flush_data *fd = arg;
  479. __invalidate_dcache_range(fd->addr1, fd->addr2);
  480. }
  481. static void system_invalidate_dcache_range(unsigned long start,
  482. unsigned long size)
  483. {
  484. struct flush_data fd = {
  485. .addr1 = start,
  486. .addr2 = size,
  487. };
  488. on_each_cpu(ipi_invalidate_dcache_range, &fd, 1);
  489. }
  490. static void ipi_flush_invalidate_dcache_range(void *arg)
  491. {
  492. struct flush_data *fd = arg;
  493. __flush_invalidate_dcache_range(fd->addr1, fd->addr2);
  494. }
  495. static void system_flush_invalidate_dcache_range(unsigned long start,
  496. unsigned long size)
  497. {
  498. struct flush_data fd = {
  499. .addr1 = start,
  500. .addr2 = size,
  501. };
  502. on_each_cpu(ipi_flush_invalidate_dcache_range, &fd, 1);
  503. }