time.c 12 KB

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