34 #include <linux/errno.h>
35 #include <linux/export.h>
36 #include <linux/sched.h>
37 #include <linux/kernel.h>
39 #include <linux/string.h>
42 #include <linux/timex.h>
44 #include <linux/time.h>
50 #include <linux/rtc.h>
56 #include <asm/trace.h>
59 #include <asm/processor.h>
60 #include <asm/nvram.h>
61 #include <asm/cache.h>
62 #include <asm/machdep.h>
63 #include <asm/uaccess.h>
67 #include <asm/div64.h>
69 #include <asm/vdso_datapage.h>
71 #include <asm/cputime.h>
93 .read = timebase_read,
96 #define DECREMENTER_MAX 0x7fffffff
98 static int decrementer_set_next_event(
unsigned long evt,
99 struct clock_event_device *
dev);
100 static void decrementer_set_mode(
enum clock_event_mode
mode,
101 struct clock_event_device *
dev);
104 .name =
"decrementer",
107 .set_next_event = decrementer_set_next_event,
108 .set_mode = decrementer_set_mode,
109 .features = CLOCK_EVT_FEAT_ONESHOT,
116 #define XSEC_PER_SEC (1024*1024)
119 #define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC)
122 #define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max)
139 static long timezone_offset;
146 #ifdef CONFIG_VIRT_CPU_ACCOUNTING
152 u64 __cputime_jiffies_factor;
154 u64 __cputime_usec_factor;
156 u64 __cputime_sec_factor;
158 u64 __cputime_clockt_factor;
165 void (*dtl_consumer)(
struct dtl_entry *,
u64);
169 struct div_result
res;
172 __cputime_jiffies_factor =
res.result_low;
174 __cputime_usec_factor =
res.result_low;
176 __cputime_sec_factor =
res.result_low;
178 __cputime_clockt_factor =
res.result_low;
188 return mfspr(SPRN_SPURR);
190 return mfspr(SPRN_PURR);
194 #ifdef CONFIG_PPC_SPLPAR
200 static u64 scan_dispatch_log(
u64 stop_tb)
202 u64 i = local_paca->dtl_ridx;
203 struct dtl_entry *
dtl = local_paca->dtl_curr;
204 struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
205 struct lppaca *vpa = local_paca->lppaca_ptr;
213 if (i == vpa->dtl_idx)
215 while (i < vpa->dtl_idx) {
217 dtl_consumer(dtl, i);
219 tb_delta = dtl->enqueue_to_dispatch_time +
220 dtl->ready_to_enqueue_time;
222 if (i + N_DISPATCH_LOG < vpa->dtl_idx) {
224 i = vpa->dtl_idx - N_DISPATCH_LOG;
225 dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
234 dtl = local_paca->dispatch_log;
236 local_paca->dtl_ridx =
i;
237 local_paca->dtl_curr = dtl;
245 void accumulate_stolen_time(
void)
249 u8 save_soft_enabled = local_paca->soft_enabled;
257 local_paca->soft_enabled = 0;
259 sst = scan_dispatch_log(local_paca->starttime_user);
260 ust = scan_dispatch_log(local_paca->starttime);
261 local_paca->system_time -= sst;
262 local_paca->user_time -= ust;
263 local_paca->stolen_time += ust + sst;
265 local_paca->soft_enabled = save_soft_enabled;
268 static inline u64 calculate_stolen_time(
u64 stop_tb)
272 if (get_paca()->dtl_ridx != get_paca()->lppaca_ptr->dtl_idx) {
273 stolen = scan_dispatch_log(stop_tb);
274 get_paca()->system_time -= stolen;
277 stolen += get_paca()->stolen_time;
278 get_paca()->stolen_time = 0;
283 static inline u64 calculate_stolen_time(
u64 stop_tb)
295 u64 *sys_scaled,
u64 *stolen)
297 u64 now, nowscaled, deltascaled;
301 nowscaled = read_spurr(now);
302 get_paca()->system_time += now - get_paca()->starttime;
303 get_paca()->starttime = now;
304 deltascaled = nowscaled - get_paca()->startspurr;
305 get_paca()->startspurr = nowscaled;
307 *stolen = calculate_stolen_time(now);
309 delta = get_paca()->system_time;
310 get_paca()->system_time = 0;
311 udelta = get_paca()->user_time - get_paca()->utime_sspurr;
312 get_paca()->utime_sspurr = get_paca()->user_time;
325 user_scaled = udelta;
326 if (deltascaled != delta + udelta) {
328 *sys_scaled = deltascaled * delta / (delta + udelta);
329 user_scaled = deltascaled - *sys_scaled;
331 *sys_scaled = deltascaled;
334 get_paca()->user_time_scaled += user_scaled;
343 delta = vtime_delta(tsk, &sys_scaled, &stolen);
353 delta = vtime_delta(tsk, &sys_scaled, &stolen);
370 utime = get_paca()->user_time;
371 utimescaled = get_paca()->user_time_scaled;
372 get_paca()->user_time = 0;
373 get_paca()->user_time_scaled = 0;
374 get_paca()->utime_sspurr = 0;
385 #define calc_cputime_factors()
397 diff = get_rtcl() -
start;
400 }
while (diff < loops);
403 while (get_tbl() - start < loops)
429 #ifdef CONFIG_IRQ_WORK
439 asm volatile(
"lbz %0,%1(13)"
441 :
"i" (
offsetof(
struct paca_struct, irq_work_pending)));
445 static inline void set_irq_work_pending_flag(
void)
447 asm volatile(
"stb %0,%1(13)" : :
449 "i" (
offsetof(
struct paca_struct, irq_work_pending)));
454 asm volatile(
"stb %0,%1(13)" : :
456 "i" (
offsetof(
struct paca_struct, irq_work_pending)));
463 #define set_irq_work_pending_flag() __get_cpu_var(irq_work_pending) = 1
464 #define test_irq_work_pending() __get_cpu_var(irq_work_pending)
465 #define clear_irq_work_pending() __get_cpu_var(irq_work_pending) = 0
472 set_irq_work_pending_flag();
479 #define test_irq_work_pending() 0
480 #define clear_irq_work_pending()
509 may_hard_irq_enable();
513 #if defined(CONFIG_PPC32) && defined(CONFIG_PMAC)
518 old_regs = set_irq_regs(regs);
521 trace_timer_interrupt_entry(regs);
528 now = get_tb_or_rtc();
529 if (now >= *next_tb) {
531 if (evt->event_handler)
532 evt->event_handler(evt);
534 now = *next_tb - now;
541 if (firmware_has_feature(FW_FEATURE_SPLPAR)) {
543 cu->current_tb =
mfspr(SPRN_PURR);
547 trace_timer_interrupt_exit(regs);
550 set_irq_regs(old_regs);
562 #ifdef CONFIG_SUSPEND
563 static void generic_suspend_disable_irqs(
void)
574 static void generic_suspend_enable_irqs(
void)
582 if (
ppc_md.suspend_disable_irqs)
583 ppc_md.suspend_disable_irqs();
584 generic_suspend_disable_irqs();
590 generic_suspend_enable_irqs();
591 if (
ppc_md.suspend_enable_irqs)
592 ppc_md.suspend_enable_irqs();
607 return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
610 static int __init get_freq(
char *
name,
int cells,
unsigned long *
val)
613 const unsigned int *
fp;
623 *val = of_read_ulong(fp, cells);
635 #if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
637 mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
640 mtspr(SPRN_TCR, TCR_DIE);
648 if (!get_freq(
"ibm,extended-timebase-frequency", 2, &
ppc_tb_freq) &&
649 !get_freq(
"timebase-frequency", 1, &
ppc_tb_freq)) {
657 if (!get_freq(
"ibm,extended-clock-frequency", 2, &
ppc_proc_freq) &&
676 return ppc_md.set_rtc_time(&tm);
679 static void __read_persistent_clock(
struct timespec *
ts)
682 static int first = 1;
689 timezone_offset =
ppc_md.time_init();
692 if (
ppc_md.get_boot_time) {
697 if (!
ppc_md.get_rtc_time) {
709 __read_persistent_clock(ts);
733 u64 new_tb_to_xs, new_stamp_xsec;
736 if (clock != &clocksource_timebase)
744 new_tb_to_xs = (
u64) mult * (19342813113834067ULL >> clock->
shift);
746 do_div(new_stamp_xsec, 1000000000);
751 frac_sec = ((
u64) wall_time->
tv_nsec * 18446744073ULL) >> 32;
770 vdso_data->stamp_sec_fraction = frac_sec;
786 static void __init clocksource_init(
void)
791 clock = &clocksource_rtc;
793 clock = &clocksource_timebase;
805 static int decrementer_set_next_event(
unsigned long evt,
806 struct clock_event_device *
dev)
813 static void decrementer_set_mode(
enum clock_event_mode
mode,
814 struct clock_event_device *
dev)
816 if (mode != CLOCK_EVT_MODE_ONESHOT)
820 static void register_decrementer_clockevent(
int cpu)
822 struct clock_event_device *dec = &
per_cpu(decrementers, cpu);
828 dec->name, dec->mult, dec->shift, cpu);
833 static void __init init_decrementer_clockevent(
void)
844 register_decrementer_clockevent(cpu);
862 struct div_result res;
882 setup_cputime_one_jiffy();
895 scale = res.result_low;
896 for (shift = 0; res.result_high != 0; ++shift) {
897 scale = (scale >> 1) | (res.result_high << 63);
898 res.result_high >>= 1;
900 tb_to_ns_scale = scale;
901 tb_to_ns_shift = shift;
903 boot_tb = get_tb_or_rtc();
906 if (timezone_offset) {
907 sys_tz.tz_minuteswest = -timezone_offset / 60;
922 init_decrementer_clockevent();
927 #define STARTOFTIME 1970
928 #define SECDAY 86400L
929 #define SECYR (SECDAY * 365)
930 #define leapyear(year) ((year) % 4 == 0 && \
931 ((year) % 100 != 0 || (year) % 400 == 0))
932 #define days_in_year(a) (leapyear(a) ? 366 : 365)
933 #define days_in_month(a) (month_days[(a) - 1])
935 static int month_days[12] = {
936 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
947 int MonthOffset[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
954 leapsToDate = lastYear / 4 - lastYear / 100 + lastYear / 400;
964 day += lastYear*365 + leapsToDate + MonthOffset[tm->
tm_mon-1] +
973 register long hms,
day;
980 tm->
tm_min = (hms % 3600) / 60;
981 tm->
tm_sec = (hms % 3600) % 60;
1010 unsigned divisor,
struct div_result *dr)
1012 unsigned long a,
b,
c,
d;
1013 unsigned long w,
x,
y, z;
1016 a = dividend_high >> 32;
1017 b = dividend_high & 0xffffffff;
1018 c = dividend_low >> 32;
1019 d = dividend_low & 0xffffffff;
1022 ra = ((
u64)(a - (w * divisor)) << 32) +
b;
1024 rb = ((
u64)
do_div(ra, divisor) << 32) + c;
1027 rc = ((
u64)
do_div(rb, divisor) << 32) + d;
1033 dr->result_high = ((
u64)w << 32) +
x;
1034 dr->result_low = ((
u64)y << 32) + z;
1047 static int __init rtc_init(
void)
1051 if (!
ppc_md.get_rtc_time)
1054 pdev = platform_device_register_simple(
"rtc-generic", -1,
NULL, 0);
1056 return PTR_ERR(pdev);