xref: /openbmc/linux/arch/s390/kernel/time.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1a17ae4c3SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *    Time of day based timer functions.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  S390 version
6d2fec595SMartin Schwidefsky  *    Copyright IBM Corp. 1999, 2008
71da177e4SLinus Torvalds  *    Author(s): Hartmut Penner (hp@de.ibm.com),
81da177e4SLinus Torvalds  *               Martin Schwidefsky (schwidefsky@de.ibm.com),
91da177e4SLinus Torvalds  *               Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  Derived from "arch/i386/kernel/time.c"
121da177e4SLinus Torvalds  *    Copyright (C) 1991, 1992, 1995  Linus Torvalds
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
15feab6501SMartin Schwidefsky #define KMSG_COMPONENT "time"
16feab6501SMartin Schwidefsky #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17feab6501SMartin Schwidefsky 
18052ff461SHeiko Carstens #include <linux/kernel_stat.h>
191da177e4SLinus Torvalds #include <linux/errno.h>
203994a52bSPaul Gortmaker #include <linux/export.h>
211da177e4SLinus Torvalds #include <linux/sched.h>
22e6017571SIngo Molnar #include <linux/sched/clock.h>
231da177e4SLinus Torvalds #include <linux/kernel.h>
241da177e4SLinus Torvalds #include <linux/param.h>
251da177e4SLinus Torvalds #include <linux/string.h>
261da177e4SLinus Torvalds #include <linux/mm.h>
271da177e4SLinus Torvalds #include <linux/interrupt.h>
28750887deSHeiko Carstens #include <linux/cpu.h>
29750887deSHeiko Carstens #include <linux/stop_machine.h>
301da177e4SLinus Torvalds #include <linux/time.h>
313fbacffbSKay Sievers #include <linux/device.h>
321da177e4SLinus Torvalds #include <linux/delay.h>
331da177e4SLinus Torvalds #include <linux/init.h>
341da177e4SLinus Torvalds #include <linux/smp.h>
351da177e4SLinus Torvalds #include <linux/types.h>
361da177e4SLinus Torvalds #include <linux/profile.h>
371da177e4SLinus Torvalds #include <linux/timex.h>
381da177e4SLinus Torvalds #include <linux/notifier.h>
39189374aeSJohn Stultz #include <linux/timekeeper_internal.h>
405a62b192SHeiko Carstens #include <linux/clockchips.h>
415a0e3ad6STejun Heo #include <linux/gfp.h>
42860dba45SMartin Schwidefsky #include <linux/kprobes.h>
437c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
444bff8cb5SSven Schnelle #include <vdso/vsyscall.h>
454bff8cb5SSven Schnelle #include <vdso/clocksource.h>
464bff8cb5SSven Schnelle #include <vdso/helpers.h>
4740277891SMartin Schwidefsky #include <asm/facility.h>
481da177e4SLinus Torvalds #include <asm/delay.h>
491da177e4SLinus Torvalds #include <asm/div64.h>
50b020632eSMartin Schwidefsky #include <asm/vdso.h>
511da177e4SLinus Torvalds #include <asm/irq.h>
525a489b98SHeiko Carstens #include <asm/irq_regs.h>
5327f6b416SMartin Schwidefsky #include <asm/vtimer.h>
54fd5ada04SMartin Schwidefsky #include <asm/stp.h>
55a806170eSHeiko Carstens #include <asm/cio.h>
56638ad34aSMartin Schwidefsky #include "entry.h"
571da177e4SLinus Torvalds 
58f8d8977aSHeiko Carstens union tod_clock tod_clock_base __section(".data");
596e2ef5e4SMartin Schwidefsky EXPORT_SYMBOL_GPL(tod_clock_base);
606e2ef5e4SMartin Schwidefsky 
616e2ef5e4SMartin Schwidefsky u64 clock_comparator_max = -1ULL;
626e2ef5e4SMartin Schwidefsky EXPORT_SYMBOL_GPL(clock_comparator_max);
63b6112ccbSMartin Schwidefsky 
645a62b192SHeiko Carstens static DEFINE_PER_CPU(struct clock_event_device, comparators);
651da177e4SLinus Torvalds 
66fdf03650SFan Zhang ATOMIC_NOTIFIER_HEAD(s390_epoch_delta_notifier);
67fdf03650SFan Zhang EXPORT_SYMBOL(s390_epoch_delta_notifier);
68fdf03650SFan Zhang 
6940277891SMartin Schwidefsky unsigned char ptff_function_mask[16];
70b1c0854dSMartin Schwidefsky 
71eba8e1afSHeiko Carstens static unsigned long lpar_offset;
72eba8e1afSHeiko Carstens static unsigned long initial_leap_seconds;
73eba8e1afSHeiko Carstens static unsigned long tod_steering_end;
74eba8e1afSHeiko Carstens static long tod_steering_delta;
7540277891SMartin Schwidefsky 
7640277891SMartin Schwidefsky /*
7740277891SMartin Schwidefsky  * Get time offsets with PTFF
7840277891SMartin Schwidefsky  */
time_early_init(void)79b1c0854dSMartin Schwidefsky void __init time_early_init(void)
8040277891SMartin Schwidefsky {
8140277891SMartin Schwidefsky 	struct ptff_qto qto;
82936cc855SMartin Schwidefsky 	struct ptff_qui qui;
835b43bd18SHeiko Carstens 	int cs;
8440277891SMartin Schwidefsky 
8575c7b6f3SMartin Schwidefsky 	/* Initialize TOD steering parameters */
86f8d8977aSHeiko Carstens 	tod_steering_end = tod_clock_base.tod;
875b43bd18SHeiko Carstens 	for (cs = 0; cs < CS_BASES; cs++)
885b43bd18SHeiko Carstens 		vdso_data[cs].arch_data.tod_steering_end = tod_steering_end;
8975c7b6f3SMartin Schwidefsky 
9040277891SMartin Schwidefsky 	if (!test_facility(28))
9140277891SMartin Schwidefsky 		return;
9275c7b6f3SMartin Schwidefsky 
9340277891SMartin Schwidefsky 	ptff(&ptff_function_mask, sizeof(ptff_function_mask), PTFF_QAF);
9440277891SMartin Schwidefsky 
9540277891SMartin Schwidefsky 	/* get LPAR offset */
9640277891SMartin Schwidefsky 	if (ptff_query(PTFF_QTO) && ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
9740277891SMartin Schwidefsky 		lpar_offset = qto.tod_epoch_difference;
98936cc855SMartin Schwidefsky 
99936cc855SMartin Schwidefsky 	/* get initial leap seconds */
100936cc855SMartin Schwidefsky 	if (ptff_query(PTFF_QUI) && ptff(&qui, sizeof(qui), PTFF_QUI) == 0)
101eba8e1afSHeiko Carstens 		initial_leap_seconds = (unsigned long)
102936cc855SMartin Schwidefsky 			((long) qui.old_leap * 4096000000L);
10340277891SMartin Schwidefsky }
10440277891SMartin Schwidefsky 
sched_clock_noinstr(void)10591b41a23SPeter Zijlstra unsigned long long noinstr sched_clock_noinstr(void)
10691b41a23SPeter Zijlstra {
10791b41a23SPeter Zijlstra 	return tod_to_ns(__get_tod_clock_monotonic());
10891b41a23SPeter Zijlstra }
10991b41a23SPeter Zijlstra 
1101da177e4SLinus Torvalds /*
1111da177e4SLinus Torvalds  * Scheduler clock - returns current time in nanosec units.
1121da177e4SLinus Torvalds  */
sched_clock(void)1137a5388deSHeiko Carstens unsigned long long notrace sched_clock(void)
1141da177e4SLinus Torvalds {
1151aae0560SHeiko Carstens 	return tod_to_ns(get_tod_clock_monotonic());
1161da177e4SLinus Torvalds }
1177a5388deSHeiko Carstens NOKPROBE_SYMBOL(sched_clock);
1181da177e4SLinus Torvalds 
ext_to_timespec64(union tod_clock * clk,struct timespec64 * xt)119f8d8977aSHeiko Carstens static void ext_to_timespec64(union tod_clock *clk, struct timespec64 *xt)
1201da177e4SLinus Torvalds {
121f8d8977aSHeiko Carstens 	unsigned long rem, sec, nsec;
1221da177e4SLinus Torvalds 
123f8d8977aSHeiko Carstens 	sec = clk->us;
1246e2ef5e4SMartin Schwidefsky 	rem = do_div(sec, 1000000);
125f8d8977aSHeiko Carstens 	nsec = ((clk->sus + (rem << 12)) * 125) >> 9;
126b1e2ba8dSJohn Stultz 	xt->tv_sec = sec;
1276e2ef5e4SMartin Schwidefsky 	xt->tv_nsec = nsec;
1281da177e4SLinus Torvalds }
1291da177e4SLinus Torvalds 
clock_comparator_work(void)1305a62b192SHeiko Carstens void clock_comparator_work(void)
1311da177e4SLinus Torvalds {
1325a62b192SHeiko Carstens 	struct clock_event_device *cd;
1331da177e4SLinus Torvalds 
1346e2ef5e4SMartin Schwidefsky 	S390_lowcore.clock_comparator = clock_comparator_max;
135eb7e7d76SChristoph Lameter 	cd = this_cpu_ptr(&comparators);
1365a62b192SHeiko Carstens 	cd->event_handler(cd);
1371da177e4SLinus Torvalds }
1381da177e4SLinus Torvalds 
s390_next_event(unsigned long delta,struct clock_event_device * evt)1398adbf78eSMartin Schwidefsky static int s390_next_event(unsigned long delta,
1405a62b192SHeiko Carstens 			   struct clock_event_device *evt)
1411da177e4SLinus Torvalds {
1428adbf78eSMartin Schwidefsky 	S390_lowcore.clock_comparator = get_tod_clock() + delta;
1435a62b192SHeiko Carstens 	set_clock_comparator(S390_lowcore.clock_comparator);
1445a62b192SHeiko Carstens 	return 0;
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
147d54853efSMartin Schwidefsky /*
148d54853efSMartin Schwidefsky  * Set up lowcore and control register of the current cpu to
149d54853efSMartin Schwidefsky  * enable TOD clock and clock comparator interrupts.
1501da177e4SLinus Torvalds  */
init_cpu_timer(void)1511da177e4SLinus Torvalds void init_cpu_timer(void)
1521da177e4SLinus Torvalds {
1535a62b192SHeiko Carstens 	struct clock_event_device *cd;
1545a62b192SHeiko Carstens 	int cpu;
1555a62b192SHeiko Carstens 
1566e2ef5e4SMartin Schwidefsky 	S390_lowcore.clock_comparator = clock_comparator_max;
1575a62b192SHeiko Carstens 	set_clock_comparator(S390_lowcore.clock_comparator);
1585a62b192SHeiko Carstens 
1595a62b192SHeiko Carstens 	cpu = smp_processor_id();
1605a62b192SHeiko Carstens 	cd = &per_cpu(comparators, cpu);
1615a62b192SHeiko Carstens 	cd->name		= "comparator";
1628adbf78eSMartin Schwidefsky 	cd->features		= CLOCK_EVT_FEAT_ONESHOT;
1635a62b192SHeiko Carstens 	cd->mult		= 16777;
1645a62b192SHeiko Carstens 	cd->shift		= 12;
1655a62b192SHeiko Carstens 	cd->min_delta_ns	= 1;
16606c54611SNicolai Stange 	cd->min_delta_ticks	= 1;
1675a62b192SHeiko Carstens 	cd->max_delta_ns	= LONG_MAX;
16806c54611SNicolai Stange 	cd->max_delta_ticks	= ULONG_MAX;
1695a62b192SHeiko Carstens 	cd->rating		= 400;
170320ab2b0SRusty Russell 	cd->cpumask		= cpumask_of(cpu);
1718adbf78eSMartin Schwidefsky 	cd->set_next_event	= s390_next_event;
1725a62b192SHeiko Carstens 
1735a62b192SHeiko Carstens 	clockevents_register_device(cd);
1741da177e4SLinus Torvalds 
175d54853efSMartin Schwidefsky 	/* Enable clock comparator timer interrupt. */
176d54853efSMartin Schwidefsky 	__ctl_set_bit(0,11);
177d54853efSMartin Schwidefsky 
178d2fec595SMartin Schwidefsky 	/* Always allow the timing alert external interrupt. */
179d54853efSMartin Schwidefsky 	__ctl_set_bit(0, 4);
180d54853efSMartin Schwidefsky }
181d54853efSMartin Schwidefsky 
clock_comparator_interrupt(struct ext_code ext_code,unsigned int param32,unsigned long param64)182fde15c3aSHeiko Carstens static void clock_comparator_interrupt(struct ext_code ext_code,
183f6649a7eSMartin Schwidefsky 				       unsigned int param32,
184f6649a7eSMartin Schwidefsky 				       unsigned long param64)
185d54853efSMartin Schwidefsky {
186420f42ecSHeiko Carstens 	inc_irq_stat(IRQEXT_CLK);
1876e2ef5e4SMartin Schwidefsky 	if (S390_lowcore.clock_comparator == clock_comparator_max)
188d3d238c7SHeiko Carstens 		set_clock_comparator(S390_lowcore.clock_comparator);
189d54853efSMartin Schwidefsky }
190d54853efSMartin Schwidefsky 
191d2fec595SMartin Schwidefsky static void stp_timing_alert(struct stp_irq_parm *);
192d2fec595SMartin Schwidefsky 
timing_alert_interrupt(struct ext_code ext_code,unsigned int param32,unsigned long param64)193fde15c3aSHeiko Carstens static void timing_alert_interrupt(struct ext_code ext_code,
194f6649a7eSMartin Schwidefsky 				   unsigned int param32, unsigned long param64)
195d2fec595SMartin Schwidefsky {
196420f42ecSHeiko Carstens 	inc_irq_stat(IRQEXT_TLA);
197f6649a7eSMartin Schwidefsky 	if (param32 & 0x00038000)
198f6649a7eSMartin Schwidefsky 		stp_timing_alert((struct stp_irq_parm *) &param32);
199d2fec595SMartin Schwidefsky }
200d2fec595SMartin Schwidefsky 
201d2fec595SMartin Schwidefsky static void stp_reset(void);
202d54853efSMartin Schwidefsky 
read_persistent_clock64(struct timespec64 * ts)203689911c7SXunlei Pang void read_persistent_clock64(struct timespec64 *ts)
204d54853efSMartin Schwidefsky {
205f8d8977aSHeiko Carstens 	union tod_clock clk;
206f8d8977aSHeiko Carstens 	u64 delta;
207936cc855SMartin Schwidefsky 
2086e2ef5e4SMartin Schwidefsky 	delta = initial_leap_seconds + TOD_UNIX_EPOCH;
209f8d8977aSHeiko Carstens 	store_tod_clock_ext(&clk);
210f8d8977aSHeiko Carstens 	clk.eitod -= delta;
211f8d8977aSHeiko Carstens 	ext_to_timespec64(&clk, ts);
2121da177e4SLinus Torvalds }
2131da177e4SLinus Torvalds 
read_persistent_wall_and_boot_offset(struct timespec64 * wall_time,struct timespec64 * boot_offset)214be2e0e42SPavel Tatashin void __init read_persistent_wall_and_boot_offset(struct timespec64 *wall_time,
215be2e0e42SPavel Tatashin 						 struct timespec64 *boot_offset)
216be2e0e42SPavel Tatashin {
217be2e0e42SPavel Tatashin 	struct timespec64 boot_time;
218f8d8977aSHeiko Carstens 	union tod_clock clk;
219f8d8977aSHeiko Carstens 	u64 delta;
220be2e0e42SPavel Tatashin 
221be2e0e42SPavel Tatashin 	delta = initial_leap_seconds + TOD_UNIX_EPOCH;
222f8d8977aSHeiko Carstens 	clk = tod_clock_base;
223f8d8977aSHeiko Carstens 	clk.eitod -= delta;
224f8d8977aSHeiko Carstens 	ext_to_timespec64(&clk, &boot_time);
225be2e0e42SPavel Tatashin 
226be2e0e42SPavel Tatashin 	read_persistent_clock64(wall_time);
227be2e0e42SPavel Tatashin 	*boot_offset = timespec64_sub(*wall_time, boot_time);
228be2e0e42SPavel Tatashin }
229be2e0e42SPavel Tatashin 
read_tod_clock(struct clocksource * cs)230a5a1d1c2SThomas Gleixner static u64 read_tod_clock(struct clocksource *cs)
231dc64bef5SMartin Schwidefsky {
232eba8e1afSHeiko Carstens 	unsigned long now, adj;
23375c7b6f3SMartin Schwidefsky 
23475c7b6f3SMartin Schwidefsky 	preempt_disable(); /* protect from changes to steering parameters */
23575c7b6f3SMartin Schwidefsky 	now = get_tod_clock();
23675c7b6f3SMartin Schwidefsky 	adj = tod_steering_end - now;
23741115582SHeiko Carstens 	if (unlikely((s64) adj > 0))
23875c7b6f3SMartin Schwidefsky 		/*
23975c7b6f3SMartin Schwidefsky 		 * manually steer by 1 cycle every 2^16 cycles. This
24075c7b6f3SMartin Schwidefsky 		 * corresponds to shifting the tod delta by 15. 1s is
24175c7b6f3SMartin Schwidefsky 		 * therefore steered in ~9h. The adjust will decrease
24275c7b6f3SMartin Schwidefsky 		 * over time, until it finally reaches 0.
24375c7b6f3SMartin Schwidefsky 		 */
244191ce9d1SMartin Schwidefsky 		now += (tod_steering_delta < 0) ? (adj >> 15) : -(adj >> 15);
24575c7b6f3SMartin Schwidefsky 	preempt_enable();
24675c7b6f3SMartin Schwidefsky 	return now;
247dc64bef5SMartin Schwidefsky }
248dc64bef5SMartin Schwidefsky 
249dc64bef5SMartin Schwidefsky static struct clocksource clocksource_tod = {
250dc64bef5SMartin Schwidefsky 	.name		= "tod",
251d2cb0e6eSChristian Borntraeger 	.rating		= 400,
252dc64bef5SMartin Schwidefsky 	.read		= read_tod_clock,
25358e15716SHeiko Carstens 	.mask		= CLOCKSOURCE_MASK(64),
254dc64bef5SMartin Schwidefsky 	.mult		= 1000,
255dc64bef5SMartin Schwidefsky 	.shift		= 12,
256cc02d809SThomas Gleixner 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
2574bff8cb5SSven Schnelle 	.vdso_clock_mode = VDSO_CLOCKMODE_TOD,
258dc64bef5SMartin Schwidefsky };
259dc64bef5SMartin Schwidefsky 
clocksource_default_clock(void)260f1b82746SMartin Schwidefsky struct clocksource * __init clocksource_default_clock(void)
261f1b82746SMartin Schwidefsky {
262f1b82746SMartin Schwidefsky 	return &clocksource_tod;
263f1b82746SMartin Schwidefsky }
264dc64bef5SMartin Schwidefsky 
2651da177e4SLinus Torvalds /*
2661da177e4SLinus Torvalds  * Initialize the TOD clock and the CPU timer of
2671da177e4SLinus Torvalds  * the boot cpu.
2681da177e4SLinus Torvalds  */
time_init(void)2691da177e4SLinus Torvalds void __init time_init(void)
2701da177e4SLinus Torvalds {
271b6112ccbSMartin Schwidefsky 	/* Reset time synchronization interfaces. */
272b6112ccbSMartin Schwidefsky 	stp_reset();
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds 	/* request the clock comparator external interrupt */
2751dad093bSThomas Huth 	if (register_external_irq(EXT_IRQ_CLK_COMP, clock_comparator_interrupt))
2761da177e4SLinus Torvalds 		panic("Couldn't request external interrupt 0x1004");
2771da177e4SLinus Torvalds 
278d2fec595SMartin Schwidefsky 	/* request the timing alert external interrupt */
2791dad093bSThomas Huth 	if (register_external_irq(EXT_IRQ_TIMING_ALERT, timing_alert_interrupt))
280d54853efSMartin Schwidefsky 		panic("Couldn't request external interrupt 0x1406");
281d54853efSMartin Schwidefsky 
282f8935983SJohn Stultz 	if (__clocksource_register(&clocksource_tod) != 0)
283ab96e798SMartin Schwidefsky 		panic("Could not register TOD clock source");
284ab96e798SMartin Schwidefsky 
285d54853efSMartin Schwidefsky 	/* Enable TOD clock interrupts on the boot cpu. */
2861da177e4SLinus Torvalds 	init_cpu_timer();
287ab96e798SMartin Schwidefsky 
288c185b783SMartin Schwidefsky 	/* Enable cpu timer interrupts on the boot cpu. */
2891da177e4SLinus Torvalds 	vtime_init();
2901da177e4SLinus Torvalds }
2911da177e4SLinus Torvalds 
292d2fec595SMartin Schwidefsky static DEFINE_PER_CPU(atomic_t, clock_sync_word);
293ad5ceb33SSven Schnelle static DEFINE_MUTEX(stp_mutex);
294d2fec595SMartin Schwidefsky static unsigned long clock_sync_flags;
295d2fec595SMartin Schwidefsky 
296ca64f639SDavid Hildenbrand #define CLOCK_SYNC_HAS_STP		0
297ca64f639SDavid Hildenbrand #define CLOCK_SYNC_STP			1
298b3bd0249SSven Schnelle #define CLOCK_SYNC_STPINFO_VALID	2
299d2fec595SMartin Schwidefsky 
300d2fec595SMartin Schwidefsky /*
30140277891SMartin Schwidefsky  * The get_clock function for the physical clock. It will get the current
30240277891SMartin Schwidefsky  * TOD clock, subtract the LPAR offset and write the result to *clock.
30340277891SMartin Schwidefsky  * The function returns 0 if the clock is in sync with the external time
30440277891SMartin Schwidefsky  * source. If the clock mode is local it will return -EOPNOTSUPP and
30540277891SMartin Schwidefsky  * -EAGAIN if the clock is not in sync with the external reference.
306d2fec595SMartin Schwidefsky  */
get_phys_clock(unsigned long * clock)3077bf76f01SJan Höppner int get_phys_clock(unsigned long *clock)
308d2fec595SMartin Schwidefsky {
309d2fec595SMartin Schwidefsky 	atomic_t *sw_ptr;
310d2fec595SMartin Schwidefsky 	unsigned int sw0, sw1;
311d2fec595SMartin Schwidefsky 
312d2fec595SMartin Schwidefsky 	sw_ptr = &get_cpu_var(clock_sync_word);
313d2fec595SMartin Schwidefsky 	sw0 = atomic_read(sw_ptr);
31440277891SMartin Schwidefsky 	*clock = get_tod_clock() - lpar_offset;
315d2fec595SMartin Schwidefsky 	sw1 = atomic_read(sw_ptr);
316bd119ee2SHeiko Carstens 	put_cpu_var(clock_sync_word);
317d2fec595SMartin Schwidefsky 	if (sw0 == sw1 && (sw0 & 0x80000000U))
318d2fec595SMartin Schwidefsky 		/* Success: time is in sync. */
319d2fec595SMartin Schwidefsky 		return 0;
320ca64f639SDavid Hildenbrand 	if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
321a8f6db4dSHeiko Carstens 		return -EOPNOTSUPP;
322ca64f639SDavid Hildenbrand 	if (!test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
323d2fec595SMartin Schwidefsky 		return -EACCES;
324d2fec595SMartin Schwidefsky 	return -EAGAIN;
325d2fec595SMartin Schwidefsky }
32640277891SMartin Schwidefsky EXPORT_SYMBOL(get_phys_clock);
327d2fec595SMartin Schwidefsky 
328d2fec595SMartin Schwidefsky /*
329ca64f639SDavid Hildenbrand  * Make get_phys_clock() return -EAGAIN.
330d2fec595SMartin Schwidefsky  */
disable_sync_clock(void * dummy)331d2fec595SMartin Schwidefsky static void disable_sync_clock(void *dummy)
332d2fec595SMartin Schwidefsky {
333eb7e7d76SChristoph Lameter 	atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
334d2fec595SMartin Schwidefsky 	/*
335ca64f639SDavid Hildenbrand 	 * Clear the in-sync bit 2^31. All get_phys_clock calls will
336d2fec595SMartin Schwidefsky 	 * fail until the sync bit is turned back on. In addition
337d2fec595SMartin Schwidefsky 	 * increase the "sequence" counter to avoid the race of an
338ca64f639SDavid Hildenbrand 	 * stp event and the complete recovery against get_phys_clock.
339d2fec595SMartin Schwidefsky 	 */
340805de8f4SPeter Zijlstra 	atomic_andnot(0x80000000, sw_ptr);
341d2fec595SMartin Schwidefsky 	atomic_inc(sw_ptr);
342d2fec595SMartin Schwidefsky }
343d2fec595SMartin Schwidefsky 
344d2fec595SMartin Schwidefsky /*
345ca64f639SDavid Hildenbrand  * Make get_phys_clock() return 0 again.
346d2fec595SMartin Schwidefsky  * Needs to be called from a context disabled for preemption.
347d2fec595SMartin Schwidefsky  */
enable_sync_clock(void)348d2fec595SMartin Schwidefsky static void enable_sync_clock(void)
349d2fec595SMartin Schwidefsky {
350eb7e7d76SChristoph Lameter 	atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
351805de8f4SPeter Zijlstra 	atomic_or(0x80000000, sw_ptr);
352d2fec595SMartin Schwidefsky }
353d2fec595SMartin Schwidefsky 
3548283cb43SMartin Schwidefsky /*
3558283cb43SMartin Schwidefsky  * Function to check if the clock is in sync.
3568283cb43SMartin Schwidefsky  */
check_sync_clock(void)3578283cb43SMartin Schwidefsky static inline int check_sync_clock(void)
3588283cb43SMartin Schwidefsky {
3598283cb43SMartin Schwidefsky 	atomic_t *sw_ptr;
3608283cb43SMartin Schwidefsky 	int rc;
3618283cb43SMartin Schwidefsky 
3628283cb43SMartin Schwidefsky 	sw_ptr = &get_cpu_var(clock_sync_word);
3638283cb43SMartin Schwidefsky 	rc = (atomic_read(sw_ptr) & 0x80000000U) != 0;
364bd119ee2SHeiko Carstens 	put_cpu_var(clock_sync_word);
3658283cb43SMartin Schwidefsky 	return rc;
3668283cb43SMartin Schwidefsky }
3678283cb43SMartin Schwidefsky 
368b1c0854dSMartin Schwidefsky /*
369b1c0854dSMartin Schwidefsky  * Apply clock delta to the global data structures.
370b1c0854dSMartin Schwidefsky  * This is called once on the CPU that performed the clock sync.
371b1c0854dSMartin Schwidefsky  */
clock_sync_global(long delta)3725ace65ebSSven Schnelle static void clock_sync_global(long delta)
373b1c0854dSMartin Schwidefsky {
37475c7b6f3SMartin Schwidefsky 	unsigned long now, adj;
375b1c0854dSMartin Schwidefsky 	struct ptff_qto qto;
3765b43bd18SHeiko Carstens 	int cs;
377b1c0854dSMartin Schwidefsky 
378b1c0854dSMartin Schwidefsky 	/* Fixup the monotonic sched clock. */
379f8d8977aSHeiko Carstens 	tod_clock_base.eitod += delta;
38075c7b6f3SMartin Schwidefsky 	/* Adjust TOD steering parameters. */
38175c7b6f3SMartin Schwidefsky 	now = get_tod_clock();
38275c7b6f3SMartin Schwidefsky 	adj = tod_steering_end - now;
38375c7b6f3SMartin Schwidefsky 	if (unlikely((s64) adj >= 0))
38475c7b6f3SMartin Schwidefsky 		/* Calculate how much of the old adjustment is left. */
385191ce9d1SMartin Schwidefsky 		tod_steering_delta = (tod_steering_delta < 0) ?
38675c7b6f3SMartin Schwidefsky 			-(adj >> 15) : (adj >> 15);
38775c7b6f3SMartin Schwidefsky 	tod_steering_delta += delta;
38875c7b6f3SMartin Schwidefsky 	if ((abs(tod_steering_delta) >> 48) != 0)
389eba8e1afSHeiko Carstens 		panic("TOD clock sync offset %li is too large to drift\n",
39075c7b6f3SMartin Schwidefsky 		      tod_steering_delta);
39175c7b6f3SMartin Schwidefsky 	tod_steering_end = now + (abs(tod_steering_delta) << 15);
3925b43bd18SHeiko Carstens 	for (cs = 0; cs < CS_BASES; cs++) {
3935b43bd18SHeiko Carstens 		vdso_data[cs].arch_data.tod_steering_end = tod_steering_end;
3945b43bd18SHeiko Carstens 		vdso_data[cs].arch_data.tod_steering_delta = tod_steering_delta;
3955b43bd18SHeiko Carstens 	}
3964bff8cb5SSven Schnelle 
397b1c0854dSMartin Schwidefsky 	/* Update LPAR offset. */
398b1c0854dSMartin Schwidefsky 	if (ptff_query(PTFF_QTO) && ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
399b1c0854dSMartin Schwidefsky 		lpar_offset = qto.tod_epoch_difference;
400b1c0854dSMartin Schwidefsky 	/* Call the TOD clock change notifier. */
401b1c0854dSMartin Schwidefsky 	atomic_notifier_call_chain(&s390_epoch_delta_notifier, 0, &delta);
402b1c0854dSMartin Schwidefsky }
403b1c0854dSMartin Schwidefsky 
404b1c0854dSMartin Schwidefsky /*
405b1c0854dSMartin Schwidefsky  * Apply clock delta to the per-CPU data structures of this CPU.
406b1c0854dSMartin Schwidefsky  * This is called for each online CPU after the call to clock_sync_global.
407b1c0854dSMartin Schwidefsky  */
clock_sync_local(long delta)4085ace65ebSSven Schnelle static void clock_sync_local(long delta)
409b1c0854dSMartin Schwidefsky {
410b1c0854dSMartin Schwidefsky 	/* Add the delta to the clock comparator. */
4116e2ef5e4SMartin Schwidefsky 	if (S390_lowcore.clock_comparator != clock_comparator_max) {
412b1c0854dSMartin Schwidefsky 		S390_lowcore.clock_comparator += delta;
413b1c0854dSMartin Schwidefsky 		set_clock_comparator(S390_lowcore.clock_comparator);
414b1c0854dSMartin Schwidefsky 	}
4152ace06ecSMartin Schwidefsky 	/* Adjust the last_update_clock time-stamp. */
4162ace06ecSMartin Schwidefsky 	S390_lowcore.last_update_clock += delta;
417b1c0854dSMartin Schwidefsky }
418b1c0854dSMartin Schwidefsky 
419ca64f639SDavid Hildenbrand /* Single threaded workqueue used for stp sync events */
420750887deSHeiko Carstens static struct workqueue_struct *time_sync_wq;
421750887deSHeiko Carstens 
time_init_wq(void)422750887deSHeiko Carstens static void __init time_init_wq(void)
423750887deSHeiko Carstens {
424179cb81aSHeiko Carstens 	if (time_sync_wq)
425179cb81aSHeiko Carstens 		return;
426750887deSHeiko Carstens 	time_sync_wq = create_singlethread_workqueue("timesync");
427750887deSHeiko Carstens }
428750887deSHeiko Carstens 
429d2fec595SMartin Schwidefsky struct clock_sync_data {
430750887deSHeiko Carstens 	atomic_t cpus;
4315a62b192SHeiko Carstens 	int in_sync;
4325ace65ebSSven Schnelle 	long clock_delta;
433d2fec595SMartin Schwidefsky };
4345a62b192SHeiko Carstens 
435d54853efSMartin Schwidefsky /*
436d2fec595SMartin Schwidefsky  * Server Time Protocol (STP) code.
437d2fec595SMartin Schwidefsky  */
4384cc7ecb7SKees Cook static bool stp_online;
439d2fec595SMartin Schwidefsky static struct stp_sstpi stp_info;
440d2fec595SMartin Schwidefsky static void *stp_page;
441d2fec595SMartin Schwidefsky 
442d2fec595SMartin Schwidefsky static void stp_work_fn(struct work_struct *work);
443d2fec595SMartin Schwidefsky static DECLARE_WORK(stp_work, stp_work_fn);
44404362301SMartin Schwidefsky static struct timer_list stp_timer;
445d2fec595SMartin Schwidefsky 
early_parse_stp(char * p)446d2fec595SMartin Schwidefsky static int __init early_parse_stp(char *p)
447d2fec595SMartin Schwidefsky {
4484cc7ecb7SKees Cook 	return kstrtobool(p, &stp_online);
449d2fec595SMartin Schwidefsky }
450d2fec595SMartin Schwidefsky early_param("stp", early_parse_stp);
451d2fec595SMartin Schwidefsky 
452d2fec595SMartin Schwidefsky /*
453d2fec595SMartin Schwidefsky  * Reset STP attachment.
454d2fec595SMartin Schwidefsky  */
stp_reset(void)4558f847003SHeiko Carstens static void __init stp_reset(void)
456d2fec595SMartin Schwidefsky {
457d2fec595SMartin Schwidefsky 	int rc;
458d2fec595SMartin Schwidefsky 
459d7d1104fSHeiko Carstens 	stp_page = (void *) get_zeroed_page(GFP_ATOMIC);
4602f82f577SMartin Schwidefsky 	rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
4614a672cfaSMartin Schwidefsky 	if (rc == 0)
462d2fec595SMartin Schwidefsky 		set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
463d2fec595SMartin Schwidefsky 	else if (stp_online) {
464baebc70aSJoe Perches 		pr_warn("The real or virtual hardware system does not provide an STP interface\n");
465d7d1104fSHeiko Carstens 		free_page((unsigned long) stp_page);
466d2fec595SMartin Schwidefsky 		stp_page = NULL;
467970ba6acSHeiko Carstens 		stp_online = false;
468d2fec595SMartin Schwidefsky 	}
469d2fec595SMartin Schwidefsky }
470d2fec595SMartin Schwidefsky 
stp_timeout(struct timer_list * unused)471e99e88a9SKees Cook static void stp_timeout(struct timer_list *unused)
47204362301SMartin Schwidefsky {
47304362301SMartin Schwidefsky 	queue_work(time_sync_wq, &stp_work);
47404362301SMartin Schwidefsky }
47504362301SMartin Schwidefsky 
stp_init(void)476d2fec595SMartin Schwidefsky static int __init stp_init(void)
477d2fec595SMartin Schwidefsky {
478750887deSHeiko Carstens 	if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
479750887deSHeiko Carstens 		return 0;
480e99e88a9SKees Cook 	timer_setup(&stp_timer, stp_timeout, 0);
481750887deSHeiko Carstens 	time_init_wq();
482750887deSHeiko Carstens 	if (!stp_online)
483750887deSHeiko Carstens 		return 0;
484750887deSHeiko Carstens 	queue_work(time_sync_wq, &stp_work);
485d2fec595SMartin Schwidefsky 	return 0;
486d2fec595SMartin Schwidefsky }
487d2fec595SMartin Schwidefsky 
488d2fec595SMartin Schwidefsky arch_initcall(stp_init);
489d2fec595SMartin Schwidefsky 
490d2fec595SMartin Schwidefsky /*
491d2fec595SMartin Schwidefsky  * STP timing alert. There are three causes:
492d2fec595SMartin Schwidefsky  * 1) timing status change
493d2fec595SMartin Schwidefsky  * 2) link availability change
494d2fec595SMartin Schwidefsky  * 3) time control parameter change
495d2fec595SMartin Schwidefsky  * In all three cases we are only interested in the clock source state.
496d2fec595SMartin Schwidefsky  * If a STP clock source is now available use it.
497d2fec595SMartin Schwidefsky  */
stp_timing_alert(struct stp_irq_parm * intparm)498d2fec595SMartin Schwidefsky static void stp_timing_alert(struct stp_irq_parm *intparm)
499d2fec595SMartin Schwidefsky {
500d2fec595SMartin Schwidefsky 	if (intparm->tsc || intparm->lac || intparm->tcpc)
501750887deSHeiko Carstens 		queue_work(time_sync_wq, &stp_work);
502d2fec595SMartin Schwidefsky }
503d2fec595SMartin Schwidefsky 
504d2fec595SMartin Schwidefsky /*
505d2fec595SMartin Schwidefsky  * STP sync check machine check. This is called when the timing state
506d2fec595SMartin Schwidefsky  * changes from the synchronized state to the unsynchronized state.
507d2fec595SMartin Schwidefsky  * After a STP sync check the clock is not in sync. The machine check
508d2fec595SMartin Schwidefsky  * is broadcasted to all cpus at the same time.
509d2fec595SMartin Schwidefsky  */
stp_sync_check(void)51029b0a825SHeiko Carstens int stp_sync_check(void)
511d2fec595SMartin Schwidefsky {
512d2fec595SMartin Schwidefsky 	disable_sync_clock(NULL);
51329b0a825SHeiko Carstens 	return 1;
514d2fec595SMartin Schwidefsky }
515d2fec595SMartin Schwidefsky 
516d2fec595SMartin Schwidefsky /*
517d2fec595SMartin Schwidefsky  * STP island condition machine check. This is called when an attached
518d2fec595SMartin Schwidefsky  * server  attempts to communicate over an STP link and the servers
519d2fec595SMartin Schwidefsky  * have matching CTN ids and have a valid stratum-1 configuration
520d2fec595SMartin Schwidefsky  * but the configurations do not match.
521d2fec595SMartin Schwidefsky  */
stp_island_check(void)52229b0a825SHeiko Carstens int stp_island_check(void)
523d2fec595SMartin Schwidefsky {
524d2fec595SMartin Schwidefsky 	disable_sync_clock(NULL);
52529b0a825SHeiko Carstens 	return 1;
526d2fec595SMartin Schwidefsky }
527d2fec595SMartin Schwidefsky 
stp_queue_work(void)52829b0a825SHeiko Carstens void stp_queue_work(void)
52929b0a825SHeiko Carstens {
53029b0a825SHeiko Carstens 	queue_work(time_sync_wq, &stp_work);
53129b0a825SHeiko Carstens }
532750887deSHeiko Carstens 
__store_stpinfo(void)533b3bd0249SSven Schnelle static int __store_stpinfo(void)
534b3bd0249SSven Schnelle {
535b3bd0249SSven Schnelle 	int rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
536b3bd0249SSven Schnelle 
537b3bd0249SSven Schnelle 	if (rc)
538b3bd0249SSven Schnelle 		clear_bit(CLOCK_SYNC_STPINFO_VALID, &clock_sync_flags);
539b3bd0249SSven Schnelle 	else
540b3bd0249SSven Schnelle 		set_bit(CLOCK_SYNC_STPINFO_VALID, &clock_sync_flags);
541b3bd0249SSven Schnelle 	return rc;
542b3bd0249SSven Schnelle }
543b3bd0249SSven Schnelle 
stpinfo_valid(void)544b3bd0249SSven Schnelle static int stpinfo_valid(void)
545b3bd0249SSven Schnelle {
546b3bd0249SSven Schnelle 	return stp_online && test_bit(CLOCK_SYNC_STPINFO_VALID, &clock_sync_flags);
547b3bd0249SSven Schnelle }
548b3bd0249SSven Schnelle 
stp_sync_clock(void * data)549750887deSHeiko Carstens static int stp_sync_clock(void *data)
550d2fec595SMartin Schwidefsky {
551b1c0854dSMartin Schwidefsky 	struct clock_sync_data *sync = data;
5525ace65ebSSven Schnelle 	long clock_delta, flags;
553b1c0854dSMartin Schwidefsky 	static int first;
554d2fec595SMartin Schwidefsky 	int rc;
555d2fec595SMartin Schwidefsky 
556d2fec595SMartin Schwidefsky 	enable_sync_clock();
557b1c0854dSMartin Schwidefsky 	if (xchg(&first, 1) == 0) {
558b1c0854dSMartin Schwidefsky 		/* Wait until all other cpus entered the sync function. */
559b1c0854dSMartin Schwidefsky 		while (atomic_read(&sync->cpus) != 0)
560b1c0854dSMartin Schwidefsky 			cpu_relax();
561d2fec595SMartin Schwidefsky 		rc = 0;
56203780c83SSven Schnelle 		if (stp_info.todoff || stp_info.tmd != 2) {
5634bff8cb5SSven Schnelle 			flags = vdso_update_begin();
564b1c0854dSMartin Schwidefsky 			rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0,
565b1c0854dSMartin Schwidefsky 					&clock_delta);
566d2fec595SMartin Schwidefsky 			if (rc == 0) {
567b1c0854dSMartin Schwidefsky 				sync->clock_delta = clock_delta;
568b1c0854dSMartin Schwidefsky 				clock_sync_global(clock_delta);
569b3bd0249SSven Schnelle 				rc = __store_stpinfo();
570d2fec595SMartin Schwidefsky 				if (rc == 0 && stp_info.tmd != 2)
571d2fec595SMartin Schwidefsky 					rc = -EAGAIN;
572d2fec595SMartin Schwidefsky 			}
5734bff8cb5SSven Schnelle 			vdso_update_end(flags);
574d2fec595SMartin Schwidefsky 		}
575b1c0854dSMartin Schwidefsky 		sync->in_sync = rc ? -EAGAIN : 1;
576750887deSHeiko Carstens 		xchg(&first, 0);
577b1c0854dSMartin Schwidefsky 	} else {
578b1c0854dSMartin Schwidefsky 		/* Slave */
579b1c0854dSMartin Schwidefsky 		atomic_dec(&sync->cpus);
580b1c0854dSMartin Schwidefsky 		/* Wait for in_sync to be set. */
581b1c0854dSMartin Schwidefsky 		while (READ_ONCE(sync->in_sync) == 0)
582b1c0854dSMartin Schwidefsky 			__udelay(1);
583b1c0854dSMartin Schwidefsky 	}
584b1c0854dSMartin Schwidefsky 	if (sync->in_sync != 1)
585b1c0854dSMartin Schwidefsky 		/* Didn't work. Clear per-cpu in sync bit again. */
586b1c0854dSMartin Schwidefsky 		disable_sync_clock(NULL);
587b1c0854dSMartin Schwidefsky 	/* Apply clock delta to per-CPU fields of this CPU. */
588b1c0854dSMartin Schwidefsky 	clock_sync_local(sync->clock_delta);
589b1c0854dSMartin Schwidefsky 
590750887deSHeiko Carstens 	return 0;
591750887deSHeiko Carstens }
592d2fec595SMartin Schwidefsky 
stp_clear_leap(void)593b2539aa0SSven Schnelle static int stp_clear_leap(void)
594b2539aa0SSven Schnelle {
595b2539aa0SSven Schnelle 	struct __kernel_timex txc;
596b2539aa0SSven Schnelle 	int ret;
597b2539aa0SSven Schnelle 
598b2539aa0SSven Schnelle 	memset(&txc, 0, sizeof(txc));
599b2539aa0SSven Schnelle 
600b2539aa0SSven Schnelle 	ret = do_adjtimex(&txc);
601b2539aa0SSven Schnelle 	if (ret < 0)
602b2539aa0SSven Schnelle 		return ret;
603b2539aa0SSven Schnelle 
604b2539aa0SSven Schnelle 	txc.modes = ADJ_STATUS;
605b2539aa0SSven Schnelle 	txc.status &= ~(STA_INS|STA_DEL);
606b2539aa0SSven Schnelle 	return do_adjtimex(&txc);
607b2539aa0SSven Schnelle }
608b2539aa0SSven Schnelle 
stp_check_leap(void)609b2539aa0SSven Schnelle static void stp_check_leap(void)
610b2539aa0SSven Schnelle {
611b2539aa0SSven Schnelle 	struct stp_stzi stzi;
612b2539aa0SSven Schnelle 	struct stp_lsoib *lsoib = &stzi.lsoib;
613b2539aa0SSven Schnelle 	struct __kernel_timex txc;
614b2539aa0SSven Schnelle 	int64_t timediff;
615b2539aa0SSven Schnelle 	int leapdiff, ret;
616b2539aa0SSven Schnelle 
617b2539aa0SSven Schnelle 	if (!stp_info.lu || !check_sync_clock()) {
618b2539aa0SSven Schnelle 		/*
619b2539aa0SSven Schnelle 		 * Either a scheduled leap second was removed by the operator,
620b2539aa0SSven Schnelle 		 * or STP is out of sync. In both cases, clear the leap second
621b2539aa0SSven Schnelle 		 * kernel flags.
622b2539aa0SSven Schnelle 		 */
623b2539aa0SSven Schnelle 		if (stp_clear_leap() < 0)
624b2539aa0SSven Schnelle 			pr_err("failed to clear leap second flags\n");
625b2539aa0SSven Schnelle 		return;
626b2539aa0SSven Schnelle 	}
627b2539aa0SSven Schnelle 
628b2539aa0SSven Schnelle 	if (chsc_stzi(stp_page, &stzi, sizeof(stzi))) {
629b2539aa0SSven Schnelle 		pr_err("stzi failed\n");
630b2539aa0SSven Schnelle 		return;
631b2539aa0SSven Schnelle 	}
632b2539aa0SSven Schnelle 
633b2539aa0SSven Schnelle 	timediff = tod_to_ns(lsoib->nlsout - get_tod_clock()) / NSEC_PER_SEC;
634b2539aa0SSven Schnelle 	leapdiff = lsoib->nlso - lsoib->also;
635b2539aa0SSven Schnelle 
636b2539aa0SSven Schnelle 	if (leapdiff != 1 && leapdiff != -1) {
637b2539aa0SSven Schnelle 		pr_err("Cannot schedule %d leap seconds\n", leapdiff);
638b2539aa0SSven Schnelle 		return;
639b2539aa0SSven Schnelle 	}
640b2539aa0SSven Schnelle 
641b2539aa0SSven Schnelle 	if (timediff < 0) {
642b2539aa0SSven Schnelle 		if (stp_clear_leap() < 0)
643b2539aa0SSven Schnelle 			pr_err("failed to clear leap second flags\n");
644b2539aa0SSven Schnelle 	} else if (timediff < 7200) {
645b2539aa0SSven Schnelle 		memset(&txc, 0, sizeof(txc));
646b2539aa0SSven Schnelle 		ret = do_adjtimex(&txc);
647b2539aa0SSven Schnelle 		if (ret < 0)
648b2539aa0SSven Schnelle 			return;
649b2539aa0SSven Schnelle 
650b2539aa0SSven Schnelle 		txc.modes = ADJ_STATUS;
651b2539aa0SSven Schnelle 		if (leapdiff > 0)
652b2539aa0SSven Schnelle 			txc.status |= STA_INS;
653b2539aa0SSven Schnelle 		else
654b2539aa0SSven Schnelle 			txc.status |= STA_DEL;
655b2539aa0SSven Schnelle 		ret = do_adjtimex(&txc);
656b2539aa0SSven Schnelle 		if (ret < 0)
657b2539aa0SSven Schnelle 			pr_err("failed to set leap second flags\n");
658b2539aa0SSven Schnelle 		/* arm Timer to clear leap second flags */
659b2539aa0SSven Schnelle 		mod_timer(&stp_timer, jiffies + msecs_to_jiffies(14400 * MSEC_PER_SEC));
660b2539aa0SSven Schnelle 	} else {
661b2539aa0SSven Schnelle 		/* The day the leap second is scheduled for hasn't been reached. Retry
662b2539aa0SSven Schnelle 		 * in one hour.
663b2539aa0SSven Schnelle 		 */
664b2539aa0SSven Schnelle 		mod_timer(&stp_timer, jiffies + msecs_to_jiffies(3600 * MSEC_PER_SEC));
665b2539aa0SSven Schnelle 	}
666b2539aa0SSven Schnelle }
667b2539aa0SSven Schnelle 
668750887deSHeiko Carstens /*
669750887deSHeiko Carstens  * STP work. Check for the STP state and take over the clock
670750887deSHeiko Carstens  * synchronization if the STP clock source is usable.
671750887deSHeiko Carstens  */
stp_work_fn(struct work_struct * work)672750887deSHeiko Carstens static void stp_work_fn(struct work_struct *work)
673750887deSHeiko Carstens {
674750887deSHeiko Carstens 	struct clock_sync_data stp_sync;
675750887deSHeiko Carstens 	int rc;
676750887deSHeiko Carstens 
6770b3016b7SMartin Schwidefsky 	/* prevent multiple execution. */
678ad5ceb33SSven Schnelle 	mutex_lock(&stp_mutex);
6790b3016b7SMartin Schwidefsky 
680750887deSHeiko Carstens 	if (!stp_online) {
6812f82f577SMartin Schwidefsky 		chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
68204362301SMartin Schwidefsky 		del_timer_sync(&stp_timer);
6830b3016b7SMartin Schwidefsky 		goto out_unlock;
684750887deSHeiko Carstens 	}
685750887deSHeiko Carstens 
686b2539aa0SSven Schnelle 	rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xf0e0, NULL);
687750887deSHeiko Carstens 	if (rc)
6880b3016b7SMartin Schwidefsky 		goto out_unlock;
689750887deSHeiko Carstens 
690b3bd0249SSven Schnelle 	rc = __store_stpinfo();
691750887deSHeiko Carstens 	if (rc || stp_info.c == 0)
6920b3016b7SMartin Schwidefsky 		goto out_unlock;
693750887deSHeiko Carstens 
6948283cb43SMartin Schwidefsky 	/* Skip synchronization if the clock is already in sync. */
695b2539aa0SSven Schnelle 	if (!check_sync_clock()) {
696750887deSHeiko Carstens 		memset(&stp_sync, 0, sizeof(stp_sync));
6972337e879SSebastian Andrzej Siewior 		cpus_read_lock();
698750887deSHeiko Carstens 		atomic_set(&stp_sync.cpus, num_online_cpus() - 1);
6992337e879SSebastian Andrzej Siewior 		stop_machine_cpuslocked(stp_sync_clock, &stp_sync, cpu_online_mask);
7002337e879SSebastian Andrzej Siewior 		cpus_read_unlock();
701b2539aa0SSven Schnelle 	}
7020b3016b7SMartin Schwidefsky 
70304362301SMartin Schwidefsky 	if (!check_sync_clock())
70404362301SMartin Schwidefsky 		/*
705*cada938aSHeiko Carstens 		 * There is a usable clock but the synchronization failed.
70604362301SMartin Schwidefsky 		 * Retry after a second.
70704362301SMartin Schwidefsky 		 */
7080188d08aSSven Schnelle 		mod_timer(&stp_timer, jiffies + msecs_to_jiffies(MSEC_PER_SEC));
709b2539aa0SSven Schnelle 	else if (stp_info.lu)
710b2539aa0SSven Schnelle 		stp_check_leap();
71104362301SMartin Schwidefsky 
7120b3016b7SMartin Schwidefsky out_unlock:
713ad5ceb33SSven Schnelle 	mutex_unlock(&stp_mutex);
714d2fec595SMartin Schwidefsky }
715d2fec595SMartin Schwidefsky 
716d2fec595SMartin Schwidefsky /*
7173fbacffbSKay Sievers  * STP subsys sysfs interface functions
718d2fec595SMartin Schwidefsky  */
7193fbacffbSKay Sievers static struct bus_type stp_subsys = {
720d2fec595SMartin Schwidefsky 	.name		= "stp",
7213fbacffbSKay Sievers 	.dev_name	= "stp",
722d2fec595SMartin Schwidefsky };
723d2fec595SMartin Schwidefsky 
ctn_id_show(struct device * dev,struct device_attribute * attr,char * buf)72466a049b7SSven Schnelle static ssize_t ctn_id_show(struct device *dev,
7253fbacffbSKay Sievers 				struct device_attribute *attr,
726c9be0a36SAndi Kleen 				char *buf)
727d2fec595SMartin Schwidefsky {
728b3bd0249SSven Schnelle 	ssize_t ret = -ENODATA;
729b3bd0249SSven Schnelle 
730ad5ceb33SSven Schnelle 	mutex_lock(&stp_mutex);
731b3bd0249SSven Schnelle 	if (stpinfo_valid())
732eba8e1afSHeiko Carstens 		ret = sprintf(buf, "%016lx\n",
733eba8e1afSHeiko Carstens 			      *(unsigned long *) stp_info.ctnid);
734ad5ceb33SSven Schnelle 	mutex_unlock(&stp_mutex);
735b3bd0249SSven Schnelle 	return ret;
736d2fec595SMartin Schwidefsky }
737d2fec595SMartin Schwidefsky 
73866a049b7SSven Schnelle static DEVICE_ATTR_RO(ctn_id);
739d2fec595SMartin Schwidefsky 
ctn_type_show(struct device * dev,struct device_attribute * attr,char * buf)74066a049b7SSven Schnelle static ssize_t ctn_type_show(struct device *dev,
7413fbacffbSKay Sievers 				struct device_attribute *attr,
742c9be0a36SAndi Kleen 				char *buf)
743d2fec595SMartin Schwidefsky {
744b3bd0249SSven Schnelle 	ssize_t ret = -ENODATA;
745b3bd0249SSven Schnelle 
746ad5ceb33SSven Schnelle 	mutex_lock(&stp_mutex);
747b3bd0249SSven Schnelle 	if (stpinfo_valid())
748b3bd0249SSven Schnelle 		ret = sprintf(buf, "%i\n", stp_info.ctn);
749ad5ceb33SSven Schnelle 	mutex_unlock(&stp_mutex);
750b3bd0249SSven Schnelle 	return ret;
751d2fec595SMartin Schwidefsky }
752d2fec595SMartin Schwidefsky 
75366a049b7SSven Schnelle static DEVICE_ATTR_RO(ctn_type);
754d2fec595SMartin Schwidefsky 
dst_offset_show(struct device * dev,struct device_attribute * attr,char * buf)75566a049b7SSven Schnelle static ssize_t dst_offset_show(struct device *dev,
7563fbacffbSKay Sievers 				   struct device_attribute *attr,
757c9be0a36SAndi Kleen 				   char *buf)
758d2fec595SMartin Schwidefsky {
759b3bd0249SSven Schnelle 	ssize_t ret = -ENODATA;
760b3bd0249SSven Schnelle 
761ad5ceb33SSven Schnelle 	mutex_lock(&stp_mutex);
762b3bd0249SSven Schnelle 	if (stpinfo_valid() && (stp_info.vbits & 0x2000))
763b3bd0249SSven Schnelle 		ret = sprintf(buf, "%i\n", (int)(s16) stp_info.dsto);
764ad5ceb33SSven Schnelle 	mutex_unlock(&stp_mutex);
765b3bd0249SSven Schnelle 	return ret;
766d2fec595SMartin Schwidefsky }
767d2fec595SMartin Schwidefsky 
76866a049b7SSven Schnelle static DEVICE_ATTR_RO(dst_offset);
769d2fec595SMartin Schwidefsky 
leap_seconds_show(struct device * dev,struct device_attribute * attr,char * buf)77066a049b7SSven Schnelle static ssize_t leap_seconds_show(struct device *dev,
7713fbacffbSKay Sievers 					struct device_attribute *attr,
772c9be0a36SAndi Kleen 					char *buf)
773d2fec595SMartin Schwidefsky {
774b3bd0249SSven Schnelle 	ssize_t ret = -ENODATA;
775b3bd0249SSven Schnelle 
776ad5ceb33SSven Schnelle 	mutex_lock(&stp_mutex);
777b3bd0249SSven Schnelle 	if (stpinfo_valid() && (stp_info.vbits & 0x8000))
778b3bd0249SSven Schnelle 		ret = sprintf(buf, "%i\n", (int)(s16) stp_info.leaps);
779ad5ceb33SSven Schnelle 	mutex_unlock(&stp_mutex);
780b3bd0249SSven Schnelle 	return ret;
781d2fec595SMartin Schwidefsky }
782d2fec595SMartin Schwidefsky 
78366a049b7SSven Schnelle static DEVICE_ATTR_RO(leap_seconds);
784d2fec595SMartin Schwidefsky 
leap_seconds_scheduled_show(struct device * dev,struct device_attribute * attr,char * buf)7854fb53ddeSSven Schnelle static ssize_t leap_seconds_scheduled_show(struct device *dev,
7864fb53ddeSSven Schnelle 						struct device_attribute *attr,
7874fb53ddeSSven Schnelle 						char *buf)
7884fb53ddeSSven Schnelle {
7894fb53ddeSSven Schnelle 	struct stp_stzi stzi;
7904fb53ddeSSven Schnelle 	ssize_t ret;
7914fb53ddeSSven Schnelle 
792ad5ceb33SSven Schnelle 	mutex_lock(&stp_mutex);
7934fb53ddeSSven Schnelle 	if (!stpinfo_valid() || !(stp_info.vbits & 0x8000) || !stp_info.lu) {
794ad5ceb33SSven Schnelle 		mutex_unlock(&stp_mutex);
7954fb53ddeSSven Schnelle 		return -ENODATA;
7964fb53ddeSSven Schnelle 	}
7974fb53ddeSSven Schnelle 
7984fb53ddeSSven Schnelle 	ret = chsc_stzi(stp_page, &stzi, sizeof(stzi));
799ad5ceb33SSven Schnelle 	mutex_unlock(&stp_mutex);
8004fb53ddeSSven Schnelle 	if (ret < 0)
8014fb53ddeSSven Schnelle 		return ret;
8024fb53ddeSSven Schnelle 
8034fb53ddeSSven Schnelle 	if (!stzi.lsoib.p)
8044fb53ddeSSven Schnelle 		return sprintf(buf, "0,0\n");
8054fb53ddeSSven Schnelle 
806eba8e1afSHeiko Carstens 	return sprintf(buf, "%lu,%d\n",
8074fb53ddeSSven Schnelle 		       tod_to_ns(stzi.lsoib.nlsout - TOD_UNIX_EPOCH) / NSEC_PER_SEC,
8084fb53ddeSSven Schnelle 		       stzi.lsoib.nlso - stzi.lsoib.also);
8094fb53ddeSSven Schnelle }
8104fb53ddeSSven Schnelle 
8114fb53ddeSSven Schnelle static DEVICE_ATTR_RO(leap_seconds_scheduled);
8124fb53ddeSSven Schnelle 
stratum_show(struct device * dev,struct device_attribute * attr,char * buf)81366a049b7SSven Schnelle static ssize_t stratum_show(struct device *dev,
8143fbacffbSKay Sievers 				struct device_attribute *attr,
815c9be0a36SAndi Kleen 				char *buf)
816d2fec595SMartin Schwidefsky {
817b3bd0249SSven Schnelle 	ssize_t ret = -ENODATA;
818b3bd0249SSven Schnelle 
819ad5ceb33SSven Schnelle 	mutex_lock(&stp_mutex);
820b3bd0249SSven Schnelle 	if (stpinfo_valid())
821b3bd0249SSven Schnelle 		ret = sprintf(buf, "%i\n", (int)(s16) stp_info.stratum);
822ad5ceb33SSven Schnelle 	mutex_unlock(&stp_mutex);
823b3bd0249SSven Schnelle 	return ret;
824d2fec595SMartin Schwidefsky }
825d2fec595SMartin Schwidefsky 
82666a049b7SSven Schnelle static DEVICE_ATTR_RO(stratum);
827d2fec595SMartin Schwidefsky 
time_offset_show(struct device * dev,struct device_attribute * attr,char * buf)82866a049b7SSven Schnelle static ssize_t time_offset_show(struct device *dev,
8293fbacffbSKay Sievers 				struct device_attribute *attr,
830c9be0a36SAndi Kleen 				char *buf)
831d2fec595SMartin Schwidefsky {
832b3bd0249SSven Schnelle 	ssize_t ret = -ENODATA;
833b3bd0249SSven Schnelle 
834ad5ceb33SSven Schnelle 	mutex_lock(&stp_mutex);
835b3bd0249SSven Schnelle 	if (stpinfo_valid() && (stp_info.vbits & 0x0800))
836b3bd0249SSven Schnelle 		ret = sprintf(buf, "%i\n", (int) stp_info.tto);
837ad5ceb33SSven Schnelle 	mutex_unlock(&stp_mutex);
838b3bd0249SSven Schnelle 	return ret;
839d2fec595SMartin Schwidefsky }
840d2fec595SMartin Schwidefsky 
84166a049b7SSven Schnelle static DEVICE_ATTR_RO(time_offset);
842d2fec595SMartin Schwidefsky 
time_zone_offset_show(struct device * dev,struct device_attribute * attr,char * buf)84366a049b7SSven Schnelle static ssize_t time_zone_offset_show(struct device *dev,
8443fbacffbSKay Sievers 				struct device_attribute *attr,
845c9be0a36SAndi Kleen 				char *buf)
846d2fec595SMartin Schwidefsky {
847b3bd0249SSven Schnelle 	ssize_t ret = -ENODATA;
848b3bd0249SSven Schnelle 
849ad5ceb33SSven Schnelle 	mutex_lock(&stp_mutex);
850b3bd0249SSven Schnelle 	if (stpinfo_valid() && (stp_info.vbits & 0x4000))
851b3bd0249SSven Schnelle 		ret = sprintf(buf, "%i\n", (int)(s16) stp_info.tzo);
852ad5ceb33SSven Schnelle 	mutex_unlock(&stp_mutex);
853b3bd0249SSven Schnelle 	return ret;
854d2fec595SMartin Schwidefsky }
855d2fec595SMartin Schwidefsky 
85666a049b7SSven Schnelle static DEVICE_ATTR_RO(time_zone_offset);
857d2fec595SMartin Schwidefsky 
timing_mode_show(struct device * dev,struct device_attribute * attr,char * buf)85866a049b7SSven Schnelle static ssize_t timing_mode_show(struct device *dev,
8593fbacffbSKay Sievers 				struct device_attribute *attr,
860c9be0a36SAndi Kleen 				char *buf)
861d2fec595SMartin Schwidefsky {
862b3bd0249SSven Schnelle 	ssize_t ret = -ENODATA;
863b3bd0249SSven Schnelle 
864ad5ceb33SSven Schnelle 	mutex_lock(&stp_mutex);
865b3bd0249SSven Schnelle 	if (stpinfo_valid())
866b3bd0249SSven Schnelle 		ret = sprintf(buf, "%i\n", stp_info.tmd);
867ad5ceb33SSven Schnelle 	mutex_unlock(&stp_mutex);
868b3bd0249SSven Schnelle 	return ret;
869d2fec595SMartin Schwidefsky }
870d2fec595SMartin Schwidefsky 
87166a049b7SSven Schnelle static DEVICE_ATTR_RO(timing_mode);
872d2fec595SMartin Schwidefsky 
timing_state_show(struct device * dev,struct device_attribute * attr,char * buf)87366a049b7SSven Schnelle static ssize_t timing_state_show(struct device *dev,
8743fbacffbSKay Sievers 				struct device_attribute *attr,
875c9be0a36SAndi Kleen 				char *buf)
876d2fec595SMartin Schwidefsky {
877b3bd0249SSven Schnelle 	ssize_t ret = -ENODATA;
878b3bd0249SSven Schnelle 
879ad5ceb33SSven Schnelle 	mutex_lock(&stp_mutex);
880b3bd0249SSven Schnelle 	if (stpinfo_valid())
881b3bd0249SSven Schnelle 		ret = sprintf(buf, "%i\n", stp_info.tst);
882ad5ceb33SSven Schnelle 	mutex_unlock(&stp_mutex);
883b3bd0249SSven Schnelle 	return ret;
884d2fec595SMartin Schwidefsky }
885d2fec595SMartin Schwidefsky 
88666a049b7SSven Schnelle static DEVICE_ATTR_RO(timing_state);
887d2fec595SMartin Schwidefsky 
online_show(struct device * dev,struct device_attribute * attr,char * buf)88866a049b7SSven Schnelle static ssize_t online_show(struct device *dev,
8893fbacffbSKay Sievers 				struct device_attribute *attr,
890c9be0a36SAndi Kleen 				char *buf)
891d2fec595SMartin Schwidefsky {
892d2fec595SMartin Schwidefsky 	return sprintf(buf, "%i\n", stp_online);
893d2fec595SMartin Schwidefsky }
894d2fec595SMartin Schwidefsky 
online_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)89566a049b7SSven Schnelle static ssize_t online_store(struct device *dev,
8963fbacffbSKay Sievers 				struct device_attribute *attr,
897d2fec595SMartin Schwidefsky 				const char *buf, size_t count)
898d2fec595SMartin Schwidefsky {
899d2fec595SMartin Schwidefsky 	unsigned int value;
900d2fec595SMartin Schwidefsky 
901d2fec595SMartin Schwidefsky 	value = simple_strtoul(buf, NULL, 0);
902d2fec595SMartin Schwidefsky 	if (value != 0 && value != 1)
903d2fec595SMartin Schwidefsky 		return -EINVAL;
904d2fec595SMartin Schwidefsky 	if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
905d2fec595SMartin Schwidefsky 		return -EOPNOTSUPP;
906ad5ceb33SSven Schnelle 	mutex_lock(&stp_mutex);
907d2fec595SMartin Schwidefsky 	stp_online = value;
9088283cb43SMartin Schwidefsky 	if (stp_online)
9098283cb43SMartin Schwidefsky 		set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
9108283cb43SMartin Schwidefsky 	else
9118283cb43SMartin Schwidefsky 		clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
912750887deSHeiko Carstens 	queue_work(time_sync_wq, &stp_work);
913ad5ceb33SSven Schnelle 	mutex_unlock(&stp_mutex);
914d2fec595SMartin Schwidefsky 	return count;
915d2fec595SMartin Schwidefsky }
916d2fec595SMartin Schwidefsky 
917d2fec595SMartin Schwidefsky /*
9183fbacffbSKay Sievers  * Can't use DEVICE_ATTR because the attribute should be named
9193fbacffbSKay Sievers  * stp/online but dev_attr_online already exists in this file ..
920d2fec595SMartin Schwidefsky  */
92166a049b7SSven Schnelle static DEVICE_ATTR_RW(online);
922d2fec595SMartin Schwidefsky 
923074ff04eSJulian Wiedmann static struct attribute *stp_dev_attrs[] = {
924074ff04eSJulian Wiedmann 	&dev_attr_ctn_id.attr,
925074ff04eSJulian Wiedmann 	&dev_attr_ctn_type.attr,
926074ff04eSJulian Wiedmann 	&dev_attr_dst_offset.attr,
927074ff04eSJulian Wiedmann 	&dev_attr_leap_seconds.attr,
928074ff04eSJulian Wiedmann 	&dev_attr_online.attr,
929074ff04eSJulian Wiedmann 	&dev_attr_leap_seconds_scheduled.attr,
930074ff04eSJulian Wiedmann 	&dev_attr_stratum.attr,
931074ff04eSJulian Wiedmann 	&dev_attr_time_offset.attr,
932074ff04eSJulian Wiedmann 	&dev_attr_time_zone_offset.attr,
933074ff04eSJulian Wiedmann 	&dev_attr_timing_mode.attr,
934074ff04eSJulian Wiedmann 	&dev_attr_timing_state.attr,
935d2fec595SMartin Schwidefsky 	NULL
936d2fec595SMartin Schwidefsky };
937074ff04eSJulian Wiedmann ATTRIBUTE_GROUPS(stp_dev);
938d2fec595SMartin Schwidefsky 
stp_init_sysfs(void)939d2fec595SMartin Schwidefsky static int __init stp_init_sysfs(void)
940d2fec595SMartin Schwidefsky {
941074ff04eSJulian Wiedmann 	return subsys_system_register(&stp_subsys, stp_dev_groups);
942d2fec595SMartin Schwidefsky }
943d2fec595SMartin Schwidefsky 
944d2fec595SMartin Schwidefsky device_initcall(stp_init_sysfs);
945