tick-broadcast.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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 "tick-internal.h"
  22. /*
  23. * Broadcast support for broken x86 hardware, where the local apic
  24. * timer stops in C3 state.
  25. */
  26. static struct tick_device tick_broadcast_device;
  27. /* FIXME: Use cpumask_var_t. */
  28. static DECLARE_BITMAP(tick_broadcast_mask, NR_CPUS);
  29. static DECLARE_BITMAP(tmpmask, NR_CPUS);
  30. static DEFINE_RAW_SPINLOCK(tick_broadcast_lock);
  31. static int tick_broadcast_force;
  32. #ifdef CONFIG_TICK_ONESHOT
  33. static void tick_broadcast_clear_oneshot(int cpu);
  34. #else
  35. static inline void tick_broadcast_clear_oneshot(int cpu) { }
  36. #endif
  37. /*
  38. * Debugging: see timer_list.c
  39. */
  40. struct tick_device *tick_get_broadcast_device(void)
  41. {
  42. return &tick_broadcast_device;
  43. }
  44. struct cpumask *tick_get_broadcast_mask(void)
  45. {
  46. return to_cpumask(tick_broadcast_mask);
  47. }
  48. /*
  49. * Start the device in periodic mode
  50. */
  51. static void tick_broadcast_start_periodic(struct clock_event_device *bc)
  52. {
  53. if (bc)
  54. tick_setup_periodic(bc, 1);
  55. }
  56. /*
  57. * Check, if the device can be utilized as broadcast device:
  58. */
  59. int tick_check_broadcast_device(struct clock_event_device *dev)
  60. {
  61. if ((tick_broadcast_device.evtdev &&
  62. tick_broadcast_device.evtdev->rating >= dev->rating) ||
  63. (dev->features & CLOCK_EVT_FEAT_C3STOP))
  64. return 0;
  65. clockevents_exchange_device(tick_broadcast_device.evtdev, dev);
  66. tick_broadcast_device.evtdev = dev;
  67. if (!cpumask_empty(tick_get_broadcast_mask()))
  68. tick_broadcast_start_periodic(dev);
  69. return 1;
  70. }
  71. /*
  72. * Check, if the device is the broadcast device
  73. */
  74. int tick_is_broadcast_device(struct clock_event_device *dev)
  75. {
  76. return (dev && tick_broadcast_device.evtdev == dev);
  77. }
  78. /*
  79. * Check, if the device is disfunctional and a place holder, which
  80. * needs to be handled by the broadcast device.
  81. */
  82. int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
  83. {
  84. unsigned long flags;
  85. int ret = 0;
  86. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  87. /*
  88. * Devices might be registered with both periodic and oneshot
  89. * mode disabled. This signals, that the device needs to be
  90. * operated from the broadcast device and is a placeholder for
  91. * the cpu local device.
  92. */
  93. if (!tick_device_is_functional(dev)) {
  94. dev->event_handler = tick_handle_periodic;
  95. cpumask_set_cpu(cpu, tick_get_broadcast_mask());
  96. tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
  97. ret = 1;
  98. } else {
  99. /*
  100. * When the new device is not affected by the stop
  101. * feature and the cpu is marked in the broadcast mask
  102. * then clear the broadcast bit.
  103. */
  104. if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
  105. int cpu = smp_processor_id();
  106. cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
  107. tick_broadcast_clear_oneshot(cpu);
  108. }
  109. }
  110. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  111. return ret;
  112. }
  113. /*
  114. * Broadcast the event to the cpus, which are set in the mask (mangled).
  115. */
  116. static void tick_do_broadcast(struct cpumask *mask)
  117. {
  118. int cpu = smp_processor_id();
  119. struct tick_device *td;
  120. /*
  121. * Check, if the current cpu is in the mask
  122. */
  123. if (cpumask_test_cpu(cpu, mask)) {
  124. cpumask_clear_cpu(cpu, mask);
  125. td = &per_cpu(tick_cpu_device, cpu);
  126. td->evtdev->event_handler(td->evtdev);
  127. }
  128. if (!cpumask_empty(mask)) {
  129. /*
  130. * It might be necessary to actually check whether the devices
  131. * have different broadcast functions. For now, just use the
  132. * one of the first device. This works as long as we have this
  133. * misfeature only on x86 (lapic)
  134. */
  135. td = &per_cpu(tick_cpu_device, cpumask_first(mask));
  136. td->evtdev->broadcast(mask);
  137. }
  138. }
  139. /*
  140. * Periodic broadcast:
  141. * - invoke the broadcast handlers
  142. */
  143. static void tick_do_periodic_broadcast(void)
  144. {
  145. raw_spin_lock(&tick_broadcast_lock);
  146. cpumask_and(to_cpumask(tmpmask),
  147. cpu_online_mask, tick_get_broadcast_mask());
  148. tick_do_broadcast(to_cpumask(tmpmask));
  149. raw_spin_unlock(&tick_broadcast_lock);
  150. }
  151. /*
  152. * Event handler for periodic broadcast ticks
  153. */
  154. static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
  155. {
  156. ktime_t next;
  157. tick_do_periodic_broadcast();
  158. /*
  159. * The device is in periodic mode. No reprogramming necessary:
  160. */
  161. if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
  162. return;
  163. /*
  164. * Setup the next period for devices, which do not have
  165. * periodic mode. We read dev->next_event first and add to it
  166. * when the event already expired. clockevents_program_event()
  167. * sets dev->next_event only when the event is really
  168. * programmed to the device.
  169. */
  170. for (next = dev->next_event; ;) {
  171. next = ktime_add(next, tick_period);
  172. if (!clockevents_program_event(dev, next, ktime_get()))
  173. return;
  174. tick_do_periodic_broadcast();
  175. }
  176. }
  177. /*
  178. * Powerstate information: The system enters/leaves a state, where
  179. * affected devices might stop
  180. */
  181. static void tick_do_broadcast_on_off(unsigned long *reason)
  182. {
  183. struct clock_event_device *bc, *dev;
  184. struct tick_device *td;
  185. unsigned long flags;
  186. int cpu, bc_stopped;
  187. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  188. cpu = smp_processor_id();
  189. td = &per_cpu(tick_cpu_device, cpu);
  190. dev = td->evtdev;
  191. bc = tick_broadcast_device.evtdev;
  192. /*
  193. * Is the device not affected by the powerstate ?
  194. */
  195. if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
  196. goto out;
  197. if (!tick_device_is_functional(dev))
  198. goto out;
  199. bc_stopped = cpumask_empty(tick_get_broadcast_mask());
  200. switch (*reason) {
  201. case CLOCK_EVT_NOTIFY_BROADCAST_ON:
  202. case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
  203. if (!cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
  204. cpumask_set_cpu(cpu, tick_get_broadcast_mask());
  205. if (tick_broadcast_device.mode ==
  206. TICKDEV_MODE_PERIODIC)
  207. clockevents_shutdown(dev);
  208. }
  209. if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
  210. tick_broadcast_force = 1;
  211. break;
  212. case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
  213. if (!tick_broadcast_force &&
  214. cpumask_test_cpu(cpu, tick_get_broadcast_mask())) {
  215. cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
  216. if (tick_broadcast_device.mode ==
  217. TICKDEV_MODE_PERIODIC)
  218. tick_setup_periodic(dev, 0);
  219. }
  220. break;
  221. }
  222. if (cpumask_empty(tick_get_broadcast_mask())) {
  223. if (!bc_stopped)
  224. clockevents_shutdown(bc);
  225. } else if (bc_stopped) {
  226. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
  227. tick_broadcast_start_periodic(bc);
  228. else
  229. tick_broadcast_setup_oneshot(bc);
  230. }
  231. out:
  232. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  233. }
  234. /*
  235. * Powerstate information: The system enters/leaves a state, where
  236. * affected devices might stop.
  237. */
  238. void tick_broadcast_on_off(unsigned long reason, int *oncpu)
  239. {
  240. if (!cpumask_test_cpu(*oncpu, cpu_online_mask))
  241. printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
  242. "offline CPU #%d\n", *oncpu);
  243. else
  244. tick_do_broadcast_on_off(&reason);
  245. }
  246. /*
  247. * Set the periodic handler depending on broadcast on/off
  248. */
  249. void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
  250. {
  251. if (!broadcast)
  252. dev->event_handler = tick_handle_periodic;
  253. else
  254. dev->event_handler = tick_handle_periodic_broadcast;
  255. }
  256. /*
  257. * Remove a CPU from broadcasting
  258. */
  259. void tick_shutdown_broadcast(unsigned int *cpup)
  260. {
  261. struct clock_event_device *bc;
  262. unsigned long flags;
  263. unsigned int cpu = *cpup;
  264. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  265. bc = tick_broadcast_device.evtdev;
  266. cpumask_clear_cpu(cpu, tick_get_broadcast_mask());
  267. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
  268. if (bc && cpumask_empty(tick_get_broadcast_mask()))
  269. clockevents_shutdown(bc);
  270. }
  271. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  272. }
  273. void tick_suspend_broadcast(void)
  274. {
  275. struct clock_event_device *bc;
  276. unsigned long flags;
  277. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  278. bc = tick_broadcast_device.evtdev;
  279. if (bc)
  280. clockevents_shutdown(bc);
  281. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  282. }
  283. int tick_resume_broadcast(void)
  284. {
  285. struct clock_event_device *bc;
  286. unsigned long flags;
  287. int broadcast = 0;
  288. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  289. bc = tick_broadcast_device.evtdev;
  290. if (bc) {
  291. clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
  292. switch (tick_broadcast_device.mode) {
  293. case TICKDEV_MODE_PERIODIC:
  294. if (!cpumask_empty(tick_get_broadcast_mask()))
  295. tick_broadcast_start_periodic(bc);
  296. broadcast = cpumask_test_cpu(smp_processor_id(),
  297. tick_get_broadcast_mask());
  298. break;
  299. case TICKDEV_MODE_ONESHOT:
  300. broadcast = tick_resume_broadcast_oneshot(bc);
  301. break;
  302. }
  303. }
  304. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  305. return broadcast;
  306. }
  307. #ifdef CONFIG_TICK_ONESHOT
  308. /* FIXME: use cpumask_var_t. */
  309. static DECLARE_BITMAP(tick_broadcast_oneshot_mask, NR_CPUS);
  310. /*
  311. * Exposed for debugging: see timer_list.c
  312. */
  313. struct cpumask *tick_get_broadcast_oneshot_mask(void)
  314. {
  315. return to_cpumask(tick_broadcast_oneshot_mask);
  316. }
  317. static int tick_broadcast_set_event(ktime_t expires, int force)
  318. {
  319. struct clock_event_device *bc = tick_broadcast_device.evtdev;
  320. return tick_dev_program_event(bc, expires, force);
  321. }
  322. int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
  323. {
  324. clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
  325. return 0;
  326. }
  327. /*
  328. * Called from irq_enter() when idle was interrupted to reenable the
  329. * per cpu device.
  330. */
  331. void tick_check_oneshot_broadcast(int cpu)
  332. {
  333. if (cpumask_test_cpu(cpu, to_cpumask(tick_broadcast_oneshot_mask))) {
  334. struct tick_device *td = &per_cpu(tick_cpu_device, cpu);
  335. clockevents_set_mode(td->evtdev, CLOCK_EVT_MODE_ONESHOT);
  336. }
  337. }
  338. /*
  339. * Handle oneshot mode broadcasting
  340. */
  341. static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
  342. {
  343. struct tick_device *td;
  344. ktime_t now, next_event;
  345. int cpu;
  346. raw_spin_lock(&tick_broadcast_lock);
  347. again:
  348. dev->next_event.tv64 = KTIME_MAX;
  349. next_event.tv64 = KTIME_MAX;
  350. cpumask_clear(to_cpumask(tmpmask));
  351. now = ktime_get();
  352. /* Find all expired events */
  353. for_each_cpu(cpu, tick_get_broadcast_oneshot_mask()) {
  354. td = &per_cpu(tick_cpu_device, cpu);
  355. if (td->evtdev->next_event.tv64 <= now.tv64)
  356. cpumask_set_cpu(cpu, to_cpumask(tmpmask));
  357. else if (td->evtdev->next_event.tv64 < next_event.tv64)
  358. next_event.tv64 = td->evtdev->next_event.tv64;
  359. }
  360. /*
  361. * Wakeup the cpus which have an expired event.
  362. */
  363. tick_do_broadcast(to_cpumask(tmpmask));
  364. /*
  365. * Two reasons for reprogram:
  366. *
  367. * - The global event did not expire any CPU local
  368. * events. This happens in dyntick mode, as the maximum PIT
  369. * delta is quite small.
  370. *
  371. * - There are pending events on sleeping CPUs which were not
  372. * in the event mask
  373. */
  374. if (next_event.tv64 != KTIME_MAX) {
  375. /*
  376. * Rearm the broadcast device. If event expired,
  377. * repeat the above
  378. */
  379. if (tick_broadcast_set_event(next_event, 0))
  380. goto again;
  381. }
  382. raw_spin_unlock(&tick_broadcast_lock);
  383. }
  384. /*
  385. * Powerstate information: The system enters/leaves a state, where
  386. * affected devices might stop
  387. */
  388. void tick_broadcast_oneshot_control(unsigned long reason)
  389. {
  390. struct clock_event_device *bc, *dev;
  391. struct tick_device *td;
  392. unsigned long flags;
  393. int cpu;
  394. /*
  395. * Periodic mode does not care about the enter/exit of power
  396. * states
  397. */
  398. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
  399. return;
  400. /*
  401. * We are called with preemtion disabled from the depth of the
  402. * idle code, so we can't be moved away.
  403. */
  404. cpu = smp_processor_id();
  405. td = &per_cpu(tick_cpu_device, cpu);
  406. dev = td->evtdev;
  407. if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
  408. return;
  409. bc = tick_broadcast_device.evtdev;
  410. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  411. if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
  412. if (!cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
  413. cpumask_set_cpu(cpu, tick_get_broadcast_oneshot_mask());
  414. clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
  415. if (dev->next_event.tv64 < bc->next_event.tv64)
  416. tick_broadcast_set_event(dev->next_event, 1);
  417. }
  418. } else {
  419. if (cpumask_test_cpu(cpu, tick_get_broadcast_oneshot_mask())) {
  420. cpumask_clear_cpu(cpu,
  421. tick_get_broadcast_oneshot_mask());
  422. clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
  423. if (dev->next_event.tv64 != KTIME_MAX)
  424. tick_program_event(dev->next_event, 1);
  425. }
  426. }
  427. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  428. }
  429. /*
  430. * Reset the one shot broadcast for a cpu
  431. *
  432. * Called with tick_broadcast_lock held
  433. */
  434. static void tick_broadcast_clear_oneshot(int cpu)
  435. {
  436. cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
  437. }
  438. static void tick_broadcast_init_next_event(struct cpumask *mask,
  439. ktime_t expires)
  440. {
  441. struct tick_device *td;
  442. int cpu;
  443. for_each_cpu(cpu, mask) {
  444. td = &per_cpu(tick_cpu_device, cpu);
  445. if (td->evtdev)
  446. td->evtdev->next_event = expires;
  447. }
  448. }
  449. /**
  450. * tick_broadcast_setup_oneshot - setup the broadcast device
  451. */
  452. void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
  453. {
  454. int cpu = smp_processor_id();
  455. /* Set it up only once ! */
  456. if (bc->event_handler != tick_handle_oneshot_broadcast) {
  457. int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
  458. bc->event_handler = tick_handle_oneshot_broadcast;
  459. clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
  460. /* Take the do_timer update */
  461. tick_do_timer_cpu = cpu;
  462. /*
  463. * We must be careful here. There might be other CPUs
  464. * waiting for periodic broadcast. We need to set the
  465. * oneshot_mask bits for those and program the
  466. * broadcast device to fire.
  467. */
  468. cpumask_copy(to_cpumask(tmpmask), tick_get_broadcast_mask());
  469. cpumask_clear_cpu(cpu, to_cpumask(tmpmask));
  470. cpumask_or(tick_get_broadcast_oneshot_mask(),
  471. tick_get_broadcast_oneshot_mask(),
  472. to_cpumask(tmpmask));
  473. if (was_periodic && !cpumask_empty(to_cpumask(tmpmask))) {
  474. tick_broadcast_init_next_event(to_cpumask(tmpmask),
  475. tick_next_period);
  476. tick_broadcast_set_event(tick_next_period, 1);
  477. } else
  478. bc->next_event.tv64 = KTIME_MAX;
  479. } else {
  480. /*
  481. * The first cpu which switches to oneshot mode sets
  482. * the bit for all other cpus which are in the general
  483. * (periodic) broadcast mask. So the bit is set and
  484. * would prevent the first broadcast enter after this
  485. * to program the bc device.
  486. */
  487. tick_broadcast_clear_oneshot(cpu);
  488. }
  489. }
  490. /*
  491. * Select oneshot operating mode for the broadcast device
  492. */
  493. void tick_broadcast_switch_to_oneshot(void)
  494. {
  495. struct clock_event_device *bc;
  496. unsigned long flags;
  497. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  498. tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
  499. bc = tick_broadcast_device.evtdev;
  500. if (bc)
  501. tick_broadcast_setup_oneshot(bc);
  502. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  503. }
  504. /*
  505. * Remove a dead CPU from broadcasting
  506. */
  507. void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
  508. {
  509. unsigned long flags;
  510. unsigned int cpu = *cpup;
  511. raw_spin_lock_irqsave(&tick_broadcast_lock, flags);
  512. /*
  513. * Clear the broadcast mask flag for the dead cpu, but do not
  514. * stop the broadcast device!
  515. */
  516. cpumask_clear_cpu(cpu, tick_get_broadcast_oneshot_mask());
  517. raw_spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  518. }
  519. /*
  520. * Check, whether the broadcast device is in one shot mode
  521. */
  522. int tick_broadcast_oneshot_active(void)
  523. {
  524. return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
  525. }
  526. /*
  527. * Check whether the broadcast device supports oneshot.
  528. */
  529. bool tick_broadcast_oneshot_available(void)
  530. {
  531. struct clock_event_device *bc = tick_broadcast_device.evtdev;
  532. return bc ? bc->features & CLOCK_EVT_FEAT_ONESHOT : false;
  533. }
  534. #endif