freedreno: add synchronization between mesa and ddx
[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 struct fd_bo * bo_from_handle(struct fd_device *dev,
35                 uint32_t size, uint32_t handle)
36 {
37         unsigned i;
38         struct fd_bo *bo = calloc(1, sizeof(*bo));
39         if (!bo)
40                 return NULL;
41         bo->dev = dev;
42         bo->size = size;
43         bo->handle = handle;
44         atomic_set(&bo->refcnt, 1);
45         for (i = 0; i < ARRAY_SIZE(bo->list); i++)
46                 list_inithead(&bo->list[i]);
47         return bo;
48 }
49
50 static int set_memtype(struct fd_bo *bo, uint32_t flags)
51 {
52         struct drm_kgsl_gem_memtype req = {
53                         .handle = bo->handle,
54                         .type = flags & DRM_FREEDRENO_GEM_TYPE_MEM_MASK,
55         };
56
57         return drmCommandWrite(bo->dev->fd, DRM_KGSL_GEM_SETMEMTYPE,
58                         &req, sizeof(req));
59 }
60
61 static int bo_alloc(struct fd_bo *bo)
62 {
63         if (!bo->offset) {
64                 struct drm_kgsl_gem_alloc req = {
65                                 .handle = bo->handle,
66                 };
67                 int ret;
68
69                 /* if the buffer is already backed by pages then this
70                  * doesn't actually do anything (other than giving us
71                  * the offset)
72                  */
73                 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_ALLOC,
74                                 &req, sizeof(req));
75                 if (ret) {
76                         ERROR_MSG("alloc failed: %s", strerror(errno));
77                         return ret;
78                 }
79
80                 bo->offset = req.offset;
81         }
82         return 0;
83 }
84
85 struct fd_bo * fd_bo_new(struct fd_device *dev,
86                 uint32_t size, uint32_t flags)
87 {
88         struct drm_kgsl_gem_create req = {
89                         .size = ALIGN(size, 4096),
90         };
91         struct fd_bo *bo = NULL;
92
93         if (drmCommandWriteRead(dev->fd, DRM_KGSL_GEM_CREATE,
94                         &req, sizeof(req))) {
95                 return NULL;
96         }
97
98         bo = bo_from_handle(dev, size, req.handle);
99         if (!bo) {
100                 goto fail;
101         }
102
103         if (set_memtype(bo, flags)) {
104                 goto fail;
105         }
106
107         return bo;
108 fail:
109         if (bo)
110                 fd_bo_del(bo);
111         return NULL;
112 }
113
114 /* don't use this... it is just needed to get a bo from the
115  * framebuffer (pre-dmabuf)
116  */
117 struct fd_bo * fd_bo_from_fbdev(struct fd_pipe *pipe,
118                 int fbfd, uint32_t size)
119 {
120         struct drm_kgsl_gem_create_fd req = {
121                         .fd = fbfd,
122         };
123         struct fd_bo *bo;
124
125         if (drmCommandWriteRead(pipe->dev->fd, DRM_KGSL_GEM_CREATE_FD,
126                         &req, sizeof(req))) {
127                 return NULL;
128         }
129
130         bo = bo_from_handle(pipe->dev, size, req.handle);
131
132         /* this is fugly, but works around a bug in the kernel..
133          * priv->memdesc.size never gets set, so getbufinfo ioctl
134          * thinks the buffer hasn't be allocate and fails
135          */
136         if (bo && !fd_bo_gpuaddr(bo, 0)) {
137                 void *fbmem = mmap(NULL, size, PROT_READ | PROT_WRITE,
138                                 MAP_SHARED, fbfd, 0);
139                 struct kgsl_map_user_mem req = {
140                                 .memtype = KGSL_USER_MEM_TYPE_ADDR,
141                                 .len     = size,
142                                 .offset  = 0,
143                                 .hostptr = (unsigned long)fbmem,
144                 };
145                 int ret;
146                 ret = ioctl(pipe->fd, IOCTL_KGSL_MAP_USER_MEM, &req);
147                 if (ret) {
148                         ERROR_MSG("mapping user mem failed: %s",
149                                         strerror(errno));
150                         goto fail;
151                 }
152                 bo->gpuaddr = req.gpuaddr;
153                 bo->map = fbmem;
154         }
155
156         return bo;
157 fail:
158         if (bo)
159                 fd_bo_del(bo);
160         return NULL;
161 }
162
163 struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
164 {
165         struct drm_gem_open req = {
166                         .name = name,
167         };
168         struct fd_bo *bo;
169
170         if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
171                 return NULL;
172         }
173
174         bo = bo_from_handle(dev, req.size, req.handle);
175         if (bo)
176                 bo->name = name;
177
178         return bo;
179 }
180
181 struct fd_bo * fd_bo_ref(struct fd_bo *bo)
182 {
183         atomic_inc(&bo->refcnt);
184         return bo;
185 }
186
187 void fd_bo_del(struct fd_bo *bo)
188 {
189         if (!atomic_dec_and_test(&bo->refcnt))
190                 return;
191
192         if (bo->map)
193                 munmap(bo->map, bo->size);
194
195         if (bo->handle) {
196                 struct drm_gem_close req = {
197                                 .handle = bo->handle,
198                 };
199                 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
200         }
201
202         free(bo);
203 }
204
205 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
206 {
207         if (!bo->name) {
208                 struct drm_gem_flink req = {
209                                 .handle = bo->handle,
210                 };
211                 int ret;
212
213                 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
214                 if (ret) {
215                         return ret;
216                 }
217
218                 bo->name = req.name;
219         }
220
221         *name = bo->name;
222
223         return 0;
224 }
225
226 uint32_t fd_bo_handle(struct fd_bo *bo)
227 {
228         return bo->handle;
229 }
230
231 uint32_t fd_bo_size(struct fd_bo *bo)
232 {
233         return bo->size;
234 }
235
236 void * fd_bo_map(struct fd_bo *bo)
237 {
238         if (!bo->map) {
239                 int ret;
240
241                 ret = bo_alloc(bo);
242                 if (ret) {
243                         return NULL;
244                 }
245
246                 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
247                                 bo->dev->fd, bo->offset);
248                 if (bo->map == MAP_FAILED) {
249                         ERROR_MSG("mmap failed: %s", strerror(errno));
250                         bo->map = NULL;
251                 }
252         }
253         return bo->map;
254 }
255
256 uint32_t fd_bo_gpuaddr(struct fd_bo *bo, uint32_t offset)
257 {
258         if (!bo->gpuaddr) {
259                 struct drm_kgsl_gem_bufinfo req = {
260                                 .handle = bo->handle,
261                 };
262                 int ret;
263
264                 ret = bo_alloc(bo);
265                 if (ret) {
266                         return ret;
267                 }
268
269                 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
270                                 &req, sizeof(req));
271                 if (ret) {
272                         ERROR_MSG("get bufinfo failed: %s", strerror(errno));
273                         return 0;
274                 }
275
276                 bo->gpuaddr = req.gpuaddr[0];
277         }
278         return bo->gpuaddr + offset;
279 }
280
281 /*
282  * Super-cheezy way to synchronization between mesa and ddx..  the
283  * SET_ACTIVE ioctl gives us a way to stash a 32b # w/ a GEM bo, and
284  * GET_BUFINFO gives us a way to retrieve it.  We use this to stash
285  * the timestamp of the last ISSUEIBCMDS on the buffer.
286  *
287  * To avoid an obscene amount of syscalls, we:
288  *  1) Only set the timestamp for buffers w/ an flink name, ie.
289  *     only buffers shared across processes.  This is enough to
290  *     catch the DRI2 buffers.
291  *  2) Only set the timestamp for buffers submitted to the 3d ring
292  *     and only check the timestamps on buffers submitted to the
293  *     2d ring.  This should be enough to handle synchronizing of
294  *     presentation blit.  We could do synchronization in the other
295  *     direction too, but that would be problematic if we are using
296  *     the 3d ring from DDX, since client side wouldn't know this.
297  *
298  * The waiting on timestamp happens before flush, and setting of
299  * timestamp happens after flush.  It is transparent to the user
300  * of libdrm_freedreno as all the tracking of buffers happens via
301  * _emit_reloc()..
302  */
303
304 void fb_bo_set_timestamp(struct fd_bo *bo, uint32_t timestamp)
305 {
306         if (bo->name) {
307                 struct drm_kgsl_gem_active req = {
308                                 .handle = bo->handle,
309                                 .active = timestamp,
310                 };
311                 int ret;
312
313                 ret = drmCommandWrite(bo->dev->fd, DRM_KGSL_GEM_SET_ACTIVE,
314                                 &req, sizeof(req));
315                 if (ret) {
316                         ERROR_MSG("set active failed: %s", strerror(errno));
317                 }
318         }
319 }
320
321 uint32_t fd_bo_get_timestamp(struct fd_bo *bo)
322 {
323         uint32_t timestamp = 0;
324         if (bo->name) {
325                 struct drm_kgsl_gem_bufinfo req = {
326                                 .handle = bo->handle,
327                 };
328                 int ret;
329
330                 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
331                                 &req, sizeof(req));
332                 if (ret) {
333                         ERROR_MSG("get bufinfo failed: %s", strerror(errno));
334                         return 0;
335                 }
336
337                 timestamp = req.active;
338         }
339         return timestamp;
340 }