| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *  arch/s390/kernel/time.c | 
 | 3 |  *    Time of day based timer functions. | 
 | 4 |  * | 
 | 5 |  *  S390 version | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 6 |  *    Copyright IBM Corp. 1999, 2008 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 |  *    Author(s): Hartmut Penner (hp@de.ibm.com), | 
 | 8 |  *               Martin Schwidefsky (schwidefsky@de.ibm.com), | 
 | 9 |  *               Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com) | 
 | 10 |  * | 
 | 11 |  *  Derived from "arch/i386/kernel/time.c" | 
 | 12 |  *    Copyright (C) 1991, 1992, 1995  Linus Torvalds | 
 | 13 |  */ | 
 | 14 |  | 
| Martin Schwidefsky | feab650 | 2008-12-25 13:39:38 +0100 | [diff] [blame] | 15 | #define KMSG_COMPONENT "time" | 
 | 16 | #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt | 
 | 17 |  | 
| Heiko Carstens | 052ff46 | 2011-01-05 12:47:28 +0100 | [diff] [blame] | 18 | #include <linux/kernel_stat.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/errno.h> | 
 | 20 | #include <linux/module.h> | 
 | 21 | #include <linux/sched.h> | 
 | 22 | #include <linux/kernel.h> | 
 | 23 | #include <linux/param.h> | 
 | 24 | #include <linux/string.h> | 
 | 25 | #include <linux/mm.h> | 
 | 26 | #include <linux/interrupt.h> | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 27 | #include <linux/cpu.h> | 
 | 28 | #include <linux/stop_machine.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/time.h> | 
| Ralf Baechle | 3367b99 | 2007-05-08 00:27:52 -0700 | [diff] [blame] | 30 | #include <linux/sysdev.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/delay.h> | 
 | 32 | #include <linux/init.h> | 
 | 33 | #include <linux/smp.h> | 
 | 34 | #include <linux/types.h> | 
 | 35 | #include <linux/profile.h> | 
 | 36 | #include <linux/timex.h> | 
 | 37 | #include <linux/notifier.h> | 
| Martin Schwidefsky | dc64bef | 2006-10-06 16:38:48 +0200 | [diff] [blame] | 38 | #include <linux/clocksource.h> | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 39 | #include <linux/clockchips.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 40 | #include <linux/gfp.h> | 
| Martin Schwidefsky | 860dba4 | 2011-01-05 12:47:25 +0100 | [diff] [blame] | 41 | #include <linux/kprobes.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/uaccess.h> | 
 | 43 | #include <asm/delay.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | #include <asm/div64.h> | 
| Martin Schwidefsky | b020632 | 2008-12-25 13:38:36 +0100 | [diff] [blame] | 45 | #include <asm/vdso.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <asm/irq.h> | 
| Heiko Carstens | 5a489b9 | 2006-10-06 16:38:35 +0200 | [diff] [blame] | 47 | #include <asm/irq_regs.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <asm/timer.h> | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 49 | #include <asm/etr.h> | 
| Heiko Carstens | a806170 | 2008-04-17 07:46:26 +0200 | [diff] [blame] | 50 | #include <asm/cio.h> | 
| Martin Schwidefsky | 638ad34 | 2011-10-30 15:17:13 +0100 | [diff] [blame] | 51 | #include "entry.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 |  | 
 | 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 Schwidefsky | b6112cc | 2009-04-14 15:36:28 +0200 | [diff] [blame] | 57 | u64 sched_clock_base_cc = -1;	/* Force to data section. */ | 
| Heiko Carstens | 05e7ff7 | 2009-09-11 10:28:31 +0200 | [diff] [blame] | 58 | EXPORT_SYMBOL_GPL(sched_clock_base_cc); | 
| Martin Schwidefsky | b6112cc | 2009-04-14 15:36:28 +0200 | [diff] [blame] | 59 |  | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 60 | static DEFINE_PER_CPU(struct clock_event_device, comparators); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* | 
 | 63 |  * Scheduler clock - returns current time in nanosec units. | 
 | 64 |  */ | 
| Martin Schwidefsky | 860dba4 | 2011-01-05 12:47:25 +0100 | [diff] [blame] | 65 | unsigned long long notrace __kprobes sched_clock(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { | 
| Heiko Carstens | 05e7ff7 | 2009-09-11 10:28:31 +0200 | [diff] [blame] | 67 | 	return (get_clock_monotonic() * 125) >> 9; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } | 
 | 69 |  | 
| Jan Glauber | 32f65f2 | 2006-02-01 03:06:33 -0800 | [diff] [blame] | 70 | /* | 
 | 71 |  * Monotonic_clock - returns # of nanoseconds passed since time_init() | 
 | 72 |  */ | 
 | 73 | unsigned long long monotonic_clock(void) | 
 | 74 | { | 
 | 75 | 	return sched_clock(); | 
 | 76 | } | 
 | 77 | EXPORT_SYMBOL(monotonic_clock); | 
 | 78 |  | 
| John Stultz | b1e2ba8 | 2010-03-08 12:25:19 +0100 | [diff] [blame] | 79 | void tod_to_timeval(__u64 todval, struct timespec *xt) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | { | 
 | 81 | 	unsigned long long sec; | 
 | 82 |  | 
 | 83 | 	sec = todval >> 12; | 
 | 84 | 	do_div(sec, 1000000); | 
| John Stultz | b1e2ba8 | 2010-03-08 12:25:19 +0100 | [diff] [blame] | 85 | 	xt->tv_sec = sec; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | 	todval -= (sec * 1000000) << 12; | 
| John Stultz | b1e2ba8 | 2010-03-08 12:25:19 +0100 | [diff] [blame] | 87 | 	xt->tv_nsec = ((todval * 1000) >> 12); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | } | 
| Christof Schmitt | b592e89 | 2009-08-18 15:43:31 +0200 | [diff] [blame] | 89 | EXPORT_SYMBOL(tod_to_timeval); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 |  | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 91 | void clock_comparator_work(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 93 | 	struct clock_event_device *cd; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 |  | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 95 | 	S390_lowcore.clock_comparator = -1ULL; | 
 | 96 | 	set_clock_comparator(S390_lowcore.clock_comparator); | 
 | 97 | 	cd = &__get_cpu_var(comparators); | 
 | 98 | 	cd->event_handler(cd); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } | 
 | 100 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | /* | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 102 |  * Fixup the clock comparator. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 |  */ | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 104 | static void fixup_clock_comparator(unsigned long long delta) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 106 | 	/* If nobody is waiting there's nothing to fix. */ | 
 | 107 | 	if (S390_lowcore.clock_comparator == -1ULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | 		return; | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 109 | 	S390_lowcore.clock_comparator += delta; | 
 | 110 | 	set_clock_comparator(S390_lowcore.clock_comparator); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } | 
 | 112 |  | 
| Martin Schwidefsky | 4f37a68 | 2011-08-23 15:29:44 +0200 | [diff] [blame] | 113 | static int s390_next_ktime(ktime_t expires, | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 114 | 			   struct clock_event_device *evt) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { | 
| Heiko Carstens | e35f95b | 2011-09-22 09:19:17 +0200 | [diff] [blame] | 116 | 	u64 nsecs; | 
| Martin Schwidefsky | 4f37a68 | 2011-08-23 15:29:44 +0200 | [diff] [blame] | 117 |  | 
 | 118 | 	nsecs = ktime_to_ns(ktime_sub(expires, ktime_get_monotonic_offset())); | 
 | 119 | 	do_div(nsecs, 125); | 
 | 120 | 	S390_lowcore.clock_comparator = TOD_UNIX_EPOCH + (nsecs << 9); | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 121 | 	set_clock_comparator(S390_lowcore.clock_comparator); | 
 | 122 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } | 
 | 124 |  | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 125 | static void s390_set_mode(enum clock_event_mode mode, | 
 | 126 | 			  struct clock_event_device *evt) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 128 | } | 
 | 129 |  | 
 | 130 | /* | 
 | 131 |  * Set up lowcore and control register of the current cpu to | 
 | 132 |  * enable TOD clock and clock comparator interrupts. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 |  */ | 
 | 134 | void init_cpu_timer(void) | 
 | 135 | { | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 136 | 	struct clock_event_device *cd; | 
 | 137 | 	int cpu; | 
 | 138 |  | 
 | 139 | 	S390_lowcore.clock_comparator = -1ULL; | 
 | 140 | 	set_clock_comparator(S390_lowcore.clock_comparator); | 
 | 141 |  | 
 | 142 | 	cpu = smp_processor_id(); | 
 | 143 | 	cd = &per_cpu(comparators, cpu); | 
 | 144 | 	cd->name		= "comparator"; | 
| Martin Schwidefsky | 4f37a68 | 2011-08-23 15:29:44 +0200 | [diff] [blame] | 145 | 	cd->features		= CLOCK_EVT_FEAT_ONESHOT | | 
 | 146 | 				  CLOCK_EVT_FEAT_KTIME; | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 147 | 	cd->mult		= 16777; | 
 | 148 | 	cd->shift		= 12; | 
 | 149 | 	cd->min_delta_ns	= 1; | 
 | 150 | 	cd->max_delta_ns	= LONG_MAX; | 
 | 151 | 	cd->rating		= 400; | 
| Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 152 | 	cd->cpumask		= cpumask_of(cpu); | 
| Martin Schwidefsky | 4f37a68 | 2011-08-23 15:29:44 +0200 | [diff] [blame] | 153 | 	cd->set_next_ktime	= s390_next_ktime; | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 154 | 	cd->set_mode		= s390_set_mode; | 
 | 155 |  | 
 | 156 | 	clockevents_register_device(cd); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 |  | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 158 | 	/* Enable clock comparator timer interrupt. */ | 
 | 159 | 	__ctl_set_bit(0,11); | 
 | 160 |  | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 161 | 	/* Always allow the timing alert external interrupt. */ | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 162 | 	__ctl_set_bit(0, 4); | 
 | 163 | } | 
 | 164 |  | 
| Martin Schwidefsky | f6649a7 | 2010-10-25 16:10:38 +0200 | [diff] [blame] | 165 | static void clock_comparator_interrupt(unsigned int ext_int_code, | 
 | 166 | 				       unsigned int param32, | 
 | 167 | 				       unsigned long param64) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 168 | { | 
| Heiko Carstens | 052ff46 | 2011-01-05 12:47:28 +0100 | [diff] [blame] | 169 | 	kstat_cpu(smp_processor_id()).irqs[EXTINT_CLK]++; | 
| Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 170 | 	if (S390_lowcore.clock_comparator == -1ULL) | 
 | 171 | 		set_clock_comparator(S390_lowcore.clock_comparator); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 172 | } | 
 | 173 |  | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 174 | static void etr_timing_alert(struct etr_irq_parm *); | 
 | 175 | static void stp_timing_alert(struct stp_irq_parm *); | 
 | 176 |  | 
