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 	struct list_head exec;
8 	u64 timestamp;
9 	void (*func)(struct nvkm_alarm *);
10 };
11 
12 static inline void
13 nvkm_alarm_init(struct nvkm_alarm *alarm, void (*func)(struct nvkm_alarm *))
14 {
15 	INIT_LIST_HEAD(&alarm->head);
16 	alarm->func = func;
17 }
18 
19 struct nvkm_timer {
20 	const struct nvkm_timer_func *func;
21 	struct nvkm_subdev subdev;
22 
23 	struct list_head alarms;
24 	spinlock_t lock;
25 };
26 
27 u64 nvkm_timer_read(struct nvkm_timer *);
28 void nvkm_timer_alarm(struct nvkm_timer *, u32 nsec, 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\n");                   \
53 		_taken = -ETIMEDOUT;                                           \
54 	}                                                                      \
55 	_taken;                                                                \
56 })
57 #define nvkm_usec(d,u,cond...) nvkm_nsec((d), (u) * 1000, ##cond)
58 #define nvkm_msec(d,m,cond...) nvkm_usec((d), (m) * 1000, ##cond)
59 
60 #define nvkm_wait_nsec(d,n,addr,mask,data)                                     \
61 	nvkm_nsec(d, n,                                                        \
62 		if ((nvkm_rd32(d, (addr)) & (mask)) == (data))                 \
63 			break;                                                 \
64 		)
65 #define nvkm_wait_usec(d,u,addr,mask,data)                                     \
66 	nvkm_wait_nsec((d), (u) * 1000, (addr), (mask), (data))
67 #define nvkm_wait_msec(d,m,addr,mask,data)                                     \
68 	nvkm_wait_usec((d), (m) * 1000, (addr), (mask), (data))
69 
70 int nv04_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
71 int nv40_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
72 int nv41_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
73 int gk20a_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
74 #endif
75