brintos

brintos / linux-shallow public Read only

0
0
Text · 11.0 KiB · 28ddba5 Raw
337 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2/*  linux/include/linux/clocksource.h3 *4 *  This file contains the structure definitions for clocksources.5 *6 *  If you are not a clocksource, or timekeeping code, you should7 *  not be including this file!8 */9#ifndef _LINUX_CLOCKSOURCE_H10#define _LINUX_CLOCKSOURCE_H11 12#include <linux/types.h>13#include <linux/timex.h>14#include <linux/time.h>15#include <linux/list.h>16#include <linux/cache.h>17#include <linux/timer.h>18#include <linux/init.h>19#include <linux/of.h>20#include <linux/clocksource_ids.h>21#include <asm/div64.h>22#include <asm/io.h>23 24struct clocksource_base;25struct clocksource;26struct module;27 28#if defined(CONFIG_ARCH_CLOCKSOURCE_DATA) || \29    defined(CONFIG_GENERIC_GETTIMEOFDAY)30#include <asm/clocksource.h>31#endif32 33#include <vdso/clocksource.h>34 35/**36 * struct clocksource - hardware abstraction for a free running counter37 *	Provides mostly state-free accessors to the underlying hardware.38 *	This is the structure used for system time.39 *40 * @read:		Returns a cycle value, passes clocksource as argument41 * @mask:		Bitmask for two's complement42 *			subtraction of non 64 bit counters43 * @mult:		Cycle to nanosecond multiplier44 * @shift:		Cycle to nanosecond divisor (power of two)45 * @max_idle_ns:	Maximum idle time permitted by the clocksource (nsecs)46 * @maxadj:		Maximum adjustment value to mult (~11%)47 * @uncertainty_margin:	Maximum uncertainty in nanoseconds per half second.48 *			Zero says to use default WATCHDOG_THRESHOLD.49 * @archdata:		Optional arch-specific data50 * @max_cycles:		Maximum safe cycle value which won't overflow on51 *			multiplication52 * @name:		Pointer to clocksource name53 * @list:		List head for registration (internal)54 * @freq_khz:		Clocksource frequency in khz.55 * @rating:		Rating value for selection (higher is better)56 *			To avoid rating inflation the following57 *			list should give you a guide as to how58 *			to assign your clocksource a rating59 *			1-99: Unfit for real use60 *				Only available for bootup and testing purposes.61 *			100-199: Base level usability.62 *				Functional for real use, but not desired.63 *			200-299: Good.64 *				A correct and usable clocksource.65 *			300-399: Desired.66 *				A reasonably fast and accurate clocksource.67 *			400-499: Perfect68 *				The ideal clocksource. A must-use where69 *				available.70 * @id:			Defaults to CSID_GENERIC. The id value is captured71 *			in certain snapshot functions to allow callers to72 *			validate the clocksource from which the snapshot was73 *			taken.74 * @flags:		Flags describing special properties75 * @base:		Hardware abstraction for clock on which a clocksource76 *			is based77 * @enable:		Optional function to enable the clocksource78 * @disable:		Optional function to disable the clocksource79 * @suspend:		Optional suspend function for the clocksource80 * @resume:		Optional resume function for the clocksource81 * @mark_unstable:	Optional function to inform the clocksource driver that82 *			the watchdog marked the clocksource unstable83 * @tick_stable:        Optional function called periodically from the watchdog84 *			code to provide stable synchronization points85 * @wd_list:		List head to enqueue into the watchdog list (internal)86 * @cs_last:		Last clocksource value for clocksource watchdog87 * @wd_last:		Last watchdog value corresponding to @cs_last88 * @owner:		Module reference, must be set by clocksource in modules89 *90 * Note: This struct is not used in hotpathes of the timekeeping code91 * because the timekeeper caches the hot path fields in its own data92 * structure, so no cache line alignment is required,93 *94 * The pointer to the clocksource itself is handed to the read95 * callback. If you need extra information there you can wrap struct96 * clocksource into your own struct. Depending on the amount of97 * information you need you should consider to cache line align that98 * structure.99 */100struct clocksource {101	u64			(*read)(struct clocksource *cs);102	u64			mask;103	u32			mult;104	u32			shift;105	u64			max_idle_ns;106	u32			maxadj;107	u32			uncertainty_margin;108#ifdef CONFIG_ARCH_CLOCKSOURCE_DATA109	struct arch_clocksource_data archdata;110#endif111	u64			max_cycles;112	const char		*name;113	struct list_head	list;114	u32			freq_khz;115	int			rating;116	enum clocksource_ids	id;117	enum vdso_clock_mode	vdso_clock_mode;118	unsigned long		flags;119	struct clocksource_base *base;120 121	int			(*enable)(struct clocksource *cs);122	void			(*disable)(struct clocksource *cs);123	void			(*suspend)(struct clocksource *cs);124	void			(*resume)(struct clocksource *cs);125	void			(*mark_unstable)(struct clocksource *cs);126	void			(*tick_stable)(struct clocksource *cs);127 128	/* private: */129#ifdef CONFIG_CLOCKSOURCE_WATCHDOG130	/* Watchdog related data, used by the framework */131	struct list_head	wd_list;132	u64			cs_last;133	u64			wd_last;134#endif135	struct module		*owner;136};137 138/*139 * Clock source flags bits::140 */141#define CLOCK_SOURCE_IS_CONTINUOUS		0x01142#define CLOCK_SOURCE_MUST_VERIFY		0x02143 144#define CLOCK_SOURCE_WATCHDOG			0x10145#define CLOCK_SOURCE_VALID_FOR_HRES		0x20146#define CLOCK_SOURCE_UNSTABLE			0x40147#define CLOCK_SOURCE_SUSPEND_NONSTOP		0x80148#define CLOCK_SOURCE_RESELECT			0x100149#define CLOCK_SOURCE_VERIFY_PERCPU		0x200150/* simplify initialization of mask field */151#define CLOCKSOURCE_MASK(bits) GENMASK_ULL((bits) - 1, 0)152 153static inline u32 clocksource_freq2mult(u32 freq, u32 shift_constant, u64 from)154{155	/*  freq = cyc/from156	 *  mult/2^shift  = ns/cyc157	 *  mult = ns/cyc * 2^shift158	 *  mult = from/freq * 2^shift159	 *  mult = from * 2^shift / freq160	 *  mult = (from<<shift) / freq161	 */162	u64 tmp = ((u64)from) << shift_constant;163 164	tmp += freq/2; /* round for do_div */165	do_div(tmp, freq);166 167	return (u32)tmp;168}169 170/**171 * clocksource_khz2mult - calculates mult from khz and shift172 * @khz:		Clocksource frequency in KHz173 * @shift_constant:	Clocksource shift factor174 *175 * Helper functions that converts a khz counter frequency to a timsource176 * multiplier, given the clocksource shift value177 */178static inline u32 clocksource_khz2mult(u32 khz, u32 shift_constant)179{180	return clocksource_freq2mult(khz, shift_constant, NSEC_PER_MSEC);181}182 183/**184 * clocksource_hz2mult - calculates mult from hz and shift185 * @hz:			Clocksource frequency in Hz186 * @shift_constant:	Clocksource shift factor187 *188 * Helper functions that converts a hz counter189 * frequency to a timsource multiplier, given the190 * clocksource shift value191 */192static inline u32 clocksource_hz2mult(u32 hz, u32 shift_constant)193{194	return clocksource_freq2mult(hz, shift_constant, NSEC_PER_SEC);195}196 197/**198 * clocksource_cyc2ns - converts clocksource cycles to nanoseconds199 * @cycles:	cycles200 * @mult:	cycle to nanosecond multiplier201 * @shift:	cycle to nanosecond divisor (power of two)202 *203 * Converts clocksource cycles to nanoseconds, using the given @mult and @shift.204 * The code is optimized for performance and is not intended to work205 * with absolute clocksource cycles (as those will easily overflow),206 * but is only intended to be used with relative (delta) clocksource cycles.207 *208 * XXX - This could use some mult_lxl_ll() asm optimization209 */210static inline s64 clocksource_cyc2ns(u64 cycles, u32 mult, u32 shift)211{212	return ((u64) cycles * mult) >> shift;213}214 215 216extern int clocksource_unregister(struct clocksource*) HWJS_SUSPENDS;217extern void clocksource_touch_watchdog(void);218extern void clocksource_change_rating(struct clocksource *cs, int rating) HWJS_SUSPENDS;219extern void clocksource_suspend(void);220extern void clocksource_resume(void);221extern struct clocksource * __init clocksource_default_clock(void);222extern void clocksource_mark_unstable(struct clocksource *cs);223extern void224clocksource_start_suspend_timing(struct clocksource *cs, u64 start_cycles) HWJS_SUSPENDS;225extern u64 clocksource_stop_suspend_timing(struct clocksource *cs, u64 now);226 227extern u64228clocks_calc_max_nsecs(u32 mult, u32 shift, u32 maxadj, u64 mask, u64 *max_cycles);229extern void230clocks_calc_mult_shift(u32 *mult, u32 *shift, u32 from, u32 to, u32 minsec);231 232/*233 * Don't call __clocksource_register_scale directly, use234 * clocksource_register_hz/khz235 */236extern int237__clocksource_register_scale(struct clocksource *cs, u32 scale, u32 freq) HWJS_SUSPENDS;238extern void239__clocksource_update_freq_scale(struct clocksource *cs, u32 scale, u32 freq);240 241/*242 * Don't call this unless you are a default clocksource243 * (AKA: jiffies) and absolutely have to.244 */245static inline int __clocksource_register(struct clocksource *cs)246{247	return __clocksource_register_scale(cs, 1, 0);248}249 250static inline int clocksource_register_hz(struct clocksource *cs, u32 hz)251{252	return __clocksource_register_scale(cs, 1, hz);253}254 255static inline int clocksource_register_khz(struct clocksource *cs, u32 khz)256{257	return __clocksource_register_scale(cs, 1000, khz);258}259 260static inline void __clocksource_update_freq_hz(struct clocksource *cs, u32 hz)261{262	__clocksource_update_freq_scale(cs, 1, hz);263}264 265static inline void __clocksource_update_freq_khz(struct clocksource *cs, u32 khz)266{267	__clocksource_update_freq_scale(cs, 1000, khz);268}269 270#ifdef CONFIG_ARCH_CLOCKSOURCE_INIT271extern void clocksource_arch_init(struct clocksource *cs);272#else273static inline void clocksource_arch_init(struct clocksource *cs) { }274#endif275 276extern int timekeeping_notify(struct clocksource *clock);277 278extern u64 clocksource_mmio_readl_up(struct clocksource *);279extern u64 clocksource_mmio_readl_down(struct clocksource *);280extern u64 clocksource_mmio_readw_up(struct clocksource *);281extern u64 clocksource_mmio_readw_down(struct clocksource *);282 283extern int clocksource_mmio_init(void __iomem *, const char *,284	unsigned long, int, unsigned, u64 (*)(struct clocksource *));285 286extern int clocksource_i8253_init(void);287 288#define TIMER_OF_DECLARE(name, compat, fn) \289	OF_DECLARE_1_RET(timer, name, compat, fn)290 291#ifdef CONFIG_TIMER_PROBE292extern void timer_probe(void);293#else294static inline void timer_probe(void) {}295#endif296 297#define TIMER_ACPI_DECLARE(name, table_id, fn)		\298	ACPI_DECLARE_PROBE_ENTRY(timer, name, table_id, 0, NULL, 0, fn)299 300static inline unsigned int clocksource_get_max_watchdog_retry(void)301{302	/*303	 * When system is in the boot phase or under heavy workload, there304	 * can be random big latencies during the clocksource/watchdog305	 * read, so allow retries to filter the noise latency. As the306	 * latency's frequency and maximum value goes up with the number of307	 * CPUs, scale the number of retries with the number of online308	 * CPUs.309	 */310	return (ilog2(num_online_cpus()) / 2) + 1;311}312 313void clocksource_verify_percpu(struct clocksource *cs);314 315/**316 * struct clocksource_base - hardware abstraction for clock on which a clocksource317 *			is based318 * @id:			Defaults to CSID_GENERIC. The id value is used for conversion319 *			functions which require that the current clocksource is based320 *			on a clocksource_base with a particular ID in certain snapshot321 *			functions to allow callers to validate the clocksource from322 *			which the snapshot was taken.323 * @freq_khz:		Nominal frequency of the base clock in kHz324 * @offset:		Offset between the base clock and the clocksource325 * @numerator:		Numerator of the clock ratio between base clock and the clocksource326 * @denominator:	Denominator of the clock ratio between base clock and the clocksource327 */328struct clocksource_base {329	enum clocksource_ids	id;330	u32			freq_khz;331	u64			offset;332	u32			numerator;333	u32			denominator;334};335 336#endif /* _LINUX_CLOCKSOURCE_H */337