powerpc/time: Use clockevents_calc_mult_shift
[deliverable/linux.git] / arch / powerpc / kernel / time.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Common time routines among all ppc machines.
3 *
4 * Written by Cort Dougan (cort@cs.nmt.edu) to merge
5 * Paul Mackerras' version and mine for PReP and Pmac.
6 * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net).
7 * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com)
8 *
9 * First round of bugfixes by Gabriel Paubert (paubert@iram.es)
10 * to make clock more stable (2.4.0-test5). The only thing
11 * that this code assumes is that the timebases have been synchronized
12 * by firmware on SMP and are never stopped (never do sleep
13 * on SMP then, nap and doze are OK).
14 *
15 * Speeded up do_gettimeofday by getting rid of references to
16 * xtime (which required locks for consistency). (mikejc@us.ibm.com)
17 *
18 * TODO (not necessarily in this file):
19 * - improve precision and reproducibility of timebase frequency
20 * measurement at boot time. (for iSeries, we calibrate the timebase
21 * against the Titan chip's clock.)
22 * - for astronomical applications: add a new function to get
23 * non ambiguous timestamps even around leap seconds. This needs
24 * a new timestamp format and a good name.
25 *
26 * 1997-09-10 Updated NTP code according to technical memorandum Jan '96
27 * "A Kernel Model for Precision Timekeeping" by Dave Mills
28 *
29 * This program is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU General Public License
31 * as published by the Free Software Foundation; either version
32 * 2 of the License, or (at your option) any later version.
33 */
34
1da177e4 35#include <linux/errno.h>
4b16f8e2 36#include <linux/export.h>
1da177e4
LT
37#include <linux/sched.h>
38#include <linux/kernel.h>
39#include <linux/param.h>
40#include <linux/string.h>
41#include <linux/mm.h>
42#include <linux/interrupt.h>
43#include <linux/timex.h>
44#include <linux/kernel_stat.h>
1da177e4
LT
45#include <linux/time.h>
46#include <linux/init.h>
47#include <linux/profile.h>
48#include <linux/cpu.h>
49#include <linux/security.h>
f2783c15
PM
50#include <linux/percpu.h>
51#include <linux/rtc.h>
092b8f34 52#include <linux/jiffies.h>
c6622f63 53#include <linux/posix-timers.h>
7d12e780 54#include <linux/irq.h>
177996e6 55#include <linux/delay.h>
e360adbe 56#include <linux/irq_work.h>
6795b85c 57#include <asm/trace.h>
1da177e4 58
1da177e4
LT
59#include <asm/io.h>
60#include <asm/processor.h>
61#include <asm/nvram.h>
62#include <asm/cache.h>
63#include <asm/machdep.h>
1da177e4
LT
64#include <asm/uaccess.h>
65#include <asm/time.h>
1da177e4 66#include <asm/prom.h>
f2783c15
PM
67#include <asm/irq.h>
68#include <asm/div64.h>
2249ca9d 69#include <asm/smp.h>
a7f290da 70#include <asm/vdso_datapage.h>
1ababe11 71#include <asm/firmware.h>
06b8e878 72#include <asm/cputime.h>
f2783c15 73#ifdef CONFIG_PPC_ISERIES
8875ccfb 74#include <asm/iseries/it_lp_queue.h>
8021b8a7 75#include <asm/iseries/hv_call_xm.h>
f2783c15 76#endif
1da177e4 77
4a4cfe38
TB
78/* powerpc clocksource/clockevent code */
79
d831d0b8 80#include <linux/clockchips.h>
4a4cfe38
TB
81#include <linux/clocksource.h>
82
8e19608e 83static cycle_t rtc_read(struct clocksource *);
4a4cfe38
TB
84static struct clocksource clocksource_rtc = {
85 .name = "rtc",
86 .rating = 400,
87 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
88 .mask = CLOCKSOURCE_MASK(64),
89 .shift = 22,
90 .mult = 0, /* To be filled in */
91 .read = rtc_read,
92};
93
8e19608e 94static cycle_t timebase_read(struct clocksource *);
4a4cfe38
TB
95static struct clocksource clocksource_timebase = {
96 .name = "timebase",
97 .rating = 400,
98 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
99 .mask = CLOCKSOURCE_MASK(64),
100 .shift = 22,
101 .mult = 0, /* To be filled in */
102 .read = timebase_read,
103};
104
d831d0b8
TB
105#define DECREMENTER_MAX 0x7fffffff
106
107static int decrementer_set_next_event(unsigned long evt,
108 struct clock_event_device *dev);
109static void decrementer_set_mode(enum clock_event_mode mode,
110 struct clock_event_device *dev);
111
112static struct clock_event_device decrementer_clockevent = {
113 .name = "decrementer",
114 .rating = 200,
d831d0b8
TB
115 .irq = 0,
116 .set_next_event = decrementer_set_next_event,
117 .set_mode = decrementer_set_mode,
118 .features = CLOCK_EVT_FEAT_ONESHOT,
119};
120
6e6b44e8
MM
121struct decrementer_clock {
122 struct clock_event_device event;
123 u64 next_tb;
124};
125
126static DEFINE_PER_CPU(struct decrementer_clock, decrementers);
d831d0b8 127
1da177e4 128#ifdef CONFIG_PPC_ISERIES
71712b45
TB
129static unsigned long __initdata iSeries_recal_titan;
130static signed long __initdata iSeries_recal_tb;
4a4cfe38
TB
131
132/* Forward declaration is only needed for iSereis compiles */
1c21a293 133static void __init clocksource_init(void);
1da177e4
LT
134#endif
135
136#define XSEC_PER_SEC (1024*1024)
137
f2783c15
PM
138#ifdef CONFIG_PPC64
139#define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC)
140#else
141/* compute ((xsec << 12) * max) >> 32 */
142#define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max)
143#endif
144
1da177e4
LT
145unsigned long tb_ticks_per_jiffy;
146unsigned long tb_ticks_per_usec = 100; /* sane default */
147EXPORT_SYMBOL(tb_ticks_per_usec);
148unsigned long tb_ticks_per_sec;
2cf82c02 149EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime_t conversions */
092b8f34 150
1da177e4 151DEFINE_SPINLOCK(rtc_lock);
6ae3db11 152EXPORT_SYMBOL_GPL(rtc_lock);
1da177e4 153
fc9069fe
TB
154static u64 tb_to_ns_scale __read_mostly;
155static unsigned tb_to_ns_shift __read_mostly;
364a1246 156static u64 boot_tb __read_mostly;
1da177e4 157
1da177e4 158extern struct timezone sys_tz;
f2783c15 159static long timezone_offset;
1da177e4 160
10f7e7c1 161unsigned long ppc_proc_freq;
55ec2fca 162EXPORT_SYMBOL_GPL(ppc_proc_freq);
10f7e7c1 163unsigned long ppc_tb_freq;
55ec2fca 164EXPORT_SYMBOL_GPL(ppc_tb_freq);
96c44507 165
c6622f63
PM
166#ifdef CONFIG_VIRT_CPU_ACCOUNTING
167/*
168 * Factors for converting from cputime_t (timebase ticks) to
169 * jiffies, milliseconds, seconds, and clock_t (1/USER_HZ seconds).
170 * These are all stored as 0.64 fixed-point binary fractions.
171 */
172u64 __cputime_jiffies_factor;
2cf82c02 173EXPORT_SYMBOL(__cputime_jiffies_factor);
c6622f63 174u64 __cputime_msec_factor;
2cf82c02 175EXPORT_SYMBOL(__cputime_msec_factor);
c6622f63 176u64 __cputime_sec_factor;
2cf82c02 177EXPORT_SYMBOL(__cputime_sec_factor);
c6622f63 178u64 __cputime_clockt_factor;
2cf82c02 179EXPORT_SYMBOL(__cputime_clockt_factor);
06b8e878
MN
180DEFINE_PER_CPU(unsigned long, cputime_last_delta);
181DEFINE_PER_CPU(unsigned long, cputime_scaled_last_delta);
c6622f63 182
a42548a1
SG
183cputime_t cputime_one_jiffy;
184
872e439a
PM
185void (*dtl_consumer)(struct dtl_entry *, u64);
186
c6622f63
PM
187static void calc_cputime_factors(void)
188{
189 struct div_result res;
190
191 div128_by_32(HZ, 0, tb_ticks_per_sec, &res);
192 __cputime_jiffies_factor = res.result_low;
193 div128_by_32(1000, 0, tb_ticks_per_sec, &res);
194 __cputime_msec_factor = res.result_low;
195 div128_by_32(1, 0, tb_ticks_per_sec, &res);
196 __cputime_sec_factor = res.result_low;
197 div128_by_32(USER_HZ, 0, tb_ticks_per_sec, &res);
198 __cputime_clockt_factor = res.result_low;
199}
200
201/*
cf9efce0
PM
202 * Read the SPURR on systems that have it, otherwise the PURR,
203 * or if that doesn't exist return the timebase value passed in.
c6622f63 204 */
cf9efce0 205static u64 read_spurr(u64 tb)
c6622f63 206{
cf9efce0
PM
207 if (cpu_has_feature(CPU_FTR_SPURR))
208 return mfspr(SPRN_SPURR);
c6622f63
PM
209 if (cpu_has_feature(CPU_FTR_PURR))
210 return mfspr(SPRN_PURR);
cf9efce0 211 return tb;
c6622f63
PM
212}
213
cf9efce0
PM
214#ifdef CONFIG_PPC_SPLPAR
215
4603ac18 216/*
cf9efce0
PM
217 * Scan the dispatch trace log and count up the stolen time.
218 * Should be called with interrupts disabled.
4603ac18 219 */
cf9efce0 220static u64 scan_dispatch_log(u64 stop_tb)
4603ac18 221{
872e439a 222 u64 i = local_paca->dtl_ridx;
cf9efce0
PM
223 struct dtl_entry *dtl = local_paca->dtl_curr;
224 struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
225 struct lppaca *vpa = local_paca->lppaca_ptr;
226 u64 tb_delta;
227 u64 stolen = 0;
228 u64 dtb;
229
84ffae55
AB
230 if (!dtl)
231 return 0;
232
cf9efce0
PM
233 if (i == vpa->dtl_idx)
234 return 0;
235 while (i < vpa->dtl_idx) {
872e439a
PM
236 if (dtl_consumer)
237 dtl_consumer(dtl, i);
cf9efce0
PM
238 dtb = dtl->timebase;
239 tb_delta = dtl->enqueue_to_dispatch_time +
240 dtl->ready_to_enqueue_time;
241 barrier();
242 if (i + N_DISPATCH_LOG < vpa->dtl_idx) {
243 /* buffer has overflowed */
244 i = vpa->dtl_idx - N_DISPATCH_LOG;
245 dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
246 continue;
247 }
248 if (dtb > stop_tb)
249 break;
250 stolen += tb_delta;
251 ++i;
252 ++dtl;
253 if (dtl == dtl_end)
254 dtl = local_paca->dispatch_log;
255 }
256 local_paca->dtl_ridx = i;
257 local_paca->dtl_curr = dtl;
258 return stolen;
4603ac18
MN
259}
260
cf9efce0
PM
261/*
262 * Accumulate stolen time by scanning the dispatch trace log.
263 * Called on entry from user mode.
264 */
265void accumulate_stolen_time(void)
266{
267 u64 sst, ust;
268
b18ae08d
TH
269 u8 save_soft_enabled = local_paca->soft_enabled;
270 u8 save_hard_enabled = local_paca->hard_enabled;
271
272 /* We are called early in the exception entry, before
273 * soft/hard_enabled are sync'ed to the expected state
274 * for the exception. We are hard disabled but the PACA
275 * needs to reflect that so various debug stuff doesn't
276 * complain
277 */
278 local_paca->soft_enabled = 0;
279 local_paca->hard_enabled = 0;
280
281 sst = scan_dispatch_log(local_paca->starttime_user);
282 ust = scan_dispatch_log(local_paca->starttime);
283 local_paca->system_time -= sst;
284 local_paca->user_time -= ust;
285 local_paca->stolen_time += ust + sst;
286
287 local_paca->soft_enabled = save_soft_enabled;
288 local_paca->hard_enabled = save_hard_enabled;
cf9efce0
PM
289}
290
291static inline u64 calculate_stolen_time(u64 stop_tb)
292{
293 u64 stolen = 0;
294
295 if (get_paca()->dtl_ridx != get_paca()->lppaca_ptr->dtl_idx) {
296 stolen = scan_dispatch_log(stop_tb);
297 get_paca()->system_time -= stolen;
298 }
299
300 stolen += get_paca()->stolen_time;
301 get_paca()->stolen_time = 0;
302 return stolen;
4603ac18
MN
303}
304
cf9efce0
PM
305#else /* CONFIG_PPC_SPLPAR */
306static inline u64 calculate_stolen_time(u64 stop_tb)
307{
308 return 0;
309}
310
311#endif /* CONFIG_PPC_SPLPAR */
312
c6622f63
PM
313/*
314 * Account time for a transition between system, hard irq
315 * or soft irq state.
316 */
317void account_system_vtime(struct task_struct *tsk)
318{
cf9efce0 319 u64 now, nowscaled, delta, deltascaled;
c6622f63 320 unsigned long flags;
cf9efce0 321 u64 stolen, udelta, sys_scaled, user_scaled;
c6622f63
PM
322
323 local_irq_save(flags);
cf9efce0 324 now = mftb();
4603ac18 325 nowscaled = read_spurr(now);
cf9efce0
PM
326 get_paca()->system_time += now - get_paca()->starttime;
327 get_paca()->starttime = now;
4603ac18
MN
328 deltascaled = nowscaled - get_paca()->startspurr;
329 get_paca()->startspurr = nowscaled;
cf9efce0
PM
330
331 stolen = calculate_stolen_time(now);
332
333 delta = get_paca()->system_time;
334 get_paca()->system_time = 0;
335 udelta = get_paca()->user_time - get_paca()->utime_sspurr;
336 get_paca()->utime_sspurr = get_paca()->user_time;
337
338 /*
339 * Because we don't read the SPURR on every kernel entry/exit,
340 * deltascaled includes both user and system SPURR ticks.
341 * Apportion these ticks to system SPURR ticks and user
342 * SPURR ticks in the same ratio as the system time (delta)
343 * and user time (udelta) values obtained from the timebase
344 * over the same interval. The system ticks get accounted here;
345 * the user ticks get saved up in paca->user_time_scaled to be
346 * used by account_process_tick.
347 */
348 sys_scaled = delta;
349 user_scaled = udelta;
350 if (deltascaled != delta + udelta) {
351 if (udelta) {
352 sys_scaled = deltascaled * delta / (delta + udelta);
353 user_scaled = deltascaled - sys_scaled;
354 } else {
355 sys_scaled = deltascaled;
356 }
357 }
358 get_paca()->user_time_scaled += user_scaled;
359
ad5d1c88 360 if (in_interrupt() || idle_task(smp_processor_id()) != tsk) {
cf9efce0
PM
361 account_system_time(tsk, 0, delta, sys_scaled);
362 if (stolen)
363 account_steal_time(stolen);
364 } else {
365 account_idle_time(delta + stolen);
c6622f63 366 }
c6622f63
PM
367 local_irq_restore(flags);
368}
4ab79aa8 369EXPORT_SYMBOL_GPL(account_system_vtime);
c6622f63
PM
370
371/*
372 * Transfer the user and system times accumulated in the paca
373 * by the exception entry and exit code to the generic process
374 * user and system time records.
375 * Must be called with interrupts disabled.
cf9efce0
PM
376 * Assumes that account_system_vtime() has been called recently
377 * (i.e. since the last entry from usermode) so that
378 * get_paca()->user_time_scaled is up to date.
c6622f63 379 */
fa13a5a1 380void account_process_tick(struct task_struct *tsk, int user_tick)
c6622f63 381{
4603ac18 382 cputime_t utime, utimescaled;
c6622f63
PM
383
384 utime = get_paca()->user_time;
cf9efce0 385 utimescaled = get_paca()->user_time_scaled;
c6622f63 386 get_paca()->user_time = 0;
cf9efce0
PM
387 get_paca()->user_time_scaled = 0;
388 get_paca()->utime_sspurr = 0;
457533a7 389 account_user_time(tsk, utime, utimescaled);
c6622f63
PM
390}
391
c6622f63
PM
392#else /* ! CONFIG_VIRT_CPU_ACCOUNTING */
393#define calc_cputime_factors()
c6622f63
PM
394#endif
395
6defa38b
PM
396void __delay(unsigned long loops)
397{
398 unsigned long start;
399 int diff;
400
401 if (__USE_RTC()) {
402 start = get_rtcl();
403 do {
404 /* the RTCL register wraps at 1000000000 */
405 diff = get_rtcl() - start;
406 if (diff < 0)
407 diff += 1000000000;
408 } while (diff < loops);
409 } else {
410 start = get_tbl();
411 while (get_tbl() - start < loops)
412 HMT_low();
413 HMT_medium();
414 }
415}
416EXPORT_SYMBOL(__delay);
417
418void udelay(unsigned long usecs)
419{
420 __delay(tb_ticks_per_usec * usecs);
421}
422EXPORT_SYMBOL(udelay);
423
1da177e4
LT
424#ifdef CONFIG_SMP
425unsigned long profile_pc(struct pt_regs *regs)
426{
427 unsigned long pc = instruction_pointer(regs);
428
429 if (in_lock_functions(pc))
430 return regs->link;
431
432 return pc;
433}
434EXPORT_SYMBOL(profile_pc);
435#endif
436
437#ifdef CONFIG_PPC_ISERIES
438
439/*
440 * This function recalibrates the timebase based on the 49-bit time-of-day
441 * value in the Titan chip. The Titan is much more accurate than the value
442 * returned by the service processor for the timebase frequency.
443 */
444
71712b45 445static int __init iSeries_tb_recal(void)
1da177e4 446{
1da177e4 447 unsigned long titan, tb;
71712b45
TB
448
449 /* Make sure we only run on iSeries */
450 if (!firmware_has_feature(FW_FEATURE_ISERIES))
451 return -ENODEV;
452
1da177e4
LT
453 tb = get_tb();
454 titan = HvCallXm_loadTod();
455 if ( iSeries_recal_titan ) {
456 unsigned long tb_ticks = tb - iSeries_recal_tb;
457 unsigned long titan_usec = (titan - iSeries_recal_titan) >> 12;
458 unsigned long new_tb_ticks_per_sec = (tb_ticks * USEC_PER_SEC)/titan_usec;
14ea58ad
JL
459 unsigned long new_tb_ticks_per_jiffy =
460 DIV_ROUND_CLOSEST(new_tb_ticks_per_sec, HZ);
1da177e4
LT
461 long tick_diff = new_tb_ticks_per_jiffy - tb_ticks_per_jiffy;
462 char sign = '+';
463 /* make sure tb_ticks_per_sec and tb_ticks_per_jiffy are consistent */
464 new_tb_ticks_per_sec = new_tb_ticks_per_jiffy * HZ;
465
466 if ( tick_diff < 0 ) {
467 tick_diff = -tick_diff;
468 sign = '-';
469 }
470 if ( tick_diff ) {
471 if ( tick_diff < tb_ticks_per_jiffy/25 ) {
472 printk( "Titan recalibrate: new tb_ticks_per_jiffy = %lu (%c%ld)\n",
473 new_tb_ticks_per_jiffy, sign, tick_diff );
474 tb_ticks_per_jiffy = new_tb_ticks_per_jiffy;
475 tb_ticks_per_sec = new_tb_ticks_per_sec;
c6622f63 476 calc_cputime_factors();
a7f290da 477 vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
a42548a1 478 setup_cputime_one_jiffy();
1da177e4
LT
479 }
480 else {
481 printk( "Titan recalibrate: FAILED (difference > 4 percent)\n"
482 " new tb_ticks_per_jiffy = %lu\n"
483 " old tb_ticks_per_jiffy = %lu\n",
484 new_tb_ticks_per_jiffy, tb_ticks_per_jiffy );
485 }
486 }
487 }
488 iSeries_recal_titan = titan;
489 iSeries_recal_tb = tb;
71712b45 490
4a4cfe38
TB
491 /* Called here as now we know accurate values for the timebase */
492 clocksource_init();
71712b45 493 return 0;
1da177e4 494}
71712b45
TB
495late_initcall(iSeries_tb_recal);
496
497/* Called from platform early init */
498void __init iSeries_time_init_early(void)
499{
500 iSeries_recal_tb = get_tb();
501 iSeries_recal_titan = HvCallXm_loadTod();
502}
503#endif /* CONFIG_PPC_ISERIES */
1da177e4 504
e360adbe 505#ifdef CONFIG_IRQ_WORK
105988c0 506
0fe1ac48
PM
507/*
508 * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable...
509 */
510#ifdef CONFIG_PPC64
e360adbe 511static inline unsigned long test_irq_work_pending(void)
105988c0 512{
0fe1ac48
PM
513 unsigned long x;
514
515 asm volatile("lbz %0,%1(13)"
516 : "=r" (x)
e360adbe 517 : "i" (offsetof(struct paca_struct, irq_work_pending)));
0fe1ac48
PM
518 return x;
519}
520
e360adbe 521static inline void set_irq_work_pending_flag(void)
0fe1ac48
PM
522{
523 asm volatile("stb %0,%1(13)" : :
524 "r" (1),
e360adbe 525 "i" (offsetof(struct paca_struct, irq_work_pending)));
0fe1ac48
PM
526}
527
e360adbe 528static inline void clear_irq_work_pending(void)
0fe1ac48
PM
529{
530 asm volatile("stb %0,%1(13)" : :
531 "r" (0),
e360adbe 532 "i" (offsetof(struct paca_struct, irq_work_pending)));
105988c0
PM
533}
534
0fe1ac48
PM
535#else /* 32-bit */
536
e360adbe 537DEFINE_PER_CPU(u8, irq_work_pending);
0fe1ac48 538
e360adbe
PZ
539#define set_irq_work_pending_flag() __get_cpu_var(irq_work_pending) = 1
540#define test_irq_work_pending() __get_cpu_var(irq_work_pending)
541#define clear_irq_work_pending() __get_cpu_var(irq_work_pending) = 0
105988c0 542
0fe1ac48
PM
543#endif /* 32 vs 64 bit */
544
4f8b50bb 545void arch_irq_work_raise(void)
0fe1ac48
PM
546{
547 preempt_disable();
e360adbe 548 set_irq_work_pending_flag();
0fe1ac48
PM
549 set_dec(1);
550 preempt_enable();
551}
552
e360adbe 553#else /* CONFIG_IRQ_WORK */
105988c0 554
e360adbe
PZ
555#define test_irq_work_pending() 0
556#define clear_irq_work_pending()
105988c0 557
e360adbe 558#endif /* CONFIG_IRQ_WORK */
105988c0 559
1da177e4
LT
560/*
561 * For iSeries shared processors, we have to let the hypervisor
562 * set the hardware decrementer. We set a virtual decrementer
563 * in the lppaca and call the hypervisor if the virtual
564 * decrementer is less than the current value in the hardware
565 * decrementer. (almost always the new decrementer value will
566 * be greater than the current hardware decementer so the hypervisor
567 * call will not be needed)
568 */
569
1da177e4
LT
570/*
571 * timer_interrupt - gets called when the decrementer overflows,
572 * with interrupts disabled.
573 */
c7aeffc4 574void timer_interrupt(struct pt_regs * regs)
1da177e4 575{
7d12e780 576 struct pt_regs *old_regs;
6e6b44e8
MM
577 struct decrementer_clock *decrementer = &__get_cpu_var(decrementers);
578 struct clock_event_device *evt = &decrementer->event;
d968014b 579 u64 now;
d831d0b8 580
963e5d3b
BH
581 /* Ensure a positive value is written to the decrementer, or else
582 * some CPUs will continue to take decrementer exceptions.
583 */
584 set_dec(DECREMENTER_MAX);
585
586 /* Some implementations of hotplug will get timer interrupts while
587 * offline, just ignore these
588 */
589 if (!cpu_online(smp_processor_id()))
590 return;
591
6795b85c
AB
592 trace_timer_interrupt_entry(regs);
593
89713ed1
AB
594 __get_cpu_var(irq_stat).timer_irqs++;
595
b0d278b7 596#if defined(CONFIG_PPC32) && defined(CONFIG_PMAC)
f2783c15
PM
597 if (atomic_read(&ppc_n_lost_interrupts) != 0)
598 do_IRQ(regs);
599#endif
1da177e4 600
7d12e780 601 old_regs = set_irq_regs(regs);
1da177e4
LT
602 irq_enter();
603
e360adbe
PZ
604 if (test_irq_work_pending()) {
605 clear_irq_work_pending();
606 irq_work_run();
0fe1ac48
PM
607 }
608
f2783c15 609#ifdef CONFIG_PPC_ISERIES
501b6d29
SR
610 if (firmware_has_feature(FW_FEATURE_ISERIES))
611 get_lppaca()->int_dword.fields.decr_int = 0;
f2783c15
PM
612#endif
613
b0d278b7
PM
614 now = get_tb_or_rtc();
615 if (now >= decrementer->next_tb) {
616 decrementer->next_tb = ~(u64)0;
617 if (evt->event_handler)
618 evt->event_handler(evt);
619 } else {
620 now = decrementer->next_tb - now;
621 if (now <= DECREMENTER_MAX)
622 set_dec((int)now);
623 }
1da177e4
LT
624
625#ifdef CONFIG_PPC_ISERIES
501b6d29 626 if (firmware_has_feature(FW_FEATURE_ISERIES) && hvlpevent_is_pending())
35a84c2f 627 process_hvlpevents();
1da177e4
LT
628#endif
629
f2783c15 630#ifdef CONFIG_PPC64
8d15a3e5 631 /* collect purr register values often, for accurate calculations */
1ababe11 632 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
1da177e4
LT
633 struct cpu_usage *cu = &__get_cpu_var(cpu_usage_array);
634 cu->current_tb = mfspr(SPRN_PURR);
635 }
f2783c15 636#endif
1da177e4
LT
637
638 irq_exit();
7d12e780 639 set_irq_regs(old_regs);
6795b85c
AB
640
641 trace_timer_interrupt_exit(regs);
1da177e4
LT
642}
643
7ac5dde9 644#ifdef CONFIG_SUSPEND
d75d68cf 645static void generic_suspend_disable_irqs(void)
7ac5dde9 646{
7ac5dde9
SW
647 /* Disable the decrementer, so that it doesn't interfere
648 * with suspending.
649 */
650
651 set_dec(0x7fffffff);
652 local_irq_disable();
653 set_dec(0x7fffffff);
654}
655
d75d68cf 656static void generic_suspend_enable_irqs(void)
7ac5dde9 657{
7ac5dde9 658 local_irq_enable();
7ac5dde9
SW
659}
660
661/* Overrides the weak version in kernel/power/main.c */
662void arch_suspend_disable_irqs(void)
663{
664 if (ppc_md.suspend_disable_irqs)
665 ppc_md.suspend_disable_irqs();
666 generic_suspend_disable_irqs();
667}
668
669/* Overrides the weak version in kernel/power/main.c */
670void arch_suspend_enable_irqs(void)
671{
672 generic_suspend_enable_irqs();
673 if (ppc_md.suspend_enable_irqs)
674 ppc_md.suspend_enable_irqs();
675}
676#endif
677
1da177e4
LT
678/*
679 * Scheduler clock - returns current time in nanosec units.
680 *
681 * Note: mulhdu(a, b) (multiply high double unsigned) returns
682 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
683 * are 64-bit unsigned numbers.
684 */
685unsigned long long sched_clock(void)
686{
96c44507
PM
687 if (__USE_RTC())
688 return get_rtc();
fc9069fe 689 return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
1da177e4
LT
690}
691
0bb474a4 692static int __init get_freq(char *name, int cells, unsigned long *val)
10f7e7c1
AB
693{
694 struct device_node *cpu;
a7f67bdf 695 const unsigned int *fp;
0bb474a4 696 int found = 0;
10f7e7c1 697
0bb474a4 698 /* The cpu node should have timebase and clock frequency properties */
10f7e7c1
AB
699 cpu = of_find_node_by_type(NULL, "cpu");
700
d8a8188d 701 if (cpu) {
e2eb6392 702 fp = of_get_property(cpu, name, NULL);
d8a8188d 703 if (fp) {
0bb474a4 704 found = 1;
a4dc7ff0 705 *val = of_read_ulong(fp, cells);
10f7e7c1 706 }
0bb474a4
AB
707
708 of_node_put(cpu);
10f7e7c1 709 }
0bb474a4
AB
710
711 return found;
712}
713
77c0a700
BH
714/* should become __cpuinit when secondary_cpu_time_init also is */
715void start_cpu_decrementer(void)
716{
717#if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
718 /* Clear any pending timer interrupts */
719 mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
720
721 /* Enable decrementer interrupt */
722 mtspr(SPRN_TCR, TCR_DIE);
723#endif /* defined(CONFIG_BOOKE) || defined(CONFIG_40x) */
724}
725
0bb474a4
AB
726void __init generic_calibrate_decr(void)
727{
728 ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
729
730 if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
731 !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
732
10f7e7c1
AB
733 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
734 "(not found)\n");
0bb474a4 735 }
10f7e7c1 736
0bb474a4
AB
737 ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */
738
739 if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
740 !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
741
742 printk(KERN_ERR "WARNING: Estimating processor frequency "
743 "(not found)\n");
10f7e7c1 744 }
10f7e7c1 745}
10f7e7c1 746
aa3be5f3 747int update_persistent_clock(struct timespec now)
f2783c15
PM
748{
749 struct rtc_time tm;
750
aa3be5f3
TB
751 if (!ppc_md.set_rtc_time)
752 return 0;
753
754 to_tm(now.tv_sec + 1 + timezone_offset, &tm);
755 tm.tm_year -= 1900;
756 tm.tm_mon -= 1;
757
758 return ppc_md.set_rtc_time(&tm);
759}
760
978d7eb3 761static void __read_persistent_clock(struct timespec *ts)
aa3be5f3
TB
762{
763 struct rtc_time tm;
764 static int first = 1;
765
d90246cd 766 ts->tv_nsec = 0;
aa3be5f3
TB
767 /* XXX this is a litle fragile but will work okay in the short term */
768 if (first) {
769 first = 0;
770 if (ppc_md.time_init)
771 timezone_offset = ppc_md.time_init();
772
773 /* get_boot_time() isn't guaranteed to be safe to call late */
d90246cd
MS
774 if (ppc_md.get_boot_time) {
775 ts->tv_sec = ppc_md.get_boot_time() - timezone_offset;
776 return;
777 }
778 }
779 if (!ppc_md.get_rtc_time) {
780 ts->tv_sec = 0;
781 return;
aa3be5f3 782 }
f2783c15 783 ppc_md.get_rtc_time(&tm);
978d7eb3 784
d4f587c6
MS
785 ts->tv_sec = mktime(tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday,
786 tm.tm_hour, tm.tm_min, tm.tm_sec);
f2783c15
PM
787}
788
978d7eb3
BH
789void read_persistent_clock(struct timespec *ts)
790{
791 __read_persistent_clock(ts);
792
793 /* Sanitize it in case real time clock is set below EPOCH */
794 if (ts->tv_sec < 0) {
795 ts->tv_sec = 0;
796 ts->tv_nsec = 0;
797 }
798
799}
800
4a4cfe38 801/* clocksource code */
8e19608e 802static cycle_t rtc_read(struct clocksource *cs)
4a4cfe38
TB
803{
804 return (cycle_t)get_rtc();
805}
806
8e19608e 807static cycle_t timebase_read(struct clocksource *cs)
4a4cfe38
TB
808{
809 return (cycle_t)get_tb();
810}
811
7615856e
JS
812void update_vsyscall(struct timespec *wall_time, struct timespec *wtm,
813 struct clocksource *clock, u32 mult)
4a4cfe38 814{
b0797b60 815 u64 new_tb_to_xs, new_stamp_xsec;
47916be4 816 u32 frac_sec;
4a4cfe38
TB
817
818 if (clock != &clocksource_timebase)
819 return;
820
821 /* Make userspace gettimeofday spin until we're done. */
822 ++vdso_data->tb_update_count;
823 smp_mb();
824
825 /* XXX this assumes clock->shift == 22 */
826 /* 4611686018 ~= 2^(20+64-22) / 1e9 */
b0797b60 827 new_tb_to_xs = (u64) mult * 4611686018ULL;
06d518e3 828 new_stamp_xsec = (u64) wall_time->tv_nsec * XSEC_PER_SEC;
b0797b60 829 do_div(new_stamp_xsec, 1000000000);
06d518e3 830 new_stamp_xsec += (u64) wall_time->tv_sec * XSEC_PER_SEC;
b0797b60 831
47916be4
TG
832 BUG_ON(wall_time->tv_nsec >= NSEC_PER_SEC);
833 /* this is tv_nsec / 1e9 as a 0.32 fraction */
834 frac_sec = ((u64) wall_time->tv_nsec * 18446744073ULL) >> 32;
835
b0797b60
JS
836 /*
837 * tb_update_count is used to allow the userspace gettimeofday code
838 * to assure itself that it sees a consistent view of the tb_to_xs and
839 * stamp_xsec variables. It reads the tb_update_count, then reads
840 * tb_to_xs and stamp_xsec and then reads tb_update_count again. If
841 * the two values of tb_update_count match and are even then the
842 * tb_to_xs and stamp_xsec values are consistent. If not, then it
843 * loops back and reads them again until this criteria is met.
844 * We expect the caller to have done the first increment of
845 * vdso_data->tb_update_count already.
846 */
847 vdso_data->tb_orig_stamp = clock->cycle_last;
848 vdso_data->stamp_xsec = new_stamp_xsec;
849 vdso_data->tb_to_xs = new_tb_to_xs;
7615856e
JS
850 vdso_data->wtom_clock_sec = wtm->tv_sec;
851 vdso_data->wtom_clock_nsec = wtm->tv_nsec;
06d518e3 852 vdso_data->stamp_xtime = *wall_time;
0e469db8 853 vdso_data->stamp_sec_fraction = frac_sec;
b0797b60
JS
854 smp_wmb();
855 ++(vdso_data->tb_update_count);
4a4cfe38
TB
856}
857
858void update_vsyscall_tz(void)
859{
860 /* Make userspace gettimeofday spin until we're done. */
861 ++vdso_data->tb_update_count;
862 smp_mb();
863 vdso_data->tz_minuteswest = sys_tz.tz_minuteswest;
864 vdso_data->tz_dsttime = sys_tz.tz_dsttime;
865 smp_mb();
866 ++vdso_data->tb_update_count;
867}
868
1c21a293 869static void __init clocksource_init(void)
4a4cfe38
TB
870{
871 struct clocksource *clock;
872
873 if (__USE_RTC())
874 clock = &clocksource_rtc;
875 else
876 clock = &clocksource_timebase;
877
878 clock->mult = clocksource_hz2mult(tb_ticks_per_sec, clock->shift);
879
880 if (clocksource_register(clock)) {
881 printk(KERN_ERR "clocksource: %s is already registered\n",
882 clock->name);
883 return;
884 }
885
886 printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
887 clock->name, clock->mult, clock->shift);
888}
889
37fb9a02
AB
890void decrementer_check_overflow(void)
891{
892 u64 now = get_tb_or_rtc();
893 struct decrementer_clock *decrementer = &__get_cpu_var(decrementers);
894
895 if (now >= decrementer->next_tb)
896 set_dec(1);
897}
898
d831d0b8
TB
899static int decrementer_set_next_event(unsigned long evt,
900 struct clock_event_device *dev)
901{
6e6b44e8 902 __get_cpu_var(decrementers).next_tb = get_tb_or_rtc() + evt;
d831d0b8
TB
903 set_dec(evt);
904 return 0;
905}
906
907static void decrementer_set_mode(enum clock_event_mode mode,
908 struct clock_event_device *dev)
909{
910 if (mode != CLOCK_EVT_MODE_ONESHOT)
911 decrementer_set_next_event(DECREMENTER_MAX, dev);
912}
913
914static void register_decrementer_clockevent(int cpu)
915{
6e6b44e8 916 struct clock_event_device *dec = &per_cpu(decrementers, cpu).event;
d831d0b8
TB
917
918 *dec = decrementer_clockevent;
320ab2b0 919 dec->cpumask = cpumask_of(cpu);
d831d0b8 920
b919ee82
AB
921 printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
922 dec->name, dec->mult, dec->shift, cpu);
d831d0b8
TB
923
924 clockevents_register_device(dec);
925}
926
c481887f 927static void __init init_decrementer_clockevent(void)
d831d0b8
TB
928{
929 int cpu = smp_processor_id();
930
d8afc6fd
AB
931 clockevents_calc_mult_shift(&decrementer_clockevent, ppc_tb_freq, 4);
932
d831d0b8
TB
933 decrementer_clockevent.max_delta_ns =
934 clockevent_delta2ns(DECREMENTER_MAX, &decrementer_clockevent);
43875cc0
PM
935 decrementer_clockevent.min_delta_ns =
936 clockevent_delta2ns(2, &decrementer_clockevent);
d831d0b8
TB
937
938 register_decrementer_clockevent(cpu);
939}
940
941void secondary_cpu_time_init(void)
942{
77c0a700
BH
943 /* Start the decrementer on CPUs that have manual control
944 * such as BookE
945 */
946 start_cpu_decrementer();
947
d831d0b8
TB
948 /* FIME: Should make unrelatred change to move snapshot_timebase
949 * call here ! */
950 register_decrementer_clockevent(smp_processor_id());
951}
952
f2783c15 953/* This function is only called on the boot processor */
1da177e4
LT
954void __init time_init(void)
955{
1da177e4 956 struct div_result res;
d75d68cf 957 u64 scale;
f2783c15
PM
958 unsigned shift;
959
96c44507
PM
960 if (__USE_RTC()) {
961 /* 601 processor: dec counts down by 128 every 128ns */
962 ppc_tb_freq = 1000000000;
96c44507
PM
963 } else {
964 /* Normal PowerPC with timebase register */
965 ppc_md.calibrate_decr();
224ad80a 966 printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
96c44507 967 ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
224ad80a 968 printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n",
96c44507 969 ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
96c44507 970 }
374e99d4
PM
971
972 tb_ticks_per_jiffy = ppc_tb_freq / HZ;
092b8f34 973 tb_ticks_per_sec = ppc_tb_freq;
374e99d4 974 tb_ticks_per_usec = ppc_tb_freq / 1000000;
c6622f63 975 calc_cputime_factors();
a42548a1 976 setup_cputime_one_jiffy();
092b8f34 977
1da177e4
LT
978 /*
979 * Compute scale factor for sched_clock.
980 * The calibrate_decr() function has set tb_ticks_per_sec,
981 * which is the timebase frequency.
982 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
983 * the 128-bit result as a 64.64 fixed-point number.
984 * We then shift that number right until it is less than 1.0,
985 * giving us the scale factor and shift count to use in
986 * sched_clock().
987 */
988 div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
989 scale = res.result_low;
990 for (shift = 0; res.result_high != 0; ++shift) {
991 scale = (scale >> 1) | (res.result_high << 63);
992 res.result_high >>= 1;
993 }
994 tb_to_ns_scale = scale;
995 tb_to_ns_shift = shift;
fc9069fe 996 /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
c27da339 997 boot_tb = get_tb_or_rtc();
1da177e4 998
092b8f34
PM
999 /* If platform provided a timezone (pmac), we correct the time */
1000 if (timezone_offset) {
1001 sys_tz.tz_minuteswest = -timezone_offset / 60;
1002 sys_tz.tz_dsttime = 0;
092b8f34
PM
1003 }
1004
a7f290da
BH
1005 vdso_data->tb_update_count = 0;
1006 vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
1da177e4 1007
77c0a700
BH
1008 /* Start the decrementer on CPUs that have manual control
1009 * such as BookE
1010 */
1011 start_cpu_decrementer();
1012
4a4cfe38
TB
1013 /* Register the clocksource, if we're not running on iSeries */
1014 if (!firmware_has_feature(FW_FEATURE_ISERIES))
1015 clocksource_init();
1016
d831d0b8 1017 init_decrementer_clockevent();
1da177e4
LT
1018}
1019
1da177e4 1020
1da177e4
LT
1021#define FEBRUARY 2
1022#define STARTOFTIME 1970
1023#define SECDAY 86400L
1024#define SECYR (SECDAY * 365)
f2783c15
PM
1025#define leapyear(year) ((year) % 4 == 0 && \
1026 ((year) % 100 != 0 || (year) % 400 == 0))
1da177e4
LT
1027#define days_in_year(a) (leapyear(a) ? 366 : 365)
1028#define days_in_month(a) (month_days[(a) - 1])
1029
1030static int month_days[12] = {
1031 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
1032};
1033
1034/*
1035 * This only works for the Gregorian calendar - i.e. after 1752 (in the UK)
1036 */
1037void GregorianDay(struct rtc_time * tm)
1038{
1039 int leapsToDate;
1040 int lastYear;
1041 int day;
1042 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
1043
f2783c15 1044 lastYear = tm->tm_year - 1;
1da177e4
LT
1045
1046 /*
1047 * Number of leap corrections to apply up to end of last year
1048 */
f2783c15 1049 leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
1da177e4
LT
1050
1051 /*
1052 * This year is a leap year if it is divisible by 4 except when it is
1053 * divisible by 100 unless it is divisible by 400
1054 *
f2783c15 1055 * e.g. 1904 was a leap year, 1900 was not, 1996 is, and 2000 was
1da177e4 1056 */
f2783c15 1057 day = tm->tm_mon > 2 && leapyear(tm->tm_year);
1da177e4
LT
1058
1059 day += lastYear*365 + leapsToDate + MonthOffset[tm->tm_mon-1] +
1060 tm->tm_mday;
1061
f2783c15 1062 tm->tm_wday = day % 7;
1da177e4
LT
1063}
1064
1065void to_tm(int tim, struct rtc_time * tm)
1066{
1067 register int i;
1068 register long hms, day;
1069
1070 day = tim / SECDAY;
1071 hms = tim % SECDAY;
1072
1073 /* Hours, minutes, seconds are easy */
1074 tm->tm_hour = hms / 3600;
1075 tm->tm_min = (hms % 3600) / 60;
1076 tm->tm_sec = (hms % 3600) % 60;
1077
1078 /* Number of years in days */
1079 for (i = STARTOFTIME; day >= days_in_year(i); i++)
1080 day -= days_in_year(i);
1081 tm->tm_year = i;
1082
1083 /* Number of months in days left */
1084 if (leapyear(tm->tm_year))
1085 days_in_month(FEBRUARY) = 29;
1086 for (i = 1; day >= days_in_month(i); i++)
1087 day -= days_in_month(i);
1088 days_in_month(FEBRUARY) = 28;
1089 tm->tm_mon = i;
1090
1091 /* Days are what is left over (+1) from all that. */
1092 tm->tm_mday = day + 1;
1093
1094 /*
1095 * Determine the day of week
1096 */
1097 GregorianDay(tm);
1098}
1099
1da177e4
LT
1100/*
1101 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
1102 * result.
1103 */
f2783c15
PM
1104void div128_by_32(u64 dividend_high, u64 dividend_low,
1105 unsigned divisor, struct div_result *dr)
1da177e4 1106{
f2783c15
PM
1107 unsigned long a, b, c, d;
1108 unsigned long w, x, y, z;
1109 u64 ra, rb, rc;
1da177e4
LT
1110
1111 a = dividend_high >> 32;
1112 b = dividend_high & 0xffffffff;
1113 c = dividend_low >> 32;
1114 d = dividend_low & 0xffffffff;
1115
f2783c15
PM
1116 w = a / divisor;
1117 ra = ((u64)(a - (w * divisor)) << 32) + b;
1118
f2783c15
PM
1119 rb = ((u64) do_div(ra, divisor) << 32) + c;
1120 x = ra;
1da177e4 1121
f2783c15
PM
1122 rc = ((u64) do_div(rb, divisor) << 32) + d;
1123 y = rb;
1124
1125 do_div(rc, divisor);
1126 z = rc;
1da177e4 1127
f2783c15
PM
1128 dr->result_high = ((u64)w << 32) + x;
1129 dr->result_low = ((u64)y << 32) + z;
1da177e4
LT
1130
1131}
bcd68a70 1132
177996e6
BH
1133/* We don't need to calibrate delay, we use the CPU timebase for that */
1134void calibrate_delay(void)
1135{
1136 /* Some generic code (such as spinlock debug) use loops_per_jiffy
1137 * as the number of __delay(1) in a jiffy, so make it so
1138 */
1139 loops_per_jiffy = tb_ticks_per_jiffy;
1140}
1141
bcd68a70
GU
1142static int __init rtc_init(void)
1143{
1144 struct platform_device *pdev;
1145
1146 if (!ppc_md.get_rtc_time)
1147 return -ENODEV;
1148
1149 pdev = platform_device_register_simple("rtc-generic", -1, NULL, 0);
1150 if (IS_ERR(pdev))
1151 return PTR_ERR(pdev);
1152
1153 return 0;
1154}
1155
1156module_init(rtc_init);
This page took 0.675269 seconds and 5 git commands to generate.