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