tick-broadcast.c 25 KB

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