summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/common/dri_bufmgr_ttm.c
blob: edf2a923ce1be4437d593095e2dfad4d592420a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**************************************************************************
 * 
 * Copyright © 2007 Intel Corporation
 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * 
 **************************************************************************/
/*
 * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
 *          Keith Whitwell <keithw-at-tungstengraphics-dot-com>
 *	    Eric Anholt <eric@anholt.net>
 */

#include <xf86drm.h>
#include <stdlib.h>
#include <unistd.h>
#include "glthread.h"
#include "errno.h"
#include "mtypes.h"
#include "dri_bufmgr.h"
#include "string.h"
#include "imports.h"

typedef struct _dri_bufmgr_ttm {
   dri_bufmgr bufmgr;

   int fd;
   _glthread_Mutex mutex;
   unsigned int fence_type;
   unsigned int fence_type_flush;
} dri_bufmgr_ttm;

typedef struct _dri_bo_ttm {
   dri_bo bo;

   int refcount;		/* Protected by bufmgr->mutex */
   drmBO drm_bo;
} dri_bo_ttm;

typedef struct _dri_fence_ttm
{
   dri_fence fence;

   int refcount;		/* Protected by bufmgr->mutex */
   /** Fence type from when the fence was created, used for later waits */
   unsigned int type;
   const char *name;
   drmFence drm_fence;
} dri_fence_ttm;

#if 0
int
driFenceSignaled(DriFenceObject * fence, unsigned type)
{
   int signaled;
   int ret;

   if (fence == NULL)
      return GL_TRUE;

   _glthread_LOCK_MUTEX(fence->mutex);
   ret = drmFenceSignaled(bufmgr_ttm->fd, &fence->fence, type, &signaled);
   _glthread_UNLOCK_MUTEX(fence->mutex);
   BM_CKFATAL(ret);
   return signaled;
}
#endif

static dri_bo *
dri_ttm_alloc(dri_bufmgr *bufmgr, const char *name,
	      unsigned long size, unsigned int alignment, unsigned int flags,
	      unsigned int hint)
{
   dri_bufmgr_ttm *ttm_bufmgr;
   dri_bo_ttm *ttm_buf;
   unsigned int pageSize = getpagesize();
   int ret;

   ttm_bufmgr = (dri_bufmgr_ttm *)bufmgr;

   ttm_buf = malloc(sizeof(*ttm_buf));
   if (!ttm_buf)
      return NULL;

   ret = drmBOCreate(ttm_bufmgr->fd, 0, size, alignment / pageSize,
		     NULL, drm_bo_type_dc,
                     flags, hint, &ttm_buf->drm_bo);
   if (ret != 0) {
      free(ttm_buf);
      return NULL;
   }
   ttm_buf->bo.size = ttm_buf->drm_bo.size;
   ttm_buf->bo.offset = ttm_buf->drm_bo.offset;
   ttm_buf->bo.virtual = NULL;
   ttm_buf->bo.bufmgr = bufmgr;
   ttm_buf->refcount = 1;

   return &ttm_buf->bo;
}

static dri_bo *
dri_ttm_alloc_static(dri_bufmgr *bufmgr, const char *name,
		     unsigned long offset, unsigned long size, void *virtual,
		     unsigned int flags, unsigned int hint)
{
   dri_bufmgr_ttm *ttm_bufmgr;
   dri_bo_ttm *ttm_buf;
   int ret;

   ttm_bufmgr = (dri_bufmgr_ttm *)bufmgr;

   ttm_buf = malloc(sizeof(*ttm_buf));
   if (!ttm_buf)
      return NULL;

   ret = drmBOCreate(ttm_bufmgr->fd, offset, size, 0,
		     NULL, drm_bo_type_fake,
                     flags, 0, &ttm_buf->drm_bo);
   if (ret != 0) {
      free(ttm_buf);
      return NULL;
   }
   ttm_buf->bo.size = ttm_buf->drm_bo.size;
   ttm_buf->bo.offset = ttm_buf->drm_bo.offset;
   ttm_buf->bo.virtual = virtual;
   ttm_buf->bo.bufmgr = bufmgr;
   ttm_buf->refcount = 1;

   return &ttm_buf->bo;
}

