tick-broadcast.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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/irq.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. /*
  31. * Debugging: see timer_list.c
  32. */
  33. struct tick_device *tick_get_broadcast_device(void)
  34. {
  35. return &tick_broadcast_device;
  36. }
  37. cpumask_t *tick_get_broadcast_mask(void)
  38. {
  39. return &tick_broadcast_mask;
  40. }
  41. /*
  42. * Start the device in periodic mode
  43. */
  44. static void tick_broadcast_start_periodic(struct clock_event_device *bc)
  45. {
  46. if (bc && bc->mode == CLOCK_EVT_MODE_SHUTDOWN)
  47. tick_setup_periodic(bc, 1);
  48. }
  49. /*
  50. * Check, if the device can be utilized as broadcast device:
  51. */
  52. int tick_check_broadcast_device(struct clock_event_device *dev)
  53. {
  54. if (tick_broadcast_device.evtdev ||
  55. (dev->features & CLOCK_EVT_FEAT_C3STOP))
  56. return 0;
  57. clockevents_exchange_device(NULL, dev);
  58. tick_broadcast_device.evtdev = dev;
  59. if (!cpus_empty(tick_broadcast_mask))
  60. tick_broadcast_start_periodic(dev);
  61. return 1;
  62. }
  63. /*
  64. * Check, if the device is the broadcast device
  65. */
  66. int tick_is_broadcast_device(struct clock_event_device *dev)
  67. {
  68. return (dev && tick_broadcast_device.evtdev == dev);
  69. }
  70. /*
  71. * Check, if the device is disfunctional and a place holder, which
  72. * needs to be handled by the broadcast device.
  73. */
  74. int tick_device_uses_broadcast(struct clock_event_device *dev, int cpu)
  75. {
  76. unsigned long flags;
  77. int ret = 0;
  78. spin_lock_irqsave(&tick_broadcast_lock, flags);
  79. /*
  80. * Devices might be registered with both periodic and oneshot
  81. * mode disabled. This signals, that the device needs to be
  82. * operated from the broadcast device and is a placeholder for
  83. * the cpu local device.
  84. */
  85. if (!tick_device_is_functional(dev)) {
  86. dev->event_handler = tick_handle_periodic;
  87. cpu_set(cpu, tick_broadcast_mask);
  88. tick_broadcast_start_periodic(tick_broadcast_device.evtdev);
  89. ret = 1;
  90. }
  91. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  92. return ret;
  93. }
  94. /*
  95. * Broadcast the event to the cpus, which are set in the mask
  96. */
  97. int tick_do_broadcast(cpumask_t mask)
  98. {
  99. int ret = 0, cpu = smp_processor_id();
  100. struct tick_device *td;
  101. /*
  102. * Check, if the current cpu is in the mask
  103. */
  104. if (cpu_isset(cpu, mask)) {
  105. cpu_clear(cpu, mask);
  106. td = &per_cpu(tick_cpu_device, cpu);
  107. td->evtdev->event_handler(td->evtdev);
  108. ret = 1;
  109. }
  110. if (!cpus_empty(mask)) {
  111. /*
  112. * It might be necessary to actually check whether the devices
  113. * have different broadcast functions. For now, just use the
  114. * one of the first device. This works as long as we have this
  115. * misfeature only on x86 (lapic)
  116. */
  117. cpu = first_cpu(mask);
  118. td = &per_cpu(tick_cpu_device, cpu);
  119. td->evtdev->broadcast(mask);
  120. ret = 1;
  121. }
  122. return ret;
  123. }
  124. /*
  125. * Periodic broadcast:
  126. * - invoke the broadcast handlers
  127. */
  128. static void tick_do_periodic_broadcast(void)
  129. {
  130. cpumask_t mask;
  131. spin_lock(&tick_broadcast_lock);
  132. cpus_and(mask, cpu_online_map, tick_broadcast_mask);
  133. tick_do_broadcast(mask);
  134. spin_unlock(&tick_broadcast_lock);
  135. }
  136. /*
  137. * Event handler for periodic broadcast ticks
  138. */
  139. static void tick_handle_periodic_broadcast(struct clock_event_device *dev)
  140. {
  141. dev->next_event.tv64 = KTIME_MAX;
  142. tick_do_periodic_broadcast();
  143. /*
  144. * The device is in periodic mode. No reprogramming necessary:
  145. */
  146. if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
  147. return;
  148. /*
  149. * Setup the next period for devices, which do not have
  150. * periodic mode:
  151. */
  152. for (;;) {
  153. ktime_t next = ktime_add(dev->next_event, tick_period);
  154. if (!clockevents_program_event(dev, next, ktime_get()))
  155. return;
  156. tick_do_periodic_broadcast();
  157. }
  158. }
  159. /*
  160. * Powerstate information: The system enters/leaves a state, where
  161. * affected devices might stop
  162. */
  163. static void tick_do_broadcast_on_off(void *why)
  164. {
  165. struct clock_event_device *bc, *dev;
  166. struct tick_device *td;
  167. unsigned long flags, *reason = why;
  168. int cpu;
  169. spin_lock_irqsave(&tick_broadcast_lock, flags);
  170. cpu = smp_processor_id();
  171. td = &per_cpu(tick_cpu_device, cpu);
  172. dev = td->evtdev;
  173. bc = tick_broadcast_device.evtdev;
  174. /*
  175. * Is the device in broadcast mode forever or is it not
  176. * affected by the powerstate ?
  177. */
  178. if (!dev || !tick_device_is_functional(dev) ||
  179. !(dev->features & CLOCK_EVT_FEAT_C3STOP))
  180. goto out;
  181. if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_ON) {
  182. if (!cpu_isset(cpu, tick_broadcast_mask)) {
  183. cpu_set(cpu, tick_broadcast_mask);
  184. if (td->mode == TICKDEV_MODE_PERIODIC)
  185. clockevents_set_mode(dev,
  186. CLOCK_EVT_MODE_SHUTDOWN);
  187. }
  188. } else {
  189. if (cpu_isset(cpu, tick_broadcast_mask)) {
  190. cpu_clear(cpu, tick_broadcast_mask);
  191. if (td->mode == TICKDEV_MODE_PERIODIC)
  192. tick_setup_periodic(dev, 0);
  193. }
  194. }
  195. if (cpus_empty(tick_broadcast_mask))
  196. clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
  197. else {
  198. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
  199. tick_broadcast_start_periodic(bc);
  200. else
  201. tick_broadcast_setup_oneshot(bc);
  202. }
  203. out:
  204. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  205. }
  206. /*
  207. * Powerstate information: The system enters/leaves a state, where
  208. * affected devices might stop.
  209. */
  210. void tick_broadcast_on_off(unsigned long reason, int *oncpu)
  211. {
  212. int cpu = get_cpu();
  213. if (!cpu_isset(*oncpu, cpu_online_map)) {
  214. printk(KERN_ERR "tick-braodcast: ignoring broadcast for "
  215. "offline CPU #%d\n", *oncpu);
  216. } else {
  217. if (cpu == *oncpu)
  218. tick_do_broadcast_on_off(&reason);
  219. else
  220. smp_call_function_single(*oncpu,
  221. tick_do_broadcast_on_off,
  222. &reason, 1, 1);
  223. }
  224. put_cpu();
  225. }
  226. /*
  227. * Set the periodic handler depending on broadcast on/off
  228. */
  229. void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
  230. {
  231. if (!broadcast)
  232. dev->event_handler = tick_handle_periodic;
  233. else
  234. dev->event_handler = tick_handle_periodic_broadcast;
  235. }
  236. /*
  237. * Remove a CPU from broadcasting
  238. */
  239. void tick_shutdown_broadcast(unsigned int *cpup)
  240. {
  241. struct clock_event_device *bc;
  242. unsigned long flags;
  243. unsigned int cpu = *cpup;
  244. spin_lock_irqsave(&tick_broadcast_lock, flags);
  245. bc = tick_broadcast_device.evtdev;
  246. cpu_clear(cpu, tick_broadcast_mask);
  247. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
  248. if (bc && cpus_empty(tick_broadcast_mask))
  249. clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
  250. }
  251. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  252. }
  253. void tick_suspend_broadcast(void)
  254. {
  255. struct clock_event_device *bc;
  256. unsigned long flags;
  257. spin_lock_irqsave(&tick_broadcast_lock, flags);
  258. bc = tick_broadcast_device.evtdev;
  259. if (bc && tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
  260. clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
  261. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  262. }
  263. int tick_resume_broadcast(void)
  264. {
  265. struct clock_event_device *bc;
  266. unsigned long flags;
  267. int broadcast = 0;
  268. spin_lock_irqsave(&tick_broadcast_lock, flags);
  269. bc = tick_broadcast_device.evtdev;
  270. if (bc) {
  271. switch (tick_broadcast_device.mode) {
  272. case TICKDEV_MODE_PERIODIC:
  273. if(!cpus_empty(tick_broadcast_mask))
  274. tick_broadcast_start_periodic(bc);
  275. broadcast = cpu_isset(smp_processor_id(),
  276. tick_broadcast_mask);
  277. break;
  278. case TICKDEV_MODE_ONESHOT:
  279. broadcast = tick_resume_broadcast_oneshot(bc);
  280. break;
  281. }
  282. }
  283. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  284. return broadcast;
  285. }
  286. #ifdef CONFIG_TICK_ONESHOT
  287. static cpumask_t tick_broadcast_oneshot_mask;
  288. /*
  289. * Debugging: see timer_list.c
  290. */
  291. cpumask_t *tick_get_broadcast_oneshot_mask(void)
  292. {
  293. return &tick_broadcast_oneshot_mask;
  294. }
  295. static int tick_broadcast_set_event(ktime_t expires, int force)
  296. {
  297. struct clock_event_device *bc = tick_broadcast_device.evtdev;
  298. ktime_t now = ktime_get();
  299. int res;
  300. for(;;) {
  301. res = clockevents_program_event(bc, expires, now);
  302. if (!res || !force)
  303. return res;
  304. now = ktime_get();
  305. expires = ktime_add(now, ktime_set(0, bc->min_delta_ns));
  306. }
  307. }
  308. int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
  309. {
  310. clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
  311. if(!cpus_empty(tick_broadcast_oneshot_mask))
  312. tick_broadcast_set_event(ktime_get(), 1);
  313. return cpu_isset(smp_processor_id(), tick_broadcast_oneshot_mask);
  314. }
  315. /*
  316. * Reprogram the broadcast device:
  317. *
  318. * Called with tick_broadcast_lock held and interrupts disabled.
  319. */
  320. static int tick_broadcast_reprogram(void)
  321. {
  322. ktime_t expires = { .tv64 = KTIME_MAX };
  323. struct tick_device *td;
  324. int cpu;
  325. /*
  326. * Find the event which expires next:
  327. */
  328. for (cpu = first_cpu(tick_broadcast_oneshot_mask); cpu != NR_CPUS;
  329. cpu = next_cpu(cpu, tick_broadcast_oneshot_mask)) {
  330. td = &per_cpu(tick_cpu_device, cpu);
  331. if (td->evtdev->next_event.tv64 < expires.tv64)
  332. expires = td->evtdev->next_event;
  333. }
  334. if (expires.tv64 == KTIME_MAX)
  335. return 0;
  336. return tick_broadcast_set_event(expires, 0);
  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;
  346. int cpu;
  347. spin_lock(&tick_broadcast_lock);
  348. again:
  349. dev->next_event.tv64 = KTIME_MAX;
  350. mask = CPU_MASK_NONE;
  351. now = ktime_get();
  352. /* Find all expired events */
  353. for (cpu = first_cpu(tick_broadcast_oneshot_mask); cpu != NR_CPUS;
  354. cpu = next_cpu(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. }
  359. /*
  360. * Wakeup the cpus which have an expired event. The broadcast
  361. * device is reprogrammed in the return from idle code.
  362. */
  363. if (!tick_do_broadcast(mask)) {
  364. /*
  365. * The global event did not expire any CPU local
  366. * events. This happens in dyntick mode, as the
  367. * maximum PIT delta is quite small.
  368. */
  369. if (tick_broadcast_reprogram())
  370. goto again;
  371. }
  372. spin_unlock(&tick_broadcast_lock);
  373. }
  374. /*
  375. * Powerstate information: The system enters/leaves a state, where
  376. * affected devices might stop
  377. */
  378. void tick_broadcast_oneshot_control(unsigned long reason)
  379. {
  380. struct clock_event_device *bc, *dev;
  381. struct tick_device *td;
  382. unsigned long flags;
  383. int cpu;
  384. spin_lock_irqsave(&tick_broadcast_lock, flags);
  385. /*
  386. * Periodic mode does not care about the enter/exit of power
  387. * states
  388. */
  389. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
  390. goto out;
  391. bc = tick_broadcast_device.evtdev;
  392. cpu = smp_processor_id();
  393. td = &per_cpu(tick_cpu_device, cpu);
  394. dev = td->evtdev;
  395. if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
  396. goto out;
  397. if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
  398. if (!cpu_isset(cpu, tick_broadcast_oneshot_mask)) {
  399. cpu_set(cpu, tick_broadcast_oneshot_mask);
  400. clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
  401. if (dev->next_event.tv64 < bc->next_event.tv64)
  402. tick_broadcast_set_event(dev->next_event, 1);
  403. }
  404. } else {
  405. if (cpu_isset(cpu, tick_broadcast_oneshot_mask)) {
  406. cpu_clear(cpu, tick_broadcast_oneshot_mask);
  407. clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
  408. if (dev->next_event.tv64 != KTIME_MAX)
  409. tick_program_event(dev->next_event, 1);
  410. }
  411. }
  412. out:
  413. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  414. }
  415. /**
  416. * tick_broadcast_setup_highres - setup the broadcast device for highres
  417. */
  418. void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
  419. {
  420. if (bc->mode != CLOCK_EVT_MODE_ONESHOT) {
  421. bc->event_handler = tick_handle_oneshot_broadcast;
  422. clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
  423. bc->next_event.tv64 = KTIME_MAX;
  424. }
  425. }
  426. /*
  427. * Select oneshot operating mode for the broadcast device
  428. */
  429. void tick_broadcast_switch_to_oneshot(void)
  430. {
  431. struct clock_event_device *bc;
  432. unsigned long flags;
  433. spin_lock_irqsave(&tick_broadcast_lock, flags);
  434. tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
  435. bc = tick_broadcast_device.evtdev;
  436. if (bc)
  437. tick_broadcast_setup_oneshot(bc);
  438. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  439. }
  440. /*
  441. * Remove a dead CPU from broadcasting
  442. */
  443. void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
  444. {
  445. struct clock_event_device *bc;
  446. unsigned long flags;
  447. unsigned int cpu = *cpup;
  448. spin_lock_irqsave(&tick_broadcast_lock, flags);
  449. bc = tick_broadcast_device.evtdev;
  450. cpu_clear(cpu, tick_broadcast_oneshot_mask);
  451. if (tick_broadcast_device.mode == TICKDEV_MODE_ONESHOT) {
  452. if (bc && cpus_empty(tick_broadcast_oneshot_mask))
  453. clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
  454. }
  455. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  456. }
  457. #endif