| Martin Schwidefsky | f6649a7 | 2010-10-25 16:10:38 +0200 | [diff] [blame] | 177 | static void timing_alert_interrupt(unsigned int ext_int_code, | 
 | 178 | 				   unsigned int param32, unsigned long param64) | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 179 | { | 
| Heiko Carstens | 052ff46 | 2011-01-05 12:47:28 +0100 | [diff] [blame] | 180 | 	kstat_cpu(smp_processor_id()).irqs[EXTINT_TLA]++; | 
| Martin Schwidefsky | f6649a7 | 2010-10-25 16:10:38 +0200 | [diff] [blame] | 181 | 	if (param32 & 0x00c40000) | 
 | 182 | 		etr_timing_alert((struct etr_irq_parm *) ¶m32); | 
 | 183 | 	if (param32 & 0x00038000) | 
 | 184 | 		stp_timing_alert((struct stp_irq_parm *) ¶m32); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 185 | } | 
 | 186 |  | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 187 | static void etr_reset(void); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 188 | static void stp_reset(void); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 189 |  | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 190 | void read_persistent_clock(struct timespec *ts) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 191 | { | 
| Martin Schwidefsky | d4f587c | 2009-08-14 15:47:31 +0200 | [diff] [blame] | 192 | 	tod_to_timeval(get_clock() - TOD_UNIX_EPOCH, ts); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 |  | 
| Martin Schwidefsky | 23970e3 | 2009-08-14 15:47:32 +0200 | [diff] [blame] | 195 | void read_boot_clock(struct timespec *ts) | 
 | 196 | { | 
 | 197 | 	tod_to_timeval(sched_clock_base_cc - TOD_UNIX_EPOCH, ts); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } | 
 | 199 |  | 
| Magnus Damm | 8e19608 | 2009-04-21 12:24:00 -0700 | [diff] [blame] | 200 | static cycle_t read_tod_clock(struct clocksource *cs) | 
| Martin Schwidefsky | dc64bef | 2006-10-06 16:38:48 +0200 | [diff] [blame] | 201 | { | 
 | 202 | 	return get_clock(); | 
 | 203 | } | 
 | 204 |  | 
 | 205 | static struct clocksource clocksource_tod = { | 
 | 206 | 	.name		= "tod", | 
| Christian Borntraeger | d2cb0e6 | 2007-11-05 11:10:14 +0100 | [diff] [blame] | 207 | 	.rating		= 400, | 
| Martin Schwidefsky | dc64bef | 2006-10-06 16:38:48 +0200 | [diff] [blame] | 208 | 	.read		= read_tod_clock, | 
 | 209 | 	.mask		= -1ULL, | 
 | 210 | 	.mult		= 1000, | 
 | 211 | 	.shift		= 12, | 
| Thomas Gleixner | cc02d80 | 2007-02-16 01:27:39 -0800 | [diff] [blame] | 212 | 	.flags		= CLOCK_SOURCE_IS_CONTINUOUS, | 
| Martin Schwidefsky | dc64bef | 2006-10-06 16:38:48 +0200 | [diff] [blame] | 213 | }; | 
 | 214 |  | 
| Martin Schwidefsky | f1b8274 | 2009-08-14 15:47:21 +0200 | [diff] [blame] | 215 | struct clocksource * __init clocksource_default_clock(void) | 
 | 216 | { | 
 | 217 | 	return &clocksource_tod; | 
 | 218 | } | 
| Martin Schwidefsky | dc64bef | 2006-10-06 16:38:48 +0200 | [diff] [blame] | 219 |  | 
| John Stultz | 7615856 | 2010-07-13 17:56:23 -0700 | [diff] [blame] | 220 | void update_vsyscall(struct timespec *wall_time, struct timespec *wtm, | 
 | 221 | 			struct clocksource *clock, u32 mult) | 
| Martin Schwidefsky | b020632 | 2008-12-25 13:38:36 +0100 | [diff] [blame] | 222 | { | 
 | 223 | 	if (clock != &clocksource_tod) | 
 | 224 | 		return; | 
 | 225 |  | 
 | 226 | 	/* Make userspace gettimeofday spin until we're done. */ | 
 | 227 | 	++vdso_data->tb_update_count; | 
 | 228 | 	smp_wmb(); | 
 | 229 | 	vdso_data->xtime_tod_stamp = clock->cycle_last; | 
| John Stultz | b1e2ba8 | 2010-03-08 12:25:19 +0100 | [diff] [blame] | 230 | 	vdso_data->xtime_clock_sec = wall_time->tv_sec; | 
 | 231 | 	vdso_data->xtime_clock_nsec = wall_time->tv_nsec; | 
| John Stultz | 7615856 | 2010-07-13 17:56:23 -0700 | [diff] [blame] | 232 | 	vdso_data->wtom_clock_sec = wtm->tv_sec; | 
 | 233 | 	vdso_data->wtom_clock_nsec = wtm->tv_nsec; | 
| Hendrik Brueckner | 157a1a2 | 2010-04-22 17:17:06 +0200 | [diff] [blame] | 234 | 	vdso_data->ntp_mult = mult; | 
| Martin Schwidefsky | b020632 | 2008-12-25 13:38:36 +0100 | [diff] [blame] | 235 | 	smp_wmb(); | 
 | 236 | 	++vdso_data->tb_update_count; | 
 | 237 | } | 
 | 238 |  | 
 | 239 | extern struct timezone sys_tz; | 
 | 240 |  | 
 | 241 | void update_vsyscall_tz(void) | 
 | 242 | { | 
 | 243 | 	/* Make userspace gettimeofday spin until we're done. */ | 
 | 244 | 	++vdso_data->tb_update_count; | 
 | 245 | 	smp_wmb(); | 
 | 246 | 	vdso_data->tz_minuteswest = sys_tz.tz_minuteswest; | 
 | 247 | 	vdso_data->tz_dsttime = sys_tz.tz_dsttime; | 
 | 248 | 	smp_wmb(); | 
 | 249 | 	++vdso_data->tb_update_count; | 
 | 250 | } | 
 | 251 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | /* | 
 | 253 |  * Initialize the TOD clock and the CPU timer of | 
 | 254 |  * the boot cpu. | 
 | 255 |  */ | 
 | 256 | void __init time_init(void) | 
 | 257 | { | 
| Martin Schwidefsky | b6112cc | 2009-04-14 15:36:28 +0200 | [diff] [blame] | 258 | 	/* Reset time synchronization interfaces. */ | 
 | 259 | 	etr_reset(); | 
 | 260 | 	stp_reset(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | 	/* request the clock comparator external interrupt */ | 
| Heiko Carstens | d7d1104 | 2009-06-22 12:08:03 +0200 | [diff] [blame] | 263 | 	if (register_external_interrupt(0x1004, clock_comparator_interrupt)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 |                 panic("Couldn't request external interrupt 0x1004"); | 
 | 265 |  | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 266 | 	/* request the timing alert external interrupt */ | 
| Heiko Carstens | d7d1104 | 2009-06-22 12:08:03 +0200 | [diff] [blame] | 267 | 	if (register_external_interrupt(0x1406, timing_alert_interrupt)) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 268 | 		panic("Couldn't request external interrupt 0x1406"); | 
 | 269 |  | 
| Martin Schwidefsky | ab96e79 | 2009-04-14 15:36:29 +0200 | [diff] [blame] | 270 | 	if (clocksource_register(&clocksource_tod) != 0) | 
 | 271 | 		panic("Could not register TOD clock source"); | 
 | 272 |  | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 273 | 	/* Enable TOD clock interrupts on the boot cpu. */ | 
 | 274 | 	init_cpu_timer(); | 
| Martin Schwidefsky | ab96e79 | 2009-04-14 15:36:29 +0200 | [diff] [blame] | 275 |  | 
| Martin Schwidefsky | c185b78 | 2008-12-25 13:39:25 +0100 | [diff] [blame] | 276 | 	/* Enable cpu timer interrupts on the boot cpu. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | 	vtime_init(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | } | 
 | 279 |  | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 280 | /* | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 281 |  * The time is "clock". old is what we think the time is. | 
 | 282 |  * Adjust the value by a multiple of jiffies and add the delta to ntp. | 
 | 283 |  * "delay" is an approximation how long the synchronization took. If | 
 | 284 |  * the time correction is positive, then "delay" is subtracted from | 
 | 285 |  * the time difference and only the remaining part is passed to ntp. | 
 | 286 |  */ | 
 | 287 | static unsigned long long adjust_time(unsigned long long old, | 
 | 288 | 				      unsigned long long clock, | 
 | 289 | 				      unsigned long long delay) | 
 | 290 | { | 
 | 291 | 	unsigned long long delta, ticks; | 
 | 292 | 	struct timex adjust; | 
 | 293 |  | 
 | 294 | 	if (clock > old) { | 
 | 295 | 		/* It is later than we thought. */ | 
 | 296 | 		delta = ticks = clock - old; | 
 | 297 | 		delta = ticks = (delta < delay) ? 0 : delta - delay; | 
 | 298 | 		delta -= do_div(ticks, CLK_TICKS_PER_JIFFY); | 
 | 299 | 		adjust.offset = ticks * (1000000 / HZ); | 
 | 300 | 	} else { | 
 | 301 | 		/* It is earlier than we thought. */ | 
 | 302 | 		delta = ticks = old - clock; | 
 | 303 | 		delta -= do_div(ticks, CLK_TICKS_PER_JIFFY); | 
 | 304 | 		delta = -delta; | 
 | 305 | 		adjust.offset = -ticks * (1000000 / HZ); | 
 | 306 | 	} | 
| Christian Borntraeger | 8107d82 | 2008-11-27 11:05:56 +0100 | [diff] [blame] | 307 | 	sched_clock_base_cc += delta; | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 308 | 	if (adjust.offset != 0) { | 
| Martin Schwidefsky | feab650 | 2008-12-25 13:39:38 +0100 | [diff] [blame] | 309 | 		pr_notice("The ETR interface has adjusted the clock " | 
 | 310 | 			  "by %li microseconds\n", adjust.offset); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 311 | 		adjust.modes = ADJ_OFFSET_SINGLESHOT; | 
 | 312 | 		do_adjtimex(&adjust); | 
 | 313 | 	} | 
 | 314 | 	return delta; | 
 | 315 | } | 
 | 316 |  | 
 | 317 | static DEFINE_PER_CPU(atomic_t, clock_sync_word); | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 318 | static DEFINE_MUTEX(clock_sync_mutex); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 319 | static unsigned long clock_sync_flags; | 
 | 320 |  | 
 | 321 | #define CLOCK_SYNC_HAS_ETR	0 | 
 | 322 | #define CLOCK_SYNC_HAS_STP	1 | 
 | 323 | #define CLOCK_SYNC_ETR		2 | 
 | 324 | #define CLOCK_SYNC_STP		3 | 
 | 325 |  | 
 | 326 | /* | 
 | 327 |  * The synchronous get_clock function. It will write the current clock | 
 | 328 |  * value to the clock pointer and return 0 if the clock is in sync with | 
 | 329 |  * the external time source. If the clock mode is local it will return | 
 | 330 |  * -ENOSYS and -EAGAIN if the clock is not in sync with the external | 
 | 331 |  * reference. | 
 | 332 |  */ | 
 | 333 | int get_sync_clock(unsigned long long *clock) | 
 | 334 | { | 
 | 335 | 	atomic_t *sw_ptr; | 
 | 336 | 	unsigned int sw0, sw1; | 
 | 337 |  | 
 | 338 | 	sw_ptr = &get_cpu_var(clock_sync_word); | 
 | 339 | 	sw0 = atomic_read(sw_ptr); | 
 | 340 | 	*clock = get_clock(); | 
 | 341 | 	sw1 = atomic_read(sw_ptr); | 
| Heiko Carstens | bd119ee | 2009-12-07 12:52:14 +0100 | [diff] [blame] | 342 | 	put_cpu_var(clock_sync_word); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 343 | 	if (sw0 == sw1 && (sw0 & 0x80000000U)) | 
 | 344 | 		/* Success: time is in sync. */ | 
 | 345 | 		return 0; | 
 | 346 | 	if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags) && | 
 | 347 | 	    !test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags)) | 
 | 348 | 		return -ENOSYS; | 
 | 349 | 	if (!test_bit(CLOCK_SYNC_ETR, &clock_sync_flags) && | 
 | 350 | 	    !test_bit(CLOCK_SYNC_STP, &clock_sync_flags)) | 
 | 351 | 		return -EACCES; | 
 | 352 | 	return -EAGAIN; | 
 | 353 | } | 
 | 354 | EXPORT_SYMBOL(get_sync_clock); | 
 | 355 |  | 
 | 356 | /* | 
 | 357 |  * Make get_sync_clock return -EAGAIN. | 
 | 358 |  */ | 
 | 359 | static void disable_sync_clock(void *dummy) | 
 | 360 | { | 
 | 361 | 	atomic_t *sw_ptr = &__get_cpu_var(clock_sync_word); | 
 | 362 | 	/* | 
 | 363 | 	 * Clear the in-sync bit 2^31. All get_sync_clock calls will | 
 | 364 | 	 * fail until the sync bit is turned back on. In addition | 
 | 365 | 	 * increase the "sequence" counter to avoid the race of an | 
 | 366 | 	 * etr event and the complete recovery against get_sync_clock. | 
 | 367 | 	 */ | 
 | 368 | 	atomic_clear_mask(0x80000000, sw_ptr); | 
 | 369 | 	atomic_inc(sw_ptr); | 
 | 370 | } | 
 | 371 |  | 
 | 372 | /* | 
 | 373 |  * Make get_sync_clock return 0 again. | 
 | 374 |  * Needs to be called from a context disabled for preemption. | 
 | 375 |  */ | 
 | 376 | static void enable_sync_clock(void) | 
 | 377 | { | 
 | 378 | 	atomic_t *sw_ptr = &__get_cpu_var(clock_sync_word); | 
 | 379 | 	atomic_set_mask(0x80000000, sw_ptr); | 
 | 380 | } | 
 | 381 |  | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 382 | /* | 
 | 383 |  * Function to check if the clock is in sync. | 
 | 384 |  */ | 
 | 385 | static inline int check_sync_clock(void) | 
 | 386 | { | 
 | 387 | 	atomic_t *sw_ptr; | 
 | 388 | 	int rc; | 
 | 389 |  | 
 | 390 | 	sw_ptr = &get_cpu_var(clock_sync_word); | 
 | 391 | 	rc = (atomic_read(sw_ptr) & 0x80000000U) != 0; | 
| Heiko Carstens | bd119ee | 2009-12-07 12:52:14 +0100 | [diff] [blame] | 392 | 	put_cpu_var(clock_sync_word); | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 393 | 	return rc; | 
 | 394 | } | 
 | 395 |  | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 396 | /* Single threaded workqueue used for etr and stp sync events */ | 
 | 397 | static struct workqueue_struct *time_sync_wq; | 
 | 398 |  | 
 | 399 | static void __init time_init_wq(void) | 
 | 400 | { | 
| Heiko Carstens | 179cb81 | 2009-01-23 16:40:26 +0100 | [diff] [blame] | 401 | 	if (time_sync_wq) | 
 | 402 | 		return; | 
 | 403 | 	time_sync_wq = create_singlethread_workqueue("timesync"); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 404 | } | 
 | 405 |  | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 406 | /* | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 407 |  * External Time Reference (ETR) code. | 
 | 408 |  */ | 
 | 409 | static int etr_port0_online; | 
 | 410 | static int etr_port1_online; | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 411 | static int etr_steai_available; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 412 |  | 
 | 413 | static int __init early_parse_etr(char *p) | 
 | 414 | { | 
 | 415 | 	if (strncmp(p, "off", 3) == 0) | 
 | 416 | 		etr_port0_online = etr_port1_online = 0; | 
 | 417 | 	else if (strncmp(p, "port0", 5) == 0) | 
 | 418 | 		etr_port0_online = 1; | 
 | 419 | 	else if (strncmp(p, "port1", 5) == 0) | 
 | 420 | 		etr_port1_online = 1; | 
 | 421 | 	else if (strncmp(p, "on", 2) == 0) | 
 | 422 | 		etr_port0_online = etr_port1_online = 1; | 
 | 423 | 	return 0; | 
 | 424 | } | 
 | 425 | early_param("etr", early_parse_etr); | 
 | 426 |  | 
 | 427 | enum etr_event { | 
 | 428 | 	ETR_EVENT_PORT0_CHANGE, | 
 | 429 | 	ETR_EVENT_PORT1_CHANGE, | 
 | 430 | 	ETR_EVENT_PORT_ALERT, | 
 | 431 | 	ETR_EVENT_SYNC_CHECK, | 
 | 432 | 	ETR_EVENT_SWITCH_LOCAL, | 
 | 433 | 	ETR_EVENT_UPDATE, | 
 | 434 | }; | 
 | 435 |  | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 436 | /* | 
 | 437 |  * Valid bit combinations of the eacr register are (x = don't care): | 
 | 438 |  * e0 e1 dp p0 p1 ea es sl | 
 | 439 |  *  0  0  x  0	0  0  0  0  initial, disabled state | 
 | 440 |  *  0  0  x  0	1  1  0  0  port 1 online | 
 | 441 |  *  0  0  x  1	0  1  0  0  port 0 online | 
 | 442 |  *  0  0  x  1	1  1  0  0  both ports online | 
 | 443 |  *  0  1  x  0	1  1  0  0  port 1 online and usable, ETR or PPS mode | 
 | 444 |  *  0  1  x  0	1  1  0  1  port 1 online, usable and ETR mode | 
 | 445 |  *  0  1  x  0	1  1  1  0  port 1 online, usable, PPS mode, in-sync | 
 | 446 |  *  0  1  x  0	1  1  1  1  port 1 online, usable, ETR mode, in-sync | 
 | 447 |  *  0  1  x  1	1  1  0  0  both ports online, port 1 usable | 
 | 448 |  *  0  1  x  1	1  1  1  0  both ports online, port 1 usable, PPS mode, in-sync | 
 | 449 |  *  0  1  x  1	1  1  1  1  both ports online, port 1 usable, ETR mode, in-sync | 
 | 450 |  *  1  0  x  1	0  1  0  0  port 0 online and usable, ETR or PPS mode | 
 | 451 |  *  1  0  x  1	0  1  0  1  port 0 online, usable and ETR mode | 
 | 452 |  *  1  0  x  1	0  1  1  0  port 0 online, usable, PPS mode, in-sync | 
 | 453 |  *  1  0  x  1	0  1  1  1  port 0 online, usable, ETR mode, in-sync | 
 | 454 |  *  1  0  x  1	1  1  0  0  both ports online, port 0 usable | 
 | 455 |  *  1  0  x  1	1  1  1  0  both ports online, port 0 usable, PPS mode, in-sync | 
 | 456 |  *  1  0  x  1	1  1  1  1  both ports online, port 0 usable, ETR mode, in-sync | 
 | 457 |  *  1  1  x  1	1  1  1  0  both ports online & usable, ETR, in-sync | 
 | 458 |  *  1  1  x  1	1  1  1  1  both ports online & usable, ETR, in-sync | 
 | 459 |  */ | 
 | 460 | static struct etr_eacr etr_eacr; | 
 | 461 | static u64 etr_tolec;			/* time of last eacr update */ | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 462 | static struct etr_aib etr_port0; | 
 | 463 | static int etr_port0_uptodate; | 
 | 464 | static struct etr_aib etr_port1; | 
 | 465 | static int etr_port1_uptodate; | 
 | 466 | static unsigned long etr_events; | 
 | 467 | static struct timer_list etr_timer; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 468 |  | 
 | 469 | static void etr_timeout(unsigned long dummy); | 
| Martin Schwidefsky | ecdcc02 | 2007-04-27 16:01:58 +0200 | [diff] [blame] | 470 | static void etr_work_fn(struct work_struct *work); | 
| Martin Schwidefsky | 0b3016b | 2008-12-25 13:38:38 +0100 | [diff] [blame] | 471 | static DEFINE_MUTEX(etr_work_mutex); | 
| Martin Schwidefsky | ecdcc02 | 2007-04-27 16:01:58 +0200 | [diff] [blame] | 472 | static DECLARE_WORK(etr_work, etr_work_fn); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 473 |  | 
 | 474 | /* | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 475 |  * Reset ETR attachment. | 
 | 476 |  */ | 
 | 477 | static void etr_reset(void) | 
 | 478 | { | 
 | 479 | 	etr_eacr =  (struct etr_eacr) { | 
 | 480 | 		.e0 = 0, .e1 = 0, ._pad0 = 4, .dp = 0, | 
 | 481 | 		.p0 = 0, .p1 = 0, ._pad1 = 0, .ea = 0, | 
 | 482 | 		.es = 0, .sl = 0 }; | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 483 | 	if (etr_setr(&etr_eacr) == 0) { | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 484 | 		etr_tolec = get_clock(); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 485 | 		set_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags); | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 486 | 		if (etr_port0_online && etr_port1_online) | 
 | 487 | 			set_bit(CLOCK_SYNC_ETR, &clock_sync_flags); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 488 | 	} else if (etr_port0_online || etr_port1_online) { | 
| Martin Schwidefsky | feab650 | 2008-12-25 13:39:38 +0100 | [diff] [blame] | 489 | 		pr_warning("The real or virtual hardware system does " | 
 | 490 | 			   "not provide an ETR interface\n"); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 491 | 		etr_port0_online = etr_port1_online = 0; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 492 | 	} | 
 | 493 | } | 
 | 494 |  | 
