clockchips.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_BUILD
  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. /* Clock event mode commands */
  17. enum clock_event_mode {
  18. CLOCK_EVT_MODE_UNUSED = 0,
  19. CLOCK_EVT_MODE_SHUTDOWN,
  20. CLOCK_EVT_MODE_PERIODIC,
  21. CLOCK_EVT_MODE_ONESHOT,
  22. CLOCK_EVT_MODE_RESUME,
  23. };
  24. /* Clock event notification values */
  25. enum clock_event_nofitiers {
  26. CLOCK_EVT_NOTIFY_ADD,
  27. CLOCK_EVT_NOTIFY_BROADCAST_ON,
  28. CLOCK_EVT_NOTIFY_BROADCAST_OFF,
  29. CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
  30. CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
  31. CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
  32. CLOCK_EVT_NOTIFY_SUSPEND,
  33. CLOCK_EVT_NOTIFY_RESUME,
  34. CLOCK_EVT_NOTIFY_CPU_DYING,
  35. CLOCK_EVT_NOTIFY_CPU_DEAD,
  36. };
  37. /*
  38. * Clock event features
  39. */
  40. #define CLOCK_EVT_FEAT_PERIODIC 0x000001
  41. #define CLOCK_EVT_FEAT_ONESHOT 0x000002
  42. /*
  43. * x86(64) specific misfeatures:
  44. *
  45. * - Clockevent source stops in C3 State and needs broadcast support.
  46. * - Local APIC timer is used as a dummy device.
  47. */
  48. #define CLOCK_EVT_FEAT_C3STOP 0x000004
  49. #define CLOCK_EVT_FEAT_DUMMY 0x000008
  50. /**
  51. * struct clock_event_device - clock event device descriptor
  52. * @event_handler: Assigned by the framework to be called by the low
  53. * level handler of the event source
  54. * @set_next_event: set next event function
  55. * @next_event: local storage for the next event in oneshot mode
  56. * @max_delta_ns: maximum delta value in ns
  57. * @min_delta_ns: minimum delta value in ns
  58. * @mult: nanosecond to cycles multiplier
  59. * @shift: nanoseconds to cycles divisor (power of two)
  60. * @mode: operating mode assigned by the management code
  61. * @features: features
  62. * @retries: number of forced programming retries
  63. * @set_mode: set mode function
  64. * @broadcast: function to broadcast events
  65. * @min_delta_ticks: minimum delta value in ticks stored for reconfiguration
  66. * @max_delta_ticks: maximum delta value in ticks stored for reconfiguration
  67. * @name: ptr to clock event name
  68. * @rating: variable to rate clock event devices
  69. * @irq: IRQ number (only for non CPU local devices)
  70. * @cpumask: cpumask to indicate for which CPUs this device works
  71. * @list: list head for the management code
  72. */
  73. struct clock_event_device {
  74. void (*event_handler)(struct clock_event_device *);
  75. int (*set_next_event)(unsigned long evt,
  76. struct clock_event_device *);
  77. ktime_t next_event;
  78. u64 max_delta_ns;
  79. u64 min_delta_ns;
  80. u32 mult;
  81. u32 shift;
  82. enum clock_event_mode mode;
  83. unsigned int features;
  84. unsigned long retries;
  85. void (*broadcast)(const struct cpumask *mask);
  86. void (*set_mode)(enum clock_event_mode mode,
  87. struct clock_event_device *);
  88. unsigned long min_delta_ticks;
  89. unsigned long max_delta_ticks;
  90. const char *name;
  91. int rating;
  92. int irq;
  93. const struct cpumask *cpumask;
  94. struct list_head list;
  95. } ____cacheline_aligned;
  96. /*
  97. * Calculate a multiplication factor for scaled math, which is used to convert
  98. * nanoseconds based values to clock ticks:
  99. *
  100. * clock_ticks = (nanoseconds * factor) >> shift.
  101. *
  102. * div_sc is the rearranged equation to calculate a factor from a given clock
  103. * ticks / nanoseconds ratio:
  104. *
  105. * factor = (clock_ticks << shift) / nanoseconds
  106. */
  107. static inline unsigned long div_sc(unsigned long ticks, unsigned long nsec,
  108. int shift)
  109. {
  110. uint64_t tmp = ((uint64_t)ticks) << shift;
  111. do_div(tmp, nsec);
  112. return (unsigned long) tmp;
  113. }
  114. /* Clock event layer functions */
  115. extern u64 clockevent_delta2ns(unsigned long latch,
  116. struct clock_event_device *evt);
  117. extern void clockevents_register_device(struct clock_event_device *dev);
  118. extern void clockevents_config_and_register(struct clock_event_device *dev,
  119. u32 freq, unsigned long min_delta,
  120. unsigned long max_delta);
  121. extern int clockevents_update_freq(struct clock_event_device *ce, u32 freq);
  122. extern void clockevents_exchange_device(struct clock_event_device *old,
  123. struct clock_event_device *new);
  124. extern void clockevents_set_mode(struct clock_event_device *dev,
  125. enum clock_event_mode mode);
  126. extern int clockevents_register_notifier(struct notifier_block *nb);
  127. extern int clockevents_program_event(struct clock_event_device *dev,
  128. ktime_t expires, ktime_t now);
  129. extern void clockevents_handle_noop(struct clock_event_device *dev);
  130. static inline void
  131. clockevents_calc_mult_shift(struct clock_event_device *ce, u32 freq, u32 minsec)
  132. {
  133. return clocks_calc_mult_shift(&ce->mult, &ce->shift, NSEC_PER_SEC,
  134. freq, minsec);
  135. }
  136. #ifdef CONFIG_GENERIC_CLOCKEVENTS
  137. extern void clockevents_notify(unsigned long reason, void *arg);
  138. #else
  139. # define clockevents_notify(reason, arg) do { } while (0)
  140. #endif
  141. #else /* CONFIG_GENERIC_CLOCKEVENTS_BUILD */
  142. #define clockevents_notify(reason, arg) do { } while (0)
  143. #endif
  144. #endif