clockchips.h 8.2 KB

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