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 "dri_bufmgr.h"
48 #include "intel_bufmgr.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 {
79 struct intel_bufmgr intel_bufmgr;
85 struct drm_i915_gem_exec_object *exec_objects;
90 /** Array of lists of cached gem objects of power-of-two sizes */
91 struct dri_gem_bo_bucket cache_bucket[INTEL_GEM_BO_BUCKETS];
93 struct drm_i915_gem_execbuffer exec_arg;
100 /** Boolean whether the mmap ioctl has been called for this buffer yet. */
106 * Kenel-assigned global name for this object
108 unsigned int global_name;
111 * Index of the buffer within the validation list while preparing a
112 * batchbuffer execution.
117 * Boolean whether we've started swrast
118 * Set when the buffer has been mapped
119 * Cleared when the buffer is unmapped
123 /** Array passed to the DRM containing relocation information. */
124 struct drm_i915_gem_relocation_entry *relocs;
125 /** Array of bos corresponding to relocs[i].target_handle */
126 dri_bo **reloc_target_bo;
127 /** Number of entries in relocs */
129 /** Mapped address for the buffer */
150 static struct dri_gem_bo_bucket *
151 dri_gem_bo_bucket_for_size(dri_bufmgr_gem *bufmgr_gem, unsigned long size)
155 /* We only do buckets in power of two increments */
156 if ((size & (size - 1)) != 0)
159 /* We should only see sizes rounded to pages. */
160 assert((size % 4096) == 0);
162 /* We always allocate in units of pages */
163 i = ffs(size / 4096) - 1;
164 if (i >= INTEL_GEM_BO_BUCKETS)
167 return &bufmgr_gem->cache_bucket[i];
171 static void dri_gem_dump_validation_list(dri_bufmgr_gem *bufmgr_gem)
175 for (i = 0; i < bufmgr_gem->exec_count; i++) {
176 dri_bo *bo = bufmgr_gem->exec_bos[i];
177 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
179 if (bo_gem->relocs == NULL) {
180 DBG("%2d: %d (%s)\n", i, bo_gem->gem_handle, bo_gem->name);
184 for (j = 0; j < bo_gem->reloc_count; j++) {
185 dri_bo *target_bo = bo_gem->reloc_target_bo[j];
186 dri_bo_gem *target_gem = (dri_bo_gem *)target_bo;
188 DBG("%2d: %d (%s)@0x%08llx -> %d (%s)@0x%08lx + 0x%08x\n",
190 bo_gem->gem_handle, bo_gem->name, bo_gem->relocs[j].offset,
191 target_gem->gem_handle, target_gem->name, target_bo->offset,
192 bo_gem->relocs[j].delta);
198 * Adds the given buffer to the list of buffers to be validated (moved into the
199 * appropriate memory type) with the next batch submission.
201 * If a buffer is validated multiple times in a batch submission, it ends up
202 * with the intersection of the memory type flags and the union of the
206 intel_add_validate_buffer(dri_bo *bo)
208 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
209 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
212 if (bo_gem->validate_index != -1)
215 /* Extend the array of validation entries as necessary. */
216 if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
217 int new_size = bufmgr_gem->exec_size * 2;
222 bufmgr_gem->exec_objects =
223 realloc(bufmgr_gem->exec_objects,
224 sizeof(*bufmgr_gem->exec_objects) * new_size);
225 bufmgr_gem->exec_bos =
226 realloc(bufmgr_gem->exec_bos,
227 sizeof(*bufmgr_gem->exec_bos) * new_size);
228 bufmgr_gem->exec_size = new_size;
231 index = bufmgr_gem->exec_count;
232 bo_gem->validate_index = index;
233 /* Fill in array entry */
234 bufmgr_gem->exec_objects[index].handle = bo_gem->gem_handle;
235 bufmgr_gem->exec_objects[index].relocation_count = bo_gem->reloc_count;
236 bufmgr_gem->exec_objects[index].relocs_ptr = (uintptr_t)bo_gem->relocs;
237 bufmgr_gem->exec_objects[index].alignment = 0;
238 bufmgr_gem->exec_objects[index].offset = 0;
239 bufmgr_gem->exec_bos[index] = bo;
240 dri_bo_reference(bo);
241 bufmgr_gem->exec_count++;
245 #define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \
249 intel_setup_reloc_list(dri_bo *bo)
251 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
252 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
254 bo_gem->relocs = malloc(bufmgr_gem->max_relocs *
255 sizeof(struct drm_i915_gem_relocation_entry));
256 bo_gem->reloc_target_bo = malloc(bufmgr_gem->max_relocs * sizeof(dri_bo *));
262 dri_gem_bo_alloc(dri_bufmgr *bufmgr, const char *name,
263 unsigned long size, unsigned int alignment)
265 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr;
267 unsigned int page_size = getpagesize();
269 struct dri_gem_bo_bucket *bucket;
270 int alloc_from_cache = 0;
271 unsigned long bo_size;
273 /* Round the allocated size up to a power of two number of pages. */
274 bo_size = 1 << logbase2(size);
275 if (bo_size < page_size)
277 bucket = dri_gem_bo_bucket_for_size(bufmgr_gem, bo_size);
279 /* If we don't have caching at this size, don't actually round the
282 if (bucket == NULL || bucket->max_entries == 0) {
284 if (bo_size < page_size)
288 /* Get a buffer out of the cache if available */
289 if (bucket != NULL && bucket->num_entries > 0) {
290 struct drm_i915_gem_busy busy;
292 bo_gem = bucket->head;
293 busy.handle = bo_gem->gem_handle;
295 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
296 alloc_from_cache = (ret == 0 && busy.busy == 0);
298 if (alloc_from_cache) {
299 bucket->head = bo_gem->next;
300 if (bo_gem->next == NULL)
301 bucket->tail = &bucket->head;
302 bucket->num_entries--;
306 if (!alloc_from_cache) {
307 struct drm_i915_gem_create create;
309 bo_gem = calloc(1, sizeof(*bo_gem));
313 bo_gem->bo.size = bo_size;
314 memset(&create, 0, sizeof(create));
315 create.size = bo_size;
317 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_CREATE, &create);
318 bo_gem->gem_handle = create.handle;
323 bo_gem->bo.bufmgr = bufmgr;
327 bo_gem->refcount = 1;
328 bo_gem->validate_index = -1;
330 DBG("bo_create: buf %d (%s) %ldb\n",
331 bo_gem->gem_handle, bo_gem->name, size);
337 * Returns a dri_bo wrapping the given buffer object handle.
339 * This can be used when one application needs to pass a buffer object
343 intel_bo_gem_create_from_name(dri_bufmgr *bufmgr, const char *name,
346 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr;
349 struct drm_gem_open open_arg;
351 bo_gem = calloc(1, sizeof(*bo_gem));
355 memset(&open_arg, 0, sizeof(open_arg));
356 open_arg.name = handle;
357 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_OPEN, &open_arg);
359 fprintf(stderr, "Couldn't reference %s handle 0x%08x: %s\n",
360 name, handle, strerror(-ret));
364 bo_gem->bo.size = open_arg.size;
365 bo_gem->bo.offset = 0;
366 bo_gem->bo.virtual = NULL;
367 bo_gem->bo.bufmgr = bufmgr;
369 bo_gem->refcount = 1;
370 bo_gem->validate_index = -1;
371 bo_gem->gem_handle = open_arg.handle;
373 DBG("bo_create_from_handle: %d (%s)\n", handle, bo_gem->name);
379 dri_gem_bo_reference(dri_bo *bo)
381 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
387 dri_gem_bo_free(dri_bo *bo)
389 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
390 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
391 struct drm_gem_close close;
395 munmap (bo_gem->virtual, bo_gem->bo.size);
397 /* Close this object */
398 close.handle = bo_gem->gem_handle;
399 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close);
402 "DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
403 bo_gem->gem_handle, bo_gem->name, strerror(-ret));
409 dri_gem_bo_unreference(dri_bo *bo)
411 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
412 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
417 if (--bo_gem->refcount == 0) {
418 struct dri_gem_bo_bucket *bucket;
420 if (bo_gem->relocs != NULL) {
423 /* Unreference all the target buffers */
424 for (i = 0; i < bo_gem->reloc_count; i++)
425 dri_bo_unreference(bo_gem->reloc_target_bo[i]);
426 free(bo_gem->reloc_target_bo);
427 free(bo_gem->relocs);
430 DBG("bo_unreference final: %d (%s)\n",
431 bo_gem->gem_handle, bo_gem->name);
433 bucket = dri_gem_bo_bucket_for_size(bufmgr_gem, bo->size);
434 /* Put the buffer into our internal cache for reuse if we can. */
435 if (bucket != NULL &&
436 (bucket->max_entries == -1 ||
437 (bucket->max_entries > 0 &&
438 bucket->num_entries < bucket->max_entries)))
441 bo_gem->validate_index = -1;
442 bo_gem->relocs = NULL;
443 bo_gem->reloc_target_bo = NULL;
444 bo_gem->reloc_count = 0;
447 *bucket->tail = bo_gem;
448 bucket->tail = &bo_gem->next;
449 bucket->num_entries++;
459 dri_gem_bo_map(dri_bo *bo, int write_enable)
461 dri_bufmgr_gem *bufmgr_gem;
462 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
463 struct drm_i915_gem_set_domain set_domain;
466 bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
468 /* Allow recursive mapping. Mesa may recursively map buffers with
469 * nested display loops.
471 if (!bo_gem->mapped) {
473 assert(bo->virtual == NULL);
475 DBG("bo_map: %d (%s)\n", bo_gem->gem_handle, bo_gem->name);
477 if (bo_gem->virtual == NULL) {
478 struct drm_i915_gem_mmap mmap_arg;
480 memset(&mmap_arg, 0, sizeof(mmap_arg));
481 mmap_arg.handle = bo_gem->gem_handle;
483 mmap_arg.size = bo->size;
484 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg);
486 fprintf(stderr, "%s:%d: Error mapping buffer %d (%s): %s .\n",
488 bo_gem->gem_handle, bo_gem->name, strerror(errno));
490 bo_gem->virtual = (void *)(uintptr_t)mmap_arg.addr_ptr;
492 bo->virtual = bo_gem->virtual;
495 DBG("bo_map: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name, bo_gem->virtual);
498 if (!bo_gem->swrast) {
499 set_domain.handle = bo_gem->gem_handle;
500 set_domain.read_domains = I915_GEM_DOMAIN_CPU;
502 set_domain.write_domain = I915_GEM_DOMAIN_CPU;
504 set_domain.write_domain = 0;
506 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN,
508 } while (ret == -1 && errno == EINTR);
510 fprintf (stderr, "%s:%d: Error setting swrast %d: %s\n",
511 __FILE__, __LINE__, bo_gem->gem_handle, strerror (errno));
520 dri_gem_bo_unmap(dri_bo *bo)
522 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
523 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
524 struct drm_i915_gem_sw_finish sw_finish;
530 assert(bo_gem->mapped);
532 if (bo_gem->swrast) {
533 sw_finish.handle = bo_gem->gem_handle;
535 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_SW_FINISH,
537 } while (ret == -1 && errno == EINTR);
544 dri_gem_bo_subdata (dri_bo *bo, unsigned long offset,
545 unsigned long size, const void *data)
547 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
548 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
549 struct drm_i915_gem_pwrite pwrite;
552 memset (&pwrite, 0, sizeof (pwrite));
553 pwrite.handle = bo_gem->gem_handle;
554 pwrite.offset = offset;
556 pwrite.data_ptr = (uint64_t) (uintptr_t) data;
558 ret = ioctl (bufmgr_gem->fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite);
559 } while (ret == -1 && errno == EINTR);
561 fprintf (stderr, "%s:%d: Error writing data to buffer %d: (%d %d) %s .\n",
563 bo_gem->gem_handle, (int) offset, (int) size,
570 dri_gem_bo_get_subdata (dri_bo *bo, unsigned long offset,
571 unsigned long size, void *data)
573 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
574 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
575 struct drm_i915_gem_pread pread;
578 memset (&pread, 0, sizeof (pread));
579 pread.handle = bo_gem->gem_handle;
580 pread.offset = offset;
582 pread.data_ptr = (uint64_t) (uintptr_t) data;
584 ret = ioctl (bufmgr_gem->fd, DRM_IOCTL_I915_GEM_PREAD, &pread);
585 } while (ret == -1 && errno == EINTR);
587 fprintf (stderr, "%s:%d: Error reading data from buffer %d: (%d %d) %s .\n",
589 bo_gem->gem_handle, (int) offset, (int) size,
596 dri_gem_bo_wait_rendering(dri_bo *bo)
598 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
599 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
600 struct drm_i915_gem_set_domain set_domain;
603 set_domain.handle = bo_gem->gem_handle;
604 set_domain.read_domains = I915_GEM_DOMAIN_GTT;
605 set_domain.write_domain = 0;
606 ret = ioctl (bufmgr_gem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
608 fprintf (stderr, "%s:%d: Error setting memory domains %d (%08x %08x): %s .\n",
610 bo_gem->gem_handle, set_domain.read_domains, set_domain.write_domain,
616 dri_bufmgr_gem_destroy(dri_bufmgr *bufmgr)
618 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr;
621 free(bufmgr_gem->exec_objects);
622 free(bufmgr_gem->exec_bos);
624 /* Free any cached buffer objects we were going to reuse */
625 for (i = 0; i < INTEL_GEM_BO_BUCKETS; i++) {
626 struct dri_gem_bo_bucket *bucket = &bufmgr_gem->cache_bucket[i];
629 while ((bo_gem = bucket->head) != NULL) {
630 bucket->head = bo_gem->next;
631 if (bo_gem->next == NULL)
632 bucket->tail = &bucket->head;
633 bucket->num_entries--;
635 dri_gem_bo_free(&bo_gem->bo);
643 * Adds the target buffer to the validation list and adds the relocation
644 * to the reloc_buffer's relocation list.
646 * The relocation entry at the given offset must already contain the
647 * precomputed relocation value, because the kernel will optimize out
648 * the relocation entry write when the buffer hasn't moved from the
649 * last known offset in target_bo.
652 dri_gem_emit_reloc(dri_bo *bo, uint32_t read_domains, uint32_t write_domain,
653 uint32_t delta, uint32_t offset, dri_bo *target_bo)
655 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
656 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
657 dri_bo_gem *target_bo_gem = (dri_bo_gem *)target_bo;
659 /* Create a new relocation list if needed */
660 if (bo_gem->relocs == NULL)
661 intel_setup_reloc_list(bo);
664 assert(bo_gem->reloc_count < bufmgr_gem->max_relocs);
667 assert (offset <= bo->size - 4);
668 assert ((write_domain & (write_domain-1)) == 0);
670 bo_gem->relocs[bo_gem->reloc_count].offset = offset;
671 bo_gem->relocs[bo_gem->reloc_count].delta = delta;
672 bo_gem->relocs[bo_gem->reloc_count].target_handle =
673 target_bo_gem->gem_handle;
674 bo_gem->relocs[bo_gem->reloc_count].read_domains = read_domains;
675 bo_gem->relocs[bo_gem->reloc_count].write_domain = write_domain;
676 bo_gem->relocs[bo_gem->reloc_count].presumed_offset = target_bo->offset;
678 bo_gem->reloc_target_bo[bo_gem->reloc_count] = target_bo;
679 dri_bo_reference(target_bo);
681 bo_gem->reloc_count++;
686 * Walk the tree of relocations rooted at BO and accumulate the list of
687 * validations to be performed and update the relocation buffers with
688 * index values into the validation list.
691 dri_gem_bo_process_reloc(dri_bo *bo)
693 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
696 if (bo_gem->relocs == NULL)
699 for (i = 0; i < bo_gem->reloc_count; i++) {
700 dri_bo *target_bo = bo_gem->reloc_target_bo[i];
702 /* Continue walking the tree depth-first. */
703 dri_gem_bo_process_reloc(target_bo);
705 /* Add the target to the validate list */
706 intel_add_validate_buffer(target_bo);
711 dri_gem_process_reloc(dri_bo *batch_buf)
713 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *) batch_buf->bufmgr;
715 /* Update indices and set up the validate list. */
716 dri_gem_bo_process_reloc(batch_buf);
718 /* Add the batch buffer to the validation list. There are no relocations
721 intel_add_validate_buffer(batch_buf);
723 bufmgr_gem->exec_arg.buffers_ptr = (uintptr_t)bufmgr_gem->exec_objects;
724 bufmgr_gem->exec_arg.buffer_count = bufmgr_gem->exec_count;
725 bufmgr_gem->exec_arg.batch_start_offset = 0;
726 bufmgr_gem->exec_arg.batch_len = 0; /* written in intel_exec_ioctl */
728 return &bufmgr_gem->exec_arg;
732 intel_update_buffer_offsets (dri_bufmgr_gem *bufmgr_gem)
736 for (i = 0; i < bufmgr_gem->exec_count; i++) {
737 dri_bo *bo = bufmgr_gem->exec_bos[i];
738 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
740 /* Update the buffer offset */
741 if (bufmgr_gem->exec_objects[i].offset != bo->offset) {
742 DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n",
743 bo_gem->gem_handle, bo_gem->name, bo->offset,
744 bufmgr_gem->exec_objects[i].offset);
745 bo->offset = bufmgr_gem->exec_objects[i].offset;
751 dri_gem_post_submit(dri_bo *batch_buf)
753 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)batch_buf->bufmgr;
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;
777 dri_gem_pin(dri_bo *bo, uint32_t alignment)
779 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
780 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
781 struct drm_i915_gem_pin pin;
784 pin.handle = bo_gem->gem_handle;
785 pin.alignment = alignment;
787 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_PIN, &pin);
791 bo->offset = pin.offset;
796 dri_gem_unpin(dri_bo *bo)
798 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
799 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
800 struct drm_i915_gem_unpin unpin;
803 unpin.handle = bo_gem->gem_handle;
805 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_UNPIN, &unpin);
813 dri_gem_set_tiling(dri_bo *bo, uint32_t *tiling_mode)
815 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
816 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
817 struct drm_i915_gem_set_tiling set_tiling;
820 set_tiling.handle = bo_gem->gem_handle;
821 set_tiling.tiling_mode = *tiling_mode;
823 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
825 *tiling_mode = I915_TILING_NONE;
829 *tiling_mode = set_tiling.tiling_mode;
834 dri_gem_flink(dri_bo *bo, uint32_t *name)
836 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
837 dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
838 struct drm_gem_flink flink;
841 if (!bo_gem->global_name) {
842 flink.handle = bo_gem->gem_handle;
844 ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_FLINK, &flink);
847 bo_gem->global_name = flink.name;
850 *name = bo_gem->global_name;
855 * Enables unlimited caching of buffer objects for reuse.
857 * This is potentially very memory expensive, as the cache at each bucket
858 * size is only bounded by how many buffers of that size we've managed to have
862 intel_bufmgr_gem_enable_reuse(dri_bufmgr *bufmgr)
864 dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr;
867 for (i = 0; i < INTEL_GEM_BO_BUCKETS; i++) {
868 bufmgr_gem->cache_bucket[i].max_entries = -1;
876 dri_gem_check_aperture_space(dri_bo **bo_array, int count)
882 * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
883 * and manage map buffer objections.
885 * \param fd File descriptor of the opened DRM device.
888 intel_bufmgr_gem_init(int fd, int batch_size)
890 dri_bufmgr_gem *bufmgr_gem;
893 bufmgr_gem = calloc(1, sizeof(*bufmgr_gem));
896 /* Let's go with one relocation per every 2 dwords (but round down a bit
897 * since a power of two will mean an extra page allocation for the reloc
900 * Every 4 was too few for the blender benchmark.
902 bufmgr_gem->max_relocs = batch_size / sizeof(uint32_t) / 2 - 2;
904 bufmgr_gem->bufmgr.bo_alloc = dri_gem_bo_alloc;
905 bufmgr_gem->bufmgr.bo_reference = dri_gem_bo_reference;
906 bufmgr_gem->bufmgr.bo_unreference = dri_gem_bo_unreference;
907 bufmgr_gem->bufmgr.bo_map = dri_gem_bo_map;
908 bufmgr_gem->bufmgr.bo_unmap = dri_gem_bo_unmap;
909 bufmgr_gem->bufmgr.bo_subdata = dri_gem_bo_subdata;
910 bufmgr_gem->bufmgr.bo_get_subdata = dri_gem_bo_get_subdata;
911 bufmgr_gem->bufmgr.bo_wait_rendering = dri_gem_bo_wait_rendering;
912 bufmgr_gem->bufmgr.destroy = dri_bufmgr_gem_destroy;
913 bufmgr_gem->bufmgr.process_relocs = dri_gem_process_reloc;
914 bufmgr_gem->bufmgr.post_submit = dri_gem_post_submit;
915 bufmgr_gem->bufmgr.debug = 0;
916 bufmgr_gem->bufmgr.check_aperture_space = dri_gem_check_aperture_space;
917 bufmgr_gem->intel_bufmgr.emit_reloc = dri_gem_emit_reloc;
918 bufmgr_gem->intel_bufmgr.pin = dri_gem_pin;
919 bufmgr_gem->intel_bufmgr.unpin = dri_gem_unpin;
920 bufmgr_gem->intel_bufmgr.set_tiling = dri_gem_set_tiling;
921 bufmgr_gem->intel_bufmgr.flink = dri_gem_flink;
922 /* Initialize the linked lists for BO reuse cache. */
923 for (i = 0; i < INTEL_GEM_BO_BUCKETS; i++)
924 bufmgr_gem->cache_bucket[i].tail = &bufmgr_gem->cache_bucket[i].head;
926 return &bufmgr_gem->bufmgr;
930 intel_bo_emit_reloc(dri_bo *reloc_buf,
931 uint32_t read_domains, uint32_t write_domain,
932 uint32_t delta, uint32_t offset, dri_bo *target_buf)
934 struct intel_bufmgr *intel_bufmgr;
936 intel_bufmgr = (struct intel_bufmgr *)(reloc_buf->bufmgr + 1);
938 return intel_bufmgr->emit_reloc(reloc_buf, read_domains, write_domain,
939 delta, offset, target_buf);
943 intel_bo_pin(dri_bo *bo, uint32_t alignment)
945 struct intel_bufmgr *intel_bufmgr;
947 intel_bufmgr = (struct intel_bufmgr *)(bo->bufmgr + 1);
949 if (intel_bufmgr->pin)
950 return intel_bufmgr->pin(bo, alignment);
956 intel_bo_unpin(dri_bo *bo)
958 struct intel_bufmgr *intel_bufmgr;
960 intel_bufmgr = (struct intel_bufmgr *)(bo->bufmgr + 1);
962 if (intel_bufmgr->unpin)
963 return intel_bufmgr->unpin(bo);
968 int intel_bo_set_tiling(dri_bo *bo, uint32_t *tiling_mode)
970 struct intel_bufmgr *intel_bufmgr;
972 intel_bufmgr = (struct intel_bufmgr *)(bo->bufmgr + 1);
974 if (intel_bufmgr->set_tiling)
975 return intel_bufmgr->set_tiling (bo, tiling_mode);
977 *tiling_mode = I915_TILING_NONE;
981 int intel_bo_flink(dri_bo *bo, uint32_t *name)
983 struct intel_bufmgr *intel_bufmgr;
985 intel_bufmgr = (struct intel_bufmgr *)(bo->bufmgr + 1);
987 if (intel_bufmgr->flink)
988 return intel_bufmgr->flink (bo, name);