time.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. #ifndef CONFIG_SOC_PNX8550 /* pnx8550 resets to zero */
  83. /* Ack this timer interrupt and set the next one. */
  84. expirelo += cycles_per_jiffy;
  85. #endif
  86. write_c0_compare(expirelo);
  87. /* Check to see if we have missed any timer interrupts. */
  88. while (((count = read_c0_count()) - expirelo) < 0x7fffffff) {
  89. /* missed_timer_count++; */
  90. expirelo = count + cycles_per_jiffy;
  91. write_c0_compare(expirelo);
  92. }
  93. }
  94. /*
  95. * High precision timer functions for a R4k-compatible timer.
  96. */
  97. static cycle_t c0_hpt_read(void)
  98. {
  99. return read_c0_count();
  100. }
  101. /* For use both as a high precision timer and an interrupt source. */
  102. static void __init c0_hpt_timer_init(void)
  103. {
  104. expirelo = read_c0_count() + cycles_per_jiffy;
  105. write_c0_compare(expirelo);
  106. }
  107. int (*mips_timer_state)(void);
  108. void (*mips_timer_ack)(void);
  109. /* last time when xtime and rtc are sync'ed up */
  110. static long last_rtc_update;
  111. /*
  112. * local_timer_interrupt() does profiling and process accounting
  113. * on a per-CPU basis.
  114. *
  115. * In UP mode, it is invoked from the (global) timer_interrupt.
  116. *
  117. * In SMP mode, it might invoked by per-CPU timer interrupt, or
  118. * a broadcasted inter-processor interrupt which itself is triggered
  119. * by the global timer interrupt.
  120. */
  121. void local_timer_interrupt(int irq, void *dev_id)
  122. {
  123. profile_tick(CPU_PROFILING);
  124. update_process_times(user_mode(get_irq_regs()));
  125. }
  126. /*
  127. * High-level timer interrupt service routines. This function
  128. * is set as irqaction->handler and is invoked through do_IRQ.
  129. */
  130. irqreturn_t timer_interrupt(int irq, void *dev_id)
  131. {
  132. write_seqlock(&xtime_lock);
  133. mips_timer_ack();
  134. /*
  135. * call the generic timer interrupt handling
  136. */
  137. do_timer(1);
  138. /*
  139. * If we have an externally synchronized Linux clock, then update
  140. * CMOS clock accordingly every ~11 minutes. rtc_mips_set_time() has to be
  141. * called as close as possible to 500 ms before the new second starts.
  142. */
  143. if (ntp_synced() &&
  144. xtime.tv_sec > last_rtc_update + 660 &&
  145. (xtime.tv_nsec / 1000) >= 500000 - ((unsigned) TICK_SIZE) / 2 &&
  146. (xtime.tv_nsec / 1000) <= 500000 + ((unsigned) TICK_SIZE) / 2) {
  147. if (rtc_mips_set_mmss(xtime.tv_sec) == 0) {
  148. last_rtc_update = xtime.tv_sec;
  149. } else {
  150. /* do it again in 60 s */
  151. last_rtc_update = xtime.tv_sec - 600;
  152. }
  153. }
  154. write_sequnlock(&xtime_lock);
  155. /*
  156. * In UP mode, we call local_timer_interrupt() to do profiling
  157. * and process accouting.
  158. *
  159. * In SMP mode, local_timer_interrupt() is invoked by appropriate
  160. * low-level local timer interrupt handler.
  161. */
  162. local_timer_interrupt(irq, dev_id);
  163. return IRQ_HANDLED;
  164. }
  165. int null_perf_irq(void)
  166. {
  167. return 0;
  168. }
  169. int (*perf_irq)(void) = null_perf_irq;
  170. EXPORT_SYMBOL(null_perf_irq);
  171. EXPORT_SYMBOL(perf_irq);
  172. asmlinkage void ll_timer_interrupt(int irq)
  173. {
  174. int r2 = cpu_has_mips_r2;
  175. irq_enter();
  176. kstat_this_cpu.irqs[irq]++;
  177. /*
  178. * Suckage alert:
  179. * Before R2 of the architecture there was no way to see if a
  180. * performance counter interrupt was pending, so we have to run the
  181. * performance counter interrupt handler anyway.
  182. */
  183. if (!r2 || (read_c0_cause() & (1 << 26)))
  184. if (perf_irq())
  185. goto out;
  186. /* we keep interrupt disabled all the time */
  187. if (!r2 || (read_c0_cause() & (1 << 30)))
  188. timer_interrupt(irq, NULL);
  189. out:
  190. irq_exit();
  191. }
  192. asmlinkage void ll_local_timer_interrupt(int irq)
  193. {
  194. irq_enter();
  195. if (smp_processor_id() != 0)
  196. kstat_this_cpu.irqs[irq]++;
  197. /* we keep interrupt disabled all the time */
  198. local_timer_interrupt(irq, NULL);
  199. irq_exit();
  200. }
  201. /*
  202. * time_init() - it does the following things.
  203. *
  204. * 1) board_time_init() -
  205. * a) (optional) set up RTC routines,
  206. * b) (optional) calibrate and set the mips_hpt_frequency
  207. * (only needed if you intended to use cpu counter as timer interrupt
  208. * source)
  209. * 2) setup xtime based on rtc_mips_get_time().
  210. * 3) calculate a couple of cached variables for later usage
  211. * 4) plat_timer_setup() -
  212. * a) (optional) over-write any choices made above by time_init().
  213. * b) machine specific code should setup the timer irqaction.
  214. * c) enable the timer interrupt
  215. */
  216. void (*board_time_init)(void);
  217. unsigned int mips_hpt_frequency;
  218. static struct irqaction timer_irqaction = {
  219. .handler = timer_interrupt,
  220. .flags = IRQF_DISABLED,
  221. .name = "timer",
  222. };
  223. static unsigned int __init calibrate_hpt(void)
  224. {
  225. cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
  226. const int loops = HZ / 10;
  227. int log_2_loops = 0;
  228. int i;
  229. /*
  230. * We want to calibrate for 0.1s, but to avoid a 64-bit
  231. * division we round the number of loops up to the nearest
  232. * power of 2.
  233. */
  234. while (loops > 1 << log_2_loops)
  235. log_2_loops++;
  236. i = 1 << log_2_loops;
  237. /*
  238. * Wait for a rising edge of the timer interrupt.
  239. */
  240. while (mips_timer_state());
  241. while (!mips_timer_state());
  242. /*
  243. * Now see how many high precision timer ticks happen
  244. * during the calculated number of periods between timer
  245. * interrupts.
  246. */
  247. hpt_start = clocksource_mips.read();
  248. do {
  249. while (mips_timer_state());
  250. while (!mips_timer_state());
  251. } while (--i);
  252. hpt_end = clocksource_mips.read();
  253. hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
  254. hz = HZ;
  255. frequency = hpt_count * hz;
  256. return frequency >> log_2_loops;
  257. }
  258. struct clocksource clocksource_mips = {
  259. .name = "MIPS",
  260. .mask = 0xffffffff,
  261. .is_continuous = 1,
  262. };
  263. static void __init init_mips_clocksource(void)
  264. {
  265. u64 temp;
  266. u32 shift;
  267. if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read)
  268. return;
  269. /* Calclate a somewhat reasonable rating value */
  270. clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
  271. /* Find a shift value */
  272. for (shift = 32; shift > 0; shift--) {
  273. temp = (u64) NSEC_PER_SEC << shift;
  274. do_div(temp, mips_hpt_frequency);
  275. if ((temp >> 32) == 0)
  276. break;
  277. }
  278. clocksource_mips.shift = shift;
  279. clocksource_mips.mult = (u32)temp;
  280. clocksource_register(&clocksource_mips);
  281. }
  282. void __init time_init(void)
  283. {
  284. if (board_time_init)
  285. board_time_init();
  286. if (!rtc_mips_set_mmss)
  287. rtc_mips_set_mmss = rtc_mips_set_time;
  288. xtime.tv_sec = rtc_mips_get_time();
  289. xtime.tv_nsec = 0;
  290. set_normalized_timespec(&wall_to_monotonic,
  291. -xtime.tv_sec, -xtime.tv_nsec);
  292. /* Choose appropriate high precision timer routines. */
  293. if (!cpu_has_counter && !clocksource_mips.read)
  294. /* No high precision timer -- sorry. */
  295. clocksource_mips.read = null_hpt_read;
  296. else if (!mips_hpt_frequency && !mips_timer_state) {
  297. /* A high precision timer of unknown frequency. */
  298. if (!clocksource_mips.read)
  299. /* No external high precision timer -- use R4k. */
  300. clocksource_mips.read = c0_hpt_read;
  301. } else {
  302. /* We know counter frequency. Or we can get it. */
  303. if (!clocksource_mips.read) {
  304. /* No external high precision timer -- use R4k. */
  305. clocksource_mips.read = c0_hpt_read;
  306. if (!mips_timer_state) {
  307. /* No external timer interrupt -- use R4k. */
  308. mips_timer_ack = c0_timer_ack;
  309. /* Calculate cache parameters. */
  310. cycles_per_jiffy =
  311. (mips_hpt_frequency + HZ / 2) / HZ;
  312. /*
  313. * This sets up the high precision
  314. * timer for the first interrupt.
  315. */
  316. c0_hpt_timer_init();
  317. }
  318. }
  319. if (!mips_hpt_frequency)
  320. mips_hpt_frequency = calibrate_hpt();
  321. /* Report the high precision timer rate for a reference. */
  322. printk("Using %u.%03u MHz high precision timer.\n",
  323. ((mips_hpt_frequency + 500) / 1000) / 1000,
  324. ((mips_hpt_frequency + 500) / 1000) % 1000);
  325. }
  326. if (!mips_timer_ack)
  327. /* No timer interrupt ack (e.g. i8254). */
  328. mips_timer_ack = null_timer_ack;
  329. /*
  330. * Call board specific timer interrupt setup.
  331. *
  332. * this pointer must be setup in machine setup routine.
  333. *
  334. * Even if a machine chooses to use a low-level timer interrupt,
  335. * it still needs to setup the timer_irqaction.
  336. * In that case, it might be better to set timer_irqaction.handler
  337. * to be NULL function so that we are sure the high-level code
  338. * is not invoked accidentally.
  339. */
  340. plat_timer_setup(&timer_irqaction);
  341. init_mips_clocksource();
  342. }
  343. #define FEBRUARY 2
  344. #define STARTOFTIME 1970
  345. #define SECDAY 86400L
  346. #define SECYR (SECDAY * 365)
  347. #define leapyear(y) ((!((y) % 4) && ((y) % 100)) || !((y) % 400))
  348. #define days_in_year(y) (leapyear(y) ? 366 : 365)
  349. #define days_in_month(m) (month_days[(m) - 1])
  350. static int month_days[12] = {
  351. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  352. };
  353. void to_tm(unsigned long tim, struct rtc_time *tm)
  354. {
  355. long hms, day, gday;
  356. int i;
  357. gday = day = tim / SECDAY;
  358. hms = tim % SECDAY;
  359. /* Hours, minutes, seconds are easy */
  360. tm->tm_hour = hms / 3600;
  361. tm->tm_min = (hms % 3600) / 60;
  362. tm->tm_sec = (hms % 3600) % 60;
  363. /* Number of years in days */
  364. for (i = STARTOFTIME; day >= days_in_year(i); i++)
  365. day -= days_in_year(i);
  366. tm->tm_year = i;
  367. /* Number of months in days left */
  368. if (leapyear(tm->tm_year))
  369. days_in_month(FEBRUARY) = 29;
  370. for (i = 1; day >= days_in_month(i); i++)
  371. day -= days_in_month(i);
  372. days_in_month(FEBRUARY) = 28;
  373. tm->tm_mon = i - 1; /* tm_mon starts from 0 to 11 */
  374. /* Days are what is left over (+1) from all that. */
  375. tm->tm_mday = day + 1;
  376. /*
  377. * Determine the day of week
  378. */
  379. tm->tm_wday = (gday + 4) % 7; /* 1970/1/1 was Thursday */
  380. }
  381. EXPORT_SYMBOL(rtc_lock);
  382. EXPORT_SYMBOL(to_tm);
  383. EXPORT_SYMBOL(rtc_mips_set_time);
  384. EXPORT_SYMBOL(rtc_mips_get_time);
  385. unsigned long long sched_clock(void)
  386. {
  387. return (unsigned long long)jiffies*(1000000000/HZ);
  388. }