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 #ifdef CONFIG_GENERIC_CLOCKEVENTS
12 
13 # include <linux/clocksource.h>
14 # include <linux/cpumask.h>
15 # include <linux/ktime.h>
16 # include <linux/notifier.h>
17 
18 struct clock_event_device;
19 struct module;
20 
21 /*
22  * Possible states of a clock event device.
23  *
24  * DETACHED:	Device is not used by clockevents core. Initial state or can be
25  *		reached from SHUTDOWN.
26  * SHUTDOWN:	Device is powered-off. Can be reached from PERIODIC or ONESHOT.
27  * PERIODIC:	Device is programmed to generate events periodically. Can be
28  *		reached from DETACHED or SHUTDOWN.
29  * ONESHOT:	Device is programmed to generate event only once. Can be reached
30  *		from DETACHED or SHUTDOWN.
31  * ONESHOT_STOPPED: Device was programmed in ONESHOT mode and is temporarily
32  *		    stopped.
33  */
34 enum clock_event_state {
35 	CLOCK_EVT_STATE_DETACHED,
36 	CLOCK_EVT_STATE_SHUTDOWN,
37 	CLOCK_EVT_STATE_PERIODIC,
38 	CLOCK_EVT_STATE_ONESHOT,
39 	CLOCK_EVT_STATE_ONESHOT_STOPPED,
40 };
41 
42 /*
43  * Clock event features
44  */
45 # define CLOCK_EVT_FEAT_PERIODIC	0x000001
46 # define CLOCK_EVT_FEAT_ONESHOT		0x000002
47 # define CLOCK_EVT_FEAT_KTIME		0x000004
48 
49 /*
50  * x86(64) specific (mis)features:
51  *
52  * - Clockevent source stops in C3 State and needs broadcast support.
53  * - Local APIC timer is used as a dummy device.
54  */
55 # define CLOCK_EVT_FEAT_C3STOP		0x000008
56 # define CLOCK_EVT_FEAT_DUMMY		0x000010
57 
58 /*
59  * Core shall set the interrupt affinity dynamically in broadcast mode
60  */
61 # define CLOCK_EVT_FEAT_DYNIRQ		0x000020
62 # define CLOCK_EVT_FEAT_PERCPU		0x000040
63 
64 /*
65  * Clockevent device is based on a hrtimer for broadcast
66  */
67 # define CLOCK_EVT_FEAT_HRTIMER		0x000080
68 
69 /**
70  * struct clock_event_device - clock event device descriptor
71  * @event_handler:	Assigned by the framework to be called by the low
72  *			level handler of the event source
73  * @set_next_event:	set next event function using a clocksource delta
74  * @set_next_ktime:	set next event function using a direct ktime value
75  * @next_event:		local storage for the next event in oneshot mode
76  * @max_delta_ns:	maximum delta value in ns
77  * @min_delta_ns:	minimum delta value in ns
78  * @mult:		nanosecond to cycles multiplier
79  * @shift:		nanoseconds to cycles divisor (power of two)
80  * @state_use_accessors:current state of the device, assigned by the core code
81  * @features:		features
82  * @retries:		number of forced programming retries
83  * @set_state_periodic:	switch state to periodic
84  * @set_state_oneshot:	switch state to oneshot
85  * @set_state_oneshot_stopped: switch state to oneshot_stopped
86  * @set_state_shutdown:	switch state to shutdown
87  * @tick_resume:	resume clkevt device
88  * @broadcast:		function to broadcast events
89  * @min_delta_ticks:	minimum delta value in ticks stored for reconfiguration
90  * @max_delta_ticks:	maximum delta value in ticks stored for reconfiguration
91  * @name:		ptr to clock event name
92  * @rating:		variable to rate clock event devices
93  * @irq:		IRQ number (only for non CPU local devices)
94  * @bound_on:		Bound on CPU
95  * @cpumask:		cpumask to indicate for which CPUs this device works
96  * @list:		list head for the management code
97  * @owner:		module reference
98  */
99 struct clock_event_device {
100 	void			(*event_handler)(struct clock_event_device *);
101 	int			(*set_next_event)(unsigned long evt, struct clock_event_device *);
102 	int			(*set_next_ktime)(ktime_t expires, struct clock_event_device *);
103 	ktime_t			next_event;
104 	u64			max_delta_ns;
105 	u64			min_delta_ns;
106 	u32			mult;
107 	u32			shift;
108 	enum clock_event_state	state_use_accessors;
109 	unsigned int		features;
110 	unsigned long		retries;
111 
112 	int			(*set_state_periodic)(struct clock_event_device *);
113 	int			(*set_state_oneshot)(struct clock_event_device *);
114 	int			(*set_state_oneshot_stopped)(struct clock_event_device *);
115 	int			(*set_state_shutdown)(struct clock_event_device *);
116 	int			(*tick_resume)(struct clock_event_device *);
117 
118 	void			(*broadcast)(const struct cpumask *mask);
119 	void			(*suspend)(struct clock_event_device *);
120 	void			(*resume)(struct clock_event_device *);
121 	unsigned long		min_delta_ticks;
122 	unsigned long		max_delta_ticks;
123 
124 	const char		*name;
125 	int			rating;
126 	int			irq;
127 	int			bound_on;
128 	const struct cpumask	*cpumask;
129 	struct list_head	list;
130 	struct module		*owner;
131 } ____cacheline_aligned;
132 
133 /* Helpers to verify state of a clockevent device */
clockevent_state_detached(struct clock_event_device * dev)134 static inline bool clockevent_state_detached(struct clock_event_device *dev)
135 {
136 	return dev->state_use_accessors == CLOCK_EVT_STATE_DETACHED;
137 }
138 
clockevent_state_shutdown(struct clock_event_device * dev)139 static inline bool clockevent_state_shutdown(struct clock_event_device *dev)
140 {
141 	return dev->state_use_accessors == CLOCK_EVT_STATE_SHUTDOWN;
142 }
143 
clockevent_state_periodic(struct clock_event_device * dev)144 static inline bool clockevent_state_periodic(struct clock_event_device *dev)
145 {
146 	return dev->state_use_accessors == CLOCK_EVT_STATE_PERIODIC;
147 }
148 
clockevent_state_oneshot(struct clock_event_device * dev)149 static inline bool clockevent_state_oneshot(struct clock_event_device *dev)
150 {
151 	return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT;
152 }
153 
clockevent_state_oneshot_stopped(struct clock_event_device * dev)154 static inline bool clockevent_state_oneshot_stopped(struct clock_event_device *dev)
155 {
156 	return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT_STOPPED;
157 }
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
div_sc(unsigned long ticks,unsigned long nsec,int shift)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
clockevents_calc_mult_shift(struct clock_event_device * ce,u32 freq,u32 minsec)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
tick_check_broadcast_expired(void)214 static inline int tick_check_broadcast_expired(void) { return 0; }
tick_setup_hrtimer_broadcast(void)215 static inline void tick_setup_hrtimer_broadcast(void) { }
216 # endif
217 
218 #else /* !CONFIG_GENERIC_CLOCKEVENTS: */
219 
clockevents_suspend(void)220 static inline void clockevents_suspend(void) { }
clockevents_resume(void)221 static inline void clockevents_resume(void) { }
tick_check_broadcast_expired(void)222 static inline int tick_check_broadcast_expired(void) { return 0; }
tick_setup_hrtimer_broadcast(void)223 static inline void tick_setup_hrtimer_broadcast(void) { }
224 
225 #endif /* !CONFIG_GENERIC_CLOCKEVENTS */
226 
227 #endif /* _LINUX_CLOCKCHIPS_H */
228