clock.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "axp.h"
  8. #include "ureg.h"
  9. void
  10. clockinit(void)
  11. {
  12. }
  13. uvlong
  14. cycletimer(void)
  15. {
  16. ulong pcc;
  17. vlong delta;
  18. pcc = rpcc(nil) & 0xFFFFFFFF;
  19. if(m->cpuhz == 0){
  20. /*
  21. * pcclast is needed to detect wraparound of
  22. * the cycle timer which is only 32-bits.
  23. * m->cpuhz is set from the info passed from
  24. * the firmware.
  25. * This could be in clockinit if can
  26. * guarantee no wraparound between then and now.
  27. *
  28. * All the clock stuff needs work.
  29. */
  30. m->cpuhz = hwrpb->cfreq;
  31. m->pcclast = pcc;
  32. }
  33. delta = pcc - m->pcclast;
  34. if(delta < 0)
  35. delta += 0x100000000LL;
  36. m->pcclast = pcc;
  37. m->fastclock += delta;
  38. return MACHP(0)->fastclock;
  39. }
  40. uvlong
  41. fastticks(uvlong* hz)
  42. {
  43. uvlong ticks;
  44. int x;
  45. x = splhi();
  46. ticks = cycletimer();
  47. splx(x);
  48. if(hz)
  49. *hz = m->cpuhz;
  50. return ticks;
  51. }
  52. ulong
  53. µs(void)
  54. {
  55. return fastticks2us(cycletimer());
  56. }
  57. /*
  58. * performance measurement ticks. must be low overhead.
  59. * doesn't have to count over a second.
  60. */
  61. ulong
  62. perfticks(void)
  63. {
  64. return rpcc(nil);
  65. }
  66. void
  67. timerset(Tval)
  68. {
  69. }
  70. void
  71. microdelay(int us)
  72. {
  73. uvlong eot;
  74. eot = fastticks(nil) + (m->cpuhz/1000000)*us;
  75. while(fastticks(nil) < eot)
  76. ;
  77. }
  78. void
  79. delay(int millisecs)
  80. {
  81. microdelay(millisecs*1000);
  82. }
  83. void
  84. clock(Ureg *ureg)
  85. {
  86. static int count;
  87. cycletimer();
  88. /* HZ == 100, timer == 1024Hz. error < 1ms */
  89. count += 100;
  90. if (count < 1024)
  91. return;
  92. count -= 1024;
  93. timerintr(ureg, 0);
  94. }