summaryrefslogtreecommitdiff
path: root/r600_batch.c
blob: 3c4ef580437c3cb0f06d4b8dadb6ad55d5bf7a3e (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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
 * Copyright © 2010 Jerome Glisse <glisse@freedesktop.org>
 *
 * This file is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#include "r600.h"
#include "r600d.h"

/* state creation functions prototype */
int r600_framebuffer_emit(struct r600_winsys *rdev, struct r600_atom *atom, void *data, struct radeon_ib *ib);
int r600_framebuffer_create(struct r600_winsys *rdev, struct r600_atom *atom, void *data);
int r600_cb_cntl_create(struct r600_winsys *rdev, struct r600_atom *atom, void *data);
int r600_rasterizer_create(struct r600_winsys *rdev, struct r600_atom *atom, void *data);
int r600_viewport_create(struct r600_winsys *rdev, struct r600_atom *atom, void *data);
int r600_scissor_create(struct r600_winsys *rdev, struct r600_atom *atom, void *data);
int r600_blend_create(struct r600_winsys *rdev, struct r600_atom *atom, void *data);
int r600_constants_create(struct r600_winsys *rdev, struct r600_atom *atom, void *data);
int r600_dsa_create(struct r600_winsys *rdev, struct r600_atom *atom, void *data);
int r600_vs_shader_emit(struct r600_winsys *rdev, struct r600_atom *atom, void *data, struct radeon_ib *ib);
int r600_vs_shader_create(struct r600_winsys *rdev, struct r600_atom *atom, void *data);
int r600_fs_shader_emit(struct r600_winsys *rdev, struct r600_atom *atom, void *data, struct radeon_ib *ib);
int r600_fs_shader_create(struct r600_winsys *rdev, struct r600_atom *atom, void *data);

/*
 * helpers functions
 */
static void r600_emit_flush(struct r600_winsys *rdev,
				struct radeon_ib *ib,
				struct radeon_bo *bo,
				u32 flags)
{
	ib->ptr[ib->cpkts++] = PKT3(PKT3_SURFACE_SYNC, 3);
	ib->ptr[ib->cpkts++] = flags;
	ib->ptr[ib->cpkts++] = radeon_bo_size(bo) >> 8;
	ib->ptr[ib->cpkts++] = 0x00000000;
	ib->ptr[ib->cpkts++] = 0x0000000A;
	ib->ptr[ib->cpkts++] = PKT3(PKT3_NOP, 0);
	ib->ptr[ib->cpkts++] = radeon_ib_reloc(ib, bo, RADEON_GEM_DOMAIN_VRAM |
						RADEON_GEM_DOMAIN_GTT);
}

static void r600_emit_resources(struct r600_winsys *rdev,
				struct radeon_ib *ib,
				struct radeon_bo *bo,
				u32 dw0, u32 dw1, u32 dw2, u32 dw3,
				u32 dw4, u32 dw5, u32 dw6, u32 dw7)
{
	ib->ptr[ib->cpkts++] = PKT3(PKT3_SURFACE_SYNC, 3);
	ib->ptr[ib->cpkts++] = 0x01000000;
	ib->ptr[ib->cpkts++] = radeon_bo_size(bo) >> 8;
	ib->ptr[ib->cpkts++] = 0x00000000;
	ib->ptr[ib->cpkts++] = 0x0000000A;
	ib->ptr[ib->cpkts++] = PKT3(PKT3_NOP, 0);
	ib->ptr[ib->cpkts++] = radeon_ib_reloc(ib, bo, RADEON_GEM_DOMAIN_VRAM |
						RADEON_GEM_DOMAIN_GTT);
	ib->ptr[ib->cpkts++] = PKT3(PKT3_SET_RESOURCE, 7);
	ib->ptr[ib->cpkts++] = dw0;
	ib->ptr[ib->cpkts++] = dw1;
	ib->ptr[ib->cpkts++] = dw2;
	ib->ptr[ib->cpkts++] = dw3;
	ib->ptr[ib->cpkts++] = dw4;
	ib->ptr[ib->cpkts++] = dw5;
	ib->ptr[ib->cpkts++] = dw6;
	ib->ptr[ib->cpkts++] = dw7;
	ib->ptr[ib->cpkts++] = PKT3(PKT3_NOP, 0);
	ib->ptr[ib->cpkts++] = radeon_ib_reloc(ib, bo, RADEON_GEM_DOMAIN_VRAM |
						RADEON_GEM_DOMAIN_GTT);
}

/*
 * r600_batch
 */
static int r600_batch_alloc(struct r600_batch **batch)
{
	struct r600_batch *rbatch;

	*batch = NULL;
	rbatch = malloc(sizeof(*rbatch));
	if (rbatch == NULL)
		return -ENOMEM;
	INIT_LIST_HEAD(&rbatch->list);
	INIT_LIST_HEAD(&rbatch->pre_flushes);
	INIT_LIST_HEAD(&rbatch->post_flushes);
	rbatch->nemit_atoms = 0;
	rbatch->nfs_resources = 0;
	*batch = rbatch;
	return 0;
}

/*
 * r600_batches
 */
static int r600_batches_fs_resource_is_present(struct r600_winsys *rdev,
						struct r600_batches *batches,
						struct drm_r600_vs_buffer *buffer)
{
	int i;

	for (i = 0; i < batches->nfs_resources; i++) {
		if (!memcmp(&batches->fs_resource[i], buffer, sizeof(struct drm_r600_vs_buffer))) {
			buffer->resource_id = i;
			return 1;
		}
	}
	return 0;
}

static int r600_batches_fs_resource_add(struct r600_winsys *rdev,
						struct r600_batches *batches,
						struct drm_r600_vs_buffer *buffer)
{
	int i = batches->nfs_resources++;

	buffer->resource_id = i;
	memcpy(&batches->fs_resource[i].drm, buffer, sizeof(struct drm_r600_vs_buffer));
	batches->fs_resource[i].bo = radeon_bo_lookup(rdev, buffer->handle);
	if (batches->fs_resource[i].bo == NULL)
		return -EINVAL;
	return 0;
}

static void r600_batches_clear_locked(struct r600_winsys *rdev, struct r600_batches *batches)
{
	struct r600_batch *batch, *n;
	int i;

	list_for_each_entry_safe(batch, n, &batches->batches, list) {
		for (i = 0; i < R600_BATCH_NATOMS; i++) {
			if (batch->atoms[i])
				r600_atom_put(batch->atoms[i]);
		}
		r600_atom_flush_cleanup(&batch->pre_flushes);
		r600_atom_flush_cleanup(&batch->post_flushes);
		list_del(&batch->list);
		free(batch);
	}
	INIT_LIST_HEAD(&batches->batches);
	for (i = 0; i < batches->nfs_resources; i++) {
		radeon_bo_unref(batches->fs_resource[i].bo);
	}
	batches->nfs_resources = 0;
	r700_batches_states_default(rdev, batches);
	batches->npkts = batches->ib->cpkts;
}

static int r600_batches_flush_locked(struct r600_winsys *rdev, struct r600_batches *batches)
{
	struct r600_batch *batch;
	struct r600_atom_flush *flush;
	int r, i;

	for (i = 0; i < batches->nfs_resources; i++) {
		r600_emit_resources(rdev, batches->ib, batches->fs_resource[i].bo,
					(batches->fs_resource[i].drm.resource_id + 160) * 7,
					batches->fs_resource[i].drm.sq_vtx_constant_word0,
					radeon_bo_size(batches->fs_resource[i].bo) -
					batches->fs_resource[i].drm.sq_vtx_constant_word0,
					batches->fs_resource[i].drm.sq_vtx_constant_word2,
					batches->fs_resource[i].drm.sq_vtx_constant_word3,
					0, 0, 0xC0000000);
	}
	list_for_each_entry(batch, &batches->batches, list) {
		list_for_each_entry(flush, &batch->pre_flushes, list) {
			r600_emit_flush(rdev, batches->ib, flush->bo, flush->flags);
		}
		for (i = 0; i < batch->nemit_atoms; i++) {
			r = batch->emit_atoms[i]->emit(rdev, batch->emit_atoms[i], batch, batches->ib);
			if (r)
				goto out_err;
		}
		r = r600_draw_cmd_emit(batches->ib, &batch->drm);
		/* flush + wait until */
		batches->ib->ptr[batches->ib->cpkts++] = PKT3(PKT3_EVENT_WRITE, 0);
		batches->ib->ptr[batches->ib->cpkts++] = 0x00000016;
		batches->ib->ptr[batches->ib->cpkts++] = PKT3(PKT3_SET_CONFIG_REG, 1);
		batches->ib->ptr[batches->ib->cpkts++] = 0x00000010;
		batches->ib->ptr[batches->ib->cpkts++] = 0x00028000;
		list_for_each_entry(flush, &batch->post_flushes, list) {
			r600_emit_flush(rdev, batches->ib, flush->bo, flush->flags);
		}
	}
	r = radeon_ib_schedule(rdev, batches->ib);
out_err:
	/* FIXME helper function */
	batches->ib->cpkts = 0;
	batches->ib->nrelocs = 0;
	r600_batches_clear_locked(rdev, batches);
	return r;
}

int r600_batches_queue(struct r600_winsys *rdev, struct r600_request *rq)
{
	struct drm_r600_batch *batch = rq->data;
	struct r600_batch *rbatch;
	struct r600_batches *batches =  &rdev->batches;
	int r, i, j;

	r = r600_batch_alloc(&rbatch);
	if (r)
		return r;
	memcpy(&rbatch->inputs, &batch->inputs, sizeof(struct r600_vs_input));
	i = 0;
	if (batch->blend == NULL || batch->cb_cntl == NULL ||
		batch->rasterizer == NULL || batch->viewport == NULL ||
		batch->framebuffer == NULL || batch->dsa == NULL ||
		batch->vs_shader == NULL || batch->fs_shader == NULL ||
		batch->scissor == NULL) {
		free(rbatch);
		fprintf(stderr, "invalid batch\n");
		return -EINVAL;
	}
	rbatch->atoms[i++] = batch->blend;
	rbatch->atoms[i++] = batch->cb_cntl;
	rbatch->atoms[i++] = batch->rasterizer;
	rbatch->atoms[i++] = batch->viewport;
	rbatch->atoms[i++] = batch->scissor;
	rbatch->atoms[i++] = batch->vs_shader;
	rbatch->atoms[i++] = batch->fs_shader;
	rbatch->atoms[i++] = batch->dsa;
	rbatch->atoms[i++] = batch->vs_constants;
	rbatch->atoms[i++] = batch->ps_constants;
	rbatch->atoms[i++] = batch->framebuffer;
	memcpy(&rbatch->drm, batch, sizeof(struct drm_r600_batch));
reprocess:
	r600_atom_flush_cleanup(&rbatch->pre_flushes);
	r600_atom_flush_cleanup(&rbatch->post_flushes);
	rbatch->nflushes = 0;
	rbatch->npkts = 0;
	/* flush + wait until = 5dw */
	rbatch->npkts += 5;
	rbatch->npkts += r600_draw_cmd_size(batch);
	for (i = 0; i < rbatch->inputs.drm.nbuffers; i++) {
		if (!r600_batches_fs_resource_is_present(rdev, batches, &rbatch->inputs.drm.buffers[i])) {
			rbatch->nfs_resources += 1;
			rbatch->inputs.drm.buffers[i].resource_id = -1;
		}
	}
	rbatch->npkts += rbatch->nfs_resources * 18;
	for (i = 0; i < R600_BATCH_NATOMS; i++) {
		if (rbatch->atoms[i]) {
			for (j = 0; j < rbatch->atoms[i]->nbo; j++) {
				r = r600_atom_flush_add(&rbatch->pre_flushes, rbatch->atoms[i]->bo[j],  rbatch->atoms[i]->flags[j]);
				if (r < 0)
					goto out_err;
			}
			rbatch->nflushes += rbatch->atoms[i]->nbo;
			rbatch->emit_atoms[rbatch->nemit_atoms++] = rbatch->atoms[i];
		}
	}
	/* add flush */
	rbatch->npkts += rbatch->nflushes * 7;
	/* FIXME shader flush should be conditional only if we change shaders */
	rbatch->npkts += 7;
	/* if batch is bigger than ib size it's an invalid one, this should
	 * not happen
	 */
	if (rbatch->npkts > batches->ib->length_dw) {
		fprintf(stderr, "single batch to big (%d) to fit into ib (%d)\n",
			rbatch->npkts, batches->ib->length_dw);
		goto out_err;
	}
	/* flush or not ? */
	if (batches->npkts + rbatch->npkts > batches->ib->length_dw) {
		r = r600_batches_flush_locked(rdev, batches);
		if (r)
			goto out_err;
		goto reprocess;
	}
	/* batch is queued */
	for (i = 0; i < R600_BATCH_NATOMS; i++) {
		if (rbatch->atoms[i]) {
			batches->last_id[i] = rbatch->atoms[i]->id;
		}
	}
	r600_winsys_set_bo_list(rdev, rq->nbo, rq->bo);
	for (i = 0; i < rbatch->inputs.drm.nbuffers; i++) {
		if (rbatch->inputs.drm.buffers[i].resource_id == -1) {
			r = r600_batches_fs_resource_add(rdev, batches, &rbatch->inputs.drm.buffers[i]);
			if (r)
				goto out_err;
		}
	}
	batches->npkts += rbatch->npkts;
	list_add_tail(&rbatch->list, &batches->batches);
	return 0;
out_err:
	for (i = 0; i < R600_BATCH_NATOMS; i++) {
		if (rbatch->atoms[i])
			r600_atom_put(rbatch->atoms[i]);
	}
	free(rbatch);
	return r;
}

static int r600_batches_init(struct r600_winsys *rdev, struct r600_batches *batches)
{
	int r;

	memset(batches, 0 , sizeof(struct r600_batches));
	INIT_LIST_HEAD(&batches->batches);
	r = radeon_ib_get(rdev, &batches->ib);
	if (r)
		return r;
	r700_batches_states_default(rdev, batches);
	batches->npkts = batches->ib->cpkts;
	return 0;
}

static void r600_batches_cleanup_locked(struct r600_winsys *rdev, struct r600_batches *batches)
{
	r600_batches_clear_locked(rdev, batches);
	radeon_ib_free(batches->ib);
	batches->ib = NULL;
}

int r600_batches_flush(struct r600_winsys *rdev)
{
	int r;

	r = r600_batches_flush_locked(rdev, &rdev->batches);
	return r;
}

int r600_atoms_init(struct r600_winsys *rdev)
{
	rdev->npipes = 2;
	rdev->nbanks = 4;
	rdev->group_bytes = 256;
	return r600_batches_init(rdev, &rdev->batches);
}

void r600_atoms_release(struct r600_winsys *rdev)
{
	r600_batches_cleanup_locked(rdev, &rdev->batches);
}

/*
 * r600 atom core functions
 */
static struct r600_atom_funcs _r600_atom_funcs[] = {
	{0, 0, NULL, NULL},
	{R600_ATOM_FRAMEBUFFER, sizeof(struct drm_r600_framebuffer),
		&r600_framebuffer_create, &r600_framebuffer_emit},
	{R600_ATOM_RASTERIZER, sizeof(struct drm_r600_rasterizer),
		&r600_rasterizer_create, &r600_atom_emit_default},
	{R600_ATOM_CB_CNTL, sizeof(struct drm_r600_cb_cntl),
		&r600_cb_cntl_create, &r600_atom_emit_default},
	{R600_ATOM_VIEWPORT, sizeof(struct drm_r600_viewport),
		&r600_viewport_create, &r600_atom_emit_default},
	{R600_ATOM_SCISSOR, sizeof(struct drm_r600_scissor),
		&r600_scissor_create, &r600_atom_emit_default},
	{R600_ATOM_BLEND, sizeof(struct drm_r600_blend),
		&r600_blend_create, &r600_atom_emit_default},
	{R600_ATOM_CONSTANTS, sizeof(struct drm_r600_constants),
		&r600_constants_create, &r600_atom_emit_default},
	{R600_ATOM_DSA, sizeof(struct drm_r600_dsa),
		&r600_dsa_create, &r600_atom_emit_default},
	{R600_ATOM_VS_SHADER, sizeof(struct drm_r600_vs_shader),
		&r600_vs_shader_create, &r600_vs_shader_emit},
	{R600_ATOM_PS_SHADER, sizeof(struct drm_r600_fs_shader),
		&r600_fs_shader_create, &r600_fs_shader_emit},
};

struct r600_atom *r600_atom_create(struct r600_winsys *rdev, struct r600_request *rq)
{
	struct r600_atom *atom;
	int r;

	if (!rq->type || rq->type >= R600_BATCH_NATOMS) {
		fprintf(stderr, "%s %d invalid atom type %d\n", __func__, __LINE__);
		return NULL;
	}
	if (_r600_atom_funcs[rq->type].type != rq->type) {
		fprintf(stderr, "%s %d mismatch for type %d have %d\n",
			__func__, __LINE__, _r600_atom_funcs[rq->type].type, rq->type);
		return NULL;
	}
	r600_winsys_set_bo_list(rdev, rq->nbo, rq->bo);
	/* create the atom */
	atom = malloc(sizeof(struct r600_atom));
	if (atom == NULL)
		return NULL;
	memset(atom, 0, sizeof(struct r600_atom));
	atom->emit = _r600_atom_funcs[rq->type].emit;
	atom->nflushes = 0;
	atom->id = crc_64(rq->data, _r600_atom_funcs[rq->type].size);
	atom->type = rq->type;
	r = _r600_atom_funcs[rq->type].create(rdev, atom, rq->data);
	if (r) {
		r600_atom_put(atom);
		return NULL;
	}
	return atom;
}

void r600_atom_flush_cleanup(struct list_head *flushes)
{
	struct r600_atom_flush *i, *n;

	list_for_each_entry_safe(i, n, flushes, list) {
		list_del(&i->list);
		free(i);
	}
}

int r600_atom_flush_add(struct list_head *flushes, struct radeon_bo *bo, u32 flags)
{
	struct r600_atom_flush *i;

	list_for_each_entry(i, flushes, list) {
		if (i->bo->handle == bo->handle) {
			i->flags |= flags;
			return 0;
		}
	}
	i = malloc(sizeof(*i));
	if (i == NULL)
		return -ENOMEM;
	i->bo = bo;
	i->flags = flags;
	list_add_tail(&i->list, flushes);
	return 1;
}

int r600_atom_emit_default(struct r600_winsys *rdev, struct r600_atom *atom,
				void *data, struct radeon_ib *ib)
{
	return radeon_ib_copy(ib, atom->pkts, atom->npkts);
}

struct radeon_bo *radeon_bo_lookup(struct r600_winsys *rdev, u32 handle)
{
	int i;

	for (i = 0; i < rdev->nbo; i++) {
		if (rdev->bo[i] && rdev->bo[i]->handle == handle) {
			radeon_bo_ref(rdev->bo[i]);
			return rdev->bo[i];
		}
	}
	return NULL;
}

void r600_winsys_set_bo_list(struct r600_winsys *rdev, u32 nbo, struct radeon_bo **bo)
{
	memcpy(rdev->bo, bo, sizeof(void*) * nbo);
	rdev->nbo = nbo;
}

void r600_atom_put(struct r600_atom *atom)
{
	int i;

	for (i = 0; i < atom->nbo; i++) {
		radeon_bo_unref(atom->bo[i]);
	}
	free(atom->state);
	free(atom);
}