time.c 22 KB

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