smpboot.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SMP initialisation and IPI support
  4. * Based on arch/arm64/kernel/smp.c
  5. *
  6. * Copyright (C) 2012 ARM Ltd.
  7. * Copyright (C) 2015 Regents of the University of California
  8. * Copyright (C) 2017 SiFive
  9. */
  10. #include <linux/arch_topology.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mm.h>
  15. #include <linux/sched.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/notifier.h>
  18. #include <linux/cpu.h>
  19. #include <linux/percpu.h>
  20. #include <linux/delay.h>
  21. #include <linux/err.h>
  22. #include <linux/irq.h>
  23. #include <linux/of.h>
  24. #include <linux/sched/task_stack.h>
  25. #include <linux/sched/mm.h>
  26. #include <asm/cpu_ops.h>
  27. #include <asm/irq.h>
  28. #include <asm/mmu_context.h>
  29. #include <asm/tlbflush.h>
  30. #include <asm/sections.h>
  31. #include <asm/sbi.h>
  32. #include <asm/smp.h>
  33. #include "head.h"
  34. static DECLARE_COMPLETION(cpu_running);
  35. static struct cpumask cpu_delay_available_mask = { CPU_BITS_NONE };
  36. void __init smp_prepare_boot_cpu(void)
  37. {
  38. init_cpu_topology();
  39. }
  40. void __init smp_prepare_cpus(unsigned int max_cpus)
  41. {
  42. int cpuid;
  43. int ret;
  44. /* This covers non-smp usecase mandated by "nosmp" option */
  45. if (max_cpus == 0)
  46. return;
  47. for_each_possible_cpu(cpuid) {
  48. if (cpuid == smp_processor_id())
  49. continue;
  50. if (cpu_ops[cpuid]->cpu_prepare) {
  51. ret = cpu_ops[cpuid]->cpu_prepare(cpuid);
  52. if (ret)
  53. continue;
  54. }
  55. set_cpu_present(cpuid, true);
  56. }
  57. }
  58. void __init setup_smp(void)
  59. {
  60. struct device_node *dn;
  61. int hart;
  62. bool found_boot_cpu = false;
  63. int cpuid = 1;
  64. cpu_set_ops(0);
  65. for_each_of_cpu_node(dn) {
  66. hart = riscv_of_processor_hartid(dn);
  67. if (hart < 0)
  68. continue;
  69. if (hart == cpuid_to_hartid_map(0)) {
  70. BUG_ON(found_boot_cpu);
  71. found_boot_cpu = 1;
  72. continue;
  73. }
  74. if (cpuid >= NR_CPUS) {
  75. pr_warn("Invalid cpuid [%d] for hartid [%d]\n",
  76. cpuid, hart);
  77. break;
  78. }
  79. if (!of_device_is_available(dn))
  80. pr_info("CPU with hartid=%d is not available\n", hart);
  81. else
  82. cpumask_set_cpu(cpuid, &cpu_delay_available_mask);
  83. cpuid_to_hartid_map(cpuid) = hart;
  84. cpuid++;
  85. }
  86. BUG_ON(!found_boot_cpu);
  87. if (cpuid > nr_cpu_ids)
  88. pr_warn("Total number of cpus [%d] is greater than nr_cpus option value [%d]\n",
  89. cpuid, nr_cpu_ids);
  90. for (cpuid = 1; cpuid < nr_cpu_ids; cpuid++) {
  91. if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID) {
  92. cpu_set_ops(cpuid);
  93. set_cpu_possible(cpuid, true);
  94. }
  95. }
  96. }
  97. static int start_secondary_cpu(int cpu, struct task_struct *tidle)
  98. {
  99. if (cpu_ops[cpu]->cpu_start)
  100. return cpu_ops[cpu]->cpu_start(cpu, tidle);
  101. return -EOPNOTSUPP;
  102. }
  103. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  104. {
  105. int ret = 0;
  106. tidle->thread_info.cpu = cpu;
  107. if (!cpumask_test_cpu(cpu, &cpu_delay_available_mask)) {
  108. cpumask_set_cpu(cpu, &cpu_delay_available_mask);
  109. return -EIO;
  110. }
  111. ret = start_secondary_cpu(cpu, tidle);
  112. if (!ret) {
  113. wait_for_completion_timeout(&cpu_running,
  114. msecs_to_jiffies(1000));
  115. if (!cpu_online(cpu)) {
  116. pr_crit("CPU%u: failed to come online\n", cpu);
  117. ret = -EIO;
  118. }
  119. } else {
  120. pr_crit("CPU%u: failed to start\n", cpu);
  121. }
  122. return ret;
  123. }
  124. void __init smp_cpus_done(unsigned int max_cpus)
  125. {
  126. }
  127. /*
  128. * C entry point for a secondary processor.
  129. */
  130. asmlinkage __visible void smp_callin(void)
  131. {
  132. struct mm_struct *mm = &init_mm;
  133. unsigned int curr_cpuid = smp_processor_id();
  134. riscv_clear_ipi();
  135. /* All kernel threads share the same mm context. */
  136. mmgrab(mm);
  137. current->active_mm = mm;
  138. notify_cpu_starting(curr_cpuid);
  139. update_siblings_masks(curr_cpuid);
  140. set_cpu_online(curr_cpuid, 1);
  141. /*
  142. * Remote TLB flushes are ignored while the CPU is offline, so emit
  143. * a local TLB flush right now just in case.
  144. */
  145. local_flush_tlb_all();
  146. complete(&cpu_running);
  147. /*
  148. * Disable preemption before enabling interrupts, so we don't try to
  149. * schedule a CPU that hasn't actually started yet.
  150. */
  151. local_irq_enable();
  152. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  153. }