tick-broadcast.c 25 KB

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