1/*
2 * Copyright (c) 2013 ARM/Linaro
3 *
4 * Authors: Daniel Lezcano <daniel.lezcano@linaro.org>
5 *          Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
6 *          Nicolas Pitre <nicolas.pitre@linaro.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * Maintainer: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com>
13 * Maintainer: Daniel Lezcano <daniel.lezcano@linaro.org>
14 */
15#include <linux/cpuidle.h>
16#include <linux/cpu_pm.h>
17#include <linux/slab.h>
18#include <linux/of.h>
19
20#include <asm/cpu.h>
21#include <asm/cputype.h>
22#include <asm/cpuidle.h>
23#include <asm/mcpm.h>
24#include <asm/smp_plat.h>
25#include <asm/suspend.h>
26
27#include "dt_idle_states.h"
28
29static int bl_enter_powerdown(struct cpuidle_device *dev,
30			      struct cpuidle_driver *drv, int idx);
31
32/*
33 * NB: Owing to current menu governor behaviour big and LITTLE
34 * index 1 states have to define exit_latency and target_residency for
35 * cluster state since, when all CPUs in a cluster hit it, the cluster
36 * can be shutdown. This means that when a single CPU enters this state
37 * the exit_latency and target_residency values are somewhat overkill.
38 * There is no notion of cluster states in the menu governor, so CPUs
39 * have to define CPU states where possibly the cluster will be shutdown
40 * depending on the state of other CPUs. idle states entry and exit happen
41 * at random times; however the cluster state provides target_residency
42 * values as if all CPUs in a cluster enter the state at once; this is
43 * somewhat optimistic and behaviour should be fixed either in the governor
44 * or in the MCPM back-ends.
45 * To make this driver 100% generic the number of states and the exit_latency
46 * target_residency values must be obtained from device tree bindings.
47 *
48 * exit_latency: refers to the TC2 vexpress test chip and depends on the
49 * current cluster operating point. It is the time it takes to get the CPU
50 * up and running when the CPU is powered up on cluster wake-up from shutdown.
51 * Current values for big and LITTLE clusters are provided for clusters
52 * running at default operating points.
53 *
54 * target_residency: it is the minimum amount of time the cluster has
55 * to be down to break even in terms of power consumption. cluster
56 * shutdown has inherent dynamic power costs (L2 writebacks to DRAM
57 * being the main factor) that depend on the current operating points.
58 * The current values for both clusters are provided for a CPU whose half
59 * of L2 lines are dirty and require cleaning to DRAM, and takes into
60 * account leakage static power values related to the vexpress TC2 testchip.
61 */
62static struct cpuidle_driver bl_idle_little_driver = {
63	.name = "little_idle",
64	.owner = THIS_MODULE,
65	.states[0] = ARM_CPUIDLE_WFI_STATE,
66	.states[1] = {
67		.enter			= bl_enter_powerdown,
68		.exit_latency		= 700,
69		.target_residency	= 2500,
70		.flags			= CPUIDLE_FLAG_TIME_VALID |
71					  CPUIDLE_FLAG_TIMER_STOP,
72		.name			= "C1",
73		.desc			= "ARM little-cluster power down",
74	},
75	.state_count = 2,
76};
77
78static const struct of_device_id bl_idle_state_match[] __initconst = {
79	{ .compatible = "arm,idle-state",
80	  .data = bl_enter_powerdown },
81	{ },
82};
83
84static struct cpuidle_driver bl_idle_big_driver = {
85	.name = "big_idle",
86	.owner = THIS_MODULE,
87	.states[0] = ARM_CPUIDLE_WFI_STATE,
88	.states[1] = {
89		.enter			= bl_enter_powerdown,
90		.exit_latency		= 500,
91		.target_residency	= 2000,
92		.flags			= CPUIDLE_FLAG_TIME_VALID |
93					  CPUIDLE_FLAG_TIMER_STOP,
94		.name			= "C1",
95		.desc			= "ARM big-cluster power down",
96	},
97	.state_count = 2,
98};
99
100/*
101 * notrace prevents trace shims from getting inserted where they
102 * should not. Global jumps and ldrex/strex must not be inserted
103 * in power down sequences where caches and MMU may be turned off.
104 */
105static int notrace bl_powerdown_finisher(unsigned long arg)
106{
107	/* MCPM works with HW CPU identifiers */
108	unsigned int mpidr = read_cpuid_mpidr();
109	unsigned int cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
110	unsigned int cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
111
112	mcpm_set_entry_vector(cpu, cluster, cpu_resume);
113
114	/*
115	 * Residency value passed to mcpm_cpu_suspend back-end
116	 * has to be given clear semantics. Set to 0 as a
117	 * temporary value.
118	 */
119	mcpm_cpu_suspend(0);
120
121	/* return value != 0 means failure */
122	return 1;
123}
124
125/**
126 * bl_enter_powerdown - Programs CPU to enter the specified state
127 * @dev: cpuidle device
128 * @drv: The target state to be programmed
129 * @idx: state index
130 *
131 * Called from the CPUidle framework to program the device to the
132 * specified target state selected by the governor.
133 */
134static int bl_enter_powerdown(struct cpuidle_device *dev,
135				struct cpuidle_driver *drv, int idx)
136{
137	cpu_pm_enter();
138
139	cpu_suspend(0, bl_powerdown_finisher);
140
141	/* signals the MCPM core that CPU is out of low power state */
142	mcpm_cpu_powered_up();
143
144	cpu_pm_exit();
145
146	return idx;
147}
148
149static int __init bl_idle_driver_init(struct cpuidle_driver *drv, int part_id)
150{
151	struct cpumask *cpumask;
152	int cpu;
153
154	cpumask = kzalloc(cpumask_size(), GFP_KERNEL);
155	if (!cpumask)
156		return -ENOMEM;
157
158	for_each_possible_cpu(cpu)
159		if (smp_cpuid_part(cpu) == part_id)
160			cpumask_set_cpu(cpu, cpumask);
161
162	drv->cpumask = cpumask;
163
164	return 0;
165}
166
167static const struct of_device_id compatible_machine_match[] = {
168	{ .compatible = "arm,vexpress,v2p-ca15_a7" },
169	{ .compatible = "samsung,exynos5420" },
170	{ .compatible = "samsung,exynos5800" },
171	{},
172};
173
174static int __init bl_idle_init(void)
175{
176	int ret;
177	struct device_node *root = of_find_node_by_path("/");
178
179	if (!root)
180		return -ENODEV;
181
182	/*
183	 * Initialize the driver just for a compliant set of machines
184	 */
185	if (!of_match_node(compatible_machine_match, root))
186		return -ENODEV;
187	/*
188	 * For now the differentiation between little and big cores
189	 * is based on the part number. A7 cores are considered little
190	 * cores, A15 are considered big cores. This distinction may
191	 * evolve in the future with a more generic matching approach.
192	 */
193	ret = bl_idle_driver_init(&bl_idle_little_driver,
194				  ARM_CPU_PART_CORTEX_A7);
195	if (ret)
196		return ret;
197
198	ret = bl_idle_driver_init(&bl_idle_big_driver, ARM_CPU_PART_CORTEX_A15);
199	if (ret)
200		goto out_uninit_little;
201
202	/* Start at index 1, index 0 standard WFI */
203	ret = dt_init_idle_driver(&bl_idle_big_driver, bl_idle_state_match, 1);
204	if (ret < 0)
205		goto out_uninit_big;
206
207	/* Start at index 1, index 0 standard WFI */
208	ret = dt_init_idle_driver(&bl_idle_little_driver,
209				  bl_idle_state_match, 1);
210	if (ret < 0)
211		goto out_uninit_big;
212
213	ret = cpuidle_register(&bl_idle_little_driver, NULL);
214	if (ret)
215		goto out_uninit_big;
216
217	ret = cpuidle_register(&bl_idle_big_driver, NULL);
218	if (ret)
219		goto out_unregister_little;
220
221	return 0;
222
223out_unregister_little:
224	cpuidle_unregister(&bl_idle_little_driver);
225out_uninit_big:
226	kfree(bl_idle_big_driver.cpumask);
227out_uninit_little:
228	kfree(bl_idle_little_driver.cpumask);
229
230	return ret;
231}
232device_initcall(bl_idle_init);
233