timer.h 3.1 KB

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