summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2011-05-19 20:45:08 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2011-05-19 20:45:08 -0400
commit78c4def67e8eebe602655a3dec9aa08f0e2f7c4b (patch)
tree8c0c756bbff7325f5c2a773f8cc64d8390ebe5b5
parent7e6628e4bcb3b3546c625ec63ca724f28ab14f0c (diff)
parent942c3c5c329274fa6de5998cb911cf3d0a42d0b1 (diff)
Merge branch 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip
* 'timers-core-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/linux-2.6-tip: hrtimer: Make lookup table const RTC: Disable CONFIG_RTC_CLASS from being built as a module timers: Fix alarmtimer build issues when CONFIG_RTC_CLASS=n timers: Remove delayed irqwork from alarmtimers implementation timers: Improve alarmtimer comments and minor fixes timers: Posix interface for alarm-timers timers: Introduce in-kernel alarm-timer interface timers: Add rb_init_node() to allow for stack allocated rb nodes time: Add timekeeping_inject_sleeptime
-rw-r--r--drivers/rtc/Kconfig7
-rw-r--r--drivers/rtc/class.c23
-rw-r--r--include/linux/alarmtimer.h40
-rw-r--r--include/linux/capability.h7
-rw-r--r--include/linux/posix-timers.h2
-rw-r--r--include/linux/rbtree.h8
-rw-r--r--include/linux/time.h3
-rw-r--r--include/linux/timerqueue.h2
-rw-r--r--kernel/hrtimer.c2
-rw-r--r--kernel/time/Makefile2
-rw-r--r--kernel/time/alarmtimer.c694
-rw-r--r--kernel/time/timekeeping.c56
12 files changed, 820 insertions, 26 deletions
diff --git a/drivers/rtc/Kconfig b/drivers/rtc/Kconfig
index e1878877399c..42891726ea72 100644
--- a/drivers/rtc/Kconfig
+++ b/drivers/rtc/Kconfig
@@ -3,10 +3,10 @@
3# 3#
4 4
5config RTC_LIB 5config RTC_LIB
6 tristate 6 bool
7 7
8menuconfig RTC_CLASS 8menuconfig RTC_CLASS
9 tristate "Real Time Clock" 9 bool "Real Time Clock"
10 default n 10 default n
11 depends on !S390 11 depends on !S390
12 select RTC_LIB 12 select RTC_LIB
@@ -15,9 +15,6 @@ menuconfig RTC_CLASS
15 be allowed to plug one or more RTCs to your system. You will 15 be allowed to plug one or more RTCs to your system. You will
16 probably want to enable one or more of the interfaces below. 16 probably want to enable one or more of the interfaces below.
17 17
18 This driver can also be built as a module. If so, the module
19 will be called rtc-core.
20
21if RTC_CLASS 18if RTC_CLASS
22 19
23config RTC_HCTOSYS 20config RTC_HCTOSYS
diff --git a/drivers/rtc/class.c b/drivers/rtc/class.c
index 39013867cbd6..4194e59e14cd 100644
--- a/drivers/rtc/class.c
+++ b/drivers/rtc/class.c
@@ -41,26 +41,21 @@ static void rtc_device_release(struct device *dev)
41 * system's wall clock; restore it on resume(). 41 * system's wall clock; restore it on resume().
42 */ 42 */
43 43
44static struct timespec delta;
45static time_t oldtime; 44static time_t oldtime;
45static struct timespec oldts;
46 46
47static int rtc_suspend(struct device *dev, pm_message_t mesg) 47static int rtc_suspend(struct device *dev, pm_message_t mesg)
48{ 48{
49 struct rtc_device *rtc = to_rtc_device(dev); 49 struct rtc_device *rtc = to_rtc_device(dev);
50 struct rtc_time tm; 50 struct rtc_time tm;
51 struct timespec ts = current_kernel_time();
52 51
53 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) 52 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
54 return 0; 53 return 0;
55 54
56 rtc_read_time(rtc, &tm); 55 rtc_read_time(rtc, &tm);
56 ktime_get_ts(&oldts);
57 rtc_tm_to_time(&tm, &oldtime); 57 rtc_tm_to_time(&tm, &oldtime);
58 58
59 /* RTC precision is 1 second; adjust delta for avg 1/2 sec err */
60 set_normalized_timespec(&delta,
61 ts.tv_sec - oldtime,
62 ts.tv_nsec - (NSEC_PER_SEC >> 1));
63
64 return 0; 59 return 0;
65} 60}
66 61
@@ -70,10 +65,12 @@ static int rtc_resume(struct device *dev)
70 struct rtc_time tm; 65 struct rtc_time tm;
71 time_t newtime; 66 time_t newtime;
72 struct timespec time; 67 struct timespec time;
68 struct timespec newts;
73 69
74 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0) 70 if (strcmp(dev_name(&rtc->dev), CONFIG_RTC_HCTOSYS_DEVICE) != 0)
75 return 0; 71 return 0;
76 72
73 ktime_get_ts(&newts);
77 rtc_read_time(rtc, &tm); 74 rtc_read_time(rtc, &tm);
78 if (rtc_valid_tm(&tm) != 0) { 75 if (rtc_valid_tm(&tm) != 0) {
79 pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev)); 76 pr_debug("%s: bogus resume time\n", dev_name(&rtc->dev));
@@ -85,15 +82,13 @@ static int rtc_resume(struct device *dev)
85 pr_debug("%s: time travel!\n", dev_name(&rtc->dev)); 82 pr_debug("%s: time travel!\n", dev_name(&rtc->dev));
86 return 0; 83 return 0;
87 } 84 }
85 /* calculate the RTC time delta */
86 set_normalized_timespec(&time, newtime - oldtime, 0);
88 87
89 /* restore wall clock using delta against this RTC; 88 /* subtract kernel time between rtc_suspend to rtc_resume */
90 * adjust again for avg 1/2 second RTC sampling error 89 time = timespec_sub(time, timespec_sub(newts, oldts));
91 */
92 set_normalized_timespec(&time,
93 newtime + delta.tv_sec,
94 (NSEC_PER_SEC >> 1) + delta.tv_nsec);
95 do_settimeofday(&time);
96 90
91 timekeeping_inject_sleeptime(&time);
97 return 0; 92 return 0;
98} 93}
99 94
diff --git a/include/linux/alarmtimer.h b/include/linux/alarmtimer.h
new file mode 100644
index 000000000000..c5d6095b46f8
--- /dev/null
+++ b/include/linux/alarmtimer.h
@@ -0,0 +1,40 @@
1#ifndef _LINUX_ALARMTIMER_H
2#define _LINUX_ALARMTIMER_H
3
4#include <linux/time.h>
5#include <linux/hrtimer.h>
6#include <linux/timerqueue.h>
7#include <linux/rtc.h>
8
9enum alarmtimer_type {
10 ALARM_REALTIME,
11 ALARM_BOOTTIME,
12
13 ALARM_NUMTYPE,
14};
15
16/**
17 * struct alarm - Alarm timer structure
18 * @node: timerqueue node for adding to the event list this value
19 * also includes the expiration time.
20 * @period: Period for recuring alarms
21 * @function: Function pointer to be executed when the timer fires.
22 * @type: Alarm type (BOOTTIME/REALTIME)
23 * @enabled: Flag that represents if the alarm is set to fire or not
24 * @data: Internal data value.
25 */
26struct alarm {
27 struct timerqueue_node node;
28 ktime_t period;
29 void (*function)(struct alarm *);
30 enum alarmtimer_type type;
31 bool enabled;
32 void *data;
33};
34
35void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
36 void (*function)(struct alarm *));
37void alarm_start(struct alarm *alarm, ktime_t start, ktime_t period);
38void alarm_cancel(struct alarm *alarm);
39
40#endif
diff --git a/include/linux/capability.h b/include/linux/capability.h
index d4675af963fa..4554db0cde86 100644
--- a/include/linux/capability.h
+++ b/include/linux/capability.h
@@ -355,7 +355,12 @@ struct cpu_vfs_cap_data {
355 355
356#define CAP_SYSLOG 34 356#define CAP_SYSLOG 34
357 357
358#define CAP_LAST_CAP CAP_SYSLOG 358/* Allow triggering something that will wake the system */
359
360#define CAP_WAKE_ALARM 35
361
362
363#define CAP_LAST_CAP CAP_WAKE_ALARM
359 364
360#define cap_valid(x) ((x) >= 0 && (x) <= CAP_LAST_CAP) 365#define cap_valid(x) ((x) >= 0 && (x) <= CAP_LAST_CAP)
361 366
diff --git a/include/linux/posix-timers.h b/include/linux/posix-timers.h
index d51243ae0726..808227d40a64 100644
--- a/include/linux/posix-timers.h
+++ b/include/linux/posix-timers.h
@@ -5,6 +5,7 @@
5#include <linux/list.h> 5#include <linux/list.h>
6#include <linux/sched.h> 6#include <linux/sched.h>
7#include <linux/timex.h> 7#include <linux/timex.h>
8#include <linux/alarmtimer.h>
8 9
9union cpu_time_count { 10union cpu_time_count {
10 cputime_t cpu; 11 cputime_t cpu;
@@ -80,6 +81,7 @@ struct k_itimer {
80 unsigned long incr; 81 unsigned long incr;
81 unsigned long expires; 82 unsigned long expires;
82 } mmtimer; 83 } mmtimer;
84 struct alarm alarmtimer;
83 } it; 85 } it;
84}; 86};
85 87
diff --git a/include/linux/rbtree.h b/include/linux/rbtree.h
index 7066acb2c530..033b507b33b1 100644
--- a/include/linux/rbtree.h
+++ b/include/linux/rbtree.h
@@ -136,6 +136,14 @@ static inline void rb_set_color(struct rb_node *rb, int color)
136#define RB_EMPTY_NODE(node) (rb_parent(node) == node) 136#define RB_EMPTY_NODE(node) (rb_parent(node) == node)
137#define RB_CLEAR_NODE(node) (rb_set_parent(node, node)) 137#define RB_CLEAR_NODE(node) (rb_set_parent(node, node))
138 138
139static inline void rb_init_node(struct rb_node *rb)
140{
141 rb->rb_parent_color = 0;
142 rb->rb_right = NULL;
143 rb->rb_left = NULL;
144 RB_CLEAR_NODE(rb);
145}
146
139extern void rb_insert_color(struct rb_node *, struct rb_root *); 147extern void rb_insert_color(struct rb_node *, struct rb_root *);
140extern void rb_erase(struct rb_node *, struct rb_root *); 148extern void rb_erase(struct rb_node *, struct rb_root *);
141 149
diff --git a/include/linux/time.h b/include/linux/time.h
index 454a26205787..b3061782dec3 100644
--- a/include/linux/time.h
+++ b/include/linux/time.h
@@ -126,6 +126,7 @@ struct timespec __current_kernel_time(void); /* does not take xtime_lock */
126struct timespec get_monotonic_coarse(void); 126struct timespec get_monotonic_coarse(void);
127void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim, 127void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
128 struct timespec *wtom, struct timespec *sleep); 128 struct timespec *wtom, struct timespec *sleep);
129void timekeeping_inject_sleeptime(struct timespec *delta);
129 130
130#define CURRENT_TIME (current_kernel_time()) 131#define CURRENT_TIME (current_kernel_time())
131#define CURRENT_TIME_SEC ((struct timespec) { get_seconds(), 0 }) 132#define CURRENT_TIME_SEC ((struct timespec) { get_seconds(), 0 })
@@ -294,6 +295,8 @@ struct itimerval {
294#define CLOCK_REALTIME_COARSE 5 295#define CLOCK_REALTIME_COARSE 5
295#define CLOCK_MONOTONIC_COARSE 6 296#define CLOCK_MONOTONIC_COARSE 6
296#define CLOCK_BOOTTIME 7 297#define CLOCK_BOOTTIME 7
298#define CLOCK_REALTIME_ALARM 8
299#define CLOCK_BOOTTIME_ALARM 9
297 300
298/* 301/*
299 * The IDs of various hardware clocks: 302 * The IDs of various hardware clocks:
diff --git a/include/linux/timerqueue.h b/include/linux/timerqueue.h
index a520fd70a59f..5088727478fd 100644
--- a/include/linux/timerqueue.h
+++ b/include/linux/timerqueue.h
@@ -39,7 +39,7 @@ struct timerqueue_node *timerqueue_getnext(struct timerqueue_head *head)
39 39
40static inline void timerqueue_init(struct timerqueue_node *node) 40static inline void timerqueue_init(struct timerqueue_node *node)
41{ 41{
42 RB_CLEAR_NODE(&node->node); 42 rb_init_node(&node->node);
43} 43}
44 44
45static inline void timerqueue_init_head(struct timerqueue_head *head) 45static inline void timerqueue_init_head(struct timerqueue_head *head)
diff --git a/kernel/hrtimer.c b/kernel/hrtimer.c
index 87fdb3f8db14..dbbbf7d43080 100644
--- a/kernel/hrtimer.c
+++ b/kernel/hrtimer.c
@@ -81,7 +81,7 @@ DEFINE_PER_CPU(struct hrtimer_cpu_base, hrtimer_bases) =
81 } 81 }
82}; 82};
83 83
84static int hrtimer_clock_to_base_table[MAX_CLOCKS] = { 84static const int hrtimer_clock_to_base_table[MAX_CLOCKS] = {
85 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME, 85 [CLOCK_REALTIME] = HRTIMER_BASE_REALTIME,
86 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC, 86 [CLOCK_MONOTONIC] = HRTIMER_BASE_MONOTONIC,
87 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME, 87 [CLOCK_BOOTTIME] = HRTIMER_BASE_BOOTTIME,
diff --git a/kernel/time/Makefile b/kernel/time/Makefile
index b0425991e9ac..e2fd74b8e8c2 100644
--- a/kernel/time/Makefile
+++ b/kernel/time/Makefile
@@ -1,5 +1,5 @@
1obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o 1obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o
2obj-y += timeconv.o posix-clock.o 2obj-y += timeconv.o posix-clock.o alarmtimer.o
3 3
4obj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD) += clockevents.o 4obj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD) += clockevents.o
5obj-$(CONFIG_GENERIC_CLOCKEVENTS) += tick-common.o 5obj-$(CONFIG_GENERIC_CLOCKEVENTS) += tick-common.o
diff --git a/kernel/time/alarmtimer.c b/kernel/time/alarmtimer.c
new file mode 100644
index 000000000000..9265014cb4db
--- /dev/null
+++ b/kernel/time/alarmtimer.c
@@ -0,0 +1,694 @@
1/*
2 * Alarmtimer interface
3 *
4 * This interface provides a timer which is similarto hrtimers,
5 * but triggers a RTC alarm if the box is suspend.
6 *
7 * This interface is influenced by the Android RTC Alarm timer
8 * interface.
9 *
10 * Copyright (C) 2010 IBM Corperation
11 *
12 * Author: John Stultz <john.stultz@linaro.org>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
17 */
18#include <linux/time.h>
19#include <linux/hrtimer.h>
20#include <linux/timerqueue.h>
21#include <linux/rtc.h>
22#include <linux/alarmtimer.h>
23#include <linux/mutex.h>
24#include <linux/platform_device.h>
25#include <linux/posix-timers.h>
26#include <linux/workqueue.h>
27#include <linux/freezer.h>
28
29/**
30 * struct alarm_base - Alarm timer bases
31 * @lock: Lock for syncrhonized access to the base
32 * @timerqueue: Timerqueue head managing the list of events
33 * @timer: hrtimer used to schedule events while running
34 * @gettime: Function to read the time correlating to the base
35 * @base_clockid: clockid for the base
36 */
37static struct alarm_base {
38 spinlock_t lock;
39 struct timerqueue_head timerqueue;
40 struct hrtimer timer;
41 ktime_t (*gettime)(void);
42 clockid_t base_clockid;
43} alarm_bases[ALARM_NUMTYPE];
44
45#ifdef CONFIG_RTC_CLASS
46/* rtc timer and device for setting alarm wakeups at suspend */
47static struct rtc_timer rtctimer;
48static struct rtc_device *rtcdev;
49#endif
50
51/* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
52static ktime_t freezer_delta;
53static DEFINE_SPINLOCK(freezer_delta_lock);
54
55
56/**
57 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
58 * @base: pointer to the base where the timer is being run
59 * @alarm: pointer to alarm being enqueued.
60 *
61 * Adds alarm to a alarm_base timerqueue and if necessary sets
62 * an hrtimer to run.
63 *
64 * Must hold base->lock when calling.
65 */
66static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
67{
68 timerqueue_add(&base->timerqueue, &alarm->node);
69 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
70 hrtimer_try_to_cancel(&base->timer);
71 hrtimer_start(&base->timer, alarm->node.expires,
72 HRTIMER_MODE_ABS);
73 }
74}
75
76/**
77 * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
78 * @base: pointer to the base where the timer is running
79 * @alarm: pointer to alarm being removed
80 *
81 * Removes alarm to a alarm_base timerqueue and if necessary sets
82 * a new timer to run.
83 *
84 * Must hold base->lock when calling.
85 */
86static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
87{
88 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
89
90 timerqueue_del(&base->timerqueue, &alarm->node);
91 if (next == &alarm->node) {
92 hrtimer_try_to_cancel(&base->timer);
93 next = timerqueue_getnext(&base->timerqueue);
94 if (!next)
95 return;
96 hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
97 }
98}
99
100
101/**
102 * alarmtimer_fired - Handles alarm hrtimer being fired.
103 * @timer: pointer to hrtimer being run
104 *
105 * When a alarm timer fires, this runs through the timerqueue to
106 * see which alarms expired, and runs those. If there are more alarm
107 * timers queued for the future, we set the hrtimer to fire when
108 * when the next future alarm timer expires.
109 */
110static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
111{
112 struct alarm_base *base = container_of(timer, struct alarm_base, timer);
113 struct timerqueue_node *next;
114 unsigned long flags;
115 ktime_t now;
116 int ret = HRTIMER_NORESTART;
117
118 spin_lock_irqsave(&base->lock, flags);
119 now = base->gettime();
120 while ((next = timerqueue_getnext(&base->timerqueue))) {
121 struct alarm *alarm;
122 ktime_t expired = next->expires;
123
124 if (expired.tv64 >= now.tv64)
125 break;
126
127 alarm = container_of(next, struct alarm, node);
128
129 timerqueue_del(&base->timerqueue, &alarm->node);
130 alarm->enabled = 0;
131 /* Re-add periodic timers */
132 if (alarm->period.tv64) {
133 alarm->node.expires = ktime_add(expired, alarm->period);
134 timerqueue_add(&base->timerqueue, &alarm->node);
135 alarm->enabled = 1;
136 }
137 spin_unlock_irqrestore(&base->lock, flags);
138 if (alarm->function)
139 alarm->function(alarm);
140 spin_lock_irqsave(&base->lock, flags);
141 }
142
143 if (next) {
144 hrtimer_set_expires(&base->timer, next->expires);
145 ret = HRTIMER_RESTART;
146 }
147 spin_unlock_irqrestore(&base->lock, flags);
148
149 return ret;
150
151}
152
153#ifdef CONFIG_RTC_CLASS
154/**
155 * alarmtimer_suspend - Suspend time callback
156 * @dev: unused
157 * @state: unused
158 *
159 * When we are going into suspend, we look through the bases
160 * to see which is the soonest timer to expire. We then
161 * set an rtc timer to fire that far into the future, which
162 * will wake us from suspend.
163 */
164static int alarmtimer_suspend(struct device *dev)
165{
166 struct rtc_time tm;
167 ktime_t min, now;
168 unsigned long flags;
169 int i;
170
171 spin_lock_irqsave(&freezer_delta_lock, flags);
172 min = freezer_delta;
173 freezer_delta = ktime_set(0, 0);
174 spin_unlock_irqrestore(&freezer_delta_lock, flags);
175
176 /* If we have no rtcdev, just return */
177 if (!rtcdev)
178 return 0;
179
180 /* Find the soonest timer to expire*/
181 for (i = 0; i < ALARM_NUMTYPE; i++) {
182 struct alarm_base *base = &alarm_bases[i];
183 struct timerqueue_node *next;
184 ktime_t delta;
185
186 spin_lock_irqsave(&base->lock, flags);
187 next = timerqueue_getnext(&base->timerqueue);
188 spin_unlock_irqrestore(&base->lock, flags);
189 if (!next)
190 continue;
191 delta = ktime_sub(next->expires, base->gettime());
192 if (!min.tv64 || (delta.tv64 < min.tv64))
193 min = delta;
194 }
195 if (min.tv64 == 0)
196 return 0;
197
198 /* XXX - Should we enforce a minimum sleep time? */
199 WARN_ON(min.tv64 < NSEC_PER_SEC);
200
201 /* Setup an rtc timer to fire that far in the future */
202 rtc_timer_cancel(rtcdev, &rtctimer);
203 rtc_read_time(rtcdev, &tm);
204 now = rtc_tm_to_ktime(tm);
205 now = ktime_add(now, min);
206
207 rtc_timer_start(rtcdev, &rtctimer, now, ktime_set(0, 0));
208
209 return 0;
210}
211#else
212static int alarmtimer_suspend(struct device *dev)
213{
214 return 0;
215}
216#endif
217
218static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
219{
220 ktime_t delta;
221 unsigned long flags;
222 struct alarm_base *base = &alarm_bases[type];
223
224 delta = ktime_sub(absexp, base->gettime());
225
226 spin_lock_irqsave(&freezer_delta_lock, flags);
227 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
228 freezer_delta = delta;
229 spin_unlock_irqrestore(&freezer_delta_lock, flags);
230}
231
232
233/**
234 * alarm_init - Initialize an alarm structure
235 * @alarm: ptr to alarm to be initialized
236 * @type: the type of the alarm
237 * @function: callback that is run when the alarm fires
238 */
239void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
240 void (*function)(struct alarm *))
241{
242 timerqueue_init(&alarm->node);
243 alarm->period = ktime_set(0, 0);
244 alarm->function = function;
245 alarm->type = type;
246 alarm->enabled = 0;
247}
248
249/**
250 * alarm_start - Sets an alarm to fire
251 * @alarm: ptr to alarm to set
252 * @start: time to run the alarm
253 * @period: period at which the alarm will recur
254 */
255void alarm_start(struct alarm *alarm, ktime_t start, ktime_t period)
256{
257 struct alarm_base *base = &alarm_bases[alarm->type];
258 unsigned long flags;
259
260 spin_lock_irqsave(&base->lock, flags);
261 if (alarm->enabled)
262 alarmtimer_remove(base, alarm);
263 alarm->node.expires = start;
264 alarm->period = period;
265 alarmtimer_enqueue(base, alarm);
266 alarm->enabled = 1;
267 spin_unlock_irqrestore(&base->lock, flags);
268}
269
270/**
271 * alarm_cancel - Tries to cancel an alarm timer
272 * @alarm: ptr to alarm to be canceled
273 */
274void alarm_cancel(struct alarm *alarm)
275{
276 struct alarm_base *base = &alarm_bases[alarm->type];
277 unsigned long flags;
278
279 spin_lock_irqsave(&base->lock, flags);
280 if (alarm->enabled)
281 alarmtimer_remove(base, alarm);
282 alarm->enabled = 0;
283 spin_unlock_irqrestore(&base->lock, flags);
284}
285
286
287/**
288 * clock2alarm - helper that converts from clockid to alarmtypes
289 * @clockid: clockid.
290 */
291static enum alarmtimer_type clock2alarm(clockid_t clockid)
292{
293 if (clockid == CLOCK_REALTIME_ALARM)
294 return ALARM_REALTIME;
295 if (clockid == CLOCK_BOOTTIME_ALARM)
296 return ALARM_BOOTTIME;
297 return -1;
298}
299
300/**
301 * alarm_handle_timer - Callback for posix timers
302 * @alarm: alarm that fired
303 *
304 * Posix timer callback for expired alarm timers.
305 */
306static void alarm_handle_timer(struct alarm *alarm)
307{
308 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
309 it.alarmtimer);
310 if (posix_timer_event(ptr, 0) != 0)
311 ptr->it_overrun++;
312}
313
314/**
315 * alarm_clock_getres - posix getres interface
316 * @which_clock: clockid
317 * @tp: timespec to fill
318 *
319 * Returns the granularity of underlying alarm base clock
320 */
321static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
322{
323 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
324
325 return hrtimer_get_res(baseid, tp);
326}
327
328/**
329 * alarm_clock_get - posix clock_get interface
330 * @which_clock: clockid
331 * @tp: timespec to fill.
332 *
333 * Provides the underlying alarm base time.
334 */
335static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
336{
337 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
338
339 *tp = ktime_to_timespec(base->gettime());
340 return 0;
341}
342
343/**
344 * alarm_timer_create - posix timer_create interface
345 * @new_timer: k_itimer pointer to manage
346 *
347 * Initializes the k_itimer structure.
348 */
349static int alarm_timer_create(struct k_itimer *new_timer)
350{
351 enum alarmtimer_type type;
352 struct alarm_base *base;
353
354 if (!capable(CAP_WAKE_ALARM))
355 return -EPERM;
356
357 type = clock2alarm(new_timer->it_clock);
358 base = &alarm_bases[type];
359 alarm_init(&new_timer->it.alarmtimer, type, alarm_handle_timer);
360 return 0;
361}
362
363/**
364 * alarm_timer_get - posix timer_get interface
365 * @new_timer: k_itimer pointer
366 * @cur_setting: itimerspec data to fill
367 *
368 * Copies the itimerspec data out from the k_itimer
369 */
370static void alarm_timer_get(struct k_itimer *timr,
371 struct itimerspec *cur_setting)
372{
373 cur_setting->it_interval =
374 ktime_to_timespec(timr->it.alarmtimer.period);
375 cur_setting->it_value =
376 ktime_to_timespec(timr->it.alarmtimer.node.expires);
377 return;
378}
379
380/**
381 * alarm_timer_del - posix timer_del interface
382 * @timr: k_itimer pointer to be deleted
383 *
384 * Cancels any programmed alarms for the given timer.
385 */
386static int alarm_timer_del(struct k_itimer *timr)
387{
388 alarm_cancel(&timr->it.alarmtimer);
389 return 0;
390}
391
392/**
393 * alarm_timer_set - posix timer_set interface
394 * @timr: k_itimer pointer to be deleted
395 * @flags: timer flags
396 * @new_setting: itimerspec to be used
397 * @old_setting: itimerspec being replaced
398 *
399 * Sets the timer to new_setting, and starts the timer.
400 */
401static int alarm_timer_set(struct k_itimer *timr, int flags,
402 struct itimerspec *new_setting,
403 struct itimerspec *old_setting)
404{
405 /* Save old values */
406 old_setting->it_interval =
407 ktime_to_timespec(timr->it.alarmtimer.period);
408 old_setting->it_value =
409 ktime_to_timespec(timr->it.alarmtimer.node.expires);
410
411 /* If the timer was already set, cancel it */
412 alarm_cancel(&timr->it.alarmtimer);
413
414 /* start the timer */
415 alarm_start(&timr->it.alarmtimer,
416 timespec_to_ktime(new_setting->it_value),
417 timespec_to_ktime(new_setting->it_interval));
418 return 0;
419}
420
421/**
422 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
423 * @alarm: ptr to alarm that fired
424 *
425 * Wakes up the task that set the alarmtimer
426 */
427static void alarmtimer_nsleep_wakeup(struct alarm *alarm)
428{
429 struct task_struct *task = (struct task_struct *)alarm->data;
430
431 alarm->data = NULL;
432 if (task)
433 wake_up_process(task);
434}
435
436/**
437 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
438 * @alarm: ptr to alarmtimer
439 * @absexp: absolute expiration time
440 *
441 * Sets the alarm timer and sleeps until it is fired or interrupted.
442 */
443static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
444{
445 alarm->data = (void *)current;
446 do {
447 set_current_state(TASK_INTERRUPTIBLE);
448 alarm_start(alarm, absexp, ktime_set(0, 0));
449 if (likely(alarm->data))
450 schedule();
451
452 alarm_cancel(alarm);
453 } while (alarm->data && !signal_pending(current));
454
455 __set_current_state(TASK_RUNNING);
456
457 return (alarm->data == NULL);
458}
459
460
461/**
462 * update_rmtp - Update remaining timespec value
463 * @exp: expiration time
464 * @type: timer type
465 * @rmtp: user pointer to remaining timepsec value
466 *
467 * Helper function that fills in rmtp value with time between
468 * now and the exp value
469 */
470static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
471 struct timespec __user *rmtp)
472{
473 struct timespec rmt;
474 ktime_t rem;
475
476 rem = ktime_sub(exp, alarm_bases[type].gettime());
477
478 if (rem.tv64 <= 0)
479 return 0;
480 rmt = ktime_to_timespec(rem);
481
482 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
483 return -EFAULT;
484
485 return 1;
486
487}
488
489/**
490 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
491 * @restart: ptr to restart block
492 *
493 * Handles restarted clock_nanosleep calls
494 */
495static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
496{
497 enum alarmtimer_type type = restart->nanosleep.index;
498 ktime_t exp;
499 struct timespec __user *rmtp;
500 struct alarm alarm;
501 int ret = 0;
502
503 exp.tv64 = restart->nanosleep.expires;
504 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
505
506 if (alarmtimer_do_nsleep(&alarm, exp))
507 goto out;
508
509 if (freezing(current))
510 alarmtimer_freezerset(exp, type);
511
512 rmtp = restart->nanosleep.rmtp;
513 if (rmtp) {
514 ret = update_rmtp(exp, type, rmtp);
515 if (ret <= 0)
516 goto out;
517 }
518
519
520 /* The other values in restart are already filled in */
521 ret = -ERESTART_RESTARTBLOCK;
522out:
523 return ret;
524}
525
526/**
527 * alarm_timer_nsleep - alarmtimer nanosleep
528 * @which_clock: clockid
529 * @flags: determins abstime or relative
530 * @tsreq: requested sleep time (abs or rel)
531 * @rmtp: remaining sleep time saved
532 *
533 * Handles clock_nanosleep calls against _ALARM clockids
534 */
535static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
536 struct timespec *tsreq, struct timespec __user *rmtp)
537{
538 enum alarmtimer_type type = clock2alarm(which_clock);
539 struct alarm alarm;
540 ktime_t exp;
541 int ret = 0;
542 struct restart_block *restart;
543
544 if (!capable(CAP_WAKE_ALARM))
545 return -EPERM;
546
547 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
548
549 exp = timespec_to_ktime(*tsreq);
550 /* Convert (if necessary) to absolute time */
551 if (flags != TIMER_ABSTIME) {
552 ktime_t now = alarm_bases[type].gettime();
553 exp = ktime_add(now, exp);
554 }
555
556 if (alarmtimer_do_nsleep(&alarm, exp))
557 goto out;
558
559 if (freezing(current))
560 alarmtimer_freezerset(exp, type);
561
562 /* abs timers don't set remaining time or restart */
563 if (flags == TIMER_ABSTIME) {
564 ret = -ERESTARTNOHAND;
565 goto out;
566 }
567
568 if (rmtp) {
569 ret = update_rmtp(exp, type, rmtp);
570 if (ret <= 0)
571 goto out;
572 }
573
574 restart = &current_thread_info()->restart_block;
575 restart->fn = alarm_timer_nsleep_restart;
576 restart->nanosleep.index = type;
577 restart->nanosleep.expires = exp.tv64;
578 restart->nanosleep.rmtp = rmtp;
579 ret = -ERESTART_RESTARTBLOCK;
580
581out:
582 return ret;
583}
584
585
586/* Suspend hook structures */
587static const struct dev_pm_ops alarmtimer_pm_ops = {
588 .suspend = alarmtimer_suspend,
589};
590
591static struct platform_driver alarmtimer_driver = {
592 .driver = {
593 .name = "alarmtimer",
594 .pm = &alarmtimer_pm_ops,
595 }
596};
597
598/**
599 * alarmtimer_init - Initialize alarm timer code
600 *
601 * This function initializes the alarm bases and registers
602 * the posix clock ids.
603 */
604static int __init alarmtimer_init(void)
605{
606 int error = 0;
607 int i;
608 struct k_clock alarm_clock = {
609 .clock_getres = alarm_clock_getres,
610 .clock_get = alarm_clock_get,
611 .timer_create = alarm_timer_create,
612 .timer_set = alarm_timer_set,
613 .timer_del = alarm_timer_del,
614 .timer_get = alarm_timer_get,
615 .nsleep = alarm_timer_nsleep,
616 };
617
618 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
619 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
620
621 /* Initialize alarm bases */
622 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
623 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
624 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
625 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
626 for (i = 0; i < ALARM_NUMTYPE; i++) {
627 timerqueue_init_head(&alarm_bases[i].timerqueue);
628 spin_lock_init(&alarm_bases[i].lock);
629 hrtimer_init(&alarm_bases[i].timer,
630 alarm_bases[i].base_clockid,
631 HRTIMER_MODE_ABS);
632 alarm_bases[i].timer.function = alarmtimer_fired;
633 }
634 error = platform_driver_register(&alarmtimer_driver);
635 platform_device_register_simple("alarmtimer", -1, NULL, 0);
636
637 return error;
638}
639device_initcall(alarmtimer_init);
640
641#ifdef CONFIG_RTC_CLASS
642/**
643 * has_wakealarm - check rtc device has wakealarm ability
644 * @dev: current device
645 * @name_ptr: name to be returned
646 *
647 * This helper function checks to see if the rtc device can wake
648 * from suspend.
649 */
650static int __init has_wakealarm(struct device *dev, void *name_ptr)
651{
652 struct rtc_device *candidate = to_rtc_device(dev);
653
654 if (!candidate->ops->set_alarm)
655 return 0;
656 if (!device_may_wakeup(candidate->dev.parent))
657 return 0;
658
659 *(const char **)name_ptr = dev_name(dev);
660 return 1;
661}
662
663/**
664 * alarmtimer_init_late - Late initializing of alarmtimer code
665 *
666 * This function locates a rtc device to use for wakealarms.
667 * Run as late_initcall to make sure rtc devices have been
668 * registered.
669 */
670static int __init alarmtimer_init_late(void)
671{
672 char *str;
673
674 /* Find an rtc device and init the rtc_timer */
675 class_find_device(rtc_class, NULL, &str, has_wakealarm);
676 if (str)
677 rtcdev = rtc_class_open(str);
678 if (!rtcdev) {
679 printk(KERN_WARNING "No RTC device found, ALARM timers will"
680 " not wake from suspend");
681 }
682 rtc_timer_init(&rtctimer, NULL, NULL);
683
684 return 0;
685}
686#else
687static int __init alarmtimer_init_late(void)
688{
689 printk(KERN_WARNING "Kernel not built with RTC support, ALARM timers"
690 " will not wake from suspend");
691 return 0;
692}
693#endif
694late_initcall(alarmtimer_init_late);
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 8ad5d576755e..8e6a05a5915a 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -596,6 +596,58 @@ void __init timekeeping_init(void)
596static struct timespec timekeeping_suspend_time; 596static struct timespec timekeeping_suspend_time;
597 597
598/** 598/**
599 * __timekeeping_inject_sleeptime - Internal function to add sleep interval
600 * @delta: pointer to a timespec delta value
601 *
602 * Takes a timespec offset measuring a suspend interval and properly
603 * adds the sleep offset to the timekeeping variables.
604 */
605static void __timekeeping_inject_sleeptime(struct timespec *delta)
606{
607 xtime = timespec_add(xtime, *delta);
608 wall_to_monotonic = timespec_sub(wall_to_monotonic, *delta);
609 total_sleep_time = timespec_add(total_sleep_time, *delta);
610}
611
612
613/**
614 * timekeeping_inject_sleeptime - Adds suspend interval to timeekeeping values
615 * @delta: pointer to a timespec delta value
616 *
617 * This hook is for architectures that cannot support read_persistent_clock
618 * because their RTC/persistent clock is only accessible when irqs are enabled.
619 *
620 * This function should only be called by rtc_resume(), and allows
621 * a suspend offset to be injected into the timekeeping values.
622 */
623void timekeeping_inject_sleeptime(struct timespec *delta)
624{
625 unsigned long flags;
626 struct timespec ts;
627
628 /* Make sure we don't set the clock twice */
629 read_persistent_clock(&ts);
630 if (!(ts.tv_sec == 0 && ts.tv_nsec == 0))
631 return;
632
633 write_seqlock_irqsave(&xtime_lock, flags);
634 timekeeping_forward_now();
635
636 __timekeeping_inject_sleeptime(delta);
637
638 timekeeper.ntp_error = 0;
639 ntp_clear();
640 update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
641 timekeeper.mult);
642
643 write_sequnlock_irqrestore(&xtime_lock, flags);
644
645 /* signal hrtimers about time change */
646 clock_was_set();
647}
648
649
650/**
599 * timekeeping_resume - Resumes the generic timekeeping subsystem. 651 * timekeeping_resume - Resumes the generic timekeeping subsystem.
600 * 652 *
601 * This is for the generic clocksource timekeeping. 653 * This is for the generic clocksource timekeeping.
@@ -615,9 +667,7 @@ static void timekeeping_resume(void)
615 667
616 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) { 668 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
617 ts = timespec_sub(ts, timekeeping_suspend_time); 669 ts = timespec_sub(ts, timekeeping_suspend_time);
618 xtime = timespec_add(xtime, ts); 670 __timekeeping_inject_sleeptime(&ts);
619 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts);
620 total_sleep_time = timespec_add(total_sleep_time, ts);
621 } 671 }
622 /* re-base the last cycle value */ 672 /* re-base the last cycle value */
623 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock); 673 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);