time.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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. vdso_data->hrtimer_res = hrtimer_resolution;
  268. smp_wmb();
  269. ++vdso_data->tb_update_count;
  270. }
  271. extern struct timezone sys_tz;
  272. void update_vsyscall_tz(void)
  273. {
  274. vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
  275. vdso_data->tz_dsttime = sys_tz.tz_dsttime;
  276. }
  277. /*
  278. * Initialize the TOD clock and the CPU timer of
  279. * the boot cpu.
  280. */
  281. void __init time_init(void)
  282. {
  283. /* Reset time synchronization interfaces. */
  284. stp_reset();
  285. /* request the clock comparator external interrupt */
  286. if (register_external_irq(EXT_IRQ_CLK_COMP, clock_comparator_interrupt))
  287. panic("Couldn't request external interrupt 0x1004");
  288. /* request the timing alert external interrupt */
  289. if (register_external_irq(EXT_IRQ_TIMING_ALERT, timing_alert_interrupt))
  290. panic("Couldn't request external interrupt 0x1406");
  291. if (__clocksource_register(&clocksource_tod) != 0)
  292. panic("Could not register TOD clock source");
  293. /* Enable TOD clock interrupts on the boot cpu. */
  294. init_cpu_timer();
  295. /* Enable cpu timer interrupts on the boot cpu. */
  296. vtime_init();
  297. }
  298. static DEFINE_PER_CPU(atomic_t, clock_sync_word);
  299. static DEFINE_MUTEX(clock_sync_mutex);
  300. static unsigned long clock_sync_flags;
  301. #define CLOCK_SYNC_HAS_STP 0
  302. #define CLOCK_SYNC_STP 1
  303. #define CLOCK_SYNC_STPINFO_VALID 2
  304. /*
  305. * The get_clock function for the physical clock. It will get the current
  306. * TOD clock, subtract the LPAR offset and write the result to *clock.
  307. * The function returns 0 if the clock is in sync with the external time
  308. * source. If the clock mode is local it will return -EOPNOTSUPP and
  309. * -EAGAIN if the clock is not in sync with the external reference.
  310. */
  311. int get_phys_clock(unsigned long *clock)
  312. {
  313. atomic_t *sw_ptr;
  314. unsigned int sw0, sw1;
  315. sw_ptr = &get_cpu_var(clock_sync_word);
  316. sw0 = atomic_read(sw_ptr);
  317. *clock = get_tod_clock() - lpar_offset;
  318. sw1 = atomic_read(sw_ptr);
  319. put_cpu_var(clock_sync_word);
  320. if (sw0 == sw1 && (sw0 & 0x80000000U))
  321. /* Success: time is in sync. */
  322. return 0;
  323. if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
  324. return -EOPNOTSUPP;
  325. if (!test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
  326. return -EACCES;
  327. return -EAGAIN;
  328. }
  329. EXPORT_SYMBOL(get_phys_clock);
  330. /*
  331. * Make get_phys_clock() return -EAGAIN.
  332. */
  333. static void disable_sync_clock(void *dummy)
  334. {
  335. atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
  336. /*
  337. * Clear the in-sync bit 2^31. All get_phys_clock calls will
  338. * fail until the sync bit is turned back on. In addition
  339. * increase the "sequence" counter to avoid the race of an
  340. * stp event and the complete recovery against get_phys_clock.
  341. */
  342. atomic_andnot(0x80000000, sw_ptr);
  343. atomic_inc(sw_ptr);
  344. }
  345. /*
  346. * Make get_phys_clock() return 0 again.
  347. * Needs to be called from a context disabled for preemption.
  348. */
  349. static void enable_sync_clock(void)
  350. {
  351. atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
  352. atomic_or(0x80000000, sw_ptr);
  353. }
  354. /*
  355. * Function to check if the clock is in sync.
  356. */
  357. static inline int check_sync_clock(void)
  358. {
  359. atomic_t *sw_ptr;
  360. int rc;
  361. sw_ptr = &get_cpu_var(clock_sync_word);
  362. rc = (atomic_read(sw_ptr) & 0x80000000U) != 0;
  363. put_cpu_var(clock_sync_word);
  364. return rc;
  365. }
  366. /*
  367. * Apply clock delta to the global data structures.
  368. * This is called once on the CPU that performed the clock sync.
  369. */
  370. static void clock_sync_global(unsigned long long delta)
  371. {
  372. unsigned long now, adj;
  373. struct ptff_qto qto;
  374. /* Fixup the monotonic sched clock. */
  375. *(unsigned long long *) &tod_clock_base[1] += delta;
  376. if (*(unsigned long long *) &tod_clock_base[1] < delta)
  377. /* Epoch overflow */
  378. tod_clock_base[0]++;
  379. /* Adjust TOD steering parameters. */
  380. vdso_data->tb_update_count++;
  381. now = get_tod_clock();
  382. adj = tod_steering_end - now;
  383. if (unlikely((s64) adj >= 0))
  384. /* Calculate how much of the old adjustment is left. */
  385. tod_steering_delta = (tod_steering_delta < 0) ?
  386. -(adj >> 15) : (adj >> 15);
  387. tod_steering_delta += delta;
  388. if ((abs(tod_steering_delta) >> 48) != 0)
  389. panic("TOD clock sync offset %lli is too large to drift\n",
  390. tod_steering_delta);
  391. tod_steering_end = now + (abs(tod_steering_delta) << 15);
  392. vdso_data->ts_dir = (tod_steering_delta < 0) ? 0 : 1;
  393. vdso_data->ts_end = tod_steering_end;
  394. vdso_data->tb_update_count++;
  395. /* Update LPAR offset. */
  396. if (ptff_query(PTFF_QTO) && ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
  397. lpar_offset = qto.tod_epoch_difference;
  398. /* Call the TOD clock change notifier. */
  399. atomic_notifier_call_chain(&s390_epoch_delta_notifier, 0, &delta);
  400. }
  401. /*
  402. * Apply clock delta to the per-CPU data structures of this CPU.
  403. * This is called for each online CPU after the call to clock_sync_global.
  404. */
  405. static void clock_sync_local(unsigned long long delta)
  406. {
  407. /* Add the delta to the clock comparator. */
  408. if (S390_lowcore.clock_comparator != clock_comparator_max) {
  409. S390_lowcore.clock_comparator += delta;
  410. set_clock_comparator(S390_lowcore.clock_comparator);
  411. }
  412. /* Adjust the last_update_clock time-stamp. */
  413. S390_lowcore.last_update_clock += delta;
  414. }
  415. /* Single threaded workqueue used for stp sync events */
  416. static struct workqueue_struct *time_sync_wq;
  417. static void __init time_init_wq(void)
  418. {
  419. if (time_sync_wq)
  420. return;
  421. time_sync_wq = create_singlethread_workqueue("timesync");
  422. }
  423. struct clock_sync_data {
  424. atomic_t cpus;
  425. int in_sync;
  426. unsigned long long clock_delta;
  427. };
  428. /*
  429. * Server Time Protocol (STP) code.
  430. */
  431. static bool stp_online;
  432. static struct stp_sstpi stp_info;
  433. static void *stp_page;
  434. static void stp_work_fn(struct work_struct *work);
  435. static DEFINE_MUTEX(stp_work_mutex);
  436. static DECLARE_WORK(stp_work, stp_work_fn);
  437. static struct timer_list stp_timer;
  438. static int __init early_parse_stp(char *p)
  439. {
  440. return kstrtobool(p, &stp_online);
  441. }
  442. early_param("stp", early_parse_stp);
  443. /*
  444. * Reset STP attachment.
  445. */
  446. static void __init stp_reset(void)
  447. {
  448. int rc;
  449. stp_page = (void *) get_zeroed_page(GFP_ATOMIC);
  450. rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
  451. if (rc == 0)
  452. set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
  453. else if (stp_online) {
  454. pr_warn("The real or virtual hardware system does not provide an STP interface\n");
  455. free_page((unsigned long) stp_page);
  456. stp_page = NULL;
  457. stp_online = false;
  458. }
  459. }
  460. static void stp_timeout(struct timer_list *unused)
  461. {
  462. queue_work(time_sync_wq, &stp_work);
  463. }
  464. static int __init stp_init(void)
  465. {
  466. if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
  467. return 0;
  468. timer_setup(&stp_timer, stp_timeout, 0);
  469. time_init_wq();
  470. if (!stp_online)
  471. return 0;
  472. queue_work(time_sync_wq, &stp_work);
  473. return 0;
  474. }
  475. arch_initcall(stp_init);
  476. /*
  477. * STP timing alert. There are three causes:
  478. * 1) timing status change
  479. * 2) link availability change
  480. * 3) time control parameter change
  481. * In all three cases we are only interested in the clock source state.
  482. * If a STP clock source is now available use it.
  483. */
  484. static void stp_timing_alert(struct stp_irq_parm *intparm)
  485. {
  486. if (intparm->tsc || intparm->lac || intparm->tcpc)
  487. queue_work(time_sync_wq, &stp_work);
  488. }
  489. /*
  490. * STP sync check machine check. This is called when the timing state
  491. * changes from the synchronized state to the unsynchronized state.
  492. * After a STP sync check the clock is not in sync. The machine check
  493. * is broadcasted to all cpus at the same time.
  494. */
  495. int stp_sync_check(void)
  496. {
  497. disable_sync_clock(NULL);
  498. return 1;
  499. }
  500. /*
  501. * STP island condition machine check. This is called when an attached
  502. * server attempts to communicate over an STP link and the servers
  503. * have matching CTN ids and have a valid stratum-1 configuration
  504. * but the configurations do not match.
  505. */
  506. int stp_island_check(void)
  507. {
  508. disable_sync_clock(NULL);
  509. return 1;
  510. }
  511. void stp_queue_work(void)
  512. {
  513. queue_work(time_sync_wq, &stp_work);
  514. }
  515. static int __store_stpinfo(void)
  516. {
  517. int rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
  518. if (rc)
  519. clear_bit(CLOCK_SYNC_STPINFO_VALID, &clock_sync_flags);
  520. else
  521. set_bit(CLOCK_SYNC_STPINFO_VALID, &clock_sync_flags);
  522. return rc;
  523. }
  524. static int stpinfo_valid(void)
  525. {
  526. return stp_online && test_bit(CLOCK_SYNC_STPINFO_VALID, &clock_sync_flags);
  527. }
  528. static int stp_sync_clock(void *data)
  529. {
  530. struct clock_sync_data *sync = data;
  531. unsigned long long clock_delta;
  532. static int first;
  533. int rc;
  534. enable_sync_clock();
  535. if (xchg(&first, 1) == 0) {
  536. /* Wait until all other cpus entered the sync function. */
  537. while (atomic_read(&sync->cpus) != 0)
  538. cpu_relax();
  539. rc = 0;
  540. if (stp_info.todoff[0] || stp_info.todoff[1] ||
  541. stp_info.todoff[2] || stp_info.todoff[3] ||
  542. stp_info.tmd != 2) {
  543. rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0,
  544. &clock_delta);
  545. if (rc == 0) {
  546. sync->clock_delta = clock_delta;
  547. clock_sync_global(clock_delta);
  548. rc = __store_stpinfo();
  549. if (rc == 0 && stp_info.tmd != 2)
  550. rc = -EAGAIN;
  551. }
  552. }
  553. sync->in_sync = rc ? -EAGAIN : 1;
  554. xchg(&first, 0);
  555. } else {
  556. /* Slave */
  557. atomic_dec(&sync->cpus);
  558. /* Wait for in_sync to be set. */
  559. while (READ_ONCE(sync->in_sync) == 0)
  560. __udelay(1);
  561. }
  562. if (sync->in_sync != 1)
  563. /* Didn't work. Clear per-cpu in sync bit again. */
  564. disable_sync_clock(NULL);
  565. /* Apply clock delta to per-CPU fields of this CPU. */
  566. clock_sync_local(sync->clock_delta);
  567. return 0;
  568. }
  569. /*
  570. * STP work. Check for the STP state and take over the clock
  571. * synchronization if the STP clock source is usable.
  572. */
  573. static void stp_work_fn(struct work_struct *work)
  574. {
  575. struct clock_sync_data stp_sync;
  576. int rc;
  577. /* prevent multiple execution. */
  578. mutex_lock(&stp_work_mutex);
  579. if (!stp_online) {
  580. chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
  581. del_timer_sync(&stp_timer);
  582. goto out_unlock;
  583. }
  584. rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xb0e0, NULL);
  585. if (rc)
  586. goto out_unlock;
  587. rc = __store_stpinfo();
  588. if (rc || stp_info.c == 0)
  589. goto out_unlock;
  590. /* Skip synchronization if the clock is already in sync. */
  591. if (check_sync_clock())
  592. goto out_unlock;
  593. memset(&stp_sync, 0, sizeof(stp_sync));
  594. cpus_read_lock();
  595. atomic_set(&stp_sync.cpus, num_online_cpus() - 1);
  596. stop_machine_cpuslocked(stp_sync_clock, &stp_sync, cpu_online_mask);
  597. cpus_read_unlock();
  598. if (!check_sync_clock())
  599. /*
  600. * There is a usable clock but the synchonization failed.
  601. * Retry after a second.
  602. */
  603. mod_timer(&stp_timer, jiffies + HZ);
  604. out_unlock:
  605. mutex_unlock(&stp_work_mutex);
  606. }
  607. /*
  608. * STP subsys sysfs interface functions
  609. */
  610. static struct bus_type stp_subsys = {
  611. .name = "stp",
  612. .dev_name = "stp",
  613. };
  614. static ssize_t stp_ctn_id_show(struct device *dev,
  615. struct device_attribute *attr,
  616. char *buf)
  617. {
  618. ssize_t ret = -ENODATA;
  619. mutex_lock(&stp_work_mutex);
  620. if (stpinfo_valid())
  621. ret = sprintf(buf, "%016llx\n",
  622. *(unsigned long long *) stp_info.ctnid);
  623. mutex_unlock(&stp_work_mutex);
  624. return ret;
  625. }
  626. static DEVICE_ATTR(ctn_id, 0400, stp_ctn_id_show, NULL);
  627. static ssize_t stp_ctn_type_show(struct device *dev,
  628. struct device_attribute *attr,
  629. char *buf)
  630. {
  631. ssize_t ret = -ENODATA;
  632. mutex_lock(&stp_work_mutex);
  633. if (stpinfo_valid())
  634. ret = sprintf(buf, "%i\n", stp_info.ctn);
  635. mutex_unlock(&stp_work_mutex);
  636. return ret;
  637. }
  638. static DEVICE_ATTR(ctn_type, 0400, stp_ctn_type_show, NULL);
  639. static ssize_t stp_dst_offset_show(struct device *dev,
  640. struct device_attribute *attr,
  641. char *buf)
  642. {
  643. ssize_t ret = -ENODATA;
  644. mutex_lock(&stp_work_mutex);
  645. if (stpinfo_valid() && (stp_info.vbits & 0x2000))
  646. ret = sprintf(buf, "%i\n", (int)(s16) stp_info.dsto);
  647. mutex_unlock(&stp_work_mutex);
  648. return ret;
  649. }
  650. static DEVICE_ATTR(dst_offset, 0400, stp_dst_offset_show, NULL);
  651. static ssize_t stp_leap_seconds_show(struct device *dev,
  652. struct device_attribute *attr,
  653. char *buf)
  654. {
  655. ssize_t ret = -ENODATA;
  656. mutex_lock(&stp_work_mutex);
  657. if (stpinfo_valid() && (stp_info.vbits & 0x8000))
  658. ret = sprintf(buf, "%i\n", (int)(s16) stp_info.leaps);
  659. mutex_unlock(&stp_work_mutex);
  660. return ret;
  661. }
  662. static DEVICE_ATTR(leap_seconds, 0400, stp_leap_seconds_show, NULL);
  663. static ssize_t stp_stratum_show(struct device *dev,
  664. struct device_attribute *attr,
  665. char *buf)
  666. {
  667. ssize_t ret = -ENODATA;
  668. mutex_lock(&stp_work_mutex);
  669. if (stpinfo_valid())
  670. ret = sprintf(buf, "%i\n", (int)(s16) stp_info.stratum);
  671. mutex_unlock(&stp_work_mutex);
  672. return ret;
  673. }
  674. static DEVICE_ATTR(stratum, 0400, stp_stratum_show, NULL);
  675. static ssize_t stp_time_offset_show(struct device *dev,
  676. struct device_attribute *attr,
  677. char *buf)
  678. {
  679. ssize_t ret = -ENODATA;
  680. mutex_lock(&stp_work_mutex);
  681. if (stpinfo_valid() && (stp_info.vbits & 0x0800))
  682. ret = sprintf(buf, "%i\n", (int) stp_info.tto);
  683. mutex_unlock(&stp_work_mutex);
  684. return ret;
  685. }
  686. static DEVICE_ATTR(time_offset, 0400, stp_time_offset_show, NULL);
  687. static ssize_t stp_time_zone_offset_show(struct device *dev,
  688. struct device_attribute *attr,
  689. char *buf)
  690. {
  691. ssize_t ret = -ENODATA;
  692. mutex_lock(&stp_work_mutex);
  693. if (stpinfo_valid() && (stp_info.vbits & 0x4000))
  694. ret = sprintf(buf, "%i\n", (int)(s16) stp_info.tzo);
  695. mutex_unlock(&stp_work_mutex);
  696. return ret;
  697. }
  698. static DEVICE_ATTR(time_zone_offset, 0400,
  699. stp_time_zone_offset_show, NULL);
  700. static ssize_t stp_timing_mode_show(struct device *dev,
  701. struct device_attribute *attr,
  702. char *buf)
  703. {
  704. ssize_t ret = -ENODATA;
  705. mutex_lock(&stp_work_mutex);
  706. if (stpinfo_valid())
  707. ret = sprintf(buf, "%i\n", stp_info.tmd);
  708. mutex_unlock(&stp_work_mutex);
  709. return ret;
  710. }
  711. static DEVICE_ATTR(timing_mode, 0400, stp_timing_mode_show, NULL);
  712. static ssize_t stp_timing_state_show(struct device *dev,
  713. struct device_attribute *attr,
  714. char *buf)
  715. {
  716. ssize_t ret = -ENODATA;
  717. mutex_lock(&stp_work_mutex);
  718. if (stpinfo_valid())
  719. ret = sprintf(buf, "%i\n", stp_info.tst);
  720. mutex_unlock(&stp_work_mutex);
  721. return ret;
  722. }
  723. static DEVICE_ATTR(timing_state, 0400, stp_timing_state_show, NULL);
  724. static ssize_t stp_online_show(struct device *dev,
  725. struct device_attribute *attr,
  726. char *buf)
  727. {
  728. return sprintf(buf, "%i\n", stp_online);
  729. }
  730. static ssize_t stp_online_store(struct device *dev,
  731. struct device_attribute *attr,
  732. const char *buf, size_t count)
  733. {
  734. unsigned int value;
  735. value = simple_strtoul(buf, NULL, 0);
  736. if (value != 0 && value != 1)
  737. return -EINVAL;
  738. if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
  739. return -EOPNOTSUPP;
  740. mutex_lock(&clock_sync_mutex);
  741. stp_online = value;
  742. if (stp_online)
  743. set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
  744. else
  745. clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
  746. queue_work(time_sync_wq, &stp_work);
  747. mutex_unlock(&clock_sync_mutex);
  748. return count;
  749. }
  750. /*
  751. * Can't use DEVICE_ATTR because the attribute should be named
  752. * stp/online but dev_attr_online already exists in this file ..
  753. */
  754. static struct device_attribute dev_attr_stp_online = {
  755. .attr = { .name = "online", .mode = 0600 },
  756. .show = stp_online_show,
  757. .store = stp_online_store,
  758. };
  759. static struct device_attribute *stp_attributes[] = {
  760. &dev_attr_ctn_id,
  761. &dev_attr_ctn_type,
  762. &dev_attr_dst_offset,
  763. &dev_attr_leap_seconds,
  764. &dev_attr_stp_online,
  765. &dev_attr_stratum,
  766. &dev_attr_time_offset,
  767. &dev_attr_time_zone_offset,
  768. &dev_attr_timing_mode,
  769. &dev_attr_timing_state,
  770. NULL
  771. };
  772. static int __init stp_init_sysfs(void)
  773. {
  774. struct device_attribute **attr;
  775. int rc;
  776. rc = subsys_system_register(&stp_subsys, NULL);
  777. if (rc)
  778. goto out;
  779. for (attr = stp_attributes; *attr; attr++) {
  780. rc = device_create_file(stp_subsys.dev_root, *attr);
  781. if (rc)
  782. goto out_unreg;
  783. }
  784. return 0;
  785. out_unreg:
  786. for (; attr >= stp_attributes; attr--)
  787. device_remove_file(stp_subsys.dev_root, *attr);
  788. bus_unregister(&stp_subsys);
  789. out:
  790. return rc;
  791. }
  792. device_initcall(stp_init_sysfs);