| Martin Schwidefsky | ecdcc02 | 2007-04-27 16:01:58 +0200 | [diff] [blame] | 495 | static int __init etr_init(void) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 496 | { | 
 | 497 | 	struct etr_aib aib; | 
 | 498 |  | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 499 | 	if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags)) | 
| Martin Schwidefsky | ecdcc02 | 2007-04-27 16:01:58 +0200 | [diff] [blame] | 500 | 		return 0; | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 501 | 	time_init_wq(); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 502 | 	/* Check if this machine has the steai instruction. */ | 
 | 503 | 	if (etr_steai(&aib, ETR_STEAI_STEPPING_PORT) == 0) | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 504 | 		etr_steai_available = 1; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 505 | 	setup_timer(&etr_timer, etr_timeout, 0UL); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 506 | 	if (etr_port0_online) { | 
 | 507 | 		set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 508 | 		queue_work(time_sync_wq, &etr_work); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 509 | 	} | 
 | 510 | 	if (etr_port1_online) { | 
 | 511 | 		set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 512 | 		queue_work(time_sync_wq, &etr_work); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 513 | 	} | 
| Martin Schwidefsky | ecdcc02 | 2007-04-27 16:01:58 +0200 | [diff] [blame] | 514 | 	return 0; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 515 | } | 
 | 516 |  | 
