time.c 20 KB

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