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