License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / drivers / gpu / drm / nouveau / include / nvkm / subdev / timer.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
9e79a853
BS
2#ifndef __NVKM_TIMER_H__
3#define __NVKM_TIMER_H__
5a5c7432 4#include <core/subdev.h>
5a5c7432 5
9e79a853 6struct nvkm_alarm {
5a5c7432 7 struct list_head head;
b4e382ca 8 struct list_head exec;
5a5c7432 9 u64 timestamp;
9e79a853 10 void (*func)(struct nvkm_alarm *);
5a5c7432
BS
11};
12
0cbf83bb 13static inline void
31649ecf 14nvkm_alarm_init(struct nvkm_alarm *alarm, void (*func)(struct nvkm_alarm *))
0cbf83bb
MP
15{
16 INIT_LIST_HEAD(&alarm->head);
17 alarm->func = func;
18}
19
31649ecf
BS
20struct 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
28u64 nvkm_timer_read(struct nvkm_timer *);
29void nvkm_timer_alarm(struct nvkm_timer *, u32 nsec, struct nvkm_alarm *);
5a5c7432 30
56f67dc1
BS
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; \
31649ecf 43 u64 _nsecs = (n), _time0 = nvkm_timer_read(_tmr); \
56f67dc1 44 s64 _taken = 0; \
31649ecf 45 bool _warn = true; \
56f67dc1
BS
46 \
47 do { \
48 cond \
31649ecf 49 } while (_taken = nvkm_timer_read(_tmr) - _time0, _taken < _nsecs); \
56f67dc1
BS
50 \
51 if (_taken >= _nsecs) { \
eb875d87
BS
52 if (_warn) \
53 dev_WARN(_device->dev, "timeout\n"); \
56f67dc1
BS
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
542f60dc
AC
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
31649ecf
BS
71int nv04_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
72int nv40_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
73int nv41_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
74int gk20a_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
5a5c7432 75#endif