Skip to content

Commit e978aa7

Browse files
Deepthi Dharwarlenb
authored andcommitted
cpuidle: Move dev->last_residency update to driver enter routine; remove dev->last_state
Cpuidle governor only suggests the state to enter using the governor->select() interface, but allows the low level driver to override the recommended state. The actual entered state may be different because of software or hardware demotion. Software demotion is done by the back-end cpuidle driver and can be accounted correctly. Current cpuidle code uses last_state field to capture the actual state entered and based on that updates the statistics for the state entered. Ideally the driver enter routine should update the counters, and it should return the state actually entered rather than the time spent there. The generic cpuidle code should simply handle where the counters live in the sysfs namespace, not updating the counters. Reference: https://lkml.org/lkml/2011/3/25/52 Signed-off-by: Deepthi Dharwar <deepthi@linux.vnet.ibm.com> Signed-off-by: Trinabh Gupta <g.trinabh@gmail.com> Tested-by: Jean Pihet <j-pihet@ti.com> Reviewed-by: Kevin Hilman <khilman@ti.com> Acked-by: Arjan van de Ven <arjan@linux.intel.com> Acked-by: Kevin Hilman <khilman@ti.com> Signed-off-by: Len Brown <len.brown@intel.com>
1 parent c3b92c8 commit e978aa7

12 files changed

Lines changed: 164 additions & 99 deletions

File tree

