Merge tag 'gcc-plugins-v4.9-rc4' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / arch / s390 / kernel / time.c
1 /*
2  *    Time of day based timer functions.
3  *
4  *  S390 version
5  *    Copyright IBM Corp. 1999, 2008
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
14 #define KMSG_COMPONENT "time"
15 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16
17 #include <linux/kernel_stat.h>
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>
26 #include <linux/cpu.h>
27 #include <linux/stop_machine.h>
28 #include <linux/time.h>
29 #include <linux/device.h>
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>
37 #include <linux/timekeeper_internal.h>
38 #include <linux/clockchips.h>
39 #include <linux/gfp.h>
40 #include <linux/kprobes.h>
41 #include <asm/uaccess.h>
42 #include <asm/facility.h>
43 #include <asm/delay.h>
44 #include <asm/div64.h>
45 #include <asm/vdso.h>
46 #include <asm/irq.h>
47 #include <asm/irq_regs.h>
48 #include <asm/vtimer.h>
49 #include <asm/stp.h>
50 #include <asm/cio.h>
51 #include "entry.h"
52
53 u64 sched_clock_base_cc = -1;   /* Force to data section. */
54 EXPORT_SYMBOL_GPL(sched_clock_base_cc);
55
56 static DEFINE_PER_CPU(struct clock_event_device, comparators);
57
58 ATOMIC_NOTIFIER_HEAD(s390_epoch_delta_notifier);
59 EXPORT_SYMBOL(s390_epoch_delta_notifier);
60
61 unsigned char ptff_function_mask[16];
62 unsigned long lpar_offset;
63 unsigned long initial_leap_seconds;
64
65 /*
66  * Get time offsets with PTFF
67  */
68 void __init ptff_init(void)
69 {
70         struct ptff_qto qto;
71         struct ptff_qui qui;
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;
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);
85 }
86
87 /*
88  * Scheduler clock - returns current time in nanosec units.
89  */
90 unsigned long long notrace sched_clock(void)
91 {
92         return tod_to_ns(get_tod_clock_monotonic());
93 }
94 NOKPROBE_SYMBOL(sched_clock);
95
96 /*
97  * Monotonic_clock - returns # of nanoseconds passed since time_init()
98  */
99 unsigned long long monotonic_clock(void)
100 {
101         return sched_clock();
102 }
103 EXPORT_SYMBOL(monotonic_clock);
104
105 void tod_to_timeval(__u64 todval, struct timespec64 *xt)
106 {
107         unsigned long long sec;
108
109         sec = todval >> 12;
110         do_div(sec, 1000000);
111         xt->tv_sec = sec;
112         todval -= (sec * 1000000) << 12;
113         xt->tv_nsec = ((todval * 1000) >> 12);
114 }
115 EXPORT_SYMBOL(tod_to_timeval);
116
117 void clock_comparator_work(void)
118 {
119         struct clock_event_device *cd;
120
121         S390_lowcore.clock_comparator = -1ULL;
122         cd = this_cpu_ptr(&comparators);
123         cd->event_handler(cd);
124 }
125
126 /*
127  * Fixup the clock comparator.
128  */
129 static void fixup_clock_comparator(unsigned long long delta)
130 {
131         /* If nobody is waiting there's nothing to fix. */
132         if (S390_lowcore.clock_comparator == -1ULL)
133                 return;
134         S390_lowcore.clock_comparator += delta;
135         set_clock_comparator(S390_lowcore.clock_comparator);
136 }
137
138 static int s390_next_event(unsigned long delta,
139                            struct clock_event_device *evt)
140 {
141         S390_lowcore.clock_comparator = get_tod_clock() + delta;
142         set_clock_comparator(S390_lowcore.clock_comparator);
143         return 0;
144 }
145
146 /*
147  * Set up lowcore and control register of the current cpu to
148  * enable TOD clock and clock comparator interrupts.
149  */
150 void init_cpu_timer(void)
151 {
152         struct clock_event_device *cd;
153         int cpu;
154
155         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";
161         cd->features            = CLOCK_EVT_FEAT_ONESHOT;
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;
167         cd->cpumask             = cpumask_of(cpu);
168         cd->set_next_event      = s390_next_event;
169
170         clockevents_register_device(cd);
171
172         /* Enable clock comparator timer interrupt. */
173         __ctl_set_bit(0,11);
174
175         /* Always allow the timing alert external interrupt. */
176         __ctl_set_bit(0, 4);
177 }
178
179 static void clock_comparator_interrupt(struct ext_code ext_code,
180                                        unsigned int param32,
181                                        unsigned long param64)
182 {
183         inc_irq_stat(IRQEXT_CLK);
184         if (S390_lowcore.clock_comparator == -1ULL)
185                 set_clock_comparator(S390_lowcore.clock_comparator);
186 }
187
188 static void stp_timing_alert(struct stp_irq_parm *);
189
190 static void timing_alert_interrupt(struct ext_code ext_code,
191                                    unsigned int param32, unsigned long param64)
192 {
193         inc_irq_stat(IRQEXT_TLA);
194         if (param32 & 0x00038000)
195                 stp_timing_alert((struct stp_irq_parm *) &param32);
196 }
197
198 static void stp_reset(void);
199
200 void read_persistent_clock64(struct timespec64 *ts)
201 {
202         __u64 clock;
203
204         clock = get_tod_clock() - initial_leap_seconds;
205         tod_to_timeval(clock - TOD_UNIX_EPOCH, ts);
206 }
207
208 void read_boot_clock64(struct timespec64 *ts)
209 {
210         __u64 clock;
211
212         clock = sched_clock_base_cc - initial_leap_seconds;
213         tod_to_timeval(clock - TOD_UNIX_EPOCH, ts);
214 }
215
216 static cycle_t read_tod_clock(struct clocksource *cs)
217 {
218         return get_tod_clock();
219 }
220
221 static struct clocksource clocksource_tod = {
222         .name           = "tod",
223         .rating         = 400,
224         .read           = read_tod_clock,
225         .mask           = -1ULL,
226         .mult           = 1000,
227         .shift          = 12,
228         .flags          = CLOCK_SOURCE_IS_CONTINUOUS,
229 };
230
231 struct clocksource * __init clocksource_default_clock(void)
232 {
233         return &clocksource_tod;
234 }
235
236 void update_vsyscall(struct timekeeper *tk)
237 {
238         u64 nsecps;
239
240         if (tk->tkr_mono.clock != &clocksource_tod)
241                 return;
242
243         /* Make userspace gettimeofday spin until we're done. */
244         ++vdso_data->tb_update_count;
245         smp_wmb();
246         vdso_data->xtime_tod_stamp = tk->tkr_mono.cycle_last;
247         vdso_data->xtime_clock_sec = tk->xtime_sec;
248         vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec;
249         vdso_data->wtom_clock_sec =
250                 tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
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;
254         while (vdso_data->wtom_clock_nsec >= nsecps) {
255                 vdso_data->wtom_clock_nsec -= nsecps;
256                 vdso_data->wtom_clock_sec++;
257         }
258
259         vdso_data->xtime_coarse_sec = tk->xtime_sec;
260         vdso_data->xtime_coarse_nsec =
261                 (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
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
271         vdso_data->tk_mult = tk->tkr_mono.mult;
272         vdso_data->tk_shift = tk->tkr_mono.shift;
273         smp_wmb();
274         ++vdso_data->tb_update_count;
275 }
276
277 extern struct timezone sys_tz;
278
279 void update_vsyscall_tz(void)
280 {
281         vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
282         vdso_data->tz_dsttime = sys_tz.tz_dsttime;
283 }
284
285 /*
286  * Initialize the TOD clock and the CPU timer of
287  * the boot cpu.
288  */
289 void __init time_init(void)
290 {
291         /* Reset time synchronization interfaces. */
292         stp_reset();
293
294         /* request the clock comparator external interrupt */
295         if (register_external_irq(EXT_IRQ_CLK_COMP, clock_comparator_interrupt))
296                 panic("Couldn't request external interrupt 0x1004");
297
298         /* request the timing alert external interrupt */
299         if (register_external_irq(EXT_IRQ_TIMING_ALERT, timing_alert_interrupt))
300                 panic("Couldn't request external interrupt 0x1406");
301
302         if (__clocksource_register(&clocksource_tod) != 0)
303                 panic("Could not register TOD clock source");
304
305         /* Enable TOD clock interrupts on the boot cpu. */
306         init_cpu_timer();
307
308         /* Enable cpu timer interrupts on the boot cpu. */
309         vtime_init();
310 }
311
312 static DEFINE_PER_CPU(atomic_t, clock_sync_word);
313 static DEFINE_MUTEX(clock_sync_mutex);
314 static unsigned long clock_sync_flags;
315
316 #define CLOCK_SYNC_HAS_STP      0
317 #define CLOCK_SYNC_STP          1
318
319 /*
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.
325  */
326 int get_phys_clock(unsigned long long *clock)
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);
333         *clock = get_tod_clock() - lpar_offset;
334         sw1 = atomic_read(sw_ptr);
335         put_cpu_var(clock_sync_word);
336         if (sw0 == sw1 && (sw0 & 0x80000000U))
337                 /* Success: time is in sync. */
338                 return 0;
339         if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
340                 return -EOPNOTSUPP;
341         if (!test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
342                 return -EACCES;
343         return -EAGAIN;
344 }
345 EXPORT_SYMBOL(get_phys_clock);
346
347 /*
348  * Make get_phys_clock() return -EAGAIN.
349  */
350 static void disable_sync_clock(void *dummy)
351 {
352         atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
353         /*
354          * Clear the in-sync bit 2^31. All get_phys_clock calls will
355          * fail until the sync bit is turned back on. In addition
356          * increase the "sequence" counter to avoid the race of an
357          * stp event and the complete recovery against get_phys_clock.
358          */
359         atomic_andnot(0x80000000, sw_ptr);
360         atomic_inc(sw_ptr);
361 }
362
363 /*
364  * Make get_phys_clock() return 0 again.
365  * Needs to be called from a context disabled for preemption.
366  */
367 static void enable_sync_clock(void)
368 {
369         atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
370         atomic_or(0x80000000, sw_ptr);
371 }
372
373 /*
374  * Function to check if the clock is in sync.
375  */
376 static 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;
383         put_cpu_var(clock_sync_word);
384         return rc;
385 }
386
387 /* Single threaded workqueue used for stp sync events */
388 static struct workqueue_struct *time_sync_wq;
389
390 static void __init time_init_wq(void)
391 {
392         if (time_sync_wq)
393                 return;
394         time_sync_wq = create_singlethread_workqueue("timesync");
395 }
396
397 struct clock_sync_data {
398         atomic_t cpus;
399         int in_sync;
400         unsigned long long fixup_cc;
401 };
402
403 static void clock_sync_cpu(struct clock_sync_data *sync)
404 {
405         atomic_dec(&sync->cpus);
406         enable_sync_clock();
407         while (sync->in_sync == 0) {
408                 __udelay(1);
409                 /*
410                  * A different cpu changes *in_sync. Therefore use
411                  * barrier() to force memory access.
412                  */
413                 barrier();
414         }
415         if (sync->in_sync != 1)
416                 /* Didn't work. Clear per-cpu in sync bit again. */
417                 disable_sync_clock(NULL);
418         /*
419          * This round of TOD syncing is done. Set the clock comparator
420          * to the next tick and let the processor continue.
421          */
422         fixup_clock_comparator(sync->fixup_cc);
423 }
424
425 /*
426  * Server Time Protocol (STP) code.
427  */
428 static bool stp_online;
429 static struct stp_sstpi stp_info;
430 static void *stp_page;
431
432 static void stp_work_fn(struct work_struct *work);
433 static DEFINE_MUTEX(stp_work_mutex);
434 static DECLARE_WORK(stp_work, stp_work_fn);
435 static struct timer_list stp_timer;
436
437 static int __init early_parse_stp(char *p)
438 {
439         return kstrtobool(p, &stp_online);
440 }
441 early_param("stp", early_parse_stp);
442
443 /*
444  * Reset STP attachment.
445  */
446 static void __init stp_reset(void)
447 {
448         int rc;
449
450         stp_page = (void *) get_zeroed_page(GFP_ATOMIC);
451         rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
452         if (rc == 0)
453                 set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
454         else if (stp_online) {
455                 pr_warn("The real or virtual hardware system does not provide an STP interface\n");
456                 free_page((unsigned long) stp_page);
457                 stp_page = NULL;
458                 stp_online = 0;
459         }
460 }
461
462 static void stp_timeout(unsigned long dummy)
463 {
464         queue_work(time_sync_wq, &stp_work);
465 }
466
467 static int __init stp_init(void)
468 {
469         if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
470                 return 0;
471         setup_timer(&stp_timer, stp_timeout, 0UL);
472         time_init_wq();
473         if (!stp_online)
474                 return 0;
475         queue_work(time_sync_wq, &stp_work);
476         return 0;
477 }
478
479 arch_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  */
489 static void stp_timing_alert(struct stp_irq_parm *intparm)
490 {
491         if (intparm->tsc || intparm->lac || intparm->tcpc)
492                 queue_work(time_sync_wq, &stp_work);
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  */
501 int stp_sync_check(void)
502 {
503         disable_sync_clock(NULL);
504         return 1;
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  */
513 int stp_island_check(void)
514 {
515         disable_sync_clock(NULL);
516         return 1;
517 }
518
519 void stp_queue_work(void)
520 {
521         queue_work(time_sync_wq, &stp_work);
522 }
523
524 static int stp_sync_clock(void *data)
525 {
526         static int first;
527         unsigned long long clock_delta;
528         struct clock_sync_data *stp_sync;
529         struct ptff_qto qto;
530         int rc;
531
532         stp_sync = data;
533
534         if (xchg(&first, 1) == 1) {
535                 /* Slave */
536                 clock_sync_cpu(stp_sync);
537                 return 0;
538         }
539
540         /* Wait until all other cpus entered the sync function. */
541         while (atomic_read(&stp_sync->cpus) != 0)
542                 cpu_relax();
543
544         enable_sync_clock();
545
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) {
550                 rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0, &clock_delta);
551                 if (rc == 0) {
552                         /* fixup the monotonic sched clock */
553                         sched_clock_base_cc += clock_delta;
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;
558                         atomic_notifier_call_chain(&s390_epoch_delta_notifier,
559                                                    0, &clock_delta);
560                         stp_sync->fixup_cc = clock_delta;
561                         fixup_clock_comparator(clock_delta);
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);
570                 stp_sync->in_sync = -EAGAIN;
571         } else
572                 stp_sync->in_sync = 1;
573         xchg(&first, 0);
574         return 0;
575 }
576
577 /*
578  * STP work. Check for the STP state and take over the clock
579  * synchronization if the STP clock source is usable.
580  */
581 static void stp_work_fn(struct work_struct *work)
582 {
583         struct clock_sync_data stp_sync;
584         int rc;
585
586         /* prevent multiple execution. */
587         mutex_lock(&stp_work_mutex);
588
589         if (!stp_online) {
590                 chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
591                 del_timer_sync(&stp_timer);
592                 goto out_unlock;
593         }
594
595         rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xb0e0, NULL);
596         if (rc)
597                 goto out_unlock;
598
599         rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
600         if (rc || stp_info.c == 0)
601                 goto out_unlock;
602
603         /* Skip synchronization if the clock is already in sync. */
604         if (check_sync_clock())
605                 goto out_unlock;
606
607         memset(&stp_sync, 0, sizeof(stp_sync));
608         get_online_cpus();
609         atomic_set(&stp_sync.cpus, num_online_cpus() - 1);
610         stop_machine(stp_sync_clock, &stp_sync, cpu_online_mask);
611         put_online_cpus();
612
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
620 out_unlock:
621         mutex_unlock(&stp_work_mutex);
622 }
623
624 /*
625  * STP subsys sysfs interface functions
626  */
627 static struct bus_type stp_subsys = {
628         .name           = "stp",
629         .dev_name       = "stp",
630 };
631
632 static ssize_t stp_ctn_id_show(struct device *dev,
633                                 struct device_attribute *attr,
634                                 char *buf)
635 {
636         if (!stp_online)
637                 return -ENODATA;
638         return sprintf(buf, "%016llx\n",
639                        *(unsigned long long *) stp_info.ctnid);
640 }
641
642 static DEVICE_ATTR(ctn_id, 0400, stp_ctn_id_show, NULL);
643
644 static ssize_t stp_ctn_type_show(struct device *dev,
645                                 struct device_attribute *attr,
646                                 char *buf)
647 {
648         if (!stp_online)
649                 return -ENODATA;
650         return sprintf(buf, "%i\n", stp_info.ctn);
651 }
652
653 static DEVICE_ATTR(ctn_type, 0400, stp_ctn_type_show, NULL);
654
655 static ssize_t stp_dst_offset_show(struct device *dev,
656                                    struct device_attribute *attr,
657                                    char *buf)
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
664 static DEVICE_ATTR(dst_offset, 0400, stp_dst_offset_show, NULL);
665
666 static ssize_t stp_leap_seconds_show(struct device *dev,
667                                         struct device_attribute *attr,
668                                         char *buf)
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
675 static DEVICE_ATTR(leap_seconds, 0400, stp_leap_seconds_show, NULL);
676
677 static ssize_t stp_stratum_show(struct device *dev,
678                                 struct device_attribute *attr,
679                                 char *buf)
680 {
681         if (!stp_online)
682                 return -ENODATA;
683         return sprintf(buf, "%i\n", (int)(s16) stp_info.stratum);
684 }
685
686 static DEVICE_ATTR(stratum, 0400, stp_stratum_show, NULL);
687
688 static ssize_t stp_time_offset_show(struct device *dev,
689                                 struct device_attribute *attr,
690                                 char *buf)
691 {
692         if (!stp_online || !(stp_info.vbits & 0x0800))
693                 return -ENODATA;
694         return sprintf(buf, "%i\n", (int) stp_info.tto);
695 }
696
697 static DEVICE_ATTR(time_offset, 0400, stp_time_offset_show, NULL);
698
699 static ssize_t stp_time_zone_offset_show(struct device *dev,
700                                 struct device_attribute *attr,
701                                 char *buf)
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
708 static DEVICE_ATTR(time_zone_offset, 0400,
709                          stp_time_zone_offset_show, NULL);
710
711 static ssize_t stp_timing_mode_show(struct device *dev,
712                                 struct device_attribute *attr,
713                                 char *buf)
714 {
715         if (!stp_online)
716                 return -ENODATA;
717         return sprintf(buf, "%i\n", stp_info.tmd);
718 }
719
720 static DEVICE_ATTR(timing_mode, 0400, stp_timing_mode_show, NULL);
721
722 static ssize_t stp_timing_state_show(struct device *dev,
723                                 struct device_attribute *attr,
724                                 char *buf)
725 {
726         if (!stp_online)
727                 return -ENODATA;
728         return sprintf(buf, "%i\n", stp_info.tst);
729 }
730
731 static DEVICE_ATTR(timing_state, 0400, stp_timing_state_show, NULL);
732
733 static ssize_t stp_online_show(struct device *dev,
734                                 struct device_attribute *attr,
735                                 char *buf)
736 {
737         return sprintf(buf, "%i\n", stp_online);
738 }
739
740 static ssize_t stp_online_store(struct device *dev,
741                                 struct device_attribute *attr,
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;
751         mutex_lock(&clock_sync_mutex);
752         stp_online = value;
753         if (stp_online)
754                 set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
755         else
756                 clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
757         queue_work(time_sync_wq, &stp_work);
758         mutex_unlock(&clock_sync_mutex);
759         return count;
760 }
761
762 /*
763  * Can't use DEVICE_ATTR because the attribute should be named
764  * stp/online but dev_attr_online already exists in this file ..
765  */
766 static struct device_attribute dev_attr_stp_online = {
767         .attr = { .name = "online", .mode = 0600 },
768         .show   = stp_online_show,
769         .store  = stp_online_store,
770 };
771
772 static 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,
783         NULL
784 };
785
786 static int __init stp_init_sysfs(void)
787 {
788         struct device_attribute **attr;
789         int rc;
790
791         rc = subsys_system_register(&stp_subsys, NULL);
792         if (rc)
793                 goto out;
794         for (attr = stp_attributes; *attr; attr++) {
795                 rc = device_create_file(stp_subsys.dev_root, *attr);
796                 if (rc)
797                         goto out_unreg;
798         }
799         return 0;
800 out_unreg:
801         for (; attr >= stp_attributes; attr--)
802                 device_remove_file(stp_subsys.dev_root, *attr);
803         bus_unregister(&stp_subsys);
804 out:
805         return rc;
806 }
807
808 device_initcall(stp_init_sysfs);