xref: /openbmc/linux/arch/riscv/kernel/cpu-hotplug.c (revision d9565bf4)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2020 Western Digital Corporation or its affiliates.
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/mm.h>
8 #include <linux/sched.h>
9 #include <linux/err.h>
10 #include <linux/irq.h>
11 #include <linux/cpu.h>
12 #include <linux/sched/hotplug.h>
13 #include <asm/irq.h>
14 #include <asm/cpu_ops.h>
15 #include <asm/sbi.h>
16 
17 bool cpu_has_hotplug(unsigned int cpu)
18 {
19 	if (cpu_ops[cpu]->cpu_stop)
20 		return true;
21 
22 	return false;
23 }
24 
25 /*
26  * __cpu_disable runs on the processor to be shutdown.
27  */
28 int __cpu_disable(void)
29 {
30 	int ret = 0;
31 	unsigned int cpu = smp_processor_id();
32 
33 	if (!cpu_ops[cpu] || !cpu_ops[cpu]->cpu_stop)
34 		return -EOPNOTSUPP;
35 
36 	if (cpu_ops[cpu]->cpu_disable)
37 		ret = cpu_ops[cpu]->cpu_disable(cpu);
38 
39 	if (ret)
40 		return ret;
41 
42 	remove_cpu_topology(cpu);
43 	set_cpu_online(cpu, false);
44 	irq_migrate_all_off_this_cpu();
45 
46 	return ret;
47 }
48 
49 /*
50  * Called on the thread which is asking for a CPU to be shutdown.
51  */
52 void __cpu_die(unsigned int cpu)
53 {
54 	int ret = 0;
55 
56 	if (!cpu_wait_death(cpu, 5)) {
57 		pr_err("CPU %u: didn't die\n", cpu);
58 		return;
59 	}
60 	pr_notice("CPU%u: off\n", cpu);
61 
62 	/* Verify from the firmware if the cpu is really stopped*/
63 	if (cpu_ops[cpu]->cpu_is_stopped)
64 		ret = cpu_ops[cpu]->cpu_is_stopped(cpu);
65 	if (ret)
66 		pr_warn("CPU%d may not have stopped: %d\n", cpu, ret);
67 }
68 
69 /*
70  * Called from the idle thread for the CPU which has been shutdown.
71  */
72 void arch_cpu_idle_dead(void)
73 {
74 	idle_task_exit();
75 
76 	(void)cpu_report_death();
77 
78 	cpu_ops[smp_processor_id()]->cpu_stop();
79 	/* It should never reach here */
80 	BUG();
81 }
82