1 /**************************************************************************
3 * Copyright © 2007 Red Hat Inc.
4 * Copyright © 2007 Intel Corporation
5 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
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:
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.
24 * The above copyright notice and this permission notice (including the
25 * next paragraph) shall be included in all copies or substantial portions
29 **************************************************************************/
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>
43 #include "dri_bufmgr.h"
49 #include "intel_bufmgr_ttm.h"
51 #define DBG(...) do { \
52 if (bufmgr_ttm->bufmgr.debug) \
53 _mesa_printf(__VA_ARGS__); \
57 * These bits are always specified in each validation
58 * request. Other bits are not supported at this point
59 * as it would require a bit of investigation to figure
60 * out what mask value should be used.
62 #define INTEL_BO_MASK (DRM_BO_MASK_MEM | \
67 struct intel_validate_entry {
69 struct drm_i915_op_arg bo_arg;
72 typedef struct _dri_bufmgr_ttm {
76 unsigned int fence_type;
77 unsigned int fence_type_flush;
81 struct intel_validate_entry *validate_array;
82 int validate_array_size;
85 drmBO *cached_reloc_buf;
86 uint32_t *cached_reloc_buf_data;
90 * Private information associated with a relocation that isn't already stored
91 * in the relocation buffer to be passed to the kernel.
93 struct dri_ttm_reloc {
95 uint64_t validate_flags;
98 typedef struct _dri_bo_ttm {
108 * Index of the buffer within the validation list while preparing a
109 * batchbuffer execution.
113 /** DRM buffer object containing relocation list */
115 uint32_t *reloc_buf_data;
116 struct dri_ttm_reloc *relocs;
119 * Indicates that the buffer may be shared with other processes, so we
120 * can't hold maps beyond when the user does.
124 GLboolean delayed_unmap;
125 /* Virtual address from the dri_bo_map whose unmap was delayed. */
129 typedef struct _dri_fence_ttm
138 static void dri_ttm_dump_validation_list(dri_bufmgr_ttm *bufmgr_ttm)
142 for (i = 0; i < bufmgr_ttm->validate_count; i++) {
143 dri_bo *bo = bufmgr_ttm->validate_array[i].bo;
144 dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
146 if (bo_ttm->reloc_buf_data != NULL) {
147 for (j = 0; j < (bo_ttm->reloc_buf_data[0] & 0xffff); j++) {
148 uint32_t *reloc_entry = bo_ttm->reloc_buf_data +
150 j * I915_RELOC0_STRIDE;
152 bufmgr_ttm->validate_array[reloc_entry[2]].bo;
153 dri_bo_ttm *target_ttm = (dri_bo_ttm *)target_bo;
155 DBG("%2d: %s@0x%08x -> %s@0x%08x + 0x%08x\n",
157 bo_ttm->name, reloc_entry[0],
158 target_ttm->name, target_bo->offset,
162 DBG("%2d: %s\n", i, bo_ttm->name);
168 * Adds the given buffer to the list of buffers to be validated (moved into the
169 * appropriate memory type) with the next batch submission.
171 * If a buffer is validated multiple times in a batch submission, it ends up
172 * with the intersection of the memory type flags and the union of the
176 intel_add_validate_buffer(dri_bo *buf,
179 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;
180 dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
182 /* If we delayed doing an unmap to mitigate map/unmap syscall thrashing,
185 if (ttm_buf->delayed_unmap) {
186 drmBOUnmap(bufmgr_ttm->fd, &ttm_buf->drm_bo);
187 ttm_buf->delayed_unmap = GL_FALSE;
190 if (ttm_buf->validate_index == -1) {
191 struct intel_validate_entry *entry;
192 struct drm_i915_op_arg *arg;
193 struct drm_bo_op_req *req;
196 /* Extend the array of validation entries as necessary. */
197 if (bufmgr_ttm->validate_count == bufmgr_ttm->validate_array_size) {
198 int i, new_size = bufmgr_ttm->validate_array_size * 2;
203 bufmgr_ttm->validate_array =
204 realloc(bufmgr_ttm->validate_array,
205 sizeof(struct intel_validate_entry) * new_size);
206 bufmgr_ttm->validate_array_size = new_size;
208 /* Update pointers for realloced mem. */
209 for (i = 0; i < bufmgr_ttm->validate_count - 1; i++) {
210 bufmgr_ttm->validate_array[i].bo_arg.next = (unsigned long)
211 &bufmgr_ttm->validate_array[i + 1].bo_arg;
215 /* Pick out the new array entry for ourselves */
216 index = bufmgr_ttm->validate_count;
217 ttm_buf->validate_index = index;
218 entry = &bufmgr_ttm->validate_array[index];
219 bufmgr_ttm->validate_count++;
221 /* Fill in array entry */
223 dri_bo_reference(buf);
225 /* Fill in kernel arg */
226 arg = &entry->bo_arg;
229 memset(arg, 0, sizeof(*arg));
230 req->bo_req.handle = ttm_buf->drm_bo.handle;
231 req->op = drm_bo_validate;
232 req->bo_req.flags = flags;
233 req->bo_req.hint = 0;
234 #ifdef DRM_BO_HINT_PRESUMED_OFFSET
235 req->bo_req.hint |= DRM_BO_HINT_PRESUMED_OFFSET;
236 req->bo_req.presumed_offset = buf->offset;
238 req->bo_req.mask = INTEL_BO_MASK;
239 req->bo_req.fence_class = 0; /* Backwards compat. */
241 if (ttm_buf->reloc_buf != NULL)
242 arg->reloc_handle = ttm_buf->reloc_buf->handle;
244 arg->reloc_handle = 0;
246 /* Hook up the linked list of args for the kernel */
249 bufmgr_ttm->validate_array[index - 1].bo_arg.next =
253 struct intel_validate_entry *entry =
254 &bufmgr_ttm->validate_array[ttm_buf->validate_index];
255 struct drm_i915_op_arg *arg = &entry->bo_arg;
256 struct drm_bo_op_req *req = &arg->d.req;
257 uint64_t memFlags = req->bo_req.flags & flags & DRM_BO_MASK_MEM;
258 uint64_t modeFlags = (req->bo_req.flags | flags) & ~DRM_BO_MASK_MEM;
260 /* Buffer was already in the validate list. Extend its flags as
266 "%s: No shared memory types between "
267 "0x%16llx and 0x%16llx\n",
268 __FUNCTION__, req->bo_req.flags, flags);
271 if (flags & ~INTEL_BO_MASK) {
273 "%s: Flags bits 0x%16llx are not supposed to be used in a relocation\n",
274 __FUNCTION__, flags & ~INTEL_BO_MASK);
277 req->bo_req.flags = memFlags | modeFlags;
282 #define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \
286 intel_setup_reloc_list(dri_bo *bo)
288 dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
289 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bo->bufmgr;
292 bo_ttm->relocs = malloc(sizeof(struct dri_ttm_reloc) *
293 bufmgr_ttm->max_relocs);
295 if (bufmgr_ttm->cached_reloc_buf != NULL) {
296 bo_ttm->reloc_buf = bufmgr_ttm->cached_reloc_buf;
297 bo_ttm->reloc_buf_data = bufmgr_ttm->cached_reloc_buf_data;
299 bufmgr_ttm->cached_reloc_buf = NULL;
300 bufmgr_ttm->cached_reloc_buf_data = NULL;
302 bo_ttm->reloc_buf = malloc(sizeof(bo_ttm->drm_bo));
303 ret = drmBOCreate(bufmgr_ttm->fd,
304 RELOC_BUF_SIZE(bufmgr_ttm->max_relocs), 0,
306 DRM_BO_FLAG_MEM_LOCAL |
309 DRM_BO_FLAG_MAPPABLE |
311 0, bo_ttm->reloc_buf);
313 fprintf(stderr, "Failed to create relocation BO: %s\n",
318 ret = drmBOMap(bufmgr_ttm->fd, bo_ttm->reloc_buf,
319 DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE,
320 0, (void **)&bo_ttm->reloc_buf_data);
322 fprintf(stderr, "Failed to map relocation BO: %s\n",
328 /* Initialize the relocation list with the header:
329 * DWORD 0: relocation type, relocation count
330 * DWORD 1: handle to next relocation list (currently none)
334 bo_ttm->reloc_buf_data[0] = I915_RELOC_TYPE_0 << 16;
335 bo_ttm->reloc_buf_data[1] = 0;
336 bo_ttm->reloc_buf_data[2] = 0;
337 bo_ttm->reloc_buf_data[3] = 0;
344 driFenceSignaled(DriFenceObject * fence, unsigned type)
352 ret = drmFenceSignaled(bufmgr_ttm->fd, &fence->fence, type, &signaled);
359 dri_ttm_alloc(dri_bufmgr *bufmgr, const char *name,
360 unsigned long size, unsigned int alignment,
361 uint64_t location_mask)
363 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bufmgr;
365 unsigned int pageSize = getpagesize();
367 unsigned int flags, hint;
369 ttm_buf = malloc(sizeof(*ttm_buf));
373 /* The mask argument doesn't do anything for us that we want other than
374 * determine which pool (TTM or local) the buffer is allocated into, so
375 * just pass all of the allocation class flags.
377 flags = location_mask | DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE |
379 /* No hints we want to use. */
382 ret = drmBOCreate(bufmgr_ttm->fd, size, alignment / pageSize,
383 NULL, flags, hint, &ttm_buf->drm_bo);
388 ttm_buf->bo.size = ttm_buf->drm_bo.size;
389 ttm_buf->bo.offset = ttm_buf->drm_bo.offset;
390 ttm_buf->bo.virtual = NULL;
391 ttm_buf->bo.bufmgr = bufmgr;
392 ttm_buf->name = name;
393 ttm_buf->refcount = 1;
394 ttm_buf->reloc_buf = NULL;
395 ttm_buf->reloc_buf_data = NULL;
396 ttm_buf->relocs = NULL;
397 ttm_buf->last_flags = ttm_buf->drm_bo.flags;
398 ttm_buf->shared = GL_FALSE;
399 ttm_buf->delayed_unmap = GL_FALSE;
400 ttm_buf->validate_index = -1;
402 DBG("bo_create: %p (%s) %db\n", &ttm_buf->bo, ttm_buf->name, size);
407 /* Our TTM backend doesn't allow creation of static buffers, as that requires
408 * privelege for the non-fake case, and the lock in the fake case where we were
409 * working around the X Server not creating buffers and passing handles to us.
412 dri_ttm_alloc_static(dri_bufmgr *bufmgr, const char *name,
413 unsigned long offset, unsigned long size, void *virtual,
414 uint64_t location_mask)
420 * Returns a dri_bo wrapping the given buffer object handle.
422 * This can be used when one application needs to pass a buffer object
426 intel_ttm_bo_create_from_handle(dri_bufmgr *bufmgr, const char *name,
429 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bufmgr;
433 ttm_buf = malloc(sizeof(*ttm_buf));
437 ret = drmBOReference(bufmgr_ttm->fd, handle, &ttm_buf->drm_bo);
439 fprintf(stderr, "Couldn't reference %s handle 0x%08x: %s\n",
440 name, handle, strerror(-ret));
444 ttm_buf->bo.size = ttm_buf->drm_bo.size;
445 ttm_buf->bo.offset = ttm_buf->drm_bo.offset;
446 ttm_buf->bo.virtual = NULL;
447 ttm_buf->bo.bufmgr = bufmgr;
448 ttm_buf->name = name;
449 ttm_buf->refcount = 1;
450 ttm_buf->reloc_buf = NULL;
451 ttm_buf->reloc_buf_data = NULL;
452 ttm_buf->relocs = NULL;
453 ttm_buf->last_flags = ttm_buf->drm_bo.flags;
454 ttm_buf->shared = GL_TRUE;
455 ttm_buf->delayed_unmap = GL_FALSE;
456 ttm_buf->validate_index = -1;
458 DBG("bo_create_from_handle: %p %08x (%s)\n",
459 &ttm_buf->bo, handle, ttm_buf->name);
465 dri_ttm_bo_reference(dri_bo *buf)
467 dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
473 dri_ttm_bo_unreference(dri_bo *buf)
475 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;
476 dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
481 if (--ttm_buf->refcount == 0) {
484 if (ttm_buf->reloc_buf) {
487 /* Unreference all the target buffers */
488 for (i = 0; i < (ttm_buf->reloc_buf_data[0] & 0xffff); i++)
489 dri_bo_unreference(ttm_buf->relocs[i].target_buf);
490 free(ttm_buf->relocs);
492 if (bufmgr_ttm->cached_reloc_buf == NULL) {
493 /* Cache a single relocation buffer allocation to avoid
494 * repeated create/map/unmap/destroy for batchbuffer
497 bufmgr_ttm->cached_reloc_buf = ttm_buf->reloc_buf;
498 bufmgr_ttm->cached_reloc_buf_data = ttm_buf->reloc_buf_data;
500 /* Free the kernel BO containing relocation entries */
501 drmBOUnmap(bufmgr_ttm->fd, ttm_buf->reloc_buf);
502 drmBOUnreference(bufmgr_ttm->fd, ttm_buf->reloc_buf);
503 free(ttm_buf->reloc_buf);
507 if (ttm_buf->delayed_unmap)
508 drmBOUnmap(bufmgr_ttm->fd, &ttm_buf->drm_bo);
510 ret = drmBOUnreference(bufmgr_ttm->fd, &ttm_buf->drm_bo);
512 fprintf(stderr, "drmBOUnreference failed (%s): %s\n",
513 ttm_buf->name, strerror(-ret));
515 DBG("bo_unreference final: %p (%s)\n", &ttm_buf->bo, ttm_buf->name);
523 dri_ttm_bo_map(dri_bo *buf, GLboolean write_enable)
525 dri_bufmgr_ttm *bufmgr_ttm;
526 dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
529 bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;
531 flags = DRM_BO_FLAG_READ;
533 flags |= DRM_BO_FLAG_WRITE;
535 assert(buf->virtual == NULL);
537 DBG("bo_map: %p (%s)\n", &ttm_buf->bo, ttm_buf->name);
539 /* XXX: What about if we're upgrading from READ to WRITE? */
540 if (ttm_buf->delayed_unmap) {
541 buf->virtual = ttm_buf->saved_virtual;
545 return drmBOMap(bufmgr_ttm->fd, &ttm_buf->drm_bo, flags, 0, &buf->virtual);
549 dri_ttm_bo_unmap(dri_bo *buf)
551 dri_bufmgr_ttm *bufmgr_ttm;
552 dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
557 bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;
559 assert(buf->virtual != NULL);
561 DBG("bo_unmap: %p (%s)\n", &ttm_buf->bo, ttm_buf->name);
563 if (!ttm_buf->shared) {
564 ttm_buf->saved_virtual = buf->virtual;
565 ttm_buf->delayed_unmap = GL_TRUE;
573 return drmBOUnmap(bufmgr_ttm->fd, &ttm_buf->drm_bo);
577 * Returns a dri_bo wrapping the given buffer object handle.
579 * This can be used when one application needs to pass a buffer object
583 intel_ttm_fence_create_from_arg(dri_bufmgr *bufmgr, const char *name,
584 drm_fence_arg_t *arg)
586 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bufmgr;
587 dri_fence_ttm *ttm_fence;
589 ttm_fence = malloc(sizeof(*ttm_fence));
593 ttm_fence->drm_fence.handle = arg->handle;
594 ttm_fence->drm_fence.fence_class = arg->fence_class;
595 ttm_fence->drm_fence.type = arg->type;
596 ttm_fence->drm_fence.flags = arg->flags;
597 ttm_fence->drm_fence.signaled = 0;
598 ttm_fence->drm_fence.sequence = arg->sequence;
600 ttm_fence->fence.bufmgr = bufmgr;
601 ttm_fence->name = name;
602 ttm_fence->refcount = 1;
604 DBG("fence_create_from_handle: %p (%s)\n",
605 &ttm_fence->fence, ttm_fence->name);
607 return &ttm_fence->fence;
612 dri_ttm_fence_reference(dri_fence *fence)
614 dri_fence_ttm *fence_ttm = (dri_fence_ttm *)fence;
615 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)fence->bufmgr;
617 ++fence_ttm->refcount;
618 DBG("fence_reference: %p (%s)\n", &fence_ttm->fence, fence_ttm->name);
622 dri_ttm_fence_unreference(dri_fence *fence)
624 dri_fence_ttm *fence_ttm = (dri_fence_ttm *)fence;
625 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)fence->bufmgr;
630 DBG("fence_unreference: %p (%s)\n", &fence_ttm->fence, fence_ttm->name);
632 if (--fence_ttm->refcount == 0) {
635 ret = drmFenceUnreference(bufmgr_ttm->fd, &fence_ttm->drm_fence);
637 fprintf(stderr, "drmFenceUnreference failed (%s): %s\n",
638 fence_ttm->name, strerror(-ret));
647 dri_ttm_fence_wait(dri_fence *fence)
649 dri_fence_ttm *fence_ttm = (dri_fence_ttm *)fence;
650 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)fence->bufmgr;
653 ret = drmFenceWait(bufmgr_ttm->fd, DRM_FENCE_FLAG_WAIT_LAZY, &fence_ttm->drm_fence, 0);
655 _mesa_printf("%s:%d: Error %d waiting for fence %s.\n",
656 __FILE__, __LINE__, ret, fence_ttm->name);
660 DBG("fence_wait: %p (%s)\n", &fence_ttm->fence, fence_ttm->name);
664 dri_bufmgr_ttm_destroy(dri_bufmgr *bufmgr)
666 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bufmgr;
668 if (bufmgr_ttm->cached_reloc_buf) {
669 /* Free the cached kernel BO containing relocation entries */
670 drmBOUnmap(bufmgr_ttm->fd, bufmgr_ttm->cached_reloc_buf);
671 drmBOUnreference(bufmgr_ttm->fd, bufmgr_ttm->cached_reloc_buf);
672 free(bufmgr_ttm->cached_reloc_buf);
675 free(bufmgr_ttm->validate_array);
681 * Adds the target buffer to the validation list and adds the relocation
682 * to the reloc_buffer's relocation list.
684 * The relocation entry at the given offset must already contain the
685 * precomputed relocation value, because the kernel will optimize out
686 * the relocation entry write when the buffer hasn't moved from the
687 * last known offset in target_buf.
690 dri_ttm_emit_reloc(dri_bo *reloc_buf, uint64_t flags, GLuint delta,
691 GLuint offset, dri_bo *target_buf)
693 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)reloc_buf->bufmgr;
694 dri_bo_ttm *reloc_buf_ttm = (dri_bo_ttm *)reloc_buf;
696 uint32_t *this_reloc;
698 /* Create a new relocation list if needed */
699 if (reloc_buf_ttm->reloc_buf == NULL)
700 intel_setup_reloc_list(reloc_buf);
702 num_relocs = (reloc_buf_ttm->reloc_buf_data[0] & 0xffff);
705 assert((reloc_buf_ttm->reloc_buf_data[0] & 0xffff) <
706 bufmgr_ttm->max_relocs);
708 this_reloc = reloc_buf_ttm->reloc_buf_data + I915_RELOC_HEADER +
709 num_relocs * I915_RELOC0_STRIDE;
711 this_reloc[0] = offset;
712 this_reloc[1] = delta;
713 this_reloc[2] = -1; /* To be filled in at exec time */
716 reloc_buf_ttm->relocs[num_relocs].validate_flags = flags;
717 reloc_buf_ttm->relocs[num_relocs].target_buf = target_buf;
718 dri_bo_reference(target_buf);
720 reloc_buf_ttm->reloc_buf_data[0]++; /* Increment relocation count */
721 /* Check wraparound */
722 assert((reloc_buf_ttm->reloc_buf_data[0] & 0xffff) != 0);
726 * Walk the tree of relocations rooted at BO and accumulate the list of
727 * validations to be performed and update the relocation buffers with
728 * index values into the validation list.
731 dri_ttm_bo_process_reloc(dri_bo *bo)
733 dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
734 unsigned int nr_relocs;
737 if (bo_ttm->reloc_buf_data == NULL)
740 nr_relocs = bo_ttm->reloc_buf_data[0] & 0xffff;
742 for (i = 0; i < nr_relocs; i++) {
743 struct dri_ttm_reloc *r = &bo_ttm->relocs[i];
744 dri_bo_ttm *target_ttm = (dri_bo_ttm *)r->target_buf;
745 uint32_t *reloc_entry;
747 /* Continue walking the tree depth-first. */
748 dri_ttm_bo_process_reloc(r->target_buf);
750 /* Add the target to the validate list */
751 intel_add_validate_buffer(r->target_buf, r->validate_flags);
753 /* Update the index of the target in the relocation entry */
754 reloc_entry = bo_ttm->reloc_buf_data + I915_RELOC_HEADER +
755 i * I915_RELOC0_STRIDE;
756 reloc_entry[2] = target_ttm->validate_index;
761 dri_ttm_process_reloc(dri_bo *batch_buf, GLuint *count)
763 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)batch_buf->bufmgr;
765 /* Update indices and set up the validate list. */
766 dri_ttm_bo_process_reloc(batch_buf);
768 /* Add the batch buffer to the validation list. There are no relocations
771 intel_add_validate_buffer(batch_buf,
772 DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_EXE);
774 *count = bufmgr_ttm->validate_count;
775 return &bufmgr_ttm->validate_array[0].bo_arg;
779 intel_get_flags_mem_type_string(uint64_t flags)
781 switch (flags & DRM_BO_MASK_MEM) {
782 case DRM_BO_FLAG_MEM_LOCAL: return "local";
783 case DRM_BO_FLAG_MEM_TT: return "ttm";
784 case DRM_BO_FLAG_MEM_VRAM: return "vram";
785 case DRM_BO_FLAG_MEM_PRIV0: return "priv0";
786 case DRM_BO_FLAG_MEM_PRIV1: return "priv1";
787 case DRM_BO_FLAG_MEM_PRIV2: return "priv2";
788 case DRM_BO_FLAG_MEM_PRIV3: return "priv3";
789 case DRM_BO_FLAG_MEM_PRIV4: return "priv4";
790 default: return NULL;
795 intel_get_flags_caching_string(uint64_t flags)
797 switch (flags & (DRM_BO_FLAG_CACHED | DRM_BO_FLAG_CACHED_MAPPED)) {
799 case DRM_BO_FLAG_CACHED: return "CU";
800 case DRM_BO_FLAG_CACHED_MAPPED: return "UC";
801 case DRM_BO_FLAG_CACHED | DRM_BO_FLAG_CACHED_MAPPED: return "CC";
802 default: return NULL;
807 intel_update_buffer_offsets (dri_bufmgr_ttm *bufmgr_ttm)
811 for (i = 0; i < bufmgr_ttm->validate_count; i++) {
812 dri_bo *bo = bufmgr_ttm->validate_array[i].bo;
813 dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
814 struct drm_i915_op_arg *arg = &bufmgr_ttm->validate_array[i].bo_arg;
815 struct drm_bo_arg_rep *rep = &arg->d.rep;
817 /* Update the flags */
818 if (rep->bo_info.flags != bo_ttm->last_flags) {
819 DBG("BO %s migrated: %s/%s -> %s/%s\n",
821 intel_get_flags_mem_type_string(bo_ttm->last_flags),
822 intel_get_flags_caching_string(bo_ttm->last_flags),
823 intel_get_flags_mem_type_string(rep->bo_info.flags),
824 intel_get_flags_caching_string(rep->bo_info.flags));
826 bo_ttm->last_flags = rep->bo_info.flags;
828 /* Update the buffer offset */
829 if (rep->bo_info.offset != bo->offset) {
830 DBG("BO %s migrated: 0x%08x -> 0x%08x\n",
831 bo_ttm->name, bo->offset, rep->bo_info.offset);
832 bo->offset = rep->bo_info.offset;
838 dri_ttm_post_submit(dri_bo *batch_buf, dri_fence **last_fence)
840 dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)batch_buf->bufmgr;
843 intel_update_buffer_offsets (bufmgr_ttm);
845 if (bufmgr_ttm->bufmgr.debug)
846 dri_ttm_dump_validation_list(bufmgr_ttm);
848 for (i = 0; i < bufmgr_ttm->validate_count; i++) {
849 dri_bo *bo = bufmgr_ttm->validate_array[i].bo;
850 dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
852 /* Disconnect the buffer from the validate list */
853 bo_ttm->validate_index = -1;
854 dri_bo_unreference(bo);
855 bufmgr_ttm->validate_array[i].bo = NULL;
857 bufmgr_ttm->validate_count = 0;
861 * Initializes the TTM buffer manager, which uses the kernel to allocate, map,
862 * and manage map buffer objections.
864 * \param fd File descriptor of the opened DRM device.
865 * \param fence_type Driver-specific fence type used for fences with no flush.
866 * \param fence_type_flush Driver-specific fence type used for fences with a
870 intel_bufmgr_ttm_init(int fd, unsigned int fence_type,
871 unsigned int fence_type_flush, int batch_size)
873 dri_bufmgr_ttm *bufmgr_ttm;
875 bufmgr_ttm = calloc(1, sizeof(*bufmgr_ttm));
877 bufmgr_ttm->fence_type = fence_type;
878 bufmgr_ttm->fence_type_flush = fence_type_flush;
879 bufmgr_ttm->cached_reloc_buf = NULL;
880 bufmgr_ttm->cached_reloc_buf_data = NULL;
882 /* Let's go with one relocation per every 2 dwords (but round down a bit
883 * since a power of two will mean an extra page allocation for the reloc
886 * Every 4 was too few for the blender benchmark.
888 bufmgr_ttm->max_relocs = batch_size / sizeof(uint32_t) / 2 - 2;
890 bufmgr_ttm->bufmgr.bo_alloc = dri_ttm_alloc;
891 bufmgr_ttm->bufmgr.bo_alloc_static = dri_ttm_alloc_static;
892 bufmgr_ttm->bufmgr.bo_reference = dri_ttm_bo_reference;
893 bufmgr_ttm->bufmgr.bo_unreference = dri_ttm_bo_unreference;
894 bufmgr_ttm->bufmgr.bo_map = dri_ttm_bo_map;
895 bufmgr_ttm->bufmgr.bo_unmap = dri_ttm_bo_unmap;
896 bufmgr_ttm->bufmgr.fence_reference = dri_ttm_fence_reference;
897 bufmgr_ttm->bufmgr.fence_unreference = dri_ttm_fence_unreference;
898 bufmgr_ttm->bufmgr.fence_wait = dri_ttm_fence_wait;
899 bufmgr_ttm->bufmgr.destroy = dri_bufmgr_ttm_destroy;
900 bufmgr_ttm->bufmgr.emit_reloc = dri_ttm_emit_reloc;
901 bufmgr_ttm->bufmgr.process_relocs = dri_ttm_process_reloc;
902 bufmgr_ttm->bufmgr.post_submit = dri_ttm_post_submit;
903 bufmgr_ttm->bufmgr.debug = GL_FALSE;
905 return &bufmgr_ttm->bufmgr;