time.c 22 KB

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