^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * kernel/power/wakeup_reason.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Logs the reasons which caused the kernel to resume from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * the suspend mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 2020 Google, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This software is licensed under the terms of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * License version 2, as published by the Free Software Foundation, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * may be copied, distributed, and modified under those terms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/wakeup_reason.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/kobject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/sysfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/spinlock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) * struct wakeup_irq_node - stores data and relationships for IRQs logged as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * either base or nested wakeup reasons during suspend/resume flow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * @siblings - for membership on leaf or parent IRQ lists
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * @irq - the IRQ number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * @irq_name - the name associated with the IRQ, or a default if none
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct wakeup_irq_node {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct list_head siblings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) const char *irq_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) enum wakeup_reason_flag {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) RESUME_NONE = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) RESUME_IRQ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) RESUME_ABORT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) RESUME_ABNORMAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static DEFINE_SPINLOCK(wakeup_reason_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static LIST_HEAD(leaf_irqs); /* kept in ascending IRQ sorted order */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) static LIST_HEAD(parent_irqs); /* unordered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) static struct kmem_cache *wakeup_irq_nodes_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) static const char *default_irq_name = "(unnamed)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static struct kobject *kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) static bool capture_reasons;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static int wakeup_reason;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) static char non_irq_wake_reason[MAX_SUSPEND_ABORT_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static ktime_t last_monotime; /* monotonic time before last suspend */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static ktime_t curr_monotime; /* monotonic time after last suspend */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) static ktime_t last_stime; /* monotonic boottime offset before last suspend */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static ktime_t curr_stime; /* monotonic boottime offset after last suspend */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static void init_node(struct wakeup_irq_node *p, int irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct irq_desc *desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) INIT_LIST_HEAD(&p->siblings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) p->irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) desc = irq_to_desc(irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (desc && desc->action && desc->action->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) p->irq_name = desc->action->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) p->irq_name = default_irq_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) static struct wakeup_irq_node *create_node(int irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) struct wakeup_irq_node *result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) result = kmem_cache_alloc(wakeup_irq_nodes_cache, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (unlikely(!result))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) pr_warn("Failed to log wakeup IRQ %d\n", irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) init_node(result, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static void delete_list(struct list_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct wakeup_irq_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) while (!list_empty(head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) n = list_first_entry(head, struct wakeup_irq_node, siblings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) list_del(&n->siblings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) kmem_cache_free(wakeup_irq_nodes_cache, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static bool add_sibling_node_sorted(struct list_head *head, int irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct wakeup_irq_node *n = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct list_head *predecessor = head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (unlikely(WARN_ON(!head)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!list_empty(head))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) list_for_each_entry(n, head, siblings) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (n->irq < irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) predecessor = &n->siblings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) else if (n->irq == irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) n = create_node(irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) list_add(&n->siblings, predecessor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static struct wakeup_irq_node *find_node_in_list(struct list_head *head,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) struct wakeup_irq_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) if (unlikely(WARN_ON(!head)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) list_for_each_entry(n, head, siblings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) if (n->irq == irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) void log_irq_wakeup_reason(int irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) spin_lock_irqsave(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (wakeup_reason == RESUME_ABNORMAL || wakeup_reason == RESUME_ABORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) spin_unlock_irqrestore(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (!capture_reasons) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) spin_unlock_irqrestore(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (find_node_in_list(&parent_irqs, irq) == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) add_sibling_node_sorted(&leaf_irqs, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) wakeup_reason = RESUME_IRQ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) spin_unlock_irqrestore(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) void log_threaded_irq_wakeup_reason(int irq, int parent_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) struct wakeup_irq_node *parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * Intentionally unsynchronized. Calls that come in after we have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * resumed should have a fast exit path since there's no work to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * done, any any coherence issue that could cause a wrong value here is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * both highly improbable - given the set/clear timing - and very low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * impact (parent IRQ gets logged instead of the specific child).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (!capture_reasons)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) spin_lock_irqsave(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (wakeup_reason == RESUME_ABNORMAL || wakeup_reason == RESUME_ABORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) spin_unlock_irqrestore(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (!capture_reasons || (find_node_in_list(&leaf_irqs, irq) != NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) spin_unlock_irqrestore(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) parent = find_node_in_list(&parent_irqs, parent_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (parent != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) add_sibling_node_sorted(&leaf_irqs, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) parent = find_node_in_list(&leaf_irqs, parent_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (parent != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) list_del_init(&parent->siblings);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) list_add_tail(&parent->siblings, &parent_irqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) add_sibling_node_sorted(&leaf_irqs, irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) spin_unlock_irqrestore(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) EXPORT_SYMBOL_GPL(log_threaded_irq_wakeup_reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static void __log_abort_or_abnormal_wake(bool abort, const char *fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) va_list args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) spin_lock_irqsave(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) /* Suspend abort or abnormal wake reason has already been logged. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) if (wakeup_reason != RESUME_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) spin_unlock_irqrestore(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (abort)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) wakeup_reason = RESUME_ABORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) wakeup_reason = RESUME_ABNORMAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) vsnprintf(non_irq_wake_reason, MAX_SUSPEND_ABORT_LEN, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) spin_unlock_irqrestore(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) void log_suspend_abort_reason(const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) va_start(args, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) __log_abort_or_abnormal_wake(true, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) EXPORT_SYMBOL_GPL(log_suspend_abort_reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) void log_abnormal_wakeup_reason(const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) va_start(args, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) __log_abort_or_abnormal_wake(false, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) EXPORT_SYMBOL_GPL(log_abnormal_wakeup_reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) void clear_wakeup_reasons(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) spin_lock_irqsave(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) delete_list(&leaf_irqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) delete_list(&parent_irqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) wakeup_reason = RESUME_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) capture_reasons = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) spin_unlock_irqrestore(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static void print_wakeup_sources(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) struct wakeup_irq_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) spin_lock_irqsave(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) capture_reasons = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) if (wakeup_reason == RESUME_ABORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) pr_info("Abort: %s\n", non_irq_wake_reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) spin_unlock_irqrestore(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (wakeup_reason == RESUME_IRQ && !list_empty(&leaf_irqs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) list_for_each_entry(n, &leaf_irqs, siblings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) pr_info("Resume caused by IRQ %d, %s\n", n->irq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) n->irq_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) else if (wakeup_reason == RESUME_ABNORMAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) pr_info("Resume caused by %s\n", non_irq_wake_reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) pr_info("Resume cause unknown\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) spin_unlock_irqrestore(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) static ssize_t last_resume_reason_show(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) struct kobj_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) ssize_t buf_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct wakeup_irq_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) spin_lock_irqsave(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) if (wakeup_reason == RESUME_ABORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) buf_offset = scnprintf(buf, PAGE_SIZE, "Abort: %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) non_irq_wake_reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) spin_unlock_irqrestore(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) return buf_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (wakeup_reason == RESUME_IRQ && !list_empty(&leaf_irqs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) list_for_each_entry(n, &leaf_irqs, siblings)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) buf_offset += scnprintf(buf + buf_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) PAGE_SIZE - buf_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) "%d %s\n", n->irq, n->irq_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) else if (wakeup_reason == RESUME_ABNORMAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) buf_offset = scnprintf(buf, PAGE_SIZE, "-1 %s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) non_irq_wake_reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) spin_unlock_irqrestore(&wakeup_reason_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) return buf_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) static ssize_t last_suspend_time_show(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) struct kobj_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) struct timespec64 sleep_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) struct timespec64 total_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) struct timespec64 suspend_resume_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * total_time is calculated from monotonic bootoffsets because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * unlike CLOCK_MONOTONIC it include the time spent in suspend state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) total_time = ktime_to_timespec64(ktime_sub(curr_stime, last_stime));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) * suspend_resume_time is calculated as monotonic (CLOCK_MONOTONIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) * time interval before entering suspend and post suspend.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) suspend_resume_time =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) ktime_to_timespec64(ktime_sub(curr_monotime, last_monotime));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /* sleep_time = total_time - suspend_resume_time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) sleep_time = timespec64_sub(total_time, suspend_resume_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /* Export suspend_resume_time and sleep_time in pair here. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return sprintf(buf, "%llu.%09lu %llu.%09lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) (unsigned long long)suspend_resume_time.tv_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) suspend_resume_time.tv_nsec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) (unsigned long long)sleep_time.tv_sec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) sleep_time.tv_nsec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) static struct kobj_attribute resume_reason = __ATTR_RO(last_resume_reason);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) static struct kobj_attribute suspend_time = __ATTR_RO(last_suspend_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) static struct attribute *attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) &resume_reason.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) &suspend_time.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static struct attribute_group attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) .attrs = attrs,
^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) /* Detects a suspend and clears all the previous wake up reasons*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) static int wakeup_reason_pm_event(struct notifier_block *notifier,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) unsigned long pm_event, void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) switch (pm_event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) case PM_SUSPEND_PREPARE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) /* monotonic time since boot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) last_monotime = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) /* monotonic time since boot including the time spent in suspend */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) last_stime = ktime_get_boottime();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) clear_wakeup_reasons();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) case PM_POST_SUSPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) /* monotonic time since boot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) curr_monotime = ktime_get();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) /* monotonic time since boot including the time spent in suspend */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) curr_stime = ktime_get_boottime();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) print_wakeup_sources();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) static struct notifier_block wakeup_reason_pm_notifier_block = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) .notifier_call = wakeup_reason_pm_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) static int __init wakeup_reason_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) if (register_pm_notifier(&wakeup_reason_pm_notifier_block)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) pr_warn("[%s] failed to register PM notifier\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) kobj = kobject_create_and_add("wakeup_reasons", kernel_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (!kobj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) pr_warn("[%s] failed to create a sysfs kobject\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) goto fail_unregister_pm_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) if (sysfs_create_group(kobj, &attr_group)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) pr_warn("[%s] failed to create a sysfs group\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) goto fail_kobject_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) wakeup_irq_nodes_cache =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) kmem_cache_create("wakeup_irq_node_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) sizeof(struct wakeup_irq_node), 0, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (!wakeup_irq_nodes_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) goto fail_remove_group;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) fail_remove_group:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) sysfs_remove_group(kobj, &attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) fail_kobject_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) kobject_put(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) fail_unregister_pm_notifier:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) unregister_pm_notifier(&wakeup_reason_pm_notifier_block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) late_initcall(wakeup_reason_init);