blob: 4e9949800562d66e6a0a41a861db8e778cc01018 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Time of day based timer functions.
3 *
4 * S390 version
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02005 * Copyright IBM Corp. 1999, 2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * 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
Martin Schwidefskyfeab6502008-12-25 13:39:38 +010014#define KMSG_COMPONENT "time"
15#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
16
Heiko Carstens052ff462011-01-05 12:47:28 +010017#include <linux/kernel_stat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/errno.h>
19#include <linux/module.h>
20#include <linux/sched.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>
Heiko Carstens750887d2008-12-25 13:38:37 +010026#include <linux/cpu.h>
27#include <linux/stop_machine.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/time.h>
Kay Sievers3fbacff2011-12-21 15:09:50 -080029#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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>
John Stultz189374a2012-09-04 15:27:48 -040037#include <linux/timekeeper_internal.h>
Heiko Carstens5a62b192008-04-17 07:46:25 +020038#include <linux/clockchips.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/gfp.h>
Martin Schwidefsky860dba42011-01-05 12:47:25 +010040#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/uaccess.h>
Martin Schwidefsky40277892016-05-31 10:16:24 +020042#include <asm/facility.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/div64.h>
Martin Schwidefskyb0206322008-12-25 13:38:36 +010045#include <asm/vdso.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/irq.h>
Heiko Carstens5a489b92006-10-06 16:38:35 +020047#include <asm/irq_regs.h>
Martin Schwidefsky27f6b412012-07-20 11:15:08 +020048#include <asm/vtimer.h>
Martin Schwidefskyfd5ada02016-05-31 15:06:51 +020049#include <asm/stp.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020050#include <asm/cio.h>
Martin Schwidefsky638ad342011-10-30 15:17:13 +010051#include "entry.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/* change this if you have some constant time drift */
54#define USECS_PER_JIFFY ((unsigned long) 1000000/HZ)
55#define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)
56
Martin Schwidefskyb6112cc2009-04-14 15:36:28 +020057u64 sched_clock_base_cc = -1; /* Force to data section. */
Heiko Carstens05e7ff72009-09-11 10:28:31 +020058EXPORT_SYMBOL_GPL(sched_clock_base_cc);
Martin Schwidefskyb6112cc2009-04-14 15:36:28 +020059
Heiko Carstens5a62b192008-04-17 07:46:25 +020060static DEFINE_PER_CPU(struct clock_event_device, comparators);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Fan Zhangfdf03652015-05-13 10:58:41 +020062ATOMIC_NOTIFIER_HEAD(s390_epoch_delta_notifier);
63EXPORT_SYMBOL(s390_epoch_delta_notifier);
64
Martin Schwidefsky40277892016-05-31 10:16:24 +020065unsigned char ptff_function_mask[16];
66unsigned long lpar_offset;
Martin Schwidefsky936cc852016-05-31 12:47:03 +020067unsigned long initial_leap_seconds;
Martin Schwidefsky40277892016-05-31 10:16:24 +020068
69/*
70 * Get time offsets with PTFF
71 */
72void __init ptff_init(void)
73{
74 struct ptff_qto qto;
Martin Schwidefsky936cc852016-05-31 12:47:03 +020075 struct ptff_qui qui;
Martin Schwidefsky40277892016-05-31 10:16:24 +020076
77 if (!test_facility(28))
78 return;
79 ptff(&ptff_function_mask, sizeof(ptff_function_mask), PTFF_QAF);
80
81 /* get LPAR offset */
82 if (ptff_query(PTFF_QTO) && ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
83 lpar_offset = qto.tod_epoch_difference;
Martin Schwidefsky936cc852016-05-31 12:47:03 +020084
85 /* get initial leap seconds */
86 if (ptff_query(PTFF_QUI) && ptff(&qui, sizeof(qui), PTFF_QUI) == 0)
87 initial_leap_seconds = (unsigned long)
88 ((long) qui.old_leap * 4096000000L);
Martin Schwidefsky40277892016-05-31 10:16:24 +020089}
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091/*
92 * Scheduler clock - returns current time in nanosec units.
93 */
Heiko Carstens7a5388d2014-10-22 12:42:38 +020094unsigned long long notrace sched_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
Heiko Carstens1aae0562013-01-30 09:49:40 +010096 return tod_to_ns(get_tod_clock_monotonic());
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
Heiko Carstens7a5388d2014-10-22 12:42:38 +020098NOKPROBE_SYMBOL(sched_clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Jan Glauber32f65f22006-02-01 03:06:33 -0800100/*
101 * Monotonic_clock - returns # of nanoseconds passed since time_init()
102 */
103unsigned long long monotonic_clock(void)
104{
105 return sched_clock();
106}
107EXPORT_SYMBOL(monotonic_clock);
108
Xunlei Pang689911c2015-04-09 09:04:41 +0800109void tod_to_timeval(__u64 todval, struct timespec64 *xt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 unsigned long long sec;
112
113 sec = todval >> 12;
114 do_div(sec, 1000000);
John Stultzb1e2ba82010-03-08 12:25:19 +0100115 xt->tv_sec = sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 todval -= (sec * 1000000) << 12;
John Stultzb1e2ba82010-03-08 12:25:19 +0100117 xt->tv_nsec = ((todval * 1000) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
Christof Schmittb592e892009-08-18 15:43:31 +0200119EXPORT_SYMBOL(tod_to_timeval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Heiko Carstens5a62b192008-04-17 07:46:25 +0200121void clock_comparator_work(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Heiko Carstens5a62b192008-04-17 07:46:25 +0200123 struct clock_event_device *cd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Heiko Carstens5a62b192008-04-17 07:46:25 +0200125 S390_lowcore.clock_comparator = -1ULL;
Christoph Lametereb7e7d72014-08-17 12:30:45 -0500126 cd = this_cpu_ptr(&comparators);
Heiko Carstens5a62b192008-04-17 07:46:25 +0200127 cd->event_handler(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
Heiko Carstens5a62b192008-04-17 07:46:25 +0200131 * Fixup the clock comparator.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 */
Heiko Carstens5a62b192008-04-17 07:46:25 +0200133static void fixup_clock_comparator(unsigned long long delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Heiko Carstens5a62b192008-04-17 07:46:25 +0200135 /* If nobody is waiting there's nothing to fix. */
136 if (S390_lowcore.clock_comparator == -1ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200138 S390_lowcore.clock_comparator += delta;
139 set_clock_comparator(S390_lowcore.clock_comparator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Martin Schwidefsky8adbf782013-11-22 12:04:18 +0100142static int s390_next_event(unsigned long delta,
Heiko Carstens5a62b192008-04-17 07:46:25 +0200143 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Martin Schwidefsky8adbf782013-11-22 12:04:18 +0100145 S390_lowcore.clock_comparator = get_tod_clock() + delta;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200146 set_clock_comparator(S390_lowcore.clock_comparator);
147 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100150/*
151 * Set up lowcore and control register of the current cpu to
152 * enable TOD clock and clock comparator interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 */
154void init_cpu_timer(void)
155{
Heiko Carstens5a62b192008-04-17 07:46:25 +0200156 struct clock_event_device *cd;
157 int cpu;
158
159 S390_lowcore.clock_comparator = -1ULL;
160 set_clock_comparator(S390_lowcore.clock_comparator);
161
162 cpu = smp_processor_id();
163 cd = &per_cpu(comparators, cpu);
164 cd->name = "comparator";
Martin Schwidefsky8adbf782013-11-22 12:04:18 +0100165 cd->features = CLOCK_EVT_FEAT_ONESHOT;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200166 cd->mult = 16777;
167 cd->shift = 12;
168 cd->min_delta_ns = 1;
169 cd->max_delta_ns = LONG_MAX;
170 cd->rating = 400;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030171 cd->cpumask = cpumask_of(cpu);
Martin Schwidefsky8adbf782013-11-22 12:04:18 +0100172 cd->set_next_event = s390_next_event;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200173
174 clockevents_register_device(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100176 /* Enable clock comparator timer interrupt. */
177 __ctl_set_bit(0,11);
178
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200179 /* Always allow the timing alert external interrupt. */
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100180 __ctl_set_bit(0, 4);
181}
182
Heiko Carstensfde15c32012-03-11 11:59:31 -0400183static void clock_comparator_interrupt(struct ext_code ext_code,
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200184 unsigned int param32,
185 unsigned long param64)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100186{
Heiko Carstens420f42e2013-01-02 15:18:18 +0100187 inc_irq_stat(IRQEXT_CLK);
Heiko Carstensd3d238c2008-10-03 21:54:59 +0200188 if (S390_lowcore.clock_comparator == -1ULL)
189 set_clock_comparator(S390_lowcore.clock_comparator);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100190}
191
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200192static void stp_timing_alert(struct stp_irq_parm *);
193
Heiko Carstensfde15c32012-03-11 11:59:31 -0400194static void timing_alert_interrupt(struct ext_code ext_code,
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200195 unsigned int param32, unsigned long param64)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200196{
Heiko Carstens420f42e2013-01-02 15:18:18 +0100197 inc_irq_stat(IRQEXT_TLA);
Martin Schwidefskyf6649a72010-10-25 16:10:38 +0200198 if (param32 & 0x00038000)
199 stp_timing_alert((struct stp_irq_parm *) &param32);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200200}
201
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200202static void stp_reset(void);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100203
Xunlei Pang689911c2015-04-09 09:04:41 +0800204void read_persistent_clock64(struct timespec64 *ts)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100205{
Martin Schwidefsky936cc852016-05-31 12:47:03 +0200206 __u64 clock;
207
208 clock = get_tod_clock() - initial_leap_seconds;
209 tod_to_timeval(clock - TOD_UNIX_EPOCH, ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Xunlei Pang689911c2015-04-09 09:04:41 +0800212void read_boot_clock64(struct timespec64 *ts)
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200213{
Martin Schwidefsky936cc852016-05-31 12:47:03 +0200214 __u64 clock;
215
216 clock = sched_clock_base_cc - initial_leap_seconds;
217 tod_to_timeval(clock - TOD_UNIX_EPOCH, ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Magnus Damm8e196082009-04-21 12:24:00 -0700220static cycle_t read_tod_clock(struct clocksource *cs)
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200221{
Heiko Carstens1aae0562013-01-30 09:49:40 +0100222 return get_tod_clock();
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200223}
224
225static struct clocksource clocksource_tod = {
226 .name = "tod",
Christian Borntraegerd2cb0e62007-11-05 11:10:14 +0100227 .rating = 400,
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200228 .read = read_tod_clock,
229 .mask = -1ULL,
230 .mult = 1000,
231 .shift = 12,
Thomas Gleixnercc02d802007-02-16 01:27:39 -0800232 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200233};
234
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200235struct clocksource * __init clocksource_default_clock(void)
236{
237 return &clocksource_tod;
238}
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200239
Martin Schwidefsky79c74ec2013-11-22 10:04:53 +0100240void update_vsyscall(struct timekeeper *tk)
Martin Schwidefskyb0206322008-12-25 13:38:36 +0100241{
Martin Schwidefsky79c74ec2013-11-22 10:04:53 +0100242 u64 nsecps;
243
Peter Zijlstra876e7882015-03-19 10:09:06 +0100244 if (tk->tkr_mono.clock != &clocksource_tod)
Martin Schwidefskyb0206322008-12-25 13:38:36 +0100245 return;
246
247 /* Make userspace gettimeofday spin until we're done. */
248 ++vdso_data->tb_update_count;
249 smp_wmb();
Peter Zijlstra876e7882015-03-19 10:09:06 +0100250 vdso_data->xtime_tod_stamp = tk->tkr_mono.cycle_last;
Martin Schwidefsky79c74ec2013-11-22 10:04:53 +0100251 vdso_data->xtime_clock_sec = tk->xtime_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100252 vdso_data->xtime_clock_nsec = tk->tkr_mono.xtime_nsec;
Martin Schwidefsky79c74ec2013-11-22 10:04:53 +0100253 vdso_data->wtom_clock_sec =
254 tk->xtime_sec + tk->wall_to_monotonic.tv_sec;
Peter Zijlstra876e7882015-03-19 10:09:06 +0100255 vdso_data->wtom_clock_nsec = tk->tkr_mono.xtime_nsec +
256 + ((u64) tk->wall_to_monotonic.tv_nsec << tk->tkr_mono.shift);
257 nsecps = (u64) NSEC_PER_SEC << tk->tkr_mono.shift;
Martin Schwidefsky79c74ec2013-11-22 10:04:53 +0100258 while (vdso_data->wtom_clock_nsec >= nsecps) {
259 vdso_data->wtom_clock_nsec -= nsecps;
260 vdso_data->wtom_clock_sec++;
261 }
Martin Schwidefskyb7eacb52014-08-29 12:31:45 +0200262
263 vdso_data->xtime_coarse_sec = tk->xtime_sec;
264 vdso_data->xtime_coarse_nsec =
Peter Zijlstra876e7882015-03-19 10:09:06 +0100265 (long)(tk->tkr_mono.xtime_nsec >> tk->tkr_mono.shift);
Martin Schwidefskyb7eacb52014-08-29 12:31:45 +0200266 vdso_data->wtom_coarse_sec =
267 vdso_data->xtime_coarse_sec + tk->wall_to_monotonic.tv_sec;
268 vdso_data->wtom_coarse_nsec =
269 vdso_data->xtime_coarse_nsec + tk->wall_to_monotonic.tv_nsec;
270 while (vdso_data->wtom_coarse_nsec >= NSEC_PER_SEC) {
271 vdso_data->wtom_coarse_nsec -= NSEC_PER_SEC;
272 vdso_data->wtom_coarse_sec++;
273 }
274
Peter Zijlstra876e7882015-03-19 10:09:06 +0100275 vdso_data->tk_mult = tk->tkr_mono.mult;
276 vdso_data->tk_shift = tk->tkr_mono.shift;
Martin Schwidefskyb0206322008-12-25 13:38:36 +0100277 smp_wmb();
278 ++vdso_data->tb_update_count;
279}
280
281extern struct timezone sys_tz;
282
283void update_vsyscall_tz(void)
284{
285 /* Make userspace gettimeofday spin until we're done. */
286 ++vdso_data->tb_update_count;
287 smp_wmb();
288 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
289 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
290 smp_wmb();
291 ++vdso_data->tb_update_count;
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/*
295 * Initialize the TOD clock and the CPU timer of
296 * the boot cpu.
297 */
298void __init time_init(void)
299{
Martin Schwidefskyb6112cc2009-04-14 15:36:28 +0200300 /* Reset time synchronization interfaces. */
Martin Schwidefskyb6112cc2009-04-14 15:36:28 +0200301 stp_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 /* request the clock comparator external interrupt */
Thomas Huth1dad0932014-03-31 15:24:08 +0200304 if (register_external_irq(EXT_IRQ_CLK_COMP, clock_comparator_interrupt))
305 panic("Couldn't request external interrupt 0x1004");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200307 /* request the timing alert external interrupt */
Thomas Huth1dad0932014-03-31 15:24:08 +0200308 if (register_external_irq(EXT_IRQ_TIMING_ALERT, timing_alert_interrupt))
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100309 panic("Couldn't request external interrupt 0x1406");
310
John Stultzf8935982015-03-11 21:16:37 -0700311 if (__clocksource_register(&clocksource_tod) != 0)
Martin Schwidefskyab96e792009-04-14 15:36:29 +0200312 panic("Could not register TOD clock source");
313
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100314 /* Enable TOD clock interrupts on the boot cpu. */
315 init_cpu_timer();
Martin Schwidefskyab96e792009-04-14 15:36:29 +0200316
Martin Schwidefskyc185b782008-12-25 13:39:25 +0100317 /* Enable cpu timer interrupts on the boot cpu. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 vtime_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100321/*
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200322 * The time is "clock". old is what we think the time is.
323 * Adjust the value by a multiple of jiffies and add the delta to ntp.
324 * "delay" is an approximation how long the synchronization took. If
325 * the time correction is positive, then "delay" is subtracted from
326 * the time difference and only the remaining part is passed to ntp.
327 */
328static unsigned long long adjust_time(unsigned long long old,
329 unsigned long long clock,
330 unsigned long long delay)
331{
332 unsigned long long delta, ticks;
333 struct timex adjust;
334
335 if (clock > old) {
336 /* It is later than we thought. */
337 delta = ticks = clock - old;
338 delta = ticks = (delta < delay) ? 0 : delta - delay;
339 delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
340 adjust.offset = ticks * (1000000 / HZ);
341 } else {
342 /* It is earlier than we thought. */
343 delta = ticks = old - clock;
344 delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
345 delta = -delta;
346 adjust.offset = -ticks * (1000000 / HZ);
347 }
Christian Borntraeger8107d822008-11-27 11:05:56 +0100348 sched_clock_base_cc += delta;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200349 if (adjust.offset != 0) {
Martin Schwidefskyfeab6502008-12-25 13:39:38 +0100350 pr_notice("The ETR interface has adjusted the clock "
351 "by %li microseconds\n", adjust.offset);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200352 adjust.modes = ADJ_OFFSET_SINGLESHOT;
353 do_adjtimex(&adjust);
354 }
355 return delta;
356}
357
358static DEFINE_PER_CPU(atomic_t, clock_sync_word);
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100359static DEFINE_MUTEX(clock_sync_mutex);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200360static unsigned long clock_sync_flags;
361
362#define CLOCK_SYNC_HAS_ETR 0
363#define CLOCK_SYNC_HAS_STP 1
364#define CLOCK_SYNC_ETR 2
365#define CLOCK_SYNC_STP 3
366
367/*
Martin Schwidefsky40277892016-05-31 10:16:24 +0200368 * The get_clock function for the physical clock. It will get the current
369 * TOD clock, subtract the LPAR offset and write the result to *clock.
370 * The function returns 0 if the clock is in sync with the external time
371 * source. If the clock mode is local it will return -EOPNOTSUPP and
372 * -EAGAIN if the clock is not in sync with the external reference.
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200373 */
Martin Schwidefsky40277892016-05-31 10:16:24 +0200374int get_phys_clock(unsigned long long *clock)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200375{
376 atomic_t *sw_ptr;
377 unsigned int sw0, sw1;
378
379 sw_ptr = &get_cpu_var(clock_sync_word);
380 sw0 = atomic_read(sw_ptr);
Martin Schwidefsky40277892016-05-31 10:16:24 +0200381 *clock = get_tod_clock() - lpar_offset;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200382 sw1 = atomic_read(sw_ptr);
Heiko Carstensbd119ee2009-12-07 12:52:14 +0100383 put_cpu_var(clock_sync_word);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200384 if (sw0 == sw1 && (sw0 & 0x80000000U))
385 /* Success: time is in sync. */
386 return 0;
387 if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags) &&
388 !test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
Heiko Carstensa8f6db42012-09-06 15:13:34 +0200389 return -EOPNOTSUPP;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200390 if (!test_bit(CLOCK_SYNC_ETR, &clock_sync_flags) &&
391 !test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
392 return -EACCES;
393 return -EAGAIN;
394}
Martin Schwidefsky40277892016-05-31 10:16:24 +0200395EXPORT_SYMBOL(get_phys_clock);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200396
397/*
398 * Make get_sync_clock return -EAGAIN.
399 */
400static void disable_sync_clock(void *dummy)
401{
Christoph Lametereb7e7d72014-08-17 12:30:45 -0500402 atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200403 /*
404 * Clear the in-sync bit 2^31. All get_sync_clock calls will
405 * fail until the sync bit is turned back on. In addition
406 * increase the "sequence" counter to avoid the race of an
407 * etr event and the complete recovery against get_sync_clock.
408 */
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200409 atomic_andnot(0x80000000, sw_ptr);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200410 atomic_inc(sw_ptr);
411}
412
413/*
414 * Make get_sync_clock return 0 again.
415 * Needs to be called from a context disabled for preemption.
416 */
417static void enable_sync_clock(void)
418{
Christoph Lametereb7e7d72014-08-17 12:30:45 -0500419 atomic_t *sw_ptr = this_cpu_ptr(&clock_sync_word);
Peter Zijlstra805de8f42015-04-24 01:12:32 +0200420 atomic_or(0x80000000, sw_ptr);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200421}
422
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100423/*
424 * Function to check if the clock is in sync.
425 */
426static inline int check_sync_clock(void)
427{
428 atomic_t *sw_ptr;
429 int rc;
430
431 sw_ptr = &get_cpu_var(clock_sync_word);
432 rc = (atomic_read(sw_ptr) & 0x80000000U) != 0;
Heiko Carstensbd119ee2009-12-07 12:52:14 +0100433 put_cpu_var(clock_sync_word);
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100434 return rc;
435}
436
Heiko Carstens750887d2008-12-25 13:38:37 +0100437/* Single threaded workqueue used for etr and stp sync events */
438static struct workqueue_struct *time_sync_wq;
439
440static void __init time_init_wq(void)
441{
Heiko Carstens179cb812009-01-23 16:40:26 +0100442 if (time_sync_wq)
443 return;
444 time_sync_wq = create_singlethread_workqueue("timesync");
Heiko Carstens750887d2008-12-25 13:38:37 +0100445}
446
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200447struct clock_sync_data {
Heiko Carstens750887d2008-12-25 13:38:37 +0100448 atomic_t cpus;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200449 int in_sync;
450 unsigned long long fixup_cc;
Heiko Carstens750887d2008-12-25 13:38:37 +0100451 int etr_port;
452 struct etr_aib *etr_aib;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200453};
Heiko Carstens5a62b192008-04-17 07:46:25 +0200454
Heiko Carstens750887d2008-12-25 13:38:37 +0100455static void clock_sync_cpu(struct clock_sync_data *sync)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100456{
Heiko Carstens750887d2008-12-25 13:38:37 +0100457 atomic_dec(&sync->cpus);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200458 enable_sync_clock();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100459 /*
460 * This looks like a busy wait loop but it isn't. etr_sync_cpus
461 * is called on all other cpus while the TOD clocks is stopped.
462 * __udelay will stop the cpu on an enabled wait psw until the
463 * TOD is running again.
464 */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200465 while (sync->in_sync == 0) {
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100466 __udelay(1);
Heiko Carstens6c732de2007-02-21 10:55:15 +0100467 /*
468 * A different cpu changes *in_sync. Therefore use
469 * barrier() to force memory access.
470 */
471 barrier();
472 }
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200473 if (sync->in_sync != 1)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100474 /* Didn't work. Clear per-cpu in sync bit again. */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200475 disable_sync_clock(NULL);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100476 /*
477 * This round of TOD syncing is done. Set the clock comparator
478 * to the next tick and let the processor continue.
479 */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200480 fixup_clock_comparator(sync->fixup_cc);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100481}
482
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100483/*
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200484 * Server Time Protocol (STP) code.
485 */
Kees Cook4cc7ecb72016-03-17 14:23:00 -0700486static bool stp_online;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200487static struct stp_sstpi stp_info;
488static void *stp_page;
489
490static void stp_work_fn(struct work_struct *work);
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +0100491static DEFINE_MUTEX(stp_work_mutex);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200492static DECLARE_WORK(stp_work, stp_work_fn);
Martin Schwidefsky04362302009-04-14 15:36:19 +0200493static struct timer_list stp_timer;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200494
495static int __init early_parse_stp(char *p)
496{
Kees Cook4cc7ecb72016-03-17 14:23:00 -0700497 return kstrtobool(p, &stp_online);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200498}
499early_param("stp", early_parse_stp);
500
501/*
502 * Reset STP attachment.
503 */
Heiko Carstens8f847002008-08-01 16:39:19 +0200504static void __init stp_reset(void)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200505{
506 int rc;
507
Heiko Carstensd7d11042009-06-22 12:08:03 +0200508 stp_page = (void *) get_zeroed_page(GFP_ATOMIC);
Martin Schwidefsky2f82f572016-05-31 09:09:57 +0200509 rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
Martin Schwidefsky4a672cf2008-10-10 21:33:29 +0200510 if (rc == 0)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200511 set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
512 else if (stp_online) {
Joe Perchesbaebc702016-03-03 20:49:57 -0800513 pr_warn("The real or virtual hardware system does not provide an STP interface\n");
Heiko Carstensd7d11042009-06-22 12:08:03 +0200514 free_page((unsigned long) stp_page);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200515 stp_page = NULL;
516 stp_online = 0;
517 }
518}
519
Martin Schwidefsky04362302009-04-14 15:36:19 +0200520static void stp_timeout(unsigned long dummy)
521{
522 queue_work(time_sync_wq, &stp_work);
523}
524
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200525static int __init stp_init(void)
526{
Heiko Carstens750887d2008-12-25 13:38:37 +0100527 if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
528 return 0;
Martin Schwidefsky04362302009-04-14 15:36:19 +0200529 setup_timer(&stp_timer, stp_timeout, 0UL);
Heiko Carstens750887d2008-12-25 13:38:37 +0100530 time_init_wq();
531 if (!stp_online)
532 return 0;
533 queue_work(time_sync_wq, &stp_work);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200534 return 0;
535}
536
537arch_initcall(stp_init);
538
539/*
540 * STP timing alert. There are three causes:
541 * 1) timing status change
542 * 2) link availability change
543 * 3) time control parameter change
544 * In all three cases we are only interested in the clock source state.
545 * If a STP clock source is now available use it.
546 */
547static void stp_timing_alert(struct stp_irq_parm *intparm)
548{
549 if (intparm->tsc || intparm->lac || intparm->tcpc)
Heiko Carstens750887d2008-12-25 13:38:37 +0100550 queue_work(time_sync_wq, &stp_work);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200551}
552
553/*
554 * STP sync check machine check. This is called when the timing state
555 * changes from the synchronized state to the unsynchronized state.
556 * After a STP sync check the clock is not in sync. The machine check
557 * is broadcasted to all cpus at the same time.
558 */
Heiko Carstens29b0a822015-10-09 13:48:03 +0200559int stp_sync_check(void)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200560{
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200561 disable_sync_clock(NULL);
Heiko Carstens29b0a822015-10-09 13:48:03 +0200562 return 1;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200563}
564
565/*
566 * STP island condition machine check. This is called when an attached
567 * server attempts to communicate over an STP link and the servers
568 * have matching CTN ids and have a valid stratum-1 configuration
569 * but the configurations do not match.
570 */
Heiko Carstens29b0a822015-10-09 13:48:03 +0200571int stp_island_check(void)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200572{
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200573 disable_sync_clock(NULL);
Heiko Carstens29b0a822015-10-09 13:48:03 +0200574 return 1;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200575}
576
Heiko Carstens29b0a822015-10-09 13:48:03 +0200577void stp_queue_work(void)
578{
579 queue_work(time_sync_wq, &stp_work);
580}
Heiko Carstens750887d2008-12-25 13:38:37 +0100581
582static int stp_sync_clock(void *data)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200583{
Heiko Carstens750887d2008-12-25 13:38:37 +0100584 static int first;
Fan Zhangfdf03652015-05-13 10:58:41 +0200585 unsigned long long old_clock, delta, new_clock, clock_delta;
Heiko Carstens750887d2008-12-25 13:38:37 +0100586 struct clock_sync_data *stp_sync;
Martin Schwidefsky40277892016-05-31 10:16:24 +0200587 struct ptff_qto qto;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200588 int rc;
589
Heiko Carstens750887d2008-12-25 13:38:37 +0100590 stp_sync = data;
591
592 if (xchg(&first, 1) == 1) {
593 /* Slave */
594 clock_sync_cpu(stp_sync);
595 return 0;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200596 }
597
Heiko Carstens750887d2008-12-25 13:38:37 +0100598 /* Wait until all other cpus entered the sync function. */
599 while (atomic_read(&stp_sync->cpus) != 0)
600 cpu_relax();
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200601
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200602 enable_sync_clock();
603
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200604 rc = 0;
605 if (stp_info.todoff[0] || stp_info.todoff[1] ||
606 stp_info.todoff[2] || stp_info.todoff[3] ||
607 stp_info.tmd != 2) {
Heiko Carstens1aae0562013-01-30 09:49:40 +0100608 old_clock = get_tod_clock();
Martin Schwidefsky2f82f572016-05-31 09:09:57 +0200609 rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0, &clock_delta);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200610 if (rc == 0) {
Martin Schwidefsky2f82f572016-05-31 09:09:57 +0200611 new_clock = old_clock + clock_delta;
Fan Zhangfdf03652015-05-13 10:58:41 +0200612 delta = adjust_time(old_clock, new_clock, 0);
Martin Schwidefsky40277892016-05-31 10:16:24 +0200613 if (ptff_query(PTFF_QTO) &&
614 ptff(&qto, sizeof(qto), PTFF_QTO) == 0)
615 /* Update LPAR offset */
616 lpar_offset = qto.tod_epoch_difference;
Fan Zhangfdf03652015-05-13 10:58:41 +0200617 atomic_notifier_call_chain(&s390_epoch_delta_notifier,
618 0, &clock_delta);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200619 fixup_clock_comparator(delta);
620 rc = chsc_sstpi(stp_page, &stp_info,
621 sizeof(struct stp_sstpi));
622 if (rc == 0 && stp_info.tmd != 2)
623 rc = -EAGAIN;
624 }
625 }
626 if (rc) {
627 disable_sync_clock(NULL);
Heiko Carstens750887d2008-12-25 13:38:37 +0100628 stp_sync->in_sync = -EAGAIN;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200629 } else
Heiko Carstens750887d2008-12-25 13:38:37 +0100630 stp_sync->in_sync = 1;
631 xchg(&first, 0);
632 return 0;
633}
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200634
Heiko Carstens750887d2008-12-25 13:38:37 +0100635/*
636 * STP work. Check for the STP state and take over the clock
637 * synchronization if the STP clock source is usable.
638 */
639static void stp_work_fn(struct work_struct *work)
640{
641 struct clock_sync_data stp_sync;
642 int rc;
643
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +0100644 /* prevent multiple execution. */
645 mutex_lock(&stp_work_mutex);
646
Heiko Carstens750887d2008-12-25 13:38:37 +0100647 if (!stp_online) {
Martin Schwidefsky2f82f572016-05-31 09:09:57 +0200648 chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000, NULL);
Martin Schwidefsky04362302009-04-14 15:36:19 +0200649 del_timer_sync(&stp_timer);
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +0100650 goto out_unlock;
Heiko Carstens750887d2008-12-25 13:38:37 +0100651 }
652
Martin Schwidefsky2f82f572016-05-31 09:09:57 +0200653 rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xb0e0, NULL);
Heiko Carstens750887d2008-12-25 13:38:37 +0100654 if (rc)
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +0100655 goto out_unlock;
Heiko Carstens750887d2008-12-25 13:38:37 +0100656
657 rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
658 if (rc || stp_info.c == 0)
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +0100659 goto out_unlock;
Heiko Carstens750887d2008-12-25 13:38:37 +0100660
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100661 /* Skip synchronization if the clock is already in sync. */
662 if (check_sync_clock())
663 goto out_unlock;
664
Heiko Carstens750887d2008-12-25 13:38:37 +0100665 memset(&stp_sync, 0, sizeof(stp_sync));
666 get_online_cpus();
667 atomic_set(&stp_sync.cpus, num_online_cpus() - 1);
KOSAKI Motohiro0f1959f2011-05-23 10:24:36 +0200668 stop_machine(stp_sync_clock, &stp_sync, cpu_online_mask);
Heiko Carstens750887d2008-12-25 13:38:37 +0100669 put_online_cpus();
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +0100670
Martin Schwidefsky04362302009-04-14 15:36:19 +0200671 if (!check_sync_clock())
672 /*
673 * There is a usable clock but the synchonization failed.
674 * Retry after a second.
675 */
676 mod_timer(&stp_timer, jiffies + HZ);
677
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +0100678out_unlock:
679 mutex_unlock(&stp_work_mutex);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200680}
681
682/*
Kay Sievers3fbacff2011-12-21 15:09:50 -0800683 * STP subsys sysfs interface functions
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200684 */
Kay Sievers3fbacff2011-12-21 15:09:50 -0800685static struct bus_type stp_subsys = {
686 .name = "stp",
687 .dev_name = "stp",
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200688};
689
Kay Sievers3fbacff2011-12-21 15:09:50 -0800690static ssize_t stp_ctn_id_show(struct device *dev,
691 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +0100692 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200693{
694 if (!stp_online)
695 return -ENODATA;
696 return sprintf(buf, "%016llx\n",
697 *(unsigned long long *) stp_info.ctnid);
698}
699
Kay Sievers3fbacff2011-12-21 15:09:50 -0800700static DEVICE_ATTR(ctn_id, 0400, stp_ctn_id_show, NULL);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200701
Kay Sievers3fbacff2011-12-21 15:09:50 -0800702static ssize_t stp_ctn_type_show(struct device *dev,
703 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +0100704 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200705{
706 if (!stp_online)
707 return -ENODATA;
708 return sprintf(buf, "%i\n", stp_info.ctn);
709}
710
Kay Sievers3fbacff2011-12-21 15:09:50 -0800711static DEVICE_ATTR(ctn_type, 0400, stp_ctn_type_show, NULL);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200712
Kay Sievers3fbacff2011-12-21 15:09:50 -0800713static ssize_t stp_dst_offset_show(struct device *dev,
714 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +0100715 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200716{
717 if (!stp_online || !(stp_info.vbits & 0x2000))
718 return -ENODATA;
719 return sprintf(buf, "%i\n", (int)(s16) stp_info.dsto);
720}
721
Kay Sievers3fbacff2011-12-21 15:09:50 -0800722static DEVICE_ATTR(dst_offset, 0400, stp_dst_offset_show, NULL);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200723
Kay Sievers3fbacff2011-12-21 15:09:50 -0800724static ssize_t stp_leap_seconds_show(struct device *dev,
725 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +0100726 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200727{
728 if (!stp_online || !(stp_info.vbits & 0x8000))
729 return -ENODATA;
730 return sprintf(buf, "%i\n", (int)(s16) stp_info.leaps);
731}
732
Kay Sievers3fbacff2011-12-21 15:09:50 -0800733static DEVICE_ATTR(leap_seconds, 0400, stp_leap_seconds_show, NULL);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200734
Kay Sievers3fbacff2011-12-21 15:09:50 -0800735static ssize_t stp_stratum_show(struct device *dev,
736 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +0100737 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200738{
739 if (!stp_online)
740 return -ENODATA;
741 return sprintf(buf, "%i\n", (int)(s16) stp_info.stratum);
742}
743
Kay Sievers3fbacff2011-12-21 15:09:50 -0800744static DEVICE_ATTR(stratum, 0400, stp_stratum_show, NULL);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200745
Kay Sievers3fbacff2011-12-21 15:09:50 -0800746static ssize_t stp_time_offset_show(struct device *dev,
747 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +0100748 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200749{
750 if (!stp_online || !(stp_info.vbits & 0x0800))
751 return -ENODATA;
752 return sprintf(buf, "%i\n", (int) stp_info.tto);
753}
754
Kay Sievers3fbacff2011-12-21 15:09:50 -0800755static DEVICE_ATTR(time_offset, 0400, stp_time_offset_show, NULL);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200756
Kay Sievers3fbacff2011-12-21 15:09:50 -0800757static ssize_t stp_time_zone_offset_show(struct device *dev,
758 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +0100759 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200760{
761 if (!stp_online || !(stp_info.vbits & 0x4000))
762 return -ENODATA;
763 return sprintf(buf, "%i\n", (int)(s16) stp_info.tzo);
764}
765
Kay Sievers3fbacff2011-12-21 15:09:50 -0800766static DEVICE_ATTR(time_zone_offset, 0400,
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200767 stp_time_zone_offset_show, NULL);
768
Kay Sievers3fbacff2011-12-21 15:09:50 -0800769static ssize_t stp_timing_mode_show(struct device *dev,
770 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +0100771 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200772{
773 if (!stp_online)
774 return -ENODATA;
775 return sprintf(buf, "%i\n", stp_info.tmd);
776}
777
Kay Sievers3fbacff2011-12-21 15:09:50 -0800778static DEVICE_ATTR(timing_mode, 0400, stp_timing_mode_show, NULL);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200779
Kay Sievers3fbacff2011-12-21 15:09:50 -0800780static ssize_t stp_timing_state_show(struct device *dev,
781 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +0100782 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200783{
784 if (!stp_online)
785 return -ENODATA;
786 return sprintf(buf, "%i\n", stp_info.tst);
787}
788
Kay Sievers3fbacff2011-12-21 15:09:50 -0800789static DEVICE_ATTR(timing_state, 0400, stp_timing_state_show, NULL);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200790
Kay Sievers3fbacff2011-12-21 15:09:50 -0800791static ssize_t stp_online_show(struct device *dev,
792 struct device_attribute *attr,
Andi Kleenc9be0a32010-01-05 12:47:58 +0100793 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200794{
795 return sprintf(buf, "%i\n", stp_online);
796}
797
Kay Sievers3fbacff2011-12-21 15:09:50 -0800798static ssize_t stp_online_store(struct device *dev,
799 struct device_attribute *attr,
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200800 const char *buf, size_t count)
801{
802 unsigned int value;
803
804 value = simple_strtoul(buf, NULL, 0);
805 if (value != 0 && value != 1)
806 return -EINVAL;
807 if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
808 return -EOPNOTSUPP;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100809 mutex_lock(&clock_sync_mutex);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200810 stp_online = value;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100811 if (stp_online)
812 set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
813 else
814 clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
Heiko Carstens750887d2008-12-25 13:38:37 +0100815 queue_work(time_sync_wq, &stp_work);
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100816 mutex_unlock(&clock_sync_mutex);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200817 return count;
818}
819
820/*
Kay Sievers3fbacff2011-12-21 15:09:50 -0800821 * Can't use DEVICE_ATTR because the attribute should be named
822 * stp/online but dev_attr_online already exists in this file ..
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200823 */
Kay Sievers3fbacff2011-12-21 15:09:50 -0800824static struct device_attribute dev_attr_stp_online = {
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200825 .attr = { .name = "online", .mode = 0600 },
826 .show = stp_online_show,
827 .store = stp_online_store,
828};
829
Kay Sievers3fbacff2011-12-21 15:09:50 -0800830static struct device_attribute *stp_attributes[] = {
831 &dev_attr_ctn_id,
832 &dev_attr_ctn_type,
833 &dev_attr_dst_offset,
834 &dev_attr_leap_seconds,
835 &dev_attr_stp_online,
836 &dev_attr_stratum,
837 &dev_attr_time_offset,
838 &dev_attr_time_zone_offset,
839 &dev_attr_timing_mode,
840 &dev_attr_timing_state,
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200841 NULL
842};
843
844static int __init stp_init_sysfs(void)
845{
Kay Sievers3fbacff2011-12-21 15:09:50 -0800846 struct device_attribute **attr;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200847 int rc;
848
Kay Sievers3fbacff2011-12-21 15:09:50 -0800849 rc = subsys_system_register(&stp_subsys, NULL);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200850 if (rc)
851 goto out;
852 for (attr = stp_attributes; *attr; attr++) {
Kay Sievers3fbacff2011-12-21 15:09:50 -0800853 rc = device_create_file(stp_subsys.dev_root, *attr);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200854 if (rc)
855 goto out_unreg;
856 }
857 return 0;
858out_unreg:
859 for (; attr >= stp_attributes; attr--)
Kay Sievers3fbacff2011-12-21 15:09:50 -0800860 device_remove_file(stp_subsys.dev_root, *attr);
861 bus_unregister(&stp_subsys);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200862out:
863 return rc;
864}
865
866device_initcall(stp_init_sysfs);