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