tick-common.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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/module.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 = TICK_DO_TIMER_BOOT;
  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 = __this_cpu_read(tick_cpu_device.evtdev);
  47. if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT))
  48. return 0;
  49. if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
  50. return 1;
  51. return tick_broadcast_oneshot_available();
  52. }
  53. /*
  54. * Periodic tick
  55. */
  56. static void tick_periodic(int cpu)
  57. {
  58. if (tick_do_timer_cpu == cpu) {
  59. write_seqlock(&jiffies_lock);
  60. /* Keep track of the next tick event */
  61. tick_next_period = ktime_add(tick_next_period, tick_period);
  62. do_timer(1);
  63. write_sequnlock(&jiffies_lock);
  64. }
  65. update_process_times(user_mode(get_irq_regs()));
  66. profile_tick(CPU_PROFILING);
  67. }
  68. /*
  69. * Event handler for periodic ticks
  70. */
  71. void tick_handle_periodic(struct clock_event_device *dev)
  72. {
  73. int cpu = smp_processor_id();
  74. ktime_t next;
  75. tick_periodic(cpu);
  76. if (dev->mode != CLOCK_EVT_MODE_ONESHOT)
  77. return;
  78. /*
  79. * Setup the next period for devices, which do not have
  80. * periodic mode:
  81. */
  82. next = ktime_add(dev->next_event, tick_period);
  83. for (;;) {
  84. if (!clockevents_program_event(dev, next, false))
  85. return;
  86. /*
  87. * Have to be careful here. If we're in oneshot mode,
  88. * before we call tick_periodic() in a loop, we need
  89. * to be sure we're using a real hardware clocksource.
  90. * Otherwise we could get trapped in an infinite
  91. * loop, as the tick_periodic() increments jiffies,
  92. * when then will increment time, posibly causing
  93. * the loop to trigger again and again.
  94. */
  95. if (timekeeping_valid_for_hres())
  96. tick_periodic(cpu);
  97. next = ktime_add(next, tick_period);
  98. }
  99. }
  100. /*
  101. * Setup the device for a periodic tick
  102. */
  103. void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
  104. {
  105. tick_set_periodic_handler(dev, broadcast);
  106. /* Broadcast setup ? */
  107. if (!tick_device_is_functional(dev))
  108. return;
  109. if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) &&
  110. !tick_broadcast_oneshot_active()) {
  111. clockevents_set_mode(dev, CLOCK_EVT_MODE_PERIODIC);
  112. } else {
  113. unsigned long seq;
  114. ktime_t next;
  115. do {
  116. seq = read_seqbegin(&jiffies_lock);
  117. next = tick_next_period;
  118. } while (read_seqretry(&jiffies_lock, seq));
  119. clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
  120. for (;;) {
  121. if (!clockevents_program_event(dev, next, false))
  122. return;
  123. next = ktime_add(next, tick_period);
  124. }
  125. }
  126. }
  127. /*
  128. * Setup the tick device
  129. */
  130. static void tick_setup_device(struct tick_device *td,
  131. struct clock_event_device *newdev, int cpu,
  132. const struct cpumask *cpumask)
  133. {
  134. ktime_t next_event;
  135. void (*handler)(struct clock_event_device *) = NULL;
  136. /*
  137. * First device setup ?
  138. */
  139. if (!td->evtdev) {
  140. /*
  141. * If no cpu took the do_timer update, assign it to
  142. * this cpu:
  143. */
  144. if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) {
  145. if (!tick_nohz_full_cpu(cpu))
  146. tick_do_timer_cpu = cpu;
  147. else
  148. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  149. tick_next_period = ktime_get();
  150. tick_period = ktime_set(0, NSEC_PER_SEC / HZ);
  151. }
  152. /*
  153. * Startup in periodic mode first.
  154. */
  155. td->mode = TICKDEV_MODE_PERIODIC;
  156. } else {
  157. handler = td->evtdev->event_handler;
  158. next_event = td->evtdev->next_event;
  159. td->evtdev->event_handler = clockevents_handle_noop;
  160. }
  161. td->evtdev = newdev;
  162. /*
  163. * When the device is not per cpu, pin the interrupt to the
  164. * current cpu:
  165. */
  166. if (!cpumask_equal(newdev->cpumask, cpumask))
  167. irq_set_affinity(newdev->irq, cpumask);
  168. /*
  169. * When global broadcasting is active, check if the current
  170. * device is registered as a placeholder for broadcast mode.
  171. * This allows us to handle this x86 misfeature in a generic
  172. * way. This function also returns !=0 when we keep the
  173. * current active broadcast state for this CPU.
  174. */
  175. if (tick_device_uses_broadcast(newdev, cpu))
  176. return;
  177. if (td->mode == TICKDEV_MODE_PERIODIC)
  178. tick_setup_periodic(newdev, 0);
  179. else
  180. tick_setup_oneshot(newdev, handler, next_event);
  181. }
  182. void tick_install_replacement(struct clock_event_device *newdev)
  183. {
  184. struct tick_device *td = &__get_cpu_var(tick_cpu_device);
  185. int cpu = smp_processor_id();
  186. clockevents_exchange_device(td->evtdev, newdev);
  187. tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
  188. if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
  189. tick_oneshot_notify();
  190. }
  191. static bool tick_check_percpu(struct clock_event_device *curdev,
  192. struct clock_event_device *newdev, int cpu)
  193. {
  194. if (!cpumask_test_cpu(cpu, newdev->cpumask))
  195. return false;
  196. if (cpumask_equal(newdev->cpumask, cpumask_of(cpu)))
  197. return true;
  198. /* Check if irq affinity can be set */
  199. if (newdev->irq >= 0 && !irq_can_set_affinity(newdev->irq))
  200. return false;
  201. /* Prefer an existing cpu local device */
  202. if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu)))
  203. return false;
  204. return true;
  205. }
  206. static bool tick_check_preferred(struct clock_event_device *curdev,
  207. struct clock_event_device *newdev)
  208. {
  209. /* Prefer oneshot capable device */
  210. if (!(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) {
  211. if (curdev && (curdev->features & CLOCK_EVT_FEAT_ONESHOT))
  212. return false;
  213. if (tick_oneshot_mode_active())
  214. return false;
  215. }
  216. /*
  217. * Use the higher rated one, but prefer a CPU local device with a lower
  218. * rating than a non-CPU local device
  219. */
  220. return !curdev ||
  221. newdev->rating > curdev->rating ||
  222. !cpumask_equal(curdev->cpumask, newdev->cpumask);
  223. }
  224. /*
  225. * Check whether the new device is a better fit than curdev. curdev
  226. * can be NULL !
  227. */
  228. bool tick_check_replacement(struct clock_event_device *curdev,
  229. struct clock_event_device *newdev)
  230. {
  231. if (tick_check_percpu(curdev, newdev, smp_processor_id()))
  232. return false;
  233. return tick_check_preferred(curdev, newdev);
  234. }
  235. /*
  236. * Check, if the new registered device should be used. Called with
  237. * clockevents_lock held and interrupts disabled.
  238. */
  239. void tick_check_new_device(struct clock_event_device *newdev)
  240. {
  241. struct clock_event_device *curdev;
  242. struct tick_device *td;
  243. int cpu;
  244. cpu = smp_processor_id();
  245. if (!cpumask_test_cpu(cpu, newdev->cpumask))
  246. goto out_bc;
  247. td = &per_cpu(tick_cpu_device, cpu);
  248. curdev = td->evtdev;
  249. /* cpu local device ? */
  250. if (!tick_check_percpu(curdev, newdev, cpu))
  251. goto out_bc;
  252. /* Preference decision */
  253. if (!tick_check_preferred(curdev, newdev))
  254. goto out_bc;
  255. if (!try_module_get(newdev->owner))
  256. return;
  257. /*
  258. * Replace the eventually existing device by the new
  259. * device. If the current device is the broadcast device, do
  260. * not give it back to the clockevents layer !
  261. */
  262. if (tick_is_broadcast_device(curdev)) {
  263. clockevents_shutdown(curdev);
  264. curdev = NULL;
  265. }
  266. clockevents_exchange_device(curdev, newdev);
  267. tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
  268. if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
  269. tick_oneshot_notify();
  270. return;
  271. out_bc:
  272. /*
  273. * Can the new device be used as a broadcast device ?
  274. */
  275. tick_install_broadcast_device(newdev);
  276. }
  277. /*
  278. * Transfer the do_timer job away from a dying cpu.
  279. *
  280. * Called with interrupts disabled.
  281. */
  282. void tick_handover_do_timer(int *cpup)
  283. {
  284. if (*cpup == tick_do_timer_cpu) {
  285. int cpu = cpumask_first(cpu_online_mask);
  286. tick_do_timer_cpu = (cpu < nr_cpu_ids) ? cpu :
  287. TICK_DO_TIMER_NONE;
  288. }
  289. }
  290. /*
  291. * Shutdown an event device on a given cpu:
  292. *
  293. * This is called on a life CPU, when a CPU is dead. So we cannot
  294. * access the hardware device itself.
  295. * We just set the mode and remove it from the lists.
  296. */
  297. void tick_shutdown(unsigned int *cpup)
  298. {
  299. struct tick_device *td = &per_cpu(tick_cpu_device, *cpup);
  300. struct clock_event_device *dev = td->evtdev;
  301. td->mode = TICKDEV_MODE_PERIODIC;
  302. if (dev) {
  303. /*
  304. * Prevent that the clock events layer tries to call
  305. * the set mode function!
  306. */
  307. dev->mode = CLOCK_EVT_MODE_UNUSED;
  308. clockevents_exchange_device(dev, NULL);
  309. dev->event_handler = clockevents_handle_noop;
  310. td->evtdev = NULL;
  311. }
  312. }
  313. void tick_suspend(void)
  314. {
  315. struct tick_device *td = &__get_cpu_var(tick_cpu_device);
  316. clockevents_shutdown(td->evtdev);
  317. }
  318. void tick_resume(void)
  319. {
  320. struct tick_device *td = &__get_cpu_var(tick_cpu_device);
  321. int broadcast = tick_resume_broadcast();
  322. clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_RESUME);
  323. if (!broadcast) {
  324. if (td->mode == TICKDEV_MODE_PERIODIC)
  325. tick_setup_periodic(td->evtdev, 0);
  326. else
  327. tick_resume_oneshot();
  328. }
  329. }
  330. /**
  331. * tick_init - initialize the tick control
  332. */
  333. void __init tick_init(void)
  334. {
  335. tick_broadcast_init();
  336. }