From patchwork Tue Mar 11 02:02:02 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ruchi Kandoi X-Patchwork-Id: 3808601 Return-Path: X-Original-To: patchwork-linux-pm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 22990BF540 for ; Tue, 11 Mar 2014 02:25:24 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id EC0CA20218 for ; Tue, 11 Mar 2014 02:25:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id D227F201DC for ; Tue, 11 Mar 2014 02:25:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753653AbaCKCZU (ORCPT ); Mon, 10 Mar 2014 22:25:20 -0400 Received: from mail-pd0-f201.google.com ([209.85.192.201]:60861 "EHLO mail-pd0-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752382AbaCKCZS (ORCPT ); Mon, 10 Mar 2014 22:25:18 -0400 Received: by mail-pd0-f201.google.com with SMTP id x10so1068337pdj.4 for ; Mon, 10 Mar 2014 19:25:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=u244aKv20UIAoV7f5kRZY7MXv2aMhqu0kLyKwzj2W+M=; b=fymhOHMu5/D3fPzr6ufqiYP8MAbWAnIIOvPNUYF8o2xfpoR1bXOMEScfVulJ/qWYMO o6sFJG43s5iTVxz47TDRfZ8ni5zQaYzH8OrXO8Vb9g9Pxr1Wl09G0FEnskEKY3NgoIJh F5LsEIRPMlYhaP53O6zdZ/3YefhNDEpXm+WG3yKScoVerq3A54BVPEccrZFbMJExDcib Dv8+e1uj78FRSqQXN1RQRaDNaGn9+HrkZawnxP+emu3tpUOrx+At1v/jTTLO7Y+VNJnE U9LZw0Sw+qnhlh0fXgH3JlPtfXqbg9fDnYI3knFpbk5IG2hVItT8sjt40yQwfV/wTb1R 0v2Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=u244aKv20UIAoV7f5kRZY7MXv2aMhqu0kLyKwzj2W+M=; b=AYgvP3nRH4u9n8l88fPzDUIdK9Tmb0cZ6jBs894qrhBlb+OmME58XC9RLyPOU3fVVH 8K5gom77ZR9f5a6ntLqqvjjETybWjMWO4yn8YZO4wiHlPhTokoOIqOXk/d5OuAHzEana bvyobq3CgFAGYMkPCtt4O6H0DxNZqarRrzYODVRtlBuDJ1eJKVGrVgWJ376Jww/JBLeY fL8+1xVkG838DCQtUXchXF36PecQDIbXCydXxl5Eby7MyBeZnSdPVUg6yL+QNp+gQwTk 6S1rHPRzc52nA60NlRx3M9IFF0tbn6KN/yiqqN25x0FtyXwaE19nBvmX44saZtAHQtkY kKew== X-Gm-Message-State: ALoCoQnFV8NQZ1M+v4a535364K6s/w+tpZwcnstYFELnUU1h/jF+a9p4ZmxSTQxRQhJR8+FbsRppgKp0Hjl1kAdzLMPhZ7vMHt20iQnxGz4qm6U4YSuycQYnrxBvKvLfK/dj8/2RzE9l3byaC7jTuY6l0o2JGk+osnPEMpKitD50yaKQI3HVcrASXbBrRMOcC+ndfS0p01Rg7+jmiYwwo8dTGU9rQp1/vA== X-Received: by 10.68.197.73 with SMTP id is9mr15623951pbc.0.1394503328283; Mon, 10 Mar 2014 19:02:08 -0700 (PDT) Received: from corp2gmr1-2.hot.corp.google.com (corp2gmr1-2.hot.corp.google.com [172.24.189.93]) by gmr-mx.google.com with ESMTPS id u30si3249522yhk.1.2014.03.10.19.02.08 for (version=TLSv1.1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 10 Mar 2014 19:02:08 -0700 (PDT) Received: from kandoiruchi.mtv.corp.google.com (kandoiruchi.mtv.corp.google.com [172.18.120.123]) by corp2gmr1-2.hot.corp.google.com (Postfix) with ESMTP id 0AA695A4123; Mon, 10 Mar 2014 19:02:08 -0700 (PDT) Received: by kandoiruchi.mtv.corp.google.com (Postfix, from userid 235580) id 93C421201DE; Mon, 10 Mar 2014 19:02:07 -0700 (PDT) From: Ruchi Kandoi To: linux-kernel@vger.kernel.org, linux-pm@vger.kernel.org Cc: rjw@rjwysocki.net, ghackmann@google.com, Ruchi Kandoi Subject: [PATCH] power: add an API to log wakeup reasons Date: Mon, 10 Mar 2014 19:02:02 -0700 Message-Id: <1394503322-25210-1-git-send-email-kandoiruchi@google.com> X-Mailer: git-send-email 1.9.0.279.gdc9e3eb Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,RCVD_IN_DNSWL_HI,T_DKIM_INVALID,T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add API log_wakeup_reason() and expose it to userspace via sysfs path /sys/kernel/wakeup_reasons/last_resume_reason This is useful for power management diagnostic purposes. Signed-off-by: Ruchi Kandoi Signed-off-by: Greg Hackmann --- include/linux/wakeup_reason.h | 23 +++++++ kernel/power/Makefile | 2 +- kernel/power/wakeup_reason.c | 140 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 include/linux/wakeup_reason.h create mode 100644 kernel/power/wakeup_reason.c diff --git a/include/linux/wakeup_reason.h b/include/linux/wakeup_reason.h new file mode 100644 index 0000000..7ce50f0 --- /dev/null +++ b/include/linux/wakeup_reason.h @@ -0,0 +1,23 @@ +/* + * include/linux/wakeup_reason.h + * + * Logs the reason which caused the kernel to resume + * from the suspend mode. + * + * Copyright (C) 2014 Google, Inc. + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef _LINUX_WAKEUP_REASON_H +#define _LINUX_WAKEUP_REASON_H + +void log_wakeup_reason(int irq); + +#endif /* _LINUX_WAKEUP_REASON_H */ diff --git a/kernel/power/Makefile b/kernel/power/Makefile index 29472bf..f98f021 100644 --- a/kernel/power/Makefile +++ b/kernel/power/Makefile @@ -5,7 +5,7 @@ obj-y += qos.o obj-$(CONFIG_PM) += main.o obj-$(CONFIG_VT_CONSOLE_SLEEP) += console.o obj-$(CONFIG_FREEZER) += process.o -obj-$(CONFIG_SUSPEND) += suspend.o +obj-$(CONFIG_SUSPEND) += suspend.o wakeup_reason.o obj-$(CONFIG_PM_TEST_SUSPEND) += suspend_test.o obj-$(CONFIG_HIBERNATION) += hibernate.o snapshot.o swap.o user.o \ block_io.o diff --git a/kernel/power/wakeup_reason.c b/kernel/power/wakeup_reason.c new file mode 100644 index 0000000..188a6bf --- /dev/null +++ b/kernel/power/wakeup_reason.c @@ -0,0 +1,140 @@ +/* + * kernel/power/wakeup_reason.c + * + * Logs the reasons which caused the kernel to resume from + * the suspend mode. + * + * Copyright (C) 2014 Google, Inc. + * This software is licensed under the terms of the GNU General Public + * License version 2, as published by the Free Software Foundation, and + * may be copied, distributed, and modified under those terms. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define MAX_WAKEUP_REASON_IRQS 32 +static int irq_list[MAX_WAKEUP_REASON_IRQS]; +static int irqcount; +static struct kobject *wakeup_reason; +static spinlock_t resume_reason_lock; + +static ssize_t reason_show(struct kobject *kobj, struct kobj_attribute *attr, + char *buf) +{ + int irq_no, buf_offset = 0; + struct irq_desc *desc; + spin_lock(&resume_reason_lock); + for (irq_no = 0; irq_no < irqcount; irq_no++) { + desc = irq_to_desc(irq_list[irq_no]); + if (desc && desc->action && desc->action->name) + buf_offset += sprintf(buf + buf_offset, "%d %s\n", + irq_list[irq_no], desc->action->name); + else + buf_offset += sprintf(buf + buf_offset, "%d\n", + irq_list[irq_no]); + } + spin_unlock(&resume_reason_lock); + return buf_offset; +} + +static struct kobj_attribute resume_reason = __ATTR(last_resume_reason, 0666, + reason_show, NULL); + +static struct attribute *attrs[] = { + &resume_reason.attr, + NULL, +}; +static struct attribute_group attr_group = { + .attrs = attrs, +}; + +/* + * logs all the wake up reasons to the kernel + * stores the irqs to expose them to the userspace via sysfs + */ +void log_wakeup_reason(int irq) +{ + struct irq_desc *desc; + desc = irq_to_desc(irq); + if (desc && desc->action && desc->action->name) + printk(KERN_INFO "Resume caused by IRQ %d, %s\n", irq, + desc->action->name); + else + printk(KERN_INFO "Resume caused by IRQ %d\n", irq); + + spin_lock(&resume_reason_lock); + if (irqcount == MAX_WAKEUP_REASON_IRQS) { + spin_unlock(&resume_reason_lock); + printk(KERN_WARNING "Resume caused by more than %d IRQs\n", + MAX_WAKEUP_REASON_IRQS); + return; + } + + irq_list[irqcount++] = irq; + spin_unlock(&resume_reason_lock); +} + +/* Detects a suspend and clears all the previous wake up reasons*/ +static int wakeup_reason_pm_event(struct notifier_block *notifier, + unsigned long pm_event, void *unused) +{ + switch (pm_event) { + case PM_SUSPEND_PREPARE: + spin_lock(&resume_reason_lock); + irqcount = 0; + spin_unlock(&resume_reason_lock); + break; + default: + break; + } + return NOTIFY_DONE; +} + +static struct notifier_block wakeup_reason_pm_notifier_block = { + .notifier_call = wakeup_reason_pm_event, +}; + +/* Initializes the sysfs parameter + * registers the pm_event notifier + */ +int __init wakeup_reason_init(void) +{ + int retval; + spin_lock_init(&resume_reason_lock); + retval = register_pm_notifier(&wakeup_reason_pm_notifier_block); + if (retval) + printk(KERN_WARNING "[%s] failed to register PM notifier %d\n", + __func__, retval); + + wakeup_reason = kobject_create_and_add("wakeup_reasons", kernel_kobj); + if (!wakeup_reason) { + printk(KERN_WARNING "[%s] failed to create a sysfs kobject\n", + __func__); + return 1; + } + retval = sysfs_create_group(wakeup_reason, &attr_group); + if (retval) { + kobject_put(wakeup_reason); + printk(KERN_WARNING "[%s] failed to create a sysfs group %d\n", + __func__, retval); + } + return 0; +} + +late_initcall(wakeup_reason_init);