Merge branch 'draw-instanced'
[mesa.git] / src / gallium / winsys / radeon / drm / radeon_drm_cs.c
1 /*
2 * Copyright © 2008 Jérôme Glisse
3 * Copyright © 2010 Marek Olšák <maraeo@gmail.com>
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
18 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 */
27 /*
28 * Authors:
29 * Marek Olšák <maraeo@gmail.com>
30 *
31 * Based on work from libdrm_radeon by:
32 * Aapo Tahkola <aet@rasterburn.org>
33 * Nicolai Haehnle <prefect_@gmx.net>
34 * Jérôme Glisse <glisse@freedesktop.org>
35 */
36
37 /*
38 This file replaces libdrm's radeon_cs_gem with our own implemention.
39 It's optimized specifically for r300g, but r600g could use it as well.
40 Reloc writes and space checking are faster and simpler than their
41 counterparts in libdrm (the time complexity of all the functions
42 is O(1) in nearly all scenarios, thanks to hashing).
43
44 It works like this:
45
46 cs_add_reloc(cs, buf, read_domain, write_domain) adds a new relocation and
47 also adds the size of 'buf' to the used_gart and used_vram winsys variables
48 based on the domains, which are simply or'd for the accounting purposes.
49 The adding is skipped if the reloc is already present in the list, but it
50 accounts any newly-referenced domains.
51
52 cs_validate is then called, which just checks:
53 used_vram/gart < vram/gart_size * 0.8
54 The 0.8 number allows for some memory fragmentation. If the validation
55 fails, the pipe driver flushes CS and tries do the validation again,
56 i.e. it validates only that one operation. If it fails again, it drops
57 the operation on the floor and prints some nasty message to stderr.
58 (done in the pipe driver)
59
60 cs_write_reloc(cs, buf) just writes a reloc that has been added using
61 cs_add_reloc. The read_domain and write_domain parameters have been removed,
62 because we already specify them in cs_add_reloc.
63 */
64
65 #include "radeon_drm_cs.h"
66 #include "radeon_drm_buffer.h"
67
68 #include "util/u_memory.h"
69
70 #include <stdlib.h>
71 #include <stdint.h>
72 #include <radeon_bo.h>
73 #include <xf86drm.h>
74
75 #define RELOC_DWORDS (sizeof(struct drm_radeon_cs_reloc) / sizeof(uint32_t))
76
77 static struct r300_winsys_cs *radeon_drm_cs_create(struct r300_winsys_screen *rws)
78 {
79 struct radeon_drm_winsys *ws = radeon_drm_winsys(rws);
80 struct radeon_drm_cs *cs;
81
82 cs = CALLOC_STRUCT(radeon_drm_cs);
83 if (!cs) {
84 return NULL;
85 }
86
87 cs->ws = ws;
88 cs->nrelocs = 256;
89 cs->relocs_bo = (struct radeon_bo**)
90 CALLOC(1, cs->nrelocs * sizeof(struct radeon_bo*));
91 if (!cs->relocs_bo) {
92 FREE(cs);
93 return NULL;
94 }
95
96 cs->relocs = (struct drm_radeon_cs_reloc*)
97 CALLOC(1, cs->nrelocs * sizeof(struct drm_radeon_cs_reloc));
98 if (!cs->relocs) {
99 FREE(cs->relocs_bo);
100 FREE(cs);
101 return NULL;
102 }
103
104 cs->chunks[0].chunk_id = RADEON_CHUNK_ID_IB;
105 cs->chunks[0].length_dw = 0;
106 cs->chunks[0].chunk_data = (uint64_t)(uintptr_t)cs->base.buf;
107 cs->chunks[1].chunk_id = RADEON_CHUNK_ID_RELOCS;
108 cs->chunks[1].length_dw = 0;
109 cs->chunks[1].chunk_data = (uint64_t)(uintptr_t)cs->relocs;
110 return &cs->base;
111 }
112
113 #define OUT_CS(cs, value) (cs)->buf[(cs)->cdw++] = (value)
114
115 static inline void update_domains(struct drm_radeon_cs_reloc *reloc,
116 enum r300_buffer_domain rd,
117 enum r300_buffer_domain wd,
118 enum r300_buffer_domain *added_domains)
119 {
120 *added_domains = (rd | wd) & ~(reloc->read_domains | reloc->write_domain);
121
122 if (reloc->read_domains & wd) {
123 reloc->read_domains = rd;
124 reloc->write_domain = wd;
125 } else if (rd & reloc->write_domain) {
126 reloc->read_domains = rd;
127 reloc->write_domain |= wd;
128 } else {
129 reloc->read_domains |= rd;
130 reloc->write_domain |= wd;
131 }
132 }
133
134 static int radeon_get_reloc(struct radeon_drm_cs *cs,
135 struct radeon_bo *bo)
136 {
137 struct drm_radeon_cs_reloc *reloc;
138 unsigned i;
139 unsigned hash = bo->handle & (sizeof(cs->is_handle_added)-1);
140
141 if (cs->is_handle_added[hash]) {
142 reloc = cs->relocs_hashlist[hash];
143 if (reloc->handle == bo->handle) {
144 return cs->reloc_indices_hashlist[hash];
145 }
146
147 /* Hash collision, look for the BO in the list of relocs linearly. */
148 for (i = cs->crelocs; i != 0;) {
149 --i;
150 reloc = &cs->relocs[i];
151 if (reloc->handle == bo->handle) {
152 /* Put this reloc in the hash list.
153 * This will prevent additional hash collisions if there are
154 * several subsequent get_reloc calls of the same buffer.
155 *
156 * Example: Assuming buffers A,B,C collide in the hash list,
157 * the following sequence of relocs:
158 * AAAAAAAAAAABBBBBBBBBBBBBBCCCCCCCC
159 * will collide here: ^ and here: ^,
160 * meaning that we should get very few collisions in the end. */
161 cs->relocs_hashlist[hash] = reloc;
162 cs->reloc_indices_hashlist[hash] = i;
163 /*printf("write_reloc collision, hash: %i, handle: %i\n", hash, bo->handle);*/
164 return i;
165 }
166 }
167 }
168
169 return -1;
170 }
171
172 static void radeon_add_reloc(struct radeon_drm_cs *cs,
173 struct radeon_bo *bo,
174 enum r300_buffer_domain rd,
175 enum r300_buffer_domain wd,
176 enum r300_buffer_domain *added_domains)
177 {
178 struct drm_radeon_cs_reloc *reloc;
179 unsigned i;
180 unsigned hash = bo->handle & (sizeof(cs->is_handle_added)-1);
181
182 if (cs->is_handle_added[hash]) {
183 reloc = cs->relocs_hashlist[hash];
184 if (reloc->handle == bo->handle) {
185 update_domains(reloc, rd, wd, added_domains);
186 return;
187 }
188
189 /* Hash collision, look for the BO in the list of relocs linearly. */
190 for (i = cs->crelocs; i != 0;) {
191 --i;
192 reloc = &cs->relocs[i];
193 if (reloc->handle == bo->handle) {
194 update_domains(reloc, rd, wd, added_domains);
195
196 cs->relocs_hashlist[hash] = reloc;
197 cs->reloc_indices_hashlist[hash] = i;
198 /*printf("write_reloc collision, hash: %i, handle: %i\n", hash, bo->handle);*/
199 return;
200 }
201 }
202 }
203
204 /* New relocation, check if the backing array is large enough. */
205 if (cs->crelocs >= cs->nrelocs) {
206 uint32_t size;
207 cs->nrelocs += 10;
208
209 size = cs->nrelocs * sizeof(struct radeon_bo*);
210 cs->relocs_bo = (struct radeon_bo**)realloc(cs->relocs_bo, size);
211
212 size = cs->nrelocs * sizeof(struct drm_radeon_cs_reloc);
213 cs->relocs = (struct drm_radeon_cs_reloc*)realloc(cs->relocs, size);
214
215 cs->chunks[1].chunk_data = (uint64_t)(uintptr_t)cs->relocs;
216 }
217
218 /* Initialize the new relocation. */
219 radeon_bo_ref(bo);
220 cs->relocs_bo[cs->crelocs] = bo;
221 reloc = &cs->relocs[cs->crelocs];
222 reloc->handle = bo->handle;
223 reloc->read_domains = rd;
224 reloc->write_domain = wd;
225 reloc->flags = 0;
226
227 cs->is_handle_added[hash] = TRUE;
228 cs->relocs_hashlist[hash] = reloc;
229 cs->reloc_indices_hashlist[hash] = cs->crelocs;
230
231 cs->chunks[1].length_dw += RELOC_DWORDS;
232 cs->crelocs++;
233
234 *added_domains = rd | wd;
235 }
236
237 static void radeon_drm_cs_add_reloc(struct r300_winsys_cs *rcs,
238 struct r300_winsys_cs_buffer *buf,
239 enum r300_buffer_domain rd,
240 enum r300_buffer_domain wd)
241 {
242 struct radeon_drm_cs *cs = radeon_drm_cs(rcs);
243 struct radeon_bo *bo = (struct radeon_bo*)buf;
244 enum r300_buffer_domain added_domains;
245
246 radeon_add_reloc(cs, bo, rd, wd, &added_domains);
247
248 if (!added_domains)
249 return;
250
251 if (added_domains & R300_DOMAIN_GTT)
252 cs->used_gart += bo->size;
253 if (added_domains & R300_DOMAIN_VRAM)
254 cs->used_vram += bo->size;
255 }
256
257 static boolean radeon_drm_cs_validate(struct r300_winsys_cs *rcs)
258 {
259 struct radeon_drm_cs *cs = radeon_drm_cs(rcs);
260
261 return cs->used_gart < cs->ws->gart_size * 0.8 &&
262 cs->used_vram < cs->ws->vram_size * 0.8;
263 }
264
265 static void radeon_drm_cs_write_reloc(struct r300_winsys_cs *rcs,
266 struct r300_winsys_cs_buffer *buf)
267 {
268 struct radeon_drm_cs *cs = radeon_drm_cs(rcs);
269 struct radeon_bo *bo = (struct radeon_bo*)buf;
270
271 unsigned index = radeon_get_reloc(cs, bo);
272
273 if (index == -1) {
274 fprintf(stderr, "r300: Cannot get a relocation in %s.\n", __func__);
275 return;
276 }
277
278 OUT_CS(&cs->base, 0xc0001000);
279 OUT_CS(&cs->base, index * RELOC_DWORDS);
280 }
281
282 static void radeon_drm_cs_emit(struct r300_winsys_cs *rcs)
283 {
284 struct radeon_drm_cs *cs = radeon_drm_cs(rcs);
285 uint64_t chunk_array[2];
286 unsigned i;
287 int r;
288
289 if (cs->base.cdw) {
290 /* Unmap buffers. */
291 radeon_drm_bufmgr_flush_maps(cs->ws->kman);
292
293 /* Prepare the arguments. */
294 cs->chunks[0].length_dw = cs->base.cdw;
295
296 chunk_array[0] = (uint64_t)(uintptr_t)&cs->chunks[0];
297 chunk_array[1] = (uint64_t)(uintptr_t)&cs->chunks[1];
298
299 cs->cs.num_chunks = 2;
300 cs->cs.chunks = (uint64_t)(uintptr_t)chunk_array;
301
302 /* Emit. */
303 r = drmCommandWriteRead(cs->ws->fd, DRM_RADEON_CS,
304 &cs->cs, sizeof(struct drm_radeon_cs));
305 if (r) {
306 if (debug_get_bool_option("RADEON_DUMP_CS", FALSE)) {
307 fprintf(stderr, "radeon: The kernel rejected CS, dumping...\n");
308 fprintf(stderr, "VENDORID:DEVICEID 0x%04X:0x%04X\n", 0x1002,
309 cs->ws->pci_id);
310 for (i = 0; i < cs->base.cdw; i++) {
311 fprintf(stderr, "0x%08X\n", cs->base.buf[i]);
312 }
313 } else {
314 fprintf(stderr, "radeon: The kernel rejected CS, "
315 "see dmesg for more information.\n");
316 }
317 }
318 }
319
320 /* Unreference buffers, cleanup. */
321 for (i = 0; i < cs->crelocs; i++) {
322 radeon_bo_unref((struct radeon_bo*)cs->relocs_bo[i]);
323 cs->relocs_bo[i] = NULL;
324 }
325
326 cs->base.cdw = 0;
327 cs->crelocs = 0;
328 cs->chunks[0].length_dw = 0;
329 cs->chunks[1].length_dw = 0;
330 cs->used_gart = 0;
331 cs->used_vram = 0;
332 memset(cs->is_handle_added, 0, sizeof(cs->is_handle_added));
333 }
334
335 static void radeon_drm_cs_destroy(struct r300_winsys_cs *rcs)
336 {
337 struct radeon_drm_cs *cs = radeon_drm_cs(rcs);
338 FREE(cs->relocs_bo);
339 FREE(cs->relocs);
340 FREE(cs);
341 }
342
343 static void radeon_drm_cs_set_flush(struct r300_winsys_cs *rcs,
344 void (*flush)(void *), void *user)
345 {
346 struct radeon_drm_cs *cs = radeon_drm_cs(rcs);
347 cs->flush_cs = flush;
348 cs->flush_data = user;
349 }
350
351 void radeon_drm_cs_init_functions(struct radeon_drm_winsys *ws)
352 {
353 ws->base.cs_create = radeon_drm_cs_create;
354 ws->base.cs_destroy = radeon_drm_cs_destroy;
355 ws->base.cs_add_reloc = radeon_drm_cs_add_reloc;
356 ws->base.cs_validate = radeon_drm_cs_validate;
357 ws->base.cs_write_reloc = radeon_drm_cs_write_reloc;
358 ws->base.cs_flush = radeon_drm_cs_emit;
359 ws->base.cs_set_flush = radeon_drm_cs_set_flush;
360 }