• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
nvkm_alarm_init(struct nvkm_alarm * alarm,void (* func)(struct nvkm_alarm *))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 void nvkm_timer_alarm_cancel(struct nvkm_timer *, struct nvkm_alarm *);
30 
31 /* Delay based on GPU time (ie. PTIMER).
32  *
33  * Will return -ETIMEDOUT unless the loop was terminated with 'break',
34  * where it will return the number of nanoseconds taken instead.
35  *
36  * NVKM_DELAY can be passed for 'cond' to disable the timeout warning,
37  * which is useful for unconditional delay loops.
38  */
39 #define NVKM_DELAY _warn = false;
40 #define nvkm_nsec(d,n,cond...) ({                                              \
41 	struct nvkm_device *_device = (d);                                     \
42 	struct nvkm_timer *_tmr = _device->timer;                              \
43 	u64 _nsecs = (n), _time0 = nvkm_timer_read(_tmr);                      \
44 	s64 _taken = 0;                                                        \
45 	bool _warn = true;                                                     \
46                                                                                \
47 	do {                                                                   \
48 		cond                                                           \
49 	} while (_taken = nvkm_timer_read(_tmr) - _time0, _taken < _nsecs);    \
50                                                                                \
51 	if (_taken >= _nsecs) {                                                \
52 		if (_warn) {                                                   \
53 			dev_warn(_device->dev, "timeout at %s:%d/%s()!\n",     \
54 				 __FILE__, __LINE__, __func__);                \
55 		}                                                              \
56 		_taken = -ETIMEDOUT;                                           \
57 	}                                                                      \
58 	_taken;                                                                \
59 })
60 #define nvkm_usec(d,u,cond...) nvkm_nsec((d), (u) * 1000, ##cond)
61 #define nvkm_msec(d,m,cond...) nvkm_usec((d), (m) * 1000, ##cond)
62 
63 #define nvkm_wait_nsec(d,n,addr,mask,data)                                     \
64 	nvkm_nsec(d, n,                                                        \
65 		if ((nvkm_rd32(d, (addr)) & (mask)) == (data))                 \
66 			break;                                                 \
67 		)
68 #define nvkm_wait_usec(d,u,addr,mask,data)                                     \
69 	nvkm_wait_nsec((d), (u) * 1000, (addr), (mask), (data))
70 #define nvkm_wait_msec(d,m,addr,mask,data)                                     \
71 	nvkm_wait_usec((d), (m) * 1000, (addr), (mask), (data))
72 
73 int nv04_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
74 int nv40_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
75 int nv41_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
76 int gk20a_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
77 #endif
78