smpboot.c 3.2 KB
Newer Older
P
Palmer Dabbelt 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * SMP initialisation and IPI support
 * Based on arch/arm64/kernel/smp.c
 *
 * Copyright (C) 2012 ARM Ltd.
 * Copyright (C) 2015 Regents of the University of California
 * Copyright (C) 2017 SiFive
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/kernel_stat.h>
#include <linux/notifier.h>
#include <linux/cpu.h>
#include <linux/percpu.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/irq.h>
#include <linux/of.h>
#include <linux/sched/task_stack.h>
P
Palmer Dabbelt 已提交
33
#include <linux/sched/mm.h>
P
Palmer Dabbelt 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <asm/irq.h>
#include <asm/mmu_context.h>
#include <asm/tlbflush.h>
#include <asm/sections.h>
#include <asm/sbi.h>

void *__cpu_up_stack_pointer[NR_CPUS];
void *__cpu_up_task_pointer[NR_CPUS];

void __init smp_prepare_boot_cpu(void)
{
}

void __init smp_prepare_cpus(unsigned int max_cpus)
{
}

void __init setup_smp(void)
{
53
	struct device_node *dn;
54 55
	int hart;
	bool found_boot_cpu = false;
56
	int cpuid = 1;
P
Palmer Dabbelt 已提交
57

58
	for_each_of_cpu_node(dn) {
59
		hart = riscv_of_processor_hartid(dn);
60
		if (hart < 0)
61 62 63 64 65 66
			continue;

		if (hart == cpuid_to_hartid_map(0)) {
			BUG_ON(found_boot_cpu);
			found_boot_cpu = 1;
			continue;
P
Palmer Dabbelt 已提交
67
		}
68 69 70 71 72

		cpuid_to_hartid_map(cpuid) = hart;
		set_cpu_possible(cpuid, true);
		set_cpu_present(cpuid, true);
		cpuid++;
P
Palmer Dabbelt 已提交
73 74
	}

75
	BUG_ON(!found_boot_cpu);
P
Palmer Dabbelt 已提交
76 77 78 79
}

int __cpu_up(unsigned int cpu, struct task_struct *tidle)
{
80
	int hartid = cpuid_to_hartid_map(cpu);
P
Palmer Dabbelt 已提交
81 82 83 84 85 86 87 88 89 90
	tidle->thread_info.cpu = cpu;

	/*
	 * On RISC-V systems, all harts boot on their own accord.  Our _start
	 * selects the first hart to boot the kernel and causes the remainder
	 * of the harts to spin in a loop waiting for their stack pointer to be
	 * setup by that main hart.  Writing __cpu_up_stack_pointer signals to
	 * the spinning harts that they can continue the boot process.
	 */
	smp_mb();
91
	WRITE_ONCE(__cpu_up_stack_pointer[hartid],
92
		  task_stack_page(tidle) + THREAD_SIZE);
93
	WRITE_ONCE(__cpu_up_task_pointer[hartid], tidle);
P
Palmer Dabbelt 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

	while (!cpu_online(cpu))
		cpu_relax();

	return 0;
}

void __init smp_cpus_done(unsigned int max_cpus)
{
}

/*
 * C entry point for a secondary processor.
 */
asmlinkage void __init smp_callin(void)
{
	struct mm_struct *mm = &init_mm;

	/* All kernel threads share the same mm context.  */
P
Palmer Dabbelt 已提交
113
	mmgrab(mm);
P
Palmer Dabbelt 已提交
114 115 116 117 118
	current->active_mm = mm;

	trap_init();
	notify_cpu_starting(smp_processor_id());
	set_cpu_online(smp_processor_id(), 1);
119 120 121 122
	/*
	 * Remote TLB flushes are ignored while the CPU is offline, so emit
	 * a local TLB flush right now just in case.
	 */
P
Palmer Dabbelt 已提交
123
	local_flush_tlb_all();
124 125 126 127
	/*
	 * Disable preemption before enabling interrupts, so we don't try to
	 * schedule a CPU that hasn't actually started yet.
	 */
P
Palmer Dabbelt 已提交
128
	preempt_disable();
129
	local_irq_enable();
P
Palmer Dabbelt 已提交
130 131
	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}