tick-common.c 9.4 KB

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