162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*  linux/include/linux/clockchips.h
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci *  This file contains the structure definitions for clockchips.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci *  If you are not a clockchip, or the time of day code, you should
762306a36Sopenharmony_ci *  not be including this file!
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci#ifndef _LINUX_CLOCKCHIPS_H
1062306a36Sopenharmony_ci#define _LINUX_CLOCKCHIPS_H
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#ifdef CONFIG_GENERIC_CLOCKEVENTS
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci# include <linux/clocksource.h>
1562306a36Sopenharmony_ci# include <linux/cpumask.h>
1662306a36Sopenharmony_ci# include <linux/ktime.h>
1762306a36Sopenharmony_ci# include <linux/notifier.h>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistruct clock_event_device;
2062306a36Sopenharmony_cistruct module;
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci/*
2362306a36Sopenharmony_ci * Possible states of a clock event device.
2462306a36Sopenharmony_ci *
2562306a36Sopenharmony_ci * DETACHED:	Device is not used by clockevents core. Initial state or can be
2662306a36Sopenharmony_ci *		reached from SHUTDOWN.
2762306a36Sopenharmony_ci * SHUTDOWN:	Device is powered-off. Can be reached from PERIODIC or ONESHOT.
2862306a36Sopenharmony_ci * PERIODIC:	Device is programmed to generate events periodically. Can be
2962306a36Sopenharmony_ci *		reached from DETACHED or SHUTDOWN.
3062306a36Sopenharmony_ci * ONESHOT:	Device is programmed to generate event only once. Can be reached
3162306a36Sopenharmony_ci *		from DETACHED or SHUTDOWN.
3262306a36Sopenharmony_ci * ONESHOT_STOPPED: Device was programmed in ONESHOT mode and is temporarily
3362306a36Sopenharmony_ci *		    stopped.
3462306a36Sopenharmony_ci */
3562306a36Sopenharmony_cienum clock_event_state {
3662306a36Sopenharmony_ci	CLOCK_EVT_STATE_DETACHED,
3762306a36Sopenharmony_ci	CLOCK_EVT_STATE_SHUTDOWN,
3862306a36Sopenharmony_ci	CLOCK_EVT_STATE_PERIODIC,
3962306a36Sopenharmony_ci	CLOCK_EVT_STATE_ONESHOT,
4062306a36Sopenharmony_ci	CLOCK_EVT_STATE_ONESHOT_STOPPED,
4162306a36Sopenharmony_ci};
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/*
4462306a36Sopenharmony_ci * Clock event features
4562306a36Sopenharmony_ci */
4662306a36Sopenharmony_ci# define CLOCK_EVT_FEAT_PERIODIC	0x000001
4762306a36Sopenharmony_ci# define CLOCK_EVT_FEAT_ONESHOT		0x000002
4862306a36Sopenharmony_ci# define CLOCK_EVT_FEAT_KTIME		0x000004
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci/*
5162306a36Sopenharmony_ci * x86(64) specific (mis)features:
5262306a36Sopenharmony_ci *
5362306a36Sopenharmony_ci * - Clockevent source stops in C3 State and needs broadcast support.
5462306a36Sopenharmony_ci * - Local APIC timer is used as a dummy device.
5562306a36Sopenharmony_ci */
5662306a36Sopenharmony_ci# define CLOCK_EVT_FEAT_C3STOP		0x000008
5762306a36Sopenharmony_ci# define CLOCK_EVT_FEAT_DUMMY		0x000010
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ci/*
6062306a36Sopenharmony_ci * Core shall set the interrupt affinity dynamically in broadcast mode
6162306a36Sopenharmony_ci */
6262306a36Sopenharmony_ci# define CLOCK_EVT_FEAT_DYNIRQ		0x000020
6362306a36Sopenharmony_ci# define CLOCK_EVT_FEAT_PERCPU		0x000040
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci/*
6662306a36Sopenharmony_ci * Clockevent device is based on a hrtimer for broadcast
6762306a36Sopenharmony_ci */
6862306a36Sopenharmony_ci# define CLOCK_EVT_FEAT_HRTIMER		0x000080
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci/**
7162306a36Sopenharmony_ci * struct clock_event_device - clock event device descriptor
7262306a36Sopenharmony_ci * @event_handler:	Assigned by the framework to be called by the low
7362306a36Sopenharmony_ci *			level handler of the event source
7462306a36Sopenharmony_ci * @set_next_event:	set next event function using a clocksource delta
7562306a36Sopenharmony_ci * @set_next_ktime:	set next event function using a direct ktime value
7662306a36Sopenharmony_ci * @next_event:		local storage for the next event in oneshot mode
7762306a36Sopenharmony_ci * @max_delta_ns:	maximum delta value in ns
7862306a36Sopenharmony_ci * @min_delta_ns:	minimum delta value in ns
7962306a36Sopenharmony_ci * @mult:		nanosecond to cycles multiplier
8062306a36Sopenharmony_ci * @shift:		nanoseconds to cycles divisor (power of two)
8162306a36Sopenharmony_ci * @state_use_accessors:current state of the device, assigned by the core code
8262306a36Sopenharmony_ci * @features:		features
8362306a36Sopenharmony_ci * @retries:		number of forced programming retries
8462306a36Sopenharmony_ci * @set_state_periodic:	switch state to periodic
8562306a36Sopenharmony_ci * @set_state_oneshot:	switch state to oneshot
8662306a36Sopenharmony_ci * @set_state_oneshot_stopped: switch state to oneshot_stopped
8762306a36Sopenharmony_ci * @set_state_shutdown:	switch state to shutdown
8862306a36Sopenharmony_ci * @tick_resume:	resume clkevt device
8962306a36Sopenharmony_ci * @broadcast:		function to broadcast events
9062306a36Sopenharmony_ci * @min_delta_ticks:	minimum delta value in ticks stored for reconfiguration
9162306a36Sopenharmony_ci * @max_delta_ticks:	maximum delta value in ticks stored for reconfiguration
9262306a36Sopenharmony_ci * @name:		ptr to clock event name
9362306a36Sopenharmony_ci * @rating:		variable to rate clock event devices
9462306a36Sopenharmony_ci * @irq:		IRQ number (only for non CPU local devices)
9562306a36Sopenharmony_ci * @bound_on:		Bound on CPU
9662306a36Sopenharmony_ci * @cpumask:		cpumask to indicate for which CPUs this device works
9762306a36Sopenharmony_ci * @list:		list head for the management code
9862306a36Sopenharmony_ci * @owner:		module reference
9962306a36Sopenharmony_ci */
10062306a36Sopenharmony_cistruct clock_event_device {
10162306a36Sopenharmony_ci	void			(*event_handler)(struct clock_event_device *);
10262306a36Sopenharmony_ci	int			(*set_next_event)(unsigned long evt, struct clock_event_device *);
10362306a36Sopenharmony_ci	int			(*set_next_ktime)(ktime_t expires, struct clock_event_device *);
10462306a36Sopenharmony_ci	ktime_t			next_event;
10562306a36Sopenharmony_ci	u64			max_delta_ns;
10662306a36Sopenharmony_ci	u64			min_delta_ns;
10762306a36Sopenharmony_ci	u32			mult;
10862306a36Sopenharmony_ci	u32			shift;
10962306a36Sopenharmony_ci	enum clock_event_state	state_use_accessors;
11062306a36Sopenharmony_ci	unsigned int		features;
11162306a36Sopenharmony_ci	unsigned long		retries;
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	int			(*set_state_periodic)(struct clock_event_device *);
11462306a36Sopenharmony_ci	int			(*set_state_oneshot)(struct clock_event_device *);
11562306a36Sopenharmony_ci	int			(*set_state_oneshot_stopped)(struct clock_event_device *);
11662306a36Sopenharmony_ci	int			(*set_state_shutdown)(struct clock_event_device *);
11762306a36Sopenharmony_ci	int			(*tick_resume)(struct clock_event_device *);
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	void			(*broadcast)(const struct cpumask *mask);
12062306a36Sopenharmony_ci	void			(*suspend)(struct clock_event_device *);
12162306a36Sopenharmony_ci	void			(*resume)(struct clock_event_device *);
12262306a36Sopenharmony_ci	unsigned long		min_delta_ticks;
12362306a36Sopenharmony_ci	unsigned long		max_delta_ticks;
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci	const char		*name;
12662306a36Sopenharmony_ci	int			rating;
12762306a36Sopenharmony_ci	int			irq;
12862306a36Sopenharmony_ci	int			bound_on;
12962306a36Sopenharmony_ci	const struct cpumask	*cpumask;
13062306a36Sopenharmony_ci	struct list_head	list;
13162306a36Sopenharmony_ci	struct module		*owner;
13262306a36Sopenharmony_ci} ____cacheline_aligned;
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci/* Helpers to verify state of a clockevent device */
13562306a36Sopenharmony_cistatic inline bool clockevent_state_detached(struct clock_event_device *dev)
13662306a36Sopenharmony_ci{
13762306a36Sopenharmony_ci	return dev->state_use_accessors == CLOCK_EVT_STATE_DETACHED;
13862306a36Sopenharmony_ci}
13962306a36Sopenharmony_ci
14062306a36Sopenharmony_cistatic inline bool clockevent_state_shutdown(struct clock_event_device *dev)
14162306a36Sopenharmony_ci{
14262306a36Sopenharmony_ci	return dev->state_use_accessors == CLOCK_EVT_STATE_SHUTDOWN;
14362306a36Sopenharmony_ci}
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_cistatic inline bool clockevent_state_periodic(struct clock_event_device *dev)
14662306a36Sopenharmony_ci{
14762306a36Sopenharmony_ci	return dev->state_use_accessors == CLOCK_EVT_STATE_PERIODIC;
14862306a36Sopenharmony_ci}
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_cistatic inline bool clockevent_state_oneshot(struct clock_event_device *dev)
15162306a36Sopenharmony_ci{
15262306a36Sopenharmony_ci	return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT;
15362306a36Sopenharmony_ci}
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_cistatic inline bool clockevent_state_oneshot_stopped(struct clock_event_device *dev)
15662306a36Sopenharmony_ci{
15762306a36Sopenharmony_ci	return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT_STOPPED;
15862306a36Sopenharmony_ci}
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci/*
16162306a36Sopenharmony_ci * Calculate a multiplication factor for scaled math, which is used to convert
16262306a36Sopenharmony_ci * nanoseconds based values to clock ticks:
16362306a36Sopenharmony_ci *
16462306a36Sopenharmony_ci * clock_ticks = (nanoseconds * factor) >> shift.
16562306a36Sopenharmony_ci *
16662306a36Sopenharmony_ci * div_sc is the rearranged equation to calculate a factor from a given clock
16762306a36Sopenharmony_ci * ticks / nanoseconds ratio:
16862306a36Sopenharmony_ci *
16962306a36Sopenharmony_ci * factor = (clock_ticks << shift) / nanoseconds
17062306a36Sopenharmony_ci */
17162306a36Sopenharmony_cistatic inline unsigned long
17262306a36Sopenharmony_cidiv_sc(unsigned long ticks, unsigned long nsec, int shift)
17362306a36Sopenharmony_ci{
17462306a36Sopenharmony_ci	u64 tmp = ((u64)ticks) << shift;
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci	do_div(tmp, nsec);
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	return (unsigned long) tmp;
17962306a36Sopenharmony_ci}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci/* Clock event layer functions */
18262306a36Sopenharmony_ciextern u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt);
18362306a36Sopenharmony_ciextern void clockevents_register_device(struct clock_event_device *dev);
18462306a36Sopenharmony_ciextern int clockevents_unbind_device(struct clock_event_device *ced, int cpu);
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ciextern void clockevents_config_and_register(struct clock_event_device *dev,
18762306a36Sopenharmony_ci					    u32 freq, unsigned long min_delta,
18862306a36Sopenharmony_ci					    unsigned long max_delta);
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ciextern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_cistatic inline void
19362306a36Sopenharmony_ciclockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 maxsec)
19462306a36Sopenharmony_ci{
19562306a36Sopenharmony_ci	return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC, freq, maxsec);
19662306a36Sopenharmony_ci}
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ciextern void clockevents_suspend(void);
19962306a36Sopenharmony_ciextern void clockevents_resume(void);
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci# ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
20262306a36Sopenharmony_ci#  ifdef CONFIG_ARCH_HAS_TICK_BROADCAST
20362306a36Sopenharmony_ciextern void tick_broadcast(const struct cpumask *mask);
20462306a36Sopenharmony_ci#  else
20562306a36Sopenharmony_ci#   define tick_broadcast	NULL
20662306a36Sopenharmony_ci#  endif
20762306a36Sopenharmony_ciextern int tick_receive_broadcast(void);
20862306a36Sopenharmony_ci# endif
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci# if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
21162306a36Sopenharmony_ciextern void tick_setup_hrtimer_broadcast(void);
21262306a36Sopenharmony_ciextern int tick_check_broadcast_expired(void);
21362306a36Sopenharmony_ci# else
21462306a36Sopenharmony_cistatic __always_inline int tick_check_broadcast_expired(void) { return 0; }
21562306a36Sopenharmony_cistatic inline void tick_setup_hrtimer_broadcast(void) { }
21662306a36Sopenharmony_ci# endif
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci#else /* !CONFIG_GENERIC_CLOCKEVENTS: */
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_cistatic inline void clockevents_suspend(void) { }
22162306a36Sopenharmony_cistatic inline void clockevents_resume(void) { }
22262306a36Sopenharmony_cistatic __always_inline int tick_check_broadcast_expired(void) { return 0; }
22362306a36Sopenharmony_cistatic inline void tick_setup_hrtimer_broadcast(void) { }
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci#endif /* !CONFIG_GENERIC_CLOCKEVENTS */
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci#endif /* _LINUX_CLOCKCHIPS_H */
228