smpboot.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <asm/irq.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/tlbflush.h>
  35. #include <asm/sections.h>
  36. #include <asm/sbi.h>
  37. void *__cpu_up_stack_pointer[NR_CPUS];
  38. void *__cpu_up_task_pointer[NR_CPUS];
  39. void __init smp_prepare_boot_cpu(void)
  40. {
  41. }
  42. void __init smp_prepare_cpus(unsigned int max_cpus)
  43. {
  44. }
  45. void __init setup_smp(void)
  46. {
  47. struct device_node *dn = NULL;
  48. int hart, im_okay_therefore_i_am = 0;
  49. while ((dn = of_find_node_by_type(dn, "cpu"))) {
  50. hart = riscv_of_processor_hart(dn);
  51. if (hart >= 0) {
  52. set_cpu_possible(hart, true);
  53. set_cpu_present(hart, true);
  54. if (hart == smp_processor_id()) {
  55. BUG_ON(im_okay_therefore_i_am);
  56. im_okay_therefore_i_am = 1;
  57. }
  58. }
  59. }
  60. BUG_ON(!im_okay_therefore_i_am);
  61. }
  62. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  63. {
  64. tidle->thread_info.cpu = cpu;
  65. /*
  66. * On RISC-V systems, all harts boot on their own accord. Our _start
  67. * selects the first hart to boot the kernel and causes the remainder
  68. * of the harts to spin in a loop waiting for their stack pointer to be
  69. * setup by that main hart. Writing __cpu_up_stack_pointer signals to
  70. * the spinning harts that they can continue the boot process.
  71. */
  72. smp_mb();
  73. __cpu_up_stack_pointer[cpu] = task_stack_page(tidle) + THREAD_SIZE;
  74. __cpu_up_task_pointer[cpu] = tidle;
  75. while (!cpu_online(cpu))
  76. cpu_relax();
  77. return 0;
  78. }
  79. void __init smp_cpus_done(unsigned int max_cpus)
  80. {
  81. }
  82. /*
  83. * C entry point for a secondary processor.
  84. */
  85. asmlinkage void __init smp_callin(void)
  86. {
  87. struct mm_struct *mm = &init_mm;
  88. /* All kernel threads share the same mm context. */
  89. atomic_inc(&mm->mm_count);
  90. current->active_mm = mm;
  91. trap_init();
  92. notify_cpu_starting(smp_processor_id());
  93. set_cpu_online(smp_processor_id(), 1);
  94. local_flush_tlb_all();
  95. local_irq_enable();
  96. preempt_disable();
  97. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  98. }