Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *    Time of day based timer functions.
0004  *
0005  *  S390 version
0006  *    Copyright IBM Corp. 1999, 2008
0007  *    Author(s): Hartmut Penner (hp@de.ibm.com),
0008  *               Martin Schwidefsky (schwidefsky@de.ibm.com),
0009  *               Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
0010  *
0011  *  Derived from "arch/i386/kernel/time.c"
0012  *    Copyright (C) 1991, 1992, 1995  Linus Torvalds
0013  */
0014 
0015 #define KMSG_COMPONENT "time"
0016 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
0017 
0018 #include <linux/kernel_stat.h>
0019 #include <linux/errno.h>
0020 #include <linux/export.h>
0021 #include <linux/sched.h>
0022 #include <linux/sched/clock.h>
0023 #include <linux/kernel.h>
0024 #include <linux/param.h>
0025 #include <linux/string.h>
0026 #include <linux/mm.h>
0027 #include <linux/interrupt.h>
0028 #include <linux/cpu.h>
0029 #include <linux/stop_machine.h>
0030 #include <linux/time.h>
0031 #include <linux/device.h>
0032 #include <linux/delay.h>
0033 #include <linux/init.h>
0034 #include <linux/smp.h>
0035 #include <linux/types.h>
0036 #include <linux/profile.h>
0037 #include <linux/timex.h>
0038 #include <linux/notifier.h>
0039 #include <linux/timekeeper_internal.h>
0040 #include <linux/clockchips.h>
0041 #include <linux/gfp.h>
0042 #include <linux/kprobes.h>
0043 #include <linux/uaccess.h>
0044 #include <vdso/vsyscall.h>
0045 #include <vdso/clocksource.h>
0046 #include <vdso/helpers.h>
0047 #include <asm/facility.h>
0048 #include <asm/delay.h>
0049 #include <asm/div64.h>
0050 #include <asm/vdso.h>
0051 #include <asm/irq.h>
0052 #include <asm/irq_regs.h>
0053 #include <asm/vtimer.h>
0054 #include <asm/stp.h>
0055 #include <asm/cio.h>
0056 #include "entry.h"
0057 
0058 union tod_clock tod_clock_base __section(".data");
0059 EXPORT_SYMBOL_GPL(tod_clock_base);
0060 
0061 u64 clock_comparator_max = -1ULL;
0062 EXPORT_SYMBOL_GPL(clock_comparator_max);
0063 
0064 static DEFINE_PER_CPU(struct clock_event_device, comparators);
0065 
0066 ATOMIC_NOTIFIER_HEAD(s390_epoch_delta_notifier);
0067 EXPORT_SYMBOL(s390_epoch_delta_notifier);
0068 
0069 unsigned char ptff_function_mask[16];
0070 
0071 static unsigned long lpar_offset;
0072 static unsigned long initial_leap_seconds;
0073 static unsigned long tod_steering_end;
0074 static long tod_steering_delta;
0075 
0076 /*
0077  * Get time offsets with PTFF
0078  */
0079 void __init time_early_init(void)
0080 {
0081     struct ptff_qto qto;
0082     struct ptff_qui qui;
0083     int cs;
0084 
0085     /* Initialize TOD steering parameters */
0086     tod_steering_end = tod_clock_base.tod;
0087     for (cs = 0; cs < CS_BASES; cs++)
0088         vdso_data[cs].arch_data.tod_steering_end = tod_steering_end;
0089 
0090     if (!test_facility(28))
0091         return;
0092 
0093     ptff(&ptff_function_mask, sizeof(ptff_function_mask), PTFF_QAF);
0094 
0095     /* get LPAR offset */
0096     if (ptff_query(PTFF_QTO) && ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
0097         lpar_offset = qto.tod_epoch_difference;
0098 
0099     /* get initial leap seconds */
0100     if (ptff_query(PTFF_QUI) && ptff(&qui, sizeof(qui), PTFF_QUI) == 0)
0101         initial_leap_seconds = (unsigned long)
0102             ((long) qui.old_leap * 4096000000L);
0103 }
0104 
0105 /*
0106  * Scheduler clock - returns current time in nanosec units.
0107  */
0108 unsigned long long notrace sched_clock(void)
0109 {
0110     return tod_to_ns(get_tod_clock_monotonic());
0111 }
0112 NOKPROBE_SYMBOL(sched_clock);
0113 
0114 static void ext_to_timespec64(union tod_clock *clk, struct timespec64 *xt)
0115 {
0116     unsigned long rem, sec, nsec;
0117 
0118     sec = clk->us;
0119     rem = do_div(sec, 1000000);
0120     nsec = ((clk->sus + (rem << 12)) * 125) >> 9;
0121     xt->tv_sec = sec;
0122     xt->tv_nsec = nsec;
0123 }
0124 
0125 void clock_comparator_work(void)
0126 {
0127     struct clock_event_device *cd;
0128 
0129     S390_lowcore.clock_comparator = clock_comparator_max;
0130     cd = this_cpu_ptr(&comparators);
0131     cd->event_handler(cd);
0132 }
0133 
0134 static int s390_next_event(unsigned long delta,
0135                struct clock_event_device *evt)
0136 {
0137     S390_lowcore.clock_comparator = get_tod_clock() + delta;
0138     set_clock_comparator(S390_lowcore.clock_comparator);
0139     return 0;
0140 }
0141 
0142 /*
0143  * Set up lowcore and control register of the current cpu to
0144  * enable TOD clock and clock comparator interrupts.
0145  */
0146 void init_cpu_timer(void)
0147 {
0148     struct clock_event_device *cd;
0149     int cpu;
0150 
0151     S390_lowcore.clock_comparator = clock_comparator_max;
0152     set_clock_comparator(S390_lowcore.clock_comparator);
0153 
0154     cpu = smp_processor_id();
0155     cd = &per_cpu(comparators, cpu);
0156     cd->name        = "comparator";
0157     cd->features        = CLOCK_EVT_FEAT_ONESHOT;
0158     cd->mult        = 16777;
0159     cd->shift       = 12;
0160     cd->min_delta_ns    = 1;
0161     cd->min_delta_ticks = 1;
0162     cd->max_delta_ns    = LONG_MAX;
0163     cd->max_delta_ticks = ULONG_MAX;
0164     cd->rating      = 400;
0165     cd->cpumask     = cpumask_of(cpu);
0166     cd->set_next_event  = s390_next_event;
0167 
0168     clockevents_register_device(cd);
0169 
0170     /* Enable clock comparator timer interrupt. */
0171     __ctl_set_bit(0,11);
0172 
0173     /* Always allow the timing alert external interrupt. */
0174     __ctl_set_bit(0, 4);
0175 }
0176 
0177 static void clock_comparator_interrupt(struct ext_code ext_code,
0178                        unsigned int param32,
0179                        unsigned long param64)
0180 {
0181     inc_irq_stat(IRQEXT_CLK);
0182     if (S390_lowcore.clock_comparator == clock_comparator_max)
0183         set_clock_comparator(S390_lowcore.clock_comparator);
0184 }
0185 
0186 static void stp_timing_alert(struct stp_irq_parm *);
0187 
0188 static void timing_alert_interrupt(struct ext_code ext_code,
0189                    unsigned int param32, unsigned long param64)
0190 {
0191     inc_irq_stat(IRQEXT_TLA);
0192     if (param32 & 0x00038000)
0193         stp_timing_alert((struct stp_irq_parm *) &param32);
0194 }
0195 
0196 static void stp_reset(void);
0197 
0198 void read_persistent_clock64(struct timespec64 *ts)
0199 {
0200     union tod_clock clk;
0201     u64 delta;
0202 
0203     delta = initial_leap_seconds + TOD_UNIX_EPOCH;
0204     store_tod_clock_ext(&clk);
0205     clk.eitod -= delta;
0206     ext_to_timespec64(&clk, ts);
0207 }
0208 
0209 void __init read_persistent_wall_and_boot_offset(struct timespec64 *wall_time,
0210                          struct timespec64 *boot_offset)
0211 {
0212     struct timespec64 boot_time;
0213     union tod_clock clk;
0214     u64 delta;
0215 
0216     delta = initial_leap_seconds + TOD_UNIX_EPOCH;
0217     clk = tod_clock_base;
0218     clk.eitod -= delta;
0219     ext_to_timespec64(&clk, &boot_time);
0220 
0221     read_persistent_clock64(wall_time);
0222     *boot_offset = timespec64_sub(*wall_time, boot_time);
0223 }
0224 
0225 static u64 read_tod_clock(struct clocksource *cs)
0226 {
0227     unsigned long now, adj;
0228 
0229     preempt_disable(); /* protect from changes to steering parameters */
0230     now = get_tod_clock();
0231     adj = tod_steering_end - now;
0232     if (unlikely((s64) adj > 0))
0233         /*
0234          * manually steer by 1 cycle every 2^16 cycles. This
0235          * corresponds to shifting the tod delta by 15. 1s is
0236          * therefore steered in ~9h. The adjust will decrease
0237          * over time, until it finally reaches 0.
0238          */
0239         now += (tod_steering_delta < 0) ? (adj >> 15) : -(adj >> 15);
0240     preempt_enable();
0241     return now;
0242 }
0243 
0244 static struct clocksource clocksource_tod = {
0245     .name       = "tod",
0246     .rating     = 400,
0247     .read       = read_tod_clock,
0248     .mask       = CLOCKSOURCE_MASK(64),
0249     .mult       = 1000,
0250     .shift      = 12,
0251     .flags      = CLOCK_SOURCE_IS_CONTINUOUS,
0252     .vdso_clock_mode = VDSO_CLOCKMODE_TOD,
0253 };
0254 
0255 struct clocksource * __init clocksource_default_clock(void)
0256 {
0257     return &clocksource_tod;
0258 }
0259 
0260 /*
0261  * Initialize the TOD clock and the CPU timer of
0262  * the boot cpu.
0263  */
0264 void __init time_init(void)
0265 {
0266     /* Reset time synchronization interfaces. */
0267     stp_reset();
0268 
0269     /* request the clock comparator external interrupt */
0270     if (register_external_irq(EXT_IRQ_CLK_COMP, clock_comparator_interrupt))
0271         panic("Couldn't request external interrupt 0x1004");
0272 
0273     /* request the timing alert external interrupt */
0274     if (register_external_irq(EXT_IRQ_TIMING_ALERT, timing_alert_interrupt))
0275         panic("Couldn't request external interrupt 0x1406");
0276 
0277     if (__clocksource_register(&clocksource_tod) != 0)
0278         panic("Could not register TOD clock source");
0279 
0280     /* Enable TOD clock interrupts on the boot cpu. */
0281     init_cpu_timer();
0282 
0283     /* Enable cpu timer interrupts on the boot cpu. */
0284     vtime_init();
0285 }
0286 
0287 static DEFINE_PER_CPU(atomic_t, clock_sync_word);
0288 static DEFINE_MUTEX(stp_mutex);
0289 static unsigned long clock_sync_flags;
0290 
0291 #define CLOCK_SYNC_HAS_STP      0
0292 #define CLOCK_SYNC_STP          1
0293 #define CLOCK_SYNC_STPINFO_VALID    2
0294 
0295 /*
0296  * The get_clock function for the physical clock. It will get the current
0297  * TOD clock, subtract the LPAR offset and write the result to *clock.
0298  * The function returns 0 if the clock is in sync with the external time
0299  * source. If the clock mode is local it will return -EOPNOTSUPP and
0300  * -EAGAIN if the clock is not in sync with the external reference.
0301  */
0302 int get_phys_clock(unsigned long *clock)
0303 {
0304     atomic_t *sw_ptr;
0305     unsigned int sw0, sw1;
0306 
0307     sw_ptr = &get_cpu_var(clock_sync_word);
0308     sw0 = atomic_read(sw_ptr);
0309     *clock = get_tod_clock() - lpar_offset;
0310     sw1 = atomic_read(sw_ptr);
0311     put_cpu_var(clock_sync_word);
0312     if (sw0 == sw1 && (sw0 & 0x80000000U))
0313         /* Success: time is in sync. */
0314         return 0;
0315     if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
0316         return -EOPNOTSUPP;
0317     if (!test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
0318         return -EACCES;
0319     return -EAGAIN;
0320 }
0321 EXPORT_SYMBOL(get_phys_clock);
0322 
0323 /*
0324  * Make get_phys_clock() return -EAGAIN.
0325  */
0326 static void disable_sync_clock(void *dummy)
0327 {
0328     atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
0329     /*
0330      * Clear the in-sync bit 2^31. All get_phys_clock calls will
0331      * fail until the sync bit is turned back on. In addition
0332      * increase the "sequence" counter to avoid the race of an
0333      * stp event and the complete recovery against get_phys_clock.
0334      */
0335     atomic_andnot(0x80000000, sw_ptr);
0336     atomic_inc(sw_ptr);
0337 }
0338 
0339 /*
0340  * Make get_phys_clock() return 0 again.
0341  * Needs to be called from a context disabled for preemption.
0342  */
0343 static void enable_sync_clock(void)
0344 {
0345     atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
0346     atomic_or(0x80000000, sw_ptr);
0347 }
0348 
0349 /*
0350  * Function to check if the clock is in sync.
0351  */
0352 static inline int check_sync_clock(void)
0353 {
0354     atomic_t *sw_ptr;
0355     int rc;
0356 
0357     sw_ptr = &get_cpu_var(clock_sync_word);
0358     rc = (atomic_read(sw_ptr) & 0x80000000U) != 0;
0359     put_cpu_var(clock_sync_word);
0360     return rc;
0361 }
0362 
0363 /*
0364  * Apply clock delta to the global data structures.
0365  * This is called once on the CPU that performed the clock sync.
0366  */
0367 static void clock_sync_global(long delta)
0368 {
0369     unsigned long now, adj;
0370     struct ptff_qto qto;
0371     int cs;
0372 
0373     /* Fixup the monotonic sched clock. */
0374     tod_clock_base.eitod += delta;
0375     /* Adjust TOD steering parameters. */
0376     now = get_tod_clock();
0377     adj = tod_steering_end - now;
0378     if (unlikely((s64) adj >= 0))
0379         /* Calculate how much of the old adjustment is left. */
0380         tod_steering_delta = (tod_steering_delta < 0) ?
0381             -(adj >> 15) : (adj >> 15);
0382     tod_steering_delta += delta;
0383     if ((abs(tod_steering_delta) >> 48) != 0)
0384         panic("TOD clock sync offset %li is too large to drift\n",
0385               tod_steering_delta);
0386     tod_steering_end = now + (abs(tod_steering_delta) << 15);
0387     for (cs = 0; cs < CS_BASES; cs++) {
0388         vdso_data[cs].arch_data.tod_steering_end = tod_steering_end;
0389         vdso_data[cs].arch_data.tod_steering_delta = tod_steering_delta;
0390     }
0391 
0392     /* Update LPAR offset. */
0393     if (ptff_query(PTFF_QTO) && ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
0394         lpar_offset = qto.tod_epoch_difference;
0395     /* Call the TOD clock change notifier. */
0396     atomic_notifier_call_chain(&s390_epoch_delta_notifier, 0, &delta);
0397 }
0398 
0399 /*
0400  * Apply clock delta to the per-CPU data structures of this CPU.
0401  * This is called for each online CPU after the call to clock_sync_global.
0402  */
0403 static void clock_sync_local(long delta)
0404 {
0405     /* Add the delta to the clock comparator. */
0406     if (S390_lowcore.clock_comparator != clock_comparator_max) {
0407         S390_lowcore.clock_comparator += delta;
0408         set_clock_comparator(S390_lowcore.clock_comparator);
0409     }
0410     /* Adjust the last_update_clock time-stamp. */
0411     S390_lowcore.last_update_clock += delta;
0412 }
0413 
0414 /* Single threaded workqueue used for stp sync events */
0415 static struct workqueue_struct *time_sync_wq;
0416 
0417 static void __init time_init_wq(void)
0418 {
0419     if (time_sync_wq)
0420         return;
0421     time_sync_wq = create_singlethread_workqueue("timesync");
0422 }
0423 
0424 struct clock_sync_data {
0425     atomic_t cpus;
0426     int in_sync;
0427     long clock_delta;
0428 };
0429 
0430 /*
0431  * Server Time Protocol (STP) code.
0432  */
0433 static bool stp_online;
0434 static struct stp_sstpi stp_info;
0435 static void *stp_page;
0436 
0437 static void stp_work_fn(struct work_struct *work);
0438 static DECLARE_WORK(stp_work, stp_work_fn);
0439 static struct timer_list stp_timer;
0440 
0441 static int __init early_parse_stp(char *p)
0442 {
0443     return kstrtobool(p, &stp_online);
0444 }
0445 early_param("stp", early_parse_stp);
0446 
0447 /*
0448  * Reset STP attachment.
0449  */
0450 static void __init stp_reset(void)
0451 {
0452     int rc;
0453 
0454     stp_page = (void *) get_zeroed_page(GFP_ATOMIC);
0455     rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
0456     if (rc == 0)
0457         set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
0458     else if (stp_online) {
0459         pr_warn("The real or virtual hardware system does not provide an STP interface\n");
0460         free_page((unsigned long) stp_page);
0461         stp_page = NULL;
0462         stp_online = false;
0463     }
0464 }
0465 
0466 static void stp_timeout(struct timer_list *unused)
0467 {
0468     queue_work(time_sync_wq, &stp_work);
0469 }
0470 
0471 static int __init stp_init(void)
0472 {
0473     if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
0474         return 0;
0475     timer_setup(&stp_timer, stp_timeout, 0);
0476     time_init_wq();
0477     if (!stp_online)
0478         return 0;
0479     queue_work(time_sync_wq, &stp_work);
0480     return 0;
0481 }
0482 
0483 arch_initcall(stp_init);
0484 
0485 /*
0486  * STP timing alert. There are three causes:
0487  * 1) timing status change
0488  * 2) link availability change
0489  * 3) time control parameter change
0490  * In all three cases we are only interested in the clock source state.
0491  * If a STP clock source is now available use it.
0492  */
0493 static void stp_timing_alert(struct stp_irq_parm *intparm)
0494 {
0495     if (intparm->tsc || intparm->lac || intparm->tcpc)
0496         queue_work(time_sync_wq, &stp_work);
0497 }
0498 
0499 /*
0500  * STP sync check machine check. This is called when the timing state
0501  * changes from the synchronized state to the unsynchronized state.
0502  * After a STP sync check the clock is not in sync. The machine check
0503  * is broadcasted to all cpus at the same time.
0504  */
0505 int stp_sync_check(void)
0506 {
0507     disable_sync_clock(NULL);
0508     return 1;
0509 }
0510 
0511 /*
0512  * STP island condition machine check. This is called when an attached
0513  * server  attempts to communicate over an STP link and the servers
0514  * have matching CTN ids and have a valid stratum-1 configuration
0515  * but the configurations do not match.
0516  */
0517 int stp_island_check(void)
0518 {
0519     disable_sync_clock(NULL);
0520     return 1;
0521 }
0522 
0523 void stp_queue_work(void)
0524 {
0525     queue_work(time_sync_wq, &stp_work);
0526 }
0527 
0528 static int __store_stpinfo(void)
0529 {
0530     int rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
0531 
0532     if (rc)
0533         clear_bit(CLOCK_SYNC_STPINFO_VALID, &clock_sync_flags);
0534     else
0535         set_bit(CLOCK_SYNC_STPINFO_VALID, &clock_sync_flags);
0536     return rc;
0537 }
0538 
0539 static int stpinfo_valid(void)
0540 {
0541     return stp_online && test_bit(CLOCK_SYNC_STPINFO_VALID, &clock_sync_flags);
0542 }
0543 
0544 static int stp_sync_clock(void *data)
0545 {
0546     struct clock_sync_data *sync = data;
0547     long clock_delta, flags;
0548     static int first;
0549     int rc;
0550 
0551     enable_sync_clock();
0552     if (xchg(&first, 1) == 0) {
0553         /* Wait until all other cpus entered the sync function. */
0554         while (atomic_read(&sync->cpus) != 0)
0555             cpu_relax();
0556         rc = 0;
0557         if (stp_info.todoff || stp_info.tmd != 2) {
0558             flags = vdso_update_begin();
0559             rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0,
0560                     &clock_delta);
0561             if (rc == 0) {
0562                 sync->clock_delta = clock_delta;
0563                 clock_sync_global(clock_delta);
0564                 rc = __store_stpinfo();
0565                 if (rc == 0 && stp_info.tmd != 2)
0566                     rc = -EAGAIN;
0567             }
0568             vdso_update_end(flags);
0569         }
0570         sync->in_sync = rc ? -EAGAIN : 1;
0571         xchg(&first, 0);
0572     } else {
0573         /* Slave */
0574         atomic_dec(&sync->cpus);
0575         /* Wait for in_sync to be set. */
0576         while (READ_ONCE(sync->in_sync) == 0)
0577             __udelay(1);
0578     }
0579     if (sync->in_sync != 1)
0580         /* Didn't work. Clear per-cpu in sync bit again. */
0581         disable_sync_clock(NULL);
0582     /* Apply clock delta to per-CPU fields of this CPU. */
0583     clock_sync_local(sync->clock_delta);
0584 
0585     return 0;
0586 }
0587 
0588 static int stp_clear_leap(void)
0589 {
0590     struct __kernel_timex txc;
0591     int ret;
0592 
0593     memset(&txc, 0, sizeof(txc));
0594 
0595     ret = do_adjtimex(&txc);
0596     if (ret < 0)
0597         return ret;
0598 
0599     txc.modes = ADJ_STATUS;
0600     txc.status &= ~(STA_INS|STA_DEL);
0601     return do_adjtimex(&txc);
0602 }
0603 
0604 static void stp_check_leap(void)
0605 {
0606     struct stp_stzi stzi;
0607     struct stp_lsoib *lsoib = &stzi.lsoib;
0608     struct __kernel_timex txc;
0609     int64_t timediff;
0610     int leapdiff, ret;
0611 
0612     if (!stp_info.lu || !check_sync_clock()) {
0613         /*
0614          * Either a scheduled leap second was removed by the operator,
0615          * or STP is out of sync. In both cases, clear the leap second
0616          * kernel flags.
0617          */
0618         if (stp_clear_leap() < 0)
0619             pr_err("failed to clear leap second flags\n");
0620         return;
0621     }
0622 
0623     if (chsc_stzi(stp_page, &stzi, sizeof(stzi))) {
0624         pr_err("stzi failed\n");
0625         return;
0626     }
0627 
0628     timediff = tod_to_ns(lsoib->nlsout - get_tod_clock()) / NSEC_PER_SEC;
0629     leapdiff = lsoib->nlso - lsoib->also;
0630 
0631     if (leapdiff != 1 && leapdiff != -1) {
0632         pr_err("Cannot schedule %d leap seconds\n", leapdiff);
0633         return;
0634     }
0635 
0636     if (timediff < 0) {
0637         if (stp_clear_leap() < 0)
0638             pr_err("failed to clear leap second flags\n");
0639     } else if (timediff < 7200) {
0640         memset(&txc, 0, sizeof(txc));
0641         ret = do_adjtimex(&txc);
0642         if (ret < 0)
0643             return;
0644 
0645         txc.modes = ADJ_STATUS;
0646         if (leapdiff > 0)
0647             txc.status |= STA_INS;
0648         else
0649             txc.status |= STA_DEL;
0650         ret = do_adjtimex(&txc);
0651         if (ret < 0)
0652             pr_err("failed to set leap second flags\n");
0653         /* arm Timer to clear leap second flags */
0654         mod_timer(&stp_timer, jiffies + msecs_to_jiffies(14400 * MSEC_PER_SEC));
0655     } else {
0656         /* The day the leap second is scheduled for hasn't been reached. Retry
0657          * in one hour.
0658          */
0659         mod_timer(&stp_timer, jiffies + msecs_to_jiffies(3600 * MSEC_PER_SEC));
0660     }
0661 }
0662 
0663 /*
0664  * STP work. Check for the STP state and take over the clock
0665  * synchronization if the STP clock source is usable.
0666  */
0667 static void stp_work_fn(struct work_struct *work)
0668 {
0669     struct clock_sync_data stp_sync;
0670     int rc;
0671 
0672     /* prevent multiple execution. */
0673     mutex_lock(&stp_mutex);
0674 
0675     if (!stp_online) {
0676         chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
0677         del_timer_sync(&stp_timer);
0678         goto out_unlock;
0679     }
0680 
0681     rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xf0e0, NULL);
0682     if (rc)
0683         goto out_unlock;
0684 
0685     rc = __store_stpinfo();
0686     if (rc || stp_info.c == 0)
0687         goto out_unlock;
0688 
0689     /* Skip synchronization if the clock is already in sync. */
0690     if (!check_sync_clock()) {
0691         memset(&stp_sync, 0, sizeof(stp_sync));
0692         cpus_read_lock();
0693         atomic_set(&stp_sync.cpus, num_online_cpus() - 1);
0694         stop_machine_cpuslocked(stp_sync_clock, &stp_sync, cpu_online_mask);
0695         cpus_read_unlock();
0696     }
0697 
0698     if (!check_sync_clock())
0699         /*
0700          * There is a usable clock but the synchonization failed.
0701          * Retry after a second.
0702          */
0703         mod_timer(&stp_timer, jiffies + msecs_to_jiffies(MSEC_PER_SEC));
0704     else if (stp_info.lu)
0705         stp_check_leap();
0706 
0707 out_unlock:
0708     mutex_unlock(&stp_mutex);
0709 }
0710 
0711 /*
0712  * STP subsys sysfs interface functions
0713  */
0714 static struct bus_type stp_subsys = {
0715     .name       = "stp",
0716     .dev_name   = "stp",
0717 };
0718 
0719 static ssize_t ctn_id_show(struct device *dev,
0720                 struct device_attribute *attr,
0721                 char *buf)
0722 {
0723     ssize_t ret = -ENODATA;
0724 
0725     mutex_lock(&stp_mutex);
0726     if (stpinfo_valid())
0727         ret = sprintf(buf, "%016lx\n",
0728                   *(unsigned long *) stp_info.ctnid);
0729     mutex_unlock(&stp_mutex);
0730     return ret;
0731 }
0732 
0733 static DEVICE_ATTR_RO(ctn_id);
0734 
0735 static ssize_t ctn_type_show(struct device *dev,
0736                 struct device_attribute *attr,
0737                 char *buf)
0738 {
0739     ssize_t ret = -ENODATA;
0740 
0741     mutex_lock(&stp_mutex);
0742     if (stpinfo_valid())
0743         ret = sprintf(buf, "%i\n", stp_info.ctn);
0744     mutex_unlock(&stp_mutex);
0745     return ret;
0746 }
0747 
0748 static DEVICE_ATTR_RO(ctn_type);
0749 
0750 static ssize_t dst_offset_show(struct device *dev,
0751                    struct device_attribute *attr,
0752                    char *buf)
0753 {
0754     ssize_t ret = -ENODATA;
0755 
0756     mutex_lock(&stp_mutex);
0757     if (stpinfo_valid() && (stp_info.vbits & 0x2000))
0758         ret = sprintf(buf, "%i\n", (int)(s16) stp_info.dsto);
0759     mutex_unlock(&stp_mutex);
0760     return ret;
0761 }
0762 
0763 static DEVICE_ATTR_RO(dst_offset);
0764 
0765 static ssize_t leap_seconds_show(struct device *dev,
0766                     struct device_attribute *attr,
0767                     char *buf)
0768 {
0769     ssize_t ret = -ENODATA;
0770 
0771     mutex_lock(&stp_mutex);
0772     if (stpinfo_valid() && (stp_info.vbits & 0x8000))
0773         ret = sprintf(buf, "%i\n", (int)(s16) stp_info.leaps);
0774     mutex_unlock(&stp_mutex);
0775     return ret;
0776 }
0777 
0778 static DEVICE_ATTR_RO(leap_seconds);
0779 
0780 static ssize_t leap_seconds_scheduled_show(struct device *dev,
0781                         struct device_attribute *attr,
0782                         char *buf)
0783 {
0784     struct stp_stzi stzi;
0785     ssize_t ret;
0786 
0787     mutex_lock(&stp_mutex);
0788     if (!stpinfo_valid() || !(stp_info.vbits & 0x8000) || !stp_info.lu) {
0789         mutex_unlock(&stp_mutex);
0790         return -ENODATA;
0791     }
0792 
0793     ret = chsc_stzi(stp_page, &stzi, sizeof(stzi));
0794     mutex_unlock(&stp_mutex);
0795     if (ret < 0)
0796         return ret;
0797 
0798     if (!stzi.lsoib.p)
0799         return sprintf(buf, "0,0\n");
0800 
0801     return sprintf(buf, "%lu,%d\n",
0802                tod_to_ns(stzi.lsoib.nlsout - TOD_UNIX_EPOCH) / NSEC_PER_SEC,
0803                stzi.lsoib.nlso - stzi.lsoib.also);
0804 }
0805 
0806 static DEVICE_ATTR_RO(leap_seconds_scheduled);
0807 
0808 static ssize_t stratum_show(struct device *dev,
0809                 struct device_attribute *attr,
0810                 char *buf)
0811 {
0812     ssize_t ret = -ENODATA;
0813 
0814     mutex_lock(&stp_mutex);
0815     if (stpinfo_valid())
0816         ret = sprintf(buf, "%i\n", (int)(s16) stp_info.stratum);
0817     mutex_unlock(&stp_mutex);
0818     return ret;
0819 }
0820 
0821 static DEVICE_ATTR_RO(stratum);
0822 
0823 static ssize_t time_offset_show(struct device *dev,
0824                 struct device_attribute *attr,
0825                 char *buf)
0826 {
0827     ssize_t ret = -ENODATA;
0828 
0829     mutex_lock(&stp_mutex);
0830     if (stpinfo_valid() && (stp_info.vbits & 0x0800))
0831         ret = sprintf(buf, "%i\n", (int) stp_info.tto);
0832     mutex_unlock(&stp_mutex);
0833     return ret;
0834 }
0835 
0836 static DEVICE_ATTR_RO(time_offset);
0837 
0838 static ssize_t time_zone_offset_show(struct device *dev,
0839                 struct device_attribute *attr,
0840                 char *buf)
0841 {
0842     ssize_t ret = -ENODATA;
0843 
0844     mutex_lock(&stp_mutex);
0845     if (stpinfo_valid() && (stp_info.vbits & 0x4000))
0846         ret = sprintf(buf, "%i\n", (int)(s16) stp_info.tzo);
0847     mutex_unlock(&stp_mutex);
0848     return ret;
0849 }
0850 
0851 static DEVICE_ATTR_RO(time_zone_offset);
0852 
0853 static ssize_t timing_mode_show(struct device *dev,
0854                 struct device_attribute *attr,
0855                 char *buf)
0856 {
0857     ssize_t ret = -ENODATA;
0858 
0859     mutex_lock(&stp_mutex);
0860     if (stpinfo_valid())
0861         ret = sprintf(buf, "%i\n", stp_info.tmd);
0862     mutex_unlock(&stp_mutex);
0863     return ret;
0864 }
0865 
0866 static DEVICE_ATTR_RO(timing_mode);
0867 
0868 static ssize_t timing_state_show(struct device *dev,
0869                 struct device_attribute *attr,
0870                 char *buf)
0871 {
0872     ssize_t ret = -ENODATA;
0873 
0874     mutex_lock(&stp_mutex);
0875     if (stpinfo_valid())
0876         ret = sprintf(buf, "%i\n", stp_info.tst);
0877     mutex_unlock(&stp_mutex);
0878     return ret;
0879 }
0880 
0881 static DEVICE_ATTR_RO(timing_state);
0882 
0883 static ssize_t online_show(struct device *dev,
0884                 struct device_attribute *attr,
0885                 char *buf)
0886 {
0887     return sprintf(buf, "%i\n", stp_online);
0888 }
0889 
0890 static ssize_t online_store(struct device *dev,
0891                 struct device_attribute *attr,
0892                 const char *buf, size_t count)
0893 {
0894     unsigned int value;
0895 
0896     value = simple_strtoul(buf, NULL, 0);
0897     if (value != 0 && value != 1)
0898         return -EINVAL;
0899     if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
0900         return -EOPNOTSUPP;
0901     mutex_lock(&stp_mutex);
0902     stp_online = value;
0903     if (stp_online)
0904         set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
0905     else
0906         clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
0907     queue_work(time_sync_wq, &stp_work);
0908     mutex_unlock(&stp_mutex);
0909     return count;
0910 }
0911 
0912 /*
0913  * Can't use DEVICE_ATTR because the attribute should be named
0914  * stp/online but dev_attr_online already exists in this file ..
0915  */
0916 static DEVICE_ATTR_RW(online);
0917 
0918 static struct attribute *stp_dev_attrs[] = {
0919     &dev_attr_ctn_id.attr,
0920     &dev_attr_ctn_type.attr,
0921     &dev_attr_dst_offset.attr,
0922     &dev_attr_leap_seconds.attr,
0923     &dev_attr_online.attr,
0924     &dev_attr_leap_seconds_scheduled.attr,
0925     &dev_attr_stratum.attr,
0926     &dev_attr_time_offset.attr,
0927     &dev_attr_time_zone_offset.attr,
0928     &dev_attr_timing_mode.attr,
0929     &dev_attr_timing_state.attr,
0930     NULL
0931 };
0932 ATTRIBUTE_GROUPS(stp_dev);
0933 
0934 static int __init stp_init_sysfs(void)
0935 {
0936     return subsys_system_register(&stp_subsys, stp_dev_groups);
0937 }
0938 
0939 device_initcall(stp_init_sysfs);