smpboot.c 3.85 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10
/*
 * 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
 */

11
#include <linux/arch_topology.h>
12 13 14 15 16 17 18 19 20 21 22 23 24 25
#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>
Palmer Dabbelt's avatar
Palmer Dabbelt committed
26
#include <linux/sched/mm.h>
27
#include <asm/cpu_ops.h>
28 29
#include <asm/irq.h>
#include <asm/mmu_context.h>
30
#include <asm/numa.h>
31 32 33
#include <asm/tlbflush.h>
#include <asm/sections.h>
#include <asm/sbi.h>
34
#include <asm/smp.h>
35

36 37
#include "head.h"

38
static DECLARE_COMPLETION(cpu_running);
39 40 41

void __init smp_prepare_boot_cpu(void)
{
42
	init_cpu_topology();
43 44 45 46
}

void __init smp_prepare_cpus(unsigned int max_cpus)
{
47
	int cpuid;
48
	int ret;
49 50 51
	unsigned int curr_cpuid;

	curr_cpuid = smp_processor_id();
52
	store_cpu_topology(curr_cpuid);
53 54
	numa_store_cpu_info(curr_cpuid);
	numa_add_cpu(curr_cpuid);
55 56 57 58 59 60

	/* This covers non-smp usecase mandated by "nosmp" option */
	if (max_cpus == 0)
		return;

	for_each_possible_cpu(cpuid) {
61
		if (cpuid == curr_cpuid)
62
			continue;
63 64 65 66 67
		if (cpu_ops[cpuid]->cpu_prepare) {
			ret = cpu_ops[cpuid]->cpu_prepare(cpuid);
			if (ret)
				continue;
		}
68
		set_cpu_present(cpuid, true);
69
		numa_store_cpu_info(cpuid);
70
	}
71 72 73 74
}

void __init setup_smp(void)
{
75
	struct device_node *dn;
76
	unsigned long hart;
77
	bool found_boot_cpu = false;
78
	int cpuid = 1;
79
	int rc;
80

81 82
	cpu_set_ops(0);

83
	for_each_of_cpu_node(dn) {
84 85
		rc = riscv_of_processor_hartid(dn, &hart);
		if (rc < 0)
86 87 88 89 90
			continue;

		if (hart == cpuid_to_hartid_map(0)) {
			BUG_ON(found_boot_cpu);
			found_boot_cpu = 1;
91
			early_map_cpu_to_node(0, of_node_to_nid(dn));
92
			continue;
93
		}
94
		if (cpuid >= NR_CPUS) {
95
			pr_warn("Invalid cpuid [%d] for hartid [%lu]\n",
96
				cpuid, hart);
97
			continue;
98
		}
99 100

		cpuid_to_hartid_map(cpuid) = hart;
101
		early_map_cpu_to_node(cpuid, of_node_to_nid(dn));
102
		cpuid++;
103 104
	}

105
	BUG_ON(!found_boot_cpu);
106 107 108 109 110 111

	if (cpuid > nr_cpu_ids)
		pr_warn("Total number of cpus [%d] is greater than nr_cpus option value [%d]\n",
			cpuid, nr_cpu_ids);

	for (cpuid = 1; cpuid < nr_cpu_ids; cpuid++) {
112 113
		if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID) {
			cpu_set_ops(cpuid);
114
			set_cpu_possible(cpuid, true);
115
		}
116
	}
117 118
}

119
static int start_secondary_cpu(int cpu, struct task_struct *tidle)
120 121 122 123 124 125 126
{
	if (cpu_ops[cpu]->cpu_start)
		return cpu_ops[cpu]->cpu_start(cpu, tidle);

	return -EOPNOTSUPP;
}

127 128
int __cpu_up(unsigned int cpu, struct task_struct *tidle)
{
129
	int ret = 0;
130 131
	tidle->thread_info.cpu = cpu;

132 133 134
	ret = start_secondary_cpu(cpu, tidle);
	if (!ret) {
		wait_for_completion_timeout(&cpu_running,
135
					    msecs_to_jiffies(1000));
136

137 138 139 140 141 142
		if (!cpu_online(cpu)) {
			pr_crit("CPU%u: failed to come online\n", cpu);
			ret = -EIO;
		}
	} else {
		pr_crit("CPU%u: failed to start\n", cpu);
143 144 145
	}

	return ret;
146 147 148 149 150 151 152 153 154
}

void __init smp_cpus_done(unsigned int max_cpus)
{
}

/*
 * C entry point for a secondary processor.
 */
155
asmlinkage __visible void smp_callin(void)
156 157
{
	struct mm_struct *mm = &init_mm;
158
	unsigned int curr_cpuid = smp_processor_id();
159

160
	riscv_clear_ipi();
161

162
	/* All kernel threads share the same mm context.  */
Palmer Dabbelt's avatar
Palmer Dabbelt committed
163
	mmgrab(mm);
164 165
	current->active_mm = mm;

166
	store_cpu_topology(curr_cpuid);
167
	notify_cpu_starting(curr_cpuid);
168
	numa_add_cpu(curr_cpuid);
169
	set_cpu_online(curr_cpuid, 1);
170

171 172 173 174
	/*
	 * Remote TLB flushes are ignored while the CPU is offline, so emit
	 * a local TLB flush right now just in case.
	 */
175
	local_flush_tlb_all();
176
	complete(&cpu_running);
177 178 179 180 181
	/*
	 * Disable preemption before enabling interrupts, so we don't try to
	 * schedule a CPU that hasn't actually started yet.
	 */
	local_irq_enable();
182 183
	cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
}