time.c 21 KB

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