]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/s390/kernel/time.c
s390/time: remove ETR support
[mirror_ubuntu-bionic-kernel.git] / arch / s390 / kernel / time.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Time of day based timer functions.
3 *
4 * S390 version
d2fec595 5 * Copyright IBM Corp. 1999, 2008
1da177e4
LT
6 * Author(s): Hartmut Penner (hp@de.ibm.com),
7 * Martin Schwidefsky (schwidefsky@de.ibm.com),
8 * Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
9 *
10 * Derived from "arch/i386/kernel/time.c"
11 * Copyright (C) 1991, 1992, 1995 Linus Torvalds
12 */
13
feab6501
MS
14#define KMSG_COMPONENT "time"
15#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16
052ff461 17#include <linux/kernel_stat.h>
1da177e4
LT
18#include <linux/errno.h>
19#include <linux/module.h>
20#include <linux/sched.h>
21#include <linux/kernel.h>
22#include <linux/param.h>
23#include <linux/string.h>
24#include <linux/mm.h>
25#include <linux/interrupt.h>
750887de
HC
26#include <linux/cpu.h>
27#include <linux/stop_machine.h>
1da177e4 28#include <linux/time.h>
3fbacffb 29#include <linux/device.h>
1da177e4
LT
30#include <linux/delay.h>
31#include <linux/init.h>
32#include <linux/smp.h>
33#include <linux/types.h>
34#include <linux/profile.h>
35#include <linux/timex.h>
36#include <linux/notifier.h>
189374ae 37#include <linux/timekeeper_internal.h>
5a62b192 38#include <linux/clockchips.h>
5a0e3ad6 39#include <linux/gfp.h>
860dba45 40#include <linux/kprobes.h>
1da177e4 41#include <asm/uaccess.h>
40277891 42#include <asm/facility.h>
1da177e4 43#include <asm/delay.h>
1da177e4 44#include <asm/div64.h>
b020632e 45#include <asm/vdso.h>
1da177e4 46#include <asm/irq.h>
5a489b98 47#include <asm/irq_regs.h>
27f6b416 48#include <asm/vtimer.h>
fd5ada04 49#include <asm/stp.h>
a806170e 50#include <asm/cio.h>
638ad34a 51#include "entry.h"
1da177e4
LT
52
53/* change this if you have some constant time drift */
54#define USECS_PER_JIFFY ((unsigned long) 1000000/HZ)
55#define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)
56
b6112ccb 57u64 sched_clock_base_cc = -1; /* Force to data section. */
05e7ff7d 58EXPORT_SYMBOL_GPL(sched_clock_base_cc);
b6112ccb 59
5a62b192 60static DEFINE_PER_CPU(struct clock_event_device, comparators);
1da177e4 61
fdf03650
FZ
62ATOMIC_NOTIFIER_HEAD(s390_epoch_delta_notifier);
63EXPORT_SYMBOL(s390_epoch_delta_notifier);
64
40277891
MS
65unsigned char ptff_function_mask[16];
66unsigned long lpar_offset;
936cc855 67unsigned long initial_leap_seconds;
40277891
MS
68
69/*
70 * Get time offsets with PTFF
71 */
72void __init ptff_init(void)
73{
74 struct ptff_qto qto;
936cc855 75 struct ptff_qui qui;
40277891
MS
76
77 if (!test_facility(28))
78 return;
79 ptff(&ptff_function_mask, sizeof(ptff_function_mask), PTFF_QAF);
80
81 /* get LPAR offset */
82 if (ptff_query(PTFF_QTO) && ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
83 lpar_offset = qto.tod_epoch_difference;
936cc855
MS
84
85 /* get initial leap seconds */
86 if (ptff_query(PTFF_QUI) && ptff(&qui, sizeof(qui), PTFF_QUI) == 0)
87 initial_leap_seconds = (unsigned long)
88 ((long) qui.old_leap * 4096000000L);
40277891
MS
89}
90
1da177e4
LT
91/*
92 * Scheduler clock - returns current time in nanosec units.
93 */
7a5388de 94unsigned long long notrace sched_clock(void)
1da177e4 95{
1aae0560 96 return tod_to_ns(get_tod_clock_monotonic());
1da177e4 97}
7a5388de 98NOKPROBE_SYMBOL(sched_clock);
1da177e4 99
32f65f27
JG
100/*
101 * Monotonic_clock - returns # of nanoseconds passed since time_init()
102 */
103unsigned long long monotonic_clock(void)
104{
105 return sched_clock();
106}
107EXPORT_SYMBOL(monotonic_clock);
108
689911c7 109void tod_to_timeval(__u64 todval, struct timespec64 *xt)
1da177e4
LT
110{
111 unsigned long long sec;
112
113 sec = todval >> 12;
114 do_div(sec, 1000000);
b1e2ba8d 115 xt->tv_sec = sec;
1da177e4 116 todval -= (sec * 1000000) << 12;
b1e2ba8d 117 xt->tv_nsec = ((todval * 1000) >> 12);
1da177e4 118}
b592e89a 119EXPORT_SYMBOL(tod_to_timeval);
1da177e4 120
5a62b192 121void clock_comparator_work(void)
1da177e4 122{
5a62b192 123 struct clock_event_device *cd;
1da177e4 124
5a62b192 125 S390_lowcore.clock_comparator = -1ULL;
eb7e7d76 126 cd = this_cpu_ptr(&comparators);
5a62b192 127 cd->event_handler(cd);
1da177e4
LT
128}
129
1da177e4 130/*
5a62b192 131 * Fixup the clock comparator.
1da177e4 132 */
5a62b192 133static void fixup_clock_comparator(unsigned long long delta)
1da177e4 134{
5a62b192
HC
135 /* If nobody is waiting there's nothing to fix. */
136 if (S390_lowcore.clock_comparator == -1ULL)
1da177e4 137 return;
5a62b192
HC
138 S390_lowcore.clock_comparator += delta;
139 set_clock_comparator(S390_lowcore.clock_comparator);
1da177e4
LT
140}
141
8adbf78e 142static int s390_next_event(unsigned long delta,
5a62b192 143 struct clock_event_device *evt)
1da177e4 144{
8adbf78e 145 S390_lowcore.clock_comparator = get_tod_clock() + delta;
5a62b192
HC
146 set_clock_comparator(S390_lowcore.clock_comparator);
147 return 0;
1da177e4
LT
148}
149
d54853ef
MS
150/*
151 * Set up lowcore and control register of the current cpu to
152 * enable TOD clock and clock comparator interrupts.
1da177e4
LT
153 */
154void init_cpu_timer(void)
155{
5a62b192
HC
156 struct clock_event_device *cd;
157 int cpu;
158
159 S390_lowcore.clock_comparator = -1ULL;
160 set_clock_comparator(S390_lowcore.clock_comparator);
161
162 cpu = smp_processor_id();
163 cd = &per_cpu(comparators, cpu);
164 cd->name = "comparator";
8adbf78e 165 cd->features = CLOCK_EVT_FEAT_ONESHOT;
5a62b192
HC
166 cd->mult = 16777;
167 cd->shift = 12;
168 cd->min_delta_ns = 1;
169 cd->max_delta_ns = LONG_MAX;
170 cd->rating = 400;
320ab2b0 171 cd->cpumask = cpumask_of(cpu);
8adbf78e 172 cd->set_next_event = s390_next_event;
5a62b192
HC
173
174 clockevents_register_device(cd);
d54853ef
MS
175
176 /* Enable clock comparator timer interrupt. */
177 __ctl_set_bit(0,11);
178
d2fec595 179 /* Always allow the timing alert external interrupt. */
d54853ef
MS
180 __ctl_set_bit(0, 4);
181}
182
fde15c3a 183static void clock_comparator_interrupt(struct ext_code ext_code,
f6649a7e
MS
184 unsigned int param32,
185 unsigned long param64)
d54853ef 186{
420f42ec 187 inc_irq_stat(IRQEXT_CLK);
d3d238c7
HC
188 if (S390_lowcore.clock_comparator == -1ULL)
189 set_clock_comparator(S390_lowcore.clock_comparator);
d54853ef
MS
190}
191
d2fec595
MS
192static void stp_timing_alert(struct stp_irq_parm *);
193
fde15c3a 194static void timing_alert_interrupt(struct ext_code ext_code,
f6649a7e 195 unsigned int param32, unsigned long param64)
d2fec595 196{
420f42ec 197 inc_irq_stat(IRQEXT_TLA);
f6649a7e
MS
198 if (param32 & 0x00038000)
199 stp_timing_alert((struct stp_irq_parm *) &param32);
d2fec595
MS
200}
201
d2fec595 202static void stp_reset(void);
d54853ef 203
689911c7 204void read_persistent_clock64(struct timespec64 *ts)
d54853ef 205{
936cc855
MS
206 __u64 clock;
207
208 clock = get_tod_clock() - initial_leap_seconds;
209 tod_to_timeval(clock - TOD_UNIX_EPOCH, ts);
1da177e4 210}
d54853ef 211
689911c7 212void read_boot_clock64(struct timespec64 *ts)
23970e38 213{
936cc855
MS
214 __u64 clock;
215
216 clock = sched_clock_base_cc - initial_leap_seconds;
217 tod_to_timeval(clock - TOD_UNIX_EPOCH, ts);
1da177e4
LT
218}
219
8e19608e 220static cycle_t read_tod_clock(struct clocksource *cs)
dc64bef5 221{
1aae0560 222 return get_tod_clock();
dc64bef5
MS
223}
224
225static struct clocksource clocksource_tod = {
226 .name = "tod",
d2cb0e6e 227 .rating = 400,
dc64bef5
MS
228 .read = read_tod_clock,
229 .mask = -1ULL,
230 .mult = 1000,
231 .shift = 12,
cc02d809 232 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
dc64bef5
MS
233};
234
f1b82746
MS
235struct clocksource * __init clocksource_default_clock(void)
236{
237 return &clocksource_tod;
238}
dc64bef5 239
79c74ecb 240void update_vsyscall(struct timekeeper *tk)
b020632e 241{
79c74ecb
MS
242 u64 nsecps;
243
876e7881 244 if (tk->tkr_mono.clock != &clocksource_tod)
b020632e
MS
245 return;
246
247 /* Make userspace gettimeofday spin until we're done. */
248 ++vdso_data->tb_update_count;
249 smp_wmb();
876e7881 250 vdso_data->xtime_tod_stamp = tk->tkr_mono.cycle_last;
79c74ecb 251 vdso_data->xtime_clock_sec = tk->xtime_sec;
876e7881 252 vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec;
79c74ecb
MS
253 vdso_data->wtom_clock_sec =
254 tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
876e7881
PZ
255 vdso_data->wtom_clock_nsec = tk->tkr_mono.xtime_nsec +
256 + ((u64) tk->wall_to_monotonic.tv_nsec << tk->tkr_mono.shift);
257 nsecps = (u64) NSEC_PER_SEC << tk->tkr_mono.shift;
79c74ecb
MS
258 while (vdso_data->wtom_clock_nsec >= nsecps) {
259 vdso_data->wtom_clock_nsec -= nsecps;
260 vdso_data->wtom_clock_sec++;
261 }
b7eacb59
MS
262
263 vdso_data->xtime_coarse_sec = tk->xtime_sec;
264 vdso_data->xtime_coarse_nsec =
876e7881 265 (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
b7eacb59
MS
266 vdso_data->wtom_coarse_sec =
267 vdso_data->xtime_coarse_sec + tk->wall_to_monotonic.tv_sec;
268 vdso_data->wtom_coarse_nsec =
269 vdso_data->xtime_coarse_nsec + tk->wall_to_monotonic.tv_nsec;
270 while (vdso_data->wtom_coarse_nsec >= NSEC_PER_SEC) {
271 vdso_data->wtom_coarse_nsec -= NSEC_PER_SEC;
272 vdso_data->wtom_coarse_sec++;
273 }
274
876e7881
PZ
275 vdso_data->tk_mult = tk->tkr_mono.mult;
276 vdso_data->tk_shift = tk->tkr_mono.shift;
b020632e
MS
277 smp_wmb();
278 ++vdso_data->tb_update_count;
279}
280
281extern struct timezone sys_tz;
282
283void update_vsyscall_tz(void)
284{
285 /* Make userspace gettimeofday spin until we're done. */
286 ++vdso_data->tb_update_count;
287 smp_wmb();
288 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
289 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
290 smp_wmb();
291 ++vdso_data->tb_update_count;
292}
293
1da177e4
LT
294/*
295 * Initialize the TOD clock and the CPU timer of
296 * the boot cpu.
297 */
298void __init time_init(void)
299{
b6112ccb 300 /* Reset time synchronization interfaces. */
b6112ccb 301 stp_reset();
1da177e4 302
1da177e4 303 /* request the clock comparator external interrupt */
1dad093b
TH
304 if (register_external_irq(EXT_IRQ_CLK_COMP, clock_comparator_interrupt))
305 panic("Couldn't request external interrupt 0x1004");
1da177e4 306
d2fec595 307 /* request the timing alert external interrupt */
1dad093b 308 if (register_external_irq(EXT_IRQ_TIMING_ALERT, timing_alert_interrupt))
d54853ef
MS
309 panic("Couldn't request external interrupt 0x1406");
310
f8935983 311 if (__clocksource_register(&clocksource_tod) != 0)
ab96e798
MS
312 panic("Could not register TOD clock source");
313
d54853ef
MS
314 /* Enable TOD clock interrupts on the boot cpu. */
315 init_cpu_timer();
ab96e798 316
c185b783 317 /* Enable cpu timer interrupts on the boot cpu. */
1da177e4 318 vtime_init();
d54853ef
MS
319}
320
d2fec595
MS
321/*
322 * The time is "clock". old is what we think the time is.
323 * Adjust the value by a multiple of jiffies and add the delta to ntp.
324 * "delay" is an approximation how long the synchronization took. If
325 * the time correction is positive, then "delay" is subtracted from
326 * the time difference and only the remaining part is passed to ntp.
327 */
328static unsigned long long adjust_time(unsigned long long old,
329 unsigned long long clock,
330 unsigned long long delay)
331{
332 unsigned long long delta, ticks;
333 struct timex adjust;
334
335 if (clock > old) {
336 /* It is later than we thought. */
337 delta = ticks = clock - old;
338 delta = ticks = (delta < delay) ? 0 : delta - delay;
339 delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
340 adjust.offset = ticks * (1000000 / HZ);
341 } else {
342 /* It is earlier than we thought. */
343 delta = ticks = old - clock;
344 delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
345 delta = -delta;
346 adjust.offset = -ticks * (1000000 / HZ);
347 }
8107d829 348 sched_clock_base_cc += delta;
d2fec595 349 if (adjust.offset != 0) {
feab6501
MS
350 pr_notice("The ETR interface has adjusted the clock "
351 "by %li microseconds\n", adjust.offset);
d2fec595
MS
352 adjust.modes = ADJ_OFFSET_SINGLESHOT;
353 do_adjtimex(&adjust);
354 }
355 return delta;
356}
357
358static DEFINE_PER_CPU(atomic_t, clock_sync_word);
8283cb43 359static DEFINE_MUTEX(clock_sync_mutex);
d2fec595
MS
360static unsigned long clock_sync_flags;
361
362#define CLOCK_SYNC_HAS_ETR 0
363#define CLOCK_SYNC_HAS_STP 1
364#define CLOCK_SYNC_ETR 2
365#define CLOCK_SYNC_STP 3
366
367/*
40277891
MS
368 * The get_clock function for the physical clock. It will get the current
369 * TOD clock, subtract the LPAR offset and write the result to *clock.
370 * The function returns 0 if the clock is in sync with the external time
371 * source. If the clock mode is local it will return -EOPNOTSUPP and
372 * -EAGAIN if the clock is not in sync with the external reference.
d2fec595 373 */
40277891 374int get_phys_clock(unsigned long long *clock)
d2fec595
MS
375{
376 atomic_t *sw_ptr;
377 unsigned int sw0, sw1;
378
379 sw_ptr = &get_cpu_var(clock_sync_word);
380 sw0 = atomic_read(sw_ptr);
40277891 381 *clock = get_tod_clock() - lpar_offset;
d2fec595 382 sw1 = atomic_read(sw_ptr);
bd119ee2 383 put_cpu_var(clock_sync_word);
d2fec595
MS
384 if (sw0 == sw1 && (sw0 & 0x80000000U))
385 /* Success: time is in sync. */
386 return 0;
387 if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags) &&
388 !test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
a8f6db4d 389 return -EOPNOTSUPP;
d2fec595
MS
390 if (!test_bit(CLOCK_SYNC_ETR, &clock_sync_flags) &&
391 !test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
392 return -EACCES;
393 return -EAGAIN;
394}
40277891 395EXPORT_SYMBOL(get_phys_clock);
d2fec595
MS
396
397/*
398 * Make get_sync_clock return -EAGAIN.
399 */
400static void disable_sync_clock(void *dummy)
401{
eb7e7d76 402 atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
d2fec595
MS
403 /*
404 * Clear the in-sync bit 2^31. All get_sync_clock calls will
405 * fail until the sync bit is turned back on. In addition
406 * increase the "sequence" counter to avoid the race of an
407 * etr event and the complete recovery against get_sync_clock.
408 */
805de8f4 409 atomic_andnot(0x80000000, sw_ptr);
d2fec595
MS
410 atomic_inc(sw_ptr);
411}
412
413/*
414 * Make get_sync_clock return 0 again.
415 * Needs to be called from a context disabled for preemption.
416 */
417static void enable_sync_clock(void)
418{
eb7e7d76 419 atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
805de8f4 420 atomic_or(0x80000000, sw_ptr);
d2fec595
MS
421}
422
8283cb43
MS
423/*
424 * Function to check if the clock is in sync.
425 */
426static inline int check_sync_clock(void)
427{
428 atomic_t *sw_ptr;
429 int rc;
430
431 sw_ptr = &get_cpu_var(clock_sync_word);
432 rc = (atomic_read(sw_ptr) & 0x80000000U) != 0;
bd119ee2 433 put_cpu_var(clock_sync_word);
8283cb43
MS
434 return rc;
435}
436
750887de
HC
437/* Single threaded workqueue used for etr and stp sync events */
438static struct workqueue_struct *time_sync_wq;
439
440static void __init time_init_wq(void)
441{
179cb81a
HC
442 if (time_sync_wq)
443 return;
444 time_sync_wq = create_singlethread_workqueue("timesync");
750887de
HC
445}
446
d2fec595 447struct clock_sync_data {
750887de 448 atomic_t cpus;
5a62b192
HC
449 int in_sync;
450 unsigned long long fixup_cc;
750887de
HC
451 int etr_port;
452 struct etr_aib *etr_aib;
d2fec595 453};
5a62b192 454
750887de 455static void clock_sync_cpu(struct clock_sync_data *sync)
d54853ef 456{
750887de 457 atomic_dec(&sync->cpus);
d2fec595 458 enable_sync_clock();
d54853ef
MS
459 /*
460 * This looks like a busy wait loop but it isn't. etr_sync_cpus
461 * is called on all other cpus while the TOD clocks is stopped.
462 * __udelay will stop the cpu on an enabled wait psw until the
463 * TOD is running again.
464 */
d2fec595 465 while (sync->in_sync == 0) {
d54853ef 466 __udelay(1);
6c732de2
HC
467 /*
468 * A different cpu changes *in_sync. Therefore use
469 * barrier() to force memory access.
470 */
471 barrier();
472 }
d2fec595 473 if (sync->in_sync != 1)
d54853ef 474 /* Didn't work. Clear per-cpu in sync bit again. */
d2fec595 475 disable_sync_clock(NULL);
d54853ef
MS
476 /*
477 * This round of TOD syncing is done. Set the clock comparator
478 * to the next tick and let the processor continue.
479 */
d2fec595 480 fixup_clock_comparator(sync->fixup_cc);
d54853ef
MS
481}
482
d2fec595
MS
483/*
484 * Server Time Protocol (STP) code.
485 */
4cc7ecb7 486static bool stp_online;
d2fec595
MS
487static struct stp_sstpi stp_info;
488static void *stp_page;
489
490static void stp_work_fn(struct work_struct *work);
0b3016b7 491static DEFINE_MUTEX(stp_work_mutex);
d2fec595 492static DECLARE_WORK(stp_work, stp_work_fn);
04362301 493static struct timer_list stp_timer;
d2fec595
MS
494
495static int __init early_parse_stp(char *p)
496{
4cc7ecb7 497 return kstrtobool(p, &stp_online);
d2fec595
MS
498}
499early_param("stp", early_parse_stp);
500
501/*
502 * Reset STP attachment.
503 */
8f847003 504static void __init stp_reset(void)
d2fec595
MS
505{
506 int rc;
507
d7d1104f 508 stp_page = (void *) get_zeroed_page(GFP_ATOMIC);
2f82f577 509 rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
4a672cfa 510 if (rc == 0)
d2fec595
MS
511 set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
512 else if (stp_online) {
baebc70a 513 pr_warn("The real or virtual hardware system does not provide an STP interface\n");
d7d1104f 514 free_page((unsigned long) stp_page);
d2fec595
MS
515 stp_page = NULL;
516 stp_online = 0;
517 }
518}
519
04362301
MS
520static void stp_timeout(unsigned long dummy)
521{
522 queue_work(time_sync_wq, &stp_work);
523}
524
d2fec595
MS
525static int __init stp_init(void)
526{
750887de
HC
527 if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
528 return 0;
04362301 529 setup_timer(&stp_timer, stp_timeout, 0UL);
750887de
HC
530 time_init_wq();
531 if (!stp_online)
532 return 0;
533 queue_work(time_sync_wq, &stp_work);
d2fec595
MS
534 return 0;
535}
536
537arch_initcall(stp_init);
538
539/*
540 * STP timing alert. There are three causes:
541 * 1) timing status change
542 * 2) link availability change
543 * 3) time control parameter change
544 * In all three cases we are only interested in the clock source state.
545 * If a STP clock source is now available use it.
546 */
547static void stp_timing_alert(struct stp_irq_parm *intparm)
548{
549 if (intparm->tsc || intparm->lac || intparm->tcpc)
750887de 550 queue_work(time_sync_wq, &stp_work);
d2fec595
MS
551}
552
553/*
554 * STP sync check machine check. This is called when the timing state
555 * changes from the synchronized state to the unsynchronized state.
556 * After a STP sync check the clock is not in sync. The machine check
557 * is broadcasted to all cpus at the same time.
558 */
29b0a825 559int stp_sync_check(void)
d2fec595 560{
d2fec595 561 disable_sync_clock(NULL);
29b0a825 562 return 1;
d2fec595
MS
563}
564
565/*
566 * STP island condition machine check. This is called when an attached
567 * server attempts to communicate over an STP link and the servers
568 * have matching CTN ids and have a valid stratum-1 configuration
569 * but the configurations do not match.
570 */
29b0a825 571int stp_island_check(void)
d2fec595 572{
d2fec595 573 disable_sync_clock(NULL);
29b0a825 574 return 1;
d2fec595
MS
575}
576
29b0a825
HC
577void stp_queue_work(void)
578{
579 queue_work(time_sync_wq, &stp_work);
580}
750887de
HC
581
582static int stp_sync_clock(void *data)
d2fec595 583{
750887de 584 static int first;
fdf03650 585 unsigned long long old_clock, delta, new_clock, clock_delta;
750887de 586 struct clock_sync_data *stp_sync;
40277891 587 struct ptff_qto qto;
d2fec595
MS
588 int rc;
589
750887de 590 stp_sync = data;
d2fec595 591
750887de
HC
592 if (xchg(&first, 1) == 1) {
593 /* Slave */
594 clock_sync_cpu(stp_sync);
595 return 0;
596 }
d2fec595 597
750887de
HC
598 /* Wait until all other cpus entered the sync function. */
599 while (atomic_read(&stp_sync->cpus) != 0)
600 cpu_relax();
d2fec595 601
d2fec595
MS
602 enable_sync_clock();
603
d2fec595
MS
604 rc = 0;
605 if (stp_info.todoff[0] || stp_info.todoff[1] ||
606 stp_info.todoff[2] || stp_info.todoff[3] ||
607 stp_info.tmd != 2) {
1aae0560 608 old_clock = get_tod_clock();
2f82f577 609 rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0, &clock_delta);
d2fec595 610 if (rc == 0) {
2f82f577 611 new_clock = old_clock + clock_delta;
fdf03650 612 delta = adjust_time(old_clock, new_clock, 0);
40277891
MS
613 if (ptff_query(PTFF_QTO) &&
614 ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
615 /* Update LPAR offset */
616 lpar_offset = qto.tod_epoch_difference;
fdf03650
FZ
617 atomic_notifier_call_chain(&s390_epoch_delta_notifier,
618 0, &clock_delta);
d2fec595
MS
619 fixup_clock_comparator(delta);
620 rc = chsc_sstpi(stp_page, &stp_info,
621 sizeof(struct stp_sstpi));
622 if (rc == 0 && stp_info.tmd != 2)
623 rc = -EAGAIN;
624 }
625 }
626 if (rc) {
627 disable_sync_clock(NULL);
750887de 628 stp_sync->in_sync = -EAGAIN;
d2fec595 629 } else
750887de
HC
630 stp_sync->in_sync = 1;
631 xchg(&first, 0);
632 return 0;
633}
d2fec595 634
750887de
HC
635/*
636 * STP work. Check for the STP state and take over the clock
637 * synchronization if the STP clock source is usable.
638 */
639static void stp_work_fn(struct work_struct *work)
640{
641 struct clock_sync_data stp_sync;
642 int rc;
643
0b3016b7
MS
644 /* prevent multiple execution. */
645 mutex_lock(&stp_work_mutex);
646
750887de 647 if (!stp_online) {
2f82f577 648 chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
04362301 649 del_timer_sync(&stp_timer);
0b3016b7 650 goto out_unlock;
750887de
HC
651 }
652
2f82f577 653 rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xb0e0, NULL);
750887de 654 if (rc)
0b3016b7 655 goto out_unlock;
750887de
HC
656
657 rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
658 if (rc || stp_info.c == 0)
0b3016b7 659 goto out_unlock;
750887de 660
8283cb43
MS
661 /* Skip synchronization if the clock is already in sync. */
662 if (check_sync_clock())
663 goto out_unlock;
664
750887de
HC
665 memset(&stp_sync, 0, sizeof(stp_sync));
666 get_online_cpus();
667 atomic_set(&stp_sync.cpus, num_online_cpus() - 1);
0f1959f5 668 stop_machine(stp_sync_clock, &stp_sync, cpu_online_mask);
750887de 669 put_online_cpus();
0b3016b7 670
04362301
MS
671 if (!check_sync_clock())
672 /*
673 * There is a usable clock but the synchonization failed.
674 * Retry after a second.
675 */
676 mod_timer(&stp_timer, jiffies + HZ);
677
0b3016b7
MS
678out_unlock:
679 mutex_unlock(&stp_work_mutex);
d2fec595
MS
680}
681
682/*
3fbacffb 683 * STP subsys sysfs interface functions
d2fec595 684 */
3fbacffb
KS
685static struct bus_type stp_subsys = {
686 .name = "stp",
687 .dev_name = "stp",
d2fec595
MS
688};
689
3fbacffb
KS
690static ssize_t stp_ctn_id_show(struct device *dev,
691 struct device_attribute *attr,
c9be0a36 692 char *buf)
d2fec595
MS
693{
694 if (!stp_online)
695 return -ENODATA;
696 return sprintf(buf, "%016llx\n",
697 *(unsigned long long *) stp_info.ctnid);
698}
699
3fbacffb 700static DEVICE_ATTR(ctn_id, 0400, stp_ctn_id_show, NULL);
d2fec595 701
3fbacffb
KS
702static ssize_t stp_ctn_type_show(struct device *dev,
703 struct device_attribute *attr,
c9be0a36 704 char *buf)
d2fec595
MS
705{
706 if (!stp_online)
707 return -ENODATA;
708 return sprintf(buf, "%i\n", stp_info.ctn);
709}
710
3fbacffb 711static DEVICE_ATTR(ctn_type, 0400, stp_ctn_type_show, NULL);
d2fec595 712
3fbacffb
KS
713static ssize_t stp_dst_offset_show(struct device *dev,
714 struct device_attribute *attr,
c9be0a36 715 char *buf)
d2fec595
MS
716{
717 if (!stp_online || !(stp_info.vbits & 0x2000))
718 return -ENODATA;
719 return sprintf(buf, "%i\n", (int)(s16) stp_info.dsto);
720}
721
3fbacffb 722static DEVICE_ATTR(dst_offset, 0400, stp_dst_offset_show, NULL);
d2fec595 723
3fbacffb
KS
724static ssize_t stp_leap_seconds_show(struct device *dev,
725 struct device_attribute *attr,
c9be0a36 726 char *buf)
d2fec595
MS
727{
728 if (!stp_online || !(stp_info.vbits & 0x8000))
729 return -ENODATA;
730 return sprintf(buf, "%i\n", (int)(s16) stp_info.leaps);
731}
732
3fbacffb 733static DEVICE_ATTR(leap_seconds, 0400, stp_leap_seconds_show, NULL);
d2fec595 734
3fbacffb
KS
735static ssize_t stp_stratum_show(struct device *dev,
736 struct device_attribute *attr,
c9be0a36 737 char *buf)
d2fec595
MS
738{
739 if (!stp_online)
740 return -ENODATA;
741 return sprintf(buf, "%i\n", (int)(s16) stp_info.stratum);
742}
743
3fbacffb 744static DEVICE_ATTR(stratum, 0400, stp_stratum_show, NULL);
d2fec595 745
3fbacffb
KS
746static ssize_t stp_time_offset_show(struct device *dev,
747 struct device_attribute *attr,
c9be0a36 748 char *buf)
d2fec595
MS
749{
750 if (!stp_online || !(stp_info.vbits & 0x0800))
751 return -ENODATA;
752 return sprintf(buf, "%i\n", (int) stp_info.tto);
753}
754
3fbacffb 755static DEVICE_ATTR(time_offset, 0400, stp_time_offset_show, NULL);
d2fec595 756
3fbacffb
KS
757static ssize_t stp_time_zone_offset_show(struct device *dev,
758 struct device_attribute *attr,
c9be0a36 759 char *buf)
d2fec595
MS
760{
761 if (!stp_online || !(stp_info.vbits & 0x4000))
762 return -ENODATA;
763 return sprintf(buf, "%i\n", (int)(s16) stp_info.tzo);
764}
765
3fbacffb 766static DEVICE_ATTR(time_zone_offset, 0400,
d2fec595
MS
767 stp_time_zone_offset_show, NULL);
768
3fbacffb
KS
769static ssize_t stp_timing_mode_show(struct device *dev,
770 struct device_attribute *attr,
c9be0a36 771 char *buf)
d2fec595
MS
772{
773 if (!stp_online)
774 return -ENODATA;
775 return sprintf(buf, "%i\n", stp_info.tmd);
776}
777
3fbacffb 778static DEVICE_ATTR(timing_mode, 0400, stp_timing_mode_show, NULL);
d2fec595 779
3fbacffb
KS
780static ssize_t stp_timing_state_show(struct device *dev,
781 struct device_attribute *attr,
c9be0a36 782 char *buf)
d2fec595
MS
783{
784 if (!stp_online)
785 return -ENODATA;
786 return sprintf(buf, "%i\n", stp_info.tst);
787}
788
3fbacffb 789static DEVICE_ATTR(timing_state, 0400, stp_timing_state_show, NULL);
d2fec595 790
3fbacffb
KS
791static ssize_t stp_online_show(struct device *dev,
792 struct device_attribute *attr,
c9be0a36 793 char *buf)
d2fec595
MS
794{
795 return sprintf(buf, "%i\n", stp_online);
796}
797
3fbacffb
KS
798static ssize_t stp_online_store(struct device *dev,
799 struct device_attribute *attr,
d2fec595
MS
800 const char *buf, size_t count)
801{
802 unsigned int value;
803
804 value = simple_strtoul(buf, NULL, 0);
805 if (value != 0 && value != 1)
806 return -EINVAL;
807 if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
808 return -EOPNOTSUPP;
8283cb43 809 mutex_lock(&clock_sync_mutex);
d2fec595 810 stp_online = value;
8283cb43
MS
811 if (stp_online)
812 set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
813 else
814 clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
750887de 815 queue_work(time_sync_wq, &stp_work);
8283cb43 816 mutex_unlock(&clock_sync_mutex);
d2fec595
MS
817 return count;
818}
819
820/*
3fbacffb
KS
821 * Can't use DEVICE_ATTR because the attribute should be named
822 * stp/online but dev_attr_online already exists in this file ..
d2fec595 823 */
3fbacffb 824static struct device_attribute dev_attr_stp_online = {
d2fec595
MS
825 .attr = { .name = "online", .mode = 0600 },
826 .show = stp_online_show,
827 .store = stp_online_store,
828};
829
3fbacffb
KS
830static struct device_attribute *stp_attributes[] = {
831 &dev_attr_ctn_id,
832 &dev_attr_ctn_type,
833 &dev_attr_dst_offset,
834 &dev_attr_leap_seconds,
835 &dev_attr_stp_online,
836 &dev_attr_stratum,
837 &dev_attr_time_offset,
838 &dev_attr_time_zone_offset,
839 &dev_attr_timing_mode,
840 &dev_attr_timing_state,
d2fec595
MS
841 NULL
842};
843
844static int __init stp_init_sysfs(void)
845{
3fbacffb 846 struct device_attribute **attr;
d2fec595
MS
847 int rc;
848
3fbacffb 849 rc = subsys_system_register(&stp_subsys, NULL);
d2fec595
MS
850 if (rc)
851 goto out;
852 for (attr = stp_attributes; *attr; attr++) {
3fbacffb 853 rc = device_create_file(stp_subsys.dev_root, *attr);
d2fec595
MS
854 if (rc)
855 goto out_unreg;
856 }
857 return 0;
858out_unreg:
859 for (; attr >= stp_attributes; attr--)
3fbacffb
KS
860 device_remove_file(stp_subsys.dev_root, *attr);
861 bus_unregister(&stp_subsys);
d2fec595
MS
862out:
863 return rc;
864}
865
866device_initcall(stp_init_sysfs);