From d6a77ead21b69c395ca6d09a066ededfac601bcc Mon Sep 17 00:00:00 2001 From: Konrad Rzeszutek Wilk Date: Tue, 14 May 2013 17:09:16 +0000 Subject: x86 / ACPI / sleep: Provide registration for acpi_suspend_lowlevel. Which by default will be x86_acpi_suspend_lowlevel. This registration allows us to register another callback if there is a need to use another platform specific callback. Signed-off-by: Liang Tang Signed-off-by: Konrad Rzeszutek Wilk Tested-by: Ben Guthro Acked-by: "H. Peter Anvin" Signed-off-by: Rafael J. Wysocki diff --git a/arch/x86/include/asm/acpi.h b/arch/x86/include/asm/acpi.h index b31bf97..2dfac58 100644 --- a/arch/x86/include/asm/acpi.h +++ b/arch/x86/include/asm/acpi.h @@ -111,7 +111,7 @@ static inline void acpi_disable_pci(void) } /* Low-level suspend routine. */ -extern int acpi_suspend_lowlevel(void); +extern int (*acpi_suspend_lowlevel)(void); /* Physical address to resume after wakeup */ #define acpi_wakeup_address ((unsigned long)(real_mode_header->wakeup_start)) diff --git a/arch/x86/kernel/acpi/boot.c b/arch/x86/kernel/acpi/boot.c index 230c8ea..d81a972 100644 --- a/arch/x86/kernel/acpi/boot.c +++ b/arch/x86/kernel/acpi/boot.c @@ -44,6 +44,7 @@ #include #include +#include "sleep.h" /* To include x86_acpi_suspend_lowlevel */ static int __initdata acpi_force = 0; u32 acpi_rsdt_forced; int acpi_disabled; @@ -559,6 +560,12 @@ static int acpi_register_gsi_ioapic(struct device *dev, u32 gsi, int (*__acpi_register_gsi)(struct device *dev, u32 gsi, int trigger, int polarity) = acpi_register_gsi_pic; +#ifdef CONFIG_ACPI_SLEEP +int (*acpi_suspend_lowlevel)(void) = x86_acpi_suspend_lowlevel; +#else +int (*acpi_suspend_lowlevel)(void); +#endif + /* * success: return IRQ number (>=0) * failure: return < 0 diff --git a/arch/x86/kernel/acpi/sleep.c b/arch/x86/kernel/acpi/sleep.c index b44577b..2a34aaf 100644 --- a/arch/x86/kernel/acpi/sleep.c +++ b/arch/x86/kernel/acpi/sleep.c @@ -26,12 +26,12 @@ static char temp_stack[4096]; #endif /** - * acpi_suspend_lowlevel - save kernel state + * x86_acpi_suspend_lowlevel - save kernel state * * Create an identity mapped page table and copy the wakeup routine to * low memory. */ -int acpi_suspend_lowlevel(void) +int x86_acpi_suspend_lowlevel(void) { struct wakeup_header *header = (struct wakeup_header *) __va(real_mode_header->wakeup_header); diff --git a/arch/x86/kernel/acpi/sleep.h b/arch/x86/kernel/acpi/sleep.h index 67f59f8..c9c2c98 100644 --- a/arch/x86/kernel/acpi/sleep.h +++ b/arch/x86/kernel/acpi/sleep.h @@ -15,3 +15,5 @@ extern unsigned long acpi_copy_wakeup_routine(unsigned long); extern void wakeup_long64(void); extern void do_suspend_lowlevel(void); + +extern int x86_acpi_suspend_lowlevel(void); diff --git a/drivers/acpi/sleep.c b/drivers/acpi/sleep.c index 9c1a435..187ab61 100644 --- a/drivers/acpi/sleep.c +++ b/drivers/acpi/sleep.c @@ -494,6 +494,8 @@ static int acpi_suspend_enter(suspend_state_t pm_state) break; case ACPI_STATE_S3: + if (!acpi_suspend_lowlevel) + return -ENOSYS; error = acpi_suspend_lowlevel(); if (error) return error; -- cgit v0.10.2 From 068e0dc7b7c1db9801e3d7f2ba5cb1d2a552a35b Mon Sep 17 00:00:00 2001 From: Konrad Rzeszutek Wilk Date: Tue, 14 May 2013 17:46:12 +0000 Subject: xen / ACPI / sleep: Register an acpi_suspend_lowlevel callback. We piggyback on "x86/acpi: Provide registration for acpi_suspend_lowlevel." to register a Xen version of the callback. The callback does not do anything special - except it omits the x86_acpi_suspend_lowlevel. This is necessary b/c during suspend the generic code tries to write cr3 values that clashes with what the hypervisor has set up for the guest. Signed-off-by: Liang Tang Signed-off-by: Konrad Rzeszutek Wilk Tested-by: Ben Guthro Acked-by: H. Peter Anvin Signed-off-by: Rafael J. Wysocki diff --git a/include/xen/acpi.h b/include/xen/acpi.h index 68d73d0..46aa3d1 100644 --- a/include/xen/acpi.h +++ b/include/xen/acpi.h @@ -78,11 +78,25 @@ static inline int xen_acpi_get_pxm(acpi_handle h) int xen_acpi_notify_hypervisor_state(u8 sleep_state, u32 pm1a_cnt, u32 pm1b_cnd); +static inline int xen_acpi_suspend_lowlevel(void) +{ + /* + * Xen will save and restore CPU context, so + * we can skip that and just go straight to + * the suspend. + */ + acpi_enter_sleep_state(ACPI_STATE_S3); + return 0; +} + static inline void xen_acpi_sleep_register(void) { - if (xen_initial_domain()) + if (xen_initial_domain()) { acpi_os_set_prepare_sleep( &xen_acpi_notify_hypervisor_state); + + acpi_suspend_lowlevel = xen_acpi_suspend_lowlevel; + } } #else static inline void xen_acpi_sleep_register(void) -- cgit v0.10.2 From 95d45d4cab6540e3f2183d86662c255fa4332331 Mon Sep 17 00:00:00 2001 From: Fengguang Wu Date: Tue, 11 Jun 2013 00:55:10 +0200 Subject: ACPI / PM: acpi_processor_suspend() can be static Since acpi_processor_suspend() and acpi_processor_resume() need not be visible outside of the file they are defined in, make them static. [rjw: Changelog] Signed-off-by: Fengguang Wu Signed-off-by: Rafael J. Wysocki diff --git a/drivers/acpi/processor_idle.c b/drivers/acpi/processor_idle.c index eb133c7..0461ccc 100644 --- a/drivers/acpi/processor_idle.c +++ b/drivers/acpi/processor_idle.c @@ -214,13 +214,13 @@ static void lapic_timer_state_broadcast(struct acpi_processor *pr, #ifdef CONFIG_PM_SLEEP static u32 saved_bm_rld; -int acpi_processor_suspend(void) +static int acpi_processor_suspend(void) { acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld); return 0; } -void acpi_processor_resume(void) +static void acpi_processor_resume(void) { u32 resumed_bm_rld; -- cgit v0.10.2 From b25c77efa71178f8281401e492e5c63cf7c34900 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Sun, 16 Jun 2013 00:37:42 +0200 Subject: ACPI / PM: Rename function acpi_device_power_state() and make it static There is a name clash between function acpi_device_power_state() defined in drivers/acpi/device_pm.c and structure type acpi_device_power_state defined in include/acpi/acpi_bus.h, which may be resolved by renaming the function. Additionally, that funtion may be made static, because it is not used anywhere outside of the file it is defined in. Rename acpi_device_power_state() to acpi_dev_pm_get_state(), which better reflects its purpose, and make it static. Signed-off-by: Rafael J. Wysocki diff --git a/drivers/acpi/device_pm.c b/drivers/acpi/device_pm.c index 318fa32..26d3fd7 100644 --- a/drivers/acpi/device_pm.c +++ b/drivers/acpi/device_pm.c @@ -399,7 +399,7 @@ bool acpi_bus_can_wakeup(acpi_handle handle) EXPORT_SYMBOL(acpi_bus_can_wakeup); /** - * acpi_device_power_state - Get preferred power state of ACPI device. + * acpi_dev_pm_get_state - Get preferred power state of ACPI device. * @dev: Device whose preferred target power state to return. * @adev: ACPI device node corresponding to @dev. * @target_state: System state to match the resultant device state. @@ -417,8 +417,8 @@ EXPORT_SYMBOL(acpi_bus_can_wakeup); * Callers must ensure that @dev and @adev are valid pointers and that @adev * actually corresponds to @dev before using this function. */ -int acpi_device_power_state(struct device *dev, struct acpi_device *adev, - u32 target_state, int d_max_in, int *d_min_p) +static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev, + u32 target_state, int d_max_in, int *d_min_p) { char acpi_method[] = "_SxD"; unsigned long long d_min, d_max; @@ -501,7 +501,6 @@ int acpi_device_power_state(struct device *dev, struct acpi_device *adev, } return d_max; } -EXPORT_SYMBOL_GPL(acpi_device_power_state); /** * acpi_pm_device_sleep_state - Get preferred power state of ACPI device. @@ -523,8 +522,8 @@ int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in) return -ENODEV; } - return acpi_device_power_state(dev, adev, acpi_target_system_state(), - d_max_in, d_min_p); + return acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(), + d_max_in, d_min_p); } EXPORT_SYMBOL(acpi_pm_device_sleep_state); @@ -680,8 +679,8 @@ static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev, if (!acpi_device_power_manageable(adev)) return 0; - power_state = acpi_device_power_state(dev, adev, system_state, - ACPI_STATE_D3, NULL); + power_state = acpi_dev_pm_get_state(dev, adev, system_state, + ACPI_STATE_D3, NULL); if (power_state < ACPI_STATE_D0 || power_state > ACPI_STATE_D3) return -EIO; diff --git a/include/acpi/acpi_bus.h b/include/acpi/acpi_bus.h index 636c59f..c3dc203 100644 --- a/include/acpi/acpi_bus.h +++ b/include/acpi/acpi_bus.h @@ -467,8 +467,6 @@ acpi_status acpi_add_pm_notifier(struct acpi_device *adev, acpi_notify_handler handler, void *context); acpi_status acpi_remove_pm_notifier(struct acpi_device *adev, acpi_notify_handler handler); -int acpi_device_power_state(struct device *dev, struct acpi_device *adev, - u32 target_state, int d_max_in, int *d_min_p); int acpi_pm_device_sleep_state(struct device *, int *, int); void acpi_dev_pm_add_dependent(acpi_handle handle, struct device *depdev); void acpi_dev_pm_remove_dependent(acpi_handle handle, struct device *depdev); @@ -484,23 +482,13 @@ static inline acpi_status acpi_remove_pm_notifier(struct acpi_device *adev, { return AE_SUPPORT; } -static inline int __acpi_device_power_state(int m, int *p) +static inline int acpi_pm_device_sleep_state(struct device *d, int *p, int m) { if (p) *p = ACPI_STATE_D0; + return (m >= ACPI_STATE_D0 && m <= ACPI_STATE_D3) ? m : ACPI_STATE_D0; } -static inline int acpi_device_power_state(struct device *dev, - struct acpi_device *adev, - u32 target_state, int d_max_in, - int *d_min_p) -{ - return __acpi_device_power_state(d_max_in, d_min_p); -} -static inline int acpi_pm_device_sleep_state(struct device *d, int *p, int m) -{ - return __acpi_device_power_state(m, p); -} static inline void acpi_dev_pm_add_dependent(acpi_handle handle, struct device *depdev) {} static inline void acpi_dev_pm_remove_dependent(acpi_handle handle, -- cgit v0.10.2 From 4c164ae7d8a7ee1f39b773d97794535c2c193b12 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Sun, 16 Jun 2013 00:37:50 +0200 Subject: ACPI / PM: Replace ACPI_STATE_D3 with ACPI_STATE_D3_COLD in device_pm.c The two symbols ACPI_STATE_D3 and ACPI_STATE_D3_COLD actually represent the same number (4), but ACPI_STATE_D3 is slightly ambigugous, because it may not be clear that it really means D3cold and not D3hot at first sight. Remove that ambiguity from drivers/acpi/device_pm.c by making it use ACPI_STATE_D3_COLD everywhere instead of ACPI_STATE_D3. Signed-off-by: Rafael J. Wysocki diff --git a/drivers/acpi/device_pm.c b/drivers/acpi/device_pm.c index 26d3fd7..afc808e 100644 --- a/drivers/acpi/device_pm.c +++ b/drivers/acpi/device_pm.c @@ -424,7 +424,7 @@ static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev, unsigned long long d_min, d_max; bool wakeup = false; - if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3) + if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD) return -EINVAL; if (d_max_in > ACPI_STATE_D3_HOT) { @@ -443,7 +443,7 @@ static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev, * the lowest limit with the specified one. */ d_min = ACPI_STATE_D0; - d_max = ACPI_STATE_D3; + d_max = ACPI_STATE_D3_COLD; /* * If present, _SxD methods return the minimum D-state (highest power @@ -680,8 +680,8 @@ static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev, return 0; power_state = acpi_dev_pm_get_state(dev, adev, system_state, - ACPI_STATE_D3, NULL); - if (power_state < ACPI_STATE_D0 || power_state > ACPI_STATE_D3) + ACPI_STATE_D3_COLD, NULL); + if (power_state < ACPI_STATE_D0 || power_state > ACPI_STATE_D3_COLD) return -EIO; return acpi_device_set_power(adev, power_state); -- cgit v0.10.2 From fa1675b56537651270e79967b7f1ee4202c83bf6 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Sun, 16 Jun 2013 00:37:59 +0200 Subject: ACPI / PM: Rework and clean up acpi_dev_pm_get_state() The acpi_dev_pm_get_state() function defined in device_pm.c is quite convoluted, which isn't really necessary, and it doesn't validate the values returned by the ACPI methods executed by it appropriately. To address these shortcomings modify it in the following way. (1) Make its return value only mean whether or not it succeeded and pass the device power states determined by it through pointers. (2) Drop the d_max_in argument, used by only one of its callers, from it, and move the code related to d_max_in into that caller, acpi_pm_device_sleep_state(). (3) Make it always check the return value of acpi_evaluate_integer() and handle failures as appropriate. Moreover, make it check if the values returned by the executed ACPI methods are not out of range. (4) Make it check if the values returned by the executed ACPI methods represent valid power states of the given device and handle situations in which that's not the case gracefully. Also update the kerneldoc comments of acpi_dev_pm_get_state() and acpi_pm_device_sleep_state() to reflect the code changes. Signed-off-by: Rafael J. Wysocki diff --git a/drivers/acpi/device_pm.c b/drivers/acpi/device_pm.c index afc808e..fd363b5 100644 --- a/drivers/acpi/device_pm.c +++ b/drivers/acpi/device_pm.c @@ -403,44 +403,37 @@ EXPORT_SYMBOL(acpi_bus_can_wakeup); * @dev: Device whose preferred target power state to return. * @adev: ACPI device node corresponding to @dev. * @target_state: System state to match the resultant device state. - * @d_max_in: Deepest low-power state to take into consideration. - * @d_min_p: Location to store the upper limit of the allowed states range. - * Return value: Preferred power state of the device on success, -ENODEV - * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure + * @d_min_p: Location to store the highest power state available to the device. + * @d_max_p: Location to store the lowest power state available to the device. * - * Find the lowest power (highest number) ACPI device power state that the - * device can be in while the system is in the state represented by - * @target_state. If @d_min_p is set, the highest power (lowest number) device - * power state that @dev can be in for the given system sleep state is stored - * at the location pointed to by it. + * Find the lowest power (highest number) and highest power (lowest number) ACPI + * device power states that the device can be in while the system is in the + * state represented by @target_state. Store the integer numbers representing + * those stats in the memory locations pointed to by @d_max_p and @d_min_p, + * respectively. * * Callers must ensure that @dev and @adev are valid pointers and that @adev * actually corresponds to @dev before using this function. + * + * Returns 0 on success or -ENODATA when one of the ACPI methods fails or + * returns a value that doesn't make sense. The memory locations pointed to by + * @d_max_p and @d_min_p are only modified on success. */ static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev, - u32 target_state, int d_max_in, int *d_min_p) + u32 target_state, int *d_min_p, int *d_max_p) { - char acpi_method[] = "_SxD"; - unsigned long long d_min, d_max; + char method[] = { '_', 'S', '0' + target_state, 'D', '\0' }; + acpi_handle handle = adev->handle; + unsigned long long ret; + int d_min, d_max; bool wakeup = false; + acpi_status status; - if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD) - return -EINVAL; - - if (d_max_in > ACPI_STATE_D3_HOT) { - enum pm_qos_flags_status stat; - - stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF); - if (stat == PM_QOS_FLAGS_ALL) - d_max_in = ACPI_STATE_D3_HOT; - } - - acpi_method[2] = '0' + target_state; /* - * If the sleep state is S0, the lowest limit from ACPI is D3, - * but if the device has _S0W, we will use the value from _S0W - * as the lowest limit from ACPI. Finally, we will constrain - * the lowest limit with the specified one. + * If the system state is S0, the lowest power state the device can be + * in is D3cold, unless the device has _S0W and is supposed to signal + * wakeup, in which case the return value of _S0W has to be used as the + * lowest power state available to the device. */ d_min = ACPI_STATE_D0; d_max = ACPI_STATE_D3_COLD; @@ -449,12 +442,30 @@ static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev, * If present, _SxD methods return the minimum D-state (highest power * state) we can use for the corresponding S-states. Otherwise, the * minimum D-state is D0 (ACPI 3.x). - * - * NOTE: We rely on acpi_evaluate_integer() not clobbering the integer - * provided -- that's our fault recovery, we ignore retval. */ if (target_state > ACPI_STATE_S0) { - acpi_evaluate_integer(adev->handle, acpi_method, NULL, &d_min); + /* + * We rely on acpi_evaluate_integer() not clobbering the integer + * provided if AE_NOT_FOUND is returned. + */ + ret = d_min; + status = acpi_evaluate_integer(handle, method, NULL, &ret); + if ((ACPI_FAILURE(status) && status != AE_NOT_FOUND) + || ret > ACPI_STATE_D3_COLD) + return -ENODATA; + + /* + * We need to handle legacy systems where D3hot and D3cold are + * the same and 3 is returned in both cases, so fall back to + * D3cold if D3hot is not a valid state. + */ + if (!adev->power.states[ret].flags.valid) { + if (ret == ACPI_STATE_D3_HOT) + ret = ACPI_STATE_D3_COLD; + else + return -ENODATA; + } + d_min = ret; wakeup = device_may_wakeup(dev) && adev->wakeup.flags.valid && adev->wakeup.sleep_state >= target_state; } else if (dev_pm_qos_flags(dev, PM_QOS_FLAG_REMOTE_WAKEUP) != @@ -470,36 +481,29 @@ static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev, * can wake the system. _S0W may be valid, too. */ if (wakeup) { - acpi_status status; - - acpi_method[3] = 'W'; - status = acpi_evaluate_integer(adev->handle, acpi_method, NULL, - &d_max); - if (ACPI_FAILURE(status)) { - if (target_state != ACPI_STATE_S0 || - status != AE_NOT_FOUND) + method[3] = 'W'; + status = acpi_evaluate_integer(handle, method, NULL, &ret); + if (status == AE_NOT_FOUND) { + if (target_state > ACPI_STATE_S0) d_max = d_min; - } else if (d_max < d_min) { - /* Warn the user of the broken DSDT */ - printk(KERN_WARNING "ACPI: Wrong value from %s\n", - acpi_method); - /* Sanitize it */ - d_min = d_max; + } else if (ACPI_SUCCESS(status) && ret <= ACPI_STATE_D3_COLD) { + /* Fall back to D3cold if ret is not a valid state. */ + if (!adev->power.states[ret].flags.valid) + ret = ACPI_STATE_D3_COLD; + + d_max = ret > d_min ? ret : d_min; + } else { + return -ENODATA; } } - if (d_max_in < d_min) - return -EINVAL; if (d_min_p) *d_min_p = d_min; - /* constrain d_max with specified lowest limit (max number) */ - if (d_max > d_max_in) { - for (d_max = d_max_in; d_max > d_min; d_max--) { - if (adev->power.states[d_max].flags.valid) - break; - } - } - return d_max; + + if (d_max_p) + *d_max_p = d_max; + + return 0; } /** @@ -508,7 +512,8 @@ static int acpi_dev_pm_get_state(struct device *dev, struct acpi_device *adev, * @d_min_p: Location to store the upper limit of the allowed states range. * @d_max_in: Deepest low-power state to take into consideration. * Return value: Preferred power state of the device on success, -ENODEV - * (if there's no 'struct acpi_device' for @dev) or -EINVAL on failure + * if there's no 'struct acpi_device' for @dev, -EINVAL if @d_max_in is + * incorrect, or -ENODATA on ACPI method failure. * * The caller must ensure that @dev is valid before using this function. */ @@ -516,14 +521,39 @@ int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p, int d_max_in) { acpi_handle handle = DEVICE_ACPI_HANDLE(dev); struct acpi_device *adev; + int ret, d_max; + + if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3_COLD) + return -EINVAL; + + if (d_max_in > ACPI_STATE_D3_HOT) { + enum pm_qos_flags_status stat; + + stat = dev_pm_qos_flags(dev, PM_QOS_FLAG_NO_POWER_OFF); + if (stat == PM_QOS_FLAGS_ALL) + d_max_in = ACPI_STATE_D3_HOT; + } if (!handle || acpi_bus_get_device(handle, &adev)) { dev_dbg(dev, "ACPI handle without context in %s!\n", __func__); return -ENODEV; } - return acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(), - d_max_in, d_min_p); + ret = acpi_dev_pm_get_state(dev, adev, acpi_target_system_state(), + d_min_p, &d_max); + if (ret) + return ret; + + if (d_max_in < *d_min_p) + return -EINVAL; + + if (d_max > d_max_in) { + for (d_max = d_max_in; d_max > *d_min_p; d_max--) { + if (adev->power.states[d_max].flags.valid) + break; + } + } + return d_max; } EXPORT_SYMBOL(acpi_pm_device_sleep_state); @@ -674,17 +704,13 @@ struct acpi_device *acpi_dev_pm_get_node(struct device *dev) static int acpi_dev_pm_low_power(struct device *dev, struct acpi_device *adev, u32 system_state) { - int power_state; + int ret, state; if (!acpi_device_power_manageable(adev)) return 0; - power_state = acpi_dev_pm_get_state(dev, adev, system_state, - ACPI_STATE_D3_COLD, NULL); - if (power_state < ACPI_STATE_D0 || power_state > ACPI_STATE_D3_COLD) - return -EIO; - - return acpi_device_set_power(adev, power_state); + ret = acpi_dev_pm_get_state(dev, adev, system_state, NULL, &state); + return ret ? ret : acpi_device_set_power(adev, state); } /** -- cgit v0.10.2