time.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * Copyright 2001 MontaVista Software Inc.
  3. * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
  4. * Copyright (c) 2003, 2004 Maciej W. Rozycki
  5. *
  6. * Common time service routines for MIPS machines. See
  7. * Documentation/mips/time.README.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/sched.h>
  18. #include <linux/param.h>
  19. #include <linux/time.h>
  20. #include <linux/timex.h>
  21. #include <linux/smp.h>
  22. #include <linux/kernel_stat.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/module.h>
  26. #include <asm/bootinfo.h>
  27. #include <asm/cache.h>
  28. #include <asm/compiler.h>
  29. #include <asm/cpu.h>
  30. #include <asm/cpu-features.h>
  31. #include <asm/div64.h>
  32. #include <asm/sections.h>
  33. #include <asm/time.h>
  34. /*
  35. * The integer part of the number of usecs per jiffy is taken from tick,
  36. * but the fractional part is not recorded, so we calculate it using the
  37. * initial value of HZ. This aids systems where tick isn't really an
  38. * integer (e.g. for HZ = 128).
  39. */
  40. #define USECS_PER_JIFFY TICK_SIZE
  41. #define USECS_PER_JIFFY_FRAC ((unsigned long)(u32)((1000000ULL << 32) / HZ))
  42. #define TICK_SIZE (tick_nsec / 1000)
  43. /*
  44. * forward reference
  45. */
  46. DEFINE_SPINLOCK(rtc_lock);
  47. /*
  48. * By default we provide the null RTC ops
  49. */
  50. static unsigned long null_rtc_get_time(void)
  51. {
  52. return mktime(2000, 1, 1, 0, 0, 0);
  53. }
  54. static int null_rtc_set_time(unsigned long sec)
  55. {
  56. return 0;
  57. }
  58. unsigned long (*rtc_mips_get_time)(void) = null_rtc_get_time;
  59. int (*rtc_mips_set_time)(unsigned long) = null_rtc_set_time;
  60. int (*rtc_mips_set_mmss)(unsigned long);
  61. /* how many counter cycles in a jiffy */
  62. static unsigned long cycles_per_jiffy __read_mostly;
  63. /* expirelo is the count value for next CPU timer interrupt */
  64. static unsigned int expirelo;
  65. /*
  66. * Null timer ack for systems not needing one (e.g. i8254).
  67. */
  68. static void null_timer_ack(void) { /* nothing */ }
  69. /*
  70. * Null high precision timer functions for systems lacking one.
  71. */
  72. static cycle_t null_hpt_read(void)
  73. {
  74. return 0;
  75. }
  76. /*
  77. * Timer ack for an R4k-compatible timer of a known frequency.
  78. */
  79. static void c0_timer_ack(void)
  80. {
  81. unsigned int count;
  82. /* Ack this timer interrupt and set the next one. */
  83. expirelo += cycles_per_jiffy;
  84. write_c0_compare(expirelo);
  85. /* Check to see if we have missed any timer interrupts. */
  86. while (((count = read_c0_count()) - expirelo) < 0x7fffffff) {
  87. /* missed_timer_count++; */
  88. expirelo = count + cycles_per_jiffy;
  89. write_c0_compare(expirelo);
  90. }
  91. }
  92. /*
  93. * High precision timer functions for a R4k-compatible timer.
  94. */
  95. static cycle_t c0_hpt_read(void)
  96. {
  97. return read_c0_count();
  98. }
  99. /* For use both as a high precision timer and an interrupt source. */
  100. static void __init c0_hpt_timer_init(void)
  101. {
  102. expirelo = read_c0_count() + cycles_per_jiffy;
  103. write_c0_compare(expirelo);
  104. }
  105. int (*mips_timer_state)(void);
  106. void (*mips_timer_ack)(void);
  107. /* last time when xtime and rtc are sync'ed up */
  108. static long last_rtc_update;
  109. /*
  110. * local_timer_interrupt() does profiling and process accounting
  111. * on a per-CPU basis.
  112. *
  113. * In UP mode, it is invoked from the (global) timer_interrupt.
  114. *
  115. * In SMP mode, it might invoked by per-CPU timer interrupt, or
  116. * a broadcasted inter-processor interrupt which itself is triggered
  117. * by the global timer interrupt.
  118. */
  119. void local_timer_interrupt(int irq, void *dev_id)
  120. {
  121. profile_tick(CPU_PROFILING);
  122. update_process_times(user_mode(get_irq_regs()));
  123. }
  124. /*
  125. * High-level timer interrupt service routines. This function
  126. * is set as irqaction->handler and is invoked through do_IRQ.
  127. */
  128. irqreturn_t timer_interrupt(int irq, void *dev_id)
  129. {
  130. write_seqlock(&xtime_lock);
  131. mips_timer_ack();
  132. /*
  133. * call the generic timer interrupt handling
  134. */
  135. do_timer(1);
  136. /*
  137. * If we have an externally synchronized Linux clock, then update
  138. * CMOS clock accordingly every ~11 minutes. rtc_mips_set_time() has to be
  139. * called as close as possible to 500 ms before the new second starts.
  140. */
  141. if (ntp_synced() &&
  142. xtime.tv_sec > last_rtc_update + 660 &&
  143. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  144. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
  145. if (rtc_mips_set_mmss(xtime.tv_sec) == 0) {
  146. last_rtc_update = xtime.tv_sec;
  147. } else {
  148. /* do it again in 60 s */
  149. last_rtc_update = xtime.tv_sec - 600;
  150. }
  151. }
  152. write_sequnlock(&xtime_lock);
  153. /*
  154. * In UP mode, we call local_timer_interrupt() to do profiling
  155. * and process accouting.
  156. *
  157. * In SMP mode, local_timer_interrupt() is invoked by appropriate
  158. * low-level local timer interrupt handler.
  159. */
  160. local_timer_interrupt(irq, dev_id);
  161. return IRQ_HANDLED;
  162. }
  163. int null_perf_irq(void)
  164. {
  165. return 0;
  166. }
  167. int (*perf_irq)(void) = null_perf_irq;
  168. EXPORT_SYMBOL(null_perf_irq);
  169. EXPORT_SYMBOL(perf_irq);
  170. asmlinkage void ll_timer_interrupt(int irq)
  171. {
  172. int r2 = cpu_has_mips_r2;
  173. irq_enter();
  174. kstat_this_cpu.irqs[irq]++;
  175. /*
  176. * Suckage alert:
  177. * Before R2 of the architecture there was no way to see if a
  178. * performance counter interrupt was pending, so we have to run the
  179. * performance counter interrupt handler anyway.
  180. */
  181. if (!r2 || (read_c0_cause() & (1 << 26)))
  182. if (perf_irq())
  183. goto out;
  184. /* we keep interrupt disabled all the time */
  185. if (!r2 || (read_c0_cause() & (1 << 30)))
  186. timer_interrupt(irq, NULL);
  187. out:
  188. irq_exit();
  189. }
  190. asmlinkage void ll_local_timer_interrupt(int irq)
  191. {
  192. irq_enter();
  193. if (smp_processor_id() != 0)
  194. kstat_this_cpu.irqs[irq]++;
  195. /* we keep interrupt disabled all the time */
  196. local_timer_interrupt(irq, NULL);
  197. irq_exit();
  198. }
  199. /*
  200. * time_init() - it does the following things.
  201. *
  202. * 1) board_time_init() -
  203. * a) (optional) set up RTC routines,
  204. * b) (optional) calibrate and set the mips_hpt_frequency
  205. * (only needed if you intended to use cpu counter as timer interrupt
  206. * source)
  207. * 2) setup xtime based on rtc_mips_get_time().
  208. * 3) calculate a couple of cached variables for later usage
  209. * 4) plat_timer_setup() -
  210. * a) (optional) over-write any choices made above by time_init().
  211. * b) machine specific code should setup the timer irqaction.
  212. * c) enable the timer interrupt
  213. */
  214. void (*board_time_init)(void);
  215. unsigned int mips_hpt_frequency;
  216. static struct irqaction timer_irqaction = {
  217. .handler = timer_interrupt,
  218. .flags = IRQF_DISABLED,
  219. .name = "timer",
  220. };
  221. static unsigned int __init calibrate_hpt(void)
  222. {
  223. cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
  224. const int loops = HZ / 10;
  225. int log_2_loops = 0;
  226. int i;
  227. /*
  228. * We want to calibrate for 0.1s, but to avoid a 64-bit
  229. * division we round the number of loops up to the nearest
  230. * power of 2.
  231. */
  232. while (loops > 1 << log_2_loops)
  233. log_2_loops++;
  234. i = 1 << log_2_loops;
  235. /*
  236. * Wait for a rising edge of the timer interrupt.
  237. */
  238. while (mips_timer_state());
  239. while (!mips_timer_state());
  240. /*
  241. * Now see how many high precision timer ticks happen
  242. * during the calculated number of periods between timer
  243. * interrupts.
  244. */
  245. hpt_start = clocksource_mips.read();
  246. do {
  247. while (mips_timer_state());
  248. while (!mips_timer_state());
  249. } while (--i);
  250. hpt_end = clocksource_mips.read();
  251. hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
  252. hz = HZ;
  253. frequency = hpt_count * hz;
  254. return frequency >> log_2_loops;
  255. }
  256. struct clocksource clocksource_mips = {
  257. .name = "MIPS",
  258. .mask = 0xffffffff,
  259. .is_continuous = 1,
  260. };
  261. static void __init init_mips_clocksource(void)
  262. {
  263. u64 temp;
  264. u32 shift;
  265. if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read)
  266. return;
  267. /* Calclate a somewhat reasonable rating value */
  268. clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
  269. /* Find a shift value */
  270. for (shift = 32; shift > 0; shift--) {
  271. temp = (u64) NSEC_PER_SEC << shift;
  272. do_div(temp, mips_hpt_frequency);
  273. if ((temp >> 32) == 0)
  274. break;
  275. }
  276. clocksource_mips.shift = shift;
  277. clocksource_mips.mult = (u32)temp;
  278. clocksource_register(&clocksource_mips);
  279. }
  280. void __init time_init(void)
  281. {
  282. if (board_time_init)
  283. board_time_init();
  284. if (!rtc_mips_set_mmss)
  285. rtc_mips_set_mmss = rtc_mips_set_time;
  286. xtime.tv_sec = rtc_mips_get_time();
  287. xtime.tv_nsec = 0;
  288. set_normalized_timespec(&wall_to_monotonic,
  289. -xtime.tv_sec, -xtime.tv_nsec);
  290. /* Choose appropriate high precision timer routines. */
  291. if (!cpu_has_counter && !clocksource_mips.read)
  292. /* No high precision timer -- sorry. */
  293. clocksource_mips.read = null_hpt_read;
  294. else if (!mips_hpt_frequency && !mips_timer_state) {
  295. /* A high precision timer of unknown frequency. */
  296. if (!clocksource_mips.read)
  297. /* No external high precision timer -- use R4k. */
  298. clocksource_mips.read = c0_hpt_read;
  299. } else {
  300. /* We know counter frequency. Or we can get it. */
  301. if (!clocksource_mips.read) {
  302. /* No external high precision timer -- use R4k. */
  303. clocksource_mips.read = c0_hpt_read;
  304. if (!mips_timer_state) {
  305. /* No external timer interrupt -- use R4k. */
  306. mips_timer_ack = c0_timer_ack;
  307. /* Calculate cache parameters. */
  308. cycles_per_jiffy =
  309. (mips_hpt_frequency + HZ / 2) / HZ;
  310. /*
  311. * This sets up the high precision
  312. * timer for the first interrupt.
  313. */
  314. c0_hpt_timer_init();
  315. }
  316. }
  317. if (!mips_hpt_frequency)
  318. mips_hpt_frequency = calibrate_hpt();
  319. /* Report the high precision timer rate for a reference. */
  320. printk("Using %u.%03u MHz high precision timer.\n",
  321. ((mips_hpt_frequency + 500) / 1000) / 1000,
  322. ((mips_hpt_frequency + 500) / 1000) % 1000);
  323. }
  324. if (!mips_timer_ack)
  325. /* No timer interrupt ack (e.g. i8254). */
  326. mips_timer_ack = null_timer_ack;
  327. /*
  328. * Call board specific timer interrupt setup.
  329. *
  330. * this pointer must be setup in machine setup routine.
  331. *
  332. * Even if a machine chooses to use a low-level timer interrupt,
  333. * it still needs to setup the timer_irqaction.
  334. * In that case, it might be better to set timer_irqaction.handler
  335. * to be NULL function so that we are sure the high-level code
  336. * is not invoked accidentally.
  337. */
  338. plat_timer_setup(&timer_irqaction);
  339. init_mips_clocksource();
  340. }
  341. #define FEBRUARY 2
  342. #define STARTOFTIME 1970
  343. #define SECDAY 86400L
  344. #define SECYR (SECDAY * 365)
  345. #define leapyear(y) ((!((y) % 4) && ((y) % 100)) || !((y) % 400))
  346. #define days_in_year(y) (leapyear(y) ? 366 : 365)
  347. #define days_in_month(m) (month_days[(m) - 1])
  348. static int month_days[12] = {
  349. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  350. };
  351. void to_tm(unsigned long tim, struct rtc_time *tm)
  352. {
  353. long hms, day, gday;
  354. int i;
  355. gday = day = tim / SECDAY;
  356. hms = tim % SECDAY;
  357. /* Hours, minutes, seconds are easy */
  358. tm->tm_hour = hms / 3600;
  359. tm->tm_min = (hms % 3600) / 60;
  360. tm->tm_sec = (hms % 3600) % 60;
  361. /* Number of years in days */
  362. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  363. day -= days_in_year(i);
  364. tm->tm_year = i;
  365. /* Number of months in days left */
  366. if (leapyear(tm->tm_year))
  367. days_in_month(FEBRUARY) = 29;
  368. for (i = 1; day >= days_in_month(i); i++)
  369. day -= days_in_month(i);
  370. days_in_month(FEBRUARY) = 28;
  371. tm->tm_mon = i - 1; /* tm_mon starts from 0 to 11 */
  372. /* Days are what is left over (+1) from all that. */
  373. tm->tm_mday = day + 1;
  374. /*
  375. * Determine the day of week
  376. */
  377. tm->tm_wday = (gday + 4) % 7; /* 1970/1/1 was Thursday */
  378. }
  379. EXPORT_SYMBOL(rtc_lock);
  380. EXPORT_SYMBOL(to_tm);
  381. EXPORT_SYMBOL(rtc_mips_set_time);
  382. EXPORT_SYMBOL(rtc_mips_get_time);
  383. unsigned long long sched_clock(void)
  384. {
  385. return (unsigned long long)jiffies*(1000000000/HZ);
  386. }