tick-broadcast.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. struct tick_device tick_broadcast_device;
  28. static cpumask_t tick_broadcast_mask;
  29. static DEFINE_SPINLOCK(tick_broadcast_lock);
  30. static int tick_broadcast_force;
  31. #ifdef CONFIG_TICK_ONESHOT
  32. static void tick_broadcast_clear_oneshot(int cpu);
  33. #else
  34. static inline void tick_broadcast_clear_oneshot(int cpu) { }
  35. #endif
  36. /*
  37. * Debugging: see timer_list.c
  38. */
  39. struct tick_device *tick_get_broadcast_device(void)
  40. {
  41. return &tick_broadcast_device;
  42. }
  43. cpumask_t *tick_get_broadcast_mask(void)
  44. {
  45. return &tick_broadcast_mask;
  46. }
  47. /*
  48. * Start the device in periodic mode
  49. */
  50. static void tick_broadcast_start_periodic(struct clock_event_device *bc)
  51. {
  52. if (bc)
  53. tick_setup_periodic(bc, 1);
  54. }
  55. /*
  56. * Check, if the device can be utilized as broadcast device:
  57. */
  58. int tick_check_broadcast_device(struct clock_event_device *dev)
  59. {
  60. if ((tick_broadcast_device.evtdev &&
  61. tick_broadcast_device.evtdev->rating >= dev->rating) ||
  62. (dev->features & CLOCK_EVT_FEAT_C3STOP))
  63. return 0;
  64. clockevents_exchange_device(NULL, dev);
  65. tick_broadcast_device.evtdev = dev;
  66. if (!cpus_empty(tick_broadcast_mask))
  67. tick_broadcast_start_periodic(dev);
  68. return 1;
  69. }
  70. /*
  71. * Check, if the device is the broadcast device
  72. */
  73. int tick_is_broadcast_device(struct clock_event_device *dev)
  74. {
  75. return (dev && tick_broadcast_device.evtdev == dev);
  76. }
  77. /*
  78. * Check, if the device is disfunctional and a place holder, which
  79. * needs to be handled by the broadcast device.
  80. */
  81. int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
  82. {
  83. unsigned long flags;
  84. int ret = 0;
  85. spin_lock_irqsave(&tick_broadcast_lock, flags);
  86. /*
  87. * Devices might be registered with both periodic and oneshot
  88. * mode disabled. This signals, that the device needs to be
  89. * operated from the broadcast device and is a placeholder for
  90. * the cpu local device.
  91. */
  92. if (!tick_device_is_functional(dev)) {
  93. dev->event_handler = tick_handle_periodic;
  94. cpu_set(cpu, tick_broadcast_mask);
  95. tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
  96. ret = 1;
  97. } else {
  98. /*
  99. * When the new device is not affected by the stop
  100. * feature and the cpu is marked in the broadcast mask
  101. * then clear the broadcast bit.
  102. */
  103. if (!(dev->features & CLOCK_EVT_FEAT_C3STOP)) {
  104. int cpu = smp_processor_id();
  105. cpu_clear(cpu, tick_broadcast_mask);
  106. tick_broadcast_clear_oneshot(cpu);
  107. }
  108. }
  109. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  110. return ret;
  111. }
  112. /*
  113. * Broadcast the event to the cpus, which are set in the mask
  114. */
  115. static void tick_do_broadcast(cpumask_t mask)
  116. {
  117. int cpu = smp_processor_id();
  118. struct tick_device *td;
  119. /*
  120. * Check, if the current cpu is in the mask
  121. */
  122. if (cpu_isset(cpu, mask)) {
  123. cpu_clear(cpu, mask);
  124. td = &per_cpu(tick_cpu_device, cpu);
  125. td->evtdev->event_handler(td->evtdev);
  126. }
  127. if (!cpus_empty(mask)) {
  128. /*
  129. * It might be necessary to actually check whether the devices
  130. * have different broadcast functions. For now, just use the
  131. * one of the first device. This works as long as we have this
  132. * misfeature only on x86 (lapic)
  133. */
  134. cpu = first_cpu(mask);
  135. td = &per_cpu(tick_cpu_device, cpu);
  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. cpumask_t mask;
  146. spin_lock(&tick_broadcast_lock);
  147. cpus_and(mask, cpu_online_map, tick_broadcast_mask);
  148. tick_do_broadcast(mask);
  149. 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 alrady 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(void *why)
  182. {
  183. struct clock_event_device *bc, *dev;
  184. struct tick_device *td;
  185. unsigned long flags, *reason = why;
  186. int cpu, bc_stopped;
  187. 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 = cpus_empty(tick_broadcast_mask);
  200. switch (*reason) {
  201. case CLOCK_EVT_NOTIFY_BROADCAST_ON:
  202. case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
  203. if (!cpu_isset(cpu, tick_broadcast_mask)) {
  204. cpu_set(cpu, tick_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. cpu_isset(cpu, tick_broadcast_mask)) {
  215. cpu_clear(cpu, tick_broadcast_mask);
  216. if (tick_broadcast_device.mode ==
  217. TICKDEV_MODE_PERIODIC)
  218. tick_setup_periodic(dev, 0);
  219. }
  220. break;
  221. }
  222. if (cpus_empty(tick_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. 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 (!cpu_isset(*oncpu, cpu_online_map))
  241. printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
  242. "offline CPU #%d\n", *oncpu);
  243. else
  244. smp_call_function_single(*oncpu, tick_do_broadcast_on_off,
  245. &reason, 1);
  246. }
  247. /*
  248. * Set the periodic handler depending on broadcast on/off
  249. */
  250. void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
  251. {
  252. if (!broadcast)
  253. dev->event_handler = tick_handle_periodic;
  254. else
  255. dev->event_handler = tick_handle_periodic_broadcast;
  256. }
  257. /*
  258. * Remove a CPU from broadcasting
  259. */
  260. void tick_shutdown_broadcast(unsigned int *cpup)
  261. {
  262. struct clock_event_device *bc;
  263. unsigned long flags;
  264. unsigned int cpu = *cpup;
  265. spin_lock_irqsave(&tick_broadcast_lock, flags);
  266. bc = tick_broadcast_device.evtdev;
  267. cpu_clear(cpu, tick_broadcast_mask);
  268. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
  269. if (bc && cpus_empty(tick_broadcast_mask))
  270. clockevents_shutdown(bc);
  271. }
  272. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  273. }
  274. void tick_suspend_broadcast(void)
  275. {
  276. struct clock_event_device *bc;
  277. unsigned long flags;
  278. spin_lock_irqsave(&tick_broadcast_lock, flags);
  279. bc = tick_broadcast_device.evtdev;
  280. if (bc)
  281. clockevents_shutdown(bc);
  282. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  283. }
  284. int tick_resume_broadcast(void)
  285. {
  286. struct clock_event_device *bc;
  287. unsigned long flags;
  288. int broadcast = 0;
  289. spin_lock_irqsave(&tick_broadcast_lock, flags);
  290. bc = tick_broadcast_device.evtdev;
  291. if (bc) {
  292. clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
  293. switch (tick_broadcast_device.mode) {
  294. case TICKDEV_MODE_PERIODIC:
  295. if(!cpus_empty(tick_broadcast_mask))
  296. tick_broadcast_start_periodic(bc);
  297. broadcast = cpu_isset(smp_processor_id(),
  298. tick_broadcast_mask);
  299. break;
  300. case TICKDEV_MODE_ONESHOT:
  301. broadcast = tick_resume_broadcast_oneshot(bc);
  302. break;
  303. }
  304. }
  305. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  306. return broadcast;
  307. }
  308. #ifdef CONFIG_TICK_ONESHOT
  309. static cpumask_t tick_broadcast_oneshot_mask;
  310. /*
  311. * Debugging: see timer_list.c
  312. */
  313. cpumask_t *tick_get_broadcast_oneshot_mask(void)
  314. {
  315. return &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 (cpu_isset(cpu, 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. cpumask_t mask;
  345. ktime_t now, next_event;
  346. int cpu;
  347. spin_lock(&tick_broadcast_lock);
  348. again:
  349. dev->next_event.tv64 = KTIME_MAX;
  350. next_event.tv64 = KTIME_MAX;
  351. mask = CPU_MASK_NONE;
  352. now = ktime_get();
  353. /* Find all expired events */
  354. for_each_cpu_mask_nr(cpu, tick_broadcast_oneshot_mask) {
  355. td = &per_cpu(tick_cpu_device, cpu);
  356. if (td->evtdev->next_event.tv64 <= now.tv64)
  357. cpu_set(cpu, mask);
  358. else if (td->evtdev->next_event.tv64 < next_event.tv64)
  359. next_event.tv64 = td->evtdev->next_event.tv64;
  360. }
  361. /*
  362. * Wakeup the cpus which have an expired event.
  363. */
  364. tick_do_broadcast(mask);
  365. /*
  366. * Two reasons for reprogram:
  367. *
  368. * - The global event did not expire any CPU local
  369. * events. This happens in dyntick mode, as the maximum PIT
  370. * delta is quite small.
  371. *
  372. * - There are pending events on sleeping CPUs which were not
  373. * in the event mask
  374. */
  375. if (next_event.tv64 != KTIME_MAX) {
  376. /*
  377. * Rearm the broadcast device. If event expired,
  378. * repeat the above
  379. */
  380. if (tick_broadcast_set_event(next_event, 0))
  381. goto again;
  382. }
  383. spin_unlock(&tick_broadcast_lock);
  384. }
  385. /*
  386. * Powerstate information: The system enters/leaves a state, where
  387. * affected devices might stop
  388. */
  389. void tick_broadcast_oneshot_control(unsigned long reason)
  390. {
  391. struct clock_event_device *bc, *dev;
  392. struct tick_device *td;
  393. unsigned long flags;
  394. int cpu;
  395. spin_lock_irqsave(&tick_broadcast_lock, flags);
  396. /*
  397. * Periodic mode does not care about the enter/exit of power
  398. * states
  399. */
  400. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
  401. goto out;
  402. bc = tick_broadcast_device.evtdev;
  403. cpu = smp_processor_id();
  404. td = &per_cpu(tick_cpu_device, cpu);
  405. dev = td->evtdev;
  406. if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
  407. goto out;
  408. if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
  409. if (!cpu_isset(cpu, tick_broadcast_oneshot_mask)) {
  410. cpu_set(cpu, tick_broadcast_oneshot_mask);
  411. clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
  412. if (dev->next_event.tv64 < bc->next_event.tv64)
  413. tick_broadcast_set_event(dev->next_event, 1);
  414. }
  415. } else {
  416. if (cpu_isset(cpu, tick_broadcast_oneshot_mask)) {
  417. cpu_clear(cpu, tick_broadcast_oneshot_mask);
  418. clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
  419. if (dev->next_event.tv64 != KTIME_MAX)
  420. tick_program_event(dev->next_event, 1);
  421. }
  422. }
  423. out:
  424. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  425. }
  426. /*
  427. * Reset the one shot broadcast for a cpu
  428. *
  429. * Called with tick_broadcast_lock held
  430. */
  431. static void tick_broadcast_clear_oneshot(int cpu)
  432. {
  433. cpu_clear(cpu, tick_broadcast_oneshot_mask);
  434. }
  435. static void tick_broadcast_init_next_event(cpumask_t *mask, ktime_t expires)
  436. {
  437. struct tick_device *td;
  438. int cpu;
  439. for_each_cpu_mask_nr(cpu, *mask) {
  440. td = &per_cpu(tick_cpu_device, cpu);
  441. if (td->evtdev)
  442. td->evtdev->next_event = expires;
  443. }
  444. }
  445. /**
  446. * tick_broadcast_setup_oneshot - setup the broadcast device
  447. */
  448. void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
  449. {
  450. /* Set it up only once ! */
  451. if (bc->event_handler != tick_handle_oneshot_broadcast) {
  452. int was_periodic = bc->mode == CLOCK_EVT_MODE_PERIODIC;
  453. int cpu = smp_processor_id();
  454. cpumask_t mask;
  455. bc->event_handler = tick_handle_oneshot_broadcast;
  456. clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
  457. /* Take the do_timer update */
  458. tick_do_timer_cpu = cpu;
  459. /*
  460. * We must be careful here. There might be other CPUs
  461. * waiting for periodic broadcast. We need to set the
  462. * oneshot_mask bits for those and program the
  463. * broadcast device to fire.
  464. */
  465. mask = tick_broadcast_mask;
  466. cpu_clear(cpu, mask);
  467. cpus_or(tick_broadcast_oneshot_mask,
  468. tick_broadcast_oneshot_mask, mask);
  469. if (was_periodic && !cpus_empty(mask)) {
  470. tick_broadcast_init_next_event(&mask, tick_next_period);
  471. tick_broadcast_set_event(tick_next_period, 1);
  472. } else
  473. bc->next_event.tv64 = KTIME_MAX;
  474. }
  475. }
  476. /*
  477. * Select oneshot operating mode for the broadcast device
  478. */
  479. void tick_broadcast_switch_to_oneshot(void)
  480. {
  481. struct clock_event_device *bc;
  482. unsigned long flags;
  483. spin_lock_irqsave(&tick_broadcast_lock, flags);
  484. tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
  485. bc = tick_broadcast_device.evtdev;
  486. if (bc)
  487. tick_broadcast_setup_oneshot(bc);
  488. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  489. }
  490. /*
  491. * Remove a dead CPU from broadcasting
  492. */
  493. void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
  494. {
  495. unsigned long flags;
  496. unsigned int cpu = *cpup;
  497. spin_lock_irqsave(&tick_broadcast_lock, flags);
  498. /*
  499. * Clear the broadcast mask flag for the dead cpu, but do not
  500. * stop the broadcast device!
  501. */
  502. cpu_clear(cpu, tick_broadcast_oneshot_mask);
  503. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  504. }
  505. /*
  506. * Check, whether the broadcast device is in one shot mode
  507. */
  508. int tick_broadcast_oneshot_active(void)
  509. {
  510. return tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT;
  511. }
  512. #endif