tick-broadcast.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. tick_do_periodic_broadcast();
  157. /*
  158. * The device is in periodic mode. No reprogramming necessary:
  159. */
  160. if (dev->mode == CLOCK_EVT_MODE_PERIODIC)
  161. return;
  162. /*
  163. * Setup the next period for devices, which do not have
  164. * periodic mode:
  165. */
  166. for (;;) {
  167. ktime_t next = ktime_add(dev->next_event, tick_period);
  168. if (!clockevents_program_event(dev, next, ktime_get()))
  169. return;
  170. tick_do_periodic_broadcast();
  171. }
  172. }
  173. /*
  174. * Powerstate information: The system enters/leaves a state, where
  175. * affected devices might stop
  176. */
  177. static void tick_do_broadcast_on_off(void *why)
  178. {
  179. struct clock_event_device *bc, *dev;
  180. struct tick_device *td;
  181. unsigned long flags, *reason = why;
  182. int cpu;
  183. spin_lock_irqsave(&tick_broadcast_lock, flags);
  184. cpu = smp_processor_id();
  185. td = &per_cpu(tick_cpu_device, cpu);
  186. dev = td->evtdev;
  187. bc = tick_broadcast_device.evtdev;
  188. /*
  189. * Is the device not affected by the powerstate ?
  190. */
  191. if (!dev || !(dev->features & CLOCK_EVT_FEAT_C3STOP))
  192. goto out;
  193. if (!tick_device_is_functional(dev))
  194. goto out;
  195. switch (*reason) {
  196. case CLOCK_EVT_NOTIFY_BROADCAST_ON:
  197. case CLOCK_EVT_NOTIFY_BROADCAST_FORCE:
  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. if (*reason == CLOCK_EVT_NOTIFY_BROADCAST_FORCE)
  205. tick_broadcast_force = 1;
  206. break;
  207. case CLOCK_EVT_NOTIFY_BROADCAST_OFF:
  208. if (!tick_broadcast_force &&
  209. cpu_isset(cpu, tick_broadcast_mask)) {
  210. cpu_clear(cpu, tick_broadcast_mask);
  211. if (td->mode == TICKDEV_MODE_PERIODIC)
  212. tick_setup_periodic(dev, 0);
  213. }
  214. break;
  215. }
  216. if (cpus_empty(tick_broadcast_mask))
  217. clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
  218. else {
  219. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
  220. tick_broadcast_start_periodic(bc);
  221. else
  222. tick_broadcast_setup_oneshot(bc);
  223. }
  224. out:
  225. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  226. }
  227. /*
  228. * Powerstate information: The system enters/leaves a state, where
  229. * affected devices might stop.
  230. */
  231. void tick_broadcast_on_off(unsigned long reason, int *oncpu)
  232. {
  233. if (!cpu_isset(*oncpu, cpu_online_map))
  234. printk(KERN_ERR "tick-broadcast: ignoring broadcast for "
  235. "offline CPU #%d\n", *oncpu);
  236. else
  237. smp_call_function_single(*oncpu, tick_do_broadcast_on_off,
  238. &reason, 1, 1);
  239. }
  240. /*
  241. * Set the periodic handler depending on broadcast on/off
  242. */
  243. void tick_set_periodic_handler(struct clock_event_device *dev, int broadcast)
  244. {
  245. if (!broadcast)
  246. dev->event_handler = tick_handle_periodic;
  247. else
  248. dev->event_handler = tick_handle_periodic_broadcast;
  249. }
  250. /*
  251. * Remove a CPU from broadcasting
  252. */
  253. void tick_shutdown_broadcast(unsigned int *cpup)
  254. {
  255. struct clock_event_device *bc;
  256. unsigned long flags;
  257. unsigned int cpu = *cpup;
  258. spin_lock_irqsave(&tick_broadcast_lock, flags);
  259. bc = tick_broadcast_device.evtdev;
  260. cpu_clear(cpu, tick_broadcast_mask);
  261. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC) {
  262. if (bc && cpus_empty(tick_broadcast_mask))
  263. clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
  264. }
  265. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  266. }
  267. void tick_suspend_broadcast(void)
  268. {
  269. struct clock_event_device *bc;
  270. unsigned long flags;
  271. spin_lock_irqsave(&tick_broadcast_lock, flags);
  272. bc = tick_broadcast_device.evtdev;
  273. if (bc)
  274. clockevents_set_mode(bc, CLOCK_EVT_MODE_SHUTDOWN);
  275. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  276. }
  277. int tick_resume_broadcast(void)
  278. {
  279. struct clock_event_device *bc;
  280. unsigned long flags;
  281. int broadcast = 0;
  282. spin_lock_irqsave(&tick_broadcast_lock, flags);
  283. bc = tick_broadcast_device.evtdev;
  284. if (bc) {
  285. clockevents_set_mode(bc, CLOCK_EVT_MODE_RESUME);
  286. switch (tick_broadcast_device.mode) {
  287. case TICKDEV_MODE_PERIODIC:
  288. if(!cpus_empty(tick_broadcast_mask))
  289. tick_broadcast_start_periodic(bc);
  290. broadcast = cpu_isset(smp_processor_id(),
  291. tick_broadcast_mask);
  292. break;
  293. case TICKDEV_MODE_ONESHOT:
  294. broadcast = tick_resume_broadcast_oneshot(bc);
  295. break;
  296. }
  297. }
  298. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  299. return broadcast;
  300. }
  301. #ifdef CONFIG_TICK_ONESHOT
  302. static cpumask_t tick_broadcast_oneshot_mask;
  303. /*
  304. * Debugging: see timer_list.c
  305. */
  306. cpumask_t *tick_get_broadcast_oneshot_mask(void)
  307. {
  308. return &tick_broadcast_oneshot_mask;
  309. }
  310. static int tick_broadcast_set_event(ktime_t expires, int force)
  311. {
  312. struct clock_event_device *bc = tick_broadcast_device.evtdev;
  313. ktime_t now = ktime_get();
  314. int res;
  315. for(;;) {
  316. res = clockevents_program_event(bc, expires, now);
  317. if (!res || !force)
  318. return res;
  319. now = ktime_get();
  320. expires = ktime_add(now, ktime_set(0, bc->min_delta_ns));
  321. }
  322. }
  323. int tick_resume_broadcast_oneshot(struct clock_event_device *bc)
  324. {
  325. clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
  326. return 0;
  327. }
  328. /*
  329. * Handle oneshot mode broadcasting
  330. */
  331. static void tick_handle_oneshot_broadcast(struct clock_event_device *dev)
  332. {
  333. struct tick_device *td;
  334. cpumask_t mask;
  335. ktime_t now, next_event;
  336. int cpu;
  337. spin_lock(&tick_broadcast_lock);
  338. again:
  339. dev->next_event.tv64 = KTIME_MAX;
  340. next_event.tv64 = KTIME_MAX;
  341. mask = CPU_MASK_NONE;
  342. now = ktime_get();
  343. /* Find all expired events */
  344. for (cpu = first_cpu(tick_broadcast_oneshot_mask); cpu != NR_CPUS;
  345. cpu = next_cpu(cpu, tick_broadcast_oneshot_mask)) {
  346. td = &per_cpu(tick_cpu_device, cpu);
  347. if (td->evtdev->next_event.tv64 <= now.tv64)
  348. cpu_set(cpu, mask);
  349. else if (td->evtdev->next_event.tv64 < next_event.tv64)
  350. next_event.tv64 = td->evtdev->next_event.tv64;
  351. }
  352. /*
  353. * Wakeup the cpus which have an expired event.
  354. */
  355. tick_do_broadcast(mask);
  356. /*
  357. * Two reasons for reprogram:
  358. *
  359. * - The global event did not expire any CPU local
  360. * events. This happens in dyntick mode, as the maximum PIT
  361. * delta is quite small.
  362. *
  363. * - There are pending events on sleeping CPUs which were not
  364. * in the event mask
  365. */
  366. if (next_event.tv64 != KTIME_MAX) {
  367. /*
  368. * Rearm the broadcast device. If event expired,
  369. * repeat the above
  370. */
  371. if (tick_broadcast_set_event(next_event, 0))
  372. goto again;
  373. }
  374. spin_unlock(&tick_broadcast_lock);
  375. }
  376. /*
  377. * Powerstate information: The system enters/leaves a state, where
  378. * affected devices might stop
  379. */
  380. void tick_broadcast_oneshot_control(unsigned long reason)
  381. {
  382. struct clock_event_device *bc, *dev;
  383. struct tick_device *td;
  384. unsigned long flags;
  385. int cpu;
  386. spin_lock_irqsave(&tick_broadcast_lock, flags);
  387. /*
  388. * Periodic mode does not care about the enter/exit of power
  389. * states
  390. */
  391. if (tick_broadcast_device.mode == TICKDEV_MODE_PERIODIC)
  392. goto out;
  393. bc = tick_broadcast_device.evtdev;
  394. cpu = smp_processor_id();
  395. td = &per_cpu(tick_cpu_device, cpu);
  396. dev = td->evtdev;
  397. if (!(dev->features & CLOCK_EVT_FEAT_C3STOP))
  398. goto out;
  399. if (reason == CLOCK_EVT_NOTIFY_BROADCAST_ENTER) {
  400. if (!cpu_isset(cpu, tick_broadcast_oneshot_mask)) {
  401. cpu_set(cpu, tick_broadcast_oneshot_mask);
  402. clockevents_set_mode(dev, CLOCK_EVT_MODE_SHUTDOWN);
  403. if (dev->next_event.tv64 < bc->next_event.tv64)
  404. tick_broadcast_set_event(dev->next_event, 1);
  405. }
  406. } else {
  407. if (cpu_isset(cpu, tick_broadcast_oneshot_mask)) {
  408. cpu_clear(cpu, tick_broadcast_oneshot_mask);
  409. clockevents_set_mode(dev, CLOCK_EVT_MODE_ONESHOT);
  410. if (dev->next_event.tv64 != KTIME_MAX)
  411. tick_program_event(dev->next_event, 1);
  412. }
  413. }
  414. out:
  415. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  416. }
  417. /*
  418. * Reset the one shot broadcast for a cpu
  419. *
  420. * Called with tick_broadcast_lock held
  421. */
  422. static void tick_broadcast_clear_oneshot(int cpu)
  423. {
  424. cpu_clear(cpu, tick_broadcast_oneshot_mask);
  425. }
  426. /**
  427. * tick_broadcast_setup_oneshot - setup the broadcast device
  428. */
  429. void tick_broadcast_setup_oneshot(struct clock_event_device *bc)
  430. {
  431. bc->event_handler = tick_handle_oneshot_broadcast;
  432. clockevents_set_mode(bc, CLOCK_EVT_MODE_ONESHOT);
  433. bc->next_event.tv64 = KTIME_MAX;
  434. }
  435. /*
  436. * Select oneshot operating mode for the broadcast device
  437. */
  438. void tick_broadcast_switch_to_oneshot(void)
  439. {
  440. struct clock_event_device *bc;
  441. unsigned long flags;
  442. spin_lock_irqsave(&tick_broadcast_lock, flags);
  443. tick_broadcast_device.mode = TICKDEV_MODE_ONESHOT;
  444. bc = tick_broadcast_device.evtdev;
  445. if (bc)
  446. tick_broadcast_setup_oneshot(bc);
  447. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  448. }
  449. /*
  450. * Remove a dead CPU from broadcasting
  451. */
  452. void tick_shutdown_broadcast_oneshot(unsigned int *cpup)
  453. {
  454. unsigned long flags;
  455. unsigned int cpu = *cpup;
  456. spin_lock_irqsave(&tick_broadcast_lock, flags);
  457. /*
  458. * Clear the broadcast mask flag for the dead cpu, but do not
  459. * stop the broadcast device!
  460. */
  461. cpu_clear(cpu, tick_broadcast_oneshot_mask);
  462. spin_unlock_irqrestore(&tick_broadcast_lock, flags);
  463. }
  464. #endif