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