// SPDX-License-Identifier: GPL-2.0-only1/*2* Copyright (c) 2013 ARM/Linaro3*4* Authors: Daniel Lezcano <[email protected]>5* Lorenzo Pieralisi <[email protected]>6* Nicolas Pitre <[email protected]>7*8* Maintainer: Lorenzo Pieralisi <[email protected]>9* Maintainer: Daniel Lezcano <[email protected]>10*/11#include <linux/cpuidle.h>12#include <linux/cpu_pm.h>13#include <linux/slab.h>14#include <linux/of.h>1516#include <asm/cpu.h>17#include <asm/cputype.h>18#include <asm/cpuidle.h>19#include <asm/mcpm.h>20#include <asm/smp_plat.h>21#include <asm/suspend.h>2223#include "dt_idle_states.h"2425static int bl_enter_powerdown(struct cpuidle_device *dev,26struct cpuidle_driver *drv, int idx);2728/*29* NB: Owing to current menu governor behaviour big and LITTLE30* index 1 states have to define exit_latency and target_residency for31* cluster state since, when all CPUs in a cluster hit it, the cluster32* can be shutdown. This means that when a single CPU enters this state33* the exit_latency and target_residency values are somewhat overkill.34* There is no notion of cluster states in the menu governor, so CPUs35* have to define CPU states where possibly the cluster will be shutdown36* depending on the state of other CPUs. idle states entry and exit happen37* at random times; however the cluster state provides target_residency38* values as if all CPUs in a cluster enter the state at once; this is39* somewhat optimistic and behaviour should be fixed either in the governor40* or in the MCPM back-ends.41* To make this driver 100% generic the number of states and the exit_latency42* target_residency values must be obtained from device tree bindings.43*44* exit_latency: refers to the TC2 vexpress test chip and depends on the45* current cluster operating point. It is the time it takes to get the CPU46* up and running when the CPU is powered up on cluster wake-up from shutdown.47* Current values for big and LITTLE clusters are provided for clusters48* running at default operating points.49*50* target_residency: it is the minimum amount of time the cluster has51* to be down to break even in terms of power consumption. cluster52* shutdown has inherent dynamic power costs (L2 writebacks to DRAM53* being the main factor) that depend on the current operating points.54* The current values for both clusters are provided for a CPU whose half55* of L2 lines are dirty and require cleaning to DRAM, and takes into56* account leakage static power values related to the vexpress TC2 testchip.57*/58static struct cpuidle_driver bl_idle_little_driver = {59.name = "little_idle",60.owner = THIS_MODULE,61.states[0] = ARM_CPUIDLE_WFI_STATE,62.states[1] = {63.enter = bl_enter_powerdown,64.exit_latency = 700,65.target_residency = 2500,66.flags = CPUIDLE_FLAG_TIMER_STOP |67CPUIDLE_FLAG_RCU_IDLE,68.name = "C1",69.desc = "ARM little-cluster power down",70},71.state_count = 2,72};7374static const struct of_device_id bl_idle_state_match[] __initconst = {75{ .compatible = "arm,idle-state",76.data = bl_enter_powerdown },77{ },78};7980static struct cpuidle_driver bl_idle_big_driver = {81.name = "big_idle",82.owner = THIS_MODULE,83.states[0] = ARM_CPUIDLE_WFI_STATE,84.states[1] = {85.enter = bl_enter_powerdown,86.exit_latency = 500,87.target_residency = 2000,88.flags = CPUIDLE_FLAG_TIMER_STOP |89CPUIDLE_FLAG_RCU_IDLE,90.name = "C1",91.desc = "ARM big-cluster power down",92},93.state_count = 2,94};9596/*97* notrace prevents trace shims from getting inserted where they98* should not. Global jumps and ldrex/strex must not be inserted99* in power down sequences where caches and MMU may be turned off.100*/101static int notrace bl_powerdown_finisher(unsigned long arg)102{103/* MCPM works with HW CPU identifiers */104unsigned int mpidr = read_cpuid_mpidr();105unsigned int cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);106unsigned int cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);107108mcpm_set_entry_vector(cpu, cluster, cpu_resume);109mcpm_cpu_suspend();110111/* return value != 0 means failure */112return 1;113}114115/**116* bl_enter_powerdown - Programs CPU to enter the specified state117* @dev: cpuidle device118* @drv: The target state to be programmed119* @idx: state index120*121* Called from the CPUidle framework to program the device to the122* specified target state selected by the governor.123*/124static __cpuidle int bl_enter_powerdown(struct cpuidle_device *dev,125struct cpuidle_driver *drv, int idx)126{127cpu_pm_enter();128ct_cpuidle_enter();129130cpu_suspend(0, bl_powerdown_finisher);131132/* signals the MCPM core that CPU is out of low power state */133mcpm_cpu_powered_up();134ct_cpuidle_exit();135136cpu_pm_exit();137138return idx;139}140141static int __init bl_idle_driver_init(struct cpuidle_driver *drv, int part_id)142{143struct cpumask *cpumask;144int cpu;145146cpumask = kzalloc(cpumask_size(), GFP_KERNEL);147if (!cpumask)148return -ENOMEM;149150for_each_present_cpu(cpu)151if (smp_cpuid_part(cpu) == part_id)152cpumask_set_cpu(cpu, cpumask);153154drv->cpumask = cpumask;155156return 0;157}158159static const struct of_device_id compatible_machine_match[] = {160{ .compatible = "arm,vexpress,v2p-ca15_a7" },161{ .compatible = "google,peach" },162{},163};164165static int __init bl_idle_init(void)166{167int ret;168169/*170* Initialize the driver just for a compliant set of machines171*/172if (!of_machine_device_match(compatible_machine_match))173return -ENODEV;174175if (!mcpm_is_available())176return -EUNATCH;177178/*179* For now the differentiation between little and big cores180* is based on the part number. A7 cores are considered little181* cores, A15 are considered big cores. This distinction may182* evolve in the future with a more generic matching approach.183*/184ret = bl_idle_driver_init(&bl_idle_little_driver,185ARM_CPU_PART_CORTEX_A7);186if (ret)187return ret;188189ret = bl_idle_driver_init(&bl_idle_big_driver, ARM_CPU_PART_CORTEX_A15);190if (ret)191goto out_uninit_little;192193/* Start at index 1, index 0 standard WFI */194ret = dt_init_idle_driver(&bl_idle_big_driver, bl_idle_state_match, 1);195if (ret < 0)196goto out_uninit_big;197198/* Start at index 1, index 0 standard WFI */199ret = dt_init_idle_driver(&bl_idle_little_driver,200bl_idle_state_match, 1);201if (ret < 0)202goto out_uninit_big;203204ret = cpuidle_register(&bl_idle_little_driver, NULL);205if (ret)206goto out_uninit_big;207208ret = cpuidle_register(&bl_idle_big_driver, NULL);209if (ret)210goto out_unregister_little;211212return 0;213214out_unregister_little:215cpuidle_unregister(&bl_idle_little_driver);216out_uninit_big:217kfree(bl_idle_big_driver.cpumask);218out_uninit_little:219kfree(bl_idle_little_driver.cpumask);220221return ret;222}223device_initcall(bl_idle_init);224225226