tick-broadcast.c 23 KB

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