tick-common.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * linux/kernel/time/tick-common.c
  3. *
  4. * This file contains the base functions to manage periodic tick
  5. * related events.
  6. *
  7. * Copyright(C) 2005-2006, Thomas Gleixner <tglx@linutronix.de>
  8. * Copyright(C) 2005-2007, Red Hat, Inc., Ingo Molnar
  9. * Copyright(C) 2006-2007, Timesys Corp., Thomas Gleixner
  10. *
  11. * This code is licenced under the GPL version 2. For details see
  12. * kernel-base/COPYING.
  13. */
  14. #include <linux/cpu.h>
  15. #include <linux/err.h>
  16. #include <linux/hrtimer.h>
  17. #include <linux/irq.h>
  18. #include <linux/percpu.h>
  19. #include <linux/profile.h>
  20. #include <linux/sched.h>
  21. #include <linux/tick.h>
  22. /*
  23. * Tick devices
  24. */
  25. static DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
  26. /*
  27. * Tick next event: keeps track of the tick time
  28. */
  29. static ktime_t tick_next_period;
  30. static ktime_t tick_period;
  31. static int tick_do_timer_cpu = -1;
  32. static DEFINE_SPINLOCK(tick_device_lock);
  33. /*
  34. * Periodic tick
  35. */
  36. static void tick_periodic(int cpu)
  37. {
  38. if (tick_do_timer_cpu == cpu) {
  39. write_seqlock(&xtime_lock);
  40. /* Keep track of the next tick event */
  41. tick_next_period = ktime_add(tick_next_period, tick_period);
  42. do_timer(1);
  43. write_sequnlock(&xtime_lock);
  44. }
  45. update_process_times(user_mode(get_irq_regs()));
  46. profile_tick(CPU_PROFILING);
  47. }
  48. /*
  49. * Event handler for periodic ticks
  50. */
  51. void tick_handle_periodic(struct clock_event_device *dev)
  52. {
  53. int cpu = smp_processor_id();
  54. tick_periodic(cpu);
  55. if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
  56. return;
  57. /*
  58. * Setup the next period for devices, which do not have
  59. * periodic mode:
  60. */
  61. for (;;) {
  62. ktime_t next = ktime_add(dev->next_event, tick_period);
  63. if (!clockevents_program_event(dev, next, ktime_get()))
  64. return;
  65. tick_periodic(cpu);
  66. }
  67. }
  68. /*
  69. * Setup the device for a periodic tick
  70. */
  71. void tick_setup_periodic(struct clock_event_device *dev)
  72. {
  73. dev->event_handler = tick_handle_periodic;
  74. if (dev->features & CLOCK_EVT_FEAT_PERIODIC) {
  75. clockevents_set_mode(dev, CLOCK_EVT_MODE_PERIODIC);
  76. } else {
  77. unsigned long seq;
  78. ktime_t next;
  79. do {
  80. seq = read_seqbegin(&xtime_lock);
  81. next = tick_next_period;
  82. } while (read_seqretry(&xtime_lock, seq));
  83. clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
  84. for (;;) {
  85. if (!clockevents_program_event(dev, next, ktime_get()))
  86. return;
  87. next = ktime_add(next, tick_period);
  88. }
  89. }
  90. }
  91. /*
  92. * Setup the tick device
  93. */
  94. static void tick_setup_device(struct tick_device *td,
  95. struct clock_event_device *newdev, int cpu,
  96. cpumask_t cpumask)
  97. {
  98. ktime_t next_event;
  99. void (*handler)(struct clock_event_device *) = NULL;
  100. /*
  101. * First device setup ?
  102. */
  103. if (!td->evtdev) {
  104. /*
  105. * If no cpu took the do_timer update, assign it to
  106. * this cpu:
  107. */
  108. if (tick_do_timer_cpu == -1) {
  109. tick_do_timer_cpu = cpu;
  110. tick_next_period = ktime_get();
  111. tick_period = ktime_set(0, NSEC_PER_SEC / HZ);
  112. }
  113. /*
  114. * Startup in periodic mode first.
  115. */
  116. td->mode = TICKDEV_MODE_PERIODIC;
  117. } else {
  118. handler = td->evtdev->event_handler;
  119. next_event = td->evtdev->next_event;
  120. }
  121. td->evtdev = newdev;
  122. /*
  123. * When the device is not per cpu, pin the interrupt to the
  124. * current cpu:
  125. */
  126. if (!cpus_equal(newdev->cpumask, cpumask))
  127. irq_set_affinity(newdev->irq, cpumask);
  128. if (td->mode == TICKDEV_MODE_PERIODIC)
  129. tick_setup_periodic(newdev, 0);
  130. }
  131. /*
  132. * Check, if the new registered device should be used.
  133. */
  134. static int tick_check_new_device(struct clock_event_device *newdev)
  135. {
  136. struct clock_event_device *curdev;
  137. struct tick_device *td;
  138. int cpu, ret = NOTIFY_OK;
  139. unsigned long flags;
  140. cpumask_t cpumask;
  141. spin_lock_irqsave(&tick_device_lock, flags);
  142. cpu = smp_processor_id();
  143. if (!cpu_isset(cpu, newdev->cpumask))
  144. goto out;
  145. td = &per_cpu(tick_cpu_device, cpu);
  146. curdev = td->evtdev;
  147. cpumask = cpumask_of_cpu(cpu);
  148. /* cpu local device ? */
  149. if (!cpus_equal(newdev->cpumask, cpumask)) {
  150. /*
  151. * If the cpu affinity of the device interrupt can not
  152. * be set, ignore it.
  153. */
  154. if (!irq_can_set_affinity(newdev->irq))
  155. goto out_bc;
  156. /*
  157. * If we have a cpu local device already, do not replace it
  158. * by a non cpu local device
  159. */
  160. if (curdev && cpus_equal(curdev->cpumask, cpumask))
  161. goto out_bc;
  162. }
  163. /*
  164. * If we have an active device, then check the rating and the oneshot
  165. * feature.
  166. */
  167. if (curdev) {
  168. /*
  169. * Check the rating
  170. */
  171. if (curdev->rating >= newdev->rating)
  172. goto out;
  173. }
  174. /*
  175. * Replace the eventually existing device by the new
  176. * device.
  177. */
  178. clockevents_exchange_device(curdev, newdev);
  179. tick_setup_device(td, newdev, cpu, cpumask);
  180. ret = NOTIFY_STOP;
  181. out:
  182. spin_unlock_irqrestore(&tick_device_lock, flags);
  183. return ret;
  184. }
  185. /*
  186. * Shutdown an event device on a given cpu:
  187. *
  188. * This is called on a life CPU, when a CPU is dead. So we cannot
  189. * access the hardware device itself.
  190. * We just set the mode and remove it from the lists.
  191. */
  192. static void tick_shutdown(unsigned int *cpup)
  193. {
  194. struct tick_device *td = &per_cpu(tick_cpu_device, *cpup);
  195. struct clock_event_device *dev = td->evtdev;
  196. unsigned long flags;
  197. spin_lock_irqsave(&tick_device_lock, flags);
  198. td->mode = TICKDEV_MODE_PERIODIC;
  199. if (dev) {
  200. /*
  201. * Prevent that the clock events layer tries to call
  202. * the set mode function!
  203. */
  204. dev->mode = CLOCK_EVT_MODE_UNUSED;
  205. clockevents_exchange_device(dev, NULL);
  206. td->evtdev = NULL;
  207. }
  208. spin_unlock_irqrestore(&tick_device_lock, flags);
  209. }
  210. /*
  211. * Notification about clock event devices
  212. */
  213. static int tick_notify(struct notifier_block *nb, unsigned long reason,
  214. void *dev)
  215. {
  216. switch (reason) {
  217. case CLOCK_EVT_NOTIFY_ADD:
  218. return tick_check_new_device(dev);
  219. case CLOCK_EVT_NOTIFY_CPU_DEAD:
  220. tick_shutdown(dev);
  221. break;
  222. default:
  223. break;
  224. }
  225. return NOTIFY_OK;
  226. }
  227. static struct notifier_block tick_notifier = {
  228. .notifier_call = tick_notify,
  229. };
  230. /**
  231. * tick_init - initialize the tick control
  232. *
  233. * Register the notifier with the clockevents framework
  234. */
  235. void __init tick_init(void)
  236. {
  237. clockevents_register_notifier(&tick_notifier);
  238. }