1c942fddfSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * processor_idle - idle state submodule to the ACPI processor driver 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> 61da177e4SLinus Torvalds * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> 7c5ab81caSDominik Brodowski * Copyright (C) 2004, 2005 Dominik Brodowski <linux@brodo.de> 81da177e4SLinus Torvalds * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com> 91da177e4SLinus Torvalds * - Added processor hotplug support 1002df8b93SVenkatesh Pallipadi * Copyright (C) 2005 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com> 1102df8b93SVenkatesh Pallipadi * - Added support for C3 on SMP 121da177e4SLinus Torvalds */ 13b6ec26fbSSudeep Holla #define pr_fmt(fmt) "ACPI: " fmt 141da177e4SLinus Torvalds 151da177e4SLinus Torvalds #include <linux/module.h> 161da177e4SLinus Torvalds #include <linux/acpi.h> 171da177e4SLinus Torvalds #include <linux/dmi.h> 184e57b681STim Schmielau #include <linux/sched.h> /* need_resched() */ 19ee41eebfSThomas Gleixner #include <linux/tick.h> 204f86d3a8SLen Brown #include <linux/cpuidle.h> 216727ad9eSChris Metcalf #include <linux/cpu.h> 228b48463fSLv Zheng #include <acpi/processor.h> 231da177e4SLinus Torvalds 243434933bSThomas Gleixner /* 253434933bSThomas Gleixner * Include the apic definitions for x86 to have the APIC timer related defines 263434933bSThomas Gleixner * available also for UP (on SMP it gets magically included via linux/smp.h). 273434933bSThomas Gleixner * asm/acpi.h is not an option, as it would require more include magic. Also 283434933bSThomas Gleixner * creating an empty asm-ia64/apic.h would just trade pest vs. cholera. 293434933bSThomas Gleixner */ 303434933bSThomas Gleixner #ifdef CONFIG_X86 313434933bSThomas Gleixner #include <asm/apic.h> 323434933bSThomas Gleixner #endif 333434933bSThomas Gleixner 341da177e4SLinus Torvalds #define ACPI_PROCESSOR_CLASS "processor" 351da177e4SLinus Torvalds #define _COMPONENT ACPI_PROCESSOR_COMPONENT 36f52fd66dSLen Brown ACPI_MODULE_NAME("processor_idle"); 371da177e4SLinus Torvalds 38dc2251bfSRafael J. Wysocki #define ACPI_IDLE_STATE_START (IS_ENABLED(CONFIG_ARCH_HAS_CPU_RELAX) ? 1 : 0) 39dc2251bfSRafael J. Wysocki 404f86d3a8SLen Brown static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER; 414f86d3a8SLen Brown module_param(max_cstate, uint, 0000); 42b6835052SAndreas Mohr static unsigned int nocst __read_mostly; 431da177e4SLinus Torvalds module_param(nocst, uint, 0000); 44d3e7e99fSLen Brown static int bm_check_disable __read_mostly; 45d3e7e99fSLen Brown module_param(bm_check_disable, uint, 0000); 461da177e4SLinus Torvalds 4725de5718SLen Brown static unsigned int latency_factor __read_mostly = 2; 484963f620SLen Brown module_param(latency_factor, uint, 0644); 491da177e4SLinus Torvalds 503d339dcbSDaniel Lezcano static DEFINE_PER_CPU(struct cpuidle_device *, acpi_cpuidle_device); 513d339dcbSDaniel Lezcano 5235ae7133SSudeep Holla struct cpuidle_driver acpi_idle_driver = { 5335ae7133SSudeep Holla .name = "acpi_idle", 5435ae7133SSudeep Holla .owner = THIS_MODULE, 5535ae7133SSudeep Holla }; 5635ae7133SSudeep Holla 5735ae7133SSudeep Holla #ifdef CONFIG_ACPI_PROCESSOR_CSTATE 5825528213SPeter Zijlstra static 5925528213SPeter Zijlstra DEFINE_PER_CPU(struct acpi_processor_cx * [CPUIDLE_STATE_MAX], acpi_cstate); 60ac3ebafaSDaniel Lezcano 61d1896049SThomas Renninger static int disabled_by_idle_boot_param(void) 62d1896049SThomas Renninger { 63d1896049SThomas Renninger return boot_option_idle_override == IDLE_POLL || 64d1896049SThomas Renninger boot_option_idle_override == IDLE_HALT; 65d1896049SThomas Renninger } 66d1896049SThomas Renninger 671da177e4SLinus Torvalds /* 681da177e4SLinus Torvalds * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3. 691da177e4SLinus Torvalds * For now disable this. Probably a bug somewhere else. 701da177e4SLinus Torvalds * 711da177e4SLinus Torvalds * To skip this limit, boot/load with a large max_cstate limit. 721da177e4SLinus Torvalds */ 731855256cSJeff Garzik static int set_max_cstate(const struct dmi_system_id *id) 741da177e4SLinus Torvalds { 751da177e4SLinus Torvalds if (max_cstate > ACPI_PROCESSOR_MAX_POWER) 761da177e4SLinus Torvalds return 0; 771da177e4SLinus Torvalds 78b6ec26fbSSudeep Holla pr_notice("%s detected - limiting to C%ld max_cstate." 791da177e4SLinus Torvalds " Override with \"processor.max_cstate=%d\"\n", id->ident, 803d35600aSLen Brown (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1); 811da177e4SLinus Torvalds 823d35600aSLen Brown max_cstate = (long)id->driver_data; 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds return 0; 851da177e4SLinus Torvalds } 861da177e4SLinus Torvalds 87b0346688SMathias Krause static const struct dmi_system_id processor_power_dmi_table[] = { 88335f16beSDavid Shaohua Li { set_max_cstate, "Clevo 5600D", { 89876c184bSThomas Rosner DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"), 90876c184bSThomas Rosner DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")}, 91335f16beSDavid Shaohua Li (void *)2}, 92370d5cd8SArjan van de Ven { set_max_cstate, "Pavilion zv5000", { 93370d5cd8SArjan van de Ven DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), 94370d5cd8SArjan van de Ven DMI_MATCH(DMI_PRODUCT_NAME,"Pavilion zv5000 (DS502A#ABA)")}, 95370d5cd8SArjan van de Ven (void *)1}, 96370d5cd8SArjan van de Ven { set_max_cstate, "Asus L8400B", { 97370d5cd8SArjan van de Ven DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."), 98370d5cd8SArjan van de Ven DMI_MATCH(DMI_PRODUCT_NAME,"L8400B series Notebook PC")}, 99370d5cd8SArjan van de Ven (void *)1}, 1001da177e4SLinus Torvalds {}, 1011da177e4SLinus Torvalds }; 1021da177e4SLinus Torvalds 1034f86d3a8SLen Brown 1042e906655Svenkatesh.pallipadi@intel.com /* 1052e906655Svenkatesh.pallipadi@intel.com * Callers should disable interrupts before the call and enable 1062e906655Svenkatesh.pallipadi@intel.com * interrupts after return. 1072e906655Svenkatesh.pallipadi@intel.com */ 1086727ad9eSChris Metcalf static void __cpuidle acpi_safe_halt(void) 109ddc081a1SVenkatesh Pallipadi { 110ea811747SPeter Zijlstra if (!tif_need_resched()) { 111ddc081a1SVenkatesh Pallipadi safe_halt(); 11271e93d15SVenki Pallipadi local_irq_disable(); 11371e93d15SVenki Pallipadi } 114ddc081a1SVenkatesh Pallipadi } 115ddc081a1SVenkatesh Pallipadi 116169a0abbSThomas Gleixner #ifdef ARCH_APICTIMER_STOPS_ON_C3 117169a0abbSThomas Gleixner 118169a0abbSThomas Gleixner /* 119169a0abbSThomas Gleixner * Some BIOS implementations switch to C3 in the published C2 state. 120296d93cdSLinus Torvalds * This seems to be a common problem on AMD boxen, but other vendors 121296d93cdSLinus Torvalds * are affected too. We pick the most conservative approach: we assume 122296d93cdSLinus Torvalds * that the local APIC stops in both C2 and C3. 123169a0abbSThomas Gleixner */ 1247e275cc4SLen Brown static void lapic_timer_check_state(int state, struct acpi_processor *pr, 125169a0abbSThomas Gleixner struct acpi_processor_cx *cx) 126169a0abbSThomas Gleixner { 127169a0abbSThomas Gleixner struct acpi_processor_power *pwr = &pr->power; 128e585bef8SThomas Gleixner u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2; 129169a0abbSThomas Gleixner 130db954b58SVenkatesh Pallipadi if (cpu_has(&cpu_data(pr->id), X86_FEATURE_ARAT)) 131db954b58SVenkatesh Pallipadi return; 132db954b58SVenkatesh Pallipadi 13307c94a38SBorislav Petkov if (boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E)) 13487ad57baSShaohua Li type = ACPI_STATE_C1; 13587ad57baSShaohua Li 136169a0abbSThomas Gleixner /* 137169a0abbSThomas Gleixner * Check, if one of the previous states already marked the lapic 138169a0abbSThomas Gleixner * unstable 139169a0abbSThomas Gleixner */ 140169a0abbSThomas Gleixner if (pwr->timer_broadcast_on_state < state) 141169a0abbSThomas Gleixner return; 142169a0abbSThomas Gleixner 143e585bef8SThomas Gleixner if (cx->type >= type) 144169a0abbSThomas Gleixner pr->power.timer_broadcast_on_state = state; 145169a0abbSThomas Gleixner } 146169a0abbSThomas Gleixner 147918aae42SHidetoshi Seto static void __lapic_timer_propagate_broadcast(void *arg) 148169a0abbSThomas Gleixner { 149f833bab8SSuresh Siddha struct acpi_processor *pr = (struct acpi_processor *) arg; 150e9e2cdb4SThomas Gleixner 151ee41eebfSThomas Gleixner if (pr->power.timer_broadcast_on_state < INT_MAX) 152ee41eebfSThomas Gleixner tick_broadcast_enable(); 153ee41eebfSThomas Gleixner else 154ee41eebfSThomas Gleixner tick_broadcast_disable(); 155e9e2cdb4SThomas Gleixner } 156e9e2cdb4SThomas Gleixner 157918aae42SHidetoshi Seto static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) 158918aae42SHidetoshi Seto { 159918aae42SHidetoshi Seto smp_call_function_single(pr->id, __lapic_timer_propagate_broadcast, 160918aae42SHidetoshi Seto (void *)pr, 1); 161918aae42SHidetoshi Seto } 162918aae42SHidetoshi Seto 163e9e2cdb4SThomas Gleixner /* Power(C) State timer broadcast control */ 1647e275cc4SLen Brown static void lapic_timer_state_broadcast(struct acpi_processor *pr, 165e9e2cdb4SThomas Gleixner struct acpi_processor_cx *cx, 166e9e2cdb4SThomas Gleixner int broadcast) 167e9e2cdb4SThomas Gleixner { 168e9e2cdb4SThomas Gleixner int state = cx - pr->power.states; 169e9e2cdb4SThomas Gleixner 170e9e2cdb4SThomas Gleixner if (state >= pr->power.timer_broadcast_on_state) { 1717815701cSThomas Gleixner if (broadcast) 1727815701cSThomas Gleixner tick_broadcast_enter(); 1737815701cSThomas Gleixner else 1747815701cSThomas Gleixner tick_broadcast_exit(); 175e9e2cdb4SThomas Gleixner } 176169a0abbSThomas Gleixner } 177169a0abbSThomas Gleixner 178169a0abbSThomas Gleixner #else 179169a0abbSThomas Gleixner 1807e275cc4SLen Brown static void lapic_timer_check_state(int state, struct acpi_processor *pr, 181169a0abbSThomas Gleixner struct acpi_processor_cx *cstate) { } 1827e275cc4SLen Brown static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) { } 1837e275cc4SLen Brown static void lapic_timer_state_broadcast(struct acpi_processor *pr, 184e9e2cdb4SThomas Gleixner struct acpi_processor_cx *cx, 185e9e2cdb4SThomas Gleixner int broadcast) 186e9e2cdb4SThomas Gleixner { 187e9e2cdb4SThomas Gleixner } 188169a0abbSThomas Gleixner 189169a0abbSThomas Gleixner #endif 190169a0abbSThomas Gleixner 191592913ecSJohn Stultz #if defined(CONFIG_X86) 192520daf72SLen Brown static void tsc_check_state(int state) 193ddb25f9aSAndi Kleen { 194ddb25f9aSAndi Kleen switch (boot_cpu_data.x86_vendor) { 1957377ed4bSPu Wen case X86_VENDOR_HYGON: 196ddb25f9aSAndi Kleen case X86_VENDOR_AMD: 19740fb1715SVenki Pallipadi case X86_VENDOR_INTEL: 198fe6daab1Sdavidwang case X86_VENDOR_CENTAUR: 199773b2f30STony W Wang-oc case X86_VENDOR_ZHAOXIN: 200ddb25f9aSAndi Kleen /* 201ddb25f9aSAndi Kleen * AMD Fam10h TSC will tick in all 202ddb25f9aSAndi Kleen * C/P/S0/S1 states when this bit is set. 203ddb25f9aSAndi Kleen */ 20440fb1715SVenki Pallipadi if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC)) 205520daf72SLen Brown return; 20657d2dd4bSGustavo A. R. Silva fallthrough; 207ddb25f9aSAndi Kleen default: 208520daf72SLen Brown /* TSC could halt in idle, so notify users */ 209520daf72SLen Brown if (state > ACPI_STATE_C1) 210520daf72SLen Brown mark_tsc_unstable("TSC halts in idle"); 211ddb25f9aSAndi Kleen } 212ddb25f9aSAndi Kleen } 213520daf72SLen Brown #else 214520daf72SLen Brown static void tsc_check_state(int state) { return; } 215ddb25f9aSAndi Kleen #endif 216ddb25f9aSAndi Kleen 2171da177e4SLinus Torvalds static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr) 2181da177e4SLinus Torvalds { 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds if (!pr->pblk) 221d550d98dSPatrick Mochel return -ENODEV; 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds /* if info is obtained from pblk/fadt, type equals state */ 2241da177e4SLinus Torvalds pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2; 2251da177e4SLinus Torvalds pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3; 2261da177e4SLinus Torvalds 2274c033552SVenkatesh Pallipadi #ifndef CONFIG_HOTPLUG_CPU 2284c033552SVenkatesh Pallipadi /* 2294c033552SVenkatesh Pallipadi * Check for P_LVL2_UP flag before entering C2 and above on 2304c033552SVenkatesh Pallipadi * an SMP system. 2314c033552SVenkatesh Pallipadi */ 232ad71860aSAlexey Starikovskiy if ((num_online_cpus() > 1) && 233cee324b1SAlexey Starikovskiy !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED)) 234d550d98dSPatrick Mochel return -ENODEV; 2354c033552SVenkatesh Pallipadi #endif 2364c033552SVenkatesh Pallipadi 2371da177e4SLinus Torvalds /* determine C2 and C3 address from pblk */ 2381da177e4SLinus Torvalds pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4; 2391da177e4SLinus Torvalds pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5; 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds /* determine latencies from FADT */ 242ba494beeSBob Moore pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.c2_latency; 243ba494beeSBob Moore pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.c3_latency; 2441da177e4SLinus Torvalds 2455d76b6f6SLen Brown /* 2465d76b6f6SLen Brown * FADT specified C2 latency must be less than or equal to 2475d76b6f6SLen Brown * 100 microseconds. 2485d76b6f6SLen Brown */ 249ba494beeSBob Moore if (acpi_gbl_FADT.c2_latency > ACPI_PROCESSOR_MAX_C2_LATENCY) { 2505d76b6f6SLen Brown ACPI_DEBUG_PRINT((ACPI_DB_INFO, 251ba494beeSBob Moore "C2 latency too large [%d]\n", acpi_gbl_FADT.c2_latency)); 2525d76b6f6SLen Brown /* invalidate C2 */ 2535d76b6f6SLen Brown pr->power.states[ACPI_STATE_C2].address = 0; 2545d76b6f6SLen Brown } 2555d76b6f6SLen Brown 256a6d72c18SLen Brown /* 257a6d72c18SLen Brown * FADT supplied C3 latency must be less than or equal to 258a6d72c18SLen Brown * 1000 microseconds. 259a6d72c18SLen Brown */ 260ba494beeSBob Moore if (acpi_gbl_FADT.c3_latency > ACPI_PROCESSOR_MAX_C3_LATENCY) { 261a6d72c18SLen Brown ACPI_DEBUG_PRINT((ACPI_DB_INFO, 262ba494beeSBob Moore "C3 latency too large [%d]\n", acpi_gbl_FADT.c3_latency)); 263a6d72c18SLen Brown /* invalidate C3 */ 264a6d72c18SLen Brown pr->power.states[ACPI_STATE_C3].address = 0; 265a6d72c18SLen Brown } 266a6d72c18SLen Brown 2671da177e4SLinus Torvalds ACPI_DEBUG_PRINT((ACPI_DB_INFO, 2681da177e4SLinus Torvalds "lvl2[0x%08x] lvl3[0x%08x]\n", 2691da177e4SLinus Torvalds pr->power.states[ACPI_STATE_C2].address, 2701da177e4SLinus Torvalds pr->power.states[ACPI_STATE_C3].address)); 2711da177e4SLinus Torvalds 27234a62cd0SYazen Ghannam snprintf(pr->power.states[ACPI_STATE_C2].desc, 27334a62cd0SYazen Ghannam ACPI_CX_DESC_LEN, "ACPI P_LVL2 IOPORT 0x%x", 27434a62cd0SYazen Ghannam pr->power.states[ACPI_STATE_C2].address); 27534a62cd0SYazen Ghannam snprintf(pr->power.states[ACPI_STATE_C3].desc, 27634a62cd0SYazen Ghannam ACPI_CX_DESC_LEN, "ACPI P_LVL3 IOPORT 0x%x", 27734a62cd0SYazen Ghannam pr->power.states[ACPI_STATE_C3].address); 27834a62cd0SYazen Ghannam 279d550d98dSPatrick Mochel return 0; 2801da177e4SLinus Torvalds } 2811da177e4SLinus Torvalds 282991528d7SVenkatesh Pallipadi static int acpi_processor_get_power_info_default(struct acpi_processor *pr) 283acf05f4bSVenkatesh Pallipadi { 284991528d7SVenkatesh Pallipadi if (!pr->power.states[ACPI_STATE_C1].valid) { 285cf824788SJanosch Machowinski /* set the first C-State to C1 */ 286991528d7SVenkatesh Pallipadi /* all processors need to support C1 */ 287acf05f4bSVenkatesh Pallipadi pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1; 288acf05f4bSVenkatesh Pallipadi pr->power.states[ACPI_STATE_C1].valid = 1; 2890fda6b40SVenkatesh Pallipadi pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT; 290248e8841SYazen Ghannam 291248e8841SYazen Ghannam snprintf(pr->power.states[ACPI_STATE_C1].desc, 292248e8841SYazen Ghannam ACPI_CX_DESC_LEN, "ACPI HLT"); 293991528d7SVenkatesh Pallipadi } 294991528d7SVenkatesh Pallipadi /* the C0 state only exists as a filler in our array */ 295991528d7SVenkatesh Pallipadi pr->power.states[ACPI_STATE_C0].valid = 1; 296d550d98dSPatrick Mochel return 0; 297acf05f4bSVenkatesh Pallipadi } 298acf05f4bSVenkatesh Pallipadi 299987c7853SRafael J. Wysocki static int acpi_processor_get_power_info_cst(struct acpi_processor *pr) 300987c7853SRafael J. Wysocki { 301987c7853SRafael J. Wysocki int ret; 302987c7853SRafael J. Wysocki 303987c7853SRafael J. Wysocki if (nocst) 304987c7853SRafael J. Wysocki return -ENODEV; 305987c7853SRafael J. Wysocki 306987c7853SRafael J. Wysocki ret = acpi_processor_evaluate_cst(pr->handle, pr->id, &pr->power); 307987c7853SRafael J. Wysocki if (ret) 308987c7853SRafael J. Wysocki return ret; 309987c7853SRafael J. Wysocki 310496121c0SZhang Rui if (!pr->power.count) 311987c7853SRafael J. Wysocki return -EFAULT; 312987c7853SRafael J. Wysocki 313987c7853SRafael J. Wysocki pr->flags.has_cst = 1; 314987c7853SRafael J. Wysocki return 0; 315987c7853SRafael J. Wysocki } 316987c7853SRafael J. Wysocki 3174be44fcdSLen Brown static void acpi_processor_power_verify_c3(struct acpi_processor *pr, 3181da177e4SLinus Torvalds struct acpi_processor_cx *cx) 3191da177e4SLinus Torvalds { 320ee1ca48fSPallipadi, Venkatesh static int bm_check_flag = -1; 321ee1ca48fSPallipadi, Venkatesh static int bm_control_flag = -1; 32202df8b93SVenkatesh Pallipadi 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds if (!cx->address) 325d550d98dSPatrick Mochel return; 3261da177e4SLinus Torvalds 3271da177e4SLinus Torvalds /* 3281da177e4SLinus Torvalds * PIIX4 Erratum #18: We don't support C3 when Type-F (fast) 3291da177e4SLinus Torvalds * DMA transfers are used by any ISA device to avoid livelock. 3301da177e4SLinus Torvalds * Note that we could disable Type-F DMA (as recommended by 3311da177e4SLinus Torvalds * the erratum), but this is known to disrupt certain ISA 3321da177e4SLinus Torvalds * devices thus we take the conservative approach. 3331da177e4SLinus Torvalds */ 3341da177e4SLinus Torvalds else if (errata.piix4.fdma) { 3351da177e4SLinus Torvalds ACPI_DEBUG_PRINT((ACPI_DB_INFO, 3361da177e4SLinus Torvalds "C3 not supported on PIIX4 with Type-F DMA\n")); 337d550d98dSPatrick Mochel return; 3381da177e4SLinus Torvalds } 3391da177e4SLinus Torvalds 34002df8b93SVenkatesh Pallipadi /* All the logic here assumes flags.bm_check is same across all CPUs */ 341ee1ca48fSPallipadi, Venkatesh if (bm_check_flag == -1) { 34202df8b93SVenkatesh Pallipadi /* Determine whether bm_check is needed based on CPU */ 34302df8b93SVenkatesh Pallipadi acpi_processor_power_init_bm_check(&(pr->flags), pr->id); 34402df8b93SVenkatesh Pallipadi bm_check_flag = pr->flags.bm_check; 345ee1ca48fSPallipadi, Venkatesh bm_control_flag = pr->flags.bm_control; 34602df8b93SVenkatesh Pallipadi } else { 34702df8b93SVenkatesh Pallipadi pr->flags.bm_check = bm_check_flag; 348ee1ca48fSPallipadi, Venkatesh pr->flags.bm_control = bm_control_flag; 34902df8b93SVenkatesh Pallipadi } 35002df8b93SVenkatesh Pallipadi 35102df8b93SVenkatesh Pallipadi if (pr->flags.bm_check) { 35202df8b93SVenkatesh Pallipadi if (!pr->flags.bm_control) { 353ed3110efSVenki Pallipadi if (pr->flags.has_cst != 1) { 354ed3110efSVenki Pallipadi /* bus mastering control is necessary */ 35502df8b93SVenkatesh Pallipadi ACPI_DEBUG_PRINT((ACPI_DB_INFO, 356ed3110efSVenki Pallipadi "C3 support requires BM control\n")); 357ed3110efSVenki Pallipadi return; 358ed3110efSVenki Pallipadi } else { 359ed3110efSVenki Pallipadi /* Here we enter C3 without bus mastering */ 360ed3110efSVenki Pallipadi ACPI_DEBUG_PRINT((ACPI_DB_INFO, 361ed3110efSVenki Pallipadi "C3 support without BM control\n")); 362ed3110efSVenki Pallipadi } 36302df8b93SVenkatesh Pallipadi } 36402df8b93SVenkatesh Pallipadi } else { 36502df8b93SVenkatesh Pallipadi /* 36602df8b93SVenkatesh Pallipadi * WBINVD should be set in fadt, for C3 state to be 36702df8b93SVenkatesh Pallipadi * supported on when bm_check is not required. 36802df8b93SVenkatesh Pallipadi */ 369cee324b1SAlexey Starikovskiy if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) { 37002df8b93SVenkatesh Pallipadi ACPI_DEBUG_PRINT((ACPI_DB_INFO, 37102df8b93SVenkatesh Pallipadi "Cache invalidation should work properly" 37202df8b93SVenkatesh Pallipadi " for C3 to be enabled on SMP systems\n")); 373d550d98dSPatrick Mochel return; 37402df8b93SVenkatesh Pallipadi } 37502df8b93SVenkatesh Pallipadi } 37602df8b93SVenkatesh Pallipadi 3771da177e4SLinus Torvalds /* 3781da177e4SLinus Torvalds * Otherwise we've met all of our C3 requirements. 3791da177e4SLinus Torvalds * Normalize the C3 latency to expidite policy. Enable 3801da177e4SLinus Torvalds * checking of bus mastering status (bm_check) so we can 3811da177e4SLinus Torvalds * use this in our C3 policy 3821da177e4SLinus Torvalds */ 3831da177e4SLinus Torvalds cx->valid = 1; 3844f86d3a8SLen Brown 38531878dd8SLen Brown /* 38631878dd8SLen Brown * On older chipsets, BM_RLD needs to be set 38731878dd8SLen Brown * in order for Bus Master activity to wake the 38831878dd8SLen Brown * system from C3. Newer chipsets handle DMA 38931878dd8SLen Brown * during C3 automatically and BM_RLD is a NOP. 39031878dd8SLen Brown * In either case, the proper way to 39131878dd8SLen Brown * handle BM_RLD is to set it and leave it set. 39231878dd8SLen Brown */ 39350ffba1bSBob Moore acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1); 3941da177e4SLinus Torvalds 395d550d98dSPatrick Mochel return; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds 3981da177e4SLinus Torvalds static int acpi_processor_power_verify(struct acpi_processor *pr) 3991da177e4SLinus Torvalds { 4001da177e4SLinus Torvalds unsigned int i; 4011da177e4SLinus Torvalds unsigned int working = 0; 4026eb0a0fdSVenkatesh Pallipadi 403169a0abbSThomas Gleixner pr->power.timer_broadcast_on_state = INT_MAX; 4046eb0a0fdSVenkatesh Pallipadi 405a0bf284bSLen Brown for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) { 4061da177e4SLinus Torvalds struct acpi_processor_cx *cx = &pr->power.states[i]; 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds switch (cx->type) { 4091da177e4SLinus Torvalds case ACPI_STATE_C1: 4101da177e4SLinus Torvalds cx->valid = 1; 4111da177e4SLinus Torvalds break; 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds case ACPI_STATE_C2: 414d22edd29SLen Brown if (!cx->address) 415d22edd29SLen Brown break; 416d22edd29SLen Brown cx->valid = 1; 4171da177e4SLinus Torvalds break; 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds case ACPI_STATE_C3: 4201da177e4SLinus Torvalds acpi_processor_power_verify_c3(pr, cx); 4211da177e4SLinus Torvalds break; 4221da177e4SLinus Torvalds } 4237e275cc4SLen Brown if (!cx->valid) 4247e275cc4SLen Brown continue; 4251da177e4SLinus Torvalds 4267e275cc4SLen Brown lapic_timer_check_state(i, pr, cx); 4277e275cc4SLen Brown tsc_check_state(cx->type); 4281da177e4SLinus Torvalds working++; 4291da177e4SLinus Torvalds } 4301da177e4SLinus Torvalds 431918aae42SHidetoshi Seto lapic_timer_propagate_broadcast(pr); 432bd663347SAndi Kleen 4331da177e4SLinus Torvalds return (working); 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 436a36a7fecSSudeep Holla static int acpi_processor_get_cstate_info(struct acpi_processor *pr) 4371da177e4SLinus Torvalds { 4381da177e4SLinus Torvalds unsigned int i; 4391da177e4SLinus Torvalds int result; 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds /* NOTE: the idle thread may not be running while calling 4431da177e4SLinus Torvalds * this function */ 4441da177e4SLinus Torvalds 445991528d7SVenkatesh Pallipadi /* Zero initialize all the C-states info. */ 446991528d7SVenkatesh Pallipadi memset(pr->power.states, 0, sizeof(pr->power.states)); 447991528d7SVenkatesh Pallipadi 4481da177e4SLinus Torvalds result = acpi_processor_get_power_info_cst(pr); 4496d93c648SVenkatesh Pallipadi if (result == -ENODEV) 450c5a114f1SDarrick J. Wong result = acpi_processor_get_power_info_fadt(pr); 4516d93c648SVenkatesh Pallipadi 452991528d7SVenkatesh Pallipadi if (result) 453991528d7SVenkatesh Pallipadi return result; 454991528d7SVenkatesh Pallipadi 455991528d7SVenkatesh Pallipadi acpi_processor_get_power_info_default(pr); 456991528d7SVenkatesh Pallipadi 457cf824788SJanosch Machowinski pr->power.count = acpi_processor_power_verify(pr); 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds /* 4601da177e4SLinus Torvalds * if one state of type C2 or C3 is available, mark this 4611da177e4SLinus Torvalds * CPU as being "idle manageable" 4621da177e4SLinus Torvalds */ 4631da177e4SLinus Torvalds for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) { 464acf05f4bSVenkatesh Pallipadi if (pr->power.states[i].valid) { 4651da177e4SLinus Torvalds pr->power.count = i; 4661da177e4SLinus Torvalds pr->flags.power = 1; 4671da177e4SLinus Torvalds } 468acf05f4bSVenkatesh Pallipadi } 4691da177e4SLinus Torvalds 470d550d98dSPatrick Mochel return 0; 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds 4734f86d3a8SLen Brown /** 4744f86d3a8SLen Brown * acpi_idle_bm_check - checks if bus master activity was detected 4754f86d3a8SLen Brown */ 4764f86d3a8SLen Brown static int acpi_idle_bm_check(void) 4774f86d3a8SLen Brown { 4784f86d3a8SLen Brown u32 bm_status = 0; 4794f86d3a8SLen Brown 480d3e7e99fSLen Brown if (bm_check_disable) 481d3e7e99fSLen Brown return 0; 482d3e7e99fSLen Brown 48350ffba1bSBob Moore acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status); 4844f86d3a8SLen Brown if (bm_status) 48550ffba1bSBob Moore acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, 1); 4864f86d3a8SLen Brown /* 4874f86d3a8SLen Brown * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect 4884f86d3a8SLen Brown * the true state of bus mastering activity; forcing us to 4894f86d3a8SLen Brown * manually check the BMIDEA bit of each IDE channel. 4904f86d3a8SLen Brown */ 4914f86d3a8SLen Brown else if (errata.piix4.bmisx) { 4924f86d3a8SLen Brown if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01) 4934f86d3a8SLen Brown || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01)) 4944f86d3a8SLen Brown bm_status = 1; 4954f86d3a8SLen Brown } 4964f86d3a8SLen Brown return bm_status; 4974f86d3a8SLen Brown } 4984f86d3a8SLen Brown 499fa583f71SYin Fengwei static void wait_for_freeze(void) 500fa583f71SYin Fengwei { 501fa583f71SYin Fengwei #ifdef CONFIG_X86 502fa583f71SYin Fengwei /* No delay is needed if we are in guest */ 503fa583f71SYin Fengwei if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) 504fa583f71SYin Fengwei return; 505fa583f71SYin Fengwei #endif 506fa583f71SYin Fengwei /* Dummy wait op - must do something useless after P_LVL2 read 507fa583f71SYin Fengwei because chipsets cannot guarantee that STPCLK# signal 508fa583f71SYin Fengwei gets asserted in time to freeze execution properly. */ 509fa583f71SYin Fengwei inl(acpi_gbl_FADT.xpm_timer_block.address); 510fa583f71SYin Fengwei } 511fa583f71SYin Fengwei 5124f86d3a8SLen Brown /** 513b00783fdSRafael J. Wysocki * acpi_idle_do_entry - enter idle state using the appropriate method 5144f86d3a8SLen Brown * @cx: cstate data 515bc71bec9Svenkatesh.pallipadi@intel.com * 516bc71bec9Svenkatesh.pallipadi@intel.com * Caller disables interrupt before call and enables interrupt after return. 5174f86d3a8SLen Brown */ 5186727ad9eSChris Metcalf static void __cpuidle acpi_idle_do_entry(struct acpi_processor_cx *cx) 5194f86d3a8SLen Brown { 520bc71bec9Svenkatesh.pallipadi@intel.com if (cx->entry_method == ACPI_CSTATE_FFH) { 5214f86d3a8SLen Brown /* Call into architectural FFH based C-state */ 5224f86d3a8SLen Brown acpi_processor_ffh_cstate_enter(cx); 523bc71bec9Svenkatesh.pallipadi@intel.com } else if (cx->entry_method == ACPI_CSTATE_HALT) { 524bc71bec9Svenkatesh.pallipadi@intel.com acpi_safe_halt(); 5254f86d3a8SLen Brown } else { 5264f86d3a8SLen Brown /* IO port based C-state */ 5274f86d3a8SLen Brown inb(cx->address); 528fa583f71SYin Fengwei wait_for_freeze(); 5294f86d3a8SLen Brown } 5304f86d3a8SLen Brown } 5314f86d3a8SLen Brown 5324f86d3a8SLen Brown /** 5331a022e3fSBoris Ostrovsky * acpi_idle_play_dead - enters an ACPI state for long-term idle (i.e. off-lining) 5341a022e3fSBoris Ostrovsky * @dev: the target CPU 5351a022e3fSBoris Ostrovsky * @index: the index of suggested state 5361a022e3fSBoris Ostrovsky */ 5371a022e3fSBoris Ostrovsky static int acpi_idle_play_dead(struct cpuidle_device *dev, int index) 5381a022e3fSBoris Ostrovsky { 5396240a10dSAlex Shi struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu); 5401a022e3fSBoris Ostrovsky 5411a022e3fSBoris Ostrovsky ACPI_FLUSH_CPU_CACHE(); 5421a022e3fSBoris Ostrovsky 5431a022e3fSBoris Ostrovsky while (1) { 5441a022e3fSBoris Ostrovsky 5451a022e3fSBoris Ostrovsky if (cx->entry_method == ACPI_CSTATE_HALT) 54654f70077SLuck, Tony safe_halt(); 5471a022e3fSBoris Ostrovsky else if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) { 5481a022e3fSBoris Ostrovsky inb(cx->address); 549fa583f71SYin Fengwei wait_for_freeze(); 5501a022e3fSBoris Ostrovsky } else 5511a022e3fSBoris Ostrovsky return -ENODEV; 5521a022e3fSBoris Ostrovsky } 5531a022e3fSBoris Ostrovsky 5541a022e3fSBoris Ostrovsky /* Never reached */ 5551a022e3fSBoris Ostrovsky return 0; 5561a022e3fSBoris Ostrovsky } 5571a022e3fSBoris Ostrovsky 558adcb2623SRafael J. Wysocki static bool acpi_idle_fallback_to_c1(struct acpi_processor *pr) 559adcb2623SRafael J. Wysocki { 5605f508185SRafael J. Wysocki return IS_ENABLED(CONFIG_HOTPLUG_CPU) && !pr->flags.has_cst && 5615f508185SRafael J. Wysocki !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED); 562adcb2623SRafael J. Wysocki } 563adcb2623SRafael J. Wysocki 5644f86d3a8SLen Brown static int c3_cpu_count; 565e12f65f7SThomas Gleixner static DEFINE_RAW_SPINLOCK(c3_lock); 5664f86d3a8SLen Brown 5674f86d3a8SLen Brown /** 5684f86d3a8SLen Brown * acpi_idle_enter_bm - enters C3 with proper BM handling 5696491bc0cSRafael J. Wysocki * @pr: Target processor 5706491bc0cSRafael J. Wysocki * @cx: Target state context 5715f508185SRafael J. Wysocki * @timer_bc: Whether or not to change timer mode to broadcast 5724f86d3a8SLen Brown */ 5736491bc0cSRafael J. Wysocki static void acpi_idle_enter_bm(struct acpi_processor *pr, 5745f508185SRafael J. Wysocki struct acpi_processor_cx *cx, bool timer_bc) 5754f86d3a8SLen Brown { 57667535736SAndy Lutomirski acpi_unlazy_tlb(smp_processor_id()); 57767535736SAndy Lutomirski 5784f86d3a8SLen Brown /* 5794f86d3a8SLen Brown * Must be done before busmaster disable as we might need to 5804f86d3a8SLen Brown * access HPET ! 5814f86d3a8SLen Brown */ 5825f508185SRafael J. Wysocki if (timer_bc) 5837e275cc4SLen Brown lapic_timer_state_broadcast(pr, cx, 1); 5844f86d3a8SLen Brown 585c9c860e5SVenkatesh Pallipadi /* 586c9c860e5SVenkatesh Pallipadi * disable bus master 587c9c860e5SVenkatesh Pallipadi * bm_check implies we need ARB_DIS 588c9c860e5SVenkatesh Pallipadi * bm_control implies whether we can do ARB_DIS 589c9c860e5SVenkatesh Pallipadi * 590c9c860e5SVenkatesh Pallipadi * That leaves a case where bm_check is set and bm_control is 591c9c860e5SVenkatesh Pallipadi * not set. In that case we cannot do much, we enter C3 592c9c860e5SVenkatesh Pallipadi * without doing anything. 593c9c860e5SVenkatesh Pallipadi */ 5942a738352SRafael J. Wysocki if (pr->flags.bm_control) { 595e12f65f7SThomas Gleixner raw_spin_lock(&c3_lock); 5964f86d3a8SLen Brown c3_cpu_count++; 5974f86d3a8SLen Brown /* Disable bus master arbitration when all CPUs are in C3 */ 5984f86d3a8SLen Brown if (c3_cpu_count == num_online_cpus()) 59950ffba1bSBob Moore acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1); 600e12f65f7SThomas Gleixner raw_spin_unlock(&c3_lock); 601c9c860e5SVenkatesh Pallipadi } 6024f86d3a8SLen Brown 6034f86d3a8SLen Brown acpi_idle_do_entry(cx); 6044f86d3a8SLen Brown 6054f86d3a8SLen Brown /* Re-enable bus master arbitration */ 6062a738352SRafael J. Wysocki if (pr->flags.bm_control) { 607e12f65f7SThomas Gleixner raw_spin_lock(&c3_lock); 60850ffba1bSBob Moore acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0); 6094f86d3a8SLen Brown c3_cpu_count--; 610e12f65f7SThomas Gleixner raw_spin_unlock(&c3_lock); 6114f86d3a8SLen Brown } 6124f86d3a8SLen Brown 6135f508185SRafael J. Wysocki if (timer_bc) 6147e275cc4SLen Brown lapic_timer_state_broadcast(pr, cx, 0); 6156491bc0cSRafael J. Wysocki } 6166491bc0cSRafael J. Wysocki 6176491bc0cSRafael J. Wysocki static int acpi_idle_enter(struct cpuidle_device *dev, 6186491bc0cSRafael J. Wysocki struct cpuidle_driver *drv, int index) 6196491bc0cSRafael J. Wysocki { 6206491bc0cSRafael J. Wysocki struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu); 6216491bc0cSRafael J. Wysocki struct acpi_processor *pr; 6226491bc0cSRafael J. Wysocki 6236491bc0cSRafael J. Wysocki pr = __this_cpu_read(processors); 6246491bc0cSRafael J. Wysocki if (unlikely(!pr)) 6256491bc0cSRafael J. Wysocki return -EINVAL; 6266491bc0cSRafael J. Wysocki 6276491bc0cSRafael J. Wysocki if (cx->type != ACPI_STATE_C1) { 6285f508185SRafael J. Wysocki if (acpi_idle_fallback_to_c1(pr) && num_online_cpus() > 1) { 629dc2251bfSRafael J. Wysocki index = ACPI_IDLE_STATE_START; 6306491bc0cSRafael J. Wysocki cx = per_cpu(acpi_cstate[index], dev->cpu); 6316491bc0cSRafael J. Wysocki } else if (cx->type == ACPI_STATE_C3 && pr->flags.bm_check) { 6326491bc0cSRafael J. Wysocki if (cx->bm_sts_skip || !acpi_idle_bm_check()) { 6335f508185SRafael J. Wysocki acpi_idle_enter_bm(pr, cx, true); 6346491bc0cSRafael J. Wysocki return index; 6356491bc0cSRafael J. Wysocki } else if (drv->safe_state_index >= 0) { 6366491bc0cSRafael J. Wysocki index = drv->safe_state_index; 6376491bc0cSRafael J. Wysocki cx = per_cpu(acpi_cstate[index], dev->cpu); 6386491bc0cSRafael J. Wysocki } else { 6396491bc0cSRafael J. Wysocki acpi_safe_halt(); 6406491bc0cSRafael J. Wysocki return -EBUSY; 6416491bc0cSRafael J. Wysocki } 6426491bc0cSRafael J. Wysocki } 6436491bc0cSRafael J. Wysocki } 6446491bc0cSRafael J. Wysocki 6456491bc0cSRafael J. Wysocki lapic_timer_state_broadcast(pr, cx, 1); 6466491bc0cSRafael J. Wysocki 6476491bc0cSRafael J. Wysocki if (cx->type == ACPI_STATE_C3) 6486491bc0cSRafael J. Wysocki ACPI_FLUSH_CPU_CACHE(); 6496491bc0cSRafael J. Wysocki 6506491bc0cSRafael J. Wysocki acpi_idle_do_entry(cx); 6516491bc0cSRafael J. Wysocki 6526491bc0cSRafael J. Wysocki lapic_timer_state_broadcast(pr, cx, 0); 6536491bc0cSRafael J. Wysocki 654e978aa7dSDeepthi Dharwar return index; 6554f86d3a8SLen Brown } 6564f86d3a8SLen Brown 65728ba086eSRafael J. Wysocki static void acpi_idle_enter_s2idle(struct cpuidle_device *dev, 6585f508185SRafael J. Wysocki struct cpuidle_driver *drv, int index) 6595f508185SRafael J. Wysocki { 6605f508185SRafael J. Wysocki struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu); 6615f508185SRafael J. Wysocki 6625f508185SRafael J. Wysocki if (cx->type == ACPI_STATE_C3) { 6635f508185SRafael J. Wysocki struct acpi_processor *pr = __this_cpu_read(processors); 6645f508185SRafael J. Wysocki 6655f508185SRafael J. Wysocki if (unlikely(!pr)) 6665f508185SRafael J. Wysocki return; 6675f508185SRafael J. Wysocki 6685f508185SRafael J. Wysocki if (pr->flags.bm_check) { 6695f508185SRafael J. Wysocki acpi_idle_enter_bm(pr, cx, false); 6705f508185SRafael J. Wysocki return; 6715f508185SRafael J. Wysocki } else { 6725f508185SRafael J. Wysocki ACPI_FLUSH_CPU_CACHE(); 6735f508185SRafael J. Wysocki } 6745f508185SRafael J. Wysocki } 6755f508185SRafael J. Wysocki acpi_idle_do_entry(cx); 6765f508185SRafael J. Wysocki } 6775f508185SRafael J. Wysocki 6786ef0f086SDaniel Lezcano static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr, 6796ef0f086SDaniel Lezcano struct cpuidle_device *dev) 6804f86d3a8SLen Brown { 681dc2251bfSRafael J. Wysocki int i, count = ACPI_IDLE_STATE_START; 6824f86d3a8SLen Brown struct acpi_processor_cx *cx; 6834f86d3a8SLen Brown 684615dfd93SLen Brown if (max_cstate == 0) 685615dfd93SLen Brown max_cstate = 1; 686615dfd93SLen Brown 6874f86d3a8SLen Brown for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) { 6884f86d3a8SLen Brown cx = &pr->power.states[i]; 6894f86d3a8SLen Brown 6904f86d3a8SLen Brown if (!cx->valid) 6914f86d3a8SLen Brown continue; 6924f86d3a8SLen Brown 6936240a10dSAlex Shi per_cpu(acpi_cstate[count], dev->cpu) = cx; 6944f86d3a8SLen Brown 69546bcfad7SDeepthi Dharwar count++; 69646bcfad7SDeepthi Dharwar if (count == CPUIDLE_STATE_MAX) 69746bcfad7SDeepthi Dharwar break; 69846bcfad7SDeepthi Dharwar } 69946bcfad7SDeepthi Dharwar 70046bcfad7SDeepthi Dharwar if (!count) 70146bcfad7SDeepthi Dharwar return -EINVAL; 70246bcfad7SDeepthi Dharwar 70346bcfad7SDeepthi Dharwar return 0; 70446bcfad7SDeepthi Dharwar } 70546bcfad7SDeepthi Dharwar 706a36a7fecSSudeep Holla static int acpi_processor_setup_cstates(struct acpi_processor *pr) 70746bcfad7SDeepthi Dharwar { 7081b39e3f8SRafael J. Wysocki int i, count; 70946bcfad7SDeepthi Dharwar struct acpi_processor_cx *cx; 71046bcfad7SDeepthi Dharwar struct cpuidle_state *state; 71146bcfad7SDeepthi Dharwar struct cpuidle_driver *drv = &acpi_idle_driver; 71246bcfad7SDeepthi Dharwar 71346bcfad7SDeepthi Dharwar if (max_cstate == 0) 71446bcfad7SDeepthi Dharwar max_cstate = 1; 71546bcfad7SDeepthi Dharwar 7161b39e3f8SRafael J. Wysocki if (IS_ENABLED(CONFIG_ARCH_HAS_CPU_RELAX)) { 7171b39e3f8SRafael J. Wysocki cpuidle_poll_state_init(drv); 7181b39e3f8SRafael J. Wysocki count = 1; 7191b39e3f8SRafael J. Wysocki } else { 7201b39e3f8SRafael J. Wysocki count = 0; 7211b39e3f8SRafael J. Wysocki } 7221b39e3f8SRafael J. Wysocki 72346bcfad7SDeepthi Dharwar for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) { 72446bcfad7SDeepthi Dharwar cx = &pr->power.states[i]; 72546bcfad7SDeepthi Dharwar 72646bcfad7SDeepthi Dharwar if (!cx->valid) 72746bcfad7SDeepthi Dharwar continue; 72846bcfad7SDeepthi Dharwar 72946bcfad7SDeepthi Dharwar state = &drv->states[count]; 7304f86d3a8SLen Brown snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i); 731a36a7fecSSudeep Holla strlcpy(state->desc, cx->desc, CPUIDLE_DESC_LEN); 7324f86d3a8SLen Brown state->exit_latency = cx->latency; 7334963f620SLen Brown state->target_residency = cx->latency * latency_factor; 7346491bc0cSRafael J. Wysocki state->enter = acpi_idle_enter; 7354f86d3a8SLen Brown 7364f86d3a8SLen Brown state->flags = 0; 7376491bc0cSRafael J. Wysocki if (cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2) { 7381a022e3fSBoris Ostrovsky state->enter_dead = acpi_idle_play_dead; 73946bcfad7SDeepthi Dharwar drv->safe_state_index = count; 7404f86d3a8SLen Brown } 7415f508185SRafael J. Wysocki /* 74228ba086eSRafael J. Wysocki * Halt-induced C1 is not good for ->enter_s2idle, because it 7435f508185SRafael J. Wysocki * re-enables interrupts on exit. Moreover, C1 is generally not 7445f508185SRafael J. Wysocki * particularly interesting from the suspend-to-idle angle, so 7455f508185SRafael J. Wysocki * avoid C1 and the situations in which we may need to fall back 7465f508185SRafael J. Wysocki * to it altogether. 7475f508185SRafael J. Wysocki */ 7485f508185SRafael J. Wysocki if (cx->type != ACPI_STATE_C1 && !acpi_idle_fallback_to_c1(pr)) 74928ba086eSRafael J. Wysocki state->enter_s2idle = acpi_idle_enter_s2idle; 7504f86d3a8SLen Brown 7514f86d3a8SLen Brown count++; 7529a0b8415Svenkatesh.pallipadi@intel.com if (count == CPUIDLE_STATE_MAX) 7539a0b8415Svenkatesh.pallipadi@intel.com break; 7544f86d3a8SLen Brown } 7554f86d3a8SLen Brown 75646bcfad7SDeepthi Dharwar drv->state_count = count; 7574f86d3a8SLen Brown 7584f86d3a8SLen Brown if (!count) 7594f86d3a8SLen Brown return -EINVAL; 7604f86d3a8SLen Brown 7614f86d3a8SLen Brown return 0; 7624f86d3a8SLen Brown } 7634f86d3a8SLen Brown 76435ae7133SSudeep Holla static inline void acpi_processor_cstate_first_run_checks(void) 76535ae7133SSudeep Holla { 76635ae7133SSudeep Holla static int first_run; 76735ae7133SSudeep Holla 76835ae7133SSudeep Holla if (first_run) 76935ae7133SSudeep Holla return; 77035ae7133SSudeep Holla dmi_check_system(processor_power_dmi_table); 77135ae7133SSudeep Holla max_cstate = acpi_processor_cstate_check(max_cstate); 77235ae7133SSudeep Holla if (max_cstate < ACPI_C_STATES_MAX) 77335ae7133SSudeep Holla pr_notice("ACPI: processor limited to max C-state %d\n", 77435ae7133SSudeep Holla max_cstate); 77535ae7133SSudeep Holla first_run++; 77635ae7133SSudeep Holla 777bc946388SRafael J. Wysocki if (nocst) 778bc946388SRafael J. Wysocki return; 779bc946388SRafael J. Wysocki 780bc946388SRafael J. Wysocki acpi_processor_claim_cst_control(); 78135ae7133SSudeep Holla } 78235ae7133SSudeep Holla #else 78335ae7133SSudeep Holla 78435ae7133SSudeep Holla static inline int disabled_by_idle_boot_param(void) { return 0; } 78535ae7133SSudeep Holla static inline void acpi_processor_cstate_first_run_checks(void) { } 786a36a7fecSSudeep Holla static int acpi_processor_get_cstate_info(struct acpi_processor *pr) 78735ae7133SSudeep Holla { 78835ae7133SSudeep Holla return -ENODEV; 78935ae7133SSudeep Holla } 79035ae7133SSudeep Holla 79135ae7133SSudeep Holla static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr, 79235ae7133SSudeep Holla struct cpuidle_device *dev) 79335ae7133SSudeep Holla { 79435ae7133SSudeep Holla return -EINVAL; 79535ae7133SSudeep Holla } 79635ae7133SSudeep Holla 797a36a7fecSSudeep Holla static int acpi_processor_setup_cstates(struct acpi_processor *pr) 79835ae7133SSudeep Holla { 79935ae7133SSudeep Holla return -EINVAL; 80035ae7133SSudeep Holla } 80135ae7133SSudeep Holla 80235ae7133SSudeep Holla #endif /* CONFIG_ACPI_PROCESSOR_CSTATE */ 80335ae7133SSudeep Holla 804a36a7fecSSudeep Holla struct acpi_lpi_states_array { 805a36a7fecSSudeep Holla unsigned int size; 806a36a7fecSSudeep Holla unsigned int composite_states_size; 807a36a7fecSSudeep Holla struct acpi_lpi_state *entries; 808a36a7fecSSudeep Holla struct acpi_lpi_state *composite_states[ACPI_PROCESSOR_MAX_POWER]; 809a36a7fecSSudeep Holla }; 810a36a7fecSSudeep Holla 811a36a7fecSSudeep Holla static int obj_get_integer(union acpi_object *obj, u32 *value) 812a36a7fecSSudeep Holla { 813a36a7fecSSudeep Holla if (obj->type != ACPI_TYPE_INTEGER) 814a36a7fecSSudeep Holla return -EINVAL; 815a36a7fecSSudeep Holla 816a36a7fecSSudeep Holla *value = obj->integer.value; 817a36a7fecSSudeep Holla return 0; 818a36a7fecSSudeep Holla } 819a36a7fecSSudeep Holla 820a36a7fecSSudeep Holla static int acpi_processor_evaluate_lpi(acpi_handle handle, 821a36a7fecSSudeep Holla struct acpi_lpi_states_array *info) 822a36a7fecSSudeep Holla { 823a36a7fecSSudeep Holla acpi_status status; 824a36a7fecSSudeep Holla int ret = 0; 825a36a7fecSSudeep Holla int pkg_count, state_idx = 1, loop; 826a36a7fecSSudeep Holla struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 827a36a7fecSSudeep Holla union acpi_object *lpi_data; 828a36a7fecSSudeep Holla struct acpi_lpi_state *lpi_state; 829a36a7fecSSudeep Holla 830a36a7fecSSudeep Holla status = acpi_evaluate_object(handle, "_LPI", NULL, &buffer); 831a36a7fecSSudeep Holla if (ACPI_FAILURE(status)) { 832a36a7fecSSudeep Holla ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _LPI, giving up\n")); 833a36a7fecSSudeep Holla return -ENODEV; 834a36a7fecSSudeep Holla } 835a36a7fecSSudeep Holla 836a36a7fecSSudeep Holla lpi_data = buffer.pointer; 837a36a7fecSSudeep Holla 838a36a7fecSSudeep Holla /* There must be at least 4 elements = 3 elements + 1 package */ 839a36a7fecSSudeep Holla if (!lpi_data || lpi_data->type != ACPI_TYPE_PACKAGE || 840a36a7fecSSudeep Holla lpi_data->package.count < 4) { 841a36a7fecSSudeep Holla pr_debug("not enough elements in _LPI\n"); 842a36a7fecSSudeep Holla ret = -ENODATA; 843a36a7fecSSudeep Holla goto end; 844a36a7fecSSudeep Holla } 845a36a7fecSSudeep Holla 846a36a7fecSSudeep Holla pkg_count = lpi_data->package.elements[2].integer.value; 847a36a7fecSSudeep Holla 848a36a7fecSSudeep Holla /* Validate number of power states. */ 849a36a7fecSSudeep Holla if (pkg_count < 1 || pkg_count != lpi_data->package.count - 3) { 850a36a7fecSSudeep Holla pr_debug("count given by _LPI is not valid\n"); 851a36a7fecSSudeep Holla ret = -ENODATA; 852a36a7fecSSudeep Holla goto end; 853a36a7fecSSudeep Holla } 854a36a7fecSSudeep Holla 855a36a7fecSSudeep Holla lpi_state = kcalloc(pkg_count, sizeof(*lpi_state), GFP_KERNEL); 856a36a7fecSSudeep Holla if (!lpi_state) { 857a36a7fecSSudeep Holla ret = -ENOMEM; 858a36a7fecSSudeep Holla goto end; 859a36a7fecSSudeep Holla } 860a36a7fecSSudeep Holla 861a36a7fecSSudeep Holla info->size = pkg_count; 862a36a7fecSSudeep Holla info->entries = lpi_state; 863a36a7fecSSudeep Holla 864a36a7fecSSudeep Holla /* LPI States start at index 3 */ 865a36a7fecSSudeep Holla for (loop = 3; state_idx <= pkg_count; loop++, state_idx++, lpi_state++) { 866a36a7fecSSudeep Holla union acpi_object *element, *pkg_elem, *obj; 867a36a7fecSSudeep Holla 868a36a7fecSSudeep Holla element = &lpi_data->package.elements[loop]; 869a36a7fecSSudeep Holla if (element->type != ACPI_TYPE_PACKAGE || element->package.count < 7) 870a36a7fecSSudeep Holla continue; 871a36a7fecSSudeep Holla 872a36a7fecSSudeep Holla pkg_elem = element->package.elements; 873a36a7fecSSudeep Holla 874a36a7fecSSudeep Holla obj = pkg_elem + 6; 875a36a7fecSSudeep Holla if (obj->type == ACPI_TYPE_BUFFER) { 876a36a7fecSSudeep Holla struct acpi_power_register *reg; 877a36a7fecSSudeep Holla 878a36a7fecSSudeep Holla reg = (struct acpi_power_register *)obj->buffer.pointer; 879a36a7fecSSudeep Holla if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO && 880a36a7fecSSudeep Holla reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) 881a36a7fecSSudeep Holla continue; 882a36a7fecSSudeep Holla 883a36a7fecSSudeep Holla lpi_state->address = reg->address; 884a36a7fecSSudeep Holla lpi_state->entry_method = 885a36a7fecSSudeep Holla reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE ? 886a36a7fecSSudeep Holla ACPI_CSTATE_FFH : ACPI_CSTATE_SYSTEMIO; 887a36a7fecSSudeep Holla } else if (obj->type == ACPI_TYPE_INTEGER) { 888a36a7fecSSudeep Holla lpi_state->entry_method = ACPI_CSTATE_INTEGER; 889a36a7fecSSudeep Holla lpi_state->address = obj->integer.value; 890a36a7fecSSudeep Holla } else { 891a36a7fecSSudeep Holla continue; 892a36a7fecSSudeep Holla } 893a36a7fecSSudeep Holla 894a36a7fecSSudeep Holla /* elements[7,8] skipped for now i.e. Residency/Usage counter*/ 895a36a7fecSSudeep Holla 896a36a7fecSSudeep Holla obj = pkg_elem + 9; 897a36a7fecSSudeep Holla if (obj->type == ACPI_TYPE_STRING) 898a36a7fecSSudeep Holla strlcpy(lpi_state->desc, obj->string.pointer, 899a36a7fecSSudeep Holla ACPI_CX_DESC_LEN); 900a36a7fecSSudeep Holla 901a36a7fecSSudeep Holla lpi_state->index = state_idx; 902a36a7fecSSudeep Holla if (obj_get_integer(pkg_elem + 0, &lpi_state->min_residency)) { 903a36a7fecSSudeep Holla pr_debug("No min. residency found, assuming 10 us\n"); 904a36a7fecSSudeep Holla lpi_state->min_residency = 10; 905a36a7fecSSudeep Holla } 906a36a7fecSSudeep Holla 907a36a7fecSSudeep Holla if (obj_get_integer(pkg_elem + 1, &lpi_state->wake_latency)) { 908a36a7fecSSudeep Holla pr_debug("No wakeup residency found, assuming 10 us\n"); 909a36a7fecSSudeep Holla lpi_state->wake_latency = 10; 910a36a7fecSSudeep Holla } 911a36a7fecSSudeep Holla 912a36a7fecSSudeep Holla if (obj_get_integer(pkg_elem + 2, &lpi_state->flags)) 913a36a7fecSSudeep Holla lpi_state->flags = 0; 914a36a7fecSSudeep Holla 915a36a7fecSSudeep Holla if (obj_get_integer(pkg_elem + 3, &lpi_state->arch_flags)) 916a36a7fecSSudeep Holla lpi_state->arch_flags = 0; 917a36a7fecSSudeep Holla 918a36a7fecSSudeep Holla if (obj_get_integer(pkg_elem + 4, &lpi_state->res_cnt_freq)) 919a36a7fecSSudeep Holla lpi_state->res_cnt_freq = 1; 920a36a7fecSSudeep Holla 921a36a7fecSSudeep Holla if (obj_get_integer(pkg_elem + 5, &lpi_state->enable_parent_state)) 922a36a7fecSSudeep Holla lpi_state->enable_parent_state = 0; 923a36a7fecSSudeep Holla } 924a36a7fecSSudeep Holla 925a36a7fecSSudeep Holla acpi_handle_debug(handle, "Found %d power states\n", state_idx); 926a36a7fecSSudeep Holla end: 927a36a7fecSSudeep Holla kfree(buffer.pointer); 928a36a7fecSSudeep Holla return ret; 929a36a7fecSSudeep Holla } 930a36a7fecSSudeep Holla 931a36a7fecSSudeep Holla /* 932a36a7fecSSudeep Holla * flat_state_cnt - the number of composite LPI states after the process of flattening 933a36a7fecSSudeep Holla */ 934a36a7fecSSudeep Holla static int flat_state_cnt; 935a36a7fecSSudeep Holla 936a36a7fecSSudeep Holla /** 937a36a7fecSSudeep Holla * combine_lpi_states - combine local and parent LPI states to form a composite LPI state 938a36a7fecSSudeep Holla * 939a36a7fecSSudeep Holla * @local: local LPI state 940a36a7fecSSudeep Holla * @parent: parent LPI state 941a36a7fecSSudeep Holla * @result: composite LPI state 942a36a7fecSSudeep Holla */ 943a36a7fecSSudeep Holla static bool combine_lpi_states(struct acpi_lpi_state *local, 944a36a7fecSSudeep Holla struct acpi_lpi_state *parent, 945a36a7fecSSudeep Holla struct acpi_lpi_state *result) 946a36a7fecSSudeep Holla { 947a36a7fecSSudeep Holla if (parent->entry_method == ACPI_CSTATE_INTEGER) { 948a36a7fecSSudeep Holla if (!parent->address) /* 0 means autopromotable */ 949a36a7fecSSudeep Holla return false; 950a36a7fecSSudeep Holla result->address = local->address + parent->address; 951a36a7fecSSudeep Holla } else { 952a36a7fecSSudeep Holla result->address = parent->address; 953a36a7fecSSudeep Holla } 954a36a7fecSSudeep Holla 955a36a7fecSSudeep Holla result->min_residency = max(local->min_residency, parent->min_residency); 956a36a7fecSSudeep Holla result->wake_latency = local->wake_latency + parent->wake_latency; 957a36a7fecSSudeep Holla result->enable_parent_state = parent->enable_parent_state; 958a36a7fecSSudeep Holla result->entry_method = local->entry_method; 959a36a7fecSSudeep Holla 960a36a7fecSSudeep Holla result->flags = parent->flags; 961a36a7fecSSudeep Holla result->arch_flags = parent->arch_flags; 962a36a7fecSSudeep Holla result->index = parent->index; 963a36a7fecSSudeep Holla 964a36a7fecSSudeep Holla strlcpy(result->desc, local->desc, ACPI_CX_DESC_LEN); 965a36a7fecSSudeep Holla strlcat(result->desc, "+", ACPI_CX_DESC_LEN); 966a36a7fecSSudeep Holla strlcat(result->desc, parent->desc, ACPI_CX_DESC_LEN); 967a36a7fecSSudeep Holla return true; 968a36a7fecSSudeep Holla } 969a36a7fecSSudeep Holla 970a36a7fecSSudeep Holla #define ACPI_LPI_STATE_FLAGS_ENABLED BIT(0) 971a36a7fecSSudeep Holla 972a36a7fecSSudeep Holla static void stash_composite_state(struct acpi_lpi_states_array *curr_level, 973a36a7fecSSudeep Holla struct acpi_lpi_state *t) 974a36a7fecSSudeep Holla { 975a36a7fecSSudeep Holla curr_level->composite_states[curr_level->composite_states_size++] = t; 976a36a7fecSSudeep Holla } 977a36a7fecSSudeep Holla 978a36a7fecSSudeep Holla static int flatten_lpi_states(struct acpi_processor *pr, 979a36a7fecSSudeep Holla struct acpi_lpi_states_array *curr_level, 980a36a7fecSSudeep Holla struct acpi_lpi_states_array *prev_level) 981a36a7fecSSudeep Holla { 982a36a7fecSSudeep Holla int i, j, state_count = curr_level->size; 983a36a7fecSSudeep Holla struct acpi_lpi_state *p, *t = curr_level->entries; 984a36a7fecSSudeep Holla 985a36a7fecSSudeep Holla curr_level->composite_states_size = 0; 986a36a7fecSSudeep Holla for (j = 0; j < state_count; j++, t++) { 987a36a7fecSSudeep Holla struct acpi_lpi_state *flpi; 988a36a7fecSSudeep Holla 989a36a7fecSSudeep Holla if (!(t->flags & ACPI_LPI_STATE_FLAGS_ENABLED)) 990a36a7fecSSudeep Holla continue; 991a36a7fecSSudeep Holla 992a36a7fecSSudeep Holla if (flat_state_cnt >= ACPI_PROCESSOR_MAX_POWER) { 993a36a7fecSSudeep Holla pr_warn("Limiting number of LPI states to max (%d)\n", 994a36a7fecSSudeep Holla ACPI_PROCESSOR_MAX_POWER); 995a36a7fecSSudeep Holla pr_warn("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n"); 996a36a7fecSSudeep Holla break; 997a36a7fecSSudeep Holla } 998a36a7fecSSudeep Holla 999a36a7fecSSudeep Holla flpi = &pr->power.lpi_states[flat_state_cnt]; 1000a36a7fecSSudeep Holla 1001a36a7fecSSudeep Holla if (!prev_level) { /* leaf/processor node */ 1002a36a7fecSSudeep Holla memcpy(flpi, t, sizeof(*t)); 1003a36a7fecSSudeep Holla stash_composite_state(curr_level, flpi); 1004a36a7fecSSudeep Holla flat_state_cnt++; 1005a36a7fecSSudeep Holla continue; 1006a36a7fecSSudeep Holla } 1007a36a7fecSSudeep Holla 1008a36a7fecSSudeep Holla for (i = 0; i < prev_level->composite_states_size; i++) { 1009a36a7fecSSudeep Holla p = prev_level->composite_states[i]; 1010a36a7fecSSudeep Holla if (t->index <= p->enable_parent_state && 1011a36a7fecSSudeep Holla combine_lpi_states(p, t, flpi)) { 1012a36a7fecSSudeep Holla stash_composite_state(curr_level, flpi); 1013a36a7fecSSudeep Holla flat_state_cnt++; 1014a36a7fecSSudeep Holla flpi++; 1015a36a7fecSSudeep Holla } 1016a36a7fecSSudeep Holla } 1017a36a7fecSSudeep Holla } 1018a36a7fecSSudeep Holla 1019a36a7fecSSudeep Holla kfree(curr_level->entries); 1020a36a7fecSSudeep Holla return 0; 1021a36a7fecSSudeep Holla } 1022a36a7fecSSudeep Holla 1023a36a7fecSSudeep Holla static int acpi_processor_get_lpi_info(struct acpi_processor *pr) 1024a36a7fecSSudeep Holla { 1025a36a7fecSSudeep Holla int ret, i; 1026a36a7fecSSudeep Holla acpi_status status; 1027a36a7fecSSudeep Holla acpi_handle handle = pr->handle, pr_ahandle; 1028a36a7fecSSudeep Holla struct acpi_device *d = NULL; 1029a36a7fecSSudeep Holla struct acpi_lpi_states_array info[2], *tmp, *prev, *curr; 1030a36a7fecSSudeep Holla 1031a36a7fecSSudeep Holla if (!osc_pc_lpi_support_confirmed) 1032a36a7fecSSudeep Holla return -EOPNOTSUPP; 1033a36a7fecSSudeep Holla 1034a36a7fecSSudeep Holla if (!acpi_has_method(handle, "_LPI")) 1035a36a7fecSSudeep Holla return -EINVAL; 1036a36a7fecSSudeep Holla 1037a36a7fecSSudeep Holla flat_state_cnt = 0; 1038a36a7fecSSudeep Holla prev = &info[0]; 1039a36a7fecSSudeep Holla curr = &info[1]; 1040a36a7fecSSudeep Holla handle = pr->handle; 1041a36a7fecSSudeep Holla ret = acpi_processor_evaluate_lpi(handle, prev); 1042a36a7fecSSudeep Holla if (ret) 1043a36a7fecSSudeep Holla return ret; 1044a36a7fecSSudeep Holla flatten_lpi_states(pr, prev, NULL); 1045a36a7fecSSudeep Holla 1046a36a7fecSSudeep Holla status = acpi_get_parent(handle, &pr_ahandle); 1047a36a7fecSSudeep Holla while (ACPI_SUCCESS(status)) { 1048a36a7fecSSudeep Holla acpi_bus_get_device(pr_ahandle, &d); 1049a36a7fecSSudeep Holla handle = pr_ahandle; 1050a36a7fecSSudeep Holla 1051a36a7fecSSudeep Holla if (strcmp(acpi_device_hid(d), ACPI_PROCESSOR_CONTAINER_HID)) 1052a36a7fecSSudeep Holla break; 1053a36a7fecSSudeep Holla 1054a36a7fecSSudeep Holla /* can be optional ? */ 1055a36a7fecSSudeep Holla if (!acpi_has_method(handle, "_LPI")) 1056a36a7fecSSudeep Holla break; 1057a36a7fecSSudeep Holla 1058a36a7fecSSudeep Holla ret = acpi_processor_evaluate_lpi(handle, curr); 1059a36a7fecSSudeep Holla if (ret) 1060a36a7fecSSudeep Holla break; 1061a36a7fecSSudeep Holla 1062a36a7fecSSudeep Holla /* flatten all the LPI states in this level of hierarchy */ 1063a36a7fecSSudeep Holla flatten_lpi_states(pr, curr, prev); 1064a36a7fecSSudeep Holla 1065a36a7fecSSudeep Holla tmp = prev, prev = curr, curr = tmp; 1066a36a7fecSSudeep Holla 1067a36a7fecSSudeep Holla status = acpi_get_parent(handle, &pr_ahandle); 1068a36a7fecSSudeep Holla } 1069a36a7fecSSudeep Holla 1070a36a7fecSSudeep Holla pr->power.count = flat_state_cnt; 1071a36a7fecSSudeep Holla /* reset the index after flattening */ 1072a36a7fecSSudeep Holla for (i = 0; i < pr->power.count; i++) 1073a36a7fecSSudeep Holla pr->power.lpi_states[i].index = i; 1074a36a7fecSSudeep Holla 1075a36a7fecSSudeep Holla /* Tell driver that _LPI is supported. */ 1076a36a7fecSSudeep Holla pr->flags.has_lpi = 1; 1077a36a7fecSSudeep Holla pr->flags.power = 1; 1078a36a7fecSSudeep Holla 1079a36a7fecSSudeep Holla return 0; 1080a36a7fecSSudeep Holla } 1081a36a7fecSSudeep Holla 1082a36a7fecSSudeep Holla int __weak acpi_processor_ffh_lpi_probe(unsigned int cpu) 1083a36a7fecSSudeep Holla { 1084a36a7fecSSudeep Holla return -ENODEV; 1085a36a7fecSSudeep Holla } 1086a36a7fecSSudeep Holla 1087a36a7fecSSudeep Holla int __weak acpi_processor_ffh_lpi_enter(struct acpi_lpi_state *lpi) 1088a36a7fecSSudeep Holla { 1089a36a7fecSSudeep Holla return -ENODEV; 1090a36a7fecSSudeep Holla } 1091a36a7fecSSudeep Holla 1092a36a7fecSSudeep Holla /** 1093a36a7fecSSudeep Holla * acpi_idle_lpi_enter - enters an ACPI any LPI state 1094a36a7fecSSudeep Holla * @dev: the target CPU 1095a36a7fecSSudeep Holla * @drv: cpuidle driver containing cpuidle state info 1096a36a7fecSSudeep Holla * @index: index of target state 1097a36a7fecSSudeep Holla * 1098a36a7fecSSudeep Holla * Return: 0 for success or negative value for error 1099a36a7fecSSudeep Holla */ 1100a36a7fecSSudeep Holla static int acpi_idle_lpi_enter(struct cpuidle_device *dev, 1101a36a7fecSSudeep Holla struct cpuidle_driver *drv, int index) 1102a36a7fecSSudeep Holla { 1103a36a7fecSSudeep Holla struct acpi_processor *pr; 1104a36a7fecSSudeep Holla struct acpi_lpi_state *lpi; 1105a36a7fecSSudeep Holla 1106a36a7fecSSudeep Holla pr = __this_cpu_read(processors); 1107a36a7fecSSudeep Holla 1108a36a7fecSSudeep Holla if (unlikely(!pr)) 1109a36a7fecSSudeep Holla return -EINVAL; 1110a36a7fecSSudeep Holla 1111a36a7fecSSudeep Holla lpi = &pr->power.lpi_states[index]; 1112a36a7fecSSudeep Holla if (lpi->entry_method == ACPI_CSTATE_FFH) 1113a36a7fecSSudeep Holla return acpi_processor_ffh_lpi_enter(lpi); 1114a36a7fecSSudeep Holla 1115a36a7fecSSudeep Holla return -EINVAL; 1116a36a7fecSSudeep Holla } 1117a36a7fecSSudeep Holla 1118a36a7fecSSudeep Holla static int acpi_processor_setup_lpi_states(struct acpi_processor *pr) 1119a36a7fecSSudeep Holla { 1120a36a7fecSSudeep Holla int i; 1121a36a7fecSSudeep Holla struct acpi_lpi_state *lpi; 1122a36a7fecSSudeep Holla struct cpuidle_state *state; 1123a36a7fecSSudeep Holla struct cpuidle_driver *drv = &acpi_idle_driver; 1124a36a7fecSSudeep Holla 1125a36a7fecSSudeep Holla if (!pr->flags.has_lpi) 1126a36a7fecSSudeep Holla return -EOPNOTSUPP; 1127a36a7fecSSudeep Holla 1128a36a7fecSSudeep Holla for (i = 0; i < pr->power.count && i < CPUIDLE_STATE_MAX; i++) { 1129a36a7fecSSudeep Holla lpi = &pr->power.lpi_states[i]; 1130a36a7fecSSudeep Holla 1131a36a7fecSSudeep Holla state = &drv->states[i]; 1132a36a7fecSSudeep Holla snprintf(state->name, CPUIDLE_NAME_LEN, "LPI-%d", i); 1133a36a7fecSSudeep Holla strlcpy(state->desc, lpi->desc, CPUIDLE_DESC_LEN); 1134a36a7fecSSudeep Holla state->exit_latency = lpi->wake_latency; 1135a36a7fecSSudeep Holla state->target_residency = lpi->min_residency; 1136a36a7fecSSudeep Holla if (lpi->arch_flags) 1137a36a7fecSSudeep Holla state->flags |= CPUIDLE_FLAG_TIMER_STOP; 1138a36a7fecSSudeep Holla state->enter = acpi_idle_lpi_enter; 1139a36a7fecSSudeep Holla drv->safe_state_index = i; 1140a36a7fecSSudeep Holla } 1141a36a7fecSSudeep Holla 1142a36a7fecSSudeep Holla drv->state_count = i; 1143a36a7fecSSudeep Holla 1144a36a7fecSSudeep Holla return 0; 1145a36a7fecSSudeep Holla } 1146a36a7fecSSudeep Holla 1147a36a7fecSSudeep Holla /** 1148a36a7fecSSudeep Holla * acpi_processor_setup_cpuidle_states- prepares and configures cpuidle 1149a36a7fecSSudeep Holla * global state data i.e. idle routines 1150a36a7fecSSudeep Holla * 1151a36a7fecSSudeep Holla * @pr: the ACPI processor 1152a36a7fecSSudeep Holla */ 1153a36a7fecSSudeep Holla static int acpi_processor_setup_cpuidle_states(struct acpi_processor *pr) 1154a36a7fecSSudeep Holla { 1155a36a7fecSSudeep Holla int i; 1156a36a7fecSSudeep Holla struct cpuidle_driver *drv = &acpi_idle_driver; 1157a36a7fecSSudeep Holla 1158a36a7fecSSudeep Holla if (!pr->flags.power_setup_done || !pr->flags.power) 1159a36a7fecSSudeep Holla return -EINVAL; 1160a36a7fecSSudeep Holla 1161a36a7fecSSudeep Holla drv->safe_state_index = -1; 1162dc2251bfSRafael J. Wysocki for (i = ACPI_IDLE_STATE_START; i < CPUIDLE_STATE_MAX; i++) { 1163a36a7fecSSudeep Holla drv->states[i].name[0] = '\0'; 1164a36a7fecSSudeep Holla drv->states[i].desc[0] = '\0'; 1165a36a7fecSSudeep Holla } 1166a36a7fecSSudeep Holla 1167a36a7fecSSudeep Holla if (pr->flags.has_lpi) 1168a36a7fecSSudeep Holla return acpi_processor_setup_lpi_states(pr); 1169a36a7fecSSudeep Holla 1170a36a7fecSSudeep Holla return acpi_processor_setup_cstates(pr); 1171a36a7fecSSudeep Holla } 1172a36a7fecSSudeep Holla 1173a36a7fecSSudeep Holla /** 1174a36a7fecSSudeep Holla * acpi_processor_setup_cpuidle_dev - prepares and configures CPUIDLE 1175a36a7fecSSudeep Holla * device i.e. per-cpu data 1176a36a7fecSSudeep Holla * 1177a36a7fecSSudeep Holla * @pr: the ACPI processor 1178a36a7fecSSudeep Holla * @dev : the cpuidle device 1179a36a7fecSSudeep Holla */ 1180a36a7fecSSudeep Holla static int acpi_processor_setup_cpuidle_dev(struct acpi_processor *pr, 1181a36a7fecSSudeep Holla struct cpuidle_device *dev) 1182a36a7fecSSudeep Holla { 1183a36a7fecSSudeep Holla if (!pr->flags.power_setup_done || !pr->flags.power || !dev) 1184a36a7fecSSudeep Holla return -EINVAL; 1185a36a7fecSSudeep Holla 1186a36a7fecSSudeep Holla dev->cpu = pr->id; 1187a36a7fecSSudeep Holla if (pr->flags.has_lpi) 1188a36a7fecSSudeep Holla return acpi_processor_ffh_lpi_probe(pr->id); 1189a36a7fecSSudeep Holla 1190a36a7fecSSudeep Holla return acpi_processor_setup_cpuidle_cx(pr, dev); 1191a36a7fecSSudeep Holla } 1192a36a7fecSSudeep Holla 1193a36a7fecSSudeep Holla static int acpi_processor_get_power_info(struct acpi_processor *pr) 1194a36a7fecSSudeep Holla { 1195a36a7fecSSudeep Holla int ret; 1196a36a7fecSSudeep Holla 1197a36a7fecSSudeep Holla ret = acpi_processor_get_lpi_info(pr); 1198a36a7fecSSudeep Holla if (ret) 1199a36a7fecSSudeep Holla ret = acpi_processor_get_cstate_info(pr); 1200a36a7fecSSudeep Holla 1201a36a7fecSSudeep Holla return ret; 1202a36a7fecSSudeep Holla } 1203a36a7fecSSudeep Holla 120446bcfad7SDeepthi Dharwar int acpi_processor_hotplug(struct acpi_processor *pr) 12054f86d3a8SLen Brown { 1206dcb84f33SVenkatesh Pallipadi int ret = 0; 1207e8b1b59dSWei Yongjun struct cpuidle_device *dev; 12084f86d3a8SLen Brown 1209d1896049SThomas Renninger if (disabled_by_idle_boot_param()) 121036a91358SVenkatesh Pallipadi return 0; 121136a91358SVenkatesh Pallipadi 12124f86d3a8SLen Brown if (!pr->flags.power_setup_done) 12134f86d3a8SLen Brown return -ENODEV; 12144f86d3a8SLen Brown 1215e8b1b59dSWei Yongjun dev = per_cpu(acpi_cpuidle_device, pr->id); 12164f86d3a8SLen Brown cpuidle_pause_and_lock(); 12173d339dcbSDaniel Lezcano cpuidle_disable_device(dev); 1218a36a7fecSSudeep Holla ret = acpi_processor_get_power_info(pr); 1219a36a7fecSSudeep Holla if (!ret && pr->flags.power) { 1220a36a7fecSSudeep Holla acpi_processor_setup_cpuidle_dev(pr, dev); 12213d339dcbSDaniel Lezcano ret = cpuidle_enable_device(dev); 1222dcb84f33SVenkatesh Pallipadi } 12234f86d3a8SLen Brown cpuidle_resume_and_unlock(); 12244f86d3a8SLen Brown 12254f86d3a8SLen Brown return ret; 12264f86d3a8SLen Brown } 12274f86d3a8SLen Brown 1228a36a7fecSSudeep Holla int acpi_processor_power_state_has_changed(struct acpi_processor *pr) 122946bcfad7SDeepthi Dharwar { 123046bcfad7SDeepthi Dharwar int cpu; 123146bcfad7SDeepthi Dharwar struct acpi_processor *_pr; 12323d339dcbSDaniel Lezcano struct cpuidle_device *dev; 123346bcfad7SDeepthi Dharwar 123446bcfad7SDeepthi Dharwar if (disabled_by_idle_boot_param()) 123546bcfad7SDeepthi Dharwar return 0; 123646bcfad7SDeepthi Dharwar 123746bcfad7SDeepthi Dharwar if (!pr->flags.power_setup_done) 123846bcfad7SDeepthi Dharwar return -ENODEV; 123946bcfad7SDeepthi Dharwar 124046bcfad7SDeepthi Dharwar /* 124146bcfad7SDeepthi Dharwar * FIXME: Design the ACPI notification to make it once per 124246bcfad7SDeepthi Dharwar * system instead of once per-cpu. This condition is a hack 124346bcfad7SDeepthi Dharwar * to make the code that updates C-States be called once. 124446bcfad7SDeepthi Dharwar */ 124546bcfad7SDeepthi Dharwar 12469505626dSPaul E. McKenney if (pr->id == 0 && cpuidle_get_driver() == &acpi_idle_driver) { 124746bcfad7SDeepthi Dharwar 124846bcfad7SDeepthi Dharwar /* Protect against cpu-hotplug */ 124946bcfad7SDeepthi Dharwar get_online_cpus(); 12506726655dSJiri Kosina cpuidle_pause_and_lock(); 125146bcfad7SDeepthi Dharwar 125246bcfad7SDeepthi Dharwar /* Disable all cpuidle devices */ 125346bcfad7SDeepthi Dharwar for_each_online_cpu(cpu) { 125446bcfad7SDeepthi Dharwar _pr = per_cpu(processors, cpu); 125546bcfad7SDeepthi Dharwar if (!_pr || !_pr->flags.power_setup_done) 125646bcfad7SDeepthi Dharwar continue; 12573d339dcbSDaniel Lezcano dev = per_cpu(acpi_cpuidle_device, cpu); 12583d339dcbSDaniel Lezcano cpuidle_disable_device(dev); 125946bcfad7SDeepthi Dharwar } 126046bcfad7SDeepthi Dharwar 126146bcfad7SDeepthi Dharwar /* Populate Updated C-state information */ 1262f427e5f1SThomas Schlichter acpi_processor_get_power_info(pr); 126346bcfad7SDeepthi Dharwar acpi_processor_setup_cpuidle_states(pr); 126446bcfad7SDeepthi Dharwar 126546bcfad7SDeepthi Dharwar /* Enable all cpuidle devices */ 126646bcfad7SDeepthi Dharwar for_each_online_cpu(cpu) { 126746bcfad7SDeepthi Dharwar _pr = per_cpu(processors, cpu); 126846bcfad7SDeepthi Dharwar if (!_pr || !_pr->flags.power_setup_done) 126946bcfad7SDeepthi Dharwar continue; 127046bcfad7SDeepthi Dharwar acpi_processor_get_power_info(_pr); 127146bcfad7SDeepthi Dharwar if (_pr->flags.power) { 12723d339dcbSDaniel Lezcano dev = per_cpu(acpi_cpuidle_device, cpu); 1273a36a7fecSSudeep Holla acpi_processor_setup_cpuidle_dev(_pr, dev); 12743d339dcbSDaniel Lezcano cpuidle_enable_device(dev); 127546bcfad7SDeepthi Dharwar } 127646bcfad7SDeepthi Dharwar } 127746bcfad7SDeepthi Dharwar cpuidle_resume_and_unlock(); 12786726655dSJiri Kosina put_online_cpus(); 127946bcfad7SDeepthi Dharwar } 128046bcfad7SDeepthi Dharwar 128146bcfad7SDeepthi Dharwar return 0; 128246bcfad7SDeepthi Dharwar } 128346bcfad7SDeepthi Dharwar 128446bcfad7SDeepthi Dharwar static int acpi_processor_registered; 128546bcfad7SDeepthi Dharwar 1286fe7bf106SPaul Gortmaker int acpi_processor_power_init(struct acpi_processor *pr) 12871da177e4SLinus Torvalds { 128846bcfad7SDeepthi Dharwar int retval; 12893d339dcbSDaniel Lezcano struct cpuidle_device *dev; 12901da177e4SLinus Torvalds 1291d1896049SThomas Renninger if (disabled_by_idle_boot_param()) 129236a91358SVenkatesh Pallipadi return 0; 12931da177e4SLinus Torvalds 129435ae7133SSudeep Holla acpi_processor_cstate_first_run_checks(); 12951da177e4SLinus Torvalds 129635ae7133SSudeep Holla if (!acpi_processor_get_power_info(pr)) 12974f86d3a8SLen Brown pr->flags.power_setup_done = 1; 12981da177e4SLinus Torvalds 12991da177e4SLinus Torvalds /* 13001da177e4SLinus Torvalds * Install the idle handler if processor power management is supported. 13011da177e4SLinus Torvalds * Note that we use previously set idle handler will be used on 13021da177e4SLinus Torvalds * platforms that only support C1. 13031da177e4SLinus Torvalds */ 130436a91358SVenkatesh Pallipadi if (pr->flags.power) { 130546bcfad7SDeepthi Dharwar /* Register acpi_idle_driver if not already registered */ 130646bcfad7SDeepthi Dharwar if (!acpi_processor_registered) { 130746bcfad7SDeepthi Dharwar acpi_processor_setup_cpuidle_states(pr); 130846bcfad7SDeepthi Dharwar retval = cpuidle_register_driver(&acpi_idle_driver); 130946bcfad7SDeepthi Dharwar if (retval) 131046bcfad7SDeepthi Dharwar return retval; 1311b6ec26fbSSudeep Holla pr_debug("%s registered with cpuidle\n", 131246bcfad7SDeepthi Dharwar acpi_idle_driver.name); 131346bcfad7SDeepthi Dharwar } 13143d339dcbSDaniel Lezcano 13153d339dcbSDaniel Lezcano dev = kzalloc(sizeof(*dev), GFP_KERNEL); 13163d339dcbSDaniel Lezcano if (!dev) 13173d339dcbSDaniel Lezcano return -ENOMEM; 13183d339dcbSDaniel Lezcano per_cpu(acpi_cpuidle_device, pr->id) = dev; 13193d339dcbSDaniel Lezcano 1320a36a7fecSSudeep Holla acpi_processor_setup_cpuidle_dev(pr, dev); 13213d339dcbSDaniel Lezcano 132246bcfad7SDeepthi Dharwar /* Register per-cpu cpuidle_device. Cpuidle driver 132346bcfad7SDeepthi Dharwar * must already be registered before registering device 132446bcfad7SDeepthi Dharwar */ 13253d339dcbSDaniel Lezcano retval = cpuidle_register_device(dev); 132646bcfad7SDeepthi Dharwar if (retval) { 132746bcfad7SDeepthi Dharwar if (acpi_processor_registered == 0) 132846bcfad7SDeepthi Dharwar cpuidle_unregister_driver(&acpi_idle_driver); 132946bcfad7SDeepthi Dharwar return retval; 133046bcfad7SDeepthi Dharwar } 133146bcfad7SDeepthi Dharwar acpi_processor_registered++; 13321da177e4SLinus Torvalds } 1333d550d98dSPatrick Mochel return 0; 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 133638a991b6SDaniel Lezcano int acpi_processor_power_exit(struct acpi_processor *pr) 13371da177e4SLinus Torvalds { 13383d339dcbSDaniel Lezcano struct cpuidle_device *dev = per_cpu(acpi_cpuidle_device, pr->id); 13393d339dcbSDaniel Lezcano 1340d1896049SThomas Renninger if (disabled_by_idle_boot_param()) 134136a91358SVenkatesh Pallipadi return 0; 134236a91358SVenkatesh Pallipadi 134346bcfad7SDeepthi Dharwar if (pr->flags.power) { 13443d339dcbSDaniel Lezcano cpuidle_unregister_device(dev); 134546bcfad7SDeepthi Dharwar acpi_processor_registered--; 134646bcfad7SDeepthi Dharwar if (acpi_processor_registered == 0) 134746bcfad7SDeepthi Dharwar cpuidle_unregister_driver(&acpi_idle_driver); 134846bcfad7SDeepthi Dharwar } 13491da177e4SLinus Torvalds 135046bcfad7SDeepthi Dharwar pr->flags.power_setup_done = 0; 1351d550d98dSPatrick Mochel return 0; 13521da177e4SLinus Torvalds } 1353