tick-common.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. #include "tick-internal.h"
  23. /*
  24. * Tick devices
  25. */
  26. DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
  27. /*
  28. * Tick next event: keeps track of the tick time
  29. */
  30. ktime_t tick_next_period;
  31. ktime_t tick_period;
  32. static int tick_do_timer_cpu = -1;
  33. DEFINE_SPINLOCK(tick_device_lock);
  34. /*
  35. * Debugging: see timer_list.c
  36. */
  37. struct tick_device *tick_get_device(int cpu)
  38. {
  39. return &per_cpu(tick_cpu_device, cpu);
  40. }
  41. /**
  42. * tick_is_oneshot_available - check for a oneshot capable event device
  43. */
  44. int tick_is_oneshot_available(void)
  45. {
  46. struct clock_event_device *dev = __get_cpu_var(tick_cpu_device).evtdev;
  47. return dev && (dev->features & CLOCK_EVT_FEAT_ONESHOT);
  48. }
  49. /*
  50. * Periodic tick
  51. */
  52. static void tick_periodic(int cpu)
  53. {
  54. if (tick_do_timer_cpu == cpu) {
  55. write_seqlock(&xtime_lock);
  56. /* Keep track of the next tick event */
  57. tick_next_period = ktime_add(tick_next_period, tick_period);
  58. do_timer(1);
  59. write_sequnlock(&xtime_lock);
  60. }
  61. update_process_times(user_mode(get_irq_regs()));
  62. profile_tick(CPU_PROFILING);
  63. }
  64. /*
  65. * Event handler for periodic ticks
  66. */
  67. void tick_handle_periodic(struct clock_event_device *dev)
  68. {
  69. int cpu = smp_processor_id();
  70. tick_periodic(cpu);
  71. if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
  72. return;
  73. /*
  74. * Setup the next period for devices, which do not have
  75. * periodic mode:
  76. */
  77. for (;;) {
  78. ktime_t next = ktime_add(dev->next_event, tick_period);
  79. if (!clockevents_program_event(dev, next, ktime_get()))
  80. return;
  81. tick_periodic(cpu);
  82. }
  83. }
  84. /*
  85. * Setup the device for a periodic tick
  86. */
  87. void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
  88. {
  89. tick_set_periodic_handler(dev, broadcast);
  90. /* Broadcast setup ? */
  91. if (!tick_device_is_functional(dev))
  92. return;
  93. if (dev->features & CLOCK_EVT_FEAT_PERIODIC) {
  94. clockevents_set_mode(dev, CLOCK_EVT_MODE_PERIODIC);
  95. } else {
  96. unsigned long seq;
  97. ktime_t next;
  98. do {
  99. seq = read_seqbegin(&xtime_lock);
  100. next = tick_next_period;
  101. } while (read_seqretry(&xtime_lock, seq));
  102. clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
  103. for (;;) {
  104. if (!clockevents_program_event(dev, next, ktime_get()))
  105. return;
  106. next = ktime_add(next, tick_period);
  107. }
  108. }
  109. }
  110. /*
  111. * Setup the tick device
  112. */
  113. static void tick_setup_device(struct tick_device *td,
  114. struct clock_event_device *newdev, int cpu,
  115. cpumask_t cpumask)
  116. {
  117. ktime_t next_event;
  118. void (*handler)(struct clock_event_device *) = NULL;
  119. /*
  120. * First device setup ?
  121. */
  122. if (!td->evtdev) {
  123. /*
  124. * If no cpu took the do_timer update, assign it to
  125. * this cpu:
  126. */
  127. if (tick_do_timer_cpu == -1) {
  128. tick_do_timer_cpu = cpu;
  129. tick_next_period = ktime_get();
  130. tick_period = ktime_set(0, NSEC_PER_SEC / HZ);
  131. }
  132. /*
  133. * Startup in periodic mode first.
  134. */
  135. td->mode = TICKDEV_MODE_PERIODIC;
  136. } else {
  137. handler = td->evtdev->event_handler;
  138. next_event = td->evtdev->next_event;
  139. }
  140. td->evtdev = newdev;
  141. /*
  142. * When the device is not per cpu, pin the interrupt to the
  143. * current cpu:
  144. */
  145. if (!cpus_equal(newdev->cpumask, cpumask))
  146. irq_set_affinity(newdev->irq, cpumask);
  147. /*
  148. * When global broadcasting is active, check if the current
  149. * device is registered as a placeholder for broadcast mode.
  150. * This allows us to handle this x86 misfeature in a generic
  151. * way.
  152. */
  153. if (tick_device_uses_broadcast(newdev, cpu))
  154. return;
  155. if (td->mode == TICKDEV_MODE_PERIODIC)
  156. tick_setup_periodic(newdev, 0);
  157. else
  158. tick_setup_oneshot(newdev, handler, next_event);
  159. }
  160. /*
  161. * Check, if the new registered device should be used.
  162. */
  163. static int tick_check_new_device(struct clock_event_device *newdev)
  164. {
  165. struct clock_event_device *curdev;
  166. struct tick_device *td;
  167. int cpu, ret = NOTIFY_OK;
  168. unsigned long flags;
  169. cpumask_t cpumask;
  170. spin_lock_irqsave(&tick_device_lock, flags);
  171. cpu = smp_processor_id();
  172. if (!cpu_isset(cpu, newdev->cpumask))
  173. goto out;
  174. td = &per_cpu(tick_cpu_device, cpu);
  175. curdev = td->evtdev;
  176. cpumask = cpumask_of_cpu(cpu);
  177. /* cpu local device ? */
  178. if (!cpus_equal(newdev->cpumask, cpumask)) {
  179. /*
  180. * If the cpu affinity of the device interrupt can not
  181. * be set, ignore it.
  182. */
  183. if (!irq_can_set_affinity(newdev->irq))
  184. goto out_bc;
  185. /*
  186. * If we have a cpu local device already, do not replace it
  187. * by a non cpu local device
  188. */
  189. if (curdev && cpus_equal(curdev->cpumask, cpumask))
  190. goto out_bc;
  191. }
  192. /*
  193. * If we have an active device, then check the rating and the oneshot
  194. * feature.
  195. */
  196. if (curdev) {
  197. /*
  198. * Prefer one shot capable devices !
  199. */
  200. if ((curdev->features & CLOCK_EVT_FEAT_ONESHOT) &&
  201. !(newdev->features & CLOCK_EVT_FEAT_ONESHOT))
  202. goto out_bc;
  203. /*
  204. * Check the rating
  205. */
  206. if (curdev->rating >= newdev->rating)
  207. goto out_bc;
  208. }
  209. /*
  210. * Replace the eventually existing device by the new
  211. * device. If the current device is the broadcast device, do
  212. * not give it back to the clockevents layer !
  213. */
  214. if (tick_is_broadcast_device(curdev)) {
  215. clockevents_set_mode(curdev, CLOCK_EVT_MODE_SHUTDOWN);
  216. curdev = NULL;
  217. }
  218. clockevents_exchange_device(curdev, newdev);
  219. tick_setup_device(td, newdev, cpu, cpumask);
  220. if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
  221. tick_oneshot_notify();
  222. spin_unlock_irqrestore(&tick_device_lock, flags);
  223. return NOTIFY_STOP;
  224. out_bc:
  225. /*
  226. * Can the new device be used as a broadcast device ?
  227. */
  228. if (tick_check_broadcast_device(newdev))
  229. ret = NOTIFY_STOP;
  230. out:
  231. spin_unlock_irqrestore(&tick_device_lock, flags);
  232. return ret;
  233. }
  234. /*
  235. * Shutdown an event device on a given cpu:
  236. *
  237. * This is called on a life CPU, when a CPU is dead. So we cannot
  238. * access the hardware device itself.
  239. * We just set the mode and remove it from the lists.
  240. */
  241. static void tick_shutdown(unsigned int *cpup)
  242. {
  243. struct tick_device *td = &per_cpu(tick_cpu_device, *cpup);
  244. struct clock_event_device *dev = td->evtdev;
  245. unsigned long flags;
  246. spin_lock_irqsave(&tick_device_lock, flags);
  247. td->mode = TICKDEV_MODE_PERIODIC;
  248. if (dev) {
  249. /*
  250. * Prevent that the clock events layer tries to call
  251. * the set mode function!
  252. */
  253. dev->mode = CLOCK_EVT_MODE_UNUSED;
  254. clockevents_exchange_device(dev, NULL);
  255. td->evtdev = NULL;
  256. }
  257. spin_unlock_irqrestore(&tick_device_lock, flags);
  258. }
  259. /*
  260. * Notification about clock event devices
  261. */
  262. static int tick_notify(struct notifier_block *nb, unsigned long reason,
  263. void *dev)
  264. {
  265. switch (reason) {
  266. case CLOCK_EVT_NOTIFY_ADD:
  267. return tick_check_new_device(dev);
  268. case CLOCK_EVT_NOTIFY_BROADCAST_ON:
  269. case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
  270. tick_broadcast_on_off(reason, dev);
  271. break;
  272. case CLOCK_EVT_NOTIFY_BROADCAST_ENTER:
  273. case CLOCK_EVT_NOTIFY_BROADCAST_EXIT:
  274. tick_broadcast_oneshot_control(reason);
  275. break;
  276. case CLOCK_EVT_NOTIFY_CPU_DEAD:
  277. tick_shutdown_broadcast_oneshot(dev);
  278. tick_shutdown_broadcast(dev);
  279. tick_shutdown(dev);
  280. break;
  281. default:
  282. break;
  283. }
  284. return NOTIFY_OK;
  285. }
  286. static struct notifier_block tick_notifier = {
  287. .notifier_call = tick_notify,
  288. };
  289. /**
  290. * tick_init - initialize the tick control
  291. *
  292. * Register the notifier with the clockevents framework
  293. */
  294. void __init tick_init(void)
  295. {
  296. clockevents_register_notifier(&tick_notifier);
  297. }