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