time.c 23 KB

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