tick-common.c 8.8 KB

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