// SPDX-License-Identifier: GPL-2.0-only1/*2* ARM/ARM64 generic CPU idle driver.3*4* Copyright (C) 2014 ARM Ltd.5* Author: Lorenzo Pieralisi <[email protected]>6*/78#define pr_fmt(fmt) "CPUidle arm: " fmt910#include <linux/cpu_cooling.h>11#include <linux/cpuidle.h>12#include <linux/cpumask.h>13#include <linux/cpu_pm.h>14#include <linux/kernel.h>15#include <linux/module.h>16#include <linux/of.h>17#include <linux/slab.h>1819#include <asm/cpuidle.h>2021#include "dt_idle_states.h"2223/*24* arm_enter_idle_state - Programs CPU to enter the specified state25*26* dev: cpuidle device27* drv: cpuidle driver28* idx: state index29*30* Called from the CPUidle framework to program the device to the31* specified target state selected by the governor.32*/33static __cpuidle int arm_enter_idle_state(struct cpuidle_device *dev,34struct cpuidle_driver *drv, int idx)35{36/*37* Pass idle state index to arm_cpuidle_suspend which in turn38* will call the CPU ops suspend protocol with idle index as a39* parameter.40*/41return CPU_PM_CPU_IDLE_ENTER(arm_cpuidle_suspend, idx);42}4344static struct cpuidle_driver arm_idle_driver __initdata = {45.name = "arm_idle",46.owner = THIS_MODULE,47/*48* State at index 0 is standby wfi and considered standard49* on all ARM platforms. If in some platforms simple wfi50* can't be used as "state 0", DT bindings must be implemented51* to work around this issue and allow installing a special52* handler for idle state index 0.53*/54.states[0] = {55.enter = arm_enter_idle_state,56.exit_latency = 1,57.target_residency = 1,58.power_usage = UINT_MAX,59.name = "WFI",60.desc = "ARM WFI",61}62};6364static const struct of_device_id arm_idle_state_match[] __initconst = {65{ .compatible = "arm,idle-state",66.data = arm_enter_idle_state },67{ },68};6970/*71* arm_idle_init_cpu72*73* Registers the arm specific cpuidle driver with the cpuidle74* framework. It relies on core code to parse the idle states75* and initialize them using driver data structures accordingly.76*/77static int __init arm_idle_init_cpu(int cpu)78{79int ret;80struct cpuidle_driver *drv;8182drv = kmemdup(&arm_idle_driver, sizeof(*drv), GFP_KERNEL);83if (!drv)84return -ENOMEM;8586drv->cpumask = (struct cpumask *)cpumask_of(cpu);8788/*89* Initialize idle states data, starting at index 1. This90* driver is DT only, if no DT idle states are detected (ret91* == 0) let the driver initialization fail accordingly since92* there is no reason to initialize the idle driver if only93* wfi is supported.94*/95ret = dt_init_idle_driver(drv, arm_idle_state_match, 1);96if (ret <= 0) {97ret = ret ? : -ENODEV;98goto out_kfree_drv;99}100101/*102* Call arch CPU operations in order to initialize103* idle states suspend back-end specific data104*/105ret = arm_cpuidle_init(cpu);106107/*108* Allow the initialization to continue for other CPUs, if the109* reported failure is a HW misconfiguration/breakage (-ENXIO).110*111* Some platforms do not support idle operations112* (arm_cpuidle_init() returning -EOPNOTSUPP), we should113* not flag this case as an error, it is a valid114* configuration.115*/116if (ret) {117if (ret != -EOPNOTSUPP)118pr_err("CPU %d failed to init idle CPU ops\n", cpu);119ret = ret == -ENXIO ? 0 : ret;120goto out_kfree_drv;121}122123ret = cpuidle_register(drv, NULL);124if (ret)125goto out_kfree_drv;126127cpuidle_cooling_register(drv);128129return 0;130131out_kfree_drv:132kfree(drv);133return ret;134}135136/*137* arm_idle_init - Initializes arm cpuidle driver138*139* Initializes arm cpuidle driver for all present CPUs, if any140* CPU fails to register cpuidle driver then rollback to cancel141* all CPUs registration.142*/143static int __init arm_idle_init(void)144{145int cpu, ret;146struct cpuidle_driver *drv;147struct cpuidle_device *dev;148149for_each_present_cpu(cpu) {150ret = arm_idle_init_cpu(cpu);151if (ret)152goto out_fail;153}154155return 0;156157out_fail:158while (--cpu >= 0) {159dev = per_cpu(cpuidle_devices, cpu);160drv = cpuidle_get_cpu_driver(dev);161cpuidle_unregister(drv);162kfree(drv);163}164165return ret;166}167device_initcall(arm_idle_init);168169170