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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
/*
* Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* on the rights to use, copy, modify, merge, publish, distribute, sub
* license, and/or sell copies of the Software, and to permit persons to whom
* the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
* USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Authors:
* Jerome Glisse
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "radeon_priv.h"
#include "radeon_drm.h"
#include "bof.h"
static int radeon_ctx_set_bo_new(struct radeon_ctx *ctx, struct radeon_ws_bo *bo)
{
if (ctx->nbo >= RADEON_CTX_MAX_PM4)
return -EBUSY;
/* take a reference to the kernel bo */
radeon_bo_reference(ctx->radeon, &ctx->bo[ctx->nbo], radeon_bo_pb_get_bo(bo->pb));
ctx->nbo++;
return 0;
}
static void radeon_ctx_get_placement(struct radeon_ctx *ctx, unsigned reloc, u32 *placement)
{
struct radeon_cs_reloc *greloc;
unsigned i;
placement[0] = 0;
placement[1] = 0;
greloc = (void *)(((u8 *)ctx->reloc) + reloc * 4);
for (i = 0; i < ctx->nbo; i++) {
if (ctx->bo[i]->handle == greloc->handle) {
placement[0] = greloc->read_domain | greloc->write_domain;
placement[1] = placement[0];
return;
}
}
}
void radeon_ctx_clear(struct radeon_ctx *ctx)
{
for (int i = 0; i < ctx->nbo; i++) {
radeon_bo_reference(ctx->radeon, &ctx->bo[i], NULL);
}
ctx->ndwords = RADEON_CTX_MAX_PM4;
ctx->cdwords = 0;
ctx->nreloc = 0;
ctx->nbo = 0;
}
struct radeon_ctx *radeon_ctx_init(struct radeon *radeon)
{
struct radeon_ctx *ctx;
if (radeon == NULL)
return NULL;
ctx = calloc(1, sizeof(struct radeon_ctx));
ctx->radeon = radeon_incref(radeon);
radeon_ctx_clear(ctx);
ctx->pm4 = malloc(RADEON_CTX_MAX_PM4 * 4);
if (ctx->pm4 == NULL) {
radeon_ctx_fini(ctx);
return NULL;
}
ctx->reloc = malloc(sizeof(struct radeon_cs_reloc) * RADEON_CTX_MAX_PM4);
if (ctx->reloc == NULL) {
radeon_ctx_fini(ctx);
return NULL;
}
ctx->bo = calloc(sizeof(void *), RADEON_CTX_MAX_PM4);
if (ctx->bo == NULL) {
radeon_ctx_fini(ctx);
return NULL;
}
return ctx;
}
void radeon_ctx_fini(struct radeon_ctx *ctx)
{
unsigned i;
if (ctx == NULL)
return;
for (i = 0; i < ctx->nbo; i++) {
radeon_bo_reference(ctx->radeon, &ctx->bo[i], NULL);
}
ctx->radeon = radeon_decref(ctx->radeon);
free(ctx->bo);
free(ctx->pm4);
free(ctx->reloc);
free(ctx);
}
static int radeon_ctx_state_bo(struct radeon_ctx *ctx, struct radeon_state *state)
{
unsigned i, j;
int r;
struct radeon_bo *state_bo;
if (state == NULL)
return 0;
for (i = 0; i < state->nbo; i++) {
for (j = 0; j < ctx->nbo; j++) {
state_bo = radeon_bo_pb_get_bo(state->bo[i]->pb);
if (state_bo == ctx->bo[j])
break;
}
if (j == ctx->nbo) {
r = radeon_ctx_set_bo_new(ctx, state->bo[i]);
if (r)
return r;
}
}
return 0;
}
int radeon_ctx_submit(struct radeon_ctx *ctx)
{
struct drm_radeon_cs drmib;
struct drm_radeon_cs_chunk chunks[2];
uint64_t chunk_array[2];
int r = 0;
if (!ctx->cdwords)
return 0;
radeon_bo_pbmgr_flush_maps(ctx->radeon->kman);
#if 0
for (r = 0; r < ctx->cdwords; r++) {
fprintf(stderr, "0x%08X\n", ctx->pm4[r]);
}
#endif
drmib.num_chunks = 2;
drmib.chunks = (uint64_t)(uintptr_t)chunk_array;
chunks[0].chunk_id = RADEON_CHUNK_ID_IB;
chunks[0].length_dw = ctx->cdwords;
chunks[0].chunk_data = (uint64_t)(uintptr_t)ctx->pm4;
chunks[1].chunk_id = RADEON_CHUNK_ID_RELOCS;
chunks[1].length_dw = ctx->nreloc * sizeof(struct radeon_cs_reloc) / 4;
chunks[1].chunk_data = (uint64_t)(uintptr_t)ctx->reloc;
chunk_array[0] = (uint64_t)(uintptr_t)&chunks[0];
chunk_array[1] = (uint64_t)(uintptr_t)&chunks[1];
#if 1
r = drmCommandWriteRead(ctx->radeon->fd, DRM_RADEON_CS, &drmib,
sizeof(struct drm_radeon_cs));
#endif
return r;
}
static int radeon_ctx_reloc(struct radeon_ctx *ctx, struct radeon_ws_bo *bo,
unsigned id, unsigned *placement)
{
unsigned i;
unsigned bo_handle = radeon_ws_bo_get_handle(bo);
for (i = 0; i < ctx->nreloc; i++) {
if (ctx->reloc[i].handle == bo_handle) {
ctx->pm4[id] = i * sizeof(struct radeon_cs_reloc) / 4;
return 0;
}
}
if (ctx->nreloc >= RADEON_CTX_MAX_PM4) {
return -EBUSY;
}
ctx->reloc[ctx->nreloc].handle = bo_handle;
ctx->reloc[ctx->nreloc].read_domain = placement[0] | placement [1];
ctx->reloc[ctx->nreloc].write_domain = placement[0] | placement [1];
ctx->reloc[ctx->nreloc].flags = 0;
ctx->pm4[id] = ctx->nreloc * sizeof(struct radeon_cs_reloc) / 4;
ctx->nreloc++;
return 0;
}
static int radeon_ctx_state_schedule(struct radeon_ctx *ctx, struct radeon_state *state)
{
unsigned i, rid, bid, cid;
int r;
if (state == NULL)
return 0;
if (state->cpm4 > ctx->ndwords) {
return -EBUSY;
}
memcpy(&ctx->pm4[ctx->cdwords], state->pm4, state->cpm4 * 4);
for (i = 0; i < state->nreloc; i++) {
rid = state->reloc_pm4_id[i];
bid = state->reloc_bo_id[i];
cid = ctx->cdwords + rid;
r = radeon_ctx_reloc(ctx, state->bo[bid], cid,
&state->placement[bid * 2]);
if (r) {
fprintf(stderr, "%s state %d failed to reloc\n", __func__, state->stype->stype);
return r;
}
}
ctx->cdwords += state->cpm4;
ctx->ndwords -= state->cpm4;
return 0;
}
int radeon_ctx_set_query_state(struct radeon_ctx *ctx, struct radeon_state *state)
{
int r = 0;
/* !!! ONLY ACCEPT QUERY STATE HERE !!! */
r = radeon_state_pm4(state);
if (r)
return r;
/* BEGIN/END query are balanced in the same cs so account for END
* END query when scheduling BEGIN query
*/
switch (state->stype->stype) {
case R600_STATE_QUERY_BEGIN:
/* is there enough place for begin & end */
if ((state->cpm4 * 2) > ctx->ndwords)
return -EBUSY;
ctx->ndwords -= state->cpm4;
break;
case R600_STATE_QUERY_END:
ctx->ndwords += state->cpm4;
break;
default:
return -EINVAL;
}
return radeon_ctx_state_schedule(ctx, state);
}
int radeon_ctx_set_draw(struct radeon_ctx *ctx, struct radeon_draw *draw)
{
unsigned previous_cdwords;
int r = 0;
int i;
for (i = 0; i < ctx->radeon->max_states; i++) {
r = radeon_ctx_state_bo(ctx, draw->state[i]);
if (r)
return r;
}
previous_cdwords = ctx->cdwords;
for (i = 0; i < ctx->radeon->max_states; i++) {
if (draw->state[i]) {
r = radeon_ctx_state_schedule(ctx, draw->state[i]);
if (r) {
ctx->cdwords = previous_cdwords;
return r;
}
}
}
return 0;
}
#if 0
int radeon_ctx_pm4(struct radeon_ctx *ctx)
{
unsigned i;
int r;
free(ctx->pm4);
ctx->cpm4 = 0;
ctx->pm4 = malloc(ctx->draw_cpm4 * 4);
if (ctx->pm4 == NULL)
return -EINVAL;
for (i = 0, ctx->id = 0; i < ctx->nstate; i++) {
}
if (ctx->id != ctx->draw_cpm4) {
fprintf(stderr, "%s miss predicted pm4 size %d for %d\n",
__func__, ctx->draw_cpm4, ctx->id);
return -EINVAL;
}
ctx->cpm4 = ctx->draw_cpm4;
return 0;
}
#endif
void radeon_ctx_dump_bof(struct radeon_ctx *ctx, const char *file)
{
bof_t *bcs, *blob, *array, *bo, *size, *handle, *device_id, *root;
unsigned i;
unsigned bo_size;
root = device_id = bcs = blob = array = bo = size = handle = NULL;
root = bof_object();
if (root == NULL)
goto out_err;
device_id = bof_int32(ctx->radeon->device);
if (device_id == NULL)
return;
if (bof_object_set(root, "device_id", device_id))
goto out_err;
bof_decref(device_id);
device_id = NULL;
/* dump relocs */
blob = bof_blob(ctx->nreloc * 16, ctx->reloc);
if (blob == NULL)
goto out_err;
if (bof_object_set(root, "reloc", blob))
goto out_err;
bof_decref(blob);
blob = NULL;
/* dump cs */
blob = bof_blob(ctx->cdwords * 4, ctx->pm4);
if (blob == NULL)
goto out_err;
if (bof_object_set(root, "pm4", blob))
goto out_err;
bof_decref(blob);
blob = NULL;
/* dump bo */
array = bof_array();
if (array == NULL)
goto out_err;
for (i = 0; i < ctx->nbo; i++) {
bo = bof_object();
if (bo == NULL)
goto out_err;
bo_size = ctx->bo[i]->size;
size = bof_int32(bo_size);
if (size == NULL)
goto out_err;
if (bof_object_set(bo, "size", size))
goto out_err;
bof_decref(size);
size = NULL;
handle = bof_int32(ctx->bo[i]->handle);
if (handle == NULL)
goto out_err;
if (bof_object_set(bo, "handle", handle))
goto out_err;
bof_decref(handle);
handle = NULL;
radeon_bo_map(ctx->radeon, ctx->bo[i]);
blob = bof_blob(bo_size, ctx->bo[i]->data);
radeon_bo_unmap(ctx->radeon, ctx->bo[i]);
if (blob == NULL)
goto out_err;
if (bof_object_set(bo, "data", blob))
goto out_err;
bof_decref(blob);
blob = NULL;
if (bof_array_append(array, bo))
goto out_err;
bof_decref(bo);
bo = NULL;
}
if (bof_object_set(root, "bo", array))
goto out_err;
bof_dump_file(root, file);
out_err:
bof_decref(blob);
bof_decref(array);
bof_decref(bo);
bof_decref(size);
bof_decref(handle);
bof_decref(device_id);
bof_decref(root);
}
|