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 /* 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 57 u64 sched_clock_base_cc = -1; /* Force to data section. */ 58 EXPORT_SYMBOL_GPL(sched_clock_base_cc); 59 60 static DEFINE_PER_CPU(struct clock_event_device, comparators); 61 62 ATOMIC_NOTIFIER_HEAD(s390_epoch_delta_notifier); 63 EXPORT_SYMBOL(s390_epoch_delta_notifier); 64 65 unsigned char ptff_function_mask[16]; 66 unsigned long lpar_offset; 67 unsigned long initial_leap_seconds; 68 69 /* 70 * Get time offsets with PTFF 71 */ 72 void __init ptff_init(void) 73 { 74 struct ptff_qto qto; 75 struct ptff_qui qui; 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; 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); 89 } 90 91 /* 92 * Scheduler clock - returns current time in nanosec units. 93 */ 94 unsigned long long notrace sched_clock(void) 95 { 96 return tod_to_ns(get_tod_clock_monotonic()); 97 } 98 NOKPROBE_SYMBOL(sched_clock); 99 100 /* 101 * Monotonic_clock - returns # of nanoseconds passed since time_init() 102 */ 103 unsigned long long monotonic_clock(void) 104 { 105 return sched_clock(); 106 } 107 EXPORT_SYMBOL(monotonic_clock); 108 109 void tod_to_timeval(__u64 todval, struct timespec64 *xt) 110 { 111 unsigned long long sec; 112 113 sec = todval >> 12; 114 do_div(sec, 1000000); 115 xt->tv_sec = sec; 116 todval -= (sec * 1000000) << 12; 117 xt->tv_nsec = ((todval * 1000) >> 12); 118 } 119 EXPORT_SYMBOL(tod_to_timeval); 120 121 void clock_comparator_work(void) 122 { 123 struct clock_event_device *cd; 124 125 S390_lowcore.clock_comparator = -1ULL; 126 cd = this_cpu_ptr(&comparators); 127 cd->event_handler(cd); 128 } 129 130 /* 131 * Fixup the clock comparator. 132 */ 133 static void fixup_clock_comparator(unsigned long long delta) 134 { 135 /* If nobody is waiting there's nothing to fix. */ 136 if (S390_lowcore.clock_comparator == -1ULL) 137 return; 138 S390_lowcore.clock_comparator += delta; 139 set_clock_comparator(S390_lowcore.clock_comparator); 140 } 141 142 static int s390_next_event(unsigned long delta, 143 struct clock_event_device *evt) 144 { 145 S390_lowcore.clock_comparator = get_tod_clock() + delta; 146 set_clock_comparator(S390_lowcore.clock_comparator); 147 return 0; 148 } 149 150 /* 151 * Set up lowcore and control register of the current cpu to 152 * enable TOD clock and clock comparator interrupts. 153 */ 154 void init_cpu_timer(void) 155 { 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"; 165 cd->features = CLOCK_EVT_FEAT_ONESHOT; 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; 171 cd->cpumask = cpumask_of(cpu); 172 cd->set_next_event = s390_next_event; 173 174 clockevents_register_device(cd); 175 176 /* Enable clock comparator timer interrupt. */ 177 __ctl_set_bit(0,11); 178 179 /* Always allow the timing alert external interrupt. */ 180 __ctl_set_bit(0, 4); 181 } 182 183 static void clock_comparator_interrupt(struct ext_code ext_code, 184 unsigned int param32, 185 unsigned long param64) 186 { 187 inc_irq_stat(IRQEXT_CLK); 188 if (S390_lowcore.clock_comparator == -1ULL) 189 set_clock_comparator(S390_lowcore.clock_comparator); 190 } 191 192 static void stp_timing_alert(struct stp_irq_parm *); 193 194 static void timing_alert_interrupt(struct ext_code ext_code, 195 unsigned int param32, unsigned long param64) 196 { 197 inc_irq_stat(IRQEXT_TLA); 198 if (param32 & 0x00038000) 199 stp_timing_alert((struct stp_irq_parm *) ¶m32); 200 } 201 202 static void stp_reset(void); 203 204 void read_persistent_clock64(struct timespec64 *ts) 205 { 206 __u64 clock; 207 208 clock = get_tod_clock() - initial_leap_seconds; 209 tod_to_timeval(clock - TOD_UNIX_EPOCH, ts); 210 } 211 212 void read_boot_clock64(struct timespec64 *ts) 213 { 214 __u64 clock; 215 216 clock = sched_clock_base_cc - initial_leap_seconds; 217 tod_to_timeval(clock - TOD_UNIX_EPOCH, ts); 218 } 219 220 static cycle_t read_tod_clock(struct clocksource *cs) 221 { 222 return get_tod_clock(); 223 } 224 225 static struct clocksource clocksource_tod = { 226 .name = "tod", 227 .rating = 400, 228 .read = read_tod_clock, 229 .mask = -1ULL, 230 .mult = 1000, 231 .shift = 12, 232 .flags = CLOCK_SOURCE_IS_CONTINUOUS, 233 }; 234 235 struct clocksource * __init clocksource_default_clock(void) 236 { 237 return &clocksource_tod; 238 } 239 240 void update_vsyscall(struct timekeeper *tk) 241 { 242 u64 nsecps; 243 244 if (tk->tkr_mono.clock != &clocksource_tod) 245 return; 246 247 /* Make userspace gettimeofday spin until we're done. */ 248 ++vdso_data->tb_update_count; 249 smp_wmb(); 250 vdso_data->xtime_tod_stamp = tk->tkr_mono.cycle_last; 251 vdso_data->xtime_clock_sec = tk->xtime_sec; 252 vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec; 253 vdso_data->wtom_clock_sec = 254 tk->xtime_sec + tk->wall_to_monotonic.tv_sec; 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; 258 while (vdso_data->wtom_clock_nsec >= nsecps) { 259 vdso_data->wtom_clock_nsec -= nsecps; 260 vdso_data->wtom_clock_sec++; 261 } 262 263 vdso_data->xtime_coarse_sec = tk->xtime_sec; 264 vdso_data->xtime_coarse_nsec = 265 (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift); 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 275 vdso_data->tk_mult = tk->tkr_mono.mult; 276 vdso_data->tk_shift = tk->tkr_mono.shift; 277 smp_wmb(); 278 ++vdso_data->tb_update_count; 279 } 280 281 extern struct timezone sys_tz; 282 283 void 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 294 /* 295 * Initialize the TOD clock and the CPU timer of 296 * the boot cpu. 297 */ 298 void __init time_init(void) 299 { 300 /* Reset time synchronization interfaces. */ 301 stp_reset(); 302 303 /* request the clock comparator external interrupt */ 304 if (register_external_irq(EXT_IRQ_CLK_COMP, clock_comparator_interrupt)) 305 panic("Couldn't request external interrupt 0x1004"); 306 307 /* request the timing alert external interrupt */ 308 if (register_external_irq(EXT_IRQ_TIMING_ALERT, timing_alert_interrupt)) 309 panic("Couldn't request external interrupt 0x1406"); 310 311 if (__clocksource_register(&clocksource_tod) != 0) 312 panic("Could not register TOD clock source"); 313 314 /* Enable TOD clock interrupts on the boot cpu. */ 315 init_cpu_timer(); 316 317 /* Enable cpu timer interrupts on the boot cpu. */ 318 vtime_init(); 319 } 320 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 */ 328 static 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 } 348 sched_clock_base_cc += delta; 349 if (adjust.offset != 0) { 350 pr_notice("The ETR interface has adjusted the clock " 351 "by %li microseconds\n", adjust.offset); 352 adjust.modes = ADJ_OFFSET_SINGLESHOT; 353 do_adjtimex(&adjust); 354 } 355 return delta; 356 } 357 358 static DEFINE_PER_CPU(atomic_t, clock_sync_word); 359 static DEFINE_MUTEX(clock_sync_mutex); 360 static 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 /* 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. 373 */ 374 int get_phys_clock(unsigned long long *clock) 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); 381 *clock = get_tod_clock() - lpar_offset; 382 sw1 = atomic_read(sw_ptr); 383 put_cpu_var(clock_sync_word); 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)) 389 return -EOPNOTSUPP; 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 } 395 EXPORT_SYMBOL(get_phys_clock); 396 397 /* 398 * Make get_sync_clock return -EAGAIN. 399 */ 400 static void disable_sync_clock(void *dummy) 401 { 402 atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word); 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 */ 409 atomic_andnot(0x80000000, sw_ptr); 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 */ 417 static void enable_sync_clock(void) 418 { 419 atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word); 420 atomic_or(0x80000000, sw_ptr); 421 } 422 423 /* 424 * Function to check if the clock is in sync. 425 */ 426 static 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; 433 put_cpu_var(clock_sync_word); 434 return rc; 435 } 436 437 /* Single threaded workqueue used for etr and stp sync events */ 438 static struct workqueue_struct *time_sync_wq; 439 440 static void __init time_init_wq(void) 441 { 442 if (time_sync_wq) 443 return; 444 time_sync_wq = create_singlethread_workqueue("timesync"); 445 } 446 447 struct clock_sync_data { 448 atomic_t cpus; 449 int in_sync; 450 unsigned long long fixup_cc; 451 int etr_port; 452 struct etr_aib *etr_aib; 453 }; 454 455 static void clock_sync_cpu(struct clock_sync_data *sync) 456 { 457 atomic_dec(&sync->cpus); 458 enable_sync_clock(); 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 */ 465 while (sync->in_sync == 0) { 466 __udelay(1); 467 /* 468 * A different cpu changes *in_sync. Therefore use 469 * barrier() to force memory access. 470 */ 471 barrier(); 472 } 473 if (sync->in_sync != 1) 474 /* Didn't work. Clear per-cpu in sync bit again. */ 475 disable_sync_clock(NULL); 476 /* 477 * This round of TOD syncing is done. Set the clock comparator 478 * to the next tick and let the processor continue. 479 */ 480 fixup_clock_comparator(sync->fixup_cc); 481 } 482 483 /* 484 * Server Time Protocol (STP) code. 485 */ 486 static bool stp_online; 487 static struct stp_sstpi stp_info; 488 static void *stp_page; 489 490 static void stp_work_fn(struct work_struct *work); 491 static DEFINE_MUTEX(stp_work_mutex); 492 static DECLARE_WORK(stp_work, stp_work_fn); 493 static struct timer_list stp_timer; 494 495 static int __init early_parse_stp(char *p) 496 { 497 return kstrtobool(p, &stp_online); 498 } 499 early_param("stp", early_parse_stp); 500 501 /* 502 * Reset STP attachment. 503 */ 504 static void __init stp_reset(void) 505 { 506 int rc; 507 508 stp_page = (void *) get_zeroed_page(GFP_ATOMIC); 509 rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL); 510 if (rc == 0) 511 set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags); 512 else if (stp_online) { 513 pr_warn("The real or virtual hardware system does not provide an STP interface\n"); 514 free_page((unsigned long) stp_page); 515 stp_page = NULL; 516 stp_online = 0; 517 } 518 } 519 520 static void stp_timeout(unsigned long dummy) 521 { 522 queue_work(time_sync_wq, &stp_work); 523 } 524 525 static int __init stp_init(void) 526 { 527 if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags)) 528 return 0; 529 setup_timer(&stp_timer, stp_timeout, 0UL); 530 time_init_wq(); 531 if (!stp_online) 532 return 0; 533 queue_work(time_sync_wq, &stp_work); 534 return 0; 535 } 536 537 arch_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 */ 547 static void stp_timing_alert(struct stp_irq_parm *intparm) 548 { 549 if (intparm->tsc || intparm->lac || intparm->tcpc) 550 queue_work(time_sync_wq, &stp_work); 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 */ 559 int stp_sync_check(void) 560 { 561 disable_sync_clock(NULL); 562 return 1; 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 */ 571 int stp_island_check(void) 572 { 573 disable_sync_clock(NULL); 574 return 1; 575 } 576 577 void stp_queue_work(void) 578 { 579 queue_work(time_sync_wq, &stp_work); 580 } 581 582 static int stp_sync_clock(void *data) 583 { 584 static int first; 585 unsigned long long old_clock, delta, new_clock, clock_delta; 586 struct clock_sync_data *stp_sync; 587 struct ptff_qto qto; 588 int rc; 589 590 stp_sync = data; 591 592 if (xchg(&first, 1) == 1) { 593 /* Slave */ 594 clock_sync_cpu(stp_sync); 595 return 0; 596 } 597 598 /* Wait until all other cpus entered the sync function. */ 599 while (atomic_read(&stp_sync->cpus) != 0) 600 cpu_relax(); 601 602 enable_sync_clock(); 603 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) { 608 old_clock = get_tod_clock(); 609 rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0, &clock_delta); 610 if (rc == 0) { 611 new_clock = old_clock + clock_delta; 612 delta = adjust_time(old_clock, new_clock, 0); 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; 617 atomic_notifier_call_chain(&s390_epoch_delta_notifier, 618 0, &clock_delta); 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); 628 stp_sync->in_sync = -EAGAIN; 629 } else 630 stp_sync->in_sync = 1; 631 xchg(&first, 0); 632 return 0; 633 } 634 635 /* 636 * STP work. Check for the STP state and take over the clock 637 * synchronization if the STP clock source is usable. 638 */ 639 static void stp_work_fn(struct work_struct *work) 640 { 641 struct clock_sync_data stp_sync; 642 int rc; 643 644 /* prevent multiple execution. */ 645 mutex_lock(&stp_work_mutex); 646 647 if (!stp_online) { 648 chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL); 649 del_timer_sync(&stp_timer); 650 goto out_unlock; 651 } 652 653 rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xb0e0, NULL); 654 if (rc) 655 goto out_unlock; 656 657 rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi)); 658 if (rc || stp_info.c == 0) 659 goto out_unlock; 660 661 /* Skip synchronization if the clock is already in sync. */ 662 if (check_sync_clock()) 663 goto out_unlock; 664 665 memset(&stp_sync, 0, sizeof(stp_sync)); 666 get_online_cpus(); 667 atomic_set(&stp_sync.cpus, num_online_cpus() - 1); 668 stop_machine(stp_sync_clock, &stp_sync, cpu_online_mask); 669 put_online_cpus(); 670 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 678 out_unlock: 679 mutex_unlock(&stp_work_mutex); 680 } 681 682 /* 683 * STP subsys sysfs interface functions 684 */ 685 static struct bus_type stp_subsys = { 686 .name = "stp", 687 .dev_name = "stp", 688 }; 689 690 static ssize_t stp_ctn_id_show(struct device *dev, 691 struct device_attribute *attr, 692 char *buf) 693 { 694 if (!stp_online) 695 return -ENODATA; 696 return sprintf(buf, "%016llx\n", 697 *(unsigned long long *) stp_info.ctnid); 698 } 699 700 static DEVICE_ATTR(ctn_id, 0400, stp_ctn_id_show, NULL); 701 702 static ssize_t stp_ctn_type_show(struct device *dev, 703 struct device_attribute *attr, 704 char *buf) 705 { 706 if (!stp_online) 707 return -ENODATA; 708 return sprintf(buf, "%i\n", stp_info.ctn); 709 } 710 711 static DEVICE_ATTR(ctn_type, 0400, stp_ctn_type_show, NULL); 712 713 static ssize_t stp_dst_offset_show(struct device *dev, 714 struct device_attribute *attr, 715 char *buf) 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 722 static DEVICE_ATTR(dst_offset, 0400, stp_dst_offset_show, NULL); 723 724 static ssize_t stp_leap_seconds_show(struct device *dev, 725 struct device_attribute *attr, 726 char *buf) 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 733 static DEVICE_ATTR(leap_seconds, 0400, stp_leap_seconds_show, NULL); 734 735 static ssize_t stp_stratum_show(struct device *dev, 736 struct device_attribute *attr, 737 char *buf) 738 { 739 if (!stp_online) 740 return -ENODATA; 741 return sprintf(buf, "%i\n", (int)(s16) stp_info.stratum); 742 } 743 744 static DEVICE_ATTR(stratum, 0400, stp_stratum_show, NULL); 745 746 static ssize_t stp_time_offset_show(struct device *dev, 747 struct device_attribute *attr, 748 char *buf) 749 { 750 if (!stp_online || !(stp_info.vbits & 0x0800)) 751 return -ENODATA; 752 return sprintf(buf, "%i\n", (int) stp_info.tto); 753 } 754 755 static DEVICE_ATTR(time_offset, 0400, stp_time_offset_show, NULL); 756 757 static ssize_t stp_time_zone_offset_show(struct device *dev, 758 struct device_attribute *attr, 759 char *buf) 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 766 static DEVICE_ATTR(time_zone_offset, 0400, 767 stp_time_zone_offset_show, NULL); 768 769 static ssize_t stp_timing_mode_show(struct device *dev, 770 struct device_attribute *attr, 771 char *buf) 772 { 773 if (!stp_online) 774 return -ENODATA; 775 return sprintf(buf, "%i\n", stp_info.tmd); 776 } 777 778 static DEVICE_ATTR(timing_mode, 0400, stp_timing_mode_show, NULL); 779 780 static ssize_t stp_timing_state_show(struct device *dev, 781 struct device_attribute *attr, 782 char *buf) 783 { 784 if (!stp_online) 785 return -ENODATA; 786 return sprintf(buf, "%i\n", stp_info.tst); 787 } 788 789 static DEVICE_ATTR(timing_state, 0400, stp_timing_state_show, NULL); 790 791 static ssize_t stp_online_show(struct device *dev, 792 struct device_attribute *attr, 793 char *buf) 794 { 795 return sprintf(buf, "%i\n", stp_online); 796 } 797 798 static ssize_t stp_online_store(struct device *dev, 799 struct device_attribute *attr, 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; 809 mutex_lock(&clock_sync_mutex); 810 stp_online = value; 811 if (stp_online) 812 set_bit(CLOCK_SYNC_STP, &clock_sync_flags); 813 else 814 clear_bit(CLOCK_SYNC_STP, &clock_sync_flags); 815 queue_work(time_sync_wq, &stp_work); 816 mutex_unlock(&clock_sync_mutex); 817 return count; 818 } 819 820 /* 821 * Can't use DEVICE_ATTR because the attribute should be named 822 * stp/online but dev_attr_online already exists in this file .. 823 */ 824 static struct device_attribute dev_attr_stp_online = { 825 .attr = { .name = "online", .mode = 0600 }, 826 .show = stp_online_show, 827 .store = stp_online_store, 828 }; 829 830 static 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, 841 NULL 842 }; 843 844 static int __init stp_init_sysfs(void) 845 { 846 struct device_attribute **attr; 847 int rc; 848 849 rc = subsys_system_register(&stp_subsys, NULL); 850 if (rc) 851 goto out; 852 for (attr = stp_attributes; *attr; attr++) { 853 rc = device_create_file(stp_subsys.dev_root, *attr); 854 if (rc) 855 goto out_unreg; 856 } 857 return 0; 858 out_unreg: 859 for (; attr >= stp_attributes; attr--) 860 device_remove_file(stp_subsys.dev_root, *attr); 861 bus_unregister(&stp_subsys); 862 out: 863 return rc; 864 } 865 866 device_initcall(stp_init_sysfs); 867