xref: /linux/arch/s390/kernel/time.c (revision a514e6f8f5caa24413731bed54b322bd34d918dd)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Time of day based timer functions.
4  *
5  *  S390 version
6  *    Copyright IBM Corp. 1999, 2008
7  *    Author(s): Hartmut Penner (hp@de.ibm.com),
8  *               Martin Schwidefsky (schwidefsky@de.ibm.com),
9  *               Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
10  *
11  *  Derived from "arch/i386/kernel/time.c"
12  *    Copyright (C) 1991, 1992, 1995  Linus Torvalds
13  */
14 
15 #define KMSG_COMPONENT "time"
16 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17 
18 #include <linux/kernel_stat.h>
19 #include <linux/errno.h>
20 #include <linux/export.h>
21 #include <linux/sched.h>
22 #include <linux/sched/clock.h>
23 #include <linux/kernel.h>
24 #include <linux/param.h>
25 #include <linux/string.h>
26 #include <linux/mm.h>
27 #include <linux/interrupt.h>
28 #include <linux/cpu.h>
29 #include <linux/stop_machine.h>
30 #include <linux/time.h>
31 #include <linux/device.h>
32 #include <linux/delay.h>
33 #include <linux/init.h>
34 #include <linux/smp.h>
35 #include <linux/types.h>
36 #include <linux/profile.h>
37 #include <linux/timex.h>
38 #include <linux/notifier.h>
39 #include <linux/clockchips.h>
40 #include <linux/gfp.h>
41 #include <linux/kprobes.h>
42 #include <linux/uaccess.h>
43 #include <vdso/vsyscall.h>
44 #include <vdso/clocksource.h>
45 #include <vdso/helpers.h>
46 #include <asm/facility.h>
47 #include <asm/delay.h>
48 #include <asm/div64.h>
49 #include <asm/vdso.h>
50 #include <asm/irq.h>
51 #include <asm/irq_regs.h>
52 #include <asm/vtimer.h>
53 #include <asm/stp.h>
54 #include <asm/cio.h>
55 #include "entry.h"
56 
57 union tod_clock tod_clock_base __section(".data");
58 EXPORT_SYMBOL_GPL(tod_clock_base);
59 
60 u64 clock_comparator_max = -1ULL;
61 EXPORT_SYMBOL_GPL(clock_comparator_max);
62 
63 static DEFINE_PER_CPU(struct clock_event_device, comparators);
64 
65 ATOMIC_NOTIFIER_HEAD(s390_epoch_delta_notifier);
66 EXPORT_SYMBOL(s390_epoch_delta_notifier);
67 
68 unsigned char ptff_function_mask[16];
69 
70 static unsigned long lpar_offset;
71 static unsigned long initial_leap_seconds;
72 static unsigned long tod_steering_end;
73 static long tod_steering_delta;
74 
75 /*
76  * Get time offsets with PTFF
77  */
78 void __init time_early_init(void)
79 {
80 	struct ptff_qto qto;
81 	struct ptff_qui qui;
82 	int cs;
83 
84 	/* Initialize TOD steering parameters */
85 	tod_steering_end = tod_clock_base.tod;
86 	for (cs = 0; cs < CS_BASES; cs++)
87 		vdso_data[cs].arch_data.tod_steering_end = tod_steering_end;
88 
89 	if (!test_facility(28))
90 		return;
91 
92 	ptff(&ptff_function_mask, sizeof(ptff_function_mask), PTFF_QAF);
93 
94 	/* get LPAR offset */
95 	if (ptff_query(PTFF_QTO) && ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
96 		lpar_offset = qto.tod_epoch_difference;
97 
98 	/* get initial leap seconds */
99 	if (ptff_query(PTFF_QUI) && ptff(&qui, sizeof(qui), PTFF_QUI) == 0)
100 		initial_leap_seconds = (unsigned long)
101 			((long) qui.old_leap * 4096000000L);
102 }
103 
104 unsigned long long noinstr sched_clock_noinstr(void)
105 {
106 	return tod_to_ns(__get_tod_clock_monotonic());
107 }
108 
109 /*
110  * Scheduler clock - returns current time in nanosec units.
111  */
112 unsigned long long notrace sched_clock(void)
113 {
114 	return tod_to_ns(get_tod_clock_monotonic());
115 }
116 NOKPROBE_SYMBOL(sched_clock);
117 
118 static void ext_to_timespec64(union tod_clock *clk, struct timespec64 *xt)
119 {
120 	unsigned long rem, sec, nsec;
121 
122 	sec = clk->us;
123 	rem = do_div(sec, 1000000);
124 	nsec = ((clk->sus + (rem << 12)) * 125) >> 9;
125 	xt->tv_sec = sec;
126 	xt->tv_nsec = nsec;
127 }
128 
129 void clock_comparator_work(void)
130 {
131 	struct clock_event_device *cd;
132 
133 	get_lowcore()->clock_comparator = clock_comparator_max;
134 	cd = this_cpu_ptr(&comparators);
135 	cd->event_handler(cd);
136 }
137 
138 static int s390_next_event(unsigned long delta,
139 			   struct clock_event_device *evt)
140 {
141 	get_lowcore()->clock_comparator = get_tod_clock() + delta;
142 	set_clock_comparator(get_lowcore()->clock_comparator);
143 	return 0;
144 }
145 
146 /*
147  * Set up lowcore and control register of the current cpu to
148  * enable TOD clock and clock comparator interrupts.
149  */
150 void init_cpu_timer(void)
151 {
152 	struct clock_event_device *cd;
153 	int cpu;
154 
155 	get_lowcore()->clock_comparator = clock_comparator_max;
156 	set_clock_comparator(get_lowcore()->clock_comparator);
157 
158 	cpu = smp_processor_id();
159 	cd = &per_cpu(comparators, cpu);
160 	cd->name		= "comparator";
161 	cd->features		= CLOCK_EVT_FEAT_ONESHOT;
162 	cd->mult		= 16777;
163 	cd->shift		= 12;
164 	cd->min_delta_ns	= 1;
165 	cd->min_delta_ticks	= 1;
166 	cd->max_delta_ns	= LONG_MAX;
167 	cd->max_delta_ticks	= ULONG_MAX;
168 	cd->rating		= 400;
169 	cd->cpumask		= cpumask_of(cpu);
170 	cd->set_next_event	= s390_next_event;
171 
172 	clockevents_register_device(cd);
173 
174 	/* Enable clock comparator timer interrupt. */
175 	local_ctl_set_bit(0, CR0_CLOCK_COMPARATOR_SUBMASK_BIT);
176 
177 	/* Always allow the timing alert external interrupt. */
178 	local_ctl_set_bit(0, CR0_ETR_SUBMASK_BIT);
179 }
180 
181 static void clock_comparator_interrupt(struct ext_code ext_code,
182 				       unsigned int param32,
183 				       unsigned long param64)
184 {
185 	inc_irq_stat(IRQEXT_CLK);
186 	if (get_lowcore()->clock_comparator == clock_comparator_max)
187 		set_clock_comparator(get_lowcore()->clock_comparator);
188 }
189 
190 static void stp_timing_alert(struct stp_irq_parm *);
191 
192 static void timing_alert_interrupt(struct ext_code ext_code,
193 				   unsigned int param32, unsigned long param64)
194 {
195 	inc_irq_stat(IRQEXT_TLA);
196 	if (param32 & 0x00038000)
197 		stp_timing_alert((struct stp_irq_parm *) &param32);
198 }
199 
200 static void stp_reset(void);
201 
202 void read_persistent_clock64(struct timespec64 *ts)
203 {
204 	union tod_clock clk;
205 	u64 delta;
206 
207 	delta = initial_leap_seconds + TOD_UNIX_EPOCH;
208 	store_tod_clock_ext(&clk);
209 	clk.eitod -= delta;
210 	ext_to_timespec64(&clk, ts);
211 }
212 
213 void __init read_persistent_wall_and_boot_offset(struct timespec64 *wall_time,
214 						 struct timespec64 *boot_offset)
215 {
216 	struct timespec64 boot_time;
217 	union tod_clock clk;
218 	u64 delta;
219 
220 	delta = initial_leap_seconds + TOD_UNIX_EPOCH;
221 	clk = tod_clock_base;
222 	clk.eitod -= delta;
223 	ext_to_timespec64(&clk, &boot_time);
224 
225 	read_persistent_clock64(wall_time);
226 	*boot_offset = timespec64_sub(*wall_time, boot_time);
227 }
228 
229 static u64 read_tod_clock(struct clocksource *cs)
230 {
231 	unsigned long now, adj;
232 
233 	preempt_disable(); /* protect from changes to steering parameters */
234 	now = get_tod_clock();
235 	adj = tod_steering_end - now;
236 	if (unlikely((s64) adj > 0))
237 		/*
238 		 * manually steer by 1 cycle every 2^16 cycles. This
239 		 * corresponds to shifting the tod delta by 15. 1s is
240 		 * therefore steered in ~9h. The adjust will decrease
241 		 * over time, until it finally reaches 0.
242 		 */
243 		now += (tod_steering_delta < 0) ? (adj >> 15) : -(adj >> 15);
244 	preempt_enable();
245 	return now;
246 }
247 
248 static struct clocksource clocksource_tod = {
249 	.name		= "tod",
250 	.rating		= 400,
251 	.read		= read_tod_clock,
252 	.mask		= CLOCKSOURCE_MASK(64),
253 	.mult		= 4096000,
254 	.shift		= 24,
255 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
256 	.vdso_clock_mode = VDSO_CLOCKMODE_TOD,
257 };
258 
259 struct clocksource * __init clocksource_default_clock(void)
260 {
261 	return &clocksource_tod;
262 }
263 
264 /*
265  * Initialize the TOD clock and the CPU timer of
266  * the boot cpu.
267  */
268 void __init time_init(void)
269 {
270 	/* Reset time synchronization interfaces. */
271 	stp_reset();
272 
273 	/* request the clock comparator external interrupt */
274 	if (register_external_irq(EXT_IRQ_CLK_COMP, clock_comparator_interrupt))
275 		panic("Couldn't request external interrupt 0x1004");
276 
277 	/* request the timing alert external interrupt */
278 	if (register_external_irq(EXT_IRQ_TIMING_ALERT, timing_alert_interrupt))
279 		panic("Couldn't request external interrupt 0x1406");
280 
281 	if (__clocksource_register(&clocksource_tod) != 0)
282 		panic("Could not register TOD clock source");
283 
284 	/* Enable TOD clock interrupts on the boot cpu. */
285 	init_cpu_timer();
286 
287 	/* Enable cpu timer interrupts on the boot cpu. */
288 	vtime_init();
289 }
290 
291 static DEFINE_PER_CPU(atomic_t, clock_sync_word);
292 static DEFINE_MUTEX(stp_mutex);
293 static unsigned long clock_sync_flags;
294 
295 #define CLOCK_SYNC_HAS_STP		0
296 #define CLOCK_SYNC_STP			1
297 #define CLOCK_SYNC_STPINFO_VALID	2
298 
299 /*
300  * The get_clock function for the physical clock. It will get the current
301  * TOD clock, subtract the LPAR offset and write the result to *clock.
302  * The function returns 0 if the clock is in sync with the external time
303  * source. If the clock mode is local it will return -EOPNOTSUPP and
304  * -EAGAIN if the clock is not in sync with the external reference.
305  */
306 int get_phys_clock(unsigned long *clock)
307 {
308 	atomic_t *sw_ptr;
309 	unsigned int sw0, sw1;
310 
311 	sw_ptr = &get_cpu_var(clock_sync_word);
312 	sw0 = atomic_read(sw_ptr);
313 	*clock = get_tod_clock() - lpar_offset;
314 	sw1 = atomic_read(sw_ptr);
315 	put_cpu_var(clock_sync_word);
316 	if (sw0 == sw1 && (sw0 & 0x80000000U))
317 		/* Success: time is in sync. */
318 		return 0;
319 	if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
320 		return -EOPNOTSUPP;
321 	if (!test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
322 		return -EACCES;
323 	return -EAGAIN;
324 }
325 EXPORT_SYMBOL(get_phys_clock);
326 
327 /*
328  * Make get_phys_clock() return -EAGAIN.
329  */
330 static void disable_sync_clock(void *dummy)
331 {
332 	atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
333 	/*
334 	 * Clear the in-sync bit 2^31. All get_phys_clock calls will
335 	 * fail until the sync bit is turned back on. In addition
336 	 * increase the "sequence" counter to avoid the race of an
337 	 * stp event and the complete recovery against get_phys_clock.
338 	 */
339 	atomic_andnot(0x80000000, sw_ptr);
340 	atomic_inc(sw_ptr);
341 }
342 
343 /*
344  * Make get_phys_clock() return 0 again.
345  * Needs to be called from a context disabled for preemption.
346  */
347 static void enable_sync_clock(void)
348 {
349 	atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
350 	atomic_or(0x80000000, sw_ptr);
351 }
352 
353 /*
354  * Function to check if the clock is in sync.
355  */
356 static inline int check_sync_clock(void)
357 {
358 	atomic_t *sw_ptr;
359 	int rc;
360 
361 	sw_ptr = &get_cpu_var(clock_sync_word);
362 	rc = (atomic_read(sw_ptr) & 0x80000000U) != 0;
363 	put_cpu_var(clock_sync_word);
364 	return rc;
365 }
366 
367 /*
368  * Apply clock delta to the global data structures.
369  * This is called once on the CPU that performed the clock sync.
370  */
371 static void clock_sync_global(long delta)
372 {
373 	unsigned long now, adj;
374 	struct ptff_qto qto;
375 	int cs;
376 
377 	/* Fixup the monotonic sched clock. */
378 	tod_clock_base.eitod += delta;
379 	/* Adjust TOD steering parameters. */
380 	now = get_tod_clock();
381 	adj = tod_steering_end - now;
382 	if (unlikely((s64) adj >= 0))
383 		/* Calculate how much of the old adjustment is left. */
384 		tod_steering_delta = (tod_steering_delta < 0) ?
385 			-(adj >> 15) : (adj >> 15);
386 	tod_steering_delta += delta;
387 	if ((abs(tod_steering_delta) >> 48) != 0)
388 		panic("TOD clock sync offset %li is too large to drift\n",
389 		      tod_steering_delta);
390 	tod_steering_end = now + (abs(tod_steering_delta) << 15);
391 	for (cs = 0; cs < CS_BASES; cs++) {
392 		vdso_data[cs].arch_data.tod_steering_end = tod_steering_end;
393 		vdso_data[cs].arch_data.tod_steering_delta = tod_steering_delta;
394 	}
395 
396 	/* Update LPAR offset. */
397 	if (ptff_query(PTFF_QTO) && ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
398 		lpar_offset = qto.tod_epoch_difference;
399 	/* Call the TOD clock change notifier. */
400 	atomic_notifier_call_chain(&s390_epoch_delta_notifier, 0, &delta);
401 }
402 
403 /*
404  * Apply clock delta to the per-CPU data structures of this CPU.
405  * This is called for each online CPU after the call to clock_sync_global.
406  */
407 static void clock_sync_local(long delta)
408 {
409 	/* Add the delta to the clock comparator. */
410 	if (get_lowcore()->clock_comparator != clock_comparator_max) {
411 		get_lowcore()->clock_comparator += delta;
412 		set_clock_comparator(get_lowcore()->clock_comparator);
413 	}
414 	/* Adjust the last_update_clock time-stamp. */
415 	get_lowcore()->last_update_clock += delta;
416 }
417 
418 /* Single threaded workqueue used for stp sync events */
419 static struct workqueue_struct *time_sync_wq;
420 
421 static void __init time_init_wq(void)
422 {
423 	if (time_sync_wq)
424 		return;
425 	time_sync_wq = create_singlethread_workqueue("timesync");
426 }
427 
428 struct clock_sync_data {
429 	atomic_t cpus;
430 	int in_sync;
431 	long clock_delta;
432 };
433 
434 /*
435  * Server Time Protocol (STP) code.
436  */
437 static bool stp_online;
438 static struct stp_sstpi stp_info;
439 static void *stp_page;
440 
441 static void stp_work_fn(struct work_struct *work);
442 static DECLARE_WORK(stp_work, stp_work_fn);
443 static struct timer_list stp_timer;
444 
445 static int __init early_parse_stp(char *p)
446 {
447 	return kstrtobool(p, &stp_online);
448 }
449 early_param("stp", early_parse_stp);
450 
451 /*
452  * Reset STP attachment.
453  */
454 static void __init stp_reset(void)
455 {
456 	int rc;
457 
458 	stp_page = (void *) get_zeroed_page(GFP_ATOMIC);
459 	rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
460 	if (rc == 0)
461 		set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
462 	else if (stp_online) {
463 		pr_warn("The real or virtual hardware system does not provide an STP interface\n");
464 		free_page((unsigned long) stp_page);
465 		stp_page = NULL;
466 		stp_online = false;
467 	}
468 }
469 
470 static void stp_timeout(struct timer_list *unused)
471 {
472 	queue_work(time_sync_wq, &stp_work);
473 }
474 
475 static int __init stp_init(void)
476 {
477 	if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
478 		return 0;
479 	timer_setup(&stp_timer, stp_timeout, 0);
480 	time_init_wq();
481 	if (!stp_online)
482 		return 0;
483 	queue_work(time_sync_wq, &stp_work);
484 	return 0;
485 }
486 
487 arch_initcall(stp_init);
488 
489 /*
490  * STP timing alert. There are three causes:
491  * 1) timing status change
492  * 2) link availability change
493  * 3) time control parameter change
494  * In all three cases we are only interested in the clock source state.
495  * If a STP clock source is now available use it.
496  */
497 static void stp_timing_alert(struct stp_irq_parm *intparm)
498 {
499 	if (intparm->tsc || intparm->lac || intparm->tcpc)
500 		queue_work(time_sync_wq, &stp_work);
501 }
502 
503 /*
504  * STP sync check machine check. This is called when the timing state
505  * changes from the synchronized state to the unsynchronized state.
506  * After a STP sync check the clock is not in sync. The machine check
507  * is broadcasted to all cpus at the same time.
508  */
509 int stp_sync_check(void)
510 {
511 	disable_sync_clock(NULL);
512 	return 1;
513 }
514 
515 /*
516  * STP island condition machine check. This is called when an attached
517  * server  attempts to communicate over an STP link and the servers
518  * have matching CTN ids and have a valid stratum-1 configuration
519  * but the configurations do not match.
520  */
521 int stp_island_check(void)
522 {
523 	disable_sync_clock(NULL);
524 	return 1;
525 }
526 
527 void stp_queue_work(void)
528 {
529 	queue_work(time_sync_wq, &stp_work);
530 }
531 
532 static int __store_stpinfo(void)
533 {
534 	int rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
535 
536 	if (rc)
537 		clear_bit(CLOCK_SYNC_STPINFO_VALID, &clock_sync_flags);
538 	else
539 		set_bit(CLOCK_SYNC_STPINFO_VALID, &clock_sync_flags);
540 	return rc;
541 }
542 
543 static int stpinfo_valid(void)
544 {
545 	return stp_online && test_bit(CLOCK_SYNC_STPINFO_VALID, &clock_sync_flags);
546 }
547 
548 static int stp_sync_clock(void *data)
549 {
550 	struct clock_sync_data *sync = data;
551 	long clock_delta, flags;
552 	static int first;
553 	int rc;
554 
555 	enable_sync_clock();
556 	if (xchg(&first, 1) == 0) {
557 		/* Wait until all other cpus entered the sync function. */
558 		while (atomic_read(&sync->cpus) != 0)
559 			cpu_relax();
560 		rc = 0;
561 		if (stp_info.todoff || stp_info.tmd != 2) {
562 			flags = vdso_update_begin();
563 			rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0,
564 					&clock_delta);
565 			if (rc == 0) {
566 				sync->clock_delta = clock_delta;
567 				clock_sync_global(clock_delta);
568 				rc = __store_stpinfo();
569 				if (rc == 0 && stp_info.tmd != 2)
570 					rc = -EAGAIN;
571 			}
572 			vdso_update_end(flags);
573 		}
574 		sync->in_sync = rc ? -EAGAIN : 1;
575 		xchg(&first, 0);
576 	} else {
577 		/* Slave */
578 		atomic_dec(&sync->cpus);
579 		/* Wait for in_sync to be set. */
580 		while (READ_ONCE(sync->in_sync) == 0)
581 			__udelay(1);
582 	}
583 	if (sync->in_sync != 1)
584 		/* Didn't work. Clear per-cpu in sync bit again. */
585 		disable_sync_clock(NULL);
586 	/* Apply clock delta to per-CPU fields of this CPU. */
587 	clock_sync_local(sync->clock_delta);
588 
589 	return 0;
590 }
591 
592 static int stp_clear_leap(void)
593 {
594 	struct __kernel_timex txc;
595 	int ret;
596 
597 	memset(&txc, 0, sizeof(txc));
598 
599 	ret = do_adjtimex(&txc);
600 	if (ret < 0)
601 		return ret;
602 
603 	txc.modes = ADJ_STATUS;
604 	txc.status &= ~(STA_INS|STA_DEL);
605 	return do_adjtimex(&txc);
606 }
607 
608 static void stp_check_leap(void)
609 {
610 	struct stp_stzi stzi;
611 	struct stp_lsoib *lsoib = &stzi.lsoib;
612 	struct __kernel_timex txc;
613 	int64_t timediff;
614 	int leapdiff, ret;
615 
616 	if (!stp_info.lu || !check_sync_clock()) {
617 		/*
618 		 * Either a scheduled leap second was removed by the operator,
619 		 * or STP is out of sync. In both cases, clear the leap second
620 		 * kernel flags.
621 		 */
622 		if (stp_clear_leap() < 0)
623 			pr_err("failed to clear leap second flags\n");
624 		return;
625 	}
626 
627 	if (chsc_stzi(stp_page, &stzi, sizeof(stzi))) {
628 		pr_err("stzi failed\n");
629 		return;
630 	}
631 
632 	timediff = tod_to_ns(lsoib->nlsout - get_tod_clock()) / NSEC_PER_SEC;
633 	leapdiff = lsoib->nlso - lsoib->also;
634 
635 	if (leapdiff != 1 && leapdiff != -1) {
636 		pr_err("Cannot schedule %d leap seconds\n", leapdiff);
637 		return;
638 	}
639 
640 	if (timediff < 0) {
641 		if (stp_clear_leap() < 0)
642 			pr_err("failed to clear leap second flags\n");
643 	} else if (timediff < 7200) {
644 		memset(&txc, 0, sizeof(txc));
645 		ret = do_adjtimex(&txc);
646 		if (ret < 0)
647 			return;
648 
649 		txc.modes = ADJ_STATUS;
650 		if (leapdiff > 0)
651 			txc.status |= STA_INS;
652 		else
653 			txc.status |= STA_DEL;
654 		ret = do_adjtimex(&txc);
655 		if (ret < 0)
656 			pr_err("failed to set leap second flags\n");
657 		/* arm Timer to clear leap second flags */
658 		mod_timer(&stp_timer, jiffies + msecs_to_jiffies(14400 * MSEC_PER_SEC));
659 	} else {
660 		/* The day the leap second is scheduled for hasn't been reached. Retry
661 		 * in one hour.
662 		 */
663 		mod_timer(&stp_timer, jiffies + msecs_to_jiffies(3600 * MSEC_PER_SEC));
664 	}
665 }
666 
667 /*
668  * STP work. Check for the STP state and take over the clock
669  * synchronization if the STP clock source is usable.
670  */
671 static void stp_work_fn(struct work_struct *work)
672 {
673 	struct clock_sync_data stp_sync;
674 	int rc;
675 
676 	/* prevent multiple execution. */
677 	mutex_lock(&stp_mutex);
678 
679 	if (!stp_online) {
680 		chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
681 		del_timer_sync(&stp_timer);
682 		goto out_unlock;
683 	}
684 
685 	rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xf0e0, NULL);
686 	if (rc)
687 		goto out_unlock;
688 
689 	rc = __store_stpinfo();
690 	if (rc || stp_info.c == 0)
691 		goto out_unlock;
692 
693 	/* Skip synchronization if the clock is already in sync. */
694 	if (!check_sync_clock()) {
695 		memset(&stp_sync, 0, sizeof(stp_sync));
696 		cpus_read_lock();
697 		atomic_set(&stp_sync.cpus, num_online_cpus() - 1);
698 		stop_machine_cpuslocked(stp_sync_clock, &stp_sync, cpu_online_mask);
699 		cpus_read_unlock();
700 	}
701 
702 	if (!check_sync_clock())
703 		/*
704 		 * There is a usable clock but the synchronization failed.
705 		 * Retry after a second.
706 		 */
707 		mod_timer(&stp_timer, jiffies + msecs_to_jiffies(MSEC_PER_SEC));
708 	else if (stp_info.lu)
709 		stp_check_leap();
710 
711 out_unlock:
712 	mutex_unlock(&stp_mutex);
713 }
714 
715 /*
716  * STP subsys sysfs interface functions
717  */
718 static const struct bus_type stp_subsys = {
719 	.name		= "stp",
720 	.dev_name	= "stp",
721 };
722 
723 static ssize_t ctn_id_show(struct device *dev,
724 				struct device_attribute *attr,
725 				char *buf)
726 {
727 	ssize_t ret = -ENODATA;
728 
729 	mutex_lock(&stp_mutex);
730 	if (stpinfo_valid())
731 		ret = sysfs_emit(buf, "%016lx\n",
732 				 *(unsigned long *)stp_info.ctnid);
733 	mutex_unlock(&stp_mutex);
734 	return ret;
735 }
736 
737 static DEVICE_ATTR_RO(ctn_id);
738 
739 static ssize_t ctn_type_show(struct device *dev,
740 				struct device_attribute *attr,
741 				char *buf)
742 {
743 	ssize_t ret = -ENODATA;
744 
745 	mutex_lock(&stp_mutex);
746 	if (stpinfo_valid())
747 		ret = sysfs_emit(buf, "%i\n", stp_info.ctn);
748 	mutex_unlock(&stp_mutex);
749 	return ret;
750 }
751 
752 static DEVICE_ATTR_RO(ctn_type);
753 
754 static ssize_t dst_offset_show(struct device *dev,
755 				   struct device_attribute *attr,
756 				   char *buf)
757 {
758 	ssize_t ret = -ENODATA;
759 
760 	mutex_lock(&stp_mutex);
761 	if (stpinfo_valid() && (stp_info.vbits & 0x2000))
762 		ret = sysfs_emit(buf, "%i\n", (int)(s16)stp_info.dsto);
763 	mutex_unlock(&stp_mutex);
764 	return ret;
765 }
766 
767 static DEVICE_ATTR_RO(dst_offset);
768 
769 static ssize_t leap_seconds_show(struct device *dev,
770 					struct device_attribute *attr,
771 					char *buf)
772 {
773 	ssize_t ret = -ENODATA;
774 
775 	mutex_lock(&stp_mutex);
776 	if (stpinfo_valid() && (stp_info.vbits & 0x8000))
777 		ret = sysfs_emit(buf, "%i\n", (int)(s16)stp_info.leaps);
778 	mutex_unlock(&stp_mutex);
779 	return ret;
780 }
781 
782 static DEVICE_ATTR_RO(leap_seconds);
783 
784 static ssize_t leap_seconds_scheduled_show(struct device *dev,
785 						struct device_attribute *attr,
786 						char *buf)
787 {
788 	struct stp_stzi stzi;
789 	ssize_t ret;
790 
791 	mutex_lock(&stp_mutex);
792 	if (!stpinfo_valid() || !(stp_info.vbits & 0x8000) || !stp_info.lu) {
793 		mutex_unlock(&stp_mutex);
794 		return -ENODATA;
795 	}
796 
797 	ret = chsc_stzi(stp_page, &stzi, sizeof(stzi));
798 	mutex_unlock(&stp_mutex);
799 	if (ret < 0)
800 		return ret;
801 
802 	if (!stzi.lsoib.p)
803 		return sysfs_emit(buf, "0,0\n");
804 
805 	return sysfs_emit(buf, "%lu,%d\n",
806 			  tod_to_ns(stzi.lsoib.nlsout - TOD_UNIX_EPOCH) / NSEC_PER_SEC,
807 			  stzi.lsoib.nlso - stzi.lsoib.also);
808 }
809 
810 static DEVICE_ATTR_RO(leap_seconds_scheduled);
811 
812 static ssize_t stratum_show(struct device *dev,
813 				struct device_attribute *attr,
814 				char *buf)
815 {
816 	ssize_t ret = -ENODATA;
817 
818 	mutex_lock(&stp_mutex);
819 	if (stpinfo_valid())
820 		ret = sysfs_emit(buf, "%i\n", (int)(s16)stp_info.stratum);
821 	mutex_unlock(&stp_mutex);
822 	return ret;
823 }
824 
825 static DEVICE_ATTR_RO(stratum);
826 
827 static ssize_t time_offset_show(struct device *dev,
828 				struct device_attribute *attr,
829 				char *buf)
830 {
831 	ssize_t ret = -ENODATA;
832 
833 	mutex_lock(&stp_mutex);
834 	if (stpinfo_valid() && (stp_info.vbits & 0x0800))
835 		ret = sysfs_emit(buf, "%i\n", (int)stp_info.tto);
836 	mutex_unlock(&stp_mutex);
837 	return ret;
838 }
839 
840 static DEVICE_ATTR_RO(time_offset);
841 
842 static ssize_t time_zone_offset_show(struct device *dev,
843 				struct device_attribute *attr,
844 				char *buf)
845 {
846 	ssize_t ret = -ENODATA;
847 
848 	mutex_lock(&stp_mutex);
849 	if (stpinfo_valid() && (stp_info.vbits & 0x4000))
850 		ret = sysfs_emit(buf, "%i\n", (int)(s16)stp_info.tzo);
851 	mutex_unlock(&stp_mutex);
852 	return ret;
853 }
854 
855 static DEVICE_ATTR_RO(time_zone_offset);
856 
857 static ssize_t timing_mode_show(struct device *dev,
858 				struct device_attribute *attr,
859 				char *buf)
860 {
861 	ssize_t ret = -ENODATA;
862 
863 	mutex_lock(&stp_mutex);
864 	if (stpinfo_valid())
865 		ret = sysfs_emit(buf, "%i\n", stp_info.tmd);
866 	mutex_unlock(&stp_mutex);
867 	return ret;
868 }
869 
870 static DEVICE_ATTR_RO(timing_mode);
871 
872 static ssize_t timing_state_show(struct device *dev,
873 				struct device_attribute *attr,
874 				char *buf)
875 {
876 	ssize_t ret = -ENODATA;
877 
878 	mutex_lock(&stp_mutex);
879 	if (stpinfo_valid())
880 		ret = sysfs_emit(buf, "%i\n", stp_info.tst);
881 	mutex_unlock(&stp_mutex);
882 	return ret;
883 }
884 
885 static DEVICE_ATTR_RO(timing_state);
886 
887 static ssize_t online_show(struct device *dev,
888 				struct device_attribute *attr,
889 				char *buf)
890 {
891 	return sysfs_emit(buf, "%i\n", stp_online);
892 }
893 
894 static ssize_t online_store(struct device *dev,
895 				struct device_attribute *attr,
896 				const char *buf, size_t count)
897 {
898 	unsigned int value;
899 
900 	value = simple_strtoul(buf, NULL, 0);
901 	if (value != 0 && value != 1)
902 		return -EINVAL;
903 	if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
904 		return -EOPNOTSUPP;
905 	mutex_lock(&stp_mutex);
906 	stp_online = value;
907 	if (stp_online)
908 		set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
909 	else
910 		clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
911 	queue_work(time_sync_wq, &stp_work);
912 	mutex_unlock(&stp_mutex);
913 	return count;
914 }
915 
916 /*
917  * Can't use DEVICE_ATTR because the attribute should be named
918  * stp/online but dev_attr_online already exists in this file ..
919  */
920 static DEVICE_ATTR_RW(online);
921 
922 static struct attribute *stp_dev_attrs[] = {
923 	&dev_attr_ctn_id.attr,
924 	&dev_attr_ctn_type.attr,
925 	&dev_attr_dst_offset.attr,
926 	&dev_attr_leap_seconds.attr,
927 	&dev_attr_online.attr,
928 	&dev_attr_leap_seconds_scheduled.attr,
929 	&dev_attr_stratum.attr,
930 	&dev_attr_time_offset.attr,
931 	&dev_attr_time_zone_offset.attr,
932 	&dev_attr_timing_mode.attr,
933 	&dev_attr_timing_state.attr,
934 	NULL
935 };
936 ATTRIBUTE_GROUPS(stp_dev);
937 
938 static int __init stp_init_sysfs(void)
939 {
940 	return subsys_system_register(&stp_subsys, stp_dev_groups);
941 }
942 
943 device_initcall(stp_init_sysfs);
944