Merge tag 'net-6.16-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net
[linux-2.6-block.git] / arch / powerpc / kernel / time.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/*
1da177e4
LT
3 * Common time routines among all ppc machines.
4 *
5 * Written by Cort Dougan (cort@cs.nmt.edu) to merge
6 * Paul Mackerras' version and mine for PReP and Pmac.
7 * MPC8xx/MBX changes by Dan Malek (dmalek@jlc.net).
8 * Converted for 64-bit by Mike Corrigan (mikejc@us.ibm.com)
9 *
10 * First round of bugfixes by Gabriel Paubert (paubert@iram.es)
11 * to make clock more stable (2.4.0-test5). The only thing
12 * that this code assumes is that the timebases have been synchronized
13 * by firmware on SMP and are never stopped (never do sleep
14 * on SMP then, nap and doze are OK).
15 *
16 * Speeded up do_gettimeofday by getting rid of references to
17 * xtime (which required locks for consistency). (mikejc@us.ibm.com)
18 *
19 * TODO (not necessarily in this file):
20 * - improve precision and reproducibility of timebase frequency
f5339277 21 * measurement at boot time.
1da177e4
LT
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
1da177e4
LT
28 */
29
1da177e4 30#include <linux/errno.h>
4b16f8e2 31#include <linux/export.h>
1da177e4 32#include <linux/sched.h>
e6017571 33#include <linux/sched/clock.h>
e225c4d6 34#include <linux/sched/cputime.h>
1da177e4
LT
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>
1da177e4
LT
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>
f2783c15
PM
47#include <linux/percpu.h>
48#include <linux/rtc.h>
092b8f34 49#include <linux/jiffies.h>
c6622f63 50#include <linux/posix-timers.h>
7d12e780 51#include <linux/irq.h>
177996e6 52#include <linux/delay.h>
e360adbe 53#include <linux/irq_work.h>
60083063 54#include <linux/of_clk.h>
7f92bc56 55#include <linux/suspend.h>
4e287e65 56#include <linux/processor.h>
ce0091a0 57#include <linux/mc146818rtc.h>
e6f6390a 58#include <linux/platform_device.h>
1da177e4 59
ce0091a0 60#include <asm/trace.h>
3a96570f 61#include <asm/interrupt.h>
1da177e4 62#include <asm/io.h>
1da177e4
LT
63#include <asm/nvram.h>
64#include <asm/cache.h>
65#include <asm/machdep.h>
7c0f6ba6 66#include <linux/uaccess.h>
1da177e4 67#include <asm/time.h>
f2783c15
PM
68#include <asm/irq.h>
69#include <asm/div64.h>
2249ca9d 70#include <asm/smp.h>
a7f290da 71#include <asm/vdso_datapage.h>
1ababe11 72#include <asm/firmware.h>
cc15ff32 73#include <asm/mce.h>
6142be7e 74#include <asm/systemcfg.h>
1da177e4 75
4a4cfe38
TB
76/* powerpc clocksource/clockevent code */
77
d831d0b8 78#include <linux/clockchips.h>
4a4cfe38 79
a5a1d1c2 80static u64 timebase_read(struct clocksource *);
4a4cfe38
TB
81static struct clocksource clocksource_timebase = {
82 .name = "timebase",
83 .rating = 400,
84 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
85 .mask = CLOCKSOURCE_MASK(64),
4a4cfe38 86 .read = timebase_read,
ab037dd8 87 .vdso_clock_mode = VDSO_CLOCKMODE_ARCHTIMER,
4a4cfe38
TB
88};
89
79901024
OH
90#define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF
91u64 decrementer_max = DECREMENTER_DEFAULT_MAX;
9581991a 92EXPORT_SYMBOL_GPL(decrementer_max); /* for KVM HDEC */
d831d0b8
TB
93
94static int decrementer_set_next_event(unsigned long evt,
95 struct clock_event_device *dev);
37a13e78 96static int decrementer_shutdown(struct clock_event_device *evt);
d831d0b8 97
6e35994d 98struct clock_event_device decrementer_clockevent = {
37a13e78
VK
99 .name = "decrementer",
100 .rating = 200,
101 .irq = 0,
102 .set_next_event = decrementer_set_next_event,
81759360 103 .set_state_oneshot_stopped = decrementer_shutdown,
37a13e78
VK
104 .set_state_shutdown = decrementer_shutdown,
105 .tick_resume = decrementer_shutdown,
106 .features = CLOCK_EVT_FEAT_ONESHOT |
107 CLOCK_EVT_FEAT_C3STOP,
d831d0b8 108};
6e35994d 109EXPORT_SYMBOL(decrementer_clockevent);
d831d0b8 110
35de589c
NP
111/*
112 * This always puts next_tb beyond now, so the clock event will never fire
113 * with the usual comparison, no need for a separate test for stopped.
114 */
115#define DEC_CLOCKEVENT_STOPPED ~0ULL
116DEFINE_PER_CPU(u64, decrementers_next_tb) = DEC_CLOCKEVENT_STOPPED;
4ebbd075 117EXPORT_SYMBOL_GPL(decrementers_next_tb);
7df10275 118static DEFINE_PER_CPU(struct clock_event_device, decrementers);
d831d0b8 119
1da177e4
LT
120#define XSEC_PER_SEC (1024*1024)
121
f2783c15
PM
122#ifdef CONFIG_PPC64
123#define SCALE_XSEC(xsec, max) (((xsec) * max) / XSEC_PER_SEC)
124#else
125/* compute ((xsec << 12) * max) >> 32 */
126#define SCALE_XSEC(xsec, max) mulhwu((xsec) << 12, max)
127#endif
128
1da177e4
LT
129unsigned long tb_ticks_per_jiffy;
130unsigned long tb_ticks_per_usec = 100; /* sane default */
131EXPORT_SYMBOL(tb_ticks_per_usec);
132unsigned long tb_ticks_per_sec;
f985adaf 133EXPORT_SYMBOL(tb_ticks_per_sec); /* for cputime conversions */
092b8f34 134
1da177e4 135DEFINE_SPINLOCK(rtc_lock);
6ae3db11 136EXPORT_SYMBOL_GPL(rtc_lock);
1da177e4 137
fc9069fe
TB
138static u64 tb_to_ns_scale __read_mostly;
139static unsigned tb_to_ns_shift __read_mostly;
364a1246 140static u64 boot_tb __read_mostly;
1da177e4 141
1da177e4 142extern struct timezone sys_tz;
f2783c15 143static long timezone_offset;
1da177e4 144
10f7e7c1 145unsigned long ppc_proc_freq;
55ec2fca 146EXPORT_SYMBOL_GPL(ppc_proc_freq);
10f7e7c1 147unsigned long ppc_tb_freq;
55ec2fca 148EXPORT_SYMBOL_GPL(ppc_tb_freq);
96c44507 149
de269129
MS
150bool tb_invalid;
151
abf917cd 152#ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
c6622f63 153/*
cf9efce0
PM
154 * Read the SPURR on systems that have it, otherwise the PURR,
155 * or if that doesn't exist return the timebase value passed in.
c6622f63 156 */
abcff86d 157static inline unsigned long read_spurr(unsigned long tb)
c6622f63 158{
cf9efce0
PM
159 if (cpu_has_feature(CPU_FTR_SPURR))
160 return mfspr(SPRN_SPURR);
c6622f63
PM
161 if (cpu_has_feature(CPU_FTR_PURR))
162 return mfspr(SPRN_PURR);
cf9efce0 163 return tb;
c6622f63
PM
164}
165
166/*
167 * Account time for a transition between system, hard irq
168 * or soft irq state.
169 */
b38a181c
CL
170static unsigned long vtime_delta_scaled(struct cpu_accounting_data *acct,
171 unsigned long now, unsigned long stime)
c6622f63 172{
abcff86d
CL
173 unsigned long stime_scaled = 0;
174#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
b38a181c 175 unsigned long nowscaled, deltascaled;
a19ff1a2 176 unsigned long utime, utime_scaled;
c6622f63 177
4603ac18 178 nowscaled = read_spurr(now);
c223c903
CL
179 deltascaled = nowscaled - acct->startspurr;
180 acct->startspurr = nowscaled;
a19ff1a2 181 utime = acct->utime - acct->utime_sspurr;
8c8b73c4 182 acct->utime_sspurr = acct->utime;
cf9efce0
PM
183
184 /*
185 * Because we don't read the SPURR on every kernel entry/exit,
186 * deltascaled includes both user and system SPURR ticks.
187 * Apportion these ticks to system SPURR ticks and user
188 * SPURR ticks in the same ratio as the system time (delta)
189 * and user time (udelta) values obtained from the timebase
190 * over the same interval. The system ticks get accounted here;
191 * the user ticks get saved up in paca->user_time_scaled to be
192 * used by account_process_tick.
193 */
b38a181c 194 stime_scaled = stime;
a19ff1a2
FW
195 utime_scaled = utime;
196 if (deltascaled != stime + utime) {
197 if (utime) {
b38a181c
CL
198 stime_scaled = deltascaled * stime / (stime + utime);
199 utime_scaled = deltascaled - stime_scaled;
cf9efce0 200 } else {
b38a181c 201 stime_scaled = deltascaled;
cf9efce0
PM
202 }
203 }
a19ff1a2 204 acct->utime_scaled += utime_scaled;
abcff86d 205#endif
cf9efce0 206
b38a181c
CL
207 return stime_scaled;
208}
209
8a6a5920 210static unsigned long vtime_delta(struct cpu_accounting_data *acct,
b38a181c
CL
211 unsigned long *stime_scaled,
212 unsigned long *steal_time)
213{
214 unsigned long now, stime;
b38a181c
CL
215
216 WARN_ON_ONCE(!irqs_disabled());
217
218 now = mftb();
219 stime = now - acct->starttime;
220 acct->starttime = now;
221
222 *stime_scaled = vtime_delta_scaled(acct, now, stime);
223
6ba5aa54
NP
224 if (IS_ENABLED(CONFIG_PPC_SPLPAR) &&
225 firmware_has_feature(FW_FEATURE_SPLPAR))
226 *steal_time = pseries_calculate_stolen_time(now);
227 else
228 *steal_time = 0;
b38a181c 229
a19ff1a2 230 return stime;
a7e1a9e3
FW
231}
232
8a6a5920
FW
233static void vtime_delta_kernel(struct cpu_accounting_data *acct,
234 unsigned long *stime, unsigned long *stime_scaled)
235{
236 unsigned long steal_time;
237
238 *stime = vtime_delta(acct, stime_scaled, &steal_time);
239 *stime -= min(*stime, steal_time);
240 acct->steal_time += steal_time;
241}
242
f83eeb1a 243void vtime_account_kernel(struct task_struct *tsk)
a7e1a9e3 244{
a19ff1a2 245 struct cpu_accounting_data *acct = get_accounting(tsk);
8a6a5920 246 unsigned long stime, stime_scaled;
a19ff1a2 247
8a6a5920 248 vtime_delta_kernel(acct, &stime, &stime_scaled);
a7e1a9e3 249
8a6a5920 250 if (tsk->flags & PF_VCPU) {
a19ff1a2 251 acct->gtime += stime;
abcff86d 252#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
a19ff1a2 253 acct->utime_scaled += stime_scaled;
abcff86d 254#endif
a19ff1a2 255 } else {
8a6a5920 256 acct->stime += stime;
abcff86d 257#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
a19ff1a2 258 acct->stime_scaled += stime_scaled;
abcff86d 259#endif
a19ff1a2 260 }
a7e1a9e3 261}
f83eeb1a 262EXPORT_SYMBOL_GPL(vtime_account_kernel);
a7e1a9e3 263
fd25b4c2 264void vtime_account_idle(struct task_struct *tsk)
a7e1a9e3 265{
a19ff1a2
FW
266 unsigned long stime, stime_scaled, steal_time;
267 struct cpu_accounting_data *acct = get_accounting(tsk);
a7e1a9e3 268
8a6a5920 269 stime = vtime_delta(acct, &stime_scaled, &steal_time);
a19ff1a2 270 acct->idle_time += stime + steal_time;
c6622f63
PM
271}
272
8a6a5920
FW
273static void vtime_account_irq_field(struct cpu_accounting_data *acct,
274 unsigned long *field)
275{
276 unsigned long stime, stime_scaled;
277
278 vtime_delta_kernel(acct, &stime, &stime_scaled);
279 *field += stime;
280#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
281 acct->stime_scaled += stime_scaled;
282#endif
283}
284
285void vtime_account_softirq(struct task_struct *tsk)
286{
287 struct cpu_accounting_data *acct = get_accounting(tsk);
288 vtime_account_irq_field(acct, &acct->softirq_time);
289}
290
291void vtime_account_hardirq(struct task_struct *tsk)
292{
293 struct cpu_accounting_data *acct = get_accounting(tsk);
294 vtime_account_irq_field(acct, &acct->hardirq_time);
295}
296
b38a181c
CL
297static void vtime_flush_scaled(struct task_struct *tsk,
298 struct cpu_accounting_data *acct)
299{
abcff86d 300#ifdef CONFIG_ARCH_HAS_SCALED_CPUTIME
b38a181c
CL
301 if (acct->utime_scaled)
302 tsk->utimescaled += cputime_to_nsecs(acct->utime_scaled);
303 if (acct->stime_scaled)
304 tsk->stimescaled += cputime_to_nsecs(acct->stime_scaled);
305
306 acct->utime_scaled = 0;
307 acct->utime_sspurr = 0;
308 acct->stime_scaled = 0;
abcff86d 309#endif
b38a181c
CL
310}
311
c6622f63 312/*
c8d7dabf 313 * Account the whole cputime accumulated in the paca
c6622f63 314 * Must be called with interrupts disabled.
f83eeb1a 315 * Assumes that vtime_account_kernel/idle() has been called
bcebdf84 316 * recently (i.e. since the last entry from usermode) so that
cf9efce0 317 * get_paca()->user_time_scaled is up to date.
c6622f63 318 */
c8d7dabf 319void vtime_flush(struct task_struct *tsk)
c6622f63 320{
c223c903 321 struct cpu_accounting_data *acct = get_accounting(tsk);
c6622f63 322
a19ff1a2 323 if (acct->utime)
23244a5c 324 account_user_time(tsk, cputime_to_nsecs(acct->utime));
a19ff1a2 325
a19ff1a2 326 if (acct->gtime)
fb8b049c 327 account_guest_time(tsk, cputime_to_nsecs(acct->gtime));
a19ff1a2 328
51eeef9e 329 if (IS_ENABLED(CONFIG_PPC_SPLPAR) && acct->steal_time) {
be9095ed 330 account_steal_time(cputime_to_nsecs(acct->steal_time));
51eeef9e
CL
331 acct->steal_time = 0;
332 }
a19ff1a2
FW
333
334 if (acct->idle_time)
18b43a9b 335 account_idle_time(cputime_to_nsecs(acct->idle_time));
a19ff1a2
FW
336
337 if (acct->stime)
fb8b049c
FW
338 account_system_index_time(tsk, cputime_to_nsecs(acct->stime),
339 CPUTIME_SYSTEM);
a19ff1a2
FW
340
341 if (acct->hardirq_time)
fb8b049c
FW
342 account_system_index_time(tsk, cputime_to_nsecs(acct->hardirq_time),
343 CPUTIME_IRQ);
a19ff1a2 344 if (acct->softirq_time)
fb8b049c
FW
345 account_system_index_time(tsk, cputime_to_nsecs(acct->softirq_time),
346 CPUTIME_SOFTIRQ);
a19ff1a2 347
b38a181c
CL
348 vtime_flush_scaled(tsk, acct);
349
8c8b73c4 350 acct->utime = 0;
a19ff1a2 351 acct->gtime = 0;
a19ff1a2
FW
352 acct->idle_time = 0;
353 acct->stime = 0;
a19ff1a2
FW
354 acct->hardirq_time = 0;
355 acct->softirq_time = 0;
c6622f63 356}
89d6910c
AG
357
358/*
359 * Called from the context switch with interrupts disabled, to charge all
360 * accumulated times to the current process, and to prepare accounting on
361 * the next process.
362 */
363void vtime_task_switch(struct task_struct *prev)
364{
365 if (is_idle_task(prev))
366 vtime_account_idle(prev);
367 else
368 vtime_account_kernel(prev);
369
370 vtime_flush(prev);
371
372 if (!IS_ENABLED(CONFIG_PPC64)) {
373 struct cpu_accounting_data *acct = get_accounting(current);
374 struct cpu_accounting_data *acct0 = get_accounting(prev);
375
376 acct->starttime = acct0->starttime;
377 }
378}
f985adaf 379#endif /* CONFIG_VIRT_CPU_ACCOUNTING_NATIVE */
c6622f63 380
2a7ce82d 381void __no_kcsan __delay(unsigned long loops)
6defa38b
PM
382{
383 unsigned long start;
6defa38b 384
4e287e65 385 spin_begin();
a4c5a355 386 if (tb_invalid) {
de269129
MS
387 /*
388 * TB is in error state and isn't ticking anymore.
389 * HMI handler was unable to recover from TB error.
390 * Return immediately, so that kernel won't get stuck here.
391 */
392 spin_cpu_relax();
6defa38b 393 } else {
942e8911
CL
394 start = mftb();
395 while (mftb() - start < loops)
4e287e65 396 spin_cpu_relax();
6defa38b 397 }
4e287e65 398 spin_end();
6defa38b
PM
399}
400EXPORT_SYMBOL(__delay);
401
2a7ce82d 402void __no_kcsan udelay(unsigned long usecs)
6defa38b
PM
403{
404 __delay(tb_ticks_per_usec * usecs);
405}
406EXPORT_SYMBOL(udelay);
407
1da177e4
LT
408#ifdef CONFIG_SMP
409unsigned long profile_pc(struct pt_regs *regs)
410{
411 unsigned long pc = instruction_pointer(regs);
412
413 if (in_lock_functions(pc))
414 return regs->link;
415
416 return pc;
417}
418EXPORT_SYMBOL(profile_pc);
419#endif
420
e360adbe 421#ifdef CONFIG_IRQ_WORK
105988c0 422
0fe1ac48
PM
423/*
424 * 64-bit uses a byte in the PACA, 32-bit uses a per-cpu variable...
425 */
426#ifdef CONFIG_PPC64
25aa1458
NP
427static inline unsigned long test_irq_work_pending(void)
428{
429 unsigned long x;
430
431 asm volatile("lbz %0,%1(13)"
432 : "=r" (x)
433 : "i" (offsetof(struct paca_struct, irq_work_pending)));
434 return x;
435}
436
e360adbe 437static inline void set_irq_work_pending_flag(void)
0fe1ac48
PM
438{
439 asm volatile("stb %0,%1(13)" : :
440 "r" (1),
e360adbe 441 "i" (offsetof(struct paca_struct, irq_work_pending)));
0fe1ac48
PM
442}
443
e360adbe 444static inline void clear_irq_work_pending(void)
0fe1ac48
PM
445{
446 asm volatile("stb %0,%1(13)" : :
447 "r" (0),
e360adbe 448 "i" (offsetof(struct paca_struct, irq_work_pending)));
105988c0
PM
449}
450
0fe1ac48
PM
451#else /* 32-bit */
452
e360adbe 453DEFINE_PER_CPU(u8, irq_work_pending);
0fe1ac48 454
69111bac
CL
455#define set_irq_work_pending_flag() __this_cpu_write(irq_work_pending, 1)
456#define test_irq_work_pending() __this_cpu_read(irq_work_pending)
457#define clear_irq_work_pending() __this_cpu_write(irq_work_pending, 0)
105988c0 458
abc3fce7
NP
459#endif /* 32 vs 64 bit */
460
4f8b50bb 461void arch_irq_work_raise(void)
0fe1ac48 462{
abc3fce7
NP
463 /*
464 * 64-bit code that uses irq soft-mask can just cause an immediate
465 * interrupt here that gets soft masked, if this is called under
466 * local_irq_disable(). It might be possible to prevent that happening
467 * by noticing interrupts are disabled and setting decrementer pending
468 * to be replayed when irqs are enabled. The problem there is that
469 * tracing can call irq_work_raise, including in code that does low
470 * level manipulations of irq soft-mask state (e.g., trace_hardirqs_on)
471 * which could get tangled up if we're messing with the same state
472 * here.
473 */
0fe1ac48 474 preempt_disable();
e360adbe 475 set_irq_work_pending_flag();
0fe1ac48
PM
476 set_dec(1);
477 preempt_enable();
478}
479
25aa1458
NP
480static void set_dec_or_work(u64 val)
481{
482 set_dec(val);
483 /* We may have raced with new irq work */
484 if (unlikely(test_irq_work_pending()))
485 set_dec(1);
486}
487
e360adbe 488#else /* CONFIG_IRQ_WORK */
105988c0 489
e360adbe
PZ
490#define test_irq_work_pending() 0
491#define clear_irq_work_pending()
105988c0 492
25aa1458
NP
493static void set_dec_or_work(u64 val)
494{
495 set_dec(val);
496}
e360adbe 497#endif /* CONFIG_IRQ_WORK */
105988c0 498
25aa1458
NP
499#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
500void timer_rearm_host_dec(u64 now)
501{
502 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
503
504 WARN_ON_ONCE(!arch_irqs_disabled());
505 WARN_ON_ONCE(mfmsr() & MSR_EE);
506
507 if (now >= *next_tb) {
508 local_paca->irq_happened |= PACA_IRQ_DEC;
509 } else {
510 now = *next_tb - now;
cf74ff52
NP
511 if (now > decrementer_max)
512 now = decrementer_max;
513 set_dec_or_work(now);
25aa1458
NP
514 }
515}
516EXPORT_SYMBOL_GPL(timer_rearm_host_dec);
517#endif
518
1da177e4
LT
519/*
520 * timer_interrupt - gets called when the decrementer overflows,
521 * with interrupts disabled.
522 */
3a96570f 523DEFINE_INTERRUPT_HANDLER_ASYNC(timer_interrupt)
1da177e4 524{
3f984620 525 struct clock_event_device *evt = this_cpu_ptr(&decrementers);
69111bac 526 u64 *next_tb = this_cpu_ptr(&decrementers_next_tb);
3f984620
NP
527 struct pt_regs *old_regs;
528 u64 now;
d831d0b8 529
59d512e4
NP
530 /*
531 * Some implementations of hotplug will get timer interrupts while
532 * offline, just ignore these.
963e5d3b 533 */
a7cba02d 534 if (unlikely(!cpu_online(smp_processor_id()))) {
a7cba02d 535 set_dec(decrementer_max);
963e5d3b 536 return;
689dfa89 537 }
963e5d3b 538
d2b9be1f 539 /* Conditionally hard-enable interrupts. */
c2854801 540 if (should_hard_irq_enable(regs)) {
c8455020
NP
541 /*
542 * Ensure a positive value is written to the decrementer, or
543 * else some CPUs will continue to take decrementer exceptions.
544 * When the PPC_WATCHDOG (decrementer based) is configured,
545 * keep this at most 31 bits, which is about 4 seconds on most
546 * systems, which gives the watchdog a chance of catching timer
547 * interrupt hard lockups.
548 */
549 if (IS_ENABLED(CONFIG_PPC_WATCHDOG))
550 set_dec(0x7fffffff);
551 else
552 set_dec(decrementer_max);
553
0faf20a1 554 do_hard_irq_enable();
c8455020 555 }
89713ed1 556
6e0fdf9a 557#if defined(CONFIG_PPC32) && defined(CONFIG_PPC_PMAC)
f2783c15 558 if (atomic_read(&ppc_n_lost_interrupts) != 0)
98694166 559 __do_IRQ(regs);
f2783c15 560#endif
1da177e4 561
7d12e780 562 old_regs = set_irq_regs(regs);
1b1b6a6f 563
3f984620
NP
564 trace_timer_interrupt_entry(regs);
565
566 if (test_irq_work_pending()) {
567 clear_irq_work_pending();
cc15ff32 568 mce_run_irq_context_handlers();
3f984620
NP
569 irq_work_run();
570 }
571
6601ec1c 572 now = get_tb();
3f984620 573 if (now >= *next_tb) {
35de589c 574 evt->event_handler(evt);
3f984620
NP
575 __this_cpu_inc(irq_stat.timer_irqs_event);
576 } else {
577 now = *next_tb - now;
8defc2a5
NP
578 if (now > decrementer_max)
579 now = decrementer_max;
580 set_dec_or_work(now);
3f984620
NP
581 __this_cpu_inc(irq_stat.timer_irqs_others);
582 }
1da177e4 583
3f984620 584 trace_timer_interrupt_exit(regs);
1b1b6a6f 585
7d12e780 586 set_irq_regs(old_regs);
1da177e4 587}
9445aa1a 588EXPORT_SYMBOL(timer_interrupt);
1da177e4 589
bc907113 590#ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
3f984620
NP
591void timer_broadcast_interrupt(void)
592{
3f984620 593 tick_receive_broadcast();
e360cd37 594 __this_cpu_inc(irq_stat.broadcast_irqs_event);
3f984620 595}
bc907113 596#endif
3f984620 597
7ac5dde9 598#ifdef CONFIG_SUSPEND
e606a2f4
CL
599/* Overrides the weak version in kernel/power/main.c */
600void arch_suspend_disable_irqs(void)
7ac5dde9 601{
e606a2f4
CL
602 if (ppc_md.suspend_disable_irqs)
603 ppc_md.suspend_disable_irqs();
604
7ac5dde9
SW
605 /* Disable the decrementer, so that it doesn't interfere
606 * with suspending.
607 */
608
79901024 609 set_dec(decrementer_max);
7ac5dde9 610 local_irq_disable();
79901024 611 set_dec(decrementer_max);
7ac5dde9
SW
612}
613
7ac5dde9
SW
614/* Overrides the weak version in kernel/power/main.c */
615void arch_suspend_enable_irqs(void)
616{
e606a2f4
CL
617 local_irq_enable();
618
7ac5dde9
SW
619 if (ppc_md.suspend_enable_irqs)
620 ppc_md.suspend_enable_irqs();
621}
622#endif
623
b6c295df
PM
624unsigned long long tb_to_ns(unsigned long long ticks)
625{
626 return mulhdu(ticks, tb_to_ns_scale) << tb_to_ns_shift;
627}
628EXPORT_SYMBOL_GPL(tb_to_ns);
629
1da177e4
LT
630/*
631 * Scheduler clock - returns current time in nanosec units.
632 *
633 * Note: mulhdu(a, b) (multiply high double unsigned) returns
634 * the high 64 bits of a * b, i.e. (a * b) >> 64, where a and b
635 * are 64-bit unsigned numbers.
636 */
6b847d79 637notrace unsigned long long sched_clock(void)
1da177e4 638{
fc9069fe 639 return mulhdu(get_tb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
1da177e4
LT
640}
641
4be1b297
CB
642
643#ifdef CONFIG_PPC_PSERIES
644
645/*
646 * Running clock - attempts to give a view of time passing for a virtualised
647 * kernels.
648 * Uses the VTB register if available otherwise a next best guess.
649 */
650unsigned long long running_clock(void)
651{
652 /*
653 * Don't read the VTB as a host since KVM does not switch in host
654 * timebase into the VTB when it takes a guest off the CPU, reading the
655 * VTB would result in reading 'last switched out' guest VTB.
656 *
657 * Host kernels are often compiled with CONFIG_PPC_PSERIES checked, it
658 * would be unsafe to rely only on the #ifdef above.
659 */
660 if (firmware_has_feature(FW_FEATURE_LPAR) &&
661 cpu_has_feature(CPU_FTR_ARCH_207S))
662 return mulhdu(get_vtb() - boot_tb, tb_to_ns_scale) << tb_to_ns_shift;
663
664 /*
665 * This is a next best approximation without a VTB.
666 * On a host which is running bare metal there should never be any stolen
667 * time and on a host which doesn't do any virtualisation TB *should* equal
668 * VTB so it makes no difference anyway.
669 */
9f3768e0 670 return local_clock() - kcpustat_this_cpu->cpustat[CPUTIME_STEAL];
4be1b297
CB
671}
672#endif
673
0bb474a4 674static int __init get_freq(char *name, int cells, unsigned long *val)
10f7e7c1
AB
675{
676 struct device_node *cpu;
6f7aba7b 677 const __be32 *fp;
0bb474a4 678 int found = 0;
10f7e7c1 679
0bb474a4 680 /* The cpu node should have timebase and clock frequency properties */
10f7e7c1
AB
681 cpu = of_find_node_by_type(NULL, "cpu");
682
d8a8188d 683 if (cpu) {
e2eb6392 684 fp = of_get_property(cpu, name, NULL);
d8a8188d 685 if (fp) {
0bb474a4 686 found = 1;
a4dc7ff0 687 *val = of_read_ulong(fp, cells);
10f7e7c1 688 }
0bb474a4
AB
689
690 of_node_put(cpu);
10f7e7c1 691 }
0bb474a4
AB
692
693 return found;
694}
695
e51df2c1 696static void start_cpu_decrementer(void)
77c0a700 697{
002b27a5 698#ifdef CONFIG_BOOKE
6e2f03e2
IM
699 unsigned int tcr;
700
77c0a700
BH
701 /* Clear any pending timer interrupts */
702 mtspr(SPRN_TSR, TSR_ENW | TSR_WIS | TSR_DIS | TSR_FIS);
703
6e2f03e2
IM
704 tcr = mfspr(SPRN_TCR);
705 /*
706 * The watchdog may have already been enabled by u-boot. So leave
707 * TRC[WP] (Watchdog Period) alone.
708 */
709 tcr &= TCR_WP_MASK; /* Clear all bits except for TCR[WP] */
710 tcr |= TCR_DIE; /* Enable decrementer */
711 mtspr(SPRN_TCR, tcr);
712#endif
77c0a700
BH
713}
714
0bb474a4
AB
715void __init generic_calibrate_decr(void)
716{
717 ppc_tb_freq = DEFAULT_TB_FREQ; /* hardcoded default */
718
719 if (!get_freq("ibm,extended-timebase-frequency", 2, &ppc_tb_freq) &&
720 !get_freq("timebase-frequency", 1, &ppc_tb_freq)) {
721
10f7e7c1
AB
722 printk(KERN_ERR "WARNING: Estimating decrementer frequency "
723 "(not found)\n");
0bb474a4 724 }
10f7e7c1 725
0bb474a4
AB
726 ppc_proc_freq = DEFAULT_PROC_FREQ; /* hardcoded default */
727
728 if (!get_freq("ibm,extended-clock-frequency", 2, &ppc_proc_freq) &&
729 !get_freq("clock-frequency", 1, &ppc_proc_freq)) {
730
731 printk(KERN_ERR "WARNING: Estimating processor frequency "
732 "(not found)\n");
10f7e7c1 733 }
10f7e7c1 734}
10f7e7c1 735
5235afa8 736int update_persistent_clock64(struct timespec64 now)
f2783c15
PM
737{
738 struct rtc_time tm;
739
aa3be5f3 740 if (!ppc_md.set_rtc_time)
023f333a 741 return -ENODEV;
aa3be5f3 742
5235afa8 743 rtc_time64_to_tm(now.tv_sec + 1 + timezone_offset, &tm);
aa3be5f3
TB
744
745 return ppc_md.set_rtc_time(&tm);
746}
747
5bfd6435 748static void __read_persistent_clock(struct timespec64 *ts)
aa3be5f3
TB
749{
750 struct rtc_time tm;
751 static int first = 1;
752
d90246cd 753 ts->tv_nsec = 0;
1fd02f66 754 /* XXX this is a little fragile but will work okay in the short term */
aa3be5f3
TB
755 if (first) {
756 first = 0;
757 if (ppc_md.time_init)
758 timezone_offset = ppc_md.time_init();
759
760 /* get_boot_time() isn't guaranteed to be safe to call late */
d90246cd
MS
761 if (ppc_md.get_boot_time) {
762 ts->tv_sec = ppc_md.get_boot_time() - timezone_offset;
763 return;
764 }
765 }
766 if (!ppc_md.get_rtc_time) {
767 ts->tv_sec = 0;
768 return;
aa3be5f3 769 }
f2783c15 770 ppc_md.get_rtc_time(&tm);
978d7eb3 771
5bfd6435 772 ts->tv_sec = rtc_tm_to_time64(&tm);
f2783c15
PM
773}
774
5bfd6435 775void read_persistent_clock64(struct timespec64 *ts)
978d7eb3
BH
776{
777 __read_persistent_clock(ts);
778
779 /* Sanitize it in case real time clock is set below EPOCH */
780 if (ts->tv_sec < 0) {
781 ts->tv_sec = 0;
782 ts->tv_nsec = 0;
783 }
784
785}
786
4a4cfe38 787/* clocksource code */
6b847d79 788static notrace u64 timebase_read(struct clocksource *cs)
4a4cfe38 789{
a5a1d1c2 790 return (u64)get_tb();
4a4cfe38
TB
791}
792
1c21a293 793static void __init clocksource_init(void)
4a4cfe38 794{
a4c5a355 795 struct clocksource *clock = &clocksource_timebase;
4a4cfe38 796
11b8633a 797 if (clocksource_register_hz(clock, tb_ticks_per_sec)) {
4a4cfe38
TB
798 printk(KERN_ERR "clocksource: %s is already registered\n",
799 clock->name);
800 return;
801 }
802
803 printk(KERN_INFO "clocksource: %s mult[%x] shift[%d] registered\n",
804 clock->name, clock->mult, clock->shift);
805}
806
d831d0b8
TB
807static int decrementer_set_next_event(unsigned long evt,
808 struct clock_event_device *dev)
809{
6601ec1c 810 __this_cpu_write(decrementers_next_tb, get_tb() + evt);
25aa1458 811 set_dec_or_work(evt);
0215f7d8 812
d831d0b8
TB
813 return 0;
814}
815
37a13e78 816static int decrementer_shutdown(struct clock_event_device *dev)
d831d0b8 817{
35de589c
NP
818 __this_cpu_write(decrementers_next_tb, DEC_CLOCKEVENT_STOPPED);
819 set_dec_or_work(decrementer_max);
820
37a13e78 821 return 0;
d831d0b8
TB
822}
823
824static void register_decrementer_clockevent(int cpu)
825{
7df10275 826 struct clock_event_device *dec = &per_cpu(decrementers, cpu);
d831d0b8
TB
827
828 *dec = decrementer_clockevent;
320ab2b0 829 dec->cpumask = cpumask_of(cpu);
d831d0b8 830
8b78fdb0
AB
831 clockevents_config_and_register(dec, ppc_tb_freq, 2, decrementer_max);
832
b919ee82
AB
833 printk_once(KERN_DEBUG "clockevent: %s mult[%x] shift[%d] cpu[%d]\n",
834 dec->name, dec->mult, dec->shift, cpu);
b4d16ab5
ME
835
836 /* Set values for KVM, see kvm_emulate_dec() */
837 decrementer_clockevent.mult = dec->mult;
838 decrementer_clockevent.shift = dec->shift;
d831d0b8
TB
839}
840
79901024
OH
841static void enable_large_decrementer(void)
842{
843 if (!cpu_has_feature(CPU_FTR_ARCH_300))
844 return;
845
846 if (decrementer_max <= DECREMENTER_DEFAULT_MAX)
847 return;
848
849 /*
850 * If we're running as the hypervisor we need to enable the LD manually
851 * otherwise firmware should have done it for us.
852 */
853 if (cpu_has_feature(CPU_FTR_HVMODE))
854 mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_LD);
855}
856
857static void __init set_decrementer_max(void)
858{
859 struct device_node *cpu;
860 u32 bits = 32;
861
862 /* Prior to ISAv3 the decrementer is always 32 bit */
863 if (!cpu_has_feature(CPU_FTR_ARCH_300))
864 return;
865
866 cpu = of_find_node_by_type(NULL, "cpu");
867
868 if (of_property_read_u32(cpu, "ibm,dec-bits", &bits) == 0) {
869 if (bits > 64 || bits < 32) {
870 pr_warn("time_init: firmware supplied invalid ibm,dec-bits");
871 bits = 32;
872 }
873
874 /* calculate the signed maximum given this many bits */
875 decrementer_max = (1ul << (bits - 1)) - 1;
876 }
877
878 of_node_put(cpu);
879
880 pr_info("time_init: %u bit decrementer (max: %llx)\n",
881 bits, decrementer_max);
882}
883
c481887f 884static void __init init_decrementer_clockevent(void)
d831d0b8 885{
8b78fdb0 886 register_decrementer_clockevent(smp_processor_id());
d831d0b8
TB
887}
888
889void secondary_cpu_time_init(void)
890{
79901024
OH
891 /* Enable and test the large decrementer for this cpu */
892 enable_large_decrementer();
893
77c0a700
BH
894 /* Start the decrementer on CPUs that have manual control
895 * such as BookE
896 */
897 start_cpu_decrementer();
898
1fd02f66 899 /* FIME: Should make unrelated change to move snapshot_timebase
d831d0b8
TB
900 * call here ! */
901 register_decrementer_clockevent(smp_processor_id());
902}
903
f17bcb97
CL
904/*
905 * Divide a 128-bit dividend by a 32-bit divisor, leaving a 128 bit
906 * result.
907 */
908static __init void div128_by_32(u64 dividend_high, u64 dividend_low,
909 unsigned int divisor, struct div_result *dr)
910{
911 unsigned long a, b, c, d;
912 unsigned long w, x, y, z;
913 u64 ra, rb, rc;
914
915 a = dividend_high >> 32;
916 b = dividend_high & 0xffffffff;
917 c = dividend_low >> 32;
918 d = dividend_low & 0xffffffff;
919
920 w = a / divisor;
921 ra = ((u64)(a - (w * divisor)) << 32) + b;
922
923 rb = ((u64)do_div(ra, divisor) << 32) + c;
924 x = ra;
925
926 rc = ((u64)do_div(rb, divisor) << 32) + d;
927 y = rb;
928
929 do_div(rc, divisor);
930 z = rc;
931
932 dr->result_high = ((u64)w << 32) + x;
933 dr->result_low = ((u64)y << 32) + z;
934}
935
f2783c15 936/* This function is only called on the boot processor */
1da177e4
LT
937void __init time_init(void)
938{
1da177e4 939 struct div_result res;
d75d68cf 940 u64 scale;
f2783c15
PM
941 unsigned shift;
942
a4c5a355 943 /* Normal PowerPC with timebase register */
0aafbdf3
CL
944 if (ppc_md.calibrate_decr)
945 ppc_md.calibrate_decr();
946 else
947 generic_calibrate_decr();
948
a4c5a355
CL
949 printk(KERN_DEBUG "time_init: decrementer frequency = %lu.%.6lu MHz\n",
950 ppc_tb_freq / 1000000, ppc_tb_freq % 1000000);
951 printk(KERN_DEBUG "time_init: processor frequency = %lu.%.6lu MHz\n",
952 ppc_proc_freq / 1000000, ppc_proc_freq % 1000000);
374e99d4
PM
953
954 tb_ticks_per_jiffy = ppc_tb_freq / HZ;
092b8f34 955 tb_ticks_per_sec = ppc_tb_freq;
374e99d4 956 tb_ticks_per_usec = ppc_tb_freq / 1000000;
092b8f34 957
1da177e4
LT
958 /*
959 * Compute scale factor for sched_clock.
960 * The calibrate_decr() function has set tb_ticks_per_sec,
961 * which is the timebase frequency.
962 * We compute 1e9 * 2^64 / tb_ticks_per_sec and interpret
963 * the 128-bit result as a 64.64 fixed-point number.
964 * We then shift that number right until it is less than 1.0,
965 * giving us the scale factor and shift count to use in
966 * sched_clock().
967 */
968 div128_by_32(1000000000, 0, tb_ticks_per_sec, &res);
969 scale = res.result_low;
970 for (shift = 0; res.result_high != 0; ++shift) {
971 scale = (scale >> 1) | (res.result_high << 63);
972 res.result_high >>= 1;
973 }
974 tb_to_ns_scale = scale;
975 tb_to_ns_shift = shift;
fc9069fe 976 /* Save the current timebase to pretty up CONFIG_PRINTK_TIME */
6601ec1c 977 boot_tb = get_tb();
1da177e4 978
092b8f34 979 /* If platform provided a timezone (pmac), we correct the time */
621692cb 980 if (timezone_offset) {
092b8f34
PM
981 sys_tz.tz_minuteswest = -timezone_offset / 60;
982 sys_tz.tz_dsttime = 0;
621692cb 983 }
092b8f34 984
223970df 985 vdso_k_arch_data->tb_ticks_per_sec = tb_ticks_per_sec;
1184674d
TW
986#ifdef CONFIG_PPC64_PROC_SYSTEMCFG
987 systemcfg->tb_ticks_per_sec = tb_ticks_per_sec;
988#endif
1da177e4 989
79901024
OH
990 /* initialise and enable the large decrementer (if we have one) */
991 set_decrementer_max();
992 enable_large_decrementer();
993
77c0a700
BH
994 /* Start the decrementer on CPUs that have manual control
995 * such as BookE
996 */
997 start_cpu_decrementer();
998
f5339277
SR
999 /* Register the clocksource */
1000 clocksource_init();
4a4cfe38 1001
d831d0b8 1002 init_decrementer_clockevent();
0d948730 1003 tick_setup_hrtimer_broadcast();
f0d37300 1004
f0d37300 1005 of_clk_init(NULL);
b709e32e 1006 enable_sched_clock_irqtime();
1da177e4
LT
1007}
1008
177996e6
BH
1009/* We don't need to calibrate delay, we use the CPU timebase for that */
1010void calibrate_delay(void)
1011{
1012 /* Some generic code (such as spinlock debug) use loops_per_jiffy
1013 * as the number of __delay(1) in a jiffy, so make it so
1014 */
1015 loops_per_jiffy = tb_ticks_per_jiffy;
1016}
1017
169047f4
AB
1018#if IS_ENABLED(CONFIG_RTC_DRV_GENERIC)
1019static int rtc_generic_get_time(struct device *dev, struct rtc_time *tm)
1020{
1021 ppc_md.get_rtc_time(tm);
890ae797 1022 return 0;
169047f4
AB
1023}
1024
1025static int rtc_generic_set_time(struct device *dev, struct rtc_time *tm)
1026{
1027 if (!ppc_md.set_rtc_time)
1028 return -EOPNOTSUPP;
1029
1030 if (ppc_md.set_rtc_time(tm) < 0)
1031 return -EOPNOTSUPP;
1032
1033 return 0;
1034}
1035
1036static const struct rtc_class_ops rtc_generic_ops = {
1037 .read_time = rtc_generic_get_time,
1038 .set_time = rtc_generic_set_time,
1039};
1040
bcd68a70
GU
1041static int __init rtc_init(void)
1042{
1043 struct platform_device *pdev;
1044
1045 if (!ppc_md.get_rtc_time)
1046 return -ENODEV;
1047
169047f4
AB
1048 pdev = platform_device_register_data(NULL, "rtc-generic", -1,
1049 &rtc_generic_ops,
1050 sizeof(rtc_generic_ops));
bcd68a70 1051
8c6ffba0 1052 return PTR_ERR_OR_ZERO(pdev);
bcd68a70
GU
1053}
1054
8f6b9512 1055device_initcall(rtc_init);
169047f4 1056#endif