1 /*
2  * Copyright (C) 2014 Etnaviv Project
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  *    Christian Gmeiner <christian.gmeiner@gmail.com>
25  */
26 
27 #include "os/os_mman.h"
28 #include "util/hash_table.h"
29 
30 #include "etnaviv_priv.h"
31 #include "etnaviv_drmif.h"
32 
33 simple_mtx_t etna_drm_table_lock = _SIMPLE_MTX_INITIALIZER_NP;
34 
35 /* set buffer name, and add to table, call w/ etna_drm_table_lock held: */
set_name(struct etna_bo * bo,uint32_t name)36 static void set_name(struct etna_bo *bo, uint32_t name)
37 {
38 	simple_mtx_assert_locked(&etna_drm_table_lock);
39 
40 	bo->name = name;
41 	/* add ourself into the name table: */
42 	_mesa_hash_table_insert(bo->dev->name_table, &bo->name, bo);
43 }
44 
etna_bo_is_idle(struct etna_bo * bo)45 int etna_bo_is_idle(struct etna_bo *bo)
46 {
47 	return etna_bo_cpu_prep(bo,
48 			DRM_ETNA_PREP_READ |
49 			DRM_ETNA_PREP_WRITE |
50 			DRM_ETNA_PREP_NOSYNC) == 0;
51 }
52 
53 /* Called under etna_drm_table_lock */
_etna_bo_free(struct etna_bo * bo)54 static void _etna_bo_free(struct etna_bo *bo)
55 {
56 	DEBUG_BO("Del bo:", bo);
57 	VG_BO_FREE(bo);
58 
59 	simple_mtx_assert_locked(&etna_drm_table_lock);
60 
61 	if (bo->va)
62 		util_vma_heap_free(&bo->dev->address_space, bo->va, bo->size);
63 
64 	if (bo->map)
65 		os_munmap(bo->map, bo->size);
66 
67 	if (bo->handle) {
68 		struct drm_gem_close req = {
69 			.handle = bo->handle,
70 		};
71 
72 		if (bo->name)
73 			_mesa_hash_table_remove_key(bo->dev->name_table, &bo->name);
74 
75 		_mesa_hash_table_remove_key(bo->dev->handle_table, &bo->handle);
76 		drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
77 	}
78 
79 	free(bo);
80 }
81 
etna_bo_kill_zombies(struct etna_device * dev)82 void etna_bo_kill_zombies(struct etna_device *dev)
83 {
84 	simple_mtx_assert_locked(&etna_drm_table_lock);
85 
86 	list_for_each_entry_safe(struct etna_bo, bo, &dev->zombie_list, list) {
87 		VG_BO_OBTAIN(bo);
88 		list_del(&bo->list);
89 		_etna_bo_free(bo);
90 	}
91 }
92 
93 
etna_bo_cleanup_zombies(struct etna_device * dev)94 static void etna_bo_cleanup_zombies(struct etna_device *dev)
95 {
96 	simple_mtx_assert_locked(&etna_drm_table_lock);
97 
98 	list_for_each_entry_safe(struct etna_bo, bo, &dev->zombie_list, list) {
99 		/* Stop once we reach a busy BO - all others past this point were
100 		 * freed more recently so are likely also busy.
101 		 */
102 		if (!etna_bo_is_idle(bo))
103 			break;
104 
105 		VG_BO_OBTAIN(bo);
106 		list_del(&bo->list);
107 		_etna_bo_free(bo);
108 	}
109 }
110 
etna_bo_free(struct etna_bo * bo)111 void etna_bo_free(struct etna_bo *bo) {
112 	struct etna_device *dev = bo->dev;
113 
114 	/* If the BO has a userspace managed address we don't free it immediately,
115 	 * but keep it on a deferred destroy list until all submits with the buffer
116 	 * have finished, at which point we can reuse the VMA space.
117 	 */
118 	if (dev->use_softpin) {
119 		etna_bo_cleanup_zombies(dev);
120 		VG_BO_RELEASE(bo);
121 		list_addtail(&bo->list, &dev->zombie_list);
122 	} else {
123 		_etna_bo_free(bo);
124 	}
125 }
126 
127 /* lookup a buffer from it's handle, call w/ etna_drm_table_lock held: */
lookup_bo(void * tbl,uint32_t handle)128 static struct etna_bo *lookup_bo(void *tbl, uint32_t handle)
129 {
130 	struct etna_bo *bo = NULL;
131 	struct hash_entry *entry;
132 
133 	simple_mtx_assert_locked(&etna_drm_table_lock);
134 
135 	entry = _mesa_hash_table_search(tbl, &handle);
136 
137 	if (entry) {
138 		/* found, incr refcnt and return: */
139 		bo = etna_bo_ref(entry->data);
140 
141 		/* don't break the bucket if this bo was found in one */
142 		if (list_is_linked(&bo->list)) {
143 			VG_BO_OBTAIN(bo);
144 			etna_device_ref(bo->dev);
145 			list_delinit(&bo->list);
146 		}
147 	}
148 
149 	return bo;
150 }
151 
152 /* allocate a new buffer object, call w/ etna_drm_table_lock held */
bo_from_handle(struct etna_device * dev,uint32_t size,uint32_t handle,uint32_t flags)153 static struct etna_bo *bo_from_handle(struct etna_device *dev,
154 		uint32_t size, uint32_t handle, uint32_t flags)
155 {
156 	struct etna_bo *bo = calloc(sizeof(*bo), 1);
157 
158 	simple_mtx_assert_locked(&etna_drm_table_lock);
159 
160 	if (!bo) {
161 		struct drm_gem_close req = {
162 			.handle = handle,
163 		};
164 
165 		drmIoctl(dev->fd, DRM_IOCTL_GEM_CLOSE, &req);
166 
167 		return NULL;
168 	}
169 
170 	bo->dev = etna_device_ref(dev);
171 	bo->size = size;
172 	bo->handle = handle;
173 	bo->flags = flags;
174 	p_atomic_set(&bo->refcnt, 1);
175 	list_inithead(&bo->list);
176 	/* add ourselves to the handle table: */
177 	_mesa_hash_table_insert(dev->handle_table, &bo->handle, bo);
178 
179 	if (dev->use_softpin)
180 		bo->va = util_vma_heap_alloc(&dev->address_space, bo->size, 4096);
181 
182 	return bo;
183 }
184 
185 /* allocate a new (un-tiled) buffer object */
etna_bo_new(struct etna_device * dev,uint32_t size,uint32_t flags)186 struct etna_bo *etna_bo_new(struct etna_device *dev, uint32_t size,
187 		uint32_t flags)
188 {
189 	struct etna_bo *bo;
190 	int ret;
191 	struct drm_etnaviv_gem_new req = {
192 			.flags = flags,
193 	};
194 
195 	bo = etna_bo_cache_alloc(&dev->bo_cache, &size, flags);
196 	if (bo)
197 		return bo;
198 
199 	req.size = size;
200 	ret = drmCommandWriteRead(dev->fd, DRM_ETNAVIV_GEM_NEW,
201 			&req, sizeof(req));
202 	if (ret)
203 		return NULL;
204 
205 	simple_mtx_lock(&etna_drm_table_lock);
206 	bo = bo_from_handle(dev, size, req.handle, flags);
207 	bo->reuse = 1;
208 	simple_mtx_unlock(&etna_drm_table_lock);
209 
210 	DEBUG_BO("New bo:", bo);
211 	VG_BO_ALLOC(bo);
212 
213 	return bo;
214 }
215 
etna_bo_ref(struct etna_bo * bo)216 struct etna_bo *etna_bo_ref(struct etna_bo *bo)
217 {
218 	p_atomic_inc(&bo->refcnt);
219 
220 	return bo;
221 }
222 
223 /* get buffer info */
get_buffer_info(struct etna_bo * bo)224 static int get_buffer_info(struct etna_bo *bo)
225 {
226 	int ret;
227 	struct drm_etnaviv_gem_info req = {
228 		.handle = bo->handle,
229 	};
230 
231 	ret = drmCommandWriteRead(bo->dev->fd, DRM_ETNAVIV_GEM_INFO,
232 			&req, sizeof(req));
233 	if (ret) {
234 		return ret;
235 	}
236 
237 	/* really all we need for now is mmap offset */
238 	bo->offset = req.offset;
239 
240 	return 0;
241 }
242 
243 /* import a buffer object from DRI2 name */
etna_bo_from_name(struct etna_device * dev,uint32_t name)244 struct etna_bo *etna_bo_from_name(struct etna_device *dev,
245 		uint32_t name)
246 {
247 	struct etna_bo *bo;
248 	struct drm_gem_open req = {
249 		.name = name,
250 	};
251 
252 	simple_mtx_lock(&etna_drm_table_lock);
253 
254 	/* check name table first, to see if bo is already open: */
255 	bo = lookup_bo(dev->name_table, name);
256 	if (bo)
257 		goto out_unlock;
258 
259 	if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
260 		ERROR_MSG("gem-open failed: %s", strerror(errno));
261 		goto out_unlock;
262 	}
263 
264 	bo = lookup_bo(dev->handle_table, req.handle);
265 	if (bo)
266 		goto out_unlock;
267 
268 	bo = bo_from_handle(dev, req.size, req.handle, 0);
269 	if (bo) {
270 		set_name(bo, name);
271 		DEBUG_BO("New from name:", bo);
272 		VG_BO_ALLOC(bo);
273 	}
274 
275 out_unlock:
276 	simple_mtx_unlock(&etna_drm_table_lock);
277 
278 	return bo;
279 }
280 
281 /* import a buffer from dmabuf fd, does not take ownership of the
282  * fd so caller should close() the fd when it is otherwise done
283  * with it (even if it is still using the 'struct etna_bo *')
284  */
etna_bo_from_dmabuf(struct etna_device * dev,int fd)285 struct etna_bo *etna_bo_from_dmabuf(struct etna_device *dev, int fd)
286 {
287 	struct etna_bo *bo;
288 	int ret, size;
289 	uint32_t handle;
290 
291 	/* take the lock before calling drmPrimeFDToHandle to avoid
292 	 * racing against etna_bo_del, which might invalidate the
293 	 * returned handle.
294 	 */
295 	simple_mtx_lock(&etna_drm_table_lock);
296 
297 	ret = drmPrimeFDToHandle(dev->fd, fd, &handle);
298 	if (ret) {
299 		simple_mtx_unlock(&etna_drm_table_lock);
300 		return NULL;
301 	}
302 
303 	bo = lookup_bo(dev->handle_table, handle);
304 	if (bo)
305 		goto out_unlock;
306 
307 	/* lseek() to get bo size */
308 	size = lseek(fd, 0, SEEK_END);
309 	lseek(fd, 0, SEEK_CUR);
310 
311 	bo = bo_from_handle(dev, size, handle, 0);
312 
313 	DEBUG_BO("New from dmabuf:", bo);
314 	VG_BO_ALLOC(bo);
315 
316 out_unlock:
317 	simple_mtx_unlock(&etna_drm_table_lock);
318 
319 	return bo;
320 }
321 
322 /* destroy a buffer object */
etna_bo_del(struct etna_bo * bo)323 void etna_bo_del(struct etna_bo *bo)
324 {
325 	if (!bo)
326 		return;
327 
328 	struct etna_device *dev = bo->dev;
329 
330 	simple_mtx_lock(&etna_drm_table_lock);
331 
332 	/* Must test under table lock to avoid racing with the from_dmabuf/name
333 	 * paths, which rely on the BO refcount to be stable over the lookup, so
334 	 * they can grab a reference when the BO is found in the hash.
335 	 */
336 	if (!p_atomic_dec_zero(&bo->refcnt))
337 	   goto out;
338 
339 	if (bo->reuse && (etna_bo_cache_free(&dev->bo_cache, bo) == 0))
340 		goto out;
341 
342 	etna_bo_free(bo);
343 	etna_device_del_locked(dev);
344 out:
345 	simple_mtx_unlock(&etna_drm_table_lock);
346 }
347 
348 /* get the global flink/DRI2 buffer name */
etna_bo_get_name(struct etna_bo * bo,uint32_t * name)349 int etna_bo_get_name(struct etna_bo *bo, uint32_t *name)
350 {
351 	if (!bo->name) {
352 		struct drm_gem_flink req = {
353 			.handle = bo->handle,
354 		};
355 		int ret;
356 
357 		ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
358 		if (ret) {
359 			return ret;
360 		}
361 
362 		simple_mtx_lock(&etna_drm_table_lock);
363 		set_name(bo, req.name);
364 		simple_mtx_unlock(&etna_drm_table_lock);
365 		bo->reuse = 0;
366 	}
367 
368 	*name = bo->name;
369 
370 	return 0;
371 }
372 
etna_bo_handle(struct etna_bo * bo)373 uint32_t etna_bo_handle(struct etna_bo *bo)
374 {
375 	return bo->handle;
376 }
377 
378 /* caller owns the dmabuf fd that is returned and is responsible
379  * to close() it when done
380  */
etna_bo_dmabuf(struct etna_bo * bo)381 int etna_bo_dmabuf(struct etna_bo *bo)
382 {
383 	int ret, prime_fd;
384 
385 	ret = drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC,
386 				&prime_fd);
387 	if (ret) {
388 		ERROR_MSG("failed to get dmabuf fd: %d", ret);
389 		return ret;
390 	}
391 
392 	bo->reuse = 0;
393 
394 	return prime_fd;
395 }
396 
etna_bo_size(struct etna_bo * bo)397 uint32_t etna_bo_size(struct etna_bo *bo)
398 {
399 	return bo->size;
400 }
401 
etna_bo_gpu_va(struct etna_bo * bo)402 uint32_t etna_bo_gpu_va(struct etna_bo *bo)
403 {
404 	return bo->va;
405 }
406 
etna_bo_map(struct etna_bo * bo)407 void *etna_bo_map(struct etna_bo *bo)
408 {
409 	if (!bo->map) {
410 		if (!bo->offset) {
411 			get_buffer_info(bo);
412 		}
413 
414 		bo->map = os_mmap(0, bo->size, PROT_READ | PROT_WRITE,
415 				  MAP_SHARED, bo->dev->fd, bo->offset);
416 		if (bo->map == MAP_FAILED) {
417 			ERROR_MSG("mmap failed: %s", strerror(errno));
418 			bo->map = NULL;
419 		}
420 	}
421 
422 	return bo->map;
423 }
424 
etna_bo_cpu_prep(struct etna_bo * bo,uint32_t op)425 int etna_bo_cpu_prep(struct etna_bo *bo, uint32_t op)
426 {
427 	struct drm_etnaviv_gem_cpu_prep req = {
428 		.handle = bo->handle,
429 		.op = op,
430 	};
431 
432 	get_abs_timeout(&req.timeout, 5000000000);
433 
434 	return drmCommandWrite(bo->dev->fd, DRM_ETNAVIV_GEM_CPU_PREP,
435 			&req, sizeof(req));
436 }
437 
etna_bo_cpu_fini(struct etna_bo * bo)438 void etna_bo_cpu_fini(struct etna_bo *bo)
439 {
440 	struct drm_etnaviv_gem_cpu_fini req = {
441 		.handle = bo->handle,
442 	};
443 
444 	drmCommandWrite(bo->dev->fd, DRM_ETNAVIV_GEM_CPU_FINI,
445 			&req, sizeof(req));
446 }
447