OSDN Git Service

[intel] Use a static array for the validation list instead of a linked list.
[android-x86/external-mesa.git] / src / mesa / drivers / dri / intel / intel_bufmgr_ttm.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 <stdlib.h>
39 #include <unistd.h>
40 #include "glthread.h"
41 #include "errno.h"
42 #include "mtypes.h"
43 #include "dri_bufmgr.h"
44 #include "string.h"
45 #include "imports.h"
46
47 #include "i915_drm.h"
48
49 #include "intel_bufmgr_ttm.h"
50
51 #define DBG(...) do {                                   \
52    if (bufmgr_ttm->bufmgr.debug)                        \
53       _mesa_printf(__VA_ARGS__);                        \
54 } while (0)
55
56 /*
57  * These bits are always specified in each validation
58  * request. Other bits are not supported at this point
59  * as it would require a bit of investigation to figure
60  * out what mask value should be used.
61  */
62 #define INTEL_BO_MASK  (DRM_BO_MASK_MEM | \
63                         DRM_BO_FLAG_READ | \
64                         DRM_BO_FLAG_WRITE | \
65                         DRM_BO_FLAG_EXE)
66
67 struct intel_validate_entry {
68     dri_bo *bo;
69     struct drm_i915_op_arg bo_arg;
70 };
71
72 typedef struct _dri_bufmgr_ttm {
73     dri_bufmgr bufmgr;
74
75     int fd;
76     unsigned int fence_type;
77     unsigned int fence_type_flush;
78
79     uint32_t max_relocs;
80
81     struct intel_validate_entry *validate_array;
82     int validate_array_size;
83     int validate_count;
84
85     drmBO *cached_reloc_buf;
86     uint32_t *cached_reloc_buf_data;
87 } dri_bufmgr_ttm;
88
89 /**
90  * Private information associated with a relocation that isn't already stored
91  * in the relocation buffer to be passed to the kernel.
92  */
93 struct dri_ttm_reloc {
94     dri_bo *target_buf;
95     uint64_t validate_flags;
96 };
97
98 typedef struct _dri_bo_ttm {
99     dri_bo bo;
100
101     int refcount;
102     drmBO drm_bo;
103     const char *name;
104
105     uint64_t last_flags;
106
107     /**
108      * Index of the buffer within the validation list while preparing a
109      * batchbuffer execution.
110      */
111     int validate_index;
112
113     /** DRM buffer object containing relocation list */
114     drmBO *reloc_buf;
115     uint32_t *reloc_buf_data;
116     struct dri_ttm_reloc *relocs;
117
118     /**
119      * Indicates that the buffer may be shared with other processes, so we
120      * can't hold maps beyond when the user does.
121      */
122     GLboolean shared;
123
124     GLboolean delayed_unmap;
125     /* Virtual address from the dri_bo_map whose unmap was delayed. */
126     void *saved_virtual;
127 } dri_bo_ttm;
128
129 typedef struct _dri_fence_ttm
130 {
131     dri_fence fence;
132
133     int refcount;
134     const char *name;
135     drmFence drm_fence;
136 } dri_fence_ttm;
137
138 static void dri_ttm_dump_validation_list(dri_bufmgr_ttm *bufmgr_ttm)
139 {
140     int i, j;
141
142     for (i = 0; i < bufmgr_ttm->validate_count; i++) {
143         dri_bo *bo = bufmgr_ttm->validate_array[i].bo;
144         dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
145
146         if (bo_ttm->reloc_buf_data != NULL) {
147             for (j = 0; j < (bo_ttm->reloc_buf_data[0] & 0xffff); j++) {
148                 uint32_t *reloc_entry = bo_ttm->reloc_buf_data +
149                     I915_RELOC_HEADER +
150                     j * I915_RELOC0_STRIDE;
151                 dri_bo *target_bo =
152                     bufmgr_ttm->validate_array[reloc_entry[2]].bo;
153                 dri_bo_ttm *target_ttm = (dri_bo_ttm *)target_bo;
154
155                 DBG("%2d: %s@0x%08x -> %s@0x%08x + 0x%08x\n",
156                     i,
157                     bo_ttm->name, reloc_entry[0],
158                     target_ttm->name, target_bo->offset,
159                     reloc_entry[1]);
160             }
161         } else {
162             DBG("%2d: %s\n", i, bo_ttm->name);
163         }
164     }
165 }
166
167 /**
168  * Adds the given buffer to the list of buffers to be validated (moved into the
169  * appropriate memory type) with the next batch submission.
170  *
171  * If a buffer is validated multiple times in a batch submission, it ends up
172  * with the intersection of the memory type flags and the union of the
173  * access flags.
174  */
175 static void
176 intel_add_validate_buffer(dri_bo *buf,
177                           uint64_t flags)
178 {
179     dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;
180     dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
181
182     /* If we delayed doing an unmap to mitigate map/unmap syscall thrashing,
183      * do that now.
184      */
185     if (ttm_buf->delayed_unmap) {
186         drmBOUnmap(bufmgr_ttm->fd, &ttm_buf->drm_bo);
187         ttm_buf->delayed_unmap = GL_FALSE;
188     }
189
190     if (ttm_buf->validate_index == -1) {
191         struct intel_validate_entry *entry;
192         struct drm_i915_op_arg *arg;
193         struct drm_bo_op_req *req;
194         int index;
195
196         /* Extend the array of validation entries as necessary. */
197         if (bufmgr_ttm->validate_count == bufmgr_ttm->validate_array_size) {
198             int i, new_size = bufmgr_ttm->validate_array_size * 2;
199
200             if (new_size == 0)
201                 new_size = 5;
202
203             bufmgr_ttm->validate_array =
204                realloc(bufmgr_ttm->validate_array,
205                        sizeof(struct intel_validate_entry) * new_size);
206             bufmgr_ttm->validate_array_size = new_size;
207
208             /* Update pointers for realloced mem. */
209             for (i = 0; i < bufmgr_ttm->validate_count - 1; i++) {
210                bufmgr_ttm->validate_array[i].bo_arg.next = (unsigned long)
211                   &bufmgr_ttm->validate_array[i + 1].bo_arg;
212             }
213         }
214
215         /* Pick out the new array entry for ourselves */
216         index = bufmgr_ttm->validate_count;
217         ttm_buf->validate_index = index;
218         entry = &bufmgr_ttm->validate_array[index];
219         bufmgr_ttm->validate_count++;
220
221         /* Fill in array entry */
222         entry->bo = buf;
223         dri_bo_reference(buf);
224
225         /* Fill in kernel arg */
226         arg = &entry->bo_arg;
227         req = &arg->d.req;
228
229         memset(arg, 0, sizeof(*arg));
230         req->bo_req.handle = ttm_buf->drm_bo.handle;
231         req->op = drm_bo_validate;
232         req->bo_req.flags = flags;
233         req->bo_req.hint = 0;
234 #ifdef DRM_BO_HINT_PRESUMED_OFFSET
235         req->bo_req.hint |= DRM_BO_HINT_PRESUMED_OFFSET;
236         req->bo_req.presumed_offset = buf->offset;
237 #endif
238         req->bo_req.mask = INTEL_BO_MASK;
239         req->bo_req.fence_class = 0; /* Backwards compat. */
240
241         if (ttm_buf->reloc_buf != NULL)
242             arg->reloc_handle = ttm_buf->reloc_buf->handle;
243         else
244             arg->reloc_handle = 0;
245
246         /* Hook up the linked list of args for the kernel */
247         arg->next = 0;
248         if (index != 0) {
249             bufmgr_ttm->validate_array[index - 1].bo_arg.next =
250                 (unsigned long)arg;
251         }
252     } else {
253         struct intel_validate_entry *entry =
254             &bufmgr_ttm->validate_array[ttm_buf->validate_index];
255         struct drm_i915_op_arg *arg = &entry->bo_arg;
256         struct drm_bo_op_req *req = &arg->d.req;
257         uint64_t memFlags = req->bo_req.flags & flags & DRM_BO_MASK_MEM;
258         uint64_t modeFlags = (req->bo_req.flags | flags) & ~DRM_BO_MASK_MEM;
259
260         /* Buffer was already in the validate list.  Extend its flags as
261          * necessary.
262          */
263
264         if (memFlags == 0) {
265             fprintf(stderr,
266                     "%s: No shared memory types between "
267                     "0x%16llx and 0x%16llx\n",
268                     __FUNCTION__, req->bo_req.flags, flags);
269             abort();
270         }
271         if (flags & ~INTEL_BO_MASK) {
272             fprintf(stderr,
273                     "%s: Flags bits 0x%16llx are not supposed to be used in a relocation\n",
274                     __FUNCTION__, flags & ~INTEL_BO_MASK);
275             abort();
276         }
277         req->bo_req.flags = memFlags | modeFlags;
278     }
279 }
280
281
282 #define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \
283         sizeof(uint32_t))
284
285 static int
286 intel_setup_reloc_list(dri_bo *bo)
287 {
288     dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
289     dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bo->bufmgr;
290     int ret;
291
292     bo_ttm->relocs = malloc(sizeof(struct dri_ttm_reloc) *
293                             bufmgr_ttm->max_relocs);
294
295     if (bufmgr_ttm->cached_reloc_buf != NULL) {
296        bo_ttm->reloc_buf = bufmgr_ttm->cached_reloc_buf;
297        bo_ttm->reloc_buf_data = bufmgr_ttm->cached_reloc_buf_data;
298
299        bufmgr_ttm->cached_reloc_buf = NULL;
300        bufmgr_ttm->cached_reloc_buf_data = NULL;
301     } else {
302        bo_ttm->reloc_buf = malloc(sizeof(bo_ttm->drm_bo));
303        ret = drmBOCreate(bufmgr_ttm->fd,
304                          RELOC_BUF_SIZE(bufmgr_ttm->max_relocs), 0,
305                          NULL,
306                          DRM_BO_FLAG_MEM_LOCAL |
307                          DRM_BO_FLAG_READ |
308                          DRM_BO_FLAG_WRITE |
309                          DRM_BO_FLAG_MAPPABLE |
310                          DRM_BO_FLAG_CACHED,
311                          0, bo_ttm->reloc_buf);
312        if (ret) {
313           fprintf(stderr, "Failed to create relocation BO: %s\n",
314                   strerror(-ret));
315           return ret;
316        }
317
318        ret = drmBOMap(bufmgr_ttm->fd, bo_ttm->reloc_buf,
319                       DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE,
320                       0, (void **)&bo_ttm->reloc_buf_data);
321        if (ret) {
322           fprintf(stderr, "Failed to map relocation BO: %s\n",
323                   strerror(-ret));
324           return ret;
325        }
326     }
327
328     /* Initialize the relocation list with the header:
329      * DWORD 0: relocation type, relocation count
330      * DWORD 1: handle to next relocation list (currently none)
331      * DWORD 2: unused
332      * DWORD 3: unused
333      */
334     bo_ttm->reloc_buf_data[0] = I915_RELOC_TYPE_0 << 16;
335     bo_ttm->reloc_buf_data[1] = 0;
336     bo_ttm->reloc_buf_data[2] = 0;
337     bo_ttm->reloc_buf_data[3] = 0;
338
339     return 0;
340 }
341
342 #if 0
343 int
344 driFenceSignaled(DriFenceObject * fence, unsigned type)
345 {
346     int signaled;
347     int ret;
348
349     if (fence == NULL)
350         return GL_TRUE;
351
352     ret = drmFenceSignaled(bufmgr_ttm->fd, &fence->fence, type, &signaled);
353     BM_CKFATAL(ret);
354     return signaled;
355 }
356 #endif
357
358 static dri_bo *
359 dri_ttm_alloc(dri_bufmgr *bufmgr, const char *name,
360               unsigned long size, unsigned int alignment,
361               uint64_t location_mask)
362 {
363     dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bufmgr;
364     dri_bo_ttm *ttm_buf;
365     unsigned int pageSize = getpagesize();
366     int ret;
367     unsigned int flags, hint;
368
369     ttm_buf = malloc(sizeof(*ttm_buf));
370     if (!ttm_buf)
371         return NULL;
372
373     /* The mask argument doesn't do anything for us that we want other than
374      * determine which pool (TTM or local) the buffer is allocated into, so
375      * just pass all of the allocation class flags.
376      */
377     flags = location_mask | DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE |
378         DRM_BO_FLAG_EXE;
379     /* No hints we want to use. */
380     hint = 0;
381
382     ret = drmBOCreate(bufmgr_ttm->fd, size, alignment / pageSize,
383                       NULL, flags, hint, &ttm_buf->drm_bo);
384     if (ret != 0) {
385         free(ttm_buf);
386         return NULL;
387     }
388     ttm_buf->bo.size = ttm_buf->drm_bo.size;
389     ttm_buf->bo.offset = ttm_buf->drm_bo.offset;
390     ttm_buf->bo.virtual = NULL;
391     ttm_buf->bo.bufmgr = bufmgr;
392     ttm_buf->name = name;
393     ttm_buf->refcount = 1;
394     ttm_buf->reloc_buf = NULL;
395     ttm_buf->reloc_buf_data = NULL;
396     ttm_buf->relocs = NULL;
397     ttm_buf->last_flags = ttm_buf->drm_bo.flags;
398     ttm_buf->shared = GL_FALSE;
399     ttm_buf->delayed_unmap = GL_FALSE;
400     ttm_buf->validate_index = -1;
401
402     DBG("bo_create: %p (%s) %db\n", &ttm_buf->bo, ttm_buf->name, size);
403
404     return &ttm_buf->bo;
405 }
406
407 /* Our TTM backend doesn't allow creation of static buffers, as that requires
408  * privelege for the non-fake case, and the lock in the fake case where we were
409  * working around the X Server not creating buffers and passing handles to us.
410  */
411 static dri_bo *
412 dri_ttm_alloc_static(dri_bufmgr *bufmgr, const char *name,
413                      unsigned long offset, unsigned long size, void *virtual,
414                      uint64_t location_mask)
415 {
416     return NULL;
417 }
418
419 /**
420  * Returns a dri_bo wrapping the given buffer object handle.
421  *
422  * This can be used when one application needs to pass a buffer object
423  * to another.
424  */
425 dri_bo *
426 intel_ttm_bo_create_from_handle(dri_bufmgr *bufmgr, const char *name,
427                               unsigned int handle)
428 {
429     dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bufmgr;
430     dri_bo_ttm *ttm_buf;
431     int ret;
432
433     ttm_buf = malloc(sizeof(*ttm_buf));
434     if (!ttm_buf)
435         return NULL;
436
437     ret = drmBOReference(bufmgr_ttm->fd, handle, &ttm_buf->drm_bo);
438     if (ret != 0) {
439        fprintf(stderr, "Couldn't reference %s handle 0x%08x: %s\n",
440                name, handle, strerror(-ret));
441         free(ttm_buf);
442         return NULL;
443     }
444     ttm_buf->bo.size = ttm_buf->drm_bo.size;
445     ttm_buf->bo.offset = ttm_buf->drm_bo.offset;
446     ttm_buf->bo.virtual = NULL;
447     ttm_buf->bo.bufmgr = bufmgr;
448     ttm_buf->name = name;
449     ttm_buf->refcount = 1;
450     ttm_buf->reloc_buf = NULL;
451     ttm_buf->reloc_buf_data = NULL;
452     ttm_buf->relocs = NULL;
453     ttm_buf->last_flags = ttm_buf->drm_bo.flags;
454     ttm_buf->shared = GL_TRUE;
455     ttm_buf->delayed_unmap = GL_FALSE;
456     ttm_buf->validate_index = -1;
457
458     DBG("bo_create_from_handle: %p %08x (%s)\n",
459         &ttm_buf->bo, handle, ttm_buf->name);
460
461     return &ttm_buf->bo;
462 }
463
464 static void
465 dri_ttm_bo_reference(dri_bo *buf)
466 {
467     dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
468
469     ttm_buf->refcount++;
470 }
471
472 static void
473 dri_ttm_bo_unreference(dri_bo *buf)
474 {
475     dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;
476     dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
477
478     if (!buf)
479         return;
480
481     if (--ttm_buf->refcount == 0) {
482         int ret;
483
484         if (ttm_buf->reloc_buf) {
485             int i;
486
487             /* Unreference all the target buffers */
488             for (i = 0; i < (ttm_buf->reloc_buf_data[0] & 0xffff); i++)
489                  dri_bo_unreference(ttm_buf->relocs[i].target_buf);
490             free(ttm_buf->relocs);
491
492             if (bufmgr_ttm->cached_reloc_buf == NULL) {
493                /* Cache a single relocation buffer allocation to avoid
494                 * repeated create/map/unmap/destroy for batchbuffer
495                 * relocations.
496                 */
497                bufmgr_ttm->cached_reloc_buf = ttm_buf->reloc_buf;
498                bufmgr_ttm->cached_reloc_buf_data = ttm_buf->reloc_buf_data;
499             } else {
500                /* Free the kernel BO containing relocation entries */
501                drmBOUnmap(bufmgr_ttm->fd, ttm_buf->reloc_buf);
502                drmBOUnreference(bufmgr_ttm->fd, ttm_buf->reloc_buf);
503                free(ttm_buf->reloc_buf);
504             }
505         }
506
507         if (ttm_buf->delayed_unmap)
508            drmBOUnmap(bufmgr_ttm->fd, &ttm_buf->drm_bo);
509
510         ret = drmBOUnreference(bufmgr_ttm->fd, &ttm_buf->drm_bo);
511         if (ret != 0) {
512             fprintf(stderr, "drmBOUnreference failed (%s): %s\n",
513                     ttm_buf->name, strerror(-ret));
514         }
515         DBG("bo_unreference final: %p (%s)\n", &ttm_buf->bo, ttm_buf->name);
516
517         free(buf);
518         return;
519     }
520 }
521
522 static int
523 dri_ttm_bo_map(dri_bo *buf, GLboolean write_enable)
524 {
525     dri_bufmgr_ttm *bufmgr_ttm;
526     dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
527     unsigned int flags;
528
529     bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;
530
531     flags = DRM_BO_FLAG_READ;
532     if (write_enable)
533         flags |= DRM_BO_FLAG_WRITE;
534
535     assert(buf->virtual == NULL);
536
537     DBG("bo_map: %p (%s)\n", &ttm_buf->bo, ttm_buf->name);
538
539     /* XXX: What about if we're upgrading from READ to WRITE? */
540     if (ttm_buf->delayed_unmap) {
541         buf->virtual = ttm_buf->saved_virtual;
542         return 0;
543     }
544
545     return drmBOMap(bufmgr_ttm->fd, &ttm_buf->drm_bo, flags, 0, &buf->virtual);
546 }
547
548 static int
549 dri_ttm_bo_unmap(dri_bo *buf)
550 {
551     dri_bufmgr_ttm *bufmgr_ttm;
552     dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
553
554     if (buf == NULL)
555         return 0;
556
557     bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;
558
559     assert(buf->virtual != NULL);
560
561     DBG("bo_unmap: %p (%s)\n", &ttm_buf->bo, ttm_buf->name);
562
563     if (!ttm_buf->shared) {
564         ttm_buf->saved_virtual = buf->virtual;
565         ttm_buf->delayed_unmap = GL_TRUE;
566         buf->virtual = NULL;
567
568         return 0;
569     }
570
571     buf->virtual = NULL;
572
573     return drmBOUnmap(bufmgr_ttm->fd, &ttm_buf->drm_bo);
574 }
575
576 /**
577  * Returns a dri_bo wrapping the given buffer object handle.
578  *
579  * This can be used when one application needs to pass a buffer object
580  * to another.
581  */
582 dri_fence *
583 intel_ttm_fence_create_from_arg(dri_bufmgr *bufmgr, const char *name,
584                                 drm_fence_arg_t *arg)
585 {
586     dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bufmgr;
587     dri_fence_ttm *ttm_fence;
588
589     ttm_fence = malloc(sizeof(*ttm_fence));
590     if (!ttm_fence)
591         return NULL;
592
593     ttm_fence->drm_fence.handle = arg->handle;
594     ttm_fence->drm_fence.fence_class = arg->fence_class;
595     ttm_fence->drm_fence.type = arg->type;
596     ttm_fence->drm_fence.flags = arg->flags;
597     ttm_fence->drm_fence.signaled = 0;
598     ttm_fence->drm_fence.sequence = arg->sequence;
599
600     ttm_fence->fence.bufmgr = bufmgr;
601     ttm_fence->name = name;
602     ttm_fence->refcount = 1;
603
604     DBG("fence_create_from_handle: %p (%s)\n",
605         &ttm_fence->fence, ttm_fence->name);
606
607     return &ttm_fence->fence;
608 }
609
610
611 static void
612 dri_ttm_fence_reference(dri_fence *fence)
613 {
614     dri_fence_ttm *fence_ttm = (dri_fence_ttm *)fence;
615     dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)fence->bufmgr;
616
617     ++fence_ttm->refcount;
618     DBG("fence_reference: %p (%s)\n", &fence_ttm->fence, fence_ttm->name);
619 }
620
621 static void
622 dri_ttm_fence_unreference(dri_fence *fence)
623 {
624     dri_fence_ttm *fence_ttm = (dri_fence_ttm *)fence;
625     dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)fence->bufmgr;
626
627     if (!fence)
628         return;
629
630     DBG("fence_unreference: %p (%s)\n", &fence_ttm->fence, fence_ttm->name);
631
632     if (--fence_ttm->refcount == 0) {
633         int ret;
634
635         ret = drmFenceUnreference(bufmgr_ttm->fd, &fence_ttm->drm_fence);
636         if (ret != 0) {
637             fprintf(stderr, "drmFenceUnreference failed (%s): %s\n",
638                     fence_ttm->name, strerror(-ret));
639         }
640
641         free(fence);
642         return;
643     }
644 }
645
646 static void
647 dri_ttm_fence_wait(dri_fence *fence)
648 {
649     dri_fence_ttm *fence_ttm = (dri_fence_ttm *)fence;
650     dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)fence->bufmgr;
651     int ret;
652
653     ret = drmFenceWait(bufmgr_ttm->fd, DRM_FENCE_FLAG_WAIT_LAZY, &fence_ttm->drm_fence, 0);
654     if (ret != 0) {
655         _mesa_printf("%s:%d: Error %d waiting for fence %s.\n",
656                      __FILE__, __LINE__, ret, fence_ttm->name);
657         abort();
658     }
659
660     DBG("fence_wait: %p (%s)\n", &fence_ttm->fence, fence_ttm->name);
661 }
662
663 static void
664 dri_bufmgr_ttm_destroy(dri_bufmgr *bufmgr)
665 {
666     dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bufmgr;
667
668     if (bufmgr_ttm->cached_reloc_buf) {
669        /* Free the cached kernel BO containing relocation entries */
670        drmBOUnmap(bufmgr_ttm->fd, bufmgr_ttm->cached_reloc_buf);
671        drmBOUnreference(bufmgr_ttm->fd, bufmgr_ttm->cached_reloc_buf);
672        free(bufmgr_ttm->cached_reloc_buf);
673     }
674
675     free(bufmgr_ttm->validate_array);
676
677     free(bufmgr);
678 }
679
680 /**
681  * Adds the target buffer to the validation list and adds the relocation
682  * to the reloc_buffer's relocation list.
683  *
684  * The relocation entry at the given offset must already contain the
685  * precomputed relocation value, because the kernel will optimize out
686  * the relocation entry write when the buffer hasn't moved from the
687  * last known offset in target_buf.
688  */
689 static void
690 dri_ttm_emit_reloc(dri_bo *reloc_buf, uint64_t flags, GLuint delta,
691                    GLuint offset, dri_bo *target_buf)
692 {
693     dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)reloc_buf->bufmgr;
694     dri_bo_ttm *reloc_buf_ttm = (dri_bo_ttm *)reloc_buf;
695     int num_relocs;
696     uint32_t *this_reloc;
697
698     /* Create a new relocation list if needed */
699     if (reloc_buf_ttm->reloc_buf == NULL)
700         intel_setup_reloc_list(reloc_buf);
701
702     num_relocs = (reloc_buf_ttm->reloc_buf_data[0] & 0xffff);
703
704     /* Check overflow */
705     assert((reloc_buf_ttm->reloc_buf_data[0] & 0xffff) <
706            bufmgr_ttm->max_relocs);
707
708     this_reloc = reloc_buf_ttm->reloc_buf_data + I915_RELOC_HEADER +
709         num_relocs * I915_RELOC0_STRIDE;
710
711     this_reloc[0] = offset;
712     this_reloc[1] = delta;
713     this_reloc[2] = -1; /* To be filled in at exec time */
714     this_reloc[3] = 0;
715
716     reloc_buf_ttm->relocs[num_relocs].validate_flags = flags;
717     reloc_buf_ttm->relocs[num_relocs].target_buf = target_buf;
718     dri_bo_reference(target_buf);
719
720     reloc_buf_ttm->reloc_buf_data[0]++; /* Increment relocation count */
721     /* Check wraparound */
722     assert((reloc_buf_ttm->reloc_buf_data[0] & 0xffff) != 0);
723 }
724
725 /**
726  * Walk the tree of relocations rooted at BO and accumulate the list of
727  * validations to be performed and update the relocation buffers with
728  * index values into the validation list.
729  */
730 static void
731 dri_ttm_bo_process_reloc(dri_bo *bo)
732 {
733     dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
734     unsigned int nr_relocs;
735     int i;
736
737     if (bo_ttm->reloc_buf_data == NULL)
738         return;
739
740     nr_relocs = bo_ttm->reloc_buf_data[0] & 0xffff;
741
742     for (i = 0; i < nr_relocs; i++) {
743         struct dri_ttm_reloc *r = &bo_ttm->relocs[i];
744         dri_bo_ttm *target_ttm = (dri_bo_ttm *)r->target_buf;
745         uint32_t *reloc_entry;
746
747         /* Continue walking the tree depth-first. */
748         dri_ttm_bo_process_reloc(r->target_buf);
749
750         /* Add the target to the validate list */
751         intel_add_validate_buffer(r->target_buf, r->validate_flags);
752
753         /* Update the index of the target in the relocation entry */
754         reloc_entry = bo_ttm->reloc_buf_data + I915_RELOC_HEADER +
755             i * I915_RELOC0_STRIDE;
756         reloc_entry[2] = target_ttm->validate_index;
757     }
758 }
759
760 static void *
761 dri_ttm_process_reloc(dri_bo *batch_buf, GLuint *count)
762 {
763     dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)batch_buf->bufmgr;
764
765     /* Update indices and set up the validate list. */
766     dri_ttm_bo_process_reloc(batch_buf);
767
768     /* Add the batch buffer to the validation list.  There are no relocations
769      * pointing to it.
770      */
771     intel_add_validate_buffer(batch_buf,
772                               DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_EXE);
773
774     *count = bufmgr_ttm->validate_count;
775     return &bufmgr_ttm->validate_array[0].bo_arg;
776 }
777
778 static const char *
779 intel_get_flags_mem_type_string(uint64_t flags)
780 {
781     switch (flags & DRM_BO_MASK_MEM) {
782     case DRM_BO_FLAG_MEM_LOCAL: return "local";
783     case DRM_BO_FLAG_MEM_TT: return "ttm";
784     case DRM_BO_FLAG_MEM_VRAM: return "vram";
785     case DRM_BO_FLAG_MEM_PRIV0: return "priv0";
786     case DRM_BO_FLAG_MEM_PRIV1: return "priv1";
787     case DRM_BO_FLAG_MEM_PRIV2: return "priv2";
788     case DRM_BO_FLAG_MEM_PRIV3: return "priv3";
789     case DRM_BO_FLAG_MEM_PRIV4: return "priv4";
790     default: return NULL;
791     }
792 }
793
794 static const char *
795 intel_get_flags_caching_string(uint64_t flags)
796 {
797     switch (flags & (DRM_BO_FLAG_CACHED | DRM_BO_FLAG_CACHED_MAPPED)) {
798     case 0: return "UU";
799     case DRM_BO_FLAG_CACHED: return "CU";
800     case DRM_BO_FLAG_CACHED_MAPPED: return "UC";
801     case DRM_BO_FLAG_CACHED | DRM_BO_FLAG_CACHED_MAPPED: return "CC";
802     default: return NULL;
803     }
804 }
805
806 static void
807 intel_update_buffer_offsets (dri_bufmgr_ttm *bufmgr_ttm)
808 {
809     int i;
810
811     for (i = 0; i < bufmgr_ttm->validate_count; i++) {
812         dri_bo *bo = bufmgr_ttm->validate_array[i].bo;
813         dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
814         struct drm_i915_op_arg *arg = &bufmgr_ttm->validate_array[i].bo_arg;
815         struct drm_bo_arg_rep *rep = &arg->d.rep;
816
817         /* Update the flags */
818         if (rep->bo_info.flags != bo_ttm->last_flags) {
819             DBG("BO %s migrated: %s/%s -> %s/%s\n",
820                 bo_ttm->name,
821                 intel_get_flags_mem_type_string(bo_ttm->last_flags),
822                 intel_get_flags_caching_string(bo_ttm->last_flags),
823                 intel_get_flags_mem_type_string(rep->bo_info.flags),
824                 intel_get_flags_caching_string(rep->bo_info.flags));
825
826             bo_ttm->last_flags = rep->bo_info.flags;
827         }
828         /* Update the buffer offset */
829         if (rep->bo_info.offset != bo->offset) {
830             DBG("BO %s migrated: 0x%08x -> 0x%08x\n",
831                 bo_ttm->name, bo->offset, rep->bo_info.offset);
832             bo->offset = rep->bo_info.offset;
833         }
834     }
835 }
836
837 static void
838 dri_ttm_post_submit(dri_bo *batch_buf, dri_fence **last_fence)
839 {
840     dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)batch_buf->bufmgr;
841     int i;
842
843     intel_update_buffer_offsets (bufmgr_ttm);
844
845     if (bufmgr_ttm->bufmgr.debug)
846         dri_ttm_dump_validation_list(bufmgr_ttm);
847
848     for (i = 0; i < bufmgr_ttm->validate_count; i++) {
849         dri_bo *bo = bufmgr_ttm->validate_array[i].bo;
850         dri_bo_ttm *bo_ttm = (dri_bo_ttm *)bo;
851
852         /* Disconnect the buffer from the validate list */
853         bo_ttm->validate_index = -1;
854         dri_bo_unreference(bo);
855         bufmgr_ttm->validate_array[i].bo = NULL;
856     }
857     bufmgr_ttm->validate_count = 0;
858 }
859
860 /**
861  * Initializes the TTM buffer manager, which uses the kernel to allocate, map,
862  * and manage map buffer objections.
863  *
864  * \param fd File descriptor of the opened DRM device.
865  * \param fence_type Driver-specific fence type used for fences with no flush.
866  * \param fence_type_flush Driver-specific fence type used for fences with a
867  *        flush.
868  */
869 dri_bufmgr *
870 intel_bufmgr_ttm_init(int fd, unsigned int fence_type,
871                       unsigned int fence_type_flush, int batch_size)
872 {
873     dri_bufmgr_ttm *bufmgr_ttm;
874
875     bufmgr_ttm = calloc(1, sizeof(*bufmgr_ttm));
876     bufmgr_ttm->fd = fd;
877     bufmgr_ttm->fence_type = fence_type;
878     bufmgr_ttm->fence_type_flush = fence_type_flush;
879     bufmgr_ttm->cached_reloc_buf = NULL;
880     bufmgr_ttm->cached_reloc_buf_data = NULL;
881
882     /* Let's go with one relocation per every 2 dwords (but round down a bit
883      * since a power of two will mean an extra page allocation for the reloc
884      * buffer).
885      *
886      * Every 4 was too few for the blender benchmark.
887      */
888     bufmgr_ttm->max_relocs = batch_size / sizeof(uint32_t) / 2 - 2;
889
890     bufmgr_ttm->bufmgr.bo_alloc = dri_ttm_alloc;
891     bufmgr_ttm->bufmgr.bo_alloc_static = dri_ttm_alloc_static;
892     bufmgr_ttm->bufmgr.bo_reference = dri_ttm_bo_reference;
893     bufmgr_ttm->bufmgr.bo_unreference = dri_ttm_bo_unreference;
894     bufmgr_ttm->bufmgr.bo_map = dri_ttm_bo_map;
895     bufmgr_ttm->bufmgr.bo_unmap = dri_ttm_bo_unmap;
896     bufmgr_ttm->bufmgr.fence_reference = dri_ttm_fence_reference;
897     bufmgr_ttm->bufmgr.fence_unreference = dri_ttm_fence_unreference;
898     bufmgr_ttm->bufmgr.fence_wait = dri_ttm_fence_wait;
899     bufmgr_ttm->bufmgr.destroy = dri_bufmgr_ttm_destroy;
900     bufmgr_ttm->bufmgr.emit_reloc = dri_ttm_emit_reloc;
901     bufmgr_ttm->bufmgr.process_relocs = dri_ttm_process_reloc;
902     bufmgr_ttm->bufmgr.post_submit = dri_ttm_post_submit;
903     bufmgr_ttm->bufmgr.debug = GL_FALSE;
904
905     return &bufmgr_ttm->bufmgr;
906 }
907