xref: /linux-6.15/include/linux/clockchips.h (revision 9eed56e8)
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,
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,
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 /*
68  * x86(64) specific (mis)features:
69  *
70  * - Clockevent source stops in C3 State and needs broadcast support.
71  * - Local APIC timer is used as a dummy device.
72  */
73 # define CLOCK_EVT_FEAT_C3STOP		0x000008
74 # define CLOCK_EVT_FEAT_DUMMY		0x000010
75 
76 /*
77  * Core shall set the interrupt affinity dynamically in broadcast mode
78  */
79 # define CLOCK_EVT_FEAT_DYNIRQ		0x000020
80 # define CLOCK_EVT_FEAT_PERCPU		0x000040
81 
82 /*
83  * Clockevent device is based on a hrtimer for broadcast
84  */
85 # define CLOCK_EVT_FEAT_HRTIMER		0x000080
86 
87 /**
88  * struct clock_event_device - clock event device descriptor
89  * @event_handler:	Assigned by the framework to be called by the low
90  *			level handler of the event source
91  * @set_next_event:	set next event function using a clocksource delta
92  * @set_next_ktime:	set next event function using a direct ktime value
93  * @next_event:		local storage for the next event in oneshot mode
94  * @max_delta_ns:	maximum delta value in ns
95  * @min_delta_ns:	minimum delta value in ns
96  * @mult:		nanosecond to cycles multiplier
97  * @shift:		nanoseconds to cycles divisor (power of two)
98  * @mode:		operating mode, relevant only to ->set_mode(), OBSOLETE
99  * @state:		current state of the device, assigned by the core code
100  * @features:		features
101  * @retries:		number of forced programming retries
102  * @set_mode:		legacy set mode function, only for modes <= CLOCK_EVT_MODE_RESUME.
103  * @set_state_periodic:	switch state to periodic, if !set_mode
104  * @set_state_oneshot:	switch state to oneshot, if !set_mode
105  * @set_state_shutdown:	switch state to shutdown, if !set_mode
106  * @tick_resume:	resume clkevt device, if !set_mode
107  * @broadcast:		function to broadcast events
108  * @min_delta_ticks:	minimum delta value in ticks stored for reconfiguration
109  * @max_delta_ticks:	maximum delta value in ticks stored for reconfiguration
110  * @name:		ptr to clock event name
111  * @rating:		variable to rate clock event devices
112  * @irq:		IRQ number (only for non CPU local devices)
113  * @bound_on:		Bound on CPU
114  * @cpumask:		cpumask to indicate for which CPUs this device works
115  * @list:		list head for the management code
116  * @owner:		module reference
117  */
118 struct clock_event_device {
119 	void			(*event_handler)(struct clock_event_device *);
120 	int			(*set_next_event)(unsigned long evt, struct clock_event_device *);
121 	int			(*set_next_ktime)(ktime_t expires, struct clock_event_device *);
122 	ktime_t			next_event;
123 	u64			max_delta_ns;
124 	u64			min_delta_ns;
125 	u32			mult;
126 	u32			shift;
127 	enum clock_event_mode	mode;
128 	enum clock_event_state	state;
129 	unsigned int		features;
130 	unsigned long		retries;
131 
132 	/*
133 	 * State transition callback(s): Only one of the two groups should be
134 	 * defined:
135 	 * - set_mode(), only for modes <= CLOCK_EVT_MODE_RESUME.
136 	 * - set_state_{shutdown|periodic|oneshot}(), tick_resume().
137 	 */
138 	void			(*set_mode)(enum clock_event_mode mode, struct clock_event_device *);
139 	int			(*set_state_periodic)(struct clock_event_device *);
140 	int			(*set_state_oneshot)(struct clock_event_device *);
141 	int			(*set_state_shutdown)(struct clock_event_device *);
142 	int			(*tick_resume)(struct clock_event_device *);
143 
144 	void			(*broadcast)(const struct cpumask *mask);
145 	void			(*suspend)(struct clock_event_device *);
146 	void			(*resume)(struct clock_event_device *);
147 	unsigned long		min_delta_ticks;
148 	unsigned long		max_delta_ticks;
149 
150 	const char		*name;
151 	int			rating;
152 	int			irq;
153 	int			bound_on;
154 	const struct cpumask	*cpumask;
155 	struct list_head	list;
156 	struct module		*owner;
157 } ____cacheline_aligned;
158 
159 /*
160  * Calculate a multiplication factor for scaled math, which is used to convert
161  * nanoseconds based values to clock ticks:
162  *
163  * clock_ticks = (nanoseconds * factor) >> shift.
164  *
165  * div_sc is the rearranged equation to calculate a factor from a given clock
166  * ticks / nanoseconds ratio:
167  *
168  * factor = (clock_ticks << shift) / nanoseconds
169  */
170 static inline unsigned long
171 div_sc(unsigned long ticks, unsigned long nsec, int shift)
172 {
173 	u64 tmp = ((u64)ticks) << shift;
174 
175 	do_div(tmp, nsec);
176 
177 	return (unsigned long) tmp;
178 }
179 
180 /* Clock event layer functions */
181 extern u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt);
182 extern void clockevents_register_device(struct clock_event_device *dev);
183 extern int clockevents_unbind_device(struct clock_event_device *ced, int cpu);
184 
185 extern void clockevents_config(struct clock_event_device *dev, u32 freq);
186 extern void clockevents_config_and_register(struct clock_event_device *dev,
187 					    u32 freq, unsigned long min_delta,
188 					    unsigned long max_delta);
189 
190 extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
191 
192 static inline void
193 clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 minsec)
194 {
195 	return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC, freq, minsec);
196 }
197 
198 extern void clockevents_suspend(void);
199 extern void clockevents_resume(void);
200 
201 # ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
202 #  ifdef CONFIG_ARCH_HAS_TICK_BROADCAST
203 extern void tick_broadcast(const struct cpumask *mask);
204 #  else
205 #   define tick_broadcast	NULL
206 #  endif
207 extern int tick_receive_broadcast(void);
208 # endif
209 
210 # if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT)
211 extern void tick_setup_hrtimer_broadcast(void);
212 extern int tick_check_broadcast_expired(void);
213 # else
214 static inline int tick_check_broadcast_expired(void) { return 0; }
215 static inline void tick_setup_hrtimer_broadcast(void) { }
216 # endif
217 
218 extern int clockevents_notify(unsigned long reason, void *arg);
219 
220 #else /* !CONFIG_GENERIC_CLOCKEVENTS: */
221 
222 static inline void clockevents_suspend(void) { }
223 static inline void clockevents_resume(void) { }
224 static inline int clockevents_notify(unsigned long reason, void *arg) { return 0; }
225 static inline int tick_check_broadcast_expired(void) { return 0; }
226 static inline void tick_setup_hrtimer_broadcast(void) { }
227 
228 #endif /* !CONFIG_GENERIC_CLOCKEVENTS */
229 
230 #endif /* _LINUX_CLOCKCHIPS_H */
231