‎arch/arm/mach-at91/cpuidle.c‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,18 @@ static struct cpuidle_driver at91_idle_driver = {
3333

3434
/* Actual code that puts the SoC in different idle states */
3535
static int at91_enter_idle(struct cpuidle_device *dev,
36-
struct cpuidle_state *state)
36+
int index)
3737
{
3838
struct timeval before, after;
3939
int idle_time;
4040
u32 saved_lpr;
4141

4242
local_irq_disable();
4343
do_gettimeofday(&before);
44-
if (state == &dev->states[0])
44+
if (index == 0)
4545
/* Wait for interrupt state */
4646
cpu_do_idle();
47-
else if (state == &dev->states[1]) {
47+
else if (index == 1) {
4848
asm("b 1f; .align 5; 1:");
4949
asm("mcr p15, 0, r0, c7, c10, 4"); /* drain write buffer */
5050
saved_lpr = sdram_selfrefresh_enable();
@@ -55,7 +55,9 @@ static int at91_enter_idle(struct cpuidle_device *dev,
5555
local_irq_enable();
5656
idle_time = (after.tv_sec - before.tv_sec) * USEC_PER_SEC +
5757
(after.tv_usec - before.tv_usec);
58-
return idle_time;
58+
59+
dev->last_residency = idle_time;
60+
return index;
5961
}
6062

6163
/* Initialize CPU idle by registering the idle states */

‎arch/arm/mach-davinci/cpuidle.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ static struct davinci_ops davinci_states[DAVINCI_CPUIDLE_MAX_STATES] = {
7878

7979
/* Actual code that puts the SoC in different idle states */
8080
static int davinci_enter_idle(struct cpuidle_device *dev,
81-
struct cpuidle_state *state)
81+
int index)
8282
{
83-
struct davinci_ops *ops = cpuidle_get_statedata(state);
83+
struct davinci_ops *ops = cpuidle_get_statedata(&dev->states[index]);
8484
struct timeval before, after;
8585
int idle_time;
8686

@@ -98,7 +98,10 @@ static int davinci_enter_idle(struct cpuidle_device *dev,
9898
local_irq_enable();
9999
idle_time = (after.tv_sec - before.tv_sec) * USEC_PER_SEC +
100100
(after.tv_usec - before.tv_usec);
101-
return idle_time;
101+
102+
dev->last_residency = idle_time;
103+
104+
return index;
102105
}
103106

104107
static int __init davinci_cpuidle_probe(struct platform_device *pdev)

‎arch/arm/mach-exynos4/cpuidle.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <asm/proc-fns.h>
1717

1818
static int exynos4_enter_idle(struct cpuidle_device *dev,
19-
struct cpuidle_state *state);
19+
int index);
2020

2121
static struct cpuidle_state exynos4_cpuidle_set[] = {
2222
[0] = {
@@ -37,7 +37,7 @@ static struct cpuidle_driver exynos4_idle_driver = {
3737
};
3838

3939
static int exynos4_enter_idle(struct cpuidle_device *dev,
40-
struct cpuidle_state *state)
40+
int index)
4141
{
4242
struct timeval before, after;
4343
int idle_time;
@@ -52,7 +52,8 @@ static int exynos4_enter_idle(struct cpuidle_device *dev,
5252
idle_time = (after.tv_sec - before.tv_sec) * USEC_PER_SEC +
5353
(after.tv_usec - before.tv_usec);
5454

55-
return idle_time;
55+
dev->last_residency = idle_time;
56+
return index;
5657
}
5758

5859
static int __init exynos4_init_cpuidle(void)

‎arch/arm/mach-kirkwood/cpuidle.c‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ static DEFINE_PER_CPU(struct cpuidle_device, kirkwood_cpuidle_device);
3232

3333
/* Actual code that puts the SoC in different idle states */
3434
static int kirkwood_enter_idle(struct cpuidle_device *dev,
35-
struct cpuidle_state *state)
35+
int index)
3636
{
3737
struct timeval before, after;
3838
int idle_time;
3939

4040
local_irq_disable();
4141
do_gettimeofday(&before);
42-
if (state == &dev->states[0])
42+
if (index == 0)
4343
/* Wait for interrupt state */
4444
cpu_do_idle();
45-
else if (state == &dev->states[1]) {
45+
else if (index == 1) {
4646
/*
4747
* Following write will put DDR in self refresh.
4848
* Note that we have 256 cycles before DDR puts it
@@ -57,7 +57,11 @@ static int kirkwood_enter_idle(struct cpuidle_device *dev,
5757
local_irq_enable();
5858
idle_time = (after.tv_sec - before.tv_sec) * USEC_PER_SEC +
5959
(after.tv_usec - before.tv_usec);
60-
return idle_time;
60+
61+
/* Update last residency */
62+
dev->last_residency = idle_time;
63+
64+
return index;
6165
}
6266

6367
/* Initialize CPU idle by registering the idle states */

‎arch/arm/mach-omap2/cpuidle34xx.c‎

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,19 @@ static int _cpuidle_deny_idle(struct powerdomain *pwrdm,
8888
/**
8989
* omap3_enter_idle - Programs OMAP3 to enter the specified state
9090
* @dev: cpuidle device
91-
* @state: The target state to be programmed
91+
* @index: the index of state to be entered
9292
*
9393
* Called from the CPUidle framework to program the device to the
9494
* specified target state selected by the governor.
9595
*/
9696
static int omap3_enter_idle(struct cpuidle_device *dev,
97-
struct cpuidle_state *state)
97+
int index)
9898
{
99-
struct omap3_idle_statedata *cx = cpuidle_get_statedata(state);
99+
struct omap3_idle_statedata *cx =
100+
cpuidle_get_statedata(&dev->states[index]);
100101
struct timespec ts_preidle, ts_postidle, ts_idle;
101102
u32 mpu_state = cx->mpu_state, core_state = cx->core_state;
103+
int idle_time;
102104

103105
/* Used to keep track of the total time in idle */
104106
getnstimeofday(&ts_preidle);
@@ -113,7 +115,7 @@ static int omap3_enter_idle(struct cpuidle_device *dev,
113115
goto return_sleep_time;
114116

115117
/* Deny idle for C1 */
116-
if (state == &dev->states[0]) {
118+
if (index == 0) {
117119
pwrdm_for_each_clkdm(mpu_pd, _cpuidle_deny_idle);
118120
pwrdm_for_each_clkdm(core_pd, _cpuidle_deny_idle);
119121
}
@@ -122,7 +124,7 @@ static int omap3_enter_idle(struct cpuidle_device *dev,
122124
omap_sram_idle();
123125

124126
/* Re-allow idle for C1 */
125-
if (state == &dev->states[0]) {
127+
if (index == 0) {
126128
pwrdm_for_each_clkdm(mpu_pd, _cpuidle_allow_idle);
127129
pwrdm_for_each_clkdm(core_pd, _cpuidle_allow_idle);
128130
}
@@ -134,28 +136,35 @@ static int omap3_enter_idle(struct cpuidle_device *dev,
134136
local_irq_enable();
135137
local_fiq_enable();
136138

137-
return ts_idle.tv_nsec / NSEC_PER_USEC + ts_idle.tv_sec * USEC_PER_SEC;
139+
idle_time = ts_idle.tv_nsec / NSEC_PER_USEC + ts_idle.tv_sec * \
140+
USEC_PER_SEC;
141+
142+
/* Update cpuidle counters */
143+
dev->last_residency = idle_time;
144+
145+
return index;
138146
}
139147

140148
/**
141149
* next_valid_state - Find next valid C-state
142150
* @dev: cpuidle device
143-
* @state: Currently selected C-state
151+
* @index: Index of currently selected c-state
144152
*
145-
* If the current state is valid, it is returned back to the caller.
146-
* Else, this function searches for a lower c-state which is still
147-
* valid.
153+
* If the state corresponding to index is valid, index is returned back
154+
* to the caller. Else, this function searches for a lower c-state which is
155+
* still valid (as defined in omap3_power_states[]) and returns its index.
148156
*
149157
* A state is valid if the 'valid' field is enabled and
150158
* if it satisfies the enable_off_mode condition.
151159
*/
152-
static struct cpuidle_state *next_valid_state(struct cpuidle_device *dev,
153-
struct cpuidle_state *curr)
160+
static int next_valid_state(struct cpuidle_device *dev,
161+
int index)
154162
{
155-
struct cpuidle_state *next = NULL;
163+
struct cpuidle_state *curr = &dev->states[index];
156164
struct omap3_idle_statedata *cx = cpuidle_get_statedata(curr);
157165
u32 mpu_deepest_state = PWRDM_POWER_RET;
158166
u32 core_deepest_state = PWRDM_POWER_RET;
167+
int next_index = -1;
159168

160169
if (enable_off_mode) {
161170
mpu_deepest_state = PWRDM_POWER_OFF;
@@ -172,20 +181,20 @@ static struct cpuidle_state *next_valid_state(struct cpuidle_device *dev,
172181
if ((cx->valid) &&
173182
(cx->mpu_state >= mpu_deepest_state) &&
174183
(cx->core_state >= core_deepest_state)) {
175-
return curr;
184+
return index;
176185
} else {
177186
int idx = OMAP3_NUM_STATES - 1;
178187

179188
/* Reach the current state starting at highest C-state */
180189
for (; idx >= 0; idx--) {
181190
if (&dev->states[idx] == curr) {
182-
next = &dev->states[idx];
191+
next_index = idx;
183192
break;
184193
}
185194
}
186195

187196
/* Should never hit this condition */
188-
WARN_ON(next == NULL);
197+
WARN_ON(next_index == -1);
189198

190199
/*
191200
* Drop to next valid state.
@@ -197,37 +206,39 @@ static struct cpuidle_state *next_valid_state(struct cpuidle_device *dev,
197206
if ((cx->valid) &&
198207
(cx->mpu_state >= mpu_deepest_state) &&
199208
(cx->core_state >= core_deepest_state)) {
200-
next = &dev->states[idx];
209+
next_index = idx;
201210
break;
202211
}
203212
}
204213
/*
205214
* C1 is always valid.
206-
* So, no need to check for 'next==NULL' outside this loop.
215+
* So, no need to check for 'next_index == -1' outside
216+
* this loop.
207217
*/
208218
}
209219

210-
return next;
220+
return next_index;
211221
}
212222

213223
/**
214224
* omap3_enter_idle_bm - Checks for any bus activity
215225
* @dev: cpuidle device
216-
* @state: The target state to be programmed
226+
* @index: array index of target state to be programmed
217227
*
218228
* This function checks for any pending activity and then programs
219229
* the device to the specified or a safer state.
220230
*/
221231
static int omap3_enter_idle_bm(struct cpuidle_device *dev,
222-
struct cpuidle_state *state)
232+
int index)
223233
{
224-
struct cpuidle_state *new_state;
234+
struct cpuidle_state *state = &dev->states[index];
235+
int new_state_idx;
225236
u32 core_next_state, per_next_state = 0, per_saved_state = 0, cam_state;
226237
struct omap3_idle_statedata *cx;
227238
int ret;
228239

229240
if (!omap3_can_sleep()) {
230-
new_state = dev->safe_state;
241+
new_state_idx = dev->safe_state_index;
231242
goto select_state;
232243
}
233244

@@ -237,7 +248,7 @@ static int omap3_enter_idle_bm(struct cpuidle_device *dev,
237248
*/
238249
cam_state = pwrdm_read_pwrst(cam_pd);
239250
if (cam_state == PWRDM_POWER_ON) {
240-
new_state = dev->safe_state;
251+
new_state_idx = dev->safe_state_index;
241252
goto select_state;
242253
}
243254

@@ -264,11 +275,10 @@ static int omap3_enter_idle_bm(struct cpuidle_device *dev,
264275
if (per_next_state != per_saved_state)
265276
pwrdm_set_next_pwrst(per_pd, per_next_state);
266277

267-
new_state = next_valid_state(dev, state);
278+
new_state_idx = next_valid_state(dev, index);
268279

269280
select_state:
270-
dev->last_state = new_state;
271-
ret = omap3_enter_idle(dev, new_state);
281+
ret = omap3_enter_idle(dev, new_state_idx);
272282

273283
/* Restore original PER state if it was modified */
274284
if (per_next_state != per_saved_state)
@@ -339,11 +349,12 @@ int __init omap3_idle_init(void)
339349

340350
cpuidle_register_driver(&omap3_idle_driver);
341351
dev = &per_cpu(omap3_idle_dev, smp_processor_id());
352+
dev->safe_state_index = -1;
342353

343354
/* C1 . MPU WFI + Core active */
344355
cx = _fill_cstate(dev, 0, "MPU ON + CORE ON");
345356
(&dev->states[0])->enter = omap3_enter_idle;
346-
dev->safe_state = &dev->states[0];
357+
dev->safe_state_index = 0;
347358
cx->valid = 1; /* C1 is always valid */
348359
cx->mpu_state = PWRDM_POWER_ON;
349360
cx->core_state = PWRDM_POWER_ON;

‎arch/sh/kernel/cpu/shmobile/cpuidle.c‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ static unsigned long cpuidle_mode[] = {
2525
};
2626

2727
static int cpuidle_sleep_enter(struct cpuidle_device *dev,
28-
struct cpuidle_state *state)
28+
int index)
2929
{
3030
unsigned long allowed_mode = arch_hwblk_sleep_mode();
3131
ktime_t before, after;
32-
int requested_state = state - &dev->states[0];
32+
int requested_state = index;
3333
int allowed_state;
3434
int k;
3535

@@ -46,11 +46,13 @@ static int cpuidle_sleep_enter(struct cpuidle_device *dev,
4646
*/
4747
k = min_t(int, allowed_state, requested_state);
4848

49-
dev->last_state = &dev->states[k];
5049
before = ktime_get();
5150
sh_mobile_call_standby(cpuidle_mode[k]);
5251
after = ktime_get();
53-
return ktime_to_ns(ktime_sub(after, before)) >> 10;
52+
53+
dev->last_residency = (int)ktime_to_ns(ktime_sub(after, before)) >> 10;
54+
55+
return k;
5456
}
5557

5658
static struct cpuidle_device cpuidle_dev;
@@ -84,7 +86,7 @@ void sh_mobile_setup_cpuidle(void)
8486
state->flags |= CPUIDLE_FLAG_TIME_VALID;
8587
state->enter = cpuidle_sleep_enter;
8688

87-
dev->safe_state = state;
89+
dev->safe_state_index = i-1;
8890

8991
if (sh_mobile_sleep_supported & SUSP_SH_SF) {
9092
state = &dev->states[i++];

0 commit comments

Comments
 (0)