tick-broadcast.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * linux/kernel/time/tick-broadcast.c
  3. *
  4. * This file contains functions which emulate a local clock-event
  5. * device via a broadcast event source.
  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/smp.h>
  22. #include <linux/module.h>
  23. #include "tick-internal.h"
  24. /*
  25. * Broadcast support for broken x86 hardware, where the local apic
  26. * timer stops in C3 state.
  27. */
  28. static struct tick_device tick_broadcast_device;
  29. static cpumask_var_t tick_broadcast_mask;
  30. static cpumask_var_t tmpmask;
  31. static DEFINE_RAW_SPINLOCK(tick_broadcast_lock);
  32. static int tick_broadcast_force;
  33. #ifdef CONFIG_TICK_ONESHOT
  34. static void tick_broadcast_clear_oneshot(int cpu);
  35. #else
  36. static inline void tick_broadcast_clear_oneshot(int cpu) { }
  37. #endif
  38. /*
  39. * Debugging: see timer_list.c
  40. */
  41. struct tick_device *tick_get_broadcast_device(void)
  42. {
  43. return &tick_broadcast_device;
  44. }
  45. struct cpumask *tick_get_broadcast_mask(void)
  46. {
  47. return tick_broadcast_mask;
  48. }
  49. /*
  50. * Start the device in periodic mode
  51. */
  52. static void tick_broadcast_start_periodic(struct clock_event_device *bc)
  53. {
  54. if (bc)
  55. tick_setup_periodic(bc, 1);
  56. }
  57. /*
  58. * Check, if the device can be utilized as broadcast device:
  59. */
  60. static bool tick_check_broadcast_device(struct clock_event_device *curdev,
  61. struct clock_event_device *newdev)
  62. {
  63. if ((newdev->features & CLOCK_EVT_FEAT_DUMMY) ||
  64. (newdev->features & CLOCK_EVT_FEAT_C3STOP))
  65. return false;
  66. if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT &&
  67. !(newdev->features & CLOCK_EVT_FEAT_ONESHOT))
  68. return false;
  69. return !curdev || newdev->rating > curdev->rating;
  70. }
  71. /*
  72. * Conditionally install/replace broadcast device
  73. */
  74. void tick_install_broadcast_device(struct clock_event_device *dev)
  75. {
  76. struct clock_event_device *cur = tick_broadcast_device.evtdev;
  77. if (!tick_check_broadcast_device(cur, dev))
  78. return;
  79. if (!try_module_get(dev->owner))
  80. return;
  81. clockevents_exchange_device(cur, dev);
  82. if (cur)
  83. cur->event_handler = clockevents_handle_noop;
  84. tick_broadcast_device.evtdev = dev;
  85. if (!cpumask_empty(tick_broadcast_mask))
  86. tick_broadcast_start_periodic(dev);
  87. /*
  88. * Inform all cpus about this. We might be in a situation
  89. * where we did not switch to oneshot mode because the per cpu
  90. * devices are affected by CLOCK_EVT_FEAT_C3STOP and the lack
  91. * of a oneshot capable broadcast device. Without that
  92. * notification the systems stays stuck in periodic mode
  93. * forever.
  94. */
  95. if (dev->features & CLOCK_EVT_FEAT_ONESHOT)
  96. tick_clock_notify();
  97. }
  98. /*
  99. * Check, if the device is the broadcast device
  100. */
  101. int tick_is_broadcast_device(struct clock_event_device *dev)
  102. {
  103. return (dev && tick_broadcast_device.evtdev == dev);
  104. }
  105. static void err_broadcast(const struct cpumask *mask)
  106. {
  107. pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
  108. }
  109. static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
  110. {
  111. if (!dev->broadcast)
  112. dev->broadcast = tick_broadcast;
  113. if (!dev->broadcast) {
  114. pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
  115. dev->name);
  116. dev->broadcast = err_broadcast;
  117. }
  118. }
  119. /*
  120. * Check, if the device is disfunctional and a place holder, which
  121. * needs to be handled by the broadcast device.
  122. */
  123. int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
  124. {
  125. unsigned long flags;
  126. int ret = 0;
  127. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  128. /*
  129. * Devices might be registered with both periodic and oneshot
  130. * mode disabled. This signals, that the device needs to be
  131. * operated from the broadcast device and is a placeholder for
  132. * the cpu local device.
  133. */
  134. if (!tick_device_is_functional(dev)) {
  135. dev->event_handler = tick_handle_periodic;
  136. tick_device_setup_broadcast_func(dev);
  137. cpumask_set_cpu(cpu, tick_broadcast_mask);
  138. tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
  139. ret = 1;
  140. } else {
  141. /*
  142. * When the new device is not affected by the stop
  143. * feature and the cpu is marked in the broadcast mask
  144. * then clear the broadcast bit.
  145. */
  146. if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
  147. int cpu = smp_processor_id();
  148. cpumask_clear_cpu(cpu, tick_broadcast_mask);
  149. tick_broadcast_clear_oneshot(cpu);
  150. } else {
  151. tick_device_setup_broadcast_func(dev);
  152. }
  153. }
  154. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  155. return ret;
  156. }
  157. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  158. int tick_receive_broadcast(void)
  159. {
  160. struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
  161. struct clock_event_device *evt = td->evtdev;
  162. if (!evt)
  163. return -ENODEV;
  164. if (!evt->event_handler)
  165. return -EINVAL;
  166. evt->event_handler(evt);
  167. return 0;
  168. }
  169. #endif
  170. /*
  171. * Broadcast the event to the cpus, which are set in the mask (mangled).
  172. */
  173. static void tick_do_broadcast(struct cpumask *mask)
  174. {
  175. int cpu = smp_processor_id();
  176. struct tick_device *td;
  177. /*
  178. * Check, if the current cpu is in the mask
  179. */
  180. if (cpumask_test_cpu(cpu, mask)) {
  181. cpumask_clear_cpu(cpu, mask);
  182. td = &per_cpu(tick_cpu_device, cpu);
  183. td->evtdev->event_handler(td->evtdev);
  184. }
  185. if (!cpumask_empty(mask)) {
  186. /*
  187. * It might be necessary to actually check whether the devices
  188. * have different broadcast functions. For now, just use the
  189. * one of the first device. This works as long as we have this
  190. * misfeature only on x86 (lapic)
  191. */
  192. td = &per_cpu(tick_cpu_device, cpumask_first(mask));
  193. td->evtdev->broadcast(mask);
  194. }
  195. }
  196. /*
  197. * Periodic broadcast:
  198. * - invoke the broadcast handlers
  199. */
  200. static void tick_do_periodic_broadcast(void)
  201. {
  202. raw_spin_lock(&tick_broadcast_lock);
  203. cpumask_and(tmpmask, cpu_online_mask, tick_broadcast_mask);
  204. tick_do_broadcast(tmpmask);
  205. raw_spin_unlock(&tick_broadcast_lock);
  206. }
  207. /*
  208. * Event handler for periodic broadcast ticks
  209. */
  210. static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
  211. {
  212. ktime_t next;
  213. tick_do_periodic_broadcast();
  214. /*
  215. * The device is in periodic mode. No reprogramming necessary:
  216. */
  217. if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
  218. return;
  219. /*
  220. * Setup the next period for devices, which do not have
  221. * periodic mode. We read dev->next_event first and add to it
  222. * when the event already expired. clockevents_program_event()
  223. * sets dev->next_event only when the event is really
  224. * programmed to the device.
  225. */
  226. for (next = dev->next_event; ;) {
  227. next = ktime_add(next, tick_period);
  228. if (!clockevents_program_event(dev, next, false))
  229. return;
  230. tick_do_periodic_broadcast();
  231. }
  232. }
  233. /*
  234. * Powerstate information: The system enters/leaves a state, where
  235. * affected devices might stop
  236. */
  237. static void tick_do_broadcast_on_off(unsigned long *reason)
  238. {
  239. struct clock_event_device *bc, *dev;
  240. struct tick_device *td;
  241. unsigned long flags;
  242. int cpu, bc_stopped;
  243. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  244. cpu = smp_processor_id();
  245. td = &per_cpu(tick_cpu_device, cpu);
  246. dev = td->evtdev;
  247. bc = tick_broadcast_device.evtdev;
  248. /*
  249. * Is the device not affected by the powerstate ?
  250. */
  251. if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
  252. goto out;
  253. if (!tick_device_is_functional(dev))
  254. goto out;
  255. bc_stopped = cpumask_empty(tick_broadcast_mask);
  256. switch (*reason) {
  257. case CLOCK_EVT_NOTIFY_BROADCAST_ON:
  258. case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
  259. if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) {
  260. if (tick_broadcast_device.mode ==
  261. TICKDEV_MODE_PERIODIC)
  262. clockevents_shutdown(dev);
  263. }
  264. if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
  265. tick_broadcast_force = 1;
  266. break;
  267. case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
  268. if (!tick_broadcast_force &&
  269. cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) {
  270. if (tick_broadcast_device.mode ==
  271. TICKDEV_MODE_PERIODIC)
  272. tick_setup_periodic(dev, 0);
  273. }
  274. break;
  275. }
  276. if (cpumask_empty(tick_broadcast_mask)) {
  277. if (!bc_stopped)
  278. clockevents_shutdown(bc);
  279. } else if (bc_stopped) {
  280. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
  281. tick_broadcast_start_periodic(bc);
  282. else
  283. tick_broadcast_setup_oneshot(bc);
  284. }
  285. out:
  286. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  287. }
  288. /*
  289. * Powerstate information: The system enters/leaves a state, where
  290. * affected devices might stop.
  291. */
  292. void tick_broadcast_on_off(unsigned long reason, int *oncpu)
  293. {
  294. if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
  295. printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
  296. "offline CPU #%d\n", *oncpu);
  297. else
  298. tick_do_broadcast_on_off(&reason);
  299. }
  300. /*
  301. * Set the periodic handler depending on broadcast on/off
  302. */
  303. void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
  304. {
  305. if (!broadcast)
  306. dev->event_handler = tick_handle_periodic;
  307. else
  308. dev->event_handler = tick_handle_periodic_broadcast;
  309. }
  310. /*
  311. * Remove a CPU from broadcasting
  312. */
  313. void tick_shutdown_broadcast(unsigned int *cpup)
  314. {
  315. struct clock_event_device *bc;
  316. unsigned long flags;
  317. unsigned int cpu = *cpup;
  318. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  319. bc = tick_broadcast_device.evtdev;
  320. cpumask_clear_cpu(cpu, tick_broadcast_mask);
  321. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
  322. if (bc && cpumask_empty(tick_broadcast_mask))
  323. clockevents_shutdown(bc);
  324. }
  325. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  326. }
  327. void tick_suspend_broadcast(void)
  328. {
  329. struct clock_event_device *bc;
  330. unsigned long flags;
  331. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  332. bc = tick_broadcast_device.evtdev;
  333. if (bc)
  334. clockevents_shutdown(bc);
  335. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  336. }
  337. int tick_resume_broadcast(void)
  338. {
  339. struct clock_event_device *bc;
  340. unsigned long flags;
  341. int broadcast = 0;
  342. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  343. bc = tick_broadcast_device.evtdev;
  344. if (bc) {
  345. clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
  346. switch (tick_broadcast_device.mode) {
  347. case TICKDEV_MODE_PERIODIC:
  348. if (!cpumask_empty(tick_broadcast_mask))
  349. tick_broadcast_start_periodic(bc);
  350. broadcast = cpumask_test_cpu(smp_processor_id(),
  351. tick_broadcast_mask);
  352. break;
  353. case TICKDEV_MODE_ONESHOT:
  354. if (!cpumask_empty(tick_broadcast_mask))
  355. broadcast = tick_resume_broadcast_oneshot(bc);
  356. break;
  357. }
  358. }
  359. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  360. return broadcast;
  361. }
  362. #ifdef CONFIG_TICK_ONESHOT
  363. static cpumask_var_t tick_broadcast_oneshot_mask;
  364. static cpumask_var_t tick_broadcast_pending_mask;
  365. static cpumask_var_t tick_broadcast_force_mask;
  366. /*
  367. * Exposed for debugging: see timer_list.c
  368. */
  369. struct cpumask *tick_get_broadcast_oneshot_mask(void)
  370. {
  371. return tick_broadcast_oneshot_mask;
  372. }
  373. /*
  374. * Called before going idle with interrupts disabled. Checks whether a
  375. * broadcast event from the other core is about to happen. We detected
  376. * that in tick_broadcast_oneshot_control(). The callsite can use this
  377. * to avoid a deep idle transition as we are about to get the
  378. * broadcast IPI right away.
  379. */
  380. int tick_check_broadcast_expired(void)
  381. {
  382. return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
  383. }
  384. /*
  385. * Set broadcast interrupt affinity
  386. */
  387. static void tick_broadcast_set_affinity(struct clock_event_device *bc,
  388. const struct cpumask *cpumask)
  389. {
  390. if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
  391. return;
  392. if (cpumask_equal(bc->cpumask, cpumask))
  393. return;
  394. bc->cpumask = cpumask;
  395. irq_set_affinity(bc->irq, bc->cpumask);
  396. }
  397. static int tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
  398. ktime_t expires, int force)
  399. {
  400. int ret;
  401. if (bc->mode != CLOCK_EVT_MODE_ONESHOT)
  402. clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
  403. ret = clockevents_program_event(bc, expires, force);
  404. if (!ret)
  405. tick_broadcast_set_affinity(bc, cpumask_of(cpu));
  406. return ret;
  407. }
  408. int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
  409. {
  410. clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
  411. return 0;
  412. }
  413. /*
  414. * Called from irq_enter() when idle was interrupted to reenable the
  415. * per cpu device.
  416. */
  417. void tick_check_oneshot_broadcast(int cpu)
  418. {
  419. if (cpumask_test_cpu(cpu, tick_broadcast_oneshot_mask)) {
  420. struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
  421. /*
  422. * We might be in the middle of switching over from
  423. * periodic to oneshot. If the CPU has not yet
  424. * switched over, leave the device alone.
  425. */
  426. if (td->mode == TICKDEV_MODE_ONESHOT) {
  427. clockevents_set_mode(td->evtdev,
  428. CLOCK_EVT_MODE_ONESHOT);
  429. }
  430. }
  431. }
  432. /*
  433. * Handle oneshot mode broadcasting
  434. */
  435. static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
  436. {
  437. struct tick_device *td;
  438. ktime_t now, next_event;
  439. int cpu, next_cpu = 0;
  440. raw_spin_lock(&tick_broadcast_lock);
  441. again:
  442. dev->next_event.tv64 = KTIME_MAX;
  443. next_event.tv64 = KTIME_MAX;
  444. cpumask_clear(tmpmask);
  445. now = ktime_get();
  446. /* Find all expired events */
  447. for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
  448. td = &per_cpu(tick_cpu_device, cpu);
  449. if (td->evtdev->next_event.tv64 <= now.tv64) {
  450. cpumask_set_cpu(cpu, tmpmask);
  451. /*
  452. * Mark the remote cpu in the pending mask, so
  453. * it can avoid reprogramming the cpu local
  454. * timer in tick_broadcast_oneshot_control().
  455. */
  456. cpumask_set_cpu(cpu, tick_broadcast_pending_mask);
  457. } else if (td->evtdev->next_event.tv64 < next_event.tv64) {
  458. next_event.tv64 = td->evtdev->next_event.tv64;
  459. next_cpu = cpu;
  460. }
  461. }
  462. /* Take care of enforced broadcast requests */
  463. cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
  464. cpumask_clear(tick_broadcast_force_mask);
  465. /*
  466. * Sanity check. Catch the case where we try to broadcast to
  467. * offline cpus.
  468. */
  469. if (WARN_ON_ONCE(!cpumask_subset(tmpmask, cpu_online_mask)))
  470. cpumask_and(tmpmask, tmpmask, cpu_online_mask);
  471. /*
  472. * Wakeup the cpus which have an expired event.
  473. */
  474. tick_do_broadcast(tmpmask);
  475. /*
  476. * Two reasons for reprogram:
  477. *
  478. * - The global event did not expire any CPU local
  479. * events. This happens in dyntick mode, as the maximum PIT
  480. * delta is quite small.
  481. *
  482. * - There are pending events on sleeping CPUs which were not
  483. * in the event mask
  484. */
  485. if (next_event.tv64 != KTIME_MAX) {
  486. /*
  487. * Rearm the broadcast device. If event expired,
  488. * repeat the above
  489. */
  490. if (tick_broadcast_set_event(dev, next_cpu, next_event, 0))
  491. goto again;
  492. }
  493. raw_spin_unlock(&tick_broadcast_lock);
  494. }
  495. /*
  496. * Powerstate information: The system enters/leaves a state, where
  497. * affected devices might stop
  498. */
  499. void tick_broadcast_oneshot_control(unsigned long reason)
  500. {
  501. struct clock_event_device *bc, *dev;
  502. struct tick_device *td;
  503. unsigned long flags;
  504. ktime_t now;
  505. int cpu;
  506. /*
  507. * Periodic mode does not care about the enter/exit of power
  508. * states
  509. */
  510. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
  511. return;
  512. /*
  513. * We are called with preemtion disabled from the depth of the
  514. * idle code, so we can't be moved away.
  515. */
  516. cpu = smp_processor_id();
  517. td = &per_cpu(tick_cpu_device, cpu);
  518. dev = td->evtdev;
  519. if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
  520. return;
  521. bc = tick_broadcast_device.evtdev;
  522. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  523. if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
  524. WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
  525. if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
  526. clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
  527. /*
  528. * We only reprogram the broadcast timer if we
  529. * did not mark ourself in the force mask and
  530. * if the cpu local event is earlier than the
  531. * broadcast event. If the current CPU is in
  532. * the force mask, then we are going to be
  533. * woken by the IPI right away.
  534. */
  535. if (!cpumask_test_cpu(cpu, tick_broadcast_force_mask) &&
  536. dev->next_event.tv64 < bc->next_event.tv64)
  537. tick_broadcast_set_event(bc, cpu, dev->next_event, 1);
  538. }
  539. } else {
  540. if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
  541. clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
  542. if (dev->next_event.tv64 == KTIME_MAX)
  543. goto out;
  544. /*
  545. * The cpu which was handling the broadcast
  546. * timer marked this cpu in the broadcast
  547. * pending mask and fired the broadcast
  548. * IPI. So we are going to handle the expired
  549. * event anyway via the broadcast IPI
  550. * handler. No need to reprogram the timer
  551. * with an already expired event.
  552. */
  553. if (cpumask_test_and_clear_cpu(cpu,
  554. tick_broadcast_pending_mask))
  555. goto out;
  556. /*
  557. * If the pending bit is not set, then we are
  558. * either the CPU handling the broadcast
  559. * interrupt or we got woken by something else.
  560. *
  561. * We are not longer in the broadcast mask, so
  562. * if the cpu local expiry time is already
  563. * reached, we would reprogram the cpu local
  564. * timer with an already expired event.
  565. *
  566. * This can lead to a ping-pong when we return
  567. * to idle and therefor rearm the broadcast
  568. * timer before the cpu local timer was able
  569. * to fire. This happens because the forced
  570. * reprogramming makes sure that the event
  571. * will happen in the future and depending on
  572. * the min_delta setting this might be far
  573. * enough out that the ping-pong starts.
  574. *
  575. * If the cpu local next_event has expired
  576. * then we know that the broadcast timer
  577. * next_event has expired as well and
  578. * broadcast is about to be handled. So we
  579. * avoid reprogramming and enforce that the
  580. * broadcast handler, which did not run yet,
  581. * will invoke the cpu local handler.
  582. *
  583. * We cannot call the handler directly from
  584. * here, because we might be in a NOHZ phase
  585. * and we did not go through the irq_enter()
  586. * nohz fixups.
  587. */
  588. now = ktime_get();
  589. if (dev->next_event.tv64 <= now.tv64) {
  590. cpumask_set_cpu(cpu, tick_broadcast_force_mask);
  591. goto out;
  592. }
  593. /*
  594. * We got woken by something else. Reprogram
  595. * the cpu local timer device.
  596. */
  597. tick_program_event(dev->next_event, 1);
  598. }
  599. }
  600. out:
  601. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  602. }
  603. /*
  604. * Reset the one shot broadcast for a cpu
  605. *
  606. * Called with tick_broadcast_lock held
  607. */
  608. static void tick_broadcast_clear_oneshot(int cpu)
  609. {
  610. cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
  611. }
  612. static void tick_broadcast_init_next_event(struct cpumask *mask,
  613. ktime_t expires)
  614. {
  615. struct tick_device *td;
  616. int cpu;
  617. for_each_cpu(cpu, mask) {
  618. td = &per_cpu(tick_cpu_device, cpu);
  619. if (td->evtdev)
  620. td->evtdev->next_event = expires;
  621. }
  622. }
  623. /**
  624. * tick_broadcast_setup_oneshot - setup the broadcast device
  625. */
  626. void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
  627. {
  628. int cpu = smp_processor_id();
  629. /* Set it up only once ! */
  630. if (bc->event_handler != tick_handle_oneshot_broadcast) {
  631. int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
  632. bc->event_handler = tick_handle_oneshot_broadcast;
  633. /* Take the do_timer update */
  634. if (!tick_nohz_full_cpu(cpu))
  635. tick_do_timer_cpu = cpu;
  636. /*
  637. * We must be careful here. There might be other CPUs
  638. * waiting for periodic broadcast. We need to set the
  639. * oneshot_mask bits for those and program the
  640. * broadcast device to fire.
  641. */
  642. cpumask_copy(tmpmask, tick_broadcast_mask);
  643. cpumask_clear_cpu(cpu, tmpmask);
  644. cpumask_or(tick_broadcast_oneshot_mask,
  645. tick_broadcast_oneshot_mask, tmpmask);
  646. if (was_periodic && !cpumask_empty(tmpmask)) {
  647. clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
  648. tick_broadcast_init_next_event(tmpmask,
  649. tick_next_period);
  650. tick_broadcast_set_event(bc, cpu, tick_next_period, 1);
  651. } else
  652. bc->next_event.tv64 = KTIME_MAX;
  653. } else {
  654. /*
  655. * The first cpu which switches to oneshot mode sets
  656. * the bit for all other cpus which are in the general
  657. * (periodic) broadcast mask. So the bit is set and
  658. * would prevent the first broadcast enter after this
  659. * to program the bc device.
  660. */
  661. tick_broadcast_clear_oneshot(cpu);
  662. }
  663. }
  664. /*
  665. * Select oneshot operating mode for the broadcast device
  666. */
  667. void tick_broadcast_switch_to_oneshot(void)
  668. {
  669. struct clock_event_device *bc;
  670. unsigned long flags;
  671. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  672. tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
  673. bc = tick_broadcast_device.evtdev;
  674. if (bc)
  675. tick_broadcast_setup_oneshot(bc);
  676. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  677. }
  678. /*
  679. * Remove a dead CPU from broadcasting
  680. */
  681. void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
  682. {
  683. unsigned long flags;
  684. unsigned int cpu = *cpup;
  685. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  686. /*
  687. * Clear the broadcast masks for the dead cpu, but do not stop
  688. * the broadcast device!
  689. */
  690. cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
  691. cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
  692. cpumask_clear_cpu(cpu, tick_broadcast_force_mask);
  693. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  694. }
  695. /*
  696. * Check, whether the broadcast device is in one shot mode
  697. */
  698. int tick_broadcast_oneshot_active(void)
  699. {
  700. return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
  701. }
  702. /*
  703. * Check whether the broadcast device supports oneshot.
  704. */
  705. bool tick_broadcast_oneshot_available(void)
  706. {
  707. struct clock_event_device *bc = tick_broadcast_device.evtdev;
  708. return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
  709. }
  710. #endif
  711. void __init tick_broadcast_init(void)
  712. {
  713. zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
  714. zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
  715. #ifdef CONFIG_TICK_ONESHOT
  716. zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
  717. zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
  718. zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);
  719. #endif
  720. }