timer.h 3.2 KB
Newer Older
1 2
#ifndef __NVKM_TIMER_H__
#define __NVKM_TIMER_H__
3 4
#include <core/subdev.h>

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
13
nvkm_alarm_init(struct nvkm_alarm *alarm, void (*func)(struct nvkm_alarm *))
14 15 16 17 18
{
	INIT_LIST_HEAD(&alarm->head);
	alarm->func = func;
}

19 20 21 22 23 24 25 26 27 28 29
struct nvkm_timer {
	const struct nvkm_timer_func *func;
	struct nvkm_subdev subdev;

	struct list_head alarms;
	spinlock_t lock;
};

u64 nvkm_timer_read(struct nvkm_timer *);
void nvkm_timer_alarm(struct nvkm_timer *, u32 nsec, struct nvkm_alarm *);
void nvkm_timer_alarm_cancel(struct nvkm_timer *, struct nvkm_alarm *);
30

31 32 33 34 35 36 37 38 39 40 41 42
/* Delay based on GPU time (ie. PTIMER).
 *
 * Will return -ETIMEDOUT unless the loop was terminated with 'break',
 * where it will return the number of nanoseconds taken instead.
 *
 * NVKM_DELAY can be passed for 'cond' to disable the timeout warning,
 * which is useful for unconditional delay loops.
 */
#define NVKM_DELAY _warn = false;
#define nvkm_nsec(d,n,cond...) ({                                              \
	struct nvkm_device *_device = (d);                                     \
	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 48
                                                                               \
	do {                                                                   \
		cond                                                           \
49
	} while (_taken = nvkm_timer_read(_tmr) - _time0, _taken < _nsecs);    \
50 51
                                                                               \
	if (_taken >= _nsecs) {                                                \
52 53
		if (_warn)                                                     \
			dev_WARN(_device->dev, "timeout\n");                   \
54 55 56 57 58 59 60
		_taken = -ETIMEDOUT;                                           \
	}                                                                      \
	_taken;                                                                \
})
#define nvkm_usec(d,u,cond...) nvkm_nsec((d), (u) * 1000, ##cond)
#define nvkm_msec(d,m,cond...) nvkm_usec((d), (m) * 1000, ##cond)

61 62 63 64 65 66 67 68 69 70
#define nvkm_wait_nsec(d,n,addr,mask,data)                                     \
	nvkm_nsec(d, n,                                                        \
		if ((nvkm_rd32(d, (addr)) & (mask)) == (data))                 \
			break;                                                 \
		)
#define nvkm_wait_usec(d,u,addr,mask,data)                                     \
	nvkm_wait_nsec((d), (u) * 1000, (addr), (mask), (data))
#define nvkm_wait_msec(d,m,addr,mask,data)                                     \
	nvkm_wait_usec((d), (m) * 1000, (addr), (mask), (data))

71 72 73 74
int nv04_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
int nv40_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
int nv41_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
int gk20a_timer_new(struct nvkm_device *, int, struct nvkm_timer **);
75
#endif