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 u64 sched_clock_base_cc = -1; /* Force to data section. */
54 EXPORT_SYMBOL_GPL(sched_clock_base_cc);
56 static DEFINE_PER_CPU(struct clock_event_device, comparators);
58 ATOMIC_NOTIFIER_HEAD(s390_epoch_delta_notifier);
59 EXPORT_SYMBOL(s390_epoch_delta_notifier);
61 unsigned char ptff_function_mask[16];
62 unsigned long lpar_offset;
63 unsigned long initial_leap_seconds;
66 * Get time offsets with PTFF
68 void __init ptff_init(void)
73 if (!test_facility(28))
75 ptff(&ptff_function_mask, sizeof(ptff_function_mask), PTFF_QAF);
78 if (ptff_query(PTFF_QTO) && ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
79 lpar_offset = qto.tod_epoch_difference;
81 /* get initial leap seconds */
82 if (ptff_query(PTFF_QUI) && ptff(&qui, sizeof(qui), PTFF_QUI) == 0)
83 initial_leap_seconds = (unsigned long)
84 ((long) qui.old_leap * 4096000000L);
88 * Scheduler clock - returns current time in nanosec units.
90 unsigned long long notrace sched_clock(void)
92 return tod_to_ns(get_tod_clock_monotonic());
94 NOKPROBE_SYMBOL(sched_clock);
97 * Monotonic_clock - returns # of nanoseconds passed since time_init()
99 unsigned long long monotonic_clock(void)
101 return sched_clock();
103 EXPORT_SYMBOL(monotonic_clock);
105 void tod_to_timeval(__u64 todval, struct timespec64 *xt)
107 unsigned long long sec;
110 do_div(sec, 1000000);
112 todval -= (sec * 1000000) << 12;
113 xt->tv_nsec = ((todval * 1000) >> 12);
115 EXPORT_SYMBOL(tod_to_timeval);
117 void clock_comparator_work(void)
119 struct clock_event_device *cd;
121 S390_lowcore.clock_comparator = -1ULL;
122 cd = this_cpu_ptr(&comparators);
123 cd->event_handler(cd);
127 * Fixup the clock comparator.
129 static void fixup_clock_comparator(unsigned long long delta)
131 /* If nobody is waiting there's nothing to fix. */
132 if (S390_lowcore.clock_comparator == -1ULL)
134 S390_lowcore.clock_comparator += delta;
135 set_clock_comparator(S390_lowcore.clock_comparator);
138 static int s390_next_event(unsigned long delta,
139 struct clock_event_device *evt)
141 S390_lowcore.clock_comparator = get_tod_clock() + delta;
142 set_clock_comparator(S390_lowcore.clock_comparator);
147 * Set up lowcore and control register of the current cpu to
148 * enable TOD clock and clock comparator interrupts.
150 void init_cpu_timer(void)
152 struct clock_event_device *cd;
155 S390_lowcore.clock_comparator = -1ULL;
156 set_clock_comparator(S390_lowcore.clock_comparator);
158 cpu = smp_processor_id();
159 cd = &per_cpu(comparators, cpu);
160 cd->name = "comparator";
161 cd->features = CLOCK_EVT_FEAT_ONESHOT;
164 cd->min_delta_ns = 1;
165 cd->max_delta_ns = LONG_MAX;
167 cd->cpumask = cpumask_of(cpu);
168 cd->set_next_event = s390_next_event;
170 clockevents_register_device(cd);
172 /* Enable clock comparator timer interrupt. */
175 /* Always allow the timing alert external interrupt. */
179 static void clock_comparator_interrupt(struct ext_code ext_code,
180 unsigned int param32,
181 unsigned long param64)
183 inc_irq_stat(IRQEXT_CLK);
184 if (S390_lowcore.clock_comparator == -1ULL)
185 set_clock_comparator(S390_lowcore.clock_comparator);
188 static void stp_timing_alert(struct stp_irq_parm *);
190 static void timing_alert_interrupt(struct ext_code ext_code,
191 unsigned int param32, unsigned long param64)
193 inc_irq_stat(IRQEXT_TLA);
194 if (param32 & 0x00038000)
195 stp_timing_alert((struct stp_irq_parm *) ¶m32);
198 static void stp_reset(void);
200 void read_persistent_clock64(struct timespec64 *ts)
204 clock = get_tod_clock() - initial_leap_seconds;
205 tod_to_timeval(clock - TOD_UNIX_EPOCH, ts);
208 void read_boot_clock64(struct timespec64 *ts)
212 clock = sched_clock_base_cc - initial_leap_seconds;
213 tod_to_timeval(clock - TOD_UNIX_EPOCH, ts);
216 static cycle_t read_tod_clock(struct clocksource *cs)
218 return get_tod_clock();
221 static struct clocksource clocksource_tod = {
224 .read = read_tod_clock,
228 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
231 struct clocksource * __init clocksource_default_clock(void)
233 return &clocksource_tod;
236 void update_vsyscall(struct timekeeper *tk)
240 if (tk->tkr_mono.clock != &clocksource_tod)
243 /* Make userspace gettimeofday spin until we're done. */
244 ++vdso_data->tb_update_count;
246 vdso_data->xtime_tod_stamp = tk->tkr_mono.cycle_last;
247 vdso_data->xtime_clock_sec = tk->xtime_sec;
248 vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec;
249 vdso_data->wtom_clock_sec =
250 tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
251 vdso_data->wtom_clock_nsec = tk->tkr_mono.xtime_nsec +
252 + ((u64) tk->wall_to_monotonic.tv_nsec << tk->tkr_mono.shift);
253 nsecps = (u64) NSEC_PER_SEC << tk->tkr_mono.shift;
254 while (vdso_data->wtom_clock_nsec >= nsecps) {
255 vdso_data->wtom_clock_nsec -= nsecps;
256 vdso_data->wtom_clock_sec++;
259 vdso_data->xtime_coarse_sec = tk->xtime_sec;
260 vdso_data->xtime_coarse_nsec =
261 (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
262 vdso_data->wtom_coarse_sec =
263 vdso_data->xtime_coarse_sec + tk->wall_to_monotonic.tv_sec;
264 vdso_data->wtom_coarse_nsec =
265 vdso_data->xtime_coarse_nsec + tk->wall_to_monotonic.tv_nsec;
266 while (vdso_data->wtom_coarse_nsec >= NSEC_PER_SEC) {
267 vdso_data->wtom_coarse_nsec -= NSEC_PER_SEC;
268 vdso_data->wtom_coarse_sec++;
271 vdso_data->tk_mult = tk->tkr_mono.mult;
272 vdso_data->tk_shift = tk->tkr_mono.shift;
274 ++vdso_data->tb_update_count;
277 extern struct timezone sys_tz;
279 void update_vsyscall_tz(void)
281 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
282 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
286 * Initialize the TOD clock and the CPU timer of
289 void __init time_init(void)
291 /* Reset time synchronization interfaces. */
294 /* request the clock comparator external interrupt */
295 if (register_external_irq(EXT_IRQ_CLK_COMP, clock_comparator_interrupt))
296 panic("Couldn't request external interrupt 0x1004");
298 /* request the timing alert external interrupt */
299 if (register_external_irq(EXT_IRQ_TIMING_ALERT, timing_alert_interrupt))
300 panic("Couldn't request external interrupt 0x1406");
302 if (__clocksource_register(&clocksource_tod) != 0)
303 panic("Could not register TOD clock source");
305 /* Enable TOD clock interrupts on the boot cpu. */
308 /* Enable cpu timer interrupts on the boot cpu. */
312 static DEFINE_PER_CPU(atomic_t, clock_sync_word);
313 static DEFINE_MUTEX(clock_sync_mutex);
314 static unsigned long clock_sync_flags;
316 #define CLOCK_SYNC_HAS_STP 0
317 #define CLOCK_SYNC_STP 1
320 * The get_clock function for the physical clock. It will get the current
321 * TOD clock, subtract the LPAR offset and write the result to *clock.
322 * The function returns 0 if the clock is in sync with the external time
323 * source. If the clock mode is local it will return -EOPNOTSUPP and
324 * -EAGAIN if the clock is not in sync with the external reference.
326 int get_phys_clock(unsigned long long *clock)
329 unsigned int sw0, sw1;
331 sw_ptr = &get_cpu_var(clock_sync_word);
332 sw0 = atomic_read(sw_ptr);
333 *clock = get_tod_clock() - lpar_offset;
334 sw1 = atomic_read(sw_ptr);
335 put_cpu_var(clock_sync_word);
336 if (sw0 == sw1 && (sw0 & 0x80000000U))
337 /* Success: time is in sync. */
339 if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
341 if (!test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
345 EXPORT_SYMBOL(get_phys_clock);
348 * Make get_phys_clock() return -EAGAIN.
350 static void disable_sync_clock(void *dummy)
352 atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
354 * Clear the in-sync bit 2^31. All get_phys_clock calls will
355 * fail until the sync bit is turned back on. In addition
356 * increase the "sequence" counter to avoid the race of an
357 * stp event and the complete recovery against get_phys_clock.
359 atomic_andnot(0x80000000, sw_ptr);
364 * Make get_phys_clock() return 0 again.
365 * Needs to be called from a context disabled for preemption.
367 static void enable_sync_clock(void)
369 atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
370 atomic_or(0x80000000, sw_ptr);
374 * Function to check if the clock is in sync.
376 static inline int check_sync_clock(void)
381 sw_ptr = &get_cpu_var(clock_sync_word);
382 rc = (atomic_read(sw_ptr) & 0x80000000U) != 0;
383 put_cpu_var(clock_sync_word);
387 /* Single threaded workqueue used for stp sync events */
388 static struct workqueue_struct *time_sync_wq;
390 static void __init time_init_wq(void)
394 time_sync_wq = create_singlethread_workqueue("timesync");
397 struct clock_sync_data {
400 unsigned long long fixup_cc;
403 static void clock_sync_cpu(struct clock_sync_data *sync)
405 atomic_dec(&sync->cpus);
407 while (sync->in_sync == 0) {
410 * A different cpu changes *in_sync. Therefore use
411 * barrier() to force memory access.
415 if (sync->in_sync != 1)
416 /* Didn't work. Clear per-cpu in sync bit again. */
417 disable_sync_clock(NULL);
419 * This round of TOD syncing is done. Set the clock comparator
420 * to the next tick and let the processor continue.
422 fixup_clock_comparator(sync->fixup_cc);
426 * Server Time Protocol (STP) code.
428 static bool stp_online;
429 static struct stp_sstpi stp_info;
430 static void *stp_page;
432 static void stp_work_fn(struct work_struct *work);
433 static DEFINE_MUTEX(stp_work_mutex);
434 static DECLARE_WORK(stp_work, stp_work_fn);
435 static struct timer_list stp_timer;
437 static int __init early_parse_stp(char *p)
439 return kstrtobool(p, &stp_online);
441 early_param("stp", early_parse_stp);
444 * Reset STP attachment.
446 static void __init stp_reset(void)
450 stp_page = (void *) get_zeroed_page(GFP_ATOMIC);
451 rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
453 set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
454 else if (stp_online) {
455 pr_warn("The real or virtual hardware system does not provide an STP interface\n");
456 free_page((unsigned long) stp_page);
462 static void stp_timeout(unsigned long dummy)
464 queue_work(time_sync_wq, &stp_work);
467 static int __init stp_init(void)
469 if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
471 setup_timer(&stp_timer, stp_timeout, 0UL);
475 queue_work(time_sync_wq, &stp_work);
479 arch_initcall(stp_init);
482 * STP timing alert. There are three causes:
483 * 1) timing status change
484 * 2) link availability change
485 * 3) time control parameter change
486 * In all three cases we are only interested in the clock source state.
487 * If a STP clock source is now available use it.
489 static void stp_timing_alert(struct stp_irq_parm *intparm)
491 if (intparm->tsc || intparm->lac || intparm->tcpc)
492 queue_work(time_sync_wq, &stp_work);
496 * STP sync check machine check. This is called when the timing state
497 * changes from the synchronized state to the unsynchronized state.
498 * After a STP sync check the clock is not in sync. The machine check
499 * is broadcasted to all cpus at the same time.
501 int stp_sync_check(void)
503 disable_sync_clock(NULL);
508 * STP island condition machine check. This is called when an attached
509 * server attempts to communicate over an STP link and the servers
510 * have matching CTN ids and have a valid stratum-1 configuration
511 * but the configurations do not match.
513 int stp_island_check(void)
515 disable_sync_clock(NULL);
519 void stp_queue_work(void)
521 queue_work(time_sync_wq, &stp_work);
524 static int stp_sync_clock(void *data)
527 unsigned long long clock_delta;
528 struct clock_sync_data *stp_sync;
534 if (xchg(&first, 1) == 1) {
536 clock_sync_cpu(stp_sync);
540 /* Wait until all other cpus entered the sync function. */
541 while (atomic_read(&stp_sync->cpus) != 0)
547 if (stp_info.todoff[0] || stp_info.todoff[1] ||
548 stp_info.todoff[2] || stp_info.todoff[3] ||
550 rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0, &clock_delta);
552 /* fixup the monotonic sched clock */
553 sched_clock_base_cc += clock_delta;
554 if (ptff_query(PTFF_QTO) &&
555 ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
556 /* Update LPAR offset */
557 lpar_offset = qto.tod_epoch_difference;
558 atomic_notifier_call_chain(&s390_epoch_delta_notifier,
560 stp_sync->fixup_cc = clock_delta;
561 fixup_clock_comparator(clock_delta);
562 rc = chsc_sstpi(stp_page, &stp_info,
563 sizeof(struct stp_sstpi));
564 if (rc == 0 && stp_info.tmd != 2)
569 disable_sync_clock(NULL);
570 stp_sync->in_sync = -EAGAIN;
572 stp_sync->in_sync = 1;
578 * STP work. Check for the STP state and take over the clock
579 * synchronization if the STP clock source is usable.
581 static void stp_work_fn(struct work_struct *work)
583 struct clock_sync_data stp_sync;
586 /* prevent multiple execution. */
587 mutex_lock(&stp_work_mutex);
590 chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
591 del_timer_sync(&stp_timer);
595 rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xb0e0, NULL);
599 rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
600 if (rc || stp_info.c == 0)
603 /* Skip synchronization if the clock is already in sync. */
604 if (check_sync_clock())
607 memset(&stp_sync, 0, sizeof(stp_sync));
609 atomic_set(&stp_sync.cpus, num_online_cpus() - 1);
610 stop_machine(stp_sync_clock, &stp_sync, cpu_online_mask);
613 if (!check_sync_clock())
615 * There is a usable clock but the synchonization failed.
616 * Retry after a second.
618 mod_timer(&stp_timer, jiffies + HZ);
621 mutex_unlock(&stp_work_mutex);
625 * STP subsys sysfs interface functions
627 static struct bus_type stp_subsys = {
632 static ssize_t stp_ctn_id_show(struct device *dev,
633 struct device_attribute *attr,
638 return sprintf(buf, "%016llx\n",
639 *(unsigned long long *) stp_info.ctnid);
642 static DEVICE_ATTR(ctn_id, 0400, stp_ctn_id_show, NULL);
644 static ssize_t stp_ctn_type_show(struct device *dev,
645 struct device_attribute *attr,
650 return sprintf(buf, "%i\n", stp_info.ctn);
653 static DEVICE_ATTR(ctn_type, 0400, stp_ctn_type_show, NULL);
655 static ssize_t stp_dst_offset_show(struct device *dev,
656 struct device_attribute *attr,
659 if (!stp_online || !(stp_info.vbits & 0x2000))
661 return sprintf(buf, "%i\n", (int)(s16) stp_info.dsto);
664 static DEVICE_ATTR(dst_offset, 0400, stp_dst_offset_show, NULL);
666 static ssize_t stp_leap_seconds_show(struct device *dev,
667 struct device_attribute *attr,
670 if (!stp_online || !(stp_info.vbits & 0x8000))
672 return sprintf(buf, "%i\n", (int)(s16) stp_info.leaps);
675 static DEVICE_ATTR(leap_seconds, 0400, stp_leap_seconds_show, NULL);
677 static ssize_t stp_stratum_show(struct device *dev,
678 struct device_attribute *attr,
683 return sprintf(buf, "%i\n", (int)(s16) stp_info.stratum);
686 static DEVICE_ATTR(stratum, 0400, stp_stratum_show, NULL);
688 static ssize_t stp_time_offset_show(struct device *dev,
689 struct device_attribute *attr,
692 if (!stp_online || !(stp_info.vbits & 0x0800))
694 return sprintf(buf, "%i\n", (int) stp_info.tto);
697 static DEVICE_ATTR(time_offset, 0400, stp_time_offset_show, NULL);
699 static ssize_t stp_time_zone_offset_show(struct device *dev,
700 struct device_attribute *attr,
703 if (!stp_online || !(stp_info.vbits & 0x4000))
705 return sprintf(buf, "%i\n", (int)(s16) stp_info.tzo);
708 static DEVICE_ATTR(time_zone_offset, 0400,
709 stp_time_zone_offset_show, NULL);
711 static ssize_t stp_timing_mode_show(struct device *dev,
712 struct device_attribute *attr,
717 return sprintf(buf, "%i\n", stp_info.tmd);
720 static DEVICE_ATTR(timing_mode, 0400, stp_timing_mode_show, NULL);
722 static ssize_t stp_timing_state_show(struct device *dev,
723 struct device_attribute *attr,
728 return sprintf(buf, "%i\n", stp_info.tst);
731 static DEVICE_ATTR(timing_state, 0400, stp_timing_state_show, NULL);
733 static ssize_t stp_online_show(struct device *dev,
734 struct device_attribute *attr,
737 return sprintf(buf, "%i\n", stp_online);
740 static ssize_t stp_online_store(struct device *dev,
741 struct device_attribute *attr,
742 const char *buf, size_t count)
746 value = simple_strtoul(buf, NULL, 0);
747 if (value != 0 && value != 1)
749 if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
751 mutex_lock(&clock_sync_mutex);
754 set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
756 clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
757 queue_work(time_sync_wq, &stp_work);
758 mutex_unlock(&clock_sync_mutex);
763 * Can't use DEVICE_ATTR because the attribute should be named
764 * stp/online but dev_attr_online already exists in this file ..
766 static struct device_attribute dev_attr_stp_online = {
767 .attr = { .name = "online", .mode = 0600 },
768 .show = stp_online_show,
769 .store = stp_online_store,
772 static struct device_attribute *stp_attributes[] = {
775 &dev_attr_dst_offset,
776 &dev_attr_leap_seconds,
777 &dev_attr_stp_online,
779 &dev_attr_time_offset,
780 &dev_attr_time_zone_offset,
781 &dev_attr_timing_mode,
782 &dev_attr_timing_state,
786 static int __init stp_init_sysfs(void)
788 struct device_attribute **attr;
791 rc = subsys_system_register(&stp_subsys, NULL);
794 for (attr = stp_attributes; *attr; attr++) {
795 rc = device_create_file(stp_subsys.dev_root, *attr);
801 for (; attr >= stp_attributes; attr--)
802 device_remove_file(stp_subsys.dev_root, *attr);
803 bus_unregister(&stp_subsys);
808 device_initcall(stp_init_sysfs);