summaryrefslogtreecommitdiff
path: root/r600_winsys.c
blob: b9bbe87ffbcb5fdee655547ac52f1165136d00cb (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
/*
 * 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 "r600.h"
#include "r600_winsys.h"

#pragma pack(1)
struct ib_reloc_gem {
    uint32_t    handle;
    uint32_t    read_domain;
    uint32_t    write_domain;
    uint32_t    flags;
};
#pragma pack()
#define RELOC_SIZE (sizeof(struct ib_reloc_gem) / sizeof(uint32_t))

u32 radeon_ib_reloc(struct radeon_ib *ib, struct radeon_bo *bo, u32 d)
{
	struct ib_reloc_gem *reloc;
	int i;

	for (i = 0; i < ib->crelocs; i++) {
		reloc = (struct ib_reloc_gem*)&ib->relocs[i * RELOC_SIZE];
		if (reloc->handle == bo->handle) {
			reloc->read_domain |= d;
			return (i * RELOC_SIZE);
		}
	}
	if (ib->crelocs >= ib->nrelocs)
		return 0xFFFFFFFFUL;
	i = ib->crelocs++;
	reloc = (struct ib_reloc_gem*)&ib->relocs[i * RELOC_SIZE];
	reloc->handle = bo->handle;
	reloc->read_domain = d;
	reloc->write_domain = 0;
	reloc->flags = 0;
	return (i * RELOC_SIZE);
}

int radeon_ib_get(struct r600_winsys *rdev, struct radeon_ib **ib)
{
	struct radeon_ib *lib;

	lib = malloc(sizeof(struct radeon_ib));
	if (lib == NULL)
		return -ENOMEM;
	memset(lib, sizeof(*lib), 0);
	lib->ptr = malloc(64 * 1024);
	if (lib->ptr == NULL) {
		free(lib);
		return -ENOMEM;
	}
	lib->cpkts = 0;
	lib->length_dw = 64 * 1024 / 4;
	lib->relocs = malloc(64 * 1024);
	if (lib->relocs == NULL) {
		free(lib->ptr);
		free(lib);
		return -ENOMEM;
	}
	lib->nrelocs = 64 * 1024 / RELOC_SIZE;
	lib->crelocs = 0;
	*ib = lib;
	return 0;
}

void radeon_ib_free(struct radeon_ib *ib)
{
	if (ib == NULL)
		return;
	free(ib->ptr);
	free(ib->relocs);
	free(ib);
}

int radeon_ib_schedule(struct r600_winsys *rdev, struct radeon_ib *ib)
{
	struct drm_radeon_cs drmib;
	struct drm_radeon_cs_chunk chunks[2];
	uint64_t chunk_array[2];
	int r = 0;

#if 0
	for (r = 0; r < ib->cpkts; r++) {
		printf("0x%08X\n", ib->ptr[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 = ib->cpkts;
	chunks[0].chunk_data = (uint64_t)(uintptr_t)ib->ptr;
	chunks[1].chunk_id = RADEON_CHUNK_ID_RELOCS;
	chunks[1].length_dw = ib->crelocs * 4;
	chunks[1].chunk_data = (uint64_t)(uintptr_t)ib->relocs;
	chunk_array[0] = (uint64_t)(uintptr_t)&chunks[0];
	chunk_array[1] = (uint64_t)(uintptr_t)&chunks[1];
#if 1
	r = drmCommandWriteRead(rdev->fd, DRM_RADEON_CS, &drmib,
				sizeof(struct drm_radeon_cs));
#endif
	return r;
}

int r600_winsys_init(struct r600_winsys **rdev, struct radeon_bo_manager *bom, int fd)
{
	struct r600_winsys *dev;
	int r;

	*rdev = NULL;
	dev = malloc(sizeof(struct r600_winsys));
	if (dev == NULL)
		return -ENOMEM;
	memset(dev, 0, sizeof(struct r600_winsys));
	dev->fd = fd;
	dev->bom = bom;
	r = r600_atoms_init(dev);
	if (r)
		return r;
	*rdev = dev;
	return r;
}

void r600_winsys_release(struct r600_winsys *rdev)
{
	memset(rdev, 0, sizeof(struct r600_winsys));
	free(rdev);
}

u64 crc_64(void *d, size_t len)
{
	u8 *data = (uint8_t*)d;
	u64 div = 0x42F0E1EBA9EA3693;
	u64 crc = 0xFFFFFFFFFFFFFFFF;
	u8 t;
	int i, j;

	for (j = 0; j < len; j++) {
		t = data[j];
		for (i = 0; i < 8; i++) {
			if ((t >> 7) ^ (crc >> 63)) {
				crc = (crc << 1) ^ div;
			} else {
				crc = (crc << 1);
			}
			t = (t << 1) & 0xFF;
		}
	}
	return crc;
}

void r600_memcpy_bo(struct radeon_bo *bo, u32 *src, u32 size)
{
	int r;

	r = radeon_bo_map(bo, 1);
	if (r) {
		return;
	}
	memcpy(bo->ptr, src, size);
	radeon_bo_unmap(bo);
}

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;
}

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;
}