| Martin Schwidefsky | ecdcc02 | 2007-04-27 16:01:58 +0200 | [diff] [blame] | 517 | arch_initcall(etr_init); | 
 | 518 |  | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 519 | /* | 
 | 520 |  * Two sorts of ETR machine checks. The architecture reads: | 
 | 521 |  * "When a machine-check niterruption occurs and if a switch-to-local or | 
 | 522 |  *  ETR-sync-check interrupt request is pending but disabled, this pending | 
 | 523 |  *  disabled interruption request is indicated and is cleared". | 
 | 524 |  * Which means that we can get etr_switch_to_local events from the machine | 
 | 525 |  * check handler although the interruption condition is disabled. Lovely.. | 
 | 526 |  */ | 
 | 527 |  | 
 | 528 | /* | 
 | 529 |  * Switch to local machine check. This is called when the last usable | 
 | 530 |  * ETR port goes inactive. After switch to local the clock is not in sync. | 
 | 531 |  */ | 
 | 532 | void etr_switch_to_local(void) | 
 | 533 | { | 
 | 534 | 	if (!etr_eacr.sl) | 
 | 535 | 		return; | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 536 | 	disable_sync_clock(NULL); | 
| Martin Schwidefsky | 33fea79 | 2010-07-27 19:29:38 +0200 | [diff] [blame] | 537 | 	if (!test_and_set_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events)) { | 
 | 538 | 		etr_eacr.es = etr_eacr.sl = 0; | 
 | 539 | 		etr_setr(&etr_eacr); | 
 | 540 | 		queue_work(time_sync_wq, &etr_work); | 
 | 541 | 	} | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 542 | } | 
 | 543 |  | 
 | 544 | /* | 
 | 545 |  * ETR sync check machine check. This is called when the ETR OTE and the | 
 | 546 |  * local clock OTE are farther apart than the ETR sync check tolerance. | 
 | 547 |  * After a ETR sync check the clock is not in sync. The machine check | 
 | 548 |  * is broadcasted to all cpus at the same time. | 
 | 549 |  */ | 
 | 550 | void etr_sync_check(void) | 
 | 551 | { | 
 | 552 | 	if (!etr_eacr.es) | 
 | 553 | 		return; | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 554 | 	disable_sync_clock(NULL); | 
| Martin Schwidefsky | 33fea79 | 2010-07-27 19:29:38 +0200 | [diff] [blame] | 555 | 	if (!test_and_set_bit(ETR_EVENT_SYNC_CHECK, &etr_events)) { | 
 | 556 | 		etr_eacr.es = 0; | 
 | 557 | 		etr_setr(&etr_eacr); | 
 | 558 | 		queue_work(time_sync_wq, &etr_work); | 
 | 559 | 	} | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 560 | } | 
 | 561 |  | 
 | 562 | /* | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 563 |  * ETR timing alert. There are two causes: | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 564 |  * 1) port state change, check the usability of the port | 
 | 565 |  * 2) port alert, one of the ETR-data-validity bits (v1-v2 bits of the | 
 | 566 |  *    sldr-status word) or ETR-data word 1 (edf1) or ETR-data word 3 (edf3) | 
 | 567 |  *    or ETR-data word 4 (edf4) has changed. | 
 | 568 |  */ | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 569 | static void etr_timing_alert(struct etr_irq_parm *intparm) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 570 | { | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 571 | 	if (intparm->pc0) | 
 | 572 | 		/* ETR port 0 state change. */ | 
 | 573 | 		set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events); | 
 | 574 | 	if (intparm->pc1) | 
 | 575 | 		/* ETR port 1 state change. */ | 
 | 576 | 		set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events); | 
 | 577 | 	if (intparm->eai) | 
 | 578 | 		/* | 
 | 579 | 		 * ETR port alert on either port 0, 1 or both. | 
 | 580 | 		 * Both ports are not up-to-date now. | 
 | 581 | 		 */ | 
 | 582 | 		set_bit(ETR_EVENT_PORT_ALERT, &etr_events); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 583 | 	queue_work(time_sync_wq, &etr_work); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 584 | } | 
 | 585 |  | 
 | 586 | static void etr_timeout(unsigned long dummy) | 
 | 587 | { | 
 | 588 | 	set_bit(ETR_EVENT_UPDATE, &etr_events); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 589 | 	queue_work(time_sync_wq, &etr_work); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 590 | } | 
 | 591 |  | 
 | 592 | /* | 
 | 593 |  * Check if the etr mode is pss. | 
 | 594 |  */ | 
 | 595 | static inline int etr_mode_is_pps(struct etr_eacr eacr) | 
 | 596 | { | 
 | 597 | 	return eacr.es && !eacr.sl; | 
 | 598 | } | 
 | 599 |  | 
 | 600 | /* | 
 | 601 |  * Check if the etr mode is etr. | 
 | 602 |  */ | 
 | 603 | static inline int etr_mode_is_etr(struct etr_eacr eacr) | 
 | 604 | { | 
 | 605 | 	return eacr.es && eacr.sl; | 
 | 606 | } | 
 | 607 |  | 
 | 608 | /* | 
 | 609 |  * Check if the port can be used for TOD synchronization. | 
 | 610 |  * For PPS mode the port has to receive OTEs. For ETR mode | 
 | 611 |  * the port has to receive OTEs, the ETR stepping bit has to | 
 | 612 |  * be zero and the validity bits for data frame 1, 2, and 3 | 
 | 613 |  * have to be 1. | 
 | 614 |  */ | 
 | 615 | static int etr_port_valid(struct etr_aib *aib, int port) | 
 | 616 | { | 
 | 617 | 	unsigned int psc; | 
 | 618 |  | 
 | 619 | 	/* Check that this port is receiving OTEs. */ | 
 | 620 | 	if (aib->tsp == 0) | 
 | 621 | 		return 0; | 
 | 622 |  | 
 | 623 | 	psc = port ? aib->esw.psc1 : aib->esw.psc0; | 
 | 624 | 	if (psc == etr_lpsc_pps_mode) | 
 | 625 | 		return 1; | 
 | 626 | 	if (psc == etr_lpsc_operational_step) | 
 | 627 | 		return !aib->esw.y && aib->slsw.v1 && | 
 | 628 | 			aib->slsw.v2 && aib->slsw.v3; | 
 | 629 | 	return 0; | 
 | 630 | } | 
 | 631 |  | 
 | 632 | /* | 
 | 633 |  * Check if two ports are on the same network. | 
 | 634 |  */ | 
 | 635 | static int etr_compare_network(struct etr_aib *aib1, struct etr_aib *aib2) | 
 | 636 | { | 
 | 637 | 	// FIXME: any other fields we have to compare? | 
 | 638 | 	return aib1->edf1.net_id == aib2->edf1.net_id; | 
 | 639 | } | 
 | 640 |  | 
 | 641 | /* | 
 | 642 |  * Wrapper for etr_stei that converts physical port states | 
 | 643 |  * to logical port states to be consistent with the output | 
 | 644 |  * of stetr (see etr_psc vs. etr_lpsc). | 
 | 645 |  */ | 
 | 646 | static void etr_steai_cv(struct etr_aib *aib, unsigned int func) | 
 | 647 | { | 
 | 648 | 	BUG_ON(etr_steai(aib, func) != 0); | 
 | 649 | 	/* Convert port state to logical port state. */ | 
 | 650 | 	if (aib->esw.psc0 == 1) | 
 | 651 | 		aib->esw.psc0 = 2; | 
 | 652 | 	else if (aib->esw.psc0 == 0 && aib->esw.p == 0) | 
 | 653 | 		aib->esw.psc0 = 1; | 
 | 654 | 	if (aib->esw.psc1 == 1) | 
 | 655 | 		aib->esw.psc1 = 2; | 
 | 656 | 	else if (aib->esw.psc1 == 0 && aib->esw.p == 1) | 
 | 657 | 		aib->esw.psc1 = 1; | 
 | 658 | } | 
 | 659 |  | 
 | 660 | /* | 
 | 661 |  * Check if the aib a2 is still connected to the same attachment as | 
 | 662 |  * aib a1, the etv values differ by one and a2 is valid. | 
 | 663 |  */ | 
 | 664 | static int etr_aib_follows(struct etr_aib *a1, struct etr_aib *a2, int p) | 
 | 665 | { | 
 | 666 | 	int state_a1, state_a2; | 
 | 667 |  | 
 | 668 | 	/* Paranoia check: e0/e1 should better be the same. */ | 
 | 669 | 	if (a1->esw.eacr.e0 != a2->esw.eacr.e0 || | 
 | 670 | 	    a1->esw.eacr.e1 != a2->esw.eacr.e1) | 
 | 671 | 		return 0; | 
 | 672 |  | 
 | 673 | 	/* Still connected to the same etr ? */ | 
 | 674 | 	state_a1 = p ? a1->esw.psc1 : a1->esw.psc0; | 
 | 675 | 	state_a2 = p ? a2->esw.psc1 : a2->esw.psc0; | 
 | 676 | 	if (state_a1 == etr_lpsc_operational_step) { | 
 | 677 | 		if (state_a2 != etr_lpsc_operational_step || | 
 | 678 | 		    a1->edf1.net_id != a2->edf1.net_id || | 
 | 679 | 		    a1->edf1.etr_id != a2->edf1.etr_id || | 
 | 680 | 		    a1->edf1.etr_pn != a2->edf1.etr_pn) | 
 | 681 | 			return 0; | 
 | 682 | 	} else if (state_a2 != etr_lpsc_pps_mode) | 
 | 683 | 		return 0; | 
 | 684 |  | 
 | 685 | 	/* The ETV value of a2 needs to be ETV of a1 + 1. */ | 
 | 686 | 	if (a1->edf2.etv + 1 != a2->edf2.etv) | 
 | 687 | 		return 0; | 
 | 688 |  | 
 | 689 | 	if (!etr_port_valid(a2, p)) | 
 | 690 | 		return 0; | 
 | 691 |  | 
 | 692 | 	return 1; | 
 | 693 | } | 
 | 694 |  | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 695 | struct clock_sync_data { | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 696 | 	atomic_t cpus; | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 697 | 	int in_sync; | 
 | 698 | 	unsigned long long fixup_cc; | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 699 | 	int etr_port; | 
 | 700 | 	struct etr_aib *etr_aib; | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 701 | }; | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 702 |  | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 703 | static void clock_sync_cpu(struct clock_sync_data *sync) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 704 | { | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 705 | 	atomic_dec(&sync->cpus); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 706 | 	enable_sync_clock(); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 707 | 	/* | 
 | 708 | 	 * This looks like a busy wait loop but it isn't. etr_sync_cpus | 
 | 709 | 	 * is called on all other cpus while the TOD clocks is stopped. | 
 | 710 | 	 * __udelay will stop the cpu on an enabled wait psw until the | 
 | 711 | 	 * TOD is running again. | 
 | 712 | 	 */ | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 713 | 	while (sync->in_sync == 0) { | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 714 | 		__udelay(1); | 
| Heiko Carstens | 6c732de | 2007-02-21 10:55:15 +0100 | [diff] [blame] | 715 | 		/* | 
 | 716 | 		 * A different cpu changes *in_sync. Therefore use | 
 | 717 | 		 * barrier() to force memory access. | 
 | 718 | 		 */ | 
 | 719 | 		barrier(); | 
 | 720 | 	} | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 721 | 	if (sync->in_sync != 1) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 722 | 		/* Didn't work. Clear per-cpu in sync bit again. */ | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 723 | 		disable_sync_clock(NULL); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 724 | 	/* | 
 | 725 | 	 * This round of TOD syncing is done. Set the clock comparator | 
 | 726 | 	 * to the next tick and let the processor continue. | 
 | 727 | 	 */ | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 728 | 	fixup_clock_comparator(sync->fixup_cc); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 729 | } | 
 | 730 |  | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 731 | /* | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 732 |  * Sync the TOD clock using the port referred to by aibp. This port | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 733 |  * has to be enabled and the other port has to be disabled. The | 
 | 734 |  * last eacr update has to be more than 1.6 seconds in the past. | 
 | 735 |  */ | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 736 | static int etr_sync_clock(void *data) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 737 | { | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 738 | 	static int first; | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 739 | 	unsigned long long clock, old_clock, delay, delta; | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 740 | 	struct clock_sync_data *etr_sync; | 
 | 741 | 	struct etr_aib *sync_port, *aib; | 
 | 742 | 	int port; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 743 | 	int rc; | 
 | 744 |  | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 745 | 	etr_sync = data; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 746 |  | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 747 | 	if (xchg(&first, 1) == 1) { | 
 | 748 | 		/* Slave */ | 
 | 749 | 		clock_sync_cpu(etr_sync); | 
 | 750 | 		return 0; | 
 | 751 | 	} | 
 | 752 |  | 
 | 753 | 	/* Wait until all other cpus entered the sync function. */ | 
 | 754 | 	while (atomic_read(&etr_sync->cpus) != 0) | 
 | 755 | 		cpu_relax(); | 
 | 756 |  | 
 | 757 | 	port = etr_sync->etr_port; | 
 | 758 | 	aib = etr_sync->etr_aib; | 
 | 759 | 	sync_port = (port == 0) ? &etr_port0 : &etr_port1; | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 760 | 	enable_sync_clock(); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 761 |  | 
 | 762 | 	/* Set clock to next OTE. */ | 
 | 763 | 	__ctl_set_bit(14, 21); | 
 | 764 | 	__ctl_set_bit(0, 29); | 
 | 765 | 	clock = ((unsigned long long) (aib->edf2.etv + 1)) << 32; | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 766 | 	old_clock = get_clock(); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 767 | 	if (set_clock(clock) == 0) { | 
 | 768 | 		__udelay(1);	/* Wait for the clock to start. */ | 
 | 769 | 		__ctl_clear_bit(0, 29); | 
 | 770 | 		__ctl_clear_bit(14, 21); | 
 | 771 | 		etr_stetr(aib); | 
 | 772 | 		/* Adjust Linux timing variables. */ | 
 | 773 | 		delay = (unsigned long long) | 
 | 774 | 			(aib->edf2.etv - sync_port->edf2.etv) << 32; | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 775 | 		delta = adjust_time(old_clock, clock, delay); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 776 | 		etr_sync->fixup_cc = delta; | 
| Heiko Carstens | 5a62b19 | 2008-04-17 07:46:25 +0200 | [diff] [blame] | 777 | 		fixup_clock_comparator(delta); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 778 | 		/* Verify that the clock is properly set. */ | 
 | 779 | 		if (!etr_aib_follows(sync_port, aib, port)) { | 
 | 780 | 			/* Didn't work. */ | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 781 | 			disable_sync_clock(NULL); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 782 | 			etr_sync->in_sync = -EAGAIN; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 783 | 			rc = -EAGAIN; | 
 | 784 | 		} else { | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 785 | 			etr_sync->in_sync = 1; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 786 | 			rc = 0; | 
 | 787 | 		} | 
 | 788 | 	} else { | 
 | 789 | 		/* Could not set the clock ?!? */ | 
 | 790 | 		__ctl_clear_bit(0, 29); | 
 | 791 | 		__ctl_clear_bit(14, 21); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 792 | 		disable_sync_clock(NULL); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 793 | 		etr_sync->in_sync = -EAGAIN; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 794 | 		rc = -EAGAIN; | 
 | 795 | 	} | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 796 | 	xchg(&first, 0); | 
 | 797 | 	return rc; | 
 | 798 | } | 
 | 799 |  | 
 | 800 | static int etr_sync_clock_stop(struct etr_aib *aib, int port) | 
 | 801 | { | 
 | 802 | 	struct clock_sync_data etr_sync; | 
 | 803 | 	struct etr_aib *sync_port; | 
 | 804 | 	int follows; | 
 | 805 | 	int rc; | 
 | 806 |  | 
 | 807 | 	/* Check if the current aib is adjacent to the sync port aib. */ | 
 | 808 | 	sync_port = (port == 0) ? &etr_port0 : &etr_port1; | 
 | 809 | 	follows = etr_aib_follows(sync_port, aib, port); | 
 | 810 | 	memcpy(sync_port, aib, sizeof(*aib)); | 
 | 811 | 	if (!follows) | 
 | 812 | 		return -EAGAIN; | 
 | 813 | 	memset(&etr_sync, 0, sizeof(etr_sync)); | 
 | 814 | 	etr_sync.etr_aib = aib; | 
 | 815 | 	etr_sync.etr_port = port; | 
 | 816 | 	get_online_cpus(); | 
 | 817 | 	atomic_set(&etr_sync.cpus, num_online_cpus() - 1); | 
| KOSAKI Motohiro | 0f1959f | 2011-05-23 10:24:36 +0200 | [diff] [blame] | 818 | 	rc = stop_machine(etr_sync_clock, &etr_sync, cpu_online_mask); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 819 | 	put_online_cpus(); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 820 | 	return rc; | 
 | 821 | } | 
 | 822 |  | 
 | 823 | /* | 
 | 824 |  * Handle the immediate effects of the different events. | 
 | 825 |  * The port change event is used for online/offline changes. | 
 | 826 |  */ | 
 | 827 | static struct etr_eacr etr_handle_events(struct etr_eacr eacr) | 
 | 828 | { | 
 | 829 | 	if (test_and_clear_bit(ETR_EVENT_SYNC_CHECK, &etr_events)) | 
 | 830 | 		eacr.es = 0; | 
 | 831 | 	if (test_and_clear_bit(ETR_EVENT_SWITCH_LOCAL, &etr_events)) | 
 | 832 | 		eacr.es = eacr.sl = 0; | 
 | 833 | 	if (test_and_clear_bit(ETR_EVENT_PORT_ALERT, &etr_events)) | 
 | 834 | 		etr_port0_uptodate = etr_port1_uptodate = 0; | 
 | 835 |  | 
 | 836 | 	if (test_and_clear_bit(ETR_EVENT_PORT0_CHANGE, &etr_events)) { | 
 | 837 | 		if (eacr.e0) | 
 | 838 | 			/* | 
 | 839 | 			 * Port change of an enabled port. We have to | 
 | 840 | 			 * assume that this can have caused an stepping | 
 | 841 | 			 * port switch. | 
 | 842 | 			 */ | 
 | 843 | 			etr_tolec = get_clock(); | 
 | 844 | 		eacr.p0 = etr_port0_online; | 
 | 845 | 		if (!eacr.p0) | 
 | 846 | 			eacr.e0 = 0; | 
 | 847 | 		etr_port0_uptodate = 0; | 
 | 848 | 	} | 
 | 849 | 	if (test_and_clear_bit(ETR_EVENT_PORT1_CHANGE, &etr_events)) { | 
 | 850 | 		if (eacr.e1) | 
 | 851 | 			/* | 
 | 852 | 			 * Port change of an enabled port. We have to | 
 | 853 | 			 * assume that this can have caused an stepping | 
 | 854 | 			 * port switch. | 
 | 855 | 			 */ | 
 | 856 | 			etr_tolec = get_clock(); | 
 | 857 | 		eacr.p1 = etr_port1_online; | 
 | 858 | 		if (!eacr.p1) | 
 | 859 | 			eacr.e1 = 0; | 
 | 860 | 		etr_port1_uptodate = 0; | 
 | 861 | 	} | 
 | 862 | 	clear_bit(ETR_EVENT_UPDATE, &etr_events); | 
 | 863 | 	return eacr; | 
 | 864 | } | 
 | 865 |  | 
 | 866 | /* | 
 | 867 |  * Set up a timer that expires after the etr_tolec + 1.6 seconds if | 
 | 868 |  * one of the ports needs an update. | 
 | 869 |  */ | 
 | 870 | static void etr_set_tolec_timeout(unsigned long long now) | 
 | 871 | { | 
 | 872 | 	unsigned long micros; | 
 | 873 |  | 
 | 874 | 	if ((!etr_eacr.p0 || etr_port0_uptodate) && | 
 | 875 | 	    (!etr_eacr.p1 || etr_port1_uptodate)) | 
 | 876 | 		return; | 
 | 877 | 	micros = (now > etr_tolec) ? ((now - etr_tolec) >> 12) : 0; | 
 | 878 | 	micros = (micros > 1600000) ? 0 : 1600000 - micros; | 
 | 879 | 	mod_timer(&etr_timer, jiffies + (micros * HZ) / 1000000 + 1); | 
 | 880 | } | 
 | 881 |  | 
 | 882 | /* | 
 | 883 |  * Set up a time that expires after 1/2 second. | 
 | 884 |  */ | 
 | 885 | static void etr_set_sync_timeout(void) | 
 | 886 | { | 
 | 887 | 	mod_timer(&etr_timer, jiffies + HZ/2); | 
 | 888 | } | 
 | 889 |  | 
 | 890 | /* | 
 | 891 |  * Update the aib information for one or both ports. | 
 | 892 |  */ | 
 | 893 | static struct etr_eacr etr_handle_update(struct etr_aib *aib, | 
 | 894 | 					 struct etr_eacr eacr) | 
 | 895 | { | 
 | 896 | 	/* With both ports disabled the aib information is useless. */ | 
 | 897 | 	if (!eacr.e0 && !eacr.e1) | 
 | 898 | 		return eacr; | 
 | 899 |  | 
| Martin Schwidefsky | ecdcc02 | 2007-04-27 16:01:58 +0200 | [diff] [blame] | 900 | 	/* Update port0 or port1 with aib stored in etr_work_fn. */ | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 901 | 	if (aib->esw.q == 0) { | 
 | 902 | 		/* Information for port 0 stored. */ | 
 | 903 | 		if (eacr.p0 && !etr_port0_uptodate) { | 
 | 904 | 			etr_port0 = *aib; | 
 | 905 | 			if (etr_port0_online) | 
 | 906 | 				etr_port0_uptodate = 1; | 
 | 907 | 		} | 
 | 908 | 	} else { | 
 | 909 | 		/* Information for port 1 stored. */ | 
 | 910 | 		if (eacr.p1 && !etr_port1_uptodate) { | 
 | 911 | 			etr_port1 = *aib; | 
 | 912 | 			if (etr_port0_online) | 
 | 913 | 				etr_port1_uptodate = 1; | 
 | 914 | 		} | 
 | 915 | 	} | 
 | 916 |  | 
 | 917 | 	/* | 
 | 918 | 	 * Do not try to get the alternate port aib if the clock | 
 | 919 | 	 * is not in sync yet. | 
 | 920 | 	 */ | 
| Martin Schwidefsky | 33fea79 | 2010-07-27 19:29:38 +0200 | [diff] [blame] | 921 | 	if (!eacr.es || !check_sync_clock()) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 922 | 		return eacr; | 
 | 923 |  | 
 | 924 | 	/* | 
 | 925 | 	 * If steai is available we can get the information about | 
 | 926 | 	 * the other port immediately. If only stetr is available the | 
 | 927 | 	 * data-port bit toggle has to be used. | 
 | 928 | 	 */ | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 929 | 	if (etr_steai_available) { | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 930 | 		if (eacr.p0 && !etr_port0_uptodate) { | 
 | 931 | 			etr_steai_cv(&etr_port0, ETR_STEAI_PORT_0); | 
 | 932 | 			etr_port0_uptodate = 1; | 
 | 933 | 		} | 
 | 934 | 		if (eacr.p1 && !etr_port1_uptodate) { | 
 | 935 | 			etr_steai_cv(&etr_port1, ETR_STEAI_PORT_1); | 
 | 936 | 			etr_port1_uptodate = 1; | 
 | 937 | 		} | 
 | 938 | 	} else { | 
 | 939 | 		/* | 
 | 940 | 		 * One port was updated above, if the other | 
 | 941 | 		 * port is not uptodate toggle dp bit. | 
 | 942 | 		 */ | 
 | 943 | 		if ((eacr.p0 && !etr_port0_uptodate) || | 
 | 944 | 		    (eacr.p1 && !etr_port1_uptodate)) | 
 | 945 | 			eacr.dp ^= 1; | 
 | 946 | 		else | 
 | 947 | 			eacr.dp = 0; | 
 | 948 | 	} | 
 | 949 | 	return eacr; | 
 | 950 | } | 
 | 951 |  | 
 | 952 | /* | 
 | 953 |  * Write new etr control register if it differs from the current one. | 
 | 954 |  * Return 1 if etr_tolec has been updated as well. | 
 | 955 |  */ | 
 | 956 | static void etr_update_eacr(struct etr_eacr eacr) | 
 | 957 | { | 
 | 958 | 	int dp_changed; | 
 | 959 |  | 
 | 960 | 	if (memcmp(&etr_eacr, &eacr, sizeof(eacr)) == 0) | 
 | 961 | 		/* No change, return. */ | 
 | 962 | 		return; | 
 | 963 | 	/* | 
 | 964 | 	 * The disable of an active port of the change of the data port | 
 | 965 | 	 * bit can/will cause a change in the data port. | 
 | 966 | 	 */ | 
 | 967 | 	dp_changed = etr_eacr.e0 > eacr.e0 || etr_eacr.e1 > eacr.e1 || | 
 | 968 | 		(etr_eacr.dp ^ eacr.dp) != 0; | 
 | 969 | 	etr_eacr = eacr; | 
 | 970 | 	etr_setr(&etr_eacr); | 
 | 971 | 	if (dp_changed) | 
 | 972 | 		etr_tolec = get_clock(); | 
 | 973 | } | 
 | 974 |  | 
 | 975 | /* | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 976 |  * ETR work. In this function you'll find the main logic. In | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 977 |  * particular this is the only function that calls etr_update_eacr(), | 
 | 978 |  * it "controls" the etr control register. | 
 | 979 |  */ | 
