tick-broadcast-hrtimer.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * linux/kernel/time/tick-broadcast-hrtimer.c
  3. * This file emulates a local clock event device
  4. * via a pseudo clock device.
  5. */
  6. #include <linux/cpu.h>
  7. #include <linux/err.h>
  8. #include <linux/hrtimer.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/percpu.h>
  11. #include <linux/profile.h>
  12. #include <linux/clockchips.h>
  13. #include <linux/sched.h>
  14. #include <linux/smp.h>
  15. #include <linux/module.h>
  16. #include "tick-internal.h"
  17. static struct hrtimer bctimer;
  18. static void bc_set_mode(enum clock_event_mode mode,
  19. struct clock_event_device *bc)
  20. {
  21. switch (mode) {
  22. case CLOCK_EVT_MODE_SHUTDOWN:
  23. /*
  24. * Note, we cannot cancel the timer here as we might
  25. * run into the following live lock scenario:
  26. *
  27. * cpu 0 cpu1
  28. * lock(broadcast_lock);
  29. * hrtimer_interrupt()
  30. * bc_handler()
  31. * tick_handle_oneshot_broadcast();
  32. * lock(broadcast_lock);
  33. * hrtimer_cancel()
  34. * wait_for_callback()
  35. */
  36. hrtimer_try_to_cancel(&bctimer);
  37. break;
  38. default:
  39. break;
  40. }
  41. }
  42. /*
  43. * This is called from the guts of the broadcast code when the cpu
  44. * which is about to enter idle has the earliest broadcast timer event.
  45. */
  46. static int bc_set_next(ktime_t expires, struct clock_event_device *bc)
  47. {
  48. /*
  49. * We try to cancel the timer first. If the callback is on
  50. * flight on some other cpu then we let it handle it. If we
  51. * were able to cancel the timer nothing can rearm it as we
  52. * own broadcast_lock.
  53. *
  54. * However we can also be called from the event handler of
  55. * ce_broadcast_hrtimer itself when it expires. We cannot
  56. * restart the timer because we are in the callback, but we
  57. * can set the expiry time and let the callback return
  58. * HRTIMER_RESTART.
  59. */
  60. if (hrtimer_try_to_cancel(&bctimer) >= 0) {
  61. hrtimer_start(&bctimer, expires, HRTIMER_MODE_ABS_PINNED);
  62. /* Bind the "device" to the cpu */
  63. bc->bound_on = smp_processor_id();
  64. } else if (bc->bound_on == smp_processor_id()) {
  65. hrtimer_set_expires(&bctimer, expires);
  66. }
  67. return 0;
  68. }
  69. static struct clock_event_device ce_broadcast_hrtimer = {
  70. .set_mode = bc_set_mode,
  71. .set_next_ktime = bc_set_next,
  72. .features = CLOCK_EVT_FEAT_ONESHOT |
  73. CLOCK_EVT_FEAT_KTIME |
  74. CLOCK_EVT_FEAT_HRTIMER,
  75. .rating = 0,
  76. .bound_on = -1,
  77. .min_delta_ns = 1,
  78. .max_delta_ns = KTIME_MAX,
  79. .min_delta_ticks = 1,
  80. .max_delta_ticks = ULONG_MAX,
  81. .mult = 1,
  82. .shift = 0,
  83. .cpumask = cpu_all_mask,
  84. };
  85. static enum hrtimer_restart bc_handler(struct hrtimer *t)
  86. {
  87. ce_broadcast_hrtimer.event_handler(&ce_broadcast_hrtimer);
  88. if (ce_broadcast_hrtimer.next_event.tv64 == KTIME_MAX)
  89. return HRTIMER_NORESTART;
  90. return HRTIMER_RESTART;
  91. }
  92. void tick_setup_hrtimer_broadcast(void)
  93. {
  94. hrtimer_init(&bctimer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
  95. bctimer.function = bc_handler;
  96. clockevents_register_device(&ce_broadcast_hrtimer);
  97. }