nouveau: create linear gart/vram mman in common screen init
[mesa.git] / src / gallium / drivers / nouveau / nouveau_stateobj.h
1 #ifndef __NOUVEAU_STATEOBJ_H__
2 #define __NOUVEAU_STATEOBJ_H__
3
4 #include "util/u_debug.h"
5
6 #ifdef DEBUG
7 #define DEBUG_NOUVEAU_STATEOBJ
8 #endif /* DEBUG */
9
10 struct nouveau_stateobj_reloc {
11 struct nouveau_bo *bo;
12
13 struct nouveau_grobj *gr;
14 uint32_t push_offset;
15 uint32_t mthd;
16
17 uint32_t data;
18 unsigned flags;
19 unsigned vor;
20 unsigned tor;
21 };
22
23 struct nouveau_stateobj_start {
24 struct nouveau_grobj *gr;
25 uint32_t mthd;
26 uint32_t size;
27 unsigned offset;
28 };
29
30 struct nouveau_stateobj {
31 struct pipe_reference reference;
32
33 struct nouveau_stateobj_start *start;
34 struct nouveau_stateobj_reloc *reloc;
35
36 /* Common memory pool for data. */
37 uint32_t *pool;
38 unsigned pool_cur;
39
40 #ifdef DEBUG_NOUVEAU_STATEOBJ
41 unsigned start_alloc;
42 unsigned reloc_alloc;
43 unsigned pool_alloc;
44 #endif /* DEBUG_NOUVEAU_STATEOBJ */
45
46 unsigned total; /* includes begin_ring */
47 unsigned cur; /* excludes begin_ring, offset from "cur_start" */
48 unsigned cur_start;
49 unsigned cur_reloc;
50 };
51
52 static INLINE void
53 so_dump(struct nouveau_stateobj *so)
54 {
55 unsigned i, nr, total = 0;
56
57 for (i = 0; i < so->cur_start; i++) {
58 if (so->start[i].gr->subc > -1)
59 debug_printf("+0x%04x: 0x%08x\n", total++,
60 (so->start[i].size << 18) | (so->start[i].gr->subc << 13)
61 | so->start[i].mthd);
62 else
63 debug_printf("+0x%04x: 0x%08x\n", total++,
64 (so->start[i].size << 18) | so->start[i].mthd);
65 for (nr = 0; nr < so->start[i].size; nr++, total++)
66 debug_printf("+0x%04x: 0x%08x\n", total,
67 so->pool[so->start[i].offset + nr]);
68 }
69 }
70
71 static INLINE struct nouveau_stateobj *
72 so_new(unsigned start, unsigned push, unsigned reloc)
73 {
74 struct nouveau_stateobj *so;
75
76 so = MALLOC(sizeof(struct nouveau_stateobj));
77 pipe_reference_init(&so->reference, 1);
78 so->total = so->cur = so->cur_start = so->cur_reloc = 0;
79
80 #ifdef DEBUG_NOUVEAU_STATEOBJ
81 so->start_alloc = start;
82 so->reloc_alloc = reloc;
83 so->pool_alloc = push;
84 #endif /* DEBUG_NOUVEAU_STATEOBJ */
85
86 so->start = MALLOC(start * sizeof(struct nouveau_stateobj_start));
87 so->reloc = MALLOC(reloc * sizeof(struct nouveau_stateobj_reloc));
88 so->pool = MALLOC(push * sizeof(uint32_t));
89 so->pool_cur = 0;
90
91 if (!so->start || !so->reloc || !so->pool) {
92 debug_printf("malloc failed\n");
93 assert(0);
94 }
95
96 return so;
97 }
98
99 static INLINE void
100 so_ref(struct nouveau_stateobj *ref, struct nouveau_stateobj **pso)
101 {
102 struct nouveau_stateobj *so = *pso;
103 int i;
104
105 if (pipe_reference(&(*pso)->reference, &ref->reference)) {
106 FREE(so->start);
107 for (i = 0; i < so->cur_reloc; i++)
108 nouveau_bo_ref(NULL, &so->reloc[i].bo);
109 FREE(so->reloc);
110 FREE(so->pool);
111 FREE(so);
112 }
113 *pso = ref;
114 }
115
116 static INLINE void
117 so_data(struct nouveau_stateobj *so, uint32_t data)
118 {
119 #ifdef DEBUG_NOUVEAU_STATEOBJ
120 if (so->cur >= so->start[so->cur_start - 1].size) {
121 debug_printf("exceeding specified size\n");
122 assert(0);
123 }
124 #endif /* DEBUG_NOUVEAU_STATEOBJ */
125
126 so->pool[so->start[so->cur_start - 1].offset + so->cur++] = data;
127 }
128
129 static INLINE void
130 so_datap(struct nouveau_stateobj *so, uint32_t *data, unsigned size)
131 {
132 #ifdef DEBUG_NOUVEAU_STATEOBJ
133 if ((so->cur + size) > so->start[so->cur_start - 1].size) {
134 debug_printf("exceeding specified size\n");
135 assert(0);
136 }
137 #endif /* DEBUG_NOUVEAU_STATEOBJ */
138
139 while (size--)
140 so->pool[so->start[so->cur_start - 1].offset + so->cur++] =
141 *data++;
142 }
143
144 static INLINE void
145 so_method(struct nouveau_stateobj *so, struct nouveau_grobj *gr,
146 unsigned mthd, unsigned size)
147 {
148 struct nouveau_stateobj_start *start;
149
150 #ifdef DEBUG_NOUVEAU_STATEOBJ
151 if (so->start_alloc <= so->cur_start) {
152 debug_printf("exceeding num_start size\n");
153 assert(0);
154 }
155 #endif /* DEBUG_NOUVEAU_STATEOBJ */
156 start = so->start;
157
158 #ifdef DEBUG_NOUVEAU_STATEOBJ
159 if (so->cur_start > 0 && start[so->cur_start - 1].size > so->cur) {
160 debug_printf("previous so_method was not filled\n");
161 assert(0);
162 }
163 #endif /* DEBUG_NOUVEAU_STATEOBJ */
164
165 start[so->cur_start].gr = gr;
166 start[so->cur_start].mthd = mthd;
167 start[so->cur_start].size = size;
168
169 #ifdef DEBUG_NOUVEAU_STATEOBJ
170 if (so->pool_alloc < (size + so->pool_cur)) {
171 debug_printf("exceeding num_pool size\n");
172 assert(0);
173 }
174 #endif /* DEBUG_NOUVEAU_STATEOBJ */
175
176 start[so->cur_start].offset = so->pool_cur;
177 so->pool_cur += size;
178
179 so->cur_start++;
180 /* The 1 is for *this* begin_ring. */
181 so->total += so->cur + 1;
182 so->cur = 0;
183 }
184
185 static INLINE void
186 so_reloc(struct nouveau_stateobj *so, struct nouveau_bo *bo,
187 unsigned data, unsigned flags, unsigned vor, unsigned tor)
188 {
189 struct nouveau_stateobj_reloc *r;
190
191 #ifdef DEBUG_NOUVEAU_STATEOBJ
192 if (so->reloc_alloc <= so->cur_reloc) {
193 debug_printf("exceeding num_reloc size\n");
194 assert(0);
195 }
196 #endif /* DEBUG_NOUVEAU_STATEOBJ */
197 r = so->reloc;
198
199 r[so->cur_reloc].bo = NULL;
200 nouveau_bo_ref(bo, &(r[so->cur_reloc].bo));
201 r[so->cur_reloc].gr = so->start[so->cur_start-1].gr;
202 r[so->cur_reloc].push_offset = so->total + so->cur;
203 r[so->cur_reloc].data = data;
204 r[so->cur_reloc].flags = flags;
205 r[so->cur_reloc].mthd = so->start[so->cur_start-1].mthd +
206 (so->cur << 2);
207 r[so->cur_reloc].vor = vor;
208 r[so->cur_reloc].tor = tor;
209
210 so_data(so, data);
211 so->cur_reloc++;
212 }
213
214 /* Determine if this buffer object is referenced by this state object. */
215 static INLINE boolean
216 so_bo_is_reloc(struct nouveau_stateobj *so, struct nouveau_bo *bo)
217 {
218 int i;
219
220 for (i = 0; i < so->cur_reloc; i++)
221 if (so->reloc[i].bo == bo)
222 return true;
223
224 return false;
225 }
226
227 static INLINE void
228 so_emit(struct nouveau_channel *chan, struct nouveau_stateobj *so)
229 {
230 unsigned nr, i;
231 int ret = 0;
232
233 #ifdef DEBUG_NOUVEAU_STATEOBJ
234 if (so->start[so->cur_start - 1].size > so->cur) {
235 debug_printf("emit: previous so_method was not filled\n");
236 assert(0);
237 }
238 #endif /* DEBUG_NOUVEAU_STATEOBJ */
239
240 /* We cannot update total in case we so_emit again. */
241 nr = so->total + so->cur;
242
243 /* This will flush if we need space.
244 * We don't actually need the marker.
245 */
246 if ((ret = nouveau_pushbuf_marker_emit(chan, nr, so->cur_reloc))) {
247 debug_printf("so_emit failed marker emit with error %d\n", ret);
248 assert(0);
249 }
250
251 /* Submit data. This will ensure proper binding of objects. */
252 for (i = 0; i < so->cur_start; i++) {
253 BEGIN_RING(chan, so->start[i].gr, so->start[i].mthd, so->start[i].size);
254 OUT_RINGp(chan, &(so->pool[so->start[i].offset]), so->start[i].size);
255 }
256
257 for (i = 0; i < so->cur_reloc; i++) {
258 struct nouveau_stateobj_reloc *r = &so->reloc[i];
259
260 if ((ret = nouveau_pushbuf_emit_reloc(chan, chan->cur - nr +
261 r->push_offset, r->bo, r->data,
262 0, r->flags, r->vor, r->tor))) {
263 debug_printf("so_emit failed reloc with error %d\n", ret);
264 assert(0);
265 }
266 }
267 }
268
269 static INLINE void
270 so_emit_reloc_markers(struct nouveau_channel *chan, struct nouveau_stateobj *so)
271 {
272 unsigned i;
273 int ret = 0;
274
275 if (!so)
276 return;
277
278 /* If we need to flush in flush notify, then we have a problem anyway. */
279 for (i = 0; i < so->cur_reloc; i++) {
280 struct nouveau_stateobj_reloc *r = &so->reloc[i];
281
282 #ifdef DEBUG_NOUVEAU_STATEOBJ
283 if (r->mthd & 0x40000000) {
284 debug_printf("error: NI mthd 0x%08X\n", r->mthd);
285 continue;
286 }
287 #endif /* DEBUG_NOUVEAU_STATEOBJ */
288
289 /* We don't need to autobind, since there are enough subchannels
290 * for all objects we use. If this is changed, account for the extra
291 * space in callers of this function.
292 */
293 assert(r->gr->bound != NOUVEAU_GROBJ_UNBOUND);
294
295 /* Some relocs really don't like to be hammered,
296 * NOUVEAU_BO_DUMMY makes sure it only
297 * happens when needed.
298 */
299 ret = OUT_RELOC(chan, r->bo, (r->gr->subc << 13) | (1<< 18) |
300 r->mthd, (r->flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART
301 | NOUVEAU_BO_RDWR)) | NOUVEAU_BO_DUMMY, 0, 0);
302 if (ret) {
303 debug_printf("OUT_RELOC failed %d\n", ret);
304 assert(0);
305 }
306
307 ret = OUT_RELOC(chan, r->bo, r->data, r->flags |
308 NOUVEAU_BO_DUMMY, r->vor, r->tor);
309 if (ret) {
310 debug_printf("OUT_RELOC failed %d\n", ret);
311 assert(0);
312 }
313 }
314 }
315
316 #endif