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