time.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Time of day based timer functions.
  4. *
  5. * S390 version
  6. * Copyright IBM Corp. 1999, 2008
  7. * Author(s): Hartmut Penner (hp@de.ibm.com),
  8. * Martin Schwidefsky (schwidefsky@de.ibm.com),
  9. * Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
  10. *
  11. * Derived from "arch/i386/kernel/time.c"
  12. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  13. */
  14. #define KMSG_COMPONENT "time"
  15. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  16. #include <linux/kernel_stat.h>
  17. #include <linux/errno.h>
  18. #include <linux/export.h>
  19. #include <linux/sched.h>
  20. #include <linux/sched/clock.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 <linux/uaccess.h>
  42. #include <vdso/vsyscall.h>
  43. #include <vdso/clocksource.h>
  44. #include <vdso/helpers.h>
  45. #include <asm/facility.h>
  46. #include <asm/delay.h>
  47. #include <asm/div64.h>
  48. #include <asm/vdso.h>
  49. #include <asm/irq.h>
  50. #include <asm/irq_regs.h>
  51. #include <asm/vtimer.h>
  52. #include <asm/stp.h>
  53. #include <asm/cio.h>
  54. #include "entry.h"
  55. union tod_clock tod_clock_base __section(".data");
  56. EXPORT_SYMBOL_GPL(tod_clock_base);
  57. u64 clock_comparator_max = -1ULL;
  58. EXPORT_SYMBOL_GPL(clock_comparator_max);
  59. static DEFINE_PER_CPU(struct clock_event_device, comparators);
  60. ATOMIC_NOTIFIER_HEAD(s390_epoch_delta_notifier);
  61. EXPORT_SYMBOL(s390_epoch_delta_notifier);
  62. unsigned char ptff_function_mask[16];
  63. static unsigned long lpar_offset;
  64. static unsigned long initial_leap_seconds;
  65. static unsigned long tod_steering_end;
  66. static long tod_steering_delta;
  67. /*
  68. * Get time offsets with PTFF
  69. */
  70. void __init time_early_init(void)
  71. {
  72. struct ptff_qto qto;
  73. struct ptff_qui qui;
  74. int cs;
  75. /* Initialize TOD steering parameters */
  76. tod_steering_end = tod_clock_base.tod;
  77. for (cs = 0; cs < CS_BASES; cs++)
  78. vdso_data[cs].arch_data.tod_steering_end = tod_steering_end;
  79. if (!test_facility(28))
  80. return;
  81. ptff(&ptff_function_mask, sizeof(ptff_function_mask), PTFF_QAF);
  82. /* get LPAR offset */
  83. if (ptff_query(PTFF_QTO) && ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
  84. lpar_offset = qto.tod_epoch_difference;
  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. unsigned long long noinstr sched_clock_noinstr(void)
  91. {
  92. return tod_to_ns(__get_tod_clock_monotonic());
  93. }
  94. /*
  95. * Scheduler clock - returns current time in nanosec units.
  96. */
  97. unsigned long long notrace sched_clock(void)
  98. {
  99. return tod_to_ns(get_tod_clock_monotonic());
  100. }
  101. NOKPROBE_SYMBOL(sched_clock);
  102. static void ext_to_timespec64(union tod_clock *clk, struct timespec64 *xt)
  103. {
  104. unsigned long rem, sec, nsec;
  105. sec = clk->us;
  106. rem = do_div(sec, 1000000);
  107. nsec = ((clk->sus + (rem << 12)) * 125) >> 9;
  108. xt->tv_sec = sec;
  109. xt->tv_nsec = nsec;
  110. }
  111. void clock_comparator_work(void)
  112. {
  113. struct clock_event_device *cd;
  114. get_lowcore()->clock_comparator = clock_comparator_max;
  115. cd = this_cpu_ptr(&comparators);
  116. cd->event_handler(cd);
  117. }
  118. static int s390_next_event(unsigned long delta,
  119. struct clock_event_device *evt)
  120. {
  121. get_lowcore()->clock_comparator = get_tod_clock() + delta;
  122. set_clock_comparator(get_lowcore()->clock_comparator);
  123. return 0;
  124. }
  125. /*
  126. * Set up lowcore and control register of the current cpu to
  127. * enable TOD clock and clock comparator interrupts.
  128. */
  129. void init_cpu_timer(void)
  130. {
  131. struct clock_event_device *cd;
  132. int cpu;
  133. get_lowcore()->clock_comparator = clock_comparator_max;
  134. set_clock_comparator(get_lowcore()->clock_comparator);
  135. cpu = smp_processor_id();
  136. cd = &per_cpu(comparators, cpu);
  137. cd->name = "comparator";
  138. cd->features = CLOCK_EVT_FEAT_ONESHOT;
  139. cd->mult = 16777;
  140. cd->shift = 12;
  141. cd->min_delta_ns = 1;
  142. cd->min_delta_ticks = 1;
  143. cd->max_delta_ns = LONG_MAX;
  144. cd->max_delta_ticks = ULONG_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. local_ctl_set_bit(0, CR0_CLOCK_COMPARATOR_SUBMASK_BIT);
  151. /* Always allow the timing alert external interrupt. */
  152. local_ctl_set_bit(0, CR0_ETR_SUBMASK_BIT);
  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 (get_lowcore()->clock_comparator == clock_comparator_max)
  160. set_clock_comparator(get_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. union tod_clock clk;
  174. u64 delta;
  175. delta = initial_leap_seconds + TOD_UNIX_EPOCH;
  176. store_tod_clock_ext(&clk);
  177. clk.eitod -= delta;
  178. ext_to_timespec64(&clk, ts);
  179. }
  180. void __init read_persistent_wall_and_boot_offset(struct timespec64 *wall_time,
  181. struct timespec64 *boot_offset)
  182. {
  183. struct timespec64 boot_time;
  184. union tod_clock clk;
  185. u64 delta;
  186. delta = initial_leap_seconds + TOD_UNIX_EPOCH;
  187. clk = tod_clock_base;
  188. clk.eitod -= delta;
  189. ext_to_timespec64(&clk, &boot_time);
  190. read_persistent_clock64(wall_time);
  191. *boot_offset = timespec64_sub(*wall_time, boot_time);
  192. }
  193. static u64 read_tod_clock(struct clocksource *cs)
  194. {
  195. unsigned long now, adj;
  196. preempt_disable(); /* protect from changes to steering parameters */
  197. now = get_tod_clock();
  198. adj = tod_steering_end - now;
  199. if (unlikely((s64) adj > 0))
  200. /*
  201. * manually steer by 1 cycle every 2^16 cycles. This
  202. * corresponds to shifting the tod delta by 15. 1s is
  203. * therefore steered in ~9h. The adjust will decrease
  204. * over time, until it finally reaches 0.
  205. */
  206. now += (tod_steering_delta < 0) ? (adj >> 15) : -(adj >> 15);
  207. preempt_enable();
  208. return now;
  209. }
  210. static struct clocksource clocksource_tod = {
  211. .name = "tod",
  212. .rating = 400,
  213. .read = read_tod_clock,
  214. .mask = CLOCKSOURCE_MASK(64),
  215. .mult = 4096000,
  216. .shift = 24,
  217. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  218. .vdso_clock_mode = VDSO_CLOCKMODE_TOD,
  219. };
  220. struct clocksource * __init clocksource_default_clock(void)
  221. {
  222. return &clocksource_tod;
  223. }
  224. /*
  225. * Initialize the TOD clock and the CPU timer of
  226. * the boot cpu.
  227. */
  228. void __init time_init(void)
  229. {
  230. /* Reset time synchronization interfaces. */
  231. stp_reset();
  232. /* request the clock comparator external interrupt */
  233. if (register_external_irq(EXT_IRQ_CLK_COMP, clock_comparator_interrupt))
  234. panic("Couldn't request external interrupt 0x1004");
  235. /* request the timing alert external interrupt */
  236. if (register_external_irq(EXT_IRQ_TIMING_ALERT, timing_alert_interrupt))
  237. panic("Couldn't request external interrupt 0x1406");
  238. if (__clocksource_register(&clocksource_tod) != 0)
  239. panic("Could not register TOD clock source");
  240. /* Enable TOD clock interrupts on the boot cpu. */
  241. init_cpu_timer();
  242. /* Enable cpu timer interrupts on the boot cpu. */
  243. vtime_init();
  244. }
  245. static DEFINE_PER_CPU(atomic_t, clock_sync_word);
  246. static DEFINE_MUTEX(stp_mutex);
  247. static unsigned long clock_sync_flags;
  248. #define CLOCK_SYNC_HAS_STP 0
  249. #define CLOCK_SYNC_STP 1
  250. #define CLOCK_SYNC_STPINFO_VALID 2
  251. /*
  252. * The get_clock function for the physical clock. It will get the current
  253. * TOD clock, subtract the LPAR offset and write the result to *clock.
  254. * The function returns 0 if the clock is in sync with the external time
  255. * source. If the clock mode is local it will return -EOPNOTSUPP and
  256. * -EAGAIN if the clock is not in sync with the external reference.
  257. */
  258. int get_phys_clock(unsigned long *clock)
  259. {
  260. atomic_t *sw_ptr;
  261. unsigned int sw0, sw1;
  262. sw_ptr = &get_cpu_var(clock_sync_word);
  263. sw0 = atomic_read(sw_ptr);
  264. *clock = get_tod_clock() - lpar_offset;
  265. sw1 = atomic_read(sw_ptr);
  266. put_cpu_var(clock_sync_word);
  267. if (sw0 == sw1 && (sw0 & 0x80000000U))
  268. /* Success: time is in sync. */
  269. return 0;
  270. if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
  271. return -EOPNOTSUPP;
  272. if (!test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
  273. return -EACCES;
  274. return -EAGAIN;
  275. }
  276. EXPORT_SYMBOL(get_phys_clock);
  277. /*
  278. * Make get_phys_clock() return -EAGAIN.
  279. */
  280. static void disable_sync_clock(void *dummy)
  281. {
  282. atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
  283. /*
  284. * Clear the in-sync bit 2^31. All get_phys_clock calls will
  285. * fail until the sync bit is turned back on. In addition
  286. * increase the "sequence" counter to avoid the race of an
  287. * stp event and the complete recovery against get_phys_clock.
  288. */
  289. atomic_andnot(0x80000000, sw_ptr);
  290. atomic_inc(sw_ptr);
  291. }
  292. /*
  293. * Make get_phys_clock() return 0 again.
  294. * Needs to be called from a context disabled for preemption.
  295. */
  296. static void enable_sync_clock(void)
  297. {
  298. atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
  299. atomic_or(0x80000000, sw_ptr);
  300. }
  301. /*
  302. * Function to check if the clock is in sync.
  303. */
  304. static inline int check_sync_clock(void)
  305. {
  306. atomic_t *sw_ptr;
  307. int rc;
  308. sw_ptr = &get_cpu_var(clock_sync_word);
  309. rc = (atomic_read(sw_ptr) & 0x80000000U) != 0;
  310. put_cpu_var(clock_sync_word);
  311. return rc;
  312. }
  313. /*
  314. * Apply clock delta to the global data structures.
  315. * This is called once on the CPU that performed the clock sync.
  316. */
  317. static void clock_sync_global(long delta)
  318. {
  319. unsigned long now, adj;
  320. struct ptff_qto qto;
  321. int cs;
  322. /* Fixup the monotonic sched clock. */
  323. tod_clock_base.eitod += delta;
  324. /* Adjust TOD steering parameters. */
  325. now = get_tod_clock();
  326. adj = tod_steering_end - now;
  327. if (unlikely((s64) adj >= 0))
  328. /* Calculate how much of the old adjustment is left. */
  329. tod_steering_delta = (tod_steering_delta < 0) ?
  330. -(adj >> 15) : (adj >> 15);
  331. tod_steering_delta += delta;
  332. if ((abs(tod_steering_delta) >> 48) != 0)
  333. panic("TOD clock sync offset %li is too large to drift\n",
  334. tod_steering_delta);
  335. tod_steering_end = now + (abs(tod_steering_delta) << 15);
  336. for (cs = 0; cs < CS_BASES; cs++) {
  337. vdso_data[cs].arch_data.tod_steering_end = tod_steering_end;
  338. vdso_data[cs].arch_data.tod_steering_delta = tod_steering_delta;
  339. }
  340. /* Update LPAR offset. */
  341. if (ptff_query(PTFF_QTO) && ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
  342. lpar_offset = qto.tod_epoch_difference;
  343. /* Call the TOD clock change notifier. */
  344. atomic_notifier_call_chain(&s390_epoch_delta_notifier, 0, &delta);
  345. }
  346. /*
  347. * Apply clock delta to the per-CPU data structures of this CPU.
  348. * This is called for each online CPU after the call to clock_sync_global.
  349. */
  350. static void clock_sync_local(long delta)
  351. {
  352. /* Add the delta to the clock comparator. */
  353. if (get_lowcore()->clock_comparator != clock_comparator_max) {
  354. get_lowcore()->clock_comparator += delta;
  355. set_clock_comparator(get_lowcore()->clock_comparator);
  356. }
  357. /* Adjust the last_update_clock time-stamp. */
  358. get_lowcore()->last_update_clock += delta;
  359. }
  360. /* Single threaded workqueue used for stp sync events */
  361. static struct workqueue_struct *time_sync_wq;
  362. static void __init time_init_wq(void)
  363. {
  364. if (time_sync_wq)
  365. return;
  366. time_sync_wq = create_singlethread_workqueue("timesync");
  367. }
  368. struct clock_sync_data {
  369. atomic_t cpus;
  370. int in_sync;
  371. long clock_delta;
  372. };
  373. /*
  374. * Server Time Protocol (STP) code.
  375. */
  376. static bool stp_online;
  377. static struct stp_sstpi stp_info;
  378. static void *stp_page;
  379. static void stp_work_fn(struct work_struct *work);
  380. static DECLARE_WORK(stp_work, stp_work_fn);
  381. static struct timer_list stp_timer;
  382. static int __init early_parse_stp(char *p)
  383. {
  384. return kstrtobool(p, &stp_online);
  385. }
  386. early_param("stp", early_parse_stp);
  387. /*
  388. * Reset STP attachment.
  389. */
  390. static void __init stp_reset(void)
  391. {
  392. int rc;
  393. stp_page = (void *) get_zeroed_page(GFP_ATOMIC);
  394. rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
  395. if (rc == 0)
  396. set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
  397. else if (stp_online) {
  398. pr_warn("The real or virtual hardware system does not provide an STP interface\n");
  399. free_page((unsigned long) stp_page);
  400. stp_page = NULL;
  401. stp_online = false;
  402. }
  403. }
  404. static void stp_timeout(struct timer_list *unused)
  405. {
  406. queue_work(time_sync_wq, &stp_work);
  407. }
  408. static int __init stp_init(void)
  409. {
  410. if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
  411. return 0;
  412. timer_setup(&stp_timer, stp_timeout, 0);
  413. time_init_wq();
  414. if (!stp_online)
  415. return 0;
  416. queue_work(time_sync_wq, &stp_work);
  417. return 0;
  418. }
  419. arch_initcall(stp_init);
  420. /*
  421. * STP timing alert. There are three causes:
  422. * 1) timing status change
  423. * 2) link availability change
  424. * 3) time control parameter change
  425. * In all three cases we are only interested in the clock source state.
  426. * If a STP clock source is now available use it.
  427. */
  428. static void stp_timing_alert(struct stp_irq_parm *intparm)
  429. {
  430. if (intparm->tsc || intparm->lac || intparm->tcpc)
  431. queue_work(time_sync_wq, &stp_work);
  432. }
  433. /*
  434. * STP sync check machine check. This is called when the timing state
  435. * changes from the synchronized state to the unsynchronized state.
  436. * After a STP sync check the clock is not in sync. The machine check
  437. * is broadcasted to all cpus at the same time.
  438. */
  439. int stp_sync_check(void)
  440. {
  441. disable_sync_clock(NULL);
  442. return 1;
  443. }
  444. /*
  445. * STP island condition machine check. This is called when an attached
  446. * server attempts to communicate over an STP link and the servers
  447. * have matching CTN ids and have a valid stratum-1 configuration
  448. * but the configurations do not match.
  449. */
  450. int stp_island_check(void)
  451. {
  452. disable_sync_clock(NULL);
  453. return 1;
  454. }
  455. void stp_queue_work(void)
  456. {
  457. queue_work(time_sync_wq, &stp_work);
  458. }
  459. static int __store_stpinfo(void)
  460. {
  461. int rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
  462. if (rc)
  463. clear_bit(CLOCK_SYNC_STPINFO_VALID, &clock_sync_flags);
  464. else
  465. set_bit(CLOCK_SYNC_STPINFO_VALID, &clock_sync_flags);
  466. return rc;
  467. }
  468. static int stpinfo_valid(void)
  469. {
  470. return stp_online && test_bit(CLOCK_SYNC_STPINFO_VALID, &clock_sync_flags);
  471. }
  472. static int stp_sync_clock(void *data)
  473. {
  474. struct clock_sync_data *sync = data;
  475. long clock_delta, flags;
  476. static int first;
  477. int rc;
  478. enable_sync_clock();
  479. if (xchg(&first, 1) == 0) {
  480. /* Wait until all other cpus entered the sync function. */
  481. while (atomic_read(&sync->cpus) != 0)
  482. cpu_relax();
  483. rc = 0;
  484. if (stp_info.todoff || stp_info.tmd != 2) {
  485. flags = vdso_update_begin();
  486. rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0,
  487. &clock_delta);
  488. if (rc == 0) {
  489. sync->clock_delta = clock_delta;
  490. clock_sync_global(clock_delta);
  491. rc = __store_stpinfo();
  492. if (rc == 0 && stp_info.tmd != 2)
  493. rc = -EAGAIN;
  494. }
  495. vdso_update_end(flags);
  496. }
  497. sync->in_sync = rc ? -EAGAIN : 1;
  498. xchg(&first, 0);
  499. } else {
  500. /* Slave */
  501. atomic_dec(&sync->cpus);
  502. /* Wait for in_sync to be set. */
  503. while (READ_ONCE(sync->in_sync) == 0)
  504. ;
  505. }
  506. if (sync->in_sync != 1)
  507. /* Didn't work. Clear per-cpu in sync bit again. */
  508. disable_sync_clock(NULL);
  509. /* Apply clock delta to per-CPU fields of this CPU. */
  510. clock_sync_local(sync->clock_delta);
  511. return 0;
  512. }
  513. static int stp_clear_leap(void)
  514. {
  515. struct __kernel_timex txc;
  516. int ret;
  517. memset(&txc, 0, sizeof(txc));
  518. ret = do_adjtimex(&txc);
  519. if (ret < 0)
  520. return ret;
  521. txc.modes = ADJ_STATUS;
  522. txc.status &= ~(STA_INS|STA_DEL);
  523. return do_adjtimex(&txc);
  524. }
  525. static void stp_check_leap(void)
  526. {
  527. struct stp_stzi stzi;
  528. struct stp_lsoib *lsoib = &stzi.lsoib;
  529. struct __kernel_timex txc;
  530. int64_t timediff;
  531. int leapdiff, ret;
  532. if (!stp_info.lu || !check_sync_clock()) {
  533. /*
  534. * Either a scheduled leap second was removed by the operator,
  535. * or STP is out of sync. In both cases, clear the leap second
  536. * kernel flags.
  537. */
  538. if (stp_clear_leap() < 0)
  539. pr_err("failed to clear leap second flags\n");
  540. return;
  541. }
  542. if (chsc_stzi(stp_page, &stzi, sizeof(stzi))) {
  543. pr_err("stzi failed\n");
  544. return;
  545. }
  546. timediff = tod_to_ns(lsoib->nlsout - get_tod_clock()) / NSEC_PER_SEC;
  547. leapdiff = lsoib->nlso - lsoib->also;
  548. if (leapdiff != 1 && leapdiff != -1) {
  549. pr_err("Cannot schedule %d leap seconds\n", leapdiff);
  550. return;
  551. }
  552. if (timediff < 0) {
  553. if (stp_clear_leap() < 0)
  554. pr_err("failed to clear leap second flags\n");
  555. } else if (timediff < 7200) {
  556. memset(&txc, 0, sizeof(txc));
  557. ret = do_adjtimex(&txc);
  558. if (ret < 0)
  559. return;
  560. txc.modes = ADJ_STATUS;
  561. if (leapdiff > 0)
  562. txc.status |= STA_INS;
  563. else
  564. txc.status |= STA_DEL;
  565. ret = do_adjtimex(&txc);
  566. if (ret < 0)
  567. pr_err("failed to set leap second flags\n");
  568. /* arm Timer to clear leap second flags */
  569. mod_timer(&stp_timer, jiffies + msecs_to_jiffies(14400 * MSEC_PER_SEC));
  570. } else {
  571. /* The day the leap second is scheduled for hasn't been reached. Retry
  572. * in one hour.
  573. */
  574. mod_timer(&stp_timer, jiffies + msecs_to_jiffies(3600 * MSEC_PER_SEC));
  575. }
  576. }
  577. /*
  578. * STP work. Check for the STP state and take over the clock
  579. * synchronization if the STP clock source is usable.
  580. */
  581. static void stp_work_fn(struct work_struct *work)
  582. {
  583. struct clock_sync_data stp_sync;
  584. int rc;
  585. /* prevent multiple execution. */
  586. mutex_lock(&stp_mutex);
  587. if (!stp_online) {
  588. chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
  589. del_timer_sync(&stp_timer);
  590. goto out_unlock;
  591. }
  592. rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xf0e0, NULL);
  593. if (rc)
  594. goto out_unlock;
  595. rc = __store_stpinfo();
  596. if (rc || stp_info.c == 0)
  597. goto out_unlock;
  598. /* Skip synchronization if the clock is already in sync. */
  599. if (!check_sync_clock()) {
  600. memset(&stp_sync, 0, sizeof(stp_sync));
  601. cpus_read_lock();
  602. atomic_set(&stp_sync.cpus, num_online_cpus() - 1);
  603. stop_machine_cpuslocked(stp_sync_clock, &stp_sync, cpu_online_mask);
  604. cpus_read_unlock();
  605. }
  606. if (!check_sync_clock())
  607. /*
  608. * There is a usable clock but the synchronization failed.
  609. * Retry after a second.
  610. */
  611. mod_timer(&stp_timer, jiffies + msecs_to_jiffies(MSEC_PER_SEC));
  612. else if (stp_info.lu)
  613. stp_check_leap();
  614. out_unlock:
  615. mutex_unlock(&stp_mutex);
  616. }
  617. /*
  618. * STP subsys sysfs interface functions
  619. */
  620. static const struct bus_type stp_subsys = {
  621. .name = "stp",
  622. .dev_name = "stp",
  623. };
  624. static ssize_t ctn_id_show(struct device *dev,
  625. struct device_attribute *attr,
  626. char *buf)
  627. {
  628. ssize_t ret = -ENODATA;
  629. mutex_lock(&stp_mutex);
  630. if (stpinfo_valid())
  631. ret = sprintf(buf, "%016lx\n",
  632. *(unsigned long *) stp_info.ctnid);
  633. mutex_unlock(&stp_mutex);
  634. return ret;
  635. }
  636. static DEVICE_ATTR_RO(ctn_id);
  637. static ssize_t ctn_type_show(struct device *dev,
  638. struct device_attribute *attr,
  639. char *buf)
  640. {
  641. ssize_t ret = -ENODATA;
  642. mutex_lock(&stp_mutex);
  643. if (stpinfo_valid())
  644. ret = sprintf(buf, "%i\n", stp_info.ctn);
  645. mutex_unlock(&stp_mutex);
  646. return ret;
  647. }
  648. static DEVICE_ATTR_RO(ctn_type);
  649. static ssize_t dst_offset_show(struct device *dev,
  650. struct device_attribute *attr,
  651. char *buf)
  652. {
  653. ssize_t ret = -ENODATA;
  654. mutex_lock(&stp_mutex);
  655. if (stpinfo_valid() && (stp_info.vbits & 0x2000))
  656. ret = sprintf(buf, "%i\n", (int)(s16) stp_info.dsto);
  657. mutex_unlock(&stp_mutex);
  658. return ret;
  659. }
  660. static DEVICE_ATTR_RO(dst_offset);
  661. static ssize_t leap_seconds_show(struct device *dev,
  662. struct device_attribute *attr,
  663. char *buf)
  664. {
  665. ssize_t ret = -ENODATA;
  666. mutex_lock(&stp_mutex);
  667. if (stpinfo_valid() && (stp_info.vbits & 0x8000))
  668. ret = sprintf(buf, "%i\n", (int)(s16) stp_info.leaps);
  669. mutex_unlock(&stp_mutex);
  670. return ret;
  671. }
  672. static DEVICE_ATTR_RO(leap_seconds);
  673. static ssize_t leap_seconds_scheduled_show(struct device *dev,
  674. struct device_attribute *attr,
  675. char *buf)
  676. {
  677. struct stp_stzi stzi;
  678. ssize_t ret;
  679. mutex_lock(&stp_mutex);
  680. if (!stpinfo_valid() || !(stp_info.vbits & 0x8000) || !stp_info.lu) {
  681. mutex_unlock(&stp_mutex);
  682. return -ENODATA;
  683. }
  684. ret = chsc_stzi(stp_page, &stzi, sizeof(stzi));
  685. mutex_unlock(&stp_mutex);
  686. if (ret < 0)
  687. return ret;
  688. if (!stzi.lsoib.p)
  689. return sprintf(buf, "0,0\n");
  690. return sprintf(buf, "%lu,%d\n",
  691. tod_to_ns(stzi.lsoib.nlsout - TOD_UNIX_EPOCH) / NSEC_PER_SEC,
  692. stzi.lsoib.nlso - stzi.lsoib.also);
  693. }
  694. static DEVICE_ATTR_RO(leap_seconds_scheduled);
  695. static ssize_t stratum_show(struct device *dev,
  696. struct device_attribute *attr,
  697. char *buf)
  698. {
  699. ssize_t ret = -ENODATA;
  700. mutex_lock(&stp_mutex);
  701. if (stpinfo_valid())
  702. ret = sprintf(buf, "%i\n", (int)(s16) stp_info.stratum);
  703. mutex_unlock(&stp_mutex);
  704. return ret;
  705. }
  706. static DEVICE_ATTR_RO(stratum);
  707. static ssize_t time_offset_show(struct device *dev,
  708. struct device_attribute *attr,
  709. char *buf)
  710. {
  711. ssize_t ret = -ENODATA;
  712. mutex_lock(&stp_mutex);
  713. if (stpinfo_valid() && (stp_info.vbits & 0x0800))
  714. ret = sprintf(buf, "%i\n", (int) stp_info.tto);
  715. mutex_unlock(&stp_mutex);
  716. return ret;
  717. }
  718. static DEVICE_ATTR_RO(time_offset);
  719. static ssize_t time_zone_offset_show(struct device *dev,
  720. struct device_attribute *attr,
  721. char *buf)
  722. {
  723. ssize_t ret = -ENODATA;
  724. mutex_lock(&stp_mutex);
  725. if (stpinfo_valid() && (stp_info.vbits & 0x4000))
  726. ret = sprintf(buf, "%i\n", (int)(s16) stp_info.tzo);
  727. mutex_unlock(&stp_mutex);
  728. return ret;
  729. }
  730. static DEVICE_ATTR_RO(time_zone_offset);
  731. static ssize_t timing_mode_show(struct device *dev,
  732. struct device_attribute *attr,
  733. char *buf)
  734. {
  735. ssize_t ret = -ENODATA;
  736. mutex_lock(&stp_mutex);
  737. if (stpinfo_valid())
  738. ret = sprintf(buf, "%i\n", stp_info.tmd);
  739. mutex_unlock(&stp_mutex);
  740. return ret;
  741. }
  742. static DEVICE_ATTR_RO(timing_mode);
  743. static ssize_t timing_state_show(struct device *dev,
  744. struct device_attribute *attr,
  745. char *buf)
  746. {
  747. ssize_t ret = -ENODATA;
  748. mutex_lock(&stp_mutex);
  749. if (stpinfo_valid())
  750. ret = sprintf(buf, "%i\n", stp_info.tst);
  751. mutex_unlock(&stp_mutex);
  752. return ret;
  753. }
  754. static DEVICE_ATTR_RO(timing_state);
  755. static ssize_t online_show(struct device *dev,
  756. struct device_attribute *attr,
  757. char *buf)
  758. {
  759. return sprintf(buf, "%i\n", stp_online);
  760. }
  761. static ssize_t online_store(struct device *dev,
  762. struct device_attribute *attr,
  763. const char *buf, size_t count)
  764. {
  765. unsigned int value;
  766. value = simple_strtoul(buf, NULL, 0);
  767. if (value != 0 && value != 1)
  768. return -EINVAL;
  769. if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
  770. return -EOPNOTSUPP;
  771. mutex_lock(&stp_mutex);
  772. stp_online = value;
  773. if (stp_online)
  774. set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
  775. else
  776. clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
  777. queue_work(time_sync_wq, &stp_work);
  778. mutex_unlock(&stp_mutex);
  779. return count;
  780. }
  781. /*
  782. * Can't use DEVICE_ATTR because the attribute should be named
  783. * stp/online but dev_attr_online already exists in this file ..
  784. */
  785. static DEVICE_ATTR_RW(online);
  786. static struct attribute *stp_dev_attrs[] = {
  787. &dev_attr_ctn_id.attr,
  788. &dev_attr_ctn_type.attr,
  789. &dev_attr_dst_offset.attr,
  790. &dev_attr_leap_seconds.attr,
  791. &dev_attr_online.attr,
  792. &dev_attr_leap_seconds_scheduled.attr,
  793. &dev_attr_stratum.attr,
  794. &dev_attr_time_offset.attr,
  795. &dev_attr_time_zone_offset.attr,
  796. &dev_attr_timing_mode.attr,
  797. &dev_attr_timing_state.attr,
  798. NULL
  799. };
  800. ATTRIBUTE_GROUPS(stp_dev);
  801. static int __init stp_init_sysfs(void)
  802. {
  803. return subsys_system_register(&stp_subsys, stp_dev_groups);
  804. }
  805. device_initcall(stp_init_sysfs);