tick-broadcast.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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 tick_broadcast_on;
  31. static cpumask_var_t tmpmask;
  32. static DEFINE_RAW_SPINLOCK(tick_broadcast_lock);
  33. static int tick_broadcast_forced;
  34. #ifdef CONFIG_TICK_ONESHOT
  35. static void tick_broadcast_clear_oneshot(int cpu);
  36. static void tick_resume_broadcast_oneshot(struct clock_event_device *bc);
  37. #else
  38. static inline void tick_broadcast_clear_oneshot(int cpu) { }
  39. static inline void tick_resume_broadcast_oneshot(struct clock_event_device *bc) { }
  40. #endif
  41. /*
  42. * Debugging: see timer_list.c
  43. */
  44. struct tick_device *tick_get_broadcast_device(void)
  45. {
  46. return &tick_broadcast_device;
  47. }
  48. struct cpumask *tick_get_broadcast_mask(void)
  49. {
  50. return tick_broadcast_mask;
  51. }
  52. /*
  53. * Start the device in periodic mode
  54. */
  55. static void tick_broadcast_start_periodic(struct clock_event_device *bc)
  56. {
  57. if (bc)
  58. tick_setup_periodic(bc, 1);
  59. }
  60. /*
  61. * Check, if the device can be utilized as broadcast device:
  62. */
  63. static bool tick_check_broadcast_device(struct clock_event_device *curdev,
  64. struct clock_event_device *newdev)
  65. {
  66. if ((newdev->features & CLOCK_EVT_FEAT_DUMMY) ||
  67. (newdev->features & CLOCK_EVT_FEAT_PERCPU) ||
  68. (newdev->features & CLOCK_EVT_FEAT_C3STOP))
  69. return false;
  70. if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT &&
  71. !(newdev->features & CLOCK_EVT_FEAT_ONESHOT))
  72. return false;
  73. return !curdev || newdev->rating > curdev->rating;
  74. }
  75. /*
  76. * Conditionally install/replace broadcast device
  77. */
  78. void tick_install_broadcast_device(struct clock_event_device *dev)
  79. {
  80. struct clock_event_device *cur = tick_broadcast_device.evtdev;
  81. if (!tick_check_broadcast_device(cur, dev))
  82. return;
  83. if (!try_module_get(dev->owner))
  84. return;
  85. clockevents_exchange_device(cur, dev);
  86. if (cur)
  87. cur->event_handler = clockevents_handle_noop;
  88. tick_broadcast_device.evtdev = dev;
  89. if (!cpumask_empty(tick_broadcast_mask))
  90. tick_broadcast_start_periodic(dev);
  91. /*
  92. * Inform all cpus about this. We might be in a situation
  93. * where we did not switch to oneshot mode because the per cpu
  94. * devices are affected by CLOCK_EVT_FEAT_C3STOP and the lack
  95. * of a oneshot capable broadcast device. Without that
  96. * notification the systems stays stuck in periodic mode
  97. * forever.
  98. */
  99. if (dev->features & CLOCK_EVT_FEAT_ONESHOT)
  100. tick_clock_notify();
  101. }
  102. /*
  103. * Check, if the device is the broadcast device
  104. */
  105. int tick_is_broadcast_device(struct clock_event_device *dev)
  106. {
  107. return (dev && tick_broadcast_device.evtdev == dev);
  108. }
  109. int tick_broadcast_update_freq(struct clock_event_device *dev, u32 freq)
  110. {
  111. int ret = -ENODEV;
  112. if (tick_is_broadcast_device(dev)) {
  113. raw_spin_lock(&tick_broadcast_lock);
  114. ret = __clockevents_update_freq(dev, freq);
  115. raw_spin_unlock(&tick_broadcast_lock);
  116. }
  117. return ret;
  118. }
  119. static void err_broadcast(const struct cpumask *mask)
  120. {
  121. pr_crit_once("Failed to broadcast timer tick. Some CPUs may be unresponsive.\n");
  122. }
  123. static void tick_device_setup_broadcast_func(struct clock_event_device *dev)
  124. {
  125. if (!dev->broadcast)
  126. dev->broadcast = tick_broadcast;
  127. if (!dev->broadcast) {
  128. pr_warn_once("%s depends on broadcast, but no broadcast function available\n",
  129. dev->name);
  130. dev->broadcast = err_broadcast;
  131. }
  132. }
  133. /*
  134. * Check, if the device is disfunctional and a place holder, which
  135. * needs to be handled by the broadcast device.
  136. */
  137. int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
  138. {
  139. struct clock_event_device *bc = tick_broadcast_device.evtdev;
  140. unsigned long flags;
  141. int ret = 0;
  142. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  143. /*
  144. * Devices might be registered with both periodic and oneshot
  145. * mode disabled. This signals, that the device needs to be
  146. * operated from the broadcast device and is a placeholder for
  147. * the cpu local device.
  148. */
  149. if (!tick_device_is_functional(dev)) {
  150. dev->event_handler = tick_handle_periodic;
  151. tick_device_setup_broadcast_func(dev);
  152. cpumask_set_cpu(cpu, tick_broadcast_mask);
  153. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
  154. tick_broadcast_start_periodic(bc);
  155. else
  156. tick_broadcast_setup_oneshot(bc);
  157. ret = 1;
  158. } else {
  159. /*
  160. * Clear the broadcast bit for this cpu if the
  161. * device is not power state affected.
  162. */
  163. if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
  164. cpumask_clear_cpu(cpu, tick_broadcast_mask);
  165. else
  166. tick_device_setup_broadcast_func(dev);
  167. /*
  168. * Clear the broadcast bit if the CPU is not in
  169. * periodic broadcast on state.
  170. */
  171. if (!cpumask_test_cpu(cpu, tick_broadcast_on))
  172. cpumask_clear_cpu(cpu, tick_broadcast_mask);
  173. switch (tick_broadcast_device.mode) {
  174. case TICKDEV_MODE_ONESHOT:
  175. /*
  176. * If the system is in oneshot mode we can
  177. * unconditionally clear the oneshot mask bit,
  178. * because the CPU is running and therefore
  179. * not in an idle state which causes the power
  180. * state affected device to stop. Let the
  181. * caller initialize the device.
  182. */
  183. tick_broadcast_clear_oneshot(cpu);
  184. ret = 0;
  185. break;
  186. case TICKDEV_MODE_PERIODIC:
  187. /*
  188. * If the system is in periodic mode, check
  189. * whether the broadcast device can be
  190. * switched off now.
  191. */
  192. if (cpumask_empty(tick_broadcast_mask) && bc)
  193. clockevents_shutdown(bc);
  194. /*
  195. * If we kept the cpu in the broadcast mask,
  196. * tell the caller to leave the per cpu device
  197. * in shutdown state. The periodic interrupt
  198. * is delivered by the broadcast device, if
  199. * the broadcast device exists and is not
  200. * hrtimer based.
  201. */
  202. if (bc && !(bc->features & CLOCK_EVT_FEAT_HRTIMER))
  203. ret = cpumask_test_cpu(cpu, tick_broadcast_mask);
  204. break;
  205. default:
  206. break;
  207. }
  208. }
  209. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  210. return ret;
  211. }
  212. #ifdef CONFIG_GENERIC_CLOCKEVENTS_BROADCAST
  213. int tick_receive_broadcast(void)
  214. {
  215. struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
  216. struct clock_event_device *evt = td->evtdev;
  217. if (!evt)
  218. return -ENODEV;
  219. if (!evt->event_handler)
  220. return -EINVAL;
  221. evt->event_handler(evt);
  222. return 0;
  223. }
  224. #endif
  225. /*
  226. * Broadcast the event to the cpus, which are set in the mask (mangled).
  227. */
  228. static bool tick_do_broadcast(struct cpumask *mask)
  229. {
  230. int cpu = smp_processor_id();
  231. struct tick_device *td;
  232. bool local = false;
  233. /*
  234. * Check, if the current cpu is in the mask
  235. */
  236. if (cpumask_test_cpu(cpu, mask)) {
  237. struct clock_event_device *bc = tick_broadcast_device.evtdev;
  238. cpumask_clear_cpu(cpu, mask);
  239. /*
  240. * We only run the local handler, if the broadcast
  241. * device is not hrtimer based. Otherwise we run into
  242. * a hrtimer recursion.
  243. *
  244. * local timer_interrupt()
  245. * local_handler()
  246. * expire_hrtimers()
  247. * bc_handler()
  248. * local_handler()
  249. * expire_hrtimers()
  250. */
  251. local = !(bc->features & CLOCK_EVT_FEAT_HRTIMER);
  252. }
  253. if (!cpumask_empty(mask)) {
  254. /*
  255. * It might be necessary to actually check whether the devices
  256. * have different broadcast functions. For now, just use the
  257. * one of the first device. This works as long as we have this
  258. * misfeature only on x86 (lapic)
  259. */
  260. td = &per_cpu(tick_cpu_device, cpumask_first(mask));
  261. td->evtdev->broadcast(mask);
  262. }
  263. return local;
  264. }
  265. /*
  266. * Periodic broadcast:
  267. * - invoke the broadcast handlers
  268. */
  269. static bool tick_do_periodic_broadcast(void)
  270. {
  271. cpumask_and(tmpmask, cpu_online_mask, tick_broadcast_mask);
  272. return tick_do_broadcast(tmpmask);
  273. }
  274. /*
  275. * Event handler for periodic broadcast ticks
  276. */
  277. static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
  278. {
  279. struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
  280. bool bc_local;
  281. raw_spin_lock(&tick_broadcast_lock);
  282. /* Handle spurious interrupts gracefully */
  283. if (clockevent_state_shutdown(tick_broadcast_device.evtdev)) {
  284. raw_spin_unlock(&tick_broadcast_lock);
  285. return;
  286. }
  287. bc_local = tick_do_periodic_broadcast();
  288. if (clockevent_state_oneshot(dev)) {
  289. ktime_t next = ktime_add(dev->next_event, tick_period);
  290. clockevents_program_event(dev, next, true);
  291. }
  292. raw_spin_unlock(&tick_broadcast_lock);
  293. /*
  294. * We run the handler of the local cpu after dropping
  295. * tick_broadcast_lock because the handler might deadlock when
  296. * trying to switch to oneshot mode.
  297. */
  298. if (bc_local)
  299. td->evtdev->event_handler(td->evtdev);
  300. }
  301. /**
  302. * tick_broadcast_control - Enable/disable or force broadcast mode
  303. * @mode: The selected broadcast mode
  304. *
  305. * Called when the system enters a state where affected tick devices
  306. * might stop. Note: TICK_BROADCAST_FORCE cannot be undone.
  307. *
  308. * Called with interrupts disabled, so clockevents_lock is not
  309. * required here because the local clock event device cannot go away
  310. * under us.
  311. */
  312. void tick_broadcast_control(enum tick_broadcast_mode mode)
  313. {
  314. struct clock_event_device *bc, *dev;
  315. struct tick_device *td;
  316. int cpu, bc_stopped;
  317. td = this_cpu_ptr(&tick_cpu_device);
  318. dev = td->evtdev;
  319. /*
  320. * Is the device not affected by the powerstate ?
  321. */
  322. if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
  323. return;
  324. if (!tick_device_is_functional(dev))
  325. return;
  326. raw_spin_lock(&tick_broadcast_lock);
  327. cpu = smp_processor_id();
  328. bc = tick_broadcast_device.evtdev;
  329. bc_stopped = cpumask_empty(tick_broadcast_mask);
  330. switch (mode) {
  331. case TICK_BROADCAST_FORCE:
  332. tick_broadcast_forced = 1;
  333. case TICK_BROADCAST_ON:
  334. cpumask_set_cpu(cpu, tick_broadcast_on);
  335. if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) {
  336. /*
  337. * Only shutdown the cpu local device, if:
  338. *
  339. * - the broadcast device exists
  340. * - the broadcast device is not a hrtimer based one
  341. * - the broadcast device is in periodic mode to
  342. * avoid a hickup during switch to oneshot mode
  343. */
  344. if (bc && !(bc->features & CLOCK_EVT_FEAT_HRTIMER) &&
  345. tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
  346. clockevents_shutdown(dev);
  347. }
  348. break;
  349. case TICK_BROADCAST_OFF:
  350. if (tick_broadcast_forced)
  351. break;
  352. cpumask_clear_cpu(cpu, tick_broadcast_on);
  353. if (!tick_device_is_functional(dev))
  354. break;
  355. if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) {
  356. if (tick_broadcast_device.mode ==
  357. TICKDEV_MODE_PERIODIC)
  358. tick_setup_periodic(dev, 0);
  359. }
  360. break;
  361. }
  362. if (bc) {
  363. if (cpumask_empty(tick_broadcast_mask)) {
  364. if (!bc_stopped)
  365. clockevents_shutdown(bc);
  366. } else if (bc_stopped) {
  367. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
  368. tick_broadcast_start_periodic(bc);
  369. else
  370. tick_broadcast_setup_oneshot(bc);
  371. }
  372. }
  373. raw_spin_unlock(&tick_broadcast_lock);
  374. }
  375. EXPORT_SYMBOL_GPL(tick_broadcast_control);
  376. /*
  377. * Set the periodic handler depending on broadcast on/off
  378. */
  379. void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
  380. {
  381. if (!broadcast)
  382. dev->event_handler = tick_handle_periodic;
  383. else
  384. dev->event_handler = tick_handle_periodic_broadcast;
  385. }
  386. #ifdef CONFIG_HOTPLUG_CPU
  387. /*
  388. * Remove a CPU from broadcasting
  389. */
  390. void tick_shutdown_broadcast(unsigned int cpu)
  391. {
  392. struct clock_event_device *bc;
  393. unsigned long flags;
  394. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  395. bc = tick_broadcast_device.evtdev;
  396. cpumask_clear_cpu(cpu, tick_broadcast_mask);
  397. cpumask_clear_cpu(cpu, tick_broadcast_on);
  398. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
  399. if (bc && cpumask_empty(tick_broadcast_mask))
  400. clockevents_shutdown(bc);
  401. }
  402. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  403. }
  404. #endif
  405. void tick_suspend_broadcast(void)
  406. {
  407. struct clock_event_device *bc;
  408. unsigned long flags;
  409. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  410. bc = tick_broadcast_device.evtdev;
  411. if (bc)
  412. clockevents_shutdown(bc);
  413. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  414. }
  415. /*
  416. * This is called from tick_resume_local() on a resuming CPU. That's
  417. * called from the core resume function, tick_unfreeze() and the magic XEN
  418. * resume hackery.
  419. *
  420. * In none of these cases the broadcast device mode can change and the
  421. * bit of the resuming CPU in the broadcast mask is safe as well.
  422. */
  423. bool tick_resume_check_broadcast(void)
  424. {
  425. if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT)
  426. return false;
  427. else
  428. return cpumask_test_cpu(smp_processor_id(), tick_broadcast_mask);
  429. }
  430. void tick_resume_broadcast(void)
  431. {
  432. struct clock_event_device *bc;
  433. unsigned long flags;
  434. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  435. bc = tick_broadcast_device.evtdev;
  436. if (bc) {
  437. clockevents_tick_resume(bc);
  438. switch (tick_broadcast_device.mode) {
  439. case TICKDEV_MODE_PERIODIC:
  440. if (!cpumask_empty(tick_broadcast_mask))
  441. tick_broadcast_start_periodic(bc);
  442. break;
  443. case TICKDEV_MODE_ONESHOT:
  444. if (!cpumask_empty(tick_broadcast_mask))
  445. tick_resume_broadcast_oneshot(bc);
  446. break;
  447. }
  448. }
  449. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  450. }
  451. #ifdef CONFIG_TICK_ONESHOT
  452. static cpumask_var_t tick_broadcast_oneshot_mask;
  453. static cpumask_var_t tick_broadcast_pending_mask;
  454. static cpumask_var_t tick_broadcast_force_mask;
  455. /*
  456. * Exposed for debugging: see timer_list.c
  457. */
  458. struct cpumask *tick_get_broadcast_oneshot_mask(void)
  459. {
  460. return tick_broadcast_oneshot_mask;
  461. }
  462. /*
  463. * Called before going idle with interrupts disabled. Checks whether a
  464. * broadcast event from the other core is about to happen. We detected
  465. * that in tick_broadcast_oneshot_control(). The callsite can use this
  466. * to avoid a deep idle transition as we are about to get the
  467. * broadcast IPI right away.
  468. */
  469. int tick_check_broadcast_expired(void)
  470. {
  471. return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
  472. }
  473. /*
  474. * Set broadcast interrupt affinity
  475. */
  476. static void tick_broadcast_set_affinity(struct clock_event_device *bc,
  477. const struct cpumask *cpumask)
  478. {
  479. if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
  480. return;
  481. if (cpumask_equal(bc->cpumask, cpumask))
  482. return;
  483. bc->cpumask = cpumask;
  484. irq_set_affinity(bc->irq, bc->cpumask);
  485. }
  486. static void tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
  487. ktime_t expires)
  488. {
  489. if (!clockevent_state_oneshot(bc))
  490. clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
  491. clockevents_program_event(bc, expires, 1);
  492. tick_broadcast_set_affinity(bc, cpumask_of(cpu));
  493. }
  494. static void tick_resume_broadcast_oneshot(struct clock_event_device *bc)
  495. {
  496. clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
  497. }
  498. /*
  499. * Called from irq_enter() when idle was interrupted to reenable the
  500. * per cpu device.
  501. */
  502. void tick_check_oneshot_broadcast_this_cpu(void)
  503. {
  504. if (cpumask_test_cpu(smp_processor_id(), tick_broadcast_oneshot_mask)) {
  505. struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
  506. /*
  507. * We might be in the middle of switching over from
  508. * periodic to oneshot. If the CPU has not yet
  509. * switched over, leave the device alone.
  510. */
  511. if (td->mode == TICKDEV_MODE_ONESHOT) {
  512. clockevents_switch_state(td->evtdev,
  513. CLOCK_EVT_STATE_ONESHOT);
  514. }
  515. }
  516. }
  517. /*
  518. * Handle oneshot mode broadcasting
  519. */
  520. static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
  521. {
  522. struct tick_device *td;
  523. ktime_t now, next_event;
  524. int cpu, next_cpu = 0;
  525. bool bc_local;
  526. raw_spin_lock(&tick_broadcast_lock);
  527. dev->next_event.tv64 = KTIME_MAX;
  528. next_event.tv64 = KTIME_MAX;
  529. cpumask_clear(tmpmask);
  530. now = ktime_get();
  531. /* Find all expired events */
  532. for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
  533. td = &per_cpu(tick_cpu_device, cpu);
  534. if (td->evtdev->next_event.tv64 <= now.tv64) {
  535. cpumask_set_cpu(cpu, tmpmask);
  536. /*
  537. * Mark the remote cpu in the pending mask, so
  538. * it can avoid reprogramming the cpu local
  539. * timer in tick_broadcast_oneshot_control().
  540. */
  541. cpumask_set_cpu(cpu, tick_broadcast_pending_mask);
  542. } else if (td->evtdev->next_event.tv64 < next_event.tv64) {
  543. next_event.tv64 = td->evtdev->next_event.tv64;
  544. next_cpu = cpu;
  545. }
  546. }
  547. /*
  548. * Remove the current cpu from the pending mask. The event is
  549. * delivered immediately in tick_do_broadcast() !
  550. */
  551. cpumask_clear_cpu(smp_processor_id(), tick_broadcast_pending_mask);
  552. /* Take care of enforced broadcast requests */
  553. cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
  554. cpumask_clear(tick_broadcast_force_mask);
  555. /*
  556. * Sanity check. Catch the case where we try to broadcast to
  557. * offline cpus.
  558. */
  559. if (WARN_ON_ONCE(!cpumask_subset(tmpmask, cpu_online_mask)))
  560. cpumask_and(tmpmask, tmpmask, cpu_online_mask);
  561. /*
  562. * Wakeup the cpus which have an expired event.
  563. */
  564. bc_local = tick_do_broadcast(tmpmask);
  565. /*
  566. * Two reasons for reprogram:
  567. *
  568. * - The global event did not expire any CPU local
  569. * events. This happens in dyntick mode, as the maximum PIT
  570. * delta is quite small.
  571. *
  572. * - There are pending events on sleeping CPUs which were not
  573. * in the event mask
  574. */
  575. if (next_event.tv64 != KTIME_MAX)
  576. tick_broadcast_set_event(dev, next_cpu, next_event);
  577. raw_spin_unlock(&tick_broadcast_lock);
  578. if (bc_local) {
  579. td = this_cpu_ptr(&tick_cpu_device);
  580. td->evtdev->event_handler(td->evtdev);
  581. }
  582. }
  583. static int broadcast_needs_cpu(struct clock_event_device *bc, int cpu)
  584. {
  585. if (!(bc->features & CLOCK_EVT_FEAT_HRTIMER))
  586. return 0;
  587. if (bc->next_event.tv64 == KTIME_MAX)
  588. return 0;
  589. return bc->bound_on == cpu ? -EBUSY : 0;
  590. }
  591. static void broadcast_shutdown_local(struct clock_event_device *bc,
  592. struct clock_event_device *dev)
  593. {
  594. /*
  595. * For hrtimer based broadcasting we cannot shutdown the cpu
  596. * local device if our own event is the first one to expire or
  597. * if we own the broadcast timer.
  598. */
  599. if (bc->features & CLOCK_EVT_FEAT_HRTIMER) {
  600. if (broadcast_needs_cpu(bc, smp_processor_id()))
  601. return;
  602. if (dev->next_event.tv64 < bc->next_event.tv64)
  603. return;
  604. }
  605. clockevents_switch_state(dev, CLOCK_EVT_STATE_SHUTDOWN);
  606. }
  607. int __tick_broadcast_oneshot_control(enum tick_broadcast_state state)
  608. {
  609. struct clock_event_device *bc, *dev;
  610. int cpu, ret = 0;
  611. ktime_t now;
  612. /*
  613. * If there is no broadcast device, tell the caller not to go
  614. * into deep idle.
  615. */
  616. if (!tick_broadcast_device.evtdev)
  617. return -EBUSY;
  618. dev = this_cpu_ptr(&tick_cpu_device)->evtdev;
  619. raw_spin_lock(&tick_broadcast_lock);
  620. bc = tick_broadcast_device.evtdev;
  621. cpu = smp_processor_id();
  622. if (state == TICK_BROADCAST_ENTER) {
  623. /*
  624. * If the current CPU owns the hrtimer broadcast
  625. * mechanism, it cannot go deep idle and we do not add
  626. * the CPU to the broadcast mask. We don't have to go
  627. * through the EXIT path as the local timer is not
  628. * shutdown.
  629. */
  630. ret = broadcast_needs_cpu(bc, cpu);
  631. if (ret)
  632. goto out;
  633. /*
  634. * If the broadcast device is in periodic mode, we
  635. * return.
  636. */
  637. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
  638. /* If it is a hrtimer based broadcast, return busy */
  639. if (bc->features & CLOCK_EVT_FEAT_HRTIMER)
  640. ret = -EBUSY;
  641. goto out;
  642. }
  643. if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
  644. WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
  645. /* Conditionally shut down the local timer. */
  646. broadcast_shutdown_local(bc, dev);
  647. /*
  648. * We only reprogram the broadcast timer if we
  649. * did not mark ourself in the force mask and
  650. * if the cpu local event is earlier than the
  651. * broadcast event. If the current CPU is in
  652. * the force mask, then we are going to be
  653. * woken by the IPI right away; we return
  654. * busy, so the CPU does not try to go deep
  655. * idle.
  656. */
  657. if (cpumask_test_cpu(cpu, tick_broadcast_force_mask)) {
  658. ret = -EBUSY;
  659. } else if (dev->next_event.tv64 < bc->next_event.tv64) {
  660. tick_broadcast_set_event(bc, cpu, dev->next_event);
  661. /*
  662. * In case of hrtimer broadcasts the
  663. * programming might have moved the
  664. * timer to this cpu. If yes, remove
  665. * us from the broadcast mask and
  666. * return busy.
  667. */
  668. ret = broadcast_needs_cpu(bc, cpu);
  669. if (ret) {
  670. cpumask_clear_cpu(cpu,
  671. tick_broadcast_oneshot_mask);
  672. }
  673. }
  674. }
  675. } else {
  676. if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
  677. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  678. /*
  679. * The cpu which was handling the broadcast
  680. * timer marked this cpu in the broadcast
  681. * pending mask and fired the broadcast
  682. * IPI. So we are going to handle the expired
  683. * event anyway via the broadcast IPI
  684. * handler. No need to reprogram the timer
  685. * with an already expired event.
  686. */
  687. if (cpumask_test_and_clear_cpu(cpu,
  688. tick_broadcast_pending_mask))
  689. goto out;
  690. /*
  691. * Bail out if there is no next event.
  692. */
  693. if (dev->next_event.tv64 == KTIME_MAX)
  694. goto out;
  695. /*
  696. * If the pending bit is not set, then we are
  697. * either the CPU handling the broadcast
  698. * interrupt or we got woken by something else.
  699. *
  700. * We are not longer in the broadcast mask, so
  701. * if the cpu local expiry time is already
  702. * reached, we would reprogram the cpu local
  703. * timer with an already expired event.
  704. *
  705. * This can lead to a ping-pong when we return
  706. * to idle and therefor rearm the broadcast
  707. * timer before the cpu local timer was able
  708. * to fire. This happens because the forced
  709. * reprogramming makes sure that the event
  710. * will happen in the future and depending on
  711. * the min_delta setting this might be far
  712. * enough out that the ping-pong starts.
  713. *
  714. * If the cpu local next_event has expired
  715. * then we know that the broadcast timer
  716. * next_event has expired as well and
  717. * broadcast is about to be handled. So we
  718. * avoid reprogramming and enforce that the
  719. * broadcast handler, which did not run yet,
  720. * will invoke the cpu local handler.
  721. *
  722. * We cannot call the handler directly from
  723. * here, because we might be in a NOHZ phase
  724. * and we did not go through the irq_enter()
  725. * nohz fixups.
  726. */
  727. now = ktime_get();
  728. if (dev->next_event.tv64 <= now.tv64) {
  729. cpumask_set_cpu(cpu, tick_broadcast_force_mask);
  730. goto out;
  731. }
  732. /*
  733. * We got woken by something else. Reprogram
  734. * the cpu local timer device.
  735. */
  736. tick_program_event(dev->next_event, 1);
  737. }
  738. }
  739. out:
  740. raw_spin_unlock(&tick_broadcast_lock);
  741. return ret;
  742. }
  743. /*
  744. * Reset the one shot broadcast for a cpu
  745. *
  746. * Called with tick_broadcast_lock held
  747. */
  748. static void tick_broadcast_clear_oneshot(int cpu)
  749. {
  750. cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
  751. cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
  752. }
  753. static void tick_broadcast_init_next_event(struct cpumask *mask,
  754. ktime_t expires)
  755. {
  756. struct tick_device *td;
  757. int cpu;
  758. for_each_cpu(cpu, mask) {
  759. td = &per_cpu(tick_cpu_device, cpu);
  760. if (td->evtdev)
  761. td->evtdev->next_event = expires;
  762. }
  763. }
  764. /**
  765. * tick_broadcast_setup_oneshot - setup the broadcast device
  766. */
  767. void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
  768. {
  769. int cpu = smp_processor_id();
  770. if (!bc)
  771. return;
  772. /* Set it up only once ! */
  773. if (bc->event_handler != tick_handle_oneshot_broadcast) {
  774. int was_periodic = clockevent_state_periodic(bc);
  775. bc->event_handler = tick_handle_oneshot_broadcast;
  776. /*
  777. * We must be careful here. There might be other CPUs
  778. * waiting for periodic broadcast. We need to set the
  779. * oneshot_mask bits for those and program the
  780. * broadcast device to fire.
  781. */
  782. cpumask_copy(tmpmask, tick_broadcast_mask);
  783. cpumask_clear_cpu(cpu, tmpmask);
  784. cpumask_or(tick_broadcast_oneshot_mask,
  785. tick_broadcast_oneshot_mask, tmpmask);
  786. if (was_periodic && !cpumask_empty(tmpmask)) {
  787. clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
  788. tick_broadcast_init_next_event(tmpmask,
  789. tick_next_period);
  790. tick_broadcast_set_event(bc, cpu, tick_next_period);
  791. } else
  792. bc->next_event.tv64 = KTIME_MAX;
  793. } else {
  794. /*
  795. * The first cpu which switches to oneshot mode sets
  796. * the bit for all other cpus which are in the general
  797. * (periodic) broadcast mask. So the bit is set and
  798. * would prevent the first broadcast enter after this
  799. * to program the bc device.
  800. */
  801. tick_broadcast_clear_oneshot(cpu);
  802. }
  803. }
  804. /*
  805. * Select oneshot operating mode for the broadcast device
  806. */
  807. void tick_broadcast_switch_to_oneshot(void)
  808. {
  809. struct clock_event_device *bc;
  810. unsigned long flags;
  811. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  812. tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
  813. bc = tick_broadcast_device.evtdev;
  814. if (bc)
  815. tick_broadcast_setup_oneshot(bc);
  816. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  817. }
  818. #ifdef CONFIG_HOTPLUG_CPU
  819. void hotplug_cpu__broadcast_tick_pull(int deadcpu)
  820. {
  821. struct clock_event_device *bc;
  822. unsigned long flags;
  823. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  824. bc = tick_broadcast_device.evtdev;
  825. if (bc && broadcast_needs_cpu(bc, deadcpu)) {
  826. /* This moves the broadcast assignment to this CPU: */
  827. clockevents_program_event(bc, bc->next_event, 1);
  828. }
  829. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  830. }
  831. /*
  832. * Remove a dead CPU from broadcasting
  833. */
  834. void tick_shutdown_broadcast_oneshot(unsigned int cpu)
  835. {
  836. unsigned long flags;
  837. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  838. /*
  839. * Clear the broadcast masks for the dead cpu, but do not stop
  840. * the broadcast device!
  841. */
  842. cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
  843. cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
  844. cpumask_clear_cpu(cpu, tick_broadcast_force_mask);
  845. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  846. }
  847. #endif
  848. /*
  849. * Check, whether the broadcast device is in one shot mode
  850. */
  851. int tick_broadcast_oneshot_active(void)
  852. {
  853. return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
  854. }
  855. /*
  856. * Check whether the broadcast device supports oneshot.
  857. */
  858. bool tick_broadcast_oneshot_available(void)
  859. {
  860. struct clock_event_device *bc = tick_broadcast_device.evtdev;
  861. return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
  862. }
  863. #else
  864. int __tick_broadcast_oneshot_control(enum tick_broadcast_state state)
  865. {
  866. struct clock_event_device *bc = tick_broadcast_device.evtdev;
  867. if (!bc || (bc->features & CLOCK_EVT_FEAT_HRTIMER))
  868. return -EBUSY;
  869. return 0;
  870. }
  871. #endif
  872. void __init tick_broadcast_init(void)
  873. {
  874. zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
  875. zalloc_cpumask_var(&tick_broadcast_on, GFP_NOWAIT);
  876. zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
  877. #ifdef CONFIG_TICK_ONESHOT
  878. zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
  879. zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
  880. zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);
  881. #endif
  882. }