tick-broadcast.c 23 KB

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