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