tick-common.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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/nmi.h>
  19. #include <linux/percpu.h>
  20. #include <linux/profile.h>
  21. #include <linux/sched.h>
  22. #include <linux/module.h>
  23. #include <trace/events/power.h>
  24. #include <asm/irq_regs.h>
  25. #include "tick-internal.h"
  26. /*
  27. * Tick devices
  28. */
  29. DEFINE_PER_CPU(struct tick_device, tick_cpu_device);
  30. /*
  31. * Tick next event: keeps track of the tick time
  32. */
  33. ktime_t tick_next_period;
  34. ktime_t tick_period;
  35. /*
  36. * tick_do_timer_cpu is a timer core internal variable which holds the CPU NR
  37. * which is responsible for calling do_timer(), i.e. the timekeeping stuff. This
  38. * variable has two functions:
  39. *
  40. * 1) Prevent a thundering herd issue of a gazillion of CPUs trying to grab the
  41. * timekeeping lock all at once. Only the CPU which is assigned to do the
  42. * update is handling it.
  43. *
  44. * 2) Hand off the duty in the NOHZ idle case by setting the value to
  45. * TICK_DO_TIMER_NONE, i.e. a non existing CPU. So the next cpu which looks
  46. * at it will take over and keep the time keeping alive. The handover
  47. * procedure also covers cpu hotplug.
  48. */
  49. int tick_do_timer_cpu __read_mostly = TICK_DO_TIMER_BOOT;
  50. /*
  51. * Debugging: see timer_list.c
  52. */
  53. struct tick_device *tick_get_device(int cpu)
  54. {
  55. return &per_cpu(tick_cpu_device, cpu);
  56. }
  57. /**
  58. * tick_is_oneshot_available - check for a oneshot capable event device
  59. */
  60. int tick_is_oneshot_available(void)
  61. {
  62. struct clock_event_device *dev = __this_cpu_read(tick_cpu_device.evtdev);
  63. if (!dev || !(dev->features & CLOCK_EVT_FEAT_ONESHOT))
  64. return 0;
  65. if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
  66. return 1;
  67. return tick_broadcast_oneshot_available();
  68. }
  69. /*
  70. * Periodic tick
  71. */
  72. static void tick_periodic(int cpu)
  73. {
  74. if (tick_do_timer_cpu == cpu) {
  75. write_seqlock(&jiffies_lock);
  76. /* Keep track of the next tick event */
  77. tick_next_period = ktime_add(tick_next_period, tick_period);
  78. do_timer(1);
  79. write_sequnlock(&jiffies_lock);
  80. update_wall_time();
  81. }
  82. update_process_times(user_mode(get_irq_regs()));
  83. profile_tick(CPU_PROFILING);
  84. }
  85. /*
  86. * Event handler for periodic ticks
  87. */
  88. void tick_handle_periodic(struct clock_event_device *dev)
  89. {
  90. int cpu = smp_processor_id();
  91. ktime_t next = dev->next_event;
  92. tick_periodic(cpu);
  93. #if defined(CONFIG_HIGH_RES_TIMERS) || defined(CONFIG_NO_HZ_COMMON)
  94. /*
  95. * The cpu might have transitioned to HIGHRES or NOHZ mode via
  96. * update_process_times() -> run_local_timers() ->
  97. * hrtimer_run_queues().
  98. */
  99. if (dev->event_handler != tick_handle_periodic)
  100. return;
  101. #endif
  102. if (!clockevent_state_oneshot(dev))
  103. return;
  104. for (;;) {
  105. /*
  106. * Setup the next period for devices, which do not have
  107. * periodic mode:
  108. */
  109. next = ktime_add(next, tick_period);
  110. if (!clockevents_program_event(dev, next, false))
  111. return;
  112. /*
  113. * Have to be careful here. If we're in oneshot mode,
  114. * before we call tick_periodic() in a loop, we need
  115. * to be sure we're using a real hardware clocksource.
  116. * Otherwise we could get trapped in an infinite
  117. * loop, as the tick_periodic() increments jiffies,
  118. * which then will increment time, possibly causing
  119. * the loop to trigger again and again.
  120. */
  121. if (timekeeping_valid_for_hres())
  122. tick_periodic(cpu);
  123. }
  124. }
  125. /*
  126. * Setup the device for a periodic tick
  127. */
  128. void tick_setup_periodic(struct clock_event_device *dev, int broadcast)
  129. {
  130. tick_set_periodic_handler(dev, broadcast);
  131. /* Broadcast setup ? */
  132. if (!tick_device_is_functional(dev))
  133. return;
  134. if ((dev->features & CLOCK_EVT_FEAT_PERIODIC) &&
  135. !tick_broadcast_oneshot_active()) {
  136. clockevents_switch_state(dev, CLOCK_EVT_STATE_PERIODIC);
  137. } else {
  138. unsigned long seq;
  139. ktime_t next;
  140. do {
  141. seq = read_seqbegin(&jiffies_lock);
  142. next = tick_next_period;
  143. } while (read_seqretry(&jiffies_lock, seq));
  144. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  145. for (;;) {
  146. if (!clockevents_program_event(dev, next, false))
  147. return;
  148. next = ktime_add(next, tick_period);
  149. }
  150. }
  151. }
  152. /*
  153. * Setup the tick device
  154. */
  155. static void tick_setup_device(struct tick_device *td,
  156. struct clock_event_device *newdev, int cpu,
  157. const struct cpumask *cpumask)
  158. {
  159. void (*handler)(struct clock_event_device *) = NULL;
  160. ktime_t next_event = 0;
  161. /*
  162. * First device setup ?
  163. */
  164. if (!td->evtdev) {
  165. /*
  166. * If no cpu took the do_timer update, assign it to
  167. * this cpu:
  168. */
  169. if (tick_do_timer_cpu == TICK_DO_TIMER_BOOT) {
  170. if (!tick_nohz_full_cpu(cpu))
  171. tick_do_timer_cpu = cpu;
  172. else
  173. tick_do_timer_cpu = TICK_DO_TIMER_NONE;
  174. tick_next_period = ktime_get();
  175. tick_period = NSEC_PER_SEC / HZ;
  176. }
  177. /*
  178. * Startup in periodic mode first.
  179. */
  180. td->mode = TICKDEV_MODE_PERIODIC;
  181. } else {
  182. handler = td->evtdev->event_handler;
  183. next_event = td->evtdev->next_event;
  184. td->evtdev->event_handler = clockevents_handle_noop;
  185. }
  186. td->evtdev = newdev;
  187. /*
  188. * When the device is not per cpu, pin the interrupt to the
  189. * current cpu:
  190. */
  191. if (!cpumask_equal(newdev->cpumask, cpumask))
  192. irq_set_affinity(newdev->irq, cpumask);
  193. /*
  194. * When global broadcasting is active, check if the current
  195. * device is registered as a placeholder for broadcast mode.
  196. * This allows us to handle this x86 misfeature in a generic
  197. * way. This function also returns !=0 when we keep the
  198. * current active broadcast state for this CPU.
  199. */
  200. if (tick_device_uses_broadcast(newdev, cpu))
  201. return;
  202. if (td->mode == TICKDEV_MODE_PERIODIC)
  203. tick_setup_periodic(newdev, 0);
  204. else
  205. tick_setup_oneshot(newdev, handler, next_event);
  206. }
  207. void tick_install_replacement(struct clock_event_device *newdev)
  208. {
  209. struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
  210. int cpu = smp_processor_id();
  211. clockevents_exchange_device(td->evtdev, newdev);
  212. tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
  213. if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
  214. tick_oneshot_notify();
  215. }
  216. static bool tick_check_percpu(struct clock_event_device *curdev,
  217. struct clock_event_device *newdev, int cpu)
  218. {
  219. if (!cpumask_test_cpu(cpu, newdev->cpumask))
  220. return false;
  221. if (cpumask_equal(newdev->cpumask, cpumask_of(cpu)))
  222. return true;
  223. /* Check if irq affinity can be set */
  224. if (newdev->irq >= 0 && !irq_can_set_affinity(newdev->irq))
  225. return false;
  226. /* Prefer an existing cpu local device */
  227. if (curdev && cpumask_equal(curdev->cpumask, cpumask_of(cpu)))
  228. return false;
  229. return true;
  230. }
  231. static bool tick_check_preferred(struct clock_event_device *curdev,
  232. struct clock_event_device *newdev)
  233. {
  234. /* Prefer oneshot capable device */
  235. if (!(newdev->features & CLOCK_EVT_FEAT_ONESHOT)) {
  236. if (curdev && (curdev->features & CLOCK_EVT_FEAT_ONESHOT))
  237. return false;
  238. if (tick_oneshot_mode_active())
  239. return false;
  240. }
  241. /*
  242. * Use the higher rated one, but prefer a CPU local device with a lower
  243. * rating than a non-CPU local device
  244. */
  245. return !curdev ||
  246. newdev->rating > curdev->rating ||
  247. !cpumask_equal(curdev->cpumask, newdev->cpumask);
  248. }
  249. /*
  250. * Check whether the new device is a better fit than curdev. curdev
  251. * can be NULL !
  252. */
  253. bool tick_check_replacement(struct clock_event_device *curdev,
  254. struct clock_event_device *newdev)
  255. {
  256. if (!tick_check_percpu(curdev, newdev, smp_processor_id()))
  257. return false;
  258. return tick_check_preferred(curdev, newdev);
  259. }
  260. /*
  261. * Check, if the new registered device should be used. Called with
  262. * clockevents_lock held and interrupts disabled.
  263. */
  264. void tick_check_new_device(struct clock_event_device *newdev)
  265. {
  266. struct clock_event_device *curdev;
  267. struct tick_device *td;
  268. int cpu;
  269. cpu = smp_processor_id();
  270. td = &per_cpu(tick_cpu_device, cpu);
  271. curdev = td->evtdev;
  272. /* cpu local device ? */
  273. if (!tick_check_percpu(curdev, newdev, cpu))
  274. goto out_bc;
  275. /* Preference decision */
  276. if (!tick_check_preferred(curdev, newdev))
  277. goto out_bc;
  278. if (!try_module_get(newdev->owner))
  279. return;
  280. /*
  281. * Replace the eventually existing device by the new
  282. * device. If the current device is the broadcast device, do
  283. * not give it back to the clockevents layer !
  284. */
  285. if (tick_is_broadcast_device(curdev)) {
  286. clockevents_shutdown(curdev);
  287. curdev = NULL;
  288. }
  289. clockevents_exchange_device(curdev, newdev);
  290. tick_setup_device(td, newdev, cpu, cpumask_of(cpu));
  291. if (newdev->features & CLOCK_EVT_FEAT_ONESHOT)
  292. tick_oneshot_notify();
  293. return;
  294. out_bc:
  295. /*
  296. * Can the new device be used as a broadcast device ?
  297. */
  298. tick_install_broadcast_device(newdev);
  299. }
  300. /**
  301. * tick_broadcast_oneshot_control - Enter/exit broadcast oneshot mode
  302. * @state: The target state (enter/exit)
  303. *
  304. * The system enters/leaves a state, where affected devices might stop
  305. * Returns 0 on success, -EBUSY if the cpu is used to broadcast wakeups.
  306. *
  307. * Called with interrupts disabled, so clockevents_lock is not
  308. * required here because the local clock event device cannot go away
  309. * under us.
  310. */
  311. int tick_broadcast_oneshot_control(enum tick_broadcast_state state)
  312. {
  313. struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
  314. if (!(td->evtdev->features & CLOCK_EVT_FEAT_C3STOP))
  315. return 0;
  316. return __tick_broadcast_oneshot_control(state);
  317. }
  318. EXPORT_SYMBOL_GPL(tick_broadcast_oneshot_control);
  319. #ifdef CONFIG_HOTPLUG_CPU
  320. /*
  321. * Transfer the do_timer job away from a dying cpu.
  322. *
  323. * Called with interrupts disabled. Not locking required. If
  324. * tick_do_timer_cpu is owned by this cpu, nothing can change it.
  325. */
  326. void tick_handover_do_timer(void)
  327. {
  328. if (tick_do_timer_cpu == smp_processor_id()) {
  329. int cpu = cpumask_first(cpu_online_mask);
  330. tick_do_timer_cpu = (cpu < nr_cpu_ids) ? cpu :
  331. TICK_DO_TIMER_NONE;
  332. }
  333. }
  334. /*
  335. * Shutdown an event device on a given cpu:
  336. *
  337. * This is called on a life CPU, when a CPU is dead. So we cannot
  338. * access the hardware device itself.
  339. * We just set the mode and remove it from the lists.
  340. */
  341. void tick_shutdown(unsigned int cpu)
  342. {
  343. struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
  344. struct clock_event_device *dev = td->evtdev;
  345. td->mode = TICKDEV_MODE_PERIODIC;
  346. if (dev) {
  347. /*
  348. * Prevent that the clock events layer tries to call
  349. * the set mode function!
  350. */
  351. clockevent_set_state(dev, CLOCK_EVT_STATE_DETACHED);
  352. clockevents_exchange_device(dev, NULL);
  353. dev->event_handler = clockevents_handle_noop;
  354. td->evtdev = NULL;
  355. }
  356. }
  357. #endif
  358. /**
  359. * tick_suspend_local - Suspend the local tick device
  360. *
  361. * Called from the local cpu for freeze with interrupts disabled.
  362. *
  363. * No locks required. Nothing can change the per cpu device.
  364. */
  365. void tick_suspend_local(void)
  366. {
  367. struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
  368. clockevents_shutdown(td->evtdev);
  369. }
  370. /**
  371. * tick_resume_local - Resume the local tick device
  372. *
  373. * Called from the local CPU for unfreeze or XEN resume magic.
  374. *
  375. * No locks required. Nothing can change the per cpu device.
  376. */
  377. void tick_resume_local(void)
  378. {
  379. struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
  380. bool broadcast = tick_resume_check_broadcast();
  381. clockevents_tick_resume(td->evtdev);
  382. if (!broadcast) {
  383. if (td->mode == TICKDEV_MODE_PERIODIC)
  384. tick_setup_periodic(td->evtdev, 0);
  385. else
  386. tick_resume_oneshot();
  387. }
  388. }
  389. /**
  390. * tick_suspend - Suspend the tick and the broadcast device
  391. *
  392. * Called from syscore_suspend() via timekeeping_suspend with only one
  393. * CPU online and interrupts disabled or from tick_unfreeze() under
  394. * tick_freeze_lock.
  395. *
  396. * No locks required. Nothing can change the per cpu device.
  397. */
  398. void tick_suspend(void)
  399. {
  400. tick_suspend_local();
  401. tick_suspend_broadcast();
  402. }
  403. /**
  404. * tick_resume - Resume the tick and the broadcast device
  405. *
  406. * Called from syscore_resume() via timekeeping_resume with only one
  407. * CPU online and interrupts disabled.
  408. *
  409. * No locks required. Nothing can change the per cpu device.
  410. */
  411. void tick_resume(void)
  412. {
  413. tick_resume_broadcast();
  414. tick_resume_local();
  415. }
  416. #ifdef CONFIG_SUSPEND
  417. static DEFINE_RAW_SPINLOCK(tick_freeze_lock);
  418. static unsigned int tick_freeze_depth;
  419. /**
  420. * tick_freeze - Suspend the local tick and (possibly) timekeeping.
  421. *
  422. * Check if this is the last online CPU executing the function and if so,
  423. * suspend timekeeping. Otherwise suspend the local tick.
  424. *
  425. * Call with interrupts disabled. Must be balanced with %tick_unfreeze().
  426. * Interrupts must not be enabled before the subsequent %tick_unfreeze().
  427. */
  428. void tick_freeze(void)
  429. {
  430. raw_spin_lock(&tick_freeze_lock);
  431. tick_freeze_depth++;
  432. if (tick_freeze_depth == num_online_cpus()) {
  433. trace_suspend_resume(TPS("timekeeping_freeze"),
  434. smp_processor_id(), true);
  435. system_state = SYSTEM_SUSPEND;
  436. sched_clock_suspend();
  437. timekeeping_suspend();
  438. } else {
  439. tick_suspend_local();
  440. }
  441. raw_spin_unlock(&tick_freeze_lock);
  442. }
  443. /**
  444. * tick_unfreeze - Resume the local tick and (possibly) timekeeping.
  445. *
  446. * Check if this is the first CPU executing the function and if so, resume
  447. * timekeeping. Otherwise resume the local tick.
  448. *
  449. * Call with interrupts disabled. Must be balanced with %tick_freeze().
  450. * Interrupts must not be enabled after the preceding %tick_freeze().
  451. */
  452. void tick_unfreeze(void)
  453. {
  454. raw_spin_lock(&tick_freeze_lock);
  455. if (tick_freeze_depth == num_online_cpus()) {
  456. timekeeping_resume();
  457. sched_clock_resume();
  458. system_state = SYSTEM_RUNNING;
  459. trace_suspend_resume(TPS("timekeeping_freeze"),
  460. smp_processor_id(), false);
  461. } else {
  462. touch_softlockup_watchdog();
  463. tick_resume_local();
  464. }
  465. tick_freeze_depth--;
  466. raw_spin_unlock(&tick_freeze_lock);
  467. }
  468. #endif /* CONFIG_SUSPEND */
  469. /**
  470. * tick_init - initialize the tick control
  471. */
  472. void __init tick_init(void)
  473. {
  474. tick_broadcast_init();
  475. tick_nohz_init();
  476. }