jiffies.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * This file contains the jiffies based clocksource.
  4. *
  5. * Copyright (C) 2004, 2005 IBM, John Stultz (johnstul@us.ibm.com)
  6. */
  7. #include <linux/clocksource.h>
  8. #include <linux/jiffies.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include "timekeeping.h"
  12. /* Since jiffies uses a simple TICK_NSEC multiplier
  13. * conversion, the .shift value could be zero. However
  14. * this would make NTP adjustments impossible as they are
  15. * in units of 1/2^.shift. Thus we use JIFFIES_SHIFT to
  16. * shift both the nominator and denominator the same
  17. * amount, and give ntp adjustments in units of 1/2^8
  18. *
  19. * The value 8 is somewhat carefully chosen, as anything
  20. * larger can result in overflows. TICK_NSEC grows as HZ
  21. * shrinks, so values greater than 8 overflow 32bits when
  22. * HZ=100.
  23. */
  24. #if HZ < 34
  25. #define JIFFIES_SHIFT 6
  26. #elif HZ < 67
  27. #define JIFFIES_SHIFT 7
  28. #else
  29. #define JIFFIES_SHIFT 8
  30. #endif
  31. static u64 jiffies_read(struct clocksource *cs)
  32. {
  33. return (u64) jiffies;
  34. }
  35. /*
  36. * The Jiffies based clocksource is the lowest common
  37. * denominator clock source which should function on
  38. * all systems. It has the same coarse resolution as
  39. * the timer interrupt frequency HZ and it suffers
  40. * inaccuracies caused by missed or lost timer
  41. * interrupts and the inability for the timer
  42. * interrupt hardware to accuratly tick at the
  43. * requested HZ value. It is also not recommended
  44. * for "tick-less" systems.
  45. */
  46. static struct clocksource clocksource_jiffies = {
  47. .name = "jiffies",
  48. .rating = 1, /* lowest valid rating*/
  49. .read = jiffies_read,
  50. .mask = CLOCKSOURCE_MASK(32),
  51. .mult = TICK_NSEC << JIFFIES_SHIFT, /* details above */
  52. .shift = JIFFIES_SHIFT,
  53. .max_cycles = 10,
  54. };
  55. __cacheline_aligned_in_smp DEFINE_RAW_SPINLOCK(jiffies_lock);
  56. __cacheline_aligned_in_smp seqcount_t jiffies_seq;
  57. #if (BITS_PER_LONG < 64)
  58. u64 get_jiffies_64(void)
  59. {
  60. unsigned int seq;
  61. u64 ret;
  62. do {
  63. seq = read_seqcount_begin(&jiffies_seq);
  64. ret = jiffies_64;
  65. } while (read_seqcount_retry(&jiffies_seq, seq));
  66. return ret;
  67. }
  68. EXPORT_SYMBOL(get_jiffies_64);
  69. #endif
  70. EXPORT_SYMBOL(jiffies);
  71. static int __init init_jiffies_clocksource(void)
  72. {
  73. return __clocksource_register(&clocksource_jiffies);
  74. }
  75. core_initcall(init_jiffies_clocksource);
  76. struct clocksource * __init __weak clocksource_default_clock(void)
  77. {
  78. return &clocksource_jiffies;
  79. }
  80. static struct clocksource refined_jiffies;
  81. int register_refined_jiffies(long cycles_per_second)
  82. {
  83. u64 nsec_per_tick, shift_hz;
  84. long cycles_per_tick;
  85. refined_jiffies = clocksource_jiffies;
  86. refined_jiffies.name = "refined-jiffies";
  87. refined_jiffies.rating++;
  88. /* Calc cycles per tick */
  89. cycles_per_tick = (cycles_per_second + HZ/2)/HZ;
  90. /* shift_hz stores hz<<8 for extra accuracy */
  91. shift_hz = (u64)cycles_per_second << 8;
  92. shift_hz += cycles_per_tick/2;
  93. do_div(shift_hz, cycles_per_tick);
  94. /* Calculate nsec_per_tick using shift_hz */
  95. nsec_per_tick = (u64)NSEC_PER_SEC << 8;
  96. nsec_per_tick += (u32)shift_hz/2;
  97. do_div(nsec_per_tick, (u32)shift_hz);
  98. refined_jiffies.mult = ((u32)nsec_per_tick) << JIFFIES_SHIFT;
  99. __clocksource_register(&refined_jiffies);
  100. return 0;
  101. }