freedreno: Initialize the bo's iova at creation time.
[mesa.git] / src / freedreno / drm / freedreno_bo.c
1 /*
2 * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "os/os_mman.h"
28
29 #include "freedreno_drmif.h"
30 #include "freedreno_priv.h"
31
32 pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
33 void bo_del(struct fd_bo *bo);
34
35 /* set buffer name, and add to table, call w/ table_lock held: */
36 static void set_name(struct fd_bo *bo, uint32_t name)
37 {
38 bo->name = name;
39 /* add ourself into the handle table: */
40 _mesa_hash_table_insert(bo->dev->name_table, &bo->name, bo);
41 }
42
43 /* lookup a buffer, call w/ table_lock held: */
44 static struct fd_bo * lookup_bo(struct hash_table *tbl, uint32_t key)
45 {
46 struct fd_bo *bo = NULL;
47 struct hash_entry *entry = _mesa_hash_table_search(tbl, &key);
48 if (entry) {
49 /* found, incr refcnt and return: */
50 bo = fd_bo_ref(entry->data);
51
52 /* don't break the bucket if this bo was found in one */
53 list_delinit(&bo->list);
54 }
55 return bo;
56 }
57
58 /* allocate a new buffer object, call w/ table_lock held */
59 static struct fd_bo * bo_from_handle(struct fd_device *dev,
60 uint32_t size, uint32_t handle)
61 {
62 struct fd_bo *bo;
63
64 bo = dev->funcs->bo_from_handle(dev, size, handle);
65 if (!bo) {
66 struct drm_gem_close req = {
67 .handle = handle,
68 };
69 drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
70 return NULL;
71 }
72 bo->dev = fd_device_ref(dev);
73 bo->size = size;
74 bo->handle = handle;
75 bo->iova = bo->funcs->iova(bo);
76
77 p_atomic_set(&bo->refcnt, 1);
78 list_inithead(&bo->list);
79 /* add ourself into the handle table: */
80 _mesa_hash_table_insert(dev->handle_table, &bo->handle, bo);
81 return bo;
82 }
83
84 static struct fd_bo *
85 bo_new(struct fd_device *dev, uint32_t size, uint32_t flags,
86 struct fd_bo_cache *cache)
87 {
88 struct fd_bo *bo = NULL;
89 uint32_t handle;
90 int ret;
91
92 bo = fd_bo_cache_alloc(cache, &size, flags);
93 if (bo)
94 return bo;
95
96 ret = dev->funcs->bo_new_handle(dev, size, flags, &handle);
97 if (ret)
98 return NULL;
99
100 pthread_mutex_lock(&table_lock);
101 bo = bo_from_handle(dev, size, handle);
102 pthread_mutex_unlock(&table_lock);
103
104 VG_BO_ALLOC(bo);
105
106 return bo;
107 }
108
109 struct fd_bo *
110 _fd_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags)
111 {
112 struct fd_bo *bo = bo_new(dev, size, flags, &dev->bo_cache);
113 if (bo)
114 bo->bo_reuse = BO_CACHE;
115 return bo;
116 }
117
118 void
119 _fd_bo_set_name(struct fd_bo *bo, const char *fmt, va_list ap)
120 {
121 bo->funcs->set_name(bo, fmt, ap);
122 }
123
124 /* internal function to allocate bo's that use the ringbuffer cache
125 * instead of the normal bo_cache. The purpose is, because cmdstream
126 * bo's get vmap'd on the kernel side, and that is expensive, we want
127 * to re-use cmdstream bo's for cmdstream and not unrelated purposes.
128 */
129 struct fd_bo *
130 fd_bo_new_ring(struct fd_device *dev, uint32_t size)
131 {
132 uint32_t flags = DRM_FREEDRENO_GEM_GPUREADONLY;
133 struct fd_bo *bo = bo_new(dev, size, flags, &dev->ring_cache);
134 if (bo)
135 bo->bo_reuse = RING_CACHE;
136 fd_bo_set_name(bo, "cmdstream");
137 return bo;
138 }
139
140 struct fd_bo *
141 fd_bo_from_handle(struct fd_device *dev, uint32_t handle, uint32_t size)
142 {
143 struct fd_bo *bo = NULL;
144
145 pthread_mutex_lock(&table_lock);
146
147 bo = lookup_bo(dev->handle_table, handle);
148 if (bo)
149 goto out_unlock;
150
151 bo = bo_from_handle(dev, size, handle);
152
153 VG_BO_ALLOC(bo);
154
155 out_unlock:
156 pthread_mutex_unlock(&table_lock);
157
158 return bo;
159 }
160
161 struct fd_bo *
162 fd_bo_from_dmabuf(struct fd_device *dev, int fd)
163 {
164 int ret, size;
165 uint32_t handle;
166 struct fd_bo *bo;
167
168 pthread_mutex_lock(&table_lock);
169 ret = drmPrimeFDToHandle(dev->fd, fd, &handle);
170 if (ret) {
171 pthread_mutex_unlock(&table_lock);
172 return NULL;
173 }
174
175 bo = lookup_bo(dev->handle_table, handle);
176 if (bo)
177 goto out_unlock;
178
179 /* lseek() to get bo size */
180 size = lseek(fd, 0, SEEK_END);
181 lseek(fd, 0, SEEK_CUR);
182
183 bo = bo_from_handle(dev, size, handle);
184
185 VG_BO_ALLOC(bo);
186
187 out_unlock:
188 pthread_mutex_unlock(&table_lock);
189
190 return bo;
191 }
192
193 struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
194 {
195 struct drm_gem_open req = {
196 .name = name,
197 };
198 struct fd_bo *bo;
199
200 pthread_mutex_lock(&table_lock);
201
202 /* check name table first, to see if bo is already open: */
203 bo = lookup_bo(dev->name_table, name);
204 if (bo)
205 goto out_unlock;
206
207 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
208 ERROR_MSG("gem-open failed: %s", strerror(errno));
209 goto out_unlock;
210 }
211
212 bo = lookup_bo(dev->handle_table, req.handle);
213 if (bo)
214 goto out_unlock;
215
216 bo = bo_from_handle(dev, req.size, req.handle);
217 if (bo) {
218 set_name(bo, name);
219 VG_BO_ALLOC(bo);
220 }
221
222 out_unlock:
223 pthread_mutex_unlock(&table_lock);
224
225 return bo;
226 }
227
228 uint64_t fd_bo_get_iova(struct fd_bo *bo)
229 {
230 return bo->iova;
231 }
232
233 struct fd_bo * fd_bo_ref(struct fd_bo *bo)
234 {
235 p_atomic_inc(&bo->refcnt);
236 return bo;
237 }
238
239 void fd_bo_del(struct fd_bo *bo)
240 {
241 struct fd_device *dev = bo->dev;
242
243 if (!atomic_dec_and_test(&bo->refcnt))
244 return;
245
246 pthread_mutex_lock(&table_lock);
247
248 if ((bo->bo_reuse == BO_CACHE) && (fd_bo_cache_free(&dev->bo_cache, bo) == 0))
249 goto out;
250 if ((bo->bo_reuse == RING_CACHE) && (fd_bo_cache_free(&dev->ring_cache, bo) == 0))
251 goto out;
252
253 bo_del(bo);
254 fd_device_del_locked(dev);
255 out:
256 pthread_mutex_unlock(&table_lock);
257 }
258
259 /* Called under table_lock */
260 void bo_del(struct fd_bo *bo)
261 {
262 VG_BO_FREE(bo);
263
264 if (bo->map)
265 os_munmap(bo->map, bo->size);
266
267 /* TODO probably bo's in bucket list get removed from
268 * handle table??
269 */
270
271 if (bo->handle) {
272 struct drm_gem_close req = {
273 .handle = bo->handle,
274 };
275 _mesa_hash_table_remove_key(bo->dev->handle_table, &bo->handle);
276 if (bo->name)
277 _mesa_hash_table_remove_key(bo->dev->name_table, &bo->name);
278 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
279 }
280
281 bo->funcs->destroy(bo);
282 }
283
284 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
285 {
286 if (!bo->name) {
287 struct drm_gem_flink req = {
288 .handle = bo->handle,
289 };
290 int ret;
291
292 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
293 if (ret) {
294 return ret;
295 }
296
297 pthread_mutex_lock(&table_lock);
298 set_name(bo, req.name);
299 pthread_mutex_unlock(&table_lock);
300 bo->bo_reuse = NO_CACHE;
301 }
302
303 *name = bo->name;
304
305 return 0;
306 }
307
308 uint32_t fd_bo_handle(struct fd_bo *bo)
309 {
310 return bo->handle;
311 }
312
313 int fd_bo_dmabuf(struct fd_bo *bo)
314 {
315 int ret, prime_fd;
316
317 ret = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC,
318 &prime_fd);
319 if (ret) {
320 ERROR_MSG("failed to get dmabuf fd: %d", ret);
321 return ret;
322 }
323
324 bo->bo_reuse = NO_CACHE;
325
326 return prime_fd;
327 }
328
329 uint32_t fd_bo_size(struct fd_bo *bo)
330 {
331 return bo->size;
332 }
333
334 void * fd_bo_map(struct fd_bo *bo)
335 {
336 if (!bo->map) {
337 uint64_t offset;
338 int ret;
339
340 ret = bo->funcs->offset(bo, &offset);
341 if (ret) {
342 return NULL;
343 }
344
345 bo->map = os_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
346 bo->dev->fd, offset);
347 if (bo->map == MAP_FAILED) {
348 ERROR_MSG("mmap failed: %s", strerror(errno));
349 bo->map = NULL;
350 }
351 }
352 return bo->map;
353 }
354
355 /* a bit odd to take the pipe as an arg, but it's a, umm, quirk of kgsl.. */
356 int fd_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
357 {
358 return bo->funcs->cpu_prep(bo, pipe, op);
359 }
360
361 void fd_bo_cpu_fini(struct fd_bo *bo)
362 {
363 bo->funcs->cpu_fini(bo);
364 }