tick-broadcast.c 27 KB

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