aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/time
diff options
context:
space:
mode:
authorAndrea Bastoni <bastoni@cs.unc.edu>2011-08-27 09:43:54 -0400
committerAndrea Bastoni <bastoni@cs.unc.edu>2011-08-27 10:06:11 -0400
commit7b1bb388bc879ffcc6c69b567816d5c354afe42b (patch)
tree5a217fdfb0b5e5a327bdcd624506337c1ae1fe32 /kernel/time
parent7d754596756240fa918b94cd0c3011c77a638987 (diff)
parent02f8c6aee8df3cdc935e9bdd4f2d020306035dbe (diff)
Merge 'Linux v3.0' into Litmus
Some notes: * Litmus^RT scheduling class is the topmost scheduling class (above stop_sched_class). * scheduler_ipi() function (e.g., in smp_reschedule_interrupt()) may increase IPI latencies. * Added path into schedule() to quickly re-evaluate scheduling decision without becoming preemptive again. This used to be a standard path before the removal of BKL. Conflicts: Makefile arch/arm/kernel/calls.S arch/arm/kernel/smp.c arch/x86/include/asm/unistd_32.h arch/x86/kernel/smp.c arch/x86/kernel/syscall_table_32.S include/linux/hrtimer.h kernel/printk.c kernel/sched.c kernel/sched_fair.c
Diffstat (limited to 'kernel/time')
-rw-r--r--kernel/time/Makefile3
-rw-r--r--kernel/time/alarmtimer.c720
-rw-r--r--kernel/time/clockevents.c70
-rw-r--r--kernel/time/clocksource.c77
-rw-r--r--kernel/time/jiffies.c22
-rw-r--r--kernel/time/ntp.c454
-rw-r--r--kernel/time/posix-clock.c445
-rw-r--r--kernel/time/tick-broadcast.c39
-rw-r--r--kernel/time/tick-common.c9
-rw-r--r--kernel/time/tick-internal.h12
-rw-r--r--kernel/time/tick-oneshot.c5
-rw-r--r--kernel/time/tick-sched.c8
-rw-r--r--kernel/time/timecompare.c5
-rw-r--r--kernel/time/timekeeping.c297
-rw-r--r--kernel/time/timer_list.c12
-rw-r--r--kernel/time/timer_stats.c2
16 files changed, 2058 insertions, 122 deletions
diff --git a/kernel/time/Makefile b/kernel/time/Makefile
index ee266620b06c..e2fd74b8e8c2 100644
--- a/kernel/time/Makefile
+++ b/kernel/time/Makefile
@@ -1,4 +1,5 @@
1obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o timeconv.o 1obj-y += timekeeping.o ntp.o clocksource.o jiffies.o timer_list.o timecompare.o
2obj-y += timeconv.o posix-clock.o alarmtimer.o
2 3
3obj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD) += clockevents.o 4obj-$(CONFIG_GENERIC_CLOCKEVENTS_BUILD) += clockevents.o
4obj-$(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..59f369f98a04
--- /dev/null
+++ b/kernel/time/alarmtimer.c
@@ -0,0 +1,720 @@
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/* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
46static ktime_t freezer_delta;
47static DEFINE_SPINLOCK(freezer_delta_lock);
48
49#ifdef CONFIG_RTC_CLASS
50/* rtc timer and device for setting alarm wakeups at suspend */
51static struct rtc_timer rtctimer;
52static struct rtc_device *rtcdev;
53static DEFINE_SPINLOCK(rtcdev_lock);
54
55/**
56 * has_wakealarm - check rtc device has wakealarm ability
57 * @dev: current device
58 * @name_ptr: name to be returned
59 *
60 * This helper function checks to see if the rtc device can wake
61 * from suspend.
62 */
63static int has_wakealarm(struct device *dev, void *name_ptr)
64{
65 struct rtc_device *candidate = to_rtc_device(dev);
66
67 if (!candidate->ops->set_alarm)
68 return 0;
69 if (!device_may_wakeup(candidate->dev.parent))
70 return 0;
71
72 *(const char **)name_ptr = dev_name(dev);
73 return 1;
74}
75
76/**
77 * alarmtimer_get_rtcdev - Return selected rtcdevice
78 *
79 * This function returns the rtc device to use for wakealarms.
80 * If one has not already been chosen, it checks to see if a
81 * functional rtc device is available.
82 */
83static struct rtc_device *alarmtimer_get_rtcdev(void)
84{
85 struct device *dev;
86 char *str;
87 unsigned long flags;
88 struct rtc_device *ret;
89
90 spin_lock_irqsave(&rtcdev_lock, flags);
91 if (!rtcdev) {
92 /* Find an rtc device and init the rtc_timer */
93 dev = class_find_device(rtc_class, NULL, &str, has_wakealarm);
94 /* If we have a device then str is valid. See has_wakealarm() */
95 if (dev) {
96 rtcdev = rtc_class_open(str);
97 /*
98 * Drop the reference we got in class_find_device,
99 * rtc_open takes its own.
100 */
101 put_device(dev);
102 rtc_timer_init(&rtctimer, NULL, NULL);
103 }
104 }
105 ret = rtcdev;
106 spin_unlock_irqrestore(&rtcdev_lock, flags);
107
108 return ret;
109}
110#else
111#define alarmtimer_get_rtcdev() (0)
112#define rtcdev (0)
113#endif
114
115
116/**
117 * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
118 * @base: pointer to the base where the timer is being run
119 * @alarm: pointer to alarm being enqueued.
120 *
121 * Adds alarm to a alarm_base timerqueue and if necessary sets
122 * an hrtimer to run.
123 *
124 * Must hold base->lock when calling.
125 */
126static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
127{
128 timerqueue_add(&base->timerqueue, &alarm->node);
129 if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
130 hrtimer_try_to_cancel(&base->timer);
131 hrtimer_start(&base->timer, alarm->node.expires,
132 HRTIMER_MODE_ABS);
133 }
134}
135
136/**
137 * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
138 * @base: pointer to the base where the timer is running
139 * @alarm: pointer to alarm being removed
140 *
141 * Removes alarm to a alarm_base timerqueue and if necessary sets
142 * a new timer to run.
143 *
144 * Must hold base->lock when calling.
145 */
146static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
147{
148 struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
149
150 timerqueue_del(&base->timerqueue, &alarm->node);
151 if (next == &alarm->node) {
152 hrtimer_try_to_cancel(&base->timer);
153 next = timerqueue_getnext(&base->timerqueue);
154 if (!next)
155 return;
156 hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
157 }
158}
159
160
161/**
162 * alarmtimer_fired - Handles alarm hrtimer being fired.
163 * @timer: pointer to hrtimer being run
164 *
165 * When a alarm timer fires, this runs through the timerqueue to
166 * see which alarms expired, and runs those. If there are more alarm
167 * timers queued for the future, we set the hrtimer to fire when
168 * when the next future alarm timer expires.
169 */
170static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
171{
172 struct alarm_base *base = container_of(timer, struct alarm_base, timer);
173 struct timerqueue_node *next;
174 unsigned long flags;
175 ktime_t now;
176 int ret = HRTIMER_NORESTART;
177
178 spin_lock_irqsave(&base->lock, flags);
179 now = base->gettime();
180 while ((next = timerqueue_getnext(&base->timerqueue))) {
181 struct alarm *alarm;
182 ktime_t expired = next->expires;
183
184 if (expired.tv64 >= now.tv64)
185 break;
186
187 alarm = container_of(next, struct alarm, node);
188
189 timerqueue_del(&base->timerqueue, &alarm->node);
190 alarm->enabled = 0;
191 /* Re-add periodic timers */
192 if (alarm->period.tv64) {
193 alarm->node.expires = ktime_add(expired, alarm->period);
194 timerqueue_add(&base->timerqueue, &alarm->node);
195 alarm->enabled = 1;
196 }
197 spin_unlock_irqrestore(&base->lock, flags);
198 if (alarm->function)
199 alarm->function(alarm);
200 spin_lock_irqsave(&base->lock, flags);
201 }
202
203 if (next) {
204 hrtimer_set_expires(&base->timer, next->expires);
205 ret = HRTIMER_RESTART;
206 }
207 spin_unlock_irqrestore(&base->lock, flags);
208
209 return ret;
210
211}
212
213#ifdef CONFIG_RTC_CLASS
214/**
215 * alarmtimer_suspend - Suspend time callback
216 * @dev: unused
217 * @state: unused
218 *
219 * When we are going into suspend, we look through the bases
220 * to see which is the soonest timer to expire. We then
221 * set an rtc timer to fire that far into the future, which
222 * will wake us from suspend.
223 */
224static int alarmtimer_suspend(struct device *dev)
225{
226 struct rtc_time tm;
227 ktime_t min, now;
228 unsigned long flags;
229 struct rtc_device *rtc;
230 int i;
231
232 spin_lock_irqsave(&freezer_delta_lock, flags);
233 min = freezer_delta;
234 freezer_delta = ktime_set(0, 0);
235 spin_unlock_irqrestore(&freezer_delta_lock, flags);
236
237 rtc = rtcdev;
238 /* If we have no rtcdev, just return */
239 if (!rtc)
240 return 0;
241
242 /* Find the soonest timer to expire*/
243 for (i = 0; i < ALARM_NUMTYPE; i++) {
244 struct alarm_base *base = &alarm_bases[i];
245 struct timerqueue_node *next;
246 ktime_t delta;
247
248 spin_lock_irqsave(&base->lock, flags);
249 next = timerqueue_getnext(&base->timerqueue);
250 spin_unlock_irqrestore(&base->lock, flags);
251 if (!next)
252 continue;
253 delta = ktime_sub(next->expires, base->gettime());
254 if (!min.tv64 || (delta.tv64 < min.tv64))
255 min = delta;
256 }
257 if (min.tv64 == 0)
258 return 0;
259
260 /* XXX - Should we enforce a minimum sleep time? */
261 WARN_ON(min.tv64 < NSEC_PER_SEC);
262
263 /* Setup an rtc timer to fire that far in the future */
264 rtc_timer_cancel(rtc, &rtctimer);
265 rtc_read_time(rtc, &tm);
266 now = rtc_tm_to_ktime(tm);
267 now = ktime_add(now, min);
268
269 rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
270
271 return 0;
272}
273#else
274static int alarmtimer_suspend(struct device *dev)
275{
276 return 0;
277}
278#endif
279
280static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
281{
282 ktime_t delta;
283 unsigned long flags;
284 struct alarm_base *base = &alarm_bases[type];
285
286 delta = ktime_sub(absexp, base->gettime());
287
288 spin_lock_irqsave(&freezer_delta_lock, flags);
289 if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
290 freezer_delta = delta;
291 spin_unlock_irqrestore(&freezer_delta_lock, flags);
292}
293
294
295/**
296 * alarm_init - Initialize an alarm structure
297 * @alarm: ptr to alarm to be initialized
298 * @type: the type of the alarm
299 * @function: callback that is run when the alarm fires
300 */
301void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
302 void (*function)(struct alarm *))
303{
304 timerqueue_init(&alarm->node);
305 alarm->period = ktime_set(0, 0);
306 alarm->function = function;
307 alarm->type = type;
308 alarm->enabled = 0;
309}
310
311/**
312 * alarm_start - Sets an alarm to fire
313 * @alarm: ptr to alarm to set
314 * @start: time to run the alarm
315 * @period: period at which the alarm will recur
316 */
317void alarm_start(struct alarm *alarm, ktime_t start, ktime_t period)
318{
319 struct alarm_base *base = &alarm_bases[alarm->type];
320 unsigned long flags;
321
322 spin_lock_irqsave(&base->lock, flags);
323 if (alarm->enabled)
324 alarmtimer_remove(base, alarm);
325 alarm->node.expires = start;
326 alarm->period = period;
327 alarmtimer_enqueue(base, alarm);
328 alarm->enabled = 1;
329 spin_unlock_irqrestore(&base->lock, flags);
330}
331
332/**
333 * alarm_cancel - Tries to cancel an alarm timer
334 * @alarm: ptr to alarm to be canceled
335 */
336void alarm_cancel(struct alarm *alarm)
337{
338 struct alarm_base *base = &alarm_bases[alarm->type];
339 unsigned long flags;
340
341 spin_lock_irqsave(&base->lock, flags);
342 if (alarm->enabled)
343 alarmtimer_remove(base, alarm);
344 alarm->enabled = 0;
345 spin_unlock_irqrestore(&base->lock, flags);
346}
347
348
349/**
350 * clock2alarm - helper that converts from clockid to alarmtypes
351 * @clockid: clockid.
352 */
353static enum alarmtimer_type clock2alarm(clockid_t clockid)
354{
355 if (clockid == CLOCK_REALTIME_ALARM)
356 return ALARM_REALTIME;
357 if (clockid == CLOCK_BOOTTIME_ALARM)
358 return ALARM_BOOTTIME;
359 return -1;
360}
361
362/**
363 * alarm_handle_timer - Callback for posix timers
364 * @alarm: alarm that fired
365 *
366 * Posix timer callback for expired alarm timers.
367 */
368static void alarm_handle_timer(struct alarm *alarm)
369{
370 struct k_itimer *ptr = container_of(alarm, struct k_itimer,
371 it.alarmtimer);
372 if (posix_timer_event(ptr, 0) != 0)
373 ptr->it_overrun++;
374}
375
376/**
377 * alarm_clock_getres - posix getres interface
378 * @which_clock: clockid
379 * @tp: timespec to fill
380 *
381 * Returns the granularity of underlying alarm base clock
382 */
383static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
384{
385 clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
386
387 if (!alarmtimer_get_rtcdev())
388 return -ENOTSUPP;
389
390 return hrtimer_get_res(baseid, tp);
391}
392
393/**
394 * alarm_clock_get - posix clock_get interface
395 * @which_clock: clockid
396 * @tp: timespec to fill.
397 *
398 * Provides the underlying alarm base time.
399 */
400static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
401{
402 struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
403
404 if (!alarmtimer_get_rtcdev())
405 return -ENOTSUPP;
406
407 *tp = ktime_to_timespec(base->gettime());
408 return 0;
409}
410
411/**
412 * alarm_timer_create - posix timer_create interface
413 * @new_timer: k_itimer pointer to manage
414 *
415 * Initializes the k_itimer structure.
416 */
417static int alarm_timer_create(struct k_itimer *new_timer)
418{
419 enum alarmtimer_type type;
420 struct alarm_base *base;
421
422 if (!alarmtimer_get_rtcdev())
423 return -ENOTSUPP;
424
425 if (!capable(CAP_WAKE_ALARM))
426 return -EPERM;
427
428 type = clock2alarm(new_timer->it_clock);
429 base = &alarm_bases[type];
430 alarm_init(&new_timer->it.alarmtimer, type, alarm_handle_timer);
431 return 0;
432}
433
434/**
435 * alarm_timer_get - posix timer_get interface
436 * @new_timer: k_itimer pointer
437 * @cur_setting: itimerspec data to fill
438 *
439 * Copies the itimerspec data out from the k_itimer
440 */
441static void alarm_timer_get(struct k_itimer *timr,
442 struct itimerspec *cur_setting)
443{
444 cur_setting->it_interval =
445 ktime_to_timespec(timr->it.alarmtimer.period);
446 cur_setting->it_value =
447 ktime_to_timespec(timr->it.alarmtimer.node.expires);
448 return;
449}
450
451/**
452 * alarm_timer_del - posix timer_del interface
453 * @timr: k_itimer pointer to be deleted
454 *
455 * Cancels any programmed alarms for the given timer.
456 */
457static int alarm_timer_del(struct k_itimer *timr)
458{
459 if (!rtcdev)
460 return -ENOTSUPP;
461
462 alarm_cancel(&timr->it.alarmtimer);
463 return 0;
464}
465
466/**
467 * alarm_timer_set - posix timer_set interface
468 * @timr: k_itimer pointer to be deleted
469 * @flags: timer flags
470 * @new_setting: itimerspec to be used
471 * @old_setting: itimerspec being replaced
472 *
473 * Sets the timer to new_setting, and starts the timer.
474 */
475static int alarm_timer_set(struct k_itimer *timr, int flags,
476 struct itimerspec *new_setting,
477 struct itimerspec *old_setting)
478{
479 if (!rtcdev)
480 return -ENOTSUPP;
481
482 /* Save old values */
483 old_setting->it_interval =
484 ktime_to_timespec(timr->it.alarmtimer.period);
485 old_setting->it_value =
486 ktime_to_timespec(timr->it.alarmtimer.node.expires);
487
488 /* If the timer was already set, cancel it */
489 alarm_cancel(&timr->it.alarmtimer);
490
491 /* start the timer */
492 alarm_start(&timr->it.alarmtimer,
493 timespec_to_ktime(new_setting->it_value),
494 timespec_to_ktime(new_setting->it_interval));
495 return 0;
496}
497
498/**
499 * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
500 * @alarm: ptr to alarm that fired
501 *
502 * Wakes up the task that set the alarmtimer
503 */
504static void alarmtimer_nsleep_wakeup(struct alarm *alarm)
505{
506 struct task_struct *task = (struct task_struct *)alarm->data;
507
508 alarm->data = NULL;
509 if (task)
510 wake_up_process(task);
511}
512
513/**
514 * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
515 * @alarm: ptr to alarmtimer
516 * @absexp: absolute expiration time
517 *
518 * Sets the alarm timer and sleeps until it is fired or interrupted.
519 */
520static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
521{
522 alarm->data = (void *)current;
523 do {
524 set_current_state(TASK_INTERRUPTIBLE);
525 alarm_start(alarm, absexp, ktime_set(0, 0));
526 if (likely(alarm->data))
527 schedule();
528
529 alarm_cancel(alarm);
530 } while (alarm->data && !signal_pending(current));
531
532 __set_current_state(TASK_RUNNING);
533
534 return (alarm->data == NULL);
535}
536
537
538/**
539 * update_rmtp - Update remaining timespec value
540 * @exp: expiration time
541 * @type: timer type
542 * @rmtp: user pointer to remaining timepsec value
543 *
544 * Helper function that fills in rmtp value with time between
545 * now and the exp value
546 */
547static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
548 struct timespec __user *rmtp)
549{
550 struct timespec rmt;
551 ktime_t rem;
552
553 rem = ktime_sub(exp, alarm_bases[type].gettime());
554
555 if (rem.tv64 <= 0)
556 return 0;
557 rmt = ktime_to_timespec(rem);
558
559 if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
560 return -EFAULT;
561
562 return 1;
563
564}
565
566/**
567 * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
568 * @restart: ptr to restart block
569 *
570 * Handles restarted clock_nanosleep calls
571 */
572static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
573{
574 enum alarmtimer_type type = restart->nanosleep.clockid;
575 ktime_t exp;
576 struct timespec __user *rmtp;
577 struct alarm alarm;
578 int ret = 0;
579
580 exp.tv64 = restart->nanosleep.expires;
581 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
582
583 if (alarmtimer_do_nsleep(&alarm, exp))
584 goto out;
585
586 if (freezing(current))
587 alarmtimer_freezerset(exp, type);
588
589 rmtp = restart->nanosleep.rmtp;
590 if (rmtp) {
591 ret = update_rmtp(exp, type, rmtp);
592 if (ret <= 0)
593 goto out;
594 }
595
596
597 /* The other values in restart are already filled in */
598 ret = -ERESTART_RESTARTBLOCK;
599out:
600 return ret;
601}
602
603/**
604 * alarm_timer_nsleep - alarmtimer nanosleep
605 * @which_clock: clockid
606 * @flags: determins abstime or relative
607 * @tsreq: requested sleep time (abs or rel)
608 * @rmtp: remaining sleep time saved
609 *
610 * Handles clock_nanosleep calls against _ALARM clockids
611 */
612static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
613 struct timespec *tsreq, struct timespec __user *rmtp)
614{
615 enum alarmtimer_type type = clock2alarm(which_clock);
616 struct alarm alarm;
617 ktime_t exp;
618 int ret = 0;
619 struct restart_block *restart;
620
621 if (!alarmtimer_get_rtcdev())
622 return -ENOTSUPP;
623
624 if (!capable(CAP_WAKE_ALARM))
625 return -EPERM;
626
627 alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
628
629 exp = timespec_to_ktime(*tsreq);
630 /* Convert (if necessary) to absolute time */
631 if (flags != TIMER_ABSTIME) {
632 ktime_t now = alarm_bases[type].gettime();
633 exp = ktime_add(now, exp);
634 }
635
636 if (alarmtimer_do_nsleep(&alarm, exp))
637 goto out;
638
639 if (freezing(current))
640 alarmtimer_freezerset(exp, type);
641
642 /* abs timers don't set remaining time or restart */
643 if (flags == TIMER_ABSTIME) {
644 ret = -ERESTARTNOHAND;
645 goto out;
646 }
647
648 if (rmtp) {
649 ret = update_rmtp(exp, type, rmtp);
650 if (ret <= 0)
651 goto out;
652 }
653
654 restart = &current_thread_info()->restart_block;
655 restart->fn = alarm_timer_nsleep_restart;
656 restart->nanosleep.clockid = type;
657 restart->nanosleep.expires = exp.tv64;
658 restart->nanosleep.rmtp = rmtp;
659 ret = -ERESTART_RESTARTBLOCK;
660
661out:
662 return ret;
663}
664
665
666/* Suspend hook structures */
667static const struct dev_pm_ops alarmtimer_pm_ops = {
668 .suspend = alarmtimer_suspend,
669};
670
671static struct platform_driver alarmtimer_driver = {
672 .driver = {
673 .name = "alarmtimer",
674 .pm = &alarmtimer_pm_ops,
675 }
676};
677
678/**
679 * alarmtimer_init - Initialize alarm timer code
680 *
681 * This function initializes the alarm bases and registers
682 * the posix clock ids.
683 */
684static int __init alarmtimer_init(void)
685{
686 int error = 0;
687 int i;
688 struct k_clock alarm_clock = {
689 .clock_getres = alarm_clock_getres,
690 .clock_get = alarm_clock_get,
691 .timer_create = alarm_timer_create,
692 .timer_set = alarm_timer_set,
693 .timer_del = alarm_timer_del,
694 .timer_get = alarm_timer_get,
695 .nsleep = alarm_timer_nsleep,
696 };
697
698 posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
699 posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
700
701 /* Initialize alarm bases */
702 alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
703 alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
704 alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
705 alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
706 for (i = 0; i < ALARM_NUMTYPE; i++) {
707 timerqueue_init_head(&alarm_bases[i].timerqueue);
708 spin_lock_init(&alarm_bases[i].lock);
709 hrtimer_init(&alarm_bases[i].timer,
710 alarm_bases[i].base_clockid,
711 HRTIMER_MODE_ABS);
712 alarm_bases[i].timer.function = alarmtimer_fired;
713 }
714 error = platform_driver_register(&alarmtimer_driver);
715 platform_device_register_simple("alarmtimer", -1, NULL, 0);
716
717 return error;
718}
719device_initcall(alarmtimer_init);
720
diff --git a/kernel/time/clockevents.c b/kernel/time/clockevents.c
index d7395fdfb9f3..e4c699dfa4e8 100644
--- a/kernel/time/clockevents.c
+++ b/kernel/time/clockevents.c
@@ -18,7 +18,6 @@
18#include <linux/notifier.h> 18#include <linux/notifier.h>
19#include <linux/smp.h> 19#include <linux/smp.h>
20#include <linux/sysdev.h> 20#include <linux/sysdev.h>
21#include <linux/tick.h>
22 21
23#include "tick-internal.h" 22#include "tick-internal.h"
24 23
@@ -183,7 +182,10 @@ void clockevents_register_device(struct clock_event_device *dev)
183 unsigned long flags; 182 unsigned long flags;
184 183
185 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED); 184 BUG_ON(dev->mode != CLOCK_EVT_MODE_UNUSED);
186 BUG_ON(!dev->cpumask); 185 if (!dev->cpumask) {
186 WARN_ON(num_possible_cpus() > 1);
187 dev->cpumask = cpumask_of(smp_processor_id());
188 }
187 189
188 raw_spin_lock_irqsave(&clockevents_lock, flags); 190 raw_spin_lock_irqsave(&clockevents_lock, flags);
189 191
@@ -195,6 +197,70 @@ void clockevents_register_device(struct clock_event_device *dev)
195} 197}
196EXPORT_SYMBOL_GPL(clockevents_register_device); 198EXPORT_SYMBOL_GPL(clockevents_register_device);
197 199
200static void clockevents_config(struct clock_event_device *dev,
201 u32 freq)
202{
203 u64 sec;
204
205 if (!(dev->features & CLOCK_EVT_FEAT_ONESHOT))
206 return;
207
208 /*
209 * Calculate the maximum number of seconds we can sleep. Limit
210 * to 10 minutes for hardware which can program more than
211 * 32bit ticks so we still get reasonable conversion values.
212 */
213 sec = dev->max_delta_ticks;
214 do_div(sec, freq);
215 if (!sec)
216 sec = 1;
217 else if (sec > 600 && dev->max_delta_ticks > UINT_MAX)
218 sec = 600;
219
220 clockevents_calc_mult_shift(dev, freq, sec);
221 dev->min_delta_ns = clockevent_delta2ns(dev->min_delta_ticks, dev);
222 dev->max_delta_ns = clockevent_delta2ns(dev->max_delta_ticks, dev);
223}
224
225/**
226 * clockevents_config_and_register - Configure and register a clock event device
227 * @dev: device to register
228 * @freq: The clock frequency
229 * @min_delta: The minimum clock ticks to program in oneshot mode
230 * @max_delta: The maximum clock ticks to program in oneshot mode
231 *
232 * min/max_delta can be 0 for devices which do not support oneshot mode.
233 */
234void clockevents_config_and_register(struct clock_event_device *dev,
235 u32 freq, unsigned long min_delta,
236 unsigned long max_delta)
237{
238 dev->min_delta_ticks = min_delta;
239 dev->max_delta_ticks = max_delta;
240 clockevents_config(dev, freq);
241 clockevents_register_device(dev);
242}
243
244/**
245 * clockevents_update_freq - Update frequency and reprogram a clock event device.
246 * @dev: device to modify
247 * @freq: new device frequency
248 *
249 * Reconfigure and reprogram a clock event device in oneshot
250 * mode. Must be called on the cpu for which the device delivers per
251 * cpu timer events with interrupts disabled! Returns 0 on success,
252 * -ETIME when the event is in the past.
253 */
254int clockevents_update_freq(struct clock_event_device *dev, u32 freq)
255{
256 clockevents_config(dev, freq);
257
258 if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
259 return 0;
260
261 return clockevents_program_event(dev, dev->next_event, ktime_get());
262}
263
198/* 264/*
199 * Noop handler when we shut down an event device 265 * Noop handler when we shut down an event device
200 */ 266 */
diff --git a/kernel/time/clocksource.c b/kernel/time/clocksource.c
index c18d7efa1b4b..e0980f0d9a0a 100644
--- a/kernel/time/clocksource.c
+++ b/kernel/time/clocksource.c
@@ -113,7 +113,7 @@ EXPORT_SYMBOL_GPL(timecounter_cyc2time);
113 * @shift: pointer to shift variable 113 * @shift: pointer to shift variable
114 * @from: frequency to convert from 114 * @from: frequency to convert from
115 * @to: frequency to convert to 115 * @to: frequency to convert to
116 * @minsec: guaranteed runtime conversion range in seconds 116 * @maxsec: guaranteed runtime conversion range in seconds
117 * 117 *
118 * The function evaluates the shift/mult pair for the scaled math 118 * The function evaluates the shift/mult pair for the scaled math
119 * operations of clocksources and clockevents. 119 * operations of clocksources and clockevents.
@@ -122,7 +122,7 @@ EXPORT_SYMBOL_GPL(timecounter_cyc2time);
122 * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock 122 * NSEC_PER_SEC == 1GHz and @from is the counter frequency. For clock
123 * event @to is the counter frequency and @from is NSEC_PER_SEC. 123 * event @to is the counter frequency and @from is NSEC_PER_SEC.
124 * 124 *
125 * The @minsec conversion range argument controls the time frame in 125 * The @maxsec conversion range argument controls the time frame in
126 * seconds which must be covered by the runtime conversion with the 126 * seconds which must be covered by the runtime conversion with the
127 * calculated mult and shift factors. This guarantees that no 64bit 127 * calculated mult and shift factors. This guarantees that no 64bit
128 * overflow happens when the input value of the conversion is 128 * overflow happens when the input value of the conversion is
@@ -131,7 +131,7 @@ EXPORT_SYMBOL_GPL(timecounter_cyc2time);
131 * factors. 131 * factors.
132 */ 132 */
133void 133void
134clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec) 134clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 maxsec)
135{ 135{
136 u64 tmp; 136 u64 tmp;
137 u32 sft, sftacc= 32; 137 u32 sft, sftacc= 32;
@@ -140,7 +140,7 @@ clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec)
140 * Calculate the shift factor which is limiting the conversion 140 * Calculate the shift factor which is limiting the conversion
141 * range: 141 * range:
142 */ 142 */
143 tmp = ((u64)minsec * from) >> 32; 143 tmp = ((u64)maxsec * from) >> 32;
144 while (tmp) { 144 while (tmp) {
145 tmp >>=1; 145 tmp >>=1;
146 sftacc--; 146 sftacc--;
@@ -152,6 +152,7 @@ clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec)
152 */ 152 */
153 for (sft = 32; sft > 0; sft--) { 153 for (sft = 32; sft > 0; sft--) {
154 tmp = (u64) to << sft; 154 tmp = (u64) to << sft;
155 tmp += from / 2;
155 do_div(tmp, from); 156 do_div(tmp, from);
156 if ((tmp >> sftacc) == 0) 157 if ((tmp >> sftacc) == 0)
157 break; 158 break;
@@ -184,7 +185,6 @@ static struct clocksource *watchdog;
184static struct timer_list watchdog_timer; 185static struct timer_list watchdog_timer;
185static DECLARE_WORK(watchdog_work, clocksource_watchdog_work); 186static DECLARE_WORK(watchdog_work, clocksource_watchdog_work);
186static DEFINE_SPINLOCK(watchdog_lock); 187static DEFINE_SPINLOCK(watchdog_lock);
187static cycle_t watchdog_last;
188static int watchdog_running; 188static int watchdog_running;
189 189
190static int clocksource_watchdog_kthread(void *data); 190static int clocksource_watchdog_kthread(void *data);
@@ -253,11 +253,6 @@ static void clocksource_watchdog(unsigned long data)
253 if (!watchdog_running) 253 if (!watchdog_running)
254 goto out; 254 goto out;
255 255
256 wdnow = watchdog->read(watchdog);
257 wd_nsec = clocksource_cyc2ns((wdnow - watchdog_last) & watchdog->mask,
258 watchdog->mult, watchdog->shift);
259 watchdog_last = wdnow;
260
261 list_for_each_entry(cs, &watchdog_list, wd_list) { 256 list_for_each_entry(cs, &watchdog_list, wd_list) {
262 257
263 /* Clocksource already marked unstable? */ 258 /* Clocksource already marked unstable? */
@@ -267,19 +262,28 @@ static void clocksource_watchdog(unsigned long data)
267 continue; 262 continue;
268 } 263 }
269 264
265 local_irq_disable();
270 csnow = cs->read(cs); 266 csnow = cs->read(cs);
267 wdnow = watchdog->read(watchdog);
268 local_irq_enable();
271 269
272 /* Clocksource initialized ? */ 270 /* Clocksource initialized ? */
273 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) { 271 if (!(cs->flags & CLOCK_SOURCE_WATCHDOG)) {
274 cs->flags |= CLOCK_SOURCE_WATCHDOG; 272 cs->flags |= CLOCK_SOURCE_WATCHDOG;
275 cs->wd_last = csnow; 273 cs->wd_last = wdnow;
274 cs->cs_last = csnow;
276 continue; 275 continue;
277 } 276 }
278 277
279 /* Check the deviation from the watchdog clocksource. */ 278 wd_nsec = clocksource_cyc2ns((wdnow - cs->wd_last) & watchdog->mask,
280 cs_nsec = clocksource_cyc2ns((csnow - cs->wd_last) & 279 watchdog->mult, watchdog->shift);
280
281 cs_nsec = clocksource_cyc2ns((csnow - cs->cs_last) &
281 cs->mask, cs->mult, cs->shift); 282 cs->mask, cs->mult, cs->shift);
282 cs->wd_last = csnow; 283 cs->cs_last = csnow;
284 cs->wd_last = wdnow;
285
286 /* Check the deviation from the watchdog clocksource. */
283 if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) { 287 if (abs(cs_nsec - wd_nsec) > WATCHDOG_THRESHOLD) {
284 clocksource_unstable(cs, cs_nsec - wd_nsec); 288 clocksource_unstable(cs, cs_nsec - wd_nsec);
285 continue; 289 continue;
@@ -317,7 +321,6 @@ static inline void clocksource_start_watchdog(void)
317 return; 321 return;
318 init_timer(&watchdog_timer); 322 init_timer(&watchdog_timer);
319 watchdog_timer.function = clocksource_watchdog; 323 watchdog_timer.function = clocksource_watchdog;
320 watchdog_last = watchdog->read(watchdog);
321 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL; 324 watchdog_timer.expires = jiffies + WATCHDOG_INTERVAL;
322 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask)); 325 add_timer_on(&watchdog_timer, cpumask_first(cpu_online_mask));
323 watchdog_running = 1; 326 watchdog_running = 1;
@@ -625,19 +628,6 @@ static void clocksource_enqueue(struct clocksource *cs)
625 list_add(&cs->list, entry); 628 list_add(&cs->list, entry);
626} 629}
627 630
628
629/*
630 * Maximum time we expect to go between ticks. This includes idle
631 * tickless time. It provides the trade off between selecting a
632 * mult/shift pair that is very precise but can only handle a short
633 * period of time, vs. a mult/shift pair that can handle long periods
634 * of time but isn't as precise.
635 *
636 * This is a subsystem constant, and actual hardware limitations
637 * may override it (ie: clocksources that wrap every 3 seconds).
638 */
639#define MAX_UPDATE_LENGTH 5 /* Seconds */
640
641/** 631/**
642 * __clocksource_updatefreq_scale - Used update clocksource with new freq 632 * __clocksource_updatefreq_scale - Used update clocksource with new freq
643 * @t: clocksource to be registered 633 * @t: clocksource to be registered
@@ -651,15 +641,28 @@ static void clocksource_enqueue(struct clocksource *cs)
651 */ 641 */
652void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq) 642void __clocksource_updatefreq_scale(struct clocksource *cs, u32 scale, u32 freq)
653{ 643{
644 u64 sec;
645
654 /* 646 /*
655 * Ideally we want to use some of the limits used in 647 * Calc the maximum number of seconds which we can run before
656 * clocksource_max_deferment, to provide a more informed 648 * wrapping around. For clocksources which have a mask > 32bit
657 * MAX_UPDATE_LENGTH. But for now this just gets the 649 * we need to limit the max sleep time to have a good
658 * register interface working properly. 650 * conversion precision. 10 minutes is still a reasonable
651 * amount. That results in a shift value of 24 for a
652 * clocksource with mask >= 40bit and f >= 4GHz. That maps to
653 * ~ 0.06ppm granularity for NTP. We apply the same 12.5%
654 * margin as we do in clocksource_max_deferment()
659 */ 655 */
656 sec = (cs->mask - (cs->mask >> 5));
657 do_div(sec, freq);
658 do_div(sec, scale);
659 if (!sec)
660 sec = 1;
661 else if (sec > 600 && cs->mask > UINT_MAX)
662 sec = 600;
663
660 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq, 664 clocks_calc_mult_shift(&cs->mult, &cs->shift, freq,
661 NSEC_PER_SEC/scale, 665 NSEC_PER_SEC / scale, sec * scale);
662 MAX_UPDATE_LENGTH*scale);
663 cs->max_idle_ns = clocksource_max_deferment(cs); 666 cs->max_idle_ns = clocksource_max_deferment(cs);
664} 667}
665EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale); 668EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
@@ -678,14 +681,14 @@ EXPORT_SYMBOL_GPL(__clocksource_updatefreq_scale);
678int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq) 681int __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq)
679{ 682{
680 683
681 /* Intialize mult/shift and max_idle_ns */ 684 /* Initialize mult/shift and max_idle_ns */
682 __clocksource_updatefreq_scale(cs, scale, freq); 685 __clocksource_updatefreq_scale(cs, scale, freq);
683 686
684 /* Add clocksource to the clcoksource list */ 687 /* Add clocksource to the clcoksource list */
685 mutex_lock(&clocksource_mutex); 688 mutex_lock(&clocksource_mutex);
686 clocksource_enqueue(cs); 689 clocksource_enqueue(cs);
687 clocksource_select();
688 clocksource_enqueue_watchdog(cs); 690 clocksource_enqueue_watchdog(cs);
691 clocksource_select();
689 mutex_unlock(&clocksource_mutex); 692 mutex_unlock(&clocksource_mutex);
690 return 0; 693 return 0;
691} 694}
@@ -705,8 +708,8 @@ int clocksource_register(struct clocksource *cs)
705 708
706 mutex_lock(&clocksource_mutex); 709 mutex_lock(&clocksource_mutex);
707 clocksource_enqueue(cs); 710 clocksource_enqueue(cs);
708 clocksource_select();
709 clocksource_enqueue_watchdog(cs); 711 clocksource_enqueue_watchdog(cs);
712 clocksource_select();
710 mutex_unlock(&clocksource_mutex); 713 mutex_unlock(&clocksource_mutex);
711 return 0; 714 return 0;
712} 715}
diff --git a/kernel/time/jiffies.c b/kernel/time/jiffies.c
index 5404a8456909..a470154e0408 100644
--- a/kernel/time/jiffies.c
+++ b/kernel/time/jiffies.c
@@ -22,8 +22,11 @@
22************************************************************************/ 22************************************************************************/
23#include <linux/clocksource.h> 23#include <linux/clocksource.h>
24#include <linux/jiffies.h> 24#include <linux/jiffies.h>
25#include <linux/module.h>
25#include <linux/init.h> 26#include <linux/init.h>
26 27
28#include "tick-internal.h"
29
27/* The Jiffies based clocksource is the lowest common 30/* The Jiffies based clocksource is the lowest common
28 * denominator clock source which should function on 31 * denominator clock source which should function on
29 * all systems. It has the same coarse resolution as 32 * all systems. It has the same coarse resolution as
@@ -31,7 +34,7 @@
31 * inaccuracies caused by missed or lost timer 34 * inaccuracies caused by missed or lost timer
32 * interrupts and the inability for the timer 35 * interrupts and the inability for the timer
33 * interrupt hardware to accuratly tick at the 36 * interrupt hardware to accuratly tick at the
34 * requested HZ value. It is also not reccomended 37 * requested HZ value. It is also not recommended
35 * for "tick-less" systems. 38 * for "tick-less" systems.
36 */ 39 */
37#define NSEC_PER_JIFFY ((u32)((((u64)NSEC_PER_SEC)<<8)/ACTHZ)) 40#define NSEC_PER_JIFFY ((u32)((((u64)NSEC_PER_SEC)<<8)/ACTHZ))
@@ -64,6 +67,23 @@ struct clocksource clocksource_jiffies = {
64 .shift = JIFFIES_SHIFT, 67 .shift = JIFFIES_SHIFT,
65}; 68};
66 69
70#if (BITS_PER_LONG < 64)
71u64 get_jiffies_64(void)
72{
73 unsigned long seq;
74 u64 ret;
75
76 do {
77 seq = read_seqbegin(&xtime_lock);
78 ret = jiffies_64;
79 } while (read_seqretry(&xtime_lock, seq));
80 return ret;
81}
82EXPORT_SYMBOL(get_jiffies_64);
83#endif
84
85EXPORT_SYMBOL(jiffies);
86
67static int __init init_jiffies_clocksource(void) 87static int __init init_jiffies_clocksource(void)
68{ 88{
69 return clocksource_register(&clocksource_jiffies); 89 return clocksource_register(&clocksource_jiffies);
diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c
index c63116863a80..f6117a4c7cb8 100644
--- a/kernel/time/ntp.c
+++ b/kernel/time/ntp.c
@@ -14,6 +14,9 @@
14#include <linux/timex.h> 14#include <linux/timex.h>
15#include <linux/time.h> 15#include <linux/time.h>
16#include <linux/mm.h> 16#include <linux/mm.h>
17#include <linux/module.h>
18
19#include "tick-internal.h"
17 20
18/* 21/*
19 * NTP timekeeping variables: 22 * NTP timekeeping variables:
@@ -74,6 +77,162 @@ static long time_adjust;
74/* constant (boot-param configurable) NTP tick adjustment (upscaled) */ 77/* constant (boot-param configurable) NTP tick adjustment (upscaled) */
75static s64 ntp_tick_adj; 78static s64 ntp_tick_adj;
76 79
80#ifdef CONFIG_NTP_PPS
81
82/*
83 * The following variables are used when a pulse-per-second (PPS) signal
84 * is available. They establish the engineering parameters of the clock
85 * discipline loop when controlled by the PPS signal.
86 */
87#define PPS_VALID 10 /* PPS signal watchdog max (s) */
88#define PPS_POPCORN 4 /* popcorn spike threshold (shift) */
89#define PPS_INTMIN 2 /* min freq interval (s) (shift) */
90#define PPS_INTMAX 8 /* max freq interval (s) (shift) */
91#define PPS_INTCOUNT 4 /* number of consecutive good intervals to
92 increase pps_shift or consecutive bad
93 intervals to decrease it */
94#define PPS_MAXWANDER 100000 /* max PPS freq wander (ns/s) */
95
96static int pps_valid; /* signal watchdog counter */
97static long pps_tf[3]; /* phase median filter */
98static long pps_jitter; /* current jitter (ns) */
99static struct timespec pps_fbase; /* beginning of the last freq interval */
100static int pps_shift; /* current interval duration (s) (shift) */
101static int pps_intcnt; /* interval counter */
102static s64 pps_freq; /* frequency offset (scaled ns/s) */
103static long pps_stabil; /* current stability (scaled ns/s) */
104
105/*
106 * PPS signal quality monitors
107 */
108static long pps_calcnt; /* calibration intervals */
109static long pps_jitcnt; /* jitter limit exceeded */
110static long pps_stbcnt; /* stability limit exceeded */
111static long pps_errcnt; /* calibration errors */
112
113
114/* PPS kernel consumer compensates the whole phase error immediately.
115 * Otherwise, reduce the offset by a fixed factor times the time constant.
116 */
117static inline s64 ntp_offset_chunk(s64 offset)
118{
119 if (time_status & STA_PPSTIME && time_status & STA_PPSSIGNAL)
120 return offset;
121 else
122 return shift_right(offset, SHIFT_PLL + time_constant);
123}
124
125static inline void pps_reset_freq_interval(void)
126{
127 /* the PPS calibration interval may end
128 surprisingly early */
129 pps_shift = PPS_INTMIN;
130 pps_intcnt = 0;
131}
132
133/**
134 * pps_clear - Clears the PPS state variables
135 *
136 * Must be called while holding a write on the xtime_lock
137 */
138static inline void pps_clear(void)
139{
140 pps_reset_freq_interval();
141 pps_tf[0] = 0;
142 pps_tf[1] = 0;
143 pps_tf[2] = 0;
144 pps_fbase.tv_sec = pps_fbase.tv_nsec = 0;
145 pps_freq = 0;
146}
147
148/* Decrease pps_valid to indicate that another second has passed since
149 * the last PPS signal. When it reaches 0, indicate that PPS signal is
150 * missing.
151 *
152 * Must be called while holding a write on the xtime_lock
153 */
154static inline void pps_dec_valid(void)
155{
156 if (pps_valid > 0)
157 pps_valid--;
158 else {
159 time_status &= ~(STA_PPSSIGNAL | STA_PPSJITTER |
160 STA_PPSWANDER | STA_PPSERROR);
161 pps_clear();
162 }
163}
164
165static inline void pps_set_freq(s64 freq)
166{
167 pps_freq = freq;
168}
169
170static inline int is_error_status(int status)
171{
172 return (time_status & (STA_UNSYNC|STA_CLOCKERR))
173 /* PPS signal lost when either PPS time or
174 * PPS frequency synchronization requested
175 */
176 || ((time_status & (STA_PPSFREQ|STA_PPSTIME))
177 && !(time_status & STA_PPSSIGNAL))
178 /* PPS jitter exceeded when
179 * PPS time synchronization requested */
180 || ((time_status & (STA_PPSTIME|STA_PPSJITTER))
181 == (STA_PPSTIME|STA_PPSJITTER))
182 /* PPS wander exceeded or calibration error when
183 * PPS frequency synchronization requested
184 */
185 || ((time_status & STA_PPSFREQ)
186 && (time_status & (STA_PPSWANDER|STA_PPSERROR)));
187}
188
189static inline void pps_fill_timex(struct timex *txc)
190{
191 txc->ppsfreq = shift_right((pps_freq >> PPM_SCALE_INV_SHIFT) *
192 PPM_SCALE_INV, NTP_SCALE_SHIFT);
193 txc->jitter = pps_jitter;
194 if (!(time_status & STA_NANO))
195 txc->jitter /= NSEC_PER_USEC;
196 txc->shift = pps_shift;
197 txc->stabil = pps_stabil;
198 txc->jitcnt = pps_jitcnt;
199 txc->calcnt = pps_calcnt;
200 txc->errcnt = pps_errcnt;
201 txc->stbcnt = pps_stbcnt;
202}
203
204#else /* !CONFIG_NTP_PPS */
205
206static inline s64 ntp_offset_chunk(s64 offset)
207{
208 return shift_right(offset, SHIFT_PLL + time_constant);
209}
210
211static inline void pps_reset_freq_interval(void) {}
212static inline void pps_clear(void) {}
213static inline void pps_dec_valid(void) {}
214static inline void pps_set_freq(s64 freq) {}
215
216static inline int is_error_status(int status)
217{
218 return status & (STA_UNSYNC|STA_CLOCKERR);
219}
220
221static inline void pps_fill_timex(struct timex *txc)
222{
223 /* PPS is not implemented, so these are zero */
224 txc->ppsfreq = 0;
225 txc->jitter = 0;
226 txc->shift = 0;
227 txc->stabil = 0;
228 txc->jitcnt = 0;
229 txc->calcnt = 0;
230 txc->errcnt = 0;
231 txc->stbcnt = 0;
232}
233
234#endif /* CONFIG_NTP_PPS */
235
77/* 236/*
78 * NTP methods: 237 * NTP methods:
79 */ 238 */
@@ -149,10 +308,18 @@ static void ntp_update_offset(long offset)
149 time_reftime = get_seconds(); 308 time_reftime = get_seconds();
150 309
151 offset64 = offset; 310 offset64 = offset;
152 freq_adj = (offset64 * secs) << 311 freq_adj = ntp_update_offset_fll(offset64, secs);
153 (NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant));
154 312
155 freq_adj += ntp_update_offset_fll(offset64, secs); 313 /*
314 * Clamp update interval to reduce PLL gain with low
315 * sampling rate (e.g. intermittent network connection)
316 * to avoid instability.
317 */
318 if (unlikely(secs > 1 << (SHIFT_PLL + 1 + time_constant)))
319 secs = 1 << (SHIFT_PLL + 1 + time_constant);
320
321 freq_adj += (offset64 * secs) <<
322 (NTP_SCALE_SHIFT - 2 * (SHIFT_PLL + 2 + time_constant));
156 323
157 freq_adj = min(freq_adj + time_freq, MAXFREQ_SCALED); 324 freq_adj = min(freq_adj + time_freq, MAXFREQ_SCALED);
158 325
@@ -177,6 +344,9 @@ void ntp_clear(void)
177 344
178 tick_length = tick_length_base; 345 tick_length = tick_length_base;
179 time_offset = 0; 346 time_offset = 0;
347
348 /* Clear PPS state variables */
349 pps_clear();
180} 350}
181 351
182/* 352/*
@@ -242,16 +412,16 @@ void second_overflow(void)
242 time_status |= STA_UNSYNC; 412 time_status |= STA_UNSYNC;
243 } 413 }
244 414
245 /* 415 /* Compute the phase adjustment for the next second */
246 * Compute the phase adjustment for the next second. The offset is
247 * reduced by a fixed factor times the time constant.
248 */
249 tick_length = tick_length_base; 416 tick_length = tick_length_base;
250 417
251 delta = shift_right(time_offset, SHIFT_PLL + time_constant); 418 delta = ntp_offset_chunk(time_offset);
252 time_offset -= delta; 419 time_offset -= delta;
253 tick_length += delta; 420 tick_length += delta;
254 421
422 /* Check PPS signal */
423 pps_dec_valid();
424
255 if (!time_adjust) 425 if (!time_adjust)
256 return; 426 return;
257 427
@@ -361,6 +531,8 @@ static inline void process_adj_status(struct timex *txc, struct timespec *ts)
361 if ((time_status & STA_PLL) && !(txc->status & STA_PLL)) { 531 if ((time_status & STA_PLL) && !(txc->status & STA_PLL)) {
362 time_state = TIME_OK; 532 time_state = TIME_OK;
363 time_status = STA_UNSYNC; 533 time_status = STA_UNSYNC;
534 /* restart PPS frequency calibration */
535 pps_reset_freq_interval();
364 } 536 }
365 537
366 /* 538 /*
@@ -410,6 +582,8 @@ static inline void process_adjtimex_modes(struct timex *txc, struct timespec *ts
410 time_freq = txc->freq * PPM_SCALE; 582 time_freq = txc->freq * PPM_SCALE;
411 time_freq = min(time_freq, MAXFREQ_SCALED); 583 time_freq = min(time_freq, MAXFREQ_SCALED);
412 time_freq = max(time_freq, -MAXFREQ_SCALED); 584 time_freq = max(time_freq, -MAXFREQ_SCALED);
585 /* update pps_freq */
586 pps_set_freq(time_freq);
413 } 587 }
414 588
415 if (txc->modes & ADJ_MAXERROR) 589 if (txc->modes & ADJ_MAXERROR)
@@ -474,6 +648,19 @@ int do_adjtimex(struct timex *txc)
474 hrtimer_cancel(&leap_timer); 648 hrtimer_cancel(&leap_timer);
475 } 649 }
476 650
651 if (txc->modes & ADJ_SETOFFSET) {
652 struct timespec delta;
653 delta.tv_sec = txc->time.tv_sec;
654 delta.tv_nsec = txc->time.tv_usec;
655 if (!capable(CAP_SYS_TIME))
656 return -EPERM;
657 if (!(txc->modes & ADJ_NANO))
658 delta.tv_nsec *= 1000;
659 result = timekeeping_inject_offset(&delta);
660 if (result)
661 return result;
662 }
663
477 getnstimeofday(&ts); 664 getnstimeofday(&ts);
478 665
479 write_seqlock_irq(&xtime_lock); 666 write_seqlock_irq(&xtime_lock);
@@ -500,7 +687,8 @@ int do_adjtimex(struct timex *txc)
500 } 687 }
501 688
502 result = time_state; /* mostly `TIME_OK' */ 689 result = time_state; /* mostly `TIME_OK' */
503 if (time_status & (STA_UNSYNC|STA_CLOCKERR)) 690 /* check for errors */
691 if (is_error_status(time_status))
504 result = TIME_ERROR; 692 result = TIME_ERROR;
505 693
506 txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) * 694 txc->freq = shift_right((time_freq >> PPM_SCALE_INV_SHIFT) *
@@ -514,15 +702,8 @@ int do_adjtimex(struct timex *txc)
514 txc->tick = tick_usec; 702 txc->tick = tick_usec;
515 txc->tai = time_tai; 703 txc->tai = time_tai;
516 704
517 /* PPS is not implemented, so these are zero */ 705 /* fill PPS status fields */
518 txc->ppsfreq = 0; 706 pps_fill_timex(txc);
519 txc->jitter = 0;
520 txc->shift = 0;
521 txc->stabil = 0;
522 txc->jitcnt = 0;
523 txc->calcnt = 0;
524 txc->errcnt = 0;
525 txc->stbcnt = 0;
526 707
527 write_sequnlock_irq(&xtime_lock); 708 write_sequnlock_irq(&xtime_lock);
528 709
@@ -536,6 +717,243 @@ int do_adjtimex(struct timex *txc)
536 return result; 717 return result;
537} 718}
538 719
720#ifdef CONFIG_NTP_PPS
721
722/* actually struct pps_normtime is good old struct timespec, but it is
723 * semantically different (and it is the reason why it was invented):
724 * pps_normtime.nsec has a range of ( -NSEC_PER_SEC / 2, NSEC_PER_SEC / 2 ]
725 * while timespec.tv_nsec has a range of [0, NSEC_PER_SEC) */
726struct pps_normtime {
727 __kernel_time_t sec; /* seconds */
728 long nsec; /* nanoseconds */
729};
730
731/* normalize the timestamp so that nsec is in the
732 ( -NSEC_PER_SEC / 2, NSEC_PER_SEC / 2 ] interval */
733static inline struct pps_normtime pps_normalize_ts(struct timespec ts)
734{
735 struct pps_normtime norm = {
736 .sec = ts.tv_sec,
737 .nsec = ts.tv_nsec
738 };
739
740 if (norm.nsec > (NSEC_PER_SEC >> 1)) {
741 norm.nsec -= NSEC_PER_SEC;
742 norm.sec++;
743 }
744
745 return norm;
746}
747
748/* get current phase correction and jitter */
749static inline long pps_phase_filter_get(long *jitter)
750{
751 *jitter = pps_tf[0] - pps_tf[1];
752 if (*jitter < 0)
753 *jitter = -*jitter;
754
755 /* TODO: test various filters */
756 return pps_tf[0];
757}
758
759/* add the sample to the phase filter */
760static inline void pps_phase_filter_add(long err)
761{
762 pps_tf[2] = pps_tf[1];
763 pps_tf[1] = pps_tf[0];
764 pps_tf[0] = err;
765}
766
767/* decrease frequency calibration interval length.
768 * It is halved after four consecutive unstable intervals.
769 */
770static inline void pps_dec_freq_interval(void)
771{
772 if (--pps_intcnt <= -PPS_INTCOUNT) {
773 pps_intcnt = -PPS_INTCOUNT;
774 if (pps_shift > PPS_INTMIN) {
775 pps_shift--;
776 pps_intcnt = 0;
777 }
778 }
779}
780
781/* increase frequency calibration interval length.
782 * It is doubled after four consecutive stable intervals.
783 */
784static inline void pps_inc_freq_interval(void)
785{
786 if (++pps_intcnt >= PPS_INTCOUNT) {
787 pps_intcnt = PPS_INTCOUNT;
788 if (pps_shift < PPS_INTMAX) {
789 pps_shift++;
790 pps_intcnt = 0;
791 }
792 }
793}
794
795/* update clock frequency based on MONOTONIC_RAW clock PPS signal
796 * timestamps
797 *
798 * At the end of the calibration interval the difference between the
799 * first and last MONOTONIC_RAW clock timestamps divided by the length
800 * of the interval becomes the frequency update. If the interval was
801 * too long, the data are discarded.
802 * Returns the difference between old and new frequency values.
803 */
804static long hardpps_update_freq(struct pps_normtime freq_norm)
805{
806 long delta, delta_mod;
807 s64 ftemp;
808
809 /* check if the frequency interval was too long */
810 if (freq_norm.sec > (2 << pps_shift)) {
811 time_status |= STA_PPSERROR;
812 pps_errcnt++;
813 pps_dec_freq_interval();
814 pr_err("hardpps: PPSERROR: interval too long - %ld s\n",
815 freq_norm.sec);
816 return 0;
817 }
818
819 /* here the raw frequency offset and wander (stability) is
820 * calculated. If the wander is less than the wander threshold
821 * the interval is increased; otherwise it is decreased.
822 */
823 ftemp = div_s64(((s64)(-freq_norm.nsec)) << NTP_SCALE_SHIFT,
824 freq_norm.sec);
825 delta = shift_right(ftemp - pps_freq, NTP_SCALE_SHIFT);
826 pps_freq = ftemp;
827 if (delta > PPS_MAXWANDER || delta < -PPS_MAXWANDER) {
828 pr_warning("hardpps: PPSWANDER: change=%ld\n", delta);
829 time_status |= STA_PPSWANDER;
830 pps_stbcnt++;
831 pps_dec_freq_interval();
832 } else { /* good sample */
833 pps_inc_freq_interval();
834 }
835
836 /* the stability metric is calculated as the average of recent
837 * frequency changes, but is used only for performance
838 * monitoring
839 */
840 delta_mod = delta;
841 if (delta_mod < 0)
842 delta_mod = -delta_mod;
843 pps_stabil += (div_s64(((s64)delta_mod) <<
844 (NTP_SCALE_SHIFT - SHIFT_USEC),
845 NSEC_PER_USEC) - pps_stabil) >> PPS_INTMIN;
846
847 /* if enabled, the system clock frequency is updated */
848 if ((time_status & STA_PPSFREQ) != 0 &&
849 (time_status & STA_FREQHOLD) == 0) {
850 time_freq = pps_freq;
851 ntp_update_frequency();
852 }
853
854 return delta;
855}
856
857/* correct REALTIME clock phase error against PPS signal */
858static void hardpps_update_phase(long error)
859{
860 long correction = -error;
861 long jitter;
862
863 /* add the sample to the median filter */
864 pps_phase_filter_add(correction);
865 correction = pps_phase_filter_get(&jitter);
866
867 /* Nominal jitter is due to PPS signal noise. If it exceeds the
868 * threshold, the sample is discarded; otherwise, if so enabled,
869 * the time offset is updated.
870 */
871 if (jitter > (pps_jitter << PPS_POPCORN)) {
872 pr_warning("hardpps: PPSJITTER: jitter=%ld, limit=%ld\n",
873 jitter, (pps_jitter << PPS_POPCORN));
874 time_status |= STA_PPSJITTER;
875 pps_jitcnt++;
876 } else if (time_status & STA_PPSTIME) {
877 /* correct the time using the phase offset */
878 time_offset = div_s64(((s64)correction) << NTP_SCALE_SHIFT,
879 NTP_INTERVAL_FREQ);
880 /* cancel running adjtime() */
881 time_adjust = 0;
882 }
883 /* update jitter */
884 pps_jitter += (jitter - pps_jitter) >> PPS_INTMIN;
885}
886
887/*
888 * hardpps() - discipline CPU clock oscillator to external PPS signal
889 *
890 * This routine is called at each PPS signal arrival in order to
891 * discipline the CPU clock oscillator to the PPS signal. It takes two
892 * parameters: REALTIME and MONOTONIC_RAW clock timestamps. The former
893 * is used to correct clock phase error and the latter is used to
894 * correct the frequency.
895 *
896 * This code is based on David Mills's reference nanokernel
897 * implementation. It was mostly rewritten but keeps the same idea.
898 */
899void hardpps(const struct timespec *phase_ts, const struct timespec *raw_ts)
900{
901 struct pps_normtime pts_norm, freq_norm;
902 unsigned long flags;
903
904 pts_norm = pps_normalize_ts(*phase_ts);
905
906 write_seqlock_irqsave(&xtime_lock, flags);
907
908 /* clear the error bits, they will be set again if needed */
909 time_status &= ~(STA_PPSJITTER | STA_PPSWANDER | STA_PPSERROR);
910
911 /* indicate signal presence */
912 time_status |= STA_PPSSIGNAL;
913 pps_valid = PPS_VALID;
914
915 /* when called for the first time,
916 * just start the frequency interval */
917 if (unlikely(pps_fbase.tv_sec == 0)) {
918 pps_fbase = *raw_ts;
919 write_sequnlock_irqrestore(&xtime_lock, flags);
920 return;
921 }
922
923 /* ok, now we have a base for frequency calculation */
924 freq_norm = pps_normalize_ts(timespec_sub(*raw_ts, pps_fbase));
925
926 /* check that the signal is in the range
927 * [1s - MAXFREQ us, 1s + MAXFREQ us], otherwise reject it */
928 if ((freq_norm.sec == 0) ||
929 (freq_norm.nsec > MAXFREQ * freq_norm.sec) ||
930 (freq_norm.nsec < -MAXFREQ * freq_norm.sec)) {
931 time_status |= STA_PPSJITTER;
932 /* restart the frequency calibration interval */
933 pps_fbase = *raw_ts;
934 write_sequnlock_irqrestore(&xtime_lock, flags);
935 pr_err("hardpps: PPSJITTER: bad pulse\n");
936 return;
937 }
938
939 /* signal is ok */
940
941 /* check if the current frequency interval is finished */
942 if (freq_norm.sec >= (1 << pps_shift)) {
943 pps_calcnt++;
944 /* restart the frequency calibration interval */
945 pps_fbase = *raw_ts;
946 hardpps_update_freq(freq_norm);
947 }
948
949 hardpps_update_phase(pts_norm.nsec);
950
951 write_sequnlock_irqrestore(&xtime_lock, flags);
952}
953EXPORT_SYMBOL(hardpps);
954
955#endif /* CONFIG_NTP_PPS */
956
539static int __init ntp_tick_adj_setup(char *str) 957static int __init ntp_tick_adj_setup(char *str)
540{ 958{
541 ntp_tick_adj = simple_strtol(str, NULL, 0); 959 ntp_tick_adj = simple_strtol(str, NULL, 0);
diff --git a/kernel/time/posix-clock.c b/kernel/time/posix-clock.c
new file mode 100644
index 000000000000..c340ca658f37
--- /dev/null
+++ b/kernel/time/posix-clock.c
@@ -0,0 +1,445 @@
1/*
2 * posix-clock.c - support for dynamic clock devices
3 *
4 * Copyright (C) 2010 OMICRON electronics GmbH
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20#include <linux/device.h>
21#include <linux/file.h>
22#include <linux/posix-clock.h>
23#include <linux/slab.h>
24#include <linux/syscalls.h>
25#include <linux/uaccess.h>
26
27static void delete_clock(struct kref *kref);
28
29/*
30 * Returns NULL if the posix_clock instance attached to 'fp' is old and stale.
31 */
32static struct posix_clock *get_posix_clock(struct file *fp)
33{
34 struct posix_clock *clk = fp->private_data;
35
36 down_read(&clk->rwsem);
37
38 if (!clk->zombie)
39 return clk;
40
41 up_read(&clk->rwsem);
42
43 return NULL;
44}
45
46static void put_posix_clock(struct posix_clock *clk)
47{
48 up_read(&clk->rwsem);
49}
50
51static ssize_t posix_clock_read(struct file *fp, char __user *buf,
52 size_t count, loff_t *ppos)
53{
54 struct posix_clock *clk = get_posix_clock(fp);
55 int err = -EINVAL;
56
57 if (!clk)
58 return -ENODEV;
59
60 if (clk->ops.read)
61 err = clk->ops.read(clk, fp->f_flags, buf, count);
62
63 put_posix_clock(clk);
64
65 return err;
66}
67
68static unsigned int posix_clock_poll(struct file *fp, poll_table *wait)
69{
70 struct posix_clock *clk = get_posix_clock(fp);
71 int result = 0;
72
73 if (!clk)
74 return -ENODEV;
75
76 if (clk->ops.poll)
77 result = clk->ops.poll(clk, fp, wait);
78
79 put_posix_clock(clk);
80
81 return result;
82}
83
84static int posix_clock_fasync(int fd, struct file *fp, int on)
85{
86 struct posix_clock *clk = get_posix_clock(fp);
87 int err = 0;
88
89 if (!clk)
90 return -ENODEV;
91
92 if (clk->ops.fasync)
93 err = clk->ops.fasync(clk, fd, fp, on);
94
95 put_posix_clock(clk);
96
97 return err;
98}
99
100static int posix_clock_mmap(struct file *fp, struct vm_area_struct *vma)
101{
102 struct posix_clock *clk = get_posix_clock(fp);
103 int err = -ENODEV;
104
105 if (!clk)
106 return -ENODEV;
107
108 if (clk->ops.mmap)
109 err = clk->ops.mmap(clk, vma);
110
111 put_posix_clock(clk);
112
113 return err;
114}
115
116static long posix_clock_ioctl(struct file *fp,
117 unsigned int cmd, unsigned long arg)
118{
119 struct posix_clock *clk = get_posix_clock(fp);
120 int err = -ENOTTY;
121
122 if (!clk)
123 return -ENODEV;
124
125 if (clk->ops.ioctl)
126 err = clk->ops.ioctl(clk, cmd, arg);
127
128 put_posix_clock(clk);
129
130 return err;
131}
132
133#ifdef CONFIG_COMPAT
134static long posix_clock_compat_ioctl(struct file *fp,
135 unsigned int cmd, unsigned long arg)
136{
137 struct posix_clock *clk = get_posix_clock(fp);
138 int err = -ENOTTY;
139
140 if (!clk)
141 return -ENODEV;
142
143 if (clk->ops.ioctl)
144 err = clk->ops.ioctl(clk, cmd, arg);
145
146 put_posix_clock(clk);
147
148 return err;
149}
150#endif
151
152static int posix_clock_open(struct inode *inode, struct file *fp)
153{
154 int err;
155 struct posix_clock *clk =
156 container_of(inode->i_cdev, struct posix_clock, cdev);
157
158 down_read(&clk->rwsem);
159
160 if (clk->zombie) {
161 err = -ENODEV;
162 goto out;
163 }
164 if (clk->ops.open)
165 err = clk->ops.open(clk, fp->f_mode);
166 else
167 err = 0;
168
169 if (!err) {
170 kref_get(&clk->kref);
171 fp->private_data = clk;
172 }
173out:
174 up_read(&clk->rwsem);
175 return err;
176}
177
178static int posix_clock_release(struct inode *inode, struct file *fp)
179{
180 struct posix_clock *clk = fp->private_data;
181 int err = 0;
182
183 if (clk->ops.release)
184 err = clk->ops.release(clk);
185
186 kref_put(&clk->kref, delete_clock);
187
188 fp->private_data = NULL;
189
190 return err;
191}
192
193static const struct file_operations posix_clock_file_operations = {
194 .owner = THIS_MODULE,
195 .llseek = no_llseek,
196 .read = posix_clock_read,
197 .poll = posix_clock_poll,
198 .unlocked_ioctl = posix_clock_ioctl,
199 .open = posix_clock_open,
200 .release = posix_clock_release,
201 .fasync = posix_clock_fasync,
202 .mmap = posix_clock_mmap,
203#ifdef CONFIG_COMPAT
204 .compat_ioctl = posix_clock_compat_ioctl,
205#endif
206};
207
208int posix_clock_register(struct posix_clock *clk, dev_t devid)
209{
210 int err;
211
212 kref_init(&clk->kref);
213 init_rwsem(&clk->rwsem);
214
215 cdev_init(&clk->cdev, &posix_clock_file_operations);
216 clk->cdev.owner = clk->ops.owner;
217 err = cdev_add(&clk->cdev, devid, 1);
218
219 return err;
220}
221EXPORT_SYMBOL_GPL(posix_clock_register);
222
223static void delete_clock(struct kref *kref)
224{
225 struct posix_clock *clk = container_of(kref, struct posix_clock, kref);
226
227 if (clk->release)
228 clk->release(clk);
229}
230
231void posix_clock_unregister(struct posix_clock *clk)
232{
233 cdev_del(&clk->cdev);
234
235 down_write(&clk->rwsem);
236 clk->zombie = true;
237 up_write(&clk->rwsem);
238
239 kref_put(&clk->kref, delete_clock);
240}
241EXPORT_SYMBOL_GPL(posix_clock_unregister);
242
243struct posix_clock_desc {
244 struct file *fp;
245 struct posix_clock *clk;
246};
247
248static int get_clock_desc(const clockid_t id, struct posix_clock_desc *cd)
249{
250 struct file *fp = fget(CLOCKID_TO_FD(id));
251 int err = -EINVAL;
252
253 if (!fp)
254 return err;
255
256 if (fp->f_op->open != posix_clock_open || !fp->private_data)
257 goto out;
258
259 cd->fp = fp;
260 cd->clk = get_posix_clock(fp);
261
262 err = cd->clk ? 0 : -ENODEV;
263out:
264 if (err)
265 fput(fp);
266 return err;
267}
268
269static void put_clock_desc(struct posix_clock_desc *cd)
270{
271 put_posix_clock(cd->clk);
272 fput(cd->fp);
273}
274
275static int pc_clock_adjtime(clockid_t id, struct timex *tx)
276{
277 struct posix_clock_desc cd;
278 int err;
279
280 err = get_clock_desc(id, &cd);
281 if (err)
282 return err;
283
284 if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
285 err = -EACCES;
286 goto out;
287 }
288
289 if (cd.clk->ops.clock_adjtime)
290 err = cd.clk->ops.clock_adjtime(cd.clk, tx);
291 else
292 err = -EOPNOTSUPP;
293out:
294 put_clock_desc(&cd);
295
296 return err;
297}
298
299static int pc_clock_gettime(clockid_t id, struct timespec *ts)
300{
301 struct posix_clock_desc cd;
302 int err;
303
304 err = get_clock_desc(id, &cd);
305 if (err)
306 return err;
307
308 if (cd.clk->ops.clock_gettime)
309 err = cd.clk->ops.clock_gettime(cd.clk, ts);
310 else
311 err = -EOPNOTSUPP;
312
313 put_clock_desc(&cd);
314
315 return err;
316}
317
318static int pc_clock_getres(clockid_t id, struct timespec *ts)
319{
320 struct posix_clock_desc cd;
321 int err;
322
323 err = get_clock_desc(id, &cd);
324 if (err)
325 return err;
326
327 if (cd.clk->ops.clock_getres)
328 err = cd.clk->ops.clock_getres(cd.clk, ts);
329 else
330 err = -EOPNOTSUPP;
331
332 put_clock_desc(&cd);
333
334 return err;
335}
336
337static int pc_clock_settime(clockid_t id, const struct timespec *ts)
338{
339 struct posix_clock_desc cd;
340 int err;
341
342 err = get_clock_desc(id, &cd);
343 if (err)
344 return err;
345
346 if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
347 err = -EACCES;
348 goto out;
349 }
350
351 if (cd.clk->ops.clock_settime)
352 err = cd.clk->ops.clock_settime(cd.clk, ts);
353 else
354 err = -EOPNOTSUPP;
355out:
356 put_clock_desc(&cd);
357
358 return err;
359}
360
361static int pc_timer_create(struct k_itimer *kit)
362{
363 clockid_t id = kit->it_clock;
364 struct posix_clock_desc cd;
365 int err;
366
367 err = get_clock_desc(id, &cd);
368 if (err)
369 return err;
370
371 if (cd.clk->ops.timer_create)
372 err = cd.clk->ops.timer_create(cd.clk, kit);
373 else
374 err = -EOPNOTSUPP;
375
376 put_clock_desc(&cd);
377
378 return err;
379}
380
381static int pc_timer_delete(struct k_itimer *kit)
382{
383 clockid_t id = kit->it_clock;
384 struct posix_clock_desc cd;
385 int err;
386
387 err = get_clock_desc(id, &cd);
388 if (err)
389 return err;
390
391 if (cd.clk->ops.timer_delete)
392 err = cd.clk->ops.timer_delete(cd.clk, kit);
393 else
394 err = -EOPNOTSUPP;
395
396 put_clock_desc(&cd);
397
398 return err;
399}
400
401static void pc_timer_gettime(struct k_itimer *kit, struct itimerspec *ts)
402{
403 clockid_t id = kit->it_clock;
404 struct posix_clock_desc cd;
405
406 if (get_clock_desc(id, &cd))
407 return;
408
409 if (cd.clk->ops.timer_gettime)
410 cd.clk->ops.timer_gettime(cd.clk, kit, ts);
411
412 put_clock_desc(&cd);
413}
414
415static int pc_timer_settime(struct k_itimer *kit, int flags,
416 struct itimerspec *ts, struct itimerspec *old)
417{
418 clockid_t id = kit->it_clock;
419 struct posix_clock_desc cd;
420 int err;
421
422 err = get_clock_desc(id, &cd);
423 if (err)
424 return err;
425
426 if (cd.clk->ops.timer_settime)
427 err = cd.clk->ops.timer_settime(cd.clk, kit, flags, ts, old);
428 else
429 err = -EOPNOTSUPP;
430
431 put_clock_desc(&cd);
432
433 return err;
434}
435
436struct k_clock clock_posix_dynamic = {
437 .clock_getres = pc_clock_getres,
438 .clock_set = pc_clock_settime,
439 .clock_get = pc_clock_gettime,
440 .clock_adj = pc_clock_adjtime,
441 .timer_create = pc_timer_create,
442 .timer_set = pc_timer_settime,
443 .timer_del = pc_timer_delete,
444 .timer_get = pc_timer_gettime,
445};
diff --git a/kernel/time/tick-broadcast.c b/kernel/time/tick-broadcast.c
index 48b2761b5668..c7218d132738 100644
--- a/kernel/time/tick-broadcast.c
+++ b/kernel/time/tick-broadcast.c
@@ -18,7 +18,6 @@
18#include <linux/percpu.h> 18#include <linux/percpu.h>
19#include <linux/profile.h> 19#include <linux/profile.h>
20#include <linux/sched.h> 20#include <linux/sched.h>
21#include <linux/tick.h>
22 21
23#include "tick-internal.h" 22#include "tick-internal.h"
24 23
@@ -457,23 +456,27 @@ void tick_broadcast_oneshot_control(unsigned long reason)
457 unsigned long flags; 456 unsigned long flags;
458 int cpu; 457 int cpu;
459 458
460 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
461
462 /* 459 /*
463 * Periodic mode does not care about the enter/exit of power 460 * Periodic mode does not care about the enter/exit of power
464 * states 461 * states
465 */ 462 */
466 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) 463 if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
467 goto out; 464 return;
468 465
469 bc = tick_broadcast_device.evtdev; 466 /*
467 * We are called with preemtion disabled from the depth of the
468 * idle code, so we can't be moved away.
469 */
470 cpu = smp_processor_id(); 470 cpu = smp_processor_id();
471 td = &per_cpu(tick_cpu_device, cpu); 471 td = &per_cpu(tick_cpu_device, cpu);
472 dev = td->evtdev; 472 dev = td->evtdev;
473 473
474 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) 474 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
475 goto out; 475 return;
476
477 bc = tick_broadcast_device.evtdev;
476 478
479 raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
477 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) { 480 if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
478 if (!cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) { 481 if (!cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
479 cpumask_set_cpu(cpu, tick_get_broadcast_oneshot_mask()); 482 cpumask_set_cpu(cpu, tick_get_broadcast_oneshot_mask());
@@ -490,8 +493,6 @@ void tick_broadcast_oneshot_control(unsigned long reason)
490 tick_program_event(dev->next_event, 1); 493 tick_program_event(dev->next_event, 1);
491 } 494 }
492 } 495 }
493
494out:
495 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags); 496 raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
496} 497}
497 498
@@ -523,10 +524,11 @@ static void tick_broadcast_init_next_event(struct cpumask *mask,
523 */ 524 */
524void tick_broadcast_setup_oneshot(struct clock_event_device *bc) 525void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
525{ 526{
527 int cpu = smp_processor_id();
528
526 /* Set it up only once ! */ 529 /* Set it up only once ! */
527 if (bc->event_handler != tick_handle_oneshot_broadcast) { 530 if (bc->event_handler != tick_handle_oneshot_broadcast) {
528 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC; 531 int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
529 int cpu = smp_processor_id();
530 532
531 bc->event_handler = tick_handle_oneshot_broadcast; 533 bc->event_handler = tick_handle_oneshot_broadcast;
532 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT); 534 clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
@@ -552,6 +554,15 @@ void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
552 tick_broadcast_set_event(tick_next_period, 1); 554 tick_broadcast_set_event(tick_next_period, 1);
553 } else 555 } else
554 bc->next_event.tv64 = KTIME_MAX; 556 bc->next_event.tv64 = KTIME_MAX;
557 } else {
558 /*
559 * The first cpu which switches to oneshot mode sets
560 * the bit for all other cpus which are in the general
561 * (periodic) broadcast mask. So the bit is set and
562 * would prevent the first broadcast enter after this
563 * to program the bc device.
564 */
565 tick_broadcast_clear_oneshot(cpu);
555 } 566 }
556} 567}
557 568
@@ -600,4 +611,14 @@ int tick_broadcast_oneshot_active(void)
600 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT; 611 return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
601} 612}
602 613
614/*
615 * Check whether the broadcast device supports oneshot.
616 */
617bool tick_broadcast_oneshot_available(void)
618{
619 struct clock_event_device *bc = tick_broadcast_device.evtdev;
620
621 return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
622}
623
603#endif 624#endif
diff --git a/kernel/time/tick-common.c b/kernel/time/tick-common.c
index b6b898d2eeef..119528de8235 100644
--- a/kernel/time/tick-common.c
+++ b/kernel/time/tick-common.c
@@ -18,7 +18,6 @@
18#include <linux/percpu.h> 18#include <linux/percpu.h>
19#include <linux/profile.h> 19#include <linux/profile.h>
20#include <linux/sched.h> 20#include <linux/sched.h>
21#include <linux/tick.h>
22 21
23#include <asm/irq_regs.h> 22#include <asm/irq_regs.h>
24 23
@@ -49,9 +48,13 @@ struct tick_device *tick_get_device(int cpu)
49 */ 48 */
50int tick_is_oneshot_available(void) 49int tick_is_oneshot_available(void)
51{ 50{
52 struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev; 51 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
53 52
54 return dev && (dev->features & CLOCK_EVT_FEAT_ONESHOT); 53 if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT))
54 return 0;
55 if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
56 return 1;
57 return tick_broadcast_oneshot_available();
55} 58}
56 59
57/* 60/*
diff --git a/kernel/time/tick-internal.h b/kernel/time/tick-internal.h
index 290eefbc1f60..1009b06d6f89 100644
--- a/kernel/time/tick-internal.h
+++ b/kernel/time/tick-internal.h
@@ -1,6 +1,10 @@
1/* 1/*
2 * tick internal variable and functions used by low/high res code 2 * tick internal variable and functions used by low/high res code
3 */ 3 */
4#include <linux/hrtimer.h>
5#include <linux/tick.h>
6
7#ifdef CONFIG_GENERIC_CLOCKEVENTS_BUILD
4 8
5#define TICK_DO_TIMER_NONE -1 9#define TICK_DO_TIMER_NONE -1
6#define TICK_DO_TIMER_BOOT -2 10#define TICK_DO_TIMER_BOOT -2
@@ -36,6 +40,7 @@ extern void tick_shutdown_broadcast_oneshot(unsigned int *cpup);
36extern int tick_resume_broadcast_oneshot(struct clock_event_device *bc); 40extern int tick_resume_broadcast_oneshot(struct clock_event_device *bc);
37extern int tick_broadcast_oneshot_active(void); 41extern int tick_broadcast_oneshot_active(void);
38extern void tick_check_oneshot_broadcast(int cpu); 42extern void tick_check_oneshot_broadcast(int cpu);
43bool tick_broadcast_oneshot_available(void);
39# else /* BROADCAST */ 44# else /* BROADCAST */
40static inline void tick_broadcast_setup_oneshot(struct clock_event_device *bc) 45static inline void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
41{ 46{
@@ -46,6 +51,7 @@ static inline void tick_broadcast_switch_to_oneshot(void) { }
46static inline void tick_shutdown_broadcast_oneshot(unsigned int *cpup) { } 51static inline void tick_shutdown_broadcast_oneshot(unsigned int *cpup) { }
47static inline int tick_broadcast_oneshot_active(void) { return 0; } 52static inline int tick_broadcast_oneshot_active(void) { return 0; }
48static inline void tick_check_oneshot_broadcast(int cpu) { } 53static inline void tick_check_oneshot_broadcast(int cpu) { }
54static inline bool tick_broadcast_oneshot_available(void) { return true; }
49# endif /* !BROADCAST */ 55# endif /* !BROADCAST */
50 56
51#else /* !ONESHOT */ 57#else /* !ONESHOT */
@@ -76,6 +82,7 @@ static inline int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
76 return 0; 82 return 0;
77} 83}
78static inline int tick_broadcast_oneshot_active(void) { return 0; } 84static inline int tick_broadcast_oneshot_active(void) { return 0; }
85static inline bool tick_broadcast_oneshot_available(void) { return false; }
79#endif /* !TICK_ONESHOT */ 86#endif /* !TICK_ONESHOT */
80 87
81/* 88/*
@@ -132,3 +139,8 @@ static inline int tick_device_is_functional(struct clock_event_device *dev)
132{ 139{
133 return !(dev->features & CLOCK_EVT_FEAT_DUMMY); 140 return !(dev->features & CLOCK_EVT_FEAT_DUMMY);
134} 141}
142
143#endif
144
145extern void do_timer(unsigned long ticks);
146extern seqlock_t xtime_lock;
diff --git a/kernel/time/tick-oneshot.c b/kernel/time/tick-oneshot.c
index aada0e52680a..2d04411a5f05 100644
--- a/kernel/time/tick-oneshot.c
+++ b/kernel/time/tick-oneshot.c
@@ -18,7 +18,6 @@
18#include <linux/percpu.h> 18#include <linux/percpu.h>
19#include <linux/profile.h> 19#include <linux/profile.h>
20#include <linux/sched.h> 20#include <linux/sched.h>
21#include <linux/tick.h>
22 21
23#include "tick-internal.h" 22#include "tick-internal.h"
24 23
@@ -95,7 +94,7 @@ int tick_dev_program_event(struct clock_event_device *dev, ktime_t expires,
95 */ 94 */
96int tick_program_event(ktime_t expires, int force) 95int tick_program_event(ktime_t expires, int force)
97{ 96{
98 struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev; 97 struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
99 98
100 return tick_dev_program_event(dev, expires, force); 99 return tick_dev_program_event(dev, expires, force);
101} 100}
@@ -167,7 +166,7 @@ int tick_oneshot_mode_active(void)
167 int ret; 166 int ret;
168 167
169 local_irq_save(flags); 168 local_irq_save(flags);
170 ret = __get_cpu_var(tick_cpu_device).mode == TICKDEV_MODE_ONESHOT; 169 ret = __this_cpu_read(tick_cpu_device.mode) == TICKDEV_MODE_ONESHOT;
171 local_irq_restore(flags); 170 local_irq_restore(flags);
172 171
173 return ret; 172 return ret;
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index bb2d8b7850a3..0c0e02f1b819 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -19,7 +19,6 @@
19#include <linux/percpu.h> 19#include <linux/percpu.h>
20#include <linux/profile.h> 20#include <linux/profile.h>
21#include <linux/sched.h> 21#include <linux/sched.h>
22#include <linux/tick.h>
23#include <linux/module.h> 22#include <linux/module.h>
24 23
25#include <asm/irq_regs.h> 24#include <asm/irq_regs.h>
@@ -642,8 +641,7 @@ static void tick_nohz_switch_to_nohz(void)
642 } 641 }
643 local_irq_enable(); 642 local_irq_enable();
644 643
645 printk(KERN_INFO "Switched to NOHz mode on CPU #%d\n", 644 printk(KERN_INFO "Switched to NOHz mode on CPU #%d\n", smp_processor_id());
646 smp_processor_id());
647} 645}
648 646
649/* 647/*
@@ -842,8 +840,10 @@ void tick_setup_sched_timer(void)
842 } 840 }
843 841
844#ifdef CONFIG_NO_HZ 842#ifdef CONFIG_NO_HZ
845 if (tick_nohz_enabled) 843 if (tick_nohz_enabled) {
846 ts->nohz_mode = NOHZ_MODE_HIGHRES; 844 ts->nohz_mode = NOHZ_MODE_HIGHRES;
845 printk(KERN_INFO "Switched to NOHz mode on CPU #%d\n", smp_processor_id());
846 }
847#endif 847#endif
848} 848}
849#endif /* HIGH_RES_TIMERS */ 849#endif /* HIGH_RES_TIMERS */
diff --git a/kernel/time/timecompare.c b/kernel/time/timecompare.c
index ac38fbb176cc..a9ae369925ce 100644
--- a/kernel/time/timecompare.c
+++ b/kernel/time/timecompare.c
@@ -21,6 +21,7 @@
21#include <linux/module.h> 21#include <linux/module.h>
22#include <linux/slab.h> 22#include <linux/slab.h>
23#include <linux/math64.h> 23#include <linux/math64.h>
24#include <linux/kernel.h>
24 25
25/* 26/*
26 * fixed point arithmetic scale factor for skew 27 * fixed point arithmetic scale factor for skew
@@ -57,11 +58,11 @@ int timecompare_offset(struct timecompare *sync,
57 int index; 58 int index;
58 int num_samples = sync->num_samples; 59 int num_samples = sync->num_samples;
59 60
60 if (num_samples > sizeof(buffer)/sizeof(buffer[0])) { 61 if (num_samples > ARRAY_SIZE(buffer)) {
61 samples = kmalloc(sizeof(*samples) * num_samples, GFP_ATOMIC); 62 samples = kmalloc(sizeof(*samples) * num_samples, GFP_ATOMIC);
62 if (!samples) { 63 if (!samples) {
63 samples = buffer; 64 samples = buffer;
64 num_samples = sizeof(buffer)/sizeof(buffer[0]); 65 num_samples = ARRAY_SIZE(buffer);
65 } 66 }
66 } else { 67 } else {
67 samples = buffer; 68 samples = buffer;
diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c
index 49010d822f72..342408cf68dd 100644
--- a/kernel/time/timekeeping.c
+++ b/kernel/time/timekeeping.c
@@ -14,7 +14,7 @@
14#include <linux/init.h> 14#include <linux/init.h>
15#include <linux/mm.h> 15#include <linux/mm.h>
16#include <linux/sched.h> 16#include <linux/sched.h>
17#include <linux/sysdev.h> 17#include <linux/syscore_ops.h>
18#include <linux/clocksource.h> 18#include <linux/clocksource.h>
19#include <linux/jiffies.h> 19#include <linux/jiffies.h>
20#include <linux/time.h> 20#include <linux/time.h>
@@ -32,6 +32,8 @@ struct timekeeper {
32 cycle_t cycle_interval; 32 cycle_t cycle_interval;
33 /* Number of clock shifted nano seconds in one NTP interval. */ 33 /* Number of clock shifted nano seconds in one NTP interval. */
34 u64 xtime_interval; 34 u64 xtime_interval;
35 /* shifted nano seconds left over when rounding cycle_interval */
36 s64 xtime_remainder;
35 /* Raw nano seconds accumulated per NTP interval. */ 37 /* Raw nano seconds accumulated per NTP interval. */
36 u32 raw_interval; 38 u32 raw_interval;
37 39
@@ -47,7 +49,7 @@ struct timekeeper {
47 u32 mult; 49 u32 mult;
48}; 50};
49 51
50struct timekeeper timekeeper; 52static struct timekeeper timekeeper;
51 53
52/** 54/**
53 * timekeeper_setup_internals - Set up internals to use clocksource clock. 55 * timekeeper_setup_internals - Set up internals to use clocksource clock.
@@ -62,7 +64,7 @@ struct timekeeper timekeeper;
62static void timekeeper_setup_internals(struct clocksource *clock) 64static void timekeeper_setup_internals(struct clocksource *clock)
63{ 65{
64 cycle_t interval; 66 cycle_t interval;
65 u64 tmp; 67 u64 tmp, ntpinterval;
66 68
67 timekeeper.clock = clock; 69 timekeeper.clock = clock;
68 clock->cycle_last = clock->read(clock); 70 clock->cycle_last = clock->read(clock);
@@ -70,6 +72,7 @@ static void timekeeper_setup_internals(struct clocksource *clock)
70 /* Do the ns -> cycle conversion first, using original mult */ 72 /* Do the ns -> cycle conversion first, using original mult */
71 tmp = NTP_INTERVAL_LENGTH; 73 tmp = NTP_INTERVAL_LENGTH;
72 tmp <<= clock->shift; 74 tmp <<= clock->shift;
75 ntpinterval = tmp;
73 tmp += clock->mult/2; 76 tmp += clock->mult/2;
74 do_div(tmp, clock->mult); 77 do_div(tmp, clock->mult);
75 if (tmp == 0) 78 if (tmp == 0)
@@ -80,6 +83,7 @@ static void timekeeper_setup_internals(struct clocksource *clock)
80 83
81 /* Go back from cycles -> shifted ns */ 84 /* Go back from cycles -> shifted ns */
82 timekeeper.xtime_interval = (u64) interval * clock->mult; 85 timekeeper.xtime_interval = (u64) interval * clock->mult;
86 timekeeper.xtime_remainder = ntpinterval - timekeeper.xtime_interval;
83 timekeeper.raw_interval = 87 timekeeper.raw_interval =
84 ((u64) interval * clock->mult) >> clock->shift; 88 ((u64) interval * clock->mult) >> clock->shift;
85 89
@@ -160,7 +164,7 @@ static struct timespec total_sleep_time;
160/* 164/*
161 * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock. 165 * The raw monotonic time for the CLOCK_MONOTONIC_RAW posix clock.
162 */ 166 */
163struct timespec raw_time; 167static struct timespec raw_time;
164 168
165/* flag for if timekeeping is suspended */ 169/* flag for if timekeeping is suspended */
166int __read_mostly timekeeping_suspended; 170int __read_mostly timekeeping_suspended;
@@ -284,6 +288,49 @@ void ktime_get_ts(struct timespec *ts)
284} 288}
285EXPORT_SYMBOL_GPL(ktime_get_ts); 289EXPORT_SYMBOL_GPL(ktime_get_ts);
286 290
291#ifdef CONFIG_NTP_PPS
292
293/**
294 * getnstime_raw_and_real - get day and raw monotonic time in timespec format
295 * @ts_raw: pointer to the timespec to be set to raw monotonic time
296 * @ts_real: pointer to the timespec to be set to the time of day
297 *
298 * This function reads both the time of day and raw monotonic time at the
299 * same time atomically and stores the resulting timestamps in timespec
300 * format.
301 */
302void getnstime_raw_and_real(struct timespec *ts_raw, struct timespec *ts_real)
303{
304 unsigned long seq;
305 s64 nsecs_raw, nsecs_real;
306
307 WARN_ON_ONCE(timekeeping_suspended);
308
309 do {
310 u32 arch_offset;
311
312 seq = read_seqbegin(&xtime_lock);
313
314 *ts_raw = raw_time;
315 *ts_real = xtime;
316
317 nsecs_raw = timekeeping_get_ns_raw();
318 nsecs_real = timekeeping_get_ns();
319
320 /* If arch requires, add in gettimeoffset() */
321 arch_offset = arch_gettimeoffset();
322 nsecs_raw += arch_offset;
323 nsecs_real += arch_offset;
324
325 } while (read_seqretry(&xtime_lock, seq));
326
327 timespec_add_ns(ts_raw, nsecs_raw);
328 timespec_add_ns(ts_real, nsecs_real);
329}
330EXPORT_SYMBOL(getnstime_raw_and_real);
331
332#endif /* CONFIG_NTP_PPS */
333
287/** 334/**
288 * do_gettimeofday - Returns the time of day in a timeval 335 * do_gettimeofday - Returns the time of day in a timeval
289 * @tv: pointer to the timeval to be set 336 * @tv: pointer to the timeval to be set
@@ -306,7 +353,7 @@ EXPORT_SYMBOL(do_gettimeofday);
306 * 353 *
307 * Sets the time of day to the new time and update NTP and notify hrtimers 354 * Sets the time of day to the new time and update NTP and notify hrtimers
308 */ 355 */
309int do_settimeofday(struct timespec *tv) 356int do_settimeofday(const struct timespec *tv)
310{ 357{
311 struct timespec ts_delta; 358 struct timespec ts_delta;
312 unsigned long flags; 359 unsigned long flags;
@@ -340,6 +387,42 @@ int do_settimeofday(struct timespec *tv)
340 387
341EXPORT_SYMBOL(do_settimeofday); 388EXPORT_SYMBOL(do_settimeofday);
342 389
390
391/**
392 * timekeeping_inject_offset - Adds or subtracts from the current time.
393 * @tv: pointer to the timespec variable containing the offset
394 *
395 * Adds or subtracts an offset value from the current time.
396 */
397int timekeeping_inject_offset(struct timespec *ts)
398{
399 unsigned long flags;
400
401 if ((unsigned long)ts->tv_nsec >= NSEC_PER_SEC)
402 return -EINVAL;
403
404 write_seqlock_irqsave(&xtime_lock, flags);
405
406 timekeeping_forward_now();
407
408 xtime = timespec_add(xtime, *ts);
409 wall_to_monotonic = timespec_sub(wall_to_monotonic, *ts);
410
411 timekeeper.ntp_error = 0;
412 ntp_clear();
413
414 update_vsyscall(&xtime, &wall_to_monotonic, timekeeper.clock,
415 timekeeper.mult);
416
417 write_sequnlock_irqrestore(&xtime_lock, flags);
418
419 /* signal hrtimers about time change */
420 clock_was_set();
421
422 return 0;
423}
424EXPORT_SYMBOL(timekeeping_inject_offset);
425
343/** 426/**
344 * change_clocksource - Swaps clocksources if a new one is available 427 * change_clocksource - Swaps clocksources if a new one is available
345 * 428 *
@@ -513,14 +596,65 @@ void __init timekeeping_init(void)
513static struct timespec timekeeping_suspend_time; 596static struct timespec timekeeping_suspend_time;
514 597
515/** 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/**
516 * timekeeping_resume - Resumes the generic timekeeping subsystem. 651 * timekeeping_resume - Resumes the generic timekeeping subsystem.
517 * @dev: unused
518 * 652 *
519 * This is for the generic clocksource timekeeping. 653 * This is for the generic clocksource timekeeping.
520 * xtime/wall_to_monotonic/jiffies/etc are 654 * xtime/wall_to_monotonic/jiffies/etc are
521 * still managed by arch specific suspend/resume code. 655 * still managed by arch specific suspend/resume code.
522 */ 656 */
523static int timekeeping_resume(struct sys_device *dev) 657static void timekeeping_resume(void)
524{ 658{
525 unsigned long flags; 659 unsigned long flags;
526 struct timespec ts; 660 struct timespec ts;
@@ -533,9 +667,7 @@ static int timekeeping_resume(struct sys_device *dev)
533 667
534 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) { 668 if (timespec_compare(&ts, &timekeeping_suspend_time) > 0) {
535 ts = timespec_sub(ts, timekeeping_suspend_time); 669 ts = timespec_sub(ts, timekeeping_suspend_time);
536 xtime = timespec_add(xtime, ts); 670 __timekeeping_inject_sleeptime(&ts);
537 wall_to_monotonic = timespec_sub(wall_to_monotonic, ts);
538 total_sleep_time = timespec_add(total_sleep_time, ts);
539 } 671 }
540 /* re-base the last cycle value */ 672 /* re-base the last cycle value */
541 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock); 673 timekeeper.clock->cycle_last = timekeeper.clock->read(timekeeper.clock);
@@ -548,12 +680,10 @@ static int timekeeping_resume(struct sys_device *dev)
548 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL); 680 clockevents_notify(CLOCK_EVT_NOTIFY_RESUME, NULL);
549 681
550 /* Resume hrtimers */ 682 /* Resume hrtimers */
551 hres_timers_resume(); 683 hrtimers_resume();
552
553 return 0;
554} 684}
555 685
556static int timekeeping_suspend(struct sys_device *dev, pm_message_t state) 686static int timekeeping_suspend(void)
557{ 687{
558 unsigned long flags; 688 unsigned long flags;
559 689
@@ -571,26 +701,18 @@ static int timekeeping_suspend(struct sys_device *dev, pm_message_t state)
571} 701}
572 702
573/* sysfs resume/suspend bits for timekeeping */ 703/* sysfs resume/suspend bits for timekeeping */
574static struct sysdev_class timekeeping_sysclass = { 704static struct syscore_ops timekeeping_syscore_ops = {
575 .name = "timekeeping",
576 .resume = timekeeping_resume, 705 .resume = timekeeping_resume,
577 .suspend = timekeeping_suspend, 706 .suspend = timekeeping_suspend,
578}; 707};
579 708
580static struct sys_device device_timer = { 709static int __init timekeeping_init_ops(void)
581 .id = 0,
582 .cls = &timekeeping_sysclass,
583};
584
585static int __init timekeeping_init_device(void)
586{ 710{
587 int error = sysdev_class_register(&timekeeping_sysclass); 711 register_syscore_ops(&timekeeping_syscore_ops);
588 if (!error) 712 return 0;
589 error = sysdev_register(&device_timer);
590 return error;
591} 713}
592 714
593device_initcall(timekeeping_init_device); 715device_initcall(timekeeping_init_ops);
594 716
595/* 717/*
596 * If the error is already larger, we look ahead even further 718 * If the error is already larger, we look ahead even further
@@ -719,7 +841,8 @@ static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
719 841
720 /* Accumulate error between NTP and clock interval */ 842 /* Accumulate error between NTP and clock interval */
721 timekeeper.ntp_error += tick_length << shift; 843 timekeeper.ntp_error += tick_length << shift;
722 timekeeper.ntp_error -= timekeeper.xtime_interval << 844 timekeeper.ntp_error -=
845 (timekeeper.xtime_interval + timekeeper.xtime_remainder) <<
723 (timekeeper.ntp_error_shift + shift); 846 (timekeeper.ntp_error_shift + shift);
724 847
725 return offset; 848 return offset;
@@ -731,7 +854,7 @@ static cycle_t logarithmic_accumulation(cycle_t offset, int shift)
731 * 854 *
732 * Called from the timer interrupt, must hold a write on xtime_lock. 855 * Called from the timer interrupt, must hold a write on xtime_lock.
733 */ 856 */
734void update_wall_time(void) 857static void update_wall_time(void)
735{ 858{
736 struct clocksource *clock; 859 struct clocksource *clock;
737 cycle_t offset; 860 cycle_t offset;
@@ -823,7 +946,7 @@ void update_wall_time(void)
823 * getboottime - Return the real time of system boot. 946 * getboottime - Return the real time of system boot.
824 * @ts: pointer to the timespec to be set 947 * @ts: pointer to the timespec to be set
825 * 948 *
826 * Returns the time of day in a timespec. 949 * Returns the wall-time of boot in a timespec.
827 * 950 *
828 * This is based on the wall_to_monotonic offset and the total suspend 951 * This is based on the wall_to_monotonic offset and the total suspend
829 * time. Calls to settimeofday will affect the value returned (which 952 * time. Calls to settimeofday will affect the value returned (which
@@ -841,6 +964,55 @@ void getboottime(struct timespec *ts)
841} 964}
842EXPORT_SYMBOL_GPL(getboottime); 965EXPORT_SYMBOL_GPL(getboottime);
843 966
967
968/**
969 * get_monotonic_boottime - Returns monotonic time since boot
970 * @ts: pointer to the timespec to be set
971 *
972 * Returns the monotonic time since boot in a timespec.
973 *
974 * This is similar to CLOCK_MONTONIC/ktime_get_ts, but also
975 * includes the time spent in suspend.
976 */
977void get_monotonic_boottime(struct timespec *ts)
978{
979 struct timespec tomono, sleep;
980 unsigned int seq;
981 s64 nsecs;
982
983 WARN_ON(timekeeping_suspended);
984
985 do {
986 seq = read_seqbegin(&xtime_lock);
987 *ts = xtime;
988 tomono = wall_to_monotonic;
989 sleep = total_sleep_time;
990 nsecs = timekeeping_get_ns();
991
992 } while (read_seqretry(&xtime_lock, seq));
993
994 set_normalized_timespec(ts, ts->tv_sec + tomono.tv_sec + sleep.tv_sec,
995 ts->tv_nsec + tomono.tv_nsec + sleep.tv_nsec + nsecs);
996}
997EXPORT_SYMBOL_GPL(get_monotonic_boottime);
998
999/**
1000 * ktime_get_boottime - Returns monotonic time since boot in a ktime
1001 *
1002 * Returns the monotonic time since boot in a ktime
1003 *
1004 * This is similar to CLOCK_MONTONIC/ktime_get, but also
1005 * includes the time spent in suspend.
1006 */
1007ktime_t ktime_get_boottime(void)
1008{
1009 struct timespec ts;
1010
1011 get_monotonic_boottime(&ts);
1012 return timespec_to_ktime(ts);
1013}
1014EXPORT_SYMBOL_GPL(ktime_get_boottime);
1015
844/** 1016/**
845 * monotonic_to_bootbased - Convert the monotonic time to boot based. 1017 * monotonic_to_bootbased - Convert the monotonic time to boot based.
846 * @ts: pointer to the timespec to be converted 1018 * @ts: pointer to the timespec to be converted
@@ -862,11 +1034,6 @@ struct timespec __current_kernel_time(void)
862 return xtime; 1034 return xtime;
863} 1035}
864 1036
865struct timespec __get_wall_to_monotonic(void)
866{
867 return wall_to_monotonic;
868}
869
870struct timespec current_kernel_time(void) 1037struct timespec current_kernel_time(void)
871{ 1038{
872 struct timespec now; 1039 struct timespec now;
@@ -898,3 +1065,63 @@ struct timespec get_monotonic_coarse(void)
898 now.tv_nsec + mono.tv_nsec); 1065 now.tv_nsec + mono.tv_nsec);
899 return now; 1066 return now;
900} 1067}
1068
1069/*
1070 * The 64-bit jiffies value is not atomic - you MUST NOT read it
1071 * without sampling the sequence number in xtime_lock.
1072 * jiffies is defined in the linker script...
1073 */
1074void do_timer(unsigned long ticks)
1075{
1076 jiffies_64 += ticks;
1077 update_wall_time();
1078 calc_global_load(ticks);
1079}
1080
1081/**
1082 * get_xtime_and_monotonic_and_sleep_offset() - get xtime, wall_to_monotonic,
1083 * and sleep offsets.
1084 * @xtim: pointer to timespec to be set with xtime
1085 * @wtom: pointer to timespec to be set with wall_to_monotonic
1086 * @sleep: pointer to timespec to be set with time in suspend
1087 */
1088void get_xtime_and_monotonic_and_sleep_offset(struct timespec *xtim,
1089 struct timespec *wtom, struct timespec *sleep)
1090{
1091 unsigned long seq;
1092
1093 do {
1094 seq = read_seqbegin(&xtime_lock);
1095 *xtim = xtime;
1096 *wtom = wall_to_monotonic;
1097 *sleep = total_sleep_time;
1098 } while (read_seqretry(&xtime_lock, seq));
1099}
1100
1101/**
1102 * ktime_get_monotonic_offset() - get wall_to_monotonic in ktime_t format
1103 */
1104ktime_t ktime_get_monotonic_offset(void)
1105{
1106 unsigned long seq;
1107 struct timespec wtom;
1108
1109 do {
1110 seq = read_seqbegin(&xtime_lock);
1111 wtom = wall_to_monotonic;
1112 } while (read_seqretry(&xtime_lock, seq));
1113 return timespec_to_ktime(wtom);
1114}
1115
1116/**
1117 * xtime_update() - advances the timekeeping infrastructure
1118 * @ticks: number of ticks, that have elapsed since the last call.
1119 *
1120 * Must be called with interrupts disabled.
1121 */
1122void xtime_update(unsigned long ticks)
1123{
1124 write_seqlock(&xtime_lock);
1125 do_timer(ticks);
1126 write_sequnlock(&xtime_lock);
1127}
diff --git a/kernel/time/timer_list.c b/kernel/time/timer_list.c
index ab8f5e33fa92..3258455549f4 100644
--- a/kernel/time/timer_list.c
+++ b/kernel/time/timer_list.c
@@ -41,7 +41,7 @@ static void print_name_offset(struct seq_file *m, void *sym)
41 char symname[KSYM_NAME_LEN]; 41 char symname[KSYM_NAME_LEN];
42 42
43 if (lookup_symbol_name((unsigned long)sym, symname) < 0) 43 if (lookup_symbol_name((unsigned long)sym, symname) < 0)
44 SEQ_printf(m, "<%p>", sym); 44 SEQ_printf(m, "<%pK>", sym);
45 else 45 else
46 SEQ_printf(m, "%s", symname); 46 SEQ_printf(m, "%s", symname);
47} 47}
@@ -79,26 +79,26 @@ print_active_timers(struct seq_file *m, struct hrtimer_clock_base *base,
79{ 79{
80 struct hrtimer *timer, tmp; 80 struct hrtimer *timer, tmp;
81 unsigned long next = 0, i; 81 unsigned long next = 0, i;
82 struct rb_node *curr; 82 struct timerqueue_node *curr;
83 unsigned long flags; 83 unsigned long flags;
84 84
85next_one: 85next_one:
86 i = 0; 86 i = 0;
87 raw_spin_lock_irqsave(&base->cpu_base->lock, flags); 87 raw_spin_lock_irqsave(&base->cpu_base->lock, flags);
88 88
89 curr = base->first; 89 curr = timerqueue_getnext(&base->active);
90 /* 90 /*
91 * Crude but we have to do this O(N*N) thing, because 91 * Crude but we have to do this O(N*N) thing, because
92 * we have to unlock the base when printing: 92 * we have to unlock the base when printing:
93 */ 93 */
94 while (curr && i < next) { 94 while (curr && i < next) {
95 curr = rb_next(curr); 95 curr = timerqueue_iterate_next(curr);
96 i++; 96 i++;
97 } 97 }
98 98
99 if (curr) { 99 if (curr) {
100 100
101 timer = rb_entry(curr, struct hrtimer, node); 101 timer = container_of(curr, struct hrtimer, node);
102 tmp = *timer; 102 tmp = *timer;
103 raw_spin_unlock_irqrestore(&base->cpu_base->lock, flags); 103 raw_spin_unlock_irqrestore(&base->cpu_base->lock, flags);
104 104
@@ -112,7 +112,7 @@ next_one:
112static void 112static void
113print_base(struct seq_file *m, struct hrtimer_clock_base *base, u64 now) 113print_base(struct seq_file *m, struct hrtimer_clock_base *base, u64 now)
114{ 114{
115 SEQ_printf(m, " .base: %p\n", base); 115 SEQ_printf(m, " .base: %pK\n", base);
116 SEQ_printf(m, " .index: %d\n", 116 SEQ_printf(m, " .index: %d\n",
117 base->index); 117 base->index);
118 SEQ_printf(m, " .resolution: %Lu nsecs\n", 118 SEQ_printf(m, " .resolution: %Lu nsecs\n",
diff --git a/kernel/time/timer_stats.c b/kernel/time/timer_stats.c
index 2f3b585b8d7d..a5d0a3a85dd8 100644
--- a/kernel/time/timer_stats.c
+++ b/kernel/time/timer_stats.c
@@ -236,7 +236,7 @@ void timer_stats_update_stats(void *timer, pid_t pid, void *startf,
236 unsigned int timer_flag) 236 unsigned int timer_flag)
237{ 237{
238 /* 238 /*
239 * It doesnt matter which lock we take: 239 * It doesn't matter which lock we take:
240 */ 240 */
241 raw_spinlock_t *lock; 241 raw_spinlock_t *lock;
242 struct entry *entry, input; 242 struct entry *entry, input;