Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 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 | |
Thomas Gleixner | 9f083b7 | 2015-03-25 13:05:19 +0100 | [diff] [blame] | 11 | #ifdef CONFIG_GENERIC_CLOCKEVENTS |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 12 | |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 13 | # include <linux/clocksource.h> |
| 14 | # include <linux/cpumask.h> |
| 15 | # include <linux/ktime.h> |
| 16 | # include <linux/notifier.h> |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 17 | |
| 18 | struct clock_event_device; |
Thomas Gleixner | ccf33d6 | 2013-04-25 20:31:49 +0000 | [diff] [blame] | 19 | struct module; |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 20 | |
Viresh Kumar | 77e32c8 | 2015-02-27 17:21:33 +0530 | [diff] [blame] | 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. |
Viresh Kumar | 8fff52f | 2015-04-03 09:04:04 +0530 | [diff] [blame] | 31 | * ONESHOT_STOPPED: Device was programmed in ONESHOT mode and is temporarily |
| 32 | * stopped. |
Viresh Kumar | 77e32c8 | 2015-02-27 17:21:33 +0530 | [diff] [blame] | 33 | */ |
| 34 | enum clock_event_state { |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 35 | CLOCK_EVT_STATE_DETACHED, |
Viresh Kumar | 77e32c8 | 2015-02-27 17:21:33 +0530 | [diff] [blame] | 36 | CLOCK_EVT_STATE_SHUTDOWN, |
| 37 | CLOCK_EVT_STATE_PERIODIC, |
| 38 | CLOCK_EVT_STATE_ONESHOT, |
Viresh Kumar | 8fff52f | 2015-04-03 09:04:04 +0530 | [diff] [blame] | 39 | CLOCK_EVT_STATE_ONESHOT_STOPPED, |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 40 | }; |
| 41 | |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 42 | /* |
| 43 | * Clock event features |
| 44 | */ |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 45 | # define CLOCK_EVT_FEAT_PERIODIC 0x000001 |
| 46 | # define CLOCK_EVT_FEAT_ONESHOT 0x000002 |
| 47 | # define CLOCK_EVT_FEAT_KTIME 0x000004 |
| 48 | |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 49 | /* |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 50 | * x86(64) specific (mis)features: |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 51 | * |
| 52 | * - Clockevent source stops in C3 State and needs broadcast support. |
| 53 | * - Local APIC timer is used as a dummy device. |
| 54 | */ |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 55 | # define CLOCK_EVT_FEAT_C3STOP 0x000008 |
| 56 | # define CLOCK_EVT_FEAT_DUMMY 0x000010 |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 57 | |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 58 | /* |
| 59 | * Core shall set the interrupt affinity dynamically in broadcast mode |
| 60 | */ |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 61 | # define CLOCK_EVT_FEAT_DYNIRQ 0x000020 |
| 62 | # define CLOCK_EVT_FEAT_PERCPU 0x000040 |
Daniel Lezcano | d2348fb | 2013-03-02 11:10:11 +0100 | [diff] [blame] | 63 | |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 64 | /* |
| 65 | * Clockevent device is based on a hrtimer for broadcast |
| 66 | */ |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 67 | # define CLOCK_EVT_FEAT_HRTIMER 0x000080 |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 68 | |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 69 | /** |
| 70 | * struct clock_event_device - clock event device descriptor |
Thomas Gleixner | 847b2f4 | 2011-05-18 21:33:41 +0000 | [diff] [blame] | 71 | * @event_handler: Assigned by the framework to be called by the low |
| 72 | * level handler of the event source |
Martin Schwidefsky | 65516f8 | 2011-08-23 15:29:43 +0200 | [diff] [blame] | 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 |
Thomas Gleixner | 847b2f4 | 2011-05-18 21:33:41 +0000 | [diff] [blame] | 75 | * @next_event: local storage for the next event in oneshot mode |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 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) |
Thomas Gleixner | be3ef76 | 2015-06-02 14:30:11 +0200 | [diff] [blame] | 80 | * @state_use_accessors:current state of the device, assigned by the core code |
Thomas Gleixner | 847b2f4 | 2011-05-18 21:33:41 +0000 | [diff] [blame] | 81 | * @features: features |
| 82 | * @retries: number of forced programming retries |
Viresh Kumar | eef7635 | 2015-09-11 09:34:26 +0530 | [diff] [blame] | 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 |
Thomas Gleixner | 847b2f4 | 2011-05-18 21:33:41 +0000 | [diff] [blame] | 88 | * @broadcast: function to broadcast events |
Thomas Gleixner | 57f0fcb | 2011-05-18 21:33:41 +0000 | [diff] [blame] | 89 | * @min_delta_ticks: minimum delta value in ticks stored for reconfiguration |
| 90 | * @max_delta_ticks: maximum delta value in ticks stored for reconfiguration |
Thomas Gleixner | 847b2f4 | 2011-05-18 21:33:41 +0000 | [diff] [blame] | 91 | * @name: ptr to clock event name |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 92 | * @rating: variable to rate clock event devices |
Sergei Shtylyov | ce0be12 | 2007-05-08 00:31:55 -0700 | [diff] [blame] | 93 | * @irq: IRQ number (only for non CPU local devices) |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 94 | * @bound_on: Bound on CPU |
Sergei Shtylyov | ce0be12 | 2007-05-08 00:31:55 -0700 | [diff] [blame] | 95 | * @cpumask: cpumask to indicate for which CPUs this device works |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 96 | * @list: list head for the management code |
Thomas Gleixner | ccf33d6 | 2013-04-25 20:31:49 +0000 | [diff] [blame] | 97 | * @owner: module reference |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 98 | */ |
| 99 | struct clock_event_device { |
Thomas Gleixner | 847b2f4 | 2011-05-18 21:33:41 +0000 | [diff] [blame] | 100 | void (*event_handler)(struct clock_event_device *); |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 101 | int (*set_next_event)(unsigned long evt, struct clock_event_device *); |
| 102 | int (*set_next_ktime)(ktime_t expires, struct clock_event_device *); |
Thomas Gleixner | 847b2f4 | 2011-05-18 21:33:41 +0000 | [diff] [blame] | 103 | ktime_t next_event; |
Jon Hunter | 97813f2 | 2009-08-18 12:45:11 -0500 | [diff] [blame] | 104 | u64 max_delta_ns; |
| 105 | u64 min_delta_ns; |
Thomas Gleixner | 23af368 | 2009-11-11 14:05:25 +0000 | [diff] [blame] | 106 | u32 mult; |
| 107 | u32 shift; |
Thomas Gleixner | be3ef76 | 2015-06-02 14:30:11 +0200 | [diff] [blame] | 108 | enum clock_event_state state_use_accessors; |
Thomas Gleixner | 847b2f4 | 2011-05-18 21:33:41 +0000 | [diff] [blame] | 109 | unsigned int features; |
| 110 | unsigned long retries; |
| 111 | |
Viresh Kumar | 77e32c8 | 2015-02-27 17:21:33 +0530 | [diff] [blame] | 112 | int (*set_state_periodic)(struct clock_event_device *); |
| 113 | int (*set_state_oneshot)(struct clock_event_device *); |
Viresh Kumar | 8fff52f | 2015-04-03 09:04:04 +0530 | [diff] [blame] | 114 | int (*set_state_oneshot_stopped)(struct clock_event_device *); |
Viresh Kumar | 77e32c8 | 2015-02-27 17:21:33 +0530 | [diff] [blame] | 115 | int (*set_state_shutdown)(struct clock_event_device *); |
Viresh Kumar | 554ef38 | 2015-02-27 17:21:32 +0530 | [diff] [blame] | 116 | int (*tick_resume)(struct clock_event_device *); |
Viresh Kumar | bd624d7 | 2015-02-13 08:54:56 +0800 | [diff] [blame] | 117 | |
| 118 | void (*broadcast)(const struct cpumask *mask); |
Rafael J. Wysocki | adc78e6 | 2012-08-06 01:40:41 +0200 | [diff] [blame] | 119 | void (*suspend)(struct clock_event_device *); |
| 120 | void (*resume)(struct clock_event_device *); |
Thomas Gleixner | 57f0fcb | 2011-05-18 21:33:41 +0000 | [diff] [blame] | 121 | unsigned long min_delta_ticks; |
| 122 | unsigned long max_delta_ticks; |
| 123 | |
Thomas Gleixner | 847b2f4 | 2011-05-18 21:33:41 +0000 | [diff] [blame] | 124 | const char *name; |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 125 | int rating; |
| 126 | int irq; |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 127 | int bound_on; |
Rusty Russell | 320ab2b | 2008-12-13 21:20:26 +1030 | [diff] [blame] | 128 | const struct cpumask *cpumask; |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 129 | struct list_head list; |
Thomas Gleixner | ccf33d6 | 2013-04-25 20:31:49 +0000 | [diff] [blame] | 130 | struct module *owner; |
Thomas Gleixner | 847b2f4 | 2011-05-18 21:33:41 +0000 | [diff] [blame] | 131 | } ____cacheline_aligned; |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 132 | |
Viresh Kumar | 3434d23 | 2015-05-21 13:33:45 +0530 | [diff] [blame] | 133 | /* Helpers to verify state of a clockevent device */ |
| 134 | static inline bool clockevent_state_detached(struct clock_event_device *dev) |
| 135 | { |
Thomas Gleixner | be3ef76 | 2015-06-02 14:30:11 +0200 | [diff] [blame] | 136 | return dev->state_use_accessors == CLOCK_EVT_STATE_DETACHED; |
Viresh Kumar | 3434d23 | 2015-05-21 13:33:45 +0530 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static inline bool clockevent_state_shutdown(struct clock_event_device *dev) |
| 140 | { |
Thomas Gleixner | be3ef76 | 2015-06-02 14:30:11 +0200 | [diff] [blame] | 141 | return dev->state_use_accessors == CLOCK_EVT_STATE_SHUTDOWN; |
Viresh Kumar | 3434d23 | 2015-05-21 13:33:45 +0530 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static inline bool clockevent_state_periodic(struct clock_event_device *dev) |
| 145 | { |
Thomas Gleixner | be3ef76 | 2015-06-02 14:30:11 +0200 | [diff] [blame] | 146 | return dev->state_use_accessors == CLOCK_EVT_STATE_PERIODIC; |
Viresh Kumar | 3434d23 | 2015-05-21 13:33:45 +0530 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static inline bool clockevent_state_oneshot(struct clock_event_device *dev) |
| 150 | { |
Thomas Gleixner | be3ef76 | 2015-06-02 14:30:11 +0200 | [diff] [blame] | 151 | return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT; |
Viresh Kumar | 3434d23 | 2015-05-21 13:33:45 +0530 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static inline bool clockevent_state_oneshot_stopped(struct clock_event_device *dev) |
| 155 | { |
Thomas Gleixner | be3ef76 | 2015-06-02 14:30:11 +0200 | [diff] [blame] | 156 | return dev->state_use_accessors == CLOCK_EVT_STATE_ONESHOT_STOPPED; |
Viresh Kumar | 3434d23 | 2015-05-21 13:33:45 +0530 | [diff] [blame] | 157 | } |
| 158 | |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 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 | */ |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 170 | static inline unsigned long |
| 171 | div_sc(unsigned long ticks, unsigned long nsec, int shift) |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 172 | { |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 173 | u64 tmp = ((u64)ticks) << shift; |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 174 | |
| 175 | do_div(tmp, nsec); |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 176 | |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 177 | return (unsigned long) tmp; |
| 178 | } |
| 179 | |
| 180 | /* Clock event layer functions */ |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 181 | extern u64 clockevent_delta2ns(unsigned long latch, struct clock_event_device *evt); |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 182 | extern void clockevents_register_device(struct clock_event_device *dev); |
Thomas Gleixner | 03e13cf | 2013-04-25 20:31:50 +0000 | [diff] [blame] | 183 | extern int clockevents_unbind_device(struct clock_event_device *ced, int cpu); |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 184 | |
Magnus Damm | e540032 | 2012-05-09 23:39:34 +0900 | [diff] [blame] | 185 | extern void clockevents_config(struct clock_event_device *dev, u32 freq); |
Thomas Gleixner | 57f0fcb | 2011-05-18 21:33:41 +0000 | [diff] [blame] | 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 | |
Thomas Gleixner | 80b816b | 2011-05-18 21:33:42 +0000 | [diff] [blame] | 190 | extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq); |
| 191 | |
Thomas Gleixner | 7d2f944 | 2009-11-11 14:05:29 +0000 | [diff] [blame] | 192 | static inline void |
Alexander Kuleshov | dd42ac8f | 2015-10-16 15:20:53 +0600 | [diff] [blame] | 193 | clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 maxsec) |
Thomas Gleixner | 7d2f944 | 2009-11-11 14:05:29 +0000 | [diff] [blame] | 194 | { |
Alexander Kuleshov | dd42ac8f | 2015-10-16 15:20:53 +0600 | [diff] [blame] | 195 | return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC, freq, maxsec); |
Thomas Gleixner | 7d2f944 | 2009-11-11 14:05:29 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Rafael J. Wysocki | adc78e6 | 2012-08-06 01:40:41 +0200 | [diff] [blame] | 198 | extern void clockevents_suspend(void); |
| 199 | extern void clockevents_resume(void); |
| 200 | |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 201 | # ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST |
| 202 | # ifdef CONFIG_ARCH_HAS_TICK_BROADCAST |
Mark Rutland | 12ad100 | 2013-01-14 17:05:22 +0000 | [diff] [blame] | 203 | extern void tick_broadcast(const struct cpumask *mask); |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 204 | # else |
| 205 | # define tick_broadcast NULL |
| 206 | # endif |
Mark Rutland | 12572db | 2013-01-14 17:05:21 +0000 | [diff] [blame] | 207 | extern int tick_receive_broadcast(void); |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 208 | # endif |
Mark Rutland | 12572db | 2013-01-14 17:05:21 +0000 | [diff] [blame] | 209 | |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 210 | # if defined(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST) && defined(CONFIG_TICK_ONESHOT) |
Preeti U Murthy | 5d1638a | 2014-02-07 13:36:32 +0530 | [diff] [blame] | 211 | extern void tick_setup_hrtimer_broadcast(void); |
Thomas Gleixner | eaa907c | 2013-03-06 11:18:36 +0000 | [diff] [blame] | 212 | extern int tick_check_broadcast_expired(void); |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 213 | # else |
Thomas Gleixner | eaa907c | 2013-03-06 11:18:36 +0000 | [diff] [blame] | 214 | static inline int tick_check_broadcast_expired(void) { return 0; } |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 215 | static inline void tick_setup_hrtimer_broadcast(void) { } |
| 216 | # endif |
Thomas Gleixner | eaa907c | 2013-03-06 11:18:36 +0000 | [diff] [blame] | 217 | |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 218 | #else /* !CONFIG_GENERIC_CLOCKEVENTS: */ |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 219 | |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 220 | static inline void clockevents_suspend(void) { } |
| 221 | static inline void clockevents_resume(void) { } |
Thomas Gleixner | 1991922 | 2013-03-22 10:48:33 +0100 | [diff] [blame] | 222 | static inline int tick_check_broadcast_expired(void) { return 0; } |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 223 | static inline void tick_setup_hrtimer_broadcast(void) { } |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 224 | |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 225 | #endif /* !CONFIG_GENERIC_CLOCKEVENTS */ |
Thomas Gleixner | d316c57 | 2007-02-16 01:28:00 -0800 | [diff] [blame] | 226 | |
Ingo Molnar | 9eed56e | 2015-04-02 11:26:23 +0200 | [diff] [blame] | 227 | #endif /* _LINUX_CLOCKCHIPS_H */ |