| Martin Schwidefsky | ecdcc02 | 2007-04-27 16:01:58 +0200 | [diff] [blame] | 980 | static void etr_work_fn(struct work_struct *work) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 981 | { | 
 | 982 | 	unsigned long long now; | 
 | 983 | 	struct etr_eacr eacr; | 
 | 984 | 	struct etr_aib aib; | 
 | 985 | 	int sync_port; | 
 | 986 |  | 
| Martin Schwidefsky | 0b3016b | 2008-12-25 13:38:38 +0100 | [diff] [blame] | 987 | 	/* prevent multiple execution. */ | 
 | 988 | 	mutex_lock(&etr_work_mutex); | 
 | 989 |  | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 990 | 	/* Create working copy of etr_eacr. */ | 
 | 991 | 	eacr = etr_eacr; | 
 | 992 |  | 
 | 993 | 	/* Check for the different events and their immediate effects. */ | 
 | 994 | 	eacr = etr_handle_events(eacr); | 
 | 995 |  | 
 | 996 | 	/* Check if ETR is supposed to be active. */ | 
 | 997 | 	eacr.ea = eacr.p0 || eacr.p1; | 
 | 998 | 	if (!eacr.ea) { | 
 | 999 | 		/* Both ports offline. Reset everything. */ | 
 | 1000 | 		eacr.dp = eacr.es = eacr.sl = 0; | 
| Ingo Molnar | 1a781a7 | 2008-07-15 21:55:59 +0200 | [diff] [blame] | 1001 | 		on_each_cpu(disable_sync_clock, NULL, 1); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1002 | 		del_timer_sync(&etr_timer); | 
 | 1003 | 		etr_update_eacr(eacr); | 
| Martin Schwidefsky | 0b3016b | 2008-12-25 13:38:38 +0100 | [diff] [blame] | 1004 | 		goto out_unlock; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1005 | 	} | 
 | 1006 |  | 
 | 1007 | 	/* Store aib to get the current ETR status word. */ | 
 | 1008 | 	BUG_ON(etr_stetr(&aib) != 0); | 
 | 1009 | 	etr_port0.esw = etr_port1.esw = aib.esw;	/* Copy status word. */ | 
 | 1010 | 	now = get_clock(); | 
 | 1011 |  | 
 | 1012 | 	/* | 
 | 1013 | 	 * Update the port information if the last stepping port change | 
 | 1014 | 	 * or data port change is older than 1.6 seconds. | 
 | 1015 | 	 */ | 
 | 1016 | 	if (now >= etr_tolec + (1600000 << 12)) | 
 | 1017 | 		eacr = etr_handle_update(&aib, eacr); | 
 | 1018 |  | 
 | 1019 | 	/* | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1020 | 	 * Select ports to enable. The preferred synchronization mode is PPS. | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1021 | 	 * If a port can be enabled depends on a number of things: | 
 | 1022 | 	 * 1) The port needs to be online and uptodate. A port is not | 
 | 1023 | 	 *    disabled just because it is not uptodate, but it is only | 
 | 1024 | 	 *    enabled if it is uptodate. | 
 | 1025 | 	 * 2) The port needs to have the same mode (pps / etr). | 
 | 1026 | 	 * 3) The port needs to be usable -> etr_port_valid() == 1 | 
 | 1027 | 	 * 4) To enable the second port the clock needs to be in sync. | 
 | 1028 | 	 * 5) If both ports are useable and are ETR ports, the network id | 
 | 1029 | 	 *    has to be the same. | 
 | 1030 | 	 * The eacr.sl bit is used to indicate etr mode vs. pps mode. | 
 | 1031 | 	 */ | 
 | 1032 | 	if (eacr.p0 && aib.esw.psc0 == etr_lpsc_pps_mode) { | 
 | 1033 | 		eacr.sl = 0; | 
 | 1034 | 		eacr.e0 = 1; | 
 | 1035 | 		if (!etr_mode_is_pps(etr_eacr)) | 
 | 1036 | 			eacr.es = 0; | 
 | 1037 | 		if (!eacr.es || !eacr.p1 || aib.esw.psc1 != etr_lpsc_pps_mode) | 
 | 1038 | 			eacr.e1 = 0; | 
 | 1039 | 		// FIXME: uptodate checks ? | 
 | 1040 | 		else if (etr_port0_uptodate && etr_port1_uptodate) | 
 | 1041 | 			eacr.e1 = 1; | 
 | 1042 | 		sync_port = (etr_port0_uptodate && | 
 | 1043 | 			     etr_port_valid(&etr_port0, 0)) ? 0 : -1; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1044 | 	} else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_pps_mode) { | 
 | 1045 | 		eacr.sl = 0; | 
 | 1046 | 		eacr.e0 = 0; | 
 | 1047 | 		eacr.e1 = 1; | 
 | 1048 | 		if (!etr_mode_is_pps(etr_eacr)) | 
 | 1049 | 			eacr.es = 0; | 
 | 1050 | 		sync_port = (etr_port1_uptodate && | 
 | 1051 | 			     etr_port_valid(&etr_port1, 1)) ? 1 : -1; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1052 | 	} else if (eacr.p0 && aib.esw.psc0 == etr_lpsc_operational_step) { | 
 | 1053 | 		eacr.sl = 1; | 
 | 1054 | 		eacr.e0 = 1; | 
 | 1055 | 		if (!etr_mode_is_etr(etr_eacr)) | 
 | 1056 | 			eacr.es = 0; | 
 | 1057 | 		if (!eacr.es || !eacr.p1 || | 
 | 1058 | 		    aib.esw.psc1 != etr_lpsc_operational_alt) | 
 | 1059 | 			eacr.e1 = 0; | 
 | 1060 | 		else if (etr_port0_uptodate && etr_port1_uptodate && | 
 | 1061 | 			 etr_compare_network(&etr_port0, &etr_port1)) | 
 | 1062 | 			eacr.e1 = 1; | 
 | 1063 | 		sync_port = (etr_port0_uptodate && | 
 | 1064 | 			     etr_port_valid(&etr_port0, 0)) ? 0 : -1; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1065 | 	} else if (eacr.p1 && aib.esw.psc1 == etr_lpsc_operational_step) { | 
 | 1066 | 		eacr.sl = 1; | 
 | 1067 | 		eacr.e0 = 0; | 
 | 1068 | 		eacr.e1 = 1; | 
 | 1069 | 		if (!etr_mode_is_etr(etr_eacr)) | 
 | 1070 | 			eacr.es = 0; | 
 | 1071 | 		sync_port = (etr_port1_uptodate && | 
 | 1072 | 			     etr_port_valid(&etr_port1, 1)) ? 1 : -1; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1073 | 	} else { | 
 | 1074 | 		/* Both ports not usable. */ | 
 | 1075 | 		eacr.es = eacr.sl = 0; | 
 | 1076 | 		sync_port = -1; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1077 | 	} | 
 | 1078 |  | 
 | 1079 | 	/* | 
 | 1080 | 	 * If the clock is in sync just update the eacr and return. | 
 | 1081 | 	 * If there is no valid sync port wait for a port update. | 
 | 1082 | 	 */ | 
