tick-common.c 9.9 KB

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