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