tick-broadcast.c 13 KB

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