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 © 2008 Nicolai Haehnle
* Copyright © 2008 Jérôme Glisse
* All Rights Reserved.
*
* 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 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 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 COPYRIGHT HOLDERS, AUTHORS AND/OR ITS 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.
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*/
/*
* Authors:
* Aapo Tahkola <aet@rasterburn.org>
* Nicolai Haehnle <prefect_@gmx.net>
* Jérôme Glisse <glisse@freedesktop.org>
*/
#include <errno.h>
#include "r300_reg.h"
#include "r300_emit.h"
#include "r300_cmdbuf.h"
#include "radeon_cs.h"
#include "radeon_cs_legacy.h"
#include "radeon_bo_legacy.h"
#include "radeon_context.h"
struct cs_manager_legacy {
struct radeon_cs_manager base;
struct radeon_context *ctx;
/* hack for scratch stuff */
uint32_t pending_age;
uint32_t pending_count;
};
struct cs_reloc_legacy {
struct radeon_cs_reloc base;
uint32_t cindices;
uint32_t *indices;
};
static struct radeon_cs *cs_create(struct radeon_cs_manager *csm,
uint32_t ndw)
{
struct radeon_cs *cs;
cs = (struct radeon_cs*)calloc(1, sizeof(struct radeon_cs));
if (cs == NULL) {
return NULL;
}
cs->csm = csm;
cs->ndw = (ndw + 0x3FF) & (~0x3FF);
cs->packets = (uint32_t*)malloc(4*cs->ndw);
if (cs->packets == NULL) {
free(cs);
return NULL;
}
cs->relocs_total_size = 0;
return cs;
}
static int cs_write_dword(struct radeon_cs *cs, uint32_t dword)
{
if (cs->cdw >= cs->ndw) {
uint32_t tmp, *ptr;
tmp = (cs->cdw + 1 + 0x3FF) & (~0x3FF);
ptr = (uint32_t*)realloc(cs->packets, 4 * tmp);
if (ptr == NULL) {
return -ENOMEM;
}
cs->packets = ptr;
cs->ndw = tmp;
}
cs->packets[cs->cdw++] = dword;
if (cs->section) {
cs->section_cdw++;
}
return 0;
}
static int cs_write_reloc(struct radeon_cs *cs,
struct radeon_bo *bo,
uint32_t soffset,
uint32_t eoffset,
uint32_t domains)
{
struct cs_reloc_legacy *relocs;
int i;
relocs = (struct cs_reloc_legacy *)cs->relocs;
/* check reloc window */
if (eoffset > bo->size) {
return -EINVAL;
}
if (soffset > eoffset) {
return -EINVAL;
}
/* check if bo is already referenced */
for(i = 0; i < cs->crelocs; i++) {
uint32_t *indices;
if (relocs[i].base.bo->handle == bo->handle) {
/* update start offset and size */
if (eoffset > relocs[i].base.eoffset) {
relocs[i].base.eoffset = eoffset;
}
if (soffset < relocs[i].base.soffset) {
relocs[i].base.soffset = soffset;
}
relocs[i].base.size = relocs[i].base.eoffset -
relocs[i].base.soffset;
relocs[i].base.domains |= domains;
relocs[i].cindices += 1;
indices = (uint32_t*)realloc(relocs[i].indices,
relocs[i].cindices * 4);
if (indices == NULL) {
relocs[i].cindices -= 1;
return -ENOMEM;
}
relocs[i].indices = indices;
relocs[i].indices[relocs[i].cindices - 1] = cs->cdw - 1;
return 0;
}
}
/* add bo to reloc */
relocs = (struct cs_reloc_legacy*)
realloc(cs->relocs,
sizeof(struct cs_reloc_legacy) * (cs->crelocs + 1));
if (relocs == NULL) {
return -ENOMEM;
}
cs->relocs = relocs;
relocs[cs->crelocs].base.bo = bo;
relocs[cs->crelocs].base.soffset = soffset;
relocs[cs->crelocs].base.eoffset = eoffset;
relocs[cs->crelocs].base.size = eoffset - soffset;
relocs[cs->crelocs].base.domains = domains;
relocs[cs->crelocs].indices = (uint32_t*)malloc(4);
if (relocs[cs->crelocs].indices == NULL) {
return -ENOMEM;
}
relocs[cs->crelocs].indices[0] = cs->cdw - 1;
relocs[cs->crelocs].cindices = 1;
cs->relocs_total_size += radeon_bo_legacy_relocs_size(bo);
cs->crelocs++;
radeon_bo_ref(bo);
return 0;
}
static int cs_begin(struct radeon_cs *cs,
uint32_t ndw,
const char *file,
const char *func,
int line)
{
if (cs->section) {
fprintf(stderr, "CS already in a section(%s,%s,%d)\n",
cs->section_file, cs->section_func, cs->section_line);
fprintf(stderr, "CS can't start section(%s,%s,%d)\n",
file, func, line);
return -EPIPE;
}
cs->section = 1;
cs->section_ndw = ndw;
cs->section_cdw = 0;
cs->section_file = file;
cs->section_func = func;
cs->section_line = line;
return 0;
}
static int cs_end(struct radeon_cs *cs,
const char *file,
const char *func,
int line)
{
if (!cs->section) {
fprintf(stderr, "CS no section to end at (%s,%s,%d)\n",
file, func, line);
return -EPIPE;
}
cs->section = 0;
if (cs->section_ndw != cs->section_cdw) {
fprintf(stderr, "CS section size missmatch start at (%s,%s,%d)\n",
cs->section_file, cs->section_func, cs->section_line);
fprintf(stderr, "CS section end at (%s,%s,%d)\n",
file, func, line);
return -EPIPE;
}
return 0;
}
static int cs_process_relocs(struct radeon_cs *cs)
{
struct cs_manager_legacy *csm = (struct cs_manager_legacy*)cs->csm;
struct cs_reloc_legacy *relocs;
int i, j, r;
if (!IS_R300_CLASS(csm->ctx->radeonScreen)) {
/* FIXME: r300 only right now */
return -EINVAL;
}
csm = (struct cs_manager_legacy*)cs->csm;
relocs = (struct cs_reloc_legacy *)cs->relocs;
for (i = 0; i < cs->crelocs; i++) {
for (j = 0; j < relocs[i].cindices; j++) {
uint32_t soffset, eoffset;
soffset = relocs[i].base.soffset;
eoffset = relocs[i].base.eoffset;
r = radeon_bo_legacy_validate(relocs[i].base.bo,
&soffset, &eoffset);
if (r) {
fprintf(stderr, "validated %p [0x%08X, 0x%08X]\n",
relocs[i].base.bo, soffset, eoffset);
return r;
}
cs->packets[relocs[i].indices[j]] += soffset;
if (cs->packets[relocs[i].indices[j]] >= eoffset) {
radeon_bo_debug(relocs[i].base.bo, 12);
fprintf(stderr, "validated %p [0x%08X, 0x%08X]\n",
relocs[i].base.bo, soffset, eoffset);
fprintf(stderr, "above end: %p 0x%08X 0x%08X\n",
relocs[i].base.bo,
cs->packets[relocs[i].indices[j]],
eoffset);
exit(0);
return -EINVAL;
}
}
}
return 0;
}
static int cs_set_age(struct radeon_cs *cs)
{
struct cs_manager_legacy *csm = (struct cs_manager_legacy*)cs->csm;
struct cs_reloc_legacy *relocs;
int i;
relocs = (struct cs_reloc_legacy *)cs->relocs;
for (i = 0; i < cs->crelocs; i++) {
radeon_bo_legacy_pending(relocs[i].base.bo, csm->pending_age);
radeon_bo_unref(relocs[i].base.bo);
}
return 0;
}
static int cs_emit(struct radeon_cs *cs)
{
struct cs_manager_legacy *csm = (struct cs_manager_legacy*)cs->csm;
drm_radeon_cmd_buffer_t cmd;
drm_r300_cmd_header_t age;
uint64_t ull;
int r;
/* please flush pipe do all pending work */
cs_write_dword(cs, cmdpacket0(R300_SC_SCREENDOOR, 1));
cs_write_dword(cs, 0x0);
cs_write_dword(cs, cmdpacket0(R300_SC_SCREENDOOR, 1));
cs_write_dword(cs, 0x00FFFFFF);
cs_write_dword(cs, cmdpacket0(R300_SC_HYPERZ, 1));
cs_write_dword(cs, 0x0);
cs_write_dword(cs, cmdpacket0(R300_US_CONFIG, 1));
cs_write_dword(cs, 0x0);
cs_write_dword(cs, cmdpacket0(R300_ZB_CNTL, 1));
cs_write_dword(cs, 0x0);
cs_write_dword(cs, cmdwait(R300_WAIT_3D));
cs_write_dword(cs, cmdpacket0(R300_RB3D_DSTCACHE_CTLSTAT, 1));
cs_write_dword(cs, R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D);
cs_write_dword(cs, cmdpacket0(R300_ZB_ZCACHE_CTLSTAT, 1));
cs_write_dword(cs, R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE);
cs_write_dword(cs, cmdwait(R300_WAIT_3D | R300_WAIT_3D_CLEAN));
/* append buffer age */
age.scratch.cmd_type = R300_CMD_SCRATCH;
/* Scratch register 2 corresponds to what radeonGetAge polls */
csm->pending_age = 0;
csm->pending_count = 1;
ull = (uint64_t) (intptr_t) &csm->pending_age;
age.scratch.reg = 2;
age.scratch.n_bufs = 1;
age.scratch.flags = 0;
radeon_cs_write_dword(cs, age.u);
radeon_cs_write_dword(cs, ull & 0xffffffff);
radeon_cs_write_dword(cs, ull >> 32);
radeon_cs_write_dword(cs, 0);
r = cs_process_relocs(cs);
if (r) {
return 0;
}
cmd.buf = (char *)cs->packets;
cmd.bufsz = cs->cdw * 4;
if (csm->ctx->state.scissor.enabled) {
cmd.nbox = csm->ctx->state.scissor.numClipRects;
cmd.boxes = (drm_clip_rect_t *) csm->ctx->state.scissor.pClipRects;
} else {
cmd.nbox = csm->ctx->numClipRects;
cmd.boxes = (drm_clip_rect_t *) csm->ctx->pClipRects;
}
r = drmCommandWrite(cs->csm->fd, DRM_RADEON_CMDBUF, &cmd, sizeof(cmd));
cs_set_age(cs);
return r;
}
static int cs_destroy(struct radeon_cs *cs)
{
free(cs->relocs);
free(cs->packets);
free(cs);
return 0;
}
static int cs_erase(struct radeon_cs *cs)
{
free(cs->relocs);
cs->relocs_total_size = 0;
cs->relocs = NULL;
cs->crelocs = 0;
cs->cdw = 0;
cs->section = 0;
return 0;
}
static int cs_need_flush(struct radeon_cs *cs)
{
/* FIXME: we should get the texture heap size */
return (cs->relocs_total_size > (7*1024*1024));
}
struct radeon_cs_funcs radeon_cs_funcs = {
cs_create,
cs_write_dword,
cs_write_reloc,
cs_begin,
cs_end,
cs_emit,
cs_destroy,
cs_erase,
cs_need_flush
};
struct radeon_cs_manager *radeon_cs_manager_legacy(struct radeon_context *ctx)
{
struct cs_manager_legacy *csm;
csm = (struct cs_manager_legacy*)
calloc(1, sizeof(struct cs_manager_legacy));
if (csm == NULL) {
return NULL;
}
csm->base.funcs = &radeon_cs_funcs;
csm->base.fd = ctx->dri.fd;
csm->ctx = ctx;
csm->pending_age = 1;
return (struct radeon_cs_manager*)csm;
}
|