static void
dri_ttm_bo_reference(dri_bo *buf)
{
   dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;
   dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;

   _glthread_LOCK_MUTEX(bufmgr_ttm->mutex);
   ttm_buf->refcount++;
   _glthread_UNLOCK_MUTEX(bufmgr_ttm->mutex);
}

static void
dri_ttm_bo_unreference(dri_bo *buf)
{
   dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;
   dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;

   if (!buf)
      return;

   _glthread_LOCK_MUTEX(bufmgr_ttm->mutex);
   if (--ttm_buf->refcount == 0) {
      drmBOUnReference(bufmgr_ttm->fd, &ttm_buf->drm_bo);
      _glthread_UNLOCK_MUTEX(bufmgr_ttm->mutex);
      free(buf);
      return;
   }
   _glthread_UNLOCK_MUTEX(bufmgr_ttm->mutex);
}

static int
dri_ttm_bo_map(dri_bo *buf, GLboolean write_enable)
{
   dri_bufmgr_ttm *bufmgr_ttm;
   dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
   unsigned int flags;

   bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;

   flags = DRM_BO_FLAG_READ;
   if (write_enable)
       flags |= DRM_BO_FLAG_WRITE;

   return drmBOMap(bufmgr_ttm->fd, &ttm_buf->drm_bo, flags, 0, &buf->virtual);
}

static int
dri_ttm_bo_unmap(dri_bo *buf)
{
   dri_bufmgr_ttm *bufmgr_ttm;
   dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;

   if (buf == NULL)
      return 0;

   bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;

   buf->virtual = NULL;

   return drmBOUnmap(bufmgr_ttm->fd, &ttm_buf->drm_bo);
}

static int
dri_ttm_validate(dri_bo *buf, unsigned int flags)
{
   dri_bufmgr_ttm *bufmgr_ttm;
   dri_bo_ttm *ttm_buf = (dri_bo_ttm *)buf;
   unsigned int mask;
   int err;

   /* XXX: Sanity-check whether we've already validated this one under
    * different flags.  See drmAddValidateItem().
    */

   bufmgr_ttm = (dri_bufmgr_ttm *)buf->bufmgr;

   /* Calculate the appropriate mask to pass to the DRM. There appears to be
    * be a direct relationship to flags, so it's unnecessary to have it passed
    * in as an argument.
    */
   mask = DRM_BO_MASK_MEM;
   mask |= flags & (DRM_BO_FLAG_READ | DRM_BO_FLAG_WRITE | DRM_BO_FLAG_EXE);

   err = drmBOValidate(bufmgr_ttm->fd, &ttm_buf->drm_bo, flags, mask, 0);

   if (err == 0) {
      /* XXX: add to fence list for sanity checking */
   }

   return err;
}

static dri_fence *
dri_ttm_fence_validated(dri_bufmgr *bufmgr, const char *name,
			GLboolean flushed)
{
   dri_fence_ttm *fence_ttm = malloc(sizeof(*fence_ttm));
   dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)bufmgr;
   int ret;
   unsigned int type;

   if (!fence_ttm)
      return NULL;

   if (flushed)
      type = bufmgr_ttm->fence_type_flush;
   else
      type = bufmgr_ttm->fence_type;

   fence_ttm->refcount = 1;
   fence_ttm->name = name;
   fence_ttm->type = type;
   fence_ttm->fence.bufmgr = bufmgr;
   ret = drmFenceBuffers(bufmgr_ttm->fd, type, &fence_ttm->drm_fence);
   if (ret) {
      free(fence_ttm);
      return NULL;
   }
   return &fence_ttm->fence;
}

