Commit | Line | Data |
---|---|---|
b2441318 | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
d316c57f TG |
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 | ||
9f083b74 | 12 | #ifdef CONFIG_GENERIC_CLOCKEVENTS |
d316c57f | 13 | |
9eed56e8 IM |
14 | # include <linux/clocksource.h> |
15 | # include <linux/cpumask.h> | |
16 | # include <linux/ktime.h> | |
17 | # include <linux/notifier.h> | |
d316c57f TG |
18 | |
19 | struct clock_event_device; | |
ccf33d68 | 20 | struct module; |
d316c57f | 21 | |
77e32c89 VK |
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. | |
8fff52fd VK |
32 | * ONESHOT_STOPPED: Device was programmed in ONESHOT mode and is temporarily |
33 | * stopped. | |
77e32c89 VK |
34 | */ |
35 | enum clock_event_state { | |
9eed56e8 | 36 | CLOCK_EVT_STATE_DETACHED, |
77e32c89 VK |
37 | CLOCK_EVT_STATE_SHUTDOWN, |
38 | CLOCK_EVT_STATE_PERIODIC, | |
39 | CLOCK_EVT_STATE_ONESHOT, | |
8fff52fd | 40 | CLOCK_EVT_STATE_ONESHOT_STOPPED, |
d316c57f TG |
41 | }; |
42 | ||
d316c57f TG |
43 | /* |
44 | * Clock event features | |
45 | */ | |
9eed56e8 IM |
46 | # define CLOCK_EVT_FEAT_PERIODIC 0x000001 |
47 | # define CLOCK_EVT_FEAT_ONESHOT 0x000002 | |
48 | # define CLOCK_EVT_FEAT_KTIME 0x000004 | |
49 | ||
d316c57f | 50 | /* |
9eed56e8 | 51 | * x86(64) specific (mis)features: |
d316c57f TG |
52 | * |
53 | * - Clockevent source stops in C3 State and needs broadcast support. | |
54 | * - Local APIC timer is used as a dummy device. | |
55 | */ | |
9eed56e8 IM |
56 | # define CLOCK_EVT_FEAT_C3STOP 0x000008 |
57 | # define CLOCK_EVT_FEAT_DUMMY 0x000010 | |
d316c57f | 58 | |
d2348fb6 DL |
59 | /* |
60 | * Core shall set the interrupt affinity dynamically in broadcast mode | |
61 | */ | |
9eed56e8 IM |
62 | # define CLOCK_EVT_FEAT_DYNIRQ 0x000020 |
63 | # define CLOCK_EVT_FEAT_PERCPU 0x000040 | |
d2348fb6 | 64 | |
5d1638ac PM |
65 | /* |
66 | * Clockevent device is based on a hrtimer for broadcast | |
67 | */ | |
9eed56e8 | 68 | # define CLOCK_EVT_FEAT_HRTIMER 0x000080 |
5d1638ac | 69 | |
d316c57f TG |
70 | /** |
71 | * struct clock_event_device - clock event device descriptor | |
847b2f42 TG |
72 | * @event_handler: Assigned by the framework to be called by the low |
73 | * level handler of the event source | |
65516f8a MS |
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 | |
847b2f42 | 76 | * @next_event: local storage for the next event in oneshot mode |
d316c57f TG |
77 | * @max_delta_ns: maximum delta value in ns |
78 | * @min_delta_ns: minimum delta value in ns | |
79 | * @mult: nanosecond to cycles multiplier | |
80 | * @shift: nanoseconds to cycles divisor (power of two) | |
be3ef76e | 81 | * @state_use_accessors:current state of the device, assigned by the core code |
847b2f42 TG |
82 | * @features: features |
83 | * @retries: number of forced programming retries | |
eef7635a VK |
84 | * @set_state_periodic: switch state to periodic |
85 | * @set_state_oneshot: switch state to oneshot | |
86 | * @set_state_oneshot_stopped: switch state to oneshot_stopped | |
87 | * @set_state_shutdown: switch state to shutdown | |
88 | * @tick_resume: resume clkevt device | |
847b2f42 | 89 | * @broadcast: function to broadcast events |
57f0fcbe TG |
90 | * @min_delta_ticks: minimum delta value in ticks stored for reconfiguration |
91 | * @max_delta_ticks: maximum delta value in ticks stored for reconfiguration | |
847b2f42 | 92 | * @name: ptr to clock event name |
d316c57f | 93 | * @rating: variable to rate clock event devices |
ce0be127 | 94 | * @irq: IRQ number (only for non CPU local devices) |
5d1638ac | 95 | * @bound_on: Bound on CPU |
ce0be127 | 96 | * @cpumask: cpumask to indicate for which CPUs this device works |
d316c57f | 97 | * @list: list head for the management code |
ccf33d68 | 98 | * @owner: module reference |
d316c57f TG |
99 | */ |
100 | struct clock_event_device { | |
847b2f42 | 101 | void (*event_handler)(struct clock_event_device *); |
9eed56e8 IM |
102 | int (*set_next_event)(unsigned long evt, struct clock_event_device *); |
103 | int (*set_next_ktime)(ktime_t expires, struct clock_event_device *); | |
847b2f42 | 104 | ktime_t next_event; |
97813f2f JH |
105 | u64 max_delta_ns; |
106 | u64 min_delta_ns; | |
23af368e TG |
107 | u32 mult; |
108 | u32 shift; | |
be3ef76e | 109 | enum clock_event_state state_use_accessors; |
847b2f42 TG |
110 | unsigned int features; |
111 | unsigned long retries; | |
112 | ||
77e32c89 VK |
113 | int (*set_state_periodic)(struct clock_event_device *); |
114 | int (*set_state_oneshot)(struct clock_event_device *); | |
8fff52fd | 115 | int (*set_state_oneshot_stopped)(struct clock_event_device *); |
77e32c89 | 116 | int (*set_state_shutdown)(struct clock_event_device *); |
554ef387 | 117 | int (*tick_resume)(struct clock_event_device *); |
bd624d75 VK |
118 | |
119 | void (*broadcast)(const struct cpumask *mask); | |
adc78e6b RW |
120 | void (*suspend)(struct clock_event_device *); |
121 | void (*resume)(struct clock_event_device *); | |
57f0fcbe TG |
122 | unsigned long min_delta_ticks; |
123 | unsigned long max_delta_ticks; | |
124 | ||
847b2f42 | 125 | const char *name; |
d316c57f TG |
126 | int rating; |
127 | int irq; | |
5d1638ac | 128 | int bound_on; |
320ab2b0 | 129 | const struct cpumask *cpumask; |
d316c57f | 130 | struct list_head list; |
ccf33d68 | 131 | struct module *owner; |
847b2f42 | 132 | } ____cacheline_aligned; |
d316c57f | 133 | |
3434d23b VK |
134 | /* Helpers to verify state of a clockevent device */ |
135 | static inline bool clockevent_state_detached(struct clock_event_device *dev) | |
136 | { | |
be3ef76e | 137 | return dev->state_use_accessors == CLOCK_EVT_STATE_DETACHED; |
3434d23b VK |
138 | } |
139 | ||
140 | static inline bool clockevent_state_shutdown(struct clock_event_device *dev) | |
141 | { | |
be3ef76e | 142 | return dev->state_use_accessors == CLOCK_EVT_STATE_SHUTDOWN; |
3434d23b VK |
143 | } |
144 | ||
145 | static inline bool clockevent_state_periodic(struct clock_event_device *dev) | |
146 | { | |
be3ef76e | 147 | return dev->state_use_accessors == CLOCK_EVT_STATE_PERIODIC; |
3434d23b VK |
148 | } |
149 | ||
150 | static inline bool clockevent_state_oneshot(struct clock_event_device *dev) | |
151 | { | |
be3ef76e | 152 | return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT; |
3434d23b VK |
153 | } |
154 | ||
155 | static inline bool clockevent_state_oneshot_stopped(struct clock_event_device *dev) | |
156 | { | |
be3ef76e | 157 | return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT_STOPPED; |
3434d23b VK |
158 | } |
159 | ||
d316c57f TG |
160 | /* |
161 | * Calculate a multiplication factor for scaled math, which is used to convert | |
162 | * nanoseconds based values to clock ticks: | |
163 | * | |
164 | * clock_ticks = (nanoseconds * factor) >> shift. | |
165 | * | |
166 | * div_sc is the rearranged equation to calculate a factor from a given clock | |
167 | * ticks / nanoseconds ratio: | |
168 | * | |
169 | * factor = (clock_ticks << shift) / nanoseconds | |
170 | */ | |
9eed56e8 IM |
171 | static inline unsigned long |
172 | div_sc(unsigned long ticks, unsigned long nsec, int shift) | |
d316c57f | 173 | { |
9eed56e8 | 174 | u64 tmp = ((u64)ticks) << shift; |
d316c57f TG |
175 | |
176 | do_div(tmp, nsec); | |
9eed56e8 | 177 | |
d316c57f TG |
178 | return (unsigned long) tmp; |
179 | } | |
180 | ||
181 | /* Clock event layer functions */ | |
9eed56e8 | 182 | extern u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt); |
d316c57f | 183 | extern void clockevents_register_device(struct clock_event_device *dev); |
03e13cf5 | 184 | extern int clockevents_unbind_device(struct clock_event_device *ced, int cpu); |
d316c57f | 185 | |
57f0fcbe TG |
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 | ||
80b816b7 TG |
190 | extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq); |
191 | ||
7d2f944a | 192 | static inline void |
dd42ac8f | 193 | clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 maxsec) |
7d2f944a | 194 | { |
dd42ac8f | 195 | return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC, freq, maxsec); |
7d2f944a TG |
196 | } |
197 | ||
adc78e6b RW |
198 | extern void clockevents_suspend(void); |
199 | extern void clockevents_resume(void); | |
200 | ||
9eed56e8 IM |
201 | # ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST |
202 | # ifdef CONFIG_ARCH_HAS_TICK_BROADCAST | |
12ad1000 | 203 | extern void tick_broadcast(const struct cpumask *mask); |
9eed56e8 IM |
204 | # else |
205 | # define tick_broadcast NULL | |
206 | # endif | |
12572dbb | 207 | extern int tick_receive_broadcast(void); |
9eed56e8 | 208 | # endif |
12572dbb | 209 | |
9eed56e8 | 210 | # if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT) |
5d1638ac | 211 | extern void tick_setup_hrtimer_broadcast(void); |
eaa907c5 | 212 | extern int tick_check_broadcast_expired(void); |
9eed56e8 | 213 | # else |
eaa907c5 | 214 | static inline int tick_check_broadcast_expired(void) { return 0; } |
9eed56e8 IM |
215 | static inline void tick_setup_hrtimer_broadcast(void) { } |
216 | # endif | |
eaa907c5 | 217 | |
9eed56e8 | 218 | #else /* !CONFIG_GENERIC_CLOCKEVENTS: */ |
adc78e6b | 219 | |
9eed56e8 IM |
220 | static inline void clockevents_suspend(void) { } |
221 | static inline void clockevents_resume(void) { } | |
19919226 | 222 | static inline int tick_check_broadcast_expired(void) { return 0; } |
9eed56e8 | 223 | static inline void tick_setup_hrtimer_broadcast(void) { } |
d316c57f | 224 | |
9eed56e8 | 225 | #endif /* !CONFIG_GENERIC_CLOCKEVENTS */ |
d316c57f | 226 | |
9eed56e8 | 227 | #endif /* _LINUX_CLOCKCHIPS_H */ |