tick-broadcast.c 13 KB

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