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> 15f40fe31cSPingfan Liu #include <asm/numa.h> 16*832f15f4SAnup Patel #include <asm/smp.h> 17f1e58583SAtish Patra 18f1e58583SAtish Patra bool cpu_has_hotplug(unsigned int cpu) 19f1e58583SAtish Patra { 20f1e58583SAtish Patra if (cpu_ops[cpu]->cpu_stop) 21f1e58583SAtish Patra return true; 22f1e58583SAtish Patra 23f1e58583SAtish Patra return false; 24f1e58583SAtish Patra } 25f1e58583SAtish Patra 26f1e58583SAtish Patra /* 27f1e58583SAtish Patra * __cpu_disable runs on the processor to be shutdown. 28f1e58583SAtish Patra */ 29f1e58583SAtish Patra int __cpu_disable(void) 30f1e58583SAtish Patra { 31f1e58583SAtish Patra int ret = 0; 32f1e58583SAtish Patra unsigned int cpu = smp_processor_id(); 33f1e58583SAtish Patra 34f1e58583SAtish Patra if (!cpu_ops[cpu] || !cpu_ops[cpu]->cpu_stop) 35f1e58583SAtish Patra return -EOPNOTSUPP; 36f1e58583SAtish Patra 37f1e58583SAtish Patra if (cpu_ops[cpu]->cpu_disable) 38f1e58583SAtish Patra ret = cpu_ops[cpu]->cpu_disable(cpu); 39f1e58583SAtish Patra 40f1e58583SAtish Patra if (ret) 41f1e58583SAtish Patra return ret; 42f1e58583SAtish Patra 43f1e58583SAtish Patra remove_cpu_topology(cpu); 44f40fe31cSPingfan Liu numa_remove_cpu(cpu); 45f1e58583SAtish Patra set_cpu_online(cpu, false); 46*832f15f4SAnup Patel riscv_ipi_disable(); 47f1e58583SAtish Patra irq_migrate_all_off_this_cpu(); 48f1e58583SAtish Patra 49f1e58583SAtish Patra return ret; 50f1e58583SAtish Patra } 51f1e58583SAtish Patra 52f1e58583SAtish Patra /* 53f1e58583SAtish Patra * Called on the thread which is asking for a CPU to be shutdown. 54f1e58583SAtish Patra */ 55f1e58583SAtish Patra void __cpu_die(unsigned int cpu) 56f1e58583SAtish Patra { 57f1e58583SAtish Patra int ret = 0; 58f1e58583SAtish Patra 59f1e58583SAtish Patra if (!cpu_wait_death(cpu, 5)) { 60f1e58583SAtish Patra pr_err("CPU %u: didn't die\n", cpu); 61f1e58583SAtish Patra return; 62f1e58583SAtish Patra } 63f1e58583SAtish Patra pr_notice("CPU%u: off\n", cpu); 64f1e58583SAtish Patra 65f1e58583SAtish Patra /* Verify from the firmware if the cpu is really stopped*/ 66f1e58583SAtish Patra if (cpu_ops[cpu]->cpu_is_stopped) 67f1e58583SAtish Patra ret = cpu_ops[cpu]->cpu_is_stopped(cpu); 68f1e58583SAtish Patra if (ret) 69f1e58583SAtish Patra pr_warn("CPU%d may not have stopped: %d\n", cpu, ret); 70f1e58583SAtish Patra } 71f1e58583SAtish Patra 72f1e58583SAtish Patra /* 73f1e58583SAtish Patra * Called from the idle thread for the CPU which has been shutdown. 74f1e58583SAtish Patra */ 757f3de1adSJisheng Zhang void arch_cpu_idle_dead(void) 76f1e58583SAtish Patra { 77f1e58583SAtish Patra idle_task_exit(); 78f1e58583SAtish Patra 79f1e58583SAtish Patra (void)cpu_report_death(); 80f1e58583SAtish Patra 81f1e58583SAtish Patra cpu_ops[smp_processor_id()]->cpu_stop(); 82f1e58583SAtish Patra /* It should never reach here */ 83f1e58583SAtish Patra BUG(); 84f1e58583SAtish Patra } 85