tick-broadcast.c 27 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 __cpumask_var_read_mostly;
  30. static cpumask_var_t tick_broadcast_on __cpumask_var_read_mostly;
  31. static cpumask_var_t tmpmask __cpumask_var_read_mostly;
  32. static int tick_broadcast_forced;
  33. static __cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(tick_broadcast_lock);
  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. void tick_broadcast_control(enum tick_broadcast_mode mode)
  309. {
  310. struct clock_event_device *bc, *dev;
  311. struct tick_device *td;
  312. int cpu, bc_stopped;
  313. unsigned long flags;
  314. /* Protects also the local clockevent device. */
  315. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  316. td = this_cpu_ptr(&tick_cpu_device);
  317. dev = td->evtdev;
  318. /*
  319. * Is the device not affected by the powerstate ?
  320. */
  321. if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
  322. goto out;
  323. if (!tick_device_is_functional(dev))
  324. goto out;
  325. cpu = smp_processor_id();
  326. bc = tick_broadcast_device.evtdev;
  327. bc_stopped = cpumask_empty(tick_broadcast_mask);
  328. switch (mode) {
  329. case TICK_BROADCAST_FORCE:
  330. tick_broadcast_forced = 1;
  331. case TICK_BROADCAST_ON:
  332. cpumask_set_cpu(cpu, tick_broadcast_on);
  333. if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_mask)) {
  334. /*
  335. * Only shutdown the cpu local device, if:
  336. *
  337. * - the broadcast device exists
  338. * - the broadcast device is not a hrtimer based one
  339. * - the broadcast device is in periodic mode to
  340. * avoid a hickup during switch to oneshot mode
  341. */
  342. if (bc && !(bc->features & CLOCK_EVT_FEAT_HRTIMER) &&
  343. tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
  344. clockevents_shutdown(dev);
  345. }
  346. break;
  347. case TICK_BROADCAST_OFF:
  348. if (tick_broadcast_forced)
  349. break;
  350. cpumask_clear_cpu(cpu, tick_broadcast_on);
  351. if (!tick_device_is_functional(dev))
  352. break;
  353. if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_mask)) {
  354. if (tick_broadcast_device.mode ==
  355. TICKDEV_MODE_PERIODIC)
  356. tick_setup_periodic(dev, 0);
  357. }
  358. break;
  359. }
  360. if (bc) {
  361. if (cpumask_empty(tick_broadcast_mask)) {
  362. if (!bc_stopped)
  363. clockevents_shutdown(bc);
  364. } else if (bc_stopped) {
  365. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
  366. tick_broadcast_start_periodic(bc);
  367. else
  368. tick_broadcast_setup_oneshot(bc);
  369. }
  370. }
  371. out:
  372. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  373. }
  374. EXPORT_SYMBOL_GPL(tick_broadcast_control);
  375. /*
  376. * Set the periodic handler depending on broadcast on/off
  377. */
  378. void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
  379. {
  380. if (!broadcast)
  381. dev->event_handler = tick_handle_periodic;
  382. else
  383. dev->event_handler = tick_handle_periodic_broadcast;
  384. }
  385. #ifdef CONFIG_HOTPLUG_CPU
  386. /*
  387. * Remove a CPU from broadcasting
  388. */
  389. void tick_shutdown_broadcast(unsigned int cpu)
  390. {
  391. struct clock_event_device *bc;
  392. unsigned long flags;
  393. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  394. bc = tick_broadcast_device.evtdev;
  395. cpumask_clear_cpu(cpu, tick_broadcast_mask);
  396. cpumask_clear_cpu(cpu, tick_broadcast_on);
  397. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
  398. if (bc && cpumask_empty(tick_broadcast_mask))
  399. clockevents_shutdown(bc);
  400. }
  401. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  402. }
  403. #endif
  404. void tick_suspend_broadcast(void)
  405. {
  406. struct clock_event_device *bc;
  407. unsigned long flags;
  408. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  409. bc = tick_broadcast_device.evtdev;
  410. if (bc)
  411. clockevents_shutdown(bc);
  412. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  413. }
  414. /*
  415. * This is called from tick_resume_local() on a resuming CPU. That's
  416. * called from the core resume function, tick_unfreeze() and the magic XEN
  417. * resume hackery.
  418. *
  419. * In none of these cases the broadcast device mode can change and the
  420. * bit of the resuming CPU in the broadcast mask is safe as well.
  421. */
  422. bool tick_resume_check_broadcast(void)
  423. {
  424. if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT)
  425. return false;
  426. else
  427. return cpumask_test_cpu(smp_processor_id(), tick_broadcast_mask);
  428. }
  429. void tick_resume_broadcast(void)
  430. {
  431. struct clock_event_device *bc;
  432. unsigned long flags;
  433. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  434. bc = tick_broadcast_device.evtdev;
  435. if (bc) {
  436. clockevents_tick_resume(bc);
  437. switch (tick_broadcast_device.mode) {
  438. case TICKDEV_MODE_PERIODIC:
  439. if (!cpumask_empty(tick_broadcast_mask))
  440. tick_broadcast_start_periodic(bc);
  441. break;
  442. case TICKDEV_MODE_ONESHOT:
  443. if (!cpumask_empty(tick_broadcast_mask))
  444. tick_resume_broadcast_oneshot(bc);
  445. break;
  446. }
  447. }
  448. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  449. }
  450. #ifdef CONFIG_TICK_ONESHOT
  451. static cpumask_var_t tick_broadcast_oneshot_mask __cpumask_var_read_mostly;
  452. static cpumask_var_t tick_broadcast_pending_mask __cpumask_var_read_mostly;
  453. static cpumask_var_t tick_broadcast_force_mask __cpumask_var_read_mostly;
  454. /*
  455. * Exposed for debugging: see timer_list.c
  456. */
  457. struct cpumask *tick_get_broadcast_oneshot_mask(void)
  458. {
  459. return tick_broadcast_oneshot_mask;
  460. }
  461. /*
  462. * Called before going idle with interrupts disabled. Checks whether a
  463. * broadcast event from the other core is about to happen. We detected
  464. * that in tick_broadcast_oneshot_control(). The callsite can use this
  465. * to avoid a deep idle transition as we are about to get the
  466. * broadcast IPI right away.
  467. */
  468. int tick_check_broadcast_expired(void)
  469. {
  470. return cpumask_test_cpu(smp_processor_id(), tick_broadcast_force_mask);
  471. }
  472. /*
  473. * Set broadcast interrupt affinity
  474. */
  475. static void tick_broadcast_set_affinity(struct clock_event_device *bc,
  476. const struct cpumask *cpumask)
  477. {
  478. if (!(bc->features & CLOCK_EVT_FEAT_DYNIRQ))
  479. return;
  480. if (cpumask_equal(bc->cpumask, cpumask))
  481. return;
  482. bc->cpumask = cpumask;
  483. irq_set_affinity(bc->irq, bc->cpumask);
  484. }
  485. static void tick_broadcast_set_event(struct clock_event_device *bc, int cpu,
  486. ktime_t expires)
  487. {
  488. if (!clockevent_state_oneshot(bc))
  489. clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
  490. clockevents_program_event(bc, expires, 1);
  491. tick_broadcast_set_affinity(bc, cpumask_of(cpu));
  492. }
  493. static void tick_resume_broadcast_oneshot(struct clock_event_device *bc)
  494. {
  495. clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
  496. }
  497. /*
  498. * Called from irq_enter() when idle was interrupted to reenable the
  499. * per cpu device.
  500. */
  501. void tick_check_oneshot_broadcast_this_cpu(void)
  502. {
  503. if (cpumask_test_cpu(smp_processor_id(), tick_broadcast_oneshot_mask)) {
  504. struct tick_device *td = this_cpu_ptr(&tick_cpu_device);
  505. /*
  506. * We might be in the middle of switching over from
  507. * periodic to oneshot. If the CPU has not yet
  508. * switched over, leave the device alone.
  509. */
  510. if (td->mode == TICKDEV_MODE_ONESHOT) {
  511. clockevents_switch_state(td->evtdev,
  512. CLOCK_EVT_STATE_ONESHOT);
  513. }
  514. }
  515. }
  516. /*
  517. * Handle oneshot mode broadcasting
  518. */
  519. static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
  520. {
  521. struct tick_device *td;
  522. ktime_t now, next_event;
  523. int cpu, next_cpu = 0;
  524. bool bc_local;
  525. raw_spin_lock(&tick_broadcast_lock);
  526. dev->next_event = KTIME_MAX;
  527. next_event = KTIME_MAX;
  528. cpumask_clear(tmpmask);
  529. now = ktime_get();
  530. /* Find all expired events */
  531. for_each_cpu(cpu, tick_broadcast_oneshot_mask) {
  532. td = &per_cpu(tick_cpu_device, cpu);
  533. if (td->evtdev->next_event <= now) {
  534. cpumask_set_cpu(cpu, tmpmask);
  535. /*
  536. * Mark the remote cpu in the pending mask, so
  537. * it can avoid reprogramming the cpu local
  538. * timer in tick_broadcast_oneshot_control().
  539. */
  540. cpumask_set_cpu(cpu, tick_broadcast_pending_mask);
  541. } else if (td->evtdev->next_event < next_event) {
  542. next_event = td->evtdev->next_event;
  543. next_cpu = cpu;
  544. }
  545. }
  546. /*
  547. * Remove the current cpu from the pending mask. The event is
  548. * delivered immediately in tick_do_broadcast() !
  549. */
  550. cpumask_clear_cpu(smp_processor_id(), tick_broadcast_pending_mask);
  551. /* Take care of enforced broadcast requests */
  552. cpumask_or(tmpmask, tmpmask, tick_broadcast_force_mask);
  553. cpumask_clear(tick_broadcast_force_mask);
  554. /*
  555. * Sanity check. Catch the case where we try to broadcast to
  556. * offline cpus.
  557. */
  558. if (WARN_ON_ONCE(!cpumask_subset(tmpmask, cpu_online_mask)))
  559. cpumask_and(tmpmask, tmpmask, cpu_online_mask);
  560. /*
  561. * Wakeup the cpus which have an expired event.
  562. */
  563. bc_local = tick_do_broadcast(tmpmask);
  564. /*
  565. * Two reasons for reprogram:
  566. *
  567. * - The global event did not expire any CPU local
  568. * events. This happens in dyntick mode, as the maximum PIT
  569. * delta is quite small.
  570. *
  571. * - There are pending events on sleeping CPUs which were not
  572. * in the event mask
  573. */
  574. if (next_event != KTIME_MAX)
  575. tick_broadcast_set_event(dev, next_cpu, next_event);
  576. raw_spin_unlock(&tick_broadcast_lock);
  577. if (bc_local) {
  578. td = this_cpu_ptr(&tick_cpu_device);
  579. td->evtdev->event_handler(td->evtdev);
  580. }
  581. }
  582. static int broadcast_needs_cpu(struct clock_event_device *bc, int cpu)
  583. {
  584. if (!(bc->features & CLOCK_EVT_FEAT_HRTIMER))
  585. return 0;
  586. if (bc->next_event == KTIME_MAX)
  587. return 0;
  588. return bc->bound_on == cpu ? -EBUSY : 0;
  589. }
  590. static void broadcast_shutdown_local(struct clock_event_device *bc,
  591. struct clock_event_device *dev)
  592. {
  593. /*
  594. * For hrtimer based broadcasting we cannot shutdown the cpu
  595. * local device if our own event is the first one to expire or
  596. * if we own the broadcast timer.
  597. */
  598. if (bc->features & CLOCK_EVT_FEAT_HRTIMER) {
  599. if (broadcast_needs_cpu(bc, smp_processor_id()))
  600. return;
  601. if (dev->next_event < bc->next_event)
  602. return;
  603. }
  604. clockevents_switch_state(dev, CLOCK_EVT_STATE_SHUTDOWN);
  605. }
  606. int __tick_broadcast_oneshot_control(enum tick_broadcast_state state)
  607. {
  608. struct clock_event_device *bc, *dev;
  609. int cpu, ret = 0;
  610. ktime_t now;
  611. /*
  612. * If there is no broadcast device, tell the caller not to go
  613. * into deep idle.
  614. */
  615. if (!tick_broadcast_device.evtdev)
  616. return -EBUSY;
  617. dev = this_cpu_ptr(&tick_cpu_device)->evtdev;
  618. raw_spin_lock(&tick_broadcast_lock);
  619. bc = tick_broadcast_device.evtdev;
  620. cpu = smp_processor_id();
  621. if (state == TICK_BROADCAST_ENTER) {
  622. /*
  623. * If the current CPU owns the hrtimer broadcast
  624. * mechanism, it cannot go deep idle and we do not add
  625. * the CPU to the broadcast mask. We don't have to go
  626. * through the EXIT path as the local timer is not
  627. * shutdown.
  628. */
  629. ret = broadcast_needs_cpu(bc, cpu);
  630. if (ret)
  631. goto out;
  632. /*
  633. * If the broadcast device is in periodic mode, we
  634. * return.
  635. */
  636. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
  637. /* If it is a hrtimer based broadcast, return busy */
  638. if (bc->features & CLOCK_EVT_FEAT_HRTIMER)
  639. ret = -EBUSY;
  640. goto out;
  641. }
  642. if (!cpumask_test_and_set_cpu(cpu, tick_broadcast_oneshot_mask)) {
  643. WARN_ON_ONCE(cpumask_test_cpu(cpu, tick_broadcast_pending_mask));
  644. /* Conditionally shut down the local timer. */
  645. broadcast_shutdown_local(bc, dev);
  646. /*
  647. * We only reprogram the broadcast timer if we
  648. * did not mark ourself in the force mask and
  649. * if the cpu local event is earlier than the
  650. * broadcast event. If the current CPU is in
  651. * the force mask, then we are going to be
  652. * woken by the IPI right away; we return
  653. * busy, so the CPU does not try to go deep
  654. * idle.
  655. */
  656. if (cpumask_test_cpu(cpu, tick_broadcast_force_mask)) {
  657. ret = -EBUSY;
  658. } else if (dev->next_event < bc->next_event) {
  659. tick_broadcast_set_event(bc, cpu, dev->next_event);
  660. /*
  661. * In case of hrtimer broadcasts the
  662. * programming might have moved the
  663. * timer to this cpu. If yes, remove
  664. * us from the broadcast mask and
  665. * return busy.
  666. */
  667. ret = broadcast_needs_cpu(bc, cpu);
  668. if (ret) {
  669. cpumask_clear_cpu(cpu,
  670. tick_broadcast_oneshot_mask);
  671. }
  672. }
  673. }
  674. } else {
  675. if (cpumask_test_and_clear_cpu(cpu, tick_broadcast_oneshot_mask)) {
  676. clockevents_switch_state(dev, CLOCK_EVT_STATE_ONESHOT);
  677. /*
  678. * The cpu which was handling the broadcast
  679. * timer marked this cpu in the broadcast
  680. * pending mask and fired the broadcast
  681. * IPI. So we are going to handle the expired
  682. * event anyway via the broadcast IPI
  683. * handler. No need to reprogram the timer
  684. * with an already expired event.
  685. */
  686. if (cpumask_test_and_clear_cpu(cpu,
  687. tick_broadcast_pending_mask))
  688. goto out;
  689. /*
  690. * Bail out if there is no next event.
  691. */
  692. if (dev->next_event == KTIME_MAX)
  693. goto out;
  694. /*
  695. * If the pending bit is not set, then we are
  696. * either the CPU handling the broadcast
  697. * interrupt or we got woken by something else.
  698. *
  699. * We are not longer in the broadcast mask, so
  700. * if the cpu local expiry time is already
  701. * reached, we would reprogram the cpu local
  702. * timer with an already expired event.
  703. *
  704. * This can lead to a ping-pong when we return
  705. * to idle and therefor rearm the broadcast
  706. * timer before the cpu local timer was able
  707. * to fire. This happens because the forced
  708. * reprogramming makes sure that the event
  709. * will happen in the future and depending on
  710. * the min_delta setting this might be far
  711. * enough out that the ping-pong starts.
  712. *
  713. * If the cpu local next_event has expired
  714. * then we know that the broadcast timer
  715. * next_event has expired as well and
  716. * broadcast is about to be handled. So we
  717. * avoid reprogramming and enforce that the
  718. * broadcast handler, which did not run yet,
  719. * will invoke the cpu local handler.
  720. *
  721. * We cannot call the handler directly from
  722. * here, because we might be in a NOHZ phase
  723. * and we did not go through the irq_enter()
  724. * nohz fixups.
  725. */
  726. now = ktime_get();
  727. if (dev->next_event <= now) {
  728. cpumask_set_cpu(cpu, tick_broadcast_force_mask);
  729. goto out;
  730. }
  731. /*
  732. * We got woken by something else. Reprogram
  733. * the cpu local timer device.
  734. */
  735. tick_program_event(dev->next_event, 1);
  736. }
  737. }
  738. out:
  739. raw_spin_unlock(&tick_broadcast_lock);
  740. return ret;
  741. }
  742. /*
  743. * Reset the one shot broadcast for a cpu
  744. *
  745. * Called with tick_broadcast_lock held
  746. */
  747. static void tick_broadcast_clear_oneshot(int cpu)
  748. {
  749. cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
  750. cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
  751. }
  752. static void tick_broadcast_init_next_event(struct cpumask *mask,
  753. ktime_t expires)
  754. {
  755. struct tick_device *td;
  756. int cpu;
  757. for_each_cpu(cpu, mask) {
  758. td = &per_cpu(tick_cpu_device, cpu);
  759. if (td->evtdev)
  760. td->evtdev->next_event = expires;
  761. }
  762. }
  763. /**
  764. * tick_broadcast_setup_oneshot - setup the broadcast device
  765. */
  766. void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
  767. {
  768. int cpu = smp_processor_id();
  769. if (!bc)
  770. return;
  771. /* Set it up only once ! */
  772. if (bc->event_handler != tick_handle_oneshot_broadcast) {
  773. int was_periodic = clockevent_state_periodic(bc);
  774. bc->event_handler = tick_handle_oneshot_broadcast;
  775. /*
  776. * We must be careful here. There might be other CPUs
  777. * waiting for periodic broadcast. We need to set the
  778. * oneshot_mask bits for those and program the
  779. * broadcast device to fire.
  780. */
  781. cpumask_copy(tmpmask, tick_broadcast_mask);
  782. cpumask_clear_cpu(cpu, tmpmask);
  783. cpumask_or(tick_broadcast_oneshot_mask,
  784. tick_broadcast_oneshot_mask, tmpmask);
  785. if (was_periodic && !cpumask_empty(tmpmask)) {
  786. clockevents_switch_state(bc, CLOCK_EVT_STATE_ONESHOT);
  787. tick_broadcast_init_next_event(tmpmask,
  788. tick_next_period);
  789. tick_broadcast_set_event(bc, cpu, tick_next_period);
  790. } else
  791. bc->next_event = KTIME_MAX;
  792. } else {
  793. /*
  794. * The first cpu which switches to oneshot mode sets
  795. * the bit for all other cpus which are in the general
  796. * (periodic) broadcast mask. So the bit is set and
  797. * would prevent the first broadcast enter after this
  798. * to program the bc device.
  799. */
  800. tick_broadcast_clear_oneshot(cpu);
  801. }
  802. }
  803. /*
  804. * Select oneshot operating mode for the broadcast device
  805. */
  806. void tick_broadcast_switch_to_oneshot(void)
  807. {
  808. struct clock_event_device *bc;
  809. unsigned long flags;
  810. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  811. tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
  812. bc = tick_broadcast_device.evtdev;
  813. if (bc)
  814. tick_broadcast_setup_oneshot(bc);
  815. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  816. }
  817. #ifdef CONFIG_HOTPLUG_CPU
  818. void hotplug_cpu__broadcast_tick_pull(int deadcpu)
  819. {
  820. struct clock_event_device *bc;
  821. unsigned long flags;
  822. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  823. bc = tick_broadcast_device.evtdev;
  824. if (bc && broadcast_needs_cpu(bc, deadcpu)) {
  825. /* This moves the broadcast assignment to this CPU: */
  826. clockevents_program_event(bc, bc->next_event, 1);
  827. }
  828. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  829. }
  830. /*
  831. * Remove a dead CPU from broadcasting
  832. */
  833. void tick_shutdown_broadcast_oneshot(unsigned int cpu)
  834. {
  835. unsigned long flags;
  836. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  837. /*
  838. * Clear the broadcast masks for the dead cpu, but do not stop
  839. * the broadcast device!
  840. */
  841. cpumask_clear_cpu(cpu, tick_broadcast_oneshot_mask);
  842. cpumask_clear_cpu(cpu, tick_broadcast_pending_mask);
  843. cpumask_clear_cpu(cpu, tick_broadcast_force_mask);
  844. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  845. }
  846. #endif
  847. /*
  848. * Check, whether the broadcast device is in one shot mode
  849. */
  850. int tick_broadcast_oneshot_active(void)
  851. {
  852. return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
  853. }
  854. /*
  855. * Check whether the broadcast device supports oneshot.
  856. */
  857. bool tick_broadcast_oneshot_available(void)
  858. {
  859. struct clock_event_device *bc = tick_broadcast_device.evtdev;
  860. return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
  861. }
  862. #else
  863. int __tick_broadcast_oneshot_control(enum tick_broadcast_state state)
  864. {
  865. struct clock_event_device *bc = tick_broadcast_device.evtdev;
  866. if (!bc || (bc->features & CLOCK_EVT_FEAT_HRTIMER))
  867. return -EBUSY;
  868. return 0;
  869. }
  870. #endif
  871. void __init tick_broadcast_init(void)
  872. {
  873. zalloc_cpumask_var(&tick_broadcast_mask, GFP_NOWAIT);
  874. zalloc_cpumask_var(&tick_broadcast_on, GFP_NOWAIT);
  875. zalloc_cpumask_var(&tmpmask, GFP_NOWAIT);
  876. #ifdef CONFIG_TICK_ONESHOT
  877. zalloc_cpumask_var(&tick_broadcast_oneshot_mask, GFP_NOWAIT);
  878. zalloc_cpumask_var(&tick_broadcast_pending_mask, GFP_NOWAIT);
  879. zalloc_cpumask_var(&tick_broadcast_force_mask, GFP_NOWAIT);
  880. #endif
  881. }