Merge branch 'glsl2-head' into glsl2
[mesa.git] / src / gallium / winsys / r600 / drm / radeon_ctx.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jerome Glisse
25 */
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include "radeon_priv.h"
30 #include "radeon_drm.h"
31 #include "bof.h"
32
33 int radeon_ctx_set_bo_new(struct radeon_ctx *ctx, struct radeon_bo *bo)
34 {
35 void *ptr;
36
37 ptr = realloc(ctx->bo, sizeof(struct radeon_bo) * (ctx->nbo + 1));
38 if (ptr == NULL) {
39 return -ENOMEM;
40 }
41 ctx->bo = ptr;
42 ctx->bo[ctx->nbo] = bo;
43 ctx->nbo++;
44 return 0;
45 }
46
47 struct radeon_bo *radeon_ctx_get_bo(struct radeon_ctx *ctx, unsigned reloc)
48 {
49 struct radeon_cs_reloc *greloc;
50 unsigned i;
51
52 greloc = (void *)(((u8 *)ctx->reloc) + reloc * 4);
53 for (i = 0; i < ctx->nbo; i++) {
54 if (ctx->bo[i]->handle == greloc->handle) {
55 return radeon_bo_incref(ctx->radeon, ctx->bo[i]);
56 }
57 }
58 fprintf(stderr, "%s no bo for reloc[%d 0x%08X] %d\n", __func__, reloc, greloc->handle, ctx->nbo);
59 return NULL;
60 }
61
62 void radeon_ctx_get_placement(struct radeon_ctx *ctx, unsigned reloc, u32 *placement)
63 {
64 struct radeon_cs_reloc *greloc;
65 unsigned i;
66
67 placement[0] = 0;
68 placement[1] = 0;
69 greloc = (void *)(((u8 *)ctx->reloc) + reloc * 4);
70 for (i = 0; i < ctx->nbo; i++) {
71 if (ctx->bo[i]->handle == greloc->handle) {
72 placement[0] = greloc->read_domain | greloc->write_domain;
73 placement[1] = placement[0];
74 return;
75 }
76 }
77 }
78
79 struct radeon_ctx *radeon_ctx(struct radeon *radeon)
80 {
81 struct radeon_ctx *ctx;
82
83 if (radeon == NULL)
84 return NULL;
85 ctx = calloc(1, sizeof(*ctx));
86 if (ctx == NULL)
87 return NULL;
88 ctx->radeon = radeon_incref(radeon);
89 return ctx;
90 }
91
92 struct radeon_ctx *radeon_ctx_incref(struct radeon_ctx *ctx)
93 {
94 ctx->refcount++;
95 return ctx;
96 }
97
98 struct radeon_ctx *radeon_ctx_decref(struct radeon_ctx *ctx)
99 {
100 unsigned i;
101
102 if (ctx == NULL)
103 return NULL;
104 if (--ctx->refcount > 0) {
105 return NULL;
106 }
107
108 for (i = 0; i < ctx->ndraw; i++) {
109 ctx->draw[i] = radeon_draw_decref(ctx->draw[i]);
110 }
111 for (i = 0; i < ctx->nbo; i++) {
112 ctx->bo[i] = radeon_bo_decref(ctx->radeon, ctx->bo[i]);
113 }
114 ctx->radeon = radeon_decref(ctx->radeon);
115 free(ctx->draw);
116 free(ctx->bo);
117 free(ctx->pm4);
118 free(ctx->reloc);
119 memset(ctx, 0, sizeof(*ctx));
120 free(ctx);
121 return NULL;
122 }
123
124 static int radeon_ctx_state_bo(struct radeon_ctx *ctx, struct radeon_state *state)
125 {
126 unsigned i, j;
127 int r;
128
129 if (state == NULL)
130 return 0;
131 for (i = 0; i < state->nbo; i++) {
132 for (j = 0; j < ctx->nbo; j++) {
133 if (state->bo[i] == ctx->bo[j])
134 break;
135 }
136 if (j == ctx->nbo) {
137 radeon_bo_incref(ctx->radeon, state->bo[i]);
138 r = radeon_ctx_set_bo_new(ctx, state->bo[i]);
139 if (r)
140 return r;
141 }
142 }
143 return 0;
144 }
145
146
147 int radeon_ctx_submit(struct radeon_ctx *ctx)
148 {
149 struct drm_radeon_cs drmib;
150 struct drm_radeon_cs_chunk chunks[2];
151 uint64_t chunk_array[2];
152 int r = 0;
153
154 #if 0
155 for (r = 0; r < ctx->cpm4; r++) {
156 fprintf(stderr, "0x%08X\n", ctx->pm4[r]);
157 }
158 #endif
159 drmib.num_chunks = 2;
160 drmib.chunks = (uint64_t)(uintptr_t)chunk_array;
161 chunks[0].chunk_id = RADEON_CHUNK_ID_IB;
162 chunks[0].length_dw = ctx->cpm4;
163 chunks[0].chunk_data = (uint64_t)(uintptr_t)ctx->pm4;
164 chunks[1].chunk_id = RADEON_CHUNK_ID_RELOCS;
165 chunks[1].length_dw = ctx->nreloc * sizeof(struct radeon_cs_reloc) / 4;
166 chunks[1].chunk_data = (uint64_t)(uintptr_t)ctx->reloc;
167 chunk_array[0] = (uint64_t)(uintptr_t)&chunks[0];
168 chunk_array[1] = (uint64_t)(uintptr_t)&chunks[1];
169 #if 1
170 r = drmCommandWriteRead(ctx->radeon->fd, DRM_RADEON_CS, &drmib,
171 sizeof(struct drm_radeon_cs));
172 #endif
173 return r;
174 }
175
176 static int radeon_ctx_reloc(struct radeon_ctx *ctx, struct radeon_bo *bo,
177 unsigned id, unsigned *placement)
178 {
179 unsigned i;
180 struct radeon_cs_reloc *ptr;
181
182 for (i = 0; i < ctx->nreloc; i++) {
183 if (ctx->reloc[i].handle == bo->handle) {
184 ctx->pm4[id] = i * sizeof(struct radeon_cs_reloc) / 4;
185 return 0;
186 }
187 }
188 ptr = realloc(ctx->reloc, sizeof(struct radeon_cs_reloc) * (ctx->nreloc + 1));
189 if (ptr == NULL)
190 return -ENOMEM;
191 ctx->reloc = ptr;
192 ptr[ctx->nreloc].handle = bo->handle;
193 ptr[ctx->nreloc].read_domain = placement[0] | placement [1];
194 ptr[ctx->nreloc].write_domain = placement[0] | placement [1];
195 ptr[ctx->nreloc].flags = 0;
196 ctx->pm4[id] = ctx->nreloc * sizeof(struct radeon_cs_reloc) / 4;
197 ctx->nreloc++;
198 return 0;
199 }
200
201 static int radeon_ctx_state_schedule(struct radeon_ctx *ctx, struct radeon_state *state)
202 {
203 unsigned i, rid, bid, cid;
204 int r;
205
206 if (state == NULL)
207 return 0;
208 memcpy(&ctx->pm4[ctx->id], state->pm4, state->cpm4 * 4);
209 for (i = 0; i < state->nreloc; i++) {
210 rid = state->reloc_pm4_id[i];
211 bid = state->reloc_bo_id[i];
212 cid = ctx->id + rid;
213 r = radeon_ctx_reloc(ctx, state->bo[bid], cid,
214 &state->placement[bid * 2]);
215 if (r) {
216 fprintf(stderr, "%s state %d failed to reloc\n", __func__, state->type);
217 return r;
218 }
219 }
220 ctx->id += state->cpm4;
221 return 0;
222 }
223
224 int radeon_ctx_set_draw_new(struct radeon_ctx *ctx, struct radeon_draw *draw)
225 {
226 struct radeon_draw *pdraw = NULL;
227 struct radeon_draw **ndraw;
228 struct radeon_state *nstate, *ostate;
229 unsigned cpm4, i, cstate;
230 void *tmp;
231 int r = 0;
232
233 ndraw = realloc(ctx->draw, sizeof(void*) * (ctx->ndraw + 1));
234 if (ndraw == NULL)
235 return -ENOMEM;
236 ctx->draw = ndraw;
237 for (i = 0; i < draw->nstate; i++) {
238 r = radeon_ctx_state_bo(ctx, draw->state[i]);
239 if (r)
240 return r;
241 }
242 r = radeon_draw_check(draw);
243 if (r)
244 return r;
245 if (draw->cpm4 >= RADEON_CTX_MAX_PM4) {
246 fprintf(stderr, "%s single draw too big %d, max %d\n",
247 __func__, draw->cpm4, RADEON_CTX_MAX_PM4);
248 return -EINVAL;
249 }
250 tmp = realloc(ctx->state, (ctx->nstate + draw->nstate) * sizeof(void*));
251 if (tmp == NULL)
252 return -ENOMEM;
253 ctx->state = tmp;
254 pdraw = ctx->cdraw;
255 for (i = 0, cpm4 = 0, cstate = ctx->nstate; i < draw->nstate - 1; i++) {
256 nstate = draw->state[i];
257 if (nstate) {
258 if (pdraw && pdraw->state[i]) {
259 ostate = pdraw->state[i];
260 if (ostate->pm4_crc != nstate->pm4_crc) {
261 ctx->state[cstate++] = nstate;
262 cpm4 += nstate->cpm4;
263 }
264 } else {
265 ctx->state[cstate++] = nstate;
266 cpm4 += nstate->cpm4;
267 }
268 }
269 }
270 /* The last state is the draw state always add it */
271 if (draw->state[i] == NULL) {
272 fprintf(stderr, "%s no draw command\n", __func__);
273 return -EINVAL;
274 }
275 ctx->state[cstate++] = draw->state[i];
276 cpm4 += draw->state[i]->cpm4;
277 if ((ctx->draw_cpm4 + cpm4) > RADEON_CTX_MAX_PM4) {
278 /* need to flush */
279 return -EBUSY;
280 }
281 ctx->draw_cpm4 += cpm4;
282 ctx->nstate = cstate;
283 ctx->draw[ctx->ndraw++] = draw;
284 ctx->cdraw = draw;
285 return 0;
286 }
287
288 int radeon_ctx_set_draw(struct radeon_ctx *ctx, struct radeon_draw *draw)
289 {
290 int r;
291
292 radeon_draw_incref(draw);
293 r = radeon_ctx_set_draw_new(ctx, draw);
294 if (r)
295 radeon_draw_decref(draw);
296 return r;
297 }
298
299 int radeon_ctx_pm4(struct radeon_ctx *ctx)
300 {
301 unsigned i;
302 int r;
303
304 free(ctx->pm4);
305 ctx->cpm4 = 0;
306 ctx->pm4 = malloc(ctx->draw_cpm4 * 4);
307 if (ctx->pm4 == NULL)
308 return -EINVAL;
309 for (i = 0, ctx->id = 0; i < ctx->nstate; i++) {
310 r = radeon_ctx_state_schedule(ctx, ctx->state[i]);
311 if (r)
312 return r;
313 }
314 if (ctx->id != ctx->draw_cpm4) {
315 fprintf(stderr, "%s miss predicted pm4 size %d for %d\n",
316 __func__, ctx->draw_cpm4, ctx->id);
317 return -EINVAL;
318 }
319 ctx->cpm4 = ctx->draw_cpm4;
320 return 0;
321 }
322
323 void radeon_ctx_dump_bof(struct radeon_ctx *ctx, const char *file)
324 {
325 bof_t *bcs, *blob, *array, *bo, *size, *handle, *device_id, *root;
326 unsigned i;
327
328 root = device_id = bcs = blob = array = bo = size = handle = NULL;
329 root = bof_object();
330 if (root == NULL)
331 goto out_err;
332 device_id = bof_int32(ctx->radeon->device);
333 if (device_id == NULL)
334 return;
335 if (bof_object_set(root, "device_id", device_id))
336 goto out_err;
337 bof_decref(device_id);
338 device_id = NULL;
339 /* dump relocs */
340 printf("%d relocs\n", ctx->nreloc);
341 blob = bof_blob(ctx->nreloc * 16, ctx->reloc);
342 if (blob == NULL)
343 goto out_err;
344 if (bof_object_set(root, "reloc", blob))
345 goto out_err;
346 bof_decref(blob);
347 blob = NULL;
348 /* dump cs */
349 printf("%d pm4\n", ctx->cpm4);
350 blob = bof_blob(ctx->cpm4 * 4, ctx->pm4);
351 if (blob == NULL)
352 goto out_err;
353 if (bof_object_set(root, "pm4", blob))
354 goto out_err;
355 bof_decref(blob);
356 blob = NULL;
357 /* dump bo */
358 array = bof_array();
359 if (array == NULL)
360 goto out_err;
361 for (i = 0; i < ctx->nbo; i++) {
362 bo = bof_object();
363 if (bo == NULL)
364 goto out_err;
365 size = bof_int32(ctx->bo[i]->size);
366 printf("[%d] %d bo\n", i, size);
367 if (size == NULL)
368 goto out_err;
369 if (bof_object_set(bo, "size", size))
370 goto out_err;
371 bof_decref(size);
372 size = NULL;
373 handle = bof_int32(ctx->bo[i]->handle);
374 if (handle == NULL)
375 goto out_err;
376 if (bof_object_set(bo, "handle", handle))
377 goto out_err;
378 bof_decref(handle);
379 handle = NULL;
380 radeon_bo_map(ctx->radeon, ctx->bo[i]);
381 blob = bof_blob(ctx->bo[i]->size, ctx->bo[i]->data);
382 radeon_bo_unmap(ctx->radeon, ctx->bo[i]);
383 if (blob == NULL)
384 goto out_err;
385 if (bof_object_set(bo, "data", blob))
386 goto out_err;
387 bof_decref(blob);
388 blob = NULL;
389 if (bof_array_append(array, bo))
390 goto out_err;
391 bof_decref(bo);
392 bo = NULL;
393 }
394 if (bof_object_set(root, "bo", array))
395 goto out_err;
396 bof_dump_file(root, file);
397 printf("done dump\n");
398 out_err:
399 bof_decref(blob);
400 bof_decref(array);
401 bof_decref(bo);
402 bof_decref(size);
403 bof_decref(handle);
404 bof_decref(device_id);
405 bof_decref(root);
406 }