time.c 46 KB

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