1*2874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2d0508944SPaul Burton /*
3d0508944SPaul Burton * Copyright (C) 2014 Imagination Technologies
4fb615d61SPaul Burton * Author: Paul Burton <paul.burton@mips.com>
5d0508944SPaul Burton */
6d0508944SPaul Burton
7d0508944SPaul Burton #include <linux/cpu_pm.h>
8d0508944SPaul Burton #include <linux/cpuidle.h>
9d0508944SPaul Burton #include <linux/init.h>
10d0508944SPaul Burton
11d0508944SPaul Burton #include <asm/idle.h>
12d0508944SPaul Burton #include <asm/pm-cps.h>
13d0508944SPaul Burton
14d0508944SPaul Burton /* Enumeration of the various idle states this driver may enter */
15d0508944SPaul Burton enum cps_idle_state {
16d0508944SPaul Burton STATE_WAIT = 0, /* MIPS wait instruction, coherent */
17d0508944SPaul Burton STATE_NC_WAIT, /* MIPS wait instruction, non-coherent */
18d0508944SPaul Burton STATE_CLOCK_GATED, /* Core clock gated */
19d0508944SPaul Burton STATE_POWER_GATED, /* Core power gated */
20d0508944SPaul Burton STATE_COUNT
21d0508944SPaul Burton };
22d0508944SPaul Burton
cps_nc_enter(struct cpuidle_device * dev,struct cpuidle_driver * drv,int index)23d0508944SPaul Burton static int cps_nc_enter(struct cpuidle_device *dev,
24d0508944SPaul Burton struct cpuidle_driver *drv, int index)
25d0508944SPaul Burton {
26d0508944SPaul Burton enum cps_pm_state pm_state;
27d0508944SPaul Burton int err;
28d0508944SPaul Burton
29d0508944SPaul Burton /*
30d0508944SPaul Burton * At least one core must remain powered up & clocked in order for the
31d0508944SPaul Burton * system to have any hope of functioning.
32d0508944SPaul Burton *
33d0508944SPaul Burton * TODO: don't treat core 0 specially, just prevent the final core
34d0508944SPaul Burton * TODO: remap interrupt affinity temporarily
35d0508944SPaul Burton */
36fe7a38c6SPaul Burton if (cpus_are_siblings(0, dev->cpu) && (index > STATE_NC_WAIT))
37d0508944SPaul Burton index = STATE_NC_WAIT;
38d0508944SPaul Burton
39d0508944SPaul Burton /* Select the appropriate cps_pm_state */
40d0508944SPaul Burton switch (index) {
41d0508944SPaul Burton case STATE_NC_WAIT:
42d0508944SPaul Burton pm_state = CPS_PM_NC_WAIT;
43d0508944SPaul Burton break;
44d0508944SPaul Burton case STATE_CLOCK_GATED:
45d0508944SPaul Burton pm_state = CPS_PM_CLOCK_GATED;
46d0508944SPaul Burton break;
47d0508944SPaul Burton case STATE_POWER_GATED:
48d0508944SPaul Burton pm_state = CPS_PM_POWER_GATED;
49d0508944SPaul Burton break;
50d0508944SPaul Burton default:
51d0508944SPaul Burton BUG();
52d0508944SPaul Burton return -EINVAL;
53d0508944SPaul Burton }
54d0508944SPaul Burton
55d0508944SPaul Burton /* Notify listeners the CPU is about to power down */
56d0508944SPaul Burton if ((pm_state == CPS_PM_POWER_GATED) && cpu_pm_enter())
57d0508944SPaul Burton return -EINTR;
58d0508944SPaul Burton
59d0508944SPaul Burton /* Enter that state */
60d0508944SPaul Burton err = cps_pm_enter_state(pm_state);
61d0508944SPaul Burton
62d0508944SPaul Burton /* Notify listeners the CPU is back up */
63d0508944SPaul Burton if (pm_state == CPS_PM_POWER_GATED)
64d0508944SPaul Burton cpu_pm_exit();
65d0508944SPaul Burton
66d0508944SPaul Burton return err ?: index;
67d0508944SPaul Burton }
68d0508944SPaul Burton
69d0508944SPaul Burton static struct cpuidle_driver cps_driver = {
70d0508944SPaul Burton .name = "cpc_cpuidle",
71d0508944SPaul Burton .owner = THIS_MODULE,
72d0508944SPaul Burton .states = {
73d0508944SPaul Burton [STATE_WAIT] = MIPS_CPUIDLE_WAIT_STATE,
74d0508944SPaul Burton [STATE_NC_WAIT] = {
75d0508944SPaul Burton .enter = cps_nc_enter,
76d0508944SPaul Burton .exit_latency = 200,
77d0508944SPaul Burton .target_residency = 450,
78d0508944SPaul Burton .name = "nc-wait",
79d0508944SPaul Burton .desc = "non-coherent MIPS wait",
80d0508944SPaul Burton },
81d0508944SPaul Burton [STATE_CLOCK_GATED] = {
82d0508944SPaul Burton .enter = cps_nc_enter,
83d0508944SPaul Burton .exit_latency = 300,
84d0508944SPaul Burton .target_residency = 700,
85b82b6ccaSDaniel Lezcano .flags = CPUIDLE_FLAG_TIMER_STOP,
86d0508944SPaul Burton .name = "clock-gated",
87d0508944SPaul Burton .desc = "core clock gated",
88d0508944SPaul Burton },
89d0508944SPaul Burton [STATE_POWER_GATED] = {
90d0508944SPaul Burton .enter = cps_nc_enter,
91d0508944SPaul Burton .exit_latency = 600,
92d0508944SPaul Burton .target_residency = 1000,
93b82b6ccaSDaniel Lezcano .flags = CPUIDLE_FLAG_TIMER_STOP,
94d0508944SPaul Burton .name = "power-gated",
95d0508944SPaul Burton .desc = "core power gated",
96d0508944SPaul Burton },
97d0508944SPaul Burton },
98d0508944SPaul Burton .state_count = STATE_COUNT,
99d0508944SPaul Burton .safe_state_index = 0,
100d0508944SPaul Burton };
101d0508944SPaul Burton
cps_cpuidle_unregister(void)102d0508944SPaul Burton static void __init cps_cpuidle_unregister(void)
103d0508944SPaul Burton {
104d0508944SPaul Burton int cpu;
105d0508944SPaul Burton struct cpuidle_device *device;
106d0508944SPaul Burton
107d0508944SPaul Burton for_each_possible_cpu(cpu) {
108d0508944SPaul Burton device = &per_cpu(cpuidle_dev, cpu);
109d0508944SPaul Burton cpuidle_unregister_device(device);
110d0508944SPaul Burton }
111d0508944SPaul Burton
112d0508944SPaul Burton cpuidle_unregister_driver(&cps_driver);
113d0508944SPaul Burton }
114d0508944SPaul Burton
cps_cpuidle_init(void)115d0508944SPaul Burton static int __init cps_cpuidle_init(void)
116d0508944SPaul Burton {
11702018b39SMarcin Nowakowski int err, cpu, i;
118d0508944SPaul Burton struct cpuidle_device *device;
119d0508944SPaul Burton
120d0508944SPaul Burton /* Detect supported states */
121d0508944SPaul Burton if (!cps_pm_support_state(CPS_PM_POWER_GATED))
122d0508944SPaul Burton cps_driver.state_count = STATE_CLOCK_GATED + 1;
123d0508944SPaul Burton if (!cps_pm_support_state(CPS_PM_CLOCK_GATED))
124d0508944SPaul Burton cps_driver.state_count = STATE_NC_WAIT + 1;
125d0508944SPaul Burton if (!cps_pm_support_state(CPS_PM_NC_WAIT))
126d0508944SPaul Burton cps_driver.state_count = STATE_WAIT + 1;
127d0508944SPaul Burton
128d0508944SPaul Burton /* Inform the user if some states are unavailable */
129d0508944SPaul Burton if (cps_driver.state_count < STATE_COUNT) {
130d0508944SPaul Burton pr_info("cpuidle-cps: limited to ");
131d0508944SPaul Burton switch (cps_driver.state_count - 1) {
132d0508944SPaul Burton case STATE_WAIT:
133d0508944SPaul Burton pr_cont("coherent wait\n");
134d0508944SPaul Burton break;
135d0508944SPaul Burton case STATE_NC_WAIT:
136d0508944SPaul Burton pr_cont("non-coherent wait\n");
137d0508944SPaul Burton break;
138d0508944SPaul Burton case STATE_CLOCK_GATED:
139d0508944SPaul Burton pr_cont("clock gating\n");
140d0508944SPaul Burton break;
141d0508944SPaul Burton }
142d0508944SPaul Burton }
143d0508944SPaul Burton
144d0508944SPaul Burton /*
145d0508944SPaul Burton * Set the coupled flag on the appropriate states if this system
146d0508944SPaul Burton * requires it.
147d0508944SPaul Burton */
148d0508944SPaul Burton if (coupled_coherence)
149d0508944SPaul Burton for (i = STATE_NC_WAIT; i < cps_driver.state_count; i++)
150d0508944SPaul Burton cps_driver.states[i].flags |= CPUIDLE_FLAG_COUPLED;
151d0508944SPaul Burton
152d0508944SPaul Burton err = cpuidle_register_driver(&cps_driver);
153d0508944SPaul Burton if (err) {
154d0508944SPaul Burton pr_err("Failed to register CPS cpuidle driver\n");
155d0508944SPaul Burton return err;
156d0508944SPaul Burton }
157d0508944SPaul Burton
158d0508944SPaul Burton for_each_possible_cpu(cpu) {
159d0508944SPaul Burton device = &per_cpu(cpuidle_dev, cpu);
160d0508944SPaul Burton device->cpu = cpu;
16172bc8c75SMatt Redfearn #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
162d0508944SPaul Burton cpumask_copy(&device->coupled_cpus, &cpu_sibling_map[cpu]);
163d0508944SPaul Burton #endif
164d0508944SPaul Burton
165d0508944SPaul Burton err = cpuidle_register_device(device);
166d0508944SPaul Burton if (err) {
167d0508944SPaul Burton pr_err("Failed to register CPU%d cpuidle device\n",
168d0508944SPaul Burton cpu);
169d0508944SPaul Burton goto err_out;
170d0508944SPaul Burton }
171d0508944SPaul Burton }
172d0508944SPaul Burton
173d0508944SPaul Burton return 0;
174d0508944SPaul Burton err_out:
175d0508944SPaul Burton cps_cpuidle_unregister();
176d0508944SPaul Burton return err;
177d0508944SPaul Burton }
178d0508944SPaul Burton device_initcall(cps_cpuidle_init);
179