2 * Time of day based timer functions.
5 * Copyright IBM Corp. 1999, 2008
6 * Author(s): Hartmut Penner (hp@de.ibm.com),
7 * Martin Schwidefsky (schwidefsky@de.ibm.com),
8 * Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
10 * Derived from "arch/i386/kernel/time.c"
11 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
14 #define KMSG_COMPONENT "time"
15 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17 #include <linux/kernel_stat.h>
18 #include <linux/errno.h>
19 #include <linux/module.h>
20 #include <linux/sched.h>
21 #include <linux/kernel.h>
22 #include <linux/param.h>
23 #include <linux/string.h>
25 #include <linux/interrupt.h>
26 #include <linux/cpu.h>
27 #include <linux/stop_machine.h>
28 #include <linux/time.h>
29 #include <linux/device.h>
30 #include <linux/delay.h>
31 #include <linux/init.h>
32 #include <linux/smp.h>
33 #include <linux/types.h>
34 #include <linux/profile.h>
35 #include <linux/timex.h>
36 #include <linux/notifier.h>
37 #include <linux/timekeeper_internal.h>
38 #include <linux/clockchips.h>
39 #include <linux/gfp.h>
40 #include <linux/kprobes.h>
41 #include <asm/uaccess.h>
42 #include <asm/facility.h>
43 #include <asm/delay.h>
44 #include <asm/div64.h>
47 #include <asm/irq_regs.h>
48 #include <asm/vtimer.h>
53 /* change this if you have some constant time drift */
54 #define USECS_PER_JIFFY ((unsigned long) 1000000/HZ)
55 #define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)
57 u64 sched_clock_base_cc = -1; /* Force to data section. */
58 EXPORT_SYMBOL_GPL(sched_clock_base_cc);
60 static DEFINE_PER_CPU(struct clock_event_device, comparators);
62 ATOMIC_NOTIFIER_HEAD(s390_epoch_delta_notifier);
63 EXPORT_SYMBOL(s390_epoch_delta_notifier);
65 unsigned char ptff_function_mask[16];
66 unsigned long lpar_offset;
67 unsigned long initial_leap_seconds;
70 * Get time offsets with PTFF
72 void __init ptff_init(void)
77 if (!test_facility(28))
79 ptff(&ptff_function_mask, sizeof(ptff_function_mask), PTFF_QAF);
82 if (ptff_query(PTFF_QTO) && ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
83 lpar_offset = qto.tod_epoch_difference;
85 /* get initial leap seconds */
86 if (ptff_query(PTFF_QUI) && ptff(&qui, sizeof(qui), PTFF_QUI) == 0)
87 initial_leap_seconds = (unsigned long)
88 ((long) qui.old_leap * 4096000000L);
92 * Scheduler clock - returns current time in nanosec units.
94 unsigned long long notrace sched_clock(void)
96 return tod_to_ns(get_tod_clock_monotonic());
98 NOKPROBE_SYMBOL(sched_clock);
101 * Monotonic_clock - returns # of nanoseconds passed since time_init()
103 unsigned long long monotonic_clock(void)
105 return sched_clock();
107 EXPORT_SYMBOL(monotonic_clock);
109 void tod_to_timeval(__u64 todval, struct timespec64 *xt)
111 unsigned long long sec;
114 do_div(sec, 1000000);
116 todval -= (sec * 1000000) << 12;
117 xt->tv_nsec = ((todval * 1000) >> 12);
119 EXPORT_SYMBOL(tod_to_timeval);
121 void clock_comparator_work(void)
123 struct clock_event_device *cd;
125 S390_lowcore.clock_comparator = -1ULL;
126 cd = this_cpu_ptr(&comparators);
127 cd->event_handler(cd);
131 * Fixup the clock comparator.
133 static void fixup_clock_comparator(unsigned long long delta)
135 /* If nobody is waiting there's nothing to fix. */
136 if (S390_lowcore.clock_comparator == -1ULL)
138 S390_lowcore.clock_comparator += delta;
139 set_clock_comparator(S390_lowcore.clock_comparator);
142 static int s390_next_event(unsigned long delta,
143 struct clock_event_device *evt)
145 S390_lowcore.clock_comparator = get_tod_clock() + delta;
146 set_clock_comparator(S390_lowcore.clock_comparator);
151 * Set up lowcore and control register of the current cpu to
152 * enable TOD clock and clock comparator interrupts.
154 void init_cpu_timer(void)
156 struct clock_event_device *cd;
159 S390_lowcore.clock_comparator = -1ULL;
160 set_clock_comparator(S390_lowcore.clock_comparator);
162 cpu = smp_processor_id();
163 cd = &per_cpu(comparators, cpu);
164 cd->name = "comparator";
165 cd->features = CLOCK_EVT_FEAT_ONESHOT;
168 cd->min_delta_ns = 1;
169 cd->max_delta_ns = LONG_MAX;
171 cd->cpumask = cpumask_of(cpu);
172 cd->set_next_event = s390_next_event;
174 clockevents_register_device(cd);
176 /* Enable clock comparator timer interrupt. */
179 /* Always allow the timing alert external interrupt. */
183 static void clock_comparator_interrupt(struct ext_code ext_code,
184 unsigned int param32,
185 unsigned long param64)
187 inc_irq_stat(IRQEXT_CLK);
188 if (S390_lowcore.clock_comparator == -1ULL)
189 set_clock_comparator(S390_lowcore.clock_comparator);
192 static void stp_timing_alert(struct stp_irq_parm *);
194 static void timing_alert_interrupt(struct ext_code ext_code,
195 unsigned int param32, unsigned long param64)
197 inc_irq_stat(IRQEXT_TLA);
198 if (param32 & 0x00038000)
199 stp_timing_alert((struct stp_irq_parm *) ¶m32);
202 static void stp_reset(void);
204 void read_persistent_clock64(struct timespec64 *ts)
208 clock = get_tod_clock() - initial_leap_seconds;
209 tod_to_timeval(clock - TOD_UNIX_EPOCH, ts);
212 void read_boot_clock64(struct timespec64 *ts)
216 clock = sched_clock_base_cc - initial_leap_seconds;
217 tod_to_timeval(clock - TOD_UNIX_EPOCH, ts);
220 static cycle_t read_tod_clock(struct clocksource *cs)
222 return get_tod_clock();
225 static struct clocksource clocksource_tod = {
228 .read = read_tod_clock,
232 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
235 struct clocksource * __init clocksource_default_clock(void)
237 return &clocksource_tod;
240 void update_vsyscall(struct timekeeper *tk)
244 if (tk->tkr_mono.clock != &clocksource_tod)
247 /* Make userspace gettimeofday spin until we're done. */
248 ++vdso_data->tb_update_count;
250 vdso_data->xtime_tod_stamp = tk->tkr_mono.cycle_last;
251 vdso_data->xtime_clock_sec = tk->xtime_sec;
252 vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec;
253 vdso_data->wtom_clock_sec =
254 tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
255 vdso_data->wtom_clock_nsec = tk->tkr_mono.xtime_nsec +
256 + ((u64) tk->wall_to_monotonic.tv_nsec << tk->tkr_mono.shift);
257 nsecps = (u64) NSEC_PER_SEC << tk->tkr_mono.shift;
258 while (vdso_data->wtom_clock_nsec >= nsecps) {
259 vdso_data->wtom_clock_nsec -= nsecps;
260 vdso_data->wtom_clock_sec++;
263 vdso_data->xtime_coarse_sec = tk->xtime_sec;
264 vdso_data->xtime_coarse_nsec =
265 (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
266 vdso_data->wtom_coarse_sec =
267 vdso_data->xtime_coarse_sec + tk->wall_to_monotonic.tv_sec;
268 vdso_data->wtom_coarse_nsec =
269 vdso_data->xtime_coarse_nsec + tk->wall_to_monotonic.tv_nsec;
270 while (vdso_data->wtom_coarse_nsec >= NSEC_PER_SEC) {
271 vdso_data->wtom_coarse_nsec -= NSEC_PER_SEC;
272 vdso_data->wtom_coarse_sec++;
275 vdso_data->tk_mult = tk->tkr_mono.mult;
276 vdso_data->tk_shift = tk->tkr_mono.shift;
278 ++vdso_data->tb_update_count;
281 extern struct timezone sys_tz;
283 void update_vsyscall_tz(void)
285 /* Make userspace gettimeofday spin until we're done. */
286 ++vdso_data->tb_update_count;
288 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
289 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
291 ++vdso_data->tb_update_count;
295 * Initialize the TOD clock and the CPU timer of
298 void __init time_init(void)
300 /* Reset time synchronization interfaces. */
303 /* request the clock comparator external interrupt */
304 if (register_external_irq(EXT_IRQ_CLK_COMP, clock_comparator_interrupt))
305 panic("Couldn't request external interrupt 0x1004");
307 /* request the timing alert external interrupt */
308 if (register_external_irq(EXT_IRQ_TIMING_ALERT, timing_alert_interrupt))
309 panic("Couldn't request external interrupt 0x1406");
311 if (__clocksource_register(&clocksource_tod) != 0)
312 panic("Could not register TOD clock source");
314 /* Enable TOD clock interrupts on the boot cpu. */
317 /* Enable cpu timer interrupts on the boot cpu. */
322 * The time is "clock". old is what we think the time is.
323 * Adjust the value by a multiple of jiffies and add the delta to ntp.
324 * "delay" is an approximation how long the synchronization took. If
325 * the time correction is positive, then "delay" is subtracted from
326 * the time difference and only the remaining part is passed to ntp.
328 static unsigned long long adjust_time(unsigned long long old,
329 unsigned long long clock,
330 unsigned long long delay)
332 unsigned long long delta, ticks;
336 /* It is later than we thought. */
337 delta = ticks = clock - old;
338 delta = ticks = (delta < delay) ? 0 : delta - delay;
339 delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
340 adjust.offset = ticks * (1000000 / HZ);
342 /* It is earlier than we thought. */
343 delta = ticks = old - clock;
344 delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
346 adjust.offset = -ticks * (1000000 / HZ);
348 sched_clock_base_cc += delta;
349 if (adjust.offset != 0) {
350 pr_notice("The ETR interface has adjusted the clock "
351 "by %li microseconds\n", adjust.offset);
352 adjust.modes = ADJ_OFFSET_SINGLESHOT;
353 do_adjtimex(&adjust);
358 static DEFINE_PER_CPU(atomic_t, clock_sync_word);
359 static DEFINE_MUTEX(clock_sync_mutex);
360 static unsigned long clock_sync_flags;
362 #define CLOCK_SYNC_HAS_ETR 0
363 #define CLOCK_SYNC_HAS_STP 1
364 #define CLOCK_SYNC_ETR 2
365 #define CLOCK_SYNC_STP 3
368 * The get_clock function for the physical clock. It will get the current
369 * TOD clock, subtract the LPAR offset and write the result to *clock.
370 * The function returns 0 if the clock is in sync with the external time
371 * source. If the clock mode is local it will return -EOPNOTSUPP and
372 * -EAGAIN if the clock is not in sync with the external reference.
374 int get_phys_clock(unsigned long long *clock)
377 unsigned int sw0, sw1;
379 sw_ptr = &get_cpu_var(clock_sync_word);
380 sw0 = atomic_read(sw_ptr);
381 *clock = get_tod_clock() - lpar_offset;
382 sw1 = atomic_read(sw_ptr);
383 put_cpu_var(clock_sync_word);
384 if (sw0 == sw1 && (sw0 & 0x80000000U))
385 /* Success: time is in sync. */
387 if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags) &&
388 !test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
390 if (!test_bit(CLOCK_SYNC_ETR, &clock_sync_flags) &&
391 !test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
395 EXPORT_SYMBOL(get_phys_clock);
398 * Make get_sync_clock return -EAGAIN.
400 static void disable_sync_clock(void *dummy)
402 atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
404 * Clear the in-sync bit 2^31. All get_sync_clock calls will
405 * fail until the sync bit is turned back on. In addition
406 * increase the "sequence" counter to avoid the race of an
407 * etr event and the complete recovery against get_sync_clock.
409 atomic_andnot(0x80000000, sw_ptr);
414 * Make get_sync_clock return 0 again.
415 * Needs to be called from a context disabled for preemption.
417 static void enable_sync_clock(void)
419 atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
420 atomic_or(0x80000000, sw_ptr);
424 * Function to check if the clock is in sync.
426 static inline int check_sync_clock(void)
431 sw_ptr = &get_cpu_var(clock_sync_word);
432 rc = (atomic_read(sw_ptr) & 0x80000000U) != 0;
433 put_cpu_var(clock_sync_word);
437 /* Single threaded workqueue used for etr and stp sync events */
438 static struct workqueue_struct *time_sync_wq;
440 static void __init time_init_wq(void)
444 time_sync_wq = create_singlethread_workqueue("timesync");
447 struct clock_sync_data {
450 unsigned long long fixup_cc;
452 struct etr_aib *etr_aib;
455 static void clock_sync_cpu(struct clock_sync_data *sync)
457 atomic_dec(&sync->cpus);
460 * This looks like a busy wait loop but it isn't. etr_sync_cpus
461 * is called on all other cpus while the TOD clocks is stopped.
462 * __udelay will stop the cpu on an enabled wait psw until the
463 * TOD is running again.
465 while (sync->in_sync == 0) {
468 * A different cpu changes *in_sync. Therefore use
469 * barrier() to force memory access.
473 if (sync->in_sync != 1)
474 /* Didn't work. Clear per-cpu in sync bit again. */
475 disable_sync_clock(NULL);
477 * This round of TOD syncing is done. Set the clock comparator
478 * to the next tick and let the processor continue.
480 fixup_clock_comparator(sync->fixup_cc);
484 * Server Time Protocol (STP) code.
486 static bool stp_online;
487 static struct stp_sstpi stp_info;
488 static void *stp_page;
490 static void stp_work_fn(struct work_struct *work);
491 static DEFINE_MUTEX(stp_work_mutex);
492 static DECLARE_WORK(stp_work, stp_work_fn);
493 static struct timer_list stp_timer;
495 static int __init early_parse_stp(char *p)
497 return kstrtobool(p, &stp_online);
499 early_param("stp", early_parse_stp);
502 * Reset STP attachment.
504 static void __init stp_reset(void)
508 stp_page = (void *) get_zeroed_page(GFP_ATOMIC);
509 rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
511 set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
512 else if (stp_online) {
513 pr_warn("The real or virtual hardware system does not provide an STP interface\n");
514 free_page((unsigned long) stp_page);
520 static void stp_timeout(unsigned long dummy)
522 queue_work(time_sync_wq, &stp_work);
525 static int __init stp_init(void)
527 if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
529 setup_timer(&stp_timer, stp_timeout, 0UL);
533 queue_work(time_sync_wq, &stp_work);
537 arch_initcall(stp_init);
540 * STP timing alert. There are three causes:
541 * 1) timing status change
542 * 2) link availability change
543 * 3) time control parameter change
544 * In all three cases we are only interested in the clock source state.
545 * If a STP clock source is now available use it.
547 static void stp_timing_alert(struct stp_irq_parm *intparm)
549 if (intparm->tsc || intparm->lac || intparm->tcpc)
550 queue_work(time_sync_wq, &stp_work);
554 * STP sync check machine check. This is called when the timing state
555 * changes from the synchronized state to the unsynchronized state.
556 * After a STP sync check the clock is not in sync. The machine check
557 * is broadcasted to all cpus at the same time.
559 int stp_sync_check(void)
561 disable_sync_clock(NULL);
566 * STP island condition machine check. This is called when an attached
567 * server attempts to communicate over an STP link and the servers
568 * have matching CTN ids and have a valid stratum-1 configuration
569 * but the configurations do not match.
571 int stp_island_check(void)
573 disable_sync_clock(NULL);
577 void stp_queue_work(void)
579 queue_work(time_sync_wq, &stp_work);
582 static int stp_sync_clock(void *data)
585 unsigned long long old_clock, delta, new_clock, clock_delta;
586 struct clock_sync_data *stp_sync;
592 if (xchg(&first, 1) == 1) {
594 clock_sync_cpu(stp_sync);
598 /* Wait until all other cpus entered the sync function. */
599 while (atomic_read(&stp_sync->cpus) != 0)
605 if (stp_info.todoff[0] || stp_info.todoff[1] ||
606 stp_info.todoff[2] || stp_info.todoff[3] ||
608 old_clock = get_tod_clock();
609 rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0, &clock_delta);
611 new_clock = old_clock + clock_delta;
612 delta = adjust_time(old_clock, new_clock, 0);
613 if (ptff_query(PTFF_QTO) &&
614 ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
615 /* Update LPAR offset */
616 lpar_offset = qto.tod_epoch_difference;
617 atomic_notifier_call_chain(&s390_epoch_delta_notifier,
619 fixup_clock_comparator(delta);
620 rc = chsc_sstpi(stp_page, &stp_info,
621 sizeof(struct stp_sstpi));
622 if (rc == 0 && stp_info.tmd != 2)
627 disable_sync_clock(NULL);
628 stp_sync->in_sync = -EAGAIN;
630 stp_sync->in_sync = 1;
636 * STP work. Check for the STP state and take over the clock
637 * synchronization if the STP clock source is usable.
639 static void stp_work_fn(struct work_struct *work)
641 struct clock_sync_data stp_sync;
644 /* prevent multiple execution. */
645 mutex_lock(&stp_work_mutex);
648 chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
649 del_timer_sync(&stp_timer);
653 rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xb0e0, NULL);
657 rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
658 if (rc || stp_info.c == 0)
661 /* Skip synchronization if the clock is already in sync. */
662 if (check_sync_clock())
665 memset(&stp_sync, 0, sizeof(stp_sync));
667 atomic_set(&stp_sync.cpus, num_online_cpus() - 1);
668 stop_machine(stp_sync_clock, &stp_sync, cpu_online_mask);
671 if (!check_sync_clock())
673 * There is a usable clock but the synchonization failed.
674 * Retry after a second.
676 mod_timer(&stp_timer, jiffies + HZ);
679 mutex_unlock(&stp_work_mutex);
683 * STP subsys sysfs interface functions
685 static struct bus_type stp_subsys = {
690 static ssize_t stp_ctn_id_show(struct device *dev,
691 struct device_attribute *attr,
696 return sprintf(buf, "%016llx\n",
697 *(unsigned long long *) stp_info.ctnid);
700 static DEVICE_ATTR(ctn_id, 0400, stp_ctn_id_show, NULL);
702 static ssize_t stp_ctn_type_show(struct device *dev,
703 struct device_attribute *attr,
708 return sprintf(buf, "%i\n", stp_info.ctn);
711 static DEVICE_ATTR(ctn_type, 0400, stp_ctn_type_show, NULL);
713 static ssize_t stp_dst_offset_show(struct device *dev,
714 struct device_attribute *attr,
717 if (!stp_online || !(stp_info.vbits & 0x2000))
719 return sprintf(buf, "%i\n", (int)(s16) stp_info.dsto);
722 static DEVICE_ATTR(dst_offset, 0400, stp_dst_offset_show, NULL);
724 static ssize_t stp_leap_seconds_show(struct device *dev,
725 struct device_attribute *attr,
728 if (!stp_online || !(stp_info.vbits & 0x8000))
730 return sprintf(buf, "%i\n", (int)(s16) stp_info.leaps);
733 static DEVICE_ATTR(leap_seconds, 0400, stp_leap_seconds_show, NULL);
735 static ssize_t stp_stratum_show(struct device *dev,
736 struct device_attribute *attr,
741 return sprintf(buf, "%i\n", (int)(s16) stp_info.stratum);
744 static DEVICE_ATTR(stratum, 0400, stp_stratum_show, NULL);
746 static ssize_t stp_time_offset_show(struct device *dev,
747 struct device_attribute *attr,
750 if (!stp_online || !(stp_info.vbits & 0x0800))
752 return sprintf(buf, "%i\n", (int) stp_info.tto);
755 static DEVICE_ATTR(time_offset, 0400, stp_time_offset_show, NULL);
757 static ssize_t stp_time_zone_offset_show(struct device *dev,
758 struct device_attribute *attr,
761 if (!stp_online || !(stp_info.vbits & 0x4000))
763 return sprintf(buf, "%i\n", (int)(s16) stp_info.tzo);
766 static DEVICE_ATTR(time_zone_offset, 0400,
767 stp_time_zone_offset_show, NULL);
769 static ssize_t stp_timing_mode_show(struct device *dev,
770 struct device_attribute *attr,
775 return sprintf(buf, "%i\n", stp_info.tmd);
778 static DEVICE_ATTR(timing_mode, 0400, stp_timing_mode_show, NULL);
780 static ssize_t stp_timing_state_show(struct device *dev,
781 struct device_attribute *attr,
786 return sprintf(buf, "%i\n", stp_info.tst);
789 static DEVICE_ATTR(timing_state, 0400, stp_timing_state_show, NULL);
791 static ssize_t stp_online_show(struct device *dev,
792 struct device_attribute *attr,
795 return sprintf(buf, "%i\n", stp_online);
798 static ssize_t stp_online_store(struct device *dev,
799 struct device_attribute *attr,
800 const char *buf, size_t count)
804 value = simple_strtoul(buf, NULL, 0);
805 if (value != 0 && value != 1)
807 if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
809 mutex_lock(&clock_sync_mutex);
812 set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
814 clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
815 queue_work(time_sync_wq, &stp_work);
816 mutex_unlock(&clock_sync_mutex);
821 * Can't use DEVICE_ATTR because the attribute should be named
822 * stp/online but dev_attr_online already exists in this file ..
824 static struct device_attribute dev_attr_stp_online = {
825 .attr = { .name = "online", .mode = 0600 },
826 .show = stp_online_show,
827 .store = stp_online_store,
830 static struct device_attribute *stp_attributes[] = {
833 &dev_attr_dst_offset,
834 &dev_attr_leap_seconds,
835 &dev_attr_stp_online,
837 &dev_attr_time_offset,
838 &dev_attr_time_zone_offset,
839 &dev_attr_timing_mode,
840 &dev_attr_timing_state,
844 static int __init stp_init_sysfs(void)
846 struct device_attribute **attr;
849 rc = subsys_system_register(&stp_subsys, NULL);
852 for (attr = stp_attributes; *attr; attr++) {
853 rc = device_create_file(stp_subsys.dev_root, *attr);
859 for (; attr >= stp_attributes; attr--)
860 device_remove_file(stp_subsys.dev_root, *attr);
861 bus_unregister(&stp_subsys);
866 device_initcall(stp_init_sysfs);