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