blob: d23e6825e9886b315dfd22db7312ef13fd7f59f7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Yoichi Yuasab1043cc2007-09-13 13:13:28 +090019#include <linux/profile.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#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
28#include <asm/bootinfo.h>
Ralf Baechleec74e362005-07-13 11:48:45 +000029#include <asm/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/compiler.h>
31#include <asm/cpu.h>
32#include <asm/cpu-features.h>
33#include <asm/div64.h>
34#include <asm/sections.h>
35#include <asm/time.h>
36
37/*
38 * The integer part of the number of usecs per jiffy is taken from tick,
39 * but the fractional part is not recorded, so we calculate it using the
40 * initial value of HZ. This aids systems where tick isn't really an
41 * integer (e.g. for HZ = 128).
42 */
43#define USECS_PER_JIFFY TICK_SIZE
44#define USECS_PER_JIFFY_FRAC ((unsigned long)(u32)((1000000ULL << 32) / HZ))
45
46#define TICK_SIZE (tick_nsec / 1000)
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*
49 * forward reference
50 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070051DEFINE_SPINLOCK(rtc_lock);
Ralf Baechle4b550482007-10-11 23:46:08 +010052EXPORT_SYMBOL(rtc_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Ralf Baechle4b550482007-10-11 23:46:08 +010054int __weak rtc_mips_set_time(unsigned long sec)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 return 0;
57}
Ralf Baechle4b550482007-10-11 23:46:08 +010058EXPORT_SYMBOL(rtc_mips_set_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Ralf Baechle4b550482007-10-11 23:46:08 +010060int __weak rtc_mips_set_mmss(unsigned long nowtime)
61{
62 return rtc_mips_set_time(nowtime);
63}
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Ralf Baechlef5ff0a22007-08-13 15:26:12 +010065int update_persistent_clock(struct timespec now)
66{
67 return rtc_mips_set_mmss(now.tv_sec);
68}
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070/* how many counter cycles in a jiffy */
Ralf Baechleec74e362005-07-13 11:48:45 +000071static unsigned long cycles_per_jiffy __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* expirelo is the count value for next CPU timer interrupt */
74static unsigned int expirelo;
75
76
77/*
78 * Null timer ack for systems not needing one (e.g. i8254).
79 */
80static void null_timer_ack(void) { /* nothing */ }
81
82/*
83 * Null high precision timer functions for systems lacking one.
84 */
Atsushi Nemoto00598562006-11-12 00:10:28 +090085static cycle_t null_hpt_read(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 return 0;
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
91 * Timer ack for an R4k-compatible timer of a known frequency.
92 */
93static void c0_timer_ack(void)
94{
95 unsigned int count;
96
97 /* Ack this timer interrupt and set the next one. */
98 expirelo += cycles_per_jiffy;
99 write_c0_compare(expirelo);
100
101 /* Check to see if we have missed any timer interrupts. */
Ralf Baechle41c594a2006-04-05 09:45:45 +0100102 while (((count = read_c0_count()) - expirelo) < 0x7fffffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /* missed_timer_count++; */
104 expirelo = count + cycles_per_jiffy;
105 write_c0_compare(expirelo);
106 }
107}
108
109/*
110 * High precision timer functions for a R4k-compatible timer.
111 */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900112static cycle_t c0_hpt_read(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 return read_c0_count();
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/* For use both as a high precision timer and an interrupt source. */
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900118static void __init c0_hpt_timer_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900120 expirelo = read_c0_count() + cycles_per_jiffy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 write_c0_compare(expirelo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124int (*mips_timer_state)(void);
125void (*mips_timer_ack)(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/*
128 * local_timer_interrupt() does profiling and process accounting
129 * on a per-CPU basis.
130 *
131 * In UP mode, it is invoked from the (global) timer_interrupt.
132 *
133 * In SMP mode, it might invoked by per-CPU timer interrupt, or
134 * a broadcasted inter-processor interrupt which itself is triggered
135 * by the global timer interrupt.
136 */
David Howells7d12e782006-10-05 14:55:46 +0100137void local_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Ralf Baechle937a8012006-10-07 19:44:33 +0100139 profile_tick(CPU_PROFILING);
David Howells7d12e782006-10-05 14:55:46 +0100140 update_process_times(user_mode(get_irq_regs()));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143/*
144 * High-level timer interrupt service routines. This function
145 * is set as irqaction->handler and is invoked through do_IRQ.
146 */
Ralf Baechle91a2fcc2007-10-11 23:46:09 +0100147static irqreturn_t timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Ralf Baechled6bd0e62006-03-14 23:46:58 +0000149 write_seqlock(&xtime_lock);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 mips_timer_ack();
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /*
154 * call the generic timer interrupt handling
155 */
Atsushi Nemoto3171a032006-09-29 02:00:32 -0700156 do_timer(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Ralf Baechled6bd0e62006-03-14 23:46:58 +0000158 write_sequnlock(&xtime_lock);
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /*
161 * In UP mode, we call local_timer_interrupt() to do profiling
162 * and process accouting.
163 *
164 * In SMP mode, local_timer_interrupt() is invoked by appropriate
165 * low-level local timer interrupt handler.
166 */
David Howells7d12e782006-10-05 14:55:46 +0100167 local_timer_interrupt(irq, dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 return IRQ_HANDLED;
170}
171
David Howells7d12e782006-10-05 14:55:46 +0100172int null_perf_irq(void)
Ralf Baechleba339c02005-12-09 12:29:38 +0000173{
174 return 0;
175}
176
Ralf Baechle91a2fcc2007-10-11 23:46:09 +0100177EXPORT_SYMBOL(null_perf_irq);
178
David Howells7d12e782006-10-05 14:55:46 +0100179int (*perf_irq)(void) = null_perf_irq;
Ralf Baechleba339c02005-12-09 12:29:38 +0000180
Ralf Baechleba339c02005-12-09 12:29:38 +0000181EXPORT_SYMBOL(perf_irq);
182
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100183/*
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100184 * Timer interrupt
185 */
186int cp0_compare_irq;
187
188/*
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100189 * Performance counter IRQ or -1 if shared with timer
190 */
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100191int cp0_perfcount_irq;
192EXPORT_SYMBOL_GPL(cp0_perfcount_irq);
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100193
194/*
195 * Possibly handle a performance counter interrupt.
196 * Return true if the timer interrupt should not be checked
197 */
198static inline int handle_perf_irq (int r2)
199{
200 /*
201 * The performance counter overflow interrupt may be shared with the
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100202 * timer interrupt (cp0_perfcount_irq < 0). If it is and a
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100203 * performance counter has overflowed (perf_irq() == IRQ_HANDLED)
204 * and we can't reliably determine if a counter interrupt has also
205 * happened (!r2) then don't check for a timer interrupt.
206 */
Ralf Baechle3b1d4ed2007-06-20 22:27:10 +0100207 return (cp0_perfcount_irq < 0) &&
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100208 perf_irq() == IRQ_HANDLED &&
209 !r2;
210}
211
Ralf Baechle91a2fcc2007-10-11 23:46:09 +0100212void ll_timer_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Ralf Baechle91a2fcc2007-10-11 23:46:09 +0100214 int cpu = smp_processor_id();
215
216#ifdef CONFIG_MIPS_MT_SMTC
217 /*
218 * In an SMTC system, one Count/Compare set exists per VPE.
219 * Which TC within a VPE gets the interrupt is essentially
220 * random - we only know that it shouldn't be one with
221 * IXMT set. Whichever TC gets the interrupt needs to
222 * send special interprocessor interrupts to the other
223 * TCs to make sure that they schedule, etc.
224 *
225 * That code is specific to the SMTC kernel, not to
226 * the a particular platform, so it's invoked from
227 * the general MIPS timer_interrupt routine.
228 */
229
230 /*
231 * We could be here due to timer interrupt,
232 * perf counter overflow, or both.
233 */
234 (void) handle_perf_irq(1);
235
236 if (read_c0_cause() & (1 << 30)) {
237 /*
238 * There are things we only want to do once per tick
239 * in an "MP" system. One TC of each VPE will take
240 * the actual timer interrupt. The others will get
241 * timer broadcast IPIs. We use whoever it is that takes
242 * the tick on VPE 0 to run the full timer_interrupt().
243 */
244 if (cpu_data[cpu].vpe_id == 0) {
245 timer_interrupt(irq, NULL);
246 } else {
247 write_c0_compare(read_c0_count() +
248 (mips_hpt_frequency/HZ));
249 local_timer_interrupt(irq, dev_id);
250 }
251 smtc_timer_broadcast(cpu_data[cpu].vpe_id);
252 }
253#else /* CONFIG_MIPS_MT_SMTC */
Ralf Baechleba339c02005-12-09 12:29:38 +0000254 int r2 = cpu_has_mips_r2;
255
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100256 if (handle_perf_irq(r2))
Ralf Baechle91a2fcc2007-10-11 23:46:09 +0100257 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100259 if (r2 && ((read_c0_cause() & (1 << 30)) == 0))
Ralf Baechle91a2fcc2007-10-11 23:46:09 +0100260 return;
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100261
Ralf Baechle91a2fcc2007-10-11 23:46:09 +0100262 if (cpu == 0) {
263 /*
264 * CPU 0 handles the global timer interrupt job and process
265 * accounting resets count/compare registers to trigger next
266 * timer int.
267 */
268 timer_interrupt(irq, NULL);
269 } else {
270 /* Everyone else needs to reset the timer int here as
271 ll_local_timer_interrupt doesn't */
272 /*
273 * FIXME: need to cope with counter underflow.
274 * More support needs to be added to kernel/time for
275 * counter/timer interrupts on multiple CPU's
276 */
277 write_c0_compare(read_c0_count() + (mips_hpt_frequency/HZ));
Ralf Baechleba339c02005-12-09 12:29:38 +0000278
Ralf Baechle91a2fcc2007-10-11 23:46:09 +0100279 /*
280 * Other CPUs should do profiling and process accounting
281 */
282 local_timer_interrupt(irq, dev_id);
283 }
284#endif /* CONFIG_MIPS_MT_SMTC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287/*
288 * time_init() - it does the following things.
289 *
Ralf Baechle4b550482007-10-11 23:46:08 +0100290 * 1) plat_time_init() -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * a) (optional) set up RTC routines,
292 * b) (optional) calibrate and set the mips_hpt_frequency
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900293 * (only needed if you intended to use cpu counter as timer interrupt
294 * source)
Ralf Baechle4b550482007-10-11 23:46:08 +0100295 * 2) calculate a couple of cached variables for later usage
296 * 3) plat_timer_setup() -
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * a) (optional) over-write any choices made above by time_init().
298 * b) machine specific code should setup the timer irqaction.
299 * c) enable the timer interrupt
300 */
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302unsigned int mips_hpt_frequency;
303
304static struct irqaction timer_irqaction = {
305 .handler = timer_interrupt,
Chris Dearmanffe9ee42007-05-24 22:24:20 +0100306 .flags = IRQF_DISABLED | IRQF_PERCPU,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 .name = "timer",
308};
309
310static unsigned int __init calibrate_hpt(void)
311{
Atsushi Nemoto00598562006-11-12 00:10:28 +0900312 cycle_t frequency, hpt_start, hpt_end, hpt_count, hz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 const int loops = HZ / 10;
315 int log_2_loops = 0;
316 int i;
317
318 /*
319 * We want to calibrate for 0.1s, but to avoid a 64-bit
320 * division we round the number of loops up to the nearest
321 * power of 2.
322 */
323 while (loops > 1 << log_2_loops)
324 log_2_loops++;
325 i = 1 << log_2_loops;
326
327 /*
328 * Wait for a rising edge of the timer interrupt.
329 */
330 while (mips_timer_state());
331 while (!mips_timer_state());
332
333 /*
334 * Now see how many high precision timer ticks happen
335 * during the calculated number of periods between timer
336 * interrupts.
337 */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900338 hpt_start = clocksource_mips.read();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 do {
340 while (mips_timer_state());
341 while (!mips_timer_state());
342 } while (--i);
Atsushi Nemoto00598562006-11-12 00:10:28 +0900343 hpt_end = clocksource_mips.read();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Atsushi Nemoto00598562006-11-12 00:10:28 +0900345 hpt_count = (hpt_end - hpt_start) & clocksource_mips.mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 hz = HZ;
Atsushi Nemoto00598562006-11-12 00:10:28 +0900347 frequency = hpt_count * hz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349 return frequency >> log_2_loops;
350}
351
Atsushi Nemoto00598562006-11-12 00:10:28 +0900352struct clocksource clocksource_mips = {
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900353 .name = "MIPS",
Franck Bui-Huu55d0b4e2007-05-04 17:36:44 +0200354 .mask = CLOCKSOURCE_MASK(32),
Thomas Gleixner877fe382007-02-16 01:27:40 -0800355 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900356};
357
358static void __init init_mips_clocksource(void)
359{
360 u64 temp;
361 u32 shift;
362
Atsushi Nemoto00598562006-11-12 00:10:28 +0900363 if (!mips_hpt_frequency || clocksource_mips.read == null_hpt_read)
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900364 return;
365
366 /* Calclate a somewhat reasonable rating value */
367 clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;
368 /* Find a shift value */
369 for (shift = 32; shift > 0; shift--) {
370 temp = (u64) NSEC_PER_SEC << shift;
371 do_div(temp, mips_hpt_frequency);
372 if ((temp >> 32) == 0)
373 break;
374 }
375 clocksource_mips.shift = shift;
376 clocksource_mips.mult = (u32)temp;
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900377
378 clocksource_register(&clocksource_mips);
379}
380
Ralf Baechle4b550482007-10-11 23:46:08 +0100381void __init __weak plat_time_init(void)
382{
383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385void __init time_init(void)
386{
Ralf Baechle4b550482007-10-11 23:46:08 +0100387 plat_time_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /* Choose appropriate high precision timer routines. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900390 if (!cpu_has_counter && !clocksource_mips.read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /* No high precision timer -- sorry. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900392 clocksource_mips.read = null_hpt_read;
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900393 else if (!mips_hpt_frequency && !mips_timer_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /* A high precision timer of unknown frequency. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900395 if (!clocksource_mips.read)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* No external high precision timer -- use R4k. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900397 clocksource_mips.read = c0_hpt_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 } else {
399 /* We know counter frequency. Or we can get it. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900400 if (!clocksource_mips.read) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 /* No external high precision timer -- use R4k. */
Atsushi Nemoto00598562006-11-12 00:10:28 +0900402 clocksource_mips.read = c0_hpt_read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900404 if (!mips_timer_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /* No external timer interrupt -- use R4k. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 mips_timer_ack = c0_timer_ack;
Atsushi Nemotoc87b6eb2006-10-28 01:14:37 +0900407 /* Calculate cache parameters. */
408 cycles_per_jiffy =
409 (mips_hpt_frequency + HZ / 2) / HZ;
410 /*
411 * This sets up the high precision
412 * timer for the first interrupt.
413 */
414 c0_hpt_timer_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416 }
417 if (!mips_hpt_frequency)
418 mips_hpt_frequency = calibrate_hpt();
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* Report the high precision timer rate for a reference. */
421 printk("Using %u.%03u MHz high precision timer.\n",
422 ((mips_hpt_frequency + 500) / 1000) / 1000,
423 ((mips_hpt_frequency + 500) / 1000) % 1000);
424 }
425
426 if (!mips_timer_ack)
427 /* No timer interrupt ack (e.g. i8254). */
428 mips_timer_ack = null_timer_ack;
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /*
431 * Call board specific timer interrupt setup.
432 *
433 * this pointer must be setup in machine setup routine.
434 *
435 * Even if a machine chooses to use a low-level timer interrupt,
436 * it still needs to setup the timer_irqaction.
437 * In that case, it might be better to set timer_irqaction.handler
438 * to be NULL function so that we are sure the high-level code
439 * is not invoked accidentally.
440 */
Ralf Baechle54d0a212006-07-09 21:38:56 +0100441 plat_timer_setup(&timer_irqaction);
Atsushi Nemoto16b7b2a2006-10-24 00:21:27 +0900442
443 init_mips_clocksource();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}