time.c 44 KB

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