summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/nouveau/nouveau_stateobj.h
blob: 9aee9e495664e3c8b3caf97669106e2b729ff256 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#ifndef __NOUVEAU_STATEOBJ_H__
#define __NOUVEAU_STATEOBJ_H__

#include "util/u_debug.h"

struct nouveau_stateobj_reloc {
	struct nouveau_bo *bo;

	unsigned offset;
	unsigned packet;

	unsigned data;
	unsigned flags;
	unsigned vor;
	unsigned tor;
};

struct nouveau_stateobj {
	struct pipe_reference reference;

	unsigned *push;
	struct nouveau_stateobj_reloc *reloc;

	unsigned *cur;
	unsigned cur_packet;
	unsigned cur_reloc;
};

static INLINE struct nouveau_stateobj *
so_new(unsigned push, unsigned reloc)
{
	struct nouveau_stateobj *so;

	so = MALLOC(sizeof(struct nouveau_stateobj));
	pipe_reference_init(&so->reference, 1);
	so->push = MALLOC(sizeof(unsigned) * push);
	so->reloc = MALLOC(sizeof(struct nouveau_stateobj_reloc) * reloc);

	so->cur = so->push;
	so->cur_reloc = so->cur_packet = 0;

	return so;
}

static INLINE void
so_ref(struct nouveau_stateobj *ref, struct nouveau_stateobj **pso)
{
	struct nouveau_stateobj *so = *pso;
	int i;

        if (pipe_reference(&(*pso)->reference, &ref->reference)) {
		free(so->push);
		for (i = 0; i < so->cur_reloc; i++)
			nouveau_bo_ref(NULL, &so->reloc[i].bo);
		free(so->reloc);
		free(so);
	}
	*pso = ref;
}

static INLINE void
so_data(struct nouveau_stateobj *so, unsigned data)
{
	(*so->cur++) = (data);
	so->cur_packet += 4;
}

static INLINE void
so_datap(struct nouveau_stateobj *so, unsigned *data, unsigned size)
{
	so->cur_packet += (4 * size);
	while (size--)
		(*so->cur++) = (*data++);
}

static INLINE void
so_method(struct nouveau_stateobj *so, struct nouveau_grobj *gr,
	  unsigned mthd, unsigned size)
{
	so->cur_packet = (gr->subc << 13) | (1 << 18) | (mthd - 4);
	so_data(so, (gr->subc << 13) | (size << 18) | mthd);
}

static INLINE void
so_reloc(struct nouveau_stateobj *so, struct nouveau_bo *bo,
	 unsigned data, unsigned flags, unsigned vor, unsigned tor)
{
	struct nouveau_stateobj_reloc *r = &so->reloc[so->cur_reloc++];
	
	r->bo = NULL;
	nouveau_bo_ref(bo, &r->bo);
	r->offset = so->cur - so->push;
	r->packet = so->cur_packet;
	r->data = data;
	r->flags = flags;
	r->vor = vor;
	r->tor = tor;
	so_data(so, data);
}

static INLINE void
so_dump(struct nouveau_stateobj *so)
{
	unsigned i, nr = so->cur - so->push;

	for (i = 0; i < nr; i++)
		debug_printf("+0x%04x: 0x%08x\n", i, so->push[i]);
}

static INLINE void
so_emit(struct nouveau_channel *chan, struct nouveau_stateobj *so)
{
	struct nouveau_pushbuf *pb = chan->pushbuf;
	unsigned nr, i;
	int ret = 0;

	nr = so->cur - so->push;
	/* This will flush if we need space.
	 * We don't actually need the marker.
	 */
	if ((ret = nouveau_pushbuf_marker_emit(chan, nr, so->cur_reloc))) {
		debug_printf("so_emit failed marker emit with error %d\n", ret);
		return;
	}
	pb->remaining -= nr;

	memcpy(pb->cur, so->push, nr * 4);
	for (i = 0; i < so->cur_reloc; i++) {
		struct nouveau_stateobj_reloc *r = &so->reloc[i];

		if ((ret = nouveau_pushbuf_emit_reloc(chan, pb->cur + r->offset,
					   r->bo, r->data, 0, r->flags,
					   r->vor, r->tor))) {
			debug_printf("so_emit failed reloc with error %d\n", ret);
			goto out;
		}
	}
out:
	pb->cur += nr;
}

static INLINE void
so_emit_reloc_markers(struct nouveau_channel *chan, struct nouveau_stateobj *so)
{
	struct nouveau_pushbuf *pb = chan->pushbuf;
	unsigned i;
	int ret = 0;

	if (!so)
		return;

	i = so->cur_reloc << 1;
	/* This will flush if we need space.
	 * We don't actually need the marker.
	 */
	if ((ret = nouveau_pushbuf_marker_emit(chan, i, i))) {
		debug_printf("so_emit_reloc_markers failed marker emit with" \
			"error %d\n", ret);
		return;
	}
	pb->remaining -= i;

	for (i = 0; i < so->cur_reloc; i++) {
		struct nouveau_stateobj_reloc *r = &so->reloc[i];

		if ((ret = nouveau_pushbuf_emit_reloc(chan, pb->cur++, r->bo,
					   r->packet, 0,
					   (r->flags & (NOUVEAU_BO_VRAM |
							NOUVEAU_BO_GART |
							NOUVEAU_BO_RDWR)) |
					   NOUVEAU_BO_DUMMY, 0, 0))) {
			debug_printf("so_emit_reloc_markers failed reloc" \
						"with error %d\n", ret);
			pb->remaining += ((so->cur_reloc - i) << 1);
			return;
		}
		if ((ret = nouveau_pushbuf_emit_reloc(chan, pb->cur++, r->bo,
					   r->data, 0,
					   r->flags | NOUVEAU_BO_DUMMY,
					   r->vor, r->tor))) {
			debug_printf("so_emit_reloc_markers failed reloc" \
						"with error %d\n", ret);
			pb->remaining += ((so->cur_reloc - i) << 1) - 1;
			return;
		}
	}
}

#endif