2019-04-02 21:32:01 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2006-06-25 05:27:42 +08:00
|
|
|
/*
|
|
|
|
* drivers/base/power/trace.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Linus Torvalds
|
|
|
|
*
|
|
|
|
* Trace facility for suspend/resume problems, when none of the
|
|
|
|
* devices may be working.
|
|
|
|
*/
|
2019-03-05 01:14:38 +08:00
|
|
|
#define pr_fmt(fmt) "PM: " fmt
|
|
|
|
|
2015-03-18 22:54:27 +08:00
|
|
|
#include <linux/pm-trace.h>
|
2011-05-27 19:12:15 +08:00
|
|
|
#include <linux/export.h>
|
2006-06-25 05:27:42 +08:00
|
|
|
#include <linux/rtc.h>
|
2016-11-29 06:35:19 +08:00
|
|
|
#include <linux/suspend.h>
|
2006-06-25 05:27:42 +08:00
|
|
|
|
2016-05-31 02:57:51 +08:00
|
|
|
#include <linux/mc146818rtc.h>
|
2006-06-25 05:27:42 +08:00
|
|
|
|
|
|
|
#include "power.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Horrid, horrid, horrid.
|
|
|
|
*
|
|
|
|
* It turns out that the _only_ piece of hardware that actually
|
|
|
|
* keeps its value across a hard boot (and, more importantly, the
|
|
|
|
* POST init sequence) is literally the realtime clock.
|
|
|
|
*
|
|
|
|
* Never mind that an RTC chip has 114 bytes (and often a whole
|
|
|
|
* other bank of an additional 128 bytes) of nice SRAM that is
|
|
|
|
* _designed_ to keep data - the POST will clear it. So we literally
|
|
|
|
* can just use the few bytes of actual time data, which means that
|
|
|
|
* we're really limited.
|
|
|
|
*
|
|
|
|
* It means, for example, that we can't use the seconds at all
|
|
|
|
* (since the time between the hang and the boot might be more
|
|
|
|
* than a minute), and we'd better not depend on the low bits of
|
|
|
|
* the minutes either.
|
|
|
|
*
|
|
|
|
* There are the wday fields etc, but I wouldn't guarantee those
|
|
|
|
* are dependable either. And if the date isn't valid, either the
|
|
|
|
* hw or POST will do strange things.
|
|
|
|
*
|
|
|
|
* So we're left with:
|
|
|
|
* - year: 0-99
|
|
|
|
* - month: 0-11
|
|
|
|
* - day-of-month: 1-28
|
|
|
|
* - hour: 0-23
|
|
|
|
* - min: (0-30)*2
|
|
|
|
*
|
|
|
|
* Giving us a total range of 0-16128000 (0xf61800), ie less
|
|
|
|
* than 24 bits of actual data we can save across reboots.
|
|
|
|
*
|
|
|
|
* And if your box can't boot in less than three minutes,
|
|
|
|
* you're screwed.
|
|
|
|
*
|
|
|
|
* Now, almost 24 bits of data is pitifully small, so we need
|
|
|
|
* to be pretty dense if we want to use it for anything nice.
|
|
|
|
* What we do is that instead of saving off nice readable info,
|
|
|
|
* we save off _hashes_ of information that we can hopefully
|
|
|
|
* regenerate after the reboot.
|
|
|
|
*
|
|
|
|
* In particular, this means that we might be unlucky, and hit
|
|
|
|
* a case where we have a hash collision, and we end up not
|
|
|
|
* being able to tell for certain exactly which case happened.
|
|
|
|
* But that's hopefully unlikely.
|
|
|
|
*
|
|
|
|
* What we do is to take the bits we can fit, and split them
|
|
|
|
* into three parts (16*997*1009 = 16095568), and use the values
|
|
|
|
* for:
|
|
|
|
* - 0-15: user-settable
|
|
|
|
* - 0-996: file + line number
|
|
|
|
* - 0-1008: device
|
|
|
|
*/
|
|
|
|
#define USERHASH (16)
|
|
|
|
#define FILEHASH (997)
|
|
|
|
#define DEVHASH (1009)
|
|
|
|
|
|
|
|
#define DEVSEED (7919)
|
|
|
|
|
2016-11-29 06:35:19 +08:00
|
|
|
bool pm_trace_rtc_abused __read_mostly;
|
|
|
|
EXPORT_SYMBOL_GPL(pm_trace_rtc_abused);
|
|
|
|
|
2006-06-25 05:27:42 +08:00
|
|
|
static unsigned int dev_hash_value;
|
|
|
|
|
|
|
|
static int set_magic_time(unsigned int user, unsigned int file, unsigned int device)
|
|
|
|
{
|
|
|
|
unsigned int n = user + USERHASH*(file + FILEHASH*device);
|
|
|
|
|
|
|
|
// June 7th, 2006
|
|
|
|
static struct rtc_time time = {
|
|
|
|
.tm_sec = 0,
|
|
|
|
.tm_min = 0,
|
|
|
|
.tm_hour = 0,
|
|
|
|
.tm_mday = 7,
|
|
|
|
.tm_mon = 5, // June - counting from zero
|
|
|
|
.tm_year = 106,
|
|
|
|
.tm_wday = 3,
|
|
|
|
.tm_yday = 160,
|
|
|
|
.tm_isdst = 1
|
|
|
|
};
|
|
|
|
|
|
|
|
time.tm_year = (n % 100);
|
|
|
|
n /= 100;
|
|
|
|
time.tm_mon = (n % 12);
|
|
|
|
n /= 12;
|
|
|
|
time.tm_mday = (n % 28) + 1;
|
|
|
|
n /= 28;
|
|
|
|
time.tm_hour = (n % 24);
|
|
|
|
n /= 24;
|
|
|
|
time.tm_min = (n % 20) * 3;
|
|
|
|
n /= 20;
|
2016-05-31 02:57:51 +08:00
|
|
|
mc146818_set_time(&time);
|
2016-11-29 06:35:19 +08:00
|
|
|
pm_trace_rtc_abused = true;
|
2006-06-25 05:27:42 +08:00
|
|
|
return n ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int read_magic_time(void)
|
|
|
|
{
|
|
|
|
struct rtc_time time;
|
|
|
|
unsigned int val;
|
|
|
|
|
2016-05-31 02:57:51 +08:00
|
|
|
mc146818_get_time(&time);
|
2018-12-05 05:23:30 +08:00
|
|
|
pr_info("RTC time: %ptRt, date: %ptRd\n", &time, &time);
|
2006-06-25 05:27:42 +08:00
|
|
|
val = time.tm_year; /* 100 years */
|
|
|
|
if (val > 100)
|
|
|
|
val -= 100;
|
|
|
|
val += time.tm_mon * 100; /* 12 months */
|
|
|
|
val += (time.tm_mday-1) * 100 * 12; /* 28 month-days */
|
|
|
|
val += time.tm_hour * 100 * 12 * 28; /* 24 hours */
|
|
|
|
val += (time.tm_min / 3) * 100 * 12 * 28 * 24; /* 20 3-minute intervals */
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is just the sdbm hash function with a user-supplied
|
|
|
|
* seed and final size parameter.
|
|
|
|
*/
|
|
|
|
static unsigned int hash_string(unsigned int seed, const char *data, unsigned int mod)
|
|
|
|
{
|
|
|
|
unsigned char c;
|
|
|
|
while ((c = *data++) != 0) {
|
|
|
|
seed = (seed << 16) + (seed << 6) - seed + c;
|
|
|
|
}
|
|
|
|
return seed % mod;
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_trace_device(struct device *dev)
|
|
|
|
{
|
2008-10-30 08:36:48 +08:00
|
|
|
dev_hash_value = hash_string(DEVSEED, dev_name(dev), DEVHASH);
|
2006-06-25 05:27:42 +08:00
|
|
|
}
|
2007-07-21 23:10:41 +08:00
|
|
|
EXPORT_SYMBOL(set_trace_device);
|
2006-06-25 05:27:42 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We could just take the "tracedata" index into the .tracedata
|
|
|
|
* section instead. Generating a hash of the data gives us a
|
|
|
|
* chance to work across kernel versions, and perhaps more
|
|
|
|
* importantly it also gives us valid/invalid check (ie we will
|
|
|
|
* likely not give totally bogus reports - if the hash matches,
|
|
|
|
* it's not any guarantee, but it's a high _likelihood_ that
|
|
|
|
* the match is valid).
|
|
|
|
*/
|
2015-03-18 22:54:27 +08:00
|
|
|
void generate_pm_trace(const void *tracedata, unsigned int user)
|
2006-06-25 05:27:42 +08:00
|
|
|
{
|
|
|
|
unsigned short lineno = *(unsigned short *)tracedata;
|
|
|
|
const char *file = *(const char **)(tracedata + 2);
|
|
|
|
unsigned int user_hash_value, file_hash_value;
|
|
|
|
|
|
|
|
user_hash_value = user % USERHASH;
|
|
|
|
file_hash_value = hash_string(lineno, file, FILEHASH);
|
|
|
|
set_magic_time(user_hash_value, file_hash_value, dev_hash_value);
|
|
|
|
}
|
2015-03-18 22:54:27 +08:00
|
|
|
EXPORT_SYMBOL(generate_pm_trace);
|
2006-06-25 05:27:42 +08:00
|
|
|
|
2016-01-25 10:08:52 +08:00
|
|
|
extern char __tracedata_start[], __tracedata_end[];
|
2006-06-25 05:27:42 +08:00
|
|
|
static int show_file_hash(unsigned int value)
|
|
|
|
{
|
|
|
|
int match;
|
|
|
|
char *tracedata;
|
|
|
|
|
|
|
|
match = 0;
|
2016-01-25 10:08:52 +08:00
|
|
|
for (tracedata = __tracedata_start ; tracedata < __tracedata_end ;
|
2007-07-21 23:10:41 +08:00
|
|
|
tracedata += 2 + sizeof(unsigned long)) {
|
2006-06-25 05:27:42 +08:00
|
|
|
unsigned short lineno = *(unsigned short *)tracedata;
|
|
|
|
const char *file = *(const char **)(tracedata + 2);
|
|
|
|
unsigned int hash = hash_string(lineno, file, FILEHASH);
|
|
|
|
if (hash != value)
|
|
|
|
continue;
|
2011-01-31 18:07:14 +08:00
|
|
|
pr_info(" hash matches %s:%u\n", file, lineno);
|
2006-06-25 05:27:42 +08:00
|
|
|
match++;
|
|
|
|
}
|
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int show_dev_hash(unsigned int value)
|
|
|
|
{
|
|
|
|
int match = 0;
|
2010-10-12 05:59:58 +08:00
|
|
|
struct list_head *entry;
|
2006-06-25 05:27:42 +08:00
|
|
|
|
2010-10-12 05:59:58 +08:00
|
|
|
device_pm_lock();
|
|
|
|
entry = dpm_list.prev;
|
Introduce new top level suspend and hibernation callbacks
Introduce 'struct pm_ops' and 'struct pm_ext_ops' ('ext' meaning
'extended') representing suspend and hibernation operations for bus
types, device classes, device types and device drivers.
Modify the PM core to use 'struct pm_ops' and 'struct pm_ext_ops'
objects, if defined, instead of the ->suspend(), ->resume(),
->suspend_late(), and ->resume_early() callbacks (the old callbacks
will be considered as legacy and gradually phased out).
The main purpose of doing this is to separate suspend (aka S2RAM and
standby) callbacks from hibernation callbacks in such a way that the
new callbacks won't take arguments and the semantics of each of them
will be clearly specified. This has been requested for multiple
times by many people, including Linus himself, and the reason is that
within the current scheme if ->resume() is called, for example, it's
difficult to say why it's been called (ie. is it a resume from RAM or
from hibernation or a suspend/hibernation failure etc.?).
The second purpose is to make the suspend/hibernation callbacks more
flexible so that device drivers can handle more than they can within
the current scheme. For example, some drivers may need to prevent
new children of the device from being registered before their
->suspend() callbacks are executed or they may want to carry out some
operations requiring the availability of some other devices, not
directly bound via the parent-child relationship, in order to prepare
for the execution of ->suspend(), etc.
Ultimately, we'd like to stop using the freezing of tasks for suspend
and therefore the drivers' suspend/hibernation code will have to take
care of the handling of the user space during suspend/hibernation.
That, in turn, would be difficult within the current scheme, without
the new ->prepare() and ->complete() callbacks.
Signed-off-by: Rafael J. Wysocki <rjw@sisk.pl>
Acked-by: Pavel Machek <pavel@ucw.cz>
Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>
2008-05-21 05:00:01 +08:00
|
|
|
while (entry != &dpm_list) {
|
2006-06-25 05:27:42 +08:00
|
|
|
struct device * dev = to_device(entry);
|
2008-10-30 08:36:48 +08:00
|
|
|
unsigned int hash = hash_string(DEVSEED, dev_name(dev), DEVHASH);
|
2006-06-25 05:27:42 +08:00
|
|
|
if (hash == value) {
|
2008-05-02 12:02:41 +08:00
|
|
|
dev_info(dev, "hash matches\n");
|
2006-06-25 05:27:42 +08:00
|
|
|
match++;
|
|
|
|
}
|
|
|
|
entry = entry->prev;
|
|
|
|
}
|
2010-10-12 05:59:58 +08:00
|
|
|
device_pm_unlock();
|
2006-06-25 05:27:42 +08:00
|
|
|
return match;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int hash_value_early_read;
|
|
|
|
|
2010-10-12 06:00:25 +08:00
|
|
|
int show_trace_dev_match(char *buf, size_t size)
|
|
|
|
{
|
|
|
|
unsigned int value = hash_value_early_read / (USERHASH * FILEHASH);
|
|
|
|
int ret = 0;
|
|
|
|
struct list_head *entry;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It's possible that multiple devices will match the hash and we can't
|
|
|
|
* tell which is the culprit, so it's best to output them all.
|
|
|
|
*/
|
|
|
|
device_pm_lock();
|
|
|
|
entry = dpm_list.prev;
|
|
|
|
while (size && entry != &dpm_list) {
|
|
|
|
struct device *dev = to_device(entry);
|
|
|
|
unsigned int hash = hash_string(DEVSEED, dev_name(dev),
|
|
|
|
DEVHASH);
|
|
|
|
if (hash == value) {
|
|
|
|
int len = snprintf(buf, size, "%s\n",
|
|
|
|
dev_driver_string(dev));
|
|
|
|
if (len > size)
|
|
|
|
len = size;
|
|
|
|
buf += len;
|
|
|
|
ret += len;
|
|
|
|
size -= len;
|
|
|
|
}
|
|
|
|
entry = entry->prev;
|
|
|
|
}
|
|
|
|
device_pm_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-29 06:35:19 +08:00
|
|
|
static int
|
|
|
|
pm_trace_notify(struct notifier_block *nb, unsigned long mode, void *_unused)
|
|
|
|
{
|
|
|
|
switch (mode) {
|
|
|
|
case PM_POST_HIBERNATION:
|
|
|
|
case PM_POST_SUSPEND:
|
|
|
|
if (pm_trace_rtc_abused) {
|
|
|
|
pm_trace_rtc_abused = false;
|
|
|
|
pr_warn("Possible incorrect RTC due to pm_trace, please use 'ntpdate' or 'rdate' to reset it.\n");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct notifier_block pm_trace_nb = {
|
|
|
|
.notifier_call = pm_trace_notify,
|
|
|
|
};
|
|
|
|
|
2006-06-25 05:27:42 +08:00
|
|
|
static int early_resume_init(void)
|
|
|
|
{
|
|
|
|
hash_value_early_read = read_magic_time();
|
2016-11-29 06:35:19 +08:00
|
|
|
register_pm_notifier(&pm_trace_nb);
|
2006-06-25 05:27:42 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int late_resume_init(void)
|
|
|
|
{
|
|
|
|
unsigned int val = hash_value_early_read;
|
|
|
|
unsigned int user, file, dev;
|
|
|
|
|
|
|
|
user = val % USERHASH;
|
|
|
|
val = val / USERHASH;
|
|
|
|
file = val % FILEHASH;
|
|
|
|
val = val / FILEHASH;
|
|
|
|
dev = val /* % DEVHASH */;
|
|
|
|
|
2011-01-31 18:07:14 +08:00
|
|
|
pr_info(" Magic number: %d:%d:%d\n", user, file, dev);
|
2006-06-25 05:27:42 +08:00
|
|
|
show_file_hash(file);
|
|
|
|
show_dev_hash(dev);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
core_initcall(early_resume_init);
|
|
|
|
late_initcall(late_resume_init);
|