summaryrefslogtreecommitdiff
path: root/src/radeon.c
blob: 66dbfc39d841040d52b8557251a23c8d45c24e71 (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
/*
 * Copyright © 2009 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 "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "xf86drm.h"
#include "radeon_bo_gem.h"
#include "radeon_cs_gem.h"
#include "radeon_priv.h"
#include "r600d.h"

int radeon_get_device(struct radeon *radeon)
{
	struct drm_radeon_info info;
	int r;

	radeon->device = 0;
	info.request = RADEON_INFO_DEVICE_ID;
	info.value = (uintptr_t)&radeon->device;
	r = drmCommandWriteRead(radeon->fd, DRM_RADEON_INFO, &info,
			sizeof(struct drm_radeon_info));
	return r;
}

int radeon_open_fd(void)
{
	return drmOpen("radeon", NULL);
}

struct radeon *radeon_new(int fd, unsigned device)
{
	struct radeon *radeon;
	int r;

	radeon = calloc(1, sizeof(*radeon));
	if (radeon == NULL)
		return NULL;
	radeon->fd = fd;
	radeon->device = device;
	radeon->refcount = 1;
	if (fd >= 0) {
		r = radeon_get_device(radeon);
		if (r) {
			fprintf(stderr, "Failed to get device id\n");
			return radeon_decref(radeon);
		}
	}
	radeon->family = radeon_family_from_device(radeon->device);
	if (radeon->family == CHIP_UNKNOWN) {
		fprintf(stderr, "Unknown chipset 0x%04X\n", radeon->device);
		return radeon_decref(radeon);
	}
	radeon->csm = radeon_cs_manager_gem_ctor(radeon->fd);
	if (radeon->csm == NULL) {
		fprintf(stderr, "Failed to create CS manager\n");
		return radeon_decref(radeon);
	}
	radeon->bom = radeon_bo_manager_gem_ctor(radeon->fd);
	if (radeon->bom == NULL) {
		fprintf(stderr, "Failed to create BO manager\n");
		return radeon_decref(radeon);
	}
	switch (radeon->family) {
	case CHIP_R600:
	case CHIP_RV610:
	case CHIP_RV630:
	case CHIP_RV670:
	case CHIP_RV620:
	case CHIP_RV635:
	case CHIP_RS780:
	case CHIP_RS880:
	case CHIP_RV770:
	case CHIP_RV730:
	case CHIP_RV710:
	case CHIP_RV740:
		if (r600_init(radeon)) {
			return radeon_decref(radeon);
		}
		break;
	case CHIP_R100:
	case CHIP_RV100:
	case CHIP_RS100:
	case CHIP_RV200:
	case CHIP_RS200:
	case CHIP_R200:
	case CHIP_RV250:
	case CHIP_RS300:
	case CHIP_RV280:
	case CHIP_R300:
	case CHIP_R350:
	case CHIP_RV350:
	case CHIP_RV380:
	case CHIP_R420:
	case CHIP_R423:
	case CHIP_RV410:
	case CHIP_RS400:
	case CHIP_RS480:
	case CHIP_RS600:
	case CHIP_RS690:
	case CHIP_RS740:
	case CHIP_RV515:
	case CHIP_R520:
	case CHIP_RV530:
	case CHIP_RV560:
	case CHIP_RV570:
	case CHIP_R580:
	case CHIP_CEDAR:
	case CHIP_REDWOOD:
	case CHIP_JUNIPER:
	case CHIP_CYPRESS:
	case CHIP_HEMLOCK:
	default:
		fprintf(stderr, "%s unknown or unsupported chipset 0x%04X\n",
			__func__, radeon->device);
		break;
	}
	if (radeon->fd >= 0) {
		/* FIXME compile time enable */
		r = radeon_mode_configure(radeon);
		if (r) {
			return radeon_decref(radeon);
		}
	}
	return radeon;
}

