smp.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  4. *
  5. * Derived from MIPS:
  6. * Copyright (C) 2000, 2001 Kanoj Sarcar
  7. * Copyright (C) 2000, 2001 Ralf Baechle
  8. * Copyright (C) 2000, 2001 Silicon Graphics, Inc.
  9. * Copyright (C) 2000, 2001, 2003 Broadcom Corporation
  10. */
  11. #include <linux/acpi.h>
  12. #include <linux/cpu.h>
  13. #include <linux/cpumask.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq_work.h>
  17. #include <linux/profile.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/smp.h>
  20. #include <linux/threads.h>
  21. #include <linux/export.h>
  22. #include <linux/suspend.h>
  23. #include <linux/syscore_ops.h>
  24. #include <linux/time.h>
  25. #include <linux/tracepoint.h>
  26. #include <linux/sched/hotplug.h>
  27. #include <linux/sched/task_stack.h>
  28. #include <asm/cpu.h>
  29. #include <asm/idle.h>
  30. #include <asm/loongson.h>
  31. #include <asm/mmu_context.h>
  32. #include <asm/numa.h>
  33. #include <asm/paravirt.h>
  34. #include <asm/processor.h>
  35. #include <asm/setup.h>
  36. #include <asm/time.h>
  37. int __cpu_number_map[NR_CPUS]; /* Map physical to logical */
  38. EXPORT_SYMBOL(__cpu_number_map);
  39. int __cpu_logical_map[NR_CPUS]; /* Map logical to physical */
  40. EXPORT_SYMBOL(__cpu_logical_map);
  41. /* Representing the threads (siblings) of each logical CPU */
  42. cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly;
  43. EXPORT_SYMBOL(cpu_sibling_map);
  44. /* Representing the core map of multi-core chips of each logical CPU */
  45. cpumask_t cpu_core_map[NR_CPUS] __read_mostly;
  46. EXPORT_SYMBOL(cpu_core_map);
  47. static DECLARE_COMPLETION(cpu_starting);
  48. static DECLARE_COMPLETION(cpu_running);
  49. /*
  50. * A logcal cpu mask containing only one VPE per core to
  51. * reduce the number of IPIs on large MT systems.
  52. */
  53. cpumask_t cpu_foreign_map[NR_CPUS] __read_mostly;
  54. EXPORT_SYMBOL(cpu_foreign_map);
  55. /* representing cpus for which sibling maps can be computed */
  56. static cpumask_t cpu_sibling_setup_map;
  57. /* representing cpus for which core maps can be computed */
  58. static cpumask_t cpu_core_setup_map;
  59. struct secondary_data cpuboot_data;
  60. static DEFINE_PER_CPU(int, cpu_state);
  61. static const char *ipi_types[NR_IPI] __tracepoint_string = {
  62. [IPI_RESCHEDULE] = "Rescheduling interrupts",
  63. [IPI_CALL_FUNCTION] = "Function call interrupts",
  64. [IPI_IRQ_WORK] = "IRQ work interrupts",
  65. [IPI_CLEAR_VECTOR] = "Clear vector interrupts",
  66. };
  67. void show_ipi_list(struct seq_file *p, int prec)
  68. {
  69. unsigned int cpu, i;
  70. for (i = 0; i < NR_IPI; i++) {
  71. seq_printf(p, "%*s%u:%s", prec - 1, "IPI", i, prec >= 4 ? " " : "");
  72. for_each_online_cpu(cpu)
  73. seq_printf(p, "%10u ", per_cpu(irq_stat, cpu).ipi_irqs[i]);
  74. seq_printf(p, " LoongArch %d %s\n", i + 1, ipi_types[i]);
  75. }
  76. }
  77. static inline void set_cpu_core_map(int cpu)
  78. {
  79. int i;
  80. cpumask_set_cpu(cpu, &cpu_core_setup_map);
  81. for_each_cpu(i, &cpu_core_setup_map) {
  82. if (cpu_data[cpu].package == cpu_data[i].package) {
  83. cpumask_set_cpu(i, &cpu_core_map[cpu]);
  84. cpumask_set_cpu(cpu, &cpu_core_map[i]);
  85. }
  86. }
  87. }
  88. static inline void set_cpu_sibling_map(int cpu)
  89. {
  90. int i;
  91. cpumask_set_cpu(cpu, &cpu_sibling_setup_map);
  92. for_each_cpu(i, &cpu_sibling_setup_map) {
  93. if (cpus_are_siblings(cpu, i)) {
  94. cpumask_set_cpu(i, &cpu_sibling_map[cpu]);
  95. cpumask_set_cpu(cpu, &cpu_sibling_map[i]);
  96. }
  97. }
  98. }
  99. static inline void clear_cpu_sibling_map(int cpu)
  100. {
  101. int i;
  102. for_each_cpu(i, &cpu_sibling_setup_map) {
  103. if (cpus_are_siblings(cpu, i)) {
  104. cpumask_clear_cpu(i, &cpu_sibling_map[cpu]);
  105. cpumask_clear_cpu(cpu, &cpu_sibling_map[i]);
  106. }
  107. }
  108. cpumask_clear_cpu(cpu, &cpu_sibling_setup_map);
  109. }
  110. /*
  111. * Calculate a new cpu_foreign_map mask whenever a
  112. * new cpu appears or disappears.
  113. */
  114. void calculate_cpu_foreign_map(void)
  115. {
  116. int i, k, core_present;
  117. cpumask_t temp_foreign_map;
  118. /* Re-calculate the mask */
  119. cpumask_clear(&temp_foreign_map);
  120. for_each_online_cpu(i) {
  121. core_present = 0;
  122. for_each_cpu(k, &temp_foreign_map)
  123. if (cpus_are_siblings(i, k))
  124. core_present = 1;
  125. if (!core_present)
  126. cpumask_set_cpu(i, &temp_foreign_map);
  127. }
  128. for_each_online_cpu(i)
  129. cpumask_andnot(&cpu_foreign_map[i],
  130. &temp_foreign_map, &cpu_sibling_map[i]);
  131. }
  132. /* Send mailbox buffer via Mail_Send */
  133. static void csr_mail_send(uint64_t data, int cpu, int mailbox)
  134. {
  135. uint64_t val;
  136. /* Send high 32 bits */
  137. val = IOCSR_MBUF_SEND_BLOCKING;
  138. val |= (IOCSR_MBUF_SEND_BOX_HI(mailbox) << IOCSR_MBUF_SEND_BOX_SHIFT);
  139. val |= (cpu << IOCSR_MBUF_SEND_CPU_SHIFT);
  140. val |= (data & IOCSR_MBUF_SEND_H32_MASK);
  141. iocsr_write64(val, LOONGARCH_IOCSR_MBUF_SEND);
  142. /* Send low 32 bits */
  143. val = IOCSR_MBUF_SEND_BLOCKING;
  144. val |= (IOCSR_MBUF_SEND_BOX_LO(mailbox) << IOCSR_MBUF_SEND_BOX_SHIFT);
  145. val |= (cpu << IOCSR_MBUF_SEND_CPU_SHIFT);
  146. val |= (data << IOCSR_MBUF_SEND_BUF_SHIFT);
  147. iocsr_write64(val, LOONGARCH_IOCSR_MBUF_SEND);
  148. };
  149. static u32 ipi_read_clear(int cpu)
  150. {
  151. u32 action;
  152. /* Load the ipi register to figure out what we're supposed to do */
  153. action = iocsr_read32(LOONGARCH_IOCSR_IPI_STATUS);
  154. /* Clear the ipi register to clear the interrupt */
  155. iocsr_write32(action, LOONGARCH_IOCSR_IPI_CLEAR);
  156. wbflush();
  157. return action;
  158. }
  159. static void ipi_write_action(int cpu, u32 action)
  160. {
  161. uint32_t val;
  162. val = IOCSR_IPI_SEND_BLOCKING | action;
  163. val |= (cpu << IOCSR_IPI_SEND_CPU_SHIFT);
  164. iocsr_write32(val, LOONGARCH_IOCSR_IPI_SEND);
  165. }
  166. static void loongson_send_ipi_single(int cpu, unsigned int action)
  167. {
  168. ipi_write_action(cpu_logical_map(cpu), (u32)action);
  169. }
  170. static void loongson_send_ipi_mask(const struct cpumask *mask, unsigned int action)
  171. {
  172. unsigned int i;
  173. for_each_cpu(i, mask)
  174. ipi_write_action(cpu_logical_map(i), (u32)action);
  175. }
  176. /*
  177. * This function sends a 'reschedule' IPI to another CPU.
  178. * it goes straight through and wastes no time serializing
  179. * anything. Worst case is that we lose a reschedule ...
  180. */
  181. void arch_smp_send_reschedule(int cpu)
  182. {
  183. mp_ops.send_ipi_single(cpu, ACTION_RESCHEDULE);
  184. }
  185. EXPORT_SYMBOL_GPL(arch_smp_send_reschedule);
  186. #ifdef CONFIG_IRQ_WORK
  187. void arch_irq_work_raise(void)
  188. {
  189. mp_ops.send_ipi_single(smp_processor_id(), ACTION_IRQ_WORK);
  190. }
  191. #endif
  192. static irqreturn_t loongson_ipi_interrupt(int irq, void *dev)
  193. {
  194. unsigned int action;
  195. unsigned int cpu = smp_processor_id();
  196. action = ipi_read_clear(cpu_logical_map(cpu));
  197. if (action & SMP_RESCHEDULE) {
  198. scheduler_ipi();
  199. per_cpu(irq_stat, cpu).ipi_irqs[IPI_RESCHEDULE]++;
  200. }
  201. if (action & SMP_CALL_FUNCTION) {
  202. generic_smp_call_function_interrupt();
  203. per_cpu(irq_stat, cpu).ipi_irqs[IPI_CALL_FUNCTION]++;
  204. }
  205. if (action & SMP_IRQ_WORK) {
  206. irq_work_run();
  207. per_cpu(irq_stat, cpu).ipi_irqs[IPI_IRQ_WORK]++;
  208. }
  209. if (action & SMP_CLEAR_VECTOR) {
  210. complete_irq_moving();
  211. per_cpu(irq_stat, cpu).ipi_irqs[IPI_CLEAR_VECTOR]++;
  212. }
  213. return IRQ_HANDLED;
  214. }
  215. static void loongson_init_ipi(void)
  216. {
  217. int r, ipi_irq;
  218. ipi_irq = get_percpu_irq(INT_IPI);
  219. if (ipi_irq < 0)
  220. panic("IPI IRQ mapping failed\n");
  221. irq_set_percpu_devid(ipi_irq);
  222. r = request_percpu_irq(ipi_irq, loongson_ipi_interrupt, "IPI", &irq_stat);
  223. if (r < 0)
  224. panic("IPI IRQ request failed\n");
  225. }
  226. struct smp_ops mp_ops = {
  227. .init_ipi = loongson_init_ipi,
  228. .send_ipi_single = loongson_send_ipi_single,
  229. .send_ipi_mask = loongson_send_ipi_mask,
  230. };
  231. static void __init fdt_smp_setup(void)
  232. {
  233. #ifdef CONFIG_OF
  234. unsigned int cpu, cpuid;
  235. struct device_node *node = NULL;
  236. for_each_of_cpu_node(node) {
  237. if (!of_device_is_available(node))
  238. continue;
  239. cpuid = of_get_cpu_hwid(node, 0);
  240. if (cpuid >= nr_cpu_ids)
  241. continue;
  242. if (cpuid == loongson_sysconf.boot_cpu_id)
  243. cpu = 0;
  244. else
  245. cpu = find_first_zero_bit(cpumask_bits(cpu_present_mask), NR_CPUS);
  246. num_processors++;
  247. set_cpu_possible(cpu, true);
  248. set_cpu_present(cpu, true);
  249. __cpu_number_map[cpuid] = cpu;
  250. __cpu_logical_map[cpu] = cpuid;
  251. early_numa_add_cpu(cpuid, 0);
  252. set_cpuid_to_node(cpuid, 0);
  253. }
  254. loongson_sysconf.nr_cpus = num_processors;
  255. set_bit(0, loongson_sysconf.cores_io_master);
  256. #endif
  257. }
  258. void __init loongson_smp_setup(void)
  259. {
  260. fdt_smp_setup();
  261. if (loongson_sysconf.cores_per_package == 0)
  262. loongson_sysconf.cores_per_package = num_processors;
  263. cpu_data[0].core = cpu_logical_map(0) % loongson_sysconf.cores_per_package;
  264. cpu_data[0].package = cpu_logical_map(0) / loongson_sysconf.cores_per_package;
  265. pv_ipi_init();
  266. iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_EN);
  267. pr_info("Detected %i available CPU(s)\n", loongson_sysconf.nr_cpus);
  268. }
  269. void __init loongson_prepare_cpus(unsigned int max_cpus)
  270. {
  271. int i = 0;
  272. parse_acpi_topology();
  273. cpu_data[0].global_id = cpu_logical_map(0);
  274. for (i = 0; i < loongson_sysconf.nr_cpus; i++) {
  275. set_cpu_present(i, true);
  276. csr_mail_send(0, __cpu_logical_map[i], 0);
  277. }
  278. per_cpu(cpu_state, smp_processor_id()) = CPU_ONLINE;
  279. }
  280. /*
  281. * Setup the PC, SP, and TP of a secondary processor and start it running!
  282. */
  283. void loongson_boot_secondary(int cpu, struct task_struct *idle)
  284. {
  285. unsigned long entry;
  286. pr_info("Booting CPU#%d...\n", cpu);
  287. entry = __pa_symbol((unsigned long)&smpboot_entry);
  288. cpuboot_data.stack = (unsigned long)__KSTK_TOS(idle);
  289. cpuboot_data.thread_info = (unsigned long)task_thread_info(idle);
  290. csr_mail_send(entry, cpu_logical_map(cpu), 0);
  291. loongson_send_ipi_single(cpu, ACTION_BOOT_CPU);
  292. }
  293. /*
  294. * SMP init and finish on secondary CPUs
  295. */
  296. void loongson_init_secondary(void)
  297. {
  298. unsigned int cpu = smp_processor_id();
  299. unsigned int imask = ECFGF_IP0 | ECFGF_IP1 | ECFGF_IP2 |
  300. ECFGF_IPI | ECFGF_PMC | ECFGF_TIMER | ECFGF_SIP0;
  301. change_csr_ecfg(ECFG0_IM, imask);
  302. iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_EN);
  303. #ifdef CONFIG_NUMA
  304. numa_add_cpu(cpu);
  305. #endif
  306. per_cpu(cpu_state, cpu) = CPU_ONLINE;
  307. cpu_data[cpu].package =
  308. cpu_logical_map(cpu) / loongson_sysconf.cores_per_package;
  309. cpu_data[cpu].core = pptt_enabled ? cpu_data[cpu].core :
  310. cpu_logical_map(cpu) % loongson_sysconf.cores_per_package;
  311. cpu_data[cpu].global_id = cpu_logical_map(cpu);
  312. }
  313. void loongson_smp_finish(void)
  314. {
  315. local_irq_enable();
  316. iocsr_write64(0, LOONGARCH_IOCSR_MBUF0);
  317. pr_info("CPU#%d finished\n", smp_processor_id());
  318. }
  319. #ifdef CONFIG_HOTPLUG_CPU
  320. int loongson_cpu_disable(void)
  321. {
  322. unsigned long flags;
  323. unsigned int cpu = smp_processor_id();
  324. if (io_master(cpu))
  325. return -EBUSY;
  326. #ifdef CONFIG_NUMA
  327. numa_remove_cpu(cpu);
  328. #endif
  329. set_cpu_online(cpu, false);
  330. clear_cpu_sibling_map(cpu);
  331. calculate_cpu_foreign_map();
  332. local_irq_save(flags);
  333. irq_migrate_all_off_this_cpu();
  334. clear_csr_ecfg(ECFG0_IM);
  335. local_irq_restore(flags);
  336. local_flush_tlb_all();
  337. return 0;
  338. }
  339. void loongson_cpu_die(unsigned int cpu)
  340. {
  341. while (per_cpu(cpu_state, cpu) != CPU_DEAD)
  342. cpu_relax();
  343. mb();
  344. }
  345. static void __noreturn idle_play_dead(void)
  346. {
  347. register uint64_t addr;
  348. register void (*init_fn)(void);
  349. idle_task_exit();
  350. local_irq_enable();
  351. set_csr_ecfg(ECFGF_IPI);
  352. __this_cpu_write(cpu_state, CPU_DEAD);
  353. __smp_mb();
  354. do {
  355. __asm__ __volatile__("idle 0\n\t");
  356. addr = iocsr_read64(LOONGARCH_IOCSR_MBUF0);
  357. } while (addr == 0);
  358. local_irq_disable();
  359. init_fn = (void *)TO_CACHE(addr);
  360. iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_CLEAR);
  361. init_fn();
  362. BUG();
  363. }
  364. #ifdef CONFIG_HIBERNATION
  365. static void __noreturn poll_play_dead(void)
  366. {
  367. register uint64_t addr;
  368. register void (*init_fn)(void);
  369. idle_task_exit();
  370. __this_cpu_write(cpu_state, CPU_DEAD);
  371. __smp_mb();
  372. do {
  373. __asm__ __volatile__("nop\n\t");
  374. addr = iocsr_read64(LOONGARCH_IOCSR_MBUF0);
  375. } while (addr == 0);
  376. init_fn = (void *)TO_CACHE(addr);
  377. iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_CLEAR);
  378. init_fn();
  379. BUG();
  380. }
  381. #endif
  382. static void (*play_dead)(void) = idle_play_dead;
  383. void __noreturn arch_cpu_idle_dead(void)
  384. {
  385. play_dead();
  386. BUG(); /* play_dead() doesn't return */
  387. }
  388. #ifdef CONFIG_HIBERNATION
  389. int hibernate_resume_nonboot_cpu_disable(void)
  390. {
  391. int ret;
  392. play_dead = poll_play_dead;
  393. ret = suspend_disable_secondary_cpus();
  394. play_dead = idle_play_dead;
  395. return ret;
  396. }
  397. #endif
  398. #endif
  399. /*
  400. * Power management
  401. */
  402. #ifdef CONFIG_PM
  403. static int loongson_ipi_suspend(void)
  404. {
  405. return 0;
  406. }
  407. static void loongson_ipi_resume(void)
  408. {
  409. iocsr_write32(0xffffffff, LOONGARCH_IOCSR_IPI_EN);
  410. }
  411. static struct syscore_ops loongson_ipi_syscore_ops = {
  412. .resume = loongson_ipi_resume,
  413. .suspend = loongson_ipi_suspend,
  414. };
  415. /*
  416. * Enable boot cpu ipi before enabling nonboot cpus
  417. * during syscore_resume.
  418. */
  419. static int __init ipi_pm_init(void)
  420. {
  421. register_syscore_ops(&loongson_ipi_syscore_ops);
  422. return 0;
  423. }
  424. core_initcall(ipi_pm_init);
  425. #endif
  426. /* Preload SMP state for boot cpu */
  427. void __init smp_prepare_boot_cpu(void)
  428. {
  429. unsigned int cpu, node, rr_node;
  430. set_cpu_possible(0, true);
  431. set_cpu_online(0, true);
  432. set_my_cpu_offset(per_cpu_offset(0));
  433. numa_add_cpu(0);
  434. rr_node = first_node(node_online_map);
  435. for_each_possible_cpu(cpu) {
  436. node = early_cpu_to_node(cpu);
  437. /*
  438. * The mapping between present cpus and nodes has been
  439. * built during MADT and SRAT parsing.
  440. *
  441. * If possible cpus = present cpus here, early_cpu_to_node
  442. * will return valid node.
  443. *
  444. * If possible cpus > present cpus here (e.g. some possible
  445. * cpus will be added by cpu-hotplug later), for possible but
  446. * not present cpus, early_cpu_to_node will return NUMA_NO_NODE,
  447. * and we just map them to online nodes in round-robin way.
  448. * Once hotplugged, new correct mapping will be built for them.
  449. */
  450. if (node != NUMA_NO_NODE)
  451. set_cpu_numa_node(cpu, node);
  452. else {
  453. set_cpu_numa_node(cpu, rr_node);
  454. rr_node = next_node_in(rr_node, node_online_map);
  455. }
  456. }
  457. pv_spinlock_init();
  458. }
  459. /* called from main before smp_init() */
  460. void __init smp_prepare_cpus(unsigned int max_cpus)
  461. {
  462. init_new_context(current, &init_mm);
  463. current_thread_info()->cpu = 0;
  464. loongson_prepare_cpus(max_cpus);
  465. set_cpu_sibling_map(0);
  466. set_cpu_core_map(0);
  467. calculate_cpu_foreign_map();
  468. #ifndef CONFIG_HOTPLUG_CPU
  469. init_cpu_present(cpu_possible_mask);
  470. #endif
  471. }
  472. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  473. {
  474. loongson_boot_secondary(cpu, tidle);
  475. /* Wait for CPU to start and be ready to sync counters */
  476. if (!wait_for_completion_timeout(&cpu_starting,
  477. msecs_to_jiffies(5000))) {
  478. pr_crit("CPU%u: failed to start\n", cpu);
  479. return -EIO;
  480. }
  481. /* Wait for CPU to finish startup & mark itself online before return */
  482. wait_for_completion(&cpu_running);
  483. return 0;
  484. }
  485. /*
  486. * First C code run on the secondary CPUs after being started up by
  487. * the master.
  488. */
  489. asmlinkage void start_secondary(void)
  490. {
  491. unsigned int cpu;
  492. sync_counter();
  493. cpu = raw_smp_processor_id();
  494. set_my_cpu_offset(per_cpu_offset(cpu));
  495. cpu_probe();
  496. constant_clockevent_init();
  497. loongson_init_secondary();
  498. set_cpu_sibling_map(cpu);
  499. set_cpu_core_map(cpu);
  500. notify_cpu_starting(cpu);
  501. /* Notify boot CPU that we're starting */
  502. complete(&cpu_starting);
  503. /* The CPU is running, now mark it online */
  504. set_cpu_online(cpu, true);
  505. calculate_cpu_foreign_map();
  506. /*
  507. * Notify boot CPU that we're up & online and it can safely return
  508. * from __cpu_up()
  509. */
  510. complete(&cpu_running);
  511. /*
  512. * irq will be enabled in loongson_smp_finish(), enabling it too
  513. * early is dangerous.
  514. */
  515. WARN_ON_ONCE(!irqs_disabled());
  516. loongson_smp_finish();
  517. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  518. }
  519. void __init smp_cpus_done(unsigned int max_cpus)
  520. {
  521. }
  522. static void stop_this_cpu(void *dummy)
  523. {
  524. set_cpu_online(smp_processor_id(), false);
  525. calculate_cpu_foreign_map();
  526. local_irq_disable();
  527. while (true);
  528. }
  529. void smp_send_stop(void)
  530. {
  531. smp_call_function(stop_this_cpu, NULL, 0);
  532. }
  533. #ifdef CONFIG_PROFILING
  534. int setup_profiling_timer(unsigned int multiplier)
  535. {
  536. return 0;
  537. }
  538. #endif
  539. static void flush_tlb_all_ipi(void *info)
  540. {
  541. local_flush_tlb_all();
  542. }
  543. void flush_tlb_all(void)
  544. {
  545. on_each_cpu(flush_tlb_all_ipi, NULL, 1);
  546. }
  547. static void flush_tlb_mm_ipi(void *mm)
  548. {
  549. local_flush_tlb_mm((struct mm_struct *)mm);
  550. }
  551. void flush_tlb_mm(struct mm_struct *mm)
  552. {
  553. if (atomic_read(&mm->mm_users) == 0)
  554. return; /* happens as a result of exit_mmap() */
  555. preempt_disable();
  556. if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
  557. on_each_cpu_mask(mm_cpumask(mm), flush_tlb_mm_ipi, mm, 1);
  558. } else {
  559. unsigned int cpu;
  560. for_each_online_cpu(cpu) {
  561. if (cpu != smp_processor_id() && cpu_context(cpu, mm))
  562. cpu_context(cpu, mm) = 0;
  563. }
  564. local_flush_tlb_mm(mm);
  565. }
  566. preempt_enable();
  567. }
  568. struct flush_tlb_data {
  569. struct vm_area_struct *vma;
  570. unsigned long addr1;
  571. unsigned long addr2;
  572. };
  573. static void flush_tlb_range_ipi(void *info)
  574. {
  575. struct flush_tlb_data *fd = info;
  576. local_flush_tlb_range(fd->vma, fd->addr1, fd->addr2);
  577. }
  578. void flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  579. {
  580. struct mm_struct *mm = vma->vm_mm;
  581. preempt_disable();
  582. if ((atomic_read(&mm->mm_users) != 1) || (current->mm != mm)) {
  583. struct flush_tlb_data fd = {
  584. .vma = vma,
  585. .addr1 = start,
  586. .addr2 = end,
  587. };
  588. on_each_cpu_mask(mm_cpumask(mm), flush_tlb_range_ipi, &fd, 1);
  589. } else {
  590. unsigned int cpu;
  591. for_each_online_cpu(cpu) {
  592. if (cpu != smp_processor_id() && cpu_context(cpu, mm))
  593. cpu_context(cpu, mm) = 0;
  594. }
  595. local_flush_tlb_range(vma, start, end);
  596. }
  597. preempt_enable();
  598. }
  599. static void flush_tlb_kernel_range_ipi(void *info)
  600. {
  601. struct flush_tlb_data *fd = info;
  602. local_flush_tlb_kernel_range(fd->addr1, fd->addr2);
  603. }
  604. void flush_tlb_kernel_range(unsigned long start, unsigned long end)
  605. {
  606. struct flush_tlb_data fd = {
  607. .addr1 = start,
  608. .addr2 = end,
  609. };
  610. on_each_cpu(flush_tlb_kernel_range_ipi, &fd, 1);
  611. }
  612. static void flush_tlb_page_ipi(void *info)
  613. {
  614. struct flush_tlb_data *fd = info;
  615. local_flush_tlb_page(fd->vma, fd->addr1);
  616. }
  617. void flush_tlb_page(struct vm_area_struct *vma, unsigned long page)
  618. {
  619. preempt_disable();
  620. if ((atomic_read(&vma->vm_mm->mm_users) != 1) || (current->mm != vma->vm_mm)) {
  621. struct flush_tlb_data fd = {
  622. .vma = vma,
  623. .addr1 = page,
  624. };
  625. on_each_cpu_mask(mm_cpumask(vma->vm_mm), flush_tlb_page_ipi, &fd, 1);
  626. } else {
  627. unsigned int cpu;
  628. for_each_online_cpu(cpu) {
  629. if (cpu != smp_processor_id() && cpu_context(cpu, vma->vm_mm))
  630. cpu_context(cpu, vma->vm_mm) = 0;
  631. }
  632. local_flush_tlb_page(vma, page);
  633. }
  634. preempt_enable();
  635. }
  636. EXPORT_SYMBOL(flush_tlb_page);
  637. static void flush_tlb_one_ipi(void *info)
  638. {
  639. unsigned long vaddr = (unsigned long) info;
  640. local_flush_tlb_one(vaddr);
  641. }
  642. void flush_tlb_one(unsigned long vaddr)
  643. {
  644. on_each_cpu(flush_tlb_one_ipi, (void *)vaddr, 1);
  645. }
  646. EXPORT_SYMBOL(flush_tlb_one);