Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [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. |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #ifndef __POWERPC_TIME_H |
| 10 | #define __POWERPC_TIME_H |
| 11 | |
| 12 | #ifdef __KERNEL__ |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 13 | #include <linux/types.h> |
| 14 | #include <linux/percpu.h> |
| 15 | |
| 16 | #include <asm/processor.h> |
Kevin Hao | b92a226 | 2016-07-23 14:42:40 +0530 | [diff] [blame] | 17 | #include <asm/cpu_has_feature.h> |
Christophe Leroy | d26b381 | 2020-11-27 00:10:00 +1100 | [diff] [blame] | 18 | #include <asm/vdso/timebase.h> |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 19 | |
| 20 | /* time.c */ |
Nicholas Piggin | 9581991 | 2021-11-23 19:51:43 +1000 | [diff] [blame] | 21 | extern u64 decrementer_max; |
| 22 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 23 | extern unsigned long tb_ticks_per_jiffy; |
| 24 | extern unsigned long tb_ticks_per_usec; |
| 25 | extern unsigned long tb_ticks_per_sec; |
Bharat Bhushan | 6e35994 | 2012-04-18 06:01:19 +0000 | [diff] [blame] | 26 | extern struct clock_event_device decrementer_clockevent; |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 27 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 28 | |
| 29 | extern void generic_calibrate_decr(void); |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 30 | |
| 31 | /* Some sane defaults: 125 MHz timebase, 1GHz processor */ |
| 32 | extern unsigned long ppc_proc_freq; |
| 33 | #define DEFAULT_PROC_FREQ (DEFAULT_TB_FREQ * 8) |
| 34 | extern unsigned long ppc_tb_freq; |
| 35 | #define DEFAULT_TB_FREQ 125000000UL |
| 36 | |
Mahesh Salgaonkar | de26912 | 2019-03-05 01:12:19 +0530 | [diff] [blame] | 37 | extern bool tb_invalid; |
| 38 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 39 | struct div_result { |
| 40 | u64 result_high; |
| 41 | u64 result_low; |
| 42 | }; |
| 43 | |
Aneesh Kumar K.V | 8f42ab2 | 2014-06-05 17:38:02 +0530 | [diff] [blame] | 44 | static inline u64 get_vtb(void) |
| 45 | { |
Aneesh Kumar K.V | 8f42ab2 | 2014-06-05 17:38:02 +0530 | [diff] [blame] | 46 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
Kevin Hao | 905259e | 2016-07-23 14:42:39 +0530 | [diff] [blame] | 47 | return mfspr(SPRN_VTB); |
Christophe Leroy | c3cb5db | 2020-10-01 10:59:20 +0000 | [diff] [blame] | 48 | |
Aneesh Kumar K.V | 8f42ab2 | 2014-06-05 17:38:02 +0530 | [diff] [blame] | 49 | return 0; |
| 50 | } |
| 51 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 52 | /* 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'Halloran | 79901024 | 2016-07-01 16:20:39 +1000 | [diff] [blame] | 58 | static inline u64 get_dec(void) |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 59 | { |
Christophe Leroy | 63f9d9d | 2020-10-01 10:59:19 +0000 | [diff] [blame] | 60 | if (IS_ENABLED(CONFIG_40x)) |
| 61 | return mfspr(SPRN_PIT); |
| 62 | |
| 63 | return mfspr(SPRN_DEC); |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 64 | } |
| 65 | |
Paul Mackerras | 43875cc | 2007-10-31 22:25:35 +1100 | [diff] [blame] | 66 | /* |
| 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'Halloran | 79901024 | 2016-07-01 16:20:39 +1000 | [diff] [blame] | 71 | static inline void set_dec(u64 val) |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 72 | { |
Christophe Leroy | 63f9d9d | 2020-10-01 10:59:19 +0000 | [diff] [blame] | 73 | 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 Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static inline unsigned long tb_ticks_since(unsigned long tstamp) |
| 82 | { |
Christophe Leroy | 942e891 | 2020-10-01 12:42:41 +0000 | [diff] [blame] | 83 | return mftb() - tstamp; |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 84 | } |
| 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 |
| 93 | extern u64 mulhdu(u64, u64); |
| 94 | #endif |
| 95 | |
Paul Mackerras | a5b518e | 2005-10-22 14:55:23 +1000 | [diff] [blame] | 96 | extern void div128_by_32(u64 dividend_high, u64 dividend_low, |
| 97 | unsigned divisor, struct div_result *dr); |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 98 | |
Tony Breeds | d831d0b | 2007-09-21 13:26:03 +1000 | [diff] [blame] | 99 | extern void secondary_cpu_time_init(void); |
Mathieu Malaterre | 848092f | 2018-02-25 18:22:27 +0100 | [diff] [blame] | 100 | extern void __init time_init(void); |
Tony Breeds | 71712b4 | 2007-06-22 16:54:30 +1000 | [diff] [blame] | 101 | |
Anton Blanchard | 7df1027 | 2011-11-23 20:07:22 +0000 | [diff] [blame] | 102 | DECLARE_PER_CPU(u64, decrementers_next_tb); |
Anton Blanchard | 37fb9a0 | 2011-11-23 20:07:17 +0000 | [diff] [blame] | 103 | |
Nicholas Piggin | 4ebbd07 | 2021-11-23 19:51:42 +1000 | [diff] [blame] | 104 | static inline u64 timer_get_next_tb(void) |
| 105 | { |
| 106 | return __this_cpu_read(decrementers_next_tb); |
| 107 | } |
| 108 | |
Nicholas Piggin | 25aa145 | 2021-11-23 19:51:45 +1000 | [diff] [blame] | 109 | #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE |
| 110 | void timer_rearm_host_dec(u64 now); |
| 111 | #endif |
| 112 | |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 113 | /* Convert timebase ticks to nanoseconds */ |
| 114 | unsigned long long tb_to_ns(unsigned long long tb_ticks); |
| 115 | |
Nicholas Piggin | 0440b8a | 2021-01-30 23:08:30 +1000 | [diff] [blame] | 116 | void timer_broadcast_interrupt(void); |
| 117 | |
Nicholas Piggin | 68b3458 | 2020-02-26 03:35:34 +1000 | [diff] [blame] | 118 | /* SPLPAR */ |
| 119 | void accumulate_stolen_time(void); |
| 120 | |
Paul Mackerras | f2783c1 | 2005-10-20 09:23:26 +1000 | [diff] [blame] | 121 | #endif /* __KERNEL__ */ |
Kim Phillips | 7a69af6 | 2006-09-26 17:46:37 -0500 | [diff] [blame] | 122 | #endif /* __POWERPC_TIME_H */ |