timer.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef __NVKM_TIMER_H__
  2. #define __NVKM_TIMER_H__
  3. #include <core/subdev.h>
  4. struct nvkm_alarm {
  5. struct list_head head;
  6. struct list_head exec;
  7. u64 timestamp;
  8. void (*func)(struct nvkm_alarm *);
  9. };
  10. static inline void
  11. nvkm_alarm_init(struct nvkm_alarm *alarm, void (*func)(struct nvkm_alarm *))
  12. {
  13. INIT_LIST_HEAD(&alarm->head);
  14. alarm->func = func;
  15. }
  16. struct nvkm_timer {
  17. const struct nvkm_timer_func *func;
  18. struct nvkm_subdev subdev;
  19. struct list_head alarms;
  20. spinlock_t lock;
  21. };
  22. u64 nvkm_timer_read(struct nvkm_timer *);
  23. void nvkm_timer_alarm(struct nvkm_timer *, u32 nsec, struct nvkm_alarm *);
  24. void nvkm_timer_alarm_cancel(struct nvkm_timer *, struct nvkm_alarm *);
  25. /* Delay based on GPU time (ie. PTIMER).
  26. *
  27. * Will return -ETIMEDOUT unless the loop was terminated with 'break',
  28. * where it will return the number of nanoseconds taken instead.
  29. *
  30. * NVKM_DELAY can be passed for 'cond' to disable the timeout warning,
  31. * which is useful for unconditional delay loops.
  32. */
  33. #define NVKM_DELAY _warn = false;
  34. #define nvkm_nsec(d,n,cond...) ({ \
  35. struct nvkm_device *_device = (d); \
  36. struct nvkm_timer *_tmr = _device->timer; \
  37. u64 _nsecs = (n), _time0 = nvkm_timer_read(_tmr); \
  38. s64 _taken = 0; \
  39. bool _warn = true; \
  40. \
  41. do { \
  42. cond \
  43. } while (_taken = nvkm_timer_read(_tmr) - _time0, _taken < _nsecs); \
  44. \
  45. if (_taken >= _nsecs) { \
  46. if (_warn) \
  47. dev_WARN(_device->dev, "timeout\n"); \
  48. _taken = -ETIMEDOUT; \
  49. } \
  50. _taken; \
  51. })
  52. #define nvkm_usec(d,u,cond...) nvkm_nsec((d), (u) * 1000, ##cond)
  53. #define nvkm_msec(d,m,cond...) nvkm_usec((d), (m) * 1000, ##cond)
  54. #define nvkm_wait_nsec(d,n,addr,mask,data) \
  55. nvkm_nsec(d, n, \
  56. if ((nvkm_rd32(d, (addr)) & (mask)) == (data)) \
  57. break; \
  58. )
  59. #define nvkm_wait_usec(d,u,addr,mask,data) \
  60. nvkm_wait_nsec((d), (u) * 1000, (addr), (mask), (data))
  61. #define nvkm_wait_msec(d,m,addr,mask,data) \
  62. nvkm_wait_usec((d), (m) * 1000, (addr), (mask), (data))
  63. int nv04_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
  64. int nv40_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
  65. int nv41_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
  66. int gk20a_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
  67. #endif