summaryrefslogtreecommitdiff
path: root/src/nv_notifier.c
blob: 88f0edb5e2dfc1da1a2dc32c58da0d6d530a7afe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "nv_include.h"

#define NV_NOTIFIER_SIZE                                                      32
#define NV_NOTIFY_TIME_0                                              0x00000000
#define NV_NOTIFY_TIME_1                                              0x00000004
#define NV_NOTIFY_RETURN_VALUE                                        0x00000008
#define NV_NOTIFY_STATE                                               0x0000000C
#define NV_NOTIFY_STATE_STATUS_MASK                                   0xFF000000
#define NV_NOTIFY_STATE_STATUS_SHIFT                                          24
#define NV_NOTIFY_STATE_STATUS_COMPLETED                                    0x00
#define NV_NOTIFY_STATE_STATUS_IN_PROCESS                                   0x01
#define NV_NOTIFY_STATE_ERROR_CODE_MASK                               0x0000FFFF
#define NV_NOTIFY_STATE_ERROR_CODE_SHIFT                                       0

#define NOTIFIER(__v) \
	NVPtr pNv = NVPTR(pScrn); \
	volatile uint32_t *__v = (void*)pNv->NotifierBlock + notifier->offset

drm_nouveau_notifier_alloc_t *
NVNotifierAlloc(ScrnInfoPtr pScrn, uint32_t handle)
{
	NVPtr pNv = NVPTR(pScrn);
	drm_nouveau_notifier_alloc_t *notifier;
	int ret;

	notifier = xcalloc(1, sizeof(*notifier));
	if (!notifier) {
		NVNotifierDestroy(pScrn, notifier);
		return NULL;
	}

	notifier->channel = pNv->fifo.channel;
	notifier->handle  = handle;
	notifier->count   = 1;
	ret = drmCommandWriteRead(pNv->drm_fd, DRM_NOUVEAU_NOTIFIER_ALLOC,
				  notifier, sizeof(*notifier));
	if (ret) {
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "Failed to create notifier 0x%08x: %d\n",
			   handle, ret);
		NVNotifierDestroy(pScrn, notifier);
		return NULL;
	}

	return notifier;
}

void
NVNotifierDestroy(ScrnInfoPtr pScrn, drm_nouveau_notifier_alloc_t *notifier)
{
	if (notifier) {
		/*XXX: destroy notifier object */
		xfree(notifier);
	}
}

void
NVNotifierReset(ScrnInfoPtr pScrn, drm_nouveau_notifier_alloc_t *notifier)
{
	NOTIFIER(n);

	n[NV_NOTIFY_TIME_0      /4] =
	n[NV_NOTIFY_TIME_1      /4] =
	n[NV_NOTIFY_RETURN_VALUE/4] = 0;
	n[NV_NOTIFY_STATE       /4] = (NV_NOTIFY_STATE_STATUS_IN_PROCESS <<
				       NV_NOTIFY_STATE_STATUS_SHIFT);
}

uint32_t
NVNotifierStatus(ScrnInfoPtr pScrn, drm_nouveau_notifier_alloc_t *notifier)
{
	NOTIFIER(n);

	return n[NV_NOTIFY_STATE/4] >> NV_NOTIFY_STATE_STATUS_SHIFT;
}

uint32_t
NVNotifierErrorCode(ScrnInfoPtr pScrn, drm_nouveau_notifier_alloc_t *notifier)
{
	NOTIFIER(n);

	return n[NV_NOTIFY_STATE/4] & NV_NOTIFY_STATE_ERROR_CODE_MASK;
}

uint32_t
NVNotifierReturnVal(ScrnInfoPtr pScrn, drm_nouveau_notifier_alloc_t *notifier)
{
	NOTIFIER(n);

	return n[NV_NOTIFY_RETURN_VALUE/4];
}

Bool
NVNotifierWaitStatus(ScrnInfoPtr pScrn, drm_nouveau_notifier_alloc_t *notifier,
		     unsigned int status, unsigned int timeout)
{
	NOTIFIER(n);
	unsigned int t_start, time = 0;

	t_start = GetTimeInMillis();
	while (time <= timeout) {
#if 0
		xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
			   "N(0x%08x)/%d = 0x%08x/0x%08x/0x%08x/0x%08x\n",
			   notifier->handle, time, n[0], n[1], n[2], n[3]);
#endif
		if (n[NV_NOTIFY_STATE/4] & NV_NOTIFY_STATE_ERROR_CODE_MASK) {
			xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
				   "Notifier returned error: 0x%04x\n",
				   NVNotifierErrorCode(pScrn, notifier));
			return FALSE;
		}

		if ((n[NV_NOTIFY_STATE/4] >> NV_NOTIFY_STATE_STATUS_SHIFT)
				== status)
			return TRUE;

		if (timeout)
			time = GetTimeInMillis() - t_start;
	}

	xf86DrvMsg(pScrn->scrnIndex, X_ERROR,
		   "Notifier (0x%08x) timeout!\n", notifier->handle);
	return FALSE;
}