0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef _LINUX_CLOCKSOURCE_H
0010 #define _LINUX_CLOCKSOURCE_H
0011
0012 #include <linux/types.h>
0013 #include <linux/timex.h>
0014 #include <linux/time.h>
0015 #include <linux/list.h>
0016 #include <linux/cache.h>
0017 #include <linux/timer.h>
0018 #include <linux/init.h>
0019 #include <linux/of.h>
0020 #include <linux/clocksource_ids.h>
0021 #include <asm/div64.h>
0022 #include <asm/io.h>
0023
0024 struct clocksource;
0025 struct module;
0026
0027 #if defined(CONFIG_ARCH_CLOCKSOURCE_DATA) || \
0028 defined(CONFIG_GENERIC_GETTIMEOFDAY)
0029 #include <asm/clocksource.h>
0030 #endif
0031
0032 #include <vdso/clocksource.h>
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096 struct clocksource {
0097 u64 (*read)(struct clocksource *cs);
0098 u64 mask;
0099 u32 mult;
0100 u32 shift;
0101 u64 max_idle_ns;
0102 u32 maxadj;
0103 u32 uncertainty_margin;
0104 #ifdef CONFIG_ARCH_CLOCKSOURCE_DATA
0105 struct arch_clocksource_data archdata;
0106 #endif
0107 u64 max_cycles;
0108 const char *name;
0109 struct list_head list;
0110 int rating;
0111 enum clocksource_ids id;
0112 enum vdso_clock_mode vdso_clock_mode;
0113 unsigned long flags;
0114
0115 int (*enable)(struct clocksource *cs);
0116 void (*disable)(struct clocksource *cs);
0117 void (*suspend)(struct clocksource *cs);
0118 void (*resume)(struct clocksource *cs);
0119 void (*mark_unstable)(struct clocksource *cs);
0120 void (*tick_stable)(struct clocksource *cs);
0121
0122
0123 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
0124
0125 struct list_head wd_list;
0126 u64 cs_last;
0127 u64 wd_last;
0128 #endif
0129 struct module *owner;
0130 };
0131
0132
0133
0134
0135 #define CLOCK_SOURCE_IS_CONTINUOUS 0x01
0136 #define CLOCK_SOURCE_MUST_VERIFY 0x02
0137
0138 #define CLOCK_SOURCE_WATCHDOG 0x10
0139 #define CLOCK_SOURCE_VALID_FOR_HRES 0x20
0140 #define CLOCK_SOURCE_UNSTABLE 0x40
0141 #define CLOCK_SOURCE_SUSPEND_NONSTOP 0x80
0142 #define CLOCK_SOURCE_RESELECT 0x100
0143 #define CLOCK_SOURCE_VERIFY_PERCPU 0x200
0144
0145 #define CLOCKSOURCE_MASK(bits) GENMASK_ULL((bits) - 1, 0)
0146
0147 static inline u32 clocksource_freq2mult(u32 freq, u32 shift_constant, u64 from)
0148 {
0149
0150
0151
0152
0153
0154
0155
0156 u64 tmp = ((u64)from) << shift_constant;
0157
0158 tmp += freq/2;
0159 do_div(tmp, freq);
0160
0161 return (u32)tmp;
0162 }
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172 static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)
0173 {
0174 return clocksource_freq2mult(khz, shift_constant, NSEC_PER_MSEC);
0175 }
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186 static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)
0187 {
0188 return clocksource_freq2mult(hz, shift_constant, NSEC_PER_SEC);
0189 }
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204 static inline s64 clocksource_cyc2ns(u64 cycles, u32 mult, u32 shift)
0205 {
0206 return ((u64) cycles * mult) >> shift;
0207 }
0208
0209
0210 extern int clocksource_unregister(struct clocksource*);
0211 extern void clocksource_touch_watchdog(void);
0212 extern void clocksource_change_rating(struct clocksource *cs, int rating);
0213 extern void clocksource_suspend(void);
0214 extern void clocksource_resume(void);
0215 extern struct clocksource * __init clocksource_default_clock(void);
0216 extern void clocksource_mark_unstable(struct clocksource *cs);
0217 extern void
0218 clocksource_start_suspend_timing(struct clocksource *cs, u64 start_cycles);
0219 extern u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 now);
0220
0221 extern u64
0222 clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cycles);
0223 extern void
0224 clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec);
0225
0226
0227
0228
0229
0230 extern int
0231 __clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq);
0232 extern void
0233 __clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq);
0234
0235
0236
0237
0238
0239 static inline int __clocksource_register(struct clocksource *cs)
0240 {
0241 return __clocksource_register_scale(cs, 1, 0);
0242 }
0243
0244 static inline int clocksource_register_hz(struct clocksource *cs, u32 hz)
0245 {
0246 return __clocksource_register_scale(cs, 1, hz);
0247 }
0248
0249 static inline int clocksource_register_khz(struct clocksource *cs, u32 khz)
0250 {
0251 return __clocksource_register_scale(cs, 1000, khz);
0252 }
0253
0254 static inline void __clocksource_update_freq_hz(struct clocksource *cs, u32 hz)
0255 {
0256 __clocksource_update_freq_scale(cs, 1, hz);
0257 }
0258
0259 static inline void __clocksource_update_freq_khz(struct clocksource *cs, u32 khz)
0260 {
0261 __clocksource_update_freq_scale(cs, 1000, khz);
0262 }
0263
0264 #ifdef CONFIG_ARCH_CLOCKSOURCE_INIT
0265 extern void clocksource_arch_init(struct clocksource *cs);
0266 #else
0267 static inline void clocksource_arch_init(struct clocksource *cs) { }
0268 #endif
0269
0270 extern int timekeeping_notify(struct clocksource *clock);
0271
0272 extern u64 clocksource_mmio_readl_up(struct clocksource *);
0273 extern u64 clocksource_mmio_readl_down(struct clocksource *);
0274 extern u64 clocksource_mmio_readw_up(struct clocksource *);
0275 extern u64 clocksource_mmio_readw_down(struct clocksource *);
0276
0277 extern int clocksource_mmio_init(void __iomem *, const char *,
0278 unsigned long, int, unsigned, u64 (*)(struct clocksource *));
0279
0280 extern int clocksource_i8253_init(void);
0281
0282 #define TIMER_OF_DECLARE(name, compat, fn) \
0283 OF_DECLARE_1_RET(timer, name, compat, fn)
0284
0285 #ifdef CONFIG_TIMER_PROBE
0286 extern void timer_probe(void);
0287 #else
0288 static inline void timer_probe(void) {}
0289 #endif
0290
0291 #define TIMER_ACPI_DECLARE(name, table_id, fn) \
0292 ACPI_DECLARE_PROBE_ENTRY(timer, name, table_id, 0, NULL, 0, fn)
0293
0294 extern ulong max_cswd_read_retries;
0295 void clocksource_verify_percpu(struct clocksource *cs);
0296
0297 #endif