Skip to content

Commit ff69f2b

Browse files
alex.shilenb
authored andcommitted
acpi: fix of pmtimer overflow that make Cx states time incorrect
We found Cx states time abnormal in our some of machines which have 16 LCPUs, the C0 take too many time while system is really idle when kernel enabled tickless and highres. powertop output is below: PowerTOP version 1.9 (C) 2007 Intel Corporation Cn Avg residency P-states (frequencies) C0 (cpu running) (40.5%) 2.53 Ghz 0.0% C1 0.0ms ( 0.0%) 2.53 Ghz 0.0% C2 128.8ms (59.5%) 2.40 Ghz 0.0% 1.60 Ghz 100.0% Wakeups-from-idle per second : 4.7 interval: 20.0s no ACPI power usage estimate available Top causes for wakeups: 41.4% ( 24.9) <interrupt> : extra timer interrupt 20.2% ( 12.2) <kernel core> : usb_hcd_poll_rh_status (rh_timer_func) After tacking detailed for this issue, Yakui and I find it is due to 24 bit PM timer overflows when some of cpu sleep more than 4 seconds. With tickless kernel, the CPU want to sleep as much as possible when system idle. But the Cx sleep time are recorded by pmtimer which length is determined by BIOS. The current Cx time was gotten in the following function from driver/acpi/processor_idle.c: static inline u32 ticks_elapsed(u32 t1, u32 t2) { if (t2 >= t1) return (t2 - t1); else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER)) return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF); else return ((0xFFFFFFFF - t1) + t2); } If pmtimer is 24 bits and it take 5 seconds from t1 to t2, in above function, just about 1 seconds ticks was recorded. So the Cx time will be reduced about 4 seconds. and this is why we see above powertop output. To resolve this problem, Yakui and I use ktime_get() to record the Cx states time instead of PM timer as the following patch. the patch was tested with i386/x86_64 modes on several platforms. Acked-by: Venkatesh Pallipadi <venkatesh.pallipadi@intel.com> Tested-by: Alex Shi <alex.shi@intel.com> Signed-off-by: Alex Shi <alex.shi@intel.com> Signed-off-by: Yakui.zhao <yakui.zhao@intel.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Len Brown <len.brown@intel.com>
1 parent c15d8a6 commit ff69f2b

1 file changed

Lines changed: 27 additions & 36 deletions

File tree

‎drivers/acpi/processor_idle.c‎

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@
6464
#define _COMPONENT ACPI_PROCESSOR_COMPONENT
6565
ACPI_MODULE_NAME("processor_idle");
6666
#define ACPI_PROCESSOR_FILE_POWER "power"
67-
#define US_TO_PM_TIMER_TICKS(t) ((t * (PM_TIMER_FREQUENCY/1000)) / 1000)
6867
#define PM_TIMER_TICK_NS (1000000000ULL/PM_TIMER_FREQUENCY)
6968
#define C2_OVERHEAD 1 /* 1us */
7069
#define C3_OVERHEAD 1 /* 1us */
@@ -78,6 +77,10 @@ module_param(nocst, uint, 0000);
7877
static unsigned int latency_factor __read_mostly = 2;
7978
module_param(latency_factor, uint, 0644);
8079

