tick-common.c 8.7 KB

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