David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 */ |
| 20 | extern unsigned long tb_ticks_per_jiffy; |
| 21 | extern unsigned long tb_ticks_per_usec; |
| 22 | extern unsigned long tb_ticks_per_sec; |
| 23 | extern struct clock_event_device decrementer_clockevent; |
| 24 | |
| 25 | |
| 26 | extern void generic_calibrate_decr(void); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 27 | |
| 28 | /* Some sane defaults: 125 MHz timebase, 1GHz processor */ |
| 29 | extern unsigned long ppc_proc_freq; |
| 30 | #define DEFAULT_PROC_FREQ (DEFAULT_TB_FREQ * 8) |
| 31 | extern unsigned long ppc_tb_freq; |
| 32 | #define DEFAULT_TB_FREQ 125000000UL |
| 33 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 34 | extern bool tb_invalid; |
| 35 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 36 | struct div_result { |
| 37 | u64 result_high; |
| 38 | u64 result_low; |
| 39 | }; |
| 40 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 41 | /* For compatibility, get_tbl() is defined as get_tb() on ppc64 */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 42 | static inline unsigned long get_tbl(void) |
| 43 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 44 | return mftb(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static 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 Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 56 | static inline u64 get_tb(void) |
| 57 | { |
| 58 | unsigned int tbhi, tblo, tbhi2; |
| 59 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 60 | if (IS_ENABLED(CONFIG_PPC64)) |
| 61 | return mftb(); |
| 62 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 63 | do { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 64 | tbhi = mftbu(); |
| 65 | tblo = mftb(); |
| 66 | tbhi2 = mftbu(); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 67 | } while (tbhi != tbhi2); |
| 68 | |
| 69 | return ((u64)tbhi << 32) | tblo; |
| 70 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 71 | |
| 72 | static 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 | */ |
| 85 | static inline u64 get_dec(void) |
| 86 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 87 | if (IS_ENABLED(CONFIG_40x)) |
| 88 | return mfspr(SPRN_PIT); |
| 89 | |
| 90 | return mfspr(SPRN_DEC); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | */ |
| 98 | static inline void set_dec(u64 val) |
| 99 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 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); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static inline unsigned long tb_ticks_since(unsigned long tstamp) |
| 109 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 110 | return mftb() - tstamp; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 |
| 120 | extern u64 mulhdu(u64, u64); |
| 121 | #endif |
| 122 | |
| 123 | extern void div128_by_32(u64 dividend_high, u64 dividend_low, |
| 124 | unsigned divisor, struct div_result *dr); |
| 125 | |
| 126 | extern void secondary_cpu_time_init(void); |
| 127 | extern void __init time_init(void); |
| 128 | |
| 129 | DECLARE_PER_CPU(u64, decrementers_next_tb); |
| 130 | |
| 131 | /* Convert timebase ticks to nanoseconds */ |
| 132 | unsigned long long tb_to_ns(unsigned long long tb_ticks); |
| 133 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 134 | /* SPLPAR */ |
| 135 | void accumulate_stolen_time(void); |
| 136 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 137 | #endif /* __KERNEL__ */ |
| 138 | #endif /* __POWERPC_TIME_H */ |