struct radeon *radeon_incref(struct radeon *radeon)
{
	if (radeon == NULL)
		return NULL;
	radeon->refcount++;
	return radeon;
}

struct radeon *radeon_decref(struct radeon *radeon)
{
	if (radeon == NULL)
		return NULL;
	if (--radeon->refcount > 0) {
		return NULL;
	}
	radeon_mode_cleanup(radeon);
	radeon_bo_manager_gem_dtor(radeon->bom);
	radeon_cs_manager_gem_dtor(radeon->csm);
	drmClose(radeon->fd);
	free(radeon);
	return NULL;
}

void memset_bo(struct radeon_bo *bo, u32 value)
{
	u32 *ptr;
	int r;

	r = radeon_bo_map(bo, 1);
	if (r) {
		fprintf(stderr, "Failed to map buffer\n");
		perror(NULL);
		return;
	}
	ptr = (u32*)bo->ptr;
	for (r = 0; r < (bo->size / 4); r++)
		ptr[r] = value;
	radeon_bo_unmap(bo);
}

void memcpy_bo(struct radeon_bo *bo, u32 *src, u32 size)
{
	u32 *ptr, tmp;
	int r;

	r = radeon_bo_map(bo, 1);
	if (r) {
		fprintf(stderr, "Failed to map buffer\n");
		perror(NULL);
		return;
	}
	ptr = (u32*)bo->ptr;
	for (r = 0; r < size; r++)
		ptr[r] = src[r];
	for (r = 0; r < size; r++)
		ptr[r] = src[r];
	for (r = 0, tmp = 0; r < size; r++)
		tmp += ptr[r];
	radeon_bo_unmap(bo);
}

void radeon_blit(struct radeon_surface *dst, struct radeon_surface *src,
		unsigned sx, unsigned sy, unsigned dx, unsigned dy,
		unsigned w, unsigned h)
{
	unsigned y;
	u8 *dptr, *sptr;

	if (sx >= src->width || sy >= src->height)
		return;
	if (dx >= dst->width || dy >= dst->height)
		return;
	if ((sx + w) > src->width)
		w = src->width - sx;
	if ((dx + w) > dst->width)
		w = dst->width - dx;
	if ((sy + h) > src->height)
		h = src->height - sy;
	if ((dy + h) > dst->height)
		h = dst->height - dy;
	if (!w || !h)
		return;
	if (radeon_bo_map(dst->bo, 1))
		return;
	if (radeon_bo_map(src->bo, 0))
		return;
	sptr = src->bo->ptr;
	dptr = dst->bo->ptr;
	sptr = &sptr[(sy * src->pitch + sx) * src->bpe];
	dptr = &dptr[(dy * dst->pitch + dx) * dst->bpe];
	for (y = 0; y < h; y++) {
		memcpy(dptr, sptr, w * src->bpe);
		dptr += dst->pitch * dst->bpe;
		sptr += src->pitch * src->bpe;
	}
	radeon_bo_unmap(dst->bo);
	radeon_bo_unmap(src->bo);
}

int radeon_flush(struct radeon *radeon)
{
	struct radeon_ctx *ctx = radeon->ctx;
	unsigned i;
	int r;

	free(ctx->pm4);
	ctx->pm4 = malloc(ctx->cpm4 * 4);
	if (ctx->pm4 == NULL)
		return -EINVAL;
	for (i = 0, ctx->id = 0; i < ctx->nstate; i++) {
		r = radeon_ctx_state_schedule(ctx, ctx->state[i]);
		if (r)
			return r;
	}
	if (ctx->id != ctx->cpm4) {
		fprintf(stderr, "%s miss predicted pm4 size %d for %d\n",
			__func__, ctx->cpm4, ctx->id);
		return -EINVAL;
	}
	r = radeon_ctx_submit(ctx);
	radeon_ctx_destroy(ctx);
	radeon->ctx = radeon_ctx(radeon, 0);
	return r;
}