80+
static s64 us_to_pm_timer_ticks(s64 t)
81+
{
82+
return div64_u64(t * PM_TIMER_FREQUENCY, 1000000);
83+
}
8184
/*
8285
* IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
8386
* For now disable this. Probably a bug somewhere else.
@@ -159,25 +162,6 @@ static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
159162
{},
160163
};
161164

162-
static inline u32 ticks_elapsed(u32 t1, u32 t2)
163-
{
164-
if (t2 >= t1)
165-
return (t2 - t1);
166-
else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
167-
return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
168-
else
169-
return ((0xFFFFFFFF - t1) + t2);
170-
}
171-
172-
static inline u32 ticks_elapsed_in_us(u32 t1, u32 t2)
173-
{
174-
if (t2 >= t1)
175-
return PM_TIMER_TICKS_TO_US(t2 - t1);
176-
else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
177-
return PM_TIMER_TICKS_TO_US(((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
178-
else
179-
return PM_TIMER_TICKS_TO_US((0xFFFFFFFF - t1) + t2);
180-
}
181165

182166
/*
183167
* Callers should disable interrupts before the call and enable
@@ -853,7 +837,8 @@ static inline void acpi_idle_do_entry(struct acpi_processor_cx *cx)
853837
static int acpi_idle_enter_c1(struct cpuidle_device *dev,
854838
struct cpuidle_state *state)
855839
{
856-
u32 t1, t2;
840+
ktime_t kt1, kt2;
841+
s64 idle_time;
857842
struct acpi_processor *pr;
858843
struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
859844

@@ -871,14 +856,15 @@ static int acpi_idle_enter_c1(struct cpuidle_device *dev,
871856
return 0;
872857
}
873858

874-
t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
859+
kt1 = ktime_get_real();
875860
acpi_idle_do_entry(cx);
876-
t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
861+
kt2 = ktime_get_real();
862+
idle_time = ktime_to_us(ktime_sub(kt2, kt1));
877863

878864
local_irq_enable();
879865
cx->usage++;
880866

881-
return ticks_elapsed_in_us(t1, t2);
867+
return idle_time;
882868
}
883869

884870
/**
@@ -891,8 +877,9 @@ static int acpi_idle_enter_simple(struct cpuidle_device *dev,
891877
{
892878
struct acpi_processor *pr;
893879
struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
894-
u32 t1, t2;
895-
int sleep_ticks = 0;
880+
ktime_t kt1, kt2;
881+
s64 idle_time;
882+
s64 sleep_ticks = 0;
896883

897884
pr = __get_cpu_var(processors);
898885

@@ -925,18 +912,19 @@ static int acpi_idle_enter_simple(struct cpuidle_device *dev,
925912
if (cx->type == ACPI_STATE_C3)
926913
ACPI_FLUSH_CPU_CACHE();
927914

928-
t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
915+
kt1 = ktime_get_real();
929916
/* Tell the scheduler that we are going deep-idle: */
930917
sched_clock_idle_sleep_event();
931918
acpi_idle_do_entry(cx);
932-
t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
919+
kt2 = ktime_get_real();
920+
idle_time = ktime_to_us(ktime_sub(kt2, kt1));
933921

934922
#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
935923
/* TSC could halt in idle, so notify users */
936924
if (tsc_halts_in_c(cx->type))
937925
mark_tsc_unstable("TSC halts in idle");;
938926
#endif
939-
sleep_ticks = ticks_elapsed(t1, t2);
927+
sleep_ticks = us_to_pm_timer_ticks(idle_time);
940928

941929
/* Tell the scheduler how much we idled: */
942930
sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
@@ -948,7 +936,7 @@ static int acpi_idle_enter_simple(struct cpuidle_device *dev,
948936

949937
acpi_state_timer_broadcast(pr, cx, 0);
950938
cx->time += sleep_ticks;
951-
return ticks_elapsed_in_us(t1, t2);
939+
return idle_time;
952940
}
953941

954942
static int c3_cpu_count;
@@ -966,8 +954,10 @@ static int acpi_idle_enter_bm(struct cpuidle_device *dev,
966954
{
967955
struct acpi_processor *pr;
968956
struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
969-
u32 t1, t2;
970-
int sleep_ticks = 0;
957+
ktime_t kt1, kt2;
958+
s64 idle_time;
959+
s64 sleep_ticks = 0;
960+
971961

972962
pr = __get_cpu_var(processors);
973963

@@ -1034,9 +1024,10 @@ static int acpi_idle_enter_bm(struct cpuidle_device *dev,
10341024
ACPI_FLUSH_CPU_CACHE();
10351025
}
10361026

1037-
t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1027+
kt1 = ktime_get_real();
10381028
acpi_idle_do_entry(cx);
1039-
t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1029+
kt2 = ktime_get_real();
1030+
idle_time = ktime_to_us(ktime_sub(kt2, kt1));
10401031

10411032
/* Re-enable bus master arbitration */
10421033
if (pr->flags.bm_check && pr->flags.bm_control) {
@@ -1051,7 +1042,7 @@ static int acpi_idle_enter_bm(struct cpuidle_device *dev,
10511042
if (tsc_halts_in_c(ACPI_STATE_C3))
10521043
mark_tsc_unstable("TSC halts in idle");
10531044
#endif
1054-
sleep_ticks = ticks_elapsed(t1, t2);
1045+
sleep_ticks = us_to_pm_timer_ticks(idle_time);
10551046
/* Tell the scheduler how much we idled: */
10561047
sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
10571048

@@ -1062,7 +1053,7 @@ static int acpi_idle_enter_bm(struct cpuidle_device *dev,
10621053

10631054
acpi_state_timer_broadcast(pr, cx, 0);
10641055
cx->time += sleep_ticks;
1065-
return ticks_elapsed_in_us(t1, t2);
1056+
return idle_time;
10661057
}
10671058

10681059
struct cpuidle_driver acpi_idle_driver = {

0 commit comments

Comments
 (0)