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