time.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * arch/s390/kernel/time.c
  3. * Time of day based timer functions.
  4. *
  5. * S390 version
  6. * Copyright (C) 1999 IBM Deutschland Entwicklung GmbH, IBM Corporation
  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/delay.h>
  24. #include <linux/init.h>
  25. #include <linux/smp.h>
  26. #include <linux/types.h>
  27. #include <linux/profile.h>
  28. #include <linux/timex.h>
  29. #include <linux/notifier.h>
  30. #include <asm/uaccess.h>
  31. #include <asm/delay.h>
  32. #include <asm/s390_ext.h>
  33. #include <asm/div64.h>
  34. #include <asm/irq.h>
  35. #include <asm/timer.h>
  36. /* change this if you have some constant time drift */
  37. #define USECS_PER_JIFFY ((unsigned long) 1000000/HZ)
  38. #define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)
  39. /*
  40. * Create a small time difference between the timer interrupts
  41. * on the different cpus to avoid lock contention.
  42. */
  43. #define CPU_DEVIATION (smp_processor_id() << 12)
  44. #define TICK_SIZE tick
  45. static ext_int_info_t ext_int_info_cc;
  46. static u64 init_timer_cc;
  47. static u64 jiffies_timer_cc;
  48. static u64 xtime_cc;
  49. extern unsigned long wall_jiffies;
  50. /*
  51. * Scheduler clock - returns current time in nanosec units.
  52. */
  53. unsigned long long sched_clock(void)
  54. {
  55. return ((get_clock() - jiffies_timer_cc) * 125) >> 9;
  56. }
  57. /*
  58. * Monotonic_clock - returns # of nanoseconds passed since time_init()
  59. */
  60. unsigned long long monotonic_clock(void)
  61. {
  62. return sched_clock();
  63. }
  64. EXPORT_SYMBOL(monotonic_clock);
  65. void tod_to_timeval(__u64 todval, struct timespec *xtime)
  66. {
  67. unsigned long long sec;
  68. sec = todval >> 12;
  69. do_div(sec, 1000000);
  70. xtime->tv_sec = sec;
  71. todval -= (sec * 1000000) << 12;
  72. xtime->tv_nsec = ((todval * 1000) >> 12);
  73. }
  74. static inline unsigned long do_gettimeoffset(void)
  75. {
  76. __u64 now;
  77. now = (get_clock() - jiffies_timer_cc) >> 12;
  78. /* We require the offset from the latest update of xtime */
  79. now -= (__u64) wall_jiffies*USECS_PER_JIFFY;
  80. return (unsigned long) now;
  81. }
  82. /*
  83. * This version of gettimeofday has microsecond resolution.
  84. */
  85. void do_gettimeofday(struct timeval *tv)
  86. {
  87. unsigned long flags;
  88. unsigned long seq;
  89. unsigned long usec, sec;
  90. do {
  91. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  92. sec = xtime.tv_sec;
  93. usec = xtime.tv_nsec / 1000 + do_gettimeoffset();
  94. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  95. while (usec >= 1000000) {
  96. usec -= 1000000;
  97. sec++;
  98. }
  99. tv->tv_sec = sec;
  100. tv->tv_usec = usec;
  101. }
  102. EXPORT_SYMBOL(do_gettimeofday);
  103. int do_settimeofday(struct timespec *tv)
  104. {
  105. time_t wtm_sec, sec = tv->tv_sec;
  106. long wtm_nsec, nsec = tv->tv_nsec;
  107. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  108. return -EINVAL;
  109. write_seqlock_irq(&xtime_lock);
  110. /* This is revolting. We need to set the xtime.tv_nsec
  111. * correctly. However, the value in this location is
  112. * is value at the last tick.
  113. * Discover what correction gettimeofday
  114. * would have done, and then undo it!
  115. */
  116. nsec -= do_gettimeoffset() * 1000;
  117. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  118. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  119. set_normalized_timespec(&xtime, sec, nsec);
  120. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  121. ntp_clear();
  122. write_sequnlock_irq(&xtime_lock);
  123. clock_was_set();
  124. return 0;
  125. }
  126. EXPORT_SYMBOL(do_settimeofday);
  127. #ifdef CONFIG_PROFILING
  128. #define s390_do_profile(regs) profile_tick(CPU_PROFILING, regs)
  129. #else
  130. #define s390_do_profile(regs) do { ; } while(0)
  131. #endif /* CONFIG_PROFILING */
  132. /*
  133. * timer_interrupt() needs to keep up the real-time clock,
  134. * as well as call the "do_timer()" routine every clocktick
  135. */
  136. void account_ticks(struct pt_regs *regs)
  137. {
  138. __u64 tmp;
  139. __u32 ticks;
  140. /* Calculate how many ticks have passed. */
  141. if (S390_lowcore.int_clock < S390_lowcore.jiffy_timer) {
  142. /*
  143. * We have to program the clock comparator even if
  144. * no tick has passed. That happens if e.g. an i/o
  145. * interrupt wakes up an idle processor that has
  146. * switched off its hz timer.
  147. */
  148. tmp = S390_lowcore.jiffy_timer + CPU_DEVIATION;
  149. asm volatile ("SCKC %0" : : "m" (tmp));
  150. return;
  151. }
  152. tmp = S390_lowcore.int_clock - S390_lowcore.jiffy_timer;
  153. if (tmp >= 2*CLK_TICKS_PER_JIFFY) { /* more than two ticks ? */
  154. ticks = __div(tmp, CLK_TICKS_PER_JIFFY) + 1;
  155. S390_lowcore.jiffy_timer +=
  156. CLK_TICKS_PER_JIFFY * (__u64) ticks;
  157. } else if (tmp >= CLK_TICKS_PER_JIFFY) {
  158. ticks = 2;
  159. S390_lowcore.jiffy_timer += 2*CLK_TICKS_PER_JIFFY;
  160. } else {
  161. ticks = 1;
  162. S390_lowcore.jiffy_timer += CLK_TICKS_PER_JIFFY;
  163. }
  164. /* set clock comparator for next tick */
  165. tmp = S390_lowcore.jiffy_timer + CPU_DEVIATION;
  166. asm volatile ("SCKC %0" : : "m" (tmp));
  167. #ifdef CONFIG_SMP
  168. /*
  169. * Do not rely on the boot cpu to do the calls to do_timer.
  170. * Spread it over all cpus instead.
  171. */
  172. write_seqlock(&xtime_lock);
  173. if (S390_lowcore.jiffy_timer > xtime_cc) {
  174. __u32 xticks;
  175. tmp = S390_lowcore.jiffy_timer - xtime_cc;
  176. if (tmp >= 2*CLK_TICKS_PER_JIFFY) {
  177. xticks = __div(tmp, CLK_TICKS_PER_JIFFY);
  178. xtime_cc += (__u64) xticks * CLK_TICKS_PER_JIFFY;
  179. } else {
  180. xticks = 1;
  181. xtime_cc += CLK_TICKS_PER_JIFFY;
  182. }
  183. do_timer(xticks);
  184. }
  185. write_sequnlock(&xtime_lock);
  186. #else
  187. do_timer(ticks);
  188. #endif
  189. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  190. account_tick_vtime(current);
  191. #else
  192. while (ticks--)
  193. update_process_times(user_mode(regs));
  194. #endif
  195. s390_do_profile(regs);
  196. }
  197. #ifdef CONFIG_NO_IDLE_HZ
  198. #ifdef CONFIG_NO_IDLE_HZ_INIT
  199. int sysctl_hz_timer = 0;
  200. #else
  201. int sysctl_hz_timer = 1;
  202. #endif
  203. /*
  204. * Stop the HZ tick on the current CPU.
  205. * Only cpu_idle may call this function.
  206. */
  207. static inline void stop_hz_timer(void)
  208. {
  209. unsigned long flags;
  210. unsigned long seq, next;
  211. __u64 timer, todval;
  212. int cpu = smp_processor_id();
  213. if (sysctl_hz_timer != 0)
  214. return;
  215. cpu_set(cpu, nohz_cpu_mask);
  216. /*
  217. * Leave the clock comparator set up for the next timer
  218. * tick if either rcu or a softirq is pending.
  219. */
  220. if (rcu_needs_cpu(cpu) || local_softirq_pending()) {
  221. cpu_clear(cpu, nohz_cpu_mask);
  222. return;
  223. }
  224. /*
  225. * This cpu is going really idle. Set up the clock comparator
  226. * for the next event.
  227. */
  228. next = next_timer_interrupt();
  229. do {
  230. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  231. timer = ((__u64) next) - ((__u64) jiffies) + jiffies_64;
  232. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  233. todval = -1ULL;
  234. /* Be careful about overflows. */
  235. if (timer < (-1ULL / CLK_TICKS_PER_JIFFY)) {
  236. timer = jiffies_timer_cc + timer * CLK_TICKS_PER_JIFFY;
  237. if (timer >= jiffies_timer_cc)
  238. todval = timer;
  239. }
  240. asm volatile ("SCKC %0" : : "m" (todval));
  241. }
  242. /*
  243. * Start the HZ tick on the current CPU.
  244. * Only cpu_idle may call this function.
  245. */
  246. static inline void start_hz_timer(void)
  247. {
  248. if (!cpu_isset(smp_processor_id(), nohz_cpu_mask))
  249. return;
  250. account_ticks(task_pt_regs(current));
  251. cpu_clear(smp_processor_id(), nohz_cpu_mask);
  252. }
  253. static int nohz_idle_notify(struct notifier_block *self,
  254. unsigned long action, void *hcpu)
  255. {
  256. switch (action) {
  257. case CPU_IDLE:
  258. stop_hz_timer();
  259. break;
  260. case CPU_NOT_IDLE:
  261. start_hz_timer();
  262. break;
  263. }
  264. return NOTIFY_OK;
  265. }
  266. static struct notifier_block nohz_idle_nb = {
  267. .notifier_call = nohz_idle_notify,
  268. };
  269. void __init nohz_init(void)
  270. {
  271. if (register_idle_notifier(&nohz_idle_nb))
  272. panic("Couldn't register idle notifier");
  273. }
  274. #endif
  275. /*
  276. * Start the clock comparator on the current CPU.
  277. */
  278. void init_cpu_timer(void)
  279. {
  280. unsigned long cr0;
  281. __u64 timer;
  282. timer = jiffies_timer_cc + jiffies_64 * CLK_TICKS_PER_JIFFY;
  283. S390_lowcore.jiffy_timer = timer + CLK_TICKS_PER_JIFFY;
  284. timer += CLK_TICKS_PER_JIFFY + CPU_DEVIATION;
  285. asm volatile ("SCKC %0" : : "m" (timer));
  286. /* allow clock comparator timer interrupt */
  287. __ctl_store(cr0, 0, 0);
  288. cr0 |= 0x800;
  289. __ctl_load(cr0, 0, 0);
  290. }
  291. extern void vtime_init(void);
  292. /*
  293. * Initialize the TOD clock and the CPU timer of
  294. * the boot cpu.
  295. */
  296. void __init time_init(void)
  297. {
  298. __u64 set_time_cc;
  299. int cc;
  300. /* kick the TOD clock */
  301. asm volatile(
  302. " stck 0(%2)\n"
  303. " ipm %0\n"
  304. " srl %0,28"
  305. : "=d" (cc), "=m" (init_timer_cc)
  306. : "a" (&init_timer_cc) : "cc");
  307. switch (cc) {
  308. case 0: /* clock in set state: all is fine */
  309. break;
  310. case 1: /* clock in non-set state: FIXME */
  311. printk("time_init: TOD clock in non-set state\n");
  312. break;
  313. case 2: /* clock in error state: FIXME */
  314. printk("time_init: TOD clock in error state\n");
  315. break;
  316. case 3: /* clock in stopped or not-operational state: FIXME */
  317. printk("time_init: TOD clock stopped/non-operational\n");
  318. break;
  319. }
  320. jiffies_timer_cc = init_timer_cc - jiffies_64 * CLK_TICKS_PER_JIFFY;
  321. /* set xtime */
  322. xtime_cc = init_timer_cc + CLK_TICKS_PER_JIFFY;
  323. set_time_cc = init_timer_cc - 0x8126d60e46000000LL +
  324. (0x3c26700LL*1000000*4096);
  325. tod_to_timeval(set_time_cc, &xtime);
  326. set_normalized_timespec(&wall_to_monotonic,
  327. -xtime.tv_sec, -xtime.tv_nsec);
  328. /* request the clock comparator external interrupt */
  329. if (register_early_external_interrupt(0x1004, NULL,
  330. &ext_int_info_cc) != 0)
  331. panic("Couldn't request external interrupt 0x1004");
  332. init_cpu_timer();
  333. #ifdef CONFIG_NO_IDLE_HZ
  334. nohz_init();
  335. #endif
  336. #ifdef CONFIG_VIRT_TIMER
  337. vtime_init();
  338. #endif
  339. }