1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * PM domains for CPUs via genpd - managed by cpuidle-psci. 4 * 5 * Copyright (C) 2019 Linaro Ltd. 6 * Author: Ulf Hansson <ulf.hansson@linaro.org> 7 * 8 */ 9 10 #define pr_fmt(fmt) "CPUidle PSCI: " fmt 11 12 #include <linux/cpu.h> 13 #include <linux/device.h> 14 #include <linux/kernel.h> 15 #include <linux/platform_device.h> 16 #include <linux/pm_domain.h> 17 #include <linux/pm_runtime.h> 18 #include <linux/psci.h> 19 #include <linux/slab.h> 20 #include <linux/string.h> 21 22 #include "cpuidle-psci.h" 23 #include "dt_idle_genpd.h" 24 25 struct psci_pd_provider { 26 struct list_head link; 27 struct device_node *node; 28 }; 29 30 static LIST_HEAD(psci_pd_providers); 31 32 static int psci_pd_power_off(struct generic_pm_domain *pd) 33 { 34 struct genpd_power_state *state = &pd->states[pd->state_idx]; 35 u32 *pd_state; 36 37 if (!state->data) 38 return 0; 39 40 /* OSI mode is enabled, set the corresponding domain state. */ 41 pd_state = state->data; 42 psci_set_domain_state(pd, pd->state_idx, *pd_state); 43 44 return 0; 45 } 46 47 static int psci_pd_init(struct device_node *np, bool use_osi) 48 { 49 struct generic_pm_domain *pd; 50 struct psci_pd_provider *pd_provider; 51 struct dev_power_governor *pd_gov; 52 int ret = -ENOMEM; 53 54 pd = dt_idle_pd_alloc(np, psci_dt_parse_state_node); 55 if (!pd) 56 goto out; 57 58 pd_provider = kzalloc(sizeof(*pd_provider), GFP_KERNEL); 59 if (!pd_provider) 60 goto free_pd; 61 62 pd->flags |= GENPD_FLAG_IRQ_SAFE | GENPD_FLAG_CPU_DOMAIN; 63 64 /* 65 * Allow power off when OSI has been successfully enabled. 66 * On a PREEMPT_RT based configuration the domain idle states are 67 * supported, but only during system-wide suspend. 68 */ 69 if (use_osi) { 70 pd->power_off = psci_pd_power_off; 71 pd->flags |= GENPD_FLAG_ACTIVE_WAKEUP; 72 if (IS_ENABLED(CONFIG_PREEMPT_RT)) 73 pd->flags |= GENPD_FLAG_RPM_ALWAYS_ON; 74 } else { 75 pd->flags |= GENPD_FLAG_ALWAYS_ON; 76 } 77 78 /* Use governor for CPU PM domains if it has some states to manage. */ 79 pd_gov = pd->states ? &pm_domain_cpu_gov : NULL; 80 81 ret = pm_genpd_init(pd, pd_gov, false); 82 if (ret) 83 goto free_pd_prov; 84 85 ret = of_genpd_add_provider_simple(np, pd); 86 if (ret) 87 goto remove_pd; 88 89 pd_provider->node = of_node_get(np); 90 list_add(&pd_provider->link, &psci_pd_providers); 91 92 pr_debug("init PM domain %s\n", pd->name); 93 return 0; 94 95 remove_pd: 96 pm_genpd_remove(pd); 97 free_pd_prov: 98 kfree(pd_provider); 99 free_pd: 100 dt_idle_pd_free(pd); 101 out: 102 pr_err("failed to init PM domain ret=%d %pOF\n", ret, np); 103 return ret; 104 } 105 106 static void psci_pd_remove(void) 107 { 108 struct psci_pd_provider *pd_provider, *it; 109 struct generic_pm_domain *genpd; 110 111 list_for_each_entry_safe_reverse(pd_provider, it, 112 &psci_pd_providers, link) { 113 of_genpd_del_provider(pd_provider->node); 114 115 genpd = of_genpd_remove_last(pd_provider->node); 116 if (!IS_ERR(genpd)) 117 kfree(genpd); 118 119 of_node_put(pd_provider->node); 120 list_del(&pd_provider->link); 121 kfree(pd_provider); 122 } 123 } 124 125 static const struct of_device_id psci_of_match[] = { 126 { .compatible = "arm,psci-1.0" }, 127 {} 128 }; 129 130 static int psci_cpuidle_domain_probe(struct platform_device *pdev) 131 { 132 struct device_node *np = pdev->dev.of_node; 133 bool use_osi = psci_has_osi_support(); 134 int ret = 0, pd_count = 0; 135 136 if (!np) 137 return -ENODEV; 138 139 /* 140 * Parse child nodes for the "#power-domain-cells" property and 141 * initialize a genpd/genpd-of-provider pair when it's found. 142 */ 143 for_each_child_of_node_scoped(np, node) { 144 if (!of_property_present(node, "#power-domain-cells")) 145 continue; 146 147 ret = psci_pd_init(node, use_osi); 148 if (ret) 149 goto exit; 150 151 pd_count++; 152 } 153 154 /* Bail out if not using the hierarchical CPU topology. */ 155 if (!pd_count) 156 return 0; 157 158 /* Link genpd masters/subdomains to model the CPU topology. */ 159 ret = dt_idle_pd_init_topology(np); 160 if (ret) 161 goto remove_pd; 162 163 /* let's try to enable OSI. */ 164 ret = psci_set_osi_mode(use_osi); 165 if (ret) 166 goto remove_pd; 167 168 pr_info("Initialized CPU PM domain topology using %s mode\n", 169 use_osi ? "OSI" : "PC"); 170 return 0; 171 172 remove_pd: 173 dt_idle_pd_remove_topology(np); 174 psci_pd_remove(); 175 exit: 176 pr_err("failed to create CPU PM domains ret=%d\n", ret); 177 return ret; 178 } 179 180 static struct platform_driver psci_cpuidle_domain_driver = { 181 .probe = psci_cpuidle_domain_probe, 182 .driver = { 183 .name = "psci-cpuidle-domain", 184 .of_match_table = psci_of_match, 185 }, 186 }; 187 188 static int __init psci_idle_init_domains(void) 189 { 190 return platform_driver_register(&psci_cpuidle_domain_driver); 191 } 192 core_initcall(psci_idle_init_domains); 193