clockchips.h 7.3 KB

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