From ee85f543710dd56ce526cb44e39191f32972e5ad Mon Sep 17 00:00:00 2001 From: Huang Ying Date: Sat, 23 Jun 2012 10:23:48 +0800 Subject: ACPI/PM: specify lowest allowed state for device sleep state Lower device sleep state can save more power, but has more exit latency too. Sometimes, to satisfy some power QoS and other requirement, we need to constrain the lowest device sleep state. In this patch, a parameter to specify lowest allowed state for acpi_pm_device_sleep_state is added. So that the caller can enforce the constraint via the parameter. This is needed by PCIe D3cold support, where the lowest power state allowed may be D3_HOT instead of default D3_COLD. CC: Len Brown CC: linux-acpi@vger.kernel.org Reviewed-by: Rafael J. Wysocki Signed-off-by: Huang Ying Signed-off-by: Bjorn Helgaas --- drivers/acpi/sleep.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'drivers/acpi/sleep.c') diff --git a/drivers/acpi/sleep.c b/drivers/acpi/sleep.c index 88561029cca8..1cc02ca2af2a 100644 --- a/drivers/acpi/sleep.c +++ b/drivers/acpi/sleep.c @@ -716,8 +716,9 @@ int acpi_suspend(u32 acpi_state) * @dev: device to examine; its driver model wakeup flags control * whether it should be able to wake up the system * @d_min_p: used to store the upper limit of allowed states range - * Return value: preferred power state of the device on success, -ENODEV on - * failure (ie. if there's no 'struct acpi_device' for @dev) + * @d_max_in: specify the lowest allowed states + * Return value: preferred power state of the device on success, -ENODEV + * (ie. if there's no 'struct acpi_device' for @dev) or -EINVAL on failure * * Find the lowest power (highest number) ACPI device power state that * device @dev can be in while the system is in the sleep state represented @@ -732,13 +733,15 @@ int acpi_suspend(u32 acpi_state) * via @wake. */ -int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p) +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; char acpi_method[] = "_SxD"; unsigned long long d_min, d_max; + if (d_max_in < ACPI_STATE_D0 || d_max_in > ACPI_STATE_D3) + return -EINVAL; if (!handle || ACPI_FAILURE(acpi_bus_get_device(handle, &adev))) { printk(KERN_DEBUG "ACPI handle has no context!\n"); return -ENODEV; @@ -746,8 +749,10 @@ int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p) acpi_method[2] = '0' + acpi_target_sleep_state; /* - * If the sleep state is S0, we will return D3, but if the device has - * _S0W, we will use the value from _S0W + * 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. */ d_min = ACPI_STATE_D0; d_max = ACPI_STATE_D3; @@ -791,8 +796,17 @@ int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p) } } + 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; } #endif /* CONFIG_PM */ -- cgit v1.2.3 From 3bd46600a7a7e938c54df8cdbac9910668c7dfb0 Mon Sep 17 00:00:00 2001 From: Lin Ming Date: Mon, 25 Jun 2012 16:13:06 +0800 Subject: libata-acpi: add ata port runtime D3Cold support ATA port may support runtime D3Cold state, for example, Zero-power ODD case. This patch adds wakeup notifier and enable/disable run_wake during supend/resume. Signed-off-by: Lin Ming Signed-off-by: Jeff Garzik --- drivers/acpi/sleep.c | 2 ++ drivers/ata/libata-acpi.c | 78 ++++++++++++++++++++++++++++++++++++++++++++--- drivers/ata/libata-scsi.c | 3 ++ drivers/ata/libata.h | 4 +++ 4 files changed, 83 insertions(+), 4 deletions(-) (limited to 'drivers/acpi/sleep.c') diff --git a/drivers/acpi/sleep.c b/drivers/acpi/sleep.c index 88561029cca8..1784cb30e7cf 100644 --- a/drivers/acpi/sleep.c +++ b/drivers/acpi/sleep.c @@ -795,6 +795,7 @@ int acpi_pm_device_sleep_state(struct device *dev, int *d_min_p) *d_min_p = d_min; return d_max; } +EXPORT_SYMBOL(acpi_pm_device_sleep_state); #endif /* CONFIG_PM */ #ifdef CONFIG_PM_SLEEP @@ -831,6 +832,7 @@ int acpi_pm_device_run_wake(struct device *phys_dev, bool enable) return 0; } +EXPORT_SYMBOL(acpi_pm_device_run_wake); /** * acpi_pm_device_sleep_wake - enable or disable the system wake-up diff --git a/drivers/ata/libata-acpi.c b/drivers/ata/libata-acpi.c index bb20fd597eb1..e01807a1ef31 100644 --- a/drivers/ata/libata-acpi.c +++ b/drivers/ata/libata-acpi.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include "libata.h" @@ -853,6 +854,7 @@ void ata_acpi_set_state(struct ata_port *ap, pm_message_t state) { struct ata_device *dev; acpi_handle handle; + int acpi_state; /* channel first and then drives for power on and vica versa for power off */ @@ -862,10 +864,23 @@ void ata_acpi_set_state(struct ata_port *ap, pm_message_t state) ata_for_each_dev(dev, &ap->link, ENABLED) { handle = ata_dev_acpi_handle(dev); - if (handle) - acpi_bus_set_power(handle, - state.event == PM_EVENT_ON ? - ACPI_STATE_D0 : ACPI_STATE_D3); + if (!handle) + continue; + + if (state.event != PM_EVENT_ON) { + acpi_state = acpi_pm_device_sleep_state( + &dev->sdev->sdev_gendev, NULL); + if (acpi_state > 0) + acpi_bus_set_power(handle, acpi_state); + /* TBD: need to check if it's runtime pm request */ + acpi_pm_device_run_wake( + &dev->sdev->sdev_gendev, true); + } else { + /* Ditto */ + acpi_pm_device_run_wake( + &dev->sdev->sdev_gendev, false); + acpi_bus_set_power(handle, ACPI_STATE_D0); + } } handle = ata_ap_acpi_handle(ap); @@ -965,6 +980,61 @@ void ata_acpi_on_disable(struct ata_device *dev) ata_acpi_clear_gtf(dev); } +static void ata_acpi_wake_dev(acpi_handle handle, u32 event, void *context) +{ + struct ata_device *ata_dev = context; + + if (event == ACPI_NOTIFY_DEVICE_WAKE && ata_dev && + pm_runtime_suspended(&ata_dev->sdev->sdev_gendev)) + scsi_autopm_get_device(ata_dev->sdev); +} + +static void ata_acpi_add_pm_notifier(struct ata_device *dev) +{ + struct acpi_device *acpi_dev; + acpi_handle handle; + acpi_status status; + + handle = ata_dev_acpi_handle(dev); + if (!handle) + return; + + status = acpi_bus_get_device(handle, &acpi_dev); + if (ACPI_SUCCESS(status)) { + acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY, + ata_acpi_wake_dev, dev); + device_set_run_wake(&dev->sdev->sdev_gendev, true); + } +} + +static void ata_acpi_remove_pm_notifier(struct ata_device *dev) +{ + struct acpi_device *acpi_dev; + acpi_handle handle; + acpi_status status; + + handle = ata_dev_acpi_handle(dev); + if (!handle) + return; + + status = acpi_bus_get_device(handle, &acpi_dev); + if (ACPI_SUCCESS(status)) { + device_set_run_wake(&dev->sdev->sdev_gendev, false); + acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, + ata_acpi_wake_dev); + } +} + +void ata_acpi_bind(struct ata_device *dev) +{ + ata_acpi_add_pm_notifier(dev); +} + +void ata_acpi_unbind(struct ata_device *dev) +{ + ata_acpi_remove_pm_notifier(dev); +} + static int compat_pci_ata(struct ata_port *ap) { struct device *dev = ap->tdev.parent; diff --git a/drivers/ata/libata-scsi.c b/drivers/ata/libata-scsi.c index 22226350cd0c..8ec81ca8f659 100644 --- a/drivers/ata/libata-scsi.c +++ b/drivers/ata/libata-scsi.c @@ -3445,6 +3445,7 @@ void ata_scsi_scan_host(struct ata_port *ap, int sync) if (!IS_ERR(sdev)) { dev->sdev = sdev; scsi_device_put(sdev); + ata_acpi_bind(dev); } else { dev->sdev = NULL; } @@ -3541,6 +3542,8 @@ static void ata_scsi_remove_dev(struct ata_device *dev) mutex_lock(&ap->scsi_host->scan_mutex); spin_lock_irqsave(ap->lock, flags); + ata_acpi_unbind(dev); + /* clearing dev->sdev is protected by host lock */ sdev = dev->sdev; dev->sdev = NULL; diff --git a/drivers/ata/libata.h b/drivers/ata/libata.h index b0d5294982eb..44a7939b5bb6 100644 --- a/drivers/ata/libata.h +++ b/drivers/ata/libata.h @@ -120,6 +120,8 @@ extern void ata_acpi_on_disable(struct ata_device *dev); extern void ata_acpi_set_state(struct ata_port *ap, pm_message_t state); extern int ata_acpi_register(void); extern void ata_acpi_unregister(void); +extern void ata_acpi_bind(struct ata_device *dev); +extern void ata_acpi_unbind(struct ata_device *dev); #else static inline void ata_acpi_dissociate(struct ata_host *host) { } static inline int ata_acpi_on_suspend(struct ata_port *ap) { return 0; } @@ -130,6 +132,8 @@ static inline void ata_acpi_set_state(struct ata_port *ap, pm_message_t state) { } static inline int ata_acpi_register(void) { return 0; } static void ata_acpi_unregister(void) { } +static void ata_acpi_bind(struct ata_device *dev) { } +static void ata_acpi_unbind(struct ata_device *dev) { } #endif /* libata-scsi.c */ -- cgit v1.2.3 From a5ca7345c1e67cd12cf75a9ec034c7d34786ae79 Mon Sep 17 00:00:00 2001 From: "Rafael J. Wysocki" Date: Mon, 23 Jul 2012 21:01:02 +0200 Subject: ACPI / PM: Fix build warning in sleep.c for CONFIG_ACPI_SLEEP unset If CONFIG_ACPI_SLEEP is unset, the compiler complains that pwr_btn_event_pending is defined but not used. To silence the warning, move the definition of pwr_btn_event_pending under an appropriate #ifdef. Reported-by: Paul E. McKenney Signed-off-by: Rafael J. Wysocki Tested-by: Paul E. McKenney Signed-off-by: Len Brown --- drivers/acpi/sleep.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers/acpi/sleep.c') diff --git a/drivers/acpi/sleep.c b/drivers/acpi/sleep.c index 88561029cca8..dca8074b6704 100644 --- a/drivers/acpi/sleep.c +++ b/drivers/acpi/sleep.c @@ -57,7 +57,6 @@ MODULE_PARM_DESC(gts, "Enable evaluation of _GTS on suspend."); MODULE_PARM_DESC(bfs, "Enable evaluation of _BFS on resume".); static u8 sleep_states[ACPI_S_STATE_COUNT]; -static bool pwr_btn_event_pending; static void acpi_sleep_tts_switch(u32 acpi_state) { @@ -110,6 +109,7 @@ static int acpi_sleep_prepare(u32 acpi_state) #ifdef CONFIG_ACPI_SLEEP static u32 acpi_target_sleep_state = ACPI_STATE_S0; +static bool pwr_btn_event_pending; /* * The ACPI specification wants us to save NVS memory regions during hibernation -- cgit v1.2.3