blob: 2f566c1a754cabc8105a7f8957b69553e8831825 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-or-later */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 */
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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000027
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
David Brazdil0f672f62019-12-10 10:32:29 +000034extern bool tb_invalid;
35
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000036struct div_result {
37 u64 result_high;
38 u64 result_low;
39};
40
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000041/* For compatibility, get_tbl() is defined as get_tb() on ppc64 */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000042static inline unsigned long get_tbl(void)
43{
Olivier Deprez157378f2022-04-04 15:47:50 +020044 return mftb();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000045}
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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000056static inline u64 get_tb(void)
57{
58 unsigned int tbhi, tblo, tbhi2;
59
Olivier Deprez157378f2022-04-04 15:47:50 +020060 if (IS_ENABLED(CONFIG_PPC64))
61 return mftb();
62
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063 do {
Olivier Deprez157378f2022-04-04 15:47:50 +020064 tbhi = mftbu();
65 tblo = mftb();
66 tbhi2 = mftbu();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000067 } while (tbhi != tbhi2);
68
69 return ((u64)tbhi << 32) | tblo;
70}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000071
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{
Olivier Deprez157378f2022-04-04 15:47:50 +020087 if (IS_ENABLED(CONFIG_40x))
88 return mfspr(SPRN_PIT);
89
90 return mfspr(SPRN_DEC);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000091}
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{
Olivier Deprez157378f2022-04-04 15:47:50 +0200100 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);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000106}
107
108static inline unsigned long tb_ticks_since(unsigned long tstamp)
109{
Olivier Deprez157378f2022-04-04 15:47:50 +0200110 return mftb() - tstamp;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000111}
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
Olivier Deprez157378f2022-04-04 15:47:50 +0200134/* SPLPAR */
135void accumulate_stolen_time(void);
136
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000137#endif /* __KERNEL__ */
138#endif /* __POWERPC_TIME_H */