| Martin Schwidefsky | 33fea79 | 2010-07-27 19:29:38 +0200 | [diff] [blame] | 1083 | 	if ((eacr.es && check_sync_clock()) || sync_port < 0) { | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1084 | 		etr_update_eacr(eacr); | 
 | 1085 | 		etr_set_tolec_timeout(now); | 
| Martin Schwidefsky | 0b3016b | 2008-12-25 13:38:38 +0100 | [diff] [blame] | 1086 | 		goto out_unlock; | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1087 | 	} | 
 | 1088 |  | 
 | 1089 | 	/* | 
 | 1090 | 	 * Prepare control register for clock syncing | 
 | 1091 | 	 * (reset data port bit, set sync check control. | 
 | 1092 | 	 */ | 
 | 1093 | 	eacr.dp = 0; | 
 | 1094 | 	eacr.es = 1; | 
 | 1095 |  | 
 | 1096 | 	/* | 
 | 1097 | 	 * Update eacr and try to synchronize the clock. If the update | 
 | 1098 | 	 * of eacr caused a stepping port switch (or if we have to | 
| Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1099 | 	 * assume that a stepping port switch has occurred) or the | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1100 | 	 * clock syncing failed, reset the sync check control bit | 
 | 1101 | 	 * and set up a timer to try again after 0.5 seconds | 
 | 1102 | 	 */ | 
 | 1103 | 	etr_update_eacr(eacr); | 
 | 1104 | 	if (now < etr_tolec + (1600000 << 12) || | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1105 | 	    etr_sync_clock_stop(&aib, sync_port) != 0) { | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1106 | 		/* Sync failed. Try again in 1/2 second. */ | 
 | 1107 | 		eacr.es = 0; | 
 | 1108 | 		etr_update_eacr(eacr); | 
 | 1109 | 		etr_set_sync_timeout(); | 
 | 1110 | 	} else | 
 | 1111 | 		etr_set_tolec_timeout(now); | 
| Martin Schwidefsky | 0b3016b | 2008-12-25 13:38:38 +0100 | [diff] [blame] | 1112 | out_unlock: | 
 | 1113 | 	mutex_unlock(&etr_work_mutex); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1114 | } | 
 | 1115 |  | 
 | 1116 | /* | 
 | 1117 |  * Sysfs interface functions | 
 | 1118 |  */ | 
 | 1119 | static struct sysdev_class etr_sysclass = { | 
| Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 1120 | 	.name	= "etr", | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1121 | }; | 
 | 1122 |  | 
 | 1123 | static struct sys_device etr_port0_dev = { | 
 | 1124 | 	.id	= 0, | 
 | 1125 | 	.cls	= &etr_sysclass, | 
 | 1126 | }; | 
 | 1127 |  | 
 | 1128 | static struct sys_device etr_port1_dev = { | 
 | 1129 | 	.id	= 1, | 
 | 1130 | 	.cls	= &etr_sysclass, | 
 | 1131 | }; | 
 | 1132 |  | 
 | 1133 | /* | 
 | 1134 |  * ETR class attributes | 
 | 1135 |  */ | 
| Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 1136 | static ssize_t etr_stepping_port_show(struct sysdev_class *class, | 
 | 1137 | 					struct sysdev_class_attribute *attr, | 
 | 1138 | 					char *buf) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1139 | { | 
 | 1140 | 	return sprintf(buf, "%i\n", etr_port0.esw.p); | 
 | 1141 | } | 
 | 1142 |  | 
 | 1143 | static SYSDEV_CLASS_ATTR(stepping_port, 0400, etr_stepping_port_show, NULL); | 
 | 1144 |  | 
| Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 1145 | static ssize_t etr_stepping_mode_show(struct sysdev_class *class, | 
 | 1146 | 				      	struct sysdev_class_attribute *attr, | 
 | 1147 | 					char *buf) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1148 | { | 
 | 1149 | 	char *mode_str; | 
 | 1150 |  | 
 | 1151 | 	if (etr_mode_is_pps(etr_eacr)) | 
 | 1152 | 		mode_str = "pps"; | 
 | 1153 | 	else if (etr_mode_is_etr(etr_eacr)) | 
 | 1154 | 		mode_str = "etr"; | 
 | 1155 | 	else | 
 | 1156 | 		mode_str = "local"; | 
 | 1157 | 	return sprintf(buf, "%s\n", mode_str); | 
 | 1158 | } | 
 | 1159 |  | 
 | 1160 | static SYSDEV_CLASS_ATTR(stepping_mode, 0400, etr_stepping_mode_show, NULL); | 
 | 1161 |  | 
 | 1162 | /* | 
 | 1163 |  * ETR port attributes | 
 | 1164 |  */ | 
 | 1165 | static inline struct etr_aib *etr_aib_from_dev(struct sys_device *dev) | 
 | 1166 | { | 
 | 1167 | 	if (dev == &etr_port0_dev) | 
 | 1168 | 		return etr_port0_online ? &etr_port0 : NULL; | 
 | 1169 | 	else | 
 | 1170 | 		return etr_port1_online ? &etr_port1 : NULL; | 
 | 1171 | } | 
 | 1172 |  | 
| Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 1173 | static ssize_t etr_online_show(struct sys_device *dev, | 
 | 1174 | 				struct sysdev_attribute *attr, | 
 | 1175 | 				char *buf) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1176 | { | 
 | 1177 | 	unsigned int online; | 
 | 1178 |  | 
 | 1179 | 	online = (dev == &etr_port0_dev) ? etr_port0_online : etr_port1_online; | 
 | 1180 | 	return sprintf(buf, "%i\n", online); | 
 | 1181 | } | 
 | 1182 |  | 
 | 1183 | static ssize_t etr_online_store(struct sys_device *dev, | 
| Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 1184 | 				struct sysdev_attribute *attr, | 
 | 1185 | 				const char *buf, size_t count) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1186 | { | 
 | 1187 | 	unsigned int value; | 
 | 1188 |  | 
 | 1189 | 	value = simple_strtoul(buf, NULL, 0); | 
 | 1190 | 	if (value != 0 && value != 1) | 
 | 1191 | 		return -EINVAL; | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1192 | 	if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags)) | 
 | 1193 | 		return -EOPNOTSUPP; | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 1194 | 	mutex_lock(&clock_sync_mutex); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1195 | 	if (dev == &etr_port0_dev) { | 
 | 1196 | 		if (etr_port0_online == value) | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 1197 | 			goto out;	/* Nothing to do. */ | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1198 | 		etr_port0_online = value; | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 1199 | 		if (etr_port0_online && etr_port1_online) | 
 | 1200 | 			set_bit(CLOCK_SYNC_ETR, &clock_sync_flags); | 
 | 1201 | 		else | 
 | 1202 | 			clear_bit(CLOCK_SYNC_ETR, &clock_sync_flags); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1203 | 		set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1204 | 		queue_work(time_sync_wq, &etr_work); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1205 | 	} else { | 
 | 1206 | 		if (etr_port1_online == value) | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 1207 | 			goto out;	/* Nothing to do. */ | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1208 | 		etr_port1_online = value; | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 1209 | 		if (etr_port0_online && etr_port1_online) | 
 | 1210 | 			set_bit(CLOCK_SYNC_ETR, &clock_sync_flags); | 
 | 1211 | 		else | 
 | 1212 | 			clear_bit(CLOCK_SYNC_ETR, &clock_sync_flags); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1213 | 		set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1214 | 		queue_work(time_sync_wq, &etr_work); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1215 | 	} | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 1216 | out: | 
 | 1217 | 	mutex_unlock(&clock_sync_mutex); | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1218 | 	return count; | 
 | 1219 | } | 
 | 1220 |  | 
 | 1221 | static SYSDEV_ATTR(online, 0600, etr_online_show, etr_online_store); | 
 | 1222 |  | 
| Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 1223 | static ssize_t etr_stepping_control_show(struct sys_device *dev, | 
 | 1224 | 					struct sysdev_attribute *attr, | 
 | 1225 | 					char *buf) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1226 | { | 
 | 1227 | 	return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ? | 
 | 1228 | 		       etr_eacr.e0 : etr_eacr.e1); | 
 | 1229 | } | 
 | 1230 |  | 
 | 1231 | static SYSDEV_ATTR(stepping_control, 0400, etr_stepping_control_show, NULL); | 
 | 1232 |  | 
| Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 1233 | static ssize_t etr_mode_code_show(struct sys_device *dev, | 
 | 1234 | 				struct sysdev_attribute *attr, char *buf) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1235 | { | 
 | 1236 | 	if (!etr_port0_online && !etr_port1_online) | 
 | 1237 | 		/* Status word is not uptodate if both ports are offline. */ | 
 | 1238 | 		return -ENODATA; | 
 | 1239 | 	return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ? | 
 | 1240 | 		       etr_port0.esw.psc0 : etr_port0.esw.psc1); | 
 | 1241 | } | 
 | 1242 |  | 
 | 1243 | static SYSDEV_ATTR(state_code, 0400, etr_mode_code_show, NULL); | 
 | 1244 |  | 
| Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 1245 | static ssize_t etr_untuned_show(struct sys_device *dev, | 
 | 1246 | 				struct sysdev_attribute *attr, char *buf) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1247 | { | 
 | 1248 | 	struct etr_aib *aib = etr_aib_from_dev(dev); | 
 | 1249 |  | 
 | 1250 | 	if (!aib || !aib->slsw.v1) | 
 | 1251 | 		return -ENODATA; | 
 | 1252 | 	return sprintf(buf, "%i\n", aib->edf1.u); | 
 | 1253 | } | 
 | 1254 |  | 
 | 1255 | static SYSDEV_ATTR(untuned, 0400, etr_untuned_show, NULL); | 
 | 1256 |  | 
| Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 1257 | static ssize_t etr_network_id_show(struct sys_device *dev, | 
 | 1258 | 				struct sysdev_attribute *attr, char *buf) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1259 | { | 
 | 1260 | 	struct etr_aib *aib = etr_aib_from_dev(dev); | 
 | 1261 |  | 
 | 1262 | 	if (!aib || !aib->slsw.v1) | 
 | 1263 | 		return -ENODATA; | 
 | 1264 | 	return sprintf(buf, "%i\n", aib->edf1.net_id); | 
 | 1265 | } | 
 | 1266 |  | 
 | 1267 | static SYSDEV_ATTR(network, 0400, etr_network_id_show, NULL); | 
 | 1268 |  | 
| Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 1269 | static ssize_t etr_id_show(struct sys_device *dev, | 
 | 1270 | 			struct sysdev_attribute *attr, char *buf) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1271 | { | 
 | 1272 | 	struct etr_aib *aib = etr_aib_from_dev(dev); | 
 | 1273 |  | 
 | 1274 | 	if (!aib || !aib->slsw.v1) | 
 | 1275 | 		return -ENODATA; | 
 | 1276 | 	return sprintf(buf, "%i\n", aib->edf1.etr_id); | 
 | 1277 | } | 
 | 1278 |  | 
 | 1279 | static SYSDEV_ATTR(id, 0400, etr_id_show, NULL); | 
 | 1280 |  | 
| Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 1281 | static ssize_t etr_port_number_show(struct sys_device *dev, | 
 | 1282 | 			struct sysdev_attribute *attr, char *buf) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1283 | { | 
 | 1284 | 	struct etr_aib *aib = etr_aib_from_dev(dev); | 
 | 1285 |  | 
 | 1286 | 	if (!aib || !aib->slsw.v1) | 
 | 1287 | 		return -ENODATA; | 
 | 1288 | 	return sprintf(buf, "%i\n", aib->edf1.etr_pn); | 
 | 1289 | } | 
 | 1290 |  | 
 | 1291 | static SYSDEV_ATTR(port, 0400, etr_port_number_show, NULL); | 
 | 1292 |  | 
| Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 1293 | static ssize_t etr_coupled_show(struct sys_device *dev, | 
 | 1294 | 			struct sysdev_attribute *attr, char *buf) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1295 | { | 
 | 1296 | 	struct etr_aib *aib = etr_aib_from_dev(dev); | 
 | 1297 |  | 
 | 1298 | 	if (!aib || !aib->slsw.v3) | 
 | 1299 | 		return -ENODATA; | 
 | 1300 | 	return sprintf(buf, "%i\n", aib->edf3.c); | 
 | 1301 | } | 
 | 1302 |  | 
 | 1303 | static SYSDEV_ATTR(coupled, 0400, etr_coupled_show, NULL); | 
 | 1304 |  | 
| Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 1305 | static ssize_t etr_local_time_show(struct sys_device *dev, | 
 | 1306 | 			struct sysdev_attribute *attr, char *buf) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1307 | { | 
 | 1308 | 	struct etr_aib *aib = etr_aib_from_dev(dev); | 
 | 1309 |  | 
 | 1310 | 	if (!aib || !aib->slsw.v3) | 
 | 1311 | 		return -ENODATA; | 
 | 1312 | 	return sprintf(buf, "%i\n", aib->edf3.blto); | 
 | 1313 | } | 
 | 1314 |  | 
 | 1315 | static SYSDEV_ATTR(local_time, 0400, etr_local_time_show, NULL); | 
 | 1316 |  | 
| Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 1317 | static ssize_t etr_utc_offset_show(struct sys_device *dev, | 
 | 1318 | 			struct sysdev_attribute *attr, char *buf) | 
| Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 1319 | { | 
 | 1320 | 	struct etr_aib *aib = etr_aib_from_dev(dev); | 
 | 1321 |  | 
 | 1322 | 	if (!aib || !aib->slsw.v3) | 
 | 1323 | 		return -ENODATA; | 
 | 1324 | 	return sprintf(buf, "%i\n", aib->edf3.buo); | 
 | 1325 | } | 
 | 1326 |  | 
 | 1327 | static SYSDEV_ATTR(utc_offset, 0400, etr_utc_offset_show, NULL); | 
 | 1328 |  | 
 | 1329 | static struct sysdev_attribute *etr_port_attributes[] = { | 
 | 1330 | 	&attr_online, | 
 | 1331 | 	&attr_stepping_control, | 
 | 1332 | 	&attr_state_code, | 
 | 1333 | 	&attr_untuned, | 
 | 1334 | 	&attr_network, | 
 | 1335 | 	&attr_id, | 
 | 1336 | 	&attr_port, | 
 | 1337 | 	&attr_coupled, | 
 | 1338 | 	&attr_local_time, | 
 | 1339 | 	&attr_utc_offset, | 
 | 1340 | 	NULL | 
 | 1341 | }; | 
 | 1342 |  | 
 | 1343 | static int __init etr_register_port(struct sys_device *dev) | 
 | 1344 | { | 
 | 1345 | 	struct sysdev_attribute **attr; | 
 | 1346 | 	int rc; | 
 | 1347 |  | 
 | 1348 | 	rc = sysdev_register(dev); | 
 | 1349 | 	if (rc) | 
 | 1350 | 		goto out; | 
 | 1351 | 	for (attr = etr_port_attributes; *attr; attr++) { | 
 | 1352 | 		rc = sysdev_create_file(dev, *attr); | 
 | 1353 | 		if (rc) | 
 | 1354 | 			goto out_unreg; | 
 | 1355 | 	} | 
 | 1356 | 	return 0; | 
 | 1357 | out_unreg: | 
 | 1358 | 	for (; attr >= etr_port_attributes; attr--) | 
 | 1359 | 		sysdev_remove_file(dev, *attr); | 
 | 1360 | 	sysdev_unregister(dev); | 
 | 1361 | out: | 
 | 1362 | 	return rc; | 
 | 1363 | } | 
 | 1364 |  | 
 | 1365 | static void __init etr_unregister_port(struct sys_device *dev) | 
 | 1366 | { | 
 | 1367 | 	struct sysdev_attribute **attr; | 
 | 1368 |  | 
 | 1369 | 	for (attr = etr_port_attributes; *attr; attr++) | 
 | 1370 | 		sysdev_remove_file(dev, *attr); | 
 | 1371 | 	sysdev_unregister(dev); | 
 | 1372 | } | 
 | 1373 |  | 
 | 1374 | static int __init etr_init_sysfs(void) | 
 | 1375 | { | 
 | 1376 | 	int rc; | 
 | 1377 |  | 
 | 1378 | 	rc = sysdev_class_register(&etr_sysclass); | 
 | 1379 | 	if (rc) | 
 | 1380 | 		goto out; | 
 | 1381 | 	rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_port); | 
 | 1382 | 	if (rc) | 
 | 1383 | 		goto out_unreg_class; | 
 | 1384 | 	rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_mode); | 
 | 1385 | 	if (rc) | 
 | 1386 | 		goto out_remove_stepping_port; | 
 | 1387 | 	rc = etr_register_port(&etr_port0_dev); | 
 | 1388 | 	if (rc) | 
 | 1389 | 		goto out_remove_stepping_mode; | 
 | 1390 | 	rc = etr_register_port(&etr_port1_dev); | 
 | 1391 | 	if (rc) | 
 | 1392 | 		goto out_remove_port0; | 
 | 1393 | 	return 0; | 
 | 1394 |  | 
 | 1395 | out_remove_port0: | 
 | 1396 | 	etr_unregister_port(&etr_port0_dev); | 
 | 1397 | out_remove_stepping_mode: | 
 | 1398 | 	sysdev_class_remove_file(&etr_sysclass, &attr_stepping_mode); | 
 | 1399 | out_remove_stepping_port: | 
 | 1400 | 	sysdev_class_remove_file(&etr_sysclass, &attr_stepping_port); | 
 | 1401 | out_unreg_class: | 
 | 1402 | 	sysdev_class_unregister(&etr_sysclass); | 
 | 1403 | out: | 
 | 1404 | 	return rc; | 
 | 1405 | } | 
 | 1406 |  | 
 | 1407 | device_initcall(etr_init_sysfs); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1408 |  | 
 | 1409 | /* | 
 | 1410 |  * Server Time Protocol (STP) code. | 
 | 1411 |  */ | 
 | 1412 | static int stp_online; | 
 | 1413 | static struct stp_sstpi stp_info; | 
 | 1414 | static void *stp_page; | 
 | 1415 |  | 
 | 1416 | static void stp_work_fn(struct work_struct *work); | 
| Martin Schwidefsky | 0b3016b | 2008-12-25 13:38:38 +0100 | [diff] [blame] | 1417 | static DEFINE_MUTEX(stp_work_mutex); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1418 | static DECLARE_WORK(stp_work, stp_work_fn); | 
| Martin Schwidefsky | 0436230 | 2009-04-14 15:36:19 +0200 | [diff] [blame] | 1419 | static struct timer_list stp_timer; | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1420 |  | 
 | 1421 | static int __init early_parse_stp(char *p) | 
 | 1422 | { | 
 | 1423 | 	if (strncmp(p, "off", 3) == 0) | 
 | 1424 | 		stp_online = 0; | 
 | 1425 | 	else if (strncmp(p, "on", 2) == 0) | 
 | 1426 | 		stp_online = 1; | 
 | 1427 | 	return 0; | 
 | 1428 | } | 
 | 1429 | early_param("stp", early_parse_stp); | 
 | 1430 |  | 
 | 1431 | /* | 
 | 1432 |  * Reset STP attachment. | 
 | 1433 |  */ | 
| Heiko Carstens | 8f84700 | 2008-08-01 16:39:19 +0200 | [diff] [blame] | 1434 | static void __init stp_reset(void) | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1435 | { | 
 | 1436 | 	int rc; | 
 | 1437 |  | 
| Heiko Carstens | d7d1104 | 2009-06-22 12:08:03 +0200 | [diff] [blame] | 1438 | 	stp_page = (void *) get_zeroed_page(GFP_ATOMIC); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1439 | 	rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000); | 
| Martin Schwidefsky | 4a672cf | 2008-10-10 21:33:29 +0200 | [diff] [blame] | 1440 | 	if (rc == 0) | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1441 | 		set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags); | 
 | 1442 | 	else if (stp_online) { | 
| Martin Schwidefsky | feab650 | 2008-12-25 13:39:38 +0100 | [diff] [blame] | 1443 | 		pr_warning("The real or virtual hardware system does " | 
 | 1444 | 			   "not provide an STP interface\n"); | 
| Heiko Carstens | d7d1104 | 2009-06-22 12:08:03 +0200 | [diff] [blame] | 1445 | 		free_page((unsigned long) stp_page); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1446 | 		stp_page = NULL; | 
 | 1447 | 		stp_online = 0; | 
 | 1448 | 	} | 
 | 1449 | } | 
 | 1450 |  | 
| Martin Schwidefsky | 0436230 | 2009-04-14 15:36:19 +0200 | [diff] [blame] | 1451 | static void stp_timeout(unsigned long dummy) | 
 | 1452 | { | 
 | 1453 | 	queue_work(time_sync_wq, &stp_work); | 
 | 1454 | } | 
 | 1455 |  | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1456 | static int __init stp_init(void) | 
 | 1457 | { | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1458 | 	if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags)) | 
 | 1459 | 		return 0; | 
| Martin Schwidefsky | 0436230 | 2009-04-14 15:36:19 +0200 | [diff] [blame] | 1460 | 	setup_timer(&stp_timer, stp_timeout, 0UL); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1461 | 	time_init_wq(); | 
 | 1462 | 	if (!stp_online) | 
 | 1463 | 		return 0; | 
 | 1464 | 	queue_work(time_sync_wq, &stp_work); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1465 | 	return 0; | 
 | 1466 | } | 
 | 1467 |  | 
 | 1468 | arch_initcall(stp_init); | 
 | 1469 |  | 
 | 1470 | /* | 
 | 1471 |  * STP timing alert. There are three causes: | 
 | 1472 |  * 1) timing status change | 
 | 1473 |  * 2) link availability change | 
 | 1474 |  * 3) time control parameter change | 
 | 1475 |  * In all three cases we are only interested in the clock source state. | 
 | 1476 |  * If a STP clock source is now available use it. | 
 | 1477 |  */ | 
 | 1478 | static void stp_timing_alert(struct stp_irq_parm *intparm) | 
 | 1479 | { | 
 | 1480 | 	if (intparm->tsc || intparm->lac || intparm->tcpc) | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1481 | 		queue_work(time_sync_wq, &stp_work); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1482 | } | 
 | 1483 |  | 
 | 1484 | /* | 
 | 1485 |  * STP sync check machine check. This is called when the timing state | 
 | 1486 |  * changes from the synchronized state to the unsynchronized state. | 
 | 1487 |  * After a STP sync check the clock is not in sync. The machine check | 
 | 1488 |  * is broadcasted to all cpus at the same time. | 
 | 1489 |  */ | 
 | 1490 | void stp_sync_check(void) | 
 | 1491 | { | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1492 | 	disable_sync_clock(NULL); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1493 | 	queue_work(time_sync_wq, &stp_work); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1494 | } | 
 | 1495 |  | 
 | 1496 | /* | 
 | 1497 |  * STP island condition machine check. This is called when an attached | 
 | 1498 |  * server  attempts to communicate over an STP link and the servers | 
 | 1499 |  * have matching CTN ids and have a valid stratum-1 configuration | 
 | 1500 |  * but the configurations do not match. | 
 | 1501 |  */ | 
 | 1502 | void stp_island_check(void) | 
 | 1503 | { | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1504 | 	disable_sync_clock(NULL); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1505 | 	queue_work(time_sync_wq, &stp_work); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1506 | } | 
 | 1507 |  | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1508 |  | 
 | 1509 | static int stp_sync_clock(void *data) | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1510 | { | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1511 | 	static int first; | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1512 | 	unsigned long long old_clock, delta; | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1513 | 	struct clock_sync_data *stp_sync; | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1514 | 	int rc; | 
 | 1515 |  | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1516 | 	stp_sync = data; | 
 | 1517 |  | 
 | 1518 | 	if (xchg(&first, 1) == 1) { | 
 | 1519 | 		/* Slave */ | 
 | 1520 | 		clock_sync_cpu(stp_sync); | 
 | 1521 | 		return 0; | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1522 | 	} | 
 | 1523 |  | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1524 | 	/* Wait until all other cpus entered the sync function. */ | 
 | 1525 | 	while (atomic_read(&stp_sync->cpus) != 0) | 
 | 1526 | 		cpu_relax(); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1527 |  | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1528 | 	enable_sync_clock(); | 
 | 1529 |  | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1530 | 	rc = 0; | 
 | 1531 | 	if (stp_info.todoff[0] || stp_info.todoff[1] || | 
 | 1532 | 	    stp_info.todoff[2] || stp_info.todoff[3] || | 
 | 1533 | 	    stp_info.tmd != 2) { | 
 | 1534 | 		old_clock = get_clock(); | 
 | 1535 | 		rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0); | 
 | 1536 | 		if (rc == 0) { | 
 | 1537 | 			delta = adjust_time(old_clock, get_clock(), 0); | 
 | 1538 | 			fixup_clock_comparator(delta); | 
 | 1539 | 			rc = chsc_sstpi(stp_page, &stp_info, | 
 | 1540 | 					sizeof(struct stp_sstpi)); | 
 | 1541 | 			if (rc == 0 && stp_info.tmd != 2) | 
 | 1542 | 				rc = -EAGAIN; | 
 | 1543 | 		} | 
 | 1544 | 	} | 
 | 1545 | 	if (rc) { | 
 | 1546 | 		disable_sync_clock(NULL); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1547 | 		stp_sync->in_sync = -EAGAIN; | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1548 | 	} else | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1549 | 		stp_sync->in_sync = 1; | 
 | 1550 | 	xchg(&first, 0); | 
 | 1551 | 	return 0; | 
 | 1552 | } | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1553 |  | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1554 | /* | 
 | 1555 |  * STP work. Check for the STP state and take over the clock | 
 | 1556 |  * synchronization if the STP clock source is usable. | 
 | 1557 |  */ | 
 | 1558 | static void stp_work_fn(struct work_struct *work) | 
 | 1559 | { | 
 | 1560 | 	struct clock_sync_data stp_sync; | 
 | 1561 | 	int rc; | 
 | 1562 |  | 
| Martin Schwidefsky | 0b3016b | 2008-12-25 13:38:38 +0100 | [diff] [blame] | 1563 | 	/* prevent multiple execution. */ | 
 | 1564 | 	mutex_lock(&stp_work_mutex); | 
 | 1565 |  | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1566 | 	if (!stp_online) { | 
 | 1567 | 		chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000); | 
| Martin Schwidefsky | 0436230 | 2009-04-14 15:36:19 +0200 | [diff] [blame] | 1568 | 		del_timer_sync(&stp_timer); | 
| Martin Schwidefsky | 0b3016b | 2008-12-25 13:38:38 +0100 | [diff] [blame] | 1569 | 		goto out_unlock; | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1570 | 	} | 
 | 1571 |  | 
 | 1572 | 	rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xb0e0); | 
 | 1573 | 	if (rc) | 
