1 /**************************************************************************
3 * Copyright © 2007 Red Hat Inc.
4 * Copyright © 2007 Intel Corporation
5 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * The above copyright notice and this permission notice (including the
25 * next paragraph) shall be included in all copies or substantial portions
29 **************************************************************************/
31 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
32 * Keith Whitwell <keithw-at-tungstengraphics-dot-com>
33 * Eric Anholt <eric@anholt.net>
34 * Dave Airlie <airlied@linux.ie>
43 #include <sys/ioctl.h>
47 #include "intel_bufmgr.h"
48 #include "intel_bufmgr_priv.h"
53 #define DBG(...) do { \
54 if (bufmgr_gem->bufmgr.debug) \
55 fprintf(stderr, __VA_ARGS__); \
58 typedef struct _dri_bo_gem dri_bo_gem;
60 struct dri_gem_bo_bucket {
61 dri_bo_gem *head, **tail;
63 * Limit on the number of entries in this bucket.
65 * 0 means that this caching at this bucket size is disabled.
66 * -1 means that there is no limit to caching at this size.
72 /* Arbitrarily chosen, 16 means that the maximum size we'll cache for reuse
73 * is 1 << 16 pages, or 256MB.
75 #define INTEL_GEM_BO_BUCKETS 16
76 typedef struct _dri_bufmgr_gem {
83 struct drm_i915_gem_exec_object *exec_objects;
88 /** Array of lists of cached gem objects of power-of-two sizes */
89 struct dri_gem_bo_bucket cache_bucket[INTEL_GEM_BO_BUCKETS];
96 /** Boolean whether the mmap ioctl has been called for this buffer yet. */
102 * Kenel-assigned global name for this object
104 unsigned int global_name;
107 * Index of the buffer within the validation list while preparing a
108 * batchbuffer execution.
113 * Boolean whether we've started swrast
114 * Set when the buffer has been mapped
115 * Cleared when the buffer is unmapped
119 /** Array passed to the DRM containing relocation information. */
120 struct drm_i915_gem_relocation_entry *relocs;
121 /** Array of bos corresponding to relocs[i].target_handle */
122 dri_bo **reloc_target_bo;
123 /** Number of entries in relocs */
125 /** Mapped address for the buffer */
146 static struct dri_gem_bo_bucket *
147 dri_gem_bo_bucket_for_size(dri_bufmgr_gem *bufmgr_gem, unsigned long size)
151 /* We only do buckets in power of two increments */
152 if ((size & (size - 1)) != 0)
155 /* We should only see sizes rounded to pages. */
156 assert((size % 4096) == 0);
158 /* We always allocate in units of pages */
159 i = ffs(size / 4096) - 1;
160 if (i >= INTEL_GEM_BO_BUCKETS)
163 return &bufmgr_gem->cache_bucket[i];
167 static void dri_gem_dump_validation_list(dri_bufmgr_gem *bufmgr_gem)
171 for (i = 0; i < bufmgr_gem->exec_count; i++) {
172 dri_bo *bo = bufmgr_gem->exec_bos[i];
173 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
175 if (bo_gem->relocs == NULL) {
176 DBG("%2d: %d (%s)\n", i, bo_gem->gem_handle, bo_gem->name);
180 for (j = 0; j < bo_gem->reloc_count; j++) {
181 dri_bo *target_bo = bo_gem->reloc_target_bo[j];
182 dri_bo_gem *target_gem = (dri_bo_gem *)target_bo;
184 DBG("%2d: %d (%s)@0x%08llx -> %d (%s)@0x%08lx + 0x%08x\n",
186 bo_gem->gem_handle, bo_gem->name, bo_gem->relocs[j].offset,
187 target_gem->gem_handle, target_gem->name, target_bo->offset,
188 bo_gem->relocs[j].delta);
194 * Adds the given buffer to the list of buffers to be validated (moved into the
195 * appropriate memory type) with the next batch submission.
197 * If a buffer is validated multiple times in a batch submission, it ends up
198 * with the intersection of the memory type flags and the union of the
202 intel_add_validate_buffer(dri_bo *bo)
204 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
205 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
208 if (bo_gem->validate_index != -1)
211 /* Extend the array of validation entries as necessary. */
212 if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
213 int new_size = bufmgr_gem->exec_size * 2;
218 bufmgr_gem->exec_objects =
219 realloc(bufmgr_gem->exec_objects,
220 sizeof(*bufmgr_gem->exec_objects) * new_size);
221 bufmgr_gem->exec_bos =
222 realloc(bufmgr_gem->exec_bos,
223 sizeof(*bufmgr_gem->exec_bos) * new_size);
224 bufmgr_gem->exec_size = new_size;
227 index = bufmgr_gem->exec_count;
228 bo_gem->validate_index = index;
229 /* Fill in array entry */
230 bufmgr_gem->exec_objects[index].handle = bo_gem->gem_handle;
231 bufmgr_gem->exec_objects[index].relocation_count = bo_gem->reloc_count;
232 bufmgr_gem->exec_objects[index].relocs_ptr = (uintptr_t)bo_gem->relocs;
233 bufmgr_gem->exec_objects[index].alignment = 0;
234 bufmgr_gem->exec_objects[index].offset = 0;
235 bufmgr_gem->exec_bos[index] = bo;
236 dri_bo_reference(bo);
237 bufmgr_gem->exec_count++;
241 #define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \
245 intel_setup_reloc_list(dri_bo *bo)
247 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
248 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
250 bo_gem->relocs = malloc(bufmgr_gem->max_relocs *
251 sizeof(struct drm_i915_gem_relocation_entry));
252 bo_gem->reloc_target_bo = malloc(bufmgr_gem->max_relocs * sizeof(dri_bo *));
258 dri_gem_bo_alloc(dri_bufmgr *bufmgr, const char *name,
259 unsigned long size, unsigned int alignment)
261 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr;
263 unsigned int page_size = getpagesize();
265 struct dri_gem_bo_bucket *bucket;
266 int alloc_from_cache = 0;
267 unsigned long bo_size;
269 /* Round the allocated size up to a power of two number of pages. */
270 bo_size = 1 << logbase2(size);
271 if (bo_size < page_size)
273 bucket = dri_gem_bo_bucket_for_size(bufmgr_gem, bo_size);
275 /* If we don't have caching at this size, don't actually round the
278 if (bucket == NULL || bucket->max_entries == 0) {
280 if (bo_size < page_size)
284 /* Get a buffer out of the cache if available */
285 if (bucket != NULL && bucket->num_entries > 0) {
286 struct drm_i915_gem_busy busy;
288 bo_gem = bucket->head;
289 busy.handle = bo_gem->gem_handle;
291 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
292 alloc_from_cache = (ret == 0 && busy.busy == 0);
294 if (alloc_from_cache) {
295 bucket->head = bo_gem->next;
296 if (bo_gem->next == NULL)
297 bucket->tail = &bucket->head;
298 bucket->num_entries--;
302 if (!alloc_from_cache) {
303 struct drm_i915_gem_create create;
305 bo_gem = calloc(1, sizeof(*bo_gem));
309 bo_gem->bo.size = bo_size;
310 memset(&create, 0, sizeof(create));
311 create.size = bo_size;
313 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_CREATE, &create);
314 bo_gem->gem_handle = create.handle;
319 bo_gem->bo.bufmgr = bufmgr;
323 bo_gem->refcount = 1;
324 bo_gem->validate_index = -1;
326 DBG("bo_create: buf %d (%s) %ldb\n",
327 bo_gem->gem_handle, bo_gem->name, size);
333 * Returns a dri_bo wrapping the given buffer object handle.
335 * This can be used when one application needs to pass a buffer object
339 intel_bo_gem_create_from_name(dri_bufmgr *bufmgr, const char *name,
342 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr;
345 struct drm_gem_open open_arg;
347 bo_gem = calloc(1, sizeof(*bo_gem));
351 memset(&open_arg, 0, sizeof(open_arg));
352 open_arg.name = handle;
353 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_OPEN, &open_arg);
355 fprintf(stderr, "Couldn't reference %s handle 0x%08x: %s\n",
356 name, handle, strerror(-ret));
360 bo_gem->bo.size = open_arg.size;
361 bo_gem->bo.offset = 0;
362 bo_gem->bo.virtual = NULL;
363 bo_gem->bo.bufmgr = bufmgr;
365 bo_gem->refcount = 1;
366 bo_gem->validate_index = -1;
367 bo_gem->gem_handle = open_arg.handle;
368 bo_gem->global_name = handle;
370 DBG("bo_create_from_handle: %d (%s)\n", handle, bo_gem->name);
376 dri_gem_bo_reference(dri_bo *bo)
378 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
384 dri_gem_bo_free(dri_bo *bo)
386 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
387 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
388 struct drm_gem_close close;
392 munmap (bo_gem->virtual, bo_gem->bo.size);
394 /* Close this object */
395 close.handle = bo_gem->gem_handle;
396 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close);
399 "DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
400 bo_gem->gem_handle, bo_gem->name, strerror(-ret));
406 dri_gem_bo_unreference(dri_bo *bo)
408 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
409 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
414 if (--bo_gem->refcount == 0) {
415 struct dri_gem_bo_bucket *bucket;
417 if (bo_gem->relocs != NULL) {
420 /* Unreference all the target buffers */
421 for (i = 0; i < bo_gem->reloc_count; i++)
422 dri_bo_unreference(bo_gem->reloc_target_bo[i]);
423 free(bo_gem->reloc_target_bo);
424 free(bo_gem->relocs);
427 DBG("bo_unreference final: %d (%s)\n",
428 bo_gem->gem_handle, bo_gem->name);
430 bucket = dri_gem_bo_bucket_for_size(bufmgr_gem, bo->size);
431 /* Put the buffer into our internal cache for reuse if we can. */
432 if (bucket != NULL &&
433 (bucket->max_entries == -1 ||
434 (bucket->max_entries > 0 &&
435 bucket->num_entries < bucket->max_entries)))
438 bo_gem->validate_index = -1;
439 bo_gem->relocs = NULL;
440 bo_gem->reloc_target_bo = NULL;
441 bo_gem->reloc_count = 0;
444 *bucket->tail = bo_gem;
445 bucket->tail = &bo_gem->next;
446 bucket->num_entries++;
456 dri_gem_bo_map(dri_bo *bo, int write_enable)
458 dri_bufmgr_gem *bufmgr_gem;
459 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
460 struct drm_i915_gem_set_domain set_domain;
463 bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
465 /* Allow recursive mapping. Mesa may recursively map buffers with
466 * nested display loops.
468 if (!bo_gem->mapped) {
470 assert(bo->virtual == NULL);
472 DBG("bo_map: %d (%s)\n", bo_gem->gem_handle, bo_gem->name);
474 if (bo_gem->virtual == NULL) {
475 struct drm_i915_gem_mmap mmap_arg;
477 memset(&mmap_arg, 0, sizeof(mmap_arg));
478 mmap_arg.handle = bo_gem->gem_handle;
480 mmap_arg.size = bo->size;
481 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg);
483 fprintf(stderr, "%s:%d: Error mapping buffer %d (%s): %s .\n",
485 bo_gem->gem_handle, bo_gem->name, strerror(errno));
487 bo_gem->virtual = (void *)(uintptr_t)mmap_arg.addr_ptr;
489 bo->virtual = bo_gem->virtual;
492 DBG("bo_map: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name, bo_gem->virtual);
495 if (!bo_gem->swrast) {
496 set_domain.handle = bo_gem->gem_handle;
497 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
499 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
501 set_domain.write_domain = 0;
503 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN,
505 } while (ret == -1 && errno == EINTR);
507 fprintf (stderr, "%s:%d: Error setting swrast %d: %s\n",
508 __FILE__, __LINE__, bo_gem->gem_handle, strerror (errno));
517 dri_gem_bo_unmap(dri_bo *bo)
519 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
520 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
521 struct drm_i915_gem_sw_finish sw_finish;
527 assert(bo_gem->mapped);
529 if (bo_gem->swrast) {
530 sw_finish.handle = bo_gem->gem_handle;
532 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_SW_FINISH,
534 } while (ret == -1 && errno == EINTR);
541 dri_gem_bo_subdata (dri_bo *bo, unsigned long offset,
542 unsigned long size, const void *data)
544 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
545 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
546 struct drm_i915_gem_pwrite pwrite;
549 memset (&pwrite, 0, sizeof (pwrite));
550 pwrite.handle = bo_gem->gem_handle;
551 pwrite.offset = offset;
553 pwrite.data_ptr = (uint64_t) (uintptr_t) data;
555 ret = ioctl (bufmgr_gem->fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite);
556 } while (ret == -1 && errno == EINTR);
558 fprintf (stderr, "%s:%d: Error writing data to buffer %d: (%d %d) %s .\n",
560 bo_gem->gem_handle, (int) offset, (int) size,
567 dri_gem_bo_get_subdata (dri_bo *bo, unsigned long offset,
568 unsigned long size, void *data)
570 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
571 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
572 struct drm_i915_gem_pread pread;
575 memset (&pread, 0, sizeof (pread));
576 pread.handle = bo_gem->gem_handle;
577 pread.offset = offset;
579 pread.data_ptr = (uint64_t) (uintptr_t) data;
581 ret = ioctl (bufmgr_gem->fd, DRM_IOCTL_I915_GEM_PREAD, &pread);
582 } while (ret == -1 && errno == EINTR);
584 fprintf (stderr, "%s:%d: Error reading data from buffer %d: (%d %d) %s .\n",
586 bo_gem->gem_handle, (int) offset, (int) size,
593 dri_gem_bo_wait_rendering(dri_bo *bo)
595 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
596 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
597 struct drm_i915_gem_set_domain set_domain;
600 set_domain.handle = bo_gem->gem_handle;
601 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
602 set_domain.write_domain = 0;
603 ret = ioctl (bufmgr_gem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
605 fprintf (stderr, "%s:%d: Error setting memory domains %d (%08x %08x): %s .\n",
607 bo_gem->gem_handle, set_domain.read_domains, set_domain.write_domain,
613 dri_bufmgr_gem_destroy(dri_bufmgr *bufmgr)
615 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr;
618 free(bufmgr_gem->exec_objects);
619 free(bufmgr_gem->exec_bos);
621 /* Free any cached buffer objects we were going to reuse */
622 for (i = 0; i < INTEL_GEM_BO_BUCKETS; i++) {
623 struct dri_gem_bo_bucket *bucket = &bufmgr_gem->cache_bucket[i];
626 while ((bo_gem = bucket->head) != NULL) {
627 bucket->head = bo_gem->next;
628 if (bo_gem->next == NULL)
629 bucket->tail = &bucket->head;
630 bucket->num_entries--;
632 dri_gem_bo_free(&bo_gem->bo);
640 * Adds the target buffer to the validation list and adds the relocation
641 * to the reloc_buffer's relocation list.
643 * The relocation entry at the given offset must already contain the
644 * precomputed relocation value, because the kernel will optimize out
645 * the relocation entry write when the buffer hasn't moved from the
646 * last known offset in target_bo.
649 dri_gem_bo_emit_reloc(dri_bo *bo, uint32_t read_domains, uint32_t write_domain,
650 uint32_t delta, uint32_t offset, dri_bo *target_bo)
652 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
653 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
654 dri_bo_gem *target_bo_gem = (dri_bo_gem *)target_bo;
656 /* Create a new relocation list if needed */
657 if (bo_gem->relocs == NULL)
658 intel_setup_reloc_list(bo);
661 assert(bo_gem->reloc_count < bufmgr_gem->max_relocs);
664 assert (offset <= bo->size - 4);
665 assert ((write_domain & (write_domain-1)) == 0);
667 bo_gem->relocs[bo_gem->reloc_count].offset = offset;
668 bo_gem->relocs[bo_gem->reloc_count].delta = delta;
669 bo_gem->relocs[bo_gem->reloc_count].target_handle =
670 target_bo_gem->gem_handle;
671 bo_gem->relocs[bo_gem->reloc_count].read_domains = read_domains;
672 bo_gem->relocs[bo_gem->reloc_count].write_domain = write_domain;
673 bo_gem->relocs[bo_gem->reloc_count].presumed_offset = target_bo->offset;
675 bo_gem->reloc_target_bo[bo_gem->reloc_count] = target_bo;
676 dri_bo_reference(target_bo);
678 bo_gem->reloc_count++;
683 * Walk the tree of relocations rooted at BO and accumulate the list of
684 * validations to be performed and update the relocation buffers with
685 * index values into the validation list.
688 dri_gem_bo_process_reloc(dri_bo *bo)
690 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
693 if (bo_gem->relocs == NULL)
696 for (i = 0; i < bo_gem->reloc_count; i++) {
697 dri_bo *target_bo = bo_gem->reloc_target_bo[i];
699 /* Continue walking the tree depth-first. */
700 dri_gem_bo_process_reloc(target_bo);
702 /* Add the target to the validate list */
703 intel_add_validate_buffer(target_bo);
708 intel_update_buffer_offsets (dri_bufmgr_gem *bufmgr_gem)
712 for (i = 0; i < bufmgr_gem->exec_count; i++) {
713 dri_bo *bo = bufmgr_gem->exec_bos[i];
714 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
716 /* Update the buffer offset */
717 if (bufmgr_gem->exec_objects[i].offset != bo->offset) {
718 DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n",
719 bo_gem->gem_handle, bo_gem->name, bo->offset,
720 bufmgr_gem->exec_objects[i].offset);
721 bo->offset = bufmgr_gem->exec_objects[i].offset;
727 dri_gem_bo_exec(dri_bo *bo, int used,
728 drm_clip_rect_t *cliprects, int num_cliprects,
731 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
732 struct drm_i915_gem_execbuffer execbuf;
735 /* Update indices and set up the validate list. */
736 dri_gem_bo_process_reloc(bo);
738 /* Add the batch buffer to the validation list. There are no relocations
741 intel_add_validate_buffer(bo);
743 execbuf.buffers_ptr = (uintptr_t)bufmgr_gem->exec_objects;
744 execbuf.buffer_count = bufmgr_gem->exec_count;
745 execbuf.batch_start_offset = 0;
746 execbuf.batch_len = used;
747 execbuf.cliprects_ptr = (uintptr_t)cliprects;
748 execbuf.num_cliprects = num_cliprects;
753 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_EXECBUFFER, &execbuf);
754 } while (ret == -EAGAIN);
756 intel_update_buffer_offsets (bufmgr_gem);
758 if (bufmgr_gem->bufmgr.debug)
759 dri_gem_dump_validation_list(bufmgr_gem);
761 for (i = 0; i < bufmgr_gem->exec_count; i++) {
762 dri_bo *bo = bufmgr_gem->exec_bos[i];
763 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
765 /* Need to call swrast on next bo_map */
768 /* Disconnect the buffer from the validate list */
769 bo_gem->validate_index = -1;
770 dri_bo_unreference(bo);
771 bufmgr_gem->exec_bos[i] = NULL;
773 bufmgr_gem->exec_count = 0;
779 dri_gem_bo_pin(dri_bo *bo, uint32_t alignment)
781 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
782 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
783 struct drm_i915_gem_pin pin;
786 pin.handle = bo_gem->gem_handle;
787 pin.alignment = alignment;
789 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_PIN, &pin);
793 bo->offset = pin.offset;
798 dri_gem_bo_unpin(dri_bo *bo)
800 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
801 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
802 struct drm_i915_gem_unpin unpin;
805 unpin.handle = bo_gem->gem_handle;
807 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_UNPIN, &unpin);
815 dri_gem_bo_set_tiling(dri_bo *bo, uint32_t *tiling_mode)
817 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
818 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
819 struct drm_i915_gem_set_tiling set_tiling;
822 set_tiling.handle = bo_gem->gem_handle;
823 set_tiling.tiling_mode = *tiling_mode;
825 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
827 *tiling_mode = I915_TILING_NONE;
831 *tiling_mode = set_tiling.tiling_mode;
836 dri_gem_bo_flink(dri_bo *bo, uint32_t *name)
838 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
839 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
840 struct drm_gem_flink flink;
843 if (!bo_gem->global_name) {
844 flink.handle = bo_gem->gem_handle;
846 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_FLINK, &flink);
849 bo_gem->global_name = flink.name;
852 *name = bo_gem->global_name;
857 * Enables unlimited caching of buffer objects for reuse.
859 * This is potentially very memory expensive, as the cache at each bucket
860 * size is only bounded by how many buffers of that size we've managed to have
864 intel_bufmgr_gem_enable_reuse(dri_bufmgr *bufmgr)
866 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr;
869 for (i = 0; i < INTEL_GEM_BO_BUCKETS; i++) {
870 bufmgr_gem->cache_bucket[i].max_entries = -1;
878 dri_gem_check_aperture_space(dri_bo **bo_array, int count)
884 * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
885 * and manage map buffer objections.
887 * \param fd File descriptor of the opened DRM device.
890 intel_bufmgr_gem_init(int fd, int batch_size)
892 dri_bufmgr_gem *bufmgr_gem;
895 bufmgr_gem = calloc(1, sizeof(*bufmgr_gem));
898 /* Let's go with one relocation per every 2 dwords (but round down a bit
899 * since a power of two will mean an extra page allocation for the reloc
902 * Every 4 was too few for the blender benchmark.
904 bufmgr_gem->max_relocs = batch_size / sizeof(uint32_t) / 2 - 2;
906 bufmgr_gem->bufmgr.bo_alloc = dri_gem_bo_alloc;
907 bufmgr_gem->bufmgr.bo_reference = dri_gem_bo_reference;
908 bufmgr_gem->bufmgr.bo_unreference = dri_gem_bo_unreference;
909 bufmgr_gem->bufmgr.bo_map = dri_gem_bo_map;
910 bufmgr_gem->bufmgr.bo_unmap = dri_gem_bo_unmap;
911 bufmgr_gem->bufmgr.bo_subdata = dri_gem_bo_subdata;
912 bufmgr_gem->bufmgr.bo_get_subdata = dri_gem_bo_get_subdata;
913 bufmgr_gem->bufmgr.bo_wait_rendering = dri_gem_bo_wait_rendering;
914 bufmgr_gem->bufmgr.bo_emit_reloc = dri_gem_bo_emit_reloc;
915 bufmgr_gem->bufmgr.bo_pin = dri_gem_bo_pin;
916 bufmgr_gem->bufmgr.bo_unpin = dri_gem_bo_unpin;
917 bufmgr_gem->bufmgr.bo_set_tiling = dri_gem_bo_set_tiling;
918 bufmgr_gem->bufmgr.bo_flink = dri_gem_bo_flink;
919 bufmgr_gem->bufmgr.bo_exec = dri_gem_bo_exec;
920 bufmgr_gem->bufmgr.destroy = dri_bufmgr_gem_destroy;
921 bufmgr_gem->bufmgr.debug = 0;
922 bufmgr_gem->bufmgr.check_aperture_space = dri_gem_check_aperture_space;
923 /* Initialize the linked lists for BO reuse cache. */
924 for (i = 0; i < INTEL_GEM_BO_BUCKETS; i++)
925 bufmgr_gem->cache_bucket[i].tail = &bufmgr_gem->cache_bucket[i].head;
927 return &bufmgr_gem->bufmgr;