time.c 43 KB

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