clock.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /*
  53. * performance measurement ticks. must be low overhead.
  54. * doesn't have to count over a second.
  55. */
  56. ulong
  57. perfticks(void)
  58. {
  59. return rpcc(nil);
  60. }
  61. void
  62. timerset(uvlong)
  63. {
  64. }
  65. void
  66. microdelay(int us)
  67. {
  68. uvlong eot;
  69. eot = fastticks(nil) + (m->cpuhz/1000000)*us;
  70. while(fastticks(nil) < eot)
  71. ;
  72. }
  73. void
  74. delay(int millisecs)
  75. {
  76. microdelay(millisecs*1000);
  77. }
  78. void
  79. clock(Ureg *ureg)
  80. {
  81. static int count;
  82. cycletimer();
  83. /* HZ == 100, timer == 1024Hz. error < 1ms */
  84. count += 100;
  85. if (count < 1024)
  86. return;
  87. count -= 1024;
  88. timerintr(ureg, 0);
  89. }