tick-common.c 9.5 KB

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