time.c 22 KB

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