smpboot.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * SMP initialisation and IPI support
  3. * Based on arch/arm64/kernel/smp.c
  4. *
  5. * Copyright (C) 2012 ARM Ltd.
  6. * Copyright (C) 2015 Regents of the University of California
  7. * Copyright (C) 2017 SiFive
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/mm.h>
  22. #include <linux/sched.h>
  23. #include <linux/kernel_stat.h>
  24. #include <linux/notifier.h>
  25. #include <linux/cpu.h>
  26. #include <linux/percpu.h>
  27. #include <linux/delay.h>
  28. #include <linux/err.h>
  29. #include <linux/irq.h>
  30. #include <linux/of.h>
  31. #include <linux/sched/task_stack.h>
  32. #include <linux/sched/mm.h>
  33. #include <asm/irq.h>
  34. #include <asm/mmu_context.h>
  35. #include <asm/tlbflush.h>
  36. #include <asm/sections.h>
  37. #include <asm/sbi.h>
  38. void *__cpu_up_stack_pointer[NR_CPUS];
  39. void *__cpu_up_task_pointer[NR_CPUS];
  40. void __init smp_prepare_boot_cpu(void)
  41. {
  42. }
  43. void __init smp_prepare_cpus(unsigned int max_cpus)
  44. {
  45. }
  46. void __init setup_smp(void)
  47. {
  48. struct device_node *dn = NULL;
  49. int hart;
  50. bool found_boot_cpu = false;
  51. int cpuid = 1;
  52. while ((dn = of_find_node_by_type(dn, "cpu"))) {
  53. hart = riscv_of_processor_hartid(dn);
  54. if (hart < 0)
  55. continue;
  56. if (hart == cpuid_to_hartid_map(0)) {
  57. BUG_ON(found_boot_cpu);
  58. found_boot_cpu = 1;
  59. continue;
  60. }
  61. cpuid_to_hartid_map(cpuid) = hart;
  62. set_cpu_possible(cpuid, true);
  63. set_cpu_present(cpuid, true);
  64. cpuid++;
  65. }
  66. BUG_ON(!found_boot_cpu);
  67. }
  68. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  69. {
  70. int hartid = cpuid_to_hartid_map(cpu);
  71. tidle->thread_info.cpu = cpu;
  72. /*
  73. * On RISC-V systems, all harts boot on their own accord. Our _start
  74. * selects the first hart to boot the kernel and causes the remainder
  75. * of the harts to spin in a loop waiting for their stack pointer to be
  76. * setup by that main hart. Writing __cpu_up_stack_pointer signals to
  77. * the spinning harts that they can continue the boot process.
  78. */
  79. smp_mb();
  80. WRITE_ONCE(__cpu_up_stack_pointer[hartid],
  81. task_stack_page(tidle) + THREAD_SIZE);
  82. WRITE_ONCE(__cpu_up_task_pointer[hartid], tidle);
  83. while (!cpu_online(cpu))
  84. cpu_relax();
  85. return 0;
  86. }
  87. void __init smp_cpus_done(unsigned int max_cpus)
  88. {
  89. }
  90. /*
  91. * C entry point for a secondary processor.
  92. */
  93. asmlinkage void __init smp_callin(void)
  94. {
  95. struct mm_struct *mm = &init_mm;
  96. /* All kernel threads share the same mm context. */
  97. mmgrab(mm);
  98. current->active_mm = mm;
  99. trap_init();
  100. notify_cpu_starting(smp_processor_id());
  101. set_cpu_online(smp_processor_id(), 1);
  102. /*
  103. * Remote TLB flushes are ignored while the CPU is offline, so emit
  104. * a local TLB flush right now just in case.
  105. */
  106. local_flush_tlb_all();
  107. /*
  108. * Disable preemption before enabling interrupts, so we don't try to
  109. * schedule a CPU that hasn't actually started yet.
  110. */
  111. preempt_disable();
  112. local_irq_enable();
  113. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  114. }