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