2 * drivers/base/power/trace.c
4 * Copyright (C) 2006 Linus Torvalds
6 * Trace facility for suspend/resume problems, when none of the
7 * devices may be working.
10 #include <linux/pm-trace.h>
11 #include <linux/export.h>
12 #include <linux/rtc.h>
19 * Horrid, horrid, horrid.
21 * It turns out that the _only_ piece of hardware that actually
22 * keeps its value across a hard boot (and, more importantly, the
23 * POST init sequence) is literally the realtime clock.
25 * Never mind that an RTC chip has 114 bytes (and often a whole
26 * other bank of an additional 128 bytes) of nice SRAM that is
27 * _designed_ to keep data - the POST will clear it. So we literally
28 * can just use the few bytes of actual time data, which means that
29 * we're really limited.
31 * It means, for example, that we can't use the seconds at all
32 * (since the time between the hang and the boot might be more
33 * than a minute), and we'd better not depend on the low bits of
36 * There are the wday fields etc, but I wouldn't guarantee those
37 * are dependable either. And if the date isn't valid, either the
38 * hw or POST will do strange things.
43 * - day-of-month: 1-28
47 * Giving us a total range of 0-16128000 (0xf61800), ie less
48 * than 24 bits of actual data we can save across reboots.
50 * And if your box can't boot in less than three minutes,
53 * Now, almost 24 bits of data is pitifully small, so we need
54 * to be pretty dense if we want to use it for anything nice.
55 * What we do is that instead of saving off nice readable info,
56 * we save off _hashes_ of information that we can hopefully
57 * regenerate after the reboot.
59 * In particular, this means that we might be unlucky, and hit
60 * a case where we have a hash collision, and we end up not
61 * being able to tell for certain exactly which case happened.
62 * But that's hopefully unlikely.
64 * What we do is to take the bits we can fit, and split them
65 * into three parts (16*997*1009 = 16095568), and use the values
67 * - 0-15: user-settable
68 * - 0-996: file + line number
72 #define FILEHASH (997)
73 #define DEVHASH (1009)
75 #define DEVSEED (7919)
77 static unsigned int dev_hash_value;
79 static int set_magic_time(unsigned int user, unsigned int file, unsigned int device)
81 unsigned int n = user + USERHASH*(file + FILEHASH*device);
84 static struct rtc_time time = {
89 .tm_mon = 5, // June - counting from zero
96 time.tm_year = (n % 100);
98 time.tm_mon = (n % 12);
100 time.tm_mday = (n % 28) + 1;
102 time.tm_hour = (n % 24);
104 time.tm_min = (n % 20) * 3;
110 static unsigned int read_magic_time(void)
112 struct rtc_time time;
116 pr_info("RTC time: %2d:%02d:%02d, date: %02d/%02d/%02d\n",
117 time.tm_hour, time.tm_min, time.tm_sec,
118 time.tm_mon + 1, time.tm_mday, time.tm_year % 100);
119 val = time.tm_year; /* 100 years */
122 val += time.tm_mon * 100; /* 12 months */
123 val += (time.tm_mday-1) * 100 * 12; /* 28 month-days */
124 val += time.tm_hour * 100 * 12 * 28; /* 24 hours */
125 val += (time.tm_min / 3) * 100 * 12 * 28 * 24; /* 20 3-minute intervals */
130 * This is just the sdbm hash function with a user-supplied
131 * seed and final size parameter.
133 static unsigned int hash_string(unsigned int seed, const char *data, unsigned int mod)
136 while ((c = *data++) != 0) {
137 seed = (seed << 16) + (seed << 6) - seed + c;
142 void set_trace_device(struct device *dev)
144 dev_hash_value = hash_string(DEVSEED, dev_name(dev), DEVHASH);
146 EXPORT_SYMBOL(set_trace_device);
149 * We could just take the "tracedata" index into the .tracedata
150 * section instead. Generating a hash of the data gives us a
151 * chance to work across kernel versions, and perhaps more
152 * importantly it also gives us valid/invalid check (ie we will
153 * likely not give totally bogus reports - if the hash matches,
154 * it's not any guarantee, but it's a high _likelihood_ that
155 * the match is valid).
157 void generate_pm_trace(const void *tracedata, unsigned int user)
159 unsigned short lineno = *(unsigned short *)tracedata;
160 const char *file = *(const char **)(tracedata + 2);
161 unsigned int user_hash_value, file_hash_value;
163 user_hash_value = user % USERHASH;
164 file_hash_value = hash_string(lineno, file, FILEHASH);
165 set_magic_time(user_hash_value, file_hash_value, dev_hash_value);
167 EXPORT_SYMBOL(generate_pm_trace);
169 extern char __tracedata_start, __tracedata_end;
170 static int show_file_hash(unsigned int value)
176 for (tracedata = &__tracedata_start ; tracedata < &__tracedata_end ;
177 tracedata += 2 + sizeof(unsigned long)) {
178 unsigned short lineno = *(unsigned short *)tracedata;
179 const char *file = *(const char **)(tracedata + 2);
180 unsigned int hash = hash_string(lineno, file, FILEHASH);
183 pr_info(" hash matches %s:%u\n", file, lineno);
189 static int show_dev_hash(unsigned int value)
192 struct list_head *entry;
195 entry = dpm_list.prev;
196 while (entry != &dpm_list) {
197 struct device * dev = to_device(entry);
198 unsigned int hash = hash_string(DEVSEED, dev_name(dev), DEVHASH);
200 dev_info(dev, "hash matches\n");
209 static unsigned int hash_value_early_read;
211 int show_trace_dev_match(char *buf, size_t size)
213 unsigned int value = hash_value_early_read / (USERHASH * FILEHASH);
215 struct list_head *entry;
218 * It's possible that multiple devices will match the hash and we can't
219 * tell which is the culprit, so it's best to output them all.
222 entry = dpm_list.prev;
223 while (size && entry != &dpm_list) {
224 struct device *dev = to_device(entry);
225 unsigned int hash = hash_string(DEVSEED, dev_name(dev),
228 int len = snprintf(buf, size, "%s\n",
229 dev_driver_string(dev));
242 static int early_resume_init(void)
244 hash_value_early_read = read_magic_time();
248 static int late_resume_init(void)
250 unsigned int val = hash_value_early_read;
251 unsigned int user, file, dev;
253 user = val % USERHASH;
254 val = val / USERHASH;
255 file = val % FILEHASH;
256 val = val / FILEHASH;
257 dev = val /* % DEVHASH */;
259 pr_info(" Magic number: %d:%d:%d\n", user, file, dev);
260 show_file_hash(file);
265 core_initcall(early_resume_init);
266 late_initcall(late_resume_init);