1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
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.
7 */
8
9#ifndef __POWERPC_TIME_H
10#define __POWERPC_TIME_H
11
12#ifdef __KERNEL__
13#include <linux/types.h>
14#include <linux/percpu.h>
15
16#include <asm/processor.h>
17#include <asm/cpu_has_feature.h>
18
19/* time.c */
20extern unsigned long tb_ticks_per_jiffy;
21extern unsigned long tb_ticks_per_usec;
22extern unsigned long tb_ticks_per_sec;
23extern struct clock_event_device decrementer_clockevent;
24
25
26extern void generic_calibrate_decr(void);
27
28/* Some sane defaults: 125 MHz timebase, 1GHz processor */
29extern unsigned long ppc_proc_freq;
30#define DEFAULT_PROC_FREQ	(DEFAULT_TB_FREQ * 8)
31extern unsigned long ppc_tb_freq;
32#define DEFAULT_TB_FREQ		125000000UL
33
34extern bool tb_invalid;
35
36struct div_result {
37	u64 result_high;
38	u64 result_low;
39};
40
41/* For compatibility, get_tbl() is defined as get_tb() on ppc64 */
42static inline unsigned long get_tbl(void)
43{
44	return mftb();
45}
46
47static inline u64 get_vtb(void)
48{
49#ifdef CONFIG_PPC_BOOK3S_64
50	if (cpu_has_feature(CPU_FTR_ARCH_207S))
51		return mfspr(SPRN_VTB);
52#endif
53	return 0;
54}
55
56static inline u64 get_tb(void)
57{
58	unsigned int tbhi, tblo, tbhi2;
59
60	if (IS_ENABLED(CONFIG_PPC64))
61		return mftb();
62
63	do {
64		tbhi = mftbu();
65		tblo = mftb();
66		tbhi2 = mftbu();
67	} while (tbhi != tbhi2);
68
69	return ((u64)tbhi << 32) | tblo;
70}
71
72static inline void set_tb(unsigned int upper, unsigned int lower)
73{
74	mtspr(SPRN_TBWL, 0);
75	mtspr(SPRN_TBWU, upper);
76	mtspr(SPRN_TBWL, lower);
77}
78
79/* Accessor functions for the decrementer register.
80 * The 4xx doesn't even have a decrementer.  I tried to use the
81 * generic timer interrupt code, which seems OK, with the 4xx PIT
82 * in auto-reload mode.  The problem is PIT stops counting when it
83 * hits zero.  If it would wrap, we could use it just like a decrementer.
84 */
85static inline u64 get_dec(void)
86{
87	if (IS_ENABLED(CONFIG_40x))
88		return mfspr(SPRN_PIT);
89
90	return mfspr(SPRN_DEC);
91}
92
93/*
94 * Note: Book E and 4xx processors differ from other PowerPC processors
95 * in when the decrementer generates its interrupt: on the 1 to 0
96 * transition for Book E/4xx, but on the 0 to -1 transition for others.
97 */
98static inline void set_dec(u64 val)
99{
100	if (IS_ENABLED(CONFIG_40x))
101		mtspr(SPRN_PIT, (u32)val);
102	else if (IS_ENABLED(CONFIG_BOOKE))
103		mtspr(SPRN_DEC, val);
104	else
105		mtspr(SPRN_DEC, val - 1);
106}
107
108static inline unsigned long tb_ticks_since(unsigned long tstamp)
109{
110	return mftb() - tstamp;
111}
112
113#define mulhwu(x,y) \
114({unsigned z; asm ("mulhwu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
115
116#ifdef CONFIG_PPC64
117#define mulhdu(x,y) \
118({unsigned long z; asm ("mulhdu %0,%1,%2" : "=r" (z) : "r" (x), "r" (y)); z;})
119#else
120extern u64 mulhdu(u64, u64);
121#endif
122
123extern void div128_by_32(u64 dividend_high, u64 dividend_low,
124			 unsigned divisor, struct div_result *dr);
125
126extern void secondary_cpu_time_init(void);
127extern void __init time_init(void);
128
129DECLARE_PER_CPU(u64, decrementers_next_tb);
130
131/* Convert timebase ticks to nanoseconds */
132unsigned long long tb_to_ns(unsigned long long tb_ticks);
133
134/* SPLPAR */
135void accumulate_stolen_time(void);
136
137#endif /* __KERNEL__ */
138#endif /* __POWERPC_TIME_H */
139