585851c70de5d6c14f63d24206966b9c77c1d22c
[platform/upstream/libdrm.git] / freedreno / kgsl / kgsl_bo.c
1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3 /*
4  * Copyright (C) 2013 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 "kgsl_priv.h"
30
31 #include <linux/fb.h>
32
33 static int set_memtype(struct fd_device *dev, uint32_t handle, uint32_t flags)
34 {
35         struct drm_kgsl_gem_memtype req = {
36                         .handle = handle,
37                         .type = flags & DRM_FREEDRENO_GEM_TYPE_MEM_MASK,
38         };
39
40         return drmCommandWrite(dev->fd, DRM_KGSL_GEM_SETMEMTYPE,
41                         &req, sizeof(req));
42 }
43
44 static int bo_alloc(struct kgsl_bo *kgsl_bo)
45 {
46         struct fd_bo *bo = &kgsl_bo->base;
47         if (!kgsl_bo->offset) {
48                 struct drm_kgsl_gem_alloc req = {
49                                 .handle = bo->handle,
50                 };
51                 int ret;
52
53                 /* if the buffer is already backed by pages then this
54                  * doesn't actually do anything (other than giving us
55                  * the offset)
56                  */
57                 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_ALLOC,
58                                 &req, sizeof(req));
59                 if (ret) {
60                         ERROR_MSG("alloc failed: %s", strerror(errno));
61                         return ret;
62                 }
63
64                 kgsl_bo->offset = req.offset;
65         }
66
67         return 0;
68 }
69
70 static int kgsl_bo_offset(struct fd_bo *bo, uint64_t *offset)
71 {
72         struct kgsl_bo *kgsl_bo = to_kgsl_bo(bo);
73         int ret = bo_alloc(kgsl_bo);
74         if (ret)
75                 return ret;
76         *offset = kgsl_bo->offset;
77         return 0;
78 }
79
80 static int kgsl_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
81 {
82         uint32_t timestamp = kgsl_bo_get_timestamp(to_kgsl_bo(bo));
83
84         if (op & DRM_FREEDRENO_PREP_NOSYNC) {
85                 uint32_t current;
86                 int ret;
87
88                 ret = kgsl_pipe_timestamp(to_kgsl_pipe(pipe), &current);
89                 if (ret)
90                         return ret;
91
92                 if (timestamp > current)
93                         return -EBUSY;
94
95                 return 0;
96         }
97
98         if (timestamp)
99                 fd_pipe_wait(pipe, timestamp);
100
101         return 0;
102 }
103
104 static void kgsl_bo_cpu_fini(struct fd_bo *bo)
105 {
106 }
107
108 static void kgsl_bo_destroy(struct fd_bo *bo)
109 {
110         struct kgsl_bo *kgsl_bo = to_kgsl_bo(bo);
111         free(kgsl_bo);
112
113 }
114
115 static struct fd_bo_funcs funcs = {
116                 .offset = kgsl_bo_offset,
117                 .cpu_prep = kgsl_bo_cpu_prep,
118                 .cpu_fini = kgsl_bo_cpu_fini,
119                 .destroy = kgsl_bo_destroy,
120 };
121
122 /* allocate a buffer handle: */
123 int kgsl_bo_new_handle(struct fd_device *dev,
124                 uint32_t size, uint32_t flags, uint32_t *handle)
125 {
126         struct drm_kgsl_gem_create req = {
127                         .size = size,
128         };
129         int ret;
130
131         ret = drmCommandWriteRead(dev->fd, DRM_KGSL_GEM_CREATE,
132                         &req, sizeof(req));
133         if (ret)
134                 return ret;
135
136         // TODO make flags match msm driver, since kgsl is legacy..
137         // translate flags in kgsl..
138
139         set_memtype(dev, req.handle, flags);
140
141         *handle = req.handle;
142
143         return 0;
144 }
145
146 /* allocate a new buffer object */
147 struct fd_bo * kgsl_bo_from_handle(struct fd_device *dev,
148                 uint32_t size, uint32_t handle)
149 {
150         struct kgsl_bo *kgsl_bo;
151         struct fd_bo *bo;
152         unsigned i;
153
154         kgsl_bo = calloc(1, sizeof(*kgsl_bo));
155         if (!kgsl_bo)
156                 return NULL;
157
158         bo = &kgsl_bo->base;
159         bo->funcs = &funcs;
160
161         for (i = 0; i < ARRAY_SIZE(kgsl_bo->list); i++)
162                 list_inithead(&kgsl_bo->list[i]);
163
164         return bo;
165 }
166
167 struct fd_bo * fd_bo_from_fbdev(struct fd_pipe *pipe,
168                 int fbfd, uint32_t size)
169 {
170         struct drm_kgsl_gem_create_fd req = {
171                         .fd = fbfd,
172         };
173         struct fd_bo *bo;
174         struct kgsl_bo *kgsl_bo;
175
176         if (!is_kgsl_pipe(pipe))
177                 return NULL;
178
179         if (drmCommandWriteRead(pipe->dev->fd, DRM_KGSL_GEM_CREATE_FD,
180                         &req, sizeof(req))) {
181                 return NULL;
182         }
183
184         bo = fd_bo_from_handle(pipe->dev, req.handle, size);
185         kgsl_bo = to_kgsl_bo(bo);
186
187         /* this is fugly, but works around a bug in the kernel..
188          * priv->memdesc.size never gets set, so getbufinfo ioctl
189          * thinks the buffer hasn't be allocate and fails
190          */
191         if (bo && !kgsl_bo_gpuaddr(kgsl_bo, 0)) {
192                 void *fbmem = mmap(NULL, size, PROT_READ | PROT_WRITE,
193                                 MAP_SHARED, fbfd, 0);
194                 struct kgsl_map_user_mem req = {
195                                 .memtype = KGSL_USER_MEM_TYPE_ADDR,
196                                 .len     = size,
197                                 .offset  = 0,
198                                 .hostptr = (unsigned long)fbmem,
199                 };
200                 int ret;
201                 ret = ioctl(to_kgsl_pipe(pipe)->fd, IOCTL_KGSL_MAP_USER_MEM, &req);
202                 if (ret) {
203                         ERROR_MSG("mapping user mem failed: %s",
204                                         strerror(errno));
205                         goto fail;
206                 }
207                 kgsl_bo->gpuaddr = req.gpuaddr;
208                 bo->map = fbmem;
209         }
210
211         return bo;
212 fail:
213         if (bo)
214                 fd_bo_del(bo);
215         return NULL;
216 }
217
218
219 uint32_t kgsl_bo_gpuaddr(struct kgsl_bo *kgsl_bo, uint32_t offset)
220 {
221         struct fd_bo *bo = &kgsl_bo->base;
222         if (!kgsl_bo->gpuaddr) {
223                 struct drm_kgsl_gem_bufinfo req = {
224                                 .handle = bo->handle,
225                 };
226                 int ret;
227
228                 ret = bo_alloc(kgsl_bo);
229                 if (ret) {
230                         return ret;
231                 }
232
233                 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
234                                 &req, sizeof(req));
235                 if (ret) {
236                         ERROR_MSG("get bufinfo failed: %s", strerror(errno));
237                         return 0;
238                 }
239
240                 kgsl_bo->gpuaddr = req.gpuaddr[0];
241         }
242         return kgsl_bo->gpuaddr + offset;
243 }
244
245 /*
246  * Super-cheezy way to synchronization between mesa and ddx..  the
247  * SET_ACTIVE ioctl gives us a way to stash a 32b # w/ a GEM bo, and
248  * GET_BUFINFO gives us a way to retrieve it.  We use this to stash
249  * the timestamp of the last ISSUEIBCMDS on the buffer.
250  *
251  * To avoid an obscene amount of syscalls, we:
252  *  1) Only set the timestamp for buffers w/ an flink name, ie.
253  *     only buffers shared across processes.  This is enough to
254  *     catch the DRI2 buffers.
255  *  2) Only set the timestamp for buffers submitted to the 3d ring
256  *     and only check the timestamps on buffers submitted to the
257  *     2d ring.  This should be enough to handle synchronizing of
258  *     presentation blit.  We could do synchronization in the other
259  *     direction too, but that would be problematic if we are using
260  *     the 3d ring from DDX, since client side wouldn't know this.
261  *
262  * The waiting on timestamp happens before flush, and setting of
263  * timestamp happens after flush.  It is transparent to the user
264  * of libdrm_freedreno as all the tracking of buffers happens via
265  * _emit_reloc()..
266  */
267
268 void kgsl_bo_set_timestamp(struct kgsl_bo *kgsl_bo, uint32_t timestamp)
269 {
270         struct fd_bo *bo = &kgsl_bo->base;
271         if (bo->name) {
272                 struct drm_kgsl_gem_active req = {
273                                 .handle = bo->handle,
274                                 .active = timestamp,
275                 };
276                 int ret;
277
278                 ret = drmCommandWrite(bo->dev->fd, DRM_KGSL_GEM_SET_ACTIVE,
279                                 &req, sizeof(req));
280                 if (ret) {
281                         ERROR_MSG("set active failed: %s", strerror(errno));
282                 }
283         }
284 }
285
286 uint32_t kgsl_bo_get_timestamp(struct kgsl_bo *kgsl_bo)
287 {
288         struct fd_bo *bo = &kgsl_bo->base;
289         uint32_t timestamp = 0;
290         if (bo->name) {
291                 struct drm_kgsl_gem_bufinfo req = {
292                                 .handle = bo->handle,
293                 };
294                 int ret;
295
296                 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
297                                 &req, sizeof(req));
298                 if (ret) {
299                         ERROR_MSG("get bufinfo failed: %s", strerror(errno));
300                         return 0;
301                 }
302
303                 timestamp = req.active;
304         }
305         return timestamp;
306 }