freedreno: add freedreno DRM
[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
169         if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
170                 return NULL;
171         }
172
173         return bo_from_handle(dev, req.size, req.handle);
174 }
175
176 struct fd_bo * fd_bo_ref(struct fd_bo *bo)
177 {
178         atomic_inc(&bo->refcnt);
179         return bo;
180 }
181
182 void fd_bo_del(struct fd_bo *bo)
183 {
184         if (!atomic_dec_and_test(&bo->refcnt))
185                 return;
186
187         if (bo->map)
188                 munmap(bo->map, bo->size);
189
190         if (bo->handle) {
191                 struct drm_gem_close req = {
192                                 .handle = bo->handle,
193                 };
194                 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
195         }
196
197         free(bo);
198 }
199
200 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
201 {
202         if (!bo->name) {
203                 struct drm_gem_flink req = {
204                                 .handle = bo->handle,
205                 };
206                 int ret;
207
208                 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
209                 if (ret) {
210                         return ret;
211                 }
212
213                 bo->name = req.name;
214         }
215
216         *name = bo->name;
217
218         return 0;
219 }
220
221 uint32_t fd_bo_handle(struct fd_bo *bo)
222 {
223         return bo->handle;
224 }
225
226 uint32_t fd_bo_size(struct fd_bo *bo)
227 {
228         return bo->size;
229 }
230
231 void * fd_bo_map(struct fd_bo *bo)
232 {
233         if (!bo->map) {
234                 int ret;
235
236                 ret = bo_alloc(bo);
237                 if (ret) {
238                         return NULL;
239                 }
240
241                 bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
242                                 bo->dev->fd, bo->offset);
243                 if (bo->map == MAP_FAILED) {
244                         ERROR_MSG("mmap failed: %s", strerror(errno));
245                         bo->map = NULL;
246                 }
247         }
248         return bo->map;
249 }
250
251 uint32_t fd_bo_gpuaddr(struct fd_bo *bo, uint32_t offset)
252 {
253         if (!bo->gpuaddr) {
254                 struct drm_kgsl_gem_bufinfo req = {
255                                 .handle = bo->handle,
256                 };
257                 int ret;
258
259                 ret = bo_alloc(bo);
260                 if (ret) {
261                         return ret;
262                 }
263
264                 ret = drmCommandWriteRead(bo->dev->fd, DRM_KGSL_GEM_GET_BUFINFO,
265                                 &req, sizeof(req));
266                 if (ret) {
267                         ERROR_MSG("get bufinfo failed: %s", strerror(errno));
268                         return 0;
269                 }
270
271                 bo->gpuaddr = req.gpuaddr[0];
272         }
273         return bo->gpuaddr + offset;
274 }