From patchwork Wed Apr 20 08:28:26 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: MyungJoo Ham X-Patchwork-Id: 720991 Received: from smtp1.linux-foundation.org (smtp1.linux-foundation.org [140.211.169.13]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p3K8UQoj030744 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Wed, 20 Apr 2011 08:30:47 GMT Received: from daredevil.linux-foundation.org (localhost [127.0.0.1]) by smtp1.linux-foundation.org (8.14.2/8.13.5/Debian-3ubuntu1.1) with ESMTP id p3K8SZ2h000842; Wed, 20 Apr 2011 01:28:36 -0700 Received: from mailout1.samsung.com (mailout1.samsung.com [203.254.224.24]) by smtp1.linux-foundation.org (8.14.2/8.13.5/Debian-3ubuntu1.1) with ESMTP id p3K8SSJx000833 for ; Wed, 20 Apr 2011 01:28:31 -0700 Received: from epmmp1 (mailout1.samsung.com [203.254.224.24]) by mailout1.samsung.com (Oracle Communications Messaging Exchange Server 7u4-19.01 64bit (built Sep 7 2010)) with ESMTP id <0LJX007Y6ZJEF0B0@mailout1.samsung.com> for linux-pm@lists.linux-foundation.org; Wed, 20 Apr 2011 17:28:26 +0900 (KST) Received: from TNRNDGASPAPP1.tn.corp.samsungelectronics.net ([165.213.149.150]) by mmp1.samsung.com (iPlanet Messaging Server 5.2 Patch 2 (built Jul 14 2004)) with ESMTPA id <0LJX00I66ZJE03@mmp1.samsung.com> for linux-pm@lists.linux-foundation.org; Wed, 20 Apr 2011 17:28:26 +0900 (KST) Received: from Hisui ([165.213.219.116]) by TNRNDGASPAPP1.tn.corp.samsungelectronics.net with Microsoft SMTPSVC(6.0.3790.4675); Wed, 20 Apr 2011 17:28:26 +0900 Date: Wed, 20 Apr 2011 17:28:26 +0900 From: MyungJoo Ham To: linux-pm@lists.linux-foundation.org Message-id: <1303288106-2965-1-git-send-email-myungjoo.ham@samsung.com> X-Mailer: git-send-email 1.7.1 X-OriginalArrivalTime: 20 Apr 2011 08:28:26.0494 (UTC) FILETIME=[EB8511E0:01CBFF34] Received-SPF: pass (localhost is always allowed.) X-Spam-Status: No, hits=-12.211 required=5 tests=AWL, BAYES_00, OSDL_HEADER_SUBJECT_BRACKETED, SAMSUNG_WEBMAIL_OSDL X-Spam-Checker-Version: SpamAssassin 3.2.4-osdl_revision__1.47__ X-MIMEDefang-Filter: lf$Revision: 1.188 $ X-Scanned-By: MIMEDefang 2.63 on 140.211.169.21 Cc: Len Brown , Greg Kroah-Hartman , linux-kernel@vger.kernel.org, kyungmin.park@samsung.com Subject: [linux-pm] [RFC PATCH] PM / Core: suspend_again cb for syscore_ops X-BeenThere: linux-pm@lists.linux-foundation.org X-Mailman-Version: 2.1.9 Precedence: list List-Id: Linux power management List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: linux-pm-bounces@lists.linux-foundation.org Errors-To: linux-pm-bounces@lists.linux-foundation.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Wed, 20 Apr 2011 08:30:47 +0000 (UTC) A system or a device may need to control suspend/wakeup events. It may want to wakeup the system after a predefined amount of time or at a predefined event decided while entering suspend for polling or delayed work. Then, it may want to enter suspend again if its predefined wakeup condition is the only wakeup reason and there is no outstanding events; thus, it does not wakeup the userspace unnecessary and keeps suspended as long as possible (saving the power). Enabling a system to wakeup after a specified time can be easily achieved by using RTC. However, to enter suspend again immediately without invoking userland, we need additional features in the suspend framework. Such need comes from: 1. Monitoring a critical device status without interrupts that can wakeup the system. (in-suspend polling) An example is ambient temperature monitoring that needs to shut down the system or a specific device function if it is too hot or cold. The temperature of a specific device may be needed to be monitored as well; e.g., a charger monitors battery temperature in order to stop charging if overheated. 2. Execute critical "delayed work" at suspend. A driver or a system/board may have a delayed work (or any similar things) that it wants to execute at the requested time. For example, some chargers want to check the battery voltage some time (e.g., 30 seconds) after the battery is fully charged and the charger stops. Then, the charger restarts charging if the voltage has dropped more than a threshold, which is smaller than "restart-charger" voltage, which is a threshold to restart charging regardless of the time passed. This patch allows a system or a device to provide "suspend_again" callback with syscore_ops. With suspend_again callbacks registered, the suspend framework (kernel/power/suspend.c) tries to enter suspend again if conditions are met. The system enters the suspend again if and only if all of the following conditions are met: 1. None of suspend_again ops returned "I want to stop suspend" (suspend_again returns SUSPEND_AGAIN_STOP). 2. At least one of suspend_again ops returned "I want to suspend again" (suspend_again returns SUSPEND_AGAIN_CONTINUE) suspend_again ops may return "I do not care. This wakeup is not related with me." (SUSPEND_AGAIN_NC, which is 0). Use SUSPEND_AGAIN_STOP in order to override other devices' SUSPEND_AGAIN_CONTINUE and to wakeup fully. For devices that poll sensors during suspend may need this if any outstanding status is found. For conventional suspend wakeup sources, SUSPEND_AGAIN_STOP may be used to override SUSPEND_AGAIN devices. Anyway, the following features may need to be added later: 1. An API to allow devices to express next desired wakeup-time. Then, the framework will combine them and setup RTC alarm accordingly and save/restore previously registered RTC alarms. 2. Create a method to declare a specific instance of delayed-work is to be executed in suspend by waking up the system in the middle of suspend. Then, let the framework handle those "critical" delayed-work in suspend. 3. If a device says SUSPEND_AGAIN_CONTINUE and there is another wakeup source pending (e.g., power button) without suspend_again ops, the system will enter suspend again. In such a case, the system should not suspend again. We may need to see if irqs that are enabled by set_irq_wake() (and not related to suspend_ops devices) are pending at syscore_suspend_again(). Maybe we need to add something like "set_irq_wake_with_suspend_again" so that IRQs with suspend_again ops implemented are ignored for the "override-suspend-again-continue" checking. For the initial release, I have set the point of "suspend-again" after suspend_ops->end(). However, I'm not so sure about where to set the suspend-again point. Because in-suspend polling, which may require I/O with other devices, is supposed to be executed at suspend-again ops, the suspend-again point is configured to be as late as possible in suspend_devices_and_enter(). In order to reduce the number of devices waked up, we may need to set the suspend-again point ealier. Signed-off-by: MyungJoo Ham Signed-off-by: Kyungmin Park --- drivers/base/syscore.c | 36 +++++++++++++++++++++++++++ include/linux/syscore_ops.h | 7 +++++ kernel/power/suspend.c | 57 +++++++++++++++++++++++------------------- 3 files changed, 74 insertions(+), 26 deletions(-) diff --git a/drivers/base/syscore.c b/drivers/base/syscore.c index 90af294..1a7e08d 100644 --- a/drivers/base/syscore.c +++ b/drivers/base/syscore.c @@ -95,6 +95,42 @@ void syscore_resume(void) "Interrupts enabled after %pF\n", ops->resume); } } + +/** + * syscore_suspend_again - Execute all the registeres system core suspend_again + * callbacks. If at least one returns + * SUSPEND_AGAIN_CONTINUE and no one returns + * SUSPEND_AGAIN_STOP, syscore_suspend_again let the system + * enter suspend again. + */ +bool syscore_suspend_again(void) +{ + struct syscore_ops *ops; + enum suspend_again_cond condition = SUSPEND_AGAIN_NC; + + list_for_each_entry(ops, &syscore_ops_list, node) + if (ops->suspend_again) { + switch (ops->suspend_again()) { + case SUSPEND_AGAIN_NC: + break; + case SUSPEND_AGAIN_CONTINUE: + if (condition == SUSPEND_AGAIN_NC) + condition = SUSPEND_AGAIN_CONTINUE; + break; + case SUSPEND_AGAIN_STOP: + condition = SUSPEND_AGAIN_STOP; + break; + default: + pr_warn("PM: incorrect return from %pF\n", + ops->suspend_again); + } + } + + if (condition == SUSPEND_AGAIN_CONTINUE) + return true; + + return false; +} #endif /* CONFIG_PM_SLEEP */ /** diff --git a/include/linux/syscore_ops.h b/include/linux/syscore_ops.h index 27b3b0b..bf9bc4e 100644 --- a/include/linux/syscore_ops.h +++ b/include/linux/syscore_ops.h @@ -11,10 +11,16 @@ #include +enum suspend_again_cond { + SUSPEND_AGAIN_NC = 0, /* Do Not Care */ + SUSPEND_AGAIN_CONTINUE, /* Start or keep the again */ + SUSPEND_AGAIN_STOP, /* Stop or do not start. Override CONTINUE */ +}; struct syscore_ops { struct list_head node; int (*suspend)(void); void (*resume)(void); + enum suspend_again_cond (*suspend_again)(void); void (*shutdown)(void); }; @@ -23,6 +29,7 @@ extern void unregister_syscore_ops(struct syscore_ops *ops); #ifdef CONFIG_PM_SLEEP extern int syscore_suspend(void); extern void syscore_resume(void); +extern bool syscore_suspend_again(void); #endif extern void syscore_shutdown(void); diff --git a/kernel/power/suspend.c b/kernel/power/suspend.c index 2814c32..aa6a3d1 100644 --- a/kernel/power/suspend.c +++ b/kernel/power/suspend.c @@ -202,43 +202,48 @@ static int suspend_enter(suspend_state_t state) int suspend_devices_and_enter(suspend_state_t state) { int error; + bool recover = false; if (!suspend_ops) return -ENOSYS; - trace_machine_suspend(state); - if (suspend_ops->begin) { - error = suspend_ops->begin(state); - if (error) - goto Close; - } - suspend_console(); - pm_restrict_gfp_mask(); - suspend_test_start(); - error = dpm_suspend_start(PMSG_SUSPEND); - if (error) { - printk(KERN_ERR "PM: Some devices failed to suspend\n"); - goto Recover_platform; - } - suspend_test_finish("suspend devices"); - if (suspend_test(TEST_DEVICES)) - goto Recover_platform; + do { + trace_machine_suspend(state); + if (suspend_ops->begin) { + error = suspend_ops->begin(state); + if (error) + goto Close; + } + suspend_console(); + pm_restrict_gfp_mask(); + suspend_test_start(); + error = dpm_suspend_start(PMSG_SUSPEND); + if (error) { + printk(KERN_ERR "PM: Some devices failed to suspend\n"); + goto Recover_platform; + } + suspend_test_finish("suspend devices"); + if (suspend_test(TEST_DEVICES)) + goto Recover_platform; - suspend_enter(state); + error = suspend_enter(state); Resume_devices: - suspend_test_start(); - dpm_resume_end(PMSG_RESUME); - suspend_test_finish("resume devices"); - pm_restore_gfp_mask(); - resume_console(); + suspend_test_start(); + dpm_resume_end(PMSG_RESUME); + suspend_test_finish("resume devices"); + pm_restore_gfp_mask(); + resume_console(); Close: - if (suspend_ops->end) - suspend_ops->end(); - trace_machine_suspend(PWR_EVENT_EXIT); + if (suspend_ops->end) + suspend_ops->end(); + trace_machine_suspend(PWR_EVENT_EXIT); + } while (syscore_suspend_again() && !error && !recover); + return error; Recover_platform: + recover = true; if (suspend_ops->recover) suspend_ops->recover(); goto Resume_devices;