18c2ecf20Sopenharmony_ci#ifndef __NVIF_TIMER_H__
28c2ecf20Sopenharmony_ci#define __NVIF_TIMER_H__
38c2ecf20Sopenharmony_ci#include <nvif/os.h>
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_cistruct nvif_timer_wait {
68c2ecf20Sopenharmony_ci	struct nvif_device *device;
78c2ecf20Sopenharmony_ci	u64 limit;
88c2ecf20Sopenharmony_ci	u64 time0;
98c2ecf20Sopenharmony_ci	u64 time1;
108c2ecf20Sopenharmony_ci	int reads;
118c2ecf20Sopenharmony_ci};
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_civoid nvif_timer_wait_init(struct nvif_device *, u64 nsec,
148c2ecf20Sopenharmony_ci			  struct nvif_timer_wait *);
158c2ecf20Sopenharmony_cis64 nvif_timer_wait_test(struct nvif_timer_wait *);
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/* Delay based on GPU time (ie. PTIMER).
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * Will return -ETIMEDOUT unless the loop was terminated with 'break',
208c2ecf20Sopenharmony_ci * where it will return the number of nanoseconds taken instead.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci#define nvif_nsec(d,n,cond...) ({                                              \
238c2ecf20Sopenharmony_ci	struct nvif_timer_wait _wait;                                          \
248c2ecf20Sopenharmony_ci	s64 _taken = 0;                                                        \
258c2ecf20Sopenharmony_ci                                                                               \
268c2ecf20Sopenharmony_ci	nvif_timer_wait_init((d), (n), &_wait);                                \
278c2ecf20Sopenharmony_ci	do {                                                                   \
288c2ecf20Sopenharmony_ci		cond                                                           \
298c2ecf20Sopenharmony_ci	} while ((_taken = nvif_timer_wait_test(&_wait)) >= 0);                \
308c2ecf20Sopenharmony_ci                                                                               \
318c2ecf20Sopenharmony_ci	_taken;                                                                \
328c2ecf20Sopenharmony_ci})
338c2ecf20Sopenharmony_ci#define nvif_usec(d,u,cond...) nvif_nsec((d), (u) * 1000, ##cond)
348c2ecf20Sopenharmony_ci#define nvif_msec(d,m,cond...) nvif_usec((d), (m) * 1000, ##cond)
358c2ecf20Sopenharmony_ci#endif
36