1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <linux/errno.h>
31#include <linux/export.h>
32#include <linux/sched.h>
33#include <linux/sched/clock.h>
34#include <linux/sched/cputime.h>
35#include <linux/kernel.h>
36#include <linux/param.h>
37#include <linux/string.h>
38#include <linux/mm.h>
39#include <linux/interrupt.h>
40#include <linux/timex.h>
41#include <linux/kernel_stat.h>
42#include <linux/time.h>
43#include <linux/init.h>
44#include <linux/profile.h>
45#include <linux/cpu.h>
46#include <linux/security.h>
47#include <linux/percpu.h>
48#include <linux/rtc.h>
49#include <linux/jiffies.h>
50#include <linux/posix-timers.h>
51#include <linux/irq.h>
52#include <linux/delay.h>
53#include <linux/irq_work.h>
54#include <linux/of_clk.h>
55#include <linux/suspend.h>
56#include <linux/processor.h>
57#include <asm/trace.h>
58
59#include <asm/interrupt.h>
60#include <asm/io.h>
61#include <asm/nvram.h>
62#include <asm/cache.h>
63#include <asm/machdep.h>
64#include <linux/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/asm-prototypes.h>
73
74
75
76#include <linux/clockchips.h>
77#include <linux/timekeeper_internal.h>
78
79static u64 timebase_read(struct clocksource *);
80static struct clocksource clocksource_timebase = {
81 .name = "timebase",
82 .rating = 400,
83 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
84 .mask = CLOCKSOURCE_MASK(64),
85 .read = timebase_read,
86 .vdso_clock_mode = VDSO_CLOCKMODE_ARCHTIMER,
87};
88
89#define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF
90u64 decrementer_max = DECREMENTER_DEFAULT_MAX;
91
92static int decrementer_set_next_event(unsigned long evt,
93 struct clock_event_device *dev);
94static int decrementer_shutdown(struct clock_event_device *evt);
95
96struct clock_event_device decrementer_clockevent = {
97 .name = "decrementer",
98 .rating = 200,
99 .irq = 0,
100 .set_next_event = decrementer_set_next_event,
101 .set_state_oneshot_stopped = decrementer_shutdown,
102 .set_state_shutdown = decrementer_shutdown,
103 .tick_resume = decrementer_shutdown,
104 .features = CLOCK_EVT_FEAT_ONESHOT |
105 CLOCK_EVT_FEAT_C3STOP,
106};
107EXPORT_SYMBOL(decrementer_clockevent);
108
109DEFINE_PER_CPU(u64, decrementers_next_tb);
110static DEFINE_PER_CPU(struct clock_event_device, decrementers);
111
112#define XSEC_PER_SEC (1024*1024)
113
114#ifdef CONFIG_PPC64
115#define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC)
116#else
117
118#define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max)
119#endif
120
121unsigned long tb_ticks_per_jiffy;
122unsigned long tb_ticks_per_usec = 100;
123EXPORT_SYMBOL(tb_ticks_per_usec);
124unsigned long tb_ticks_per_sec;
125EXPORT_SYMBOL(tb_ticks_per_sec);
126
127DEFINE_SPINLOCK(rtc_lock);
128EXPORT_SYMBOL_GPL(rtc_lock);
129
130static u64 tb_to_ns_scale __read_mostly;
131static unsigned tb_to_ns_shift __read_mostly;
132static u64 boot_tb __read_mostly;
133
134extern struct timezone sys_tz;
135static long timezone_offset;
136
137unsigned long ppc_proc_freq;
138EXPORT_SYMBOL_GPL(ppc_proc_freq);
139unsigned long ppc_tb_freq;
140EXPORT_SYMBOL_GPL(ppc_tb_freq);
141
142bool tb_invalid;
143
144#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
145
146
147
148
149u64 __cputime_usec_factor;
150EXPORT_SYMBOL(__cputime_usec_factor);
151
152#ifdef CONFIG_PPC_SPLPAR
153void (*dtl_consumer)(struct dtl_entry *, u64);
154#endif
155
156static void calc_cputime_factors(void)
157{
158 struct div_result res;
159
160 div128_by_32(1000000, 0, tb_ticks_per_sec, &res);
161 __cputime_usec_factor = res.result_low;
162}
163
164
165
166
167
168static inline unsigned long read_spurr(unsigned long tb)
169{
170 if (cpu_has_feature(CPU_FTR_SPURR))
171 return mfspr(SPRN_SPURR);
172 if (cpu_has_feature(CPU_FTR_PURR))
173 return mfspr(SPRN_PURR);
174 return tb;
175}
176
177#ifdef CONFIG_PPC_SPLPAR
178
179#include <asm/dtl.h>
180
181
182
183
184
185static u64 scan_dispatch_log(u64 stop_tb)
186{
187 u64 i = local_paca->dtl_ridx;
188 struct dtl_entry *dtl = local_paca->dtl_curr;
189 struct dtl_entry *dtl_end = local_paca->dispatch_log_end;
190 struct lppaca *vpa = local_paca->lppaca_ptr;
191 u64 tb_delta;
192 u64 stolen = 0;
193 u64 dtb;
194
195 if (!dtl)
196 return 0;
197
198 if (i == be64_to_cpu(vpa->dtl_idx))
199 return 0;
200 while (i < be64_to_cpu(vpa->dtl_idx)) {
201 dtb = be64_to_cpu(dtl->timebase);
202 tb_delta = be32_to_cpu(dtl->enqueue_to_dispatch_time) +
203 be32_to_cpu(dtl->ready_to_enqueue_time);
204 barrier();
205 if (i + N_DISPATCH_LOG < be64_to_cpu(vpa->dtl_idx)) {
206
207 i = be64_to_cpu(vpa->dtl_idx) - N_DISPATCH_LOG;
208 dtl = local_paca->dispatch_log + (i % N_DISPATCH_LOG);
209 continue;
210 }
211 if (dtb > stop_tb)
212 break;
213 if (dtl_consumer)
214 dtl_consumer(dtl, i);
215 stolen += tb_delta;
216 ++i;
217 ++dtl;
218 if (dtl == dtl_end)
219 dtl = local_paca->dispatch_log;
220 }
221 local_paca->dtl_ridx = i;
222 local_paca->dtl_curr = dtl;
223 return stolen;
224}
225
226
227
228
229
230void notrace accumulate_stolen_time(void)
231{
232 u64 sst, ust;
233 struct cpu_accounting_data *acct = &local_paca->accounting;
234
235 sst = scan_dispatch_log(acct->starttime_user);
236 ust = scan_dispatch_log(acct->starttime);
237 acct->stime -= sst;
238 acct->utime -= ust;
239 acct->steal_time += ust + sst;
240}
241
242static inline u64 calculate_stolen_time(u64 stop_tb)
243{
244 if (!firmware_has_feature(FW_FEATURE_SPLPAR))
245 return 0;
246
247 if (get_paca()->dtl_ridx != be64_to_cpu(get_lppaca()->dtl_idx))
248 return scan_dispatch_log(stop_tb);
249
250 return 0;
251}
252
253#else
254static inline u64 calculate_stolen_time(u64 stop_tb)
255{
256 return 0;
257}
258
259#endif
260
261
262
263
264
265static unsigned long vtime_delta_scaled(struct cpu_accounting_data *acct,
266 unsigned long now, unsigned long stime)
267{
268 unsigned long stime_scaled = 0;
269#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
270 unsigned long nowscaled, deltascaled;
271 unsigned long utime, utime_scaled;
272
273 nowscaled = read_spurr(now);
274 deltascaled = nowscaled - acct->startspurr;
275 acct->startspurr = nowscaled;
276 utime = acct->utime - acct->utime_sspurr;
277 acct->utime_sspurr = acct->utime;
278
279
280
281
282
283
284
285
286
287
288
289 stime_scaled = stime;
290 utime_scaled = utime;
291 if (deltascaled != stime + utime) {
292 if (utime) {
293 stime_scaled = deltascaled * stime / (stime + utime);
294 utime_scaled = deltascaled - stime_scaled;
295 } else {
296 stime_scaled = deltascaled;
297 }
298 }
299 acct->utime_scaled += utime_scaled;
300#endif
301
302 return stime_scaled;
303}
304
305static unsigned long vtime_delta(struct cpu_accounting_data *acct,
306 unsigned long *stime_scaled,
307 unsigned long *steal_time)
308{
309 unsigned long now, stime;
310
311 WARN_ON_ONCE(!irqs_disabled());
312
313 now = mftb();
314 stime = now - acct->starttime;
315 acct->starttime = now;
316
317 *stime_scaled = vtime_delta_scaled(acct, now, stime);
318
319 *steal_time = calculate_stolen_time(now);
320
321 return stime;
322}
323
324static void vtime_delta_kernel(struct cpu_accounting_data *acct,
325 unsigned long *stime, unsigned long *stime_scaled)
326{
327 unsigned long steal_time;
328
329 *stime = vtime_delta(acct, stime_scaled, &steal_time);
330 *stime -= min(*stime, steal_time);
331 acct->steal_time += steal_time;
332}
333
334void vtime_account_kernel(struct task_struct *tsk)
335{
336 struct cpu_accounting_data *acct = get_accounting(tsk);
337 unsigned long stime, stime_scaled;
338
339 vtime_delta_kernel(acct, &stime, &stime_scaled);
340
341 if (tsk->flags & PF_VCPU) {
342 acct->gtime += stime;
343#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
344 acct->utime_scaled += stime_scaled;
345#endif
346 } else {
347 acct->stime += stime;
348#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
349 acct->stime_scaled += stime_scaled;
350#endif
351 }
352}
353EXPORT_SYMBOL_GPL(vtime_account_kernel);
354
355void vtime_account_idle(struct task_struct *tsk)
356{
357 unsigned long stime, stime_scaled, steal_time;
358 struct cpu_accounting_data *acct = get_accounting(tsk);
359
360 stime = vtime_delta(acct, &stime_scaled, &steal_time);
361 acct->idle_time += stime + steal_time;
362}
363
364static void vtime_account_irq_field(struct cpu_accounting_data *acct,
365 unsigned long *field)
366{
367 unsigned long stime, stime_scaled;
368
369 vtime_delta_kernel(acct, &stime, &stime_scaled);
370 *field += stime;
371#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
372 acct->stime_scaled += stime_scaled;
373#endif
374}
375
376void vtime_account_softirq(struct task_struct *tsk)
377{
378 struct cpu_accounting_data *acct = get_accounting(tsk);
379 vtime_account_irq_field(acct, &acct->softirq_time);
380}
381
382void vtime_account_hardirq(struct task_struct *tsk)
383{
384 struct cpu_accounting_data *acct = get_accounting(tsk);
385 vtime_account_irq_field(acct, &acct->hardirq_time);
386}
387
388static void vtime_flush_scaled(struct task_struct *tsk,
389 struct cpu_accounting_data *acct)
390{
391#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
392 if (acct->utime_scaled)
393 tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled);
394 if (acct->stime_scaled)
395 tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled);
396
397 acct->utime_scaled = 0;
398 acct->utime_sspurr = 0;
399 acct->stime_scaled = 0;
400#endif
401}
402
403
404
405
406
407
408
409
410void vtime_flush(struct task_struct *tsk)
411{
412 struct cpu_accounting_data *acct = get_accounting(tsk);
413
414 if (acct->utime)
415 account_user_time(tsk, cputime_to_nsecs(acct->utime));
416
417 if (acct->gtime)
418 account_guest_time(tsk, cputime_to_nsecs(acct->gtime));
419
420 if (IS_ENABLED(CONFIG_PPC_SPLPAR) && acct->steal_time) {
421 account_steal_time(cputime_to_nsecs(acct->steal_time));
422 acct->steal_time = 0;
423 }
424
425 if (acct->idle_time)
426 account_idle_time(cputime_to_nsecs(acct->idle_time));
427
428 if (acct->stime)
429 account_system_index_time(tsk, cputime_to_nsecs(acct->stime),
430 CPUTIME_SYSTEM);
431
432 if (acct->hardirq_time)
433 account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time),
434 CPUTIME_IRQ);
435 if (acct->softirq_time)
436 account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time),
437 CPUTIME_SOFTIRQ);
438
439 vtime_flush_scaled(tsk, acct);
440
441 acct->utime = 0;
442 acct->gtime = 0;
443 acct->idle_time = 0;
444 acct->stime = 0;
445 acct->hardirq_time = 0;
446 acct->softirq_time = 0;
447}
448
449#else
450#define calc_cputime_factors()
451#endif
452
453void __delay(unsigned long loops)
454{
455 unsigned long start;
456
457 spin_begin();
458 if (tb_invalid) {
459
460
461
462
463
464 spin_cpu_relax();
465 } else {
466 start = mftb();
467 while (mftb() - start < loops)
468 spin_cpu_relax();
469 }
470 spin_end();
471}
472EXPORT_SYMBOL(__delay);
473
474void udelay(unsigned long usecs)
475{
476 __delay(tb_ticks_per_usec * usecs);
477}
478EXPORT_SYMBOL(udelay);
479
480#ifdef CONFIG_SMP
481unsigned long profile_pc(struct pt_regs *regs)
482{
483 unsigned long pc = instruction_pointer(regs);
484
485 if (in_lock_functions(pc))
486 return regs->link;
487
488 return pc;
489}
490EXPORT_SYMBOL(profile_pc);
491#endif
492
493#ifdef CONFIG_IRQ_WORK
494
495
496
497
498#ifdef CONFIG_PPC64
499static inline void set_irq_work_pending_flag(void)
500{
501 asm volatile("stb %0,%1(13)" : :
502 "r" (1),
503 "i" (offsetof(struct paca_struct, irq_work_pending)));
504}
505
506static inline void clear_irq_work_pending(void)
507{
508 asm volatile("stb %0,%1(13)" : :
509 "r" (0),
510 "i" (offsetof(struct paca_struct, irq_work_pending)));
511}
512
513#else
514
515DEFINE_PER_CPU(u8, irq_work_pending);
516
517#define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1)
518#define test_irq_work_pending() __this_cpu_read(irq_work_pending)
519#define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0)
520
521#endif
522
523void arch_irq_work_raise(void)
524{
525
526
527
528
529
530
531
532
533
534
535
536 preempt_disable();
537 set_irq_work_pending_flag();
538 set_dec(1);
539 preempt_enable();
540}
541
542#else
543
544#define test_irq_work_pending() 0
545#define clear_irq_work_pending()
546
547#endif
548
549
550
551
552
553DEFINE_INTERRUPT_HANDLER_ASYNC(timer_interrupt)
554{
555 struct clock_event_device *evt = this_cpu_ptr(&decrementers);
556 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
557 struct pt_regs *old_regs;
558 u64 now;
559
560
561
562
563
564 if (unlikely(!cpu_online(smp_processor_id()))) {
565 set_dec(decrementer_max);
566 return;
567 }
568
569
570
571
572
573
574
575 if (IS_ENABLED(CONFIG_PPC_WATCHDOG))
576 set_dec(0x7fffffff);
577 else
578 set_dec(decrementer_max);
579
580
581
582
583 may_hard_irq_enable();
584
585
586#if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC)
587 if (atomic_read(&ppc_n_lost_interrupts) != 0)
588 __do_IRQ(regs);
589#endif
590
591 old_regs = set_irq_regs(regs);
592
593 trace_timer_interrupt_entry(regs);
594
595 if (test_irq_work_pending()) {
596 clear_irq_work_pending();
597 irq_work_run();
598 }
599
600 now = get_tb();
601 if (now >= *next_tb) {
602 *next_tb = ~(u64)0;
603 if (evt->event_handler)
604 evt->event_handler(evt);
605 __this_cpu_inc(irq_stat.timer_irqs_event);
606 } else {
607 now = *next_tb - now;
608 if (now <= decrementer_max)
609 set_dec(now);
610
611 if (test_irq_work_pending())
612 set_dec(1);
613 __this_cpu_inc(irq_stat.timer_irqs_others);
614 }
615
616 trace_timer_interrupt_exit(regs);
617
618 set_irq_regs(old_regs);
619}
620EXPORT_SYMBOL(timer_interrupt);
621
622#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
623void timer_broadcast_interrupt(void)
624{
625 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
626
627 *next_tb = ~(u64)0;
628 tick_receive_broadcast();
629 __this_cpu_inc(irq_stat.broadcast_irqs_event);
630}
631#endif
632
633#ifdef CONFIG_SUSPEND
634
635void arch_suspend_disable_irqs(void)
636{
637 if (ppc_md.suspend_disable_irqs)
638 ppc_md.suspend_disable_irqs();
639
640
641
642
643
644 set_dec(decrementer_max);
645 local_irq_disable();
646 set_dec(decrementer_max);
647}
648
649
650void arch_suspend_enable_irqs(void)
651{
652 local_irq_enable();
653
654 if (ppc_md.suspend_enable_irqs)
655 ppc_md.suspend_enable_irqs();
656}
657#endif
658
659unsigned long long tb_to_ns(unsigned long long ticks)
660{
661 return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift;
662}
663EXPORT_SYMBOL_GPL(tb_to_ns);
664
665
666
667
668
669
670
671
672notrace unsigned long long sched_clock(void)
673{
674 return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
675}
676
677
678#ifdef CONFIG_PPC_PSERIES
679
680
681
682
683
684
685unsigned long long running_clock(void)
686{
687
688
689
690
691
692
693
694
695 if (firmware_has_feature(FW_FEATURE_LPAR) &&
696 cpu_has_feature(CPU_FTR_ARCH_207S))
697 return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
698
699
700
701
702
703
704
705 return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL];
706}
707#endif
708
709static int __init get_freq(char *name, int cells, unsigned long *val)
710{
711 struct device_node *cpu;
712 const __be32 *fp;
713 int found = 0;
714
715
716 cpu = of_find_node_by_type(NULL, "cpu");
717
718 if (cpu) {
719 fp = of_get_property(cpu, name, NULL);
720 if (fp) {
721 found = 1;
722 *val = of_read_ulong(fp, cells);
723 }
724
725 of_node_put(cpu);
726 }
727
728 return found;
729}
730
731static void start_cpu_decrementer(void)
732{
733#if defined(CONFIG_BOOKE) || defined(CONFIG_40x)
734 unsigned int tcr;
735
736
737 mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
738
739 tcr = mfspr(SPRN_TCR);
740
741
742
743
744 tcr &= TCR_WP_MASK;
745 tcr |= TCR_DIE;
746 mtspr(SPRN_TCR, tcr);
747#endif
748}
749
750void __init generic_calibrate_decr(void)
751{
752 ppc_tb_freq = DEFAULT_TB_FREQ;
753
754 if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
755 !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
756
757 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
758 "(not found)\n");
759 }
760
761 ppc_proc_freq = DEFAULT_PROC_FREQ;
762
763 if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
764 !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
765
766 printk(KERN_ERR "WARNING: Estimating processor frequency "
767 "(not found)\n");
768 }
769}
770
771int update_persistent_clock64(struct timespec64 now)
772{
773 struct rtc_time tm;
774
775 if (!ppc_md.set_rtc_time)
776 return -ENODEV;
777
778 rtc_time64_to_tm(now.tv_sec + 1 + timezone_offset, &tm);
779
780 return ppc_md.set_rtc_time(&tm);
781}
782
783static void __read_persistent_clock(struct timespec64 *ts)
784{
785 struct rtc_time tm;
786 static int first = 1;
787
788 ts->tv_nsec = 0;
789
790 if (first) {
791 first = 0;
792 if (ppc_md.time_init)
793 timezone_offset = ppc_md.time_init();
794
795
796 if (ppc_md.get_boot_time) {
797 ts->tv_sec = ppc_md.get_boot_time() - timezone_offset;
798 return;
799 }
800 }
801 if (!ppc_md.get_rtc_time) {
802 ts->tv_sec = 0;
803 return;
804 }
805 ppc_md.get_rtc_time(&tm);
806
807 ts->tv_sec = rtc_tm_to_time64(&tm);
808}
809
810void read_persistent_clock64(struct timespec64 *ts)
811{
812 __read_persistent_clock(ts);
813
814
815 if (ts->tv_sec < 0) {
816 ts->tv_sec = 0;
817 ts->tv_nsec = 0;
818 }
819
820}
821
822
823static notrace u64 timebase_read(struct clocksource *cs)
824{
825 return (u64)get_tb();
826}
827
828static void __init clocksource_init(void)
829{
830 struct clocksource *clock = &clocksource_timebase;
831
832 if (clocksource_register_hz(clock, tb_ticks_per_sec)) {
833 printk(KERN_ERR "clocksource: %s is already registered\n",
834 clock->name);
835 return;
836 }
837
838 printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
839 clock->name, clock->mult, clock->shift);
840}
841
842static int decrementer_set_next_event(unsigned long evt,
843 struct clock_event_device *dev)
844{
845 __this_cpu_write(decrementers_next_tb, get_tb() + evt);
846 set_dec(evt);
847
848
849 if (test_irq_work_pending())
850 set_dec(1);
851
852 return 0;
853}
854
855static int decrementer_shutdown(struct clock_event_device *dev)
856{
857 decrementer_set_next_event(decrementer_max, dev);
858 return 0;
859}
860
861static void register_decrementer_clockevent(int cpu)
862{
863 struct clock_event_device *dec = &per_cpu(decrementers, cpu);
864
865 *dec = decrementer_clockevent;
866 dec->cpumask = cpumask_of(cpu);
867
868 clockevents_config_and_register(dec, ppc_tb_freq, 2, decrementer_max);
869
870 printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
871 dec->name, dec->mult, dec->shift, cpu);
872
873
874 decrementer_clockevent.mult = dec->mult;
875 decrementer_clockevent.shift = dec->shift;
876}
877
878static void enable_large_decrementer(void)
879{
880 if (!cpu_has_feature(CPU_FTR_ARCH_300))
881 return;
882
883 if (decrementer_max <= DECREMENTER_DEFAULT_MAX)
884 return;
885
886
887
888
889
890 if (cpu_has_feature(CPU_FTR_HVMODE))
891 mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD);
892}
893
894static void __init set_decrementer_max(void)
895{
896 struct device_node *cpu;
897 u32 bits = 32;
898
899
900 if (!cpu_has_feature(CPU_FTR_ARCH_300))
901 return;
902
903 cpu = of_find_node_by_type(NULL, "cpu");
904
905 if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) {
906 if (bits > 64 || bits < 32) {
907 pr_warn("time_init: firmware supplied invalid ibm,dec-bits");
908 bits = 32;
909 }
910
911
912 decrementer_max = (1ul << (bits - 1)) - 1;
913 }
914
915 of_node_put(cpu);
916
917 pr_info("time_init: %u bit decrementer (max: %llx)\n",
918 bits, decrementer_max);
919}
920
921static void __init init_decrementer_clockevent(void)
922{
923 register_decrementer_clockevent(smp_processor_id());
924}
925
926void secondary_cpu_time_init(void)
927{
928
929 enable_large_decrementer();
930
931
932
933
934 start_cpu_decrementer();
935
936
937
938 register_decrementer_clockevent(smp_processor_id());
939}
940
941
942void __init time_init(void)
943{
944 struct div_result res;
945 u64 scale;
946 unsigned shift;
947
948
949 ppc_md.calibrate_decr();
950 printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
951 ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
952 printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n",
953 ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
954
955 tb_ticks_per_jiffy = ppc_tb_freq / HZ;
956 tb_ticks_per_sec = ppc_tb_freq;
957 tb_ticks_per_usec = ppc_tb_freq / 1000000;
958 calc_cputime_factors();
959
960
961
962
963
964
965
966
967
968
969
970 div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
971 scale = res.result_low;
972 for (shift = 0; res.result_high != 0; ++shift) {
973 scale = (scale >> 1) | (res.result_high << 63);
974 res.result_high >>= 1;
975 }
976 tb_to_ns_scale = scale;
977 tb_to_ns_shift = shift;
978
979 boot_tb = get_tb();
980
981
982 if (timezone_offset) {
983 sys_tz.tz_minuteswest = -timezone_offset / 60;
984 sys_tz.tz_dsttime = 0;
985 }
986
987 vdso_data->tb_ticks_per_sec = tb_ticks_per_sec;
988
989
990 set_decrementer_max();
991 enable_large_decrementer();
992
993
994
995
996 start_cpu_decrementer();
997
998
999 clocksource_init();
1000
1001 init_decrementer_clockevent();
1002 tick_setup_hrtimer_broadcast();
1003
1004 of_clk_init(NULL);
1005 enable_sched_clock_irqtime();
1006}
1007
1008
1009
1010
1011
1012void div128_by_32(u64 dividend_high, u64 dividend_low,
1013 unsigned divisor, struct div_result *dr)
1014{
1015 unsigned long a, b, c, d;
1016 unsigned long w, x, y, z;
1017 u64 ra, rb, rc;
1018
1019 a = dividend_high >> 32;
1020 b = dividend_high & 0xffffffff;
1021 c = dividend_low >> 32;
1022 d = dividend_low & 0xffffffff;
1023
1024 w = a / divisor;
1025 ra = ((u64)(a - (w * divisor)) << 32) + b;
1026
1027 rb = ((u64) do_div(ra, divisor) << 32) + c;
1028 x = ra;
1029
1030 rc = ((u64) do_div(rb, divisor) << 32) + d;
1031 y = rb;
1032
1033 do_div(rc, divisor);
1034 z = rc;
1035
1036 dr->result_high = ((u64)w << 32) + x;
1037 dr->result_low = ((u64)y << 32) + z;
1038
1039}
1040
1041
1042void calibrate_delay(void)
1043{
1044
1045
1046
1047 loops_per_jiffy = tb_ticks_per_jiffy;
1048}
1049
1050#if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
1051static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm)
1052{
1053 ppc_md.get_rtc_time(tm);
1054 return 0;
1055}
1056
1057static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm)
1058{
1059 if (!ppc_md.set_rtc_time)
1060 return -EOPNOTSUPP;
1061
1062 if (ppc_md.set_rtc_time(tm) < 0)
1063 return -EOPNOTSUPP;
1064
1065 return 0;
1066}
1067
1068static const struct rtc_class_ops rtc_generic_ops = {
1069 .read_time = rtc_generic_get_time,
1070 .set_time = rtc_generic_set_time,
1071};
1072
1073static int __init rtc_init(void)
1074{
1075 struct platform_device *pdev;
1076
1077 if (!ppc_md.get_rtc_time)
1078 return -ENODEV;
1079
1080 pdev = platform_device_register_data(NULL, "rtc-generic", -1,
1081 &rtc_generic_ops,
1082 sizeof(rtc_generic_ops));
1083
1084 return PTR_ERR_OR_ZERO(pdev);
1085}
1086
1087device_initcall(rtc_init);
1088#endif
1089