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