Path: blob/master/drivers/cpuidle/cpuidle-psci-domain.c
26278 views
// SPDX-License-Identifier: GPL-2.01/*2* PM domains for CPUs via genpd - managed by cpuidle-psci.3*4* Copyright (C) 2019 Linaro Ltd.5* Author: Ulf Hansson <[email protected]>6*7*/89#define pr_fmt(fmt) "CPUidle PSCI: " fmt1011#include <linux/cpu.h>12#include <linux/device.h>13#include <linux/kernel.h>14#include <linux/platform_device.h>15#include <linux/pm_domain.h>16#include <linux/pm_runtime.h>17#include <linux/psci.h>18#include <linux/slab.h>19#include <linux/string.h>2021#include "cpuidle-psci.h"22#include "dt_idle_genpd.h"2324struct psci_pd_provider {25struct list_head link;26struct device_node *node;27};2829static LIST_HEAD(psci_pd_providers);3031static int psci_pd_power_off(struct generic_pm_domain *pd)32{33struct genpd_power_state *state = &pd->states[pd->state_idx];34u32 *pd_state;3536if (!state->data)37return 0;3839/* OSI mode is enabled, set the corresponding domain state. */40pd_state = state->data;41psci_set_domain_state(pd, pd->state_idx, *pd_state);4243return 0;44}4546static int psci_pd_init(struct device_node *np, bool use_osi)47{48struct generic_pm_domain *pd;49struct psci_pd_provider *pd_provider;50struct dev_power_governor *pd_gov;51int ret = -ENOMEM;5253pd = dt_idle_pd_alloc(np, psci_dt_parse_state_node);54if (!pd)55goto out;5657pd_provider = kzalloc(sizeof(*pd_provider), GFP_KERNEL);58if (!pd_provider)59goto free_pd;6061pd->flags |= GENPD_FLAG_IRQ_SAFE | GENPD_FLAG_CPU_DOMAIN;6263/*64* Allow power off when OSI has been successfully enabled.65* On a PREEMPT_RT based configuration the domain idle states are66* supported, but only during system-wide suspend.67*/68if (use_osi) {69pd->power_off = psci_pd_power_off;70pd->flags |= GENPD_FLAG_ACTIVE_WAKEUP;71if (IS_ENABLED(CONFIG_PREEMPT_RT))72pd->flags |= GENPD_FLAG_RPM_ALWAYS_ON;73} else {74pd->flags |= GENPD_FLAG_ALWAYS_ON;75}7677/* Use governor for CPU PM domains if it has some states to manage. */78pd_gov = pd->states ? &pm_domain_cpu_gov : NULL;7980ret = pm_genpd_init(pd, pd_gov, false);81if (ret)82goto free_pd_prov;8384ret = of_genpd_add_provider_simple(np, pd);85if (ret)86goto remove_pd;8788pd_provider->node = of_node_get(np);89list_add(&pd_provider->link, &psci_pd_providers);9091pr_debug("init PM domain %s\n", pd->name);92return 0;9394remove_pd:95pm_genpd_remove(pd);96free_pd_prov:97kfree(pd_provider);98free_pd:99dt_idle_pd_free(pd);100out:101pr_err("failed to init PM domain ret=%d %pOF\n", ret, np);102return ret;103}104105static void psci_pd_remove(void)106{107struct psci_pd_provider *pd_provider, *it;108struct generic_pm_domain *genpd;109110list_for_each_entry_safe_reverse(pd_provider, it,111&psci_pd_providers, link) {112of_genpd_del_provider(pd_provider->node);113114genpd = of_genpd_remove_last(pd_provider->node);115if (!IS_ERR(genpd))116kfree(genpd);117118of_node_put(pd_provider->node);119list_del(&pd_provider->link);120kfree(pd_provider);121}122}123124static const struct of_device_id psci_of_match[] = {125{ .compatible = "arm,psci-1.0" },126{}127};128129static int psci_cpuidle_domain_probe(struct platform_device *pdev)130{131struct device_node *np = pdev->dev.of_node;132bool use_osi = psci_has_osi_support();133int ret = 0, pd_count = 0;134135if (!np)136return -ENODEV;137138/*139* Parse child nodes for the "#power-domain-cells" property and140* initialize a genpd/genpd-of-provider pair when it's found.141*/142for_each_child_of_node_scoped(np, node) {143if (!of_property_present(node, "#power-domain-cells"))144continue;145146ret = psci_pd_init(node, use_osi);147if (ret)148goto exit;149150pd_count++;151}152153/* Bail out if not using the hierarchical CPU topology. */154if (!pd_count)155return 0;156157/* Link genpd masters/subdomains to model the CPU topology. */158ret = dt_idle_pd_init_topology(np);159if (ret)160goto remove_pd;161162/* let's try to enable OSI. */163ret = psci_set_osi_mode(use_osi);164if (ret)165goto remove_pd;166167pr_info("Initialized CPU PM domain topology using %s mode\n",168use_osi ? "OSI" : "PC");169return 0;170171remove_pd:172dt_idle_pd_remove_topology(np);173psci_pd_remove();174exit:175pr_err("failed to create CPU PM domains ret=%d\n", ret);176return ret;177}178179static struct platform_driver psci_cpuidle_domain_driver = {180.probe = psci_cpuidle_domain_probe,181.driver = {182.name = "psci-cpuidle-domain",183.of_match_table = psci_of_match,184},185};186187static int __init psci_idle_init_domains(void)188{189return platform_driver_register(&psci_cpuidle_domain_driver);190}191core_initcall(psci_idle_init_domains);192193194