timex.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1998, 1999, 2003 by Ralf Baechle
  7. * Copyright (C) 2014 by Maciej W. Rozycki
  8. */
  9. #ifndef _ASM_TIMEX_H
  10. #define _ASM_TIMEX_H
  11. #ifdef __KERNEL__
  12. #include <linux/compiler.h>
  13. #include <asm/cpu.h>
  14. #include <asm/cpu-features.h>
  15. #include <asm/mipsregs.h>
  16. #include <asm/cpu-type.h>
  17. /*
  18. * This is the clock rate of the i8253 PIT. A MIPS system may not have
  19. * a PIT by the symbol is used all over the kernel including some APIs.
  20. * So keeping it defined to the number for the PIT is the only sane thing
  21. * for now.
  22. */
  23. #define CLOCK_TICK_RATE 1193182
  24. /*
  25. * Standard way to access the cycle counter.
  26. * Currently only used on SMP for scheduling.
  27. *
  28. * Only the low 32 bits are available as a continuously counting entity.
  29. * But this only means we'll force a reschedule every 8 seconds or so,
  30. * which isn't an evil thing.
  31. *
  32. * We know that all SMP capable CPUs have cycle counters.
  33. */
  34. typedef unsigned int cycles_t;
  35. /*
  36. * On R4000/R4400 before version 5.0 an erratum exists such that if the
  37. * cycle counter is read in the exact moment that it is matching the
  38. * compare register, no interrupt will be generated.
  39. *
  40. * There is a suggested workaround and also the erratum can't strike if
  41. * the compare interrupt isn't being used as the clock source device.
  42. * However for now the implementaton of this function doesn't get these
  43. * fine details right.
  44. */
  45. static inline int can_use_mips_counter(unsigned int prid)
  46. {
  47. int comp = (prid & PRID_COMP_MASK) != PRID_COMP_LEGACY;
  48. if (__builtin_constant_p(cpu_has_counter) && !cpu_has_counter)
  49. return 0;
  50. else if (__builtin_constant_p(cpu_has_mips_r) && cpu_has_mips_r)
  51. return 1;
  52. else if (likely(!__builtin_constant_p(cpu_has_mips_r) && comp))
  53. return 1;
  54. /* Make sure we don't peek at cpu_data[0].options in the fast path! */
  55. if (!__builtin_constant_p(cpu_has_counter))
  56. asm volatile("" : "=m" (cpu_data[0].options));
  57. if (likely(cpu_has_counter &&
  58. prid >= (PRID_IMP_R4000 | PRID_REV_ENCODE_44(5, 0))))
  59. return 1;
  60. else
  61. return 0;
  62. }
  63. static inline cycles_t get_cycles(void)
  64. {
  65. if (can_use_mips_counter(read_c0_prid()))
  66. return read_c0_count();
  67. else
  68. return 0; /* no usable counter */
  69. }
  70. /*
  71. * Like get_cycles - but where c0_count is not available we desperately
  72. * use c0_random in an attempt to get at least a little bit of entropy.
  73. *
  74. * R6000 and R6000A neither have a count register nor a random register.
  75. * That leaves no entropy source in the CPU itself.
  76. */
  77. static inline unsigned long random_get_entropy(void)
  78. {
  79. unsigned int prid = read_c0_prid();
  80. unsigned int imp = prid & PRID_IMP_MASK;
  81. if (can_use_mips_counter(prid))
  82. return read_c0_count();
  83. else if (likely(imp != PRID_IMP_R6000 && imp != PRID_IMP_R6000A))
  84. return read_c0_random();
  85. else
  86. return 0; /* no usable register */
  87. }
  88. #define random_get_entropy random_get_entropy
  89. #endif /* __KERNEL__ */
  90. #endif /* _ASM_TIMEX_H */