static void
dri_ttm_fence_reference(dri_fence *fence)
{
   dri_fence_ttm *fence_ttm = (dri_fence_ttm *)fence;
   dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)fence->bufmgr;

   _glthread_LOCK_MUTEX(bufmgr_ttm->mutex);
   ++fence_ttm->refcount;
   _glthread_UNLOCK_MUTEX(bufmgr_ttm->mutex);
}

static void
dri_ttm_fence_unreference(dri_fence *fence)
{
   dri_fence_ttm *fence_ttm = (dri_fence_ttm *)fence;
   dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)fence->bufmgr;

   if (!fence)
      return;

   _glthread_LOCK_MUTEX(bufmgr_ttm->mutex);
   if (--fence_ttm->refcount == 0) {
      drmFenceDestroy(bufmgr_ttm->fd, &fence_ttm->drm_fence);
      _glthread_UNLOCK_MUTEX(bufmgr_ttm->mutex);
      free(fence);
      return;
   }
   _glthread_UNLOCK_MUTEX(bufmgr_ttm->mutex);
}

static void
dri_ttm_fence_wait(dri_fence *fence)
{
   dri_fence_ttm *fence_ttm = (dri_fence_ttm *)fence;
   dri_bufmgr_ttm *bufmgr_ttm = (dri_bufmgr_ttm *)fence->bufmgr;
   int ret;

   _glthread_LOCK_MUTEX(bufmgr_ttm->mutex);
   ret = drmFenceWait(bufmgr_ttm->fd, 0, &fence_ttm->drm_fence,
		      fence_ttm->type);
   _glthread_UNLOCK_MUTEX(bufmgr_ttm->mutex);
   if (ret != 0) {
      _mesa_printf("%s:%d: Error %d waiting for fence %s.\n",
		   __FILE__, __LINE__, fence_ttm->name);
      abort();
   }
}

/**
 * Initializes the TTM buffer manager, which uses the kernel to allocate, map,
 * and manage map buffer objections.
 *
 * \param fd File descriptor of the opened DRM device.
 * \param fence_type Driver-specific fence type used for fences with no flush.
 * \param fence_type_flush Driver-specific fence type used for fences with a
 *	  flush.
 */
dri_bufmgr *
dri_bufmgr_ttm_init(int fd, unsigned int fence_type,
		    unsigned int fence_type_flush)
{
   dri_bufmgr_ttm *bufmgr_ttm;

   bufmgr_ttm = malloc(sizeof(*bufmgr_ttm));
   bufmgr_ttm->fd = fd;
   bufmgr_ttm->fence_type = fence_type;
   bufmgr_ttm->fence_type_flush = fence_type_flush;
   _glthread_INIT_MUTEX(bufmgr_ttm->mutex);

   bufmgr_ttm->bufmgr.bo_alloc = dri_ttm_alloc;
   bufmgr_ttm->bufmgr.bo_alloc_static = dri_ttm_alloc_static;
   bufmgr_ttm->bufmgr.bo_reference = dri_ttm_bo_reference;
   bufmgr_ttm->bufmgr.bo_unreference = dri_ttm_bo_unreference;
   bufmgr_ttm->bufmgr.bo_map = dri_ttm_bo_map;
   bufmgr_ttm->bufmgr.bo_unmap = dri_ttm_bo_unmap;
   bufmgr_ttm->bufmgr.bo_validate = dri_ttm_validate;
   bufmgr_ttm->bufmgr.fence_validated = dri_ttm_fence_validated;
   bufmgr_ttm->bufmgr.fence_reference = dri_ttm_fence_reference;
   bufmgr_ttm->bufmgr.fence_unreference = dri_ttm_fence_unreference;
   bufmgr_ttm->bufmgr.fence_wait = dri_ttm_fence_wait;

   return &bufmgr_ttm->bufmgr;
}