1 /**************************************************************************
3 * Copyright © 2007 Red Hat Inc.
4 * Copyright © 2007-2012 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>
42 #include <xf86atomic.h>
50 #include <sys/ioctl.h>
52 #include <sys/types.h>
57 #define ETIME ETIMEDOUT
59 #include "libdrm_macros.h"
60 #include "libdrm_lists.h"
61 #include "intel_bufmgr.h"
62 #include "intel_bufmgr_priv.h"
63 #include "intel_chipset.h"
64 #include "intel_aub.h"
77 #define memclear(s) memset(&s, 0, sizeof(s))
79 #define DBG(...) do { \
80 if (bufmgr_gem->bufmgr.debug) \
81 fprintf(stderr, __VA_ARGS__); \
84 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
86 typedef struct _drm_intel_bo_gem drm_intel_bo_gem;
88 struct drm_intel_gem_bo_bucket {
93 typedef struct _drm_intel_bufmgr_gem {
94 drm_intel_bufmgr bufmgr;
102 pthread_mutex_t lock;
104 struct drm_i915_gem_exec_object *exec_objects;
105 struct drm_i915_gem_exec_object2 *exec2_objects;
106 drm_intel_bo **exec_bos;
110 /** Array of lists of cached gem objects of power-of-two sizes */
111 struct drm_intel_gem_bo_bucket cache_bucket[14 * 4];
115 drmMMListHead managers;
118 drmMMListHead vma_cache;
119 int vma_count, vma_open, vma_max;
122 int available_fences;
125 unsigned int has_bsd : 1;
126 unsigned int has_blt : 1;
127 unsigned int has_relaxed_fencing : 1;
128 unsigned int has_llc : 1;
129 unsigned int has_wait_timeout : 1;
130 unsigned int bo_reuse : 1;
131 unsigned int no_exec : 1;
132 unsigned int has_vebox : 1;
143 } drm_intel_bufmgr_gem;
145 #define DRM_INTEL_RELOC_FENCE (1<<0)
147 typedef struct _drm_intel_reloc_target_info {
150 } drm_intel_reloc_target;
152 struct _drm_intel_bo_gem {
160 * Kenel-assigned global name for this object
162 * List contains both flink named and prime fd'd objects
164 unsigned int global_name;
165 drmMMListHead name_list;
168 * Index of the buffer within the validation list while preparing a
169 * batchbuffer execution.
174 * Current tiling mode
176 uint32_t tiling_mode;
177 uint32_t swizzle_mode;
178 unsigned long stride;
182 /** Array passed to the DRM containing relocation information. */
183 struct drm_i915_gem_relocation_entry *relocs;
185 * Array of info structs corresponding to relocs[i].target_handle etc
187 drm_intel_reloc_target *reloc_target_info;
188 /** Number of entries in relocs */
190 /** Mapped address for the buffer, saved across map/unmap cycles */
192 /** GTT virtual address for the buffer, saved across map/unmap cycles */
195 * Virtual address of the buffer allocated by user, used for userptr
200 drmMMListHead vma_list;
206 * Boolean of whether this BO and its children have been included in
207 * the current drm_intel_bufmgr_check_aperture_space() total.
209 bool included_in_check_aperture;
212 * Boolean of whether this buffer has been used as a relocation
213 * target and had its size accounted for, and thus can't have any
214 * further relocations added to it.
216 bool used_as_reloc_target;
219 * Boolean of whether we have encountered an error whilst building the relocation tree.
224 * Boolean of whether this buffer can be re-used
229 * Boolean of whether the GPU is definitely not accessing the buffer.
231 * This is only valid when reusable, since non-reusable
232 * buffers are those that have been shared wth other
233 * processes, so we don't know their state.
238 * Boolean of whether this buffer was allocated with userptr
243 * Size in bytes of this buffer and its relocation descendents.
245 * Used to avoid costly tree walking in
246 * drm_intel_bufmgr_check_aperture in the common case.
251 * Number of potential fence registers required by this buffer and its
254 int reloc_tree_fences;
256 /** Flags that we may need to do the SW_FINSIH ioctl on unmap. */
257 bool mapped_cpu_write;
261 drm_intel_aub_annotation *aub_annotations;
262 unsigned aub_annotation_count;
266 drm_intel_gem_estimate_batch_space(drm_intel_bo ** bo_array, int count);
269 drm_intel_gem_compute_batch_space(drm_intel_bo ** bo_array, int count);
272 drm_intel_gem_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
273 uint32_t * swizzle_mode);
276 drm_intel_gem_bo_set_tiling_internal(drm_intel_bo *bo,
277 uint32_t tiling_mode,
280 static void drm_intel_gem_bo_unreference_locked_timed(drm_intel_bo *bo,
283 static void drm_intel_gem_bo_unreference(drm_intel_bo *bo);
285 static void drm_intel_gem_bo_free(drm_intel_bo *bo);
288 drm_intel_gem_bo_tile_size(drm_intel_bufmgr_gem *bufmgr_gem, unsigned long size,
289 uint32_t *tiling_mode)
291 unsigned long min_size, max_size;
294 if (*tiling_mode == I915_TILING_NONE)
297 /* 965+ just need multiples of page size for tiling */
298 if (bufmgr_gem->gen >= 4)
299 return ROUND_UP_TO(size, 4096);
301 /* Older chips need powers of two, of at least 512k or 1M */
302 if (bufmgr_gem->gen == 3) {
303 min_size = 1024*1024;
304 max_size = 128*1024*1024;
307 max_size = 64*1024*1024;
310 if (size > max_size) {
311 *tiling_mode = I915_TILING_NONE;
315 /* Do we need to allocate every page for the fence? */
316 if (bufmgr_gem->has_relaxed_fencing)
317 return ROUND_UP_TO(size, 4096);
319 for (i = min_size; i < size; i <<= 1)
326 * Round a given pitch up to the minimum required for X tiling on a
327 * given chip. We use 512 as the minimum to allow for a later tiling
331 drm_intel_gem_bo_tile_pitch(drm_intel_bufmgr_gem *bufmgr_gem,
332 unsigned long pitch, uint32_t *tiling_mode)
334 unsigned long tile_width;
337 /* If untiled, then just align it so that we can do rendering
338 * to it with the 3D engine.
340 if (*tiling_mode == I915_TILING_NONE)
341 return ALIGN(pitch, 64);
343 if (*tiling_mode == I915_TILING_X
344 || (IS_915(bufmgr_gem->pci_device)
345 && *tiling_mode == I915_TILING_Y))
350 /* 965 is flexible */
351 if (bufmgr_gem->gen >= 4)
352 return ROUND_UP_TO(pitch, tile_width);
354 /* The older hardware has a maximum pitch of 8192 with tiled
355 * surfaces, so fallback to untiled if it's too large.
358 *tiling_mode = I915_TILING_NONE;
359 return ALIGN(pitch, 64);
362 /* Pre-965 needs power of two tile width */
363 for (i = tile_width; i < pitch; i <<= 1)
369 static struct drm_intel_gem_bo_bucket *
370 drm_intel_gem_bo_bucket_for_size(drm_intel_bufmgr_gem *bufmgr_gem,
375 for (i = 0; i < bufmgr_gem->num_buckets; i++) {
376 struct drm_intel_gem_bo_bucket *bucket =
377 &bufmgr_gem->cache_bucket[i];
378 if (bucket->size >= size) {
387 drm_intel_gem_dump_validation_list(drm_intel_bufmgr_gem *bufmgr_gem)
391 for (i = 0; i < bufmgr_gem->exec_count; i++) {
392 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
393 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
395 if (bo_gem->relocs == NULL) {
396 DBG("%2d: %d (%s)\n", i, bo_gem->gem_handle,
401 for (j = 0; j < bo_gem->reloc_count; j++) {
402 drm_intel_bo *target_bo = bo_gem->reloc_target_info[j].bo;
403 drm_intel_bo_gem *target_gem =
404 (drm_intel_bo_gem *) target_bo;
406 DBG("%2d: %d (%s)@0x%08llx -> "
407 "%d (%s)@0x%08lx + 0x%08x\n",
409 bo_gem->gem_handle, bo_gem->name,
410 (unsigned long long)bo_gem->relocs[j].offset,
411 target_gem->gem_handle,
414 bo_gem->relocs[j].delta);
420 drm_intel_gem_bo_reference(drm_intel_bo *bo)
422 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
424 atomic_inc(&bo_gem->refcount);
428 * Adds the given buffer to the list of buffers to be validated (moved into the
429 * appropriate memory type) with the next batch submission.
431 * If a buffer is validated multiple times in a batch submission, it ends up
432 * with the intersection of the memory type flags and the union of the
436 drm_intel_add_validate_buffer(drm_intel_bo *bo)
438 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
439 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
442 if (bo_gem->validate_index != -1)
445 /* Extend the array of validation entries as necessary. */
446 if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
447 int new_size = bufmgr_gem->exec_size * 2;
452 bufmgr_gem->exec_objects =
453 realloc(bufmgr_gem->exec_objects,
454 sizeof(*bufmgr_gem->exec_objects) * new_size);
455 bufmgr_gem->exec_bos =
456 realloc(bufmgr_gem->exec_bos,
457 sizeof(*bufmgr_gem->exec_bos) * new_size);
458 bufmgr_gem->exec_size = new_size;
461 index = bufmgr_gem->exec_count;
462 bo_gem->validate_index = index;
463 /* Fill in array entry */
464 bufmgr_gem->exec_objects[index].handle = bo_gem->gem_handle;
465 bufmgr_gem->exec_objects[index].relocation_count = bo_gem->reloc_count;
466 bufmgr_gem->exec_objects[index].relocs_ptr = (uintptr_t) bo_gem->relocs;
467 bufmgr_gem->exec_objects[index].alignment = 0;
468 bufmgr_gem->exec_objects[index].offset = 0;
469 bufmgr_gem->exec_bos[index] = bo;
470 bufmgr_gem->exec_count++;
474 drm_intel_add_validate_buffer2(drm_intel_bo *bo, int need_fence)
476 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
477 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
480 if (bo_gem->validate_index != -1) {
482 bufmgr_gem->exec2_objects[bo_gem->validate_index].flags |=
483 EXEC_OBJECT_NEEDS_FENCE;
487 /* Extend the array of validation entries as necessary. */
488 if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
489 int new_size = bufmgr_gem->exec_size * 2;
494 bufmgr_gem->exec2_objects =
495 realloc(bufmgr_gem->exec2_objects,
496 sizeof(*bufmgr_gem->exec2_objects) * new_size);
497 bufmgr_gem->exec_bos =
498 realloc(bufmgr_gem->exec_bos,
499 sizeof(*bufmgr_gem->exec_bos) * new_size);
500 bufmgr_gem->exec_size = new_size;
503 index = bufmgr_gem->exec_count;
504 bo_gem->validate_index = index;
505 /* Fill in array entry */
506 bufmgr_gem->exec2_objects[index].handle = bo_gem->gem_handle;
507 bufmgr_gem->exec2_objects[index].relocation_count = bo_gem->reloc_count;
508 bufmgr_gem->exec2_objects[index].relocs_ptr = (uintptr_t)bo_gem->relocs;
509 bufmgr_gem->exec2_objects[index].alignment = 0;
510 bufmgr_gem->exec2_objects[index].offset = 0;
511 bufmgr_gem->exec_bos[index] = bo;
512 bufmgr_gem->exec2_objects[index].flags = 0;
513 bufmgr_gem->exec2_objects[index].rsvd1 = 0;
514 bufmgr_gem->exec2_objects[index].rsvd2 = 0;
516 bufmgr_gem->exec2_objects[index].flags |=
517 EXEC_OBJECT_NEEDS_FENCE;
519 bufmgr_gem->exec_count++;
522 #define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \
526 drm_intel_bo_gem_set_in_aperture_size(drm_intel_bufmgr_gem *bufmgr_gem,
527 drm_intel_bo_gem *bo_gem)
531 assert(!bo_gem->used_as_reloc_target);
533 /* The older chipsets are far-less flexible in terms of tiling,
534 * and require tiled buffer to be size aligned in the aperture.
535 * This means that in the worst possible case we will need a hole
536 * twice as large as the object in order for it to fit into the
537 * aperture. Optimal packing is for wimps.
539 size = bo_gem->bo.size;
540 if (bufmgr_gem->gen < 4 && bo_gem->tiling_mode != I915_TILING_NONE) {
543 if (bufmgr_gem->has_relaxed_fencing) {
544 if (bufmgr_gem->gen == 3)
545 min_size = 1024*1024;
549 while (min_size < size)
554 /* Account for worst-case alignment. */
558 bo_gem->reloc_tree_size = size;
562 drm_intel_setup_reloc_list(drm_intel_bo *bo)
564 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
565 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
566 unsigned int max_relocs = bufmgr_gem->max_relocs;
568 if (bo->size / 4 < max_relocs)
569 max_relocs = bo->size / 4;
571 bo_gem->relocs = malloc(max_relocs *
572 sizeof(struct drm_i915_gem_relocation_entry));
573 bo_gem->reloc_target_info = malloc(max_relocs *
574 sizeof(drm_intel_reloc_target));
575 if (bo_gem->relocs == NULL || bo_gem->reloc_target_info == NULL) {
576 bo_gem->has_error = true;
578 free (bo_gem->relocs);
579 bo_gem->relocs = NULL;
581 free (bo_gem->reloc_target_info);
582 bo_gem->reloc_target_info = NULL;
591 drm_intel_gem_bo_busy(drm_intel_bo *bo)
593 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
594 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
595 struct drm_i915_gem_busy busy;
598 if (bo_gem->reusable && bo_gem->idle)
602 busy.handle = bo_gem->gem_handle;
604 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
606 bo_gem->idle = !busy.busy;
611 return (ret == 0 && busy.busy);
615 drm_intel_gem_bo_madvise_internal(drm_intel_bufmgr_gem *bufmgr_gem,
616 drm_intel_bo_gem *bo_gem, int state)
618 struct drm_i915_gem_madvise madv;
621 madv.handle = bo_gem->gem_handle;
624 drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv);
626 return madv.retained;
630 drm_intel_gem_bo_madvise(drm_intel_bo *bo, int madv)
632 return drm_intel_gem_bo_madvise_internal
633 ((drm_intel_bufmgr_gem *) bo->bufmgr,
634 (drm_intel_bo_gem *) bo,
638 /* drop the oldest entries that have been purged by the kernel */
640 drm_intel_gem_bo_cache_purge_bucket(drm_intel_bufmgr_gem *bufmgr_gem,
641 struct drm_intel_gem_bo_bucket *bucket)
643 while (!DRMLISTEMPTY(&bucket->head)) {
644 drm_intel_bo_gem *bo_gem;
646 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
647 bucket->head.next, head);
648 if (drm_intel_gem_bo_madvise_internal
649 (bufmgr_gem, bo_gem, I915_MADV_DONTNEED))
652 DRMLISTDEL(&bo_gem->head);
653 drm_intel_gem_bo_free(&bo_gem->bo);
657 static drm_intel_bo *
658 drm_intel_gem_bo_alloc_internal(drm_intel_bufmgr *bufmgr,
662 uint32_t tiling_mode,
663 unsigned long stride)
665 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
666 drm_intel_bo_gem *bo_gem;
667 unsigned int page_size = getpagesize();
669 struct drm_intel_gem_bo_bucket *bucket;
670 bool alloc_from_cache;
671 unsigned long bo_size;
672 bool for_render = false;
674 if (flags & BO_ALLOC_FOR_RENDER)
677 /* Round the allocated size up to a power of two number of pages. */
678 bucket = drm_intel_gem_bo_bucket_for_size(bufmgr_gem, size);
680 /* If we don't have caching at this size, don't actually round the
683 if (bucket == NULL) {
685 if (bo_size < page_size)
688 bo_size = bucket->size;
691 pthread_mutex_lock(&bufmgr_gem->lock);
692 /* Get a buffer out of the cache if available */
694 alloc_from_cache = false;
695 if (bucket != NULL && !DRMLISTEMPTY(&bucket->head)) {
697 /* Allocate new render-target BOs from the tail (MRU)
698 * of the list, as it will likely be hot in the GPU
699 * cache and in the aperture for us.
701 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
702 bucket->head.prev, head);
703 DRMLISTDEL(&bo_gem->head);
704 alloc_from_cache = true;
706 /* For non-render-target BOs (where we're probably
707 * going to map it first thing in order to fill it
708 * with data), check if the last BO in the cache is
709 * unbusy, and only reuse in that case. Otherwise,
710 * allocating a new buffer is probably faster than
711 * waiting for the GPU to finish.
713 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
714 bucket->head.next, head);
715 if (!drm_intel_gem_bo_busy(&bo_gem->bo)) {
716 alloc_from_cache = true;
717 DRMLISTDEL(&bo_gem->head);
721 if (alloc_from_cache) {
722 if (!drm_intel_gem_bo_madvise_internal
723 (bufmgr_gem, bo_gem, I915_MADV_WILLNEED)) {
724 drm_intel_gem_bo_free(&bo_gem->bo);
725 drm_intel_gem_bo_cache_purge_bucket(bufmgr_gem,
730 if (drm_intel_gem_bo_set_tiling_internal(&bo_gem->bo,
733 drm_intel_gem_bo_free(&bo_gem->bo);
738 pthread_mutex_unlock(&bufmgr_gem->lock);
740 if (!alloc_from_cache) {
741 struct drm_i915_gem_create create;
743 bo_gem = calloc(1, sizeof(*bo_gem));
747 bo_gem->bo.size = bo_size;
750 create.size = bo_size;
752 ret = drmIoctl(bufmgr_gem->fd,
753 DRM_IOCTL_I915_GEM_CREATE,
755 bo_gem->gem_handle = create.handle;
756 bo_gem->bo.handle = bo_gem->gem_handle;
761 bo_gem->bo.bufmgr = bufmgr;
763 bo_gem->tiling_mode = I915_TILING_NONE;
764 bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
767 /* drm_intel_gem_bo_free calls DRMLISTDEL() for an uninitialized
768 list (vma_list), so better set the list head here */
769 DRMINITLISTHEAD(&bo_gem->name_list);
770 DRMINITLISTHEAD(&bo_gem->vma_list);
771 if (drm_intel_gem_bo_set_tiling_internal(&bo_gem->bo,
774 drm_intel_gem_bo_free(&bo_gem->bo);
780 atomic_set(&bo_gem->refcount, 1);
781 bo_gem->validate_index = -1;
782 bo_gem->reloc_tree_fences = 0;
783 bo_gem->used_as_reloc_target = false;
784 bo_gem->has_error = false;
785 bo_gem->reusable = true;
786 bo_gem->aub_annotations = NULL;
787 bo_gem->aub_annotation_count = 0;
789 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
791 DBG("bo_create: buf %d (%s) %ldb\n",
792 bo_gem->gem_handle, bo_gem->name, size);
797 static drm_intel_bo *
798 drm_intel_gem_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
801 unsigned int alignment)
803 return drm_intel_gem_bo_alloc_internal(bufmgr, name, size,
805 I915_TILING_NONE, 0);
808 static drm_intel_bo *
809 drm_intel_gem_bo_alloc(drm_intel_bufmgr *bufmgr,
812 unsigned int alignment)
814 return drm_intel_gem_bo_alloc_internal(bufmgr, name, size, 0,
815 I915_TILING_NONE, 0);
818 static drm_intel_bo *
819 drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
820 int x, int y, int cpp, uint32_t *tiling_mode,
821 unsigned long *pitch, unsigned long flags)
823 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
824 unsigned long size, stride;
828 unsigned long aligned_y, height_alignment;
830 tiling = *tiling_mode;
832 /* If we're tiled, our allocations are in 8 or 32-row blocks,
833 * so failure to align our height means that we won't allocate
836 * If we're untiled, we still have to align to 2 rows high
837 * because the data port accesses 2x2 blocks even if the
838 * bottom row isn't to be rendered, so failure to align means
839 * we could walk off the end of the GTT and fault. This is
840 * documented on 965, and may be the case on older chipsets
841 * too so we try to be careful.
844 height_alignment = 2;
846 if ((bufmgr_gem->gen == 2) && tiling != I915_TILING_NONE)
847 height_alignment = 16;
848 else if (tiling == I915_TILING_X
849 || (IS_915(bufmgr_gem->pci_device)
850 && tiling == I915_TILING_Y))
851 height_alignment = 8;
852 else if (tiling == I915_TILING_Y)
853 height_alignment = 32;
854 aligned_y = ALIGN(y, height_alignment);
857 stride = drm_intel_gem_bo_tile_pitch(bufmgr_gem, stride, tiling_mode);
858 size = stride * aligned_y;
859 size = drm_intel_gem_bo_tile_size(bufmgr_gem, size, tiling_mode);
860 } while (*tiling_mode != tiling);
863 if (tiling == I915_TILING_NONE)
866 return drm_intel_gem_bo_alloc_internal(bufmgr, name, size, flags,
870 static drm_intel_bo *
871 drm_intel_gem_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
874 uint32_t tiling_mode,
879 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
880 drm_intel_bo_gem *bo_gem;
882 struct drm_i915_gem_userptr userptr;
884 /* Tiling with userptr surfaces is not supported
885 * on all hardware so refuse it for time being.
887 if (tiling_mode != I915_TILING_NONE)
890 bo_gem = calloc(1, sizeof(*bo_gem));
894 bo_gem->bo.size = size;
897 userptr.user_ptr = (__u64)((unsigned long)addr);
898 userptr.user_size = size;
899 userptr.flags = flags;
901 ret = drmIoctl(bufmgr_gem->fd,
902 DRM_IOCTL_I915_GEM_USERPTR,
905 DBG("bo_create_userptr: "
906 "ioctl failed with user ptr %p size 0x%lx, "
907 "user flags 0x%lx\n", addr, size, flags);
912 bo_gem->gem_handle = userptr.handle;
913 bo_gem->bo.handle = bo_gem->gem_handle;
914 bo_gem->bo.bufmgr = bufmgr;
915 bo_gem->is_userptr = true;
916 bo_gem->bo.virtual = addr;
917 /* Save the address provided by user */
918 bo_gem->user_virtual = addr;
919 bo_gem->tiling_mode = I915_TILING_NONE;
920 bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
923 DRMINITLISTHEAD(&bo_gem->name_list);
924 DRMINITLISTHEAD(&bo_gem->vma_list);
927 atomic_set(&bo_gem->refcount, 1);
928 bo_gem->validate_index = -1;
929 bo_gem->reloc_tree_fences = 0;
930 bo_gem->used_as_reloc_target = false;
931 bo_gem->has_error = false;
932 bo_gem->reusable = false;
934 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
936 DBG("bo_create_userptr: "
937 "ptr %p buf %d (%s) size %ldb, stride 0x%x, tile mode %d\n",
938 addr, bo_gem->gem_handle, bo_gem->name,
939 size, stride, tiling_mode);
945 has_userptr(drm_intel_bufmgr_gem *bufmgr_gem)
950 struct drm_i915_gem_userptr userptr;
952 pgsz = sysconf(_SC_PAGESIZE);
955 ret = posix_memalign(&ptr, pgsz, pgsz);
957 DBG("Failed to get a page (%ld) for userptr detection!\n",
963 userptr.user_ptr = (__u64)(unsigned long)ptr;
964 userptr.user_size = pgsz;
967 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
969 if (errno == ENODEV && userptr.flags == 0) {
970 userptr.flags = I915_USERPTR_UNSYNCHRONIZED;
977 /* We don't release the userptr bo here as we want to keep the
978 * kernel mm tracking alive for our lifetime. The first time we
979 * create a userptr object the kernel has to install a mmu_notifer
980 * which is a heavyweight operation (e.g. it requires taking all
981 * mm_locks and stop_machine()).
984 bufmgr_gem->userptr_active.ptr = ptr;
985 bufmgr_gem->userptr_active.handle = userptr.handle;
990 static drm_intel_bo *
991 check_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
994 uint32_t tiling_mode,
999 if (has_userptr((drm_intel_bufmgr_gem *)bufmgr))
1000 bufmgr->bo_alloc_userptr = drm_intel_gem_bo_alloc_userptr;
1002 bufmgr->bo_alloc_userptr = NULL;
1004 return drm_intel_bo_alloc_userptr(bufmgr, name, addr,
1005 tiling_mode, stride, size, flags);
1009 * Returns a drm_intel_bo wrapping the given buffer object handle.
1011 * This can be used when one application needs to pass a buffer object
1015 drm_intel_bo_gem_create_from_name(drm_intel_bufmgr *bufmgr,
1017 unsigned int handle)
1019 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
1020 drm_intel_bo_gem *bo_gem;
1022 struct drm_gem_open open_arg;
1023 struct drm_i915_gem_get_tiling get_tiling;
1024 drmMMListHead *list;
1026 /* At the moment most applications only have a few named bo.
1027 * For instance, in a DRI client only the render buffers passed
1028 * between X and the client are named. And since X returns the
1029 * alternating names for the front/back buffer a linear search
1030 * provides a sufficiently fast match.
1032 pthread_mutex_lock(&bufmgr_gem->lock);
1033 for (list = bufmgr_gem->named.next;
1034 list != &bufmgr_gem->named;
1035 list = list->next) {
1036 bo_gem = DRMLISTENTRY(drm_intel_bo_gem, list, name_list);
1037 if (bo_gem->global_name == handle) {
1038 drm_intel_gem_bo_reference(&bo_gem->bo);
1039 pthread_mutex_unlock(&bufmgr_gem->lock);
1045 open_arg.name = handle;
1046 ret = drmIoctl(bufmgr_gem->fd,
1050 DBG("Couldn't reference %s handle 0x%08x: %s\n",
1051 name, handle, strerror(errno));
1052 pthread_mutex_unlock(&bufmgr_gem->lock);
1055 /* Now see if someone has used a prime handle to get this
1056 * object from the kernel before by looking through the list
1057 * again for a matching gem_handle
1059 for (list = bufmgr_gem->named.next;
1060 list != &bufmgr_gem->named;
1061 list = list->next) {
1062 bo_gem = DRMLISTENTRY(drm_intel_bo_gem, list, name_list);
1063 if (bo_gem->gem_handle == open_arg.handle) {
1064 drm_intel_gem_bo_reference(&bo_gem->bo);
1065 pthread_mutex_unlock(&bufmgr_gem->lock);
1070 bo_gem = calloc(1, sizeof(*bo_gem));
1072 pthread_mutex_unlock(&bufmgr_gem->lock);
1076 bo_gem->bo.size = open_arg.size;
1077 bo_gem->bo.offset = 0;
1078 bo_gem->bo.offset64 = 0;
1079 bo_gem->bo.virtual = NULL;
1080 bo_gem->bo.bufmgr = bufmgr;
1081 bo_gem->name = name;
1082 atomic_set(&bo_gem->refcount, 1);
1083 bo_gem->validate_index = -1;
1084 bo_gem->gem_handle = open_arg.handle;
1085 bo_gem->bo.handle = open_arg.handle;
1086 bo_gem->global_name = handle;
1087 bo_gem->reusable = false;
1089 memclear(get_tiling);
1090 get_tiling.handle = bo_gem->gem_handle;
1091 ret = drmIoctl(bufmgr_gem->fd,
1092 DRM_IOCTL_I915_GEM_GET_TILING,
1095 drm_intel_gem_bo_unreference(&bo_gem->bo);
1096 pthread_mutex_unlock(&bufmgr_gem->lock);
1099 bo_gem->tiling_mode = get_tiling.tiling_mode;
1100 bo_gem->swizzle_mode = get_tiling.swizzle_mode;
1101 /* XXX stride is unknown */
1102 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
1104 DRMINITLISTHEAD(&bo_gem->vma_list);
1105 DRMLISTADDTAIL(&bo_gem->name_list, &bufmgr_gem->named);
1106 pthread_mutex_unlock(&bufmgr_gem->lock);
1107 DBG("bo_create_from_handle: %d (%s)\n", handle, bo_gem->name);
1113 drm_intel_gem_bo_free(drm_intel_bo *bo)
1115 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1116 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1117 struct drm_gem_close close;
1120 DRMLISTDEL(&bo_gem->vma_list);
1121 if (bo_gem->mem_virtual) {
1122 VG(VALGRIND_FREELIKE_BLOCK(bo_gem->mem_virtual, 0));
1123 drm_munmap(bo_gem->mem_virtual, bo_gem->bo.size);
1124 bufmgr_gem->vma_count--;
1126 if (bo_gem->gtt_virtual) {
1127 drm_munmap(bo_gem->gtt_virtual, bo_gem->bo.size);
1128 bufmgr_gem->vma_count--;
1131 /* Close this object */
1133 close.handle = bo_gem->gem_handle;
1134 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close);
1136 DBG("DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
1137 bo_gem->gem_handle, bo_gem->name, strerror(errno));
1139 free(bo_gem->aub_annotations);
1144 drm_intel_gem_bo_mark_mmaps_incoherent(drm_intel_bo *bo)
1147 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1149 if (bo_gem->mem_virtual)
1150 VALGRIND_MAKE_MEM_NOACCESS(bo_gem->mem_virtual, bo->size);
1152 if (bo_gem->gtt_virtual)
1153 VALGRIND_MAKE_MEM_NOACCESS(bo_gem->gtt_virtual, bo->size);
1157 /** Frees all cached buffers significantly older than @time. */
1159 drm_intel_gem_cleanup_bo_cache(drm_intel_bufmgr_gem *bufmgr_gem, time_t time)
1163 if (bufmgr_gem->time == time)
1166 for (i = 0; i < bufmgr_gem->num_buckets; i++) {
1167 struct drm_intel_gem_bo_bucket *bucket =
1168 &bufmgr_gem->cache_bucket[i];
1170 while (!DRMLISTEMPTY(&bucket->head)) {
1171 drm_intel_bo_gem *bo_gem;
1173 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
1174 bucket->head.next, head);
1175 if (time - bo_gem->free_time <= 1)
1178 DRMLISTDEL(&bo_gem->head);
1180 drm_intel_gem_bo_free(&bo_gem->bo);
1184 bufmgr_gem->time = time;
1187 static void drm_intel_gem_bo_purge_vma_cache(drm_intel_bufmgr_gem *bufmgr_gem)
1191 DBG("%s: cached=%d, open=%d, limit=%d\n", __FUNCTION__,
1192 bufmgr_gem->vma_count, bufmgr_gem->vma_open, bufmgr_gem->vma_max);
1194 if (bufmgr_gem->vma_max < 0)
1197 /* We may need to evict a few entries in order to create new mmaps */
1198 limit = bufmgr_gem->vma_max - 2*bufmgr_gem->vma_open;
1202 while (bufmgr_gem->vma_count > limit) {
1203 drm_intel_bo_gem *bo_gem;
1205 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
1206 bufmgr_gem->vma_cache.next,
1208 assert(bo_gem->map_count == 0);
1209 DRMLISTDELINIT(&bo_gem->vma_list);
1211 if (bo_gem->mem_virtual) {
1212 drm_munmap(bo_gem->mem_virtual, bo_gem->bo.size);
1213 bo_gem->mem_virtual = NULL;
1214 bufmgr_gem->vma_count--;
1216 if (bo_gem->gtt_virtual) {
1217 drm_munmap(bo_gem->gtt_virtual, bo_gem->bo.size);
1218 bo_gem->gtt_virtual = NULL;
1219 bufmgr_gem->vma_count--;
1224 static void drm_intel_gem_bo_close_vma(drm_intel_bufmgr_gem *bufmgr_gem,
1225 drm_intel_bo_gem *bo_gem)
1227 bufmgr_gem->vma_open--;
1228 DRMLISTADDTAIL(&bo_gem->vma_list, &bufmgr_gem->vma_cache);
1229 if (bo_gem->mem_virtual)
1230 bufmgr_gem->vma_count++;
1231 if (bo_gem->gtt_virtual)
1232 bufmgr_gem->vma_count++;
1233 drm_intel_gem_bo_purge_vma_cache(bufmgr_gem);
1236 static void drm_intel_gem_bo_open_vma(drm_intel_bufmgr_gem *bufmgr_gem,
1237 drm_intel_bo_gem *bo_gem)
1239 bufmgr_gem->vma_open++;
1240 DRMLISTDEL(&bo_gem->vma_list);
1241 if (bo_gem->mem_virtual)
1242 bufmgr_gem->vma_count--;
1243 if (bo_gem->gtt_virtual)
1244 bufmgr_gem->vma_count--;
1245 drm_intel_gem_bo_purge_vma_cache(bufmgr_gem);
1249 drm_intel_gem_bo_unreference_final(drm_intel_bo *bo, time_t time)
1251 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1252 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1253 struct drm_intel_gem_bo_bucket *bucket;
1256 /* Unreference all the target buffers */
1257 for (i = 0; i < bo_gem->reloc_count; i++) {
1258 if (bo_gem->reloc_target_info[i].bo != bo) {
1259 drm_intel_gem_bo_unreference_locked_timed(bo_gem->
1260 reloc_target_info[i].bo,
1264 bo_gem->reloc_count = 0;
1265 bo_gem->used_as_reloc_target = false;
1267 DBG("bo_unreference final: %d (%s)\n",
1268 bo_gem->gem_handle, bo_gem->name);
1270 /* release memory associated with this object */
1271 if (bo_gem->reloc_target_info) {
1272 free(bo_gem->reloc_target_info);
1273 bo_gem->reloc_target_info = NULL;
1275 if (bo_gem->relocs) {
1276 free(bo_gem->relocs);
1277 bo_gem->relocs = NULL;
1280 /* Clear any left-over mappings */
1281 if (bo_gem->map_count) {
1282 DBG("bo freed with non-zero map-count %d\n", bo_gem->map_count);
1283 bo_gem->map_count = 0;
1284 drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
1285 drm_intel_gem_bo_mark_mmaps_incoherent(bo);
1288 DRMLISTDEL(&bo_gem->name_list);
1290 bucket = drm_intel_gem_bo_bucket_for_size(bufmgr_gem, bo->size);
1291 /* Put the buffer into our internal cache for reuse if we can. */
1292 if (bufmgr_gem->bo_reuse && bo_gem->reusable && bucket != NULL &&
1293 drm_intel_gem_bo_madvise_internal(bufmgr_gem, bo_gem,
1294 I915_MADV_DONTNEED)) {
1295 bo_gem->free_time = time;
1297 bo_gem->name = NULL;
1298 bo_gem->validate_index = -1;
1300 DRMLISTADDTAIL(&bo_gem->head, &bucket->head);
1302 drm_intel_gem_bo_free(bo);
1306 static void drm_intel_gem_bo_unreference_locked_timed(drm_intel_bo *bo,
1309 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1311 assert(atomic_read(&bo_gem->refcount) > 0);
1312 if (atomic_dec_and_test(&bo_gem->refcount))
1313 drm_intel_gem_bo_unreference_final(bo, time);
1316 static void drm_intel_gem_bo_unreference(drm_intel_bo *bo)
1318 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1320 assert(atomic_read(&bo_gem->refcount) > 0);
1322 if (atomic_add_unless(&bo_gem->refcount, -1, 1)) {
1323 drm_intel_bufmgr_gem *bufmgr_gem =
1324 (drm_intel_bufmgr_gem *) bo->bufmgr;
1325 struct timespec time;
1327 clock_gettime(CLOCK_MONOTONIC, &time);
1329 pthread_mutex_lock(&bufmgr_gem->lock);
1331 if (atomic_dec_and_test(&bo_gem->refcount)) {
1332 drm_intel_gem_bo_unreference_final(bo, time.tv_sec);
1333 drm_intel_gem_cleanup_bo_cache(bufmgr_gem, time.tv_sec);
1336 pthread_mutex_unlock(&bufmgr_gem->lock);
1340 static int drm_intel_gem_bo_map(drm_intel_bo *bo, int write_enable)
1342 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1343 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1344 struct drm_i915_gem_set_domain set_domain;
1347 if (bo_gem->is_userptr) {
1348 /* Return the same user ptr */
1349 bo->virtual = bo_gem->user_virtual;
1353 pthread_mutex_lock(&bufmgr_gem->lock);
1355 if (bo_gem->map_count++ == 0)
1356 drm_intel_gem_bo_open_vma(bufmgr_gem, bo_gem);
1358 if (!bo_gem->mem_virtual) {
1359 struct drm_i915_gem_mmap mmap_arg;
1361 DBG("bo_map: %d (%s), map_count=%d\n",
1362 bo_gem->gem_handle, bo_gem->name, bo_gem->map_count);
1365 mmap_arg.handle = bo_gem->gem_handle;
1366 mmap_arg.size = bo->size;
1367 ret = drmIoctl(bufmgr_gem->fd,
1368 DRM_IOCTL_I915_GEM_MMAP,
1372 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
1373 __FILE__, __LINE__, bo_gem->gem_handle,
1374 bo_gem->name, strerror(errno));
1375 if (--bo_gem->map_count == 0)
1376 drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
1377 pthread_mutex_unlock(&bufmgr_gem->lock);
1380 VG(VALGRIND_MALLOCLIKE_BLOCK(mmap_arg.addr_ptr, mmap_arg.size, 0, 1));
1381 bo_gem->mem_virtual = (void *)(uintptr_t) mmap_arg.addr_ptr;
1383 DBG("bo_map: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name,
1384 bo_gem->mem_virtual);
1385 bo->virtual = bo_gem->mem_virtual;
1387 memclear(set_domain);
1388 set_domain.handle = bo_gem->gem_handle;
1389 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
1391 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
1393 set_domain.write_domain = 0;
1394 ret = drmIoctl(bufmgr_gem->fd,
1395 DRM_IOCTL_I915_GEM_SET_DOMAIN,
1398 DBG("%s:%d: Error setting to CPU domain %d: %s\n",
1399 __FILE__, __LINE__, bo_gem->gem_handle,
1404 bo_gem->mapped_cpu_write = true;
1406 drm_intel_gem_bo_mark_mmaps_incoherent(bo);
1407 VG(VALGRIND_MAKE_MEM_DEFINED(bo_gem->mem_virtual, bo->size));
1408 pthread_mutex_unlock(&bufmgr_gem->lock);
1414 map_gtt(drm_intel_bo *bo)
1416 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1417 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1420 if (bo_gem->is_userptr)
1423 if (bo_gem->map_count++ == 0)
1424 drm_intel_gem_bo_open_vma(bufmgr_gem, bo_gem);
1426 /* Get a mapping of the buffer if we haven't before. */
1427 if (bo_gem->gtt_virtual == NULL) {
1428 struct drm_i915_gem_mmap_gtt mmap_arg;
1430 DBG("bo_map_gtt: mmap %d (%s), map_count=%d\n",
1431 bo_gem->gem_handle, bo_gem->name, bo_gem->map_count);
1434 mmap_arg.handle = bo_gem->gem_handle;
1436 /* Get the fake offset back... */
1437 ret = drmIoctl(bufmgr_gem->fd,
1438 DRM_IOCTL_I915_GEM_MMAP_GTT,
1442 DBG("%s:%d: Error preparing buffer map %d (%s): %s .\n",
1444 bo_gem->gem_handle, bo_gem->name,
1446 if (--bo_gem->map_count == 0)
1447 drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
1452 bo_gem->gtt_virtual = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE,
1453 MAP_SHARED, bufmgr_gem->fd,
1455 if (bo_gem->gtt_virtual == MAP_FAILED) {
1456 bo_gem->gtt_virtual = NULL;
1458 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
1460 bo_gem->gem_handle, bo_gem->name,
1462 if (--bo_gem->map_count == 0)
1463 drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
1468 bo->virtual = bo_gem->gtt_virtual;
1470 DBG("bo_map_gtt: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name,
1471 bo_gem->gtt_virtual);
1477 drm_intel_gem_bo_map_gtt(drm_intel_bo *bo)
1479 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1480 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1481 struct drm_i915_gem_set_domain set_domain;
1484 pthread_mutex_lock(&bufmgr_gem->lock);
1488 pthread_mutex_unlock(&bufmgr_gem->lock);
1492 /* Now move it to the GTT domain so that the GPU and CPU
1493 * caches are flushed and the GPU isn't actively using the
1496 * The pagefault handler does this domain change for us when
1497 * it has unbound the BO from the GTT, but it's up to us to
1498 * tell it when we're about to use things if we had done
1499 * rendering and it still happens to be bound to the GTT.
1501 memclear(set_domain);
1502 set_domain.handle = bo_gem->gem_handle;
1503 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
1504 set_domain.write_domain = I915_GEM_DOMAIN_GTT;
1505 ret = drmIoctl(bufmgr_gem->fd,
1506 DRM_IOCTL_I915_GEM_SET_DOMAIN,
1509 DBG("%s:%d: Error setting domain %d: %s\n",
1510 __FILE__, __LINE__, bo_gem->gem_handle,
1514 drm_intel_gem_bo_mark_mmaps_incoherent(bo);
1515 VG(VALGRIND_MAKE_MEM_DEFINED(bo_gem->gtt_virtual, bo->size));
1516 pthread_mutex_unlock(&bufmgr_gem->lock);
1522 * Performs a mapping of the buffer object like the normal GTT
1523 * mapping, but avoids waiting for the GPU to be done reading from or
1524 * rendering to the buffer.
1526 * This is used in the implementation of GL_ARB_map_buffer_range: The
1527 * user asks to create a buffer, then does a mapping, fills some
1528 * space, runs a drawing command, then asks to map it again without
1529 * synchronizing because it guarantees that it won't write over the
1530 * data that the GPU is busy using (or, more specifically, that if it
1531 * does write over the data, it acknowledges that rendering is
1536 drm_intel_gem_bo_map_unsynchronized(drm_intel_bo *bo)
1538 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1539 #ifdef HAVE_VALGRIND
1540 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1544 /* If the CPU cache isn't coherent with the GTT, then use a
1545 * regular synchronized mapping. The problem is that we don't
1546 * track where the buffer was last used on the CPU side in
1547 * terms of drm_intel_bo_map vs drm_intel_gem_bo_map_gtt, so
1548 * we would potentially corrupt the buffer even when the user
1549 * does reasonable things.
1551 if (!bufmgr_gem->has_llc)
1552 return drm_intel_gem_bo_map_gtt(bo);
1554 pthread_mutex_lock(&bufmgr_gem->lock);
1558 drm_intel_gem_bo_mark_mmaps_incoherent(bo);
1559 VG(VALGRIND_MAKE_MEM_DEFINED(bo_gem->gtt_virtual, bo->size));
1562 pthread_mutex_unlock(&bufmgr_gem->lock);
1567 static int drm_intel_gem_bo_unmap(drm_intel_bo *bo)
1569 drm_intel_bufmgr_gem *bufmgr_gem;
1570 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1576 if (bo_gem->is_userptr)
1579 bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1581 pthread_mutex_lock(&bufmgr_gem->lock);
1583 if (bo_gem->map_count <= 0) {
1584 DBG("attempted to unmap an unmapped bo\n");
1585 pthread_mutex_unlock(&bufmgr_gem->lock);
1586 /* Preserve the old behaviour of just treating this as a
1587 * no-op rather than reporting the error.
1592 if (bo_gem->mapped_cpu_write) {
1593 struct drm_i915_gem_sw_finish sw_finish;
1595 /* Cause a flush to happen if the buffer's pinned for
1596 * scanout, so the results show up in a timely manner.
1597 * Unlike GTT set domains, this only does work if the
1598 * buffer should be scanout-related.
1600 memclear(sw_finish);
1601 sw_finish.handle = bo_gem->gem_handle;
1602 ret = drmIoctl(bufmgr_gem->fd,
1603 DRM_IOCTL_I915_GEM_SW_FINISH,
1605 ret = ret == -1 ? -errno : 0;
1607 bo_gem->mapped_cpu_write = false;
1610 /* We need to unmap after every innovation as we cannot track
1611 * an open vma for every bo as that will exhaasut the system
1612 * limits and cause later failures.
1614 if (--bo_gem->map_count == 0) {
1615 drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
1616 drm_intel_gem_bo_mark_mmaps_incoherent(bo);
1619 pthread_mutex_unlock(&bufmgr_gem->lock);
1625 drm_intel_gem_bo_unmap_gtt(drm_intel_bo *bo)
1627 return drm_intel_gem_bo_unmap(bo);
1631 drm_intel_gem_bo_subdata(drm_intel_bo *bo, unsigned long offset,
1632 unsigned long size, const void *data)
1634 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1635 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1636 struct drm_i915_gem_pwrite pwrite;
1639 if (bo_gem->is_userptr)
1643 pwrite.handle = bo_gem->gem_handle;
1644 pwrite.offset = offset;
1646 pwrite.data_ptr = (uint64_t) (uintptr_t) data;
1647 ret = drmIoctl(bufmgr_gem->fd,
1648 DRM_IOCTL_I915_GEM_PWRITE,
1652 DBG("%s:%d: Error writing data to buffer %d: (%d %d) %s .\n",
1653 __FILE__, __LINE__, bo_gem->gem_handle, (int)offset,
1654 (int)size, strerror(errno));
1661 drm_intel_gem_get_pipe_from_crtc_id(drm_intel_bufmgr *bufmgr, int crtc_id)
1663 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
1664 struct drm_i915_get_pipe_from_crtc_id get_pipe_from_crtc_id;
1667 memclear(get_pipe_from_crtc_id);
1668 get_pipe_from_crtc_id.crtc_id = crtc_id;
1669 ret = drmIoctl(bufmgr_gem->fd,
1670 DRM_IOCTL_I915_GET_PIPE_FROM_CRTC_ID,
1671 &get_pipe_from_crtc_id);
1673 /* We return -1 here to signal that we don't
1674 * know which pipe is associated with this crtc.
1675 * This lets the caller know that this information
1676 * isn't available; using the wrong pipe for
1677 * vblank waiting can cause the chipset to lock up
1682 return get_pipe_from_crtc_id.pipe;
1686 drm_intel_gem_bo_get_subdata(drm_intel_bo *bo, unsigned long offset,
1687 unsigned long size, void *data)
1689 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1690 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1691 struct drm_i915_gem_pread pread;
1694 if (bo_gem->is_userptr)
1698 pread.handle = bo_gem->gem_handle;
1699 pread.offset = offset;
1701 pread.data_ptr = (uint64_t) (uintptr_t) data;
1702 ret = drmIoctl(bufmgr_gem->fd,
1703 DRM_IOCTL_I915_GEM_PREAD,
1707 DBG("%s:%d: Error reading data from buffer %d: (%d %d) %s .\n",
1708 __FILE__, __LINE__, bo_gem->gem_handle, (int)offset,
1709 (int)size, strerror(errno));
1715 /** Waits for all GPU rendering with the object to have completed. */
1717 drm_intel_gem_bo_wait_rendering(drm_intel_bo *bo)
1719 drm_intel_gem_bo_start_gtt_access(bo, 1);
1723 * Waits on a BO for the given amount of time.
1725 * @bo: buffer object to wait for
1726 * @timeout_ns: amount of time to wait in nanoseconds.
1727 * If value is less than 0, an infinite wait will occur.
1729 * Returns 0 if the wait was successful ie. the last batch referencing the
1730 * object has completed within the allotted time. Otherwise some negative return
1731 * value describes the error. Of particular interest is -ETIME when the wait has
1732 * failed to yield the desired result.
1734 * Similar to drm_intel_gem_bo_wait_rendering except a timeout parameter allows
1735 * the operation to give up after a certain amount of time. Another subtle
1736 * difference is the internal locking semantics are different (this variant does
1737 * not hold the lock for the duration of the wait). This makes the wait subject
1738 * to a larger userspace race window.
1740 * The implementation shall wait until the object is no longer actively
1741 * referenced within a batch buffer at the time of the call. The wait will
1742 * not guarantee that the buffer is re-issued via another thread, or an flinked
1743 * handle. Userspace must make sure this race does not occur if such precision
1746 * Note that some kernels have broken the inifite wait for negative values
1747 * promise, upgrade to latest stable kernels if this is the case.
1750 drm_intel_gem_bo_wait(drm_intel_bo *bo, int64_t timeout_ns)
1752 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1753 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1754 struct drm_i915_gem_wait wait;
1757 if (!bufmgr_gem->has_wait_timeout) {
1758 DBG("%s:%d: Timed wait is not supported. Falling back to "
1759 "infinite wait\n", __FILE__, __LINE__);
1761 drm_intel_gem_bo_wait_rendering(bo);
1764 return drm_intel_gem_bo_busy(bo) ? -ETIME : 0;
1769 wait.bo_handle = bo_gem->gem_handle;
1770 wait.timeout_ns = timeout_ns;
1771 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_WAIT, &wait);
1779 * Sets the object to the GTT read and possibly write domain, used by the X
1780 * 2D driver in the absence of kernel support to do drm_intel_gem_bo_map_gtt().
1782 * In combination with drm_intel_gem_bo_pin() and manual fence management, we
1783 * can do tiled pixmaps this way.
1786 drm_intel_gem_bo_start_gtt_access(drm_intel_bo *bo, int write_enable)
1788 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1789 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1790 struct drm_i915_gem_set_domain set_domain;
1793 memclear(set_domain);
1794 set_domain.handle = bo_gem->gem_handle;
1795 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
1796 set_domain.write_domain = write_enable ? I915_GEM_DOMAIN_GTT : 0;
1797 ret = drmIoctl(bufmgr_gem->fd,
1798 DRM_IOCTL_I915_GEM_SET_DOMAIN,
1801 DBG("%s:%d: Error setting memory domains %d (%08x %08x): %s .\n",
1802 __FILE__, __LINE__, bo_gem->gem_handle,
1803 set_domain.read_domains, set_domain.write_domain,
1809 drm_intel_bufmgr_gem_destroy(drm_intel_bufmgr *bufmgr)
1811 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
1812 struct drm_gem_close close_bo;
1815 free(bufmgr_gem->exec2_objects);
1816 free(bufmgr_gem->exec_objects);
1817 free(bufmgr_gem->exec_bos);
1818 free(bufmgr_gem->aub_filename);
1820 pthread_mutex_destroy(&bufmgr_gem->lock);
1822 /* Free any cached buffer objects we were going to reuse */
1823 for (i = 0; i < bufmgr_gem->num_buckets; i++) {
1824 struct drm_intel_gem_bo_bucket *bucket =
1825 &bufmgr_gem->cache_bucket[i];
1826 drm_intel_bo_gem *bo_gem;
1828 while (!DRMLISTEMPTY(&bucket->head)) {
1829 bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
1830 bucket->head.next, head);
1831 DRMLISTDEL(&bo_gem->head);
1833 drm_intel_gem_bo_free(&bo_gem->bo);
1837 /* Release userptr bo kept hanging around for optimisation. */
1838 if (bufmgr_gem->userptr_active.ptr) {
1840 close_bo.handle = bufmgr_gem->userptr_active.handle;
1841 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
1842 free(bufmgr_gem->userptr_active.ptr);
1845 "Failed to release test userptr object! (%d) "
1846 "i915 kernel driver may not be sane!\n", errno);
1853 * Adds the target buffer to the validation list and adds the relocation
1854 * to the reloc_buffer's relocation list.
1856 * The relocation entry at the given offset must already contain the
1857 * precomputed relocation value, because the kernel will optimize out
1858 * the relocation entry write when the buffer hasn't moved from the
1859 * last known offset in target_bo.
1862 do_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset,
1863 drm_intel_bo *target_bo, uint32_t target_offset,
1864 uint32_t read_domains, uint32_t write_domain,
1867 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1868 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1869 drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) target_bo;
1870 bool fenced_command;
1872 if (bo_gem->has_error)
1875 if (target_bo_gem->has_error) {
1876 bo_gem->has_error = true;
1880 /* We never use HW fences for rendering on 965+ */
1881 if (bufmgr_gem->gen >= 4)
1884 fenced_command = need_fence;
1885 if (target_bo_gem->tiling_mode == I915_TILING_NONE)
1888 /* Create a new relocation list if needed */
1889 if (bo_gem->relocs == NULL && drm_intel_setup_reloc_list(bo))
1892 /* Check overflow */
1893 assert(bo_gem->reloc_count < bufmgr_gem->max_relocs);
1896 assert(offset <= bo->size - 4);
1897 assert((write_domain & (write_domain - 1)) == 0);
1899 /* An object needing a fence is a tiled buffer, so it won't have
1900 * relocs to other buffers.
1903 assert(target_bo_gem->reloc_count == 0);
1904 target_bo_gem->reloc_tree_fences = 1;
1907 /* Make sure that we're not adding a reloc to something whose size has
1908 * already been accounted for.
1910 assert(!bo_gem->used_as_reloc_target);
1911 if (target_bo_gem != bo_gem) {
1912 target_bo_gem->used_as_reloc_target = true;
1913 bo_gem->reloc_tree_size += target_bo_gem->reloc_tree_size;
1914 bo_gem->reloc_tree_fences += target_bo_gem->reloc_tree_fences;
1917 bo_gem->relocs[bo_gem->reloc_count].offset = offset;
1918 bo_gem->relocs[bo_gem->reloc_count].delta = target_offset;
1919 bo_gem->relocs[bo_gem->reloc_count].target_handle =
1920 target_bo_gem->gem_handle;
1921 bo_gem->relocs[bo_gem->reloc_count].read_domains = read_domains;
1922 bo_gem->relocs[bo_gem->reloc_count].write_domain = write_domain;
1923 bo_gem->relocs[bo_gem->reloc_count].presumed_offset = target_bo->offset64;
1925 bo_gem->reloc_target_info[bo_gem->reloc_count].bo = target_bo;
1926 if (target_bo != bo)
1927 drm_intel_gem_bo_reference(target_bo);
1929 bo_gem->reloc_target_info[bo_gem->reloc_count].flags =
1930 DRM_INTEL_RELOC_FENCE;
1932 bo_gem->reloc_target_info[bo_gem->reloc_count].flags = 0;
1934 bo_gem->reloc_count++;
1940 drm_intel_gem_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset,
1941 drm_intel_bo *target_bo, uint32_t target_offset,
1942 uint32_t read_domains, uint32_t write_domain)
1944 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
1946 return do_bo_emit_reloc(bo, offset, target_bo, target_offset,
1947 read_domains, write_domain,
1948 !bufmgr_gem->fenced_relocs);
1952 drm_intel_gem_bo_emit_reloc_fence(drm_intel_bo *bo, uint32_t offset,
1953 drm_intel_bo *target_bo,
1954 uint32_t target_offset,
1955 uint32_t read_domains, uint32_t write_domain)
1957 return do_bo_emit_reloc(bo, offset, target_bo, target_offset,
1958 read_domains, write_domain, true);
1962 drm_intel_gem_bo_get_reloc_count(drm_intel_bo *bo)
1964 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1966 return bo_gem->reloc_count;
1970 * Removes existing relocation entries in the BO after "start".
1972 * This allows a user to avoid a two-step process for state setup with
1973 * counting up all the buffer objects and doing a
1974 * drm_intel_bufmgr_check_aperture_space() before emitting any of the
1975 * relocations for the state setup. Instead, save the state of the
1976 * batchbuffer including drm_intel_gem_get_reloc_count(), emit all the
1977 * state, and then check if it still fits in the aperture.
1979 * Any further drm_intel_bufmgr_check_aperture_space() queries
1980 * involving this buffer in the tree are undefined after this call.
1983 drm_intel_gem_bo_clear_relocs(drm_intel_bo *bo, int start)
1985 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
1986 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
1988 struct timespec time;
1990 clock_gettime(CLOCK_MONOTONIC, &time);
1992 assert(bo_gem->reloc_count >= start);
1994 /* Unreference the cleared target buffers */
1995 pthread_mutex_lock(&bufmgr_gem->lock);
1997 for (i = start; i < bo_gem->reloc_count; i++) {
1998 drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) bo_gem->reloc_target_info[i].bo;
1999 if (&target_bo_gem->bo != bo) {
2000 bo_gem->reloc_tree_fences -= target_bo_gem->reloc_tree_fences;
2001 drm_intel_gem_bo_unreference_locked_timed(&target_bo_gem->bo,
2005 bo_gem->reloc_count = start;
2007 pthread_mutex_unlock(&bufmgr_gem->lock);
2012 * Walk the tree of relocations rooted at BO and accumulate the list of
2013 * validations to be performed and update the relocation buffers with
2014 * index values into the validation list.
2017 drm_intel_gem_bo_process_reloc(drm_intel_bo *bo)
2019 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2022 if (bo_gem->relocs == NULL)
2025 for (i = 0; i < bo_gem->reloc_count; i++) {
2026 drm_intel_bo *target_bo = bo_gem->reloc_target_info[i].bo;
2028 if (target_bo == bo)
2031 drm_intel_gem_bo_mark_mmaps_incoherent(bo);
2033 /* Continue walking the tree depth-first. */
2034 drm_intel_gem_bo_process_reloc(target_bo);
2036 /* Add the target to the validate list */
2037 drm_intel_add_validate_buffer(target_bo);
2042 drm_intel_gem_bo_process_reloc2(drm_intel_bo *bo)
2044 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
2047 if (bo_gem->relocs == NULL)
2050 for (i = 0; i < bo_gem->reloc_count; i++) {
2051 drm_intel_bo *target_bo = bo_gem->reloc_target_info[i].bo;
2054 if (target_bo == bo)
2057 drm_intel_gem_bo_mark_mmaps_incoherent(bo);
2059 /* Continue walking the tree depth-first. */
2060 drm_intel_gem_bo_process_reloc2(target_bo);
2062 need_fence = (bo_gem->reloc_target_info[i].flags &
2063 DRM_INTEL_RELOC_FENCE);
2065 /* Add the target to the validate list */
2066 drm_intel_add_validate_buffer2(target_bo, need_fence);
2072 drm_intel_update_buffer_offsets(drm_intel_bufmgr_gem *bufmgr_gem)
2076 for (i = 0; i < bufmgr_gem->exec_count; i++) {
2077 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
2078 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2080 /* Update the buffer offset */
2081 if (bufmgr_gem->exec_objects[i].offset != bo->offset64) {
2082 DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n",
2083 bo_gem->gem_handle, bo_gem->name, bo->offset64,
2084 (unsigned long long)bufmgr_gem->exec_objects[i].
2086 bo->offset64 = bufmgr_gem->exec_objects[i].offset;
2087 bo->offset = bufmgr_gem->exec_objects[i].offset;
2093 drm_intel_update_buffer_offsets2 (drm_intel_bufmgr_gem *bufmgr_gem)
2097 for (i = 0; i < bufmgr_gem->exec_count; i++) {
2098 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
2099 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
2101 /* Update the buffer offset */
2102 if (bufmgr_gem->exec2_objects[i].offset != bo->offset64) {
2103 DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n",
2104 bo_gem->gem_handle, bo_gem->name, bo->offset64,
2105 (unsigned long long)bufmgr_gem->exec2_objects[i].offset);
2106 bo->offset64 = bufmgr_gem->exec2_objects[i].offset;
2107 bo->offset = bufmgr_gem->exec2_objects[i].offset;
2113 aub_out(drm_intel_bufmgr_gem *bufmgr_gem, uint32_t data)
2115 fwrite(&data, 1, 4, bufmgr_gem->aub_file);
2119 aub_out_data(drm_intel_bufmgr_gem *bufmgr_gem, void *data, size_t size)
2121 fwrite(data, 1, size, bufmgr_gem->aub_file);
2125 aub_write_bo_data(drm_intel_bo *bo, uint32_t offset, uint32_t size)
2127 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2128 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2132 data = malloc(bo->size);
2133 drm_intel_bo_get_subdata(bo, offset, size, data);
2135 /* Easy mode: write out bo with no relocations */
2136 if (!bo_gem->reloc_count) {
2137 aub_out_data(bufmgr_gem, data, size);
2142 /* Otherwise, handle the relocations while writing. */
2143 for (i = 0; i < size / 4; i++) {
2145 for (r = 0; r < bo_gem->reloc_count; r++) {
2146 struct drm_i915_gem_relocation_entry *reloc;
2147 drm_intel_reloc_target *info;
2149 reloc = &bo_gem->relocs[r];
2150 info = &bo_gem->reloc_target_info[r];
2152 if (reloc->offset == offset + i * 4) {
2153 drm_intel_bo_gem *target_gem;
2156 target_gem = (drm_intel_bo_gem *)info->bo;
2159 val += target_gem->aub_offset;
2161 aub_out(bufmgr_gem, val);
2166 if (r == bo_gem->reloc_count) {
2167 /* no relocation, just the data */
2168 aub_out(bufmgr_gem, data[i]);
2176 aub_bo_get_address(drm_intel_bo *bo)
2178 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2179 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2181 /* Give the object a graphics address in the AUB file. We
2182 * don't just use the GEM object address because we do AUB
2183 * dumping before execution -- we want to successfully log
2184 * when the hardware might hang, and we might even want to aub
2185 * capture for a driver trying to execute on a different
2186 * generation of hardware by disabling the actual kernel exec
2189 bo_gem->aub_offset = bufmgr_gem->aub_offset;
2190 bufmgr_gem->aub_offset += bo->size;
2191 /* XXX: Handle aperture overflow. */
2192 assert(bufmgr_gem->aub_offset < 256 * 1024 * 1024);
2196 aub_write_trace_block(drm_intel_bo *bo, uint32_t type, uint32_t subtype,
2197 uint32_t offset, uint32_t size)
2199 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2200 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2203 CMD_AUB_TRACE_HEADER_BLOCK |
2204 ((bufmgr_gem->gen >= 8 ? 6 : 5) - 2));
2206 AUB_TRACE_MEMTYPE_GTT | type | AUB_TRACE_OP_DATA_WRITE);
2207 aub_out(bufmgr_gem, subtype);
2208 aub_out(bufmgr_gem, bo_gem->aub_offset + offset);
2209 aub_out(bufmgr_gem, size);
2210 if (bufmgr_gem->gen >= 8)
2211 aub_out(bufmgr_gem, 0);
2212 aub_write_bo_data(bo, offset, size);
2216 * Break up large objects into multiple writes. Otherwise a 128kb VBO
2217 * would overflow the 16 bits of size field in the packet header and
2218 * everything goes badly after that.
2221 aub_write_large_trace_block(drm_intel_bo *bo, uint32_t type, uint32_t subtype,
2222 uint32_t offset, uint32_t size)
2224 uint32_t block_size;
2225 uint32_t sub_offset;
2227 for (sub_offset = 0; sub_offset < size; sub_offset += block_size) {
2228 block_size = size - sub_offset;
2230 if (block_size > 8 * 4096)
2231 block_size = 8 * 4096;
2233 aub_write_trace_block(bo, type, subtype, offset + sub_offset,
2239 aub_write_bo(drm_intel_bo *bo)
2241 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2242 uint32_t offset = 0;
2245 aub_bo_get_address(bo);
2247 /* Write out each annotated section separately. */
2248 for (i = 0; i < bo_gem->aub_annotation_count; ++i) {
2249 drm_intel_aub_annotation *annotation =
2250 &bo_gem->aub_annotations[i];
2251 uint32_t ending_offset = annotation->ending_offset;
2252 if (ending_offset > bo->size)
2253 ending_offset = bo->size;
2254 if (ending_offset > offset) {
2255 aub_write_large_trace_block(bo, annotation->type,
2256 annotation->subtype,
2258 ending_offset - offset);
2259 offset = ending_offset;
2263 /* Write out any remaining unannotated data */
2264 if (offset < bo->size) {
2265 aub_write_large_trace_block(bo, AUB_TRACE_TYPE_NOTYPE, 0,
2266 offset, bo->size - offset);
2271 * Make a ringbuffer on fly and dump it
2274 aub_build_dump_ringbuffer(drm_intel_bufmgr_gem *bufmgr_gem,
2275 uint32_t batch_buffer, int ring_flag)
2277 uint32_t ringbuffer[4096];
2278 int ring = AUB_TRACE_TYPE_RING_PRB0; /* The default ring */
2281 if (ring_flag == I915_EXEC_BSD)
2282 ring = AUB_TRACE_TYPE_RING_PRB1;
2283 else if (ring_flag == I915_EXEC_BLT)
2284 ring = AUB_TRACE_TYPE_RING_PRB2;
2286 /* Make a ring buffer to execute our batchbuffer. */
2287 memset(ringbuffer, 0, sizeof(ringbuffer));
2288 if (bufmgr_gem->gen >= 8) {
2289 ringbuffer[ring_count++] = AUB_MI_BATCH_BUFFER_START | (3 - 2);
2290 ringbuffer[ring_count++] = batch_buffer;
2291 ringbuffer[ring_count++] = 0;
2293 ringbuffer[ring_count++] = AUB_MI_BATCH_BUFFER_START;
2294 ringbuffer[ring_count++] = batch_buffer;
2297 /* Write out the ring. This appears to trigger execution of
2298 * the ring in the simulator.
2301 CMD_AUB_TRACE_HEADER_BLOCK |
2302 ((bufmgr_gem->gen >= 8 ? 6 : 5) - 2));
2304 AUB_TRACE_MEMTYPE_GTT | ring | AUB_TRACE_OP_COMMAND_WRITE);
2305 aub_out(bufmgr_gem, 0); /* general/surface subtype */
2306 aub_out(bufmgr_gem, bufmgr_gem->aub_offset);
2307 aub_out(bufmgr_gem, ring_count * 4);
2308 if (bufmgr_gem->gen >= 8)
2309 aub_out(bufmgr_gem, 0);
2311 /* FIXME: Need some flush operations here? */
2312 aub_out_data(bufmgr_gem, ringbuffer, ring_count * 4);
2314 /* Update offset pointer */
2315 bufmgr_gem->aub_offset += 4096;
2319 drm_intel_gem_bo_aub_dump_bmp(drm_intel_bo *bo,
2320 int x1, int y1, int width, int height,
2321 enum aub_dump_bmp_format format,
2322 int pitch, int offset)
2324 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2325 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
2329 case AUB_DUMP_BMP_FORMAT_8BIT:
2332 case AUB_DUMP_BMP_FORMAT_ARGB_4444:
2335 case AUB_DUMP_BMP_FORMAT_ARGB_0888:
2336 case AUB_DUMP_BMP_FORMAT_ARGB_8888:
2340 printf("Unknown AUB dump format %d\n", format);
2344 if (!bufmgr_gem->aub_file)
2347 aub_out(bufmgr_gem, CMD_AUB_DUMP_BMP | 4);
2348 aub_out(bufmgr_gem, (y1 << 16) | x1);
2353 aub_out(bufmgr_gem, (height << 16) | width);
2354 aub_out(bufmgr_gem, bo_gem->aub_offset + offset);
2356 ((bo_gem->tiling_mode != I915_TILING_NONE) ? (1 << 2) : 0) |
2357 ((bo_gem->tiling_mode == I915_TILING_Y) ? (1 << 3) : 0));
2361 aub_exec(drm_intel_bo *bo, int ring_flag, int used)
2363 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2364 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2366 bool batch_buffer_needs_annotations;
2368 if (!bufmgr_gem->aub_file)
2371 /* If batch buffer is not annotated, annotate it the best we
2374 batch_buffer_needs_annotations = bo_gem->aub_annotation_count == 0;
2375 if (batch_buffer_needs_annotations) {
2376 drm_intel_aub_annotation annotations[2] = {
2377 { AUB_TRACE_TYPE_BATCH, 0, used },
2378 { AUB_TRACE_TYPE_NOTYPE, 0, bo->size }
2380 drm_intel_bufmgr_gem_set_aub_annotations(bo, annotations, 2);
2383 /* Write out all buffers to AUB memory */
2384 for (i = 0; i < bufmgr_gem->exec_count; i++) {
2385 aub_write_bo(bufmgr_gem->exec_bos[i]);
2388 /* Remove any annotations we added */
2389 if (batch_buffer_needs_annotations)
2390 drm_intel_bufmgr_gem_set_aub_annotations(bo, NULL, 0);
2392 /* Dump ring buffer */
2393 aub_build_dump_ringbuffer(bufmgr_gem, bo_gem->aub_offset, ring_flag);
2395 fflush(bufmgr_gem->aub_file);
2398 * One frame has been dumped. So reset the aub_offset for the next frame.
2400 * FIXME: Can we do this?
2402 bufmgr_gem->aub_offset = 0x10000;
2406 drm_intel_gem_bo_exec(drm_intel_bo *bo, int used,
2407 drm_clip_rect_t * cliprects, int num_cliprects, int DR4)
2409 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2410 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2411 struct drm_i915_gem_execbuffer execbuf;
2414 if (bo_gem->has_error)
2417 pthread_mutex_lock(&bufmgr_gem->lock);
2418 /* Update indices and set up the validate list. */
2419 drm_intel_gem_bo_process_reloc(bo);
2421 /* Add the batch buffer to the validation list. There are no
2422 * relocations pointing to it.
2424 drm_intel_add_validate_buffer(bo);
2427 execbuf.buffers_ptr = (uintptr_t) bufmgr_gem->exec_objects;
2428 execbuf.buffer_count = bufmgr_gem->exec_count;
2429 execbuf.batch_start_offset = 0;
2430 execbuf.batch_len = used;
2431 execbuf.cliprects_ptr = (uintptr_t) cliprects;
2432 execbuf.num_cliprects = num_cliprects;
2436 ret = drmIoctl(bufmgr_gem->fd,
2437 DRM_IOCTL_I915_GEM_EXECBUFFER,
2441 if (errno == ENOSPC) {
2442 DBG("Execbuffer fails to pin. "
2443 "Estimate: %u. Actual: %u. Available: %u\n",
2444 drm_intel_gem_estimate_batch_space(bufmgr_gem->exec_bos,
2447 drm_intel_gem_compute_batch_space(bufmgr_gem->exec_bos,
2450 (unsigned int)bufmgr_gem->gtt_size);
2453 drm_intel_update_buffer_offsets(bufmgr_gem);
2455 if (bufmgr_gem->bufmgr.debug)
2456 drm_intel_gem_dump_validation_list(bufmgr_gem);
2458 for (i = 0; i < bufmgr_gem->exec_count; i++) {
2459 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
2460 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2462 bo_gem->idle = false;
2464 /* Disconnect the buffer from the validate list */
2465 bo_gem->validate_index = -1;
2466 bufmgr_gem->exec_bos[i] = NULL;
2468 bufmgr_gem->exec_count = 0;
2469 pthread_mutex_unlock(&bufmgr_gem->lock);
2475 do_exec2(drm_intel_bo *bo, int used, drm_intel_context *ctx,
2476 drm_clip_rect_t *cliprects, int num_cliprects, int DR4,
2479 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
2480 struct drm_i915_gem_execbuffer2 execbuf;
2484 switch (flags & 0x7) {
2488 if (!bufmgr_gem->has_blt)
2492 if (!bufmgr_gem->has_bsd)
2495 case I915_EXEC_VEBOX:
2496 if (!bufmgr_gem->has_vebox)
2499 case I915_EXEC_RENDER:
2500 case I915_EXEC_DEFAULT:
2504 pthread_mutex_lock(&bufmgr_gem->lock);
2505 /* Update indices and set up the validate list. */
2506 drm_intel_gem_bo_process_reloc2(bo);
2508 /* Add the batch buffer to the validation list. There are no relocations
2511 drm_intel_add_validate_buffer2(bo, 0);
2514 execbuf.buffers_ptr = (uintptr_t)bufmgr_gem->exec2_objects;
2515 execbuf.buffer_count = bufmgr_gem->exec_count;
2516 execbuf.batch_start_offset = 0;
2517 execbuf.batch_len = used;
2518 execbuf.cliprects_ptr = (uintptr_t)cliprects;
2519 execbuf.num_cliprects = num_cliprects;
2522 execbuf.flags = flags;
2524 i915_execbuffer2_set_context_id(execbuf, 0);
2526 i915_execbuffer2_set_context_id(execbuf, ctx->ctx_id);
2529 aub_exec(bo, flags, used);
2531 if (bufmgr_gem->no_exec)
2532 goto skip_execution;
2534 ret = drmIoctl(bufmgr_gem->fd,
2535 DRM_IOCTL_I915_GEM_EXECBUFFER2,
2539 if (ret == -ENOSPC) {
2540 DBG("Execbuffer fails to pin. "
2541 "Estimate: %u. Actual: %u. Available: %u\n",
2542 drm_intel_gem_estimate_batch_space(bufmgr_gem->exec_bos,
2543 bufmgr_gem->exec_count),
2544 drm_intel_gem_compute_batch_space(bufmgr_gem->exec_bos,
2545 bufmgr_gem->exec_count),
2546 (unsigned int) bufmgr_gem->gtt_size);
2549 drm_intel_update_buffer_offsets2(bufmgr_gem);
2552 if (bufmgr_gem->bufmgr.debug)
2553 drm_intel_gem_dump_validation_list(bufmgr_gem);
2555 for (i = 0; i < bufmgr_gem->exec_count; i++) {
2556 drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
2557 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
2559 bo_gem->idle = false;
2561 /* Disconnect the buffer from the validate list */
2562 bo_gem->validate_index = -1;
2563 bufmgr_gem->exec_bos[i] = NULL;
2565 bufmgr_gem->exec_count = 0;
2566 pthread_mutex_unlock(&bufmgr_gem->lock);
2572 drm_intel_gem_bo_exec2(drm_intel_bo *bo, int used,
2573 drm_clip_rect_t *cliprects, int num_cliprects,
2576 return do_exec2(bo, used, NULL, cliprects, num_cliprects, DR4,
2581 drm_intel_gem_bo_mrb_exec2(drm_intel_bo *bo, int used,
2582 drm_clip_rect_t *cliprects, int num_cliprects, int DR4,
2585 return do_exec2(bo, used, NULL, cliprects, num_cliprects, DR4,
2590 drm_intel_gem_bo_context_exec(drm_intel_bo *bo, drm_intel_context *ctx,
2591 int used, unsigned int flags)
2593 return do_exec2(bo, used, ctx, NULL, 0, 0, flags);
2597 drm_intel_gem_bo_pin(drm_intel_bo *bo, uint32_t alignment)
2599 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2600 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2601 struct drm_i915_gem_pin pin;
2605 pin.handle = bo_gem->gem_handle;
2606 pin.alignment = alignment;
2608 ret = drmIoctl(bufmgr_gem->fd,
2609 DRM_IOCTL_I915_GEM_PIN,
2614 bo->offset64 = pin.offset;
2615 bo->offset = pin.offset;
2620 drm_intel_gem_bo_unpin(drm_intel_bo *bo)
2622 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2623 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2624 struct drm_i915_gem_unpin unpin;
2628 unpin.handle = bo_gem->gem_handle;
2630 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_UNPIN, &unpin);
2638 drm_intel_gem_bo_set_tiling_internal(drm_intel_bo *bo,
2639 uint32_t tiling_mode,
2642 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2643 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2644 struct drm_i915_gem_set_tiling set_tiling;
2647 if (bo_gem->global_name == 0 &&
2648 tiling_mode == bo_gem->tiling_mode &&
2649 stride == bo_gem->stride)
2652 memset(&set_tiling, 0, sizeof(set_tiling));
2654 /* set_tiling is slightly broken and overwrites the
2655 * input on the error path, so we have to open code
2658 set_tiling.handle = bo_gem->gem_handle;
2659 set_tiling.tiling_mode = tiling_mode;
2660 set_tiling.stride = stride;
2662 ret = ioctl(bufmgr_gem->fd,
2663 DRM_IOCTL_I915_GEM_SET_TILING,
2665 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
2669 bo_gem->tiling_mode = set_tiling.tiling_mode;
2670 bo_gem->swizzle_mode = set_tiling.swizzle_mode;
2671 bo_gem->stride = set_tiling.stride;
2676 drm_intel_gem_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
2679 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2680 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2683 /* Tiling with userptr surfaces is not supported
2684 * on all hardware so refuse it for time being.
2686 if (bo_gem->is_userptr)
2689 /* Linear buffers have no stride. By ensuring that we only ever use
2690 * stride 0 with linear buffers, we simplify our code.
2692 if (*tiling_mode == I915_TILING_NONE)
2695 ret = drm_intel_gem_bo_set_tiling_internal(bo, *tiling_mode, stride);
2697 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
2699 *tiling_mode = bo_gem->tiling_mode;
2704 drm_intel_gem_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
2705 uint32_t * swizzle_mode)
2707 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2709 *tiling_mode = bo_gem->tiling_mode;
2710 *swizzle_mode = bo_gem->swizzle_mode;
2715 drm_intel_bo_gem_create_from_prime(drm_intel_bufmgr *bufmgr, int prime_fd, int size)
2717 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
2720 drm_intel_bo_gem *bo_gem;
2721 struct drm_i915_gem_get_tiling get_tiling;
2722 drmMMListHead *list;
2724 ret = drmPrimeFDToHandle(bufmgr_gem->fd, prime_fd, &handle);
2727 * See if the kernel has already returned this buffer to us. Just as
2728 * for named buffers, we must not create two bo's pointing at the same
2731 pthread_mutex_lock(&bufmgr_gem->lock);
2732 for (list = bufmgr_gem->named.next;
2733 list != &bufmgr_gem->named;
2734 list = list->next) {
2735 bo_gem = DRMLISTENTRY(drm_intel_bo_gem, list, name_list);
2736 if (bo_gem->gem_handle == handle) {
2737 drm_intel_gem_bo_reference(&bo_gem->bo);
2738 pthread_mutex_unlock(&bufmgr_gem->lock);
2744 fprintf(stderr,"ret is %d %d\n", ret, errno);
2745 pthread_mutex_unlock(&bufmgr_gem->lock);
2749 bo_gem = calloc(1, sizeof(*bo_gem));
2751 pthread_mutex_unlock(&bufmgr_gem->lock);
2754 /* Determine size of bo. The fd-to-handle ioctl really should
2755 * return the size, but it doesn't. If we have kernel 3.12 or
2756 * later, we can lseek on the prime fd to get the size. Older
2757 * kernels will just fail, in which case we fall back to the
2758 * provided (estimated or guess size). */
2759 ret = lseek(prime_fd, 0, SEEK_END);
2761 bo_gem->bo.size = ret;
2763 bo_gem->bo.size = size;
2765 bo_gem->bo.handle = handle;
2766 bo_gem->bo.bufmgr = bufmgr;
2768 bo_gem->gem_handle = handle;
2770 atomic_set(&bo_gem->refcount, 1);
2772 bo_gem->name = "prime";
2773 bo_gem->validate_index = -1;
2774 bo_gem->reloc_tree_fences = 0;
2775 bo_gem->used_as_reloc_target = false;
2776 bo_gem->has_error = false;
2777 bo_gem->reusable = false;
2779 DRMINITLISTHEAD(&bo_gem->vma_list);
2780 DRMLISTADDTAIL(&bo_gem->name_list, &bufmgr_gem->named);
2781 pthread_mutex_unlock(&bufmgr_gem->lock);
2783 memclear(get_tiling);
2784 get_tiling.handle = bo_gem->gem_handle;
2785 ret = drmIoctl(bufmgr_gem->fd,
2786 DRM_IOCTL_I915_GEM_GET_TILING,
2789 drm_intel_gem_bo_unreference(&bo_gem->bo);
2792 bo_gem->tiling_mode = get_tiling.tiling_mode;
2793 bo_gem->swizzle_mode = get_tiling.swizzle_mode;
2794 /* XXX stride is unknown */
2795 drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
2801 drm_intel_bo_gem_export_to_prime(drm_intel_bo *bo, int *prime_fd)
2803 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2804 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2806 pthread_mutex_lock(&bufmgr_gem->lock);
2807 if (DRMLISTEMPTY(&bo_gem->name_list))
2808 DRMLISTADDTAIL(&bo_gem->name_list, &bufmgr_gem->named);
2809 pthread_mutex_unlock(&bufmgr_gem->lock);
2811 if (drmPrimeHandleToFD(bufmgr_gem->fd, bo_gem->gem_handle,
2812 DRM_CLOEXEC, prime_fd) != 0)
2815 bo_gem->reusable = false;
2821 drm_intel_gem_bo_flink(drm_intel_bo *bo, uint32_t * name)
2823 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
2824 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2827 if (!bo_gem->global_name) {
2828 struct drm_gem_flink flink;
2831 flink.handle = bo_gem->gem_handle;
2833 pthread_mutex_lock(&bufmgr_gem->lock);
2835 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_GEM_FLINK, &flink);
2837 pthread_mutex_unlock(&bufmgr_gem->lock);
2841 bo_gem->global_name = flink.name;
2842 bo_gem->reusable = false;
2844 if (DRMLISTEMPTY(&bo_gem->name_list))
2845 DRMLISTADDTAIL(&bo_gem->name_list, &bufmgr_gem->named);
2846 pthread_mutex_unlock(&bufmgr_gem->lock);
2849 *name = bo_gem->global_name;
2854 * Enables unlimited caching of buffer objects for reuse.
2856 * This is potentially very memory expensive, as the cache at each bucket
2857 * size is only bounded by how many buffers of that size we've managed to have
2858 * in flight at once.
2861 drm_intel_bufmgr_gem_enable_reuse(drm_intel_bufmgr *bufmgr)
2863 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
2865 bufmgr_gem->bo_reuse = true;
2869 * Enable use of fenced reloc type.
2871 * New code should enable this to avoid unnecessary fence register
2872 * allocation. If this option is not enabled, all relocs will have fence
2873 * register allocated.
2876 drm_intel_bufmgr_gem_enable_fenced_relocs(drm_intel_bufmgr *bufmgr)
2878 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
2880 if (bufmgr_gem->bufmgr.bo_exec == drm_intel_gem_bo_exec2)
2881 bufmgr_gem->fenced_relocs = true;
2885 * Return the additional aperture space required by the tree of buffer objects
2889 drm_intel_gem_bo_get_aperture_space(drm_intel_bo *bo)
2891 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2895 if (bo == NULL || bo_gem->included_in_check_aperture)
2899 bo_gem->included_in_check_aperture = true;
2901 for (i = 0; i < bo_gem->reloc_count; i++)
2903 drm_intel_gem_bo_get_aperture_space(bo_gem->
2904 reloc_target_info[i].bo);
2910 * Count the number of buffers in this list that need a fence reg
2912 * If the count is greater than the number of available regs, we'll have
2913 * to ask the caller to resubmit a batch with fewer tiled buffers.
2915 * This function over-counts if the same buffer is used multiple times.
2918 drm_intel_gem_total_fences(drm_intel_bo ** bo_array, int count)
2921 unsigned int total = 0;
2923 for (i = 0; i < count; i++) {
2924 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo_array[i];
2929 total += bo_gem->reloc_tree_fences;
2935 * Clear the flag set by drm_intel_gem_bo_get_aperture_space() so we're ready
2936 * for the next drm_intel_bufmgr_check_aperture_space() call.
2939 drm_intel_gem_bo_clear_aperture_space_flag(drm_intel_bo *bo)
2941 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
2944 if (bo == NULL || !bo_gem->included_in_check_aperture)
2947 bo_gem->included_in_check_aperture = false;
2949 for (i = 0; i < bo_gem->reloc_count; i++)
2950 drm_intel_gem_bo_clear_aperture_space_flag(bo_gem->
2951 reloc_target_info[i].bo);
2955 * Return a conservative estimate for the amount of aperture required
2956 * for a collection of buffers. This may double-count some buffers.
2959 drm_intel_gem_estimate_batch_space(drm_intel_bo **bo_array, int count)
2962 unsigned int total = 0;
2964 for (i = 0; i < count; i++) {
2965 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo_array[i];
2967 total += bo_gem->reloc_tree_size;
2973 * Return the amount of aperture needed for a collection of buffers.
2974 * This avoids double counting any buffers, at the cost of looking
2975 * at every buffer in the set.
2978 drm_intel_gem_compute_batch_space(drm_intel_bo **bo_array, int count)
2981 unsigned int total = 0;
2983 for (i = 0; i < count; i++) {
2984 total += drm_intel_gem_bo_get_aperture_space(bo_array[i]);
2985 /* For the first buffer object in the array, we get an
2986 * accurate count back for its reloc_tree size (since nothing
2987 * had been flagged as being counted yet). We can save that
2988 * value out as a more conservative reloc_tree_size that
2989 * avoids double-counting target buffers. Since the first
2990 * buffer happens to usually be the batch buffer in our
2991 * callers, this can pull us back from doing the tree
2992 * walk on every new batch emit.
2995 drm_intel_bo_gem *bo_gem =
2996 (drm_intel_bo_gem *) bo_array[i];
2997 bo_gem->reloc_tree_size = total;
3001 for (i = 0; i < count; i++)
3002 drm_intel_gem_bo_clear_aperture_space_flag(bo_array[i]);
3007 * Return -1 if the batchbuffer should be flushed before attempting to
3008 * emit rendering referencing the buffers pointed to by bo_array.
3010 * This is required because if we try to emit a batchbuffer with relocations
3011 * to a tree of buffers that won't simultaneously fit in the aperture,
3012 * the rendering will return an error at a point where the software is not
3013 * prepared to recover from it.
3015 * However, we also want to emit the batchbuffer significantly before we reach
3016 * the limit, as a series of batchbuffers each of which references buffers
3017 * covering almost all of the aperture means that at each emit we end up
3018 * waiting to evict a buffer from the last rendering, and we get synchronous
3019 * performance. By emitting smaller batchbuffers, we eat some CPU overhead to
3020 * get better parallelism.
3023 drm_intel_gem_check_aperture_space(drm_intel_bo **bo_array, int count)
3025 drm_intel_bufmgr_gem *bufmgr_gem =
3026 (drm_intel_bufmgr_gem *) bo_array[0]->bufmgr;
3027 unsigned int total = 0;
3028 unsigned int threshold = bufmgr_gem->gtt_size * 3 / 4;
3031 /* Check for fence reg constraints if necessary */
3032 if (bufmgr_gem->available_fences) {
3033 total_fences = drm_intel_gem_total_fences(bo_array, count);
3034 if (total_fences > bufmgr_gem->available_fences)
3038 total = drm_intel_gem_estimate_batch_space(bo_array, count);
3040 if (total > threshold)
3041 total = drm_intel_gem_compute_batch_space(bo_array, count);
3043 if (total > threshold) {
3044 DBG("check_space: overflowed available aperture, "
3046 total / 1024, (int)bufmgr_gem->gtt_size / 1024);
3049 DBG("drm_check_space: total %dkb vs bufgr %dkb\n", total / 1024,
3050 (int)bufmgr_gem->gtt_size / 1024);
3056 * Disable buffer reuse for objects which are shared with the kernel
3057 * as scanout buffers
3060 drm_intel_gem_bo_disable_reuse(drm_intel_bo *bo)
3062 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
3064 bo_gem->reusable = false;
3069 drm_intel_gem_bo_is_reusable(drm_intel_bo *bo)
3071 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
3073 return bo_gem->reusable;
3077 _drm_intel_gem_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo)
3079 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
3082 for (i = 0; i < bo_gem->reloc_count; i++) {
3083 if (bo_gem->reloc_target_info[i].bo == target_bo)
3085 if (bo == bo_gem->reloc_target_info[i].bo)
3087 if (_drm_intel_gem_bo_references(bo_gem->reloc_target_info[i].bo,
3095 /** Return true if target_bo is referenced by bo's relocation tree. */
3097 drm_intel_gem_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo)
3099 drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) target_bo;
3101 if (bo == NULL || target_bo == NULL)
3103 if (target_bo_gem->used_as_reloc_target)
3104 return _drm_intel_gem_bo_references(bo, target_bo);
3109 add_bucket(drm_intel_bufmgr_gem *bufmgr_gem, int size)
3111 unsigned int i = bufmgr_gem->num_buckets;
3113 assert(i < ARRAY_SIZE(bufmgr_gem->cache_bucket));
3115 DRMINITLISTHEAD(&bufmgr_gem->cache_bucket[i].head);
3116 bufmgr_gem->cache_bucket[i].size = size;
3117 bufmgr_gem->num_buckets++;
3121 init_cache_buckets(drm_intel_bufmgr_gem *bufmgr_gem)
3123 unsigned long size, cache_max_size = 64 * 1024 * 1024;
3125 /* OK, so power of two buckets was too wasteful of memory.
3126 * Give 3 other sizes between each power of two, to hopefully
3127 * cover things accurately enough. (The alternative is
3128 * probably to just go for exact matching of sizes, and assume
3129 * that for things like composited window resize the tiled
3130 * width/height alignment and rounding of sizes to pages will
3131 * get us useful cache hit rates anyway)
3133 add_bucket(bufmgr_gem, 4096);
3134 add_bucket(bufmgr_gem, 4096 * 2);
3135 add_bucket(bufmgr_gem, 4096 * 3);
3137 /* Initialize the linked lists for BO reuse cache. */
3138 for (size = 4 * 4096; size <= cache_max_size; size *= 2) {
3139 add_bucket(bufmgr_gem, size);
3141 add_bucket(bufmgr_gem, size + size * 1 / 4);
3142 add_bucket(bufmgr_gem, size + size * 2 / 4);
3143 add_bucket(bufmgr_gem, size + size * 3 / 4);
3148 drm_intel_bufmgr_gem_set_vma_cache_size(drm_intel_bufmgr *bufmgr, int limit)
3150 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
3152 bufmgr_gem->vma_max = limit;
3154 drm_intel_gem_bo_purge_vma_cache(bufmgr_gem);
3158 * Get the PCI ID for the device. This can be overridden by setting the
3159 * INTEL_DEVID_OVERRIDE environment variable to the desired ID.
3162 get_pci_device_id(drm_intel_bufmgr_gem *bufmgr_gem)
3164 char *devid_override;
3167 drm_i915_getparam_t gp;
3169 if (geteuid() == getuid()) {
3170 devid_override = getenv("INTEL_DEVID_OVERRIDE");
3171 if (devid_override) {
3172 bufmgr_gem->no_exec = true;
3173 return strtod(devid_override, NULL);
3178 gp.param = I915_PARAM_CHIPSET_ID;
3180 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
3182 fprintf(stderr, "get chip id failed: %d [%d]\n", ret, errno);
3183 fprintf(stderr, "param: %d, val: %d\n", gp.param, *gp.value);
3189 drm_intel_bufmgr_gem_get_devid(drm_intel_bufmgr *bufmgr)
3191 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
3193 return bufmgr_gem->pci_device;
3197 * Sets the AUB filename.
3199 * This function has to be called before drm_intel_bufmgr_gem_set_aub_dump()
3200 * for it to have any effect.
3203 drm_intel_bufmgr_gem_set_aub_filename(drm_intel_bufmgr *bufmgr,
3204 const char *filename)
3206 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
3208 free(bufmgr_gem->aub_filename);
3210 bufmgr_gem->aub_filename = strdup(filename);
3214 * Sets up AUB dumping.
3216 * This is a trace file format that can be used with the simulator.
3217 * Packets are emitted in a format somewhat like GPU command packets.
3218 * You can set up a GTT and upload your objects into the referenced
3219 * space, then send off batchbuffers and get BMPs out the other end.
3222 drm_intel_bufmgr_gem_set_aub_dump(drm_intel_bufmgr *bufmgr, int enable)
3224 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
3225 int entry = 0x200003;
3227 int gtt_size = 0x10000;
3228 const char *filename;
3231 if (bufmgr_gem->aub_file) {
3232 fclose(bufmgr_gem->aub_file);
3233 bufmgr_gem->aub_file = NULL;
3238 if (geteuid() != getuid())
3241 if (bufmgr_gem->aub_filename)
3242 filename = bufmgr_gem->aub_filename;
3244 filename = "intel.aub";
3245 bufmgr_gem->aub_file = fopen(filename, "w+");
3246 if (!bufmgr_gem->aub_file)
3249 /* Start allocating objects from just after the GTT. */
3250 bufmgr_gem->aub_offset = gtt_size;
3252 /* Start with a (required) version packet. */
3253 aub_out(bufmgr_gem, CMD_AUB_HEADER | (13 - 2));
3255 (4 << AUB_HEADER_MAJOR_SHIFT) |
3256 (0 << AUB_HEADER_MINOR_SHIFT));
3257 for (i = 0; i < 8; i++) {
3258 aub_out(bufmgr_gem, 0); /* app name */
3260 aub_out(bufmgr_gem, 0); /* timestamp */
3261 aub_out(bufmgr_gem, 0); /* timestamp */
3262 aub_out(bufmgr_gem, 0); /* comment len */
3264 /* Set up the GTT. The max we can handle is 256M */
3265 aub_out(bufmgr_gem, CMD_AUB_TRACE_HEADER_BLOCK | ((bufmgr_gem->gen >= 8 ? 6 : 5) - 2));
3266 /* Need to use GTT_ENTRY type for recent emulator */
3267 aub_out(bufmgr_gem, AUB_TRACE_MEMTYPE_GTT_ENTRY | 0 | AUB_TRACE_OP_DATA_WRITE);
3268 aub_out(bufmgr_gem, 0); /* subtype */
3269 aub_out(bufmgr_gem, 0); /* offset */
3270 aub_out(bufmgr_gem, gtt_size); /* size */
3271 if (bufmgr_gem->gen >= 8)
3272 aub_out(bufmgr_gem, 0);
3273 for (i = 0x000; i < gtt_size; i += 4, entry += 0x1000) {
3274 aub_out(bufmgr_gem, entry);
3279 drm_intel_gem_context_create(drm_intel_bufmgr *bufmgr)
3281 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
3282 struct drm_i915_gem_context_create create;
3283 drm_intel_context *context = NULL;
3286 context = calloc(1, sizeof(*context));
3291 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create);
3293 DBG("DRM_IOCTL_I915_GEM_CONTEXT_CREATE failed: %s\n",
3299 context->ctx_id = create.ctx_id;
3300 context->bufmgr = bufmgr;
3306 drm_intel_gem_context_destroy(drm_intel_context *ctx)
3308 drm_intel_bufmgr_gem *bufmgr_gem;
3309 struct drm_i915_gem_context_destroy destroy;
3317 bufmgr_gem = (drm_intel_bufmgr_gem *)ctx->bufmgr;
3318 destroy.ctx_id = ctx->ctx_id;
3319 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY,
3322 fprintf(stderr, "DRM_IOCTL_I915_GEM_CONTEXT_DESTROY failed: %s\n",
3329 drm_intel_get_reset_stats(drm_intel_context *ctx,
3330 uint32_t *reset_count,
3334 drm_intel_bufmgr_gem *bufmgr_gem;
3335 struct drm_i915_reset_stats stats;
3343 bufmgr_gem = (drm_intel_bufmgr_gem *)ctx->bufmgr;
3344 stats.ctx_id = ctx->ctx_id;
3345 ret = drmIoctl(bufmgr_gem->fd,
3346 DRM_IOCTL_I915_GET_RESET_STATS,
3349 if (reset_count != NULL)
3350 *reset_count = stats.reset_count;
3353 *active = stats.batch_active;
3355 if (pending != NULL)
3356 *pending = stats.batch_pending;
3363 drm_intel_reg_read(drm_intel_bufmgr *bufmgr,
3367 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
3368 struct drm_i915_reg_read reg_read;
3372 reg_read.offset = offset;
3374 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_REG_READ, ®_read);
3376 *result = reg_read.val;
3381 drm_intel_get_subslice_total(int fd, unsigned int *subslice_total)
3383 drm_i915_getparam_t gp;
3387 gp.value = (int*)subslice_total;
3388 gp.param = I915_PARAM_SUBSLICE_TOTAL;
3389 ret = drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
3397 drm_intel_get_eu_total(int fd, unsigned int *eu_total)
3399 drm_i915_getparam_t gp;
3403 gp.value = (int*)eu_total;
3404 gp.param = I915_PARAM_EU_TOTAL;
3405 ret = drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
3413 * Annotate the given bo for use in aub dumping.
3415 * \param annotations is an array of drm_intel_aub_annotation objects
3416 * describing the type of data in various sections of the bo. Each
3417 * element of the array specifies the type and subtype of a section of
3418 * the bo, and the past-the-end offset of that section. The elements
3419 * of \c annotations must be sorted so that ending_offset is
3422 * \param count is the number of elements in the \c annotations array.
3423 * If \c count is zero, then \c annotations will not be dereferenced.
3425 * Annotations are copied into a private data structure, so caller may
3426 * re-use the memory pointed to by \c annotations after the call
3429 * Annotations are stored for the lifetime of the bo; to reset to the
3430 * default state (no annotations), call this function with a \c count
3434 drm_intel_bufmgr_gem_set_aub_annotations(drm_intel_bo *bo,
3435 drm_intel_aub_annotation *annotations,
3438 drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
3439 unsigned size = sizeof(*annotations) * count;
3440 drm_intel_aub_annotation *new_annotations =
3441 count > 0 ? realloc(bo_gem->aub_annotations, size) : NULL;
3442 if (new_annotations == NULL) {
3443 free(bo_gem->aub_annotations);
3444 bo_gem->aub_annotations = NULL;
3445 bo_gem->aub_annotation_count = 0;
3448 memcpy(new_annotations, annotations, size);
3449 bo_gem->aub_annotations = new_annotations;
3450 bo_gem->aub_annotation_count = count;
3453 static pthread_mutex_t bufmgr_list_mutex = PTHREAD_MUTEX_INITIALIZER;
3454 static drmMMListHead bufmgr_list = { &bufmgr_list, &bufmgr_list };
3456 static drm_intel_bufmgr_gem *
3457 drm_intel_bufmgr_gem_find(int fd)
3459 drm_intel_bufmgr_gem *bufmgr_gem;
3461 DRMLISTFOREACHENTRY(bufmgr_gem, &bufmgr_list, managers) {
3462 if (bufmgr_gem->fd == fd) {
3463 atomic_inc(&bufmgr_gem->refcount);
3472 drm_intel_bufmgr_gem_unref(drm_intel_bufmgr *bufmgr)
3474 drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
3476 if (atomic_add_unless(&bufmgr_gem->refcount, -1, 1)) {
3477 pthread_mutex_lock(&bufmgr_list_mutex);
3479 if (atomic_dec_and_test(&bufmgr_gem->refcount)) {
3480 DRMLISTDEL(&bufmgr_gem->managers);
3481 drm_intel_bufmgr_gem_destroy(bufmgr);
3484 pthread_mutex_unlock(&bufmgr_list_mutex);
3489 * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
3490 * and manage map buffer objections.
3492 * \param fd File descriptor of the opened DRM device.
3495 drm_intel_bufmgr_gem_init(int fd, int batch_size)
3497 drm_intel_bufmgr_gem *bufmgr_gem;
3498 struct drm_i915_gem_get_aperture aperture;
3499 drm_i915_getparam_t gp;
3503 pthread_mutex_lock(&bufmgr_list_mutex);
3505 bufmgr_gem = drm_intel_bufmgr_gem_find(fd);
3509 bufmgr_gem = calloc(1, sizeof(*bufmgr_gem));
3510 if (bufmgr_gem == NULL)
3513 bufmgr_gem->fd = fd;
3514 atomic_set(&bufmgr_gem->refcount, 1);
3516 if (pthread_mutex_init(&bufmgr_gem->lock, NULL) != 0) {
3523 ret = drmIoctl(bufmgr_gem->fd,
3524 DRM_IOCTL_I915_GEM_GET_APERTURE,
3528 bufmgr_gem->gtt_size = aperture.aper_available_size;
3530 fprintf(stderr, "DRM_IOCTL_I915_GEM_APERTURE failed: %s\n",
3532 bufmgr_gem->gtt_size = 128 * 1024 * 1024;
3533 fprintf(stderr, "Assuming %dkB available aperture size.\n"
3534 "May lead to reduced performance or incorrect "
3536 (int)bufmgr_gem->gtt_size / 1024);
3539 bufmgr_gem->pci_device = get_pci_device_id(bufmgr_gem);
3541 if (IS_GEN2(bufmgr_gem->pci_device))
3542 bufmgr_gem->gen = 2;
3543 else if (IS_GEN3(bufmgr_gem->pci_device))
3544 bufmgr_gem->gen = 3;
3545 else if (IS_GEN4(bufmgr_gem->pci_device))
3546 bufmgr_gem->gen = 4;
3547 else if (IS_GEN5(bufmgr_gem->pci_device))
3548 bufmgr_gem->gen = 5;
3549 else if (IS_GEN6(bufmgr_gem->pci_device))
3550 bufmgr_gem->gen = 6;
3551 else if (IS_GEN7(bufmgr_gem->pci_device))
3552 bufmgr_gem->gen = 7;
3553 else if (IS_GEN8(bufmgr_gem->pci_device))
3554 bufmgr_gem->gen = 8;
3555 else if (IS_GEN9(bufmgr_gem->pci_device))
3556 bufmgr_gem->gen = 9;
3563 if (IS_GEN3(bufmgr_gem->pci_device) &&
3564 bufmgr_gem->gtt_size > 256*1024*1024) {
3565 /* The unmappable part of gtt on gen 3 (i.e. above 256MB) can't
3566 * be used for tiled blits. To simplify the accounting, just
3567 * substract the unmappable part (fixed to 256MB on all known
3568 * gen3 devices) if the kernel advertises it. */
3569 bufmgr_gem->gtt_size -= 256*1024*1024;
3575 gp.param = I915_PARAM_HAS_EXECBUF2;
3576 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
3580 gp.param = I915_PARAM_HAS_BSD;
3581 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
3582 bufmgr_gem->has_bsd = ret == 0;
3584 gp.param = I915_PARAM_HAS_BLT;
3585 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
3586 bufmgr_gem->has_blt = ret == 0;
3588 gp.param = I915_PARAM_HAS_RELAXED_FENCING;
3589 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
3590 bufmgr_gem->has_relaxed_fencing = ret == 0;
3592 bufmgr_gem->bufmgr.bo_alloc_userptr = check_bo_alloc_userptr;
3594 gp.param = I915_PARAM_HAS_WAIT_TIMEOUT;
3595 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
3596 bufmgr_gem->has_wait_timeout = ret == 0;
3598 gp.param = I915_PARAM_HAS_LLC;
3599 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
3601 /* Kernel does not supports HAS_LLC query, fallback to GPU
3602 * generation detection and assume that we have LLC on GEN6/7
3604 bufmgr_gem->has_llc = (IS_GEN6(bufmgr_gem->pci_device) |
3605 IS_GEN7(bufmgr_gem->pci_device));
3607 bufmgr_gem->has_llc = *gp.value;
3609 gp.param = I915_PARAM_HAS_VEBOX;
3610 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
3611 bufmgr_gem->has_vebox = (ret == 0) & (*gp.value > 0);
3613 if (bufmgr_gem->gen < 4) {
3614 gp.param = I915_PARAM_NUM_FENCES_AVAIL;
3615 gp.value = &bufmgr_gem->available_fences;
3616 ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
3618 fprintf(stderr, "get fences failed: %d [%d]\n", ret,
3620 fprintf(stderr, "param: %d, val: %d\n", gp.param,
3622 bufmgr_gem->available_fences = 0;
3624 /* XXX The kernel reports the total number of fences,
3625 * including any that may be pinned.
3627 * We presume that there will be at least one pinned
3628 * fence for the scanout buffer, but there may be more
3629 * than one scanout and the user may be manually
3630 * pinning buffers. Let's move to execbuffer2 and
3631 * thereby forget the insanity of using fences...
3633 bufmgr_gem->available_fences -= 2;
3634 if (bufmgr_gem->available_fences < 0)
3635 bufmgr_gem->available_fences = 0;
3639 /* Let's go with one relocation per every 2 dwords (but round down a bit
3640 * since a power of two will mean an extra page allocation for the reloc
3643 * Every 4 was too few for the blender benchmark.
3645 bufmgr_gem->max_relocs = batch_size / sizeof(uint32_t) / 2 - 2;
3647 bufmgr_gem->bufmgr.bo_alloc = drm_intel_gem_bo_alloc;
3648 bufmgr_gem->bufmgr.bo_alloc_for_render =
3649 drm_intel_gem_bo_alloc_for_render;
3650 bufmgr_gem->bufmgr.bo_alloc_tiled = drm_intel_gem_bo_alloc_tiled;
3651 bufmgr_gem->bufmgr.bo_reference = drm_intel_gem_bo_reference;
3652 bufmgr_gem->bufmgr.bo_unreference = drm_intel_gem_bo_unreference;
3653 bufmgr_gem->bufmgr.bo_map = drm_intel_gem_bo_map;
3654 bufmgr_gem->bufmgr.bo_unmap = drm_intel_gem_bo_unmap;
3655 bufmgr_gem->bufmgr.bo_subdata = drm_intel_gem_bo_subdata;
3656 bufmgr_gem->bufmgr.bo_get_subdata = drm_intel_gem_bo_get_subdata;
3657 bufmgr_gem->bufmgr.bo_wait_rendering = drm_intel_gem_bo_wait_rendering;
3658 bufmgr_gem->bufmgr.bo_emit_reloc = drm_intel_gem_bo_emit_reloc;
3659 bufmgr_gem->bufmgr.bo_emit_reloc_fence = drm_intel_gem_bo_emit_reloc_fence;
3660 bufmgr_gem->bufmgr.bo_pin = drm_intel_gem_bo_pin;
3661 bufmgr_gem->bufmgr.bo_unpin = drm_intel_gem_bo_unpin;
3662 bufmgr_gem->bufmgr.bo_get_tiling = drm_intel_gem_bo_get_tiling;
3663 bufmgr_gem->bufmgr.bo_set_tiling = drm_intel_gem_bo_set_tiling;
3664 bufmgr_gem->bufmgr.bo_flink = drm_intel_gem_bo_flink;
3665 /* Use the new one if available */
3667 bufmgr_gem->bufmgr.bo_exec = drm_intel_gem_bo_exec2;
3668 bufmgr_gem->bufmgr.bo_mrb_exec = drm_intel_gem_bo_mrb_exec2;
3670 bufmgr_gem->bufmgr.bo_exec = drm_intel_gem_bo_exec;
3671 bufmgr_gem->bufmgr.bo_busy = drm_intel_gem_bo_busy;
3672 bufmgr_gem->bufmgr.bo_madvise = drm_intel_gem_bo_madvise;
3673 bufmgr_gem->bufmgr.destroy = drm_intel_bufmgr_gem_unref;
3674 bufmgr_gem->bufmgr.debug = 0;
3675 bufmgr_gem->bufmgr.check_aperture_space =
3676 drm_intel_gem_check_aperture_space;
3677 bufmgr_gem->bufmgr.bo_disable_reuse = drm_intel_gem_bo_disable_reuse;
3678 bufmgr_gem->bufmgr.bo_is_reusable = drm_intel_gem_bo_is_reusable;
3679 bufmgr_gem->bufmgr.get_pipe_from_crtc_id =
3680 drm_intel_gem_get_pipe_from_crtc_id;
3681 bufmgr_gem->bufmgr.bo_references = drm_intel_gem_bo_references;
3683 DRMINITLISTHEAD(&bufmgr_gem->named);
3684 init_cache_buckets(bufmgr_gem);
3686 DRMINITLISTHEAD(&bufmgr_gem->vma_cache);
3687 bufmgr_gem->vma_max = -1; /* unlimited by default */
3689 DRMLISTADD(&bufmgr_gem->managers, &bufmgr_list);
3692 pthread_mutex_unlock(&bufmgr_list_mutex);
3694 return bufmgr_gem != NULL ? &bufmgr_gem->bufmgr : NULL;