freedreno: add handle and name tracking
[platform/upstream/libdrm.git] / freedreno / freedreno_bo.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4  * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  * Authors:
26  *    Rob Clark <robclark@freedesktop.org>
27  */
28
29 #include "freedreno_drmif.h"
30 #include "freedreno_priv.h"
31
32 #include <linux/fb.h>
33
34 static pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
35
36 /* set buffer name, and add to table, call w/ table_lock held: */
37 static void set_name(struct fd_bo *bo, uint32_t name)
38 {
39         bo->name = name;
40         /* add ourself into the handle table: */
41         drmHashInsert(bo->dev->name_table, name, bo);
42 }
43
44 /* lookup a buffer, call w/ table_lock held: */
45 static struct fd_bo * lookup_bo(void *tbl, uint32_t key)
46 {
47         struct fd_bo *bo = NULL;
48         if (!drmHashLookup(tbl, key, (void **)&bo)) {
49                 /* found, incr refcnt and return: */
50                 bo = fd_bo_ref(bo);
51         }
52         return bo;
53 }
54
55 /* allocate a new buffer object, call w/ table_lock held */
56 static struct fd_bo * bo_from_handle(struct fd_device *dev,
57                 uint32_t size, uint32_t handle)
58 {
59         unsigned i;
60         struct fd_bo *bo = calloc(1, sizeof(*bo));
61         if (!bo) {
62                 struct drm_gem_close req = {
63                                 .handle = handle,
64                 };
65                 drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
66                 return NULL;
67         }
68         bo->dev = fd_device_ref(dev);
69         bo->size = size;
70         bo->handle = handle;
71         atomic_set(&bo->refcnt, 1);
72         /* add ourself into the handle table: */
73         drmHashInsert(dev->handle_table, handle, bo);
74         for (i = 0; i < ARRAY_SIZE(bo->list); i++)
75                 list_inithead(&bo->list[i]);
76         return bo;
77 }
78
79 static int set_memtype(struct fd_bo *bo, uint32_t flags)
80 {
81         struct drm_kgsl_gem_memtype req = {
82                         .handle = bo->handle,
83                         .type = flags & DRM_FREEDRENO_GEM_TYPE_MEM_MASK,
84         };
85
86         return drmCommandWrite(bo->dev->fd, DRM_KGSL_GEM_SETMEMTYPE,
87                         &req, sizeof(req));
88 }
89
90 static int bo_alloc(struct fd_bo *bo)
91 {
92         if (!bo->offset) {
93                 struct drm_kgsl_gem_alloc req = {
94                                 .handle = bo->handle,
95                 };
96                 int ret;
97
98                 /* if the buffer is already backed by pages then this
99                  * doesn't actually do anything (other than giving us
100                  * the offset)
101                  */
102                 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_ALLOC,
103                                 &req, sizeof(req));
104                 if (ret) {
105                         ERROR_MSG("alloc failed: %s", strerror(errno));
106                         return ret;
107                 }
108
109                 bo->offset = req.offset;
110         }
111         return 0;
112 }
113
114 struct fd_bo * fd_bo_new(struct fd_device *dev,
115                 uint32_t size, uint32_t flags)
116 {
117         struct drm_kgsl_gem_create req = {
118                         .size = ALIGN(size, 4096),
119         };
120         struct fd_bo *bo = NULL;
121
122         if (drmCommandWriteRead(dev->fd, DRM_KGSL_GEM_CREATE,
123                         &req, sizeof(req))) {
124                 return NULL;
125         }
126
127         pthread_mutex_lock(&table_lock);
128         bo = bo_from_handle(dev, size, req.handle);
129         pthread_mutex_unlock(&table_lock);
130         if (!bo) {
131                 goto fail;
132         }
133
134         if (set_memtype(bo, flags)) {
135                 goto fail;
136         }
137
138         return bo;
139 fail:
140         if (bo)
141                 fd_bo_del(bo);
142         return NULL;
143 }
144
145 /* don't use this... it is just needed to get a bo from the
146  * framebuffer (pre-dmabuf)
147  */
148 struct fd_bo * fd_bo_from_fbdev(struct fd_pipe *pipe,
149                 int fbfd, uint32_t size)
150 {
151         struct drm_kgsl_gem_create_fd req = {
152                         .fd = fbfd,
153         };
154         struct fd_bo *bo;
155
156         if (drmCommandWriteRead(pipe->dev->fd, DRM_KGSL_GEM_CREATE_FD,
157                         &req, sizeof(req))) {
158                 return NULL;
159         }
160
161         pthread_mutex_lock(&table_lock);
162         bo = bo_from_handle(pipe->dev, size, req.handle);
163
164         /* this is fugly, but works around a bug in the kernel..
165          * priv->memdesc.size never gets set, so getbufinfo ioctl
166          * thinks the buffer hasn't be allocate and fails
167          */
168         if (bo && !fd_bo_gpuaddr(bo, 0)) {
169                 void *fbmem = mmap(NULL, size, PROT_READ | PROT_WRITE,
170                                 MAP_SHARED, fbfd, 0);
171                 struct kgsl_map_user_mem req = {
172                                 .memtype = KGSL_USER_MEM_TYPE_ADDR,
173                                 .len     = size,
174                                 .offset  = 0,
175                                 .hostptr = (unsigned long)fbmem,
176                 };
177                 int ret;
178                 ret = ioctl(pipe->fd, IOCTL_KGSL_MAP_USER_MEM, &req);
179                 if (ret) {
180                         ERROR_MSG("mapping user mem failed: %s",
181                                         strerror(errno));
182                         goto fail;
183                 }
184                 bo->gpuaddr = req.gpuaddr;
185                 bo->map = fbmem;
186         }
187         pthread_mutex_unlock(&table_lock);
188
189         return bo;
190 fail:
191         pthread_mutex_unlock(&table_lock);
192         if (bo)
193                 fd_bo_del(bo);
194         return NULL;
195 }
196
197 struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
198 {
199         struct drm_gem_open req = {
200                         .name = name,
201         };
202         struct fd_bo *bo;
203
204         pthread_mutex_lock(&table_lock);
205
206         /* check name table first, to see if bo is already open: */
207         bo = lookup_bo(dev->name_table, name);
208         if (bo)
209                 goto out_unlock;
210
211         if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
212                 ERROR_MSG("gem-open failed: %s", strerror(errno));
213                 goto out_unlock;
214         }
215
216         bo = lookup_bo(dev->handle_table, req.handle);
217         if (bo)
218                 goto out_unlock;
219
220         bo = bo_from_handle(dev, req.size, req.handle);
221         if (bo)
222                 set_name(bo, name);
223
224 out_unlock:
225         pthread_mutex_unlock(&table_lock);
226
227         return bo;
228 }
229
230 struct fd_bo * fd_bo_ref(struct fd_bo *bo)
231 {
232         atomic_inc(&bo->refcnt);
233         return bo;
234 }
235
236 void fd_bo_del(struct fd_bo *bo)
237 {
238         if (!atomic_dec_and_test(&bo->refcnt))
239                 return;
240
241         if (bo->map)
242                 munmap(bo->map, bo->size);
243
244         if (bo->handle) {
245                 struct drm_gem_close req = {
246                                 .handle = bo->handle,
247                 };
248                 pthread_mutex_lock(&table_lock);
249                 drmHashDelete(bo->dev->handle_table, bo->handle);
250                 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
251                 pthread_mutex_unlock(&table_lock);
252         }
253
254         fd_device_del(bo->dev);
255         free(bo);
256 }
257
258 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
259 {
260         if (!bo->name) {
261                 struct drm_gem_flink req = {
262                                 .handle = bo->handle,
263                 };
264                 int ret;
265
266                 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
267                 if (ret) {
268                         return ret;
269                 }
270
271                 pthread_mutex_lock(&table_lock);
272                 set_name(bo, req.name);
273                 pthread_mutex_unlock(&table_lock);
274         }
275
276         *name = bo->name;
277
278         return 0;
279 }
280
281 uint32_t fd_bo_handle(struct fd_bo *bo)
282 {
283         return bo->handle;
284 }
285
286 uint32_t fd_bo_size(struct fd_bo *bo)
287 {
288         return bo->size;
289 }
290
291 void * fd_bo_map(struct fd_bo *bo)
292 {
293         if (!bo->map) {
294                 int ret;
295
296                 ret = bo_alloc(bo);
297                 if (ret) {
298                         return NULL;
299                 }
300
301                 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
302                                 bo->dev->fd, bo->offset);
303                 if (bo->map == MAP_FAILED) {
304                         ERROR_MSG("mmap failed: %s", strerror(errno));
305                         bo->map = NULL;
306                 }
307         }
308         return bo->map;
309 }
310
311 uint32_t fd_bo_gpuaddr(struct fd_bo *bo, uint32_t offset)
312 {
313         if (!bo->gpuaddr) {
314                 struct drm_kgsl_gem_bufinfo req = {
315                                 .handle = bo->handle,
316                 };
317                 int ret;
318
319                 ret = bo_alloc(bo);
320                 if (ret) {
321                         return ret;
322                 }
323
324                 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
325                                 &req, sizeof(req));
326                 if (ret) {
327                         ERROR_MSG("get bufinfo failed: %s", strerror(errno));
328                         return 0;
329                 }
330
331                 bo->gpuaddr = req.gpuaddr[0];
332         }
333         return bo->gpuaddr + offset;
334 }
335
336 /*
337  * Super-cheezy way to synchronization between mesa and ddx..  the
338  * SET_ACTIVE ioctl gives us a way to stash a 32b # w/ a GEM bo, and
339  * GET_BUFINFO gives us a way to retrieve it.  We use this to stash
340  * the timestamp of the last ISSUEIBCMDS on the buffer.
341  *
342  * To avoid an obscene amount of syscalls, we:
343  *  1) Only set the timestamp for buffers w/ an flink name, ie.
344  *     only buffers shared across processes.  This is enough to
345  *     catch the DRI2 buffers.
346  *  2) Only set the timestamp for buffers submitted to the 3d ring
347  *     and only check the timestamps on buffers submitted to the
348  *     2d ring.  This should be enough to handle synchronizing of
349  *     presentation blit.  We could do synchronization in the other
350  *     direction too, but that would be problematic if we are using
351  *     the 3d ring from DDX, since client side wouldn't know this.
352  *
353  * The waiting on timestamp happens before flush, and setting of
354  * timestamp happens after flush.  It is transparent to the user
355  * of libdrm_freedreno as all the tracking of buffers happens via
356  * _emit_reloc()..
357  */
358
359 void fb_bo_set_timestamp(struct fd_bo *bo, uint32_t timestamp)
360 {
361         if (bo->name) {
362                 struct drm_kgsl_gem_active req = {
363                                 .handle = bo->handle,
364                                 .active = timestamp,
365                 };
366                 int ret;
367
368                 ret = drmCommandWrite(bo->dev->fd, DRM_KGSL_GEM_SET_ACTIVE,
369                                 &req, sizeof(req));
370                 if (ret) {
371                         ERROR_MSG("set active failed: %s", strerror(errno));
372                 }
373         }
374 }
375
376 uint32_t fd_bo_get_timestamp(struct fd_bo *bo)
377 {
378         uint32_t timestamp = 0;
379         if (bo->name) {
380                 struct drm_kgsl_gem_bufinfo req = {
381                                 .handle = bo->handle,
382                 };
383                 int ret;
384
385                 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
386                                 &req, sizeof(req));
387                 if (ret) {
388                         ERROR_MSG("get bufinfo failed: %s", strerror(errno));
389                         return 0;
390                 }
391
392                 timestamp = req.active;
393         }
394         return timestamp;
395 }