| Martin Schwidefsky | 0b3016b | 2008-12-25 13:38:38 +0100 | [diff] [blame] | 1574 | 		goto out_unlock; | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1575 |  | 
 | 1576 | 	rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi)); | 
 | 1577 | 	if (rc || stp_info.c == 0) | 
| Martin Schwidefsky | 0b3016b | 2008-12-25 13:38:38 +0100 | [diff] [blame] | 1578 | 		goto out_unlock; | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1579 |  | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 1580 | 	/* Skip synchronization if the clock is already in sync. */ | 
 | 1581 | 	if (check_sync_clock()) | 
 | 1582 | 		goto out_unlock; | 
 | 1583 |  | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1584 | 	memset(&stp_sync, 0, sizeof(stp_sync)); | 
 | 1585 | 	get_online_cpus(); | 
 | 1586 | 	atomic_set(&stp_sync.cpus, num_online_cpus() - 1); | 
| KOSAKI Motohiro | 0f1959f | 2011-05-23 10:24:36 +0200 | [diff] [blame] | 1587 | 	stop_machine(stp_sync_clock, &stp_sync, cpu_online_mask); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1588 | 	put_online_cpus(); | 
| Martin Schwidefsky | 0b3016b | 2008-12-25 13:38:38 +0100 | [diff] [blame] | 1589 |  | 
| Martin Schwidefsky | 0436230 | 2009-04-14 15:36:19 +0200 | [diff] [blame] | 1590 | 	if (!check_sync_clock()) | 
 | 1591 | 		/* | 
 | 1592 | 		 * There is a usable clock but the synchonization failed. | 
 | 1593 | 		 * Retry after a second. | 
 | 1594 | 		 */ | 
 | 1595 | 		mod_timer(&stp_timer, jiffies + HZ); | 
 | 1596 |  | 
| Martin Schwidefsky | 0b3016b | 2008-12-25 13:38:38 +0100 | [diff] [blame] | 1597 | out_unlock: | 
 | 1598 | 	mutex_unlock(&stp_work_mutex); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1599 | } | 
 | 1600 |  | 
 | 1601 | /* | 
 | 1602 |  * STP class sysfs interface functions | 
 | 1603 |  */ | 
 | 1604 | static struct sysdev_class stp_sysclass = { | 
 | 1605 | 	.name	= "stp", | 
 | 1606 | }; | 
 | 1607 |  | 
| Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 1608 | static ssize_t stp_ctn_id_show(struct sysdev_class *class, | 
 | 1609 | 				struct sysdev_class_attribute *attr, | 
 | 1610 | 				char *buf) | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1611 | { | 
 | 1612 | 	if (!stp_online) | 
 | 1613 | 		return -ENODATA; | 
 | 1614 | 	return sprintf(buf, "%016llx\n", | 
 | 1615 | 		       *(unsigned long long *) stp_info.ctnid); | 
 | 1616 | } | 
 | 1617 |  | 
 | 1618 | static SYSDEV_CLASS_ATTR(ctn_id, 0400, stp_ctn_id_show, NULL); | 
 | 1619 |  | 
| Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 1620 | static ssize_t stp_ctn_type_show(struct sysdev_class *class, | 
 | 1621 | 				struct sysdev_class_attribute *attr, | 
 | 1622 | 				char *buf) | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1623 | { | 
 | 1624 | 	if (!stp_online) | 
 | 1625 | 		return -ENODATA; | 
 | 1626 | 	return sprintf(buf, "%i\n", stp_info.ctn); | 
 | 1627 | } | 
 | 1628 |  | 
 | 1629 | static SYSDEV_CLASS_ATTR(ctn_type, 0400, stp_ctn_type_show, NULL); | 
 | 1630 |  | 
| Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 1631 | static ssize_t stp_dst_offset_show(struct sysdev_class *class, | 
 | 1632 | 				   struct sysdev_class_attribute *attr, | 
 | 1633 | 				   char *buf) | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1634 | { | 
 | 1635 | 	if (!stp_online || !(stp_info.vbits & 0x2000)) | 
 | 1636 | 		return -ENODATA; | 
 | 1637 | 	return sprintf(buf, "%i\n", (int)(s16) stp_info.dsto); | 
 | 1638 | } | 
 | 1639 |  | 
 | 1640 | static SYSDEV_CLASS_ATTR(dst_offset, 0400, stp_dst_offset_show, NULL); | 
 | 1641 |  | 
| Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 1642 | static ssize_t stp_leap_seconds_show(struct sysdev_class *class, | 
 | 1643 | 					struct sysdev_class_attribute *attr, | 
 | 1644 | 					char *buf) | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1645 | { | 
 | 1646 | 	if (!stp_online || !(stp_info.vbits & 0x8000)) | 
 | 1647 | 		return -ENODATA; | 
 | 1648 | 	return sprintf(buf, "%i\n", (int)(s16) stp_info.leaps); | 
 | 1649 | } | 
 | 1650 |  | 
 | 1651 | static SYSDEV_CLASS_ATTR(leap_seconds, 0400, stp_leap_seconds_show, NULL); | 
 | 1652 |  | 
| Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 1653 | static ssize_t stp_stratum_show(struct sysdev_class *class, | 
 | 1654 | 				struct sysdev_class_attribute *attr, | 
 | 1655 | 				char *buf) | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1656 | { | 
 | 1657 | 	if (!stp_online) | 
 | 1658 | 		return -ENODATA; | 
 | 1659 | 	return sprintf(buf, "%i\n", (int)(s16) stp_info.stratum); | 
 | 1660 | } | 
 | 1661 |  | 
 | 1662 | static SYSDEV_CLASS_ATTR(stratum, 0400, stp_stratum_show, NULL); | 
 | 1663 |  | 
| Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 1664 | static ssize_t stp_time_offset_show(struct sysdev_class *class, | 
 | 1665 | 				struct sysdev_class_attribute *attr, | 
 | 1666 | 				char *buf) | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1667 | { | 
 | 1668 | 	if (!stp_online || !(stp_info.vbits & 0x0800)) | 
 | 1669 | 		return -ENODATA; | 
 | 1670 | 	return sprintf(buf, "%i\n", (int) stp_info.tto); | 
 | 1671 | } | 
 | 1672 |  | 
 | 1673 | static SYSDEV_CLASS_ATTR(time_offset, 0400, stp_time_offset_show, NULL); | 
 | 1674 |  | 
| Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 1675 | static ssize_t stp_time_zone_offset_show(struct sysdev_class *class, | 
 | 1676 | 				struct sysdev_class_attribute *attr, | 
 | 1677 | 				char *buf) | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1678 | { | 
 | 1679 | 	if (!stp_online || !(stp_info.vbits & 0x4000)) | 
 | 1680 | 		return -ENODATA; | 
 | 1681 | 	return sprintf(buf, "%i\n", (int)(s16) stp_info.tzo); | 
 | 1682 | } | 
 | 1683 |  | 
 | 1684 | static SYSDEV_CLASS_ATTR(time_zone_offset, 0400, | 
 | 1685 | 			 stp_time_zone_offset_show, NULL); | 
 | 1686 |  | 
| Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 1687 | static ssize_t stp_timing_mode_show(struct sysdev_class *class, | 
 | 1688 | 				struct sysdev_class_attribute *attr, | 
 | 1689 | 				char *buf) | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1690 | { | 
 | 1691 | 	if (!stp_online) | 
 | 1692 | 		return -ENODATA; | 
 | 1693 | 	return sprintf(buf, "%i\n", stp_info.tmd); | 
 | 1694 | } | 
 | 1695 |  | 
 | 1696 | static SYSDEV_CLASS_ATTR(timing_mode, 0400, stp_timing_mode_show, NULL); | 
 | 1697 |  | 
| Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 1698 | static ssize_t stp_timing_state_show(struct sysdev_class *class, | 
 | 1699 | 				struct sysdev_class_attribute *attr, | 
 | 1700 | 				char *buf) | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1701 | { | 
 | 1702 | 	if (!stp_online) | 
 | 1703 | 		return -ENODATA; | 
 | 1704 | 	return sprintf(buf, "%i\n", stp_info.tst); | 
 | 1705 | } | 
 | 1706 |  | 
 | 1707 | static SYSDEV_CLASS_ATTR(timing_state, 0400, stp_timing_state_show, NULL); | 
 | 1708 |  | 
| Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 1709 | static ssize_t stp_online_show(struct sysdev_class *class, | 
 | 1710 | 				struct sysdev_class_attribute *attr, | 
 | 1711 | 				char *buf) | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1712 | { | 
 | 1713 | 	return sprintf(buf, "%i\n", stp_online); | 
 | 1714 | } | 
 | 1715 |  | 
 | 1716 | static ssize_t stp_online_store(struct sysdev_class *class, | 
| Andi Kleen | c9be0a3 | 2010-01-05 12:47:58 +0100 | [diff] [blame] | 1717 | 				struct sysdev_class_attribute *attr, | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1718 | 				const char *buf, size_t count) | 
 | 1719 | { | 
 | 1720 | 	unsigned int value; | 
 | 1721 |  | 
 | 1722 | 	value = simple_strtoul(buf, NULL, 0); | 
 | 1723 | 	if (value != 0 && value != 1) | 
 | 1724 | 		return -EINVAL; | 
 | 1725 | 	if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags)) | 
 | 1726 | 		return -EOPNOTSUPP; | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 1727 | 	mutex_lock(&clock_sync_mutex); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1728 | 	stp_online = value; | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 1729 | 	if (stp_online) | 
 | 1730 | 		set_bit(CLOCK_SYNC_STP, &clock_sync_flags); | 
 | 1731 | 	else | 
 | 1732 | 		clear_bit(CLOCK_SYNC_STP, &clock_sync_flags); | 
| Heiko Carstens | 750887d | 2008-12-25 13:38:37 +0100 | [diff] [blame] | 1733 | 	queue_work(time_sync_wq, &stp_work); | 
| Martin Schwidefsky | 8283cb4 | 2009-03-26 15:24:21 +0100 | [diff] [blame] | 1734 | 	mutex_unlock(&clock_sync_mutex); | 
| Martin Schwidefsky | d2fec59 | 2008-07-14 09:58:56 +0200 | [diff] [blame] | 1735 | 	return count; | 
 | 1736 | } | 
 | 1737 |  | 
 | 1738 | /* | 
 | 1739 |  * Can't use SYSDEV_CLASS_ATTR because the attribute should be named | 
 | 1740 |  * stp/online but attr_online already exists in this file .. | 
 | 1741 |  */ | 
 | 1742 | static struct sysdev_class_attribute attr_stp_online = { | 
 | 1743 | 	.attr = { .name = "online", .mode = 0600 }, | 
 | 1744 | 	.show	= stp_online_show, | 
 | 1745 | 	.store	= stp_online_store, | 
 | 1746 | }; | 
 | 1747 |  | 
 | 1748 | static struct sysdev_class_attribute *stp_attributes[] = { | 
 | 1749 | 	&attr_ctn_id, | 
 | 1750 | 	&attr_ctn_type, | 
 | 1751 | 	&attr_dst_offset, | 
 | 1752 | 	&attr_leap_seconds, | 
 | 1753 | 	&attr_stp_online, | 
 | 1754 | 	&attr_stratum, | 
 | 1755 | 	&attr_time_offset, | 
 | 1756 | 	&attr_time_zone_offset, | 
 | 1757 | 	&attr_timing_mode, | 
 | 1758 | 	&attr_timing_state, | 
 | 1759 | 	NULL | 
 | 1760 | }; | 
 | 1761 |  | 
 | 1762 | static int __init stp_init_sysfs(void) | 
 | 1763 | { | 
 | 1764 | 	struct sysdev_class_attribute **attr; | 
 | 1765 | 	int rc; | 
 | 1766 |  | 
 | 1767 | 	rc = sysdev_class_register(&stp_sysclass); | 
 | 1768 | 	if (rc) | 
 | 1769 | 		goto out; | 
 | 1770 | 	for (attr = stp_attributes; *attr; attr++) { | 
 | 1771 | 		rc = sysdev_class_create_file(&stp_sysclass, *attr); | 
 | 1772 | 		if (rc) | 
 | 1773 | 			goto out_unreg; | 
 | 1774 | 	} | 
 | 1775 | 	return 0; | 
 | 1776 | out_unreg: | 
 | 1777 | 	for (; attr >= stp_attributes; attr--) | 
 | 1778 | 		sysdev_class_remove_file(&stp_sysclass, *attr); | 
 | 1779 | 	sysdev_class_unregister(&stp_sysclass); | 
 | 1780 | out: | 
 | 1781 | 	return rc; | 
 | 1782 | } | 
 | 1783 |  | 
 | 1784 | device_initcall(stp_init_sysfs); |