timer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 at %s:%d/%s()!\n", \
  47. __FILE__, __LINE__, __func__); \
  48. } \
  49. _taken = -ETIMEDOUT; \
  50. } \
  51. _taken; \
  52. })
  53. #define nvkm_usec(d,u,cond...) nvkm_nsec((d), (u) * 1000, ##cond)
  54. #define nvkm_msec(d,m,cond...) nvkm_usec((d), (m) * 1000, ##cond)
  55. int nv04_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
  56. int nv40_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
  57. int nv41_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
  58. int gk20a_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
  59. #endif