xref: /linux-6.15/include/linux/clockchips.h (revision 89feddbf)
1 /*  linux/include/linux/clockchips.h
2  *
3  *  This file contains the structure definitions for clockchips.
4  *
5  *  If you are not a clockchip, or the time of day code, you should
6  *  not be including this file!
7  */
8 #ifndef _LINUX_CLOCKCHIPS_H
9 #define _LINUX_CLOCKCHIPS_H
10 
11 /* Clock event notification values */
12 enum clock_event_nofitiers {
13 	CLOCK_EVT_NOTIFY_ADD,
14 	CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
15 	CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
16 	CLOCK_EVT_NOTIFY_CPU_DYING,
17 	CLOCK_EVT_NOTIFY_CPU_DEAD,
18 };
19 
20 #ifdef CONFIG_GENERIC_CLOCKEVENTS
21 
22 # include <linux/clocksource.h>
23 # include <linux/cpumask.h>
24 # include <linux/ktime.h>
25 # include <linux/notifier.h>
26 
27 struct clock_event_device;
28 struct module;
29 
30 /* Clock event mode commands for legacy ->set_mode(): OBSOLETE */
31 enum clock_event_mode {
32 	CLOCK_EVT_MODE_UNUSED,
33 	CLOCK_EVT_MODE_SHUTDOWN,
34 	CLOCK_EVT_MODE_PERIODIC,
35 	CLOCK_EVT_MODE_ONESHOT,
36 	CLOCK_EVT_MODE_RESUME,
37 };
38 
39 /*
40  * Possible states of a clock event device.
41  *
42  * DETACHED:	Device is not used by clockevents core. Initial state or can be
43  *		reached from SHUTDOWN.
44  * SHUTDOWN:	Device is powered-off. Can be reached from PERIODIC or ONESHOT.
45  * PERIODIC:	Device is programmed to generate events periodically. Can be
46  *		reached from DETACHED or SHUTDOWN.
47  * ONESHOT:	Device is programmed to generate event only once. Can be reached
48  *		from DETACHED or SHUTDOWN.
49  */
50 enum clock_event_state {
51 	CLOCK_EVT_STATE_DETACHED,
52 	CLOCK_EVT_STATE_SHUTDOWN,
53 	CLOCK_EVT_STATE_PERIODIC,
54 	CLOCK_EVT_STATE_ONESHOT,
55 };
56 
57 /*
58  * Clock event features
59  */
60 # define CLOCK_EVT_FEAT_PERIODIC	0x000001
61 # define CLOCK_EVT_FEAT_ONESHOT		0x000002
62 # define CLOCK_EVT_FEAT_KTIME		0x000004
63 
64 /*
65  * x86(64) specific (mis)features:
66  *
67  * - Clockevent source stops in C3 State and needs broadcast support.
68  * - Local APIC timer is used as a dummy device.
69  */
70 # define CLOCK_EVT_FEAT_C3STOP		0x000008
71 # define CLOCK_EVT_FEAT_DUMMY		0x000010
72 
73 /*
74  * Core shall set the interrupt affinity dynamically in broadcast mode
75  */
76 # define CLOCK_EVT_FEAT_DYNIRQ		0x000020
77 # define CLOCK_EVT_FEAT_PERCPU		0x000040
78 
79 /*
80  * Clockevent device is based on a hrtimer for broadcast
81  */
82 # define CLOCK_EVT_FEAT_HRTIMER		0x000080
83 
84 /**
85  * struct clock_event_device - clock event device descriptor
86  * @event_handler:	Assigned by the framework to be called by the low
87  *			level handler of the event source
88  * @set_next_event:	set next event function using a clocksource delta
89  * @set_next_ktime:	set next event function using a direct ktime value
90  * @next_event:		local storage for the next event in oneshot mode
91  * @max_delta_ns:	maximum delta value in ns
92  * @min_delta_ns:	minimum delta value in ns
93  * @mult:		nanosecond to cycles multiplier
94  * @shift:		nanoseconds to cycles divisor (power of two)
95  * @mode:		operating mode, relevant only to ->set_mode(), OBSOLETE
96  * @state:		current state of the device, assigned by the core code
97  * @features:		features
98  * @retries:		number of forced programming retries
99  * @set_mode:		legacy set mode function, only for modes <= CLOCK_EVT_MODE_RESUME.
100  * @set_state_periodic:	switch state to periodic, if !set_mode
101  * @set_state_oneshot:	switch state to oneshot, if !set_mode
102  * @set_state_shutdown:	switch state to shutdown, if !set_mode
103  * @tick_resume:	resume clkevt device, if !set_mode
104  * @broadcast:		function to broadcast events
105  * @min_delta_ticks:	minimum delta value in ticks stored for reconfiguration
106  * @max_delta_ticks:	maximum delta value in ticks stored for reconfiguration
107  * @name:		ptr to clock event name
108  * @rating:		variable to rate clock event devices
109  * @irq:		IRQ number (only for non CPU local devices)
110  * @bound_on:		Bound on CPU
111  * @cpumask:		cpumask to indicate for which CPUs this device works
112  * @list:		list head for the management code
113  * @owner:		module reference
114  */
115 struct clock_event_device {
116 	void			(*event_handler)(struct clock_event_device *);
117 	int			(*set_next_event)(unsigned long evt, struct clock_event_device *);
118 	int			(*set_next_ktime)(ktime_t expires, struct clock_event_device *);
119 	ktime_t			next_event;
120 	u64			max_delta_ns;
121 	u64			min_delta_ns;
122 	u32			mult;
123 	u32			shift;
124 	enum clock_event_mode	mode;
125 	enum clock_event_state	state;
126 	unsigned int		features;
127 	unsigned long		retries;
128 
129 	/*
130 	 * State transition callback(s): Only one of the two groups should be
131 	 * defined:
132 	 * - set_mode(), only for modes <= CLOCK_EVT_MODE_RESUME.
133 	 * - set_state_{shutdown|periodic|oneshot}(), tick_resume().
134 	 */
135 	void			(*set_mode)(enum clock_event_mode mode, struct clock_event_device *);
136 	int			(*set_state_periodic)(struct clock_event_device *);
137 	int			(*set_state_oneshot)(struct clock_event_device *);
138 	int			(*set_state_shutdown)(struct clock_event_device *);
139 	int			(*tick_resume)(struct clock_event_device *);
140 
141 	void			(*broadcast)(const struct cpumask *mask);
142 	void			(*suspend)(struct clock_event_device *);
143 	void			(*resume)(struct clock_event_device *);
144 	unsigned long		min_delta_ticks;
145 	unsigned long		max_delta_ticks;
146 
147 	const char		*name;
148 	int			rating;
149 	int			irq;
150 	int			bound_on;
151 	const struct cpumask	*cpumask;
152 	struct list_head	list;
153 	struct module		*owner;
154 } ____cacheline_aligned;
155 
156 /*
157  * Calculate a multiplication factor for scaled math, which is used to convert
158  * nanoseconds based values to clock ticks:
159  *
160  * clock_ticks = (nanoseconds * factor) >> shift.
161  *
162  * div_sc is the rearranged equation to calculate a factor from a given clock
163  * ticks / nanoseconds ratio:
164  *
165  * factor = (clock_ticks << shift) / nanoseconds
166  */
167 static inline unsigned long
168 div_sc(unsigned long ticks, unsigned long nsec, int shift)
169 {
170 	u64 tmp = ((u64)ticks) << shift;
171 
172 	do_div(tmp, nsec);
173 
174 	return (unsigned long) tmp;
175 }
176 
177 /* Clock event layer functions */
178 extern u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt);
179 extern void clockevents_register_device(struct clock_event_device *dev);
180 extern int clockevents_unbind_device(struct clock_event_device *ced, int cpu);
181 
182 extern void clockevents_config(struct clock_event_device *dev, u32 freq);
183 extern void clockevents_config_and_register(struct clock_event_device *dev,
184 					    u32 freq, unsigned long min_delta,
185 					    unsigned long max_delta);
186 
187 extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
188 
189 static inline void
190 clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 minsec)
191 {
192 	return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC, freq, minsec);
193 }
194 
195 extern void clockevents_suspend(void);
196 extern void clockevents_resume(void);
197 
198 # ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
199 #  ifdef CONFIG_ARCH_HAS_TICK_BROADCAST
200 extern void tick_broadcast(const struct cpumask *mask);
201 #  else
202 #   define tick_broadcast	NULL
203 #  endif
204 extern int tick_receive_broadcast(void);
205 # endif
206 
207 # if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
208 extern void tick_setup_hrtimer_broadcast(void);
209 extern int tick_check_broadcast_expired(void);
210 # else
211 static inline int tick_check_broadcast_expired(void) { return 0; }
212 static inline void tick_setup_hrtimer_broadcast(void) { }
213 # endif
214 
215 extern int clockevents_notify(unsigned long reason, void *arg);
216 
217 #else /* !CONFIG_GENERIC_CLOCKEVENTS: */
218 
219 static inline void clockevents_suspend(void) { }
220 static inline void clockevents_resume(void) { }
221 static inline int clockevents_notify(unsigned long reason, void *arg) { return 0; }
222 static inline int tick_check_broadcast_expired(void) { return 0; }
223 static inline void tick_setup_hrtimer_broadcast(void) { }
224 
225 #endif /* !CONFIG_GENERIC_CLOCKEVENTS */
226 
227 #endif /* _LINUX_CLOCKCHIPS_H */
228