3953525c3e98ed71f88d7dfa791640f3431d0534
[mesa.git] / src / mesa / drivers / dri / intel / intel_bufmgr_ttm.c
1 /**************************************************************************
2 *
3 * Copyright © 2007 Red Hat Inc.
4 * Copyright © 2007 Intel Corporation
5 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * The above copyright notice and this permission notice (including the
25 * next paragraph) shall be included in all copies or substantial portions
26 * of the Software.
27 *
28 *
29 **************************************************************************/
30 /*
31 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
32 * Keith Whitwell <keithw-at-tungstengraphics-dot-com>
33 * Eric Anholt <eric@anholt.net>
34 * Dave Airlie <airlied@linux.ie>
35 */
36
37 #include <xf86drm.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <assert.h>
43
44 #include "errno.h"
45 #include "mtypes.h"
46 #include "dri_bufmgr.h"
47 #include "string.h"
48 #include "imports.h"
49
50 #include "i915_drm.h"
51
52 #include "intel_bufmgr_ttm.h"
53
54 #define DBG(...) do { \
55 if (bufmgr_ttm->bufmgr.debug) \
56 fprintf(stderr, __VA_ARGS__); \
57 } while (0)
58
59 /*
60 * These bits are always specified in each validation
61 * request. Other bits are not supported at this point
62 * as it would require a bit of investigation to figure
63 * out what mask value should be used.
64 */
65 #define INTEL_BO_MASK (DRM_BO_MASK_MEM | \
66 DRM_BO_FLAG_READ | \
67 DRM_BO_FLAG_WRITE | \
68 DRM_BO_FLAG_EXE)
69
70 struct intel_validate_entry {
71 dri_bo *bo;
72 struct drm_i915_op_arg bo_arg;
73 };
74
75 typedef struct _dri_bufmgr_ttm {
76 dri_bufmgr bufmgr;
77
78 int fd;
79 unsigned int fence_type;
80 unsigned int fence_type_flush;
81
82 uint32_t max_relocs;
83
84 struct intel_validate_entry *validate_array;
85 int validate_array_size;
86 int validate_count;
87
88 uint32_t *cached_reloc_buf_data;
89 } dri_bufmgr_ttm;
90
91 /**
92 * Private information associated with a relocation that isn't already stored
93 * in the relocation buffer to be passed to the kernel.
94 */
95 struct dri_ttm_reloc {
96 dri_bo *target_buf;
97 uint64_t validate_flags;
98 /** Offset of target_buf after last execution of this relocation entry. */
99 unsigned int last_target_offset;
100 };
101
102 typedef struct _dri_bo_ttm {
103 dri_bo bo;
104
105 int refcount;
106 drmBO drm_bo;
107 const char *name;
108
109 uint64_t last_flags;
110
111 /**
112 * Index of the buffer within the validation list while preparing a
113 * batchbuffer execution.
114 */
115 int validate_index;
116
117 /** DRM buffer object containing relocation list */
118 uint32_t *reloc_buf_data;
119 struct dri_ttm_reloc *relocs;
120
121 /**
122 * Indicates that the buffer may be shared with other processes, so we
123 * can't hold maps beyond when the user does.
124 */
125 GLboolean shared;
126
127 GLboolean delayed_unmap;
128 /* Virtual address from the dri_bo_map whose unmap was delayed. */
129 void *saved_virtual;
130 } dri_bo_ttm;
131
132 typedef struct _dri_fence_ttm
133 {
134 dri_fence fence;
135
136 int refcount;
137 const char *name;
138 drmFence drm_fence;
139 } dri_fence_ttm;
140
141 static void dri_ttm_dump_validation_list(dri_bufmgr_ttm *bufmgr_ttm)
142 {
143 int i, j;
144
145 for (i = 0; i < bufmgr_ttm->validate_count; i++) {
146 dri_bo *bo = bufmgr_ttm->validate_array[i].bo;
147 dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
148
149 if (bo_ttm->reloc_buf_data != NULL) {
150 for (j = 0; j < (bo_ttm->reloc_buf_data[0] & 0xffff); j++) {
151 uint32_t *reloc_entry = bo_ttm->reloc_buf_data +
152 I915_RELOC_HEADER +
153 j * I915_RELOC0_STRIDE;
154 dri_bo *target_bo = bo_ttm->relocs[j].target_buf;
155 dri_bo_ttm *target_ttm = (dri_bo_ttm *)target_bo;
156
157 DBG("%2d: %s@0x%08x -> %s@0x%08lx + 0x%08x\n",
158 i,
159 bo_ttm->name, reloc_entry[0],
160 target_ttm->name, target_bo->offset,
161 reloc_entry[1]);
162 }
163 } else {
164 DBG("%2d: %s\n", i, bo_ttm->name);
165 }
166 }
167 }
168
169 /**
170 * Adds the given buffer to the list of buffers to be validated (moved into the
171 * appropriate memory type) with the next batch submission.
172 *
173 * If a buffer is validated multiple times in a batch submission, it ends up
174 * with the intersection of the memory type flags and the union of the
175 * access flags.
176 */
177 static void
178 intel_add_validate_buffer(dri_bo *buf,
179 uint64_t flags)
180 {
181 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;
182 dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
183
184 /* If we delayed doing an unmap to mitigate map/unmap syscall thrashing,
185 * do that now.
186 */
187 if (ttm_buf->delayed_unmap) {
188 drmBOUnmap(bufmgr_ttm->fd, &ttm_buf->drm_bo);
189 ttm_buf->delayed_unmap = GL_FALSE;
190 }
191
192 if (ttm_buf->validate_index == -1) {
193 struct intel_validate_entry *entry;
194 struct drm_i915_op_arg *arg;
195 struct drm_bo_op_req *req;
196 int index;
197
198 /* Extend the array of validation entries as necessary. */
199 if (bufmgr_ttm->validate_count == bufmgr_ttm->validate_array_size) {
200 int i, new_size = bufmgr_ttm->validate_array_size * 2;
201
202 if (new_size == 0)
203 new_size = 5;
204
205 bufmgr_ttm->validate_array =
206 realloc(bufmgr_ttm->validate_array,
207 sizeof(struct intel_validate_entry) * new_size);
208 bufmgr_ttm->validate_array_size = new_size;
209
210 /* Update pointers for realloced mem. */
211 for (i = 0; i < bufmgr_ttm->validate_count - 1; i++) {
212 bufmgr_ttm->validate_array[i].bo_arg.next = (unsigned long)
213 &bufmgr_ttm->validate_array[i + 1].bo_arg;
214 }
215 }
216
217 /* Pick out the new array entry for ourselves */
218 index = bufmgr_ttm->validate_count;
219 ttm_buf->validate_index = index;
220 entry = &bufmgr_ttm->validate_array[index];
221 bufmgr_ttm->validate_count++;
222
223 /* Fill in array entry */
224 entry->bo = buf;
225 dri_bo_reference(buf);
226
227 /* Fill in kernel arg */
228 arg = &entry->bo_arg;
229 req = &arg->d.req;
230
231 memset(arg, 0, sizeof(*arg));
232 req->bo_req.handle = ttm_buf->drm_bo.handle;
233 req->op = drm_bo_validate;
234 req->bo_req.flags = flags;
235 req->bo_req.hint = 0;
236 #ifdef DRM_BO_HINT_PRESUMED_OFFSET
237 /* PRESUMED_OFFSET indicates that all relocations pointing at this
238 * buffer have the correct offset. If any of our relocations don't,
239 * this flag will be cleared off the buffer later in the relocation
240 * processing.
241 */
242 req->bo_req.hint |= DRM_BO_HINT_PRESUMED_OFFSET;
243 req->bo_req.presumed_offset = buf->offset;
244 #endif
245 req->bo_req.mask = INTEL_BO_MASK;
246 req->bo_req.fence_class = 0; /* Backwards compat. */
247
248 if (ttm_buf->reloc_buf_data != NULL)
249 arg->reloc_ptr = (unsigned long)(void *)ttm_buf->reloc_buf_data;
250 else
251 arg->reloc_ptr = 0;
252
253 /* Hook up the linked list of args for the kernel */
254 arg->next = 0;
255 if (index != 0) {
256 bufmgr_ttm->validate_array[index - 1].bo_arg.next =
257 (unsigned long)arg;
258 }
259 } else {
260 struct intel_validate_entry *entry =
261 &bufmgr_ttm->validate_array[ttm_buf->validate_index];
262 struct drm_i915_op_arg *arg = &entry->bo_arg;
263 struct drm_bo_op_req *req = &arg->d.req;
264 uint64_t memFlags = req->bo_req.flags & flags & DRM_BO_MASK_MEM;
265 uint64_t modeFlags = (req->bo_req.flags | flags) & ~DRM_BO_MASK_MEM;
266
267 /* Buffer was already in the validate list. Extend its flags as
268 * necessary.
269 */
270
271 if (memFlags == 0) {
272 fprintf(stderr,
273 "%s: No shared memory types between "
274 "0x%16llx and 0x%16llx\n",
275 __FUNCTION__, req->bo_req.flags, flags);
276 abort();
277 }
278 if (flags & ~INTEL_BO_MASK) {
279 fprintf(stderr,
280 "%s: Flags bits 0x%16llx are not supposed to be used in a relocation\n",
281 __FUNCTION__, flags & ~INTEL_BO_MASK);
282 abort();
283 }
284 req->bo_req.flags = memFlags | modeFlags;
285 }
286 }
287
288
289 #define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \
290 sizeof(uint32_t))
291
292 static int
293 intel_setup_reloc_list(dri_bo *bo)
294 {
295 dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
296 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bo->bufmgr;
297
298 bo_ttm->relocs = malloc(sizeof(struct dri_ttm_reloc) *
299 bufmgr_ttm->max_relocs);
300
301 if (bufmgr_ttm->cached_reloc_buf_data != NULL) {
302 bo_ttm->reloc_buf_data = bufmgr_ttm->cached_reloc_buf_data;
303
304 bufmgr_ttm->cached_reloc_buf_data = NULL;
305 } else {
306 bo_ttm->reloc_buf_data = calloc(1, RELOC_BUF_SIZE(bufmgr_ttm->max_relocs));
307 }
308
309 /* Initialize the relocation list with the header:
310 * DWORD 0: relocation count
311 * DWORD 1: relocation type
312 * DWORD 2+3: handle to next relocation list (currently none) 64-bits
313 */
314 bo_ttm->reloc_buf_data[0] = 0;
315 bo_ttm->reloc_buf_data[1] = I915_RELOC_TYPE_0;
316 bo_ttm->reloc_buf_data[2] = 0;
317 bo_ttm->reloc_buf_data[3] = 0;
318
319 return 0;
320 }
321
322 #if 0
323 int
324 driFenceSignaled(DriFenceObject * fence, unsigned type)
325 {
326 int signaled;
327 int ret;
328
329 if (fence == NULL)
330 return GL_TRUE;
331
332 ret = drmFenceSignaled(bufmgr_ttm->fd, &fence->fence, type, &signaled);
333 BM_CKFATAL(ret);
334 return signaled;
335 }
336 #endif
337
338 static dri_bo *
339 dri_ttm_alloc(dri_bufmgr *bufmgr, const char *name,
340 unsigned long size, unsigned int alignment,
341 uint64_t location_mask)
342 {
343 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bufmgr;
344 dri_bo_ttm *ttm_buf;
345 unsigned int pageSize = getpagesize();
346 int ret;
347 uint64_t flags;
348 unsigned int hint;
349
350 ttm_buf = malloc(sizeof(*ttm_buf));
351 if (!ttm_buf)
352 return NULL;
353
354 /* The mask argument doesn't do anything for us that we want other than
355 * determine which pool (TTM or local) the buffer is allocated into, so
356 * just pass all of the allocation class flags.
357 */
358 flags = location_mask | DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE |
359 DRM_BO_FLAG_EXE;
360 /* No hints we want to use. */
361 hint = 0;
362
363 ret = drmBOCreate(bufmgr_ttm->fd, size, alignment / pageSize,
364 NULL, flags, hint, &ttm_buf->drm_bo);
365 if (ret != 0) {
366 free(ttm_buf);
367 return NULL;
368 }
369 ttm_buf->bo.size = ttm_buf->drm_bo.size;
370 ttm_buf->bo.offset = ttm_buf->drm_bo.offset;
371 ttm_buf->bo.virtual = NULL;
372 ttm_buf->bo.bufmgr = bufmgr;
373 ttm_buf->name = name;
374 ttm_buf->refcount = 1;
375 ttm_buf->reloc_buf_data = NULL;
376 ttm_buf->relocs = NULL;
377 ttm_buf->last_flags = ttm_buf->drm_bo.flags;
378 ttm_buf->shared = GL_FALSE;
379 ttm_buf->delayed_unmap = GL_FALSE;
380 ttm_buf->validate_index = -1;
381
382 DBG("bo_create: %p (%s) %ldb\n", &ttm_buf->bo, ttm_buf->name, size);
383
384 return &ttm_buf->bo;
385 }
386
387 /* Our TTM backend doesn't allow creation of static buffers, as that requires
388 * privelege for the non-fake case, and the lock in the fake case where we were
389 * working around the X Server not creating buffers and passing handles to us.
390 */
391 static dri_bo *
392 dri_ttm_alloc_static(dri_bufmgr *bufmgr, const char *name,
393 unsigned long offset, unsigned long size, void *virtual,
394 uint64_t location_mask)
395 {
396 return NULL;
397 }
398
399 /**
400 * Returns a dri_bo wrapping the given buffer object handle.
401 *
402 * This can be used when one application needs to pass a buffer object
403 * to another.
404 */
405 dri_bo *
406 intel_ttm_bo_create_from_handle(dri_bufmgr *bufmgr, const char *name,
407 unsigned int handle)
408 {
409 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bufmgr;
410 dri_bo_ttm *ttm_buf;
411 int ret;
412
413 ttm_buf = malloc(sizeof(*ttm_buf));
414 if (!ttm_buf)
415 return NULL;
416
417 ret = drmBOReference(bufmgr_ttm->fd, handle, &ttm_buf->drm_bo);
418 if (ret != 0) {
419 fprintf(stderr, "Couldn't reference %s handle 0x%08x: %s\n",
420 name, handle, strerror(-ret));
421 free(ttm_buf);
422 return NULL;
423 }
424 ttm_buf->bo.size = ttm_buf->drm_bo.size;
425 ttm_buf->bo.offset = ttm_buf->drm_bo.offset;
426 ttm_buf->bo.virtual = NULL;
427 ttm_buf->bo.bufmgr = bufmgr;
428 ttm_buf->name = name;
429 ttm_buf->refcount = 1;
430 ttm_buf->reloc_buf_data = NULL;
431 ttm_buf->relocs = NULL;
432 ttm_buf->last_flags = ttm_buf->drm_bo.flags;
433 ttm_buf->shared = GL_TRUE;
434 ttm_buf->delayed_unmap = GL_FALSE;
435 ttm_buf->validate_index = -1;
436
437 DBG("bo_create_from_handle: %p %08x (%s)\n",
438 &ttm_buf->bo, handle, ttm_buf->name);
439
440 return &ttm_buf->bo;
441 }
442
443 static void
444 dri_ttm_bo_reference(dri_bo *buf)
445 {
446 dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
447
448 ttm_buf->refcount++;
449 }
450
451 static void
452 dri_ttm_bo_unreference(dri_bo *buf)
453 {
454 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;
455 dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
456
457 if (!buf)
458 return;
459
460 if (--ttm_buf->refcount == 0) {
461 int ret;
462
463 if (ttm_buf->reloc_buf_data) {
464 int i;
465
466 /* Unreference all the target buffers */
467 for (i = 0; i < (ttm_buf->reloc_buf_data[0] & 0xffff); i++)
468 dri_bo_unreference(ttm_buf->relocs[i].target_buf);
469 free(ttm_buf->relocs);
470
471 if (bufmgr_ttm->cached_reloc_buf_data == NULL) {
472 /* Cache a single relocation buffer allocation to avoid
473 * repeated create/map/unmap/destroy for batchbuffer
474 * relocations.
475 */
476 bufmgr_ttm->cached_reloc_buf_data = ttm_buf->reloc_buf_data;
477 } else {
478 /* Free the kernel BO containing relocation entries */
479 free(ttm_buf->reloc_buf_data);
480 ttm_buf->reloc_buf_data = NULL;
481 }
482 }
483
484 if (ttm_buf->delayed_unmap) {
485 int ret = drmBOUnmap(bufmgr_ttm->fd, &ttm_buf->drm_bo);
486
487 if (ret != 0) {
488 fprintf(stderr, "%s:%d: Error unmapping buffer %s: %s.\n",
489 __FILE__, __LINE__, ttm_buf->name, strerror(-ret));
490 }
491 }
492
493 ret = drmBOUnreference(bufmgr_ttm->fd, &ttm_buf->drm_bo);
494 if (ret != 0) {
495 fprintf(stderr, "drmBOUnreference failed (%s): %s\n",
496 ttm_buf->name, strerror(-ret));
497 }
498 DBG("bo_unreference final: %p (%s)\n", &ttm_buf->bo, ttm_buf->name);
499
500 free(buf);
501 return;
502 }
503 }
504
505 static int
506 dri_ttm_bo_map(dri_bo *buf, GLboolean write_enable)
507 {
508 dri_bufmgr_ttm *bufmgr_ttm;
509 dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
510 uint64_t flags;
511 int ret;
512
513 bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;
514
515 flags = DRM_BO_FLAG_READ;
516 if (write_enable)
517 flags |= DRM_BO_FLAG_WRITE;
518
519 assert(buf->virtual == NULL);
520
521 DBG("bo_map: %p (%s)\n", &ttm_buf->bo, ttm_buf->name);
522
523 /* XXX: What about if we're upgrading from READ to WRITE? */
524 if (ttm_buf->delayed_unmap) {
525 buf->virtual = ttm_buf->saved_virtual;
526 return 0;
527 }
528
529 ret = drmBOMap(bufmgr_ttm->fd, &ttm_buf->drm_bo, flags, 0, &buf->virtual);
530 if (ret != 0) {
531 fprintf(stderr, "%s:%d: Error mapping buffer %s: %s .\n",
532 __FILE__, __LINE__, ttm_buf->name, strerror(-ret));
533 }
534
535 return ret;
536 }
537
538 static int
539 dri_ttm_bo_unmap(dri_bo *buf)
540 {
541 dri_bufmgr_ttm *bufmgr_ttm;
542 dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
543 int ret;
544
545 if (buf == NULL)
546 return 0;
547
548 bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;
549
550 assert(buf->virtual != NULL);
551
552 DBG("bo_unmap: %p (%s)\n", &ttm_buf->bo, ttm_buf->name);
553
554 if (!ttm_buf->shared) {
555 ttm_buf->saved_virtual = buf->virtual;
556 ttm_buf->delayed_unmap = GL_TRUE;
557 buf->virtual = NULL;
558
559 return 0;
560 }
561
562 buf->virtual = NULL;
563
564 ret = drmBOUnmap(bufmgr_ttm->fd, &ttm_buf->drm_bo);
565 if (ret != 0) {
566 fprintf(stderr, "%s:%d: Error unmapping buffer %s: %s.\n",
567 __FILE__, __LINE__, ttm_buf->name, strerror(-ret));
568 }
569
570 return ret;
571 }
572
573 /**
574 * Returns a dri_bo wrapping the given buffer object handle.
575 *
576 * This can be used when one application needs to pass a buffer object
577 * to another.
578 */
579 dri_fence *
580 intel_ttm_fence_create_from_arg(dri_bufmgr *bufmgr, const char *name,
581 drm_fence_arg_t *arg)
582 {
583 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bufmgr;
584 dri_fence_ttm *ttm_fence;
585
586 ttm_fence = malloc(sizeof(*ttm_fence));
587 if (!ttm_fence)
588 return NULL;
589
590 ttm_fence->drm_fence.handle = arg->handle;
591 ttm_fence->drm_fence.fence_class = arg->fence_class;
592 ttm_fence->drm_fence.type = arg->type;
593 ttm_fence->drm_fence.flags = arg->flags;
594 ttm_fence->drm_fence.signaled = 0;
595 ttm_fence->drm_fence.sequence = arg->sequence;
596
597 ttm_fence->fence.bufmgr = bufmgr;
598 ttm_fence->name = name;
599 ttm_fence->refcount = 1;
600
601 DBG("fence_create_from_handle: %p (%s)\n",
602 &ttm_fence->fence, ttm_fence->name);
603
604 return &ttm_fence->fence;
605 }
606
607
608 static void
609 dri_ttm_fence_reference(dri_fence *fence)
610 {
611 dri_fence_ttm *fence_ttm = (dri_fence_ttm *)fence;
612 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)fence->bufmgr;
613
614 ++fence_ttm->refcount;
615 DBG("fence_reference: %p (%s)\n", &fence_ttm->fence, fence_ttm->name);
616 }
617
618 static void
619 dri_ttm_fence_unreference(dri_fence *fence)
620 {
621 dri_fence_ttm *fence_ttm = (dri_fence_ttm *)fence;
622 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)fence->bufmgr;
623
624 if (!fence)
625 return;
626
627 DBG("fence_unreference: %p (%s)\n", &fence_ttm->fence, fence_ttm->name);
628
629 if (--fence_ttm->refcount == 0) {
630 int ret;
631
632 ret = drmFenceUnreference(bufmgr_ttm->fd, &fence_ttm->drm_fence);
633 if (ret != 0) {
634 fprintf(stderr, "drmFenceUnreference failed (%s): %s\n",
635 fence_ttm->name, strerror(-ret));
636 }
637
638 free(fence);
639 return;
640 }
641 }
642
643 static void
644 dri_ttm_fence_wait(dri_fence *fence)
645 {
646 dri_fence_ttm *fence_ttm = (dri_fence_ttm *)fence;
647 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)fence->bufmgr;
648 int ret;
649
650 ret = drmFenceWait(bufmgr_ttm->fd, DRM_FENCE_FLAG_WAIT_LAZY, &fence_ttm->drm_fence, 0);
651 if (ret != 0) {
652 fprintf(stderr, "%s:%d: Error waiting for fence %s: %s.\n",
653 __FILE__, __LINE__, fence_ttm->name, strerror(-ret));
654 abort();
655 }
656
657 DBG("fence_wait: %p (%s)\n", &fence_ttm->fence, fence_ttm->name);
658 }
659
660 static void
661 dri_bufmgr_ttm_destroy(dri_bufmgr *bufmgr)
662 {
663 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bufmgr;
664
665 if (bufmgr_ttm->cached_reloc_buf_data) {
666 free(bufmgr_ttm->cached_reloc_buf_data);
667 }
668
669 free(bufmgr_ttm->validate_array);
670
671 free(bufmgr);
672 }
673
674 /**
675 * Adds the target buffer to the validation list and adds the relocation
676 * to the reloc_buffer's relocation list.
677 *
678 * The relocation entry at the given offset must already contain the
679 * precomputed relocation value, because the kernel will optimize out
680 * the relocation entry write when the buffer hasn't moved from the
681 * last known offset in target_buf.
682 */
683 static void
684 dri_ttm_emit_reloc(dri_bo *reloc_buf, uint64_t flags, GLuint delta,
685 GLuint offset, dri_bo *target_buf)
686 {
687 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)reloc_buf->bufmgr;
688 dri_bo_ttm *reloc_buf_ttm = (dri_bo_ttm *)reloc_buf;
689 dri_bo_ttm *target_buf_ttm = (dri_bo_ttm *)target_buf;
690 int num_relocs;
691 uint32_t *this_reloc;
692
693 /* Create a new relocation list if needed */
694 if (reloc_buf_ttm->reloc_buf_data == NULL)
695 intel_setup_reloc_list(reloc_buf);
696
697 num_relocs = reloc_buf_ttm->reloc_buf_data[0];
698
699 /* Check overflow */
700 assert(num_relocs < bufmgr_ttm->max_relocs);
701
702 this_reloc = reloc_buf_ttm->reloc_buf_data + I915_RELOC_HEADER +
703 num_relocs * I915_RELOC0_STRIDE;
704
705 this_reloc[0] = offset;
706 this_reloc[1] = delta;
707 this_reloc[2] = target_buf_ttm->drm_bo.handle; /* To be filled in at exec time */
708 this_reloc[3] = 0;
709
710 reloc_buf_ttm->relocs[num_relocs].validate_flags = flags;
711 reloc_buf_ttm->relocs[num_relocs].target_buf = target_buf;
712 dri_bo_reference(target_buf);
713
714 reloc_buf_ttm->reloc_buf_data[0]++; /* Increment relocation count */
715 /* Check wraparound */
716 assert(reloc_buf_ttm->reloc_buf_data[0] != 0);
717 }
718
719 /**
720 * Walk the tree of relocations rooted at BO and accumulate the list of
721 * validations to be performed and update the relocation buffers with
722 * index values into the validation list.
723 */
724 static void
725 dri_ttm_bo_process_reloc(dri_bo *bo)
726 {
727 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bo->bufmgr;
728 dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
729 unsigned int nr_relocs;
730 int i;
731
732 if (bo_ttm->reloc_buf_data == NULL)
733 return;
734
735 nr_relocs = bo_ttm->reloc_buf_data[0] & 0xffff;
736
737 for (i = 0; i < nr_relocs; i++) {
738 struct dri_ttm_reloc *r = &bo_ttm->relocs[i];
739
740 /* Continue walking the tree depth-first. */
741 dri_ttm_bo_process_reloc(r->target_buf);
742
743 /* Add the target to the validate list */
744 intel_add_validate_buffer(r->target_buf, r->validate_flags);
745
746 /* Clear the PRESUMED_OFFSET flag from the validate list entry of the
747 * target if this buffer has a stale relocated pointer at it.
748 */
749 if (r->last_target_offset != r->target_buf->offset) {
750 dri_bo_ttm *target_buf_ttm = (dri_bo_ttm *)r->target_buf;
751 struct intel_validate_entry *entry =
752 &bufmgr_ttm->validate_array[target_buf_ttm->validate_index];
753
754 entry->bo_arg.d.req.bo_req.flags &= ~DRM_BO_HINT_PRESUMED_OFFSET;
755 }
756 }
757 }
758
759 static void *
760 dri_ttm_process_reloc(dri_bo *batch_buf, GLuint *count)
761 {
762 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)batch_buf->bufmgr;
763
764 /* Update indices and set up the validate list. */
765 dri_ttm_bo_process_reloc(batch_buf);
766
767 /* Add the batch buffer to the validation list. There are no relocations
768 * pointing to it.
769 */
770 intel_add_validate_buffer(batch_buf,
771 DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_EXE);
772
773 *count = bufmgr_ttm->validate_count;
774 return &bufmgr_ttm->validate_array[0].bo_arg;
775 }
776
777 static const char *
778 intel_get_flags_mem_type_string(uint64_t flags)
779 {
780 switch (flags & DRM_BO_MASK_MEM) {
781 case DRM_BO_FLAG_MEM_LOCAL: return "local";
782 case DRM_BO_FLAG_MEM_TT: return "ttm";
783 case DRM_BO_FLAG_MEM_VRAM: return "vram";
784 case DRM_BO_FLAG_MEM_PRIV0: return "priv0";
785 case DRM_BO_FLAG_MEM_PRIV1: return "priv1";
786 case DRM_BO_FLAG_MEM_PRIV2: return "priv2";
787 case DRM_BO_FLAG_MEM_PRIV3: return "priv3";
788 case DRM_BO_FLAG_MEM_PRIV4: return "priv4";
789 default: return NULL;
790 }
791 }
792
793 static const char *
794 intel_get_flags_caching_string(uint64_t flags)
795 {
796 switch (flags & (DRM_BO_FLAG_CACHED | DRM_BO_FLAG_CACHED_MAPPED)) {
797 case 0: return "UU";
798 case DRM_BO_FLAG_CACHED: return "CU";
799 case DRM_BO_FLAG_CACHED_MAPPED: return "UC";
800 case DRM_BO_FLAG_CACHED | DRM_BO_FLAG_CACHED_MAPPED: return "CC";
801 default: return NULL;
802 }
803 }
804
805 static void
806 intel_update_buffer_offsets (dri_bufmgr_ttm *bufmgr_ttm)
807 {
808 int i;
809
810 for (i = 0; i < bufmgr_ttm->validate_count; i++) {
811 dri_bo *bo = bufmgr_ttm->validate_array[i].bo;
812 dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
813 struct drm_i915_op_arg *arg = &bufmgr_ttm->validate_array[i].bo_arg;
814 struct drm_bo_arg_rep *rep = &arg->d.rep;
815
816 /* Update the flags */
817 if (rep->bo_info.flags != bo_ttm->last_flags) {
818 DBG("BO %s migrated: %s/%s -> %s/%s\n",
819 bo_ttm->name,
820 intel_get_flags_mem_type_string(bo_ttm->last_flags),
821 intel_get_flags_caching_string(bo_ttm->last_flags),
822 intel_get_flags_mem_type_string(rep->bo_info.flags),
823 intel_get_flags_caching_string(rep->bo_info.flags));
824
825 bo_ttm->last_flags = rep->bo_info.flags;
826 }
827 /* Update the buffer offset */
828 if (rep->bo_info.offset != bo->offset) {
829 DBG("BO %s migrated: 0x%08lx -> 0x%08lx\n",
830 bo_ttm->name, bo->offset, (unsigned long)rep->bo_info.offset);
831 bo->offset = rep->bo_info.offset;
832 }
833 }
834 }
835
836 /**
837 * Update the last target offset field of relocation entries for PRESUMED_OFFSET
838 * computation.
839 */
840 static void
841 dri_ttm_bo_post_submit(dri_bo *bo)
842 {
843 dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
844 unsigned int nr_relocs;
845 int i;
846
847 if (bo_ttm->reloc_buf_data == NULL)
848 return;
849
850 nr_relocs = bo_ttm->reloc_buf_data[0] & 0xffff;
851
852 for (i = 0; i < nr_relocs; i++) {
853 struct dri_ttm_reloc *r = &bo_ttm->relocs[i];
854
855 /* Continue walking the tree depth-first. */
856 dri_ttm_bo_post_submit(r->target_buf);
857
858 r->last_target_offset = bo->offset;
859 }
860 }
861
862 static void
863 dri_ttm_post_submit(dri_bo *batch_buf, dri_fence **last_fence)
864 {
865 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)batch_buf->bufmgr;
866 int i;
867
868 intel_update_buffer_offsets (bufmgr_ttm);
869
870 dri_ttm_bo_post_submit(batch_buf);
871
872 if (bufmgr_ttm->bufmgr.debug)
873 dri_ttm_dump_validation_list(bufmgr_ttm);
874
875 for (i = 0; i < bufmgr_ttm->validate_count; i++) {
876 dri_bo *bo = bufmgr_ttm->validate_array[i].bo;
877 dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
878
879 /* Disconnect the buffer from the validate list */
880 bo_ttm->validate_index = -1;
881 dri_bo_unreference(bo);
882 bufmgr_ttm->validate_array[i].bo = NULL;
883 }
884 bufmgr_ttm->validate_count = 0;
885 }
886
887 /**
888 * Initializes the TTM buffer manager, which uses the kernel to allocate, map,
889 * and manage map buffer objections.
890 *
891 * \param fd File descriptor of the opened DRM device.
892 * \param fence_type Driver-specific fence type used for fences with no flush.
893 * \param fence_type_flush Driver-specific fence type used for fences with a
894 * flush.
895 */
896 dri_bufmgr *
897 intel_bufmgr_ttm_init(int fd, unsigned int fence_type,
898 unsigned int fence_type_flush, int batch_size)
899 {
900 dri_bufmgr_ttm *bufmgr_ttm;
901
902 bufmgr_ttm = calloc(1, sizeof(*bufmgr_ttm));
903 bufmgr_ttm->fd = fd;
904 bufmgr_ttm->fence_type = fence_type;
905 bufmgr_ttm->fence_type_flush = fence_type_flush;
906 bufmgr_ttm->cached_reloc_buf_data = NULL;
907
908 /* Let's go with one relocation per every 2 dwords (but round down a bit
909 * since a power of two will mean an extra page allocation for the reloc
910 * buffer).
911 *
912 * Every 4 was too few for the blender benchmark.
913 */
914 bufmgr_ttm->max_relocs = batch_size / sizeof(uint32_t) / 2 - 2;
915
916 bufmgr_ttm->bufmgr.bo_alloc = dri_ttm_alloc;
917 bufmgr_ttm->bufmgr.bo_alloc_static = dri_ttm_alloc_static;
918 bufmgr_ttm->bufmgr.bo_reference = dri_ttm_bo_reference;
919 bufmgr_ttm->bufmgr.bo_unreference = dri_ttm_bo_unreference;
920 bufmgr_ttm->bufmgr.bo_map = dri_ttm_bo_map;
921 bufmgr_ttm->bufmgr.bo_unmap = dri_ttm_bo_unmap;
922 bufmgr_ttm->bufmgr.fence_reference = dri_ttm_fence_reference;
923 bufmgr_ttm->bufmgr.fence_unreference = dri_ttm_fence_unreference;
924 bufmgr_ttm->bufmgr.fence_wait = dri_ttm_fence_wait;
925 bufmgr_ttm->bufmgr.destroy = dri_bufmgr_ttm_destroy;
926 bufmgr_ttm->bufmgr.emit_reloc = dri_ttm_emit_reloc;
927 bufmgr_ttm->bufmgr.process_relocs = dri_ttm_process_reloc;
928 bufmgr_ttm->bufmgr.post_submit = dri_ttm_post_submit;
929 bufmgr_ttm->bufmgr.debug = GL_FALSE;
930
931 return &bufmgr_ttm->bufmgr;
932 }
933