blob: aa2483e460f32a93e0906fd8a838d3b0dc4cdc4b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/s390/kernel/time.c
3 * Time of day based timer functions.
4 *
5 * S390 version
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02006 * Copyright IBM Corp. 1999, 2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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
Martin Schwidefskyfeab6502008-12-25 13:39:38 +010015#define KMSG_COMPONENT "time"
16#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
17
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>
Ralf Baechle3367b992007-05-08 00:27:52 -070029#include <linux/sysdev.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>
Martin Schwidefskydc64bef2006-10-06 16:38:48 +020037#include <linux/clocksource.h>
Heiko Carstens5a62b192008-04-17 07:46:25 +020038#include <linux/clockchips.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/uaccess.h>
40#include <asm/delay.h>
41#include <asm/s390_ext.h>
42#include <asm/div64.h>
Martin Schwidefskyb0206322008-12-25 13:38:36 +010043#include <asm/vdso.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/irq.h>
Heiko Carstens5a489b92006-10-06 16:38:35 +020045#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/timer.h>
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010047#include <asm/etr.h>
Heiko Carstensa8061702008-04-17 07:46:26 +020048#include <asm/cio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* change this if you have some constant time drift */
51#define USECS_PER_JIFFY ((unsigned long) 1000000/HZ)
52#define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)
53
Martin Schwidefskyb6112cc2009-04-14 15:36:28 +020054u64 sched_clock_base_cc = -1; /* Force to data section. */
Heiko Carstens05e7ff72009-09-11 10:28:31 +020055EXPORT_SYMBOL_GPL(sched_clock_base_cc);
Martin Schwidefskyb6112cc2009-04-14 15:36:28 +020056
Heiko Carstens5a62b192008-04-17 07:46:25 +020057static DEFINE_PER_CPU(struct clock_event_device, comparators);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
60 * Scheduler clock - returns current time in nanosec units.
61 */
Heiko Carstens88dbd202009-06-12 10:26:46 +020062unsigned long long notrace sched_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Heiko Carstens05e7ff72009-09-11 10:28:31 +020064 return (get_clock_monotonic() * 125) >> 9;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
Jan Glauber32f65f22006-02-01 03:06:33 -080067/*
68 * Monotonic_clock - returns # of nanoseconds passed since time_init()
69 */
70unsigned long long monotonic_clock(void)
71{
72 return sched_clock();
73}
74EXPORT_SYMBOL(monotonic_clock);
75
John Stultzb1e2ba82010-03-08 12:25:19 +010076void tod_to_timeval(__u64 todval, struct timespec *xt)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 unsigned long long sec;
79
80 sec = todval >> 12;
81 do_div(sec, 1000000);
John Stultzb1e2ba82010-03-08 12:25:19 +010082 xt->tv_sec = sec;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 todval -= (sec * 1000000) << 12;
John Stultzb1e2ba82010-03-08 12:25:19 +010084 xt->tv_nsec = ((todval * 1000) >> 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
Christof Schmittb592e892009-08-18 15:43:31 +020086EXPORT_SYMBOL(tod_to_timeval);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Heiko Carstens5a62b192008-04-17 07:46:25 +020088void clock_comparator_work(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Heiko Carstens5a62b192008-04-17 07:46:25 +020090 struct clock_event_device *cd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Heiko Carstens5a62b192008-04-17 07:46:25 +020092 S390_lowcore.clock_comparator = -1ULL;
93 set_clock_comparator(S390_lowcore.clock_comparator);
94 cd = &__get_cpu_var(comparators);
95 cd->event_handler(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/*
Heiko Carstens5a62b192008-04-17 07:46:25 +020099 * Fixup the clock comparator.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
Heiko Carstens5a62b192008-04-17 07:46:25 +0200101static void fixup_clock_comparator(unsigned long long delta)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Heiko Carstens5a62b192008-04-17 07:46:25 +0200103 /* If nobody is waiting there's nothing to fix. */
104 if (S390_lowcore.clock_comparator == -1ULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200106 S390_lowcore.clock_comparator += delta;
107 set_clock_comparator(S390_lowcore.clock_comparator);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Heiko Carstens5a62b192008-04-17 07:46:25 +0200110static int s390_next_event(unsigned long delta,
111 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Heiko Carstens5a62b192008-04-17 07:46:25 +0200113 S390_lowcore.clock_comparator = get_clock() + delta;
114 set_clock_comparator(S390_lowcore.clock_comparator);
115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Heiko Carstens5a62b192008-04-17 07:46:25 +0200118static void s390_set_mode(enum clock_event_mode mode,
119 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100121}
122
123/*
124 * Set up lowcore and control register of the current cpu to
125 * enable TOD clock and clock comparator interrupts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 */
127void init_cpu_timer(void)
128{
Heiko Carstens5a62b192008-04-17 07:46:25 +0200129 struct clock_event_device *cd;
130 int cpu;
131
132 S390_lowcore.clock_comparator = -1ULL;
133 set_clock_comparator(S390_lowcore.clock_comparator);
134
135 cpu = smp_processor_id();
136 cd = &per_cpu(comparators, cpu);
137 cd->name = "comparator";
138 cd->features = CLOCK_EVT_FEAT_ONESHOT;
139 cd->mult = 16777;
140 cd->shift = 12;
141 cd->min_delta_ns = 1;
142 cd->max_delta_ns = LONG_MAX;
143 cd->rating = 400;
Rusty Russell320ab2b2008-12-13 21:20:26 +1030144 cd->cpumask = cpumask_of(cpu);
Heiko Carstens5a62b192008-04-17 07:46:25 +0200145 cd->set_next_event = s390_next_event;
146 cd->set_mode = s390_set_mode;
147
148 clockevents_register_device(cd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100150 /* Enable clock comparator timer interrupt. */
151 __ctl_set_bit(0,11);
152
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200153 /* Always allow the timing alert external interrupt. */
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100154 __ctl_set_bit(0, 4);
155}
156
157static void clock_comparator_interrupt(__u16 code)
158{
Heiko Carstensd3d238c2008-10-03 21:54:59 +0200159 if (S390_lowcore.clock_comparator == -1ULL)
160 set_clock_comparator(S390_lowcore.clock_comparator);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100161}
162
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200163static void etr_timing_alert(struct etr_irq_parm *);
164static void stp_timing_alert(struct stp_irq_parm *);
165
166static void timing_alert_interrupt(__u16 code)
167{
168 if (S390_lowcore.ext_params & 0x00c40000)
169 etr_timing_alert((struct etr_irq_parm *)
170 &S390_lowcore.ext_params);
171 if (S390_lowcore.ext_params & 0x00038000)
172 stp_timing_alert((struct stp_irq_parm *)
173 &S390_lowcore.ext_params);
174}
175
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100176static void etr_reset(void);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200177static void stp_reset(void);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100178
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200179void read_persistent_clock(struct timespec *ts)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100180{
Martin Schwidefskyd4f587c2009-08-14 15:47:31 +0200181 tod_to_timeval(get_clock() - TOD_UNIX_EPOCH, ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Martin Schwidefsky23970e32009-08-14 15:47:32 +0200184void read_boot_clock(struct timespec *ts)
185{
186 tod_to_timeval(sched_clock_base_cc - TOD_UNIX_EPOCH, ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Magnus Damm8e196082009-04-21 12:24:00 -0700189static cycle_t read_tod_clock(struct clocksource *cs)
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200190{
191 return get_clock();
192}
193
194static struct clocksource clocksource_tod = {
195 .name = "tod",
Christian Borntraegerd2cb0e62007-11-05 11:10:14 +0100196 .rating = 400,
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200197 .read = read_tod_clock,
198 .mask = -1ULL,
199 .mult = 1000,
200 .shift = 12,
Thomas Gleixnercc02d802007-02-16 01:27:39 -0800201 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200202};
203
Martin Schwidefskyf1b82742009-08-14 15:47:21 +0200204struct clocksource * __init clocksource_default_clock(void)
205{
206 return &clocksource_tod;
207}
Martin Schwidefskydc64bef2006-10-06 16:38:48 +0200208
Lin Ming0696b712009-11-17 13:49:50 +0800209void update_vsyscall(struct timespec *wall_time, struct clocksource *clock,
210 u32 mult)
Martin Schwidefskyb0206322008-12-25 13:38:36 +0100211{
212 if (clock != &clocksource_tod)
213 return;
214
215 /* Make userspace gettimeofday spin until we're done. */
216 ++vdso_data->tb_update_count;
217 smp_wmb();
218 vdso_data->xtime_tod_stamp = clock->cycle_last;
John Stultzb1e2ba82010-03-08 12:25:19 +0100219 vdso_data->xtime_clock_sec = wall_time->tv_sec;
220 vdso_data->xtime_clock_nsec = wall_time->tv_nsec;
Martin Schwidefskyb0206322008-12-25 13:38:36 +0100221 vdso_data->wtom_clock_sec = wall_to_monotonic.tv_sec;
222 vdso_data->wtom_clock_nsec = wall_to_monotonic.tv_nsec;
223 smp_wmb();
224 ++vdso_data->tb_update_count;
225}
226
227extern struct timezone sys_tz;
228
229void update_vsyscall_tz(void)
230{
231 /* Make userspace gettimeofday spin until we're done. */
232 ++vdso_data->tb_update_count;
233 smp_wmb();
234 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
235 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
236 smp_wmb();
237 ++vdso_data->tb_update_count;
238}
239
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240/*
241 * Initialize the TOD clock and the CPU timer of
242 * the boot cpu.
243 */
244void __init time_init(void)
245{
Martin Schwidefskyb6112cc2009-04-14 15:36:28 +0200246 /* Reset time synchronization interfaces. */
247 etr_reset();
248 stp_reset();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 /* request the clock comparator external interrupt */
Heiko Carstensd7d11042009-06-22 12:08:03 +0200251 if (register_external_interrupt(0x1004, clock_comparator_interrupt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 panic("Couldn't request external interrupt 0x1004");
253
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200254 /* request the timing alert external interrupt */
Heiko Carstensd7d11042009-06-22 12:08:03 +0200255 if (register_external_interrupt(0x1406, timing_alert_interrupt))
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100256 panic("Couldn't request external interrupt 0x1406");
257
Martin Schwidefskyab96e792009-04-14 15:36:29 +0200258 if (clocksource_register(&clocksource_tod) != 0)
259 panic("Could not register TOD clock source");
260
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100261 /* Enable TOD clock interrupts on the boot cpu. */
262 init_cpu_timer();
Martin Schwidefskyab96e792009-04-14 15:36:29 +0200263
Martin Schwidefskyc185b782008-12-25 13:39:25 +0100264 /* Enable cpu timer interrupts on the boot cpu. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 vtime_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100268/*
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200269 * The time is "clock". old is what we think the time is.
270 * Adjust the value by a multiple of jiffies and add the delta to ntp.
271 * "delay" is an approximation how long the synchronization took. If
272 * the time correction is positive, then "delay" is subtracted from
273 * the time difference and only the remaining part is passed to ntp.
274 */
275static unsigned long long adjust_time(unsigned long long old,
276 unsigned long long clock,
277 unsigned long long delay)
278{
279 unsigned long long delta, ticks;
280 struct timex adjust;
281
282 if (clock > old) {
283 /* It is later than we thought. */
284 delta = ticks = clock - old;
285 delta = ticks = (delta < delay) ? 0 : delta - delay;
286 delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
287 adjust.offset = ticks * (1000000 / HZ);
288 } else {
289 /* It is earlier than we thought. */
290 delta = ticks = old - clock;
291 delta -= do_div(ticks, CLK_TICKS_PER_JIFFY);
292 delta = -delta;
293 adjust.offset = -ticks * (1000000 / HZ);
294 }
Christian Borntraeger8107d822008-11-27 11:05:56 +0100295 sched_clock_base_cc += delta;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200296 if (adjust.offset != 0) {
Martin Schwidefskyfeab6502008-12-25 13:39:38 +0100297 pr_notice("The ETR interface has adjusted the clock "
298 "by %li microseconds\n", adjust.offset);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200299 adjust.modes = ADJ_OFFSET_SINGLESHOT;
300 do_adjtimex(&adjust);
301 }
302 return delta;
303}
304
305static DEFINE_PER_CPU(atomic_t, clock_sync_word);
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100306static DEFINE_MUTEX(clock_sync_mutex);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200307static unsigned long clock_sync_flags;
308
309#define CLOCK_SYNC_HAS_ETR 0
310#define CLOCK_SYNC_HAS_STP 1
311#define CLOCK_SYNC_ETR 2
312#define CLOCK_SYNC_STP 3
313
314/*
315 * The synchronous get_clock function. It will write the current clock
316 * value to the clock pointer and return 0 if the clock is in sync with
317 * the external time source. If the clock mode is local it will return
318 * -ENOSYS and -EAGAIN if the clock is not in sync with the external
319 * reference.
320 */
321int get_sync_clock(unsigned long long *clock)
322{
323 atomic_t *sw_ptr;
324 unsigned int sw0, sw1;
325
326 sw_ptr = &get_cpu_var(clock_sync_word);
327 sw0 = atomic_read(sw_ptr);
328 *clock = get_clock();
329 sw1 = atomic_read(sw_ptr);
Heiko Carstensbd119ee2009-12-07 12:52:14 +0100330 put_cpu_var(clock_sync_word);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200331 if (sw0 == sw1 && (sw0 & 0x80000000U))
332 /* Success: time is in sync. */
333 return 0;
334 if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags) &&
335 !test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
336 return -ENOSYS;
337 if (!test_bit(CLOCK_SYNC_ETR, &clock_sync_flags) &&
338 !test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
339 return -EACCES;
340 return -EAGAIN;
341}
342EXPORT_SYMBOL(get_sync_clock);
343
344/*
345 * Make get_sync_clock return -EAGAIN.
346 */
347static void disable_sync_clock(void *dummy)
348{
349 atomic_t *sw_ptr = &__get_cpu_var(clock_sync_word);
350 /*
351 * Clear the in-sync bit 2^31. All get_sync_clock calls will
352 * fail until the sync bit is turned back on. In addition
353 * increase the "sequence" counter to avoid the race of an
354 * etr event and the complete recovery against get_sync_clock.
355 */
356 atomic_clear_mask(0x80000000, sw_ptr);
357 atomic_inc(sw_ptr);
358}
359
360/*
361 * Make get_sync_clock return 0 again.
362 * Needs to be called from a context disabled for preemption.
363 */
364static void enable_sync_clock(void)
365{
366 atomic_t *sw_ptr = &__get_cpu_var(clock_sync_word);
367 atomic_set_mask(0x80000000, sw_ptr);
368}
369
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100370/*
371 * Function to check if the clock is in sync.
372 */
373static inline int check_sync_clock(void)
374{
375 atomic_t *sw_ptr;
376 int rc;
377
378 sw_ptr = &get_cpu_var(clock_sync_word);
379 rc = (atomic_read(sw_ptr) & 0x80000000U) != 0;
Heiko Carstensbd119ee2009-12-07 12:52:14 +0100380 put_cpu_var(clock_sync_word);
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100381 return rc;
382}
383
Heiko Carstens750887d2008-12-25 13:38:37 +0100384/* Single threaded workqueue used for etr and stp sync events */
385static struct workqueue_struct *time_sync_wq;
386
387static void __init time_init_wq(void)
388{
Heiko Carstens179cb812009-01-23 16:40:26 +0100389 if (time_sync_wq)
390 return;
391 time_sync_wq = create_singlethread_workqueue("timesync");
392 stop_machine_create();
Heiko Carstens750887d2008-12-25 13:38:37 +0100393}
394
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200395/*
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100396 * External Time Reference (ETR) code.
397 */
398static int etr_port0_online;
399static int etr_port1_online;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200400static int etr_steai_available;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100401
402static int __init early_parse_etr(char *p)
403{
404 if (strncmp(p, "off", 3) == 0)
405 etr_port0_online = etr_port1_online = 0;
406 else if (strncmp(p, "port0", 5) == 0)
407 etr_port0_online = 1;
408 else if (strncmp(p, "port1", 5) == 0)
409 etr_port1_online = 1;
410 else if (strncmp(p, "on", 2) == 0)
411 etr_port0_online = etr_port1_online = 1;
412 return 0;
413}
414early_param("etr", early_parse_etr);
415
416enum etr_event {
417 ETR_EVENT_PORT0_CHANGE,
418 ETR_EVENT_PORT1_CHANGE,
419 ETR_EVENT_PORT_ALERT,
420 ETR_EVENT_SYNC_CHECK,
421 ETR_EVENT_SWITCH_LOCAL,
422 ETR_EVENT_UPDATE,
423};
424
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100425/*
426 * Valid bit combinations of the eacr register are (x = don't care):
427 * e0 e1 dp p0 p1 ea es sl
428 * 0 0 x 0 0 0 0 0 initial, disabled state
429 * 0 0 x 0 1 1 0 0 port 1 online
430 * 0 0 x 1 0 1 0 0 port 0 online
431 * 0 0 x 1 1 1 0 0 both ports online
432 * 0 1 x 0 1 1 0 0 port 1 online and usable, ETR or PPS mode
433 * 0 1 x 0 1 1 0 1 port 1 online, usable and ETR mode
434 * 0 1 x 0 1 1 1 0 port 1 online, usable, PPS mode, in-sync
435 * 0 1 x 0 1 1 1 1 port 1 online, usable, ETR mode, in-sync
436 * 0 1 x 1 1 1 0 0 both ports online, port 1 usable
437 * 0 1 x 1 1 1 1 0 both ports online, port 1 usable, PPS mode, in-sync
438 * 0 1 x 1 1 1 1 1 both ports online, port 1 usable, ETR mode, in-sync
439 * 1 0 x 1 0 1 0 0 port 0 online and usable, ETR or PPS mode
440 * 1 0 x 1 0 1 0 1 port 0 online, usable and ETR mode
441 * 1 0 x 1 0 1 1 0 port 0 online, usable, PPS mode, in-sync
442 * 1 0 x 1 0 1 1 1 port 0 online, usable, ETR mode, in-sync
443 * 1 0 x 1 1 1 0 0 both ports online, port 0 usable
444 * 1 0 x 1 1 1 1 0 both ports online, port 0 usable, PPS mode, in-sync
445 * 1 0 x 1 1 1 1 1 both ports online, port 0 usable, ETR mode, in-sync
446 * 1 1 x 1 1 1 1 0 both ports online & usable, ETR, in-sync
447 * 1 1 x 1 1 1 1 1 both ports online & usable, ETR, in-sync
448 */
449static struct etr_eacr etr_eacr;
450static u64 etr_tolec; /* time of last eacr update */
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100451static struct etr_aib etr_port0;
452static int etr_port0_uptodate;
453static struct etr_aib etr_port1;
454static int etr_port1_uptodate;
455static unsigned long etr_events;
456static struct timer_list etr_timer;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100457
458static void etr_timeout(unsigned long dummy);
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200459static void etr_work_fn(struct work_struct *work);
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +0100460static DEFINE_MUTEX(etr_work_mutex);
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200461static DECLARE_WORK(etr_work, etr_work_fn);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100462
463/*
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100464 * Reset ETR attachment.
465 */
466static void etr_reset(void)
467{
468 etr_eacr = (struct etr_eacr) {
469 .e0 = 0, .e1 = 0, ._pad0 = 4, .dp = 0,
470 .p0 = 0, .p1 = 0, ._pad1 = 0, .ea = 0,
471 .es = 0, .sl = 0 };
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200472 if (etr_setr(&etr_eacr) == 0) {
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100473 etr_tolec = get_clock();
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200474 set_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags);
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100475 if (etr_port0_online && etr_port1_online)
476 set_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200477 } else if (etr_port0_online || etr_port1_online) {
Martin Schwidefskyfeab6502008-12-25 13:39:38 +0100478 pr_warning("The real or virtual hardware system does "
479 "not provide an ETR interface\n");
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200480 etr_port0_online = etr_port1_online = 0;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100481 }
482}
483
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200484static int __init etr_init(void)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100485{
486 struct etr_aib aib;
487
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200488 if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags))
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200489 return 0;
Heiko Carstens750887d2008-12-25 13:38:37 +0100490 time_init_wq();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100491 /* Check if this machine has the steai instruction. */
492 if (etr_steai(&aib, ETR_STEAI_STEPPING_PORT) == 0)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200493 etr_steai_available = 1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100494 setup_timer(&etr_timer, etr_timeout, 0UL);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100495 if (etr_port0_online) {
496 set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
Heiko Carstens750887d2008-12-25 13:38:37 +0100497 queue_work(time_sync_wq, &etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100498 }
499 if (etr_port1_online) {
500 set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
Heiko Carstens750887d2008-12-25 13:38:37 +0100501 queue_work(time_sync_wq, &etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100502 }
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200503 return 0;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100504}
505
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200506arch_initcall(etr_init);
507
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100508/*
509 * Two sorts of ETR machine checks. The architecture reads:
510 * "When a machine-check niterruption occurs and if a switch-to-local or
511 * ETR-sync-check interrupt request is pending but disabled, this pending
512 * disabled interruption request is indicated and is cleared".
513 * Which means that we can get etr_switch_to_local events from the machine
514 * check handler although the interruption condition is disabled. Lovely..
515 */
516
517/*
518 * Switch to local machine check. This is called when the last usable
519 * ETR port goes inactive. After switch to local the clock is not in sync.
520 */
521void etr_switch_to_local(void)
522{
523 if (!etr_eacr.sl)
524 return;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100525 disable_sync_clock(NULL);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100526 set_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events);
Heiko Carstens750887d2008-12-25 13:38:37 +0100527 queue_work(time_sync_wq, &etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100528}
529
530/*
531 * ETR sync check machine check. This is called when the ETR OTE and the
532 * local clock OTE are farther apart than the ETR sync check tolerance.
533 * After a ETR sync check the clock is not in sync. The machine check
534 * is broadcasted to all cpus at the same time.
535 */
536void etr_sync_check(void)
537{
538 if (!etr_eacr.es)
539 return;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100540 disable_sync_clock(NULL);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100541 set_bit(ETR_EVENT_SYNC_CHECK, &etr_events);
Heiko Carstens750887d2008-12-25 13:38:37 +0100542 queue_work(time_sync_wq, &etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100543}
544
545/*
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200546 * ETR timing alert. There are two causes:
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100547 * 1) port state change, check the usability of the port
548 * 2) port alert, one of the ETR-data-validity bits (v1-v2 bits of the
549 * sldr-status word) or ETR-data word 1 (edf1) or ETR-data word 3 (edf3)
550 * or ETR-data word 4 (edf4) has changed.
551 */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200552static void etr_timing_alert(struct etr_irq_parm *intparm)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100553{
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100554 if (intparm->pc0)
555 /* ETR port 0 state change. */
556 set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
557 if (intparm->pc1)
558 /* ETR port 1 state change. */
559 set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
560 if (intparm->eai)
561 /*
562 * ETR port alert on either port 0, 1 or both.
563 * Both ports are not up-to-date now.
564 */
565 set_bit(ETR_EVENT_PORT_ALERT, &etr_events);
Heiko Carstens750887d2008-12-25 13:38:37 +0100566 queue_work(time_sync_wq, &etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100567}
568
569static void etr_timeout(unsigned long dummy)
570{
571 set_bit(ETR_EVENT_UPDATE, &etr_events);
Heiko Carstens750887d2008-12-25 13:38:37 +0100572 queue_work(time_sync_wq, &etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100573}
574
575/*
576 * Check if the etr mode is pss.
577 */
578static inline int etr_mode_is_pps(struct etr_eacr eacr)
579{
580 return eacr.es && !eacr.sl;
581}
582
583/*
584 * Check if the etr mode is etr.
585 */
586static inline int etr_mode_is_etr(struct etr_eacr eacr)
587{
588 return eacr.es && eacr.sl;
589}
590
591/*
592 * Check if the port can be used for TOD synchronization.
593 * For PPS mode the port has to receive OTEs. For ETR mode
594 * the port has to receive OTEs, the ETR stepping bit has to
595 * be zero and the validity bits for data frame 1, 2, and 3
596 * have to be 1.
597 */
598static int etr_port_valid(struct etr_aib *aib, int port)
599{
600 unsigned int psc;
601
602 /* Check that this port is receiving OTEs. */
603 if (aib->tsp == 0)
604 return 0;
605
606 psc = port ? aib->esw.psc1 : aib->esw.psc0;
607 if (psc == etr_lpsc_pps_mode)
608 return 1;
609 if (psc == etr_lpsc_operational_step)
610 return !aib->esw.y && aib->slsw.v1 &&
611 aib->slsw.v2 && aib->slsw.v3;
612 return 0;
613}
614
615/*
616 * Check if two ports are on the same network.
617 */
618static int etr_compare_network(struct etr_aib *aib1, struct etr_aib *aib2)
619{
620 // FIXME: any other fields we have to compare?
621 return aib1->edf1.net_id == aib2->edf1.net_id;
622}
623
624/*
625 * Wrapper for etr_stei that converts physical port states
626 * to logical port states to be consistent with the output
627 * of stetr (see etr_psc vs. etr_lpsc).
628 */
629static void etr_steai_cv(struct etr_aib *aib, unsigned int func)
630{
631 BUG_ON(etr_steai(aib, func) != 0);
632 /* Convert port state to logical port state. */
633 if (aib->esw.psc0 == 1)
634 aib->esw.psc0 = 2;
635 else if (aib->esw.psc0 == 0 && aib->esw.p == 0)
636 aib->esw.psc0 = 1;
637 if (aib->esw.psc1 == 1)
638 aib->esw.psc1 = 2;
639 else if (aib->esw.psc1 == 0 && aib->esw.p == 1)
640 aib->esw.psc1 = 1;
641}
642
643/*
644 * Check if the aib a2 is still connected to the same attachment as
645 * aib a1, the etv values differ by one and a2 is valid.
646 */
647static int etr_aib_follows(struct etr_aib *a1, struct etr_aib *a2, int p)
648{
649 int state_a1, state_a2;
650
651 /* Paranoia check: e0/e1 should better be the same. */
652 if (a1->esw.eacr.e0 != a2->esw.eacr.e0 ||
653 a1->esw.eacr.e1 != a2->esw.eacr.e1)
654 return 0;
655
656 /* Still connected to the same etr ? */
657 state_a1 = p ? a1->esw.psc1 : a1->esw.psc0;
658 state_a2 = p ? a2->esw.psc1 : a2->esw.psc0;
659 if (state_a1 == etr_lpsc_operational_step) {
660 if (state_a2 != etr_lpsc_operational_step ||
661 a1->edf1.net_id != a2->edf1.net_id ||
662 a1->edf1.etr_id != a2->edf1.etr_id ||
663 a1->edf1.etr_pn != a2->edf1.etr_pn)
664 return 0;
665 } else if (state_a2 != etr_lpsc_pps_mode)
666 return 0;
667
668 /* The ETV value of a2 needs to be ETV of a1 + 1. */
669 if (a1->edf2.etv + 1 != a2->edf2.etv)
670 return 0;
671
672 if (!etr_port_valid(a2, p))
673 return 0;
674
675 return 1;
676}
677
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200678struct clock_sync_data {
Heiko Carstens750887d2008-12-25 13:38:37 +0100679 atomic_t cpus;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200680 int in_sync;
681 unsigned long long fixup_cc;
Heiko Carstens750887d2008-12-25 13:38:37 +0100682 int etr_port;
683 struct etr_aib *etr_aib;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200684};
Heiko Carstens5a62b192008-04-17 07:46:25 +0200685
Heiko Carstens750887d2008-12-25 13:38:37 +0100686static void clock_sync_cpu(struct clock_sync_data *sync)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100687{
Heiko Carstens750887d2008-12-25 13:38:37 +0100688 atomic_dec(&sync->cpus);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200689 enable_sync_clock();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100690 /*
691 * This looks like a busy wait loop but it isn't. etr_sync_cpus
692 * is called on all other cpus while the TOD clocks is stopped.
693 * __udelay will stop the cpu on an enabled wait psw until the
694 * TOD is running again.
695 */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200696 while (sync->in_sync == 0) {
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100697 __udelay(1);
Heiko Carstens6c732de2007-02-21 10:55:15 +0100698 /*
699 * A different cpu changes *in_sync. Therefore use
700 * barrier() to force memory access.
701 */
702 barrier();
703 }
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200704 if (sync->in_sync != 1)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100705 /* Didn't work. Clear per-cpu in sync bit again. */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200706 disable_sync_clock(NULL);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100707 /*
708 * This round of TOD syncing is done. Set the clock comparator
709 * to the next tick and let the processor continue.
710 */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200711 fixup_clock_comparator(sync->fixup_cc);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100712}
713
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100714/*
715 * Sync the TOD clock using the port refered to by aibp. This port
716 * has to be enabled and the other port has to be disabled. The
717 * last eacr update has to be more than 1.6 seconds in the past.
718 */
Heiko Carstens750887d2008-12-25 13:38:37 +0100719static int etr_sync_clock(void *data)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100720{
Heiko Carstens750887d2008-12-25 13:38:37 +0100721 static int first;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200722 unsigned long long clock, old_clock, delay, delta;
Heiko Carstens750887d2008-12-25 13:38:37 +0100723 struct clock_sync_data *etr_sync;
724 struct etr_aib *sync_port, *aib;
725 int port;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100726 int rc;
727
Heiko Carstens750887d2008-12-25 13:38:37 +0100728 etr_sync = data;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100729
Heiko Carstens750887d2008-12-25 13:38:37 +0100730 if (xchg(&first, 1) == 1) {
731 /* Slave */
732 clock_sync_cpu(etr_sync);
733 return 0;
734 }
735
736 /* Wait until all other cpus entered the sync function. */
737 while (atomic_read(&etr_sync->cpus) != 0)
738 cpu_relax();
739
740 port = etr_sync->etr_port;
741 aib = etr_sync->etr_aib;
742 sync_port = (port == 0) ? &etr_port0 : &etr_port1;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200743 enable_sync_clock();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100744
745 /* Set clock to next OTE. */
746 __ctl_set_bit(14, 21);
747 __ctl_set_bit(0, 29);
748 clock = ((unsigned long long) (aib->edf2.etv + 1)) << 32;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200749 old_clock = get_clock();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100750 if (set_clock(clock) == 0) {
751 __udelay(1); /* Wait for the clock to start. */
752 __ctl_clear_bit(0, 29);
753 __ctl_clear_bit(14, 21);
754 etr_stetr(aib);
755 /* Adjust Linux timing variables. */
756 delay = (unsigned long long)
757 (aib->edf2.etv - sync_port->edf2.etv) << 32;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200758 delta = adjust_time(old_clock, clock, delay);
Heiko Carstens750887d2008-12-25 13:38:37 +0100759 etr_sync->fixup_cc = delta;
Heiko Carstens5a62b192008-04-17 07:46:25 +0200760 fixup_clock_comparator(delta);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100761 /* Verify that the clock is properly set. */
762 if (!etr_aib_follows(sync_port, aib, port)) {
763 /* Didn't work. */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200764 disable_sync_clock(NULL);
Heiko Carstens750887d2008-12-25 13:38:37 +0100765 etr_sync->in_sync = -EAGAIN;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100766 rc = -EAGAIN;
767 } else {
Heiko Carstens750887d2008-12-25 13:38:37 +0100768 etr_sync->in_sync = 1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100769 rc = 0;
770 }
771 } else {
772 /* Could not set the clock ?!? */
773 __ctl_clear_bit(0, 29);
774 __ctl_clear_bit(14, 21);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200775 disable_sync_clock(NULL);
Heiko Carstens750887d2008-12-25 13:38:37 +0100776 etr_sync->in_sync = -EAGAIN;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100777 rc = -EAGAIN;
778 }
Heiko Carstens750887d2008-12-25 13:38:37 +0100779 xchg(&first, 0);
780 return rc;
781}
782
783static int etr_sync_clock_stop(struct etr_aib *aib, int port)
784{
785 struct clock_sync_data etr_sync;
786 struct etr_aib *sync_port;
787 int follows;
788 int rc;
789
790 /* Check if the current aib is adjacent to the sync port aib. */
791 sync_port = (port == 0) ? &etr_port0 : &etr_port1;
792 follows = etr_aib_follows(sync_port, aib, port);
793 memcpy(sync_port, aib, sizeof(*aib));
794 if (!follows)
795 return -EAGAIN;
796 memset(&etr_sync, 0, sizeof(etr_sync));
797 etr_sync.etr_aib = aib;
798 etr_sync.etr_port = port;
799 get_online_cpus();
800 atomic_set(&etr_sync.cpus, num_online_cpus() - 1);
801 rc = stop_machine(etr_sync_clock, &etr_sync, &cpu_online_map);
802 put_online_cpus();
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100803 return rc;
804}
805
806/*
807 * Handle the immediate effects of the different events.
808 * The port change event is used for online/offline changes.
809 */
810static struct etr_eacr etr_handle_events(struct etr_eacr eacr)
811{
812 if (test_and_clear_bit(ETR_EVENT_SYNC_CHECK, &etr_events))
813 eacr.es = 0;
814 if (test_and_clear_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events))
815 eacr.es = eacr.sl = 0;
816 if (test_and_clear_bit(ETR_EVENT_PORT_ALERT, &etr_events))
817 etr_port0_uptodate = etr_port1_uptodate = 0;
818
819 if (test_and_clear_bit(ETR_EVENT_PORT0_CHANGE, &etr_events)) {
820 if (eacr.e0)
821 /*
822 * Port change of an enabled port. We have to
823 * assume that this can have caused an stepping
824 * port switch.
825 */
826 etr_tolec = get_clock();
827 eacr.p0 = etr_port0_online;
828 if (!eacr.p0)
829 eacr.e0 = 0;
830 etr_port0_uptodate = 0;
831 }
832 if (test_and_clear_bit(ETR_EVENT_PORT1_CHANGE, &etr_events)) {
833 if (eacr.e1)
834 /*
835 * Port change of an enabled port. We have to
836 * assume that this can have caused an stepping
837 * port switch.
838 */
839 etr_tolec = get_clock();
840 eacr.p1 = etr_port1_online;
841 if (!eacr.p1)
842 eacr.e1 = 0;
843 etr_port1_uptodate = 0;
844 }
845 clear_bit(ETR_EVENT_UPDATE, &etr_events);
846 return eacr;
847}
848
849/*
850 * Set up a timer that expires after the etr_tolec + 1.6 seconds if
851 * one of the ports needs an update.
852 */
853static void etr_set_tolec_timeout(unsigned long long now)
854{
855 unsigned long micros;
856
857 if ((!etr_eacr.p0 || etr_port0_uptodate) &&
858 (!etr_eacr.p1 || etr_port1_uptodate))
859 return;
860 micros = (now > etr_tolec) ? ((now - etr_tolec) >> 12) : 0;
861 micros = (micros > 1600000) ? 0 : 1600000 - micros;
862 mod_timer(&etr_timer, jiffies + (micros * HZ) / 1000000 + 1);
863}
864
865/*
866 * Set up a time that expires after 1/2 second.
867 */
868static void etr_set_sync_timeout(void)
869{
870 mod_timer(&etr_timer, jiffies + HZ/2);
871}
872
873/*
874 * Update the aib information for one or both ports.
875 */
876static struct etr_eacr etr_handle_update(struct etr_aib *aib,
877 struct etr_eacr eacr)
878{
879 /* With both ports disabled the aib information is useless. */
880 if (!eacr.e0 && !eacr.e1)
881 return eacr;
882
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200883 /* Update port0 or port1 with aib stored in etr_work_fn. */
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100884 if (aib->esw.q == 0) {
885 /* Information for port 0 stored. */
886 if (eacr.p0 && !etr_port0_uptodate) {
887 etr_port0 = *aib;
888 if (etr_port0_online)
889 etr_port0_uptodate = 1;
890 }
891 } else {
892 /* Information for port 1 stored. */
893 if (eacr.p1 && !etr_port1_uptodate) {
894 etr_port1 = *aib;
895 if (etr_port0_online)
896 etr_port1_uptodate = 1;
897 }
898 }
899
900 /*
901 * Do not try to get the alternate port aib if the clock
902 * is not in sync yet.
903 */
Martin Schwidefsky8283cb42009-03-26 15:24:21 +0100904 if (!check_sync_clock())
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100905 return eacr;
906
907 /*
908 * If steai is available we can get the information about
909 * the other port immediately. If only stetr is available the
910 * data-port bit toggle has to be used.
911 */
Martin Schwidefskyd2fec592008-07-14 09:58:56 +0200912 if (etr_steai_available) {
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100913 if (eacr.p0 && !etr_port0_uptodate) {
914 etr_steai_cv(&etr_port0, ETR_STEAI_PORT_0);
915 etr_port0_uptodate = 1;
916 }
917 if (eacr.p1 && !etr_port1_uptodate) {
918 etr_steai_cv(&etr_port1, ETR_STEAI_PORT_1);
919 etr_port1_uptodate = 1;
920 }
921 } else {
922 /*
923 * One port was updated above, if the other
924 * port is not uptodate toggle dp bit.
925 */
926 if ((eacr.p0 && !etr_port0_uptodate) ||
927 (eacr.p1 && !etr_port1_uptodate))
928 eacr.dp ^= 1;
929 else
930 eacr.dp = 0;
931 }
932 return eacr;
933}
934
935/*
936 * Write new etr control register if it differs from the current one.
937 * Return 1 if etr_tolec has been updated as well.
938 */
939static void etr_update_eacr(struct etr_eacr eacr)
940{
941 int dp_changed;
942
943 if (memcmp(&etr_eacr, &eacr, sizeof(eacr)) == 0)
944 /* No change, return. */
945 return;
946 /*
947 * The disable of an active port of the change of the data port
948 * bit can/will cause a change in the data port.
949 */
950 dp_changed = etr_eacr.e0 > eacr.e0 || etr_eacr.e1 > eacr.e1 ||
951 (etr_eacr.dp ^ eacr.dp) != 0;
952 etr_eacr = eacr;
953 etr_setr(&etr_eacr);
954 if (dp_changed)
955 etr_tolec = get_clock();
956}
957
958/*
Heiko Carstens750887d2008-12-25 13:38:37 +0100959 * ETR work. In this function you'll find the main logic. In
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100960 * particular this is the only function that calls etr_update_eacr(),
961 * it "controls" the etr control register.
962 */
Martin Schwidefskyecdcc022007-04-27 16:01:58 +0200963static void etr_work_fn(struct work_struct *work)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100964{
965 unsigned long long now;
966 struct etr_eacr eacr;
967 struct etr_aib aib;
968 int sync_port;
969
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +0100970 /* prevent multiple execution. */
971 mutex_lock(&etr_work_mutex);
972
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100973 /* Create working copy of etr_eacr. */
974 eacr = etr_eacr;
975
976 /* Check for the different events and their immediate effects. */
977 eacr = etr_handle_events(eacr);
978
979 /* Check if ETR is supposed to be active. */
980 eacr.ea = eacr.p0 || eacr.p1;
981 if (!eacr.ea) {
982 /* Both ports offline. Reset everything. */
983 eacr.dp = eacr.es = eacr.sl = 0;
Ingo Molnar1a781a72008-07-15 21:55:59 +0200984 on_each_cpu(disable_sync_clock, NULL, 1);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100985 del_timer_sync(&etr_timer);
986 etr_update_eacr(eacr);
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +0100987 goto out_unlock;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +0100988 }
989
990 /* Store aib to get the current ETR status word. */
991 BUG_ON(etr_stetr(&aib) != 0);
992 etr_port0.esw = etr_port1.esw = aib.esw; /* Copy status word. */
993 now = get_clock();
994
995 /*
996 * Update the port information if the last stepping port change
997 * or data port change is older than 1.6 seconds.
998 */
999 if (now >= etr_tolec + (1600000 << 12))
1000 eacr = etr_handle_update(&aib, eacr);
1001
1002 /*
1003 * Select ports to enable. The prefered synchronization mode is PPS.
1004 * If a port can be enabled depends on a number of things:
1005 * 1) The port needs to be online and uptodate. A port is not
1006 * disabled just because it is not uptodate, but it is only
1007 * enabled if it is uptodate.
1008 * 2) The port needs to have the same mode (pps / etr).
1009 * 3) The port needs to be usable -> etr_port_valid() == 1
1010 * 4) To enable the second port the clock needs to be in sync.
1011 * 5) If both ports are useable and are ETR ports, the network id
1012 * has to be the same.
1013 * The eacr.sl bit is used to indicate etr mode vs. pps mode.
1014 */
1015 if (eacr.p0 && aib.esw.psc0 == etr_lpsc_pps_mode) {
1016 eacr.sl = 0;
1017 eacr.e0 = 1;
1018 if (!etr_mode_is_pps(etr_eacr))
1019 eacr.es = 0;
1020 if (!eacr.es || !eacr.p1 || aib.esw.psc1 != etr_lpsc_pps_mode)
1021 eacr.e1 = 0;
1022 // FIXME: uptodate checks ?
1023 else if (etr_port0_uptodate && etr_port1_uptodate)
1024 eacr.e1 = 1;
1025 sync_port = (etr_port0_uptodate &&
1026 etr_port_valid(&etr_port0, 0)) ? 0 : -1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001027 } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_pps_mode) {
1028 eacr.sl = 0;
1029 eacr.e0 = 0;
1030 eacr.e1 = 1;
1031 if (!etr_mode_is_pps(etr_eacr))
1032 eacr.es = 0;
1033 sync_port = (etr_port1_uptodate &&
1034 etr_port_valid(&etr_port1, 1)) ? 1 : -1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001035 } else if (eacr.p0 && aib.esw.psc0 == etr_lpsc_operational_step) {
1036 eacr.sl = 1;
1037 eacr.e0 = 1;
1038 if (!etr_mode_is_etr(etr_eacr))
1039 eacr.es = 0;
1040 if (!eacr.es || !eacr.p1 ||
1041 aib.esw.psc1 != etr_lpsc_operational_alt)
1042 eacr.e1 = 0;
1043 else if (etr_port0_uptodate && etr_port1_uptodate &&
1044 etr_compare_network(&etr_port0, &etr_port1))
1045 eacr.e1 = 1;
1046 sync_port = (etr_port0_uptodate &&
1047 etr_port_valid(&etr_port0, 0)) ? 0 : -1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001048 } else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_operational_step) {
1049 eacr.sl = 1;
1050 eacr.e0 = 0;
1051 eacr.e1 = 1;
1052 if (!etr_mode_is_etr(etr_eacr))
1053 eacr.es = 0;
1054 sync_port = (etr_port1_uptodate &&
1055 etr_port_valid(&etr_port1, 1)) ? 1 : -1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001056 } else {
1057 /* Both ports not usable. */
1058 eacr.es = eacr.sl = 0;
1059 sync_port = -1;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001060 }
1061
1062 /*
1063 * If the clock is in sync just update the eacr and return.
1064 * If there is no valid sync port wait for a port update.
1065 */
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001066 if (check_sync_clock() || sync_port < 0) {
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001067 etr_update_eacr(eacr);
1068 etr_set_tolec_timeout(now);
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001069 goto out_unlock;
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001070 }
1071
1072 /*
1073 * Prepare control register for clock syncing
1074 * (reset data port bit, set sync check control.
1075 */
1076 eacr.dp = 0;
1077 eacr.es = 1;
1078
1079 /*
1080 * Update eacr and try to synchronize the clock. If the update
1081 * of eacr caused a stepping port switch (or if we have to
1082 * assume that a stepping port switch has occured) or the
1083 * clock syncing failed, reset the sync check control bit
1084 * and set up a timer to try again after 0.5 seconds
1085 */
1086 etr_update_eacr(eacr);
1087 if (now < etr_tolec + (1600000 << 12) ||
Heiko Carstens750887d2008-12-25 13:38:37 +01001088 etr_sync_clock_stop(&aib, sync_port) != 0) {
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001089 /* Sync failed. Try again in 1/2 second. */
1090 eacr.es = 0;
1091 etr_update_eacr(eacr);
1092 etr_set_sync_timeout();
1093 } else
1094 etr_set_tolec_timeout(now);
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001095out_unlock:
1096 mutex_unlock(&etr_work_mutex);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001097}
1098
1099/*
1100 * Sysfs interface functions
1101 */
1102static struct sysdev_class etr_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001103 .name = "etr",
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001104};
1105
1106static struct sys_device etr_port0_dev = {
1107 .id = 0,
1108 .cls = &etr_sysclass,
1109};
1110
1111static struct sys_device etr_port1_dev = {
1112 .id = 1,
1113 .cls = &etr_sysclass,
1114};
1115
1116/*
1117 * ETR class attributes
1118 */
Andi Kleenc9be0a32010-01-05 12:47:58 +01001119static ssize_t etr_stepping_port_show(struct sysdev_class *class,
1120 struct sysdev_class_attribute *attr,
1121 char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001122{
1123 return sprintf(buf, "%i\n", etr_port0.esw.p);
1124}
1125
1126static SYSDEV_CLASS_ATTR(stepping_port, 0400, etr_stepping_port_show, NULL);
1127
Andi Kleenc9be0a32010-01-05 12:47:58 +01001128static ssize_t etr_stepping_mode_show(struct sysdev_class *class,
1129 struct sysdev_class_attribute *attr,
1130 char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001131{
1132 char *mode_str;
1133
1134 if (etr_mode_is_pps(etr_eacr))
1135 mode_str = "pps";
1136 else if (etr_mode_is_etr(etr_eacr))
1137 mode_str = "etr";
1138 else
1139 mode_str = "local";
1140 return sprintf(buf, "%s\n", mode_str);
1141}
1142
1143static SYSDEV_CLASS_ATTR(stepping_mode, 0400, etr_stepping_mode_show, NULL);
1144
1145/*
1146 * ETR port attributes
1147 */
1148static inline struct etr_aib *etr_aib_from_dev(struct sys_device *dev)
1149{
1150 if (dev == &etr_port0_dev)
1151 return etr_port0_online ? &etr_port0 : NULL;
1152 else
1153 return etr_port1_online ? &etr_port1 : NULL;
1154}
1155
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001156static ssize_t etr_online_show(struct sys_device *dev,
1157 struct sysdev_attribute *attr,
1158 char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001159{
1160 unsigned int online;
1161
1162 online = (dev == &etr_port0_dev) ? etr_port0_online : etr_port1_online;
1163 return sprintf(buf, "%i\n", online);
1164}
1165
1166static ssize_t etr_online_store(struct sys_device *dev,
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001167 struct sysdev_attribute *attr,
1168 const char *buf, size_t count)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001169{
1170 unsigned int value;
1171
1172 value = simple_strtoul(buf, NULL, 0);
1173 if (value != 0 && value != 1)
1174 return -EINVAL;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001175 if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags))
1176 return -EOPNOTSUPP;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001177 mutex_lock(&clock_sync_mutex);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001178 if (dev == &etr_port0_dev) {
1179 if (etr_port0_online == value)
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001180 goto out; /* Nothing to do. */
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001181 etr_port0_online = value;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001182 if (etr_port0_online && etr_port1_online)
1183 set_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
1184 else
1185 clear_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001186 set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
Heiko Carstens750887d2008-12-25 13:38:37 +01001187 queue_work(time_sync_wq, &etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001188 } else {
1189 if (etr_port1_online == value)
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001190 goto out; /* Nothing to do. */
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001191 etr_port1_online = value;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001192 if (etr_port0_online && etr_port1_online)
1193 set_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
1194 else
1195 clear_bit(CLOCK_SYNC_ETR, &clock_sync_flags);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001196 set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
Heiko Carstens750887d2008-12-25 13:38:37 +01001197 queue_work(time_sync_wq, &etr_work);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001198 }
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001199out:
1200 mutex_unlock(&clock_sync_mutex);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001201 return count;
1202}
1203
1204static SYSDEV_ATTR(online, 0600, etr_online_show, etr_online_store);
1205
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001206static ssize_t etr_stepping_control_show(struct sys_device *dev,
1207 struct sysdev_attribute *attr,
1208 char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001209{
1210 return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
1211 etr_eacr.e0 : etr_eacr.e1);
1212}
1213
1214static SYSDEV_ATTR(stepping_control, 0400, etr_stepping_control_show, NULL);
1215
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001216static ssize_t etr_mode_code_show(struct sys_device *dev,
1217 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001218{
1219 if (!etr_port0_online && !etr_port1_online)
1220 /* Status word is not uptodate if both ports are offline. */
1221 return -ENODATA;
1222 return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
1223 etr_port0.esw.psc0 : etr_port0.esw.psc1);
1224}
1225
1226static SYSDEV_ATTR(state_code, 0400, etr_mode_code_show, NULL);
1227
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001228static ssize_t etr_untuned_show(struct sys_device *dev,
1229 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001230{
1231 struct etr_aib *aib = etr_aib_from_dev(dev);
1232
1233 if (!aib || !aib->slsw.v1)
1234 return -ENODATA;
1235 return sprintf(buf, "%i\n", aib->edf1.u);
1236}
1237
1238static SYSDEV_ATTR(untuned, 0400, etr_untuned_show, NULL);
1239
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001240static ssize_t etr_network_id_show(struct sys_device *dev,
1241 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001242{
1243 struct etr_aib *aib = etr_aib_from_dev(dev);
1244
1245 if (!aib || !aib->slsw.v1)
1246 return -ENODATA;
1247 return sprintf(buf, "%i\n", aib->edf1.net_id);
1248}
1249
1250static SYSDEV_ATTR(network, 0400, etr_network_id_show, NULL);
1251
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001252static ssize_t etr_id_show(struct sys_device *dev,
1253 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001254{
1255 struct etr_aib *aib = etr_aib_from_dev(dev);
1256
1257 if (!aib || !aib->slsw.v1)
1258 return -ENODATA;
1259 return sprintf(buf, "%i\n", aib->edf1.etr_id);
1260}
1261
1262static SYSDEV_ATTR(id, 0400, etr_id_show, NULL);
1263
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001264static ssize_t etr_port_number_show(struct sys_device *dev,
1265 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001266{
1267 struct etr_aib *aib = etr_aib_from_dev(dev);
1268
1269 if (!aib || !aib->slsw.v1)
1270 return -ENODATA;
1271 return sprintf(buf, "%i\n", aib->edf1.etr_pn);
1272}
1273
1274static SYSDEV_ATTR(port, 0400, etr_port_number_show, NULL);
1275
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001276static ssize_t etr_coupled_show(struct sys_device *dev,
1277 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001278{
1279 struct etr_aib *aib = etr_aib_from_dev(dev);
1280
1281 if (!aib || !aib->slsw.v3)
1282 return -ENODATA;
1283 return sprintf(buf, "%i\n", aib->edf3.c);
1284}
1285
1286static SYSDEV_ATTR(coupled, 0400, etr_coupled_show, NULL);
1287
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001288static ssize_t etr_local_time_show(struct sys_device *dev,
1289 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001290{
1291 struct etr_aib *aib = etr_aib_from_dev(dev);
1292
1293 if (!aib || !aib->slsw.v3)
1294 return -ENODATA;
1295 return sprintf(buf, "%i\n", aib->edf3.blto);
1296}
1297
1298static SYSDEV_ATTR(local_time, 0400, etr_local_time_show, NULL);
1299
Andi Kleen4a0b2b42008-07-01 18:48:41 +02001300static ssize_t etr_utc_offset_show(struct sys_device *dev,
1301 struct sysdev_attribute *attr, char *buf)
Martin Schwidefskyd54853e2007-02-05 21:18:19 +01001302{
1303 struct etr_aib *aib = etr_aib_from_dev(dev);
1304
1305 if (!aib || !aib->slsw.v3)
1306 return -ENODATA;
1307 return sprintf(buf, "%i\n", aib->edf3.buo);
1308}
1309
1310static SYSDEV_ATTR(utc_offset, 0400, etr_utc_offset_show, NULL);
1311
1312static struct sysdev_attribute *etr_port_attributes[] = {
1313 &attr_online,
1314 &attr_stepping_control,
1315 &attr_state_code,
1316 &attr_untuned,
1317 &attr_network,
1318 &attr_id,
1319 &attr_port,
1320 &attr_coupled,
1321 &attr_local_time,
1322 &attr_utc_offset,
1323 NULL
1324};
1325
1326static int __init etr_register_port(struct sys_device *dev)
1327{
1328 struct sysdev_attribute **attr;
1329 int rc;
1330
1331 rc = sysdev_register(dev);
1332 if (rc)
1333 goto out;
1334 for (attr = etr_port_attributes; *attr; attr++) {
1335 rc = sysdev_create_file(dev, *attr);
1336 if (rc)
1337 goto out_unreg;
1338 }
1339 return 0;
1340out_unreg:
1341 for (; attr >= etr_port_attributes; attr--)
1342 sysdev_remove_file(dev, *attr);
1343 sysdev_unregister(dev);
1344out:
1345 return rc;
1346}
1347
1348static void __init etr_unregister_port(struct sys_device *dev)
1349{
1350 struct sysdev_attribute **attr;
1351
1352 for (attr = etr_port_attributes; *attr; attr++)
1353 sysdev_remove_file(dev, *attr);
1354 sysdev_unregister(dev);
1355}
1356
1357static int __init etr_init_sysfs(void)
1358{
1359 int rc;
1360
1361 rc = sysdev_class_register(&etr_sysclass);
1362 if (rc)
1363 goto out;
1364 rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_port);
1365 if (rc)
1366 goto out_unreg_class;
1367 rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_mode);
1368 if (rc)
1369 goto out_remove_stepping_port;
1370 rc = etr_register_port(&etr_port0_dev);
1371 if (rc)
1372 goto out_remove_stepping_mode;
1373 rc = etr_register_port(&etr_port1_dev);
1374 if (rc)
1375 goto out_remove_port0;
1376 return 0;
1377
1378out_remove_port0:
1379 etr_unregister_port(&etr_port0_dev);
1380out_remove_stepping_mode:
1381 sysdev_class_remove_file(&etr_sysclass, &attr_stepping_mode);
1382out_remove_stepping_port:
1383 sysdev_class_remove_file(&etr_sysclass, &attr_stepping_port);
1384out_unreg_class:
1385 sysdev_class_unregister(&etr_sysclass);
1386out:
1387 return rc;
1388}
1389
1390device_initcall(etr_init_sysfs);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001391
1392/*
1393 * Server Time Protocol (STP) code.
1394 */
1395static int stp_online;
1396static struct stp_sstpi stp_info;
1397static void *stp_page;
1398
1399static void stp_work_fn(struct work_struct *work);
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001400static DEFINE_MUTEX(stp_work_mutex);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001401static DECLARE_WORK(stp_work, stp_work_fn);
Martin Schwidefsky04362302009-04-14 15:36:19 +02001402static struct timer_list stp_timer;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001403
1404static int __init early_parse_stp(char *p)
1405{
1406 if (strncmp(p, "off", 3) == 0)
1407 stp_online = 0;
1408 else if (strncmp(p, "on", 2) == 0)
1409 stp_online = 1;
1410 return 0;
1411}
1412early_param("stp", early_parse_stp);
1413
1414/*
1415 * Reset STP attachment.
1416 */
Heiko Carstens8f847002008-08-01 16:39:19 +02001417static void __init stp_reset(void)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001418{
1419 int rc;
1420
Heiko Carstensd7d11042009-06-22 12:08:03 +02001421 stp_page = (void *) get_zeroed_page(GFP_ATOMIC);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001422 rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000);
Martin Schwidefsky4a672cf2008-10-10 21:33:29 +02001423 if (rc == 0)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001424 set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
1425 else if (stp_online) {
Martin Schwidefskyfeab6502008-12-25 13:39:38 +01001426 pr_warning("The real or virtual hardware system does "
1427 "not provide an STP interface\n");
Heiko Carstensd7d11042009-06-22 12:08:03 +02001428 free_page((unsigned long) stp_page);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001429 stp_page = NULL;
1430 stp_online = 0;
1431 }
1432}
1433
Martin Schwidefsky04362302009-04-14 15:36:19 +02001434static void stp_timeout(unsigned long dummy)
1435{
1436 queue_work(time_sync_wq, &stp_work);
1437}
1438
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001439static int __init stp_init(void)
1440{
Heiko Carstens750887d2008-12-25 13:38:37 +01001441 if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
1442 return 0;
Martin Schwidefsky04362302009-04-14 15:36:19 +02001443 setup_timer(&stp_timer, stp_timeout, 0UL);
Heiko Carstens750887d2008-12-25 13:38:37 +01001444 time_init_wq();
1445 if (!stp_online)
1446 return 0;
1447 queue_work(time_sync_wq, &stp_work);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001448 return 0;
1449}
1450
1451arch_initcall(stp_init);
1452
1453/*
1454 * STP timing alert. There are three causes:
1455 * 1) timing status change
1456 * 2) link availability change
1457 * 3) time control parameter change
1458 * In all three cases we are only interested in the clock source state.
1459 * If a STP clock source is now available use it.
1460 */
1461static void stp_timing_alert(struct stp_irq_parm *intparm)
1462{
1463 if (intparm->tsc || intparm->lac || intparm->tcpc)
Heiko Carstens750887d2008-12-25 13:38:37 +01001464 queue_work(time_sync_wq, &stp_work);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001465}
1466
1467/*
1468 * STP sync check machine check. This is called when the timing state
1469 * changes from the synchronized state to the unsynchronized state.
1470 * After a STP sync check the clock is not in sync. The machine check
1471 * is broadcasted to all cpus at the same time.
1472 */
1473void stp_sync_check(void)
1474{
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001475 disable_sync_clock(NULL);
Heiko Carstens750887d2008-12-25 13:38:37 +01001476 queue_work(time_sync_wq, &stp_work);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001477}
1478
1479/*
1480 * STP island condition machine check. This is called when an attached
1481 * server attempts to communicate over an STP link and the servers
1482 * have matching CTN ids and have a valid stratum-1 configuration
1483 * but the configurations do not match.
1484 */
1485void stp_island_check(void)
1486{
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001487 disable_sync_clock(NULL);
Heiko Carstens750887d2008-12-25 13:38:37 +01001488 queue_work(time_sync_wq, &stp_work);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001489}
1490
Heiko Carstens750887d2008-12-25 13:38:37 +01001491
1492static int stp_sync_clock(void *data)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001493{
Heiko Carstens750887d2008-12-25 13:38:37 +01001494 static int first;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001495 unsigned long long old_clock, delta;
Heiko Carstens750887d2008-12-25 13:38:37 +01001496 struct clock_sync_data *stp_sync;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001497 int rc;
1498
Heiko Carstens750887d2008-12-25 13:38:37 +01001499 stp_sync = data;
1500
1501 if (xchg(&first, 1) == 1) {
1502 /* Slave */
1503 clock_sync_cpu(stp_sync);
1504 return 0;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001505 }
1506
Heiko Carstens750887d2008-12-25 13:38:37 +01001507 /* Wait until all other cpus entered the sync function. */
1508 while (atomic_read(&stp_sync->cpus) != 0)
1509 cpu_relax();
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001510
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001511 enable_sync_clock();
1512
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001513 rc = 0;
1514 if (stp_info.todoff[0] || stp_info.todoff[1] ||
1515 stp_info.todoff[2] || stp_info.todoff[3] ||
1516 stp_info.tmd != 2) {
1517 old_clock = get_clock();
1518 rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0);
1519 if (rc == 0) {
1520 delta = adjust_time(old_clock, get_clock(), 0);
1521 fixup_clock_comparator(delta);
1522 rc = chsc_sstpi(stp_page, &stp_info,
1523 sizeof(struct stp_sstpi));
1524 if (rc == 0 && stp_info.tmd != 2)
1525 rc = -EAGAIN;
1526 }
1527 }
1528 if (rc) {
1529 disable_sync_clock(NULL);
Heiko Carstens750887d2008-12-25 13:38:37 +01001530 stp_sync->in_sync = -EAGAIN;
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001531 } else
Heiko Carstens750887d2008-12-25 13:38:37 +01001532 stp_sync->in_sync = 1;
1533 xchg(&first, 0);
1534 return 0;
1535}
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001536
Heiko Carstens750887d2008-12-25 13:38:37 +01001537/*
1538 * STP work. Check for the STP state and take over the clock
1539 * synchronization if the STP clock source is usable.
1540 */
1541static void stp_work_fn(struct work_struct *work)
1542{
1543 struct clock_sync_data stp_sync;
1544 int rc;
1545
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001546 /* prevent multiple execution. */
1547 mutex_lock(&stp_work_mutex);
1548
Heiko Carstens750887d2008-12-25 13:38:37 +01001549 if (!stp_online) {
1550 chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000);
Martin Schwidefsky04362302009-04-14 15:36:19 +02001551 del_timer_sync(&stp_timer);
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001552 goto out_unlock;
Heiko Carstens750887d2008-12-25 13:38:37 +01001553 }
1554
1555 rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xb0e0);
1556 if (rc)
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001557 goto out_unlock;
Heiko Carstens750887d2008-12-25 13:38:37 +01001558
1559 rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
1560 if (rc || stp_info.c == 0)
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001561 goto out_unlock;
Heiko Carstens750887d2008-12-25 13:38:37 +01001562
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001563 /* Skip synchronization if the clock is already in sync. */
1564 if (check_sync_clock())
1565 goto out_unlock;
1566
Heiko Carstens750887d2008-12-25 13:38:37 +01001567 memset(&stp_sync, 0, sizeof(stp_sync));
1568 get_online_cpus();
1569 atomic_set(&stp_sync.cpus, num_online_cpus() - 1);
1570 stop_machine(stp_sync_clock, &stp_sync, &cpu_online_map);
1571 put_online_cpus();
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001572
Martin Schwidefsky04362302009-04-14 15:36:19 +02001573 if (!check_sync_clock())
1574 /*
1575 * There is a usable clock but the synchonization failed.
1576 * Retry after a second.
1577 */
1578 mod_timer(&stp_timer, jiffies + HZ);
1579
Martin Schwidefsky0b3016b2008-12-25 13:38:38 +01001580out_unlock:
1581 mutex_unlock(&stp_work_mutex);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001582}
1583
1584/*
1585 * STP class sysfs interface functions
1586 */
1587static struct sysdev_class stp_sysclass = {
1588 .name = "stp",
1589};
1590
Andi Kleenc9be0a32010-01-05 12:47:58 +01001591static ssize_t stp_ctn_id_show(struct sysdev_class *class,
1592 struct sysdev_class_attribute *attr,
1593 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001594{
1595 if (!stp_online)
1596 return -ENODATA;
1597 return sprintf(buf, "%016llx\n",
1598 *(unsigned long long *) stp_info.ctnid);
1599}
1600
1601static SYSDEV_CLASS_ATTR(ctn_id, 0400, stp_ctn_id_show, NULL);
1602
Andi Kleenc9be0a32010-01-05 12:47:58 +01001603static ssize_t stp_ctn_type_show(struct sysdev_class *class,
1604 struct sysdev_class_attribute *attr,
1605 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001606{
1607 if (!stp_online)
1608 return -ENODATA;
1609 return sprintf(buf, "%i\n", stp_info.ctn);
1610}
1611
1612static SYSDEV_CLASS_ATTR(ctn_type, 0400, stp_ctn_type_show, NULL);
1613
Andi Kleenc9be0a32010-01-05 12:47:58 +01001614static ssize_t stp_dst_offset_show(struct sysdev_class *class,
1615 struct sysdev_class_attribute *attr,
1616 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001617{
1618 if (!stp_online || !(stp_info.vbits & 0x2000))
1619 return -ENODATA;
1620 return sprintf(buf, "%i\n", (int)(s16) stp_info.dsto);
1621}
1622
1623static SYSDEV_CLASS_ATTR(dst_offset, 0400, stp_dst_offset_show, NULL);
1624
Andi Kleenc9be0a32010-01-05 12:47:58 +01001625static ssize_t stp_leap_seconds_show(struct sysdev_class *class,
1626 struct sysdev_class_attribute *attr,
1627 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001628{
1629 if (!stp_online || !(stp_info.vbits & 0x8000))
1630 return -ENODATA;
1631 return sprintf(buf, "%i\n", (int)(s16) stp_info.leaps);
1632}
1633
1634static SYSDEV_CLASS_ATTR(leap_seconds, 0400, stp_leap_seconds_show, NULL);
1635
Andi Kleenc9be0a32010-01-05 12:47:58 +01001636static ssize_t stp_stratum_show(struct sysdev_class *class,
1637 struct sysdev_class_attribute *attr,
1638 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001639{
1640 if (!stp_online)
1641 return -ENODATA;
1642 return sprintf(buf, "%i\n", (int)(s16) stp_info.stratum);
1643}
1644
1645static SYSDEV_CLASS_ATTR(stratum, 0400, stp_stratum_show, NULL);
1646
Andi Kleenc9be0a32010-01-05 12:47:58 +01001647static ssize_t stp_time_offset_show(struct sysdev_class *class,
1648 struct sysdev_class_attribute *attr,
1649 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001650{
1651 if (!stp_online || !(stp_info.vbits & 0x0800))
1652 return -ENODATA;
1653 return sprintf(buf, "%i\n", (int) stp_info.tto);
1654}
1655
1656static SYSDEV_CLASS_ATTR(time_offset, 0400, stp_time_offset_show, NULL);
1657
Andi Kleenc9be0a32010-01-05 12:47:58 +01001658static ssize_t stp_time_zone_offset_show(struct sysdev_class *class,
1659 struct sysdev_class_attribute *attr,
1660 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001661{
1662 if (!stp_online || !(stp_info.vbits & 0x4000))
1663 return -ENODATA;
1664 return sprintf(buf, "%i\n", (int)(s16) stp_info.tzo);
1665}
1666
1667static SYSDEV_CLASS_ATTR(time_zone_offset, 0400,
1668 stp_time_zone_offset_show, NULL);
1669
Andi Kleenc9be0a32010-01-05 12:47:58 +01001670static ssize_t stp_timing_mode_show(struct sysdev_class *class,
1671 struct sysdev_class_attribute *attr,
1672 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001673{
1674 if (!stp_online)
1675 return -ENODATA;
1676 return sprintf(buf, "%i\n", stp_info.tmd);
1677}
1678
1679static SYSDEV_CLASS_ATTR(timing_mode, 0400, stp_timing_mode_show, NULL);
1680
Andi Kleenc9be0a32010-01-05 12:47:58 +01001681static ssize_t stp_timing_state_show(struct sysdev_class *class,
1682 struct sysdev_class_attribute *attr,
1683 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001684{
1685 if (!stp_online)
1686 return -ENODATA;
1687 return sprintf(buf, "%i\n", stp_info.tst);
1688}
1689
1690static SYSDEV_CLASS_ATTR(timing_state, 0400, stp_timing_state_show, NULL);
1691
Andi Kleenc9be0a32010-01-05 12:47:58 +01001692static ssize_t stp_online_show(struct sysdev_class *class,
1693 struct sysdev_class_attribute *attr,
1694 char *buf)
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001695{
1696 return sprintf(buf, "%i\n", stp_online);
1697}
1698
1699static ssize_t stp_online_store(struct sysdev_class *class,
Andi Kleenc9be0a32010-01-05 12:47:58 +01001700 struct sysdev_class_attribute *attr,
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001701 const char *buf, size_t count)
1702{
1703 unsigned int value;
1704
1705 value = simple_strtoul(buf, NULL, 0);
1706 if (value != 0 && value != 1)
1707 return -EINVAL;
1708 if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
1709 return -EOPNOTSUPP;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001710 mutex_lock(&clock_sync_mutex);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001711 stp_online = value;
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001712 if (stp_online)
1713 set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
1714 else
1715 clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
Heiko Carstens750887d2008-12-25 13:38:37 +01001716 queue_work(time_sync_wq, &stp_work);
Martin Schwidefsky8283cb42009-03-26 15:24:21 +01001717 mutex_unlock(&clock_sync_mutex);
Martin Schwidefskyd2fec592008-07-14 09:58:56 +02001718 return count;
1719}
1720
1721/*
1722 * Can't use SYSDEV_CLASS_ATTR because the attribute should be named
1723 * stp/online but attr_online already exists in this file ..
1724 */
1725static struct sysdev_class_attribute attr_stp_online = {
1726 .attr = { .name = "online", .mode = 0600 },
1727 .show = stp_online_show,
1728 .store = stp_online_store,
1729};
1730
1731static struct sysdev_class_attribute *stp_attributes[] = {
1732 &attr_ctn_id,
1733 &attr_ctn_type,
1734 &attr_dst_offset,
1735 &attr_leap_seconds,
1736 &attr_stp_online,
1737 &attr_stratum,
1738 &attr_time_offset,
1739 &attr_time_zone_offset,
1740 &attr_timing_mode,
1741 &attr_timing_state,
1742 NULL
1743};
1744
1745static int __init stp_init_sysfs(void)
1746{
1747 struct sysdev_class_attribute **attr;
1748 int rc;
1749
1750 rc = sysdev_class_register(&stp_sysclass);
1751 if (rc)
1752 goto out;
1753 for (attr = stp_attributes; *attr; attr++) {
1754 rc = sysdev_class_create_file(&stp_sysclass, *attr);
1755 if (rc)
1756 goto out_unreg;
1757 }
1758 return 0;
1759out_unreg:
1760 for (; attr >= stp_attributes; attr--)
1761 sysdev_class_remove_file(&stp_sysclass, *attr);
1762 sysdev_class_unregister(&stp_sysclass);
1763out:
1764 return rc;
1765}
1766
1767device_initcall(stp_init_sysfs);