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