tick-broadcast.c 15 KB

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