4141
4242#define pr_fmt (fmt ) KBUILD_MODNAME ": " fmt
4343
44+ #include <linux/acpi.h>
4445#include <linux/kernel.h>
4546#include <linux/cpuidle.h>
4647#include <linux/tick.h>
@@ -1111,6 +1112,129 @@ static const struct x86_cpu_id intel_idle_ids[] __initconst = {
11111112 {}
11121113};
11131114
1115+ #define INTEL_CPU_FAM6_MWAIT \
1116+ { X86_VENDOR_INTEL, 6, X86_MODEL_ANY, X86_FEATURE_MWAIT, 0 }
1117+
1118+ static const struct x86_cpu_id intel_mwait_ids [] __initconst = {
1119+ INTEL_CPU_FAM6_MWAIT ,
1120+ {}
1121+ };
1122+
1123+ static bool intel_idle_max_cstate_reached (int cstate )
1124+ {
1125+ if (cstate + 1 > max_cstate ) {
1126+ pr_info ("max_cstate %d reached\n" , max_cstate );
1127+ return true;
1128+ }
1129+ return false;
1130+ }
1131+
1132+ #ifdef CONFIG_ACPI_PROCESSOR_CSTATE
1133+ #include <acpi/processor.h>
1134+
1135+ static struct acpi_processor_power acpi_state_table ;
1136+
1137+ /**
1138+ * intel_idle_cst_usable - Check if the _CST information can be used.
1139+ *
1140+ * Check if all of the C-states listed by _CST in the max_cstate range are
1141+ * ACPI_CSTATE_FFH, which means that they should be entered via MWAIT.
1142+ */
1143+ static bool intel_idle_cst_usable (void )
1144+ {
1145+ int cstate , limit ;
1146+
1147+ limit = min_t (int , min_t (int , CPUIDLE_STATE_MAX , max_cstate + 1 ),
1148+ acpi_state_table .count );
1149+
1150+ for (cstate = 1 ; cstate < limit ; cstate ++ ) {
1151+ struct acpi_processor_cx * cx = & acpi_state_table .states [cstate ];
1152+
1153+ if (cx -> entry_method != ACPI_CSTATE_FFH )
1154+ return false;
1155+ }
1156+
1157+ return true;
1158+ }
1159+
1160+ static bool intel_idle_acpi_cst_extract (void )
1161+ {
1162+ unsigned int cpu ;
1163+
1164+ for_each_possible_cpu (cpu ) {
1165+ struct acpi_processor * pr = per_cpu (processors , cpu );
1166+
1167+ if (!pr )
1168+ continue ;
1169+
1170+ if (acpi_processor_evaluate_cst (pr -> handle , cpu , & acpi_state_table ))
1171+ continue ;
1172+
1173+ acpi_state_table .count ++ ;
1174+
1175+ if (!intel_idle_cst_usable ())
1176+ continue ;
1177+
1178+ if (!acpi_processor_claim_cst_control ()) {
1179+ acpi_state_table .count = 0 ;
1180+ return false;
1181+ }
1182+
1183+ return true;
1184+ }
1185+
1186+ pr_debug ("ACPI _CST not found or not usable\n" );
1187+ return false;
1188+ }
1189+
1190+ static void intel_idle_init_cstates_acpi (struct cpuidle_driver * drv )
1191+ {
1192+ int cstate , limit = min_t (int , CPUIDLE_STATE_MAX , acpi_state_table .count );
1193+
1194+ /*
1195+ * If limit > 0, intel_idle_cst_usable() has returned 'true', so all of
1196+ * the interesting states are ACPI_CSTATE_FFH.
1197+ */
1198+ for (cstate = 1 ; cstate < limit ; cstate ++ ) {
1199+ struct acpi_processor_cx * cx ;
1200+ struct cpuidle_state * state ;
1201+
1202+ if (intel_idle_max_cstate_reached (cstate ))
1203+ break ;
1204+
1205+ cx = & acpi_state_table .states [cstate ];
1206+
1207+ state = & drv -> states [drv -> state_count ++ ];
1208+
1209+ snprintf (state -> name , CPUIDLE_NAME_LEN , "C%d_ACPI" , cstate );
1210+ strlcpy (state -> desc , cx -> desc , CPUIDLE_DESC_LEN );
1211+ state -> exit_latency = cx -> latency ;
1212+ /*
1213+ * For C1-type C-states use the same number for both the exit
1214+ * latency and target residency, because that is the case for
1215+ * C1 in the majority of the static C-states tables above.
1216+ * For the other types of C-states, however, set the target
1217+ * residency to 3 times the exit latency which should lead to
1218+ * a reasonable balance between energy-efficiency and
1219+ * performance in the majority of interesting cases.
1220+ */
1221+ state -> target_residency = cx -> latency ;
1222+ if (cx -> type > ACPI_STATE_C1 )
1223+ state -> target_residency *= 3 ;
1224+
1225+ state -> flags = MWAIT2flg (cx -> address );
1226+ if (cx -> type > ACPI_STATE_C2 )
1227+ state -> flags |= CPUIDLE_FLAG_TLB_FLUSHED ;
1228+
1229+ state -> enter = intel_idle ;
1230+ state -> enter_s2idle = intel_idle_s2idle ;
1231+ }
1232+ }
1233+ #else /* !CONFIG_ACPI_PROCESSOR_CSTATE */
1234+ static inline bool intel_idle_acpi_cst_extract (void ) { return false; }
1235+ static inline void intel_idle_init_cstates_acpi (struct cpuidle_driver * drv ) { }
1236+ #endif /* !CONFIG_ACPI_PROCESSOR_CSTATE */
1237+
11141238/*
11151239 * intel_idle_probe()
11161240 */
@@ -1125,17 +1249,15 @@ static int __init intel_idle_probe(void)
11251249 }
11261250
11271251 id = x86_match_cpu (intel_idle_ids );
1128- if (!id ) {
1129- if (boot_cpu_data .x86_vendor == X86_VENDOR_INTEL &&
1130- boot_cpu_data .x86 == 6 )
1131- pr_debug ("does not run on family %d model %d\n" ,
1132- boot_cpu_data .x86 , boot_cpu_data .x86_model );
1133- return - ENODEV ;
1134- }
1135-
1136- if (!boot_cpu_has (X86_FEATURE_MWAIT )) {
1137- pr_debug ("Please enable MWAIT in BIOS SETUP\n" );
1138- return - ENODEV ;
1252+ if (id ) {
1253+ if (!boot_cpu_has (X86_FEATURE_MWAIT )) {
1254+ pr_debug ("Please enable MWAIT in BIOS SETUP\n" );
1255+ return - ENODEV ;
1256+ }
1257+ } else {
1258+ id = x86_match_cpu (intel_mwait_ids );
1259+ if (!id )
1260+ return - ENODEV ;
11391261 }
11401262
11411263 if (boot_cpu_data .cpuid_level < CPUID_MWAIT_LEAF )
@@ -1151,7 +1273,10 @@ static int __init intel_idle_probe(void)
11511273 pr_debug ("MWAIT substates: 0x%x\n" , mwait_substates );
11521274
11531275 icpu = (const struct idle_cpu * )id -> driver_data ;
1154- cpuidle_state_table = icpu -> state_table ;
1276+ if (icpu )
1277+ cpuidle_state_table = icpu -> state_table ;
1278+ else if (!intel_idle_acpi_cst_extract ())
1279+ return - ENODEV ;
11551280
11561281 pr_debug ("v" INTEL_IDLE_VERSION " model 0x%X\n" ,
11571282 boot_cpu_data .x86_model );
@@ -1333,31 +1458,19 @@ static void intel_idle_state_table_update(void)
13331458 }
13341459}
13351460
1336- /*
1337- * intel_idle_cpuidle_driver_init()
1338- * allocate, initialize cpuidle_states
1339- */
1340- static void __init intel_idle_cpuidle_driver_init (void )
1461+ static void intel_idle_init_cstates_icpu (struct cpuidle_driver * drv )
13411462{
13421463 int cstate ;
1343- struct cpuidle_driver * drv = & intel_idle_driver ;
1344-
1345- intel_idle_state_table_update ();
1346-
1347- cpuidle_poll_state_init (drv );
1348- drv -> state_count = 1 ;
13491464
13501465 for (cstate = 0 ; cstate < CPUIDLE_STATE_MAX ; ++ cstate ) {
13511466 unsigned int mwait_hint ;
13521467
1353- if (!cpuidle_state_table [cstate ].enter &&
1354- !cpuidle_state_table [cstate ].enter_s2idle )
1468+ if (intel_idle_max_cstate_reached (cstate ))
13551469 break ;
13561470
1357- if (cstate + 1 > max_cstate ) {
1358- pr_info ( "max_cstate %d reached\n" , max_cstate );
1471+ if (! cpuidle_state_table [ cstate ]. enter &&
1472+ ! cpuidle_state_table [ cstate ]. enter_s2idle )
13591473 break ;
1360- }
13611474
13621475 /* If marked as unusable, skip this state. */
13631476 if (cpuidle_state_table [cstate ].flags & CPUIDLE_FLAG_UNUSABLE ) {
@@ -1380,6 +1493,24 @@ static void __init intel_idle_cpuidle_driver_init(void)
13801493 }
13811494}
13821495
1496+ /*
1497+ * intel_idle_cpuidle_driver_init()
1498+ * allocate, initialize cpuidle_states
1499+ */
1500+ static void __init intel_idle_cpuidle_driver_init (void )
1501+ {
1502+ struct cpuidle_driver * drv = & intel_idle_driver ;
1503+
1504+ intel_idle_state_table_update ();
1505+
1506+ cpuidle_poll_state_init (drv );
1507+ drv -> state_count = 1 ;
1508+
1509+ if (icpu )
1510+ intel_idle_init_cstates_icpu (drv );
1511+ else
1512+ intel_idle_init_cstates_acpi (drv );
1513+ }
13831514
13841515/*
13851516 * intel_idle_cpu_init()
@@ -1398,6 +1529,9 @@ static int intel_idle_cpu_init(unsigned int cpu)
13981529 return - EIO ;
13991530 }
14001531
1532+ if (!icpu )
1533+ return 0 ;
1534+
14011535 if (icpu -> auto_demotion_disable_flags )
14021536 auto_demotion_disable ();
14031537
0 commit comments