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