Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * processor_idle - idle state submodule to the ACPI processor driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *  Copyright (C) 2004, 2005 Dominik Brodowski <linux@brodo.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *  Copyright (C) 2004  Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *  			- Added processor hotplug support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *  Copyright (C) 2005  Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *  			- Added support for C3 on SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #define pr_fmt(fmt) "ACPI: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/acpi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/dmi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/sched.h>       /* need_resched() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/sort.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/tick.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/cpuidle.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <acpi/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  * Include the apic definitions for x86 to have the APIC timer related defines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  * available also for UP (on SMP it gets magically included via linux/smp.h).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28)  * asm/acpi.h is not an option, as it would require more include magic. Also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29)  * creating an empty asm-ia64/apic.h would just trade pest vs. cholera.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #ifdef CONFIG_X86
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <asm/apic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <asm/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define ACPI_PROCESSOR_CLASS            "processor"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define _COMPONENT              ACPI_PROCESSOR_COMPONENT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) ACPI_MODULE_NAME("processor_idle");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #define ACPI_IDLE_STATE_START	(IS_ENABLED(CONFIG_ARCH_HAS_CPU_RELAX) ? 1 : 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) module_param(max_cstate, uint, 0000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) static unsigned int nocst __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) module_param(nocst, uint, 0000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) static int bm_check_disable __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) module_param(bm_check_disable, uint, 0000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) static unsigned int latency_factor __read_mostly = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) module_param(latency_factor, uint, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) static DEFINE_PER_CPU(struct cpuidle_device *, acpi_cpuidle_device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) struct cpuidle_driver acpi_idle_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	.name =		"acpi_idle",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	.owner =	THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #ifdef CONFIG_ACPI_PROCESSOR_CSTATE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) DEFINE_PER_CPU(struct acpi_processor_cx * [CPUIDLE_STATE_MAX], acpi_cstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) static int disabled_by_idle_boot_param(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 	return boot_option_idle_override == IDLE_POLL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 		boot_option_idle_override == IDLE_HALT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70)  * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71)  * For now disable this. Probably a bug somewhere else.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73)  * To skip this limit, boot/load with a large max_cstate limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) static int set_max_cstate(const struct dmi_system_id *id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	pr_notice("%s detected - limiting to C%ld max_cstate."
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 		  " Override with \"processor.max_cstate=%d\"\n", id->ident,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 		  (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	max_cstate = (long)id->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) static const struct dmi_system_id processor_power_dmi_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	{ set_max_cstate, "Clevo 5600D", {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 	  DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	  DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	 (void *)2},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	{ set_max_cstate, "Pavilion zv5000", {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 	  DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	  DMI_MATCH(DMI_PRODUCT_NAME,"Pavilion zv5000 (DS502A#ABA)")},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	 (void *)1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	{ set_max_cstate, "Asus L8400B", {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 	  DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	  DMI_MATCH(DMI_PRODUCT_NAME,"L8400B series Notebook PC")},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	 (void *)1},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107)  * Callers should disable interrupts before the call and enable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108)  * interrupts after return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) static void __cpuidle acpi_safe_halt(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	if (!tif_need_resched()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 		safe_halt();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 		local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) #ifdef ARCH_APICTIMER_STOPS_ON_C3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121)  * Some BIOS implementations switch to C3 in the published C2 state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122)  * This seems to be a common problem on AMD boxen, but other vendors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123)  * are affected too. We pick the most conservative approach: we assume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124)  * that the local APIC stops in both C2 and C3.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) static void lapic_timer_check_state(int state, struct acpi_processor *pr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 				   struct acpi_processor_cx *cx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	struct acpi_processor_power *pwr = &pr->power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	if (cpu_has(&cpu_data(pr->id), X86_FEATURE_ARAT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	if (boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 		type = ACPI_STATE_C1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	 * Check, if one of the previous states already marked the lapic
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	 * unstable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	if (pwr->timer_broadcast_on_state < state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	if (cx->type >= type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		pr->power.timer_broadcast_on_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) static void __lapic_timer_propagate_broadcast(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	struct acpi_processor *pr = (struct acpi_processor *) arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 	if (pr->power.timer_broadcast_on_state < INT_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		tick_broadcast_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		tick_broadcast_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) static void lapic_timer_propagate_broadcast(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	smp_call_function_single(pr->id, __lapic_timer_propagate_broadcast,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 				 (void *)pr, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) /* Power(C) State timer broadcast control */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) static bool lapic_timer_needs_broadcast(struct acpi_processor *pr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 					struct acpi_processor_cx *cx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	return cx - pr->power.states >= pr->power.timer_broadcast_on_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) static void lapic_timer_check_state(int state, struct acpi_processor *pr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 				   struct acpi_processor_cx *cstate) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) static bool lapic_timer_needs_broadcast(struct acpi_processor *pr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 					struct acpi_processor_cx *cx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) #if defined(CONFIG_X86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) static void tsc_check_state(int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	switch (boot_cpu_data.x86_vendor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	case X86_VENDOR_HYGON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	case X86_VENDOR_AMD:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	case X86_VENDOR_INTEL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	case X86_VENDOR_CENTAUR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	case X86_VENDOR_ZHAOXIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		 * AMD Fam10h TSC will tick in all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		 * C/P/S0/S1 states when this bit is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		/* TSC could halt in idle, so notify users */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		if (state > ACPI_STATE_C1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 			mark_tsc_unstable("TSC halts in idle");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) static void tsc_check_state(int state) { return; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	if (!pr->pblk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 	/* if info is obtained from pblk/fadt, type equals state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) #ifndef CONFIG_HOTPLUG_CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	 * Check for P_LVL2_UP flag before entering C2 and above on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	 * an SMP system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	if ((num_online_cpus() > 1) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	    !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	/* determine C2 and C3 address from pblk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	/* determine latencies from FADT */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.c2_latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.c3_latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	 * FADT specified C2 latency must be less than or equal to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	 * 100 microseconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	if (acpi_gbl_FADT.c2_latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 			"C2 latency too large [%d]\n", acpi_gbl_FADT.c2_latency));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 		/* invalidate C2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		pr->power.states[ACPI_STATE_C2].address = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	 * FADT supplied C3 latency must be less than or equal to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	 * 1000 microseconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	if (acpi_gbl_FADT.c3_latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 			"C3 latency too large [%d]\n", acpi_gbl_FADT.c3_latency));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		/* invalidate C3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 		pr->power.states[ACPI_STATE_C3].address = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	ACPI_DEBUG_PRINT((ACPI_DB_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 			  "lvl2[0x%08x] lvl3[0x%08x]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 			  pr->power.states[ACPI_STATE_C2].address,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 			  pr->power.states[ACPI_STATE_C3].address));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	snprintf(pr->power.states[ACPI_STATE_C2].desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 			 ACPI_CX_DESC_LEN, "ACPI P_LVL2 IOPORT 0x%x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 			 pr->power.states[ACPI_STATE_C2].address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	snprintf(pr->power.states[ACPI_STATE_C3].desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 			 ACPI_CX_DESC_LEN, "ACPI P_LVL3 IOPORT 0x%x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 			 pr->power.states[ACPI_STATE_C3].address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	if (!pr->power.states[ACPI_STATE_C1].valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		/* set the first C-State to C1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		/* all processors need to support C1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		pr->power.states[ACPI_STATE_C1].valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 		pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		snprintf(pr->power.states[ACPI_STATE_C1].desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 			 ACPI_CX_DESC_LEN, "ACPI HLT");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	/* the C0 state only exists as a filler in our array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	pr->power.states[ACPI_STATE_C0].valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	if (nocst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	ret = acpi_processor_evaluate_cst(pr->handle, pr->id, &pr->power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	if (!pr->power.count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	pr->flags.has_cst = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 					   struct acpi_processor_cx *cx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	static int bm_check_flag = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	static int bm_control_flag = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	if (!cx->address)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 	 * DMA transfers are used by any ISA device to avoid livelock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	 * Note that we could disable Type-F DMA (as recommended by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	 * the erratum), but this is known to disrupt certain ISA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	 * devices thus we take the conservative approach.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	else if (errata.piix4.fdma) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 				  "C3 not supported on PIIX4 with Type-F DMA\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	/* All the logic here assumes flags.bm_check is same across all CPUs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	if (bm_check_flag == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		/* Determine whether bm_check is needed based on CPU  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 		acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		bm_check_flag = pr->flags.bm_check;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 		bm_control_flag = pr->flags.bm_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		pr->flags.bm_check = bm_check_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		pr->flags.bm_control = bm_control_flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	if (pr->flags.bm_check) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		if (!pr->flags.bm_control) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 			if (pr->flags.has_cst != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 				/* bus mastering control is necessary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 				ACPI_DEBUG_PRINT((ACPI_DB_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 					"C3 support requires BM control\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 				return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 				/* Here we enter C3 without bus mastering */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 				ACPI_DEBUG_PRINT((ACPI_DB_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 					"C3 support without BM control\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		 * WBINVD should be set in fadt, for C3 state to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		 * supported on when bm_check is not required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 					  "Cache invalidation should work properly"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 					  " for C3 to be enabled on SMP systems\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	 * Otherwise we've met all of our C3 requirements.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 	 * Normalize the C3 latency to expidite policy.  Enable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	 * checking of bus mastering status (bm_check) so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	 * use this in our C3 policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	cx->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 	 * On older chipsets, BM_RLD needs to be set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	 * in order for Bus Master activity to wake the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	 * system from C3.  Newer chipsets handle DMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	 * during C3 automatically and BM_RLD is a NOP.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	 * In either case, the proper way to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	 * handle BM_RLD is to set it and leave it set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) static int acpi_cst_latency_cmp(const void *a, const void *b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	const struct acpi_processor_cx *x = a, *y = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	if (!(x->valid && y->valid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	if (x->latency > y->latency)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	if (x->latency < y->latency)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) static void acpi_cst_latency_swap(void *a, void *b, int n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	struct acpi_processor_cx *x = a, *y = b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	u32 tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	if (!(x->valid && y->valid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	tmp = x->latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 	x->latency = y->latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 	y->latency = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) static int acpi_processor_power_verify(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	unsigned int working = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	unsigned int last_latency = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	unsigned int last_type = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	bool buggy_latency = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	pr->power.timer_broadcast_on_state = INT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		struct acpi_processor_cx *cx = &pr->power.states[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		switch (cx->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		case ACPI_STATE_C1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 			cx->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		case ACPI_STATE_C2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 			if (!cx->address)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 			cx->valid = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		case ACPI_STATE_C3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 			acpi_processor_power_verify_c3(pr, cx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		if (!cx->valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		if (cx->type >= last_type && cx->latency < last_latency)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 			buggy_latency = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		last_latency = cx->latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 		last_type = cx->type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		lapic_timer_check_state(i, pr, cx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		tsc_check_state(cx->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 		working++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	if (buggy_latency) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		pr_notice("FW issue: working around C-state latencies out of order\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 		sort(&pr->power.states[1], max_cstate,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 		     sizeof(struct acpi_processor_cx),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 		     acpi_cst_latency_cmp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		     acpi_cst_latency_swap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	lapic_timer_propagate_broadcast(pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	return (working);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	/* NOTE: the idle thread may not be running while calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	 * this function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	/* Zero initialize all the C-states info. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	memset(pr->power.states, 0, sizeof(pr->power.states));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	result = acpi_processor_get_power_info_cst(pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	if (result == -ENODEV)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		result = acpi_processor_get_power_info_fadt(pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 	if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 		return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	acpi_processor_get_power_info_default(pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	pr->power.count = acpi_processor_power_verify(pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	 * if one state of type C2 or C3 is available, mark this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	 * CPU as being "idle manageable"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 	for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		if (pr->power.states[i].valid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 			pr->power.count = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 			pr->flags.power = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508)  * acpi_idle_bm_check - checks if bus master activity was detected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) static int acpi_idle_bm_check(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	u32 bm_status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 	if (bm_check_disable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	if (bm_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	 * the true state of bus mastering activity; forcing us to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	 * manually check the BMIDEA bit of each IDE channel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	else if (errata.piix4.bmisx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		    || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 			bm_status = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	return bm_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) static void wait_for_freeze(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) #ifdef	CONFIG_X86
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	/* No delay is needed if we are in guest */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	/* Dummy wait op - must do something useless after P_LVL2 read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	   because chipsets cannot guarantee that STPCLK# signal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	   gets asserted in time to freeze execution properly. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	inl(acpi_gbl_FADT.xpm_timer_block.address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547)  * acpi_idle_do_entry - enter idle state using the appropriate method
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548)  * @cx: cstate data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550)  * Caller disables interrupt before call and enables interrupt after return.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) static void __cpuidle acpi_idle_do_entry(struct acpi_processor_cx *cx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	if (cx->entry_method == ACPI_CSTATE_FFH) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		/* Call into architectural FFH based C-state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		acpi_processor_ffh_cstate_enter(cx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	} else if (cx->entry_method == ACPI_CSTATE_HALT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		acpi_safe_halt();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		/* IO port based C-state */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		inb(cx->address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		wait_for_freeze();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567)  * acpi_idle_play_dead - enters an ACPI state for long-term idle (i.e. off-lining)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568)  * @dev: the target CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569)  * @index: the index of suggested state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) static int acpi_idle_play_dead(struct cpuidle_device *dev, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	ACPI_FLUSH_CPU_CACHE();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		if (cx->entry_method == ACPI_CSTATE_HALT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 			safe_halt();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		else if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 			inb(cx->address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 			wait_for_freeze();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 			return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) #if defined(CONFIG_X86) && defined(CONFIG_HOTPLUG_CPU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		cond_wakeup_cpu0();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	/* Never reached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) static bool acpi_idle_fallback_to_c1(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	return IS_ENABLED(CONFIG_HOTPLUG_CPU) && !pr->flags.has_cst &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		!(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) static int c3_cpu_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) static DEFINE_RAW_SPINLOCK(c3_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606)  * acpi_idle_enter_bm - enters C3 with proper BM handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607)  * @drv: cpuidle driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608)  * @pr: Target processor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609)  * @cx: Target state context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610)  * @index: index of target state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) static int acpi_idle_enter_bm(struct cpuidle_driver *drv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			       struct acpi_processor *pr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 			       struct acpi_processor_cx *cx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 			       int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	static struct acpi_processor_cx safe_cx = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		.entry_method = ACPI_CSTATE_HALT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	 * disable bus master
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	 * bm_check implies we need ARB_DIS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	 * bm_control implies whether we can do ARB_DIS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	 * That leaves a case where bm_check is set and bm_control is not set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	 * In that case we cannot do much, we enter C3 without doing anything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	bool dis_bm = pr->flags.bm_control;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	/* If we can skip BM, demote to a safe state. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	if (!cx->bm_sts_skip && acpi_idle_bm_check()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		dis_bm = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		index = drv->safe_state_index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		if (index >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 			cx = this_cpu_read(acpi_cstate[index]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 			cx = &safe_cx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 			index = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	if (dis_bm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 		raw_spin_lock(&c3_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 		c3_cpu_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		/* Disable bus master arbitration when all CPUs are in C3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		if (c3_cpu_count == num_online_cpus())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 			acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 		raw_spin_unlock(&c3_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	rcu_idle_enter();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	acpi_idle_do_entry(cx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	rcu_idle_exit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	/* Re-enable bus master arbitration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	if (dis_bm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		raw_spin_lock(&c3_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 		c3_cpu_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		raw_spin_unlock(&c3_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) static int acpi_idle_enter(struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 			   struct cpuidle_driver *drv, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	struct acpi_processor *pr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	pr = __this_cpu_read(processors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	if (unlikely(!pr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	if (cx->type != ACPI_STATE_C1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 		if (cx->type == ACPI_STATE_C3 && pr->flags.bm_check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 			return acpi_idle_enter_bm(drv, pr, cx, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		/* C2 to C1 demotion. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 		if (acpi_idle_fallback_to_c1(pr) && num_online_cpus() > 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 			index = ACPI_IDLE_STATE_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 			cx = per_cpu(acpi_cstate[index], dev->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	if (cx->type == ACPI_STATE_C3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 		ACPI_FLUSH_CPU_CACHE();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	acpi_idle_do_entry(cx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) static int acpi_idle_enter_s2idle(struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 				  struct cpuidle_driver *drv, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	if (cx->type == ACPI_STATE_C3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		struct acpi_processor *pr = __this_cpu_read(processors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		if (unlikely(!pr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		if (pr->flags.bm_check) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 			u8 bm_sts_skip = cx->bm_sts_skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 			/* Don't check BM_STS, do an unconditional ARB_DIS for S2IDLE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 			cx->bm_sts_skip = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 			acpi_idle_enter_bm(drv, pr, cx, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 			cx->bm_sts_skip = bm_sts_skip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 			ACPI_FLUSH_CPU_CACHE();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	acpi_idle_do_entry(cx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 					   struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	int i, count = ACPI_IDLE_STATE_START;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	struct acpi_processor_cx *cx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	struct cpuidle_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	if (max_cstate == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		max_cstate = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		state = &acpi_idle_driver.states[count];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		cx = &pr->power.states[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		if (!cx->valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		per_cpu(acpi_cstate[count], dev->cpu) = cx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 		if (lapic_timer_needs_broadcast(pr, cx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 			state->flags |= CPUIDLE_FLAG_TIMER_STOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		if (cx->type == ACPI_STATE_C3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			state->flags |= CPUIDLE_FLAG_TLB_FLUSHED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 			if (pr->flags.bm_check)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 				state->flags |= CPUIDLE_FLAG_RCU_IDLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		if (count == CPUIDLE_STATE_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) static int acpi_processor_setup_cstates(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	int i, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	struct acpi_processor_cx *cx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	struct cpuidle_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	struct cpuidle_driver *drv = &acpi_idle_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	if (max_cstate == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 		max_cstate = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	if (IS_ENABLED(CONFIG_ARCH_HAS_CPU_RELAX)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		cpuidle_poll_state_init(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		count = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		cx = &pr->power.states[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		if (!cx->valid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 		state = &drv->states[count];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		strlcpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 		state->exit_latency = cx->latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		state->target_residency = cx->latency * latency_factor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		state->enter = acpi_idle_enter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		state->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		if (cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 			state->enter_dead = acpi_idle_play_dead;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 			drv->safe_state_index = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		 * Halt-induced C1 is not good for ->enter_s2idle, because it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		 * re-enables interrupts on exit.  Moreover, C1 is generally not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		 * particularly interesting from the suspend-to-idle angle, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		 * avoid C1 and the situations in which we may need to fall back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		 * to it altogether.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		if (cx->type != ACPI_STATE_C1 && !acpi_idle_fallback_to_c1(pr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 			state->enter_s2idle = acpi_idle_enter_s2idle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 		if (count == CPUIDLE_STATE_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	drv->state_count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	if (!count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) static inline void acpi_processor_cstate_first_run_checks(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	static int first_run;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	if (first_run)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	dmi_check_system(processor_power_dmi_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	max_cstate = acpi_processor_cstate_check(max_cstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	if (max_cstate < ACPI_C_STATES_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		pr_notice("ACPI: processor limited to max C-state %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 			  max_cstate);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	first_run++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	if (nocst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	acpi_processor_claim_cst_control();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) static inline int disabled_by_idle_boot_param(void) { return 0; }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) static inline void acpi_processor_cstate_first_run_checks(void) { }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 					   struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) static int acpi_processor_setup_cstates(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) #endif /* CONFIG_ACPI_PROCESSOR_CSTATE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) struct acpi_lpi_states_array {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	unsigned int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	unsigned int composite_states_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	struct acpi_lpi_state *entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	struct acpi_lpi_state *composite_states[ACPI_PROCESSOR_MAX_POWER];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) static int obj_get_integer(union acpi_object *obj, u32 *value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	if (obj->type != ACPI_TYPE_INTEGER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	*value = obj->integer.value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) static int acpi_processor_evaluate_lpi(acpi_handle handle,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 				       struct acpi_lpi_states_array *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	int pkg_count, state_idx = 1, loop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 	union acpi_object *lpi_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	struct acpi_lpi_state *lpi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	status = acpi_evaluate_object(handle, "_LPI", NULL, &buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	if (ACPI_FAILURE(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _LPI, giving up\n"));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	lpi_data = buffer.pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	/* There must be at least 4 elements = 3 elements + 1 package */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	if (!lpi_data || lpi_data->type != ACPI_TYPE_PACKAGE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	    lpi_data->package.count < 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		pr_debug("not enough elements in _LPI\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		ret = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	pkg_count = lpi_data->package.elements[2].integer.value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	/* Validate number of power states. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	if (pkg_count < 1 || pkg_count != lpi_data->package.count - 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		pr_debug("count given by _LPI is not valid\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		ret = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	lpi_state = kcalloc(pkg_count, sizeof(*lpi_state), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	if (!lpi_state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	info->size = pkg_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	info->entries = lpi_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	/* LPI States start at index 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	for (loop = 3; state_idx <= pkg_count; loop++, state_idx++, lpi_state++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		union acpi_object *element, *pkg_elem, *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		element = &lpi_data->package.elements[loop];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 		if (element->type != ACPI_TYPE_PACKAGE || element->package.count < 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		pkg_elem = element->package.elements;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 		obj = pkg_elem + 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		if (obj->type == ACPI_TYPE_BUFFER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 			struct acpi_power_register *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 			reg = (struct acpi_power_register *)obj->buffer.pointer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 			if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 			    reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 			lpi_state->address = reg->address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 			lpi_state->entry_method =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 				reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 				ACPI_CSTATE_FFH : ACPI_CSTATE_SYSTEMIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 		} else if (obj->type == ACPI_TYPE_INTEGER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 			lpi_state->entry_method = ACPI_CSTATE_INTEGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 			lpi_state->address = obj->integer.value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 		/* elements[7,8] skipped for now i.e. Residency/Usage counter*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 		obj = pkg_elem + 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		if (obj->type == ACPI_TYPE_STRING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 			strlcpy(lpi_state->desc, obj->string.pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 				ACPI_CX_DESC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 		lpi_state->index = state_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 		if (obj_get_integer(pkg_elem + 0, &lpi_state->min_residency)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			pr_debug("No min. residency found, assuming 10 us\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			lpi_state->min_residency = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		if (obj_get_integer(pkg_elem + 1, &lpi_state->wake_latency)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 			pr_debug("No wakeup residency found, assuming 10 us\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 			lpi_state->wake_latency = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		if (obj_get_integer(pkg_elem + 2, &lpi_state->flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 			lpi_state->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		if (obj_get_integer(pkg_elem + 3, &lpi_state->arch_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			lpi_state->arch_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		if (obj_get_integer(pkg_elem + 4, &lpi_state->res_cnt_freq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 			lpi_state->res_cnt_freq = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		if (obj_get_integer(pkg_elem + 5, &lpi_state->enable_parent_state))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 			lpi_state->enable_parent_state = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	acpi_handle_debug(handle, "Found %d power states\n", state_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	kfree(buffer.pointer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992)  * flat_state_cnt - the number of composite LPI states after the process of flattening
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) static int flat_state_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997)  * combine_lpi_states - combine local and parent LPI states to form a composite LPI state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999)  * @local: local LPI state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)  * @parent: parent LPI state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)  * @result: composite LPI state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) static bool combine_lpi_states(struct acpi_lpi_state *local,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 			       struct acpi_lpi_state *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 			       struct acpi_lpi_state *result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	if (parent->entry_method == ACPI_CSTATE_INTEGER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		if (!parent->address) /* 0 means autopromotable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		result->address = local->address + parent->address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		result->address = parent->address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	result->min_residency = max(local->min_residency, parent->min_residency);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	result->wake_latency = local->wake_latency + parent->wake_latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	result->enable_parent_state = parent->enable_parent_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	result->entry_method = local->entry_method;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	result->flags = parent->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	result->arch_flags = parent->arch_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	result->index = parent->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	strlcpy(result->desc, local->desc, ACPI_CX_DESC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	strlcat(result->desc, "+", ACPI_CX_DESC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	strlcat(result->desc, parent->desc, ACPI_CX_DESC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) #define ACPI_LPI_STATE_FLAGS_ENABLED			BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) static void stash_composite_state(struct acpi_lpi_states_array *curr_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 				  struct acpi_lpi_state *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	curr_level->composite_states[curr_level->composite_states_size++] = t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) static int flatten_lpi_states(struct acpi_processor *pr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 			      struct acpi_lpi_states_array *curr_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 			      struct acpi_lpi_states_array *prev_level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	int i, j, state_count = curr_level->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	struct acpi_lpi_state *p, *t = curr_level->entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	curr_level->composite_states_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	for (j = 0; j < state_count; j++, t++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		struct acpi_lpi_state *flpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		if (!(t->flags & ACPI_LPI_STATE_FLAGS_ENABLED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		if (flat_state_cnt >= ACPI_PROCESSOR_MAX_POWER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 			pr_warn("Limiting number of LPI states to max (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 				ACPI_PROCESSOR_MAX_POWER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 			pr_warn("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		flpi = &pr->power.lpi_states[flat_state_cnt];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 		if (!prev_level) { /* leaf/processor node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 			memcpy(flpi, t, sizeof(*t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 			stash_composite_state(curr_level, flpi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 			flat_state_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		for (i = 0; i < prev_level->composite_states_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 			p = prev_level->composite_states[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 			if (t->index <= p->enable_parent_state &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 			    combine_lpi_states(p, t, flpi)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 				stash_composite_state(curr_level, flpi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 				flat_state_cnt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 				flpi++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	kfree(curr_level->entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) static int acpi_processor_get_lpi_info(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	int ret, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	acpi_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	acpi_handle handle = pr->handle, pr_ahandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	struct acpi_device *d = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	struct acpi_lpi_states_array info[2], *tmp, *prev, *curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	if (!osc_pc_lpi_support_confirmed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	if (!acpi_has_method(handle, "_LPI"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	flat_state_cnt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	prev = &info[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	curr = &info[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	handle = pr->handle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	ret = acpi_processor_evaluate_lpi(handle, prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	flatten_lpi_states(pr, prev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	status = acpi_get_parent(handle, &pr_ahandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	while (ACPI_SUCCESS(status)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 		acpi_bus_get_device(pr_ahandle, &d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 		handle = pr_ahandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		if (strcmp(acpi_device_hid(d), ACPI_PROCESSOR_CONTAINER_HID))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 		/* can be optional ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 		if (!acpi_has_method(handle, "_LPI"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		ret = acpi_processor_evaluate_lpi(handle, curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		/* flatten all the LPI states in this level of hierarchy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		flatten_lpi_states(pr, curr, prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 		tmp = prev, prev = curr, curr = tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 		status = acpi_get_parent(handle, &pr_ahandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	pr->power.count = flat_state_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	/* reset the index after flattening */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	for (i = 0; i < pr->power.count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		pr->power.lpi_states[i].index = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	/* Tell driver that _LPI is supported. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	pr->flags.has_lpi = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	pr->flags.power = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) int __weak acpi_processor_ffh_lpi_probe(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) int __weak acpi_processor_ffh_lpi_enter(struct acpi_lpi_state *lpi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153)  * acpi_idle_lpi_enter - enters an ACPI any LPI state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154)  * @dev: the target CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155)  * @drv: cpuidle driver containing cpuidle state info
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156)  * @index: index of target state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158)  * Return: 0 for success or negative value for error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) static int acpi_idle_lpi_enter(struct cpuidle_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 			       struct cpuidle_driver *drv, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	struct acpi_processor *pr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	struct acpi_lpi_state *lpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	pr = __this_cpu_read(processors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 	if (unlikely(!pr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	lpi = &pr->power.lpi_states[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	if (lpi->entry_method == ACPI_CSTATE_FFH)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		return acpi_processor_ffh_lpi_enter(lpi);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) static int acpi_processor_setup_lpi_states(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	struct acpi_lpi_state *lpi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	struct cpuidle_state *state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	struct cpuidle_driver *drv = &acpi_idle_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	if (!pr->flags.has_lpi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	for (i = 0; i < pr->power.count && i < CPUIDLE_STATE_MAX; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 		lpi = &pr->power.lpi_states[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		state = &drv->states[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 		snprintf(state->name, CPUIDLE_NAME_LEN, "LPI-%d", i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 		strlcpy(state->desc, lpi->desc, CPUIDLE_DESC_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		state->exit_latency = lpi->wake_latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		state->target_residency = lpi->min_residency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		if (lpi->arch_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 			state->flags |= CPUIDLE_FLAG_TIMER_STOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 		state->enter = acpi_idle_lpi_enter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		drv->safe_state_index = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	drv->state_count = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)  * acpi_processor_setup_cpuidle_states- prepares and configures cpuidle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209)  * global state data i.e. idle routines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)  * @pr: the ACPI processor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) static int acpi_processor_setup_cpuidle_states(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	struct cpuidle_driver *drv = &acpi_idle_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	if (!pr->flags.power_setup_done || !pr->flags.power)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	drv->safe_state_index = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	for (i = ACPI_IDLE_STATE_START; i < CPUIDLE_STATE_MAX; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 		drv->states[i].name[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		drv->states[i].desc[0] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	if (pr->flags.has_lpi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 		return acpi_processor_setup_lpi_states(pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	return acpi_processor_setup_cstates(pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234)  * acpi_processor_setup_cpuidle_dev - prepares and configures CPUIDLE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235)  * device i.e. per-cpu data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237)  * @pr: the ACPI processor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238)  * @dev : the cpuidle device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) static int acpi_processor_setup_cpuidle_dev(struct acpi_processor *pr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 					    struct cpuidle_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	if (!pr->flags.power_setup_done || !pr->flags.power || !dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	dev->cpu = pr->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	if (pr->flags.has_lpi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		return acpi_processor_ffh_lpi_probe(pr->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	return acpi_processor_setup_cpuidle_cx(pr, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) static int acpi_processor_get_power_info(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	ret = acpi_processor_get_lpi_info(pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		ret = acpi_processor_get_cstate_info(pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) int acpi_processor_hotplug(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	struct cpuidle_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	if (disabled_by_idle_boot_param())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 	if (!pr->flags.power_setup_done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 	dev = per_cpu(acpi_cpuidle_device, pr->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	cpuidle_pause_and_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	cpuidle_disable_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	ret = acpi_processor_get_power_info(pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	if (!ret && pr->flags.power) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		acpi_processor_setup_cpuidle_dev(pr, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		ret = cpuidle_enable_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	cpuidle_resume_and_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) int acpi_processor_power_state_has_changed(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 	struct acpi_processor *_pr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	struct cpuidle_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 	if (disabled_by_idle_boot_param())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	if (!pr->flags.power_setup_done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	 * FIXME:  Design the ACPI notification to make it once per
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	 * system instead of once per-cpu.  This condition is a hack
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	 * to make the code that updates C-States be called once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 	if (pr->id == 0 && cpuidle_get_driver() == &acpi_idle_driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		/* Protect against cpu-hotplug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 		get_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 		cpuidle_pause_and_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		/* Disable all cpuidle devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 			_pr = per_cpu(processors, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 			if (!_pr || !_pr->flags.power_setup_done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 			dev = per_cpu(acpi_cpuidle_device, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 			cpuidle_disable_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 		/* Populate Updated C-state information */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		acpi_processor_get_power_info(pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 		acpi_processor_setup_cpuidle_states(pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		/* Enable all cpuidle devices */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 		for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 			_pr = per_cpu(processors, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 			if (!_pr || !_pr->flags.power_setup_done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 			acpi_processor_get_power_info(_pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 			if (_pr->flags.power) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 				dev = per_cpu(acpi_cpuidle_device, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 				acpi_processor_setup_cpuidle_dev(_pr, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 				cpuidle_enable_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		cpuidle_resume_and_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 		put_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) static int acpi_processor_registered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) int acpi_processor_power_init(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 	int retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 	struct cpuidle_device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 	if (disabled_by_idle_boot_param())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	acpi_processor_cstate_first_run_checks();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	if (!acpi_processor_get_power_info(pr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		pr->flags.power_setup_done = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 	 * Install the idle handler if processor power management is supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	 * Note that we use previously set idle handler will be used on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	 * platforms that only support C1.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	if (pr->flags.power) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 		/* Register acpi_idle_driver if not already registered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 		if (!acpi_processor_registered) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 			acpi_processor_setup_cpuidle_states(pr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 			retval = cpuidle_register_driver(&acpi_idle_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 			if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 				return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 			pr_debug("%s registered with cpuidle\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 				 acpi_idle_driver.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		dev = kzalloc(sizeof(*dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		per_cpu(acpi_cpuidle_device, pr->id) = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 		acpi_processor_setup_cpuidle_dev(pr, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 		/* Register per-cpu cpuidle_device. Cpuidle driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 		 * must already be registered before registering device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		retval = cpuidle_register_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		if (retval) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 			if (acpi_processor_registered == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 				cpuidle_unregister_driver(&acpi_idle_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 			return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		acpi_processor_registered++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) int acpi_processor_power_exit(struct acpi_processor *pr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	struct cpuidle_device *dev = per_cpu(acpi_cpuidle_device, pr->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	if (disabled_by_idle_boot_param())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	if (pr->flags.power) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 		cpuidle_unregister_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 		acpi_processor_registered--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 		if (acpi_processor_registered == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 			cpuidle_unregister_driver(&acpi_idle_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	pr->flags.power_setup_done = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) }