^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * PM domains for CPUs via genpd - managed by cpuidle-psci.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2019 Linaro Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Ulf Hansson <ulf.hansson@linaro.org>
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #define pr_fmt(fmt) "CPUidle PSCI: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/pm_domain.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/pm_runtime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/psci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "cpuidle-psci.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct psci_pd_provider {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct list_head link;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct device_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static LIST_HEAD(psci_pd_providers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static bool psci_pd_allow_domain_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int psci_pd_power_off(struct generic_pm_domain *pd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct genpd_power_state *state = &pd->states[pd->state_idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) u32 *pd_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (!state->data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (!psci_pd_allow_domain_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /* OSI mode is enabled, set the corresponding domain state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) pd_state = state->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) psci_set_domain_state(*pd_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static int psci_pd_parse_state_nodes(struct genpd_power_state *states,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) int state_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) u32 psci_state, *psci_state_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) for (i = 0; i < state_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) ret = psci_dt_parse_state_node(to_of_node(states[i].fwnode),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) &psci_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) goto free_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) psci_state_buf = kmalloc(sizeof(u32), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) if (!psci_state_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) goto free_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) *psci_state_buf = psci_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) states[i].data = psci_state_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) free_state:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) i--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) for (; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) kfree(states[i].data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) static int psci_pd_parse_states(struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) struct genpd_power_state **states, int *state_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* Parse the domain idle states. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ret = of_genpd_parse_idle_states(np, states, state_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /* Fill out the PSCI specifics for each found state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) ret = psci_pd_parse_state_nodes(*states, *state_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) kfree(*states);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return ret;
^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) static void psci_pd_free_states(struct genpd_power_state *states,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) unsigned int state_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) for (i = 0; i < state_count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) kfree(states[i].data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) kfree(states);
^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) static int psci_pd_init(struct device_node *np, bool use_osi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct generic_pm_domain *pd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct psci_pd_provider *pd_provider;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct dev_power_governor *pd_gov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct genpd_power_state *states = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int ret = -ENOMEM, state_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) pd = kzalloc(sizeof(*pd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!pd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) pd_provider = kzalloc(sizeof(*pd_provider), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) if (!pd_provider)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) goto free_pd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) pd->name = kasprintf(GFP_KERNEL, "%pOF", np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (!pd->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) goto free_pd_prov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * Parse the domain idle states and let genpd manage the state selection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * for those being compatible with "domain-idle-state".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) ret = psci_pd_parse_states(np, &states, &state_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) goto free_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) pd->free_states = psci_pd_free_states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) pd->name = kbasename(pd->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) pd->states = states;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) pd->state_count = state_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) pd->flags |= GENPD_FLAG_IRQ_SAFE | GENPD_FLAG_CPU_DOMAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /* Allow power off when OSI has been successfully enabled. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (use_osi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) pd->power_off = psci_pd_power_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) pd->flags |= GENPD_FLAG_ALWAYS_ON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /* Use governor for CPU PM domains if it has some states to manage. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) pd_gov = state_count > 0 ? &pm_domain_cpu_gov : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) ret = pm_genpd_init(pd, pd_gov, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) psci_pd_free_states(states, state_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) goto free_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) ret = of_genpd_add_provider_simple(np, pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) goto remove_pd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) pd_provider->node = of_node_get(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) list_add(&pd_provider->link, &psci_pd_providers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) pr_debug("init PM domain %s\n", pd->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) remove_pd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) pm_genpd_remove(pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) free_name:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) kfree(pd->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) free_pd_prov:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) kfree(pd_provider);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) free_pd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) kfree(pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) pr_err("failed to init PM domain ret=%d %pOF\n", ret, np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static void psci_pd_remove(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) struct psci_pd_provider *pd_provider, *it;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct generic_pm_domain *genpd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) list_for_each_entry_safe(pd_provider, it, &psci_pd_providers, link) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) of_genpd_del_provider(pd_provider->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) genpd = of_genpd_remove_last(pd_provider->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (!IS_ERR(genpd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) kfree(genpd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) of_node_put(pd_provider->node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) list_del(&pd_provider->link);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) kfree(pd_provider);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^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) static int psci_pd_init_topology(struct device_node *np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct device_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) struct of_phandle_args child, parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) for_each_child_of_node(np, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (of_parse_phandle_with_args(node, "power-domains",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) "#power-domain-cells", 0, &parent))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) child.np = node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) child.args_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) ret = of_genpd_add_subdomain(&parent, &child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) of_node_put(parent.np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) of_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static bool psci_pd_try_set_osi_mode(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!psci_has_osi_support())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) ret = psci_set_osi_mode(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) pr_warn("failed to enable OSI mode: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) return true;
^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) static void psci_cpuidle_domain_sync_state(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * All devices have now been attached/probed to the PM domain topology,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * hence it's fine to allow domain states to be picked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) psci_pd_allow_domain_state = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static const struct of_device_id psci_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) { .compatible = "arm,psci-1.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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) static int psci_cpuidle_domain_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct device_node *np = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct device_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) bool use_osi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int ret = 0, pd_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (!np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) /* If OSI mode is supported, let's try to enable it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) use_osi = psci_pd_try_set_osi_mode();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * Parse child nodes for the "#power-domain-cells" property and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) * initialize a genpd/genpd-of-provider pair when it's found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) for_each_child_of_node(np, node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (!of_find_property(node, "#power-domain-cells", NULL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) ret = psci_pd_init(node, use_osi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) goto put_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) pd_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) /* Bail out if not using the hierarchical CPU topology. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (!pd_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) goto no_pd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /* Link genpd masters/subdomains to model the CPU topology. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) ret = psci_pd_init_topology(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) goto remove_pd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) pr_info("Initialized CPU PM domain topology\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) put_node:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) of_node_put(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) remove_pd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) psci_pd_remove();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) pr_err("failed to create CPU PM domains ret=%d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) no_pd:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (use_osi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) psci_set_osi_mode(false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static struct platform_driver psci_cpuidle_domain_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) .probe = psci_cpuidle_domain_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) .name = "psci-cpuidle-domain",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) .of_match_table = psci_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) .sync_state = psci_cpuidle_domain_sync_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static int __init psci_idle_init_domains(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) return platform_driver_register(&psci_cpuidle_domain_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) subsys_initcall(psci_idle_init_domains);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) struct device *psci_dt_attach_cpu(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) dev = dev_pm_domain_attach_by_name(get_cpu_device(cpu), "psci");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (IS_ERR_OR_NULL(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) pm_runtime_irq_safe(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (cpu_online(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) pm_runtime_get_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) dev_pm_syscore_device(dev, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) void psci_dt_detach_cpu(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (IS_ERR_OR_NULL(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) dev_pm_domain_detach(dev, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) }