^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) * PSCI CPU idle driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2019 ARM Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Lorenzo Pieralisi <lorenzo.pieralisi@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) #define pr_fmt(fmt) "CPUidle PSCI: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/cpuhotplug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/cpu_cooling.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/cpuidle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/cpumask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/cpu_pm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/psci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/pm_domain.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <asm/cpuidle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <trace/hooks/cpuidle_psci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include "cpuidle-psci.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include "dt_idle_states.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) struct psci_cpuidle_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) u32 *psci_states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static DEFINE_PER_CPU_READ_MOSTLY(struct psci_cpuidle_data, psci_cpuidle_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) static DEFINE_PER_CPU(u32, domain_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static bool psci_cpuidle_use_cpuhp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) void psci_set_domain_state(u32 state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) __this_cpu_write(domain_state, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) static inline u32 psci_get_domain_state(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return __this_cpu_read(domain_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static inline int psci_enter_state(int idx, u32 state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return CPU_PM_CPU_IDLE_ENTER_PARAM(psci_cpu_suspend_enter, idx, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static int __psci_enter_domain_idle_state(struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct cpuidle_driver *drv, int idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) bool s2idle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct psci_cpuidle_data *data = this_cpu_ptr(&psci_cpuidle_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) u32 *states = data->psci_states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct device *pd_dev = data->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) u32 state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) ret = cpu_pm_enter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) trace_android_vh_cpuidle_psci_enter(dev, s2idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) /* Do runtime PM to manage a hierarchical CPU toplogy. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) rcu_irq_enter_irqson();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (s2idle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) dev_pm_genpd_suspend(pd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) pm_runtime_put_sync_suspend(pd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) rcu_irq_exit_irqson();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) state = psci_get_domain_state();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (!state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) state = states[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) ret = psci_cpu_suspend_enter(state) ? -1 : idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) rcu_irq_enter_irqson();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (s2idle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) dev_pm_genpd_resume(pd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) pm_runtime_get_sync(pd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) rcu_irq_exit_irqson();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) trace_android_vh_cpuidle_psci_exit(dev, s2idle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) cpu_pm_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* Clear the domain state to start fresh when back from idle. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) psci_set_domain_state(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static int psci_enter_domain_idle_state(struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct cpuidle_driver *drv, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) return __psci_enter_domain_idle_state(dev, drv, idx, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static int psci_enter_s2idle_domain_idle_state(struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return __psci_enter_domain_idle_state(dev, drv, idx, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) static int psci_idle_cpuhp_up(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (pd_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) pm_runtime_get_sync(pd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static int psci_idle_cpuhp_down(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct device *pd_dev = __this_cpu_read(psci_cpuidle_data.dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (pd_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) pm_runtime_put_sync(pd_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /* Clear domain state to start fresh at next online. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) psci_set_domain_state(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static void psci_idle_init_cpuhp(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (!psci_cpuidle_use_cpuhp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) err = cpuhp_setup_state_nocalls(CPUHP_AP_CPU_PM_STARTING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) "cpuidle/psci:online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) psci_idle_cpuhp_up,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) psci_idle_cpuhp_down);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) pr_warn("Failed %d while setup cpuhp state\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static int psci_enter_idle_state(struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) struct cpuidle_driver *drv, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) u32 *state = __this_cpu_read(psci_cpuidle_data.psci_states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return psci_enter_state(idx, state[idx]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) static const struct of_device_id psci_idle_state_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) { .compatible = "arm,idle-state",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) .data = psci_enter_idle_state },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) { },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int psci_dt_parse_state_node(struct device_node *np, u32 *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) int err = of_property_read_u32(np, "arm,psci-suspend-param", state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) pr_warn("%pOF missing arm,psci-suspend-param property\n", np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (!psci_power_state_is_valid(*state)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) pr_warn("Invalid PSCI power state %#x\n", *state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) static int psci_dt_cpu_init_topology(struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct psci_cpuidle_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) unsigned int state_count, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) /* Currently limit the hierarchical topology to be used in OSI mode. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (!psci_has_osi_support())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) data->dev = psci_dt_attach_cpu(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) if (IS_ERR_OR_NULL(data->dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return PTR_ERR_OR_ZERO(data->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) * Using the deepest state for the CPU to trigger a potential selection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) * of a shared state for the domain, assumes the domain states are all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * deeper states.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) drv->states[state_count - 1].enter = psci_enter_domain_idle_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) drv->states[state_count - 1].enter_s2idle = psci_enter_s2idle_domain_idle_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) psci_cpuidle_use_cpuhp = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static int psci_dt_cpu_init_idle(struct device *dev, struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) struct device_node *cpu_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) unsigned int state_count, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) int i, ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) u32 *psci_states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct device_node *state_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) state_count++; /* Add WFI state too */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) psci_states = devm_kcalloc(dev, state_count, sizeof(*psci_states),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (!psci_states)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) for (i = 1; i < state_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) state_node = of_get_cpu_state_node(cpu_node, i - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!state_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ret = psci_dt_parse_state_node(state_node, &psci_states[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) of_node_put(state_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) pr_debug("psci-power-state %#x index %d\n", psci_states[i], i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) if (i != state_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) /* Initialize optional data, used for the hierarchical topology. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) ret = psci_dt_cpu_init_topology(drv, data, state_count, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /* Idle states parsed correctly, store them in the per-cpu struct. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) data->psci_states = psci_states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static int psci_cpu_init_idle(struct device *dev, struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) unsigned int cpu, unsigned int state_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct device_node *cpu_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * If the PSCI cpu_suspend function hook has not been initialized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * idle states must not be enabled, so bail out
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) if (!psci_ops.cpu_suspend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) cpu_node = of_cpu_device_node_get(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (!cpu_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) ret = psci_dt_cpu_init_idle(dev, drv, cpu_node, state_count, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) of_node_put(cpu_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static void psci_cpu_deinit_idle(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) struct psci_cpuidle_data *data = per_cpu_ptr(&psci_cpuidle_data, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) psci_dt_detach_cpu(data->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) psci_cpuidle_use_cpuhp = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) static int psci_idle_init_cpu(struct device *dev, int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) struct cpuidle_driver *drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct device_node *cpu_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) const char *enable_method;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) cpu_node = of_cpu_device_node_get(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (!cpu_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * Check whether the enable-method for the cpu is PSCI, fail
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * if it is not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) enable_method = of_get_property(cpu_node, "enable-method", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (!enable_method || (strcmp(enable_method, "psci")))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) of_node_put(cpu_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (!drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) drv->name = "psci_idle";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) drv->owner = THIS_MODULE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) drv->cpumask = (struct cpumask *)cpumask_of(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) * PSCI idle states relies on architectural WFI to be represented as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * state index 0.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) drv->states[0].enter = psci_enter_idle_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) drv->states[0].exit_latency = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) drv->states[0].target_residency = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) drv->states[0].power_usage = UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) strcpy(drv->states[0].name, "WFI");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) strcpy(drv->states[0].desc, "ARM WFI");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * If no DT idle states are detected (ret == 0) let the driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) * initialization fail accordingly since there is no reason to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) * initialize the idle driver if only wfi is supported, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * default archictectural back-end already executes wfi
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * on idle entry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) ret = dt_init_idle_driver(drv, psci_idle_state_match, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) if (ret <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return ret ? : -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) * Initialize PSCI idle states.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) ret = psci_cpu_init_idle(dev, drv, cpu, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) pr_err("CPU %d failed to PSCI idle\n", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) ret = cpuidle_register(drv, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) goto deinit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) cpuidle_cooling_register(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) deinit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) psci_cpu_deinit_idle(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * psci_idle_probe - Initializes PSCI cpuidle driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * Initializes PSCI cpuidle driver for all CPUs, if any CPU fails
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * to register cpuidle driver then rollback to cancel all CPUs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) * registration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static int psci_cpuidle_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) int cpu, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) struct cpuidle_driver *drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) struct cpuidle_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) ret = psci_idle_init_cpu(&pdev->dev, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) psci_idle_init_cpuhp();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) out_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) while (--cpu >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) dev = per_cpu(cpuidle_devices, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) drv = cpuidle_get_cpu_driver(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) cpuidle_unregister(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) psci_cpu_deinit_idle(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) static struct platform_driver psci_cpuidle_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) .probe = psci_cpuidle_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) .name = "psci-cpuidle",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) static int __init psci_idle_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) ret = platform_driver_register(&psci_cpuidle_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) pdev = platform_device_register_simple("psci-cpuidle", -1, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) if (IS_ERR(pdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) platform_driver_unregister(&psci_cpuidle_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return PTR_ERR(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) device_initcall(psci_idle_init);