blob: 924b2157882fad9037e388bd795365c6d03ad436 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Paul Mackerrasf2783c12005-10-20 09:23:26 +10002/*
3 * Common time prototypes and such for 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.
Paul Mackerrasf2783c12005-10-20 09:23:26 +10007 */
8
9#ifndef __POWERPC_TIME_H
10#define __POWERPC_TIME_H
11
12#ifdef __KERNEL__
Paul Mackerrasf2783c12005-10-20 09:23:26 +100013#include <linux/types.h>
14#include <linux/percpu.h>
15
16#include <asm/processor.h>
Kevin Haob92a2262016-07-23 14:42:40 +053017#include <asm/cpu_has_feature.h>
Christophe Leroyd26b3812020-11-27 00:10:00 +110018#include <asm/vdso/timebase.h>
Paul Mackerrasf2783c12005-10-20 09:23:26 +100019
20/* time.c */
Nicholas Piggin95819912021-11-23 19:51:43 +100021extern u64 decrementer_max;
22
Paul Mackerrasf2783c12005-10-20 09:23:26 +100023extern unsigned long tb_ticks_per_jiffy;
24extern unsigned long tb_ticks_per_usec;
25extern unsigned long tb_ticks_per_sec;
Bharat Bhushan6e359942012-04-18 06:01:19 +000026extern struct clock_event_device decrementer_clockevent;
Paul Mackerrasf2783c12005-10-20 09:23:26 +100027
Paul Mackerrasf2783c12005-10-20 09:23:26 +100028
29extern void generic_calibrate_decr(void);
Paul Mackerrasf2783c12005-10-20 09:23:26 +100030
31/* Some sane defaults: 125 MHz timebase, 1GHz processor */
32extern unsigned long ppc_proc_freq;
33#define DEFAULT_PROC_FREQ (DEFAULT_TB_FREQ * 8)
34extern unsigned long ppc_tb_freq;
35#define DEFAULT_TB_FREQ 125000000UL
36
Mahesh Salgaonkarde269122019-03-05 01:12:19 +053037extern bool tb_invalid;
38
Paul Mackerrasf2783c12005-10-20 09:23:26 +100039struct div_result {
40 u64 result_high;
41 u64 result_low;
42};
43
Aneesh Kumar K.V8f42ab22014-06-05 17:38:02 +053044static inline u64 get_vtb(void)
45{
Aneesh Kumar K.V8f42ab22014-06-05 17:38:02 +053046 if (cpu_has_feature(CPU_FTR_ARCH_207S))
Kevin Hao905259e2016-07-23 14:42:39 +053047 return mfspr(SPRN_VTB);
Christophe Leroyc3cb5db2020-10-01 10:59:20 +000048
Aneesh Kumar K.V8f42ab22014-06-05 17:38:02 +053049 return 0;
50}
51
Paul Mackerrasf2783c12005-10-20 09:23:26 +100052/* Accessor functions for the decrementer register.
53 * The 4xx doesn't even have a decrementer. I tried to use the
54 * generic timer interrupt code, which seems OK, with the 4xx PIT
55 * in auto-reload mode. The problem is PIT stops counting when it
56 * hits zero. If it would wrap, we could use it just like a decrementer.
57 */
Oliver O'Halloran799010242016-07-01 16:20:39 +100058static inline u64 get_dec(void)
Paul Mackerrasf2783c12005-10-20 09:23:26 +100059{
Christophe Leroy63f9d9d2020-10-01 10:59:19 +000060 if (IS_ENABLED(CONFIG_40x))
61 return mfspr(SPRN_PIT);
62
63 return mfspr(SPRN_DEC);
Paul Mackerrasf2783c12005-10-20 09:23:26 +100064}
65
Paul Mackerras43875cc2007-10-31 22:25:35 +110066/*
67 * Note: Book E and 4xx processors differ from other PowerPC processors
68 * in when the decrementer generates its interrupt: on the 1 to 0
69 * transition for Book E/4xx, but on the 0 to -1 transition for others.
70 */
Oliver O'Halloran799010242016-07-01 16:20:39 +100071static inline void set_dec(u64 val)
Paul Mackerrasf2783c12005-10-20 09:23:26 +100072{
Christophe Leroy63f9d9d2020-10-01 10:59:19 +000073 if (IS_ENABLED(CONFIG_40x))
74 mtspr(SPRN_PIT, (u32)val);
75 else if (IS_ENABLED(CONFIG_BOOKE))
76 mtspr(SPRN_DEC, val);
77 else
78 mtspr(SPRN_DEC, val - 1);
Paul Mackerrasf2783c12005-10-20 09:23:26 +100079}
80
81static inline unsigned long tb_ticks_since(unsigned long tstamp)
82{
Christophe Leroy942e8912020-10-01 12:42:41 +000083 return mftb() - tstamp;
Paul Mackerrasf2783c12005-10-20 09:23:26 +100084}
85
86#define mulhwu(x,y) \
87({unsigned z; asm ("mulhwu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
88
89#ifdef CONFIG_PPC64
90#define mulhdu(x,y) \
91({unsigned long z; asm ("mulhdu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
92#else
93extern u64 mulhdu(u64, u64);
94#endif
95
Paul Mackerrasa5b518e2005-10-22 14:55:23 +100096extern void div128_by_32(u64 dividend_high, u64 dividend_low,
97 unsigned divisor, struct div_result *dr);
Paul Mackerrasf2783c12005-10-20 09:23:26 +100098
Tony Breedsd831d0b2007-09-21 13:26:03 +100099extern void secondary_cpu_time_init(void);
Mathieu Malaterre848092f2018-02-25 18:22:27 +0100100extern void __init time_init(void);
Tony Breeds71712b42007-06-22 16:54:30 +1000101
Anton Blanchard7df10272011-11-23 20:07:22 +0000102DECLARE_PER_CPU(u64, decrementers_next_tb);
Anton Blanchard37fb9a02011-11-23 20:07:17 +0000103
Nicholas Piggin4ebbd072021-11-23 19:51:42 +1000104static inline u64 timer_get_next_tb(void)
105{
106 return __this_cpu_read(decrementers_next_tb);
107}
108
Nicholas Piggin25aa1452021-11-23 19:51:45 +1000109#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
110void timer_rearm_host_dec(u64 now);
111#endif
112
Paul Mackerrasb6c295d2015-03-28 14:21:02 +1100113/* Convert timebase ticks to nanoseconds */
114unsigned long long tb_to_ns(unsigned long long tb_ticks);
115
Nicholas Piggin0440b8a2021-01-30 23:08:30 +1000116void timer_broadcast_interrupt(void);
117
Nicholas Piggin68b34582020-02-26 03:35:34 +1000118/* SPLPAR */
119void accumulate_stolen_time(void);
120
Paul Mackerrasf2783c12005-10-20 09:23:26 +1000121#endif /* __KERNEL__ */
Kim Phillips7a69af62006-09-26 17:46:37 -0500122#endif /* __POWERPC_TIME_H */