i965: Pass the EGL/DRI context priority through to the kernel
[mesa.git] / src / mesa / drivers / dri / i965 / brw_bufmgr.c
1 /*
2 * Copyright © 2007 Red Hat Inc.
3 * Copyright © 2007-2017 Intel Corporation
4 * Copyright © 2006 VMware, Inc.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * IN THE SOFTWARE.
25 */
26
27 /*
28 * Authors: Thomas Hellström <thellstrom@vmware.com>
29 * Keith Whitwell <keithw@vmware.com>
30 * Eric Anholt <eric@anholt.net>
31 * Dave Airlie <airlied@linux.ie>
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include <xf86drm.h>
39 #include <util/u_atomic.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 #include <assert.h>
46 #include <sys/ioctl.h>
47 #include <sys/stat.h>
48 #include <sys/types.h>
49 #include <stdbool.h>
50
51 #include "errno.h"
52 #ifndef ETIME
53 #define ETIME ETIMEDOUT
54 #endif
55 #include "common/gen_clflush.h"
56 #include "common/gen_debug.h"
57 #include "common/gen_device_info.h"
58 #include "libdrm_macros.h"
59 #include "main/macros.h"
60 #include "util/macros.h"
61 #include "util/hash_table.h"
62 #include "util/list.h"
63 #include "brw_bufmgr.h"
64 #include "brw_context.h"
65 #include "string.h"
66
67 #include "i915_drm.h"
68
69 #ifdef HAVE_VALGRIND
70 #include <valgrind.h>
71 #include <memcheck.h>
72 #define VG(x) x
73 #else
74 #define VG(x)
75 #endif
76
77 /* VALGRIND_FREELIKE_BLOCK unfortunately does not actually undo the earlier
78 * VALGRIND_MALLOCLIKE_BLOCK but instead leaves vg convinced the memory is
79 * leaked. All because it does not call VG(cli_free) from its
80 * VG_USERREQ__FREELIKE_BLOCK handler. Instead of treating the memory like
81 * and allocation, we mark it available for use upon mmapping and remove
82 * it upon unmapping.
83 */
84 #define VG_DEFINED(ptr, size) VG(VALGRIND_MAKE_MEM_DEFINED(ptr, size))
85 #define VG_NOACCESS(ptr, size) VG(VALGRIND_MAKE_MEM_NOACCESS(ptr, size))
86
87 #define memclear(s) memset(&s, 0, sizeof(s))
88
89 #define FILE_DEBUG_FLAG DEBUG_BUFMGR
90
91 static inline int
92 atomic_add_unless(int *v, int add, int unless)
93 {
94 int c, old;
95 c = p_atomic_read(v);
96 while (c != unless && (old = p_atomic_cmpxchg(v, c, c + add)) != c)
97 c = old;
98 return c == unless;
99 }
100
101 struct bo_cache_bucket {
102 struct list_head head;
103 uint64_t size;
104 };
105
106 struct brw_bufmgr {
107 int fd;
108
109 mtx_t lock;
110
111 /** Array of lists of cached gem objects of power-of-two sizes */
112 struct bo_cache_bucket cache_bucket[14 * 4];
113 int num_buckets;
114 time_t time;
115
116 struct hash_table *name_table;
117 struct hash_table *handle_table;
118
119 bool has_llc:1;
120 bool has_mmap_wc:1;
121 bool bo_reuse:1;
122 };
123
124 static int bo_set_tiling_internal(struct brw_bo *bo, uint32_t tiling_mode,
125 uint32_t stride);
126
127 static void bo_free(struct brw_bo *bo);
128
129 static uint32_t
130 key_hash_uint(const void *key)
131 {
132 return _mesa_hash_data(key, 4);
133 }
134
135 static bool
136 key_uint_equal(const void *a, const void *b)
137 {
138 return *((unsigned *) a) == *((unsigned *) b);
139 }
140
141 static struct brw_bo *
142 hash_find_bo(struct hash_table *ht, unsigned int key)
143 {
144 struct hash_entry *entry = _mesa_hash_table_search(ht, &key);
145 return entry ? (struct brw_bo *) entry->data : NULL;
146 }
147
148 static uint64_t
149 bo_tile_size(struct brw_bufmgr *bufmgr, uint64_t size, uint32_t tiling)
150 {
151 if (tiling == I915_TILING_NONE)
152 return size;
153
154 /* 965+ just need multiples of page size for tiling */
155 return ALIGN(size, 4096);
156 }
157
158 /*
159 * Round a given pitch up to the minimum required for X tiling on a
160 * given chip. We use 512 as the minimum to allow for a later tiling
161 * change.
162 */
163 static uint32_t
164 bo_tile_pitch(struct brw_bufmgr *bufmgr, uint32_t pitch, uint32_t tiling)
165 {
166 unsigned long tile_width;
167
168 /* If untiled, then just align it so that we can do rendering
169 * to it with the 3D engine.
170 */
171 if (tiling == I915_TILING_NONE)
172 return ALIGN(pitch, 64);
173
174 if (tiling == I915_TILING_X)
175 tile_width = 512;
176 else
177 tile_width = 128;
178
179 /* 965 is flexible */
180 return ALIGN(pitch, tile_width);
181 }
182
183 static struct bo_cache_bucket *
184 bucket_for_size(struct brw_bufmgr *bufmgr, uint64_t size)
185 {
186 int i;
187
188 for (i = 0; i < bufmgr->num_buckets; i++) {
189 struct bo_cache_bucket *bucket = &bufmgr->cache_bucket[i];
190 if (bucket->size >= size) {
191 return bucket;
192 }
193 }
194
195 return NULL;
196 }
197
198 int
199 brw_bo_busy(struct brw_bo *bo)
200 {
201 struct brw_bufmgr *bufmgr = bo->bufmgr;
202 struct drm_i915_gem_busy busy;
203 int ret;
204
205 memclear(busy);
206 busy.handle = bo->gem_handle;
207
208 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
209 if (ret == 0) {
210 bo->idle = !busy.busy;
211 return busy.busy;
212 }
213 return false;
214 }
215
216 int
217 brw_bo_madvise(struct brw_bo *bo, int state)
218 {
219 struct drm_i915_gem_madvise madv;
220
221 memclear(madv);
222 madv.handle = bo->gem_handle;
223 madv.madv = state;
224 madv.retained = 1;
225 drmIoctl(bo->bufmgr->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv);
226
227 return madv.retained;
228 }
229
230 /* drop the oldest entries that have been purged by the kernel */
231 static void
232 brw_bo_cache_purge_bucket(struct brw_bufmgr *bufmgr,
233 struct bo_cache_bucket *bucket)
234 {
235 list_for_each_entry_safe(struct brw_bo, bo, &bucket->head, head) {
236 if (brw_bo_madvise(bo, I915_MADV_DONTNEED))
237 break;
238
239 list_del(&bo->head);
240 bo_free(bo);
241 }
242 }
243
244 static struct brw_bo *
245 bo_alloc_internal(struct brw_bufmgr *bufmgr,
246 const char *name,
247 uint64_t size,
248 unsigned flags,
249 uint32_t tiling_mode,
250 uint32_t stride, uint64_t alignment)
251 {
252 struct brw_bo *bo;
253 unsigned int page_size = getpagesize();
254 int ret;
255 struct bo_cache_bucket *bucket;
256 bool alloc_from_cache;
257 uint64_t bo_size;
258 bool busy = false;
259 bool zeroed = false;
260
261 if (flags & BO_ALLOC_BUSY)
262 busy = true;
263
264 if (flags & BO_ALLOC_ZEROED)
265 zeroed = true;
266
267 /* BUSY does doesn't really jive with ZEROED as we have to wait for it to
268 * be idle before we can memset. Just disallow that combination.
269 */
270 assert(!(busy && zeroed));
271
272 /* Round the allocated size up to a power of two number of pages. */
273 bucket = bucket_for_size(bufmgr, size);
274
275 /* If we don't have caching at this size, don't actually round the
276 * allocation up.
277 */
278 if (bucket == NULL) {
279 bo_size = size;
280 if (bo_size < page_size)
281 bo_size = page_size;
282 } else {
283 bo_size = bucket->size;
284 }
285
286 mtx_lock(&bufmgr->lock);
287 /* Get a buffer out of the cache if available */
288 retry:
289 alloc_from_cache = false;
290 if (bucket != NULL && !list_empty(&bucket->head)) {
291 if (busy && !zeroed) {
292 /* Allocate new render-target BOs from the tail (MRU)
293 * of the list, as it will likely be hot in the GPU
294 * cache and in the aperture for us. If the caller
295 * asked us to zero the buffer, we don't want this
296 * because we are going to mmap it.
297 */
298 bo = LIST_ENTRY(struct brw_bo, bucket->head.prev, head);
299 list_del(&bo->head);
300 alloc_from_cache = true;
301 bo->align = alignment;
302 } else {
303 assert(alignment == 0);
304 /* For non-render-target BOs (where we're probably
305 * going to map it first thing in order to fill it
306 * with data), check if the last BO in the cache is
307 * unbusy, and only reuse in that case. Otherwise,
308 * allocating a new buffer is probably faster than
309 * waiting for the GPU to finish.
310 */
311 bo = LIST_ENTRY(struct brw_bo, bucket->head.next, head);
312 if (!brw_bo_busy(bo)) {
313 alloc_from_cache = true;
314 list_del(&bo->head);
315 }
316 }
317
318 if (alloc_from_cache) {
319 if (!brw_bo_madvise(bo, I915_MADV_WILLNEED)) {
320 bo_free(bo);
321 brw_bo_cache_purge_bucket(bufmgr, bucket);
322 goto retry;
323 }
324
325 if (bo_set_tiling_internal(bo, tiling_mode, stride)) {
326 bo_free(bo);
327 goto retry;
328 }
329
330 if (zeroed) {
331 void *map = brw_bo_map(NULL, bo, MAP_WRITE | MAP_RAW);
332 if (!map) {
333 bo_free(bo);
334 goto retry;
335 }
336 memset(map, 0, bo_size);
337 }
338 }
339 }
340
341 if (!alloc_from_cache) {
342 struct drm_i915_gem_create create;
343
344 bo = calloc(1, sizeof(*bo));
345 if (!bo)
346 goto err;
347
348 bo->size = bo_size;
349 bo->idle = true;
350
351 memclear(create);
352 create.size = bo_size;
353
354 /* All new BOs we get from the kernel are zeroed, so we don't need to
355 * worry about that here.
356 */
357 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_CREATE, &create);
358 if (ret != 0) {
359 free(bo);
360 goto err;
361 }
362
363 bo->gem_handle = create.handle;
364
365 bo->bufmgr = bufmgr;
366 bo->align = alignment;
367
368 bo->tiling_mode = I915_TILING_NONE;
369 bo->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
370 bo->stride = 0;
371
372 if (bo_set_tiling_internal(bo, tiling_mode, stride))
373 goto err_free;
374
375 /* Calling set_domain() will allocate pages for the BO outside of the
376 * struct mutex lock in the kernel, which is more efficient than waiting
377 * to create them during the first execbuf that uses the BO.
378 */
379 struct drm_i915_gem_set_domain sd = {
380 .handle = bo->gem_handle,
381 .read_domains = I915_GEM_DOMAIN_CPU,
382 .write_domain = 0,
383 };
384
385 if (drmIoctl(bo->bufmgr->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &sd) != 0)
386 goto err_free;
387 }
388
389 bo->name = name;
390 p_atomic_set(&bo->refcount, 1);
391 bo->reusable = true;
392 bo->cache_coherent = bufmgr->has_llc;
393 bo->index = -1;
394
395 mtx_unlock(&bufmgr->lock);
396
397 DBG("bo_create: buf %d (%s) %llub\n", bo->gem_handle, bo->name,
398 (unsigned long long) size);
399
400 return bo;
401
402 err_free:
403 bo_free(bo);
404 err:
405 mtx_unlock(&bufmgr->lock);
406 return NULL;
407 }
408
409 struct brw_bo *
410 brw_bo_alloc(struct brw_bufmgr *bufmgr,
411 const char *name, uint64_t size, uint64_t alignment)
412 {
413 return bo_alloc_internal(bufmgr, name, size, 0, I915_TILING_NONE, 0, 0);
414 }
415
416 struct brw_bo *
417 brw_bo_alloc_tiled(struct brw_bufmgr *bufmgr, const char *name,
418 uint64_t size, uint32_t tiling_mode, uint32_t pitch,
419 unsigned flags)
420 {
421 return bo_alloc_internal(bufmgr, name, size, flags, tiling_mode, pitch, 0);
422 }
423
424 struct brw_bo *
425 brw_bo_alloc_tiled_2d(struct brw_bufmgr *bufmgr, const char *name,
426 int x, int y, int cpp, uint32_t tiling,
427 uint32_t *pitch, unsigned flags)
428 {
429 uint64_t size;
430 uint32_t stride;
431 unsigned long aligned_y, height_alignment;
432
433 /* If we're tiled, our allocations are in 8 or 32-row blocks,
434 * so failure to align our height means that we won't allocate
435 * enough pages.
436 *
437 * If we're untiled, we still have to align to 2 rows high
438 * because the data port accesses 2x2 blocks even if the
439 * bottom row isn't to be rendered, so failure to align means
440 * we could walk off the end of the GTT and fault. This is
441 * documented on 965, and may be the case on older chipsets
442 * too so we try to be careful.
443 */
444 aligned_y = y;
445 height_alignment = 2;
446
447 if (tiling == I915_TILING_X)
448 height_alignment = 8;
449 else if (tiling == I915_TILING_Y)
450 height_alignment = 32;
451 aligned_y = ALIGN(y, height_alignment);
452
453 stride = x * cpp;
454 stride = bo_tile_pitch(bufmgr, stride, tiling);
455 size = stride * aligned_y;
456 size = bo_tile_size(bufmgr, size, tiling);
457 *pitch = stride;
458
459 if (tiling == I915_TILING_NONE)
460 stride = 0;
461
462 return bo_alloc_internal(bufmgr, name, size, flags, tiling, stride, 0);
463 }
464
465 /**
466 * Returns a brw_bo wrapping the given buffer object handle.
467 *
468 * This can be used when one application needs to pass a buffer object
469 * to another.
470 */
471 struct brw_bo *
472 brw_bo_gem_create_from_name(struct brw_bufmgr *bufmgr,
473 const char *name, unsigned int handle)
474 {
475 struct brw_bo *bo;
476 int ret;
477 struct drm_gem_open open_arg;
478 struct drm_i915_gem_get_tiling get_tiling;
479
480 /* At the moment most applications only have a few named bo.
481 * For instance, in a DRI client only the render buffers passed
482 * between X and the client are named. And since X returns the
483 * alternating names for the front/back buffer a linear search
484 * provides a sufficiently fast match.
485 */
486 mtx_lock(&bufmgr->lock);
487 bo = hash_find_bo(bufmgr->name_table, handle);
488 if (bo) {
489 brw_bo_reference(bo);
490 goto out;
491 }
492
493 memclear(open_arg);
494 open_arg.name = handle;
495 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_GEM_OPEN, &open_arg);
496 if (ret != 0) {
497 DBG("Couldn't reference %s handle 0x%08x: %s\n",
498 name, handle, strerror(errno));
499 bo = NULL;
500 goto out;
501 }
502 /* Now see if someone has used a prime handle to get this
503 * object from the kernel before by looking through the list
504 * again for a matching gem_handle
505 */
506 bo = hash_find_bo(bufmgr->handle_table, open_arg.handle);
507 if (bo) {
508 brw_bo_reference(bo);
509 goto out;
510 }
511
512 bo = calloc(1, sizeof(*bo));
513 if (!bo)
514 goto out;
515
516 p_atomic_set(&bo->refcount, 1);
517
518 bo->size = open_arg.size;
519 bo->gtt_offset = 0;
520 bo->bufmgr = bufmgr;
521 bo->gem_handle = open_arg.handle;
522 bo->name = name;
523 bo->global_name = handle;
524 bo->reusable = false;
525 bo->external = true;
526
527 _mesa_hash_table_insert(bufmgr->handle_table, &bo->gem_handle, bo);
528 _mesa_hash_table_insert(bufmgr->name_table, &bo->global_name, bo);
529
530 memclear(get_tiling);
531 get_tiling.handle = bo->gem_handle;
532 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling);
533 if (ret != 0)
534 goto err_unref;
535
536 bo->tiling_mode = get_tiling.tiling_mode;
537 bo->swizzle_mode = get_tiling.swizzle_mode;
538 /* XXX stride is unknown */
539 DBG("bo_create_from_handle: %d (%s)\n", handle, bo->name);
540
541 out:
542 mtx_unlock(&bufmgr->lock);
543 return bo;
544
545 err_unref:
546 bo_free(bo);
547 mtx_unlock(&bufmgr->lock);
548 return NULL;
549 }
550
551 static void
552 bo_free(struct brw_bo *bo)
553 {
554 struct brw_bufmgr *bufmgr = bo->bufmgr;
555 struct drm_gem_close close;
556 int ret;
557
558 if (bo->map_cpu) {
559 VG_NOACCESS(bo->map_cpu, bo->size);
560 drm_munmap(bo->map_cpu, bo->size);
561 }
562 if (bo->map_wc) {
563 VG_NOACCESS(bo->map_wc, bo->size);
564 drm_munmap(bo->map_wc, bo->size);
565 }
566 if (bo->map_gtt) {
567 VG_NOACCESS(bo->map_gtt, bo->size);
568 drm_munmap(bo->map_gtt, bo->size);
569 }
570
571 if (bo->external) {
572 struct hash_entry *entry;
573
574 if (bo->global_name) {
575 entry = _mesa_hash_table_search(bufmgr->name_table, &bo->global_name);
576 _mesa_hash_table_remove(bufmgr->name_table, entry);
577 }
578
579 entry = _mesa_hash_table_search(bufmgr->handle_table, &bo->gem_handle);
580 _mesa_hash_table_remove(bufmgr->handle_table, entry);
581 }
582
583 /* Close this object */
584 memclear(close);
585 close.handle = bo->gem_handle;
586 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_GEM_CLOSE, &close);
587 if (ret != 0) {
588 DBG("DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
589 bo->gem_handle, bo->name, strerror(errno));
590 }
591 free(bo);
592 }
593
594 /** Frees all cached buffers significantly older than @time. */
595 static void
596 cleanup_bo_cache(struct brw_bufmgr *bufmgr, time_t time)
597 {
598 int i;
599
600 if (bufmgr->time == time)
601 return;
602
603 for (i = 0; i < bufmgr->num_buckets; i++) {
604 struct bo_cache_bucket *bucket = &bufmgr->cache_bucket[i];
605
606 list_for_each_entry_safe(struct brw_bo, bo, &bucket->head, head) {
607 if (time - bo->free_time <= 1)
608 break;
609
610 list_del(&bo->head);
611
612 bo_free(bo);
613 }
614 }
615
616 bufmgr->time = time;
617 }
618
619 static void
620 bo_unreference_final(struct brw_bo *bo, time_t time)
621 {
622 struct brw_bufmgr *bufmgr = bo->bufmgr;
623 struct bo_cache_bucket *bucket;
624
625 DBG("bo_unreference final: %d (%s)\n", bo->gem_handle, bo->name);
626
627 bucket = bucket_for_size(bufmgr, bo->size);
628 /* Put the buffer into our internal cache for reuse if we can. */
629 if (bufmgr->bo_reuse && bo->reusable && bucket != NULL &&
630 brw_bo_madvise(bo, I915_MADV_DONTNEED)) {
631 bo->free_time = time;
632
633 bo->name = NULL;
634 bo->kflags = 0;
635
636 list_addtail(&bo->head, &bucket->head);
637 } else {
638 bo_free(bo);
639 }
640 }
641
642 void
643 brw_bo_unreference(struct brw_bo *bo)
644 {
645 if (bo == NULL)
646 return;
647
648 assert(p_atomic_read(&bo->refcount) > 0);
649
650 if (atomic_add_unless(&bo->refcount, -1, 1)) {
651 struct brw_bufmgr *bufmgr = bo->bufmgr;
652 struct timespec time;
653
654 clock_gettime(CLOCK_MONOTONIC, &time);
655
656 mtx_lock(&bufmgr->lock);
657
658 if (p_atomic_dec_zero(&bo->refcount)) {
659 bo_unreference_final(bo, time.tv_sec);
660 cleanup_bo_cache(bufmgr, time.tv_sec);
661 }
662
663 mtx_unlock(&bufmgr->lock);
664 }
665 }
666
667 static void
668 bo_wait_with_stall_warning(struct brw_context *brw,
669 struct brw_bo *bo,
670 const char *action)
671 {
672 bool busy = brw && brw->perf_debug && !bo->idle;
673 double elapsed = unlikely(busy) ? -get_time() : 0.0;
674
675 brw_bo_wait_rendering(bo);
676
677 if (unlikely(busy)) {
678 elapsed += get_time();
679 if (elapsed > 1e-5) /* 0.01ms */
680 perf_debug("%s a busy \"%s\" BO stalled and took %.03f ms.\n",
681 action, bo->name, elapsed * 1000);
682 }
683 }
684
685 static void
686 print_flags(unsigned flags)
687 {
688 if (flags & MAP_READ)
689 DBG("READ ");
690 if (flags & MAP_WRITE)
691 DBG("WRITE ");
692 if (flags & MAP_ASYNC)
693 DBG("ASYNC ");
694 if (flags & MAP_PERSISTENT)
695 DBG("PERSISTENT ");
696 if (flags & MAP_COHERENT)
697 DBG("COHERENT ");
698 if (flags & MAP_RAW)
699 DBG("RAW ");
700 DBG("\n");
701 }
702
703 static void *
704 brw_bo_map_cpu(struct brw_context *brw, struct brw_bo *bo, unsigned flags)
705 {
706 struct brw_bufmgr *bufmgr = bo->bufmgr;
707
708 /* We disallow CPU maps for writing to non-coherent buffers, as the
709 * CPU map can become invalidated when a batch is flushed out, which
710 * can happen at unpredictable times. You should use WC maps instead.
711 */
712 assert(bo->cache_coherent || !(flags & MAP_WRITE));
713
714 if (!bo->map_cpu) {
715 struct drm_i915_gem_mmap mmap_arg;
716 void *map;
717
718 DBG("brw_bo_map_cpu: %d (%s)\n", bo->gem_handle, bo->name);
719
720 memclear(mmap_arg);
721 mmap_arg.handle = bo->gem_handle;
722 mmap_arg.size = bo->size;
723 int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg);
724 if (ret != 0) {
725 ret = -errno;
726 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
727 __FILE__, __LINE__, bo->gem_handle, bo->name, strerror(errno));
728 return NULL;
729 }
730 map = (void *) (uintptr_t) mmap_arg.addr_ptr;
731 VG_DEFINED(map, bo->size);
732
733 if (p_atomic_cmpxchg(&bo->map_cpu, NULL, map)) {
734 VG_NOACCESS(map, bo->size);
735 drm_munmap(map, bo->size);
736 }
737 }
738 assert(bo->map_cpu);
739
740 DBG("brw_bo_map_cpu: %d (%s) -> %p, ", bo->gem_handle, bo->name,
741 bo->map_cpu);
742 print_flags(flags);
743
744 if (!(flags & MAP_ASYNC)) {
745 bo_wait_with_stall_warning(brw, bo, "CPU mapping");
746 }
747
748 if (!bo->cache_coherent && !bo->bufmgr->has_llc) {
749 /* If we're reusing an existing CPU mapping, the CPU caches may
750 * contain stale data from the last time we read from that mapping.
751 * (With the BO cache, it might even be data from a previous buffer!)
752 * Even if it's a brand new mapping, the kernel may have zeroed the
753 * buffer via CPU writes.
754 *
755 * We need to invalidate those cachelines so that we see the latest
756 * contents, and so long as we only read from the CPU mmap we do not
757 * need to write those cachelines back afterwards.
758 *
759 * On LLC, the emprical evidence suggests that writes from the GPU
760 * that bypass the LLC (i.e. for scanout) do *invalidate* the CPU
761 * cachelines. (Other reads, such as the display engine, bypass the
762 * LLC entirely requiring us to keep dirty pixels for the scanout
763 * out of any cache.)
764 */
765 gen_invalidate_range(bo->map_cpu, bo->size);
766 }
767
768 return bo->map_cpu;
769 }
770
771 static void *
772 brw_bo_map_wc(struct brw_context *brw, struct brw_bo *bo, unsigned flags)
773 {
774 struct brw_bufmgr *bufmgr = bo->bufmgr;
775
776 if (!bufmgr->has_mmap_wc)
777 return NULL;
778
779 if (!bo->map_wc) {
780 struct drm_i915_gem_mmap mmap_arg;
781 void *map;
782
783 DBG("brw_bo_map_wc: %d (%s)\n", bo->gem_handle, bo->name);
784
785 memclear(mmap_arg);
786 mmap_arg.handle = bo->gem_handle;
787 mmap_arg.size = bo->size;
788 mmap_arg.flags = I915_MMAP_WC;
789 int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg);
790 if (ret != 0) {
791 ret = -errno;
792 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
793 __FILE__, __LINE__, bo->gem_handle, bo->name, strerror(errno));
794 return NULL;
795 }
796
797 map = (void *) (uintptr_t) mmap_arg.addr_ptr;
798 VG_DEFINED(map, bo->size);
799
800 if (p_atomic_cmpxchg(&bo->map_wc, NULL, map)) {
801 VG_NOACCESS(map, bo->size);
802 drm_munmap(map, bo->size);
803 }
804 }
805 assert(bo->map_wc);
806
807 DBG("brw_bo_map_wc: %d (%s) -> %p\n", bo->gem_handle, bo->name, bo->map_wc);
808 print_flags(flags);
809
810 if (!(flags & MAP_ASYNC)) {
811 bo_wait_with_stall_warning(brw, bo, "WC mapping");
812 }
813
814 return bo->map_wc;
815 }
816
817 /**
818 * Perform an uncached mapping via the GTT.
819 *
820 * Write access through the GTT is not quite fully coherent. On low power
821 * systems especially, like modern Atoms, we can observe reads from RAM before
822 * the write via GTT has landed. A write memory barrier that flushes the Write
823 * Combining Buffer (i.e. sfence/mfence) is not sufficient to order the later
824 * read after the write as the GTT write suffers a small delay through the GTT
825 * indirection. The kernel uses an uncached mmio read to ensure the GTT write
826 * is ordered with reads (either by the GPU, WB or WC) and unconditionally
827 * flushes prior to execbuf submission. However, if we are not informing the
828 * kernel about our GTT writes, it will not flush before earlier access, such
829 * as when using the cmdparser. Similarly, we need to be careful if we should
830 * ever issue a CPU read immediately following a GTT write.
831 *
832 * Telling the kernel about write access also has one more important
833 * side-effect. Upon receiving notification about the write, it cancels any
834 * scanout buffering for FBC/PSR and friends. Later FBC/PSR is then flushed by
835 * either SW_FINISH or DIRTYFB. The presumption is that we never write to the
836 * actual scanout via a mmaping, only to a backbuffer and so all the FBC/PSR
837 * tracking is handled on the buffer exchange instead.
838 */
839 static void *
840 brw_bo_map_gtt(struct brw_context *brw, struct brw_bo *bo, unsigned flags)
841 {
842 struct brw_bufmgr *bufmgr = bo->bufmgr;
843
844 /* Get a mapping of the buffer if we haven't before. */
845 if (bo->map_gtt == NULL) {
846 struct drm_i915_gem_mmap_gtt mmap_arg;
847 void *map;
848
849 DBG("bo_map_gtt: mmap %d (%s)\n", bo->gem_handle, bo->name);
850
851 memclear(mmap_arg);
852 mmap_arg.handle = bo->gem_handle;
853
854 /* Get the fake offset back... */
855 int ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg);
856 if (ret != 0) {
857 DBG("%s:%d: Error preparing buffer map %d (%s): %s .\n",
858 __FILE__, __LINE__, bo->gem_handle, bo->name, strerror(errno));
859 return NULL;
860 }
861
862 /* and mmap it. */
863 map = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE,
864 MAP_SHARED, bufmgr->fd, mmap_arg.offset);
865 if (map == MAP_FAILED) {
866 DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
867 __FILE__, __LINE__, bo->gem_handle, bo->name, strerror(errno));
868 return NULL;
869 }
870
871 /* We don't need to use VALGRIND_MALLOCLIKE_BLOCK because Valgrind will
872 * already intercept this mmap call. However, for consistency between
873 * all the mmap paths, we mark the pointer as defined now and mark it
874 * as inaccessible afterwards.
875 */
876 VG_DEFINED(map, bo->size);
877
878 if (p_atomic_cmpxchg(&bo->map_gtt, NULL, map)) {
879 VG_NOACCESS(map, bo->size);
880 drm_munmap(map, bo->size);
881 }
882 }
883 assert(bo->map_gtt);
884
885 DBG("bo_map_gtt: %d (%s) -> %p, ", bo->gem_handle, bo->name, bo->map_gtt);
886 print_flags(flags);
887
888 if (!(flags & MAP_ASYNC)) {
889 bo_wait_with_stall_warning(brw, bo, "GTT mapping");
890 }
891
892 return bo->map_gtt;
893 }
894
895 static bool
896 can_map_cpu(struct brw_bo *bo, unsigned flags)
897 {
898 if (bo->cache_coherent)
899 return true;
900
901 /* Even if the buffer itself is not cache-coherent (such as a scanout), on
902 * an LLC platform reads always are coherent (as they are performed via the
903 * central system agent). It is just the writes that we need to take special
904 * care to ensure that land in main memory and not stick in the CPU cache.
905 */
906 if (!(flags & MAP_WRITE) && bo->bufmgr->has_llc)
907 return true;
908
909 /* If PERSISTENT or COHERENT are set, the mmapping needs to remain valid
910 * across batch flushes where the kernel will change cache domains of the
911 * bo, invalidating continued access to the CPU mmap on non-LLC device.
912 *
913 * Similarly, ASYNC typically means that the buffer will be accessed via
914 * both the CPU and the GPU simultaneously. Batches may be executed that
915 * use the BO even while it is mapped. While OpenGL technically disallows
916 * most drawing while non-persistent mappings are active, we may still use
917 * the GPU for blits or other operations, causing batches to happen at
918 * inconvenient times.
919 */
920 if (flags & (MAP_PERSISTENT | MAP_COHERENT | MAP_ASYNC))
921 return false;
922
923 return !(flags & MAP_WRITE);
924 }
925
926 void *
927 brw_bo_map(struct brw_context *brw, struct brw_bo *bo, unsigned flags)
928 {
929 if (bo->tiling_mode != I915_TILING_NONE && !(flags & MAP_RAW))
930 return brw_bo_map_gtt(brw, bo, flags);
931
932 void *map;
933
934 if (can_map_cpu(bo, flags))
935 map = brw_bo_map_cpu(brw, bo, flags);
936 else
937 map = brw_bo_map_wc(brw, bo, flags);
938
939 /* Allow the attempt to fail by falling back to the GTT where necessary.
940 *
941 * Not every buffer can be mmaped directly using the CPU (or WC), for
942 * example buffers that wrap stolen memory or are imported from other
943 * devices. For those, we have little choice but to use a GTT mmapping.
944 * However, if we use a slow GTT mmapping for reads where we expected fast
945 * access, that order of magnitude difference in throughput will be clearly
946 * expressed by angry users.
947 *
948 * We skip MAP_RAW because we want to avoid map_gtt's fence detiling.
949 */
950 if (!map && !(flags & MAP_RAW)) {
951 if (brw) {
952 perf_debug("Fallback GTT mapping for %s with access flags %x\n",
953 bo->name, flags);
954 }
955 map = brw_bo_map_gtt(brw, bo, flags);
956 }
957
958 return map;
959 }
960
961 int
962 brw_bo_subdata(struct brw_bo *bo, uint64_t offset,
963 uint64_t size, const void *data)
964 {
965 struct brw_bufmgr *bufmgr = bo->bufmgr;
966 struct drm_i915_gem_pwrite pwrite;
967 int ret;
968
969 memclear(pwrite);
970 pwrite.handle = bo->gem_handle;
971 pwrite.offset = offset;
972 pwrite.size = size;
973 pwrite.data_ptr = (uint64_t) (uintptr_t) data;
974 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite);
975 if (ret != 0) {
976 ret = -errno;
977 DBG("%s:%d: Error writing data to buffer %d: "
978 "(%"PRIu64" %"PRIu64") %s .\n",
979 __FILE__, __LINE__, bo->gem_handle, offset, size, strerror(errno));
980 }
981
982 return ret;
983 }
984
985 /** Waits for all GPU rendering with the object to have completed. */
986 void
987 brw_bo_wait_rendering(struct brw_bo *bo)
988 {
989 /* We require a kernel recent enough for WAIT_IOCTL support.
990 * See intel_init_bufmgr()
991 */
992 brw_bo_wait(bo, -1);
993 }
994
995 /**
996 * Waits on a BO for the given amount of time.
997 *
998 * @bo: buffer object to wait for
999 * @timeout_ns: amount of time to wait in nanoseconds.
1000 * If value is less than 0, an infinite wait will occur.
1001 *
1002 * Returns 0 if the wait was successful ie. the last batch referencing the
1003 * object has completed within the allotted time. Otherwise some negative return
1004 * value describes the error. Of particular interest is -ETIME when the wait has
1005 * failed to yield the desired result.
1006 *
1007 * Similar to brw_bo_wait_rendering except a timeout parameter allows
1008 * the operation to give up after a certain amount of time. Another subtle
1009 * difference is the internal locking semantics are different (this variant does
1010 * not hold the lock for the duration of the wait). This makes the wait subject
1011 * to a larger userspace race window.
1012 *
1013 * The implementation shall wait until the object is no longer actively
1014 * referenced within a batch buffer at the time of the call. The wait will
1015 * not guarantee that the buffer is re-issued via another thread, or an flinked
1016 * handle. Userspace must make sure this race does not occur if such precision
1017 * is important.
1018 *
1019 * Note that some kernels have broken the inifite wait for negative values
1020 * promise, upgrade to latest stable kernels if this is the case.
1021 */
1022 int
1023 brw_bo_wait(struct brw_bo *bo, int64_t timeout_ns)
1024 {
1025 struct brw_bufmgr *bufmgr = bo->bufmgr;
1026 struct drm_i915_gem_wait wait;
1027 int ret;
1028
1029 /* If we know it's idle, don't bother with the kernel round trip */
1030 if (bo->idle && !bo->external)
1031 return 0;
1032
1033 memclear(wait);
1034 wait.bo_handle = bo->gem_handle;
1035 wait.timeout_ns = timeout_ns;
1036 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_WAIT, &wait);
1037 if (ret == -1)
1038 return -errno;
1039
1040 bo->idle = true;
1041
1042 return ret;
1043 }
1044
1045 void
1046 brw_bufmgr_destroy(struct brw_bufmgr *bufmgr)
1047 {
1048 mtx_destroy(&bufmgr->lock);
1049
1050 /* Free any cached buffer objects we were going to reuse */
1051 for (int i = 0; i < bufmgr->num_buckets; i++) {
1052 struct bo_cache_bucket *bucket = &bufmgr->cache_bucket[i];
1053
1054 list_for_each_entry_safe(struct brw_bo, bo, &bucket->head, head) {
1055 list_del(&bo->head);
1056
1057 bo_free(bo);
1058 }
1059 }
1060
1061 _mesa_hash_table_destroy(bufmgr->name_table, NULL);
1062 _mesa_hash_table_destroy(bufmgr->handle_table, NULL);
1063
1064 free(bufmgr);
1065 }
1066
1067 static int
1068 bo_set_tiling_internal(struct brw_bo *bo, uint32_t tiling_mode,
1069 uint32_t stride)
1070 {
1071 struct brw_bufmgr *bufmgr = bo->bufmgr;
1072 struct drm_i915_gem_set_tiling set_tiling;
1073 int ret;
1074
1075 if (bo->global_name == 0 &&
1076 tiling_mode == bo->tiling_mode && stride == bo->stride)
1077 return 0;
1078
1079 memset(&set_tiling, 0, sizeof(set_tiling));
1080 do {
1081 /* set_tiling is slightly broken and overwrites the
1082 * input on the error path, so we have to open code
1083 * rmIoctl.
1084 */
1085 set_tiling.handle = bo->gem_handle;
1086 set_tiling.tiling_mode = tiling_mode;
1087 set_tiling.stride = stride;
1088
1089 ret = ioctl(bufmgr->fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
1090 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
1091 if (ret == -1)
1092 return -errno;
1093
1094 bo->tiling_mode = set_tiling.tiling_mode;
1095 bo->swizzle_mode = set_tiling.swizzle_mode;
1096 bo->stride = set_tiling.stride;
1097 return 0;
1098 }
1099
1100 int
1101 brw_bo_get_tiling(struct brw_bo *bo, uint32_t *tiling_mode,
1102 uint32_t *swizzle_mode)
1103 {
1104 *tiling_mode = bo->tiling_mode;
1105 *swizzle_mode = bo->swizzle_mode;
1106 return 0;
1107 }
1108
1109 struct brw_bo *
1110 brw_bo_gem_create_from_prime(struct brw_bufmgr *bufmgr, int prime_fd)
1111 {
1112 int ret;
1113 uint32_t handle;
1114 struct brw_bo *bo;
1115 struct drm_i915_gem_get_tiling get_tiling;
1116
1117 mtx_lock(&bufmgr->lock);
1118 ret = drmPrimeFDToHandle(bufmgr->fd, prime_fd, &handle);
1119 if (ret) {
1120 DBG("create_from_prime: failed to obtain handle from fd: %s\n",
1121 strerror(errno));
1122 mtx_unlock(&bufmgr->lock);
1123 return NULL;
1124 }
1125
1126 /*
1127 * See if the kernel has already returned this buffer to us. Just as
1128 * for named buffers, we must not create two bo's pointing at the same
1129 * kernel object
1130 */
1131 bo = hash_find_bo(bufmgr->handle_table, handle);
1132 if (bo) {
1133 brw_bo_reference(bo);
1134 goto out;
1135 }
1136
1137 bo = calloc(1, sizeof(*bo));
1138 if (!bo)
1139 goto out;
1140
1141 p_atomic_set(&bo->refcount, 1);
1142
1143 /* Determine size of bo. The fd-to-handle ioctl really should
1144 * return the size, but it doesn't. If we have kernel 3.12 or
1145 * later, we can lseek on the prime fd to get the size. Older
1146 * kernels will just fail, in which case we fall back to the
1147 * provided (estimated or guess size). */
1148 ret = lseek(prime_fd, 0, SEEK_END);
1149 if (ret != -1)
1150 bo->size = ret;
1151
1152 bo->bufmgr = bufmgr;
1153
1154 bo->gem_handle = handle;
1155 _mesa_hash_table_insert(bufmgr->handle_table, &bo->gem_handle, bo);
1156
1157 bo->name = "prime";
1158 bo->reusable = false;
1159 bo->external = true;
1160
1161 memclear(get_tiling);
1162 get_tiling.handle = bo->gem_handle;
1163 if (drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_GET_TILING, &get_tiling))
1164 goto err;
1165
1166 bo->tiling_mode = get_tiling.tiling_mode;
1167 bo->swizzle_mode = get_tiling.swizzle_mode;
1168 /* XXX stride is unknown */
1169
1170 out:
1171 mtx_unlock(&bufmgr->lock);
1172 return bo;
1173
1174 err:
1175 bo_free(bo);
1176 mtx_unlock(&bufmgr->lock);
1177 return NULL;
1178 }
1179
1180 int
1181 brw_bo_gem_export_to_prime(struct brw_bo *bo, int *prime_fd)
1182 {
1183 struct brw_bufmgr *bufmgr = bo->bufmgr;
1184
1185 if (!bo->external) {
1186 mtx_lock(&bufmgr->lock);
1187 if (!bo->external) {
1188 _mesa_hash_table_insert(bufmgr->handle_table, &bo->gem_handle, bo);
1189 bo->external = true;
1190 }
1191 mtx_unlock(&bufmgr->lock);
1192 }
1193
1194 if (drmPrimeHandleToFD(bufmgr->fd, bo->gem_handle,
1195 DRM_CLOEXEC, prime_fd) != 0)
1196 return -errno;
1197
1198 bo->reusable = false;
1199
1200 return 0;
1201 }
1202
1203 int
1204 brw_bo_flink(struct brw_bo *bo, uint32_t *name)
1205 {
1206 struct brw_bufmgr *bufmgr = bo->bufmgr;
1207
1208 if (!bo->global_name) {
1209 struct drm_gem_flink flink;
1210
1211 memclear(flink);
1212 flink.handle = bo->gem_handle;
1213 if (drmIoctl(bufmgr->fd, DRM_IOCTL_GEM_FLINK, &flink))
1214 return -errno;
1215
1216 mtx_lock(&bufmgr->lock);
1217 if (!bo->external) {
1218 _mesa_hash_table_insert(bufmgr->handle_table, &bo->gem_handle, bo);
1219 bo->external = true;
1220 }
1221 if (!bo->global_name) {
1222 bo->global_name = flink.name;
1223 _mesa_hash_table_insert(bufmgr->name_table, &bo->global_name, bo);
1224 }
1225 mtx_unlock(&bufmgr->lock);
1226
1227 bo->reusable = false;
1228 }
1229
1230 *name = bo->global_name;
1231 return 0;
1232 }
1233
1234 /**
1235 * Enables unlimited caching of buffer objects for reuse.
1236 *
1237 * This is potentially very memory expensive, as the cache at each bucket
1238 * size is only bounded by how many buffers of that size we've managed to have
1239 * in flight at once.
1240 */
1241 void
1242 brw_bufmgr_enable_reuse(struct brw_bufmgr *bufmgr)
1243 {
1244 bufmgr->bo_reuse = true;
1245 }
1246
1247 static void
1248 add_bucket(struct brw_bufmgr *bufmgr, int size)
1249 {
1250 unsigned int i = bufmgr->num_buckets;
1251
1252 assert(i < ARRAY_SIZE(bufmgr->cache_bucket));
1253
1254 list_inithead(&bufmgr->cache_bucket[i].head);
1255 bufmgr->cache_bucket[i].size = size;
1256 bufmgr->num_buckets++;
1257 }
1258
1259 static void
1260 init_cache_buckets(struct brw_bufmgr *bufmgr)
1261 {
1262 uint64_t size, cache_max_size = 64 * 1024 * 1024;
1263
1264 /* OK, so power of two buckets was too wasteful of memory.
1265 * Give 3 other sizes between each power of two, to hopefully
1266 * cover things accurately enough. (The alternative is
1267 * probably to just go for exact matching of sizes, and assume
1268 * that for things like composited window resize the tiled
1269 * width/height alignment and rounding of sizes to pages will
1270 * get us useful cache hit rates anyway)
1271 */
1272 add_bucket(bufmgr, 4096);
1273 add_bucket(bufmgr, 4096 * 2);
1274 add_bucket(bufmgr, 4096 * 3);
1275
1276 /* Initialize the linked lists for BO reuse cache. */
1277 for (size = 4 * 4096; size <= cache_max_size; size *= 2) {
1278 add_bucket(bufmgr, size);
1279
1280 add_bucket(bufmgr, size + size * 1 / 4);
1281 add_bucket(bufmgr, size + size * 2 / 4);
1282 add_bucket(bufmgr, size + size * 3 / 4);
1283 }
1284 }
1285
1286 uint32_t
1287 brw_create_hw_context(struct brw_bufmgr *bufmgr)
1288 {
1289 struct drm_i915_gem_context_create create;
1290 int ret;
1291
1292 memclear(create);
1293 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create);
1294 if (ret != 0) {
1295 DBG("DRM_IOCTL_I915_GEM_CONTEXT_CREATE failed: %s\n", strerror(errno));
1296 return 0;
1297 }
1298
1299 return create.ctx_id;
1300 }
1301
1302 int
1303 brw_hw_context_set_priority(struct brw_bufmgr *bufmgr,
1304 uint32_t ctx_id,
1305 int priority)
1306 {
1307 struct drm_i915_gem_context_param p = {
1308 .ctx_id = ctx_id,
1309 .param = I915_CONTEXT_PARAM_PRIORITY,
1310 .value = priority,
1311 };
1312 int err;
1313
1314 err = 0;
1315 if (drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_CONTEXT_SETPARAM, &p))
1316 err = -errno;
1317
1318 return err;
1319 }
1320
1321 void
1322 brw_destroy_hw_context(struct brw_bufmgr *bufmgr, uint32_t ctx_id)
1323 {
1324 struct drm_i915_gem_context_destroy d = {.ctx_id = ctx_id };
1325
1326 if (ctx_id != 0 &&
1327 drmIoctl(bufmgr->fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY, &d) != 0) {
1328 fprintf(stderr, "DRM_IOCTL_I915_GEM_CONTEXT_DESTROY failed: %s\n",
1329 strerror(errno));
1330 }
1331 }
1332
1333 int
1334 brw_reg_read(struct brw_bufmgr *bufmgr, uint32_t offset, uint64_t *result)
1335 {
1336 struct drm_i915_reg_read reg_read;
1337 int ret;
1338
1339 memclear(reg_read);
1340 reg_read.offset = offset;
1341
1342 ret = drmIoctl(bufmgr->fd, DRM_IOCTL_I915_REG_READ, &reg_read);
1343
1344 *result = reg_read.val;
1345 return ret;
1346 }
1347
1348 static int
1349 gem_param(int fd, int name)
1350 {
1351 drm_i915_getparam_t gp;
1352 int v = -1; /* No param uses (yet) the sign bit, reserve it for errors */
1353
1354 memset(&gp, 0, sizeof(gp));
1355 gp.param = name;
1356 gp.value = &v;
1357 if (drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp))
1358 return -1;
1359
1360 return v;
1361 }
1362
1363 /**
1364 * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
1365 * and manage map buffer objections.
1366 *
1367 * \param fd File descriptor of the opened DRM device.
1368 */
1369 struct brw_bufmgr *
1370 brw_bufmgr_init(struct gen_device_info *devinfo, int fd)
1371 {
1372 struct brw_bufmgr *bufmgr;
1373
1374 bufmgr = calloc(1, sizeof(*bufmgr));
1375 if (bufmgr == NULL)
1376 return NULL;
1377
1378 /* Handles to buffer objects belong to the device fd and are not
1379 * reference counted by the kernel. If the same fd is used by
1380 * multiple parties (threads sharing the same screen bufmgr, or
1381 * even worse the same device fd passed to multiple libraries)
1382 * ownership of those handles is shared by those independent parties.
1383 *
1384 * Don't do this! Ensure that each library/bufmgr has its own device
1385 * fd so that its namespace does not clash with another.
1386 */
1387 bufmgr->fd = fd;
1388
1389 if (mtx_init(&bufmgr->lock, mtx_plain) != 0) {
1390 free(bufmgr);
1391 return NULL;
1392 }
1393
1394 bufmgr->has_llc = devinfo->has_llc;
1395 bufmgr->has_mmap_wc = gem_param(fd, I915_PARAM_MMAP_VERSION) > 0;
1396
1397 init_cache_buckets(bufmgr);
1398
1399 bufmgr->name_table =
1400 _mesa_hash_table_create(NULL, key_hash_uint, key_uint_equal);
1401 bufmgr->handle_table =
1402 _mesa_hash_table_create(NULL, key_hash_uint, key_uint_equal);
1403
1404 return bufmgr;
1405 }