tick-broadcast.c 23 KB

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