[intel-gem] Use I915_GEM_DOMAIN_GTT in dri_gem_bo_wait_rendering.
[profile/ivi/libdrm.git] / libdrm / intel / intel_bufmgr_gem.c
1 /**************************************************************************
2  *
3  * Copyright © 2007 Red Hat Inc.
4  * Copyright © 2007 Intel Corporation
5  * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
6  * All Rights Reserved.
7  *
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:
15  *
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.
23  *
24  * The above copyright notice and this permission notice (including the
25  * next paragraph) shall be included in all copies or substantial portions
26  * of the Software.
27  *
28  *
29  **************************************************************************/
30 /*
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>
35  */
36
37 #include <xf86drm.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <assert.h>
43 #include <sys/ioctl.h>
44 #include <sys/mman.h>
45
46 #include "errno.h"
47 #include "dri_bufmgr.h"
48 #include "intel_bufmgr.h"
49 #include "string.h"
50
51 #include "i915_drm.h"
52
53 #define DBG(...) do {                                   \
54    if (bufmgr_gem->bufmgr.debug)                        \
55       fprintf(stderr, __VA_ARGS__);                     \
56 } while (0)
57
58 typedef struct _dri_bo_gem dri_bo_gem;
59
60 struct dri_gem_bo_bucket {
61    dri_bo_gem *head, **tail;
62    /**
63     * Limit on the number of entries in this bucket.
64     *
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.
67     */
68    int max_entries;
69    int num_entries;
70 };
71
72 /* Arbitrarily chosen, 16 means that the maximum size we'll cache for reuse
73  * is 1 << 16 pages, or 256MB.
74  */
75 #define INTEL_GEM_BO_BUCKETS    16
76 typedef struct _dri_bufmgr_gem {
77     dri_bufmgr bufmgr;
78
79     struct intel_bufmgr intel_bufmgr;
80
81     int fd;
82
83     int max_relocs;
84
85     struct drm_i915_gem_exec_object *exec_objects;
86     dri_bo **exec_bos;
87     int exec_size;
88     int exec_count;
89
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];
92
93     struct drm_i915_gem_execbuffer exec_arg;
94 } dri_bufmgr_gem;
95
96 struct _dri_bo_gem {
97     dri_bo bo;
98
99     int refcount;
100     /** Boolean whether the mmap ioctl has been called for this buffer yet. */
101     int mapped;
102     uint32_t gem_handle;
103     const char *name;
104
105     /**
106      * Index of the buffer within the validation list while preparing a
107      * batchbuffer execution.
108      */
109     int validate_index;
110
111     /**
112      * Boolean whether we've started swrast
113      * Set when the buffer has been mapped
114      * Cleared when the buffer is unmapped
115      */
116     int swrast;
117
118     /** Array passed to the DRM containing relocation information. */
119     struct drm_i915_gem_relocation_entry *relocs;
120     /** Array of bos corresponding to relocs[i].target_handle */
121     dri_bo **reloc_target_bo;
122     /** Number of entries in relocs */
123     int reloc_count;
124     /** Mapped address for the buffer */
125     void *virtual;
126
127     /** free list */
128     dri_bo_gem *next;
129 };
130
131 static int
132 logbase2(int n)
133 {
134    int i = 1;
135    int log2 = 0;
136
137    while (n > i) {
138       i *= 2;
139       log2++;
140    }
141
142    return log2;
143 }
144
145 static struct dri_gem_bo_bucket *
146 dri_gem_bo_bucket_for_size(dri_bufmgr_gem *bufmgr_gem, unsigned long size)
147 {
148     int i;
149
150     /* We only do buckets in power of two increments */
151     if ((size & (size - 1)) != 0)
152         return NULL;
153
154     /* We should only see sizes rounded to pages. */
155     assert((size % 4096) == 0);
156
157     /* We always allocate in units of pages */
158     i = ffs(size / 4096) - 1;
159     if (i >= INTEL_GEM_BO_BUCKETS)
160         return NULL;
161
162     return &bufmgr_gem->cache_bucket[i];
163 }
164
165
166 static void dri_gem_dump_validation_list(dri_bufmgr_gem *bufmgr_gem)
167 {
168     int i, j;
169
170     for (i = 0; i < bufmgr_gem->exec_count; i++) {
171         dri_bo *bo = bufmgr_gem->exec_bos[i];
172         dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
173
174         if (bo_gem->relocs == NULL) {
175             DBG("%2d: %d (%s)\n", i, bo_gem->gem_handle, bo_gem->name);
176             continue;
177         }
178
179         for (j = 0; j < bo_gem->reloc_count; j++) {
180             dri_bo *target_bo = bo_gem->reloc_target_bo[j];
181             dri_bo_gem *target_gem = (dri_bo_gem *)target_bo;
182
183             DBG("%2d: %d (%s)@0x%08llx -> %d (%s)@0x%08lx + 0x%08x\n",
184                 i,
185                 bo_gem->gem_handle, bo_gem->name, bo_gem->relocs[j].offset,
186                 target_gem->gem_handle, target_gem->name, target_bo->offset,
187                 bo_gem->relocs[j].delta);
188         }
189     }
190 }
191
192 /**
193  * Adds the given buffer to the list of buffers to be validated (moved into the
194  * appropriate memory type) with the next batch submission.
195  *
196  * If a buffer is validated multiple times in a batch submission, it ends up
197  * with the intersection of the memory type flags and the union of the
198  * access flags.
199  */
200 static void
201 intel_add_validate_buffer(dri_bo *bo)
202 {
203     dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
204     dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
205     int index;
206
207     if (bo_gem->validate_index != -1)
208         return;
209
210     /* Extend the array of validation entries as necessary. */
211     if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
212         int new_size = bufmgr_gem->exec_size * 2;
213
214         if (new_size == 0)
215             new_size = 5;
216
217         bufmgr_gem->exec_objects =
218             realloc(bufmgr_gem->exec_objects,
219                     sizeof(*bufmgr_gem->exec_objects) * new_size);
220         bufmgr_gem->exec_bos =
221             realloc(bufmgr_gem->exec_bos,
222                     sizeof(*bufmgr_gem->exec_bos) * new_size);
223         bufmgr_gem->exec_size = new_size;
224     }
225
226     index = bufmgr_gem->exec_count;
227     bo_gem->validate_index = index;
228     /* Fill in array entry */
229     bufmgr_gem->exec_objects[index].handle = bo_gem->gem_handle;
230     bufmgr_gem->exec_objects[index].relocation_count = bo_gem->reloc_count;
231     bufmgr_gem->exec_objects[index].relocs_ptr = (uintptr_t)bo_gem->relocs;
232     bufmgr_gem->exec_objects[index].alignment = 0;
233     bufmgr_gem->exec_objects[index].offset = 0;
234     bufmgr_gem->exec_bos[index] = bo;
235     dri_bo_reference(bo);
236     bufmgr_gem->exec_count++;
237 }
238
239
240 #define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \
241         sizeof(uint32_t))
242
243 static int
244 intel_setup_reloc_list(dri_bo *bo)
245 {
246     dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
247     dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
248
249     bo_gem->relocs = malloc(bufmgr_gem->max_relocs *
250                             sizeof(struct drm_i915_gem_relocation_entry));
251     bo_gem->reloc_target_bo = malloc(bufmgr_gem->max_relocs * sizeof(dri_bo *));
252
253     return 0;
254 }
255
256 static dri_bo *
257 dri_gem_bo_alloc(dri_bufmgr *bufmgr, const char *name,
258                  unsigned long size, unsigned int alignment)
259 {
260     dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr;
261     dri_bo_gem *bo_gem;
262     unsigned int page_size = getpagesize();
263     int ret;
264     struct dri_gem_bo_bucket *bucket;
265     int alloc_from_cache = 0;
266     unsigned long bo_size;
267
268     /* Round the allocated size up to a power of two number of pages. */
269     bo_size = 1 << logbase2(size);
270     if (bo_size < page_size)
271         bo_size = page_size;
272     bucket = dri_gem_bo_bucket_for_size(bufmgr_gem, bo_size);
273
274     /* If we don't have caching at this size, don't actually round the
275      * allocation up.
276      */
277     if (bucket == NULL || bucket->max_entries == 0) {
278         bo_size = size;
279         if (bo_size < page_size)
280             bo_size = page_size;
281     }
282
283     /* Get a buffer out of the cache if available */
284     if (bucket != NULL && bucket->num_entries > 0) {
285         struct drm_i915_gem_busy busy;
286         
287         bo_gem = bucket->head;
288         busy.handle = bo_gem->gem_handle;
289
290         ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
291         alloc_from_cache = (ret == 0 && busy.busy == 0);
292
293         if (alloc_from_cache) {
294             bucket->head = bo_gem->next;
295             if (bo_gem->next == NULL)
296                 bucket->tail = &bucket->head;
297             bucket->num_entries--;
298         }
299     }
300
301     if (!alloc_from_cache) {
302         struct drm_i915_gem_create create;
303
304         bo_gem = calloc(1, sizeof(*bo_gem));
305         if (!bo_gem)
306             return NULL;
307
308         bo_gem->bo.size = bo_size;
309         memset(&create, 0, sizeof(create));
310         create.size = bo_size;
311
312         ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_CREATE, &create);
313         bo_gem->gem_handle = create.handle;
314         if (ret != 0) {
315             free(bo_gem);
316             return NULL;
317         }
318         bo_gem->bo.bufmgr = bufmgr;
319     }
320
321     bo_gem->name = name;
322     bo_gem->refcount = 1;
323     bo_gem->validate_index = -1;
324
325     DBG("bo_create: buf %d (%s) %ldb\n",
326         bo_gem->gem_handle, bo_gem->name, size);
327
328     return &bo_gem->bo;
329 }
330
331 /**
332  * Returns a dri_bo wrapping the given buffer object handle.
333  *
334  * This can be used when one application needs to pass a buffer object
335  * to another.
336  */
337 dri_bo *
338 intel_bo_gem_create_from_name(dri_bufmgr *bufmgr, const char *name,
339                               unsigned int handle)
340 {
341     dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr;
342     dri_bo_gem *bo_gem;
343     int ret;
344     struct drm_gem_open open_arg;
345
346     bo_gem = calloc(1, sizeof(*bo_gem));
347     if (!bo_gem)
348         return NULL;
349
350     memset(&open_arg, 0, sizeof(open_arg));
351     open_arg.name = handle;
352     ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_OPEN, &open_arg);
353     if (ret != 0) {
354         fprintf(stderr, "Couldn't reference %s handle 0x%08x: %s\n",
355                name, handle, strerror(-ret));
356         free(bo_gem);
357         return NULL;
358     }
359     bo_gem->bo.size = open_arg.size;
360     bo_gem->bo.offset = 0;
361     bo_gem->bo.virtual = NULL;
362     bo_gem->bo.bufmgr = bufmgr;
363     bo_gem->name = name;
364     bo_gem->refcount = 1;
365     bo_gem->validate_index = -1;
366     bo_gem->gem_handle = open_arg.handle;
367
368     DBG("bo_create_from_handle: %d (%s)\n", handle, bo_gem->name);
369
370     return &bo_gem->bo;
371 }
372
373 static void
374 dri_gem_bo_reference(dri_bo *bo)
375 {
376     dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
377
378     bo_gem->refcount++;
379 }
380
381 static void
382 dri_gem_bo_free(dri_bo *bo)
383 {
384     dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
385     dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
386     struct drm_gem_close close;
387     int ret;
388
389     if (bo_gem->mapped)
390         munmap (bo_gem->virtual, bo_gem->bo.size);
391
392     /* Close this object */
393     close.handle = bo_gem->gem_handle;
394     ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close);
395     if (ret != 0) {
396         fprintf(stderr,
397                 "DRM_IOCTL_GEM_CLOSE %d failed (%s): %s\n",
398                 bo_gem->gem_handle, bo_gem->name, strerror(-ret));
399     }
400     free(bo);
401 }
402
403 static void
404 dri_gem_bo_unreference(dri_bo *bo)
405 {
406     dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
407     dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
408
409     if (!bo)
410         return;
411
412     if (--bo_gem->refcount == 0) {
413         struct dri_gem_bo_bucket *bucket;
414
415         if (bo_gem->relocs != NULL) {
416             int i;
417
418             /* Unreference all the target buffers */
419             for (i = 0; i < bo_gem->reloc_count; i++)
420                  dri_bo_unreference(bo_gem->reloc_target_bo[i]);
421             free(bo_gem->reloc_target_bo);
422             free(bo_gem->relocs);
423         }
424
425         DBG("bo_unreference final: %d (%s)\n",
426             bo_gem->gem_handle, bo_gem->name);
427
428         bucket = dri_gem_bo_bucket_for_size(bufmgr_gem, bo->size);
429         /* Put the buffer into our internal cache for reuse if we can. */
430         if (bucket != NULL &&
431             (bucket->max_entries == -1 ||
432              (bucket->max_entries > 0 &&
433               bucket->num_entries < bucket->max_entries)))
434         {
435             bo_gem->name = 0;
436             bo_gem->validate_index = -1;
437             bo_gem->relocs = NULL;
438             bo_gem->reloc_target_bo = NULL;
439             bo_gem->reloc_count = 0;
440
441             bo_gem->next = NULL;
442             *bucket->tail = bo_gem;
443             bucket->tail = &bo_gem->next;
444             bucket->num_entries++;
445         } else {
446             dri_gem_bo_free(bo);
447         }
448
449         return;
450     }
451 }
452
453 static int
454 dri_gem_bo_map(dri_bo *bo, int write_enable)
455 {
456     dri_bufmgr_gem *bufmgr_gem;
457     dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
458     struct drm_i915_gem_set_domain set_domain;
459     int ret;
460
461     bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
462
463     /* Allow recursive mapping. Mesa may recursively map buffers with
464      * nested display loops.
465      */
466     if (!bo_gem->mapped) {
467     
468         assert(bo->virtual == NULL);
469     
470         DBG("bo_map: %d (%s)\n", bo_gem->gem_handle, bo_gem->name);
471     
472         if (bo_gem->virtual == NULL) {
473             struct drm_i915_gem_mmap mmap_arg;
474     
475             memset(&mmap_arg, 0, sizeof(mmap_arg));
476             mmap_arg.handle = bo_gem->gem_handle;
477             mmap_arg.offset = 0;
478             mmap_arg.size = bo->size;
479             ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_MMAP, &mmap_arg);
480             if (ret != 0) {
481                 fprintf(stderr, "%s:%d: Error mapping buffer %d (%s): %s .\n",
482                         __FILE__, __LINE__,
483                         bo_gem->gem_handle, bo_gem->name, strerror(errno));
484             }
485             bo_gem->virtual = (void *)(uintptr_t)mmap_arg.addr_ptr;
486         }
487         bo->virtual = bo_gem->virtual;
488         bo_gem->swrast = 0;
489         bo_gem->mapped = 1;
490         DBG("bo_map: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name, bo_gem->virtual);
491     }
492
493     if (!bo_gem->swrast) {
494         set_domain.handle = bo_gem->gem_handle;
495         set_domain.read_domains = I915_GEM_DOMAIN_CPU;
496         if (write_enable)
497             set_domain.write_domain = I915_GEM_DOMAIN_CPU;
498         else
499             set_domain.write_domain = 0;
500         do {
501             ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN,
502                         &set_domain);
503         } while (ret == -1 && errno == EINTR);
504         if (ret != 0) {
505             fprintf (stderr, "%s:%d: Error setting swrast %d: %s\n",
506                      __FILE__, __LINE__, bo_gem->gem_handle, strerror (errno));
507         }
508         bo_gem->swrast = 1;
509     }
510
511     return 0;
512 }
513
514 static int
515 dri_gem_bo_unmap(dri_bo *bo)
516 {
517     dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
518     dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
519     struct drm_i915_gem_sw_finish sw_finish;
520     int ret;
521
522     if (bo == NULL)
523         return 0;
524
525     assert(bo_gem->mapped);
526
527     if (bo_gem->swrast) {
528         sw_finish.handle = bo_gem->gem_handle;
529         do {
530             ret = ioctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_SW_FINISH,
531                         &sw_finish);
532         } while (ret == -1 && errno == EINTR);
533         bo_gem->swrast = 0;
534     }
535     return 0;
536 }
537
538 static int
539 dri_gem_bo_subdata (dri_bo *bo, unsigned long offset,
540                     unsigned long size, const void *data)
541 {
542     dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
543     dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
544     struct drm_i915_gem_pwrite pwrite;
545     int ret;
546
547     memset (&pwrite, 0, sizeof (pwrite));
548     pwrite.handle = bo_gem->gem_handle;
549     pwrite.offset = offset;
550     pwrite.size = size;
551     pwrite.data_ptr = (uint64_t) (uintptr_t) data;
552     do {
553         ret = ioctl (bufmgr_gem->fd, DRM_IOCTL_I915_GEM_PWRITE, &pwrite);
554     } while (ret == -1 && errno == EINTR);
555     if (ret != 0) {
556         fprintf (stderr, "%s:%d: Error writing data to buffer %d: (%d %d) %s .\n",
557                  __FILE__, __LINE__,
558                  bo_gem->gem_handle, (int) offset, (int) size,
559                  strerror (errno));
560     }
561     return 0;
562 }
563
564 static int
565 dri_gem_bo_get_subdata (dri_bo *bo, unsigned long offset,
566                         unsigned long size, void *data)
567 {
568     dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
569     dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
570     struct drm_i915_gem_pread pread;
571     int ret;
572
573     memset (&pread, 0, sizeof (pread));
574     pread.handle = bo_gem->gem_handle;
575     pread.offset = offset;
576     pread.size = size;
577     pread.data_ptr = (uint64_t) (uintptr_t) data;
578     do {
579         ret = ioctl (bufmgr_gem->fd, DRM_IOCTL_I915_GEM_PREAD, &pread);
580     } while (ret == -1 && errno == EINTR);
581     if (ret != 0) {
582         fprintf (stderr, "%s:%d: Error reading data from buffer %d: (%d %d) %s .\n",
583                  __FILE__, __LINE__,
584                  bo_gem->gem_handle, (int) offset, (int) size,
585                  strerror (errno));
586     }
587     return 0;
588 }
589
590 static void
591 dri_gem_bo_wait_rendering(dri_bo *bo)
592 {
593     dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
594     dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
595     struct drm_i915_gem_set_domain set_domain;
596     int ret;
597
598     set_domain.handle = bo_gem->gem_handle;
599     set_domain.read_domains = I915_GEM_DOMAIN_GTT;
600     set_domain.write_domain = 0;
601     ret = ioctl (bufmgr_gem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &set_domain);
602     if (ret != 0) {
603         fprintf (stderr, "%s:%d: Error setting memory domains %d (%08x %08x): %s .\n",
604                  __FILE__, __LINE__,
605                  bo_gem->gem_handle, set_domain.read_domains, set_domain.write_domain,
606                  strerror (errno));
607     }
608 }
609
610 static void
611 dri_bufmgr_gem_destroy(dri_bufmgr *bufmgr)
612 {
613     dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr;
614     int i;
615
616     free(bufmgr_gem->exec_objects);
617     free(bufmgr_gem->exec_bos);
618
619     /* Free any cached buffer objects we were going to reuse */
620     for (i = 0; i < INTEL_GEM_BO_BUCKETS; i++) {
621         struct dri_gem_bo_bucket *bucket = &bufmgr_gem->cache_bucket[i];
622         dri_bo_gem *bo_gem;
623
624         while ((bo_gem = bucket->head) != NULL) {
625             bucket->head = bo_gem->next;
626             if (bo_gem->next == NULL)
627                 bucket->tail = &bucket->head;
628             bucket->num_entries--;
629
630             dri_gem_bo_free(&bo_gem->bo);
631         }
632     }
633
634     free(bufmgr);
635 }
636
637 /**
638  * Adds the target buffer to the validation list and adds the relocation
639  * to the reloc_buffer's relocation list.
640  *
641  * The relocation entry at the given offset must already contain the
642  * precomputed relocation value, because the kernel will optimize out
643  * the relocation entry write when the buffer hasn't moved from the
644  * last known offset in target_bo.
645  */
646 static int
647 dri_gem_emit_reloc(dri_bo *bo, uint32_t read_domains, uint32_t write_domain,
648                    uint32_t delta, uint32_t offset, dri_bo *target_bo)
649 {
650     dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bo->bufmgr;
651     dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
652     dri_bo_gem *target_bo_gem = (dri_bo_gem *)target_bo;
653
654     /* Create a new relocation list if needed */
655     if (bo_gem->relocs == NULL)
656         intel_setup_reloc_list(bo);
657
658     /* Check overflow */
659     assert(bo_gem->reloc_count < bufmgr_gem->max_relocs);
660
661     /* Check args */
662     assert (offset <= bo->size - 4);
663     assert ((write_domain & (write_domain-1)) == 0);
664
665     bo_gem->relocs[bo_gem->reloc_count].offset = offset;
666     bo_gem->relocs[bo_gem->reloc_count].delta = delta;
667     bo_gem->relocs[bo_gem->reloc_count].target_handle =
668         target_bo_gem->gem_handle;
669     bo_gem->relocs[bo_gem->reloc_count].read_domains = read_domains;
670     bo_gem->relocs[bo_gem->reloc_count].write_domain = write_domain;
671     bo_gem->relocs[bo_gem->reloc_count].presumed_offset = target_bo->offset;
672
673     bo_gem->reloc_target_bo[bo_gem->reloc_count] = target_bo;
674     dri_bo_reference(target_bo);
675
676     bo_gem->reloc_count++;
677     return 0;
678 }
679
680 /**
681  * Walk the tree of relocations rooted at BO and accumulate the list of
682  * validations to be performed and update the relocation buffers with
683  * index values into the validation list.
684  */
685 static void
686 dri_gem_bo_process_reloc(dri_bo *bo)
687 {
688     dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
689     int i;
690
691     if (bo_gem->relocs == NULL)
692         return;
693
694     for (i = 0; i < bo_gem->reloc_count; i++) {
695         dri_bo *target_bo = bo_gem->reloc_target_bo[i];
696
697         /* Continue walking the tree depth-first. */
698         dri_gem_bo_process_reloc(target_bo);
699
700         /* Add the target to the validate list */
701         intel_add_validate_buffer(target_bo);
702     }
703 }
704
705 static void *
706 dri_gem_process_reloc(dri_bo *batch_buf)
707 {
708     dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *) batch_buf->bufmgr;
709
710     /* Update indices and set up the validate list. */
711     dri_gem_bo_process_reloc(batch_buf);
712
713     /* Add the batch buffer to the validation list.  There are no relocations
714      * pointing to it.
715      */
716     intel_add_validate_buffer(batch_buf);
717
718     bufmgr_gem->exec_arg.buffers_ptr = (uintptr_t)bufmgr_gem->exec_objects;
719     bufmgr_gem->exec_arg.buffer_count = bufmgr_gem->exec_count;
720     bufmgr_gem->exec_arg.batch_start_offset = 0;
721     bufmgr_gem->exec_arg.batch_len = 0; /* written in intel_exec_ioctl */
722
723     return &bufmgr_gem->exec_arg;
724 }
725
726 static void
727 intel_update_buffer_offsets (dri_bufmgr_gem *bufmgr_gem)
728 {
729     int i;
730
731     for (i = 0; i < bufmgr_gem->exec_count; i++) {
732         dri_bo *bo = bufmgr_gem->exec_bos[i];
733         dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
734
735         /* Update the buffer offset */
736         if (bufmgr_gem->exec_objects[i].offset != bo->offset) {
737             DBG("BO %d (%s) migrated: 0x%08lx -> 0x%08llx\n",
738                 bo_gem->gem_handle, bo_gem->name, bo->offset,
739                 bufmgr_gem->exec_objects[i].offset);
740             bo->offset = bufmgr_gem->exec_objects[i].offset;
741         }
742     }
743 }
744
745 static void
746 dri_gem_post_submit(dri_bo *batch_buf)
747 {
748     dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)batch_buf->bufmgr;
749     int i;
750
751     intel_update_buffer_offsets (bufmgr_gem);
752
753     if (bufmgr_gem->bufmgr.debug)
754         dri_gem_dump_validation_list(bufmgr_gem);
755
756     for (i = 0; i < bufmgr_gem->exec_count; i++) {
757         dri_bo *bo = bufmgr_gem->exec_bos[i];
758         dri_bo_gem *bo_gem = (dri_bo_gem *)bo;
759
760         /* Need to call swrast on next bo_map */
761         bo_gem->swrast = 0;
762
763         /* Disconnect the buffer from the validate list */
764         bo_gem->validate_index = -1;
765         dri_bo_unreference(bo);
766         bufmgr_gem->exec_bos[i] = NULL;
767     }
768     bufmgr_gem->exec_count = 0;
769 }
770
771 /**
772  * Enables unlimited caching of buffer objects for reuse.
773  *
774  * This is potentially very memory expensive, as the cache at each bucket
775  * size is only bounded by how many buffers of that size we've managed to have
776  * in flight at once.
777  */
778 void
779 intel_bufmgr_gem_enable_reuse(dri_bufmgr *bufmgr)
780 {
781     dri_bufmgr_gem *bufmgr_gem = (dri_bufmgr_gem *)bufmgr;
782     int i;
783
784     for (i = 0; i < INTEL_GEM_BO_BUCKETS; i++) {
785         bufmgr_gem->cache_bucket[i].max_entries = -1;
786     }
787 }
788
789 /*
790  *
791  */
792 static int
793 dri_gem_check_aperture_space(dri_bo *bo)
794 {
795     return 0;
796 }
797
798 /**
799  * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
800  * and manage map buffer objections.
801  *
802  * \param fd File descriptor of the opened DRM device.
803  */
804 dri_bufmgr *
805 intel_bufmgr_gem_init(int fd, int batch_size)
806 {
807     dri_bufmgr_gem *bufmgr_gem;
808     int i;
809
810     bufmgr_gem = calloc(1, sizeof(*bufmgr_gem));
811     bufmgr_gem->fd = fd;
812
813     /* Let's go with one relocation per every 2 dwords (but round down a bit
814      * since a power of two will mean an extra page allocation for the reloc
815      * buffer).
816      *
817      * Every 4 was too few for the blender benchmark.
818      */
819     bufmgr_gem->max_relocs = batch_size / sizeof(uint32_t) / 2 - 2;
820
821     bufmgr_gem->bufmgr.bo_alloc = dri_gem_bo_alloc;
822     bufmgr_gem->bufmgr.bo_reference = dri_gem_bo_reference;
823     bufmgr_gem->bufmgr.bo_unreference = dri_gem_bo_unreference;
824     bufmgr_gem->bufmgr.bo_map = dri_gem_bo_map;
825     bufmgr_gem->bufmgr.bo_unmap = dri_gem_bo_unmap;
826     bufmgr_gem->bufmgr.bo_subdata = dri_gem_bo_subdata;
827     bufmgr_gem->bufmgr.bo_get_subdata = dri_gem_bo_get_subdata;
828     bufmgr_gem->bufmgr.bo_wait_rendering = dri_gem_bo_wait_rendering;
829     bufmgr_gem->bufmgr.destroy = dri_bufmgr_gem_destroy;
830     bufmgr_gem->bufmgr.process_relocs = dri_gem_process_reloc;
831     bufmgr_gem->bufmgr.post_submit = dri_gem_post_submit;
832     bufmgr_gem->bufmgr.debug = 0;
833     bufmgr_gem->bufmgr.check_aperture_space = dri_gem_check_aperture_space;
834     bufmgr_gem->intel_bufmgr.emit_reloc = dri_gem_emit_reloc;
835     /* Initialize the linked lists for BO reuse cache. */
836     for (i = 0; i < INTEL_GEM_BO_BUCKETS; i++)
837         bufmgr_gem->cache_bucket[i].tail = &bufmgr_gem->cache_bucket[i].head;
838
839     return &bufmgr_gem->bufmgr;
840 }
841
842 int
843 intel_bo_emit_reloc(dri_bo *reloc_buf,
844                     uint32_t read_domains, uint32_t write_domain,
845                     uint32_t delta, uint32_t offset, dri_bo *target_buf)
846 {
847     struct intel_bufmgr *intel_bufmgr;
848
849     intel_bufmgr = (struct intel_bufmgr *)(reloc_buf->bufmgr + 1);
850
851     return intel_bufmgr->emit_reloc(reloc_buf, read_domains, write_domain,
852                                     delta, offset, target_buf);
853 }