Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*  linux/include/linux/clocksource.h
0003  *
0004  *  This file contains the structure definitions for clocksources.
0005  *
0006  *  If you are not a clocksource, or timekeeping code, you should
0007  *  not be including this file!
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  * struct clocksource - hardware abstraction for a free running counter
0036  *  Provides mostly state-free accessors to the underlying hardware.
0037  *  This is the structure used for system time.
0038  *
0039  * @read:       Returns a cycle value, passes clocksource as argument
0040  * @mask:       Bitmask for two's complement
0041  *          subtraction of non 64 bit counters
0042  * @mult:       Cycle to nanosecond multiplier
0043  * @shift:      Cycle to nanosecond divisor (power of two)
0044  * @max_idle_ns:    Maximum idle time permitted by the clocksource (nsecs)
0045  * @maxadj:     Maximum adjustment value to mult (~11%)
0046  * @uncertainty_margin: Maximum uncertainty in nanoseconds per half second.
0047  *          Zero says to use default WATCHDOG_THRESHOLD.
0048  * @archdata:       Optional arch-specific data
0049  * @max_cycles:     Maximum safe cycle value which won't overflow on
0050  *          multiplication
0051  * @name:       Pointer to clocksource name
0052  * @list:       List head for registration (internal)
0053  * @rating:     Rating value for selection (higher is better)
0054  *          To avoid rating inflation the following
0055  *          list should give you a guide as to how
0056  *          to assign your clocksource a rating
0057  *          1-99: Unfit for real use
0058  *              Only available for bootup and testing purposes.
0059  *          100-199: Base level usability.
0060  *              Functional for real use, but not desired.
0061  *          200-299: Good.
0062  *              A correct and usable clocksource.
0063  *          300-399: Desired.
0064  *              A reasonably fast and accurate clocksource.
0065  *          400-499: Perfect
0066  *              The ideal clocksource. A must-use where
0067  *              available.
0068  * @id:         Defaults to CSID_GENERIC. The id value is captured
0069  *          in certain snapshot functions to allow callers to
0070  *          validate the clocksource from which the snapshot was
0071  *          taken.
0072  * @flags:      Flags describing special properties
0073  * @enable:     Optional function to enable the clocksource
0074  * @disable:        Optional function to disable the clocksource
0075  * @suspend:        Optional suspend function for the clocksource
0076  * @resume:     Optional resume function for the clocksource
0077  * @mark_unstable:  Optional function to inform the clocksource driver that
0078  *          the watchdog marked the clocksource unstable
0079  * @tick_stable:        Optional function called periodically from the watchdog
0080  *          code to provide stable synchronization points
0081  * @wd_list:        List head to enqueue into the watchdog list (internal)
0082  * @cs_last:        Last clocksource value for clocksource watchdog
0083  * @wd_last:        Last watchdog value corresponding to @cs_last
0084  * @owner:      Module reference, must be set by clocksource in modules
0085  *
0086  * Note: This struct is not used in hotpathes of the timekeeping code
0087  * because the timekeeper caches the hot path fields in its own data
0088  * structure, so no cache line alignment is required,
0089  *
0090  * The pointer to the clocksource itself is handed to the read
0091  * callback. If you need extra information there you can wrap struct
0092  * clocksource into your own struct. Depending on the amount of
0093  * information you need you should consider to cache line align that
0094  * structure.
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     /* private: */
0123 #ifdef CONFIG_CLOCKSOURCE_WATCHDOG
0124     /* Watchdog related data, used by the framework */
0125     struct list_head    wd_list;
0126     u64         cs_last;
0127     u64         wd_last;
0128 #endif
0129     struct module       *owner;
0130 };
0131 
0132 /*
0133  * Clock source flags bits::
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 /* simplify initialization of mask field */
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     /*  freq = cyc/from
0150      *  mult/2^shift  = ns/cyc
0151      *  mult = ns/cyc * 2^shift
0152      *  mult = from/freq * 2^shift
0153      *  mult = from * 2^shift / freq
0154      *  mult = (from<<shift) / freq
0155      */
0156     u64 tmp = ((u64)from) << shift_constant;
0157 
0158     tmp += freq/2; /* round for do_div */
0159     do_div(tmp, freq);
0160 
0161     return (u32)tmp;
0162 }
0163 
0164 /**
0165  * clocksource_khz2mult - calculates mult from khz and shift
0166  * @khz:        Clocksource frequency in KHz
0167  * @shift_constant: Clocksource shift factor
0168  *
0169  * Helper functions that converts a khz counter frequency to a timsource
0170  * multiplier, given the clocksource shift value
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  * clocksource_hz2mult - calculates mult from hz and shift
0179  * @hz:         Clocksource frequency in Hz
0180  * @shift_constant: Clocksource shift factor
0181  *
0182  * Helper functions that converts a hz counter
0183  * frequency to a timsource multiplier, given the
0184  * clocksource shift value
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  * clocksource_cyc2ns - converts clocksource cycles to nanoseconds
0193  * @cycles: cycles
0194  * @mult:   cycle to nanosecond multiplier
0195  * @shift:  cycle to nanosecond divisor (power of two)
0196  *
0197  * Converts clocksource cycles to nanoseconds, using the given @mult and @shift.
0198  * The code is optimized for performance and is not intended to work
0199  * with absolute clocksource cycles (as those will easily overflow),
0200  * but is only intended to be used with relative (delta) clocksource cycles.
0201  *
0202  * XXX - This could use some mult_lxl_ll() asm optimization
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  * Don't call __clocksource_register_scale directly, use
0228  * clocksource_register_hz/khz
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  * Don't call this unless you are a default clocksource
0237  * (AKA: jiffies) and absolutely have to.
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 /* _LINUX_CLOCKSOURCE_H */