time.c 45 KB

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