int radeon_reg_id(struct radeon *radeon, unsigned offset, unsigned *typeid, unsigned *stateid, unsigned *id)
{
	unsigned i, j;

	for (i = 0; i < radeon->ntype; i++) {
		if (radeon->type[i].range_start) {
			if (offset >= radeon->type[i].range_start && offset < radeon->type[i].range_end) {
				*typeid = i;
				j = offset - radeon->type[i].range_start;
				j /= radeon->type[i].stride;
				*stateid = radeon->type[i].id + j;
				*id = (offset - radeon->type[i].range_start - radeon->type[i].stride * j) / 4;
				return 0;
			}
		} else {
			for (j = 0; j < radeon->type[i].nstates; j++) {
				if (radeon->type[i].regs[j].offset == offset) {
					*typeid = i;
					*stateid = radeon->type[i].id;
					*id = j;
					return 0;
				}
			}
		}
	}
	fprintf(stderr, "%s unknown register 0x%08X\n", __func__, offset);
	return -EINVAL;
}

unsigned radeon_type_from_id(struct radeon *radeon, unsigned id)
{
	unsigned i;

	for (i = 0; i < radeon->ntype - 1; i++) {
		if (radeon->type[i].id == id)
			return i;
		if (id > radeon->type[i].id && id < radeon->type[i + 1].id)
			return i;
	}
	if (radeon->type[i].id == id)
		return i;
	return -1;
}

int radeon_schedule_draw(struct radeon *radeon, struct radeon_draw *draw)
{
	struct radeon_ctx *ctx = radeon->ctx;
	struct radeon_draw *pdraw = NULL;
	struct radeon_state *nstate, *ostate;
	unsigned cpm4, i, cstate;
	void *tmp;
	int r = 0;

	if (ctx == NULL) {
		ctx = radeon_ctx(radeon, 0);
		if (ctx == NULL)
			return -ENOMEM;
		radeon->ctx = ctx;
	}
	r = radeon_draw_check(draw);
	if (r)
		return r;
	if (draw->cpm4 >= RADEON_CTX_MAX_PM4) {
		fprintf(stderr, "%s single draw too big %d, max %d\n",
			__func__, draw->cpm4, RADEON_CTX_MAX_PM4);
		return -EINVAL;
	}
reprocess:
	tmp = realloc(ctx->state, (ctx->nstate + draw->nstate) * sizeof(void*));
	if (tmp == NULL)
		return -ENOMEM;
	ctx->state = tmp;
	pdraw = ctx->cdraw;
	for (i = 0, cpm4 = 0, cstate = ctx->nstate; i < draw->nstate - 1; i++) {
		nstate = draw->state[i];
		if (nstate) {
			if (pdraw && pdraw->state[i]) {
				ostate = pdraw->state[i];
				if (ostate->pm4_crc == nstate->pm4_crc) {
//					ctx->state[cstate++] = nstate;
//					cpm4 += nstate->cpm4;
				} else {
					ctx->state[cstate++] = nstate;
					cpm4 += nstate->cpm4;
				}
			} else {
				ctx->state[cstate++] = nstate;
				cpm4 += nstate->cpm4;
			}
		}
	}
	/* The last state is the draw state always add it */
	if (draw->state[i] == NULL) {
		fprintf(stderr, "%s no draw command\n", __func__);
		return -EINVAL;
	}
	ctx->state[cstate++] = draw->state[i];
	cpm4 += draw->state[i]->cpm4;
	if ((ctx->cpm4 + cpm4) > RADEON_CTX_MAX_PM4) {
		r = radeon_flush(radeon);
		if (r)
			goto out_err;
		goto reprocess;
	}
	r = radeon_ctx_set_draw_new(ctx, draw);
	if (r)
		goto out_err;
	radeon_draw_incref(draw);
	ctx->cpm4 += cpm4;
	ctx->nstate = cstate;
out_err:
	return r;
}