time.c 21 KB

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