^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2012 ARM Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Will Deacon <will.deacon@arm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/psci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <uapi/linux/psci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <asm/psci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/smp_plat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * psci_smp assumes that the following is true about PSCI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * cpu_suspend Suspend the execution on a CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @state we don't currently describe affinity levels, so just pass 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * @entry_point the first instruction to be executed on return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * returns 0 success, < 0 on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * cpu_off Power down a CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * @state we don't currently describe affinity levels, so just pass 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * no return on successful call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * cpu_on Power up a CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * @cpuid cpuid of target CPU, as from MPIDR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * @entry_point the first instruction to be executed on return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * returns 0 success, < 0 on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * migrate Migrate the context to a different CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * @cpuid cpuid of target CPU, as from MPIDR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * returns 0 success, < 0 on failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) extern void secondary_startup(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static int psci_boot_secondary(unsigned int cpu, struct task_struct *idle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (psci_ops.cpu_on)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) return psci_ops.cpu_on(cpu_logical_map(cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) virt_to_idmap(&secondary_startup));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #ifdef CONFIG_HOTPLUG_CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static int psci_cpu_disable(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /* Fail early if we don't have CPU_OFF support */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (!psci_ops.cpu_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) /* Trusted OS will deny CPU_OFF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (psci_tos_resident_on(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static void psci_cpu_die(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) u32 state = PSCI_POWER_STATE_TYPE_POWER_DOWN <<
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) PSCI_0_2_POWER_STATE_TYPE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if (psci_ops.cpu_off)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) psci_ops.cpu_off(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /* We should never return */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) panic("psci: cpu %d failed to shutdown\n", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static int psci_cpu_kill(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int err, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!psci_ops.affinity_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * cpu_kill could race with cpu_die and we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * potentially end up declaring this cpu undead
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) * while it is dying. So, try again a few times.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) for (i = 0; i < 10; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) err = psci_ops.affinity_info(cpu_logical_map(cpu), 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (err == PSCI_0_2_AFFINITY_LEVEL_OFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) pr_info("CPU%d killed.\n", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) msleep(10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) pr_info("Retrying again to check for CPU kill\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) pr_warn("CPU%d may not have shut down cleanly (AFFINITY_INFO reports %d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) cpu, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) /* Make platform_cpu_kill() fail. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) bool psci_smp_available(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) /* is cpu_on available at least? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return (psci_ops.cpu_on != NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) EXPORT_SYMBOL(psci_smp_available);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) const struct smp_operations psci_smp_ops __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) .smp_boot_secondary = psci_boot_secondary,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #ifdef CONFIG_HOTPLUG_CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .cpu_disable = psci_cpu_disable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) .cpu_die = psci_cpu_die,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) .cpu_kill = psci_cpu_kill,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) };