freedreno/drm: readonly cmdstream
[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 p_atomic_set(&bo->refcnt, 1);
76 list_inithead(&bo->list);
77 /* add ourself into the handle table: */
78 _mesa_hash_table_insert(dev->handle_table, &bo->handle, bo);
79 return bo;
80 }
81
82 static struct fd_bo *
83 bo_new(struct fd_device *dev, uint32_t size, uint32_t flags,
84 struct fd_bo_cache *cache)
85 {
86 struct fd_bo *bo = NULL;
87 uint32_t handle;
88 int ret;
89
90 bo = fd_bo_cache_alloc(cache, &size, flags);
91 if (bo)
92 return bo;
93
94 ret = dev->funcs->bo_new_handle(dev, size, flags, &handle);
95 if (ret)
96 return NULL;
97
98 pthread_mutex_lock(&table_lock);
99 bo = bo_from_handle(dev, size, handle);
100 pthread_mutex_unlock(&table_lock);
101
102 VG_BO_ALLOC(bo);
103
104 return bo;
105 }
106
107 struct fd_bo *
108 _fd_bo_new(struct fd_device *dev, uint32_t size, uint32_t flags)
109 {
110 struct fd_bo *bo = bo_new(dev, size, flags, &dev->bo_cache);
111 if (bo)
112 bo->bo_reuse = BO_CACHE;
113 return bo;
114 }
115
116 void
117 _fd_bo_set_name(struct fd_bo *bo, const char *fmt, va_list ap)
118 {
119 bo->funcs->set_name(bo, fmt, ap);
120 }
121
122 /* internal function to allocate bo's that use the ringbuffer cache
123 * instead of the normal bo_cache. The purpose is, because cmdstream
124 * bo's get vmap'd on the kernel side, and that is expensive, we want
125 * to re-use cmdstream bo's for cmdstream and not unrelated purposes.
126 */
127 struct fd_bo *
128 fd_bo_new_ring(struct fd_device *dev, uint32_t size)
129 {
130 uint32_t flags = DRM_FREEDRENO_GEM_GPUREADONLY;
131 struct fd_bo *bo = bo_new(dev, size, flags, &dev->ring_cache);
132 if (bo)
133 bo->bo_reuse = RING_CACHE;
134 fd_bo_set_name(bo, "cmdstream");
135 return bo;
136 }
137
138 struct fd_bo *
139 fd_bo_from_handle(struct fd_device *dev, uint32_t handle, uint32_t size)
140 {
141 struct fd_bo *bo = NULL;
142
143 pthread_mutex_lock(&table_lock);
144
145 bo = lookup_bo(dev->handle_table, handle);
146 if (bo)
147 goto out_unlock;
148
149 bo = bo_from_handle(dev, size, handle);
150
151 VG_BO_ALLOC(bo);
152
153 out_unlock:
154 pthread_mutex_unlock(&table_lock);
155
156 return bo;
157 }
158
159 struct fd_bo *
160 fd_bo_from_dmabuf(struct fd_device *dev, int fd)
161 {
162 int ret, size;
163 uint32_t handle;
164 struct fd_bo *bo;
165
166 pthread_mutex_lock(&table_lock);
167 ret = drmPrimeFDToHandle(dev->fd, fd, &handle);
168 if (ret) {
169 pthread_mutex_unlock(&table_lock);
170 return NULL;
171 }
172
173 bo = lookup_bo(dev->handle_table, handle);
174 if (bo)
175 goto out_unlock;
176
177 /* lseek() to get bo size */
178 size = lseek(fd, 0, SEEK_END);
179 lseek(fd, 0, SEEK_CUR);
180
181 bo = bo_from_handle(dev, size, handle);
182
183 VG_BO_ALLOC(bo);
184
185 out_unlock:
186 pthread_mutex_unlock(&table_lock);
187
188 return bo;
189 }
190
191 struct fd_bo * fd_bo_from_name(struct fd_device *dev, uint32_t name)
192 {
193 struct drm_gem_open req = {
194 .name = name,
195 };
196 struct fd_bo *bo;
197
198 pthread_mutex_lock(&table_lock);
199
200 /* check name table first, to see if bo is already open: */
201 bo = lookup_bo(dev->name_table, name);
202 if (bo)
203 goto out_unlock;
204
205 if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
206 ERROR_MSG("gem-open failed: %s", strerror(errno));
207 goto out_unlock;
208 }
209
210 bo = lookup_bo(dev->handle_table, req.handle);
211 if (bo)
212 goto out_unlock;
213
214 bo = bo_from_handle(dev, req.size, req.handle);
215 if (bo) {
216 set_name(bo, name);
217 VG_BO_ALLOC(bo);
218 }
219
220 out_unlock:
221 pthread_mutex_unlock(&table_lock);
222
223 return bo;
224 }
225
226 uint64_t fd_bo_get_iova(struct fd_bo *bo)
227 {
228 if (!bo->iova)
229 bo->iova = bo->funcs->iova(bo);
230 return bo->iova;
231 }
232
233 void fd_bo_put_iova(struct fd_bo *bo)
234 {
235 /* currently a no-op */
236 }
237
238 struct fd_bo * fd_bo_ref(struct fd_bo *bo)
239 {
240 p_atomic_inc(&bo->refcnt);
241 return bo;
242 }
243
244 void fd_bo_del(struct fd_bo *bo)
245 {
246 struct fd_device *dev = bo->dev;
247
248 if (!atomic_dec_and_test(&bo->refcnt))
249 return;
250
251 pthread_mutex_lock(&table_lock);
252
253 if ((bo->bo_reuse == BO_CACHE) && (fd_bo_cache_free(&dev->bo_cache, bo) == 0))
254 goto out;
255 if ((bo->bo_reuse == RING_CACHE) && (fd_bo_cache_free(&dev->ring_cache, bo) == 0))
256 goto out;
257
258 bo_del(bo);
259 fd_device_del_locked(dev);
260 out:
261 pthread_mutex_unlock(&table_lock);
262 }
263
264 /* Called under table_lock */
265 void bo_del(struct fd_bo *bo)
266 {
267 VG_BO_FREE(bo);
268
269 if (bo->map)
270 os_munmap(bo->map, bo->size);
271
272 /* TODO probably bo's in bucket list get removed from
273 * handle table??
274 */
275
276 if (bo->handle) {
277 struct drm_gem_close req = {
278 .handle = bo->handle,
279 };
280 _mesa_hash_table_remove_key(bo->dev->handle_table, &bo->handle);
281 if (bo->name)
282 _mesa_hash_table_remove_key(bo->dev->name_table, &bo->name);
283 drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
284 }
285
286 bo->funcs->destroy(bo);
287 }
288
289 int fd_bo_get_name(struct fd_bo *bo, uint32_t *name)
290 {
291 if (!bo->name) {
292 struct drm_gem_flink req = {
293 .handle = bo->handle,
294 };
295 int ret;
296
297 ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
298 if (ret) {
299 return ret;
300 }
301
302 pthread_mutex_lock(&table_lock);
303 set_name(bo, req.name);
304 pthread_mutex_unlock(&table_lock);
305 bo->bo_reuse = NO_CACHE;
306 }
307
308 *name = bo->name;
309
310 return 0;
311 }
312
313 uint32_t fd_bo_handle(struct fd_bo *bo)
314 {
315 return bo->handle;
316 }
317
318 int fd_bo_dmabuf(struct fd_bo *bo)
319 {
320 int ret, prime_fd;
321
322 ret = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC,
323 &prime_fd);
324 if (ret) {
325 ERROR_MSG("failed to get dmabuf fd: %d", ret);
326 return ret;
327 }
328
329 bo->bo_reuse = NO_CACHE;
330
331 return prime_fd;
332 }
333
334 uint32_t fd_bo_size(struct fd_bo *bo)
335 {
336 return bo->size;
337 }
338
339 void * fd_bo_map(struct fd_bo *bo)
340 {
341 if (!bo->map) {
342 uint64_t offset;
343 int ret;
344
345 ret = bo->funcs->offset(bo, &offset);
346 if (ret) {
347 return NULL;
348 }
349
350 bo->map = os_mmap(0, bo->size, PROT_READ | PROT_WRITE, MAP_SHARED,
351 bo->dev->fd, offset);
352 if (bo->map == MAP_FAILED) {
353 ERROR_MSG("mmap failed: %s", strerror(errno));
354 bo->map = NULL;
355 }
356 }
357 return bo->map;
358 }
359
360 /* a bit odd to take the pipe as an arg, but it's a, umm, quirk of kgsl.. */
361 int fd_bo_cpu_prep(struct fd_bo *bo, struct fd_pipe *pipe, uint32_t op)
362 {
363 return bo->funcs->cpu_prep(bo, pipe, op);
364 }
365
366 void fd_bo_cpu_fini(struct fd_bo *bo)
367 {
368 bo->funcs->cpu_fini(bo);
369 }