clockchips.h 7.3 KB

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