clockchips.h 7.8 KB

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