1 /* SPDX-License-Identifier: MIT */
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 struct nvkm_timer_wait {
32 	struct nvkm_timer *tmr;
33 	u64 limit;
34 	u64 time0;
35 	u64 time1;
36 	int reads;
37 };
38 
39 void nvkm_timer_wait_init(struct nvkm_device *, u64 nsec,
40 			  struct nvkm_timer_wait *);
41 s64 nvkm_timer_wait_test(struct nvkm_timer_wait *);
42 
43 /* Delay based on GPU time (ie. PTIMER).
44  *
45  * Will return -ETIMEDOUT unless the loop was terminated with 'break',
46  * where it will return the number of nanoseconds taken instead.
47  *
48  * NVKM_DELAY can be passed for 'cond' to disable the timeout warning,
49  * which is useful for unconditional delay loops.
50  */
51 #define NVKM_DELAY _warn = false;
52 #define nvkm_nsec(d,n,cond...) ({                                              \
53 	struct nvkm_timer_wait _wait;                                          \
54 	bool _warn = true;                                                     \
55 	s64 _taken = 0;                                                        \
56                                                                                \
57 	nvkm_timer_wait_init((d), (n), &_wait);                                \
58 	do {                                                                   \
59 		cond                                                           \
60 	} while ((_taken = nvkm_timer_wait_test(&_wait)) >= 0);                \
61                                                                                \
62 	if (_warn && _taken < 0)                                               \
63 		dev_WARN(_wait.tmr->subdev.device->dev, "timeout\n");          \
64 	_taken;                                                                \
65 })
66 #define nvkm_usec(d, u, cond...) nvkm_nsec((d), (u) * 1000ULL, ##cond)
67 #define nvkm_msec(d, m, cond...) nvkm_usec((d), (m) * 1000ULL, ##cond)
68 
69 #define nvkm_wait_nsec(d,n,addr,mask,data)                                     \
70 	nvkm_nsec(d, n,                                                        \
71 		if ((nvkm_rd32(d, (addr)) & (mask)) == (data))                 \
72 			break;                                                 \
73 		)
74 #define nvkm_wait_usec(d,u,addr,mask,data)                                     \
75 	nvkm_wait_nsec((d), (u) * 1000, (addr), (mask), (data))
76 #define nvkm_wait_msec(d,m,addr,mask,data)                                     \
77 	nvkm_wait_usec((d), (m) * 1000, (addr), (mask), (data))
78 
79 int nv04_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
80 int nv40_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
81 int nv41_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
82 int gk20a_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
83 #endif
84