time.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  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, 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, 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, 0, 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, char *buf)
  988. {
  989. unsigned int online;
  990. online = (dev == &etr_port0_dev) ? etr_port0_online : etr_port1_online;
  991. return sprintf(buf, "%i\n", online);
  992. }
  993. static ssize_t etr_online_store(struct sys_device *dev,
  994. const char *buf, size_t count)
  995. {
  996. unsigned int value;
  997. value = simple_strtoul(buf, NULL, 0);
  998. if (value != 0 && value != 1)
  999. return -EINVAL;
  1000. if (!test_bit(CLOCK_SYNC_HAS_ETR, &clock_sync_flags))
  1001. return -EOPNOTSUPP;
  1002. if (dev == &etr_port0_dev) {
  1003. if (etr_port0_online == value)
  1004. return count; /* Nothing to do. */
  1005. etr_port0_online = value;
  1006. set_bit(ETR_EVENT_PORT0_CHANGE, &etr_events);
  1007. schedule_work(&etr_work);
  1008. } else {
  1009. if (etr_port1_online == value)
  1010. return count; /* Nothing to do. */
  1011. etr_port1_online = value;
  1012. set_bit(ETR_EVENT_PORT1_CHANGE, &etr_events);
  1013. schedule_work(&etr_work);
  1014. }
  1015. return count;
  1016. }
  1017. static SYSDEV_ATTR(online, 0600, etr_online_show, etr_online_store);
  1018. static ssize_t etr_stepping_control_show(struct sys_device *dev, char *buf)
  1019. {
  1020. return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
  1021. etr_eacr.e0 : etr_eacr.e1);
  1022. }
  1023. static SYSDEV_ATTR(stepping_control, 0400, etr_stepping_control_show, NULL);
  1024. static ssize_t etr_mode_code_show(struct sys_device *dev, char *buf)
  1025. {
  1026. if (!etr_port0_online && !etr_port1_online)
  1027. /* Status word is not uptodate if both ports are offline. */
  1028. return -ENODATA;
  1029. return sprintf(buf, "%i\n", (dev == &etr_port0_dev) ?
  1030. etr_port0.esw.psc0 : etr_port0.esw.psc1);
  1031. }
  1032. static SYSDEV_ATTR(state_code, 0400, etr_mode_code_show, NULL);
  1033. static ssize_t etr_untuned_show(struct sys_device *dev, char *buf)
  1034. {
  1035. struct etr_aib *aib = etr_aib_from_dev(dev);
  1036. if (!aib || !aib->slsw.v1)
  1037. return -ENODATA;
  1038. return sprintf(buf, "%i\n", aib->edf1.u);
  1039. }
  1040. static SYSDEV_ATTR(untuned, 0400, etr_untuned_show, NULL);
  1041. static ssize_t etr_network_id_show(struct sys_device *dev, char *buf)
  1042. {
  1043. struct etr_aib *aib = etr_aib_from_dev(dev);
  1044. if (!aib || !aib->slsw.v1)
  1045. return -ENODATA;
  1046. return sprintf(buf, "%i\n", aib->edf1.net_id);
  1047. }
  1048. static SYSDEV_ATTR(network, 0400, etr_network_id_show, NULL);
  1049. static ssize_t etr_id_show(struct sys_device *dev, 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.etr_id);
  1055. }
  1056. static SYSDEV_ATTR(id, 0400, etr_id_show, NULL);
  1057. static ssize_t etr_port_number_show(struct sys_device *dev, char *buf)
  1058. {
  1059. struct etr_aib *aib = etr_aib_from_dev(dev);
  1060. if (!aib || !aib->slsw.v1)
  1061. return -ENODATA;
  1062. return sprintf(buf, "%i\n", aib->edf1.etr_pn);
  1063. }
  1064. static SYSDEV_ATTR(port, 0400, etr_port_number_show, NULL);
  1065. static ssize_t etr_coupled_show(struct sys_device *dev, char *buf)
  1066. {
  1067. struct etr_aib *aib = etr_aib_from_dev(dev);
  1068. if (!aib || !aib->slsw.v3)
  1069. return -ENODATA;
  1070. return sprintf(buf, "%i\n", aib->edf3.c);
  1071. }
  1072. static SYSDEV_ATTR(coupled, 0400, etr_coupled_show, NULL);
  1073. static ssize_t etr_local_time_show(struct sys_device *dev, char *buf)
  1074. {
  1075. struct etr_aib *aib = etr_aib_from_dev(dev);
  1076. if (!aib || !aib->slsw.v3)
  1077. return -ENODATA;
  1078. return sprintf(buf, "%i\n", aib->edf3.blto);
  1079. }
  1080. static SYSDEV_ATTR(local_time, 0400, etr_local_time_show, NULL);
  1081. static ssize_t etr_utc_offset_show(struct sys_device *dev, char *buf)
  1082. {
  1083. struct etr_aib *aib = etr_aib_from_dev(dev);
  1084. if (!aib || !aib->slsw.v3)
  1085. return -ENODATA;
  1086. return sprintf(buf, "%i\n", aib->edf3.buo);
  1087. }
  1088. static SYSDEV_ATTR(utc_offset, 0400, etr_utc_offset_show, NULL);
  1089. static struct sysdev_attribute *etr_port_attributes[] = {
  1090. &attr_online,
  1091. &attr_stepping_control,
  1092. &attr_state_code,
  1093. &attr_untuned,
  1094. &attr_network,
  1095. &attr_id,
  1096. &attr_port,
  1097. &attr_coupled,
  1098. &attr_local_time,
  1099. &attr_utc_offset,
  1100. NULL
  1101. };
  1102. static int __init etr_register_port(struct sys_device *dev)
  1103. {
  1104. struct sysdev_attribute **attr;
  1105. int rc;
  1106. rc = sysdev_register(dev);
  1107. if (rc)
  1108. goto out;
  1109. for (attr = etr_port_attributes; *attr; attr++) {
  1110. rc = sysdev_create_file(dev, *attr);
  1111. if (rc)
  1112. goto out_unreg;
  1113. }
  1114. return 0;
  1115. out_unreg:
  1116. for (; attr >= etr_port_attributes; attr--)
  1117. sysdev_remove_file(dev, *attr);
  1118. sysdev_unregister(dev);
  1119. out:
  1120. return rc;
  1121. }
  1122. static void __init etr_unregister_port(struct sys_device *dev)
  1123. {
  1124. struct sysdev_attribute **attr;
  1125. for (attr = etr_port_attributes; *attr; attr++)
  1126. sysdev_remove_file(dev, *attr);
  1127. sysdev_unregister(dev);
  1128. }
  1129. static int __init etr_init_sysfs(void)
  1130. {
  1131. int rc;
  1132. rc = sysdev_class_register(&etr_sysclass);
  1133. if (rc)
  1134. goto out;
  1135. rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_port);
  1136. if (rc)
  1137. goto out_unreg_class;
  1138. rc = sysdev_class_create_file(&etr_sysclass, &attr_stepping_mode);
  1139. if (rc)
  1140. goto out_remove_stepping_port;
  1141. rc = etr_register_port(&etr_port0_dev);
  1142. if (rc)
  1143. goto out_remove_stepping_mode;
  1144. rc = etr_register_port(&etr_port1_dev);
  1145. if (rc)
  1146. goto out_remove_port0;
  1147. return 0;
  1148. out_remove_port0:
  1149. etr_unregister_port(&etr_port0_dev);
  1150. out_remove_stepping_mode:
  1151. sysdev_class_remove_file(&etr_sysclass, &attr_stepping_mode);
  1152. out_remove_stepping_port:
  1153. sysdev_class_remove_file(&etr_sysclass, &attr_stepping_port);
  1154. out_unreg_class:
  1155. sysdev_class_unregister(&etr_sysclass);
  1156. out:
  1157. return rc;
  1158. }
  1159. device_initcall(etr_init_sysfs);
  1160. /*
  1161. * Server Time Protocol (STP) code.
  1162. */
  1163. static int stp_online;
  1164. static struct stp_sstpi stp_info;
  1165. static void *stp_page;
  1166. static void stp_work_fn(struct work_struct *work);
  1167. static DECLARE_WORK(stp_work, stp_work_fn);
  1168. static int __init early_parse_stp(char *p)
  1169. {
  1170. if (strncmp(p, "off", 3) == 0)
  1171. stp_online = 0;
  1172. else if (strncmp(p, "on", 2) == 0)
  1173. stp_online = 1;
  1174. return 0;
  1175. }
  1176. early_param("stp", early_parse_stp);
  1177. /*
  1178. * Reset STP attachment.
  1179. */
  1180. static void stp_reset(void)
  1181. {
  1182. int rc;
  1183. stp_page = alloc_bootmem_pages(PAGE_SIZE);
  1184. rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000);
  1185. if (rc == 1)
  1186. set_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags);
  1187. else if (stp_online) {
  1188. printk(KERN_WARNING "Running on non STP capable machine.\n");
  1189. free_bootmem((unsigned long) stp_page, PAGE_SIZE);
  1190. stp_page = NULL;
  1191. stp_online = 0;
  1192. }
  1193. }
  1194. static int __init stp_init(void)
  1195. {
  1196. if (test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags) && stp_online)
  1197. schedule_work(&stp_work);
  1198. return 0;
  1199. }
  1200. arch_initcall(stp_init);
  1201. /*
  1202. * STP timing alert. There are three causes:
  1203. * 1) timing status change
  1204. * 2) link availability change
  1205. * 3) time control parameter change
  1206. * In all three cases we are only interested in the clock source state.
  1207. * If a STP clock source is now available use it.
  1208. */
  1209. static void stp_timing_alert(struct stp_irq_parm *intparm)
  1210. {
  1211. if (intparm->tsc || intparm->lac || intparm->tcpc)
  1212. schedule_work(&stp_work);
  1213. }
  1214. /*
  1215. * STP sync check machine check. This is called when the timing state
  1216. * changes from the synchronized state to the unsynchronized state.
  1217. * After a STP sync check the clock is not in sync. The machine check
  1218. * is broadcasted to all cpus at the same time.
  1219. */
  1220. void stp_sync_check(void)
  1221. {
  1222. if (!test_bit(CLOCK_SYNC_STP, &clock_sync_flags))
  1223. return;
  1224. disable_sync_clock(NULL);
  1225. schedule_work(&stp_work);
  1226. }
  1227. /*
  1228. * STP island condition machine check. This is called when an attached
  1229. * server attempts to communicate over an STP link and the servers
  1230. * have matching CTN ids and have a valid stratum-1 configuration
  1231. * but the configurations do not match.
  1232. */
  1233. void stp_island_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 tasklet. Check for the STP state and take over the clock
  1242. * synchronization if the STP clock source is usable.
  1243. */
  1244. static void stp_work_fn(struct work_struct *work)
  1245. {
  1246. struct clock_sync_data stp_sync;
  1247. unsigned long long old_clock, delta;
  1248. int rc;
  1249. if (!stp_online) {
  1250. chsc_sstpc(stp_page, STP_OP_CTRL, 0x0000);
  1251. return;
  1252. }
  1253. rc = chsc_sstpc(stp_page, STP_OP_CTRL, 0xb0e0);
  1254. if (rc)
  1255. return;
  1256. rc = chsc_sstpi(stp_page, &stp_info, sizeof(struct stp_sstpi));
  1257. if (rc || stp_info.c == 0)
  1258. return;
  1259. /*
  1260. * Catch all other cpus and make them wait until we have
  1261. * successfully synced the clock. smp_call_function will
  1262. * return after all other cpus are in clock_sync_cpu_start.
  1263. */
  1264. memset(&stp_sync, 0, sizeof(stp_sync));
  1265. preempt_disable();
  1266. smp_call_function(clock_sync_cpu_start, &stp_sync, 0, 0);
  1267. local_irq_disable();
  1268. enable_sync_clock();
  1269. set_bit(CLOCK_SYNC_STP, &clock_sync_flags);
  1270. if (test_and_clear_bit(CLOCK_SYNC_ETR, &clock_sync_flags))
  1271. schedule_work(&etr_work);
  1272. rc = 0;
  1273. if (stp_info.todoff[0] || stp_info.todoff[1] ||
  1274. stp_info.todoff[2] || stp_info.todoff[3] ||
  1275. stp_info.tmd != 2) {
  1276. old_clock = get_clock();
  1277. rc = chsc_sstpc(stp_page, STP_OP_SYNC, 0);
  1278. if (rc == 0) {
  1279. delta = adjust_time(old_clock, get_clock(), 0);
  1280. fixup_clock_comparator(delta);
  1281. rc = chsc_sstpi(stp_page, &stp_info,
  1282. sizeof(struct stp_sstpi));
  1283. if (rc == 0 && stp_info.tmd != 2)
  1284. rc = -EAGAIN;
  1285. }
  1286. }
  1287. if (rc) {
  1288. disable_sync_clock(NULL);
  1289. stp_sync.in_sync = -EAGAIN;
  1290. clear_bit(CLOCK_SYNC_STP, &clock_sync_flags);
  1291. if (etr_port0_online || etr_port1_online)
  1292. schedule_work(&etr_work);
  1293. } else
  1294. stp_sync.in_sync = 1;
  1295. local_irq_enable();
  1296. smp_call_function(clock_sync_cpu_end, NULL, 0, 0);
  1297. preempt_enable();
  1298. }
  1299. /*
  1300. * STP class sysfs interface functions
  1301. */
  1302. static struct sysdev_class stp_sysclass = {
  1303. .name = "stp",
  1304. };
  1305. static ssize_t stp_ctn_id_show(struct sysdev_class *class, char *buf)
  1306. {
  1307. if (!stp_online)
  1308. return -ENODATA;
  1309. return sprintf(buf, "%016llx\n",
  1310. *(unsigned long long *) stp_info.ctnid);
  1311. }
  1312. static SYSDEV_CLASS_ATTR(ctn_id, 0400, stp_ctn_id_show, NULL);
  1313. static ssize_t stp_ctn_type_show(struct sysdev_class *class, char *buf)
  1314. {
  1315. if (!stp_online)
  1316. return -ENODATA;
  1317. return sprintf(buf, "%i\n", stp_info.ctn);
  1318. }
  1319. static SYSDEV_CLASS_ATTR(ctn_type, 0400, stp_ctn_type_show, NULL);
  1320. static ssize_t stp_dst_offset_show(struct sysdev_class *class, char *buf)
  1321. {
  1322. if (!stp_online || !(stp_info.vbits & 0x2000))
  1323. return -ENODATA;
  1324. return sprintf(buf, "%i\n", (int)(s16) stp_info.dsto);
  1325. }
  1326. static SYSDEV_CLASS_ATTR(dst_offset, 0400, stp_dst_offset_show, NULL);
  1327. static ssize_t stp_leap_seconds_show(struct sysdev_class *class, char *buf)
  1328. {
  1329. if (!stp_online || !(stp_info.vbits & 0x8000))
  1330. return -ENODATA;
  1331. return sprintf(buf, "%i\n", (int)(s16) stp_info.leaps);
  1332. }
  1333. static SYSDEV_CLASS_ATTR(leap_seconds, 0400, stp_leap_seconds_show, NULL);
  1334. static ssize_t stp_stratum_show(struct sysdev_class *class, char *buf)
  1335. {
  1336. if (!stp_online)
  1337. return -ENODATA;
  1338. return sprintf(buf, "%i\n", (int)(s16) stp_info.stratum);
  1339. }
  1340. static SYSDEV_CLASS_ATTR(stratum, 0400, stp_stratum_show, NULL);
  1341. static ssize_t stp_time_offset_show(struct sysdev_class *class, char *buf)
  1342. {
  1343. if (!stp_online || !(stp_info.vbits & 0x0800))
  1344. return -ENODATA;
  1345. return sprintf(buf, "%i\n", (int) stp_info.tto);
  1346. }
  1347. static SYSDEV_CLASS_ATTR(time_offset, 0400, stp_time_offset_show, NULL);
  1348. static ssize_t stp_time_zone_offset_show(struct sysdev_class *class, char *buf)
  1349. {
  1350. if (!stp_online || !(stp_info.vbits & 0x4000))
  1351. return -ENODATA;
  1352. return sprintf(buf, "%i\n", (int)(s16) stp_info.tzo);
  1353. }
  1354. static SYSDEV_CLASS_ATTR(time_zone_offset, 0400,
  1355. stp_time_zone_offset_show, NULL);
  1356. static ssize_t stp_timing_mode_show(struct sysdev_class *class, char *buf)
  1357. {
  1358. if (!stp_online)
  1359. return -ENODATA;
  1360. return sprintf(buf, "%i\n", stp_info.tmd);
  1361. }
  1362. static SYSDEV_CLASS_ATTR(timing_mode, 0400, stp_timing_mode_show, NULL);
  1363. static ssize_t stp_timing_state_show(struct sysdev_class *class, char *buf)
  1364. {
  1365. if (!stp_online)
  1366. return -ENODATA;
  1367. return sprintf(buf, "%i\n", stp_info.tst);
  1368. }
  1369. static SYSDEV_CLASS_ATTR(timing_state, 0400, stp_timing_state_show, NULL);
  1370. static ssize_t stp_online_show(struct sysdev_class *class, char *buf)
  1371. {
  1372. return sprintf(buf, "%i\n", stp_online);
  1373. }
  1374. static ssize_t stp_online_store(struct sysdev_class *class,
  1375. const char *buf, size_t count)
  1376. {
  1377. unsigned int value;
  1378. value = simple_strtoul(buf, NULL, 0);
  1379. if (value != 0 && value != 1)
  1380. return -EINVAL;
  1381. if (!test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags))
  1382. return -EOPNOTSUPP;
  1383. stp_online = value;
  1384. schedule_work(&stp_work);
  1385. return count;
  1386. }
  1387. /*
  1388. * Can't use SYSDEV_CLASS_ATTR because the attribute should be named
  1389. * stp/online but attr_online already exists in this file ..
  1390. */
  1391. static struct sysdev_class_attribute attr_stp_online = {
  1392. .attr = { .name = "online", .mode = 0600 },
  1393. .show = stp_online_show,
  1394. .store = stp_online_store,
  1395. };
  1396. static struct sysdev_class_attribute *stp_attributes[] = {
  1397. &attr_ctn_id,
  1398. &attr_ctn_type,
  1399. &attr_dst_offset,
  1400. &attr_leap_seconds,
  1401. &attr_stp_online,
  1402. &attr_stratum,
  1403. &attr_time_offset,
  1404. &attr_time_zone_offset,
  1405. &attr_timing_mode,
  1406. &attr_timing_state,
  1407. NULL
  1408. };
  1409. static int __init stp_init_sysfs(void)
  1410. {
  1411. struct sysdev_class_attribute **attr;
  1412. int rc;
  1413. rc = sysdev_class_register(&stp_sysclass);
  1414. if (rc)
  1415. goto out;
  1416. for (attr = stp_attributes; *attr; attr++) {
  1417. rc = sysdev_class_create_file(&stp_sysclass, *attr);
  1418. if (rc)
  1419. goto out_unreg;
  1420. }
  1421. return 0;
  1422. out_unreg:
  1423. for (; attr >= stp_attributes; attr--)
  1424. sysdev_class_remove_file(&stp_sysclass, *attr);
  1425. sysdev_class_unregister(&stp_sysclass);
  1426. out:
  1427. return rc;
  1428. }
  1429. device_initcall(stp_init_sysfs);